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