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 GHashTable *hidden_blocks;
196 enum supplicant_state state;
198 GSList *scan_results;
199 DBusPendingCall *scan_call;
200 DBusPendingCall *result_call;
201 struct iw_range *range;
202 gboolean disconnecting;
205 static GSList *task_list = NULL;
207 static DBusConnection *connection;
209 static void free_task(struct supplicant_task *task)
211 DBG("task %p", task);
213 g_free(task->ifname);
218 static void remove_block(gpointer user_data)
220 struct supplicant_block *block = user_data;
225 g_free(block->netpath);
228 static struct supplicant_task *find_task_by_index(int index)
232 for (list = task_list; list; list = list->next) {
233 struct supplicant_task *task = list->data;
235 if (task->ifindex == index)
242 static struct supplicant_task *find_task_by_path(const char *path)
246 for (list = task_list; list; list = list->next) {
247 struct supplicant_task *task = list->data;
249 if (g_strcmp0(task->path, path) == 0)
256 static int get_range(struct supplicant_task *task)
261 fd = socket(PF_INET, SOCK_DGRAM, 0);
265 memset(&wrq, 0, sizeof(struct iwreq));
266 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
267 wrq.u.data.pointer = task->range;
268 wrq.u.data.length = sizeof(struct iw_range);
270 err = ioctl(fd, SIOCGIWRANGE, &wrq);
275 task->range->max_qual.updated |= IW_QUAL_ALL_INVALID;
277 connman_info("%s {scan} capabilities 0x%02x", task->ifname,
278 task->range->scan_capa);
280 connman_info("%s {quality} flags 0x%02x", task->ifname,
281 task->range->max_qual.updated);
286 static int get_bssid(struct connman_device *device,
287 unsigned char *bssid, unsigned int *bssid_len)
294 ifindex = connman_device_get_index(device);
298 ifname = connman_inet_ifname(ifindex);
302 fd = socket(PF_INET, SOCK_DGRAM, 0);
308 memset(&wrq, 0, sizeof(wrq));
309 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
311 err = ioctl(fd, SIOCGIWAP, &wrq);
319 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
320 *bssid_len = ETH_ALEN;
325 static int enable_network(struct supplicant_task *task, const char *netpath,
326 connman_bool_t enable)
328 DBusMessage *message, *reply;
332 DBG("enable %d", enable);
334 enable_string = enable ? "enable" : "disable";
336 message = dbus_message_new_method_call(SUPPLICANT_NAME, netpath,
337 SUPPLICANT_INTF ".Network", enable_string);
341 dbus_message_set_auto_start(message, FALSE);
343 dbus_error_init(&error);
345 reply = dbus_connection_send_with_reply_and_block(connection,
346 message, -1, &error);
348 if (dbus_error_is_set(&error) == TRUE) {
349 connman_error("%s", error.message);
350 dbus_error_free(&error);
352 connman_error("Failed to select network");
353 dbus_message_unref(message);
357 dbus_message_unref(reply);
359 dbus_message_unref(message);
364 static int set_hidden_network(struct supplicant_task *task, const char *netpath,
365 const unsigned char *ssid, int ssid_len)
367 DBusMessage *message, *reply;
368 DBusMessageIter array, dict;
370 dbus_uint32_t scan_ssid = 1;
372 message = dbus_message_new_method_call(SUPPLICANT_NAME, netpath,
373 SUPPLICANT_INTF ".Network", "set");
377 dbus_message_set_auto_start(message, FALSE);
379 dbus_message_iter_init_append(message, &array);
381 connman_dbus_dict_open(&array, &dict);
383 connman_dbus_dict_append_basic(&dict, "scan_ssid",
384 DBUS_TYPE_UINT32, &scan_ssid);
386 connman_dbus_dict_append_fixed_array(&dict, "ssid",
387 DBUS_TYPE_BYTE, &ssid, ssid_len);
389 connman_dbus_dict_close(&array, &dict);
391 dbus_error_init(&error);
393 reply = dbus_connection_send_with_reply_and_block(connection,
394 message, -1, &error);
396 if (dbus_error_is_set(&error) == TRUE) {
397 connman_error("%s", error.message);
398 dbus_error_free(&error);
400 connman_error("Failed to set network options");
401 dbus_message_unref(message);
405 dbus_message_unref(reply);
407 dbus_message_unref(message);
412 static void block_reset(gpointer key, gpointer value, gpointer user_data)
414 struct supplicant_block *block = value;
415 struct supplicant_task *task = user_data;
417 block->num_scans = 0;
419 enable_network(task, block->netpath, FALSE);
421 block->enabled = FALSE;
424 #define MAX_BLOCK_SCANS 2
425 static void hidden_block_enable(struct supplicant_task *task)
429 struct supplicant_block *block;
431 DBG("network %p", task->network);
433 if (g_hash_table_size(task->hidden_blocks) == 0)
437 * If we're associated or associating, we no longer need to
438 * look for hidden networks.
444 * We go through the block list and:
445 * - If we scanned it more than twice, we disable it and move
446 * on to the next block.
447 * - If the next block is not enabled, we enable it, start
448 * the scan counter, and return. This routine will be called
449 * again when the next scan results are available.
450 * - If we're done with all the blocks there, we just reset them.
452 g_hash_table_iter_init(&iter, task->hidden_blocks);
453 while (g_hash_table_iter_next(&iter, &key, &value)) {
456 DBG("%s num of scans %d enabled %d",
457 block->ssid, block->num_scans, block->enabled);
459 if (block->num_scans > MAX_BLOCK_SCANS) {
460 if (block->enabled == FALSE)
463 enable_network(task, block->netpath, FALSE);
464 block->enabled = FALSE;
468 if (block->enabled == FALSE) {
469 enable_network(task, block->netpath, TRUE);
470 block->enabled = TRUE;
478 g_hash_table_foreach(task->hidden_blocks, block_reset, task);
481 static int add_hidden_network(struct supplicant_task *task,
482 const unsigned char *ssid, int ssid_len)
484 DBusMessage *message, *reply;
487 struct supplicant_block *block;
488 char *netpath = NULL;
491 DBG("task %p", task);
493 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
494 SUPPLICANT_INTF ".Interface", "addNetwork");
498 dbus_message_set_auto_start(message, FALSE);
500 dbus_error_init(&error);
502 reply = dbus_connection_send_with_reply_and_block(connection,
503 message, -1, &error);
505 if (dbus_error_is_set(&error) == TRUE) {
506 connman_error("%s", error.message);
507 dbus_error_free(&error);
509 connman_error("Failed to add network");
510 dbus_message_unref(message);
514 dbus_error_init(&error);
516 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
517 DBUS_TYPE_INVALID) == FALSE) {
518 if (dbus_error_is_set(&error) == TRUE) {
519 connman_error("%s", error.message);
520 dbus_error_free(&error);
522 connman_error("Wrong arguments for network");
523 dbus_message_unref(reply);
527 netpath = g_strdup(path);
529 ret = set_hidden_network(task, netpath, ssid, ssid_len);
533 block = g_try_new0(struct supplicant_block, 1);
537 block->ssid = g_try_malloc0(ssid_len + 1);
538 if (block->ssid == NULL) {
543 for (i = 0; i < ssid_len; i++) {
544 if (g_ascii_isprint(ssid[i]))
545 block->ssid[i] = ssid[i];
547 block->ssid[i] = ' ';
550 block->netpath = netpath;
551 block->enabled = FALSE;
552 block->num_scans = 0;
554 DBG("path %s ssid %s", block->netpath, block->ssid);
556 g_hash_table_replace(task->hidden_blocks, block->ssid, block);
562 dbus_message_unref(reply);
564 dbus_message_unref(message);
569 static void add_interface_reply(DBusPendingCall *call, void *user_data)
571 struct supplicant_task *task = user_data;
575 char **hex_ssids, *hex_ssid;
578 DBG("task %p", task);
580 reply = dbus_pending_call_steal_reply(call);
582 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
585 dbus_error_init(&error);
587 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
588 DBUS_TYPE_INVALID) == FALSE) {
589 if (dbus_error_is_set(&error) == TRUE) {
590 connman_error("%s", error.message);
591 dbus_error_free(&error);
593 connman_error("Wrong arguments for add interface");
597 DBG("path %s", path);
599 task->path = g_strdup(path);
600 task->created = TRUE;
602 connman_device_set_powered(task->device, TRUE);
604 hex_ssids = connman_wifi_load_ssid();
606 for (i = 0; hex_ssids[i]; i++) {
608 unsigned int j, k = 0, hex;
611 hex_ssid = hex_ssids[i];
612 hex_ssid_len = strlen(hex_ssid);
614 ssid = g_try_malloc0(hex_ssid_len / 2 + 1);
618 for (j = 0, k = 0; j < hex_ssid_len; j += 2) {
619 sscanf(hex_ssid + j, "%02x", &hex);
623 if (add_hidden_network(task, ssid, hex_ssid_len / 2) < 0)
627 g_strfreev(hex_ssids);
629 dbus_message_unref(reply);
631 dbus_pending_call_unref(call);
636 dbus_message_unref(reply);
638 dbus_pending_call_unref(call);
640 task_list = g_slist_remove(task_list, task);
642 connman_device_unref(task->device);
647 static int add_interface(struct supplicant_task *task)
649 const char *driver = connman_option_get_string("wifi");
650 DBusMessage *message;
651 DBusMessageIter array, dict;
652 DBusPendingCall *call;
654 DBG("task %p", task);
656 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
657 SUPPLICANT_INTF, "addInterface");
661 dbus_message_set_auto_start(message, FALSE);
663 dbus_message_iter_init_append(message, &array);
665 dbus_message_iter_append_basic(&array,
666 DBUS_TYPE_STRING, &task->ifname);
668 connman_dbus_dict_open(&array, &dict);
670 connman_dbus_dict_append_basic(&dict, "driver",
671 DBUS_TYPE_STRING, &driver);
673 connman_dbus_dict_close(&array, &dict);
675 if (dbus_connection_send_with_reply(connection, message,
676 &call, TIMEOUT) == FALSE) {
677 connman_error("Failed to add interface");
678 dbus_message_unref(message);
683 connman_error("D-Bus connection not available");
684 dbus_message_unref(message);
688 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
690 dbus_message_unref(message);
695 static void get_interface_reply(DBusPendingCall *call, void *user_data)
697 struct supplicant_task *task = user_data;
702 DBG("task %p", task);
704 reply = dbus_pending_call_steal_reply(call);
706 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
711 dbus_error_init(&error);
713 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
714 DBUS_TYPE_INVALID) == FALSE) {
715 if (dbus_error_is_set(&error) == TRUE) {
716 connman_error("%s", error.message);
717 dbus_error_free(&error);
719 connman_error("Wrong arguments for get interface");
723 DBG("path %s", path);
725 task->path = g_strdup(path);
726 task->created = FALSE;
728 connman_device_set_powered(task->device, TRUE);
731 dbus_message_unref(reply);
733 dbus_pending_call_unref(call);
736 static int create_interface(struct supplicant_task *task)
738 DBusMessage *message;
739 DBusPendingCall *call;
741 DBG("task %p", task);
743 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
744 SUPPLICANT_INTF, "getInterface");
748 dbus_message_set_auto_start(message, FALSE);
750 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
753 if (dbus_connection_send_with_reply(connection, message,
754 &call, TIMEOUT) == FALSE) {
755 connman_error("Failed to get interface");
756 dbus_message_unref(message);
761 connman_error("D-Bus connection not available");
762 dbus_message_unref(message);
766 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
768 dbus_message_unref(message);
773 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
775 struct supplicant_task *task = user_data;
778 DBG("task %p", task);
780 reply = dbus_pending_call_steal_reply(call);
782 connman_device_set_powered(task->device, FALSE);
784 connman_device_unref(task->device);
786 connman_inet_ifdown(task->ifindex);
790 dbus_message_unref(reply);
792 dbus_pending_call_unref(call);
795 static int remove_interface(struct supplicant_task *task)
797 DBusMessage *message;
798 DBusPendingCall *call;
800 DBG("task %p", task);
802 if (task->path == NULL)
806 if (task->created == FALSE) {
807 connman_device_set_powered(task->device, FALSE);
812 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
813 SUPPLICANT_INTF, "removeInterface");
817 dbus_message_set_auto_start(message, FALSE);
819 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
822 if (dbus_connection_send_with_reply(connection, message,
823 &call, TIMEOUT) == FALSE) {
824 connman_error("Failed to remove interface");
825 dbus_message_unref(message);
830 connman_error("D-Bus connection not available");
831 dbus_message_unref(message);
835 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
837 dbus_message_unref(message);
842 static int set_ap_scan(struct supplicant_task *task)
844 DBusMessage *message, *reply;
848 DBG("task %p", task);
850 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
851 SUPPLICANT_INTF ".Interface", "setAPScan");
855 dbus_message_set_auto_start(message, FALSE);
857 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
860 dbus_error_init(&error);
862 reply = dbus_connection_send_with_reply_and_block(connection,
863 message, -1, &error);
865 if (dbus_error_is_set(&error) == TRUE) {
866 connman_error("%s", error.message);
867 dbus_error_free(&error);
869 connman_error("Failed to set AP scan");
870 dbus_message_unref(message);
874 dbus_message_unref(message);
876 dbus_message_unref(reply);
881 static int add_network(struct supplicant_task *task)
883 DBusMessage *message, *reply;
887 DBG("task %p", task);
889 if (task->netpath != NULL)
892 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
893 SUPPLICANT_INTF ".Interface", "addNetwork");
897 dbus_message_set_auto_start(message, FALSE);
899 dbus_error_init(&error);
901 reply = dbus_connection_send_with_reply_and_block(connection,
902 message, -1, &error);
904 if (dbus_error_is_set(&error) == TRUE) {
905 connman_error("%s", error.message);
906 dbus_error_free(&error);
908 connman_error("Failed to add network");
909 dbus_message_unref(message);
913 dbus_message_unref(message);
915 dbus_error_init(&error);
917 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
918 DBUS_TYPE_INVALID) == FALSE) {
919 if (dbus_error_is_set(&error) == TRUE) {
920 connman_error("%s", error.message);
921 dbus_error_free(&error);
923 connman_error("Wrong arguments for network");
924 dbus_message_unref(reply);
928 DBG("path %s", path);
930 task->netpath = g_strdup(path);
932 dbus_message_unref(reply);
937 static int remove_network(struct supplicant_task *task)
939 DBusMessage *message, *reply;
942 DBG("task %p", task);
944 if (task->netpath == NULL || task->path == NULL)
947 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
948 SUPPLICANT_INTF ".Interface", "removeNetwork");
952 dbus_message_set_auto_start(message, FALSE);
954 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
957 dbus_error_init(&error);
959 reply = dbus_connection_send_with_reply_and_block(connection,
960 message, -1, &error);
962 if (dbus_error_is_set(&error) == TRUE) {
963 connman_error("%s", error.message);
964 dbus_error_free(&error);
966 connman_error("Failed to remove network");
967 dbus_message_unref(message);
971 dbus_message_unref(message);
973 dbus_message_unref(reply);
975 g_free(task->netpath);
976 task->netpath = NULL;
981 static int select_network(struct supplicant_task *task)
983 DBusMessage *message, *reply;
986 DBG("task %p", task);
988 if (task->netpath == NULL)
991 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
992 SUPPLICANT_INTF ".Interface", "selectNetwork");
996 dbus_message_set_auto_start(message, FALSE);
998 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
1001 dbus_error_init(&error);
1003 reply = dbus_connection_send_with_reply_and_block(connection,
1004 message, -1, &error);
1005 if (reply == NULL) {
1006 if (dbus_error_is_set(&error) == TRUE) {
1007 connman_error("%s", error.message);
1008 dbus_error_free(&error);
1010 connman_error("Failed to select network");
1011 dbus_message_unref(message);
1015 dbus_message_unref(message);
1017 dbus_message_unref(reply);
1022 static int disconnect_network(struct supplicant_task *task)
1024 DBusMessage *message, *reply;
1027 DBG("task %p", task);
1029 if (task->path == NULL)
1032 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1033 SUPPLICANT_INTF ".Interface", "disconnect");
1034 if (message == NULL)
1037 dbus_message_set_auto_start(message, FALSE);
1039 dbus_error_init(&error);
1041 reply = dbus_connection_send_with_reply_and_block(connection,
1042 message, -1, &error);
1043 if (reply == NULL) {
1044 if (dbus_error_is_set(&error) == TRUE) {
1045 connman_error("%s", error.message);
1046 dbus_error_free(&error);
1048 connman_error("Failed to disconnect network");
1049 dbus_message_unref(message);
1053 dbus_message_unref(message);
1055 dbus_message_unref(reply);
1060 static int set_network_tls(struct connman_network *network,
1061 DBusMessageIter *dict)
1063 const char *private_key, *client_cert, *ca_cert;
1064 const char *private_key_password;
1067 * For TLS, we at least need a key, the client cert,
1069 * Server cert is optional.
1071 client_cert = connman_network_get_string(network,
1072 "WiFi.ClientCertFile");
1073 if (client_cert == NULL)
1076 private_key = connman_network_get_string(network,
1077 "WiFi.PrivateKeyFile");
1078 if (private_key == NULL)
1081 private_key_password = connman_network_get_string(network,
1082 "WiFi.PrivateKeyPassphrase");
1083 if (private_key_password == NULL)
1086 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
1088 connman_dbus_dict_append_basic(dict, "ca_cert",
1089 DBUS_TYPE_STRING, &ca_cert);
1091 DBG("client cert %s private key %s", client_cert, private_key);
1093 connman_dbus_dict_append_basic(dict, "private_key",
1094 DBUS_TYPE_STRING, &private_key);
1095 connman_dbus_dict_append_basic(dict, "private_key_passwd",
1097 &private_key_password);
1098 connman_dbus_dict_append_basic(dict, "client_cert",
1099 DBUS_TYPE_STRING, &client_cert);
1104 static int set_network_peap(struct connman_network *network,
1105 DBusMessageIter *dict, const char *passphrase)
1107 const char *client_cert, *ca_cert, *phase2;
1111 * For PEAP, we at least need the sever cert, a 2nd
1112 * phase authentication and a passhprase.
1113 * Client cert is optional although strongly required
1114 * When setting the client cert, we then need a private
1117 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
1118 if (ca_cert == NULL)
1121 phase2 = connman_network_get_string(network, "WiFi.Phase2");
1125 DBG("CA cert %s phase2 auth %s", ca_cert, phase2);
1127 client_cert = connman_network_get_string(network,
1128 "WiFi.ClientCertFile");
1130 const char *private_key, *private_key_password;
1132 private_key = connman_network_get_string(network,
1133 "WiFi.PrivateKeyFile");
1134 if (private_key == NULL)
1137 private_key_password =
1138 connman_network_get_string(network,
1139 "WiFi.PrivateKeyPassphrase");
1140 if (private_key_password == NULL)
1143 connman_dbus_dict_append_basic(dict, "client_cert",
1144 DBUS_TYPE_STRING, &client_cert);
1146 connman_dbus_dict_append_basic(dict, "private_key",
1147 DBUS_TYPE_STRING, &private_key);
1149 connman_dbus_dict_append_basic(dict, "private_key_passwd",
1151 &private_key_password);
1153 DBG("client cert %s private key %s", client_cert, private_key);
1156 phase2_auth = g_strdup_printf("\"auth=%s\"", phase2);
1158 connman_dbus_dict_append_basic(dict, "password",
1159 DBUS_TYPE_STRING, &passphrase);
1161 connman_dbus_dict_append_basic(dict, "ca_cert",
1162 DBUS_TYPE_STRING, &ca_cert);
1164 connman_dbus_dict_append_basic(dict, "phase2",
1165 DBUS_TYPE_STRING, &phase2_auth);
1167 g_free(phase2_auth);
1172 static int set_network(struct supplicant_task *task,
1173 const unsigned char *network, int len,
1174 const char *address, const char *security,
1175 const char *passphrase)
1177 DBusMessage *message, *reply;
1178 DBusMessageIter array, dict;
1180 dbus_uint32_t scan_ssid = 1;
1182 DBG("task %p", task);
1184 if (task->netpath == NULL)
1187 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
1188 SUPPLICANT_INTF ".Network", "set");
1189 if (message == NULL)
1192 dbus_message_set_auto_start(message, FALSE);
1194 dbus_message_iter_init_append(message, &array);
1196 connman_dbus_dict_open(&array, &dict);
1198 connman_dbus_dict_append_basic(&dict, "scan_ssid",
1199 DBUS_TYPE_UINT32, &scan_ssid);
1202 connman_dbus_dict_append_fixed_array(&dict, "ssid",
1203 DBUS_TYPE_BYTE, &network, len);
1205 connman_dbus_dict_append_basic(&dict, "bssid",
1206 DBUS_TYPE_STRING, &address);
1208 if (g_ascii_strcasecmp(security, "psk") == 0 ||
1209 g_ascii_strcasecmp(security, "wpa") == 0 ||
1210 g_ascii_strcasecmp(security, "rsn") == 0) {
1211 const char *key_mgmt = "WPA-PSK";
1212 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1213 DBUS_TYPE_STRING, &key_mgmt);
1215 if (passphrase && strlen(passphrase) > 0)
1216 connman_dbus_dict_append_basic(&dict, "psk",
1217 DBUS_TYPE_STRING, &passphrase);
1218 } else if (g_ascii_strcasecmp(security, "ieee8021x") == 0) {
1219 struct connman_network *network = task->network;
1220 const char *key_mgmt = "WPA-EAP", *eap, *identity;
1224 * If our private key password is unset,
1225 * we use the supplied passphrase. That is needed
1226 * for PEAP where 2 passphrases (identity and client
1227 * cert may have to be provided.
1229 if (connman_network_get_string(network,
1230 "WiFi.PrivateKeyPassphrase") == NULL)
1231 connman_network_set_string(network,
1232 "WiFi.PrivateKeyPassphrase",
1235 eap = connman_network_get_string(network, "WiFi.EAP");
1239 /* We must have an identity for both PEAP and TLS */
1240 identity = connman_network_get_string(network, "WiFi.Identity");
1241 if (identity == NULL)
1244 DBG("key_mgmt %s eap %s identity %s", key_mgmt, eap, identity);
1246 if (g_strcmp0(eap, "tls") == 0) {
1249 err = set_network_tls(network, &dict);
1251 dbus_message_unref(message);
1254 } else if (g_strcmp0(eap, "peap") == 0) {
1257 err = set_network_peap(network, &dict, passphrase);
1259 dbus_message_unref(message);
1263 connman_error("Unknown EAP %s", eap);
1267 /* wpa_supplicant only accepts upper case EAPs */
1268 eap_value = g_ascii_strup(eap, -1);
1270 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1273 connman_dbus_dict_append_basic(&dict, "eap",
1276 connman_dbus_dict_append_basic(&dict, "identity",
1282 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
1283 const char *key_mgmt = "NONE";
1284 const char *auth_alg = "OPEN";
1285 const char *key_index = "0";
1287 if (task->cfg80211 == TRUE)
1288 auth_alg = "OPEN SHARED";
1290 connman_dbus_dict_append_basic(&dict, "auth_alg",
1291 DBUS_TYPE_STRING, &auth_alg);
1293 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1294 DBUS_TYPE_STRING, &key_mgmt);
1297 int size = strlen(passphrase);
1298 if (size == 10 || size == 26) {
1299 unsigned char *key = malloc(13);
1302 memset(tmp, 0, sizeof(tmp));
1305 for (i = 0; i < size / 2; i++) {
1306 memcpy(tmp, passphrase + (i * 2), 2);
1307 key[i] = (unsigned char) strtol(tmp,
1310 connman_dbus_dict_append_fixed_array(&dict,
1311 "wep_key0", DBUS_TYPE_BYTE,
1314 } else if (size == 5 || size == 13) {
1315 unsigned char *key = malloc(13);
1319 for (i = 0; i < size; i++)
1320 key[i] = (unsigned char) passphrase[i];
1321 connman_dbus_dict_append_fixed_array(&dict,
1322 "wep_key0", DBUS_TYPE_BYTE,
1326 connman_dbus_dict_append_basic(&dict,
1327 "wep_key0", DBUS_TYPE_STRING,
1330 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
1331 DBUS_TYPE_STRING, &key_index);
1334 const char *key_mgmt = "NONE";
1335 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1336 DBUS_TYPE_STRING, &key_mgmt);
1339 connman_dbus_dict_close(&array, &dict);
1341 dbus_error_init(&error);
1343 reply = dbus_connection_send_with_reply_and_block(connection,
1344 message, -1, &error);
1345 if (reply == NULL) {
1346 if (dbus_error_is_set(&error) == TRUE) {
1347 connman_error("%s", error.message);
1348 dbus_error_free(&error);
1350 connman_error("Failed to set network options");
1351 dbus_message_unref(message);
1355 dbus_message_unref(message);
1357 dbus_message_unref(reply);
1362 dbus_message_unref(message);
1366 static void scan_reply(DBusPendingCall *call, void *user_data)
1368 struct supplicant_task *task = user_data;
1371 DBG("task %p", task);
1373 task->scan_call = NULL;
1375 reply = dbus_pending_call_steal_reply(call);
1377 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1378 connman_device_set_scanning(task->device, FALSE);
1382 if (task->scanning == TRUE)
1383 connman_device_set_scanning(task->device, TRUE);
1386 dbus_message_unref(reply);
1388 dbus_pending_call_unref(call);
1392 static int initiate_scan(struct supplicant_task *task)
1394 DBusMessage *message;
1396 DBG("task %p", task);
1398 if (task->path == NULL)
1401 if (task->scan_call != NULL)
1404 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1405 SUPPLICANT_INTF ".Interface", "scan");
1406 if (message == NULL)
1409 dbus_message_set_auto_start(message, FALSE);
1411 if (dbus_connection_send_with_reply(connection, message,
1412 &task->scan_call, TIMEOUT) == FALSE) {
1413 connman_error("Failed to initiate scan");
1414 dbus_message_unref(message);
1418 if (task->scan_call == NULL) {
1419 connman_error("D-Bus connection not available");
1420 dbus_message_unref(message);
1424 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
1426 dbus_message_unref(message);
1428 return -EINPROGRESS;
1434 } special_ssid[] = {
1435 { "<hidden>", "hidden" },
1436 { "default", "linksys" },
1444 { "comcomcom", "3com" },
1446 { "Symbol", "symbol" },
1447 { "Motorola", "motorola" },
1448 { "Wireless" , "wireless" },
1453 static char *build_group(const char *addr, const char *name,
1454 const unsigned char *ssid, unsigned int ssid_len,
1455 const char *mode, const char *security)
1463 str = g_string_sized_new((ssid_len * 2) + 24);
1468 g_string_append_printf(str, "hidden_%s", addr);
1472 for (i = 0; special_ssid[i].name; i++) {
1473 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1474 if (special_ssid[i].value == NULL)
1475 g_string_append_printf(str, "%s_%s",
1478 g_string_append_printf(str, "%s_%s",
1479 special_ssid[i].value, addr);
1484 if (ssid_len > 0 && ssid[0] != '\0') {
1485 for (i = 0; i < ssid_len; i++)
1486 g_string_append_printf(str, "%02x", ssid[i]);
1488 g_string_append_printf(str, "hidden_%s", addr);
1491 g_string_append_printf(str, "_%s_%s", mode, security);
1493 return g_string_free(str, FALSE);
1496 static void extract_addr(DBusMessageIter *value,
1497 struct supplicant_result *result)
1499 DBusMessageIter array;
1500 struct ether_addr eth;
1501 unsigned char *addr;
1504 dbus_message_iter_recurse(value, &array);
1505 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1510 result->addr = g_try_malloc(addr_len);
1511 if (result->addr == NULL)
1514 memcpy(result->addr, addr, addr_len);
1515 result->addr_len = addr_len;
1517 result->path = g_try_malloc0(13);
1518 if (result->path == NULL)
1521 memcpy(ð, addr, sizeof(eth));
1522 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1523 eth.ether_addr_octet[0],
1524 eth.ether_addr_octet[1],
1525 eth.ether_addr_octet[2],
1526 eth.ether_addr_octet[3],
1527 eth.ether_addr_octet[4],
1528 eth.ether_addr_octet[5]);
1531 static void extract_ssid(DBusMessageIter *value,
1532 struct supplicant_result *result)
1534 DBusMessageIter array;
1535 unsigned char *ssid;
1538 dbus_message_iter_recurse(value, &array);
1539 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1544 if (ssid[0] == '\0')
1547 result->ssid = g_try_malloc(ssid_len);
1548 if (result->ssid == NULL)
1551 memcpy(result->ssid, ssid, ssid_len);
1552 result->ssid_len = ssid_len;
1554 result->name = g_try_malloc0(ssid_len + 1);
1555 if (result->name == NULL)
1558 for (i = 0; i < ssid_len; i++) {
1559 if (g_ascii_isprint(ssid[i]))
1560 result->name[i] = ssid[i];
1562 result->name[i] = ' ';
1566 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1567 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1569 static void extract_rsn(struct supplicant_result *result,
1570 const unsigned char *buf, int len)
1589 /* Pairwise cipher */
1593 count = buf[0] | (buf[1] << 8);
1594 if (2 + (count * 4) > len)
1597 buf += 2 + (count * 4);
1598 len -= 2 + (count * 4);
1600 /* Authentication */
1604 count = buf[0] | (buf[1] << 8);
1605 if (2 + (count * 4) > len)
1608 for (i = 0; i < count; i++) {
1609 const unsigned char *ptr = buf + 2 + (i * 4);
1611 if (memcmp(ptr, wifi_oui, 3) == 0) {
1614 result->has_8021x = TRUE;
1617 result->has_psk = TRUE;
1620 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1623 result->has_8021x = TRUE;
1626 result->has_psk = TRUE;
1632 buf += 2 + (count * 4);
1633 len -= 2 + (count * 4);
1636 static void extract_wpaie(DBusMessageIter *value,
1637 struct supplicant_result *result)
1639 DBusMessageIter array;
1643 dbus_message_iter_recurse(value, &array);
1644 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1647 result->has_wpa = TRUE;
1648 extract_rsn(result, ie + 6, ie_len - 6);
1652 static void extract_rsnie(DBusMessageIter *value,
1653 struct supplicant_result *result)
1655 DBusMessageIter array;
1659 dbus_message_iter_recurse(value, &array);
1660 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1663 result->has_rsn = TRUE;
1664 extract_rsn(result, ie + 2, ie_len - 2);
1668 static void extract_wpsie(DBusMessageIter *value,
1669 struct supplicant_result *result)
1671 DBusMessageIter array;
1675 dbus_message_iter_recurse(value, &array);
1676 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1679 result->has_wps = TRUE;
1682 static void extract_capabilites(DBusMessageIter *value,
1683 struct supplicant_result *result)
1685 dbus_message_iter_get_basic(value, &result->capabilities);
1687 if (result->capabilities & IEEE80211_CAP_ESS)
1688 result->adhoc = FALSE;
1689 else if (result->capabilities & IEEE80211_CAP_IBSS)
1690 result->adhoc = TRUE;
1692 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1693 result->has_wep = TRUE;
1696 static unsigned char calculate_strength(struct supplicant_task *task,
1697 struct supplicant_result *result)
1699 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1700 unsigned char strength;
1702 if (result->level > 0)
1703 strength = 100 - result->level;
1705 strength = 120 + result->level;
1713 return (result->quality * 100) / task->range->max_qual.qual;
1716 static unsigned short calculate_channel(struct supplicant_result *result)
1718 if (result->frequency < 0)
1721 return (result->frequency - 2407) / 5;
1724 static void get_properties(struct supplicant_task *task);
1726 static void properties_reply(DBusPendingCall *call, void *user_data)
1728 struct supplicant_task *task = user_data;
1729 struct supplicant_result result;
1730 struct supplicant_block *block;
1731 struct connman_network *network;
1733 DBusMessageIter array, dict;
1734 unsigned char strength;
1735 unsigned short channel, frequency;
1736 const char *mode, *security;
1739 DBG("task %p", task);
1741 reply = dbus_pending_call_steal_reply(call);
1743 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1746 memset(&result, 0, sizeof(result));
1747 result.frequency = -1;
1748 result.quality = -1;
1752 dbus_message_iter_init(reply, &array);
1754 dbus_message_iter_recurse(&array, &dict);
1756 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1757 DBusMessageIter entry, value;
1760 dbus_message_iter_recurse(&dict, &entry);
1761 dbus_message_iter_get_basic(&entry, &key);
1763 dbus_message_iter_next(&entry);
1765 dbus_message_iter_recurse(&entry, &value);
1767 //type = dbus_message_iter_get_arg_type(&value);
1768 //dbus_message_iter_get_basic(&value, &val);
1776 * frequency : i (105)
1777 * capabilities : q (113)
1784 if (g_str_equal(key, "bssid") == TRUE)
1785 extract_addr(&value, &result);
1786 else if (g_str_equal(key, "ssid") == TRUE)
1787 extract_ssid(&value, &result);
1788 else if (g_str_equal(key, "wpaie") == TRUE)
1789 extract_wpaie(&value, &result);
1790 else if (g_str_equal(key, "rsnie") == TRUE)
1791 extract_rsnie(&value, &result);
1792 else if (g_str_equal(key, "wpsie") == TRUE)
1793 extract_wpsie(&value, &result);
1794 else if (g_str_equal(key, "capabilities") == TRUE)
1795 extract_capabilites(&value, &result);
1796 else if (g_str_equal(key, "frequency") == TRUE)
1797 dbus_message_iter_get_basic(&value, &result.frequency);
1798 else if (g_str_equal(key, "quality") == TRUE)
1799 dbus_message_iter_get_basic(&value, &result.quality);
1800 else if (g_str_equal(key, "noise") == TRUE)
1801 dbus_message_iter_get_basic(&value, &result.noise);
1802 else if (g_str_equal(key, "level") == TRUE)
1803 dbus_message_iter_get_basic(&value, &result.level);
1804 else if (g_str_equal(key, "maxrate") == TRUE)
1805 dbus_message_iter_get_basic(&value, &result.maxrate);
1807 dbus_message_iter_next(&dict);
1810 DBG("capabilties %u frequency %d "
1811 "quality %d noise %d level %d maxrate %d",
1812 result.capabilities, result.frequency,
1813 result.quality, result.noise,
1814 result.level, result.maxrate);
1816 if (result.path == NULL)
1819 if (result.path[0] == '\0')
1823 block = g_hash_table_lookup(task->hidden_blocks, result.name);
1825 enable_network(task, block->netpath, FALSE);
1826 g_hash_table_remove(task->hidden_blocks, block->ssid);
1830 if (result.frequency > 0 && result.frequency < 14)
1831 result.frequency = 2407 + (5 * result.frequency);
1832 else if (result.frequency == 14)
1833 result.frequency = 2484;
1835 strength = calculate_strength(task, &result);
1836 channel = calculate_channel(&result);
1838 frequency = (result.frequency < 0) ? 0 : result.frequency;
1840 if (result.has_8021x == TRUE)
1841 security = "ieee8021x";
1842 else if (result.has_psk == TRUE)
1844 else if (result.has_wep == TRUE)
1849 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1851 group = build_group(result.path, result.name,
1852 result.ssid, result.ssid_len,
1855 if (result.has_psk == TRUE) {
1856 if (result.has_rsn == TRUE)
1858 else if (result.has_wpa == TRUE)
1862 network = connman_device_get_network(task->device, result.path);
1863 if (network == NULL) {
1866 network = connman_network_create(result.path,
1867 CONNMAN_NETWORK_TYPE_WIFI);
1868 if (network == NULL)
1871 index = connman_device_get_index(task->device);
1872 connman_network_set_index(network, index);
1874 connman_network_set_protocol(network,
1875 CONNMAN_NETWORK_PROTOCOL_IP);
1877 connman_network_set_address(network, result.addr,
1880 if (connman_device_add_network(task->device, network) < 0) {
1881 connman_network_unref(network);
1886 if (result.name != NULL && result.name[0] != '\0')
1887 connman_network_set_name(network, result.name);
1889 if (result.ssid_len != 0)
1890 connman_network_set_blob(network, "WiFi.SSID",
1891 result.ssid, result.ssid_len);
1893 connman_network_set_string(network, "WiFi.Mode", mode);
1895 DBG("%s (%s %s) strength %d (%s)",
1896 result.name, mode, security, strength,
1897 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1899 connman_network_set_available(network, TRUE);
1900 connman_network_set_strength(network, strength);
1902 connman_network_set_uint16(network, "Frequency", frequency);
1903 connman_network_set_uint16(network, "WiFi.Channel", channel);
1904 connman_network_set_string(network, "WiFi.Security", security);
1906 if (result.ssid != NULL)
1907 connman_network_set_group(network, group);
1912 g_free(result.path);
1913 g_free(result.addr);
1914 g_free(result.name);
1915 g_free(result.ssid);
1918 dbus_message_unref(reply);
1920 dbus_pending_call_unref(call);
1922 get_properties(task);
1925 static void get_properties(struct supplicant_task *task)
1927 DBusMessage *message;
1930 path = g_slist_nth_data(task->scan_results, 0);
1933 * We're done with regular scanning, let's enable the missing
1936 hidden_block_enable(task);
1940 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1941 SUPPLICANT_INTF ".BSSID",
1944 task->scan_results = g_slist_remove(task->scan_results, path);
1947 if (message == NULL)
1950 dbus_message_set_auto_start(message, FALSE);
1952 if (dbus_connection_send_with_reply(connection, message,
1953 &task->result_call, TIMEOUT) == FALSE) {
1954 connman_error("Failed to get network properties");
1955 dbus_message_unref(message);
1959 if (task->result_call == NULL) {
1960 connman_error("D-Bus connection not available");
1961 dbus_message_unref(message);
1965 dbus_pending_call_set_notify(task->result_call,
1966 properties_reply, task, NULL);
1968 dbus_message_unref(message);
1973 task->result_call = NULL;
1975 if (task->scanning == TRUE) {
1976 connman_device_set_scanning(task->device, FALSE);
1977 task->scanning = FALSE;
1981 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1983 struct supplicant_task *task = user_data;
1989 DBG("task %p", task);
1991 reply = dbus_pending_call_steal_reply(call);
1993 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1996 dbus_error_init(&error);
1998 if (dbus_message_get_args(reply, &error,
1999 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
2000 &results, &num_results,
2001 DBUS_TYPE_INVALID) == FALSE) {
2002 if (dbus_error_is_set(&error) == TRUE) {
2003 connman_error("%s", error.message);
2004 dbus_error_free(&error);
2006 connman_error("Wrong arguments for scan result");
2010 if (num_results == 0)
2013 for (i = 0; i < num_results; i++) {
2014 char *path = g_strdup(results[i]);
2018 task->scan_results = g_slist_append(task->scan_results, path);
2021 g_strfreev(results);
2023 dbus_message_unref(reply);
2025 dbus_pending_call_unref(call);
2027 get_properties(task);
2032 dbus_message_unref(reply);
2034 dbus_pending_call_unref(call);
2036 task->result_call = NULL;
2038 if (task->scanning == TRUE) {
2039 connman_device_set_scanning(task->device, FALSE);
2040 task->scanning = FALSE;
2044 static void scan_results_available(struct supplicant_task *task)
2046 DBusMessage *message;
2048 DBG("task %p", task);
2050 if (task->result_call != NULL)
2053 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
2054 SUPPLICANT_INTF ".Interface",
2056 if (message == NULL)
2059 dbus_message_set_auto_start(message, FALSE);
2061 if (dbus_connection_send_with_reply(connection, message,
2062 &task->result_call, TIMEOUT) == FALSE) {
2063 connman_error("Failed to request scan result");
2067 if (task->result_call == NULL) {
2068 connman_error("D-Bus connection not available");
2072 if (task->scanning == TRUE)
2073 connman_device_set_scanning(task->device, TRUE);
2075 dbus_pending_call_set_notify(task->result_call,
2076 scan_results_reply, task, NULL);
2079 dbus_message_unref(message);
2082 static enum supplicant_state string2state(const char *state)
2084 if (g_str_equal(state, "INACTIVE") == TRUE)
2085 return WPA_INACTIVE;
2086 else if (g_str_equal(state, "SCANNING") == TRUE)
2087 return WPA_SCANNING;
2088 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
2089 return WPA_ASSOCIATING;
2090 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
2091 return WPA_ASSOCIATED;
2092 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
2093 return WPA_GROUP_HANDSHAKE;
2094 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
2095 return WPA_4WAY_HANDSHAKE;
2096 else if (g_str_equal(state, "COMPLETED") == TRUE)
2097 return WPA_COMPLETED;
2098 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
2099 return WPA_DISCONNECTED;
2104 static int task_connect(struct supplicant_task *task)
2106 const char *address, *security, *passphrase;
2108 unsigned int ssid_len;
2111 g_hash_table_foreach(task->hidden_blocks, block_reset, task);
2113 connman_inet_ifup(task->ifindex);
2115 address = connman_network_get_string(task->network, "Address");
2116 security = connman_network_get_string(task->network, "WiFi.Security");
2117 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
2119 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
2121 DBG("address %s security %s", address, security);
2123 if (security == NULL)
2126 if (passphrase == NULL && g_str_equal(security, "none") == FALSE &&
2127 g_str_equal(security, "ieee8021x") == FALSE)
2130 remove_network(task);
2136 err = set_network(task, ssid, ssid_len, address, security, passphrase);
2140 err = select_network(task);
2144 return -EINPROGRESS;
2147 static void scanning(struct supplicant_task *task, DBusMessage *msg)
2150 dbus_bool_t scanning;
2152 dbus_error_init(&error);
2154 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
2155 DBUS_TYPE_INVALID) == FALSE) {
2156 if (dbus_error_is_set(&error) == TRUE) {
2157 connman_error("%s", error.message);
2158 dbus_error_free(&error);
2160 connman_error("Wrong arguments for scanning");
2164 connman_info("%s scanning %s", task->ifname,
2165 scanning == TRUE ? "started" : "finished");
2168 static void state_change(struct supplicant_task *task, DBusMessage *msg)
2171 const char *newstate, *oldstate;
2172 unsigned char bssid[ETH_ALEN];
2173 unsigned int bssid_len;
2174 enum supplicant_state state, prevstate;
2176 dbus_error_init(&error);
2178 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
2179 DBUS_TYPE_STRING, &oldstate,
2180 DBUS_TYPE_INVALID) == FALSE) {
2181 if (dbus_error_is_set(&error) == TRUE) {
2182 connman_error("%s", error.message);
2183 dbus_error_free(&error);
2185 connman_error("Wrong arguments for state change");
2189 DBG("state %s ==> %s", oldstate, newstate);
2191 connman_info("%s %s%s", task->ifname, newstate,
2192 task->scanning == TRUE ? " (scanning)" : "");
2194 state = string2state(newstate);
2195 if (state == WPA_INVALID)
2198 prevstate = task->state;
2199 task->state = state;
2201 if (task->network == NULL)
2204 switch (task->state) {
2206 switch (prevstate) {
2207 case WPA_ASSOCIATED:
2208 case WPA_GROUP_HANDSHAKE:
2214 /* reset scan trigger and schedule background scan */
2215 connman_device_schedule_scan(task->device);
2217 if (get_bssid(task->device, bssid, &bssid_len) == 0)
2218 connman_network_set_address(task->network,
2222 connman_network_set_connected(task->network, TRUE);
2225 case WPA_ASSOCIATING:
2226 switch (prevstate) {
2230 connman_network_set_associating(task->network, TRUE);
2238 switch (prevstate) {
2240 case WPA_DISCONNECTED:
2247 case WPA_DISCONNECTED:
2249 connman_network_set_connected(task->network, FALSE);
2251 if (task->disconnecting == TRUE) {
2252 connman_network_unref(task->network);
2253 task->disconnecting = FALSE;
2255 if (task->pending_network != NULL) {
2256 task->network = task->pending_network;
2257 task->pending_network = NULL;
2260 task->network = NULL;
2265 connman_network_set_associating(task->network, FALSE);
2272 connman_error("%s invalid state change %s -> %s", task->ifname,
2273 oldstate, newstate);
2276 static gboolean supplicant_filter(DBusConnection *conn,
2277 DBusMessage *msg, void *data)
2279 struct supplicant_task *task;
2280 const char *member, *path;
2282 member = dbus_message_get_member(msg);
2286 path = dbus_message_get_path(msg);
2290 task = find_task_by_path(path);
2294 DBG("task %p member %s", task, member);
2296 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
2297 scan_results_available(task);
2298 else if (g_str_equal(member, "Scanning") == TRUE)
2299 scanning(task, msg);
2300 else if (g_str_equal(member, "StateChange") == TRUE)
2301 state_change(task, msg);
2306 int supplicant_start(struct connman_device *device)
2308 struct supplicant_task *task;
2311 DBG("device %p", device);
2313 task = g_try_new0(struct supplicant_task, 1);
2317 task->ifindex = connman_device_get_index(device);
2318 task->ifname = connman_inet_ifname(task->ifindex);
2320 if (task->ifname == NULL) {
2325 task->cfg80211 = connman_inet_is_cfg80211(task->ifindex);
2326 if (task->cfg80211 == FALSE)
2327 connman_warn("Enabling quirks for unsupported driver");
2329 task->range = g_try_malloc0(sizeof(struct iw_range));
2330 if (task->range == NULL) {
2335 err = get_range(task);
2339 task->device = connman_device_ref(device);
2341 task->created = FALSE;
2342 task->scanning = FALSE;
2343 task->state = WPA_INVALID;
2344 task->disconnecting = FALSE;
2345 task->pending_network = NULL;
2346 task->hidden_blocks = g_hash_table_new_full(g_str_hash, g_str_equal,
2347 NULL, remove_block);
2348 task_list = g_slist_append(task_list, task);
2350 return create_interface(task);
2353 g_free(task->range);
2354 g_free(task->ifname);
2360 int supplicant_stop(struct connman_device *device)
2362 int index = connman_device_get_index(device);
2363 struct supplicant_task *task;
2365 DBG("device %p", device);
2367 task = find_task_by_index(index);
2371 g_free(task->range);
2373 task_list = g_slist_remove(task_list, task);
2374 g_hash_table_destroy(task->hidden_blocks);
2376 if (task->scan_call != NULL) {
2377 dbus_pending_call_cancel(task->scan_call);
2378 task->scan_call = NULL;
2381 if (task->result_call != NULL) {
2382 dbus_pending_call_cancel(task->result_call);
2383 task->result_call = NULL;
2386 if (task->scanning == TRUE)
2387 connman_device_set_scanning(task->device, FALSE);
2389 remove_network(task);
2391 disconnect_network(task);
2393 return remove_interface(task);
2396 int supplicant_scan(struct connman_device *device)
2398 int index = connman_device_get_index(device);
2399 struct supplicant_task *task;
2402 DBG("device %p", device);
2404 task = find_task_by_index(index);
2408 switch (task->state) {
2411 case WPA_ASSOCIATING:
2412 case WPA_ASSOCIATED:
2413 case WPA_4WAY_HANDSHAKE:
2414 case WPA_GROUP_HANDSHAKE:
2420 task->scanning = TRUE;
2422 err = initiate_scan(task);
2424 if (err == -EINPROGRESS)
2427 task->scanning = FALSE;
2431 connman_device_set_scanning(task->device, TRUE);
2436 int supplicant_connect(struct connman_network *network)
2438 struct supplicant_task *task;
2441 DBG("network %p", network);
2443 index = connman_network_get_index(network);
2445 task = find_task_by_index(index);
2449 if (task->disconnecting == TRUE)
2450 task->pending_network = connman_network_ref(network);
2452 task->network = connman_network_ref(network);
2453 return task_connect(task);
2456 return -EINPROGRESS;
2459 int supplicant_disconnect(struct connman_network *network)
2461 struct supplicant_task *task;
2464 DBG("network %p", network);
2466 index = connman_network_get_index(network);
2468 task = find_task_by_index(index);
2472 if (task->disconnecting == TRUE)
2475 remove_network(task);
2477 disconnect_network(task);
2479 task->disconnecting = TRUE;
2484 static void supplicant_activate(DBusConnection *conn)
2486 DBusMessage *message;
2488 DBG("conn %p", conn);
2490 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
2491 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
2492 if (message == NULL)
2495 dbus_message_set_no_reply(message, TRUE);
2497 dbus_connection_send(conn, message, NULL);
2499 dbus_message_unref(message);
2502 static GSList *driver_list = NULL;
2504 static void supplicant_probe(DBusConnection *conn, void *user_data)
2508 DBG("conn %p", conn);
2510 for (list = driver_list; list; list = list->next) {
2511 struct supplicant_driver *driver = list->data;
2513 DBG("driver %p name %s", driver, driver->name);
2520 static void supplicant_remove(DBusConnection *conn, void *user_data)
2524 DBG("conn %p", conn);
2526 for (list = driver_list; list; list = list->next) {
2527 struct supplicant_driver *driver = list->data;
2529 DBG("driver %p name %s", driver, driver->name);
2537 static guint iface_watch;
2539 static int supplicant_create(void)
2541 if (g_slist_length(driver_list) > 0)
2544 connection = connman_dbus_get_connection();
2545 if (connection == NULL)
2548 DBG("connection %p", connection);
2550 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2551 supplicant_probe, supplicant_remove, NULL, NULL);
2553 iface_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2554 SUPPLICANT_INTF ".Interface",
2555 NULL, supplicant_filter,
2558 if (watch == 0 || iface_watch == 0) {
2559 g_dbus_remove_watch(connection, watch);
2560 g_dbus_remove_watch(connection, iface_watch);
2567 static void supplicant_destroy(void)
2569 if (g_slist_length(driver_list) > 0)
2572 DBG("connection %p", connection);
2574 g_dbus_remove_watch(connection, watch);
2575 g_dbus_remove_watch(connection, iface_watch);
2577 dbus_connection_unref(connection);
2581 int supplicant_register(struct supplicant_driver *driver)
2585 DBG("driver %p name %s", driver, driver->name);
2587 err = supplicant_create();
2591 driver_list = g_slist_append(driver_list, driver);
2593 supplicant_activate(connection);
2598 void supplicant_unregister(struct supplicant_driver *driver)
2600 DBG("driver %p name %s", driver, driver->name);
2602 supplicant_remove(connection, NULL);
2604 driver_list = g_slist_remove(driver_list, driver);
2606 supplicant_destroy();