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);
513 if (task->created == FALSE) {
514 connman_device_set_powered(task->device, FALSE);
519 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
520 SUPPLICANT_INTF, "removeInterface");
524 dbus_message_set_auto_start(message, FALSE);
526 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
529 if (dbus_connection_send_with_reply(connection, message,
530 &call, TIMEOUT) == FALSE) {
531 connman_error("Failed to remove interface");
532 dbus_message_unref(message);
537 connman_error("D-Bus connection not available");
538 dbus_message_unref(message);
542 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
544 dbus_message_unref(message);
549 static int set_ap_scan(struct supplicant_task *task)
551 DBusMessage *message, *reply;
555 DBG("task %p", task);
557 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
558 SUPPLICANT_INTF ".Interface", "setAPScan");
562 dbus_message_set_auto_start(message, FALSE);
564 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
567 dbus_error_init(&error);
569 reply = dbus_connection_send_with_reply_and_block(connection,
570 message, -1, &error);
572 if (dbus_error_is_set(&error) == TRUE) {
573 connman_error("%s", error.message);
574 dbus_error_free(&error);
576 connman_error("Failed to set AP scan");
577 dbus_message_unref(message);
581 dbus_message_unref(message);
583 dbus_message_unref(reply);
588 static int add_network(struct supplicant_task *task)
590 DBusMessage *message, *reply;
594 DBG("task %p", task);
596 if (task->netpath != NULL)
599 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
600 SUPPLICANT_INTF ".Interface", "addNetwork");
604 dbus_message_set_auto_start(message, FALSE);
606 dbus_error_init(&error);
608 reply = dbus_connection_send_with_reply_and_block(connection,
609 message, -1, &error);
611 if (dbus_error_is_set(&error) == TRUE) {
612 connman_error("%s", error.message);
613 dbus_error_free(&error);
615 connman_error("Failed to add network");
616 dbus_message_unref(message);
620 dbus_message_unref(message);
622 dbus_error_init(&error);
624 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
625 DBUS_TYPE_INVALID) == FALSE) {
626 if (dbus_error_is_set(&error) == TRUE) {
627 connman_error("%s", error.message);
628 dbus_error_free(&error);
630 connman_error("Wrong arguments for network");
631 dbus_message_unref(reply);
635 DBG("path %s", path);
637 task->netpath = g_strdup(path);
639 dbus_message_unref(reply);
644 static int remove_network(struct supplicant_task *task)
646 DBusMessage *message, *reply;
649 DBG("task %p", task);
651 if (task->netpath == NULL)
654 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
655 SUPPLICANT_INTF ".Interface", "removeNetwork");
659 dbus_message_set_auto_start(message, FALSE);
661 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
664 dbus_error_init(&error);
666 reply = dbus_connection_send_with_reply_and_block(connection,
667 message, -1, &error);
669 if (dbus_error_is_set(&error) == TRUE) {
670 connman_error("%s", error.message);
671 dbus_error_free(&error);
673 connman_error("Failed to remove network");
674 dbus_message_unref(message);
678 dbus_message_unref(message);
680 dbus_message_unref(reply);
682 g_free(task->netpath);
683 task->netpath = NULL;
688 static int select_network(struct supplicant_task *task)
690 DBusMessage *message, *reply;
693 DBG("task %p", task);
695 if (task->netpath == NULL)
698 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
699 SUPPLICANT_INTF ".Interface", "selectNetwork");
703 dbus_message_set_auto_start(message, FALSE);
705 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
708 dbus_error_init(&error);
710 reply = dbus_connection_send_with_reply_and_block(connection,
711 message, -1, &error);
713 if (dbus_error_is_set(&error) == TRUE) {
714 connman_error("%s", error.message);
715 dbus_error_free(&error);
717 connman_error("Failed to select network");
718 dbus_message_unref(message);
722 dbus_message_unref(message);
724 dbus_message_unref(reply);
729 static int disconnect_network(struct supplicant_task *task)
731 DBusMessage *message, *reply;
734 DBG("task %p", task);
736 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
737 SUPPLICANT_INTF ".Interface", "disconnect");
741 dbus_message_set_auto_start(message, FALSE);
743 dbus_error_init(&error);
745 reply = dbus_connection_send_with_reply_and_block(connection,
746 message, -1, &error);
748 if (dbus_error_is_set(&error) == TRUE) {
749 connman_error("%s", error.message);
750 dbus_error_free(&error);
752 connman_error("Failed to disconnect network");
753 dbus_message_unref(message);
757 dbus_message_unref(message);
759 dbus_message_unref(reply);
764 static int set_network_tls(struct connman_network *network,
765 DBusMessageIter *dict)
767 const char *private_key, *client_cert, *ca_cert;
768 const char *private_key_password;
771 * For TLS, we at least need a key, the client cert,
773 * Server cert is optional.
775 client_cert = connman_network_get_string(network,
776 "WiFi.ClientCertFile");
777 if (client_cert == NULL)
780 private_key = connman_network_get_string(network,
781 "WiFi.PrivateKeyFile");
782 if (private_key == NULL)
785 private_key_password = connman_network_get_string(network,
786 "WiFi.PrivateKeyPassphrase");
787 if (private_key_password == NULL)
790 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
792 connman_dbus_dict_append_basic(dict, "ca_cert",
793 DBUS_TYPE_STRING, &ca_cert);
795 DBG("client cert %s private key %s", client_cert, private_key);
797 connman_dbus_dict_append_basic(dict, "private_key",
798 DBUS_TYPE_STRING, &private_key);
799 connman_dbus_dict_append_basic(dict, "private_key_passwd",
801 &private_key_password);
802 connman_dbus_dict_append_basic(dict, "client_cert",
803 DBUS_TYPE_STRING, &client_cert);
808 static int set_network_peap(struct connman_network *network,
809 DBusMessageIter *dict, const char *passphrase)
811 const char *client_cert, *ca_cert, *phase2;
815 * For PEAP, we at least need the sever cert, a 2nd
816 * phase authentication and a passhprase.
817 * Client cert is optional although strongly required
818 * When setting the client cert, we then need a private
821 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
825 phase2 = connman_network_get_string(network, "WiFi.Phase2");
829 DBG("CA cert %s phase2 auth %s", ca_cert, phase2);
831 client_cert = connman_network_get_string(network,
832 "WiFi.ClientCertFile");
834 const char *private_key, *private_key_password;
836 private_key = connman_network_get_string(network,
837 "WiFi.PrivateKeyFile");
838 if (private_key == NULL)
841 private_key_password =
842 connman_network_get_string(network,
843 "WiFi.PrivateKeyPassphrase");
844 if (private_key_password == NULL)
847 connman_dbus_dict_append_basic(dict, "client_cert",
848 DBUS_TYPE_STRING, &client_cert);
850 connman_dbus_dict_append_basic(dict, "private_key",
851 DBUS_TYPE_STRING, &private_key);
853 connman_dbus_dict_append_basic(dict, "private_key_passwd",
855 &private_key_password);
857 DBG("client cert %s private key %s", client_cert, private_key);
860 phase2_auth = g_strdup_printf("\"auth=%s\"", phase2);
862 connman_dbus_dict_append_basic(dict, "password",
863 DBUS_TYPE_STRING, &passphrase);
865 connman_dbus_dict_append_basic(dict, "ca_cert",
866 DBUS_TYPE_STRING, &ca_cert);
868 connman_dbus_dict_append_basic(dict, "phase2",
869 DBUS_TYPE_STRING, &phase2_auth);
876 static int set_network(struct supplicant_task *task,
877 const unsigned char *network, int len,
878 const char *address, const char *security,
879 const char *passphrase)
881 DBusMessage *message, *reply;
882 DBusMessageIter array, dict;
884 dbus_uint32_t scan_ssid = 1;
886 DBG("task %p", task);
888 if (task->netpath == NULL)
891 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
892 SUPPLICANT_INTF ".Network", "set");
896 dbus_message_set_auto_start(message, FALSE);
898 dbus_message_iter_init_append(message, &array);
900 connman_dbus_dict_open(&array, &dict);
902 connman_dbus_dict_append_basic(&dict, "scan_ssid",
903 DBUS_TYPE_UINT32, &scan_ssid);
906 connman_dbus_dict_append_fixed_array(&dict, "ssid",
907 DBUS_TYPE_BYTE, &network, len);
909 connman_dbus_dict_append_basic(&dict, "bssid",
910 DBUS_TYPE_STRING, &address);
912 if (g_ascii_strcasecmp(security, "psk") == 0 ||
913 g_ascii_strcasecmp(security, "wpa") == 0 ||
914 g_ascii_strcasecmp(security, "rsn") == 0) {
915 const char *key_mgmt = "WPA-PSK";
916 connman_dbus_dict_append_basic(&dict, "key_mgmt",
917 DBUS_TYPE_STRING, &key_mgmt);
919 if (passphrase && strlen(passphrase) > 0)
920 connman_dbus_dict_append_basic(&dict, "psk",
921 DBUS_TYPE_STRING, &passphrase);
922 } else if (g_ascii_strcasecmp(security, "ieee8021x") == 0) {
923 struct connman_network *network = task->network;
924 const char *key_mgmt = "WPA-EAP", *eap, *identity;
928 * If our private key password is unset,
929 * we use the supplied passphrase. That is needed
930 * for PEAP where 2 passphrases (identity and client
931 * cert may have to be provided.
933 if (connman_network_get_string(network,
934 "WiFi.PrivateKeyPassphrase") == NULL)
935 connman_network_set_string(network,
936 "WiFi.PrivateKeyPassphrase",
939 eap = connman_network_get_string(network, "WiFi.EAP");
943 /* We must have an identity for both PEAP and TLS */
944 identity = connman_network_get_string(network, "WiFi.Identity");
945 if (identity == NULL)
948 DBG("key_mgmt %s eap %s identity %s", key_mgmt, eap, identity);
950 if (g_strcmp0(eap, "tls") == 0) {
953 err = set_network_tls(network, &dict);
955 dbus_message_unref(message);
958 } else if (g_strcmp0(eap, "peap") == 0) {
961 err = set_network_peap(network, &dict, passphrase);
963 dbus_message_unref(message);
967 connman_error("Unknown EAP %s", eap);
971 /* wpa_supplicant only accepts upper case EAPs */
972 eap_value = g_ascii_strup(eap, -1);
974 connman_dbus_dict_append_basic(&dict, "key_mgmt",
977 connman_dbus_dict_append_basic(&dict, "eap",
980 connman_dbus_dict_append_basic(&dict, "identity",
986 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
987 const char *key_mgmt = "NONE";
988 const char *auth_alg = "OPEN";
989 const char *key_index = "0";
991 if (task->mac80211 == TRUE)
992 auth_alg = "OPEN SHARED";
994 connman_dbus_dict_append_basic(&dict, "auth_alg",
995 DBUS_TYPE_STRING, &auth_alg);
997 connman_dbus_dict_append_basic(&dict, "key_mgmt",
998 DBUS_TYPE_STRING, &key_mgmt);
1001 int size = strlen(passphrase);
1002 if (size == 10 || size == 26) {
1003 unsigned char *key = malloc(13);
1006 memset(tmp, 0, sizeof(tmp));
1009 for (i = 0; i < size / 2; i++) {
1010 memcpy(tmp, passphrase + (i * 2), 2);
1011 key[i] = (unsigned char) strtol(tmp,
1014 connman_dbus_dict_append_fixed_array(&dict,
1015 "wep_key0", DBUS_TYPE_BYTE,
1019 connman_dbus_dict_append_basic(&dict,
1020 "wep_key0", DBUS_TYPE_STRING,
1023 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
1024 DBUS_TYPE_STRING, &key_index);
1027 const char *key_mgmt = "NONE";
1028 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1029 DBUS_TYPE_STRING, &key_mgmt);
1032 connman_dbus_dict_close(&array, &dict);
1034 dbus_error_init(&error);
1036 reply = dbus_connection_send_with_reply_and_block(connection,
1037 message, -1, &error);
1038 if (reply == NULL) {
1039 if (dbus_error_is_set(&error) == TRUE) {
1040 connman_error("%s", error.message);
1041 dbus_error_free(&error);
1043 connman_error("Failed to set network options");
1044 dbus_message_unref(message);
1048 dbus_message_unref(message);
1050 dbus_message_unref(reply);
1055 dbus_message_unref(message);
1059 static void scan_reply(DBusPendingCall *call, void *user_data)
1061 struct supplicant_task *task = user_data;
1064 DBG("task %p", task);
1066 task->scan_call = NULL;
1068 reply = dbus_pending_call_steal_reply(call);
1070 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1071 connman_device_set_scanning(task->device, FALSE);
1075 if (task->scanning == TRUE)
1076 connman_device_set_scanning(task->device, TRUE);
1079 dbus_message_unref(reply);
1081 dbus_pending_call_unref(call);
1085 static int initiate_scan(struct supplicant_task *task)
1087 DBusMessage *message;
1089 DBG("task %p", task);
1091 if (task->path == NULL)
1094 if (task->scan_call != NULL)
1097 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1098 SUPPLICANT_INTF ".Interface", "scan");
1099 if (message == NULL)
1102 dbus_message_set_auto_start(message, FALSE);
1104 if (dbus_connection_send_with_reply(connection, message,
1105 &task->scan_call, TIMEOUT) == FALSE) {
1106 connman_error("Failed to initiate scan");
1107 dbus_message_unref(message);
1111 if (task->scan_call == NULL) {
1112 connman_error("D-Bus connection not available");
1113 dbus_message_unref(message);
1117 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
1119 dbus_message_unref(message);
1121 return -EINPROGRESS;
1127 } special_ssid[] = {
1128 { "<hidden>", "hidden" },
1129 { "default", "linksys" },
1137 { "comcomcom", "3com" },
1139 { "Symbol", "symbol" },
1140 { "Motorola", "motorola" },
1141 { "Wireless" , "wireless" },
1146 static char *build_group(const char *addr, const char *name,
1147 const unsigned char *ssid, unsigned int ssid_len,
1148 const char *mode, const char *security)
1156 str = g_string_sized_new((ssid_len * 2) + 24);
1161 g_string_append_printf(str, "hidden_%s", addr);
1165 for (i = 0; special_ssid[i].name; i++) {
1166 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1167 if (special_ssid[i].value == NULL)
1168 g_string_append_printf(str, "%s_%s",
1171 g_string_append_printf(str, "%s_%s",
1172 special_ssid[i].value, addr);
1177 if (ssid_len > 0 && ssid[0] != '\0') {
1178 for (i = 0; i < ssid_len; i++)
1179 g_string_append_printf(str, "%02x", ssid[i]);
1181 g_string_append_printf(str, "hidden_%s", addr);
1184 g_string_append_printf(str, "_%s_%s", mode, security);
1186 return g_string_free(str, FALSE);
1189 static void extract_addr(DBusMessageIter *value,
1190 struct supplicant_result *result)
1192 DBusMessageIter array;
1193 struct ether_addr eth;
1194 unsigned char *addr;
1197 dbus_message_iter_recurse(value, &array);
1198 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1203 result->addr = g_try_malloc(addr_len);
1204 if (result->addr == NULL)
1207 memcpy(result->addr, addr, addr_len);
1208 result->addr_len = addr_len;
1210 result->path = g_try_malloc0(13);
1211 if (result->path == NULL)
1214 memcpy(ð, addr, sizeof(eth));
1215 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1216 eth.ether_addr_octet[0],
1217 eth.ether_addr_octet[1],
1218 eth.ether_addr_octet[2],
1219 eth.ether_addr_octet[3],
1220 eth.ether_addr_octet[4],
1221 eth.ether_addr_octet[5]);
1224 static void extract_ssid(DBusMessageIter *value,
1225 struct supplicant_result *result)
1227 DBusMessageIter array;
1228 unsigned char *ssid;
1231 dbus_message_iter_recurse(value, &array);
1232 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1237 if (ssid[0] == '\0')
1240 result->ssid = g_try_malloc(ssid_len);
1241 if (result->ssid == NULL)
1244 memcpy(result->ssid, ssid, ssid_len);
1245 result->ssid_len = ssid_len;
1247 result->name = g_try_malloc0(ssid_len + 1);
1248 if (result->name == NULL)
1251 for (i = 0; i < ssid_len; i++) {
1252 if (g_ascii_isprint(ssid[i]))
1253 result->name[i] = ssid[i];
1255 result->name[i] = ' ';
1259 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1260 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1262 static void extract_rsn(struct supplicant_result *result,
1263 const unsigned char *buf, int len)
1282 /* Pairwise cipher */
1286 count = buf[0] | (buf[1] << 8);
1287 if (2 + (count * 4) > len)
1290 buf += 2 + (count * 4);
1291 len -= 2 + (count * 4);
1293 /* Authentication */
1297 count = buf[0] | (buf[1] << 8);
1298 if (2 + (count * 4) > len)
1301 for (i = 0; i < count; i++) {
1302 const unsigned char *ptr = buf + 2 + (i * 4);
1304 if (memcmp(ptr, wifi_oui, 3) == 0) {
1307 result->has_8021x = TRUE;
1310 result->has_psk = TRUE;
1313 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1316 result->has_8021x = TRUE;
1319 result->has_psk = TRUE;
1325 buf += 2 + (count * 4);
1326 len -= 2 + (count * 4);
1329 static void extract_wpaie(DBusMessageIter *value,
1330 struct supplicant_result *result)
1332 DBusMessageIter array;
1336 dbus_message_iter_recurse(value, &array);
1337 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1340 result->has_wpa = TRUE;
1341 extract_rsn(result, ie + 6, ie_len - 6);
1345 static void extract_rsnie(DBusMessageIter *value,
1346 struct supplicant_result *result)
1348 DBusMessageIter array;
1352 dbus_message_iter_recurse(value, &array);
1353 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1356 result->has_rsn = TRUE;
1357 extract_rsn(result, ie + 2, ie_len - 2);
1361 static void extract_wpsie(DBusMessageIter *value,
1362 struct supplicant_result *result)
1364 DBusMessageIter array;
1368 dbus_message_iter_recurse(value, &array);
1369 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1372 result->has_wps = TRUE;
1375 static void extract_capabilites(DBusMessageIter *value,
1376 struct supplicant_result *result)
1378 dbus_message_iter_get_basic(value, &result->capabilities);
1380 if (result->capabilities & IEEE80211_CAP_ESS)
1381 result->adhoc = FALSE;
1382 else if (result->capabilities & IEEE80211_CAP_IBSS)
1383 result->adhoc = TRUE;
1385 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1386 result->has_wep = TRUE;
1389 static unsigned char calculate_strength(struct supplicant_task *task,
1390 struct supplicant_result *result)
1392 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1393 unsigned char strength;
1395 if (result->level > 0)
1396 strength = 100 - result->level;
1398 strength = 120 + result->level;
1406 return (result->quality * 100) / task->range->max_qual.qual;
1409 static unsigned short calculate_channel(struct supplicant_result *result)
1411 if (result->frequency < 0)
1414 return (result->frequency - 2407) / 5;
1417 static void get_properties(struct supplicant_task *task);
1419 static void properties_reply(DBusPendingCall *call, void *user_data)
1421 struct supplicant_task *task = user_data;
1422 struct supplicant_result result;
1423 struct connman_network *network;
1425 DBusMessageIter array, dict;
1426 unsigned char strength;
1427 unsigned short channel, frequency;
1428 const char *mode, *security;
1431 DBG("task %p", task);
1433 reply = dbus_pending_call_steal_reply(call);
1435 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1438 memset(&result, 0, sizeof(result));
1439 result.frequency = -1;
1440 result.quality = -1;
1444 dbus_message_iter_init(reply, &array);
1446 dbus_message_iter_recurse(&array, &dict);
1448 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1449 DBusMessageIter entry, value;
1452 dbus_message_iter_recurse(&dict, &entry);
1453 dbus_message_iter_get_basic(&entry, &key);
1455 dbus_message_iter_next(&entry);
1457 dbus_message_iter_recurse(&entry, &value);
1459 //type = dbus_message_iter_get_arg_type(&value);
1460 //dbus_message_iter_get_basic(&value, &val);
1468 * frequency : i (105)
1469 * capabilities : q (113)
1476 if (g_str_equal(key, "bssid") == TRUE)
1477 extract_addr(&value, &result);
1478 else if (g_str_equal(key, "ssid") == TRUE)
1479 extract_ssid(&value, &result);
1480 else if (g_str_equal(key, "wpaie") == TRUE)
1481 extract_wpaie(&value, &result);
1482 else if (g_str_equal(key, "rsnie") == TRUE)
1483 extract_rsnie(&value, &result);
1484 else if (g_str_equal(key, "wpsie") == TRUE)
1485 extract_wpsie(&value, &result);
1486 else if (g_str_equal(key, "capabilities") == TRUE)
1487 extract_capabilites(&value, &result);
1488 else if (g_str_equal(key, "frequency") == TRUE)
1489 dbus_message_iter_get_basic(&value, &result.frequency);
1490 else if (g_str_equal(key, "quality") == TRUE)
1491 dbus_message_iter_get_basic(&value, &result.quality);
1492 else if (g_str_equal(key, "noise") == TRUE)
1493 dbus_message_iter_get_basic(&value, &result.noise);
1494 else if (g_str_equal(key, "level") == TRUE)
1495 dbus_message_iter_get_basic(&value, &result.level);
1496 else if (g_str_equal(key, "maxrate") == TRUE)
1497 dbus_message_iter_get_basic(&value, &result.maxrate);
1499 dbus_message_iter_next(&dict);
1502 DBG("capabilties %u frequency %d "
1503 "quality %d noise %d level %d maxrate %d",
1504 result.capabilities, result.frequency,
1505 result.quality, result.noise,
1506 result.level, result.maxrate);
1508 if (result.path == NULL)
1511 if (result.path[0] == '\0')
1514 if (result.frequency > 0 && result.frequency < 14)
1515 result.frequency = 2407 + (5 * result.frequency);
1516 else if (result.frequency == 14)
1517 result.frequency = 2484;
1519 strength = calculate_strength(task, &result);
1520 channel = calculate_channel(&result);
1522 frequency = (result.frequency < 0) ? 0 : result.frequency;
1524 if (result.has_8021x == TRUE)
1525 security = "ieee8021x";
1526 else if (result.has_psk == TRUE)
1528 else if (result.has_wep == TRUE)
1533 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1535 group = build_group(result.path, result.name,
1536 result.ssid, result.ssid_len,
1539 if (result.has_psk == TRUE) {
1540 if (result.has_rsn == TRUE)
1542 else if (result.has_wpa == TRUE)
1546 network = connman_device_get_network(task->device, result.path);
1547 if (network == NULL) {
1550 network = connman_network_create(result.path,
1551 CONNMAN_NETWORK_TYPE_WIFI);
1552 if (network == NULL)
1555 index = connman_device_get_index(task->device);
1556 connman_network_set_index(network, index);
1558 connman_network_set_protocol(network,
1559 CONNMAN_NETWORK_PROTOCOL_IP);
1561 connman_network_set_address(network, result.addr,
1564 if (connman_device_add_network(task->device, network) < 0) {
1565 connman_network_unref(network);
1570 if (result.name != NULL && result.name[0] != '\0')
1571 connman_network_set_name(network, result.name);
1573 connman_network_set_blob(network, "WiFi.SSID",
1574 result.ssid, result.ssid_len);
1576 connman_network_set_string(network, "WiFi.Mode", mode);
1578 DBG("%s (%s %s) strength %d (%s)",
1579 result.name, mode, security, strength,
1580 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1582 connman_network_set_available(network, TRUE);
1583 connman_network_set_strength(network, strength);
1585 connman_network_set_uint16(network, "Frequency", frequency);
1586 connman_network_set_uint16(network, "WiFi.Channel", channel);
1587 connman_network_set_string(network, "WiFi.Security", security);
1589 if (result.ssid != NULL)
1590 connman_network_set_group(network, group);
1595 g_free(result.path);
1596 g_free(result.addr);
1597 g_free(result.name);
1598 g_free(result.ssid);
1601 dbus_message_unref(reply);
1603 dbus_pending_call_unref(call);
1605 get_properties(task);
1608 static void get_properties(struct supplicant_task *task)
1610 DBusMessage *message;
1613 path = g_slist_nth_data(task->scan_results, 0);
1617 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1618 SUPPLICANT_INTF ".BSSID",
1621 task->scan_results = g_slist_remove(task->scan_results, path);
1624 if (message == NULL)
1627 dbus_message_set_auto_start(message, FALSE);
1629 if (dbus_connection_send_with_reply(connection, message,
1630 &task->result_call, TIMEOUT) == FALSE) {
1631 connman_error("Failed to get network properties");
1632 dbus_message_unref(message);
1636 if (task->result_call == NULL) {
1637 connman_error("D-Bus connection not available");
1638 dbus_message_unref(message);
1642 dbus_pending_call_set_notify(task->result_call,
1643 properties_reply, task, NULL);
1645 dbus_message_unref(message);
1650 task->result_call = NULL;
1652 if (task->scanning == TRUE) {
1653 connman_device_set_scanning(task->device, FALSE);
1654 task->scanning = FALSE;
1658 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1660 struct supplicant_task *task = user_data;
1666 DBG("task %p", task);
1668 reply = dbus_pending_call_steal_reply(call);
1670 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1673 dbus_error_init(&error);
1675 if (dbus_message_get_args(reply, &error,
1676 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1677 &results, &num_results,
1678 DBUS_TYPE_INVALID) == FALSE) {
1679 if (dbus_error_is_set(&error) == TRUE) {
1680 connman_error("%s", error.message);
1681 dbus_error_free(&error);
1683 connman_error("Wrong arguments for scan result");
1687 if (num_results == 0)
1690 for (i = 0; i < num_results; i++) {
1691 char *path = g_strdup(results[i]);
1695 task->scan_results = g_slist_append(task->scan_results, path);
1698 g_strfreev(results);
1700 dbus_message_unref(reply);
1702 dbus_pending_call_unref(call);
1704 get_properties(task);
1709 dbus_message_unref(reply);
1711 dbus_pending_call_unref(call);
1713 task->result_call = NULL;
1715 if (task->scanning == TRUE) {
1716 connman_device_set_scanning(task->device, FALSE);
1717 task->scanning = FALSE;
1721 static void scan_results_available(struct supplicant_task *task)
1723 DBusMessage *message;
1725 DBG("task %p", task);
1727 if (task->result_call != NULL)
1730 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1731 SUPPLICANT_INTF ".Interface",
1733 if (message == NULL)
1736 dbus_message_set_auto_start(message, FALSE);
1738 if (dbus_connection_send_with_reply(connection, message,
1739 &task->result_call, TIMEOUT) == FALSE) {
1740 connman_error("Failed to request scan result");
1744 if (task->result_call == NULL) {
1745 connman_error("D-Bus connection not available");
1749 if (task->scanning == TRUE)
1750 connman_device_set_scanning(task->device, TRUE);
1752 dbus_pending_call_set_notify(task->result_call,
1753 scan_results_reply, task, NULL);
1756 dbus_message_unref(message);
1759 static enum supplicant_state string2state(const char *state)
1761 if (g_str_equal(state, "INACTIVE") == TRUE)
1762 return WPA_INACTIVE;
1763 else if (g_str_equal(state, "SCANNING") == TRUE)
1764 return WPA_SCANNING;
1765 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1766 return WPA_ASSOCIATING;
1767 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1768 return WPA_ASSOCIATED;
1769 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1770 return WPA_GROUP_HANDSHAKE;
1771 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1772 return WPA_4WAY_HANDSHAKE;
1773 else if (g_str_equal(state, "COMPLETED") == TRUE)
1774 return WPA_COMPLETED;
1775 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1776 return WPA_DISCONNECTED;
1781 static int task_connect(struct supplicant_task *task)
1783 const char *address, *security, *passphrase;
1785 unsigned int ssid_len;
1788 connman_inet_ifup(task->ifindex);
1790 address = connman_network_get_string(task->network, "Address");
1791 security = connman_network_get_string(task->network, "WiFi.Security");
1792 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1794 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1796 DBG("address %s security %s", address, security);
1798 if (security == NULL)
1801 if (passphrase == NULL && g_str_equal(security, "none") == FALSE &&
1802 g_str_equal(security, "ieee8021x") == FALSE)
1805 remove_network(task);
1811 err = set_network(task, ssid, ssid_len, address, security, passphrase);
1815 err = select_network(task);
1819 return -EINPROGRESS;
1822 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1825 dbus_bool_t scanning;
1827 dbus_error_init(&error);
1829 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1830 DBUS_TYPE_INVALID) == FALSE) {
1831 if (dbus_error_is_set(&error) == TRUE) {
1832 connman_error("%s", error.message);
1833 dbus_error_free(&error);
1835 connman_error("Wrong arguments for scanning");
1839 connman_info("%s scanning %s", task->ifname,
1840 scanning == TRUE ? "started" : "finished");
1843 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1846 const char *newstate, *oldstate;
1847 unsigned char bssid[ETH_ALEN];
1848 unsigned int bssid_len;
1849 enum supplicant_state state, prevstate;
1851 dbus_error_init(&error);
1853 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1854 DBUS_TYPE_STRING, &oldstate,
1855 DBUS_TYPE_INVALID) == FALSE) {
1856 if (dbus_error_is_set(&error) == TRUE) {
1857 connman_error("%s", error.message);
1858 dbus_error_free(&error);
1860 connman_error("Wrong arguments for state change");
1864 DBG("state %s ==> %s", oldstate, newstate);
1866 connman_info("%s %s%s", task->ifname, newstate,
1867 task->scanning == TRUE ? " (scanning)" : "");
1869 state = string2state(newstate);
1870 if (state == WPA_INVALID)
1873 if (task->scanning == TRUE && state != WPA_SCANNING) {
1874 connman_device_cleanup_scanning(task->device);
1875 task->scanning = FALSE;
1878 prevstate = task->state;
1879 task->state = state;
1881 if (task->network == NULL)
1884 switch (task->state) {
1886 switch (prevstate) {
1887 case WPA_ASSOCIATED:
1888 case WPA_GROUP_HANDSHAKE:
1894 /* reset scan trigger and schedule background scan */
1895 connman_device_schedule_scan(task->device);
1897 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1898 connman_network_set_address(task->network,
1902 connman_network_set_method(task->network,
1903 CONNMAN_IPCONFIG_METHOD_DHCP);
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->mac80211 = connman_inet_is_mac80211(task->ifindex);
2008 if (task->mac80211 == 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();