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