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
30 #include <net/ethernet.h>
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/device.h>
36 #include <connman/dbus.h>
37 #include <connman/log.h>
40 #include "supplicant.h"
44 #define IEEE80211_CAP_ESS 0x0001
45 #define IEEE80211_CAP_IBSS 0x0002
46 #define IEEE80211_CAP_PRIVACY 0x0010
48 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
49 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
50 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
52 /* Taken from "WPA Supplicant - Common definitions" */
53 enum supplicant_state {
55 * WPA_DISCONNECTED - Disconnected state
57 * This state indicates that client is not associated, but is likely to
58 * start looking for an access point. This state is entered when a
64 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
66 * This state is entered if there are no enabled networks in the
67 * configuration. wpa_supplicant is not trying to associate with a new
68 * network and external interaction (e.g., ctrl_iface call to add or
69 * enable a network) is needed to start association.
74 * WPA_SCANNING - Scanning for a network
76 * This state is entered when wpa_supplicant starts scanning for a
82 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
84 * This state is entered when wpa_supplicant has found a suitable BSS
85 * to associate with and the driver is configured to try to associate
86 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
87 * state is entered when the driver is configured to try to associate
88 * with a network using the configured SSID and security policy.
93 * WPA_ASSOCIATED - Association completed
95 * This state is entered when the driver reports that association has
96 * been successfully completed with an AP. If IEEE 802.1X is used
97 * (with or without WPA/WPA2), wpa_supplicant remains in this state
98 * until the IEEE 802.1X/EAPOL authentication has been completed.
103 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
105 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
106 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
107 * frame after association. In case of WPA-EAP, this state is entered
108 * when the IEEE 802.1X/EAPOL authentication has been completed.
113 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
115 * This state is entered when 4-Way Key Handshake has been completed
116 * (i.e., when the supplicant sends out message 4/4) and when Group
117 * Key rekeying is started by the AP (i.e., when supplicant receives
123 * WPA_COMPLETED - All authentication completed
125 * This state is entered when the full authentication process is
126 * completed. In case of WPA2, this happens when the 4-Way Handshake is
127 * successfully completed. With WPA, this state is entered after the
128 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
129 * completed after dynamic keys are received (or if not used, after
130 * the EAP authentication has been completed). With static WEP keys and
131 * plaintext connections, this state is entered when an association
132 * has been completed.
134 * This state indicates that the supplicant has completed its
135 * processing for the association phase and that data connection is
141 * WPA_INVALID - Invalid state (parsing error)
143 * This state is returned if the string input is invalid. It is not
144 * an official wpa_supplicant state.
149 struct supplicant_result {
154 unsigned int ssid_len;
155 dbus_uint16_t capabilities;
161 dbus_int32_t frequency;
162 dbus_int32_t quality;
165 dbus_int32_t maxrate;
168 struct supplicant_task {
171 struct connman_device *device;
172 struct connman_network *network;
176 enum supplicant_state state;
178 GSList *scan_results;
181 static GSList *task_list = NULL;
183 static DBusConnection *connection;
185 static void free_task(struct supplicant_task *task)
187 DBG("task %p", task);
189 g_free(task->ifname);
194 static struct supplicant_task *find_task_by_index(int index)
198 for (list = task_list; list; list = list->next) {
199 struct supplicant_task *task = list->data;
201 if (task->ifindex == index)
208 static struct supplicant_task *find_task_by_path(const char *path)
212 for (list = task_list; list; list = list->next) {
213 struct supplicant_task *task = list->data;
215 if (g_str_equal(task->path, path) == TRUE)
222 static void add_interface_reply(DBusPendingCall *call, void *user_data)
224 struct supplicant_task *task = user_data;
229 DBG("task %p", task);
231 reply = dbus_pending_call_steal_reply(call);
235 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
238 dbus_error_init(&error);
240 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
241 DBUS_TYPE_INVALID) == FALSE) {
242 if (dbus_error_is_set(&error) == TRUE) {
243 connman_error("%s", error.message);
244 dbus_error_free(&error);
246 connman_error("Wrong arguments for add interface");
250 DBG("path %s", path);
252 task->path = g_strdup(path);
253 task->created = TRUE;
255 connman_device_set_powered(task->device, TRUE);
258 dbus_message_unref(reply);
261 static int add_interface(struct supplicant_task *task)
263 DBusMessage *message;
264 DBusPendingCall *call;
266 DBG("task %p", task);
268 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
269 SUPPLICANT_INTF, "addInterface");
273 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
276 if (dbus_connection_send_with_reply(connection, message,
277 &call, TIMEOUT) == FALSE) {
278 connman_error("Failed to add interface");
279 dbus_message_unref(message);
284 connman_error("D-Bus connection not available");
285 dbus_message_unref(message);
289 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
291 dbus_message_unref(message);
296 static void get_interface_reply(DBusPendingCall *call, void *user_data)
298 struct supplicant_task *task = user_data;
303 DBG("task %p", task);
305 reply = dbus_pending_call_steal_reply(call);
309 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
314 dbus_error_init(&error);
316 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
317 DBUS_TYPE_INVALID) == FALSE) {
318 if (dbus_error_is_set(&error) == TRUE) {
319 connman_error("%s", error.message);
320 dbus_error_free(&error);
322 connman_error("Wrong arguments for get interface");
326 DBG("path %s", path);
328 task->path = g_strdup(path);
329 task->created = FALSE;
331 connman_device_set_powered(task->device, TRUE);
334 dbus_message_unref(reply);
337 static int create_interface(struct supplicant_task *task)
339 DBusMessage *message;
340 DBusPendingCall *call;
342 DBG("task %p", task);
344 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
345 SUPPLICANT_INTF, "getInterface");
349 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
352 if (dbus_connection_send_with_reply(connection, message,
353 &call, TIMEOUT) == FALSE) {
354 connman_error("Failed to get interface");
355 dbus_message_unref(message);
360 connman_error("D-Bus connection not available");
361 dbus_message_unref(message);
365 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
367 dbus_message_unref(message);
372 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
374 struct supplicant_task *task = user_data;
377 DBG("task %p", task);
379 reply = dbus_pending_call_steal_reply(call);
381 connman_device_set_powered(task->device, FALSE);
383 connman_device_unref(task->device);
385 inet_ifdown(task->ifindex);
389 dbus_message_unref(reply);
392 static int remove_interface(struct supplicant_task *task)
394 DBusMessage *message;
395 DBusPendingCall *call;
397 DBG("task %p", task);
399 if (task->created == FALSE) {
400 connman_device_set_powered(task->device, FALSE);
404 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
405 SUPPLICANT_INTF, "removeInterface");
409 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
412 if (dbus_connection_send_with_reply(connection, message,
413 &call, TIMEOUT) == FALSE) {
414 connman_error("Failed to remove interface");
415 dbus_message_unref(message);
420 connman_error("D-Bus connection not available");
421 dbus_message_unref(message);
425 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
427 dbus_message_unref(message);
433 static int set_ap_scan(struct supplicant_task *task)
435 DBusMessage *message, *reply;
439 DBG("task %p", task);
441 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
442 SUPPLICANT_INTF ".Interface", "setAPScan");
446 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
449 dbus_error_init(&error);
451 reply = dbus_connection_send_with_reply_and_block(connection,
452 message, -1, &error);
454 if (dbus_error_is_set(&error) == TRUE) {
455 connman_error("%s", error.message);
456 dbus_error_free(&error);
458 connman_error("Failed to set AP scan");
459 dbus_message_unref(message);
463 dbus_message_unref(message);
465 dbus_message_unref(reply);
471 static int add_network(struct supplicant_task *task)
473 DBusMessage *message, *reply;
477 DBG("task %p", task);
479 if (task->netpath != NULL)
482 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
483 SUPPLICANT_INTF ".Interface", "addNetwork");
487 dbus_error_init(&error);
489 reply = dbus_connection_send_with_reply_and_block(connection,
490 message, -1, &error);
492 if (dbus_error_is_set(&error) == TRUE) {
493 connman_error("%s", error.message);
494 dbus_error_free(&error);
496 connman_error("Failed to add network");
497 dbus_message_unref(message);
501 dbus_message_unref(message);
503 dbus_error_init(&error);
505 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
506 DBUS_TYPE_INVALID) == FALSE) {
507 if (dbus_error_is_set(&error) == TRUE) {
508 connman_error("%s", error.message);
509 dbus_error_free(&error);
511 connman_error("Wrong arguments for network");
512 dbus_message_unref(reply);
516 DBG("path %s", path);
518 task->netpath = g_strdup(path);
520 dbus_message_unref(reply);
525 static int remove_network(struct supplicant_task *task)
527 DBusMessage *message, *reply;
530 DBG("task %p", task);
532 if (task->netpath == NULL)
535 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
536 SUPPLICANT_INTF ".Interface", "removeNetwork");
540 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
543 dbus_error_init(&error);
545 reply = dbus_connection_send_with_reply_and_block(connection,
546 message, -1, &error);
548 if (dbus_error_is_set(&error) == TRUE) {
549 connman_error("%s", error.message);
550 dbus_error_free(&error);
552 connman_error("Failed to remove network");
553 dbus_message_unref(message);
557 dbus_message_unref(message);
559 dbus_message_unref(reply);
561 g_free(task->netpath);
562 task->netpath = NULL;
567 static int select_network(struct supplicant_task *task)
569 DBusMessage *message, *reply;
572 DBG("task %p", task);
574 if (task->netpath == NULL)
577 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
578 SUPPLICANT_INTF ".Interface", "selectNetwork");
582 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
585 dbus_error_init(&error);
587 reply = dbus_connection_send_with_reply_and_block(connection,
588 message, -1, &error);
590 if (dbus_error_is_set(&error) == TRUE) {
591 connman_error("%s", error.message);
592 dbus_error_free(&error);
594 connman_error("Failed to select network");
595 dbus_message_unref(message);
599 dbus_message_unref(message);
601 dbus_message_unref(reply);
606 static int enable_network(struct supplicant_task *task)
608 DBusMessage *message, *reply;
611 DBG("task %p", task);
613 if (task->netpath == NULL)
616 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
617 SUPPLICANT_INTF ".Network", "enable");
621 dbus_error_init(&error);
623 reply = dbus_connection_send_with_reply_and_block(connection,
624 message, -1, &error);
626 if (dbus_error_is_set(&error) == TRUE) {
627 connman_error("%s", error.message);
628 dbus_error_free(&error);
630 connman_error("Failed to enable network");
631 dbus_message_unref(message);
635 dbus_message_unref(message);
637 dbus_message_unref(reply);
642 static int disable_network(struct supplicant_task *task)
644 DBusMessage *message, *reply;
647 DBG("task %p", task);
649 if (task->netpath == NULL)
652 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
653 SUPPLICANT_INTF ".Network", "disable");
657 dbus_error_init(&error);
659 reply = dbus_connection_send_with_reply_and_block(connection,
660 message, -1, &error);
662 if (dbus_error_is_set(&error) == TRUE) {
663 connman_error("%s", error.message);
664 dbus_error_free(&error);
666 connman_error("Failed to disable network");
667 dbus_message_unref(message);
671 dbus_message_unref(message);
673 dbus_message_unref(reply);
678 static int set_network(struct supplicant_task *task,
679 const unsigned char *network, int len,
680 const char *address, const char *security,
681 const char *passphrase)
683 DBusMessage *message, *reply;
684 DBusMessageIter array, dict;
687 DBG("task %p", task);
689 if (task->netpath == NULL)
692 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
693 SUPPLICANT_INTF ".Network", "set");
697 dbus_message_iter_init_append(message, &array);
699 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
700 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
701 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
702 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
704 if (address == NULL) {
705 dbus_uint32_t scan_ssid = 1;
706 connman_dbus_dict_append_variant(&dict, "scan_ssid",
707 DBUS_TYPE_UINT32, &scan_ssid);
709 connman_dbus_dict_append_variant(&dict, "bssid",
710 DBUS_TYPE_STRING, &address);
712 connman_dbus_dict_append_array(&dict, "ssid",
713 DBUS_TYPE_BYTE, &network, len);
715 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
716 g_ascii_strcasecmp(security, "rsn") == 0) {
717 const char *key_mgmt = "WPA-PSK";
718 connman_dbus_dict_append_variant(&dict, "key_mgmt",
719 DBUS_TYPE_STRING, &key_mgmt);
721 if (passphrase && strlen(passphrase) > 0)
722 connman_dbus_dict_append_variant(&dict, "psk",
723 DBUS_TYPE_STRING, &passphrase);
724 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
725 const char *key_mgmt = "NONE", *index = "0";
726 connman_dbus_dict_append_variant(&dict, "key_mgmt",
727 DBUS_TYPE_STRING, &key_mgmt);
730 int size = strlen(passphrase);
731 if (size == 10 || size == 26) {
732 unsigned char *key = malloc(13);
735 memset(tmp, 0, sizeof(tmp));
738 for (i = 0; i < size / 2; i++) {
739 memcpy(tmp, passphrase + (i * 2), 2);
740 key[i] = (unsigned char) strtol(tmp,
743 connman_dbus_dict_append_array(&dict,
744 "wep_key0", DBUS_TYPE_BYTE,
748 connman_dbus_dict_append_variant(&dict,
749 "wep_key0", DBUS_TYPE_STRING,
751 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
752 DBUS_TYPE_STRING, &index);
755 const char *key_mgmt = "NONE";
756 connman_dbus_dict_append_variant(&dict, "key_mgmt",
757 DBUS_TYPE_STRING, &key_mgmt);
760 dbus_message_iter_close_container(&array, &dict);
762 dbus_error_init(&error);
764 reply = dbus_connection_send_with_reply_and_block(connection,
765 message, -1, &error);
767 if (dbus_error_is_set(&error) == TRUE) {
768 connman_error("%s", error.message);
769 dbus_error_free(&error);
771 connman_error("Failed to set network options");
772 dbus_message_unref(message);
776 dbus_message_unref(message);
778 dbus_message_unref(reply);
783 static int initiate_scan(struct supplicant_task *task)
785 DBusMessage *message;
786 DBusPendingCall *call;
788 DBG("task %p", task);
790 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
791 SUPPLICANT_INTF ".Interface", "scan");
795 if (dbus_connection_send_with_reply(connection, message,
796 &call, TIMEOUT) == FALSE) {
797 connman_error("Failed to initiate scan");
798 dbus_message_unref(message);
802 dbus_message_unref(message);
811 { "<hidden>", "hidden" },
812 { "default", "linksys" },
821 { "Symbol", "symbol" },
822 { "Wireless" , "wireless" },
827 static char *build_group(const char *addr, const char *name,
828 const unsigned char *ssid, unsigned int ssid_len,
829 const char *mode, const char *security)
837 str = g_string_sized_new((ssid_len * 2) + 24);
841 for (i = 0; special_ssid[i].name; i++) {
842 if (g_strcmp0(special_ssid[i].name, name) == 0) {
843 if (special_ssid[i].value == NULL)
844 g_string_append_printf(str, "%s_%s",
847 g_string_append_printf(str, "%s_%s",
848 special_ssid[i].value, addr);
853 if (ssid_len > 0 && ssid[0] != '\0') {
854 for (i = 0; i < ssid_len; i++)
855 g_string_append_printf(str, "%02x", ssid[i]);
857 g_string_append_printf(str, "hidden_%s", addr);
860 g_string_append_printf(str, "_%s_%s", mode, security);
862 return g_string_free(str, FALSE);
865 static void extract_addr(DBusMessageIter *value,
866 struct supplicant_result *result)
868 DBusMessageIter array;
869 struct ether_addr *eth;
873 dbus_message_iter_recurse(value, &array);
874 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
881 result->addr = g_try_malloc0(18);
882 if (result->addr == NULL)
885 snprintf(result->addr, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
886 eth->ether_addr_octet[0],
887 eth->ether_addr_octet[1],
888 eth->ether_addr_octet[2],
889 eth->ether_addr_octet[3],
890 eth->ether_addr_octet[4],
891 eth->ether_addr_octet[5]);
893 result->path = g_try_malloc0(18);
894 if (result->path == NULL)
897 snprintf(result->path, 18, "%02x%02x%02x%02x%02x%02x",
898 eth->ether_addr_octet[0],
899 eth->ether_addr_octet[1],
900 eth->ether_addr_octet[2],
901 eth->ether_addr_octet[3],
902 eth->ether_addr_octet[4],
903 eth->ether_addr_octet[5]);
906 static void extract_ssid(DBusMessageIter *value,
907 struct supplicant_result *result)
909 DBusMessageIter array;
913 dbus_message_iter_recurse(value, &array);
914 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
919 result->ssid = g_try_malloc(ssid_len);
920 if (result->ssid == NULL)
923 memcpy(result->ssid, ssid, ssid_len);
924 result->ssid_len = ssid_len;
926 result->name = g_try_malloc0(ssid_len + 1);
927 if (result->name == NULL)
930 memcpy(result->name, ssid, ssid_len);
933 static void extract_wpaie(DBusMessageIter *value,
934 struct supplicant_result *result)
936 DBusMessageIter array;
940 dbus_message_iter_recurse(value, &array);
941 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
944 result->has_wpa = TRUE;
947 static void extract_rsnie(DBusMessageIter *value,
948 struct supplicant_result *result)
950 DBusMessageIter array;
954 dbus_message_iter_recurse(value, &array);
955 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
958 result->has_rsn = TRUE;
961 static void extract_wpsie(DBusMessageIter *value,
962 struct supplicant_result *result)
964 DBusMessageIter array;
968 dbus_message_iter_recurse(value, &array);
969 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
972 result->has_wps = TRUE;
975 static void extract_capabilites(DBusMessageIter *value,
976 struct supplicant_result *result)
978 dbus_message_iter_get_basic(value, &result->capabilities);
980 if (result->capabilities & IEEE80211_CAP_ESS)
981 result->adhoc = FALSE;
982 else if (result->capabilities & IEEE80211_CAP_IBSS)
983 result->adhoc = TRUE;
985 if (result->capabilities & IEEE80211_CAP_PRIVACY)
986 result->has_wep = TRUE;
989 static unsigned char calculate_strength(struct supplicant_result *result)
991 if (result->quality < 0)
994 return result->quality;
997 static unsigned short calculate_channel(struct supplicant_result *result)
1002 static void get_properties(struct supplicant_task *task);
1004 static void properties_reply(DBusPendingCall *call, void *user_data)
1006 struct supplicant_task *task = user_data;
1007 struct supplicant_result result;
1008 struct connman_network *network;
1010 DBusMessageIter array, dict;
1011 unsigned char strength;
1012 unsigned short channel, frequency;
1013 const char *mode, *security;
1016 DBG("task %p", task);
1018 reply = dbus_pending_call_steal_reply(call);
1019 if (reply == NULL) {
1020 get_properties(task);
1024 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1025 dbus_message_unref(reply);
1026 get_properties(task);
1030 memset(&result, 0, sizeof(result));
1031 result.frequency = -1;
1032 result.quality = -1;
1036 dbus_message_iter_init(reply, &array);
1038 dbus_message_iter_recurse(&array, &dict);
1040 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1041 DBusMessageIter entry, value;
1044 dbus_message_iter_recurse(&dict, &entry);
1045 dbus_message_iter_get_basic(&entry, &key);
1047 dbus_message_iter_next(&entry);
1049 dbus_message_iter_recurse(&entry, &value);
1051 //type = dbus_message_iter_get_arg_type(&value);
1052 //dbus_message_iter_get_basic(&value, &val);
1060 * frequency : i (105)
1061 * capabilities : q (113)
1068 if (g_str_equal(key, "bssid") == TRUE)
1069 extract_addr(&value, &result);
1070 else if (g_str_equal(key, "ssid") == TRUE)
1071 extract_ssid(&value, &result);
1072 else if (g_str_equal(key, "wpaie") == TRUE)
1073 extract_wpaie(&value, &result);
1074 else if (g_str_equal(key, "rsnie") == TRUE)
1075 extract_rsnie(&value, &result);
1076 else if (g_str_equal(key, "wpsie") == TRUE)
1077 extract_wpsie(&value, &result);
1078 else if (g_str_equal(key, "capabilities") == TRUE)
1079 extract_capabilites(&value, &result);
1080 else if (g_str_equal(key, "frequency") == TRUE)
1081 dbus_message_iter_get_basic(&value, &result.frequency);
1082 else if (g_str_equal(key, "quality") == TRUE)
1083 dbus_message_iter_get_basic(&value, &result.quality);
1084 else if (g_str_equal(key, "noise") == TRUE)
1085 dbus_message_iter_get_basic(&value, &result.noise);
1086 else if (g_str_equal(key, "level") == TRUE)
1087 dbus_message_iter_get_basic(&value, &result.level);
1088 else if (g_str_equal(key, "maxrate") == TRUE)
1089 dbus_message_iter_get_basic(&value, &result.maxrate);
1091 dbus_message_iter_next(&dict);
1094 if (result.path == NULL)
1097 if (result.path[0] == '\0')
1100 strength = calculate_strength(&result);
1101 channel = calculate_channel(&result);
1103 frequency = (result.frequency < 0) ? 0 : result.frequency;
1105 if (result.has_rsn == TRUE)
1107 else if (result.has_wpa == TRUE)
1109 else if (result.has_wep == TRUE)
1114 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1116 group = build_group(result.path, result.name,
1117 result.ssid, result.ssid_len,
1120 network = connman_device_get_network(task->device, result.path);
1121 if (network == NULL) {
1124 network = connman_network_create(result.path,
1125 CONNMAN_NETWORK_TYPE_WIFI);
1126 if (network == NULL)
1129 index = connman_device_get_index(task->device);
1130 connman_network_set_index(network, index);
1132 connman_network_set_protocol(network,
1133 CONNMAN_NETWORK_PROTOCOL_IP);
1135 connman_network_set_string(network, "Address", result.addr);
1137 if (connman_device_add_network(task->device, network) < 0) {
1138 connman_network_unref(network);
1143 if (result.name != NULL && result.name[0] != '\0')
1144 connman_network_set_string(network, "Name", result.name);
1146 connman_network_set_blob(network, "WiFi.SSID",
1147 result.ssid, result.ssid_len);
1149 connman_network_set_string(network, "WiFi.Mode", mode);
1151 DBG("%s (%s %s) strength %d (%s)",
1152 result.name, mode, security, strength,
1153 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1155 connman_network_set_available(network, TRUE);
1156 connman_network_set_uint8(network, "Strength", strength);
1157 connman_network_set_uint16(network, "Frequency", frequency);
1159 connman_network_set_uint16(network, "WiFi.Channel", channel);
1160 connman_network_set_string(network, "WiFi.Security", security);
1162 connman_network_set_group(network, group);
1167 g_free(result.path);
1168 g_free(result.addr);
1169 g_free(result.name);
1170 g_free(result.ssid);
1172 dbus_message_unref(reply);
1174 get_properties(task);
1177 static void get_properties(struct supplicant_task *task)
1179 DBusMessage *message;
1180 DBusPendingCall *call;
1183 path = g_slist_nth_data(task->scan_results, 0);
1187 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1188 SUPPLICANT_INTF ".BSSID",
1191 task->scan_results = g_slist_remove(task->scan_results, path);
1194 if (message == NULL)
1197 if (dbus_connection_send_with_reply(connection, message,
1198 &call, TIMEOUT) == FALSE) {
1199 connman_error("Failed to get network properties");
1200 dbus_message_unref(message);
1205 connman_error("D-Bus connection not available");
1206 dbus_message_unref(message);
1210 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1212 dbus_message_unref(message);
1217 if (task->noscan == FALSE)
1218 connman_device_set_scanning(task->device, FALSE);
1221 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1223 struct supplicant_task *task = user_data;
1229 DBG("task %p", task);
1231 reply = dbus_pending_call_steal_reply(call);
1235 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1238 dbus_error_init(&error);
1240 if (dbus_message_get_args(reply, &error,
1241 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1242 &results, &num_results,
1243 DBUS_TYPE_INVALID) == FALSE) {
1244 if (dbus_error_is_set(&error) == TRUE) {
1245 connman_error("%s", error.message);
1246 dbus_error_free(&error);
1248 connman_error("Wrong arguments for scan result");
1252 if (num_results == 0)
1255 for (i = 0; i < num_results; i++) {
1256 char *path = g_strdup(results[i]);
1260 task->scan_results = g_slist_append(task->scan_results, path);
1263 g_strfreev(results);
1265 dbus_message_unref(reply);
1267 get_properties(task);
1272 dbus_message_unref(reply);
1275 if (task->noscan == FALSE)
1276 connman_device_set_scanning(task->device, FALSE);
1279 static void scan_results_available(struct supplicant_task *task)
1281 DBusMessage *message;
1282 DBusPendingCall *call;
1284 DBG("task %p", task);
1286 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1287 SUPPLICANT_INTF ".Interface",
1289 if (message == NULL)
1292 if (dbus_connection_send_with_reply(connection, message,
1293 &call, TIMEOUT) == FALSE) {
1294 connman_error("Failed to request scan result");
1298 if (task->noscan == FALSE)
1299 connman_device_set_scanning(task->device, TRUE);
1302 connman_error("D-Bus connection not available");
1306 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1309 dbus_message_unref(message);
1312 static enum supplicant_state string2state(const char *state)
1314 if (g_str_equal(state, "INACTIVE") == TRUE)
1315 return WPA_INACTIVE;
1316 else if (g_str_equal(state, "SCANNING") == TRUE)
1317 return WPA_SCANNING;
1318 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1319 return WPA_ASSOCIATING;
1320 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1321 return WPA_ASSOCIATED;
1322 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1323 return WPA_GROUP_HANDSHAKE;
1324 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1325 return WPA_4WAY_HANDSHAKE;
1326 else if (g_str_equal(state, "COMPLETED") == TRUE)
1327 return WPA_COMPLETED;
1328 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1329 return WPA_DISCONNECTED;
1334 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1337 const char *newstate, *oldstate;
1338 enum supplicant_state state;
1340 dbus_error_init(&error);
1342 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1343 DBUS_TYPE_STRING, &oldstate,
1344 DBUS_TYPE_INVALID) == FALSE) {
1345 if (dbus_error_is_set(&error) == TRUE) {
1346 connman_error("%s", error.message);
1347 dbus_error_free(&error);
1349 connman_error("Wrong arguments for state change");
1353 DBG("state %s ==> %s", oldstate, newstate);
1355 state = string2state(newstate);
1356 if (state == WPA_INVALID)
1359 task->state = state;
1361 switch (task->state) {
1363 task->noscan = TRUE;
1364 connman_device_set_scanning(task->device, TRUE);
1366 case WPA_ASSOCIATING:
1367 case WPA_ASSOCIATED:
1368 case WPA_4WAY_HANDSHAKE:
1369 case WPA_GROUP_HANDSHAKE:
1370 task->noscan = TRUE;
1373 case WPA_DISCONNECTED:
1374 task->noscan = FALSE;
1377 task->noscan = FALSE;
1378 connman_device_set_scanning(task->device, FALSE);
1384 if (task->network == NULL)
1387 switch (task->state) {
1390 connman_network_set_connected(task->network, TRUE);
1391 connman_device_set_scanning(task->device, FALSE);
1393 case WPA_DISCONNECTED:
1395 connman_network_set_connected(task->network, FALSE);
1396 connman_device_set_scanning(task->device, FALSE);
1398 case WPA_ASSOCIATING:
1399 connman_network_set_associating(task->network, TRUE);
1402 connman_network_set_associating(task->network, FALSE);
1407 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1408 DBusMessage *msg, void *data)
1410 struct supplicant_task *task;
1411 const char *member, *path;
1413 if (dbus_message_has_interface(msg,
1414 SUPPLICANT_INTF ".Interface") == FALSE)
1415 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1417 member = dbus_message_get_member(msg);
1419 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1421 path = dbus_message_get_path(msg);
1423 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1425 task = find_task_by_path(path);
1427 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1429 DBG("task %p member %s", task, member);
1431 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1432 scan_results_available(task);
1433 else if (g_str_equal(member, "StateChange") == TRUE)
1434 state_change(task, msg);
1436 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1439 int supplicant_start(struct connman_device *device)
1441 struct supplicant_task *task;
1443 DBG("device %p", device);
1445 task = g_try_new0(struct supplicant_task, 1);
1449 task->ifindex = connman_device_get_index(device);
1450 task->ifname = inet_index2name(task->ifindex);
1452 if (task->ifname == NULL) {
1457 task->device = connman_device_ref(device);
1459 task->created = FALSE;
1460 task->noscan = FALSE;
1461 task->state = WPA_INVALID;
1463 task_list = g_slist_append(task_list, task);
1465 return create_interface(task);
1468 int supplicant_stop(struct connman_device *device)
1470 int index = connman_device_get_index(device);
1471 struct supplicant_task *task;
1473 DBG("device %p", device);
1475 task = find_task_by_index(index);
1479 task_list = g_slist_remove(task_list, task);
1481 disable_network(task);
1483 remove_network(task);
1485 return remove_interface(task);
1488 int supplicant_scan(struct connman_device *device)
1490 int index = connman_device_get_index(device);
1491 struct supplicant_task *task;
1494 DBG("device %p", device);
1496 task = find_task_by_index(index);
1500 switch (task->state) {
1503 case WPA_ASSOCIATING:
1504 case WPA_ASSOCIATED:
1505 case WPA_4WAY_HANDSHAKE:
1506 case WPA_GROUP_HANDSHAKE:
1512 err = initiate_scan(task);
1517 int supplicant_connect(struct connman_network *network)
1519 struct supplicant_task *task;
1520 const char *address, *security, *passphrase;
1522 unsigned int ssid_len;
1525 DBG("network %p", network);
1527 address = connman_network_get_string(network, "Address");
1528 security = connman_network_get_string(network, "WiFi.Security");
1529 passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1531 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1533 DBG("address %s security %s passphrase %s",
1534 address, security, passphrase);
1536 if (security == NULL && passphrase == NULL)
1539 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1542 index = connman_network_get_index(network);
1544 task = find_task_by_index(index);
1548 task->network = connman_network_ref(network);
1552 select_network(task);
1553 disable_network(task);
1555 set_network(task, ssid, ssid_len, address, security, passphrase);
1557 enable_network(task);
1559 connman_network_set_associating(task->network, TRUE);
1564 int supplicant_disconnect(struct connman_network *network)
1566 struct supplicant_task *task;
1569 DBG("network %p", network);
1571 index = connman_network_get_index(network);
1573 task = find_task_by_index(index);
1577 disable_network(task);
1579 remove_network(task);
1581 connman_network_set_connected(task->network, FALSE);
1583 connman_network_unref(task->network);
1588 static void supplicant_activate(DBusConnection *conn)
1590 DBusMessage *message;
1592 DBG("conn %p", conn);
1594 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1595 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1596 if (message == NULL)
1599 dbus_message_set_no_reply(message, TRUE);
1601 dbus_connection_send(conn, message, NULL);
1603 dbus_message_unref(message);
1606 static GSList *driver_list = NULL;
1608 static void supplicant_probe(DBusConnection *conn, void *user_data)
1612 DBG("conn %p", conn);
1614 for (list = driver_list; list; list = list->next) {
1615 struct supplicant_driver *driver = list->data;
1617 DBG("driver %p name %s", driver, driver->name);
1624 static void supplicant_remove(DBusConnection *conn, void *user_data)
1628 DBG("conn %p", conn);
1630 for (list = driver_list; list; list = list->next) {
1631 struct supplicant_driver *driver = list->data;
1633 DBG("driver %p name %s", driver, driver->name);
1640 static const char *supplicant_rule = "type=signal,"
1641 "interface=" SUPPLICANT_INTF ".Interface";
1644 static int supplicant_create(void)
1646 if (g_slist_length(driver_list) > 0)
1649 connection = connman_dbus_get_connection();
1650 if (connection == NULL)
1653 DBG("connection %p", connection);
1655 if (dbus_connection_add_filter(connection,
1656 supplicant_filter, NULL, NULL) == FALSE) {
1657 connection = connman_dbus_get_connection();
1661 dbus_bus_add_match(connection, supplicant_rule, NULL);
1662 dbus_connection_flush(connection);
1664 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1665 supplicant_probe, supplicant_remove, NULL, NULL);
1670 static void supplicant_destroy(void)
1672 if (g_slist_length(driver_list) > 0)
1675 DBG("connection %p", connection);
1678 g_dbus_remove_watch(connection, watch);
1680 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1681 dbus_connection_flush(connection);
1683 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1685 dbus_connection_unref(connection);
1689 int supplicant_register(struct supplicant_driver *driver)
1693 DBG("driver %p name %s", driver, driver->name);
1695 err = supplicant_create();
1699 driver_list = g_slist_append(driver_list, driver);
1701 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1702 supplicant_probe(connection, NULL);
1704 supplicant_activate(connection);
1709 void supplicant_unregister(struct supplicant_driver *driver)
1711 DBG("driver %p name %s", driver, driver->name);
1713 supplicant_remove(connection, NULL);
1715 driver_list = g_slist_remove(driver_list, driver);
1717 supplicant_destroy();