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