5 * Copyright (C) 2007-2010 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/wifi.h>
45 #include <connman/log.h>
47 #include "supplicant.h"
51 #define IEEE80211_CAP_ESS 0x0001
52 #define IEEE80211_CAP_IBSS 0x0002
53 #define IEEE80211_CAP_PRIVACY 0x0010
55 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
56 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
57 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
59 /* Taken from "WPA Supplicant - Common definitions" */
60 enum supplicant_state {
62 * WPA_DISCONNECTED - Disconnected state
64 * This state indicates that client is not associated, but is likely to
65 * start looking for an access point. This state is entered when a
71 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
73 * This state is entered if there are no enabled networks in the
74 * configuration. wpa_supplicant is not trying to associate with a new
75 * network and external interaction (e.g., ctrl_iface call to add or
76 * enable a network) is needed to start association.
81 * WPA_SCANNING - Scanning for a network
83 * This state is entered when wpa_supplicant starts scanning for a
89 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
91 * This state is entered when wpa_supplicant has found a suitable BSS
92 * to associate with and the driver is configured to try to associate
93 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
94 * state is entered when the driver is configured to try to associate
95 * with a network using the configured SSID and security policy.
100 * WPA_ASSOCIATED - Association completed
102 * This state is entered when the driver reports that association has
103 * been successfully completed with an AP. If IEEE 802.1X is used
104 * (with or without WPA/WPA2), wpa_supplicant remains in this state
105 * until the IEEE 802.1X/EAPOL authentication has been completed.
110 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
112 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
113 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
114 * frame after association. In case of WPA-EAP, this state is entered
115 * when the IEEE 802.1X/EAPOL authentication has been completed.
120 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
122 * This state is entered when 4-Way Key Handshake has been completed
123 * (i.e., when the supplicant sends out message 4/4) and when Group
124 * Key rekeying is started by the AP (i.e., when supplicant receives
130 * WPA_COMPLETED - All authentication completed
132 * This state is entered when the full authentication process is
133 * completed. In case of WPA2, this happens when the 4-Way Handshake is
134 * successfully completed. With WPA, this state is entered after the
135 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
136 * completed after dynamic keys are received (or if not used, after
137 * the EAP authentication has been completed). With static WEP keys and
138 * plaintext connections, this state is entered when an association
139 * has been completed.
141 * This state indicates that the supplicant has completed its
142 * processing for the association phase and that data connection is
148 * WPA_INVALID - Invalid state (parsing error)
150 * This state is returned if the string input is invalid. It is not
151 * an official wpa_supplicant state.
156 struct supplicant_result {
160 unsigned int addr_len;
162 unsigned int ssid_len;
163 dbus_uint16_t capabilities;
171 dbus_int32_t frequency;
172 dbus_int32_t quality;
175 dbus_int32_t maxrate;
178 struct supplicant_block {
185 struct supplicant_task {
189 struct connman_device *device;
190 struct connman_network *network;
191 struct connman_network *pending_network;
194 gboolean hidden_found;
195 GHashTable *hidden_blocks;
197 enum supplicant_state state;
199 GSList *scan_results;
200 DBusPendingCall *scan_call;
201 DBusPendingCall *result_call;
202 struct iw_range *range;
203 gboolean disconnecting;
206 static GSList *task_list = NULL;
208 static DBusConnection *connection;
210 static void free_task(struct supplicant_task *task)
212 DBG("task %p", task);
214 g_free(task->ifname);
219 static void remove_block(gpointer user_data)
221 struct supplicant_block *block = user_data;
226 g_free(block->netpath);
230 static struct supplicant_task *find_task_by_index(int index)
234 for (list = task_list; list; list = list->next) {
235 struct supplicant_task *task = list->data;
237 if (task->ifindex == index)
244 static struct supplicant_task *find_task_by_path(const char *path)
248 for (list = task_list; list; list = list->next) {
249 struct supplicant_task *task = list->data;
251 if (g_strcmp0(task->path, path) == 0)
258 static int get_range(struct supplicant_task *task)
263 fd = socket(PF_INET, SOCK_DGRAM, 0);
267 memset(&wrq, 0, sizeof(struct iwreq));
268 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
269 wrq.u.data.pointer = task->range;
270 wrq.u.data.length = sizeof(struct iw_range);
272 err = ioctl(fd, SIOCGIWRANGE, &wrq);
277 task->range->max_qual.updated |= IW_QUAL_ALL_INVALID;
279 connman_info("%s {scan} capabilities 0x%02x", task->ifname,
280 task->range->scan_capa);
282 connman_info("%s {quality} flags 0x%02x", task->ifname,
283 task->range->max_qual.updated);
288 static int get_bssid(struct connman_device *device,
289 unsigned char *bssid, unsigned int *bssid_len)
296 ifindex = connman_device_get_index(device);
300 ifname = connman_inet_ifname(ifindex);
304 fd = socket(PF_INET, SOCK_DGRAM, 0);
310 memset(&wrq, 0, sizeof(wrq));
311 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
313 err = ioctl(fd, SIOCGIWAP, &wrq);
321 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
322 *bssid_len = ETH_ALEN;
327 static int enable_network(struct supplicant_task *task, const char *netpath,
328 connman_bool_t enable)
330 DBusMessage *message, *reply;
334 DBG("enable %d", enable);
336 enable_string = enable ? "enable" : "disable";
338 message = dbus_message_new_method_call(SUPPLICANT_NAME, netpath,
339 SUPPLICANT_INTF ".Network", enable_string);
343 dbus_message_set_auto_start(message, FALSE);
345 dbus_error_init(&error);
347 reply = dbus_connection_send_with_reply_and_block(connection,
348 message, -1, &error);
350 if (dbus_error_is_set(&error) == TRUE) {
351 connman_error("%s", error.message);
352 dbus_error_free(&error);
354 connman_error("Failed to select network");
355 dbus_message_unref(message);
359 dbus_message_unref(reply);
361 dbus_message_unref(message);
366 static int set_hidden_network(struct supplicant_task *task, const char *netpath,
367 const unsigned char *ssid, int ssid_len)
369 DBusMessage *message, *reply;
370 DBusMessageIter array, dict;
372 dbus_uint32_t scan_ssid = 1;
373 const char *invalid_address = "ff:ff:ff:ff:ff:ff";
375 message = dbus_message_new_method_call(SUPPLICANT_NAME, netpath,
376 SUPPLICANT_INTF ".Network", "set");
380 dbus_message_set_auto_start(message, FALSE);
382 dbus_message_iter_init_append(message, &array);
384 connman_dbus_dict_open(&array, &dict);
386 connman_dbus_dict_append_basic(&dict, "scan_ssid",
387 DBUS_TYPE_UINT32, &scan_ssid);
389 connman_dbus_dict_append_fixed_array(&dict, "ssid",
390 DBUS_TYPE_BYTE, &ssid, ssid_len);
393 * We're setting an invalid BSSID to prevent wpa_s from associating
394 * automatically to this block once it's found.
396 connman_dbus_dict_append_basic(&dict, "bssid",
397 DBUS_TYPE_STRING, &invalid_address);
399 connman_dbus_dict_close(&array, &dict);
401 dbus_error_init(&error);
403 reply = dbus_connection_send_with_reply_and_block(connection,
404 message, -1, &error);
406 if (dbus_error_is_set(&error) == TRUE) {
407 connman_error("%s", error.message);
408 dbus_error_free(&error);
410 connman_error("Failed to set network options");
411 dbus_message_unref(message);
415 dbus_message_unref(reply);
417 dbus_message_unref(message);
422 static void block_reset(gpointer key, gpointer value, gpointer user_data)
424 struct supplicant_block *block = value;
425 struct supplicant_task *task = user_data;
427 block->num_scans = 0;
429 enable_network(task, block->netpath, FALSE);
431 block->enabled = FALSE;
434 #define MAX_BLOCK_SCANS 2
435 static void hidden_block_enable(struct supplicant_task *task)
439 struct supplicant_block *block;
441 DBG("network %p", task->network);
443 if (g_hash_table_size(task->hidden_blocks) == 0)
447 * If we're associated or associating, we no longer need to
448 * look for hidden networks.
454 * We go through the block list and:
455 * - If we scanned it more than twice, we disable it and move
456 * on to the next block.
457 * - If the next block is not enabled, we enable it, start
458 * the scan counter, and return. This routine will be called
459 * again when the next scan results are available.
460 * - If we're done with all the blocks there, we just reset them.
462 g_hash_table_iter_init(&iter, task->hidden_blocks);
463 while (g_hash_table_iter_next(&iter, &key, &value)) {
466 DBG("%s num of scans %d enabled %d",
467 block->ssid, block->num_scans, block->enabled);
469 if (block->num_scans > MAX_BLOCK_SCANS) {
470 if (block->enabled == FALSE)
473 enable_network(task, block->netpath, FALSE);
474 block->enabled = FALSE;
478 if (block->enabled == FALSE) {
479 enable_network(task, block->netpath, TRUE);
480 block->enabled = TRUE;
488 g_hash_table_foreach(task->hidden_blocks, block_reset, task);
491 static int add_hidden_network(struct supplicant_task *task,
492 const unsigned char *ssid, int ssid_len)
494 DBusMessage *message, *reply;
497 struct supplicant_block *block;
498 char *netpath = NULL;
501 DBG("task %p", task);
503 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
504 SUPPLICANT_INTF ".Interface", "addNetwork");
508 dbus_message_set_auto_start(message, FALSE);
510 dbus_error_init(&error);
512 reply = dbus_connection_send_with_reply_and_block(connection,
513 message, -1, &error);
515 if (dbus_error_is_set(&error) == TRUE) {
516 connman_error("%s", error.message);
517 dbus_error_free(&error);
519 connman_error("Failed to add network");
520 dbus_message_unref(message);
524 dbus_error_init(&error);
526 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
527 DBUS_TYPE_INVALID) == FALSE) {
528 if (dbus_error_is_set(&error) == TRUE) {
529 connman_error("%s", error.message);
530 dbus_error_free(&error);
532 connman_error("Wrong arguments for network");
533 dbus_message_unref(reply);
537 netpath = g_strdup(path);
539 ret = set_hidden_network(task, netpath, ssid, ssid_len);
543 block = g_try_new0(struct supplicant_block, 1);
547 block->ssid = g_try_malloc0(ssid_len + 1);
548 if (block->ssid == NULL) {
553 for (i = 0; i < ssid_len; i++) {
554 if (g_ascii_isprint(ssid[i]))
555 block->ssid[i] = ssid[i];
557 block->ssid[i] = ' ';
560 block->netpath = netpath;
561 block->enabled = FALSE;
562 block->num_scans = 0;
564 DBG("path %s ssid %s", block->netpath, block->ssid);
566 g_hash_table_replace(task->hidden_blocks, block->ssid, block);
572 dbus_message_unref(reply);
574 dbus_message_unref(message);
579 static void add_interface_reply(DBusPendingCall *call, void *user_data)
581 struct supplicant_task *task = user_data;
585 char **hex_ssids, *hex_ssid;
588 DBG("task %p", task);
590 reply = dbus_pending_call_steal_reply(call);
592 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
595 dbus_error_init(&error);
597 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
598 DBUS_TYPE_INVALID) == FALSE) {
599 if (dbus_error_is_set(&error) == TRUE) {
600 connman_error("%s", error.message);
601 dbus_error_free(&error);
603 connman_error("Wrong arguments for add interface");
607 DBG("path %s", path);
609 task->path = g_strdup(path);
610 task->created = TRUE;
612 connman_device_set_powered(task->device, TRUE);
614 dbus_message_unref(reply);
616 dbus_pending_call_unref(call);
618 hex_ssids = connman_wifi_load_ssid();
619 if (hex_ssids == NULL)
622 for (i = 0; hex_ssids[i]; i++) {
624 unsigned int j, k = 0, hex;
627 hex_ssid = hex_ssids[i];
628 hex_ssid_len = strlen(hex_ssid);
630 ssid = g_try_malloc0(hex_ssid_len / 2 + 1);
634 for (j = 0, k = 0; j < hex_ssid_len; j += 2) {
635 sscanf(hex_ssid + j, "%02x", &hex);
639 if (add_hidden_network(task, ssid, hex_ssid_len / 2) < 0) {
647 g_strfreev(hex_ssids);
652 dbus_message_unref(reply);
654 dbus_pending_call_unref(call);
656 task_list = g_slist_remove(task_list, task);
658 connman_device_unref(task->device);
663 static int add_interface(struct supplicant_task *task)
665 const char *driver = connman_option_get_string("wifi");
666 DBusMessage *message;
667 DBusMessageIter array, dict;
668 DBusPendingCall *call;
670 DBG("task %p", task);
672 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
673 SUPPLICANT_INTF, "addInterface");
677 dbus_message_set_auto_start(message, FALSE);
679 dbus_message_iter_init_append(message, &array);
681 dbus_message_iter_append_basic(&array,
682 DBUS_TYPE_STRING, &task->ifname);
684 connman_dbus_dict_open(&array, &dict);
686 connman_dbus_dict_append_basic(&dict, "driver",
687 DBUS_TYPE_STRING, &driver);
689 connman_dbus_dict_close(&array, &dict);
691 if (dbus_connection_send_with_reply(connection, message,
692 &call, TIMEOUT) == FALSE) {
693 connman_error("Failed to add interface");
694 dbus_message_unref(message);
699 connman_error("D-Bus connection not available");
700 dbus_message_unref(message);
704 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
706 dbus_message_unref(message);
711 static void get_interface_reply(DBusPendingCall *call, void *user_data)
713 struct supplicant_task *task = user_data;
718 DBG("task %p", task);
720 reply = dbus_pending_call_steal_reply(call);
722 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
727 dbus_error_init(&error);
729 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
730 DBUS_TYPE_INVALID) == FALSE) {
731 if (dbus_error_is_set(&error) == TRUE) {
732 connman_error("%s", error.message);
733 dbus_error_free(&error);
735 connman_error("Wrong arguments for get interface");
739 DBG("path %s", path);
741 task->path = g_strdup(path);
742 task->created = FALSE;
744 connman_device_set_powered(task->device, TRUE);
747 dbus_message_unref(reply);
749 dbus_pending_call_unref(call);
752 static int create_interface(struct supplicant_task *task)
754 DBusMessage *message;
755 DBusPendingCall *call;
757 DBG("task %p", task);
759 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
760 SUPPLICANT_INTF, "getInterface");
764 dbus_message_set_auto_start(message, FALSE);
766 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
769 if (dbus_connection_send_with_reply(connection, message,
770 &call, TIMEOUT) == FALSE) {
771 connman_error("Failed to get interface");
772 dbus_message_unref(message);
777 connman_error("D-Bus connection not available");
778 dbus_message_unref(message);
782 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
784 dbus_message_unref(message);
789 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
791 struct supplicant_task *task = user_data;
794 DBG("task %p", task);
796 reply = dbus_pending_call_steal_reply(call);
798 connman_device_set_powered(task->device, FALSE);
800 connman_device_unref(task->device);
802 connman_inet_ifdown(task->ifindex);
806 dbus_message_unref(reply);
808 dbus_pending_call_unref(call);
811 static int remove_interface(struct supplicant_task *task)
813 DBusMessage *message;
814 DBusPendingCall *call;
816 DBG("task %p", task);
818 if (task->path == NULL)
822 if (task->created == FALSE) {
823 connman_device_set_powered(task->device, FALSE);
828 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
829 SUPPLICANT_INTF, "removeInterface");
833 dbus_message_set_auto_start(message, FALSE);
835 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
838 if (dbus_connection_send_with_reply(connection, message,
839 &call, TIMEOUT) == FALSE) {
840 connman_error("Failed to remove interface");
841 dbus_message_unref(message);
846 connman_error("D-Bus connection not available");
847 dbus_message_unref(message);
851 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
853 dbus_message_unref(message);
858 static int set_ap_scan(struct supplicant_task *task)
860 DBusMessage *message, *reply;
864 DBG("task %p", task);
866 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
867 SUPPLICANT_INTF ".Interface", "setAPScan");
871 dbus_message_set_auto_start(message, FALSE);
873 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
876 dbus_error_init(&error);
878 reply = dbus_connection_send_with_reply_and_block(connection,
879 message, -1, &error);
881 if (dbus_error_is_set(&error) == TRUE) {
882 connman_error("%s", error.message);
883 dbus_error_free(&error);
885 connman_error("Failed to set AP scan");
886 dbus_message_unref(message);
890 dbus_message_unref(message);
892 dbus_message_unref(reply);
897 static int add_network(struct supplicant_task *task)
899 DBusMessage *message, *reply;
903 DBG("task %p", task);
905 if (task->netpath != NULL)
908 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
909 SUPPLICANT_INTF ".Interface", "addNetwork");
913 dbus_message_set_auto_start(message, FALSE);
915 dbus_error_init(&error);
917 reply = dbus_connection_send_with_reply_and_block(connection,
918 message, -1, &error);
920 if (dbus_error_is_set(&error) == TRUE) {
921 connman_error("%s", error.message);
922 dbus_error_free(&error);
924 connman_error("Failed to add network");
925 dbus_message_unref(message);
929 dbus_message_unref(message);
931 dbus_error_init(&error);
933 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
934 DBUS_TYPE_INVALID) == FALSE) {
935 if (dbus_error_is_set(&error) == TRUE) {
936 connman_error("%s", error.message);
937 dbus_error_free(&error);
939 connman_error("Wrong arguments for network");
940 dbus_message_unref(reply);
944 DBG("path %s", path);
946 task->netpath = g_strdup(path);
948 dbus_message_unref(reply);
953 static int remove_network(struct supplicant_task *task)
955 DBusMessage *message, *reply;
958 DBG("task %p", task);
960 if (task->netpath == NULL || task->path == NULL)
963 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
964 SUPPLICANT_INTF ".Interface", "removeNetwork");
968 dbus_message_set_auto_start(message, FALSE);
970 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
973 dbus_error_init(&error);
975 reply = dbus_connection_send_with_reply_and_block(connection,
976 message, -1, &error);
978 if (dbus_error_is_set(&error) == TRUE) {
979 connman_error("%s", error.message);
980 dbus_error_free(&error);
982 connman_error("Failed to remove network");
983 dbus_message_unref(message);
987 dbus_message_unref(message);
989 dbus_message_unref(reply);
991 g_free(task->netpath);
992 task->netpath = NULL;
997 static int select_network(struct supplicant_task *task)
999 DBusMessage *message, *reply;
1002 DBG("task %p", task);
1004 if (task->netpath == NULL)
1007 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1008 SUPPLICANT_INTF ".Interface", "selectNetwork");
1009 if (message == NULL)
1012 dbus_message_set_auto_start(message, FALSE);
1014 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
1017 dbus_error_init(&error);
1019 reply = dbus_connection_send_with_reply_and_block(connection,
1020 message, -1, &error);
1021 if (reply == NULL) {
1022 if (dbus_error_is_set(&error) == TRUE) {
1023 connman_error("%s", error.message);
1024 dbus_error_free(&error);
1026 connman_error("Failed to select network");
1027 dbus_message_unref(message);
1031 dbus_message_unref(message);
1033 dbus_message_unref(reply);
1038 static int disconnect_network(struct supplicant_task *task)
1040 DBusMessage *message, *reply;
1043 DBG("task %p", task);
1045 if (task->path == NULL)
1048 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1049 SUPPLICANT_INTF ".Interface", "disconnect");
1050 if (message == NULL)
1053 dbus_message_set_auto_start(message, FALSE);
1055 dbus_error_init(&error);
1057 reply = dbus_connection_send_with_reply_and_block(connection,
1058 message, -1, &error);
1059 if (reply == NULL) {
1060 if (dbus_error_is_set(&error) == TRUE) {
1061 connman_error("%s", error.message);
1062 dbus_error_free(&error);
1064 connman_error("Failed to disconnect network");
1065 dbus_message_unref(message);
1069 dbus_message_unref(message);
1071 dbus_message_unref(reply);
1076 static int set_network_tls(struct connman_network *network,
1077 DBusMessageIter *dict)
1079 const char *private_key, *client_cert, *ca_cert;
1080 const char *private_key_password;
1083 * For TLS, we at least need:
1084 * The client certificate
1085 * The client private key file
1086 * The client private key file password
1088 * The Authority certificate is optional.
1090 client_cert = connman_network_get_string(network,
1091 "WiFi.ClientCertFile");
1092 if (client_cert == NULL) {
1093 connman_error("Error in TLS authentication: "
1094 "a ClientCertFile must be defined\n");
1098 private_key = connman_network_get_string(network,
1099 "WiFi.PrivateKeyFile");
1100 if (private_key == NULL) {
1101 connman_error("Error in TLS authentication: "
1102 "a PrivateKeyFile must be defined\n");
1106 private_key_password = connman_network_get_string(network,
1107 "WiFi.PrivateKeyPassphrase");
1108 if (private_key_password == NULL) {
1109 connman_error("Error in TLS authentication: "
1110 "a PrivateKeyPassphrase must be defined\n");
1114 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
1116 connman_dbus_dict_append_basic(dict, "ca_cert",
1117 DBUS_TYPE_STRING, &ca_cert);
1119 connman_info("No CACertFile has been provided "
1120 "to do the TLS authentication\n");
1122 DBG("client cert %s private key %s", client_cert, private_key);
1124 connman_dbus_dict_append_basic(dict, "private_key",
1125 DBUS_TYPE_STRING, &private_key);
1126 connman_dbus_dict_append_basic(dict, "private_key_passwd",
1128 &private_key_password);
1129 connman_dbus_dict_append_basic(dict, "client_cert",
1130 DBUS_TYPE_STRING, &client_cert);
1135 static int set_network_peap(struct connman_network *network,
1136 DBusMessageIter *dict, const char *passphrase)
1138 const char *client_cert, *ca_cert, *phase2;
1142 * For PEAP/TTLS, we at least need
1143 * The authority certificate
1144 * The 2nd phase authentication method
1145 * The 2nd phase passphrase
1147 * The Client certificate is optional although strongly required
1148 * When setting it, we need in addition
1149 * The Client private key file
1150 * The Client private key file password
1152 if (passphrase == NULL) {
1153 connman_error("Error in PEAP/TTLS authentication: "
1154 "a phase2 passphrase must be defined\n");
1158 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
1159 if (ca_cert == NULL) {
1160 connman_error("Error in PEAP/TTLS authentication: "
1161 "CACertFile must be defined\n");
1165 phase2 = connman_network_get_string(network, "WiFi.Phase2");
1166 if (phase2 == NULL) {
1167 connman_error("Error in PEAP/TTLS authentication: "
1168 "Phase2 must be defined\n");
1172 DBG("CA cert %s phase2 auth %s", ca_cert, phase2);
1174 client_cert = connman_network_get_string(network,
1175 "WiFi.ClientCertFile");
1177 const char *private_key, *private_key_password;
1179 private_key = connman_network_get_string(network,
1180 "WiFi.PrivateKeyFile");
1181 if (private_key == NULL) {
1182 connman_error("Error in PEAP/TTLS authentication: "
1183 "with ClientCertFile, "
1184 "PrivateKeyFile must be defined\n");
1188 private_key_password =
1189 connman_network_get_string(network,
1190 "WiFi.PrivateKeyPassphrase");
1191 if (private_key_password == NULL) {
1192 connman_error("Error in PEAP/TTLS authentication: "
1193 "with ClientCertFile, "
1194 "PrivateKeyPassphrase must be defined\n");
1198 connman_dbus_dict_append_basic(dict, "client_cert",
1199 DBUS_TYPE_STRING, &client_cert);
1201 connman_dbus_dict_append_basic(dict, "private_key",
1202 DBUS_TYPE_STRING, &private_key);
1204 connman_dbus_dict_append_basic(dict, "private_key_passwd",
1206 &private_key_password);
1208 DBG("client cert %s private key %s", client_cert, private_key);
1210 connman_info("No client certificate has been provided "
1211 "to do the PEAP/TTLS authentication\n");
1213 if (g_str_has_prefix(phase2, "EAP-") == TRUE) {
1214 phase2_auth = g_strdup_printf("autheap=%s",
1215 phase2 + strlen("EAP-"));
1217 phase2_auth = g_strdup_printf("auth=%s", phase2);
1219 connman_dbus_dict_append_basic(dict, "password",
1220 DBUS_TYPE_STRING, &passphrase);
1222 connman_dbus_dict_append_basic(dict, "ca_cert",
1223 DBUS_TYPE_STRING, &ca_cert);
1225 connman_dbus_dict_append_basic(dict, "phase2",
1226 DBUS_TYPE_STRING, &phase2_auth);
1228 g_free(phase2_auth);
1233 static int set_network(struct supplicant_task *task,
1234 const unsigned char *network, int len,
1235 const char *address, const char *security,
1236 const char *passphrase)
1238 DBusMessage *message, *reply;
1239 DBusMessageIter array, dict;
1241 dbus_uint32_t scan_ssid = 1;
1243 DBG("task %p", task);
1245 if (task->netpath == NULL)
1248 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
1249 SUPPLICANT_INTF ".Network", "set");
1250 if (message == NULL)
1253 dbus_message_set_auto_start(message, FALSE);
1255 dbus_message_iter_init_append(message, &array);
1257 connman_dbus_dict_open(&array, &dict);
1259 connman_dbus_dict_append_basic(&dict, "scan_ssid",
1260 DBUS_TYPE_UINT32, &scan_ssid);
1263 connman_dbus_dict_append_fixed_array(&dict, "ssid",
1264 DBUS_TYPE_BYTE, &network, len);
1266 connman_dbus_dict_append_basic(&dict, "bssid",
1267 DBUS_TYPE_STRING, &address);
1269 if (g_ascii_strcasecmp(security, "psk") == 0 ||
1270 g_ascii_strcasecmp(security, "wpa") == 0 ||
1271 g_ascii_strcasecmp(security, "rsn") == 0) {
1272 const char *key_mgmt = "WPA-PSK";
1273 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1274 DBUS_TYPE_STRING, &key_mgmt);
1276 if (passphrase && strlen(passphrase) > 0)
1277 connman_dbus_dict_append_basic(&dict, "psk",
1278 DBUS_TYPE_STRING, &passphrase);
1279 } else if (g_ascii_strcasecmp(security, "ieee8021x") == 0) {
1280 struct connman_network *network = task->network;
1281 const char *key_mgmt = "WPA-EAP", *eap, *identity;
1285 * If our private key password is unset,
1286 * we use the supplied passphrase. That is needed
1287 * for PEAP where 2 passphrases (identity and client
1288 * cert may have to be provided.
1290 if (connman_network_get_string(network,
1291 "WiFi.PrivateKeyPassphrase") == NULL)
1292 connman_network_set_string(network,
1293 "WiFi.PrivateKeyPassphrase",
1296 eap = connman_network_get_string(network, "WiFi.EAP");
1300 /* We must have an identity for both PEAP and TLS */
1301 identity = connman_network_get_string(network, "WiFi.Identity");
1302 if (identity == NULL)
1305 DBG("key_mgmt %s eap %s identity %s", key_mgmt, eap, identity);
1307 if (g_strcmp0(eap, "tls") == 0) {
1310 err = set_network_tls(network, &dict);
1312 dbus_message_unref(message);
1315 } else if (g_strcmp0(eap, "peap") == 0 ||
1316 g_strcmp0(eap, "ttls") == 0) {
1319 err = set_network_peap(network, &dict, passphrase);
1321 dbus_message_unref(message);
1325 connman_error("Unknown EAP %s", eap);
1329 /* wpa_supplicant only accepts upper case EAPs */
1330 eap_value = g_ascii_strup(eap, -1);
1332 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1335 connman_dbus_dict_append_basic(&dict, "eap",
1338 connman_dbus_dict_append_basic(&dict, "identity",
1344 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
1345 const char *key_mgmt = "NONE";
1346 const char *auth_alg = "OPEN";
1347 const char *key_index = "0";
1349 if (task->cfg80211 == TRUE)
1350 auth_alg = "OPEN SHARED";
1352 connman_dbus_dict_append_basic(&dict, "auth_alg",
1353 DBUS_TYPE_STRING, &auth_alg);
1355 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1356 DBUS_TYPE_STRING, &key_mgmt);
1359 int size = strlen(passphrase);
1360 if (size == 10 || size == 26) {
1361 unsigned char *key = malloc(13);
1364 memset(tmp, 0, sizeof(tmp));
1367 for (i = 0; i < size / 2; i++) {
1368 memcpy(tmp, passphrase + (i * 2), 2);
1369 key[i] = (unsigned char) strtol(tmp,
1372 connman_dbus_dict_append_fixed_array(&dict,
1373 "wep_key0", DBUS_TYPE_BYTE,
1376 } else if (size == 5 || size == 13) {
1377 unsigned char *key = malloc(13);
1381 for (i = 0; i < size; i++)
1382 key[i] = (unsigned char) passphrase[i];
1383 connman_dbus_dict_append_fixed_array(&dict,
1384 "wep_key0", DBUS_TYPE_BYTE,
1388 connman_dbus_dict_append_basic(&dict,
1389 "wep_key0", DBUS_TYPE_STRING,
1392 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
1393 DBUS_TYPE_STRING, &key_index);
1396 const char *key_mgmt = "NONE";
1397 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1398 DBUS_TYPE_STRING, &key_mgmt);
1401 connman_dbus_dict_close(&array, &dict);
1403 dbus_error_init(&error);
1405 reply = dbus_connection_send_with_reply_and_block(connection,
1406 message, -1, &error);
1407 if (reply == NULL) {
1408 if (dbus_error_is_set(&error) == TRUE) {
1409 connman_error("%s", error.message);
1410 dbus_error_free(&error);
1412 connman_error("Failed to set network options");
1413 dbus_message_unref(message);
1417 dbus_message_unref(message);
1419 dbus_message_unref(reply);
1424 dbus_message_unref(message);
1428 static void scan_reply(DBusPendingCall *call, void *user_data)
1430 struct supplicant_task *task = user_data;
1433 DBG("task %p", task);
1435 task->scan_call = NULL;
1437 reply = dbus_pending_call_steal_reply(call);
1439 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1440 connman_device_set_scanning(task->device, FALSE);
1444 if (task->scanning == TRUE)
1445 connman_device_set_scanning(task->device, TRUE);
1448 dbus_message_unref(reply);
1450 dbus_pending_call_unref(call);
1454 static int initiate_scan(struct supplicant_task *task)
1456 DBusMessage *message;
1458 DBG("task %p", task);
1460 if (task->path == NULL)
1463 if (task->scan_call != NULL)
1466 connman_info("%s initiating scan", task->ifname);
1468 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1469 SUPPLICANT_INTF ".Interface", "scan");
1470 if (message == NULL)
1473 dbus_message_set_auto_start(message, FALSE);
1475 if (dbus_connection_send_with_reply(connection, message,
1476 &task->scan_call, TIMEOUT) == FALSE) {
1477 connman_error("Failed to initiate scan");
1478 dbus_message_unref(message);
1482 if (task->scan_call == NULL) {
1483 connman_error("D-Bus connection not available");
1484 dbus_message_unref(message);
1488 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
1490 dbus_message_unref(message);
1492 return -EINPROGRESS;
1498 } special_ssid[] = {
1499 { "<hidden>", "hidden" },
1500 { "default", "linksys" },
1508 { "comcomcom", "3com" },
1510 { "Symbol", "symbol" },
1511 { "Motorola", "motorola" },
1512 { "Wireless" , "wireless" },
1517 static char *build_group(const char *addr, const char *name,
1518 const unsigned char *ssid, unsigned int ssid_len,
1519 const char *mode, const char *security)
1527 str = g_string_sized_new((ssid_len * 2) + 24);
1532 g_string_append_printf(str, "hidden_%s", addr);
1536 for (i = 0; special_ssid[i].name; i++) {
1537 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1538 if (special_ssid[i].value == NULL)
1539 g_string_append_printf(str, "%s_%s",
1542 g_string_append_printf(str, "%s_%s",
1543 special_ssid[i].value, addr);
1548 if (ssid_len > 0 && ssid[0] != '\0') {
1549 for (i = 0; i < ssid_len; i++)
1550 g_string_append_printf(str, "%02x", ssid[i]);
1552 g_string_append_printf(str, "hidden_%s", addr);
1555 g_string_append_printf(str, "_%s_%s", mode, security);
1557 return g_string_free(str, FALSE);
1560 static void extract_addr(DBusMessageIter *value,
1561 struct supplicant_result *result)
1563 DBusMessageIter array;
1564 struct ether_addr eth;
1565 unsigned char *addr;
1568 dbus_message_iter_recurse(value, &array);
1569 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1574 result->addr = g_try_malloc(addr_len);
1575 if (result->addr == NULL)
1578 memcpy(result->addr, addr, addr_len);
1579 result->addr_len = addr_len;
1581 result->path = g_try_malloc0(13);
1582 if (result->path == NULL)
1585 memcpy(ð, addr, sizeof(eth));
1586 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1587 eth.ether_addr_octet[0],
1588 eth.ether_addr_octet[1],
1589 eth.ether_addr_octet[2],
1590 eth.ether_addr_octet[3],
1591 eth.ether_addr_octet[4],
1592 eth.ether_addr_octet[5]);
1595 static void extract_ssid(DBusMessageIter *value,
1596 struct supplicant_result *result)
1598 DBusMessageIter array;
1599 unsigned char *ssid;
1602 dbus_message_iter_recurse(value, &array);
1603 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1608 if (ssid[0] == '\0')
1611 result->ssid = g_try_malloc(ssid_len);
1612 if (result->ssid == NULL)
1615 memcpy(result->ssid, ssid, ssid_len);
1616 result->ssid_len = ssid_len;
1618 result->name = g_try_malloc0(ssid_len + 1);
1619 if (result->name == NULL)
1622 for (i = 0; i < ssid_len; i++) {
1623 if (g_ascii_isprint(ssid[i]))
1624 result->name[i] = ssid[i];
1626 result->name[i] = ' ';
1630 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1631 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1633 static void extract_rsn(struct supplicant_result *result,
1634 const unsigned char *buf, int len)
1653 /* Pairwise cipher */
1657 count = buf[0] | (buf[1] << 8);
1658 if (2 + (count * 4) > len)
1661 buf += 2 + (count * 4);
1662 len -= 2 + (count * 4);
1664 /* Authentication */
1668 count = buf[0] | (buf[1] << 8);
1669 if (2 + (count * 4) > len)
1672 for (i = 0; i < count; i++) {
1673 const unsigned char *ptr = buf + 2 + (i * 4);
1675 if (memcmp(ptr, wifi_oui, 3) == 0) {
1678 result->has_8021x = TRUE;
1681 result->has_psk = TRUE;
1684 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1687 result->has_8021x = TRUE;
1690 result->has_psk = TRUE;
1696 buf += 2 + (count * 4);
1697 len -= 2 + (count * 4);
1700 static void extract_wpaie(DBusMessageIter *value,
1701 struct supplicant_result *result)
1703 DBusMessageIter array;
1707 dbus_message_iter_recurse(value, &array);
1708 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1711 result->has_wpa = TRUE;
1712 extract_rsn(result, ie + 6, ie_len - 6);
1716 static void extract_rsnie(DBusMessageIter *value,
1717 struct supplicant_result *result)
1719 DBusMessageIter array;
1723 dbus_message_iter_recurse(value, &array);
1724 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1727 result->has_rsn = TRUE;
1728 extract_rsn(result, ie + 2, ie_len - 2);
1732 static void extract_wpsie(DBusMessageIter *value,
1733 struct supplicant_result *result)
1735 DBusMessageIter array;
1739 dbus_message_iter_recurse(value, &array);
1740 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1743 result->has_wps = TRUE;
1746 static void extract_capabilites(DBusMessageIter *value,
1747 struct supplicant_result *result)
1749 dbus_message_iter_get_basic(value, &result->capabilities);
1751 if (result->capabilities & IEEE80211_CAP_ESS)
1752 result->adhoc = FALSE;
1753 else if (result->capabilities & IEEE80211_CAP_IBSS)
1754 result->adhoc = TRUE;
1756 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1757 result->has_wep = TRUE;
1760 static unsigned char calculate_strength(struct supplicant_task *task,
1761 struct supplicant_result *result)
1763 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1764 unsigned char strength;
1766 if (result->level > 0)
1767 strength = 100 - result->level;
1769 strength = 120 + result->level;
1777 return (result->quality * 100) / task->range->max_qual.qual;
1780 static unsigned short calculate_channel(struct supplicant_result *result)
1782 if (result->frequency < 0)
1785 return (result->frequency - 2407) / 5;
1788 static void get_properties(struct supplicant_task *task);
1790 static void properties_reply(DBusPendingCall *call, void *user_data)
1792 struct supplicant_task *task = user_data;
1793 struct supplicant_result result;
1794 struct supplicant_block *block;
1795 struct connman_network *network;
1797 DBusMessageIter array, dict;
1798 unsigned char strength;
1799 unsigned short channel, frequency;
1800 const char *mode, *security;
1803 DBG("task %p", task);
1805 reply = dbus_pending_call_steal_reply(call);
1807 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1810 memset(&result, 0, sizeof(result));
1811 result.frequency = -1;
1812 result.quality = -1;
1816 dbus_message_iter_init(reply, &array);
1818 dbus_message_iter_recurse(&array, &dict);
1820 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1821 DBusMessageIter entry, value;
1824 dbus_message_iter_recurse(&dict, &entry);
1825 dbus_message_iter_get_basic(&entry, &key);
1827 dbus_message_iter_next(&entry);
1829 dbus_message_iter_recurse(&entry, &value);
1831 //type = dbus_message_iter_get_arg_type(&value);
1832 //dbus_message_iter_get_basic(&value, &val);
1840 * frequency : i (105)
1841 * capabilities : q (113)
1848 if (g_str_equal(key, "bssid") == TRUE)
1849 extract_addr(&value, &result);
1850 else if (g_str_equal(key, "ssid") == TRUE)
1851 extract_ssid(&value, &result);
1852 else if (g_str_equal(key, "wpaie") == TRUE)
1853 extract_wpaie(&value, &result);
1854 else if (g_str_equal(key, "rsnie") == TRUE)
1855 extract_rsnie(&value, &result);
1856 else if (g_str_equal(key, "wpsie") == TRUE)
1857 extract_wpsie(&value, &result);
1858 else if (g_str_equal(key, "capabilities") == TRUE)
1859 extract_capabilites(&value, &result);
1860 else if (g_str_equal(key, "frequency") == TRUE)
1861 dbus_message_iter_get_basic(&value, &result.frequency);
1862 else if (g_str_equal(key, "quality") == TRUE)
1863 dbus_message_iter_get_basic(&value, &result.quality);
1864 else if (g_str_equal(key, "noise") == TRUE)
1865 dbus_message_iter_get_basic(&value, &result.noise);
1866 else if (g_str_equal(key, "level") == TRUE)
1867 dbus_message_iter_get_basic(&value, &result.level);
1868 else if (g_str_equal(key, "maxrate") == TRUE)
1869 dbus_message_iter_get_basic(&value, &result.maxrate);
1871 dbus_message_iter_next(&dict);
1874 DBG("capabilties %u frequency %d "
1875 "quality %d noise %d level %d maxrate %d",
1876 result.capabilities, result.frequency,
1877 result.quality, result.noise,
1878 result.level, result.maxrate);
1880 if (result.path == NULL)
1883 if (result.path[0] == '\0')
1887 block = g_hash_table_lookup(task->hidden_blocks, result.name);
1889 enable_network(task, block->netpath, FALSE);
1890 g_hash_table_remove(task->hidden_blocks, block->ssid);
1894 if (result.ssid == NULL)
1895 task->hidden_found = TRUE;
1897 if (result.frequency > 0 && result.frequency < 14)
1898 result.frequency = 2407 + (5 * result.frequency);
1899 else if (result.frequency == 14)
1900 result.frequency = 2484;
1902 strength = calculate_strength(task, &result);
1903 channel = calculate_channel(&result);
1905 frequency = (result.frequency < 0) ? 0 : result.frequency;
1907 if (result.has_8021x == TRUE)
1908 security = "ieee8021x";
1909 else if (result.has_psk == TRUE)
1911 else if (result.has_wep == TRUE)
1916 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1918 group = build_group(result.path, result.name,
1919 result.ssid, result.ssid_len,
1922 if (result.has_psk == TRUE) {
1923 if (result.has_rsn == TRUE)
1925 else if (result.has_wpa == TRUE)
1929 network = connman_device_get_network(task->device, result.path);
1930 if (network == NULL) {
1933 network = connman_network_create(result.path,
1934 CONNMAN_NETWORK_TYPE_WIFI);
1935 if (network == NULL)
1938 index = connman_device_get_index(task->device);
1939 connman_network_set_index(network, index);
1941 connman_network_set_address(network, result.addr,
1944 if (connman_device_add_network(task->device, network) < 0) {
1945 connman_network_unref(network);
1950 if (result.name != NULL && result.name[0] != '\0')
1951 connman_network_set_name(network, result.name);
1953 if (result.ssid_len != 0)
1954 connman_network_set_blob(network, "WiFi.SSID",
1955 result.ssid, result.ssid_len);
1957 connman_network_set_string(network, "WiFi.Mode", mode);
1959 DBG("%s (%s %s) strength %d (%s)",
1960 result.name, mode, security, strength,
1961 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1963 connman_network_set_available(network, TRUE);
1964 connman_network_set_strength(network, strength);
1966 connman_network_set_uint16(network, "Frequency", frequency);
1967 connman_network_set_uint16(network, "WiFi.Channel", channel);
1968 connman_network_set_string(network, "WiFi.Security", security);
1970 if (result.ssid != NULL)
1971 connman_network_set_group(network, group);
1976 g_free(result.path);
1977 g_free(result.addr);
1978 g_free(result.name);
1979 g_free(result.ssid);
1982 dbus_message_unref(reply);
1984 dbus_pending_call_unref(call);
1986 get_properties(task);
1989 static void get_properties(struct supplicant_task *task)
1991 DBusMessage *message;
1994 path = g_slist_nth_data(task->scan_results, 0);
1996 if (task->hidden_found == TRUE) {
1998 * We're done with regular scanning, let's enable
1999 * the missing network blocks if there are hidden
2002 hidden_block_enable(task);
2007 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
2008 SUPPLICANT_INTF ".BSSID",
2011 task->scan_results = g_slist_remove(task->scan_results, path);
2014 if (message == NULL)
2017 dbus_message_set_auto_start(message, FALSE);
2019 if (dbus_connection_send_with_reply(connection, message,
2020 &task->result_call, TIMEOUT) == FALSE) {
2021 connman_error("Failed to get network properties");
2022 dbus_message_unref(message);
2026 if (task->result_call == NULL) {
2027 connman_error("D-Bus connection not available");
2028 dbus_message_unref(message);
2032 dbus_pending_call_set_notify(task->result_call,
2033 properties_reply, task, NULL);
2035 dbus_message_unref(message);
2040 task->result_call = NULL;
2042 if (task->scanning == TRUE) {
2043 connman_device_set_scanning(task->device, FALSE);
2044 task->scanning = FALSE;
2048 static void scan_results_reply(DBusPendingCall *call, void *user_data)
2050 struct supplicant_task *task = user_data;
2056 DBG("task %p", task);
2058 reply = dbus_pending_call_steal_reply(call);
2060 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
2063 dbus_error_init(&error);
2065 if (dbus_message_get_args(reply, &error,
2066 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
2067 &results, &num_results,
2068 DBUS_TYPE_INVALID) == FALSE) {
2069 if (dbus_error_is_set(&error) == TRUE) {
2070 connman_error("%s", error.message);
2071 dbus_error_free(&error);
2073 connman_error("Wrong arguments for scan result");
2077 if (num_results == 0)
2080 for (i = 0; i < num_results; i++) {
2081 char *path = g_strdup(results[i]);
2085 task->scan_results = g_slist_append(task->scan_results, path);
2088 task->hidden_found = FALSE;
2090 g_strfreev(results);
2092 dbus_message_unref(reply);
2094 dbus_pending_call_unref(call);
2096 get_properties(task);
2101 dbus_message_unref(reply);
2103 dbus_pending_call_unref(call);
2105 task->result_call = NULL;
2107 if (task->scanning == TRUE) {
2108 connman_device_set_scanning(task->device, FALSE);
2109 task->scanning = FALSE;
2113 static void scan_results_available(struct supplicant_task *task)
2115 DBusMessage *message;
2117 DBG("task %p", task);
2119 if (task->result_call != NULL)
2122 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
2123 SUPPLICANT_INTF ".Interface",
2125 if (message == NULL)
2128 dbus_message_set_auto_start(message, FALSE);
2130 if (dbus_connection_send_with_reply(connection, message,
2131 &task->result_call, TIMEOUT) == FALSE) {
2132 connman_error("Failed to request scan result");
2136 if (task->result_call == NULL) {
2137 connman_error("D-Bus connection not available");
2141 if (task->scanning == TRUE)
2142 connman_device_set_scanning(task->device, TRUE);
2144 dbus_pending_call_set_notify(task->result_call,
2145 scan_results_reply, task, NULL);
2148 dbus_message_unref(message);
2151 static enum supplicant_state string2state(const char *state)
2153 if (g_str_equal(state, "INACTIVE") == TRUE)
2154 return WPA_INACTIVE;
2155 else if (g_str_equal(state, "SCANNING") == TRUE)
2156 return WPA_SCANNING;
2157 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
2158 return WPA_ASSOCIATING;
2159 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
2160 return WPA_ASSOCIATED;
2161 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
2162 return WPA_GROUP_HANDSHAKE;
2163 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
2164 return WPA_4WAY_HANDSHAKE;
2165 else if (g_str_equal(state, "COMPLETED") == TRUE)
2166 return WPA_COMPLETED;
2167 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
2168 return WPA_DISCONNECTED;
2173 static int task_connect(struct supplicant_task *task)
2175 const char *address, *security, *passphrase;
2177 unsigned int ssid_len;
2180 g_hash_table_foreach(task->hidden_blocks, block_reset, task);
2182 connman_inet_ifup(task->ifindex);
2184 address = connman_network_get_string(task->network, "Address");
2185 security = connman_network_get_string(task->network, "WiFi.Security");
2186 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
2188 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
2190 DBG("address %s security %s", address, security);
2192 if (security == NULL)
2195 if (passphrase == NULL && g_str_equal(security, "none") == FALSE &&
2196 g_str_equal(security, "ieee8021x") == FALSE)
2199 remove_network(task);
2205 err = set_network(task, ssid, ssid_len, address, security, passphrase);
2209 err = select_network(task);
2213 return -EINPROGRESS;
2216 static void scanning(struct supplicant_task *task, DBusMessage *msg)
2219 dbus_bool_t scanning;
2221 dbus_error_init(&error);
2223 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
2224 DBUS_TYPE_INVALID) == FALSE) {
2225 if (dbus_error_is_set(&error) == TRUE) {
2226 connman_error("%s", error.message);
2227 dbus_error_free(&error);
2229 connman_error("Wrong arguments for scanning");
2233 connman_info("%s scanning %s", task->ifname,
2234 scanning == TRUE ? "started" : "finished");
2236 if (scanning == TRUE)
2237 task->scanning = TRUE;
2240 static void state_change(struct supplicant_task *task, DBusMessage *msg)
2243 const char *newstate, *oldstate;
2244 unsigned char bssid[ETH_ALEN];
2245 unsigned int bssid_len;
2246 enum supplicant_state state, prevstate;
2248 dbus_error_init(&error);
2250 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
2251 DBUS_TYPE_STRING, &oldstate,
2252 DBUS_TYPE_INVALID) == FALSE) {
2253 if (dbus_error_is_set(&error) == TRUE) {
2254 connman_error("%s", error.message);
2255 dbus_error_free(&error);
2257 connman_error("Wrong arguments for state change");
2261 DBG("state %s ==> %s", oldstate, newstate);
2263 connman_info("%s %s%s", task->ifname, newstate,
2264 task->scanning == TRUE ? " (scanning)" : "");
2266 state = string2state(newstate);
2267 if (state == WPA_INVALID)
2270 prevstate = task->state;
2271 task->state = state;
2273 if (task->network == NULL)
2276 switch (task->state) {
2278 switch (prevstate) {
2279 case WPA_ASSOCIATED:
2280 case WPA_GROUP_HANDSHAKE:
2286 /* reset scan trigger and schedule background scan */
2287 connman_device_schedule_scan(task->device);
2289 if (get_bssid(task->device, bssid, &bssid_len) == 0)
2290 connman_network_set_address(task->network,
2294 connman_network_set_connected(task->network, TRUE);
2297 case WPA_ASSOCIATING:
2298 switch (prevstate) {
2302 connman_network_set_associating(task->network, TRUE);
2310 switch (prevstate) {
2312 case WPA_DISCONNECTED:
2319 case WPA_DISCONNECTED:
2321 connman_network_set_connected(task->network, FALSE);
2323 if (task->disconnecting == TRUE) {
2324 connman_network_unref(task->network);
2325 task->disconnecting = FALSE;
2327 if (task->pending_network != NULL) {
2328 task->network = task->pending_network;
2329 task->pending_network = NULL;
2332 task->network = NULL;
2337 connman_network_set_associating(task->network, FALSE);
2344 connman_error("%s invalid state change %s -> %s", task->ifname,
2345 oldstate, newstate);
2348 static gboolean supplicant_filter(DBusConnection *conn,
2349 DBusMessage *msg, void *data)
2351 struct supplicant_task *task;
2352 const char *member, *path;
2354 member = dbus_message_get_member(msg);
2358 path = dbus_message_get_path(msg);
2362 task = find_task_by_path(path);
2366 DBG("task %p member %s", task, member);
2368 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
2369 scan_results_available(task);
2370 else if (g_str_equal(member, "Scanning") == TRUE)
2371 scanning(task, msg);
2372 else if (g_str_equal(member, "StateChange") == TRUE)
2373 state_change(task, msg);
2378 int supplicant_start(struct connman_device *device)
2380 struct supplicant_task *task;
2383 DBG("device %p", device);
2385 task = g_try_new0(struct supplicant_task, 1);
2389 task->ifindex = connman_device_get_index(device);
2390 task->ifname = connman_inet_ifname(task->ifindex);
2392 if (task->ifname == NULL) {
2397 task->cfg80211 = connman_inet_is_cfg80211(task->ifindex);
2398 if (task->cfg80211 == FALSE)
2399 connman_warn("Enabling quirks for unsupported driver");
2401 task->range = g_try_malloc0(sizeof(struct iw_range));
2402 if (task->range == NULL) {
2407 err = get_range(task);
2411 task->device = connman_device_ref(device);
2413 task->created = FALSE;
2414 task->scanning = FALSE;
2415 task->state = WPA_INVALID;
2416 task->disconnecting = FALSE;
2417 task->pending_network = NULL;
2418 task->hidden_blocks = g_hash_table_new_full(g_str_hash, g_str_equal,
2419 NULL, remove_block);
2420 task_list = g_slist_append(task_list, task);
2422 return create_interface(task);
2425 g_free(task->range);
2426 g_free(task->ifname);
2432 int supplicant_stop(struct connman_device *device)
2434 int index = connman_device_get_index(device);
2435 struct supplicant_task *task;
2437 DBG("device %p", device);
2439 task = find_task_by_index(index);
2443 g_free(task->range);
2445 task_list = g_slist_remove(task_list, task);
2446 g_hash_table_destroy(task->hidden_blocks);
2448 if (task->scan_call != NULL) {
2449 dbus_pending_call_cancel(task->scan_call);
2450 task->scan_call = NULL;
2453 if (task->result_call != NULL) {
2454 dbus_pending_call_cancel(task->result_call);
2455 task->result_call = NULL;
2458 if (task->scanning == TRUE)
2459 connman_device_set_scanning(task->device, FALSE);
2461 remove_network(task);
2463 disconnect_network(task);
2465 return remove_interface(task);
2468 int supplicant_scan(struct connman_device *device)
2470 int index = connman_device_get_index(device);
2471 struct supplicant_task *task;
2474 DBG("device %p", device);
2476 task = find_task_by_index(index);
2480 switch (task->state) {
2483 case WPA_ASSOCIATING:
2484 case WPA_ASSOCIATED:
2485 case WPA_4WAY_HANDSHAKE:
2486 case WPA_GROUP_HANDSHAKE:
2492 task->scanning = TRUE;
2494 err = initiate_scan(task);
2496 if (err == -EINPROGRESS)
2499 task->scanning = FALSE;
2503 connman_device_set_scanning(task->device, TRUE);
2508 int supplicant_connect(struct connman_network *network)
2510 struct supplicant_task *task;
2513 DBG("network %p", network);
2515 index = connman_network_get_index(network);
2517 task = find_task_by_index(index);
2521 if (task->disconnecting == TRUE)
2522 task->pending_network = connman_network_ref(network);
2524 task->network = connman_network_ref(network);
2525 return task_connect(task);
2528 return -EINPROGRESS;
2531 int supplicant_disconnect(struct connman_network *network)
2533 struct supplicant_task *task;
2536 DBG("network %p", network);
2538 index = connman_network_get_index(network);
2540 task = find_task_by_index(index);
2544 if (task->disconnecting == TRUE)
2547 if (task->network != network)
2550 remove_network(task);
2552 disconnect_network(task);
2554 task->disconnecting = TRUE;
2559 void supplicant_remove_network(struct connman_network *network)
2561 struct supplicant_task *task;
2564 DBG("network %p", network);
2566 index = connman_network_get_index(network);
2568 task = find_task_by_index(index);
2572 if (task->network != network)
2575 remove_network(task);
2578 static void supplicant_activate(DBusConnection *conn)
2580 DBusMessage *message;
2582 DBG("conn %p", conn);
2584 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
2585 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
2586 if (message == NULL)
2589 dbus_message_set_no_reply(message, TRUE);
2591 dbus_connection_send(conn, message, NULL);
2593 dbus_message_unref(message);
2596 static GSList *driver_list = NULL;
2598 static void supplicant_probe(DBusConnection *conn, void *user_data)
2602 DBG("conn %p", conn);
2604 for (list = driver_list; list; list = list->next) {
2605 struct supplicant_driver *driver = list->data;
2607 DBG("driver %p name %s", driver, driver->name);
2614 static void supplicant_remove(DBusConnection *conn, void *user_data)
2618 DBG("conn %p", conn);
2620 for (list = driver_list; list; list = list->next) {
2621 struct supplicant_driver *driver = list->data;
2623 DBG("driver %p name %s", driver, driver->name);
2631 static guint iface_watch;
2633 static int supplicant_create(void)
2635 if (g_slist_length(driver_list) > 0)
2638 connection = connman_dbus_get_connection();
2639 if (connection == NULL)
2642 DBG("connection %p", connection);
2644 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2645 supplicant_probe, supplicant_remove, NULL, NULL);
2647 iface_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2648 SUPPLICANT_INTF ".Interface",
2649 NULL, supplicant_filter,
2652 if (watch == 0 || iface_watch == 0) {
2653 g_dbus_remove_watch(connection, watch);
2654 g_dbus_remove_watch(connection, iface_watch);
2661 static void supplicant_destroy(void)
2663 if (g_slist_length(driver_list) > 0)
2666 DBG("connection %p", connection);
2668 g_dbus_remove_watch(connection, watch);
2669 g_dbus_remove_watch(connection, iface_watch);
2671 dbus_connection_unref(connection);
2675 int supplicant_register(struct supplicant_driver *driver)
2679 DBG("driver %p name %s", driver, driver->name);
2681 err = supplicant_create();
2685 driver_list = g_slist_append(driver_list, driver);
2687 supplicant_activate(connection);
2692 void supplicant_unregister(struct supplicant_driver *driver)
2694 DBG("driver %p name %s", driver, driver->name);
2696 supplicant_remove(connection, NULL);
2698 driver_list = g_slist_remove(driver_list, driver);
2700 supplicant_destroy();