vpn: Hash provider data using the ident instead of path
[platform/upstream/connman.git] / plugins / vpn.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/socket.h>
29 #include <stdlib.h>
30 #include <glib.h>
31
32 #include <gdbus.h>
33
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>
44
45 #define DBUS_TIMEOUT 10000
46
47 static DBusConnection *connection;
48
49 static GHashTable *vpn_connections = NULL;
50 static gboolean starting_vpnd = TRUE;
51 static guint watch;
52 static guint added_watch;
53 static guint removed_watch;
54 static guint property_watch;
55
56 struct vpn_route {
57         int family;
58         char *network;
59         char *netmask;
60         char *gateway;
61 };
62
63 struct connection_data {
64         char *path;
65         char *ident;
66         struct connman_provider *provider;
67         int index;
68         DBusPendingCall *call;
69         connman_bool_t connect_pending;
70
71         char *state;
72         char *type;
73         char *name;
74         char *host;
75         char **host_ip;
76         char *domain;
77         char **nameservers;
78
79         GHashTable *server_routes;
80         GHashTable *user_routes;
81         GHashTable *setting_strings;
82
83         struct connman_ipaddress *ip;
84
85         GResolv *resolv;
86         guint resolv_id;
87 };
88
89 static int set_string(struct connman_provider *provider,
90                                         const char *key, const char *value)
91 {
92         struct connection_data *data;
93
94         data = connman_provider_get_data(provider);
95         if (data == NULL)
96                 return -EINVAL;
97
98         DBG("data %p provider %p key %s value %s", data, provider, key, value);
99
100         if (g_str_equal(key, "Type") == TRUE) {
101                 g_free(data->type);
102                 data->type = g_strdup(value);
103         } else if (g_str_equal(key, "Name") == TRUE) {
104                 g_free(data->name);
105                 data->name = g_strdup(value);
106         } else if (g_str_equal(key, "Host") == TRUE) {
107                 g_free(data->host);
108                 data->host = g_strdup(value);
109         } else if (g_str_equal(key, "VPN.Domain") == TRUE ||
110                                 g_str_equal(key, "Domain") == TRUE) {
111                 g_free(data->domain);
112                 data->domain = g_strdup(value);
113         } else
114                 g_hash_table_replace(data->setting_strings,
115                                 g_strdup(key), g_strdup(value));
116         return 0;
117 }
118
119 static const char *get_string(struct connman_provider *provider,
120                                                         const char *key)
121 {
122         struct connection_data *data;
123
124         data = connman_provider_get_data(provider);
125         if (data == NULL)
126                 return NULL;
127
128         DBG("data %p provider %p key %s", data, provider, key);
129
130         if (g_str_equal(key, "Type") == TRUE)
131                 return data->type;
132         else if (g_str_equal(key, "Name") == TRUE)
133                 return data->name;
134         else if (g_str_equal(key, "Host") == TRUE)
135                 return data->host;
136         else if (g_str_equal(key, "HostIP") == TRUE) {
137                 if (data->host_ip == NULL ||
138                                 data->host_ip[0] == NULL)
139                         return data->host;
140                 else
141                         return data->host_ip[0];
142         } else if (g_str_equal(key, "VPN.Domain") == TRUE)
143                 return data->domain;
144
145         return g_hash_table_lookup(data->setting_strings, key);
146 }
147
148 static char *get_ident(const char *path)
149 {
150         char *pos;
151
152         if (*path != '/')
153                 return NULL;
154
155         pos = strrchr(path, '/');
156         if (pos == NULL)
157                 return NULL;
158
159         return pos + 1;
160 }
161
162 static void cancel_host_resolv(struct connection_data *data)
163 {
164         if (data->resolv_id != 0)
165                 g_resolv_cancel_lookup(data->resolv, data->resolv_id);
166
167         data->resolv_id = 0;
168
169         g_resolv_unref(data->resolv);
170         data->resolv = NULL;
171 }
172
173 static gboolean remove_resolv(gpointer user_data)
174 {
175         struct connection_data *data = user_data;
176
177         cancel_host_resolv(data);
178
179         return FALSE;
180 }
181
182 static void resolv_result(GResolvResultStatus status,
183                                         char **results, gpointer user_data)
184 {
185         struct connection_data *data = user_data;
186
187         DBG("status %d", status);
188
189         if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
190                                                 g_strv_length(results) > 0)
191                 data->host_ip = g_strdupv(results);
192
193         /*
194          * We cannot unref the resolver here as resolv struct is manipulated
195          * by gresolv.c after we return from this callback.
196          */
197         g_timeout_add_seconds(0, remove_resolv, data);
198
199         data->resolv_id = 0;
200 }
201
202 static void resolv_host_addr(struct connection_data *data)
203 {
204         if (data->host == NULL)
205                 return;
206
207         if (connman_inet_check_ipaddress(data->host) > 0)
208                 return;
209
210         if (data->host_ip != NULL)
211                 return;
212
213         data->resolv = g_resolv_new(0);
214         if (data->resolv == NULL) {
215                 DBG("Cannot resolv %s", data->host);
216                 return;
217         }
218
219         DBG("Trying to resolv %s", data->host);
220
221         data->resolv_id = g_resolv_lookup_hostname(data->resolv, data->host,
222                                                 resolv_result, data);
223 }
224
225 static void set_provider_state(struct connection_data *data)
226 {
227         if (g_str_equal(data->state, "ready") == TRUE)
228                 connman_provider_set_state(data->provider,
229                                         CONNMAN_PROVIDER_STATE_READY);
230         else if (g_str_equal(data->state, "configuration") == TRUE)
231                 connman_provider_set_state(data->provider,
232                                         CONNMAN_PROVIDER_STATE_CONNECT);
233         else if (g_str_equal(data->state, "idle") == TRUE)
234                 connman_provider_set_state(data->provider,
235                                         CONNMAN_PROVIDER_STATE_IDLE);
236         else if (g_str_equal(data->state, "disconnect") == TRUE)
237                 connman_provider_set_state(data->provider,
238                                         CONNMAN_PROVIDER_STATE_DISCONNECT);
239         else if (g_str_equal(data->state, "failure") == TRUE)
240                 connman_provider_set_state(data->provider,
241                                         CONNMAN_PROVIDER_STATE_FAILURE);
242         else
243                 connman_provider_set_state(data->provider,
244                                         CONNMAN_PROVIDER_STATE_UNKNOWN);
245 }
246
247 static int create_provider(struct connection_data *data, void *user_data)
248 {
249         struct connman_provider_driver *driver = user_data;
250         int err;
251
252         DBG("%s", data->path);
253
254         data->provider = connman_provider_get(data->ident);
255         if (data->provider == NULL)
256                 return -ENOMEM;
257
258         DBG("provider %p name %s", data->provider, data->name);
259
260         connman_provider_set_data(data->provider, data);
261         connman_provider_set_driver(data->provider, driver);
262
263         err = connman_provider_create_service(data->provider);
264         if (err == 0) {
265                 if (g_str_equal(data->state, "ready") == TRUE) {
266                         connman_provider_set_index(data->provider,
267                                                         data->index);
268                         if (data->ip != NULL)
269                                 connman_provider_set_ipaddress(data->provider,
270                                                                 data->ip);
271                 }
272
273                 set_provider_state(data);
274         }
275
276         return 0;
277 }
278
279 static void destroy_route(gpointer user_data)
280 {
281         struct vpn_route *route = user_data;
282
283         g_free(route->network);
284         g_free(route->netmask);
285         g_free(route->gateway);
286         g_free(route);
287 }
288
289 static struct connection_data *create_connection_data(const char *path)
290 {
291         struct connection_data *data;
292
293         data = g_try_new0(struct connection_data, 1);
294         if (data == NULL)
295                 return NULL;
296
297         DBG("path %s", path);
298
299         data->path = g_strdup(path);
300         data->ident = g_strdup(get_ident(path));
301         data->index = -1;
302
303         data->setting_strings = g_hash_table_new_full(g_str_hash,
304                                                 g_str_equal, g_free, g_free);
305
306         data->server_routes = g_hash_table_new_full(g_direct_hash,
307                                         g_str_equal, g_free, destroy_route);
308         data->user_routes = g_hash_table_new_full(g_str_hash,
309                                         g_str_equal, g_free, destroy_route);
310
311         return data;
312 }
313
314 static int extract_ip(DBusMessageIter *array, int family,
315                                                 struct connection_data *data)
316 {
317         DBusMessageIter dict;
318         char *address = NULL, *gateway = NULL, *netmask = NULL, *peer = NULL;
319         unsigned char prefix_len;
320
321         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
322                 return -EINVAL;
323
324         dbus_message_iter_recurse(array, &dict);
325
326         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
327                 DBusMessageIter entry, value;
328                 const char *key;
329
330                 dbus_message_iter_recurse(&dict, &entry);
331                 dbus_message_iter_get_basic(&entry, &key);
332
333                 dbus_message_iter_next(&entry);
334                 dbus_message_iter_recurse(&entry, &value);
335
336                 if (g_str_equal(key, "Address") == TRUE) {
337                         dbus_message_iter_get_basic(&value, &address);
338                         DBG("address %s", address);
339                 } else if (g_str_equal(key, "Netmask") == TRUE) {
340                         dbus_message_iter_get_basic(&value, &netmask);
341                         DBG("netmask %s", netmask);
342                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
343                         dbus_message_iter_get_basic(&value, &netmask);
344                         DBG("prefix length %s", netmask);
345                 } else if (g_str_equal(key, "Peer") == TRUE) {
346                         dbus_message_iter_get_basic(&value, &peer);
347                         DBG("peer %s", peer);
348                 } else if (g_str_equal(key, "Gateway") == TRUE) {
349                         dbus_message_iter_get_basic(&value, &gateway);
350                         DBG("gateway %s", gateway);
351                 }
352
353                 dbus_message_iter_next(&dict);
354         }
355
356         data->ip = connman_ipaddress_alloc(family);
357         if (data->ip == NULL)
358                 return -ENOMEM;
359
360         switch (family) {
361         case AF_INET:
362                 connman_ipaddress_set_ipv4(data->ip, address, netmask,
363                                                                 gateway);
364                 break;
365         case AF_INET6:
366                 prefix_len = atoi(netmask);
367                 connman_ipaddress_set_ipv6(data->ip, address, prefix_len,
368                                                                 gateway);
369                 break;
370         default:
371                 return -EINVAL;
372         }
373
374         connman_ipaddress_set_peer(data->ip, peer);
375
376         return 0;
377 }
378
379 static int extract_nameservers(DBusMessageIter *array,
380                                                 struct connection_data *data)
381 {
382         DBusMessageIter entry;
383         char **nameservers = NULL;
384         int i = 0;
385
386         dbus_message_iter_recurse(array, &entry);
387
388         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
389                 const char *nameserver;
390
391                 dbus_message_iter_get_basic(&entry, &nameserver);
392
393                 nameservers = g_try_renew(char *, nameservers, i + 2);
394                 if (nameservers == NULL)
395                         return -ENOMEM;
396
397                 DBG("[%d] %s", i, nameserver);
398
399                 nameservers[i] = g_strdup(nameserver);
400                 if (nameservers[i] == NULL)
401                         return -ENOMEM;
402
403                 nameservers[++i] = NULL;
404
405                 dbus_message_iter_next(&entry);
406         }
407
408         g_strfreev(data->nameservers);
409         data->nameservers = nameservers;
410
411         return 0;
412 }
413
414 static void connect_reply(DBusPendingCall *call, void *user_data)
415 {
416         DBusMessage *reply;
417         DBusError error;
418
419         if (dbus_pending_call_get_completed(call) == FALSE)
420                 return;
421
422         DBG("user_data %p", user_data);
423
424         reply = dbus_pending_call_steal_reply(call);
425
426         dbus_error_init(&error);
427
428         if (dbus_set_error_from_message(&error, reply) == TRUE) {
429                 if (dbus_error_has_name(&error, CONNMAN_ERROR_INTERFACE
430                                                 ".InProgress") == FALSE) {
431                         connman_error("Connect reply: %s (%s)", error.message,
432                                                                 error.name);
433                         dbus_error_free(&error);
434                         goto done;
435                 }
436                 dbus_error_free(&error);
437         }
438
439         /*
440          * The vpn connection is up when we get a "ready" state
441          * property so at this point we do nothing for the provider
442          * state.
443          */
444
445 done:
446         dbus_message_unref(reply);
447
448         dbus_pending_call_unref(call);
449 }
450
451 static int connect_provider(struct connection_data *data, void *user_data)
452 {
453         DBusPendingCall *call;
454         DBusMessage *message;
455
456         DBG("data %p", data);
457
458         message = dbus_message_new_method_call(VPN_SERVICE, data->path,
459                                         VPN_CONNECTION_INTERFACE,
460                                         VPN_CONNECT);
461         if (message == NULL)
462                 return -ENOMEM;
463
464         if (dbus_connection_send_with_reply(connection, message,
465                                         &call, DBUS_TIMEOUT) == FALSE) {
466                 connman_error("Unable to call %s.%s()",
467                         VPN_CONNECTION_INTERFACE, VPN_CONNECT);
468                 dbus_message_unref(message);
469                 return -EINVAL;
470         }
471
472         if (call == NULL) {
473                 dbus_message_unref(message);
474                 return -EINVAL;
475         }
476
477         dbus_pending_call_set_notify(call, connect_reply, NULL, NULL);
478
479         dbus_message_unref(message);
480
481         return -EINPROGRESS;
482 }
483
484 static void add_connection(const char *path, DBusMessageIter *properties,
485                         void *user_data)
486 {
487         struct connection_data *data;
488         int err;
489         char *ident = get_ident(path);
490
491         data = g_hash_table_lookup(vpn_connections, ident);
492         if (data != NULL) {
493                 /*
494                  * We might have a dummy connection struct here that
495                  * was created by configuration_create_reply() so in
496                  * that case just continue.
497                  */
498                 if (data->connect_pending == FALSE)
499                         return;
500         } else {
501                 data = create_connection_data(path);
502                 if (data == NULL)
503                         return;
504         }
505
506         DBG("data %p path %s", data, path);
507
508         while (dbus_message_iter_get_arg_type(properties) ==
509                         DBUS_TYPE_DICT_ENTRY) {
510                 DBusMessageIter entry, value;
511                 const char *key;
512                 char *str;
513
514                 dbus_message_iter_recurse(properties, &entry);
515                 dbus_message_iter_get_basic(&entry, &key);
516
517                 dbus_message_iter_next(&entry);
518                 dbus_message_iter_recurse(&entry, &value);
519
520                 if (g_str_equal(key, "State") == TRUE) {
521                         dbus_message_iter_get_basic(&value, &str);
522                         DBG("state %s -> %s", data->state, str);
523                         data->state = g_strdup(str);
524                 } else if (g_str_equal(key, "IPv4") == TRUE) {
525                         extract_ip(&value, AF_INET, data);
526                 } else if (g_str_equal(key, "IPv6") == TRUE) {
527                         extract_ip(&value, AF_INET6, data);
528                 } else if (g_str_equal(key, "Name") == TRUE) {
529                         dbus_message_iter_get_basic(&value, &str);
530                         data->name = g_strdup(str);
531                 } else if (g_str_equal(key, "Type") == TRUE) {
532                         dbus_message_iter_get_basic(&value, &str);
533                         data->type = g_strdup(str);
534                 } else if (g_str_equal(key, "Host") == TRUE) {
535                         dbus_message_iter_get_basic(&value, &str);
536                         data->host = g_strdup(str);
537                 } else if (g_str_equal(key, "Domain") == TRUE) {
538                         dbus_message_iter_get_basic(&value, &str);
539                         data->domain = g_strdup(str);
540                 } else if (g_str_equal(key, "Nameservers") == TRUE) {
541                         extract_nameservers(&value, data);
542                 } else if (g_str_equal(key, "Index") == TRUE) {
543                         dbus_message_iter_get_basic(&value, &data->index);
544                 } else {
545                         if (dbus_message_iter_get_arg_type(&value) ==
546                                                         DBUS_TYPE_STRING) {
547                                 dbus_message_iter_get_basic(&value, &str);
548                                 g_hash_table_replace(data->setting_strings,
549                                                 g_strdup(key), g_strdup(str));
550                         } else {
551                                 DBG("unknown key %s", key);
552                         }
553                 }
554
555                 dbus_message_iter_next(properties);
556         }
557
558         g_hash_table_insert(vpn_connections, g_strdup(data->ident), data);
559
560         err = create_provider(data, user_data);
561         if (err < 0)
562                 goto out;
563
564         resolv_host_addr(data);
565
566         if (data->connect_pending == TRUE) {
567                 connect_provider(data, NULL);
568                 data->connect_pending = FALSE;
569         }
570
571         return;
572
573 out:
574         DBG("removing %s", data->ident);
575         g_hash_table_remove(vpn_connections, data->ident);
576 }
577
578 static void get_connections_reply(DBusPendingCall *call, void *user_data)
579 {
580         DBusMessage *reply;
581         DBusError error;
582         DBusMessageIter array, dict;
583         const char *signature = DBUS_TYPE_ARRAY_AS_STRING
584                 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
585                 DBUS_TYPE_OBJECT_PATH_AS_STRING
586                 DBUS_TYPE_ARRAY_AS_STRING
587                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
588                 DBUS_TYPE_STRING_AS_STRING
589                 DBUS_TYPE_VARIANT_AS_STRING
590                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
591                 DBUS_STRUCT_END_CHAR_AS_STRING;
592
593         if (dbus_pending_call_get_completed(call) == FALSE)
594                 return;
595
596         DBG("");
597
598         reply = dbus_pending_call_steal_reply(call);
599
600         dbus_error_init(&error);
601
602         if (dbus_set_error_from_message(&error, reply) == TRUE) {
603                 connman_error("%s", error.message);
604                 dbus_error_free(&error);
605                 goto done;
606         }
607
608         if (dbus_message_has_signature(reply, signature) == FALSE) {
609                 connman_error("vpnd signature \"%s\" does not match "
610                                                         "expected \"%s\"",
611                         dbus_message_get_signature(reply), signature);
612                 goto done;
613         }
614
615         if (dbus_message_iter_init(reply, &array) == FALSE)
616                 goto done;
617
618         dbus_message_iter_recurse(&array, &dict);
619
620         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
621                 DBusMessageIter value, properties;
622                 const char *path;
623
624                 dbus_message_iter_recurse(&dict, &value);
625                 dbus_message_iter_get_basic(&value, &path);
626
627                 dbus_message_iter_next(&value);
628                 dbus_message_iter_recurse(&value, &properties);
629
630                 add_connection(path, &properties, user_data);
631
632                 dbus_message_iter_next(&dict);
633         }
634
635 done:
636         dbus_message_unref(reply);
637
638         dbus_pending_call_unref(call);
639 }
640
641 static int get_connections(void *user_data)
642 {
643         DBusPendingCall *call;
644         DBusMessage *message;
645
646         DBG("");
647
648         message = dbus_message_new_method_call(VPN_SERVICE, "/",
649                                         VPN_MANAGER_INTERFACE,
650                                         GET_CONNECTIONS);
651         if (message == NULL)
652                 return -ENOMEM;
653
654         if (dbus_connection_send_with_reply(connection, message,
655                                         &call, DBUS_TIMEOUT) == FALSE) {
656                 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
657                                                         GET_CONNECTIONS);
658                 dbus_message_unref(message);
659                 return -EINVAL;
660         }
661
662         if (call == NULL) {
663                 dbus_message_unref(message);
664                 return -EINVAL;
665         }
666
667         dbus_pending_call_set_notify(call, get_connections_reply,
668                                                         user_data, NULL);
669
670         dbus_message_unref(message);
671
672         return -EINPROGRESS;
673 }
674
675 static int provider_probe(struct connman_provider *provider)
676 {
677         return 0;
678 }
679
680 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
681 {
682         DBusMessage *reply;
683         DBusError error;
684
685         if (dbus_pending_call_get_completed(call) == FALSE)
686                 return;
687
688         DBG("");
689
690         reply = dbus_pending_call_steal_reply(call);
691
692         dbus_error_init(&error);
693
694         if (dbus_set_error_from_message(&error, reply) == TRUE) {
695                 /*
696                  * If the returned error is NotFound, it means that we
697                  * have actually removed the provider in vpnd already.
698                  */
699                 if (dbus_error_has_name(&error, CONNMAN_ERROR_INTERFACE
700                                                 ".NotFound") == FALSE)
701                         connman_error("%s", error.message);
702
703                 dbus_error_free(&error);
704         }
705
706         dbus_message_unref(reply);
707
708         dbus_pending_call_unref(call);
709 }
710
711 static int provider_remove(struct connman_provider *provider)
712 {
713         DBusPendingCall *call;
714         DBusMessage *message;
715         struct connection_data *data;
716
717         data = connman_provider_get_data(provider);
718
719         DBG("provider %p data %p", provider, data);
720
721         /*
722          * When provider.c:provider_remove() calls this function,
723          * it will remove the provider itself after the call.
724          * This means that we cannot use the provider pointer later
725          * as it is no longer valid.
726          */
727         data->provider = NULL;
728
729         message = dbus_message_new_method_call(VPN_SERVICE, "/",
730                                         VPN_MANAGER_INTERFACE,
731                                         VPN_REMOVE);
732         if (message == NULL)
733                 return -ENOMEM;
734
735         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
736                                 NULL);
737
738         if (dbus_connection_send_with_reply(connection, message,
739                                         &call, DBUS_TIMEOUT) == FALSE) {
740                 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
741                                                         VPN_REMOVE);
742                 dbus_message_unref(message);
743                 return -EINVAL;
744         }
745
746         if (call == NULL) {
747                 dbus_message_unref(message);
748                 return -EINVAL;
749         }
750
751         dbus_pending_call_set_notify(call, remove_connection_reply,
752                                                         NULL, NULL);
753
754         dbus_message_unref(message);
755
756         return 0;
757 }
758
759 static int provider_connect(struct connman_provider *provider)
760 {
761         struct connection_data *data;
762
763         data = connman_provider_get_data(provider);
764         if (data == NULL)
765                 return -EINVAL;
766
767         return connect_provider(data, NULL);
768
769 }
770
771 static void disconnect_reply(DBusPendingCall *call, void *user_data)
772 {
773         DBusMessage *reply;
774         DBusError error;
775
776         if (dbus_pending_call_get_completed(call) == FALSE)
777                 return;
778
779         DBG("");
780
781         reply = dbus_pending_call_steal_reply(call);
782
783         dbus_error_init(&error);
784
785         if (dbus_set_error_from_message(&error, reply) == TRUE) {
786                 connman_error("%s", error.message);
787                 dbus_error_free(&error);
788                 goto done;
789         }
790
791 done:
792         dbus_message_unref(reply);
793
794         dbus_pending_call_unref(call);
795 }
796
797 static int disconnect_provider(struct connection_data *data)
798 {
799         DBusPendingCall *call;
800         DBusMessage *message;
801
802         DBG("data %p path %s", data, data->path);
803
804         message = dbus_message_new_method_call(VPN_SERVICE, data->path,
805                                         VPN_CONNECTION_INTERFACE,
806                                         VPN_DISCONNECT);
807         if (message == NULL)
808                 return -ENOMEM;
809
810         if (dbus_connection_send_with_reply(connection, message,
811                                         &call, DBUS_TIMEOUT) == FALSE) {
812                 connman_error("Unable to call %s.%s()",
813                         VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
814                 dbus_message_unref(message);
815                 return -EINVAL;
816         }
817
818         if (call == NULL) {
819                 dbus_message_unref(message);
820                 return -EINVAL;
821         }
822
823         dbus_pending_call_set_notify(call, disconnect_reply, NULL, NULL);
824
825         dbus_message_unref(message);
826
827         connman_provider_set_state(data->provider,
828                                         CONNMAN_PROVIDER_STATE_DISCONNECT);
829         /*
830          * We return 0 here instead of -EINPROGRESS because
831          * __connman_service_disconnect() needs to return something
832          * to gdbus so that gdbus will not call Disconnect() more
833          * than once. This way we do not need to pass the dbus reply
834          * message around the code.
835          */
836         return 0;
837 }
838
839 static int provider_disconnect(struct connman_provider *provider)
840 {
841         struct connection_data *data;
842
843         DBG("provider %p", provider);
844
845         data = connman_provider_get_data(provider);
846         if (data == NULL)
847                 return -EINVAL;
848
849         if (g_str_equal(data->state, "ready") == TRUE ||
850                         g_str_equal(data->state, "configuration") == TRUE)
851                 return disconnect_provider(data);
852
853         return 0;
854 }
855
856 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
857 {
858         DBusMessage *reply;
859         DBusError error;
860         DBusMessageIter iter;
861         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
862         const char *path;
863         char *ident;
864         struct connection_data *data;
865
866         if (dbus_pending_call_get_completed(call) == FALSE)
867                 return;
868
869         DBG("user %p", user_data);
870
871         reply = dbus_pending_call_steal_reply(call);
872
873         dbus_error_init(&error);
874
875         if (dbus_set_error_from_message(&error, reply) == TRUE) {
876                 connman_error("dbus error: %s", error.message);
877                 dbus_error_free(&error);
878                 goto done;
879         }
880
881         if (dbus_message_has_signature(reply, signature) == FALSE) {
882                 connman_error("vpn configuration signature \"%s\" does not "
883                                                 "match expected \"%s\"",
884                         dbus_message_get_signature(reply), signature);
885                 goto done;
886         }
887
888         if (dbus_message_iter_init(reply, &iter) == FALSE)
889                 goto done;
890
891         dbus_message_iter_get_basic(&iter, &path);
892
893         /*
894          * Then try to connect the VPN as expected by ConnectProvider API
895          */
896         ident = get_ident(path);
897
898         data = g_hash_table_lookup(vpn_connections, ident);
899         if (data == NULL) {
900                 /*
901                  * We have not yet received service created message
902                  * from vpnd. So create a dummy connection struct
903                  * and wait a while.
904                  */
905                 data = create_connection_data(path);
906                 data->connect_pending = TRUE;
907
908                 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
909
910         } else {
911                 connect_provider(data, NULL);
912         }
913
914 done:
915         dbus_message_unref(reply);
916
917         dbus_pending_call_unref(call);
918 }
919
920 static void set_dbus_ident(char *ident)
921 {
922         int i, len = strlen(ident);
923
924         for (i = 0; i < len; i++) {
925                 if (ident[i] >= '0' && ident[i] <= '9')
926                         continue;
927                 if (ident[i] >= 'a' && ident[i] <= 'z')
928                         continue;
929                 if (ident[i] >= 'A' && ident[i] <= 'Z')
930                         continue;
931                 ident[i] = '_';
932         }
933 }
934
935 static struct vpn_route *parse_user_route(const char *user_route)
936 {
937         char *network, *netmask;
938         struct vpn_route *route = NULL;
939         int family = PF_UNSPEC;
940         char **elems = g_strsplit(user_route, "/", 0);
941
942         if (elems == NULL)
943                 return NULL;
944
945         network = elems[0];
946         if (network == NULL || *network == '\0') {
947                 DBG("no network/netmask set");
948                 goto out;
949         }
950
951         netmask = elems[1];
952         if (netmask != NULL && *netmask == '\0') {
953                 DBG("no netmask set");
954                 goto out;
955         }
956
957         if (g_strrstr(network, ":") != NULL)
958                 family = AF_INET6;
959         else if (g_strrstr(network, ".") != NULL) {
960                 family = AF_INET;
961
962                 if (g_strrstr(netmask, ".") == NULL) {
963                         /* We have netmask length */
964                         in_addr_t addr;
965                         struct in_addr netmask_in;
966                         unsigned char prefix_len = 32;
967
968                         if (netmask != NULL) {
969                                 char *ptr;
970                                 long int value = strtol(netmask, &ptr, 10);
971                                 if (ptr != netmask && *ptr == '\0' &&
972                                                                 value <= 32)
973                                         prefix_len = value;
974                         }
975
976                         addr = 0xffffffff << (32 - prefix_len);
977                         netmask_in.s_addr = htonl(addr);
978                         netmask = inet_ntoa(netmask_in);
979
980                         DBG("network %s netmask %s", network, netmask);
981                 }
982         }
983
984         route = g_try_new(struct vpn_route, 1);
985         if (route == NULL)
986                 goto out;
987
988         route->network = g_strdup(network);
989         route->netmask = g_strdup(netmask);
990         route->gateway = NULL;
991         route->family = family;
992
993 out:
994         g_strfreev(elems);
995         return route;
996 }
997
998 static GSList *get_user_networks(DBusMessageIter *array)
999 {
1000         DBusMessageIter entry;
1001         GSList *list = NULL;
1002
1003         dbus_message_iter_recurse(array, &entry);
1004
1005         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1006                 const char *val;
1007                 struct vpn_route *route;
1008
1009                 dbus_message_iter_get_basic(&entry, &val);
1010
1011                 route = parse_user_route(val);
1012                 if (route != NULL)
1013                         list = g_slist_prepend(list, route);
1014
1015                 dbus_message_iter_next(&entry);
1016         }
1017
1018         return list;
1019 }
1020
1021 static void append_route(DBusMessageIter *iter, void *user_data)
1022 {
1023         struct vpn_route *route = user_data;
1024         DBusMessageIter item;
1025         int family = 0;
1026
1027         connman_dbus_dict_open(iter, &item);
1028
1029         if (route == NULL)
1030                 goto empty_dict;
1031
1032         if (route->family == AF_INET)
1033                 family = 4;
1034         else if (route->family == AF_INET6)
1035                 family = 6;
1036
1037         if (family != 0)
1038                 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1039                                         DBUS_TYPE_INT32, &family);
1040
1041         if (route->network != NULL)
1042                 connman_dbus_dict_append_basic(&item, "Network",
1043                                         DBUS_TYPE_STRING, &route->network);
1044
1045         if (route->netmask != NULL)
1046                 connman_dbus_dict_append_basic(&item, "Netmask",
1047                                         DBUS_TYPE_STRING, &route->netmask);
1048
1049         if (route->gateway != NULL)
1050                 connman_dbus_dict_append_basic(&item, "Gateway",
1051                                         DBUS_TYPE_STRING, &route->gateway);
1052
1053 empty_dict:
1054         connman_dbus_dict_close(iter, &item);
1055 }
1056
1057 static void append_routes(DBusMessageIter *iter, void *user_data)
1058 {
1059         GSList *list, *routes = user_data;
1060
1061         DBG("routes %p", routes);
1062
1063         for (list = routes; list != NULL; list = g_slist_next(list)) {
1064                 DBusMessageIter dict;
1065                 struct vpn_route *route = list->data;
1066
1067                 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1068                                                 &dict);
1069                 append_route(&dict, route);
1070                 dbus_message_iter_close_container(iter, &dict);
1071         }
1072 }
1073
1074 static int create_configuration(DBusMessage *msg)
1075 {
1076         DBusMessage *new_msg;
1077         DBusPendingCall *call;
1078         DBusMessageIter iter, array, new_iter, new_dict;
1079         const char *type = NULL, *name = NULL;
1080         const char *host = NULL, *domain = NULL;
1081         char *ident, *me = NULL;
1082         int err = 0;
1083         dbus_bool_t result;
1084         struct connection_data *data;
1085         GSList *networks = NULL;
1086
1087         /*
1088          * We copy the old message data into new message. We cannot
1089          * just use the old message as is because the user route
1090          * information is not in the same format in vpnd.
1091          */
1092         new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1093         dbus_message_iter_init_append(new_msg, &new_iter);
1094         connman_dbus_dict_open(&new_iter, &new_dict);
1095
1096         dbus_message_iter_init(msg, &iter);
1097         dbus_message_iter_recurse(&iter, &array);
1098
1099         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1100                 DBusMessageIter entry, value;
1101                 void *item_value;
1102                 const char *key;
1103                 int value_type;
1104
1105                 dbus_message_iter_recurse(&array, &entry);
1106                 dbus_message_iter_get_basic(&entry, &key);
1107
1108                 dbus_message_iter_next(&entry);
1109                 dbus_message_iter_recurse(&entry, &value);
1110
1111                 value_type = dbus_message_iter_get_arg_type(&value);
1112                 item_value = NULL;
1113
1114                 switch (value_type) {
1115                 case DBUS_TYPE_STRING:
1116                         dbus_message_iter_get_basic(&value, &item_value);
1117
1118                         if (g_str_equal(key, "Type") == TRUE) {
1119                                 type = (const char *)item_value;
1120                         } else if (g_str_equal(key, "Name") == TRUE) {
1121                                 name = (const char *)item_value;
1122                         } else if (g_str_equal(key, "Host") == TRUE) {
1123                                 host = (const char *)item_value;
1124                         } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1125                                 domain = (const char *)item_value;
1126                         }
1127
1128                         DBG("%s %s", key, (char *)item_value);
1129
1130                         if (item_value != NULL)
1131                                 connman_dbus_dict_append_basic(&new_dict, key,
1132                                                 value_type, &item_value);
1133                         break;
1134                 case DBUS_TYPE_ARRAY:
1135                         if (g_str_equal(key, "Networks") == TRUE) {
1136                                 networks = get_user_networks(&value);
1137                                 connman_dbus_dict_append_array(&new_dict,
1138                                                         "UserRoutes",
1139                                                         DBUS_TYPE_DICT_ENTRY,
1140                                                         append_routes,
1141                                                         networks);
1142                         }
1143                         break;
1144                 }
1145
1146                 dbus_message_iter_next(&array);
1147         }
1148
1149         connman_dbus_dict_close(&new_iter, &new_dict);
1150
1151         DBG("VPN type %s name %s host %s domain %s networks %p",
1152                 type, name, host, domain, networks);
1153
1154         if (host == NULL || domain == NULL) {
1155                 err = -EINVAL;
1156                 goto done;
1157         }
1158
1159         if (type == NULL || name == NULL) {
1160                 err = -EOPNOTSUPP;
1161                 goto done;
1162         }
1163
1164         ident = g_strdup_printf("%s_%s", host, domain);
1165         set_dbus_ident(ident);
1166
1167         DBG("ident %s", ident);
1168
1169         data = g_hash_table_lookup(vpn_connections, ident);
1170         if (data != NULL) {
1171                 if (data->call != NULL) {
1172                         connman_error("Dbus call already pending");
1173                         err = -EINPROGRESS;
1174                         goto done;
1175                 }
1176         } else {
1177                 data = create_connection_data(ident);
1178                 if (data == NULL) {
1179                         err = -ENOMEM;
1180                         goto done;
1181                 }
1182
1183                 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1184         }
1185
1186         /*
1187          * User called net.connman.Manager.ConnectProvider if we are here.
1188          * So use the data from original message in the new msg.
1189          */
1190         me = g_strdup(dbus_message_get_destination(msg));
1191
1192         dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1193         dbus_message_set_path(new_msg, "/");
1194         dbus_message_set_destination(new_msg, VPN_SERVICE);
1195         dbus_message_set_sender(new_msg, me);
1196         dbus_message_set_member(new_msg, "Create");
1197
1198         result = dbus_connection_send_with_reply(connection, new_msg,
1199                                                 &call, DBUS_TIMEOUT);
1200         if (result == FALSE || call == NULL) {
1201                 err = -EIO;
1202                 goto done;
1203         }
1204
1205         dbus_pending_call_set_notify(call, configuration_create_reply,
1206                                                                 NULL, NULL);
1207         data->call = call;
1208
1209 done:
1210         dbus_message_unref(new_msg);
1211
1212         if (networks != NULL)
1213                 g_slist_free_full(networks, destroy_route);
1214
1215
1216         g_free(me);
1217         return err;
1218 }
1219
1220 static connman_bool_t check_host(char **hosts, char *host)
1221 {
1222         int i;
1223
1224         if (hosts == NULL)
1225                 return FALSE;
1226
1227         for (i = 0; hosts[i] != NULL; i++) {
1228                 if (g_strcmp0(hosts[i], host) == 0)
1229                         return TRUE;
1230         }
1231
1232         return FALSE;
1233 }
1234
1235 static void set_route(struct connection_data *data, struct vpn_route *route)
1236 {
1237         /*
1238          * If the VPN administrator/user has given a route to
1239          * VPN server, then we must discard that because the
1240          * server cannot be contacted via VPN tunnel.
1241          */
1242         if (check_host(data->host_ip, route->network) == TRUE) {
1243                 DBG("Discarding VPN route to %s via %s at index %d",
1244                         route->network, route->gateway, data->index);
1245                 return;
1246         }
1247
1248         if (route->family == AF_INET6) {
1249                 unsigned char prefix_len = atoi(route->netmask);
1250
1251                 connman_inet_add_ipv6_network_route(data->index,
1252                                                         route->network,
1253                                                         route->gateway,
1254                                                         prefix_len);
1255         } else {
1256                 connman_inet_add_network_route(data->index, route->network,
1257                                                 route->gateway,
1258                                                 route->netmask);
1259         }
1260 }
1261
1262 static int set_routes(struct connman_provider *provider,
1263                                 enum connman_provider_route_type type)
1264 {
1265         struct connection_data *data;
1266         GHashTableIter iter;
1267         gpointer value, key;
1268
1269         DBG("provider %p", provider);
1270
1271         data = connman_provider_get_data(provider);
1272         if (data == NULL)
1273                 return -EINVAL;
1274
1275         if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1276                                         type == CONNMAN_PROVIDER_ROUTE_USER) {
1277                 g_hash_table_iter_init(&iter, data->user_routes);
1278
1279                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1280                         set_route(data, value);
1281         }
1282
1283         if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1284                                 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1285                 g_hash_table_iter_init(&iter, data->server_routes);
1286
1287                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
1288                         set_route(data, value);
1289         }
1290
1291         return 0;
1292 }
1293
1294 static connman_bool_t check_routes(struct connman_provider *provider)
1295 {
1296         struct connection_data *data;
1297
1298         DBG("provider %p", provider);
1299
1300         data = connman_provider_get_data(provider);
1301         if (data == NULL)
1302                 return FALSE;
1303
1304         if (data->user_routes != NULL &&
1305                         g_hash_table_size(data->user_routes) > 0)
1306                 return TRUE;
1307
1308         if (data->server_routes != NULL &&
1309                         g_hash_table_size(data->server_routes) > 0)
1310                 return TRUE;
1311
1312         return FALSE;
1313 }
1314
1315 static struct connman_provider_driver provider_driver = {
1316         .name = "VPN",
1317         .type = CONNMAN_PROVIDER_TYPE_VPN,
1318         .probe = provider_probe,
1319         .remove = provider_remove,
1320         .connect = provider_connect,
1321         .disconnect = provider_disconnect,
1322         .set_property = set_string,
1323         .get_property = get_string,
1324         .create = create_configuration,
1325         .set_routes = set_routes,
1326         .check_routes = check_routes,
1327 };
1328
1329 static void destroy_provider(struct connection_data *data)
1330 {
1331         DBG("data %p", data);
1332
1333         if (g_str_equal(data->state, "ready") == TRUE ||
1334                         g_str_equal(data->state, "configuration") == TRUE)
1335                 connman_provider_disconnect(data->provider);
1336
1337         if (data->call != NULL)
1338                 dbus_pending_call_cancel(data->call);
1339
1340         connman_provider_put(data->provider);
1341
1342         data->provider = NULL;
1343 }
1344
1345 static void connection_destroy(gpointer hash_data)
1346 {
1347         struct connection_data *data = hash_data;
1348
1349         DBG("data %p", data);
1350
1351         if (data->provider != NULL)
1352                 destroy_provider(data);
1353
1354         g_free(data->path);
1355         g_free(data->ident);
1356         g_free(data->state);
1357         g_free(data->type);
1358         g_free(data->name);
1359         g_free(data->host);
1360         g_free(data->domain);
1361         g_hash_table_destroy(data->server_routes);
1362         g_hash_table_destroy(data->user_routes);
1363         g_strfreev(data->nameservers);
1364         g_hash_table_destroy(data->setting_strings);
1365         connman_ipaddress_free(data->ip);
1366
1367         cancel_host_resolv(data);
1368
1369         g_free(data);
1370 }
1371
1372 static void vpnd_created(DBusConnection *conn, void *user_data)
1373 {
1374         DBG("connection %p", conn);
1375
1376         if (starting_vpnd == TRUE) {
1377                 vpn_connections = g_hash_table_new_full(g_str_hash,
1378                                                 g_str_equal,
1379                                                 g_free, connection_destroy);
1380                 get_connections(user_data);
1381                 starting_vpnd = FALSE;
1382         }
1383 }
1384
1385 static void vpnd_removed(DBusConnection *conn, void *user_data)
1386 {
1387         DBG("connection %p", conn);
1388
1389         g_hash_table_destroy(vpn_connections);
1390         vpn_connections = NULL;
1391         starting_vpnd = TRUE;
1392 }
1393
1394 static void remove_connection(DBusConnection *conn, const char *path)
1395 {
1396         DBG("path %s", path);
1397
1398         g_hash_table_remove(vpn_connections, get_ident(path));
1399 }
1400
1401 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1402                                 void *user_data)
1403 {
1404         const char *path;
1405         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1406
1407         if (dbus_message_has_signature(message, signature) == FALSE) {
1408                 connman_error("vpn removed signature \"%s\" does not match "
1409                                                         "expected \"%s\"",
1410                         dbus_message_get_signature(message), signature);
1411                 return TRUE;
1412         }
1413
1414         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1415                                 DBUS_TYPE_INVALID);
1416         remove_connection(conn, path);
1417         return TRUE;
1418 }
1419
1420 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1421                                 void *user_data)
1422 {
1423         DBusMessageIter iter, properties;
1424         const char *path;
1425         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1426                 DBUS_TYPE_ARRAY_AS_STRING
1427                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1428                 DBUS_TYPE_STRING_AS_STRING
1429                 DBUS_TYPE_VARIANT_AS_STRING
1430                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1431
1432         if (dbus_message_has_signature(message, signature) == FALSE) {
1433                 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1434                                                 "match expected \"%s\"",
1435                         dbus_message_get_signature(message), signature);
1436                 return TRUE;
1437         }
1438
1439         DBG("");
1440
1441         if (dbus_message_iter_init(message, &iter) == FALSE)
1442                 return TRUE;
1443
1444         dbus_message_iter_get_basic(&iter, &path);
1445
1446         dbus_message_iter_next(&iter);
1447         dbus_message_iter_recurse(&iter, &properties);
1448
1449         add_connection(path, &properties, user_data);
1450
1451         return TRUE;
1452 }
1453
1454 static int save_route(GHashTable *routes, int family, const char *network,
1455                         const char *netmask, const char *gateway)
1456 {
1457         struct vpn_route *route;
1458         char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1459
1460         DBG("family %d network %s netmask %s", family, network, netmask);
1461
1462         route = g_hash_table_lookup(routes, key);
1463         if (route == NULL) {
1464                 route = g_try_new0(struct vpn_route, 1);
1465                 if (route == NULL) {
1466                         connman_error("out of memory");
1467                         return -ENOMEM;
1468                 }
1469
1470                 route->family = family;
1471                 route->network = g_strdup(network);
1472                 route->netmask = g_strdup(netmask);
1473                 route->gateway = g_strdup(gateway);
1474
1475                 g_hash_table_replace(routes, key, route);
1476         } else
1477                 g_free(key);
1478
1479         return 0;
1480 }
1481
1482 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1483 {
1484         DBusMessageIter dict;
1485         const char *network, *netmask, *gateway;
1486         int family;
1487
1488         dbus_message_iter_recurse(dicts, &dict);
1489
1490         network = netmask = gateway = NULL;
1491         family = PF_UNSPEC;
1492
1493         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1494
1495                 DBusMessageIter entry, value;
1496                 const char *key;
1497
1498                 dbus_message_iter_recurse(&dict, &entry);
1499                 dbus_message_iter_get_basic(&entry, &key);
1500
1501                 dbus_message_iter_next(&entry);
1502                 dbus_message_iter_recurse(&entry, &value);
1503
1504                 if (g_str_equal(key, "ProtocolFamily") == TRUE) {
1505                         int pf;
1506                         dbus_message_iter_get_basic(&value, &pf);
1507                         switch (pf) {
1508                         case 4:
1509                                 family = AF_INET;
1510                                 break;
1511                         case 6:
1512                                 family = AF_INET6;
1513                                 break;
1514                         }
1515                         DBG("family %d", family);
1516                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1517                         dbus_message_iter_get_basic(&value, &netmask);
1518                         DBG("netmask %s", netmask);
1519                 } else if (g_str_equal(key, "Network") == TRUE) {
1520                         dbus_message_iter_get_basic(&value, &network);
1521                         DBG("host %s", network);
1522                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1523                         dbus_message_iter_get_basic(&value, &gateway);
1524                         DBG("gateway %s", gateway);
1525                 }
1526
1527                 dbus_message_iter_next(&dict);
1528         }
1529
1530         if (netmask == NULL || network == NULL || gateway == NULL) {
1531                 DBG("Value missing.");
1532                 return -EINVAL;
1533         }
1534
1535         return save_route(routes, family, network, netmask, gateway);
1536 }
1537
1538 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1539 {
1540         DBusMessageIter entry;
1541         int ret = -EINVAL;
1542
1543         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1544                 DBG("Expecting array, ignoring routes.");
1545                 return -EINVAL;
1546         }
1547
1548         while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1549
1550                 dbus_message_iter_recurse(array, &entry);
1551
1552                 while (dbus_message_iter_get_arg_type(&entry) ==
1553                                                         DBUS_TYPE_STRUCT) {
1554                         DBusMessageIter dicts;
1555
1556                         dbus_message_iter_recurse(&entry, &dicts);
1557
1558                         while (dbus_message_iter_get_arg_type(&dicts) ==
1559                                                         DBUS_TYPE_ARRAY) {
1560                                 int err = read_route_dict(routes, &dicts);
1561                                 if (ret != 0)
1562                                         ret = err;
1563                                 dbus_message_iter_next(&dicts);
1564                         }
1565
1566                         dbus_message_iter_next(&entry);
1567                 }
1568
1569                 dbus_message_iter_next(array);
1570         }
1571
1572         return ret;
1573 }
1574
1575 static gboolean property_changed(DBusConnection *conn,
1576                                 DBusMessage *message,
1577                                 void *user_data)
1578 {
1579         const char *path = dbus_message_get_path(message);
1580         struct connection_data *data = NULL;
1581         DBusMessageIter iter, value;
1582         connman_bool_t ip_set = FALSE;
1583         int err;
1584         char *str;
1585         const char *key;
1586         const char *signature = DBUS_TYPE_STRING_AS_STRING
1587                 DBUS_TYPE_VARIANT_AS_STRING;
1588
1589         if (dbus_message_has_signature(message, signature) == FALSE) {
1590                 connman_error("vpn property signature \"%s\" does not match "
1591                                                         "expected \"%s\"",
1592                         dbus_message_get_signature(message), signature);
1593                 return TRUE;
1594         }
1595
1596         data = g_hash_table_lookup(vpn_connections, get_ident(path));
1597         if (data == NULL)
1598                 return TRUE;
1599
1600         if (dbus_message_iter_init(message, &iter) == FALSE)
1601                 return TRUE;
1602
1603         dbus_message_iter_get_basic(&iter, &key);
1604
1605         dbus_message_iter_next(&iter);
1606         dbus_message_iter_recurse(&iter, &value);
1607
1608         DBG("key %s", key);
1609
1610         if (g_str_equal(key, "State") == TRUE) {
1611                 dbus_message_iter_get_basic(&value, &str);
1612
1613                 DBG("%s %s -> %s", data->path, data->state, str);
1614
1615                 if (g_str_equal(data->state, str) == TRUE)
1616                         return TRUE;
1617
1618                 g_free(data->state);
1619                 data->state = g_strdup(str);
1620
1621                 set_provider_state(data);
1622         } else if (g_str_equal(key, "Index") == TRUE) {
1623                 dbus_message_iter_get_basic(&value, &data->index);
1624                 connman_provider_set_index(data->provider, data->index);
1625         } else if (g_str_equal(key, "IPv4") == TRUE) {
1626                 err = extract_ip(&value, AF_INET, data);
1627                 ip_set = TRUE;
1628         } else if (g_str_equal(key, "IPv6") == TRUE) {
1629                 err = extract_ip(&value, AF_INET6, data);
1630                 ip_set = TRUE;
1631         } else if (g_str_equal(key, "ServerRoutes") == TRUE) {
1632                 err = routes_changed(&value, data->server_routes);
1633                 /*
1634                  * Note that the vpnd will delay the route sending a bit
1635                  * (in order to collect the routes from VPN client),
1636                  * so we might have got the State changed property before
1637                  * we got ServerRoutes. This means that we must try to set
1638                  * the routes here because they would be left unset otherwise.
1639                  */
1640                 if (err == 0)
1641                         set_routes(data->provider,
1642                                                 CONNMAN_PROVIDER_ROUTE_SERVER);
1643         } else if (g_str_equal(key, "UserRoutes") == TRUE) {
1644                 err = routes_changed(&value, data->user_routes);
1645                 if (err == 0)
1646                         set_routes(data->provider,
1647                                                 CONNMAN_PROVIDER_ROUTE_USER);
1648         } else if (g_str_equal(key, "Nameservers") == TRUE) {
1649                 extract_nameservers(&value, data);
1650         }
1651
1652         if (ip_set == TRUE && err == 0) {
1653                 err = connman_provider_set_ipaddress(data->provider, data->ip);
1654                 if (err < 0)
1655                         DBG("setting provider IP address failed (%s/%d)",
1656                                 strerror(-err), -err);
1657         }
1658
1659         return TRUE;
1660 }
1661
1662 static int vpn_init(void)
1663 {
1664         int err;
1665
1666         connection = connman_dbus_get_connection();
1667         if (connection == NULL)
1668                 return -EIO;
1669
1670         watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
1671                         vpnd_created, vpnd_removed, &provider_driver, NULL);
1672
1673         added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1674                                         VPN_MANAGER_INTERFACE,
1675                                         CONNECTION_ADDED, connection_added,
1676                                         &provider_driver, NULL);
1677
1678         removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1679                                         VPN_MANAGER_INTERFACE,
1680                                         CONNECTION_REMOVED, connection_removed,
1681                                         NULL, NULL);
1682
1683         property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1684                                         VPN_CONNECTION_INTERFACE,
1685                                         PROPERTY_CHANGED, property_changed,
1686                                         NULL, NULL);
1687
1688         if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
1689                 err = -EIO;
1690                 goto remove;
1691         }
1692
1693         err = connman_provider_driver_register(&provider_driver);
1694         if (err == 0)
1695                 vpnd_created(connection, &provider_driver);
1696
1697         return err;
1698
1699 remove:
1700         g_dbus_remove_watch(connection, watch);
1701         g_dbus_remove_watch(connection, added_watch);
1702         g_dbus_remove_watch(connection, removed_watch);
1703         g_dbus_remove_watch(connection, property_watch);
1704
1705         dbus_connection_unref(connection);
1706
1707         return err;
1708 }
1709
1710 static void vpn_exit(void)
1711 {
1712         g_dbus_remove_watch(connection, watch);
1713         g_dbus_remove_watch(connection, added_watch);
1714         g_dbus_remove_watch(connection, removed_watch);
1715         g_dbus_remove_watch(connection, property_watch);
1716
1717         connman_provider_driver_unregister(&provider_driver);
1718
1719         g_hash_table_destroy(vpn_connections);
1720
1721         dbus_connection_unref(connection);
1722 }
1723
1724 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
1725                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)