1415638b6a4604b70c0fcfde1ab4cc0adb7fd21d
[platform/upstream/connman.git] / src / detect.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/stat.h>
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>
37
38 #include <glib.h>
39
40 #include "connman.h"
41
42 static GSList *device_list = NULL;
43
44 static struct connman_device *find_device(int index)
45 {
46         GSList *list;
47
48         for (list = device_list; list; list = list->next) {
49                 struct connman_device *device = list->data;
50
51                 if (connman_device_get_index(device) == index)
52                         return device;
53         }
54
55         return NULL;
56 }
57
58 static char *index2name(int index)
59 {
60         struct ifreq ifr;
61         int sk, err;
62
63         if (index < 0)
64                 return NULL;
65
66         sk = socket(PF_INET, SOCK_DGRAM, 0);
67         if (sk < 0)
68                 return NULL;
69
70         memset(&ifr, 0, sizeof(ifr));
71         ifr.ifr_ifindex = index;
72
73         err = ioctl(sk, SIOCGIFNAME, &ifr);
74
75         close(sk);
76
77         if (err < 0)
78                 return NULL;
79
80         return strdup(ifr.ifr_name);
81 }
82
83 static char *index2addr(int index)
84 {
85         struct ifreq ifr;
86         struct ether_addr *eth;
87         char *str;
88         int sk, err;
89
90         if (index < 0)
91                 return NULL;
92
93         sk = socket(PF_INET, SOCK_DGRAM, 0);
94         if (sk < 0)
95                 return NULL;
96
97         memset(&ifr, 0, sizeof(ifr));
98         ifr.ifr_ifindex = index;
99
100         err = ioctl(sk, SIOCGIFNAME, &ifr);
101
102         if (err == 0)
103                 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
104
105         close(sk);
106
107         if (err < 0)
108                 return NULL;
109
110         str = malloc(18);
111         if (!str)
112                 return NULL;
113
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]);
122
123         return str;
124 }
125
126 static char *index2ident(int index, const char *prefix)
127 {
128         struct ifreq ifr;
129         struct ether_addr *eth;
130         char *str;
131         int sk, err, len;
132
133         if (index < 0)
134                 return NULL;
135
136         sk = socket(PF_INET, SOCK_DGRAM, 0);
137         if (sk < 0)
138                 return NULL;
139
140         memset(&ifr, 0, sizeof(ifr));
141         ifr.ifr_ifindex = index;
142
143         err = ioctl(sk, SIOCGIFNAME, &ifr);
144
145         if (err == 0)
146                 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
147
148         close(sk);
149
150         if (err < 0)
151                 return NULL;
152
153         len = prefix ? strlen(prefix) + 18 : 18;
154
155         str = malloc(len);
156         if (!str)
157                 return NULL;
158
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]);
168
169         return str;
170 }
171
172 static void detect_newlink(unsigned short type, int index,
173                                         unsigned flags, unsigned change)
174 {
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;
179
180         DBG("type %d index %d", type, index);
181
182         device = find_device(index);
183         if (device != NULL)
184                 return;
185
186         devname = index2name(index);
187         if (devname == NULL)
188                 return;
189
190         if (type == ARPHRD_ETHER) {
191                 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
192                 struct stat st;
193                 struct iwreq iwr;
194                 int sk;
195
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);
200
201                 memset(&iwr, 0, sizeof(iwr));
202                 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
203
204                 sk = socket(PF_INET, SOCK_DGRAM, 0);
205
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;
216                 else
217                         devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
218
219                 close(sk);
220         } else if (type == ARPHRD_NONE) {
221                 if (g_str_has_prefix(devname, "hso") == TRUE)
222                         devtype = CONNMAN_DEVICE_TYPE_HSO;
223         }
224
225         switch (devtype) {
226         case CONNMAN_DEVICE_TYPE_UNKNOWN:
227                 g_free(devname);
228                 return;
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);
234                 break;
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);
243                 addr = NULL;
244                 break;
245         }
246
247         device = connman_device_create(name, devtype);
248         if (device == NULL) {
249                 g_free(devname);
250                 g_free(name);
251                 g_free(addr);
252                 return;
253         }
254
255         switch (devtype) {
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;
263                 break;
264         case CONNMAN_DEVICE_TYPE_ETHERNET:
265                 mode = CONNMAN_DEVICE_MODE_TRANSPORT_IP;
266                 break;
267         case CONNMAN_DEVICE_TYPE_WIFI:
268         case CONNMAN_DEVICE_TYPE_WIMAX:
269                 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
270                 break;
271         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
272                 mode = CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE;
273                 break;
274         case CONNMAN_DEVICE_TYPE_HSO:
275                 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
276                 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
277                 break;
278         }
279
280         connman_device_set_mode(device, mode);
281
282         connman_device_set_index(device, index);
283         connman_device_set_interface(device, devname);
284
285         connman_device_set_string(device, "Address", addr);
286
287         g_free(devname);
288         g_free(name);
289         g_free(addr);
290
291         if (connman_device_register(device) < 0) {
292                 connman_device_unref(device);
293                 return;
294         }
295
296         device_list = g_slist_append(device_list, device);
297 }
298
299 static void detect_dellink(unsigned short type, int index,
300                                         unsigned flags, unsigned change)
301 {
302         struct connman_device *device;
303
304         DBG("type %d index %d", type, index);
305
306         device = find_device(index);
307         if (device == NULL)
308                 return;
309
310         device_list = g_slist_remove(device_list, device);
311
312         connman_device_unregister(device);
313         connman_device_unref(device);
314 }
315
316 static struct connman_rtnl detect_rtnl = {
317         .name           = "detect",
318         .priority       = CONNMAN_RTNL_PRIORITY_LOW,
319         .newlink        = detect_newlink,
320         .dellink        = detect_dellink,
321 };
322
323 int __connman_detect_init(void)
324 {
325         int err;
326
327         err = connman_rtnl_register(&detect_rtnl);
328         if (err < 0)
329                 return err;
330
331         connman_rtnl_send_getlink();
332
333         return 0;
334 }
335
336 void __connman_detect_cleanup(void)
337 {
338         GSList *list;
339
340         connman_rtnl_unregister(&detect_rtnl);
341
342         for (list = device_list; list; list = list->next) {
343                 struct connman_device *device = list->data;
344
345                 connman_device_unregister(device);
346                 connman_device_unref(device);
347         }
348
349         g_slist_free(device_list);
350         device_list = NULL;
351 }