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"
49 static int network_probe(struct connman_element *element)
51 DBG("element %p name %s", element, element->name);
56 static void network_remove(struct connman_element *element)
58 DBG("element %p name %s", element, element->name);
61 static int network_enable(struct connman_element *element)
63 char *identifier, *passphrase = NULL;
67 DBG("element %p name %s", element, element->name);
69 if (connman_element_get_static_property(element,
70 "WiFi.Name", &identifier) == FALSE)
73 if (connman_element_get_static_array_property(element,
74 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
77 if (element->parent != NULL) {
78 struct wifi_data *data = connman_element_get_data(element->parent);
81 g_free(data->identifier);
82 data->identifier = g_strdup(identifier);
86 connman_element_get_value(element,
87 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
89 DBG("identifier %s passhprase %s", identifier, passphrase);
91 if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
92 connman_error("Failed to initiate connect");
97 static int network_disable(struct connman_element *element)
99 DBG("element %p name %s", element, element->name);
101 connman_element_unregister_children(element);
103 __supplicant_disconnect(element);
108 static struct connman_driver network_driver = {
109 .name = "wifi-network",
110 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
111 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
112 .probe = network_probe,
113 .remove = network_remove,
114 .enable = network_enable,
115 .disable = network_disable,
118 static struct connman_element *find_element(struct wifi_data *data,
119 const char *identifier)
123 for (list = data->list; list; list = list->next) {
124 struct connman_element *element = list->data;
126 if (connman_element_match_static_property(element,
127 "WiFi.Name", &identifier) == TRUE)
134 static void state_change(struct connman_element *parent,
135 enum supplicant_state state)
137 struct wifi_data *data = connman_element_get_data(parent);
138 struct connman_element *element;
140 DBG("state %d", state);
142 if (data->identifier == NULL)
145 element = find_element(data, data->identifier);
149 if (state == STATE_COMPLETED) {
150 struct connman_element *dhcp;
152 dhcp = connman_element_create(NULL);
154 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
155 dhcp->index = element->index;
157 connman_element_register(dhcp, element);
161 static void scan_result(struct connman_element *parent,
162 struct supplicant_network *network)
164 struct wifi_data *data = connman_element_get_data(parent);
165 struct connman_element *element;
169 DBG("network %p identifier %s", network, network->identifier);
174 if (network->identifier == NULL)
177 if (network->identifier[0] == '\0')
180 temp = g_strdup(network->identifier);
182 for (i = 0; i < strlen(temp); i++) {
183 if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
185 else if (temp[i] == '(' || temp[i] == ')')
187 else if (g_ascii_isprint(temp[i]) == FALSE)
189 temp[i] = g_ascii_tolower(temp[i]);
192 element = find_element(data, network->identifier);
193 if (element == NULL) {
194 const char *security;
197 element = connman_element_create(temp);
199 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
200 element->index = parent->index;
202 data->list = g_slist_append(data->list, element);
204 connman_element_add_static_property(element, "WiFi.Name",
205 DBUS_TYPE_STRING, &network->identifier);
207 connman_element_add_static_array_property(element, "WiFi.SSID",
208 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
210 if (network->has_rsn == TRUE)
212 else if (network->has_wpa == TRUE)
214 else if (network->has_wep == TRUE)
219 connman_element_add_static_property(element, "WiFi.Security",
220 DBUS_TYPE_STRING, &security);
222 strength = network->quality;
224 connman_element_add_static_property(element, "WiFi.Strength",
225 DBUS_TYPE_BYTE, &strength);
227 connman_element_add_static_property(element, "WiFi.Noise",
228 DBUS_TYPE_INT32, &network->noise);
230 DBG("%s (%s) strength %d", network->identifier,
233 connman_element_register(element, parent);
239 static struct supplicant_callback wifi_callback = {
240 .state_change = state_change,
241 .scan_result = scan_result,
244 static int wifi_probe(struct connman_element *element)
246 struct wifi_data *data;
248 DBG("element %p name %s", element, element->name);
250 data = g_try_new0(struct wifi_data, 1);
254 connman_element_set_data(element, data);
259 static void wifi_remove(struct connman_element *element)
261 struct wifi_data *data = connman_element_get_data(element);
263 DBG("element %p name %s", element, element->name);
265 connman_element_set_data(element, NULL);
267 g_free(data->identifier);
271 static int wifi_update(struct connman_element *element)
273 DBG("element %p name %s", element, element->name);
275 __supplicant_scan(element);
280 static int wifi_enable(struct connman_element *element)
284 DBG("element %p name %s", element, element->name);
286 err = __supplicant_start(element, &wifi_callback);
290 __supplicant_scan(element);
295 static int wifi_disable(struct connman_element *element)
297 struct wifi_data *data = connman_element_get_data(element);
300 DBG("element %p name %s", element, element->name);
302 __supplicant_disconnect(element);
304 for (list = data->list; list; list = list->next) {
305 struct connman_element *network = list->data;
307 connman_element_unref(network);
310 g_slist_free(data->list);
313 connman_element_unregister_children(element);
318 static struct connman_driver wifi_driver = {
319 .name = "wifi-device",
320 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
321 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
323 .remove = wifi_remove,
324 .update = wifi_update,
325 .enable = wifi_enable,
326 .disable = wifi_disable,
329 static GSList *device_list = NULL;
331 static void wifi_newlink(unsigned short type, int index,
332 unsigned flags, unsigned change)
334 struct connman_element *device;
336 gboolean exists = FALSE;
341 DBG("index %d", index);
343 if (type != ARPHRD_ETHER)
346 name = inet_index2name(index);
348 memset(&iwr, 0, sizeof(iwr));
349 strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
351 sk = socket(PF_INET, SOCK_DGRAM, 0);
353 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
361 for (list = device_list; list; list = list->next) {
362 struct connman_element *device = list->data;
364 if (device->index == index) {
370 if (exists == TRUE) {
375 device = connman_element_create(NULL);
376 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
377 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
379 device->index = index;
382 connman_element_register(device, NULL);
383 device_list = g_slist_append(device_list, device);
386 static void wifi_dellink(unsigned short type, int index,
387 unsigned flags, unsigned change)
391 DBG("index %d", index);
393 for (list = device_list; list; list = list->next) {
394 struct connman_element *device = list->data;
396 if (device->index == index) {
397 device_list = g_slist_remove(device_list, device);
398 connman_element_unregister(device);
399 connman_element_unref(device);
405 static struct connman_rtnl wifi_rtnl = {
407 .newlink = wifi_newlink,
408 .dellink = wifi_dellink,
411 static void supplicant_connect(DBusConnection *connection, void *user_data)
413 DBG("connection %p", connection);
415 __supplicant_init(connection);
417 if (connman_rtnl_register(&wifi_rtnl) < 0)
420 connman_rtnl_send_getlink();
423 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
427 DBG("connection %p", connection);
429 connman_rtnl_unregister(&wifi_rtnl);
431 for (list = device_list; list; list = list->next) {
432 struct connman_element *device = list->data;
434 connman_element_unregister(device);
435 connman_element_unref(device);
438 g_slist_free(device_list);
444 static DBusConnection *connection;
447 static int wifi_init(void)
451 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
452 if (connection == NULL)
455 err = connman_driver_register(&network_driver);
457 dbus_connection_unref(connection);
461 err = connman_driver_register(&wifi_driver);
463 connman_driver_unregister(&network_driver);
464 dbus_connection_unref(connection);
468 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
469 supplicant_connect, supplicant_disconnect, NULL, NULL);
471 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
472 supplicant_connect(connection, NULL);
477 static void wifi_exit(void)
479 connman_driver_unregister(&network_driver);
480 connman_driver_unregister(&wifi_driver);
483 g_dbus_remove_watch(connection, watch);
485 supplicant_disconnect(connection, NULL);
487 dbus_connection_unref(connection);
490 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
491 wifi_init, wifi_exit)