5 * Copyright (C) 2007-2009 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>
42 static GSList *device_list = NULL;
44 static struct connman_device *find_device(int index)
48 for (list = device_list; list; list = list->next) {
49 struct connman_device *device = list->data;
51 if (connman_device_get_index(device) == index)
58 static char *index2name(int index)
66 sk = socket(PF_INET, SOCK_DGRAM, 0);
70 memset(&ifr, 0, sizeof(ifr));
71 ifr.ifr_ifindex = index;
73 err = ioctl(sk, SIOCGIFNAME, &ifr);
80 return strdup(ifr.ifr_name);
83 static char *index2addr(int index)
86 struct ether_addr *eth;
93 sk = socket(PF_INET, SOCK_DGRAM, 0);
97 memset(&ifr, 0, sizeof(ifr));
98 ifr.ifr_ifindex = index;
100 err = ioctl(sk, SIOCGIFNAME, &ifr);
103 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
114 eth = (void *) &ifr.ifr_hwaddr.sa_data;
115 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
116 eth->ether_addr_octet[0],
117 eth->ether_addr_octet[1],
118 eth->ether_addr_octet[2],
119 eth->ether_addr_octet[3],
120 eth->ether_addr_octet[4],
121 eth->ether_addr_octet[5]);
126 static char *index2ident(int index, const char *prefix)
129 struct ether_addr *eth;
136 sk = socket(PF_INET, SOCK_DGRAM, 0);
140 memset(&ifr, 0, sizeof(ifr));
141 ifr.ifr_ifindex = index;
143 err = ioctl(sk, SIOCGIFNAME, &ifr);
146 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
153 len = prefix ? strlen(prefix) + 18 : 18;
159 eth = (void *) &ifr.ifr_hwaddr.sa_data;
160 snprintf(str, len, "%s%02X_%02X_%02X_%02X_%02X_%02X",
161 prefix ? prefix : "",
162 eth->ether_addr_octet[0],
163 eth->ether_addr_octet[1],
164 eth->ether_addr_octet[2],
165 eth->ether_addr_octet[3],
166 eth->ether_addr_octet[4],
167 eth->ether_addr_octet[5]);
172 static void detect_newlink(unsigned short type, int index,
173 unsigned flags, unsigned change)
175 enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
176 enum connman_device_mode mode = CONNMAN_DEVICE_MODE_UNKNOWN;
177 struct connman_device *device;
178 gchar *addr, *name, *devname;
180 DBG("type %d index %d", type, index);
182 device = find_device(index);
186 devname = index2name(index);
190 if (type == ARPHRD_ETHER) {
191 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
196 snprintf(bridge_path, PATH_MAX,
197 "/sys/class/net/%s/bridge", devname);
198 snprintf(wimax_path, PATH_MAX,
199 "/sys/class/net/%s/wimax", devname);
201 memset(&iwr, 0, sizeof(iwr));
202 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
204 sk = socket(PF_INET, SOCK_DGRAM, 0);
206 if (g_str_has_prefix(devname, "bnep") == TRUE)
207 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
208 else if (g_str_has_prefix(devname, "wmx") == TRUE)
209 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
210 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
211 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
212 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
213 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
214 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
215 devtype = CONNMAN_DEVICE_TYPE_WIFI;
217 devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
220 } else if (type == ARPHRD_NONE) {
221 if (g_str_has_prefix(devname, "hso") == TRUE)
222 devtype = CONNMAN_DEVICE_TYPE_HSO;
226 case CONNMAN_DEVICE_TYPE_UNKNOWN:
229 case CONNMAN_DEVICE_TYPE_ETHERNET:
230 case CONNMAN_DEVICE_TYPE_WIFI:
231 case CONNMAN_DEVICE_TYPE_WIMAX:
232 name = index2ident(index, "dev_");
233 addr = index2addr(index);
235 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
236 case CONNMAN_DEVICE_TYPE_GPS:
237 case CONNMAN_DEVICE_TYPE_HSO:
238 case CONNMAN_DEVICE_TYPE_NOZOMI:
239 case CONNMAN_DEVICE_TYPE_HUAWEI:
240 case CONNMAN_DEVICE_TYPE_NOVATEL:
241 case CONNMAN_DEVICE_TYPE_VENDOR:
242 name = strdup(devname);
247 device = connman_device_create(name, devtype);
248 if (device == NULL) {
256 case CONNMAN_DEVICE_TYPE_UNKNOWN:
257 case CONNMAN_DEVICE_TYPE_VENDOR:
258 case CONNMAN_DEVICE_TYPE_NOZOMI:
259 case CONNMAN_DEVICE_TYPE_HUAWEI:
260 case CONNMAN_DEVICE_TYPE_NOVATEL:
261 case CONNMAN_DEVICE_TYPE_GPS:
262 mode = CONNMAN_DEVICE_MODE_UNKNOWN;
264 case CONNMAN_DEVICE_TYPE_ETHERNET:
265 mode = CONNMAN_DEVICE_MODE_TRANSPORT_IP;
267 case CONNMAN_DEVICE_TYPE_WIFI:
268 case CONNMAN_DEVICE_TYPE_WIMAX:
269 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
271 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
272 mode = CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE;
274 case CONNMAN_DEVICE_TYPE_HSO:
275 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
276 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
280 connman_device_set_mode(device, mode);
282 connman_device_set_index(device, index);
283 connman_device_set_interface(device, devname);
285 connman_device_set_string(device, "Address", addr);
291 if (connman_device_register(device) < 0) {
292 connman_device_unref(device);
296 device_list = g_slist_append(device_list, device);
299 static void detect_dellink(unsigned short type, int index,
300 unsigned flags, unsigned change)
302 struct connman_device *device;
304 DBG("type %d index %d", type, index);
306 device = find_device(index);
310 device_list = g_slist_remove(device_list, device);
312 connman_device_unregister(device);
313 connman_device_unref(device);
316 static struct connman_rtnl detect_rtnl = {
318 .priority = CONNMAN_RTNL_PRIORITY_LOW,
319 .newlink = detect_newlink,
320 .dellink = detect_dellink,
323 int __connman_detect_init(void)
327 err = connman_rtnl_register(&detect_rtnl);
331 connman_rtnl_send_getlink();
336 void __connman_detect_cleanup(void)
340 connman_rtnl_unregister(&detect_rtnl);
342 for (list = device_list; list; list = list->next) {
343 struct connman_device *device = list->data;
345 connman_device_unregister(device);
346 connman_device_unref(device);
349 g_slist_free(device_list);