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)
999 if (result->frequency < 0)
1002 return (result->frequency - 2407) / 5;
1005 static void get_properties(struct supplicant_task *task);
1007 static void properties_reply(DBusPendingCall *call, void *user_data)
1009 struct supplicant_task *task = user_data;
1010 struct supplicant_result result;
1011 struct connman_network *network;
1013 DBusMessageIter array, dict;
1014 unsigned char strength;
1015 unsigned short channel, frequency;
1016 const char *mode, *security;
1019 DBG("task %p", task);
1021 reply = dbus_pending_call_steal_reply(call);
1022 if (reply == NULL) {
1023 get_properties(task);
1027 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1028 dbus_message_unref(reply);
1029 get_properties(task);
1033 memset(&result, 0, sizeof(result));
1034 result.frequency = -1;
1035 result.quality = -1;
1039 dbus_message_iter_init(reply, &array);
1041 dbus_message_iter_recurse(&array, &dict);
1043 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1044 DBusMessageIter entry, value;
1047 dbus_message_iter_recurse(&dict, &entry);
1048 dbus_message_iter_get_basic(&entry, &key);
1050 dbus_message_iter_next(&entry);
1052 dbus_message_iter_recurse(&entry, &value);
1054 //type = dbus_message_iter_get_arg_type(&value);
1055 //dbus_message_iter_get_basic(&value, &val);
1063 * frequency : i (105)
1064 * capabilities : q (113)
1071 if (g_str_equal(key, "bssid") == TRUE)
1072 extract_addr(&value, &result);
1073 else if (g_str_equal(key, "ssid") == TRUE)
1074 extract_ssid(&value, &result);
1075 else if (g_str_equal(key, "wpaie") == TRUE)
1076 extract_wpaie(&value, &result);
1077 else if (g_str_equal(key, "rsnie") == TRUE)
1078 extract_rsnie(&value, &result);
1079 else if (g_str_equal(key, "wpsie") == TRUE)
1080 extract_wpsie(&value, &result);
1081 else if (g_str_equal(key, "capabilities") == TRUE)
1082 extract_capabilites(&value, &result);
1083 else if (g_str_equal(key, "frequency") == TRUE)
1084 dbus_message_iter_get_basic(&value, &result.frequency);
1085 else if (g_str_equal(key, "quality") == TRUE)
1086 dbus_message_iter_get_basic(&value, &result.quality);
1087 else if (g_str_equal(key, "noise") == TRUE)
1088 dbus_message_iter_get_basic(&value, &result.noise);
1089 else if (g_str_equal(key, "level") == TRUE)
1090 dbus_message_iter_get_basic(&value, &result.level);
1091 else if (g_str_equal(key, "maxrate") == TRUE)
1092 dbus_message_iter_get_basic(&value, &result.maxrate);
1094 dbus_message_iter_next(&dict);
1097 if (result.path == NULL)
1100 if (result.path[0] == '\0')
1103 if (result.frequency > 0 && result.frequency < 14)
1104 result.frequency = 2407 + (5 * result.frequency);
1105 else if (result.frequency == 14)
1106 result.frequency = 2484;
1108 strength = calculate_strength(&result);
1109 channel = calculate_channel(&result);
1111 frequency = (result.frequency < 0) ? 0 : result.frequency;
1113 if (result.has_rsn == TRUE)
1115 else if (result.has_wpa == TRUE)
1117 else if (result.has_wep == TRUE)
1122 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1124 group = build_group(result.path, result.name,
1125 result.ssid, result.ssid_len,
1128 network = connman_device_get_network(task->device, result.path);
1129 if (network == NULL) {
1132 network = connman_network_create(result.path,
1133 CONNMAN_NETWORK_TYPE_WIFI);
1134 if (network == NULL)
1137 index = connman_device_get_index(task->device);
1138 connman_network_set_index(network, index);
1140 connman_network_set_protocol(network,
1141 CONNMAN_NETWORK_PROTOCOL_IP);
1143 connman_network_set_string(network, "Address", result.addr);
1145 if (connman_device_add_network(task->device, network) < 0) {
1146 connman_network_unref(network);
1151 if (result.name != NULL && result.name[0] != '\0')
1152 connman_network_set_string(network, "Name", result.name);
1154 connman_network_set_blob(network, "WiFi.SSID",
1155 result.ssid, result.ssid_len);
1157 connman_network_set_string(network, "WiFi.Mode", mode);
1159 DBG("%s (%s %s) strength %d (%s)",
1160 result.name, mode, security, strength,
1161 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1163 connman_network_set_available(network, TRUE);
1164 connman_network_set_uint8(network, "Strength", strength);
1165 connman_network_set_uint16(network, "Frequency", frequency);
1167 connman_network_set_uint16(network, "WiFi.Channel", channel);
1168 connman_network_set_string(network, "WiFi.Security", security);
1170 connman_network_set_group(network, group);
1175 g_free(result.path);
1176 g_free(result.addr);
1177 g_free(result.name);
1178 g_free(result.ssid);
1180 dbus_message_unref(reply);
1182 get_properties(task);
1185 static void get_properties(struct supplicant_task *task)
1187 DBusMessage *message;
1188 DBusPendingCall *call;
1191 path = g_slist_nth_data(task->scan_results, 0);
1195 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1196 SUPPLICANT_INTF ".BSSID",
1199 task->scan_results = g_slist_remove(task->scan_results, path);
1202 if (message == NULL)
1205 if (dbus_connection_send_with_reply(connection, message,
1206 &call, TIMEOUT) == FALSE) {
1207 connman_error("Failed to get network properties");
1208 dbus_message_unref(message);
1213 connman_error("D-Bus connection not available");
1214 dbus_message_unref(message);
1218 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1220 dbus_message_unref(message);
1225 if (task->noscan == FALSE)
1226 connman_device_set_scanning(task->device, FALSE);
1229 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1231 struct supplicant_task *task = user_data;
1237 DBG("task %p", task);
1239 reply = dbus_pending_call_steal_reply(call);
1243 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1246 dbus_error_init(&error);
1248 if (dbus_message_get_args(reply, &error,
1249 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1250 &results, &num_results,
1251 DBUS_TYPE_INVALID) == FALSE) {
1252 if (dbus_error_is_set(&error) == TRUE) {
1253 connman_error("%s", error.message);
1254 dbus_error_free(&error);
1256 connman_error("Wrong arguments for scan result");
1260 if (num_results == 0)
1263 for (i = 0; i < num_results; i++) {
1264 char *path = g_strdup(results[i]);
1268 task->scan_results = g_slist_append(task->scan_results, path);
1271 g_strfreev(results);
1273 dbus_message_unref(reply);
1275 get_properties(task);
1280 dbus_message_unref(reply);
1283 if (task->noscan == FALSE)
1284 connman_device_set_scanning(task->device, FALSE);
1287 static void scan_results_available(struct supplicant_task *task)
1289 DBusMessage *message;
1290 DBusPendingCall *call;
1292 DBG("task %p", task);
1294 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1295 SUPPLICANT_INTF ".Interface",
1297 if (message == NULL)
1300 if (dbus_connection_send_with_reply(connection, message,
1301 &call, TIMEOUT) == FALSE) {
1302 connman_error("Failed to request scan result");
1306 if (task->noscan == FALSE)
1307 connman_device_set_scanning(task->device, TRUE);
1310 connman_error("D-Bus connection not available");
1314 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1317 dbus_message_unref(message);
1320 static enum supplicant_state string2state(const char *state)
1322 if (g_str_equal(state, "INACTIVE") == TRUE)
1323 return WPA_INACTIVE;
1324 else if (g_str_equal(state, "SCANNING") == TRUE)
1325 return WPA_SCANNING;
1326 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1327 return WPA_ASSOCIATING;
1328 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1329 return WPA_ASSOCIATED;
1330 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1331 return WPA_GROUP_HANDSHAKE;
1332 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1333 return WPA_4WAY_HANDSHAKE;
1334 else if (g_str_equal(state, "COMPLETED") == TRUE)
1335 return WPA_COMPLETED;
1336 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1337 return WPA_DISCONNECTED;
1342 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1345 const char *newstate, *oldstate;
1346 enum supplicant_state state;
1348 dbus_error_init(&error);
1350 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1351 DBUS_TYPE_STRING, &oldstate,
1352 DBUS_TYPE_INVALID) == FALSE) {
1353 if (dbus_error_is_set(&error) == TRUE) {
1354 connman_error("%s", error.message);
1355 dbus_error_free(&error);
1357 connman_error("Wrong arguments for state change");
1361 DBG("state %s ==> %s", oldstate, newstate);
1363 state = string2state(newstate);
1364 if (state == WPA_INVALID)
1367 task->state = state;
1369 switch (task->state) {
1371 task->noscan = TRUE;
1372 connman_device_set_scanning(task->device, TRUE);
1374 case WPA_ASSOCIATING:
1375 case WPA_ASSOCIATED:
1376 case WPA_4WAY_HANDSHAKE:
1377 case WPA_GROUP_HANDSHAKE:
1378 task->noscan = TRUE;
1381 case WPA_DISCONNECTED:
1382 task->noscan = FALSE;
1385 task->noscan = FALSE;
1386 connman_device_set_scanning(task->device, FALSE);
1392 if (task->network == NULL)
1395 switch (task->state) {
1398 connman_network_set_connected(task->network, TRUE);
1399 connman_device_set_scanning(task->device, FALSE);
1401 case WPA_DISCONNECTED:
1403 connman_network_set_connected(task->network, FALSE);
1404 connman_device_set_scanning(task->device, FALSE);
1406 case WPA_ASSOCIATING:
1407 connman_network_set_associating(task->network, TRUE);
1410 connman_network_set_associating(task->network, FALSE);
1415 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1416 DBusMessage *msg, void *data)
1418 struct supplicant_task *task;
1419 const char *member, *path;
1421 if (dbus_message_has_interface(msg,
1422 SUPPLICANT_INTF ".Interface") == FALSE)
1423 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1425 member = dbus_message_get_member(msg);
1427 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1429 path = dbus_message_get_path(msg);
1431 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1433 task = find_task_by_path(path);
1435 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1437 DBG("task %p member %s", task, member);
1439 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1440 scan_results_available(task);
1441 else if (g_str_equal(member, "StateChange") == TRUE)
1442 state_change(task, msg);
1444 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1447 int supplicant_start(struct connman_device *device)
1449 struct supplicant_task *task;
1451 DBG("device %p", device);
1453 task = g_try_new0(struct supplicant_task, 1);
1457 task->ifindex = connman_device_get_index(device);
1458 task->ifname = inet_index2name(task->ifindex);
1460 if (task->ifname == NULL) {
1465 task->device = connman_device_ref(device);
1467 task->created = FALSE;
1468 task->noscan = FALSE;
1469 task->state = WPA_INVALID;
1471 task_list = g_slist_append(task_list, task);
1473 return create_interface(task);
1476 int supplicant_stop(struct connman_device *device)
1478 int index = connman_device_get_index(device);
1479 struct supplicant_task *task;
1481 DBG("device %p", device);
1483 task = find_task_by_index(index);
1487 task_list = g_slist_remove(task_list, task);
1489 disable_network(task);
1491 remove_network(task);
1493 return remove_interface(task);
1496 int supplicant_scan(struct connman_device *device)
1498 int index = connman_device_get_index(device);
1499 struct supplicant_task *task;
1502 DBG("device %p", device);
1504 task = find_task_by_index(index);
1508 switch (task->state) {
1511 case WPA_ASSOCIATING:
1512 case WPA_ASSOCIATED:
1513 case WPA_4WAY_HANDSHAKE:
1514 case WPA_GROUP_HANDSHAKE:
1520 err = initiate_scan(task);
1525 int supplicant_connect(struct connman_network *network)
1527 struct supplicant_task *task;
1528 const char *address, *security, *passphrase;
1530 unsigned int ssid_len;
1533 DBG("network %p", network);
1535 address = connman_network_get_string(network, "Address");
1536 security = connman_network_get_string(network, "WiFi.Security");
1537 passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1539 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1541 DBG("address %s security %s passphrase %s",
1542 address, security, passphrase);
1544 if (security == NULL && passphrase == NULL)
1547 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1550 index = connman_network_get_index(network);
1552 task = find_task_by_index(index);
1556 task->network = connman_network_ref(network);
1560 select_network(task);
1561 disable_network(task);
1563 set_network(task, ssid, ssid_len, address, security, passphrase);
1565 enable_network(task);
1567 connman_network_set_associating(task->network, TRUE);
1572 int supplicant_disconnect(struct connman_network *network)
1574 struct supplicant_task *task;
1577 DBG("network %p", network);
1579 index = connman_network_get_index(network);
1581 task = find_task_by_index(index);
1585 disable_network(task);
1587 remove_network(task);
1589 connman_network_set_connected(task->network, FALSE);
1591 connman_network_unref(task->network);
1596 static void supplicant_activate(DBusConnection *conn)
1598 DBusMessage *message;
1600 DBG("conn %p", conn);
1602 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1603 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1604 if (message == NULL)
1607 dbus_message_set_no_reply(message, TRUE);
1609 dbus_connection_send(conn, message, NULL);
1611 dbus_message_unref(message);
1614 static GSList *driver_list = NULL;
1616 static void supplicant_probe(DBusConnection *conn, void *user_data)
1620 DBG("conn %p", conn);
1622 for (list = driver_list; list; list = list->next) {
1623 struct supplicant_driver *driver = list->data;
1625 DBG("driver %p name %s", driver, driver->name);
1632 static void supplicant_remove(DBusConnection *conn, void *user_data)
1636 DBG("conn %p", conn);
1638 for (list = driver_list; list; list = list->next) {
1639 struct supplicant_driver *driver = list->data;
1641 DBG("driver %p name %s", driver, driver->name);
1648 static const char *supplicant_rule = "type=signal,"
1649 "interface=" SUPPLICANT_INTF ".Interface";
1652 static int supplicant_create(void)
1654 if (g_slist_length(driver_list) > 0)
1657 connection = connman_dbus_get_connection();
1658 if (connection == NULL)
1661 DBG("connection %p", connection);
1663 if (dbus_connection_add_filter(connection,
1664 supplicant_filter, NULL, NULL) == FALSE) {
1665 connection = connman_dbus_get_connection();
1669 dbus_bus_add_match(connection, supplicant_rule, NULL);
1670 dbus_connection_flush(connection);
1672 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1673 supplicant_probe, supplicant_remove, NULL, NULL);
1678 static void supplicant_destroy(void)
1680 if (g_slist_length(driver_list) > 0)
1683 DBG("connection %p", connection);
1686 g_dbus_remove_watch(connection, watch);
1688 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1689 dbus_connection_flush(connection);
1691 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1693 dbus_connection_unref(connection);
1697 int supplicant_register(struct supplicant_driver *driver)
1701 DBG("driver %p name %s", driver, driver->name);
1703 err = supplicant_create();
1707 driver_list = g_slist_append(driver_list, driver);
1709 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1710 supplicant_probe(connection, NULL);
1712 supplicant_activate(connection);
1717 void supplicant_unregister(struct supplicant_driver *driver)
1719 DBG("driver %p name %s", driver, driver->name);
1721 supplicant_remove(connection, NULL);
1723 driver_list = g_slist_remove(driver_list, driver);
1725 supplicant_destroy();