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 GList *iface_list = NULL;
75 static int get_bssid(struct connman_device *device,
76 unsigned char *bssid, unsigned int *bssid_len)
83 ifindex = connman_device_get_index(device);
87 ifname = connman_inet_ifname(ifindex);
91 fd = socket(PF_INET, SOCK_DGRAM, 0);
97 memset(&wrq, 0, sizeof(wrq));
98 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
100 err = ioctl(fd, SIOCGIWAP, &wrq);
108 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
109 *bssid_len = ETH_ALEN;
114 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
116 struct connman_device *device = user_data;
117 struct wifi_data *wifi = connman_device_get_data(device);
119 DBG("index %d flags %d change %d", wifi->index, flags, change);
124 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
128 DBG("interface down");
131 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
132 if (flags & IFF_LOWER_UP)
141 static int wifi_probe(struct connman_device *device)
143 struct wifi_data *wifi;
145 DBG("device %p", device);
147 wifi = g_try_new0(struct wifi_data, 1);
151 wifi->connected = FALSE;
152 wifi->disconnecting = FALSE;
153 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
155 connman_device_set_data(device, wifi);
156 wifi->device = connman_device_ref(device);
158 wifi->index = connman_device_get_index(device);
161 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
162 wifi_newlink, device);
164 iface_list = g_list_append(iface_list, wifi);
169 static void wifi_remove(struct connman_device *device)
171 struct wifi_data *wifi = connman_device_get_data(device);
173 DBG("device %p", device);
178 iface_list = g_list_remove(iface_list, wifi);
180 if (wifi->pending_network != NULL) {
181 connman_network_unref(wifi->pending_network);
182 wifi->pending_network = NULL;
185 connman_device_set_data(device, NULL);
186 connman_device_unref(wifi->device);
187 connman_rtnl_remove_watch(wifi->watch);
189 g_supplicant_interface_set_data(wifi->interface, NULL);
191 g_free(wifi->identifier);
195 static void interface_create_callback(int result,
196 GSupplicantInterface *interface,
199 struct wifi_data *wifi = user_data;
201 DBG("result %d ifname %s", result,
202 g_supplicant_interface_get_ifname(interface));
207 wifi->interface = interface;
208 g_supplicant_interface_set_data(interface, wifi);
211 static void interface_remove_callback(int result,
212 GSupplicantInterface *interface,
215 struct wifi_data *wifi = user_data;
217 DBG("result %d", result);
222 wifi->interface = NULL;
226 static int wifi_enable(struct connman_device *device)
228 struct wifi_data *wifi = connman_device_get_data(device);
229 const char *interface = connman_device_get_string(device, "Interface");
230 const char *driver = connman_option_get_string("wifi");
232 DBG("device %p %p", device, wifi);
234 return g_supplicant_interface_create(interface, driver,
235 interface_create_callback,
239 static int wifi_disable(struct connman_device *device)
241 struct wifi_data *wifi = connman_device_get_data(device);
243 DBG("device %p", device);
245 wifi->connected = FALSE;
246 wifi->disconnecting = FALSE;
248 if (wifi->pending_network != NULL) {
249 connman_network_unref(wifi->pending_network);
250 wifi->pending_network = NULL;
253 return g_supplicant_interface_remove(wifi->interface,
254 interface_remove_callback,
258 static void scan_callback(int result, GSupplicantInterface *interface,
261 struct connman_device *device = user_data;
263 DBG("result %d", result);
266 connman_device_reset_scanning(device);
268 connman_device_set_scanning(device, FALSE);
271 static int wifi_scan(struct connman_device *device)
273 struct wifi_data *wifi = connman_device_get_data(device);
276 DBG("device %p %p", device, wifi->interface);
278 ret = g_supplicant_interface_scan(wifi->interface, scan_callback,
281 connman_device_set_scanning(device, TRUE);
286 static struct connman_device_driver wifi_ng_driver = {
288 .type = CONNMAN_DEVICE_TYPE_WIFI,
289 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
291 .remove = wifi_remove,
292 .enable = wifi_enable,
293 .disable = wifi_disable,
297 static void system_ready(void)
301 if (connman_device_driver_register(&wifi_ng_driver) < 0)
302 connman_error("Failed to register WiFi driver");
305 static void system_killed(void)
309 connman_device_driver_unregister(&wifi_ng_driver);
312 static int network_probe(struct connman_network *network)
314 DBG("network %p", network);
319 static void network_remove(struct connman_network *network)
321 DBG("network %p", network);
324 static void connect_callback(int result, GSupplicantInterface *interface,
327 connman_error("%s", __func__);
330 static GSupplicantSecurity network_security(const char *security)
332 if (g_str_equal(security, "none") == TRUE)
333 return G_SUPPLICANT_SECURITY_NONE;
334 else if (g_str_equal(security, "wep") == TRUE)
335 return G_SUPPLICANT_SECURITY_WEP;
336 else if (g_str_equal(security, "psk") == TRUE)
337 return G_SUPPLICANT_SECURITY_PSK;
338 else if (g_str_equal(security, "wpa") == TRUE)
339 return G_SUPPLICANT_SECURITY_PSK;
340 else if (g_str_equal(security, "rsn") == TRUE)
341 return G_SUPPLICANT_SECURITY_PSK;
342 else if (g_str_equal(security, "ieee8021x") == TRUE)
343 return G_SUPPLICANT_SECURITY_IEEE8021X;
345 return G_SUPPLICANT_SECURITY_UNKNOWN;
348 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
350 const char *security, *passphrase;
352 memset(ssid, 0, sizeof(*ssid));
353 ssid->mode = G_SUPPLICANT_MODE_INFRA;
354 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
356 security = connman_network_get_string(network, "WiFi.Security");
357 ssid->security = network_security(security);
358 passphrase = connman_network_get_string(network,
360 if (passphrase == NULL || strlen(passphrase) == 0)
361 ssid->passphrase = NULL;
363 ssid->passphrase = passphrase;
365 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
368 * If our private key password is unset,
369 * we use the supplied passphrase. That is needed
370 * for PEAP where 2 passphrases (identity and client
371 * cert may have to be provided.
373 if (connman_network_get_string(network,
374 "WiFi.PrivateKeyPassphrase") == NULL)
375 connman_network_set_string(network,
376 "WiFi.PrivateKeyPassphrase",
378 /* We must have an identity for both PEAP and TLS */
379 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
380 ssid->ca_cert_path = connman_network_get_string(network,
382 ssid->client_cert_path = connman_network_get_string(network,
383 "WiFi.ClientCertFile");
384 ssid->private_key_path = connman_network_get_string(network,
385 "WiFi.PrivateKeyFile");
386 ssid->private_key_passphrase = connman_network_get_string(network,
387 "WiFi.PrivateKeyPassphrase");
388 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
390 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
391 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
395 static int network_connect(struct connman_network *network)
397 struct connman_device *device = connman_network_get_device(network);
398 struct wifi_data *wifi;
399 GSupplicantInterface *interface;
400 GSupplicantSSID *ssid;
402 DBG("network %p", network);
407 wifi = connman_device_get_data(device);
411 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
415 interface = wifi->interface;
417 ssid_init(ssid, network);
419 if (wifi->disconnecting == TRUE)
420 wifi->pending_network = connman_network_ref(network);
422 wifi->network = connman_network_ref(network);
424 return g_supplicant_interface_connect(interface, ssid,
425 connect_callback, NULL);
431 static void disconnect_callback(int result, GSupplicantInterface *interface,
434 struct wifi_data *wifi = user_data;
436 if (wifi->network != NULL) {
438 * if result < 0 supplican return an error because
439 * the network is not current.
440 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
441 * failed, call connman_network_set_connected to report
442 * disconnect is completed.
445 connman_network_set_connected(wifi->network, FALSE);
447 connman_network_unref(wifi->network);
450 wifi->network = NULL;
452 wifi->disconnecting = FALSE;
454 if (wifi->pending_network != NULL) {
455 network_connect(wifi->pending_network);
456 connman_network_unref(wifi->pending_network);
457 wifi->pending_network = NULL;
462 static int network_disconnect(struct connman_network *network)
464 struct connman_device *device = connman_network_get_device(network);
465 struct wifi_data *wifi;
468 DBG("network %p", network);
470 wifi = connman_device_get_data(device);
471 if (wifi == NULL || wifi->interface == NULL)
474 connman_network_set_associating(network, FALSE);
476 if (wifi->disconnecting == TRUE)
479 wifi->disconnecting = TRUE;
481 err = g_supplicant_interface_disconnect(wifi->interface,
482 disconnect_callback, wifi);
484 wifi->disconnecting = FALSE;
489 static struct connman_network_driver network_driver = {
491 .type = CONNMAN_NETWORK_TYPE_WIFI,
492 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
493 .probe = network_probe,
494 .remove = network_remove,
495 .connect = network_connect,
496 .disconnect = network_disconnect,
499 static void interface_added(GSupplicantInterface *interface)
501 const char *ifname = g_supplicant_interface_get_ifname(interface);
502 const char *driver = g_supplicant_interface_get_driver(interface);
503 struct wifi_data *wifi;
505 wifi = g_supplicant_interface_get_data(interface);
508 * We can get here with a NULL wifi pointer when
509 * the interface added signal is sent before the
510 * interface creation callback is called.
515 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
517 if (wifi->device == NULL) {
518 connman_error("WiFi device not set");
522 connman_device_set_powered(wifi->device, TRUE);
523 wifi_scan(wifi->device);
526 static connman_bool_t is_idle(struct wifi_data *wifi)
528 DBG("state %d", wifi->state);
530 switch (wifi->state) {
531 case G_SUPPLICANT_STATE_UNKNOWN:
532 case G_SUPPLICANT_STATE_DISCONNECTED:
533 case G_SUPPLICANT_STATE_INACTIVE:
534 case G_SUPPLICANT_STATE_SCANNING:
537 case G_SUPPLICANT_STATE_AUTHENTICATING:
538 case G_SUPPLICANT_STATE_ASSOCIATING:
539 case G_SUPPLICANT_STATE_ASSOCIATED:
540 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
541 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
542 case G_SUPPLICANT_STATE_COMPLETED:
549 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
550 struct wifi_data *wifi)
552 /* First, let's check if WPS processing did not went wrong */
553 if (g_supplicant_interface_get_wps_state(interface) ==
554 G_SUPPLICANT_WPS_STATE_FAIL)
557 /* Unlike normal connection, being associated while processing wps
558 * actually means that we are idling. */
559 switch (wifi->state) {
560 case G_SUPPLICANT_STATE_UNKNOWN:
561 case G_SUPPLICANT_STATE_DISCONNECTED:
562 case G_SUPPLICANT_STATE_INACTIVE:
563 case G_SUPPLICANT_STATE_SCANNING:
564 case G_SUPPLICANT_STATE_ASSOCIATED:
566 case G_SUPPLICANT_STATE_AUTHENTICATING:
567 case G_SUPPLICANT_STATE_ASSOCIATING:
568 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
569 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
570 case G_SUPPLICANT_STATE_COMPLETED:
577 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
578 struct connman_network *network,
579 struct connman_device *device,
580 struct wifi_data *wifi)
584 wps = connman_network_get_bool(network, "WiFi.UseWPS");
586 const unsigned char *ssid, *wps_ssid;
587 unsigned int ssid_len, wps_ssid_len;
590 /* Checking if we got associated with requested
592 ssid = connman_network_get_blob(network, "WiFi.SSID",
595 wps_ssid = g_supplicant_interface_get_wps_ssid(
596 interface, &wps_ssid_len);
598 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
599 memcmp(ssid, wps_ssid, ssid_len) != 0) {
600 connman_network_set_associating(network, FALSE);
601 g_supplicant_interface_disconnect(wifi->interface,
602 disconnect_callback, wifi);
606 wps_key = g_supplicant_interface_get_wps_key(interface);
607 connman_network_set_string(network, "WiFi.Passphrase",
610 connman_network_set_string(network, "WiFi.PinWPS", NULL);
616 static void interface_state(GSupplicantInterface *interface)
618 struct connman_network *network;
619 struct connman_device *device;
620 struct wifi_data *wifi;
621 GSupplicantState state = g_supplicant_interface_get_state(interface);
622 unsigned char bssid[ETH_ALEN];
623 unsigned int bssid_len;
626 wifi = g_supplicant_interface_get_data(interface);
628 DBG("wifi %p interface state %d", wifi, state);
633 network = wifi->network;
634 device = wifi->device;
636 if (device == NULL || network == NULL)
640 case G_SUPPLICANT_STATE_SCANNING:
643 case G_SUPPLICANT_STATE_AUTHENTICATING:
644 case G_SUPPLICANT_STATE_ASSOCIATING:
645 connman_network_set_associating(network, TRUE);
648 case G_SUPPLICANT_STATE_COMPLETED:
649 if (handle_wps_completion(interface, network, device, wifi) ==
653 /* reset scan trigger and schedule background scan */
654 connman_device_schedule_scan(device);
656 if (get_bssid(device, bssid, &bssid_len) == 0)
657 connman_network_set_address(network,
659 connman_network_set_connected(network, TRUE);
662 case G_SUPPLICANT_STATE_DISCONNECTED:
664 * If we're in one of the idle modes, we have
665 * not started association yet and thus setting
666 * those ones to FALSE could cancel an association
669 wps = connman_network_get_bool(network, "WiFi.UseWPS");
671 if (is_idle_wps(interface, wifi) == TRUE)
676 connman_network_set_associating(network, FALSE);
677 connman_network_set_connected(network, FALSE);
680 case G_SUPPLICANT_STATE_INACTIVE:
681 connman_network_set_associating(network, FALSE);
684 case G_SUPPLICANT_STATE_UNKNOWN:
685 case G_SUPPLICANT_STATE_ASSOCIATED:
686 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
687 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
696 static void interface_removed(GSupplicantInterface *interface)
698 const char *ifname = g_supplicant_interface_get_ifname(interface);
699 struct wifi_data *wifi;
701 DBG("ifname %s", ifname);
703 wifi = g_supplicant_interface_get_data(interface);
705 if (wifi == NULL || wifi->device == NULL) {
706 connman_error("Wrong wifi pointer");
710 connman_device_set_powered(wifi->device, FALSE);
713 static void scan_started(GSupplicantInterface *interface)
715 struct wifi_data *wifi;
719 wifi = g_supplicant_interface_get_data(interface);
725 static void scan_finished(GSupplicantInterface *interface)
727 struct wifi_data *wifi;
731 wifi = g_supplicant_interface_get_data(interface);
737 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
739 unsigned char strength;
741 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
748 static void network_added(GSupplicantNetwork *supplicant_network)
750 struct connman_network *network;
751 GSupplicantInterface *interface;
752 struct wifi_data *wifi;
753 const char *name, *identifier, *mode, *security, *group;
754 const unsigned char *ssid;
755 unsigned int ssid_len;
760 interface = g_supplicant_network_get_interface(supplicant_network);
761 wifi = g_supplicant_interface_get_data(interface);
762 name = g_supplicant_network_get_name(supplicant_network);
763 identifier = g_supplicant_network_get_identifier(supplicant_network);
764 mode = g_supplicant_network_get_mode(supplicant_network);
765 security = g_supplicant_network_get_security(supplicant_network);
766 group = g_supplicant_network_get_identifier(supplicant_network);
767 wps = g_supplicant_network_get_wps(supplicant_network);
772 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
774 network = connman_device_get_network(wifi->device, identifier);
776 if (network == NULL) {
777 network = connman_network_create(identifier,
778 CONNMAN_NETWORK_TYPE_WIFI);
782 connman_network_set_index(network, wifi->index);
784 if (connman_device_add_network(wifi->device, network) < 0) {
785 connman_network_unref(network);
790 if (name != NULL && name[0] != '\0')
791 connman_network_set_name(network, name);
793 connman_network_set_blob(network, "WiFi.SSID",
795 connman_network_set_string(network, "WiFi.Mode", mode);
796 connman_network_set_string(network, "WiFi.Security", security);
797 connman_network_set_strength(network,
798 calculate_strength(supplicant_network));
799 connman_network_set_bool(network, "WiFi.WPS", wps);
801 connman_network_set_available(network, TRUE);
804 connman_network_set_group(network, group);
807 static void network_removed(GSupplicantNetwork *network)
809 GSupplicantInterface *interface;
810 struct wifi_data *wifi;
811 const char *name, *identifier;
813 interface = g_supplicant_network_get_interface(network);
814 wifi = g_supplicant_interface_get_data(interface);
815 identifier = g_supplicant_network_get_identifier(network);
816 name = g_supplicant_network_get_name(network);
818 DBG("name %s", name);
821 connman_device_remove_network(wifi->device, identifier);
824 static void debug(const char *str)
826 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
827 connman_debug("%s", str);
830 static const GSupplicantCallbacks callbacks = {
831 .system_ready = system_ready,
832 .system_killed = system_killed,
833 .interface_added = interface_added,
834 .interface_state = interface_state,
835 .interface_removed = interface_removed,
836 .scan_started = scan_started,
837 .scan_finished = scan_finished,
838 .network_added = network_added,
839 .network_removed = network_removed,
844 static int tech_probe(struct connman_technology *technology)
846 wifi_technology = technology;
851 static void tech_remove(struct connman_technology *technology)
853 wifi_technology = NULL;
856 static void regdom_callback(void *user_data)
858 char *alpha2 = user_data;
862 if (wifi_technology == NULL)
865 connman_technology_regdom_notify(wifi_technology, alpha2);
868 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
870 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
873 static struct connman_technology_driver tech_driver = {
875 .type = CONNMAN_SERVICE_TYPE_WIFI,
877 .remove = tech_remove,
878 .set_regdom = tech_set_regdom,
881 static int wifi_init(void)
885 err = connman_network_driver_register(&network_driver);
889 err = g_supplicant_register(&callbacks);
891 connman_network_driver_unregister(&network_driver);
895 err = connman_technology_driver_register(&tech_driver);
897 g_supplicant_unregister(&callbacks);
898 connman_network_driver_unregister(&network_driver);
905 static void wifi_exit(void)
909 connman_technology_driver_unregister(&tech_driver);
911 g_supplicant_unregister(&callbacks);
913 connman_network_driver_unregister(&network_driver);
916 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
917 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)