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