Don't overwrite security settings if already set
[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         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] == '.' || temp[i] == '-')
189                         temp[i] = '_';
190                 else if (temp[i] == '(' || temp[i] == ')')
191                         temp[i] = '_';
192                 else if (g_ascii_isprint(temp[i]) == FALSE)
193                         temp[i] = '_';
194                 temp[i] = g_ascii_tolower(temp[i]);
195         }
196
197         element = find_element(data, network->identifier);
198         if (element == NULL) {
199                 guint8 strength;
200
201                 element = connman_element_create(temp);
202
203                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
204                 element->index = parent->index;
205
206                 data->list = g_slist_append(data->list, element);
207
208                 connman_element_add_static_property(element, "WiFi.Name",
209                                 DBUS_TYPE_STRING, &network->identifier);
210
211                 connman_element_add_static_array_property(element, "WiFi.SSID",
212                         DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
213
214                 if (element->wifi.security == NULL) {
215                         const char *security;
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                         element->wifi.security = g_strdup(security);
227                 }
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                                         element->wifi.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)