5 * Copyright (C) 2007-2012 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 #include <sys/ioctl.h>
32 #include <net/ethernet.h>
37 static GSList *device_list = NULL;
38 static gchar **device_filter = NULL;
39 static gchar **nodevice_filter = NULL;
41 enum connman_pending_type {
47 struct connman_device {
49 enum connman_device_type type;
50 enum connman_pending_type powered_pending; /* Indicates a pending
51 enable/disable request */
52 connman_bool_t powered;
53 connman_bool_t scanning;
54 connman_bool_t disconnected;
55 connman_bool_t reconnect;
64 guint pending_timeout;
66 struct connman_device_driver *driver;
70 struct connman_network *network;
74 static void clear_pending_trigger(struct connman_device *device)
76 if (device->pending_timeout > 0) {
77 g_source_remove(device->pending_timeout);
78 device->pending_timeout = 0;
82 static const char *type2description(enum connman_device_type type)
85 case CONNMAN_DEVICE_TYPE_UNKNOWN:
86 case CONNMAN_DEVICE_TYPE_VENDOR:
88 case CONNMAN_DEVICE_TYPE_ETHERNET:
90 case CONNMAN_DEVICE_TYPE_WIFI:
92 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
94 case CONNMAN_DEVICE_TYPE_GPS:
96 case CONNMAN_DEVICE_TYPE_CELLULAR:
98 case CONNMAN_DEVICE_TYPE_GADGET:
106 static const char *type2string(enum connman_device_type type)
109 case CONNMAN_DEVICE_TYPE_UNKNOWN:
110 case CONNMAN_DEVICE_TYPE_VENDOR:
112 case CONNMAN_DEVICE_TYPE_ETHERNET:
114 case CONNMAN_DEVICE_TYPE_WIFI:
116 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
118 case CONNMAN_DEVICE_TYPE_GPS:
120 case CONNMAN_DEVICE_TYPE_CELLULAR:
122 case CONNMAN_DEVICE_TYPE_GADGET:
130 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
132 enum connman_device_type type = connman_device_get_type(device);
135 case CONNMAN_DEVICE_TYPE_UNKNOWN:
136 case CONNMAN_DEVICE_TYPE_VENDOR:
137 case CONNMAN_DEVICE_TYPE_GPS:
139 case CONNMAN_DEVICE_TYPE_ETHERNET:
140 return CONNMAN_SERVICE_TYPE_ETHERNET;
141 case CONNMAN_DEVICE_TYPE_WIFI:
142 return CONNMAN_SERVICE_TYPE_WIFI;
143 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
144 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
145 case CONNMAN_DEVICE_TYPE_CELLULAR:
146 return CONNMAN_SERVICE_TYPE_CELLULAR;
147 case CONNMAN_DEVICE_TYPE_GADGET:
148 return CONNMAN_SERVICE_TYPE_GADGET;
152 return CONNMAN_SERVICE_TYPE_UNKNOWN;
155 static gboolean device_pending_reset(gpointer user_data)
157 struct connman_device *device = user_data;
159 DBG("device %p", device);
161 /* Power request timedout, reset power pending state. */
162 device->pending_timeout = 0;
163 device->powered_pending = PENDING_NONE;
168 int __connman_device_enable(struct connman_device *device)
172 DBG("device %p", device);
174 if (!device->driver || !device->driver->enable)
177 /* There is an ongoing power disable request. */
178 if (device->powered_pending == PENDING_DISABLE)
181 if (device->powered_pending == PENDING_ENABLE)
184 if (device->powered_pending == PENDING_NONE && device->powered == TRUE)
187 device->powered_pending = PENDING_ENABLE;
189 err = device->driver->enable(device);
191 * device gets enabled right away.
192 * Invoke the callback
195 connman_device_set_powered(device, TRUE);
199 if (err == -EALREADY) {
200 /* If device is already powered, but connman is not updated */
201 connman_device_set_powered(device, TRUE);
205 * if err == -EINPROGRESS, then the DBus call to the respective daemon
206 * was successful. We set a 4 sec timeout so if the daemon never
207 * returns a reply, we would reset the pending request.
209 if (err == -EINPROGRESS)
210 device->pending_timeout = g_timeout_add_seconds(4,
211 device_pending_reset, device);
216 int __connman_device_disable(struct connman_device *device)
220 DBG("device %p", device);
222 if (!device->driver || !device->driver->disable)
225 /* Ongoing power enable request */
226 if (device->powered_pending == PENDING_ENABLE)
229 if (device->powered_pending == PENDING_DISABLE)
232 if (device->powered_pending == PENDING_NONE && device->powered == FALSE)
235 device->powered_pending = PENDING_DISABLE;
236 device->reconnect = FALSE;
238 if (device->network) {
239 struct connman_service *service =
240 connman_service_lookup_from_network(device->network);
243 __connman_service_disconnect(service);
245 connman_network_set_connected(device->network, FALSE);
248 err = device->driver->disable(device);
249 if (err == 0 || err == -EALREADY) {
250 connman_device_set_powered(device, FALSE);
254 if (err == -EINPROGRESS)
255 device->pending_timeout = g_timeout_add_seconds(4,
256 device_pending_reset, device);
261 static void probe_driver(struct connman_device_driver *driver)
265 DBG("driver %p name %s", driver, driver->name);
267 for (list = device_list; list != NULL; list = list->next) {
268 struct connman_device *device = list->data;
270 if (device->driver != NULL)
273 if (driver->type != device->type)
276 if (driver->probe(device) < 0)
279 device->driver = driver;
281 __connman_technology_add_device(device);
285 static void remove_device(struct connman_device *device)
287 DBG("device %p", device);
289 __connman_device_disable(device);
291 __connman_technology_remove_device(device);
293 if (device->driver->remove)
294 device->driver->remove(device);
296 device->driver = NULL;
299 static void remove_driver(struct connman_device_driver *driver)
303 DBG("driver %p name %s", driver, driver->name);
305 for (list = device_list; list != NULL; list = list->next) {
306 struct connman_device *device = list->data;
308 if (device->driver == driver)
309 remove_device(device);
313 connman_bool_t __connman_device_has_driver(struct connman_device *device)
315 if (device == NULL || device->driver == NULL)
321 static GSList *driver_list = NULL;
323 static gint compare_priority(gconstpointer a, gconstpointer b)
325 const struct connman_device_driver *driver1 = a;
326 const struct connman_device_driver *driver2 = b;
328 return driver2->priority - driver1->priority;
332 * connman_device_driver_register:
333 * @driver: device driver definition
335 * Register a new device driver
337 * Returns: %0 on success
339 int connman_device_driver_register(struct connman_device_driver *driver)
341 DBG("driver %p name %s", driver, driver->name);
343 driver_list = g_slist_insert_sorted(driver_list, driver,
345 probe_driver(driver);
351 * connman_device_driver_unregister:
352 * @driver: device driver definition
354 * Remove a previously registered device driver
356 void connman_device_driver_unregister(struct connman_device_driver *driver)
358 DBG("driver %p name %s", driver, driver->name);
360 driver_list = g_slist_remove(driver_list, driver);
362 remove_driver(driver);
365 static void free_network(gpointer data)
367 struct connman_network *network = data;
369 DBG("network %p", network);
371 __connman_network_set_device(network, NULL);
373 connman_network_unref(network);
376 static void device_destruct(struct connman_device *device)
378 DBG("device %p name %s", device, device->name);
380 clear_pending_trigger(device);
382 g_free(device->ident);
383 g_free(device->node);
384 g_free(device->name);
385 g_free(device->address);
386 g_free(device->interface);
387 g_free(device->path);
388 g_free(device->devname);
390 g_free(device->last_network);
392 g_hash_table_destroy(device->networks);
393 device->networks = NULL;
399 * connman_device_create:
400 * @node: device node name (for example an address)
403 * Allocate a new device of given #type and assign the #node name to it.
405 * Returns: a newly-allocated #connman_device structure
407 struct connman_device *connman_device_create(const char *node,
408 enum connman_device_type type)
410 struct connman_device *device;
412 DBG("node %s type %d", node, type);
414 device = g_try_new0(struct connman_device, 1);
418 DBG("device %p", device);
420 device->refcount = 1;
423 device->name = g_strdup(type2description(device->type));
425 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
426 g_free, free_network);
428 device_list = g_slist_prepend(device_list, device);
434 * connman_device_ref:
435 * @device: device structure
437 * Increase reference counter of device
439 struct connman_device *connman_device_ref_debug(struct connman_device *device,
440 const char *file, int line, const char *caller)
442 DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
445 __sync_fetch_and_add(&device->refcount, 1);
451 * connman_device_unref:
452 * @device: device structure
454 * Decrease reference counter of device
456 void connman_device_unref_debug(struct connman_device *device,
457 const char *file, int line, const char *caller)
459 DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
462 if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
465 if (device->driver) {
466 device->driver->remove(device);
467 device->driver = NULL;
470 device_list = g_slist_remove(device_list, device);
472 device_destruct(device);
475 const char *__connman_device_get_type(struct connman_device *device)
477 return type2string(device->type);
481 * connman_device_get_type:
482 * @device: device structure
486 enum connman_device_type connman_device_get_type(struct connman_device *device)
492 * connman_device_set_index:
493 * @device: device structure
494 * @index: index number
496 * Set index number of device
498 void connman_device_set_index(struct connman_device *device, int index)
500 device->index = index;
504 * connman_device_get_index:
505 * @device: device structure
507 * Get index number of device
509 int connman_device_get_index(struct connman_device *device)
511 return device->index;
515 * connman_device_set_interface:
516 * @device: device structure
517 * @interface: interface name
519 * Set interface name of device
521 void connman_device_set_interface(struct connman_device *device,
522 const char *interface)
524 g_free(device->devname);
525 device->devname = g_strdup(interface);
527 g_free(device->interface);
528 device->interface = g_strdup(interface);
530 if (device->name == NULL) {
531 const char *str = type2description(device->type);
532 if (str != NULL && device->interface != NULL)
533 device->name = g_strdup_printf("%s (%s)", str,
539 * connman_device_set_ident:
540 * @device: device structure
541 * @ident: unique identifier
543 * Set unique identifier of device
545 void connman_device_set_ident(struct connman_device *device,
548 g_free(device->ident);
549 device->ident = g_strdup(ident);
552 const char *connman_device_get_ident(struct connman_device *device)
554 return device->ident;
558 * connman_device_set_powered:
559 * @device: device structure
560 * @powered: powered state
562 * Change power state of device
564 int connman_device_set_powered(struct connman_device *device,
565 connman_bool_t powered)
567 enum connman_service_type type;
569 DBG("driver %p powered %d", device, powered);
571 if (device->powered == powered)
574 clear_pending_trigger(device);
576 device->powered_pending = PENDING_NONE;
578 device->powered = powered;
580 type = __connman_device_get_service_type(device);
582 if (device->powered == FALSE) {
583 __connman_technology_disabled(type);
587 __connman_technology_enabled(type);
589 connman_device_set_disconnected(device, FALSE);
590 device->scanning = FALSE;
592 if (device->driver && device->driver->scan)
593 device->driver->scan(device, NULL, 0, NULL, NULL, NULL);
598 connman_bool_t connman_device_get_powered(struct connman_device *device)
600 return device->powered;
603 static int device_scan(struct connman_device *device)
605 if (!device->driver || !device->driver->scan)
608 if (device->powered == FALSE)
611 return device->driver->scan(device, NULL, 0, NULL, NULL, NULL);
614 int __connman_device_disconnect(struct connman_device *device)
619 DBG("device %p", device);
621 connman_device_set_disconnected(device, TRUE);
623 g_hash_table_iter_init(&iter, device->networks);
625 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
626 struct connman_network *network = value;
628 if (connman_network_get_connecting(network) == TRUE) {
630 * Skip network in the process of connecting.
631 * This is a workaround for WiFi networks serviced
632 * by the supplicant plugin that hold a reference
633 * to the network. If we disconnect the network
634 * here then the referenced object will not be
635 * registered and usage (like launching DHCP client)
636 * will fail. There is nothing to be gained by
637 * removing the network here anyway.
639 connman_warn("Skipping disconnect of %s, network is connecting.",
640 connman_network_get_identifier(network));
644 __connman_network_disconnect(network);
650 static void mark_network_available(gpointer key, gpointer value,
653 struct connman_network *network = value;
655 connman_network_set_available(network, TRUE);
658 static void mark_network_unavailable(gpointer key, gpointer value,
661 struct connman_network *network = value;
663 if (connman_network_get_connected(network) == TRUE ||
664 connman_network_get_connecting(network) == TRUE)
667 connman_network_set_available(network, FALSE);
670 static gboolean remove_unavailable_network(gpointer key, gpointer value,
673 struct connman_network *network = value;
675 if (connman_network_get_connected(network) == TRUE)
678 if (connman_network_get_available(network) == TRUE)
684 void __connman_device_cleanup_networks(struct connman_device *device)
686 g_hash_table_foreach_remove(device->networks,
687 remove_unavailable_network, NULL);
690 connman_bool_t connman_device_get_scanning(struct connman_device *device)
692 return device->scanning;
695 void connman_device_reset_scanning(struct connman_device *device)
697 g_hash_table_foreach(device->networks,
698 mark_network_available, NULL);
702 * connman_device_set_scanning:
703 * @device: device structure
704 * @scanning: scanning state
706 * Change scanning state of device
708 int connman_device_set_scanning(struct connman_device *device,
709 connman_bool_t scanning)
711 DBG("device %p scanning %d", device, scanning);
713 if (!device->driver || !device->driver->scan)
716 if (device->scanning == scanning)
719 device->scanning = scanning;
721 if (scanning == TRUE) {
722 __connman_technology_scan_started(device);
724 g_hash_table_foreach(device->networks,
725 mark_network_unavailable, NULL);
730 __connman_device_cleanup_networks(device);
732 __connman_technology_scan_stopped(device);
734 __connman_service_auto_connect();
740 * connman_device_set_disconnected:
741 * @device: device structure
742 * @disconnected: disconnected state
744 * Change disconnected state of device (only for device with networks)
746 int connman_device_set_disconnected(struct connman_device *device,
747 connman_bool_t disconnected)
749 DBG("device %p disconnected %d", device, disconnected);
751 if (device->disconnected == disconnected)
754 device->disconnected = disconnected;
760 * connman_device_get_disconnected:
761 * @device: device structure
763 * Get device disconnected state
765 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
767 return device->disconnected;
771 * connman_device_set_string:
772 * @device: device structure
773 * @key: unique identifier
774 * @value: string value
776 * Set string value for specific key
778 int connman_device_set_string(struct connman_device *device,
779 const char *key, const char *value)
781 DBG("device %p key %s value %s", device, key, value);
783 if (g_str_equal(key, "Address") == TRUE) {
784 g_free(device->address);
785 device->address = g_strdup(value);
786 } else if (g_str_equal(key, "Name") == TRUE) {
787 g_free(device->name);
788 device->name = g_strdup(value);
789 } else if (g_str_equal(key, "Node") == TRUE) {
790 g_free(device->node);
791 device->node = g_strdup(value);
792 } else if (g_str_equal(key, "Path") == TRUE) {
793 g_free(device->path);
794 device->path = g_strdup(value);
803 * connman_device_get_string:
804 * @device: device structure
805 * @key: unique identifier
807 * Get string value for specific key
809 const char *connman_device_get_string(struct connman_device *device,
812 DBG("device %p key %s", device, key);
814 if (g_str_equal(key, "Address") == TRUE)
815 return device->address;
816 else if (g_str_equal(key, "Name") == TRUE)
818 else if (g_str_equal(key, "Node") == TRUE)
820 else if (g_str_equal(key, "Interface") == TRUE)
821 return device->interface;
822 else if (g_str_equal(key, "Path") == TRUE)
829 * connman_device_add_network:
830 * @device: device structure
831 * @network: network structure
833 * Add new network to the device
835 int connman_device_add_network(struct connman_device *device,
836 struct connman_network *network)
838 const char *identifier = connman_network_get_identifier(network);
840 DBG("device %p network %p", device, network);
842 if (identifier == NULL)
845 connman_network_ref(network);
847 __connman_network_set_device(network, device);
849 g_hash_table_replace(device->networks, g_strdup(identifier),
856 * connman_device_get_network:
857 * @device: device structure
858 * @identifier: network identifier
860 * Get network for given identifier
862 struct connman_network *connman_device_get_network(struct connman_device *device,
863 const char *identifier)
865 DBG("device %p identifier %s", device, identifier);
867 return g_hash_table_lookup(device->networks, identifier);
871 * connman_device_remove_network:
872 * @device: device structure
873 * @identifier: network identifier
875 * Remove network for given identifier
877 int connman_device_remove_network(struct connman_device *device,
878 struct connman_network *network)
880 const char *identifier;
882 DBG("device %p network %p", device, network);
887 identifier = connman_network_get_identifier(network);
888 g_hash_table_remove(device->networks, identifier);
893 void connman_device_remove_all_networks(struct connman_device *device)
895 g_hash_table_remove_all(device->networks);
898 void __connman_device_set_network(struct connman_device *device,
899 struct connman_network *network)
906 if (device->network == network)
909 if (network != NULL) {
910 name = connman_network_get_string(network, "Name");
911 g_free(device->last_network);
912 device->last_network = g_strdup(name);
914 device->network = network;
916 g_free(device->last_network);
917 device->last_network = NULL;
919 device->network = NULL;
923 void __connman_device_set_reconnect(struct connman_device *device,
924 connman_bool_t reconnect)
926 device->reconnect = reconnect;
929 connman_bool_t __connman_device_get_reconnect(
930 struct connman_device *device)
932 return device->reconnect;
935 static gboolean match_driver(struct connman_device *device,
936 struct connman_device_driver *driver)
938 if (device->type == driver->type ||
939 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
946 * connman_device_register:
947 * @device: device structure
949 * Register device with the system
951 int connman_device_register(struct connman_device *device)
955 DBG("device %p name %s", device, device->name);
957 if (device->driver != NULL)
960 for (list = driver_list; list; list = list->next) {
961 struct connman_device_driver *driver = list->data;
963 if (match_driver(device, driver) == FALSE)
966 DBG("driver %p name %s", driver, driver->name);
968 if (driver->probe(device) == 0) {
969 device->driver = driver;
974 if (device->driver == NULL)
977 return __connman_technology_add_device(device);
981 * connman_device_unregister:
982 * @device: device structure
984 * Unregister device with the system
986 void connman_device_unregister(struct connman_device *device)
988 DBG("device %p name %s", device, device->name);
990 if (device->driver == NULL)
993 remove_device(device);
997 * connman_device_get_data:
998 * @device: device structure
1000 * Get private device data pointer
1002 void *connman_device_get_data(struct connman_device *device)
1004 return device->driver_data;
1008 * connman_device_set_data:
1009 * @device: device structure
1010 * @data: data pointer
1012 * Set private device data pointer
1014 void connman_device_set_data(struct connman_device *device, void *data)
1016 device->driver_data = data;
1019 struct connman_device *__connman_device_find_device(
1020 enum connman_service_type type)
1024 for (list = device_list; list != NULL; list = list->next) {
1025 struct connman_device *device = list->data;
1026 enum connman_service_type service_type =
1027 __connman_device_get_service_type(device);
1029 if (service_type != type)
1039 * connman_device_set_regdom
1040 * @device: device structure
1041 * @alpha2: string representing regulatory domain
1043 * Set regulatory domain on device basis
1045 int connman_device_set_regdom(struct connman_device *device,
1048 if (device->driver == NULL || device->driver->set_regdom == NULL)
1051 if (device->powered == FALSE)
1054 return device->driver->set_regdom(device, alpha2);
1058 * connman_device_regdom_notify
1059 * @device: device structure
1060 * @alpha2: string representing regulatory domain
1062 * Notify on setting regulatory domain on device basis
1064 void connman_device_regdom_notify(struct connman_device *device,
1065 int result, const char *alpha2)
1067 __connman_technology_notify_regdom_by_device(device, result, alpha2);
1070 int __connman_device_request_scan(enum connman_service_type type)
1072 connman_bool_t success = FALSE;
1073 int last_err = -ENOSYS;
1078 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1079 case CONNMAN_SERVICE_TYPE_SYSTEM:
1080 case CONNMAN_SERVICE_TYPE_ETHERNET:
1081 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1082 case CONNMAN_SERVICE_TYPE_CELLULAR:
1083 case CONNMAN_SERVICE_TYPE_GPS:
1084 case CONNMAN_SERVICE_TYPE_VPN:
1085 case CONNMAN_SERVICE_TYPE_GADGET:
1087 case CONNMAN_SERVICE_TYPE_WIFI:
1091 for (list = device_list; list != NULL; list = list->next) {
1092 struct connman_device *device = list->data;
1093 enum connman_service_type service_type =
1094 __connman_device_get_service_type(device);
1096 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1097 service_type != type) {
1101 err = device_scan(device);
1102 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1106 DBG("device %p err %d", device, err);
1110 if (success == TRUE)
1116 int __connman_device_request_hidden_scan(struct connman_device *device,
1117 const char *ssid, unsigned int ssid_len,
1118 const char *identity, const char *passphrase,
1121 DBG("device %p", device);
1123 if (device == NULL || device->driver == NULL ||
1124 device->driver->scan == NULL)
1127 if (device->scanning == TRUE)
1130 return device->driver->scan(device, ssid, ssid_len,
1131 identity, passphrase, user_data);
1134 static char *index2ident(int index, const char *prefix)
1137 struct ether_addr eth;
1144 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1148 memset(&ifr, 0, sizeof(ifr));
1149 ifr.ifr_ifindex = index;
1151 err = ioctl(sk, SIOCGIFNAME, &ifr);
1154 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1161 len = prefix ? strlen(prefix) + 18 : 18;
1163 str = g_malloc(len);
1167 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1168 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
1169 prefix ? prefix : "",
1170 eth.ether_addr_octet[0],
1171 eth.ether_addr_octet[1],
1172 eth.ether_addr_octet[2],
1173 eth.ether_addr_octet[3],
1174 eth.ether_addr_octet[4],
1175 eth.ether_addr_octet[5]);
1180 static char *index2addr(int index)
1183 struct ether_addr eth;
1190 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1194 memset(&ifr, 0, sizeof(ifr));
1195 ifr.ifr_ifindex = index;
1197 err = ioctl(sk, SIOCGIFNAME, &ifr);
1200 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1211 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1212 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
1213 eth.ether_addr_octet[0],
1214 eth.ether_addr_octet[1],
1215 eth.ether_addr_octet[2],
1216 eth.ether_addr_octet[3],
1217 eth.ether_addr_octet[4],
1218 eth.ether_addr_octet[5]);
1223 struct connman_device *connman_device_create_from_index(int index)
1225 enum connman_device_type type;
1226 struct connman_device *device;
1227 char *devname, *ident = NULL;
1228 char *addr = NULL, *name = NULL;
1233 devname = connman_inet_ifname(index);
1234 if (devname == NULL)
1237 if (__connman_device_isfiltered(devname) == TRUE) {
1238 connman_info("Ignoring interface %s (filtered)", devname);
1243 type = __connman_rtnl_get_device_type(index);
1246 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1247 connman_info("Ignoring interface %s (type unknown)", devname);
1250 case CONNMAN_DEVICE_TYPE_ETHERNET:
1251 case CONNMAN_DEVICE_TYPE_GADGET:
1252 case CONNMAN_DEVICE_TYPE_WIFI:
1253 name = index2ident(index, "");
1254 addr = index2addr(index);
1256 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1257 case CONNMAN_DEVICE_TYPE_CELLULAR:
1258 case CONNMAN_DEVICE_TYPE_GPS:
1259 case CONNMAN_DEVICE_TYPE_VENDOR:
1260 name = g_strdup(devname);
1264 device = connman_device_create(name, type);
1269 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1270 case CONNMAN_DEVICE_TYPE_VENDOR:
1271 case CONNMAN_DEVICE_TYPE_GPS:
1273 case CONNMAN_DEVICE_TYPE_ETHERNET:
1274 case CONNMAN_DEVICE_TYPE_GADGET:
1275 ident = index2ident(index, NULL);
1277 case CONNMAN_DEVICE_TYPE_WIFI:
1278 ident = index2ident(index, NULL);
1280 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1282 case CONNMAN_DEVICE_TYPE_CELLULAR:
1283 ident = index2ident(index, NULL);
1287 connman_device_set_index(device, index);
1288 connman_device_set_interface(device, devname);
1290 if (ident != NULL) {
1291 connman_device_set_ident(device, ident);
1295 connman_device_set_string(device, "Address", addr);
1305 connman_bool_t __connman_device_isfiltered(const char *devname)
1308 char **blacklisted_interfaces;
1311 if (device_filter == NULL)
1314 for (pattern = device_filter, match = FALSE; *pattern; pattern++) {
1315 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1321 if (match == FALSE) {
1322 DBG("ignoring device %s (match)", devname);
1327 if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1328 DBG("ignoring dummy networking devices");
1332 if (nodevice_filter == NULL)
1335 for (pattern = nodevice_filter; *pattern; pattern++) {
1336 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1337 DBG("ignoring device %s (no match)", devname);
1343 blacklisted_interfaces =
1344 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1345 if (blacklisted_interfaces == NULL)
1348 for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1349 if (g_str_has_prefix(devname, *pattern) == TRUE) {
1350 DBG("ignoring device %s (blacklist)", devname);
1358 static void cleanup_devices(void)
1361 * Check what interfaces are currently up and if connman is
1362 * suppose to handle the interface, then cleanup the mess
1363 * related to that interface. There might be weird routes etc
1364 * that are related to that interface and that might confuse
1365 * connmand. So in this case we just turn the interface down
1366 * so that kernel removes routes/addresses automatically and
1367 * then proceed the startup.
1369 * Note that this cleanup must be done before rtnl/detect code
1370 * has activated interface watches.
1376 interfaces = __connman_inet_get_running_interfaces();
1378 if (interfaces == NULL)
1381 for (i = 0; interfaces[i] != NULL; i++) {
1382 connman_bool_t filtered;
1385 filtered = __connman_device_isfiltered(interfaces[i]);
1386 if (filtered == TRUE)
1389 index = connman_inet_ifindex(interfaces[i]);
1393 DBG("cleaning up %s index %d", interfaces[i], index);
1395 connman_inet_ifdown(index);
1398 * ConnMan will turn the interface UP automatically so
1399 * no need to do it here.
1403 g_strfreev(interfaces);
1406 int __connman_device_init(const char *device, const char *nodevice)
1411 device_filter = g_strsplit(device, ",", -1);
1413 if (nodevice != NULL)
1414 nodevice_filter = g_strsplit(nodevice, ",", -1);
1421 void __connman_device_cleanup(void)
1425 g_strfreev(nodevice_filter);
1426 g_strfreev(device_filter);