rtnl: Ignore netlink messages generated by wext
[platform/upstream/connman.git] / src / provider.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-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 <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gdbus.h>
31 #include <gweb/gresolv.h>
32
33 #include "connman.h"
34
35 static DBusConnection *connection = NULL;
36
37 static GHashTable *provider_hash = NULL;
38
39 static GSList *driver_list = NULL;
40
41 struct connman_route {
42         int family;
43         char *host;
44         char *netmask;
45         char *gateway;
46 };
47
48 struct connman_provider {
49         int refcount;
50         struct connman_service *vpn_service;
51         int index;
52         char *identifier;
53         char *name;
54         char *type;
55         char *host;
56         char *domain;
57         int family;
58         GHashTable *routes;
59         struct connman_provider_driver *driver;
60         void *driver_data;
61         GHashTable *setting_strings;
62         GHashTable *user_routes;
63         gchar **user_networks;
64         gsize num_user_networks;
65         GResolv *resolv;
66         char **host_ip;
67 };
68
69 static void resolv_result(GResolvResultStatus status,
70                                         char **results, gpointer user_data)
71 {
72         struct connman_provider *provider = user_data;
73
74         DBG("status %d", status);
75
76         if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
77                                                 g_strv_length(results) > 0)
78                 provider->host_ip = g_strdupv(results);
79
80         connman_provider_unref(provider);
81 }
82
83 static void provider_resolv_host_addr(struct connman_provider *provider)
84 {
85         if (provider->host == NULL)
86                 return;
87
88         if (connman_inet_check_ipaddress(provider->host) > 0)
89                 return;
90
91         if (provider->host_ip != NULL)
92                 return;
93
94         /*
95          * If the hostname is not numeric, try to resolv it. We do not wait
96          * the result as it might take some time. We will get the result
97          * before VPN will feed routes to us because VPN client will need
98          * the IP address also before VPN connection can be established.
99          */
100         provider->resolv = g_resolv_new(0);
101         if (provider->resolv == NULL) {
102                 DBG("Cannot resolv %s", provider->host);
103                 return;
104         }
105
106         DBG("Trying to resolv %s", provider->host);
107
108         connman_provider_ref(provider);
109
110         g_resolv_lookup_hostname(provider->resolv, provider->host,
111                                 resolv_result, provider);
112 }
113
114 void __connman_provider_append_properties(struct connman_provider *provider,
115                                                         DBusMessageIter *iter)
116 {
117         if (provider->host != NULL)
118                 connman_dbus_dict_append_basic(iter, "Host",
119                                         DBUS_TYPE_STRING, &provider->host);
120
121         if (provider->domain != NULL)
122                 connman_dbus_dict_append_basic(iter, "Domain",
123                                         DBUS_TYPE_STRING, &provider->domain);
124
125         if (provider->type != NULL)
126                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
127                                                  &provider->type);
128 }
129
130 int __connman_provider_append_user_route(struct connman_provider *provider,
131                         int family, const char *network, const char *netmask)
132 {
133         struct connman_route *route;
134         char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
135
136         DBG("family %d network %s netmask %s", family, network, netmask);
137
138         route = g_hash_table_lookup(provider->user_routes, key);
139         if (route == NULL) {
140                 route = g_try_new0(struct connman_route, 1);
141                 if (route == NULL) {
142                         connman_error("out of memory");
143                         return -ENOMEM;
144                 }
145
146                 route->family = family;
147                 route->host = g_strdup(network);
148                 route->netmask = g_strdup(netmask);
149
150                 g_hash_table_replace(provider->user_routes, key, route);
151         } else
152                 g_free(key);
153
154         return 0;
155 }
156
157 static void set_user_networks(struct connman_provider *provider,
158                                                         char **networks)
159 {
160         int i = 0;
161
162         while (networks[i] != NULL) {
163                 char **elems = g_strsplit(networks[i], "/", 0);
164                 char *network, *netmask;
165                 int family = PF_UNSPEC, ret;
166
167                 if (elems == NULL)
168                         break;
169
170                 network = elems[0];
171                 if (network == NULL || *network == '\0') {
172                         DBG("no network/netmask set");
173                         g_strfreev(elems);
174                         break;
175                 }
176
177                 netmask = elems[1];
178                 if (netmask != NULL && *netmask == '\0') {
179                         DBG("no netmask set");
180                         g_strfreev(elems);
181                         break;
182                 }
183
184                 if (g_strrstr(network, ":") != NULL)
185                         family = AF_INET6;
186                 else if (g_strrstr(network, ".") != NULL) {
187                         family = AF_INET;
188
189                         if (g_strrstr(netmask, ".") == NULL) {
190                                 /* We have netmask length */
191                                 in_addr_t addr;
192                                 struct in_addr netmask_in;
193                                 unsigned char prefix_len = 32;
194
195                                 if (netmask != NULL)
196                                         prefix_len = atoi(netmask);
197
198                                 addr = 0xffffffff << (32 - prefix_len);
199                                 netmask_in.s_addr = htonl(addr);
200                                 netmask = inet_ntoa(netmask_in);
201
202                                 DBG("network %s netmask %s", network, netmask);
203                         }
204                 }
205
206                 ret = __connman_provider_append_user_route(provider,
207                                                 family, network, netmask);
208                 g_strfreev(elems);
209
210                 if (ret != 0)
211                         break;
212
213                 i++;
214         }
215 }
216
217 static int provider_load_from_keyfile(struct connman_provider *provider,
218                 GKeyFile *keyfile)
219 {
220         gsize idx = 0;
221         gchar **settings;
222         gchar *key, *value;
223         gsize length;
224
225         settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
226                                 NULL);
227         if (settings == NULL) {
228                 g_key_file_free(keyfile);
229                 return -ENOENT;
230         }
231
232         while (idx < length) {
233                 key = settings[idx];
234                 if (key != NULL) {
235                         if (g_str_equal(key, "Networks") == TRUE) {
236                                 g_strfreev(provider->user_networks);
237                                 provider->user_networks =
238                                         g_key_file_get_string_list(keyfile,
239                                                 provider->identifier,
240                                                 key,
241                                                 &provider->num_user_networks,
242                                                 NULL);
243                         } else {
244                                 value = g_key_file_get_string(keyfile,
245                                                         provider->identifier,
246                                                         key, NULL);
247                                 connman_provider_set_string(provider, key,
248                                                         value);
249                                 g_free(value);
250                         }
251                 }
252                 idx += 1;
253         }
254         g_strfreev(settings);
255
256         if (provider->user_networks != NULL)
257                 set_user_networks(provider, provider->user_networks);
258
259         return 0;
260 }
261
262 static int connman_provider_load(struct connman_provider *provider)
263 {
264         GKeyFile *keyfile;
265
266         DBG("provider %p", provider);
267
268         keyfile = __connman_storage_load_provider(provider->identifier);
269         if (keyfile == NULL)
270                 return -ENOENT;
271
272         provider_load_from_keyfile(provider, keyfile);
273
274         g_key_file_free(keyfile);
275         return 0;
276 }
277
278 static int connman_provider_save(struct connman_provider *provider)
279 {
280         GKeyFile *keyfile;
281
282         DBG("provider %p", provider);
283
284         keyfile = g_key_file_new();
285         if (keyfile == NULL)
286                 return -ENOMEM;
287
288         g_key_file_set_string(keyfile, provider->identifier,
289                         "Name", provider->name);
290         g_key_file_set_string(keyfile, provider->identifier,
291                         "Type", provider->type);
292         g_key_file_set_string(keyfile, provider->identifier,
293                         "Host", provider->host);
294         g_key_file_set_string(keyfile, provider->identifier,
295                         "VPN.Domain", provider->domain);
296         if (provider->user_networks != NULL)
297                 g_key_file_set_string_list(keyfile, provider->identifier,
298                                 "Networks",
299                                 (const gchar **)provider->user_networks,
300                                 provider->num_user_networks);
301
302         if (provider->driver != NULL && provider->driver->save != NULL)
303                 provider->driver->save(provider, keyfile);
304
305         __connman_storage_save_provider(keyfile, provider->identifier);
306         g_key_file_free(keyfile);
307
308         return 0;
309 }
310
311 static struct connman_provider *connman_provider_lookup(const char *identifier)
312 {
313         struct connman_provider *provider = NULL;
314
315         provider = g_hash_table_lookup(provider_hash, identifier);
316
317         return provider;
318 }
319
320 static gboolean match_driver(struct connman_provider *provider,
321                                 struct connman_provider_driver *driver)
322 {
323         if (g_strcmp0(driver->name, provider->type) == 0)
324                 return TRUE;
325
326         return FALSE;
327 }
328
329 static int provider_probe(struct connman_provider *provider)
330 {
331         GSList *list;
332
333         DBG("provider %p name %s", provider, provider->name);
334
335         if (provider->driver != NULL)
336                 return -EALREADY;
337
338         for (list = driver_list; list; list = list->next) {
339                 struct connman_provider_driver *driver = list->data;
340
341                 if (match_driver(provider, driver) == FALSE)
342                         continue;
343
344                 DBG("driver %p name %s", driver, driver->name);
345
346                 if (driver->probe != NULL && driver->probe(provider) == 0) {
347                         provider->driver = driver;
348                         break;
349                 }
350         }
351
352         if (provider->driver == NULL)
353                 return -ENODEV;
354
355         return 0;
356 }
357
358 static void provider_remove(struct connman_provider *provider)
359 {
360         if (provider->driver != NULL) {
361                 provider->driver->remove(provider);
362                 provider->driver = NULL;
363         }
364 }
365
366 static int provider_register(struct connman_provider *provider)
367 {
368         return provider_probe(provider);
369 }
370
371 static void provider_unregister(struct connman_provider *provider)
372 {
373         provider_remove(provider);
374 }
375
376 struct connman_provider *
377 connman_provider_ref_debug(struct connman_provider *provider,
378                         const char *file, int line, const char *caller)
379 {
380         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
381                 file, line, caller);
382
383         __sync_fetch_and_add(&provider->refcount, 1);
384
385         return provider;
386 }
387
388 static void provider_destruct(struct connman_provider *provider)
389 {
390         DBG("provider %p", provider);
391
392         g_free(provider->name);
393         g_free(provider->type);
394         g_free(provider->host);
395         g_free(provider->domain);
396         g_free(provider->identifier);
397         g_strfreev(provider->user_networks);
398         g_hash_table_destroy(provider->routes);
399         g_hash_table_destroy(provider->user_routes);
400         g_hash_table_destroy(provider->setting_strings);
401         if (provider->resolv != NULL) {
402                 g_resolv_unref(provider->resolv);
403                 provider->resolv = NULL;
404         }
405         g_strfreev(provider->host_ip);
406         g_free(provider);
407 }
408
409 void connman_provider_unref_debug(struct connman_provider *provider,
410                                 const char *file, int line, const char *caller)
411 {
412         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
413                 file, line, caller);
414
415         if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
416                 return;
417
418         provider_remove(provider);
419
420         provider_destruct(provider);
421 }
422
423 static int provider_indicate_state(struct connman_provider *provider,
424                                         enum connman_service_state state)
425 {
426         DBG("state %d", state);
427
428         __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
429                                         CONNMAN_IPCONFIG_TYPE_IPV4);
430
431         return __connman_service_ipconfig_indicate_state(provider->vpn_service,
432                                         state, CONNMAN_IPCONFIG_TYPE_IPV6);
433 }
434
435 int __connman_provider_disconnect(struct connman_provider *provider)
436 {
437         int err;
438
439         DBG("provider %p", provider);
440
441         if (provider->driver != NULL && provider->driver->disconnect != NULL)
442                 err = provider->driver->disconnect(provider);
443         else
444                 return -EOPNOTSUPP;
445
446         if (provider->vpn_service != NULL)
447                 provider_indicate_state(provider,
448                                         CONNMAN_SERVICE_STATE_DISCONNECT);
449
450         if (err < 0) {
451                 if (err != -EINPROGRESS)
452                         return err;
453
454                 return -EINPROGRESS;
455         }
456
457         return 0;
458 }
459
460 int __connman_provider_connect(struct connman_provider *provider)
461 {
462         int err;
463
464         DBG("provider %p", provider);
465
466         if (provider->driver != NULL && provider->driver->connect != NULL) {
467                 provider_resolv_host_addr(provider);
468                 err = provider->driver->connect(provider);
469         } else
470                 return -EOPNOTSUPP;
471
472         if (err < 0) {
473                 if (err != -EINPROGRESS)
474                         return err;
475
476                 provider_indicate_state(provider,
477                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
478
479                 return -EINPROGRESS;
480         }
481
482         return 0;
483 }
484
485 int __connman_provider_remove(const char *path)
486 {
487         struct connman_provider *provider;
488         GHashTableIter iter;
489         gpointer value, key;
490
491         DBG("path %s", path);
492
493         g_hash_table_iter_init(&iter, provider_hash);
494         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
495                 const char *srv_path;
496                 provider = value;
497
498                 if (provider->vpn_service == NULL)
499                         continue;
500
501                 srv_path = __connman_service_get_path(provider->vpn_service);
502
503                 if (g_strcmp0(srv_path, path) == 0) {
504                         DBG("Removing VPN %s", provider->identifier);
505
506                         provider_unregister(provider);
507                         g_hash_table_remove(provider_hash,
508                                                 provider->identifier);
509                         return 0;
510                 }
511         }
512
513         return -ENXIO;
514 }
515
516 static connman_bool_t check_host(char **hosts, char *host)
517 {
518         int i;
519
520         if (hosts == NULL)
521                 return FALSE;
522
523         for (i = 0; hosts[i] != NULL; i++) {
524                 if (g_strcmp0(hosts[i], host) == 0)
525                         return TRUE;
526         }
527
528         return FALSE;
529 }
530
531 static void provider_append_routes(gpointer key, gpointer value,
532                                         gpointer user_data)
533 {
534         struct connman_route *route = value;
535         struct connman_provider *provider = user_data;
536         int index = provider->index;
537
538         /*
539          * If the VPN administrator/user has given a route to
540          * VPN server, then we must discard that because the
541          * server cannot be contacted via VPN tunnel.
542          */
543         if (check_host(provider->host_ip, route->host) == TRUE) {
544                 DBG("Discarding VPN route to %s via %s at index %d",
545                         route->host, route->gateway, index);
546                 return;
547         }
548
549         if (route->family == AF_INET6) {
550                 unsigned char prefix_len = atoi(route->netmask);
551
552                 connman_inet_add_ipv6_network_route(index, route->host,
553                                                         route->gateway,
554                                                         prefix_len);
555         } else {
556                 connman_inet_add_network_route(index, route->host,
557                                                 route->gateway,
558                                                 route->netmask);
559         }
560 }
561
562 static int set_connected(struct connman_provider *provider,
563                                         connman_bool_t connected)
564 {
565         struct connman_service *service = provider->vpn_service;
566         struct connman_ipconfig *ipconfig;
567
568         if (service == NULL)
569                 return -ENODEV;
570
571         ipconfig = __connman_service_get_ipconfig(service, provider->family);
572
573         if (connected == TRUE) {
574                 if (ipconfig == NULL) {
575                         provider_indicate_state(provider,
576                                                 CONNMAN_SERVICE_STATE_FAILURE);
577                         return -EIO;
578                 }
579
580                 __connman_ipconfig_address_add(ipconfig);
581                 __connman_ipconfig_gateway_add(ipconfig);
582
583                 provider_indicate_state(provider,
584                                         CONNMAN_SERVICE_STATE_READY);
585
586                 g_hash_table_foreach(provider->routes, provider_append_routes,
587                                         provider);
588
589                 g_hash_table_foreach(provider->user_routes, provider_append_routes,
590                                         provider);
591
592         } else {
593                 if (ipconfig != NULL) {
594                         provider_indicate_state(provider,
595                                         CONNMAN_SERVICE_STATE_DISCONNECT);
596                         __connman_ipconfig_gateway_remove(ipconfig);
597                 }
598
599                 provider_indicate_state(provider,
600                                         CONNMAN_SERVICE_STATE_IDLE);
601         }
602
603         return 0;
604 }
605
606 int connman_provider_set_state(struct connman_provider *provider,
607                                         enum connman_provider_state state)
608 {
609         if (provider == NULL || provider->vpn_service == NULL)
610                 return -EINVAL;
611
612         switch (state) {
613         case CONNMAN_PROVIDER_STATE_UNKNOWN:
614                 return -EINVAL;
615         case CONNMAN_PROVIDER_STATE_IDLE:
616                 return set_connected(provider, FALSE);
617         case CONNMAN_PROVIDER_STATE_CONNECT:
618                 return provider_indicate_state(provider,
619                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
620         case CONNMAN_PROVIDER_STATE_READY:
621                 return set_connected(provider, TRUE);
622         case CONNMAN_PROVIDER_STATE_DISCONNECT:
623                 return provider_indicate_state(provider,
624                                         CONNMAN_SERVICE_STATE_DISCONNECT);
625         case CONNMAN_PROVIDER_STATE_FAILURE:
626                 return provider_indicate_state(provider,
627                                         CONNMAN_SERVICE_STATE_FAILURE);
628         }
629
630         return -EINVAL;
631 }
632
633 int connman_provider_indicate_error(struct connman_provider *provider,
634                                         enum connman_provider_error error)
635 {
636         enum connman_service_error service_error;
637
638         switch (error) {
639         case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
640                 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
641                 break;
642         case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
643                 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
644                 break;
645         case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
646                 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
647                 break;
648         default:
649                 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
650                 break;
651         }
652
653         return __connman_service_indicate_error(provider->vpn_service,
654                                                         service_error);
655 }
656
657 static void unregister_provider(gpointer data)
658 {
659         struct connman_provider *provider = data;
660
661         DBG("provider %p service %p", provider, provider->vpn_service);
662
663         if (provider->vpn_service != NULL) {
664                 connman_service_unref(provider->vpn_service);
665                 provider->vpn_service = NULL;
666         }
667
668         connman_provider_unref(provider);
669 }
670
671 static void destroy_route(gpointer user_data)
672 {
673         struct connman_route *route = user_data;
674
675         g_free(route->host);
676         g_free(route->netmask);
677         g_free(route->gateway);
678         g_free(route);
679 }
680
681 static void provider_initialize(struct connman_provider *provider)
682 {
683         DBG("provider %p", provider);
684
685         provider->index = 0;
686         provider->name = NULL;
687         provider->type = NULL;
688         provider->domain = NULL;
689         provider->identifier = NULL;
690         provider->user_networks = NULL;
691         provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
692                                         NULL, destroy_route);
693         provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
694                                         g_free, destroy_route);
695         provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
696                                                         g_free, g_free);
697 }
698
699 static struct connman_provider *connman_provider_new(void)
700 {
701         struct connman_provider *provider;
702
703         provider = g_try_new0(struct connman_provider, 1);
704         if (provider == NULL)
705                 return NULL;
706
707         provider->refcount = 1;
708
709         DBG("provider %p", provider);
710         provider_initialize(provider);
711
712         return provider;
713 }
714
715 static struct connman_provider *connman_provider_get(const char *identifier)
716 {
717         struct connman_provider *provider;
718
719         provider = g_hash_table_lookup(provider_hash, identifier);
720         if (provider != NULL)
721                 return provider;
722
723         provider = connman_provider_new();
724         if (provider == NULL)
725                 return NULL;
726
727         DBG("provider %p", provider);
728
729         provider->identifier = g_strdup(identifier);
730
731         g_hash_table_insert(provider_hash, provider->identifier, provider);
732
733         return provider;
734 }
735
736 static void provider_dbus_ident(char *ident)
737 {
738         int i, len = strlen(ident);
739
740         for (i = 0; i < len; i++) {
741                 if (ident[i] >= '0' && ident[i] <= '9')
742                         continue;
743                 if (ident[i] >= 'a' && ident[i] <= 'z')
744                         continue;
745                 if (ident[i] >= 'A' && ident[i] <= 'Z')
746                         continue;
747                 ident[i] = '_';
748         }
749 }
750
751 static struct connman_provider *provider_create_from_keyfile(GKeyFile *keyfile,
752                 const char *ident)
753 {
754         struct connman_provider *provider;
755
756         if (keyfile == NULL || ident == NULL)
757                 return NULL;
758
759         provider = connman_provider_lookup(ident);
760         if (provider == NULL) {
761                 provider = connman_provider_get(ident);
762                 if (provider == NULL) {
763                         DBG("can not create provider");
764                         return NULL;
765                 }
766
767                 provider_load_from_keyfile(provider, keyfile);
768
769                 if (provider->name == NULL || provider->host == NULL ||
770                                 provider->domain == NULL) {
771                         DBG("cannot get name, host or domain");
772                         connman_provider_unref(provider);
773                         return NULL;
774                 }
775
776                 provider_register(provider);
777         }
778         return provider;
779 }
780
781 static int provider_create_service(struct connman_provider *provider)
782 {
783         if (provider->vpn_service != NULL) {
784                 connman_bool_t connected;
785
786                 connected = __connman_service_is_connected_state(
787                         provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV4);
788                 if (connected == TRUE)
789                         return -EALREADY;
790
791                 connected = __connman_service_is_connected_state(
792                         provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV6);
793                 if (connected == TRUE)
794                         return -EALREADY;
795
796                 return 0;
797         }
798
799         provider->vpn_service =
800                 __connman_service_create_from_provider(provider);
801
802         if (provider->vpn_service == NULL)
803                 return -EOPNOTSUPP;
804
805         return 0;
806 }
807
808 static void provider_create_all_from_type(const char *provider_type)
809 {
810         unsigned int i;
811         char **providers;
812         char *id, *type;
813         GKeyFile *keyfile;
814         struct connman_provider *provider;
815
816         DBG("provider type %s", provider_type);
817
818         providers = __connman_storage_get_providers();
819
820         for (i = 0; providers[i] != NULL; i+=1) {
821
822                 if (strncmp(providers[i], "provider_", 9) != 0)
823                         continue;
824
825                 id = providers[i] + 9;
826                 keyfile = __connman_storage_load_provider(id);
827
828                 if (keyfile == NULL)
829                         continue;
830
831                 type = g_key_file_get_string(keyfile, id, "Type", NULL);
832
833                 DBG("keyfile %p id %s type %s", keyfile, id, type);
834
835                 if (strcmp(provider_type, type) != 0) {
836                         g_free(type);
837                         g_key_file_free(keyfile);
838                         continue;
839                 }
840
841                 provider = provider_create_from_keyfile(keyfile, id);
842                 if (provider != NULL) {
843                         if (provider_create_service(provider) == -EOPNOTSUPP) {
844                                 DBG("could not create service");
845                                 connman_provider_unref(provider);
846                         }
847                 }
848
849                 g_free(type);
850                 g_key_file_free(keyfile);
851         }
852         g_strfreev(providers);
853 }
854
855 static char **get_user_networks(DBusMessageIter *array, int *count)
856 {
857         DBusMessageIter entry;
858         char **networks = NULL;
859         GSList *list = NULL, *l;
860         int len;
861
862         dbus_message_iter_recurse(array, &entry);
863
864         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
865                 const char *val;
866                 dbus_message_iter_get_basic(&entry, &val);
867
868                 list = g_slist_prepend(list, g_strdup(val));
869                 dbus_message_iter_next(&entry);
870         }
871
872         len = g_slist_length(list);
873         if (len == 0)
874                 goto out;
875
876         networks = g_try_new(char *, len + 1);
877         if (networks == NULL)
878                 goto out;
879
880         *count = len;
881         networks[len] = 0;
882
883         for (l = list; l != NULL; l = g_slist_next(l))
884                 networks[--len] = l->data;
885
886 out:
887         g_slist_free(list);
888
889         return networks;
890 }
891
892 int __connman_provider_create_and_connect(DBusMessage *msg)
893 {
894         struct connman_provider *provider;
895         DBusMessageIter iter, array;
896         const char *type = NULL, *name = NULL, *service_path;
897         const char *host = NULL, *domain = NULL;
898         char **networks = NULL;
899         char *ident;
900         int err, count = 0;
901         connman_bool_t connecting = FALSE;
902
903         dbus_message_iter_init(msg, &iter);
904         dbus_message_iter_recurse(&iter, &array);
905
906         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
907                 DBusMessageIter entry, value;
908                 const char *key;
909
910                 dbus_message_iter_recurse(&array, &entry);
911                 dbus_message_iter_get_basic(&entry, &key);
912
913                 dbus_message_iter_next(&entry);
914                 dbus_message_iter_recurse(&entry, &value);
915
916                 switch (dbus_message_iter_get_arg_type(&value)) {
917                 case DBUS_TYPE_STRING:
918                         if (g_str_equal(key, "Type") == TRUE)
919                                 dbus_message_iter_get_basic(&value, &type);
920                         else if (g_str_equal(key, "Name") == TRUE)
921                                 dbus_message_iter_get_basic(&value, &name);
922                         else if (g_str_equal(key, "Host") == TRUE)
923                                 dbus_message_iter_get_basic(&value, &host);
924                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
925                                 dbus_message_iter_get_basic(&value, &domain);
926                         break;
927                 case DBUS_TYPE_ARRAY:
928                         if (g_str_equal(key, "Networks") == TRUE)
929                                 networks = get_user_networks(&value, &count);
930                         break;
931                 }
932
933                 dbus_message_iter_next(&array);
934         }
935
936         if (host == NULL || domain == NULL)
937                 return -EINVAL;
938
939         DBG("Type %s name %s networks %p", type, name, networks);
940
941         if (type == NULL || name == NULL)
942                 return -EOPNOTSUPP;
943
944         ident = g_strdup_printf("%s_%s", host, domain);
945         provider_dbus_ident(ident);
946
947         DBG("ident %s", ident);
948
949         provider = connman_provider_lookup(ident);
950         if (provider == NULL) {
951                 provider = connman_provider_get(ident);
952                 if (provider == NULL) {
953                         DBG("can not create provider");
954                         g_free(ident);
955                         return -EOPNOTSUPP;
956                 }
957
958                 provider->host = g_strdup(host);
959                 provider->domain = g_strdup(domain);
960                 provider->name = g_strdup(name);
961                 provider->type = g_strdup(type);
962
963                 if (provider_register(provider) == 0)
964                         connman_provider_load(provider);
965
966                 provider_resolv_host_addr(provider);
967         }
968
969         if (networks != NULL) {
970                 g_strfreev(provider->user_networks);
971                 provider->user_networks = networks;
972                 provider->num_user_networks = count;
973                 set_user_networks(provider, provider->user_networks);
974         }
975
976         dbus_message_iter_init(msg, &iter);
977         dbus_message_iter_recurse(&iter, &array);
978
979         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
980                 DBusMessageIter entry, value;
981                 const char *key, *str;
982
983                 dbus_message_iter_recurse(&array, &entry);
984                 dbus_message_iter_get_basic(&entry, &key);
985
986                 dbus_message_iter_next(&entry);
987                 dbus_message_iter_recurse(&entry, &value);
988
989                 switch (dbus_message_iter_get_arg_type(&value)) {
990                 case DBUS_TYPE_STRING:
991                         dbus_message_iter_get_basic(&value, &str);
992                         connman_provider_set_string(provider, key, str);
993                         break;
994                 }
995
996                 dbus_message_iter_next(&array);
997         }
998
999         g_free(ident);
1000
1001         err = provider_create_service(provider);
1002         if (err == -EALREADY) {
1003                 DBG("provider already connected");
1004         } else {
1005                 if (err == -EOPNOTSUPP) {
1006                         goto unref;
1007                 } else {
1008                         if (__connman_service_is_provider_pending(
1009                                         provider->vpn_service) == TRUE) {
1010                                 DBG("Provider %p connection already pending",
1011                                         provider);
1012                                 dbus_message_ref(msg);
1013                                 __connman_service_reply_dbus_pending(msg,
1014                                                         EINPROGRESS, NULL);
1015                                 return 0;
1016                         }
1017
1018                         err = __connman_service_connect(provider->vpn_service);
1019
1020                         if (err < 0 && err != -EINPROGRESS)
1021                                 goto failed;
1022
1023                         if (err == -EINPROGRESS)
1024                                 connecting = TRUE;
1025                 }
1026         }
1027
1028         if (connecting == TRUE) {
1029                 /*
1030                  * Save the dbus message so that we can reply the
1031                  * caller when VPN connection is established.
1032                  */
1033                 dbus_message_ref(msg);
1034
1035                 __connman_service_set_provider_pending(provider->vpn_service,
1036                                                                         msg);
1037                 connman_provider_save(provider);
1038
1039                 return 0;
1040         }
1041
1042         service_path = __connman_service_get_path(provider->vpn_service);
1043         g_dbus_send_reply(connection, msg,
1044                                 DBUS_TYPE_OBJECT_PATH, &service_path,
1045                                                         DBUS_TYPE_INVALID);
1046         return 0;
1047
1048 failed:
1049         connman_service_unref(provider->vpn_service);
1050         provider->vpn_service = NULL;
1051
1052 unref:
1053         DBG("can not connect, delete provider");
1054
1055         g_hash_table_remove(provider_hash, provider->identifier);
1056
1057         return err;
1058 }
1059
1060 const char * __connman_provider_get_ident(struct connman_provider *provider)
1061 {
1062         if (provider == NULL)
1063                 return NULL;
1064
1065         return provider->identifier;
1066 }
1067
1068 int connman_provider_set_string(struct connman_provider *provider,
1069                                         const char *key, const char *value)
1070 {
1071         DBG("provider %p key %s value %s", provider, key, value);
1072
1073         if (g_str_equal(key, "Type") == TRUE) {
1074                 g_free(provider->type);
1075                 provider->type = g_strdup(value);
1076         } else if (g_str_equal(key, "Name") == TRUE) {
1077                 g_free(provider->name);
1078                 provider->name = g_strdup(value);
1079         } else if (g_str_equal(key, "Host") == TRUE) {
1080                 g_free(provider->host);
1081                 provider->host = g_strdup(value);
1082         } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1083                 g_free(provider->domain);
1084                 provider->domain = g_strdup(value);
1085         } else
1086                 g_hash_table_replace(provider->setting_strings,
1087                                 g_strdup(key), g_strdup(value));
1088         return 0;
1089 }
1090
1091 const char *connman_provider_get_string(struct connman_provider *provider,
1092                                                         const char *key)
1093 {
1094         DBG("provider %p key %s", provider, key);
1095
1096         if (g_str_equal(key, "Type") == TRUE)
1097                 return provider->type;
1098         else if (g_str_equal(key, "Name") == TRUE)
1099                 return provider->name;
1100         else if (g_str_equal(key, "Host") == TRUE)
1101                 return provider->host;
1102         else if (g_str_equal(key, "HostIP") == TRUE) {
1103                 if (provider->host_ip == NULL ||
1104                                 provider->host_ip[0] == NULL)
1105                         return provider->host;
1106                 else
1107                         return provider->host_ip[0];
1108         } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1109                 return provider->domain;
1110
1111         return g_hash_table_lookup(provider->setting_strings, key);
1112 }
1113
1114 connman_bool_t
1115 __connman_provider_check_routes(struct connman_provider *provider)
1116 {
1117         if (provider == NULL)
1118                 return FALSE;
1119
1120         if (provider->user_routes != NULL &&
1121                         g_hash_table_size(provider->user_routes) > 0)
1122                 return TRUE;
1123
1124         if (provider->routes != NULL &&
1125                         g_hash_table_size(provider->routes) > 0)
1126                 return TRUE;
1127
1128         return FALSE;
1129 }
1130
1131 void *connman_provider_get_data(struct connman_provider *provider)
1132 {
1133         return provider->driver_data;
1134 }
1135
1136 void connman_provider_set_data(struct connman_provider *provider, void *data)
1137 {
1138         provider->driver_data = data;
1139 }
1140
1141 void connman_provider_set_index(struct connman_provider *provider, int index)
1142 {
1143         struct connman_service *service = provider->vpn_service;
1144         struct connman_ipconfig *ipconfig;
1145
1146         DBG("");
1147
1148         if (service == NULL)
1149                 return;
1150
1151         ipconfig = __connman_service_get_ip4config(service);
1152
1153         if (ipconfig == NULL) {
1154                 connman_service_create_ip4config(service, index);
1155
1156                 ipconfig = __connman_service_get_ip4config(service);
1157                 if (ipconfig == NULL) {
1158                         DBG("Couldnt create ipconfig");
1159                         goto done;
1160                 }
1161         }
1162
1163         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
1164         __connman_ipconfig_set_index(ipconfig, index);
1165
1166
1167         ipconfig = __connman_service_get_ip6config(service);
1168
1169         if (ipconfig == NULL) {
1170                 connman_service_create_ip6config(service, index);
1171
1172                 ipconfig = __connman_service_get_ip6config(service);
1173                 if (ipconfig == NULL) {
1174                         DBG("Couldnt create ipconfig for IPv6");
1175                         goto done;
1176                 }
1177         }
1178
1179         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
1180         __connman_ipconfig_set_index(ipconfig, index);
1181
1182 done:
1183         provider->index = index;
1184 }
1185
1186 int connman_provider_get_index(struct connman_provider *provider)
1187 {
1188         return provider->index;
1189 }
1190
1191 int connman_provider_set_ipaddress(struct connman_provider *provider,
1192                                         struct connman_ipaddress *ipaddress)
1193 {
1194         struct connman_ipconfig *ipconfig = NULL;
1195
1196         ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
1197                                                         ipaddress->family);
1198         if (ipconfig == NULL)
1199                 return -EINVAL;
1200
1201         provider->family = ipaddress->family;
1202
1203         __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1204         __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1205         __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1206         __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1207         __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1208
1209         return 0;
1210 }
1211
1212 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
1213 {
1214         DBG("provider %p pac %s", provider, pac);
1215
1216         __connman_service_set_pac(provider->vpn_service, pac);
1217
1218         return 0;
1219 }
1220
1221
1222 int connman_provider_set_domain(struct connman_provider *provider,
1223                                         const char *domain)
1224 {
1225         DBG("provider %p domain %s", provider, domain);
1226
1227         g_free(provider->domain);
1228         provider->domain = g_strdup(domain);
1229
1230         __connman_service_set_domainname(provider->vpn_service, domain);
1231
1232         return 0;
1233 }
1234
1235 int connman_provider_set_nameservers(struct connman_provider *provider,
1236                                         const char *nameservers)
1237 {
1238         int i;
1239         char **nameservers_array = NULL;
1240
1241         DBG("provider %p nameservers %s", provider, nameservers);
1242
1243         __connman_service_nameserver_clear(provider->vpn_service);
1244
1245         if (nameservers == NULL)
1246                 return 0;
1247
1248         nameservers_array = g_strsplit(nameservers, " ", 0);
1249
1250         for (i = 0; nameservers_array[i] != NULL; i++) {
1251                 __connman_service_nameserver_append(provider->vpn_service,
1252                                                 nameservers_array[i], FALSE);
1253         }
1254
1255         g_strfreev(nameservers_array);
1256
1257         return 0;
1258 }
1259
1260 enum provider_route_type {
1261         PROVIDER_ROUTE_TYPE_NONE = 0,
1262         PROVIDER_ROUTE_TYPE_MASK = 1,
1263         PROVIDER_ROUTE_TYPE_ADDR = 2,
1264         PROVIDER_ROUTE_TYPE_GW   = 3,
1265 };
1266
1267 static int route_env_parse(struct connman_provider *provider, const char *key,
1268                                 int *family, unsigned long *idx,
1269                                 enum provider_route_type *type)
1270 {
1271         char *end;
1272         const char *start;
1273
1274         DBG("name %s", provider->name);
1275
1276         if (!strcmp(provider->type, "openvpn")) {
1277                 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1278                         start = key + strlen("route_network_");
1279                         *type = PROVIDER_ROUTE_TYPE_ADDR;
1280                 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1281                         start = key + strlen("route_netmask_");
1282                         *type = PROVIDER_ROUTE_TYPE_MASK;
1283                 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
1284                         start = key + strlen("route_gateway_");
1285                         *type = PROVIDER_ROUTE_TYPE_GW;
1286                 } else
1287                         return -EINVAL;
1288
1289                 *family = AF_INET;
1290                 *idx = g_ascii_strtoull(start, &end, 10);
1291
1292         } else if (!strcmp(provider->type, "openconnect")) {
1293                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
1294                         *family = AF_INET;
1295                         start = key + strlen("CISCO_SPLIT_INC_");
1296                 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
1297                         *family = AF_INET6;
1298                         start = key + strlen("CISCO_IPV6_SPLIT_INC_");
1299                 } else
1300                         return -EINVAL;
1301
1302                 *idx = g_ascii_strtoull(start, &end, 10);
1303
1304                 if (strncmp(end, "_ADDR", 5) == 0)
1305                         *type = PROVIDER_ROUTE_TYPE_ADDR;
1306                 else if (strncmp(end, "_MASK", 5) == 0)
1307                         *type = PROVIDER_ROUTE_TYPE_MASK;
1308                 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
1309                                 *family == AF_INET6) {
1310                         *type = PROVIDER_ROUTE_TYPE_MASK;
1311                 } else
1312                         return -EINVAL;
1313         }
1314
1315         return 0;
1316 }
1317
1318 int connman_provider_append_route(struct connman_provider *provider,
1319                                         const char *key, const char *value)
1320 {
1321         struct connman_route *route;
1322         int ret, family = 0;
1323         unsigned long idx = 0;
1324         enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
1325
1326         DBG("key %s value %s", key, value);
1327
1328         ret = route_env_parse(provider, key, &family, &idx, &type);
1329         if (ret < 0)
1330                 return ret;
1331
1332         DBG("idx %lu family %d type %d", idx, family, type);
1333
1334         route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
1335         if (route == NULL) {
1336                 route = g_try_new0(struct connman_route, 1);
1337                 if (route == NULL) {
1338                         connman_error("out of memory");
1339                         return -ENOMEM;
1340                 }
1341
1342                 route->family = family;
1343
1344                 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
1345                                                 route);
1346         }
1347
1348         switch (type) {
1349         case PROVIDER_ROUTE_TYPE_NONE:
1350                 break;
1351         case PROVIDER_ROUTE_TYPE_MASK:
1352                 route->netmask = g_strdup(value);
1353                 break;
1354         case PROVIDER_ROUTE_TYPE_ADDR:
1355                 route->host = g_strdup(value);
1356                 break;
1357         case PROVIDER_ROUTE_TYPE_GW:
1358                 route->gateway = g_strdup(value);
1359                 break;
1360         }
1361
1362         return 0;
1363 }
1364
1365 const char *connman_provider_get_driver_name(struct connman_provider *provider)
1366 {
1367         if (provider->driver == NULL)
1368                 return NULL;
1369
1370         return provider->driver->name;
1371 }
1372
1373 const char *connman_provider_get_save_group(struct connman_provider *provider)
1374 {
1375         return provider->identifier;
1376 }
1377
1378 static gint compare_priority(gconstpointer a, gconstpointer b)
1379 {
1380         return 0;
1381 }
1382
1383 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
1384 {
1385         struct connman_provider *provider = value;
1386
1387         if (provider->driver != NULL && provider->driver->remove)
1388                 provider->driver->remove(provider);
1389 }
1390
1391 int connman_provider_driver_register(struct connman_provider_driver *driver)
1392 {
1393         DBG("driver %p name %s", driver, driver->name);
1394
1395         driver_list = g_slist_insert_sorted(driver_list, driver,
1396                                                         compare_priority);
1397         provider_create_all_from_type(driver->name);
1398         return 0;
1399 }
1400
1401 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
1402 {
1403         DBG("driver %p name %s", driver, driver->name);
1404
1405         driver_list = g_slist_remove(driver_list, driver);
1406 }
1407
1408 static void provider_remove_all(gpointer key, gpointer value,
1409                                                 gpointer user_data)
1410 {
1411         struct connman_provider *provider = value;
1412
1413         __connman_provider_remove(provider->identifier);
1414 }
1415
1416 static void provider_offline_mode(connman_bool_t enabled)
1417 {
1418         DBG("enabled %d", enabled);
1419
1420         if (enabled == TRUE)
1421                 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
1422
1423 }
1424
1425 static struct connman_provider *provider_get(int index)
1426 {
1427         GHashTableIter iter;
1428         gpointer value, key;
1429
1430         g_hash_table_iter_init(&iter, provider_hash);
1431
1432         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1433                 struct connman_provider *provider = value;
1434
1435                 if (provider->index == index)
1436                         return provider;
1437         }
1438
1439         return NULL;
1440 }
1441
1442 static void provider_service_changed(struct connman_service *service,
1443                                 enum connman_service_state state)
1444 {
1445         struct connman_provider *provider;
1446         int vpn_index, service_index;
1447
1448         if (service == NULL)
1449                 return;
1450
1451         switch (state) {
1452         case CONNMAN_SERVICE_STATE_UNKNOWN:
1453         case CONNMAN_SERVICE_STATE_IDLE:
1454         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1455         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1456         case CONNMAN_SERVICE_STATE_READY:
1457         case CONNMAN_SERVICE_STATE_ONLINE:
1458                 return;
1459         case CONNMAN_SERVICE_STATE_DISCONNECT:
1460         case CONNMAN_SERVICE_STATE_FAILURE:
1461                 break;
1462         }
1463
1464         service_index = __connman_service_get_index(service);
1465
1466         vpn_index = __connman_connection_get_vpn_index(service_index);
1467
1468         DBG("service %p %s state %d index %d/%d", service,
1469                 __connman_service_get_ident(service),
1470                 state, service_index, vpn_index);
1471
1472         if (vpn_index < 0)
1473                 return;
1474
1475         provider = provider_get(vpn_index);
1476         if (provider == NULL)
1477                 return;
1478
1479         DBG("disconnect %p index %d", provider, vpn_index);
1480
1481         __connman_provider_disconnect(provider);
1482
1483         return;
1484 }
1485
1486 static struct connman_notifier provider_notifier = {
1487         .name                   = "provider",
1488         .offline_mode           = provider_offline_mode,
1489         .service_state_changed  = provider_service_changed,
1490 };
1491
1492 int __connman_provider_init(void)
1493 {
1494         int err;
1495
1496         DBG("");
1497
1498         connection = connman_dbus_get_connection();
1499
1500         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1501                                                 NULL, unregister_provider);
1502
1503         err = connman_notifier_register(&provider_notifier);
1504         if (err < 0) {
1505                 g_hash_table_destroy(provider_hash);
1506                 dbus_connection_unref(connection);
1507         }
1508
1509         return err;
1510 }
1511
1512 void __connman_provider_cleanup(void)
1513 {
1514         DBG("");
1515
1516         connman_notifier_unregister(&provider_notifier);
1517
1518         g_hash_table_foreach(provider_hash, clean_provider, NULL);
1519
1520         g_hash_table_destroy(provider_hash);
1521         provider_hash = NULL;
1522
1523         dbus_connection_unref(connection);
1524 }