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