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);
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);
970 if (err != -EINPROGRESS)
979 static void connect_cb(struct vpn_provider *provider, void *user_data,
982 DBusMessage *pending = user_data;
984 DBG("provider %p user %p error %d", provider, user_data, error);
987 DBusMessage *reply = __connman_error_failed(pending, error);
989 g_dbus_send_message(connection, reply);
991 vpn_provider_indicate_error(provider,
992 VPN_PROVIDER_ERROR_CONNECT_FAILED);
993 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
995 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
997 dbus_message_unref(pending);
1000 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
1004 DBG("provider %p", provider);
1006 if (provider->driver != NULL && provider->driver->connect != NULL) {
1007 dbus_message_ref(msg);
1008 err = provider->driver->connect(provider, connect_cb, msg);
1015 static void connection_removed_signal(struct vpn_provider *provider)
1017 DBusMessage *signal;
1018 DBusMessageIter iter;
1020 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1021 VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1025 dbus_message_iter_init_append(signal, &iter);
1026 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1028 dbus_connection_send(connection, signal, NULL);
1029 dbus_message_unref(signal);
1032 static char *get_ident(const char *path)
1039 pos = strrchr(path, '/');
1046 int __vpn_provider_remove(const char *path)
1048 struct vpn_provider *provider;
1051 DBG("path %s", path);
1053 ident = get_ident(path);
1055 provider = vpn_provider_lookup(ident);
1056 if (provider != NULL) {
1057 DBG("Removing VPN %s", provider->identifier);
1059 connection_removed_signal(provider);
1061 provider_unregister(provider);
1062 g_hash_table_remove(provider_hash, provider->identifier);
1064 __connman_storage_remove_provider(ident);
1071 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1073 struct vpn_provider *provider = user_data;
1074 const char *address, *gateway, *peer;
1076 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1077 if (address != NULL) {
1079 struct in_addr netmask;
1083 prefixlen = __vpn_ipconfig_get_prefixlen(
1084 provider->ipconfig_ipv4);
1086 addr = 0xffffffff << (32 - prefixlen);
1087 netmask.s_addr = htonl(addr);
1088 mask = inet_ntoa(netmask);
1090 connman_dbus_dict_append_basic(iter, "Address",
1091 DBUS_TYPE_STRING, &address);
1093 connman_dbus_dict_append_basic(iter, "Netmask",
1094 DBUS_TYPE_STRING, &mask);
1097 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1098 if (gateway != NULL)
1099 connman_dbus_dict_append_basic(iter, "Gateway",
1100 DBUS_TYPE_STRING, &gateway);
1102 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1104 connman_dbus_dict_append_basic(iter, "Peer",
1105 DBUS_TYPE_STRING, &peer);
1108 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1110 struct vpn_provider *provider = user_data;
1111 const char *address, *gateway, *peer;
1113 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1114 if (address != NULL) {
1115 unsigned char prefixlen;
1117 connman_dbus_dict_append_basic(iter, "Address",
1118 DBUS_TYPE_STRING, &address);
1120 prefixlen = __vpn_ipconfig_get_prefixlen(
1121 provider->ipconfig_ipv6);
1123 connman_dbus_dict_append_basic(iter, "PrefixLength",
1124 DBUS_TYPE_BYTE, &prefixlen);
1127 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1128 if (gateway != NULL)
1129 connman_dbus_dict_append_basic(iter, "Gateway",
1130 DBUS_TYPE_STRING, &gateway);
1132 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1134 connman_dbus_dict_append_basic(iter, "Peer",
1135 DBUS_TYPE_STRING, &peer);
1138 static const char *state2string(enum vpn_provider_state state)
1141 case VPN_PROVIDER_STATE_UNKNOWN:
1143 case VPN_PROVIDER_STATE_IDLE:
1145 case VPN_PROVIDER_STATE_CONNECT:
1146 return "configuration";
1147 case VPN_PROVIDER_STATE_READY:
1149 case VPN_PROVIDER_STATE_DISCONNECT:
1150 return "disconnect";
1151 case VPN_PROVIDER_STATE_FAILURE:
1158 static int provider_indicate_state(struct vpn_provider *provider,
1159 enum vpn_provider_state state)
1163 DBG("provider %p state %d", provider, state);
1165 str = state2string(state);
1169 provider->state = state;
1171 if (state == VPN_PROVIDER_STATE_READY) {
1172 connman_dbus_property_changed_basic(provider->path,
1173 VPN_CONNECTION_INTERFACE, "Index",
1174 DBUS_TYPE_INT32, &provider->index);
1176 if (provider->family == AF_INET)
1177 connman_dbus_property_changed_dict(provider->path,
1178 VPN_CONNECTION_INTERFACE, "IPv4",
1179 append_ipv4, provider);
1180 else if (provider->family == AF_INET6)
1181 connman_dbus_property_changed_dict(provider->path,
1182 VPN_CONNECTION_INTERFACE, "IPv6",
1183 append_ipv6, provider);
1186 connman_dbus_property_changed_basic(provider->path,
1187 VPN_CONNECTION_INTERFACE, "State",
1188 DBUS_TYPE_STRING, &str);
1192 static void append_nameservers(DBusMessageIter *iter, char **servers)
1198 for (i = 0; servers[i] != NULL; i++) {
1199 DBG("servers[%d] %s", i, servers[i]);
1200 dbus_message_iter_append_basic(iter,
1201 DBUS_TYPE_STRING, &servers[i]);
1205 static void append_dns(DBusMessageIter *iter, void *user_data)
1207 struct vpn_provider *provider = user_data;
1209 if (provider->nameservers != NULL)
1210 append_nameservers(iter, provider->nameservers);
1213 static void append_state(DBusMessageIter *iter,
1214 struct vpn_provider *provider)
1218 switch (provider->state) {
1219 case VPN_PROVIDER_STATE_UNKNOWN:
1220 case VPN_PROVIDER_STATE_IDLE:
1223 case VPN_PROVIDER_STATE_CONNECT:
1224 str = "configuration";
1226 case VPN_PROVIDER_STATE_READY:
1229 case VPN_PROVIDER_STATE_DISCONNECT:
1232 case VPN_PROVIDER_STATE_FAILURE:
1237 connman_dbus_dict_append_basic(iter, "State",
1238 DBUS_TYPE_STRING, &str);
1241 static void append_properties(DBusMessageIter *iter,
1242 struct vpn_provider *provider)
1244 DBusMessageIter dict;
1246 connman_dbus_dict_open(iter, &dict);
1248 append_state(&dict, provider);
1250 if (provider->type != NULL)
1251 connman_dbus_dict_append_basic(&dict, "Type",
1252 DBUS_TYPE_STRING, &provider->type);
1254 if (provider->name != NULL)
1255 connman_dbus_dict_append_basic(&dict, "Name",
1256 DBUS_TYPE_STRING, &provider->name);
1258 if (provider->host != NULL)
1259 connman_dbus_dict_append_basic(&dict, "Host",
1260 DBUS_TYPE_STRING, &provider->host);
1261 if (provider->index >= 0)
1262 connman_dbus_dict_append_basic(&dict, "Index",
1263 DBUS_TYPE_INT32, &provider->index);
1264 if (provider->domain != NULL)
1265 connman_dbus_dict_append_basic(&dict, "Domain",
1266 DBUS_TYPE_STRING, &provider->domain);
1268 if (provider->family == AF_INET)
1269 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1271 else if (provider->family == AF_INET6)
1272 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1275 connman_dbus_dict_append_array(&dict, "Nameservers",
1276 DBUS_TYPE_STRING, append_dns, provider);
1278 connman_dbus_dict_append_array(&dict, "UserRoutes",
1279 DBUS_TYPE_DICT_ENTRY, append_routes,
1280 provider->user_routes);
1282 connman_dbus_dict_append_array(&dict, "ServerRoutes",
1283 DBUS_TYPE_DICT_ENTRY, append_routes,
1286 connman_dbus_dict_close(iter, &dict);
1289 static void connection_added_signal(struct vpn_provider *provider)
1291 DBusMessage *signal;
1292 DBusMessageIter iter;
1294 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1295 VPN_MANAGER_INTERFACE, "ConnectionAdded");
1299 dbus_message_iter_init_append(signal, &iter);
1300 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1302 append_properties(&iter, provider);
1304 dbus_connection_send(connection, signal, NULL);
1305 dbus_message_unref(signal);
1308 static connman_bool_t check_host(char **hosts, char *host)
1315 for (i = 0; hosts[i] != NULL; i++) {
1316 if (g_strcmp0(hosts[i], host) == 0)
1323 static void provider_append_routes(gpointer key, gpointer value,
1326 struct vpn_route *route = value;
1327 struct vpn_provider *provider = user_data;
1328 int index = provider->index;
1330 if (handle_routes == FALSE)
1334 * If the VPN administrator/user has given a route to
1335 * VPN server, then we must discard that because the
1336 * server cannot be contacted via VPN tunnel.
1338 if (check_host(provider->host_ip, route->network) == TRUE) {
1339 DBG("Discarding VPN route to %s via %s at index %d",
1340 route->network, route->gateway, index);
1344 if (route->family == AF_INET6) {
1345 unsigned char prefix_len = atoi(route->netmask);
1347 connman_inet_add_ipv6_network_route(index, route->network,
1351 connman_inet_add_network_route(index, route->network,
1357 static int set_connected(struct vpn_provider *provider,
1358 connman_bool_t connected)
1360 struct vpn_ipconfig *ipconfig;
1362 DBG("provider %p id %s connected %d", provider,
1363 provider->identifier, connected);
1365 if (connected == TRUE) {
1366 if (provider->family == AF_INET6)
1367 ipconfig = provider->ipconfig_ipv6;
1369 ipconfig = provider->ipconfig_ipv4;
1371 __vpn_ipconfig_address_add(ipconfig, provider->family);
1373 if (handle_routes == TRUE)
1374 __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1376 provider_indicate_state(provider,
1377 VPN_PROVIDER_STATE_READY);
1379 g_hash_table_foreach(provider->routes, provider_append_routes,
1382 g_hash_table_foreach(provider->user_routes,
1383 provider_append_routes, provider);
1386 provider_indicate_state(provider,
1387 VPN_PROVIDER_STATE_DISCONNECT);
1389 provider_indicate_state(provider,
1390 VPN_PROVIDER_STATE_IDLE);
1396 int vpn_provider_set_state(struct vpn_provider *provider,
1397 enum vpn_provider_state state)
1399 if (provider == NULL)
1403 case VPN_PROVIDER_STATE_UNKNOWN:
1405 case VPN_PROVIDER_STATE_IDLE:
1406 return set_connected(provider, FALSE);
1407 case VPN_PROVIDER_STATE_CONNECT:
1408 return provider_indicate_state(provider, state);
1409 case VPN_PROVIDER_STATE_READY:
1410 return set_connected(provider, TRUE);
1411 case VPN_PROVIDER_STATE_DISCONNECT:
1412 return provider_indicate_state(provider, state);
1413 case VPN_PROVIDER_STATE_FAILURE:
1414 return provider_indicate_state(provider, state);
1419 int vpn_provider_indicate_error(struct vpn_provider *provider,
1420 enum vpn_provider_error error)
1422 DBG("provider %p id %s error %d", provider, provider->identifier,
1426 case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1428 case VPN_PROVIDER_ERROR_AUTH_FAILED:
1430 case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1439 static int connection_unregister(struct vpn_provider *provider)
1441 DBG("provider %p path %s", provider, provider->path);
1443 if (provider->path == NULL)
1446 g_dbus_unregister_interface(connection, provider->path,
1447 VPN_CONNECTION_INTERFACE);
1449 g_free(provider->path);
1450 provider->path = NULL;
1455 static int connection_register(struct vpn_provider *provider)
1457 DBG("provider %p path %s", provider, provider->path);
1459 if (provider->path != NULL)
1462 provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1463 provider->identifier);
1465 g_dbus_register_interface(connection, provider->path,
1466 VPN_CONNECTION_INTERFACE,
1467 connection_methods, connection_signals,
1468 NULL, provider, NULL);
1473 static void unregister_provider(gpointer data)
1475 struct vpn_provider *provider = data;
1477 configuration_count_del();
1479 connection_unregister(provider);
1481 vpn_provider_unref(provider);
1484 static void provider_initialize(struct vpn_provider *provider)
1486 DBG("provider %p", provider);
1488 provider->index = 0;
1490 provider->name = NULL;
1491 provider->type = NULL;
1492 provider->domain = NULL;
1493 provider->identifier = NULL;
1494 provider->user_networks = NULL;
1495 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1497 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1498 g_free, free_route);
1499 provider->setting_strings = g_hash_table_new_full(g_str_hash,
1500 g_str_equal, g_free, g_free);
1503 static struct vpn_provider *vpn_provider_new(void)
1505 struct vpn_provider *provider;
1507 provider = g_try_new0(struct vpn_provider, 1);
1508 if (provider == NULL)
1511 provider->refcount = 1;
1513 DBG("provider %p", provider);
1514 provider_initialize(provider);
1519 static struct vpn_provider *vpn_provider_get(const char *identifier)
1521 struct vpn_provider *provider;
1523 provider = g_hash_table_lookup(provider_hash, identifier);
1524 if (provider != NULL)
1527 provider = vpn_provider_new();
1528 if (provider == NULL)
1531 DBG("provider %p", provider);
1533 provider->identifier = g_strdup(identifier);
1535 g_hash_table_insert(provider_hash, provider->identifier, provider);
1537 configuration_count_add();
1542 static void provider_dbus_ident(char *ident)
1544 int i, len = strlen(ident);
1546 for (i = 0; i < len; i++) {
1547 if (ident[i] >= '0' && ident[i] <= '9')
1549 if (ident[i] >= 'a' && ident[i] <= 'z')
1551 if (ident[i] >= 'A' && ident[i] <= 'Z')
1557 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1560 struct vpn_provider *provider;
1562 if (keyfile == NULL || ident == NULL)
1565 provider = vpn_provider_lookup(ident);
1566 if (provider == NULL) {
1567 provider = vpn_provider_get(ident);
1568 if (provider == NULL) {
1569 DBG("can not create provider");
1573 provider_load_from_keyfile(provider, keyfile);
1575 if (provider->name == NULL || provider->host == NULL ||
1576 provider->domain == NULL) {
1577 DBG("cannot get name, host or domain");
1578 vpn_provider_unref(provider);
1582 if (provider_register(provider) == 0)
1583 connection_register(provider);
1588 static void provider_create_all_from_type(const char *provider_type)
1595 DBG("provider type %s", provider_type);
1597 providers = __connman_storage_get_providers();
1599 for (i = 0; providers[i] != NULL; i+=1) {
1601 if (strncmp(providers[i], "provider_", 9) != 0)
1604 id = providers[i] + 9;
1605 keyfile = __connman_storage_load_provider(id);
1607 if (keyfile == NULL)
1610 type = g_key_file_get_string(keyfile, id, "Type", NULL);
1612 DBG("keyfile %p id %s type %s", keyfile, id, type);
1614 if (strcmp(provider_type, type) != 0) {
1616 g_key_file_free(keyfile);
1620 if (provider_create_from_keyfile(keyfile, id) == NULL)
1621 DBG("could not create provider");
1624 g_key_file_free(keyfile);
1626 g_strfreev(providers);
1629 int __vpn_provider_create(DBusMessage *msg)
1631 struct vpn_provider *provider;
1632 DBusMessageIter iter, array;
1633 const char *type = NULL, *name = NULL;
1634 const char *host = NULL, *domain = NULL;
1635 GSList *networks = NULL;
1639 dbus_message_iter_init(msg, &iter);
1640 dbus_message_iter_recurse(&iter, &array);
1642 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1643 DBusMessageIter entry, value;
1646 dbus_message_iter_recurse(&array, &entry);
1647 dbus_message_iter_get_basic(&entry, &key);
1649 dbus_message_iter_next(&entry);
1650 dbus_message_iter_recurse(&entry, &value);
1652 switch (dbus_message_iter_get_arg_type(&value)) {
1653 case DBUS_TYPE_STRING:
1654 if (g_str_equal(key, "Type") == TRUE)
1655 dbus_message_iter_get_basic(&value, &type);
1656 else if (g_str_equal(key, "Name") == TRUE)
1657 dbus_message_iter_get_basic(&value, &name);
1658 else if (g_str_equal(key, "Host") == TRUE)
1659 dbus_message_iter_get_basic(&value, &host);
1660 else if (g_str_equal(key, "VPN.Domain") == TRUE)
1661 dbus_message_iter_get_basic(&value, &domain);
1663 case DBUS_TYPE_ARRAY:
1664 if (g_str_equal(key, "UserRoutes") == TRUE)
1665 networks = get_user_networks(&value);
1669 dbus_message_iter_next(&array);
1672 if (host == NULL || domain == NULL)
1675 DBG("Type %s name %s networks %p", type, name, networks);
1677 if (type == NULL || name == NULL)
1680 ident = g_strdup_printf("%s_%s", host, domain);
1681 provider_dbus_ident(ident);
1683 DBG("ident %s", ident);
1685 provider = vpn_provider_lookup(ident);
1686 if (provider == NULL) {
1687 provider = vpn_provider_get(ident);
1688 if (provider == NULL) {
1689 DBG("can not create provider");
1694 provider->host = g_strdup(host);
1695 provider->domain = g_strdup(domain);
1696 provider->name = g_strdup(name);
1697 provider->type = g_strdup(type);
1699 if (provider_register(provider) == 0)
1700 vpn_provider_load(provider);
1702 provider_resolv_host_addr(provider);
1705 if (networks != NULL) {
1706 g_slist_free_full(provider->user_networks, free_route);
1707 provider->user_networks = networks;
1708 set_user_networks(provider, provider->user_networks);
1711 dbus_message_iter_init(msg, &iter);
1712 dbus_message_iter_recurse(&iter, &array);
1714 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1715 DBusMessageIter entry, value;
1716 const char *key, *str;
1718 dbus_message_iter_recurse(&array, &entry);
1719 dbus_message_iter_get_basic(&entry, &key);
1721 dbus_message_iter_next(&entry);
1722 dbus_message_iter_recurse(&entry, &value);
1724 switch (dbus_message_iter_get_arg_type(&value)) {
1725 case DBUS_TYPE_STRING:
1726 dbus_message_iter_get_basic(&value, &str);
1727 vpn_provider_set_string(provider, key, str);
1731 dbus_message_iter_next(&array);
1736 vpn_provider_save(provider);
1738 err = provider_register(provider);
1739 if (err != 0 && err != -EALREADY)
1742 connection_register(provider);
1744 DBG("provider %p index %d path %s", provider, provider->index,
1747 g_dbus_send_reply(connection, msg,
1748 DBUS_TYPE_OBJECT_PATH, &provider->path,
1751 connection_added_signal(provider);
1756 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
1758 DBusMessageIter entry;
1759 GHashTableIter hash;
1760 gpointer value, key;
1762 g_hash_table_iter_init(&hash, provider_hash);
1764 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
1765 struct vpn_provider *provider = value;
1767 DBG("path %s", provider->path);
1769 if (provider->identifier == NULL)
1772 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
1774 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1776 append_properties(&entry, provider);
1777 dbus_message_iter_close_container(iter, &entry);
1781 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
1787 reply = dbus_message_new_method_return(msg);
1791 __connman_dbus_append_objpath_dict_array(reply,
1792 append_connection_structs, NULL);
1797 const char * __vpn_provider_get_ident(struct vpn_provider *provider)
1799 if (provider == NULL)
1802 return provider->identifier;
1805 int vpn_provider_set_string(struct vpn_provider *provider,
1806 const char *key, const char *value)
1808 DBG("provider %p key %s value %s", provider, key, value);
1810 if (g_str_equal(key, "Type") == TRUE) {
1811 g_free(provider->type);
1812 provider->type = g_strdup(value);
1813 } else if (g_str_equal(key, "Name") == TRUE) {
1814 g_free(provider->name);
1815 provider->name = g_strdup(value);
1816 } else if (g_str_equal(key, "Host") == TRUE) {
1817 g_free(provider->host);
1818 provider->host = g_strdup(value);
1819 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1820 g_free(provider->domain);
1821 provider->domain = g_strdup(value);
1823 g_hash_table_replace(provider->setting_strings,
1824 g_strdup(key), g_strdup(value));
1828 const char *vpn_provider_get_string(struct vpn_provider *provider,
1831 DBG("provider %p key %s", provider, key);
1833 if (g_str_equal(key, "Type") == TRUE)
1834 return provider->type;
1835 else if (g_str_equal(key, "Name") == TRUE)
1836 return provider->name;
1837 else if (g_str_equal(key, "Host") == TRUE)
1838 return provider->host;
1839 else if (g_str_equal(key, "HostIP") == TRUE) {
1840 if (provider->host_ip == NULL ||
1841 provider->host_ip[0] == NULL)
1842 return provider->host;
1844 return provider->host_ip[0];
1845 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1846 return provider->domain;
1848 return g_hash_table_lookup(provider->setting_strings, key);
1851 connman_bool_t __vpn_provider_check_routes(struct vpn_provider *provider)
1853 if (provider == NULL)
1856 if (provider->user_routes != NULL &&
1857 g_hash_table_size(provider->user_routes) > 0)
1860 if (provider->routes != NULL &&
1861 g_hash_table_size(provider->routes) > 0)
1867 void *vpn_provider_get_data(struct vpn_provider *provider)
1869 return provider->driver_data;
1872 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
1874 provider->driver_data = data;
1877 void vpn_provider_set_index(struct vpn_provider *provider, int index)
1879 DBG("index %d provider %p", index, provider);
1881 if (provider->ipconfig_ipv4 == NULL) {
1882 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
1884 if (provider->ipconfig_ipv4 == NULL) {
1885 DBG("Couldnt create ipconfig for IPv4");
1890 __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
1892 if (provider->ipconfig_ipv6 == NULL) {
1893 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
1895 if (provider->ipconfig_ipv6 == NULL) {
1896 DBG("Couldnt create ipconfig for IPv6");
1901 __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
1904 provider->index = index;
1907 int vpn_provider_get_index(struct vpn_provider *provider)
1909 return provider->index;
1912 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
1913 struct connman_ipaddress *ipaddress)
1915 struct vpn_ipconfig *ipconfig = NULL;
1917 switch (ipaddress->family) {
1919 ipconfig = provider->ipconfig_ipv4;
1922 ipconfig = provider->ipconfig_ipv6;
1928 DBG("provider %p ipconfig %p family %d", provider, ipconfig,
1931 if (ipconfig == NULL)
1934 provider->family = ipaddress->family;
1936 __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
1937 __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
1938 __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1939 __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1940 __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1945 int vpn_provider_set_pac(struct vpn_provider *provider,
1948 DBG("provider %p pac %s", provider, pac);
1954 int vpn_provider_set_domain(struct vpn_provider *provider,
1957 DBG("provider %p domain %s", provider, domain);
1959 g_free(provider->domain);
1960 provider->domain = g_strdup(domain);
1965 int vpn_provider_set_nameservers(struct vpn_provider *provider,
1966 const char *nameservers)
1968 DBG("provider %p nameservers %s", provider, nameservers);
1970 g_strfreev(provider->nameservers);
1971 provider->nameservers = NULL;
1973 if (nameservers == NULL)
1976 provider->nameservers = g_strsplit(nameservers, " ", 0);
1981 enum provider_route_type {
1982 PROVIDER_ROUTE_TYPE_NONE = 0,
1983 PROVIDER_ROUTE_TYPE_MASK = 1,
1984 PROVIDER_ROUTE_TYPE_ADDR = 2,
1985 PROVIDER_ROUTE_TYPE_GW = 3,
1988 static int route_env_parse(struct vpn_provider *provider, const char *key,
1989 int *family, unsigned long *idx,
1990 enum provider_route_type *type)
1995 DBG("name %s", provider->name);
1997 if (!strcmp(provider->type, "openvpn")) {
1998 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1999 start = key + strlen("route_network_");
2000 *type = PROVIDER_ROUTE_TYPE_ADDR;
2001 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
2002 start = key + strlen("route_netmask_");
2003 *type = PROVIDER_ROUTE_TYPE_MASK;
2004 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
2005 start = key + strlen("route_gateway_");
2006 *type = PROVIDER_ROUTE_TYPE_GW;
2011 *idx = g_ascii_strtoull(start, &end, 10);
2013 } else if (!strcmp(provider->type, "openconnect")) {
2014 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
2016 start = key + strlen("CISCO_SPLIT_INC_");
2017 } else if (g_str_has_prefix(key,
2018 "CISCO_IPV6_SPLIT_INC_") == TRUE) {
2020 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
2024 *idx = g_ascii_strtoull(start, &end, 10);
2026 if (strncmp(end, "_ADDR", 5) == 0)
2027 *type = PROVIDER_ROUTE_TYPE_ADDR;
2028 else if (strncmp(end, "_MASK", 5) == 0)
2029 *type = PROVIDER_ROUTE_TYPE_MASK;
2030 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2031 *family == AF_INET6) {
2032 *type = PROVIDER_ROUTE_TYPE_MASK;
2040 int vpn_provider_append_route(struct vpn_provider *provider,
2041 const char *key, const char *value)
2043 struct vpn_route *route;
2044 int ret, family = 0;
2045 unsigned long idx = 0;
2046 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2048 DBG("key %s value %s", key, value);
2050 ret = route_env_parse(provider, key, &family, &idx, &type);
2054 DBG("idx %lu family %d type %d", idx, family, type);
2056 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2057 if (route == NULL) {
2058 route = g_try_new0(struct vpn_route, 1);
2059 if (route == NULL) {
2060 connman_error("out of memory");
2064 route->family = family;
2066 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2071 case PROVIDER_ROUTE_TYPE_NONE:
2073 case PROVIDER_ROUTE_TYPE_MASK:
2074 route->netmask = g_strdup(value);
2076 case PROVIDER_ROUTE_TYPE_ADDR:
2077 route->network = g_strdup(value);
2079 case PROVIDER_ROUTE_TYPE_GW:
2080 route->gateway = g_strdup(value);
2084 if (handle_routes == FALSE) {
2085 if (route->netmask != NULL && route->gateway != NULL &&
2086 route->network != NULL)
2087 provider_schedule_changed(provider,
2088 SERVER_ROUTES_CHANGED);
2094 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2096 if (provider->driver == NULL)
2099 return provider->driver->name;
2102 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2104 return provider->identifier;
2107 static gint compare_priority(gconstpointer a, gconstpointer b)
2112 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2114 struct vpn_provider *provider = value;
2116 if (provider->driver != NULL && provider->driver->remove)
2117 provider->driver->remove(provider);
2119 connection_unregister(provider);
2122 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2124 DBG("driver %p name %s", driver, driver->name);
2126 driver_list = g_slist_insert_sorted(driver_list, driver,
2128 provider_create_all_from_type(driver->name);
2132 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2134 GHashTableIter iter;
2135 gpointer value, key;
2137 DBG("driver %p name %s", driver, driver->name);
2139 driver_list = g_slist_remove(driver_list, driver);
2141 g_hash_table_iter_init(&iter, provider_hash);
2142 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2143 struct vpn_provider *provider = value;
2145 if (provider != NULL && provider->driver != NULL &&
2146 provider->driver->type == driver->type &&
2147 g_strcmp0(provider->driver->name,
2148 driver->name) == 0) {
2149 provider->driver = NULL;
2154 static gboolean check_vpn_count(gpointer data)
2156 if (configuration_count == 0) {
2157 connman_info("No VPN configurations found, quitting.");
2164 void __vpn_provider_check_connections(void)
2167 * If we were started when there is no providers configured,
2168 * then just quit. This happens when connman starts and its
2169 * vpn plugin asks connman-vpnd if it has any connections
2170 * configured. If there are none, then we can stop the vpn
2173 g_timeout_add(1000, check_vpn_count, NULL);
2176 const char *vpn_provider_get_name(struct vpn_provider *provider)
2178 return provider->name;
2181 const char *vpn_provider_get_host(struct vpn_provider *provider)
2183 return provider->host;
2186 const char *vpn_provider_get_path(struct vpn_provider *provider)
2188 return provider->path;
2191 static int agent_probe(struct connman_agent *agent)
2193 DBG("agent %p", agent);
2197 static void agent_remove(struct connman_agent *agent)
2199 DBG("agent %p", agent);
2202 static struct connman_agent_driver agent_driver = {
2204 .interface = VPN_AGENT_INTERFACE,
2205 .probe = agent_probe,
2206 .remove = agent_remove,
2209 int __vpn_provider_init(gboolean do_routes)
2215 handle_routes = do_routes;
2217 err = connman_agent_driver_register(&agent_driver);
2219 connman_error("Cannot register agent driver for %s",
2224 connection = connman_dbus_get_connection();
2226 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2227 NULL, unregister_provider);
2232 void __vpn_provider_cleanup(void)
2236 g_hash_table_foreach(provider_hash, clean_provider, NULL);
2238 g_hash_table_destroy(provider_hash);
2239 provider_hash = NULL;
2241 connman_agent_driver_unregister(&agent_driver);
2243 dbus_connection_unref(connection);