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;
88 static void free_route(gpointer data)
90 struct vpn_route *route = data;
92 g_free(route->network);
93 g_free(route->netmask);
94 g_free(route->gateway);
99 static void append_route(DBusMessageIter *iter, void *user_data)
101 struct vpn_route *route = user_data;
102 DBusMessageIter item;
105 connman_dbus_dict_open(iter, &item);
110 if (route->family == AF_INET)
112 else if (route->family == AF_INET6)
116 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
117 DBUS_TYPE_INT32, &family);
119 if (route->network != NULL)
120 connman_dbus_dict_append_basic(&item, "Network",
121 DBUS_TYPE_STRING, &route->network);
123 if (route->netmask != NULL)
124 connman_dbus_dict_append_basic(&item, "Netmask",
125 DBUS_TYPE_STRING, &route->netmask);
127 if (route->gateway != NULL)
128 connman_dbus_dict_append_basic(&item, "Gateway",
129 DBUS_TYPE_STRING, &route->gateway);
132 connman_dbus_dict_close(iter, &item);
135 static void append_routes(DBusMessageIter *iter, void *user_data)
137 GHashTable *routes = user_data;
141 if (routes == NULL) {
142 append_route(iter, NULL);
146 g_hash_table_iter_init(&hash, routes);
148 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
149 DBusMessageIter dict;
151 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
153 append_route(&dict, value);
154 dbus_message_iter_close_container(iter, &dict);
158 static void send_routes(struct vpn_provider *provider, GHashTable *routes,
161 connman_dbus_property_changed_array(provider->path,
162 VPN_CONNECTION_INTERFACE,
164 DBUS_TYPE_DICT_ENTRY,
169 static int provider_property_changed(struct vpn_provider *provider,
172 DBG("provider %p name %s", provider, name);
174 if (g_str_equal(name, "UserRoutes") == TRUE)
175 send_routes(provider, provider->user_routes, name);
176 else if (g_str_equal(name, "ServerRoutes") == TRUE)
177 send_routes(provider, provider->routes, name);
182 static GSList *read_route_dict(GSList *routes, DBusMessageIter *dicts)
184 DBusMessageIter dict, value, entry;
185 const char *network, *netmask, *gateway;
186 struct vpn_route *route;
190 dbus_message_iter_recurse(dicts, &entry);
192 network = netmask = gateway = NULL;
195 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_DICT_ENTRY) {
197 dbus_message_iter_recurse(&entry, &dict);
198 dbus_message_iter_get_basic(&dict, &key);
200 dbus_message_iter_next(&dict);
201 dbus_message_iter_recurse(&dict, &value);
203 type = dbus_message_iter_get_arg_type(&value);
206 case DBUS_TYPE_STRING:
207 if (g_str_equal(key, "ProtocolFamily") == TRUE)
208 dbus_message_iter_get_basic(&value, &family);
209 else if (g_str_equal(key, "Network") == TRUE)
210 dbus_message_iter_get_basic(&value, &network);
211 else if (g_str_equal(key, "Netmask") == TRUE)
212 dbus_message_iter_get_basic(&value, &netmask);
213 else if (g_str_equal(key, "Gateway") == TRUE)
214 dbus_message_iter_get_basic(&value, &gateway);
218 dbus_message_iter_next(&entry);
221 DBG("family %d network %s netmask %s gateway %s", family,
222 network, netmask, gateway);
224 if (network == NULL || netmask == NULL) {
225 DBG("Ignoring route as network/netmask is missing");
229 route = g_try_new(struct vpn_route, 1);
231 g_slist_free_full(routes, free_route);
235 if (family == PF_UNSPEC) {
236 family = connman_inet_check_ipaddress(network);
238 DBG("Cannot get address family of %s (%d/%s)", network,
239 family, gai_strerror(family));
240 if (strstr(network, ":") != NULL) {
241 DBG("Guessing it is IPv6");
244 DBG("Guessing it is IPv4");
262 route->family = family;
263 route->network = g_strdup(network);
264 route->netmask = g_strdup(netmask);
265 route->gateway = g_strdup(gateway);
267 routes = g_slist_prepend(routes, route);
271 static GSList *get_user_networks(DBusMessageIter *array)
273 DBusMessageIter entry;
276 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
278 dbus_message_iter_recurse(array, &entry);
280 while (dbus_message_iter_get_arg_type(&entry) ==
282 DBusMessageIter dicts;
284 dbus_message_iter_recurse(&entry, &dicts);
286 while (dbus_message_iter_get_arg_type(&dicts) ==
289 list = read_route_dict(list, &dicts);
290 dbus_message_iter_next(&dicts);
293 dbus_message_iter_next(&entry);
296 dbus_message_iter_next(array);
302 static void set_user_networks(struct vpn_provider *provider, GSList *networks)
306 for (list = networks; list != NULL; list = g_slist_next(list)) {
307 struct vpn_route *route= list->data;
309 if (__vpn_provider_append_user_route(provider,
310 route->family, route->network,
311 route->netmask) != 0)
316 static void del_routes(struct vpn_provider *provider)
321 g_hash_table_iter_init(&hash, provider->user_routes);
322 while (handle_routes == TRUE && g_hash_table_iter_next(&hash,
323 &key, &value) == TRUE) {
324 struct vpn_route *route = value;
325 if (route->family == AF_INET6) {
326 unsigned char prefixlen = atoi(route->netmask);
327 connman_inet_del_ipv6_network_route(provider->index,
331 connman_inet_del_host_route(provider->index,
335 g_hash_table_remove_all(provider->user_routes);
336 g_slist_free_full(provider->user_networks, free_route);
337 provider->user_networks = NULL;
340 static gboolean provider_send_changed(gpointer data)
342 struct vpn_provider *provider = data;
344 if (provider->what_changed & USER_ROUTES_CHANGED)
345 provider_property_changed(provider, "UserRoutes");
347 if (provider->what_changed & SERVER_ROUTES_CHANGED)
348 provider_property_changed(provider, "ServerRoutes");
350 provider->what_changed = 0;
351 provider->notify_id = 0;
356 static void provider_schedule_changed(struct vpn_provider *provider, int flag)
358 if (provider->notify_id != 0)
359 g_source_remove(provider->notify_id);
361 provider->what_changed |= flag;
363 provider->notify_id = g_timeout_add(100, provider_send_changed,
367 static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
370 struct vpn_provider *provider = data;
371 DBusMessageIter iter, value;
375 DBG("conn %p", conn);
377 if (dbus_message_iter_init(msg, &iter) == FALSE)
378 return __connman_error_invalid_arguments(msg);
380 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
381 return __connman_error_invalid_arguments(msg);
383 dbus_message_iter_get_basic(&iter, &name);
384 dbus_message_iter_next(&iter);
386 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
387 return __connman_error_invalid_arguments(msg);
389 dbus_message_iter_recurse(&iter, &value);
391 type = dbus_message_iter_get_arg_type(&value);
393 if (g_str_equal(name, "UserRoutes") == TRUE) {
396 if (type != DBUS_TYPE_ARRAY)
397 return __connman_error_invalid_arguments(msg);
399 networks = get_user_networks(&value);
400 if (networks != NULL) {
401 del_routes(provider);
402 provider->user_networks = networks;
403 set_user_networks(provider, provider->user_networks);
405 if (handle_routes == FALSE)
406 provider_schedule_changed(provider,
407 USER_ROUTES_CHANGED);
410 return __connman_error_invalid_property(msg);
412 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
415 static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
418 struct vpn_provider *provider = data;
421 DBG("conn %p", conn);
423 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
426 if (g_str_equal(name, "UserRoutes") == TRUE) {
427 del_routes(provider);
429 if (handle_routes == FALSE)
430 provider_property_changed(provider, name);
432 return __connman_error_invalid_property(msg);
435 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
438 static DBusMessage *do_connect(DBusConnection *conn, DBusMessage *msg,
441 struct vpn_provider *provider = data;
444 DBG("conn %p provider %p", conn, provider);
446 err = __vpn_provider_connect(provider, msg);
448 return __connman_error_failed(msg, -err);
453 static DBusMessage *do_disconnect(DBusConnection *conn, DBusMessage *msg,
456 struct vpn_provider *provider = data;
459 DBG("conn %p provider %p", conn, provider);
461 err = __vpn_provider_disconnect(provider);
462 if (err < 0 && err != -EINPROGRESS)
463 return __connman_error_failed(msg, -err);
465 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
468 static const GDBusMethodTable connection_methods[] = {
469 { GDBUS_METHOD("SetProperty",
470 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
471 NULL, set_property) },
472 { GDBUS_METHOD("ClearProperty",
473 GDBUS_ARGS({ "name", "s" }), NULL,
475 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, do_connect) },
476 { GDBUS_METHOD("Disconnect", NULL, NULL, do_disconnect) },
480 static const GDBusSignalTable connection_signals[] = {
481 { GDBUS_SIGNAL("PropertyChanged",
482 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
486 static void resolv_result(GResolvResultStatus status,
487 char **results, gpointer user_data)
489 struct vpn_provider *provider = user_data;
491 DBG("status %d", status);
493 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
494 g_strv_length(results) > 0)
495 provider->host_ip = g_strdupv(results);
497 vpn_provider_unref(provider);
500 static void provider_resolv_host_addr(struct vpn_provider *provider)
502 if (provider->host == NULL)
505 if (connman_inet_check_ipaddress(provider->host) > 0)
508 if (provider->host_ip != NULL)
512 * If the hostname is not numeric, try to resolv it. We do not wait
513 * the result as it might take some time. We will get the result
514 * before VPN will feed routes to us because VPN client will need
515 * the IP address also before VPN connection can be established.
517 provider->resolv = g_resolv_new(0);
518 if (provider->resolv == NULL) {
519 DBG("Cannot resolv %s", provider->host);
523 DBG("Trying to resolv %s", provider->host);
525 vpn_provider_ref(provider);
527 g_resolv_lookup_hostname(provider->resolv, provider->host,
528 resolv_result, provider);
531 void __vpn_provider_append_properties(struct vpn_provider *provider,
532 DBusMessageIter *iter)
534 if (provider->host != NULL)
535 connman_dbus_dict_append_basic(iter, "Host",
536 DBUS_TYPE_STRING, &provider->host);
538 if (provider->domain != NULL)
539 connman_dbus_dict_append_basic(iter, "Domain",
540 DBUS_TYPE_STRING, &provider->domain);
542 if (provider->type != NULL)
543 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
547 int __vpn_provider_append_user_route(struct vpn_provider *provider,
548 int family, const char *network, const char *netmask)
550 struct vpn_route *route;
551 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
553 DBG("family %d network %s netmask %s", family, network, netmask);
555 route = g_hash_table_lookup(provider->user_routes, key);
557 route = g_try_new0(struct vpn_route, 1);
559 connman_error("out of memory");
563 route->family = family;
564 route->network = g_strdup(network);
565 route->netmask = g_strdup(netmask);
567 g_hash_table_replace(provider->user_routes, key, route);
574 static struct vpn_route *get_route(char *route_str)
576 char **elems = g_strsplit(route_str, "/", 0);
577 char *network, *netmask, *gateway, *family_str;
578 int family = PF_UNSPEC;
579 struct vpn_route *route = NULL;
584 family_str = elems[0];
587 if (network == NULL || network[0] == '\0')
591 if (netmask == NULL || netmask[0] == '\0')
596 route = g_try_new0(struct vpn_route, 1);
600 if (family_str[0] == '\0' || atoi(family_str) == 0) {
603 switch (family_str[0]) {
613 if (g_strrstr(network, ":") != NULL) {
614 if (family != PF_UNSPEC && family != AF_INET6)
615 DBG("You have IPv6 address but you have non IPv6 route");
616 } else if (g_strrstr(network, ".") != NULL) {
617 if (family != PF_UNSPEC && family != AF_INET)
618 DBG("You have IPv4 address but you have non IPv4 route");
620 if (g_strrstr(netmask, ".") == NULL) {
621 /* We have netmask length */
623 struct in_addr netmask_in;
624 unsigned char prefix_len = 32;
626 if (netmask != NULL) {
628 long int value = strtol(netmask, &ptr, 10);
629 if (ptr != netmask && *ptr == '\0' &&
634 addr = 0xffffffff << (32 - prefix_len);
635 netmask_in.s_addr = htonl(addr);
636 netmask = inet_ntoa(netmask_in);
638 DBG("network %s netmask %s", network, netmask);
642 if (family == PF_UNSPEC) {
643 family = connman_inet_check_ipaddress(network);
644 if (family < 0 || family == PF_UNSPEC)
648 route->family = family;
649 route->network = g_strdup(network);
650 route->netmask = g_strdup(netmask);
651 route->gateway = g_strdup(gateway);
658 static GSList *get_routes(gchar **networks)
660 struct vpn_route *route;
661 GSList *routes = NULL;
664 for (i = 0; networks[i] != NULL; i++) {
665 route = get_route(networks[i]);
667 routes = g_slist_prepend(routes, route);
673 static int provider_load_from_keyfile(struct vpn_provider *provider,
679 gsize length, num_user_networks;
680 gchar **networks = NULL;
682 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
684 if (settings == NULL) {
685 g_key_file_free(keyfile);
689 while (idx < length) {
692 if (g_str_equal(key, "Networks") == TRUE) {
693 networks = g_key_file_get_string_list(keyfile,
694 provider->identifier,
698 provider->user_networks = get_routes(networks);
701 value = g_key_file_get_string(keyfile,
702 provider->identifier,
704 vpn_provider_set_string(provider, key,
711 g_strfreev(settings);
712 g_strfreev(networks);
714 if (provider->user_networks != NULL)
715 set_user_networks(provider, provider->user_networks);
721 static int vpn_provider_load(struct vpn_provider *provider)
725 DBG("provider %p", provider);
727 keyfile = __connman_storage_load_provider(provider->identifier);
731 provider_load_from_keyfile(provider, keyfile);
733 g_key_file_free(keyfile);
737 static gchar **create_network_list(GSList *networks, gsize *count)
740 gchar **result = NULL;
741 unsigned int num_elems = 0;
743 for (list = networks; list != NULL; list = g_slist_next(list)) {
744 struct vpn_route *route = list->data;
747 result = g_try_realloc(result,
748 (num_elems + 1) * sizeof(gchar *));
752 switch (route->family) {
764 result[num_elems] = g_strdup_printf("%d/%s/%s/%s",
765 family, route->network, route->netmask,
766 route->gateway == NULL ? "" : route->gateway);
771 result = g_try_realloc(result, (num_elems + 1) * sizeof(gchar *));
775 result[num_elems] = NULL;
780 static int vpn_provider_save(struct vpn_provider *provider)
784 DBG("provider %p", provider);
786 keyfile = g_key_file_new();
790 g_key_file_set_string(keyfile, provider->identifier,
791 "Name", provider->name);
792 g_key_file_set_string(keyfile, provider->identifier,
793 "Type", provider->type);
794 g_key_file_set_string(keyfile, provider->identifier,
795 "Host", provider->host);
796 g_key_file_set_string(keyfile, provider->identifier,
797 "VPN.Domain", provider->domain);
798 if (provider->user_networks != NULL) {
802 networks = create_network_list(provider->user_networks,
804 if (networks != NULL) {
805 g_key_file_set_string_list(keyfile,
806 provider->identifier,
808 (const gchar ** const)networks,
810 g_strfreev(networks);
814 if (provider->config_file != NULL && strlen(provider->config_file) > 0)
815 g_key_file_set_string(keyfile, provider->identifier,
816 "Config.file", provider->config_file);
818 if (provider->config_entry != NULL &&
819 strlen(provider->config_entry) > 0)
820 g_key_file_set_string(keyfile, provider->identifier,
821 "Config.ident", provider->config_entry);
823 if (provider->driver != NULL && provider->driver->save != NULL)
824 provider->driver->save(provider, keyfile);
826 __connman_storage_save_provider(keyfile, provider->identifier);
827 g_key_file_free(keyfile);
832 struct vpn_provider *__vpn_provider_lookup(const char *identifier)
834 struct vpn_provider *provider = NULL;
836 provider = g_hash_table_lookup(provider_hash, identifier);
841 static gboolean match_driver(struct vpn_provider *provider,
842 struct vpn_provider_driver *driver)
844 if (g_strcmp0(driver->name, provider->type) == 0)
850 static int provider_probe(struct vpn_provider *provider)
854 DBG("provider %p driver %p name %s", provider, provider->driver,
857 if (provider->driver != NULL)
860 for (list = driver_list; list; list = list->next) {
861 struct vpn_provider_driver *driver = list->data;
863 if (match_driver(provider, driver) == FALSE)
866 DBG("driver %p name %s", driver, driver->name);
868 if (driver->probe != NULL && driver->probe(provider) == 0) {
869 provider->driver = driver;
874 if (provider->driver == NULL)
880 static void provider_remove(struct vpn_provider *provider)
882 if (provider->driver != NULL) {
883 provider->driver->remove(provider);
884 provider->driver = NULL;
888 static int provider_register(struct vpn_provider *provider)
890 return provider_probe(provider);
893 static void provider_unregister(struct vpn_provider *provider)
895 provider_remove(provider);
898 struct vpn_provider *
899 vpn_provider_ref_debug(struct vpn_provider *provider,
900 const char *file, int line, const char *caller)
902 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
905 __sync_fetch_and_add(&provider->refcount, 1);
910 static void provider_destruct(struct vpn_provider *provider)
912 DBG("provider %p", provider);
914 if (provider->notify_id != 0)
915 g_source_remove(provider->notify_id);
917 g_free(provider->name);
918 g_free(provider->type);
919 g_free(provider->host);
920 g_free(provider->domain);
921 g_free(provider->identifier);
922 g_free(provider->path);
923 g_slist_free_full(provider->user_networks, free_route);
924 g_strfreev(provider->nameservers);
925 g_hash_table_destroy(provider->routes);
926 g_hash_table_destroy(provider->user_routes);
927 g_hash_table_destroy(provider->setting_strings);
928 if (provider->resolv != NULL) {
929 g_resolv_unref(provider->resolv);
930 provider->resolv = NULL;
932 __vpn_ipconfig_unref(provider->ipconfig_ipv4);
933 __vpn_ipconfig_unref(provider->ipconfig_ipv6);
935 g_strfreev(provider->host_ip);
936 g_free(provider->config_file);
937 g_free(provider->config_entry);
941 void vpn_provider_unref_debug(struct vpn_provider *provider,
942 const char *file, int line, const char *caller)
944 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
947 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
950 provider_remove(provider);
952 provider_destruct(provider);
955 static void configuration_count_add(void)
957 DBG("count %d", configuration_count + 1);
959 __sync_fetch_and_add(&configuration_count, 1);
962 static void configuration_count_del(void)
964 DBG("count %d", configuration_count - 1);
966 if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
972 int __vpn_provider_disconnect(struct vpn_provider *provider)
976 DBG("provider %p", provider);
978 if (provider->driver != NULL && provider->driver->disconnect != NULL)
979 err = provider->driver->disconnect(provider);
983 if (err == -EINPROGRESS)
984 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
989 static void connect_cb(struct vpn_provider *provider, void *user_data,
992 DBusMessage *pending = user_data;
994 DBG("provider %p user %p error %d", provider, user_data, error);
997 DBusMessage *reply = __connman_error_failed(pending, error);
999 g_dbus_send_message(connection, reply);
1001 vpn_provider_indicate_error(provider,
1002 VPN_PROVIDER_ERROR_CONNECT_FAILED);
1003 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
1005 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
1007 dbus_message_unref(pending);
1010 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
1014 DBG("provider %p", provider);
1016 if (provider->driver != NULL && provider->driver->connect != NULL) {
1017 dbus_message_ref(msg);
1018 err = provider->driver->connect(provider, connect_cb, msg);
1022 if (err == -EINPROGRESS)
1023 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1028 static void connection_removed_signal(struct vpn_provider *provider)
1030 DBusMessage *signal;
1031 DBusMessageIter iter;
1033 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1034 VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1038 dbus_message_iter_init_append(signal, &iter);
1039 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1041 dbus_connection_send(connection, signal, NULL);
1042 dbus_message_unref(signal);
1045 static char *get_ident(const char *path)
1052 pos = strrchr(path, '/');
1059 int __vpn_provider_remove(const char *path)
1061 struct vpn_provider *provider;
1064 DBG("path %s", path);
1066 ident = get_ident(path);
1068 provider = __vpn_provider_lookup(ident);
1069 if (provider != NULL)
1070 return __vpn_provider_delete(provider);
1075 int __vpn_provider_delete(struct vpn_provider *provider)
1077 DBG("Deleting VPN %s", provider->identifier);
1079 connection_removed_signal(provider);
1081 provider_unregister(provider);
1083 __connman_storage_remove_provider(provider->identifier);
1085 g_hash_table_remove(provider_hash, provider->identifier);
1090 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1092 struct vpn_provider *provider = user_data;
1093 const char *address, *gateway, *peer;
1095 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1096 if (address != NULL) {
1098 struct in_addr netmask;
1102 prefixlen = __vpn_ipconfig_get_prefixlen(
1103 provider->ipconfig_ipv4);
1105 addr = 0xffffffff << (32 - prefixlen);
1106 netmask.s_addr = htonl(addr);
1107 mask = inet_ntoa(netmask);
1109 connman_dbus_dict_append_basic(iter, "Address",
1110 DBUS_TYPE_STRING, &address);
1112 connman_dbus_dict_append_basic(iter, "Netmask",
1113 DBUS_TYPE_STRING, &mask);
1116 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1117 if (gateway != NULL)
1118 connman_dbus_dict_append_basic(iter, "Gateway",
1119 DBUS_TYPE_STRING, &gateway);
1121 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1123 connman_dbus_dict_append_basic(iter, "Peer",
1124 DBUS_TYPE_STRING, &peer);
1127 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1129 struct vpn_provider *provider = user_data;
1130 const char *address, *gateway, *peer;
1132 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1133 if (address != NULL) {
1134 unsigned char prefixlen;
1136 connman_dbus_dict_append_basic(iter, "Address",
1137 DBUS_TYPE_STRING, &address);
1139 prefixlen = __vpn_ipconfig_get_prefixlen(
1140 provider->ipconfig_ipv6);
1142 connman_dbus_dict_append_basic(iter, "PrefixLength",
1143 DBUS_TYPE_BYTE, &prefixlen);
1146 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1147 if (gateway != NULL)
1148 connman_dbus_dict_append_basic(iter, "Gateway",
1149 DBUS_TYPE_STRING, &gateway);
1151 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1153 connman_dbus_dict_append_basic(iter, "Peer",
1154 DBUS_TYPE_STRING, &peer);
1157 static const char *state2string(enum vpn_provider_state state)
1160 case VPN_PROVIDER_STATE_UNKNOWN:
1162 case VPN_PROVIDER_STATE_IDLE:
1164 case VPN_PROVIDER_STATE_CONNECT:
1165 return "configuration";
1166 case VPN_PROVIDER_STATE_READY:
1168 case VPN_PROVIDER_STATE_DISCONNECT:
1169 return "disconnect";
1170 case VPN_PROVIDER_STATE_FAILURE:
1177 static int provider_indicate_state(struct vpn_provider *provider,
1178 enum vpn_provider_state state)
1182 DBG("provider %p state %d", provider, state);
1184 str = state2string(state);
1188 provider->state = state;
1190 if (state == VPN_PROVIDER_STATE_READY) {
1191 connman_dbus_property_changed_basic(provider->path,
1192 VPN_CONNECTION_INTERFACE, "Index",
1193 DBUS_TYPE_INT32, &provider->index);
1195 if (provider->family == AF_INET)
1196 connman_dbus_property_changed_dict(provider->path,
1197 VPN_CONNECTION_INTERFACE, "IPv4",
1198 append_ipv4, provider);
1199 else if (provider->family == AF_INET6)
1200 connman_dbus_property_changed_dict(provider->path,
1201 VPN_CONNECTION_INTERFACE, "IPv6",
1202 append_ipv6, provider);
1205 connman_dbus_property_changed_basic(provider->path,
1206 VPN_CONNECTION_INTERFACE, "State",
1207 DBUS_TYPE_STRING, &str);
1211 static void append_nameservers(DBusMessageIter *iter, char **servers)
1217 for (i = 0; servers[i] != NULL; i++) {
1218 DBG("servers[%d] %s", i, servers[i]);
1219 dbus_message_iter_append_basic(iter,
1220 DBUS_TYPE_STRING, &servers[i]);
1224 static void append_dns(DBusMessageIter *iter, void *user_data)
1226 struct vpn_provider *provider = user_data;
1228 if (provider->nameservers != NULL)
1229 append_nameservers(iter, provider->nameservers);
1232 static void append_state(DBusMessageIter *iter,
1233 struct vpn_provider *provider)
1237 switch (provider->state) {
1238 case VPN_PROVIDER_STATE_UNKNOWN:
1239 case VPN_PROVIDER_STATE_IDLE:
1242 case VPN_PROVIDER_STATE_CONNECT:
1243 str = "configuration";
1245 case VPN_PROVIDER_STATE_READY:
1248 case VPN_PROVIDER_STATE_DISCONNECT:
1251 case VPN_PROVIDER_STATE_FAILURE:
1256 connman_dbus_dict_append_basic(iter, "State",
1257 DBUS_TYPE_STRING, &str);
1260 static void append_properties(DBusMessageIter *iter,
1261 struct vpn_provider *provider)
1263 DBusMessageIter dict;
1265 connman_dbus_dict_open(iter, &dict);
1267 append_state(&dict, provider);
1269 if (provider->type != NULL)
1270 connman_dbus_dict_append_basic(&dict, "Type",
1271 DBUS_TYPE_STRING, &provider->type);
1273 if (provider->name != NULL)
1274 connman_dbus_dict_append_basic(&dict, "Name",
1275 DBUS_TYPE_STRING, &provider->name);
1277 if (provider->host != NULL)
1278 connman_dbus_dict_append_basic(&dict, "Host",
1279 DBUS_TYPE_STRING, &provider->host);
1280 if (provider->index >= 0)
1281 connman_dbus_dict_append_basic(&dict, "Index",
1282 DBUS_TYPE_INT32, &provider->index);
1283 if (provider->domain != NULL)
1284 connman_dbus_dict_append_basic(&dict, "Domain",
1285 DBUS_TYPE_STRING, &provider->domain);
1287 if (provider->family == AF_INET)
1288 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1290 else if (provider->family == AF_INET6)
1291 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1294 connman_dbus_dict_append_array(&dict, "Nameservers",
1295 DBUS_TYPE_STRING, append_dns, provider);
1297 connman_dbus_dict_append_array(&dict, "UserRoutes",
1298 DBUS_TYPE_DICT_ENTRY, append_routes,
1299 provider->user_routes);
1301 connman_dbus_dict_append_array(&dict, "ServerRoutes",
1302 DBUS_TYPE_DICT_ENTRY, append_routes,
1305 connman_dbus_dict_close(iter, &dict);
1308 static void connection_added_signal(struct vpn_provider *provider)
1310 DBusMessage *signal;
1311 DBusMessageIter iter;
1313 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1314 VPN_MANAGER_INTERFACE, "ConnectionAdded");
1318 dbus_message_iter_init_append(signal, &iter);
1319 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1321 append_properties(&iter, provider);
1323 dbus_connection_send(connection, signal, NULL);
1324 dbus_message_unref(signal);
1327 static connman_bool_t check_host(char **hosts, char *host)
1334 for (i = 0; hosts[i] != NULL; i++) {
1335 if (g_strcmp0(hosts[i], host) == 0)
1342 static void provider_append_routes(gpointer key, gpointer value,
1345 struct vpn_route *route = value;
1346 struct vpn_provider *provider = user_data;
1347 int index = provider->index;
1349 if (handle_routes == FALSE)
1353 * If the VPN administrator/user has given a route to
1354 * VPN server, then we must discard that because the
1355 * server cannot be contacted via VPN tunnel.
1357 if (check_host(provider->host_ip, route->network) == TRUE) {
1358 DBG("Discarding VPN route to %s via %s at index %d",
1359 route->network, route->gateway, index);
1363 if (route->family == AF_INET6) {
1364 unsigned char prefix_len = atoi(route->netmask);
1366 connman_inet_add_ipv6_network_route(index, route->network,
1370 connman_inet_add_network_route(index, route->network,
1376 static int set_connected(struct vpn_provider *provider,
1377 connman_bool_t connected)
1379 struct vpn_ipconfig *ipconfig;
1381 DBG("provider %p id %s connected %d", provider,
1382 provider->identifier, connected);
1384 if (connected == TRUE) {
1385 if (provider->family == AF_INET6)
1386 ipconfig = provider->ipconfig_ipv6;
1388 ipconfig = provider->ipconfig_ipv4;
1390 __vpn_ipconfig_address_add(ipconfig, provider->family);
1392 if (handle_routes == TRUE)
1393 __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1395 provider_indicate_state(provider,
1396 VPN_PROVIDER_STATE_READY);
1398 g_hash_table_foreach(provider->routes, provider_append_routes,
1401 g_hash_table_foreach(provider->user_routes,
1402 provider_append_routes, provider);
1405 provider_indicate_state(provider,
1406 VPN_PROVIDER_STATE_DISCONNECT);
1408 provider_indicate_state(provider,
1409 VPN_PROVIDER_STATE_IDLE);
1415 int vpn_provider_set_state(struct vpn_provider *provider,
1416 enum vpn_provider_state state)
1418 if (provider == NULL)
1422 case VPN_PROVIDER_STATE_UNKNOWN:
1424 case VPN_PROVIDER_STATE_IDLE:
1425 return set_connected(provider, FALSE);
1426 case VPN_PROVIDER_STATE_CONNECT:
1427 return provider_indicate_state(provider, state);
1428 case VPN_PROVIDER_STATE_READY:
1429 return set_connected(provider, TRUE);
1430 case VPN_PROVIDER_STATE_DISCONNECT:
1431 return provider_indicate_state(provider, state);
1432 case VPN_PROVIDER_STATE_FAILURE:
1433 return provider_indicate_state(provider, state);
1438 int vpn_provider_indicate_error(struct vpn_provider *provider,
1439 enum vpn_provider_error error)
1441 DBG("provider %p id %s error %d", provider, provider->identifier,
1445 case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1447 case VPN_PROVIDER_ERROR_AUTH_FAILED:
1449 case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1458 static int connection_unregister(struct vpn_provider *provider)
1460 DBG("provider %p path %s", provider, provider->path);
1462 if (provider->path == NULL)
1465 g_dbus_unregister_interface(connection, provider->path,
1466 VPN_CONNECTION_INTERFACE);
1468 g_free(provider->path);
1469 provider->path = NULL;
1474 static int connection_register(struct vpn_provider *provider)
1476 DBG("provider %p path %s", provider, provider->path);
1478 if (provider->path != NULL)
1481 provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1482 provider->identifier);
1484 g_dbus_register_interface(connection, provider->path,
1485 VPN_CONNECTION_INTERFACE,
1486 connection_methods, connection_signals,
1487 NULL, provider, NULL);
1492 static void unregister_provider(gpointer data)
1494 struct vpn_provider *provider = data;
1496 configuration_count_del();
1498 connection_unregister(provider);
1500 vpn_provider_unref(provider);
1503 static void provider_initialize(struct vpn_provider *provider)
1505 DBG("provider %p", provider);
1507 provider->index = 0;
1509 provider->name = NULL;
1510 provider->type = NULL;
1511 provider->domain = NULL;
1512 provider->identifier = NULL;
1513 provider->user_networks = NULL;
1514 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1516 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1517 g_free, free_route);
1518 provider->setting_strings = g_hash_table_new_full(g_str_hash,
1519 g_str_equal, g_free, g_free);
1522 static struct vpn_provider *vpn_provider_new(void)
1524 struct vpn_provider *provider;
1526 provider = g_try_new0(struct vpn_provider, 1);
1527 if (provider == NULL)
1530 provider->refcount = 1;
1532 DBG("provider %p", provider);
1533 provider_initialize(provider);
1538 static struct vpn_provider *vpn_provider_get(const char *identifier)
1540 struct vpn_provider *provider;
1542 provider = g_hash_table_lookup(provider_hash, identifier);
1543 if (provider != NULL)
1546 provider = vpn_provider_new();
1547 if (provider == NULL)
1550 DBG("provider %p", provider);
1552 provider->identifier = g_strdup(identifier);
1554 g_hash_table_insert(provider_hash, provider->identifier, provider);
1556 configuration_count_add();
1561 static void provider_dbus_ident(char *ident)
1563 int i, len = strlen(ident);
1565 for (i = 0; i < len; i++) {
1566 if (ident[i] >= '0' && ident[i] <= '9')
1568 if (ident[i] >= 'a' && ident[i] <= 'z')
1570 if (ident[i] >= 'A' && ident[i] <= 'Z')
1576 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1579 struct vpn_provider *provider;
1581 if (keyfile == NULL || ident == NULL)
1584 provider = __vpn_provider_lookup(ident);
1585 if (provider == NULL) {
1586 provider = vpn_provider_get(ident);
1587 if (provider == NULL) {
1588 DBG("can not create provider");
1592 provider_load_from_keyfile(provider, keyfile);
1594 if (provider->name == NULL || provider->host == NULL ||
1595 provider->domain == NULL) {
1596 DBG("cannot get name, host or domain");
1597 vpn_provider_unref(provider);
1601 if (provider_register(provider) == 0)
1602 connection_register(provider);
1607 static void provider_create_all_from_type(const char *provider_type)
1614 DBG("provider type %s", provider_type);
1616 providers = __connman_storage_get_providers();
1618 if (providers == NULL)
1621 for (i = 0; providers[i] != NULL; i+=1) {
1623 if (strncmp(providers[i], "provider_", 9) != 0)
1626 id = providers[i] + 9;
1627 keyfile = __connman_storage_load_provider(id);
1629 if (keyfile == NULL)
1632 type = g_key_file_get_string(keyfile, id, "Type", NULL);
1634 DBG("keyfile %p id %s type %s", keyfile, id, type);
1636 if (strcmp(provider_type, type) != 0) {
1638 g_key_file_free(keyfile);
1642 if (provider_create_from_keyfile(keyfile, id) == NULL)
1643 DBG("could not create provider");
1646 g_key_file_free(keyfile);
1648 g_strfreev(providers);
1651 char *__vpn_provider_create_identifier(const char *host, const char *domain)
1655 ident = g_strdup_printf("%s_%s", host, domain);
1659 provider_dbus_ident(ident);
1664 int __vpn_provider_create(DBusMessage *msg)
1666 struct vpn_provider *provider;
1667 DBusMessageIter iter, array;
1668 const char *type = NULL, *name = NULL;
1669 const char *host = NULL, *domain = NULL;
1670 GSList *networks = NULL;
1674 dbus_message_iter_init(msg, &iter);
1675 dbus_message_iter_recurse(&iter, &array);
1677 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1678 DBusMessageIter entry, value;
1681 dbus_message_iter_recurse(&array, &entry);
1682 dbus_message_iter_get_basic(&entry, &key);
1684 dbus_message_iter_next(&entry);
1685 dbus_message_iter_recurse(&entry, &value);
1687 switch (dbus_message_iter_get_arg_type(&value)) {
1688 case DBUS_TYPE_STRING:
1689 if (g_str_equal(key, "Type") == TRUE)
1690 dbus_message_iter_get_basic(&value, &type);
1691 else if (g_str_equal(key, "Name") == TRUE)
1692 dbus_message_iter_get_basic(&value, &name);
1693 else if (g_str_equal(key, "Host") == TRUE)
1694 dbus_message_iter_get_basic(&value, &host);
1695 else if (g_str_equal(key, "VPN.Domain") == TRUE)
1696 dbus_message_iter_get_basic(&value, &domain);
1698 case DBUS_TYPE_ARRAY:
1699 if (g_str_equal(key, "UserRoutes") == TRUE)
1700 networks = get_user_networks(&value);
1704 dbus_message_iter_next(&array);
1707 if (host == NULL || domain == NULL)
1710 DBG("Type %s name %s networks %p", type, name, networks);
1712 if (type == NULL || name == NULL)
1715 ident = __vpn_provider_create_identifier(host, domain);
1716 DBG("ident %s", ident);
1718 provider = __vpn_provider_lookup(ident);
1719 if (provider == NULL) {
1720 provider = vpn_provider_get(ident);
1721 if (provider == NULL) {
1722 DBG("can not create provider");
1727 provider->host = g_strdup(host);
1728 provider->domain = g_strdup(domain);
1729 provider->name = g_strdup(name);
1730 provider->type = g_strdup(type);
1732 if (provider_register(provider) == 0)
1733 vpn_provider_load(provider);
1735 provider_resolv_host_addr(provider);
1738 if (networks != NULL) {
1739 g_slist_free_full(provider->user_networks, free_route);
1740 provider->user_networks = networks;
1741 set_user_networks(provider, provider->user_networks);
1744 dbus_message_iter_init(msg, &iter);
1745 dbus_message_iter_recurse(&iter, &array);
1747 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1748 DBusMessageIter entry, value;
1749 const char *key, *str;
1751 dbus_message_iter_recurse(&array, &entry);
1752 dbus_message_iter_get_basic(&entry, &key);
1754 dbus_message_iter_next(&entry);
1755 dbus_message_iter_recurse(&entry, &value);
1757 switch (dbus_message_iter_get_arg_type(&value)) {
1758 case DBUS_TYPE_STRING:
1759 dbus_message_iter_get_basic(&value, &str);
1760 vpn_provider_set_string(provider, key, str);
1764 dbus_message_iter_next(&array);
1769 vpn_provider_save(provider);
1771 err = provider_register(provider);
1772 if (err != 0 && err != -EALREADY)
1775 connection_register(provider);
1777 DBG("provider %p index %d path %s", provider, provider->index,
1780 g_dbus_send_reply(connection, msg,
1781 DBUS_TYPE_OBJECT_PATH, &provider->path,
1784 connection_added_signal(provider);
1789 static const char *get_string(GHashTable *settings, const char *key)
1791 DBG("settings %p key %s", settings, key);
1793 return g_hash_table_lookup(settings, key);
1796 static GSList *parse_user_networks(const char *network_str)
1801 int __vpn_provider_create_from_config(GHashTable *settings,
1802 const char *config_ident,
1803 const char *config_entry)
1805 struct vpn_provider *provider;
1806 const char *type, *name, *host, *domain, *networks_str;
1809 GHashTableIter hash;
1810 gpointer value, key;
1813 type = get_string(settings, "Type");
1814 name = get_string(settings, "Name");
1815 host = get_string(settings, "Host");
1816 domain = get_string(settings, "Domain");
1817 networks_str = get_string(settings, "Networks");
1818 networks = parse_user_networks(networks_str);
1820 if (host == NULL || domain == NULL) {
1825 DBG("type %s name %s networks %s", type, name, networks_str);
1827 if (type == NULL || name == NULL) {
1832 ident = __vpn_provider_create_identifier(host, domain);
1833 DBG("ident %s", ident);
1835 provider = __vpn_provider_lookup(ident);
1836 if (provider == NULL) {
1837 provider = vpn_provider_get(ident);
1838 if (provider == NULL) {
1839 DBG("can not create provider");
1844 provider->host = g_strdup(host);
1845 provider->domain = g_strdup(domain);
1846 provider->name = g_strdup(name);
1847 provider->type = g_ascii_strdown(type, -1);
1849 provider->config_file = g_strdup(config_ident);
1850 provider->config_entry = g_strdup(config_entry);
1852 if (provider_register(provider) == 0)
1853 vpn_provider_load(provider);
1855 provider_resolv_host_addr(provider);
1858 if (networks != NULL) {
1859 g_slist_free_full(provider->user_networks, free_route);
1860 provider->user_networks = networks;
1861 set_user_networks(provider, provider->user_networks);
1864 g_hash_table_iter_init(&hash, settings);
1866 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE)
1867 vpn_provider_set_string(provider, key, value);
1869 vpn_provider_save(provider);
1871 err = provider_register(provider);
1872 if (err != 0 && err != -EALREADY)
1875 connection_register(provider);
1877 DBG("provider %p index %d path %s", provider, provider->index,
1880 connection_added_signal(provider);
1886 if (networks != NULL)
1887 g_slist_free_full(networks, free_route);
1892 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
1894 DBusMessageIter entry;
1895 GHashTableIter hash;
1896 gpointer value, key;
1898 g_hash_table_iter_init(&hash, provider_hash);
1900 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
1901 struct vpn_provider *provider = value;
1903 DBG("path %s", provider->path);
1905 if (provider->identifier == NULL)
1908 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
1910 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1912 append_properties(&entry, provider);
1913 dbus_message_iter_close_container(iter, &entry);
1917 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
1923 reply = dbus_message_new_method_return(msg);
1927 __connman_dbus_append_objpath_dict_array(reply,
1928 append_connection_structs, NULL);
1933 const char * __vpn_provider_get_ident(struct vpn_provider *provider)
1935 if (provider == NULL)
1938 return provider->identifier;
1941 int vpn_provider_set_string(struct vpn_provider *provider,
1942 const char *key, const char *value)
1944 DBG("provider %p key %s value %s", provider, key, value);
1946 if (g_str_equal(key, "Type") == TRUE) {
1947 g_free(provider->type);
1948 provider->type = g_ascii_strdown(value, -1);
1949 } else if (g_str_equal(key, "Name") == TRUE) {
1950 g_free(provider->name);
1951 provider->name = g_strdup(value);
1952 } else if (g_str_equal(key, "Host") == TRUE) {
1953 g_free(provider->host);
1954 provider->host = g_strdup(value);
1955 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1956 g_free(provider->domain);
1957 provider->domain = g_strdup(value);
1959 g_hash_table_replace(provider->setting_strings,
1960 g_strdup(key), g_strdup(value));
1964 const char *vpn_provider_get_string(struct vpn_provider *provider,
1967 DBG("provider %p key %s", provider, key);
1969 if (g_str_equal(key, "Type") == TRUE)
1970 return provider->type;
1971 else if (g_str_equal(key, "Name") == TRUE)
1972 return provider->name;
1973 else if (g_str_equal(key, "Host") == TRUE)
1974 return provider->host;
1975 else if (g_str_equal(key, "HostIP") == TRUE) {
1976 if (provider->host_ip == NULL ||
1977 provider->host_ip[0] == NULL)
1978 return provider->host;
1980 return provider->host_ip[0];
1981 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1982 return provider->domain;
1984 return g_hash_table_lookup(provider->setting_strings, key);
1987 connman_bool_t __vpn_provider_check_routes(struct vpn_provider *provider)
1989 if (provider == NULL)
1992 if (provider->user_routes != NULL &&
1993 g_hash_table_size(provider->user_routes) > 0)
1996 if (provider->routes != NULL &&
1997 g_hash_table_size(provider->routes) > 0)
2003 void *vpn_provider_get_data(struct vpn_provider *provider)
2005 return provider->driver_data;
2008 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
2010 provider->driver_data = data;
2013 void vpn_provider_set_index(struct vpn_provider *provider, int index)
2015 DBG("index %d provider %p", index, provider);
2017 if (provider->ipconfig_ipv4 == NULL) {
2018 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
2020 if (provider->ipconfig_ipv4 == NULL) {
2021 DBG("Couldnt create ipconfig for IPv4");
2026 __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
2028 if (provider->ipconfig_ipv6 == NULL) {
2029 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
2031 if (provider->ipconfig_ipv6 == NULL) {
2032 DBG("Couldnt create ipconfig for IPv6");
2037 __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
2040 provider->index = index;
2043 int vpn_provider_get_index(struct vpn_provider *provider)
2045 return provider->index;
2048 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
2049 struct connman_ipaddress *ipaddress)
2051 struct vpn_ipconfig *ipconfig = NULL;
2053 switch (ipaddress->family) {
2055 ipconfig = provider->ipconfig_ipv4;
2058 ipconfig = provider->ipconfig_ipv6;
2064 DBG("provider %p ipconfig %p family %d", provider, ipconfig,
2067 if (ipconfig == NULL)
2070 provider->family = ipaddress->family;
2072 __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
2073 __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
2074 __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
2075 __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
2076 __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
2081 int vpn_provider_set_pac(struct vpn_provider *provider,
2084 DBG("provider %p pac %s", provider, pac);
2090 int vpn_provider_set_domain(struct vpn_provider *provider,
2093 DBG("provider %p domain %s", provider, domain);
2095 g_free(provider->domain);
2096 provider->domain = g_strdup(domain);
2101 int vpn_provider_set_nameservers(struct vpn_provider *provider,
2102 const char *nameservers)
2104 DBG("provider %p nameservers %s", provider, nameservers);
2106 g_strfreev(provider->nameservers);
2107 provider->nameservers = NULL;
2109 if (nameservers == NULL)
2112 provider->nameservers = g_strsplit(nameservers, " ", 0);
2117 enum provider_route_type {
2118 PROVIDER_ROUTE_TYPE_NONE = 0,
2119 PROVIDER_ROUTE_TYPE_MASK = 1,
2120 PROVIDER_ROUTE_TYPE_ADDR = 2,
2121 PROVIDER_ROUTE_TYPE_GW = 3,
2124 static int route_env_parse(struct vpn_provider *provider, const char *key,
2125 int *family, unsigned long *idx,
2126 enum provider_route_type *type)
2131 DBG("name %s", provider->name);
2133 if (!strcmp(provider->type, "openvpn")) {
2134 if (g_str_has_prefix(key, "route_network_") == TRUE) {
2135 start = key + strlen("route_network_");
2136 *type = PROVIDER_ROUTE_TYPE_ADDR;
2137 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
2138 start = key + strlen("route_netmask_");
2139 *type = PROVIDER_ROUTE_TYPE_MASK;
2140 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
2141 start = key + strlen("route_gateway_");
2142 *type = PROVIDER_ROUTE_TYPE_GW;
2147 *idx = g_ascii_strtoull(start, &end, 10);
2149 } else if (!strcmp(provider->type, "openconnect")) {
2150 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
2152 start = key + strlen("CISCO_SPLIT_INC_");
2153 } else if (g_str_has_prefix(key,
2154 "CISCO_IPV6_SPLIT_INC_") == TRUE) {
2156 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
2160 *idx = g_ascii_strtoull(start, &end, 10);
2162 if (strncmp(end, "_ADDR", 5) == 0)
2163 *type = PROVIDER_ROUTE_TYPE_ADDR;
2164 else if (strncmp(end, "_MASK", 5) == 0)
2165 *type = PROVIDER_ROUTE_TYPE_MASK;
2166 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2167 *family == AF_INET6) {
2168 *type = PROVIDER_ROUTE_TYPE_MASK;
2176 int vpn_provider_append_route(struct vpn_provider *provider,
2177 const char *key, const char *value)
2179 struct vpn_route *route;
2180 int ret, family = 0;
2181 unsigned long idx = 0;
2182 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2184 DBG("key %s value %s", key, value);
2186 ret = route_env_parse(provider, key, &family, &idx, &type);
2190 DBG("idx %lu family %d type %d", idx, family, type);
2192 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2193 if (route == NULL) {
2194 route = g_try_new0(struct vpn_route, 1);
2195 if (route == NULL) {
2196 connman_error("out of memory");
2200 route->family = family;
2202 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2207 case PROVIDER_ROUTE_TYPE_NONE:
2209 case PROVIDER_ROUTE_TYPE_MASK:
2210 route->netmask = g_strdup(value);
2212 case PROVIDER_ROUTE_TYPE_ADDR:
2213 route->network = g_strdup(value);
2215 case PROVIDER_ROUTE_TYPE_GW:
2216 route->gateway = g_strdup(value);
2220 if (handle_routes == FALSE) {
2221 if (route->netmask != NULL && route->gateway != NULL &&
2222 route->network != NULL)
2223 provider_schedule_changed(provider,
2224 SERVER_ROUTES_CHANGED);
2230 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2232 if (provider->driver == NULL)
2235 return provider->driver->name;
2238 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2240 return provider->identifier;
2243 static gint compare_priority(gconstpointer a, gconstpointer b)
2248 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2250 struct vpn_provider *provider = value;
2252 if (provider->driver != NULL && provider->driver->remove)
2253 provider->driver->remove(provider);
2255 connection_unregister(provider);
2258 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2260 DBG("driver %p name %s", driver, driver->name);
2262 driver_list = g_slist_insert_sorted(driver_list, driver,
2264 provider_create_all_from_type(driver->name);
2268 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2270 GHashTableIter iter;
2271 gpointer value, key;
2273 DBG("driver %p name %s", driver, driver->name);
2275 driver_list = g_slist_remove(driver_list, driver);
2277 g_hash_table_iter_init(&iter, provider_hash);
2278 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2279 struct vpn_provider *provider = value;
2281 if (provider != NULL && provider->driver != NULL &&
2282 provider->driver->type == driver->type &&
2283 g_strcmp0(provider->driver->name,
2284 driver->name) == 0) {
2285 provider->driver = NULL;
2290 static gboolean check_vpn_count(gpointer data)
2292 if (configuration_count == 0) {
2293 connman_info("No VPN configurations found, quitting.");
2300 void __vpn_provider_check_connections(void)
2303 * If we were started when there is no providers configured,
2304 * then just quit. This happens when connman starts and its
2305 * vpn plugin asks connman-vpnd if it has any connections
2306 * configured. If there are none, then we can stop the vpn
2309 g_timeout_add(1000, check_vpn_count, NULL);
2312 const char *vpn_provider_get_name(struct vpn_provider *provider)
2314 return provider->name;
2317 const char *vpn_provider_get_host(struct vpn_provider *provider)
2319 return provider->host;
2322 const char *vpn_provider_get_path(struct vpn_provider *provider)
2324 return provider->path;
2327 static int agent_probe(struct connman_agent *agent)
2329 DBG("agent %p", agent);
2333 static void agent_remove(struct connman_agent *agent)
2335 DBG("agent %p", agent);
2338 static struct connman_agent_driver agent_driver = {
2340 .interface = VPN_AGENT_INTERFACE,
2341 .probe = agent_probe,
2342 .remove = agent_remove,
2345 static void remove_unprovisioned_providers()
2348 GKeyFile *keyfile, *configkeyfile;
2349 char *file, *section;
2352 providers = __connman_storage_get_providers();
2353 if (providers == NULL)
2356 for (; providers[i] != NULL; i++) {
2357 char *group = providers[i] + sizeof("provider_") - 1;
2358 file = section = NULL;
2359 keyfile = configkeyfile = NULL;
2361 keyfile = __connman_storage_load_provider(group);
2362 if (keyfile == NULL)
2365 file = g_key_file_get_string(keyfile, group,
2366 "Config.file", NULL);
2370 section = g_key_file_get_string(keyfile, group,
2371 "Config.ident", NULL);
2372 if (section == NULL)
2375 configkeyfile = __connman_storage_load_provider_config(file);
2376 if (configkeyfile == NULL) {
2378 * Config file is missing, remove the provisioned
2381 __connman_storage_remove_provider(group);
2385 if (g_key_file_has_group(configkeyfile, section) == FALSE)
2387 * Config section is missing, remove the provisioned
2390 __connman_storage_remove_provider(group);
2393 if (keyfile != NULL)
2394 g_key_file_free(keyfile);
2396 if (configkeyfile != NULL)
2397 g_key_file_free(configkeyfile);
2403 g_strfreev(providers);
2406 int __vpn_provider_init(gboolean do_routes)
2412 handle_routes = do_routes;
2414 err = connman_agent_driver_register(&agent_driver);
2416 connman_error("Cannot register agent driver for %s",
2421 connection = connman_dbus_get_connection();
2423 remove_unprovisioned_providers();
2425 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2426 NULL, unregister_provider);
2430 void __vpn_provider_cleanup(void)
2434 g_hash_table_foreach(provider_hash, clean_provider, NULL);
2436 g_hash_table_destroy(provider_hash);
2437 provider_hash = NULL;
2439 connman_agent_driver_unregister(&agent_driver);
2441 dbus_connection_unref(connection);