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;
1202 unsigned int ssid_len;
1204 DBG("task %p", task);
1206 reply = dbus_pending_call_steal_reply(call);
1207 if (reply == NULL) {
1208 get_properties(task);
1212 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1213 dbus_message_unref(reply);
1214 get_properties(task);
1218 memset(&result, 0, sizeof(result));
1219 result.frequency = -1;
1220 result.quality = -1;
1224 dbus_message_iter_init(reply, &array);
1226 dbus_message_iter_recurse(&array, &dict);
1228 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1229 DBusMessageIter entry, value;
1232 dbus_message_iter_recurse(&dict, &entry);
1233 dbus_message_iter_get_basic(&entry, &key);
1235 dbus_message_iter_next(&entry);
1237 dbus_message_iter_recurse(&entry, &value);
1239 //type = dbus_message_iter_get_arg_type(&value);
1240 //dbus_message_iter_get_basic(&value, &val);
1248 * frequency : i (105)
1249 * capabilities : q (113)
1256 if (g_str_equal(key, "bssid") == TRUE)
1257 extract_addr(&value, &result);
1258 else if (g_str_equal(key, "ssid") == TRUE)
1259 extract_ssid(&value, &result);
1260 else if (g_str_equal(key, "wpaie") == TRUE)
1261 extract_wpaie(&value, &result);
1262 else if (g_str_equal(key, "rsnie") == TRUE)
1263 extract_rsnie(&value, &result);
1264 else if (g_str_equal(key, "wpsie") == TRUE)
1265 extract_wpsie(&value, &result);
1266 else if (g_str_equal(key, "capabilities") == TRUE)
1267 extract_capabilites(&value, &result);
1268 else if (g_str_equal(key, "frequency") == TRUE)
1269 dbus_message_iter_get_basic(&value, &result.frequency);
1270 else if (g_str_equal(key, "quality") == TRUE)
1271 dbus_message_iter_get_basic(&value, &result.quality);
1272 else if (g_str_equal(key, "noise") == TRUE)
1273 dbus_message_iter_get_basic(&value, &result.noise);
1274 else if (g_str_equal(key, "level") == TRUE)
1275 dbus_message_iter_get_basic(&value, &result.level);
1276 else if (g_str_equal(key, "maxrate") == TRUE)
1277 dbus_message_iter_get_basic(&value, &result.maxrate);
1279 dbus_message_iter_next(&dict);
1282 if (result.path == NULL)
1285 if (result.path[0] == '\0')
1288 if (result.frequency > 0 && result.frequency < 14)
1289 result.frequency = 2407 + (5 * result.frequency);
1290 else if (result.frequency == 14)
1291 result.frequency = 2484;
1293 strength = calculate_strength(task, &result);
1294 channel = calculate_channel(&result);
1296 frequency = (result.frequency < 0) ? 0 : result.frequency;
1298 if (result.has_rsn == TRUE)
1300 else if (result.has_wpa == TRUE)
1302 else if (result.has_wep == TRUE)
1307 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1309 group = build_group(result.path, result.name,
1310 result.ssid, result.ssid_len,
1313 network = connman_device_get_network(task->device, result.path);
1314 if (network == NULL) {
1317 network = connman_network_create(result.path,
1318 CONNMAN_NETWORK_TYPE_WIFI);
1319 if (network == NULL)
1322 index = connman_device_get_index(task->device);
1323 connman_network_set_index(network, index);
1325 connman_network_set_protocol(network,
1326 CONNMAN_NETWORK_PROTOCOL_IP);
1328 connman_network_set_address(network, result.addr,
1331 if (connman_device_add_network(task->device, network) < 0) {
1332 connman_network_unref(network);
1337 if (result.name != NULL && result.name[0] != '\0')
1338 connman_network_set_name(network, result.name);
1340 if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1341 connman_network_set_blob(network, "WiFi.SSID",
1342 result.ssid, result.ssid_len);
1345 connman_network_set_string(network, "WiFi.Mode", mode);
1347 DBG("%s (%s %s) strength %d (%s)",
1348 result.name, mode, security, strength,
1349 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1351 connman_network_set_available(network, TRUE);
1352 connman_network_set_strength(network, strength);
1354 connman_network_set_uint16(network, "Frequency", frequency);
1355 connman_network_set_uint16(network, "WiFi.Channel", channel);
1356 connman_network_set_string(network, "WiFi.Security", security);
1358 if (result.ssid != NULL)
1359 connman_network_set_group(network, group);
1364 g_free(result.path);
1365 g_free(result.addr);
1366 g_free(result.name);
1367 g_free(result.ssid);
1369 dbus_message_unref(reply);
1371 get_properties(task);
1374 static void get_properties(struct supplicant_task *task)
1376 DBusMessage *message;
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 &task->result_call, TIMEOUT) == FALSE) {
1397 connman_error("Failed to get network properties");
1398 dbus_message_unref(message);
1402 if (task->result_call == NULL) {
1403 connman_error("D-Bus connection not available");
1404 dbus_message_unref(message);
1408 dbus_pending_call_set_notify(task->result_call,
1409 properties_reply, task, NULL);
1411 dbus_message_unref(message);
1416 task->result_call = NULL;
1418 if (task->scanning == TRUE) {
1419 connman_device_set_scanning(task->device, FALSE);
1420 task->scanning = FALSE;
1424 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1426 struct supplicant_task *task = user_data;
1432 DBG("task %p", task);
1434 reply = dbus_pending_call_steal_reply(call);
1438 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1441 dbus_error_init(&error);
1443 if (dbus_message_get_args(reply, &error,
1444 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1445 &results, &num_results,
1446 DBUS_TYPE_INVALID) == FALSE) {
1447 if (dbus_error_is_set(&error) == TRUE) {
1448 connman_error("%s", error.message);
1449 dbus_error_free(&error);
1451 connman_error("Wrong arguments for scan result");
1455 if (num_results == 0)
1458 for (i = 0; i < num_results; i++) {
1459 char *path = g_strdup(results[i]);
1463 task->scan_results = g_slist_append(task->scan_results, path);
1466 g_strfreev(results);
1468 dbus_message_unref(reply);
1470 get_properties(task);
1475 dbus_message_unref(reply);
1478 task->result_call = NULL;
1480 if (task->scanning == TRUE) {
1481 connman_device_set_scanning(task->device, FALSE);
1482 task->scanning = FALSE;
1486 static void scan_results_available(struct supplicant_task *task)
1488 DBusMessage *message;
1490 DBG("task %p", task);
1492 if (task->result_call != NULL)
1495 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1496 SUPPLICANT_INTF ".Interface",
1498 if (message == NULL)
1501 dbus_message_set_auto_start(message, FALSE);
1503 if (dbus_connection_send_with_reply(connection, message,
1504 &task->result_call, TIMEOUT) == FALSE) {
1505 connman_error("Failed to request scan result");
1509 if (task->result_call == NULL) {
1510 connman_error("D-Bus connection not available");
1514 if (task->scanning == TRUE)
1515 connman_device_set_scanning(task->device, TRUE);
1517 dbus_pending_call_set_notify(task->result_call,
1518 scan_results_reply, task, NULL);
1521 dbus_message_unref(message);
1524 static enum supplicant_state string2state(const char *state)
1526 if (g_str_equal(state, "INACTIVE") == TRUE)
1527 return WPA_INACTIVE;
1528 else if (g_str_equal(state, "SCANNING") == TRUE)
1529 return WPA_SCANNING;
1530 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1531 return WPA_ASSOCIATING;
1532 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1533 return WPA_ASSOCIATED;
1534 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1535 return WPA_GROUP_HANDSHAKE;
1536 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1537 return WPA_4WAY_HANDSHAKE;
1538 else if (g_str_equal(state, "COMPLETED") == TRUE)
1539 return WPA_COMPLETED;
1540 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1541 return WPA_DISCONNECTED;
1546 static int task_connect(struct supplicant_task *task)
1548 const char *address, *security, *passphrase;
1550 unsigned int ssid_len;
1553 address = connman_network_get_string(task->network, "Address");
1554 security = connman_network_get_string(task->network, "WiFi.Security");
1555 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1557 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1559 DBG("address %s security %s passphrase %s",
1560 address, security, passphrase);
1562 if (security == NULL && passphrase == NULL)
1565 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1570 select_network(task);
1571 disable_network(task);
1573 set_network(task, ssid, ssid_len, address, security, passphrase);
1575 err = enable_network(task);
1579 return -EINPROGRESS;
1582 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1585 dbus_bool_t scanning;
1587 dbus_error_init(&error);
1589 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1590 DBUS_TYPE_INVALID) == FALSE) {
1591 if (dbus_error_is_set(&error) == TRUE) {
1592 connman_error("%s", error.message);
1593 dbus_error_free(&error);
1595 connman_error("Wrong arguments for scanning");
1599 connman_info("%s scanning %s", task->ifname,
1600 scanning == TRUE ? "started" : "finished");
1603 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1606 const char *newstate, *oldstate;
1607 unsigned char bssid[ETH_ALEN];
1608 unsigned int bssid_len;
1609 enum supplicant_state state;
1611 dbus_error_init(&error);
1613 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1614 DBUS_TYPE_STRING, &oldstate,
1615 DBUS_TYPE_INVALID) == FALSE) {
1616 if (dbus_error_is_set(&error) == TRUE) {
1617 connman_error("%s", error.message);
1618 dbus_error_free(&error);
1620 connman_error("Wrong arguments for state change");
1624 DBG("state %s ==> %s", oldstate, newstate);
1626 connman_info("%s %s", task->ifname, newstate);
1628 state = string2state(newstate);
1629 if (state == WPA_INVALID)
1632 if (task->scanning == TRUE && state != WPA_SCANNING) {
1633 connman_device_set_scanning(task->device, FALSE);
1634 task->scanning = FALSE;
1637 task->state = state;
1639 if (task->network == NULL)
1642 switch (task->state) {
1644 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1645 connman_network_set_address(task->network,
1649 connman_network_set_connected(task->network, TRUE);
1652 case WPA_DISCONNECTED:
1653 disable_network(task);
1656 connman_network_set_connected(task->network, FALSE);
1658 if (task->disconnecting == TRUE) {
1659 connman_network_unref(task->network);
1660 task->disconnecting = FALSE;
1662 if (task->pending_network != NULL) {
1663 task->network = task->pending_network;
1664 task->pending_network = NULL;
1667 task->network = NULL;
1671 case WPA_ASSOCIATING:
1672 connman_network_set_associating(task->network, TRUE);
1676 connman_network_set_connected(task->network, FALSE);
1678 if (task->disconnecting == TRUE) {
1679 connman_network_unref(task->network);
1680 task->disconnecting = FALSE;
1682 if (task->pending_network != NULL) {
1683 task->network = task->pending_network;
1684 task->pending_network = NULL;
1687 task->network = NULL;
1692 connman_network_set_associating(task->network, FALSE);
1697 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1698 DBusMessage *msg, void *data)
1700 struct supplicant_task *task;
1701 const char *member, *path;
1703 if (dbus_message_has_interface(msg,
1704 SUPPLICANT_INTF ".Interface") == FALSE)
1705 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1707 member = dbus_message_get_member(msg);
1709 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1711 path = dbus_message_get_path(msg);
1713 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1715 task = find_task_by_path(path);
1717 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1719 DBG("task %p member %s", task, member);
1721 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1722 scan_results_available(task);
1723 else if (g_str_equal(member, "Scanning") == TRUE)
1724 scanning(task, msg);
1725 else if (g_str_equal(member, "StateChange") == TRUE)
1726 state_change(task, msg);
1728 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1731 int supplicant_start(struct connman_device *device)
1733 struct supplicant_task *task;
1736 DBG("device %p", device);
1738 task = g_try_new0(struct supplicant_task, 1);
1742 task->ifindex = connman_device_get_index(device);
1743 task->ifname = connman_inet_ifname(task->ifindex);
1745 if (task->ifname == NULL) {
1750 task->range = g_try_malloc0(sizeof(struct iw_range));
1751 if (task->range == NULL) {
1756 err = get_range(task);
1760 task->device = connman_device_ref(device);
1762 task->created = FALSE;
1763 task->scanning = FALSE;
1764 task->state = WPA_INVALID;
1765 task->disconnecting = FALSE;
1766 task->pending_network = NULL;
1768 task_list = g_slist_append(task_list, task);
1770 return create_interface(task);
1773 g_free(task->range);
1774 g_free(task->ifname);
1780 int supplicant_stop(struct connman_device *device)
1782 int index = connman_device_get_index(device);
1783 struct supplicant_task *task;
1785 DBG("device %p", device);
1787 task = find_task_by_index(index);
1791 g_free(task->range);
1793 task_list = g_slist_remove(task_list, task);
1795 if (task->scan_call != NULL) {
1796 dbus_pending_call_cancel(task->scan_call);
1797 task->scan_call = NULL;
1800 if (task->result_call != NULL) {
1801 dbus_pending_call_cancel(task->result_call);
1802 task->result_call = NULL;
1805 if (task->scanning == TRUE)
1806 connman_device_set_scanning(task->device, FALSE);
1808 disable_network(task);
1810 remove_network(task);
1812 return remove_interface(task);
1815 int supplicant_scan(struct connman_device *device)
1817 int index = connman_device_get_index(device);
1818 struct supplicant_task *task;
1821 DBG("device %p", device);
1823 task = find_task_by_index(index);
1827 switch (task->state) {
1830 case WPA_ASSOCIATING:
1831 case WPA_ASSOCIATED:
1832 case WPA_4WAY_HANDSHAKE:
1833 case WPA_GROUP_HANDSHAKE:
1839 task->scanning = TRUE;
1841 err = initiate_scan(task);
1843 if (err == -EINPROGRESS)
1846 task->scanning = FALSE;
1850 connman_device_set_scanning(task->device, TRUE);
1855 int supplicant_connect(struct connman_network *network)
1857 struct supplicant_task *task;
1860 DBG("network %p", network);
1862 index = connman_network_get_index(network);
1864 task = find_task_by_index(index);
1868 if (task->disconnecting == TRUE)
1869 task->pending_network = connman_network_ref(network);
1871 task->network = connman_network_ref(network);
1872 return task_connect(task);
1875 return -EINPROGRESS;
1878 int supplicant_disconnect(struct connman_network *network)
1880 struct supplicant_task *task;
1883 DBG("network %p", network);
1885 index = connman_network_get_index(network);
1887 task = find_task_by_index(index);
1891 if (task->disconnecting == TRUE)
1894 disable_network(task);
1896 remove_network(task);
1898 task->disconnecting = TRUE;
1903 static void supplicant_activate(DBusConnection *conn)
1905 DBusMessage *message;
1907 DBG("conn %p", conn);
1909 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1910 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1911 if (message == NULL)
1914 dbus_message_set_no_reply(message, TRUE);
1916 dbus_connection_send(conn, message, NULL);
1918 dbus_message_unref(message);
1921 static GSList *driver_list = NULL;
1923 static void supplicant_probe(DBusConnection *conn, void *user_data)
1927 DBG("conn %p", conn);
1929 for (list = driver_list; list; list = list->next) {
1930 struct supplicant_driver *driver = list->data;
1932 DBG("driver %p name %s", driver, driver->name);
1939 static void supplicant_remove(DBusConnection *conn, void *user_data)
1943 DBG("conn %p", conn);
1945 for (list = driver_list; list; list = list->next) {
1946 struct supplicant_driver *driver = list->data;
1948 DBG("driver %p name %s", driver, driver->name);
1955 static const char *supplicant_rule = "type=signal,"
1956 "interface=" SUPPLICANT_INTF ".Interface";
1959 static int supplicant_create(void)
1961 if (g_slist_length(driver_list) > 0)
1964 connection = connman_dbus_get_connection();
1965 if (connection == NULL)
1968 DBG("connection %p", connection);
1970 if (dbus_connection_add_filter(connection,
1971 supplicant_filter, NULL, NULL) == FALSE) {
1972 connection = connman_dbus_get_connection();
1976 dbus_bus_add_match(connection, supplicant_rule, NULL);
1977 dbus_connection_flush(connection);
1979 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1980 supplicant_probe, supplicant_remove, NULL, NULL);
1985 static void supplicant_destroy(void)
1987 if (g_slist_length(driver_list) > 0)
1990 DBG("connection %p", connection);
1993 g_dbus_remove_watch(connection, watch);
1995 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1996 dbus_connection_flush(connection);
1998 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
2000 dbus_connection_unref(connection);
2004 int supplicant_register(struct supplicant_driver *driver)
2008 DBG("driver %p name %s", driver, driver->name);
2010 err = supplicant_create();
2014 driver_list = g_slist_append(driver_list, driver);
2016 supplicant_activate(connection);
2021 void supplicant_unregister(struct supplicant_driver *driver)
2023 DBG("driver %p name %s", driver, driver->name);
2025 supplicant_remove(connection, NULL);
2027 driver_list = g_slist_remove(driver_list, driver);
2029 supplicant_destroy();