eed975910fecb4d70c2c9b7365cb382f24371ac9
[framework/connectivity/connman.git] / src / network.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 <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static unsigned int hidden_counter = 0;
34
35 struct connman_network {
36         struct connman_element element;
37         enum connman_network_type type;
38         enum connman_network_protocol protocol;
39         connman_bool_t available;
40         connman_bool_t connected;
41         connman_bool_t roaming;
42         connman_bool_t hidden;
43         connman_uint8_t strength;
44         connman_uint16_t frequency;
45         char *identifier;
46         char *address;
47         char *name;
48         char *node;
49         char *group;
50         struct connman_ipconfig *ipconfig;
51
52         struct connman_network_driver *driver;
53         void *driver_data;
54
55         connman_bool_t registered;
56         connman_bool_t connecting;
57         connman_bool_t associating;
58
59         struct connman_device *device;
60
61         struct {
62                 void *ssid;
63                 int ssid_len;
64                 char *mode;
65                 unsigned short channel;
66                 char *security;
67                 char *passphrase;
68                 char *eap;
69                 char *identity;
70                 char *ca_cert_path;
71                 char *client_cert_path;
72                 char *private_key_path;
73                 char *private_key_passphrase;
74                 char *phase2_auth;
75         } wifi;
76 };
77
78 static const char *type2string(enum connman_network_type type)
79 {
80         switch (type) {
81         case CONNMAN_NETWORK_TYPE_UNKNOWN:
82         case CONNMAN_NETWORK_TYPE_VENDOR:
83                 break;
84         case CONNMAN_NETWORK_TYPE_ETHERNET:
85                 return "ethernet";
86         case CONNMAN_NETWORK_TYPE_WIFI:
87                 return "wifi";
88         case CONNMAN_NETWORK_TYPE_WIMAX:
89                 return "wimax";
90         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
91         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
92                 return "bluetooth";
93         case CONNMAN_NETWORK_TYPE_CELLULAR:
94                 return "cellular";
95         }
96
97         return NULL;
98 }
99
100 static DBusMessage *get_properties(DBusConnection *conn,
101                                         DBusMessage *msg, void *data)
102 {
103         struct connman_network *network = data;
104         DBusMessage *reply;
105         DBusMessageIter array, dict;
106
107         DBG("conn %p", conn);
108
109         if (__connman_security_check_privilege(msg,
110                                         CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
111                 return __connman_error_permission_denied(msg);
112
113         reply = dbus_message_new_method_return(msg);
114         if (reply == NULL)
115                 return NULL;
116
117         dbus_message_iter_init_append(reply, &array);
118
119         connman_dbus_dict_open(&array, &dict);
120
121         if (network->device) {
122                 const char *path = connman_device_get_path(network->device);
123                 if (path != NULL)
124                         connman_dbus_dict_append_basic(&dict, "Device",
125                                                 DBUS_TYPE_OBJECT_PATH, &path);
126         }
127
128         if (network->address != NULL)
129                 connman_dbus_dict_append_basic(&dict, "Address",
130                                         DBUS_TYPE_STRING, &network->address);
131
132         if (network->name != NULL)
133                 connman_dbus_dict_append_basic(&dict, "Name",
134                                         DBUS_TYPE_STRING, &network->name);
135
136         connman_dbus_dict_append_basic(&dict, "Connected",
137                                 DBUS_TYPE_BOOLEAN, &network->connected);
138
139         if (network->strength > 0)
140                 connman_dbus_dict_append_basic(&dict, "Strength",
141                                         DBUS_TYPE_BYTE, &network->strength);
142
143         if (network->frequency > 0)
144                 connman_dbus_dict_append_basic(&dict, "Frequency",
145                                         DBUS_TYPE_UINT16, &network->frequency);
146
147         if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
148                 connman_dbus_dict_append_fixed_array(&dict, "WiFi.SSID",
149                                 DBUS_TYPE_BYTE, &network->wifi.ssid,
150                                                 network->wifi.ssid_len);
151
152         if (network->wifi.mode != NULL)
153                 connman_dbus_dict_append_basic(&dict, "WiFi.Mode",
154                                 DBUS_TYPE_STRING, &network->wifi.mode);
155
156         if (network->wifi.channel > 0)
157                 connman_dbus_dict_append_basic(&dict, "WiFi.Channel",
158                                 DBUS_TYPE_UINT16, &network->wifi.channel);
159
160         if (network->wifi.security != NULL) {
161                 connman_dbus_dict_append_basic(&dict, "WiFi.Security",
162                                 DBUS_TYPE_STRING, &network->wifi.security);
163
164                 if (g_strcmp0(network->wifi.security, "ieee8021x") == 0 &&
165                                                     network->wifi.eap != NULL)
166                         connman_dbus_dict_append_basic(&dict, "WiFi.EAP",
167                                         DBUS_TYPE_STRING, &network->wifi.eap);
168         }
169
170
171         if (network->wifi.passphrase != NULL &&
172                         __connman_security_check_privilege(msg,
173                                 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
174                 connman_dbus_dict_append_basic(&dict, "WiFi.Passphrase",
175                                 DBUS_TYPE_STRING, &network->wifi.passphrase);
176
177         connman_dbus_dict_close(&array, &dict);
178
179         return reply;
180 }
181
182 static GDBusMethodTable network_methods[] = {
183         { "GetProperties", "",   "a{sv}", get_properties },
184         { },
185 };
186
187 static GDBusSignalTable network_signals[] = {
188         { "PropertyChanged", "sv" },
189         { },
190 };
191
192 static DBusConnection *connection;
193
194 static void append_networks(DBusMessageIter *iter, void *user_data)
195 {
196         struct connman_device *device = user_data;
197
198         __connman_element_list((struct connman_element *) device,
199                                         CONNMAN_ELEMENT_TYPE_NETWORK, iter);
200 }
201
202 static void emit_networks_signal(struct connman_device *device)
203 {
204         const char *path = connman_device_get_path(device);
205
206         connman_dbus_property_changed_array(path,
207                         CONNMAN_DEVICE_INTERFACE, "Networks",
208                         DBUS_TYPE_OBJECT_PATH, append_networks, device);
209 }
210
211 static int register_interface(struct connman_element *element)
212 {
213         struct connman_network *network = element->network;
214
215         DBG("element %p name %s", element, element->name);
216
217         if (g_dbus_register_interface(connection, element->path,
218                                         CONNMAN_NETWORK_INTERFACE,
219                                         network_methods, network_signals,
220                                         NULL, network, NULL) == FALSE) {
221                 connman_error("Failed to register %s network", element->path);
222                 return -EIO;
223         }
224
225         network->registered = TRUE;
226
227         emit_networks_signal(network->device);
228
229         return 0;
230 }
231
232 static void unregister_interface(struct connman_element *element)
233 {
234         struct connman_network * network = element->network;
235
236         DBG("element %p name %s", element, element->name);
237
238         network->registered = FALSE;
239
240         if (network->device != NULL)
241                 emit_networks_signal(network->device);
242
243         g_dbus_unregister_interface(connection, element->path,
244                                                 CONNMAN_NETWORK_INTERFACE);
245 }
246
247 connman_bool_t __connman_network_has_driver(struct connman_network *network)
248 {
249         if (network == NULL || network->driver == NULL)
250                 return FALSE;
251
252         return network->registered;
253 }
254
255 static GSList *driver_list = NULL;
256
257 static gint compare_priority(gconstpointer a, gconstpointer b)
258 {
259         const struct connman_network_driver *driver1 = a;
260         const struct connman_network_driver *driver2 = b;
261
262         return driver2->priority - driver1->priority;
263 }
264
265 /**
266  * connman_network_driver_register:
267  * @driver: network driver definition
268  *
269  * Register a new network driver
270  *
271  * Returns: %0 on success
272  */
273 int connman_network_driver_register(struct connman_network_driver *driver)
274 {
275         DBG("driver %p name %s", driver, driver->name);
276
277         driver_list = g_slist_insert_sorted(driver_list, driver,
278                                                         compare_priority);
279
280         return 0;
281 }
282
283 /**
284  * connman_network_driver_unregister:
285  * @driver: network driver definition
286  *
287  * Remove a previously registered network driver
288  */
289 void connman_network_driver_unregister(struct connman_network_driver *driver)
290 {
291         DBG("driver %p name %s", driver, driver->name);
292
293         driver_list = g_slist_remove(driver_list, driver);
294 }
295
296 static void network_destruct(struct connman_element *element)
297 {
298         struct connman_network *network = element->network;
299
300         DBG("element %p name %s", element, element->name);
301
302         g_free(network->wifi.ssid);
303         g_free(network->wifi.mode);
304         g_free(network->wifi.security);
305         g_free(network->wifi.passphrase);
306
307         g_free(network->group);
308         g_free(network->node);
309         g_free(network->name);
310         g_free(network->address);
311         g_free(network->identifier);
312
313         if (network->ipconfig) {
314                 connman_ipconfig_unref(network->ipconfig);
315                 network->ipconfig = NULL;
316         }
317
318         network->device = NULL;
319 }
320
321 /**
322  * connman_network_create:
323  * @identifier: network identifier (for example an unqiue name)
324  *
325  * Allocate a new network and assign the #identifier to it.
326  *
327  * Returns: a newly-allocated #connman_network structure
328  */
329 struct connman_network *connman_network_create(const char *identifier,
330                                                 enum connman_network_type type)
331 {
332         struct connman_network *network;
333         connman_uint8_t strength = 0;
334         const char *str;
335         char *temp;
336
337         DBG("identifier %s type %d", identifier, type);
338
339         network = g_try_new0(struct connman_network, 1);
340         if (network == NULL)
341                 return NULL;
342
343         DBG("network %p", network);
344
345         __connman_element_initialize(&network->element);
346
347         //temp = connman_dbus_encode_string(identifier);
348         if (identifier == NULL) {
349                 temp = g_strdup_printf("hidden_%d", hidden_counter++);
350                 network->hidden = TRUE;
351         } else
352                 temp = g_strdup(identifier);
353
354         if (temp == NULL) {
355                 g_free(network);
356                 return NULL;
357         }
358
359         network->element.name = temp;
360         network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
361
362         network->element.network = network;
363         network->element.destruct = network_destruct;
364
365         str = type2string(type);
366         if (str != NULL)
367                 connman_element_set_string(&network->element, "Type", str);
368
369         connman_element_set_uint8(&network->element, "Strength", strength);
370
371         network->type       = type;
372         network->identifier = g_strdup(temp);
373
374         return network;
375 }
376
377 /**
378  * connman_network_ref:
379  * @network: network structure
380  *
381  * Increase reference counter of  network
382  */
383 struct connman_network *connman_network_ref(struct connman_network *network)
384 {
385         if (connman_element_ref(&network->element) == NULL)
386                 return NULL;
387
388         return network;
389 }
390
391 /**
392  * connman_network_unref:
393  * @network: network structure
394  *
395  * Decrease reference counter of network
396  */
397 void connman_network_unref(struct connman_network *network)
398 {
399         connman_element_unref(&network->element);
400 }
401
402 const char *__connman_network_get_type(struct connman_network *network)
403 {
404         return type2string(network->type);
405 }
406
407 /**
408  * connman_network_get_type:
409  * @network: network structure
410  *
411  * Get type of network
412  */
413 enum connman_network_type connman_network_get_type(struct connman_network *network)
414 {
415         return network->type;
416 }
417
418 /**
419  * connman_network_get_identifier:
420  * @network: network structure
421  *
422  * Get identifier of network
423  */
424 const char *connman_network_get_identifier(struct connman_network *network)
425 {
426         return network->identifier;
427 }
428
429 /**
430  * connman_network_get_path:
431  * @network: network structure
432  *
433  * Get path name of network
434  */
435 const char *connman_network_get_path(struct connman_network *network)
436 {
437         return network->element.path;
438 }
439
440 /**
441  * connman_network_set_index:
442  * @network: network structure
443  * @index: index number
444  *
445  * Set index number of network
446  */
447 void connman_network_set_index(struct connman_network *network, int index)
448 {
449         struct connman_service *service;
450
451         service = __connman_service_lookup_from_network(network);
452         if (service == NULL)
453                 goto done;
454
455         if (network->element.index < 0)
456                 /*
457                  * This is needed for plugins that havent set their ipconfig
458                  * layer yet, due to not being able to get a network index
459                  * prior to creating a service.
460                  */
461                 __connman_service_create_ipconfig(service, index);
462         else {
463                 struct connman_ipconfig *ipconfig;
464
465                 /* If index changed, the index of ipconfig must be reset. */
466                 ipconfig = __connman_service_get_ipconfig(service);
467                 if (ipconfig == NULL)
468                         goto done;
469
470                 __connman_ipconfig_set_index(ipconfig, index);
471         }
472
473 done:
474         network->element.index = index;
475 }
476
477 /**
478  * connman_network_get_index:
479  * @network: network structure
480  *
481  * Get index number of network
482  */
483 int connman_network_get_index(struct connman_network *network)
484 {
485         return network->element.index;
486 }
487
488 /**
489  * connman_network_get_element:
490  * @network: network structure
491  *
492  * Get connman_element of network
493  */
494 struct connman_element *connman_network_get_element(
495                                 struct connman_network *network)
496 {
497         return &network->element;
498 }
499
500 /**
501  * connman_network_set_protocol:
502  * @network: network structure
503  * @protocol: network protocol
504  *
505  * Change protocol of network
506  */
507 void connman_network_set_protocol(struct connman_network *network,
508                                         enum connman_network_protocol protocol)
509 {
510         network->protocol = protocol;
511 }
512
513 /**
514  * connman_network_set_group:
515  * @network: network structure
516  * @group: group name
517  *
518  * Set group name for automatic clustering
519  */
520 void connman_network_set_group(struct connman_network *network,
521                                                         const char *group)
522 {
523         switch (network->type) {
524         case CONNMAN_NETWORK_TYPE_UNKNOWN:
525         case CONNMAN_NETWORK_TYPE_VENDOR:
526                 return;
527         case CONNMAN_NETWORK_TYPE_ETHERNET:
528         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
529         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
530         case CONNMAN_NETWORK_TYPE_CELLULAR:
531         case CONNMAN_NETWORK_TYPE_WIFI:
532         case CONNMAN_NETWORK_TYPE_WIMAX:
533                 break;
534         }
535
536         if (g_strcmp0(network->group, group) == 0) {
537                 if (group != NULL)
538                         __connman_profile_update_network(network);
539                 return;
540         }
541
542         if (network->group != NULL) {
543                 __connman_profile_remove_network(network);
544
545                 g_free(network->group);
546         }
547
548         network->group = g_strdup(group);
549
550         if (network->group != NULL)
551                 __connman_profile_add_network(network);
552 }
553
554 /**
555  * connman_network_get_group:
556  * @network: network structure
557  *
558  * Get group name for automatic clustering
559  */
560 const char *connman_network_get_group(struct connman_network *network)
561 {
562         return network->group;
563 }
564
565 const char *__connman_network_get_ident(struct connman_network *network)
566 {
567         if (network->device == NULL)
568                 return NULL;
569
570         return __connman_device_get_ident(network->device);
571 }
572
573 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
574 {
575         switch (network->type) {
576         case CONNMAN_NETWORK_TYPE_UNKNOWN:
577         case CONNMAN_NETWORK_TYPE_VENDOR:
578         case CONNMAN_NETWORK_TYPE_ETHERNET:
579         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
580         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
581         case CONNMAN_NETWORK_TYPE_CELLULAR:
582         case CONNMAN_NETWORK_TYPE_WIMAX:
583                 break;
584         case CONNMAN_NETWORK_TYPE_WIFI:
585                 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
586                         return TRUE;
587                 if (network->strength > 0 && network->strength < 20)
588                         return TRUE;
589                 break;
590         }
591
592         return FALSE;
593 }
594
595 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
596 {
597         return network->connecting;
598 }
599
600 /**
601  * connman_network_set_available:
602  * @network: network structure
603  * @available: availability state
604  *
605  * Change availability state of network (in range)
606  */
607 int connman_network_set_available(struct connman_network *network,
608                                                 connman_bool_t available)
609 {
610         DBG("network %p available %d", network, available);
611
612         if (network->available == available)
613                 return -EALREADY;
614
615         network->available = available;
616
617         return 0;
618 }
619
620 /**
621  * connman_network_get_available:
622  * @network: network structure
623  *
624  * Get network available setting
625  */
626 connman_bool_t connman_network_get_available(struct connman_network *network)
627 {
628         if (network->hidden == TRUE)
629                 return TRUE;
630
631         return network->available;
632 }
633
634 /**
635  * connman_network_set_associating:
636  * @network: network structure
637  * @associating: associating state
638  *
639  * Change associating state of network
640  */
641 int connman_network_set_associating(struct connman_network *network,
642                                                 connman_bool_t associating)
643 {
644         DBG("network %p associating %d", network, associating);
645
646         if (network->associating == associating)
647                 return -EALREADY;
648
649         network->associating = associating;
650
651         if (associating == TRUE) {
652                 struct connman_service *service;
653
654                 service = __connman_service_lookup_from_network(network);
655                 __connman_service_indicate_state(service,
656                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
657         }
658
659         return 0;
660 }
661
662 static void set_associate_error(struct connman_network *network)
663 {
664         struct connman_service *service;
665
666         if (network->associating == FALSE)
667                 return ;
668
669         network->associating = FALSE;
670
671         service = __connman_service_lookup_from_network(network);
672
673         __connman_service_indicate_state(service,
674                                         CONNMAN_SERVICE_STATE_FAILURE);
675 }
676
677 static void set_configure_error(struct connman_network *network)
678 {
679         struct connman_service *service;
680
681         network->connecting = FALSE;
682
683         service = __connman_service_lookup_from_network(network);
684
685         __connman_service_indicate_state(service,
686                                         CONNMAN_SERVICE_STATE_FAILURE);
687 }
688
689 void connman_network_set_method(struct connman_network *network,
690                                         enum connman_ipconfig_method method)
691 {
692         struct connman_service *service;
693         struct connman_ipconfig *ipconfig;
694
695         network->element.ipv4.method = method;
696
697         service = __connman_service_lookup_from_network(network);
698         if (service == NULL)
699                 return;
700
701         ipconfig = __connman_service_get_ipconfig(service);
702         if (ipconfig == NULL)
703                 return;
704
705         connman_ipconfig_set_method(ipconfig, method);
706 }
707
708 void connman_network_set_error(struct connman_network *network,
709                                         enum connman_network_error error)
710 {
711         DBG("nework %p, error %d", network, error);
712
713         network->connecting = FALSE;
714
715         switch (error) {
716         case CONNMAN_NETWORK_ERROR_UNKNOWN:
717                 return;
718         case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
719                 set_associate_error(network);
720                 break;
721         case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
722                 set_configure_error(network);
723                 break;
724         }
725 }
726
727 static void set_configuration(struct connman_network *network)
728 {
729         struct connman_service *service;
730
731         DBG("network %p", network);
732
733         __connman_device_increase_connections(network->device);
734
735         __connman_device_set_network(network->device, network);
736
737         connman_device_set_disconnected(network->device, FALSE);
738
739         service = __connman_service_lookup_from_network(network);
740         __connman_service_indicate_state(service,
741                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
742 }
743
744 static int set_connected_fixed(struct connman_network *network)
745 {
746         struct connman_service *service;
747         struct connman_element *parent, *element;
748
749         DBG("");
750
751         service = __connman_service_lookup_from_network(network);
752
753         parent = connman_network_get_element(network);
754
755         set_configuration(network);
756
757         if (parent->ipv4.address == NULL)
758                 return -EINVAL;
759
760         if (parent->ipv4.netmask == NULL)
761                 return -EINVAL;
762
763         element = connman_element_create(NULL);
764         if (element == NULL) {
765                 connman_error("Can not create connman_element");
766                 return -ENOMEM;
767         }
768
769         element->type = CONNMAN_ELEMENT_TYPE_IPV4;
770         element->index = parent->index;
771
772         if (connman_element_register(element, parent) < 0) {
773                 connman_error("Can not register connman_element");
774                 return -EINVAL;
775         }
776
777         network->connecting = FALSE;
778
779         connman_network_set_associating(network, FALSE);
780
781         return 0;
782 }
783
784 static void set_connected_manual(struct connman_network *network)
785 {
786         struct connman_service *service;
787         struct connman_ipconfig *ipconfig;
788         const char *nameserver = NULL;
789         int err;
790
791         DBG("network %p", network);
792
793         service = __connman_service_lookup_from_network(network);
794
795         ipconfig = __connman_service_get_ipconfig(service);
796
797         set_configuration(network);
798
799         err = __connman_ipconfig_set_address(ipconfig);
800         if (err < 0) {
801                 connman_network_set_error(network,
802                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
803                 return;
804         }
805
806         connman_element_get_value(&network->element,
807                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
808         if (nameserver != NULL)
809                 __connman_service_append_nameserver(service, nameserver);
810
811         __connman_ipconfig_set_gateway(ipconfig, &network->element);
812
813         network->connecting = FALSE;
814
815         connman_network_set_associating(network, FALSE);
816
817         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
818 }
819
820 static int set_connected_dhcp(struct connman_network *network)
821 {
822         struct connman_element *element;
823         int error;
824
825         DBG("network %p", network);
826
827         if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
828                 return -EINVAL;
829
830         element = connman_element_create(NULL);
831         if (element == NULL)
832                 return -ENOMEM;
833
834         element->type  = CONNMAN_ELEMENT_TYPE_DHCP;
835         element->index = network->element.index;
836
837         error = connman_element_register(element, &network->element);
838         if (error < 0) {
839                 connman_element_unref(element);
840                 return error;
841         }
842
843         set_configuration(network);
844
845         return 0;
846 }
847
848 static gboolean set_connected(gpointer user_data)
849 {
850         struct connman_network *network = user_data;
851         struct connman_service *service;
852         struct connman_ipconfig *ipconfig;
853         enum connman_ipconfig_method method;
854
855         service = __connman_service_lookup_from_network(network);
856
857         ipconfig = __connman_service_get_ipconfig(service);
858
859         method = __connman_ipconfig_get_method(ipconfig);
860
861         DBG("method %d", method);
862
863         if (network->connected == TRUE) {
864                 switch (method) {
865                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
866                 case CONNMAN_IPCONFIG_METHOD_OFF:
867                         return FALSE;
868                 case CONNMAN_IPCONFIG_METHOD_FIXED:
869                         if (set_connected_fixed(network) < 0) {
870                                 connman_network_set_error(network,
871                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
872                                 return FALSE;
873                         }
874                         return TRUE;
875                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
876                         set_connected_manual(network);
877                         return TRUE;
878                 case CONNMAN_IPCONFIG_METHOD_DHCP:
879                         if (set_connected_dhcp(network) < 0) {
880                                 connman_network_set_error(network,
881                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
882                                 return FALSE;
883                         }
884                 }
885
886         } else {
887                 struct connman_service *service;
888
889                 connman_element_unregister_children(&network->element);
890
891                 __connman_device_set_network(network->device, NULL);
892                 network->hidden = FALSE;
893
894                 __connman_device_decrease_connections(network->device);
895
896                 service = __connman_service_lookup_from_network(network);
897
898                 __connman_service_indicate_state(service,
899                                                 CONNMAN_SERVICE_STATE_IDLE);
900         }
901
902         network->connecting = FALSE;
903
904         connman_network_set_associating(network, FALSE);
905
906         return FALSE;
907 }
908
909 /**
910  * connman_network_set_connected:
911  * @network: network structure
912  * @connected: connected state
913  *
914  * Change connected state of network
915  */
916 int connman_network_set_connected(struct connman_network *network,
917                                                 connman_bool_t connected)
918 {
919         DBG("network %p connected %d", network, connected);
920
921         if ((network->connecting == TRUE || network->associating == TRUE) &&
922                                                         connected == FALSE) {
923                 connman_element_set_error(&network->element,
924                                         CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
925                 __connman_network_disconnect(network);
926         }
927
928         if (network->connected == connected)
929                 return -EALREADY;
930
931         network->connected = connected;
932
933         if (network->registered == FALSE) {
934                 g_idle_add(set_connected, network);
935                 return 0;
936         }
937
938         connman_dbus_property_changed_basic(network->element.path,
939                                 CONNMAN_NETWORK_INTERFACE, "Connected",
940                                                 DBUS_TYPE_BOOLEAN, &connected);
941
942         set_connected(network);
943
944         return 0;
945 }
946
947 /**
948  * connman_network_get_connected:
949  * @network: network structure
950  *
951  * Get network connection status
952  */
953 connman_bool_t connman_network_get_connected(struct connman_network *network)
954 {
955         return network->connected;
956 }
957
958 /**
959  * connman_network_get_associating:
960  * @network: network structure
961  *
962  * Get network associating status
963  */
964 connman_bool_t connman_network_get_associating(struct connman_network *network)
965 {
966         return network->associating;
967 }
968
969 /**
970  * __connman_network_connect:
971  * @network: network structure
972  *
973  * Connect network
974  */
975 int __connman_network_connect(struct connman_network *network)
976 {
977         int err;
978
979         DBG("network %p", network);
980
981         if (network->connected == TRUE)
982                 return -EISCONN;
983
984         if (network->connecting == TRUE || network->associating == TRUE)
985                 return -EALREADY;
986
987         if (network->driver == NULL)
988                 return -EUNATCH;
989
990         if (network->driver->connect == NULL)
991                 return -ENOSYS;
992
993         __connman_device_disconnect(network->device);
994
995         network->connecting = TRUE;
996
997         err = network->driver->connect(network);
998         if (err < 0) {
999                 if (err == -EINPROGRESS)
1000                         connman_network_set_associating(network, TRUE);
1001                 else {
1002                         network->connecting = FALSE;
1003                         network->hidden = FALSE;
1004                 }
1005
1006                 return err;
1007         }
1008
1009         network->connected = TRUE;
1010         set_connected(network);
1011
1012         return err;
1013 }
1014
1015 /**
1016  * __connman_network_disconnect:
1017  * @network: network structure
1018  *
1019  * Disconnect network
1020  */
1021 int __connman_network_disconnect(struct connman_network *network)
1022 {
1023         int err;
1024
1025         DBG("network %p", network);
1026
1027         if (network->connected == FALSE && network->connecting == FALSE &&
1028                                                 network->associating == FALSE)
1029                 return -ENOTCONN;
1030
1031         if (network->driver == NULL)
1032                 return -EUNATCH;
1033
1034         if (network->driver->disconnect == NULL)
1035                 return -ENOSYS;
1036
1037         network->connecting = FALSE;
1038
1039         err = network->driver->disconnect(network);
1040         if (err == 0) {
1041                 connman_network_set_connected(network, FALSE);
1042                 set_connected(network);
1043         }
1044
1045         return err;
1046 }
1047
1048 static int dhcp_start(struct connman_network *network)
1049 {
1050         struct connman_element *element;
1051         int error;
1052
1053         if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1054                 return -EINVAL;
1055
1056         element = connman_element_create(NULL);
1057         if (element == NULL)
1058                 return -ENOMEM;
1059
1060         element->type  = CONNMAN_ELEMENT_TYPE_DHCP;
1061         element->index = network->element.index;
1062
1063         error = connman_element_register(element, &network->element);
1064         if (error < 0) {
1065                 connman_element_unref(element);
1066                 return error;
1067         }
1068
1069         return 0;
1070 }
1071
1072 static int dhcp_stop(struct connman_network *network)
1073 {
1074         if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1075                 return -EINVAL;
1076
1077         connman_element_unregister_children_type(&network->element,
1078                                         CONNMAN_ELEMENT_TYPE_CONNECTION);
1079         connman_element_unregister_children_type(&network->element,
1080                                                 CONNMAN_ELEMENT_TYPE_IPV4);
1081         connman_element_unregister_children_type(&network->element,
1082                                                 CONNMAN_ELEMENT_TYPE_DHCP);
1083
1084         return 0;
1085 }
1086
1087 static int manual_ipv4_set(struct connman_network *network,
1088                                 struct connman_ipconfig *ipconfig)
1089 {
1090         struct connman_service *service;
1091         int err;
1092
1093         service = __connman_service_lookup_from_network(network);
1094         if (service == NULL)
1095                 return -EINVAL;
1096
1097         err = __connman_ipconfig_set_address(ipconfig);
1098         if (err < 0) {
1099                 connman_network_set_error(network,
1100                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1101                 return err;
1102         }
1103
1104         __connman_ipconfig_set_gateway(ipconfig, &network->element);
1105
1106         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
1107
1108         return 0;
1109 }
1110
1111 int __connman_network_clear_ipconfig(struct connman_network *network,
1112                                         struct connman_ipconfig *ipconfig)
1113 {
1114         struct connman_service *service;
1115         enum connman_ipconfig_method method;
1116
1117         service = __connman_service_lookup_from_network(network);
1118         if (service == NULL)
1119                 return -EINVAL;
1120
1121         method = __connman_ipconfig_get_method(ipconfig);
1122
1123         switch (method) {
1124         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1125         case CONNMAN_IPCONFIG_METHOD_OFF:
1126         case CONNMAN_IPCONFIG_METHOD_FIXED:
1127                 return -EINVAL;
1128         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1129                 connman_element_unregister_children_type(&network->element,
1130                                         CONNMAN_ELEMENT_TYPE_CONNECTION);
1131                 __connman_ipconfig_clear_address(ipconfig);
1132                 break;
1133         case CONNMAN_IPCONFIG_METHOD_DHCP:
1134                 dhcp_stop(network);
1135                 break;
1136         }
1137
1138         __connman_service_indicate_state(service,
1139                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
1140
1141         return 0;
1142 }
1143
1144 int __connman_network_set_ipconfig(struct connman_network *network, struct connman_ipconfig *ipconfig)
1145 {
1146         enum connman_ipconfig_method method;
1147
1148         method = __connman_ipconfig_get_method(ipconfig);
1149
1150         switch (method) {
1151         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1152         case CONNMAN_IPCONFIG_METHOD_OFF:
1153         case CONNMAN_IPCONFIG_METHOD_FIXED:
1154                 return -EINVAL;
1155         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1156                 return manual_ipv4_set(network, ipconfig);
1157         case CONNMAN_IPCONFIG_METHOD_DHCP:
1158                 return dhcp_start(network);
1159         }
1160
1161         return 0;
1162 }
1163
1164 /**
1165  * connman_network_set_address:
1166  * @network: network structure
1167  * @address: binary address value
1168  * @size: binary address length
1169  *
1170  * Set unique address value for network
1171  */
1172 int connman_network_set_address(struct connman_network *network,
1173                                 const void *address, unsigned int size)
1174 {
1175         const unsigned char *addr_octet = address;
1176         char *str;
1177
1178         DBG("network %p size %d", network, size);
1179
1180         if (size != 6)
1181                 return -EINVAL;
1182
1183         str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1184                                 addr_octet[0], addr_octet[1], addr_octet[2],
1185                                 addr_octet[3], addr_octet[4], addr_octet[5]);
1186         if (str == NULL)
1187                 return -ENOMEM;
1188
1189         g_free(network->address);
1190         network->address = str;
1191
1192         return connman_element_set_string(&network->element,
1193                                                 "Address", network->address);
1194 }
1195
1196 /**
1197  * connman_network_set_name:
1198  * @network: network structure
1199  * @name: name value
1200  *
1201  * Set display name value for network
1202  */
1203 int connman_network_set_name(struct connman_network *network,
1204                                                         const char *name)
1205 {
1206         DBG("network %p name %s", network, name);
1207
1208         g_free(network->name);
1209         network->name = g_strdup(name);
1210
1211         return connman_element_set_string(&network->element, "Name", name);
1212 }
1213
1214 /**
1215  * connman_network_set_strength:
1216  * @network: network structure
1217  * @strength: strength value
1218  *
1219  * Set signal strength value for network
1220  */
1221 int connman_network_set_strength(struct connman_network *network,
1222                                                 connman_uint8_t strength)
1223 {
1224         DBG("network %p strengh %d", network, strength);
1225
1226         network->strength = strength;
1227
1228         return connman_element_set_uint8(&network->element,
1229                                                 "Strength", strength);
1230 }
1231
1232 /**
1233  * connman_network_set_roaming:
1234  * @network: network structure
1235  * @roaming: roaming state
1236  *
1237  * Set roaming state for network
1238  */
1239 int connman_network_set_roaming(struct connman_network *network,
1240                                                 connman_bool_t roaming)
1241 {
1242         DBG("network %p roaming %d", network, roaming);
1243
1244         network->roaming = roaming;
1245
1246         return connman_element_set_bool(&network->element,
1247                                                 "Roaming", roaming);
1248 }
1249
1250 /**
1251  * connman_network_set_string:
1252  * @network: network structure
1253  * @key: unique identifier
1254  * @value: string value
1255  *
1256  * Set string value for specific key
1257  */
1258 int connman_network_set_string(struct connman_network *network,
1259                                         const char *key, const char *value)
1260 {
1261         int err;
1262
1263         DBG("network %p key %s value %s", network, key, value);
1264
1265         if (g_strcmp0(key, "Name") == 0)
1266                 return connman_network_set_name(network, value);
1267
1268         if (g_str_equal(key, "Address") == TRUE) {
1269                 g_free(network->address);
1270                 network->address = g_strdup(value);
1271         } else if (g_str_equal(key, "Node") == TRUE) {
1272                 g_free(network->node);
1273                 network->node = g_strdup(value);
1274         } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1275                 g_free(network->wifi.mode);
1276                 network->wifi.mode = g_strdup(value);
1277         } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1278                 g_free(network->wifi.security);
1279                 network->wifi.security = g_strdup(value);
1280         } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1281                 g_free(network->wifi.passphrase);
1282                 network->wifi.passphrase = g_strdup(value);
1283         } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1284                 g_free(network->wifi.eap);
1285                 network->wifi.eap = g_strdup(value);
1286         } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1287                 g_free(network->wifi.identity);
1288                 network->wifi.identity = g_strdup(value);
1289         } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1290                 g_free(network->wifi.ca_cert_path);
1291                 network->wifi.ca_cert_path = g_strdup(value);
1292         } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1293                 g_free(network->wifi.client_cert_path);
1294                 network->wifi.client_cert_path = g_strdup(value);
1295         } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1296                 g_free(network->wifi.private_key_path);
1297                 network->wifi.private_key_path = g_strdup(value);
1298         } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1299                 g_free(network->wifi.private_key_passphrase);
1300                 network->wifi.private_key_passphrase = g_strdup(value);
1301         } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1302                 g_free(network->wifi.phase2_auth);
1303                 network->wifi.phase2_auth = g_strdup(value);
1304         }
1305
1306         err = connman_element_set_string(&network->element, key, value);
1307         if (err < 0)
1308                 return err;
1309
1310         if (network->driver == NULL)
1311                 return 0;
1312
1313         if (network->driver->setup)
1314                 return network->driver->setup(network, key);
1315
1316         return 0;
1317 }
1318
1319 /**
1320  * connman_network_get_string:
1321  * @network: network structure
1322  * @key: unique identifier
1323  *
1324  * Get string value for specific key
1325  */
1326 const char *connman_network_get_string(struct connman_network *network,
1327                                                         const char *key)
1328 {
1329         DBG("network %p key %s", network, key);
1330
1331         if (g_str_equal(key, "Address") == TRUE)
1332                 return network->address;
1333         else if (g_str_equal(key, "Name") == TRUE)
1334                 return network->name;
1335         else if (g_str_equal(key, "Node") == TRUE)
1336                 return network->node;
1337         else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1338                 return network->wifi.mode;
1339         else if (g_str_equal(key, "WiFi.Security") == TRUE)
1340                 return network->wifi.security;
1341         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1342                 return network->wifi.passphrase;
1343         else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1344                 return network->wifi.eap;
1345         else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1346                 return network->wifi.identity;
1347         else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1348                 return network->wifi.ca_cert_path;
1349         else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1350                 return network->wifi.client_cert_path;
1351         else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1352                 return network->wifi.private_key_path;
1353         else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1354                 return network->wifi.private_key_passphrase;
1355         else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1356                 return network->wifi.phase2_auth;
1357
1358         return connman_element_get_string(&network->element, key);
1359 }
1360
1361 /**
1362  * connman_network_set_bool:
1363  * @network: network structure
1364  * @key: unique identifier
1365  * @value: boolean value
1366  *
1367  * Set boolean value for specific key
1368  */
1369 int connman_network_set_bool(struct connman_network *network,
1370                                         const char *key, connman_bool_t value)
1371 {
1372         DBG("network %p key %s value %d", network, key, value);
1373
1374         if (g_strcmp0(key, "Roaming") == 0)
1375                 return connman_network_set_roaming(network, value);
1376
1377         return connman_element_set_bool(&network->element, key, value);
1378 }
1379
1380 /**
1381  * connman_network_get_bool:
1382  * @network: network structure
1383  * @key: unique identifier
1384  *
1385  * Get boolean value for specific key
1386  */
1387 connman_bool_t connman_network_get_bool(struct connman_network *network,
1388                                                         const char *key)
1389 {
1390         DBG("network %p key %s", network, key);
1391
1392         if (g_str_equal(key, "Roaming") == TRUE)
1393                 return network->roaming;
1394
1395         return connman_element_get_bool(&network->element, key);
1396 }
1397
1398 /**
1399  * connman_network_set_uint8:
1400  * @network: network structure
1401  * @key: unique identifier
1402  * @value: integer value
1403  *
1404  * Set integer value for specific key
1405  */
1406 int connman_network_set_uint8(struct connman_network *network,
1407                                         const char *key, connman_uint8_t value)
1408 {
1409         DBG("network %p key %s value %d", network, key, value);
1410
1411         if (g_strcmp0(key, "Strength") == 0)
1412                 return connman_network_set_strength(network, value);
1413
1414         return connman_element_set_uint8(&network->element, key, value);
1415 }
1416
1417 /**
1418  * connman_network_get_uint8:
1419  * @network: network structure
1420  * @key: unique identifier
1421  *
1422  * Get integer value for specific key
1423  */
1424 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1425                                                         const char *key)
1426 {
1427         DBG("network %p key %s", network, key);
1428
1429         if (g_str_equal(key, "Strength") == TRUE)
1430                 return network->strength;
1431
1432         return connman_element_get_uint8(&network->element, key);
1433 }
1434
1435 /**
1436  * connman_network_set_uint16:
1437  * @network: network structure
1438  * @key: unique identifier
1439  * @value: integer value
1440  *
1441  * Set integer value for specific key
1442  */
1443 int connman_network_set_uint16(struct connman_network *network,
1444                                 const char *key, connman_uint16_t value)
1445 {
1446         DBG("network %p key %s value %d", network, key, value);
1447
1448         if (g_str_equal(key, "Frequency") == TRUE)
1449                 network->frequency = value;
1450         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1451                 network->wifi.channel = value;
1452
1453         return -EINVAL;
1454 }
1455
1456 /**
1457  * connman_network_get_uint16:
1458  * @network: network structure
1459  * @key: unique identifier
1460  *
1461  * Get integer value for specific key
1462  */
1463 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1464                                                         const char *key)
1465 {
1466         DBG("network %p key %s", network, key);
1467
1468         if (g_str_equal(key, "Frequency") == TRUE)
1469                 return network->frequency;
1470         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1471                 return network->wifi.channel;
1472
1473         return 0;
1474 }
1475
1476 /**
1477  * connman_network_set_blob:
1478  * @network: network structure
1479  * @key: unique identifier
1480  * @data: blob data
1481  * @size: blob size
1482  *
1483  * Set binary blob value for specific key
1484  */
1485 int connman_network_set_blob(struct connman_network *network,
1486                         const char *key, const void *data, unsigned int size)
1487 {
1488         DBG("network %p key %s size %d", network, key, size);
1489
1490         if (g_strcmp0(key, "Address") == 0)
1491                 return connman_network_set_address(network, data, size);
1492
1493         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1494                 g_free(network->wifi.ssid);
1495                 network->wifi.ssid = g_try_malloc(size);
1496                 if (network->wifi.ssid != NULL) {
1497                         memcpy(network->wifi.ssid, data, size);
1498                         network->wifi.ssid_len = size;
1499                 } else
1500                         network->wifi.ssid_len = 0;
1501         }
1502
1503         return connman_element_set_blob(&network->element, key, data, size);
1504 }
1505
1506 /**
1507  * connman_network_get_blob:
1508  * @network: network structure
1509  * @key: unique identifier
1510  * @size: pointer to blob size
1511  *
1512  * Get binary blob value for specific key
1513  */
1514 const void *connman_network_get_blob(struct connman_network *network,
1515                                         const char *key, unsigned int *size)
1516 {
1517         DBG("network %p key %s", network, key);
1518
1519         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1520                 if (size != NULL)
1521                         *size = network->wifi.ssid_len;
1522                 return network->wifi.ssid;
1523         }
1524
1525         return connman_element_get_blob(&network->element, key, size);
1526 }
1527
1528 void __connman_network_set_device(struct connman_network *network,
1529                                         struct connman_device *device)
1530 {
1531         network->device = device;
1532 }
1533
1534 /**
1535  * connman_network_get_device:
1536  * @network: network structure
1537  *
1538  * Get parent device of network
1539  */
1540 struct connman_device *connman_network_get_device(struct connman_network *network)
1541 {
1542         return network->device;
1543 }
1544
1545 /**
1546  * connman_network_get_data:
1547  * @network: network structure
1548  *
1549  * Get private network data pointer
1550  */
1551 void *connman_network_get_data(struct connman_network *network)
1552 {
1553         return network->driver_data;
1554 }
1555
1556 /**
1557  * connman_network_set_data:
1558  * @network: network structure
1559  * @data: data pointer
1560  *
1561  * Set private network data pointer
1562  */
1563 void connman_network_set_data(struct connman_network *network, void *data)
1564 {
1565         network->driver_data = data;
1566 }
1567
1568 static gboolean match_driver(struct connman_network *network,
1569                                         struct connman_network_driver *driver)
1570 {
1571         if (network->type == driver->type ||
1572                         driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1573                 return TRUE;
1574
1575         return FALSE;
1576 }
1577
1578 static int network_probe(struct connman_element *element)
1579 {
1580         struct connman_network *network = element->network;
1581         GSList *list;
1582         int err;
1583
1584         DBG("element %p name %s", element, element->name);
1585
1586         if (network == NULL)
1587                 return -ENODEV;
1588
1589         for (list = driver_list; list; list = list->next) {
1590                 struct connman_network_driver *driver = list->data;
1591
1592                 if (match_driver(network, driver) == FALSE)
1593                         continue;
1594
1595                 DBG("driver %p name %s", driver, driver->name);
1596
1597                 if (driver->probe(network) == 0) {
1598                         network->driver = driver;
1599                         break;
1600                 }
1601         }
1602
1603         if (network->driver == NULL)
1604                 return -ENODEV;
1605
1606         err = register_interface(element);
1607         if (err < 0) {
1608                 if (network->driver->remove)
1609                         network->driver->remove(network);
1610                 return err;
1611         }
1612
1613         switch (network->type) {
1614         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1615         case CONNMAN_NETWORK_TYPE_VENDOR:
1616                 break;
1617         case CONNMAN_NETWORK_TYPE_ETHERNET:
1618         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1619         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1620         case CONNMAN_NETWORK_TYPE_CELLULAR:
1621         case CONNMAN_NETWORK_TYPE_WIFI:
1622         case CONNMAN_NETWORK_TYPE_WIMAX:
1623                 if (network->group != NULL)
1624                         __connman_profile_add_network(network);
1625                 break;
1626         }
1627
1628         return 0;
1629 }
1630
1631 static void network_remove(struct connman_element *element)
1632 {
1633         struct connman_network *network = element->network;
1634
1635         DBG("element %p name %s", element, element->name);
1636
1637         if (network == NULL)
1638                 return;
1639
1640         if (network->driver == NULL)
1641                 return;
1642
1643         switch (network->type) {
1644         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1645         case CONNMAN_NETWORK_TYPE_VENDOR:
1646                 break;
1647         case CONNMAN_NETWORK_TYPE_ETHERNET:
1648         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1649         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1650         case CONNMAN_NETWORK_TYPE_CELLULAR:
1651         case CONNMAN_NETWORK_TYPE_WIFI:
1652         case CONNMAN_NETWORK_TYPE_WIMAX:
1653                 if (network->group != NULL) {
1654                         __connman_profile_remove_network(network);
1655
1656                         g_free(network->group);
1657                         network->group = NULL;
1658                 }
1659                 break;
1660         }
1661
1662         unregister_interface(element);
1663
1664         if (network->driver->remove)
1665                 network->driver->remove(network);
1666 }
1667
1668 static void network_change(struct connman_element *element)
1669 {
1670         struct connman_network *network = element->network;
1671
1672         DBG("element %p name %s", element, element->name);
1673
1674         if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1675                 return;
1676
1677         if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1678                 return;
1679
1680         if (network->connected == FALSE)
1681                 return;
1682
1683         connman_element_unregister_children(element);
1684
1685         connman_device_set_disconnected(network->device, TRUE);
1686
1687         if (network->driver && network->driver->disconnect) {
1688                 network->driver->disconnect(network);
1689                 return;
1690         }
1691
1692         network->connected = FALSE;
1693 }
1694
1695 static struct connman_driver network_driver = {
1696         .name           = "network",
1697         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
1698         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1699         .probe          = network_probe,
1700         .remove         = network_remove,
1701         .change         = network_change,
1702 };
1703
1704 int __connman_network_init(void)
1705 {
1706         DBG("");
1707
1708         connection = connman_dbus_get_connection();
1709
1710         return connman_driver_register(&network_driver);
1711 }
1712
1713 void __connman_network_cleanup(void)
1714 {
1715         DBG("");
1716
1717         connman_driver_unregister(&network_driver);
1718
1719         dbus_connection_unref(connection);
1720 }