5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
39 #define CONNMAN_API_SUBJECT_TO_CHANGE
40 #include <connman/device.h>
41 #include <connman/option.h>
42 #include <connman/inet.h>
43 #include <connman/dbus.h>
44 #include <connman/log.h>
46 #include "supplicant.h"
50 #define IEEE80211_CAP_ESS 0x0001
51 #define IEEE80211_CAP_IBSS 0x0002
52 #define IEEE80211_CAP_PRIVACY 0x0010
54 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
55 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
56 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
58 /* Taken from "WPA Supplicant - Common definitions" */
59 enum supplicant_state {
61 * WPA_DISCONNECTED - Disconnected state
63 * This state indicates that client is not associated, but is likely to
64 * start looking for an access point. This state is entered when a
70 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
72 * This state is entered if there are no enabled networks in the
73 * configuration. wpa_supplicant is not trying to associate with a new
74 * network and external interaction (e.g., ctrl_iface call to add or
75 * enable a network) is needed to start association.
80 * WPA_SCANNING - Scanning for a network
82 * This state is entered when wpa_supplicant starts scanning for a
88 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
90 * This state is entered when wpa_supplicant has found a suitable BSS
91 * to associate with and the driver is configured to try to associate
92 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
93 * state is entered when the driver is configured to try to associate
94 * with a network using the configured SSID and security policy.
99 * WPA_ASSOCIATED - Association completed
101 * This state is entered when the driver reports that association has
102 * been successfully completed with an AP. If IEEE 802.1X is used
103 * (with or without WPA/WPA2), wpa_supplicant remains in this state
104 * until the IEEE 802.1X/EAPOL authentication has been completed.
109 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
111 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
112 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
113 * frame after association. In case of WPA-EAP, this state is entered
114 * when the IEEE 802.1X/EAPOL authentication has been completed.
119 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
121 * This state is entered when 4-Way Key Handshake has been completed
122 * (i.e., when the supplicant sends out message 4/4) and when Group
123 * Key rekeying is started by the AP (i.e., when supplicant receives
129 * WPA_COMPLETED - All authentication completed
131 * This state is entered when the full authentication process is
132 * completed. In case of WPA2, this happens when the 4-Way Handshake is
133 * successfully completed. With WPA, this state is entered after the
134 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
135 * completed after dynamic keys are received (or if not used, after
136 * the EAP authentication has been completed). With static WEP keys and
137 * plaintext connections, this state is entered when an association
138 * has been completed.
140 * This state indicates that the supplicant has completed its
141 * processing for the association phase and that data connection is
147 * WPA_INVALID - Invalid state (parsing error)
149 * This state is returned if the string input is invalid. It is not
150 * an official wpa_supplicant state.
155 struct supplicant_result {
159 unsigned int addr_len;
161 unsigned int ssid_len;
162 dbus_uint16_t capabilities;
168 dbus_int32_t frequency;
169 dbus_int32_t quality;
172 dbus_int32_t maxrate;
175 struct supplicant_task {
178 struct connman_device *device;
179 struct connman_network *network;
180 struct connman_network *pending_network;
184 enum supplicant_state state;
186 GSList *scan_results;
187 DBusPendingCall *scan_call;
188 struct iw_range *range;
189 gboolean disconnecting;
192 static GSList *task_list = NULL;
194 static DBusConnection *connection;
196 static void free_task(struct supplicant_task *task)
198 DBG("task %p", task);
200 g_free(task->ifname);
205 static struct supplicant_task *find_task_by_index(int index)
209 for (list = task_list; list; list = list->next) {
210 struct supplicant_task *task = list->data;
212 if (task->ifindex == index)
219 static struct supplicant_task *find_task_by_path(const char *path)
223 for (list = task_list; list; list = list->next) {
224 struct supplicant_task *task = list->data;
226 if (g_strcmp0(task->path, path) == 0)
233 static int get_range(struct supplicant_task *task)
238 fd = socket(PF_INET, SOCK_DGRAM, 0);
242 memset(&wrq, 0, sizeof(struct iwreq));
243 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
244 wrq.u.data.pointer = task->range;
245 wrq.u.data.length = sizeof(struct iw_range);
247 err = ioctl(fd, SIOCGIWRANGE, &wrq);
254 static int get_bssid(struct connman_device *device,
255 unsigned char *bssid, unsigned int *bssid_len)
262 ifindex = connman_device_get_index(device);
266 ifname = connman_inet_ifname(ifindex);
270 fd = socket(PF_INET, SOCK_DGRAM, 0);
276 memset(&wrq, 0, sizeof(wrq));
277 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
279 err = ioctl(fd, SIOCGIWAP, &wrq);
287 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
288 *bssid_len = ETH_ALEN;
293 static void add_interface_reply(DBusPendingCall *call, void *user_data)
295 struct supplicant_task *task = user_data;
300 DBG("task %p", task);
302 reply = dbus_pending_call_steal_reply(call);
306 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
309 dbus_error_init(&error);
311 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
312 DBUS_TYPE_INVALID) == FALSE) {
313 if (dbus_error_is_set(&error) == TRUE) {
314 connman_error("%s", error.message);
315 dbus_error_free(&error);
317 connman_error("Wrong arguments for add interface");
321 DBG("path %s", path);
323 task->path = g_strdup(path);
324 task->created = TRUE;
326 connman_device_set_powered(task->device, TRUE);
328 dbus_message_unref(reply);
333 dbus_message_unref(reply);
335 task_list = g_slist_remove(task_list, task);
337 connman_device_unref(task->device);
342 static int add_interface(struct supplicant_task *task)
344 const char *driver = connman_option_get_string("wifi");
345 DBusMessage *message;
346 DBusMessageIter array, dict;
347 DBusPendingCall *call;
349 DBG("task %p", task);
351 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
352 SUPPLICANT_INTF, "addInterface");
356 dbus_message_set_auto_start(message, FALSE);
358 dbus_message_iter_init_append(message, &array);
360 dbus_message_iter_append_basic(&array,
361 DBUS_TYPE_STRING, &task->ifname);
363 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
364 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
365 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
366 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
368 connman_dbus_dict_append_variant(&dict, "driver",
369 DBUS_TYPE_STRING, &driver);
371 dbus_message_iter_close_container(&array, &dict);
373 if (dbus_connection_send_with_reply(connection, message,
374 &call, TIMEOUT) == FALSE) {
375 connman_error("Failed to add interface");
376 dbus_message_unref(message);
381 connman_error("D-Bus connection not available");
382 dbus_message_unref(message);
386 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
388 dbus_message_unref(message);
393 static void get_interface_reply(DBusPendingCall *call, void *user_data)
395 struct supplicant_task *task = user_data;
400 DBG("task %p", task);
402 reply = dbus_pending_call_steal_reply(call);
406 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
411 dbus_error_init(&error);
413 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
414 DBUS_TYPE_INVALID) == FALSE) {
415 if (dbus_error_is_set(&error) == TRUE) {
416 connman_error("%s", error.message);
417 dbus_error_free(&error);
419 connman_error("Wrong arguments for get interface");
423 DBG("path %s", path);
425 task->path = g_strdup(path);
426 task->created = FALSE;
428 connman_device_set_powered(task->device, TRUE);
431 dbus_message_unref(reply);
434 static int create_interface(struct supplicant_task *task)
436 DBusMessage *message;
437 DBusPendingCall *call;
439 DBG("task %p", task);
441 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
442 SUPPLICANT_INTF, "getInterface");
446 dbus_message_set_auto_start(message, FALSE);
448 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
451 if (dbus_connection_send_with_reply(connection, message,
452 &call, TIMEOUT) == FALSE) {
453 connman_error("Failed to get interface");
454 dbus_message_unref(message);
459 connman_error("D-Bus connection not available");
460 dbus_message_unref(message);
464 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
466 dbus_message_unref(message);
471 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
473 struct supplicant_task *task = user_data;
476 DBG("task %p", task);
478 reply = dbus_pending_call_steal_reply(call);
480 connman_device_set_powered(task->device, FALSE);
482 connman_device_unref(task->device);
484 connman_inet_ifdown(task->ifindex);
488 dbus_message_unref(reply);
491 static int remove_interface(struct supplicant_task *task)
493 DBusMessage *message;
494 DBusPendingCall *call;
496 DBG("task %p", task);
499 if (task->created == FALSE) {
500 connman_device_set_powered(task->device, FALSE);
505 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
506 SUPPLICANT_INTF, "removeInterface");
510 dbus_message_set_auto_start(message, FALSE);
512 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
515 if (dbus_connection_send_with_reply(connection, message,
516 &call, TIMEOUT) == FALSE) {
517 connman_error("Failed to remove interface");
518 dbus_message_unref(message);
523 connman_error("D-Bus connection not available");
524 dbus_message_unref(message);
528 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
530 dbus_message_unref(message);
536 static int set_ap_scan(struct supplicant_task *task)
538 DBusMessage *message, *reply;
542 DBG("task %p", task);
544 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
545 SUPPLICANT_INTF ".Interface", "setAPScan");
549 dbus_message_set_auto_start(message, FALSE);
551 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
554 dbus_error_init(&error);
556 reply = dbus_connection_send_with_reply_and_block(connection,
557 message, -1, &error);
559 if (dbus_error_is_set(&error) == TRUE) {
560 connman_error("%s", error.message);
561 dbus_error_free(&error);
563 connman_error("Failed to set AP scan");
564 dbus_message_unref(message);
568 dbus_message_unref(message);
570 dbus_message_unref(reply);
576 static int add_network(struct supplicant_task *task)
578 DBusMessage *message, *reply;
582 DBG("task %p", task);
584 if (task->netpath != NULL)
587 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
588 SUPPLICANT_INTF ".Interface", "addNetwork");
592 dbus_message_set_auto_start(message, FALSE);
594 dbus_error_init(&error);
596 reply = dbus_connection_send_with_reply_and_block(connection,
597 message, -1, &error);
599 if (dbus_error_is_set(&error) == TRUE) {
600 connman_error("%s", error.message);
601 dbus_error_free(&error);
603 connman_error("Failed to add network");
604 dbus_message_unref(message);
608 dbus_message_unref(message);
610 dbus_error_init(&error);
612 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
613 DBUS_TYPE_INVALID) == FALSE) {
614 if (dbus_error_is_set(&error) == TRUE) {
615 connman_error("%s", error.message);
616 dbus_error_free(&error);
618 connman_error("Wrong arguments for network");
619 dbus_message_unref(reply);
623 DBG("path %s", path);
625 task->netpath = g_strdup(path);
627 dbus_message_unref(reply);
632 static int remove_network(struct supplicant_task *task)
634 DBusMessage *message, *reply;
637 DBG("task %p", task);
639 if (task->netpath == NULL)
642 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
643 SUPPLICANT_INTF ".Interface", "removeNetwork");
647 dbus_message_set_auto_start(message, FALSE);
649 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
652 dbus_error_init(&error);
654 reply = dbus_connection_send_with_reply_and_block(connection,
655 message, -1, &error);
657 if (dbus_error_is_set(&error) == TRUE) {
658 connman_error("%s", error.message);
659 dbus_error_free(&error);
661 connman_error("Failed to remove network");
662 dbus_message_unref(message);
666 dbus_message_unref(message);
668 dbus_message_unref(reply);
670 g_free(task->netpath);
671 task->netpath = NULL;
676 static int select_network(struct supplicant_task *task)
678 DBusMessage *message, *reply;
681 DBG("task %p", task);
683 if (task->netpath == NULL)
686 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
687 SUPPLICANT_INTF ".Interface", "selectNetwork");
691 dbus_message_set_auto_start(message, FALSE);
693 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
696 dbus_error_init(&error);
698 reply = dbus_connection_send_with_reply_and_block(connection,
699 message, -1, &error);
701 if (dbus_error_is_set(&error) == TRUE) {
702 connman_error("%s", error.message);
703 dbus_error_free(&error);
705 connman_error("Failed to select network");
706 dbus_message_unref(message);
710 dbus_message_unref(message);
712 dbus_message_unref(reply);
717 static int enable_network(struct supplicant_task *task)
719 DBusMessage *message, *reply;
722 DBG("task %p", task);
724 if (task->netpath == NULL)
727 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
728 SUPPLICANT_INTF ".Network", "enable");
732 dbus_message_set_auto_start(message, FALSE);
734 dbus_error_init(&error);
736 reply = dbus_connection_send_with_reply_and_block(connection,
737 message, -1, &error);
739 if (dbus_error_is_set(&error) == TRUE) {
740 connman_error("%s", error.message);
741 dbus_error_free(&error);
743 connman_error("Failed to enable network");
744 dbus_message_unref(message);
748 dbus_message_unref(message);
750 dbus_message_unref(reply);
755 static int disable_network(struct supplicant_task *task)
757 DBusMessage *message, *reply;
760 DBG("task %p", task);
762 if (task->netpath == NULL)
765 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
766 SUPPLICANT_INTF ".Network", "disable");
770 dbus_message_set_auto_start(message, FALSE);
772 dbus_error_init(&error);
774 reply = dbus_connection_send_with_reply_and_block(connection,
775 message, -1, &error);
777 if (dbus_error_is_set(&error) == TRUE) {
778 connman_error("%s", error.message);
779 dbus_error_free(&error);
781 connman_error("Failed to disable network");
782 dbus_message_unref(message);
786 dbus_message_unref(message);
788 dbus_message_unref(reply);
793 static int set_network(struct supplicant_task *task,
794 const unsigned char *network, int len,
795 const char *address, const char *security,
796 const char *passphrase)
798 DBusMessage *message, *reply;
799 DBusMessageIter array, dict;
801 dbus_uint32_t scan_ssid = 1;
803 DBG("task %p", task);
805 if (task->netpath == NULL)
808 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
809 SUPPLICANT_INTF ".Network", "set");
813 dbus_message_set_auto_start(message, FALSE);
815 dbus_message_iter_init_append(message, &array);
817 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
818 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
819 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
820 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
822 connman_dbus_dict_append_variant(&dict, "scan_ssid",
823 DBUS_TYPE_UINT32, &scan_ssid);
826 connman_dbus_dict_append_variant(&dict, "bssid",
827 DBUS_TYPE_STRING, &address);
829 connman_dbus_dict_append_array(&dict, "ssid",
830 DBUS_TYPE_BYTE, &network, len);
832 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
833 g_ascii_strcasecmp(security, "rsn") == 0) {
834 const char *key_mgmt = "WPA-PSK";
835 connman_dbus_dict_append_variant(&dict, "key_mgmt",
836 DBUS_TYPE_STRING, &key_mgmt);
838 if (passphrase && strlen(passphrase) > 0)
839 connman_dbus_dict_append_variant(&dict, "psk",
840 DBUS_TYPE_STRING, &passphrase);
841 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
842 const char *key_mgmt = "NONE";
843 const char *auth_alg = "OPEN SHARED";
844 const char *key_index = "0";
846 connman_dbus_dict_append_variant(&dict, "auth_alg",
847 DBUS_TYPE_STRING, &auth_alg);
849 connman_dbus_dict_append_variant(&dict, "key_mgmt",
850 DBUS_TYPE_STRING, &key_mgmt);
853 int size = strlen(passphrase);
854 if (size == 10 || size == 26) {
855 unsigned char *key = malloc(13);
858 memset(tmp, 0, sizeof(tmp));
861 for (i = 0; i < size / 2; i++) {
862 memcpy(tmp, passphrase + (i * 2), 2);
863 key[i] = (unsigned char) strtol(tmp,
866 connman_dbus_dict_append_array(&dict,
867 "wep_key0", DBUS_TYPE_BYTE,
871 connman_dbus_dict_append_variant(&dict,
872 "wep_key0", DBUS_TYPE_STRING,
875 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
876 DBUS_TYPE_STRING, &key_index);
879 const char *key_mgmt = "NONE";
880 connman_dbus_dict_append_variant(&dict, "key_mgmt",
881 DBUS_TYPE_STRING, &key_mgmt);
884 dbus_message_iter_close_container(&array, &dict);
886 dbus_error_init(&error);
888 reply = dbus_connection_send_with_reply_and_block(connection,
889 message, -1, &error);
891 if (dbus_error_is_set(&error) == TRUE) {
892 connman_error("%s", error.message);
893 dbus_error_free(&error);
895 connman_error("Failed to set network options");
896 dbus_message_unref(message);
900 dbus_message_unref(message);
902 dbus_message_unref(reply);
907 static void scan_reply(DBusPendingCall *call, void *user_data)
909 struct supplicant_task *task = user_data;
912 DBG("task %p", task);
914 task->scan_call = NULL;
916 reply = dbus_pending_call_steal_reply(call);
920 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
921 connman_device_set_scanning(task->device, FALSE);
925 if (task->scanning == TRUE)
926 connman_device_set_scanning(task->device, TRUE);
929 dbus_message_unref(reply);
933 static int initiate_scan(struct supplicant_task *task)
935 DBusMessage *message;
937 DBG("task %p", task);
939 if (task->scan_call != NULL)
942 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
943 SUPPLICANT_INTF ".Interface", "scan");
947 dbus_message_set_auto_start(message, FALSE);
949 if (dbus_connection_send_with_reply(connection, message,
950 &task->scan_call, TIMEOUT) == FALSE) {
951 connman_error("Failed to initiate scan");
952 dbus_message_unref(message);
956 if (task->scan_call == NULL) {
957 connman_error("D-Bus connection not available");
958 dbus_message_unref(message);
962 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
964 dbus_message_unref(message);
973 { "<hidden>", "hidden" },
974 { "default", "linksys" },
982 { "comcomcom", "3com" },
984 { "Symbol", "symbol" },
985 { "Motorola", "motorola" },
986 { "Wireless" , "wireless" },
991 static char *build_group(const char *addr, const char *name,
992 const unsigned char *ssid, unsigned int ssid_len,
993 const char *mode, const char *security)
1001 str = g_string_sized_new((ssid_len * 2) + 24);
1006 g_string_append_printf(str, "hidden_%s", addr);
1010 for (i = 0; special_ssid[i].name; i++) {
1011 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1012 if (special_ssid[i].value == NULL)
1013 g_string_append_printf(str, "%s_%s",
1016 g_string_append_printf(str, "%s_%s",
1017 special_ssid[i].value, addr);
1022 if (ssid_len > 0 && ssid[0] != '\0') {
1023 for (i = 0; i < ssid_len; i++)
1024 g_string_append_printf(str, "%02x", ssid[i]);
1026 g_string_append_printf(str, "hidden_%s", addr);
1029 g_string_append_printf(str, "_%s_%s", mode, security);
1031 return g_string_free(str, FALSE);
1034 static void extract_addr(DBusMessageIter *value,
1035 struct supplicant_result *result)
1037 DBusMessageIter array;
1038 struct ether_addr eth;
1039 unsigned char *addr;
1042 dbus_message_iter_recurse(value, &array);
1043 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1048 result->addr = g_try_malloc(addr_len);
1049 if (result->addr == NULL)
1052 memcpy(result->addr, addr, addr_len);
1053 result->addr_len = addr_len;
1055 result->path = g_try_malloc0(13);
1056 if (result->path == NULL)
1059 memcpy(ð, addr, sizeof(eth));
1060 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1061 eth.ether_addr_octet[0],
1062 eth.ether_addr_octet[1],
1063 eth.ether_addr_octet[2],
1064 eth.ether_addr_octet[3],
1065 eth.ether_addr_octet[4],
1066 eth.ether_addr_octet[5]);
1069 static void extract_ssid(DBusMessageIter *value,
1070 struct supplicant_result *result)
1072 DBusMessageIter array;
1073 unsigned char *ssid;
1076 dbus_message_iter_recurse(value, &array);
1077 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1082 if (ssid[0] == '\0')
1085 result->ssid = g_try_malloc(ssid_len);
1086 if (result->ssid == NULL)
1089 memcpy(result->ssid, ssid, ssid_len);
1090 result->ssid_len = ssid_len;
1092 result->name = g_try_malloc0(ssid_len + 1);
1093 if (result->name == NULL)
1096 for (i = 0; i < ssid_len; i++) {
1097 if (g_ascii_isprint(ssid[i]))
1098 result->name[i] = ssid[i];
1100 result->name[i] = ' ';
1104 static void extract_wpaie(DBusMessageIter *value,
1105 struct supplicant_result *result)
1107 DBusMessageIter array;
1111 dbus_message_iter_recurse(value, &array);
1112 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1115 result->has_wpa = TRUE;
1118 static void extract_rsnie(DBusMessageIter *value,
1119 struct supplicant_result *result)
1121 DBusMessageIter array;
1125 dbus_message_iter_recurse(value, &array);
1126 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1129 result->has_rsn = TRUE;
1132 static void extract_wpsie(DBusMessageIter *value,
1133 struct supplicant_result *result)
1135 DBusMessageIter array;
1139 dbus_message_iter_recurse(value, &array);
1140 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1143 result->has_wps = TRUE;
1146 static void extract_capabilites(DBusMessageIter *value,
1147 struct supplicant_result *result)
1149 dbus_message_iter_get_basic(value, &result->capabilities);
1151 if (result->capabilities & IEEE80211_CAP_ESS)
1152 result->adhoc = FALSE;
1153 else if (result->capabilities & IEEE80211_CAP_IBSS)
1154 result->adhoc = TRUE;
1156 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1157 result->has_wep = TRUE;
1160 static unsigned char calculate_strength(struct supplicant_task *task,
1161 struct supplicant_result *result)
1163 if (task->range->max_qual.qual == 0) {
1164 unsigned char strength;
1166 if (result->level > 0)
1167 strength = 100 - result->level;
1169 strength = 120 + result->level;
1177 return (result->quality * 100) / task->range->max_qual.qual;
1180 static unsigned short calculate_channel(struct supplicant_result *result)
1182 if (result->frequency < 0)
1185 return (result->frequency - 2407) / 5;
1188 static void get_properties(struct supplicant_task *task);
1190 static void properties_reply(DBusPendingCall *call, void *user_data)
1192 struct supplicant_task *task = user_data;
1193 struct supplicant_result result;
1194 struct connman_network *network;
1196 DBusMessageIter array, dict;
1197 unsigned char strength;
1198 unsigned short channel, frequency;
1199 const char *mode, *security;
1201 unsigned int ssid_len;
1203 DBG("task %p", task);
1205 reply = dbus_pending_call_steal_reply(call);
1206 if (reply == NULL) {
1207 get_properties(task);
1211 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1212 dbus_message_unref(reply);
1213 get_properties(task);
1217 memset(&result, 0, sizeof(result));
1218 result.frequency = -1;
1219 result.quality = -1;
1223 dbus_message_iter_init(reply, &array);
1225 dbus_message_iter_recurse(&array, &dict);
1227 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1228 DBusMessageIter entry, value;
1231 dbus_message_iter_recurse(&dict, &entry);
1232 dbus_message_iter_get_basic(&entry, &key);
1234 dbus_message_iter_next(&entry);
1236 dbus_message_iter_recurse(&entry, &value);
1238 //type = dbus_message_iter_get_arg_type(&value);
1239 //dbus_message_iter_get_basic(&value, &val);
1247 * frequency : i (105)
1248 * capabilities : q (113)
1255 if (g_str_equal(key, "bssid") == TRUE)
1256 extract_addr(&value, &result);
1257 else if (g_str_equal(key, "ssid") == TRUE)
1258 extract_ssid(&value, &result);
1259 else if (g_str_equal(key, "wpaie") == TRUE)
1260 extract_wpaie(&value, &result);
1261 else if (g_str_equal(key, "rsnie") == TRUE)
1262 extract_rsnie(&value, &result);
1263 else if (g_str_equal(key, "wpsie") == TRUE)
1264 extract_wpsie(&value, &result);
1265 else if (g_str_equal(key, "capabilities") == TRUE)
1266 extract_capabilites(&value, &result);
1267 else if (g_str_equal(key, "frequency") == TRUE)
1268 dbus_message_iter_get_basic(&value, &result.frequency);
1269 else if (g_str_equal(key, "quality") == TRUE)
1270 dbus_message_iter_get_basic(&value, &result.quality);
1271 else if (g_str_equal(key, "noise") == TRUE)
1272 dbus_message_iter_get_basic(&value, &result.noise);
1273 else if (g_str_equal(key, "level") == TRUE)
1274 dbus_message_iter_get_basic(&value, &result.level);
1275 else if (g_str_equal(key, "maxrate") == TRUE)
1276 dbus_message_iter_get_basic(&value, &result.maxrate);
1278 dbus_message_iter_next(&dict);
1281 if (result.path == NULL)
1284 if (result.path[0] == '\0')
1287 if (result.frequency > 0 && result.frequency < 14)
1288 result.frequency = 2407 + (5 * result.frequency);
1289 else if (result.frequency == 14)
1290 result.frequency = 2484;
1292 strength = calculate_strength(task, &result);
1293 channel = calculate_channel(&result);
1295 frequency = (result.frequency < 0) ? 0 : result.frequency;
1297 if (result.has_rsn == TRUE)
1299 else if (result.has_wpa == TRUE)
1301 else if (result.has_wep == TRUE)
1306 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1308 group = build_group(result.path, result.name,
1309 result.ssid, result.ssid_len,
1312 network = connman_device_get_network(task->device, result.path);
1313 if (network == NULL) {
1316 network = connman_network_create(result.path,
1317 CONNMAN_NETWORK_TYPE_WIFI);
1318 if (network == NULL)
1321 index = connman_device_get_index(task->device);
1322 connman_network_set_index(network, index);
1324 connman_network_set_protocol(network,
1325 CONNMAN_NETWORK_PROTOCOL_IP);
1327 connman_network_set_address(network, result.addr,
1330 if (connman_device_add_network(task->device, network) < 0) {
1331 connman_network_unref(network);
1336 if (result.name != NULL && result.name[0] != '\0')
1337 connman_network_set_name(network, result.name);
1339 if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1340 connman_network_set_blob(network, "WiFi.SSID",
1341 result.ssid, result.ssid_len);
1344 connman_network_set_string(network, "WiFi.Mode", mode);
1346 DBG("%s (%s %s) strength %d (%s)",
1347 result.name, mode, security, strength,
1348 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1350 connman_network_set_available(network, TRUE);
1351 connman_network_set_strength(network, strength);
1353 connman_network_set_uint16(network, "Frequency", frequency);
1354 connman_network_set_uint16(network, "WiFi.Channel", channel);
1355 connman_network_set_string(network, "WiFi.Security", security);
1357 if (result.ssid != NULL)
1358 connman_network_set_group(network, group);
1363 g_free(result.path);
1364 g_free(result.addr);
1365 g_free(result.name);
1366 g_free(result.ssid);
1368 dbus_message_unref(reply);
1370 get_properties(task);
1373 static void get_properties(struct supplicant_task *task)
1375 DBusMessage *message;
1376 DBusPendingCall *call;
1379 path = g_slist_nth_data(task->scan_results, 0);
1383 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1384 SUPPLICANT_INTF ".BSSID",
1387 task->scan_results = g_slist_remove(task->scan_results, path);
1390 if (message == NULL)
1393 dbus_message_set_auto_start(message, FALSE);
1395 if (dbus_connection_send_with_reply(connection, message,
1396 &call, TIMEOUT) == FALSE) {
1397 connman_error("Failed to get network properties");
1398 dbus_message_unref(message);
1403 connman_error("D-Bus connection not available");
1404 dbus_message_unref(message);
1408 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1410 dbus_message_unref(message);
1415 if (task->scanning == TRUE) {
1416 connman_device_set_scanning(task->device, FALSE);
1417 task->scanning = FALSE;
1421 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1423 struct supplicant_task *task = user_data;
1429 DBG("task %p", task);
1431 reply = dbus_pending_call_steal_reply(call);
1435 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1438 dbus_error_init(&error);
1440 if (dbus_message_get_args(reply, &error,
1441 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1442 &results, &num_results,
1443 DBUS_TYPE_INVALID) == FALSE) {
1444 if (dbus_error_is_set(&error) == TRUE) {
1445 connman_error("%s", error.message);
1446 dbus_error_free(&error);
1448 connman_error("Wrong arguments for scan result");
1452 if (num_results == 0)
1455 for (i = 0; i < num_results; i++) {
1456 char *path = g_strdup(results[i]);
1460 task->scan_results = g_slist_append(task->scan_results, path);
1463 g_strfreev(results);
1465 dbus_message_unref(reply);
1467 get_properties(task);
1472 dbus_message_unref(reply);
1475 if (task->scanning == TRUE) {
1476 connman_device_set_scanning(task->device, FALSE);
1477 task->scanning = FALSE;
1481 static void scan_results_available(struct supplicant_task *task)
1483 DBusMessage *message;
1484 DBusPendingCall *call;
1486 DBG("task %p", task);
1488 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1489 SUPPLICANT_INTF ".Interface",
1491 if (message == NULL)
1494 dbus_message_set_auto_start(message, FALSE);
1496 if (dbus_connection_send_with_reply(connection, message,
1497 &call, TIMEOUT) == FALSE) {
1498 connman_error("Failed to request scan result");
1503 connman_error("D-Bus connection not available");
1507 if (task->scanning == TRUE)
1508 connman_device_set_scanning(task->device, TRUE);
1510 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1513 dbus_message_unref(message);
1516 static enum supplicant_state string2state(const char *state)
1518 if (g_str_equal(state, "INACTIVE") == TRUE)
1519 return WPA_INACTIVE;
1520 else if (g_str_equal(state, "SCANNING") == TRUE)
1521 return WPA_SCANNING;
1522 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1523 return WPA_ASSOCIATING;
1524 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1525 return WPA_ASSOCIATED;
1526 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1527 return WPA_GROUP_HANDSHAKE;
1528 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1529 return WPA_4WAY_HANDSHAKE;
1530 else if (g_str_equal(state, "COMPLETED") == TRUE)
1531 return WPA_COMPLETED;
1532 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1533 return WPA_DISCONNECTED;
1538 static int task_connect(struct supplicant_task *task)
1540 const char *address, *security, *passphrase;
1542 unsigned int ssid_len;
1545 address = connman_network_get_string(task->network, "Address");
1546 security = connman_network_get_string(task->network, "WiFi.Security");
1547 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1549 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1551 DBG("address %s security %s passphrase %s",
1552 address, security, passphrase);
1554 if (security == NULL && passphrase == NULL)
1557 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1562 select_network(task);
1563 disable_network(task);
1565 set_network(task, ssid, ssid_len, address, security, passphrase);
1567 err = enable_network(task);
1571 return -EINPROGRESS;
1574 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1577 const char *newstate, *oldstate;
1578 unsigned char bssid[ETH_ALEN];
1579 unsigned int bssid_len;
1580 enum supplicant_state state;
1582 dbus_error_init(&error);
1584 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1585 DBUS_TYPE_STRING, &oldstate,
1586 DBUS_TYPE_INVALID) == FALSE) {
1587 if (dbus_error_is_set(&error) == TRUE) {
1588 connman_error("%s", error.message);
1589 dbus_error_free(&error);
1591 connman_error("Wrong arguments for state change");
1595 DBG("state %s ==> %s", oldstate, newstate);
1597 state = string2state(newstate);
1598 if (state == WPA_INVALID)
1601 if (task->scanning == TRUE && state != WPA_SCANNING) {
1602 connman_device_set_scanning(task->device, FALSE);
1603 task->scanning = FALSE;
1606 task->state = state;
1608 if (task->network == NULL)
1611 switch (task->state) {
1613 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1614 connman_network_set_address(task->network,
1618 connman_network_set_connected(task->network, TRUE);
1621 case WPA_DISCONNECTED:
1622 disable_network(task);
1625 connman_network_set_connected(task->network, FALSE);
1627 if (task->disconnecting == TRUE) {
1628 connman_network_unref(task->network);
1629 task->disconnecting = FALSE;
1631 if (task->pending_network != NULL) {
1632 task->network = task->pending_network;
1633 task->pending_network = NULL;
1639 case WPA_ASSOCIATING:
1640 connman_network_set_associating(task->network, TRUE);
1644 connman_network_set_connected(task->network, FALSE);
1646 if (task->disconnecting == TRUE) {
1647 connman_network_unref(task->network);
1648 task->disconnecting = FALSE;
1650 if (task->pending_network != NULL) {
1651 task->network = task->pending_network;
1652 task->pending_network = NULL;
1659 connman_network_set_associating(task->network, FALSE);
1664 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1665 DBusMessage *msg, void *data)
1667 struct supplicant_task *task;
1668 const char *member, *path;
1670 if (dbus_message_has_interface(msg,
1671 SUPPLICANT_INTF ".Interface") == FALSE)
1672 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1674 member = dbus_message_get_member(msg);
1676 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1678 path = dbus_message_get_path(msg);
1680 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1682 task = find_task_by_path(path);
1684 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1686 DBG("task %p member %s", task, member);
1688 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1689 scan_results_available(task);
1690 else if (g_str_equal(member, "StateChange") == TRUE)
1691 state_change(task, msg);
1693 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1696 int supplicant_start(struct connman_device *device)
1698 struct supplicant_task *task;
1701 DBG("device %p", device);
1703 task = g_try_new0(struct supplicant_task, 1);
1707 task->ifindex = connman_device_get_index(device);
1708 task->ifname = connman_inet_ifname(task->ifindex);
1710 if (task->ifname == NULL) {
1715 task->range = g_try_malloc0(sizeof(struct iw_range));
1716 if (task->range == NULL) {
1721 err = get_range(task);
1725 task->device = connman_device_ref(device);
1727 task->created = FALSE;
1728 task->scanning = FALSE;
1729 task->state = WPA_INVALID;
1730 task->disconnecting = FALSE;
1731 task->pending_network = NULL;
1733 task_list = g_slist_append(task_list, task);
1735 return create_interface(task);
1738 g_free(task->range);
1739 g_free(task->ifname);
1745 int supplicant_stop(struct connman_device *device)
1747 int index = connman_device_get_index(device);
1748 struct supplicant_task *task;
1750 DBG("device %p", device);
1752 task = find_task_by_index(index);
1756 g_free(task->range);
1758 task_list = g_slist_remove(task_list, task);
1760 if (task->scan_call != NULL) {
1761 dbus_pending_call_cancel(task->scan_call);
1762 task->scan_call = NULL;
1765 if (task->scanning == TRUE)
1766 connman_device_set_scanning(task->device, FALSE);
1768 connman_device_set_scanning(task->device, FALSE);
1770 disable_network(task);
1772 remove_network(task);
1774 return remove_interface(task);
1777 int supplicant_scan(struct connman_device *device)
1779 int index = connman_device_get_index(device);
1780 struct supplicant_task *task;
1783 DBG("device %p", device);
1785 task = find_task_by_index(index);
1789 switch (task->state) {
1792 case WPA_ASSOCIATING:
1793 case WPA_ASSOCIATED:
1794 case WPA_4WAY_HANDSHAKE:
1795 case WPA_GROUP_HANDSHAKE:
1801 task->scanning = TRUE;
1803 err = initiate_scan(task);
1805 if (err == -EINPROGRESS)
1808 task->scanning = FALSE;
1812 connman_device_set_scanning(task->device, TRUE);
1817 int supplicant_connect(struct connman_network *network)
1819 struct supplicant_task *task;
1822 DBG("network %p", network);
1824 index = connman_network_get_index(network);
1826 task = find_task_by_index(index);
1830 if (task->disconnecting == TRUE)
1831 task->pending_network = connman_network_ref(network);
1833 task->network = connman_network_ref(network);
1834 return task_connect(task);
1837 return -EINPROGRESS;
1840 int supplicant_disconnect(struct connman_network *network)
1842 struct supplicant_task *task;
1845 DBG("network %p", network);
1847 index = connman_network_get_index(network);
1849 task = find_task_by_index(index);
1853 if (task->disconnecting == TRUE)
1856 disable_network(task);
1858 remove_network(task);
1860 task->disconnecting = TRUE;
1865 static void supplicant_activate(DBusConnection *conn)
1867 DBusMessage *message;
1869 DBG("conn %p", conn);
1871 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1872 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1873 if (message == NULL)
1876 dbus_message_set_no_reply(message, TRUE);
1878 dbus_connection_send(conn, message, NULL);
1880 dbus_message_unref(message);
1883 static GSList *driver_list = NULL;
1885 static void supplicant_probe(DBusConnection *conn, void *user_data)
1889 DBG("conn %p", conn);
1891 for (list = driver_list; list; list = list->next) {
1892 struct supplicant_driver *driver = list->data;
1894 DBG("driver %p name %s", driver, driver->name);
1901 static void supplicant_remove(DBusConnection *conn, void *user_data)
1905 DBG("conn %p", conn);
1907 for (list = driver_list; list; list = list->next) {
1908 struct supplicant_driver *driver = list->data;
1910 DBG("driver %p name %s", driver, driver->name);
1917 static const char *supplicant_rule = "type=signal,"
1918 "interface=" SUPPLICANT_INTF ".Interface";
1921 static int supplicant_create(void)
1923 if (g_slist_length(driver_list) > 0)
1926 connection = connman_dbus_get_connection();
1927 if (connection == NULL)
1930 DBG("connection %p", connection);
1932 if (dbus_connection_add_filter(connection,
1933 supplicant_filter, NULL, NULL) == FALSE) {
1934 connection = connman_dbus_get_connection();
1938 dbus_bus_add_match(connection, supplicant_rule, NULL);
1939 dbus_connection_flush(connection);
1941 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1942 supplicant_probe, supplicant_remove, NULL, NULL);
1947 static void supplicant_destroy(void)
1949 if (g_slist_length(driver_list) > 0)
1952 DBG("connection %p", connection);
1955 g_dbus_remove_watch(connection, watch);
1957 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1958 dbus_connection_flush(connection);
1960 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1962 dbus_connection_unref(connection);
1966 int supplicant_register(struct supplicant_driver *driver)
1970 DBG("driver %p name %s", driver, driver->name);
1972 err = supplicant_create();
1976 driver_list = g_slist_append(driver_list, driver);
1978 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1979 supplicant_probe(connection, NULL);
1981 supplicant_activate(connection);
1986 void supplicant_unregister(struct supplicant_driver *driver)
1988 DBG("driver %p name %s", driver, driver->name);
1990 supplicant_remove(connection, NULL);
1992 driver_list = g_slist_remove(driver_list, driver);
1994 supplicant_destroy();