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 char *get_bssid(struct connman_device *device)
256 unsigned char ioctl_bssid[ETH_ALEN];
262 ifindex = connman_device_get_index(device);
266 ifname = connman_inet_ifname(ifindex);
270 fd = socket(PF_INET, SOCK_DGRAM, 0);
276 memset(&wrq, 0, sizeof(wrq));
277 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
279 err = ioctl(fd, SIOCGIWAP, &wrq);
287 memcpy(ioctl_bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
289 bssid = g_try_malloc0(13);
293 snprintf(bssid, 13, "%02x%02x%02x%02x%02x%02x",
294 ioctl_bssid[0], ioctl_bssid[1],
295 ioctl_bssid[2], ioctl_bssid[3],
296 ioctl_bssid[4], ioctl_bssid[5]);
301 static void add_interface_reply(DBusPendingCall *call, void *user_data)
303 struct supplicant_task *task = user_data;
308 DBG("task %p", task);
310 reply = dbus_pending_call_steal_reply(call);
314 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
317 dbus_error_init(&error);
319 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
320 DBUS_TYPE_INVALID) == FALSE) {
321 if (dbus_error_is_set(&error) == TRUE) {
322 connman_error("%s", error.message);
323 dbus_error_free(&error);
325 connman_error("Wrong arguments for add interface");
329 DBG("path %s", path);
331 task->path = g_strdup(path);
332 task->created = TRUE;
334 connman_device_set_powered(task->device, TRUE);
336 dbus_message_unref(reply);
341 task_list = g_slist_remove(task_list, task);
343 connman_device_unref(task->device);
348 static int add_interface(struct supplicant_task *task)
350 const char *driver = connman_option_get_string("wifi");
351 DBusMessage *message;
352 DBusMessageIter array, dict;
353 DBusPendingCall *call;
355 DBG("task %p", task);
357 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
358 SUPPLICANT_INTF, "addInterface");
362 dbus_message_iter_init_append(message, &array);
364 dbus_message_iter_append_basic(&array,
365 DBUS_TYPE_STRING, &task->ifname);
367 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
368 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
369 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
370 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
372 connman_dbus_dict_append_variant(&dict, "driver",
373 DBUS_TYPE_STRING, &driver);
375 dbus_message_iter_close_container(&array, &dict);
377 if (dbus_connection_send_with_reply(connection, message,
378 &call, TIMEOUT) == FALSE) {
379 connman_error("Failed to add interface");
380 dbus_message_unref(message);
385 connman_error("D-Bus connection not available");
386 dbus_message_unref(message);
390 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
392 dbus_message_unref(message);
397 static void get_interface_reply(DBusPendingCall *call, void *user_data)
399 struct supplicant_task *task = user_data;
404 DBG("task %p", task);
406 reply = dbus_pending_call_steal_reply(call);
410 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
415 dbus_error_init(&error);
417 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
418 DBUS_TYPE_INVALID) == FALSE) {
419 if (dbus_error_is_set(&error) == TRUE) {
420 connman_error("%s", error.message);
421 dbus_error_free(&error);
423 connman_error("Wrong arguments for get interface");
427 DBG("path %s", path);
429 task->path = g_strdup(path);
430 task->created = FALSE;
432 connman_device_set_powered(task->device, TRUE);
435 dbus_message_unref(reply);
438 static int create_interface(struct supplicant_task *task)
440 DBusMessage *message;
441 DBusPendingCall *call;
443 DBG("task %p", task);
445 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
446 SUPPLICANT_INTF, "getInterface");
450 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
453 if (dbus_connection_send_with_reply(connection, message,
454 &call, TIMEOUT) == FALSE) {
455 connman_error("Failed to get interface");
456 dbus_message_unref(message);
461 connman_error("D-Bus connection not available");
462 dbus_message_unref(message);
466 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
468 dbus_message_unref(message);
473 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
475 struct supplicant_task *task = user_data;
478 DBG("task %p", task);
480 reply = dbus_pending_call_steal_reply(call);
482 connman_device_set_powered(task->device, FALSE);
484 connman_device_unref(task->device);
486 connman_inet_ifdown(task->ifindex);
490 dbus_message_unref(reply);
493 static int remove_interface(struct supplicant_task *task)
495 DBusMessage *message;
496 DBusPendingCall *call;
498 DBG("task %p", task);
500 if (task->created == FALSE) {
501 connman_device_set_powered(task->device, FALSE);
505 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
506 SUPPLICANT_INTF, "removeInterface");
510 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
513 if (dbus_connection_send_with_reply(connection, message,
514 &call, TIMEOUT) == FALSE) {
515 connman_error("Failed to remove interface");
516 dbus_message_unref(message);
521 connman_error("D-Bus connection not available");
522 dbus_message_unref(message);
526 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
528 dbus_message_unref(message);
534 static int set_ap_scan(struct supplicant_task *task)
536 DBusMessage *message, *reply;
540 DBG("task %p", task);
542 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
543 SUPPLICANT_INTF ".Interface", "setAPScan");
547 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
550 dbus_error_init(&error);
552 reply = dbus_connection_send_with_reply_and_block(connection,
553 message, -1, &error);
555 if (dbus_error_is_set(&error) == TRUE) {
556 connman_error("%s", error.message);
557 dbus_error_free(&error);
559 connman_error("Failed to set AP scan");
560 dbus_message_unref(message);
564 dbus_message_unref(message);
566 dbus_message_unref(reply);
572 static int add_network(struct supplicant_task *task)
574 DBusMessage *message, *reply;
578 DBG("task %p", task);
580 if (task->netpath != NULL)
583 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
584 SUPPLICANT_INTF ".Interface", "addNetwork");
588 dbus_error_init(&error);
590 reply = dbus_connection_send_with_reply_and_block(connection,
591 message, -1, &error);
593 if (dbus_error_is_set(&error) == TRUE) {
594 connman_error("%s", error.message);
595 dbus_error_free(&error);
597 connman_error("Failed to add network");
598 dbus_message_unref(message);
602 dbus_message_unref(message);
604 dbus_error_init(&error);
606 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
607 DBUS_TYPE_INVALID) == FALSE) {
608 if (dbus_error_is_set(&error) == TRUE) {
609 connman_error("%s", error.message);
610 dbus_error_free(&error);
612 connman_error("Wrong arguments for network");
613 dbus_message_unref(reply);
617 DBG("path %s", path);
619 task->netpath = g_strdup(path);
621 dbus_message_unref(reply);
626 static int remove_network(struct supplicant_task *task)
628 DBusMessage *message, *reply;
631 DBG("task %p", task);
633 if (task->netpath == NULL)
636 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
637 SUPPLICANT_INTF ".Interface", "removeNetwork");
641 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
644 dbus_error_init(&error);
646 reply = dbus_connection_send_with_reply_and_block(connection,
647 message, -1, &error);
649 if (dbus_error_is_set(&error) == TRUE) {
650 connman_error("%s", error.message);
651 dbus_error_free(&error);
653 connman_error("Failed to remove network");
654 dbus_message_unref(message);
658 dbus_message_unref(message);
660 dbus_message_unref(reply);
662 g_free(task->netpath);
663 task->netpath = NULL;
668 static int select_network(struct supplicant_task *task)
670 DBusMessage *message, *reply;
673 DBG("task %p", task);
675 if (task->netpath == NULL)
678 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
679 SUPPLICANT_INTF ".Interface", "selectNetwork");
683 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
686 dbus_error_init(&error);
688 reply = dbus_connection_send_with_reply_and_block(connection,
689 message, -1, &error);
691 if (dbus_error_is_set(&error) == TRUE) {
692 connman_error("%s", error.message);
693 dbus_error_free(&error);
695 connman_error("Failed to select network");
696 dbus_message_unref(message);
700 dbus_message_unref(message);
702 dbus_message_unref(reply);
707 static int enable_network(struct supplicant_task *task)
709 DBusMessage *message, *reply;
712 DBG("task %p", task);
714 if (task->netpath == NULL)
717 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
718 SUPPLICANT_INTF ".Network", "enable");
722 dbus_error_init(&error);
724 reply = dbus_connection_send_with_reply_and_block(connection,
725 message, -1, &error);
727 if (dbus_error_is_set(&error) == TRUE) {
728 connman_error("%s", error.message);
729 dbus_error_free(&error);
731 connman_error("Failed to enable network");
732 dbus_message_unref(message);
736 dbus_message_unref(message);
738 dbus_message_unref(reply);
743 static int disable_network(struct supplicant_task *task)
745 DBusMessage *message, *reply;
748 DBG("task %p", task);
750 if (task->netpath == NULL)
753 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
754 SUPPLICANT_INTF ".Network", "disable");
758 dbus_error_init(&error);
760 reply = dbus_connection_send_with_reply_and_block(connection,
761 message, -1, &error);
763 if (dbus_error_is_set(&error) == TRUE) {
764 connman_error("%s", error.message);
765 dbus_error_free(&error);
767 connman_error("Failed to disable network");
768 dbus_message_unref(message);
772 dbus_message_unref(message);
774 dbus_message_unref(reply);
779 static int set_network(struct supplicant_task *task,
780 const unsigned char *network, int len,
781 const char *address, const char *security,
782 const char *passphrase)
784 DBusMessage *message, *reply;
785 DBusMessageIter array, dict;
787 dbus_uint32_t scan_ssid = 1;
789 DBG("task %p", task);
791 if (task->netpath == NULL)
794 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
795 SUPPLICANT_INTF ".Network", "set");
799 dbus_message_iter_init_append(message, &array);
801 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
802 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
803 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
804 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
806 connman_dbus_dict_append_variant(&dict, "scan_ssid",
807 DBUS_TYPE_UINT32, &scan_ssid);
810 connman_dbus_dict_append_variant(&dict, "bssid",
811 DBUS_TYPE_STRING, &address);
813 connman_dbus_dict_append_array(&dict, "ssid",
814 DBUS_TYPE_BYTE, &network, len);
816 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
817 g_ascii_strcasecmp(security, "rsn") == 0) {
818 const char *key_mgmt = "WPA-PSK";
819 connman_dbus_dict_append_variant(&dict, "key_mgmt",
820 DBUS_TYPE_STRING, &key_mgmt);
822 if (passphrase && strlen(passphrase) > 0)
823 connman_dbus_dict_append_variant(&dict, "psk",
824 DBUS_TYPE_STRING, &passphrase);
825 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
826 const char *key_mgmt = "NONE";
827 const char *auth_alg = "OPEN SHARED";
828 const char *key_index = "0";
830 connman_dbus_dict_append_variant(&dict, "auth_alg",
831 DBUS_TYPE_STRING, &auth_alg);
833 connman_dbus_dict_append_variant(&dict, "key_mgmt",
834 DBUS_TYPE_STRING, &key_mgmt);
837 int size = strlen(passphrase);
838 if (size == 10 || size == 26) {
839 unsigned char *key = malloc(13);
842 memset(tmp, 0, sizeof(tmp));
845 for (i = 0; i < size / 2; i++) {
846 memcpy(tmp, passphrase + (i * 2), 2);
847 key[i] = (unsigned char) strtol(tmp,
850 connman_dbus_dict_append_array(&dict,
851 "wep_key0", DBUS_TYPE_BYTE,
855 connman_dbus_dict_append_variant(&dict,
856 "wep_key0", DBUS_TYPE_STRING,
859 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
860 DBUS_TYPE_STRING, &key_index);
863 const char *key_mgmt = "NONE";
864 connman_dbus_dict_append_variant(&dict, "key_mgmt",
865 DBUS_TYPE_STRING, &key_mgmt);
868 dbus_message_iter_close_container(&array, &dict);
870 dbus_error_init(&error);
872 reply = dbus_connection_send_with_reply_and_block(connection,
873 message, -1, &error);
875 if (dbus_error_is_set(&error) == TRUE) {
876 connman_error("%s", error.message);
877 dbus_error_free(&error);
879 connman_error("Failed to set network options");
880 dbus_message_unref(message);
884 dbus_message_unref(message);
886 dbus_message_unref(reply);
891 static int initiate_scan(struct supplicant_task *task)
893 DBusMessage *message;
894 DBusPendingCall *call;
896 DBG("task %p", task);
898 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
899 SUPPLICANT_INTF ".Interface", "scan");
903 if (dbus_connection_send_with_reply(connection, message,
904 &call, TIMEOUT) == FALSE) {
905 connman_error("Failed to initiate scan");
906 dbus_message_unref(message);
910 dbus_message_unref(message);
919 { "<hidden>", "hidden" },
920 { "default", "linksys" },
928 { "comcomcom", "3com" },
930 { "Symbol", "symbol" },
931 { "Motorola", "motorola" },
932 { "Wireless" , "wireless" },
937 static char *build_group(const char *addr, const char *name,
938 const unsigned char *ssid, unsigned int ssid_len,
939 const char *mode, const char *security)
947 str = g_string_sized_new((ssid_len * 2) + 24);
952 g_string_append_printf(str, "hidden_%s", addr);
956 for (i = 0; special_ssid[i].name; i++) {
957 if (g_strcmp0(special_ssid[i].name, name) == 0) {
958 if (special_ssid[i].value == NULL)
959 g_string_append_printf(str, "%s_%s",
962 g_string_append_printf(str, "%s_%s",
963 special_ssid[i].value, addr);
968 if (ssid_len > 0 && ssid[0] != '\0') {
969 for (i = 0; i < ssid_len; i++)
970 g_string_append_printf(str, "%02x", ssid[i]);
972 g_string_append_printf(str, "hidden_%s", addr);
975 g_string_append_printf(str, "_%s_%s", mode, security);
977 return g_string_free(str, FALSE);
980 static void extract_addr(DBusMessageIter *value,
981 struct supplicant_result *result)
983 DBusMessageIter array;
984 struct ether_addr eth;
988 dbus_message_iter_recurse(value, &array);
989 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
994 result->addr = g_try_malloc(addr_len);
995 if (result->addr == NULL)
998 memcpy(result->addr, addr, addr_len);
999 result->addr_len = addr_len;
1001 result->path = g_try_malloc0(13);
1002 if (result->path == NULL)
1005 memcpy(ð, addr, sizeof(eth));
1006 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1007 eth.ether_addr_octet[0],
1008 eth.ether_addr_octet[1],
1009 eth.ether_addr_octet[2],
1010 eth.ether_addr_octet[3],
1011 eth.ether_addr_octet[4],
1012 eth.ether_addr_octet[5]);
1015 static void extract_ssid(DBusMessageIter *value,
1016 struct supplicant_result *result)
1018 DBusMessageIter array;
1019 unsigned char *ssid;
1022 dbus_message_iter_recurse(value, &array);
1023 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1028 if (ssid[0] == '\0')
1031 result->ssid = g_try_malloc(ssid_len);
1032 if (result->ssid == NULL)
1035 memcpy(result->ssid, ssid, ssid_len);
1036 result->ssid_len = ssid_len;
1038 result->name = g_try_malloc0(ssid_len + 1);
1039 if (result->name == NULL)
1042 for (i = 0; i < ssid_len; i++) {
1043 if (g_ascii_isprint(ssid[i]))
1044 result->name[i] = ssid[i];
1046 result->name[i] = ' ';
1050 static void extract_wpaie(DBusMessageIter *value,
1051 struct supplicant_result *result)
1053 DBusMessageIter array;
1057 dbus_message_iter_recurse(value, &array);
1058 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1061 result->has_wpa = TRUE;
1064 static void extract_rsnie(DBusMessageIter *value,
1065 struct supplicant_result *result)
1067 DBusMessageIter array;
1071 dbus_message_iter_recurse(value, &array);
1072 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1075 result->has_rsn = TRUE;
1078 static void extract_wpsie(DBusMessageIter *value,
1079 struct supplicant_result *result)
1081 DBusMessageIter array;
1085 dbus_message_iter_recurse(value, &array);
1086 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1089 result->has_wps = TRUE;
1092 static void extract_capabilites(DBusMessageIter *value,
1093 struct supplicant_result *result)
1095 dbus_message_iter_get_basic(value, &result->capabilities);
1097 if (result->capabilities & IEEE80211_CAP_ESS)
1098 result->adhoc = FALSE;
1099 else if (result->capabilities & IEEE80211_CAP_IBSS)
1100 result->adhoc = TRUE;
1102 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1103 result->has_wep = TRUE;
1106 static unsigned char calculate_strength(struct supplicant_task *task,
1107 struct supplicant_result *result)
1109 if (task->range->max_qual.qual == 0) {
1110 unsigned char strength;
1112 if (result->level > 0)
1113 strength = 100 - result->level;
1115 strength = 120 + result->level;
1123 return (result->quality * 100) / task->range->max_qual.qual;
1126 static unsigned short calculate_channel(struct supplicant_result *result)
1128 if (result->frequency < 0)
1131 return (result->frequency - 2407) / 5;
1134 static void get_properties(struct supplicant_task *task);
1136 static void properties_reply(DBusPendingCall *call, void *user_data)
1138 struct supplicant_task *task = user_data;
1139 struct supplicant_result result;
1140 struct connman_network *network;
1142 DBusMessageIter array, dict;
1143 unsigned char strength;
1144 unsigned short channel, frequency;
1145 const char *mode, *security;
1147 unsigned int ssid_len;
1149 DBG("task %p", task);
1151 reply = dbus_pending_call_steal_reply(call);
1152 if (reply == NULL) {
1153 get_properties(task);
1157 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1158 dbus_message_unref(reply);
1159 get_properties(task);
1163 memset(&result, 0, sizeof(result));
1164 result.frequency = -1;
1165 result.quality = -1;
1169 dbus_message_iter_init(reply, &array);
1171 dbus_message_iter_recurse(&array, &dict);
1173 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1174 DBusMessageIter entry, value;
1177 dbus_message_iter_recurse(&dict, &entry);
1178 dbus_message_iter_get_basic(&entry, &key);
1180 dbus_message_iter_next(&entry);
1182 dbus_message_iter_recurse(&entry, &value);
1184 //type = dbus_message_iter_get_arg_type(&value);
1185 //dbus_message_iter_get_basic(&value, &val);
1193 * frequency : i (105)
1194 * capabilities : q (113)
1201 if (g_str_equal(key, "bssid") == TRUE)
1202 extract_addr(&value, &result);
1203 else if (g_str_equal(key, "ssid") == TRUE)
1204 extract_ssid(&value, &result);
1205 else if (g_str_equal(key, "wpaie") == TRUE)
1206 extract_wpaie(&value, &result);
1207 else if (g_str_equal(key, "rsnie") == TRUE)
1208 extract_rsnie(&value, &result);
1209 else if (g_str_equal(key, "wpsie") == TRUE)
1210 extract_wpsie(&value, &result);
1211 else if (g_str_equal(key, "capabilities") == TRUE)
1212 extract_capabilites(&value, &result);
1213 else if (g_str_equal(key, "frequency") == TRUE)
1214 dbus_message_iter_get_basic(&value, &result.frequency);
1215 else if (g_str_equal(key, "quality") == TRUE)
1216 dbus_message_iter_get_basic(&value, &result.quality);
1217 else if (g_str_equal(key, "noise") == TRUE)
1218 dbus_message_iter_get_basic(&value, &result.noise);
1219 else if (g_str_equal(key, "level") == TRUE)
1220 dbus_message_iter_get_basic(&value, &result.level);
1221 else if (g_str_equal(key, "maxrate") == TRUE)
1222 dbus_message_iter_get_basic(&value, &result.maxrate);
1224 dbus_message_iter_next(&dict);
1227 if (result.path == NULL)
1230 if (result.path[0] == '\0')
1233 if (result.frequency > 0 && result.frequency < 14)
1234 result.frequency = 2407 + (5 * result.frequency);
1235 else if (result.frequency == 14)
1236 result.frequency = 2484;
1238 strength = calculate_strength(task, &result);
1239 channel = calculate_channel(&result);
1241 frequency = (result.frequency < 0) ? 0 : result.frequency;
1243 if (result.has_rsn == TRUE)
1245 else if (result.has_wpa == TRUE)
1247 else if (result.has_wep == TRUE)
1252 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1254 group = build_group(result.path, result.name,
1255 result.ssid, result.ssid_len,
1258 network = connman_device_get_network(task->device, result.path);
1259 if (network == NULL) {
1262 network = connman_network_create(result.path,
1263 CONNMAN_NETWORK_TYPE_WIFI);
1264 if (network == NULL)
1267 index = connman_device_get_index(task->device);
1268 connman_network_set_index(network, index);
1270 connman_network_set_protocol(network,
1271 CONNMAN_NETWORK_PROTOCOL_IP);
1273 connman_network_set_address(network, result.addr,
1276 if (connman_device_add_network(task->device, network) < 0) {
1277 connman_network_unref(network);
1282 if (result.name != NULL && result.name[0] != '\0')
1283 connman_network_set_name(network, result.name);
1285 if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1286 connman_network_set_blob(network, "WiFi.SSID",
1287 result.ssid, result.ssid_len);
1290 connman_network_set_string(network, "WiFi.Mode", mode);
1292 DBG("%s (%s %s) strength %d (%s)",
1293 result.name, mode, security, strength,
1294 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1296 connman_network_set_available(network, TRUE);
1297 connman_network_set_strength(network, strength);
1299 connman_network_set_uint16(network, "Frequency", frequency);
1300 connman_network_set_uint16(network, "WiFi.Channel", channel);
1301 connman_network_set_string(network, "WiFi.Security", security);
1303 if (result.ssid != NULL)
1304 connman_network_set_group(network, group);
1309 g_free(result.path);
1310 g_free(result.addr);
1311 g_free(result.name);
1312 g_free(result.ssid);
1314 dbus_message_unref(reply);
1316 get_properties(task);
1319 static void get_properties(struct supplicant_task *task)
1321 DBusMessage *message;
1322 DBusPendingCall *call;
1325 path = g_slist_nth_data(task->scan_results, 0);
1329 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1330 SUPPLICANT_INTF ".BSSID",
1333 task->scan_results = g_slist_remove(task->scan_results, path);
1336 if (message == NULL)
1339 if (dbus_connection_send_with_reply(connection, message,
1340 &call, TIMEOUT) == FALSE) {
1341 connman_error("Failed to get network properties");
1342 dbus_message_unref(message);
1347 connman_error("D-Bus connection not available");
1348 dbus_message_unref(message);
1352 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1354 dbus_message_unref(message);
1359 if (task->noscan == FALSE)
1360 connman_device_set_scanning(task->device, FALSE);
1363 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1365 struct supplicant_task *task = user_data;
1371 DBG("task %p", task);
1373 reply = dbus_pending_call_steal_reply(call);
1377 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1380 dbus_error_init(&error);
1382 if (dbus_message_get_args(reply, &error,
1383 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1384 &results, &num_results,
1385 DBUS_TYPE_INVALID) == FALSE) {
1386 if (dbus_error_is_set(&error) == TRUE) {
1387 connman_error("%s", error.message);
1388 dbus_error_free(&error);
1390 connman_error("Wrong arguments for scan result");
1394 if (num_results == 0)
1397 for (i = 0; i < num_results; i++) {
1398 char *path = g_strdup(results[i]);
1402 task->scan_results = g_slist_append(task->scan_results, path);
1405 g_strfreev(results);
1407 dbus_message_unref(reply);
1409 get_properties(task);
1414 dbus_message_unref(reply);
1417 if (task->noscan == FALSE)
1418 connman_device_set_scanning(task->device, FALSE);
1421 static void scan_results_available(struct supplicant_task *task)
1423 DBusMessage *message;
1424 DBusPendingCall *call;
1426 DBG("task %p", task);
1428 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1429 SUPPLICANT_INTF ".Interface",
1431 if (message == NULL)
1434 if (dbus_connection_send_with_reply(connection, message,
1435 &call, TIMEOUT) == FALSE) {
1436 connman_error("Failed to request scan result");
1440 if (task->noscan == FALSE)
1441 connman_device_set_scanning(task->device, TRUE);
1444 connman_error("D-Bus connection not available");
1448 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1451 dbus_message_unref(message);
1454 static enum supplicant_state string2state(const char *state)
1456 if (g_str_equal(state, "INACTIVE") == TRUE)
1457 return WPA_INACTIVE;
1458 else if (g_str_equal(state, "SCANNING") == TRUE)
1459 return WPA_SCANNING;
1460 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1461 return WPA_ASSOCIATING;
1462 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1463 return WPA_ASSOCIATED;
1464 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1465 return WPA_GROUP_HANDSHAKE;
1466 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1467 return WPA_4WAY_HANDSHAKE;
1468 else if (g_str_equal(state, "COMPLETED") == TRUE)
1469 return WPA_COMPLETED;
1470 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1471 return WPA_DISCONNECTED;
1476 static int task_connect(struct supplicant_task *task)
1478 const char *address, *security, *passphrase;
1480 unsigned int ssid_len;
1482 address = connman_network_get_string(task->network, "Address");
1483 security = connman_network_get_string(task->network, "WiFi.Security");
1484 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1486 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1488 DBG("address %s security %s passphrase %s",
1489 address, security, passphrase);
1491 if (security == NULL && passphrase == NULL)
1494 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1499 select_network(task);
1500 disable_network(task);
1502 set_network(task, ssid, ssid_len, address, security, passphrase);
1504 enable_network(task);
1509 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1512 const char *newstate, *oldstate;
1513 enum supplicant_state state;
1515 dbus_error_init(&error);
1517 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1518 DBUS_TYPE_STRING, &oldstate,
1519 DBUS_TYPE_INVALID) == FALSE) {
1520 if (dbus_error_is_set(&error) == TRUE) {
1521 connman_error("%s", error.message);
1522 dbus_error_free(&error);
1524 connman_error("Wrong arguments for state change");
1528 DBG("state %s ==> %s", oldstate, newstate);
1530 state = string2state(newstate);
1531 if (state == WPA_INVALID)
1534 task->state = state;
1536 switch (task->state) {
1538 task->noscan = TRUE;
1539 connman_device_set_scanning(task->device, TRUE);
1541 case WPA_ASSOCIATING:
1542 case WPA_ASSOCIATED:
1543 case WPA_4WAY_HANDSHAKE:
1544 case WPA_GROUP_HANDSHAKE:
1545 task->noscan = TRUE;
1548 case WPA_DISCONNECTED:
1549 task->noscan = FALSE;
1552 task->noscan = FALSE;
1553 connman_device_set_scanning(task->device, FALSE);
1559 if (task->network == NULL)
1562 switch (task->state) {
1564 if (connman_network_get_group(task->network) == NULL) {
1565 const char *name, *mode, *security;
1569 * This is a hidden network, we need to set its
1570 * group based on the BSSID we just joined.
1572 bssid = get_bssid(task->device);
1574 name = connman_network_get_string(task->network,
1576 mode = connman_network_get_string(task->network,
1578 security = connman_network_get_string(task->network,
1581 if (bssid && name && mode && security) {
1584 group = build_group(bssid, name, NULL, 0,
1586 connman_network_set_group(task->network, group);
1594 connman_network_set_connected(task->network, TRUE);
1595 connman_device_set_scanning(task->device, FALSE);
1598 case WPA_DISCONNECTED:
1599 if (task->disconnecting == TRUE) {
1600 connman_network_set_connected(task->network, FALSE);
1601 connman_network_unref(task->network);
1602 task->disconnecting = FALSE;
1604 if (task->pending_network != NULL) {
1605 task->network = task->pending_network;
1606 task->pending_network = NULL;
1611 connman_network_set_connected(task->network, FALSE);
1612 connman_device_set_scanning(task->device, FALSE);
1616 case WPA_ASSOCIATING:
1617 connman_network_set_associating(task->network, TRUE);
1621 connman_network_set_associating(task->network, FALSE);
1626 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1627 DBusMessage *msg, void *data)
1629 struct supplicant_task *task;
1630 const char *member, *path;
1632 if (dbus_message_has_interface(msg,
1633 SUPPLICANT_INTF ".Interface") == FALSE)
1634 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1636 member = dbus_message_get_member(msg);
1638 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1640 path = dbus_message_get_path(msg);
1642 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1644 task = find_task_by_path(path);
1646 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1648 DBG("task %p member %s", task, member);
1650 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1651 scan_results_available(task);
1652 else if (g_str_equal(member, "StateChange") == TRUE)
1653 state_change(task, msg);
1655 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1658 int supplicant_start(struct connman_device *device)
1660 struct supplicant_task *task;
1663 DBG("device %p", device);
1665 task = g_try_new0(struct supplicant_task, 1);
1669 task->ifindex = connman_device_get_index(device);
1670 task->ifname = connman_inet_ifname(task->ifindex);
1672 if (task->ifname == NULL) {
1677 task->range = g_try_malloc0(sizeof(struct iw_range));
1678 if (task->range == NULL) {
1683 err = get_range(task);
1687 task->device = connman_device_ref(device);
1689 task->created = FALSE;
1690 task->noscan = FALSE;
1691 task->state = WPA_INVALID;
1692 task->disconnecting = FALSE;
1693 task->pending_network = NULL;
1695 task_list = g_slist_append(task_list, task);
1697 return create_interface(task);
1700 g_free(task->range);
1701 g_free(task->ifname);
1707 int supplicant_stop(struct connman_device *device)
1709 int index = connman_device_get_index(device);
1710 struct supplicant_task *task;
1712 DBG("device %p", device);
1714 task = find_task_by_index(index);
1718 g_free(task->range);
1720 task_list = g_slist_remove(task_list, task);
1722 disable_network(task);
1724 remove_network(task);
1726 return remove_interface(task);
1729 int supplicant_scan(struct connman_device *device)
1731 int index = connman_device_get_index(device);
1732 struct supplicant_task *task;
1735 DBG("device %p", device);
1737 task = find_task_by_index(index);
1741 switch (task->state) {
1744 case WPA_ASSOCIATING:
1745 case WPA_ASSOCIATED:
1746 case WPA_4WAY_HANDSHAKE:
1747 case WPA_GROUP_HANDSHAKE:
1753 err = initiate_scan(task);
1758 int supplicant_connect(struct connman_network *network)
1760 struct supplicant_task *task;
1763 DBG("network %p", network);
1765 index = connman_network_get_index(network);
1767 task = find_task_by_index(index);
1771 if (task->disconnecting == TRUE)
1772 task->pending_network = connman_network_ref(network);
1774 task->network = connman_network_ref(network);
1775 return task_connect(task);
1781 int supplicant_disconnect(struct connman_network *network)
1783 struct supplicant_task *task;
1786 DBG("network %p", network);
1788 index = connman_network_get_index(network);
1790 task = find_task_by_index(index);
1794 if (task->disconnecting == TRUE)
1795 return -EINPROGRESS;
1797 disable_network(task);
1799 remove_network(task);
1801 task->disconnecting = TRUE;
1806 static void supplicant_activate(DBusConnection *conn)
1808 DBusMessage *message;
1810 DBG("conn %p", conn);
1812 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1813 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1814 if (message == NULL)
1817 dbus_message_set_no_reply(message, TRUE);
1819 dbus_connection_send(conn, message, NULL);
1821 dbus_message_unref(message);
1824 static GSList *driver_list = NULL;
1826 static void supplicant_probe(DBusConnection *conn, void *user_data)
1830 DBG("conn %p", conn);
1832 for (list = driver_list; list; list = list->next) {
1833 struct supplicant_driver *driver = list->data;
1835 DBG("driver %p name %s", driver, driver->name);
1842 static void supplicant_remove(DBusConnection *conn, void *user_data)
1846 DBG("conn %p", conn);
1848 for (list = driver_list; list; list = list->next) {
1849 struct supplicant_driver *driver = list->data;
1851 DBG("driver %p name %s", driver, driver->name);
1858 static const char *supplicant_rule = "type=signal,"
1859 "interface=" SUPPLICANT_INTF ".Interface";
1862 static int supplicant_create(void)
1864 if (g_slist_length(driver_list) > 0)
1867 connection = connman_dbus_get_connection();
1868 if (connection == NULL)
1871 DBG("connection %p", connection);
1873 if (dbus_connection_add_filter(connection,
1874 supplicant_filter, NULL, NULL) == FALSE) {
1875 connection = connman_dbus_get_connection();
1879 dbus_bus_add_match(connection, supplicant_rule, NULL);
1880 dbus_connection_flush(connection);
1882 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1883 supplicant_probe, supplicant_remove, NULL, NULL);
1888 static void supplicant_destroy(void)
1890 if (g_slist_length(driver_list) > 0)
1893 DBG("connection %p", connection);
1896 g_dbus_remove_watch(connection, watch);
1898 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1899 dbus_connection_flush(connection);
1901 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1903 dbus_connection_unref(connection);
1907 int supplicant_register(struct supplicant_driver *driver)
1911 DBG("driver %p name %s", driver, driver->name);
1913 err = supplicant_create();
1917 driver_list = g_slist_append(driver_list, driver);
1919 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1920 supplicant_probe(connection, NULL);
1922 supplicant_activate(connection);
1927 void supplicant_unregister(struct supplicant_driver *driver)
1929 DBG("driver %p name %s", driver, driver->name);
1931 supplicant_remove(connection, NULL);
1933 driver_list = g_slist_remove(driver_list, driver);
1935 supplicant_destroy();