Only try to connect to available networks
[framework/connectivity/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_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         if (data->cleanup_timer > 0) {
174                 g_source_remove(data->cleanup_timer);
175                 data->cleanup_timer = 0;
176         }
177
178         __supplicant_scan(device);
179
180         data->inactive_timer = 0;
181
182         return FALSE;
183 }
184
185 static void connect_known_networks(struct connman_element *device)
186 {
187         struct wifi_data *data = connman_element_get_data(device);
188         GSList *list;
189
190         DBG("");
191
192         if (data->inactive_timer > 0) {
193                 g_source_remove(data->inactive_timer);
194                 data->inactive_timer = 0;
195         }
196
197         for (list = data->current; list; list = list->next) {
198                 struct connman_element *element = list->data;
199
200                 if (element->policy == CONNMAN_ELEMENT_POLICY_AUTO &&
201                                                 element->remember == TRUE &&
202                                                 element->available == TRUE) {
203                         if (network_enable(element) == 0)
204                                 return;
205                 }
206         }
207
208         data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
209                                                         inactive_scan, device);
210 }
211
212 static void state_change(struct connman_element *device,
213                                                 enum supplicant_state state)
214 {
215         struct wifi_data *data = connman_element_get_data(device);
216         struct connman_element *element;
217
218         DBG("state %d", state);
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                 connman_element_register(dhcp, element);
242         } else if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) {
243                 data->connected = FALSE;
244                 connman_element_set_enabled(element, FALSE);
245
246                 connman_element_unregister_children(element);
247         }
248
249 reconnect:
250         if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) {
251                 data->connected = FALSE;
252                 connect_known_networks(device);
253         }
254 }
255
256 static gboolean cleanup_pending(gpointer user_data)
257 {
258         struct wifi_data *data = user_data;
259         GSList *list;
260
261         DBG("");
262
263         for (list = data->pending; list; list = list->next) {
264                 struct connman_element *element = list->data;
265
266                 DBG("element %p name %s", element, element->name);
267
268                 connman_element_unregister(element);
269                 connman_element_unref(element);
270         }
271
272         g_slist_free(data->pending);
273         data->pending = NULL;
274
275         data->cleanup_timer = 0;
276
277         return FALSE;
278 }
279
280 static void clear_results(struct connman_element *device)
281 {
282         struct wifi_data *data = connman_element_get_data(device);
283
284         DBG("pending %d", g_slist_length(data->pending));
285         DBG("current %d", g_slist_length(data->current));
286
287         data->pending = data->current;
288         data->current = NULL;
289
290         if (data->cleanup_timer > 0)
291                 return;
292
293         data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
294                                                         cleanup_pending, data);
295 }
296
297 static void scan_result(struct connman_element *device,
298                                         struct supplicant_network *network)
299 {
300         struct wifi_data *data = connman_element_get_data(device);
301         struct connman_element *element;
302         gchar *temp;
303         int i;
304
305         DBG("network %p identifier %s", network, network->identifier);
306
307         if (data == NULL)
308                 return;
309
310         if (network->identifier == NULL)
311                 return;
312
313         if (network->identifier[0] == '\0')
314                 return;
315
316         temp = g_strdup(network->identifier);
317
318         for (i = 0; i < strlen(temp); i++) {
319                 gchar tmp = g_ascii_tolower(temp[i]);
320
321                 if (tmp < 'a' || tmp > 'z')
322                         temp[i] = '_';
323         }
324
325         element = find_pending_element(data, network->identifier);
326         if (element == NULL) {
327                 guint8 strength;
328
329                 element = connman_element_create(temp);
330
331                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
332                 element->index = device->index;
333
334                 connman_element_add_static_property(element, "Name",
335                                 DBUS_TYPE_STRING, &network->identifier);
336
337                 connman_element_add_static_array_property(element, "WiFi.SSID",
338                         DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
339
340                 if (element->wifi.security == NULL) {
341                         const char *security;
342
343                         if (network->has_rsn == TRUE)
344                                 security = "wpa2";
345                         else if (network->has_wpa == TRUE)
346                                 security = "wpa";
347                         else if (network->has_wep == TRUE)
348                                 security = "wep";
349                         else
350                                 security = "none";
351
352                         element->wifi.security = g_strdup(security);
353                 }
354
355                 strength = network->quality;
356
357                 connman_element_add_static_property(element, "WiFi.Strength",
358                                                 DBUS_TYPE_BYTE, &strength);
359
360                 //connman_element_add_static_property(element, "WiFi.Noise",
361                 //                      DBUS_TYPE_INT32, &network->noise);
362
363                 DBG("%s (%s) strength %d", network->identifier,
364                                         element->wifi.security, strength);
365
366                 connman_element_register(element, device);
367         } else
368                 data->pending = g_slist_remove(data->pending, element);
369
370         data->current = g_slist_append(data->current, element);
371
372         element->available = TRUE;
373
374         g_free(temp);
375 }
376
377 static struct supplicant_callback wifi_callback = {
378         .state_change   = state_change,
379         .clear_results  = clear_results,
380         .scan_result    = scan_result,
381 };
382
383 static int wifi_probe(struct connman_element *element)
384 {
385         struct wifi_data *data;
386
387         DBG("element %p name %s", element, element->name);
388
389         data = g_try_new0(struct wifi_data, 1);
390         if (data == NULL)
391                 return -ENOMEM;
392
393         data->connected = FALSE;
394
395         connman_element_set_data(element, data);
396
397         return 0;
398 }
399
400 static void wifi_remove(struct connman_element *element)
401 {
402         struct wifi_data *data = connman_element_get_data(element);
403
404         DBG("element %p name %s", element, element->name);
405
406         connman_element_set_data(element, NULL);
407
408         g_free(data->identifier);
409         g_free(data);
410 }
411
412 static int wifi_update(struct connman_element *element)
413 {
414         DBG("element %p name %s", element, element->name);
415
416         __supplicant_scan(element);
417
418         return 0;
419 }
420
421 static int wifi_enable(struct connman_element *element)
422 {
423         int err;
424
425         DBG("element %p name %s", element, element->name);
426
427         err = __supplicant_start(element, &wifi_callback);
428         if (err < 0)
429                 return err;
430
431         __supplicant_scan(element);
432
433         return 0;
434 }
435
436 static int wifi_disable(struct connman_element *element)
437 {
438         struct wifi_data *data = connman_element_get_data(element);
439         GSList *list;
440
441         DBG("element %p name %s", element, element->name);
442
443         if (data->cleanup_timer > 0) {
444                 g_source_remove(data->cleanup_timer);
445                 data->cleanup_timer = 0;
446         }
447
448         if (data->inactive_timer > 0) {
449                 g_source_remove(data->inactive_timer);
450                 data->inactive_timer = 0;
451         }
452
453         __supplicant_disconnect(element);
454
455         for (list = data->pending; list; list = list->next) {
456                 struct connman_element *network = list->data;
457
458                 connman_element_unref(network);
459         }
460
461         g_slist_free(data->pending);
462         data->pending = NULL;
463
464         for (list = data->current; list; list = list->next) {
465                 struct connman_element *network = list->data;
466
467                 connman_element_unref(network);
468         }
469
470         g_slist_free(data->current);
471         data->current = NULL;
472
473         connman_element_unregister_children(element);
474
475         __supplicant_stop(element);
476
477         return 0;
478 }
479
480 static struct connman_driver wifi_driver = {
481         .name           = "wifi-device",
482         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
483         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
484         .probe          = wifi_probe,
485         .remove         = wifi_remove,
486         .update         = wifi_update,
487         .enable         = wifi_enable,
488         .disable        = wifi_disable,
489 };
490
491 static GSList *device_list = NULL;
492
493 static void wifi_newlink(unsigned short type, int index,
494                                         unsigned flags, unsigned change)
495 {
496         struct connman_element *device;
497         GSList *list;
498         gboolean exists = FALSE;
499         gchar *name, *devname;
500         struct iwreq iwr;
501         int sk;
502
503         DBG("index %d", index);
504
505         if (type != ARPHRD_ETHER)
506                 return;
507
508         name = inet_index2ident(index, "dev_");
509         devname = inet_index2name(index);
510
511         memset(&iwr, 0, sizeof(iwr));
512         strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
513
514         sk = socket(PF_INET, SOCK_DGRAM, 0);
515
516         if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
517                 g_free(name);
518                 close(sk);
519                 return;
520         }
521
522         close(sk);
523
524         for (list = device_list; list; list = list->next) {
525                 struct connman_element *device = list->data;
526
527                 if (device->index == index) {
528                         exists = TRUE;
529                         break;
530                 }
531         }
532
533         if (exists == TRUE) {
534                 g_free(name);
535                 return;
536         }
537
538         device = connman_element_create(NULL);
539         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
540         device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
541
542         device->index = index;
543         device->name = name;
544         device->devname = devname;
545
546         connman_element_register(device, NULL);
547         device_list = g_slist_append(device_list, device);
548 }
549
550 static void wifi_dellink(unsigned short type, int index,
551                                         unsigned flags, unsigned change)
552 {
553         GSList *list;
554
555         DBG("index %d", index);
556
557         for (list = device_list; list; list = list->next) {
558                 struct connman_element *device = list->data;
559
560                 if (device->index == index) {
561                         device_list = g_slist_remove(device_list, device);
562                         connman_element_unregister(device);
563                         connman_element_unref(device);
564                         break;
565                 }
566         }
567 }
568
569 static struct connman_rtnl wifi_rtnl = {
570         .name           = "wifi",
571         .newlink        = wifi_newlink,
572         .dellink        = wifi_dellink,
573 };
574
575 static void supplicant_connect(DBusConnection *connection, void *user_data)
576 {
577         DBG("connection %p", connection);
578
579         __supplicant_init(connection);
580
581         if (connman_rtnl_register(&wifi_rtnl) < 0)
582                 return;
583
584         connman_rtnl_send_getlink();
585 }
586
587 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
588 {
589         GSList *list;
590
591         DBG("connection %p", connection);
592
593         connman_rtnl_unregister(&wifi_rtnl);
594
595         for (list = device_list; list; list = list->next) {
596                 struct connman_element *device = list->data;
597
598                 connman_element_unregister(device);
599                 connman_element_unref(device);
600         }
601
602         g_slist_free(device_list);
603         device_list = NULL;
604
605         __supplicant_exit();
606 }
607
608 static DBusConnection *connection;
609 static guint watch;
610
611 static int wifi_init(void)
612 {
613         int err;
614
615         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
616         if (connection == NULL)
617                 return -EIO;
618
619         err = connman_driver_register(&network_driver);
620         if (err < 0) {
621                 dbus_connection_unref(connection);
622                 return err;
623         }
624
625         err = connman_driver_register(&wifi_driver);
626         if (err < 0) {
627                 connman_driver_unregister(&network_driver);
628                 dbus_connection_unref(connection);
629                 return err;
630         }
631
632         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
633                         supplicant_connect, supplicant_disconnect, NULL, NULL);
634
635         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
636                 supplicant_connect(connection, NULL);
637
638         return 0;
639 }
640
641 static void wifi_exit(void)
642 {
643         connman_driver_unregister(&network_driver);
644         connman_driver_unregister(&wifi_driver);
645
646         if (watch > 0)
647                 g_dbus_remove_watch(connection, watch);
648
649         supplicant_disconnect(connection, NULL);
650
651         dbus_connection_unref(connection);
652 }
653
654 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
655                                                         wifi_init, wifi_exit)