5 * Copyright (C) 2007-2008 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
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <net/ethernet.h>
35 #include <linux/if_arp.h>
36 #include <linux/wireless.h>
38 #include <connman/device.h>
39 #include <connman/rtnl.h>
40 #include <connman/log.h>
44 static GSList *device_list = NULL;
46 static struct connman_device *find_device(int index)
50 for (list = device_list; list; list = list->next) {
51 struct connman_device *device = list->data;
53 if (connman_device_get_index(device) == index)
60 static char *index2name(int index)
68 sk = socket(PF_INET, SOCK_DGRAM, 0);
72 memset(&ifr, 0, sizeof(ifr));
73 ifr.ifr_ifindex = index;
75 err = ioctl(sk, SIOCGIFNAME, &ifr);
82 return strdup(ifr.ifr_name);
85 static char *index2ident(int index, const char *prefix)
88 struct ether_addr *eth;
95 sk = socket(PF_INET, SOCK_DGRAM, 0);
99 memset(&ifr, 0, sizeof(ifr));
100 ifr.ifr_ifindex = index;
102 err = ioctl(sk, SIOCGIFNAME, &ifr);
105 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
112 len = prefix ? strlen(prefix) + 18 : 18;
118 eth = (void *) &ifr.ifr_hwaddr.sa_data;
119 snprintf(str, len, "%s%02X_%02X_%02X_%02X_%02X_%02X",
120 prefix ? prefix : "",
121 eth->ether_addr_octet[0],
122 eth->ether_addr_octet[1],
123 eth->ether_addr_octet[2],
124 eth->ether_addr_octet[3],
125 eth->ether_addr_octet[4],
126 eth->ether_addr_octet[5]);
131 static void detect_newlink(unsigned short type, int index,
132 unsigned flags, unsigned change)
134 enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
135 struct connman_device *device;
136 gchar *name, *devname;
138 DBG("index %d", index);
140 device = find_device(index);
144 devname = index2name(index);
146 if (type == ARPHRD_ETHER) {
147 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
152 snprintf(bridge_path, PATH_MAX,
153 "/sys/class/net/%s/bridge", devname);
154 snprintf(wimax_path, PATH_MAX,
155 "/sys/class/net/%s/wimax", devname);
157 memset(&iwr, 0, sizeof(iwr));
158 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
160 sk = socket(PF_INET, SOCK_DGRAM, 0);
162 if (g_str_has_prefix(devname, "bnep") == TRUE)
163 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
164 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
165 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
166 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
167 devtype = CONNMAN_DEVICE_TYPE_WIMAX;
168 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
169 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
171 devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
176 if (devtype == CONNMAN_DEVICE_TYPE_UNKNOWN) {
181 name = index2ident(index, "dev_");
183 device = connman_device_create(name, devtype);
184 if (device == NULL) {
190 connman_device_set_index(device, index);
191 connman_device_set_interface(device, devname);
196 if (connman_device_register(device) < 0) {
197 connman_device_unref(device);
201 device_list = g_slist_append(device_list, device);
204 static void detect_dellink(unsigned short type, int index,
205 unsigned flags, unsigned change)
207 struct connman_device *device;
209 DBG("index %d", index);
211 device = find_device(index);
215 device_list = g_slist_remove(device_list, device);
217 connman_device_unregister(device);
218 connman_device_unref(device);
221 static struct connman_rtnl detect_rtnl = {
223 .priority = CONNMAN_RTNL_PRIORITY_LOW,
224 .newlink = detect_newlink,
225 .dellink = detect_dellink,
228 int __connman_detect_init(void)
232 err = connman_rtnl_register(&detect_rtnl);
236 connman_rtnl_send_getlink();
241 void __connman_detect_cleanup(void)
245 connman_rtnl_unregister(&detect_rtnl);
247 for (list = device_list; list; list = list->next) {
248 struct connman_device *device = list->data;
250 connman_device_unregister(device);
251 connman_device_unref(device);
254 g_slist_free(device_list);