Add device type for Novatel Wireless
[connman] / 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 *index2ident(int index, const char *prefix)
84 {
85         struct ifreq ifr;
86         struct ether_addr *eth;
87         char *str;
88         int sk, err, len;
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         len = prefix ? strlen(prefix) + 18 : 18;
111
112         str = malloc(len);
113         if (!str)
114                 return NULL;
115
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]);
125
126         return str;
127 }
128
129 static void detect_newlink(unsigned short type, int index,
130                                         unsigned flags, unsigned change)
131 {
132         enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
133         enum connman_device_mode mode = CONNMAN_DEVICE_MODE_UNKNOWN;
134         struct connman_device *device;
135         gchar *name, *devname;
136
137         DBG("type %d index %d", type, index);
138
139         device = find_device(index);
140         if (device != NULL)
141                 return;
142
143         devname = index2name(index);
144         if (devname == NULL)
145                 return;
146
147         if (type == ARPHRD_ETHER) {
148                 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
149                 struct stat st;
150                 struct iwreq iwr;
151                 int sk;
152
153                 snprintf(bridge_path, PATH_MAX,
154                                         "/sys/class/net/%s/bridge", devname);
155                 snprintf(wimax_path, PATH_MAX,
156                                         "/sys/class/net/%s/wimax", devname);
157
158                 memset(&iwr, 0, sizeof(iwr));
159                 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
160
161                 sk = socket(PF_INET, SOCK_DGRAM, 0);
162
163                 if (g_str_has_prefix(devname, "bnep") == TRUE)
164                         devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
165                 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
166                         devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
167                 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
168                         devtype = CONNMAN_DEVICE_TYPE_WIMAX;
169                 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
170                         devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
171                 else
172                         devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
173
174                 close(sk);
175         } else if (type == ARPHRD_NONE) {
176                 if (g_str_has_prefix(devname, "hso") == TRUE)
177                         devtype = CONNMAN_DEVICE_TYPE_HSO;
178         }
179
180         switch (devtype) {
181         case CONNMAN_DEVICE_TYPE_UNKNOWN:
182                 g_free(devname);
183                 return;
184         case CONNMAN_DEVICE_TYPE_ETHERNET:
185         case CONNMAN_DEVICE_TYPE_WIFI:
186         case CONNMAN_DEVICE_TYPE_WIMAX:
187                 name = index2ident(index, "dev_");
188                 break;
189         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
190         case CONNMAN_DEVICE_TYPE_HSO:
191         case CONNMAN_DEVICE_TYPE_HUAWEI:
192         case CONNMAN_DEVICE_TYPE_NOVATEL:
193         case CONNMAN_DEVICE_TYPE_VENDOR:
194                 name = strdup(devname);
195                 break;
196         }
197
198         device = connman_device_create(name, devtype);
199         if (device == NULL) {
200                 g_free(devname);
201                 g_free(name);
202                 return;
203         }
204
205         switch (devtype) {
206         case CONNMAN_DEVICE_TYPE_UNKNOWN:
207         case CONNMAN_DEVICE_TYPE_VENDOR:
208         case CONNMAN_DEVICE_TYPE_HUAWEI:
209         case CONNMAN_DEVICE_TYPE_NOVATEL:
210                 mode = CONNMAN_DEVICE_MODE_UNKNOWN;
211                 break;
212         case CONNMAN_DEVICE_TYPE_ETHERNET:
213                 mode = CONNMAN_DEVICE_MODE_TRANSPORT_IP;
214                 break;
215         case CONNMAN_DEVICE_TYPE_WIFI:
216         case CONNMAN_DEVICE_TYPE_WIMAX:
217                 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
218                 break;
219         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
220                 mode = CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE;
221                 break;
222         case CONNMAN_DEVICE_TYPE_HSO:
223                 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
224                 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
225                 break;
226         }
227
228         connman_device_set_mode(device, mode);
229
230         connman_device_set_index(device, index);
231         connman_device_set_interface(device, devname);
232
233         g_free(devname);
234         g_free(name);
235
236         if (connman_device_register(device) < 0) {
237                 connman_device_unref(device);
238                 return;
239         }
240
241         device_list = g_slist_append(device_list, device);
242 }
243
244 static void detect_dellink(unsigned short type, int index,
245                                         unsigned flags, unsigned change)
246 {
247         struct connman_device *device;
248
249         DBG("type %d index %d", type, index);
250
251         device = find_device(index);
252         if (device == NULL)
253                 return;
254
255         device_list = g_slist_remove(device_list, device);
256
257         connman_device_unregister(device);
258         connman_device_unref(device);
259 }
260
261 static struct connman_rtnl detect_rtnl = {
262         .name           = "detect",
263         .priority       = CONNMAN_RTNL_PRIORITY_LOW,
264         .newlink        = detect_newlink,
265         .dellink        = detect_dellink,
266 };
267
268 int __connman_detect_init(void)
269 {
270         int err;
271
272         err = connman_rtnl_register(&detect_rtnl);
273         if (err < 0)
274                 return err;
275
276         connman_rtnl_send_getlink();
277
278         return 0;
279 }
280
281 void __connman_detect_cleanup(void)
282 {
283         GSList *list;
284
285         connman_rtnl_unregister(&detect_rtnl);
286
287         for (list = device_list; list; list = list->next) {
288                 struct connman_device *device = list->data;
289
290                 connman_device_unregister(device);
291                 connman_device_unref(device);
292         }
293
294         g_slist_free(device_list);
295         device_list = NULL;
296 }