5 * Copyright (C) 2007-2010 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>
33 #include <net/ethernet.h>
36 #define IFF_LOWER_UP 0x10000
39 #include <dbus/dbus.h>
42 #define CONNMAN_API_SUBJECT_TO_CHANGE
43 #include <connman/plugin.h>
44 #include <connman/inet.h>
45 #include <connman/device.h>
46 #include <connman/rtnl.h>
47 #include <connman/log.h>
49 #include <gsupplicant/gsupplicant.h>
51 #define CLEANUP_TIMEOUT 8 /* in seconds */
52 #define INACTIVE_TIMEOUT 12 /* in seconds */
56 struct connman_device *device;
57 struct connman_network *network;
58 GSupplicantInterface *interface;
59 connman_bool_t connected;
65 static int get_bssid(struct connman_device *device,
66 unsigned char *bssid, unsigned int *bssid_len)
73 ifindex = connman_device_get_index(device);
77 ifname = connman_inet_ifname(ifindex);
81 fd = socket(PF_INET, SOCK_DGRAM, 0);
87 memset(&wrq, 0, sizeof(wrq));
88 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
90 err = ioctl(fd, SIOCGIWAP, &wrq);
98 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
99 *bssid_len = ETH_ALEN;
104 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
106 struct connman_device *device = user_data;
107 struct wifi_data *wifi = connman_device_get_data(device);
109 DBG("index %d flags %d change %d", wifi->index, flags, change);
114 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
118 DBG("interface down");
121 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
122 if (flags & IFF_LOWER_UP)
131 static int wifi_probe(struct connman_device *device)
133 struct wifi_data *wifi;
135 DBG("device %p", device);
137 wifi = g_try_new0(struct wifi_data, 1);
141 wifi->connected = FALSE;
143 connman_device_set_data(device, wifi);
144 wifi->device = connman_device_ref(device);
146 wifi->index = connman_device_get_index(device);
149 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
150 wifi_newlink, device);
155 static void wifi_remove(struct connman_device *device)
157 struct wifi_data *wifi = connman_device_get_data(device);
159 DBG("device %p", device);
161 connman_device_set_data(device, NULL);
162 connman_device_unref(wifi->device);
163 connman_rtnl_remove_watch(wifi->watch);
165 g_free(wifi->identifier);
169 static void interface_create_callback(int result,
170 GSupplicantInterface *interface,
173 struct wifi_data *wifi = user_data;
175 DBG("result %d ifname %s", result,
176 g_supplicant_interface_get_ifname(interface));
181 wifi->interface = interface;
182 g_supplicant_interface_set_data(interface, wifi);
185 static void interface_remove_callback(int result,
186 GSupplicantInterface *interface,
189 struct wifi_data *wifi = user_data;
191 DBG("result %d", result);
196 wifi->interface = NULL;
200 static int wifi_enable(struct connman_device *device)
202 struct wifi_data *wifi = connman_device_get_data(device);
203 const char *interface = connman_device_get_string(device, "Interface");
205 DBG("device %p %p", device, wifi);
207 return g_supplicant_interface_create(interface, "nl80211,wext",
208 interface_create_callback,
212 static int wifi_disable(struct connman_device *device)
214 struct wifi_data *wifi = connman_device_get_data(device);
216 DBG("device %p", device);
218 wifi->connected = FALSE;
220 return g_supplicant_interface_remove(wifi->interface,
221 interface_remove_callback,
225 static void scan_callback(int result, GSupplicantInterface *interface,
228 struct connman_device *device = user_data;
230 DBG("result %d", result);
233 connman_device_set_scanning(device, FALSE);
237 connman_device_set_scanning(device, TRUE);
240 static int wifi_scan(struct connman_device *device)
242 struct wifi_data *wifi = connman_device_get_data(device);
244 DBG("device %p %p", device, wifi->interface);
246 return g_supplicant_interface_scan(wifi->interface, scan_callback,
250 static struct connman_device_driver wifi_ng_driver = {
252 .type = CONNMAN_DEVICE_TYPE_WIFI,
253 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
255 .remove = wifi_remove,
256 .enable = wifi_enable,
257 .disable = wifi_disable,
261 static void system_ready(void)
265 if (connman_device_driver_register(&wifi_ng_driver) < 0)
266 connman_error("Failed to register WiFi driver");
269 static void system_killed(void)
273 connman_device_driver_unregister(&wifi_ng_driver);
276 static void interface_added(GSupplicantInterface *interface)
278 const char *ifname = g_supplicant_interface_get_ifname(interface);
279 const char *driver = g_supplicant_interface_get_driver(interface);
280 struct wifi_data *wifi;
282 wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
284 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
286 if (wifi == NULL || wifi->device == NULL) {
287 connman_error("Wrong wifi pointer");
291 connman_device_set_powered(wifi->device, TRUE);
292 wifi_scan(wifi->device);
295 static void interface_state(GSupplicantInterface *interface)
297 struct connman_network *network;
298 struct connman_device *device;
299 struct wifi_data *wifi;
300 GSupplicantState state = g_supplicant_interface_get_state(interface);
301 unsigned char bssid[ETH_ALEN];
302 unsigned int bssid_len;
304 wifi = (struct wifi_data *) g_supplicant_interface_get_data(interface);
306 DBG("wifi %p interface state %d", wifi, state);
311 network = wifi->network;
312 device = wifi->device;
314 if (device == NULL || network == NULL)
318 case G_SUPPLICANT_STATE_SCANNING:
319 connman_device_set_scanning(device, TRUE);
322 case G_SUPPLICANT_STATE_AUTHENTICATING:
323 case G_SUPPLICANT_STATE_ASSOCIATING:
324 connman_network_set_associating(network, TRUE);
327 case G_SUPPLICANT_STATE_COMPLETED:
328 /* reset scan trigger and schedule background scan */
329 connman_device_schedule_scan(device);
331 if (get_bssid(device, bssid, &bssid_len) == 0)
332 connman_network_set_address(network,
334 connman_network_set_connected(network, TRUE);
337 case G_SUPPLICANT_STATE_DISCONNECTED:
338 connman_network_set_connected(network, FALSE);
341 case G_SUPPLICANT_STATE_INACTIVE:
344 case G_SUPPLICANT_STATE_UNKNOWN:
345 case G_SUPPLICANT_STATE_ASSOCIATED:
346 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
347 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
354 static void interface_removed(GSupplicantInterface *interface)
356 const char *ifname = g_supplicant_interface_get_ifname(interface);
357 struct wifi_data *wifi;
359 DBG("ifname %s", ifname);
361 wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
363 if (wifi == NULL || wifi->device == NULL) {
364 connman_error("Wrong wifi pointer");
368 connman_device_set_powered(wifi->device, FALSE);
371 static void scan_started(GSupplicantInterface *interface)
373 struct wifi_data *wifi;
377 wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
383 connman_device_set_scanning(wifi->device, TRUE);
386 static void scan_finished(GSupplicantInterface *interface)
388 struct wifi_data *wifi;
392 wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
398 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
400 unsigned char strength;
402 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
409 static void network_added(GSupplicantNetwork *supplicant_network)
411 struct connman_network *network;
412 GSupplicantInterface *interface;
413 struct wifi_data *wifi;
414 const char *name, *path, *identifier, *mode, *security, *group;
416 unsigned int ssid_len;
420 interface = g_supplicant_network_get_interface(supplicant_network);
421 wifi = (struct wifi_data *) g_supplicant_interface_get_data(interface);
422 name = g_supplicant_network_get_name(supplicant_network);
423 path = g_supplicant_network_get_path(supplicant_network);
424 identifier = g_supplicant_network_get_identifier(supplicant_network);
425 mode = g_supplicant_network_get_mode(supplicant_network);
426 security = g_supplicant_network_get_security(supplicant_network);
427 group = g_supplicant_network_get_identifier(supplicant_network);
432 ssid = (unsigned char *)g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
434 network = connman_device_get_network(wifi->device, path);
436 if (network == NULL) {
437 network = connman_network_create(identifier,
438 CONNMAN_NETWORK_TYPE_WIFI);
442 connman_network_set_index(network, wifi->index);
444 connman_network_set_protocol(network,
445 CONNMAN_NETWORK_PROTOCOL_IP);
447 if (connman_device_add_network(wifi->device, network) < 0) {
448 connman_network_unref(network);
453 if (name != NULL && name[0] != '\0')
454 connman_network_set_name(network, name);
456 connman_network_set_blob(network, "WiFi.SSID",
458 connman_network_set_string(network, "WiFi.Mode", mode);
459 connman_network_set_string(network, "WiFi.Security", security);
460 connman_network_set_strength(network,
461 calculate_strength(supplicant_network));
463 connman_network_set_available(network, TRUE);
466 connman_network_set_group(network, group);
469 static void network_removed(GSupplicantNetwork *network)
471 const char *name = g_supplicant_network_get_name(network);
473 DBG("* name %s", name);
476 static void debug(const char *str)
478 connman_debug("%s", str);
481 static const GSupplicantCallbacks callbacks = {
482 .system_ready = system_ready,
483 .system_killed = system_killed,
484 .interface_added = interface_added,
485 .interface_state = interface_state,
486 .interface_removed = interface_removed,
487 .scan_started = scan_started,
488 .scan_finished = scan_finished,
489 .network_added = network_added,
490 .network_removed = network_removed,
495 static int network_probe(struct connman_network *network)
497 DBG("network %p", network);
502 static void network_remove(struct connman_network *network)
504 DBG("network %p", network);
507 static void connect_callback(int result, GSupplicantInterface *interface,
510 connman_error("%s", __func__);
513 static void disconnect_callback(int result, GSupplicantInterface *interface,
516 struct wifi_data *wifi = user_data;
519 connman_error("%s", __func__);
523 connman_network_unref(wifi->network);
525 wifi->network = NULL;
529 static GSupplicantSecurity network_security(const char *security)
531 if (g_str_equal(security, "none") == TRUE)
532 return G_SUPPLICANT_SECURITY_NONE;
533 else if (g_str_equal(security, "wep") == TRUE)
534 return G_SUPPLICANT_SECURITY_WEP;
535 else if (g_str_equal(security, "psk") == TRUE)
536 return G_SUPPLICANT_SECURITY_PSK;
537 else if (g_str_equal(security, "wpa") == TRUE)
538 return G_SUPPLICANT_SECURITY_PSK;
539 else if (g_str_equal(security, "rsn") == TRUE)
540 return G_SUPPLICANT_SECURITY_PSK;
541 else if (g_str_equal(security, "ieee8021x") == TRUE)
542 return G_SUPPLICANT_SECURITY_IEEE8021X;
544 return G_SUPPLICANT_SECURITY_UNKNOWN;
547 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
549 const char *security;
551 memset(ssid, 0, sizeof(*ssid));
552 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
554 security = connman_network_get_string(network, "WiFi.Security");
555 ssid->security = network_security(security);
556 ssid->passphrase = connman_network_get_string(network,
558 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
561 * If our private key password is unset,
562 * we use the supplied passphrase. That is needed
563 * for PEAP where 2 passphrases (identity and client
564 * cert may have to be provided.
566 if (connman_network_get_string(network,
567 "WiFi.PrivateKeyPassphrase") == NULL)
568 connman_network_set_string(network,
569 "WiFi.PrivateKeyPassphrase",
571 /* We must have an identity for both PEAP and TLS */
572 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
573 ssid->ca_cert_path = connman_network_get_string(network,
575 ssid->client_cert_path = connman_network_get_string(network,
576 "WiFi.ClientCertFile");
577 ssid->private_key_path = connman_network_get_string(network,
578 "WiFi.PrivateKeyFile");
579 ssid->private_key_passphrase = connman_network_get_string(network,
580 "WiFi.PrivateKeyPassphrase");
581 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
585 static int network_connect(struct connman_network *network)
587 struct connman_device *device = connman_network_get_device(network);
588 struct wifi_data *wifi;
589 GSupplicantInterface *interface;
590 GSupplicantSSID ssid;
592 DBG("network %p", network);
597 wifi = connman_device_get_data(device);
601 interface = wifi->interface;
603 ssid_init(&ssid, network);
605 wifi->network = connman_network_ref(network);
607 return g_supplicant_interface_connect(interface, &ssid,
608 connect_callback, NULL);
611 static int network_disconnect(struct connman_network *network)
613 struct connman_device *device = connman_network_get_device(network);
614 struct wifi_data *wifi;
616 DBG("network %p", network);
618 wifi = connman_device_get_data(device);
619 if (wifi == NULL || wifi->interface == NULL)
622 return g_supplicant_interface_disconnect(wifi->interface,
623 disconnect_callback, wifi);
626 static struct connman_network_driver network_driver = {
628 .type = CONNMAN_NETWORK_TYPE_WIFI,
629 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
630 .probe = network_probe,
631 .remove = network_remove,
632 .connect = network_connect,
633 .disconnect = network_disconnect,
636 static int wifi_init(void)
640 err = connman_network_driver_register(&network_driver);
644 err = g_supplicant_register(&callbacks);
646 connman_network_driver_unregister(&network_driver);
653 static void wifi_exit(void)
657 g_supplicant_unregister(&callbacks);
659 connman_network_driver_unregister(&network_driver);
662 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
663 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)