f2d811d4e45643a86a35fdd9e05d16f1e8c17979
[framework/connectivity/connman.git] / src / detect.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <connman/device.h>
39 #include <connman/rtnl.h>
40 #include <connman/log.h>
41
42 #include "connman.h"
43
44 static GSList *device_list = NULL;
45
46 static struct connman_device *find_device(int index)
47 {
48         GSList *list;
49
50         for (list = device_list; list; list = list->next) {
51                 struct connman_device *device = list->data;
52
53                 if (connman_device_get_index(device) == index)
54                         return device;
55         }
56
57         return NULL;
58 }
59
60 static char *index2name(int index)
61 {
62         struct ifreq ifr;
63         int sk, err;
64
65         if (index < 0)
66                 return NULL;
67
68         sk = socket(PF_INET, SOCK_DGRAM, 0);
69         if (sk < 0)
70                 return NULL;
71
72         memset(&ifr, 0, sizeof(ifr));
73         ifr.ifr_ifindex = index;
74
75         err = ioctl(sk, SIOCGIFNAME, &ifr);
76
77         close(sk);
78
79         if (err < 0)
80                 return NULL;
81
82         return strdup(ifr.ifr_name);
83 }
84
85 static char *index2ident(int index, const char *prefix)
86 {
87         struct ifreq ifr;
88         struct ether_addr *eth;
89         char *str;
90         int sk, err, len;
91
92         if (index < 0)
93                 return NULL;
94
95         sk = socket(PF_INET, SOCK_DGRAM, 0);
96         if (sk < 0)
97                 return NULL;
98
99         memset(&ifr, 0, sizeof(ifr));
100         ifr.ifr_ifindex = index;
101
102         err = ioctl(sk, SIOCGIFNAME, &ifr);
103
104         if (err == 0)
105                 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
106
107         close(sk);
108
109         if (err < 0)
110                 return NULL;
111
112         len = prefix ? strlen(prefix) + 18 : 18;
113
114         str = malloc(len);
115         if (!str)
116                 return NULL;
117
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]);
127
128         return str;
129 }
130
131 static void detect_newlink(unsigned short type, int index,
132                                         unsigned flags, unsigned change)
133 {
134         enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
135         struct connman_device *device;
136         gchar *name, *devname;
137
138         DBG("type %d index %d", type, index);
139
140         device = find_device(index);
141         if (device != NULL)
142                 return;
143
144         devname = index2name(index);
145
146         if (type == ARPHRD_ETHER) {
147                 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
148                 struct stat st;
149                 struct iwreq iwr;
150                 int sk;
151
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);
156
157                 memset(&iwr, 0, sizeof(iwr));
158                 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
159
160                 sk = socket(PF_INET, SOCK_DGRAM, 0);
161
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;
170                 else
171                         devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
172
173                 close(sk);
174         } else if (type == ARPHRD_NONE) {
175                 if (g_str_has_prefix(devname, "hso") == TRUE)
176                         devtype = CONNMAN_DEVICE_TYPE_HSO;
177         }
178
179         if (devtype == CONNMAN_DEVICE_TYPE_UNKNOWN) {
180                 g_free(devname);
181                 return;
182         }
183
184         switch (devtype) {
185         case CONNMAN_DEVICE_TYPE_HSO:
186                 name = strdup(devname);
187                 break;
188         default:
189                 name = index2ident(index, "dev_");
190                 break;
191         }
192
193         device = connman_device_create(name, devtype);
194         if (device == NULL) {
195                 g_free(devname);
196                 g_free(name);
197                 return;
198         }
199
200         connman_device_set_index(device, index);
201         connman_device_set_interface(device, devname);
202
203         g_free(devname);
204         g_free(name);
205
206         if (connman_device_register(device) < 0) {
207                 connman_device_unref(device);
208                 return;
209         }
210
211         device_list = g_slist_append(device_list, device);
212 }
213
214 static void detect_dellink(unsigned short type, int index,
215                                         unsigned flags, unsigned change)
216 {
217         struct connman_device *device;
218
219         DBG("type %d index %d", type, index);
220
221         device = find_device(index);
222         if (device == NULL)
223                 return;
224
225         device_list = g_slist_remove(device_list, device);
226
227         connman_device_unregister(device);
228         connman_device_unref(device);
229 }
230
231 static struct connman_rtnl detect_rtnl = {
232         .name           = "detect",
233         .priority       = CONNMAN_RTNL_PRIORITY_LOW,
234         .newlink        = detect_newlink,
235         .dellink        = detect_dellink,
236 };
237
238 int __connman_detect_init(void)
239 {
240         int err;
241
242         err = connman_rtnl_register(&detect_rtnl);
243         if (err < 0)
244                 return err;
245
246         connman_rtnl_send_getlink();
247
248         return 0;
249 }
250
251 void __connman_detect_cleanup(void)
252 {
253         GSList *list;
254
255         connman_rtnl_unregister(&detect_rtnl);
256
257         for (list = device_list; list; list = list->next) {
258                 struct connman_device *device = list->data;
259
260                 connman_device_unregister(device);
261                 connman_device_unref(device);
262         }
263
264         g_slist_free(device_list);
265         device_list = NULL;
266 }