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