5 * Copyright (C) 2007-2014 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
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(
131 struct connman_device *device)
133 enum connman_device_type type = connman_device_get_type(device);
136 case CONNMAN_DEVICE_TYPE_UNKNOWN:
137 case CONNMAN_DEVICE_TYPE_VENDOR:
138 case CONNMAN_DEVICE_TYPE_GPS:
140 case CONNMAN_DEVICE_TYPE_ETHERNET:
141 return CONNMAN_SERVICE_TYPE_ETHERNET;
142 case CONNMAN_DEVICE_TYPE_WIFI:
143 return CONNMAN_SERVICE_TYPE_WIFI;
144 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
145 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
146 case CONNMAN_DEVICE_TYPE_CELLULAR:
147 return CONNMAN_SERVICE_TYPE_CELLULAR;
148 case CONNMAN_DEVICE_TYPE_GADGET:
149 return CONNMAN_SERVICE_TYPE_GADGET;
153 return CONNMAN_SERVICE_TYPE_UNKNOWN;
156 static gboolean device_pending_reset(gpointer user_data)
158 struct connman_device *device = user_data;
160 DBG("device %p", device);
162 /* Power request timedout, reset power pending state. */
163 device->pending_timeout = 0;
164 device->powered_pending = PENDING_NONE;
169 int __connman_device_enable(struct connman_device *device)
173 DBG("device %p", device);
175 if (!device->driver || !device->driver->enable)
178 /* There is an ongoing power disable request. */
179 if (device->powered_pending == PENDING_DISABLE)
182 if (device->powered_pending == PENDING_ENABLE)
185 if (device->powered_pending == PENDING_NONE && device->powered)
188 if (device->index > 0) {
189 err = connman_inet_ifup(device->index);
190 if (err < 0 && err != -EALREADY)
194 device->powered_pending = PENDING_ENABLE;
196 err = device->driver->enable(device);
198 * device gets enabled right away.
199 * Invoke the callback
202 connman_device_set_powered(device, true);
206 if (err == -EALREADY) {
207 /* If device is already powered, but connman is not updated */
208 connman_device_set_powered(device, true);
212 * if err == -EINPROGRESS, then the DBus call to the respective daemon
213 * was successful. We set a 4 sec timeout so if the daemon never
214 * returns a reply, we would reset the pending request.
216 if (err == -EINPROGRESS)
217 device->pending_timeout = g_timeout_add_seconds(4,
218 device_pending_reset, device);
223 int __connman_device_disable(struct connman_device *device)
227 DBG("device %p", device);
229 /* Ongoing power enable request */
230 if (device->powered_pending == PENDING_ENABLE)
233 if (device->powered_pending == PENDING_DISABLE)
236 if (device->powered_pending == PENDING_NONE && !device->powered)
239 device->powered_pending = PENDING_DISABLE;
241 if (device->network) {
242 struct connman_service *service =
243 connman_service_lookup_from_network(device->network);
246 __connman_service_disconnect(service);
248 connman_network_set_connected(device->network, false);
251 if (!device->driver || !device->driver->disable)
254 err = device->driver->disable(device);
255 if (err == 0 || err == -EALREADY) {
256 connman_device_set_powered(device, false);
260 if (err == -EINPROGRESS)
261 device->pending_timeout = g_timeout_add_seconds(4,
262 device_pending_reset, device);
267 static void probe_driver(struct connman_device_driver *driver)
271 DBG("driver %p name %s", driver, driver->name);
273 for (list = device_list; list; list = list->next) {
274 struct connman_device *device = list->data;
279 if (driver->type != device->type)
282 if (driver->probe(device) < 0)
285 device->driver = driver;
287 __connman_technology_add_device(device);
291 static void remove_device(struct connman_device *device)
293 DBG("device %p", device);
295 __connman_device_disable(device);
297 __connman_technology_remove_device(device);
299 if (device->driver->remove)
300 device->driver->remove(device);
302 device->driver = NULL;
305 static void remove_driver(struct connman_device_driver *driver)
309 DBG("driver %p name %s", driver, driver->name);
311 for (list = device_list; list; list = list->next) {
312 struct connman_device *device = list->data;
314 if (device->driver == driver)
315 remove_device(device);
319 bool __connman_device_has_driver(struct connman_device *device)
321 if (!device || !device->driver)
327 static GSList *driver_list = NULL;
329 static gint compare_priority(gconstpointer a, gconstpointer b)
331 const struct connman_device_driver *driver1 = a;
332 const struct connman_device_driver *driver2 = b;
334 return driver2->priority - driver1->priority;
338 * connman_device_driver_register:
339 * @driver: device driver definition
341 * Register a new device driver
343 * Returns: %0 on success
345 int connman_device_driver_register(struct connman_device_driver *driver)
347 DBG("driver %p name %s", driver, driver->name);
349 driver_list = g_slist_insert_sorted(driver_list, driver,
351 probe_driver(driver);
357 * connman_device_driver_unregister:
358 * @driver: device driver definition
360 * Remove a previously registered device driver
362 void connman_device_driver_unregister(struct connman_device_driver *driver)
364 DBG("driver %p name %s", driver, driver->name);
366 driver_list = g_slist_remove(driver_list, driver);
368 remove_driver(driver);
371 static void free_network(gpointer data)
373 struct connman_network *network = data;
375 DBG("network %p", network);
377 __connman_network_set_device(network, NULL);
379 connman_network_unref(network);
382 static void device_destruct(struct connman_device *device)
384 DBG("device %p name %s", device, device->name);
386 clear_pending_trigger(device);
388 g_free(device->ident);
389 g_free(device->node);
390 g_free(device->name);
391 g_free(device->address);
392 g_free(device->interface);
393 g_free(device->path);
395 g_free(device->last_network);
397 g_hash_table_destroy(device->networks);
398 device->networks = NULL;
404 * connman_device_create:
405 * @node: device node name (for example an address)
408 * Allocate a new device of given #type and assign the #node name to it.
410 * Returns: a newly-allocated #connman_device structure
412 struct connman_device *connman_device_create(const char *node,
413 enum connman_device_type type)
415 struct connman_device *device;
417 DBG("node %s type %d", node, type);
419 device = g_try_new0(struct connman_device, 1);
423 DBG("device %p", device);
425 device->refcount = 1;
428 device->name = g_strdup(type2description(device->type));
430 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
431 g_free, free_network);
433 device_list = g_slist_prepend(device_list, device);
439 * connman_device_ref:
440 * @device: device structure
442 * Increase reference counter of device
444 struct connman_device *connman_device_ref_debug(struct connman_device *device,
445 const char *file, int line, const char *caller)
447 DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
450 __sync_fetch_and_add(&device->refcount, 1);
456 * connman_device_unref:
457 * @device: device structure
459 * Decrease reference counter of device
461 void connman_device_unref_debug(struct connman_device *device,
462 const char *file, int line, const char *caller)
464 DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
467 if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
470 if (device->driver) {
471 device->driver->remove(device);
472 device->driver = NULL;
475 device_list = g_slist_remove(device_list, device);
477 device_destruct(device);
480 const char *__connman_device_get_type(struct connman_device *device)
482 return type2string(device->type);
486 * connman_device_get_type:
487 * @device: device structure
491 enum connman_device_type connman_device_get_type(struct connman_device *device)
497 * connman_device_set_index:
498 * @device: device structure
499 * @index: index number
501 * Set index number of device
503 void connman_device_set_index(struct connman_device *device, int index)
505 device->index = index;
509 * connman_device_get_index:
510 * @device: device structure
512 * Get index number of device
514 int connman_device_get_index(struct connman_device *device)
516 return device->index;
520 * connman_device_set_interface:
521 * @device: device structure
522 * @interface: interface name
524 * Set interface name of device
526 void connman_device_set_interface(struct connman_device *device,
527 const char *interface)
529 g_free(device->interface);
530 device->interface = g_strdup(interface);
533 const char *str = type2description(device->type);
534 if (str && device->interface)
535 device->name = g_strdup_printf("%s (%s)", str,
541 * connman_device_set_ident:
542 * @device: device structure
543 * @ident: unique identifier
545 * Set unique identifier of device
547 void connman_device_set_ident(struct connman_device *device,
550 g_free(device->ident);
551 device->ident = g_strdup(ident);
554 const char *connman_device_get_ident(struct connman_device *device)
556 return device->ident;
560 * connman_device_set_powered:
561 * @device: device structure
562 * @powered: powered state
564 * Change power state of device
566 int connman_device_set_powered(struct connman_device *device,
569 enum connman_service_type type;
571 DBG("driver %p powered %d", device, powered);
573 if (device->powered == powered)
576 clear_pending_trigger(device);
578 device->powered_pending = PENDING_NONE;
580 device->powered = powered;
582 type = __connman_device_get_service_type(device);
584 if (!device->powered) {
585 __connman_technology_disabled(type);
589 __connman_technology_enabled(type);
591 connman_device_set_disconnected(device, false);
592 device->scanning = false;
594 if (device->driver && device->driver->scan)
595 device->driver->scan(CONNMAN_SERVICE_TYPE_UNKNOWN, device,
596 NULL, 0, NULL, NULL, NULL, NULL);
601 bool connman_device_get_powered(struct connman_device *device)
603 return device->powered;
606 static int device_scan(enum connman_service_type type,
607 struct connman_device *device)
609 if (!device->driver || !device->driver->scan)
612 if (!device->powered)
615 return device->driver->scan(type, device, NULL, 0,
616 NULL, NULL, NULL, NULL);
619 int __connman_device_disconnect(struct connman_device *device)
624 DBG("device %p", device);
626 connman_device_set_disconnected(device, true);
628 g_hash_table_iter_init(&iter, device->networks);
630 while (g_hash_table_iter_next(&iter, &key, &value)) {
631 struct connman_network *network = value;
633 if (connman_network_get_connecting(network)) {
635 * Skip network in the process of connecting.
636 * This is a workaround for WiFi networks serviced
637 * by the supplicant plugin that hold a reference
638 * to the network. If we disconnect the network
639 * here then the referenced object will not be
640 * registered and usage (like launching DHCP client)
641 * will fail. There is nothing to be gained by
642 * removing the network here anyway.
644 connman_warn("Skipping disconnect of %s, network is connecting.",
645 connman_network_get_identifier(network));
649 __connman_network_disconnect(network);
655 int connman_device_reconnect_service(struct connman_device *device)
657 DBG("device %p", device);
659 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
664 static void mark_network_available(gpointer key, gpointer value,
667 struct connman_network *network = value;
669 connman_network_set_available(network, true);
672 static void mark_network_unavailable(gpointer key, gpointer value,
675 struct connman_network *network = value;
677 if (connman_network_get_connected(network) ||
678 connman_network_get_connecting(network))
681 connman_network_set_available(network, false);
684 static gboolean remove_unavailable_network(gpointer key, gpointer value,
687 struct connman_network *network = value;
689 if (connman_network_get_connected(network))
692 if (connman_network_get_available(network))
698 void __connman_device_cleanup_networks(struct connman_device *device)
700 g_hash_table_foreach_remove(device->networks,
701 remove_unavailable_network, NULL);
704 bool connman_device_get_scanning(struct connman_device *device)
706 return device->scanning;
709 void connman_device_reset_scanning(struct connman_device *device)
711 g_hash_table_foreach(device->networks,
712 mark_network_available, NULL);
716 * connman_device_set_scanning:
717 * @device: device structure
718 * @scanning: scanning state
720 * Change scanning state of device
722 int connman_device_set_scanning(struct connman_device *device,
723 enum connman_service_type type, bool scanning)
725 DBG("device %p scanning %d", device, scanning);
727 if (!device->driver || !device->driver->scan)
730 if (device->scanning == scanning)
733 device->scanning = scanning;
736 __connman_technology_scan_started(device);
738 g_hash_table_foreach(device->networks,
739 mark_network_unavailable, NULL);
744 __connman_device_cleanup_networks(device);
746 __connman_technology_scan_stopped(device, type);
748 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
754 * connman_device_set_disconnected:
755 * @device: device structure
756 * @disconnected: disconnected state
758 * Change disconnected state of device (only for device with networks)
760 int connman_device_set_disconnected(struct connman_device *device,
763 DBG("device %p disconnected %d", device, disconnected);
765 if (device->disconnected == disconnected)
768 device->disconnected = disconnected;
774 * connman_device_get_disconnected:
775 * @device: device structure
777 * Get device disconnected state
779 bool connman_device_get_disconnected(struct connman_device *device)
781 return device->disconnected;
785 * connman_device_set_string:
786 * @device: device structure
787 * @key: unique identifier
788 * @value: string value
790 * Set string value for specific key
792 int connman_device_set_string(struct connman_device *device,
793 const char *key, const char *value)
795 DBG("device %p key %s value %s", device, key, value);
797 if (g_str_equal(key, "Address")) {
798 g_free(device->address);
799 device->address = g_strdup(value);
800 } else if (g_str_equal(key, "Name")) {
801 g_free(device->name);
802 device->name = g_strdup(value);
803 } else if (g_str_equal(key, "Node")) {
804 g_free(device->node);
805 device->node = g_strdup(value);
806 } else if (g_str_equal(key, "Path")) {
807 g_free(device->path);
808 device->path = g_strdup(value);
817 * connman_device_get_string:
818 * @device: device structure
819 * @key: unique identifier
821 * Get string value for specific key
823 const char *connman_device_get_string(struct connman_device *device,
826 DBG("device %p key %s", device, key);
828 if (g_str_equal(key, "Address"))
829 return device->address;
830 else if (g_str_equal(key, "Name"))
832 else if (g_str_equal(key, "Node"))
834 else if (g_str_equal(key, "Interface"))
835 return device->interface;
836 else if (g_str_equal(key, "Path"))
843 * connman_device_add_network:
844 * @device: device structure
845 * @network: network structure
847 * Add new network to the device
849 int connman_device_add_network(struct connman_device *device,
850 struct connman_network *network)
852 const char *identifier = connman_network_get_identifier(network);
854 DBG("device %p network %p", device, network);
859 connman_network_ref(network);
861 __connman_network_set_device(network, device);
863 g_hash_table_replace(device->networks, g_strdup(identifier),
870 * connman_device_get_network:
871 * @device: device structure
872 * @identifier: network identifier
874 * Get network for given identifier
876 struct connman_network *connman_device_get_network(struct connman_device *device,
877 const char *identifier)
879 DBG("device %p identifier %s", device, identifier);
881 return g_hash_table_lookup(device->networks, identifier);
885 * connman_device_remove_network:
886 * @device: device structure
887 * @identifier: network identifier
889 * Remove network for given identifier
891 int connman_device_remove_network(struct connman_device *device,
892 struct connman_network *network)
894 const char *identifier;
896 DBG("device %p network %p", device, network);
901 identifier = connman_network_get_identifier(network);
902 g_hash_table_remove(device->networks, identifier);
907 void __connman_device_set_network(struct connman_device *device,
908 struct connman_network *network)
915 if (device->network == network)
919 name = connman_network_get_string(network, "Name");
920 g_free(device->last_network);
921 device->last_network = g_strdup(name);
923 device->network = network;
925 g_free(device->last_network);
926 device->last_network = NULL;
928 device->network = NULL;
932 static bool match_driver(struct connman_device *device,
933 struct connman_device_driver *driver)
935 if (device->type == driver->type ||
936 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
943 * connman_device_register:
944 * @device: device structure
946 * Register device with the system
948 int connman_device_register(struct connman_device *device)
952 DBG("device %p name %s", device, device->name);
957 for (list = driver_list; list; list = list->next) {
958 struct connman_device_driver *driver = list->data;
960 if (!match_driver(device, driver))
963 DBG("driver %p name %s", driver, driver->name);
965 if (driver->probe(device) == 0) {
966 device->driver = driver;
974 return __connman_technology_add_device(device);
978 * connman_device_unregister:
979 * @device: device structure
981 * Unregister device with the system
983 void connman_device_unregister(struct connman_device *device)
985 DBG("device %p name %s", device, device->name);
990 remove_device(device);
994 * connman_device_get_data:
995 * @device: device structure
997 * Get private device data pointer
999 void *connman_device_get_data(struct connman_device *device)
1001 return device->driver_data;
1005 * connman_device_set_data:
1006 * @device: device structure
1007 * @data: data pointer
1009 * Set private device data pointer
1011 void connman_device_set_data(struct connman_device *device, void *data)
1013 device->driver_data = data;
1016 struct connman_device *__connman_device_find_device(
1017 enum connman_service_type type)
1021 for (list = device_list; list; list = list->next) {
1022 struct connman_device *device = list->data;
1023 enum connman_service_type service_type =
1024 __connman_device_get_service_type(device);
1026 if (service_type != type)
1035 struct connman_device *connman_device_find_by_index(int index)
1039 for (list = device_list; list; list = list->next) {
1040 struct connman_device *device = list->data;
1041 if (device->index == index)
1049 * connman_device_set_regdom
1050 * @device: device structure
1051 * @alpha2: string representing regulatory domain
1053 * Set regulatory domain on device basis
1055 int connman_device_set_regdom(struct connman_device *device,
1058 if (!device->driver || !device->driver->set_regdom)
1061 if (!device->powered)
1064 return device->driver->set_regdom(device, alpha2);
1068 * connman_device_regdom_notify
1069 * @device: device structure
1070 * @alpha2: string representing regulatory domain
1072 * Notify on setting regulatory domain on device basis
1074 void connman_device_regdom_notify(struct connman_device *device,
1075 int result, const char *alpha2)
1077 __connman_technology_notify_regdom_by_device(device, result, alpha2);
1080 int __connman_device_request_scan(enum connman_service_type type)
1082 bool success = false;
1083 int last_err = -ENOSYS;
1088 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1089 case CONNMAN_SERVICE_TYPE_SYSTEM:
1090 case CONNMAN_SERVICE_TYPE_ETHERNET:
1091 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1092 case CONNMAN_SERVICE_TYPE_CELLULAR:
1093 case CONNMAN_SERVICE_TYPE_GPS:
1094 case CONNMAN_SERVICE_TYPE_VPN:
1095 case CONNMAN_SERVICE_TYPE_GADGET:
1097 case CONNMAN_SERVICE_TYPE_WIFI:
1098 case CONNMAN_SERVICE_TYPE_P2P:
1102 for (list = device_list; list; list = list->next) {
1103 struct connman_device *device = list->data;
1104 enum connman_service_type service_type =
1105 __connman_device_get_service_type(device);
1107 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1108 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1109 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1111 } else if (service_type != type)
1115 err = device_scan(type, device);
1116 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1120 DBG("device %p err %d", device, err);
1130 int __connman_device_request_hidden_scan(struct connman_device *device,
1131 const char *ssid, unsigned int ssid_len,
1132 const char *identity, const char *passphrase,
1133 const char *security, void *user_data)
1135 DBG("device %p", device);
1137 if (!device || !device->driver ||
1138 !device->driver->scan)
1141 return device->driver->scan(CONNMAN_SERVICE_TYPE_UNKNOWN,
1142 device, ssid, ssid_len, identity,
1143 passphrase, security, user_data);
1146 #if defined TIZEN_EXT
1147 char *index2ident(int index, const char *prefix)
1149 static char *index2ident(int index, const char *prefix)
1153 struct ether_addr eth;
1160 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1164 memset(&ifr, 0, sizeof(ifr));
1165 ifr.ifr_ifindex = index;
1167 err = ioctl(sk, SIOCGIFNAME, &ifr);
1170 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1177 len = prefix ? strlen(prefix) + 18 : 18;
1179 str = g_malloc(len);
1183 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1184 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
1185 prefix ? prefix : "",
1186 eth.ether_addr_octet[0],
1187 eth.ether_addr_octet[1],
1188 eth.ether_addr_octet[2],
1189 eth.ether_addr_octet[3],
1190 eth.ether_addr_octet[4],
1191 eth.ether_addr_octet[5]);
1196 #if defined TIZEN_EXT
1197 char *index2addr(int index)
1199 static char *index2addr(int index)
1203 struct ether_addr eth;
1210 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1214 memset(&ifr, 0, sizeof(ifr));
1215 ifr.ifr_ifindex = index;
1217 err = ioctl(sk, SIOCGIFNAME, &ifr);
1220 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1231 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1232 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
1233 eth.ether_addr_octet[0],
1234 eth.ether_addr_octet[1],
1235 eth.ether_addr_octet[2],
1236 eth.ether_addr_octet[3],
1237 eth.ether_addr_octet[4],
1238 eth.ether_addr_octet[5]);
1243 struct connman_device *connman_device_create_from_index(int index)
1245 enum connman_device_type type;
1246 struct connman_device *device;
1247 char *devname, *ident = NULL;
1248 char *addr = NULL, *name = NULL;
1253 devname = connman_inet_ifname(index);
1257 if (__connman_device_isfiltered(devname)) {
1258 connman_info("Ignoring interface %s (filtered)", devname);
1263 type = __connman_rtnl_get_device_type(index);
1266 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1267 connman_info("Ignoring interface %s (type unknown)", devname);
1270 case CONNMAN_DEVICE_TYPE_ETHERNET:
1271 case CONNMAN_DEVICE_TYPE_GADGET:
1272 case CONNMAN_DEVICE_TYPE_WIFI:
1273 name = index2ident(index, "");
1274 addr = index2addr(index);
1276 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1277 case CONNMAN_DEVICE_TYPE_CELLULAR:
1278 case CONNMAN_DEVICE_TYPE_GPS:
1279 case CONNMAN_DEVICE_TYPE_VENDOR:
1280 name = g_strdup(devname);
1284 device = connman_device_create(name, type);
1289 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1290 case CONNMAN_DEVICE_TYPE_VENDOR:
1291 case CONNMAN_DEVICE_TYPE_GPS:
1293 case CONNMAN_DEVICE_TYPE_ETHERNET:
1294 case CONNMAN_DEVICE_TYPE_GADGET:
1295 ident = index2ident(index, NULL);
1297 case CONNMAN_DEVICE_TYPE_WIFI:
1298 ident = index2ident(index, NULL);
1300 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1302 case CONNMAN_DEVICE_TYPE_CELLULAR:
1303 ident = index2ident(index, NULL);
1307 connman_device_set_index(device, index);
1308 connman_device_set_interface(device, devname);
1311 connman_device_set_ident(device, ident);
1315 connman_device_set_string(device, "Address", addr);
1325 bool __connman_device_isfiltered(const char *devname)
1328 char **blacklisted_interfaces;
1334 for (pattern = device_filter, match = false; *pattern; pattern++) {
1335 if (g_pattern_match_simple(*pattern, devname)) {
1342 DBG("ignoring device %s (match)", devname);
1347 if (g_pattern_match_simple("dummy*", devname)) {
1348 DBG("ignoring dummy networking devices");
1352 if (!nodevice_filter)
1355 for (pattern = nodevice_filter; *pattern; pattern++) {
1356 if (g_pattern_match_simple(*pattern, devname)) {
1357 DBG("ignoring device %s (no match)", devname);
1363 blacklisted_interfaces =
1364 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1365 if (!blacklisted_interfaces)
1368 for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1369 if (g_str_has_prefix(devname, *pattern)) {
1370 DBG("ignoring device %s (blacklist)", devname);
1378 static void cleanup_devices(void)
1381 * Check what interfaces are currently up and if connman is
1382 * suppose to handle the interface, then cleanup the mess
1383 * related to that interface. There might be weird routes etc
1384 * that are related to that interface and that might confuse
1385 * connmand. So in this case we just turn the interface down
1386 * so that kernel removes routes/addresses automatically and
1387 * then proceed the startup.
1389 * Note that this cleanup must be done before rtnl/detect code
1390 * has activated interface watches.
1396 interfaces = __connman_inet_get_running_interfaces();
1401 for (i = 0; interfaces[i]; i++) {
1404 struct sockaddr_in sin_addr, sin_mask;
1406 filtered = __connman_device_isfiltered(interfaces[i]);
1410 index = connman_inet_ifindex(interfaces[i]);
1414 if (!__connman_inet_get_address_netmask(index, &sin_addr,
1416 char *address = g_strdup(inet_ntoa(sin_addr.sin_addr));
1417 char *netmask = g_strdup(inet_ntoa(sin_mask.sin_addr));
1419 if (__connman_config_address_provisioned(address,
1421 DBG("Skip %s which is already provisioned "
1422 "with %s/%s", interfaces[i], address,
1433 DBG("cleaning up %s index %d", interfaces[i], index);
1435 connman_inet_ifdown(index);
1438 * ConnMan will turn the interface UP automatically so
1439 * no need to do it here.
1443 g_strfreev(interfaces);
1446 int __connman_device_init(const char *device, const char *nodevice)
1451 device_filter = g_strsplit(device, ",", -1);
1454 nodevice_filter = g_strsplit(nodevice, ",", -1);
1461 void __connman_device_cleanup(void)
1465 g_strfreev(nodevice_filter);
1466 g_strfreev(device_filter);