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