5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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;
74 connman_bool_t connect_pending;
75 struct config_create_data *cb_data;
85 GHashTable *server_routes;
86 GHashTable *user_routes;
87 GHashTable *setting_strings;
89 struct connman_ipaddress *ip;
95 static int set_string(struct connman_provider *provider,
96 const char *key, const char *value)
98 struct connection_data *data;
100 data = connman_provider_get_data(provider);
104 DBG("data %p provider %p key %s value %s", data, provider, key, value);
106 if (g_str_equal(key, "Type") == TRUE) {
108 data->type = g_strdup(value);
109 } else if (g_str_equal(key, "Name") == TRUE) {
111 data->name = g_strdup(value);
112 } else if (g_str_equal(key, "Host") == TRUE) {
114 data->host = g_strdup(value);
115 } else if (g_str_equal(key, "VPN.Domain") == TRUE ||
116 g_str_equal(key, "Domain") == TRUE) {
117 g_free(data->domain);
118 data->domain = g_strdup(value);
120 g_hash_table_replace(data->setting_strings,
121 g_strdup(key), g_strdup(value));
125 static const char *get_string(struct connman_provider *provider,
128 struct connection_data *data;
130 data = connman_provider_get_data(provider);
134 DBG("data %p provider %p key %s", data, provider, key);
136 if (g_str_equal(key, "Type") == TRUE)
138 else if (g_str_equal(key, "Name") == TRUE)
140 else if (g_str_equal(key, "Host") == TRUE)
142 else if (g_str_equal(key, "HostIP") == TRUE) {
143 if (data->host_ip == NULL ||
144 data->host_ip[0] == NULL)
147 return data->host_ip[0];
148 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
151 return g_hash_table_lookup(data->setting_strings, key);
154 static char *get_ident(const char *path)
161 pos = strrchr(path, '/');
168 static void cancel_host_resolv(struct connection_data *data)
170 if (data->resolv_id != 0)
171 g_resolv_cancel_lookup(data->resolv, data->resolv_id);
175 g_resolv_unref(data->resolv);
179 static gboolean remove_resolv(gpointer user_data)
181 struct connection_data *data = user_data;
183 cancel_host_resolv(data);
188 static void resolv_result(GResolvResultStatus status,
189 char **results, gpointer user_data)
191 struct connection_data *data = user_data;
193 DBG("status %d", status);
195 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
196 g_strv_length(results) > 0)
197 data->host_ip = g_strdupv(results);
200 * We cannot unref the resolver here as resolv struct is manipulated
201 * by gresolv.c after we return from this callback.
203 g_timeout_add_seconds(0, remove_resolv, data);
208 static void resolv_host_addr(struct connection_data *data)
210 if (data->host == NULL)
213 if (connman_inet_check_ipaddress(data->host) > 0)
216 if (data->host_ip != NULL)
219 data->resolv = g_resolv_new(0);
220 if (data->resolv == NULL) {
221 DBG("Cannot resolv %s", data->host);
225 DBG("Trying to resolv %s", data->host);
227 data->resolv_id = g_resolv_lookup_hostname(data->resolv, data->host,
228 resolv_result, data);
231 static void free_config_cb_data(struct config_create_data *cb_data)
236 g_free(cb_data->path);
237 cb_data->path = NULL;
239 if (cb_data->message != NULL) {
240 dbus_message_unref(cb_data->message);
241 cb_data->message = NULL;
244 cb_data->callback = NULL;
249 static void set_provider_state(struct connection_data *data)
251 enum connman_provider_state state = CONNMAN_PROVIDER_STATE_UNKNOWN;
254 if (g_str_equal(data->state, "ready") == TRUE) {
255 state = CONNMAN_PROVIDER_STATE_READY;
257 } else if (g_str_equal(data->state, "configuration") == TRUE) {
258 state = CONNMAN_PROVIDER_STATE_CONNECT;
259 } else if (g_str_equal(data->state, "idle") == TRUE) {
260 state = CONNMAN_PROVIDER_STATE_IDLE;
261 } else if (g_str_equal(data->state, "disconnect") == TRUE) {
263 state = CONNMAN_PROVIDER_STATE_DISCONNECT;
265 } else if (g_str_equal(data->state, "failure") == TRUE) {
267 state = CONNMAN_PROVIDER_STATE_FAILURE;
271 connman_provider_set_state(data->provider, state);
275 if (data->cb_data != NULL)
276 data->cb_data->callback(data->cb_data->message,
279 connman_provider_set_state(data->provider, state);
281 free_config_cb_data(data->cb_data);
282 data->cb_data = NULL;
285 static int create_provider(struct connection_data *data, void *user_data)
287 struct connman_provider_driver *driver = user_data;
290 DBG("%s", data->path);
292 data->provider = connman_provider_get(data->ident);
293 if (data->provider == NULL)
296 DBG("provider %p name %s", data->provider, data->name);
298 connman_provider_set_data(data->provider, data);
299 connman_provider_set_driver(data->provider, driver);
301 err = connman_provider_create_service(data->provider);
303 if (g_str_equal(data->state, "ready") == TRUE) {
304 connman_provider_set_index(data->provider,
306 if (data->ip != NULL)
307 connman_provider_set_ipaddress(data->provider,
311 set_provider_state(data);
317 static void destroy_route(gpointer user_data)
319 struct vpn_route *route = user_data;
321 g_free(route->network);
322 g_free(route->netmask);
323 g_free(route->gateway);
327 static struct connection_data *create_connection_data(const char *path)
329 struct connection_data *data;
331 data = g_try_new0(struct connection_data, 1);
335 DBG("path %s", path);
337 data->path = g_strdup(path);
338 data->ident = g_strdup(get_ident(path));
341 data->setting_strings = g_hash_table_new_full(g_str_hash,
342 g_str_equal, g_free, g_free);
344 data->server_routes = g_hash_table_new_full(g_direct_hash,
345 g_str_equal, g_free, destroy_route);
346 data->user_routes = g_hash_table_new_full(g_str_hash,
347 g_str_equal, g_free, destroy_route);
352 static int extract_ip(DBusMessageIter *array, int family,
353 struct connection_data *data)
355 DBusMessageIter dict;
356 char *address = NULL, *gateway = NULL, *netmask = NULL, *peer = NULL;
357 unsigned char prefix_len;
359 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
362 dbus_message_iter_recurse(array, &dict);
364 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
365 DBusMessageIter entry, value;
368 dbus_message_iter_recurse(&dict, &entry);
369 dbus_message_iter_get_basic(&entry, &key);
371 dbus_message_iter_next(&entry);
372 dbus_message_iter_recurse(&entry, &value);
374 if (g_str_equal(key, "Address") == TRUE) {
375 dbus_message_iter_get_basic(&value, &address);
376 DBG("address %s", address);
377 } else if (g_str_equal(key, "Netmask") == TRUE) {
378 dbus_message_iter_get_basic(&value, &netmask);
379 DBG("netmask %s", netmask);
380 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
381 dbus_message_iter_get_basic(&value, &netmask);
382 DBG("prefix length %s", netmask);
383 } else if (g_str_equal(key, "Peer") == TRUE) {
384 dbus_message_iter_get_basic(&value, &peer);
385 DBG("peer %s", peer);
386 } else if (g_str_equal(key, "Gateway") == TRUE) {
387 dbus_message_iter_get_basic(&value, &gateway);
388 DBG("gateway %s", gateway);
391 dbus_message_iter_next(&dict);
394 data->ip = connman_ipaddress_alloc(family);
395 if (data->ip == NULL)
400 connman_ipaddress_set_ipv4(data->ip, address, netmask,
404 prefix_len = atoi(netmask);
405 connman_ipaddress_set_ipv6(data->ip, address, prefix_len,
412 connman_ipaddress_set_peer(data->ip, peer);
417 static int extract_nameservers(DBusMessageIter *array,
418 struct connection_data *data)
420 DBusMessageIter entry;
421 char **nameservers = NULL;
424 dbus_message_iter_recurse(array, &entry);
426 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
427 const char *nameserver;
429 dbus_message_iter_get_basic(&entry, &nameserver);
431 nameservers = g_try_renew(char *, nameservers, i + 2);
432 if (nameservers == NULL)
435 DBG("[%d] %s", i, nameserver);
437 nameservers[i] = g_strdup(nameserver);
438 if (nameservers[i] == NULL)
441 nameservers[++i] = NULL;
443 dbus_message_iter_next(&entry);
446 g_strfreev(data->nameservers);
447 data->nameservers = nameservers;
452 static int errorstr2val(const char *error) {
453 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".InProgress") == 0)
456 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".AlreadyConnected") == 0)
459 return -ECONNREFUSED;
462 static void connect_reply(DBusPendingCall *call, void *user_data)
466 struct connection_data *data = user_data;
467 struct config_create_data *cb_data = data->cb_data;
469 if (dbus_pending_call_get_completed(call) == FALSE)
472 DBG("user_data %p path %s", user_data, cb_data ? cb_data->path : NULL);
474 reply = dbus_pending_call_steal_reply(call);
476 dbus_error_init(&error);
478 if (dbus_set_error_from_message(&error, reply) == TRUE) {
479 int err = errorstr2val(error.name);
480 if (err != -EINPROGRESS) {
481 connman_error("Connect reply: %s (%s)", error.message,
483 dbus_error_free(&error);
485 DBG("data %p cb_data %p", data, cb_data);
486 if (cb_data != NULL) {
487 cb_data->callback(cb_data->message, err, NULL);
488 free_config_cb_data(cb_data);
489 data->cb_data = NULL;
493 dbus_error_free(&error);
497 * The vpn connection is up when we get a "ready" state
498 * property so at this point we do nothing for the provider
503 dbus_message_unref(reply);
505 dbus_pending_call_unref(call);
508 static int connect_provider(struct connection_data *data, void *user_data)
510 DBusPendingCall *call;
511 DBusMessage *message;
512 struct config_create_data *cb_data = user_data;
514 DBG("data %p user %p path %s", data, cb_data, data->path);
516 data->connect_pending = FALSE;
518 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
519 VPN_CONNECTION_INTERFACE,
524 if (dbus_connection_send_with_reply(connection, message,
525 &call, DBUS_TIMEOUT) == FALSE) {
526 connman_error("Unable to call %s.%s()",
527 VPN_CONNECTION_INTERFACE, VPN_CONNECT);
528 dbus_message_unref(message);
533 dbus_message_unref(message);
537 if (cb_data != NULL) {
538 g_free(cb_data->path);
539 cb_data->path = g_strdup(data->path);
542 dbus_pending_call_set_notify(call, connect_reply, data, NULL);
544 dbus_message_unref(message);
549 static void add_connection(const char *path, DBusMessageIter *properties,
552 struct connection_data *data;
554 char *ident = get_ident(path);
555 connman_bool_t found = FALSE;
557 data = g_hash_table_lookup(vpn_connections, ident);
560 * We might have a dummy connection struct here that
561 * was created by configuration_create_reply() so in
562 * that case just continue.
564 if (data->connect_pending == FALSE)
569 data = create_connection_data(path);
574 DBG("data %p path %s", data, path);
576 while (dbus_message_iter_get_arg_type(properties) ==
577 DBUS_TYPE_DICT_ENTRY) {
578 DBusMessageIter entry, value;
582 dbus_message_iter_recurse(properties, &entry);
583 dbus_message_iter_get_basic(&entry, &key);
585 dbus_message_iter_next(&entry);
586 dbus_message_iter_recurse(&entry, &value);
588 if (g_str_equal(key, "State") == TRUE) {
589 dbus_message_iter_get_basic(&value, &str);
590 DBG("state %s -> %s", data->state, str);
591 data->state = g_strdup(str);
592 } else if (g_str_equal(key, "IPv4") == TRUE) {
593 extract_ip(&value, AF_INET, data);
594 } else if (g_str_equal(key, "IPv6") == TRUE) {
595 extract_ip(&value, AF_INET6, data);
596 } else if (g_str_equal(key, "Name") == TRUE) {
597 dbus_message_iter_get_basic(&value, &str);
598 data->name = g_strdup(str);
599 } else if (g_str_equal(key, "Type") == TRUE) {
600 dbus_message_iter_get_basic(&value, &str);
601 data->type = g_strdup(str);
602 } else if (g_str_equal(key, "Host") == TRUE) {
603 dbus_message_iter_get_basic(&value, &str);
604 data->host = g_strdup(str);
605 } else if (g_str_equal(key, "Domain") == TRUE) {
606 dbus_message_iter_get_basic(&value, &str);
607 data->domain = g_strdup(str);
608 } else if (g_str_equal(key, "Nameservers") == TRUE) {
609 extract_nameservers(&value, data);
610 } else if (g_str_equal(key, "Index") == TRUE) {
611 dbus_message_iter_get_basic(&value, &data->index);
613 if (dbus_message_iter_get_arg_type(&value) ==
615 dbus_message_iter_get_basic(&value, &str);
616 g_hash_table_replace(data->setting_strings,
617 g_strdup(key), g_strdup(str));
619 DBG("unknown key %s", key);
623 dbus_message_iter_next(properties);
627 g_hash_table_insert(vpn_connections, g_strdup(data->ident),
630 err = create_provider(data, user_data);
634 resolv_host_addr(data);
636 if (data->connect_pending == TRUE)
637 connect_provider(data, data->cb_data);
642 DBG("removing %s", data->ident);
643 g_hash_table_remove(vpn_connections, data->ident);
646 static void get_connections_reply(DBusPendingCall *call, void *user_data)
650 DBusMessageIter array, dict;
651 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
652 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
653 DBUS_TYPE_OBJECT_PATH_AS_STRING
654 DBUS_TYPE_ARRAY_AS_STRING
655 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
656 DBUS_TYPE_STRING_AS_STRING
657 DBUS_TYPE_VARIANT_AS_STRING
658 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
659 DBUS_STRUCT_END_CHAR_AS_STRING;
661 if (dbus_pending_call_get_completed(call) == FALSE)
666 reply = dbus_pending_call_steal_reply(call);
668 dbus_error_init(&error);
670 if (dbus_set_error_from_message(&error, reply) == TRUE) {
671 connman_error("%s", error.message);
672 dbus_error_free(&error);
676 if (dbus_message_has_signature(reply, signature) == FALSE) {
677 connman_error("vpnd signature \"%s\" does not match "
679 dbus_message_get_signature(reply), signature);
683 if (dbus_message_iter_init(reply, &array) == FALSE)
686 dbus_message_iter_recurse(&array, &dict);
688 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
689 DBusMessageIter value, properties;
692 dbus_message_iter_recurse(&dict, &value);
693 dbus_message_iter_get_basic(&value, &path);
695 dbus_message_iter_next(&value);
696 dbus_message_iter_recurse(&value, &properties);
698 add_connection(path, &properties, user_data);
700 dbus_message_iter_next(&dict);
704 dbus_message_unref(reply);
706 dbus_pending_call_unref(call);
709 static int get_connections(void *user_data)
711 DBusPendingCall *call;
712 DBusMessage *message;
716 message = dbus_message_new_method_call(VPN_SERVICE, "/",
717 VPN_MANAGER_INTERFACE,
722 if (dbus_connection_send_with_reply(connection, message,
723 &call, DBUS_TIMEOUT) == FALSE) {
724 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
726 dbus_message_unref(message);
731 dbus_message_unref(message);
735 dbus_pending_call_set_notify(call, get_connections_reply,
738 dbus_message_unref(message);
743 static int provider_probe(struct connman_provider *provider)
748 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
753 if (dbus_pending_call_get_completed(call) == FALSE)
758 reply = dbus_pending_call_steal_reply(call);
760 dbus_error_init(&error);
762 if (dbus_set_error_from_message(&error, reply) == TRUE) {
764 * If the returned error is NotFound, it means that we
765 * have actually removed the provider in vpnd already.
767 if (dbus_error_has_name(&error, CONNMAN_ERROR_INTERFACE
768 ".NotFound") == FALSE)
769 connman_error("%s", error.message);
771 dbus_error_free(&error);
774 dbus_message_unref(reply);
776 dbus_pending_call_unref(call);
779 static int provider_remove(struct connman_provider *provider)
781 DBusPendingCall *call;
782 DBusMessage *message;
783 struct connection_data *data;
785 data = connman_provider_get_data(provider);
787 DBG("provider %p data %p", provider, data);
790 * When provider.c:provider_remove() calls this function,
791 * it will remove the provider itself after the call.
792 * This means that we cannot use the provider pointer later
793 * as it is no longer valid.
795 data->provider = NULL;
797 message = dbus_message_new_method_call(VPN_SERVICE, "/",
798 VPN_MANAGER_INTERFACE,
803 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
806 if (dbus_connection_send_with_reply(connection, message,
807 &call, DBUS_TIMEOUT) == FALSE) {
808 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
810 dbus_message_unref(message);
815 dbus_message_unref(message);
819 dbus_pending_call_set_notify(call, remove_connection_reply,
822 dbus_message_unref(message);
827 static int provider_connect(struct connman_provider *provider)
829 struct connection_data *data;
831 data = connman_provider_get_data(provider);
835 return connect_provider(data, NULL);
838 static void disconnect_reply(DBusPendingCall *call, void *user_data)
843 if (dbus_pending_call_get_completed(call) == FALSE)
846 DBG("user %p", user_data);
848 reply = dbus_pending_call_steal_reply(call);
850 dbus_error_init(&error);
852 if (dbus_set_error_from_message(&error, reply) == TRUE) {
853 connman_error("%s", error.message);
854 dbus_error_free(&error);
859 dbus_message_unref(reply);
861 dbus_pending_call_unref(call);
864 static int disconnect_provider(struct connection_data *data)
866 DBusPendingCall *call;
867 DBusMessage *message;
869 DBG("data %p path %s", data, data->path);
871 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
872 VPN_CONNECTION_INTERFACE,
877 if (dbus_connection_send_with_reply(connection, message,
878 &call, DBUS_TIMEOUT) == FALSE) {
879 connman_error("Unable to call %s.%s()",
880 VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
881 dbus_message_unref(message);
886 dbus_message_unref(message);
890 dbus_pending_call_set_notify(call, disconnect_reply, NULL, NULL);
892 dbus_message_unref(message);
894 connman_provider_set_state(data->provider,
895 CONNMAN_PROVIDER_STATE_DISCONNECT);
897 * We return 0 here instead of -EINPROGRESS because
898 * __connman_service_disconnect() needs to return something
899 * to gdbus so that gdbus will not call Disconnect() more
900 * than once. This way we do not need to pass the dbus reply
901 * message around the code.
906 static int provider_disconnect(struct connman_provider *provider)
908 struct connection_data *data;
910 DBG("provider %p", provider);
912 data = connman_provider_get_data(provider);
916 if (g_str_equal(data->state, "ready") == TRUE ||
917 g_str_equal(data->state, "configuration") == TRUE)
918 return disconnect_provider(data);
923 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
927 DBusMessageIter iter;
928 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
931 struct connection_data *data;
932 struct config_create_data *cb_data = user_data;
934 if (dbus_pending_call_get_completed(call) == FALSE)
937 DBG("user %p", cb_data);
939 reply = dbus_pending_call_steal_reply(call);
941 dbus_error_init(&error);
943 if (dbus_set_error_from_message(&error, reply) == TRUE) {
944 connman_error("dbus error: %s", error.message);
945 dbus_error_free(&error);
949 if (dbus_message_has_signature(reply, signature) == FALSE) {
950 connman_error("vpn configuration signature \"%s\" does not "
951 "match expected \"%s\"",
952 dbus_message_get_signature(reply), signature);
956 if (dbus_message_iter_init(reply, &iter) == FALSE)
959 dbus_message_iter_get_basic(&iter, &path);
962 * Then try to connect the VPN as expected by ConnectProvider API
964 ident = get_ident(path);
966 data = g_hash_table_lookup(vpn_connections, ident);
969 * Someone removed the data. We cannot really continue.
971 DBG("Pending data not found for %s, cannot continue!", ident);
974 data->connect_pending = TRUE;
976 if (data->cb_data == NULL)
977 data->cb_data = cb_data;
979 DBG("Connection callback data already in use!");
982 * Connection is created in add_connections() after
983 * we have received the ConnectionAdded signal.
986 DBG("cb %p msg %p", data->cb_data,
987 data->cb_data ? data->cb_data->message : NULL);
991 dbus_message_unref(reply);
993 dbus_pending_call_unref(call);
996 static void set_dbus_ident(char *ident)
998 int i, len = strlen(ident);
1000 for (i = 0; i < len; i++) {
1001 if (ident[i] >= '0' && ident[i] <= '9')
1003 if (ident[i] >= 'a' && ident[i] <= 'z')
1005 if (ident[i] >= 'A' && ident[i] <= 'Z')
1011 static struct vpn_route *parse_user_route(const char *user_route)
1013 char *network, *netmask;
1014 struct vpn_route *route = NULL;
1015 int family = PF_UNSPEC;
1016 char **elems = g_strsplit(user_route, "/", 0);
1022 if (network == NULL || *network == '\0') {
1023 DBG("no network/netmask set");
1028 if (netmask != NULL && *netmask == '\0') {
1029 DBG("no netmask set");
1033 if (g_strrstr(network, ":") != NULL)
1035 else if (g_strrstr(network, ".") != NULL) {
1038 if (g_strrstr(netmask, ".") == NULL) {
1039 /* We have netmask length */
1041 struct in_addr netmask_in;
1042 unsigned char prefix_len = 32;
1044 if (netmask != NULL) {
1046 long int value = strtol(netmask, &ptr, 10);
1047 if (ptr != netmask && *ptr == '\0' &&
1052 addr = 0xffffffff << (32 - prefix_len);
1053 netmask_in.s_addr = htonl(addr);
1054 netmask = inet_ntoa(netmask_in);
1056 DBG("network %s netmask %s", network, netmask);
1060 route = g_try_new(struct vpn_route, 1);
1064 route->network = g_strdup(network);
1065 route->netmask = g_strdup(netmask);
1066 route->gateway = NULL;
1067 route->family = family;
1074 static GSList *get_user_networks(DBusMessageIter *array)
1076 DBusMessageIter entry;
1077 GSList *list = NULL;
1079 dbus_message_iter_recurse(array, &entry);
1081 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1083 struct vpn_route *route;
1085 dbus_message_iter_get_basic(&entry, &val);
1087 route = parse_user_route(val);
1089 list = g_slist_prepend(list, route);
1091 dbus_message_iter_next(&entry);
1097 static void append_route(DBusMessageIter *iter, void *user_data)
1099 struct vpn_route *route = user_data;
1100 DBusMessageIter item;
1103 connman_dbus_dict_open(iter, &item);
1108 if (route->family == AF_INET)
1110 else if (route->family == AF_INET6)
1114 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1115 DBUS_TYPE_INT32, &family);
1117 if (route->network != NULL)
1118 connman_dbus_dict_append_basic(&item, "Network",
1119 DBUS_TYPE_STRING, &route->network);
1121 if (route->netmask != NULL)
1122 connman_dbus_dict_append_basic(&item, "Netmask",
1123 DBUS_TYPE_STRING, &route->netmask);
1125 if (route->gateway != NULL)
1126 connman_dbus_dict_append_basic(&item, "Gateway",
1127 DBUS_TYPE_STRING, &route->gateway);
1130 connman_dbus_dict_close(iter, &item);
1133 static void append_routes(DBusMessageIter *iter, void *user_data)
1135 GSList *list, *routes = user_data;
1137 DBG("routes %p", routes);
1139 for (list = routes; list != NULL; list = g_slist_next(list)) {
1140 DBusMessageIter dict;
1141 struct vpn_route *route = list->data;
1143 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1145 append_route(&dict, route);
1146 dbus_message_iter_close_container(iter, &dict);
1150 static int create_configuration(DBusMessage *msg, connection_ready_cb callback)
1152 DBusMessage *new_msg = NULL;
1153 DBusPendingCall *call;
1154 DBusMessageIter iter, array, new_iter, new_dict;
1155 const char *type = NULL, *name = NULL;
1156 const char *host = NULL, *domain = NULL;
1157 char *ident, *me = NULL;
1160 struct connection_data *data;
1161 struct config_create_data *user_data = NULL;
1162 GSList *networks = NULL;
1165 * We copy the old message data into new message. We cannot
1166 * just use the old message as is because the user route
1167 * information is not in the same format in vpnd.
1169 new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1170 dbus_message_iter_init_append(new_msg, &new_iter);
1171 connman_dbus_dict_open(&new_iter, &new_dict);
1173 dbus_message_iter_init(msg, &iter);
1174 dbus_message_iter_recurse(&iter, &array);
1176 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1177 DBusMessageIter entry, value;
1182 dbus_message_iter_recurse(&array, &entry);
1183 dbus_message_iter_get_basic(&entry, &key);
1185 dbus_message_iter_next(&entry);
1186 dbus_message_iter_recurse(&entry, &value);
1188 value_type = dbus_message_iter_get_arg_type(&value);
1191 switch (value_type) {
1192 case DBUS_TYPE_STRING:
1193 dbus_message_iter_get_basic(&value, &item_value);
1195 if (g_str_equal(key, "Type") == TRUE) {
1196 type = (const char *)item_value;
1197 } else if (g_str_equal(key, "Name") == TRUE) {
1198 name = (const char *)item_value;
1199 } else if (g_str_equal(key, "Host") == TRUE) {
1200 host = (const char *)item_value;
1201 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1202 domain = (const char *)item_value;
1205 DBG("%s %s", key, (char *)item_value);
1207 if (item_value != NULL)
1208 connman_dbus_dict_append_basic(&new_dict, key,
1209 value_type, &item_value);
1211 case DBUS_TYPE_ARRAY:
1212 if (g_str_equal(key, "Networks") == TRUE) {
1213 networks = get_user_networks(&value);
1214 connman_dbus_dict_append_array(&new_dict,
1216 DBUS_TYPE_DICT_ENTRY,
1223 dbus_message_iter_next(&array);
1226 connman_dbus_dict_close(&new_iter, &new_dict);
1228 DBG("VPN type %s name %s host %s domain %s networks %p",
1229 type, name, host, domain, networks);
1231 if (host == NULL || domain == NULL) {
1236 if (type == NULL || name == NULL) {
1241 ident = g_strdup_printf("%s_%s", host, domain);
1242 set_dbus_ident(ident);
1244 DBG("ident %s", ident);
1246 data = g_hash_table_lookup(vpn_connections, ident);
1248 if (data->call != NULL || data->cb_data != NULL) {
1249 DBG("create configuration call already pending");
1254 char *path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1256 data = create_connection_data(path);
1264 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1268 * User called net.connman.Manager.ConnectProvider if we are here.
1269 * So use the data from original message in the new msg.
1271 me = g_strdup(dbus_message_get_destination(msg));
1273 dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1274 dbus_message_set_path(new_msg, "/");
1275 dbus_message_set_destination(new_msg, VPN_SERVICE);
1276 dbus_message_set_sender(new_msg, me);
1277 dbus_message_set_member(new_msg, "Create");
1279 user_data = g_try_new0(struct config_create_data, 1);
1280 if (user_data == NULL) {
1285 user_data->callback = callback;
1286 user_data->message = dbus_message_ref(msg);
1287 user_data->path = NULL;
1289 DBG("cb %p msg %p", user_data, msg);
1291 result = dbus_connection_send_with_reply(connection, new_msg,
1292 &call, DBUS_TIMEOUT);
1293 if (result == FALSE || call == NULL) {
1298 dbus_pending_call_set_notify(call, configuration_create_reply,
1303 if (new_msg != NULL)
1304 dbus_message_unref(new_msg);
1306 if (networks != NULL)
1307 g_slist_free_full(networks, destroy_route);
1313 static connman_bool_t check_host(char **hosts, char *host)
1320 for (i = 0; hosts[i] != NULL; i++) {
1321 if (g_strcmp0(hosts[i], host) == 0)
1328 static void set_route(struct connection_data *data, struct vpn_route *route)
1331 * If the VPN administrator/user has given a route to
1332 * VPN server, then we must discard that because the
1333 * server cannot be contacted via VPN tunnel.
1335 if (check_host(data->host_ip, route->network) == TRUE) {
1336 DBG("Discarding VPN route to %s via %s at index %d",
1337 route->network, route->gateway, data->index);
1341 if (route->family == AF_INET6) {
1342 unsigned char prefix_len = atoi(route->netmask);
1344 connman_inet_add_ipv6_network_route(data->index,
1349 connman_inet_add_network_route(data->index, route->network,
1355 static int set_routes(struct connman_provider *provider,
1356 enum connman_provider_route_type type)
1358 struct connection_data *data;
1359 GHashTableIter iter;
1360 gpointer value, key;
1362 DBG("provider %p", provider);
1364 data = connman_provider_get_data(provider);
1368 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1369 type == CONNMAN_PROVIDER_ROUTE_USER) {
1370 g_hash_table_iter_init(&iter, data->user_routes);
1372 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1373 set_route(data, value);
1376 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1377 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1378 g_hash_table_iter_init(&iter, data->server_routes);
1380 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1381 set_route(data, value);
1387 static connman_bool_t check_routes(struct connman_provider *provider)
1389 struct connection_data *data;
1391 DBG("provider %p", provider);
1393 data = connman_provider_get_data(provider);
1397 if (data->user_routes != NULL &&
1398 g_hash_table_size(data->user_routes) > 0)
1401 if (data->server_routes != NULL &&
1402 g_hash_table_size(data->server_routes) > 0)
1408 static struct connman_provider_driver provider_driver = {
1410 .type = CONNMAN_PROVIDER_TYPE_VPN,
1411 .probe = provider_probe,
1412 .remove = provider_remove,
1413 .connect = provider_connect,
1414 .disconnect = provider_disconnect,
1415 .set_property = set_string,
1416 .get_property = get_string,
1417 .create = create_configuration,
1418 .set_routes = set_routes,
1419 .check_routes = check_routes,
1422 static void destroy_provider(struct connection_data *data)
1424 DBG("data %p", data);
1426 if (g_str_equal(data->state, "ready") == TRUE ||
1427 g_str_equal(data->state, "configuration") == TRUE)
1428 connman_provider_disconnect(data->provider);
1430 if (data->call != NULL)
1431 dbus_pending_call_cancel(data->call);
1433 connman_provider_put(data->provider);
1435 data->provider = NULL;
1438 static void connection_destroy(gpointer hash_data)
1440 struct connection_data *data = hash_data;
1442 DBG("data %p", data);
1444 if (data->provider != NULL)
1445 destroy_provider(data);
1448 g_free(data->ident);
1449 g_free(data->state);
1453 g_free(data->domain);
1454 g_hash_table_destroy(data->server_routes);
1455 g_hash_table_destroy(data->user_routes);
1456 g_strfreev(data->nameservers);
1457 g_hash_table_destroy(data->setting_strings);
1458 connman_ipaddress_free(data->ip);
1460 cancel_host_resolv(data);
1465 static void vpnd_created(DBusConnection *conn, void *user_data)
1467 DBG("connection %p", conn);
1469 get_connections(user_data);
1472 static void vpnd_removed(DBusConnection *conn, void *user_data)
1474 DBG("connection %p", conn);
1476 g_hash_table_remove_all(vpn_connections);
1479 static void remove_connection(DBusConnection *conn, const char *path)
1481 DBG("path %s", path);
1483 g_hash_table_remove(vpn_connections, get_ident(path));
1486 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1490 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1492 if (dbus_message_has_signature(message, signature) == FALSE) {
1493 connman_error("vpn removed signature \"%s\" does not match "
1495 dbus_message_get_signature(message), signature);
1499 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1501 remove_connection(conn, path);
1505 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1508 DBusMessageIter iter, properties;
1510 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1511 DBUS_TYPE_ARRAY_AS_STRING
1512 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1513 DBUS_TYPE_STRING_AS_STRING
1514 DBUS_TYPE_VARIANT_AS_STRING
1515 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1517 if (dbus_message_has_signature(message, signature) == FALSE) {
1518 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1519 "match expected \"%s\"",
1520 dbus_message_get_signature(message), signature);
1526 if (dbus_message_iter_init(message, &iter) == FALSE)
1529 dbus_message_iter_get_basic(&iter, &path);
1531 dbus_message_iter_next(&iter);
1532 dbus_message_iter_recurse(&iter, &properties);
1534 add_connection(path, &properties, user_data);
1539 static int save_route(GHashTable *routes, int family, const char *network,
1540 const char *netmask, const char *gateway)
1542 struct vpn_route *route;
1543 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1545 DBG("family %d network %s netmask %s", family, network, netmask);
1547 route = g_hash_table_lookup(routes, key);
1548 if (route == NULL) {
1549 route = g_try_new0(struct vpn_route, 1);
1550 if (route == NULL) {
1551 connman_error("out of memory");
1555 route->family = family;
1556 route->network = g_strdup(network);
1557 route->netmask = g_strdup(netmask);
1558 route->gateway = g_strdup(gateway);
1560 g_hash_table_replace(routes, key, route);
1567 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1569 DBusMessageIter dict;
1570 const char *network, *netmask, *gateway;
1573 dbus_message_iter_recurse(dicts, &dict);
1575 network = netmask = gateway = NULL;
1578 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1580 DBusMessageIter entry, value;
1583 dbus_message_iter_recurse(&dict, &entry);
1584 dbus_message_iter_get_basic(&entry, &key);
1586 dbus_message_iter_next(&entry);
1587 dbus_message_iter_recurse(&entry, &value);
1589 if (g_str_equal(key, "ProtocolFamily") == TRUE) {
1591 dbus_message_iter_get_basic(&value, &pf);
1600 DBG("family %d", family);
1601 } else if (g_str_equal(key, "Netmask") == TRUE) {
1602 dbus_message_iter_get_basic(&value, &netmask);
1603 DBG("netmask %s", netmask);
1604 } else if (g_str_equal(key, "Network") == TRUE) {
1605 dbus_message_iter_get_basic(&value, &network);
1606 DBG("host %s", network);
1607 } else if (g_str_equal(key, "Gateway") == TRUE) {
1608 dbus_message_iter_get_basic(&value, &gateway);
1609 DBG("gateway %s", gateway);
1612 dbus_message_iter_next(&dict);
1615 if (netmask == NULL || network == NULL || gateway == NULL) {
1616 DBG("Value missing.");
1620 return save_route(routes, family, network, netmask, gateway);
1623 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1625 DBusMessageIter entry;
1628 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1629 DBG("Expecting array, ignoring routes.");
1633 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1635 dbus_message_iter_recurse(array, &entry);
1637 while (dbus_message_iter_get_arg_type(&entry) ==
1639 DBusMessageIter dicts;
1641 dbus_message_iter_recurse(&entry, &dicts);
1643 while (dbus_message_iter_get_arg_type(&dicts) ==
1645 int err = read_route_dict(routes, &dicts);
1648 dbus_message_iter_next(&dicts);
1651 dbus_message_iter_next(&entry);
1654 dbus_message_iter_next(array);
1660 static gboolean property_changed(DBusConnection *conn,
1661 DBusMessage *message,
1664 const char *path = dbus_message_get_path(message);
1665 struct connection_data *data = NULL;
1666 DBusMessageIter iter, value;
1667 connman_bool_t ip_set = FALSE;
1671 const char *signature = DBUS_TYPE_STRING_AS_STRING
1672 DBUS_TYPE_VARIANT_AS_STRING;
1674 if (dbus_message_has_signature(message, signature) == FALSE) {
1675 connman_error("vpn property signature \"%s\" does not match "
1677 dbus_message_get_signature(message), signature);
1681 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1685 if (dbus_message_iter_init(message, &iter) == FALSE)
1688 dbus_message_iter_get_basic(&iter, &key);
1690 dbus_message_iter_next(&iter);
1691 dbus_message_iter_recurse(&iter, &value);
1695 if (g_str_equal(key, "State") == TRUE) {
1696 dbus_message_iter_get_basic(&value, &str);
1698 DBG("%s %s -> %s", data->path, data->state, str);
1700 if (g_str_equal(data->state, str) == TRUE)
1703 g_free(data->state);
1704 data->state = g_strdup(str);
1706 set_provider_state(data);
1707 } else if (g_str_equal(key, "Index") == TRUE) {
1708 dbus_message_iter_get_basic(&value, &data->index);
1709 connman_provider_set_index(data->provider, data->index);
1710 } else if (g_str_equal(key, "IPv4") == TRUE) {
1711 err = extract_ip(&value, AF_INET, data);
1713 } else if (g_str_equal(key, "IPv6") == TRUE) {
1714 err = extract_ip(&value, AF_INET6, data);
1716 } else if (g_str_equal(key, "ServerRoutes") == TRUE) {
1717 err = routes_changed(&value, data->server_routes);
1719 * Note that the vpnd will delay the route sending a bit
1720 * (in order to collect the routes from VPN client),
1721 * so we might have got the State changed property before
1722 * we got ServerRoutes. This means that we must try to set
1723 * the routes here because they would be left unset otherwise.
1726 set_routes(data->provider,
1727 CONNMAN_PROVIDER_ROUTE_SERVER);
1728 } else if (g_str_equal(key, "UserRoutes") == TRUE) {
1729 err = routes_changed(&value, data->user_routes);
1731 set_routes(data->provider,
1732 CONNMAN_PROVIDER_ROUTE_USER);
1733 } else if (g_str_equal(key, "Nameservers") == TRUE) {
1734 extract_nameservers(&value, data);
1737 if (ip_set == TRUE && err == 0) {
1738 err = connman_provider_set_ipaddress(data->provider, data->ip);
1740 DBG("setting provider IP address failed (%s/%d)",
1741 strerror(-err), -err);
1747 static int vpn_init(void)
1751 connection = connman_dbus_get_connection();
1752 if (connection == NULL)
1755 watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
1756 vpnd_created, vpnd_removed, &provider_driver, NULL);
1758 added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1759 VPN_MANAGER_INTERFACE,
1760 CONNECTION_ADDED, connection_added,
1761 &provider_driver, NULL);
1763 removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1764 VPN_MANAGER_INTERFACE,
1765 CONNECTION_REMOVED, connection_removed,
1768 property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1769 VPN_CONNECTION_INTERFACE,
1770 PROPERTY_CHANGED, property_changed,
1773 if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
1778 err = connman_provider_driver_register(&provider_driver);
1780 vpn_connections = g_hash_table_new_full(g_str_hash,
1782 g_free, connection_destroy);
1784 vpnd_created(connection, &provider_driver);
1790 g_dbus_remove_watch(connection, watch);
1791 g_dbus_remove_watch(connection, added_watch);
1792 g_dbus_remove_watch(connection, removed_watch);
1793 g_dbus_remove_watch(connection, property_watch);
1795 dbus_connection_unref(connection);
1800 static void vpn_exit(void)
1802 g_dbus_remove_watch(connection, watch);
1803 g_dbus_remove_watch(connection, added_watch);
1804 g_dbus_remove_watch(connection, removed_watch);
1805 g_dbus_remove_watch(connection, property_watch);
1807 connman_provider_driver_unregister(&provider_driver);
1809 if (vpn_connections != NULL)
1810 g_hash_table_destroy(vpn_connections);
1812 dbus_connection_unref(connection);
1815 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
1816 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)