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 {
179 struct connman_device *device;
180 struct connman_network *network;
181 struct connman_network *pending_network;
185 enum supplicant_state state;
187 GSList *scan_results;
188 DBusPendingCall *scan_call;
189 DBusPendingCall *result_call;
190 struct iw_range *range;
191 gboolean disconnecting;
194 static GSList *task_list = NULL;
196 static DBusConnection *connection;
198 static void free_task(struct supplicant_task *task)
200 DBG("task %p", task);
202 g_free(task->ifname);
207 static struct supplicant_task *find_task_by_index(int index)
211 for (list = task_list; list; list = list->next) {
212 struct supplicant_task *task = list->data;
214 if (task->ifindex == index)
221 static struct supplicant_task *find_task_by_path(const char *path)
225 for (list = task_list; list; list = list->next) {
226 struct supplicant_task *task = list->data;
228 if (g_strcmp0(task->path, path) == 0)
235 static int get_range(struct supplicant_task *task)
240 fd = socket(PF_INET, SOCK_DGRAM, 0);
244 memset(&wrq, 0, sizeof(struct iwreq));
245 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
246 wrq.u.data.pointer = task->range;
247 wrq.u.data.length = sizeof(struct iw_range);
249 err = ioctl(fd, SIOCGIWRANGE, &wrq);
256 static int get_bssid(struct connman_device *device,
257 unsigned char *bssid, unsigned int *bssid_len)
264 ifindex = connman_device_get_index(device);
268 ifname = connman_inet_ifname(ifindex);
272 fd = socket(PF_INET, SOCK_DGRAM, 0);
278 memset(&wrq, 0, sizeof(wrq));
279 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
281 err = ioctl(fd, SIOCGIWAP, &wrq);
289 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
290 *bssid_len = ETH_ALEN;
295 static void add_interface_reply(DBusPendingCall *call, void *user_data)
297 struct supplicant_task *task = user_data;
302 DBG("task %p", task);
304 reply = dbus_pending_call_steal_reply(call);
308 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
311 dbus_error_init(&error);
313 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
314 DBUS_TYPE_INVALID) == FALSE) {
315 if (dbus_error_is_set(&error) == TRUE) {
316 connman_error("%s", error.message);
317 dbus_error_free(&error);
319 connman_error("Wrong arguments for add interface");
323 DBG("path %s", path);
325 task->path = g_strdup(path);
326 task->created = TRUE;
328 connman_device_set_powered(task->device, TRUE);
330 dbus_message_unref(reply);
335 dbus_message_unref(reply);
337 task_list = g_slist_remove(task_list, task);
339 connman_device_unref(task->device);
344 static int add_interface(struct supplicant_task *task)
346 const char *driver = connman_option_get_string("wifi");
347 DBusMessage *message;
348 DBusMessageIter array, dict;
349 DBusPendingCall *call;
351 DBG("task %p", task);
353 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
354 SUPPLICANT_INTF, "addInterface");
358 dbus_message_set_auto_start(message, FALSE);
360 dbus_message_iter_init_append(message, &array);
362 dbus_message_iter_append_basic(&array,
363 DBUS_TYPE_STRING, &task->ifname);
365 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
366 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
367 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
368 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
370 connman_dbus_dict_append_variant(&dict, "driver",
371 DBUS_TYPE_STRING, &driver);
373 dbus_message_iter_close_container(&array, &dict);
375 if (dbus_connection_send_with_reply(connection, message,
376 &call, TIMEOUT) == FALSE) {
377 connman_error("Failed to add interface");
378 dbus_message_unref(message);
383 connman_error("D-Bus connection not available");
384 dbus_message_unref(message);
388 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
390 dbus_message_unref(message);
395 static void get_interface_reply(DBusPendingCall *call, void *user_data)
397 struct supplicant_task *task = user_data;
402 DBG("task %p", task);
404 reply = dbus_pending_call_steal_reply(call);
408 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
413 dbus_error_init(&error);
415 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
416 DBUS_TYPE_INVALID) == FALSE) {
417 if (dbus_error_is_set(&error) == TRUE) {
418 connman_error("%s", error.message);
419 dbus_error_free(&error);
421 connman_error("Wrong arguments for get interface");
425 DBG("path %s", path);
427 task->path = g_strdup(path);
428 task->created = FALSE;
430 connman_device_set_powered(task->device, TRUE);
433 dbus_message_unref(reply);
436 static int create_interface(struct supplicant_task *task)
438 DBusMessage *message;
439 DBusPendingCall *call;
441 DBG("task %p", task);
443 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
444 SUPPLICANT_INTF, "getInterface");
448 dbus_message_set_auto_start(message, FALSE);
450 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
453 if (dbus_connection_send_with_reply(connection, message,
454 &call, TIMEOUT) == FALSE) {
455 connman_error("Failed to get interface");
456 dbus_message_unref(message);
461 connman_error("D-Bus connection not available");
462 dbus_message_unref(message);
466 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
468 dbus_message_unref(message);
473 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
475 struct supplicant_task *task = user_data;
478 DBG("task %p", task);
480 reply = dbus_pending_call_steal_reply(call);
482 connman_device_set_powered(task->device, FALSE);
484 connman_device_unref(task->device);
486 connman_inet_ifdown(task->ifindex);
490 dbus_message_unref(reply);
493 static int remove_interface(struct supplicant_task *task)
495 DBusMessage *message;
496 DBusPendingCall *call;
498 DBG("task %p", task);
501 if (task->created == FALSE) {
502 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_set_auto_start(message, FALSE);
514 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
517 if (dbus_connection_send_with_reply(connection, message,
518 &call, TIMEOUT) == FALSE) {
519 connman_error("Failed to remove interface");
520 dbus_message_unref(message);
525 connman_error("D-Bus connection not available");
526 dbus_message_unref(message);
530 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
532 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);
576 static int add_network(struct supplicant_task *task)
578 DBusMessage *message, *reply;
582 DBG("task %p", task);
584 if (task->netpath != NULL)
587 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
588 SUPPLICANT_INTF ".Interface", "addNetwork");
592 dbus_message_set_auto_start(message, FALSE);
594 dbus_error_init(&error);
596 reply = dbus_connection_send_with_reply_and_block(connection,
597 message, -1, &error);
599 if (dbus_error_is_set(&error) == TRUE) {
600 connman_error("%s", error.message);
601 dbus_error_free(&error);
603 connman_error("Failed to add network");
604 dbus_message_unref(message);
608 dbus_message_unref(message);
610 dbus_error_init(&error);
612 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
613 DBUS_TYPE_INVALID) == FALSE) {
614 if (dbus_error_is_set(&error) == TRUE) {
615 connman_error("%s", error.message);
616 dbus_error_free(&error);
618 connman_error("Wrong arguments for network");
619 dbus_message_unref(reply);
623 DBG("path %s", path);
625 task->netpath = g_strdup(path);
627 dbus_message_unref(reply);
632 static int remove_network(struct supplicant_task *task)
634 DBusMessage *message, *reply;
637 DBG("task %p", task);
639 if (task->netpath == NULL)
642 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
643 SUPPLICANT_INTF ".Interface", "removeNetwork");
647 dbus_message_set_auto_start(message, FALSE);
649 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
652 dbus_error_init(&error);
654 reply = dbus_connection_send_with_reply_and_block(connection,
655 message, -1, &error);
657 if (dbus_error_is_set(&error) == TRUE) {
658 connman_error("%s", error.message);
659 dbus_error_free(&error);
661 connman_error("Failed to remove network");
662 dbus_message_unref(message);
666 dbus_message_unref(message);
668 dbus_message_unref(reply);
670 g_free(task->netpath);
671 task->netpath = NULL;
676 static int select_network(struct supplicant_task *task)
678 DBusMessage *message, *reply;
681 DBG("task %p", task);
683 if (task->netpath == NULL)
686 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
687 SUPPLICANT_INTF ".Interface", "selectNetwork");
691 dbus_message_set_auto_start(message, FALSE);
693 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
696 dbus_error_init(&error);
698 reply = dbus_connection_send_with_reply_and_block(connection,
699 message, -1, &error);
701 if (dbus_error_is_set(&error) == TRUE) {
702 connman_error("%s", error.message);
703 dbus_error_free(&error);
705 connman_error("Failed to select network");
706 dbus_message_unref(message);
710 dbus_message_unref(message);
712 dbus_message_unref(reply);
717 static int disconnect_network(struct supplicant_task *task)
719 DBusMessage *message, *reply;
722 DBG("task %p", task);
724 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
725 SUPPLICANT_INTF ".Interface", "disconnect");
729 dbus_message_set_auto_start(message, FALSE);
731 dbus_error_init(&error);
733 reply = dbus_connection_send_with_reply_and_block(connection,
734 message, -1, &error);
736 if (dbus_error_is_set(&error) == TRUE) {
737 connman_error("%s", error.message);
738 dbus_error_free(&error);
740 connman_error("Failed to disconnect network");
741 dbus_message_unref(message);
745 dbus_message_unref(message);
747 dbus_message_unref(reply);
752 static int set_network(struct supplicant_task *task,
753 const unsigned char *network, int len,
754 const char *address, const char *security,
755 const char *passphrase)
757 DBusMessage *message, *reply;
758 DBusMessageIter array, dict;
760 dbus_uint32_t scan_ssid = 1;
762 DBG("task %p", task);
764 if (task->netpath == NULL)
767 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
768 SUPPLICANT_INTF ".Network", "set");
772 dbus_message_set_auto_start(message, FALSE);
774 dbus_message_iter_init_append(message, &array);
776 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
777 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
778 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
779 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
781 connman_dbus_dict_append_variant(&dict, "scan_ssid",
782 DBUS_TYPE_UINT32, &scan_ssid);
785 connman_dbus_dict_append_array(&dict, "ssid",
786 DBUS_TYPE_BYTE, &network, len);
788 connman_dbus_dict_append_variant(&dict, "bssid",
789 DBUS_TYPE_STRING, &address);
791 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
792 g_ascii_strcasecmp(security, "rsn") == 0) {
793 const char *key_mgmt = "WPA-PSK";
794 connman_dbus_dict_append_variant(&dict, "key_mgmt",
795 DBUS_TYPE_STRING, &key_mgmt);
797 if (passphrase && strlen(passphrase) > 0)
798 connman_dbus_dict_append_variant(&dict, "psk",
799 DBUS_TYPE_STRING, &passphrase);
800 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
801 const char *key_mgmt = "NONE";
802 const char *auth_alg = "OPEN";
803 const char *key_index = "0";
805 if (task->mac80211 == TRUE)
806 auth_alg = "OPEN SHARED";
808 connman_dbus_dict_append_variant(&dict, "auth_alg",
809 DBUS_TYPE_STRING, &auth_alg);
811 connman_dbus_dict_append_variant(&dict, "key_mgmt",
812 DBUS_TYPE_STRING, &key_mgmt);
815 int size = strlen(passphrase);
816 if (size == 10 || size == 26) {
817 unsigned char *key = malloc(13);
820 memset(tmp, 0, sizeof(tmp));
823 for (i = 0; i < size / 2; i++) {
824 memcpy(tmp, passphrase + (i * 2), 2);
825 key[i] = (unsigned char) strtol(tmp,
828 connman_dbus_dict_append_array(&dict,
829 "wep_key0", DBUS_TYPE_BYTE,
833 connman_dbus_dict_append_variant(&dict,
834 "wep_key0", DBUS_TYPE_STRING,
837 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
838 DBUS_TYPE_STRING, &key_index);
841 const char *key_mgmt = "NONE";
842 connman_dbus_dict_append_variant(&dict, "key_mgmt",
843 DBUS_TYPE_STRING, &key_mgmt);
846 dbus_message_iter_close_container(&array, &dict);
848 dbus_error_init(&error);
850 reply = dbus_connection_send_with_reply_and_block(connection,
851 message, -1, &error);
853 if (dbus_error_is_set(&error) == TRUE) {
854 connman_error("%s", error.message);
855 dbus_error_free(&error);
857 connman_error("Failed to set network options");
858 dbus_message_unref(message);
862 dbus_message_unref(message);
864 dbus_message_unref(reply);
869 static void scan_reply(DBusPendingCall *call, void *user_data)
871 struct supplicant_task *task = user_data;
874 DBG("task %p", task);
876 task->scan_call = NULL;
878 reply = dbus_pending_call_steal_reply(call);
882 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
883 connman_device_set_scanning(task->device, FALSE);
887 if (task->scanning == TRUE)
888 connman_device_set_scanning(task->device, TRUE);
891 dbus_message_unref(reply);
895 static int initiate_scan(struct supplicant_task *task)
897 DBusMessage *message;
899 DBG("task %p", task);
901 if (task->path == NULL)
904 if (task->scan_call != NULL)
907 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
908 SUPPLICANT_INTF ".Interface", "scan");
912 dbus_message_set_auto_start(message, FALSE);
914 if (dbus_connection_send_with_reply(connection, message,
915 &task->scan_call, TIMEOUT) == FALSE) {
916 connman_error("Failed to initiate scan");
917 dbus_message_unref(message);
921 if (task->scan_call == NULL) {
922 connman_error("D-Bus connection not available");
923 dbus_message_unref(message);
927 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
929 dbus_message_unref(message);
938 { "<hidden>", "hidden" },
939 { "default", "linksys" },
947 { "comcomcom", "3com" },
949 { "Symbol", "symbol" },
950 { "Motorola", "motorola" },
951 { "Wireless" , "wireless" },
956 static char *build_group(const char *addr, const char *name,
957 const unsigned char *ssid, unsigned int ssid_len,
958 const char *mode, const char *security)
966 str = g_string_sized_new((ssid_len * 2) + 24);
971 g_string_append_printf(str, "hidden_%s", addr);
975 for (i = 0; special_ssid[i].name; i++) {
976 if (g_strcmp0(special_ssid[i].name, name) == 0) {
977 if (special_ssid[i].value == NULL)
978 g_string_append_printf(str, "%s_%s",
981 g_string_append_printf(str, "%s_%s",
982 special_ssid[i].value, addr);
987 if (ssid_len > 0 && ssid[0] != '\0') {
988 for (i = 0; i < ssid_len; i++)
989 g_string_append_printf(str, "%02x", ssid[i]);
991 g_string_append_printf(str, "hidden_%s", addr);
994 g_string_append_printf(str, "_%s_%s", mode, security);
996 return g_string_free(str, FALSE);
999 static void extract_addr(DBusMessageIter *value,
1000 struct supplicant_result *result)
1002 DBusMessageIter array;
1003 struct ether_addr eth;
1004 unsigned char *addr;
1007 dbus_message_iter_recurse(value, &array);
1008 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1013 result->addr = g_try_malloc(addr_len);
1014 if (result->addr == NULL)
1017 memcpy(result->addr, addr, addr_len);
1018 result->addr_len = addr_len;
1020 result->path = g_try_malloc0(13);
1021 if (result->path == NULL)
1024 memcpy(ð, addr, sizeof(eth));
1025 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1026 eth.ether_addr_octet[0],
1027 eth.ether_addr_octet[1],
1028 eth.ether_addr_octet[2],
1029 eth.ether_addr_octet[3],
1030 eth.ether_addr_octet[4],
1031 eth.ether_addr_octet[5]);
1034 static void extract_ssid(DBusMessageIter *value,
1035 struct supplicant_result *result)
1037 DBusMessageIter array;
1038 unsigned char *ssid;
1041 dbus_message_iter_recurse(value, &array);
1042 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1047 if (ssid[0] == '\0')
1050 result->ssid = g_try_malloc(ssid_len);
1051 if (result->ssid == NULL)
1054 memcpy(result->ssid, ssid, ssid_len);
1055 result->ssid_len = ssid_len;
1057 result->name = g_try_malloc0(ssid_len + 1);
1058 if (result->name == NULL)
1061 for (i = 0; i < ssid_len; i++) {
1062 if (g_ascii_isprint(ssid[i]))
1063 result->name[i] = ssid[i];
1065 result->name[i] = ' ';
1069 static void extract_wpaie(DBusMessageIter *value,
1070 struct supplicant_result *result)
1072 DBusMessageIter array;
1076 dbus_message_iter_recurse(value, &array);
1077 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1080 result->has_wpa = TRUE;
1083 static void extract_rsnie(DBusMessageIter *value,
1084 struct supplicant_result *result)
1086 DBusMessageIter array;
1090 dbus_message_iter_recurse(value, &array);
1091 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1094 result->has_rsn = TRUE;
1097 static void extract_wpsie(DBusMessageIter *value,
1098 struct supplicant_result *result)
1100 DBusMessageIter array;
1104 dbus_message_iter_recurse(value, &array);
1105 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1108 result->has_wps = TRUE;
1111 static void extract_capabilites(DBusMessageIter *value,
1112 struct supplicant_result *result)
1114 dbus_message_iter_get_basic(value, &result->capabilities);
1116 if (result->capabilities & IEEE80211_CAP_ESS)
1117 result->adhoc = FALSE;
1118 else if (result->capabilities & IEEE80211_CAP_IBSS)
1119 result->adhoc = TRUE;
1121 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1122 result->has_wep = TRUE;
1125 static unsigned char calculate_strength(struct supplicant_task *task,
1126 struct supplicant_result *result)
1128 if (task->range->max_qual.qual == 0) {
1129 unsigned char strength;
1131 if (result->level > 0)
1132 strength = 100 - result->level;
1134 strength = 120 + result->level;
1142 return (result->quality * 100) / task->range->max_qual.qual;
1145 static unsigned short calculate_channel(struct supplicant_result *result)
1147 if (result->frequency < 0)
1150 return (result->frequency - 2407) / 5;
1153 static void get_properties(struct supplicant_task *task);
1155 static void properties_reply(DBusPendingCall *call, void *user_data)
1157 struct supplicant_task *task = user_data;
1158 struct supplicant_result result;
1159 struct connman_network *network;
1161 DBusMessageIter array, dict;
1162 unsigned char strength;
1163 unsigned short channel, frequency;
1164 const char *mode, *security;
1167 DBG("task %p", task);
1169 reply = dbus_pending_call_steal_reply(call);
1170 if (reply == NULL) {
1171 get_properties(task);
1175 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1176 dbus_message_unref(reply);
1177 get_properties(task);
1181 memset(&result, 0, sizeof(result));
1182 result.frequency = -1;
1183 result.quality = -1;
1187 dbus_message_iter_init(reply, &array);
1189 dbus_message_iter_recurse(&array, &dict);
1191 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1192 DBusMessageIter entry, value;
1195 dbus_message_iter_recurse(&dict, &entry);
1196 dbus_message_iter_get_basic(&entry, &key);
1198 dbus_message_iter_next(&entry);
1200 dbus_message_iter_recurse(&entry, &value);
1202 //type = dbus_message_iter_get_arg_type(&value);
1203 //dbus_message_iter_get_basic(&value, &val);
1211 * frequency : i (105)
1212 * capabilities : q (113)
1219 if (g_str_equal(key, "bssid") == TRUE)
1220 extract_addr(&value, &result);
1221 else if (g_str_equal(key, "ssid") == TRUE)
1222 extract_ssid(&value, &result);
1223 else if (g_str_equal(key, "wpaie") == TRUE)
1224 extract_wpaie(&value, &result);
1225 else if (g_str_equal(key, "rsnie") == TRUE)
1226 extract_rsnie(&value, &result);
1227 else if (g_str_equal(key, "wpsie") == TRUE)
1228 extract_wpsie(&value, &result);
1229 else if (g_str_equal(key, "capabilities") == TRUE)
1230 extract_capabilites(&value, &result);
1231 else if (g_str_equal(key, "frequency") == TRUE)
1232 dbus_message_iter_get_basic(&value, &result.frequency);
1233 else if (g_str_equal(key, "quality") == TRUE)
1234 dbus_message_iter_get_basic(&value, &result.quality);
1235 else if (g_str_equal(key, "noise") == TRUE)
1236 dbus_message_iter_get_basic(&value, &result.noise);
1237 else if (g_str_equal(key, "level") == TRUE)
1238 dbus_message_iter_get_basic(&value, &result.level);
1239 else if (g_str_equal(key, "maxrate") == TRUE)
1240 dbus_message_iter_get_basic(&value, &result.maxrate);
1242 dbus_message_iter_next(&dict);
1245 if (result.path == NULL)
1248 if (result.path[0] == '\0')
1251 if (result.frequency > 0 && result.frequency < 14)
1252 result.frequency = 2407 + (5 * result.frequency);
1253 else if (result.frequency == 14)
1254 result.frequency = 2484;
1256 strength = calculate_strength(task, &result);
1257 channel = calculate_channel(&result);
1259 frequency = (result.frequency < 0) ? 0 : result.frequency;
1261 if (result.has_rsn == TRUE)
1263 else if (result.has_wpa == TRUE)
1265 else if (result.has_wep == TRUE)
1270 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1272 group = build_group(result.path, result.name,
1273 result.ssid, result.ssid_len,
1276 network = connman_device_get_network(task->device, result.path);
1277 if (network == NULL) {
1280 network = connman_network_create(result.path,
1281 CONNMAN_NETWORK_TYPE_WIFI);
1282 if (network == NULL)
1285 index = connman_device_get_index(task->device);
1286 connman_network_set_index(network, index);
1288 connman_network_set_protocol(network,
1289 CONNMAN_NETWORK_PROTOCOL_IP);
1291 connman_network_set_address(network, result.addr,
1294 if (connman_device_add_network(task->device, network) < 0) {
1295 connman_network_unref(network);
1300 if (result.name != NULL && result.name[0] != '\0')
1301 connman_network_set_name(network, result.name);
1303 connman_network_set_blob(network, "WiFi.SSID",
1304 result.ssid, result.ssid_len);
1306 connman_network_set_string(network, "WiFi.Mode", mode);
1308 DBG("%s (%s %s) strength %d (%s)",
1309 result.name, mode, security, strength,
1310 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1312 connman_network_set_available(network, TRUE);
1313 connman_network_set_strength(network, strength);
1315 connman_network_set_uint16(network, "Frequency", frequency);
1316 connman_network_set_uint16(network, "WiFi.Channel", channel);
1317 connman_network_set_string(network, "WiFi.Security", security);
1319 if (result.ssid != NULL)
1320 connman_network_set_group(network, group);
1325 g_free(result.path);
1326 g_free(result.addr);
1327 g_free(result.name);
1328 g_free(result.ssid);
1330 dbus_message_unref(reply);
1332 get_properties(task);
1335 static void get_properties(struct supplicant_task *task)
1337 DBusMessage *message;
1340 path = g_slist_nth_data(task->scan_results, 0);
1344 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1345 SUPPLICANT_INTF ".BSSID",
1348 task->scan_results = g_slist_remove(task->scan_results, path);
1351 if (message == NULL)
1354 dbus_message_set_auto_start(message, FALSE);
1356 if (dbus_connection_send_with_reply(connection, message,
1357 &task->result_call, TIMEOUT) == FALSE) {
1358 connman_error("Failed to get network properties");
1359 dbus_message_unref(message);
1363 if (task->result_call == NULL) {
1364 connman_error("D-Bus connection not available");
1365 dbus_message_unref(message);
1369 dbus_pending_call_set_notify(task->result_call,
1370 properties_reply, task, NULL);
1372 dbus_message_unref(message);
1377 task->result_call = NULL;
1379 if (task->scanning == TRUE) {
1380 connman_device_set_scanning(task->device, FALSE);
1381 task->scanning = FALSE;
1385 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1387 struct supplicant_task *task = user_data;
1393 DBG("task %p", task);
1395 reply = dbus_pending_call_steal_reply(call);
1399 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1402 dbus_error_init(&error);
1404 if (dbus_message_get_args(reply, &error,
1405 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1406 &results, &num_results,
1407 DBUS_TYPE_INVALID) == FALSE) {
1408 if (dbus_error_is_set(&error) == TRUE) {
1409 connman_error("%s", error.message);
1410 dbus_error_free(&error);
1412 connman_error("Wrong arguments for scan result");
1416 if (num_results == 0)
1419 for (i = 0; i < num_results; i++) {
1420 char *path = g_strdup(results[i]);
1424 task->scan_results = g_slist_append(task->scan_results, path);
1427 g_strfreev(results);
1429 dbus_message_unref(reply);
1431 get_properties(task);
1436 dbus_message_unref(reply);
1439 task->result_call = NULL;
1441 if (task->scanning == TRUE) {
1442 connman_device_set_scanning(task->device, FALSE);
1443 task->scanning = FALSE;
1447 static void scan_results_available(struct supplicant_task *task)
1449 DBusMessage *message;
1451 DBG("task %p", task);
1453 if (task->result_call != NULL)
1456 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1457 SUPPLICANT_INTF ".Interface",
1459 if (message == NULL)
1462 dbus_message_set_auto_start(message, FALSE);
1464 if (dbus_connection_send_with_reply(connection, message,
1465 &task->result_call, TIMEOUT) == FALSE) {
1466 connman_error("Failed to request scan result");
1470 if (task->result_call == NULL) {
1471 connman_error("D-Bus connection not available");
1475 if (task->scanning == TRUE)
1476 connman_device_set_scanning(task->device, TRUE);
1478 dbus_pending_call_set_notify(task->result_call,
1479 scan_results_reply, task, NULL);
1482 dbus_message_unref(message);
1485 static enum supplicant_state string2state(const char *state)
1487 if (g_str_equal(state, "INACTIVE") == TRUE)
1488 return WPA_INACTIVE;
1489 else if (g_str_equal(state, "SCANNING") == TRUE)
1490 return WPA_SCANNING;
1491 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1492 return WPA_ASSOCIATING;
1493 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1494 return WPA_ASSOCIATED;
1495 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1496 return WPA_GROUP_HANDSHAKE;
1497 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1498 return WPA_4WAY_HANDSHAKE;
1499 else if (g_str_equal(state, "COMPLETED") == TRUE)
1500 return WPA_COMPLETED;
1501 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1502 return WPA_DISCONNECTED;
1507 static int task_connect(struct supplicant_task *task)
1509 const char *address, *security, *passphrase;
1511 unsigned int ssid_len;
1514 connman_inet_ifup(task->ifindex);
1516 address = connman_network_get_string(task->network, "Address");
1517 security = connman_network_get_string(task->network, "WiFi.Security");
1518 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1520 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1522 DBG("address %s security %s", address, security);
1524 if (security == NULL && passphrase == NULL)
1527 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1530 remove_network(task);
1536 set_network(task, ssid, ssid_len, address, security, passphrase);
1538 err = select_network(task);
1542 return -EINPROGRESS;
1545 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1548 dbus_bool_t scanning;
1550 dbus_error_init(&error);
1552 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1553 DBUS_TYPE_INVALID) == FALSE) {
1554 if (dbus_error_is_set(&error) == TRUE) {
1555 connman_error("%s", error.message);
1556 dbus_error_free(&error);
1558 connman_error("Wrong arguments for scanning");
1562 connman_info("%s scanning %s", task->ifname,
1563 scanning == TRUE ? "started" : "finished");
1566 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1569 const char *newstate, *oldstate;
1570 unsigned char bssid[ETH_ALEN];
1571 unsigned int bssid_len;
1572 enum supplicant_state state, prevstate;
1574 dbus_error_init(&error);
1576 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1577 DBUS_TYPE_STRING, &oldstate,
1578 DBUS_TYPE_INVALID) == FALSE) {
1579 if (dbus_error_is_set(&error) == TRUE) {
1580 connman_error("%s", error.message);
1581 dbus_error_free(&error);
1583 connman_error("Wrong arguments for state change");
1587 DBG("state %s ==> %s", oldstate, newstate);
1589 connman_info("%s %s%s", task->ifname, newstate,
1590 task->scanning == TRUE ? " (scanning)" : "");
1592 state = string2state(newstate);
1593 if (state == WPA_INVALID)
1596 if (task->scanning == TRUE && state != WPA_SCANNING) {
1597 connman_device_cleanup_scanning(task->device);
1598 task->scanning = FALSE;
1601 prevstate = task->state;
1602 task->state = state;
1604 if (task->network == NULL)
1607 switch (task->state) {
1609 switch (prevstate) {
1610 case WPA_ASSOCIATED:
1611 case WPA_GROUP_HANDSHAKE:
1617 /* reset scan trigger and schedule background scan */
1618 connman_device_schedule_scan(task->device);
1620 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1621 connman_network_set_address(task->network,
1625 connman_network_set_connected(task->network, TRUE);
1628 case WPA_ASSOCIATING:
1629 switch (prevstate) {
1633 connman_network_set_associating(task->network, TRUE);
1641 switch (prevstate) {
1643 case WPA_DISCONNECTED:
1650 case WPA_DISCONNECTED:
1652 connman_network_set_connected(task->network, FALSE);
1654 if (task->disconnecting == TRUE) {
1655 connman_network_unref(task->network);
1656 task->disconnecting = FALSE;
1658 if (task->pending_network != NULL) {
1659 task->network = task->pending_network;
1660 task->pending_network = NULL;
1663 task->network = NULL;
1668 connman_network_set_associating(task->network, FALSE);
1675 connman_error("%s invalid state change %s -> %s", task->ifname,
1676 oldstate, newstate);
1679 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1680 DBusMessage *msg, void *data)
1682 struct supplicant_task *task;
1683 const char *member, *path;
1685 if (dbus_message_has_interface(msg,
1686 SUPPLICANT_INTF ".Interface") == FALSE)
1687 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1689 member = dbus_message_get_member(msg);
1691 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1693 path = dbus_message_get_path(msg);
1695 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1697 task = find_task_by_path(path);
1699 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1701 DBG("task %p member %s", task, member);
1703 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1704 scan_results_available(task);
1705 else if (g_str_equal(member, "Scanning") == TRUE)
1706 scanning(task, msg);
1707 else if (g_str_equal(member, "StateChange") == TRUE)
1708 state_change(task, msg);
1710 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1713 int supplicant_start(struct connman_device *device)
1715 struct supplicant_task *task;
1718 DBG("device %p", device);
1720 task = g_try_new0(struct supplicant_task, 1);
1724 task->ifindex = connman_device_get_index(device);
1725 task->ifname = connman_inet_ifname(task->ifindex);
1727 if (task->ifname == NULL) {
1732 task->mac80211 = connman_inet_is_mac80211(task->ifindex);
1733 if (task->mac80211 == FALSE)
1734 connman_warn("Enabling quirks for unsupported driver");
1736 task->range = g_try_malloc0(sizeof(struct iw_range));
1737 if (task->range == NULL) {
1742 err = get_range(task);
1746 task->device = connman_device_ref(device);
1748 task->created = FALSE;
1749 task->scanning = FALSE;
1750 task->state = WPA_INVALID;
1751 task->disconnecting = FALSE;
1752 task->pending_network = NULL;
1754 task_list = g_slist_append(task_list, task);
1756 return create_interface(task);
1759 g_free(task->range);
1760 g_free(task->ifname);
1766 int supplicant_stop(struct connman_device *device)
1768 int index = connman_device_get_index(device);
1769 struct supplicant_task *task;
1771 DBG("device %p", device);
1773 task = find_task_by_index(index);
1777 g_free(task->range);
1779 task_list = g_slist_remove(task_list, task);
1781 if (task->scan_call != NULL) {
1782 dbus_pending_call_cancel(task->scan_call);
1783 task->scan_call = NULL;
1786 if (task->result_call != NULL) {
1787 dbus_pending_call_cancel(task->result_call);
1788 task->result_call = NULL;
1791 if (task->scanning == TRUE)
1792 connman_device_set_scanning(task->device, FALSE);
1794 remove_network(task);
1796 disconnect_network(task);
1798 return remove_interface(task);
1801 int supplicant_scan(struct connman_device *device)
1803 int index = connman_device_get_index(device);
1804 struct supplicant_task *task;
1807 DBG("device %p", device);
1809 task = find_task_by_index(index);
1813 switch (task->state) {
1816 case WPA_ASSOCIATING:
1817 case WPA_ASSOCIATED:
1818 case WPA_4WAY_HANDSHAKE:
1819 case WPA_GROUP_HANDSHAKE:
1825 task->scanning = TRUE;
1827 err = initiate_scan(task);
1829 if (err == -EINPROGRESS)
1832 task->scanning = FALSE;
1836 connman_device_set_scanning(task->device, TRUE);
1841 int supplicant_connect(struct connman_network *network)
1843 struct supplicant_task *task;
1846 DBG("network %p", network);
1848 index = connman_network_get_index(network);
1850 task = find_task_by_index(index);
1854 if (task->disconnecting == TRUE)
1855 task->pending_network = connman_network_ref(network);
1857 task->network = connman_network_ref(network);
1858 return task_connect(task);
1861 return -EINPROGRESS;
1864 int supplicant_disconnect(struct connman_network *network)
1866 struct supplicant_task *task;
1869 DBG("network %p", network);
1871 index = connman_network_get_index(network);
1873 task = find_task_by_index(index);
1877 if (task->disconnecting == TRUE)
1880 remove_network(task);
1882 disconnect_network(task);
1884 task->disconnecting = TRUE;
1889 static void supplicant_activate(DBusConnection *conn)
1891 DBusMessage *message;
1893 DBG("conn %p", conn);
1895 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1896 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1897 if (message == NULL)
1900 dbus_message_set_no_reply(message, TRUE);
1902 dbus_connection_send(conn, message, NULL);
1904 dbus_message_unref(message);
1907 static GSList *driver_list = NULL;
1909 static void supplicant_probe(DBusConnection *conn, void *user_data)
1913 DBG("conn %p", conn);
1915 for (list = driver_list; list; list = list->next) {
1916 struct supplicant_driver *driver = list->data;
1918 DBG("driver %p name %s", driver, driver->name);
1925 static void supplicant_remove(DBusConnection *conn, void *user_data)
1929 DBG("conn %p", conn);
1931 for (list = driver_list; list; list = list->next) {
1932 struct supplicant_driver *driver = list->data;
1934 DBG("driver %p name %s", driver, driver->name);
1941 static const char *supplicant_rule = "type=signal,"
1942 "interface=" SUPPLICANT_INTF ".Interface";
1945 static int supplicant_create(void)
1947 if (g_slist_length(driver_list) > 0)
1950 connection = connman_dbus_get_connection();
1951 if (connection == NULL)
1954 DBG("connection %p", connection);
1956 if (dbus_connection_add_filter(connection,
1957 supplicant_filter, NULL, NULL) == FALSE) {
1958 connection = connman_dbus_get_connection();
1962 dbus_bus_add_match(connection, supplicant_rule, NULL);
1963 dbus_connection_flush(connection);
1965 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1966 supplicant_probe, supplicant_remove, NULL, NULL);
1971 static void supplicant_destroy(void)
1973 if (g_slist_length(driver_list) > 0)
1976 DBG("connection %p", connection);
1979 g_dbus_remove_watch(connection, watch);
1981 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1982 dbus_connection_flush(connection);
1984 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1986 dbus_connection_unref(connection);
1990 int supplicant_register(struct supplicant_driver *driver)
1994 DBG("driver %p name %s", driver, driver->name);
1996 err = supplicant_create();
2000 driver_list = g_slist_append(driver_list, driver);
2002 supplicant_activate(connection);
2007 void supplicant_unregister(struct supplicant_driver *driver)
2009 DBG("driver %p name %s", driver, driver->name);
2011 supplicant_remove(connection, NULL);
2013 driver_list = g_slist_remove(driver_list, driver);
2015 supplicant_destroy();