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
30 #include <sys/ioctl.h>
31 #include <net/ethernet.h>
36 static GSList *device_list = NULL;
37 static gchar **device_filter = NULL;
38 static gchar **nodevice_filter = NULL;
40 enum connman_pending_type {
46 struct connman_device {
48 enum connman_device_type type;
49 enum connman_pending_type powered_pending; /* Indicates a pending
50 enable/disable request */
51 connman_bool_t powered;
52 connman_bool_t scanning;
53 connman_bool_t disconnected;
54 connman_bool_t reconnect;
63 guint pending_timeout;
65 struct connman_device_driver *driver;
69 struct connman_network *network;
73 static void clear_pending_trigger(struct connman_device *device)
75 if (device->pending_timeout > 0) {
76 g_source_remove(device->pending_timeout);
77 device->pending_timeout = 0;
81 static const char *type2description(enum connman_device_type type)
84 case CONNMAN_DEVICE_TYPE_UNKNOWN:
85 case CONNMAN_DEVICE_TYPE_VENDOR:
87 case CONNMAN_DEVICE_TYPE_ETHERNET:
89 case CONNMAN_DEVICE_TYPE_WIFI:
91 case CONNMAN_DEVICE_TYPE_WIMAX:
93 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
95 case CONNMAN_DEVICE_TYPE_GPS:
97 case CONNMAN_DEVICE_TYPE_CELLULAR:
99 case CONNMAN_DEVICE_TYPE_GADGET:
107 static const char *type2string(enum connman_device_type type)
110 case CONNMAN_DEVICE_TYPE_UNKNOWN:
111 case CONNMAN_DEVICE_TYPE_VENDOR:
113 case CONNMAN_DEVICE_TYPE_ETHERNET:
115 case CONNMAN_DEVICE_TYPE_WIFI:
117 case CONNMAN_DEVICE_TYPE_WIMAX:
119 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
121 case CONNMAN_DEVICE_TYPE_GPS:
123 case CONNMAN_DEVICE_TYPE_CELLULAR:
125 case CONNMAN_DEVICE_TYPE_GADGET:
133 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
135 enum connman_device_type type = connman_device_get_type(device);
138 case CONNMAN_DEVICE_TYPE_UNKNOWN:
139 case CONNMAN_DEVICE_TYPE_VENDOR:
140 case CONNMAN_DEVICE_TYPE_GPS:
142 case CONNMAN_DEVICE_TYPE_ETHERNET:
143 return CONNMAN_SERVICE_TYPE_ETHERNET;
144 case CONNMAN_DEVICE_TYPE_WIFI:
145 return CONNMAN_SERVICE_TYPE_WIFI;
146 case CONNMAN_DEVICE_TYPE_WIMAX:
147 return CONNMAN_SERVICE_TYPE_WIMAX;
148 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
149 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
150 case CONNMAN_DEVICE_TYPE_CELLULAR:
151 return CONNMAN_SERVICE_TYPE_CELLULAR;
152 case CONNMAN_DEVICE_TYPE_GADGET:
153 return CONNMAN_SERVICE_TYPE_GADGET;
157 return CONNMAN_SERVICE_TYPE_UNKNOWN;
160 static gboolean device_pending_reset(gpointer user_data)
162 struct connman_device *device = user_data;
164 DBG("device %p", device);
166 /* Power request timedout, reset power pending state. */
167 device->pending_timeout = 0;
168 device->powered_pending = PENDING_NONE;
173 int __connman_device_enable(struct connman_device *device)
177 DBG("device %p", device);
179 if (!device->driver || !device->driver->enable)
182 /* There is an ongoing power disable request. */
183 if (device->powered_pending == PENDING_DISABLE)
186 if (device->powered_pending == PENDING_ENABLE)
189 if (device->powered_pending == PENDING_NONE && device->powered == TRUE)
192 device->powered_pending = PENDING_ENABLE;
194 err = device->driver->enable(device);
196 * device gets enabled right away.
197 * Invoke the callback
200 connman_device_set_powered(device, TRUE);
204 if (err == -EALREADY) {
205 /* If device is already powered, but connman is not updated */
206 connman_device_set_powered(device, TRUE);
210 * if err == -EINPROGRESS, then the DBus call to the respective daemon
211 * was successful. We set a 4 sec timeout so if the daemon never
212 * returns a reply, we would reset the pending request.
214 if (err == -EINPROGRESS)
215 device->pending_timeout = g_timeout_add_seconds(4,
216 device_pending_reset, device);
221 int __connman_device_disable(struct connman_device *device)
225 DBG("device %p", device);
227 if (!device->driver || !device->driver->disable)
230 /* Ongoing power enable request */
231 if (device->powered_pending == PENDING_ENABLE)
234 if (device->powered_pending == PENDING_DISABLE)
237 if (device->powered_pending == PENDING_NONE && device->powered == FALSE)
240 device->powered_pending = PENDING_DISABLE;
241 device->reconnect = FALSE;
243 if (device->network) {
244 struct connman_service *service =
245 connman_service_lookup_from_network(device->network);
248 __connman_service_disconnect(service);
250 connman_network_set_connected(device->network, FALSE);
253 err = device->driver->disable(device);
254 if (err == 0 || err == -EALREADY) {
255 connman_device_set_powered(device, FALSE);
259 if (err == -EINPROGRESS)
260 device->pending_timeout = g_timeout_add_seconds(4,
261 device_pending_reset, device);
266 static void probe_driver(struct connman_device_driver *driver)
270 DBG("driver %p name %s", driver, driver->name);
272 for (list = device_list; list != NULL; list = list->next) {
273 struct connman_device *device = list->data;
275 if (device->driver != NULL)
278 if (driver->type != device->type)
281 if (driver->probe(device) < 0)
284 device->driver = driver;
286 __connman_technology_add_device(device);
290 static void remove_device(struct connman_device *device)
292 DBG("device %p", device);
294 __connman_device_disable(device);
296 __connman_technology_remove_device(device);
298 if (device->driver->remove)
299 device->driver->remove(device);
301 device->driver = NULL;
304 static void remove_driver(struct connman_device_driver *driver)
308 DBG("driver %p name %s", driver, driver->name);
310 for (list = device_list; list != NULL; list = list->next) {
311 struct connman_device *device = list->data;
313 if (device->driver == driver)
314 remove_device(device);
318 connman_bool_t __connman_device_has_driver(struct connman_device *device)
320 if (device == NULL || device->driver == NULL)
326 static GSList *driver_list = NULL;
328 static gint compare_priority(gconstpointer a, gconstpointer b)
330 const struct connman_device_driver *driver1 = a;
331 const struct connman_device_driver *driver2 = b;
333 return driver2->priority - driver1->priority;
337 * connman_device_driver_register:
338 * @driver: device driver definition
340 * Register a new device driver
342 * Returns: %0 on success
344 int connman_device_driver_register(struct connman_device_driver *driver)
346 DBG("driver %p name %s", driver, driver->name);
348 driver_list = g_slist_insert_sorted(driver_list, driver,
350 probe_driver(driver);
356 * connman_device_driver_unregister:
357 * @driver: device driver definition
359 * Remove a previously registered device driver
361 void connman_device_driver_unregister(struct connman_device_driver *driver)
363 DBG("driver %p name %s", driver, driver->name);
365 driver_list = g_slist_remove(driver_list, driver);
367 remove_driver(driver);
370 static void free_network(gpointer data)
372 struct connman_network *network = data;
374 DBG("network %p", network);
376 __connman_network_set_device(network, NULL);
378 connman_network_unref(network);
381 static void device_destruct(struct connman_device *device)
383 DBG("device %p name %s", device, device->name);
385 clear_pending_trigger(device);
387 g_free(device->ident);
388 g_free(device->node);
389 g_free(device->name);
390 g_free(device->address);
391 g_free(device->interface);
392 g_free(device->path);
393 g_free(device->devname);
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->devname);
530 device->devname = g_strdup(interface);
532 g_free(device->interface);
533 device->interface = g_strdup(interface);
535 if (device->name == NULL) {
536 const char *str = type2description(device->type);
537 if (str != NULL && device->interface != NULL)
538 device->name = g_strdup_printf("%s (%s)", str,
544 * connman_device_set_ident:
545 * @device: device structure
546 * @ident: unique identifier
548 * Set unique identifier of device
550 void connman_device_set_ident(struct connman_device *device,
553 g_free(device->ident);
554 device->ident = g_strdup(ident);
557 const char *connman_device_get_ident(struct connman_device *device)
559 return device->ident;
563 * connman_device_set_powered:
564 * @device: device structure
565 * @powered: powered state
567 * Change power state of device
569 int connman_device_set_powered(struct connman_device *device,
570 connman_bool_t powered)
572 enum connman_service_type type;
574 DBG("driver %p powered %d", device, powered);
576 if (device->powered == powered)
579 clear_pending_trigger(device);
581 device->powered_pending = PENDING_NONE;
583 device->powered = powered;
585 type = __connman_device_get_service_type(device);
587 if (device->powered == FALSE) {
588 __connman_technology_disabled(type);
592 __connman_technology_enabled(type);
594 connman_device_set_disconnected(device, FALSE);
595 device->scanning = FALSE;
597 if (device->driver && device->driver->scan)
598 device->driver->scan(device, NULL, 0, NULL, NULL, NULL);
603 connman_bool_t connman_device_get_powered(struct connman_device *device)
605 return device->powered;
608 static int device_scan(struct connman_device *device)
610 if (!device->driver || !device->driver->scan)
613 if (device->powered == FALSE)
616 return device->driver->scan(device, NULL, 0, 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) == TRUE) {
631 struct connman_network *network = value;
633 if (connman_network_get_connecting(network) == TRUE) {
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 static void mark_network_available(gpointer key, gpointer value,
658 struct connman_network *network = value;
660 connman_network_set_available(network, TRUE);
663 static void mark_network_unavailable(gpointer key, gpointer value,
666 struct connman_network *network = value;
668 if (connman_network_get_connected(network) == TRUE ||
669 connman_network_get_connecting(network) == TRUE)
672 connman_network_set_available(network, FALSE);
675 static gboolean remove_unavailable_network(gpointer key, gpointer value,
678 struct connman_network *network = value;
680 if (connman_network_get_connected(network) == TRUE)
683 if (connman_network_get_available(network) == TRUE)
689 void __connman_device_cleanup_networks(struct connman_device *device)
691 g_hash_table_foreach_remove(device->networks,
692 remove_unavailable_network, NULL);
695 connman_bool_t connman_device_get_scanning(struct connman_device *device)
697 return device->scanning;
700 void connman_device_reset_scanning(struct connman_device *device)
702 g_hash_table_foreach(device->networks,
703 mark_network_available, NULL);
707 * connman_device_set_scanning:
708 * @device: device structure
709 * @scanning: scanning state
711 * Change scanning state of device
713 int connman_device_set_scanning(struct connman_device *device,
714 connman_bool_t scanning)
716 DBG("device %p scanning %d", device, scanning);
718 if (!device->driver || !device->driver->scan)
721 if (device->scanning == scanning)
724 device->scanning = scanning;
726 if (scanning == TRUE) {
727 __connman_technology_scan_started(device);
729 g_hash_table_foreach(device->networks,
730 mark_network_unavailable, NULL);
735 __connman_device_cleanup_networks(device);
737 __connman_technology_scan_stopped(device);
739 __connman_service_auto_connect();
745 * connman_device_set_disconnected:
746 * @device: device structure
747 * @disconnected: disconnected state
749 * Change disconnected state of device (only for device with networks)
751 int connman_device_set_disconnected(struct connman_device *device,
752 connman_bool_t disconnected)
754 DBG("device %p disconnected %d", device, disconnected);
756 if (device->disconnected == disconnected)
759 device->disconnected = disconnected;
765 * connman_device_get_disconnected:
766 * @device: device structure
768 * Get device disconnected state
770 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
772 return device->disconnected;
776 * connman_device_set_string:
777 * @device: device structure
778 * @key: unique identifier
779 * @value: string value
781 * Set string value for specific key
783 int connman_device_set_string(struct connman_device *device,
784 const char *key, const char *value)
786 DBG("device %p key %s value %s", device, key, value);
788 if (g_str_equal(key, "Address") == TRUE) {
789 g_free(device->address);
790 device->address = g_strdup(value);
791 } else if (g_str_equal(key, "Name") == TRUE) {
792 g_free(device->name);
793 device->name = g_strdup(value);
794 } else if (g_str_equal(key, "Node") == TRUE) {
795 g_free(device->node);
796 device->node = g_strdup(value);
797 } else if (g_str_equal(key, "Path") == TRUE) {
798 g_free(device->path);
799 device->path = g_strdup(value);
808 * connman_device_get_string:
809 * @device: device structure
810 * @key: unique identifier
812 * Get string value for specific key
814 const char *connman_device_get_string(struct connman_device *device,
817 DBG("device %p key %s", device, key);
819 if (g_str_equal(key, "Address") == TRUE)
820 return device->address;
821 else if (g_str_equal(key, "Name") == TRUE)
823 else if (g_str_equal(key, "Node") == TRUE)
825 else if (g_str_equal(key, "Interface") == TRUE)
826 return device->interface;
827 else if (g_str_equal(key, "Path") == TRUE)
834 * connman_device_add_network:
835 * @device: device structure
836 * @network: network structure
838 * Add new network to the device
840 int connman_device_add_network(struct connman_device *device,
841 struct connman_network *network)
843 const char *identifier = connman_network_get_identifier(network);
845 DBG("device %p network %p", device, network);
847 if (identifier == NULL)
850 connman_network_ref(network);
852 __connman_network_set_device(network, device);
854 g_hash_table_replace(device->networks, g_strdup(identifier),
861 * connman_device_get_network:
862 * @device: device structure
863 * @identifier: network identifier
865 * Get network for given identifier
867 struct connman_network *connman_device_get_network(struct connman_device *device,
868 const char *identifier)
870 DBG("device %p identifier %s", device, identifier);
872 return g_hash_table_lookup(device->networks, identifier);
876 * connman_device_remove_network:
877 * @device: device structure
878 * @identifier: network identifier
880 * Remove network for given identifier
882 int connman_device_remove_network(struct connman_device *device,
883 struct connman_network *network)
885 const char *identifier;
887 DBG("device %p network %p", device, network);
892 identifier = connman_network_get_identifier(network);
893 g_hash_table_remove(device->networks, identifier);
898 void connman_device_remove_all_networks(struct connman_device *device)
900 g_hash_table_remove_all(device->networks);
903 void __connman_device_set_network(struct connman_device *device,
904 struct connman_network *network)
911 if (device->network == network)
914 if (network != NULL) {
915 name = connman_network_get_string(network, "Name");
916 g_free(device->last_network);
917 device->last_network = g_strdup(name);
919 device->network = network;
921 g_free(device->last_network);
922 device->last_network = NULL;
924 device->network = NULL;
928 void __connman_device_set_reconnect(struct connman_device *device,
929 connman_bool_t reconnect)
931 device->reconnect = reconnect;
934 connman_bool_t __connman_device_get_reconnect(
935 struct connman_device *device)
937 return device->reconnect;
940 static gboolean match_driver(struct connman_device *device,
941 struct connman_device_driver *driver)
943 if (device->type == driver->type ||
944 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
951 * connman_device_register:
952 * @device: device structure
954 * Register device with the system
956 int connman_device_register(struct connman_device *device)
960 DBG("device %p name %s", device, device->name);
962 if (device->driver != NULL)
965 for (list = driver_list; list; list = list->next) {
966 struct connman_device_driver *driver = list->data;
968 if (match_driver(device, driver) == FALSE)
971 DBG("driver %p name %s", driver, driver->name);
973 if (driver->probe(device) == 0) {
974 device->driver = driver;
979 if (device->driver == NULL)
982 return __connman_technology_add_device(device);
986 * connman_device_unregister:
987 * @device: device structure
989 * Unregister device with the system
991 void connman_device_unregister(struct connman_device *device)
993 DBG("device %p name %s", device, device->name);
995 if (device->driver == NULL)
998 remove_device(device);
1002 * connman_device_get_data:
1003 * @device: device structure
1005 * Get private device data pointer
1007 void *connman_device_get_data(struct connman_device *device)
1009 return device->driver_data;
1013 * connman_device_set_data:
1014 * @device: device structure
1015 * @data: data pointer
1017 * Set private device data pointer
1019 void connman_device_set_data(struct connman_device *device, void *data)
1021 device->driver_data = data;
1024 struct connman_device *__connman_device_find_device(
1025 enum connman_service_type type)
1029 for (list = device_list; list != NULL; list = list->next) {
1030 struct connman_device *device = list->data;
1031 enum connman_service_type service_type =
1032 __connman_device_get_service_type(device);
1034 if (service_type != type)
1044 * connman_device_set_regdom
1045 * @device: device structure
1046 * @alpha2: string representing regulatory domain
1048 * Set regulatory domain on device basis
1050 int connman_device_set_regdom(struct connman_device *device,
1053 if (device->driver == NULL || device->driver->set_regdom == NULL)
1056 if (device->powered == FALSE)
1059 return device->driver->set_regdom(device, alpha2);
1063 * connman_device_regdom_notify
1064 * @device: device structure
1065 * @alpha2: string representing regulatory domain
1067 * Notify on setting regulatory domain on device basis
1069 void connman_device_regdom_notify(struct connman_device *device,
1070 int result, const char *alpha2)
1072 __connman_technology_notify_regdom_by_device(device, result, alpha2);
1075 int __connman_device_request_scan(enum connman_service_type type)
1077 connman_bool_t success = FALSE;
1078 int last_err = -ENOSYS;
1083 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1084 case CONNMAN_SERVICE_TYPE_SYSTEM:
1085 case CONNMAN_SERVICE_TYPE_ETHERNET:
1086 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1087 case CONNMAN_SERVICE_TYPE_CELLULAR:
1088 case CONNMAN_SERVICE_TYPE_GPS:
1089 case CONNMAN_SERVICE_TYPE_VPN:
1090 case CONNMAN_SERVICE_TYPE_GADGET:
1092 case CONNMAN_SERVICE_TYPE_WIFI:
1093 case CONNMAN_SERVICE_TYPE_WIMAX:
1097 for (list = device_list; list != NULL; list = list->next) {
1098 struct connman_device *device = list->data;
1099 enum connman_service_type service_type =
1100 __connman_device_get_service_type(device);
1102 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1103 service_type != type) {
1107 err = device_scan(device);
1108 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1112 DBG("device %p err %d", device, err);
1116 if (success == TRUE)
1122 int __connman_device_request_hidden_scan(struct connman_device *device,
1123 const char *ssid, unsigned int ssid_len,
1124 const char *identity, const char *passphrase,
1127 DBG("device %p", device);
1129 if (device == NULL || device->driver == NULL ||
1130 device->driver->scan == NULL)
1133 if (device->scanning == TRUE)
1136 return device->driver->scan(device, ssid, ssid_len,
1137 identity, passphrase, user_data);
1140 static char *index2ident(int index, const char *prefix)
1143 struct ether_addr eth;
1150 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1154 memset(&ifr, 0, sizeof(ifr));
1155 ifr.ifr_ifindex = index;
1157 err = ioctl(sk, SIOCGIFNAME, &ifr);
1160 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1167 len = prefix ? strlen(prefix) + 18 : 18;
1173 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1174 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
1175 prefix ? prefix : "",
1176 eth.ether_addr_octet[0],
1177 eth.ether_addr_octet[1],
1178 eth.ether_addr_octet[2],
1179 eth.ether_addr_octet[3],
1180 eth.ether_addr_octet[4],
1181 eth.ether_addr_octet[5]);
1186 static char *index2addr(int index)
1189 struct ether_addr eth;
1196 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1200 memset(&ifr, 0, sizeof(ifr));
1201 ifr.ifr_ifindex = index;
1203 err = ioctl(sk, SIOCGIFNAME, &ifr);
1206 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1217 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1218 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
1219 eth.ether_addr_octet[0],
1220 eth.ether_addr_octet[1],
1221 eth.ether_addr_octet[2],
1222 eth.ether_addr_octet[3],
1223 eth.ether_addr_octet[4],
1224 eth.ether_addr_octet[5]);
1229 struct connman_device *connman_device_create_from_index(int index)
1231 enum connman_device_type type;
1232 struct connman_device *device;
1233 char *devname, *ident = NULL;
1234 char *addr = NULL, *name = NULL;
1239 devname = connman_inet_ifname(index);
1240 if (devname == NULL)
1243 if (__connman_device_isfiltered(devname) == TRUE) {
1244 connman_info("Ignoring interface %s (filtered)", devname);
1249 type = __connman_rtnl_get_device_type(index);
1252 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1253 connman_info("Ignoring interface %s (type unknown)", devname);
1256 case CONNMAN_DEVICE_TYPE_ETHERNET:
1257 case CONNMAN_DEVICE_TYPE_GADGET:
1258 case CONNMAN_DEVICE_TYPE_WIFI:
1259 case CONNMAN_DEVICE_TYPE_WIMAX:
1260 name = index2ident(index, "");
1261 addr = index2addr(index);
1263 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1264 case CONNMAN_DEVICE_TYPE_CELLULAR:
1265 case CONNMAN_DEVICE_TYPE_GPS:
1266 case CONNMAN_DEVICE_TYPE_VENDOR:
1267 name = strdup(devname);
1271 device = connman_device_create(name, type);
1276 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1277 case CONNMAN_DEVICE_TYPE_VENDOR:
1278 case CONNMAN_DEVICE_TYPE_GPS:
1280 case CONNMAN_DEVICE_TYPE_ETHERNET:
1281 case CONNMAN_DEVICE_TYPE_GADGET:
1282 ident = index2ident(index, NULL);
1284 case CONNMAN_DEVICE_TYPE_WIFI:
1285 case CONNMAN_DEVICE_TYPE_WIMAX:
1286 ident = index2ident(index, NULL);
1288 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1290 case CONNMAN_DEVICE_TYPE_CELLULAR:
1291 ident = index2ident(index, NULL);
1295 connman_device_set_index(device, index);
1296 connman_device_set_interface(device, devname);
1298 if (ident != NULL) {
1299 connman_device_set_ident(device, ident);
1303 connman_device_set_string(device, "Address", addr);
1313 connman_bool_t __connman_device_isfiltered(const char *devname)
1316 char **blacklisted_interfaces;
1318 if (device_filter == NULL)
1321 for (pattern = device_filter; *pattern; pattern++) {
1322 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1323 DBG("ignoring device %s (match)", devname);
1329 if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1330 DBG("ignoring dummy networking devices");
1334 if (nodevice_filter == NULL)
1337 for (pattern = nodevice_filter; *pattern; pattern++) {
1338 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1339 DBG("ignoring device %s (no match)", devname);
1345 blacklisted_interfaces =
1346 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1347 if (blacklisted_interfaces == NULL)
1350 for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1351 if (g_str_has_prefix(devname, *pattern) == TRUE) {
1352 DBG("ignoring device %s (blacklist)", devname);
1360 static void cleanup_devices(void)
1363 * Check what interfaces are currently up and if connman is
1364 * suppose to handle the interface, then cleanup the mess
1365 * related to that interface. There might be weird routes etc
1366 * that are related to that interface and that might confuse
1367 * connmand. So in this case we just turn the interface down
1368 * so that kernel removes routes/addresses automatically and
1369 * then proceed the startup.
1371 * Note that this cleanup must be done before rtnl/detect code
1372 * has activated interface watches.
1378 interfaces = __connman_inet_get_running_interfaces();
1380 if (interfaces == NULL)
1383 for (i = 0; interfaces[i] != NULL; i++) {
1384 connman_bool_t filtered;
1387 filtered = __connman_device_isfiltered(interfaces[i]);
1388 if (filtered == TRUE)
1391 index = connman_inet_ifindex(interfaces[i]);
1395 DBG("cleaning up %s index %d", interfaces[i], index);
1397 connman_inet_ifdown(index);
1400 * ConnMan will turn the interface UP automatically so
1401 * no need to do it here.
1405 g_strfreev(interfaces);
1408 int __connman_device_init(const char *device, const char *nodevice)
1413 device_filter = g_strsplit(device, ",", -1);
1415 if (nodevice != NULL)
1416 nodevice_filter = g_strsplit(nodevice, ",", -1);
1423 void __connman_device_cleanup(void)
1427 g_strfreev(nodevice_filter);
1428 g_strfreev(device_filter);