5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
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.
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.
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
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
36 #include <connman/plugin.h>
37 #include <connman/driver.h>
38 #include <connman/rtnl.h>
39 #include <connman/log.h>
42 #include "supplicant.h"
44 #define CLEANUP_TIMEOUT 8 /* in seconds */
45 #define INACTIVE_TIMEOUT 12 /* in seconds */
56 static int network_probe(struct connman_element *element)
58 DBG("element %p name %s", element, element->name);
63 static void network_remove(struct connman_element *element)
65 DBG("element %p name %s", element, element->name);
68 static int network_enable(struct connman_element *element)
70 struct connman_element *device = element->parent;
71 char *name, *security = NULL, *passphrase = NULL;
75 DBG("element %p name %s", element, element->name);
77 if (connman_element_get_static_property(element,
78 "Name", &name) == FALSE)
81 if (connman_element_get_static_array_property(element,
82 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
86 struct wifi_data *data = connman_element_get_data(device);
89 if (data->connected == TRUE)
92 g_free(data->identifier);
93 data->identifier = g_strdup(name);
97 connman_element_get_value(element,
98 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
100 connman_element_get_value(element,
101 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
103 DBG("name %s security %s passhprase %s",
104 name, security, passphrase);
106 if (__supplicant_connect(element, ssid, ssid_len,
107 security, passphrase) < 0)
108 connman_error("Failed to initiate connect");
113 static int network_disable(struct connman_element *element)
115 DBG("element %p name %s", element, element->name);
117 connman_element_unregister_children(element);
119 __supplicant_disconnect(element);
124 static struct connman_driver network_driver = {
125 .name = "wifi-network",
126 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
127 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
128 .probe = network_probe,
129 .remove = network_remove,
130 .enable = network_enable,
131 .disable = network_disable,
134 static struct connman_element *find_current_element(struct wifi_data *data,
135 const char *identifier)
139 for (list = data->current; list; list = list->next) {
140 struct connman_element *element = list->data;
142 if (connman_element_match_static_property(element,
143 "Name", &identifier) == TRUE)
150 static struct connman_element *find_pending_element(struct wifi_data *data,
151 const char *identifier)
155 for (list = data->pending; list; list = list->next) {
156 struct connman_element *element = list->data;
158 if (connman_element_match_static_property(element,
159 "Name", &identifier) == TRUE)
166 static gboolean inactive_scan(gpointer user_data)
168 struct connman_element *device = user_data;
169 struct wifi_data *data = connman_element_get_data(device);
173 if (data->cleanup_timer > 0) {
174 g_source_remove(data->cleanup_timer);
175 data->cleanup_timer = 0;
178 __supplicant_scan(device);
180 data->inactive_timer = 0;
185 static void connect_known_networks(struct connman_element *device)
187 struct wifi_data *data = connman_element_get_data(device);
192 if (data->inactive_timer > 0) {
193 g_source_remove(data->inactive_timer);
194 data->inactive_timer = 0;
197 for (list = data->current; list; list = list->next) {
198 struct connman_element *element = list->data;
200 if (element->policy == CONNMAN_ELEMENT_POLICY_AUTO &&
201 element->remember == TRUE &&
202 element->available == TRUE) {
203 if (network_enable(element) == 0)
208 data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
209 inactive_scan, device);
212 static void state_change(struct connman_element *device,
213 enum supplicant_state state)
215 struct wifi_data *data = connman_element_get_data(device);
216 struct connman_element *element;
218 DBG("state %d", state);
220 if (state == STATE_SCANNING)
221 connman_element_set_scanning(device, TRUE);
223 connman_element_set_scanning(device, FALSE);
228 if (data->identifier == NULL)
231 element = find_current_element(data, data->identifier);
235 if (state == STATE_COMPLETED) {
236 struct connman_element *dhcp;
238 data->connected = TRUE;
239 connman_element_set_enabled(element, TRUE);
241 dhcp = connman_element_create(NULL);
243 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
244 dhcp->index = element->index;
246 if (connman_element_register(dhcp, element) < 0)
247 connman_element_unref(dhcp);
248 } else if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) {
249 data->connected = FALSE;
250 connman_element_set_enabled(element, FALSE);
252 connman_element_unregister_children(element);
256 if (state == STATE_INACTIVE) {
257 data->connected = FALSE;
258 connect_known_networks(device);
262 static gboolean cleanup_pending(gpointer user_data)
264 struct wifi_data *data = user_data;
269 for (list = data->pending; list; list = list->next) {
270 struct connman_element *element = list->data;
272 DBG("element %p name %s", element, element->name);
274 connman_element_unregister(element);
275 connman_element_unref(element);
278 g_slist_free(data->pending);
279 data->pending = NULL;
281 data->cleanup_timer = 0;
286 static void clear_results(struct connman_element *device)
288 struct wifi_data *data = connman_element_get_data(device);
290 DBG("pending %d", g_slist_length(data->pending));
291 DBG("current %d", g_slist_length(data->current));
293 data->pending = data->current;
294 data->current = NULL;
296 if (data->cleanup_timer > 0)
299 data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
300 cleanup_pending, data);
303 static void scan_result(struct connman_element *device,
304 struct supplicant_network *network)
306 struct wifi_data *data = connman_element_get_data(device);
307 struct connman_element *element;
311 DBG("network %p identifier %s", network, network->identifier);
316 if (network->identifier == NULL)
319 if (network->identifier[0] == '\0')
322 temp = g_strdup(network->identifier);
324 for (i = 0; i < strlen(temp); i++) {
326 if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
327 (tmp < 'a' || tmp > 'z'))
331 element = find_pending_element(data, network->identifier);
332 if (element == NULL) {
333 element = connman_element_create(temp);
335 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
336 element->index = device->index;
338 connman_element_add_static_property(element, "Name",
339 DBUS_TYPE_STRING, &network->identifier);
341 connman_element_add_static_array_property(element, "WiFi.SSID",
342 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
344 if (element->wifi.security == NULL) {
345 const char *security;
347 if (network->has_rsn == TRUE)
349 else if (network->has_wpa == TRUE)
351 else if (network->has_wep == TRUE)
356 element->wifi.security = g_strdup(security);
359 element->strength = network->quality;
361 connman_element_add_static_property(element, "Strength",
362 DBUS_TYPE_BYTE, &element->strength);
364 DBG("%s (%s) strength %d", network->identifier,
365 element->wifi.security, element->strength);
367 if (connman_element_register(element, device) < 0) {
368 connman_element_unref(element);
372 data->pending = g_slist_remove(data->pending, element);
374 if (element->strength != network->quality) {
375 element->strength = network->quality;
377 connman_element_set_static_property(element, "Strength",
378 DBUS_TYPE_BYTE, &element->strength);
380 connman_element_update(element);
384 data->current = g_slist_append(data->current, element);
386 element->available = TRUE;
392 static struct supplicant_callback wifi_callback = {
393 .state_change = state_change,
394 .clear_results = clear_results,
395 .scan_result = scan_result,
398 static int wifi_probe(struct connman_element *element)
400 struct wifi_data *data;
402 DBG("element %p name %s", element, element->name);
404 data = g_try_new0(struct wifi_data, 1);
408 data->connected = FALSE;
410 connman_element_set_data(element, data);
415 static void wifi_remove(struct connman_element *element)
417 struct wifi_data *data = connman_element_get_data(element);
419 DBG("element %p name %s", element, element->name);
421 connman_element_set_data(element, NULL);
423 g_free(data->identifier);
427 static int wifi_update(struct connman_element *element)
429 DBG("element %p name %s", element, element->name);
431 __supplicant_scan(element);
436 static int wifi_enable(struct connman_element *element)
440 DBG("element %p name %s", element, element->name);
442 err = __supplicant_start(element, &wifi_callback);
446 __supplicant_scan(element);
451 static int wifi_disable(struct connman_element *element)
453 struct wifi_data *data = connman_element_get_data(element);
456 DBG("element %p name %s", element, element->name);
458 if (data->cleanup_timer > 0) {
459 g_source_remove(data->cleanup_timer);
460 data->cleanup_timer = 0;
463 if (data->inactive_timer > 0) {
464 g_source_remove(data->inactive_timer);
465 data->inactive_timer = 0;
468 __supplicant_disconnect(element);
470 for (list = data->pending; list; list = list->next) {
471 struct connman_element *network = list->data;
473 connman_element_unref(network);
476 g_slist_free(data->pending);
477 data->pending = NULL;
479 for (list = data->current; list; list = list->next) {
480 struct connman_element *network = list->data;
482 connman_element_unref(network);
485 g_slist_free(data->current);
486 data->current = NULL;
488 connman_element_unregister_children(element);
490 __supplicant_stop(element);
495 static struct connman_driver wifi_driver = {
496 .name = "wifi-device",
497 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
498 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
500 .remove = wifi_remove,
501 .update = wifi_update,
502 .enable = wifi_enable,
503 .disable = wifi_disable,
506 static GSList *device_list = NULL;
508 static void wifi_newlink(unsigned short type, int index,
509 unsigned flags, unsigned change)
511 struct connman_element *device;
513 gboolean exists = FALSE;
514 gchar *name, *devname;
518 DBG("index %d", index);
520 if (type != ARPHRD_ETHER)
523 name = inet_index2ident(index, "dev_");
524 devname = inet_index2name(index);
526 memset(&iwr, 0, sizeof(iwr));
527 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
529 sk = socket(PF_INET, SOCK_DGRAM, 0);
531 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
539 for (list = device_list; list; list = list->next) {
540 struct connman_element *device = list->data;
542 if (device->index == index) {
548 if (exists == TRUE) {
553 device = connman_element_create(NULL);
554 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
555 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
557 device->index = index;
559 device->devname = devname;
561 if (connman_element_register(device, NULL) < 0) {
562 connman_element_unregister(device);
566 device_list = g_slist_append(device_list, device);
569 static void wifi_dellink(unsigned short type, int index,
570 unsigned flags, unsigned change)
574 DBG("index %d", index);
576 for (list = device_list; list; list = list->next) {
577 struct connman_element *device = list->data;
579 if (device->index == index) {
580 device_list = g_slist_remove(device_list, device);
581 connman_element_unregister(device);
582 connman_element_unref(device);
588 static struct connman_rtnl wifi_rtnl = {
590 .newlink = wifi_newlink,
591 .dellink = wifi_dellink,
594 static void supplicant_connect(DBusConnection *connection, void *user_data)
596 DBG("connection %p", connection);
598 __supplicant_init(connection);
600 if (connman_rtnl_register(&wifi_rtnl) < 0)
603 connman_rtnl_send_getlink();
606 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
610 DBG("connection %p", connection);
612 connman_rtnl_unregister(&wifi_rtnl);
614 for (list = device_list; list; list = list->next) {
615 struct connman_element *device = list->data;
617 connman_element_unregister(device);
618 connman_element_unref(device);
621 g_slist_free(device_list);
627 static DBusConnection *connection;
630 static int wifi_init(void)
634 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
635 if (connection == NULL)
638 err = connman_driver_register(&network_driver);
640 dbus_connection_unref(connection);
644 err = connman_driver_register(&wifi_driver);
646 connman_driver_unregister(&network_driver);
647 dbus_connection_unref(connection);
651 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
652 supplicant_connect, supplicant_disconnect, NULL, NULL);
654 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
655 supplicant_connect(connection, NULL);
657 __supplicant_activate(connection);
662 static void wifi_exit(void)
664 connman_driver_unregister(&network_driver);
665 connman_driver_unregister(&wifi_driver);
668 g_dbus_remove_watch(connection, watch);
670 supplicant_disconnect(connection, NULL);
672 dbus_connection_unref(connection);
675 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
676 wifi_init, wifi_exit)