5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/provider.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/dbus.h>
43 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
45 static DBusConnection *connection;
51 { "Host", "--remote" },
52 { "OpenVPN.CACert", "--ca" },
53 { "OpenVPN.Cert", "--cert" },
54 { "OpenVPN.Key", "--key" },
55 { "OpenVPN.MTU", "--mtu" },
56 { "OpenVPN.Proto", "--proto" },
57 { "OpenVPN.Port", "--port" },
58 { "OpenVPN.AuthUserPass", "--auth-user-pass" },
59 { "OpenVPN.TLSRemote", "--tls-remote" },
60 { "OpenVPN.Cipher", "--cipher" },
61 { "OpenVPN.Auth", "--auth" },
62 { "OpenVPN.CompLZO", "--comp-lzo" },
65 static void ov_append_dns_entries(const char *key, const char *value,
70 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
73 options = g_strsplit(value, " ", 3);
74 if (options[0] != NULL &&
75 !strcmp(options[0], "dhcp-option") &&
77 !strcmp(options[1], "DNS") &&
80 if (*dns_entries != NULL) {
83 tmp = g_strjoin(" ", *dns_entries,
88 *dns_entries = g_strdup(options[2]);
95 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
97 DBusMessageIter iter, dict;
98 const char *reason, *key, *value;
99 const char *domain = NULL;
100 char *dns_entries = NULL;
102 dbus_message_iter_init(msg, &iter);
104 dbus_message_iter_get_basic(&iter, &reason);
105 dbus_message_iter_next(&iter);
108 connman_error("No provider found");
109 return VPN_STATE_FAILURE;
112 if (strcmp(reason, "up"))
113 return VPN_STATE_DISCONNECT;
115 domain = connman_provider_get_string(provider, "VPN.Domain");
117 dbus_message_iter_recurse(&iter, &dict);
119 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
120 DBusMessageIter entry;
122 dbus_message_iter_recurse(&dict, &entry);
123 dbus_message_iter_get_basic(&entry, &key);
124 dbus_message_iter_next(&entry);
125 dbus_message_iter_get_basic(&entry, &value);
127 DBG("%s = %s", key, value);
129 if (!strcmp(key, "trusted_ip"))
130 connman_provider_set_string(provider, "Gateway", value);
132 if (!strcmp(key, "ifconfig_local"))
133 connman_provider_set_string(provider, "Address", value);
135 if (!strcmp(key, "ifconfig_remote"))
136 connman_provider_set_string(provider, "Peer", value);
138 if (g_str_has_prefix(key, "route_") == TRUE)
139 connman_provider_append_route(provider, key, value);
141 ov_append_dns_entries(key, value, &dns_entries);
143 dbus_message_iter_next(&dict);
146 if (dns_entries != NULL) {
147 connman_provider_set_string(provider, "DNS", dns_entries);
151 return VPN_STATE_CONNECT;
154 static int task_append_config_data(struct connman_provider *provider,
155 struct connman_task *task)
160 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
161 option = connman_provider_get_string(provider,
162 ov_options[i].cm_opt);
166 if (connman_task_add_argument(task,
167 ov_options[i].ov_opt, option) < 0) {
175 static int ov_connect(struct connman_provider *provider,
176 struct connman_task *task, const char *if_name)
181 option = connman_provider_get_string(provider, "Host");
182 if (option == NULL) {
183 connman_error("Host not set; cannot enable VPN");
187 task_append_config_data(provider, task);
189 connman_task_add_argument(task, "--syslog", NULL);
191 connman_task_add_argument(task, "--script-security", "2");
193 connman_task_add_argument(task, "--up",
194 SCRIPTDIR "/openvpn-script");
195 connman_task_add_argument(task, "--up-restart", NULL);
197 connman_task_add_argument(task, "--setenv", NULL);
198 connman_task_add_argument(task, "CONNMAN_BUSNAME",
199 dbus_bus_get_unique_name(connection));
201 connman_task_add_argument(task, "--setenv", NULL);
202 connman_task_add_argument(task, "CONNMAN_INTERFACE",
203 CONNMAN_TASK_INTERFACE);
205 connman_task_add_argument(task, "--setenv", NULL);
206 connman_task_add_argument(task, "CONNMAN_PATH",
207 connman_task_get_path(task));
209 connman_task_add_argument(task, "--dev", if_name);
210 connman_task_add_argument(task, "--dev-type", "tun");
212 connman_task_add_argument(task, "--tls-client", NULL);
213 connman_task_add_argument(task, "--nobind", NULL);
214 connman_task_add_argument(task, "--persist-key", NULL);
215 connman_task_add_argument(task, "--persist-tun", NULL);
217 connman_task_add_argument(task, "--route-noexec", NULL);
218 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
221 * Disable client restarts because we can't handle this at the
222 * moment. The problem is that when OpenVPN decides to switch
223 * from CONNECTED state to RECONNECTING and then to RESOLVE,
224 * it is not possible to do a DNS lookup. The DNS server is
225 * not accessable through the tunnel anymore and so we end up
226 * trying to resolve the OpenVPN servers address.
228 connman_task_add_argument(task, "--ping-restart", "0");
230 connman_task_add_argument(task, "--client", NULL);
233 err = connman_task_run(task, vpn_died, provider,
236 connman_error("openvpn failed to start");
243 static struct vpn_driver vpn_driver = {
245 .connect = ov_connect,
248 static int openvpn_init(void)
250 connection = connman_dbus_get_connection();
252 return vpn_register("openvpn", &vpn_driver, OPENVPN);
255 static void openvpn_exit(void)
257 vpn_unregister("openvpn");
259 dbus_connection_unref(connection);
262 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
263 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)