Fail if interface name can't be retrieved
[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 <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         struct connman_device *device;
134         gchar *name, *devname;
135
136         DBG("type %d index %d", type, index);
137
138         device = find_device(index);
139         if (device != NULL)
140                 return;
141
142         devname = index2name(index);
143         if (devname == NULL)
144                 return;
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         switch (devtype) {
201         case CONNMAN_DEVICE_TYPE_HSO:
202                 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
203                 connman_device_set_mode(device,
204                                         CONNMAN_DEVICE_MODE_SINGLE_NETWORK);
205                 break;
206         default:
207                 break;
208         }
209
210         connman_device_set_index(device, index);
211         connman_device_set_interface(device, devname);
212
213         g_free(devname);
214         g_free(name);
215
216         if (connman_device_register(device) < 0) {
217                 connman_device_unref(device);
218                 return;
219         }
220
221         device_list = g_slist_append(device_list, device);
222 }
223
224 static void detect_dellink(unsigned short type, int index,
225                                         unsigned flags, unsigned change)
226 {
227         struct connman_device *device;
228
229         DBG("type %d index %d", type, index);
230
231         device = find_device(index);
232         if (device == NULL)
233                 return;
234
235         device_list = g_slist_remove(device_list, device);
236
237         connman_device_unregister(device);
238         connman_device_unref(device);
239 }
240
241 static struct connman_rtnl detect_rtnl = {
242         .name           = "detect",
243         .priority       = CONNMAN_RTNL_PRIORITY_LOW,
244         .newlink        = detect_newlink,
245         .dellink        = detect_dellink,
246 };
247
248 int __connman_detect_init(void)
249 {
250         int err;
251
252         err = connman_rtnl_register(&detect_rtnl);
253         if (err < 0)
254                 return err;
255
256         connman_rtnl_send_getlink();
257
258         return 0;
259 }
260
261 void __connman_detect_cleanup(void)
262 {
263         GSList *list;
264
265         connman_rtnl_unregister(&detect_rtnl);
266
267         for (list = device_list; list; list = list->next) {
268                 struct connman_device *device = list->data;
269
270                 connman_device_unregister(device);
271                 connman_device_unref(device);
272         }
273
274         g_slist_free(device_list);
275         device_list = NULL;
276 }