5 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
6 * Copyright (C) 2019-2021 Jolla Ltd. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 * One hour difference in seconds between connections for checking whether to
29 * treat the authentication error as a real error or as a result of rapid
32 #define CONNECT_OK_DIFF ((time_t)60*60)
33 #define AUTH_ERROR_LIMIT_DEFAULT 1
35 #define STATE_INTERVAL_DEFAULT 0
37 #define CONNMAN_STATE_ONLINE "online"
38 #define CONNMAN_STATE_OFFLINE "offline"
39 #define CONNMAN_STATE_READY "ready"
40 #define CONNMAN_STATE_IDLE "idle"
47 #include <connman/log.h>
48 #include <gweb/gresolv.h>
52 #include "../src/connman.h"
53 #include "connman/agent.h"
54 #include "connman/vpn-dbus.h"
55 #include "vpn-provider.h"
57 #include "plugins/vpn.h"
59 static DBusConnection *connection;
60 static GHashTable *provider_hash;
61 static GSList *driver_list;
62 static int configuration_count;
81 enum vpn_provider_state state;
89 bool do_split_routing;
91 struct vpn_provider_driver *driver;
93 GHashTable *setting_strings;
94 GHashTable *user_routes;
95 GSList *user_networks;
98 struct vpn_ipconfig *ipconfig_ipv4;
99 struct vpn_ipconfig *ipconfig_ipv6;
105 struct connman_ipaddress *prev_ipv4_addr;
106 struct connman_ipaddress *prev_ipv6_addr;
108 unsigned int do_connect_timeout;
109 unsigned int auth_error_counter;
110 unsigned int conn_error_counter;
111 unsigned int signal_watch;
112 unsigned int auth_error_limit;
113 time_t previous_connect_time;
116 struct vpn_provider_connect_data {
117 DBusConnection *conn;
119 struct vpn_provider *provider;
122 static unsigned int get_connman_state_timeout;
124 static guint connman_signal_watch;
125 static guint connman_service_watch;
127 static bool connman_online;
128 static bool state_query_completed;
130 static void append_properties(DBusMessageIter *iter,
131 struct vpn_provider *provider);
132 static int vpn_provider_save(struct vpn_provider *provider);
134 static void get_connman_state(void);
136 static void set_state(const char *new_state)
138 if (!new_state || !*new_state)
141 DBG("old state %s new state %s",
143 CONNMAN_STATE_ONLINE "/" CONNMAN_STATE_READY :
144 CONNMAN_STATE_OFFLINE "/" CONNMAN_STATE_IDLE,
147 /* States "online" and "ready" mean connman is online */
148 if (!g_ascii_strcasecmp(new_state, CONNMAN_STATE_ONLINE) ||
149 !g_ascii_strcasecmp(new_state, CONNMAN_STATE_READY))
150 connman_online = true;
151 /* Everything else means connman is offline */
153 connman_online = false;
155 DBG("set state %s connman_online=%s ", new_state,
156 connman_online ? "true" : "false");
159 static void free_route(gpointer data)
161 struct vpn_route *route = data;
163 g_free(route->network);
164 g_free(route->netmask);
165 g_free(route->gateway);
170 static void free_setting(gpointer data)
172 struct vpn_setting *setting = data;
174 g_free(setting->value);
178 static void append_route(DBusMessageIter *iter, void *user_data)
180 struct vpn_route *route = user_data;
181 DBusMessageIter item;
184 connman_dbus_dict_open(iter, &item);
189 if (route->family == AF_INET)
191 else if (route->family == AF_INET6)
195 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
196 DBUS_TYPE_INT32, &family);
199 connman_dbus_dict_append_basic(&item, "Network",
200 DBUS_TYPE_STRING, &route->network);
203 connman_dbus_dict_append_basic(&item, "Netmask",
204 DBUS_TYPE_STRING, &route->netmask);
207 connman_dbus_dict_append_basic(&item, "Gateway",
208 DBUS_TYPE_STRING, &route->gateway);
211 connman_dbus_dict_close(iter, &item);
214 static void append_routes(DBusMessageIter *iter, void *user_data)
216 GHashTable *routes = user_data;
221 append_route(iter, NULL);
225 g_hash_table_iter_init(&hash, routes);
227 while (g_hash_table_iter_next(&hash, &key, &value)) {
228 DBusMessageIter dict;
230 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
232 append_route(&dict, value);
233 dbus_message_iter_close_container(iter, &dict);
237 static void send_routes(struct vpn_provider *provider, GHashTable *routes,
240 connman_dbus_property_changed_array(provider->path,
241 VPN_CONNECTION_INTERFACE,
243 DBUS_TYPE_DICT_ENTRY,
248 static int provider_routes_changed(struct vpn_provider *provider)
250 DBG("provider %p", provider);
252 send_routes(provider, provider->routes, "ServerRoutes");
258 * Sort vpn_route struct based on (similarly to the route key in hash table):
259 * 1) IP protocol number
260 * 2) Network addresses
261 * 3) Netmask addresses
262 * 4) Gateway addresses
264 static gint compare_route(gconstpointer a, gconstpointer b)
266 const struct vpn_route *route_a = a;
267 const struct vpn_route *route_b = b;
270 /* If IP families differ, prefer IPv6 over IPv4 */
271 if (route_a->family != route_b->family) {
272 if (route_a->family < route_b->family)
275 if (route_a->family > route_b->family)
279 /* If networks differ, return */
280 if ((difference = g_strcmp0(route_a->network, route_b->network)))
283 /* If netmasks differ, return. */
284 if ((difference = g_strcmp0(route_a->netmask, route_b->netmask)))
287 return g_strcmp0(route_a->gateway, route_b->gateway);
290 static GSList *read_route_dict(GSList *routes, DBusMessageIter *dicts)
292 DBusMessageIter dict, value, entry;
293 const char *network, *netmask, *gateway;
294 struct vpn_route *route;
298 dbus_message_iter_recurse(dicts, &entry);
300 network = netmask = gateway = NULL;
303 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_DICT_ENTRY) {
305 dbus_message_iter_recurse(&entry, &dict);
306 dbus_message_iter_get_basic(&dict, &key);
308 dbus_message_iter_next(&dict);
309 dbus_message_iter_recurse(&dict, &value);
311 type = dbus_message_iter_get_arg_type(&value);
314 case DBUS_TYPE_INT32:
315 if (g_str_equal(key, "ProtocolFamily"))
316 dbus_message_iter_get_basic(&value, &family);
319 case DBUS_TYPE_STRING:
320 if (g_str_equal(key, "Network"))
321 dbus_message_iter_get_basic(&value, &network);
322 else if (g_str_equal(key, "Netmask"))
323 dbus_message_iter_get_basic(&value, &netmask);
324 else if (g_str_equal(key, "Gateway"))
325 dbus_message_iter_get_basic(&value, &gateway);
329 dbus_message_iter_next(&entry);
332 DBG("family %d network %s netmask %s gateway %s", family,
333 network, netmask, gateway);
335 if (!network || !netmask) {
336 DBG("Ignoring route as network/netmask is missing");
340 route = g_try_new(struct vpn_route, 1);
342 g_slist_free_full(routes, free_route);
346 if (family == PF_UNSPEC) {
347 family = connman_inet_check_ipaddress(network);
349 DBG("Cannot get address family of %s (%d/%s)", network,
350 family, gai_strerror(family));
371 route->family = family;
372 route->network = g_strdup(network);
373 route->netmask = g_strdup(netmask);
374 route->gateway = g_strdup(gateway);
376 routes = g_slist_insert_sorted(routes, route, compare_route);
380 static GSList *get_user_networks(DBusMessageIter *array)
382 DBusMessageIter entry;
385 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
387 dbus_message_iter_recurse(array, &entry);
389 while (dbus_message_iter_get_arg_type(&entry) ==
391 DBusMessageIter dicts;
393 dbus_message_iter_recurse(&entry, &dicts);
395 while (dbus_message_iter_get_arg_type(&dicts) ==
398 list = read_route_dict(list, &dicts);
399 dbus_message_iter_next(&dicts);
402 dbus_message_iter_next(&entry);
405 dbus_message_iter_next(array);
411 static void set_user_networks(struct vpn_provider *provider, GSList *networks)
415 for (list = networks; list; list = g_slist_next(list)) {
416 struct vpn_route *route = list->data;
418 if (__vpn_provider_append_user_route(provider,
419 route->family, route->network,
420 route->netmask, route->gateway) != 0)
425 static void del_routes(struct vpn_provider *provider)
429 g_hash_table_iter_init(&hash, provider->user_routes);
430 g_hash_table_remove_all(provider->user_routes);
431 g_slist_free_full(provider->user_networks, free_route);
432 provider->user_networks = NULL;
435 static void send_value(const char *path, const char *key, const char *value)
437 const char *empty = "";
445 connman_dbus_property_changed_basic(path,
446 VPN_CONNECTION_INTERFACE,
452 static void send_value_boolean(const char *path, const char *key,
455 connman_dbus_property_changed_basic(path,
456 VPN_CONNECTION_INTERFACE,
462 static gboolean provider_send_changed(gpointer data)
464 struct vpn_provider *provider = data;
466 provider_routes_changed(provider);
468 provider->notify_id = 0;
473 static void provider_schedule_changed(struct vpn_provider *provider)
475 if (provider->notify_id != 0)
476 g_source_remove(provider->notify_id);
478 provider->notify_id = g_timeout_add(100, provider_send_changed,
482 static DBusMessage *get_properties(DBusConnection *conn,
483 DBusMessage *msg, void *data)
485 struct vpn_provider *provider = data;
487 DBusMessageIter array;
489 DBG("provider %p", provider);
491 reply = dbus_message_new_method_return(msg);
495 dbus_message_iter_init_append(reply, &array);
497 append_properties(&array, provider);
502 /* True when lists are equal, false otherwise */
503 static bool compare_network_lists(GSList *a, GSList *b)
505 struct vpn_route *route_a, *route_b;
506 GSList *iter_a, *iter_b;
512 * If either of lists is NULL or the lists are of different size, the
513 * lists are not equal.
515 if ((!a || !b) || (g_slist_length(a) != g_slist_length(b)))
518 /* Routes are in sorted list so items can be compared in order. */
519 for (iter_a = a, iter_b = b; iter_a && iter_b;
520 iter_a = iter_a->next, iter_b = iter_b->next) {
522 route_a = iter_a->data;
523 route_b = iter_b->data;
525 if (compare_route(route_a, route_b))
532 static const char *bool2str(bool value)
534 return value ? "true" : "false";
537 static int set_provider_property(struct vpn_provider *provider,
538 const char *name, DBusMessageIter *value, int type)
542 DBG("provider %p", provider);
544 if (!provider || !name || !value)
547 if (g_str_equal(name, "UserRoutes")) {
550 if (type != DBUS_TYPE_ARRAY)
553 networks = get_user_networks(value);
555 if (compare_network_lists(provider->user_networks, networks)) {
556 g_slist_free_full(networks, free_route);
560 del_routes(provider);
561 provider->user_networks = networks;
562 set_user_networks(provider, provider->user_networks);
563 send_routes(provider, provider->user_routes, "UserRoutes");
564 } else if (g_str_equal(name, "SplitRouting")) {
565 dbus_bool_t split_routing;
567 if (type != DBUS_TYPE_BOOLEAN)
570 dbus_message_iter_get_basic(value, &split_routing);
572 DBG("property %s value %s ", name, bool2str(split_routing));
573 vpn_provider_set_boolean(provider, name, split_routing, false);
577 if (type != DBUS_TYPE_STRING)
580 dbus_message_iter_get_basic(value, &str);
582 DBG("property %s value %s", name, str);
584 /* Empty string clears the value, similar to ClearProperty. */
585 err = vpn_provider_set_string(provider, name,
592 static GString *append_to_gstring(GString *str, const char *value)
595 return g_string_new(value);
597 g_string_append_printf(str, ",%s", value);
602 static DBusMessage *set_properties(DBusMessageIter *iter, DBusMessage *msg,
605 struct vpn_provider *provider = data;
606 DBusMessageIter dict;
609 GString *invalid = NULL;
610 GString *denied = NULL;
614 for (dbus_message_iter_recurse(iter, &dict);
615 dbus_message_iter_get_arg_type(&dict) ==
616 DBUS_TYPE_DICT_ENTRY;
617 dbus_message_iter_next(&dict)) {
618 DBusMessageIter entry, value;
620 dbus_message_iter_recurse(&dict, &entry);
622 * Ignore invalid types in order to process all values in the
623 * dict. If there is an invalid type in between the dict there
624 * may already be changes on some values and breaking out here
625 * would have the provider in an inconsistent state, leaving
626 * the rest, potentially correct property values untouched.
628 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
631 dbus_message_iter_get_basic(&entry, &key);
635 dbus_message_iter_next(&entry);
636 /* Ignore and report back all non variant types. */
637 if (dbus_message_iter_get_arg_type(&entry)
638 != DBUS_TYPE_VARIANT) {
639 invalid = append_to_gstring(invalid, key);
643 dbus_message_iter_recurse(&entry, &value);
645 type = dbus_message_iter_get_arg_type(&value);
647 case DBUS_TYPE_STRING:
648 case DBUS_TYPE_ARRAY:
649 case DBUS_TYPE_BOOLEAN:
652 /* Ignore and report back all invalid property types */
653 invalid = append_to_gstring(invalid, key);
657 err = set_provider_property(provider, key, &value, type);
663 invalid = append_to_gstring(invalid, key);
666 denied = append_to_gstring(denied, key);
672 vpn_provider_save(provider);
674 if (invalid || denied) {
676 char *invalid_str = g_string_free(invalid, FALSE);
677 char *denied_str = g_string_free(denied, FALSE);
680 * If there are both invalid and denied properties report
681 * back invalid arguments. Add also the failed properties to
684 error = g_dbus_create_error(msg, (invalid ?
685 CONNMAN_ERROR_INTERFACE ".InvalidProperty" :
686 CONNMAN_ERROR_INTERFACE ".PermissionDenied"),
687 "%s %s%s%s", (invalid ? "Invalid properties" :
688 "Permission denied"),
689 (invalid ? invalid_str : ""),
690 (invalid && denied ? "," : ""),
691 (denied ? denied_str : ""));
699 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
702 static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
705 struct vpn_provider *provider = data;
706 DBusMessageIter iter, value;
711 DBG("conn %p", conn);
713 if (provider->immutable)
714 return __connman_error_not_supported(msg);
716 if (!dbus_message_iter_init(msg, &iter))
717 return __connman_error_invalid_arguments(msg);
719 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
720 return __connman_error_invalid_arguments(msg);
722 dbus_message_iter_get_basic(&iter, &name);
723 dbus_message_iter_next(&iter);
725 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
726 return __connman_error_invalid_arguments(msg);
728 dbus_message_iter_recurse(&iter, &value);
730 type = dbus_message_iter_get_arg_type(&value);
731 if (type == DBUS_TYPE_ARRAY && g_str_equal(name, "Properties"))
732 return set_properties(&value, msg, data);
734 err = set_provider_property(provider, name, &value, type);
737 vpn_provider_save(provider);
742 return __connman_error_invalid_property(msg);
744 return __connman_error_failed(msg, -err);
747 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
750 static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
753 struct vpn_provider *provider = data;
758 DBG("conn %p", conn);
760 if (provider->immutable)
761 return __connman_error_not_supported(msg);
763 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
766 if (g_str_equal(name, "UserRoutes")) {
768 * If either user_routes or user_networks has any entries
769 * there is a change that is to be written to settings file.
771 if (g_hash_table_size(provider->user_routes) ||
772 provider->user_networks)
775 del_routes(provider);
777 send_routes(provider, provider->user_routes, name);
778 } else if (vpn_provider_get_string(provider, name)) {
779 err = vpn_provider_set_string(provider, name, NULL);
787 return __connman_error_invalid_property(msg);
789 return __connman_error_failed(msg, -err);
792 return __connman_error_invalid_property(msg);
796 vpn_provider_save(provider);
798 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
801 static gboolean do_connect_timeout_function(gpointer data)
803 struct vpn_provider_connect_data *cdata = data;
804 struct vpn_provider *provider = cdata->provider;
809 /* Keep in main loop if connman is not online. */
811 return G_SOURCE_CONTINUE;
813 provider->do_connect_timeout = 0;
814 err = __vpn_provider_connect(provider, cdata->msg);
816 if (err < 0 && err != -EINPROGRESS) {
817 g_dbus_send_message(cdata->conn,
818 __connman_error_failed(cdata->msg, -err));
822 return G_SOURCE_REMOVE;
825 static void do_connect_timeout_free(gpointer data)
827 struct vpn_provider_connect_data *cdata = data;
830 g_dbus_send_message(cdata->conn,
831 __connman_error_operation_aborted(cdata->msg));
833 dbus_connection_unref(cdata->conn);
837 static void do_connect_later(struct vpn_provider *provider,
838 DBusConnection *conn, DBusMessage *msg)
840 struct vpn_provider_connect_data *cdata =
841 g_new0(struct vpn_provider_connect_data, 1);
843 cdata->conn = dbus_connection_ref(conn);
844 cdata->msg = dbus_message_ref(msg);
845 cdata->provider = provider;
847 if (provider->do_connect_timeout)
848 g_source_remove(provider->do_connect_timeout);
850 provider->do_connect_timeout =
851 g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 1,
852 do_connect_timeout_function, cdata,
853 do_connect_timeout_free);
856 static DBusMessage *do_connect(DBusConnection *conn, DBusMessage *msg,
859 struct vpn_provider *provider = data;
862 DBG("conn %p provider %p", conn, provider);
864 if (!connman_online) {
865 if (state_query_completed) {
866 DBG("%s not started - ConnMan not online/ready",
867 provider->identifier);
868 return __connman_error_failed(msg, ENOLINK);
871 DBG("%s start delayed - ConnMan state not queried",
872 provider->identifier);
873 do_connect_later(provider, conn, msg);
877 /* Cancel delayed connection if connmand is online. */
878 if (provider->do_connect_timeout) {
879 g_source_remove(provider->do_connect_timeout);
880 provider->do_connect_timeout = 0;
883 err = __vpn_provider_connect(provider, msg);
884 if (err < 0 && err != -EINPROGRESS)
885 return __connman_error_failed(msg, -err);
890 static DBusMessage *do_connect2(DBusConnection *conn, DBusMessage *msg,
893 return do_connect(conn, msg, data);
896 static DBusMessage *do_disconnect(DBusConnection *conn, DBusMessage *msg,
899 struct vpn_provider *provider = data;
902 DBG("conn %p provider %p", conn, provider);
904 err = __vpn_provider_disconnect(provider);
905 if (err < 0 && err != -EINPROGRESS)
906 return __connman_error_failed(msg, -err);
908 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
911 static const GDBusMethodTable connection_methods[] = {
912 { GDBUS_METHOD("GetProperties",
913 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
915 { GDBUS_METHOD("SetProperty",
916 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
917 NULL, set_property) },
918 { GDBUS_METHOD("ClearProperty",
919 GDBUS_ARGS({ "name", "s" }), NULL,
921 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, do_connect) },
922 { GDBUS_ASYNC_METHOD("Connect2",
923 GDBUS_ARGS({ "dbus_sender", "s" }),
924 NULL, do_connect2) },
925 { GDBUS_METHOD("Disconnect", NULL, NULL, do_disconnect) },
929 static const GDBusSignalTable connection_signals[] = {
930 { GDBUS_SIGNAL("PropertyChanged",
931 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
935 static void resolv_result(GResolvResultStatus status,
936 char **results, gpointer user_data)
938 struct vpn_provider *provider = user_data;
940 DBG("status %d", status);
942 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results &&
943 g_strv_length(results) > 0)
944 provider->host_ip = g_strdupv(results);
946 vpn_provider_unref(provider);
948 /* Remove the resolver here so that it will not be left
949 * hanging around and cause double free in unregister_provider()
951 g_resolv_unref(provider->resolv);
952 provider->resolv = NULL;
955 static void provider_resolv_host_addr(struct vpn_provider *provider)
960 if (connman_inet_check_ipaddress(provider->host) > 0)
963 if (provider->host_ip)
967 * If the hostname is not numeric, try to resolv it. We do not wait
968 * the result as it might take some time. We will get the result
969 * before VPN will feed routes to us because VPN client will need
970 * the IP address also before VPN connection can be established.
972 provider->resolv = g_resolv_new(0);
973 if (!provider->resolv) {
974 DBG("Cannot resolv %s", provider->host);
978 DBG("Trying to resolv %s", provider->host);
980 vpn_provider_ref(provider);
982 g_resolv_lookup_hostname(provider->resolv, provider->host,
983 resolv_result, provider);
986 void __vpn_provider_append_properties(struct vpn_provider *provider,
987 DBusMessageIter *iter)
989 dbus_bool_t split_routing;
992 connman_dbus_dict_append_basic(iter, "Host",
993 DBUS_TYPE_STRING, &provider->host);
995 if (provider->domain)
996 connman_dbus_dict_append_basic(iter, "Domain",
997 DBUS_TYPE_STRING, &provider->domain);
1000 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
1003 split_routing = provider->do_split_routing;
1004 connman_dbus_dict_append_basic(iter, "SplitRouting", DBUS_TYPE_BOOLEAN,
1008 int __vpn_provider_append_user_route(struct vpn_provider *provider,
1009 int family, const char *network,
1010 const char *netmask, const char *gateway)
1012 struct vpn_route *route;
1013 char *key = g_strdup_printf("%d/%s/%s/%s", family, network,
1014 netmask, gateway ? gateway : "");
1016 DBG("family %d network %s netmask %s gw %s", family, network,
1019 route = g_hash_table_lookup(provider->user_routes, key);
1021 route = g_try_new0(struct vpn_route, 1);
1023 connman_error("out of memory");
1027 route->family = family;
1028 route->network = g_strdup(network);
1029 route->netmask = g_strdup(netmask);
1030 route->gateway = g_strdup(gateway);
1032 g_hash_table_replace(provider->user_routes, key, route);
1039 static struct vpn_route *get_route(char *route_str)
1041 char **elems = g_strsplit(route_str, "/", 0);
1042 char *network, *netmask, *gateway, *family_str;
1043 int family = PF_UNSPEC;
1044 struct vpn_route *route = NULL;
1049 family_str = elems[0];
1052 if (!network || network[0] == '\0')
1056 if (!netmask || netmask[0] == '\0')
1061 route = g_try_new0(struct vpn_route, 1);
1065 if (family_str[0] == '\0' || atoi(family_str) == 0) {
1068 switch (family_str[0]) {
1078 if (g_strrstr(network, ":")) {
1079 if (family != PF_UNSPEC && family != AF_INET6)
1080 DBG("You have IPv6 address but you have non IPv6 route");
1081 } else if (g_strrstr(network, ".")) {
1082 if (family != PF_UNSPEC && family != AF_INET)
1083 DBG("You have IPv4 address but you have non IPv4 route");
1085 if (!g_strrstr(netmask, ".")) {
1086 /* We have netmask length */
1088 struct in_addr netmask_in;
1089 unsigned char prefix_len = 32;
1091 long int value = strtol(netmask, &ptr, 10);
1093 if (ptr != netmask && *ptr == '\0' && value <= 32)
1096 addr = 0xffffffff << (32 - prefix_len);
1097 netmask_in.s_addr = htonl(addr);
1098 netmask = inet_ntoa(netmask_in);
1100 DBG("network %s netmask %s", network, netmask);
1104 if (family == PF_UNSPEC) {
1105 family = connman_inet_check_ipaddress(network);
1106 if (family < 0 || family == PF_UNSPEC)
1110 route->family = family;
1111 route->network = g_strdup(network);
1112 route->netmask = g_strdup(netmask);
1113 route->gateway = g_strdup(gateway);
1120 static GSList *get_routes(gchar **networks)
1122 struct vpn_route *route;
1123 GSList *routes = NULL;
1126 for (i = 0; networks[i]; i++) {
1127 route = get_route(networks[i]);
1129 routes = g_slist_prepend(routes, route);
1135 static int provider_load_from_keyfile(struct vpn_provider *provider,
1141 gsize length, num_user_networks;
1142 gchar **networks = NULL;
1144 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
1147 g_key_file_free(keyfile);
1151 for (idx = 0; idx < length; idx++) {
1152 key = settings[idx];
1156 if (g_str_equal(key, "Networks")) {
1157 networks = __vpn_config_get_string_list(keyfile,
1158 provider->identifier,key,
1159 &num_user_networks, NULL);
1160 provider->user_networks = get_routes(networks);
1162 value = __vpn_config_get_string(keyfile,
1163 provider->identifier, key,
1166 vpn_provider_set_string(provider, key, value);
1171 g_strfreev(settings);
1172 g_strfreev(networks);
1174 if (provider->user_networks)
1175 set_user_networks(provider, provider->user_networks);
1181 static int vpn_provider_load(struct vpn_provider *provider)
1185 DBG("provider %p", provider);
1187 keyfile = __connman_storage_load_provider(provider->identifier);
1191 provider_load_from_keyfile(provider, keyfile);
1193 g_key_file_free(keyfile);
1197 static gchar **create_network_list(GSList *networks, gsize *count)
1200 gchar **result = NULL;
1201 gchar **prev_result;
1202 unsigned int num_elems = 0;
1204 for (list = networks; list; list = g_slist_next(list)) {
1205 struct vpn_route *route = list->data;
1208 prev_result = result;
1209 result = g_try_realloc(result,
1210 (num_elems + 1) * sizeof(gchar *));
1212 g_free(prev_result);
1216 switch (route->family) {
1228 result[num_elems] = g_strdup_printf("%d/%s/%s/%s",
1229 family, route->network, route->netmask,
1230 !route->gateway ? "" : route->gateway);
1235 prev_result = result;
1236 result = g_try_realloc(result, (num_elems + 1) * sizeof(gchar *));
1238 g_free(prev_result);
1242 result[num_elems] = NULL;
1247 static void reset_error_counters(struct vpn_provider *provider)
1252 DBG("provider %p", provider);
1254 provider->auth_error_counter = provider->conn_error_counter = 0;
1257 static int vpn_provider_save(struct vpn_provider *provider)
1262 DBG("provider %p immutable %s", provider,
1263 provider->immutable ? "yes" : "no");
1265 reset_error_counters(provider);
1267 if (provider->state == VPN_PROVIDER_STATE_FAILURE)
1268 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
1270 if (provider->immutable) {
1272 * Do not save providers that are provisioned via .config
1278 keyfile = g_key_file_new();
1282 g_key_file_set_string(keyfile, provider->identifier,
1283 "Name", provider->name);
1284 g_key_file_set_string(keyfile, provider->identifier,
1285 "Type", provider->type);
1286 g_key_file_set_string(keyfile, provider->identifier,
1287 "Host", provider->host);
1288 g_key_file_set_string(keyfile, provider->identifier,
1289 "VPN.Domain", provider->domain);
1291 value = vpn_provider_get_string(provider, "AuthErrorLimit");
1293 g_key_file_set_string(keyfile, provider->identifier,
1294 "AuthErrorLimit", value);
1296 if (provider->user_networks) {
1298 gsize network_count;
1300 networks = create_network_list(provider->user_networks,
1303 g_key_file_set_string_list(keyfile,
1304 provider->identifier,
1306 (const gchar ** const)networks,
1308 g_strfreev(networks);
1312 if (provider->config_file && strlen(provider->config_file) > 0)
1313 g_key_file_set_string(keyfile, provider->identifier,
1314 "Config.file", provider->config_file);
1316 if (provider->config_entry &&
1317 strlen(provider->config_entry) > 0)
1318 g_key_file_set_string(keyfile, provider->identifier,
1319 "Config.ident", provider->config_entry);
1321 if (provider->driver && provider->driver->save)
1322 provider->driver->save(provider, keyfile);
1324 __connman_storage_save_provider(keyfile, provider->identifier);
1325 g_key_file_free(keyfile);
1330 struct vpn_provider *__vpn_provider_lookup(const char *identifier)
1332 struct vpn_provider *provider = NULL;
1334 provider = g_hash_table_lookup(provider_hash, identifier);
1339 static bool match_driver(struct vpn_provider *provider,
1340 struct vpn_provider_driver *driver)
1342 if (g_strcmp0(driver->name, provider->type) == 0)
1348 static int provider_probe(struct vpn_provider *provider)
1352 DBG("provider %p driver %p name %s", provider, provider->driver,
1355 if (provider->driver)
1358 for (list = driver_list; list; list = list->next) {
1359 struct vpn_provider_driver *driver = list->data;
1361 if (!match_driver(provider, driver))
1364 DBG("driver %p name %s", driver, driver->name);
1366 if (driver->probe && driver->probe(provider) == 0) {
1367 provider->driver = driver;
1372 if (!provider->driver)
1378 static void provider_remove(struct vpn_provider *provider)
1380 if (provider->driver) {
1381 provider->driver->remove(provider);
1382 provider->driver = NULL;
1386 static int provider_register(struct vpn_provider *provider)
1388 return provider_probe(provider);
1391 static void provider_unregister(struct vpn_provider *provider)
1393 provider_remove(provider);
1396 struct vpn_provider *
1397 vpn_provider_ref_debug(struct vpn_provider *provider,
1398 const char *file, int line, const char *caller)
1400 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
1401 file, line, caller);
1403 __sync_fetch_and_add(&provider->refcount, 1);
1408 static void provider_destruct(struct vpn_provider *provider)
1410 DBG("provider %p", provider);
1412 if (provider->do_connect_timeout)
1413 g_source_remove(provider->do_connect_timeout);
1415 if (provider->notify_id != 0)
1416 g_source_remove(provider->notify_id);
1418 g_free(provider->name);
1419 g_free(provider->type);
1420 g_free(provider->host);
1421 g_free(provider->domain);
1422 g_free(provider->identifier);
1423 g_free(provider->path);
1424 g_slist_free_full(provider->user_networks, free_route);
1425 g_strfreev(provider->nameservers);
1426 g_hash_table_destroy(provider->routes);
1427 g_hash_table_destroy(provider->user_routes);
1428 g_hash_table_destroy(provider->setting_strings);
1429 if (provider->resolv) {
1430 g_resolv_unref(provider->resolv);
1431 provider->resolv = NULL;
1433 __vpn_ipconfig_unref(provider->ipconfig_ipv4);
1434 __vpn_ipconfig_unref(provider->ipconfig_ipv6);
1436 g_strfreev(provider->host_ip);
1437 g_free(provider->config_file);
1438 g_free(provider->config_entry);
1439 connman_ipaddress_free(provider->prev_ipv4_addr);
1440 connman_ipaddress_free(provider->prev_ipv6_addr);
1444 void vpn_provider_unref_debug(struct vpn_provider *provider,
1445 const char *file, int line, const char *caller)
1447 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
1448 file, line, caller);
1450 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
1453 provider_remove(provider);
1455 provider_destruct(provider);
1458 static void configuration_count_add(void)
1460 DBG("count %d", configuration_count + 1);
1462 __sync_fetch_and_add(&configuration_count, 1);
1465 static void configuration_count_del(void)
1467 DBG("count %d", configuration_count - 1);
1469 if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
1473 int __vpn_provider_disconnect(struct vpn_provider *provider)
1477 DBG("provider %p", provider);
1479 if (provider->driver && provider->driver->disconnect)
1480 err = provider->driver->disconnect(provider);
1484 if (err == -EINPROGRESS)
1485 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1490 static void connect_cb(struct vpn_provider *provider, void *user_data,
1493 DBusMessage *pending = user_data;
1495 DBG("provider %p user %p error %d", provider, user_data, error);
1498 DBusMessage *reply = __connman_error_failed(pending, error);
1500 g_dbus_send_message(connection, reply);
1504 vpn_provider_indicate_error(provider,
1505 VPN_PROVIDER_ERROR_AUTH_FAILED);
1509 * No reply, disconnect called by connmand because of
1510 * connection timeout.
1516 /* No reply or timed out -> cancel the agent request */
1517 connman_agent_cancel(provider);
1518 vpn_provider_indicate_error(provider,
1519 VPN_PROVIDER_ERROR_UNKNOWN);
1525 * This can be called in other situations than when
1526 * VPN agent error checker is called. In such case
1527 * react to both ECONNABORTED and ECANCELED as if the
1528 * connection was called to terminate and do full
1529 * disconnect -> idle cycle when being connected or
1530 * ready. Setting the state also using the driver
1531 * callback (vpn_set_state()) ensures that the driver is
1532 * being disconnected as well and eventually the vpn
1533 * process gets killed and vpn_died() is called to make
1534 * the provider back to idle state.
1536 if (provider->state == VPN_PROVIDER_STATE_CONNECT ||
1538 VPN_PROVIDER_STATE_READY) {
1539 if (provider->driver->set_state)
1540 provider->driver->set_state(provider,
1541 VPN_PROVIDER_STATE_DISCONNECT);
1543 vpn_provider_set_state(provider,
1544 VPN_PROVIDER_STATE_DISCONNECT);
1548 vpn_provider_indicate_error(provider,
1549 VPN_PROVIDER_ERROR_CONNECT_FAILED);
1550 vpn_provider_set_state(provider,
1551 VPN_PROVIDER_STATE_FAILURE);
1554 reset_error_counters(provider);
1555 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
1558 dbus_message_unref(pending);
1561 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
1566 DBG("provider %p state %d", provider, provider->state);
1568 switch (provider->state) {
1570 * When previous connection has failed change state to idle and let
1571 * the connmand to process this information as well. Return -EINPROGRESS
1572 * to indicate that transition is in progress and next connection
1573 * attempt will continue as normal.
1575 case VPN_PROVIDER_STATE_FAILURE:
1576 if (provider->driver && provider->driver->set_state)
1577 provider->driver->set_state(provider,
1578 VPN_PROVIDER_STATE_IDLE);
1580 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
1583 * If re-using a provider and it is being disconnected let it finish
1584 * the disconnect process in order to let vpn.c:vpn_died() to get
1585 * processed and everything cleaned up. Otherwise the reference
1586 * counters are not decreased properly causing the previous interface
1587 * being left up and its routes will remain in routing table. Return
1588 * -EINPROGRESS to indicate that transition is in progress.
1590 case VPN_PROVIDER_STATE_DISCONNECT:
1592 * Failure transition or disconnecting does not yield a
1593 * message to be sent. Send in progress message to avoid
1594 * D-Bus LimitsExceeded error message.
1596 reply = __connman_error_in_progress(msg);
1598 g_dbus_send_message(connection, reply);
1600 return -EINPROGRESS;
1601 case VPN_PROVIDER_STATE_UNKNOWN:
1602 case VPN_PROVIDER_STATE_IDLE:
1603 case VPN_PROVIDER_STATE_CONNECT:
1604 case VPN_PROVIDER_STATE_READY:
1608 if (provider->driver && provider->driver->connect) {
1609 const char *dbus_sender = dbus_message_get_sender(msg);
1611 dbus_message_ref(msg);
1613 if (dbus_message_has_signature(msg,
1614 DBUS_TYPE_STRING_AS_STRING)) {
1615 const char *sender = NULL;
1617 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING,
1618 &sender, DBUS_TYPE_INVALID);
1619 if (sender && sender[0])
1620 dbus_sender = sender;
1623 err = provider->driver->connect(provider, connect_cb,
1628 if (err == -EINPROGRESS)
1629 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1634 static void connection_removed_signal(struct vpn_provider *provider)
1636 DBusMessage *signal;
1637 DBusMessageIter iter;
1639 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1640 VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1644 dbus_message_iter_init_append(signal, &iter);
1645 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1647 dbus_connection_send(connection, signal, NULL);
1648 dbus_message_unref(signal);
1651 static char *get_ident(const char *path)
1658 pos = strrchr(path, '/');
1665 int __vpn_provider_remove(const char *path)
1667 struct vpn_provider *provider;
1670 DBG("path %s", path);
1672 ident = get_ident(path);
1674 provider = __vpn_provider_lookup(ident);
1676 return __vpn_provider_delete(provider);
1681 int __vpn_provider_delete(struct vpn_provider *provider)
1683 DBG("Deleting VPN %s", provider->identifier);
1685 connection_removed_signal(provider);
1687 provider_unregister(provider);
1689 __connman_storage_remove_provider(provider->identifier);
1691 g_hash_table_remove(provider_hash, provider->identifier);
1696 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1698 struct vpn_provider *provider = user_data;
1699 const char *address, *gateway, *peer;
1701 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1704 struct in_addr netmask;
1708 prefixlen = __vpn_ipconfig_get_prefixlen(
1709 provider->ipconfig_ipv4);
1711 addr = 0xffffffff << (32 - prefixlen);
1712 netmask.s_addr = htonl(addr);
1713 mask = inet_ntoa(netmask);
1715 connman_dbus_dict_append_basic(iter, "Address",
1716 DBUS_TYPE_STRING, &address);
1718 connman_dbus_dict_append_basic(iter, "Netmask",
1719 DBUS_TYPE_STRING, &mask);
1722 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1724 connman_dbus_dict_append_basic(iter, "Gateway",
1725 DBUS_TYPE_STRING, &gateway);
1727 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1729 connman_dbus_dict_append_basic(iter, "Peer",
1730 DBUS_TYPE_STRING, &peer);
1733 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1735 struct vpn_provider *provider = user_data;
1736 const char *address, *gateway, *peer;
1738 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1740 unsigned char prefixlen;
1742 connman_dbus_dict_append_basic(iter, "Address",
1743 DBUS_TYPE_STRING, &address);
1745 prefixlen = __vpn_ipconfig_get_prefixlen(
1746 provider->ipconfig_ipv6);
1748 connman_dbus_dict_append_basic(iter, "PrefixLength",
1749 DBUS_TYPE_BYTE, &prefixlen);
1752 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1754 connman_dbus_dict_append_basic(iter, "Gateway",
1755 DBUS_TYPE_STRING, &gateway);
1757 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1759 connman_dbus_dict_append_basic(iter, "Peer",
1760 DBUS_TYPE_STRING, &peer);
1763 static const char *state2string(enum vpn_provider_state state)
1766 case VPN_PROVIDER_STATE_UNKNOWN:
1768 case VPN_PROVIDER_STATE_IDLE:
1770 case VPN_PROVIDER_STATE_CONNECT:
1771 return "configuration";
1772 case VPN_PROVIDER_STATE_READY:
1774 case VPN_PROVIDER_STATE_DISCONNECT:
1775 return "disconnect";
1776 case VPN_PROVIDER_STATE_FAILURE:
1783 static void append_nameservers(DBusMessageIter *iter, char **servers)
1789 for (i = 0; servers[i]; i++) {
1790 DBG("servers[%d] %s", i, servers[i]);
1791 dbus_message_iter_append_basic(iter,
1792 DBUS_TYPE_STRING, &servers[i]);
1796 static void append_dns(DBusMessageIter *iter, void *user_data)
1798 struct vpn_provider *provider = user_data;
1800 if (provider->nameservers)
1801 append_nameservers(iter, provider->nameservers);
1804 static time_t get_uptime(void)
1806 struct timespec t = { 0 };
1808 if (clock_gettime(CLOCK_BOOTTIME, &t) == -1) {
1809 connman_warn("clock_gettime() error %d, uptime failed", errno);
1816 static int provider_indicate_state(struct vpn_provider *provider,
1817 enum vpn_provider_state state)
1820 enum vpn_provider_state old_state;
1822 str = state2string(state);
1823 DBG("provider %p state %s/%d", provider, str, state);
1827 old_state = provider->state;
1828 provider->state = state;
1830 if (state == VPN_PROVIDER_STATE_READY) {
1831 provider->previous_connect_time = get_uptime();
1833 connman_dbus_property_changed_basic(provider->path,
1834 VPN_CONNECTION_INTERFACE, "Index",
1835 DBUS_TYPE_INT32, &provider->index);
1837 if (provider->family == AF_INET)
1838 connman_dbus_property_changed_dict(provider->path,
1839 VPN_CONNECTION_INTERFACE, "IPv4",
1840 append_ipv4, provider);
1841 else if (provider->family == AF_INET6)
1842 connman_dbus_property_changed_dict(provider->path,
1843 VPN_CONNECTION_INTERFACE, "IPv6",
1844 append_ipv6, provider);
1846 connman_dbus_property_changed_array(provider->path,
1847 VPN_CONNECTION_INTERFACE,
1850 append_dns, provider);
1852 if (provider->domain)
1853 connman_dbus_property_changed_basic(provider->path,
1854 VPN_CONNECTION_INTERFACE,
1860 if (old_state != state)
1861 connman_dbus_property_changed_basic(provider->path,
1862 VPN_CONNECTION_INTERFACE, "State",
1863 DBUS_TYPE_STRING, &str);
1868 static void append_state(DBusMessageIter *iter,
1869 struct vpn_provider *provider)
1873 switch (provider->state) {
1874 case VPN_PROVIDER_STATE_UNKNOWN:
1875 case VPN_PROVIDER_STATE_IDLE:
1878 case VPN_PROVIDER_STATE_CONNECT:
1879 str = "configuration";
1881 case VPN_PROVIDER_STATE_READY:
1884 case VPN_PROVIDER_STATE_DISCONNECT:
1887 case VPN_PROVIDER_STATE_FAILURE:
1892 connman_dbus_dict_append_basic(iter, "State",
1893 DBUS_TYPE_STRING, &str);
1896 static void append_properties(DBusMessageIter *iter,
1897 struct vpn_provider *provider)
1899 DBusMessageIter dict;
1900 GHashTableIter hash;
1901 gpointer value, key;
1902 dbus_bool_t immutable;
1903 dbus_bool_t split_routing;
1905 connman_dbus_dict_open(iter, &dict);
1907 append_state(&dict, provider);
1910 connman_dbus_dict_append_basic(&dict, "Type",
1911 DBUS_TYPE_STRING, &provider->type);
1914 connman_dbus_dict_append_basic(&dict, "Name",
1915 DBUS_TYPE_STRING, &provider->name);
1918 connman_dbus_dict_append_basic(&dict, "Host",
1919 DBUS_TYPE_STRING, &provider->host);
1920 if (provider->index >= 0)
1921 connman_dbus_dict_append_basic(&dict, "Index",
1922 DBUS_TYPE_INT32, &provider->index);
1923 if (provider->domain)
1924 connman_dbus_dict_append_basic(&dict, "Domain",
1925 DBUS_TYPE_STRING, &provider->domain);
1927 immutable = provider->immutable;
1928 connman_dbus_dict_append_basic(&dict, "Immutable", DBUS_TYPE_BOOLEAN,
1931 split_routing = provider->do_split_routing;
1932 connman_dbus_dict_append_basic(&dict, "SplitRouting",
1933 DBUS_TYPE_BOOLEAN, &split_routing);
1935 if (provider->family == AF_INET)
1936 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1938 else if (provider->family == AF_INET6)
1939 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1942 connman_dbus_dict_append_array(&dict, "Nameservers",
1943 DBUS_TYPE_STRING, append_dns, provider);
1945 connman_dbus_dict_append_array(&dict, "UserRoutes",
1946 DBUS_TYPE_DICT_ENTRY, append_routes,
1947 provider->user_routes);
1949 connman_dbus_dict_append_array(&dict, "ServerRoutes",
1950 DBUS_TYPE_DICT_ENTRY, append_routes,
1953 if (provider->setting_strings) {
1954 g_hash_table_iter_init(&hash, provider->setting_strings);
1956 while (g_hash_table_iter_next(&hash, &key, &value)) {
1957 struct vpn_setting *setting = value;
1959 if (!setting->hide_value && setting->value)
1960 connman_dbus_dict_append_basic(&dict, key,
1966 connman_dbus_dict_close(iter, &dict);
1969 static void connection_added_signal(struct vpn_provider *provider)
1971 DBusMessage *signal;
1972 DBusMessageIter iter;
1974 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1975 VPN_MANAGER_INTERFACE, "ConnectionAdded");
1979 dbus_message_iter_init_append(signal, &iter);
1980 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1982 append_properties(&iter, provider);
1984 dbus_connection_send(connection, signal, NULL);
1985 dbus_message_unref(signal);
1988 static int set_connected(struct vpn_provider *provider,
1991 struct vpn_ipconfig *ipconfig;
1993 DBG("provider %p id %s connected %d", provider,
1994 provider->identifier, connected);
1997 if (provider->family == AF_INET6)
1998 ipconfig = provider->ipconfig_ipv6;
2000 ipconfig = provider->ipconfig_ipv4;
2002 __vpn_ipconfig_address_add(ipconfig, provider->family);
2004 provider_indicate_state(provider,
2005 VPN_PROVIDER_STATE_READY);
2007 provider_indicate_state(provider,
2008 VPN_PROVIDER_STATE_DISCONNECT);
2010 provider_indicate_state(provider,
2011 VPN_PROVIDER_STATE_IDLE);
2017 int vpn_provider_set_state(struct vpn_provider *provider,
2018 enum vpn_provider_state state)
2024 case VPN_PROVIDER_STATE_UNKNOWN:
2026 case VPN_PROVIDER_STATE_IDLE:
2027 return set_connected(provider, false);
2028 case VPN_PROVIDER_STATE_CONNECT:
2029 return provider_indicate_state(provider, state);
2030 case VPN_PROVIDER_STATE_READY:
2031 return set_connected(provider, true);
2032 case VPN_PROVIDER_STATE_DISCONNECT:
2033 return provider_indicate_state(provider, state);
2034 case VPN_PROVIDER_STATE_FAILURE:
2035 return provider_indicate_state(provider, state);
2040 void vpn_provider_add_error(struct vpn_provider *provider,
2041 enum vpn_provider_error error)
2044 case VPN_PROVIDER_ERROR_UNKNOWN:
2045 provider->previous_connect_time = 0;
2047 case VPN_PROVIDER_ERROR_CONNECT_FAILED:
2048 provider->previous_connect_time = 0;
2049 ++provider->conn_error_counter;
2051 case VPN_PROVIDER_ERROR_LOGIN_FAILED:
2052 case VPN_PROVIDER_ERROR_AUTH_FAILED:
2053 ++provider->auth_error_counter;
2057 DBG("%p connect errors %d auth errors %d", provider,
2058 provider->conn_error_counter,
2059 provider->auth_error_counter);
2062 int vpn_provider_indicate_error(struct vpn_provider *provider,
2063 enum vpn_provider_error error)
2065 DBG("provider %p id %s error %d", provider, provider->identifier,
2069 * Ignore adding of errors when the VPN is idle or not set. Calls may
2070 * happen in a case when networks are rapidly changed and the call to
2071 * vpn_died() is done before executing the connect_cb() from the
2072 * plugin. Then vpn.c:vpn_died() executes the plugin specific died()
2073 * function which may free the plugin private data, containing also
2074 * the callback which hasn't yet been called. As a result the provider
2075 * might already been reset to idle state when the callback is executed
2076 * resulting in unnecessary reset of the previous successful connect
2077 * timer and adding of an error for already disconnected VPN.
2079 if (provider->state == VPN_PROVIDER_STATE_IDLE ||
2080 provider->state == VPN_PROVIDER_STATE_UNKNOWN)
2083 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
2085 vpn_provider_add_error(provider, error);
2087 if (provider->driver && provider->driver->set_state)
2088 provider->driver->set_state(provider, provider->state);
2093 static gboolean provider_property_changed(DBusConnection *conn,
2094 DBusMessage *message, void *user_data)
2096 DBusMessageIter iter;
2097 DBusMessageIter value;
2098 struct vpn_provider *provider = user_data;
2101 if (!dbus_message_iter_init(message, &iter))
2104 dbus_message_iter_get_basic(&iter, &key);
2106 dbus_message_iter_next(&iter);
2107 dbus_message_iter_recurse(&iter, &value);
2109 DBG("provider %p key %s", provider, key);
2111 if (g_str_equal(key, "SplitRouting")) {
2112 dbus_bool_t split_routing;
2114 if (dbus_message_iter_get_arg_type(&value) !=
2118 dbus_message_iter_get_basic(&value, &split_routing);
2120 DBG("property %s value %s", key, bool2str(split_routing));
2123 * Even though this is coming from connmand, signal the value
2124 * for other components listening to the changes via VPN API
2125 * only. provider.c will skip setting the same value in order
2126 * to avoid signaling loop. This is needed for ensuring that
2127 * all components using VPN API will be informed about the
2128 * correct status of SplitRouting. Especially when loading the
2129 * services after a crash, for instance.
2131 vpn_provider_set_boolean(provider, "SplitRouting",
2132 split_routing, true);
2139 static int connection_unregister(struct vpn_provider *provider)
2141 DBG("provider %p path %s", provider, provider->path);
2143 if (provider->signal_watch) {
2144 g_dbus_remove_watch(connection, provider->signal_watch);
2145 provider->signal_watch = 0;
2148 if (!provider->path)
2151 g_dbus_unregister_interface(connection, provider->path,
2152 VPN_CONNECTION_INTERFACE);
2154 g_free(provider->path);
2155 provider->path = NULL;
2160 static int connection_register(struct vpn_provider *provider)
2162 char *connmand_vpn_path;
2164 DBG("provider %p path %s", provider, provider->path);
2169 provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
2170 provider->identifier);
2172 g_dbus_register_interface(connection, provider->path,
2173 VPN_CONNECTION_INTERFACE,
2174 connection_methods, connection_signals,
2175 NULL, provider, NULL);
2177 connmand_vpn_path = g_strdup_printf("%s/service/vpn_%s", CONNMAN_PATH,
2178 provider->identifier);
2180 provider->signal_watch = g_dbus_add_signal_watch(connection,
2181 CONNMAN_SERVICE, connmand_vpn_path,
2182 CONNMAN_SERVICE_INTERFACE,
2184 provider_property_changed,
2187 g_free(connmand_vpn_path);
2192 static void unregister_provider(gpointer data)
2194 struct vpn_provider *provider = data;
2196 configuration_count_del();
2198 connection_unregister(provider);
2200 /* If the provider has any DNS resolver queries pending,
2201 * they need to be cleared here because the unref will not
2202 * be able to do that (because the provider_resolv_host_addr()
2203 * has increased the ref count by 1). This is quite rare as
2204 * normally the resolving either returns a value or has a
2205 * timeout which clears the memory. Typically resolv_result() will
2206 * unref the provider but in this case that call has not yet
2209 if (provider->resolv)
2210 vpn_provider_unref(provider);
2212 vpn_provider_unref(provider);
2215 static void provider_initialize(struct vpn_provider *provider)
2217 DBG("provider %p", provider);
2219 provider->index = 0;
2221 provider->name = NULL;
2222 provider->type = NULL;
2223 provider->domain = NULL;
2224 provider->identifier = NULL;
2225 provider->immutable = false;
2226 provider->do_split_routing = false;
2227 provider->user_networks = NULL;
2228 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2230 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
2231 g_free, free_route);
2232 provider->setting_strings = g_hash_table_new_full(g_str_hash,
2233 g_str_equal, g_free, free_setting);
2234 provider->auth_error_limit = AUTH_ERROR_LIMIT_DEFAULT;
2237 static struct vpn_provider *vpn_provider_new(void)
2239 struct vpn_provider *provider;
2241 provider = g_try_new0(struct vpn_provider, 1);
2245 provider->refcount = 1;
2247 DBG("provider %p", provider);
2248 provider_initialize(provider);
2253 static struct vpn_provider *vpn_provider_get(const char *identifier)
2255 struct vpn_provider *provider;
2257 provider = g_hash_table_lookup(provider_hash, identifier);
2261 provider = vpn_provider_new();
2265 DBG("provider %p", provider);
2267 provider->identifier = g_strdup(identifier);
2269 g_hash_table_insert(provider_hash, provider->identifier, provider);
2271 configuration_count_add();
2276 static void vpn_provider_put(const char *identifier)
2278 configuration_count_del();
2280 g_hash_table_remove(provider_hash, identifier);
2283 static void provider_dbus_ident(char *ident)
2285 int i, len = strlen(ident);
2287 for (i = 0; i < len; i++) {
2288 if (ident[i] >= '0' && ident[i] <= '9')
2290 if (ident[i] >= 'a' && ident[i] <= 'z')
2292 if (ident[i] >= 'A' && ident[i] <= 'Z')
2298 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
2301 struct vpn_provider *provider;
2303 if (!keyfile || !ident)
2306 provider = __vpn_provider_lookup(ident);
2308 provider = vpn_provider_get(ident);
2310 DBG("can not create provider");
2314 provider_load_from_keyfile(provider, keyfile);
2316 if (!provider->name || !provider->host ||
2317 !provider->domain) {
2318 DBG("cannot get name, host or domain");
2319 vpn_provider_unref(provider);
2323 if (!provider_register(provider)) {
2324 connection_register(provider);
2325 connection_added_signal(provider);
2332 static void provider_create_all_from_type(const char *provider_type)
2339 DBG("provider type %s", provider_type);
2341 providers = __connman_storage_get_providers();
2346 for (i = 0; providers[i]; i += 1) {
2348 if (strncmp(providers[i], "provider_", 9) != 0)
2351 id = providers[i] + 9;
2352 keyfile = __connman_storage_load_provider(id);
2357 type = __vpn_config_get_string(keyfile, id, "Type", NULL);
2359 DBG("keyfile %p id %s type %s", keyfile, id, type);
2361 if (strcmp(provider_type, type) != 0) {
2363 g_key_file_free(keyfile);
2367 if (!provider_create_from_keyfile(keyfile, id))
2368 DBG("could not create provider");
2371 g_key_file_free(keyfile);
2373 g_strfreev(providers);
2376 #if !defined TIZEN_EXT
2377 char *__vpn_provider_create_identifier(const char *host, const char *domain)
2382 ident = g_strdup_printf("%s_%s", host, domain);
2384 ident = g_strdup_printf("%s", host);
2386 provider_dbus_ident(ident);
2391 char *__vpn_provider_create_identifier(const char *host, const char *domain, const char *name)
2396 ident = g_strdup_printf("%s_%s_%s", host, domain, name);
2398 ident = g_strdup_printf("%s_%s", host, name);
2402 provider_dbus_ident(ident);
2408 int __vpn_provider_create(DBusMessage *msg)
2410 struct vpn_provider *provider;
2411 DBusMessageIter iter, array;
2412 const char *type = NULL, *name = NULL;
2413 const char *host = NULL, *domain = NULL;
2414 GSList *networks = NULL;
2417 dbus_bool_t split_routing = false;
2419 dbus_message_iter_init(msg, &iter);
2420 dbus_message_iter_recurse(&iter, &array);
2422 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
2423 DBusMessageIter entry, value;
2426 dbus_message_iter_recurse(&array, &entry);
2427 dbus_message_iter_get_basic(&entry, &key);
2429 dbus_message_iter_next(&entry);
2430 dbus_message_iter_recurse(&entry, &value);
2432 switch (dbus_message_iter_get_arg_type(&value)) {
2433 case DBUS_TYPE_STRING:
2434 if (g_str_equal(key, "Type"))
2435 dbus_message_iter_get_basic(&value, &type);
2436 else if (g_str_equal(key, "Name"))
2437 dbus_message_iter_get_basic(&value, &name);
2438 else if (g_str_equal(key, "Host"))
2439 dbus_message_iter_get_basic(&value, &host);
2440 else if (g_str_equal(key, "VPN.Domain") ||
2441 g_str_equal(key, "Domain"))
2442 dbus_message_iter_get_basic(&value, &domain);
2444 case DBUS_TYPE_BOOLEAN:
2445 if (g_str_equal(key, "SplitRouting"))
2446 dbus_message_iter_get_basic(&value,
2449 case DBUS_TYPE_ARRAY:
2450 if (g_str_equal(key, "UserRoutes"))
2451 networks = get_user_networks(&value);
2455 dbus_message_iter_next(&array);
2461 DBG("Type %s name %s networks %p", type, name, networks);
2466 #if !defined TIZEN_EXT
2467 ident = __vpn_provider_create_identifier(host, domain);
2469 ident = __vpn_provider_create_identifier(host, domain, name);
2471 DBG("ident %s", ident);
2473 provider = __vpn_provider_lookup(ident);
2475 provider = vpn_provider_get(ident);
2477 DBG("can not create provider");
2482 provider->host = g_strdup(host);
2483 provider->domain = g_strdup(domain);
2484 provider->name = g_strdup(name);
2485 provider->type = g_strdup(type);
2486 provider->do_split_routing = split_routing;
2488 if (provider_register(provider) == 0)
2489 vpn_provider_load(provider);
2491 provider_resolv_host_addr(provider);
2495 g_slist_free_full(provider->user_networks, free_route);
2496 provider->user_networks = networks;
2497 set_user_networks(provider, provider->user_networks);
2500 dbus_message_iter_init(msg, &iter);
2501 dbus_message_iter_recurse(&iter, &array);
2503 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
2504 DBusMessageIter entry, value;
2505 const char *key, *str;
2507 dbus_message_iter_recurse(&array, &entry);
2508 dbus_message_iter_get_basic(&entry, &key);
2510 dbus_message_iter_next(&entry);
2511 dbus_message_iter_recurse(&entry, &value);
2513 switch (dbus_message_iter_get_arg_type(&value)) {
2514 case DBUS_TYPE_STRING:
2515 dbus_message_iter_get_basic(&value, &str);
2516 vpn_provider_set_string(provider, key, str);
2520 dbus_message_iter_next(&array);
2525 vpn_provider_save(provider);
2527 err = provider_register(provider);
2528 if (err != 0 && err != -EALREADY)
2531 connection_register(provider);
2533 DBG("provider %p index %d path %s", provider, provider->index,
2536 g_dbus_send_reply(connection, msg,
2537 DBUS_TYPE_OBJECT_PATH, &provider->path,
2540 connection_added_signal(provider);
2545 static const char *get_string(GHashTable *settings, const char *key)
2547 DBG("settings %p key %s", settings, key);
2549 return g_hash_table_lookup(settings, key);
2552 static GSList *parse_user_networks(const char *network_str)
2554 GSList *networks = NULL;
2561 elems = g_strsplit(network_str, ",", 0);
2566 struct vpn_route *vpn_route;
2567 char *network, *netmask, *gateway;
2571 route = g_strsplit(elems[i], "/", 0);
2576 if (!network || network[0] == '\0')
2579 family = connman_inet_check_ipaddress(network);
2581 DBG("Cannot get address family of %s (%d/%s)", network,
2582 family, gai_strerror(family));
2593 DBG("Unsupported address family %d", family);
2598 if (!netmask || netmask[0] == '\0')
2603 vpn_route = g_try_new0(struct vpn_route, 1);
2609 vpn_route->family = family;
2610 vpn_route->network = g_strdup(network);
2611 vpn_route->netmask = g_strdup(netmask);
2612 vpn_route->gateway = g_strdup(gateway);
2614 DBG("route %s/%s%s%s", network, netmask,
2615 gateway ? " via " : "", gateway ? gateway : "");
2617 networks = g_slist_prepend(networks, vpn_route);
2626 return g_slist_reverse(networks);
2629 int __vpn_provider_create_from_config(GHashTable *settings,
2630 const char *config_ident,
2631 const char *config_entry)
2633 struct vpn_provider *provider;
2634 const char *type, *name, *host, *domain, *networks_str;
2637 GHashTableIter hash;
2638 gpointer value, key;
2641 type = get_string(settings, "Type");
2642 name = get_string(settings, "Name");
2643 host = get_string(settings, "Host");
2644 domain = get_string(settings, "Domain");
2645 networks_str = get_string(settings, "Networks");
2646 networks = parse_user_networks(networks_str);
2653 DBG("type %s name %s networks %s", type, name, networks_str);
2655 if (!type || !name) {
2660 #if !defined TIZEN_EXT
2661 ident = __vpn_provider_create_identifier(host, domain);
2663 ident = __vpn_provider_create_identifier(host, domain, name);
2665 DBG("ident %s", ident);
2667 provider = __vpn_provider_lookup(ident);
2669 provider = vpn_provider_get(ident);
2671 DBG("can not create provider");
2676 provider->host = g_strdup(host);
2677 provider->domain = g_strdup(domain);
2678 provider->name = g_strdup(name);
2679 provider->type = g_ascii_strdown(type, -1);
2681 provider->config_file = g_strdup(config_ident);
2682 provider->config_entry = g_strdup(config_entry);
2684 provider_resolv_host_addr(provider);
2688 g_slist_free_full(provider->user_networks, free_route);
2689 provider->user_networks = networks;
2690 set_user_networks(provider, provider->user_networks);
2693 g_hash_table_iter_init(&hash, settings);
2695 while (g_hash_table_iter_next(&hash, &key, &value))
2696 __vpn_provider_set_string_immutable(provider, key, value);
2698 provider->immutable = true;
2700 vpn_provider_save(provider);
2702 err = provider_register(provider);
2703 if (err != 0 && err != -EALREADY)
2706 connection_register(provider);
2708 DBG("provider %p index %d path %s", provider, provider->index,
2711 connection_added_signal(provider);
2718 vpn_provider_put(ident);
2720 g_slist_free_full(networks, free_route);
2725 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
2727 DBusMessageIter entry;
2728 GHashTableIter hash;
2729 gpointer value, key;
2731 g_hash_table_iter_init(&hash, provider_hash);
2733 while (g_hash_table_iter_next(&hash, &key, &value)) {
2734 struct vpn_provider *provider = value;
2736 #if defined TIZEN_EXT
2737 DBG("provider %p", provider);
2739 DBG("path %s", provider->path);
2742 if (!provider->identifier)
2745 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
2747 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2749 append_properties(&entry, provider);
2750 dbus_message_iter_close_container(iter, &entry);
2754 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
2760 reply = dbus_message_new_method_return(msg);
2764 __connman_dbus_append_objpath_dict_array(reply,
2765 append_connection_structs, NULL);
2770 const char *vpn_provider_get_ident(struct vpn_provider *provider)
2775 return provider->identifier;
2778 static int set_string(struct vpn_provider *provider,
2779 const char *key, const char *value,
2780 bool hide_value, bool immutable)
2782 DBG("provider %p key %s immutable %s value %s", provider, key,
2783 immutable ? "yes" : "no",
2784 hide_value ? "<not printed>" : value);
2786 if (g_str_equal(key, "Type")) {
2787 if (!g_strcmp0(provider->type, value))
2790 g_free(provider->type);
2791 provider->type = g_ascii_strdown(value, -1);
2792 send_value(provider->path, "Type", provider->type);
2793 } else if (g_str_equal(key, "Name")) {
2794 if (!g_strcmp0(provider->name, value))
2797 g_free(provider->name);
2798 provider->name = g_strdup(value);
2799 send_value(provider->path, "Name", provider->name);
2800 } else if (g_str_equal(key, "Host")) {
2801 if (!g_strcmp0(provider->host, value))
2804 g_free(provider->host);
2805 provider->host = g_strdup(value);
2806 send_value(provider->path, "Host", provider->host);
2807 } else if (g_str_equal(key, "VPN.Domain") ||
2808 g_str_equal(key, "Domain")) {
2809 if (!g_strcmp0(provider->domain, value))
2812 g_free(provider->domain);
2813 provider->domain = g_strdup(value);
2814 send_value(provider->path, "Domain", provider->domain);
2815 } else if (g_str_equal(key, "SplitRouting")) {
2816 connman_warn("VPN SplitRouting value attempted to set as "
2817 "string, is boolean");
2820 struct vpn_setting *setting;
2821 bool replace = true;
2823 setting = g_hash_table_lookup(provider->setting_strings, key);
2825 if (!immutable && setting->immutable) {
2826 DBG("Trying to set immutable variable %s", key);
2828 } else if (!g_strcmp0(setting->value, value)) {
2832 g_free(setting->value);
2835 setting = g_try_new0(struct vpn_setting, 1);
2840 setting->value = g_strdup(value);
2841 setting->hide_value = hide_value;
2844 setting->immutable = true;
2847 send_value(provider->path, key, setting->value);
2850 g_hash_table_replace(provider->setting_strings,
2851 g_strdup(key), setting);
2857 int vpn_provider_set_string(struct vpn_provider *provider,
2858 const char *key, const char *value)
2860 return set_string(provider, key, value, false, false);
2863 int vpn_provider_set_string_hide_value(struct vpn_provider *provider,
2864 const char *key, const char *value)
2866 return set_string(provider, key, value, true, false);
2869 int __vpn_provider_set_string_immutable(struct vpn_provider *provider,
2870 const char *key, const char *value)
2872 return set_string(provider, key, value, false, true);
2875 const char *vpn_provider_get_string(struct vpn_provider *provider,
2878 struct vpn_setting *setting;
2880 DBG("provider %p key %s", provider, key);
2882 if (g_str_equal(key, "Type"))
2883 return provider->type;
2884 else if (g_str_equal(key, "Name"))
2885 return provider->name;
2886 else if (g_str_equal(key, "Host"))
2887 return provider->host;
2888 else if (g_str_equal(key, "HostIP")) {
2889 if (!provider->host_ip ||
2890 !provider->host_ip[0])
2891 return provider->host;
2893 return provider->host_ip[0];
2894 } else if (g_str_equal(key, "VPN.Domain") ||
2895 g_str_equal(key, "Domain"))
2896 return provider->domain;
2898 setting = g_hash_table_lookup(provider->setting_strings, key);
2902 return setting->value;
2905 int vpn_provider_set_boolean(struct vpn_provider *provider, const char *key,
2906 bool value, bool force_change)
2908 DBG("provider %p key %s", provider, key);
2910 if (g_str_equal(key, "SplitRouting")) {
2911 if (provider->do_split_routing == value && !force_change)
2914 DBG("SplitRouting set to %s", bool2str(value));
2916 provider->do_split_routing = value;
2917 send_value_boolean(provider->path, key,
2918 provider->do_split_routing);
2924 bool vpn_provider_get_boolean(struct vpn_provider *provider, const char *key,
2927 struct vpn_setting *setting;
2929 connman_info("provider %p key %s", provider, key);
2931 setting = g_hash_table_lookup(provider->setting_strings, key);
2932 if (!setting || !setting->value)
2933 return default_value;
2935 if (!g_strcmp0(setting->value, "true"))
2938 if (!g_strcmp0(setting->value, "false"))
2941 return default_value;
2944 bool vpn_provider_get_string_immutable(struct vpn_provider *provider,
2947 struct vpn_setting *setting;
2949 /* These values can be changed if the provider is not immutable */
2950 if (g_str_equal(key, "Type")) {
2951 return provider->immutable;
2952 } else if (g_str_equal(key, "Name")) {
2953 return provider->immutable;
2954 } else if (g_str_equal(key, "Host")) {
2955 return provider->immutable;
2956 } else if (g_str_equal(key, "HostIP")) {
2957 return provider->immutable;
2958 } else if (g_str_equal(key, "VPN.Domain") ||
2959 g_str_equal(key, "Domain")) {
2960 return provider->immutable;
2963 setting = g_hash_table_lookup(provider->setting_strings, key);
2965 return true; /* Not found, regard as immutable - no changes */
2967 return setting->immutable;
2970 bool vpn_provider_setting_key_exists(struct vpn_provider *provider,
2973 return g_hash_table_contains(provider->setting_strings, key);
2976 void vpn_provider_set_auth_error_limit(struct vpn_provider *provider,
2982 provider->auth_error_limit = limit;
2985 bool __vpn_provider_check_routes(struct vpn_provider *provider)
2990 if (provider->user_routes &&
2991 g_hash_table_size(provider->user_routes) > 0)
2994 if (provider->routes &&
2995 g_hash_table_size(provider->routes) > 0)
3001 void *vpn_provider_get_data(struct vpn_provider *provider)
3003 return provider->driver_data;
3006 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
3008 provider->driver_data = data;
3011 void *vpn_provider_get_plugin_data(struct vpn_provider *provider)
3013 #if defined TIZEN_EXT
3017 return provider->plugin_data;
3020 void vpn_provider_set_plugin_data(struct vpn_provider *provider, void *data)
3022 provider->plugin_data = data;
3025 void vpn_provider_set_index(struct vpn_provider *provider, int index)
3027 DBG("index %d provider %p", index, provider);
3029 if (!provider->ipconfig_ipv4) {
3030 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
3032 if (!provider->ipconfig_ipv4) {
3033 DBG("Couldn't create ipconfig for IPv4");
3038 __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
3040 if (!provider->ipconfig_ipv6) {
3041 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
3043 if (!provider->ipconfig_ipv6) {
3044 DBG("Couldn't create ipconfig for IPv6");
3049 __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
3052 provider->index = index;
3055 int vpn_provider_get_index(struct vpn_provider *provider)
3057 return provider->index;
3060 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
3061 struct connman_ipaddress *ipaddress)
3063 struct vpn_ipconfig *ipconfig = NULL;
3065 switch (ipaddress->family) {
3067 ipconfig = provider->ipconfig_ipv4;
3070 ipconfig = provider->ipconfig_ipv6;
3076 DBG("provider %p state %d ipconfig %p family %d", provider,
3077 provider->state, ipconfig, ipaddress->family);
3082 provider->family = ipaddress->family;
3084 if (provider->state == VPN_PROVIDER_STATE_CONNECT ||
3085 provider->state == VPN_PROVIDER_STATE_READY) {
3086 struct connman_ipaddress *addr =
3087 __vpn_ipconfig_get_address(ipconfig);
3090 * Remember the old address so that we can remove it in notify
3091 * function in plugins/vpn.c if we ever restart
3093 if (ipaddress->family == AF_INET6) {
3094 connman_ipaddress_free(provider->prev_ipv6_addr);
3095 provider->prev_ipv6_addr =
3096 connman_ipaddress_copy(addr);
3098 connman_ipaddress_free(provider->prev_ipv4_addr);
3099 provider->prev_ipv4_addr =
3100 connman_ipaddress_copy(addr);
3104 if (ipaddress->local) {
3105 __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
3106 __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
3107 __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
3108 __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
3109 __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
3115 int vpn_provider_set_pac(struct vpn_provider *provider,
3118 DBG("provider %p pac %s", provider, pac);
3124 int vpn_provider_set_domain(struct vpn_provider *provider,
3127 DBG("provider %p domain %s", provider, domain);
3129 g_free(provider->domain);
3130 provider->domain = g_strdup(domain);
3135 int vpn_provider_set_nameservers(struct vpn_provider *provider,
3136 const char *nameservers)
3138 DBG("provider %p nameservers %s", provider, nameservers);
3140 g_strfreev(provider->nameservers);
3141 provider->nameservers = NULL;
3146 provider->nameservers = g_strsplit_set(nameservers, ", ", 0);
3151 static int route_env_parse(struct vpn_provider *provider, const char *key,
3152 int *family, unsigned long *idx,
3153 enum vpn_provider_route_type *type)
3158 DBG("name %s", provider->name);
3160 if (provider->driver && provider->driver->route_env_parse)
3161 return provider->driver->route_env_parse(provider, key, family, idx,
3167 int vpn_provider_append_route(struct vpn_provider *provider,
3168 const char *key, const char *value)
3170 struct vpn_route *route;
3171 int ret, family = 0;
3172 unsigned long idx = 0;
3173 enum vpn_provider_route_type type = VPN_PROVIDER_ROUTE_TYPE_NONE;
3175 DBG("key %s value %s", key, value);
3177 ret = route_env_parse(provider, key, &family, &idx, &type);
3181 DBG("idx %lu family %d type %d", idx, family, type);
3183 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
3185 route = g_try_new0(struct vpn_route, 1);
3187 connman_error("out of memory");
3191 route->family = family;
3193 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
3198 case VPN_PROVIDER_ROUTE_TYPE_NONE:
3200 case VPN_PROVIDER_ROUTE_TYPE_MASK:
3201 route->netmask = g_strdup(value);
3203 case VPN_PROVIDER_ROUTE_TYPE_ADDR:
3204 route->network = g_strdup(value);
3206 case VPN_PROVIDER_ROUTE_TYPE_GW:
3207 route->gateway = g_strdup(value);
3211 if (route->netmask && route->gateway && route->network)
3212 provider_schedule_changed(provider);
3217 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
3219 if (!provider->driver)
3222 return provider->driver->name;
3225 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
3227 return provider->identifier;
3230 static gint compare_priority(gconstpointer a, gconstpointer b)
3235 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
3237 struct vpn_provider *provider = value;
3239 if (provider->driver && provider->driver->remove)
3240 provider->driver->remove(provider);
3242 connection_unregister(provider);
3245 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
3247 DBG("driver %p name %s", driver, driver->name);
3249 driver_list = g_slist_insert_sorted(driver_list, driver,
3251 provider_create_all_from_type(driver->name);
3255 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
3257 GHashTableIter iter;
3258 gpointer value, key;
3260 DBG("driver %p name %s", driver, driver->name);
3262 driver_list = g_slist_remove(driver_list, driver);
3264 g_hash_table_iter_init(&iter, provider_hash);
3265 while (g_hash_table_iter_next(&iter, &key, &value)) {
3266 struct vpn_provider *provider = value;
3268 if (provider && provider->driver &&
3269 g_strcmp0(provider->driver->name,
3270 driver->name) == 0) {
3272 * Cancel VPN agent request to avoid segfault at
3273 * shutdown as the callback, if set can point to a
3274 * function in the plugin that is to be removed.
3276 connman_agent_cancel(provider);
3277 provider->driver = NULL;
3282 const char *vpn_provider_get_name(struct vpn_provider *provider)
3284 return provider->name;
3287 const char *vpn_provider_get_host(struct vpn_provider *provider)
3289 return provider->host;
3292 const char *vpn_provider_get_path(struct vpn_provider *provider)
3294 return provider->path;
3298 * This crude heuristic is meant to mitigate an issue with certain VPN
3299 * providers that allow only one authentication per account at a time. These
3300 * providers require the VPN client shuts down cleanly by sending an exit
3301 * notification. In many cases this is not possible as the transport may
3302 * already be gone and there is no route to the VPN server. In such case server
3303 * may return authentication error to indicate that an other client is active
3304 * and reserves the slot.
3306 * By allowing the VPN client to try again with the following conditons the
3307 * unnecessary credential resets done by VPN agent can be avoided. VPN client
3308 * is allowed to retry if 1) there was a successful connection to the server
3309 * within the specified CONNECT_OK_DIFF time and 2) the provider specific
3310 * limit for auth errors is not reached the unnecessary credential resets in
3311 * this case are avoided.
3313 * This feature is controlled by the provider specific value for
3314 * "AuthErrorLimit". Setting the value to 0 feature is disabled. User defined
3315 * value is preferred if set, otherwise plugin default set with
3316 * vpn_provider_set_auth_error_limit() is used, which defaults to
3317 * AUTH_ERROR_LIMIT_DEFAULT.
3319 static bool ignore_authentication_errors(struct vpn_provider *provider)
3326 val = vpn_provider_get_string(provider, "AuthErrorLimit");
3328 limit = g_ascii_strtoull(val, NULL, 10);
3330 limit = provider->auth_error_limit;
3332 if (!limit || !provider->previous_connect_time) {
3333 DBG("%p errors %u %s", provider, provider->auth_error_counter,
3335 "disabled by 0 limit" :
3336 "no previous ok conn");
3340 uptime = get_uptime();
3341 diff = uptime - provider->previous_connect_time;
3343 DBG("%p errors %u limit %u uptime %jd time diff %jd", provider,
3344 provider->auth_error_counter, limit,
3345 (intmax_t)uptime, (intmax_t)diff);
3347 if (diff <= CONNECT_OK_DIFF && provider->auth_error_counter <= limit) {
3348 DBG("ignore auth errors");
3355 unsigned int vpn_provider_get_authentication_errors(
3356 struct vpn_provider *provider)
3358 if (ignore_authentication_errors(provider))
3361 return provider->auth_error_counter;
3364 unsigned int vpn_provider_get_connection_errors(
3365 struct vpn_provider *provider)
3367 return provider->conn_error_counter;
3370 void vpn_provider_change_address(struct vpn_provider *provider)
3372 switch (provider->family) {
3374 connman_inet_set_address(provider->index,
3375 __vpn_ipconfig_get_address(provider->ipconfig_ipv4));
3378 connman_inet_set_ipv6_address(provider->index,
3379 __vpn_ipconfig_get_address(provider->ipconfig_ipv6));
3386 void vpn_provider_clear_address(struct vpn_provider *provider, int family)
3388 const char *address;
3391 DBG("provider %p family %d ipv4 %p ipv6 %p", provider, family,
3392 provider->prev_ipv4_addr, provider->prev_ipv6_addr);
3396 if (provider->prev_ipv4_addr) {
3397 connman_ipaddress_get_ip(provider->prev_ipv4_addr,
3400 DBG("ipv4 %s/%d", address, len);
3402 connman_inet_clear_address(provider->index,
3403 provider->prev_ipv4_addr);
3404 connman_ipaddress_free(provider->prev_ipv4_addr);
3405 provider->prev_ipv4_addr = NULL;
3409 if (provider->prev_ipv6_addr) {
3410 connman_ipaddress_get_ip(provider->prev_ipv6_addr,
3413 DBG("ipv6 %s/%d", address, len);
3415 connman_inet_clear_ipv6_address(provider->index,
3416 provider->prev_ipv6_addr);
3418 connman_ipaddress_free(provider->prev_ipv6_addr);
3419 provider->prev_ipv6_addr = NULL;
3427 static int agent_probe(struct connman_agent *agent)
3429 DBG("agent %p", agent);
3433 static void agent_remove(struct connman_agent *agent)
3435 DBG("agent %p", agent);
3438 static struct connman_agent_driver agent_driver = {
3440 .interface = VPN_AGENT_INTERFACE,
3441 .probe = agent_probe,
3442 .remove = agent_remove,
3445 static void remove_unprovisioned_providers(void)
3448 GKeyFile *keyfile, *configkeyfile;
3449 char *file, *section;
3452 providers = __connman_storage_get_providers();
3456 for (; providers[i]; i++) {
3457 char *group = providers[i] + sizeof("provider_") - 1;
3458 file = section = NULL;
3459 keyfile = configkeyfile = NULL;
3461 keyfile = __connman_storage_load_provider(group);
3465 file = __vpn_config_get_string(keyfile, group,
3466 "Config.file", NULL);
3470 section = __vpn_config_get_string(keyfile, group,
3471 "Config.ident", NULL);
3475 configkeyfile = __connman_storage_load_provider_config(file);
3476 if (!configkeyfile) {
3478 * Config file is missing, remove the provisioned
3481 __connman_storage_remove_provider(group);
3485 if (!g_key_file_has_group(configkeyfile, section))
3487 * Config section is missing, remove the provisioned
3490 __connman_storage_remove_provider(group);
3494 g_key_file_free(keyfile);
3497 g_key_file_free(configkeyfile);
3503 g_strfreev(providers);
3506 static gboolean connman_property_changed(DBusConnection *conn,
3507 DBusMessage *message,
3510 DBusMessageIter iter, value;
3512 const char *signature = DBUS_TYPE_STRING_AS_STRING
3513 DBUS_TYPE_VARIANT_AS_STRING;
3515 if (!dbus_message_has_signature(message, signature)) {
3516 connman_error("vpn connman property signature \"%s\" "
3517 "does not match expected \"%s\"",
3518 dbus_message_get_signature(message),
3523 if (!dbus_message_iter_init(message, &iter))
3526 dbus_message_iter_get_basic(&iter, &key);
3528 dbus_message_iter_next(&iter);
3529 dbus_message_iter_recurse(&iter, &value);
3533 if (g_str_equal(key, "State")) {
3536 if (dbus_message_iter_get_arg_type(&value) != DBUS_TYPE_STRING)
3539 dbus_message_iter_get_basic(&value, &str);
3546 static void get_connman_state_reply(DBusPendingCall *call, void *user_data)
3548 DBusMessage *reply = NULL;
3550 DBusMessageIter iter, array, dict, value;
3552 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
3553 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
3554 DBUS_TYPE_STRING_AS_STRING
3555 DBUS_TYPE_VARIANT_AS_STRING
3556 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
3563 if (!dbus_pending_call_get_completed(call))
3566 reply = dbus_pending_call_steal_reply(call);
3568 dbus_error_init(&error);
3570 if (dbus_set_error_from_message(&error, reply)) {
3571 connman_error("%s", error.message);
3574 * In case of timeout re-add the state function to main
3577 if (g_ascii_strcasecmp(error.name, DBUS_ERROR_TIMEOUT) == 0) {
3578 DBG("D-Bus timeout, re-add get_connman_state()");
3579 get_connman_state();
3581 dbus_error_free(&error);
3586 if (!dbus_message_has_signature(reply, signature)) {
3587 connman_error("vpnd signature \"%s\" does not match "
3589 dbus_message_get_signature(reply),
3595 if (!dbus_message_iter_init(reply, &array))
3598 dbus_message_iter_recurse(&array, &dict);
3600 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
3601 dbus_message_iter_recurse(&dict, &iter);
3603 dbus_message_iter_get_basic(&iter, &key);
3605 dbus_message_iter_next(&iter);
3606 dbus_message_iter_recurse(&iter, &value);
3608 if (g_ascii_strcasecmp(key, "State") == 0 &&
3609 dbus_message_iter_get_arg_type(&value) ==
3611 dbus_message_iter_get_basic(&value, &str);
3613 DBG("Got initial state %s", str);
3617 /* No need to process further */
3621 dbus_message_iter_next(&dict);
3624 state_query_completed = true;
3628 dbus_message_unref(reply);
3631 dbus_pending_call_unref(call);
3634 static gboolean run_get_connman_state(gpointer user_data)
3636 const char *path = "/";
3637 const char *method = "GetProperties";
3638 gboolean rval = FALSE;
3640 DBusMessage *msg = NULL;
3641 DBusPendingCall *call = NULL;
3645 msg = dbus_message_new_method_call(CONNMAN_SERVICE, path,
3646 CONNMAN_MANAGER_INTERFACE, method);
3650 rval = g_dbus_send_message_with_reply(connection, msg, &call, -1);
3652 connman_error("Cannot call %s on %s", method,
3653 CONNMAN_MANAGER_INTERFACE);
3658 connman_error("set pending call failed");
3663 if (!dbus_pending_call_set_notify(call, get_connman_state_reply,
3665 connman_error("set notify to pending call failed");
3668 dbus_pending_call_unref(call);
3675 dbus_message_unref(msg);
3677 /* In case sending was success, unset timeout function id */
3679 DBG("unsetting get_connman_state_timeout id");
3680 get_connman_state_timeout = 0;
3683 /* Return FALSE in case of success to remove from main event loop */
3687 static void get_connman_state(void)
3689 if (get_connman_state_timeout)
3692 get_connman_state_timeout = g_timeout_add(STATE_INTERVAL_DEFAULT,
3693 run_get_connman_state, NULL);
3696 static void connman_service_watch_connected(DBusConnection *conn,
3701 get_connman_state();
3704 static void connman_service_watch_disconnected(DBusConnection *conn,
3709 set_state(CONNMAN_STATE_IDLE);
3711 /* Set state query variable to initial state */
3712 state_query_completed = false;
3715 int __vpn_provider_init(void)
3721 err = connman_agent_driver_register(&agent_driver);
3723 connman_error("Cannot register agent driver for %s",
3728 connection = connman_dbus_get_connection();
3730 remove_unprovisioned_providers();
3732 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3733 NULL, unregister_provider);
3735 connman_service_watch = g_dbus_add_service_watch(connection,
3737 connman_service_watch_connected,
3738 connman_service_watch_disconnected,
3741 connman_signal_watch = g_dbus_add_signal_watch(connection,
3742 CONNMAN_SERVICE, NULL,
3743 CONNMAN_MANAGER_INTERFACE,
3745 connman_property_changed,
3751 void __vpn_provider_cleanup(void)
3755 connman_agent_driver_unregister(&agent_driver);
3757 g_hash_table_foreach(provider_hash, clean_provider, NULL);
3759 g_hash_table_destroy(provider_hash);
3760 provider_hash = NULL;
3762 if (get_connman_state_timeout) {
3763 if (!g_source_remove(get_connman_state_timeout))
3764 connman_error("connman state timeout not removed");
3767 g_dbus_remove_watch(connection, connman_service_watch);
3768 g_dbus_remove_watch(connection, connman_signal_watch);
3770 dbus_connection_unref(connection);