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;
51 static void destroy_route(gpointer user_data)
53 struct ov_route *route = user_data;
56 g_free(route->netmask);
57 g_free(route->gateway);
61 static void ov_provider_append_routes(gpointer key, gpointer value,
64 struct ov_route *route = value;
65 struct connman_provider *provider = user_data;
67 connman_provider_append_route(provider, route->host, route->netmask,
71 static struct ov_route *ov_route_lookup(const char *key, const char *prefix_key,
77 struct ov_route *route;
79 if (g_str_has_prefix(key, prefix_key) == FALSE)
82 start = key + strlen(prefix_key);
83 idx = g_ascii_strtoull(start, &end, 10);
85 if (idx == 0 && start == end) {
86 connman_error("string conversion failed %s", start);
90 route = g_hash_table_lookup(routes, GINT_TO_POINTER(idx));
92 route = g_try_new0(struct ov_route, 1);
94 connman_error("out of memory");
98 g_hash_table_replace(routes, GINT_TO_POINTER(idx),
105 static void ov_append_route(const char *key, const char *value, GHashTable *routes)
107 struct ov_route *route;
110 * OpenVPN pushes routing tupples (host, nw, gw) as several
111 * environment values, e.g.
113 * route_gateway_2 = 10.242.2.13
114 * route_netmask_2 = 255.255.0.0
115 * route_network_2 = 192.168.0.0
116 * route_gateway_1 = 10.242.2.13
117 * route_netmask_1 = 255.255.255.255
118 * route_network_1 = 10.242.2.1
120 * The hash table is used to group the separate environment
121 * variables together. It also makes sure all tupples are
122 * complete even when OpenVPN pushes the information in a
123 * wrong order (unlikely).
126 route = ov_route_lookup(key, "route_network_", routes);
128 route->host = g_strdup(value);
132 route = ov_route_lookup(key, "route_netmask_", routes);
134 route->netmask = g_strdup(value);
138 route = ov_route_lookup(key, "route_gateway_", routes);
140 route->gateway = g_strdup(value);
143 static void ov_append_dns_entries(const char *key, const char *value,
148 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
151 options = g_strsplit(value, " ", 3);
152 if (options[0] != NULL &&
153 !strcmp(options[0], "dhcp-option") &&
154 options[1] != NULL &&
155 !strcmp(options[1], "DNS") &&
156 options[2] != NULL) {
158 if (*dns_entries != NULL) {
161 tmp = g_strjoin(" ", *dns_entries,
163 g_free(*dns_entries);
166 *dns_entries = g_strdup(options[2]);
173 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
175 DBusMessageIter iter, dict;
176 const char *reason, *key, *value;
177 const char *domain = NULL;
178 char *dns_entries = NULL;
181 dbus_message_iter_init(msg, &iter);
183 dbus_message_iter_get_basic(&iter, &reason);
184 dbus_message_iter_next(&iter);
186 dbus_message_iter_init(msg, &iter);
188 dbus_message_iter_get_basic(&iter, &reason);
189 dbus_message_iter_next(&iter);
192 connman_error("No provider found");
193 return VPN_STATE_FAILURE;
196 if (strcmp(reason, "up"))
197 return VPN_STATE_DISCONNECT;
199 domain = connman_provider_get_string(provider, "VPN.Domain");
201 dbus_message_iter_recurse(&iter, &dict);
203 routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
204 NULL, destroy_route);
206 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
207 DBusMessageIter entry;
209 dbus_message_iter_recurse(&dict, &entry);
210 dbus_message_iter_get_basic(&entry, &key);
211 dbus_message_iter_next(&entry);
212 dbus_message_iter_get_basic(&entry, &value);
214 DBG("%s = %s", key, value);
216 if (!strcmp(key, "trusted_ip"))
217 connman_provider_set_string(provider, "Gateway", value);
219 if (!strcmp(key, "ifconfig_local"))
220 connman_provider_set_string(provider, "Address", value);
222 if (!strcmp(key, "ifconfig_remote"))
223 connman_provider_set_string(provider, "Peer", value);
225 ov_append_route(key, value, routes);
227 ov_append_dns_entries(key, value, &dns_entries);
229 dbus_message_iter_next(&dict);
232 if (dns_entries != NULL) {
233 connman_provider_set_string(provider, "DNS", dns_entries);
237 g_hash_table_foreach(routes, ov_provider_append_routes, provider);
239 g_hash_table_destroy(routes);
241 return VPN_STATE_CONNECT;
244 static int ov_connect(struct connman_provider *provider,
245 struct connman_task *task, const char *if_name)
247 const char *vpnhost, *cafile, *mtu, *certfile, *keyfile;
248 const char *proto, *port, *auth_user_pass;
249 const char *tls_remote, *cipher, *auth, *comp_lzo;
252 vpnhost = connman_provider_get_string(provider, "Host");
254 connman_error("Host not set; cannot enable VPN");
258 cafile = connman_provider_get_string(provider, "OpenVPN.CACert");
259 certfile = connman_provider_get_string(provider, "OpenVPN.Cert");
260 keyfile = connman_provider_get_string(provider, "OpenVPN.Key");
261 mtu = connman_provider_get_string(provider, "VPN.MTU");
262 proto = connman_provider_get_string(provider, "OpenVPN.Proto");
263 port = connman_provider_get_string(provider, "OpenVPN.Port");
264 auth_user_pass = connman_provider_get_string(provider,
265 "OpenVPN.AuthUserPass");
266 tls_remote = connman_provider_get_string(provider, "OpenVPN.TLSRemote");
267 cipher = connman_provider_get_string(provider, "OpenVPN.Cipher");
268 auth = connman_provider_get_string(provider, "OpenVPN.Auth");
269 comp_lzo = connman_provider_get_string(provider, "OpenVPN.CompLZO");
272 connman_task_add_argument(task, "--mtu", (char *)mtu);
275 connman_task_add_argument(task, "--proto", (char *)proto);
278 connman_task_add_argument(task, "--port", (char *)port);
280 if (auth_user_pass != NULL) {
281 connman_task_add_argument(task, "--auth-user-pass",
282 (char *)auth_user_pass);
285 if (tls_remote != NULL) {
286 connman_task_add_argument(task, "--tls-remote",
291 connman_task_add_argument(task, "--cipher", (char *)cipher);
294 connman_task_add_argument(task, "--auth", (char *)auth);
297 connman_task_add_argument(task, "--comp-lzo", (char *)comp_lzo);
299 connman_task_add_argument(task, "--syslog", NULL);
301 connman_task_add_argument(task, "--script-security", "2");
303 connman_task_add_argument(task, "--up",
304 SCRIPTDIR "/openvpn-script");
305 connman_task_add_argument(task, "--up-restart", NULL);
307 connman_task_add_argument(task, "--setenv", NULL);
308 connman_task_add_argument(task, "CONNMAN_BUSNAME",
309 dbus_bus_get_unique_name(connection));
311 connman_task_add_argument(task, "--setenv", NULL);
312 connman_task_add_argument(task, "CONNMAN_INTERFACE",
313 CONNMAN_TASK_INTERFACE);
315 connman_task_add_argument(task, "--setenv", NULL);
316 connman_task_add_argument(task, "CONNMAN_PATH",
317 connman_task_get_path(task));
319 connman_task_add_argument(task, "--dev", if_name);
320 connman_task_add_argument(task, "--dev-type", "tun");
322 connman_task_add_argument(task, "--tls-client", NULL);
323 connman_task_add_argument(task, "--remote", (char *)vpnhost);
324 connman_task_add_argument(task, "--nobind", NULL);
325 connman_task_add_argument(task, "--persist-key", NULL);
326 connman_task_add_argument(task, "--persist-tun", NULL);
328 connman_task_add_argument(task, "--route-noexec", NULL);
329 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
332 * Disable client restarts because we can't handle this at the
333 * moment. The problem is that when OpenVPN decides to switch
334 * from CONNECTED state to RECONNECTING and then to RESOLVE,
335 * it is not possible to do a DNS lookup. The DNS server is
336 * not accessable through the tunnel anymore and so we end up
337 * trying to resolve the OpenVPN servers address.
339 connman_task_add_argument(task, "--ping-restart", "0");
341 connman_task_add_argument(task, "--client", NULL);
344 connman_task_add_argument(task, "--ca",
349 connman_task_add_argument(task, "--cert",
354 connman_task_add_argument(task, "--key",
359 err = connman_task_run(task, vpn_died, provider,
362 connman_error("openvpn failed to start");
369 static struct vpn_driver vpn_driver = {
371 .connect = ov_connect,
374 static int openvpn_init(void)
376 connection = connman_dbus_get_connection();
378 return vpn_register("openvpn", &vpn_driver, OPENVPN);
381 static void openvpn_exit(void)
383 vpn_unregister("openvpn");
385 dbus_connection_unref(connection);
388 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
389 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)