5 * Copyright (C) 2012-2013 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"
41 static DBusConnection *connection;
42 static GHashTable *provider_hash;
43 static GSList *driver_list;
44 static int configuration_count;
45 static bool handle_routes;
64 enum vpn_provider_state state;
73 struct vpn_provider_driver *driver;
75 GHashTable *setting_strings;
76 GHashTable *user_routes;
77 GSList *user_networks;
80 struct vpn_ipconfig *ipconfig_ipv4;
81 struct vpn_ipconfig *ipconfig_ipv6;
87 struct connman_ipaddress *prev_ipv4_addr;
88 struct connman_ipaddress *prev_ipv6_addr;
91 static void append_properties(DBusMessageIter *iter,
92 struct vpn_provider *provider);
94 static void free_route(gpointer data)
96 struct vpn_route *route = data;
98 g_free(route->network);
99 g_free(route->netmask);
100 g_free(route->gateway);
105 static void free_setting(gpointer data)
107 struct vpn_setting *setting = data;
109 g_free(setting->value);
113 static void append_route(DBusMessageIter *iter, void *user_data)
115 struct vpn_route *route = user_data;
116 DBusMessageIter item;
119 connman_dbus_dict_open(iter, &item);
124 if (route->family == AF_INET)
126 else if (route->family == AF_INET6)
130 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
131 DBUS_TYPE_INT32, &family);
134 connman_dbus_dict_append_basic(&item, "Network",
135 DBUS_TYPE_STRING, &route->network);
138 connman_dbus_dict_append_basic(&item, "Netmask",
139 DBUS_TYPE_STRING, &route->netmask);
142 connman_dbus_dict_append_basic(&item, "Gateway",
143 DBUS_TYPE_STRING, &route->gateway);
146 connman_dbus_dict_close(iter, &item);
149 static void append_routes(DBusMessageIter *iter, void *user_data)
151 GHashTable *routes = user_data;
156 append_route(iter, NULL);
160 g_hash_table_iter_init(&hash, routes);
162 while (g_hash_table_iter_next(&hash, &key, &value)) {
163 DBusMessageIter dict;
165 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
167 append_route(&dict, value);
168 dbus_message_iter_close_container(iter, &dict);
172 static void send_routes(struct vpn_provider *provider, GHashTable *routes,
175 connman_dbus_property_changed_array(provider->path,
176 VPN_CONNECTION_INTERFACE,
178 DBUS_TYPE_DICT_ENTRY,
183 static int provider_routes_changed(struct vpn_provider *provider)
185 DBG("provider %p", provider);
187 send_routes(provider, provider->routes, "ServerRoutes");
192 static GSList *read_route_dict(GSList *routes, DBusMessageIter *dicts)
194 DBusMessageIter dict, value, entry;
195 const char *network, *netmask, *gateway;
196 struct vpn_route *route;
200 dbus_message_iter_recurse(dicts, &entry);
202 network = netmask = gateway = NULL;
205 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_DICT_ENTRY) {
207 dbus_message_iter_recurse(&entry, &dict);
208 dbus_message_iter_get_basic(&dict, &key);
210 dbus_message_iter_next(&dict);
211 dbus_message_iter_recurse(&dict, &value);
213 type = dbus_message_iter_get_arg_type(&value);
216 case DBUS_TYPE_INT32:
217 if (g_str_equal(key, "ProtocolFamily"))
218 dbus_message_iter_get_basic(&value, &family);
221 case DBUS_TYPE_STRING:
222 if (g_str_equal(key, "Network"))
223 dbus_message_iter_get_basic(&value, &network);
224 else if (g_str_equal(key, "Netmask"))
225 dbus_message_iter_get_basic(&value, &netmask);
226 else if (g_str_equal(key, "Gateway"))
227 dbus_message_iter_get_basic(&value, &gateway);
231 dbus_message_iter_next(&entry);
234 DBG("family %d network %s netmask %s gateway %s", family,
235 network, netmask, gateway);
237 if (!network || !netmask) {
238 DBG("Ignoring route as network/netmask is missing");
242 route = g_try_new(struct vpn_route, 1);
244 g_slist_free_full(routes, free_route);
248 if (family == PF_UNSPEC) {
249 family = connman_inet_check_ipaddress(network);
251 DBG("Cannot get address family of %s (%d/%s)", network,
252 family, gai_strerror(family));
271 route->family = family;
272 route->network = g_strdup(network);
273 route->netmask = g_strdup(netmask);
274 route->gateway = g_strdup(gateway);
276 routes = g_slist_prepend(routes, route);
280 static GSList *get_user_networks(DBusMessageIter *array)
282 DBusMessageIter entry;
285 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
287 dbus_message_iter_recurse(array, &entry);
289 while (dbus_message_iter_get_arg_type(&entry) ==
291 DBusMessageIter dicts;
293 dbus_message_iter_recurse(&entry, &dicts);
295 while (dbus_message_iter_get_arg_type(&dicts) ==
298 list = read_route_dict(list, &dicts);
299 dbus_message_iter_next(&dicts);
302 dbus_message_iter_next(&entry);
305 dbus_message_iter_next(array);
311 static void set_user_networks(struct vpn_provider *provider, GSList *networks)
315 for (list = networks; list; list = g_slist_next(list)) {
316 struct vpn_route *route = list->data;
318 if (__vpn_provider_append_user_route(provider,
319 route->family, route->network,
320 route->netmask, route->gateway) != 0)
325 static void del_routes(struct vpn_provider *provider)
330 g_hash_table_iter_init(&hash, provider->user_routes);
331 while (handle_routes && g_hash_table_iter_next(&hash,
333 struct vpn_route *route = value;
334 if (route->family == AF_INET6) {
335 unsigned char prefixlen = atoi(route->netmask);
336 connman_inet_del_ipv6_network_route(provider->index,
340 connman_inet_del_host_route(provider->index,
344 g_hash_table_remove_all(provider->user_routes);
345 g_slist_free_full(provider->user_networks, free_route);
346 provider->user_networks = NULL;
349 static void send_value(const char *path, const char *key, const char *value)
351 const char *empty = "";
359 connman_dbus_property_changed_basic(path,
360 VPN_CONNECTION_INTERFACE,
366 static gboolean provider_send_changed(gpointer data)
368 struct vpn_provider *provider = data;
370 provider_routes_changed(provider);
372 provider->notify_id = 0;
377 static void provider_schedule_changed(struct vpn_provider *provider)
379 if (provider->notify_id != 0)
380 g_source_remove(provider->notify_id);
382 provider->notify_id = g_timeout_add(100, provider_send_changed,
386 static DBusMessage *get_properties(DBusConnection *conn,
387 DBusMessage *msg, void *data)
389 struct vpn_provider *provider = data;
391 DBusMessageIter array;
393 DBG("provider %p", provider);
395 reply = dbus_message_new_method_return(msg);
399 dbus_message_iter_init_append(reply, &array);
401 append_properties(&array, provider);
406 static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
409 struct vpn_provider *provider = data;
410 DBusMessageIter iter, value;
414 DBG("conn %p", conn);
416 if (provider->immutable)
417 return __connman_error_not_supported(msg);
419 if (!dbus_message_iter_init(msg, &iter))
420 return __connman_error_invalid_arguments(msg);
422 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
423 return __connman_error_invalid_arguments(msg);
425 dbus_message_iter_get_basic(&iter, &name);
426 dbus_message_iter_next(&iter);
428 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
429 return __connman_error_invalid_arguments(msg);
431 dbus_message_iter_recurse(&iter, &value);
433 type = dbus_message_iter_get_arg_type(&value);
435 if (g_str_equal(name, "UserRoutes")) {
438 if (type != DBUS_TYPE_ARRAY)
439 return __connman_error_invalid_arguments(msg);
441 networks = get_user_networks(&value);
443 del_routes(provider);
444 provider->user_networks = networks;
445 set_user_networks(provider, provider->user_networks);
448 send_routes(provider, provider->user_routes,
454 dbus_message_iter_get_basic(&value, &str);
455 vpn_provider_set_string(provider, name, str);
458 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
461 static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
464 struct vpn_provider *provider = data;
467 DBG("conn %p", conn);
469 if (provider->immutable)
470 return __connman_error_not_supported(msg);
472 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
475 if (g_str_equal(name, "UserRoutes")) {
476 del_routes(provider);
479 send_routes(provider, provider->user_routes, name);
480 } else if (vpn_provider_get_string(provider, name)) {
481 vpn_provider_set_string(provider, name, NULL);
483 return __connman_error_invalid_property(msg);
486 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
489 static DBusMessage *do_connect(DBusConnection *conn, DBusMessage *msg,
492 struct vpn_provider *provider = data;
495 DBG("conn %p provider %p", conn, provider);
497 err = __vpn_provider_connect(provider, msg);
499 return __connman_error_failed(msg, -err);
504 static DBusMessage *do_connect2(DBusConnection *conn, DBusMessage *msg,
507 return do_connect(conn, msg, data);
510 static DBusMessage *do_disconnect(DBusConnection *conn, DBusMessage *msg,
513 struct vpn_provider *provider = data;
516 DBG("conn %p provider %p", conn, provider);
518 err = __vpn_provider_disconnect(provider);
519 if (err < 0 && err != -EINPROGRESS)
520 return __connman_error_failed(msg, -err);
522 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
525 static const GDBusMethodTable connection_methods[] = {
526 { GDBUS_METHOD("GetProperties",
527 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
529 { GDBUS_METHOD("SetProperty",
530 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
531 NULL, set_property) },
532 { GDBUS_METHOD("ClearProperty",
533 GDBUS_ARGS({ "name", "s" }), NULL,
535 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, do_connect) },
536 { GDBUS_ASYNC_METHOD("Connect2",
537 GDBUS_ARGS({ "dbus_sender", "s" }),
538 NULL, do_connect2) },
539 { GDBUS_METHOD("Disconnect", NULL, NULL, do_disconnect) },
543 static const GDBusSignalTable connection_signals[] = {
544 { GDBUS_SIGNAL("PropertyChanged",
545 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
549 static void resolv_result(GResolvResultStatus status,
550 char **results, gpointer user_data)
552 struct vpn_provider *provider = user_data;
554 DBG("status %d", status);
556 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results &&
557 g_strv_length(results) > 0)
558 provider->host_ip = g_strdupv(results);
560 vpn_provider_unref(provider);
562 /* Remove the resolver here so that it will not be left
563 * hanging around and cause double free in unregister_provider()
565 g_resolv_unref(provider->resolv);
566 provider->resolv = NULL;
569 static void provider_resolv_host_addr(struct vpn_provider *provider)
574 if (connman_inet_check_ipaddress(provider->host) > 0)
577 if (provider->host_ip)
581 * If the hostname is not numeric, try to resolv it. We do not wait
582 * the result as it might take some time. We will get the result
583 * before VPN will feed routes to us because VPN client will need
584 * the IP address also before VPN connection can be established.
586 provider->resolv = g_resolv_new(0);
587 if (!provider->resolv) {
588 DBG("Cannot resolv %s", provider->host);
592 DBG("Trying to resolv %s", provider->host);
594 vpn_provider_ref(provider);
596 g_resolv_lookup_hostname(provider->resolv, provider->host,
597 resolv_result, provider);
600 void __vpn_provider_append_properties(struct vpn_provider *provider,
601 DBusMessageIter *iter)
604 connman_dbus_dict_append_basic(iter, "Host",
605 DBUS_TYPE_STRING, &provider->host);
607 if (provider->domain)
608 connman_dbus_dict_append_basic(iter, "Domain",
609 DBUS_TYPE_STRING, &provider->domain);
612 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
616 int __vpn_provider_append_user_route(struct vpn_provider *provider,
617 int family, const char *network,
618 const char *netmask, const char *gateway)
620 struct vpn_route *route;
621 char *key = g_strdup_printf("%d/%s/%s/%s", family, network,
622 netmask, gateway ? gateway : "");
624 DBG("family %d network %s netmask %s gw %s", family, network,
627 route = g_hash_table_lookup(provider->user_routes, key);
629 route = g_try_new0(struct vpn_route, 1);
631 connman_error("out of memory");
635 route->family = family;
636 route->network = g_strdup(network);
637 route->netmask = g_strdup(netmask);
638 route->gateway = g_strdup(gateway);
640 g_hash_table_replace(provider->user_routes, key, route);
647 static struct vpn_route *get_route(char *route_str)
649 char **elems = g_strsplit(route_str, "/", 0);
650 char *network, *netmask, *gateway, *family_str;
651 int family = PF_UNSPEC;
652 struct vpn_route *route = NULL;
657 family_str = elems[0];
660 if (!network || network[0] == '\0')
664 if (!netmask || netmask[0] == '\0')
669 route = g_try_new0(struct vpn_route, 1);
673 if (family_str[0] == '\0' || atoi(family_str) == 0) {
676 switch (family_str[0]) {
686 if (g_strrstr(network, ":")) {
687 if (family != PF_UNSPEC && family != AF_INET6)
688 DBG("You have IPv6 address but you have non IPv6 route");
689 } else if (g_strrstr(network, ".")) {
690 if (family != PF_UNSPEC && family != AF_INET)
691 DBG("You have IPv4 address but you have non IPv4 route");
693 if (!g_strrstr(netmask, ".")) {
694 /* We have netmask length */
696 struct in_addr netmask_in;
697 unsigned char prefix_len = 32;
699 long int value = strtol(netmask, &ptr, 10);
701 if (ptr != netmask && *ptr == '\0' && value <= 32)
704 addr = 0xffffffff << (32 - prefix_len);
705 netmask_in.s_addr = htonl(addr);
706 netmask = inet_ntoa(netmask_in);
708 DBG("network %s netmask %s", network, netmask);
712 if (family == PF_UNSPEC) {
713 family = connman_inet_check_ipaddress(network);
714 if (family < 0 || family == PF_UNSPEC)
718 route->family = family;
719 route->network = g_strdup(network);
720 route->netmask = g_strdup(netmask);
721 route->gateway = g_strdup(gateway);
728 static GSList *get_routes(gchar **networks)
730 struct vpn_route *route;
731 GSList *routes = NULL;
734 for (i = 0; networks[i]; i++) {
735 route = get_route(networks[i]);
737 routes = g_slist_prepend(routes, route);
743 static int provider_load_from_keyfile(struct vpn_provider *provider,
749 gsize length, num_user_networks;
750 gchar **networks = NULL;
752 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
755 g_key_file_free(keyfile);
759 while (idx < length) {
762 if (g_str_equal(key, "Networks")) {
763 networks = __vpn_config_get_string_list(keyfile,
764 provider->identifier,
768 provider->user_networks = get_routes(networks);
771 value = __vpn_config_get_string(keyfile,
772 provider->identifier,
774 vpn_provider_set_string(provider, key,
781 g_strfreev(settings);
782 g_strfreev(networks);
784 if (provider->user_networks)
785 set_user_networks(provider, provider->user_networks);
791 static int vpn_provider_load(struct vpn_provider *provider)
795 DBG("provider %p", provider);
797 keyfile = __connman_storage_load_provider(provider->identifier);
801 provider_load_from_keyfile(provider, keyfile);
803 g_key_file_free(keyfile);
807 static gchar **create_network_list(GSList *networks, gsize *count)
810 gchar **result = NULL;
812 unsigned int num_elems = 0;
814 for (list = networks; list; list = g_slist_next(list)) {
815 struct vpn_route *route = list->data;
818 prev_result = result;
819 result = g_try_realloc(result,
820 (num_elems + 1) * sizeof(gchar *));
826 switch (route->family) {
838 result[num_elems] = g_strdup_printf("%d/%s/%s/%s",
839 family, route->network, route->netmask,
840 !route->gateway ? "" : route->gateway);
845 prev_result = result;
846 result = g_try_realloc(result, (num_elems + 1) * sizeof(gchar *));
852 result[num_elems] = NULL;
857 static int vpn_provider_save(struct vpn_provider *provider)
861 DBG("provider %p immutable %s", provider,
862 provider->immutable ? "yes" : "no");
864 if (provider->immutable) {
866 * Do not save providers that are provisioned via .config
872 keyfile = g_key_file_new();
876 g_key_file_set_string(keyfile, provider->identifier,
877 "Name", provider->name);
878 g_key_file_set_string(keyfile, provider->identifier,
879 "Type", provider->type);
880 g_key_file_set_string(keyfile, provider->identifier,
881 "Host", provider->host);
882 g_key_file_set_string(keyfile, provider->identifier,
883 "VPN.Domain", provider->domain);
884 if (provider->user_networks) {
888 networks = create_network_list(provider->user_networks,
891 g_key_file_set_string_list(keyfile,
892 provider->identifier,
894 (const gchar ** const)networks,
896 g_strfreev(networks);
900 if (provider->config_file && strlen(provider->config_file) > 0)
901 g_key_file_set_string(keyfile, provider->identifier,
902 "Config.file", provider->config_file);
904 if (provider->config_entry &&
905 strlen(provider->config_entry) > 0)
906 g_key_file_set_string(keyfile, provider->identifier,
907 "Config.ident", provider->config_entry);
909 if (provider->driver && provider->driver->save)
910 provider->driver->save(provider, keyfile);
912 __connman_storage_save_provider(keyfile, provider->identifier);
913 g_key_file_free(keyfile);
918 struct vpn_provider *__vpn_provider_lookup(const char *identifier)
920 struct vpn_provider *provider = NULL;
922 provider = g_hash_table_lookup(provider_hash, identifier);
927 static bool match_driver(struct vpn_provider *provider,
928 struct vpn_provider_driver *driver)
930 if (g_strcmp0(driver->name, provider->type) == 0)
936 static int provider_probe(struct vpn_provider *provider)
940 DBG("provider %p driver %p name %s", provider, provider->driver,
943 if (provider->driver)
946 for (list = driver_list; list; list = list->next) {
947 struct vpn_provider_driver *driver = list->data;
949 if (!match_driver(provider, driver))
952 DBG("driver %p name %s", driver, driver->name);
954 if (driver->probe && driver->probe(provider) == 0) {
955 provider->driver = driver;
960 if (!provider->driver)
966 static void provider_remove(struct vpn_provider *provider)
968 if (provider->driver) {
969 provider->driver->remove(provider);
970 provider->driver = NULL;
974 static int provider_register(struct vpn_provider *provider)
976 return provider_probe(provider);
979 static void provider_unregister(struct vpn_provider *provider)
981 provider_remove(provider);
984 struct vpn_provider *
985 vpn_provider_ref_debug(struct vpn_provider *provider,
986 const char *file, int line, const char *caller)
988 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
991 __sync_fetch_and_add(&provider->refcount, 1);
996 static void provider_destruct(struct vpn_provider *provider)
998 DBG("provider %p", provider);
1000 if (provider->notify_id != 0)
1001 g_source_remove(provider->notify_id);
1003 g_free(provider->name);
1004 g_free(provider->type);
1005 g_free(provider->host);
1006 g_free(provider->domain);
1007 g_free(provider->identifier);
1008 g_free(provider->path);
1009 g_slist_free_full(provider->user_networks, free_route);
1010 g_strfreev(provider->nameservers);
1011 g_hash_table_destroy(provider->routes);
1012 g_hash_table_destroy(provider->user_routes);
1013 g_hash_table_destroy(provider->setting_strings);
1014 if (provider->resolv) {
1015 g_resolv_unref(provider->resolv);
1016 provider->resolv = NULL;
1018 __vpn_ipconfig_unref(provider->ipconfig_ipv4);
1019 __vpn_ipconfig_unref(provider->ipconfig_ipv6);
1021 g_strfreev(provider->host_ip);
1022 g_free(provider->config_file);
1023 g_free(provider->config_entry);
1024 connman_ipaddress_free(provider->prev_ipv4_addr);
1025 connman_ipaddress_free(provider->prev_ipv6_addr);
1029 void vpn_provider_unref_debug(struct vpn_provider *provider,
1030 const char *file, int line, const char *caller)
1032 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
1033 file, line, caller);
1035 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
1038 provider_remove(provider);
1040 provider_destruct(provider);
1043 static void configuration_count_add(void)
1045 DBG("count %d", configuration_count + 1);
1047 __sync_fetch_and_add(&configuration_count, 1);
1050 static void configuration_count_del(void)
1052 DBG("count %d", configuration_count - 1);
1054 if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
1058 int __vpn_provider_disconnect(struct vpn_provider *provider)
1062 DBG("provider %p", provider);
1064 if (provider->driver && provider->driver->disconnect)
1065 err = provider->driver->disconnect(provider);
1069 if (err == -EINPROGRESS)
1070 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1075 static void connect_cb(struct vpn_provider *provider, void *user_data,
1078 DBusMessage *pending = user_data;
1080 DBG("provider %p user %p error %d", provider, user_data, error);
1083 DBusMessage *reply = __connman_error_failed(pending, error);
1085 g_dbus_send_message(connection, reply);
1087 vpn_provider_indicate_error(provider,
1088 VPN_PROVIDER_ERROR_CONNECT_FAILED);
1089 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
1091 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
1093 dbus_message_unref(pending);
1096 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
1100 DBG("provider %p", provider);
1102 if (provider->driver && provider->driver->connect) {
1103 const char *dbus_sender = dbus_message_get_sender(msg);
1105 dbus_message_ref(msg);
1107 if (dbus_message_has_signature(msg,
1108 DBUS_TYPE_STRING_AS_STRING)) {
1109 const char *sender = NULL;
1111 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING,
1112 &sender, DBUS_TYPE_INVALID);
1113 if (sender && sender[0])
1114 dbus_sender = sender;
1117 err = provider->driver->connect(provider, connect_cb,
1122 if (err == -EINPROGRESS)
1123 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1128 static void connection_removed_signal(struct vpn_provider *provider)
1130 DBusMessage *signal;
1131 DBusMessageIter iter;
1133 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1134 VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1138 dbus_message_iter_init_append(signal, &iter);
1139 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1141 dbus_connection_send(connection, signal, NULL);
1142 dbus_message_unref(signal);
1145 static char *get_ident(const char *path)
1152 pos = strrchr(path, '/');
1159 int __vpn_provider_remove(const char *path)
1161 struct vpn_provider *provider;
1164 DBG("path %s", path);
1166 ident = get_ident(path);
1168 provider = __vpn_provider_lookup(ident);
1170 return __vpn_provider_delete(provider);
1175 int __vpn_provider_delete(struct vpn_provider *provider)
1177 DBG("Deleting VPN %s", provider->identifier);
1179 connection_removed_signal(provider);
1181 provider_unregister(provider);
1183 __connman_storage_remove_provider(provider->identifier);
1185 g_hash_table_remove(provider_hash, provider->identifier);
1190 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1192 struct vpn_provider *provider = user_data;
1193 const char *address, *gateway, *peer;
1195 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1198 struct in_addr netmask;
1202 prefixlen = __vpn_ipconfig_get_prefixlen(
1203 provider->ipconfig_ipv4);
1205 addr = 0xffffffff << (32 - prefixlen);
1206 netmask.s_addr = htonl(addr);
1207 mask = inet_ntoa(netmask);
1209 connman_dbus_dict_append_basic(iter, "Address",
1210 DBUS_TYPE_STRING, &address);
1212 connman_dbus_dict_append_basic(iter, "Netmask",
1213 DBUS_TYPE_STRING, &mask);
1216 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1218 connman_dbus_dict_append_basic(iter, "Gateway",
1219 DBUS_TYPE_STRING, &gateway);
1221 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1223 connman_dbus_dict_append_basic(iter, "Peer",
1224 DBUS_TYPE_STRING, &peer);
1227 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1229 struct vpn_provider *provider = user_data;
1230 const char *address, *gateway, *peer;
1232 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1234 unsigned char prefixlen;
1236 connman_dbus_dict_append_basic(iter, "Address",
1237 DBUS_TYPE_STRING, &address);
1239 prefixlen = __vpn_ipconfig_get_prefixlen(
1240 provider->ipconfig_ipv6);
1242 connman_dbus_dict_append_basic(iter, "PrefixLength",
1243 DBUS_TYPE_BYTE, &prefixlen);
1246 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1248 connman_dbus_dict_append_basic(iter, "Gateway",
1249 DBUS_TYPE_STRING, &gateway);
1251 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1253 connman_dbus_dict_append_basic(iter, "Peer",
1254 DBUS_TYPE_STRING, &peer);
1257 static const char *state2string(enum vpn_provider_state state)
1260 case VPN_PROVIDER_STATE_UNKNOWN:
1262 case VPN_PROVIDER_STATE_IDLE:
1264 case VPN_PROVIDER_STATE_CONNECT:
1265 return "configuration";
1266 case VPN_PROVIDER_STATE_READY:
1268 case VPN_PROVIDER_STATE_DISCONNECT:
1269 return "disconnect";
1270 case VPN_PROVIDER_STATE_FAILURE:
1277 static void append_nameservers(DBusMessageIter *iter, char **servers)
1283 for (i = 0; servers[i]; i++) {
1284 DBG("servers[%d] %s", i, servers[i]);
1285 dbus_message_iter_append_basic(iter,
1286 DBUS_TYPE_STRING, &servers[i]);
1290 static void append_dns(DBusMessageIter *iter, void *user_data)
1292 struct vpn_provider *provider = user_data;
1294 if (provider->nameservers)
1295 append_nameservers(iter, provider->nameservers);
1298 static int provider_indicate_state(struct vpn_provider *provider,
1299 enum vpn_provider_state state)
1302 enum vpn_provider_state old_state;
1304 str = state2string(state);
1305 DBG("provider %p state %s/%d", provider, str, state);
1309 old_state = provider->state;
1310 provider->state = state;
1312 if (state == VPN_PROVIDER_STATE_READY) {
1313 connman_dbus_property_changed_basic(provider->path,
1314 VPN_CONNECTION_INTERFACE, "Index",
1315 DBUS_TYPE_INT32, &provider->index);
1317 if (provider->family == AF_INET)
1318 connman_dbus_property_changed_dict(provider->path,
1319 VPN_CONNECTION_INTERFACE, "IPv4",
1320 append_ipv4, provider);
1321 else if (provider->family == AF_INET6)
1322 connman_dbus_property_changed_dict(provider->path,
1323 VPN_CONNECTION_INTERFACE, "IPv6",
1324 append_ipv6, provider);
1326 connman_dbus_property_changed_array(provider->path,
1327 VPN_CONNECTION_INTERFACE,
1330 append_dns, provider);
1332 if (provider->domain)
1333 connman_dbus_property_changed_basic(provider->path,
1334 VPN_CONNECTION_INTERFACE,
1340 if (old_state != state)
1341 connman_dbus_property_changed_basic(provider->path,
1342 VPN_CONNECTION_INTERFACE, "State",
1343 DBUS_TYPE_STRING, &str);
1348 static void append_state(DBusMessageIter *iter,
1349 struct vpn_provider *provider)
1353 switch (provider->state) {
1354 case VPN_PROVIDER_STATE_UNKNOWN:
1355 case VPN_PROVIDER_STATE_IDLE:
1358 case VPN_PROVIDER_STATE_CONNECT:
1359 str = "configuration";
1361 case VPN_PROVIDER_STATE_READY:
1364 case VPN_PROVIDER_STATE_DISCONNECT:
1367 case VPN_PROVIDER_STATE_FAILURE:
1372 connman_dbus_dict_append_basic(iter, "State",
1373 DBUS_TYPE_STRING, &str);
1376 static void append_properties(DBusMessageIter *iter,
1377 struct vpn_provider *provider)
1379 DBusMessageIter dict;
1380 GHashTableIter hash;
1381 gpointer value, key;
1382 dbus_bool_t immutable;
1384 connman_dbus_dict_open(iter, &dict);
1386 append_state(&dict, provider);
1389 connman_dbus_dict_append_basic(&dict, "Type",
1390 DBUS_TYPE_STRING, &provider->type);
1393 connman_dbus_dict_append_basic(&dict, "Name",
1394 DBUS_TYPE_STRING, &provider->name);
1397 connman_dbus_dict_append_basic(&dict, "Host",
1398 DBUS_TYPE_STRING, &provider->host);
1399 if (provider->index >= 0)
1400 connman_dbus_dict_append_basic(&dict, "Index",
1401 DBUS_TYPE_INT32, &provider->index);
1402 if (provider->domain)
1403 connman_dbus_dict_append_basic(&dict, "Domain",
1404 DBUS_TYPE_STRING, &provider->domain);
1406 immutable = provider->immutable;
1407 connman_dbus_dict_append_basic(&dict, "Immutable", DBUS_TYPE_BOOLEAN,
1410 if (provider->family == AF_INET)
1411 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1413 else if (provider->family == AF_INET6)
1414 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1417 connman_dbus_dict_append_array(&dict, "Nameservers",
1418 DBUS_TYPE_STRING, append_dns, provider);
1420 connman_dbus_dict_append_array(&dict, "UserRoutes",
1421 DBUS_TYPE_DICT_ENTRY, append_routes,
1422 provider->user_routes);
1424 connman_dbus_dict_append_array(&dict, "ServerRoutes",
1425 DBUS_TYPE_DICT_ENTRY, append_routes,
1428 if (provider->setting_strings) {
1429 g_hash_table_iter_init(&hash, provider->setting_strings);
1431 while (g_hash_table_iter_next(&hash, &key, &value)) {
1432 struct vpn_setting *setting = value;
1434 if (!setting->hide_value &&
1436 connman_dbus_dict_append_basic(&dict, key,
1442 connman_dbus_dict_close(iter, &dict);
1445 static void connection_added_signal(struct vpn_provider *provider)
1447 DBusMessage *signal;
1448 DBusMessageIter iter;
1450 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1451 VPN_MANAGER_INTERFACE, "ConnectionAdded");
1455 dbus_message_iter_init_append(signal, &iter);
1456 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1458 append_properties(&iter, provider);
1460 dbus_connection_send(connection, signal, NULL);
1461 dbus_message_unref(signal);
1464 static bool check_host(char **hosts, char *host)
1471 for (i = 0; hosts[i]; i++) {
1472 if (g_strcmp0(hosts[i], host) == 0)
1479 static void provider_append_routes(gpointer key, gpointer value,
1482 struct vpn_route *route = value;
1483 struct vpn_provider *provider = user_data;
1484 int index = provider->index;
1490 * If the VPN administrator/user has given a route to
1491 * VPN server, then we must discard that because the
1492 * server cannot be contacted via VPN tunnel.
1494 if (check_host(provider->host_ip, route->network)) {
1495 DBG("Discarding VPN route to %s via %s at index %d",
1496 route->network, route->gateway, index);
1500 if (route->family == AF_INET6) {
1501 unsigned char prefix_len = atoi(route->netmask);
1503 connman_inet_add_ipv6_network_route(index, route->network,
1507 connman_inet_add_network_route(index, route->network,
1513 static int set_connected(struct vpn_provider *provider,
1516 struct vpn_ipconfig *ipconfig;
1518 DBG("provider %p id %s connected %d", provider,
1519 provider->identifier, connected);
1522 if (provider->family == AF_INET6)
1523 ipconfig = provider->ipconfig_ipv6;
1525 ipconfig = provider->ipconfig_ipv4;
1527 __vpn_ipconfig_address_add(ipconfig, provider->family);
1530 __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1532 provider_indicate_state(provider,
1533 VPN_PROVIDER_STATE_READY);
1535 g_hash_table_foreach(provider->routes, provider_append_routes,
1538 g_hash_table_foreach(provider->user_routes,
1539 provider_append_routes, provider);
1542 provider_indicate_state(provider,
1543 VPN_PROVIDER_STATE_DISCONNECT);
1545 provider_indicate_state(provider,
1546 VPN_PROVIDER_STATE_IDLE);
1552 int vpn_provider_set_state(struct vpn_provider *provider,
1553 enum vpn_provider_state state)
1559 case VPN_PROVIDER_STATE_UNKNOWN:
1561 case VPN_PROVIDER_STATE_IDLE:
1562 return set_connected(provider, false);
1563 case VPN_PROVIDER_STATE_CONNECT:
1564 return provider_indicate_state(provider, state);
1565 case VPN_PROVIDER_STATE_READY:
1566 return set_connected(provider, true);
1567 case VPN_PROVIDER_STATE_DISCONNECT:
1568 return provider_indicate_state(provider, state);
1569 case VPN_PROVIDER_STATE_FAILURE:
1570 return provider_indicate_state(provider, state);
1575 int vpn_provider_indicate_error(struct vpn_provider *provider,
1576 enum vpn_provider_error error)
1578 DBG("provider %p id %s error %d", provider, provider->identifier,
1581 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
1584 case VPN_PROVIDER_ERROR_UNKNOWN:
1585 case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1588 case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1589 case VPN_PROVIDER_ERROR_AUTH_FAILED:
1590 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
1594 if (provider->driver && provider->driver->set_state)
1595 provider->driver->set_state(provider, provider->state);
1600 static int connection_unregister(struct vpn_provider *provider)
1602 DBG("provider %p path %s", provider, provider->path);
1604 if (!provider->path)
1607 g_dbus_unregister_interface(connection, provider->path,
1608 VPN_CONNECTION_INTERFACE);
1610 g_free(provider->path);
1611 provider->path = NULL;
1616 static int connection_register(struct vpn_provider *provider)
1618 DBG("provider %p path %s", provider, provider->path);
1623 provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1624 provider->identifier);
1626 g_dbus_register_interface(connection, provider->path,
1627 VPN_CONNECTION_INTERFACE,
1628 connection_methods, connection_signals,
1629 NULL, provider, NULL);
1634 static void unregister_provider(gpointer data)
1636 struct vpn_provider *provider = data;
1638 configuration_count_del();
1640 connection_unregister(provider);
1642 /* If the provider has any DNS resolver queries pending,
1643 * they need to be cleared here because the unref will not
1644 * be able to do that (because the provider_resolv_host_addr()
1645 * has increased the ref count by 1). This is quite rare as
1646 * normally the resolving either returns a value or has a
1647 * timeout which clears the memory. Typically resolv_result() will
1648 * unref the provider but in this case that call has not yet
1651 if (provider->resolv)
1652 vpn_provider_unref(provider);
1654 vpn_provider_unref(provider);
1657 static void provider_initialize(struct vpn_provider *provider)
1659 DBG("provider %p", provider);
1661 provider->index = 0;
1663 provider->name = NULL;
1664 provider->type = NULL;
1665 provider->domain = NULL;
1666 provider->identifier = NULL;
1667 provider->immutable = false;
1668 provider->user_networks = NULL;
1669 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1671 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1672 g_free, free_route);
1673 provider->setting_strings = g_hash_table_new_full(g_str_hash,
1674 g_str_equal, g_free, free_setting);
1677 static struct vpn_provider *vpn_provider_new(void)
1679 struct vpn_provider *provider;
1681 provider = g_try_new0(struct vpn_provider, 1);
1685 provider->refcount = 1;
1687 DBG("provider %p", provider);
1688 provider_initialize(provider);
1693 static struct vpn_provider *vpn_provider_get(const char *identifier)
1695 struct vpn_provider *provider;
1697 provider = g_hash_table_lookup(provider_hash, identifier);
1701 provider = vpn_provider_new();
1705 DBG("provider %p", provider);
1707 provider->identifier = g_strdup(identifier);
1709 g_hash_table_insert(provider_hash, provider->identifier, provider);
1711 configuration_count_add();
1716 static void provider_dbus_ident(char *ident)
1718 int i, len = strlen(ident);
1720 for (i = 0; i < len; i++) {
1721 if (ident[i] >= '0' && ident[i] <= '9')
1723 if (ident[i] >= 'a' && ident[i] <= 'z')
1725 if (ident[i] >= 'A' && ident[i] <= 'Z')
1731 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1734 struct vpn_provider *provider;
1736 if (!keyfile || !ident)
1739 provider = __vpn_provider_lookup(ident);
1741 provider = vpn_provider_get(ident);
1743 DBG("can not create provider");
1747 provider_load_from_keyfile(provider, keyfile);
1749 if (!provider->name || !provider->host ||
1750 !provider->domain) {
1751 DBG("cannot get name, host or domain");
1752 vpn_provider_unref(provider);
1756 if (provider_register(provider) == 0)
1757 connection_register(provider);
1762 static void provider_create_all_from_type(const char *provider_type)
1769 DBG("provider type %s", provider_type);
1771 providers = __connman_storage_get_providers();
1776 for (i = 0; providers[i]; i += 1) {
1778 if (strncmp(providers[i], "provider_", 9) != 0)
1781 id = providers[i] + 9;
1782 keyfile = __connman_storage_load_provider(id);
1787 type = __vpn_config_get_string(keyfile, id, "Type", NULL);
1789 DBG("keyfile %p id %s type %s", keyfile, id, type);
1791 if (strcmp(provider_type, type) != 0) {
1793 g_key_file_free(keyfile);
1797 if (!provider_create_from_keyfile(keyfile, id))
1798 DBG("could not create provider");
1801 g_key_file_free(keyfile);
1803 g_strfreev(providers);
1806 #if !defined TIZEN_EXT
1807 char *__vpn_provider_create_identifier(const char *host, const char *domain)
1811 ident = g_strdup_printf("%s_%s", host, domain);
1815 provider_dbus_ident(ident);
1820 char *__vpn_provider_create_identifier(const char *host, const char *domain, const char *name)
1824 ident = g_strdup_printf("%s_%s_%s", host, domain, name);
1828 provider_dbus_ident(ident);
1834 int __vpn_provider_create(DBusMessage *msg)
1836 struct vpn_provider *provider;
1837 DBusMessageIter iter, array;
1838 const char *type = NULL, *name = NULL;
1839 const char *host = NULL, *domain = NULL;
1840 GSList *networks = NULL;
1844 dbus_message_iter_init(msg, &iter);
1845 dbus_message_iter_recurse(&iter, &array);
1847 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1848 DBusMessageIter entry, value;
1851 dbus_message_iter_recurse(&array, &entry);
1852 dbus_message_iter_get_basic(&entry, &key);
1854 dbus_message_iter_next(&entry);
1855 dbus_message_iter_recurse(&entry, &value);
1857 switch (dbus_message_iter_get_arg_type(&value)) {
1858 case DBUS_TYPE_STRING:
1859 if (g_str_equal(key, "Type"))
1860 dbus_message_iter_get_basic(&value, &type);
1861 else if (g_str_equal(key, "Name"))
1862 dbus_message_iter_get_basic(&value, &name);
1863 else if (g_str_equal(key, "Host"))
1864 dbus_message_iter_get_basic(&value, &host);
1865 else if (g_str_equal(key, "VPN.Domain") ||
1866 g_str_equal(key, "Domain"))
1867 dbus_message_iter_get_basic(&value, &domain);
1869 case DBUS_TYPE_ARRAY:
1870 if (g_str_equal(key, "UserRoutes"))
1871 networks = get_user_networks(&value);
1875 dbus_message_iter_next(&array);
1878 if (!host || !domain)
1881 DBG("Type %s name %s networks %p", type, name, networks);
1886 #if !defined TIZEN_EXT
1887 ident = __vpn_provider_create_identifier(host, domain);
1889 ident = __vpn_provider_create_identifier(host, domain, name);
1891 DBG("ident %s", ident);
1893 provider = __vpn_provider_lookup(ident);
1895 provider = vpn_provider_get(ident);
1897 DBG("can not create provider");
1902 provider->host = g_strdup(host);
1903 provider->domain = g_strdup(domain);
1904 provider->name = g_strdup(name);
1905 provider->type = g_strdup(type);
1907 if (provider_register(provider) == 0)
1908 vpn_provider_load(provider);
1910 provider_resolv_host_addr(provider);
1914 g_slist_free_full(provider->user_networks, free_route);
1915 provider->user_networks = networks;
1916 set_user_networks(provider, provider->user_networks);
1919 dbus_message_iter_init(msg, &iter);
1920 dbus_message_iter_recurse(&iter, &array);
1922 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1923 DBusMessageIter entry, value;
1924 const char *key, *str;
1926 dbus_message_iter_recurse(&array, &entry);
1927 dbus_message_iter_get_basic(&entry, &key);
1929 dbus_message_iter_next(&entry);
1930 dbus_message_iter_recurse(&entry, &value);
1932 switch (dbus_message_iter_get_arg_type(&value)) {
1933 case DBUS_TYPE_STRING:
1934 dbus_message_iter_get_basic(&value, &str);
1935 vpn_provider_set_string(provider, key, str);
1939 dbus_message_iter_next(&array);
1944 vpn_provider_save(provider);
1946 err = provider_register(provider);
1947 if (err != 0 && err != -EALREADY)
1950 connection_register(provider);
1952 DBG("provider %p index %d path %s", provider, provider->index,
1955 g_dbus_send_reply(connection, msg,
1956 DBUS_TYPE_OBJECT_PATH, &provider->path,
1959 connection_added_signal(provider);
1964 static const char *get_string(GHashTable *settings, const char *key)
1966 DBG("settings %p key %s", settings, key);
1968 return g_hash_table_lookup(settings, key);
1971 static GSList *parse_user_networks(const char *network_str)
1973 GSList *networks = NULL;
1980 elems = g_strsplit(network_str, ",", 0);
1985 struct vpn_route *vpn_route;
1986 char *network, *netmask, *gateway;
1990 route = g_strsplit(elems[i], "/", 0);
1995 if (!network || network[0] == '\0')
1998 family = connman_inet_check_ipaddress(network);
2000 DBG("Cannot get address family of %s (%d/%s)", network,
2001 family, gai_strerror(family));
2012 DBG("Unsupported address family %d", family);
2017 if (!netmask || netmask[0] == '\0')
2022 vpn_route = g_try_new0(struct vpn_route, 1);
2028 vpn_route->family = family;
2029 vpn_route->network = g_strdup(network);
2030 vpn_route->netmask = g_strdup(netmask);
2031 vpn_route->gateway = g_strdup(gateway);
2033 DBG("route %s/%s%s%s", network, netmask,
2034 gateway ? " via " : "", gateway ? gateway : "");
2036 networks = g_slist_prepend(networks, vpn_route);
2045 return g_slist_reverse(networks);
2048 int __vpn_provider_create_from_config(GHashTable *settings,
2049 const char *config_ident,
2050 const char *config_entry)
2052 struct vpn_provider *provider;
2053 const char *type, *name, *host, *domain, *networks_str;
2056 GHashTableIter hash;
2057 gpointer value, key;
2060 type = get_string(settings, "Type");
2061 name = get_string(settings, "Name");
2062 host = get_string(settings, "Host");
2063 domain = get_string(settings, "Domain");
2064 networks_str = get_string(settings, "Networks");
2065 networks = parse_user_networks(networks_str);
2067 if (!host || !domain) {
2072 DBG("type %s name %s networks %s", type, name, networks_str);
2074 if (!type || !name) {
2079 #if !defined TIZEN_EXT
2080 ident = __vpn_provider_create_identifier(host, domain);
2082 ident = __vpn_provider_create_identifier(host, domain, name);
2084 DBG("ident %s", ident);
2086 provider = __vpn_provider_lookup(ident);
2088 provider = vpn_provider_get(ident);
2090 DBG("can not create provider");
2095 provider->host = g_strdup(host);
2096 provider->domain = g_strdup(domain);
2097 provider->name = g_strdup(name);
2098 provider->type = g_ascii_strdown(type, -1);
2100 provider->config_file = g_strdup(config_ident);
2101 provider->config_entry = g_strdup(config_entry);
2103 provider_register(provider);
2105 provider_resolv_host_addr(provider);
2109 g_slist_free_full(provider->user_networks, free_route);
2110 provider->user_networks = networks;
2111 set_user_networks(provider, provider->user_networks);
2114 g_hash_table_iter_init(&hash, settings);
2116 while (g_hash_table_iter_next(&hash, &key, &value))
2117 __vpn_provider_set_string_immutable(provider, key, value);
2119 provider->immutable = true;
2121 vpn_provider_save(provider);
2123 err = provider_register(provider);
2124 if (err != 0 && err != -EALREADY)
2127 connection_register(provider);
2129 DBG("provider %p index %d path %s", provider, provider->index,
2132 connection_added_signal(provider);
2140 g_slist_free_full(networks, free_route);
2145 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
2147 DBusMessageIter entry;
2148 GHashTableIter hash;
2149 gpointer value, key;
2151 g_hash_table_iter_init(&hash, provider_hash);
2153 while (g_hash_table_iter_next(&hash, &key, &value)) {
2154 struct vpn_provider *provider = value;
2156 DBG("path %s", provider->path);
2158 if (!provider->identifier)
2161 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
2163 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2165 append_properties(&entry, provider);
2166 dbus_message_iter_close_container(iter, &entry);
2170 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
2176 reply = dbus_message_new_method_return(msg);
2180 __connman_dbus_append_objpath_dict_array(reply,
2181 append_connection_structs, NULL);
2186 const char *__vpn_provider_get_ident(struct vpn_provider *provider)
2191 return provider->identifier;
2194 static int set_string(struct vpn_provider *provider,
2195 const char *key, const char *value,
2196 bool hide_value, bool immutable)
2198 DBG("provider %p key %s immutable %s value %s", provider, key,
2199 immutable ? "yes" : "no",
2200 hide_value ? "<not printed>" : value);
2202 if (g_str_equal(key, "Type")) {
2203 g_free(provider->type);
2204 provider->type = g_ascii_strdown(value, -1);
2205 send_value(provider->path, "Type", provider->type);
2206 } else if (g_str_equal(key, "Name")) {
2207 g_free(provider->name);
2208 provider->name = g_strdup(value);
2209 send_value(provider->path, "Name", provider->name);
2210 } else if (g_str_equal(key, "Host")) {
2211 g_free(provider->host);
2212 provider->host = g_strdup(value);
2213 send_value(provider->path, "Host", provider->host);
2214 } else if (g_str_equal(key, "VPN.Domain") ||
2215 g_str_equal(key, "Domain")) {
2216 g_free(provider->domain);
2217 provider->domain = g_strdup(value);
2218 send_value(provider->path, "Domain", provider->domain);
2220 struct vpn_setting *setting;
2222 setting = g_hash_table_lookup(provider->setting_strings, key);
2223 if (setting && !immutable &&
2224 setting->immutable) {
2225 DBG("Trying to set immutable variable %s", key);
2229 setting = g_try_new0(struct vpn_setting, 1);
2233 setting->value = g_strdup(value);
2234 setting->hide_value = hide_value;
2237 setting->immutable = true;
2240 send_value(provider->path, key, setting->value);
2242 g_hash_table_replace(provider->setting_strings,
2243 g_strdup(key), setting);
2249 int vpn_provider_set_string(struct vpn_provider *provider,
2250 const char *key, const char *value)
2252 return set_string(provider, key, value, false, false);
2255 int vpn_provider_set_string_hide_value(struct vpn_provider *provider,
2256 const char *key, const char *value)
2258 return set_string(provider, key, value, true, false);
2261 int __vpn_provider_set_string_immutable(struct vpn_provider *provider,
2262 const char *key, const char *value)
2264 return set_string(provider, key, value, false, true);
2267 const char *vpn_provider_get_string(struct vpn_provider *provider,
2270 struct vpn_setting *setting;
2272 DBG("provider %p key %s", provider, key);
2274 if (g_str_equal(key, "Type"))
2275 return provider->type;
2276 else if (g_str_equal(key, "Name"))
2277 return provider->name;
2278 else if (g_str_equal(key, "Host"))
2279 return provider->host;
2280 else if (g_str_equal(key, "HostIP")) {
2281 if (!provider->host_ip ||
2282 !provider->host_ip[0])
2283 return provider->host;
2285 return provider->host_ip[0];
2286 } else if (g_str_equal(key, "VPN.Domain") ||
2287 g_str_equal(key, "Domain"))
2288 return provider->domain;
2290 setting = g_hash_table_lookup(provider->setting_strings, key);
2294 return setting->value;
2297 bool __vpn_provider_check_routes(struct vpn_provider *provider)
2302 if (provider->user_routes &&
2303 g_hash_table_size(provider->user_routes) > 0)
2306 if (provider->routes &&
2307 g_hash_table_size(provider->routes) > 0)
2313 void *vpn_provider_get_data(struct vpn_provider *provider)
2315 return provider->driver_data;
2318 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
2320 provider->driver_data = data;
2323 void vpn_provider_set_index(struct vpn_provider *provider, int index)
2325 DBG("index %d provider %p", index, provider);
2327 if (!provider->ipconfig_ipv4) {
2328 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
2330 if (!provider->ipconfig_ipv4) {
2331 DBG("Couldnt create ipconfig for IPv4");
2336 __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
2338 if (!provider->ipconfig_ipv6) {
2339 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
2341 if (!provider->ipconfig_ipv6) {
2342 DBG("Couldnt create ipconfig for IPv6");
2347 __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
2350 provider->index = index;
2353 int vpn_provider_get_index(struct vpn_provider *provider)
2355 return provider->index;
2358 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
2359 struct connman_ipaddress *ipaddress)
2361 struct vpn_ipconfig *ipconfig = NULL;
2363 switch (ipaddress->family) {
2365 ipconfig = provider->ipconfig_ipv4;
2368 ipconfig = provider->ipconfig_ipv6;
2374 DBG("provider %p state %d ipconfig %p family %d", provider,
2375 provider->state, ipconfig, ipaddress->family);
2380 provider->family = ipaddress->family;
2382 if (provider->state == VPN_PROVIDER_STATE_CONNECT ||
2383 provider->state == VPN_PROVIDER_STATE_READY) {
2384 struct connman_ipaddress *addr =
2385 __vpn_ipconfig_get_address(ipconfig);
2388 * Remember the old address so that we can remove it in notify
2389 * function in plugins/vpn.c if we ever restart
2391 if (ipaddress->family == AF_INET6) {
2392 connman_ipaddress_free(provider->prev_ipv6_addr);
2393 provider->prev_ipv6_addr =
2394 connman_ipaddress_copy(addr);
2396 connman_ipaddress_free(provider->prev_ipv4_addr);
2397 provider->prev_ipv4_addr =
2398 connman_ipaddress_copy(addr);
2402 if (ipaddress->local) {
2403 __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
2404 __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
2405 __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
2406 __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
2407 __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
2413 int vpn_provider_set_pac(struct vpn_provider *provider,
2416 DBG("provider %p pac %s", provider, pac);
2422 int vpn_provider_set_domain(struct vpn_provider *provider,
2425 DBG("provider %p domain %s", provider, domain);
2427 g_free(provider->domain);
2428 provider->domain = g_strdup(domain);
2433 int vpn_provider_set_nameservers(struct vpn_provider *provider,
2434 const char *nameservers)
2436 DBG("provider %p nameservers %s", provider, nameservers);
2438 g_strfreev(provider->nameservers);
2439 provider->nameservers = NULL;
2444 provider->nameservers = g_strsplit(nameservers, " ", 0);
2449 enum provider_route_type {
2450 PROVIDER_ROUTE_TYPE_NONE = 0,
2451 PROVIDER_ROUTE_TYPE_MASK = 1,
2452 PROVIDER_ROUTE_TYPE_ADDR = 2,
2453 PROVIDER_ROUTE_TYPE_GW = 3,
2456 static int route_env_parse(struct vpn_provider *provider, const char *key,
2457 int *family, unsigned long *idx,
2458 enum provider_route_type *type)
2463 DBG("name %s", provider->name);
2465 if (!strcmp(provider->type, "openvpn")) {
2466 if (g_str_has_prefix(key, "route_network_")) {
2467 start = key + strlen("route_network_");
2468 *type = PROVIDER_ROUTE_TYPE_ADDR;
2469 } else if (g_str_has_prefix(key, "route_netmask_")) {
2470 start = key + strlen("route_netmask_");
2471 *type = PROVIDER_ROUTE_TYPE_MASK;
2472 } else if (g_str_has_prefix(key, "route_gateway_")) {
2473 start = key + strlen("route_gateway_");
2474 *type = PROVIDER_ROUTE_TYPE_GW;
2479 *idx = g_ascii_strtoull(start, &end, 10);
2481 } else if (!strcmp(provider->type, "openconnect")) {
2482 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_")) {
2484 start = key + strlen("CISCO_SPLIT_INC_");
2485 } else if (g_str_has_prefix(key,
2486 "CISCO_IPV6_SPLIT_INC_")) {
2488 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
2492 *idx = g_ascii_strtoull(start, &end, 10);
2494 if (strncmp(end, "_ADDR", 5) == 0)
2495 *type = PROVIDER_ROUTE_TYPE_ADDR;
2496 else if (strncmp(end, "_MASK", 5) == 0)
2497 *type = PROVIDER_ROUTE_TYPE_MASK;
2498 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2499 *family == AF_INET6) {
2500 *type = PROVIDER_ROUTE_TYPE_MASK;
2508 int vpn_provider_append_route(struct vpn_provider *provider,
2509 const char *key, const char *value)
2511 struct vpn_route *route;
2512 int ret, family = 0;
2513 unsigned long idx = 0;
2514 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2516 DBG("key %s value %s", key, value);
2518 ret = route_env_parse(provider, key, &family, &idx, &type);
2522 DBG("idx %lu family %d type %d", idx, family, type);
2524 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2526 route = g_try_new0(struct vpn_route, 1);
2528 connman_error("out of memory");
2532 route->family = family;
2534 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2539 case PROVIDER_ROUTE_TYPE_NONE:
2541 case PROVIDER_ROUTE_TYPE_MASK:
2542 route->netmask = g_strdup(value);
2544 case PROVIDER_ROUTE_TYPE_ADDR:
2545 route->network = g_strdup(value);
2547 case PROVIDER_ROUTE_TYPE_GW:
2548 route->gateway = g_strdup(value);
2552 if (!handle_routes) {
2553 if (route->netmask && route->gateway &&
2555 provider_schedule_changed(provider);
2561 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2563 if (!provider->driver)
2566 return provider->driver->name;
2569 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2571 return provider->identifier;
2574 static gint compare_priority(gconstpointer a, gconstpointer b)
2579 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2581 struct vpn_provider *provider = value;
2583 if (provider->driver && provider->driver->remove)
2584 provider->driver->remove(provider);
2586 connection_unregister(provider);
2589 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2591 DBG("driver %p name %s", driver, driver->name);
2593 driver_list = g_slist_insert_sorted(driver_list, driver,
2595 provider_create_all_from_type(driver->name);
2599 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2601 GHashTableIter iter;
2602 gpointer value, key;
2604 DBG("driver %p name %s", driver, driver->name);
2606 driver_list = g_slist_remove(driver_list, driver);
2608 g_hash_table_iter_init(&iter, provider_hash);
2609 while (g_hash_table_iter_next(&iter, &key, &value)) {
2610 struct vpn_provider *provider = value;
2612 if (provider && provider->driver &&
2613 provider->driver->type == driver->type &&
2614 g_strcmp0(provider->driver->name,
2615 driver->name) == 0) {
2616 provider->driver = NULL;
2621 const char *vpn_provider_get_name(struct vpn_provider *provider)
2623 return provider->name;
2626 const char *vpn_provider_get_host(struct vpn_provider *provider)
2628 return provider->host;
2631 const char *vpn_provider_get_path(struct vpn_provider *provider)
2633 return provider->path;
2636 void vpn_provider_change_address(struct vpn_provider *provider)
2638 switch (provider->family) {
2640 connman_inet_set_address(provider->index,
2641 __vpn_ipconfig_get_address(provider->ipconfig_ipv4));
2644 connman_inet_set_ipv6_address(provider->index,
2645 __vpn_ipconfig_get_address(provider->ipconfig_ipv6));
2652 void vpn_provider_clear_address(struct vpn_provider *provider, int family)
2654 const char *address;
2657 DBG("provider %p family %d ipv4 %p ipv6 %p", provider, family,
2658 provider->prev_ipv4_addr, provider->prev_ipv6_addr);
2662 if (provider->prev_ipv4_addr) {
2663 connman_ipaddress_get_ip(provider->prev_ipv4_addr,
2666 DBG("ipv4 %s/%d", address, len);
2668 connman_inet_clear_address(provider->index,
2669 provider->prev_ipv4_addr);
2670 connman_ipaddress_free(provider->prev_ipv4_addr);
2671 provider->prev_ipv4_addr = NULL;
2675 if (provider->prev_ipv6_addr) {
2676 connman_ipaddress_get_ip(provider->prev_ipv6_addr,
2679 DBG("ipv6 %s/%d", address, len);
2681 connman_inet_clear_ipv6_address(provider->index,
2684 connman_ipaddress_free(provider->prev_ipv6_addr);
2685 provider->prev_ipv6_addr = NULL;
2693 static int agent_probe(struct connman_agent *agent)
2695 DBG("agent %p", agent);
2699 static void agent_remove(struct connman_agent *agent)
2701 DBG("agent %p", agent);
2704 static struct connman_agent_driver agent_driver = {
2706 .interface = VPN_AGENT_INTERFACE,
2707 .probe = agent_probe,
2708 .remove = agent_remove,
2711 static void remove_unprovisioned_providers(void)
2714 GKeyFile *keyfile, *configkeyfile;
2715 char *file, *section;
2718 providers = __connman_storage_get_providers();
2722 for (; providers[i]; i++) {
2723 char *group = providers[i] + sizeof("provider_") - 1;
2724 file = section = NULL;
2725 keyfile = configkeyfile = NULL;
2727 keyfile = __connman_storage_load_provider(group);
2731 file = __vpn_config_get_string(keyfile, group,
2732 "Config.file", NULL);
2736 section = __vpn_config_get_string(keyfile, group,
2737 "Config.ident", NULL);
2741 configkeyfile = __connman_storage_load_provider_config(file);
2742 if (!configkeyfile) {
2744 * Config file is missing, remove the provisioned
2747 __connman_storage_remove_provider(group);
2751 if (!g_key_file_has_group(configkeyfile, section))
2753 * Config section is missing, remove the provisioned
2756 __connman_storage_remove_provider(group);
2760 g_key_file_free(keyfile);
2763 g_key_file_free(configkeyfile);
2769 g_strfreev(providers);
2772 int __vpn_provider_init(bool do_routes)
2778 handle_routes = do_routes;
2780 err = connman_agent_driver_register(&agent_driver);
2782 connman_error("Cannot register agent driver for %s",
2787 connection = connman_dbus_get_connection();
2789 remove_unprovisioned_providers();
2791 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2792 NULL, unregister_provider);
2796 void __vpn_provider_cleanup(void)
2800 connman_agent_driver_unregister(&agent_driver);
2802 g_hash_table_foreach(provider_hash, clean_provider, NULL);
2804 g_hash_table_destroy(provider_hash);
2805 provider_hash = NULL;
2807 dbus_connection_unref(connection);