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>
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 *index2ident(int index, const char *prefix)
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);
110 len = prefix ? strlen(prefix) + 18 : 18;
116 eth = (void *) &ifr.ifr_hwaddr.sa_data;
117 snprintf(str, len, "%s%02X_%02X_%02X_%02X_%02X_%02X",
118 prefix ? prefix : "",
119 eth->ether_addr_octet[0],
120 eth->ether_addr_octet[1],
121 eth->ether_addr_octet[2],
122 eth->ether_addr_octet[3],
123 eth->ether_addr_octet[4],
124 eth->ether_addr_octet[5]);
129 static void detect_newlink(unsigned short type, int index,
130 unsigned flags, unsigned change)
132 enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
133 struct connman_device *device;
134 gchar *name, *devname;
136 DBG("type %d index %d", type, index);
138 device = find_device(index);
142 devname = index2name(index);
144 if (type == ARPHRD_ETHER) {
145 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
150 snprintf(bridge_path, PATH_MAX,
151 "/sys/class/net/%s/bridge", devname);
152 snprintf(wimax_path, PATH_MAX,
153 "/sys/class/net/%s/wimax", devname);
155 memset(&iwr, 0, sizeof(iwr));
156 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
158 sk = socket(PF_INET, SOCK_DGRAM, 0);
160 if (g_str_has_prefix(devname, "bnep") == TRUE)
161 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
162 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
163 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
164 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
165 devtype = CONNMAN_DEVICE_TYPE_WIMAX;
166 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
167 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
169 devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
172 } else if (type == ARPHRD_NONE) {
173 if (g_str_has_prefix(devname, "hso") == TRUE)
174 devtype = CONNMAN_DEVICE_TYPE_HSO;
177 if (devtype == CONNMAN_DEVICE_TYPE_UNKNOWN) {
183 case CONNMAN_DEVICE_TYPE_HSO:
184 name = strdup(devname);
187 name = index2ident(index, "dev_");
191 device = connman_device_create(name, devtype);
192 if (device == NULL) {
199 case CONNMAN_DEVICE_TYPE_HSO:
200 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
201 connman_device_set_mode(device,
202 CONNMAN_DEVICE_MODE_SINGLE_NETWORK);
208 connman_device_set_index(device, index);
209 connman_device_set_interface(device, devname);
214 if (connman_device_register(device) < 0) {
215 connman_device_unref(device);
219 device_list = g_slist_append(device_list, device);
222 static void detect_dellink(unsigned short type, int index,
223 unsigned flags, unsigned change)
225 struct connman_device *device;
227 DBG("type %d index %d", type, index);
229 device = find_device(index);
233 device_list = g_slist_remove(device_list, device);
235 connman_device_unregister(device);
236 connman_device_unref(device);
239 static struct connman_rtnl detect_rtnl = {
241 .priority = CONNMAN_RTNL_PRIORITY_LOW,
242 .newlink = detect_newlink,
243 .dellink = detect_dellink,
246 int __connman_detect_init(void)
250 err = connman_rtnl_register(&detect_rtnl);
254 connman_rtnl_send_getlink();
259 void __connman_detect_cleanup(void)
263 connman_rtnl_unregister(&detect_rtnl);
265 for (list = device_list; list; list = list->next) {
266 struct connman_device *device = list->data;
268 connman_device_unregister(device);
269 connman_device_unref(device);
272 g_slist_free(device_list);