5 * Copyright (C) 2007-2009 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
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
39 #define CONNMAN_API_SUBJECT_TO_CHANGE
40 #include <connman/device.h>
41 #include <connman/option.h>
42 #include <connman/inet.h>
43 #include <connman/dbus.h>
44 #include <connman/log.h>
46 #include "supplicant.h"
50 #define IEEE80211_CAP_ESS 0x0001
51 #define IEEE80211_CAP_IBSS 0x0002
52 #define IEEE80211_CAP_PRIVACY 0x0010
54 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
55 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
56 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
58 /* Taken from "WPA Supplicant - Common definitions" */
59 enum supplicant_state {
61 * WPA_DISCONNECTED - Disconnected state
63 * This state indicates that client is not associated, but is likely to
64 * start looking for an access point. This state is entered when a
70 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
72 * This state is entered if there are no enabled networks in the
73 * configuration. wpa_supplicant is not trying to associate with a new
74 * network and external interaction (e.g., ctrl_iface call to add or
75 * enable a network) is needed to start association.
80 * WPA_SCANNING - Scanning for a network
82 * This state is entered when wpa_supplicant starts scanning for a
88 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
90 * This state is entered when wpa_supplicant has found a suitable BSS
91 * to associate with and the driver is configured to try to associate
92 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
93 * state is entered when the driver is configured to try to associate
94 * with a network using the configured SSID and security policy.
99 * WPA_ASSOCIATED - Association completed
101 * This state is entered when the driver reports that association has
102 * been successfully completed with an AP. If IEEE 802.1X is used
103 * (with or without WPA/WPA2), wpa_supplicant remains in this state
104 * until the IEEE 802.1X/EAPOL authentication has been completed.
109 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
111 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
112 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
113 * frame after association. In case of WPA-EAP, this state is entered
114 * when the IEEE 802.1X/EAPOL authentication has been completed.
119 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
121 * This state is entered when 4-Way Key Handshake has been completed
122 * (i.e., when the supplicant sends out message 4/4) and when Group
123 * Key rekeying is started by the AP (i.e., when supplicant receives
129 * WPA_COMPLETED - All authentication completed
131 * This state is entered when the full authentication process is
132 * completed. In case of WPA2, this happens when the 4-Way Handshake is
133 * successfully completed. With WPA, this state is entered after the
134 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
135 * completed after dynamic keys are received (or if not used, after
136 * the EAP authentication has been completed). With static WEP keys and
137 * plaintext connections, this state is entered when an association
138 * has been completed.
140 * This state indicates that the supplicant has completed its
141 * processing for the association phase and that data connection is
147 * WPA_INVALID - Invalid state (parsing error)
149 * This state is returned if the string input is invalid. It is not
150 * an official wpa_supplicant state.
155 struct supplicant_result {
159 unsigned int addr_len;
161 unsigned int ssid_len;
162 dbus_uint16_t capabilities;
170 dbus_int32_t frequency;
171 dbus_int32_t quality;
174 dbus_int32_t maxrate;
177 struct supplicant_task {
181 struct connman_device *device;
182 struct connman_network *network;
183 struct connman_network *pending_network;
187 enum supplicant_state state;
189 GSList *scan_results;
190 DBusPendingCall *scan_call;
191 DBusPendingCall *result_call;
192 struct iw_range *range;
193 gboolean disconnecting;
196 static GSList *task_list = NULL;
198 static DBusConnection *connection;
200 static void free_task(struct supplicant_task *task)
202 DBG("task %p", task);
204 g_free(task->ifname);
209 static struct supplicant_task *find_task_by_index(int index)
213 for (list = task_list; list; list = list->next) {
214 struct supplicant_task *task = list->data;
216 if (task->ifindex == index)
223 static struct supplicant_task *find_task_by_path(const char *path)
227 for (list = task_list; list; list = list->next) {
228 struct supplicant_task *task = list->data;
230 if (g_strcmp0(task->path, path) == 0)
237 static int get_range(struct supplicant_task *task)
242 fd = socket(PF_INET, SOCK_DGRAM, 0);
246 memset(&wrq, 0, sizeof(struct iwreq));
247 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
248 wrq.u.data.pointer = task->range;
249 wrq.u.data.length = sizeof(struct iw_range);
251 err = ioctl(fd, SIOCGIWRANGE, &wrq);
256 task->range->max_qual.updated |= IW_QUAL_ALL_INVALID;
258 connman_info("%s {scan} capabilities 0x%02x", task->ifname,
259 task->range->scan_capa);
261 connman_info("%s {quality} flags 0x%02x", task->ifname,
262 task->range->max_qual.updated);
267 static int get_bssid(struct connman_device *device,
268 unsigned char *bssid, unsigned int *bssid_len)
275 ifindex = connman_device_get_index(device);
279 ifname = connman_inet_ifname(ifindex);
283 fd = socket(PF_INET, SOCK_DGRAM, 0);
289 memset(&wrq, 0, sizeof(wrq));
290 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
292 err = ioctl(fd, SIOCGIWAP, &wrq);
300 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
301 *bssid_len = ETH_ALEN;
306 static void add_interface_reply(DBusPendingCall *call, void *user_data)
308 struct supplicant_task *task = user_data;
313 DBG("task %p", task);
315 reply = dbus_pending_call_steal_reply(call);
319 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
322 dbus_error_init(&error);
324 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
325 DBUS_TYPE_INVALID) == FALSE) {
326 if (dbus_error_is_set(&error) == TRUE) {
327 connman_error("%s", error.message);
328 dbus_error_free(&error);
330 connman_error("Wrong arguments for add interface");
334 DBG("path %s", path);
336 task->path = g_strdup(path);
337 task->created = TRUE;
339 connman_device_set_powered(task->device, TRUE);
341 dbus_message_unref(reply);
346 dbus_message_unref(reply);
348 task_list = g_slist_remove(task_list, task);
350 connman_device_unref(task->device);
355 static int add_interface(struct supplicant_task *task)
357 const char *driver = connman_option_get_string("wifi");
358 DBusMessage *message;
359 DBusMessageIter array, dict;
360 DBusPendingCall *call;
362 DBG("task %p", task);
364 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
365 SUPPLICANT_INTF, "addInterface");
369 dbus_message_set_auto_start(message, FALSE);
371 dbus_message_iter_init_append(message, &array);
373 dbus_message_iter_append_basic(&array,
374 DBUS_TYPE_STRING, &task->ifname);
376 connman_dbus_dict_open(&array, &dict);
378 connman_dbus_dict_append_basic(&dict, "driver",
379 DBUS_TYPE_STRING, &driver);
381 connman_dbus_dict_close(&array, &dict);
383 if (dbus_connection_send_with_reply(connection, message,
384 &call, TIMEOUT) == FALSE) {
385 connman_error("Failed to add interface");
386 dbus_message_unref(message);
391 connman_error("D-Bus connection not available");
392 dbus_message_unref(message);
396 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
398 dbus_message_unref(message);
403 static void get_interface_reply(DBusPendingCall *call, void *user_data)
405 struct supplicant_task *task = user_data;
410 DBG("task %p", task);
412 reply = dbus_pending_call_steal_reply(call);
416 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
421 dbus_error_init(&error);
423 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
424 DBUS_TYPE_INVALID) == FALSE) {
425 if (dbus_error_is_set(&error) == TRUE) {
426 connman_error("%s", error.message);
427 dbus_error_free(&error);
429 connman_error("Wrong arguments for get interface");
433 DBG("path %s", path);
435 task->path = g_strdup(path);
436 task->created = FALSE;
438 connman_device_set_powered(task->device, TRUE);
441 dbus_message_unref(reply);
444 static int create_interface(struct supplicant_task *task)
446 DBusMessage *message;
447 DBusPendingCall *call;
449 DBG("task %p", task);
451 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
452 SUPPLICANT_INTF, "getInterface");
456 dbus_message_set_auto_start(message, FALSE);
458 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
461 if (dbus_connection_send_with_reply(connection, message,
462 &call, TIMEOUT) == FALSE) {
463 connman_error("Failed to get interface");
464 dbus_message_unref(message);
469 connman_error("D-Bus connection not available");
470 dbus_message_unref(message);
474 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
476 dbus_message_unref(message);
481 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
483 struct supplicant_task *task = user_data;
486 DBG("task %p", task);
488 reply = dbus_pending_call_steal_reply(call);
490 connman_device_set_powered(task->device, FALSE);
492 connman_device_unref(task->device);
494 connman_inet_ifdown(task->ifindex);
498 dbus_message_unref(reply);
501 static int remove_interface(struct supplicant_task *task)
503 DBusMessage *message;
504 DBusPendingCall *call;
506 DBG("task %p", task);
509 if (task->created == FALSE) {
510 connman_device_set_powered(task->device, FALSE);
515 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
516 SUPPLICANT_INTF, "removeInterface");
520 dbus_message_set_auto_start(message, FALSE);
522 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
525 if (dbus_connection_send_with_reply(connection, message,
526 &call, TIMEOUT) == FALSE) {
527 connman_error("Failed to remove interface");
528 dbus_message_unref(message);
533 connman_error("D-Bus connection not available");
534 dbus_message_unref(message);
538 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
540 dbus_message_unref(message);
545 static int set_ap_scan(struct supplicant_task *task)
547 DBusMessage *message, *reply;
551 DBG("task %p", task);
553 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
554 SUPPLICANT_INTF ".Interface", "setAPScan");
558 dbus_message_set_auto_start(message, FALSE);
560 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
563 dbus_error_init(&error);
565 reply = dbus_connection_send_with_reply_and_block(connection,
566 message, -1, &error);
568 if (dbus_error_is_set(&error) == TRUE) {
569 connman_error("%s", error.message);
570 dbus_error_free(&error);
572 connman_error("Failed to set AP scan");
573 dbus_message_unref(message);
577 dbus_message_unref(message);
579 dbus_message_unref(reply);
584 static int add_network(struct supplicant_task *task)
586 DBusMessage *message, *reply;
590 DBG("task %p", task);
592 if (task->netpath != NULL)
595 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
596 SUPPLICANT_INTF ".Interface", "addNetwork");
600 dbus_message_set_auto_start(message, FALSE);
602 dbus_error_init(&error);
604 reply = dbus_connection_send_with_reply_and_block(connection,
605 message, -1, &error);
607 if (dbus_error_is_set(&error) == TRUE) {
608 connman_error("%s", error.message);
609 dbus_error_free(&error);
611 connman_error("Failed to add network");
612 dbus_message_unref(message);
616 dbus_message_unref(message);
618 dbus_error_init(&error);
620 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
621 DBUS_TYPE_INVALID) == FALSE) {
622 if (dbus_error_is_set(&error) == TRUE) {
623 connman_error("%s", error.message);
624 dbus_error_free(&error);
626 connman_error("Wrong arguments for network");
627 dbus_message_unref(reply);
631 DBG("path %s", path);
633 task->netpath = g_strdup(path);
635 dbus_message_unref(reply);
640 static int remove_network(struct supplicant_task *task)
642 DBusMessage *message, *reply;
645 DBG("task %p", task);
647 if (task->netpath == NULL)
650 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
651 SUPPLICANT_INTF ".Interface", "removeNetwork");
655 dbus_message_set_auto_start(message, FALSE);
657 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
660 dbus_error_init(&error);
662 reply = dbus_connection_send_with_reply_and_block(connection,
663 message, -1, &error);
665 if (dbus_error_is_set(&error) == TRUE) {
666 connman_error("%s", error.message);
667 dbus_error_free(&error);
669 connman_error("Failed to remove network");
670 dbus_message_unref(message);
674 dbus_message_unref(message);
676 dbus_message_unref(reply);
678 g_free(task->netpath);
679 task->netpath = NULL;
684 static int select_network(struct supplicant_task *task)
686 DBusMessage *message, *reply;
689 DBG("task %p", task);
691 if (task->netpath == NULL)
694 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
695 SUPPLICANT_INTF ".Interface", "selectNetwork");
699 dbus_message_set_auto_start(message, FALSE);
701 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
704 dbus_error_init(&error);
706 reply = dbus_connection_send_with_reply_and_block(connection,
707 message, -1, &error);
709 if (dbus_error_is_set(&error) == TRUE) {
710 connman_error("%s", error.message);
711 dbus_error_free(&error);
713 connman_error("Failed to select network");
714 dbus_message_unref(message);
718 dbus_message_unref(message);
720 dbus_message_unref(reply);
725 static int disconnect_network(struct supplicant_task *task)
727 DBusMessage *message, *reply;
730 DBG("task %p", task);
732 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
733 SUPPLICANT_INTF ".Interface", "disconnect");
737 dbus_message_set_auto_start(message, FALSE);
739 dbus_error_init(&error);
741 reply = dbus_connection_send_with_reply_and_block(connection,
742 message, -1, &error);
744 if (dbus_error_is_set(&error) == TRUE) {
745 connman_error("%s", error.message);
746 dbus_error_free(&error);
748 connman_error("Failed to disconnect network");
749 dbus_message_unref(message);
753 dbus_message_unref(message);
755 dbus_message_unref(reply);
760 static int set_network(struct supplicant_task *task,
761 const unsigned char *network, int len,
762 const char *address, const char *security,
763 const char *passphrase)
765 DBusMessage *message, *reply;
766 DBusMessageIter array, dict;
768 dbus_uint32_t scan_ssid = 1;
770 DBG("task %p", task);
772 if (task->netpath == NULL)
775 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
776 SUPPLICANT_INTF ".Network", "set");
780 dbus_message_set_auto_start(message, FALSE);
782 dbus_message_iter_init_append(message, &array);
784 connman_dbus_dict_open(&array, &dict);
786 connman_dbus_dict_append_basic(&dict, "scan_ssid",
787 DBUS_TYPE_UINT32, &scan_ssid);
790 connman_dbus_dict_append_fixed_array(&dict, "ssid",
791 DBUS_TYPE_BYTE, &network, len);
793 connman_dbus_dict_append_basic(&dict, "bssid",
794 DBUS_TYPE_STRING, &address);
796 if (g_ascii_strcasecmp(security, "psk") == 0 ||
797 g_ascii_strcasecmp(security, "wpa") == 0 ||
798 g_ascii_strcasecmp(security, "rsn") == 0) {
799 const char *key_mgmt = "WPA-PSK";
800 connman_dbus_dict_append_basic(&dict, "key_mgmt",
801 DBUS_TYPE_STRING, &key_mgmt);
803 if (passphrase && strlen(passphrase) > 0)
804 connman_dbus_dict_append_basic(&dict, "psk",
805 DBUS_TYPE_STRING, &passphrase);
806 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
807 const char *key_mgmt = "NONE";
808 const char *auth_alg = "OPEN";
809 const char *key_index = "0";
811 if (task->mac80211 == TRUE)
812 auth_alg = "OPEN SHARED";
814 connman_dbus_dict_append_basic(&dict, "auth_alg",
815 DBUS_TYPE_STRING, &auth_alg);
817 connman_dbus_dict_append_basic(&dict, "key_mgmt",
818 DBUS_TYPE_STRING, &key_mgmt);
821 int size = strlen(passphrase);
822 if (size == 10 || size == 26) {
823 unsigned char *key = malloc(13);
826 memset(tmp, 0, sizeof(tmp));
829 for (i = 0; i < size / 2; i++) {
830 memcpy(tmp, passphrase + (i * 2), 2);
831 key[i] = (unsigned char) strtol(tmp,
834 connman_dbus_dict_append_fixed_array(&dict,
835 "wep_key0", DBUS_TYPE_BYTE,
839 connman_dbus_dict_append_basic(&dict,
840 "wep_key0", DBUS_TYPE_STRING,
843 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
844 DBUS_TYPE_STRING, &key_index);
847 const char *key_mgmt = "NONE";
848 connman_dbus_dict_append_basic(&dict, "key_mgmt",
849 DBUS_TYPE_STRING, &key_mgmt);
852 connman_dbus_dict_close(&array, &dict);
854 dbus_error_init(&error);
856 reply = dbus_connection_send_with_reply_and_block(connection,
857 message, -1, &error);
859 if (dbus_error_is_set(&error) == TRUE) {
860 connman_error("%s", error.message);
861 dbus_error_free(&error);
863 connman_error("Failed to set network options");
864 dbus_message_unref(message);
868 dbus_message_unref(message);
870 dbus_message_unref(reply);
875 static void scan_reply(DBusPendingCall *call, void *user_data)
877 struct supplicant_task *task = user_data;
880 DBG("task %p", task);
882 task->scan_call = NULL;
884 reply = dbus_pending_call_steal_reply(call);
888 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
889 connman_device_set_scanning(task->device, FALSE);
893 if (task->scanning == TRUE)
894 connman_device_set_scanning(task->device, TRUE);
897 dbus_message_unref(reply);
901 static int initiate_scan(struct supplicant_task *task)
903 DBusMessage *message;
905 DBG("task %p", task);
907 if (task->path == NULL)
910 if (task->scan_call != NULL)
913 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
914 SUPPLICANT_INTF ".Interface", "scan");
918 dbus_message_set_auto_start(message, FALSE);
920 if (dbus_connection_send_with_reply(connection, message,
921 &task->scan_call, TIMEOUT) == FALSE) {
922 connman_error("Failed to initiate scan");
923 dbus_message_unref(message);
927 if (task->scan_call == NULL) {
928 connman_error("D-Bus connection not available");
929 dbus_message_unref(message);
933 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
935 dbus_message_unref(message);
944 { "<hidden>", "hidden" },
945 { "default", "linksys" },
953 { "comcomcom", "3com" },
955 { "Symbol", "symbol" },
956 { "Motorola", "motorola" },
957 { "Wireless" , "wireless" },
962 static char *build_group(const char *addr, const char *name,
963 const unsigned char *ssid, unsigned int ssid_len,
964 const char *mode, const char *security)
972 str = g_string_sized_new((ssid_len * 2) + 24);
977 g_string_append_printf(str, "hidden_%s", addr);
981 for (i = 0; special_ssid[i].name; i++) {
982 if (g_strcmp0(special_ssid[i].name, name) == 0) {
983 if (special_ssid[i].value == NULL)
984 g_string_append_printf(str, "%s_%s",
987 g_string_append_printf(str, "%s_%s",
988 special_ssid[i].value, addr);
993 if (ssid_len > 0 && ssid[0] != '\0') {
994 for (i = 0; i < ssid_len; i++)
995 g_string_append_printf(str, "%02x", ssid[i]);
997 g_string_append_printf(str, "hidden_%s", addr);
1000 g_string_append_printf(str, "_%s_%s", mode, security);
1002 return g_string_free(str, FALSE);
1005 static void extract_addr(DBusMessageIter *value,
1006 struct supplicant_result *result)
1008 DBusMessageIter array;
1009 struct ether_addr eth;
1010 unsigned char *addr;
1013 dbus_message_iter_recurse(value, &array);
1014 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1019 result->addr = g_try_malloc(addr_len);
1020 if (result->addr == NULL)
1023 memcpy(result->addr, addr, addr_len);
1024 result->addr_len = addr_len;
1026 result->path = g_try_malloc0(13);
1027 if (result->path == NULL)
1030 memcpy(ð, addr, sizeof(eth));
1031 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1032 eth.ether_addr_octet[0],
1033 eth.ether_addr_octet[1],
1034 eth.ether_addr_octet[2],
1035 eth.ether_addr_octet[3],
1036 eth.ether_addr_octet[4],
1037 eth.ether_addr_octet[5]);
1040 static void extract_ssid(DBusMessageIter *value,
1041 struct supplicant_result *result)
1043 DBusMessageIter array;
1044 unsigned char *ssid;
1047 dbus_message_iter_recurse(value, &array);
1048 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1053 if (ssid[0] == '\0')
1056 result->ssid = g_try_malloc(ssid_len);
1057 if (result->ssid == NULL)
1060 memcpy(result->ssid, ssid, ssid_len);
1061 result->ssid_len = ssid_len;
1063 result->name = g_try_malloc0(ssid_len + 1);
1064 if (result->name == NULL)
1067 for (i = 0; i < ssid_len; i++) {
1068 if (g_ascii_isprint(ssid[i]))
1069 result->name[i] = ssid[i];
1071 result->name[i] = ' ';
1075 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1076 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1078 static void extract_rsn(struct supplicant_result *result,
1079 const unsigned char *buf, int len)
1098 /* Pairwise cipher */
1102 count = buf[0] | (buf[1] << 8);
1103 if (2 + (count * 4) > len)
1106 buf += 2 + (count * 4);
1107 len -= 2 + (count * 4);
1109 /* Authentication */
1113 count = buf[0] | (buf[1] << 8);
1114 if (2 + (count * 4) > len)
1117 for (i = 0; i < count; i++) {
1118 const unsigned char *ptr = buf + 2 + (i * 4);
1120 if (memcmp(ptr, wifi_oui, 3) == 0) {
1123 result->has_8021x = TRUE;
1126 result->has_psk = TRUE;
1129 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1132 result->has_8021x = TRUE;
1135 result->has_psk = TRUE;
1141 buf += 2 + (count * 4);
1142 len -= 2 + (count * 4);
1145 static void extract_wpaie(DBusMessageIter *value,
1146 struct supplicant_result *result)
1148 DBusMessageIter array;
1152 dbus_message_iter_recurse(value, &array);
1153 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1156 result->has_wpa = TRUE;
1157 extract_rsn(result, ie + 6, ie_len - 6);
1161 static void extract_rsnie(DBusMessageIter *value,
1162 struct supplicant_result *result)
1164 DBusMessageIter array;
1168 dbus_message_iter_recurse(value, &array);
1169 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1172 result->has_rsn = TRUE;
1173 extract_rsn(result, ie + 2, ie_len - 2);
1177 static void extract_wpsie(DBusMessageIter *value,
1178 struct supplicant_result *result)
1180 DBusMessageIter array;
1184 dbus_message_iter_recurse(value, &array);
1185 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1188 result->has_wps = TRUE;
1191 static void extract_capabilites(DBusMessageIter *value,
1192 struct supplicant_result *result)
1194 dbus_message_iter_get_basic(value, &result->capabilities);
1196 if (result->capabilities & IEEE80211_CAP_ESS)
1197 result->adhoc = FALSE;
1198 else if (result->capabilities & IEEE80211_CAP_IBSS)
1199 result->adhoc = TRUE;
1201 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1202 result->has_wep = TRUE;
1205 static unsigned char calculate_strength(struct supplicant_task *task,
1206 struct supplicant_result *result)
1208 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1209 unsigned char strength;
1211 if (result->level > 0)
1212 strength = 100 - result->level;
1214 strength = 120 + result->level;
1222 return (result->quality * 100) / task->range->max_qual.qual;
1225 static unsigned short calculate_channel(struct supplicant_result *result)
1227 if (result->frequency < 0)
1230 return (result->frequency - 2407) / 5;
1233 static void get_properties(struct supplicant_task *task);
1235 static void properties_reply(DBusPendingCall *call, void *user_data)
1237 struct supplicant_task *task = user_data;
1238 struct supplicant_result result;
1239 struct connman_network *network;
1241 DBusMessageIter array, dict;
1242 unsigned char strength;
1243 unsigned short channel, frequency;
1244 const char *mode, *security;
1247 DBG("task %p", task);
1249 reply = dbus_pending_call_steal_reply(call);
1250 if (reply == NULL) {
1251 get_properties(task);
1255 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1256 dbus_message_unref(reply);
1257 get_properties(task);
1261 memset(&result, 0, sizeof(result));
1262 result.frequency = -1;
1263 result.quality = -1;
1267 dbus_message_iter_init(reply, &array);
1269 dbus_message_iter_recurse(&array, &dict);
1271 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1272 DBusMessageIter entry, value;
1275 dbus_message_iter_recurse(&dict, &entry);
1276 dbus_message_iter_get_basic(&entry, &key);
1278 dbus_message_iter_next(&entry);
1280 dbus_message_iter_recurse(&entry, &value);
1282 //type = dbus_message_iter_get_arg_type(&value);
1283 //dbus_message_iter_get_basic(&value, &val);
1291 * frequency : i (105)
1292 * capabilities : q (113)
1299 if (g_str_equal(key, "bssid") == TRUE)
1300 extract_addr(&value, &result);
1301 else if (g_str_equal(key, "ssid") == TRUE)
1302 extract_ssid(&value, &result);
1303 else if (g_str_equal(key, "wpaie") == TRUE)
1304 extract_wpaie(&value, &result);
1305 else if (g_str_equal(key, "rsnie") == TRUE)
1306 extract_rsnie(&value, &result);
1307 else if (g_str_equal(key, "wpsie") == TRUE)
1308 extract_wpsie(&value, &result);
1309 else if (g_str_equal(key, "capabilities") == TRUE)
1310 extract_capabilites(&value, &result);
1311 else if (g_str_equal(key, "frequency") == TRUE)
1312 dbus_message_iter_get_basic(&value, &result.frequency);
1313 else if (g_str_equal(key, "quality") == TRUE)
1314 dbus_message_iter_get_basic(&value, &result.quality);
1315 else if (g_str_equal(key, "noise") == TRUE)
1316 dbus_message_iter_get_basic(&value, &result.noise);
1317 else if (g_str_equal(key, "level") == TRUE)
1318 dbus_message_iter_get_basic(&value, &result.level);
1319 else if (g_str_equal(key, "maxrate") == TRUE)
1320 dbus_message_iter_get_basic(&value, &result.maxrate);
1322 dbus_message_iter_next(&dict);
1325 DBG("capabilties %u frequency %d "
1326 "quality %d noise %d level %d maxrate %d",
1327 result.capabilities, result.frequency,
1328 result.quality, result.noise,
1329 result.level, result.maxrate);
1331 if (result.path == NULL)
1334 if (result.path[0] == '\0')
1337 if (result.frequency > 0 && result.frequency < 14)
1338 result.frequency = 2407 + (5 * result.frequency);
1339 else if (result.frequency == 14)
1340 result.frequency = 2484;
1342 strength = calculate_strength(task, &result);
1343 channel = calculate_channel(&result);
1345 frequency = (result.frequency < 0) ? 0 : result.frequency;
1347 if (result.has_8021x == TRUE)
1348 security = "ieee8021x";
1349 else if (result.has_psk == TRUE)
1351 else if (result.has_wep == TRUE)
1356 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1358 group = build_group(result.path, result.name,
1359 result.ssid, result.ssid_len,
1362 if (result.has_psk == TRUE) {
1363 if (result.has_rsn == TRUE)
1365 else if (result.has_wpa == TRUE)
1369 network = connman_device_get_network(task->device, result.path);
1370 if (network == NULL) {
1373 network = connman_network_create(result.path,
1374 CONNMAN_NETWORK_TYPE_WIFI);
1375 if (network == NULL)
1378 index = connman_device_get_index(task->device);
1379 connman_network_set_index(network, index);
1381 connman_network_set_protocol(network,
1382 CONNMAN_NETWORK_PROTOCOL_IP);
1384 connman_network_set_address(network, result.addr,
1387 if (connman_device_add_network(task->device, network) < 0) {
1388 connman_network_unref(network);
1393 if (result.name != NULL && result.name[0] != '\0')
1394 connman_network_set_name(network, result.name);
1396 connman_network_set_blob(network, "WiFi.SSID",
1397 result.ssid, result.ssid_len);
1399 connman_network_set_string(network, "WiFi.Mode", mode);
1401 DBG("%s (%s %s) strength %d (%s)",
1402 result.name, mode, security, strength,
1403 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1405 connman_network_set_available(network, TRUE);
1406 connman_network_set_strength(network, strength);
1408 connman_network_set_uint16(network, "Frequency", frequency);
1409 connman_network_set_uint16(network, "WiFi.Channel", channel);
1410 connman_network_set_string(network, "WiFi.Security", security);
1412 if (result.ssid != NULL)
1413 connman_network_set_group(network, group);
1418 g_free(result.path);
1419 g_free(result.addr);
1420 g_free(result.name);
1421 g_free(result.ssid);
1423 dbus_message_unref(reply);
1425 get_properties(task);
1428 static void get_properties(struct supplicant_task *task)
1430 DBusMessage *message;
1433 path = g_slist_nth_data(task->scan_results, 0);
1437 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1438 SUPPLICANT_INTF ".BSSID",
1441 task->scan_results = g_slist_remove(task->scan_results, path);
1444 if (message == NULL)
1447 dbus_message_set_auto_start(message, FALSE);
1449 if (dbus_connection_send_with_reply(connection, message,
1450 &task->result_call, TIMEOUT) == FALSE) {
1451 connman_error("Failed to get network properties");
1452 dbus_message_unref(message);
1456 if (task->result_call == NULL) {
1457 connman_error("D-Bus connection not available");
1458 dbus_message_unref(message);
1462 dbus_pending_call_set_notify(task->result_call,
1463 properties_reply, task, NULL);
1465 dbus_message_unref(message);
1470 task->result_call = NULL;
1472 if (task->scanning == TRUE) {
1473 connman_device_set_scanning(task->device, FALSE);
1474 task->scanning = FALSE;
1478 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1480 struct supplicant_task *task = user_data;
1486 DBG("task %p", task);
1488 reply = dbus_pending_call_steal_reply(call);
1492 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1495 dbus_error_init(&error);
1497 if (dbus_message_get_args(reply, &error,
1498 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1499 &results, &num_results,
1500 DBUS_TYPE_INVALID) == FALSE) {
1501 if (dbus_error_is_set(&error) == TRUE) {
1502 connman_error("%s", error.message);
1503 dbus_error_free(&error);
1505 connman_error("Wrong arguments for scan result");
1509 if (num_results == 0)
1512 for (i = 0; i < num_results; i++) {
1513 char *path = g_strdup(results[i]);
1517 task->scan_results = g_slist_append(task->scan_results, path);
1520 g_strfreev(results);
1522 dbus_message_unref(reply);
1524 get_properties(task);
1529 dbus_message_unref(reply);
1532 task->result_call = NULL;
1534 if (task->scanning == TRUE) {
1535 connman_device_set_scanning(task->device, FALSE);
1536 task->scanning = FALSE;
1540 static void scan_results_available(struct supplicant_task *task)
1542 DBusMessage *message;
1544 DBG("task %p", task);
1546 if (task->result_call != NULL)
1549 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1550 SUPPLICANT_INTF ".Interface",
1552 if (message == NULL)
1555 dbus_message_set_auto_start(message, FALSE);
1557 if (dbus_connection_send_with_reply(connection, message,
1558 &task->result_call, TIMEOUT) == FALSE) {
1559 connman_error("Failed to request scan result");
1563 if (task->result_call == NULL) {
1564 connman_error("D-Bus connection not available");
1568 if (task->scanning == TRUE)
1569 connman_device_set_scanning(task->device, TRUE);
1571 dbus_pending_call_set_notify(task->result_call,
1572 scan_results_reply, task, NULL);
1575 dbus_message_unref(message);
1578 static enum supplicant_state string2state(const char *state)
1580 if (g_str_equal(state, "INACTIVE") == TRUE)
1581 return WPA_INACTIVE;
1582 else if (g_str_equal(state, "SCANNING") == TRUE)
1583 return WPA_SCANNING;
1584 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1585 return WPA_ASSOCIATING;
1586 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1587 return WPA_ASSOCIATED;
1588 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1589 return WPA_GROUP_HANDSHAKE;
1590 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1591 return WPA_4WAY_HANDSHAKE;
1592 else if (g_str_equal(state, "COMPLETED") == TRUE)
1593 return WPA_COMPLETED;
1594 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1595 return WPA_DISCONNECTED;
1600 static int task_connect(struct supplicant_task *task)
1602 const char *address, *security, *passphrase;
1604 unsigned int ssid_len;
1607 connman_inet_ifup(task->ifindex);
1609 address = connman_network_get_string(task->network, "Address");
1610 security = connman_network_get_string(task->network, "WiFi.Security");
1611 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1613 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1615 DBG("address %s security %s", address, security);
1617 if (security == NULL && passphrase == NULL)
1620 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1623 remove_network(task);
1629 set_network(task, ssid, ssid_len, address, security, passphrase);
1631 err = select_network(task);
1635 return -EINPROGRESS;
1638 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1641 dbus_bool_t scanning;
1643 dbus_error_init(&error);
1645 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1646 DBUS_TYPE_INVALID) == FALSE) {
1647 if (dbus_error_is_set(&error) == TRUE) {
1648 connman_error("%s", error.message);
1649 dbus_error_free(&error);
1651 connman_error("Wrong arguments for scanning");
1655 connman_info("%s scanning %s", task->ifname,
1656 scanning == TRUE ? "started" : "finished");
1659 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1662 const char *newstate, *oldstate;
1663 unsigned char bssid[ETH_ALEN];
1664 unsigned int bssid_len;
1665 enum supplicant_state state, prevstate;
1667 dbus_error_init(&error);
1669 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1670 DBUS_TYPE_STRING, &oldstate,
1671 DBUS_TYPE_INVALID) == FALSE) {
1672 if (dbus_error_is_set(&error) == TRUE) {
1673 connman_error("%s", error.message);
1674 dbus_error_free(&error);
1676 connman_error("Wrong arguments for state change");
1680 DBG("state %s ==> %s", oldstate, newstate);
1682 connman_info("%s %s%s", task->ifname, newstate,
1683 task->scanning == TRUE ? " (scanning)" : "");
1685 state = string2state(newstate);
1686 if (state == WPA_INVALID)
1689 if (task->scanning == TRUE && state != WPA_SCANNING) {
1690 connman_device_cleanup_scanning(task->device);
1691 task->scanning = FALSE;
1694 prevstate = task->state;
1695 task->state = state;
1697 if (task->network == NULL)
1700 switch (task->state) {
1702 switch (prevstate) {
1703 case WPA_ASSOCIATED:
1704 case WPA_GROUP_HANDSHAKE:
1710 /* reset scan trigger and schedule background scan */
1711 connman_device_schedule_scan(task->device);
1713 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1714 connman_network_set_address(task->network,
1718 connman_network_set_connected(task->network, TRUE);
1721 case WPA_ASSOCIATING:
1722 switch (prevstate) {
1726 connman_network_set_associating(task->network, TRUE);
1734 switch (prevstate) {
1736 case WPA_DISCONNECTED:
1743 case WPA_DISCONNECTED:
1745 connman_network_set_connected(task->network, FALSE);
1747 if (task->disconnecting == TRUE) {
1748 connman_network_unref(task->network);
1749 task->disconnecting = FALSE;
1751 if (task->pending_network != NULL) {
1752 task->network = task->pending_network;
1753 task->pending_network = NULL;
1756 task->network = NULL;
1761 connman_network_set_associating(task->network, FALSE);
1768 connman_error("%s invalid state change %s -> %s", task->ifname,
1769 oldstate, newstate);
1772 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1773 DBusMessage *msg, void *data)
1775 struct supplicant_task *task;
1776 const char *member, *path;
1778 if (dbus_message_has_interface(msg,
1779 SUPPLICANT_INTF ".Interface") == FALSE)
1780 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1782 member = dbus_message_get_member(msg);
1784 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1786 path = dbus_message_get_path(msg);
1788 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1790 task = find_task_by_path(path);
1792 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1794 DBG("task %p member %s", task, member);
1796 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1797 scan_results_available(task);
1798 else if (g_str_equal(member, "Scanning") == TRUE)
1799 scanning(task, msg);
1800 else if (g_str_equal(member, "StateChange") == TRUE)
1801 state_change(task, msg);
1803 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1806 int supplicant_start(struct connman_device *device)
1808 struct supplicant_task *task;
1811 DBG("device %p", device);
1813 task = g_try_new0(struct supplicant_task, 1);
1817 task->ifindex = connman_device_get_index(device);
1818 task->ifname = connman_inet_ifname(task->ifindex);
1820 if (task->ifname == NULL) {
1825 task->mac80211 = connman_inet_is_mac80211(task->ifindex);
1826 if (task->mac80211 == FALSE)
1827 connman_warn("Enabling quirks for unsupported driver");
1829 task->range = g_try_malloc0(sizeof(struct iw_range));
1830 if (task->range == NULL) {
1835 err = get_range(task);
1839 task->device = connman_device_ref(device);
1841 task->created = FALSE;
1842 task->scanning = FALSE;
1843 task->state = WPA_INVALID;
1844 task->disconnecting = FALSE;
1845 task->pending_network = NULL;
1847 task_list = g_slist_append(task_list, task);
1849 return create_interface(task);
1852 g_free(task->range);
1853 g_free(task->ifname);
1859 int supplicant_stop(struct connman_device *device)
1861 int index = connman_device_get_index(device);
1862 struct supplicant_task *task;
1864 DBG("device %p", device);
1866 task = find_task_by_index(index);
1870 g_free(task->range);
1872 task_list = g_slist_remove(task_list, task);
1874 if (task->scan_call != NULL) {
1875 dbus_pending_call_cancel(task->scan_call);
1876 task->scan_call = NULL;
1879 if (task->result_call != NULL) {
1880 dbus_pending_call_cancel(task->result_call);
1881 task->result_call = NULL;
1884 if (task->scanning == TRUE)
1885 connman_device_set_scanning(task->device, FALSE);
1887 remove_network(task);
1889 disconnect_network(task);
1891 return remove_interface(task);
1894 int supplicant_scan(struct connman_device *device)
1896 int index = connman_device_get_index(device);
1897 struct supplicant_task *task;
1900 DBG("device %p", device);
1902 task = find_task_by_index(index);
1906 switch (task->state) {
1909 case WPA_ASSOCIATING:
1910 case WPA_ASSOCIATED:
1911 case WPA_4WAY_HANDSHAKE:
1912 case WPA_GROUP_HANDSHAKE:
1918 task->scanning = TRUE;
1920 err = initiate_scan(task);
1922 if (err == -EINPROGRESS)
1925 task->scanning = FALSE;
1929 connman_device_set_scanning(task->device, TRUE);
1934 int supplicant_connect(struct connman_network *network)
1936 struct supplicant_task *task;
1939 DBG("network %p", network);
1941 index = connman_network_get_index(network);
1943 task = find_task_by_index(index);
1947 if (task->disconnecting == TRUE)
1948 task->pending_network = connman_network_ref(network);
1950 task->network = connman_network_ref(network);
1951 return task_connect(task);
1954 return -EINPROGRESS;
1957 int supplicant_disconnect(struct connman_network *network)
1959 struct supplicant_task *task;
1962 DBG("network %p", network);
1964 index = connman_network_get_index(network);
1966 task = find_task_by_index(index);
1970 if (task->disconnecting == TRUE)
1973 remove_network(task);
1975 disconnect_network(task);
1977 task->disconnecting = TRUE;
1982 static void supplicant_activate(DBusConnection *conn)
1984 DBusMessage *message;
1986 DBG("conn %p", conn);
1988 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1989 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1990 if (message == NULL)
1993 dbus_message_set_no_reply(message, TRUE);
1995 dbus_connection_send(conn, message, NULL);
1997 dbus_message_unref(message);
2000 static GSList *driver_list = NULL;
2002 static void supplicant_probe(DBusConnection *conn, void *user_data)
2006 DBG("conn %p", conn);
2008 for (list = driver_list; list; list = list->next) {
2009 struct supplicant_driver *driver = list->data;
2011 DBG("driver %p name %s", driver, driver->name);
2018 static void supplicant_remove(DBusConnection *conn, void *user_data)
2022 DBG("conn %p", conn);
2024 for (list = driver_list; list; list = list->next) {
2025 struct supplicant_driver *driver = list->data;
2027 DBG("driver %p name %s", driver, driver->name);
2034 static const char *supplicant_rule = "type=signal,"
2035 "interface=" SUPPLICANT_INTF ".Interface";
2038 static int supplicant_create(void)
2040 if (g_slist_length(driver_list) > 0)
2043 connection = connman_dbus_get_connection();
2044 if (connection == NULL)
2047 DBG("connection %p", connection);
2049 if (dbus_connection_add_filter(connection,
2050 supplicant_filter, NULL, NULL) == FALSE) {
2051 connection = connman_dbus_get_connection();
2055 dbus_bus_add_match(connection, supplicant_rule, NULL);
2056 dbus_connection_flush(connection);
2058 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2059 supplicant_probe, supplicant_remove, NULL, NULL);
2064 static void supplicant_destroy(void)
2066 if (g_slist_length(driver_list) > 0)
2069 DBG("connection %p", connection);
2072 g_dbus_remove_watch(connection, watch);
2074 dbus_bus_remove_match(connection, supplicant_rule, NULL);
2075 dbus_connection_flush(connection);
2077 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
2079 dbus_connection_unref(connection);
2083 int supplicant_register(struct supplicant_driver *driver)
2087 DBG("driver %p name %s", driver, driver->name);
2089 err = supplicant_create();
2093 driver_list = g_slist_append(driver_list, driver);
2095 supplicant_activate(connection);
2100 void supplicant_unregister(struct supplicant_driver *driver)
2102 DBG("driver %p name %s", driver, driver->name);
2104 supplicant_remove(connection, NULL);
2106 driver_list = g_slist_remove(driver_list, driver);
2108 supplicant_destroy();