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 quality;
164 dbus_int32_t maxrate;
167 struct supplicant_task {
170 struct connman_device *device;
171 struct connman_network *network;
175 enum supplicant_state state;
177 GSList *scan_results;
180 static GSList *task_list = NULL;
182 static DBusConnection *connection;
184 static void free_task(struct supplicant_task *task)
186 DBG("task %p", task);
188 g_free(task->ifname);
193 static struct supplicant_task *find_task_by_index(int index)
197 for (list = task_list; list; list = list->next) {
198 struct supplicant_task *task = list->data;
200 if (task->ifindex == index)
207 static struct supplicant_task *find_task_by_path(const char *path)
211 for (list = task_list; list; list = list->next) {
212 struct supplicant_task *task = list->data;
214 if (g_str_equal(task->path, path) == TRUE)
221 static void add_interface_reply(DBusPendingCall *call, void *user_data)
223 struct supplicant_task *task = user_data;
228 DBG("task %p", task);
230 reply = dbus_pending_call_steal_reply(call);
234 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
237 dbus_error_init(&error);
239 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
240 DBUS_TYPE_INVALID) == FALSE) {
241 if (dbus_error_is_set(&error) == TRUE) {
242 connman_error("%s", error.message);
243 dbus_error_free(&error);
245 connman_error("Wrong arguments for add interface");
249 DBG("path %s", path);
251 task->path = g_strdup(path);
252 task->created = TRUE;
254 connman_device_set_powered(task->device, TRUE);
257 dbus_message_unref(reply);
260 static int add_interface(struct supplicant_task *task)
262 DBusMessage *message;
263 DBusPendingCall *call;
265 DBG("task %p", task);
267 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
268 SUPPLICANT_INTF, "addInterface");
272 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
275 if (dbus_connection_send_with_reply(connection, message,
276 &call, TIMEOUT) == FALSE) {
277 connman_error("Failed to add interface");
278 dbus_message_unref(message);
283 connman_error("D-Bus connection not available");
284 dbus_message_unref(message);
288 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
290 dbus_message_unref(message);
295 static void get_interface_reply(DBusPendingCall *call, void *user_data)
297 struct supplicant_task *task = user_data;
302 DBG("task %p", task);
304 reply = dbus_pending_call_steal_reply(call);
308 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
313 dbus_error_init(&error);
315 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
316 DBUS_TYPE_INVALID) == FALSE) {
317 if (dbus_error_is_set(&error) == TRUE) {
318 connman_error("%s", error.message);
319 dbus_error_free(&error);
321 connman_error("Wrong arguments for get interface");
325 DBG("path %s", path);
327 task->path = g_strdup(path);
328 task->created = FALSE;
330 connman_device_set_powered(task->device, TRUE);
333 dbus_message_unref(reply);
336 static int create_interface(struct supplicant_task *task)
338 DBusMessage *message;
339 DBusPendingCall *call;
341 DBG("task %p", task);
343 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
344 SUPPLICANT_INTF, "getInterface");
348 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
351 if (dbus_connection_send_with_reply(connection, message,
352 &call, TIMEOUT) == FALSE) {
353 connman_error("Failed to get interface");
354 dbus_message_unref(message);
359 connman_error("D-Bus connection not available");
360 dbus_message_unref(message);
364 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
366 dbus_message_unref(message);
371 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
373 struct supplicant_task *task = user_data;
376 DBG("task %p", task);
378 reply = dbus_pending_call_steal_reply(call);
380 connman_device_set_powered(task->device, FALSE);
382 connman_device_unref(task->device);
384 inet_ifdown(task->ifindex);
388 dbus_message_unref(reply);
391 static int remove_interface(struct supplicant_task *task)
393 DBusMessage *message;
394 DBusPendingCall *call;
396 DBG("task %p", task);
398 if (task->created == FALSE) {
399 connman_device_set_powered(task->device, FALSE);
403 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
404 SUPPLICANT_INTF, "removeInterface");
408 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
411 if (dbus_connection_send_with_reply(connection, message,
412 &call, TIMEOUT) == FALSE) {
413 connman_error("Failed to remove interface");
414 dbus_message_unref(message);
419 connman_error("D-Bus connection not available");
420 dbus_message_unref(message);
424 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
426 dbus_message_unref(message);
432 static int set_ap_scan(struct supplicant_task *task)
434 DBusMessage *message, *reply;
438 DBG("task %p", task);
440 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
441 SUPPLICANT_INTF ".Interface", "setAPScan");
445 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
448 dbus_error_init(&error);
450 reply = dbus_connection_send_with_reply_and_block(connection,
451 message, -1, &error);
453 if (dbus_error_is_set(&error) == TRUE) {
454 connman_error("%s", error.message);
455 dbus_error_free(&error);
457 connman_error("Failed to set AP scan");
458 dbus_message_unref(message);
462 dbus_message_unref(message);
464 dbus_message_unref(reply);
470 static int add_network(struct supplicant_task *task)
472 DBusMessage *message, *reply;
476 DBG("task %p", task);
478 if (task->netpath != NULL)
481 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
482 SUPPLICANT_INTF ".Interface", "addNetwork");
486 dbus_error_init(&error);
488 reply = dbus_connection_send_with_reply_and_block(connection,
489 message, -1, &error);
491 if (dbus_error_is_set(&error) == TRUE) {
492 connman_error("%s", error.message);
493 dbus_error_free(&error);
495 connman_error("Failed to add network");
496 dbus_message_unref(message);
500 dbus_message_unref(message);
502 dbus_error_init(&error);
504 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
505 DBUS_TYPE_INVALID) == FALSE) {
506 if (dbus_error_is_set(&error) == TRUE) {
507 connman_error("%s", error.message);
508 dbus_error_free(&error);
510 connman_error("Wrong arguments for network");
511 dbus_message_unref(reply);
515 DBG("path %s", path);
517 task->netpath = g_strdup(path);
519 dbus_message_unref(reply);
524 static int remove_network(struct supplicant_task *task)
526 DBusMessage *message, *reply;
529 DBG("task %p", task);
531 if (task->netpath == NULL)
534 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
535 SUPPLICANT_INTF ".Interface", "removeNetwork");
539 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
542 dbus_error_init(&error);
544 reply = dbus_connection_send_with_reply_and_block(connection,
545 message, -1, &error);
547 if (dbus_error_is_set(&error) == TRUE) {
548 connman_error("%s", error.message);
549 dbus_error_free(&error);
551 connman_error("Failed to remove network");
552 dbus_message_unref(message);
556 dbus_message_unref(message);
558 dbus_message_unref(reply);
560 g_free(task->netpath);
561 task->netpath = NULL;
566 static int select_network(struct supplicant_task *task)
568 DBusMessage *message, *reply;
571 DBG("task %p", task);
573 if (task->netpath == NULL)
576 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
577 SUPPLICANT_INTF ".Interface", "selectNetwork");
581 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
584 dbus_error_init(&error);
586 reply = dbus_connection_send_with_reply_and_block(connection,
587 message, -1, &error);
589 if (dbus_error_is_set(&error) == TRUE) {
590 connman_error("%s", error.message);
591 dbus_error_free(&error);
593 connman_error("Failed to select network");
594 dbus_message_unref(message);
598 dbus_message_unref(message);
600 dbus_message_unref(reply);
605 static int enable_network(struct supplicant_task *task)
607 DBusMessage *message, *reply;
610 DBG("task %p", task);
612 if (task->netpath == NULL)
615 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
616 SUPPLICANT_INTF ".Network", "enable");
620 dbus_error_init(&error);
622 reply = dbus_connection_send_with_reply_and_block(connection,
623 message, -1, &error);
625 if (dbus_error_is_set(&error) == TRUE) {
626 connman_error("%s", error.message);
627 dbus_error_free(&error);
629 connman_error("Failed to enable network");
630 dbus_message_unref(message);
634 dbus_message_unref(message);
636 dbus_message_unref(reply);
641 static int disable_network(struct supplicant_task *task)
643 DBusMessage *message, *reply;
646 DBG("task %p", task);
648 if (task->netpath == NULL)
651 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
652 SUPPLICANT_INTF ".Network", "disable");
656 dbus_error_init(&error);
658 reply = dbus_connection_send_with_reply_and_block(connection,
659 message, -1, &error);
661 if (dbus_error_is_set(&error) == TRUE) {
662 connman_error("%s", error.message);
663 dbus_error_free(&error);
665 connman_error("Failed to disable network");
666 dbus_message_unref(message);
670 dbus_message_unref(message);
672 dbus_message_unref(reply);
677 static int set_network(struct supplicant_task *task,
678 const unsigned char *network, int len,
679 const char *address, const char *security,
680 const char *passphrase)
682 DBusMessage *message, *reply;
683 DBusMessageIter array, dict;
686 DBG("task %p", task);
688 if (task->netpath == NULL)
691 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
692 SUPPLICANT_INTF ".Network", "set");
696 dbus_message_iter_init_append(message, &array);
698 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
699 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
700 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
701 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
703 if (address == NULL) {
704 dbus_uint32_t scan_ssid = 1;
705 connman_dbus_dict_append_variant(&dict, "scan_ssid",
706 DBUS_TYPE_UINT32, &scan_ssid);
708 connman_dbus_dict_append_variant(&dict, "bssid",
709 DBUS_TYPE_STRING, &address);
711 connman_dbus_dict_append_array(&dict, "ssid",
712 DBUS_TYPE_BYTE, &network, len);
714 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
715 g_ascii_strcasecmp(security, "rsn") == 0) {
716 const char *key_mgmt = "WPA-PSK";
717 connman_dbus_dict_append_variant(&dict, "key_mgmt",
718 DBUS_TYPE_STRING, &key_mgmt);
720 if (passphrase && strlen(passphrase) > 0)
721 connman_dbus_dict_append_variant(&dict, "psk",
722 DBUS_TYPE_STRING, &passphrase);
723 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
724 const char *key_mgmt = "NONE", *index = "0";
725 connman_dbus_dict_append_variant(&dict, "key_mgmt",
726 DBUS_TYPE_STRING, &key_mgmt);
729 int size = strlen(passphrase);
730 if (size == 10 || size == 26) {
731 unsigned char *key = malloc(13);
734 memset(tmp, 0, sizeof(tmp));
737 for (i = 0; i < size / 2; i++) {
738 memcpy(tmp, passphrase + (i * 2), 2);
739 key[i] = (unsigned char) strtol(tmp,
742 connman_dbus_dict_append_array(&dict,
743 "wep_key0", DBUS_TYPE_BYTE,
747 connman_dbus_dict_append_variant(&dict,
748 "wep_key0", DBUS_TYPE_STRING,
750 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
751 DBUS_TYPE_STRING, &index);
754 const char *key_mgmt = "NONE";
755 connman_dbus_dict_append_variant(&dict, "key_mgmt",
756 DBUS_TYPE_STRING, &key_mgmt);
759 dbus_message_iter_close_container(&array, &dict);
761 dbus_error_init(&error);
763 reply = dbus_connection_send_with_reply_and_block(connection,
764 message, -1, &error);
766 if (dbus_error_is_set(&error) == TRUE) {
767 connman_error("%s", error.message);
768 dbus_error_free(&error);
770 connman_error("Failed to set network options");
771 dbus_message_unref(message);
775 dbus_message_unref(message);
777 dbus_message_unref(reply);
782 static int initiate_scan(struct supplicant_task *task)
784 DBusMessage *message;
785 DBusPendingCall *call;
787 DBG("task %p", task);
789 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
790 SUPPLICANT_INTF ".Interface", "scan");
794 if (dbus_connection_send_with_reply(connection, message,
795 &call, TIMEOUT) == FALSE) {
796 connman_error("Failed to initiate scan");
797 dbus_message_unref(message);
801 dbus_message_unref(message);
814 static char *build_group(const char *addr, const char *name,
815 const unsigned char *ssid, unsigned int ssid_len,
816 const char *mode, const char *security)
824 str = g_string_sized_new((ssid_len * 2) + 24);
828 for (i = 0; special_ssid[i].name; i++) {
829 if (g_strcmp0(special_ssid[i].name, name) == 0) {
830 g_string_append_printf(str, "%s_%s", name, addr);
835 if (ssid_len > 0 && ssid[0] != '\0') {
836 for (i = 0; i < ssid_len; i++)
837 g_string_append_printf(str, "%02x", ssid[i]);
839 g_string_append_printf(str, "hidden_%s", addr);
842 g_string_append_printf(str, "_%s_%s", mode, security);
844 return g_string_free(str, FALSE);
847 static void extract_addr(DBusMessageIter *value,
848 struct supplicant_result *result)
850 DBusMessageIter array;
851 struct ether_addr *eth;
855 dbus_message_iter_recurse(value, &array);
856 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
863 result->addr = g_try_malloc0(18);
864 if (result->addr == NULL)
867 snprintf(result->addr, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
868 eth->ether_addr_octet[0],
869 eth->ether_addr_octet[1],
870 eth->ether_addr_octet[2],
871 eth->ether_addr_octet[3],
872 eth->ether_addr_octet[4],
873 eth->ether_addr_octet[5]);
875 result->path = g_try_malloc0(18);
876 if (result->path == NULL)
879 snprintf(result->path, 18, "%02x%02x%02x%02x%02x%02x",
880 eth->ether_addr_octet[0],
881 eth->ether_addr_octet[1],
882 eth->ether_addr_octet[2],
883 eth->ether_addr_octet[3],
884 eth->ether_addr_octet[4],
885 eth->ether_addr_octet[5]);
888 static void extract_ssid(DBusMessageIter *value,
889 struct supplicant_result *result)
891 DBusMessageIter array;
895 dbus_message_iter_recurse(value, &array);
896 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
901 result->ssid = g_try_malloc(ssid_len);
902 if (result->ssid == NULL)
905 memcpy(result->ssid, ssid, ssid_len);
906 result->ssid_len = ssid_len;
908 result->name = g_try_malloc0(ssid_len + 1);
909 if (result->name == NULL)
912 memcpy(result->name, ssid, ssid_len);
915 static void extract_wpaie(DBusMessageIter *value,
916 struct supplicant_result *result)
918 DBusMessageIter array;
922 dbus_message_iter_recurse(value, &array);
923 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
926 result->has_wpa = TRUE;
929 static void extract_rsnie(DBusMessageIter *value,
930 struct supplicant_result *result)
932 DBusMessageIter array;
936 dbus_message_iter_recurse(value, &array);
937 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
940 result->has_rsn = TRUE;
943 static void extract_wpsie(DBusMessageIter *value,
944 struct supplicant_result *result)
946 DBusMessageIter array;
950 dbus_message_iter_recurse(value, &array);
951 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
954 result->has_wps = TRUE;
957 static void extract_capabilites(DBusMessageIter *value,
958 struct supplicant_result *result)
960 dbus_message_iter_get_basic(value, &result->capabilities);
962 if (result->capabilities & IEEE80211_CAP_ESS)
963 result->adhoc = FALSE;
964 else if (result->capabilities & IEEE80211_CAP_IBSS)
965 result->adhoc = TRUE;
967 if (result->capabilities & IEEE80211_CAP_PRIVACY)
968 result->has_wep = TRUE;
971 static void get_properties(struct supplicant_task *task);
973 static void properties_reply(DBusPendingCall *call, void *user_data)
975 struct supplicant_task *task = user_data;
976 struct supplicant_result result;
977 struct connman_network *network;
979 DBusMessageIter array, dict;
980 unsigned char strength;
981 const char *mode, *security;
984 DBG("task %p", task);
986 reply = dbus_pending_call_steal_reply(call);
988 get_properties(task);
992 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
993 dbus_message_unref(reply);
994 get_properties(task);
998 memset(&result, 0, sizeof(result));
1000 dbus_message_iter_init(reply, &array);
1002 dbus_message_iter_recurse(&array, &dict);
1004 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1005 DBusMessageIter entry, value;
1008 dbus_message_iter_recurse(&dict, &entry);
1009 dbus_message_iter_get_basic(&entry, &key);
1011 dbus_message_iter_next(&entry);
1013 dbus_message_iter_recurse(&entry, &value);
1015 //type = dbus_message_iter_get_arg_type(&value);
1016 //dbus_message_iter_get_basic(&value, &val);
1024 * frequency : i (105)
1025 * capabilities : q (113)
1032 if (g_str_equal(key, "bssid") == TRUE)
1033 extract_addr(&value, &result);
1034 else if (g_str_equal(key, "ssid") == TRUE)
1035 extract_ssid(&value, &result);
1036 else if (g_str_equal(key, "wpaie") == TRUE)
1037 extract_wpaie(&value, &result);
1038 else if (g_str_equal(key, "rsnie") == TRUE)
1039 extract_rsnie(&value, &result);
1040 else if (g_str_equal(key, "wpsie") == TRUE)
1041 extract_wpsie(&value, &result);
1042 else if (g_str_equal(key, "capabilities") == TRUE)
1043 extract_capabilites(&value, &result);
1044 else if (g_str_equal(key, "quality") == TRUE)
1045 dbus_message_iter_get_basic(&value, &result.quality);
1046 else if (g_str_equal(key, "noise") == TRUE)
1047 dbus_message_iter_get_basic(&value, &result.noise);
1048 else if (g_str_equal(key, "level") == TRUE)
1049 dbus_message_iter_get_basic(&value, &result.level);
1050 else if (g_str_equal(key, "maxrate") == TRUE)
1051 dbus_message_iter_get_basic(&value, &result.maxrate);
1053 dbus_message_iter_next(&dict);
1056 if (result.path == NULL)
1059 if (result.path[0] == '\0')
1062 strength = result.quality;
1064 if (result.has_rsn == TRUE)
1066 else if (result.has_wpa == TRUE)
1068 else if (result.has_wep == TRUE)
1073 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1075 group = build_group(result.path, result.name,
1076 result.ssid, result.ssid_len,
1079 network = connman_device_get_network(task->device, result.path);
1080 if (network == NULL) {
1083 network = connman_network_create(result.path,
1084 CONNMAN_NETWORK_TYPE_WIFI);
1085 if (network == NULL)
1088 index = connman_device_get_index(task->device);
1089 connman_network_set_index(network, index);
1091 connman_network_set_protocol(network,
1092 CONNMAN_NETWORK_PROTOCOL_IP);
1094 connman_network_set_string(network, "Address", result.addr);
1096 connman_network_set_group(network, group);
1098 if (result.name != NULL && result.name[0] != '\0')
1099 connman_network_set_string(network, "Name", result.name);
1101 connman_network_set_uint8(network, "Strength", strength);
1103 connman_network_set_string(network, "WiFi.Mode", mode);
1104 connman_network_set_string(network, "WiFi.Security", security);
1106 if (connman_device_add_network(task->device, network) < 0) {
1107 connman_network_unref(network);
1112 connman_network_set_group(network, group);
1116 if (result.name != NULL && result.name[0] != '\0')
1117 connman_network_set_string(network, "Name", result.name);
1119 connman_network_set_blob(network, "WiFi.SSID",
1120 result.ssid, result.ssid_len);
1122 connman_network_set_string(network, "WiFi.Mode", mode);
1124 DBG("%s (%s %s) strength %d (%s)",
1125 result.name, mode, security, strength,
1126 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1128 connman_network_set_available(network, TRUE);
1129 connman_network_set_uint8(network, "Strength", strength);
1131 connman_network_set_string(network, "WiFi.Security", security);
1134 g_free(result.path);
1135 g_free(result.addr);
1136 g_free(result.name);
1137 g_free(result.ssid);
1139 dbus_message_unref(reply);
1141 get_properties(task);
1144 static void get_properties(struct supplicant_task *task)
1146 DBusMessage *message;
1147 DBusPendingCall *call;
1150 path = g_slist_nth_data(task->scan_results, 0);
1154 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1155 SUPPLICANT_INTF ".BSSID",
1158 task->scan_results = g_slist_remove(task->scan_results, path);
1161 if (message == NULL)
1164 if (dbus_connection_send_with_reply(connection, message,
1165 &call, TIMEOUT) == FALSE) {
1166 connman_error("Failed to get network properties");
1167 dbus_message_unref(message);
1172 connman_error("D-Bus connection not available");
1173 dbus_message_unref(message);
1177 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1179 dbus_message_unref(message);
1184 if (task->noscan == FALSE)
1185 connman_device_set_scanning(task->device, FALSE);
1188 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1190 struct supplicant_task *task = user_data;
1196 DBG("task %p", task);
1198 reply = dbus_pending_call_steal_reply(call);
1202 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1205 dbus_error_init(&error);
1207 if (dbus_message_get_args(reply, &error,
1208 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1209 &results, &num_results,
1210 DBUS_TYPE_INVALID) == FALSE) {
1211 if (dbus_error_is_set(&error) == TRUE) {
1212 connman_error("%s", error.message);
1213 dbus_error_free(&error);
1215 connman_error("Wrong arguments for scan result");
1219 if (num_results == 0)
1222 for (i = 0; i < num_results; i++) {
1223 char *path = g_strdup(results[i]);
1227 task->scan_results = g_slist_append(task->scan_results, path);
1230 g_strfreev(results);
1232 dbus_message_unref(reply);
1234 get_properties(task);
1239 dbus_message_unref(reply);
1242 if (task->noscan == FALSE)
1243 connman_device_set_scanning(task->device, FALSE);
1246 static void scan_results_available(struct supplicant_task *task)
1248 DBusMessage *message;
1249 DBusPendingCall *call;
1251 DBG("task %p", task);
1253 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1254 SUPPLICANT_INTF ".Interface",
1256 if (message == NULL)
1259 if (dbus_connection_send_with_reply(connection, message,
1260 &call, TIMEOUT) == FALSE) {
1261 connman_error("Failed to request scan result");
1265 if (task->noscan == FALSE)
1266 connman_device_set_scanning(task->device, TRUE);
1269 connman_error("D-Bus connection not available");
1273 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1276 dbus_message_unref(message);
1279 static enum supplicant_state string2state(const char *state)
1281 if (g_str_equal(state, "INACTIVE") == TRUE)
1282 return WPA_INACTIVE;
1283 else if (g_str_equal(state, "SCANNING") == TRUE)
1284 return WPA_SCANNING;
1285 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1286 return WPA_ASSOCIATING;
1287 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1288 return WPA_ASSOCIATED;
1289 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1290 return WPA_GROUP_HANDSHAKE;
1291 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1292 return WPA_4WAY_HANDSHAKE;
1293 else if (g_str_equal(state, "COMPLETED") == TRUE)
1294 return WPA_COMPLETED;
1295 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1296 return WPA_DISCONNECTED;
1301 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1304 const char *newstate, *oldstate;
1305 enum supplicant_state state;
1307 dbus_error_init(&error);
1309 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1310 DBUS_TYPE_STRING, &oldstate,
1311 DBUS_TYPE_INVALID) == FALSE) {
1312 if (dbus_error_is_set(&error) == TRUE) {
1313 connman_error("%s", error.message);
1314 dbus_error_free(&error);
1316 connman_error("Wrong arguments for state change");
1320 DBG("state %s ==> %s", oldstate, newstate);
1322 state = string2state(newstate);
1323 if (state == WPA_INVALID)
1326 task->state = state;
1328 switch (task->state) {
1330 task->noscan = TRUE;
1331 connman_device_set_scanning(task->device, TRUE);
1333 case WPA_ASSOCIATING:
1334 case WPA_ASSOCIATED:
1335 case WPA_4WAY_HANDSHAKE:
1336 case WPA_GROUP_HANDSHAKE:
1337 task->noscan = TRUE;
1340 case WPA_DISCONNECTED:
1341 task->noscan = FALSE;
1344 task->noscan = FALSE;
1345 connman_device_set_scanning(task->device, FALSE);
1351 if (task->network == NULL)
1354 switch (task->state) {
1357 connman_network_set_connected(task->network, TRUE);
1358 connman_device_set_scanning(task->device, FALSE);
1360 case WPA_DISCONNECTED:
1362 connman_network_set_connected(task->network, FALSE);
1363 connman_device_set_scanning(task->device, FALSE);
1365 case WPA_ASSOCIATING:
1366 connman_network_set_associating(task->network, TRUE);
1369 connman_network_set_associating(task->network, FALSE);
1374 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1375 DBusMessage *msg, void *data)
1377 struct supplicant_task *task;
1378 const char *member, *path;
1380 if (dbus_message_has_interface(msg,
1381 SUPPLICANT_INTF ".Interface") == FALSE)
1382 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1384 member = dbus_message_get_member(msg);
1386 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1388 path = dbus_message_get_path(msg);
1390 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1392 task = find_task_by_path(path);
1394 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1396 DBG("task %p member %s", task, member);
1398 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1399 scan_results_available(task);
1400 else if (g_str_equal(member, "StateChange") == TRUE)
1401 state_change(task, msg);
1403 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1406 int supplicant_start(struct connman_device *device)
1408 struct supplicant_task *task;
1410 DBG("device %p", device);
1412 task = g_try_new0(struct supplicant_task, 1);
1416 task->ifindex = connman_device_get_index(device);
1417 task->ifname = inet_index2name(task->ifindex);
1419 if (task->ifname == NULL) {
1424 task->device = connman_device_ref(device);
1426 task->created = FALSE;
1427 task->noscan = FALSE;
1428 task->state = WPA_INVALID;
1430 task_list = g_slist_append(task_list, task);
1432 return create_interface(task);
1435 int supplicant_stop(struct connman_device *device)
1437 int index = connman_device_get_index(device);
1438 struct supplicant_task *task;
1440 DBG("device %p", device);
1442 task = find_task_by_index(index);
1446 task_list = g_slist_remove(task_list, task);
1448 disable_network(task);
1450 remove_network(task);
1452 return remove_interface(task);
1455 int supplicant_scan(struct connman_device *device)
1457 int index = connman_device_get_index(device);
1458 struct supplicant_task *task;
1461 DBG("device %p", device);
1463 task = find_task_by_index(index);
1467 switch (task->state) {
1470 case WPA_ASSOCIATING:
1471 case WPA_ASSOCIATED:
1472 case WPA_4WAY_HANDSHAKE:
1473 case WPA_GROUP_HANDSHAKE:
1479 err = initiate_scan(task);
1484 int supplicant_connect(struct connman_network *network)
1486 struct supplicant_task *task;
1487 const char *address, *security, *passphrase;
1489 unsigned int ssid_len;
1492 DBG("network %p", network);
1494 address = connman_network_get_string(network, "Address");
1495 security = connman_network_get_string(network, "WiFi.Security");
1496 passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1498 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1500 DBG("address %s security %s passphrase %s",
1501 address, security, passphrase);
1503 if (security == NULL && passphrase == NULL)
1506 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1509 index = connman_network_get_index(network);
1511 task = find_task_by_index(index);
1515 task->network = connman_network_ref(network);
1519 select_network(task);
1520 disable_network(task);
1522 set_network(task, ssid, ssid_len, address, security, passphrase);
1524 enable_network(task);
1526 connman_network_set_associating(task->network, TRUE);
1531 int supplicant_disconnect(struct connman_network *network)
1533 struct supplicant_task *task;
1536 DBG("network %p", network);
1538 index = connman_network_get_index(network);
1540 task = find_task_by_index(index);
1544 disable_network(task);
1546 remove_network(task);
1548 connman_network_set_connected(task->network, FALSE);
1550 connman_network_unref(task->network);
1555 static void supplicant_activate(DBusConnection *conn)
1557 DBusMessage *message;
1559 DBG("conn %p", conn);
1561 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1562 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1563 if (message == NULL)
1566 dbus_message_set_no_reply(message, TRUE);
1568 dbus_connection_send(conn, message, NULL);
1570 dbus_message_unref(message);
1573 static GSList *driver_list = NULL;
1575 static void supplicant_probe(DBusConnection *conn, void *user_data)
1579 DBG("conn %p", conn);
1581 for (list = driver_list; list; list = list->next) {
1582 struct supplicant_driver *driver = list->data;
1584 DBG("driver %p name %s", driver, driver->name);
1591 static void supplicant_remove(DBusConnection *conn, void *user_data)
1595 DBG("conn %p", conn);
1597 for (list = driver_list; list; list = list->next) {
1598 struct supplicant_driver *driver = list->data;
1600 DBG("driver %p name %s", driver, driver->name);
1607 static const char *supplicant_rule = "type=signal,"
1608 "interface=" SUPPLICANT_INTF ".Interface";
1611 static int supplicant_create(void)
1613 if (g_slist_length(driver_list) > 0)
1616 connection = connman_dbus_get_connection();
1617 if (connection == NULL)
1620 DBG("connection %p", connection);
1622 if (dbus_connection_add_filter(connection,
1623 supplicant_filter, NULL, NULL) == FALSE) {
1624 connection = connman_dbus_get_connection();
1628 dbus_bus_add_match(connection, supplicant_rule, NULL);
1629 dbus_connection_flush(connection);
1631 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1632 supplicant_probe, supplicant_remove, NULL, NULL);
1637 static void supplicant_destroy(void)
1639 if (g_slist_length(driver_list) > 0)
1642 DBG("connection %p", connection);
1645 g_dbus_remove_watch(connection, watch);
1647 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1648 dbus_connection_flush(connection);
1650 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1652 dbus_connection_unref(connection);
1656 int supplicant_register(struct supplicant_driver *driver)
1660 DBG("driver %p name %s", driver, driver->name);
1662 err = supplicant_create();
1666 driver_list = g_slist_append(driver_list, driver);
1668 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1669 supplicant_probe(connection, NULL);
1671 supplicant_activate(connection);
1676 void supplicant_unregister(struct supplicant_driver *driver)
1678 DBG("driver %p name %s", driver, driver->name);
1680 supplicant_remove(connection, NULL);
1682 driver_list = g_slist_remove(driver_list, driver);
1684 supplicant_destroy();