5 * Copyright (C) 2012 Intel Corporation. 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
31 #include <connman/log.h>
32 #include <gweb/gresolv.h>
35 #include "../src/connman.h"
36 #include "connman/agent.h"
37 #include "connman/vpn-dbus.h"
38 #include "vpn-provider.h"
42 USER_ROUTES_CHANGED = 0x01,
43 SERVER_ROUTES_CHANGED = 0x02,
46 static DBusConnection *connection;
47 static GHashTable *provider_hash;
48 static GSList *driver_list;
49 static int configuration_count;
50 static gboolean handle_routes;
63 enum vpn_provider_state state;
72 struct vpn_provider_driver *driver;
74 GHashTable *setting_strings;
75 GHashTable *user_routes;
76 GSList *user_networks;
79 struct vpn_ipconfig *ipconfig_ipv4;
80 struct vpn_ipconfig *ipconfig_ipv6;
86 static void free_route(gpointer data)
88 struct vpn_route *route = data;
90 g_free(route->network);
91 g_free(route->netmask);
92 g_free(route->gateway);
97 static void append_route(DBusMessageIter *iter, void *user_data)
99 struct vpn_route *route = user_data;
100 DBusMessageIter item;
103 connman_dbus_dict_open(iter, &item);
108 if (route->family == AF_INET)
110 else if (route->family == AF_INET6)
114 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
115 DBUS_TYPE_INT32, &family);
117 if (route->network != NULL)
118 connman_dbus_dict_append_basic(&item, "Network",
119 DBUS_TYPE_STRING, &route->network);
121 if (route->netmask != NULL)
122 connman_dbus_dict_append_basic(&item, "Netmask",
123 DBUS_TYPE_STRING, &route->netmask);
125 if (route->gateway != NULL)
126 connman_dbus_dict_append_basic(&item, "Gateway",
127 DBUS_TYPE_STRING, &route->gateway);
130 connman_dbus_dict_close(iter, &item);
133 static void append_routes(DBusMessageIter *iter, void *user_data)
135 GHashTable *routes = user_data;
139 if (routes == NULL) {
140 append_route(iter, NULL);
144 g_hash_table_iter_init(&hash, routes);
146 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
147 DBusMessageIter dict;
149 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
151 append_route(&dict, value);
152 dbus_message_iter_close_container(iter, &dict);
156 static void send_routes(struct vpn_provider *provider, GHashTable *routes,
159 connman_dbus_property_changed_array(provider->path,
160 VPN_CONNECTION_INTERFACE,
162 DBUS_TYPE_DICT_ENTRY,
167 static int provider_property_changed(struct vpn_provider *provider,
170 DBG("provider %p name %s", provider, name);
172 if (g_str_equal(name, "UserRoutes") == TRUE)
173 send_routes(provider, provider->user_routes, name);
174 else if (g_str_equal(name, "ServerRoutes") == TRUE)
175 send_routes(provider, provider->routes, name);
180 static GSList *read_route_dict(GSList *routes, DBusMessageIter *dicts)
182 DBusMessageIter dict, value, entry;
183 const char *network, *netmask, *gateway;
184 struct vpn_route *route;
188 dbus_message_iter_recurse(dicts, &entry);
190 network = netmask = gateway = NULL;
193 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_DICT_ENTRY) {
195 dbus_message_iter_recurse(&entry, &dict);
196 dbus_message_iter_get_basic(&dict, &key);
198 dbus_message_iter_next(&dict);
199 dbus_message_iter_recurse(&dict, &value);
201 type = dbus_message_iter_get_arg_type(&value);
204 case DBUS_TYPE_STRING:
205 if (g_str_equal(key, "ProtocolFamily") == TRUE)
206 dbus_message_iter_get_basic(&value, &family);
207 else if (g_str_equal(key, "Network") == TRUE)
208 dbus_message_iter_get_basic(&value, &network);
209 else if (g_str_equal(key, "Netmask") == TRUE)
210 dbus_message_iter_get_basic(&value, &netmask);
211 else if (g_str_equal(key, "Gateway") == TRUE)
212 dbus_message_iter_get_basic(&value, &gateway);
216 dbus_message_iter_next(&entry);
219 DBG("family %d network %s netmask %s gateway %s", family,
220 network, netmask, gateway);
222 if (network == NULL || netmask == NULL) {
223 DBG("Ignoring route as network/netmask is missing");
227 route = g_try_new(struct vpn_route, 1);
229 g_slist_free_full(routes, free_route);
233 if (family == PF_UNSPEC) {
234 family = connman_inet_check_ipaddress(network);
236 DBG("Cannot get address family of %s (%d/%s)", network,
237 family, gai_strerror(family));
238 if (strstr(network, ":") != NULL) {
239 DBG("Guessing it is IPv6");
242 DBG("Guessing it is IPv4");
260 route->family = family;
261 route->network = g_strdup(network);
262 route->netmask = g_strdup(netmask);
263 route->gateway = g_strdup(gateway);
265 routes = g_slist_prepend(routes, route);
269 static GSList *get_user_networks(DBusMessageIter *array)
271 DBusMessageIter entry;
274 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
276 dbus_message_iter_recurse(array, &entry);
278 while (dbus_message_iter_get_arg_type(&entry) ==
280 DBusMessageIter dicts;
282 dbus_message_iter_recurse(&entry, &dicts);
284 while (dbus_message_iter_get_arg_type(&dicts) ==
287 list = read_route_dict(list, &dicts);
288 dbus_message_iter_next(&dicts);
291 dbus_message_iter_next(&entry);
294 dbus_message_iter_next(array);
300 static void set_user_networks(struct vpn_provider *provider, GSList *networks)
304 for (list = networks; list != NULL; list = g_slist_next(list)) {
305 struct vpn_route *route= list->data;
307 if (__vpn_provider_append_user_route(provider,
308 route->family, route->network,
309 route->netmask) != 0)
314 static void del_routes(struct vpn_provider *provider)
319 g_hash_table_iter_init(&hash, provider->user_routes);
320 while (handle_routes == TRUE && g_hash_table_iter_next(&hash,
321 &key, &value) == TRUE) {
322 struct vpn_route *route = value;
323 if (route->family == AF_INET6) {
324 unsigned char prefixlen = atoi(route->netmask);
325 connman_inet_del_ipv6_network_route(provider->index,
329 connman_inet_del_host_route(provider->index,
333 g_hash_table_remove_all(provider->user_routes);
334 g_slist_free_full(provider->user_networks, free_route);
335 provider->user_networks = NULL;
338 static gboolean provider_send_changed(gpointer data)
340 struct vpn_provider *provider = data;
342 if (provider->what_changed & USER_ROUTES_CHANGED)
343 provider_property_changed(provider, "UserRoutes");
345 if (provider->what_changed & SERVER_ROUTES_CHANGED)
346 provider_property_changed(provider, "ServerRoutes");
348 provider->what_changed = 0;
349 provider->notify_id = 0;
354 static void provider_schedule_changed(struct vpn_provider *provider, int flag)
356 if (provider->notify_id != 0)
357 g_source_remove(provider->notify_id);
359 provider->what_changed |= flag;
361 provider->notify_id = g_timeout_add(100, provider_send_changed,
365 static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
368 struct vpn_provider *provider = data;
369 DBusMessageIter iter, value;
373 DBG("conn %p", conn);
375 if (dbus_message_iter_init(msg, &iter) == FALSE)
376 return __connman_error_invalid_arguments(msg);
378 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
379 return __connman_error_invalid_arguments(msg);
381 dbus_message_iter_get_basic(&iter, &name);
382 dbus_message_iter_next(&iter);
384 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
385 return __connman_error_invalid_arguments(msg);
387 dbus_message_iter_recurse(&iter, &value);
389 type = dbus_message_iter_get_arg_type(&value);
391 if (g_str_equal(name, "UserRoutes") == TRUE) {
394 if (type != DBUS_TYPE_ARRAY)
395 return __connman_error_invalid_arguments(msg);
397 networks = get_user_networks(&value);
398 if (networks != NULL) {
399 del_routes(provider);
400 provider->user_networks = networks;
401 set_user_networks(provider, provider->user_networks);
403 if (handle_routes == FALSE)
404 provider_schedule_changed(provider,
405 USER_ROUTES_CHANGED);
408 return __connman_error_invalid_property(msg);
410 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
413 static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
416 struct vpn_provider *provider = data;
419 DBG("conn %p", conn);
421 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
424 if (g_str_equal(name, "UserRoutes") == TRUE) {
425 del_routes(provider);
427 if (handle_routes == FALSE)
428 provider_property_changed(provider, name);
430 return __connman_error_invalid_property(msg);
433 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
436 static DBusMessage *do_connect(DBusConnection *conn, DBusMessage *msg,
439 struct vpn_provider *provider = data;
442 DBG("conn %p provider %p", conn, provider);
444 err = __vpn_provider_connect(provider, msg);
446 return __connman_error_failed(msg, -err);
451 static DBusMessage *do_disconnect(DBusConnection *conn, DBusMessage *msg,
454 struct vpn_provider *provider = data;
457 DBG("conn %p provider %p", conn, provider);
459 err = __vpn_provider_disconnect(provider);
460 if (err < 0 && err != -EINPROGRESS)
461 return __connman_error_failed(msg, -err);
463 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
466 static const GDBusMethodTable connection_methods[] = {
467 { GDBUS_METHOD("SetProperty",
468 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
469 NULL, set_property) },
470 { GDBUS_METHOD("ClearProperty",
471 GDBUS_ARGS({ "name", "s" }), NULL,
473 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, do_connect) },
474 { GDBUS_METHOD("Disconnect", NULL, NULL, do_disconnect) },
478 static const GDBusSignalTable connection_signals[] = {
479 { GDBUS_SIGNAL("PropertyChanged",
480 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
484 static void resolv_result(GResolvResultStatus status,
485 char **results, gpointer user_data)
487 struct vpn_provider *provider = user_data;
489 DBG("status %d", status);
491 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
492 g_strv_length(results) > 0)
493 provider->host_ip = g_strdupv(results);
495 vpn_provider_unref(provider);
498 static void provider_resolv_host_addr(struct vpn_provider *provider)
500 if (provider->host == NULL)
503 if (connman_inet_check_ipaddress(provider->host) > 0)
506 if (provider->host_ip != NULL)
510 * If the hostname is not numeric, try to resolv it. We do not wait
511 * the result as it might take some time. We will get the result
512 * before VPN will feed routes to us because VPN client will need
513 * the IP address also before VPN connection can be established.
515 provider->resolv = g_resolv_new(0);
516 if (provider->resolv == NULL) {
517 DBG("Cannot resolv %s", provider->host);
521 DBG("Trying to resolv %s", provider->host);
523 vpn_provider_ref(provider);
525 g_resolv_lookup_hostname(provider->resolv, provider->host,
526 resolv_result, provider);
529 void __vpn_provider_append_properties(struct vpn_provider *provider,
530 DBusMessageIter *iter)
532 if (provider->host != NULL)
533 connman_dbus_dict_append_basic(iter, "Host",
534 DBUS_TYPE_STRING, &provider->host);
536 if (provider->domain != NULL)
537 connman_dbus_dict_append_basic(iter, "Domain",
538 DBUS_TYPE_STRING, &provider->domain);
540 if (provider->type != NULL)
541 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
545 int __vpn_provider_append_user_route(struct vpn_provider *provider,
546 int family, const char *network, const char *netmask)
548 struct vpn_route *route;
549 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
551 DBG("family %d network %s netmask %s", family, network, netmask);
553 route = g_hash_table_lookup(provider->user_routes, key);
555 route = g_try_new0(struct vpn_route, 1);
557 connman_error("out of memory");
561 route->family = family;
562 route->network = g_strdup(network);
563 route->netmask = g_strdup(netmask);
565 g_hash_table_replace(provider->user_routes, key, route);
572 static struct vpn_route *get_route(char *route_str)
574 char **elems = g_strsplit(route_str, "/", 0);
575 char *network, *netmask, *gateway, *family_str;
576 int family = PF_UNSPEC;
577 struct vpn_route *route = NULL;
582 family_str = elems[0];
585 if (network == NULL || network[0] == '\0')
589 if (netmask == NULL || netmask[0] == '\0')
594 route = g_try_new0(struct vpn_route, 1);
598 if (family_str[0] == '\0' || atoi(family_str) == 0) {
601 switch (family_str[0]) {
611 if (g_strrstr(network, ":") != NULL) {
612 if (family != PF_UNSPEC && family != AF_INET6)
613 DBG("You have IPv6 address but you have non IPv6 route");
614 } else if (g_strrstr(network, ".") != NULL) {
615 if (family != PF_UNSPEC && family != AF_INET)
616 DBG("You have IPv4 address but you have non IPv4 route");
618 if (g_strrstr(netmask, ".") == NULL) {
619 /* We have netmask length */
621 struct in_addr netmask_in;
622 unsigned char prefix_len = 32;
624 if (netmask != NULL) {
626 long int value = strtol(netmask, &ptr, 10);
627 if (ptr != netmask && *ptr == '\0' &&
632 addr = 0xffffffff << (32 - prefix_len);
633 netmask_in.s_addr = htonl(addr);
634 netmask = inet_ntoa(netmask_in);
636 DBG("network %s netmask %s", network, netmask);
640 if (family == PF_UNSPEC) {
641 family = connman_inet_check_ipaddress(network);
642 if (family < 0 || family == PF_UNSPEC)
646 route->family = family;
647 route->network = g_strdup(network);
648 route->netmask = g_strdup(netmask);
649 route->gateway = g_strdup(gateway);
656 static GSList *get_routes(gchar **networks)
658 struct vpn_route *route;
659 GSList *routes = NULL;
662 for (i = 0; networks[i] != NULL; i++) {
663 route = get_route(networks[i]);
665 routes = g_slist_prepend(routes, route);
671 static int provider_load_from_keyfile(struct vpn_provider *provider,
677 gsize length, num_user_networks;
678 gchar **networks = NULL;
680 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
682 if (settings == NULL) {
683 g_key_file_free(keyfile);
687 while (idx < length) {
690 if (g_str_equal(key, "Networks") == TRUE) {
691 networks = g_key_file_get_string_list(keyfile,
692 provider->identifier,
696 provider->user_networks = get_routes(networks);
699 value = g_key_file_get_string(keyfile,
700 provider->identifier,
702 vpn_provider_set_string(provider, key,
709 g_strfreev(settings);
710 g_strfreev(networks);
712 if (provider->user_networks != NULL)
713 set_user_networks(provider, provider->user_networks);
719 static int vpn_provider_load(struct vpn_provider *provider)
723 DBG("provider %p", provider);
725 keyfile = __connman_storage_load_provider(provider->identifier);
729 provider_load_from_keyfile(provider, keyfile);
731 g_key_file_free(keyfile);
735 static gchar **create_network_list(GSList *networks, gsize *count)
738 gchar **result = NULL;
739 unsigned int num_elems = 0;
741 for (list = networks; list != NULL; list = g_slist_next(list)) {
742 struct vpn_route *route = list->data;
745 result = g_try_realloc(result,
746 (num_elems + 1) * sizeof(gchar *));
750 switch (route->family) {
762 result[num_elems] = g_strdup_printf("%d/%s/%s/%s",
763 family, route->network, route->netmask,
764 route->gateway == NULL ? "" : route->gateway);
769 result = g_try_realloc(result, (num_elems + 1) * sizeof(gchar *));
773 result[num_elems] = NULL;
778 static int vpn_provider_save(struct vpn_provider *provider)
782 DBG("provider %p", provider);
784 keyfile = g_key_file_new();
788 g_key_file_set_string(keyfile, provider->identifier,
789 "Name", provider->name);
790 g_key_file_set_string(keyfile, provider->identifier,
791 "Type", provider->type);
792 g_key_file_set_string(keyfile, provider->identifier,
793 "Host", provider->host);
794 g_key_file_set_string(keyfile, provider->identifier,
795 "VPN.Domain", provider->domain);
796 if (provider->user_networks != NULL) {
800 networks = create_network_list(provider->user_networks,
802 if (networks != NULL) {
803 g_key_file_set_string_list(keyfile,
804 provider->identifier,
806 (const gchar ** const)networks,
808 g_strfreev(networks);
812 if (provider->driver != NULL && provider->driver->save != NULL)
813 provider->driver->save(provider, keyfile);
815 __connman_storage_save_provider(keyfile, provider->identifier);
816 g_key_file_free(keyfile);
821 static struct vpn_provider *vpn_provider_lookup(const char *identifier)
823 struct vpn_provider *provider = NULL;
825 provider = g_hash_table_lookup(provider_hash, identifier);
830 static gboolean match_driver(struct vpn_provider *provider,
831 struct vpn_provider_driver *driver)
833 if (g_strcmp0(driver->name, provider->type) == 0)
839 static int provider_probe(struct vpn_provider *provider)
843 DBG("provider %p name %s", provider, provider->name);
845 if (provider->driver != NULL)
848 for (list = driver_list; list; list = list->next) {
849 struct vpn_provider_driver *driver = list->data;
851 if (match_driver(provider, driver) == FALSE)
854 DBG("driver %p name %s", driver, driver->name);
856 if (driver->probe != NULL && driver->probe(provider) == 0) {
857 provider->driver = driver;
862 if (provider->driver == NULL)
868 static void provider_remove(struct vpn_provider *provider)
870 if (provider->driver != NULL) {
871 provider->driver->remove(provider);
872 provider->driver = NULL;
876 static int provider_register(struct vpn_provider *provider)
878 return provider_probe(provider);
881 static void provider_unregister(struct vpn_provider *provider)
883 provider_remove(provider);
886 struct vpn_provider *
887 vpn_provider_ref_debug(struct vpn_provider *provider,
888 const char *file, int line, const char *caller)
890 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
893 __sync_fetch_and_add(&provider->refcount, 1);
898 static void provider_destruct(struct vpn_provider *provider)
900 DBG("provider %p", provider);
902 if (provider->notify_id != 0)
903 g_source_remove(provider->notify_id);
905 g_free(provider->name);
906 g_free(provider->type);
907 g_free(provider->host);
908 g_free(provider->domain);
909 g_free(provider->identifier);
910 g_free(provider->path);
911 g_slist_free_full(provider->user_networks, free_route);
912 g_strfreev(provider->nameservers);
913 g_hash_table_destroy(provider->routes);
914 g_hash_table_destroy(provider->user_routes);
915 g_hash_table_destroy(provider->setting_strings);
916 if (provider->resolv != NULL) {
917 g_resolv_unref(provider->resolv);
918 provider->resolv = NULL;
920 __vpn_ipconfig_unref(provider->ipconfig_ipv4);
921 __vpn_ipconfig_unref(provider->ipconfig_ipv6);
923 g_strfreev(provider->host_ip);
927 void vpn_provider_unref_debug(struct vpn_provider *provider,
928 const char *file, int line, const char *caller)
930 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
933 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
936 provider_remove(provider);
938 provider_destruct(provider);
941 static void configuration_count_add(void)
943 DBG("count %d", configuration_count + 1);
945 __sync_fetch_and_add(&configuration_count, 1);
948 static void configuration_count_del(void)
950 DBG("count %d", configuration_count - 1);
952 if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
958 int __vpn_provider_disconnect(struct vpn_provider *provider)
962 DBG("provider %p", provider);
964 if (provider->driver != NULL && provider->driver->disconnect != NULL)
965 err = provider->driver->disconnect(provider);
969 if (err == -EINPROGRESS)
970 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
975 static void connect_cb(struct vpn_provider *provider, void *user_data,
978 DBusMessage *pending = user_data;
980 DBG("provider %p user %p error %d", provider, user_data, error);
983 DBusMessage *reply = __connman_error_failed(pending, error);
985 g_dbus_send_message(connection, reply);
987 vpn_provider_indicate_error(provider,
988 VPN_PROVIDER_ERROR_CONNECT_FAILED);
989 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
991 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
993 dbus_message_unref(pending);
996 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
1000 DBG("provider %p", provider);
1002 if (provider->driver != NULL && provider->driver->connect != NULL) {
1003 dbus_message_ref(msg);
1004 err = provider->driver->connect(provider, connect_cb, msg);
1008 if (err == -EINPROGRESS)
1009 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1014 static void connection_removed_signal(struct vpn_provider *provider)
1016 DBusMessage *signal;
1017 DBusMessageIter iter;
1019 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1020 VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1024 dbus_message_iter_init_append(signal, &iter);
1025 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1027 dbus_connection_send(connection, signal, NULL);
1028 dbus_message_unref(signal);
1031 static char *get_ident(const char *path)
1038 pos = strrchr(path, '/');
1045 int __vpn_provider_remove(const char *path)
1047 struct vpn_provider *provider;
1050 DBG("path %s", path);
1052 ident = get_ident(path);
1054 provider = vpn_provider_lookup(ident);
1055 if (provider != NULL) {
1056 DBG("Removing VPN %s", provider->identifier);
1058 connection_removed_signal(provider);
1060 provider_unregister(provider);
1061 g_hash_table_remove(provider_hash, provider->identifier);
1063 __connman_storage_remove_provider(ident);
1070 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1072 struct vpn_provider *provider = user_data;
1073 const char *address, *gateway, *peer;
1075 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1076 if (address != NULL) {
1078 struct in_addr netmask;
1082 prefixlen = __vpn_ipconfig_get_prefixlen(
1083 provider->ipconfig_ipv4);
1085 addr = 0xffffffff << (32 - prefixlen);
1086 netmask.s_addr = htonl(addr);
1087 mask = inet_ntoa(netmask);
1089 connman_dbus_dict_append_basic(iter, "Address",
1090 DBUS_TYPE_STRING, &address);
1092 connman_dbus_dict_append_basic(iter, "Netmask",
1093 DBUS_TYPE_STRING, &mask);
1096 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1097 if (gateway != NULL)
1098 connman_dbus_dict_append_basic(iter, "Gateway",
1099 DBUS_TYPE_STRING, &gateway);
1101 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1103 connman_dbus_dict_append_basic(iter, "Peer",
1104 DBUS_TYPE_STRING, &peer);
1107 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1109 struct vpn_provider *provider = user_data;
1110 const char *address, *gateway, *peer;
1112 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1113 if (address != NULL) {
1114 unsigned char prefixlen;
1116 connman_dbus_dict_append_basic(iter, "Address",
1117 DBUS_TYPE_STRING, &address);
1119 prefixlen = __vpn_ipconfig_get_prefixlen(
1120 provider->ipconfig_ipv6);
1122 connman_dbus_dict_append_basic(iter, "PrefixLength",
1123 DBUS_TYPE_BYTE, &prefixlen);
1126 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1127 if (gateway != NULL)
1128 connman_dbus_dict_append_basic(iter, "Gateway",
1129 DBUS_TYPE_STRING, &gateway);
1131 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1133 connman_dbus_dict_append_basic(iter, "Peer",
1134 DBUS_TYPE_STRING, &peer);
1137 static const char *state2string(enum vpn_provider_state state)
1140 case VPN_PROVIDER_STATE_UNKNOWN:
1142 case VPN_PROVIDER_STATE_IDLE:
1144 case VPN_PROVIDER_STATE_CONNECT:
1145 return "configuration";
1146 case VPN_PROVIDER_STATE_READY:
1148 case VPN_PROVIDER_STATE_DISCONNECT:
1149 return "disconnect";
1150 case VPN_PROVIDER_STATE_FAILURE:
1157 static int provider_indicate_state(struct vpn_provider *provider,
1158 enum vpn_provider_state state)
1162 DBG("provider %p state %d", provider, state);
1164 str = state2string(state);
1168 provider->state = state;
1170 if (state == VPN_PROVIDER_STATE_READY) {
1171 connman_dbus_property_changed_basic(provider->path,
1172 VPN_CONNECTION_INTERFACE, "Index",
1173 DBUS_TYPE_INT32, &provider->index);
1175 if (provider->family == AF_INET)
1176 connman_dbus_property_changed_dict(provider->path,
1177 VPN_CONNECTION_INTERFACE, "IPv4",
1178 append_ipv4, provider);
1179 else if (provider->family == AF_INET6)
1180 connman_dbus_property_changed_dict(provider->path,
1181 VPN_CONNECTION_INTERFACE, "IPv6",
1182 append_ipv6, provider);
1185 connman_dbus_property_changed_basic(provider->path,
1186 VPN_CONNECTION_INTERFACE, "State",
1187 DBUS_TYPE_STRING, &str);
1191 static void append_nameservers(DBusMessageIter *iter, char **servers)
1197 for (i = 0; servers[i] != NULL; i++) {
1198 DBG("servers[%d] %s", i, servers[i]);
1199 dbus_message_iter_append_basic(iter,
1200 DBUS_TYPE_STRING, &servers[i]);
1204 static void append_dns(DBusMessageIter *iter, void *user_data)
1206 struct vpn_provider *provider = user_data;
1208 if (provider->nameservers != NULL)
1209 append_nameservers(iter, provider->nameservers);
1212 static void append_state(DBusMessageIter *iter,
1213 struct vpn_provider *provider)
1217 switch (provider->state) {
1218 case VPN_PROVIDER_STATE_UNKNOWN:
1219 case VPN_PROVIDER_STATE_IDLE:
1222 case VPN_PROVIDER_STATE_CONNECT:
1223 str = "configuration";
1225 case VPN_PROVIDER_STATE_READY:
1228 case VPN_PROVIDER_STATE_DISCONNECT:
1231 case VPN_PROVIDER_STATE_FAILURE:
1236 connman_dbus_dict_append_basic(iter, "State",
1237 DBUS_TYPE_STRING, &str);
1240 static void append_properties(DBusMessageIter *iter,
1241 struct vpn_provider *provider)
1243 DBusMessageIter dict;
1245 connman_dbus_dict_open(iter, &dict);
1247 append_state(&dict, provider);
1249 if (provider->type != NULL)
1250 connman_dbus_dict_append_basic(&dict, "Type",
1251 DBUS_TYPE_STRING, &provider->type);
1253 if (provider->name != NULL)
1254 connman_dbus_dict_append_basic(&dict, "Name",
1255 DBUS_TYPE_STRING, &provider->name);
1257 if (provider->host != NULL)
1258 connman_dbus_dict_append_basic(&dict, "Host",
1259 DBUS_TYPE_STRING, &provider->host);
1260 if (provider->index >= 0)
1261 connman_dbus_dict_append_basic(&dict, "Index",
1262 DBUS_TYPE_INT32, &provider->index);
1263 if (provider->domain != NULL)
1264 connman_dbus_dict_append_basic(&dict, "Domain",
1265 DBUS_TYPE_STRING, &provider->domain);
1267 if (provider->family == AF_INET)
1268 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1270 else if (provider->family == AF_INET6)
1271 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1274 connman_dbus_dict_append_array(&dict, "Nameservers",
1275 DBUS_TYPE_STRING, append_dns, provider);
1277 connman_dbus_dict_append_array(&dict, "UserRoutes",
1278 DBUS_TYPE_DICT_ENTRY, append_routes,
1279 provider->user_routes);
1281 connman_dbus_dict_append_array(&dict, "ServerRoutes",
1282 DBUS_TYPE_DICT_ENTRY, append_routes,
1285 connman_dbus_dict_close(iter, &dict);
1288 static void connection_added_signal(struct vpn_provider *provider)
1290 DBusMessage *signal;
1291 DBusMessageIter iter;
1293 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1294 VPN_MANAGER_INTERFACE, "ConnectionAdded");
1298 dbus_message_iter_init_append(signal, &iter);
1299 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1301 append_properties(&iter, provider);
1303 dbus_connection_send(connection, signal, NULL);
1304 dbus_message_unref(signal);
1307 static connman_bool_t check_host(char **hosts, char *host)
1314 for (i = 0; hosts[i] != NULL; i++) {
1315 if (g_strcmp0(hosts[i], host) == 0)
1322 static void provider_append_routes(gpointer key, gpointer value,
1325 struct vpn_route *route = value;
1326 struct vpn_provider *provider = user_data;
1327 int index = provider->index;
1329 if (handle_routes == FALSE)
1333 * If the VPN administrator/user has given a route to
1334 * VPN server, then we must discard that because the
1335 * server cannot be contacted via VPN tunnel.
1337 if (check_host(provider->host_ip, route->network) == TRUE) {
1338 DBG("Discarding VPN route to %s via %s at index %d",
1339 route->network, route->gateway, index);
1343 if (route->family == AF_INET6) {
1344 unsigned char prefix_len = atoi(route->netmask);
1346 connman_inet_add_ipv6_network_route(index, route->network,
1350 connman_inet_add_network_route(index, route->network,
1356 static int set_connected(struct vpn_provider *provider,
1357 connman_bool_t connected)
1359 struct vpn_ipconfig *ipconfig;
1361 DBG("provider %p id %s connected %d", provider,
1362 provider->identifier, connected);
1364 if (connected == TRUE) {
1365 if (provider->family == AF_INET6)
1366 ipconfig = provider->ipconfig_ipv6;
1368 ipconfig = provider->ipconfig_ipv4;
1370 __vpn_ipconfig_address_add(ipconfig, provider->family);
1372 if (handle_routes == TRUE)
1373 __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1375 provider_indicate_state(provider,
1376 VPN_PROVIDER_STATE_READY);
1378 g_hash_table_foreach(provider->routes, provider_append_routes,
1381 g_hash_table_foreach(provider->user_routes,
1382 provider_append_routes, provider);
1385 provider_indicate_state(provider,
1386 VPN_PROVIDER_STATE_DISCONNECT);
1388 provider_indicate_state(provider,
1389 VPN_PROVIDER_STATE_IDLE);
1395 int vpn_provider_set_state(struct vpn_provider *provider,
1396 enum vpn_provider_state state)
1398 if (provider == NULL)
1402 case VPN_PROVIDER_STATE_UNKNOWN:
1404 case VPN_PROVIDER_STATE_IDLE:
1405 return set_connected(provider, FALSE);
1406 case VPN_PROVIDER_STATE_CONNECT:
1407 return provider_indicate_state(provider, state);
1408 case VPN_PROVIDER_STATE_READY:
1409 return set_connected(provider, TRUE);
1410 case VPN_PROVIDER_STATE_DISCONNECT:
1411 return provider_indicate_state(provider, state);
1412 case VPN_PROVIDER_STATE_FAILURE:
1413 return provider_indicate_state(provider, state);
1418 int vpn_provider_indicate_error(struct vpn_provider *provider,
1419 enum vpn_provider_error error)
1421 DBG("provider %p id %s error %d", provider, provider->identifier,
1425 case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1427 case VPN_PROVIDER_ERROR_AUTH_FAILED:
1429 case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1438 static int connection_unregister(struct vpn_provider *provider)
1440 DBG("provider %p path %s", provider, provider->path);
1442 if (provider->path == NULL)
1445 g_dbus_unregister_interface(connection, provider->path,
1446 VPN_CONNECTION_INTERFACE);
1448 g_free(provider->path);
1449 provider->path = NULL;
1454 static int connection_register(struct vpn_provider *provider)
1456 DBG("provider %p path %s", provider, provider->path);
1458 if (provider->path != NULL)
1461 provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1462 provider->identifier);
1464 g_dbus_register_interface(connection, provider->path,
1465 VPN_CONNECTION_INTERFACE,
1466 connection_methods, connection_signals,
1467 NULL, provider, NULL);
1472 static void unregister_provider(gpointer data)
1474 struct vpn_provider *provider = data;
1476 configuration_count_del();
1478 connection_unregister(provider);
1480 vpn_provider_unref(provider);
1483 static void provider_initialize(struct vpn_provider *provider)
1485 DBG("provider %p", provider);
1487 provider->index = 0;
1489 provider->name = NULL;
1490 provider->type = NULL;
1491 provider->domain = NULL;
1492 provider->identifier = NULL;
1493 provider->user_networks = NULL;
1494 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1496 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1497 g_free, free_route);
1498 provider->setting_strings = g_hash_table_new_full(g_str_hash,
1499 g_str_equal, g_free, g_free);
1502 static struct vpn_provider *vpn_provider_new(void)
1504 struct vpn_provider *provider;
1506 provider = g_try_new0(struct vpn_provider, 1);
1507 if (provider == NULL)
1510 provider->refcount = 1;
1512 DBG("provider %p", provider);
1513 provider_initialize(provider);
1518 static struct vpn_provider *vpn_provider_get(const char *identifier)
1520 struct vpn_provider *provider;
1522 provider = g_hash_table_lookup(provider_hash, identifier);
1523 if (provider != NULL)
1526 provider = vpn_provider_new();
1527 if (provider == NULL)
1530 DBG("provider %p", provider);
1532 provider->identifier = g_strdup(identifier);
1534 g_hash_table_insert(provider_hash, provider->identifier, provider);
1536 configuration_count_add();
1541 static void provider_dbus_ident(char *ident)
1543 int i, len = strlen(ident);
1545 for (i = 0; i < len; i++) {
1546 if (ident[i] >= '0' && ident[i] <= '9')
1548 if (ident[i] >= 'a' && ident[i] <= 'z')
1550 if (ident[i] >= 'A' && ident[i] <= 'Z')
1556 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1559 struct vpn_provider *provider;
1561 if (keyfile == NULL || ident == NULL)
1564 provider = vpn_provider_lookup(ident);
1565 if (provider == NULL) {
1566 provider = vpn_provider_get(ident);
1567 if (provider == NULL) {
1568 DBG("can not create provider");
1572 provider_load_from_keyfile(provider, keyfile);
1574 if (provider->name == NULL || provider->host == NULL ||
1575 provider->domain == NULL) {
1576 DBG("cannot get name, host or domain");
1577 vpn_provider_unref(provider);
1581 if (provider_register(provider) == 0)
1582 connection_register(provider);
1587 static void provider_create_all_from_type(const char *provider_type)
1594 DBG("provider type %s", provider_type);
1596 providers = __connman_storage_get_providers();
1598 if (providers == NULL)
1601 for (i = 0; providers[i] != NULL; i+=1) {
1603 if (strncmp(providers[i], "provider_", 9) != 0)
1606 id = providers[i] + 9;
1607 keyfile = __connman_storage_load_provider(id);
1609 if (keyfile == NULL)
1612 type = g_key_file_get_string(keyfile, id, "Type", NULL);
1614 DBG("keyfile %p id %s type %s", keyfile, id, type);
1616 if (strcmp(provider_type, type) != 0) {
1618 g_key_file_free(keyfile);
1622 if (provider_create_from_keyfile(keyfile, id) == NULL)
1623 DBG("could not create provider");
1626 g_key_file_free(keyfile);
1628 g_strfreev(providers);
1631 int __vpn_provider_create(DBusMessage *msg)
1633 struct vpn_provider *provider;
1634 DBusMessageIter iter, array;
1635 const char *type = NULL, *name = NULL;
1636 const char *host = NULL, *domain = NULL;
1637 GSList *networks = NULL;
1641 dbus_message_iter_init(msg, &iter);
1642 dbus_message_iter_recurse(&iter, &array);
1644 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1645 DBusMessageIter entry, value;
1648 dbus_message_iter_recurse(&array, &entry);
1649 dbus_message_iter_get_basic(&entry, &key);
1651 dbus_message_iter_next(&entry);
1652 dbus_message_iter_recurse(&entry, &value);
1654 switch (dbus_message_iter_get_arg_type(&value)) {
1655 case DBUS_TYPE_STRING:
1656 if (g_str_equal(key, "Type") == TRUE)
1657 dbus_message_iter_get_basic(&value, &type);
1658 else if (g_str_equal(key, "Name") == TRUE)
1659 dbus_message_iter_get_basic(&value, &name);
1660 else if (g_str_equal(key, "Host") == TRUE)
1661 dbus_message_iter_get_basic(&value, &host);
1662 else if (g_str_equal(key, "VPN.Domain") == TRUE)
1663 dbus_message_iter_get_basic(&value, &domain);
1665 case DBUS_TYPE_ARRAY:
1666 if (g_str_equal(key, "UserRoutes") == TRUE)
1667 networks = get_user_networks(&value);
1671 dbus_message_iter_next(&array);
1674 if (host == NULL || domain == NULL)
1677 DBG("Type %s name %s networks %p", type, name, networks);
1679 if (type == NULL || name == NULL)
1682 ident = g_strdup_printf("%s_%s", host, domain);
1683 provider_dbus_ident(ident);
1685 DBG("ident %s", ident);
1687 provider = vpn_provider_lookup(ident);
1688 if (provider == NULL) {
1689 provider = vpn_provider_get(ident);
1690 if (provider == NULL) {
1691 DBG("can not create provider");
1696 provider->host = g_strdup(host);
1697 provider->domain = g_strdup(domain);
1698 provider->name = g_strdup(name);
1699 provider->type = g_strdup(type);
1701 if (provider_register(provider) == 0)
1702 vpn_provider_load(provider);
1704 provider_resolv_host_addr(provider);
1707 if (networks != NULL) {
1708 g_slist_free_full(provider->user_networks, free_route);
1709 provider->user_networks = networks;
1710 set_user_networks(provider, provider->user_networks);
1713 dbus_message_iter_init(msg, &iter);
1714 dbus_message_iter_recurse(&iter, &array);
1716 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1717 DBusMessageIter entry, value;
1718 const char *key, *str;
1720 dbus_message_iter_recurse(&array, &entry);
1721 dbus_message_iter_get_basic(&entry, &key);
1723 dbus_message_iter_next(&entry);
1724 dbus_message_iter_recurse(&entry, &value);
1726 switch (dbus_message_iter_get_arg_type(&value)) {
1727 case DBUS_TYPE_STRING:
1728 dbus_message_iter_get_basic(&value, &str);
1729 vpn_provider_set_string(provider, key, str);
1733 dbus_message_iter_next(&array);
1738 vpn_provider_save(provider);
1740 err = provider_register(provider);
1741 if (err != 0 && err != -EALREADY)
1744 connection_register(provider);
1746 DBG("provider %p index %d path %s", provider, provider->index,
1749 g_dbus_send_reply(connection, msg,
1750 DBUS_TYPE_OBJECT_PATH, &provider->path,
1753 connection_added_signal(provider);
1758 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
1760 DBusMessageIter entry;
1761 GHashTableIter hash;
1762 gpointer value, key;
1764 g_hash_table_iter_init(&hash, provider_hash);
1766 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
1767 struct vpn_provider *provider = value;
1769 DBG("path %s", provider->path);
1771 if (provider->identifier == NULL)
1774 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
1776 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1778 append_properties(&entry, provider);
1779 dbus_message_iter_close_container(iter, &entry);
1783 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
1789 reply = dbus_message_new_method_return(msg);
1793 __connman_dbus_append_objpath_dict_array(reply,
1794 append_connection_structs, NULL);
1799 const char * __vpn_provider_get_ident(struct vpn_provider *provider)
1801 if (provider == NULL)
1804 return provider->identifier;
1807 int vpn_provider_set_string(struct vpn_provider *provider,
1808 const char *key, const char *value)
1810 DBG("provider %p key %s value %s", provider, key, value);
1812 if (g_str_equal(key, "Type") == TRUE) {
1813 g_free(provider->type);
1814 provider->type = g_strdup(value);
1815 } else if (g_str_equal(key, "Name") == TRUE) {
1816 g_free(provider->name);
1817 provider->name = g_strdup(value);
1818 } else if (g_str_equal(key, "Host") == TRUE) {
1819 g_free(provider->host);
1820 provider->host = g_strdup(value);
1821 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1822 g_free(provider->domain);
1823 provider->domain = g_strdup(value);
1825 g_hash_table_replace(provider->setting_strings,
1826 g_strdup(key), g_strdup(value));
1830 const char *vpn_provider_get_string(struct vpn_provider *provider,
1833 DBG("provider %p key %s", provider, key);
1835 if (g_str_equal(key, "Type") == TRUE)
1836 return provider->type;
1837 else if (g_str_equal(key, "Name") == TRUE)
1838 return provider->name;
1839 else if (g_str_equal(key, "Host") == TRUE)
1840 return provider->host;
1841 else if (g_str_equal(key, "HostIP") == TRUE) {
1842 if (provider->host_ip == NULL ||
1843 provider->host_ip[0] == NULL)
1844 return provider->host;
1846 return provider->host_ip[0];
1847 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1848 return provider->domain;
1850 return g_hash_table_lookup(provider->setting_strings, key);
1853 connman_bool_t __vpn_provider_check_routes(struct vpn_provider *provider)
1855 if (provider == NULL)
1858 if (provider->user_routes != NULL &&
1859 g_hash_table_size(provider->user_routes) > 0)
1862 if (provider->routes != NULL &&
1863 g_hash_table_size(provider->routes) > 0)
1869 void *vpn_provider_get_data(struct vpn_provider *provider)
1871 return provider->driver_data;
1874 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
1876 provider->driver_data = data;
1879 void vpn_provider_set_index(struct vpn_provider *provider, int index)
1881 DBG("index %d provider %p", index, provider);
1883 if (provider->ipconfig_ipv4 == NULL) {
1884 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
1886 if (provider->ipconfig_ipv4 == NULL) {
1887 DBG("Couldnt create ipconfig for IPv4");
1892 __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
1894 if (provider->ipconfig_ipv6 == NULL) {
1895 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
1897 if (provider->ipconfig_ipv6 == NULL) {
1898 DBG("Couldnt create ipconfig for IPv6");
1903 __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
1906 provider->index = index;
1909 int vpn_provider_get_index(struct vpn_provider *provider)
1911 return provider->index;
1914 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
1915 struct connman_ipaddress *ipaddress)
1917 struct vpn_ipconfig *ipconfig = NULL;
1919 switch (ipaddress->family) {
1921 ipconfig = provider->ipconfig_ipv4;
1924 ipconfig = provider->ipconfig_ipv6;
1930 DBG("provider %p ipconfig %p family %d", provider, ipconfig,
1933 if (ipconfig == NULL)
1936 provider->family = ipaddress->family;
1938 __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
1939 __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
1940 __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1941 __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1942 __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1947 int vpn_provider_set_pac(struct vpn_provider *provider,
1950 DBG("provider %p pac %s", provider, pac);
1956 int vpn_provider_set_domain(struct vpn_provider *provider,
1959 DBG("provider %p domain %s", provider, domain);
1961 g_free(provider->domain);
1962 provider->domain = g_strdup(domain);
1967 int vpn_provider_set_nameservers(struct vpn_provider *provider,
1968 const char *nameservers)
1970 DBG("provider %p nameservers %s", provider, nameservers);
1972 g_strfreev(provider->nameservers);
1973 provider->nameservers = NULL;
1975 if (nameservers == NULL)
1978 provider->nameservers = g_strsplit(nameservers, " ", 0);
1983 enum provider_route_type {
1984 PROVIDER_ROUTE_TYPE_NONE = 0,
1985 PROVIDER_ROUTE_TYPE_MASK = 1,
1986 PROVIDER_ROUTE_TYPE_ADDR = 2,
1987 PROVIDER_ROUTE_TYPE_GW = 3,
1990 static int route_env_parse(struct vpn_provider *provider, const char *key,
1991 int *family, unsigned long *idx,
1992 enum provider_route_type *type)
1997 DBG("name %s", provider->name);
1999 if (!strcmp(provider->type, "openvpn")) {
2000 if (g_str_has_prefix(key, "route_network_") == TRUE) {
2001 start = key + strlen("route_network_");
2002 *type = PROVIDER_ROUTE_TYPE_ADDR;
2003 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
2004 start = key + strlen("route_netmask_");
2005 *type = PROVIDER_ROUTE_TYPE_MASK;
2006 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
2007 start = key + strlen("route_gateway_");
2008 *type = PROVIDER_ROUTE_TYPE_GW;
2013 *idx = g_ascii_strtoull(start, &end, 10);
2015 } else if (!strcmp(provider->type, "openconnect")) {
2016 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
2018 start = key + strlen("CISCO_SPLIT_INC_");
2019 } else if (g_str_has_prefix(key,
2020 "CISCO_IPV6_SPLIT_INC_") == TRUE) {
2022 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
2026 *idx = g_ascii_strtoull(start, &end, 10);
2028 if (strncmp(end, "_ADDR", 5) == 0)
2029 *type = PROVIDER_ROUTE_TYPE_ADDR;
2030 else if (strncmp(end, "_MASK", 5) == 0)
2031 *type = PROVIDER_ROUTE_TYPE_MASK;
2032 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2033 *family == AF_INET6) {
2034 *type = PROVIDER_ROUTE_TYPE_MASK;
2042 int vpn_provider_append_route(struct vpn_provider *provider,
2043 const char *key, const char *value)
2045 struct vpn_route *route;
2046 int ret, family = 0;
2047 unsigned long idx = 0;
2048 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2050 DBG("key %s value %s", key, value);
2052 ret = route_env_parse(provider, key, &family, &idx, &type);
2056 DBG("idx %lu family %d type %d", idx, family, type);
2058 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2059 if (route == NULL) {
2060 route = g_try_new0(struct vpn_route, 1);
2061 if (route == NULL) {
2062 connman_error("out of memory");
2066 route->family = family;
2068 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2073 case PROVIDER_ROUTE_TYPE_NONE:
2075 case PROVIDER_ROUTE_TYPE_MASK:
2076 route->netmask = g_strdup(value);
2078 case PROVIDER_ROUTE_TYPE_ADDR:
2079 route->network = g_strdup(value);
2081 case PROVIDER_ROUTE_TYPE_GW:
2082 route->gateway = g_strdup(value);
2086 if (handle_routes == FALSE) {
2087 if (route->netmask != NULL && route->gateway != NULL &&
2088 route->network != NULL)
2089 provider_schedule_changed(provider,
2090 SERVER_ROUTES_CHANGED);
2096 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2098 if (provider->driver == NULL)
2101 return provider->driver->name;
2104 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2106 return provider->identifier;
2109 static gint compare_priority(gconstpointer a, gconstpointer b)
2114 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2116 struct vpn_provider *provider = value;
2118 if (provider->driver != NULL && provider->driver->remove)
2119 provider->driver->remove(provider);
2121 connection_unregister(provider);
2124 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2126 DBG("driver %p name %s", driver, driver->name);
2128 driver_list = g_slist_insert_sorted(driver_list, driver,
2130 provider_create_all_from_type(driver->name);
2134 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2136 GHashTableIter iter;
2137 gpointer value, key;
2139 DBG("driver %p name %s", driver, driver->name);
2141 driver_list = g_slist_remove(driver_list, driver);
2143 g_hash_table_iter_init(&iter, provider_hash);
2144 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2145 struct vpn_provider *provider = value;
2147 if (provider != NULL && provider->driver != NULL &&
2148 provider->driver->type == driver->type &&
2149 g_strcmp0(provider->driver->name,
2150 driver->name) == 0) {
2151 provider->driver = NULL;
2156 static gboolean check_vpn_count(gpointer data)
2158 if (configuration_count == 0) {
2159 connman_info("No VPN configurations found, quitting.");
2166 void __vpn_provider_check_connections(void)
2169 * If we were started when there is no providers configured,
2170 * then just quit. This happens when connman starts and its
2171 * vpn plugin asks connman-vpnd if it has any connections
2172 * configured. If there are none, then we can stop the vpn
2175 g_timeout_add(1000, check_vpn_count, NULL);
2178 const char *vpn_provider_get_name(struct vpn_provider *provider)
2180 return provider->name;
2183 const char *vpn_provider_get_host(struct vpn_provider *provider)
2185 return provider->host;
2188 const char *vpn_provider_get_path(struct vpn_provider *provider)
2190 return provider->path;
2193 static int agent_probe(struct connman_agent *agent)
2195 DBG("agent %p", agent);
2199 static void agent_remove(struct connman_agent *agent)
2201 DBG("agent %p", agent);
2204 static struct connman_agent_driver agent_driver = {
2206 .interface = VPN_AGENT_INTERFACE,
2207 .probe = agent_probe,
2208 .remove = agent_remove,
2211 int __vpn_provider_init(gboolean do_routes)
2217 handle_routes = do_routes;
2219 err = connman_agent_driver_register(&agent_driver);
2221 connman_error("Cannot register agent driver for %s",
2226 connection = connman_dbus_get_connection();
2228 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2229 NULL, unregister_provider);
2234 void __vpn_provider_cleanup(void)
2238 g_hash_table_foreach(provider_hash, clean_provider, NULL);
2240 g_hash_table_destroy(provider_hash);
2241 provider_hash = NULL;
2243 connman_agent_driver_unregister(&agent_driver);
2245 dbus_connection_unref(connection);