Replace more unallowed characters
[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 struct wifi_data {
45         GSList *list;
46         gchar *identifier;
47 };
48
49 static int network_probe(struct connman_element *element)
50 {
51         DBG("element %p name %s", element, element->name);
52
53         return 0;
54 }
55
56 static void network_remove(struct connman_element *element)
57 {
58         DBG("element %p name %s", element, element->name);
59 }
60
61 static int network_enable(struct connman_element *element)
62 {
63         char *identifier, *security = NULL, *passphrase = NULL;
64         unsigned char *ssid;
65         int ssid_len;
66
67         DBG("element %p name %s", element, element->name);
68
69         if (connman_element_get_static_property(element,
70                                         "WiFi.Name", &identifier) == FALSE)
71                 return -EIO;
72
73         if (connman_element_get_static_array_property(element,
74                                 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
75                 return -EIO;
76
77         if (element->parent != NULL) {
78                 struct wifi_data *data = connman_element_get_data(element->parent);
79
80                 if (data != NULL) {
81                         g_free(data->identifier);
82                         data->identifier = g_strdup(identifier);
83                 }
84         }
85
86         connman_element_get_value(element,
87                         CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
88
89         connman_element_get_value(element,
90                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
91
92         DBG("identifier %s security %s passhprase %s",
93                                         identifier, security, passphrase);
94
95         if (__supplicant_connect(element, ssid, ssid_len,
96                                                 security, passphrase) < 0)
97                 connman_error("Failed to initiate connect");
98
99         return 0;
100 }
101
102 static int network_disable(struct connman_element *element)
103 {
104         DBG("element %p name %s", element, element->name);
105
106         connman_element_unregister_children(element);
107
108         __supplicant_disconnect(element);
109
110         return 0;
111 }
112
113 static struct connman_driver network_driver = {
114         .name           = "wifi-network",
115         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
116         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
117         .probe          = network_probe,
118         .remove         = network_remove,
119         .enable         = network_enable,
120         .disable        = network_disable,
121 };
122
123 static struct connman_element *find_element(struct wifi_data *data,
124                                                 const char *identifier)
125 {
126         GSList *list;
127
128         for (list = data->list; list; list = list->next) {
129                 struct connman_element *element = list->data;
130
131                 if (connman_element_match_static_property(element,
132                                         "WiFi.Name", &identifier) == TRUE)
133                         return element;
134         }
135
136         return NULL;
137 }
138
139 static void state_change(struct connman_element *parent,
140                                                 enum supplicant_state state)
141 {
142         struct wifi_data *data = connman_element_get_data(parent);
143         struct connman_element *element;
144
145         DBG("state %d", state);
146
147         if (data->identifier == NULL)
148                 return;
149
150         element = find_element(data, data->identifier);
151         if (element == NULL)
152                 return;
153
154         if (state == STATE_COMPLETED) {
155                 struct connman_element *dhcp;
156
157                 dhcp = connman_element_create(NULL);
158
159                 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
160                 dhcp->index = element->index;
161
162                 connman_element_register(dhcp, element);
163         }
164 }
165
166 static void scan_result(struct connman_element *parent,
167                                         struct supplicant_network *network)
168 {
169         struct wifi_data *data = connman_element_get_data(parent);
170         struct connman_element *element;
171         gchar *temp;
172         int i;
173
174         DBG("network %p identifier %s", network, network->identifier);
175
176         if (data == NULL)
177                 return;
178
179         if (network->identifier == NULL)
180                 return;
181
182         if (network->identifier[0] == '\0')
183                 return;
184
185         temp = g_strdup(network->identifier);
186
187         for (i = 0; i < strlen(temp); i++) {
188                 if (temp[i] == ' ' || temp[i] == '.')
189                         temp[i] = '_';
190                 else if (temp[i] == '-' || temp[i] == '+')
191                         temp[i] = '_';
192                 else if (temp[i] == '!' || temp[i] == '?')
193                         temp[i] = '_';
194                 else if (temp[i] == '(' || temp[i] == ')')
195                         temp[i] = '_';
196                 else if (g_ascii_isprint(temp[i]) == FALSE)
197                         temp[i] = '_';
198                 temp[i] = g_ascii_tolower(temp[i]);
199         }
200
201         element = find_element(data, network->identifier);
202         if (element == NULL) {
203                 guint8 strength;
204
205                 element = connman_element_create(temp);
206
207                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
208                 element->index = parent->index;
209
210                 data->list = g_slist_append(data->list, element);
211
212                 connman_element_add_static_property(element, "WiFi.Name",
213                                 DBUS_TYPE_STRING, &network->identifier);
214
215                 connman_element_add_static_array_property(element, "WiFi.SSID",
216                         DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
217
218                 if (element->wifi.security == NULL) {
219                         const char *security;
220
221                         if (network->has_rsn == TRUE)
222                                 security = "wpa2";
223                         else if (network->has_wpa == TRUE)
224                                 security = "wpa";
225                         else if (network->has_wep == TRUE)
226                                 security = "wep";
227                         else
228                                 security = "none";
229
230                         element->wifi.security = g_strdup(security);
231                 }
232
233                 strength = network->quality;
234
235                 //connman_element_add_static_property(element, "WiFi.Strength",
236                 //                              DBUS_TYPE_BYTE, &strength);
237
238                 //connman_element_add_static_property(element, "WiFi.Noise",
239                 //                      DBUS_TYPE_INT32, &network->noise);
240
241                 DBG("%s (%s) strength %d", network->identifier,
242                                         element->wifi.security, strength);
243
244                 connman_element_register(element, parent);
245         }
246
247         g_free(temp);
248 }
249
250 static struct supplicant_callback wifi_callback = {
251         .state_change   = state_change,
252         .scan_result    = scan_result,
253 };
254
255 static int wifi_probe(struct connman_element *element)
256 {
257         struct wifi_data *data;
258
259         DBG("element %p name %s", element, element->name);
260
261         data = g_try_new0(struct wifi_data, 1);
262         if (data == NULL)
263                 return -ENOMEM;
264
265         connman_element_set_data(element, data);
266
267         return 0;
268 }
269
270 static void wifi_remove(struct connman_element *element)
271 {
272         struct wifi_data *data = connman_element_get_data(element);
273
274         DBG("element %p name %s", element, element->name);
275
276         connman_element_set_data(element, NULL);
277
278         g_free(data->identifier);
279         g_free(data);
280 }
281
282 static int wifi_update(struct connman_element *element)
283 {
284         DBG("element %p name %s", element, element->name);
285
286         __supplicant_scan(element);
287
288         return 0;
289 }
290
291 static int wifi_enable(struct connman_element *element)
292 {
293         int err;
294
295         DBG("element %p name %s", element, element->name);
296
297         err = __supplicant_start(element, &wifi_callback);
298         if (err < 0)
299                 return err;
300
301         __supplicant_scan(element);
302
303         return 0;
304 }
305
306 static int wifi_disable(struct connman_element *element)
307 {
308         struct wifi_data *data = connman_element_get_data(element);
309         GSList *list;
310
311         DBG("element %p name %s", element, element->name);
312
313         __supplicant_disconnect(element);
314
315         for (list = data->list; list; list = list->next) {
316                 struct connman_element *network = list->data;
317
318                 connman_element_unref(network);
319         }
320
321         g_slist_free(data->list);
322         data->list = NULL;
323
324         connman_element_unregister_children(element);
325
326         return 0;
327 }
328
329 static struct connman_driver wifi_driver = {
330         .name           = "wifi-device",
331         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
332         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
333         .probe          = wifi_probe,
334         .remove         = wifi_remove,
335         .update         = wifi_update,
336         .enable         = wifi_enable,
337         .disable        = wifi_disable,
338 };
339
340 static GSList *device_list = NULL;
341
342 static void wifi_newlink(unsigned short type, int index,
343                                         unsigned flags, unsigned change)
344 {
345         struct connman_element *device;
346         GSList *list;
347         gboolean exists = FALSE;
348         gchar *name;
349         struct iwreq iwr;
350         int sk;
351
352         DBG("index %d", index);
353
354         if (type != ARPHRD_ETHER)
355                 return;
356
357         name = inet_index2name(index);
358
359         memset(&iwr, 0, sizeof(iwr));
360         strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
361
362         sk = socket(PF_INET, SOCK_DGRAM, 0);
363
364         if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
365                 g_free(name);
366                 close(sk);
367                 return;
368         }
369
370         close(sk);
371
372         for (list = device_list; list; list = list->next) {
373                 struct connman_element *device = list->data;
374
375                 if (device->index == index) {
376                         exists = TRUE;
377                         break;
378                 }
379         }
380
381         if (exists == TRUE) {
382                 g_free(name);
383                 return;
384         }
385
386         device = connman_element_create(NULL);
387         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
388         device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
389
390         device->index = index;
391         device->name = name;
392
393         connman_element_register(device, NULL);
394         device_list = g_slist_append(device_list, device);
395 }
396
397 static void wifi_dellink(unsigned short type, int index,
398                                         unsigned flags, unsigned change)
399 {
400         GSList *list;
401
402         DBG("index %d", index);
403
404         for (list = device_list; list; list = list->next) {
405                 struct connman_element *device = list->data;
406
407                 if (device->index == index) {
408                         device_list = g_slist_remove(device_list, device);
409                         connman_element_unregister(device);
410                         connman_element_unref(device);
411                         break;
412                 }
413         }
414 }
415
416 static struct connman_rtnl wifi_rtnl = {
417         .name           = "wifi",
418         .newlink        = wifi_newlink,
419         .dellink        = wifi_dellink,
420 };
421
422 static void supplicant_connect(DBusConnection *connection, void *user_data)
423 {
424         DBG("connection %p", connection);
425
426         __supplicant_init(connection);
427
428         if (connman_rtnl_register(&wifi_rtnl) < 0)
429                 return;
430
431         connman_rtnl_send_getlink();
432 }
433
434 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
435 {
436         GSList *list;
437
438         DBG("connection %p", connection);
439
440         connman_rtnl_unregister(&wifi_rtnl);
441
442         for (list = device_list; list; list = list->next) {
443                 struct connman_element *device = list->data;
444
445                 connman_element_unregister(device);
446                 connman_element_unref(device);
447         }
448
449         g_slist_free(device_list);
450         device_list = NULL;
451
452         __supplicant_exit();
453 }
454
455 static DBusConnection *connection;
456 static guint watch;
457
458 static int wifi_init(void)
459 {
460         int err;
461
462         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
463         if (connection == NULL)
464                 return -EIO;
465
466         err = connman_driver_register(&network_driver);
467         if (err < 0) {
468                 dbus_connection_unref(connection);
469                 return err;
470         }
471
472         err = connman_driver_register(&wifi_driver);
473         if (err < 0) {
474                 connman_driver_unregister(&network_driver);
475                 dbus_connection_unref(connection);
476                 return err;
477         }
478
479         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
480                         supplicant_connect, supplicant_disconnect, NULL, NULL);
481
482         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
483                 supplicant_connect(connection, NULL);
484
485         return 0;
486 }
487
488 static void wifi_exit(void)
489 {
490         connman_driver_unregister(&network_driver);
491         connman_driver_unregister(&wifi_driver);
492
493         if (watch > 0)
494                 g_dbus_remove_watch(connection, watch);
495
496         supplicant_disconnect(connection, NULL);
497
498         dbus_connection_unref(connection);
499 }
500
501 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
502                                                         wifi_init, wifi_exit)