dbus: Add connman_dbus_setting_changed_*()
[framework/connectivity/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 <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection = NULL;
34
35 static GHashTable *provider_hash = NULL;
36
37 static GSList *driver_list = NULL;
38
39 struct connman_route {
40         int family;
41         char *host;
42         char *netmask;
43         char *gateway;
44 };
45
46 struct connman_provider {
47         struct connman_element element;
48         struct connman_service *vpn_service;
49         char *identifier;
50         char *name;
51         char *type;
52         char *host;
53         char *domain;
54         int family;
55         GHashTable *routes;
56         struct connman_provider_driver *driver;
57         void *driver_data;
58 };
59
60 void __connman_provider_append_properties(struct connman_provider *provider,
61                                                         DBusMessageIter *iter)
62 {
63         if (provider->host != NULL)
64                 connman_dbus_dict_append_basic(iter, "Host",
65                                         DBUS_TYPE_STRING, &provider->host);
66
67         if (provider->domain != NULL)
68                 connman_dbus_dict_append_basic(iter, "Domain",
69                                         DBUS_TYPE_STRING, &provider->domain);
70
71         if (provider->type != NULL)
72                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
73                                                  &provider->type);
74 }
75
76 static struct connman_provider *connman_provider_lookup(const char *identifier)
77 {
78         struct connman_provider *provider = NULL;
79
80         provider = g_hash_table_lookup(provider_hash, identifier);
81
82         return provider;
83 }
84
85 struct connman_provider *connman_provider_ref(struct connman_provider *provider)
86 {
87         DBG("provider %p", provider);
88
89         if (connman_element_ref(&provider->element) == NULL)
90                 return NULL;
91
92         return provider;
93 }
94
95 void connman_provider_unref(struct connman_provider *provider)
96 {
97         DBG("provider %p", provider);
98
99         connman_element_unref(&provider->element);
100 }
101
102 static gboolean match_driver(struct connman_provider *provider,
103                                 struct connman_provider_driver *driver)
104 {
105         if (g_strcmp0(driver->name, provider->type) == 0)
106                 return TRUE;
107
108         return FALSE;
109 }
110
111 static int provider_probe(struct connman_provider *provider)
112 {
113         GSList *list;
114
115         DBG("provider %p name %s", provider, provider->name);
116
117         if (provider->driver != NULL)
118                 return -EALREADY;
119
120         for (list = driver_list; list; list = list->next) {
121                 struct connman_provider_driver *driver = list->data;
122
123                 if (match_driver(provider, driver) == FALSE)
124                         continue;
125
126                 DBG("driver %p name %s", driver, driver->name);
127
128                 if (driver->probe != NULL && driver->probe(provider) == 0) {
129                         provider->driver = driver;
130                         break;
131                 }
132         }
133
134         if (provider->driver == NULL)
135                 return -ENODEV;
136
137         return 0;
138 }
139
140 static int provider_indicate_state(struct connman_provider *provider,
141                                         enum connman_service_state state)
142 {
143         DBG("state %d", state);
144
145         __connman_service_indicate_state(provider->vpn_service, state,
146                                         CONNMAN_IPCONFIG_TYPE_IPV4);
147
148         return __connman_service_indicate_state(provider->vpn_service, state,
149                                         CONNMAN_IPCONFIG_TYPE_IPV6);
150 }
151
152 int __connman_provider_disconnect(struct connman_provider *provider)
153 {
154         int err;
155
156         DBG("provider %p", provider);
157
158         if (provider->driver != NULL && provider->driver->disconnect != NULL)
159                 err = provider->driver->disconnect(provider);
160         else
161                 return -EOPNOTSUPP;
162
163         if (provider->vpn_service != NULL)
164                 provider_indicate_state(provider,
165                                         CONNMAN_SERVICE_STATE_DISCONNECT);
166
167         if (err < 0) {
168                 if (err != -EINPROGRESS)
169                         return err;
170
171                 return -EINPROGRESS;
172         }
173
174         return 0;
175 }
176
177 int __connman_provider_connect(struct connman_provider *provider)
178 {
179         int err;
180
181         DBG("provider %p", provider);
182
183         if (provider->driver != NULL && provider->driver->connect != NULL)
184                 err = provider->driver->connect(provider);
185         else
186                 return -EOPNOTSUPP;
187
188         if (err < 0) {
189                 if (err != -EINPROGRESS)
190                         return err;
191
192                 provider_indicate_state(provider,
193                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
194
195                 return -EINPROGRESS;
196         }
197
198         return 0;
199 }
200
201 int __connman_provider_remove(const char *path)
202 {
203         struct connman_provider *provider;
204         GHashTableIter iter;
205         gpointer value, key;
206
207         DBG("path %s", path);
208
209         g_hash_table_iter_init(&iter, provider_hash);
210         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
211                 const char *srv_path;
212                 provider = value;
213
214                 if (provider->vpn_service == NULL)
215                         continue;
216
217                 srv_path = __connman_service_get_path(provider->vpn_service);
218
219                 if (g_strcmp0(srv_path, path) == 0) {
220                         DBG("Removing VPN %s", provider->identifier);
221                         g_hash_table_remove(provider_hash,
222                                                 provider->identifier);
223                         return 0;
224                 }
225         }
226
227         return -ENXIO;
228 }
229
230 static void provider_append_routes(gpointer key, gpointer value,
231                                         gpointer user_data)
232 {
233         struct connman_route *route = value;
234         struct connman_provider *provider = user_data;
235         int index = provider->element.index;
236
237         if (route->family == AF_INET6) {
238                 unsigned char prefix_len = atoi(route->netmask);
239
240                 connman_inet_add_ipv6_network_route(index, route->host,
241                                                         route->gateway,
242                                                         prefix_len);
243         } else {
244                 connman_inet_add_network_route(index, route->host,
245                                                 route->gateway,
246                                                 route->netmask);
247         }
248 }
249
250 static int set_connected(struct connman_provider *provider,
251                                         connman_bool_t connected)
252 {
253         struct connman_service *service = provider->vpn_service;
254         struct connman_ipconfig *ipconfig;
255
256         if (service == NULL)
257                 return -ENODEV;
258
259         ipconfig = __connman_service_get_ipconfig(service, provider->family);
260
261         if (connected == TRUE) {
262                 if (ipconfig == NULL) {
263                         provider_indicate_state(provider,
264                                                 CONNMAN_SERVICE_STATE_FAILURE);
265                         return -EIO;
266                 }
267
268                 __connman_ipconfig_address_add(ipconfig);
269                 __connman_ipconfig_gateway_add(ipconfig);
270
271                 provider_indicate_state(provider,
272                                         CONNMAN_SERVICE_STATE_READY);
273
274                 g_hash_table_foreach(provider->routes, provider_append_routes,
275                                         provider);
276
277         } else {
278                 if (ipconfig != NULL) {
279                         provider_indicate_state(provider,
280                                         CONNMAN_SERVICE_STATE_DISCONNECT);
281                         __connman_ipconfig_gateway_remove(ipconfig);
282                 }
283
284                 provider_indicate_state(provider,
285                                         CONNMAN_SERVICE_STATE_IDLE);
286         }
287
288         return 0;
289 }
290
291 int connman_provider_set_state(struct connman_provider *provider,
292                                         enum connman_provider_state state)
293 {
294         if (provider == NULL || provider->vpn_service == NULL)
295                 return -EINVAL;
296
297         switch (state) {
298         case CONNMAN_PROVIDER_STATE_UNKNOWN:
299                 return -EINVAL;
300         case CONNMAN_PROVIDER_STATE_IDLE:
301                 return set_connected(provider, FALSE);
302         case CONNMAN_PROVIDER_STATE_CONNECT:
303                 return provider_indicate_state(provider,
304                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
305         case CONNMAN_PROVIDER_STATE_READY:
306                 return set_connected(provider, TRUE);
307         case CONNMAN_PROVIDER_STATE_DISCONNECT:
308                 return provider_indicate_state(provider,
309                                         CONNMAN_SERVICE_STATE_DISCONNECT);
310         case CONNMAN_PROVIDER_STATE_FAILURE:
311                 return provider_indicate_state(provider,
312                                         CONNMAN_SERVICE_STATE_FAILURE);
313         }
314
315         return -EINVAL;
316 }
317
318 int connman_provider_indicate_error(struct connman_provider *provider,
319                                         enum connman_provider_error error)
320 {
321         enum connman_service_error service_error;
322
323         switch (error) {
324         case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
325                 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
326                 break;
327         case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
328                 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
329                 break;
330         case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
331                 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
332                 break;
333         default:
334                 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
335                 break;
336         }
337
338         return __connman_service_indicate_error(provider->vpn_service,
339                                                         service_error);
340 }
341
342 static void unregister_provider(gpointer data)
343 {
344         struct connman_provider *provider = data;
345         struct connman_service *service = provider->vpn_service;
346
347         DBG("provider %p", provider);
348
349         provider->vpn_service = NULL;
350         __connman_service_put(service);
351
352         connman_element_unregister(&provider->element);
353         connman_provider_unref(provider);
354 }
355
356 static void provider_destruct(struct connman_element *element)
357 {
358         struct connman_provider *provider = element->private;
359
360         DBG("provider %p", provider);
361
362         g_free(provider->name);
363         g_free(provider->type);
364         g_free(provider->domain);
365         g_free(provider->identifier);
366         g_hash_table_destroy(provider->routes);
367 }
368
369 static void destroy_route(gpointer user_data)
370 {
371         struct connman_route *route = user_data;
372
373         g_free(route->host);
374         g_free(route->netmask);
375         g_free(route->gateway);
376         g_free(route);
377 }
378
379 static void provider_initialize(struct connman_provider *provider)
380 {
381         DBG("provider %p", provider);
382
383         __connman_element_initialize(&provider->element);
384
385         provider->element.private = provider;
386         provider->element.destruct = provider_destruct;
387
388         provider->name = NULL;
389         provider->type = NULL;
390         provider->domain = NULL;
391         provider->identifier = NULL;
392         provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
393                                         NULL, destroy_route);
394 }
395
396 static struct connman_provider *connman_provider_new(void)
397 {
398         struct connman_provider *provider;
399
400         provider = g_try_new0(struct connman_provider, 1);
401         if (provider == NULL)
402                 return NULL;
403
404         DBG("provider %p", provider);
405         provider_initialize(provider);
406
407         return provider;
408 }
409
410 static struct connman_provider *connman_provider_get(const char *identifier)
411 {
412         struct connman_provider *provider;
413
414         provider = g_hash_table_lookup(provider_hash, identifier);
415         if (provider != NULL)
416                 return provider;
417
418         provider = connman_provider_new();
419         if (provider == NULL)
420                 return NULL;
421
422         DBG("provider %p", provider);
423
424         provider->identifier = g_strdup(identifier);
425
426         g_hash_table_insert(provider_hash, provider->identifier, provider);
427
428         provider->element.name = g_strdup(identifier);
429         connman_element_register(&provider->element, NULL);
430
431         return provider;
432 }
433
434 static void provider_dbus_ident(char *ident)
435 {
436         int i, len = strlen(ident);
437
438         for (i = 0; i < len; i++) {
439                 if (ident[i] >= '0' && ident[i] <= '9')
440                         continue;
441                 if (ident[i] >= 'a' && ident[i] <= 'z')
442                         continue;
443                 if (ident[i] >= 'A' && ident[i] <= 'Z')
444                         continue;
445                 ident[i] = '_';
446         }
447 }
448
449 int __connman_provider_create_and_connect(DBusMessage *msg)
450 {
451         struct connman_provider *provider;
452         DBusMessageIter iter, array;
453         const char *type = NULL, *name = NULL, *service_path = NULL;
454         const char *host = NULL, *domain = NULL;
455         char *ident;
456         gboolean created = FALSE;
457         int err;
458
459         dbus_message_iter_init(msg, &iter);
460         dbus_message_iter_recurse(&iter, &array);
461
462         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
463                 DBusMessageIter entry, value;
464                 const char *key;
465
466                 dbus_message_iter_recurse(&array, &entry);
467                 dbus_message_iter_get_basic(&entry, &key);
468
469                 dbus_message_iter_next(&entry);
470                 dbus_message_iter_recurse(&entry, &value);
471
472                 switch (dbus_message_iter_get_arg_type(&value)) {
473                 case DBUS_TYPE_STRING:
474                         if (g_str_equal(key, "Type") == TRUE)
475                                 dbus_message_iter_get_basic(&value, &type);
476                         else if (g_str_equal(key, "Name") == TRUE)
477                                 dbus_message_iter_get_basic(&value, &name);
478                         else if (g_str_equal(key, "Host") == TRUE)
479                                 dbus_message_iter_get_basic(&value, &host);
480                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
481                                 dbus_message_iter_get_basic(&value, &domain);
482                         break;
483                 }
484
485                 dbus_message_iter_next(&array);
486         }
487
488         if (host == NULL || domain == NULL) {
489                 err = -EINVAL;
490                 goto failed;
491         }
492
493         DBG("Type %s name %s", type, name);
494
495         if (type == NULL || name == NULL) {
496                 err = -EOPNOTSUPP;
497                 goto failed;
498         }
499
500         ident = g_strdup_printf("%s_%s", host, domain);
501         provider_dbus_ident(ident);
502
503         DBG("ident %s", ident);
504
505         provider = connman_provider_lookup(ident);
506
507         if (provider == NULL) {
508                 created = TRUE;
509                 provider = connman_provider_get(ident);
510                 if (provider) {
511                         provider->host = g_strdup(host);
512                         provider->domain = g_strdup(domain);
513                         provider->name = g_strdup(name);
514                         provider->type = g_strdup(type);
515                 }
516         }
517
518         if (provider == NULL) {
519                 DBG("can not create provider");
520                 err = -EOPNOTSUPP;
521                 goto failed;
522         }
523         dbus_message_iter_init(msg, &iter);
524         dbus_message_iter_recurse(&iter, &array);
525
526         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
527                 DBusMessageIter entry, value;
528                 const char *key, *str;
529
530                 dbus_message_iter_recurse(&array, &entry);
531                 dbus_message_iter_get_basic(&entry, &key);
532
533                 dbus_message_iter_next(&entry);
534                 dbus_message_iter_recurse(&entry, &value);
535
536                 switch (dbus_message_iter_get_arg_type(&value)) {
537                 case DBUS_TYPE_STRING:
538                         dbus_message_iter_get_basic(&value, &str);
539                         connman_provider_set_string(provider, key, str);
540                         break;
541                 }
542
543                 dbus_message_iter_next(&array);
544         }
545
546         g_free(ident);
547
548         if (provider == NULL) {
549                 err = -EOPNOTSUPP;
550                 goto failed;
551         }
552
553         if (created == TRUE)
554                 provider_probe(provider);
555
556         if (provider->vpn_service == NULL)
557                 provider->vpn_service =
558                         __connman_service_create_from_provider(provider);
559         if (provider->vpn_service == NULL) {
560                 err = -EOPNOTSUPP;
561                 goto failed;
562         }
563
564         err = __connman_service_connect(provider->vpn_service);
565         if (err < 0 && err != -EINPROGRESS)
566                 goto failed;
567
568         service_path = __connman_service_get_path(provider->vpn_service);
569         g_dbus_send_reply(connection, msg,
570                                 DBUS_TYPE_OBJECT_PATH, &service_path,
571                                                         DBUS_TYPE_INVALID);
572         return 0;
573
574 failed:
575         if (provider != NULL && created == TRUE) {
576                 DBG("can not connect delete provider");
577                 connman_provider_unref(provider);
578
579                 if (provider->vpn_service != NULL) {
580                         __connman_service_put(provider->vpn_service);
581                         provider->vpn_service = NULL;
582                 }
583         }
584
585         return err;
586 }
587
588 const char * __connman_provider_get_ident(struct connman_provider *provider)
589 {
590         if (provider == NULL)
591                 return NULL;
592
593         return provider->identifier;
594 }
595
596 int connman_provider_set_string(struct connman_provider *provider,
597                                         const char *key, const char *value)
598 {
599         DBG("provider %p key %s value %s", provider, key, value);
600
601         if (g_str_equal(key, "Type") == TRUE) {
602                 g_free(provider->type);
603                 provider->type = g_strdup(value);
604         } else if (g_str_equal(key, "Name") == TRUE) {
605                 g_free(provider->name);
606                 provider->name = g_strdup(value);
607         }
608
609         return connman_element_set_string(&provider->element, key, value);
610 }
611
612 const char *connman_provider_get_string(struct connman_provider *provider,
613                                                         const char *key)
614 {
615         DBG("provider %p key %s", provider, key);
616
617         if (g_str_equal(key, "Type") == TRUE)
618                 return provider->type;
619         else if (g_str_equal(key, "Name") == TRUE)
620                 return provider->name;
621
622         return connman_element_get_string(&provider->element, key);
623 }
624
625 void *connman_provider_get_data(struct connman_provider *provider)
626 {
627         return provider->driver_data;
628 }
629
630 void connman_provider_set_data(struct connman_provider *provider, void *data)
631 {
632         provider->driver_data = data;
633 }
634
635 void connman_provider_set_index(struct connman_provider *provider, int index)
636 {
637         struct connman_service *service = provider->vpn_service;
638         struct connman_ipconfig *ipconfig;
639
640         DBG("");
641
642         if (service == NULL)
643                 return;
644
645         ipconfig = __connman_service_get_ip4config(service);
646
647         if (ipconfig == NULL) {
648                 __connman_service_create_ip4config(service, index);
649
650                 ipconfig = __connman_service_get_ip4config(service);
651                 if (ipconfig == NULL) {
652                         DBG("Couldnt create ipconfig");
653                         goto done;
654                 }
655         }
656
657         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
658         __connman_ipconfig_set_index(ipconfig, index);
659
660
661         ipconfig = __connman_service_get_ip6config(service);
662
663         if (ipconfig == NULL) {
664                 __connman_service_create_ip6config(service, index);
665
666                 ipconfig = __connman_service_get_ip6config(service);
667                 if (ipconfig == NULL) {
668                         DBG("Couldnt create ipconfig for IPv6");
669                         goto done;
670                 }
671         }
672
673         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
674         __connman_ipconfig_set_index(ipconfig, index);
675
676 done:
677         provider->element.index = index;
678 }
679
680 int connman_provider_get_index(struct connman_provider *provider)
681 {
682         return provider->element.index;
683 }
684
685 int connman_provider_set_ipaddress(struct connman_provider *provider,
686                                         struct connman_ipaddress *ipaddress)
687 {
688         struct connman_ipconfig *ipconfig = NULL;
689
690         ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
691                                                         ipaddress->family);
692         if (ipconfig == NULL)
693                 return -EINVAL;
694
695         provider->family = ipaddress->family;
696
697         __connman_ipconfig_set_local(ipconfig, ipaddress->local);
698         __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
699         __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
700         __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
701         __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
702
703         return 0;
704 }
705
706 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
707 {
708         DBG("provider %p pac %s", provider, pac);
709
710         __connman_service_set_pac(provider->vpn_service, pac);
711
712         return 0;
713 }
714
715
716 int connman_provider_set_domain(struct connman_provider *provider,
717                                         const char *domain)
718 {
719         DBG("provider %p domain %s", provider, domain);
720
721         g_free(provider->domain);
722         provider->domain = g_strdup(domain);
723
724         __connman_service_set_domainname(provider->vpn_service, domain);
725
726         return 0;
727 }
728
729 int connman_provider_set_nameservers(struct connman_provider *provider,
730                                         const char *nameservers)
731 {
732         int i;
733         char **nameservers_array = NULL;
734
735         DBG("provider %p nameservers %s", provider, nameservers);
736
737         __connman_service_nameserver_clear(provider->vpn_service);
738
739         if (nameservers == NULL)
740                 return 0;
741
742         nameservers_array = g_strsplit(nameservers, " ", 0);
743
744         for (i = 0; nameservers_array[i] != NULL; i++) {
745                 __connman_service_nameserver_append(provider->vpn_service,
746                                                         nameservers_array[i]);
747         }
748
749         g_strfreev(nameservers_array);
750
751         return 0;
752 }
753
754 enum provider_route_type {
755         PROVIDER_ROUTE_TYPE_NONE = 0,
756         PROVIDER_ROUTE_TYPE_MASK = 1,
757         PROVIDER_ROUTE_TYPE_ADDR = 2,
758         PROVIDER_ROUTE_TYPE_GW   = 3,
759 };
760
761 static int route_env_parse(struct connman_provider *provider, const char *key,
762                                 int *family, unsigned long *idx,
763                                 enum provider_route_type *type)
764 {
765         char *end;
766         const char *start;
767
768         DBG("name %s", provider->name);
769
770         if (!strcmp(provider->type, "openvpn")) {
771                 if (g_str_has_prefix(key, "route_network_") == TRUE) {
772                         start = key + strlen("route_network_");
773                         *type = PROVIDER_ROUTE_TYPE_ADDR;
774                 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
775                         start = key + strlen("route_netmask_");
776                         *type = PROVIDER_ROUTE_TYPE_MASK;
777                 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
778                         start = key + strlen("route_gateway_");
779                         *type = PROVIDER_ROUTE_TYPE_GW;
780                 } else
781                         return -EINVAL;
782
783                 *family = AF_INET;
784                 *idx = g_ascii_strtoull(start, &end, 10);
785
786         } else if (!strcmp(provider->type, "openconnect")) {
787                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
788                         *family = AF_INET;
789                         start = key + strlen("CISCO_SPLIT_INC_");
790                 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
791                         *family = AF_INET6;
792                         start = key + strlen("CISCO_IPV6_SPLIT_INC_");
793                 } else
794                         return -EINVAL;
795
796                 *idx = g_ascii_strtoull(start, &end, 10);
797
798                 if (strncmp(end, "_ADDR", 5) == 0)
799                         *type = PROVIDER_ROUTE_TYPE_ADDR;
800                 else if (strncmp(end, "_MASK", 5) == 0)
801                         *type = PROVIDER_ROUTE_TYPE_MASK;
802                 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
803                                 *family == AF_INET6) {
804                         *type = PROVIDER_ROUTE_TYPE_MASK;
805                 } else
806                         return -EINVAL;
807         }
808
809         return 0;
810 }
811
812 int connman_provider_append_route(struct connman_provider *provider,
813                                         const char *key, const char *value)
814 {
815         struct connman_route *route;
816         int ret, family = 0;
817         unsigned long idx = 0;
818         enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
819
820         DBG("key %s value %s", key, value);
821
822         ret = route_env_parse(provider, key, &family, &idx, &type);
823         if (ret < 0)
824                 return ret;
825
826         DBG("idx %lu family %d type %d", idx, family, type);
827
828         route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
829         if (route == NULL) {
830                 route = g_try_new0(struct connman_route, 1);
831                 if (route == NULL) {
832                         connman_error("out of memory");
833                         return -ENOMEM;
834                 }
835
836                 route->family = family;
837
838                 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
839                                                 route);
840         }
841
842         switch (type) {
843         case PROVIDER_ROUTE_TYPE_NONE:
844                 break;
845         case PROVIDER_ROUTE_TYPE_MASK:
846                 route->netmask = g_strdup(value);
847                 break;
848         case PROVIDER_ROUTE_TYPE_ADDR:
849                 route->host = g_strdup(value);
850                 break;
851         case PROVIDER_ROUTE_TYPE_GW:
852                 route->gateway = g_strdup(value);
853                 break;
854         }
855
856         return 0;
857 }
858
859 const char *connman_provider_get_driver_name(struct connman_provider *provider)
860 {
861         return provider->driver->name;
862 }
863
864 static gint compare_priority(gconstpointer a, gconstpointer b)
865 {
866         return 0;
867 }
868
869 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
870 {
871         struct connman_provider *provider = value;
872
873         if (provider->driver != NULL && provider->driver->remove)
874                 provider->driver->remove(provider);
875 }
876
877 int connman_provider_driver_register(struct connman_provider_driver *driver)
878 {
879         DBG("driver %p name %s", driver, driver->name);
880
881         driver_list = g_slist_insert_sorted(driver_list, driver,
882                                                         compare_priority);
883         return 0;
884 }
885
886 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
887 {
888         DBG("driver %p name %s", driver, driver->name);
889
890         driver_list = g_slist_remove(driver_list, driver);
891 }
892
893 static void provider_remove(gpointer key, gpointer value,
894                                                 gpointer user_data)
895 {
896         struct connman_provider *provider = value;
897
898         g_hash_table_remove(provider_hash, provider->identifier);
899 }
900
901 static void provider_offline_mode(connman_bool_t enabled)
902 {
903         DBG("enabled %d", enabled);
904
905         if (enabled == TRUE)
906                 g_hash_table_foreach(provider_hash, provider_remove, NULL);
907
908 }
909
910 static struct connman_notifier provider_notifier = {
911         .name                   = "provider",
912         .offline_mode           = provider_offline_mode,
913 };
914
915 int __connman_provider_init(void)
916 {
917         int err;
918
919         DBG("");
920
921         connection = connman_dbus_get_connection();
922
923         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
924                                                 NULL, unregister_provider);
925
926         err = connman_notifier_register(&provider_notifier);
927         if (err < 0) {
928                 g_hash_table_destroy(provider_hash);
929                 dbus_connection_unref(connection);
930         }
931
932         return err;
933 }
934
935 void __connman_provider_cleanup(void)
936 {
937         DBG("");
938
939         connman_notifier_unregister(&provider_notifier);
940
941         g_hash_table_foreach(provider_hash, clean_provider, NULL);
942
943         g_hash_table_destroy(provider_hash);
944         provider_hash = NULL;
945
946         dbus_connection_unref(connection);
947 }