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