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 struct iw_range *range;
188 gboolean disconnecting;
191 static GSList *task_list = NULL;
193 static DBusConnection *connection;
195 static void free_task(struct supplicant_task *task)
197 DBG("task %p", task);
199 g_free(task->ifname);
204 static struct supplicant_task *find_task_by_index(int index)
208 for (list = task_list; list; list = list->next) {
209 struct supplicant_task *task = list->data;
211 if (task->ifindex == index)
218 static struct supplicant_task *find_task_by_path(const char *path)
222 for (list = task_list; list; list = list->next) {
223 struct supplicant_task *task = list->data;
225 if (g_strcmp0(task->path, path) == 0)
232 static int get_range(struct supplicant_task *task)
237 fd = socket(PF_INET, SOCK_DGRAM, 0);
241 memset(&wrq, 0, sizeof(struct iwreq));
242 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
243 wrq.u.data.pointer = task->range;
244 wrq.u.data.length = sizeof(struct iw_range);
246 err = ioctl(fd, SIOCGIWRANGE, &wrq);
254 static char *get_bssid(struct connman_device *device)
257 unsigned char ioctl_bssid[ETH_ALEN];
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(ioctl_bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
290 bssid = g_try_malloc0(13);
294 snprintf(bssid, 13, "%02x%02x%02x%02x%02x%02x",
295 ioctl_bssid[0], ioctl_bssid[1],
296 ioctl_bssid[2], ioctl_bssid[3],
297 ioctl_bssid[4], ioctl_bssid[5]);
303 static void add_interface_reply(DBusPendingCall *call, void *user_data)
305 struct supplicant_task *task = user_data;
310 DBG("task %p", task);
312 reply = dbus_pending_call_steal_reply(call);
316 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
319 dbus_error_init(&error);
321 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
322 DBUS_TYPE_INVALID) == FALSE) {
323 if (dbus_error_is_set(&error) == TRUE) {
324 connman_error("%s", error.message);
325 dbus_error_free(&error);
327 connman_error("Wrong arguments for add interface");
331 DBG("path %s", path);
333 task->path = g_strdup(path);
334 task->created = TRUE;
336 connman_device_set_powered(task->device, TRUE);
338 dbus_message_unref(reply);
343 task_list = g_slist_remove(task_list, task);
345 connman_device_unref(task->device);
350 static int add_interface(struct supplicant_task *task)
352 const char *driver = connman_option_get_string("wifi");
353 DBusMessage *message;
354 DBusMessageIter array, dict;
355 DBusPendingCall *call;
357 DBG("task %p", task);
359 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
360 SUPPLICANT_INTF, "addInterface");
364 dbus_message_iter_init_append(message, &array);
366 dbus_message_iter_append_basic(&array,
367 DBUS_TYPE_STRING, &task->ifname);
369 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
370 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
371 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
372 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
374 connman_dbus_dict_append_variant(&dict, "driver",
375 DBUS_TYPE_STRING, &driver);
377 dbus_message_iter_close_container(&array, &dict);
379 if (dbus_connection_send_with_reply(connection, message,
380 &call, TIMEOUT) == FALSE) {
381 connman_error("Failed to add interface");
382 dbus_message_unref(message);
387 connman_error("D-Bus connection not available");
388 dbus_message_unref(message);
392 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
394 dbus_message_unref(message);
399 static void get_interface_reply(DBusPendingCall *call, void *user_data)
401 struct supplicant_task *task = user_data;
406 DBG("task %p", task);
408 reply = dbus_pending_call_steal_reply(call);
412 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
417 dbus_error_init(&error);
419 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
420 DBUS_TYPE_INVALID) == FALSE) {
421 if (dbus_error_is_set(&error) == TRUE) {
422 connman_error("%s", error.message);
423 dbus_error_free(&error);
425 connman_error("Wrong arguments for get interface");
429 DBG("path %s", path);
431 task->path = g_strdup(path);
432 task->created = FALSE;
434 connman_device_set_powered(task->device, TRUE);
437 dbus_message_unref(reply);
440 static int create_interface(struct supplicant_task *task)
442 DBusMessage *message;
443 DBusPendingCall *call;
445 DBG("task %p", task);
447 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
448 SUPPLICANT_INTF, "getInterface");
452 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
455 if (dbus_connection_send_with_reply(connection, message,
456 &call, TIMEOUT) == FALSE) {
457 connman_error("Failed to get interface");
458 dbus_message_unref(message);
463 connman_error("D-Bus connection not available");
464 dbus_message_unref(message);
468 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
470 dbus_message_unref(message);
475 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
477 struct supplicant_task *task = user_data;
480 DBG("task %p", task);
482 reply = dbus_pending_call_steal_reply(call);
484 connman_device_set_powered(task->device, FALSE);
486 connman_device_unref(task->device);
488 connman_inet_ifdown(task->ifindex);
492 dbus_message_unref(reply);
495 static int remove_interface(struct supplicant_task *task)
497 DBusMessage *message;
498 DBusPendingCall *call;
500 DBG("task %p", task);
502 if (task->created == FALSE) {
503 connman_device_set_powered(task->device, FALSE);
507 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
508 SUPPLICANT_INTF, "removeInterface");
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_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
552 dbus_error_init(&error);
554 reply = dbus_connection_send_with_reply_and_block(connection,
555 message, -1, &error);
557 if (dbus_error_is_set(&error) == TRUE) {
558 connman_error("%s", error.message);
559 dbus_error_free(&error);
561 connman_error("Failed to set AP scan");
562 dbus_message_unref(message);
566 dbus_message_unref(message);
568 dbus_message_unref(reply);
574 static int add_network(struct supplicant_task *task)
576 DBusMessage *message, *reply;
580 DBG("task %p", task);
582 if (task->netpath != NULL)
585 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
586 SUPPLICANT_INTF ".Interface", "addNetwork");
590 dbus_error_init(&error);
592 reply = dbus_connection_send_with_reply_and_block(connection,
593 message, -1, &error);
595 if (dbus_error_is_set(&error) == TRUE) {
596 connman_error("%s", error.message);
597 dbus_error_free(&error);
599 connman_error("Failed to add network");
600 dbus_message_unref(message);
604 dbus_message_unref(message);
606 dbus_error_init(&error);
608 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
609 DBUS_TYPE_INVALID) == FALSE) {
610 if (dbus_error_is_set(&error) == TRUE) {
611 connman_error("%s", error.message);
612 dbus_error_free(&error);
614 connman_error("Wrong arguments for network");
615 dbus_message_unref(reply);
619 DBG("path %s", path);
621 task->netpath = g_strdup(path);
623 dbus_message_unref(reply);
628 static int remove_network(struct supplicant_task *task)
630 DBusMessage *message, *reply;
633 DBG("task %p", task);
635 if (task->netpath == NULL)
638 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
639 SUPPLICANT_INTF ".Interface", "removeNetwork");
643 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
646 dbus_error_init(&error);
648 reply = dbus_connection_send_with_reply_and_block(connection,
649 message, -1, &error);
651 if (dbus_error_is_set(&error) == TRUE) {
652 connman_error("%s", error.message);
653 dbus_error_free(&error);
655 connman_error("Failed to remove network");
656 dbus_message_unref(message);
660 dbus_message_unref(message);
662 dbus_message_unref(reply);
664 g_free(task->netpath);
665 task->netpath = NULL;
670 static int select_network(struct supplicant_task *task)
672 DBusMessage *message, *reply;
675 DBG("task %p", task);
677 if (task->netpath == NULL)
680 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
681 SUPPLICANT_INTF ".Interface", "selectNetwork");
685 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
688 dbus_error_init(&error);
690 reply = dbus_connection_send_with_reply_and_block(connection,
691 message, -1, &error);
693 if (dbus_error_is_set(&error) == TRUE) {
694 connman_error("%s", error.message);
695 dbus_error_free(&error);
697 connman_error("Failed to select network");
698 dbus_message_unref(message);
702 dbus_message_unref(message);
704 dbus_message_unref(reply);
709 static int enable_network(struct supplicant_task *task)
711 DBusMessage *message, *reply;
714 DBG("task %p", task);
716 if (task->netpath == NULL)
719 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
720 SUPPLICANT_INTF ".Network", "enable");
724 dbus_error_init(&error);
726 reply = dbus_connection_send_with_reply_and_block(connection,
727 message, -1, &error);
729 if (dbus_error_is_set(&error) == TRUE) {
730 connman_error("%s", error.message);
731 dbus_error_free(&error);
733 connman_error("Failed to enable network");
734 dbus_message_unref(message);
738 dbus_message_unref(message);
740 dbus_message_unref(reply);
745 static int disable_network(struct supplicant_task *task)
747 DBusMessage *message, *reply;
750 DBG("task %p", task);
752 if (task->netpath == NULL)
755 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
756 SUPPLICANT_INTF ".Network", "disable");
760 dbus_error_init(&error);
762 reply = dbus_connection_send_with_reply_and_block(connection,
763 message, -1, &error);
765 if (dbus_error_is_set(&error) == TRUE) {
766 connman_error("%s", error.message);
767 dbus_error_free(&error);
769 connman_error("Failed to disable network");
770 dbus_message_unref(message);
774 dbus_message_unref(message);
776 dbus_message_unref(reply);
781 static int set_network(struct supplicant_task *task,
782 const unsigned char *network, int len,
783 const char *address, const char *security,
784 const char *passphrase)
786 DBusMessage *message, *reply;
787 DBusMessageIter array, dict;
789 dbus_uint32_t scan_ssid = 1;
791 DBG("task %p", task);
793 if (task->netpath == NULL)
796 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
797 SUPPLICANT_INTF ".Network", "set");
801 dbus_message_iter_init_append(message, &array);
803 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
804 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
805 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
806 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
808 connman_dbus_dict_append_variant(&dict, "scan_ssid",
809 DBUS_TYPE_UINT32, &scan_ssid);
812 connman_dbus_dict_append_variant(&dict, "bssid",
813 DBUS_TYPE_STRING, &address);
815 connman_dbus_dict_append_array(&dict, "ssid",
816 DBUS_TYPE_BYTE, &network, len);
818 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
819 g_ascii_strcasecmp(security, "rsn") == 0) {
820 const char *key_mgmt = "WPA-PSK";
821 connman_dbus_dict_append_variant(&dict, "key_mgmt",
822 DBUS_TYPE_STRING, &key_mgmt);
824 if (passphrase && strlen(passphrase) > 0)
825 connman_dbus_dict_append_variant(&dict, "psk",
826 DBUS_TYPE_STRING, &passphrase);
827 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
828 const char *key_mgmt = "NONE";
829 const char *auth_alg = "OPEN SHARED";
830 const char *key_index = "0";
832 connman_dbus_dict_append_variant(&dict, "auth_alg",
833 DBUS_TYPE_STRING, &auth_alg);
835 connman_dbus_dict_append_variant(&dict, "key_mgmt",
836 DBUS_TYPE_STRING, &key_mgmt);
839 int size = strlen(passphrase);
840 if (size == 10 || size == 26) {
841 unsigned char *key = malloc(13);
844 memset(tmp, 0, sizeof(tmp));
847 for (i = 0; i < size / 2; i++) {
848 memcpy(tmp, passphrase + (i * 2), 2);
849 key[i] = (unsigned char) strtol(tmp,
852 connman_dbus_dict_append_array(&dict,
853 "wep_key0", DBUS_TYPE_BYTE,
857 connman_dbus_dict_append_variant(&dict,
858 "wep_key0", DBUS_TYPE_STRING,
861 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
862 DBUS_TYPE_STRING, &key_index);
865 const char *key_mgmt = "NONE";
866 connman_dbus_dict_append_variant(&dict, "key_mgmt",
867 DBUS_TYPE_STRING, &key_mgmt);
870 dbus_message_iter_close_container(&array, &dict);
872 dbus_error_init(&error);
874 reply = dbus_connection_send_with_reply_and_block(connection,
875 message, -1, &error);
877 if (dbus_error_is_set(&error) == TRUE) {
878 connman_error("%s", error.message);
879 dbus_error_free(&error);
881 connman_error("Failed to set network options");
882 dbus_message_unref(message);
886 dbus_message_unref(message);
888 dbus_message_unref(reply);
893 static int initiate_scan(struct supplicant_task *task)
895 DBusMessage *message;
896 DBusPendingCall *call;
898 DBG("task %p", task);
900 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
901 SUPPLICANT_INTF ".Interface", "scan");
905 if (dbus_connection_send_with_reply(connection, message,
906 &call, TIMEOUT) == FALSE) {
907 connman_error("Failed to initiate scan");
908 dbus_message_unref(message);
912 dbus_message_unref(message);
921 { "<hidden>", "hidden" },
922 { "default", "linksys" },
930 { "comcomcom", "3com" },
932 { "Symbol", "symbol" },
933 { "Motorola", "motorola" },
934 { "Wireless" , "wireless" },
939 static char *build_group(const char *addr, const char *name,
940 const unsigned char *ssid, unsigned int ssid_len,
941 const char *mode, const char *security)
949 str = g_string_sized_new((ssid_len * 2) + 24);
954 g_string_append_printf(str, "hidden_%s", addr);
958 for (i = 0; special_ssid[i].name; i++) {
959 if (g_strcmp0(special_ssid[i].name, name) == 0) {
960 if (special_ssid[i].value == NULL)
961 g_string_append_printf(str, "%s_%s",
964 g_string_append_printf(str, "%s_%s",
965 special_ssid[i].value, addr);
970 if (ssid_len > 0 && ssid[0] != '\0') {
971 for (i = 0; i < ssid_len; i++)
972 g_string_append_printf(str, "%02x", ssid[i]);
974 g_string_append_printf(str, "hidden_%s", addr);
977 g_string_append_printf(str, "_%s_%s", mode, security);
979 return g_string_free(str, FALSE);
982 static void extract_addr(DBusMessageIter *value,
983 struct supplicant_result *result)
985 DBusMessageIter array;
986 struct ether_addr eth;
990 dbus_message_iter_recurse(value, &array);
991 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
996 result->addr = g_try_malloc(addr_len);
997 if (result->addr == NULL)
1000 memcpy(result->addr, addr, addr_len);
1001 result->addr_len = addr_len;
1003 result->path = g_try_malloc0(13);
1004 if (result->path == NULL)
1007 memcpy(ð, addr, sizeof(eth));
1008 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1009 eth.ether_addr_octet[0],
1010 eth.ether_addr_octet[1],
1011 eth.ether_addr_octet[2],
1012 eth.ether_addr_octet[3],
1013 eth.ether_addr_octet[4],
1014 eth.ether_addr_octet[5]);
1017 static void extract_ssid(DBusMessageIter *value,
1018 struct supplicant_result *result)
1020 DBusMessageIter array;
1021 unsigned char *ssid;
1024 dbus_message_iter_recurse(value, &array);
1025 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1030 if (ssid[0] == '\0')
1033 result->ssid = g_try_malloc(ssid_len);
1034 if (result->ssid == NULL)
1037 memcpy(result->ssid, ssid, ssid_len);
1038 result->ssid_len = ssid_len;
1040 result->name = g_try_malloc0(ssid_len + 1);
1041 if (result->name == NULL)
1044 for (i = 0; i < ssid_len; i++) {
1045 if (g_ascii_isprint(ssid[i]))
1046 result->name[i] = ssid[i];
1048 result->name[i] = ' ';
1052 static void extract_wpaie(DBusMessageIter *value,
1053 struct supplicant_result *result)
1055 DBusMessageIter array;
1059 dbus_message_iter_recurse(value, &array);
1060 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1063 result->has_wpa = TRUE;
1066 static void extract_rsnie(DBusMessageIter *value,
1067 struct supplicant_result *result)
1069 DBusMessageIter array;
1073 dbus_message_iter_recurse(value, &array);
1074 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1077 result->has_rsn = TRUE;
1080 static void extract_wpsie(DBusMessageIter *value,
1081 struct supplicant_result *result)
1083 DBusMessageIter array;
1087 dbus_message_iter_recurse(value, &array);
1088 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1091 result->has_wps = TRUE;
1094 static void extract_capabilites(DBusMessageIter *value,
1095 struct supplicant_result *result)
1097 dbus_message_iter_get_basic(value, &result->capabilities);
1099 if (result->capabilities & IEEE80211_CAP_ESS)
1100 result->adhoc = FALSE;
1101 else if (result->capabilities & IEEE80211_CAP_IBSS)
1102 result->adhoc = TRUE;
1104 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1105 result->has_wep = TRUE;
1108 static unsigned char calculate_strength(struct supplicant_task *task,
1109 struct supplicant_result *result)
1111 if (task->range->max_qual.qual == 0) {
1112 unsigned char strength;
1114 if (result->level > 0)
1115 strength = 100 - result->level;
1117 strength = 120 + result->level;
1125 return (result->quality * 100) / task->range->max_qual.qual;
1128 static unsigned short calculate_channel(struct supplicant_result *result)
1130 if (result->frequency < 0)
1133 return (result->frequency - 2407) / 5;
1136 static void get_properties(struct supplicant_task *task);
1138 static void properties_reply(DBusPendingCall *call, void *user_data)
1140 struct supplicant_task *task = user_data;
1141 struct supplicant_result result;
1142 struct connman_network *network;
1144 DBusMessageIter array, dict;
1145 unsigned char strength;
1146 unsigned short channel, frequency;
1147 const char *mode, *security;
1149 unsigned int ssid_len;
1151 DBG("task %p", task);
1153 reply = dbus_pending_call_steal_reply(call);
1154 if (reply == NULL) {
1155 get_properties(task);
1159 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1160 dbus_message_unref(reply);
1161 get_properties(task);
1165 memset(&result, 0, sizeof(result));
1166 result.frequency = -1;
1167 result.quality = -1;
1171 dbus_message_iter_init(reply, &array);
1173 dbus_message_iter_recurse(&array, &dict);
1175 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1176 DBusMessageIter entry, value;
1179 dbus_message_iter_recurse(&dict, &entry);
1180 dbus_message_iter_get_basic(&entry, &key);
1182 dbus_message_iter_next(&entry);
1184 dbus_message_iter_recurse(&entry, &value);
1186 //type = dbus_message_iter_get_arg_type(&value);
1187 //dbus_message_iter_get_basic(&value, &val);
1195 * frequency : i (105)
1196 * capabilities : q (113)
1203 if (g_str_equal(key, "bssid") == TRUE)
1204 extract_addr(&value, &result);
1205 else if (g_str_equal(key, "ssid") == TRUE)
1206 extract_ssid(&value, &result);
1207 else if (g_str_equal(key, "wpaie") == TRUE)
1208 extract_wpaie(&value, &result);
1209 else if (g_str_equal(key, "rsnie") == TRUE)
1210 extract_rsnie(&value, &result);
1211 else if (g_str_equal(key, "wpsie") == TRUE)
1212 extract_wpsie(&value, &result);
1213 else if (g_str_equal(key, "capabilities") == TRUE)
1214 extract_capabilites(&value, &result);
1215 else if (g_str_equal(key, "frequency") == TRUE)
1216 dbus_message_iter_get_basic(&value, &result.frequency);
1217 else if (g_str_equal(key, "quality") == TRUE)
1218 dbus_message_iter_get_basic(&value, &result.quality);
1219 else if (g_str_equal(key, "noise") == TRUE)
1220 dbus_message_iter_get_basic(&value, &result.noise);
1221 else if (g_str_equal(key, "level") == TRUE)
1222 dbus_message_iter_get_basic(&value, &result.level);
1223 else if (g_str_equal(key, "maxrate") == TRUE)
1224 dbus_message_iter_get_basic(&value, &result.maxrate);
1226 dbus_message_iter_next(&dict);
1229 if (result.path == NULL)
1232 if (result.path[0] == '\0')
1235 if (result.frequency > 0 && result.frequency < 14)
1236 result.frequency = 2407 + (5 * result.frequency);
1237 else if (result.frequency == 14)
1238 result.frequency = 2484;
1240 strength = calculate_strength(task, &result);
1241 channel = calculate_channel(&result);
1243 frequency = (result.frequency < 0) ? 0 : result.frequency;
1245 if (result.has_rsn == TRUE)
1247 else if (result.has_wpa == TRUE)
1249 else if (result.has_wep == TRUE)
1254 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1256 group = build_group(result.path, result.name,
1257 result.ssid, result.ssid_len,
1260 network = connman_device_get_network(task->device, result.path);
1261 if (network == NULL) {
1264 network = connman_network_create(result.path,
1265 CONNMAN_NETWORK_TYPE_WIFI);
1266 if (network == NULL)
1269 index = connman_device_get_index(task->device);
1270 connman_network_set_index(network, index);
1272 connman_network_set_protocol(network,
1273 CONNMAN_NETWORK_PROTOCOL_IP);
1275 connman_network_set_address(network, result.addr,
1278 if (connman_device_add_network(task->device, network) < 0) {
1279 connman_network_unref(network);
1284 if (result.name != NULL && result.name[0] != '\0')
1285 connman_network_set_name(network, result.name);
1287 if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1288 connman_network_set_blob(network, "WiFi.SSID",
1289 result.ssid, result.ssid_len);
1292 connman_network_set_string(network, "WiFi.Mode", mode);
1294 DBG("%s (%s %s) strength %d (%s)",
1295 result.name, mode, security, strength,
1296 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1298 connman_network_set_available(network, TRUE);
1299 connman_network_set_strength(network, strength);
1301 connman_network_set_uint16(network, "Frequency", frequency);
1302 connman_network_set_uint16(network, "WiFi.Channel", channel);
1303 connman_network_set_string(network, "WiFi.Security", security);
1305 if (result.ssid != NULL)
1306 connman_network_set_group(network, group);
1311 g_free(result.path);
1312 g_free(result.addr);
1313 g_free(result.name);
1314 g_free(result.ssid);
1316 dbus_message_unref(reply);
1318 get_properties(task);
1321 static void get_properties(struct supplicant_task *task)
1323 DBusMessage *message;
1324 DBusPendingCall *call;
1327 path = g_slist_nth_data(task->scan_results, 0);
1331 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1332 SUPPLICANT_INTF ".BSSID",
1335 task->scan_results = g_slist_remove(task->scan_results, path);
1338 if (message == NULL)
1341 if (dbus_connection_send_with_reply(connection, message,
1342 &call, TIMEOUT) == FALSE) {
1343 connman_error("Failed to get network properties");
1344 dbus_message_unref(message);
1349 connman_error("D-Bus connection not available");
1350 dbus_message_unref(message);
1354 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1356 dbus_message_unref(message);
1361 connman_device_set_scanning(task->device, FALSE);
1364 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1366 struct supplicant_task *task = user_data;
1372 DBG("task %p", task);
1374 reply = dbus_pending_call_steal_reply(call);
1378 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1381 dbus_error_init(&error);
1383 if (dbus_message_get_args(reply, &error,
1384 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1385 &results, &num_results,
1386 DBUS_TYPE_INVALID) == FALSE) {
1387 if (dbus_error_is_set(&error) == TRUE) {
1388 connman_error("%s", error.message);
1389 dbus_error_free(&error);
1391 connman_error("Wrong arguments for scan result");
1395 if (num_results == 0)
1398 for (i = 0; i < num_results; i++) {
1399 char *path = g_strdup(results[i]);
1403 task->scan_results = g_slist_append(task->scan_results, path);
1406 g_strfreev(results);
1408 dbus_message_unref(reply);
1410 get_properties(task);
1415 dbus_message_unref(reply);
1418 connman_device_set_scanning(task->device, FALSE);
1421 static void scan_results_available(struct supplicant_task *task)
1423 DBusMessage *message;
1424 DBusPendingCall *call;
1426 DBG("task %p", task);
1428 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1429 SUPPLICANT_INTF ".Interface",
1431 if (message == NULL)
1434 if (dbus_connection_send_with_reply(connection, message,
1435 &call, TIMEOUT) == FALSE) {
1436 connman_error("Failed to request scan result");
1440 if (task->noscan == FALSE)
1441 connman_device_set_scanning(task->device, TRUE);
1444 connman_error("D-Bus connection not available");
1448 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1451 dbus_message_unref(message);
1454 static enum supplicant_state string2state(const char *state)
1456 if (g_str_equal(state, "INACTIVE") == TRUE)
1457 return WPA_INACTIVE;
1458 else if (g_str_equal(state, "SCANNING") == TRUE)
1459 return WPA_SCANNING;
1460 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1461 return WPA_ASSOCIATING;
1462 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1463 return WPA_ASSOCIATED;
1464 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1465 return WPA_GROUP_HANDSHAKE;
1466 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1467 return WPA_4WAY_HANDSHAKE;
1468 else if (g_str_equal(state, "COMPLETED") == TRUE)
1469 return WPA_COMPLETED;
1470 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1471 return WPA_DISCONNECTED;
1476 static int task_connect(struct supplicant_task *task)
1478 const char *address, *security, *passphrase;
1480 unsigned int ssid_len;
1482 address = connman_network_get_string(task->network, "Address");
1483 security = connman_network_get_string(task->network, "WiFi.Security");
1484 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1486 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1488 DBG("address %s security %s passphrase %s",
1489 address, security, passphrase);
1491 if (security == NULL && passphrase == NULL)
1494 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1499 select_network(task);
1500 disable_network(task);
1502 set_network(task, ssid, ssid_len, address, security, passphrase);
1504 enable_network(task);
1506 return -EINPROGRESS;
1509 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1512 const char *newstate, *oldstate;
1513 enum supplicant_state state;
1515 dbus_error_init(&error);
1517 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1518 DBUS_TYPE_STRING, &oldstate,
1519 DBUS_TYPE_INVALID) == FALSE) {
1520 if (dbus_error_is_set(&error) == TRUE) {
1521 connman_error("%s", error.message);
1522 dbus_error_free(&error);
1524 connman_error("Wrong arguments for state change");
1528 DBG("state %s ==> %s", oldstate, newstate);
1530 state = string2state(newstate);
1531 if (state == WPA_INVALID)
1534 task->state = state;
1536 switch (task->state) {
1538 task->noscan = TRUE;
1539 connman_device_set_scanning(task->device, TRUE);
1541 case WPA_ASSOCIATING:
1542 case WPA_ASSOCIATED:
1543 case WPA_4WAY_HANDSHAKE:
1544 case WPA_GROUP_HANDSHAKE:
1545 task->noscan = TRUE;
1548 case WPA_DISCONNECTED:
1549 task->noscan = FALSE;
1552 task->noscan = FALSE;
1553 connman_device_set_scanning(task->device, FALSE);
1559 if (task->network == NULL)
1562 switch (task->state) {
1565 connman_network_set_connected(task->network, TRUE);
1566 connman_device_set_scanning(task->device, FALSE);
1569 case WPA_DISCONNECTED:
1570 disable_network(task);
1572 if (task->disconnecting == TRUE) {
1573 connman_network_set_connected(task->network, FALSE);
1574 connman_network_unref(task->network);
1575 task->disconnecting = FALSE;
1577 if (task->pending_network != NULL) {
1578 task->network = task->pending_network;
1579 task->pending_network = NULL;
1584 connman_network_set_connected(task->network, FALSE);
1585 connman_device_set_scanning(task->device, FALSE);
1589 case WPA_ASSOCIATING:
1590 connman_network_set_associating(task->network, TRUE);
1594 remove_network(task);
1596 if (task->disconnecting == TRUE) {
1597 connman_network_set_connected(task->network, FALSE);
1598 connman_network_unref(task->network);
1599 task->disconnecting = FALSE;
1601 if (task->pending_network != NULL) {
1602 task->network = task->pending_network;
1603 task->pending_network = NULL;
1610 connman_network_set_associating(task->network, FALSE);
1615 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1616 DBusMessage *msg, void *data)
1618 struct supplicant_task *task;
1619 const char *member, *path;
1621 if (dbus_message_has_interface(msg,
1622 SUPPLICANT_INTF ".Interface") == FALSE)
1623 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1625 member = dbus_message_get_member(msg);
1627 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1629 path = dbus_message_get_path(msg);
1631 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1633 task = find_task_by_path(path);
1635 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1637 DBG("task %p member %s", task, member);
1639 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1640 scan_results_available(task);
1641 else if (g_str_equal(member, "StateChange") == TRUE)
1642 state_change(task, msg);
1644 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1647 int supplicant_start(struct connman_device *device)
1649 struct supplicant_task *task;
1652 DBG("device %p", device);
1654 task = g_try_new0(struct supplicant_task, 1);
1658 task->ifindex = connman_device_get_index(device);
1659 task->ifname = connman_inet_ifname(task->ifindex);
1661 if (task->ifname == NULL) {
1666 task->range = g_try_malloc0(sizeof(struct iw_range));
1667 if (task->range == NULL) {
1672 err = get_range(task);
1676 task->device = connman_device_ref(device);
1678 task->created = FALSE;
1679 task->noscan = FALSE;
1680 task->state = WPA_INVALID;
1681 task->disconnecting = FALSE;
1682 task->pending_network = NULL;
1684 task_list = g_slist_append(task_list, task);
1686 return create_interface(task);
1689 g_free(task->range);
1690 g_free(task->ifname);
1696 int supplicant_stop(struct connman_device *device)
1698 int index = connman_device_get_index(device);
1699 struct supplicant_task *task;
1701 DBG("device %p", device);
1703 task = find_task_by_index(index);
1707 g_free(task->range);
1709 task_list = g_slist_remove(task_list, task);
1711 disable_network(task);
1713 remove_network(task);
1715 return remove_interface(task);
1718 int supplicant_scan(struct connman_device *device)
1720 int index = connman_device_get_index(device);
1721 struct supplicant_task *task;
1724 DBG("device %p", device);
1726 task = find_task_by_index(index);
1730 switch (task->state) {
1733 case WPA_ASSOCIATING:
1734 case WPA_ASSOCIATED:
1735 case WPA_4WAY_HANDSHAKE:
1736 case WPA_GROUP_HANDSHAKE:
1742 err = initiate_scan(task);
1747 int supplicant_connect(struct connman_network *network)
1749 struct supplicant_task *task;
1752 DBG("network %p", network);
1754 index = connman_network_get_index(network);
1756 task = find_task_by_index(index);
1760 if (task->disconnecting == TRUE)
1761 task->pending_network = connman_network_ref(network);
1763 task->network = connman_network_ref(network);
1764 return task_connect(task);
1767 return -EINPROGRESS;
1770 int supplicant_disconnect(struct connman_network *network)
1772 struct supplicant_task *task;
1775 DBG("network %p", network);
1777 index = connman_network_get_index(network);
1779 task = find_task_by_index(index);
1783 if (task->disconnecting == TRUE)
1784 return -EINPROGRESS;
1786 disable_network(task);
1788 remove_network(task);
1790 task->disconnecting = TRUE;
1795 static void supplicant_activate(DBusConnection *conn)
1797 DBusMessage *message;
1799 DBG("conn %p", conn);
1801 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1802 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1803 if (message == NULL)
1806 dbus_message_set_no_reply(message, TRUE);
1808 dbus_connection_send(conn, message, NULL);
1810 dbus_message_unref(message);
1813 static GSList *driver_list = NULL;
1815 static void supplicant_probe(DBusConnection *conn, void *user_data)
1819 DBG("conn %p", conn);
1821 for (list = driver_list; list; list = list->next) {
1822 struct supplicant_driver *driver = list->data;
1824 DBG("driver %p name %s", driver, driver->name);
1831 static void supplicant_remove(DBusConnection *conn, void *user_data)
1835 DBG("conn %p", conn);
1837 for (list = driver_list; list; list = list->next) {
1838 struct supplicant_driver *driver = list->data;
1840 DBG("driver %p name %s", driver, driver->name);
1847 static const char *supplicant_rule = "type=signal,"
1848 "interface=" SUPPLICANT_INTF ".Interface";
1851 static int supplicant_create(void)
1853 if (g_slist_length(driver_list) > 0)
1856 connection = connman_dbus_get_connection();
1857 if (connection == NULL)
1860 DBG("connection %p", connection);
1862 if (dbus_connection_add_filter(connection,
1863 supplicant_filter, NULL, NULL) == FALSE) {
1864 connection = connman_dbus_get_connection();
1868 dbus_bus_add_match(connection, supplicant_rule, NULL);
1869 dbus_connection_flush(connection);
1871 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1872 supplicant_probe, supplicant_remove, NULL, NULL);
1877 static void supplicant_destroy(void)
1879 if (g_slist_length(driver_list) > 0)
1882 DBG("connection %p", connection);
1885 g_dbus_remove_watch(connection, watch);
1887 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1888 dbus_connection_flush(connection);
1890 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1892 dbus_connection_unref(connection);
1896 int supplicant_register(struct supplicant_driver *driver)
1900 DBG("driver %p name %s", driver, driver->name);
1902 err = supplicant_create();
1906 driver_list = g_slist_append(driver_list, driver);
1908 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1909 supplicant_probe(connection, NULL);
1911 supplicant_activate(connection);
1916 void supplicant_unregister(struct supplicant_driver *driver)
1918 DBG("driver %p name %s", driver, driver->name);
1920 supplicant_remove(connection, NULL);
1922 driver_list = g_slist_remove(driver_list, driver);
1924 supplicant_destroy();