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;
718 dbus_uint32_t scan_ssid = 1;
720 DBG("task %p", task);
722 if (task->netpath == NULL)
725 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
726 SUPPLICANT_INTF ".Network", "set");
730 dbus_message_iter_init_append(message, &array);
732 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
733 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
734 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
735 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
737 connman_dbus_dict_append_variant(&dict, "scan_ssid",
738 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;
1068 unsigned int ssid_len;
1070 DBG("task %p", task);
1072 reply = dbus_pending_call_steal_reply(call);
1073 if (reply == NULL) {
1074 get_properties(task);
1078 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1079 dbus_message_unref(reply);
1080 get_properties(task);
1084 memset(&result, 0, sizeof(result));
1085 result.frequency = -1;
1086 result.quality = -1;
1090 dbus_message_iter_init(reply, &array);
1092 dbus_message_iter_recurse(&array, &dict);
1094 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1095 DBusMessageIter entry, value;
1098 dbus_message_iter_recurse(&dict, &entry);
1099 dbus_message_iter_get_basic(&entry, &key);
1101 dbus_message_iter_next(&entry);
1103 dbus_message_iter_recurse(&entry, &value);
1105 //type = dbus_message_iter_get_arg_type(&value);
1106 //dbus_message_iter_get_basic(&value, &val);
1114 * frequency : i (105)
1115 * capabilities : q (113)
1122 if (g_str_equal(key, "bssid") == TRUE)
1123 extract_addr(&value, &result);
1124 else if (g_str_equal(key, "ssid") == TRUE)
1125 extract_ssid(&value, &result);
1126 else if (g_str_equal(key, "wpaie") == TRUE)
1127 extract_wpaie(&value, &result);
1128 else if (g_str_equal(key, "rsnie") == TRUE)
1129 extract_rsnie(&value, &result);
1130 else if (g_str_equal(key, "wpsie") == TRUE)
1131 extract_wpsie(&value, &result);
1132 else if (g_str_equal(key, "capabilities") == TRUE)
1133 extract_capabilites(&value, &result);
1134 else if (g_str_equal(key, "frequency") == TRUE)
1135 dbus_message_iter_get_basic(&value, &result.frequency);
1136 else if (g_str_equal(key, "quality") == TRUE)
1137 dbus_message_iter_get_basic(&value, &result.quality);
1138 else if (g_str_equal(key, "noise") == TRUE)
1139 dbus_message_iter_get_basic(&value, &result.noise);
1140 else if (g_str_equal(key, "level") == TRUE)
1141 dbus_message_iter_get_basic(&value, &result.level);
1142 else if (g_str_equal(key, "maxrate") == TRUE)
1143 dbus_message_iter_get_basic(&value, &result.maxrate);
1145 dbus_message_iter_next(&dict);
1148 if (result.path == NULL)
1151 if (result.path[0] == '\0')
1154 if (result.frequency > 0 && result.frequency < 14)
1155 result.frequency = 2407 + (5 * result.frequency);
1156 else if (result.frequency == 14)
1157 result.frequency = 2484;
1159 strength = calculate_strength(task, &result);
1160 channel = calculate_channel(&result);
1162 frequency = (result.frequency < 0) ? 0 : result.frequency;
1164 if (result.has_rsn == TRUE)
1166 else if (result.has_wpa == TRUE)
1168 else if (result.has_wep == TRUE)
1173 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1175 group = build_group(result.path, result.name,
1176 result.ssid, result.ssid_len,
1179 network = connman_device_get_network(task->device, result.path);
1180 if (network == NULL) {
1183 network = connman_network_create(result.path,
1184 CONNMAN_NETWORK_TYPE_WIFI);
1185 if (network == NULL)
1188 index = connman_device_get_index(task->device);
1189 connman_network_set_index(network, index);
1191 connman_network_set_protocol(network,
1192 CONNMAN_NETWORK_PROTOCOL_IP);
1194 connman_network_set_address(network, result.addr,
1197 if (connman_device_add_network(task->device, network) < 0) {
1198 connman_network_unref(network);
1203 if (result.name != NULL && result.name[0] != '\0')
1204 connman_network_set_name(network, result.name);
1206 if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1207 connman_network_set_blob(network, "WiFi.SSID",
1208 result.ssid, result.ssid_len);
1211 connman_network_set_string(network, "WiFi.Mode", mode);
1213 DBG("%s (%s %s) strength %d (%s)",
1214 result.name, mode, security, strength,
1215 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1217 connman_network_set_available(network, TRUE);
1218 connman_network_set_strength(network, strength);
1220 connman_network_set_uint16(network, "Frequency", frequency);
1221 connman_network_set_uint16(network, "WiFi.Channel", channel);
1222 connman_network_set_string(network, "WiFi.Security", security);
1224 connman_network_set_group(network, group);
1229 g_free(result.path);
1230 g_free(result.addr);
1231 g_free(result.name);
1232 g_free(result.ssid);
1234 dbus_message_unref(reply);
1236 get_properties(task);
1239 static void get_properties(struct supplicant_task *task)
1241 DBusMessage *message;
1242 DBusPendingCall *call;
1245 path = g_slist_nth_data(task->scan_results, 0);
1249 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1250 SUPPLICANT_INTF ".BSSID",
1253 task->scan_results = g_slist_remove(task->scan_results, path);
1256 if (message == NULL)
1259 if (dbus_connection_send_with_reply(connection, message,
1260 &call, TIMEOUT) == FALSE) {
1261 connman_error("Failed to get network properties");
1262 dbus_message_unref(message);
1267 connman_error("D-Bus connection not available");
1268 dbus_message_unref(message);
1272 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1274 dbus_message_unref(message);
1279 if (task->noscan == FALSE)
1280 connman_device_set_scanning(task->device, FALSE);
1283 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1285 struct supplicant_task *task = user_data;
1291 DBG("task %p", task);
1293 reply = dbus_pending_call_steal_reply(call);
1297 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1300 dbus_error_init(&error);
1302 if (dbus_message_get_args(reply, &error,
1303 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1304 &results, &num_results,
1305 DBUS_TYPE_INVALID) == FALSE) {
1306 if (dbus_error_is_set(&error) == TRUE) {
1307 connman_error("%s", error.message);
1308 dbus_error_free(&error);
1310 connman_error("Wrong arguments for scan result");
1314 if (num_results == 0)
1317 for (i = 0; i < num_results; i++) {
1318 char *path = g_strdup(results[i]);
1322 task->scan_results = g_slist_append(task->scan_results, path);
1325 g_strfreev(results);
1327 dbus_message_unref(reply);
1329 get_properties(task);
1334 dbus_message_unref(reply);
1337 if (task->noscan == FALSE)
1338 connman_device_set_scanning(task->device, FALSE);
1341 static void scan_results_available(struct supplicant_task *task)
1343 DBusMessage *message;
1344 DBusPendingCall *call;
1346 DBG("task %p", task);
1348 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1349 SUPPLICANT_INTF ".Interface",
1351 if (message == NULL)
1354 if (dbus_connection_send_with_reply(connection, message,
1355 &call, TIMEOUT) == FALSE) {
1356 connman_error("Failed to request scan result");
1360 if (task->noscan == FALSE)
1361 connman_device_set_scanning(task->device, TRUE);
1364 connman_error("D-Bus connection not available");
1368 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1371 dbus_message_unref(message);
1374 static enum supplicant_state string2state(const char *state)
1376 if (g_str_equal(state, "INACTIVE") == TRUE)
1377 return WPA_INACTIVE;
1378 else if (g_str_equal(state, "SCANNING") == TRUE)
1379 return WPA_SCANNING;
1380 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1381 return WPA_ASSOCIATING;
1382 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1383 return WPA_ASSOCIATED;
1384 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1385 return WPA_GROUP_HANDSHAKE;
1386 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1387 return WPA_4WAY_HANDSHAKE;
1388 else if (g_str_equal(state, "COMPLETED") == TRUE)
1389 return WPA_COMPLETED;
1390 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1391 return WPA_DISCONNECTED;
1396 static int task_connect(struct supplicant_task *task)
1398 const char *address, *security, *passphrase;
1400 unsigned int ssid_len;
1402 address = connman_network_get_string(task->network, "Address");
1403 security = connman_network_get_string(task->network, "WiFi.Security");
1404 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1406 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1408 DBG("address %s security %s passphrase %s",
1409 address, security, passphrase);
1411 if (security == NULL && passphrase == NULL)
1414 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1419 select_network(task);
1420 disable_network(task);
1422 set_network(task, ssid, ssid_len, address, security, passphrase);
1424 enable_network(task);
1429 static char *get_bssid(struct connman_device *device)
1432 unsigned char ioctl_bssid[ETH_ALEN];
1436 if (connman_device_get_type(device) != CONNMAN_DEVICE_TYPE_WIFI)
1439 fd = socket(PF_INET, SOCK_DGRAM, 0);
1443 memset(&wrq, 0, sizeof(wrq));
1444 strncpy(wrq.ifr_name, connman_device_get_interface(device), IFNAMSIZ);
1446 ret = ioctl(fd, SIOCGIWAP, &wrq);
1451 memcpy(ioctl_bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
1453 bssid = g_try_malloc0(13);
1457 snprintf(bssid, 13, "%02x%02x%02x%02x%02x%02x",
1458 ioctl_bssid[0], ioctl_bssid[1],
1459 ioctl_bssid[2], ioctl_bssid[3],
1460 ioctl_bssid[4], ioctl_bssid[5]);
1466 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1469 const char *newstate, *oldstate;
1470 enum supplicant_state state;
1472 dbus_error_init(&error);
1474 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1475 DBUS_TYPE_STRING, &oldstate,
1476 DBUS_TYPE_INVALID) == FALSE) {
1477 if (dbus_error_is_set(&error) == TRUE) {
1478 connman_error("%s", error.message);
1479 dbus_error_free(&error);
1481 connman_error("Wrong arguments for state change");
1485 DBG("state %s ==> %s", oldstate, newstate);
1487 state = string2state(newstate);
1488 if (state == WPA_INVALID)
1491 task->state = state;
1493 switch (task->state) {
1495 task->noscan = TRUE;
1496 connman_device_set_scanning(task->device, TRUE);
1498 case WPA_ASSOCIATING:
1499 case WPA_ASSOCIATED:
1500 case WPA_4WAY_HANDSHAKE:
1501 case WPA_GROUP_HANDSHAKE:
1502 task->noscan = TRUE;
1505 case WPA_DISCONNECTED:
1506 task->noscan = FALSE;
1509 task->noscan = FALSE;
1510 connman_device_set_scanning(task->device, FALSE);
1516 if (task->network == NULL)
1519 switch (task->state) {
1522 if (connman_network_get_group(task->network) == NULL) {
1523 const char *name, *mode, *security;
1524 char *group, *bssid;
1527 * This is a hidden network, we need to set its
1528 * group based on the BSSID we just joined.
1530 bssid = get_bssid(task->device);
1532 name = connman_network_get_string(task->network,
1534 mode = connman_network_get_string(task->network,
1536 security = connman_network_get_string(task->network,
1539 if (bssid && name && mode && security) {
1540 group = build_group(bssid, name, NULL, 0,
1542 connman_network_set_group(task->network, group);
1548 connman_network_set_connected(task->network, TRUE);
1549 connman_device_set_scanning(task->device, FALSE);
1551 case WPA_DISCONNECTED:
1552 if (task->disconnecting == TRUE) {
1553 connman_network_set_connected(task->network, FALSE);
1554 connman_network_unref(task->network);
1555 task->disconnecting = FALSE;
1557 if (task->pending_network != NULL) {
1558 task->network = task->pending_network;
1559 task->pending_network = NULL;
1564 connman_network_set_connected(task->network, FALSE);
1565 connman_device_set_scanning(task->device, FALSE);
1568 case WPA_ASSOCIATING:
1569 connman_network_set_associating(task->network, TRUE);
1572 connman_network_set_associating(task->network, FALSE);
1577 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1578 DBusMessage *msg, void *data)
1580 struct supplicant_task *task;
1581 const char *member, *path;
1583 if (dbus_message_has_interface(msg,
1584 SUPPLICANT_INTF ".Interface") == FALSE)
1585 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1587 member = dbus_message_get_member(msg);
1589 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1591 path = dbus_message_get_path(msg);
1593 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1595 task = find_task_by_path(path);
1597 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1599 DBG("task %p member %s", task, member);
1601 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1602 scan_results_available(task);
1603 else if (g_str_equal(member, "StateChange") == TRUE)
1604 state_change(task, msg);
1606 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1609 static int supplicant_get_range(struct supplicant_task *task)
1614 fd = socket(PF_INET, SOCK_DGRAM, 0);
1618 memset(&wrq, 0, sizeof(struct iwreq));
1619 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
1620 wrq.u.data.pointer = task->range;
1621 wrq.u.data.length = sizeof(struct iw_range);
1623 err = ioctl(fd, SIOCGIWRANGE, &wrq);
1630 int supplicant_start(struct connman_device *device)
1632 struct supplicant_task *task;
1635 DBG("device %p", device);
1637 task = g_try_new0(struct supplicant_task, 1);
1641 task->ifindex = connman_device_get_index(device);
1642 task->ifname = connman_inet_ifname(task->ifindex);
1644 if (task->ifname == NULL) {
1649 task->range = g_try_malloc0(sizeof(struct iw_range));
1650 if (task->range == NULL) {
1655 err = supplicant_get_range(task);
1659 task->device = connman_device_ref(device);
1661 task->created = FALSE;
1662 task->noscan = FALSE;
1663 task->state = WPA_INVALID;
1664 task->disconnecting = FALSE;
1665 task->pending_network = NULL;
1667 task_list = g_slist_append(task_list, task);
1669 return create_interface(task);
1672 g_free(task->range);
1673 g_free(task->ifname);
1679 int supplicant_stop(struct connman_device *device)
1681 int index = connman_device_get_index(device);
1682 struct supplicant_task *task;
1684 DBG("device %p", device);
1686 task = find_task_by_index(index);
1690 g_free(task->range);
1692 task_list = g_slist_remove(task_list, task);
1694 disable_network(task);
1696 remove_network(task);
1698 return remove_interface(task);
1701 int supplicant_scan(struct connman_device *device)
1703 int index = connman_device_get_index(device);
1704 struct supplicant_task *task;
1707 DBG("device %p", device);
1709 task = find_task_by_index(index);
1713 switch (task->state) {
1716 case WPA_ASSOCIATING:
1717 case WPA_ASSOCIATED:
1718 case WPA_4WAY_HANDSHAKE:
1719 case WPA_GROUP_HANDSHAKE:
1725 err = initiate_scan(task);
1730 int supplicant_connect(struct connman_network *network)
1732 struct supplicant_task *task;
1735 DBG("network %p", network);
1737 index = connman_network_get_index(network);
1739 task = find_task_by_index(index);
1743 if (task->disconnecting == TRUE)
1744 task->pending_network = connman_network_ref(network);
1746 task->network = connman_network_ref(network);
1747 return task_connect(task);
1753 int supplicant_disconnect(struct connman_network *network)
1755 struct supplicant_task *task;
1758 DBG("network %p", network);
1760 index = connman_network_get_index(network);
1762 task = find_task_by_index(index);
1766 if (task->disconnecting == TRUE)
1767 return -EINPROGRESS;
1769 disable_network(task);
1771 remove_network(task);
1773 task->disconnecting = TRUE;
1778 static void supplicant_activate(DBusConnection *conn)
1780 DBusMessage *message;
1782 DBG("conn %p", conn);
1784 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1785 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1786 if (message == NULL)
1789 dbus_message_set_no_reply(message, TRUE);
1791 dbus_connection_send(conn, message, NULL);
1793 dbus_message_unref(message);
1796 static GSList *driver_list = NULL;
1798 static void supplicant_probe(DBusConnection *conn, void *user_data)
1802 DBG("conn %p", conn);
1804 for (list = driver_list; list; list = list->next) {
1805 struct supplicant_driver *driver = list->data;
1807 DBG("driver %p name %s", driver, driver->name);
1814 static void supplicant_remove(DBusConnection *conn, void *user_data)
1818 DBG("conn %p", conn);
1820 for (list = driver_list; list; list = list->next) {
1821 struct supplicant_driver *driver = list->data;
1823 DBG("driver %p name %s", driver, driver->name);
1830 static const char *supplicant_rule = "type=signal,"
1831 "interface=" SUPPLICANT_INTF ".Interface";
1834 static int supplicant_create(void)
1836 if (g_slist_length(driver_list) > 0)
1839 connection = connman_dbus_get_connection();
1840 if (connection == NULL)
1843 DBG("connection %p", connection);
1845 if (dbus_connection_add_filter(connection,
1846 supplicant_filter, NULL, NULL) == FALSE) {
1847 connection = connman_dbus_get_connection();
1851 dbus_bus_add_match(connection, supplicant_rule, NULL);
1852 dbus_connection_flush(connection);
1854 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1855 supplicant_probe, supplicant_remove, NULL, NULL);
1860 static void supplicant_destroy(void)
1862 if (g_slist_length(driver_list) > 0)
1865 DBG("connection %p", connection);
1868 g_dbus_remove_watch(connection, watch);
1870 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1871 dbus_connection_flush(connection);
1873 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1875 dbus_connection_unref(connection);
1879 int supplicant_register(struct supplicant_driver *driver)
1883 DBG("driver %p name %s", driver, driver->name);
1885 err = supplicant_create();
1889 driver_list = g_slist_append(driver_list, driver);
1891 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1892 supplicant_probe(connection, NULL);
1894 supplicant_activate(connection);
1899 void supplicant_unregister(struct supplicant_driver *driver)
1901 DBG("driver %p name %s", driver, driver->name);
1903 supplicant_remove(connection, NULL);
1905 driver_list = g_slist_remove(driver_list, driver);
1907 supplicant_destroy();