5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
31 static GSList *network_list = NULL;
32 static GSList *driver_list = NULL;
33 static unsigned int hidden_counter = 0;
35 struct connman_network {
37 enum connman_network_type type;
38 connman_bool_t available;
39 connman_bool_t connected;
40 connman_bool_t roaming;
41 connman_bool_t hidden;
42 connman_uint8_t strength;
43 connman_uint16_t frequency;
51 struct connman_network_driver *driver;
54 connman_bool_t connecting;
55 connman_bool_t associating;
57 struct connman_device *device;
63 unsigned short channel;
69 char *client_cert_path;
70 char *private_key_path;
71 char *private_key_passphrase;
74 connman_bool_t use_wps;
84 static const char *type2string(enum connman_network_type type)
87 case CONNMAN_NETWORK_TYPE_UNKNOWN:
88 case CONNMAN_NETWORK_TYPE_VENDOR:
90 case CONNMAN_NETWORK_TYPE_ETHERNET:
92 case CONNMAN_NETWORK_TYPE_WIFI:
94 case CONNMAN_NETWORK_TYPE_WIMAX:
96 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
97 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
99 case CONNMAN_NETWORK_TYPE_CELLULAR:
106 connman_bool_t __connman_network_has_driver(struct connman_network *network)
108 if (network == NULL || network->driver == NULL)
114 static gboolean match_driver(struct connman_network *network,
115 struct connman_network_driver *driver)
117 if (network->type == driver->type ||
118 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
124 static int network_probe(struct connman_network *network)
127 struct connman_network_driver *driver = NULL;
129 DBG("network %p name %s", network, network->name);
131 if (network->driver != NULL)
134 for (list = driver_list; list; list = list->next) {
137 if (match_driver(network, driver) == FALSE)
140 DBG("driver %p name %s", driver, driver->name);
142 if (driver->probe(network) == 0)
151 if (network->group == NULL)
154 switch (network->type) {
155 case CONNMAN_NETWORK_TYPE_UNKNOWN:
156 case CONNMAN_NETWORK_TYPE_VENDOR:
158 case CONNMAN_NETWORK_TYPE_ETHERNET:
159 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
160 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
161 case CONNMAN_NETWORK_TYPE_CELLULAR:
162 case CONNMAN_NETWORK_TYPE_WIFI:
163 case CONNMAN_NETWORK_TYPE_WIMAX:
164 if (__connman_service_create_from_network(network) == NULL)
168 network->driver = driver;
173 static void network_remove(struct connman_network *network)
175 DBG("network %p name %s", network, network->name);
177 if (network->driver == NULL)
180 switch (network->type) {
181 case CONNMAN_NETWORK_TYPE_UNKNOWN:
182 case CONNMAN_NETWORK_TYPE_VENDOR:
184 case CONNMAN_NETWORK_TYPE_ETHERNET:
185 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
186 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
187 case CONNMAN_NETWORK_TYPE_CELLULAR:
188 case CONNMAN_NETWORK_TYPE_WIFI:
189 case CONNMAN_NETWORK_TYPE_WIMAX:
190 if (network->group != NULL) {
191 __connman_service_remove_from_network(network);
193 g_free(network->group);
194 network->group = NULL;
199 if (network->driver->remove)
200 network->driver->remove(network);
202 network->driver = NULL;
205 static void network_change(struct connman_network *network)
207 DBG("network %p name %s", network, network->name);
209 if (network->connected == FALSE)
212 connman_network_unref(network);
214 connman_device_set_disconnected(network->device, TRUE);
216 if (network->driver && network->driver->disconnect) {
217 network->driver->disconnect(network);
221 network->connected = FALSE;
224 static void probe_driver(struct connman_network_driver *driver)
228 DBG("driver %p name %s", driver, driver->name);
230 for (list = network_list; list != NULL; list = list->next) {
231 struct connman_network *network = list->data;
233 if (network->driver != NULL)
236 if (driver->type != network->type)
239 if (driver->probe(network) < 0)
242 network->driver = driver;
246 int connman_network_register(struct connman_network *network)
248 network_list = g_slist_append(network_list, network);
250 return network_probe(network);
254 void connman_network_unregister(struct connman_network *network)
256 network_list = g_slist_remove(network_list, network);
258 network_remove(network);
261 static void remove_driver(struct connman_network_driver *driver)
265 DBG("driver %p name %s", driver, driver->name);
267 for (list = network_list; list != NULL; list = list->next) {
268 struct connman_network *network = list->data;
270 if (network->driver == driver)
271 network_remove(network);
275 static gint compare_priority(gconstpointer a, gconstpointer b)
277 const struct connman_network_driver *driver1 = a;
278 const struct connman_network_driver *driver2 = b;
280 return driver2->priority - driver1->priority;
284 * connman_network_driver_register:
285 * @driver: network driver definition
287 * Register a new network driver
289 * Returns: %0 on success
291 int connman_network_driver_register(struct connman_network_driver *driver)
295 DBG("driver %p name %s", driver, driver->name);
297 for (list = driver_list; list; list = list->next) {
298 struct connman_network_driver *tmp = list->data;
300 if (tmp->type == driver->type)
305 driver_list = g_slist_insert_sorted(driver_list, driver,
308 probe_driver(driver);
314 * connman_network_driver_unregister:
315 * @driver: network driver definition
317 * Remove a previously registered network driver
319 void connman_network_driver_unregister(struct connman_network_driver *driver)
321 DBG("driver %p name %s", driver, driver->name);
323 driver_list = g_slist_remove(driver_list, driver);
325 remove_driver(driver);
328 static void network_destruct(struct connman_network *network)
330 DBG("network %p name %s", network, network->name);
332 g_free(network->wifi.ssid);
333 g_free(network->wifi.mode);
334 g_free(network->wifi.security);
335 g_free(network->wifi.passphrase);
336 g_free(network->wifi.eap);
337 g_free(network->wifi.identity);
338 g_free(network->wifi.ca_cert_path);
339 g_free(network->wifi.client_cert_path);
340 g_free(network->wifi.private_key_path);
341 g_free(network->wifi.private_key_passphrase);
342 g_free(network->wifi.phase2_auth);
343 g_free(network->wifi.pin_wps);
345 g_free(network->path);
346 g_free(network->group);
347 g_free(network->node);
348 g_free(network->name);
349 g_free(network->identifier);
350 g_free(network->path);
352 network->device = NULL;
358 * connman_network_create:
359 * @identifier: network identifier (for example an unqiue name)
361 * Allocate a new network and assign the #identifier to it.
363 * Returns: a newly-allocated #connman_network structure
365 struct connman_network *connman_network_create(const char *identifier,
366 enum connman_network_type type)
368 struct connman_network *network;
371 DBG("identifier %s type %d", identifier, type);
373 network = g_try_new0(struct connman_network, 1);
377 DBG("network %p", network);
379 network->refcount = 1;
381 if (identifier == NULL) {
382 temp = g_strdup_printf("hidden_%d", hidden_counter++);
383 network->hidden = TRUE;
385 temp = g_strdup(identifier);
392 network->type = type;
393 network->identifier = g_strdup(temp);
399 * connman_network_ref:
400 * @network: network structure
402 * Increase reference counter of network
404 struct connman_network *connman_network_ref(struct connman_network *network)
406 DBG("network %p name %s refcount %d", network, network->name,
407 g_atomic_int_get(&network->refcount) + 1);
409 g_atomic_int_inc(&network->refcount);
415 * connman_network_unref:
416 * @network: network structure
418 * Decrease reference counter of network
420 void connman_network_unref(struct connman_network *network)
422 DBG("network %p name %s refcount %d", network, network->name,
423 g_atomic_int_get(&network->refcount) - 1);
425 if (g_atomic_int_dec_and_test(&network->refcount) == FALSE)
428 network_destruct(network);
431 const char *__connman_network_get_type(struct connman_network *network)
433 return type2string(network->type);
437 * connman_network_get_type:
438 * @network: network structure
440 * Get type of network
442 enum connman_network_type connman_network_get_type(struct connman_network *network)
444 return network->type;
448 * connman_network_get_identifier:
449 * @network: network structure
451 * Get identifier of network
453 const char *connman_network_get_identifier(struct connman_network *network)
455 return network->identifier;
459 * connman_network_set_index:
460 * @network: network structure
461 * @index: index number
463 * Set index number of network
465 void connman_network_set_index(struct connman_network *network, int index)
467 struct connman_service *service;
468 struct connman_ipconfig *ipconfig;
470 service = __connman_service_lookup_from_network(network);
474 ipconfig = __connman_service_get_ip4config(service);
476 DBG("index %d service %p ip4config %p", network->index,
479 if (network->index < 0 && ipconfig == NULL) {
481 ipconfig = __connman_service_get_ip4config(service);
482 if (ipconfig == NULL)
484 * This is needed for plugins that havent set their
485 * ipconfig layer yet, due to not being able to get
486 * a network index prior to creating a service.
488 __connman_service_create_ip4config(service, index);
490 __connman_ipconfig_set_index(ipconfig, index);
493 /* If index changed, the index of ipconfig must be reset. */
494 if (ipconfig == NULL)
497 __connman_ipconfig_set_index(ipconfig, index);
501 network->index = index;
505 * connman_network_get_index:
506 * @network: network structure
508 * Get index number of network
510 int connman_network_get_index(struct connman_network *network)
512 return network->index;
516 * connman_network_set_group:
517 * @network: network structure
520 * Set group name for automatic clustering
522 void connman_network_set_group(struct connman_network *network,
525 switch (network->type) {
526 case CONNMAN_NETWORK_TYPE_UNKNOWN:
527 case CONNMAN_NETWORK_TYPE_VENDOR:
529 case CONNMAN_NETWORK_TYPE_ETHERNET:
530 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
531 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
532 case CONNMAN_NETWORK_TYPE_CELLULAR:
533 case CONNMAN_NETWORK_TYPE_WIFI:
534 case CONNMAN_NETWORK_TYPE_WIMAX:
538 if (g_strcmp0(network->group, group) == 0) {
540 __connman_service_update_from_network(network);
544 if (network->group != NULL) {
545 __connman_service_remove_from_network(network);
547 g_free(network->group);
550 network->group = g_strdup(group);
552 if (network->group != NULL)
553 network_probe(network);
557 * connman_network_get_group:
558 * @network: network structure
560 * Get group name for automatic clustering
562 const char *connman_network_get_group(struct connman_network *network)
564 return network->group;
567 const char *__connman_network_get_ident(struct connman_network *network)
569 if (network->device == NULL)
572 return connman_device_get_ident(network->device);
575 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
577 switch (network->type) {
578 case CONNMAN_NETWORK_TYPE_UNKNOWN:
579 case CONNMAN_NETWORK_TYPE_VENDOR:
580 case CONNMAN_NETWORK_TYPE_ETHERNET:
581 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
582 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
583 case CONNMAN_NETWORK_TYPE_CELLULAR:
584 case CONNMAN_NETWORK_TYPE_WIMAX:
586 case CONNMAN_NETWORK_TYPE_WIFI:
587 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
589 if (network->strength > 0 && network->strength < 20)
597 connman_bool_t connman_network_get_connecting(struct connman_network *network)
599 return network->connecting;
603 * connman_network_set_available:
604 * @network: network structure
605 * @available: availability state
607 * Change availability state of network (in range)
609 int connman_network_set_available(struct connman_network *network,
610 connman_bool_t available)
612 DBG("network %p available %d", network, available);
614 if (network->available == available)
617 network->available = available;
623 * connman_network_get_available:
624 * @network: network structure
626 * Get network available setting
628 connman_bool_t connman_network_get_available(struct connman_network *network)
630 if (network->hidden == TRUE)
633 return network->available;
637 * connman_network_set_associating:
638 * @network: network structure
639 * @associating: associating state
641 * Change associating state of network
643 int connman_network_set_associating(struct connman_network *network,
644 connman_bool_t associating)
646 DBG("network %p associating %d", network, associating);
648 if (network->associating == associating)
651 network->associating = associating;
653 if (associating == TRUE) {
654 struct connman_service *service;
656 service = __connman_service_lookup_from_network(network);
657 __connman_service_indicate_state(service,
658 CONNMAN_SERVICE_STATE_ASSOCIATION,
659 CONNMAN_IPCONFIG_TYPE_IPV4);
660 __connman_service_indicate_state(service,
661 CONNMAN_SERVICE_STATE_ASSOCIATION,
662 CONNMAN_IPCONFIG_TYPE_IPV6);
668 static void set_associate_error(struct connman_network *network)
670 struct connman_service *service;
672 if (network->associating == FALSE)
675 network->associating = FALSE;
677 service = __connman_service_lookup_from_network(network);
679 __connman_service_indicate_state(service,
680 CONNMAN_SERVICE_STATE_FAILURE,
681 CONNMAN_IPCONFIG_TYPE_IPV4);
684 static void set_configure_error(struct connman_network *network)
686 struct connman_service *service;
688 network->connecting = FALSE;
690 service = __connman_service_lookup_from_network(network);
692 __connman_service_indicate_state(service,
693 CONNMAN_SERVICE_STATE_FAILURE,
694 CONNMAN_IPCONFIG_TYPE_IPV4);
697 static void set_invalid_key_error(struct connman_network *network)
699 struct connman_service *service;
701 service = __connman_service_lookup_from_network(network);
703 __connman_service_indicate_error(service,
704 CONNMAN_SERVICE_ERROR_INVALID_KEY);
707 static void set_connect_error(struct connman_network *network)
709 struct connman_service *service;
711 service = __connman_service_lookup_from_network(network);
713 __connman_service_indicate_error(service,
714 CONNMAN_SERVICE_ERROR_CONNECT_FAILED);
717 void connman_network_set_ipv4_method(struct connman_network *network,
718 enum connman_ipconfig_method method)
720 struct connman_service *service;
721 struct connman_ipconfig *ipconfig;
723 service = __connman_service_lookup_from_network(network);
727 ipconfig = __connman_service_get_ip4config(service);
728 if (ipconfig == NULL)
731 connman_ipconfig_set_method(ipconfig, method);
734 void connman_network_set_ipv6_method(struct connman_network *network,
735 enum connman_ipconfig_method method)
737 struct connman_service *service;
738 struct connman_ipconfig *ipconfig;
740 service = __connman_service_lookup_from_network(network);
744 ipconfig = __connman_service_get_ip6config(service);
745 if (ipconfig == NULL)
748 connman_ipconfig_set_method(ipconfig, method);
751 void connman_network_set_error(struct connman_network *network,
752 enum connman_network_error error)
754 DBG("nework %p, error %d", network, error);
756 network->connecting = FALSE;
759 case CONNMAN_NETWORK_ERROR_UNKNOWN:
761 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
762 set_associate_error(network);
764 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
765 set_configure_error(network);
767 case CONNMAN_NETWORK_ERROR_INVALID_KEY:
768 set_invalid_key_error(network);
770 case CONNMAN_NETWORK_ERROR_CONNECT_FAIL:
771 set_connect_error(network);
775 network_change(network);
778 void connman_network_clear_error(struct connman_network *network)
780 struct connman_service *service;
782 DBG("network %p", network);
787 if (network->connecting == TRUE || network->associating == TRUE)
790 service = __connman_service_lookup_from_network(network);
791 __connman_service_clear_error(service);
794 static void set_configuration(struct connman_network *network)
796 struct connman_service *service;
798 DBG("network %p", network);
800 __connman_device_increase_connections(network->device);
802 __connman_device_set_network(network->device, network);
804 connman_device_set_disconnected(network->device, FALSE);
806 service = __connman_service_lookup_from_network(network);
807 __connman_service_indicate_state(service,
808 CONNMAN_SERVICE_STATE_CONFIGURATION,
809 CONNMAN_IPCONFIG_TYPE_IPV4);
812 static void dhcp_success(struct connman_network *network)
814 struct connman_service *service;
815 struct connman_ipconfig *ipconfig_ipv4;
818 service = __connman_service_lookup_from_network(network);
822 connman_network_set_associating(network, FALSE);
824 network->connecting = FALSE;
826 ipconfig_ipv4 = __connman_service_get_ip4config(service);
827 err = __connman_ipconfig_address_add(ipconfig_ipv4);
831 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
835 __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
840 connman_network_set_error(network,
841 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
844 static void dhcp_failure(struct connman_network *network)
846 struct connman_service *service;
848 service = __connman_service_lookup_from_network(network);
852 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
853 CONNMAN_IPCONFIG_TYPE_IPV4);
856 static void dhcp_callback(struct connman_network *network,
857 connman_bool_t success)
859 DBG("success %d", success);
862 dhcp_success(network);
864 dhcp_failure(network);
867 static int set_connected_fixed(struct connman_network *network)
869 struct connman_service *service;
870 struct connman_ipconfig *ipconfig_ipv4;
875 service = __connman_service_lookup_from_network(network);
877 ipconfig_ipv4 = __connman_service_get_ip4config(service);
879 set_configuration(network);
881 network->connecting = FALSE;
883 connman_network_set_associating(network, FALSE);
885 err = __connman_ipconfig_address_add(ipconfig_ipv4);
889 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
893 __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
898 connman_network_set_error(network,
899 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
904 static void set_connected_manual(struct connman_network *network)
906 struct connman_service *service;
907 struct connman_ipconfig *ipconfig;
910 DBG("network %p", network);
912 service = __connman_service_lookup_from_network(network);
914 ipconfig = __connman_service_get_ip4config(service);
916 set_configuration(network);
918 err = __connman_ipconfig_address_add(ipconfig);
922 err = __connman_ipconfig_gateway_add(ipconfig);
926 network->connecting = FALSE;
928 connman_network_set_associating(network, FALSE);
930 __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
935 connman_network_set_error(network,
936 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
940 static int set_connected_dhcp(struct connman_network *network)
944 DBG("network %p", network);
946 set_configuration(network);
948 err = __connman_dhcp_start(network, dhcp_callback);
950 connman_error("Can not request DHCP lease");
957 static int manual_ipv6_set(struct connman_network *network,
958 struct connman_ipconfig *ipconfig_ipv6)
960 struct connman_service *service;
963 DBG("network %p ipv6 %p", network, ipconfig_ipv6);
965 service = __connman_service_lookup_from_network(network);
969 err = __connman_ipconfig_address_add(ipconfig_ipv6);
971 connman_network_set_error(network,
972 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
976 err = __connman_ipconfig_gateway_add(ipconfig_ipv6);
980 __connman_connection_gateway_activate(service,
981 CONNMAN_IPCONFIG_TYPE_IPV6);
983 __connman_device_increase_connections(network->device);
985 __connman_device_set_network(network->device, network);
987 connman_device_set_disconnected(network->device, FALSE);
989 network->connecting = FALSE;
991 __connman_service_set_ipconfig_ready(service,
992 CONNMAN_IPCONFIG_TYPE_IPV6);
997 static void autoconf_ipv6_set(struct connman_network *network)
999 struct connman_service *service;
1001 DBG("network %p", network);
1003 service = __connman_service_lookup_from_network(network);
1005 __connman_device_increase_connections(network->device);
1007 __connman_device_set_network(network->device, network);
1009 connman_device_set_disconnected(network->device, FALSE);
1011 /* XXX: Append IPv6 nameservers here */
1013 network->connecting = FALSE;
1015 __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1018 static gboolean set_connected(gpointer user_data)
1020 struct connman_network *network = user_data;
1021 struct connman_service *service;
1022 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
1023 enum connman_ipconfig_method ipv4_method, ipv6_method;
1025 service = __connman_service_lookup_from_network(network);
1027 ipconfig_ipv4 = __connman_service_get_ip4config(service);
1028 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1030 DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
1033 ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
1034 ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
1036 DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
1037 DBG("network connected %d", network->connected);
1039 if (network->connected == TRUE) {
1042 switch (ipv6_method) {
1043 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1044 case CONNMAN_IPCONFIG_METHOD_OFF:
1046 case CONNMAN_IPCONFIG_METHOD_AUTO:
1047 autoconf_ipv6_set(network);
1049 case CONNMAN_IPCONFIG_METHOD_FIXED:
1050 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1051 ret = manual_ipv6_set(network, ipconfig_ipv6);
1053 connman_network_set_error(network,
1054 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1058 case CONNMAN_IPCONFIG_METHOD_DHCP:
1062 switch (ipv4_method) {
1063 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1064 case CONNMAN_IPCONFIG_METHOD_OFF:
1065 case CONNMAN_IPCONFIG_METHOD_AUTO:
1067 case CONNMAN_IPCONFIG_METHOD_FIXED:
1068 if (set_connected_fixed(network) < 0) {
1069 connman_network_set_error(network,
1070 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1074 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1075 set_connected_manual(network);
1077 case CONNMAN_IPCONFIG_METHOD_DHCP:
1078 if (set_connected_dhcp(network) < 0) {
1079 connman_network_set_error(network,
1080 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1086 struct connman_service *service;
1088 __connman_device_set_network(network->device, NULL);
1089 network->hidden = FALSE;
1091 service = __connman_service_lookup_from_network(network);
1093 __connman_service_indicate_state(service,
1094 CONNMAN_SERVICE_STATE_DISCONNECT,
1095 CONNMAN_IPCONFIG_TYPE_IPV4);
1097 __connman_service_indicate_state(service,
1098 CONNMAN_SERVICE_STATE_DISCONNECT,
1099 CONNMAN_IPCONFIG_TYPE_IPV6);
1101 __connman_connection_gateway_remove(service,
1102 CONNMAN_IPCONFIG_TYPE_ALL);
1104 __connman_ipconfig_address_unset(ipconfig_ipv4);
1105 __connman_ipconfig_address_unset(ipconfig_ipv6);
1108 * Special handling for IPv6 autoconfigured address.
1109 * The simplest way to remove autoconfigured routes is to
1110 * disable IPv6 temporarily so that kernel will do the cleanup
1113 if (ipv6_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1114 __connman_ipconfig_disable_ipv6(ipconfig_ipv6);
1115 __connman_ipconfig_enable_ipv6(ipconfig_ipv6);
1118 __connman_service_indicate_state(service,
1119 CONNMAN_SERVICE_STATE_IDLE,
1120 CONNMAN_IPCONFIG_TYPE_IPV4);
1122 __connman_service_indicate_state(service,
1123 CONNMAN_SERVICE_STATE_IDLE,
1124 CONNMAN_IPCONFIG_TYPE_IPV6);
1127 network->connecting = FALSE;
1129 connman_network_set_associating(network, FALSE);
1135 * connman_network_set_connected:
1136 * @network: network structure
1137 * @connected: connected state
1139 * Change connected state of network
1141 int connman_network_set_connected(struct connman_network *network,
1142 connman_bool_t connected)
1144 DBG("network %p connected %d", network, connected);
1146 if ((network->connecting == TRUE || network->associating == TRUE) &&
1147 connected == FALSE) {
1148 connman_network_set_error(network,
1149 CONNMAN_NETWORK_ERROR_CONNECT_FAIL);
1150 __connman_network_disconnect(network);
1153 if (network->connected == connected)
1156 if (connected == FALSE)
1157 __connman_device_decrease_connections(network->device);
1159 network->connected = connected;
1161 set_connected(network);
1167 * connman_network_get_connected:
1168 * @network: network structure
1170 * Get network connection status
1172 connman_bool_t connman_network_get_connected(struct connman_network *network)
1174 return network->connected;
1178 * connman_network_get_associating:
1179 * @network: network structure
1181 * Get network associating status
1183 connman_bool_t connman_network_get_associating(struct connman_network *network)
1185 return network->associating;
1189 * __connman_network_connect:
1190 * @network: network structure
1194 int __connman_network_connect(struct connman_network *network)
1198 DBG("network %p", network);
1200 if (network->connected == TRUE)
1203 if (network->connecting == TRUE || network->associating == TRUE)
1206 if (network->driver == NULL)
1209 if (network->driver->connect == NULL)
1212 if (network->device == NULL)
1215 network->connecting = TRUE;
1217 __connman_device_disconnect(network->device);
1219 err = network->driver->connect(network);
1221 if (err == -EINPROGRESS)
1222 connman_network_set_associating(network, TRUE);
1224 network->connecting = FALSE;
1225 network->hidden = FALSE;
1231 network->connected = TRUE;
1232 set_connected(network);
1238 * __connman_network_disconnect:
1239 * @network: network structure
1241 * Disconnect network
1243 int __connman_network_disconnect(struct connman_network *network)
1247 DBG("network %p", network);
1249 if (network->connected == FALSE && network->connecting == FALSE &&
1250 network->associating == FALSE)
1253 if (network->driver == NULL)
1256 if (network->driver->disconnect == NULL)
1259 network->connecting = FALSE;
1261 err = network->driver->disconnect(network);
1263 connman_network_set_connected(network, FALSE);
1264 set_connected(network);
1270 static int manual_ipv4_set(struct connman_network *network,
1271 struct connman_ipconfig *ipconfig)
1273 struct connman_service *service;
1276 service = __connman_service_lookup_from_network(network);
1277 if (service == NULL)
1280 err = __connman_ipconfig_address_add(ipconfig);
1282 connman_network_set_error(network,
1283 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1287 __connman_ipconfig_gateway_add(ipconfig);
1289 __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
1294 int __connman_network_clear_ipconfig(struct connman_network *network,
1295 struct connman_ipconfig *ipconfig)
1297 struct connman_service *service;
1298 enum connman_ipconfig_method method;
1299 enum connman_ipconfig_type type;
1301 service = __connman_service_lookup_from_network(network);
1302 if (service == NULL)
1305 method = __connman_ipconfig_get_method(ipconfig);
1306 type = __connman_ipconfig_get_config_type(ipconfig);
1309 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1310 case CONNMAN_IPCONFIG_METHOD_OFF:
1311 case CONNMAN_IPCONFIG_METHOD_FIXED:
1312 case CONNMAN_IPCONFIG_METHOD_AUTO:
1314 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1315 __connman_ipconfig_address_remove(ipconfig);
1317 case CONNMAN_IPCONFIG_METHOD_DHCP:
1318 __connman_dhcp_stop(network);
1322 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1323 __connman_service_indicate_state(service,
1324 CONNMAN_SERVICE_STATE_CONFIGURATION,
1325 CONNMAN_IPCONFIG_TYPE_IPV6);
1326 else if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1327 __connman_service_indicate_state(service,
1328 CONNMAN_SERVICE_STATE_CONFIGURATION,
1329 CONNMAN_IPCONFIG_TYPE_IPV4);
1334 int __connman_network_set_ipconfig(struct connman_network *network,
1335 struct connman_ipconfig *ipconfig_ipv4,
1336 struct connman_ipconfig *ipconfig_ipv6)
1338 enum connman_ipconfig_method method;
1341 if (ipconfig_ipv6) {
1342 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1345 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1346 case CONNMAN_IPCONFIG_METHOD_OFF:
1348 case CONNMAN_IPCONFIG_METHOD_AUTO:
1349 autoconf_ipv6_set(network);
1351 case CONNMAN_IPCONFIG_METHOD_FIXED:
1352 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1353 ret = manual_ipv6_set(network, ipconfig_ipv6);
1355 connman_network_set_error(network,
1356 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1360 case CONNMAN_IPCONFIG_METHOD_DHCP:
1365 if (ipconfig_ipv4) {
1366 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1369 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1370 case CONNMAN_IPCONFIG_METHOD_OFF:
1371 case CONNMAN_IPCONFIG_METHOD_FIXED:
1372 case CONNMAN_IPCONFIG_METHOD_AUTO:
1374 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1375 return manual_ipv4_set(network, ipconfig_ipv4);
1376 case CONNMAN_IPCONFIG_METHOD_DHCP:
1377 return __connman_dhcp_start(network, dhcp_callback);
1384 int connman_network_set_ipaddress(struct connman_network *network,
1385 struct connman_ipaddress *ipaddress)
1387 struct connman_service *service;
1388 struct connman_ipconfig *ipconfig = NULL;
1390 DBG("network %p", network);
1392 service = __connman_service_lookup_from_network(network);
1393 if (service == NULL)
1396 ipconfig = __connman_service_get_ipconfig(service, ipaddress->family);
1397 if (ipconfig == NULL)
1400 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1401 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1402 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1403 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1404 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1409 int connman_network_set_pac(struct connman_network *network,
1412 struct connman_service *service;
1414 DBG("network %p pac %s", network, pac);
1416 service = __connman_service_lookup_from_network(network);
1417 if (service == NULL)
1420 __connman_service_set_pac(service, pac);
1425 int connman_network_set_nameservers(struct connman_network *network,
1426 const char *nameservers)
1428 struct connman_service *service;
1429 char **nameservers_array = NULL;
1432 DBG("network %p nameservers %s", network, nameservers);
1434 service = __connman_service_lookup_from_network(network);
1435 if (service == NULL)
1438 __connman_service_nameserver_clear(service);
1440 if (nameservers != NULL)
1441 nameservers_array = g_strsplit(nameservers, " ", 0);
1443 for (i = 0; nameservers_array[i] != NULL; i++) {
1444 __connman_service_nameserver_append(service,
1445 nameservers_array[i]);
1448 g_strfreev(nameservers_array);
1453 int connman_network_set_domain(struct connman_network *network,
1456 struct connman_service *service;
1458 DBG("network %p domain %s", network, domain);
1460 service = __connman_service_lookup_from_network(network);
1461 if (service == NULL)
1464 __connman_service_set_domainname(service, domain);
1470 * connman_network_set_name:
1471 * @network: network structure
1474 * Set display name value for network
1476 int connman_network_set_name(struct connman_network *network,
1479 DBG("network %p name %s", network, name);
1481 g_free(network->name);
1482 network->name = g_strdup(name);
1488 * connman_network_set_strength:
1489 * @network: network structure
1490 * @strength: strength value
1492 * Set signal strength value for network
1495 int connman_network_set_strength(struct connman_network *network,
1496 connman_uint8_t strength)
1498 DBG("network %p strengh %d", network, strength);
1500 network->strength = strength;
1505 connman_uint8_t connman_network_get_strength(struct connman_network *network)
1507 return network->strength;
1510 int connman_network_set_frequency(struct connman_network *network,
1511 connman_uint16_t frequency)
1513 DBG("network %p frequency %d", network, frequency);
1515 network->frequency = frequency;
1520 connman_uint16_t connman_network_get_frequency(struct connman_network *network)
1522 return network->frequency;
1525 int connman_network_set_wifi_channel(struct connman_network *network,
1526 connman_uint16_t channel)
1528 DBG("network %p wifi channel %d", network, channel);
1530 network->wifi.channel = channel;
1535 connman_uint16_t connman_network_get_wifi_channel(struct connman_network *network)
1537 return network->wifi.channel;
1541 * connman_network_set_roaming:
1542 * @network: network structure
1543 * @roaming: roaming state
1545 * Set roaming state for network
1547 int connman_network_set_roaming(struct connman_network *network,
1548 connman_bool_t roaming)
1550 DBG("network %p roaming %d", network, roaming);
1552 network->roaming = roaming;
1558 * connman_network_set_string:
1559 * @network: network structure
1560 * @key: unique identifier
1561 * @value: string value
1563 * Set string value for specific key
1565 int connman_network_set_string(struct connman_network *network,
1566 const char *key, const char *value)
1568 DBG("network %p key %s value %s", network, key, value);
1570 if (g_strcmp0(key, "Name") == 0)
1571 return connman_network_set_name(network, value);
1573 if (g_str_equal(key, "Path") == TRUE) {
1574 g_free(network->path);
1575 network->path = g_strdup(value);
1576 } else if (g_str_equal(key, "Node") == TRUE) {
1577 g_free(network->node);
1578 network->node = g_strdup(value);
1579 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1580 g_free(network->wifi.mode);
1581 network->wifi.mode = g_strdup(value);
1582 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1583 g_free(network->wifi.security);
1584 network->wifi.security = g_strdup(value);
1585 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1586 g_free(network->wifi.passphrase);
1587 network->wifi.passphrase = g_strdup(value);
1588 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1589 g_free(network->wifi.eap);
1590 network->wifi.eap = g_strdup(value);
1591 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1592 g_free(network->wifi.identity);
1593 network->wifi.identity = g_strdup(value);
1594 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1595 g_free(network->wifi.ca_cert_path);
1596 network->wifi.ca_cert_path = g_strdup(value);
1597 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1598 g_free(network->wifi.client_cert_path);
1599 network->wifi.client_cert_path = g_strdup(value);
1600 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1601 g_free(network->wifi.private_key_path);
1602 network->wifi.private_key_path = g_strdup(value);
1603 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1604 g_free(network->wifi.private_key_passphrase);
1605 network->wifi.private_key_passphrase = g_strdup(value);
1606 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1607 g_free(network->wifi.phase2_auth);
1608 network->wifi.phase2_auth = g_strdup(value);
1609 } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1610 g_free(network->wifi.pin_wps);
1611 network->wifi.pin_wps = g_strdup(value);
1620 * connman_network_get_string:
1621 * @network: network structure
1622 * @key: unique identifier
1624 * Get string value for specific key
1626 const char *connman_network_get_string(struct connman_network *network,
1629 DBG("network %p key %s", network, key);
1631 if (g_str_equal(key, "Path") == TRUE)
1632 return network->path;
1633 else if (g_str_equal(key, "Name") == TRUE)
1634 return network->name;
1635 else if (g_str_equal(key, "Node") == TRUE)
1636 return network->node;
1637 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1638 return network->wifi.mode;
1639 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1640 return network->wifi.security;
1641 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1642 return network->wifi.passphrase;
1643 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1644 return network->wifi.eap;
1645 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1646 return network->wifi.identity;
1647 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1648 return network->wifi.ca_cert_path;
1649 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1650 return network->wifi.client_cert_path;
1651 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1652 return network->wifi.private_key_path;
1653 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1654 return network->wifi.private_key_passphrase;
1655 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1656 return network->wifi.phase2_auth;
1657 else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1658 return network->wifi.pin_wps;
1664 * connman_network_set_bool:
1665 * @network: network structure
1666 * @key: unique identifier
1667 * @value: boolean value
1669 * Set boolean value for specific key
1671 int connman_network_set_bool(struct connman_network *network,
1672 const char *key, connman_bool_t value)
1674 DBG("network %p key %s value %d", network, key, value);
1676 if (g_strcmp0(key, "Roaming") == 0)
1677 return connman_network_set_roaming(network, value);
1678 else if (g_strcmp0(key, "WiFi.WPS") == 0)
1679 network->wifi.wps = value;
1680 else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1681 network->wifi.use_wps = value;
1687 * connman_network_get_bool:
1688 * @network: network structure
1689 * @key: unique identifier
1691 * Get boolean value for specific key
1693 connman_bool_t connman_network_get_bool(struct connman_network *network,
1696 DBG("network %p key %s", network, key);
1698 if (g_str_equal(key, "Roaming") == TRUE)
1699 return network->roaming;
1700 else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1701 return network->wifi.wps;
1702 else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1703 return network->wifi.use_wps;
1709 * connman_network_set_blob:
1710 * @network: network structure
1711 * @key: unique identifier
1715 * Set binary blob value for specific key
1717 int connman_network_set_blob(struct connman_network *network,
1718 const char *key, const void *data, unsigned int size)
1720 DBG("network %p key %s size %d", network, key, size);
1722 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1723 g_free(network->wifi.ssid);
1724 network->wifi.ssid = g_try_malloc(size);
1725 if (network->wifi.ssid != NULL) {
1726 memcpy(network->wifi.ssid, data, size);
1727 network->wifi.ssid_len = size;
1729 network->wifi.ssid_len = 0;
1738 * connman_network_get_blob:
1739 * @network: network structure
1740 * @key: unique identifier
1741 * @size: pointer to blob size
1743 * Get binary blob value for specific key
1745 const void *connman_network_get_blob(struct connman_network *network,
1746 const char *key, unsigned int *size)
1748 DBG("network %p key %s", network, key);
1750 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1752 *size = network->wifi.ssid_len;
1753 return network->wifi.ssid;
1759 void __connman_network_set_device(struct connman_network *network,
1760 struct connman_device *device)
1762 if (network->device == device)
1765 if (network->device != NULL)
1766 network_remove(network);
1768 network->device = device;
1770 if (network->device != NULL)
1771 network_probe(network);
1775 * connman_network_get_device:
1776 * @network: network structure
1778 * Get parent device of network
1780 struct connman_device *connman_network_get_device(struct connman_network *network)
1782 return network->device;
1786 * connman_network_get_data:
1787 * @network: network structure
1789 * Get private network data pointer
1791 void *connman_network_get_data(struct connman_network *network)
1793 return network->driver_data;
1797 * connman_network_set_data:
1798 * @network: network structure
1799 * @data: data pointer
1801 * Set private network data pointer
1803 void connman_network_set_data(struct connman_network *network, void *data)
1805 network->driver_data = data;
1808 void connman_network_update(struct connman_network *network)
1810 switch (network->type) {
1811 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1812 case CONNMAN_NETWORK_TYPE_VENDOR:
1814 case CONNMAN_NETWORK_TYPE_ETHERNET:
1815 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1816 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1817 case CONNMAN_NETWORK_TYPE_CELLULAR:
1818 case CONNMAN_NETWORK_TYPE_WIFI:
1819 case CONNMAN_NETWORK_TYPE_WIMAX:
1823 if (network->group != NULL)
1824 __connman_service_update_from_network(network);
1827 int __connman_network_init(void)
1834 void __connman_network_cleanup(void)