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 static DBusConnection *connection;
52 static void destroy_route(gpointer user_data)
54 struct ov_route *route = user_data;
57 g_free(route->netmask);
58 g_free(route->gateway);
62 static void ov_provider_append_routes(gpointer key, gpointer value,
65 struct ov_route *route = value;
66 struct connman_provider *provider = user_data;
68 connman_provider_append_route(provider, route->family, route->host,
69 route->netmask, route->gateway);
72 static struct ov_route *ov_route_lookup(const char *key, const char *prefix_key,
78 struct ov_route *route;
80 if (g_str_has_prefix(key, prefix_key) == FALSE)
83 start = key + strlen(prefix_key);
84 idx = g_ascii_strtoull(start, &end, 10);
86 if (idx == 0 && start == end) {
87 connman_error("string conversion failed %s", start);
91 route = g_hash_table_lookup(routes, GINT_TO_POINTER(idx));
93 route = g_try_new0(struct ov_route, 1);
95 connman_error("out of memory");
99 route->family = AF_INET;
101 g_hash_table_replace(routes, GINT_TO_POINTER(idx),
108 static void ov_append_route(const char *key, const char *value, GHashTable *routes)
110 struct ov_route *route;
113 * OpenVPN pushes routing tupples (host, nw, gw) as several
114 * environment values, e.g.
116 * route_gateway_2 = 10.242.2.13
117 * route_netmask_2 = 255.255.0.0
118 * route_network_2 = 192.168.0.0
119 * route_gateway_1 = 10.242.2.13
120 * route_netmask_1 = 255.255.255.255
121 * route_network_1 = 10.242.2.1
123 * The hash table is used to group the separate environment
124 * variables together. It also makes sure all tupples are
125 * complete even when OpenVPN pushes the information in a
126 * wrong order (unlikely).
129 route = ov_route_lookup(key, "route_network_", routes);
131 route->host = g_strdup(value);
135 route = ov_route_lookup(key, "route_netmask_", routes);
137 route->netmask = g_strdup(value);
141 route = ov_route_lookup(key, "route_gateway_", routes);
143 route->gateway = g_strdup(value);
146 static void ov_append_dns_entries(const char *key, const char *value,
151 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
154 options = g_strsplit(value, " ", 3);
155 if (options[0] != NULL &&
156 !strcmp(options[0], "dhcp-option") &&
157 options[1] != NULL &&
158 !strcmp(options[1], "DNS") &&
159 options[2] != NULL) {
161 if (*dns_entries != NULL) {
164 tmp = g_strjoin(" ", *dns_entries,
166 g_free(*dns_entries);
169 *dns_entries = g_strdup(options[2]);
176 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
178 DBusMessageIter iter, dict;
179 const char *reason, *key, *value;
180 const char *domain = NULL;
181 char *dns_entries = NULL;
184 dbus_message_iter_init(msg, &iter);
186 dbus_message_iter_get_basic(&iter, &reason);
187 dbus_message_iter_next(&iter);
190 connman_error("No provider found");
191 return VPN_STATE_FAILURE;
194 if (strcmp(reason, "up"))
195 return VPN_STATE_DISCONNECT;
197 domain = connman_provider_get_string(provider, "VPN.Domain");
199 dbus_message_iter_recurse(&iter, &dict);
201 routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
202 NULL, destroy_route);
204 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
205 DBusMessageIter entry;
207 dbus_message_iter_recurse(&dict, &entry);
208 dbus_message_iter_get_basic(&entry, &key);
209 dbus_message_iter_next(&entry);
210 dbus_message_iter_get_basic(&entry, &value);
212 DBG("%s = %s", key, value);
214 if (!strcmp(key, "trusted_ip"))
215 connman_provider_set_string(provider, "Gateway", value);
217 if (!strcmp(key, "ifconfig_local"))
218 connman_provider_set_string(provider, "Address", value);
220 if (!strcmp(key, "ifconfig_remote"))
221 connman_provider_set_string(provider, "Peer", value);
223 ov_append_route(key, value, routes);
225 ov_append_dns_entries(key, value, &dns_entries);
227 dbus_message_iter_next(&dict);
230 if (dns_entries != NULL) {
231 connman_provider_set_string(provider, "DNS", dns_entries);
235 g_hash_table_foreach(routes, ov_provider_append_routes, provider);
237 g_hash_table_destroy(routes);
239 return VPN_STATE_CONNECT;
242 static int ov_connect(struct connman_provider *provider,
243 struct connman_task *task, const char *if_name)
245 const char *vpnhost, *cafile, *mtu, *certfile, *keyfile;
246 const char *proto, *port, *auth_user_pass;
247 const char *tls_remote, *cipher, *auth, *comp_lzo;
250 vpnhost = connman_provider_get_string(provider, "Host");
252 connman_error("Host not set; cannot enable VPN");
256 cafile = connman_provider_get_string(provider, "OpenVPN.CACert");
257 certfile = connman_provider_get_string(provider, "OpenVPN.Cert");
258 keyfile = connman_provider_get_string(provider, "OpenVPN.Key");
259 mtu = connman_provider_get_string(provider, "VPN.MTU");
260 proto = connman_provider_get_string(provider, "OpenVPN.Proto");
261 port = connman_provider_get_string(provider, "OpenVPN.Port");
262 auth_user_pass = connman_provider_get_string(provider,
263 "OpenVPN.AuthUserPass");
264 tls_remote = connman_provider_get_string(provider, "OpenVPN.TLSRemote");
265 cipher = connman_provider_get_string(provider, "OpenVPN.Cipher");
266 auth = connman_provider_get_string(provider, "OpenVPN.Auth");
267 comp_lzo = connman_provider_get_string(provider, "OpenVPN.CompLZO");
270 connman_task_add_argument(task, "--mtu", (char *)mtu);
273 connman_task_add_argument(task, "--proto", (char *)proto);
276 connman_task_add_argument(task, "--port", (char *)port);
278 if (auth_user_pass != NULL) {
279 connman_task_add_argument(task, "--auth-user-pass",
280 (char *)auth_user_pass);
283 if (tls_remote != NULL) {
284 connman_task_add_argument(task, "--tls-remote",
289 connman_task_add_argument(task, "--cipher", (char *)cipher);
292 connman_task_add_argument(task, "--auth", (char *)auth);
295 connman_task_add_argument(task, "--comp-lzo", (char *)comp_lzo);
297 connman_task_add_argument(task, "--syslog", NULL);
299 connman_task_add_argument(task, "--script-security", "2");
301 connman_task_add_argument(task, "--up",
302 SCRIPTDIR "/openvpn-script");
303 connman_task_add_argument(task, "--up-restart", NULL);
305 connman_task_add_argument(task, "--setenv", NULL);
306 connman_task_add_argument(task, "CONNMAN_BUSNAME",
307 dbus_bus_get_unique_name(connection));
309 connman_task_add_argument(task, "--setenv", NULL);
310 connman_task_add_argument(task, "CONNMAN_INTERFACE",
311 CONNMAN_TASK_INTERFACE);
313 connman_task_add_argument(task, "--setenv", NULL);
314 connman_task_add_argument(task, "CONNMAN_PATH",
315 connman_task_get_path(task));
317 connman_task_add_argument(task, "--dev", if_name);
318 connman_task_add_argument(task, "--dev-type", "tun");
320 connman_task_add_argument(task, "--tls-client", NULL);
321 connman_task_add_argument(task, "--remote", (char *)vpnhost);
322 connman_task_add_argument(task, "--nobind", NULL);
323 connman_task_add_argument(task, "--persist-key", NULL);
324 connman_task_add_argument(task, "--persist-tun", NULL);
326 connman_task_add_argument(task, "--route-noexec", NULL);
327 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
330 * Disable client restarts because we can't handle this at the
331 * moment. The problem is that when OpenVPN decides to switch
332 * from CONNECTED state to RECONNECTING and then to RESOLVE,
333 * it is not possible to do a DNS lookup. The DNS server is
334 * not accessable through the tunnel anymore and so we end up
335 * trying to resolve the OpenVPN servers address.
337 connman_task_add_argument(task, "--ping-restart", "0");
339 connman_task_add_argument(task, "--client", NULL);
342 connman_task_add_argument(task, "--ca",
347 connman_task_add_argument(task, "--cert",
352 connman_task_add_argument(task, "--key",
357 err = connman_task_run(task, vpn_died, provider,
360 connman_error("openvpn failed to start");
367 static struct vpn_driver vpn_driver = {
369 .connect = ov_connect,
372 static int openvpn_init(void)
374 connection = connman_dbus_get_connection();
376 return vpn_register("openvpn", &vpn_driver, OPENVPN);
379 static void openvpn_exit(void)
381 vpn_unregister("openvpn");
383 dbus_connection_unref(connection);
386 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
387 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)