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