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;
168 dbus_int32_t frequency;
169 dbus_int32_t quality;
172 dbus_int32_t maxrate;
175 struct supplicant_task {
178 struct connman_device *device;
179 struct connman_network *network;
180 struct connman_network *pending_network;
184 enum supplicant_state state;
186 GSList *scan_results;
187 struct iw_range *range;
188 gboolean disconnecting;
191 static GSList *task_list = NULL;
193 static DBusConnection *connection;
195 static void free_task(struct supplicant_task *task)
197 DBG("task %p", task);
199 g_free(task->ifname);
204 static struct supplicant_task *find_task_by_index(int index)
208 for (list = task_list; list; list = list->next) {
209 struct supplicant_task *task = list->data;
211 if (task->ifindex == index)
218 static struct supplicant_task *find_task_by_path(const char *path)
222 for (list = task_list; list; list = list->next) {
223 struct supplicant_task *task = list->data;
225 if (g_strcmp0(task->path, path) == 0)
232 static int get_range(struct supplicant_task *task)
237 fd = socket(PF_INET, SOCK_DGRAM, 0);
241 memset(&wrq, 0, sizeof(struct iwreq));
242 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
243 wrq.u.data.pointer = task->range;
244 wrq.u.data.length = sizeof(struct iw_range);
246 err = ioctl(fd, SIOCGIWRANGE, &wrq);
253 static int get_bssid(struct connman_device *device,
254 unsigned char *bssid, unsigned int *bssid_len)
261 ifindex = connman_device_get_index(device);
265 ifname = connman_inet_ifname(ifindex);
269 fd = socket(PF_INET, SOCK_DGRAM, 0);
275 memset(&wrq, 0, sizeof(wrq));
276 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
278 err = ioctl(fd, SIOCGIWAP, &wrq);
286 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
287 *bssid_len = ETH_ALEN;
292 static void add_interface_reply(DBusPendingCall *call, void *user_data)
294 struct supplicant_task *task = user_data;
299 DBG("task %p", task);
301 reply = dbus_pending_call_steal_reply(call);
305 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
308 dbus_error_init(&error);
310 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
311 DBUS_TYPE_INVALID) == FALSE) {
312 if (dbus_error_is_set(&error) == TRUE) {
313 connman_error("%s", error.message);
314 dbus_error_free(&error);
316 connman_error("Wrong arguments for add interface");
320 DBG("path %s", path);
322 task->path = g_strdup(path);
323 task->created = TRUE;
325 connman_device_set_powered(task->device, TRUE);
327 dbus_message_unref(reply);
332 task_list = g_slist_remove(task_list, task);
334 connman_device_unref(task->device);
339 static int add_interface(struct supplicant_task *task)
341 const char *driver = connman_option_get_string("wifi");
342 DBusMessage *message;
343 DBusMessageIter array, dict;
344 DBusPendingCall *call;
346 DBG("task %p", task);
348 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
349 SUPPLICANT_INTF, "addInterface");
353 dbus_message_set_auto_start(message, FALSE);
355 dbus_message_iter_init_append(message, &array);
357 dbus_message_iter_append_basic(&array,
358 DBUS_TYPE_STRING, &task->ifname);
360 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
361 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
362 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
363 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
365 connman_dbus_dict_append_variant(&dict, "driver",
366 DBUS_TYPE_STRING, &driver);
368 dbus_message_iter_close_container(&array, &dict);
370 if (dbus_connection_send_with_reply(connection, message,
371 &call, TIMEOUT) == FALSE) {
372 connman_error("Failed to add interface");
373 dbus_message_unref(message);
378 connman_error("D-Bus connection not available");
379 dbus_message_unref(message);
383 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
385 dbus_message_unref(message);
390 static void get_interface_reply(DBusPendingCall *call, void *user_data)
392 struct supplicant_task *task = user_data;
397 DBG("task %p", task);
399 reply = dbus_pending_call_steal_reply(call);
403 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
408 dbus_error_init(&error);
410 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
411 DBUS_TYPE_INVALID) == FALSE) {
412 if (dbus_error_is_set(&error) == TRUE) {
413 connman_error("%s", error.message);
414 dbus_error_free(&error);
416 connman_error("Wrong arguments for get interface");
420 DBG("path %s", path);
422 task->path = g_strdup(path);
423 task->created = FALSE;
425 connman_device_set_powered(task->device, TRUE);
428 dbus_message_unref(reply);
431 static int create_interface(struct supplicant_task *task)
433 DBusMessage *message;
434 DBusPendingCall *call;
436 DBG("task %p", task);
438 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
439 SUPPLICANT_INTF, "getInterface");
443 dbus_message_set_auto_start(message, FALSE);
445 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
448 if (dbus_connection_send_with_reply(connection, message,
449 &call, TIMEOUT) == FALSE) {
450 connman_error("Failed to get interface");
451 dbus_message_unref(message);
456 connman_error("D-Bus connection not available");
457 dbus_message_unref(message);
461 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
463 dbus_message_unref(message);
468 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
470 struct supplicant_task *task = user_data;
473 DBG("task %p", task);
475 reply = dbus_pending_call_steal_reply(call);
477 connman_device_set_powered(task->device, FALSE);
479 connman_device_unref(task->device);
481 connman_inet_ifdown(task->ifindex);
485 dbus_message_unref(reply);
488 static int remove_interface(struct supplicant_task *task)
490 DBusMessage *message;
491 DBusPendingCall *call;
493 DBG("task %p", task);
496 if (task->created == FALSE) {
497 connman_device_set_powered(task->device, FALSE);
502 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
503 SUPPLICANT_INTF, "removeInterface");
507 dbus_message_set_auto_start(message, FALSE);
509 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
512 if (dbus_connection_send_with_reply(connection, message,
513 &call, TIMEOUT) == FALSE) {
514 connman_error("Failed to remove interface");
515 dbus_message_unref(message);
520 connman_error("D-Bus connection not available");
521 dbus_message_unref(message);
525 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
527 dbus_message_unref(message);
533 static int set_ap_scan(struct supplicant_task *task)
535 DBusMessage *message, *reply;
539 DBG("task %p", task);
541 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
542 SUPPLICANT_INTF ".Interface", "setAPScan");
546 dbus_message_set_auto_start(message, FALSE);
548 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
551 dbus_error_init(&error);
553 reply = dbus_connection_send_with_reply_and_block(connection,
554 message, -1, &error);
556 if (dbus_error_is_set(&error) == TRUE) {
557 connman_error("%s", error.message);
558 dbus_error_free(&error);
560 connman_error("Failed to set AP scan");
561 dbus_message_unref(message);
565 dbus_message_unref(message);
567 dbus_message_unref(reply);
573 static int add_network(struct supplicant_task *task)
575 DBusMessage *message, *reply;
579 DBG("task %p", task);
581 if (task->netpath != NULL)
584 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
585 SUPPLICANT_INTF ".Interface", "addNetwork");
589 dbus_message_set_auto_start(message, FALSE);
591 dbus_error_init(&error);
593 reply = dbus_connection_send_with_reply_and_block(connection,
594 message, -1, &error);
596 if (dbus_error_is_set(&error) == TRUE) {
597 connman_error("%s", error.message);
598 dbus_error_free(&error);
600 connman_error("Failed to add network");
601 dbus_message_unref(message);
605 dbus_message_unref(message);
607 dbus_error_init(&error);
609 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
610 DBUS_TYPE_INVALID) == FALSE) {
611 if (dbus_error_is_set(&error) == TRUE) {
612 connman_error("%s", error.message);
613 dbus_error_free(&error);
615 connman_error("Wrong arguments for network");
616 dbus_message_unref(reply);
620 DBG("path %s", path);
622 task->netpath = g_strdup(path);
624 dbus_message_unref(reply);
629 static int remove_network(struct supplicant_task *task)
631 DBusMessage *message, *reply;
634 DBG("task %p", task);
636 if (task->netpath == NULL)
639 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
640 SUPPLICANT_INTF ".Interface", "removeNetwork");
644 dbus_message_set_auto_start(message, FALSE);
646 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
649 dbus_error_init(&error);
651 reply = dbus_connection_send_with_reply_and_block(connection,
652 message, -1, &error);
654 if (dbus_error_is_set(&error) == TRUE) {
655 connman_error("%s", error.message);
656 dbus_error_free(&error);
658 connman_error("Failed to remove network");
659 dbus_message_unref(message);
663 dbus_message_unref(message);
665 dbus_message_unref(reply);
667 g_free(task->netpath);
668 task->netpath = NULL;
673 static int select_network(struct supplicant_task *task)
675 DBusMessage *message, *reply;
678 DBG("task %p", task);
680 if (task->netpath == NULL)
683 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
684 SUPPLICANT_INTF ".Interface", "selectNetwork");
688 dbus_message_set_auto_start(message, FALSE);
690 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
693 dbus_error_init(&error);
695 reply = dbus_connection_send_with_reply_and_block(connection,
696 message, -1, &error);
698 if (dbus_error_is_set(&error) == TRUE) {
699 connman_error("%s", error.message);
700 dbus_error_free(&error);
702 connman_error("Failed to select network");
703 dbus_message_unref(message);
707 dbus_message_unref(message);
709 dbus_message_unref(reply);
714 static int enable_network(struct supplicant_task *task)
716 DBusMessage *message, *reply;
719 DBG("task %p", task);
721 if (task->netpath == NULL)
724 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
725 SUPPLICANT_INTF ".Network", "enable");
729 dbus_message_set_auto_start(message, FALSE);
731 dbus_error_init(&error);
733 reply = dbus_connection_send_with_reply_and_block(connection,
734 message, -1, &error);
736 if (dbus_error_is_set(&error) == TRUE) {
737 connman_error("%s", error.message);
738 dbus_error_free(&error);
740 connman_error("Failed to enable network");
741 dbus_message_unref(message);
745 dbus_message_unref(message);
747 dbus_message_unref(reply);
752 static int disable_network(struct supplicant_task *task)
754 DBusMessage *message, *reply;
757 DBG("task %p", task);
759 if (task->netpath == NULL)
762 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
763 SUPPLICANT_INTF ".Network", "disable");
767 dbus_message_set_auto_start(message, FALSE);
769 dbus_error_init(&error);
771 reply = dbus_connection_send_with_reply_and_block(connection,
772 message, -1, &error);
774 if (dbus_error_is_set(&error) == TRUE) {
775 connman_error("%s", error.message);
776 dbus_error_free(&error);
778 connman_error("Failed to disable network");
779 dbus_message_unref(message);
783 dbus_message_unref(message);
785 dbus_message_unref(reply);
790 static int set_network(struct supplicant_task *task,
791 const unsigned char *network, int len,
792 const char *address, const char *security,
793 const char *passphrase)
795 DBusMessage *message, *reply;
796 DBusMessageIter array, dict;
798 dbus_uint32_t scan_ssid = 1;
800 DBG("task %p", task);
802 if (task->netpath == NULL)
805 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
806 SUPPLICANT_INTF ".Network", "set");
810 dbus_message_set_auto_start(message, FALSE);
812 dbus_message_iter_init_append(message, &array);
814 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
815 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
816 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
817 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
819 connman_dbus_dict_append_variant(&dict, "scan_ssid",
820 DBUS_TYPE_UINT32, &scan_ssid);
823 connman_dbus_dict_append_variant(&dict, "bssid",
824 DBUS_TYPE_STRING, &address);
826 connman_dbus_dict_append_array(&dict, "ssid",
827 DBUS_TYPE_BYTE, &network, len);
829 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
830 g_ascii_strcasecmp(security, "rsn") == 0) {
831 const char *key_mgmt = "WPA-PSK";
832 connman_dbus_dict_append_variant(&dict, "key_mgmt",
833 DBUS_TYPE_STRING, &key_mgmt);
835 if (passphrase && strlen(passphrase) > 0)
836 connman_dbus_dict_append_variant(&dict, "psk",
837 DBUS_TYPE_STRING, &passphrase);
838 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
839 const char *key_mgmt = "NONE";
840 const char *auth_alg = "OPEN SHARED";
841 const char *key_index = "0";
843 connman_dbus_dict_append_variant(&dict, "auth_alg",
844 DBUS_TYPE_STRING, &auth_alg);
846 connman_dbus_dict_append_variant(&dict, "key_mgmt",
847 DBUS_TYPE_STRING, &key_mgmt);
850 int size = strlen(passphrase);
851 if (size == 10 || size == 26) {
852 unsigned char *key = malloc(13);
855 memset(tmp, 0, sizeof(tmp));
858 for (i = 0; i < size / 2; i++) {
859 memcpy(tmp, passphrase + (i * 2), 2);
860 key[i] = (unsigned char) strtol(tmp,
863 connman_dbus_dict_append_array(&dict,
864 "wep_key0", DBUS_TYPE_BYTE,
868 connman_dbus_dict_append_variant(&dict,
869 "wep_key0", DBUS_TYPE_STRING,
872 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
873 DBUS_TYPE_STRING, &key_index);
876 const char *key_mgmt = "NONE";
877 connman_dbus_dict_append_variant(&dict, "key_mgmt",
878 DBUS_TYPE_STRING, &key_mgmt);
881 dbus_message_iter_close_container(&array, &dict);
883 dbus_error_init(&error);
885 reply = dbus_connection_send_with_reply_and_block(connection,
886 message, -1, &error);
888 if (dbus_error_is_set(&error) == TRUE) {
889 connman_error("%s", error.message);
890 dbus_error_free(&error);
892 connman_error("Failed to set network options");
893 dbus_message_unref(message);
897 dbus_message_unref(message);
899 dbus_message_unref(reply);
904 static int initiate_scan(struct supplicant_task *task)
906 DBusMessage *message;
907 DBusPendingCall *call;
909 DBG("task %p", task);
911 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
912 SUPPLICANT_INTF ".Interface", "scan");
916 dbus_message_set_auto_start(message, FALSE);
918 if (dbus_connection_send_with_reply(connection, message,
919 &call, TIMEOUT) == FALSE) {
920 connman_error("Failed to initiate scan");
921 dbus_message_unref(message);
925 dbus_message_unref(message);
934 { "<hidden>", "hidden" },
935 { "default", "linksys" },
943 { "comcomcom", "3com" },
945 { "Symbol", "symbol" },
946 { "Motorola", "motorola" },
947 { "Wireless" , "wireless" },
952 static char *build_group(const char *addr, const char *name,
953 const unsigned char *ssid, unsigned int ssid_len,
954 const char *mode, const char *security)
962 str = g_string_sized_new((ssid_len * 2) + 24);
967 g_string_append_printf(str, "hidden_%s", addr);
971 for (i = 0; special_ssid[i].name; i++) {
972 if (g_strcmp0(special_ssid[i].name, name) == 0) {
973 if (special_ssid[i].value == NULL)
974 g_string_append_printf(str, "%s_%s",
977 g_string_append_printf(str, "%s_%s",
978 special_ssid[i].value, addr);
983 if (ssid_len > 0 && ssid[0] != '\0') {
984 for (i = 0; i < ssid_len; i++)
985 g_string_append_printf(str, "%02x", ssid[i]);
987 g_string_append_printf(str, "hidden_%s", addr);
990 g_string_append_printf(str, "_%s_%s", mode, security);
992 return g_string_free(str, FALSE);
995 static void extract_addr(DBusMessageIter *value,
996 struct supplicant_result *result)
998 DBusMessageIter array;
999 struct ether_addr eth;
1000 unsigned char *addr;
1003 dbus_message_iter_recurse(value, &array);
1004 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1009 result->addr = g_try_malloc(addr_len);
1010 if (result->addr == NULL)
1013 memcpy(result->addr, addr, addr_len);
1014 result->addr_len = addr_len;
1016 result->path = g_try_malloc0(13);
1017 if (result->path == NULL)
1020 memcpy(ð, addr, sizeof(eth));
1021 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1022 eth.ether_addr_octet[0],
1023 eth.ether_addr_octet[1],
1024 eth.ether_addr_octet[2],
1025 eth.ether_addr_octet[3],
1026 eth.ether_addr_octet[4],
1027 eth.ether_addr_octet[5]);
1030 static void extract_ssid(DBusMessageIter *value,
1031 struct supplicant_result *result)
1033 DBusMessageIter array;
1034 unsigned char *ssid;
1037 dbus_message_iter_recurse(value, &array);
1038 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1043 if (ssid[0] == '\0')
1046 result->ssid = g_try_malloc(ssid_len);
1047 if (result->ssid == NULL)
1050 memcpy(result->ssid, ssid, ssid_len);
1051 result->ssid_len = ssid_len;
1053 result->name = g_try_malloc0(ssid_len + 1);
1054 if (result->name == NULL)
1057 for (i = 0; i < ssid_len; i++) {
1058 if (g_ascii_isprint(ssid[i]))
1059 result->name[i] = ssid[i];
1061 result->name[i] = ' ';
1065 static void extract_wpaie(DBusMessageIter *value,
1066 struct supplicant_result *result)
1068 DBusMessageIter array;
1072 dbus_message_iter_recurse(value, &array);
1073 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1076 result->has_wpa = TRUE;
1079 static void extract_rsnie(DBusMessageIter *value,
1080 struct supplicant_result *result)
1082 DBusMessageIter array;
1086 dbus_message_iter_recurse(value, &array);
1087 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1090 result->has_rsn = TRUE;
1093 static void extract_wpsie(DBusMessageIter *value,
1094 struct supplicant_result *result)
1096 DBusMessageIter array;
1100 dbus_message_iter_recurse(value, &array);
1101 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1104 result->has_wps = TRUE;
1107 static void extract_capabilites(DBusMessageIter *value,
1108 struct supplicant_result *result)
1110 dbus_message_iter_get_basic(value, &result->capabilities);
1112 if (result->capabilities & IEEE80211_CAP_ESS)
1113 result->adhoc = FALSE;
1114 else if (result->capabilities & IEEE80211_CAP_IBSS)
1115 result->adhoc = TRUE;
1117 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1118 result->has_wep = TRUE;
1121 static unsigned char calculate_strength(struct supplicant_task *task,
1122 struct supplicant_result *result)
1124 if (task->range->max_qual.qual == 0) {
1125 unsigned char strength;
1127 if (result->level > 0)
1128 strength = 100 - result->level;
1130 strength = 120 + result->level;
1138 return (result->quality * 100) / task->range->max_qual.qual;
1141 static unsigned short calculate_channel(struct supplicant_result *result)
1143 if (result->frequency < 0)
1146 return (result->frequency - 2407) / 5;
1149 static void get_properties(struct supplicant_task *task);
1151 static void properties_reply(DBusPendingCall *call, void *user_data)
1153 struct supplicant_task *task = user_data;
1154 struct supplicant_result result;
1155 struct connman_network *network;
1157 DBusMessageIter array, dict;
1158 unsigned char strength;
1159 unsigned short channel, frequency;
1160 const char *mode, *security;
1162 unsigned int ssid_len;
1164 DBG("task %p", task);
1166 reply = dbus_pending_call_steal_reply(call);
1167 if (reply == NULL) {
1168 get_properties(task);
1172 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1173 dbus_message_unref(reply);
1174 get_properties(task);
1178 memset(&result, 0, sizeof(result));
1179 result.frequency = -1;
1180 result.quality = -1;
1184 dbus_message_iter_init(reply, &array);
1186 dbus_message_iter_recurse(&array, &dict);
1188 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1189 DBusMessageIter entry, value;
1192 dbus_message_iter_recurse(&dict, &entry);
1193 dbus_message_iter_get_basic(&entry, &key);
1195 dbus_message_iter_next(&entry);
1197 dbus_message_iter_recurse(&entry, &value);
1199 //type = dbus_message_iter_get_arg_type(&value);
1200 //dbus_message_iter_get_basic(&value, &val);
1208 * frequency : i (105)
1209 * capabilities : q (113)
1216 if (g_str_equal(key, "bssid") == TRUE)
1217 extract_addr(&value, &result);
1218 else if (g_str_equal(key, "ssid") == TRUE)
1219 extract_ssid(&value, &result);
1220 else if (g_str_equal(key, "wpaie") == TRUE)
1221 extract_wpaie(&value, &result);
1222 else if (g_str_equal(key, "rsnie") == TRUE)
1223 extract_rsnie(&value, &result);
1224 else if (g_str_equal(key, "wpsie") == TRUE)
1225 extract_wpsie(&value, &result);
1226 else if (g_str_equal(key, "capabilities") == TRUE)
1227 extract_capabilites(&value, &result);
1228 else if (g_str_equal(key, "frequency") == TRUE)
1229 dbus_message_iter_get_basic(&value, &result.frequency);
1230 else if (g_str_equal(key, "quality") == TRUE)
1231 dbus_message_iter_get_basic(&value, &result.quality);
1232 else if (g_str_equal(key, "noise") == TRUE)
1233 dbus_message_iter_get_basic(&value, &result.noise);
1234 else if (g_str_equal(key, "level") == TRUE)
1235 dbus_message_iter_get_basic(&value, &result.level);
1236 else if (g_str_equal(key, "maxrate") == TRUE)
1237 dbus_message_iter_get_basic(&value, &result.maxrate);
1239 dbus_message_iter_next(&dict);
1242 if (result.path == NULL)
1245 if (result.path[0] == '\0')
1248 if (result.frequency > 0 && result.frequency < 14)
1249 result.frequency = 2407 + (5 * result.frequency);
1250 else if (result.frequency == 14)
1251 result.frequency = 2484;
1253 strength = calculate_strength(task, &result);
1254 channel = calculate_channel(&result);
1256 frequency = (result.frequency < 0) ? 0 : result.frequency;
1258 if (result.has_rsn == TRUE)
1260 else if (result.has_wpa == TRUE)
1262 else if (result.has_wep == TRUE)
1267 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1269 group = build_group(result.path, result.name,
1270 result.ssid, result.ssid_len,
1273 network = connman_device_get_network(task->device, result.path);
1274 if (network == NULL) {
1277 network = connman_network_create(result.path,
1278 CONNMAN_NETWORK_TYPE_WIFI);
1279 if (network == NULL)
1282 index = connman_device_get_index(task->device);
1283 connman_network_set_index(network, index);
1285 connman_network_set_protocol(network,
1286 CONNMAN_NETWORK_PROTOCOL_IP);
1288 connman_network_set_address(network, result.addr,
1291 if (connman_device_add_network(task->device, network) < 0) {
1292 connman_network_unref(network);
1297 if (result.name != NULL && result.name[0] != '\0')
1298 connman_network_set_name(network, result.name);
1300 if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1301 connman_network_set_blob(network, "WiFi.SSID",
1302 result.ssid, result.ssid_len);
1305 connman_network_set_string(network, "WiFi.Mode", mode);
1307 DBG("%s (%s %s) strength %d (%s)",
1308 result.name, mode, security, strength,
1309 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1311 connman_network_set_available(network, TRUE);
1312 connman_network_set_strength(network, strength);
1314 connman_network_set_uint16(network, "Frequency", frequency);
1315 connman_network_set_uint16(network, "WiFi.Channel", channel);
1316 connman_network_set_string(network, "WiFi.Security", security);
1318 if (result.ssid != NULL)
1319 connman_network_set_group(network, group);
1324 g_free(result.path);
1325 g_free(result.addr);
1326 g_free(result.name);
1327 g_free(result.ssid);
1329 dbus_message_unref(reply);
1331 get_properties(task);
1334 static void get_properties(struct supplicant_task *task)
1336 DBusMessage *message;
1337 DBusPendingCall *call;
1340 path = g_slist_nth_data(task->scan_results, 0);
1344 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1345 SUPPLICANT_INTF ".BSSID",
1348 task->scan_results = g_slist_remove(task->scan_results, path);
1351 if (message == NULL)
1354 dbus_message_set_auto_start(message, FALSE);
1356 if (dbus_connection_send_with_reply(connection, message,
1357 &call, TIMEOUT) == FALSE) {
1358 connman_error("Failed to get network properties");
1359 dbus_message_unref(message);
1364 connman_error("D-Bus connection not available");
1365 dbus_message_unref(message);
1369 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1371 dbus_message_unref(message);
1376 if (task->noscan == FALSE)
1377 connman_device_set_scanning(task->device, FALSE);
1380 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1382 struct supplicant_task *task = user_data;
1388 DBG("task %p", task);
1390 reply = dbus_pending_call_steal_reply(call);
1394 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1397 dbus_error_init(&error);
1399 if (dbus_message_get_args(reply, &error,
1400 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1401 &results, &num_results,
1402 DBUS_TYPE_INVALID) == FALSE) {
1403 if (dbus_error_is_set(&error) == TRUE) {
1404 connman_error("%s", error.message);
1405 dbus_error_free(&error);
1407 connman_error("Wrong arguments for scan result");
1411 if (num_results == 0)
1414 for (i = 0; i < num_results; i++) {
1415 char *path = g_strdup(results[i]);
1419 task->scan_results = g_slist_append(task->scan_results, path);
1422 g_strfreev(results);
1424 dbus_message_unref(reply);
1426 get_properties(task);
1431 dbus_message_unref(reply);
1434 if (task->noscan == FALSE)
1435 connman_device_set_scanning(task->device, FALSE);
1438 static void scan_results_available(struct supplicant_task *task)
1440 DBusMessage *message;
1441 DBusPendingCall *call;
1443 DBG("task %p", task);
1445 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1446 SUPPLICANT_INTF ".Interface",
1448 if (message == NULL)
1451 dbus_message_set_auto_start(message, FALSE);
1453 if (dbus_connection_send_with_reply(connection, message,
1454 &call, TIMEOUT) == FALSE) {
1455 connman_error("Failed to request scan result");
1459 if (task->noscan == FALSE)
1460 connman_device_set_scanning(task->device, TRUE);
1463 connman_error("D-Bus connection not available");
1467 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1470 dbus_message_unref(message);
1473 static enum supplicant_state string2state(const char *state)
1475 if (g_str_equal(state, "INACTIVE") == TRUE)
1476 return WPA_INACTIVE;
1477 else if (g_str_equal(state, "SCANNING") == TRUE)
1478 return WPA_SCANNING;
1479 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1480 return WPA_ASSOCIATING;
1481 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1482 return WPA_ASSOCIATED;
1483 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1484 return WPA_GROUP_HANDSHAKE;
1485 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1486 return WPA_4WAY_HANDSHAKE;
1487 else if (g_str_equal(state, "COMPLETED") == TRUE)
1488 return WPA_COMPLETED;
1489 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1490 return WPA_DISCONNECTED;
1495 static int task_connect(struct supplicant_task *task)
1497 const char *address, *security, *passphrase;
1499 unsigned int ssid_len;
1501 address = connman_network_get_string(task->network, "Address");
1502 security = connman_network_get_string(task->network, "WiFi.Security");
1503 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1505 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1507 DBG("address %s security %s passphrase %s",
1508 address, security, passphrase);
1510 if (security == NULL && passphrase == NULL)
1513 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1518 select_network(task);
1519 disable_network(task);
1521 set_network(task, ssid, ssid_len, address, security, passphrase);
1523 enable_network(task);
1525 return -EINPROGRESS;
1528 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1531 const char *newstate, *oldstate;
1532 unsigned char bssid[ETH_ALEN];
1533 unsigned int bssid_len;
1534 enum supplicant_state state;
1536 dbus_error_init(&error);
1538 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1539 DBUS_TYPE_STRING, &oldstate,
1540 DBUS_TYPE_INVALID) == FALSE) {
1541 if (dbus_error_is_set(&error) == TRUE) {
1542 connman_error("%s", error.message);
1543 dbus_error_free(&error);
1545 connman_error("Wrong arguments for state change");
1549 DBG("state %s ==> %s", oldstate, newstate);
1551 state = string2state(newstate);
1552 if (state == WPA_INVALID)
1555 task->state = state;
1557 switch (task->state) {
1559 task->noscan = TRUE;
1560 connman_device_set_scanning(task->device, TRUE);
1562 case WPA_ASSOCIATING:
1563 case WPA_ASSOCIATED:
1564 case WPA_4WAY_HANDSHAKE:
1565 case WPA_GROUP_HANDSHAKE:
1566 task->noscan = TRUE;
1569 case WPA_DISCONNECTED:
1570 task->noscan = FALSE;
1573 task->noscan = FALSE;
1574 connman_device_set_scanning(task->device, FALSE);
1580 if (task->network == NULL)
1583 switch (task->state) {
1585 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1586 connman_network_set_address(task->network,
1590 connman_network_set_connected(task->network, TRUE);
1591 connman_device_set_scanning(task->device, FALSE);
1594 case WPA_DISCONNECTED:
1595 disable_network(task);
1597 if (task->disconnecting == TRUE) {
1598 connman_network_set_connected(task->network, FALSE);
1599 connman_network_unref(task->network);
1600 task->disconnecting = FALSE;
1602 if (task->pending_network != NULL) {
1603 task->network = task->pending_network;
1604 task->pending_network = NULL;
1609 connman_network_set_connected(task->network, FALSE);
1610 connman_device_set_scanning(task->device, FALSE);
1614 case WPA_ASSOCIATING:
1615 connman_network_set_associating(task->network, TRUE);
1619 remove_network(task);
1621 if (task->disconnecting == TRUE) {
1622 connman_network_set_connected(task->network, FALSE);
1623 connman_network_unref(task->network);
1624 task->disconnecting = FALSE;
1626 if (task->pending_network != NULL) {
1627 task->network = task->pending_network;
1628 task->pending_network = NULL;
1635 connman_network_set_associating(task->network, FALSE);
1640 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1641 DBusMessage *msg, void *data)
1643 struct supplicant_task *task;
1644 const char *member, *path;
1646 if (dbus_message_has_interface(msg,
1647 SUPPLICANT_INTF ".Interface") == FALSE)
1648 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1650 member = dbus_message_get_member(msg);
1652 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1654 path = dbus_message_get_path(msg);
1656 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1658 task = find_task_by_path(path);
1660 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1662 DBG("task %p member %s", task, member);
1664 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1665 scan_results_available(task);
1666 else if (g_str_equal(member, "StateChange") == TRUE)
1667 state_change(task, msg);
1669 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1672 int supplicant_start(struct connman_device *device)
1674 struct supplicant_task *task;
1677 DBG("device %p", device);
1679 task = g_try_new0(struct supplicant_task, 1);
1683 task->ifindex = connman_device_get_index(device);
1684 task->ifname = connman_inet_ifname(task->ifindex);
1686 if (task->ifname == NULL) {
1691 task->range = g_try_malloc0(sizeof(struct iw_range));
1692 if (task->range == NULL) {
1697 err = get_range(task);
1701 task->device = connman_device_ref(device);
1703 task->created = FALSE;
1704 task->noscan = FALSE;
1705 task->state = WPA_INVALID;
1706 task->disconnecting = FALSE;
1707 task->pending_network = NULL;
1709 task_list = g_slist_append(task_list, task);
1711 return create_interface(task);
1714 g_free(task->range);
1715 g_free(task->ifname);
1721 int supplicant_stop(struct connman_device *device)
1723 int index = connman_device_get_index(device);
1724 struct supplicant_task *task;
1726 DBG("device %p", device);
1728 task = find_task_by_index(index);
1732 g_free(task->range);
1734 task_list = g_slist_remove(task_list, task);
1736 disable_network(task);
1738 remove_network(task);
1740 return remove_interface(task);
1743 int supplicant_scan(struct connman_device *device)
1745 int index = connman_device_get_index(device);
1746 struct supplicant_task *task;
1749 DBG("device %p", device);
1751 task = find_task_by_index(index);
1755 switch (task->state) {
1758 case WPA_ASSOCIATING:
1759 case WPA_ASSOCIATED:
1760 case WPA_4WAY_HANDSHAKE:
1761 case WPA_GROUP_HANDSHAKE:
1767 err = initiate_scan(task);
1772 int supplicant_connect(struct connman_network *network)
1774 struct supplicant_task *task;
1777 DBG("network %p", network);
1779 index = connman_network_get_index(network);
1781 task = find_task_by_index(index);
1785 if (task->disconnecting == TRUE)
1786 task->pending_network = connman_network_ref(network);
1788 task->network = connman_network_ref(network);
1789 return task_connect(task);
1792 return -EINPROGRESS;
1795 int supplicant_disconnect(struct connman_network *network)
1797 struct supplicant_task *task;
1800 DBG("network %p", network);
1802 index = connman_network_get_index(network);
1804 task = find_task_by_index(index);
1808 if (task->disconnecting == TRUE)
1811 disable_network(task);
1813 remove_network(task);
1815 task->disconnecting = TRUE;
1820 static void supplicant_activate(DBusConnection *conn)
1822 DBusMessage *message;
1824 DBG("conn %p", conn);
1826 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1827 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1828 if (message == NULL)
1831 dbus_message_set_no_reply(message, TRUE);
1833 dbus_connection_send(conn, message, NULL);
1835 dbus_message_unref(message);
1838 static GSList *driver_list = NULL;
1840 static void supplicant_probe(DBusConnection *conn, void *user_data)
1844 DBG("conn %p", conn);
1846 for (list = driver_list; list; list = list->next) {
1847 struct supplicant_driver *driver = list->data;
1849 DBG("driver %p name %s", driver, driver->name);
1856 static void supplicant_remove(DBusConnection *conn, void *user_data)
1860 DBG("conn %p", conn);
1862 for (list = driver_list; list; list = list->next) {
1863 struct supplicant_driver *driver = list->data;
1865 DBG("driver %p name %s", driver, driver->name);
1872 static const char *supplicant_rule = "type=signal,"
1873 "interface=" SUPPLICANT_INTF ".Interface";
1876 static int supplicant_create(void)
1878 if (g_slist_length(driver_list) > 0)
1881 connection = connman_dbus_get_connection();
1882 if (connection == NULL)
1885 DBG("connection %p", connection);
1887 if (dbus_connection_add_filter(connection,
1888 supplicant_filter, NULL, NULL) == FALSE) {
1889 connection = connman_dbus_get_connection();
1893 dbus_bus_add_match(connection, supplicant_rule, NULL);
1894 dbus_connection_flush(connection);
1896 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1897 supplicant_probe, supplicant_remove, NULL, NULL);
1902 static void supplicant_destroy(void)
1904 if (g_slist_length(driver_list) > 0)
1907 DBG("connection %p", connection);
1910 g_dbus_remove_watch(connection, watch);
1912 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1913 dbus_connection_flush(connection);
1915 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1917 dbus_connection_unref(connection);
1921 int supplicant_register(struct supplicant_driver *driver)
1925 DBG("driver %p name %s", driver, driver->name);
1927 err = supplicant_create();
1931 driver_list = g_slist_append(driver_list, driver);
1933 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1934 supplicant_probe(connection, NULL);
1936 supplicant_activate(connection);
1941 void supplicant_unregister(struct supplicant_driver *driver)
1943 DBG("driver %p name %s", driver, driver->name);
1945 supplicant_remove(connection, NULL);
1947 driver_list = g_slist_remove(driver_list, driver);
1949 supplicant_destroy();