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);
229 static struct supplicant_task *find_task_by_index(int index)
233 for (list = task_list; list; list = list->next) {
234 struct supplicant_task *task = list->data;
236 if (task->ifindex == index)
243 static struct supplicant_task *find_task_by_path(const char *path)
247 for (list = task_list; list; list = list->next) {
248 struct supplicant_task *task = list->data;
250 if (g_strcmp0(task->path, path) == 0)
257 static int get_range(struct supplicant_task *task)
262 fd = socket(PF_INET, SOCK_DGRAM, 0);
266 memset(&wrq, 0, sizeof(struct iwreq));
267 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
268 wrq.u.data.pointer = task->range;
269 wrq.u.data.length = sizeof(struct iw_range);
271 err = ioctl(fd, SIOCGIWRANGE, &wrq);
276 task->range->max_qual.updated |= IW_QUAL_ALL_INVALID;
278 connman_info("%s {scan} capabilities 0x%02x", task->ifname,
279 task->range->scan_capa);
281 connman_info("%s {quality} flags 0x%02x", task->ifname,
282 task->range->max_qual.updated);
287 static int get_bssid(struct connman_device *device,
288 unsigned char *bssid, unsigned int *bssid_len)
295 ifindex = connman_device_get_index(device);
299 ifname = connman_inet_ifname(ifindex);
303 fd = socket(PF_INET, SOCK_DGRAM, 0);
309 memset(&wrq, 0, sizeof(wrq));
310 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
312 err = ioctl(fd, SIOCGIWAP, &wrq);
320 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
321 *bssid_len = ETH_ALEN;
326 static int enable_network(struct supplicant_task *task, const char *netpath,
327 connman_bool_t enable)
329 DBusMessage *message, *reply;
333 DBG("enable %d", enable);
335 enable_string = enable ? "enable" : "disable";
337 message = dbus_message_new_method_call(SUPPLICANT_NAME, netpath,
338 SUPPLICANT_INTF ".Network", enable_string);
342 dbus_message_set_auto_start(message, FALSE);
344 dbus_error_init(&error);
346 reply = dbus_connection_send_with_reply_and_block(connection,
347 message, -1, &error);
349 if (dbus_error_is_set(&error) == TRUE) {
350 connman_error("%s", error.message);
351 dbus_error_free(&error);
353 connman_error("Failed to select network");
354 dbus_message_unref(message);
358 dbus_message_unref(reply);
360 dbus_message_unref(message);
365 static int set_hidden_network(struct supplicant_task *task, const char *netpath,
366 const unsigned char *ssid, int ssid_len)
368 DBusMessage *message, *reply;
369 DBusMessageIter array, dict;
371 dbus_uint32_t scan_ssid = 1;
372 const char *invalid_address = "ff:ff:ff:ff:ff:ff";
374 message = dbus_message_new_method_call(SUPPLICANT_NAME, netpath,
375 SUPPLICANT_INTF ".Network", "set");
379 dbus_message_set_auto_start(message, FALSE);
381 dbus_message_iter_init_append(message, &array);
383 connman_dbus_dict_open(&array, &dict);
385 connman_dbus_dict_append_basic(&dict, "scan_ssid",
386 DBUS_TYPE_UINT32, &scan_ssid);
388 connman_dbus_dict_append_fixed_array(&dict, "ssid",
389 DBUS_TYPE_BYTE, &ssid, ssid_len);
392 * We're setting an invalid BSSID to prevent wpa_s from associating
393 * automatically to this block once it's found.
395 connman_dbus_dict_append_basic(&dict, "bssid",
396 DBUS_TYPE_STRING, &invalid_address);
398 connman_dbus_dict_close(&array, &dict);
400 dbus_error_init(&error);
402 reply = dbus_connection_send_with_reply_and_block(connection,
403 message, -1, &error);
405 if (dbus_error_is_set(&error) == TRUE) {
406 connman_error("%s", error.message);
407 dbus_error_free(&error);
409 connman_error("Failed to set network options");
410 dbus_message_unref(message);
414 dbus_message_unref(reply);
416 dbus_message_unref(message);
421 static void block_reset(gpointer key, gpointer value, gpointer user_data)
423 struct supplicant_block *block = value;
424 struct supplicant_task *task = user_data;
426 block->num_scans = 0;
428 enable_network(task, block->netpath, FALSE);
430 block->enabled = FALSE;
433 #define MAX_BLOCK_SCANS 2
434 static void hidden_block_enable(struct supplicant_task *task)
438 struct supplicant_block *block;
440 DBG("network %p", task->network);
442 if (g_hash_table_size(task->hidden_blocks) == 0)
446 * If we're associated or associating, we no longer need to
447 * look for hidden networks.
453 * We go through the block list and:
454 * - If we scanned it more than twice, we disable it and move
455 * on to the next block.
456 * - If the next block is not enabled, we enable it, start
457 * the scan counter, and return. This routine will be called
458 * again when the next scan results are available.
459 * - If we're done with all the blocks there, we just reset them.
461 g_hash_table_iter_init(&iter, task->hidden_blocks);
462 while (g_hash_table_iter_next(&iter, &key, &value)) {
465 DBG("%s num of scans %d enabled %d",
466 block->ssid, block->num_scans, block->enabled);
468 if (block->num_scans > MAX_BLOCK_SCANS) {
469 if (block->enabled == FALSE)
472 enable_network(task, block->netpath, FALSE);
473 block->enabled = FALSE;
477 if (block->enabled == FALSE) {
478 enable_network(task, block->netpath, TRUE);
479 block->enabled = TRUE;
487 g_hash_table_foreach(task->hidden_blocks, block_reset, task);
490 static int add_hidden_network(struct supplicant_task *task,
491 const unsigned char *ssid, int ssid_len)
493 DBusMessage *message, *reply;
496 struct supplicant_block *block;
497 char *netpath = NULL;
500 DBG("task %p", task);
502 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
503 SUPPLICANT_INTF ".Interface", "addNetwork");
507 dbus_message_set_auto_start(message, FALSE);
509 dbus_error_init(&error);
511 reply = dbus_connection_send_with_reply_and_block(connection,
512 message, -1, &error);
514 if (dbus_error_is_set(&error) == TRUE) {
515 connman_error("%s", error.message);
516 dbus_error_free(&error);
518 connman_error("Failed to add network");
519 dbus_message_unref(message);
523 dbus_error_init(&error);
525 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
526 DBUS_TYPE_INVALID) == FALSE) {
527 if (dbus_error_is_set(&error) == TRUE) {
528 connman_error("%s", error.message);
529 dbus_error_free(&error);
531 connman_error("Wrong arguments for network");
532 dbus_message_unref(reply);
536 netpath = g_strdup(path);
538 ret = set_hidden_network(task, netpath, ssid, ssid_len);
542 block = g_try_new0(struct supplicant_block, 1);
546 block->ssid = g_try_malloc0(ssid_len + 1);
547 if (block->ssid == NULL) {
552 for (i = 0; i < ssid_len; i++) {
553 if (g_ascii_isprint(ssid[i]))
554 block->ssid[i] = ssid[i];
556 block->ssid[i] = ' ';
559 block->netpath = netpath;
560 block->enabled = FALSE;
561 block->num_scans = 0;
563 DBG("path %s ssid %s", block->netpath, block->ssid);
565 g_hash_table_replace(task->hidden_blocks, block->ssid, block);
571 dbus_message_unref(reply);
573 dbus_message_unref(message);
578 static void add_interface_reply(DBusPendingCall *call, void *user_data)
580 struct supplicant_task *task = user_data;
584 char **hex_ssids, *hex_ssid;
587 DBG("task %p", task);
589 reply = dbus_pending_call_steal_reply(call);
591 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
594 dbus_error_init(&error);
596 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
597 DBUS_TYPE_INVALID) == FALSE) {
598 if (dbus_error_is_set(&error) == TRUE) {
599 connman_error("%s", error.message);
600 dbus_error_free(&error);
602 connman_error("Wrong arguments for add interface");
606 DBG("path %s", path);
608 task->path = g_strdup(path);
609 task->created = TRUE;
611 connman_device_set_powered(task->device, TRUE);
613 dbus_message_unref(reply);
615 dbus_pending_call_unref(call);
617 hex_ssids = connman_wifi_load_ssid();
618 if (hex_ssids == NULL)
621 for (i = 0; hex_ssids[i]; i++) {
623 unsigned int j, k = 0, hex;
626 hex_ssid = hex_ssids[i];
627 hex_ssid_len = strlen(hex_ssid);
629 ssid = g_try_malloc0(hex_ssid_len / 2 + 1);
633 for (j = 0, k = 0; j < hex_ssid_len; j += 2) {
634 sscanf(hex_ssid + j, "%02x", &hex);
638 if (add_hidden_network(task, ssid, hex_ssid_len / 2) < 0) {
646 g_strfreev(hex_ssids);
651 dbus_message_unref(reply);
653 dbus_pending_call_unref(call);
655 task_list = g_slist_remove(task_list, task);
657 connman_device_unref(task->device);
662 static int add_interface(struct supplicant_task *task)
664 const char *driver = connman_option_get_string("wifi");
665 DBusMessage *message;
666 DBusMessageIter array, dict;
667 DBusPendingCall *call;
669 DBG("task %p", task);
671 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
672 SUPPLICANT_INTF, "addInterface");
676 dbus_message_set_auto_start(message, FALSE);
678 dbus_message_iter_init_append(message, &array);
680 dbus_message_iter_append_basic(&array,
681 DBUS_TYPE_STRING, &task->ifname);
683 connman_dbus_dict_open(&array, &dict);
685 connman_dbus_dict_append_basic(&dict, "driver",
686 DBUS_TYPE_STRING, &driver);
688 connman_dbus_dict_close(&array, &dict);
690 if (dbus_connection_send_with_reply(connection, message,
691 &call, TIMEOUT) == FALSE) {
692 connman_error("Failed to add interface");
693 dbus_message_unref(message);
698 connman_error("D-Bus connection not available");
699 dbus_message_unref(message);
703 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
705 dbus_message_unref(message);
710 static void get_interface_reply(DBusPendingCall *call, void *user_data)
712 struct supplicant_task *task = user_data;
717 DBG("task %p", task);
719 reply = dbus_pending_call_steal_reply(call);
721 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
726 dbus_error_init(&error);
728 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
729 DBUS_TYPE_INVALID) == FALSE) {
730 if (dbus_error_is_set(&error) == TRUE) {
731 connman_error("%s", error.message);
732 dbus_error_free(&error);
734 connman_error("Wrong arguments for get interface");
738 DBG("path %s", path);
740 task->path = g_strdup(path);
741 task->created = FALSE;
743 connman_device_set_powered(task->device, TRUE);
746 dbus_message_unref(reply);
748 dbus_pending_call_unref(call);
751 static int create_interface(struct supplicant_task *task)
753 DBusMessage *message;
754 DBusPendingCall *call;
756 DBG("task %p", task);
758 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
759 SUPPLICANT_INTF, "getInterface");
763 dbus_message_set_auto_start(message, FALSE);
765 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
768 if (dbus_connection_send_with_reply(connection, message,
769 &call, TIMEOUT) == FALSE) {
770 connman_error("Failed to get interface");
771 dbus_message_unref(message);
776 connman_error("D-Bus connection not available");
777 dbus_message_unref(message);
781 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
783 dbus_message_unref(message);
788 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
790 struct supplicant_task *task = user_data;
793 DBG("task %p", task);
795 reply = dbus_pending_call_steal_reply(call);
797 connman_device_set_powered(task->device, FALSE);
799 connman_device_unref(task->device);
801 connman_inet_ifdown(task->ifindex);
805 dbus_message_unref(reply);
807 dbus_pending_call_unref(call);
810 static int remove_interface(struct supplicant_task *task)
812 DBusMessage *message;
813 DBusPendingCall *call;
815 DBG("task %p", task);
817 if (task->path == NULL)
821 if (task->created == FALSE) {
822 connman_device_set_powered(task->device, FALSE);
827 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
828 SUPPLICANT_INTF, "removeInterface");
832 dbus_message_set_auto_start(message, FALSE);
834 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
837 if (dbus_connection_send_with_reply(connection, message,
838 &call, TIMEOUT) == FALSE) {
839 connman_error("Failed to remove interface");
840 dbus_message_unref(message);
845 connman_error("D-Bus connection not available");
846 dbus_message_unref(message);
850 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
852 dbus_message_unref(message);
857 static int set_ap_scan(struct supplicant_task *task)
859 DBusMessage *message, *reply;
863 DBG("task %p", task);
865 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
866 SUPPLICANT_INTF ".Interface", "setAPScan");
870 dbus_message_set_auto_start(message, FALSE);
872 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
875 dbus_error_init(&error);
877 reply = dbus_connection_send_with_reply_and_block(connection,
878 message, -1, &error);
880 if (dbus_error_is_set(&error) == TRUE) {
881 connman_error("%s", error.message);
882 dbus_error_free(&error);
884 connman_error("Failed to set AP scan");
885 dbus_message_unref(message);
889 dbus_message_unref(message);
891 dbus_message_unref(reply);
896 static int add_network(struct supplicant_task *task)
898 DBusMessage *message, *reply;
902 DBG("task %p", task);
904 if (task->netpath != NULL)
907 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
908 SUPPLICANT_INTF ".Interface", "addNetwork");
912 dbus_message_set_auto_start(message, FALSE);
914 dbus_error_init(&error);
916 reply = dbus_connection_send_with_reply_and_block(connection,
917 message, -1, &error);
919 if (dbus_error_is_set(&error) == TRUE) {
920 connman_error("%s", error.message);
921 dbus_error_free(&error);
923 connman_error("Failed to add network");
924 dbus_message_unref(message);
928 dbus_message_unref(message);
930 dbus_error_init(&error);
932 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
933 DBUS_TYPE_INVALID) == FALSE) {
934 if (dbus_error_is_set(&error) == TRUE) {
935 connman_error("%s", error.message);
936 dbus_error_free(&error);
938 connman_error("Wrong arguments for network");
939 dbus_message_unref(reply);
943 DBG("path %s", path);
945 task->netpath = g_strdup(path);
947 dbus_message_unref(reply);
952 static int remove_network(struct supplicant_task *task)
954 DBusMessage *message, *reply;
957 DBG("task %p", task);
959 if (task->netpath == NULL || task->path == NULL)
962 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
963 SUPPLICANT_INTF ".Interface", "removeNetwork");
967 dbus_message_set_auto_start(message, FALSE);
969 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
972 dbus_error_init(&error);
974 reply = dbus_connection_send_with_reply_and_block(connection,
975 message, -1, &error);
977 if (dbus_error_is_set(&error) == TRUE) {
978 connman_error("%s", error.message);
979 dbus_error_free(&error);
981 connman_error("Failed to remove network");
982 dbus_message_unref(message);
986 dbus_message_unref(message);
988 dbus_message_unref(reply);
990 g_free(task->netpath);
991 task->netpath = NULL;
996 static int select_network(struct supplicant_task *task)
998 DBusMessage *message, *reply;
1001 DBG("task %p", task);
1003 if (task->netpath == NULL)
1006 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1007 SUPPLICANT_INTF ".Interface", "selectNetwork");
1008 if (message == NULL)
1011 dbus_message_set_auto_start(message, FALSE);
1013 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
1016 dbus_error_init(&error);
1018 reply = dbus_connection_send_with_reply_and_block(connection,
1019 message, -1, &error);
1020 if (reply == NULL) {
1021 if (dbus_error_is_set(&error) == TRUE) {
1022 connman_error("%s", error.message);
1023 dbus_error_free(&error);
1025 connman_error("Failed to select network");
1026 dbus_message_unref(message);
1030 dbus_message_unref(message);
1032 dbus_message_unref(reply);
1037 static int disconnect_network(struct supplicant_task *task)
1039 DBusMessage *message, *reply;
1042 DBG("task %p", task);
1044 if (task->path == NULL)
1047 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1048 SUPPLICANT_INTF ".Interface", "disconnect");
1049 if (message == NULL)
1052 dbus_message_set_auto_start(message, FALSE);
1054 dbus_error_init(&error);
1056 reply = dbus_connection_send_with_reply_and_block(connection,
1057 message, -1, &error);
1058 if (reply == NULL) {
1059 if (dbus_error_is_set(&error) == TRUE) {
1060 connman_error("%s", error.message);
1061 dbus_error_free(&error);
1063 connman_error("Failed to disconnect network");
1064 dbus_message_unref(message);
1068 dbus_message_unref(message);
1070 dbus_message_unref(reply);
1075 static int set_network_tls(struct connman_network *network,
1076 DBusMessageIter *dict)
1078 const char *private_key, *client_cert, *ca_cert;
1079 const char *private_key_password;
1082 * For TLS, we at least need:
1083 * The client certificate
1084 * The client private key file
1085 * The client private key file password
1087 * The Authority certificate is optional.
1089 client_cert = connman_network_get_string(network,
1090 "WiFi.ClientCertFile");
1091 if (client_cert == NULL) {
1092 connman_error("Error in TLS authentication: "
1093 "a ClientCertFile must be defined\n");
1097 private_key = connman_network_get_string(network,
1098 "WiFi.PrivateKeyFile");
1099 if (private_key == NULL) {
1100 connman_error("Error in TLS authentication: "
1101 "a PrivateKeyFile must be defined\n");
1105 private_key_password = connman_network_get_string(network,
1106 "WiFi.PrivateKeyPassphrase");
1107 if (private_key_password == NULL) {
1108 connman_error("Error in TLS authentication: "
1109 "a PrivateKeyPassphrase must be defined\n");
1113 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
1115 connman_dbus_dict_append_basic(dict, "ca_cert",
1116 DBUS_TYPE_STRING, &ca_cert);
1118 connman_info("No CACertFile has been provided "
1119 "to do the TLS authentication\n");
1121 DBG("client cert %s private key %s", client_cert, private_key);
1123 connman_dbus_dict_append_basic(dict, "private_key",
1124 DBUS_TYPE_STRING, &private_key);
1125 connman_dbus_dict_append_basic(dict, "private_key_passwd",
1127 &private_key_password);
1128 connman_dbus_dict_append_basic(dict, "client_cert",
1129 DBUS_TYPE_STRING, &client_cert);
1134 static int set_network_peap(struct connman_network *network,
1135 DBusMessageIter *dict, const char *passphrase)
1137 const char *client_cert, *ca_cert, *phase2;
1141 * For PEAP/TTLS, we at least need
1142 * The authority certificate
1143 * The 2nd phase authentication method
1144 * The 2nd phase passphrase
1146 * The Client certificate is optional although strongly required
1147 * When setting it, we need in addition
1148 * The Client private key file
1149 * The Client private key file password
1151 if (passphrase == NULL) {
1152 connman_error("Error in PEAP/TTLS authentication: "
1153 "a phase2 passphrase must be defined\n");
1157 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
1158 if (ca_cert == NULL) {
1159 connman_error("Error in PEAP/TTLS authentication: "
1160 "CACertFile must be defined\n");
1164 phase2 = connman_network_get_string(network, "WiFi.Phase2");
1165 if (phase2 == NULL) {
1166 connman_error("Error in PEAP/TTLS authentication: "
1167 "Phase2 must be defined\n");
1171 DBG("CA cert %s phase2 auth %s", ca_cert, phase2);
1173 client_cert = connman_network_get_string(network,
1174 "WiFi.ClientCertFile");
1176 const char *private_key, *private_key_password;
1178 private_key = connman_network_get_string(network,
1179 "WiFi.PrivateKeyFile");
1180 if (private_key == NULL) {
1181 connman_error("Error in PEAP/TTLS authentication: "
1182 "with ClientCertFile, "
1183 "PrivateKeyFile must be defined\n");
1187 private_key_password =
1188 connman_network_get_string(network,
1189 "WiFi.PrivateKeyPassphrase");
1190 if (private_key_password == NULL) {
1191 connman_error("Error in PEAP/TTLS authentication: "
1192 "with ClientCertFile, "
1193 "PrivateKeyPassphrase must be defined\n");
1197 connman_dbus_dict_append_basic(dict, "client_cert",
1198 DBUS_TYPE_STRING, &client_cert);
1200 connman_dbus_dict_append_basic(dict, "private_key",
1201 DBUS_TYPE_STRING, &private_key);
1203 connman_dbus_dict_append_basic(dict, "private_key_passwd",
1205 &private_key_password);
1207 DBG("client cert %s private key %s", client_cert, private_key);
1209 connman_info("No client certificate has been provided "
1210 "to do the PEAP/TTLS authentication\n");
1212 phase2_auth = g_strdup_printf("\"auth=%s\"", phase2);
1214 connman_dbus_dict_append_basic(dict, "password",
1215 DBUS_TYPE_STRING, &passphrase);
1217 connman_dbus_dict_append_basic(dict, "ca_cert",
1218 DBUS_TYPE_STRING, &ca_cert);
1220 connman_dbus_dict_append_basic(dict, "phase2",
1221 DBUS_TYPE_STRING, &phase2_auth);
1223 g_free(phase2_auth);
1228 static int set_network(struct supplicant_task *task,
1229 const unsigned char *network, int len,
1230 const char *address, const char *security,
1231 const char *passphrase)
1233 DBusMessage *message, *reply;
1234 DBusMessageIter array, dict;
1236 dbus_uint32_t scan_ssid = 1;
1238 DBG("task %p", task);
1240 if (task->netpath == NULL)
1243 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
1244 SUPPLICANT_INTF ".Network", "set");
1245 if (message == NULL)
1248 dbus_message_set_auto_start(message, FALSE);
1250 dbus_message_iter_init_append(message, &array);
1252 connman_dbus_dict_open(&array, &dict);
1254 connman_dbus_dict_append_basic(&dict, "scan_ssid",
1255 DBUS_TYPE_UINT32, &scan_ssid);
1258 connman_dbus_dict_append_fixed_array(&dict, "ssid",
1259 DBUS_TYPE_BYTE, &network, len);
1261 connman_dbus_dict_append_basic(&dict, "bssid",
1262 DBUS_TYPE_STRING, &address);
1264 if (g_ascii_strcasecmp(security, "psk") == 0 ||
1265 g_ascii_strcasecmp(security, "wpa") == 0 ||
1266 g_ascii_strcasecmp(security, "rsn") == 0) {
1267 const char *key_mgmt = "WPA-PSK";
1268 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1269 DBUS_TYPE_STRING, &key_mgmt);
1271 if (passphrase && strlen(passphrase) > 0)
1272 connman_dbus_dict_append_basic(&dict, "psk",
1273 DBUS_TYPE_STRING, &passphrase);
1274 } else if (g_ascii_strcasecmp(security, "ieee8021x") == 0) {
1275 struct connman_network *network = task->network;
1276 const char *key_mgmt = "WPA-EAP", *eap, *identity;
1280 * If our private key password is unset,
1281 * we use the supplied passphrase. That is needed
1282 * for PEAP where 2 passphrases (identity and client
1283 * cert may have to be provided.
1285 if (connman_network_get_string(network,
1286 "WiFi.PrivateKeyPassphrase") == NULL)
1287 connman_network_set_string(network,
1288 "WiFi.PrivateKeyPassphrase",
1291 eap = connman_network_get_string(network, "WiFi.EAP");
1295 /* We must have an identity for both PEAP and TLS */
1296 identity = connman_network_get_string(network, "WiFi.Identity");
1297 if (identity == NULL)
1300 DBG("key_mgmt %s eap %s identity %s", key_mgmt, eap, identity);
1302 if (g_strcmp0(eap, "tls") == 0) {
1305 err = set_network_tls(network, &dict);
1307 dbus_message_unref(message);
1310 } else if (g_strcmp0(eap, "peap") == 0 ||
1311 g_strcmp0(eap, "ttls") == 0) {
1314 err = set_network_peap(network, &dict, passphrase);
1316 dbus_message_unref(message);
1320 connman_error("Unknown EAP %s", eap);
1324 /* wpa_supplicant only accepts upper case EAPs */
1325 eap_value = g_ascii_strup(eap, -1);
1327 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1330 connman_dbus_dict_append_basic(&dict, "eap",
1333 connman_dbus_dict_append_basic(&dict, "identity",
1339 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
1340 const char *key_mgmt = "NONE";
1341 const char *auth_alg = "OPEN";
1342 const char *key_index = "0";
1344 if (task->cfg80211 == TRUE)
1345 auth_alg = "OPEN SHARED";
1347 connman_dbus_dict_append_basic(&dict, "auth_alg",
1348 DBUS_TYPE_STRING, &auth_alg);
1350 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1351 DBUS_TYPE_STRING, &key_mgmt);
1354 int size = strlen(passphrase);
1355 if (size == 10 || size == 26) {
1356 unsigned char *key = malloc(13);
1359 memset(tmp, 0, sizeof(tmp));
1362 for (i = 0; i < size / 2; i++) {
1363 memcpy(tmp, passphrase + (i * 2), 2);
1364 key[i] = (unsigned char) strtol(tmp,
1367 connman_dbus_dict_append_fixed_array(&dict,
1368 "wep_key0", DBUS_TYPE_BYTE,
1371 } else if (size == 5 || size == 13) {
1372 unsigned char *key = malloc(13);
1376 for (i = 0; i < size; i++)
1377 key[i] = (unsigned char) passphrase[i];
1378 connman_dbus_dict_append_fixed_array(&dict,
1379 "wep_key0", DBUS_TYPE_BYTE,
1383 connman_dbus_dict_append_basic(&dict,
1384 "wep_key0", DBUS_TYPE_STRING,
1387 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
1388 DBUS_TYPE_STRING, &key_index);
1391 const char *key_mgmt = "NONE";
1392 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1393 DBUS_TYPE_STRING, &key_mgmt);
1396 connman_dbus_dict_close(&array, &dict);
1398 dbus_error_init(&error);
1400 reply = dbus_connection_send_with_reply_and_block(connection,
1401 message, -1, &error);
1402 if (reply == NULL) {
1403 if (dbus_error_is_set(&error) == TRUE) {
1404 connman_error("%s", error.message);
1405 dbus_error_free(&error);
1407 connman_error("Failed to set network options");
1408 dbus_message_unref(message);
1412 dbus_message_unref(message);
1414 dbus_message_unref(reply);
1419 dbus_message_unref(message);
1423 static void scan_reply(DBusPendingCall *call, void *user_data)
1425 struct supplicant_task *task = user_data;
1428 DBG("task %p", task);
1430 task->scan_call = NULL;
1432 reply = dbus_pending_call_steal_reply(call);
1434 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1435 connman_device_set_scanning(task->device, FALSE);
1439 if (task->scanning == TRUE)
1440 connman_device_set_scanning(task->device, TRUE);
1443 dbus_message_unref(reply);
1445 dbus_pending_call_unref(call);
1449 static int initiate_scan(struct supplicant_task *task)
1451 DBusMessage *message;
1453 DBG("task %p", task);
1455 if (task->path == NULL)
1458 if (task->scan_call != NULL)
1461 connman_info("%s initiating scan", task->ifname);
1463 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1464 SUPPLICANT_INTF ".Interface", "scan");
1465 if (message == NULL)
1468 dbus_message_set_auto_start(message, FALSE);
1470 if (dbus_connection_send_with_reply(connection, message,
1471 &task->scan_call, TIMEOUT) == FALSE) {
1472 connman_error("Failed to initiate scan");
1473 dbus_message_unref(message);
1477 if (task->scan_call == NULL) {
1478 connman_error("D-Bus connection not available");
1479 dbus_message_unref(message);
1483 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
1485 dbus_message_unref(message);
1487 return -EINPROGRESS;
1493 } special_ssid[] = {
1494 { "<hidden>", "hidden" },
1495 { "default", "linksys" },
1503 { "comcomcom", "3com" },
1505 { "Symbol", "symbol" },
1506 { "Motorola", "motorola" },
1507 { "Wireless" , "wireless" },
1512 static char *build_group(const char *addr, const char *name,
1513 const unsigned char *ssid, unsigned int ssid_len,
1514 const char *mode, const char *security)
1522 str = g_string_sized_new((ssid_len * 2) + 24);
1527 g_string_append_printf(str, "hidden_%s", addr);
1531 for (i = 0; special_ssid[i].name; i++) {
1532 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1533 if (special_ssid[i].value == NULL)
1534 g_string_append_printf(str, "%s_%s",
1537 g_string_append_printf(str, "%s_%s",
1538 special_ssid[i].value, addr);
1543 if (ssid_len > 0 && ssid[0] != '\0') {
1544 for (i = 0; i < ssid_len; i++)
1545 g_string_append_printf(str, "%02x", ssid[i]);
1547 g_string_append_printf(str, "hidden_%s", addr);
1550 g_string_append_printf(str, "_%s_%s", mode, security);
1552 return g_string_free(str, FALSE);
1555 static void extract_addr(DBusMessageIter *value,
1556 struct supplicant_result *result)
1558 DBusMessageIter array;
1559 struct ether_addr eth;
1560 unsigned char *addr;
1563 dbus_message_iter_recurse(value, &array);
1564 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1569 result->addr = g_try_malloc(addr_len);
1570 if (result->addr == NULL)
1573 memcpy(result->addr, addr, addr_len);
1574 result->addr_len = addr_len;
1576 result->path = g_try_malloc0(13);
1577 if (result->path == NULL)
1580 memcpy(ð, addr, sizeof(eth));
1581 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1582 eth.ether_addr_octet[0],
1583 eth.ether_addr_octet[1],
1584 eth.ether_addr_octet[2],
1585 eth.ether_addr_octet[3],
1586 eth.ether_addr_octet[4],
1587 eth.ether_addr_octet[5]);
1590 static void extract_ssid(DBusMessageIter *value,
1591 struct supplicant_result *result)
1593 DBusMessageIter array;
1594 unsigned char *ssid;
1597 dbus_message_iter_recurse(value, &array);
1598 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1603 if (ssid[0] == '\0')
1606 result->ssid = g_try_malloc(ssid_len);
1607 if (result->ssid == NULL)
1610 memcpy(result->ssid, ssid, ssid_len);
1611 result->ssid_len = ssid_len;
1613 result->name = g_try_malloc0(ssid_len + 1);
1614 if (result->name == NULL)
1617 for (i = 0; i < ssid_len; i++) {
1618 if (g_ascii_isprint(ssid[i]))
1619 result->name[i] = ssid[i];
1621 result->name[i] = ' ';
1625 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1626 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1628 static void extract_rsn(struct supplicant_result *result,
1629 const unsigned char *buf, int len)
1648 /* Pairwise cipher */
1652 count = buf[0] | (buf[1] << 8);
1653 if (2 + (count * 4) > len)
1656 buf += 2 + (count * 4);
1657 len -= 2 + (count * 4);
1659 /* Authentication */
1663 count = buf[0] | (buf[1] << 8);
1664 if (2 + (count * 4) > len)
1667 for (i = 0; i < count; i++) {
1668 const unsigned char *ptr = buf + 2 + (i * 4);
1670 if (memcmp(ptr, wifi_oui, 3) == 0) {
1673 result->has_8021x = TRUE;
1676 result->has_psk = TRUE;
1679 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1682 result->has_8021x = TRUE;
1685 result->has_psk = TRUE;
1691 buf += 2 + (count * 4);
1692 len -= 2 + (count * 4);
1695 static void extract_wpaie(DBusMessageIter *value,
1696 struct supplicant_result *result)
1698 DBusMessageIter array;
1702 dbus_message_iter_recurse(value, &array);
1703 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1706 result->has_wpa = TRUE;
1707 extract_rsn(result, ie + 6, ie_len - 6);
1711 static void extract_rsnie(DBusMessageIter *value,
1712 struct supplicant_result *result)
1714 DBusMessageIter array;
1718 dbus_message_iter_recurse(value, &array);
1719 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1722 result->has_rsn = TRUE;
1723 extract_rsn(result, ie + 2, ie_len - 2);
1727 static void extract_wpsie(DBusMessageIter *value,
1728 struct supplicant_result *result)
1730 DBusMessageIter array;
1734 dbus_message_iter_recurse(value, &array);
1735 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1738 result->has_wps = TRUE;
1741 static void extract_capabilites(DBusMessageIter *value,
1742 struct supplicant_result *result)
1744 dbus_message_iter_get_basic(value, &result->capabilities);
1746 if (result->capabilities & IEEE80211_CAP_ESS)
1747 result->adhoc = FALSE;
1748 else if (result->capabilities & IEEE80211_CAP_IBSS)
1749 result->adhoc = TRUE;
1751 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1752 result->has_wep = TRUE;
1755 static unsigned char calculate_strength(struct supplicant_task *task,
1756 struct supplicant_result *result)
1758 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1759 unsigned char strength;
1761 if (result->level > 0)
1762 strength = 100 - result->level;
1764 strength = 120 + result->level;
1772 return (result->quality * 100) / task->range->max_qual.qual;
1775 static unsigned short calculate_channel(struct supplicant_result *result)
1777 if (result->frequency < 0)
1780 return (result->frequency - 2407) / 5;
1783 static void get_properties(struct supplicant_task *task);
1785 static void properties_reply(DBusPendingCall *call, void *user_data)
1787 struct supplicant_task *task = user_data;
1788 struct supplicant_result result;
1789 struct supplicant_block *block;
1790 struct connman_network *network;
1792 DBusMessageIter array, dict;
1793 unsigned char strength;
1794 unsigned short channel, frequency;
1795 const char *mode, *security;
1798 DBG("task %p", task);
1800 reply = dbus_pending_call_steal_reply(call);
1802 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1805 memset(&result, 0, sizeof(result));
1806 result.frequency = -1;
1807 result.quality = -1;
1811 dbus_message_iter_init(reply, &array);
1813 dbus_message_iter_recurse(&array, &dict);
1815 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1816 DBusMessageIter entry, value;
1819 dbus_message_iter_recurse(&dict, &entry);
1820 dbus_message_iter_get_basic(&entry, &key);
1822 dbus_message_iter_next(&entry);
1824 dbus_message_iter_recurse(&entry, &value);
1826 //type = dbus_message_iter_get_arg_type(&value);
1827 //dbus_message_iter_get_basic(&value, &val);
1835 * frequency : i (105)
1836 * capabilities : q (113)
1843 if (g_str_equal(key, "bssid") == TRUE)
1844 extract_addr(&value, &result);
1845 else if (g_str_equal(key, "ssid") == TRUE)
1846 extract_ssid(&value, &result);
1847 else if (g_str_equal(key, "wpaie") == TRUE)
1848 extract_wpaie(&value, &result);
1849 else if (g_str_equal(key, "rsnie") == TRUE)
1850 extract_rsnie(&value, &result);
1851 else if (g_str_equal(key, "wpsie") == TRUE)
1852 extract_wpsie(&value, &result);
1853 else if (g_str_equal(key, "capabilities") == TRUE)
1854 extract_capabilites(&value, &result);
1855 else if (g_str_equal(key, "frequency") == TRUE)
1856 dbus_message_iter_get_basic(&value, &result.frequency);
1857 else if (g_str_equal(key, "quality") == TRUE)
1858 dbus_message_iter_get_basic(&value, &result.quality);
1859 else if (g_str_equal(key, "noise") == TRUE)
1860 dbus_message_iter_get_basic(&value, &result.noise);
1861 else if (g_str_equal(key, "level") == TRUE)
1862 dbus_message_iter_get_basic(&value, &result.level);
1863 else if (g_str_equal(key, "maxrate") == TRUE)
1864 dbus_message_iter_get_basic(&value, &result.maxrate);
1866 dbus_message_iter_next(&dict);
1869 DBG("capabilties %u frequency %d "
1870 "quality %d noise %d level %d maxrate %d",
1871 result.capabilities, result.frequency,
1872 result.quality, result.noise,
1873 result.level, result.maxrate);
1875 if (result.path == NULL)
1878 if (result.path[0] == '\0')
1882 block = g_hash_table_lookup(task->hidden_blocks, result.name);
1884 enable_network(task, block->netpath, FALSE);
1885 g_hash_table_remove(task->hidden_blocks, block->ssid);
1889 if (result.ssid == NULL)
1890 task->hidden_found = TRUE;
1892 if (result.frequency > 0 && result.frequency < 14)
1893 result.frequency = 2407 + (5 * result.frequency);
1894 else if (result.frequency == 14)
1895 result.frequency = 2484;
1897 strength = calculate_strength(task, &result);
1898 channel = calculate_channel(&result);
1900 frequency = (result.frequency < 0) ? 0 : result.frequency;
1902 if (result.has_8021x == TRUE)
1903 security = "ieee8021x";
1904 else if (result.has_psk == TRUE)
1906 else if (result.has_wep == TRUE)
1911 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1913 group = build_group(result.path, result.name,
1914 result.ssid, result.ssid_len,
1917 if (result.has_psk == TRUE) {
1918 if (result.has_rsn == TRUE)
1920 else if (result.has_wpa == TRUE)
1924 network = connman_device_get_network(task->device, result.path);
1925 if (network == NULL) {
1928 network = connman_network_create(result.path,
1929 CONNMAN_NETWORK_TYPE_WIFI);
1930 if (network == NULL)
1933 index = connman_device_get_index(task->device);
1934 connman_network_set_index(network, index);
1936 connman_network_set_address(network, result.addr,
1939 if (connman_device_add_network(task->device, network) < 0) {
1940 connman_network_unref(network);
1945 if (result.name != NULL && result.name[0] != '\0')
1946 connman_network_set_name(network, result.name);
1948 if (result.ssid_len != 0)
1949 connman_network_set_blob(network, "WiFi.SSID",
1950 result.ssid, result.ssid_len);
1952 connman_network_set_string(network, "WiFi.Mode", mode);
1954 DBG("%s (%s %s) strength %d (%s)",
1955 result.name, mode, security, strength,
1956 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1958 connman_network_set_available(network, TRUE);
1959 connman_network_set_strength(network, strength);
1961 connman_network_set_uint16(network, "Frequency", frequency);
1962 connman_network_set_uint16(network, "WiFi.Channel", channel);
1963 connman_network_set_string(network, "WiFi.Security", security);
1965 if (result.ssid != NULL)
1966 connman_network_set_group(network, group);
1971 g_free(result.path);
1972 g_free(result.addr);
1973 g_free(result.name);
1974 g_free(result.ssid);
1977 dbus_message_unref(reply);
1979 dbus_pending_call_unref(call);
1981 get_properties(task);
1984 static void get_properties(struct supplicant_task *task)
1986 DBusMessage *message;
1989 path = g_slist_nth_data(task->scan_results, 0);
1991 if (task->hidden_found == TRUE) {
1993 * We're done with regular scanning, let's enable
1994 * the missing network blocks if there are hidden
1997 hidden_block_enable(task);
2002 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
2003 SUPPLICANT_INTF ".BSSID",
2006 task->scan_results = g_slist_remove(task->scan_results, path);
2009 if (message == NULL)
2012 dbus_message_set_auto_start(message, FALSE);
2014 if (dbus_connection_send_with_reply(connection, message,
2015 &task->result_call, TIMEOUT) == FALSE) {
2016 connman_error("Failed to get network properties");
2017 dbus_message_unref(message);
2021 if (task->result_call == NULL) {
2022 connman_error("D-Bus connection not available");
2023 dbus_message_unref(message);
2027 dbus_pending_call_set_notify(task->result_call,
2028 properties_reply, task, NULL);
2030 dbus_message_unref(message);
2035 task->result_call = NULL;
2037 if (task->scanning == TRUE) {
2038 connman_device_set_scanning(task->device, FALSE);
2039 task->scanning = FALSE;
2043 static void scan_results_reply(DBusPendingCall *call, void *user_data)
2045 struct supplicant_task *task = user_data;
2051 DBG("task %p", task);
2053 reply = dbus_pending_call_steal_reply(call);
2055 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
2058 dbus_error_init(&error);
2060 if (dbus_message_get_args(reply, &error,
2061 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
2062 &results, &num_results,
2063 DBUS_TYPE_INVALID) == FALSE) {
2064 if (dbus_error_is_set(&error) == TRUE) {
2065 connman_error("%s", error.message);
2066 dbus_error_free(&error);
2068 connman_error("Wrong arguments for scan result");
2072 if (num_results == 0)
2075 for (i = 0; i < num_results; i++) {
2076 char *path = g_strdup(results[i]);
2080 task->scan_results = g_slist_append(task->scan_results, path);
2083 task->hidden_found = FALSE;
2085 g_strfreev(results);
2087 dbus_message_unref(reply);
2089 dbus_pending_call_unref(call);
2091 get_properties(task);
2096 dbus_message_unref(reply);
2098 dbus_pending_call_unref(call);
2100 task->result_call = NULL;
2102 if (task->scanning == TRUE) {
2103 connman_device_set_scanning(task->device, FALSE);
2104 task->scanning = FALSE;
2108 static void scan_results_available(struct supplicant_task *task)
2110 DBusMessage *message;
2112 DBG("task %p", task);
2114 if (task->result_call != NULL)
2117 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
2118 SUPPLICANT_INTF ".Interface",
2120 if (message == NULL)
2123 dbus_message_set_auto_start(message, FALSE);
2125 if (dbus_connection_send_with_reply(connection, message,
2126 &task->result_call, TIMEOUT) == FALSE) {
2127 connman_error("Failed to request scan result");
2131 if (task->result_call == NULL) {
2132 connman_error("D-Bus connection not available");
2136 if (task->scanning == TRUE)
2137 connman_device_set_scanning(task->device, TRUE);
2139 dbus_pending_call_set_notify(task->result_call,
2140 scan_results_reply, task, NULL);
2143 dbus_message_unref(message);
2146 static enum supplicant_state string2state(const char *state)
2148 if (g_str_equal(state, "INACTIVE") == TRUE)
2149 return WPA_INACTIVE;
2150 else if (g_str_equal(state, "SCANNING") == TRUE)
2151 return WPA_SCANNING;
2152 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
2153 return WPA_ASSOCIATING;
2154 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
2155 return WPA_ASSOCIATED;
2156 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
2157 return WPA_GROUP_HANDSHAKE;
2158 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
2159 return WPA_4WAY_HANDSHAKE;
2160 else if (g_str_equal(state, "COMPLETED") == TRUE)
2161 return WPA_COMPLETED;
2162 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
2163 return WPA_DISCONNECTED;
2168 static int task_connect(struct supplicant_task *task)
2170 const char *address, *security, *passphrase;
2172 unsigned int ssid_len;
2175 g_hash_table_foreach(task->hidden_blocks, block_reset, task);
2177 connman_inet_ifup(task->ifindex);
2179 address = connman_network_get_string(task->network, "Address");
2180 security = connman_network_get_string(task->network, "WiFi.Security");
2181 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
2183 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
2185 DBG("address %s security %s", address, security);
2187 if (security == NULL)
2190 if (passphrase == NULL && g_str_equal(security, "none") == FALSE &&
2191 g_str_equal(security, "ieee8021x") == FALSE)
2194 remove_network(task);
2200 err = set_network(task, ssid, ssid_len, address, security, passphrase);
2204 err = select_network(task);
2208 return -EINPROGRESS;
2211 static void scanning(struct supplicant_task *task, DBusMessage *msg)
2214 dbus_bool_t scanning;
2216 dbus_error_init(&error);
2218 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
2219 DBUS_TYPE_INVALID) == FALSE) {
2220 if (dbus_error_is_set(&error) == TRUE) {
2221 connman_error("%s", error.message);
2222 dbus_error_free(&error);
2224 connman_error("Wrong arguments for scanning");
2228 connman_info("%s scanning %s", task->ifname,
2229 scanning == TRUE ? "started" : "finished");
2231 if (scanning == TRUE)
2232 task->scanning = TRUE;
2235 static void state_change(struct supplicant_task *task, DBusMessage *msg)
2238 const char *newstate, *oldstate;
2239 unsigned char bssid[ETH_ALEN];
2240 unsigned int bssid_len;
2241 enum supplicant_state state, prevstate;
2243 dbus_error_init(&error);
2245 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
2246 DBUS_TYPE_STRING, &oldstate,
2247 DBUS_TYPE_INVALID) == FALSE) {
2248 if (dbus_error_is_set(&error) == TRUE) {
2249 connman_error("%s", error.message);
2250 dbus_error_free(&error);
2252 connman_error("Wrong arguments for state change");
2256 DBG("state %s ==> %s", oldstate, newstate);
2258 connman_info("%s %s%s", task->ifname, newstate,
2259 task->scanning == TRUE ? " (scanning)" : "");
2261 state = string2state(newstate);
2262 if (state == WPA_INVALID)
2265 prevstate = task->state;
2266 task->state = state;
2268 if (task->network == NULL)
2271 switch (task->state) {
2273 switch (prevstate) {
2274 case WPA_ASSOCIATED:
2275 case WPA_GROUP_HANDSHAKE:
2281 /* reset scan trigger and schedule background scan */
2282 connman_device_schedule_scan(task->device);
2284 if (get_bssid(task->device, bssid, &bssid_len) == 0)
2285 connman_network_set_address(task->network,
2289 connman_network_set_connected(task->network, TRUE);
2292 case WPA_ASSOCIATING:
2293 switch (prevstate) {
2297 connman_network_set_associating(task->network, TRUE);
2305 switch (prevstate) {
2307 case WPA_DISCONNECTED:
2314 case WPA_DISCONNECTED:
2316 connman_network_set_connected(task->network, FALSE);
2318 if (task->disconnecting == TRUE) {
2319 connman_network_unref(task->network);
2320 task->disconnecting = FALSE;
2322 if (task->pending_network != NULL) {
2323 task->network = task->pending_network;
2324 task->pending_network = NULL;
2327 task->network = NULL;
2332 connman_network_set_associating(task->network, FALSE);
2339 connman_error("%s invalid state change %s -> %s", task->ifname,
2340 oldstate, newstate);
2343 static gboolean supplicant_filter(DBusConnection *conn,
2344 DBusMessage *msg, void *data)
2346 struct supplicant_task *task;
2347 const char *member, *path;
2349 member = dbus_message_get_member(msg);
2353 path = dbus_message_get_path(msg);
2357 task = find_task_by_path(path);
2361 DBG("task %p member %s", task, member);
2363 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
2364 scan_results_available(task);
2365 else if (g_str_equal(member, "Scanning") == TRUE)
2366 scanning(task, msg);
2367 else if (g_str_equal(member, "StateChange") == TRUE)
2368 state_change(task, msg);
2373 int supplicant_start(struct connman_device *device)
2375 struct supplicant_task *task;
2378 DBG("device %p", device);
2380 task = g_try_new0(struct supplicant_task, 1);
2384 task->ifindex = connman_device_get_index(device);
2385 task->ifname = connman_inet_ifname(task->ifindex);
2387 if (task->ifname == NULL) {
2392 task->cfg80211 = connman_inet_is_cfg80211(task->ifindex);
2393 if (task->cfg80211 == FALSE)
2394 connman_warn("Enabling quirks for unsupported driver");
2396 task->range = g_try_malloc0(sizeof(struct iw_range));
2397 if (task->range == NULL) {
2402 err = get_range(task);
2406 task->device = connman_device_ref(device);
2408 task->created = FALSE;
2409 task->scanning = FALSE;
2410 task->state = WPA_INVALID;
2411 task->disconnecting = FALSE;
2412 task->pending_network = NULL;
2413 task->hidden_blocks = g_hash_table_new_full(g_str_hash, g_str_equal,
2414 NULL, remove_block);
2415 task_list = g_slist_append(task_list, task);
2417 return create_interface(task);
2420 g_free(task->range);
2421 g_free(task->ifname);
2427 int supplicant_stop(struct connman_device *device)
2429 int index = connman_device_get_index(device);
2430 struct supplicant_task *task;
2432 DBG("device %p", device);
2434 task = find_task_by_index(index);
2438 g_free(task->range);
2440 task_list = g_slist_remove(task_list, task);
2441 g_hash_table_destroy(task->hidden_blocks);
2443 if (task->scan_call != NULL) {
2444 dbus_pending_call_cancel(task->scan_call);
2445 task->scan_call = NULL;
2448 if (task->result_call != NULL) {
2449 dbus_pending_call_cancel(task->result_call);
2450 task->result_call = NULL;
2453 if (task->scanning == TRUE)
2454 connman_device_set_scanning(task->device, FALSE);
2456 remove_network(task);
2458 disconnect_network(task);
2460 return remove_interface(task);
2463 int supplicant_scan(struct connman_device *device)
2465 int index = connman_device_get_index(device);
2466 struct supplicant_task *task;
2469 DBG("device %p", device);
2471 task = find_task_by_index(index);
2475 switch (task->state) {
2478 case WPA_ASSOCIATING:
2479 case WPA_ASSOCIATED:
2480 case WPA_4WAY_HANDSHAKE:
2481 case WPA_GROUP_HANDSHAKE:
2487 task->scanning = TRUE;
2489 err = initiate_scan(task);
2491 if (err == -EINPROGRESS)
2494 task->scanning = FALSE;
2498 connman_device_set_scanning(task->device, TRUE);
2503 int supplicant_connect(struct connman_network *network)
2505 struct supplicant_task *task;
2508 DBG("network %p", network);
2510 index = connman_network_get_index(network);
2512 task = find_task_by_index(index);
2516 if (task->disconnecting == TRUE)
2517 task->pending_network = connman_network_ref(network);
2519 task->network = connman_network_ref(network);
2520 return task_connect(task);
2523 return -EINPROGRESS;
2526 int supplicant_disconnect(struct connman_network *network)
2528 struct supplicant_task *task;
2531 DBG("network %p", network);
2533 index = connman_network_get_index(network);
2535 task = find_task_by_index(index);
2539 if (task->disconnecting == TRUE)
2542 if (task->network != network)
2545 remove_network(task);
2547 disconnect_network(task);
2549 task->disconnecting = TRUE;
2554 void supplicant_remove_network(struct connman_network *network)
2556 struct supplicant_task *task;
2559 DBG("network %p", network);
2561 index = connman_network_get_index(network);
2563 task = find_task_by_index(index);
2567 if (task->network != network)
2570 remove_network(task);
2573 static void supplicant_activate(DBusConnection *conn)
2575 DBusMessage *message;
2577 DBG("conn %p", conn);
2579 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
2580 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
2581 if (message == NULL)
2584 dbus_message_set_no_reply(message, TRUE);
2586 dbus_connection_send(conn, message, NULL);
2588 dbus_message_unref(message);
2591 static GSList *driver_list = NULL;
2593 static void supplicant_probe(DBusConnection *conn, void *user_data)
2597 DBG("conn %p", conn);
2599 for (list = driver_list; list; list = list->next) {
2600 struct supplicant_driver *driver = list->data;
2602 DBG("driver %p name %s", driver, driver->name);
2609 static void supplicant_remove(DBusConnection *conn, void *user_data)
2613 DBG("conn %p", conn);
2615 for (list = driver_list; list; list = list->next) {
2616 struct supplicant_driver *driver = list->data;
2618 DBG("driver %p name %s", driver, driver->name);
2626 static guint iface_watch;
2628 static int supplicant_create(void)
2630 if (g_slist_length(driver_list) > 0)
2633 connection = connman_dbus_get_connection();
2634 if (connection == NULL)
2637 DBG("connection %p", connection);
2639 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2640 supplicant_probe, supplicant_remove, NULL, NULL);
2642 iface_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2643 SUPPLICANT_INTF ".Interface",
2644 NULL, supplicant_filter,
2647 if (watch == 0 || iface_watch == 0) {
2648 g_dbus_remove_watch(connection, watch);
2649 g_dbus_remove_watch(connection, iface_watch);
2656 static void supplicant_destroy(void)
2658 if (g_slist_length(driver_list) > 0)
2661 DBG("connection %p", connection);
2663 g_dbus_remove_watch(connection, watch);
2664 g_dbus_remove_watch(connection, iface_watch);
2666 dbus_connection_unref(connection);
2670 int supplicant_register(struct supplicant_driver *driver)
2674 DBG("driver %p name %s", driver, driver->name);
2676 err = supplicant_create();
2680 driver_list = g_slist_append(driver_list, driver);
2682 supplicant_activate(connection);
2687 void supplicant_unregister(struct supplicant_driver *driver)
2689 DBG("driver %p name %s", driver, driver->name);
2691 supplicant_remove(connection, NULL);
2693 driver_list = g_slist_remove(driver_list, driver);
2695 supplicant_destroy();