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
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <linux/if_arp.h>
33 #include <linux/wireless.h>
34 #include <net/ethernet.h>
37 #define IFF_LOWER_UP 0x10000
40 #include <dbus/dbus.h>
43 #define CONNMAN_API_SUBJECT_TO_CHANGE
44 #include <connman/plugin.h>
45 #include <connman/inet.h>
46 #include <connman/device.h>
47 #include <connman/rtnl.h>
48 #include <connman/technology.h>
49 #include <connman/log.h>
50 #include <connman/option.h>
52 #include <gsupplicant/gsupplicant.h>
54 #define CLEANUP_TIMEOUT 8 /* in seconds */
55 #define INACTIVE_TIMEOUT 12 /* in seconds */
57 struct connman_technology *wifi_technology = NULL;
61 struct connman_device *device;
62 struct connman_network *network;
63 GSupplicantInterface *interface;
64 connman_bool_t connected;
70 static int get_bssid(struct connman_device *device,
71 unsigned char *bssid, unsigned int *bssid_len)
78 ifindex = connman_device_get_index(device);
82 ifname = connman_inet_ifname(ifindex);
86 fd = socket(PF_INET, SOCK_DGRAM, 0);
92 memset(&wrq, 0, sizeof(wrq));
93 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
95 err = ioctl(fd, SIOCGIWAP, &wrq);
103 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
104 *bssid_len = ETH_ALEN;
109 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
111 struct connman_device *device = user_data;
112 struct wifi_data *wifi = connman_device_get_data(device);
114 DBG("index %d flags %d change %d", wifi->index, flags, change);
119 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
123 DBG("interface down");
126 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
127 if (flags & IFF_LOWER_UP)
136 static int wifi_probe(struct connman_device *device)
138 struct wifi_data *wifi;
140 DBG("device %p", device);
142 wifi = g_try_new0(struct wifi_data, 1);
146 wifi->connected = FALSE;
148 connman_device_set_data(device, wifi);
149 wifi->device = connman_device_ref(device);
151 wifi->index = connman_device_get_index(device);
154 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
155 wifi_newlink, device);
160 static void wifi_remove(struct connman_device *device)
162 struct wifi_data *wifi = connman_device_get_data(device);
164 DBG("device %p", device);
166 connman_device_set_data(device, NULL);
167 connman_device_unref(wifi->device);
168 connman_rtnl_remove_watch(wifi->watch);
170 g_supplicant_interface_set_data(wifi->interface, NULL);
172 g_free(wifi->identifier);
176 static void interface_create_callback(int result,
177 GSupplicantInterface *interface,
180 struct wifi_data *wifi = user_data;
182 DBG("result %d ifname %s", result,
183 g_supplicant_interface_get_ifname(interface));
188 wifi->interface = interface;
189 g_supplicant_interface_set_data(interface, wifi);
192 static void interface_remove_callback(int result,
193 GSupplicantInterface *interface,
196 struct wifi_data *wifi = user_data;
198 DBG("result %d", result);
203 wifi->interface = NULL;
207 static int wifi_enable(struct connman_device *device)
209 struct wifi_data *wifi = connman_device_get_data(device);
210 const char *interface = connman_device_get_string(device, "Interface");
211 const char *driver = connman_option_get_string("wifi");
213 DBG("device %p %p", device, wifi);
215 return g_supplicant_interface_create(interface, driver,
216 interface_create_callback,
220 static int wifi_disable(struct connman_device *device)
222 struct wifi_data *wifi = connman_device_get_data(device);
224 DBG("device %p", device);
226 wifi->connected = FALSE;
228 return g_supplicant_interface_remove(wifi->interface,
229 interface_remove_callback,
233 static void scan_callback(int result, GSupplicantInterface *interface,
236 struct connman_device *device = user_data;
238 DBG("result %d", result);
240 connman_device_set_scanning(device, FALSE);
243 static int wifi_scan(struct connman_device *device)
245 struct wifi_data *wifi = connman_device_get_data(device);
247 DBG("device %p %p", device, wifi->interface);
249 return g_supplicant_interface_scan(wifi->interface, scan_callback,
253 static struct connman_device_driver wifi_ng_driver = {
255 .type = CONNMAN_DEVICE_TYPE_WIFI,
256 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
258 .remove = wifi_remove,
259 .enable = wifi_enable,
260 .disable = wifi_disable,
264 static void system_ready(void)
268 if (connman_device_driver_register(&wifi_ng_driver) < 0)
269 connman_error("Failed to register WiFi driver");
272 static void system_killed(void)
276 connman_device_driver_unregister(&wifi_ng_driver);
279 static void interface_added(GSupplicantInterface *interface)
281 const char *ifname = g_supplicant_interface_get_ifname(interface);
282 const char *driver = g_supplicant_interface_get_driver(interface);
283 struct wifi_data *wifi;
285 wifi = g_supplicant_interface_get_data(interface);
288 * We can get here with a NULL wifi pointer when
289 * the interface added signal is sent before the
290 * interface creation callback is called.
295 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
297 if (wifi->device == NULL) {
298 connman_error("WiFi device not set");
302 connman_device_set_powered(wifi->device, TRUE);
303 wifi_scan(wifi->device);
306 static void interface_state(GSupplicantInterface *interface)
308 struct connman_network *network;
309 struct connman_device *device;
310 struct wifi_data *wifi;
311 GSupplicantState state = g_supplicant_interface_get_state(interface);
312 unsigned char bssid[ETH_ALEN];
313 unsigned int bssid_len;
315 wifi = g_supplicant_interface_get_data(interface);
317 DBG("wifi %p interface state %d", wifi, state);
322 network = wifi->network;
323 device = wifi->device;
325 if (device == NULL || network == NULL)
329 case G_SUPPLICANT_STATE_SCANNING:
330 connman_device_set_scanning(device, TRUE);
333 case G_SUPPLICANT_STATE_AUTHENTICATING:
334 case G_SUPPLICANT_STATE_ASSOCIATING:
335 connman_network_set_associating(network, TRUE);
338 case G_SUPPLICANT_STATE_COMPLETED:
339 /* reset scan trigger and schedule background scan */
340 connman_device_schedule_scan(device);
342 if (get_bssid(device, bssid, &bssid_len) == 0)
343 connman_network_set_address(network,
345 connman_network_set_connected(network, TRUE);
348 case G_SUPPLICANT_STATE_DISCONNECTED:
349 connman_network_set_associating(network, FALSE);
350 connman_network_set_connected(network, FALSE);
353 case G_SUPPLICANT_STATE_INACTIVE:
354 connman_network_set_associating(network, FALSE);
357 case G_SUPPLICANT_STATE_UNKNOWN:
358 case G_SUPPLICANT_STATE_ASSOCIATED:
359 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
360 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
367 static void interface_removed(GSupplicantInterface *interface)
369 const char *ifname = g_supplicant_interface_get_ifname(interface);
370 struct wifi_data *wifi;
372 DBG("ifname %s", ifname);
374 wifi = g_supplicant_interface_get_data(interface);
376 if (wifi == NULL || wifi->device == NULL) {
377 connman_error("Wrong wifi pointer");
381 connman_device_set_powered(wifi->device, FALSE);
384 static void scan_started(GSupplicantInterface *interface)
386 struct wifi_data *wifi;
390 wifi = g_supplicant_interface_get_data(interface);
396 connman_device_set_scanning(wifi->device, TRUE);
399 static void scan_finished(GSupplicantInterface *interface)
401 struct wifi_data *wifi;
405 wifi = g_supplicant_interface_get_data(interface);
411 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
413 unsigned char strength;
415 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
422 static void network_added(GSupplicantNetwork *supplicant_network)
424 struct connman_network *network;
425 GSupplicantInterface *interface;
426 struct wifi_data *wifi;
427 const char *name, *identifier, *mode, *security, *group;
428 const unsigned char *ssid;
429 unsigned int ssid_len;
433 interface = g_supplicant_network_get_interface(supplicant_network);
434 wifi = g_supplicant_interface_get_data(interface);
435 name = g_supplicant_network_get_name(supplicant_network);
436 identifier = g_supplicant_network_get_identifier(supplicant_network);
437 mode = g_supplicant_network_get_mode(supplicant_network);
438 security = g_supplicant_network_get_security(supplicant_network);
439 group = g_supplicant_network_get_identifier(supplicant_network);
444 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
446 network = connman_device_get_network(wifi->device, identifier);
448 if (network == NULL) {
449 network = connman_network_create(identifier,
450 CONNMAN_NETWORK_TYPE_WIFI);
454 connman_network_set_index(network, wifi->index);
456 if (connman_device_add_network(wifi->device, network) < 0) {
457 connman_network_unref(network);
462 if (name != NULL && name[0] != '\0')
463 connman_network_set_name(network, name);
465 connman_network_set_blob(network, "WiFi.SSID",
467 connman_network_set_string(network, "WiFi.Mode", mode);
468 connman_network_set_string(network, "WiFi.Security", security);
469 connman_network_set_strength(network,
470 calculate_strength(supplicant_network));
472 connman_network_set_available(network, TRUE);
475 connman_network_set_group(network, group);
478 static void network_removed(GSupplicantNetwork *network)
480 const char *name = g_supplicant_network_get_name(network);
482 DBG("* name %s", name);
485 static void debug(const char *str)
487 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
488 connman_debug("%s", str);
491 static const GSupplicantCallbacks callbacks = {
492 .system_ready = system_ready,
493 .system_killed = system_killed,
494 .interface_added = interface_added,
495 .interface_state = interface_state,
496 .interface_removed = interface_removed,
497 .scan_started = scan_started,
498 .scan_finished = scan_finished,
499 .network_added = network_added,
500 .network_removed = network_removed,
505 static int network_probe(struct connman_network *network)
507 DBG("network %p", network);
512 static void network_remove(struct connman_network *network)
514 DBG("network %p", network);
517 static void connect_callback(int result, GSupplicantInterface *interface,
520 connman_error("%s", __func__);
523 static void disconnect_callback(int result, GSupplicantInterface *interface,
526 struct wifi_data *wifi = user_data;
529 connman_error("%s", __func__);
533 connman_network_unref(wifi->network);
535 wifi->network = NULL;
539 static GSupplicantSecurity network_security(const char *security)
541 if (g_str_equal(security, "none") == TRUE)
542 return G_SUPPLICANT_SECURITY_NONE;
543 else if (g_str_equal(security, "wep") == TRUE)
544 return G_SUPPLICANT_SECURITY_WEP;
545 else if (g_str_equal(security, "psk") == TRUE)
546 return G_SUPPLICANT_SECURITY_PSK;
547 else if (g_str_equal(security, "wpa") == TRUE)
548 return G_SUPPLICANT_SECURITY_PSK;
549 else if (g_str_equal(security, "rsn") == TRUE)
550 return G_SUPPLICANT_SECURITY_PSK;
551 else if (g_str_equal(security, "ieee8021x") == TRUE)
552 return G_SUPPLICANT_SECURITY_IEEE8021X;
554 return G_SUPPLICANT_SECURITY_UNKNOWN;
557 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
559 const char *security;
561 memset(ssid, 0, sizeof(*ssid));
562 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
564 security = connman_network_get_string(network, "WiFi.Security");
565 ssid->security = network_security(security);
566 ssid->passphrase = connman_network_get_string(network,
568 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
571 * If our private key password is unset,
572 * we use the supplied passphrase. That is needed
573 * for PEAP where 2 passphrases (identity and client
574 * cert may have to be provided.
576 if (connman_network_get_string(network,
577 "WiFi.PrivateKeyPassphrase") == NULL)
578 connman_network_set_string(network,
579 "WiFi.PrivateKeyPassphrase",
581 /* We must have an identity for both PEAP and TLS */
582 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
583 ssid->ca_cert_path = connman_network_get_string(network,
585 ssid->client_cert_path = connman_network_get_string(network,
586 "WiFi.ClientCertFile");
587 ssid->private_key_path = connman_network_get_string(network,
588 "WiFi.PrivateKeyFile");
589 ssid->private_key_passphrase = connman_network_get_string(network,
590 "WiFi.PrivateKeyPassphrase");
591 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
595 static int network_connect(struct connman_network *network)
597 struct connman_device *device = connman_network_get_device(network);
598 struct wifi_data *wifi;
599 GSupplicantInterface *interface;
600 GSupplicantSSID ssid;
602 DBG("network %p", network);
607 wifi = connman_device_get_data(device);
611 interface = wifi->interface;
613 ssid_init(&ssid, network);
615 wifi->network = connman_network_ref(network);
617 return g_supplicant_interface_connect(interface, &ssid,
618 connect_callback, NULL);
621 static int network_disconnect(struct connman_network *network)
623 struct connman_device *device = connman_network_get_device(network);
624 struct wifi_data *wifi;
626 DBG("network %p", network);
628 wifi = connman_device_get_data(device);
629 if (wifi == NULL || wifi->interface == NULL)
632 connman_network_set_associating(network, FALSE);
634 return g_supplicant_interface_disconnect(wifi->interface,
635 disconnect_callback, wifi);
638 static struct connman_network_driver network_driver = {
640 .type = CONNMAN_NETWORK_TYPE_WIFI,
641 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
642 .probe = network_probe,
643 .remove = network_remove,
644 .connect = network_connect,
645 .disconnect = network_disconnect,
648 static int tech_probe(struct connman_technology *technology)
650 wifi_technology = technology;
655 static void tech_remove(struct connman_technology *technology)
657 wifi_technology = NULL;
660 static void regdom_callback(void *user_data)
662 char *alpha2 = user_data;
666 if (wifi_technology == NULL)
669 connman_technology_regdom_notify(wifi_technology, alpha2);
672 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
674 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
677 static struct connman_technology_driver tech_driver = {
679 .type = CONNMAN_SERVICE_TYPE_WIFI,
681 .remove = tech_remove,
682 .set_regdom = tech_set_regdom,
685 static int wifi_init(void)
689 err = connman_network_driver_register(&network_driver);
693 err = g_supplicant_register(&callbacks);
695 connman_network_driver_unregister(&network_driver);
699 err = connman_technology_driver_register(&tech_driver);
701 g_supplicant_unregister(&callbacks);
702 connman_network_driver_unregister(&network_driver);
709 static void wifi_exit(void)
713 connman_technology_driver_unregister(&tech_driver);
715 g_supplicant_unregister(&callbacks);
717 connman_network_driver_unregister(&network_driver);
720 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
721 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)