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