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
28 #include <sys/socket.h>
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/technology.h>
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/dbus.h>
39 #include <connman/provider.h>
40 #include <connman/ipaddress.h>
41 #include <connman/vpn-dbus.h>
42 #include <connman/inet.h>
43 #include <gweb/gresolv.h>
45 #define DBUS_TIMEOUT 10000
47 static DBusConnection *connection;
49 static GHashTable *vpn_connections = NULL;
51 static guint added_watch;
52 static guint removed_watch;
53 static guint property_watch;
62 struct config_create_data {
63 connection_ready_cb callback;
68 struct connection_data {
71 struct connman_provider *provider;
73 DBusPendingCall *call;
75 struct config_create_data *cb_data;
86 GHashTable *server_routes;
87 GHashTable *user_routes;
88 GHashTable *setting_strings;
90 struct connman_ipaddress *ip;
96 static int set_string(struct connman_provider *provider,
97 const char *key, const char *value)
99 struct connection_data *data;
101 data = connman_provider_get_data(provider);
105 DBG("data %p provider %p key %s value %s", data, provider, key, value);
107 if (g_str_equal(key, "Type")) {
109 data->type = g_strdup(value);
110 } else if (g_str_equal(key, "Name")) {
112 data->name = g_strdup(value);
113 } else if (g_str_equal(key, "Host")) {
115 data->host = g_strdup(value);
116 } else if (g_str_equal(key, "VPN.Domain") ||
117 g_str_equal(key, "Domain")) {
118 g_free(data->domain);
119 data->domain = g_strdup(value);
121 g_hash_table_replace(data->setting_strings,
122 g_strdup(key), g_strdup(value));
126 static const char *get_string(struct connman_provider *provider,
129 struct connection_data *data;
131 data = connman_provider_get_data(provider);
135 DBG("data %p provider %p key %s", data, provider, key);
137 if (g_str_equal(key, "Type"))
139 else if (g_str_equal(key, "Name"))
141 else if (g_str_equal(key, "Host"))
143 else if (g_str_equal(key, "HostIP")) {
144 if (!data->host_ip ||
148 return data->host_ip[0];
149 } else if (g_str_equal(key, "VPN.Domain"))
152 return g_hash_table_lookup(data->setting_strings, key);
155 static char *get_ident(const char *path)
162 pos = strrchr(path, '/');
169 static void cancel_host_resolv(struct connection_data *data)
171 if (data->resolv_id != 0)
172 g_resolv_cancel_lookup(data->resolv, data->resolv_id);
176 g_resolv_unref(data->resolv);
180 static gboolean remove_resolv(gpointer user_data)
182 struct connection_data *data = user_data;
184 cancel_host_resolv(data);
189 static void resolv_result(GResolvResultStatus status,
190 char **results, gpointer user_data)
192 struct connection_data *data = user_data;
194 DBG("status %d", status);
196 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results &&
197 g_strv_length(results) > 0) {
198 g_strfreev(data->host_ip);
199 data->host_ip = g_strdupv(results);
203 * We cannot unref the resolver here as resolv struct is manipulated
204 * by gresolv.c after we return from this callback.
206 g_timeout_add_seconds(0, remove_resolv, data);
211 static void resolv_host_addr(struct connection_data *data)
216 if (connman_inet_check_ipaddress(data->host) > 0)
222 data->resolv = g_resolv_new(0);
224 DBG("Cannot resolv %s", data->host);
228 DBG("Trying to resolv %s", data->host);
230 data->resolv_id = g_resolv_lookup_hostname(data->resolv, data->host,
231 resolv_result, data);
234 static void free_config_cb_data(struct config_create_data *cb_data)
239 g_free(cb_data->path);
240 cb_data->path = NULL;
242 if (cb_data->message) {
243 dbus_message_unref(cb_data->message);
244 cb_data->message = NULL;
247 cb_data->callback = NULL;
252 static void set_provider_state(struct connection_data *data)
254 enum connman_provider_state state = CONNMAN_PROVIDER_STATE_UNKNOWN;
257 DBG("provider %p new state %s", data->provider, data->state);
259 if (g_str_equal(data->state, "ready")) {
260 state = CONNMAN_PROVIDER_STATE_READY;
262 } else if (g_str_equal(data->state, "configuration")) {
263 state = CONNMAN_PROVIDER_STATE_CONNECT;
264 } else if (g_str_equal(data->state, "idle")) {
265 state = CONNMAN_PROVIDER_STATE_IDLE;
266 } else if (g_str_equal(data->state, "disconnect")) {
268 state = CONNMAN_PROVIDER_STATE_DISCONNECT;
270 } else if (g_str_equal(data->state, "failure")) {
272 state = CONNMAN_PROVIDER_STATE_FAILURE;
276 connman_provider_set_state(data->provider, state);
281 data->cb_data->callback(data->cb_data->message,
284 connman_provider_set_state(data->provider, state);
286 free_config_cb_data(data->cb_data);
287 data->cb_data = NULL;
290 static int create_provider(struct connection_data *data, void *user_data)
292 struct connman_provider_driver *driver = user_data;
295 DBG("%s", data->path);
297 data->provider = connman_provider_get(data->ident);
301 DBG("provider %p name %s", data->provider, data->name);
303 connman_provider_set_data(data->provider, data);
304 connman_provider_set_driver(data->provider, driver);
306 err = connman_provider_create_service(data->provider);
308 connman_provider_set_immutable(data->provider, data->immutable);
309 if (g_str_equal(data->state, "ready")) {
310 connman_provider_set_index(data->provider,
313 connman_provider_set_ipaddress(data->provider,
317 set_provider_state(data);
323 static void destroy_route(gpointer user_data)
325 struct vpn_route *route = user_data;
327 g_free(route->network);
328 g_free(route->netmask);
329 g_free(route->gateway);
333 static struct connection_data *create_connection_data(const char *path)
335 struct connection_data *data;
337 data = g_try_new0(struct connection_data, 1);
341 DBG("path %s", path);
343 data->path = g_strdup(path);
344 data->ident = g_strdup(get_ident(path));
347 data->setting_strings = g_hash_table_new_full(g_str_hash,
348 g_str_equal, g_free, g_free);
350 data->server_routes = g_hash_table_new_full(g_direct_hash,
351 g_str_equal, g_free, destroy_route);
352 data->user_routes = g_hash_table_new_full(g_str_hash,
353 g_str_equal, g_free, destroy_route);
358 static int extract_ip(DBusMessageIter *array, int family,
359 struct connection_data *data)
361 DBusMessageIter dict;
362 char *address = NULL, *gateway = NULL, *netmask = NULL, *peer = NULL;
363 unsigned char prefix_len;
365 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
368 dbus_message_iter_recurse(array, &dict);
370 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
371 DBusMessageIter entry, value;
374 dbus_message_iter_recurse(&dict, &entry);
375 dbus_message_iter_get_basic(&entry, &key);
377 dbus_message_iter_next(&entry);
378 dbus_message_iter_recurse(&entry, &value);
380 if (g_str_equal(key, "Address")) {
381 dbus_message_iter_get_basic(&value, &address);
382 DBG("address %s", address);
383 } else if (g_str_equal(key, "Netmask")) {
384 dbus_message_iter_get_basic(&value, &netmask);
385 DBG("netmask %s", netmask);
386 } else if (g_str_equal(key, "PrefixLength")) {
387 dbus_message_iter_get_basic(&value, &netmask);
388 DBG("prefix length %s", netmask);
389 } else if (g_str_equal(key, "Peer")) {
390 dbus_message_iter_get_basic(&value, &peer);
391 DBG("peer %s", peer);
392 } else if (g_str_equal(key, "Gateway")) {
393 dbus_message_iter_get_basic(&value, &gateway);
394 DBG("gateway %s", gateway);
397 dbus_message_iter_next(&dict);
400 connman_ipaddress_free(data->ip);
401 data->ip = connman_ipaddress_alloc(family);
407 connman_ipaddress_set_ipv4(data->ip, address, netmask,
411 prefix_len = atoi(netmask);
412 connman_ipaddress_set_ipv6(data->ip, address, prefix_len,
419 connman_ipaddress_set_peer(data->ip, peer);
424 static int extract_nameservers(DBusMessageIter *array,
425 struct connection_data *data)
427 DBusMessageIter entry;
428 char **nameservers = NULL;
431 dbus_message_iter_recurse(array, &entry);
433 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
434 const char *nameserver;
436 dbus_message_iter_get_basic(&entry, &nameserver);
438 nameservers = g_try_renew(char *, nameservers, i + 2);
442 DBG("[%d] %s", i, nameserver);
444 nameservers[i] = g_strdup(nameserver);
448 nameservers[++i] = NULL;
450 dbus_message_iter_next(&entry);
453 g_strfreev(data->nameservers);
454 data->nameservers = nameservers;
459 static int errorstr2val(const char *error) {
460 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".InProgress") == 0)
463 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".AlreadyConnected") == 0)
466 return -ECONNREFUSED;
469 static void connect_reply(DBusPendingCall *call, void *user_data)
473 struct connection_data *data = user_data;
474 struct config_create_data *cb_data = data->cb_data;
476 if (!dbus_pending_call_get_completed(call))
479 DBG("user_data %p path %s", user_data, cb_data ? cb_data->path : NULL);
481 reply = dbus_pending_call_steal_reply(call);
483 dbus_error_init(&error);
485 if (dbus_set_error_from_message(&error, reply)) {
486 int err = errorstr2val(error.name);
487 if (err != -EINPROGRESS) {
488 connman_error("Connect reply: %s (%s)", error.message,
490 dbus_error_free(&error);
492 DBG("data %p cb_data %p", data, cb_data);
494 cb_data->callback(cb_data->message, err, NULL);
495 free_config_cb_data(cb_data);
496 data->cb_data = NULL;
500 dbus_error_free(&error);
504 * The vpn connection is up when we get a "ready" state
505 * property so at this point we do nothing for the provider
510 dbus_message_unref(reply);
512 dbus_pending_call_unref(call);
515 static int connect_provider(struct connection_data *data, void *user_data)
517 DBusPendingCall *call;
518 DBusMessage *message;
519 struct config_create_data *cb_data = user_data;
521 DBG("data %p user %p path %s", data, cb_data, data->path);
523 data->connect_pending = false;
525 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
526 VPN_CONNECTION_INTERFACE,
531 if (!dbus_connection_send_with_reply(connection, message,
532 &call, DBUS_TIMEOUT)) {
533 connman_error("Unable to call %s.%s()",
534 VPN_CONNECTION_INTERFACE, VPN_CONNECT);
535 dbus_message_unref(message);
540 dbus_message_unref(message);
545 g_free(cb_data->path);
546 cb_data->path = g_strdup(data->path);
549 dbus_pending_call_set_notify(call, connect_reply, data, NULL);
551 dbus_message_unref(message);
556 static void add_connection(const char *path, DBusMessageIter *properties,
559 struct connection_data *data;
561 char *ident = get_ident(path);
564 data = g_hash_table_lookup(vpn_connections, ident);
567 * We might have a dummy connection struct here that
568 * was created by configuration_create_reply() so in
569 * that case just continue.
571 if (!data->connect_pending)
576 data = create_connection_data(path);
581 DBG("data %p path %s", data, path);
583 while (dbus_message_iter_get_arg_type(properties) ==
584 DBUS_TYPE_DICT_ENTRY) {
585 DBusMessageIter entry, value;
589 dbus_message_iter_recurse(properties, &entry);
590 dbus_message_iter_get_basic(&entry, &key);
592 dbus_message_iter_next(&entry);
593 dbus_message_iter_recurse(&entry, &value);
595 if (g_str_equal(key, "State")) {
596 dbus_message_iter_get_basic(&value, &str);
597 DBG("state %s -> %s", data->state, str);
598 data->state = g_strdup(str);
599 } else if (g_str_equal(key, "IPv4")) {
600 extract_ip(&value, AF_INET, data);
601 } else if (g_str_equal(key, "IPv6")) {
602 extract_ip(&value, AF_INET6, data);
603 } else if (g_str_equal(key, "Name")) {
604 dbus_message_iter_get_basic(&value, &str);
605 data->name = g_strdup(str);
606 } else if (g_str_equal(key, "Type")) {
607 dbus_message_iter_get_basic(&value, &str);
608 data->type = g_strdup(str);
609 } else if (g_str_equal(key, "Immutable")) {
610 dbus_bool_t immutable;
612 dbus_message_iter_get_basic(&value, &immutable);
613 data->immutable = immutable;
614 } else if (g_str_equal(key, "Host")) {
615 dbus_message_iter_get_basic(&value, &str);
616 data->host = g_strdup(str);
617 } else if (g_str_equal(key, "Domain")) {
618 dbus_message_iter_get_basic(&value, &str);
619 g_free(data->domain);
620 data->domain = g_strdup(str);
621 } else if (g_str_equal(key, "Nameservers")) {
622 extract_nameservers(&value, data);
623 } else if (g_str_equal(key, "Index")) {
624 dbus_message_iter_get_basic(&value, &data->index);
625 } else if (g_str_equal(key, "ServerRoutes")) {
627 } else if (g_str_equal(key, "UserRoutes")) {
630 if (dbus_message_iter_get_arg_type(&value) ==
632 dbus_message_iter_get_basic(&value, &str);
633 g_hash_table_replace(data->setting_strings,
634 g_strdup(key), g_strdup(str));
636 DBG("unknown key %s", key);
640 dbus_message_iter_next(properties);
644 g_hash_table_insert(vpn_connections, g_strdup(data->ident),
647 err = create_provider(data, user_data);
651 resolv_host_addr(data);
653 if (data->nameservers)
654 connman_provider_set_nameservers(data->provider,
658 connman_provider_set_domain(data->provider,
661 if (data->connect_pending)
662 connect_provider(data, data->cb_data);
667 DBG("removing %s", data->ident);
668 g_hash_table_remove(vpn_connections, data->ident);
671 static void get_connections_reply(DBusPendingCall *call, void *user_data)
675 DBusMessageIter array, dict;
676 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
677 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
678 DBUS_TYPE_OBJECT_PATH_AS_STRING
679 DBUS_TYPE_ARRAY_AS_STRING
680 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
681 DBUS_TYPE_STRING_AS_STRING
682 DBUS_TYPE_VARIANT_AS_STRING
683 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
684 DBUS_STRUCT_END_CHAR_AS_STRING;
686 if (!dbus_pending_call_get_completed(call))
691 reply = dbus_pending_call_steal_reply(call);
693 dbus_error_init(&error);
695 if (dbus_set_error_from_message(&error, reply)) {
696 connman_error("%s", error.message);
697 dbus_error_free(&error);
701 if (!dbus_message_has_signature(reply, signature)) {
702 connman_error("vpnd signature \"%s\" does not match "
704 dbus_message_get_signature(reply), signature);
708 if (!dbus_message_iter_init(reply, &array))
711 dbus_message_iter_recurse(&array, &dict);
713 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
714 DBusMessageIter value, properties;
717 dbus_message_iter_recurse(&dict, &value);
718 dbus_message_iter_get_basic(&value, &path);
720 dbus_message_iter_next(&value);
721 dbus_message_iter_recurse(&value, &properties);
723 add_connection(path, &properties, user_data);
725 dbus_message_iter_next(&dict);
729 dbus_message_unref(reply);
731 dbus_pending_call_unref(call);
734 static int get_connections(void *user_data)
736 DBusPendingCall *call;
737 DBusMessage *message;
741 message = dbus_message_new_method_call(VPN_SERVICE, "/",
742 VPN_MANAGER_INTERFACE,
747 if (!dbus_connection_send_with_reply(connection, message,
748 &call, DBUS_TIMEOUT)) {
749 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
751 dbus_message_unref(message);
756 dbus_message_unref(message);
760 dbus_pending_call_set_notify(call, get_connections_reply,
763 dbus_message_unref(message);
768 static int provider_probe(struct connman_provider *provider)
773 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
778 if (!dbus_pending_call_get_completed(call))
783 reply = dbus_pending_call_steal_reply(call);
785 dbus_error_init(&error);
787 if (dbus_set_error_from_message(&error, reply)) {
789 * If the returned error is NotFound, it means that we
790 * have actually removed the provider in vpnd already.
792 if (!dbus_error_has_name(&error,
793 CONNMAN_ERROR_INTERFACE".NotFound"))
794 connman_error("%s", error.message);
796 dbus_error_free(&error);
799 dbus_message_unref(reply);
801 dbus_pending_call_unref(call);
804 static int provider_remove(struct connman_provider *provider)
806 DBusPendingCall *call;
807 DBusMessage *message;
808 struct connection_data *data;
810 data = connman_provider_get_data(provider);
812 DBG("provider %p data %p", provider, data);
816 * This means the provider is already removed,
817 * just ignore the dbus in this case.
823 * When provider.c:provider_remove() calls this function,
824 * it will remove the provider itself after the call.
825 * This means that we cannot use the provider pointer later
826 * as it is no longer valid.
828 data->provider = NULL;
830 message = dbus_message_new_method_call(VPN_SERVICE, "/",
831 VPN_MANAGER_INTERFACE,
836 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
839 if (!dbus_connection_send_with_reply(connection, message,
840 &call, DBUS_TIMEOUT)) {
841 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
843 dbus_message_unref(message);
848 dbus_message_unref(message);
852 dbus_pending_call_set_notify(call, remove_connection_reply,
855 dbus_message_unref(message);
860 static int provider_connect(struct connman_provider *provider)
862 struct connection_data *data;
864 data = connman_provider_get_data(provider);
868 return connect_provider(data, NULL);
871 static void disconnect_reply(DBusPendingCall *call, void *user_data)
876 if (!dbus_pending_call_get_completed(call))
879 DBG("user %p", user_data);
881 reply = dbus_pending_call_steal_reply(call);
883 dbus_error_init(&error);
885 if (dbus_set_error_from_message(&error, reply)) {
886 connman_error("%s", error.message);
887 dbus_error_free(&error);
892 dbus_message_unref(reply);
894 dbus_pending_call_unref(call);
897 static int disconnect_provider(struct connection_data *data)
899 DBusPendingCall *call;
900 DBusMessage *message;
902 DBG("data %p path %s", data, data->path);
904 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
905 VPN_CONNECTION_INTERFACE,
910 if (!dbus_connection_send_with_reply(connection, message,
911 &call, DBUS_TIMEOUT)) {
912 connman_error("Unable to call %s.%s()",
913 VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
914 dbus_message_unref(message);
919 dbus_message_unref(message);
923 dbus_pending_call_set_notify(call, disconnect_reply, NULL, NULL);
925 dbus_message_unref(message);
927 connman_provider_set_state(data->provider,
928 CONNMAN_PROVIDER_STATE_DISCONNECT);
930 * We return 0 here instead of -EINPROGRESS because
931 * __connman_service_disconnect() needs to return something
932 * to gdbus so that gdbus will not call Disconnect() more
933 * than once. This way we do not need to pass the dbus reply
934 * message around the code.
939 static int provider_disconnect(struct connman_provider *provider)
941 struct connection_data *data;
943 DBG("provider %p", provider);
945 data = connman_provider_get_data(provider);
949 if (g_str_equal(data->state, "ready") ||
950 g_str_equal(data->state, "configuration"))
951 return disconnect_provider(data);
956 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
960 DBusMessageIter iter;
961 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
964 struct connection_data *data;
965 struct config_create_data *cb_data = user_data;
967 if (!dbus_pending_call_get_completed(call))
970 DBG("user %p", cb_data);
972 reply = dbus_pending_call_steal_reply(call);
974 dbus_error_init(&error);
976 if (dbus_set_error_from_message(&error, reply)) {
977 connman_error("dbus error: %s", error.message);
978 dbus_error_free(&error);
982 if (!dbus_message_has_signature(reply, signature)) {
983 connman_error("vpn configuration signature \"%s\" does not "
984 "match expected \"%s\"",
985 dbus_message_get_signature(reply), signature);
989 if (!dbus_message_iter_init(reply, &iter))
992 dbus_message_iter_get_basic(&iter, &path);
995 * Then try to connect the VPN as expected by ConnectProvider API
997 ident = get_ident(path);
999 data = g_hash_table_lookup(vpn_connections, ident);
1002 * Someone removed the data. We cannot really continue.
1004 DBG("Pending data not found for %s, cannot continue!", ident);
1007 data->connect_pending = true;
1010 data->cb_data = cb_data;
1012 DBG("Connection callback data already in use!");
1015 * Connection is created in add_connections() after
1016 * we have received the ConnectionAdded signal.
1019 DBG("cb %p msg %p", data->cb_data,
1020 data->cb_data ? data->cb_data->message : NULL);
1024 dbus_message_unref(reply);
1026 dbus_pending_call_unref(call);
1029 static void set_dbus_ident(char *ident)
1031 int i, len = strlen(ident);
1033 for (i = 0; i < len; i++) {
1034 if (ident[i] >= '0' && ident[i] <= '9')
1036 if (ident[i] >= 'a' && ident[i] <= 'z')
1038 if (ident[i] >= 'A' && ident[i] <= 'Z')
1044 static struct vpn_route *parse_user_route(const char *user_route)
1046 char *network, *netmask;
1047 struct vpn_route *route = NULL;
1048 int family = PF_UNSPEC;
1049 char **elems = g_strsplit(user_route, "/", 0);
1055 if (!network || *network == '\0') {
1056 DBG("no network/netmask set");
1061 if (netmask && *netmask == '\0') {
1062 DBG("no netmask set");
1066 if (g_strrstr(network, ":"))
1068 else if (g_strrstr(network, ".")) {
1071 if (!g_strrstr(netmask, ".")) {
1072 /* We have netmask length */
1074 struct in_addr netmask_in;
1075 unsigned char prefix_len = 32;
1079 long int value = strtol(netmask, &ptr, 10);
1080 if (ptr != netmask && *ptr == '\0' &&
1081 value && value <= 32)
1085 addr = 0xffffffff << (32 - prefix_len);
1086 netmask_in.s_addr = htonl(addr);
1087 netmask = inet_ntoa(netmask_in);
1089 DBG("network %s netmask %s", network, netmask);
1093 route = g_try_new(struct vpn_route, 1);
1097 route->network = g_strdup(network);
1098 route->netmask = g_strdup(netmask);
1099 route->gateway = NULL;
1100 route->family = family;
1107 static GSList *get_user_networks(DBusMessageIter *array)
1109 DBusMessageIter entry;
1110 GSList *list = NULL;
1112 dbus_message_iter_recurse(array, &entry);
1114 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1116 struct vpn_route *route;
1118 dbus_message_iter_get_basic(&entry, &val);
1120 route = parse_user_route(val);
1122 list = g_slist_prepend(list, route);
1124 dbus_message_iter_next(&entry);
1130 static void append_route(DBusMessageIter *iter, void *user_data)
1132 struct vpn_route *route = user_data;
1133 DBusMessageIter item;
1136 connman_dbus_dict_open(iter, &item);
1141 if (route->family == AF_INET)
1143 else if (route->family == AF_INET6)
1147 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1148 DBUS_TYPE_INT32, &family);
1151 connman_dbus_dict_append_basic(&item, "Network",
1152 DBUS_TYPE_STRING, &route->network);
1155 connman_dbus_dict_append_basic(&item, "Netmask",
1156 DBUS_TYPE_STRING, &route->netmask);
1159 connman_dbus_dict_append_basic(&item, "Gateway",
1160 DBUS_TYPE_STRING, &route->gateway);
1163 connman_dbus_dict_close(iter, &item);
1166 static void append_routes(DBusMessageIter *iter, void *user_data)
1168 GSList *list, *routes = user_data;
1170 DBG("routes %p", routes);
1172 for (list = routes; list; list = g_slist_next(list)) {
1173 DBusMessageIter dict;
1174 struct vpn_route *route = list->data;
1176 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1178 append_route(&dict, route);
1179 dbus_message_iter_close_container(iter, &dict);
1183 static int create_configuration(DBusMessage *msg, connection_ready_cb callback)
1185 DBusMessage *new_msg = NULL;
1186 DBusPendingCall *call;
1187 DBusMessageIter iter, array, new_iter, new_dict;
1188 const char *type = NULL, *name = NULL;
1189 const char *host = NULL, *domain = NULL;
1190 char *ident, *me = NULL;
1193 struct connection_data *data;
1194 struct config_create_data *user_data = NULL;
1195 GSList *networks = NULL;
1198 * We copy the old message data into new message. We cannot
1199 * just use the old message as is because the user route
1200 * information is not in the same format in vpnd.
1202 new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1203 dbus_message_iter_init_append(new_msg, &new_iter);
1204 connman_dbus_dict_open(&new_iter, &new_dict);
1206 dbus_message_iter_init(msg, &iter);
1207 dbus_message_iter_recurse(&iter, &array);
1209 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1210 DBusMessageIter entry, value;
1215 dbus_message_iter_recurse(&array, &entry);
1216 dbus_message_iter_get_basic(&entry, &key);
1218 dbus_message_iter_next(&entry);
1219 dbus_message_iter_recurse(&entry, &value);
1221 value_type = dbus_message_iter_get_arg_type(&value);
1224 switch (value_type) {
1225 case DBUS_TYPE_STRING:
1226 dbus_message_iter_get_basic(&value, &item_value);
1228 if (g_str_equal(key, "Type"))
1229 type = (const char *)item_value;
1230 else if (g_str_equal(key, "Name"))
1231 name = (const char *)item_value;
1232 else if (g_str_equal(key, "Host"))
1233 host = (const char *)item_value;
1234 else if (g_str_equal(key, "VPN.Domain"))
1235 domain = (const char *)item_value;
1237 DBG("%s %s", key, (char *)item_value);
1240 connman_dbus_dict_append_basic(&new_dict, key,
1241 value_type, &item_value);
1243 case DBUS_TYPE_ARRAY:
1244 if (g_str_equal(key, "Networks")) {
1245 networks = get_user_networks(&value);
1246 connman_dbus_dict_append_array(&new_dict,
1248 DBUS_TYPE_DICT_ENTRY,
1255 dbus_message_iter_next(&array);
1258 connman_dbus_dict_close(&new_iter, &new_dict);
1260 DBG("VPN type %s name %s host %s domain %s networks %p",
1261 type, name, host, domain, networks);
1263 if (!host || !domain) {
1268 if (!type || !name) {
1273 ident = g_strdup_printf("%s_%s", host, domain);
1274 set_dbus_ident(ident);
1276 DBG("ident %s", ident);
1278 data = g_hash_table_lookup(vpn_connections, ident);
1280 if (data->call || data->cb_data) {
1281 DBG("create configuration call already pending");
1286 char *path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1288 data = create_connection_data(path);
1296 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1300 * User called net.connman.Manager.ConnectProvider if we are here.
1301 * So use the data from original message in the new msg.
1303 me = g_strdup(dbus_message_get_destination(msg));
1305 dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1306 dbus_message_set_path(new_msg, "/");
1307 dbus_message_set_destination(new_msg, VPN_SERVICE);
1308 dbus_message_set_sender(new_msg, me);
1309 dbus_message_set_member(new_msg, "Create");
1311 user_data = g_try_new0(struct config_create_data, 1);
1317 user_data->callback = callback;
1318 user_data->message = dbus_message_ref(msg);
1319 user_data->path = NULL;
1321 DBG("cb %p msg %p", user_data, msg);
1323 result = dbus_connection_send_with_reply(connection, new_msg,
1324 &call, DBUS_TIMEOUT);
1325 if (!result || !call) {
1330 dbus_pending_call_set_notify(call, configuration_create_reply,
1336 dbus_message_unref(new_msg);
1339 g_slist_free_full(networks, destroy_route);
1345 static bool check_host(char **hosts, char *host)
1352 for (i = 0; hosts[i]; i++) {
1353 if (g_strcmp0(hosts[i], host) == 0)
1360 static void set_route(struct connection_data *data, struct vpn_route *route)
1363 * If the VPN administrator/user has given a route to
1364 * VPN server, then we must discard that because the
1365 * server cannot be contacted via VPN tunnel.
1367 if (check_host(data->host_ip, route->network)) {
1368 DBG("Discarding VPN route to %s via %s at index %d",
1369 route->network, route->gateway, data->index);
1373 if (route->family == AF_INET6) {
1374 unsigned char prefix_len = atoi(route->netmask);
1376 connman_inet_add_ipv6_network_route(data->index,
1381 connman_inet_add_network_route(data->index, route->network,
1387 static int set_routes(struct connman_provider *provider,
1388 enum connman_provider_route_type type)
1390 struct connection_data *data;
1391 GHashTableIter iter;
1392 gpointer value, key;
1394 DBG("provider %p", provider);
1396 data = connman_provider_get_data(provider);
1400 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1401 type == CONNMAN_PROVIDER_ROUTE_USER) {
1402 g_hash_table_iter_init(&iter, data->user_routes);
1404 while (g_hash_table_iter_next(&iter, &key, &value))
1405 set_route(data, value);
1408 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1409 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1410 g_hash_table_iter_init(&iter, data->server_routes);
1412 while (g_hash_table_iter_next(&iter, &key, &value))
1413 set_route(data, value);
1419 static bool check_routes(struct connman_provider *provider)
1421 struct connection_data *data;
1423 DBG("provider %p", provider);
1425 data = connman_provider_get_data(provider);
1429 if (data->user_routes &&
1430 g_hash_table_size(data->user_routes) > 0)
1433 if (data->server_routes &&
1434 g_hash_table_size(data->server_routes) > 0)
1440 static struct connman_provider_driver provider_driver = {
1442 .type = CONNMAN_PROVIDER_TYPE_VPN,
1443 .probe = provider_probe,
1444 .remove = provider_remove,
1445 .connect = provider_connect,
1446 .disconnect = provider_disconnect,
1447 .set_property = set_string,
1448 .get_property = get_string,
1449 .create = create_configuration,
1450 .set_routes = set_routes,
1451 .check_routes = check_routes,
1454 static void destroy_provider(struct connection_data *data)
1456 DBG("data %p", data);
1458 if (g_str_equal(data->state, "ready") ||
1459 g_str_equal(data->state, "configuration"))
1460 connman_provider_disconnect(data->provider);
1463 dbus_pending_call_cancel(data->call);
1465 connman_provider_set_data(data->provider, NULL);
1467 connman_provider_remove(data->provider);
1469 data->provider = NULL;
1472 static void connection_destroy(gpointer hash_data)
1474 struct connection_data *data = hash_data;
1476 DBG("data %p", data);
1479 destroy_provider(data);
1482 g_free(data->ident);
1483 g_free(data->state);
1487 g_free(data->host_ip);
1488 g_free(data->domain);
1489 g_hash_table_destroy(data->server_routes);
1490 g_hash_table_destroy(data->user_routes);
1491 g_strfreev(data->nameservers);
1492 g_hash_table_destroy(data->setting_strings);
1493 connman_ipaddress_free(data->ip);
1495 cancel_host_resolv(data);
1500 static void vpnd_created(DBusConnection *conn, void *user_data)
1502 DBG("connection %p", conn);
1504 get_connections(user_data);
1507 static void vpnd_removed(DBusConnection *conn, void *user_data)
1509 DBG("connection %p", conn);
1511 g_hash_table_remove_all(vpn_connections);
1514 static void remove_connection(DBusConnection *conn, const char *path)
1516 DBG("path %s", path);
1518 g_hash_table_remove(vpn_connections, get_ident(path));
1521 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1525 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1526 struct connection_data *data;
1528 if (!dbus_message_has_signature(message, signature)) {
1529 connman_error("vpn removed signature \"%s\" does not match "
1531 dbus_message_get_signature(message), signature);
1535 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1538 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1540 remove_connection(conn, path);
1545 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1548 DBusMessageIter iter, properties;
1550 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1551 DBUS_TYPE_ARRAY_AS_STRING
1552 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1553 DBUS_TYPE_STRING_AS_STRING
1554 DBUS_TYPE_VARIANT_AS_STRING
1555 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1557 if (!dbus_message_has_signature(message, signature)) {
1558 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1559 "match expected \"%s\"",
1560 dbus_message_get_signature(message), signature);
1566 if (!dbus_message_iter_init(message, &iter))
1569 dbus_message_iter_get_basic(&iter, &path);
1571 dbus_message_iter_next(&iter);
1572 dbus_message_iter_recurse(&iter, &properties);
1574 add_connection(path, &properties, user_data);
1579 static int save_route(GHashTable *routes, int family, const char *network,
1580 const char *netmask, const char *gateway)
1582 struct vpn_route *route;
1583 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1585 DBG("family %d network %s netmask %s", family, network, netmask);
1587 route = g_hash_table_lookup(routes, key);
1589 route = g_try_new0(struct vpn_route, 1);
1591 connman_error("out of memory");
1595 route->family = family;
1596 route->network = g_strdup(network);
1597 route->netmask = g_strdup(netmask);
1598 route->gateway = g_strdup(gateway);
1600 g_hash_table_replace(routes, key, route);
1607 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1609 DBusMessageIter dict;
1610 const char *network, *netmask, *gateway;
1613 dbus_message_iter_recurse(dicts, &dict);
1615 network = netmask = gateway = NULL;
1618 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1620 DBusMessageIter entry, value;
1623 dbus_message_iter_recurse(&dict, &entry);
1624 dbus_message_iter_get_basic(&entry, &key);
1626 dbus_message_iter_next(&entry);
1627 dbus_message_iter_recurse(&entry, &value);
1629 if (g_str_equal(key, "ProtocolFamily")) {
1631 dbus_message_iter_get_basic(&value, &pf);
1640 DBG("family %d", family);
1641 } else if (g_str_equal(key, "Netmask")) {
1642 dbus_message_iter_get_basic(&value, &netmask);
1643 DBG("netmask %s", netmask);
1644 } else if (g_str_equal(key, "Network")) {
1645 dbus_message_iter_get_basic(&value, &network);
1646 DBG("host %s", network);
1647 } else if (g_str_equal(key, "Gateway")) {
1648 dbus_message_iter_get_basic(&value, &gateway);
1649 DBG("gateway %s", gateway);
1652 dbus_message_iter_next(&dict);
1655 if (!netmask || !network || !gateway) {
1656 DBG("Value missing.");
1660 return save_route(routes, family, network, netmask, gateway);
1663 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1665 DBusMessageIter entry;
1668 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1669 DBG("Expecting array, ignoring routes.");
1673 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1675 dbus_message_iter_recurse(array, &entry);
1677 while (dbus_message_iter_get_arg_type(&entry) ==
1679 DBusMessageIter dicts;
1681 dbus_message_iter_recurse(&entry, &dicts);
1683 while (dbus_message_iter_get_arg_type(&dicts) ==
1685 int err = read_route_dict(routes, &dicts);
1688 dbus_message_iter_next(&dicts);
1691 dbus_message_iter_next(&entry);
1694 dbus_message_iter_next(array);
1700 static gboolean property_changed(DBusConnection *conn,
1701 DBusMessage *message,
1704 const char *path = dbus_message_get_path(message);
1705 struct connection_data *data = NULL;
1706 DBusMessageIter iter, value;
1707 bool ip_set = false;
1711 const char *signature = DBUS_TYPE_STRING_AS_STRING
1712 DBUS_TYPE_VARIANT_AS_STRING;
1714 if (!dbus_message_has_signature(message, signature)) {
1715 connman_error("vpn property signature \"%s\" does not match "
1717 dbus_message_get_signature(message), signature);
1721 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1725 if (!dbus_message_iter_init(message, &iter))
1728 dbus_message_iter_get_basic(&iter, &key);
1730 dbus_message_iter_next(&iter);
1731 dbus_message_iter_recurse(&iter, &value);
1735 if (g_str_equal(key, "State")) {
1736 dbus_message_iter_get_basic(&value, &str);
1738 DBG("%s %s -> %s", data->path, data->state, str);
1740 if (g_str_equal(data->state, str))
1743 g_free(data->state);
1744 data->state = g_strdup(str);
1746 set_provider_state(data);
1747 } else if (g_str_equal(key, "Index")) {
1748 dbus_message_iter_get_basic(&value, &data->index);
1749 connman_provider_set_index(data->provider, data->index);
1750 } else if (g_str_equal(key, "IPv4")) {
1751 err = extract_ip(&value, AF_INET, data);
1753 } else if (g_str_equal(key, "IPv6")) {
1754 err = extract_ip(&value, AF_INET6, data);
1756 } else if (g_str_equal(key, "ServerRoutes")) {
1757 err = routes_changed(&value, data->server_routes);
1759 * Note that the vpnd will delay the route sending a bit
1760 * (in order to collect the routes from VPN client),
1761 * so we might have got the State changed property before
1762 * we got ServerRoutes. This means that we must try to set
1763 * the routes here because they would be left unset otherwise.
1766 set_routes(data->provider,
1767 CONNMAN_PROVIDER_ROUTE_SERVER);
1768 } else if (g_str_equal(key, "UserRoutes")) {
1769 err = routes_changed(&value, data->user_routes);
1771 set_routes(data->provider,
1772 CONNMAN_PROVIDER_ROUTE_USER);
1773 } else if (g_str_equal(key, "Nameservers")) {
1774 if (extract_nameservers(&value, data) == 0 &&
1776 connman_provider_set_nameservers(data->provider,
1778 } else if (g_str_equal(key, "Domain")) {
1779 dbus_message_iter_get_basic(&value, &str);
1780 g_free(data->domain);
1781 data->domain = g_strdup(str);
1782 connman_provider_set_domain(data->provider, data->domain);
1785 if (ip_set && err == 0) {
1786 err = connman_provider_set_ipaddress(data->provider, data->ip);
1788 DBG("setting provider IP address failed (%s/%d)",
1789 strerror(-err), -err);
1795 static int vpn_init(void)
1799 connection = connman_dbus_get_connection();
1803 watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
1804 vpnd_created, vpnd_removed, &provider_driver, NULL);
1806 added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1807 VPN_MANAGER_INTERFACE,
1808 CONNECTION_ADDED, connection_added,
1809 &provider_driver, NULL);
1811 removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1812 VPN_MANAGER_INTERFACE,
1813 CONNECTION_REMOVED, connection_removed,
1816 property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1817 VPN_CONNECTION_INTERFACE,
1818 PROPERTY_CHANGED, property_changed,
1821 if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
1826 err = connman_provider_driver_register(&provider_driver);
1828 vpn_connections = g_hash_table_new_full(g_str_hash,
1830 g_free, connection_destroy);
1832 vpnd_created(connection, &provider_driver);
1838 g_dbus_remove_watch(connection, watch);
1839 g_dbus_remove_watch(connection, added_watch);
1840 g_dbus_remove_watch(connection, removed_watch);
1841 g_dbus_remove_watch(connection, property_watch);
1843 dbus_connection_unref(connection);
1848 static void vpn_exit(void)
1850 g_dbus_remove_watch(connection, watch);
1851 g_dbus_remove_watch(connection, added_watch);
1852 g_dbus_remove_watch(connection, removed_watch);
1853 g_dbus_remove_watch(connection, property_watch);
1855 connman_provider_driver_unregister(&provider_driver);
1857 if (vpn_connections)
1858 g_hash_table_destroy(vpn_connections);
1860 dbus_connection_unref(connection);
1863 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
1864 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)