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, *security = NULL, *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 if (connman_element_get_static_property(element,
87 "WiFi.Security", &security) == FALSE)
88 connman_element_get_value(element,
89 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
91 connman_element_get_value(element,
92 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
94 DBG("identifier %s security %s passhprase %s",
95 identifier, security, passphrase);
97 if (__supplicant_connect(element, ssid, ssid_len,
98 security, passphrase) < 0)
99 connman_error("Failed to initiate connect");
104 static int network_disable(struct connman_element *element)
106 DBG("element %p name %s", element, element->name);
108 connman_element_unregister_children(element);
110 __supplicant_disconnect(element);
115 static struct connman_driver network_driver = {
116 .name = "wifi-network",
117 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
118 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
119 .probe = network_probe,
120 .remove = network_remove,
121 .enable = network_enable,
122 .disable = network_disable,
125 static struct connman_element *find_element(struct wifi_data *data,
126 const char *identifier)
130 for (list = data->list; list; list = list->next) {
131 struct connman_element *element = list->data;
133 if (connman_element_match_static_property(element,
134 "WiFi.Name", &identifier) == TRUE)
141 static void state_change(struct connman_element *parent,
142 enum supplicant_state state)
144 struct wifi_data *data = connman_element_get_data(parent);
145 struct connman_element *element;
147 DBG("state %d", state);
149 if (data->identifier == NULL)
152 element = find_element(data, data->identifier);
156 if (state == STATE_COMPLETED) {
157 struct connman_element *dhcp;
159 dhcp = connman_element_create(NULL);
161 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
162 dhcp->index = element->index;
164 connman_element_register(dhcp, element);
168 static void scan_result(struct connman_element *parent,
169 struct supplicant_network *network)
171 struct wifi_data *data = connman_element_get_data(parent);
172 struct connman_element *element;
176 DBG("network %p identifier %s", network, network->identifier);
181 if (network->identifier == NULL)
184 if (network->identifier[0] == '\0')
187 temp = g_strdup(network->identifier);
189 for (i = 0; i < strlen(temp); i++) {
190 if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
192 else if (temp[i] == '(' || temp[i] == ')')
194 else if (g_ascii_isprint(temp[i]) == FALSE)
196 temp[i] = g_ascii_tolower(temp[i]);
199 element = find_element(data, network->identifier);
200 if (element == NULL) {
201 const char *security;
204 element = connman_element_create(temp);
206 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
207 element->index = parent->index;
209 data->list = g_slist_append(data->list, element);
211 connman_element_add_static_property(element, "WiFi.Name",
212 DBUS_TYPE_STRING, &network->identifier);
214 connman_element_add_static_array_property(element, "WiFi.SSID",
215 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
217 if (network->has_rsn == TRUE)
219 else if (network->has_wpa == TRUE)
221 else if (network->has_wep == TRUE)
226 connman_element_add_static_property(element, "WiFi.Security",
227 DBUS_TYPE_STRING, &security);
229 strength = network->quality;
231 connman_element_add_static_property(element, "WiFi.Strength",
232 DBUS_TYPE_BYTE, &strength);
234 connman_element_add_static_property(element, "WiFi.Noise",
235 DBUS_TYPE_INT32, &network->noise);
237 DBG("%s (%s) strength %d", network->identifier,
240 connman_element_register(element, parent);
246 static struct supplicant_callback wifi_callback = {
247 .state_change = state_change,
248 .scan_result = scan_result,
251 static int wifi_probe(struct connman_element *element)
253 struct wifi_data *data;
255 DBG("element %p name %s", element, element->name);
257 data = g_try_new0(struct wifi_data, 1);
261 connman_element_set_data(element, data);
266 static void wifi_remove(struct connman_element *element)
268 struct wifi_data *data = connman_element_get_data(element);
270 DBG("element %p name %s", element, element->name);
272 connman_element_set_data(element, NULL);
274 g_free(data->identifier);
278 static int wifi_update(struct connman_element *element)
280 DBG("element %p name %s", element, element->name);
282 __supplicant_scan(element);
287 static int wifi_enable(struct connman_element *element)
291 DBG("element %p name %s", element, element->name);
293 err = __supplicant_start(element, &wifi_callback);
297 __supplicant_scan(element);
302 static int wifi_disable(struct connman_element *element)
304 struct wifi_data *data = connman_element_get_data(element);
307 DBG("element %p name %s", element, element->name);
309 __supplicant_disconnect(element);
311 for (list = data->list; list; list = list->next) {
312 struct connman_element *network = list->data;
314 connman_element_unref(network);
317 g_slist_free(data->list);
320 connman_element_unregister_children(element);
325 static struct connman_driver wifi_driver = {
326 .name = "wifi-device",
327 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
328 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
330 .remove = wifi_remove,
331 .update = wifi_update,
332 .enable = wifi_enable,
333 .disable = wifi_disable,
336 static GSList *device_list = NULL;
338 static void wifi_newlink(unsigned short type, int index,
339 unsigned flags, unsigned change)
341 struct connman_element *device;
343 gboolean exists = FALSE;
348 DBG("index %d", index);
350 if (type != ARPHRD_ETHER)
353 name = inet_index2name(index);
355 memset(&iwr, 0, sizeof(iwr));
356 strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
358 sk = socket(PF_INET, SOCK_DGRAM, 0);
360 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
368 for (list = device_list; list; list = list->next) {
369 struct connman_element *device = list->data;
371 if (device->index == index) {
377 if (exists == TRUE) {
382 device = connman_element_create(NULL);
383 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
384 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
386 device->index = index;
389 connman_element_register(device, NULL);
390 device_list = g_slist_append(device_list, device);
393 static void wifi_dellink(unsigned short type, int index,
394 unsigned flags, unsigned change)
398 DBG("index %d", index);
400 for (list = device_list; list; list = list->next) {
401 struct connman_element *device = list->data;
403 if (device->index == index) {
404 device_list = g_slist_remove(device_list, device);
405 connman_element_unregister(device);
406 connman_element_unref(device);
412 static struct connman_rtnl wifi_rtnl = {
414 .newlink = wifi_newlink,
415 .dellink = wifi_dellink,
418 static void supplicant_connect(DBusConnection *connection, void *user_data)
420 DBG("connection %p", connection);
422 __supplicant_init(connection);
424 if (connman_rtnl_register(&wifi_rtnl) < 0)
427 connman_rtnl_send_getlink();
430 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
434 DBG("connection %p", connection);
436 connman_rtnl_unregister(&wifi_rtnl);
438 for (list = device_list; list; list = list->next) {
439 struct connman_element *device = list->data;
441 connman_element_unregister(device);
442 connman_element_unref(device);
445 g_slist_free(device_list);
451 static DBusConnection *connection;
454 static int wifi_init(void)
458 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
459 if (connection == NULL)
462 err = connman_driver_register(&network_driver);
464 dbus_connection_unref(connection);
468 err = connman_driver_register(&wifi_driver);
470 connman_driver_unregister(&network_driver);
471 dbus_connection_unref(connection);
475 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
476 supplicant_connect, supplicant_disconnect, NULL, NULL);
478 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
479 supplicant_connect(connection, NULL);
484 static void wifi_exit(void)
486 connman_driver_unregister(&network_driver);
487 connman_driver_unregister(&wifi_driver);
490 g_dbus_remove_watch(connection, watch);
492 supplicant_disconnect(connection, NULL);
494 dbus_connection_unref(connection);
497 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
498 wifi_init, wifi_exit)