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 struct connman_network *pending_network;
64 GSupplicantInterface *interface;
65 GSupplicantState state;
66 connman_bool_t connected;
67 connman_bool_t disconnecting;
73 static int get_bssid(struct connman_device *device,
74 unsigned char *bssid, unsigned int *bssid_len)
81 ifindex = connman_device_get_index(device);
85 ifname = connman_inet_ifname(ifindex);
89 fd = socket(PF_INET, SOCK_DGRAM, 0);
95 memset(&wrq, 0, sizeof(wrq));
96 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
98 err = ioctl(fd, SIOCGIWAP, &wrq);
106 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
107 *bssid_len = ETH_ALEN;
112 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
114 struct connman_device *device = user_data;
115 struct wifi_data *wifi = connman_device_get_data(device);
117 DBG("index %d flags %d change %d", wifi->index, flags, change);
122 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
126 DBG("interface down");
129 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
130 if (flags & IFF_LOWER_UP)
139 static int wifi_probe(struct connman_device *device)
141 struct wifi_data *wifi;
143 DBG("device %p", device);
145 wifi = g_try_new0(struct wifi_data, 1);
149 wifi->connected = FALSE;
150 wifi->disconnecting = FALSE;
151 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
153 connman_device_set_data(device, wifi);
154 wifi->device = connman_device_ref(device);
156 wifi->index = connman_device_get_index(device);
159 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
160 wifi_newlink, device);
165 static void wifi_remove(struct connman_device *device)
167 struct wifi_data *wifi = connman_device_get_data(device);
169 DBG("device %p", device);
171 if (wifi->pending_network != NULL) {
172 connman_network_unref(wifi->pending_network);
173 wifi->pending_network = NULL;
176 connman_device_set_data(device, NULL);
177 connman_device_unref(wifi->device);
178 connman_rtnl_remove_watch(wifi->watch);
180 g_supplicant_interface_set_data(wifi->interface, NULL);
182 g_free(wifi->identifier);
186 static void interface_create_callback(int result,
187 GSupplicantInterface *interface,
190 struct wifi_data *wifi = user_data;
192 DBG("result %d ifname %s", result,
193 g_supplicant_interface_get_ifname(interface));
198 wifi->interface = interface;
199 g_supplicant_interface_set_data(interface, wifi);
202 static void interface_remove_callback(int result,
203 GSupplicantInterface *interface,
206 struct wifi_data *wifi = user_data;
208 DBG("result %d", result);
213 wifi->interface = NULL;
217 static int wifi_enable(struct connman_device *device)
219 struct wifi_data *wifi = connman_device_get_data(device);
220 const char *interface = connman_device_get_string(device, "Interface");
221 const char *driver = connman_option_get_string("wifi");
223 DBG("device %p %p", device, wifi);
225 return g_supplicant_interface_create(interface, driver,
226 interface_create_callback,
230 static int wifi_disable(struct connman_device *device)
232 struct wifi_data *wifi = connman_device_get_data(device);
234 DBG("device %p", device);
236 wifi->connected = FALSE;
237 wifi->disconnecting = FALSE;
239 if (wifi->pending_network != NULL) {
240 connman_network_unref(wifi->pending_network);
241 wifi->pending_network = NULL;
244 return g_supplicant_interface_remove(wifi->interface,
245 interface_remove_callback,
249 static void scan_callback(int result, GSupplicantInterface *interface,
252 struct connman_device *device = user_data;
254 DBG("result %d", result);
256 connman_device_set_scanning(device, FALSE);
259 static int wifi_scan(struct connman_device *device)
261 struct wifi_data *wifi = connman_device_get_data(device);
263 DBG("device %p %p", device, wifi->interface);
265 return g_supplicant_interface_scan(wifi->interface, scan_callback,
269 static struct connman_device_driver wifi_ng_driver = {
271 .type = CONNMAN_DEVICE_TYPE_WIFI,
272 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
274 .remove = wifi_remove,
275 .enable = wifi_enable,
276 .disable = wifi_disable,
280 static void system_ready(void)
284 if (connman_device_driver_register(&wifi_ng_driver) < 0)
285 connman_error("Failed to register WiFi driver");
288 static void system_killed(void)
292 connman_device_driver_unregister(&wifi_ng_driver);
295 static void interface_added(GSupplicantInterface *interface)
297 const char *ifname = g_supplicant_interface_get_ifname(interface);
298 const char *driver = g_supplicant_interface_get_driver(interface);
299 struct wifi_data *wifi;
301 wifi = g_supplicant_interface_get_data(interface);
304 * We can get here with a NULL wifi pointer when
305 * the interface added signal is sent before the
306 * interface creation callback is called.
311 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
313 if (wifi->device == NULL) {
314 connman_error("WiFi device not set");
318 connman_device_set_powered(wifi->device, TRUE);
319 wifi_scan(wifi->device);
322 static connman_bool_t is_idle(struct wifi_data *wifi)
324 DBG("state %d", wifi->state);
326 switch (wifi->state) {
327 case G_SUPPLICANT_STATE_UNKNOWN:
328 case G_SUPPLICANT_STATE_DISCONNECTED:
329 case G_SUPPLICANT_STATE_INACTIVE:
330 case G_SUPPLICANT_STATE_SCANNING:
333 case G_SUPPLICANT_STATE_AUTHENTICATING:
334 case G_SUPPLICANT_STATE_ASSOCIATING:
335 case G_SUPPLICANT_STATE_ASSOCIATED:
336 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
337 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
338 case G_SUPPLICANT_STATE_COMPLETED:
345 static void interface_state(GSupplicantInterface *interface)
347 struct connman_network *network;
348 struct connman_device *device;
349 struct wifi_data *wifi;
350 GSupplicantState state = g_supplicant_interface_get_state(interface);
351 unsigned char bssid[ETH_ALEN];
352 unsigned int bssid_len;
354 wifi = g_supplicant_interface_get_data(interface);
356 DBG("wifi %p interface state %d", wifi, state);
361 network = wifi->network;
362 device = wifi->device;
364 if (device == NULL || network == NULL)
368 case G_SUPPLICANT_STATE_SCANNING:
369 connman_device_set_scanning(device, TRUE);
372 case G_SUPPLICANT_STATE_AUTHENTICATING:
373 case G_SUPPLICANT_STATE_ASSOCIATING:
374 connman_network_set_associating(network, TRUE);
377 case G_SUPPLICANT_STATE_COMPLETED:
378 /* reset scan trigger and schedule background scan */
379 connman_device_schedule_scan(device);
381 if (get_bssid(device, bssid, &bssid_len) == 0)
382 connman_network_set_address(network,
384 connman_network_set_connected(network, TRUE);
387 case G_SUPPLICANT_STATE_DISCONNECTED:
389 * If we're in one of the idle modes, we have
390 * not started association yet and thus setting
391 * those ones to FALSE could cancel an association
396 connman_network_set_associating(network, FALSE);
397 connman_network_set_connected(network, FALSE);
400 case G_SUPPLICANT_STATE_INACTIVE:
401 connman_network_set_associating(network, FALSE);
404 case G_SUPPLICANT_STATE_UNKNOWN:
405 case G_SUPPLICANT_STATE_ASSOCIATED:
406 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
407 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
416 static void interface_removed(GSupplicantInterface *interface)
418 const char *ifname = g_supplicant_interface_get_ifname(interface);
419 struct wifi_data *wifi;
421 DBG("ifname %s", ifname);
423 wifi = g_supplicant_interface_get_data(interface);
425 if (wifi == NULL || wifi->device == NULL) {
426 connman_error("Wrong wifi pointer");
430 connman_device_set_powered(wifi->device, FALSE);
433 static void scan_started(GSupplicantInterface *interface)
435 struct wifi_data *wifi;
439 wifi = g_supplicant_interface_get_data(interface);
445 connman_device_set_scanning(wifi->device, TRUE);
448 static void scan_finished(GSupplicantInterface *interface)
450 struct wifi_data *wifi;
454 wifi = g_supplicant_interface_get_data(interface);
460 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
462 unsigned char strength;
464 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
471 static void network_added(GSupplicantNetwork *supplicant_network)
473 struct connman_network *network;
474 GSupplicantInterface *interface;
475 struct wifi_data *wifi;
476 const char *name, *identifier, *mode, *security, *group;
477 const unsigned char *ssid;
478 unsigned int ssid_len;
482 interface = g_supplicant_network_get_interface(supplicant_network);
483 wifi = g_supplicant_interface_get_data(interface);
484 name = g_supplicant_network_get_name(supplicant_network);
485 identifier = g_supplicant_network_get_identifier(supplicant_network);
486 mode = g_supplicant_network_get_mode(supplicant_network);
487 security = g_supplicant_network_get_security(supplicant_network);
488 group = g_supplicant_network_get_identifier(supplicant_network);
493 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
495 network = connman_device_get_network(wifi->device, identifier);
497 if (network == NULL) {
498 network = connman_network_create(identifier,
499 CONNMAN_NETWORK_TYPE_WIFI);
503 connman_network_set_index(network, wifi->index);
505 if (connman_device_add_network(wifi->device, network) < 0) {
506 connman_network_unref(network);
511 if (name != NULL && name[0] != '\0')
512 connman_network_set_name(network, name);
514 connman_network_set_blob(network, "WiFi.SSID",
516 connman_network_set_string(network, "WiFi.Mode", mode);
517 connman_network_set_string(network, "WiFi.Security", security);
518 connman_network_set_strength(network,
519 calculate_strength(supplicant_network));
521 connman_network_set_available(network, TRUE);
524 connman_network_set_group(network, group);
527 static void network_removed(GSupplicantNetwork *network)
529 GSupplicantInterface *interface;
530 struct wifi_data *wifi;
531 const char *name, *identifier;
533 interface = g_supplicant_network_get_interface(network);
534 wifi = g_supplicant_interface_get_data(interface);
535 identifier = g_supplicant_network_get_identifier(network);
536 name = g_supplicant_network_get_name(network);
538 DBG("name %s", name);
541 connman_device_remove_network(wifi->device, identifier);
544 static void debug(const char *str)
546 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
547 connman_debug("%s", str);
550 static const GSupplicantCallbacks callbacks = {
551 .system_ready = system_ready,
552 .system_killed = system_killed,
553 .interface_added = interface_added,
554 .interface_state = interface_state,
555 .interface_removed = interface_removed,
556 .scan_started = scan_started,
557 .scan_finished = scan_finished,
558 .network_added = network_added,
559 .network_removed = network_removed,
564 static int network_probe(struct connman_network *network)
566 DBG("network %p", network);
571 static void network_remove(struct connman_network *network)
573 DBG("network %p", network);
576 static void connect_callback(int result, GSupplicantInterface *interface,
579 connman_error("%s", __func__);
582 static GSupplicantSecurity network_security(const char *security)
584 if (g_str_equal(security, "none") == TRUE)
585 return G_SUPPLICANT_SECURITY_NONE;
586 else if (g_str_equal(security, "wep") == TRUE)
587 return G_SUPPLICANT_SECURITY_WEP;
588 else if (g_str_equal(security, "psk") == TRUE)
589 return G_SUPPLICANT_SECURITY_PSK;
590 else if (g_str_equal(security, "wpa") == TRUE)
591 return G_SUPPLICANT_SECURITY_PSK;
592 else if (g_str_equal(security, "rsn") == TRUE)
593 return G_SUPPLICANT_SECURITY_PSK;
594 else if (g_str_equal(security, "ieee8021x") == TRUE)
595 return G_SUPPLICANT_SECURITY_IEEE8021X;
597 return G_SUPPLICANT_SECURITY_UNKNOWN;
600 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
602 const char *security, *passphrase;
604 memset(ssid, 0, sizeof(*ssid));
605 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
607 security = connman_network_get_string(network, "WiFi.Security");
608 ssid->security = network_security(security);
609 passphrase = connman_network_get_string(network,
611 if (passphrase == NULL || strlen(passphrase) == 0)
612 ssid->passphrase = NULL;
614 ssid->passphrase = passphrase;
616 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
619 * If our private key password is unset,
620 * we use the supplied passphrase. That is needed
621 * for PEAP where 2 passphrases (identity and client
622 * cert may have to be provided.
624 if (connman_network_get_string(network,
625 "WiFi.PrivateKeyPassphrase") == NULL)
626 connman_network_set_string(network,
627 "WiFi.PrivateKeyPassphrase",
629 /* We must have an identity for both PEAP and TLS */
630 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
631 ssid->ca_cert_path = connman_network_get_string(network,
633 ssid->client_cert_path = connman_network_get_string(network,
634 "WiFi.ClientCertFile");
635 ssid->private_key_path = connman_network_get_string(network,
636 "WiFi.PrivateKeyFile");
637 ssid->private_key_passphrase = connman_network_get_string(network,
638 "WiFi.PrivateKeyPassphrase");
639 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
643 static int network_connect(struct connman_network *network)
645 struct connman_device *device = connman_network_get_device(network);
646 struct wifi_data *wifi;
647 GSupplicantInterface *interface;
648 GSupplicantSSID ssid;
650 DBG("network %p", network);
655 wifi = connman_device_get_data(device);
659 interface = wifi->interface;
661 ssid_init(&ssid, network);
663 if (wifi->disconnecting == TRUE)
664 wifi->pending_network = connman_network_ref(network);
666 wifi->network = connman_network_ref(network);
668 return g_supplicant_interface_connect(interface, &ssid,
669 connect_callback, NULL);
675 static void disconnect_callback(int result, GSupplicantInterface *interface,
678 struct wifi_data *wifi = user_data;
680 if (wifi->network != NULL) {
682 * if result < 0 supplican return an error because
683 * the network is not current.
684 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
685 * failed, call connman_network_set_connected to report
686 * disconnect is completed.
689 connman_network_set_connected(wifi->network, FALSE);
691 connman_network_unref(wifi->network);
694 wifi->network = NULL;
696 wifi->disconnecting = FALSE;
698 if (wifi->pending_network != NULL) {
699 network_connect(wifi->pending_network);
700 connman_network_unref(wifi->pending_network);
701 wifi->pending_network = NULL;
706 static int network_disconnect(struct connman_network *network)
708 struct connman_device *device = connman_network_get_device(network);
709 struct wifi_data *wifi;
712 DBG("network %p", network);
714 wifi = connman_device_get_data(device);
715 if (wifi == NULL || wifi->interface == NULL)
718 connman_network_set_associating(network, FALSE);
720 if (wifi->disconnecting == TRUE)
723 wifi->disconnecting = TRUE;
725 err = g_supplicant_interface_disconnect(wifi->interface,
726 disconnect_callback, wifi);
728 wifi->disconnecting = FALSE;
733 static struct connman_network_driver network_driver = {
735 .type = CONNMAN_NETWORK_TYPE_WIFI,
736 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
737 .probe = network_probe,
738 .remove = network_remove,
739 .connect = network_connect,
740 .disconnect = network_disconnect,
743 static int tech_probe(struct connman_technology *technology)
745 wifi_technology = technology;
750 static void tech_remove(struct connman_technology *technology)
752 wifi_technology = NULL;
755 static void regdom_callback(void *user_data)
757 char *alpha2 = user_data;
761 if (wifi_technology == NULL)
764 connman_technology_regdom_notify(wifi_technology, alpha2);
767 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
769 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
772 static struct connman_technology_driver tech_driver = {
774 .type = CONNMAN_SERVICE_TYPE_WIFI,
776 .remove = tech_remove,
777 .set_regdom = tech_set_regdom,
780 static int wifi_init(void)
784 err = connman_network_driver_register(&network_driver);
788 err = g_supplicant_register(&callbacks);
790 connman_network_driver_unregister(&network_driver);
794 err = connman_technology_driver_register(&tech_driver);
796 g_supplicant_unregister(&callbacks);
797 connman_network_driver_unregister(&network_driver);
804 static void wifi_exit(void)
808 connman_technology_driver_unregister(&tech_driver);
810 g_supplicant_unregister(&callbacks);
812 connman_network_driver_unregister(&network_driver);
815 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
816 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)