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/log.h>
46 #include "supplicant.h"
50 #define IEEE80211_CAP_ESS 0x0001
51 #define IEEE80211_CAP_IBSS 0x0002
52 #define IEEE80211_CAP_PRIVACY 0x0010
54 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
55 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
56 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
58 /* Taken from "WPA Supplicant - Common definitions" */
59 enum supplicant_state {
61 * WPA_DISCONNECTED - Disconnected state
63 * This state indicates that client is not associated, but is likely to
64 * start looking for an access point. This state is entered when a
70 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
72 * This state is entered if there are no enabled networks in the
73 * configuration. wpa_supplicant is not trying to associate with a new
74 * network and external interaction (e.g., ctrl_iface call to add or
75 * enable a network) is needed to start association.
80 * WPA_SCANNING - Scanning for a network
82 * This state is entered when wpa_supplicant starts scanning for a
88 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
90 * This state is entered when wpa_supplicant has found a suitable BSS
91 * to associate with and the driver is configured to try to associate
92 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
93 * state is entered when the driver is configured to try to associate
94 * with a network using the configured SSID and security policy.
99 * WPA_ASSOCIATED - Association completed
101 * This state is entered when the driver reports that association has
102 * been successfully completed with an AP. If IEEE 802.1X is used
103 * (with or without WPA/WPA2), wpa_supplicant remains in this state
104 * until the IEEE 802.1X/EAPOL authentication has been completed.
109 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
111 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
112 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
113 * frame after association. In case of WPA-EAP, this state is entered
114 * when the IEEE 802.1X/EAPOL authentication has been completed.
119 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
121 * This state is entered when 4-Way Key Handshake has been completed
122 * (i.e., when the supplicant sends out message 4/4) and when Group
123 * Key rekeying is started by the AP (i.e., when supplicant receives
129 * WPA_COMPLETED - All authentication completed
131 * This state is entered when the full authentication process is
132 * completed. In case of WPA2, this happens when the 4-Way Handshake is
133 * successfully completed. With WPA, this state is entered after the
134 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
135 * completed after dynamic keys are received (or if not used, after
136 * the EAP authentication has been completed). With static WEP keys and
137 * plaintext connections, this state is entered when an association
138 * has been completed.
140 * This state indicates that the supplicant has completed its
141 * processing for the association phase and that data connection is
147 * WPA_INVALID - Invalid state (parsing error)
149 * This state is returned if the string input is invalid. It is not
150 * an official wpa_supplicant state.
155 struct supplicant_result {
159 unsigned int addr_len;
161 unsigned int ssid_len;
162 dbus_uint16_t capabilities;
170 dbus_int32_t frequency;
171 dbus_int32_t quality;
174 dbus_int32_t maxrate;
177 struct supplicant_task {
181 struct connman_device *device;
182 struct connman_network *network;
183 struct connman_network *pending_network;
187 enum supplicant_state state;
189 GSList *scan_results;
190 DBusPendingCall *scan_call;
191 DBusPendingCall *result_call;
192 struct iw_range *range;
193 gboolean disconnecting;
196 static GSList *task_list = NULL;
198 static DBusConnection *connection;
200 static void free_task(struct supplicant_task *task)
202 DBG("task %p", task);
204 g_free(task->ifname);
209 static struct supplicant_task *find_task_by_index(int index)
213 for (list = task_list; list; list = list->next) {
214 struct supplicant_task *task = list->data;
216 if (task->ifindex == index)
223 static struct supplicant_task *find_task_by_path(const char *path)
227 for (list = task_list; list; list = list->next) {
228 struct supplicant_task *task = list->data;
230 if (g_strcmp0(task->path, path) == 0)
237 static int get_range(struct supplicant_task *task)
242 fd = socket(PF_INET, SOCK_DGRAM, 0);
246 memset(&wrq, 0, sizeof(struct iwreq));
247 strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
248 wrq.u.data.pointer = task->range;
249 wrq.u.data.length = sizeof(struct iw_range);
251 err = ioctl(fd, SIOCGIWRANGE, &wrq);
256 task->range->max_qual.updated |= IW_QUAL_ALL_INVALID;
258 connman_info("%s {scan} capabilities 0x%02x", task->ifname,
259 task->range->scan_capa);
261 connman_info("%s {quality} flags 0x%02x", task->ifname,
262 task->range->max_qual.updated);
267 static int get_bssid(struct connman_device *device,
268 unsigned char *bssid, unsigned int *bssid_len)
275 ifindex = connman_device_get_index(device);
279 ifname = connman_inet_ifname(ifindex);
283 fd = socket(PF_INET, SOCK_DGRAM, 0);
289 memset(&wrq, 0, sizeof(wrq));
290 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
292 err = ioctl(fd, SIOCGIWAP, &wrq);
300 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
301 *bssid_len = ETH_ALEN;
306 static void add_interface_reply(DBusPendingCall *call, void *user_data)
308 struct supplicant_task *task = user_data;
313 DBG("task %p", task);
315 reply = dbus_pending_call_steal_reply(call);
317 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
320 dbus_error_init(&error);
322 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
323 DBUS_TYPE_INVALID) == FALSE) {
324 if (dbus_error_is_set(&error) == TRUE) {
325 connman_error("%s", error.message);
326 dbus_error_free(&error);
328 connman_error("Wrong arguments for add interface");
332 DBG("path %s", path);
334 task->path = g_strdup(path);
335 task->created = TRUE;
337 connman_device_set_powered(task->device, TRUE);
339 dbus_message_unref(reply);
341 dbus_pending_call_unref(call);
346 dbus_message_unref(reply);
348 dbus_pending_call_unref(call);
350 task_list = g_slist_remove(task_list, task);
352 connman_device_unref(task->device);
357 static int add_interface(struct supplicant_task *task)
359 const char *driver = connman_option_get_string("wifi");
360 DBusMessage *message;
361 DBusMessageIter array, dict;
362 DBusPendingCall *call;
364 DBG("task %p", task);
366 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
367 SUPPLICANT_INTF, "addInterface");
371 dbus_message_set_auto_start(message, FALSE);
373 dbus_message_iter_init_append(message, &array);
375 dbus_message_iter_append_basic(&array,
376 DBUS_TYPE_STRING, &task->ifname);
378 connman_dbus_dict_open(&array, &dict);
380 connman_dbus_dict_append_basic(&dict, "driver",
381 DBUS_TYPE_STRING, &driver);
383 connman_dbus_dict_close(&array, &dict);
385 if (dbus_connection_send_with_reply(connection, message,
386 &call, TIMEOUT) == FALSE) {
387 connman_error("Failed to add interface");
388 dbus_message_unref(message);
393 connman_error("D-Bus connection not available");
394 dbus_message_unref(message);
398 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
400 dbus_message_unref(message);
405 static void get_interface_reply(DBusPendingCall *call, void *user_data)
407 struct supplicant_task *task = user_data;
412 DBG("task %p", task);
414 reply = dbus_pending_call_steal_reply(call);
416 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
421 dbus_error_init(&error);
423 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
424 DBUS_TYPE_INVALID) == FALSE) {
425 if (dbus_error_is_set(&error) == TRUE) {
426 connman_error("%s", error.message);
427 dbus_error_free(&error);
429 connman_error("Wrong arguments for get interface");
433 DBG("path %s", path);
435 task->path = g_strdup(path);
436 task->created = FALSE;
438 connman_device_set_powered(task->device, TRUE);
441 dbus_message_unref(reply);
443 dbus_pending_call_unref(call);
446 static int create_interface(struct supplicant_task *task)
448 DBusMessage *message;
449 DBusPendingCall *call;
451 DBG("task %p", task);
453 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
454 SUPPLICANT_INTF, "getInterface");
458 dbus_message_set_auto_start(message, FALSE);
460 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
463 if (dbus_connection_send_with_reply(connection, message,
464 &call, TIMEOUT) == FALSE) {
465 connman_error("Failed to get interface");
466 dbus_message_unref(message);
471 connman_error("D-Bus connection not available");
472 dbus_message_unref(message);
476 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
478 dbus_message_unref(message);
483 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
485 struct supplicant_task *task = user_data;
488 DBG("task %p", task);
490 reply = dbus_pending_call_steal_reply(call);
492 connman_device_set_powered(task->device, FALSE);
494 connman_device_unref(task->device);
496 connman_inet_ifdown(task->ifindex);
500 dbus_message_unref(reply);
502 dbus_pending_call_unref(call);
505 static int remove_interface(struct supplicant_task *task)
507 DBusMessage *message;
508 DBusPendingCall *call;
510 DBG("task %p", task);
512 if (task->path == NULL)
516 if (task->created == FALSE) {
517 connman_device_set_powered(task->device, FALSE);
522 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
523 SUPPLICANT_INTF, "removeInterface");
527 dbus_message_set_auto_start(message, FALSE);
529 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
532 if (dbus_connection_send_with_reply(connection, message,
533 &call, TIMEOUT) == FALSE) {
534 connman_error("Failed to remove interface");
535 dbus_message_unref(message);
540 connman_error("D-Bus connection not available");
541 dbus_message_unref(message);
545 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
547 dbus_message_unref(message);
552 static int set_ap_scan(struct supplicant_task *task)
554 DBusMessage *message, *reply;
558 DBG("task %p", task);
560 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
561 SUPPLICANT_INTF ".Interface", "setAPScan");
565 dbus_message_set_auto_start(message, FALSE);
567 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
570 dbus_error_init(&error);
572 reply = dbus_connection_send_with_reply_and_block(connection,
573 message, -1, &error);
575 if (dbus_error_is_set(&error) == TRUE) {
576 connman_error("%s", error.message);
577 dbus_error_free(&error);
579 connman_error("Failed to set AP scan");
580 dbus_message_unref(message);
584 dbus_message_unref(message);
586 dbus_message_unref(reply);
591 static int add_network(struct supplicant_task *task)
593 DBusMessage *message, *reply;
597 DBG("task %p", task);
599 if (task->netpath != NULL)
602 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
603 SUPPLICANT_INTF ".Interface", "addNetwork");
607 dbus_message_set_auto_start(message, FALSE);
609 dbus_error_init(&error);
611 reply = dbus_connection_send_with_reply_and_block(connection,
612 message, -1, &error);
614 if (dbus_error_is_set(&error) == TRUE) {
615 connman_error("%s", error.message);
616 dbus_error_free(&error);
618 connman_error("Failed to add network");
619 dbus_message_unref(message);
623 dbus_message_unref(message);
625 dbus_error_init(&error);
627 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
628 DBUS_TYPE_INVALID) == FALSE) {
629 if (dbus_error_is_set(&error) == TRUE) {
630 connman_error("%s", error.message);
631 dbus_error_free(&error);
633 connman_error("Wrong arguments for network");
634 dbus_message_unref(reply);
638 DBG("path %s", path);
640 task->netpath = g_strdup(path);
642 dbus_message_unref(reply);
647 static int remove_network(struct supplicant_task *task)
649 DBusMessage *message, *reply;
652 DBG("task %p", task);
654 if (task->netpath == NULL || task->path == NULL)
657 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
658 SUPPLICANT_INTF ".Interface", "removeNetwork");
662 dbus_message_set_auto_start(message, FALSE);
664 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
667 dbus_error_init(&error);
669 reply = dbus_connection_send_with_reply_and_block(connection,
670 message, -1, &error);
672 if (dbus_error_is_set(&error) == TRUE) {
673 connman_error("%s", error.message);
674 dbus_error_free(&error);
676 connman_error("Failed to remove network");
677 dbus_message_unref(message);
681 dbus_message_unref(message);
683 dbus_message_unref(reply);
685 g_free(task->netpath);
686 task->netpath = NULL;
691 static int select_network(struct supplicant_task *task)
693 DBusMessage *message, *reply;
696 DBG("task %p", task);
698 if (task->netpath == NULL)
701 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
702 SUPPLICANT_INTF ".Interface", "selectNetwork");
706 dbus_message_set_auto_start(message, FALSE);
708 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
711 dbus_error_init(&error);
713 reply = dbus_connection_send_with_reply_and_block(connection,
714 message, -1, &error);
716 if (dbus_error_is_set(&error) == TRUE) {
717 connman_error("%s", error.message);
718 dbus_error_free(&error);
720 connman_error("Failed to select network");
721 dbus_message_unref(message);
725 dbus_message_unref(message);
727 dbus_message_unref(reply);
732 static int disconnect_network(struct supplicant_task *task)
734 DBusMessage *message, *reply;
737 DBG("task %p", task);
739 if (task->path == NULL)
742 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
743 SUPPLICANT_INTF ".Interface", "disconnect");
747 dbus_message_set_auto_start(message, FALSE);
749 dbus_error_init(&error);
751 reply = dbus_connection_send_with_reply_and_block(connection,
752 message, -1, &error);
754 if (dbus_error_is_set(&error) == TRUE) {
755 connman_error("%s", error.message);
756 dbus_error_free(&error);
758 connman_error("Failed to disconnect network");
759 dbus_message_unref(message);
763 dbus_message_unref(message);
765 dbus_message_unref(reply);
770 static int set_network_tls(struct connman_network *network,
771 DBusMessageIter *dict)
773 const char *private_key, *client_cert, *ca_cert;
774 const char *private_key_password;
777 * For TLS, we at least need a key, the client cert,
779 * Server cert is optional.
781 client_cert = connman_network_get_string(network,
782 "WiFi.ClientCertFile");
783 if (client_cert == NULL)
786 private_key = connman_network_get_string(network,
787 "WiFi.PrivateKeyFile");
788 if (private_key == NULL)
791 private_key_password = connman_network_get_string(network,
792 "WiFi.PrivateKeyPassphrase");
793 if (private_key_password == NULL)
796 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
798 connman_dbus_dict_append_basic(dict, "ca_cert",
799 DBUS_TYPE_STRING, &ca_cert);
801 DBG("client cert %s private key %s", client_cert, private_key);
803 connman_dbus_dict_append_basic(dict, "private_key",
804 DBUS_TYPE_STRING, &private_key);
805 connman_dbus_dict_append_basic(dict, "private_key_passwd",
807 &private_key_password);
808 connman_dbus_dict_append_basic(dict, "client_cert",
809 DBUS_TYPE_STRING, &client_cert);
814 static int set_network_peap(struct connman_network *network,
815 DBusMessageIter *dict, const char *passphrase)
817 const char *client_cert, *ca_cert, *phase2;
821 * For PEAP, we at least need the sever cert, a 2nd
822 * phase authentication and a passhprase.
823 * Client cert is optional although strongly required
824 * When setting the client cert, we then need a private
827 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
831 phase2 = connman_network_get_string(network, "WiFi.Phase2");
835 DBG("CA cert %s phase2 auth %s", ca_cert, phase2);
837 client_cert = connman_network_get_string(network,
838 "WiFi.ClientCertFile");
840 const char *private_key, *private_key_password;
842 private_key = connman_network_get_string(network,
843 "WiFi.PrivateKeyFile");
844 if (private_key == NULL)
847 private_key_password =
848 connman_network_get_string(network,
849 "WiFi.PrivateKeyPassphrase");
850 if (private_key_password == NULL)
853 connman_dbus_dict_append_basic(dict, "client_cert",
854 DBUS_TYPE_STRING, &client_cert);
856 connman_dbus_dict_append_basic(dict, "private_key",
857 DBUS_TYPE_STRING, &private_key);
859 connman_dbus_dict_append_basic(dict, "private_key_passwd",
861 &private_key_password);
863 DBG("client cert %s private key %s", client_cert, private_key);
866 phase2_auth = g_strdup_printf("\"auth=%s\"", phase2);
868 connman_dbus_dict_append_basic(dict, "password",
869 DBUS_TYPE_STRING, &passphrase);
871 connman_dbus_dict_append_basic(dict, "ca_cert",
872 DBUS_TYPE_STRING, &ca_cert);
874 connman_dbus_dict_append_basic(dict, "phase2",
875 DBUS_TYPE_STRING, &phase2_auth);
882 static int set_network(struct supplicant_task *task,
883 const unsigned char *network, int len,
884 const char *address, const char *security,
885 const char *passphrase)
887 DBusMessage *message, *reply;
888 DBusMessageIter array, dict;
890 dbus_uint32_t scan_ssid = 1;
892 DBG("task %p", task);
894 if (task->netpath == NULL)
897 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
898 SUPPLICANT_INTF ".Network", "set");
902 dbus_message_set_auto_start(message, FALSE);
904 dbus_message_iter_init_append(message, &array);
906 connman_dbus_dict_open(&array, &dict);
908 connman_dbus_dict_append_basic(&dict, "scan_ssid",
909 DBUS_TYPE_UINT32, &scan_ssid);
912 connman_dbus_dict_append_fixed_array(&dict, "ssid",
913 DBUS_TYPE_BYTE, &network, len);
915 connman_dbus_dict_append_basic(&dict, "bssid",
916 DBUS_TYPE_STRING, &address);
918 if (g_ascii_strcasecmp(security, "psk") == 0 ||
919 g_ascii_strcasecmp(security, "wpa") == 0 ||
920 g_ascii_strcasecmp(security, "rsn") == 0) {
921 const char *key_mgmt = "WPA-PSK";
922 connman_dbus_dict_append_basic(&dict, "key_mgmt",
923 DBUS_TYPE_STRING, &key_mgmt);
925 if (passphrase && strlen(passphrase) > 0)
926 connman_dbus_dict_append_basic(&dict, "psk",
927 DBUS_TYPE_STRING, &passphrase);
928 } else if (g_ascii_strcasecmp(security, "ieee8021x") == 0) {
929 struct connman_network *network = task->network;
930 const char *key_mgmt = "WPA-EAP", *eap, *identity;
934 * If our private key password is unset,
935 * we use the supplied passphrase. That is needed
936 * for PEAP where 2 passphrases (identity and client
937 * cert may have to be provided.
939 if (connman_network_get_string(network,
940 "WiFi.PrivateKeyPassphrase") == NULL)
941 connman_network_set_string(network,
942 "WiFi.PrivateKeyPassphrase",
945 eap = connman_network_get_string(network, "WiFi.EAP");
949 /* We must have an identity for both PEAP and TLS */
950 identity = connman_network_get_string(network, "WiFi.Identity");
951 if (identity == NULL)
954 DBG("key_mgmt %s eap %s identity %s", key_mgmt, eap, identity);
956 if (g_strcmp0(eap, "tls") == 0) {
959 err = set_network_tls(network, &dict);
961 dbus_message_unref(message);
964 } else if (g_strcmp0(eap, "peap") == 0) {
967 err = set_network_peap(network, &dict, passphrase);
969 dbus_message_unref(message);
973 connman_error("Unknown EAP %s", eap);
977 /* wpa_supplicant only accepts upper case EAPs */
978 eap_value = g_ascii_strup(eap, -1);
980 connman_dbus_dict_append_basic(&dict, "key_mgmt",
983 connman_dbus_dict_append_basic(&dict, "eap",
986 connman_dbus_dict_append_basic(&dict, "identity",
992 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
993 const char *key_mgmt = "NONE";
994 const char *auth_alg = "OPEN";
995 const char *key_index = "0";
997 if (task->cfg80211 == TRUE)
998 auth_alg = "OPEN SHARED";
1000 connman_dbus_dict_append_basic(&dict, "auth_alg",
1001 DBUS_TYPE_STRING, &auth_alg);
1003 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1004 DBUS_TYPE_STRING, &key_mgmt);
1007 int size = strlen(passphrase);
1008 if (size == 10 || size == 26) {
1009 unsigned char *key = malloc(13);
1012 memset(tmp, 0, sizeof(tmp));
1015 for (i = 0; i < size / 2; i++) {
1016 memcpy(tmp, passphrase + (i * 2), 2);
1017 key[i] = (unsigned char) strtol(tmp,
1020 connman_dbus_dict_append_fixed_array(&dict,
1021 "wep_key0", DBUS_TYPE_BYTE,
1025 connman_dbus_dict_append_basic(&dict,
1026 "wep_key0", DBUS_TYPE_STRING,
1029 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
1030 DBUS_TYPE_STRING, &key_index);
1033 const char *key_mgmt = "NONE";
1034 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1035 DBUS_TYPE_STRING, &key_mgmt);
1038 connman_dbus_dict_close(&array, &dict);
1040 dbus_error_init(&error);
1042 reply = dbus_connection_send_with_reply_and_block(connection,
1043 message, -1, &error);
1044 if (reply == NULL) {
1045 if (dbus_error_is_set(&error) == TRUE) {
1046 connman_error("%s", error.message);
1047 dbus_error_free(&error);
1049 connman_error("Failed to set network options");
1050 dbus_message_unref(message);
1054 dbus_message_unref(message);
1056 dbus_message_unref(reply);
1061 dbus_message_unref(message);
1065 static void scan_reply(DBusPendingCall *call, void *user_data)
1067 struct supplicant_task *task = user_data;
1070 DBG("task %p", task);
1072 task->scan_call = NULL;
1074 reply = dbus_pending_call_steal_reply(call);
1076 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1077 connman_device_set_scanning(task->device, FALSE);
1081 if (task->scanning == TRUE)
1082 connman_device_set_scanning(task->device, TRUE);
1085 dbus_message_unref(reply);
1087 dbus_pending_call_unref(call);
1091 static int initiate_scan(struct supplicant_task *task)
1093 DBusMessage *message;
1095 DBG("task %p", task);
1097 if (task->path == NULL)
1100 if (task->scan_call != NULL)
1103 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1104 SUPPLICANT_INTF ".Interface", "scan");
1105 if (message == NULL)
1108 dbus_message_set_auto_start(message, FALSE);
1110 if (dbus_connection_send_with_reply(connection, message,
1111 &task->scan_call, TIMEOUT) == FALSE) {
1112 connman_error("Failed to initiate scan");
1113 dbus_message_unref(message);
1117 if (task->scan_call == NULL) {
1118 connman_error("D-Bus connection not available");
1119 dbus_message_unref(message);
1123 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
1125 dbus_message_unref(message);
1127 return -EINPROGRESS;
1133 } special_ssid[] = {
1134 { "<hidden>", "hidden" },
1135 { "default", "linksys" },
1143 { "comcomcom", "3com" },
1145 { "Symbol", "symbol" },
1146 { "Motorola", "motorola" },
1147 { "Wireless" , "wireless" },
1152 static char *build_group(const char *addr, const char *name,
1153 const unsigned char *ssid, unsigned int ssid_len,
1154 const char *mode, const char *security)
1162 str = g_string_sized_new((ssid_len * 2) + 24);
1167 g_string_append_printf(str, "hidden_%s", addr);
1171 for (i = 0; special_ssid[i].name; i++) {
1172 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1173 if (special_ssid[i].value == NULL)
1174 g_string_append_printf(str, "%s_%s",
1177 g_string_append_printf(str, "%s_%s",
1178 special_ssid[i].value, addr);
1183 if (ssid_len > 0 && ssid[0] != '\0') {
1184 for (i = 0; i < ssid_len; i++)
1185 g_string_append_printf(str, "%02x", ssid[i]);
1187 g_string_append_printf(str, "hidden_%s", addr);
1190 g_string_append_printf(str, "_%s_%s", mode, security);
1192 return g_string_free(str, FALSE);
1195 static void extract_addr(DBusMessageIter *value,
1196 struct supplicant_result *result)
1198 DBusMessageIter array;
1199 struct ether_addr eth;
1200 unsigned char *addr;
1203 dbus_message_iter_recurse(value, &array);
1204 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1209 result->addr = g_try_malloc(addr_len);
1210 if (result->addr == NULL)
1213 memcpy(result->addr, addr, addr_len);
1214 result->addr_len = addr_len;
1216 result->path = g_try_malloc0(13);
1217 if (result->path == NULL)
1220 memcpy(ð, addr, sizeof(eth));
1221 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1222 eth.ether_addr_octet[0],
1223 eth.ether_addr_octet[1],
1224 eth.ether_addr_octet[2],
1225 eth.ether_addr_octet[3],
1226 eth.ether_addr_octet[4],
1227 eth.ether_addr_octet[5]);
1230 static void extract_ssid(DBusMessageIter *value,
1231 struct supplicant_result *result)
1233 DBusMessageIter array;
1234 unsigned char *ssid;
1237 dbus_message_iter_recurse(value, &array);
1238 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1243 if (ssid[0] == '\0')
1246 result->ssid = g_try_malloc(ssid_len);
1247 if (result->ssid == NULL)
1250 memcpy(result->ssid, ssid, ssid_len);
1251 result->ssid_len = ssid_len;
1253 result->name = g_try_malloc0(ssid_len + 1);
1254 if (result->name == NULL)
1257 for (i = 0; i < ssid_len; i++) {
1258 if (g_ascii_isprint(ssid[i]))
1259 result->name[i] = ssid[i];
1261 result->name[i] = ' ';
1265 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1266 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1268 static void extract_rsn(struct supplicant_result *result,
1269 const unsigned char *buf, int len)
1288 /* Pairwise cipher */
1292 count = buf[0] | (buf[1] << 8);
1293 if (2 + (count * 4) > len)
1296 buf += 2 + (count * 4);
1297 len -= 2 + (count * 4);
1299 /* Authentication */
1303 count = buf[0] | (buf[1] << 8);
1304 if (2 + (count * 4) > len)
1307 for (i = 0; i < count; i++) {
1308 const unsigned char *ptr = buf + 2 + (i * 4);
1310 if (memcmp(ptr, wifi_oui, 3) == 0) {
1313 result->has_8021x = TRUE;
1316 result->has_psk = TRUE;
1319 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1322 result->has_8021x = TRUE;
1325 result->has_psk = TRUE;
1331 buf += 2 + (count * 4);
1332 len -= 2 + (count * 4);
1335 static void extract_wpaie(DBusMessageIter *value,
1336 struct supplicant_result *result)
1338 DBusMessageIter array;
1342 dbus_message_iter_recurse(value, &array);
1343 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1346 result->has_wpa = TRUE;
1347 extract_rsn(result, ie + 6, ie_len - 6);
1351 static void extract_rsnie(DBusMessageIter *value,
1352 struct supplicant_result *result)
1354 DBusMessageIter array;
1358 dbus_message_iter_recurse(value, &array);
1359 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1362 result->has_rsn = TRUE;
1363 extract_rsn(result, ie + 2, ie_len - 2);
1367 static void extract_wpsie(DBusMessageIter *value,
1368 struct supplicant_result *result)
1370 DBusMessageIter array;
1374 dbus_message_iter_recurse(value, &array);
1375 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1378 result->has_wps = TRUE;
1381 static void extract_capabilites(DBusMessageIter *value,
1382 struct supplicant_result *result)
1384 dbus_message_iter_get_basic(value, &result->capabilities);
1386 if (result->capabilities & IEEE80211_CAP_ESS)
1387 result->adhoc = FALSE;
1388 else if (result->capabilities & IEEE80211_CAP_IBSS)
1389 result->adhoc = TRUE;
1391 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1392 result->has_wep = TRUE;
1395 static unsigned char calculate_strength(struct supplicant_task *task,
1396 struct supplicant_result *result)
1398 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1399 unsigned char strength;
1401 if (result->level > 0)
1402 strength = 100 - result->level;
1404 strength = 120 + result->level;
1412 return (result->quality * 100) / task->range->max_qual.qual;
1415 static unsigned short calculate_channel(struct supplicant_result *result)
1417 if (result->frequency < 0)
1420 return (result->frequency - 2407) / 5;
1423 static void get_properties(struct supplicant_task *task);
1425 static void properties_reply(DBusPendingCall *call, void *user_data)
1427 struct supplicant_task *task = user_data;
1428 struct supplicant_result result;
1429 struct connman_network *network;
1431 DBusMessageIter array, dict;
1432 unsigned char strength;
1433 unsigned short channel, frequency;
1434 const char *mode, *security;
1437 DBG("task %p", task);
1439 reply = dbus_pending_call_steal_reply(call);
1441 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1444 memset(&result, 0, sizeof(result));
1445 result.frequency = -1;
1446 result.quality = -1;
1450 dbus_message_iter_init(reply, &array);
1452 dbus_message_iter_recurse(&array, &dict);
1454 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1455 DBusMessageIter entry, value;
1458 dbus_message_iter_recurse(&dict, &entry);
1459 dbus_message_iter_get_basic(&entry, &key);
1461 dbus_message_iter_next(&entry);
1463 dbus_message_iter_recurse(&entry, &value);
1465 //type = dbus_message_iter_get_arg_type(&value);
1466 //dbus_message_iter_get_basic(&value, &val);
1474 * frequency : i (105)
1475 * capabilities : q (113)
1482 if (g_str_equal(key, "bssid") == TRUE)
1483 extract_addr(&value, &result);
1484 else if (g_str_equal(key, "ssid") == TRUE)
1485 extract_ssid(&value, &result);
1486 else if (g_str_equal(key, "wpaie") == TRUE)
1487 extract_wpaie(&value, &result);
1488 else if (g_str_equal(key, "rsnie") == TRUE)
1489 extract_rsnie(&value, &result);
1490 else if (g_str_equal(key, "wpsie") == TRUE)
1491 extract_wpsie(&value, &result);
1492 else if (g_str_equal(key, "capabilities") == TRUE)
1493 extract_capabilites(&value, &result);
1494 else if (g_str_equal(key, "frequency") == TRUE)
1495 dbus_message_iter_get_basic(&value, &result.frequency);
1496 else if (g_str_equal(key, "quality") == TRUE)
1497 dbus_message_iter_get_basic(&value, &result.quality);
1498 else if (g_str_equal(key, "noise") == TRUE)
1499 dbus_message_iter_get_basic(&value, &result.noise);
1500 else if (g_str_equal(key, "level") == TRUE)
1501 dbus_message_iter_get_basic(&value, &result.level);
1502 else if (g_str_equal(key, "maxrate") == TRUE)
1503 dbus_message_iter_get_basic(&value, &result.maxrate);
1505 dbus_message_iter_next(&dict);
1508 DBG("capabilties %u frequency %d "
1509 "quality %d noise %d level %d maxrate %d",
1510 result.capabilities, result.frequency,
1511 result.quality, result.noise,
1512 result.level, result.maxrate);
1514 if (result.path == NULL)
1517 if (result.path[0] == '\0')
1520 if (result.frequency > 0 && result.frequency < 14)
1521 result.frequency = 2407 + (5 * result.frequency);
1522 else if (result.frequency == 14)
1523 result.frequency = 2484;
1525 strength = calculate_strength(task, &result);
1526 channel = calculate_channel(&result);
1528 frequency = (result.frequency < 0) ? 0 : result.frequency;
1530 if (result.has_8021x == TRUE)
1531 security = "ieee8021x";
1532 else if (result.has_psk == TRUE)
1534 else if (result.has_wep == TRUE)
1539 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1541 group = build_group(result.path, result.name,
1542 result.ssid, result.ssid_len,
1545 if (result.has_psk == TRUE) {
1546 if (result.has_rsn == TRUE)
1548 else if (result.has_wpa == TRUE)
1552 network = connman_device_get_network(task->device, result.path);
1553 if (network == NULL) {
1556 network = connman_network_create(result.path,
1557 CONNMAN_NETWORK_TYPE_WIFI);
1558 if (network == NULL)
1561 index = connman_device_get_index(task->device);
1562 connman_network_set_index(network, index);
1564 connman_network_set_protocol(network,
1565 CONNMAN_NETWORK_PROTOCOL_IP);
1567 connman_network_set_address(network, result.addr,
1570 if (connman_device_add_network(task->device, network) < 0) {
1571 connman_network_unref(network);
1576 if (result.name != NULL && result.name[0] != '\0')
1577 connman_network_set_name(network, result.name);
1579 if (result.ssid_len != 0)
1580 connman_network_set_blob(network, "WiFi.SSID",
1581 result.ssid, result.ssid_len);
1583 connman_network_set_string(network, "WiFi.Mode", mode);
1585 DBG("%s (%s %s) strength %d (%s)",
1586 result.name, mode, security, strength,
1587 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1589 connman_network_set_available(network, TRUE);
1590 connman_network_set_strength(network, strength);
1592 connman_network_set_uint16(network, "Frequency", frequency);
1593 connman_network_set_uint16(network, "WiFi.Channel", channel);
1594 connman_network_set_string(network, "WiFi.Security", security);
1596 if (result.ssid != NULL)
1597 connman_network_set_group(network, group);
1602 g_free(result.path);
1603 g_free(result.addr);
1604 g_free(result.name);
1605 g_free(result.ssid);
1608 dbus_message_unref(reply);
1610 dbus_pending_call_unref(call);
1612 get_properties(task);
1615 static void get_properties(struct supplicant_task *task)
1617 DBusMessage *message;
1620 path = g_slist_nth_data(task->scan_results, 0);
1624 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1625 SUPPLICANT_INTF ".BSSID",
1628 task->scan_results = g_slist_remove(task->scan_results, path);
1631 if (message == NULL)
1634 dbus_message_set_auto_start(message, FALSE);
1636 if (dbus_connection_send_with_reply(connection, message,
1637 &task->result_call, TIMEOUT) == FALSE) {
1638 connman_error("Failed to get network properties");
1639 dbus_message_unref(message);
1643 if (task->result_call == NULL) {
1644 connman_error("D-Bus connection not available");
1645 dbus_message_unref(message);
1649 dbus_pending_call_set_notify(task->result_call,
1650 properties_reply, task, NULL);
1652 dbus_message_unref(message);
1657 task->result_call = NULL;
1659 if (task->scanning == TRUE) {
1660 connman_device_set_scanning(task->device, FALSE);
1661 task->scanning = FALSE;
1665 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1667 struct supplicant_task *task = user_data;
1673 DBG("task %p", task);
1675 reply = dbus_pending_call_steal_reply(call);
1677 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1680 dbus_error_init(&error);
1682 if (dbus_message_get_args(reply, &error,
1683 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1684 &results, &num_results,
1685 DBUS_TYPE_INVALID) == FALSE) {
1686 if (dbus_error_is_set(&error) == TRUE) {
1687 connman_error("%s", error.message);
1688 dbus_error_free(&error);
1690 connman_error("Wrong arguments for scan result");
1694 if (num_results == 0)
1697 for (i = 0; i < num_results; i++) {
1698 char *path = g_strdup(results[i]);
1702 task->scan_results = g_slist_append(task->scan_results, path);
1705 g_strfreev(results);
1707 dbus_message_unref(reply);
1709 dbus_pending_call_unref(call);
1711 get_properties(task);
1716 dbus_message_unref(reply);
1718 dbus_pending_call_unref(call);
1720 task->result_call = NULL;
1722 if (task->scanning == TRUE) {
1723 connman_device_set_scanning(task->device, FALSE);
1724 task->scanning = FALSE;
1728 static void scan_results_available(struct supplicant_task *task)
1730 DBusMessage *message;
1732 DBG("task %p", task);
1734 if (task->result_call != NULL)
1737 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1738 SUPPLICANT_INTF ".Interface",
1740 if (message == NULL)
1743 dbus_message_set_auto_start(message, FALSE);
1745 if (dbus_connection_send_with_reply(connection, message,
1746 &task->result_call, TIMEOUT) == FALSE) {
1747 connman_error("Failed to request scan result");
1751 if (task->result_call == NULL) {
1752 connman_error("D-Bus connection not available");
1756 if (task->scanning == TRUE)
1757 connman_device_set_scanning(task->device, TRUE);
1759 dbus_pending_call_set_notify(task->result_call,
1760 scan_results_reply, task, NULL);
1763 dbus_message_unref(message);
1766 static enum supplicant_state string2state(const char *state)
1768 if (g_str_equal(state, "INACTIVE") == TRUE)
1769 return WPA_INACTIVE;
1770 else if (g_str_equal(state, "SCANNING") == TRUE)
1771 return WPA_SCANNING;
1772 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1773 return WPA_ASSOCIATING;
1774 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1775 return WPA_ASSOCIATED;
1776 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1777 return WPA_GROUP_HANDSHAKE;
1778 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1779 return WPA_4WAY_HANDSHAKE;
1780 else if (g_str_equal(state, "COMPLETED") == TRUE)
1781 return WPA_COMPLETED;
1782 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1783 return WPA_DISCONNECTED;
1788 static int task_connect(struct supplicant_task *task)
1790 const char *address, *security, *passphrase;
1792 unsigned int ssid_len;
1795 connman_inet_ifup(task->ifindex);
1797 address = connman_network_get_string(task->network, "Address");
1798 security = connman_network_get_string(task->network, "WiFi.Security");
1799 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1801 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1803 DBG("address %s security %s", address, security);
1805 if (security == NULL)
1808 if (passphrase == NULL && g_str_equal(security, "none") == FALSE &&
1809 g_str_equal(security, "ieee8021x") == FALSE)
1812 remove_network(task);
1818 err = set_network(task, ssid, ssid_len, address, security, passphrase);
1822 err = select_network(task);
1826 return -EINPROGRESS;
1829 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1832 dbus_bool_t scanning;
1834 dbus_error_init(&error);
1836 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1837 DBUS_TYPE_INVALID) == FALSE) {
1838 if (dbus_error_is_set(&error) == TRUE) {
1839 connman_error("%s", error.message);
1840 dbus_error_free(&error);
1842 connman_error("Wrong arguments for scanning");
1846 connman_info("%s scanning %s", task->ifname,
1847 scanning == TRUE ? "started" : "finished");
1850 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1853 const char *newstate, *oldstate;
1854 unsigned char bssid[ETH_ALEN];
1855 unsigned int bssid_len;
1856 enum supplicant_state state, prevstate;
1858 dbus_error_init(&error);
1860 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1861 DBUS_TYPE_STRING, &oldstate,
1862 DBUS_TYPE_INVALID) == FALSE) {
1863 if (dbus_error_is_set(&error) == TRUE) {
1864 connman_error("%s", error.message);
1865 dbus_error_free(&error);
1867 connman_error("Wrong arguments for state change");
1871 DBG("state %s ==> %s", oldstate, newstate);
1873 connman_info("%s %s%s", task->ifname, newstate,
1874 task->scanning == TRUE ? " (scanning)" : "");
1876 state = string2state(newstate);
1877 if (state == WPA_INVALID)
1880 prevstate = task->state;
1881 task->state = state;
1883 if (task->network == NULL)
1886 switch (task->state) {
1888 switch (prevstate) {
1889 case WPA_ASSOCIATED:
1890 case WPA_GROUP_HANDSHAKE:
1896 /* reset scan trigger and schedule background scan */
1897 connman_device_schedule_scan(task->device);
1899 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1900 connman_network_set_address(task->network,
1904 connman_network_set_connected(task->network, TRUE);
1907 case WPA_ASSOCIATING:
1908 switch (prevstate) {
1912 connman_network_set_associating(task->network, TRUE);
1920 switch (prevstate) {
1922 case WPA_DISCONNECTED:
1929 case WPA_DISCONNECTED:
1931 connman_network_set_connected(task->network, FALSE);
1933 if (task->disconnecting == TRUE) {
1934 connman_network_unref(task->network);
1935 task->disconnecting = FALSE;
1937 if (task->pending_network != NULL) {
1938 task->network = task->pending_network;
1939 task->pending_network = NULL;
1942 task->network = NULL;
1947 connman_network_set_associating(task->network, FALSE);
1954 connman_error("%s invalid state change %s -> %s", task->ifname,
1955 oldstate, newstate);
1958 static gboolean supplicant_filter(DBusConnection *conn,
1959 DBusMessage *msg, void *data)
1961 struct supplicant_task *task;
1962 const char *member, *path;
1964 member = dbus_message_get_member(msg);
1968 path = dbus_message_get_path(msg);
1972 task = find_task_by_path(path);
1976 DBG("task %p member %s", task, member);
1978 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1979 scan_results_available(task);
1980 else if (g_str_equal(member, "Scanning") == TRUE)
1981 scanning(task, msg);
1982 else if (g_str_equal(member, "StateChange") == TRUE)
1983 state_change(task, msg);
1988 int supplicant_start(struct connman_device *device)
1990 struct supplicant_task *task;
1993 DBG("device %p", device);
1995 task = g_try_new0(struct supplicant_task, 1);
1999 task->ifindex = connman_device_get_index(device);
2000 task->ifname = connman_inet_ifname(task->ifindex);
2002 if (task->ifname == NULL) {
2007 task->cfg80211 = connman_inet_is_cfg80211(task->ifindex);
2008 if (task->cfg80211 == FALSE)
2009 connman_warn("Enabling quirks for unsupported driver");
2011 task->range = g_try_malloc0(sizeof(struct iw_range));
2012 if (task->range == NULL) {
2017 err = get_range(task);
2021 task->device = connman_device_ref(device);
2023 task->created = FALSE;
2024 task->scanning = FALSE;
2025 task->state = WPA_INVALID;
2026 task->disconnecting = FALSE;
2027 task->pending_network = NULL;
2029 task_list = g_slist_append(task_list, task);
2031 return create_interface(task);
2034 g_free(task->range);
2035 g_free(task->ifname);
2041 int supplicant_stop(struct connman_device *device)
2043 int index = connman_device_get_index(device);
2044 struct supplicant_task *task;
2046 DBG("device %p", device);
2048 task = find_task_by_index(index);
2052 g_free(task->range);
2054 task_list = g_slist_remove(task_list, task);
2056 if (task->scan_call != NULL) {
2057 dbus_pending_call_cancel(task->scan_call);
2058 task->scan_call = NULL;
2061 if (task->result_call != NULL) {
2062 dbus_pending_call_cancel(task->result_call);
2063 task->result_call = NULL;
2066 if (task->scanning == TRUE)
2067 connman_device_set_scanning(task->device, FALSE);
2069 remove_network(task);
2071 disconnect_network(task);
2073 return remove_interface(task);
2076 int supplicant_scan(struct connman_device *device)
2078 int index = connman_device_get_index(device);
2079 struct supplicant_task *task;
2082 DBG("device %p", device);
2084 task = find_task_by_index(index);
2088 switch (task->state) {
2091 case WPA_ASSOCIATING:
2092 case WPA_ASSOCIATED:
2093 case WPA_4WAY_HANDSHAKE:
2094 case WPA_GROUP_HANDSHAKE:
2100 task->scanning = TRUE;
2102 err = initiate_scan(task);
2104 if (err == -EINPROGRESS)
2107 task->scanning = FALSE;
2111 connman_device_set_scanning(task->device, TRUE);
2116 int supplicant_connect(struct connman_network *network)
2118 struct supplicant_task *task;
2121 DBG("network %p", network);
2123 index = connman_network_get_index(network);
2125 task = find_task_by_index(index);
2129 if (task->disconnecting == TRUE)
2130 task->pending_network = connman_network_ref(network);
2132 task->network = connman_network_ref(network);
2133 return task_connect(task);
2136 return -EINPROGRESS;
2139 int supplicant_disconnect(struct connman_network *network)
2141 struct supplicant_task *task;
2144 DBG("network %p", network);
2146 index = connman_network_get_index(network);
2148 task = find_task_by_index(index);
2152 if (task->disconnecting == TRUE)
2155 remove_network(task);
2157 disconnect_network(task);
2159 task->disconnecting = TRUE;
2164 static void supplicant_activate(DBusConnection *conn)
2166 DBusMessage *message;
2168 DBG("conn %p", conn);
2170 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
2171 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
2172 if (message == NULL)
2175 dbus_message_set_no_reply(message, TRUE);
2177 dbus_connection_send(conn, message, NULL);
2179 dbus_message_unref(message);
2182 static GSList *driver_list = NULL;
2184 static void supplicant_probe(DBusConnection *conn, void *user_data)
2188 DBG("conn %p", conn);
2190 for (list = driver_list; list; list = list->next) {
2191 struct supplicant_driver *driver = list->data;
2193 DBG("driver %p name %s", driver, driver->name);
2200 static void supplicant_remove(DBusConnection *conn, void *user_data)
2204 DBG("conn %p", conn);
2206 for (list = driver_list; list; list = list->next) {
2207 struct supplicant_driver *driver = list->data;
2209 DBG("driver %p name %s", driver, driver->name);
2217 static guint iface_watch;
2219 static int supplicant_create(void)
2221 if (g_slist_length(driver_list) > 0)
2224 connection = connman_dbus_get_connection();
2225 if (connection == NULL)
2228 DBG("connection %p", connection);
2230 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2231 supplicant_probe, supplicant_remove, NULL, NULL);
2233 iface_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2234 SUPPLICANT_INTF ".Interface",
2235 NULL, supplicant_filter,
2238 if (watch == 0 || iface_watch == 0) {
2239 g_dbus_remove_watch(connection, watch);
2240 g_dbus_remove_watch(connection, iface_watch);
2247 static void supplicant_destroy(void)
2249 if (g_slist_length(driver_list) > 0)
2252 DBG("connection %p", connection);
2254 g_dbus_remove_watch(connection, watch);
2255 g_dbus_remove_watch(connection, iface_watch);
2257 dbus_connection_unref(connection);
2261 int supplicant_register(struct supplicant_driver *driver)
2265 DBG("driver %p name %s", driver, driver->name);
2267 err = supplicant_create();
2271 driver_list = g_slist_append(driver_list, driver);
2273 supplicant_activate(connection);
2278 void supplicant_unregister(struct supplicant_driver *driver)
2280 DBG("driver %p name %s", driver, driver->name);
2282 supplicant_remove(connection, NULL);
2284 driver_list = g_slist_remove(driver_list, driver);
2286 supplicant_destroy();