87ad8ccc8fcd4961c2412e646220470679e3919c
[connman] / plugins / wifi.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 <unistd.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
33
34 #include <gdbus.h>
35
36 #include <connman/plugin.h>
37 #include <connman/driver.h>
38 #include <connman/rtnl.h>
39 #include <connman/log.h>
40
41 #include "inet.h"
42 #include "supplicant.h"
43
44 struct wifi_data {
45         GSList *list;
46         gchar *identifier;
47 };
48
49 static int network_probe(struct connman_element *element)
50 {
51         DBG("element %p name %s", element, element->name);
52
53         return 0;
54 }
55
56 static void network_remove(struct connman_element *element)
57 {
58         DBG("element %p name %s", element, element->name);
59 }
60
61 static int network_enable(struct connman_element *element)
62 {
63         char *identifier, *passphrase = NULL;
64         unsigned char *ssid;
65         int ssid_len;
66
67         DBG("element %p name %s", element, element->name);
68
69         if (connman_element_get_static_property(element,
70                                         "WiFi.Name", &identifier) == FALSE)
71                 return -EIO;
72
73         if (connman_element_get_static_array_property(element,
74                                 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
75                 return -EIO;
76
77         if (element->parent != NULL) {
78                 struct wifi_data *data = connman_element_get_data(element->parent);
79
80                 if (data != NULL) {
81                         g_free(data->identifier);
82                         data->identifier = g_strdup(identifier);
83                 }
84         }
85
86         connman_element_get_value(element,
87                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
88
89         DBG("identifier %s passhprase %s", identifier, passphrase);
90
91         if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
92                 connman_error("Failed to initiate connect");
93
94         return 0;
95 }
96
97 static int network_disable(struct connman_element *element)
98 {
99         DBG("element %p name %s", element, element->name);
100
101         connman_element_unregister_children(element);
102
103         __supplicant_disconnect(element);
104
105         return 0;
106 }
107
108 static struct connman_driver network_driver = {
109         .name           = "wifi-network",
110         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
111         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
112         .probe          = network_probe,
113         .remove         = network_remove,
114         .enable         = network_enable,
115         .disable        = network_disable,
116 };
117
118 static struct connman_element *find_element(struct wifi_data *data,
119                                                 const char *identifier)
120 {
121         GSList *list;
122
123         for (list = data->list; list; list = list->next) {
124                 struct connman_element *element = list->data;
125
126                 if (connman_element_match_static_property(element,
127                                         "WiFi.Name", &identifier) == TRUE)
128                         return element;
129         }
130
131         return NULL;
132 }
133
134 static void state_change(struct connman_element *parent,
135                                                 enum supplicant_state state)
136 {
137         struct wifi_data *data = connman_element_get_data(parent);
138         struct connman_element *element;
139
140         DBG("state %d", state);
141
142         if (data->identifier == NULL)
143                 return;
144
145         element = find_element(data, data->identifier);
146         if (element == NULL)
147                 return;
148
149         if (state == STATE_COMPLETED) {
150                 struct connman_element *dhcp;
151
152                 dhcp = connman_element_create(NULL);
153
154                 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
155                 dhcp->index = element->index;
156
157                 connman_element_register(dhcp, element);
158         }
159 }
160
161 static void scan_result(struct connman_element *parent,
162                                         struct supplicant_network *network)
163 {
164         struct wifi_data *data = connman_element_get_data(parent);
165         struct connman_element *element;
166         gchar *temp;
167         int i;
168
169         DBG("network %p identifier %s", network, network->identifier);
170
171         if (data == NULL)
172                 return;
173
174         if (network->identifier == NULL)
175                 return;
176
177         if (network->identifier[0] == '\0')
178                 return;
179
180         temp = g_strdup(network->identifier);
181
182         for (i = 0; i < strlen(temp); i++) {
183                 if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
184                         temp[i] = '_';
185                 if (temp[i] == '(' || temp[i] == ')')
186                         temp[i] = '_';
187                 if (g_ascii_isprint(temp[i]) == FALSE)
188                         temp[i] = '_';
189                 temp[i] = g_ascii_tolower(temp[i]);
190         }
191
192         element = find_element(data, network->identifier);
193         if (element == NULL) {
194                 const char *security;
195
196                 element = connman_element_create(temp);
197
198                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
199                 element->index = parent->index;
200
201                 data->list = g_slist_append(data->list, element);
202
203                 connman_element_add_static_property(element, "WiFi.Name",
204                                 DBUS_TYPE_STRING, &network->identifier);
205
206                 connman_element_add_static_array_property(element, "WiFi.SSID",
207                         DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
208
209                 if (network->has_rsn == TRUE)
210                         security = "wpa2";
211                 else if (network->has_wpa == TRUE)
212                         security = "wpa";
213                 else if (network->has_wep == TRUE)
214                         security = "wep";
215                 else
216                         security = "none";
217
218                 connman_element_add_static_property(element, "WiFi.Security",
219                                                 DBUS_TYPE_STRING, &security);
220
221                 connman_element_register(element, parent);
222         }
223
224         g_free(temp);
225 }
226
227 static struct supplicant_callback wifi_callback = {
228         .state_change   = state_change,
229         .scan_result    = scan_result,
230 };
231
232 static int wifi_probe(struct connman_element *element)
233 {
234         struct wifi_data *data;
235
236         DBG("element %p name %s", element, element->name);
237
238         data = g_try_new0(struct wifi_data, 1);
239         if (data == NULL)
240                 return -ENOMEM;
241
242         connman_element_set_data(element, data);
243
244         return 0;
245 }
246
247 static void wifi_remove(struct connman_element *element)
248 {
249         struct wifi_data *data = connman_element_get_data(element);
250
251         DBG("element %p name %s", element, element->name);
252
253         connman_element_set_data(element, NULL);
254
255         g_free(data->identifier);
256         g_free(data);
257 }
258
259 static int wifi_update(struct connman_element *element)
260 {
261         DBG("element %p name %s", element, element->name);
262
263         __supplicant_scan(element);
264
265         return 0;
266 }
267
268 static int wifi_enable(struct connman_element *element)
269 {
270         int err;
271
272         DBG("element %p name %s", element, element->name);
273
274         err = __supplicant_start(element, &wifi_callback);
275         if (err < 0)
276                 return err;
277
278         __supplicant_scan(element);
279
280         return 0;
281 }
282
283 static int wifi_disable(struct connman_element *element)
284 {
285         struct wifi_data *data = connman_element_get_data(element);
286         GSList *list;
287
288         DBG("element %p name %s", element, element->name);
289
290         __supplicant_disconnect(element);
291
292         for (list = data->list; list; list = list->next) {
293                 struct connman_element *network = list->data;
294
295                 connman_element_unref(network);
296         }
297
298         g_slist_free(data->list);
299         data->list = NULL;
300
301         connman_element_unregister_children(element);
302
303         return 0;
304 }
305
306 static struct connman_driver wifi_driver = {
307         .name           = "wifi-device",
308         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
309         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
310         .probe          = wifi_probe,
311         .remove         = wifi_remove,
312         .update         = wifi_update,
313         .enable         = wifi_enable,
314         .disable        = wifi_disable,
315 };
316
317 static GSList *device_list = NULL;
318
319 static void wifi_newlink(unsigned short type, int index,
320                                         unsigned flags, unsigned change)
321 {
322         struct connman_element *device;
323         GSList *list;
324         gboolean exists = FALSE;
325         gchar *name;
326         struct iwreq iwr;
327         int sk;
328
329         DBG("index %d", index);
330
331         if (type != ARPHRD_ETHER)
332                 return;
333
334         name = inet_index2name(index);
335
336         memset(&iwr, 0, sizeof(iwr));
337         strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
338
339         sk = socket(PF_INET, SOCK_DGRAM, 0);
340
341         if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
342                 g_free(name);
343                 close(sk);
344                 return;
345         }
346
347         close(sk);
348
349         for (list = device_list; list; list = list->next) {
350                 struct connman_element *device = list->data;
351
352                 if (device->index == index) {
353                         exists = TRUE;
354                         break;
355                 }
356         }
357
358         if (exists == TRUE) {
359                 g_free(name);
360                 return;
361         }
362
363         device = connman_element_create(NULL);
364         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
365         device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
366
367         device->index = index;
368         device->name = name;
369
370         connman_element_register(device, NULL);
371         device_list = g_slist_append(device_list, device);
372 }
373
374 static void wifi_dellink(unsigned short type, int index,
375                                         unsigned flags, unsigned change)
376 {
377         GSList *list;
378
379         DBG("index %d", index);
380
381         for (list = device_list; list; list = list->next) {
382                 struct connman_element *device = list->data;
383
384                 if (device->index == index) {
385                         device_list = g_slist_remove(device_list, device);
386                         connman_element_unregister(device);
387                         connman_element_unref(device);
388                         break;
389                 }
390         }
391 }
392
393 static struct connman_rtnl wifi_rtnl = {
394         .name           = "wifi",
395         .newlink        = wifi_newlink,
396         .dellink        = wifi_dellink,
397 };
398
399 static void supplicant_connect(DBusConnection *connection, void *user_data)
400 {
401         DBG("connection %p", connection);
402
403         __supplicant_init(connection);
404
405         if (connman_rtnl_register(&wifi_rtnl) < 0)
406                 return;
407
408         connman_rtnl_send_getlink();
409 }
410
411 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
412 {
413         GSList *list;
414
415         DBG("connection %p", connection);
416
417         connman_rtnl_unregister(&wifi_rtnl);
418
419         for (list = device_list; list; list = list->next) {
420                 struct connman_element *device = list->data;
421
422                 connman_element_unregister(device);
423                 connman_element_unref(device);
424         }
425
426         g_slist_free(device_list);
427         device_list = NULL;
428
429         __supplicant_exit();
430 }
431
432 static DBusConnection *connection;
433 static guint watch;
434
435 static int wifi_init(void)
436 {
437         int err;
438
439         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
440         if (connection == NULL)
441                 return -EIO;
442
443         err = connman_driver_register(&network_driver);
444         if (err < 0) {
445                 dbus_connection_unref(connection);
446                 return err;
447         }
448
449         err = connman_driver_register(&wifi_driver);
450         if (err < 0) {
451                 connman_driver_unregister(&network_driver);
452                 dbus_connection_unref(connection);
453                 return err;
454         }
455
456         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
457                         supplicant_connect, supplicant_disconnect, NULL, NULL);
458
459         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
460                 supplicant_connect(connection, NULL);
461
462         return 0;
463 }
464
465 static void wifi_exit(void)
466 {
467         connman_driver_unregister(&network_driver);
468         connman_driver_unregister(&wifi_driver);
469
470         if (watch > 0)
471                 g_dbus_remove_watch(connection, watch);
472
473         supplicant_disconnect(connection, NULL);
474
475         dbus_connection_unref(connection);
476 }
477
478 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
479                                                         wifi_init, wifi_exit)