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;
170 dbus_int32_t frequency;
171 dbus_int32_t quality;
174 dbus_int32_t maxrate;
177 struct supplicant_task {
181 struct connman_device *device;
182 struct connman_network *network;
183 struct connman_network *pending_network;
187 enum supplicant_state state;
189 GSList *scan_results;
190 DBusPendingCall *scan_call;
191 DBusPendingCall *result_call;
192 struct iw_range *range;
193 gboolean disconnecting;
196 static GSList *task_list = NULL;
198 static DBusConnection *connection;
200 static void free_task(struct supplicant_task *task)
202 DBG("task %p", task);
204 g_free(task->ifname);
209 static struct supplicant_task *find_task_by_index(int index)
213 for (list = task_list; list; list = list->next) {
214 struct supplicant_task *task = list->data;
216 if (task->ifindex == index)
223 static struct supplicant_task *find_task_by_path(const char *path)
227 for (list = task_list; list; list = list->next) {
228 struct supplicant_task *task = list->data;
230 if (g_strcmp0(task->path, path) == 0)
237 static int get_range(struct supplicant_task *task)
242 fd = socket(PF_INET, SOCK_DGRAM, 0);
246 memset(&wrq, 0, sizeof(struct iwreq));
247 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
248 wrq.u.data.pointer = task->range;
249 wrq.u.data.length = sizeof(struct iw_range);
251 err = ioctl(fd, SIOCGIWRANGE, &wrq);
256 task->range->max_qual.updated |= IW_QUAL_ALL_INVALID;
258 connman_info("%s {scan} capabilities 0x%02x", task->ifname,
259 task->range->scan_capa);
261 connman_info("%s {quality} flags 0x%02x", task->ifname,
262 task->range->max_qual.updated);
267 static int get_bssid(struct connman_device *device,
268 unsigned char *bssid, unsigned int *bssid_len)
275 ifindex = connman_device_get_index(device);
279 ifname = connman_inet_ifname(ifindex);
283 fd = socket(PF_INET, SOCK_DGRAM, 0);
289 memset(&wrq, 0, sizeof(wrq));
290 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
292 err = ioctl(fd, SIOCGIWAP, &wrq);
300 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
301 *bssid_len = ETH_ALEN;
306 static void add_interface_reply(DBusPendingCall *call, void *user_data)
308 struct supplicant_task *task = user_data;
313 DBG("task %p", task);
315 reply = dbus_pending_call_steal_reply(call);
319 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
322 dbus_error_init(&error);
324 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
325 DBUS_TYPE_INVALID) == FALSE) {
326 if (dbus_error_is_set(&error) == TRUE) {
327 connman_error("%s", error.message);
328 dbus_error_free(&error);
330 connman_error("Wrong arguments for add interface");
334 DBG("path %s", path);
336 task->path = g_strdup(path);
337 task->created = TRUE;
339 connman_device_set_powered(task->device, TRUE);
341 dbus_message_unref(reply);
346 dbus_message_unref(reply);
348 task_list = g_slist_remove(task_list, task);
350 connman_device_unref(task->device);
355 static int add_interface(struct supplicant_task *task)
357 const char *driver = connman_option_get_string("wifi");
358 DBusMessage *message;
359 DBusMessageIter array, dict;
360 DBusPendingCall *call;
362 DBG("task %p", task);
364 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
365 SUPPLICANT_INTF, "addInterface");
369 dbus_message_set_auto_start(message, FALSE);
371 dbus_message_iter_init_append(message, &array);
373 dbus_message_iter_append_basic(&array,
374 DBUS_TYPE_STRING, &task->ifname);
376 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
377 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
378 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
379 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
381 connman_dbus_dict_append_variant(&dict, "driver",
382 DBUS_TYPE_STRING, &driver);
384 dbus_message_iter_close_container(&array, &dict);
386 if (dbus_connection_send_with_reply(connection, message,
387 &call, TIMEOUT) == FALSE) {
388 connman_error("Failed to add interface");
389 dbus_message_unref(message);
394 connman_error("D-Bus connection not available");
395 dbus_message_unref(message);
399 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
401 dbus_message_unref(message);
406 static void get_interface_reply(DBusPendingCall *call, void *user_data)
408 struct supplicant_task *task = user_data;
413 DBG("task %p", task);
415 reply = dbus_pending_call_steal_reply(call);
419 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
424 dbus_error_init(&error);
426 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
427 DBUS_TYPE_INVALID) == FALSE) {
428 if (dbus_error_is_set(&error) == TRUE) {
429 connman_error("%s", error.message);
430 dbus_error_free(&error);
432 connman_error("Wrong arguments for get interface");
436 DBG("path %s", path);
438 task->path = g_strdup(path);
439 task->created = FALSE;
441 connman_device_set_powered(task->device, TRUE);
444 dbus_message_unref(reply);
447 static int create_interface(struct supplicant_task *task)
449 DBusMessage *message;
450 DBusPendingCall *call;
452 DBG("task %p", task);
454 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
455 SUPPLICANT_INTF, "getInterface");
459 dbus_message_set_auto_start(message, FALSE);
461 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
464 if (dbus_connection_send_with_reply(connection, message,
465 &call, TIMEOUT) == FALSE) {
466 connman_error("Failed to get interface");
467 dbus_message_unref(message);
472 connman_error("D-Bus connection not available");
473 dbus_message_unref(message);
477 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
479 dbus_message_unref(message);
484 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
486 struct supplicant_task *task = user_data;
489 DBG("task %p", task);
491 reply = dbus_pending_call_steal_reply(call);
493 connman_device_set_powered(task->device, FALSE);
495 connman_device_unref(task->device);
497 connman_inet_ifdown(task->ifindex);
501 dbus_message_unref(reply);
504 static int remove_interface(struct supplicant_task *task)
506 DBusMessage *message;
507 DBusPendingCall *call;
509 DBG("task %p", task);
512 if (task->created == FALSE) {
513 connman_device_set_powered(task->device, FALSE);
518 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
519 SUPPLICANT_INTF, "removeInterface");
523 dbus_message_set_auto_start(message, FALSE);
525 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
528 if (dbus_connection_send_with_reply(connection, message,
529 &call, TIMEOUT) == FALSE) {
530 connman_error("Failed to remove interface");
531 dbus_message_unref(message);
536 connman_error("D-Bus connection not available");
537 dbus_message_unref(message);
541 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
543 dbus_message_unref(message);
548 static int set_ap_scan(struct supplicant_task *task)
550 DBusMessage *message, *reply;
554 DBG("task %p", task);
556 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
557 SUPPLICANT_INTF ".Interface", "setAPScan");
561 dbus_message_set_auto_start(message, FALSE);
563 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
566 dbus_error_init(&error);
568 reply = dbus_connection_send_with_reply_and_block(connection,
569 message, -1, &error);
571 if (dbus_error_is_set(&error) == TRUE) {
572 connman_error("%s", error.message);
573 dbus_error_free(&error);
575 connman_error("Failed to set AP scan");
576 dbus_message_unref(message);
580 dbus_message_unref(message);
582 dbus_message_unref(reply);
587 static int add_network(struct supplicant_task *task)
589 DBusMessage *message, *reply;
593 DBG("task %p", task);
595 if (task->netpath != NULL)
598 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
599 SUPPLICANT_INTF ".Interface", "addNetwork");
603 dbus_message_set_auto_start(message, FALSE);
605 dbus_error_init(&error);
607 reply = dbus_connection_send_with_reply_and_block(connection,
608 message, -1, &error);
610 if (dbus_error_is_set(&error) == TRUE) {
611 connman_error("%s", error.message);
612 dbus_error_free(&error);
614 connman_error("Failed to add network");
615 dbus_message_unref(message);
619 dbus_message_unref(message);
621 dbus_error_init(&error);
623 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
624 DBUS_TYPE_INVALID) == FALSE) {
625 if (dbus_error_is_set(&error) == TRUE) {
626 connman_error("%s", error.message);
627 dbus_error_free(&error);
629 connman_error("Wrong arguments for network");
630 dbus_message_unref(reply);
634 DBG("path %s", path);
636 task->netpath = g_strdup(path);
638 dbus_message_unref(reply);
643 static int remove_network(struct supplicant_task *task)
645 DBusMessage *message, *reply;
648 DBG("task %p", task);
650 if (task->netpath == NULL)
653 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
654 SUPPLICANT_INTF ".Interface", "removeNetwork");
658 dbus_message_set_auto_start(message, FALSE);
660 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
663 dbus_error_init(&error);
665 reply = dbus_connection_send_with_reply_and_block(connection,
666 message, -1, &error);
668 if (dbus_error_is_set(&error) == TRUE) {
669 connman_error("%s", error.message);
670 dbus_error_free(&error);
672 connman_error("Failed to remove network");
673 dbus_message_unref(message);
677 dbus_message_unref(message);
679 dbus_message_unref(reply);
681 g_free(task->netpath);
682 task->netpath = NULL;
687 static int select_network(struct supplicant_task *task)
689 DBusMessage *message, *reply;
692 DBG("task %p", task);
694 if (task->netpath == NULL)
697 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
698 SUPPLICANT_INTF ".Interface", "selectNetwork");
702 dbus_message_set_auto_start(message, FALSE);
704 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
707 dbus_error_init(&error);
709 reply = dbus_connection_send_with_reply_and_block(connection,
710 message, -1, &error);
712 if (dbus_error_is_set(&error) == TRUE) {
713 connman_error("%s", error.message);
714 dbus_error_free(&error);
716 connman_error("Failed to select network");
717 dbus_message_unref(message);
721 dbus_message_unref(message);
723 dbus_message_unref(reply);
728 static int disconnect_network(struct supplicant_task *task)
730 DBusMessage *message, *reply;
733 DBG("task %p", task);
735 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
736 SUPPLICANT_INTF ".Interface", "disconnect");
740 dbus_message_set_auto_start(message, FALSE);
742 dbus_error_init(&error);
744 reply = dbus_connection_send_with_reply_and_block(connection,
745 message, -1, &error);
747 if (dbus_error_is_set(&error) == TRUE) {
748 connman_error("%s", error.message);
749 dbus_error_free(&error);
751 connman_error("Failed to disconnect network");
752 dbus_message_unref(message);
756 dbus_message_unref(message);
758 dbus_message_unref(reply);
763 static int set_network(struct supplicant_task *task,
764 const unsigned char *network, int len,
765 const char *address, const char *security,
766 const char *passphrase)
768 DBusMessage *message, *reply;
769 DBusMessageIter array, dict;
771 dbus_uint32_t scan_ssid = 1;
773 DBG("task %p", task);
775 if (task->netpath == NULL)
778 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
779 SUPPLICANT_INTF ".Network", "set");
783 dbus_message_set_auto_start(message, FALSE);
785 dbus_message_iter_init_append(message, &array);
787 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
788 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
789 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
790 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
792 connman_dbus_dict_append_variant(&dict, "scan_ssid",
793 DBUS_TYPE_UINT32, &scan_ssid);
796 connman_dbus_dict_append_array(&dict, "ssid",
797 DBUS_TYPE_BYTE, &network, len);
799 connman_dbus_dict_append_variant(&dict, "bssid",
800 DBUS_TYPE_STRING, &address);
802 if (g_ascii_strcasecmp(security, "psk") == 0 ||
803 g_ascii_strcasecmp(security, "wpa") == 0 ||
804 g_ascii_strcasecmp(security, "rsn") == 0) {
805 const char *key_mgmt = "WPA-PSK";
806 connman_dbus_dict_append_variant(&dict, "key_mgmt",
807 DBUS_TYPE_STRING, &key_mgmt);
809 if (passphrase && strlen(passphrase) > 0)
810 connman_dbus_dict_append_variant(&dict, "psk",
811 DBUS_TYPE_STRING, &passphrase);
812 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
813 const char *key_mgmt = "NONE";
814 const char *auth_alg = "OPEN";
815 const char *key_index = "0";
817 if (task->mac80211 == TRUE)
818 auth_alg = "OPEN SHARED";
820 connman_dbus_dict_append_variant(&dict, "auth_alg",
821 DBUS_TYPE_STRING, &auth_alg);
823 connman_dbus_dict_append_variant(&dict, "key_mgmt",
824 DBUS_TYPE_STRING, &key_mgmt);
827 int size = strlen(passphrase);
828 if (size == 10 || size == 26) {
829 unsigned char *key = malloc(13);
832 memset(tmp, 0, sizeof(tmp));
835 for (i = 0; i < size / 2; i++) {
836 memcpy(tmp, passphrase + (i * 2), 2);
837 key[i] = (unsigned char) strtol(tmp,
840 connman_dbus_dict_append_array(&dict,
841 "wep_key0", DBUS_TYPE_BYTE,
845 connman_dbus_dict_append_variant(&dict,
846 "wep_key0", DBUS_TYPE_STRING,
849 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
850 DBUS_TYPE_STRING, &key_index);
853 const char *key_mgmt = "NONE";
854 connman_dbus_dict_append_variant(&dict, "key_mgmt",
855 DBUS_TYPE_STRING, &key_mgmt);
858 dbus_message_iter_close_container(&array, &dict);
860 dbus_error_init(&error);
862 reply = dbus_connection_send_with_reply_and_block(connection,
863 message, -1, &error);
865 if (dbus_error_is_set(&error) == TRUE) {
866 connman_error("%s", error.message);
867 dbus_error_free(&error);
869 connman_error("Failed to set network options");
870 dbus_message_unref(message);
874 dbus_message_unref(message);
876 dbus_message_unref(reply);
881 static void scan_reply(DBusPendingCall *call, void *user_data)
883 struct supplicant_task *task = user_data;
886 DBG("task %p", task);
888 task->scan_call = NULL;
890 reply = dbus_pending_call_steal_reply(call);
894 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
895 connman_device_set_scanning(task->device, FALSE);
899 if (task->scanning == TRUE)
900 connman_device_set_scanning(task->device, TRUE);
903 dbus_message_unref(reply);
907 static int initiate_scan(struct supplicant_task *task)
909 DBusMessage *message;
911 DBG("task %p", task);
913 if (task->path == NULL)
916 if (task->scan_call != NULL)
919 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
920 SUPPLICANT_INTF ".Interface", "scan");
924 dbus_message_set_auto_start(message, FALSE);
926 if (dbus_connection_send_with_reply(connection, message,
927 &task->scan_call, TIMEOUT) == FALSE) {
928 connman_error("Failed to initiate scan");
929 dbus_message_unref(message);
933 if (task->scan_call == NULL) {
934 connman_error("D-Bus connection not available");
935 dbus_message_unref(message);
939 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
941 dbus_message_unref(message);
950 { "<hidden>", "hidden" },
951 { "default", "linksys" },
959 { "comcomcom", "3com" },
961 { "Symbol", "symbol" },
962 { "Motorola", "motorola" },
963 { "Wireless" , "wireless" },
968 static char *build_group(const char *addr, const char *name,
969 const unsigned char *ssid, unsigned int ssid_len,
970 const char *mode, const char *security)
978 str = g_string_sized_new((ssid_len * 2) + 24);
983 g_string_append_printf(str, "hidden_%s", addr);
987 for (i = 0; special_ssid[i].name; i++) {
988 if (g_strcmp0(special_ssid[i].name, name) == 0) {
989 if (special_ssid[i].value == NULL)
990 g_string_append_printf(str, "%s_%s",
993 g_string_append_printf(str, "%s_%s",
994 special_ssid[i].value, addr);
999 if (ssid_len > 0 && ssid[0] != '\0') {
1000 for (i = 0; i < ssid_len; i++)
1001 g_string_append_printf(str, "%02x", ssid[i]);
1003 g_string_append_printf(str, "hidden_%s", addr);
1006 g_string_append_printf(str, "_%s_%s", mode, security);
1008 return g_string_free(str, FALSE);
1011 static void extract_addr(DBusMessageIter *value,
1012 struct supplicant_result *result)
1014 DBusMessageIter array;
1015 struct ether_addr eth;
1016 unsigned char *addr;
1019 dbus_message_iter_recurse(value, &array);
1020 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1025 result->addr = g_try_malloc(addr_len);
1026 if (result->addr == NULL)
1029 memcpy(result->addr, addr, addr_len);
1030 result->addr_len = addr_len;
1032 result->path = g_try_malloc0(13);
1033 if (result->path == NULL)
1036 memcpy(ð, addr, sizeof(eth));
1037 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1038 eth.ether_addr_octet[0],
1039 eth.ether_addr_octet[1],
1040 eth.ether_addr_octet[2],
1041 eth.ether_addr_octet[3],
1042 eth.ether_addr_octet[4],
1043 eth.ether_addr_octet[5]);
1046 static void extract_ssid(DBusMessageIter *value,
1047 struct supplicant_result *result)
1049 DBusMessageIter array;
1050 unsigned char *ssid;
1053 dbus_message_iter_recurse(value, &array);
1054 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1059 if (ssid[0] == '\0')
1062 result->ssid = g_try_malloc(ssid_len);
1063 if (result->ssid == NULL)
1066 memcpy(result->ssid, ssid, ssid_len);
1067 result->ssid_len = ssid_len;
1069 result->name = g_try_malloc0(ssid_len + 1);
1070 if (result->name == NULL)
1073 for (i = 0; i < ssid_len; i++) {
1074 if (g_ascii_isprint(ssid[i]))
1075 result->name[i] = ssid[i];
1077 result->name[i] = ' ';
1081 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1082 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1084 static void extract_rsn(struct supplicant_result *result,
1085 const unsigned char *buf, int len)
1104 /* Pairwise cipher */
1108 count = buf[0] | (buf[1] << 8);
1109 if (2 + (count * 4) > len)
1112 buf += 2 + (count * 4);
1113 len -= 2 + (count * 4);
1115 /* Authentication */
1119 count = buf[0] | (buf[1] << 8);
1120 if (2 + (count * 4) > len)
1123 for (i = 0; i < count; i++) {
1124 const unsigned char *ptr = buf + 2 + (i * 4);
1126 if (memcmp(ptr, wifi_oui, 3) == 0) {
1129 result->has_8021x = TRUE;
1132 result->has_psk = TRUE;
1135 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1138 result->has_8021x = TRUE;
1141 result->has_psk = TRUE;
1147 buf += 2 + (count * 4);
1148 len -= 2 + (count * 4);
1151 static void extract_wpaie(DBusMessageIter *value,
1152 struct supplicant_result *result)
1154 DBusMessageIter array;
1158 dbus_message_iter_recurse(value, &array);
1159 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1162 result->has_wpa = TRUE;
1163 extract_rsn(result, ie + 6, ie_len - 6);
1167 static void extract_rsnie(DBusMessageIter *value,
1168 struct supplicant_result *result)
1170 DBusMessageIter array;
1174 dbus_message_iter_recurse(value, &array);
1175 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1178 result->has_rsn = TRUE;
1179 extract_rsn(result, ie + 2, ie_len - 2);
1183 static void extract_wpsie(DBusMessageIter *value,
1184 struct supplicant_result *result)
1186 DBusMessageIter array;
1190 dbus_message_iter_recurse(value, &array);
1191 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1194 result->has_wps = TRUE;
1197 static void extract_capabilites(DBusMessageIter *value,
1198 struct supplicant_result *result)
1200 dbus_message_iter_get_basic(value, &result->capabilities);
1202 if (result->capabilities & IEEE80211_CAP_ESS)
1203 result->adhoc = FALSE;
1204 else if (result->capabilities & IEEE80211_CAP_IBSS)
1205 result->adhoc = TRUE;
1207 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1208 result->has_wep = TRUE;
1211 static unsigned char calculate_strength(struct supplicant_task *task,
1212 struct supplicant_result *result)
1214 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1215 unsigned char strength;
1217 if (result->level > 0)
1218 strength = 100 - result->level;
1220 strength = 120 + result->level;
1228 return (result->quality * 100) / task->range->max_qual.qual;
1231 static unsigned short calculate_channel(struct supplicant_result *result)
1233 if (result->frequency < 0)
1236 return (result->frequency - 2407) / 5;
1239 static void get_properties(struct supplicant_task *task);
1241 static void properties_reply(DBusPendingCall *call, void *user_data)
1243 struct supplicant_task *task = user_data;
1244 struct supplicant_result result;
1245 struct connman_network *network;
1247 DBusMessageIter array, dict;
1248 unsigned char strength;
1249 unsigned short channel, frequency;
1250 const char *mode, *security;
1253 DBG("task %p", task);
1255 reply = dbus_pending_call_steal_reply(call);
1256 if (reply == NULL) {
1257 get_properties(task);
1261 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1262 dbus_message_unref(reply);
1263 get_properties(task);
1267 memset(&result, 0, sizeof(result));
1268 result.frequency = -1;
1269 result.quality = -1;
1273 dbus_message_iter_init(reply, &array);
1275 dbus_message_iter_recurse(&array, &dict);
1277 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1278 DBusMessageIter entry, value;
1281 dbus_message_iter_recurse(&dict, &entry);
1282 dbus_message_iter_get_basic(&entry, &key);
1284 dbus_message_iter_next(&entry);
1286 dbus_message_iter_recurse(&entry, &value);
1288 //type = dbus_message_iter_get_arg_type(&value);
1289 //dbus_message_iter_get_basic(&value, &val);
1297 * frequency : i (105)
1298 * capabilities : q (113)
1305 if (g_str_equal(key, "bssid") == TRUE)
1306 extract_addr(&value, &result);
1307 else if (g_str_equal(key, "ssid") == TRUE)
1308 extract_ssid(&value, &result);
1309 else if (g_str_equal(key, "wpaie") == TRUE)
1310 extract_wpaie(&value, &result);
1311 else if (g_str_equal(key, "rsnie") == TRUE)
1312 extract_rsnie(&value, &result);
1313 else if (g_str_equal(key, "wpsie") == TRUE)
1314 extract_wpsie(&value, &result);
1315 else if (g_str_equal(key, "capabilities") == TRUE)
1316 extract_capabilites(&value, &result);
1317 else if (g_str_equal(key, "frequency") == TRUE)
1318 dbus_message_iter_get_basic(&value, &result.frequency);
1319 else if (g_str_equal(key, "quality") == TRUE)
1320 dbus_message_iter_get_basic(&value, &result.quality);
1321 else if (g_str_equal(key, "noise") == TRUE)
1322 dbus_message_iter_get_basic(&value, &result.noise);
1323 else if (g_str_equal(key, "level") == TRUE)
1324 dbus_message_iter_get_basic(&value, &result.level);
1325 else if (g_str_equal(key, "maxrate") == TRUE)
1326 dbus_message_iter_get_basic(&value, &result.maxrate);
1328 dbus_message_iter_next(&dict);
1331 DBG("capabilties %u frequency %d "
1332 "quality %d noise %d level %d maxrate %d",
1333 result.capabilities, result.frequency,
1334 result.quality, result.noise,
1335 result.level, result.maxrate);
1337 if (result.path == NULL)
1340 if (result.path[0] == '\0')
1343 if (result.frequency > 0 && result.frequency < 14)
1344 result.frequency = 2407 + (5 * result.frequency);
1345 else if (result.frequency == 14)
1346 result.frequency = 2484;
1348 strength = calculate_strength(task, &result);
1349 channel = calculate_channel(&result);
1351 frequency = (result.frequency < 0) ? 0 : result.frequency;
1353 if (result.has_8021x == TRUE)
1354 security = "ieee8021x";
1355 else if (result.has_psk == TRUE)
1357 else if (result.has_wep == TRUE)
1362 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1364 group = build_group(result.path, result.name,
1365 result.ssid, result.ssid_len,
1368 if (result.has_psk == TRUE) {
1369 if (result.has_rsn == TRUE)
1371 else if (result.has_wpa == TRUE)
1375 network = connman_device_get_network(task->device, result.path);
1376 if (network == NULL) {
1379 network = connman_network_create(result.path,
1380 CONNMAN_NETWORK_TYPE_WIFI);
1381 if (network == NULL)
1384 index = connman_device_get_index(task->device);
1385 connman_network_set_index(network, index);
1387 connman_network_set_protocol(network,
1388 CONNMAN_NETWORK_PROTOCOL_IP);
1390 connman_network_set_address(network, result.addr,
1393 if (connman_device_add_network(task->device, network) < 0) {
1394 connman_network_unref(network);
1399 if (result.name != NULL && result.name[0] != '\0')
1400 connman_network_set_name(network, result.name);
1402 connman_network_set_blob(network, "WiFi.SSID",
1403 result.ssid, result.ssid_len);
1405 connman_network_set_string(network, "WiFi.Mode", mode);
1407 DBG("%s (%s %s) strength %d (%s)",
1408 result.name, mode, security, strength,
1409 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1411 connman_network_set_available(network, TRUE);
1412 connman_network_set_strength(network, strength);
1414 connman_network_set_uint16(network, "Frequency", frequency);
1415 connman_network_set_uint16(network, "WiFi.Channel", channel);
1416 connman_network_set_string(network, "WiFi.Security", security);
1418 if (result.ssid != NULL)
1419 connman_network_set_group(network, group);
1424 g_free(result.path);
1425 g_free(result.addr);
1426 g_free(result.name);
1427 g_free(result.ssid);
1429 dbus_message_unref(reply);
1431 get_properties(task);
1434 static void get_properties(struct supplicant_task *task)
1436 DBusMessage *message;
1439 path = g_slist_nth_data(task->scan_results, 0);
1443 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1444 SUPPLICANT_INTF ".BSSID",
1447 task->scan_results = g_slist_remove(task->scan_results, path);
1450 if (message == NULL)
1453 dbus_message_set_auto_start(message, FALSE);
1455 if (dbus_connection_send_with_reply(connection, message,
1456 &task->result_call, TIMEOUT) == FALSE) {
1457 connman_error("Failed to get network properties");
1458 dbus_message_unref(message);
1462 if (task->result_call == NULL) {
1463 connman_error("D-Bus connection not available");
1464 dbus_message_unref(message);
1468 dbus_pending_call_set_notify(task->result_call,
1469 properties_reply, task, NULL);
1471 dbus_message_unref(message);
1476 task->result_call = NULL;
1478 if (task->scanning == TRUE) {
1479 connman_device_set_scanning(task->device, FALSE);
1480 task->scanning = FALSE;
1484 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1486 struct supplicant_task *task = user_data;
1492 DBG("task %p", task);
1494 reply = dbus_pending_call_steal_reply(call);
1498 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1501 dbus_error_init(&error);
1503 if (dbus_message_get_args(reply, &error,
1504 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1505 &results, &num_results,
1506 DBUS_TYPE_INVALID) == FALSE) {
1507 if (dbus_error_is_set(&error) == TRUE) {
1508 connman_error("%s", error.message);
1509 dbus_error_free(&error);
1511 connman_error("Wrong arguments for scan result");
1515 if (num_results == 0)
1518 for (i = 0; i < num_results; i++) {
1519 char *path = g_strdup(results[i]);
1523 task->scan_results = g_slist_append(task->scan_results, path);
1526 g_strfreev(results);
1528 dbus_message_unref(reply);
1530 get_properties(task);
1535 dbus_message_unref(reply);
1538 task->result_call = NULL;
1540 if (task->scanning == TRUE) {
1541 connman_device_set_scanning(task->device, FALSE);
1542 task->scanning = FALSE;
1546 static void scan_results_available(struct supplicant_task *task)
1548 DBusMessage *message;
1550 DBG("task %p", task);
1552 if (task->result_call != NULL)
1555 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1556 SUPPLICANT_INTF ".Interface",
1558 if (message == NULL)
1561 dbus_message_set_auto_start(message, FALSE);
1563 if (dbus_connection_send_with_reply(connection, message,
1564 &task->result_call, TIMEOUT) == FALSE) {
1565 connman_error("Failed to request scan result");
1569 if (task->result_call == NULL) {
1570 connman_error("D-Bus connection not available");
1574 if (task->scanning == TRUE)
1575 connman_device_set_scanning(task->device, TRUE);
1577 dbus_pending_call_set_notify(task->result_call,
1578 scan_results_reply, task, NULL);
1581 dbus_message_unref(message);
1584 static enum supplicant_state string2state(const char *state)
1586 if (g_str_equal(state, "INACTIVE") == TRUE)
1587 return WPA_INACTIVE;
1588 else if (g_str_equal(state, "SCANNING") == TRUE)
1589 return WPA_SCANNING;
1590 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1591 return WPA_ASSOCIATING;
1592 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1593 return WPA_ASSOCIATED;
1594 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1595 return WPA_GROUP_HANDSHAKE;
1596 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1597 return WPA_4WAY_HANDSHAKE;
1598 else if (g_str_equal(state, "COMPLETED") == TRUE)
1599 return WPA_COMPLETED;
1600 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1601 return WPA_DISCONNECTED;
1606 static int task_connect(struct supplicant_task *task)
1608 const char *address, *security, *passphrase;
1610 unsigned int ssid_len;
1613 connman_inet_ifup(task->ifindex);
1615 address = connman_network_get_string(task->network, "Address");
1616 security = connman_network_get_string(task->network, "WiFi.Security");
1617 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1619 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1621 DBG("address %s security %s", address, security);
1623 if (security == NULL && passphrase == NULL)
1626 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1629 remove_network(task);
1635 set_network(task, ssid, ssid_len, address, security, passphrase);
1637 err = select_network(task);
1641 return -EINPROGRESS;
1644 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1647 dbus_bool_t scanning;
1649 dbus_error_init(&error);
1651 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1652 DBUS_TYPE_INVALID) == FALSE) {
1653 if (dbus_error_is_set(&error) == TRUE) {
1654 connman_error("%s", error.message);
1655 dbus_error_free(&error);
1657 connman_error("Wrong arguments for scanning");
1661 connman_info("%s scanning %s", task->ifname,
1662 scanning == TRUE ? "started" : "finished");
1665 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1668 const char *newstate, *oldstate;
1669 unsigned char bssid[ETH_ALEN];
1670 unsigned int bssid_len;
1671 enum supplicant_state state, prevstate;
1673 dbus_error_init(&error);
1675 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1676 DBUS_TYPE_STRING, &oldstate,
1677 DBUS_TYPE_INVALID) == FALSE) {
1678 if (dbus_error_is_set(&error) == TRUE) {
1679 connman_error("%s", error.message);
1680 dbus_error_free(&error);
1682 connman_error("Wrong arguments for state change");
1686 DBG("state %s ==> %s", oldstate, newstate);
1688 connman_info("%s %s%s", task->ifname, newstate,
1689 task->scanning == TRUE ? " (scanning)" : "");
1691 state = string2state(newstate);
1692 if (state == WPA_INVALID)
1695 if (task->scanning == TRUE && state != WPA_SCANNING) {
1696 connman_device_cleanup_scanning(task->device);
1697 task->scanning = FALSE;
1700 prevstate = task->state;
1701 task->state = state;
1703 if (task->network == NULL)
1706 switch (task->state) {
1708 switch (prevstate) {
1709 case WPA_ASSOCIATED:
1710 case WPA_GROUP_HANDSHAKE:
1716 /* reset scan trigger and schedule background scan */
1717 connman_device_schedule_scan(task->device);
1719 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1720 connman_network_set_address(task->network,
1724 connman_network_set_connected(task->network, TRUE);
1727 case WPA_ASSOCIATING:
1728 switch (prevstate) {
1732 connman_network_set_associating(task->network, TRUE);
1740 switch (prevstate) {
1742 case WPA_DISCONNECTED:
1749 case WPA_DISCONNECTED:
1751 connman_network_set_connected(task->network, FALSE);
1753 if (task->disconnecting == TRUE) {
1754 connman_network_unref(task->network);
1755 task->disconnecting = FALSE;
1757 if (task->pending_network != NULL) {
1758 task->network = task->pending_network;
1759 task->pending_network = NULL;
1762 task->network = NULL;
1767 connman_network_set_associating(task->network, FALSE);
1774 connman_error("%s invalid state change %s -> %s", task->ifname,
1775 oldstate, newstate);
1778 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1779 DBusMessage *msg, void *data)
1781 struct supplicant_task *task;
1782 const char *member, *path;
1784 if (dbus_message_has_interface(msg,
1785 SUPPLICANT_INTF ".Interface") == FALSE)
1786 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1788 member = dbus_message_get_member(msg);
1790 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1792 path = dbus_message_get_path(msg);
1794 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1796 task = find_task_by_path(path);
1798 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1800 DBG("task %p member %s", task, member);
1802 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1803 scan_results_available(task);
1804 else if (g_str_equal(member, "Scanning") == TRUE)
1805 scanning(task, msg);
1806 else if (g_str_equal(member, "StateChange") == TRUE)
1807 state_change(task, msg);
1809 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1812 int supplicant_start(struct connman_device *device)
1814 struct supplicant_task *task;
1817 DBG("device %p", device);
1819 task = g_try_new0(struct supplicant_task, 1);
1823 task->ifindex = connman_device_get_index(device);
1824 task->ifname = connman_inet_ifname(task->ifindex);
1826 if (task->ifname == NULL) {
1831 task->mac80211 = connman_inet_is_mac80211(task->ifindex);
1832 if (task->mac80211 == FALSE)
1833 connman_warn("Enabling quirks for unsupported driver");
1835 task->range = g_try_malloc0(sizeof(struct iw_range));
1836 if (task->range == NULL) {
1841 err = get_range(task);
1845 task->device = connman_device_ref(device);
1847 task->created = FALSE;
1848 task->scanning = FALSE;
1849 task->state = WPA_INVALID;
1850 task->disconnecting = FALSE;
1851 task->pending_network = NULL;
1853 task_list = g_slist_append(task_list, task);
1855 return create_interface(task);
1858 g_free(task->range);
1859 g_free(task->ifname);
1865 int supplicant_stop(struct connman_device *device)
1867 int index = connman_device_get_index(device);
1868 struct supplicant_task *task;
1870 DBG("device %p", device);
1872 task = find_task_by_index(index);
1876 g_free(task->range);
1878 task_list = g_slist_remove(task_list, task);
1880 if (task->scan_call != NULL) {
1881 dbus_pending_call_cancel(task->scan_call);
1882 task->scan_call = NULL;
1885 if (task->result_call != NULL) {
1886 dbus_pending_call_cancel(task->result_call);
1887 task->result_call = NULL;
1890 if (task->scanning == TRUE)
1891 connman_device_set_scanning(task->device, FALSE);
1893 remove_network(task);
1895 disconnect_network(task);
1897 return remove_interface(task);
1900 int supplicant_scan(struct connman_device *device)
1902 int index = connman_device_get_index(device);
1903 struct supplicant_task *task;
1906 DBG("device %p", device);
1908 task = find_task_by_index(index);
1912 switch (task->state) {
1915 case WPA_ASSOCIATING:
1916 case WPA_ASSOCIATED:
1917 case WPA_4WAY_HANDSHAKE:
1918 case WPA_GROUP_HANDSHAKE:
1924 task->scanning = TRUE;
1926 err = initiate_scan(task);
1928 if (err == -EINPROGRESS)
1931 task->scanning = FALSE;
1935 connman_device_set_scanning(task->device, TRUE);
1940 int supplicant_connect(struct connman_network *network)
1942 struct supplicant_task *task;
1945 DBG("network %p", network);
1947 index = connman_network_get_index(network);
1949 task = find_task_by_index(index);
1953 if (task->disconnecting == TRUE)
1954 task->pending_network = connman_network_ref(network);
1956 task->network = connman_network_ref(network);
1957 return task_connect(task);
1960 return -EINPROGRESS;
1963 int supplicant_disconnect(struct connman_network *network)
1965 struct supplicant_task *task;
1968 DBG("network %p", network);
1970 index = connman_network_get_index(network);
1972 task = find_task_by_index(index);
1976 if (task->disconnecting == TRUE)
1979 remove_network(task);
1981 disconnect_network(task);
1983 task->disconnecting = TRUE;
1988 static void supplicant_activate(DBusConnection *conn)
1990 DBusMessage *message;
1992 DBG("conn %p", conn);
1994 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1995 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1996 if (message == NULL)
1999 dbus_message_set_no_reply(message, TRUE);
2001 dbus_connection_send(conn, message, NULL);
2003 dbus_message_unref(message);
2006 static GSList *driver_list = NULL;
2008 static void supplicant_probe(DBusConnection *conn, void *user_data)
2012 DBG("conn %p", conn);
2014 for (list = driver_list; list; list = list->next) {
2015 struct supplicant_driver *driver = list->data;
2017 DBG("driver %p name %s", driver, driver->name);
2024 static void supplicant_remove(DBusConnection *conn, void *user_data)
2028 DBG("conn %p", conn);
2030 for (list = driver_list; list; list = list->next) {
2031 struct supplicant_driver *driver = list->data;
2033 DBG("driver %p name %s", driver, driver->name);
2040 static const char *supplicant_rule = "type=signal,"
2041 "interface=" SUPPLICANT_INTF ".Interface";
2044 static int supplicant_create(void)
2046 if (g_slist_length(driver_list) > 0)
2049 connection = connman_dbus_get_connection();
2050 if (connection == NULL)
2053 DBG("connection %p", connection);
2055 if (dbus_connection_add_filter(connection,
2056 supplicant_filter, NULL, NULL) == FALSE) {
2057 connection = connman_dbus_get_connection();
2061 dbus_bus_add_match(connection, supplicant_rule, NULL);
2062 dbus_connection_flush(connection);
2064 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2065 supplicant_probe, supplicant_remove, NULL, NULL);
2070 static void supplicant_destroy(void)
2072 if (g_slist_length(driver_list) > 0)
2075 DBG("connection %p", connection);
2078 g_dbus_remove_watch(connection, watch);
2080 dbus_bus_remove_match(connection, supplicant_rule, NULL);
2081 dbus_connection_flush(connection);
2083 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
2085 dbus_connection_unref(connection);
2089 int supplicant_register(struct supplicant_driver *driver)
2093 DBG("driver %p name %s", driver, driver->name);
2095 err = supplicant_create();
2099 driver_list = g_slist_append(driver_list, driver);
2101 supplicant_activate(connection);
2106 void supplicant_unregister(struct supplicant_driver *driver)
2108 DBG("driver %p name %s", driver, driver->name);
2110 supplicant_remove(connection, NULL);
2112 driver_list = g_slist_remove(driver_list, driver);
2114 supplicant_destroy();