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;
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") == TRUE) {
109 data->type = g_strdup(value);
110 } else if (g_str_equal(key, "Name") == TRUE) {
112 data->name = g_strdup(value);
113 } else if (g_str_equal(key, "Host") == TRUE) {
115 data->host = g_strdup(value);
116 } else if (g_str_equal(key, "VPN.Domain") == TRUE ||
117 g_str_equal(key, "Domain") == TRUE) {
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") == TRUE)
139 else if (g_str_equal(key, "Name") == TRUE)
141 else if (g_str_equal(key, "Host") == TRUE)
143 else if (g_str_equal(key, "HostIP") == TRUE) {
144 if (data->host_ip == NULL ||
145 data->host_ip[0] == NULL)
148 return data->host_ip[0];
149 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
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 != NULL &&
197 g_strv_length(results) > 0)
198 data->host_ip = g_strdupv(results);
201 * We cannot unref the resolver here as resolv struct is manipulated
202 * by gresolv.c after we return from this callback.
204 g_timeout_add_seconds(0, remove_resolv, data);
209 static void resolv_host_addr(struct connection_data *data)
211 if (data->host == NULL)
214 if (connman_inet_check_ipaddress(data->host) > 0)
217 if (data->host_ip != NULL)
220 data->resolv = g_resolv_new(0);
221 if (data->resolv == NULL) {
222 DBG("Cannot resolv %s", data->host);
226 DBG("Trying to resolv %s", data->host);
228 data->resolv_id = g_resolv_lookup_hostname(data->resolv, data->host,
229 resolv_result, data);
232 static void free_config_cb_data(struct config_create_data *cb_data)
237 g_free(cb_data->path);
238 cb_data->path = NULL;
240 if (cb_data->message != NULL) {
241 dbus_message_unref(cb_data->message);
242 cb_data->message = NULL;
245 cb_data->callback = NULL;
250 static void set_provider_state(struct connection_data *data)
252 enum connman_provider_state state = CONNMAN_PROVIDER_STATE_UNKNOWN;
255 DBG("provider %p new state %s", data->provider, data->state);
257 if (g_str_equal(data->state, "ready") == TRUE) {
258 state = CONNMAN_PROVIDER_STATE_READY;
260 } else if (g_str_equal(data->state, "configuration") == TRUE) {
261 state = CONNMAN_PROVIDER_STATE_CONNECT;
262 } else if (g_str_equal(data->state, "idle") == TRUE) {
263 state = CONNMAN_PROVIDER_STATE_IDLE;
264 } else if (g_str_equal(data->state, "disconnect") == TRUE) {
266 state = CONNMAN_PROVIDER_STATE_DISCONNECT;
268 } else if (g_str_equal(data->state, "failure") == TRUE) {
270 state = CONNMAN_PROVIDER_STATE_FAILURE;
274 connman_provider_set_state(data->provider, state);
278 if (data->cb_data != NULL)
279 data->cb_data->callback(data->cb_data->message,
282 connman_provider_set_state(data->provider, state);
284 free_config_cb_data(data->cb_data);
285 data->cb_data = NULL;
288 static int create_provider(struct connection_data *data, void *user_data)
290 struct connman_provider_driver *driver = user_data;
293 DBG("%s", data->path);
295 data->provider = connman_provider_get(data->ident);
296 if (data->provider == NULL)
299 DBG("provider %p name %s", data->provider, data->name);
301 connman_provider_set_data(data->provider, data);
302 connman_provider_set_driver(data->provider, driver);
304 err = connman_provider_create_service(data->provider);
306 connman_provider_set_immutable(data->provider, data->immutable);
307 if (g_str_equal(data->state, "ready") == TRUE) {
308 connman_provider_set_index(data->provider,
310 if (data->ip != NULL)
311 connman_provider_set_ipaddress(data->provider,
315 set_provider_state(data);
321 static void destroy_route(gpointer user_data)
323 struct vpn_route *route = user_data;
325 g_free(route->network);
326 g_free(route->netmask);
327 g_free(route->gateway);
331 static struct connection_data *create_connection_data(const char *path)
333 struct connection_data *data;
335 data = g_try_new0(struct connection_data, 1);
339 DBG("path %s", path);
341 data->path = g_strdup(path);
342 data->ident = g_strdup(get_ident(path));
345 data->setting_strings = g_hash_table_new_full(g_str_hash,
346 g_str_equal, g_free, g_free);
348 data->server_routes = g_hash_table_new_full(g_direct_hash,
349 g_str_equal, g_free, destroy_route);
350 data->user_routes = g_hash_table_new_full(g_str_hash,
351 g_str_equal, g_free, destroy_route);
356 static int extract_ip(DBusMessageIter *array, int family,
357 struct connection_data *data)
359 DBusMessageIter dict;
360 char *address = NULL, *gateway = NULL, *netmask = NULL, *peer = NULL;
361 unsigned char prefix_len;
363 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
366 dbus_message_iter_recurse(array, &dict);
368 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
369 DBusMessageIter entry, value;
372 dbus_message_iter_recurse(&dict, &entry);
373 dbus_message_iter_get_basic(&entry, &key);
375 dbus_message_iter_next(&entry);
376 dbus_message_iter_recurse(&entry, &value);
378 if (g_str_equal(key, "Address") == TRUE) {
379 dbus_message_iter_get_basic(&value, &address);
380 DBG("address %s", address);
381 } else if (g_str_equal(key, "Netmask") == TRUE) {
382 dbus_message_iter_get_basic(&value, &netmask);
383 DBG("netmask %s", netmask);
384 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
385 dbus_message_iter_get_basic(&value, &netmask);
386 DBG("prefix length %s", netmask);
387 } else if (g_str_equal(key, "Peer") == TRUE) {
388 dbus_message_iter_get_basic(&value, &peer);
389 DBG("peer %s", peer);
390 } else if (g_str_equal(key, "Gateway") == TRUE) {
391 dbus_message_iter_get_basic(&value, &gateway);
392 DBG("gateway %s", gateway);
395 dbus_message_iter_next(&dict);
398 data->ip = connman_ipaddress_alloc(family);
399 if (data->ip == NULL)
404 connman_ipaddress_set_ipv4(data->ip, address, netmask,
408 prefix_len = atoi(netmask);
409 connman_ipaddress_set_ipv6(data->ip, address, prefix_len,
416 connman_ipaddress_set_peer(data->ip, peer);
421 static int extract_nameservers(DBusMessageIter *array,
422 struct connection_data *data)
424 DBusMessageIter entry;
425 char **nameservers = NULL;
428 dbus_message_iter_recurse(array, &entry);
430 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
431 const char *nameserver;
433 dbus_message_iter_get_basic(&entry, &nameserver);
435 nameservers = g_try_renew(char *, nameservers, i + 2);
436 if (nameservers == NULL)
439 DBG("[%d] %s", i, nameserver);
441 nameservers[i] = g_strdup(nameserver);
442 if (nameservers[i] == NULL)
445 nameservers[++i] = NULL;
447 dbus_message_iter_next(&entry);
450 g_strfreev(data->nameservers);
451 data->nameservers = nameservers;
456 static int errorstr2val(const char *error) {
457 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".InProgress") == 0)
460 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".AlreadyConnected") == 0)
463 return -ECONNREFUSED;
466 static void connect_reply(DBusPendingCall *call, void *user_data)
470 struct connection_data *data = user_data;
471 struct config_create_data *cb_data = data->cb_data;
473 if (dbus_pending_call_get_completed(call) == FALSE)
476 DBG("user_data %p path %s", user_data, cb_data ? cb_data->path : NULL);
478 reply = dbus_pending_call_steal_reply(call);
480 dbus_error_init(&error);
482 if (dbus_set_error_from_message(&error, reply) == TRUE) {
483 int err = errorstr2val(error.name);
484 if (err != -EINPROGRESS) {
485 connman_error("Connect reply: %s (%s)", error.message,
487 dbus_error_free(&error);
489 DBG("data %p cb_data %p", data, cb_data);
490 if (cb_data != NULL) {
491 cb_data->callback(cb_data->message, err, NULL);
492 free_config_cb_data(cb_data);
493 data->cb_data = NULL;
497 dbus_error_free(&error);
501 * The vpn connection is up when we get a "ready" state
502 * property so at this point we do nothing for the provider
507 dbus_message_unref(reply);
509 dbus_pending_call_unref(call);
512 static int connect_provider(struct connection_data *data, void *user_data)
514 DBusPendingCall *call;
515 DBusMessage *message;
516 struct config_create_data *cb_data = user_data;
518 DBG("data %p user %p path %s", data, cb_data, data->path);
520 data->connect_pending = FALSE;
522 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
523 VPN_CONNECTION_INTERFACE,
528 if (dbus_connection_send_with_reply(connection, message,
529 &call, DBUS_TIMEOUT) == FALSE) {
530 connman_error("Unable to call %s.%s()",
531 VPN_CONNECTION_INTERFACE, VPN_CONNECT);
532 dbus_message_unref(message);
537 dbus_message_unref(message);
541 if (cb_data != NULL) {
542 g_free(cb_data->path);
543 cb_data->path = g_strdup(data->path);
546 dbus_pending_call_set_notify(call, connect_reply, data, NULL);
548 dbus_message_unref(message);
553 static void add_connection(const char *path, DBusMessageIter *properties,
556 struct connection_data *data;
558 char *ident = get_ident(path);
559 connman_bool_t found = FALSE;
561 data = g_hash_table_lookup(vpn_connections, ident);
564 * We might have a dummy connection struct here that
565 * was created by configuration_create_reply() so in
566 * that case just continue.
568 if (data->connect_pending == FALSE)
573 data = create_connection_data(path);
578 DBG("data %p path %s", data, path);
580 while (dbus_message_iter_get_arg_type(properties) ==
581 DBUS_TYPE_DICT_ENTRY) {
582 DBusMessageIter entry, value;
586 dbus_message_iter_recurse(properties, &entry);
587 dbus_message_iter_get_basic(&entry, &key);
589 dbus_message_iter_next(&entry);
590 dbus_message_iter_recurse(&entry, &value);
592 if (g_str_equal(key, "State") == TRUE) {
593 dbus_message_iter_get_basic(&value, &str);
594 DBG("state %s -> %s", data->state, str);
595 data->state = g_strdup(str);
596 } else if (g_str_equal(key, "IPv4") == TRUE) {
597 extract_ip(&value, AF_INET, data);
598 } else if (g_str_equal(key, "IPv6") == TRUE) {
599 extract_ip(&value, AF_INET6, data);
600 } else if (g_str_equal(key, "Name") == TRUE) {
601 dbus_message_iter_get_basic(&value, &str);
602 data->name = g_strdup(str);
603 } else if (g_str_equal(key, "Type") == TRUE) {
604 dbus_message_iter_get_basic(&value, &str);
605 data->type = g_strdup(str);
606 } else if (g_str_equal(key, "Immutable") == TRUE) {
607 dbus_message_iter_get_basic(&value, &data->immutable);
608 } else if (g_str_equal(key, "Host") == TRUE) {
609 dbus_message_iter_get_basic(&value, &str);
610 data->host = g_strdup(str);
611 } else if (g_str_equal(key, "Domain") == TRUE) {
612 dbus_message_iter_get_basic(&value, &str);
613 g_free(data->domain);
614 data->domain = g_strdup(str);
615 } else if (g_str_equal(key, "Nameservers") == TRUE) {
616 extract_nameservers(&value, data);
617 } else if (g_str_equal(key, "Index") == TRUE) {
618 dbus_message_iter_get_basic(&value, &data->index);
619 } else if (g_str_equal(key, "ServerRoutes") == TRUE) {
621 } else if (g_str_equal(key, "UserRoutes") == TRUE) {
624 if (dbus_message_iter_get_arg_type(&value) ==
626 dbus_message_iter_get_basic(&value, &str);
627 g_hash_table_replace(data->setting_strings,
628 g_strdup(key), g_strdup(str));
630 DBG("unknown key %s", key);
634 dbus_message_iter_next(properties);
638 g_hash_table_insert(vpn_connections, g_strdup(data->ident),
641 err = create_provider(data, user_data);
645 resolv_host_addr(data);
647 if (data->nameservers != NULL)
648 connman_provider_set_nameservers(data->provider,
651 if (data->domain != NULL)
652 connman_provider_set_domain(data->provider,
655 if (data->connect_pending == TRUE)
656 connect_provider(data, data->cb_data);
661 DBG("removing %s", data->ident);
662 g_hash_table_remove(vpn_connections, data->ident);
665 static void get_connections_reply(DBusPendingCall *call, void *user_data)
669 DBusMessageIter array, dict;
670 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
671 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
672 DBUS_TYPE_OBJECT_PATH_AS_STRING
673 DBUS_TYPE_ARRAY_AS_STRING
674 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
675 DBUS_TYPE_STRING_AS_STRING
676 DBUS_TYPE_VARIANT_AS_STRING
677 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
678 DBUS_STRUCT_END_CHAR_AS_STRING;
680 if (dbus_pending_call_get_completed(call) == FALSE)
685 reply = dbus_pending_call_steal_reply(call);
687 dbus_error_init(&error);
689 if (dbus_set_error_from_message(&error, reply) == TRUE) {
690 connman_error("%s", error.message);
691 dbus_error_free(&error);
695 if (dbus_message_has_signature(reply, signature) == FALSE) {
696 connman_error("vpnd signature \"%s\" does not match "
698 dbus_message_get_signature(reply), signature);
702 if (dbus_message_iter_init(reply, &array) == FALSE)
705 dbus_message_iter_recurse(&array, &dict);
707 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
708 DBusMessageIter value, properties;
711 dbus_message_iter_recurse(&dict, &value);
712 dbus_message_iter_get_basic(&value, &path);
714 dbus_message_iter_next(&value);
715 dbus_message_iter_recurse(&value, &properties);
717 add_connection(path, &properties, user_data);
719 dbus_message_iter_next(&dict);
723 dbus_message_unref(reply);
725 dbus_pending_call_unref(call);
728 static int get_connections(void *user_data)
730 DBusPendingCall *call;
731 DBusMessage *message;
735 message = dbus_message_new_method_call(VPN_SERVICE, "/",
736 VPN_MANAGER_INTERFACE,
741 if (dbus_connection_send_with_reply(connection, message,
742 &call, DBUS_TIMEOUT) == FALSE) {
743 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
745 dbus_message_unref(message);
750 dbus_message_unref(message);
754 dbus_pending_call_set_notify(call, get_connections_reply,
757 dbus_message_unref(message);
762 static int provider_probe(struct connman_provider *provider)
767 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
772 if (dbus_pending_call_get_completed(call) == FALSE)
777 reply = dbus_pending_call_steal_reply(call);
779 dbus_error_init(&error);
781 if (dbus_set_error_from_message(&error, reply) == TRUE) {
783 * If the returned error is NotFound, it means that we
784 * have actually removed the provider in vpnd already.
786 if (dbus_error_has_name(&error, CONNMAN_ERROR_INTERFACE
787 ".NotFound") == FALSE)
788 connman_error("%s", error.message);
790 dbus_error_free(&error);
793 dbus_message_unref(reply);
795 dbus_pending_call_unref(call);
798 static int provider_remove(struct connman_provider *provider)
800 DBusPendingCall *call;
801 DBusMessage *message;
802 struct connection_data *data;
804 data = connman_provider_get_data(provider);
806 DBG("provider %p data %p", provider, data);
810 * This means the provider is already removed,
811 * just ignore the dbus in this case.
817 * When provider.c:provider_remove() calls this function,
818 * it will remove the provider itself after the call.
819 * This means that we cannot use the provider pointer later
820 * as it is no longer valid.
822 data->provider = NULL;
824 message = dbus_message_new_method_call(VPN_SERVICE, "/",
825 VPN_MANAGER_INTERFACE,
830 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
833 if (dbus_connection_send_with_reply(connection, message,
834 &call, DBUS_TIMEOUT) == FALSE) {
835 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
837 dbus_message_unref(message);
842 dbus_message_unref(message);
846 dbus_pending_call_set_notify(call, remove_connection_reply,
849 dbus_message_unref(message);
854 static int provider_connect(struct connman_provider *provider)
856 struct connection_data *data;
858 data = connman_provider_get_data(provider);
862 return connect_provider(data, NULL);
865 static void disconnect_reply(DBusPendingCall *call, void *user_data)
870 if (dbus_pending_call_get_completed(call) == FALSE)
873 DBG("user %p", user_data);
875 reply = dbus_pending_call_steal_reply(call);
877 dbus_error_init(&error);
879 if (dbus_set_error_from_message(&error, reply) == TRUE) {
880 connman_error("%s", error.message);
881 dbus_error_free(&error);
886 dbus_message_unref(reply);
888 dbus_pending_call_unref(call);
891 static int disconnect_provider(struct connection_data *data)
893 DBusPendingCall *call;
894 DBusMessage *message;
896 DBG("data %p path %s", data, data->path);
898 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
899 VPN_CONNECTION_INTERFACE,
904 if (dbus_connection_send_with_reply(connection, message,
905 &call, DBUS_TIMEOUT) == FALSE) {
906 connman_error("Unable to call %s.%s()",
907 VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
908 dbus_message_unref(message);
913 dbus_message_unref(message);
917 dbus_pending_call_set_notify(call, disconnect_reply, NULL, NULL);
919 dbus_message_unref(message);
921 connman_provider_set_state(data->provider,
922 CONNMAN_PROVIDER_STATE_DISCONNECT);
924 * We return 0 here instead of -EINPROGRESS because
925 * __connman_service_disconnect() needs to return something
926 * to gdbus so that gdbus will not call Disconnect() more
927 * than once. This way we do not need to pass the dbus reply
928 * message around the code.
933 static int provider_disconnect(struct connman_provider *provider)
935 struct connection_data *data;
937 DBG("provider %p", provider);
939 data = connman_provider_get_data(provider);
943 if (g_str_equal(data->state, "ready") == TRUE ||
944 g_str_equal(data->state, "configuration") == TRUE)
945 return disconnect_provider(data);
950 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
954 DBusMessageIter iter;
955 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
958 struct connection_data *data;
959 struct config_create_data *cb_data = user_data;
961 if (dbus_pending_call_get_completed(call) == FALSE)
964 DBG("user %p", cb_data);
966 reply = dbus_pending_call_steal_reply(call);
968 dbus_error_init(&error);
970 if (dbus_set_error_from_message(&error, reply) == TRUE) {
971 connman_error("dbus error: %s", error.message);
972 dbus_error_free(&error);
976 if (dbus_message_has_signature(reply, signature) == FALSE) {
977 connman_error("vpn configuration signature \"%s\" does not "
978 "match expected \"%s\"",
979 dbus_message_get_signature(reply), signature);
983 if (dbus_message_iter_init(reply, &iter) == FALSE)
986 dbus_message_iter_get_basic(&iter, &path);
989 * Then try to connect the VPN as expected by ConnectProvider API
991 ident = get_ident(path);
993 data = g_hash_table_lookup(vpn_connections, ident);
996 * Someone removed the data. We cannot really continue.
998 DBG("Pending data not found for %s, cannot continue!", ident);
1001 data->connect_pending = TRUE;
1003 if (data->cb_data == NULL)
1004 data->cb_data = cb_data;
1006 DBG("Connection callback data already in use!");
1009 * Connection is created in add_connections() after
1010 * we have received the ConnectionAdded signal.
1013 DBG("cb %p msg %p", data->cb_data,
1014 data->cb_data ? data->cb_data->message : NULL);
1018 dbus_message_unref(reply);
1020 dbus_pending_call_unref(call);
1023 static void set_dbus_ident(char *ident)
1025 int i, len = strlen(ident);
1027 for (i = 0; i < len; i++) {
1028 if (ident[i] >= '0' && ident[i] <= '9')
1030 if (ident[i] >= 'a' && ident[i] <= 'z')
1032 if (ident[i] >= 'A' && ident[i] <= 'Z')
1038 static struct vpn_route *parse_user_route(const char *user_route)
1040 char *network, *netmask;
1041 struct vpn_route *route = NULL;
1042 int family = PF_UNSPEC;
1043 char **elems = g_strsplit(user_route, "/", 0);
1049 if (network == NULL || *network == '\0') {
1050 DBG("no network/netmask set");
1055 if (netmask != NULL && *netmask == '\0') {
1056 DBG("no netmask set");
1060 if (g_strrstr(network, ":") != NULL)
1062 else if (g_strrstr(network, ".") != NULL) {
1065 if (g_strrstr(netmask, ".") == NULL) {
1066 /* We have netmask length */
1068 struct in_addr netmask_in;
1069 unsigned char prefix_len = 32;
1071 if (netmask != NULL) {
1073 long int value = strtol(netmask, &ptr, 10);
1074 if (ptr != netmask && *ptr == '\0' &&
1079 addr = 0xffffffff << (32 - prefix_len);
1080 netmask_in.s_addr = htonl(addr);
1081 netmask = inet_ntoa(netmask_in);
1083 DBG("network %s netmask %s", network, netmask);
1087 route = g_try_new(struct vpn_route, 1);
1091 route->network = g_strdup(network);
1092 route->netmask = g_strdup(netmask);
1093 route->gateway = NULL;
1094 route->family = family;
1101 static GSList *get_user_networks(DBusMessageIter *array)
1103 DBusMessageIter entry;
1104 GSList *list = NULL;
1106 dbus_message_iter_recurse(array, &entry);
1108 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1110 struct vpn_route *route;
1112 dbus_message_iter_get_basic(&entry, &val);
1114 route = parse_user_route(val);
1116 list = g_slist_prepend(list, route);
1118 dbus_message_iter_next(&entry);
1124 static void append_route(DBusMessageIter *iter, void *user_data)
1126 struct vpn_route *route = user_data;
1127 DBusMessageIter item;
1130 connman_dbus_dict_open(iter, &item);
1135 if (route->family == AF_INET)
1137 else if (route->family == AF_INET6)
1141 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1142 DBUS_TYPE_INT32, &family);
1144 if (route->network != NULL)
1145 connman_dbus_dict_append_basic(&item, "Network",
1146 DBUS_TYPE_STRING, &route->network);
1148 if (route->netmask != NULL)
1149 connman_dbus_dict_append_basic(&item, "Netmask",
1150 DBUS_TYPE_STRING, &route->netmask);
1152 if (route->gateway != NULL)
1153 connman_dbus_dict_append_basic(&item, "Gateway",
1154 DBUS_TYPE_STRING, &route->gateway);
1157 connman_dbus_dict_close(iter, &item);
1160 static void append_routes(DBusMessageIter *iter, void *user_data)
1162 GSList *list, *routes = user_data;
1164 DBG("routes %p", routes);
1166 for (list = routes; list != NULL; list = g_slist_next(list)) {
1167 DBusMessageIter dict;
1168 struct vpn_route *route = list->data;
1170 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1172 append_route(&dict, route);
1173 dbus_message_iter_close_container(iter, &dict);
1177 static int create_configuration(DBusMessage *msg, connection_ready_cb callback)
1179 DBusMessage *new_msg = NULL;
1180 DBusPendingCall *call;
1181 DBusMessageIter iter, array, new_iter, new_dict;
1182 const char *type = NULL, *name = NULL;
1183 const char *host = NULL, *domain = NULL;
1184 char *ident, *me = NULL;
1187 struct connection_data *data;
1188 struct config_create_data *user_data = NULL;
1189 GSList *networks = NULL;
1192 * We copy the old message data into new message. We cannot
1193 * just use the old message as is because the user route
1194 * information is not in the same format in vpnd.
1196 new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1197 dbus_message_iter_init_append(new_msg, &new_iter);
1198 connman_dbus_dict_open(&new_iter, &new_dict);
1200 dbus_message_iter_init(msg, &iter);
1201 dbus_message_iter_recurse(&iter, &array);
1203 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1204 DBusMessageIter entry, value;
1209 dbus_message_iter_recurse(&array, &entry);
1210 dbus_message_iter_get_basic(&entry, &key);
1212 dbus_message_iter_next(&entry);
1213 dbus_message_iter_recurse(&entry, &value);
1215 value_type = dbus_message_iter_get_arg_type(&value);
1218 switch (value_type) {
1219 case DBUS_TYPE_STRING:
1220 dbus_message_iter_get_basic(&value, &item_value);
1222 if (g_str_equal(key, "Type") == TRUE) {
1223 type = (const char *)item_value;
1224 } else if (g_str_equal(key, "Name") == TRUE) {
1225 name = (const char *)item_value;
1226 } else if (g_str_equal(key, "Host") == TRUE) {
1227 host = (const char *)item_value;
1228 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1229 domain = (const char *)item_value;
1232 DBG("%s %s", key, (char *)item_value);
1234 if (item_value != NULL)
1235 connman_dbus_dict_append_basic(&new_dict, key,
1236 value_type, &item_value);
1238 case DBUS_TYPE_ARRAY:
1239 if (g_str_equal(key, "Networks") == TRUE) {
1240 networks = get_user_networks(&value);
1241 connman_dbus_dict_append_array(&new_dict,
1243 DBUS_TYPE_DICT_ENTRY,
1250 dbus_message_iter_next(&array);
1253 connman_dbus_dict_close(&new_iter, &new_dict);
1255 DBG("VPN type %s name %s host %s domain %s networks %p",
1256 type, name, host, domain, networks);
1258 if (host == NULL || domain == NULL) {
1263 if (type == NULL || name == NULL) {
1268 ident = g_strdup_printf("%s_%s", host, domain);
1269 set_dbus_ident(ident);
1271 DBG("ident %s", ident);
1273 data = g_hash_table_lookup(vpn_connections, ident);
1275 if (data->call != NULL || data->cb_data != NULL) {
1276 DBG("create configuration call already pending");
1281 char *path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1283 data = create_connection_data(path);
1291 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1295 * User called net.connman.Manager.ConnectProvider if we are here.
1296 * So use the data from original message in the new msg.
1298 me = g_strdup(dbus_message_get_destination(msg));
1300 dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1301 dbus_message_set_path(new_msg, "/");
1302 dbus_message_set_destination(new_msg, VPN_SERVICE);
1303 dbus_message_set_sender(new_msg, me);
1304 dbus_message_set_member(new_msg, "Create");
1306 user_data = g_try_new0(struct config_create_data, 1);
1307 if (user_data == NULL) {
1312 user_data->callback = callback;
1313 user_data->message = dbus_message_ref(msg);
1314 user_data->path = NULL;
1316 DBG("cb %p msg %p", user_data, msg);
1318 result = dbus_connection_send_with_reply(connection, new_msg,
1319 &call, DBUS_TIMEOUT);
1320 if (result == FALSE || call == NULL) {
1325 dbus_pending_call_set_notify(call, configuration_create_reply,
1330 if (new_msg != NULL)
1331 dbus_message_unref(new_msg);
1333 if (networks != NULL)
1334 g_slist_free_full(networks, destroy_route);
1340 static connman_bool_t check_host(char **hosts, char *host)
1347 for (i = 0; hosts[i] != NULL; i++) {
1348 if (g_strcmp0(hosts[i], host) == 0)
1355 static void set_route(struct connection_data *data, struct vpn_route *route)
1358 * If the VPN administrator/user has given a route to
1359 * VPN server, then we must discard that because the
1360 * server cannot be contacted via VPN tunnel.
1362 if (check_host(data->host_ip, route->network) == TRUE) {
1363 DBG("Discarding VPN route to %s via %s at index %d",
1364 route->network, route->gateway, data->index);
1368 if (route->family == AF_INET6) {
1369 unsigned char prefix_len = atoi(route->netmask);
1371 connman_inet_add_ipv6_network_route(data->index,
1376 connman_inet_add_network_route(data->index, route->network,
1382 static int set_routes(struct connman_provider *provider,
1383 enum connman_provider_route_type type)
1385 struct connection_data *data;
1386 GHashTableIter iter;
1387 gpointer value, key;
1389 DBG("provider %p", provider);
1391 data = connman_provider_get_data(provider);
1395 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1396 type == CONNMAN_PROVIDER_ROUTE_USER) {
1397 g_hash_table_iter_init(&iter, data->user_routes);
1399 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1400 set_route(data, value);
1403 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1404 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1405 g_hash_table_iter_init(&iter, data->server_routes);
1407 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1408 set_route(data, value);
1414 static connman_bool_t check_routes(struct connman_provider *provider)
1416 struct connection_data *data;
1418 DBG("provider %p", provider);
1420 data = connman_provider_get_data(provider);
1424 if (data->user_routes != NULL &&
1425 g_hash_table_size(data->user_routes) > 0)
1428 if (data->server_routes != NULL &&
1429 g_hash_table_size(data->server_routes) > 0)
1435 static struct connman_provider_driver provider_driver = {
1437 .type = CONNMAN_PROVIDER_TYPE_VPN,
1438 .probe = provider_probe,
1439 .remove = provider_remove,
1440 .connect = provider_connect,
1441 .disconnect = provider_disconnect,
1442 .set_property = set_string,
1443 .get_property = get_string,
1444 .create = create_configuration,
1445 .set_routes = set_routes,
1446 .check_routes = check_routes,
1449 static void destroy_provider(struct connection_data *data)
1451 DBG("data %p", data);
1453 if (g_str_equal(data->state, "ready") == TRUE ||
1454 g_str_equal(data->state, "configuration") == TRUE)
1455 connman_provider_disconnect(data->provider);
1457 if (data->call != NULL)
1458 dbus_pending_call_cancel(data->call);
1460 connman_provider_set_data(data->provider, NULL);
1462 connman_provider_remove(data->provider);
1464 data->provider = NULL;
1467 static void connection_destroy(gpointer hash_data)
1469 struct connection_data *data = hash_data;
1471 DBG("data %p", data);
1473 if (data->provider != NULL)
1474 destroy_provider(data);
1477 g_free(data->ident);
1478 g_free(data->state);
1482 g_free(data->domain);
1483 g_hash_table_destroy(data->server_routes);
1484 g_hash_table_destroy(data->user_routes);
1485 g_strfreev(data->nameservers);
1486 g_hash_table_destroy(data->setting_strings);
1487 connman_ipaddress_free(data->ip);
1489 cancel_host_resolv(data);
1494 static void vpnd_created(DBusConnection *conn, void *user_data)
1496 DBG("connection %p", conn);
1498 get_connections(user_data);
1501 static void vpnd_removed(DBusConnection *conn, void *user_data)
1503 DBG("connection %p", conn);
1505 g_hash_table_remove_all(vpn_connections);
1508 static void remove_connection(DBusConnection *conn, const char *path)
1510 DBG("path %s", path);
1512 g_hash_table_remove(vpn_connections, get_ident(path));
1515 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1519 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1520 struct connection_data *data;
1522 if (dbus_message_has_signature(message, signature) == FALSE) {
1523 connman_error("vpn removed signature \"%s\" does not match "
1525 dbus_message_get_signature(message), signature);
1529 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1532 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1534 remove_connection(conn, path);
1539 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1542 DBusMessageIter iter, properties;
1544 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1545 DBUS_TYPE_ARRAY_AS_STRING
1546 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1547 DBUS_TYPE_STRING_AS_STRING
1548 DBUS_TYPE_VARIANT_AS_STRING
1549 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1551 if (dbus_message_has_signature(message, signature) == FALSE) {
1552 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1553 "match expected \"%s\"",
1554 dbus_message_get_signature(message), signature);
1560 if (dbus_message_iter_init(message, &iter) == FALSE)
1563 dbus_message_iter_get_basic(&iter, &path);
1565 dbus_message_iter_next(&iter);
1566 dbus_message_iter_recurse(&iter, &properties);
1568 add_connection(path, &properties, user_data);
1573 static int save_route(GHashTable *routes, int family, const char *network,
1574 const char *netmask, const char *gateway)
1576 struct vpn_route *route;
1577 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1579 DBG("family %d network %s netmask %s", family, network, netmask);
1581 route = g_hash_table_lookup(routes, key);
1582 if (route == NULL) {
1583 route = g_try_new0(struct vpn_route, 1);
1584 if (route == NULL) {
1585 connman_error("out of memory");
1589 route->family = family;
1590 route->network = g_strdup(network);
1591 route->netmask = g_strdup(netmask);
1592 route->gateway = g_strdup(gateway);
1594 g_hash_table_replace(routes, key, route);
1601 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1603 DBusMessageIter dict;
1604 const char *network, *netmask, *gateway;
1607 dbus_message_iter_recurse(dicts, &dict);
1609 network = netmask = gateway = NULL;
1612 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1614 DBusMessageIter entry, value;
1617 dbus_message_iter_recurse(&dict, &entry);
1618 dbus_message_iter_get_basic(&entry, &key);
1620 dbus_message_iter_next(&entry);
1621 dbus_message_iter_recurse(&entry, &value);
1623 if (g_str_equal(key, "ProtocolFamily") == TRUE) {
1625 dbus_message_iter_get_basic(&value, &pf);
1634 DBG("family %d", family);
1635 } else if (g_str_equal(key, "Netmask") == TRUE) {
1636 dbus_message_iter_get_basic(&value, &netmask);
1637 DBG("netmask %s", netmask);
1638 } else if (g_str_equal(key, "Network") == TRUE) {
1639 dbus_message_iter_get_basic(&value, &network);
1640 DBG("host %s", network);
1641 } else if (g_str_equal(key, "Gateway") == TRUE) {
1642 dbus_message_iter_get_basic(&value, &gateway);
1643 DBG("gateway %s", gateway);
1646 dbus_message_iter_next(&dict);
1649 if (netmask == NULL || network == NULL || gateway == NULL) {
1650 DBG("Value missing.");
1654 return save_route(routes, family, network, netmask, gateway);
1657 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1659 DBusMessageIter entry;
1662 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1663 DBG("Expecting array, ignoring routes.");
1667 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1669 dbus_message_iter_recurse(array, &entry);
1671 while (dbus_message_iter_get_arg_type(&entry) ==
1673 DBusMessageIter dicts;
1675 dbus_message_iter_recurse(&entry, &dicts);
1677 while (dbus_message_iter_get_arg_type(&dicts) ==
1679 int err = read_route_dict(routes, &dicts);
1682 dbus_message_iter_next(&dicts);
1685 dbus_message_iter_next(&entry);
1688 dbus_message_iter_next(array);
1694 static gboolean property_changed(DBusConnection *conn,
1695 DBusMessage *message,
1698 const char *path = dbus_message_get_path(message);
1699 struct connection_data *data = NULL;
1700 DBusMessageIter iter, value;
1701 connman_bool_t ip_set = FALSE;
1705 const char *signature = DBUS_TYPE_STRING_AS_STRING
1706 DBUS_TYPE_VARIANT_AS_STRING;
1708 if (dbus_message_has_signature(message, signature) == FALSE) {
1709 connman_error("vpn property signature \"%s\" does not match "
1711 dbus_message_get_signature(message), signature);
1715 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1719 if (dbus_message_iter_init(message, &iter) == FALSE)
1722 dbus_message_iter_get_basic(&iter, &key);
1724 dbus_message_iter_next(&iter);
1725 dbus_message_iter_recurse(&iter, &value);
1729 if (g_str_equal(key, "State") == TRUE) {
1730 dbus_message_iter_get_basic(&value, &str);
1732 DBG("%s %s -> %s", data->path, data->state, str);
1734 if (g_str_equal(data->state, str) == TRUE)
1737 g_free(data->state);
1738 data->state = g_strdup(str);
1740 set_provider_state(data);
1741 } else if (g_str_equal(key, "Index") == TRUE) {
1742 dbus_message_iter_get_basic(&value, &data->index);
1743 connman_provider_set_index(data->provider, data->index);
1744 } else if (g_str_equal(key, "IPv4") == TRUE) {
1745 err = extract_ip(&value, AF_INET, data);
1747 } else if (g_str_equal(key, "IPv6") == TRUE) {
1748 err = extract_ip(&value, AF_INET6, data);
1750 } else if (g_str_equal(key, "ServerRoutes") == TRUE) {
1751 err = routes_changed(&value, data->server_routes);
1753 * Note that the vpnd will delay the route sending a bit
1754 * (in order to collect the routes from VPN client),
1755 * so we might have got the State changed property before
1756 * we got ServerRoutes. This means that we must try to set
1757 * the routes here because they would be left unset otherwise.
1760 set_routes(data->provider,
1761 CONNMAN_PROVIDER_ROUTE_SERVER);
1762 } else if (g_str_equal(key, "UserRoutes") == TRUE) {
1763 err = routes_changed(&value, data->user_routes);
1765 set_routes(data->provider,
1766 CONNMAN_PROVIDER_ROUTE_USER);
1767 } else if (g_str_equal(key, "Nameservers") == TRUE) {
1768 if (extract_nameservers(&value, data) == 0 &&
1769 data->nameservers != NULL)
1770 connman_provider_set_nameservers(data->provider,
1772 } else if (g_str_equal(key, "Domain") == TRUE) {
1773 dbus_message_iter_get_basic(&value, &str);
1774 g_free(data->domain);
1775 data->domain = g_strdup(str);
1776 connman_provider_set_domain(data->provider, data->domain);
1779 if (ip_set == TRUE && err == 0) {
1780 err = connman_provider_set_ipaddress(data->provider, data->ip);
1782 DBG("setting provider IP address failed (%s/%d)",
1783 strerror(-err), -err);
1789 static int vpn_init(void)
1793 connection = connman_dbus_get_connection();
1794 if (connection == NULL)
1797 watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
1798 vpnd_created, vpnd_removed, &provider_driver, NULL);
1800 added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1801 VPN_MANAGER_INTERFACE,
1802 CONNECTION_ADDED, connection_added,
1803 &provider_driver, NULL);
1805 removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1806 VPN_MANAGER_INTERFACE,
1807 CONNECTION_REMOVED, connection_removed,
1810 property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1811 VPN_CONNECTION_INTERFACE,
1812 PROPERTY_CHANGED, property_changed,
1815 if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
1820 err = connman_provider_driver_register(&provider_driver);
1822 vpn_connections = g_hash_table_new_full(g_str_hash,
1824 g_free, connection_destroy);
1826 vpnd_created(connection, &provider_driver);
1832 g_dbus_remove_watch(connection, watch);
1833 g_dbus_remove_watch(connection, added_watch);
1834 g_dbus_remove_watch(connection, removed_watch);
1835 g_dbus_remove_watch(connection, property_watch);
1837 dbus_connection_unref(connection);
1842 static void vpn_exit(void)
1844 g_dbus_remove_watch(connection, watch);
1845 g_dbus_remove_watch(connection, added_watch);
1846 g_dbus_remove_watch(connection, removed_watch);
1847 g_dbus_remove_watch(connection, property_watch);
1849 connman_provider_driver_unregister(&provider_driver);
1851 if (vpn_connections != NULL)
1852 g_hash_table_destroy(vpn_connections);
1854 dbus_connection_unref(connection);
1857 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
1858 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)