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 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/driver.h>
39 #include <connman/rtnl.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
44 #include "supplicant.h"
46 #define CLEANUP_TIMEOUT 8 /* in seconds */
47 #define INACTIVE_TIMEOUT 12 /* in seconds */
58 static int network_probe(struct connman_element *element)
60 DBG("element %p name %s", element, element->name);
65 static void network_remove(struct connman_element *element)
67 DBG("element %p name %s", element, element->name);
70 static int network_enable(struct connman_element *element)
72 struct connman_device *device = (struct connman_device *) element->parent;
73 char *name, *security = NULL, *passphrase = NULL;
77 DBG("element %p name %s", element, element->name);
79 if (connman_element_get_static_property(element,
80 "Name", &name) == FALSE)
83 if (connman_element_get_static_array_property(element,
84 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
88 struct wifi_data *data = connman_device_get_data(device);
91 if (data->connected == TRUE)
94 g_free(data->identifier);
95 data->identifier = g_strdup(name);
99 connman_element_get_value(element,
100 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
102 connman_element_get_value(element,
103 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
105 DBG("name %s security %s passhprase %s",
106 name, security, passphrase);
108 if (__supplicant_connect(element, ssid, ssid_len,
109 security, passphrase) < 0)
110 connman_error("Failed to initiate connect");
115 static int network_disable(struct connman_element *element)
117 DBG("element %p name %s", element, element->name);
119 connman_element_unregister_children(element);
121 __supplicant_disconnect(element);
126 static struct connman_driver network_driver = {
127 .name = "wifi-network",
128 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
129 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
130 .probe = network_probe,
131 .remove = network_remove,
132 .enable = network_enable,
133 .disable = network_disable,
136 static struct connman_element *find_current_element(struct wifi_data *data,
137 const char *identifier)
141 for (list = data->current; list; list = list->next) {
142 struct connman_element *element = list->data;
144 if (connman_element_match_static_property(element,
145 "Name", &identifier) == TRUE)
152 static struct connman_element *find_pending_element(struct wifi_data *data,
153 const char *identifier)
157 for (list = data->pending; list; list = list->next) {
158 struct connman_element *element = list->data;
160 if (connman_element_match_static_property(element,
161 "Name", &identifier) == TRUE)
168 static gboolean inactive_scan(gpointer user_data)
170 struct connman_device *device = user_data;
171 struct wifi_data *data = connman_device_get_data(device);
173 DBG("device %p", device);
175 __supplicant_scan(device);
177 data->inactive_timer = 0;
182 static void connect_known_networks(struct connman_device *device)
184 struct wifi_data *data = connman_device_get_data(device);
187 DBG("device %p", device);
189 if (data->inactive_timer > 0) {
190 g_source_remove(data->inactive_timer);
191 data->inactive_timer = 0;
194 for (list = data->current; list; list = list->next) {
195 struct connman_element *element = list->data;
197 if (element->policy == CONNMAN_ELEMENT_POLICY_AUTO &&
198 element->remember == TRUE &&
199 element->available == TRUE) {
200 if (network_enable(element) == 0)
205 data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
206 inactive_scan, device);
209 static void state_change(struct connman_device *device,
210 enum supplicant_state state)
212 struct wifi_data *data = connman_device_get_data(device);
213 struct connman_element *element;
215 DBG("device %p state %d", device, state);
217 if (state == STATE_SCANNING)
218 connman_device_set_scanning(device, TRUE);
220 connman_device_set_scanning(device, FALSE);
225 DBG("identifier %s", data->identifier);
227 if (data->identifier == NULL)
230 element = find_current_element(data, data->identifier);
234 if (state == STATE_COMPLETED && data->connected == FALSE) {
235 struct connman_element *dhcp;
237 data->connected = TRUE;
238 connman_element_set_enabled(element, TRUE);
240 dhcp = connman_element_create(NULL);
242 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
243 dhcp->index = element->index;
245 if (connman_element_register(dhcp, element) < 0)
246 connman_element_unref(dhcp);
247 } else if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) {
248 data->connected = FALSE;
249 connman_element_set_enabled(element, FALSE);
251 connman_element_unregister_children(element);
255 if (state == STATE_INACTIVE) {
256 data->connected = FALSE;
257 connect_known_networks(device);
261 static gboolean cleanup_pending(gpointer user_data)
263 struct wifi_data *data = user_data;
268 for (list = data->pending; list; list = list->next) {
269 struct connman_element *element = list->data;
271 DBG("element %p name %s", element, element->name);
273 connman_element_unregister(element);
274 connman_element_unref(element);
277 g_slist_free(data->pending);
278 data->pending = NULL;
280 data->cleanup_timer = 0;
285 static void clear_results(struct connman_device *device)
287 struct wifi_data *data = connman_device_get_data(device);
289 DBG("pending %d", g_slist_length(data->pending));
290 DBG("current %d", g_slist_length(data->current));
292 if (data->cleanup_timer > 0) {
293 g_source_remove(data->cleanup_timer);
294 cleanup_pending(data);
297 data->pending = data->current;
298 data->current = NULL;
300 data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
301 cleanup_pending, data);
304 static void scan_result(struct connman_device *device,
305 struct supplicant_network *network)
307 struct wifi_data *data = connman_device_get_data(device);
308 struct connman_element *element;
312 DBG("device %p identifier %s", device, network->identifier);
317 if (network->identifier == NULL)
320 if (network->identifier[0] == '\0')
323 temp = g_strdup(network->identifier);
325 for (i = 0; i < strlen(temp); i++) {
327 if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
328 (tmp < 'a' || tmp > 'z'))
332 element = find_pending_element(data, network->identifier);
333 if (element == NULL) {
336 element = connman_element_create(temp);
338 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
339 element->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
340 element->index = connman_device_get_index(device);
342 connman_element_add_static_property(element, "Name",
343 DBUS_TYPE_STRING, &network->identifier);
345 connman_element_add_static_array_property(element, "WiFi.SSID",
346 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
348 mode = (network->adhoc == TRUE) ? "adhoc" : "managed";
349 connman_element_add_static_property(element, "WiFi.Mode",
350 DBUS_TYPE_STRING, &mode);
352 if (element->wifi.security == NULL) {
353 const char *security;
355 if (network->has_rsn == TRUE)
357 else if (network->has_wpa == TRUE)
359 else if (network->has_wep == TRUE)
364 element->wifi.security = g_strdup(security);
367 element->strength = network->quality;
369 connman_element_add_static_property(element, "Strength",
370 DBUS_TYPE_BYTE, &element->strength);
372 DBG("%s (%s %s) strength %d", network->identifier, mode,
373 element->wifi.security, element->strength);
375 if (connman_element_register(element,
376 (struct connman_element *) device) < 0) {
377 connman_element_unref(element);
381 data->pending = g_slist_remove(data->pending, element);
383 if (element->strength != network->quality) {
384 element->strength = network->quality;
386 connman_element_set_static_property(element, "Strength",
387 DBUS_TYPE_BYTE, &element->strength);
389 connman_element_update(element);
393 data->current = g_slist_append(data->current, element);
395 element->available = TRUE;
401 static struct supplicant_callback wifi_callback = {
402 .state_change = state_change,
403 .clear_results = clear_results,
404 .scan_result = scan_result,
407 static int wifi_probe(struct connman_device *device)
409 struct wifi_data *data;
411 DBG("device %p", device);
413 data = g_try_new0(struct wifi_data, 1);
417 data->connected = FALSE;
419 connman_device_set_data(device, data);
424 static void wifi_remove(struct connman_device *device)
426 struct wifi_data *data = connman_device_get_data(device);
428 DBG("device %p", device);
430 connman_device_set_data(device, NULL);
432 g_free(data->identifier);
436 static int wifi_enable(struct connman_device *device)
440 DBG("device %p", device);
442 err = __supplicant_start(device, &wifi_callback);
446 connman_device_set_powered(device, TRUE);
448 __supplicant_scan(device);
453 static int wifi_disable(struct connman_device *device)
455 struct wifi_data *data = connman_device_get_data(device);
458 DBG("device %p", device);
460 if (data->cleanup_timer > 0) {
461 g_source_remove(data->cleanup_timer);
462 cleanup_pending(data);
465 if (data->inactive_timer > 0) {
466 g_source_remove(data->inactive_timer);
467 data->inactive_timer = 0;
470 for (list = data->current; list; list = list->next) {
471 struct connman_element *network = list->data;
473 if (network->enabled == TRUE)
474 __supplicant_disconnect(network);
476 connman_element_unref(network);
479 g_slist_free(data->current);
480 data->current = NULL;
482 connman_element_unregister_children((struct connman_element *) device);
484 __supplicant_stop(device);
486 connman_device_set_powered(device, FALSE);
491 static int wifi_scan(struct connman_device *device)
493 DBG("device %p", device);
495 __supplicant_scan(device);
500 static struct connman_device_driver wifi_driver = {
501 .name = "wifi-device",
502 .type = CONNMAN_DEVICE_TYPE_WIFI,
504 .remove = wifi_remove,
505 .enable = wifi_enable,
506 .disable = wifi_disable,
510 static GSList *device_list = NULL;
512 static void wifi_newlink(unsigned short type, int index,
513 unsigned flags, unsigned change)
515 struct connman_device *device;
517 gboolean exists = FALSE;
518 gchar *name, *devname;
522 DBG("index %d", index);
524 if (type != ARPHRD_ETHER)
527 devname = inet_index2name(index);
529 memset(&iwr, 0, sizeof(iwr));
530 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
532 sk = socket(PF_INET, SOCK_DGRAM, 0);
534 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
542 for (list = device_list; list; list = list->next) {
543 struct connman_device *device = list->data;
545 if (connman_device_get_index(device) == index) {
551 if (exists == TRUE) {
556 name = inet_index2ident(index, "dev_");
558 device = connman_device_create(name, CONNMAN_DEVICE_TYPE_WIFI);
559 if (device == NULL) {
565 connman_device_set_index(device, index);
566 connman_device_set_interface(device, devname);
571 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_SINGLE);
573 if (connman_device_register(device) < 0) {
574 connman_device_unregister(device);
578 device_list = g_slist_append(device_list, device);
581 static void wifi_dellink(unsigned short type, int index,
582 unsigned flags, unsigned change)
586 DBG("index %d", index);
588 for (list = device_list; list; list = list->next) {
589 struct connman_device *device = list->data;
591 if (connman_device_get_index(device) == index) {
592 device_list = g_slist_remove(device_list, device);
593 connman_device_unregister(device);
594 connman_device_unref(device);
600 static struct connman_rtnl wifi_rtnl = {
602 .newlink = wifi_newlink,
603 .dellink = wifi_dellink,
606 static void supplicant_connect(DBusConnection *connection, void *user_data)
608 DBG("connection %p", connection);
610 __supplicant_init(connection);
612 if (connman_rtnl_register(&wifi_rtnl) < 0)
615 connman_rtnl_send_getlink();
618 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
622 DBG("connection %p", connection);
624 connman_rtnl_unregister(&wifi_rtnl);
626 for (list = device_list; list; list = list->next) {
627 struct connman_device *device = list->data;
629 connman_device_unregister(device);
630 connman_device_unref(device);
633 g_slist_free(device_list);
639 static DBusConnection *connection;
642 static int wifi_init(void)
646 connection = connman_dbus_get_connection();
647 if (connection == NULL)
650 err = connman_driver_register(&network_driver);
652 dbus_connection_unref(connection);
656 err = connman_device_driver_register(&wifi_driver);
658 connman_driver_unregister(&network_driver);
659 dbus_connection_unref(connection);
663 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
664 supplicant_connect, supplicant_disconnect, NULL, NULL);
666 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
667 supplicant_connect(connection, NULL);
669 __supplicant_activate(connection);
674 static void wifi_exit(void)
676 connman_driver_unregister(&network_driver);
677 connman_device_driver_unregister(&wifi_driver);
680 g_dbus_remove_watch(connection, watch);
682 supplicant_disconnect(connection, NULL);
684 dbus_connection_unref(connection);
687 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
688 wifi_init, wifi_exit)