5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
39 #define CONNMAN_API_SUBJECT_TO_CHANGE
40 #include <connman/device.h>
41 #include <connman/option.h>
42 #include <connman/inet.h>
43 #include <connman/dbus.h>
44 #include <connman/log.h>
46 #include "supplicant.h"
50 #define IEEE80211_CAP_ESS 0x0001
51 #define IEEE80211_CAP_IBSS 0x0002
52 #define IEEE80211_CAP_PRIVACY 0x0010
54 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
55 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
56 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
58 /* Taken from "WPA Supplicant - Common definitions" */
59 enum supplicant_state {
61 * WPA_DISCONNECTED - Disconnected state
63 * This state indicates that client is not associated, but is likely to
64 * start looking for an access point. This state is entered when a
70 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
72 * This state is entered if there are no enabled networks in the
73 * configuration. wpa_supplicant is not trying to associate with a new
74 * network and external interaction (e.g., ctrl_iface call to add or
75 * enable a network) is needed to start association.
80 * WPA_SCANNING - Scanning for a network
82 * This state is entered when wpa_supplicant starts scanning for a
88 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
90 * This state is entered when wpa_supplicant has found a suitable BSS
91 * to associate with and the driver is configured to try to associate
92 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
93 * state is entered when the driver is configured to try to associate
94 * with a network using the configured SSID and security policy.
99 * WPA_ASSOCIATED - Association completed
101 * This state is entered when the driver reports that association has
102 * been successfully completed with an AP. If IEEE 802.1X is used
103 * (with or without WPA/WPA2), wpa_supplicant remains in this state
104 * until the IEEE 802.1X/EAPOL authentication has been completed.
109 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
111 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
112 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
113 * frame after association. In case of WPA-EAP, this state is entered
114 * when the IEEE 802.1X/EAPOL authentication has been completed.
119 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
121 * This state is entered when 4-Way Key Handshake has been completed
122 * (i.e., when the supplicant sends out message 4/4) and when Group
123 * Key rekeying is started by the AP (i.e., when supplicant receives
129 * WPA_COMPLETED - All authentication completed
131 * This state is entered when the full authentication process is
132 * completed. In case of WPA2, this happens when the 4-Way Handshake is
133 * successfully completed. With WPA, this state is entered after the
134 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
135 * completed after dynamic keys are received (or if not used, after
136 * the EAP authentication has been completed). With static WEP keys and
137 * plaintext connections, this state is entered when an association
138 * has been completed.
140 * This state indicates that the supplicant has completed its
141 * processing for the association phase and that data connection is
147 * WPA_INVALID - Invalid state (parsing error)
149 * This state is returned if the string input is invalid. It is not
150 * an official wpa_supplicant state.
155 struct supplicant_result {
159 unsigned int addr_len;
161 unsigned int ssid_len;
162 dbus_uint16_t capabilities;
168 dbus_int32_t frequency;
169 dbus_int32_t quality;
172 dbus_int32_t maxrate;
175 struct supplicant_task {
178 struct connman_device *device;
179 struct connman_network *network;
183 enum supplicant_state state;
185 GSList *scan_results;
186 struct iw_range *range;
189 static GSList *task_list = NULL;
191 static DBusConnection *connection;
193 static void free_task(struct supplicant_task *task)
195 DBG("task %p", task);
197 g_free(task->ifname);
202 static struct supplicant_task *find_task_by_index(int index)
206 for (list = task_list; list; list = list->next) {
207 struct supplicant_task *task = list->data;
209 if (task->ifindex == index)
216 static struct supplicant_task *find_task_by_path(const char *path)
220 for (list = task_list; list; list = list->next) {
221 struct supplicant_task *task = list->data;
223 if (g_strcmp0(task->path, path) == 0)
230 static void add_interface_reply(DBusPendingCall *call, void *user_data)
232 struct supplicant_task *task = user_data;
237 DBG("task %p", task);
239 reply = dbus_pending_call_steal_reply(call);
243 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
246 dbus_error_init(&error);
248 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
249 DBUS_TYPE_INVALID) == FALSE) {
250 if (dbus_error_is_set(&error) == TRUE) {
251 connman_error("%s", error.message);
252 dbus_error_free(&error);
254 connman_error("Wrong arguments for add interface");
258 DBG("path %s", path);
260 task->path = g_strdup(path);
261 task->created = TRUE;
263 connman_device_set_powered(task->device, TRUE);
265 dbus_message_unref(reply);
270 task_list = g_slist_remove(task_list, task);
272 connman_device_unref(task->device);
277 static int add_interface(struct supplicant_task *task)
279 const char *driver = connman_option_get_string("wifi");
280 DBusMessage *message;
281 DBusMessageIter array, dict;
282 DBusPendingCall *call;
284 DBG("task %p", task);
286 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
287 SUPPLICANT_INTF, "addInterface");
291 dbus_message_iter_init_append(message, &array);
293 dbus_message_iter_append_basic(&array,
294 DBUS_TYPE_STRING, &task->ifname);
296 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
297 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
298 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
299 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
301 connman_dbus_dict_append_variant(&dict, "driver",
302 DBUS_TYPE_STRING, &driver);
304 dbus_message_iter_close_container(&array, &dict);
306 if (dbus_connection_send_with_reply(connection, message,
307 &call, TIMEOUT) == FALSE) {
308 connman_error("Failed to add interface");
309 dbus_message_unref(message);
314 connman_error("D-Bus connection not available");
315 dbus_message_unref(message);
319 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
321 dbus_message_unref(message);
326 static void get_interface_reply(DBusPendingCall *call, void *user_data)
328 struct supplicant_task *task = user_data;
333 DBG("task %p", task);
335 reply = dbus_pending_call_steal_reply(call);
339 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
344 dbus_error_init(&error);
346 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
347 DBUS_TYPE_INVALID) == FALSE) {
348 if (dbus_error_is_set(&error) == TRUE) {
349 connman_error("%s", error.message);
350 dbus_error_free(&error);
352 connman_error("Wrong arguments for get interface");
356 DBG("path %s", path);
358 task->path = g_strdup(path);
359 task->created = FALSE;
361 connman_device_set_powered(task->device, TRUE);
364 dbus_message_unref(reply);
367 static int create_interface(struct supplicant_task *task)
369 DBusMessage *message;
370 DBusPendingCall *call;
372 DBG("task %p", task);
374 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
375 SUPPLICANT_INTF, "getInterface");
379 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
382 if (dbus_connection_send_with_reply(connection, message,
383 &call, TIMEOUT) == FALSE) {
384 connman_error("Failed to get interface");
385 dbus_message_unref(message);
390 connman_error("D-Bus connection not available");
391 dbus_message_unref(message);
395 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
397 dbus_message_unref(message);
402 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
404 struct supplicant_task *task = user_data;
407 DBG("task %p", task);
409 reply = dbus_pending_call_steal_reply(call);
411 connman_device_set_powered(task->device, FALSE);
413 connman_device_unref(task->device);
415 connman_inet_ifdown(task->ifindex);
419 dbus_message_unref(reply);
422 static int remove_interface(struct supplicant_task *task)
424 DBusMessage *message;
425 DBusPendingCall *call;
427 DBG("task %p", task);
429 if (task->created == FALSE) {
430 connman_device_set_powered(task->device, FALSE);
434 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
435 SUPPLICANT_INTF, "removeInterface");
439 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
442 if (dbus_connection_send_with_reply(connection, message,
443 &call, TIMEOUT) == FALSE) {
444 connman_error("Failed to remove interface");
445 dbus_message_unref(message);
450 connman_error("D-Bus connection not available");
451 dbus_message_unref(message);
455 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
457 dbus_message_unref(message);
463 static int set_ap_scan(struct supplicant_task *task)
465 DBusMessage *message, *reply;
469 DBG("task %p", task);
471 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
472 SUPPLICANT_INTF ".Interface", "setAPScan");
476 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
479 dbus_error_init(&error);
481 reply = dbus_connection_send_with_reply_and_block(connection,
482 message, -1, &error);
484 if (dbus_error_is_set(&error) == TRUE) {
485 connman_error("%s", error.message);
486 dbus_error_free(&error);
488 connman_error("Failed to set AP scan");
489 dbus_message_unref(message);
493 dbus_message_unref(message);
495 dbus_message_unref(reply);
501 static int add_network(struct supplicant_task *task)
503 DBusMessage *message, *reply;
507 DBG("task %p", task);
509 if (task->netpath != NULL)
512 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
513 SUPPLICANT_INTF ".Interface", "addNetwork");
517 dbus_error_init(&error);
519 reply = dbus_connection_send_with_reply_and_block(connection,
520 message, -1, &error);
522 if (dbus_error_is_set(&error) == TRUE) {
523 connman_error("%s", error.message);
524 dbus_error_free(&error);
526 connman_error("Failed to add network");
527 dbus_message_unref(message);
531 dbus_message_unref(message);
533 dbus_error_init(&error);
535 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
536 DBUS_TYPE_INVALID) == FALSE) {
537 if (dbus_error_is_set(&error) == TRUE) {
538 connman_error("%s", error.message);
539 dbus_error_free(&error);
541 connman_error("Wrong arguments for network");
542 dbus_message_unref(reply);
546 DBG("path %s", path);
548 task->netpath = g_strdup(path);
550 dbus_message_unref(reply);
555 static int remove_network(struct supplicant_task *task)
557 DBusMessage *message, *reply;
560 DBG("task %p", task);
562 if (task->netpath == NULL)
565 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
566 SUPPLICANT_INTF ".Interface", "removeNetwork");
570 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
573 dbus_error_init(&error);
575 reply = dbus_connection_send_with_reply_and_block(connection,
576 message, -1, &error);
578 if (dbus_error_is_set(&error) == TRUE) {
579 connman_error("%s", error.message);
580 dbus_error_free(&error);
582 connman_error("Failed to remove network");
583 dbus_message_unref(message);
587 dbus_message_unref(message);
589 dbus_message_unref(reply);
591 g_free(task->netpath);
592 task->netpath = NULL;
597 static int select_network(struct supplicant_task *task)
599 DBusMessage *message, *reply;
602 DBG("task %p", task);
604 if (task->netpath == NULL)
607 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
608 SUPPLICANT_INTF ".Interface", "selectNetwork");
612 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
615 dbus_error_init(&error);
617 reply = dbus_connection_send_with_reply_and_block(connection,
618 message, -1, &error);
620 if (dbus_error_is_set(&error) == TRUE) {
621 connman_error("%s", error.message);
622 dbus_error_free(&error);
624 connman_error("Failed to select network");
625 dbus_message_unref(message);
629 dbus_message_unref(message);
631 dbus_message_unref(reply);
636 static int enable_network(struct supplicant_task *task)
638 DBusMessage *message, *reply;
641 DBG("task %p", task);
643 if (task->netpath == NULL)
646 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
647 SUPPLICANT_INTF ".Network", "enable");
651 dbus_error_init(&error);
653 reply = dbus_connection_send_with_reply_and_block(connection,
654 message, -1, &error);
656 if (dbus_error_is_set(&error) == TRUE) {
657 connman_error("%s", error.message);
658 dbus_error_free(&error);
660 connman_error("Failed to enable network");
661 dbus_message_unref(message);
665 dbus_message_unref(message);
667 dbus_message_unref(reply);
672 static int disable_network(struct supplicant_task *task)
674 DBusMessage *message, *reply;
677 DBG("task %p", task);
679 if (task->netpath == NULL)
682 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
683 SUPPLICANT_INTF ".Network", "disable");
687 dbus_error_init(&error);
689 reply = dbus_connection_send_with_reply_and_block(connection,
690 message, -1, &error);
692 if (dbus_error_is_set(&error) == TRUE) {
693 connman_error("%s", error.message);
694 dbus_error_free(&error);
696 connman_error("Failed to disable network");
697 dbus_message_unref(message);
701 dbus_message_unref(message);
703 dbus_message_unref(reply);
708 static int set_network(struct supplicant_task *task,
709 const unsigned char *network, int len,
710 const char *address, const char *security,
711 const char *passphrase)
713 DBusMessage *message, *reply;
714 DBusMessageIter array, dict;
717 DBG("task %p", task);
719 if (task->netpath == NULL)
722 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
723 SUPPLICANT_INTF ".Network", "set");
727 dbus_message_iter_init_append(message, &array);
729 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
730 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
731 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
732 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
734 if (address == NULL) {
735 dbus_uint32_t scan_ssid = 1;
736 connman_dbus_dict_append_variant(&dict, "scan_ssid",
737 DBUS_TYPE_UINT32, &scan_ssid);
739 connman_dbus_dict_append_variant(&dict, "bssid",
740 DBUS_TYPE_STRING, &address);
742 connman_dbus_dict_append_array(&dict, "ssid",
743 DBUS_TYPE_BYTE, &network, len);
745 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
746 g_ascii_strcasecmp(security, "rsn") == 0) {
747 const char *key_mgmt = "WPA-PSK";
748 connman_dbus_dict_append_variant(&dict, "key_mgmt",
749 DBUS_TYPE_STRING, &key_mgmt);
751 if (passphrase && strlen(passphrase) > 0)
752 connman_dbus_dict_append_variant(&dict, "psk",
753 DBUS_TYPE_STRING, &passphrase);
754 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
755 const char *key_mgmt = "NONE", *index = "0";
756 const char *auth_alg = "OPEN SHARED";
757 connman_dbus_dict_append_variant(&dict, "key_mgmt",
758 DBUS_TYPE_STRING, &key_mgmt);
760 connman_dbus_dict_append_variant(&dict, "auth_alg",
761 DBUS_TYPE_STRING, &auth_alg);
764 int size = strlen(passphrase);
765 if (size == 10 || size == 26) {
766 unsigned char *key = malloc(13);
769 memset(tmp, 0, sizeof(tmp));
772 for (i = 0; i < size / 2; i++) {
773 memcpy(tmp, passphrase + (i * 2), 2);
774 key[i] = (unsigned char) strtol(tmp,
777 connman_dbus_dict_append_array(&dict,
778 "wep_key0", DBUS_TYPE_BYTE,
782 connman_dbus_dict_append_variant(&dict,
783 "wep_key0", DBUS_TYPE_STRING,
785 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
786 DBUS_TYPE_STRING, &index);
789 const char *key_mgmt = "NONE";
790 connman_dbus_dict_append_variant(&dict, "key_mgmt",
791 DBUS_TYPE_STRING, &key_mgmt);
794 dbus_message_iter_close_container(&array, &dict);
796 dbus_error_init(&error);
798 reply = dbus_connection_send_with_reply_and_block(connection,
799 message, -1, &error);
801 if (dbus_error_is_set(&error) == TRUE) {
802 connman_error("%s", error.message);
803 dbus_error_free(&error);
805 connman_error("Failed to set network options");
806 dbus_message_unref(message);
810 dbus_message_unref(message);
812 dbus_message_unref(reply);
817 static int initiate_scan(struct supplicant_task *task)
819 DBusMessage *message;
820 DBusPendingCall *call;
822 DBG("task %p", task);
824 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
825 SUPPLICANT_INTF ".Interface", "scan");
829 if (dbus_connection_send_with_reply(connection, message,
830 &call, TIMEOUT) == FALSE) {
831 connman_error("Failed to initiate scan");
832 dbus_message_unref(message);
836 dbus_message_unref(message);
845 { "<hidden>", "hidden" },
846 { "default", "linksys" },
854 { "comcomcom", "3com" },
856 { "Symbol", "symbol" },
857 { "Motorola", "motorola" },
858 { "Wireless" , "wireless" },
863 static char *build_group(const char *addr, const char *name,
864 const unsigned char *ssid, unsigned int ssid_len,
865 const char *mode, const char *security)
873 str = g_string_sized_new((ssid_len * 2) + 24);
877 for (i = 0; special_ssid[i].name; i++) {
878 if (g_strcmp0(special_ssid[i].name, name) == 0) {
879 if (special_ssid[i].value == NULL)
880 g_string_append_printf(str, "%s_%s",
883 g_string_append_printf(str, "%s_%s",
884 special_ssid[i].value, addr);
889 if (ssid_len > 0 && ssid[0] != '\0') {
890 for (i = 0; i < ssid_len; i++)
891 g_string_append_printf(str, "%02x", ssid[i]);
893 g_string_append_printf(str, "hidden_%s", addr);
896 g_string_append_printf(str, "_%s_%s", mode, security);
898 return g_string_free(str, FALSE);
901 static void extract_addr(DBusMessageIter *value,
902 struct supplicant_result *result)
904 DBusMessageIter array;
905 struct ether_addr *eth;
909 dbus_message_iter_recurse(value, &array);
910 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
915 result->addr = g_try_malloc(addr_len);
916 if (result->addr == NULL)
919 memcpy(result->addr, addr, addr_len);
920 result->addr_len = addr_len;
922 result->path = g_try_malloc0(13);
923 if (result->path == NULL)
928 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
929 eth->ether_addr_octet[0],
930 eth->ether_addr_octet[1],
931 eth->ether_addr_octet[2],
932 eth->ether_addr_octet[3],
933 eth->ether_addr_octet[4],
934 eth->ether_addr_octet[5]);
937 static void extract_ssid(DBusMessageIter *value,
938 struct supplicant_result *result)
940 DBusMessageIter array;
944 dbus_message_iter_recurse(value, &array);
945 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
950 result->ssid = g_try_malloc(ssid_len);
951 if (result->ssid == NULL)
954 memcpy(result->ssid, ssid, ssid_len);
955 result->ssid_len = ssid_len;
957 result->name = g_try_malloc0(ssid_len + 1);
958 if (result->name == NULL)
961 for (i = 0; i < ssid_len; i++) {
962 if (g_ascii_isprint(ssid[i]))
963 result->name[i] = ssid[i];
965 result->name[i] = ' ';
969 static void extract_wpaie(DBusMessageIter *value,
970 struct supplicant_result *result)
972 DBusMessageIter array;
976 dbus_message_iter_recurse(value, &array);
977 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
980 result->has_wpa = TRUE;
983 static void extract_rsnie(DBusMessageIter *value,
984 struct supplicant_result *result)
986 DBusMessageIter array;
990 dbus_message_iter_recurse(value, &array);
991 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
994 result->has_rsn = TRUE;
997 static void extract_wpsie(DBusMessageIter *value,
998 struct supplicant_result *result)
1000 DBusMessageIter array;
1004 dbus_message_iter_recurse(value, &array);
1005 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1008 result->has_wps = TRUE;
1011 static void extract_capabilites(DBusMessageIter *value,
1012 struct supplicant_result *result)
1014 dbus_message_iter_get_basic(value, &result->capabilities);
1016 if (result->capabilities & IEEE80211_CAP_ESS)
1017 result->adhoc = FALSE;
1018 else if (result->capabilities & IEEE80211_CAP_IBSS)
1019 result->adhoc = TRUE;
1021 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1022 result->has_wep = TRUE;
1025 static unsigned char calculate_strength(struct supplicant_task *task,
1026 struct supplicant_result *result)
1028 if (task->range->max_qual.qual == 0) {
1029 unsigned char strength;
1031 if (result->level > 0)
1032 strength = 100 - result->level;
1034 strength = 120 + result->level;
1042 return (result->quality * 100) / task->range->max_qual.qual;
1045 static unsigned short calculate_channel(struct supplicant_result *result)
1047 if (result->frequency < 0)
1050 return (result->frequency - 2407) / 5;
1053 static void get_properties(struct supplicant_task *task);
1055 static void properties_reply(DBusPendingCall *call, void *user_data)
1057 struct supplicant_task *task = user_data;
1058 struct supplicant_result result;
1059 struct connman_network *network;
1061 DBusMessageIter array, dict;
1062 unsigned char strength;
1063 unsigned short channel, frequency;
1064 const char *mode, *security;
1067 DBG("task %p", task);
1069 reply = dbus_pending_call_steal_reply(call);
1070 if (reply == NULL) {
1071 get_properties(task);
1075 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1076 dbus_message_unref(reply);
1077 get_properties(task);
1081 memset(&result, 0, sizeof(result));
1082 result.frequency = -1;
1083 result.quality = -1;
1087 dbus_message_iter_init(reply, &array);
1089 dbus_message_iter_recurse(&array, &dict);
1091 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1092 DBusMessageIter entry, value;
1095 dbus_message_iter_recurse(&dict, &entry);
1096 dbus_message_iter_get_basic(&entry, &key);
1098 dbus_message_iter_next(&entry);
1100 dbus_message_iter_recurse(&entry, &value);
1102 //type = dbus_message_iter_get_arg_type(&value);
1103 //dbus_message_iter_get_basic(&value, &val);
1111 * frequency : i (105)
1112 * capabilities : q (113)
1119 if (g_str_equal(key, "bssid") == TRUE)
1120 extract_addr(&value, &result);
1121 else if (g_str_equal(key, "ssid") == TRUE)
1122 extract_ssid(&value, &result);
1123 else if (g_str_equal(key, "wpaie") == TRUE)
1124 extract_wpaie(&value, &result);
1125 else if (g_str_equal(key, "rsnie") == TRUE)
1126 extract_rsnie(&value, &result);
1127 else if (g_str_equal(key, "wpsie") == TRUE)
1128 extract_wpsie(&value, &result);
1129 else if (g_str_equal(key, "capabilities") == TRUE)
1130 extract_capabilites(&value, &result);
1131 else if (g_str_equal(key, "frequency") == TRUE)
1132 dbus_message_iter_get_basic(&value, &result.frequency);
1133 else if (g_str_equal(key, "quality") == TRUE)
1134 dbus_message_iter_get_basic(&value, &result.quality);
1135 else if (g_str_equal(key, "noise") == TRUE)
1136 dbus_message_iter_get_basic(&value, &result.noise);
1137 else if (g_str_equal(key, "level") == TRUE)
1138 dbus_message_iter_get_basic(&value, &result.level);
1139 else if (g_str_equal(key, "maxrate") == TRUE)
1140 dbus_message_iter_get_basic(&value, &result.maxrate);
1142 dbus_message_iter_next(&dict);
1145 if (result.path == NULL)
1148 if (result.path[0] == '\0')
1151 if (result.frequency > 0 && result.frequency < 14)
1152 result.frequency = 2407 + (5 * result.frequency);
1153 else if (result.frequency == 14)
1154 result.frequency = 2484;
1156 strength = calculate_strength(task, &result);
1157 channel = calculate_channel(&result);
1159 frequency = (result.frequency < 0) ? 0 : result.frequency;
1161 if (result.has_rsn == TRUE)
1163 else if (result.has_wpa == TRUE)
1165 else if (result.has_wep == TRUE)
1170 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1172 group = build_group(result.path, result.name,
1173 result.ssid, result.ssid_len,
1176 network = connman_device_get_network(task->device, result.path);
1177 if (network == NULL) {
1180 network = connman_network_create(result.path,
1181 CONNMAN_NETWORK_TYPE_WIFI);
1182 if (network == NULL)
1185 index = connman_device_get_index(task->device);
1186 connman_network_set_index(network, index);
1188 connman_network_set_protocol(network,
1189 CONNMAN_NETWORK_PROTOCOL_IP);
1191 connman_network_set_address(network, result.addr,
1194 if (connman_device_add_network(task->device, network) < 0) {
1195 connman_network_unref(network);
1200 if (result.name != NULL && result.name[0] != '\0')
1201 connman_network_set_name(network, result.name);
1203 connman_network_set_blob(network, "WiFi.SSID",
1204 result.ssid, result.ssid_len);
1206 connman_network_set_string(network, "WiFi.Mode", mode);
1208 DBG("%s (%s %s) strength %d (%s)",
1209 result.name, mode, security, strength,
1210 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1212 connman_network_set_available(network, TRUE);
1213 connman_network_set_strength(network, strength);
1215 connman_network_set_uint16(network, "Frequency", frequency);
1216 connman_network_set_uint16(network, "WiFi.Channel", channel);
1217 connman_network_set_string(network, "WiFi.Security", security);
1219 connman_network_set_group(network, group);
1224 g_free(result.path);
1225 g_free(result.addr);
1226 g_free(result.name);
1227 g_free(result.ssid);
1229 dbus_message_unref(reply);
1231 get_properties(task);
1234 static void get_properties(struct supplicant_task *task)
1236 DBusMessage *message;
1237 DBusPendingCall *call;
1240 path = g_slist_nth_data(task->scan_results, 0);
1244 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1245 SUPPLICANT_INTF ".BSSID",
1248 task->scan_results = g_slist_remove(task->scan_results, path);
1251 if (message == NULL)
1254 if (dbus_connection_send_with_reply(connection, message,
1255 &call, TIMEOUT) == FALSE) {
1256 connman_error("Failed to get network properties");
1257 dbus_message_unref(message);
1262 connman_error("D-Bus connection not available");
1263 dbus_message_unref(message);
1267 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1269 dbus_message_unref(message);
1274 if (task->noscan == FALSE)
1275 connman_device_set_scanning(task->device, FALSE);
1278 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1280 struct supplicant_task *task = user_data;
1286 DBG("task %p", task);
1288 reply = dbus_pending_call_steal_reply(call);
1292 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1295 dbus_error_init(&error);
1297 if (dbus_message_get_args(reply, &error,
1298 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1299 &results, &num_results,
1300 DBUS_TYPE_INVALID) == FALSE) {
1301 if (dbus_error_is_set(&error) == TRUE) {
1302 connman_error("%s", error.message);
1303 dbus_error_free(&error);
1305 connman_error("Wrong arguments for scan result");
1309 if (num_results == 0)
1312 for (i = 0; i < num_results; i++) {
1313 char *path = g_strdup(results[i]);
1317 task->scan_results = g_slist_append(task->scan_results, path);
1320 g_strfreev(results);
1322 dbus_message_unref(reply);
1324 get_properties(task);
1329 dbus_message_unref(reply);
1332 if (task->noscan == FALSE)
1333 connman_device_set_scanning(task->device, FALSE);
1336 static void scan_results_available(struct supplicant_task *task)
1338 DBusMessage *message;
1339 DBusPendingCall *call;
1341 DBG("task %p", task);
1343 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1344 SUPPLICANT_INTF ".Interface",
1346 if (message == NULL)
1349 if (dbus_connection_send_with_reply(connection, message,
1350 &call, TIMEOUT) == FALSE) {
1351 connman_error("Failed to request scan result");
1355 if (task->noscan == FALSE)
1356 connman_device_set_scanning(task->device, TRUE);
1359 connman_error("D-Bus connection not available");
1363 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1366 dbus_message_unref(message);
1369 static enum supplicant_state string2state(const char *state)
1371 if (g_str_equal(state, "INACTIVE") == TRUE)
1372 return WPA_INACTIVE;
1373 else if (g_str_equal(state, "SCANNING") == TRUE)
1374 return WPA_SCANNING;
1375 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1376 return WPA_ASSOCIATING;
1377 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1378 return WPA_ASSOCIATED;
1379 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1380 return WPA_GROUP_HANDSHAKE;
1381 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1382 return WPA_4WAY_HANDSHAKE;
1383 else if (g_str_equal(state, "COMPLETED") == TRUE)
1384 return WPA_COMPLETED;
1385 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1386 return WPA_DISCONNECTED;
1391 static int task_connect(struct supplicant_task *task)
1393 const char *address, *security, *passphrase;
1395 unsigned int ssid_len;
1397 address = connman_network_get_string(task->network, "Address");
1398 security = connman_network_get_string(task->network, "WiFi.Security");
1399 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1401 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1403 DBG("address %s security %s passphrase %s",
1404 address, security, passphrase);
1406 if (security == NULL && passphrase == NULL)
1409 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1414 select_network(task);
1415 disable_network(task);
1417 set_network(task, ssid, ssid_len, address, security, passphrase);
1419 enable_network(task);
1424 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1427 const char *newstate, *oldstate;
1428 enum supplicant_state state;
1430 dbus_error_init(&error);
1432 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1433 DBUS_TYPE_STRING, &oldstate,
1434 DBUS_TYPE_INVALID) == FALSE) {
1435 if (dbus_error_is_set(&error) == TRUE) {
1436 connman_error("%s", error.message);
1437 dbus_error_free(&error);
1439 connman_error("Wrong arguments for state change");
1443 DBG("state %s ==> %s", oldstate, newstate);
1445 state = string2state(newstate);
1446 if (state == WPA_INVALID)
1449 task->state = state;
1451 switch (task->state) {
1453 task->noscan = TRUE;
1454 connman_device_set_scanning(task->device, TRUE);
1456 case WPA_ASSOCIATING:
1457 case WPA_ASSOCIATED:
1458 case WPA_4WAY_HANDSHAKE:
1459 case WPA_GROUP_HANDSHAKE:
1460 task->noscan = TRUE;
1463 case WPA_DISCONNECTED:
1464 task->noscan = FALSE;
1467 task->noscan = FALSE;
1468 connman_device_set_scanning(task->device, FALSE);
1474 if (task->network == NULL)
1477 switch (task->state) {
1480 connman_network_set_connected(task->network, TRUE);
1481 connman_device_set_scanning(task->device, FALSE);
1483 case WPA_DISCONNECTED:
1485 connman_network_set_connected(task->network, FALSE);
1486 connman_device_set_scanning(task->device, FALSE);
1488 case WPA_ASSOCIATING:
1489 connman_network_set_associating(task->network, TRUE);
1492 connman_network_set_associating(task->network, FALSE);
1497 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1498 DBusMessage *msg, void *data)
1500 struct supplicant_task *task;
1501 const char *member, *path;
1503 if (dbus_message_has_interface(msg,
1504 SUPPLICANT_INTF ".Interface") == FALSE)
1505 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1507 member = dbus_message_get_member(msg);
1509 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1511 path = dbus_message_get_path(msg);
1513 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1515 task = find_task_by_path(path);
1517 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1519 DBG("task %p member %s", task, member);
1521 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1522 scan_results_available(task);
1523 else if (g_str_equal(member, "StateChange") == TRUE)
1524 state_change(task, msg);
1526 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1529 static int supplicant_get_range(struct supplicant_task *task)
1534 fd = socket(PF_INET, SOCK_DGRAM, 0);
1538 memset(&wrq, 0, sizeof(struct iwreq));
1539 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
1540 wrq.u.data.pointer = task->range;
1541 wrq.u.data.length = sizeof(struct iw_range);
1543 err = ioctl(fd, SIOCGIWRANGE, &wrq);
1550 int supplicant_start(struct connman_device *device)
1552 struct supplicant_task *task;
1555 DBG("device %p", device);
1557 task = g_try_new0(struct supplicant_task, 1);
1561 task->ifindex = connman_device_get_index(device);
1562 task->ifname = connman_inet_ifname(task->ifindex);
1564 if (task->ifname == NULL) {
1569 task->range = g_try_malloc0(sizeof(struct iw_range));
1570 if (task->range == NULL) {
1575 err = supplicant_get_range(task);
1579 task->device = connman_device_ref(device);
1581 task->created = FALSE;
1582 task->noscan = FALSE;
1583 task->state = WPA_INVALID;
1585 task_list = g_slist_append(task_list, task);
1587 return create_interface(task);
1590 g_free(task->range);
1591 g_free(task->ifname);
1597 int supplicant_stop(struct connman_device *device)
1599 int index = connman_device_get_index(device);
1600 struct supplicant_task *task;
1602 DBG("device %p", device);
1604 task = find_task_by_index(index);
1608 g_free(task->range);
1610 task_list = g_slist_remove(task_list, task);
1612 disable_network(task);
1614 remove_network(task);
1616 return remove_interface(task);
1619 int supplicant_scan(struct connman_device *device)
1621 int index = connman_device_get_index(device);
1622 struct supplicant_task *task;
1625 DBG("device %p", device);
1627 task = find_task_by_index(index);
1631 switch (task->state) {
1634 case WPA_ASSOCIATING:
1635 case WPA_ASSOCIATED:
1636 case WPA_4WAY_HANDSHAKE:
1637 case WPA_GROUP_HANDSHAKE:
1643 err = initiate_scan(task);
1648 int supplicant_connect(struct connman_network *network)
1650 struct supplicant_task *task;
1653 DBG("network %p", network);
1655 index = connman_network_get_index(network);
1657 task = find_task_by_index(index);
1661 task->network = connman_network_ref(network);
1663 return task_connect(task);
1666 int supplicant_disconnect(struct connman_network *network)
1668 struct supplicant_task *task;
1671 DBG("network %p", network);
1673 index = connman_network_get_index(network);
1675 task = find_task_by_index(index);
1679 disable_network(task);
1681 remove_network(task);
1683 connman_network_set_connected(task->network, FALSE);
1685 connman_network_unref(task->network);
1690 static void supplicant_activate(DBusConnection *conn)
1692 DBusMessage *message;
1694 DBG("conn %p", conn);
1696 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1697 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1698 if (message == NULL)
1701 dbus_message_set_no_reply(message, TRUE);
1703 dbus_connection_send(conn, message, NULL);
1705 dbus_message_unref(message);
1708 static GSList *driver_list = NULL;
1710 static void supplicant_probe(DBusConnection *conn, void *user_data)
1714 DBG("conn %p", conn);
1716 for (list = driver_list; list; list = list->next) {
1717 struct supplicant_driver *driver = list->data;
1719 DBG("driver %p name %s", driver, driver->name);
1726 static void supplicant_remove(DBusConnection *conn, void *user_data)
1730 DBG("conn %p", conn);
1732 for (list = driver_list; list; list = list->next) {
1733 struct supplicant_driver *driver = list->data;
1735 DBG("driver %p name %s", driver, driver->name);
1742 static const char *supplicant_rule = "type=signal,"
1743 "interface=" SUPPLICANT_INTF ".Interface";
1746 static int supplicant_create(void)
1748 if (g_slist_length(driver_list) > 0)
1751 connection = connman_dbus_get_connection();
1752 if (connection == NULL)
1755 DBG("connection %p", connection);
1757 if (dbus_connection_add_filter(connection,
1758 supplicant_filter, NULL, NULL) == FALSE) {
1759 connection = connman_dbus_get_connection();
1763 dbus_bus_add_match(connection, supplicant_rule, NULL);
1764 dbus_connection_flush(connection);
1766 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1767 supplicant_probe, supplicant_remove, NULL, NULL);
1772 static void supplicant_destroy(void)
1774 if (g_slist_length(driver_list) > 0)
1777 DBG("connection %p", connection);
1780 g_dbus_remove_watch(connection, watch);
1782 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1783 dbus_connection_flush(connection);
1785 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1787 dbus_connection_unref(connection);
1791 int supplicant_register(struct supplicant_driver *driver)
1795 DBG("driver %p name %s", driver, driver->name);
1797 err = supplicant_create();
1801 driver_list = g_slist_append(driver_list, driver);
1803 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1804 supplicant_probe(connection, NULL);
1806 supplicant_activate(connection);
1811 void supplicant_unregister(struct supplicant_driver *driver)
1813 DBG("driver %p name %s", driver, driver->name);
1815 supplicant_remove(connection, NULL);
1817 driver_list = g_slist_remove(driver_list, driver);
1819 supplicant_destroy();