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 void add_interface_reply(DBusPendingCall *call, void *user_data)
234 struct supplicant_task *task = user_data;
239 DBG("task %p", task);
241 reply = dbus_pending_call_steal_reply(call);
245 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
248 dbus_error_init(&error);
250 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
251 DBUS_TYPE_INVALID) == FALSE) {
252 if (dbus_error_is_set(&error) == TRUE) {
253 connman_error("%s", error.message);
254 dbus_error_free(&error);
256 connman_error("Wrong arguments for add interface");
260 DBG("path %s", path);
262 task->path = g_strdup(path);
263 task->created = TRUE;
265 connman_device_set_powered(task->device, TRUE);
267 dbus_message_unref(reply);
272 task_list = g_slist_remove(task_list, task);
274 connman_device_unref(task->device);
279 static int add_interface(struct supplicant_task *task)
281 const char *driver = connman_option_get_string("wifi");
282 DBusMessage *message;
283 DBusMessageIter array, dict;
284 DBusPendingCall *call;
286 DBG("task %p", task);
288 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
289 SUPPLICANT_INTF, "addInterface");
293 dbus_message_iter_init_append(message, &array);
295 dbus_message_iter_append_basic(&array,
296 DBUS_TYPE_STRING, &task->ifname);
298 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
299 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
300 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
301 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
303 connman_dbus_dict_append_variant(&dict, "driver",
304 DBUS_TYPE_STRING, &driver);
306 dbus_message_iter_close_container(&array, &dict);
308 if (dbus_connection_send_with_reply(connection, message,
309 &call, TIMEOUT) == FALSE) {
310 connman_error("Failed to add interface");
311 dbus_message_unref(message);
316 connman_error("D-Bus connection not available");
317 dbus_message_unref(message);
321 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
323 dbus_message_unref(message);
328 static void get_interface_reply(DBusPendingCall *call, void *user_data)
330 struct supplicant_task *task = user_data;
335 DBG("task %p", task);
337 reply = dbus_pending_call_steal_reply(call);
341 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
346 dbus_error_init(&error);
348 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
349 DBUS_TYPE_INVALID) == FALSE) {
350 if (dbus_error_is_set(&error) == TRUE) {
351 connman_error("%s", error.message);
352 dbus_error_free(&error);
354 connman_error("Wrong arguments for get interface");
358 DBG("path %s", path);
360 task->path = g_strdup(path);
361 task->created = FALSE;
363 connman_device_set_powered(task->device, TRUE);
366 dbus_message_unref(reply);
369 static int create_interface(struct supplicant_task *task)
371 DBusMessage *message;
372 DBusPendingCall *call;
374 DBG("task %p", task);
376 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
377 SUPPLICANT_INTF, "getInterface");
381 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
384 if (dbus_connection_send_with_reply(connection, message,
385 &call, TIMEOUT) == FALSE) {
386 connman_error("Failed to get interface");
387 dbus_message_unref(message);
392 connman_error("D-Bus connection not available");
393 dbus_message_unref(message);
397 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
399 dbus_message_unref(message);
404 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
406 struct supplicant_task *task = user_data;
409 DBG("task %p", task);
411 reply = dbus_pending_call_steal_reply(call);
413 connman_device_set_powered(task->device, FALSE);
415 connman_device_unref(task->device);
417 connman_inet_ifdown(task->ifindex);
421 dbus_message_unref(reply);
424 static int remove_interface(struct supplicant_task *task)
426 DBusMessage *message;
427 DBusPendingCall *call;
429 DBG("task %p", task);
431 if (task->created == FALSE) {
432 connman_device_set_powered(task->device, FALSE);
436 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
437 SUPPLICANT_INTF, "removeInterface");
441 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
444 if (dbus_connection_send_with_reply(connection, message,
445 &call, TIMEOUT) == FALSE) {
446 connman_error("Failed to remove interface");
447 dbus_message_unref(message);
452 connman_error("D-Bus connection not available");
453 dbus_message_unref(message);
457 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
459 dbus_message_unref(message);
465 static int set_ap_scan(struct supplicant_task *task)
467 DBusMessage *message, *reply;
471 DBG("task %p", task);
473 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
474 SUPPLICANT_INTF ".Interface", "setAPScan");
478 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
481 dbus_error_init(&error);
483 reply = dbus_connection_send_with_reply_and_block(connection,
484 message, -1, &error);
486 if (dbus_error_is_set(&error) == TRUE) {
487 connman_error("%s", error.message);
488 dbus_error_free(&error);
490 connman_error("Failed to set AP scan");
491 dbus_message_unref(message);
495 dbus_message_unref(message);
497 dbus_message_unref(reply);
503 static int add_network(struct supplicant_task *task)
505 DBusMessage *message, *reply;
509 DBG("task %p", task);
511 if (task->netpath != NULL)
514 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
515 SUPPLICANT_INTF ".Interface", "addNetwork");
519 dbus_error_init(&error);
521 reply = dbus_connection_send_with_reply_and_block(connection,
522 message, -1, &error);
524 if (dbus_error_is_set(&error) == TRUE) {
525 connman_error("%s", error.message);
526 dbus_error_free(&error);
528 connman_error("Failed to add network");
529 dbus_message_unref(message);
533 dbus_message_unref(message);
535 dbus_error_init(&error);
537 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
538 DBUS_TYPE_INVALID) == FALSE) {
539 if (dbus_error_is_set(&error) == TRUE) {
540 connman_error("%s", error.message);
541 dbus_error_free(&error);
543 connman_error("Wrong arguments for network");
544 dbus_message_unref(reply);
548 DBG("path %s", path);
550 task->netpath = g_strdup(path);
552 dbus_message_unref(reply);
557 static int remove_network(struct supplicant_task *task)
559 DBusMessage *message, *reply;
562 DBG("task %p", task);
564 if (task->netpath == NULL)
567 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
568 SUPPLICANT_INTF ".Interface", "removeNetwork");
572 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
575 dbus_error_init(&error);
577 reply = dbus_connection_send_with_reply_and_block(connection,
578 message, -1, &error);
580 if (dbus_error_is_set(&error) == TRUE) {
581 connman_error("%s", error.message);
582 dbus_error_free(&error);
584 connman_error("Failed to remove network");
585 dbus_message_unref(message);
589 dbus_message_unref(message);
591 dbus_message_unref(reply);
593 g_free(task->netpath);
594 task->netpath = NULL;
599 static int select_network(struct supplicant_task *task)
601 DBusMessage *message, *reply;
604 DBG("task %p", task);
606 if (task->netpath == NULL)
609 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
610 SUPPLICANT_INTF ".Interface", "selectNetwork");
614 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
617 dbus_error_init(&error);
619 reply = dbus_connection_send_with_reply_and_block(connection,
620 message, -1, &error);
622 if (dbus_error_is_set(&error) == TRUE) {
623 connman_error("%s", error.message);
624 dbus_error_free(&error);
626 connman_error("Failed to select network");
627 dbus_message_unref(message);
631 dbus_message_unref(message);
633 dbus_message_unref(reply);
638 static int enable_network(struct supplicant_task *task)
640 DBusMessage *message, *reply;
643 DBG("task %p", task);
645 if (task->netpath == NULL)
648 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
649 SUPPLICANT_INTF ".Network", "enable");
653 dbus_error_init(&error);
655 reply = dbus_connection_send_with_reply_and_block(connection,
656 message, -1, &error);
658 if (dbus_error_is_set(&error) == TRUE) {
659 connman_error("%s", error.message);
660 dbus_error_free(&error);
662 connman_error("Failed to enable network");
663 dbus_message_unref(message);
667 dbus_message_unref(message);
669 dbus_message_unref(reply);
674 static int disable_network(struct supplicant_task *task)
676 DBusMessage *message, *reply;
679 DBG("task %p", task);
681 if (task->netpath == NULL)
684 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
685 SUPPLICANT_INTF ".Network", "disable");
689 dbus_error_init(&error);
691 reply = dbus_connection_send_with_reply_and_block(connection,
692 message, -1, &error);
694 if (dbus_error_is_set(&error) == TRUE) {
695 connman_error("%s", error.message);
696 dbus_error_free(&error);
698 connman_error("Failed to disable network");
699 dbus_message_unref(message);
703 dbus_message_unref(message);
705 dbus_message_unref(reply);
710 static int set_network(struct supplicant_task *task,
711 const unsigned char *network, int len,
712 const char *address, const char *security,
713 const char *passphrase)
715 DBusMessage *message, *reply;
716 DBusMessageIter array, dict;
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", "set");
729 dbus_message_iter_init_append(message, &array);
731 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
732 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
733 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
734 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
736 if (address == NULL) {
737 dbus_uint32_t scan_ssid = 1;
738 connman_dbus_dict_append_variant(&dict, "scan_ssid",
739 DBUS_TYPE_UINT32, &scan_ssid);
741 connman_dbus_dict_append_variant(&dict, "bssid",
742 DBUS_TYPE_STRING, &address);
744 connman_dbus_dict_append_array(&dict, "ssid",
745 DBUS_TYPE_BYTE, &network, len);
747 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
748 g_ascii_strcasecmp(security, "rsn") == 0) {
749 const char *key_mgmt = "WPA-PSK";
750 connman_dbus_dict_append_variant(&dict, "key_mgmt",
751 DBUS_TYPE_STRING, &key_mgmt);
753 if (passphrase && strlen(passphrase) > 0)
754 connman_dbus_dict_append_variant(&dict, "psk",
755 DBUS_TYPE_STRING, &passphrase);
756 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
757 const char *key_mgmt = "NONE", *index = "0";
758 const char *auth_alg = "OPEN SHARED";
759 connman_dbus_dict_append_variant(&dict, "key_mgmt",
760 DBUS_TYPE_STRING, &key_mgmt);
762 connman_dbus_dict_append_variant(&dict, "auth_alg",
763 DBUS_TYPE_STRING, &auth_alg);
766 int size = strlen(passphrase);
767 if (size == 10 || size == 26) {
768 unsigned char *key = malloc(13);
771 memset(tmp, 0, sizeof(tmp));
774 for (i = 0; i < size / 2; i++) {
775 memcpy(tmp, passphrase + (i * 2), 2);
776 key[i] = (unsigned char) strtol(tmp,
779 connman_dbus_dict_append_array(&dict,
780 "wep_key0", DBUS_TYPE_BYTE,
784 connman_dbus_dict_append_variant(&dict,
785 "wep_key0", DBUS_TYPE_STRING,
787 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
788 DBUS_TYPE_STRING, &index);
791 const char *key_mgmt = "NONE";
792 connman_dbus_dict_append_variant(&dict, "key_mgmt",
793 DBUS_TYPE_STRING, &key_mgmt);
796 dbus_message_iter_close_container(&array, &dict);
798 dbus_error_init(&error);
800 reply = dbus_connection_send_with_reply_and_block(connection,
801 message, -1, &error);
803 if (dbus_error_is_set(&error) == TRUE) {
804 connman_error("%s", error.message);
805 dbus_error_free(&error);
807 connman_error("Failed to set network options");
808 dbus_message_unref(message);
812 dbus_message_unref(message);
814 dbus_message_unref(reply);
819 static int initiate_scan(struct supplicant_task *task)
821 DBusMessage *message;
822 DBusPendingCall *call;
824 DBG("task %p", task);
826 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
827 SUPPLICANT_INTF ".Interface", "scan");
831 if (dbus_connection_send_with_reply(connection, message,
832 &call, TIMEOUT) == FALSE) {
833 connman_error("Failed to initiate scan");
834 dbus_message_unref(message);
838 dbus_message_unref(message);
847 { "<hidden>", "hidden" },
848 { "default", "linksys" },
856 { "comcomcom", "3com" },
858 { "Symbol", "symbol" },
859 { "Motorola", "motorola" },
860 { "Wireless" , "wireless" },
865 static char *build_group(const char *addr, const char *name,
866 const unsigned char *ssid, unsigned int ssid_len,
867 const char *mode, const char *security)
875 str = g_string_sized_new((ssid_len * 2) + 24);
879 for (i = 0; special_ssid[i].name; i++) {
880 if (g_strcmp0(special_ssid[i].name, name) == 0) {
881 if (special_ssid[i].value == NULL)
882 g_string_append_printf(str, "%s_%s",
885 g_string_append_printf(str, "%s_%s",
886 special_ssid[i].value, addr);
891 if (ssid_len > 0 && ssid[0] != '\0') {
892 for (i = 0; i < ssid_len; i++)
893 g_string_append_printf(str, "%02x", ssid[i]);
895 g_string_append_printf(str, "hidden_%s", addr);
898 g_string_append_printf(str, "_%s_%s", mode, security);
900 return g_string_free(str, FALSE);
903 static void extract_addr(DBusMessageIter *value,
904 struct supplicant_result *result)
906 DBusMessageIter array;
907 struct ether_addr *eth;
911 dbus_message_iter_recurse(value, &array);
912 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
917 result->addr = g_try_malloc(addr_len);
918 if (result->addr == NULL)
921 memcpy(result->addr, addr, addr_len);
922 result->addr_len = addr_len;
924 result->path = g_try_malloc0(13);
925 if (result->path == NULL)
930 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
931 eth->ether_addr_octet[0],
932 eth->ether_addr_octet[1],
933 eth->ether_addr_octet[2],
934 eth->ether_addr_octet[3],
935 eth->ether_addr_octet[4],
936 eth->ether_addr_octet[5]);
939 static void extract_ssid(DBusMessageIter *value,
940 struct supplicant_result *result)
942 DBusMessageIter array;
946 dbus_message_iter_recurse(value, &array);
947 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
952 result->ssid = g_try_malloc(ssid_len);
953 if (result->ssid == NULL)
956 memcpy(result->ssid, ssid, ssid_len);
957 result->ssid_len = ssid_len;
959 result->name = g_try_malloc0(ssid_len + 1);
960 if (result->name == NULL)
963 for (i = 0; i < ssid_len; i++) {
964 if (g_ascii_isprint(ssid[i]))
965 result->name[i] = ssid[i];
967 result->name[i] = ' ';
971 static void extract_wpaie(DBusMessageIter *value,
972 struct supplicant_result *result)
974 DBusMessageIter array;
978 dbus_message_iter_recurse(value, &array);
979 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
982 result->has_wpa = TRUE;
985 static void extract_rsnie(DBusMessageIter *value,
986 struct supplicant_result *result)
988 DBusMessageIter array;
992 dbus_message_iter_recurse(value, &array);
993 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
996 result->has_rsn = TRUE;
999 static void extract_wpsie(DBusMessageIter *value,
1000 struct supplicant_result *result)
1002 DBusMessageIter array;
1006 dbus_message_iter_recurse(value, &array);
1007 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1010 result->has_wps = TRUE;
1013 static void extract_capabilites(DBusMessageIter *value,
1014 struct supplicant_result *result)
1016 dbus_message_iter_get_basic(value, &result->capabilities);
1018 if (result->capabilities & IEEE80211_CAP_ESS)
1019 result->adhoc = FALSE;
1020 else if (result->capabilities & IEEE80211_CAP_IBSS)
1021 result->adhoc = TRUE;
1023 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1024 result->has_wep = TRUE;
1027 static unsigned char calculate_strength(struct supplicant_task *task,
1028 struct supplicant_result *result)
1030 if (task->range->max_qual.qual == 0) {
1031 unsigned char strength;
1033 if (result->level > 0)
1034 strength = 100 - result->level;
1036 strength = 120 + result->level;
1044 return (result->quality * 100) / task->range->max_qual.qual;
1047 static unsigned short calculate_channel(struct supplicant_result *result)
1049 if (result->frequency < 0)
1052 return (result->frequency - 2407) / 5;
1055 static void get_properties(struct supplicant_task *task);
1057 static void properties_reply(DBusPendingCall *call, void *user_data)
1059 struct supplicant_task *task = user_data;
1060 struct supplicant_result result;
1061 struct connman_network *network;
1063 DBusMessageIter array, dict;
1064 unsigned char strength;
1065 unsigned short channel, frequency;
1066 const char *mode, *security;
1069 DBG("task %p", task);
1071 reply = dbus_pending_call_steal_reply(call);
1072 if (reply == NULL) {
1073 get_properties(task);
1077 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1078 dbus_message_unref(reply);
1079 get_properties(task);
1083 memset(&result, 0, sizeof(result));
1084 result.frequency = -1;
1085 result.quality = -1;
1089 dbus_message_iter_init(reply, &array);
1091 dbus_message_iter_recurse(&array, &dict);
1093 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1094 DBusMessageIter entry, value;
1097 dbus_message_iter_recurse(&dict, &entry);
1098 dbus_message_iter_get_basic(&entry, &key);
1100 dbus_message_iter_next(&entry);
1102 dbus_message_iter_recurse(&entry, &value);
1104 //type = dbus_message_iter_get_arg_type(&value);
1105 //dbus_message_iter_get_basic(&value, &val);
1113 * frequency : i (105)
1114 * capabilities : q (113)
1121 if (g_str_equal(key, "bssid") == TRUE)
1122 extract_addr(&value, &result);
1123 else if (g_str_equal(key, "ssid") == TRUE)
1124 extract_ssid(&value, &result);
1125 else if (g_str_equal(key, "wpaie") == TRUE)
1126 extract_wpaie(&value, &result);
1127 else if (g_str_equal(key, "rsnie") == TRUE)
1128 extract_rsnie(&value, &result);
1129 else if (g_str_equal(key, "wpsie") == TRUE)
1130 extract_wpsie(&value, &result);
1131 else if (g_str_equal(key, "capabilities") == TRUE)
1132 extract_capabilites(&value, &result);
1133 else if (g_str_equal(key, "frequency") == TRUE)
1134 dbus_message_iter_get_basic(&value, &result.frequency);
1135 else if (g_str_equal(key, "quality") == TRUE)
1136 dbus_message_iter_get_basic(&value, &result.quality);
1137 else if (g_str_equal(key, "noise") == TRUE)
1138 dbus_message_iter_get_basic(&value, &result.noise);
1139 else if (g_str_equal(key, "level") == TRUE)
1140 dbus_message_iter_get_basic(&value, &result.level);
1141 else if (g_str_equal(key, "maxrate") == TRUE)
1142 dbus_message_iter_get_basic(&value, &result.maxrate);
1144 dbus_message_iter_next(&dict);
1147 if (result.path == NULL)
1150 if (result.path[0] == '\0')
1153 if (result.frequency > 0 && result.frequency < 14)
1154 result.frequency = 2407 + (5 * result.frequency);
1155 else if (result.frequency == 14)
1156 result.frequency = 2484;
1158 strength = calculate_strength(task, &result);
1159 channel = calculate_channel(&result);
1161 frequency = (result.frequency < 0) ? 0 : result.frequency;
1163 if (result.has_rsn == TRUE)
1165 else if (result.has_wpa == TRUE)
1167 else if (result.has_wep == TRUE)
1172 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1174 group = build_group(result.path, result.name,
1175 result.ssid, result.ssid_len,
1178 network = connman_device_get_network(task->device, result.path);
1179 if (network == NULL) {
1182 network = connman_network_create(result.path,
1183 CONNMAN_NETWORK_TYPE_WIFI);
1184 if (network == NULL)
1187 index = connman_device_get_index(task->device);
1188 connman_network_set_index(network, index);
1190 connman_network_set_protocol(network,
1191 CONNMAN_NETWORK_PROTOCOL_IP);
1193 connman_network_set_address(network, result.addr,
1196 if (connman_device_add_network(task->device, network) < 0) {
1197 connman_network_unref(network);
1202 if (result.name != NULL && result.name[0] != '\0')
1203 connman_network_set_name(network, result.name);
1205 connman_network_set_blob(network, "WiFi.SSID",
1206 result.ssid, result.ssid_len);
1208 connman_network_set_string(network, "WiFi.Mode", mode);
1210 DBG("%s (%s %s) strength %d (%s)",
1211 result.name, mode, security, strength,
1212 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1214 connman_network_set_available(network, TRUE);
1215 connman_network_set_strength(network, strength);
1217 connman_network_set_uint16(network, "Frequency", frequency);
1218 connman_network_set_uint16(network, "WiFi.Channel", channel);
1219 connman_network_set_string(network, "WiFi.Security", security);
1221 connman_network_set_group(network, group);
1226 g_free(result.path);
1227 g_free(result.addr);
1228 g_free(result.name);
1229 g_free(result.ssid);
1231 dbus_message_unref(reply);
1233 get_properties(task);
1236 static void get_properties(struct supplicant_task *task)
1238 DBusMessage *message;
1239 DBusPendingCall *call;
1242 path = g_slist_nth_data(task->scan_results, 0);
1246 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1247 SUPPLICANT_INTF ".BSSID",
1250 task->scan_results = g_slist_remove(task->scan_results, path);
1253 if (message == NULL)
1256 if (dbus_connection_send_with_reply(connection, message,
1257 &call, TIMEOUT) == FALSE) {
1258 connman_error("Failed to get network properties");
1259 dbus_message_unref(message);
1264 connman_error("D-Bus connection not available");
1265 dbus_message_unref(message);
1269 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1271 dbus_message_unref(message);
1276 if (task->noscan == FALSE)
1277 connman_device_set_scanning(task->device, FALSE);
1280 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1282 struct supplicant_task *task = user_data;
1288 DBG("task %p", task);
1290 reply = dbus_pending_call_steal_reply(call);
1294 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1297 dbus_error_init(&error);
1299 if (dbus_message_get_args(reply, &error,
1300 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1301 &results, &num_results,
1302 DBUS_TYPE_INVALID) == FALSE) {
1303 if (dbus_error_is_set(&error) == TRUE) {
1304 connman_error("%s", error.message);
1305 dbus_error_free(&error);
1307 connman_error("Wrong arguments for scan result");
1311 if (num_results == 0)
1314 for (i = 0; i < num_results; i++) {
1315 char *path = g_strdup(results[i]);
1319 task->scan_results = g_slist_append(task->scan_results, path);
1322 g_strfreev(results);
1324 dbus_message_unref(reply);
1326 get_properties(task);
1331 dbus_message_unref(reply);
1334 if (task->noscan == FALSE)
1335 connman_device_set_scanning(task->device, FALSE);
1338 static void scan_results_available(struct supplicant_task *task)
1340 DBusMessage *message;
1341 DBusPendingCall *call;
1343 DBG("task %p", task);
1345 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1346 SUPPLICANT_INTF ".Interface",
1348 if (message == NULL)
1351 if (dbus_connection_send_with_reply(connection, message,
1352 &call, TIMEOUT) == FALSE) {
1353 connman_error("Failed to request scan result");
1357 if (task->noscan == FALSE)
1358 connman_device_set_scanning(task->device, TRUE);
1361 connman_error("D-Bus connection not available");
1365 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1368 dbus_message_unref(message);
1371 static enum supplicant_state string2state(const char *state)
1373 if (g_str_equal(state, "INACTIVE") == TRUE)
1374 return WPA_INACTIVE;
1375 else if (g_str_equal(state, "SCANNING") == TRUE)
1376 return WPA_SCANNING;
1377 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1378 return WPA_ASSOCIATING;
1379 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1380 return WPA_ASSOCIATED;
1381 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1382 return WPA_GROUP_HANDSHAKE;
1383 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1384 return WPA_4WAY_HANDSHAKE;
1385 else if (g_str_equal(state, "COMPLETED") == TRUE)
1386 return WPA_COMPLETED;
1387 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1388 return WPA_DISCONNECTED;
1393 static int task_connect(struct supplicant_task *task)
1395 const char *address, *security, *passphrase;
1397 unsigned int ssid_len;
1399 address = connman_network_get_string(task->network, "Address");
1400 security = connman_network_get_string(task->network, "WiFi.Security");
1401 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1403 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1405 DBG("address %s security %s passphrase %s",
1406 address, security, passphrase);
1408 if (security == NULL && passphrase == NULL)
1411 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1416 select_network(task);
1417 disable_network(task);
1419 set_network(task, ssid, ssid_len, address, security, passphrase);
1421 enable_network(task);
1426 static char *get_bssid(struct connman_device *device)
1429 unsigned char ioctl_bssid[ETH_ALEN];
1433 if (connman_device_get_type(device) != CONNMAN_DEVICE_TYPE_WIFI)
1436 fd = socket(PF_INET, SOCK_DGRAM, 0);
1440 memset(&wrq, 0, sizeof(wrq));
1441 strncpy(wrq.ifr_name, connman_device_get_interface(device), IFNAMSIZ);
1443 ret = ioctl(fd, SIOCGIWAP, &wrq);
1448 memcpy(ioctl_bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
1450 bssid = g_try_malloc0(13);
1454 snprintf(bssid, 13, "%02x%02x%02x%02x%02x%02x",
1455 ioctl_bssid[0], ioctl_bssid[1],
1456 ioctl_bssid[2], ioctl_bssid[3],
1457 ioctl_bssid[4], ioctl_bssid[5]);
1463 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1466 const char *newstate, *oldstate;
1467 enum supplicant_state state;
1469 dbus_error_init(&error);
1471 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1472 DBUS_TYPE_STRING, &oldstate,
1473 DBUS_TYPE_INVALID) == FALSE) {
1474 if (dbus_error_is_set(&error) == TRUE) {
1475 connman_error("%s", error.message);
1476 dbus_error_free(&error);
1478 connman_error("Wrong arguments for state change");
1482 DBG("state %s ==> %s", oldstate, newstate);
1484 state = string2state(newstate);
1485 if (state == WPA_INVALID)
1488 task->state = state;
1490 switch (task->state) {
1492 task->noscan = TRUE;
1493 connman_device_set_scanning(task->device, TRUE);
1495 case WPA_ASSOCIATING:
1496 case WPA_ASSOCIATED:
1497 case WPA_4WAY_HANDSHAKE:
1498 case WPA_GROUP_HANDSHAKE:
1499 task->noscan = TRUE;
1502 case WPA_DISCONNECTED:
1503 task->noscan = FALSE;
1506 task->noscan = FALSE;
1507 connman_device_set_scanning(task->device, FALSE);
1513 if (task->network == NULL)
1516 switch (task->state) {
1519 if (connman_network_get_group(task->network) == NULL) {
1520 const char *name, *mode, *security;
1521 char *group, *bssid;
1524 * This is a hidden network, we need to set its
1525 * group based on the BSSID we just joined.
1527 bssid = get_bssid(task->device);
1529 name = connman_network_get_string(task->network,
1531 mode = connman_network_get_string(task->network,
1533 security = connman_network_get_string(task->network,
1536 if (bssid && name && mode && security) {
1537 group = build_group(bssid, name, NULL, 0,
1539 connman_network_set_group(task->network, group);
1545 connman_network_set_connected(task->network, TRUE);
1546 connman_device_set_scanning(task->device, FALSE);
1548 case WPA_DISCONNECTED:
1549 if (task->disconnecting == TRUE) {
1550 connman_network_set_connected(task->network, FALSE);
1551 connman_network_unref(task->network);
1552 task->disconnecting = FALSE;
1554 if (task->pending_network != NULL) {
1555 task->network = task->pending_network;
1556 task->pending_network = NULL;
1561 connman_network_set_connected(task->network, FALSE);
1562 connman_device_set_scanning(task->device, FALSE);
1565 case WPA_ASSOCIATING:
1566 connman_network_set_associating(task->network, TRUE);
1569 connman_network_set_associating(task->network, FALSE);
1574 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1575 DBusMessage *msg, void *data)
1577 struct supplicant_task *task;
1578 const char *member, *path;
1580 if (dbus_message_has_interface(msg,
1581 SUPPLICANT_INTF ".Interface") == FALSE)
1582 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1584 member = dbus_message_get_member(msg);
1586 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1588 path = dbus_message_get_path(msg);
1590 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1592 task = find_task_by_path(path);
1594 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1596 DBG("task %p member %s", task, member);
1598 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1599 scan_results_available(task);
1600 else if (g_str_equal(member, "StateChange") == TRUE)
1601 state_change(task, msg);
1603 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1606 static int supplicant_get_range(struct supplicant_task *task)
1611 fd = socket(PF_INET, SOCK_DGRAM, 0);
1615 memset(&wrq, 0, sizeof(struct iwreq));
1616 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
1617 wrq.u.data.pointer = task->range;
1618 wrq.u.data.length = sizeof(struct iw_range);
1620 err = ioctl(fd, SIOCGIWRANGE, &wrq);
1627 int supplicant_start(struct connman_device *device)
1629 struct supplicant_task *task;
1632 DBG("device %p", device);
1634 task = g_try_new0(struct supplicant_task, 1);
1638 task->ifindex = connman_device_get_index(device);
1639 task->ifname = connman_inet_ifname(task->ifindex);
1641 if (task->ifname == NULL) {
1646 task->range = g_try_malloc0(sizeof(struct iw_range));
1647 if (task->range == NULL) {
1652 err = supplicant_get_range(task);
1656 task->device = connman_device_ref(device);
1658 task->created = FALSE;
1659 task->noscan = FALSE;
1660 task->state = WPA_INVALID;
1661 task->disconnecting = FALSE;
1662 task->pending_network = NULL;
1664 task_list = g_slist_append(task_list, task);
1666 return create_interface(task);
1669 g_free(task->range);
1670 g_free(task->ifname);
1676 int supplicant_stop(struct connman_device *device)
1678 int index = connman_device_get_index(device);
1679 struct supplicant_task *task;
1681 DBG("device %p", device);
1683 task = find_task_by_index(index);
1687 g_free(task->range);
1689 task_list = g_slist_remove(task_list, task);
1691 disable_network(task);
1693 remove_network(task);
1695 return remove_interface(task);
1698 int supplicant_scan(struct connman_device *device)
1700 int index = connman_device_get_index(device);
1701 struct supplicant_task *task;
1704 DBG("device %p", device);
1706 task = find_task_by_index(index);
1710 switch (task->state) {
1713 case WPA_ASSOCIATING:
1714 case WPA_ASSOCIATED:
1715 case WPA_4WAY_HANDSHAKE:
1716 case WPA_GROUP_HANDSHAKE:
1722 err = initiate_scan(task);
1727 int supplicant_connect(struct connman_network *network)
1729 struct supplicant_task *task;
1732 DBG("network %p", network);
1734 index = connman_network_get_index(network);
1736 task = find_task_by_index(index);
1740 if (task->disconnecting == TRUE)
1741 task->pending_network = connman_network_ref(network);
1743 task->network = connman_network_ref(network);
1744 return task_connect(task);
1750 int supplicant_disconnect(struct connman_network *network)
1752 struct supplicant_task *task;
1755 DBG("network %p", network);
1757 index = connman_network_get_index(network);
1759 task = find_task_by_index(index);
1763 if (task->disconnecting == TRUE)
1764 return -EINPROGRESS;
1766 disable_network(task);
1768 remove_network(task);
1770 task->disconnecting = TRUE;
1775 static void supplicant_activate(DBusConnection *conn)
1777 DBusMessage *message;
1779 DBG("conn %p", conn);
1781 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1782 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1783 if (message == NULL)
1786 dbus_message_set_no_reply(message, TRUE);
1788 dbus_connection_send(conn, message, NULL);
1790 dbus_message_unref(message);
1793 static GSList *driver_list = NULL;
1795 static void supplicant_probe(DBusConnection *conn, void *user_data)
1799 DBG("conn %p", conn);
1801 for (list = driver_list; list; list = list->next) {
1802 struct supplicant_driver *driver = list->data;
1804 DBG("driver %p name %s", driver, driver->name);
1811 static void supplicant_remove(DBusConnection *conn, void *user_data)
1815 DBG("conn %p", conn);
1817 for (list = driver_list; list; list = list->next) {
1818 struct supplicant_driver *driver = list->data;
1820 DBG("driver %p name %s", driver, driver->name);
1827 static const char *supplicant_rule = "type=signal,"
1828 "interface=" SUPPLICANT_INTF ".Interface";
1831 static int supplicant_create(void)
1833 if (g_slist_length(driver_list) > 0)
1836 connection = connman_dbus_get_connection();
1837 if (connection == NULL)
1840 DBG("connection %p", connection);
1842 if (dbus_connection_add_filter(connection,
1843 supplicant_filter, NULL, NULL) == FALSE) {
1844 connection = connman_dbus_get_connection();
1848 dbus_bus_add_match(connection, supplicant_rule, NULL);
1849 dbus_connection_flush(connection);
1851 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1852 supplicant_probe, supplicant_remove, NULL, NULL);
1857 static void supplicant_destroy(void)
1859 if (g_slist_length(driver_list) > 0)
1862 DBG("connection %p", connection);
1865 g_dbus_remove_watch(connection, watch);
1867 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1868 dbus_connection_flush(connection);
1870 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1872 dbus_connection_unref(connection);
1876 int supplicant_register(struct supplicant_driver *driver)
1880 DBG("driver %p name %s", driver, driver->name);
1882 err = supplicant_create();
1886 driver_list = g_slist_append(driver_list, driver);
1888 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1889 supplicant_probe(connection, NULL);
1891 supplicant_activate(connection);
1896 void supplicant_unregister(struct supplicant_driver *driver)
1898 DBG("driver %p name %s", driver, driver->name);
1900 supplicant_remove(connection, NULL);
1902 driver_list = g_slist_remove(driver_list, driver);
1904 supplicant_destroy();