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