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);
612 } else if (g_str_equal(key, "ServerRoutes") == TRUE) {
614 } else if (g_str_equal(key, "UserRoutes") == TRUE) {
617 if (dbus_message_iter_get_arg_type(&value) ==
619 dbus_message_iter_get_basic(&value, &str);
620 g_hash_table_replace(data->setting_strings,
621 g_strdup(key), g_strdup(str));
623 DBG("unknown key %s", key);
627 dbus_message_iter_next(properties);
631 g_hash_table_insert(vpn_connections, g_strdup(data->ident),
634 err = create_provider(data, user_data);
638 resolv_host_addr(data);
640 if (data->connect_pending == TRUE)
641 connect_provider(data, data->cb_data);
646 DBG("removing %s", data->ident);
647 g_hash_table_remove(vpn_connections, data->ident);
650 static void get_connections_reply(DBusPendingCall *call, void *user_data)
654 DBusMessageIter array, dict;
655 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
656 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
657 DBUS_TYPE_OBJECT_PATH_AS_STRING
658 DBUS_TYPE_ARRAY_AS_STRING
659 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
660 DBUS_TYPE_STRING_AS_STRING
661 DBUS_TYPE_VARIANT_AS_STRING
662 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
663 DBUS_STRUCT_END_CHAR_AS_STRING;
665 if (dbus_pending_call_get_completed(call) == FALSE)
670 reply = dbus_pending_call_steal_reply(call);
672 dbus_error_init(&error);
674 if (dbus_set_error_from_message(&error, reply) == TRUE) {
675 connman_error("%s", error.message);
676 dbus_error_free(&error);
680 if (dbus_message_has_signature(reply, signature) == FALSE) {
681 connman_error("vpnd signature \"%s\" does not match "
683 dbus_message_get_signature(reply), signature);
687 if (dbus_message_iter_init(reply, &array) == FALSE)
690 dbus_message_iter_recurse(&array, &dict);
692 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
693 DBusMessageIter value, properties;
696 dbus_message_iter_recurse(&dict, &value);
697 dbus_message_iter_get_basic(&value, &path);
699 dbus_message_iter_next(&value);
700 dbus_message_iter_recurse(&value, &properties);
702 add_connection(path, &properties, user_data);
704 dbus_message_iter_next(&dict);
708 dbus_message_unref(reply);
710 dbus_pending_call_unref(call);
713 static int get_connections(void *user_data)
715 DBusPendingCall *call;
716 DBusMessage *message;
720 message = dbus_message_new_method_call(VPN_SERVICE, "/",
721 VPN_MANAGER_INTERFACE,
726 if (dbus_connection_send_with_reply(connection, message,
727 &call, DBUS_TIMEOUT) == FALSE) {
728 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
730 dbus_message_unref(message);
735 dbus_message_unref(message);
739 dbus_pending_call_set_notify(call, get_connections_reply,
742 dbus_message_unref(message);
747 static int provider_probe(struct connman_provider *provider)
752 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
757 if (dbus_pending_call_get_completed(call) == FALSE)
762 reply = dbus_pending_call_steal_reply(call);
764 dbus_error_init(&error);
766 if (dbus_set_error_from_message(&error, reply) == TRUE) {
768 * If the returned error is NotFound, it means that we
769 * have actually removed the provider in vpnd already.
771 if (dbus_error_has_name(&error, CONNMAN_ERROR_INTERFACE
772 ".NotFound") == FALSE)
773 connman_error("%s", error.message);
775 dbus_error_free(&error);
778 dbus_message_unref(reply);
780 dbus_pending_call_unref(call);
783 static int provider_remove(struct connman_provider *provider)
785 DBusPendingCall *call;
786 DBusMessage *message;
787 struct connection_data *data;
789 data = connman_provider_get_data(provider);
791 DBG("provider %p data %p", provider, data);
794 * When provider.c:provider_remove() calls this function,
795 * it will remove the provider itself after the call.
796 * This means that we cannot use the provider pointer later
797 * as it is no longer valid.
799 data->provider = NULL;
801 message = dbus_message_new_method_call(VPN_SERVICE, "/",
802 VPN_MANAGER_INTERFACE,
807 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
810 if (dbus_connection_send_with_reply(connection, message,
811 &call, DBUS_TIMEOUT) == FALSE) {
812 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
814 dbus_message_unref(message);
819 dbus_message_unref(message);
823 dbus_pending_call_set_notify(call, remove_connection_reply,
826 dbus_message_unref(message);
831 static int provider_connect(struct connman_provider *provider)
833 struct connection_data *data;
835 data = connman_provider_get_data(provider);
839 return connect_provider(data, NULL);
842 static void disconnect_reply(DBusPendingCall *call, void *user_data)
847 if (dbus_pending_call_get_completed(call) == FALSE)
850 DBG("user %p", user_data);
852 reply = dbus_pending_call_steal_reply(call);
854 dbus_error_init(&error);
856 if (dbus_set_error_from_message(&error, reply) == TRUE) {
857 connman_error("%s", error.message);
858 dbus_error_free(&error);
863 dbus_message_unref(reply);
865 dbus_pending_call_unref(call);
868 static int disconnect_provider(struct connection_data *data)
870 DBusPendingCall *call;
871 DBusMessage *message;
873 DBG("data %p path %s", data, data->path);
875 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
876 VPN_CONNECTION_INTERFACE,
881 if (dbus_connection_send_with_reply(connection, message,
882 &call, DBUS_TIMEOUT) == FALSE) {
883 connman_error("Unable to call %s.%s()",
884 VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
885 dbus_message_unref(message);
890 dbus_message_unref(message);
894 dbus_pending_call_set_notify(call, disconnect_reply, NULL, NULL);
896 dbus_message_unref(message);
898 connman_provider_set_state(data->provider,
899 CONNMAN_PROVIDER_STATE_DISCONNECT);
901 * We return 0 here instead of -EINPROGRESS because
902 * __connman_service_disconnect() needs to return something
903 * to gdbus so that gdbus will not call Disconnect() more
904 * than once. This way we do not need to pass the dbus reply
905 * message around the code.
910 static int provider_disconnect(struct connman_provider *provider)
912 struct connection_data *data;
914 DBG("provider %p", provider);
916 data = connman_provider_get_data(provider);
920 if (g_str_equal(data->state, "ready") == TRUE ||
921 g_str_equal(data->state, "configuration") == TRUE)
922 return disconnect_provider(data);
927 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
931 DBusMessageIter iter;
932 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
935 struct connection_data *data;
936 struct config_create_data *cb_data = user_data;
938 if (dbus_pending_call_get_completed(call) == FALSE)
941 DBG("user %p", cb_data);
943 reply = dbus_pending_call_steal_reply(call);
945 dbus_error_init(&error);
947 if (dbus_set_error_from_message(&error, reply) == TRUE) {
948 connman_error("dbus error: %s", error.message);
949 dbus_error_free(&error);
953 if (dbus_message_has_signature(reply, signature) == FALSE) {
954 connman_error("vpn configuration signature \"%s\" does not "
955 "match expected \"%s\"",
956 dbus_message_get_signature(reply), signature);
960 if (dbus_message_iter_init(reply, &iter) == FALSE)
963 dbus_message_iter_get_basic(&iter, &path);
966 * Then try to connect the VPN as expected by ConnectProvider API
968 ident = get_ident(path);
970 data = g_hash_table_lookup(vpn_connections, ident);
973 * Someone removed the data. We cannot really continue.
975 DBG("Pending data not found for %s, cannot continue!", ident);
978 data->connect_pending = TRUE;
980 if (data->cb_data == NULL)
981 data->cb_data = cb_data;
983 DBG("Connection callback data already in use!");
986 * Connection is created in add_connections() after
987 * we have received the ConnectionAdded signal.
990 DBG("cb %p msg %p", data->cb_data,
991 data->cb_data ? data->cb_data->message : NULL);
995 dbus_message_unref(reply);
997 dbus_pending_call_unref(call);
1000 static void set_dbus_ident(char *ident)
1002 int i, len = strlen(ident);
1004 for (i = 0; i < len; i++) {
1005 if (ident[i] >= '0' && ident[i] <= '9')
1007 if (ident[i] >= 'a' && ident[i] <= 'z')
1009 if (ident[i] >= 'A' && ident[i] <= 'Z')
1015 static struct vpn_route *parse_user_route(const char *user_route)
1017 char *network, *netmask;
1018 struct vpn_route *route = NULL;
1019 int family = PF_UNSPEC;
1020 char **elems = g_strsplit(user_route, "/", 0);
1026 if (network == NULL || *network == '\0') {
1027 DBG("no network/netmask set");
1032 if (netmask != NULL && *netmask == '\0') {
1033 DBG("no netmask set");
1037 if (g_strrstr(network, ":") != NULL)
1039 else if (g_strrstr(network, ".") != NULL) {
1042 if (g_strrstr(netmask, ".") == NULL) {
1043 /* We have netmask length */
1045 struct in_addr netmask_in;
1046 unsigned char prefix_len = 32;
1048 if (netmask != NULL) {
1050 long int value = strtol(netmask, &ptr, 10);
1051 if (ptr != netmask && *ptr == '\0' &&
1056 addr = 0xffffffff << (32 - prefix_len);
1057 netmask_in.s_addr = htonl(addr);
1058 netmask = inet_ntoa(netmask_in);
1060 DBG("network %s netmask %s", network, netmask);
1064 route = g_try_new(struct vpn_route, 1);
1068 route->network = g_strdup(network);
1069 route->netmask = g_strdup(netmask);
1070 route->gateway = NULL;
1071 route->family = family;
1078 static GSList *get_user_networks(DBusMessageIter *array)
1080 DBusMessageIter entry;
1081 GSList *list = NULL;
1083 dbus_message_iter_recurse(array, &entry);
1085 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1087 struct vpn_route *route;
1089 dbus_message_iter_get_basic(&entry, &val);
1091 route = parse_user_route(val);
1093 list = g_slist_prepend(list, route);
1095 dbus_message_iter_next(&entry);
1101 static void append_route(DBusMessageIter *iter, void *user_data)
1103 struct vpn_route *route = user_data;
1104 DBusMessageIter item;
1107 connman_dbus_dict_open(iter, &item);
1112 if (route->family == AF_INET)
1114 else if (route->family == AF_INET6)
1118 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1119 DBUS_TYPE_INT32, &family);
1121 if (route->network != NULL)
1122 connman_dbus_dict_append_basic(&item, "Network",
1123 DBUS_TYPE_STRING, &route->network);
1125 if (route->netmask != NULL)
1126 connman_dbus_dict_append_basic(&item, "Netmask",
1127 DBUS_TYPE_STRING, &route->netmask);
1129 if (route->gateway != NULL)
1130 connman_dbus_dict_append_basic(&item, "Gateway",
1131 DBUS_TYPE_STRING, &route->gateway);
1134 connman_dbus_dict_close(iter, &item);
1137 static void append_routes(DBusMessageIter *iter, void *user_data)
1139 GSList *list, *routes = user_data;
1141 DBG("routes %p", routes);
1143 for (list = routes; list != NULL; list = g_slist_next(list)) {
1144 DBusMessageIter dict;
1145 struct vpn_route *route = list->data;
1147 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1149 append_route(&dict, route);
1150 dbus_message_iter_close_container(iter, &dict);
1154 static int create_configuration(DBusMessage *msg, connection_ready_cb callback)
1156 DBusMessage *new_msg = NULL;
1157 DBusPendingCall *call;
1158 DBusMessageIter iter, array, new_iter, new_dict;
1159 const char *type = NULL, *name = NULL;
1160 const char *host = NULL, *domain = NULL;
1161 char *ident, *me = NULL;
1164 struct connection_data *data;
1165 struct config_create_data *user_data = NULL;
1166 GSList *networks = NULL;
1169 * We copy the old message data into new message. We cannot
1170 * just use the old message as is because the user route
1171 * information is not in the same format in vpnd.
1173 new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1174 dbus_message_iter_init_append(new_msg, &new_iter);
1175 connman_dbus_dict_open(&new_iter, &new_dict);
1177 dbus_message_iter_init(msg, &iter);
1178 dbus_message_iter_recurse(&iter, &array);
1180 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1181 DBusMessageIter entry, value;
1186 dbus_message_iter_recurse(&array, &entry);
1187 dbus_message_iter_get_basic(&entry, &key);
1189 dbus_message_iter_next(&entry);
1190 dbus_message_iter_recurse(&entry, &value);
1192 value_type = dbus_message_iter_get_arg_type(&value);
1195 switch (value_type) {
1196 case DBUS_TYPE_STRING:
1197 dbus_message_iter_get_basic(&value, &item_value);
1199 if (g_str_equal(key, "Type") == TRUE) {
1200 type = (const char *)item_value;
1201 } else if (g_str_equal(key, "Name") == TRUE) {
1202 name = (const char *)item_value;
1203 } else if (g_str_equal(key, "Host") == TRUE) {
1204 host = (const char *)item_value;
1205 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1206 domain = (const char *)item_value;
1209 DBG("%s %s", key, (char *)item_value);
1211 if (item_value != NULL)
1212 connman_dbus_dict_append_basic(&new_dict, key,
1213 value_type, &item_value);
1215 case DBUS_TYPE_ARRAY:
1216 if (g_str_equal(key, "Networks") == TRUE) {
1217 networks = get_user_networks(&value);
1218 connman_dbus_dict_append_array(&new_dict,
1220 DBUS_TYPE_DICT_ENTRY,
1227 dbus_message_iter_next(&array);
1230 connman_dbus_dict_close(&new_iter, &new_dict);
1232 DBG("VPN type %s name %s host %s domain %s networks %p",
1233 type, name, host, domain, networks);
1235 if (host == NULL || domain == NULL) {
1240 if (type == NULL || name == NULL) {
1245 ident = g_strdup_printf("%s_%s", host, domain);
1246 set_dbus_ident(ident);
1248 DBG("ident %s", ident);
1250 data = g_hash_table_lookup(vpn_connections, ident);
1252 if (data->call != NULL || data->cb_data != NULL) {
1253 DBG("create configuration call already pending");
1258 char *path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1260 data = create_connection_data(path);
1268 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1272 * User called net.connman.Manager.ConnectProvider if we are here.
1273 * So use the data from original message in the new msg.
1275 me = g_strdup(dbus_message_get_destination(msg));
1277 dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1278 dbus_message_set_path(new_msg, "/");
1279 dbus_message_set_destination(new_msg, VPN_SERVICE);
1280 dbus_message_set_sender(new_msg, me);
1281 dbus_message_set_member(new_msg, "Create");
1283 user_data = g_try_new0(struct config_create_data, 1);
1284 if (user_data == NULL) {
1289 user_data->callback = callback;
1290 user_data->message = dbus_message_ref(msg);
1291 user_data->path = NULL;
1293 DBG("cb %p msg %p", user_data, msg);
1295 result = dbus_connection_send_with_reply(connection, new_msg,
1296 &call, DBUS_TIMEOUT);
1297 if (result == FALSE || call == NULL) {
1302 dbus_pending_call_set_notify(call, configuration_create_reply,
1307 if (new_msg != NULL)
1308 dbus_message_unref(new_msg);
1310 if (networks != NULL)
1311 g_slist_free_full(networks, destroy_route);
1317 static connman_bool_t check_host(char **hosts, char *host)
1324 for (i = 0; hosts[i] != NULL; i++) {
1325 if (g_strcmp0(hosts[i], host) == 0)
1332 static void set_route(struct connection_data *data, struct vpn_route *route)
1335 * If the VPN administrator/user has given a route to
1336 * VPN server, then we must discard that because the
1337 * server cannot be contacted via VPN tunnel.
1339 if (check_host(data->host_ip, route->network) == TRUE) {
1340 DBG("Discarding VPN route to %s via %s at index %d",
1341 route->network, route->gateway, data->index);
1345 if (route->family == AF_INET6) {
1346 unsigned char prefix_len = atoi(route->netmask);
1348 connman_inet_add_ipv6_network_route(data->index,
1353 connman_inet_add_network_route(data->index, route->network,
1359 static int set_routes(struct connman_provider *provider,
1360 enum connman_provider_route_type type)
1362 struct connection_data *data;
1363 GHashTableIter iter;
1364 gpointer value, key;
1366 DBG("provider %p", provider);
1368 data = connman_provider_get_data(provider);
1372 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1373 type == CONNMAN_PROVIDER_ROUTE_USER) {
1374 g_hash_table_iter_init(&iter, data->user_routes);
1376 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1377 set_route(data, value);
1380 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1381 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1382 g_hash_table_iter_init(&iter, data->server_routes);
1384 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1385 set_route(data, value);
1391 static connman_bool_t check_routes(struct connman_provider *provider)
1393 struct connection_data *data;
1395 DBG("provider %p", provider);
1397 data = connman_provider_get_data(provider);
1401 if (data->user_routes != NULL &&
1402 g_hash_table_size(data->user_routes) > 0)
1405 if (data->server_routes != NULL &&
1406 g_hash_table_size(data->server_routes) > 0)
1412 static struct connman_provider_driver provider_driver = {
1414 .type = CONNMAN_PROVIDER_TYPE_VPN,
1415 .probe = provider_probe,
1416 .remove = provider_remove,
1417 .connect = provider_connect,
1418 .disconnect = provider_disconnect,
1419 .set_property = set_string,
1420 .get_property = get_string,
1421 .create = create_configuration,
1422 .set_routes = set_routes,
1423 .check_routes = check_routes,
1426 static void destroy_provider(struct connection_data *data)
1428 DBG("data %p", data);
1430 if (g_str_equal(data->state, "ready") == TRUE ||
1431 g_str_equal(data->state, "configuration") == TRUE)
1432 connman_provider_disconnect(data->provider);
1434 if (data->call != NULL)
1435 dbus_pending_call_cancel(data->call);
1437 connman_provider_put(data->provider);
1439 data->provider = NULL;
1442 static void connection_destroy(gpointer hash_data)
1444 struct connection_data *data = hash_data;
1446 DBG("data %p", data);
1448 if (data->provider != NULL)
1449 destroy_provider(data);
1452 g_free(data->ident);
1453 g_free(data->state);
1457 g_free(data->domain);
1458 g_hash_table_destroy(data->server_routes);
1459 g_hash_table_destroy(data->user_routes);
1460 g_strfreev(data->nameservers);
1461 g_hash_table_destroy(data->setting_strings);
1462 connman_ipaddress_free(data->ip);
1464 cancel_host_resolv(data);
1469 static void vpnd_created(DBusConnection *conn, void *user_data)
1471 DBG("connection %p", conn);
1473 get_connections(user_data);
1476 static void vpnd_removed(DBusConnection *conn, void *user_data)
1478 DBG("connection %p", conn);
1480 g_hash_table_remove_all(vpn_connections);
1483 static void remove_connection(DBusConnection *conn, const char *path)
1485 DBG("path %s", path);
1487 g_hash_table_remove(vpn_connections, get_ident(path));
1490 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1494 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1496 if (dbus_message_has_signature(message, signature) == FALSE) {
1497 connman_error("vpn removed signature \"%s\" does not match "
1499 dbus_message_get_signature(message), signature);
1503 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1505 remove_connection(conn, path);
1509 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1512 DBusMessageIter iter, properties;
1514 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1515 DBUS_TYPE_ARRAY_AS_STRING
1516 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1517 DBUS_TYPE_STRING_AS_STRING
1518 DBUS_TYPE_VARIANT_AS_STRING
1519 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1521 if (dbus_message_has_signature(message, signature) == FALSE) {
1522 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1523 "match expected \"%s\"",
1524 dbus_message_get_signature(message), signature);
1530 if (dbus_message_iter_init(message, &iter) == FALSE)
1533 dbus_message_iter_get_basic(&iter, &path);
1535 dbus_message_iter_next(&iter);
1536 dbus_message_iter_recurse(&iter, &properties);
1538 add_connection(path, &properties, user_data);
1543 static int save_route(GHashTable *routes, int family, const char *network,
1544 const char *netmask, const char *gateway)
1546 struct vpn_route *route;
1547 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1549 DBG("family %d network %s netmask %s", family, network, netmask);
1551 route = g_hash_table_lookup(routes, key);
1552 if (route == NULL) {
1553 route = g_try_new0(struct vpn_route, 1);
1554 if (route == NULL) {
1555 connman_error("out of memory");
1559 route->family = family;
1560 route->network = g_strdup(network);
1561 route->netmask = g_strdup(netmask);
1562 route->gateway = g_strdup(gateway);
1564 g_hash_table_replace(routes, key, route);
1571 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1573 DBusMessageIter dict;
1574 const char *network, *netmask, *gateway;
1577 dbus_message_iter_recurse(dicts, &dict);
1579 network = netmask = gateway = NULL;
1582 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1584 DBusMessageIter entry, value;
1587 dbus_message_iter_recurse(&dict, &entry);
1588 dbus_message_iter_get_basic(&entry, &key);
1590 dbus_message_iter_next(&entry);
1591 dbus_message_iter_recurse(&entry, &value);
1593 if (g_str_equal(key, "ProtocolFamily") == TRUE) {
1595 dbus_message_iter_get_basic(&value, &pf);
1604 DBG("family %d", family);
1605 } else if (g_str_equal(key, "Netmask") == TRUE) {
1606 dbus_message_iter_get_basic(&value, &netmask);
1607 DBG("netmask %s", netmask);
1608 } else if (g_str_equal(key, "Network") == TRUE) {
1609 dbus_message_iter_get_basic(&value, &network);
1610 DBG("host %s", network);
1611 } else if (g_str_equal(key, "Gateway") == TRUE) {
1612 dbus_message_iter_get_basic(&value, &gateway);
1613 DBG("gateway %s", gateway);
1616 dbus_message_iter_next(&dict);
1619 if (netmask == NULL || network == NULL || gateway == NULL) {
1620 DBG("Value missing.");
1624 return save_route(routes, family, network, netmask, gateway);
1627 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1629 DBusMessageIter entry;
1632 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1633 DBG("Expecting array, ignoring routes.");
1637 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1639 dbus_message_iter_recurse(array, &entry);
1641 while (dbus_message_iter_get_arg_type(&entry) ==
1643 DBusMessageIter dicts;
1645 dbus_message_iter_recurse(&entry, &dicts);
1647 while (dbus_message_iter_get_arg_type(&dicts) ==
1649 int err = read_route_dict(routes, &dicts);
1652 dbus_message_iter_next(&dicts);
1655 dbus_message_iter_next(&entry);
1658 dbus_message_iter_next(array);
1664 static gboolean property_changed(DBusConnection *conn,
1665 DBusMessage *message,
1668 const char *path = dbus_message_get_path(message);
1669 struct connection_data *data = NULL;
1670 DBusMessageIter iter, value;
1671 connman_bool_t ip_set = FALSE;
1675 const char *signature = DBUS_TYPE_STRING_AS_STRING
1676 DBUS_TYPE_VARIANT_AS_STRING;
1678 if (dbus_message_has_signature(message, signature) == FALSE) {
1679 connman_error("vpn property signature \"%s\" does not match "
1681 dbus_message_get_signature(message), signature);
1685 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1689 if (dbus_message_iter_init(message, &iter) == FALSE)
1692 dbus_message_iter_get_basic(&iter, &key);
1694 dbus_message_iter_next(&iter);
1695 dbus_message_iter_recurse(&iter, &value);
1699 if (g_str_equal(key, "State") == TRUE) {
1700 dbus_message_iter_get_basic(&value, &str);
1702 DBG("%s %s -> %s", data->path, data->state, str);
1704 if (g_str_equal(data->state, str) == TRUE)
1707 g_free(data->state);
1708 data->state = g_strdup(str);
1710 set_provider_state(data);
1711 } else if (g_str_equal(key, "Index") == TRUE) {
1712 dbus_message_iter_get_basic(&value, &data->index);
1713 connman_provider_set_index(data->provider, data->index);
1714 } else if (g_str_equal(key, "IPv4") == TRUE) {
1715 err = extract_ip(&value, AF_INET, data);
1717 } else if (g_str_equal(key, "IPv6") == TRUE) {
1718 err = extract_ip(&value, AF_INET6, data);
1720 } else if (g_str_equal(key, "ServerRoutes") == TRUE) {
1721 err = routes_changed(&value, data->server_routes);
1723 * Note that the vpnd will delay the route sending a bit
1724 * (in order to collect the routes from VPN client),
1725 * so we might have got the State changed property before
1726 * we got ServerRoutes. This means that we must try to set
1727 * the routes here because they would be left unset otherwise.
1730 set_routes(data->provider,
1731 CONNMAN_PROVIDER_ROUTE_SERVER);
1732 } else if (g_str_equal(key, "UserRoutes") == TRUE) {
1733 err = routes_changed(&value, data->user_routes);
1735 set_routes(data->provider,
1736 CONNMAN_PROVIDER_ROUTE_USER);
1737 } else if (g_str_equal(key, "Nameservers") == TRUE) {
1738 extract_nameservers(&value, data);
1741 if (ip_set == TRUE && err == 0) {
1742 err = connman_provider_set_ipaddress(data->provider, data->ip);
1744 DBG("setting provider IP address failed (%s/%d)",
1745 strerror(-err), -err);
1751 static int vpn_init(void)
1755 connection = connman_dbus_get_connection();
1756 if (connection == NULL)
1759 watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
1760 vpnd_created, vpnd_removed, &provider_driver, NULL);
1762 added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1763 VPN_MANAGER_INTERFACE,
1764 CONNECTION_ADDED, connection_added,
1765 &provider_driver, NULL);
1767 removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1768 VPN_MANAGER_INTERFACE,
1769 CONNECTION_REMOVED, connection_removed,
1772 property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1773 VPN_CONNECTION_INTERFACE,
1774 PROPERTY_CHANGED, property_changed,
1777 if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
1782 err = connman_provider_driver_register(&provider_driver);
1784 vpn_connections = g_hash_table_new_full(g_str_hash,
1786 g_free, connection_destroy);
1788 vpnd_created(connection, &provider_driver);
1794 g_dbus_remove_watch(connection, watch);
1795 g_dbus_remove_watch(connection, added_watch);
1796 g_dbus_remove_watch(connection, removed_watch);
1797 g_dbus_remove_watch(connection, property_watch);
1799 dbus_connection_unref(connection);
1804 static void vpn_exit(void)
1806 g_dbus_remove_watch(connection, watch);
1807 g_dbus_remove_watch(connection, added_watch);
1808 g_dbus_remove_watch(connection, removed_watch);
1809 g_dbus_remove_watch(connection, property_watch);
1811 connman_provider_driver_unregister(&provider_driver);
1813 if (vpn_connections != NULL)
1814 g_hash_table_destroy(vpn_connections);
1816 dbus_connection_unref(connection);
1819 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
1820 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)