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 DBusPendingCall *result_call;
189 struct iw_range *range;
190 gboolean disconnecting;
193 static GSList *task_list = NULL;
195 static DBusConnection *connection;
197 static void free_task(struct supplicant_task *task)
199 DBG("task %p", task);
201 g_free(task->ifname);
206 static struct supplicant_task *find_task_by_index(int index)
210 for (list = task_list; list; list = list->next) {
211 struct supplicant_task *task = list->data;
213 if (task->ifindex == index)
220 static struct supplicant_task *find_task_by_path(const char *path)
224 for (list = task_list; list; list = list->next) {
225 struct supplicant_task *task = list->data;
227 if (g_strcmp0(task->path, path) == 0)
234 static int get_range(struct supplicant_task *task)
239 fd = socket(PF_INET, SOCK_DGRAM, 0);
243 memset(&wrq, 0, sizeof(struct iwreq));
244 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
245 wrq.u.data.pointer = task->range;
246 wrq.u.data.length = sizeof(struct iw_range);
248 err = ioctl(fd, SIOCGIWRANGE, &wrq);
255 static int get_bssid(struct connman_device *device,
256 unsigned char *bssid, unsigned int *bssid_len)
263 ifindex = connman_device_get_index(device);
267 ifname = connman_inet_ifname(ifindex);
271 fd = socket(PF_INET, SOCK_DGRAM, 0);
277 memset(&wrq, 0, sizeof(wrq));
278 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
280 err = ioctl(fd, SIOCGIWAP, &wrq);
288 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
289 *bssid_len = ETH_ALEN;
294 static void add_interface_reply(DBusPendingCall *call, void *user_data)
296 struct supplicant_task *task = user_data;
301 DBG("task %p", task);
303 reply = dbus_pending_call_steal_reply(call);
307 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
310 dbus_error_init(&error);
312 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
313 DBUS_TYPE_INVALID) == FALSE) {
314 if (dbus_error_is_set(&error) == TRUE) {
315 connman_error("%s", error.message);
316 dbus_error_free(&error);
318 connman_error("Wrong arguments for add interface");
322 DBG("path %s", path);
324 task->path = g_strdup(path);
325 task->created = TRUE;
327 connman_device_set_powered(task->device, TRUE);
329 dbus_message_unref(reply);
334 dbus_message_unref(reply);
336 task_list = g_slist_remove(task_list, task);
338 connman_device_unref(task->device);
343 static int add_interface(struct supplicant_task *task)
345 const char *driver = connman_option_get_string("wifi");
346 DBusMessage *message;
347 DBusMessageIter array, dict;
348 DBusPendingCall *call;
350 DBG("task %p", task);
352 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
353 SUPPLICANT_INTF, "addInterface");
357 dbus_message_set_auto_start(message, FALSE);
359 dbus_message_iter_init_append(message, &array);
361 dbus_message_iter_append_basic(&array,
362 DBUS_TYPE_STRING, &task->ifname);
364 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
365 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
366 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
367 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
369 connman_dbus_dict_append_variant(&dict, "driver",
370 DBUS_TYPE_STRING, &driver);
372 dbus_message_iter_close_container(&array, &dict);
374 if (dbus_connection_send_with_reply(connection, message,
375 &call, TIMEOUT) == FALSE) {
376 connman_error("Failed to add interface");
377 dbus_message_unref(message);
382 connman_error("D-Bus connection not available");
383 dbus_message_unref(message);
387 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
389 dbus_message_unref(message);
394 static void get_interface_reply(DBusPendingCall *call, void *user_data)
396 struct supplicant_task *task = user_data;
401 DBG("task %p", task);
403 reply = dbus_pending_call_steal_reply(call);
407 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
412 dbus_error_init(&error);
414 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
415 DBUS_TYPE_INVALID) == FALSE) {
416 if (dbus_error_is_set(&error) == TRUE) {
417 connman_error("%s", error.message);
418 dbus_error_free(&error);
420 connman_error("Wrong arguments for get interface");
424 DBG("path %s", path);
426 task->path = g_strdup(path);
427 task->created = FALSE;
429 connman_device_set_powered(task->device, TRUE);
432 dbus_message_unref(reply);
435 static int create_interface(struct supplicant_task *task)
437 DBusMessage *message;
438 DBusPendingCall *call;
440 DBG("task %p", task);
442 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
443 SUPPLICANT_INTF, "getInterface");
447 dbus_message_set_auto_start(message, FALSE);
449 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
452 if (dbus_connection_send_with_reply(connection, message,
453 &call, TIMEOUT) == FALSE) {
454 connman_error("Failed to get interface");
455 dbus_message_unref(message);
460 connman_error("D-Bus connection not available");
461 dbus_message_unref(message);
465 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
467 dbus_message_unref(message);
472 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
474 struct supplicant_task *task = user_data;
477 DBG("task %p", task);
479 reply = dbus_pending_call_steal_reply(call);
481 connman_device_set_powered(task->device, FALSE);
483 connman_device_unref(task->device);
485 connman_inet_ifdown(task->ifindex);
489 dbus_message_unref(reply);
492 static int remove_interface(struct supplicant_task *task)
494 DBusMessage *message;
495 DBusPendingCall *call;
497 DBG("task %p", task);
500 if (task->created == FALSE) {
501 connman_device_set_powered(task->device, FALSE);
506 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
507 SUPPLICANT_INTF, "removeInterface");
511 dbus_message_set_auto_start(message, FALSE);
513 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
516 if (dbus_connection_send_with_reply(connection, message,
517 &call, TIMEOUT) == FALSE) {
518 connman_error("Failed to remove interface");
519 dbus_message_unref(message);
524 connman_error("D-Bus connection not available");
525 dbus_message_unref(message);
529 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
531 dbus_message_unref(message);
537 static int set_ap_scan(struct supplicant_task *task)
539 DBusMessage *message, *reply;
543 DBG("task %p", task);
545 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
546 SUPPLICANT_INTF ".Interface", "setAPScan");
550 dbus_message_set_auto_start(message, FALSE);
552 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
555 dbus_error_init(&error);
557 reply = dbus_connection_send_with_reply_and_block(connection,
558 message, -1, &error);
560 if (dbus_error_is_set(&error) == TRUE) {
561 connman_error("%s", error.message);
562 dbus_error_free(&error);
564 connman_error("Failed to set AP scan");
565 dbus_message_unref(message);
569 dbus_message_unref(message);
571 dbus_message_unref(reply);
577 static int add_network(struct supplicant_task *task)
579 DBusMessage *message, *reply;
583 DBG("task %p", task);
585 if (task->netpath != NULL)
588 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
589 SUPPLICANT_INTF ".Interface", "addNetwork");
593 dbus_message_set_auto_start(message, FALSE);
595 dbus_error_init(&error);
597 reply = dbus_connection_send_with_reply_and_block(connection,
598 message, -1, &error);
600 if (dbus_error_is_set(&error) == TRUE) {
601 connman_error("%s", error.message);
602 dbus_error_free(&error);
604 connman_error("Failed to add network");
605 dbus_message_unref(message);
609 dbus_message_unref(message);
611 dbus_error_init(&error);
613 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
614 DBUS_TYPE_INVALID) == FALSE) {
615 if (dbus_error_is_set(&error) == TRUE) {
616 connman_error("%s", error.message);
617 dbus_error_free(&error);
619 connman_error("Wrong arguments for network");
620 dbus_message_unref(reply);
624 DBG("path %s", path);
626 task->netpath = g_strdup(path);
628 dbus_message_unref(reply);
633 static int remove_network(struct supplicant_task *task)
635 DBusMessage *message, *reply;
638 DBG("task %p", task);
640 if (task->netpath == NULL)
643 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
644 SUPPLICANT_INTF ".Interface", "removeNetwork");
648 dbus_message_set_auto_start(message, FALSE);
650 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
653 dbus_error_init(&error);
655 reply = dbus_connection_send_with_reply_and_block(connection,
656 message, -1, &error);
658 if (dbus_error_is_set(&error) == TRUE) {
659 connman_error("%s", error.message);
660 dbus_error_free(&error);
662 connman_error("Failed to remove network");
663 dbus_message_unref(message);
667 dbus_message_unref(message);
669 dbus_message_unref(reply);
671 g_free(task->netpath);
672 task->netpath = NULL;
677 static int select_network(struct supplicant_task *task)
679 DBusMessage *message, *reply;
682 DBG("task %p", task);
684 if (task->netpath == NULL)
687 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
688 SUPPLICANT_INTF ".Interface", "selectNetwork");
692 dbus_message_set_auto_start(message, FALSE);
694 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
697 dbus_error_init(&error);
699 reply = dbus_connection_send_with_reply_and_block(connection,
700 message, -1, &error);
702 if (dbus_error_is_set(&error) == TRUE) {
703 connman_error("%s", error.message);
704 dbus_error_free(&error);
706 connman_error("Failed to select network");
707 dbus_message_unref(message);
711 dbus_message_unref(message);
713 dbus_message_unref(reply);
718 static int enable_network(struct supplicant_task *task)
720 DBusMessage *message, *reply;
723 DBG("task %p", task);
725 if (task->netpath == NULL)
728 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
729 SUPPLICANT_INTF ".Network", "enable");
733 dbus_message_set_auto_start(message, FALSE);
735 dbus_error_init(&error);
737 reply = dbus_connection_send_with_reply_and_block(connection,
738 message, -1, &error);
740 if (dbus_error_is_set(&error) == TRUE) {
741 connman_error("%s", error.message);
742 dbus_error_free(&error);
744 connman_error("Failed to enable network");
745 dbus_message_unref(message);
749 dbus_message_unref(message);
751 dbus_message_unref(reply);
756 static int disable_network(struct supplicant_task *task)
758 DBusMessage *message, *reply;
761 DBG("task %p", task);
763 if (task->netpath == NULL)
766 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
767 SUPPLICANT_INTF ".Network", "disable");
771 dbus_message_set_auto_start(message, FALSE);
773 dbus_error_init(&error);
775 reply = dbus_connection_send_with_reply_and_block(connection,
776 message, -1, &error);
778 if (dbus_error_is_set(&error) == TRUE) {
779 connman_error("%s", error.message);
780 dbus_error_free(&error);
782 connman_error("Failed to disable network");
783 dbus_message_unref(message);
787 dbus_message_unref(message);
789 dbus_message_unref(reply);
794 static int set_network(struct supplicant_task *task,
795 const unsigned char *network, int len,
796 const char *address, const char *security,
797 const char *passphrase)
799 DBusMessage *message, *reply;
800 DBusMessageIter array, dict;
802 dbus_uint32_t scan_ssid = 1;
804 DBG("task %p", task);
806 if (task->netpath == NULL)
809 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
810 SUPPLICANT_INTF ".Network", "set");
814 dbus_message_set_auto_start(message, FALSE);
816 dbus_message_iter_init_append(message, &array);
818 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
819 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
820 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
821 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
823 connman_dbus_dict_append_variant(&dict, "scan_ssid",
824 DBUS_TYPE_UINT32, &scan_ssid);
827 connman_dbus_dict_append_variant(&dict, "bssid",
828 DBUS_TYPE_STRING, &address);
830 connman_dbus_dict_append_array(&dict, "ssid",
831 DBUS_TYPE_BYTE, &network, len);
833 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
834 g_ascii_strcasecmp(security, "rsn") == 0) {
835 const char *key_mgmt = "WPA-PSK";
836 connman_dbus_dict_append_variant(&dict, "key_mgmt",
837 DBUS_TYPE_STRING, &key_mgmt);
839 if (passphrase && strlen(passphrase) > 0)
840 connman_dbus_dict_append_variant(&dict, "psk",
841 DBUS_TYPE_STRING, &passphrase);
842 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
843 const char *key_mgmt = "NONE";
844 const char *auth_alg = "OPEN SHARED";
845 const char *key_index = "0";
847 connman_dbus_dict_append_variant(&dict, "auth_alg",
848 DBUS_TYPE_STRING, &auth_alg);
850 connman_dbus_dict_append_variant(&dict, "key_mgmt",
851 DBUS_TYPE_STRING, &key_mgmt);
854 int size = strlen(passphrase);
855 if (size == 10 || size == 26) {
856 unsigned char *key = malloc(13);
859 memset(tmp, 0, sizeof(tmp));
862 for (i = 0; i < size / 2; i++) {
863 memcpy(tmp, passphrase + (i * 2), 2);
864 key[i] = (unsigned char) strtol(tmp,
867 connman_dbus_dict_append_array(&dict,
868 "wep_key0", DBUS_TYPE_BYTE,
872 connman_dbus_dict_append_variant(&dict,
873 "wep_key0", DBUS_TYPE_STRING,
876 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
877 DBUS_TYPE_STRING, &key_index);
880 const char *key_mgmt = "NONE";
881 connman_dbus_dict_append_variant(&dict, "key_mgmt",
882 DBUS_TYPE_STRING, &key_mgmt);
885 dbus_message_iter_close_container(&array, &dict);
887 dbus_error_init(&error);
889 reply = dbus_connection_send_with_reply_and_block(connection,
890 message, -1, &error);
892 if (dbus_error_is_set(&error) == TRUE) {
893 connman_error("%s", error.message);
894 dbus_error_free(&error);
896 connman_error("Failed to set network options");
897 dbus_message_unref(message);
901 dbus_message_unref(message);
903 dbus_message_unref(reply);
908 static void scan_reply(DBusPendingCall *call, void *user_data)
910 struct supplicant_task *task = user_data;
913 DBG("task %p", task);
915 task->scan_call = NULL;
917 reply = dbus_pending_call_steal_reply(call);
921 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
922 connman_device_set_scanning(task->device, FALSE);
926 if (task->scanning == TRUE)
927 connman_device_set_scanning(task->device, TRUE);
930 dbus_message_unref(reply);
934 static int initiate_scan(struct supplicant_task *task)
936 DBusMessage *message;
938 DBG("task %p", task);
940 if (task->scan_call != NULL)
943 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
944 SUPPLICANT_INTF ".Interface", "scan");
948 dbus_message_set_auto_start(message, FALSE);
950 if (dbus_connection_send_with_reply(connection, message,
951 &task->scan_call, TIMEOUT) == FALSE) {
952 connman_error("Failed to initiate scan");
953 dbus_message_unref(message);
957 if (task->scan_call == NULL) {
958 connman_error("D-Bus connection not available");
959 dbus_message_unref(message);
963 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
965 dbus_message_unref(message);
974 { "<hidden>", "hidden" },
975 { "default", "linksys" },
983 { "comcomcom", "3com" },
985 { "Symbol", "symbol" },
986 { "Motorola", "motorola" },
987 { "Wireless" , "wireless" },
992 static char *build_group(const char *addr, const char *name,
993 const unsigned char *ssid, unsigned int ssid_len,
994 const char *mode, const char *security)
1002 str = g_string_sized_new((ssid_len * 2) + 24);
1007 g_string_append_printf(str, "hidden_%s", addr);
1011 for (i = 0; special_ssid[i].name; i++) {
1012 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1013 if (special_ssid[i].value == NULL)
1014 g_string_append_printf(str, "%s_%s",
1017 g_string_append_printf(str, "%s_%s",
1018 special_ssid[i].value, addr);
1023 if (ssid_len > 0 && ssid[0] != '\0') {
1024 for (i = 0; i < ssid_len; i++)
1025 g_string_append_printf(str, "%02x", ssid[i]);
1027 g_string_append_printf(str, "hidden_%s", addr);
1030 g_string_append_printf(str, "_%s_%s", mode, security);
1032 return g_string_free(str, FALSE);
1035 static void extract_addr(DBusMessageIter *value,
1036 struct supplicant_result *result)
1038 DBusMessageIter array;
1039 struct ether_addr eth;
1040 unsigned char *addr;
1043 dbus_message_iter_recurse(value, &array);
1044 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1049 result->addr = g_try_malloc(addr_len);
1050 if (result->addr == NULL)
1053 memcpy(result->addr, addr, addr_len);
1054 result->addr_len = addr_len;
1056 result->path = g_try_malloc0(13);
1057 if (result->path == NULL)
1060 memcpy(ð, addr, sizeof(eth));
1061 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1062 eth.ether_addr_octet[0],
1063 eth.ether_addr_octet[1],
1064 eth.ether_addr_octet[2],
1065 eth.ether_addr_octet[3],
1066 eth.ether_addr_octet[4],
1067 eth.ether_addr_octet[5]);
1070 static void extract_ssid(DBusMessageIter *value,
1071 struct supplicant_result *result)
1073 DBusMessageIter array;
1074 unsigned char *ssid;
1077 dbus_message_iter_recurse(value, &array);
1078 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1083 if (ssid[0] == '\0')
1086 result->ssid = g_try_malloc(ssid_len);
1087 if (result->ssid == NULL)
1090 memcpy(result->ssid, ssid, ssid_len);
1091 result->ssid_len = ssid_len;
1093 result->name = g_try_malloc0(ssid_len + 1);
1094 if (result->name == NULL)
1097 for (i = 0; i < ssid_len; i++) {
1098 if (g_ascii_isprint(ssid[i]))
1099 result->name[i] = ssid[i];
1101 result->name[i] = ' ';
1105 static void extract_wpaie(DBusMessageIter *value,
1106 struct supplicant_result *result)
1108 DBusMessageIter array;
1112 dbus_message_iter_recurse(value, &array);
1113 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1116 result->has_wpa = TRUE;
1119 static void extract_rsnie(DBusMessageIter *value,
1120 struct supplicant_result *result)
1122 DBusMessageIter array;
1126 dbus_message_iter_recurse(value, &array);
1127 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1130 result->has_rsn = TRUE;
1133 static void extract_wpsie(DBusMessageIter *value,
1134 struct supplicant_result *result)
1136 DBusMessageIter array;
1140 dbus_message_iter_recurse(value, &array);
1141 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1144 result->has_wps = TRUE;
1147 static void extract_capabilites(DBusMessageIter *value,
1148 struct supplicant_result *result)
1150 dbus_message_iter_get_basic(value, &result->capabilities);
1152 if (result->capabilities & IEEE80211_CAP_ESS)
1153 result->adhoc = FALSE;
1154 else if (result->capabilities & IEEE80211_CAP_IBSS)
1155 result->adhoc = TRUE;
1157 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1158 result->has_wep = TRUE;
1161 static unsigned char calculate_strength(struct supplicant_task *task,
1162 struct supplicant_result *result)
1164 if (task->range->max_qual.qual == 0) {
1165 unsigned char strength;
1167 if (result->level > 0)
1168 strength = 100 - result->level;
1170 strength = 120 + result->level;
1178 return (result->quality * 100) / task->range->max_qual.qual;
1181 static unsigned short calculate_channel(struct supplicant_result *result)
1183 if (result->frequency < 0)
1186 return (result->frequency - 2407) / 5;
1189 static void get_properties(struct supplicant_task *task);
1191 static void properties_reply(DBusPendingCall *call, void *user_data)
1193 struct supplicant_task *task = user_data;
1194 struct supplicant_result result;
1195 struct connman_network *network;
1197 DBusMessageIter array, dict;
1198 unsigned char strength;
1199 unsigned short channel, frequency;
1200 const char *mode, *security;
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 connman_network_set_blob(network, "WiFi.SSID",
1340 result.ssid, result.ssid_len);
1342 connman_network_set_string(network, "WiFi.Mode", mode);
1344 DBG("%s (%s %s) strength %d (%s)",
1345 result.name, mode, security, strength,
1346 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1348 connman_network_set_available(network, TRUE);
1349 connman_network_set_strength(network, strength);
1351 connman_network_set_uint16(network, "Frequency", frequency);
1352 connman_network_set_uint16(network, "WiFi.Channel", channel);
1353 connman_network_set_string(network, "WiFi.Security", security);
1355 if (result.ssid != NULL)
1356 connman_network_set_group(network, group);
1361 g_free(result.path);
1362 g_free(result.addr);
1363 g_free(result.name);
1364 g_free(result.ssid);
1366 dbus_message_unref(reply);
1368 get_properties(task);
1371 static void get_properties(struct supplicant_task *task)
1373 DBusMessage *message;
1376 path = g_slist_nth_data(task->scan_results, 0);
1380 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1381 SUPPLICANT_INTF ".BSSID",
1384 task->scan_results = g_slist_remove(task->scan_results, path);
1387 if (message == NULL)
1390 dbus_message_set_auto_start(message, FALSE);
1392 if (dbus_connection_send_with_reply(connection, message,
1393 &task->result_call, TIMEOUT) == FALSE) {
1394 connman_error("Failed to get network properties");
1395 dbus_message_unref(message);
1399 if (task->result_call == NULL) {
1400 connman_error("D-Bus connection not available");
1401 dbus_message_unref(message);
1405 dbus_pending_call_set_notify(task->result_call,
1406 properties_reply, task, NULL);
1408 dbus_message_unref(message);
1413 task->result_call = NULL;
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 task->result_call = NULL;
1477 if (task->scanning == TRUE) {
1478 connman_device_set_scanning(task->device, FALSE);
1479 task->scanning = FALSE;
1483 static void scan_results_available(struct supplicant_task *task)
1485 DBusMessage *message;
1487 DBG("task %p", task);
1489 if (task->result_call != NULL)
1492 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1493 SUPPLICANT_INTF ".Interface",
1495 if (message == NULL)
1498 dbus_message_set_auto_start(message, FALSE);
1500 if (dbus_connection_send_with_reply(connection, message,
1501 &task->result_call, TIMEOUT) == FALSE) {
1502 connman_error("Failed to request scan result");
1506 if (task->result_call == NULL) {
1507 connman_error("D-Bus connection not available");
1511 if (task->scanning == TRUE)
1512 connman_device_set_scanning(task->device, TRUE);
1514 dbus_pending_call_set_notify(task->result_call,
1515 scan_results_reply, task, NULL);
1518 dbus_message_unref(message);
1521 static enum supplicant_state string2state(const char *state)
1523 if (g_str_equal(state, "INACTIVE") == TRUE)
1524 return WPA_INACTIVE;
1525 else if (g_str_equal(state, "SCANNING") == TRUE)
1526 return WPA_SCANNING;
1527 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1528 return WPA_ASSOCIATING;
1529 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1530 return WPA_ASSOCIATED;
1531 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1532 return WPA_GROUP_HANDSHAKE;
1533 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1534 return WPA_4WAY_HANDSHAKE;
1535 else if (g_str_equal(state, "COMPLETED") == TRUE)
1536 return WPA_COMPLETED;
1537 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1538 return WPA_DISCONNECTED;
1543 static int task_connect(struct supplicant_task *task)
1545 const char *address, *security, *passphrase;
1547 unsigned int ssid_len;
1550 address = connman_network_get_string(task->network, "Address");
1551 security = connman_network_get_string(task->network, "WiFi.Security");
1552 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1554 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1556 DBG("address %s security %s passphrase %s",
1557 address, security, passphrase);
1559 if (security == NULL && passphrase == NULL)
1562 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1567 select_network(task);
1568 disable_network(task);
1570 set_network(task, ssid, ssid_len, address, security, passphrase);
1572 err = enable_network(task);
1576 return -EINPROGRESS;
1579 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1582 dbus_bool_t scanning;
1584 dbus_error_init(&error);
1586 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1587 DBUS_TYPE_INVALID) == FALSE) {
1588 if (dbus_error_is_set(&error) == TRUE) {
1589 connman_error("%s", error.message);
1590 dbus_error_free(&error);
1592 connman_error("Wrong arguments for scanning");
1596 connman_info("%s scanning %s", task->ifname,
1597 scanning == TRUE ? "started" : "finished");
1600 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1603 const char *newstate, *oldstate;
1604 unsigned char bssid[ETH_ALEN];
1605 unsigned int bssid_len;
1606 enum supplicant_state state;
1608 dbus_error_init(&error);
1610 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1611 DBUS_TYPE_STRING, &oldstate,
1612 DBUS_TYPE_INVALID) == FALSE) {
1613 if (dbus_error_is_set(&error) == TRUE) {
1614 connman_error("%s", error.message);
1615 dbus_error_free(&error);
1617 connman_error("Wrong arguments for state change");
1621 DBG("state %s ==> %s", oldstate, newstate);
1623 connman_info("%s %s", task->ifname, newstate);
1625 state = string2state(newstate);
1626 if (state == WPA_INVALID)
1629 if (task->scanning == TRUE && state != WPA_SCANNING) {
1630 connman_device_set_scanning(task->device, FALSE);
1631 task->scanning = FALSE;
1634 task->state = state;
1636 if (task->network == NULL)
1639 switch (task->state) {
1641 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1642 connman_network_set_address(task->network,
1646 connman_network_set_connected(task->network, TRUE);
1649 case WPA_DISCONNECTED:
1650 disable_network(task);
1653 connman_network_set_connected(task->network, FALSE);
1655 if (task->disconnecting == TRUE) {
1656 connman_network_unref(task->network);
1657 task->disconnecting = FALSE;
1659 if (task->pending_network != NULL) {
1660 task->network = task->pending_network;
1661 task->pending_network = NULL;
1664 task->network = NULL;
1668 case WPA_ASSOCIATING:
1669 connman_network_set_associating(task->network, TRUE);
1673 connman_network_set_connected(task->network, FALSE);
1675 if (task->disconnecting == TRUE) {
1676 connman_network_unref(task->network);
1677 task->disconnecting = FALSE;
1679 if (task->pending_network != NULL) {
1680 task->network = task->pending_network;
1681 task->pending_network = NULL;
1684 task->network = NULL;
1689 connman_network_set_associating(task->network, FALSE);
1694 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1695 DBusMessage *msg, void *data)
1697 struct supplicant_task *task;
1698 const char *member, *path;
1700 if (dbus_message_has_interface(msg,
1701 SUPPLICANT_INTF ".Interface") == FALSE)
1702 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1704 member = dbus_message_get_member(msg);
1706 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1708 path = dbus_message_get_path(msg);
1710 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1712 task = find_task_by_path(path);
1714 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1716 DBG("task %p member %s", task, member);
1718 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1719 scan_results_available(task);
1720 else if (g_str_equal(member, "Scanning") == TRUE)
1721 scanning(task, msg);
1722 else if (g_str_equal(member, "StateChange") == TRUE)
1723 state_change(task, msg);
1725 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1728 int supplicant_start(struct connman_device *device)
1730 struct supplicant_task *task;
1733 DBG("device %p", device);
1735 task = g_try_new0(struct supplicant_task, 1);
1739 task->ifindex = connman_device_get_index(device);
1740 task->ifname = connman_inet_ifname(task->ifindex);
1742 if (task->ifname == NULL) {
1747 task->range = g_try_malloc0(sizeof(struct iw_range));
1748 if (task->range == NULL) {
1753 err = get_range(task);
1757 task->device = connman_device_ref(device);
1759 task->created = FALSE;
1760 task->scanning = FALSE;
1761 task->state = WPA_INVALID;
1762 task->disconnecting = FALSE;
1763 task->pending_network = NULL;
1765 task_list = g_slist_append(task_list, task);
1767 return create_interface(task);
1770 g_free(task->range);
1771 g_free(task->ifname);
1777 int supplicant_stop(struct connman_device *device)
1779 int index = connman_device_get_index(device);
1780 struct supplicant_task *task;
1782 DBG("device %p", device);
1784 task = find_task_by_index(index);
1788 g_free(task->range);
1790 task_list = g_slist_remove(task_list, task);
1792 if (task->scan_call != NULL) {
1793 dbus_pending_call_cancel(task->scan_call);
1794 task->scan_call = NULL;
1797 if (task->result_call != NULL) {
1798 dbus_pending_call_cancel(task->result_call);
1799 task->result_call = NULL;
1802 if (task->scanning == TRUE)
1803 connman_device_set_scanning(task->device, FALSE);
1805 disable_network(task);
1807 remove_network(task);
1809 return remove_interface(task);
1812 int supplicant_scan(struct connman_device *device)
1814 int index = connman_device_get_index(device);
1815 struct supplicant_task *task;
1818 DBG("device %p", device);
1820 task = find_task_by_index(index);
1824 switch (task->state) {
1827 case WPA_ASSOCIATING:
1828 case WPA_ASSOCIATED:
1829 case WPA_4WAY_HANDSHAKE:
1830 case WPA_GROUP_HANDSHAKE:
1836 task->scanning = TRUE;
1838 err = initiate_scan(task);
1840 if (err == -EINPROGRESS)
1843 task->scanning = FALSE;
1847 connman_device_set_scanning(task->device, TRUE);
1852 int supplicant_connect(struct connman_network *network)
1854 struct supplicant_task *task;
1857 DBG("network %p", network);
1859 index = connman_network_get_index(network);
1861 task = find_task_by_index(index);
1865 if (task->disconnecting == TRUE)
1866 task->pending_network = connman_network_ref(network);
1868 task->network = connman_network_ref(network);
1869 return task_connect(task);
1872 return -EINPROGRESS;
1875 int supplicant_disconnect(struct connman_network *network)
1877 struct supplicant_task *task;
1880 DBG("network %p", network);
1882 index = connman_network_get_index(network);
1884 task = find_task_by_index(index);
1888 if (task->disconnecting == TRUE)
1891 disable_network(task);
1893 remove_network(task);
1895 task->disconnecting = TRUE;
1900 static void supplicant_activate(DBusConnection *conn)
1902 DBusMessage *message;
1904 DBG("conn %p", conn);
1906 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1907 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1908 if (message == NULL)
1911 dbus_message_set_no_reply(message, TRUE);
1913 dbus_connection_send(conn, message, NULL);
1915 dbus_message_unref(message);
1918 static GSList *driver_list = NULL;
1920 static void supplicant_probe(DBusConnection *conn, void *user_data)
1924 DBG("conn %p", conn);
1926 for (list = driver_list; list; list = list->next) {
1927 struct supplicant_driver *driver = list->data;
1929 DBG("driver %p name %s", driver, driver->name);
1936 static void supplicant_remove(DBusConnection *conn, void *user_data)
1940 DBG("conn %p", conn);
1942 for (list = driver_list; list; list = list->next) {
1943 struct supplicant_driver *driver = list->data;
1945 DBG("driver %p name %s", driver, driver->name);
1952 static const char *supplicant_rule = "type=signal,"
1953 "interface=" SUPPLICANT_INTF ".Interface";
1956 static int supplicant_create(void)
1958 if (g_slist_length(driver_list) > 0)
1961 connection = connman_dbus_get_connection();
1962 if (connection == NULL)
1965 DBG("connection %p", connection);
1967 if (dbus_connection_add_filter(connection,
1968 supplicant_filter, NULL, NULL) == FALSE) {
1969 connection = connman_dbus_get_connection();
1973 dbus_bus_add_match(connection, supplicant_rule, NULL);
1974 dbus_connection_flush(connection);
1976 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1977 supplicant_probe, supplicant_remove, NULL, NULL);
1982 static void supplicant_destroy(void)
1984 if (g_slist_length(driver_list) > 0)
1987 DBG("connection %p", connection);
1990 g_dbus_remove_watch(connection, watch);
1992 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1993 dbus_connection_flush(connection);
1995 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1997 dbus_connection_unref(connection);
2001 int supplicant_register(struct supplicant_driver *driver)
2005 DBG("driver %p name %s", driver, driver->name);
2007 err = supplicant_create();
2011 driver_list = g_slist_append(driver_list, driver);
2013 supplicant_activate(connection);
2018 void supplicant_unregister(struct supplicant_driver *driver)
2020 DBG("driver %p name %s", driver, driver->name);
2022 supplicant_remove(connection, NULL);
2024 driver_list = g_slist_remove(driver_list, driver);
2026 supplicant_destroy();