5 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/socket.h>
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/technology.h>
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/dbus.h>
39 #include <connman/provider.h>
40 #include <connman/ipaddress.h>
41 #include <connman/notifier.h>
42 #include <connman/vpn-dbus.h>
43 #include <connman/inet.h>
44 #include <gweb/gresolv.h>
46 #define DBUS_TIMEOUT 10000
48 static DBusConnection *connection;
50 static GHashTable *vpn_connections = NULL;
52 static guint added_watch;
53 static guint removed_watch;
54 static guint property_watch;
63 struct config_create_data {
64 connection_ready_cb callback;
69 struct connection_data {
72 struct connman_provider *provider;
74 DBusPendingCall *call;
75 DBusPendingCall *disconnect_call;
77 struct config_create_data *cb_data;
89 GHashTable *server_routes;
90 GHashTable *user_routes;
91 GHashTable *setting_strings;
93 struct connman_ipaddress *ip;
99 static int set_string(struct connman_provider *provider,
100 const char *key, const char *value)
102 struct connection_data *data;
104 data = connman_provider_get_data(provider);
108 DBG("data %p provider %p key %s value %s", data, provider, key, value);
110 if (g_str_equal(key, "Type")) {
112 data->type = g_strdup(value);
113 } else if (g_str_equal(key, "Name")) {
115 data->name = g_strdup(value);
116 } else if (g_str_equal(key, "Host")) {
118 data->host = g_strdup(value);
119 } else if (g_str_equal(key, "VPN.Domain") ||
120 g_str_equal(key, "Domain")) {
121 g_free(data->domain);
122 data->domain = g_strdup(value);
124 g_hash_table_replace(data->setting_strings,
125 g_strdup(key), g_strdup(value));
129 static const char *get_string(struct connman_provider *provider,
132 struct connection_data *data;
134 data = connman_provider_get_data(provider);
138 DBG("data %p provider %p key %s", data, provider, key);
140 if (g_str_equal(key, "Type"))
142 else if (g_str_equal(key, "Name"))
144 else if (g_str_equal(key, "Host"))
146 else if (g_str_equal(key, "HostIP")) {
147 if (!data->host_ip ||
151 return data->host_ip[0];
152 } else if (g_str_equal(key, "VPN.Domain"))
155 return g_hash_table_lookup(data->setting_strings, key);
158 static char *get_ident(const char *path)
165 pos = strrchr(path, '/');
172 static void cancel_host_resolv(struct connection_data *data)
174 if (data->resolv_id != 0)
175 g_resolv_cancel_lookup(data->resolv, data->resolv_id);
179 g_resolv_unref(data->resolv);
183 static gboolean remove_resolv(gpointer user_data)
185 struct connection_data *data = user_data;
187 cancel_host_resolv(data);
192 static void resolv_result(GResolvResultStatus status,
193 char **results, gpointer user_data)
195 struct connection_data *data = user_data;
197 DBG("status %d", status);
199 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results &&
200 g_strv_length(results) > 0) {
201 g_strfreev(data->host_ip);
202 data->host_ip = g_strdupv(results);
206 * We cannot unref the resolver here as resolv struct is manipulated
207 * by gresolv.c after we return from this callback.
209 g_idle_add(remove_resolv, data);
214 static void resolv_host_addr(struct connection_data *data)
219 if (connman_inet_check_ipaddress(data->host) > 0)
225 data->resolv = g_resolv_new(0);
227 DBG("Cannot resolv %s", data->host);
231 DBG("Trying to resolv %s", data->host);
233 data->resolv_id = g_resolv_lookup_hostname(data->resolv, data->host,
234 resolv_result, data);
237 static void free_config_cb_data(struct config_create_data *cb_data)
242 g_free(cb_data->path);
243 cb_data->path = NULL;
245 if (cb_data->message) {
246 dbus_message_unref(cb_data->message);
247 cb_data->message = NULL;
250 cb_data->callback = NULL;
255 static bool provider_is_connected(struct connection_data *data)
257 return data && (g_str_equal(data->state, "ready") ||
258 g_str_equal(data->state, "configuration"));
261 static void set_provider_state(struct connection_data *data)
263 enum connman_provider_state state = CONNMAN_PROVIDER_STATE_UNKNOWN;
266 DBG("provider %p new state %s", data->provider, data->state);
268 if (!provider_is_connected(data)) {
269 g_free(data->service_ident);
270 data->service_ident = NULL;
273 if (g_str_equal(data->state, "ready")) {
274 state = CONNMAN_PROVIDER_STATE_READY;
276 } else if (g_str_equal(data->state, "configuration")) {
277 state = CONNMAN_PROVIDER_STATE_CONNECT;
278 } else if (g_str_equal(data->state, "idle")) {
279 state = CONNMAN_PROVIDER_STATE_IDLE;
280 } else if (g_str_equal(data->state, "disconnect")) {
282 state = CONNMAN_PROVIDER_STATE_DISCONNECT;
284 } else if (g_str_equal(data->state, "failure")) {
286 state = CONNMAN_PROVIDER_STATE_FAILURE;
290 connman_provider_set_state(data->provider, state);
295 data->cb_data->callback(data->cb_data->message,
298 connman_provider_set_state(data->provider, state);
300 free_config_cb_data(data->cb_data);
301 data->cb_data = NULL;
304 static int create_provider(struct connection_data *data, void *user_data)
306 struct connman_provider_driver *driver = user_data;
309 DBG("%s", data->path);
311 data->provider = connman_provider_get(data->ident);
315 DBG("provider %p name %s", data->provider, data->name);
317 connman_provider_set_data(data->provider, data);
318 connman_provider_set_driver(data->provider, driver);
320 err = connman_provider_create_service(data->provider);
322 connman_provider_set_immutable(data->provider, data->immutable);
323 if (g_str_equal(data->state, "ready")) {
324 connman_provider_set_index(data->provider,
327 connman_provider_set_ipaddress(data->provider,
331 set_provider_state(data);
337 static void destroy_route(gpointer user_data)
339 struct vpn_route *route = user_data;
341 g_free(route->network);
342 g_free(route->netmask);
343 g_free(route->gateway);
347 static struct connection_data *create_connection_data(const char *path)
349 struct connection_data *data;
351 data = g_try_new0(struct connection_data, 1);
355 DBG("path %s", path);
357 data->path = g_strdup(path);
358 data->ident = g_strdup(get_ident(path));
361 data->setting_strings = g_hash_table_new_full(g_str_hash,
362 g_str_equal, g_free, g_free);
364 data->server_routes = g_hash_table_new_full(g_direct_hash,
365 g_str_equal, g_free, destroy_route);
366 data->user_routes = g_hash_table_new_full(g_str_hash,
367 g_str_equal, g_free, destroy_route);
372 static int extract_ip(DBusMessageIter *array, int family,
373 struct connection_data *data)
375 DBusMessageIter dict;
376 char *address = NULL, *gateway = NULL, *netmask = NULL, *peer = NULL;
377 unsigned char prefix_len;
379 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
382 dbus_message_iter_recurse(array, &dict);
384 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
385 DBusMessageIter entry, value;
388 dbus_message_iter_recurse(&dict, &entry);
389 dbus_message_iter_get_basic(&entry, &key);
391 dbus_message_iter_next(&entry);
392 dbus_message_iter_recurse(&entry, &value);
394 if (g_str_equal(key, "Address")) {
395 dbus_message_iter_get_basic(&value, &address);
396 DBG("address %s", address);
397 } else if (g_str_equal(key, "Netmask")) {
398 dbus_message_iter_get_basic(&value, &netmask);
399 DBG("netmask %s", netmask);
400 } else if (g_str_equal(key, "PrefixLength")) {
401 dbus_message_iter_get_basic(&value, &netmask);
402 DBG("prefix length %s", netmask);
403 } else if (g_str_equal(key, "Peer")) {
404 dbus_message_iter_get_basic(&value, &peer);
405 DBG("peer %s", peer);
406 } else if (g_str_equal(key, "Gateway")) {
407 dbus_message_iter_get_basic(&value, &gateway);
408 DBG("gateway %s", gateway);
411 dbus_message_iter_next(&dict);
414 connman_ipaddress_free(data->ip);
415 data->ip = connman_ipaddress_alloc(family);
421 connman_ipaddress_set_ipv4(data->ip, address, netmask,
425 prefix_len = atoi(netmask);
426 connman_ipaddress_set_ipv6(data->ip, address, prefix_len,
433 connman_ipaddress_set_peer(data->ip, peer);
438 static int extract_nameservers(DBusMessageIter *array,
439 struct connection_data *data)
441 DBusMessageIter entry;
442 char **nameservers = NULL;
445 dbus_message_iter_recurse(array, &entry);
447 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
448 const char *nameserver;
450 dbus_message_iter_get_basic(&entry, &nameserver);
452 nameservers = g_try_renew(char *, nameservers, i + 2);
456 DBG("[%d] %s", i, nameserver);
458 nameservers[i] = g_strdup(nameserver);
462 nameservers[++i] = NULL;
464 dbus_message_iter_next(&entry);
467 g_strfreev(data->nameservers);
468 data->nameservers = nameservers;
473 static int errorstr2val(const char *error) {
474 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".InProgress") == 0)
477 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".AlreadyConnected") == 0)
480 if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".OperationCanceled") == 0)
483 return -ECONNREFUSED;
486 static void connect_reply(DBusPendingCall *call, void *user_data)
490 struct connection_data *data = user_data;
491 struct config_create_data *cb_data = data->cb_data;
493 if (!dbus_pending_call_get_completed(call))
496 if (call != data->call) {
497 connman_error("invalid call %p to VPN connect_reply data %p "
498 " call %p ", call, data, data->call);
499 dbus_pending_call_unref(call);
503 DBG("user_data %p path %s", user_data, cb_data ? cb_data->path : NULL);
505 reply = dbus_pending_call_steal_reply(call);
509 dbus_error_init(&error);
511 if (dbus_set_error_from_message(&error, reply)) {
512 int err = errorstr2val(error.name);
515 * ECANCELED means that user has canceled authentication
516 * dialog. That's not really an error, it's part of a normal
517 * workflow. We also take it as a request to turn autoconnect
518 * off, in case if it was on.
520 if (err == -ECANCELED) {
521 DBG("%s connect canceled", data->path);
522 connman_provider_set_autoconnect(data->provider, false);
523 } else if (err != -EINPROGRESS) {
524 connman_error("Connect reply: %s (%s)", error.message,
526 DBG("data %p cb_data %p", data, cb_data);
529 cb_data->callback(cb_data->message, err, NULL);
530 free_config_cb_data(cb_data);
531 data->cb_data = NULL;
535 dbus_error_free(&error);
539 * The vpn connection is up when we get a "ready" state
540 * property so at this point we do nothing for the provider
544 dbus_message_unref(reply);
547 dbus_pending_call_unref(data->call);
550 data->connect_pending = false;
553 static int connect_provider(struct connection_data *data, void *user_data,
554 const char *dbus_sender)
556 DBusPendingCall *call;
557 DBusMessage *message;
558 struct config_create_data *cb_data = user_data;
559 struct connman_service *transport = connman_service_get_default();
561 DBG("data %p user %p path %s sender %s", data, cb_data, data->path,
565 DBG("no default service, refusing to connect");
569 if (data->connect_pending && data->call) {
570 connman_info("connect already pending");
574 /* We need to pass original dbus sender to connman-vpnd,
575 * use a Connect2 method for that if the original dbus sender is set.
576 * Connect method requires no parameter, Connect2 requires dbus sender
579 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
580 VPN_CONNECTION_INTERFACE,
581 dbus_sender && *dbus_sender ?
582 VPN_CONNECT2 : VPN_CONNECT);
586 if (dbus_sender && *dbus_sender)
587 dbus_message_append_args(message, DBUS_TYPE_STRING,
592 if (!dbus_connection_send_with_reply(connection, message,
593 &call, DBUS_TIMEOUT)) {
594 connman_error("Unable to call %s.%s()",
595 VPN_CONNECTION_INTERFACE, dbus_sender && *dbus_sender ?
596 VPN_CONNECT2 : VPN_CONNECT);
597 dbus_message_unref(message);
602 dbus_message_unref(message);
607 dbus_pending_call_cancel(data->call);
608 dbus_pending_call_unref(data->call);
612 data->connect_pending = true;
615 g_free(cb_data->path);
616 cb_data->path = g_strdup(data->path);
620 * This is the service which (most likely) will be used
621 * as a transport for VPN connection.
623 g_free(data->service_ident);
624 data->service_ident =
625 g_strdup(connman_service_get_identifier(transport));
626 DBG("transport %s", data->service_ident);
628 dbus_pending_call_set_notify(call, connect_reply, data, NULL);
630 dbus_message_unref(message);
635 static void add_connection(const char *path, DBusMessageIter *properties,
638 struct connection_data *data;
640 char *ident = get_ident(path);
643 data = g_hash_table_lookup(vpn_connections, ident);
646 * We might have a dummy connection struct here that
647 * was created by configuration_create_reply() so in
648 * that case just continue.
650 if (!data->connect_pending)
655 data = create_connection_data(path);
660 DBG("data %p path %s", data, path);
662 while (dbus_message_iter_get_arg_type(properties) ==
663 DBUS_TYPE_DICT_ENTRY) {
664 DBusMessageIter entry, value;
668 dbus_message_iter_recurse(properties, &entry);
669 dbus_message_iter_get_basic(&entry, &key);
671 dbus_message_iter_next(&entry);
672 dbus_message_iter_recurse(&entry, &value);
674 if (g_str_equal(key, "State")) {
675 dbus_message_iter_get_basic(&value, &str);
676 DBG("state %s -> %s", data->state, str);
677 data->state = g_strdup(str);
678 } else if (g_str_equal(key, "IPv4")) {
679 extract_ip(&value, AF_INET, data);
680 } else if (g_str_equal(key, "IPv6")) {
681 extract_ip(&value, AF_INET6, data);
682 } else if (g_str_equal(key, "Name")) {
683 dbus_message_iter_get_basic(&value, &str);
684 data->name = g_strdup(str);
685 } else if (g_str_equal(key, "Type")) {
686 dbus_message_iter_get_basic(&value, &str);
687 data->type = g_strdup(str);
688 } else if (g_str_equal(key, "Immutable")) {
689 dbus_bool_t immutable;
691 dbus_message_iter_get_basic(&value, &immutable);
692 data->immutable = immutable;
693 } else if (g_str_equal(key, "Host")) {
694 dbus_message_iter_get_basic(&value, &str);
695 data->host = g_strdup(str);
696 } else if (g_str_equal(key, "Domain")) {
697 dbus_message_iter_get_basic(&value, &str);
698 g_free(data->domain);
699 data->domain = g_strdup(str);
700 } else if (g_str_equal(key, "Nameservers")) {
701 extract_nameservers(&value, data);
702 } else if (g_str_equal(key, "Index")) {
703 dbus_message_iter_get_basic(&value, &data->index);
704 } else if (g_str_equal(key, "ServerRoutes")) {
706 } else if (g_str_equal(key, "UserRoutes")) {
709 if (dbus_message_iter_get_arg_type(&value) ==
711 dbus_message_iter_get_basic(&value, &str);
712 g_hash_table_replace(data->setting_strings,
713 g_strdup(key), g_strdup(str));
715 DBG("unknown key %s", key);
719 dbus_message_iter_next(properties);
723 g_hash_table_insert(vpn_connections, g_strdup(data->ident),
726 err = create_provider(data, user_data);
730 resolv_host_addr(data);
732 if (data->nameservers)
733 connman_provider_set_nameservers(data->provider,
737 connman_provider_set_domain(data->provider,
740 if (data->connect_pending) {
741 const char *dbus_sender = NULL;
743 if (data->cb_data && data->cb_data->message) {
745 dbus_message_get_sender(data->cb_data->message);
747 connect_provider(data, data->cb_data, dbus_sender);
753 DBG("removing %s", data->ident);
754 g_hash_table_remove(vpn_connections, data->ident);
757 static void get_connections_reply(DBusPendingCall *call, void *user_data)
761 DBusMessageIter array, dict;
762 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
763 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
764 DBUS_TYPE_OBJECT_PATH_AS_STRING
765 DBUS_TYPE_ARRAY_AS_STRING
766 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
767 DBUS_TYPE_STRING_AS_STRING
768 DBUS_TYPE_VARIANT_AS_STRING
769 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
770 DBUS_STRUCT_END_CHAR_AS_STRING;
772 if (!dbus_pending_call_get_completed(call))
777 reply = dbus_pending_call_steal_reply(call);
779 dbus_error_init(&error);
781 if (dbus_set_error_from_message(&error, reply)) {
782 connman_error("%s", error.message);
783 dbus_error_free(&error);
787 if (!dbus_message_has_signature(reply, signature)) {
788 connman_error("vpnd signature \"%s\" does not match "
790 dbus_message_get_signature(reply), signature);
794 if (!dbus_message_iter_init(reply, &array))
797 dbus_message_iter_recurse(&array, &dict);
799 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
800 DBusMessageIter value, properties;
803 dbus_message_iter_recurse(&dict, &value);
804 dbus_message_iter_get_basic(&value, &path);
806 dbus_message_iter_next(&value);
807 dbus_message_iter_recurse(&value, &properties);
809 add_connection(path, &properties, user_data);
811 dbus_message_iter_next(&dict);
815 dbus_message_unref(reply);
817 dbus_pending_call_unref(call);
820 static int get_connections(void *user_data)
822 DBusPendingCall *call;
823 DBusMessage *message;
827 message = dbus_message_new_method_call(VPN_SERVICE, "/",
828 VPN_MANAGER_INTERFACE,
833 if (!dbus_connection_send_with_reply(connection, message,
834 &call, DBUS_TIMEOUT)) {
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, get_connections_reply,
849 dbus_message_unref(message);
854 static int provider_probe(struct connman_provider *provider)
859 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
864 if (!dbus_pending_call_get_completed(call))
869 reply = dbus_pending_call_steal_reply(call);
871 dbus_error_init(&error);
873 if (dbus_set_error_from_message(&error, reply)) {
875 * If the returned error is NotFound, it means that we
876 * have actually removed the provider in vpnd already.
878 if (!dbus_error_has_name(&error,
879 CONNMAN_ERROR_INTERFACE".NotFound"))
880 connman_error("%s", error.message);
882 dbus_error_free(&error);
885 dbus_message_unref(reply);
887 dbus_pending_call_unref(call);
890 static int provider_remove(struct connman_provider *provider)
892 DBusPendingCall *call;
893 DBusMessage *message;
894 struct connection_data *data;
896 data = connman_provider_get_data(provider);
898 DBG("provider %p data %p", provider, data);
902 * This means the provider is already removed,
903 * just ignore the dbus in this case.
909 * When provider.c:provider_remove() calls this function,
910 * it will remove the provider itself after the call.
911 * This means that we cannot use the provider pointer later
912 * as it is no longer valid.
914 data->provider = NULL;
916 message = dbus_message_new_method_call(VPN_SERVICE, "/",
917 VPN_MANAGER_INTERFACE,
922 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
925 if (!dbus_connection_send_with_reply(connection, message,
926 &call, DBUS_TIMEOUT)) {
927 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
929 dbus_message_unref(message);
934 dbus_message_unref(message);
938 dbus_pending_call_set_notify(call, remove_connection_reply,
941 dbus_message_unref(message);
946 static int provider_connect(struct connman_provider *provider,
947 const char *dbus_sender)
949 struct connection_data *data;
951 data = connman_provider_get_data(provider);
955 return connect_provider(data, NULL, dbus_sender);
958 static void disconnect_reply(DBusPendingCall *call, void *user_data)
960 struct connection_data *data = user_data;
964 DBG("user %p", user_data);
966 reply = dbus_pending_call_steal_reply(call);
968 dbus_error_init(&error);
970 if (dbus_set_error_from_message(&error, reply)) {
971 connman_error("%s", error.message);
972 dbus_error_free(&error);
977 dbus_message_unref(reply);
978 dbus_pending_call_unref(call);
979 data->disconnect_call = NULL;
982 static int disconnect_provider(struct connection_data *data)
985 DBusMessage *message;
987 DBG("data %p path %s", data, data->path);
989 if (data->disconnect_call) {
990 DBG("already disconnecting");
994 message = dbus_message_new_method_call(VPN_SERVICE, data->path,
995 VPN_CONNECTION_INTERFACE,
1000 sent = dbus_connection_send_with_reply(connection, message,
1001 &data->disconnect_call, DBUS_TIMEOUT);
1002 dbus_message_unref(message);
1004 if (!sent || !data->disconnect_call) {
1005 connman_error("Unable to call %s.%s()",
1006 VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
1010 dbus_pending_call_set_notify(data->disconnect_call, disconnect_reply,
1013 g_free(data->service_ident);
1014 data->service_ident = NULL;
1016 connman_provider_set_state(data->provider,
1017 CONNMAN_PROVIDER_STATE_DISCONNECT);
1018 return -EINPROGRESS;
1021 static int provider_disconnect(struct connman_provider *provider)
1025 struct connection_data *data;
1027 DBG("provider %p", provider);
1029 data = connman_provider_get_data(provider);
1033 if (provider_is_connected(data))
1034 err = disconnect_provider(data);
1037 dbus_pending_call_cancel(data->call);
1038 dbus_pending_call_unref(data->call);
1042 data->connect_pending = false;
1047 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
1051 DBusMessageIter iter;
1052 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1055 struct connection_data *data;
1056 struct config_create_data *cb_data = user_data;
1058 if (!dbus_pending_call_get_completed(call))
1061 DBG("user %p", cb_data);
1063 reply = dbus_pending_call_steal_reply(call);
1065 dbus_error_init(&error);
1067 if (dbus_set_error_from_message(&error, reply)) {
1068 connman_error("dbus error: %s", error.message);
1069 dbus_error_free(&error);
1073 if (!dbus_message_has_signature(reply, signature)) {
1074 connman_error("vpn configuration signature \"%s\" does not "
1075 "match expected \"%s\"",
1076 dbus_message_get_signature(reply), signature);
1080 if (!dbus_message_iter_init(reply, &iter))
1083 dbus_message_iter_get_basic(&iter, &path);
1086 * Then try to connect the VPN as expected by ConnectProvider API
1088 ident = get_ident(path);
1090 data = g_hash_table_lookup(vpn_connections, ident);
1093 * Someone removed the data. We cannot really continue.
1095 DBG("Pending data not found for %s, cannot continue!", ident);
1098 data->connect_pending = true;
1101 data->cb_data = cb_data;
1103 DBG("Connection callback data already in use!");
1106 * Connection is created in add_connections() after
1107 * we have received the ConnectionAdded signal.
1110 DBG("cb %p msg %p", data->cb_data,
1111 data->cb_data ? data->cb_data->message : NULL);
1115 dbus_message_unref(reply);
1117 dbus_pending_call_unref(call);
1120 static void set_dbus_ident(char *ident)
1122 int i, len = strlen(ident);
1124 for (i = 0; i < len; i++) {
1125 if (ident[i] >= '0' && ident[i] <= '9')
1127 if (ident[i] >= 'a' && ident[i] <= 'z')
1129 if (ident[i] >= 'A' && ident[i] <= 'Z')
1135 static struct vpn_route *parse_user_route(const char *user_route)
1137 char *network, *netmask;
1138 struct vpn_route *route = NULL;
1139 int family = PF_UNSPEC;
1140 char **elems = g_strsplit(user_route, "/", 0);
1146 if (!network || *network == '\0') {
1147 DBG("no network/netmask set");
1152 if (netmask && *netmask == '\0') {
1153 DBG("no netmask set");
1157 if (g_strrstr(network, ":"))
1159 else if (g_strrstr(network, ".")) {
1162 if (!g_strrstr(netmask, ".")) {
1163 /* We have netmask length */
1165 struct in_addr netmask_in;
1166 unsigned char prefix_len = 32;
1170 long int value = strtol(netmask, &ptr, 10);
1171 if (ptr != netmask && *ptr == '\0' &&
1172 value && value <= 32)
1176 addr = 0xffffffff << (32 - prefix_len);
1177 netmask_in.s_addr = htonl(addr);
1178 netmask = inet_ntoa(netmask_in);
1180 DBG("network %s netmask %s", network, netmask);
1184 route = g_try_new(struct vpn_route, 1);
1188 route->network = g_strdup(network);
1189 route->netmask = g_strdup(netmask);
1190 route->gateway = NULL;
1191 route->family = family;
1198 static GSList *get_user_networks(DBusMessageIter *array)
1200 DBusMessageIter entry;
1201 GSList *list = NULL;
1203 dbus_message_iter_recurse(array, &entry);
1205 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1207 struct vpn_route *route;
1209 dbus_message_iter_get_basic(&entry, &val);
1211 route = parse_user_route(val);
1213 list = g_slist_prepend(list, route);
1215 dbus_message_iter_next(&entry);
1221 static void append_route(DBusMessageIter *iter, void *user_data)
1223 struct vpn_route *route = user_data;
1224 DBusMessageIter item;
1227 connman_dbus_dict_open(iter, &item);
1232 if (route->family == AF_INET)
1234 else if (route->family == AF_INET6)
1238 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1239 DBUS_TYPE_INT32, &family);
1242 connman_dbus_dict_append_basic(&item, "Network",
1243 DBUS_TYPE_STRING, &route->network);
1246 connman_dbus_dict_append_basic(&item, "Netmask",
1247 DBUS_TYPE_STRING, &route->netmask);
1250 connman_dbus_dict_append_basic(&item, "Gateway",
1251 DBUS_TYPE_STRING, &route->gateway);
1254 connman_dbus_dict_close(iter, &item);
1257 static void append_routes(DBusMessageIter *iter, void *user_data)
1259 GSList *list, *routes = user_data;
1261 DBG("routes %p", routes);
1263 for (list = routes; list; list = g_slist_next(list)) {
1264 DBusMessageIter dict;
1265 struct vpn_route *route = list->data;
1267 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1269 append_route(&dict, route);
1270 dbus_message_iter_close_container(iter, &dict);
1274 static int create_configuration(DBusMessage *msg, connection_ready_cb callback)
1276 DBusMessage *new_msg = NULL;
1277 DBusPendingCall *call;
1278 DBusMessageIter iter, array, new_iter, new_dict;
1279 const char *type = NULL, *name = NULL;
1280 const char *host = NULL, *domain = NULL;
1281 char *ident, *me = NULL;
1284 struct connection_data *data;
1285 struct config_create_data *user_data = NULL;
1286 GSList *networks = NULL;
1289 * We copy the old message data into new message. We cannot
1290 * just use the old message as is because the user route
1291 * information is not in the same format in vpnd.
1293 new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1294 dbus_message_iter_init_append(new_msg, &new_iter);
1295 connman_dbus_dict_open(&new_iter, &new_dict);
1297 dbus_message_iter_init(msg, &iter);
1298 dbus_message_iter_recurse(&iter, &array);
1300 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1301 DBusMessageIter entry, value;
1306 dbus_message_iter_recurse(&array, &entry);
1307 dbus_message_iter_get_basic(&entry, &key);
1309 dbus_message_iter_next(&entry);
1310 dbus_message_iter_recurse(&entry, &value);
1312 value_type = dbus_message_iter_get_arg_type(&value);
1315 switch (value_type) {
1316 case DBUS_TYPE_STRING:
1317 dbus_message_iter_get_basic(&value, &item_value);
1319 if (g_str_equal(key, "Type"))
1320 type = (const char *)item_value;
1321 else if (g_str_equal(key, "Name"))
1322 name = (const char *)item_value;
1323 else if (g_str_equal(key, "Host"))
1324 host = (const char *)item_value;
1325 else if (g_str_equal(key, "VPN.Domain"))
1326 domain = (const char *)item_value;
1328 DBG("%s %s", key, (char *)item_value);
1331 connman_dbus_dict_append_basic(&new_dict, key,
1332 value_type, &item_value);
1334 case DBUS_TYPE_ARRAY:
1335 if (g_str_equal(key, "Networks")) {
1336 networks = get_user_networks(&value);
1337 connman_dbus_dict_append_array(&new_dict,
1339 DBUS_TYPE_DICT_ENTRY,
1346 dbus_message_iter_next(&array);
1349 connman_dbus_dict_close(&new_iter, &new_dict);
1351 DBG("VPN type %s name %s host %s domain %s networks %p",
1352 type, name, host, domain, networks);
1354 if (!host || !domain) {
1359 if (!type || !name) {
1364 ident = g_strdup_printf("%s_%s", host, domain);
1365 set_dbus_ident(ident);
1367 DBG("ident %s", ident);
1369 data = g_hash_table_lookup(vpn_connections, ident);
1371 if (data->call || data->cb_data) {
1372 DBG("create configuration call already pending");
1377 char *path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1379 data = create_connection_data(path);
1387 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1391 * User called net.connman.Manager.ConnectProvider if we are here.
1392 * So use the data from original message in the new msg.
1394 me = g_strdup(dbus_message_get_destination(msg));
1396 dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1397 dbus_message_set_path(new_msg, "/");
1398 dbus_message_set_destination(new_msg, VPN_SERVICE);
1399 dbus_message_set_sender(new_msg, me);
1400 dbus_message_set_member(new_msg, "Create");
1402 user_data = g_try_new0(struct config_create_data, 1);
1408 user_data->callback = callback;
1409 user_data->message = dbus_message_ref(msg);
1410 user_data->path = NULL;
1412 DBG("cb %p msg %p", user_data, msg);
1414 result = dbus_connection_send_with_reply(connection, new_msg,
1415 &call, DBUS_TIMEOUT);
1416 if (!result || !call) {
1421 dbus_pending_call_set_notify(call, configuration_create_reply,
1427 dbus_message_unref(new_msg);
1430 g_slist_free_full(networks, destroy_route);
1436 static bool check_host(char **hosts, char *host)
1443 for (i = 0; hosts[i]; i++) {
1444 if (g_strcmp0(hosts[i], host) == 0)
1451 static void set_route(struct connection_data *data, struct vpn_route *route)
1454 * If the VPN administrator/user has given a route to
1455 * VPN server, then we must discard that because the
1456 * server cannot be contacted via VPN tunnel.
1458 if (check_host(data->host_ip, route->network)) {
1459 DBG("Discarding VPN route to %s via %s at index %d",
1460 route->network, route->gateway, data->index);
1464 if (route->family == AF_INET6) {
1465 unsigned char prefix_len = atoi(route->netmask);
1467 connman_inet_add_ipv6_network_route(data->index,
1472 connman_inet_add_network_route(data->index, route->network,
1478 static int set_routes(struct connman_provider *provider,
1479 enum connman_provider_route_type type)
1481 struct connection_data *data;
1482 GHashTableIter iter;
1483 gpointer value, key;
1485 DBG("provider %p", provider);
1487 data = connman_provider_get_data(provider);
1491 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1492 type == CONNMAN_PROVIDER_ROUTE_USER) {
1493 g_hash_table_iter_init(&iter, data->user_routes);
1495 while (g_hash_table_iter_next(&iter, &key, &value))
1496 set_route(data, value);
1499 if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1500 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1501 g_hash_table_iter_init(&iter, data->server_routes);
1503 while (g_hash_table_iter_next(&iter, &key, &value))
1504 set_route(data, value);
1510 static bool check_routes(struct connman_provider *provider)
1512 struct connection_data *data;
1514 DBG("provider %p", provider);
1516 data = connman_provider_get_data(provider);
1520 if (data->user_routes &&
1521 g_hash_table_size(data->user_routes) > 0)
1524 if (data->server_routes &&
1525 g_hash_table_size(data->server_routes) > 0)
1531 static struct connman_provider_driver provider_driver = {
1533 .type = CONNMAN_PROVIDER_TYPE_VPN,
1534 .probe = provider_probe,
1535 .remove = provider_remove,
1536 .connect = provider_connect,
1537 .disconnect = provider_disconnect,
1538 .set_property = set_string,
1539 .get_property = get_string,
1540 .create = create_configuration,
1541 .set_routes = set_routes,
1542 .check_routes = check_routes,
1545 static void destroy_provider(struct connection_data *data)
1547 DBG("data %p", data);
1549 if (provider_is_connected(data))
1550 connman_provider_disconnect(data->provider);
1552 connman_provider_set_data(data->provider, NULL);
1553 connman_provider_remove(data->provider);
1554 data->provider = NULL;
1557 static void connection_destroy(gpointer hash_data)
1559 struct connection_data *data = hash_data;
1561 DBG("data %p", data);
1564 destroy_provider(data);
1567 dbus_pending_call_cancel(data->call);
1568 dbus_pending_call_unref(data->call);
1571 if (data->disconnect_call) {
1572 dbus_pending_call_cancel(data->disconnect_call);
1573 dbus_pending_call_unref(data->disconnect_call);
1576 g_free(data->service_ident);
1578 g_free(data->ident);
1579 g_free(data->state);
1583 g_strfreev(data->host_ip);
1584 g_free(data->domain);
1585 g_hash_table_destroy(data->server_routes);
1586 g_hash_table_destroy(data->user_routes);
1587 g_strfreev(data->nameservers);
1588 g_hash_table_destroy(data->setting_strings);
1589 connman_ipaddress_free(data->ip);
1591 cancel_host_resolv(data);
1596 static void vpnd_created(DBusConnection *conn, void *user_data)
1598 DBG("connection %p", conn);
1600 get_connections(user_data);
1603 static void vpnd_removed(DBusConnection *conn, void *user_data)
1605 DBG("connection %p", conn);
1607 g_hash_table_remove_all(vpn_connections);
1610 static void remove_connection(DBusConnection *conn, const char *path)
1612 DBG("path %s", path);
1614 g_hash_table_remove(vpn_connections, get_ident(path));
1617 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1621 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1622 struct connection_data *data;
1624 if (!dbus_message_has_signature(message, signature)) {
1625 connman_error("vpn removed signature \"%s\" does not match "
1627 dbus_message_get_signature(message), signature);
1631 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1634 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1636 remove_connection(conn, path);
1641 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1644 DBusMessageIter iter, properties;
1646 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1647 DBUS_TYPE_ARRAY_AS_STRING
1648 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1649 DBUS_TYPE_STRING_AS_STRING
1650 DBUS_TYPE_VARIANT_AS_STRING
1651 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1653 if (!dbus_message_has_signature(message, signature)) {
1654 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1655 "match expected \"%s\"",
1656 dbus_message_get_signature(message), signature);
1662 if (!dbus_message_iter_init(message, &iter))
1665 dbus_message_iter_get_basic(&iter, &path);
1667 dbus_message_iter_next(&iter);
1668 dbus_message_iter_recurse(&iter, &properties);
1670 add_connection(path, &properties, user_data);
1675 static int save_route(GHashTable *routes, int family, const char *network,
1676 const char *netmask, const char *gateway)
1678 struct vpn_route *route;
1679 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1681 DBG("family %d network %s netmask %s", family, network, netmask);
1683 route = g_hash_table_lookup(routes, key);
1685 route = g_try_new0(struct vpn_route, 1);
1687 connman_error("out of memory");
1691 route->family = family;
1692 route->network = g_strdup(network);
1693 route->netmask = g_strdup(netmask);
1694 route->gateway = g_strdup(gateway);
1696 g_hash_table_replace(routes, key, route);
1703 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1705 DBusMessageIter dict;
1706 const char *network, *netmask, *gateway;
1709 dbus_message_iter_recurse(dicts, &dict);
1711 network = netmask = gateway = NULL;
1714 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1716 DBusMessageIter entry, value;
1719 dbus_message_iter_recurse(&dict, &entry);
1720 dbus_message_iter_get_basic(&entry, &key);
1722 dbus_message_iter_next(&entry);
1723 dbus_message_iter_recurse(&entry, &value);
1725 if (g_str_equal(key, "ProtocolFamily")) {
1727 dbus_message_iter_get_basic(&value, &pf);
1736 DBG("family %d", family);
1737 } else if (g_str_equal(key, "Netmask")) {
1738 dbus_message_iter_get_basic(&value, &netmask);
1739 DBG("netmask %s", netmask);
1740 } else if (g_str_equal(key, "Network")) {
1741 dbus_message_iter_get_basic(&value, &network);
1742 DBG("host %s", network);
1743 } else if (g_str_equal(key, "Gateway")) {
1744 dbus_message_iter_get_basic(&value, &gateway);
1745 DBG("gateway %s", gateway);
1748 dbus_message_iter_next(&dict);
1751 if (!netmask || !network || !gateway) {
1752 DBG("Value missing.");
1756 return save_route(routes, family, network, netmask, gateway);
1759 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1761 DBusMessageIter entry;
1764 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1765 DBG("Expecting array, ignoring routes.");
1769 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1771 dbus_message_iter_recurse(array, &entry);
1773 while (dbus_message_iter_get_arg_type(&entry) ==
1775 DBusMessageIter dicts;
1777 dbus_message_iter_recurse(&entry, &dicts);
1779 while (dbus_message_iter_get_arg_type(&dicts) ==
1781 int err = read_route_dict(routes, &dicts);
1784 dbus_message_iter_next(&dicts);
1787 dbus_message_iter_next(&entry);
1790 dbus_message_iter_next(array);
1796 static gboolean property_changed(DBusConnection *conn,
1797 DBusMessage *message,
1800 const char *path = dbus_message_get_path(message);
1801 struct connection_data *data = NULL;
1802 DBusMessageIter iter, value;
1803 bool ip_set = false;
1807 const char *signature = DBUS_TYPE_STRING_AS_STRING
1808 DBUS_TYPE_VARIANT_AS_STRING;
1810 if (!dbus_message_has_signature(message, signature)) {
1811 connman_error("vpn property signature \"%s\" does not match "
1813 dbus_message_get_signature(message), signature);
1817 data = g_hash_table_lookup(vpn_connections, get_ident(path));
1821 if (!dbus_message_iter_init(message, &iter))
1824 dbus_message_iter_get_basic(&iter, &key);
1826 dbus_message_iter_next(&iter);
1827 dbus_message_iter_recurse(&iter, &value);
1831 if (g_str_equal(key, "State")) {
1832 dbus_message_iter_get_basic(&value, &str);
1834 DBG("%s %s -> %s", data->path, data->state, str);
1836 if (g_str_equal(data->state, str))
1839 g_free(data->state);
1840 data->state = g_strdup(str);
1842 set_provider_state(data);
1843 } else if (g_str_equal(key, "Index")) {
1844 dbus_message_iter_get_basic(&value, &data->index);
1845 connman_provider_set_index(data->provider, data->index);
1846 } else if (g_str_equal(key, "IPv4")) {
1847 err = extract_ip(&value, AF_INET, data);
1849 } else if (g_str_equal(key, "IPv6")) {
1850 err = extract_ip(&value, AF_INET6, data);
1852 } else if (g_str_equal(key, "ServerRoutes")) {
1853 err = routes_changed(&value, data->server_routes);
1855 * Note that the vpnd will delay the route sending a bit
1856 * (in order to collect the routes from VPN client),
1857 * so we might have got the State changed property before
1858 * we got ServerRoutes. This means that we must try to set
1859 * the routes here because they would be left unset otherwise.
1862 set_routes(data->provider,
1863 CONNMAN_PROVIDER_ROUTE_SERVER);
1864 } else if (g_str_equal(key, "UserRoutes")) {
1865 err = routes_changed(&value, data->user_routes);
1867 set_routes(data->provider,
1868 CONNMAN_PROVIDER_ROUTE_USER);
1869 } else if (g_str_equal(key, "Nameservers")) {
1870 if (extract_nameservers(&value, data) == 0 &&
1872 connman_provider_set_nameservers(data->provider,
1874 } else if (g_str_equal(key, "Domain")) {
1875 dbus_message_iter_get_basic(&value, &str);
1876 g_free(data->domain);
1877 data->domain = g_strdup(str);
1878 connman_provider_set_domain(data->provider, data->domain);
1881 if (ip_set && err == 0) {
1882 err = connman_provider_set_ipaddress(data->provider, data->ip);
1884 DBG("setting provider IP address failed (%s/%d)",
1885 strerror(-err), -err);
1891 static int vpn_find_online_transport_cb(struct connman_service *service,
1894 if (connman_service_get_type(service) != CONNMAN_SERVICE_TYPE_VPN) {
1895 switch (connman_service_get_state(service)) {
1896 case CONNMAN_SERVICE_STATE_ONLINE:
1897 *((struct connman_service**)user_data) = service;
1907 static struct connman_service *vpn_find_online_transport()
1909 struct connman_service *service = NULL;
1911 connman_service_iterate_services(vpn_find_online_transport_cb,
1916 static bool vpn_is_valid_transport(struct connman_service *transport)
1919 struct connman_service *online;
1921 switch (connman_service_get_state(transport)) {
1922 case CONNMAN_SERVICE_STATE_READY:
1923 online = vpn_find_online_transport();
1925 /* Stay connected if there are no online services */
1929 DBG("%s is ready, %s is online, disconnecting",
1930 connman_service_get_identifier(transport),
1931 connman_service_get_identifier(online));
1934 case CONNMAN_SERVICE_STATE_ONLINE:
1935 online = vpn_find_online_transport();
1937 /* Check if our transport is still the default */
1938 if (online == transport)
1941 DBG("%s is replaced by %s as default, disconnecting",
1942 connman_service_get_identifier(transport),
1943 connman_service_get_identifier(online));
1950 DBG("transport gone");
1956 static void vpn_disconnect_check_provider(struct connection_data *data)
1958 if (provider_is_connected(data)) {
1959 /* With NULL service ident NULL is returned immediately */
1960 struct connman_service *service =
1961 connman_service_lookup_from_identifier
1962 (data->service_ident);
1964 if (!vpn_is_valid_transport(service)) {
1965 connman_provider_disconnect(data->provider);
1970 static void vpn_disconnect_check()
1972 GHashTableIter iter;
1976 g_hash_table_iter_init(&iter, vpn_connections);
1977 while (g_hash_table_iter_next(&iter, NULL, &value))
1978 vpn_disconnect_check_provider(value);
1981 static void vpn_service_add(struct connman_service *service, const char *name)
1983 vpn_disconnect_check();
1986 static void vpn_service_list_changed(struct connman_service *service)
1988 vpn_disconnect_check();
1991 static void vpn_service_state_changed(struct connman_service *service,
1992 enum connman_service_state state)
1994 vpn_disconnect_check();
1997 static const struct connman_notifier vpn_notifier = {
1999 .priority = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
2000 .default_changed = vpn_service_list_changed,
2001 .service_add = vpn_service_add,
2002 .service_remove = vpn_service_list_changed,
2003 .service_state_changed = vpn_service_state_changed
2006 static int vpn_init(void)
2010 connection = connman_dbus_get_connection();
2014 watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
2015 vpnd_created, vpnd_removed, &provider_driver, NULL);
2017 added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
2018 VPN_MANAGER_INTERFACE,
2019 CONNECTION_ADDED, connection_added,
2020 &provider_driver, NULL);
2022 removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
2023 VPN_MANAGER_INTERFACE,
2024 CONNECTION_REMOVED, connection_removed,
2027 property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
2028 VPN_CONNECTION_INTERFACE,
2029 PROPERTY_CHANGED, property_changed,
2032 if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
2037 err = connman_provider_driver_register(&provider_driver);
2039 vpn_connections = g_hash_table_new_full(g_str_hash,
2041 g_free, connection_destroy);
2043 vpnd_created(connection, &provider_driver);
2046 connman_notifier_register(&vpn_notifier);
2050 g_dbus_remove_watch(connection, watch);
2051 g_dbus_remove_watch(connection, added_watch);
2052 g_dbus_remove_watch(connection, removed_watch);
2053 g_dbus_remove_watch(connection, property_watch);
2055 dbus_connection_unref(connection);
2060 static void vpn_exit(void)
2062 g_dbus_remove_watch(connection, watch);
2063 g_dbus_remove_watch(connection, added_watch);
2064 g_dbus_remove_watch(connection, removed_watch);
2065 g_dbus_remove_watch(connection, property_watch);
2067 connman_notifier_unregister(&vpn_notifier);
2068 connman_provider_driver_unregister(&provider_driver);
2070 if (vpn_connections)
2071 g_hash_table_destroy(vpn_connections);
2073 dbus_connection_unref(connection);
2076 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
2077 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)