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, NULL,
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",
357 security = connman_network_get_string(network, "WiFi.Security");
358 ssid->security = network_security(security);
359 passphrase = connman_network_get_string(network,
361 if (passphrase == NULL || strlen(passphrase) == 0)
362 ssid->passphrase = NULL;
364 ssid->passphrase = passphrase;
366 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
369 * If our private key password is unset,
370 * we use the supplied passphrase. That is needed
371 * for PEAP where 2 passphrases (identity and client
372 * cert may have to be provided.
374 if (connman_network_get_string(network,
375 "WiFi.PrivateKeyPassphrase") == NULL)
376 connman_network_set_string(network,
377 "WiFi.PrivateKeyPassphrase",
379 /* We must have an identity for both PEAP and TLS */
380 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
381 ssid->ca_cert_path = connman_network_get_string(network,
383 ssid->client_cert_path = connman_network_get_string(network,
384 "WiFi.ClientCertFile");
385 ssid->private_key_path = connman_network_get_string(network,
386 "WiFi.PrivateKeyFile");
387 ssid->private_key_passphrase = connman_network_get_string(network,
388 "WiFi.PrivateKeyPassphrase");
389 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
391 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
392 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
396 static int network_connect(struct connman_network *network)
398 struct connman_device *device = connman_network_get_device(network);
399 struct wifi_data *wifi;
400 GSupplicantInterface *interface;
401 GSupplicantSSID *ssid;
403 DBG("network %p", network);
408 wifi = connman_device_get_data(device);
412 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
416 interface = wifi->interface;
418 ssid_init(ssid, network);
420 if (wifi->disconnecting == TRUE)
421 wifi->pending_network = connman_network_ref(network);
423 wifi->network = connman_network_ref(network);
425 return g_supplicant_interface_connect(interface, ssid,
426 connect_callback, NULL);
432 static void disconnect_callback(int result, GSupplicantInterface *interface,
435 struct wifi_data *wifi = user_data;
437 if (wifi->network != NULL) {
439 * if result < 0 supplican return an error because
440 * the network is not current.
441 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
442 * failed, call connman_network_set_connected to report
443 * disconnect is completed.
446 connman_network_set_connected(wifi->network, FALSE);
448 connman_network_unref(wifi->network);
451 wifi->network = NULL;
453 wifi->disconnecting = FALSE;
455 if (wifi->pending_network != NULL) {
456 network_connect(wifi->pending_network);
457 connman_network_unref(wifi->pending_network);
458 wifi->pending_network = NULL;
463 static int network_disconnect(struct connman_network *network)
465 struct connman_device *device = connman_network_get_device(network);
466 struct wifi_data *wifi;
469 DBG("network %p", network);
471 wifi = connman_device_get_data(device);
472 if (wifi == NULL || wifi->interface == NULL)
475 connman_network_set_associating(network, FALSE);
477 if (wifi->disconnecting == TRUE)
480 wifi->disconnecting = TRUE;
482 err = g_supplicant_interface_disconnect(wifi->interface,
483 disconnect_callback, wifi);
485 wifi->disconnecting = FALSE;
490 static struct connman_network_driver network_driver = {
492 .type = CONNMAN_NETWORK_TYPE_WIFI,
493 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
494 .probe = network_probe,
495 .remove = network_remove,
496 .connect = network_connect,
497 .disconnect = network_disconnect,
500 static void interface_added(GSupplicantInterface *interface)
502 const char *ifname = g_supplicant_interface_get_ifname(interface);
503 const char *driver = g_supplicant_interface_get_driver(interface);
504 struct wifi_data *wifi;
506 wifi = g_supplicant_interface_get_data(interface);
509 * We can get here with a NULL wifi pointer when
510 * the interface added signal is sent before the
511 * interface creation callback is called.
516 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
518 if (wifi->device == NULL) {
519 connman_error("WiFi device not set");
523 connman_device_set_powered(wifi->device, TRUE);
524 wifi_scan(wifi->device);
527 static connman_bool_t is_idle(struct wifi_data *wifi)
529 DBG("state %d", wifi->state);
531 switch (wifi->state) {
532 case G_SUPPLICANT_STATE_UNKNOWN:
533 case G_SUPPLICANT_STATE_DISCONNECTED:
534 case G_SUPPLICANT_STATE_INACTIVE:
535 case G_SUPPLICANT_STATE_SCANNING:
538 case G_SUPPLICANT_STATE_AUTHENTICATING:
539 case G_SUPPLICANT_STATE_ASSOCIATING:
540 case G_SUPPLICANT_STATE_ASSOCIATED:
541 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
542 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
543 case G_SUPPLICANT_STATE_COMPLETED:
550 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
551 struct wifi_data *wifi)
553 /* First, let's check if WPS processing did not went wrong */
554 if (g_supplicant_interface_get_wps_state(interface) ==
555 G_SUPPLICANT_WPS_STATE_FAIL)
558 /* Unlike normal connection, being associated while processing wps
559 * actually means that we are idling. */
560 switch (wifi->state) {
561 case G_SUPPLICANT_STATE_UNKNOWN:
562 case G_SUPPLICANT_STATE_DISCONNECTED:
563 case G_SUPPLICANT_STATE_INACTIVE:
564 case G_SUPPLICANT_STATE_SCANNING:
565 case G_SUPPLICANT_STATE_ASSOCIATED:
567 case G_SUPPLICANT_STATE_AUTHENTICATING:
568 case G_SUPPLICANT_STATE_ASSOCIATING:
569 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
570 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
571 case G_SUPPLICANT_STATE_COMPLETED:
578 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
579 struct connman_network *network,
580 struct connman_device *device,
581 struct wifi_data *wifi)
585 wps = connman_network_get_bool(network, "WiFi.UseWPS");
587 const unsigned char *ssid, *wps_ssid;
588 unsigned int ssid_len, wps_ssid_len;
591 /* Checking if we got associated with requested
593 ssid = connman_network_get_blob(network, "WiFi.SSID",
596 wps_ssid = g_supplicant_interface_get_wps_ssid(
597 interface, &wps_ssid_len);
599 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
600 memcmp(ssid, wps_ssid, ssid_len) != 0) {
601 connman_network_set_associating(network, FALSE);
602 g_supplicant_interface_disconnect(wifi->interface,
603 disconnect_callback, wifi);
607 wps_key = g_supplicant_interface_get_wps_key(interface);
608 connman_network_set_string(network, "WiFi.Passphrase",
611 connman_network_set_string(network, "WiFi.PinWPS", NULL);
617 static void interface_state(GSupplicantInterface *interface)
619 struct connman_network *network;
620 struct connman_device *device;
621 struct wifi_data *wifi;
622 GSupplicantState state = g_supplicant_interface_get_state(interface);
623 unsigned char bssid[ETH_ALEN];
624 unsigned int bssid_len;
627 wifi = g_supplicant_interface_get_data(interface);
629 DBG("wifi %p interface state %d", wifi, state);
634 network = wifi->network;
635 device = wifi->device;
637 if (device == NULL || network == NULL)
641 case G_SUPPLICANT_STATE_SCANNING:
644 case G_SUPPLICANT_STATE_AUTHENTICATING:
645 case G_SUPPLICANT_STATE_ASSOCIATING:
646 connman_network_set_associating(network, TRUE);
649 case G_SUPPLICANT_STATE_COMPLETED:
650 if (handle_wps_completion(interface, network, device, wifi) ==
654 /* reset scan trigger and schedule background scan */
655 connman_device_schedule_scan(device);
657 if (get_bssid(device, bssid, &bssid_len) == 0)
658 connman_network_set_address(network,
660 connman_network_set_connected(network, TRUE);
663 case G_SUPPLICANT_STATE_DISCONNECTED:
665 * If we're in one of the idle modes, we have
666 * not started association yet and thus setting
667 * those ones to FALSE could cancel an association
670 wps = connman_network_get_bool(network, "WiFi.UseWPS");
672 if (is_idle_wps(interface, wifi) == TRUE)
677 connman_network_set_associating(network, FALSE);
678 connman_network_set_connected(network, FALSE);
681 case G_SUPPLICANT_STATE_INACTIVE:
682 connman_network_set_associating(network, FALSE);
685 case G_SUPPLICANT_STATE_UNKNOWN:
686 case G_SUPPLICANT_STATE_ASSOCIATED:
687 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
688 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
697 static void interface_removed(GSupplicantInterface *interface)
699 const char *ifname = g_supplicant_interface_get_ifname(interface);
700 struct wifi_data *wifi;
702 DBG("ifname %s", ifname);
704 wifi = g_supplicant_interface_get_data(interface);
706 if (wifi == NULL || wifi->device == NULL) {
707 connman_error("Wrong wifi pointer");
711 connman_device_set_powered(wifi->device, FALSE);
714 static void scan_started(GSupplicantInterface *interface)
716 struct wifi_data *wifi;
720 wifi = g_supplicant_interface_get_data(interface);
726 static void scan_finished(GSupplicantInterface *interface)
728 struct wifi_data *wifi;
732 wifi = g_supplicant_interface_get_data(interface);
738 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
740 unsigned char strength;
742 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
749 static void network_added(GSupplicantNetwork *supplicant_network)
751 struct connman_network *network;
752 GSupplicantInterface *interface;
753 struct wifi_data *wifi;
754 const char *name, *identifier, *mode, *security, *group;
755 const unsigned char *ssid;
756 unsigned int ssid_len;
761 interface = g_supplicant_network_get_interface(supplicant_network);
762 wifi = g_supplicant_interface_get_data(interface);
763 name = g_supplicant_network_get_name(supplicant_network);
764 identifier = g_supplicant_network_get_identifier(supplicant_network);
765 mode = g_supplicant_network_get_mode(supplicant_network);
766 security = g_supplicant_network_get_security(supplicant_network);
767 group = g_supplicant_network_get_identifier(supplicant_network);
768 wps = g_supplicant_network_get_wps(supplicant_network);
773 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
775 network = connman_device_get_network(wifi->device, identifier);
777 if (network == NULL) {
778 network = connman_network_create(identifier,
779 CONNMAN_NETWORK_TYPE_WIFI);
783 connman_network_set_index(network, wifi->index);
785 if (connman_device_add_network(wifi->device, network) < 0) {
786 connman_network_unref(network);
791 if (name != NULL && name[0] != '\0')
792 connman_network_set_name(network, name);
794 connman_network_set_blob(network, "WiFi.SSID",
796 connman_network_set_string(network, "WiFi.Mode", mode);
797 connman_network_set_string(network, "WiFi.Security", security);
798 connman_network_set_strength(network,
799 calculate_strength(supplicant_network));
800 connman_network_set_bool(network, "WiFi.WPS", wps);
802 connman_network_set_available(network, TRUE);
805 connman_network_set_group(network, group);
808 static void network_removed(GSupplicantNetwork *network)
810 GSupplicantInterface *interface;
811 struct wifi_data *wifi;
812 const char *name, *identifier;
814 interface = g_supplicant_network_get_interface(network);
815 wifi = g_supplicant_interface_get_data(interface);
816 identifier = g_supplicant_network_get_identifier(network);
817 name = g_supplicant_network_get_name(network);
819 DBG("name %s", name);
822 connman_device_remove_network(wifi->device, identifier);
825 static void debug(const char *str)
827 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
828 connman_debug("%s", str);
831 static const GSupplicantCallbacks callbacks = {
832 .system_ready = system_ready,
833 .system_killed = system_killed,
834 .interface_added = interface_added,
835 .interface_state = interface_state,
836 .interface_removed = interface_removed,
837 .scan_started = scan_started,
838 .scan_finished = scan_finished,
839 .network_added = network_added,
840 .network_removed = network_removed,
845 static int tech_probe(struct connman_technology *technology)
847 wifi_technology = technology;
852 static void tech_remove(struct connman_technology *technology)
854 wifi_technology = NULL;
857 static void regdom_callback(void *user_data)
859 char *alpha2 = user_data;
863 if (wifi_technology == NULL)
866 connman_technology_regdom_notify(wifi_technology, alpha2);
869 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
871 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
874 static struct connman_technology_driver tech_driver = {
876 .type = CONNMAN_SERVICE_TYPE_WIFI,
878 .remove = tech_remove,
879 .set_regdom = tech_set_regdom,
882 static int wifi_init(void)
886 err = connman_network_driver_register(&network_driver);
890 err = g_supplicant_register(&callbacks);
892 connman_network_driver_unregister(&network_driver);
896 err = connman_technology_driver_register(&tech_driver);
898 g_supplicant_unregister(&callbacks);
899 connman_network_driver_unregister(&network_driver);
906 static void wifi_exit(void)
910 connman_technology_driver_unregister(&tech_driver);
912 g_supplicant_unregister(&callbacks);
914 connman_network_driver_unregister(&network_driver);
917 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
918 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)