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