5 * Copyright (C) 2007-2009 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);
319 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
322 dbus_error_init(&error);
324 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
325 DBUS_TYPE_INVALID) == FALSE) {
326 if (dbus_error_is_set(&error) == TRUE) {
327 connman_error("%s", error.message);
328 dbus_error_free(&error);
330 connman_error("Wrong arguments for add interface");
334 DBG("path %s", path);
336 task->path = g_strdup(path);
337 task->created = TRUE;
339 connman_device_set_powered(task->device, TRUE);
341 dbus_message_unref(reply);
346 dbus_message_unref(reply);
348 task_list = g_slist_remove(task_list, task);
350 connman_device_unref(task->device);
355 static int add_interface(struct supplicant_task *task)
357 const char *driver = connman_option_get_string("wifi");
358 DBusMessage *message;
359 DBusMessageIter array, dict;
360 DBusPendingCall *call;
362 DBG("task %p", task);
364 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
365 SUPPLICANT_INTF, "addInterface");
369 dbus_message_set_auto_start(message, FALSE);
371 dbus_message_iter_init_append(message, &array);
373 dbus_message_iter_append_basic(&array,
374 DBUS_TYPE_STRING, &task->ifname);
376 connman_dbus_dict_open(&array, &dict);
378 connman_dbus_dict_append_basic(&dict, "driver",
379 DBUS_TYPE_STRING, &driver);
381 connman_dbus_dict_close(&array, &dict);
383 if (dbus_connection_send_with_reply(connection, message,
384 &call, TIMEOUT) == FALSE) {
385 connman_error("Failed to add interface");
386 dbus_message_unref(message);
391 connman_error("D-Bus connection not available");
392 dbus_message_unref(message);
396 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
398 dbus_message_unref(message);
403 static void get_interface_reply(DBusPendingCall *call, void *user_data)
405 struct supplicant_task *task = user_data;
410 DBG("task %p", task);
412 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);
444 static int create_interface(struct supplicant_task *task)
446 DBusMessage *message;
447 DBusPendingCall *call;
449 DBG("task %p", task);
451 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
452 SUPPLICANT_INTF, "getInterface");
456 dbus_message_set_auto_start(message, FALSE);
458 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
461 if (dbus_connection_send_with_reply(connection, message,
462 &call, TIMEOUT) == FALSE) {
463 connman_error("Failed to get interface");
464 dbus_message_unref(message);
469 connman_error("D-Bus connection not available");
470 dbus_message_unref(message);
474 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
476 dbus_message_unref(message);
481 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
483 struct supplicant_task *task = user_data;
486 DBG("task %p", task);
488 reply = dbus_pending_call_steal_reply(call);
490 connman_device_set_powered(task->device, FALSE);
492 connman_device_unref(task->device);
494 connman_inet_ifdown(task->ifindex);
498 dbus_message_unref(reply);
501 static int remove_interface(struct supplicant_task *task)
503 DBusMessage *message;
504 DBusPendingCall *call;
506 DBG("task %p", task);
509 if (task->created == FALSE) {
510 connman_device_set_powered(task->device, FALSE);
515 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
516 SUPPLICANT_INTF, "removeInterface");
520 dbus_message_set_auto_start(message, FALSE);
522 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
525 if (dbus_connection_send_with_reply(connection, message,
526 &call, TIMEOUT) == FALSE) {
527 connman_error("Failed to remove interface");
528 dbus_message_unref(message);
533 connman_error("D-Bus connection not available");
534 dbus_message_unref(message);
538 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
540 dbus_message_unref(message);
545 static int set_ap_scan(struct supplicant_task *task)
547 DBusMessage *message, *reply;
551 DBG("task %p", task);
553 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
554 SUPPLICANT_INTF ".Interface", "setAPScan");
558 dbus_message_set_auto_start(message, FALSE);
560 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
563 dbus_error_init(&error);
565 reply = dbus_connection_send_with_reply_and_block(connection,
566 message, -1, &error);
568 if (dbus_error_is_set(&error) == TRUE) {
569 connman_error("%s", error.message);
570 dbus_error_free(&error);
572 connman_error("Failed to set AP scan");
573 dbus_message_unref(message);
577 dbus_message_unref(message);
579 dbus_message_unref(reply);
584 static int add_network(struct supplicant_task *task)
586 DBusMessage *message, *reply;
590 DBG("task %p", task);
592 if (task->netpath != NULL)
595 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
596 SUPPLICANT_INTF ".Interface", "addNetwork");
600 dbus_message_set_auto_start(message, FALSE);
602 dbus_error_init(&error);
604 reply = dbus_connection_send_with_reply_and_block(connection,
605 message, -1, &error);
607 if (dbus_error_is_set(&error) == TRUE) {
608 connman_error("%s", error.message);
609 dbus_error_free(&error);
611 connman_error("Failed to add network");
612 dbus_message_unref(message);
616 dbus_message_unref(message);
618 dbus_error_init(&error);
620 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
621 DBUS_TYPE_INVALID) == FALSE) {
622 if (dbus_error_is_set(&error) == TRUE) {
623 connman_error("%s", error.message);
624 dbus_error_free(&error);
626 connman_error("Wrong arguments for network");
627 dbus_message_unref(reply);
631 DBG("path %s", path);
633 task->netpath = g_strdup(path);
635 dbus_message_unref(reply);
640 static int remove_network(struct supplicant_task *task)
642 DBusMessage *message, *reply;
645 DBG("task %p", task);
647 if (task->netpath == NULL)
650 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
651 SUPPLICANT_INTF ".Interface", "removeNetwork");
655 dbus_message_set_auto_start(message, FALSE);
657 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
660 dbus_error_init(&error);
662 reply = dbus_connection_send_with_reply_and_block(connection,
663 message, -1, &error);
665 if (dbus_error_is_set(&error) == TRUE) {
666 connman_error("%s", error.message);
667 dbus_error_free(&error);
669 connman_error("Failed to remove network");
670 dbus_message_unref(message);
674 dbus_message_unref(message);
676 dbus_message_unref(reply);
678 g_free(task->netpath);
679 task->netpath = NULL;
684 static int select_network(struct supplicant_task *task)
686 DBusMessage *message, *reply;
689 DBG("task %p", task);
691 if (task->netpath == NULL)
694 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
695 SUPPLICANT_INTF ".Interface", "selectNetwork");
699 dbus_message_set_auto_start(message, FALSE);
701 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
704 dbus_error_init(&error);
706 reply = dbus_connection_send_with_reply_and_block(connection,
707 message, -1, &error);
709 if (dbus_error_is_set(&error) == TRUE) {
710 connman_error("%s", error.message);
711 dbus_error_free(&error);
713 connman_error("Failed to select network");
714 dbus_message_unref(message);
718 dbus_message_unref(message);
720 dbus_message_unref(reply);
725 static int disconnect_network(struct supplicant_task *task)
727 DBusMessage *message, *reply;
730 DBG("task %p", task);
732 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
733 SUPPLICANT_INTF ".Interface", "disconnect");
737 dbus_message_set_auto_start(message, FALSE);
739 dbus_error_init(&error);
741 reply = dbus_connection_send_with_reply_and_block(connection,
742 message, -1, &error);
744 if (dbus_error_is_set(&error) == TRUE) {
745 connman_error("%s", error.message);
746 dbus_error_free(&error);
748 connman_error("Failed to disconnect network");
749 dbus_message_unref(message);
753 dbus_message_unref(message);
755 dbus_message_unref(reply);
760 static int set_network_tls(struct connman_network *network,
761 DBusMessageIter *dict)
763 const char *private_key, *client_cert, *ca_cert;
764 const char *private_key_password;
767 * For TLS, we at least need a key, the client cert,
769 * Server cert is optional.
771 client_cert = connman_network_get_string(network,
772 "WiFi.ClientCertFile");
773 if (client_cert == NULL)
776 private_key = connman_network_get_string(network,
777 "WiFi.PrivateKeyFile");
778 if (private_key == NULL)
781 private_key_password = connman_network_get_string(network,
782 "WiFi.PrivateKeyPassphrase");
783 if (private_key_password == NULL)
786 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
788 connman_dbus_dict_append_basic(dict, "ca_cert",
789 DBUS_TYPE_STRING, &ca_cert);
791 DBG("client cert %s private key %s", client_cert, private_key);
793 connman_dbus_dict_append_basic(dict, "private_key",
794 DBUS_TYPE_STRING, &private_key);
795 connman_dbus_dict_append_basic(dict, "private_key_passwd",
797 &private_key_password);
798 connman_dbus_dict_append_basic(dict, "client_cert",
799 DBUS_TYPE_STRING, &client_cert);
804 static int set_network_peap(struct connman_network *network,
805 DBusMessageIter *dict, const char *passphrase)
807 const char *client_cert, *ca_cert, *phase2;
811 * For PEAP, we at least need the sever cert, a 2nd
812 * phase authentication and a passhprase.
813 * Client cert is optional although strongly required
814 * When setting the client cert, we then need a private
817 ca_cert = connman_network_get_string(network, "WiFi.CACertFile");
821 phase2 = connman_network_get_string(network, "WiFi.Phase2");
825 DBG("CA cert %s phase2 auth %s", ca_cert, phase2);
827 client_cert = connman_network_get_string(network,
828 "WiFi.ClientCertFile");
830 const char *private_key, *private_key_password;
832 private_key = connman_network_get_string(network,
833 "WiFi.PrivateKeyFile");
834 if (private_key == NULL)
837 private_key_password =
838 connman_network_get_string(network,
839 "WiFi.PrivateKeyPassphrase");
840 if (private_key_password == NULL)
843 connman_dbus_dict_append_basic(dict, "client_cert",
844 DBUS_TYPE_STRING, &client_cert);
846 connman_dbus_dict_append_basic(dict, "private_key",
847 DBUS_TYPE_STRING, &private_key);
849 connman_dbus_dict_append_basic(dict, "private_key_passwd",
851 &private_key_password);
853 DBG("client cert %s private key %s", client_cert, private_key);
856 phase2_auth = g_strdup_printf("\"auth=%s\"", phase2);
858 connman_dbus_dict_append_basic(dict, "password",
859 DBUS_TYPE_STRING, &passphrase);
861 connman_dbus_dict_append_basic(dict, "ca_cert",
862 DBUS_TYPE_STRING, &ca_cert);
864 connman_dbus_dict_append_basic(dict, "phase2",
865 DBUS_TYPE_STRING, &phase2_auth);
872 static int set_network(struct supplicant_task *task,
873 const unsigned char *network, int len,
874 const char *address, const char *security,
875 const char *passphrase)
877 DBusMessage *message, *reply;
878 DBusMessageIter array, dict;
880 dbus_uint32_t scan_ssid = 1;
882 DBG("task %p", task);
884 if (task->netpath == NULL)
887 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
888 SUPPLICANT_INTF ".Network", "set");
892 dbus_message_set_auto_start(message, FALSE);
894 dbus_message_iter_init_append(message, &array);
896 connman_dbus_dict_open(&array, &dict);
898 connman_dbus_dict_append_basic(&dict, "scan_ssid",
899 DBUS_TYPE_UINT32, &scan_ssid);
902 connman_dbus_dict_append_fixed_array(&dict, "ssid",
903 DBUS_TYPE_BYTE, &network, len);
905 connman_dbus_dict_append_basic(&dict, "bssid",
906 DBUS_TYPE_STRING, &address);
908 if (g_ascii_strcasecmp(security, "psk") == 0 ||
909 g_ascii_strcasecmp(security, "wpa") == 0 ||
910 g_ascii_strcasecmp(security, "rsn") == 0) {
911 const char *key_mgmt = "WPA-PSK";
912 connman_dbus_dict_append_basic(&dict, "key_mgmt",
913 DBUS_TYPE_STRING, &key_mgmt);
915 if (passphrase && strlen(passphrase) > 0)
916 connman_dbus_dict_append_basic(&dict, "psk",
917 DBUS_TYPE_STRING, &passphrase);
918 } else if (g_ascii_strcasecmp(security, "ieee8021x") == 0) {
919 struct connman_network *network = task->network;
920 const char *key_mgmt = "WPA-EAP", *eap, *identity;
924 * If our private key password is unset,
925 * we use the supplied passphrase. That is needed
926 * for PEAP where 2 passphrases (identity and client
927 * cert may have to be provided.
929 if (connman_network_get_string(network,
930 "WiFi.PrivateKeyPassphrase") == NULL)
931 connman_network_set_string(network,
932 "WiFi.PrivateKeyPassphrase",
935 eap = connman_network_get_string(network, "WiFi.EAP");
939 /* We must have an identity for both PEAP and TLS */
940 identity = connman_network_get_string(network, "WiFi.Identity");
941 if (identity == NULL)
944 DBG("key_mgmt %s eap %s identity %s", key_mgmt, eap, identity);
946 if (g_strcmp0(eap, "tls") == 0) {
949 err = set_network_tls(network, &dict);
951 dbus_message_unref(message);
954 } else if (g_strcmp0(eap, "peap") == 0) {
957 err = set_network_peap(network, &dict, passphrase);
959 dbus_message_unref(message);
963 connman_error("Unknown EAP %s", eap);
967 /* wpa_supplicant only accepts upper case EAPs */
968 eap_value = g_ascii_strup(eap, -1);
970 connman_dbus_dict_append_basic(&dict, "key_mgmt",
973 connman_dbus_dict_append_basic(&dict, "eap",
976 connman_dbus_dict_append_basic(&dict, "identity",
982 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
983 const char *key_mgmt = "NONE";
984 const char *auth_alg = "OPEN";
985 const char *key_index = "0";
987 if (task->mac80211 == TRUE)
988 auth_alg = "OPEN SHARED";
990 connman_dbus_dict_append_basic(&dict, "auth_alg",
991 DBUS_TYPE_STRING, &auth_alg);
993 connman_dbus_dict_append_basic(&dict, "key_mgmt",
994 DBUS_TYPE_STRING, &key_mgmt);
997 int size = strlen(passphrase);
998 if (size == 10 || size == 26) {
999 unsigned char *key = malloc(13);
1002 memset(tmp, 0, sizeof(tmp));
1005 for (i = 0; i < size / 2; i++) {
1006 memcpy(tmp, passphrase + (i * 2), 2);
1007 key[i] = (unsigned char) strtol(tmp,
1010 connman_dbus_dict_append_fixed_array(&dict,
1011 "wep_key0", DBUS_TYPE_BYTE,
1015 connman_dbus_dict_append_basic(&dict,
1016 "wep_key0", DBUS_TYPE_STRING,
1019 connman_dbus_dict_append_basic(&dict, "wep_tx_keyidx",
1020 DBUS_TYPE_STRING, &key_index);
1023 const char *key_mgmt = "NONE";
1024 connman_dbus_dict_append_basic(&dict, "key_mgmt",
1025 DBUS_TYPE_STRING, &key_mgmt);
1028 connman_dbus_dict_close(&array, &dict);
1030 dbus_error_init(&error);
1032 reply = dbus_connection_send_with_reply_and_block(connection,
1033 message, -1, &error);
1034 if (reply == NULL) {
1035 if (dbus_error_is_set(&error) == TRUE) {
1036 connman_error("%s", error.message);
1037 dbus_error_free(&error);
1039 connman_error("Failed to set network options");
1040 dbus_message_unref(message);
1044 dbus_message_unref(message);
1046 dbus_message_unref(reply);
1051 dbus_message_unref(message);
1055 static void scan_reply(DBusPendingCall *call, void *user_data)
1057 struct supplicant_task *task = user_data;
1060 DBG("task %p", task);
1062 task->scan_call = NULL;
1064 reply = dbus_pending_call_steal_reply(call);
1068 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1069 connman_device_set_scanning(task->device, FALSE);
1073 if (task->scanning == TRUE)
1074 connman_device_set_scanning(task->device, TRUE);
1077 dbus_message_unref(reply);
1081 static int initiate_scan(struct supplicant_task *task)
1083 DBusMessage *message;
1085 DBG("task %p", task);
1087 if (task->path == NULL)
1090 if (task->scan_call != NULL)
1093 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1094 SUPPLICANT_INTF ".Interface", "scan");
1095 if (message == NULL)
1098 dbus_message_set_auto_start(message, FALSE);
1100 if (dbus_connection_send_with_reply(connection, message,
1101 &task->scan_call, TIMEOUT) == FALSE) {
1102 connman_error("Failed to initiate scan");
1103 dbus_message_unref(message);
1107 if (task->scan_call == NULL) {
1108 connman_error("D-Bus connection not available");
1109 dbus_message_unref(message);
1113 dbus_pending_call_set_notify(task->scan_call, scan_reply, task, NULL);
1115 dbus_message_unref(message);
1117 return -EINPROGRESS;
1123 } special_ssid[] = {
1124 { "<hidden>", "hidden" },
1125 { "default", "linksys" },
1133 { "comcomcom", "3com" },
1135 { "Symbol", "symbol" },
1136 { "Motorola", "motorola" },
1137 { "Wireless" , "wireless" },
1142 static char *build_group(const char *addr, const char *name,
1143 const unsigned char *ssid, unsigned int ssid_len,
1144 const char *mode, const char *security)
1152 str = g_string_sized_new((ssid_len * 2) + 24);
1157 g_string_append_printf(str, "hidden_%s", addr);
1161 for (i = 0; special_ssid[i].name; i++) {
1162 if (g_strcmp0(special_ssid[i].name, name) == 0) {
1163 if (special_ssid[i].value == NULL)
1164 g_string_append_printf(str, "%s_%s",
1167 g_string_append_printf(str, "%s_%s",
1168 special_ssid[i].value, addr);
1173 if (ssid_len > 0 && ssid[0] != '\0') {
1174 for (i = 0; i < ssid_len; i++)
1175 g_string_append_printf(str, "%02x", ssid[i]);
1177 g_string_append_printf(str, "hidden_%s", addr);
1180 g_string_append_printf(str, "_%s_%s", mode, security);
1182 return g_string_free(str, FALSE);
1185 static void extract_addr(DBusMessageIter *value,
1186 struct supplicant_result *result)
1188 DBusMessageIter array;
1189 struct ether_addr eth;
1190 unsigned char *addr;
1193 dbus_message_iter_recurse(value, &array);
1194 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1199 result->addr = g_try_malloc(addr_len);
1200 if (result->addr == NULL)
1203 memcpy(result->addr, addr, addr_len);
1204 result->addr_len = addr_len;
1206 result->path = g_try_malloc0(13);
1207 if (result->path == NULL)
1210 memcpy(ð, addr, sizeof(eth));
1211 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
1212 eth.ether_addr_octet[0],
1213 eth.ether_addr_octet[1],
1214 eth.ether_addr_octet[2],
1215 eth.ether_addr_octet[3],
1216 eth.ether_addr_octet[4],
1217 eth.ether_addr_octet[5]);
1220 static void extract_ssid(DBusMessageIter *value,
1221 struct supplicant_result *result)
1223 DBusMessageIter array;
1224 unsigned char *ssid;
1227 dbus_message_iter_recurse(value, &array);
1228 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1233 if (ssid[0] == '\0')
1236 result->ssid = g_try_malloc(ssid_len);
1237 if (result->ssid == NULL)
1240 memcpy(result->ssid, ssid, ssid_len);
1241 result->ssid_len = ssid_len;
1243 result->name = g_try_malloc0(ssid_len + 1);
1244 if (result->name == NULL)
1247 for (i = 0; i < ssid_len; i++) {
1248 if (g_ascii_isprint(ssid[i]))
1249 result->name[i] = ssid[i];
1251 result->name[i] = ' ';
1255 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
1256 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
1258 static void extract_rsn(struct supplicant_result *result,
1259 const unsigned char *buf, int len)
1278 /* Pairwise cipher */
1282 count = buf[0] | (buf[1] << 8);
1283 if (2 + (count * 4) > len)
1286 buf += 2 + (count * 4);
1287 len -= 2 + (count * 4);
1289 /* Authentication */
1293 count = buf[0] | (buf[1] << 8);
1294 if (2 + (count * 4) > len)
1297 for (i = 0; i < count; i++) {
1298 const unsigned char *ptr = buf + 2 + (i * 4);
1300 if (memcmp(ptr, wifi_oui, 3) == 0) {
1303 result->has_8021x = TRUE;
1306 result->has_psk = TRUE;
1309 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
1312 result->has_8021x = TRUE;
1315 result->has_psk = TRUE;
1321 buf += 2 + (count * 4);
1322 len -= 2 + (count * 4);
1325 static void extract_wpaie(DBusMessageIter *value,
1326 struct supplicant_result *result)
1328 DBusMessageIter array;
1332 dbus_message_iter_recurse(value, &array);
1333 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1336 result->has_wpa = TRUE;
1337 extract_rsn(result, ie + 6, ie_len - 6);
1341 static void extract_rsnie(DBusMessageIter *value,
1342 struct supplicant_result *result)
1344 DBusMessageIter array;
1348 dbus_message_iter_recurse(value, &array);
1349 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1352 result->has_rsn = TRUE;
1353 extract_rsn(result, ie + 2, ie_len - 2);
1357 static void extract_wpsie(DBusMessageIter *value,
1358 struct supplicant_result *result)
1360 DBusMessageIter array;
1364 dbus_message_iter_recurse(value, &array);
1365 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1368 result->has_wps = TRUE;
1371 static void extract_capabilites(DBusMessageIter *value,
1372 struct supplicant_result *result)
1374 dbus_message_iter_get_basic(value, &result->capabilities);
1376 if (result->capabilities & IEEE80211_CAP_ESS)
1377 result->adhoc = FALSE;
1378 else if (result->capabilities & IEEE80211_CAP_IBSS)
1379 result->adhoc = TRUE;
1381 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1382 result->has_wep = TRUE;
1385 static unsigned char calculate_strength(struct supplicant_task *task,
1386 struct supplicant_result *result)
1388 if (result->quality == -1 || task->range->max_qual.qual == 0) {
1389 unsigned char strength;
1391 if (result->level > 0)
1392 strength = 100 - result->level;
1394 strength = 120 + result->level;
1402 return (result->quality * 100) / task->range->max_qual.qual;
1405 static unsigned short calculate_channel(struct supplicant_result *result)
1407 if (result->frequency < 0)
1410 return (result->frequency - 2407) / 5;
1413 static void get_properties(struct supplicant_task *task);
1415 static void properties_reply(DBusPendingCall *call, void *user_data)
1417 struct supplicant_task *task = user_data;
1418 struct supplicant_result result;
1419 struct connman_network *network;
1421 DBusMessageIter array, dict;
1422 unsigned char strength;
1423 unsigned short channel, frequency;
1424 const char *mode, *security;
1427 DBG("task %p", task);
1429 reply = dbus_pending_call_steal_reply(call);
1430 if (reply == NULL) {
1431 get_properties(task);
1435 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1436 dbus_message_unref(reply);
1437 get_properties(task);
1441 memset(&result, 0, sizeof(result));
1442 result.frequency = -1;
1443 result.quality = -1;
1447 dbus_message_iter_init(reply, &array);
1449 dbus_message_iter_recurse(&array, &dict);
1451 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1452 DBusMessageIter entry, value;
1455 dbus_message_iter_recurse(&dict, &entry);
1456 dbus_message_iter_get_basic(&entry, &key);
1458 dbus_message_iter_next(&entry);
1460 dbus_message_iter_recurse(&entry, &value);
1462 //type = dbus_message_iter_get_arg_type(&value);
1463 //dbus_message_iter_get_basic(&value, &val);
1471 * frequency : i (105)
1472 * capabilities : q (113)
1479 if (g_str_equal(key, "bssid") == TRUE)
1480 extract_addr(&value, &result);
1481 else if (g_str_equal(key, "ssid") == TRUE)
1482 extract_ssid(&value, &result);
1483 else if (g_str_equal(key, "wpaie") == TRUE)
1484 extract_wpaie(&value, &result);
1485 else if (g_str_equal(key, "rsnie") == TRUE)
1486 extract_rsnie(&value, &result);
1487 else if (g_str_equal(key, "wpsie") == TRUE)
1488 extract_wpsie(&value, &result);
1489 else if (g_str_equal(key, "capabilities") == TRUE)
1490 extract_capabilites(&value, &result);
1491 else if (g_str_equal(key, "frequency") == TRUE)
1492 dbus_message_iter_get_basic(&value, &result.frequency);
1493 else if (g_str_equal(key, "quality") == TRUE)
1494 dbus_message_iter_get_basic(&value, &result.quality);
1495 else if (g_str_equal(key, "noise") == TRUE)
1496 dbus_message_iter_get_basic(&value, &result.noise);
1497 else if (g_str_equal(key, "level") == TRUE)
1498 dbus_message_iter_get_basic(&value, &result.level);
1499 else if (g_str_equal(key, "maxrate") == TRUE)
1500 dbus_message_iter_get_basic(&value, &result.maxrate);
1502 dbus_message_iter_next(&dict);
1505 DBG("capabilties %u frequency %d "
1506 "quality %d noise %d level %d maxrate %d",
1507 result.capabilities, result.frequency,
1508 result.quality, result.noise,
1509 result.level, result.maxrate);
1511 if (result.path == NULL)
1514 if (result.path[0] == '\0')
1517 if (result.frequency > 0 && result.frequency < 14)
1518 result.frequency = 2407 + (5 * result.frequency);
1519 else if (result.frequency == 14)
1520 result.frequency = 2484;
1522 strength = calculate_strength(task, &result);
1523 channel = calculate_channel(&result);
1525 frequency = (result.frequency < 0) ? 0 : result.frequency;
1527 if (result.has_8021x == TRUE)
1528 security = "ieee8021x";
1529 else if (result.has_psk == TRUE)
1531 else if (result.has_wep == TRUE)
1536 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1538 group = build_group(result.path, result.name,
1539 result.ssid, result.ssid_len,
1542 if (result.has_psk == TRUE) {
1543 if (result.has_rsn == TRUE)
1545 else if (result.has_wpa == TRUE)
1549 network = connman_device_get_network(task->device, result.path);
1550 if (network == NULL) {
1553 network = connman_network_create(result.path,
1554 CONNMAN_NETWORK_TYPE_WIFI);
1555 if (network == NULL)
1558 index = connman_device_get_index(task->device);
1559 connman_network_set_index(network, index);
1561 connman_network_set_protocol(network,
1562 CONNMAN_NETWORK_PROTOCOL_IP);
1564 connman_network_set_address(network, result.addr,
1567 if (connman_device_add_network(task->device, network) < 0) {
1568 connman_network_unref(network);
1573 if (result.name != NULL && result.name[0] != '\0')
1574 connman_network_set_name(network, result.name);
1576 connman_network_set_blob(network, "WiFi.SSID",
1577 result.ssid, result.ssid_len);
1579 connman_network_set_string(network, "WiFi.Mode", mode);
1581 DBG("%s (%s %s) strength %d (%s)",
1582 result.name, mode, security, strength,
1583 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1585 connman_network_set_available(network, TRUE);
1586 connman_network_set_strength(network, strength);
1588 connman_network_set_uint16(network, "Frequency", frequency);
1589 connman_network_set_uint16(network, "WiFi.Channel", channel);
1590 connman_network_set_string(network, "WiFi.Security", security);
1592 if (result.ssid != NULL)
1593 connman_network_set_group(network, group);
1598 g_free(result.path);
1599 g_free(result.addr);
1600 g_free(result.name);
1601 g_free(result.ssid);
1603 dbus_message_unref(reply);
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);
1672 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1675 dbus_error_init(&error);
1677 if (dbus_message_get_args(reply, &error,
1678 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1679 &results, &num_results,
1680 DBUS_TYPE_INVALID) == FALSE) {
1681 if (dbus_error_is_set(&error) == TRUE) {
1682 connman_error("%s", error.message);
1683 dbus_error_free(&error);
1685 connman_error("Wrong arguments for scan result");
1689 if (num_results == 0)
1692 for (i = 0; i < num_results; i++) {
1693 char *path = g_strdup(results[i]);
1697 task->scan_results = g_slist_append(task->scan_results, path);
1700 g_strfreev(results);
1702 dbus_message_unref(reply);
1704 get_properties(task);
1709 dbus_message_unref(reply);
1712 task->result_call = NULL;
1714 if (task->scanning == TRUE) {
1715 connman_device_set_scanning(task->device, FALSE);
1716 task->scanning = FALSE;
1720 static void scan_results_available(struct supplicant_task *task)
1722 DBusMessage *message;
1724 DBG("task %p", task);
1726 if (task->result_call != NULL)
1729 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1730 SUPPLICANT_INTF ".Interface",
1732 if (message == NULL)
1735 dbus_message_set_auto_start(message, FALSE);
1737 if (dbus_connection_send_with_reply(connection, message,
1738 &task->result_call, TIMEOUT) == FALSE) {
1739 connman_error("Failed to request scan result");
1743 if (task->result_call == NULL) {
1744 connman_error("D-Bus connection not available");
1748 if (task->scanning == TRUE)
1749 connman_device_set_scanning(task->device, TRUE);
1751 dbus_pending_call_set_notify(task->result_call,
1752 scan_results_reply, task, NULL);
1755 dbus_message_unref(message);
1758 static enum supplicant_state string2state(const char *state)
1760 if (g_str_equal(state, "INACTIVE") == TRUE)
1761 return WPA_INACTIVE;
1762 else if (g_str_equal(state, "SCANNING") == TRUE)
1763 return WPA_SCANNING;
1764 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1765 return WPA_ASSOCIATING;
1766 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1767 return WPA_ASSOCIATED;
1768 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1769 return WPA_GROUP_HANDSHAKE;
1770 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1771 return WPA_4WAY_HANDSHAKE;
1772 else if (g_str_equal(state, "COMPLETED") == TRUE)
1773 return WPA_COMPLETED;
1774 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1775 return WPA_DISCONNECTED;
1780 static int task_connect(struct supplicant_task *task)
1782 const char *address, *security, *passphrase;
1784 unsigned int ssid_len;
1787 connman_inet_ifup(task->ifindex);
1789 address = connman_network_get_string(task->network, "Address");
1790 security = connman_network_get_string(task->network, "WiFi.Security");
1791 passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1793 ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1795 DBG("address %s security %s", address, security);
1797 if (security == NULL && passphrase == NULL)
1800 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1803 remove_network(task);
1809 err = set_network(task, ssid, ssid_len, address, security, passphrase);
1813 err = select_network(task);
1817 return -EINPROGRESS;
1820 static void scanning(struct supplicant_task *task, DBusMessage *msg)
1823 dbus_bool_t scanning;
1825 dbus_error_init(&error);
1827 if (dbus_message_get_args(msg, &error, DBUS_TYPE_BOOLEAN, &scanning,
1828 DBUS_TYPE_INVALID) == FALSE) {
1829 if (dbus_error_is_set(&error) == TRUE) {
1830 connman_error("%s", error.message);
1831 dbus_error_free(&error);
1833 connman_error("Wrong arguments for scanning");
1837 connman_info("%s scanning %s", task->ifname,
1838 scanning == TRUE ? "started" : "finished");
1841 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1844 const char *newstate, *oldstate;
1845 unsigned char bssid[ETH_ALEN];
1846 unsigned int bssid_len;
1847 enum supplicant_state state, prevstate;
1849 dbus_error_init(&error);
1851 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1852 DBUS_TYPE_STRING, &oldstate,
1853 DBUS_TYPE_INVALID) == FALSE) {
1854 if (dbus_error_is_set(&error) == TRUE) {
1855 connman_error("%s", error.message);
1856 dbus_error_free(&error);
1858 connman_error("Wrong arguments for state change");
1862 DBG("state %s ==> %s", oldstate, newstate);
1864 connman_info("%s %s%s", task->ifname, newstate,
1865 task->scanning == TRUE ? " (scanning)" : "");
1867 state = string2state(newstate);
1868 if (state == WPA_INVALID)
1871 if (task->scanning == TRUE && state != WPA_SCANNING) {
1872 connman_device_cleanup_scanning(task->device);
1873 task->scanning = FALSE;
1876 prevstate = task->state;
1877 task->state = state;
1879 if (task->network == NULL)
1882 switch (task->state) {
1884 switch (prevstate) {
1885 case WPA_ASSOCIATED:
1886 case WPA_GROUP_HANDSHAKE:
1892 /* reset scan trigger and schedule background scan */
1893 connman_device_schedule_scan(task->device);
1895 if (get_bssid(task->device, bssid, &bssid_len) == 0)
1896 connman_network_set_address(task->network,
1900 connman_network_set_method(task->network,
1901 CONNMAN_IPCONFIG_METHOD_DHCP);
1902 connman_network_set_connected(task->network, TRUE);
1905 case WPA_ASSOCIATING:
1906 switch (prevstate) {
1910 connman_network_set_associating(task->network, TRUE);
1918 switch (prevstate) {
1920 case WPA_DISCONNECTED:
1927 case WPA_DISCONNECTED:
1929 connman_network_set_connected(task->network, FALSE);
1931 if (task->disconnecting == TRUE) {
1932 connman_network_unref(task->network);
1933 task->disconnecting = FALSE;
1935 if (task->pending_network != NULL) {
1936 task->network = task->pending_network;
1937 task->pending_network = NULL;
1940 task->network = NULL;
1945 connman_network_set_associating(task->network, FALSE);
1952 connman_error("%s invalid state change %s -> %s", task->ifname,
1953 oldstate, newstate);
1956 static gboolean supplicant_filter(DBusConnection *conn,
1957 DBusMessage *msg, void *data)
1959 struct supplicant_task *task;
1960 const char *member, *path;
1962 member = dbus_message_get_member(msg);
1966 path = dbus_message_get_path(msg);
1970 task = find_task_by_path(path);
1974 DBG("task %p member %s", task, member);
1976 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1977 scan_results_available(task);
1978 else if (g_str_equal(member, "Scanning") == TRUE)
1979 scanning(task, msg);
1980 else if (g_str_equal(member, "StateChange") == TRUE)
1981 state_change(task, msg);
1986 int supplicant_start(struct connman_device *device)
1988 struct supplicant_task *task;
1991 DBG("device %p", device);
1993 task = g_try_new0(struct supplicant_task, 1);
1997 task->ifindex = connman_device_get_index(device);
1998 task->ifname = connman_inet_ifname(task->ifindex);
2000 if (task->ifname == NULL) {
2005 task->mac80211 = connman_inet_is_mac80211(task->ifindex);
2006 if (task->mac80211 == FALSE)
2007 connman_warn("Enabling quirks for unsupported driver");
2009 task->range = g_try_malloc0(sizeof(struct iw_range));
2010 if (task->range == NULL) {
2015 err = get_range(task);
2019 task->device = connman_device_ref(device);
2021 task->created = FALSE;
2022 task->scanning = FALSE;
2023 task->state = WPA_INVALID;
2024 task->disconnecting = FALSE;
2025 task->pending_network = NULL;
2027 task_list = g_slist_append(task_list, task);
2029 return create_interface(task);
2032 g_free(task->range);
2033 g_free(task->ifname);
2039 int supplicant_stop(struct connman_device *device)
2041 int index = connman_device_get_index(device);
2042 struct supplicant_task *task;
2044 DBG("device %p", device);
2046 task = find_task_by_index(index);
2050 g_free(task->range);
2052 task_list = g_slist_remove(task_list, task);
2054 if (task->scan_call != NULL) {
2055 dbus_pending_call_cancel(task->scan_call);
2056 task->scan_call = NULL;
2059 if (task->result_call != NULL) {
2060 dbus_pending_call_cancel(task->result_call);
2061 task->result_call = NULL;
2064 if (task->scanning == TRUE)
2065 connman_device_set_scanning(task->device, FALSE);
2067 remove_network(task);
2069 disconnect_network(task);
2071 return remove_interface(task);
2074 int supplicant_scan(struct connman_device *device)
2076 int index = connman_device_get_index(device);
2077 struct supplicant_task *task;
2080 DBG("device %p", device);
2082 task = find_task_by_index(index);
2086 switch (task->state) {
2089 case WPA_ASSOCIATING:
2090 case WPA_ASSOCIATED:
2091 case WPA_4WAY_HANDSHAKE:
2092 case WPA_GROUP_HANDSHAKE:
2098 task->scanning = TRUE;
2100 err = initiate_scan(task);
2102 if (err == -EINPROGRESS)
2105 task->scanning = FALSE;
2109 connman_device_set_scanning(task->device, TRUE);
2114 int supplicant_connect(struct connman_network *network)
2116 struct supplicant_task *task;
2119 DBG("network %p", network);
2121 index = connman_network_get_index(network);
2123 task = find_task_by_index(index);
2127 if (task->disconnecting == TRUE)
2128 task->pending_network = connman_network_ref(network);
2130 task->network = connman_network_ref(network);
2131 return task_connect(task);
2134 return -EINPROGRESS;
2137 int supplicant_disconnect(struct connman_network *network)
2139 struct supplicant_task *task;
2142 DBG("network %p", network);
2144 index = connman_network_get_index(network);
2146 task = find_task_by_index(index);
2150 if (task->disconnecting == TRUE)
2153 remove_network(task);
2155 disconnect_network(task);
2157 task->disconnecting = TRUE;
2162 static void supplicant_activate(DBusConnection *conn)
2164 DBusMessage *message;
2166 DBG("conn %p", conn);
2168 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
2169 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
2170 if (message == NULL)
2173 dbus_message_set_no_reply(message, TRUE);
2175 dbus_connection_send(conn, message, NULL);
2177 dbus_message_unref(message);
2180 static GSList *driver_list = NULL;
2182 static void supplicant_probe(DBusConnection *conn, void *user_data)
2186 DBG("conn %p", conn);
2188 for (list = driver_list; list; list = list->next) {
2189 struct supplicant_driver *driver = list->data;
2191 DBG("driver %p name %s", driver, driver->name);
2198 static void supplicant_remove(DBusConnection *conn, void *user_data)
2202 DBG("conn %p", conn);
2204 for (list = driver_list; list; list = list->next) {
2205 struct supplicant_driver *driver = list->data;
2207 DBG("driver %p name %s", driver, driver->name);
2215 static guint iface_watch;
2217 static int supplicant_create(void)
2219 if (g_slist_length(driver_list) > 0)
2222 connection = connman_dbus_get_connection();
2223 if (connection == NULL)
2226 DBG("connection %p", connection);
2228 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
2229 supplicant_probe, supplicant_remove, NULL, NULL);
2231 iface_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2232 SUPPLICANT_INTF ".Interface",
2233 NULL, supplicant_filter,
2236 if (watch == 0 || iface_watch == 0) {
2237 g_dbus_remove_watch(connection, watch);
2238 g_dbus_remove_watch(connection, iface_watch);
2245 static void supplicant_destroy(void)
2247 if (g_slist_length(driver_list) > 0)
2250 DBG("connection %p", connection);
2252 g_dbus_remove_watch(connection, watch);
2253 g_dbus_remove_watch(connection, iface_watch);
2255 dbus_connection_unref(connection);
2259 int supplicant_register(struct supplicant_driver *driver)
2263 DBG("driver %p name %s", driver, driver->name);
2265 err = supplicant_create();
2269 driver_list = g_slist_append(driver_list, driver);
2271 supplicant_activate(connection);
2276 void supplicant_unregister(struct supplicant_driver *driver)
2278 DBG("driver %p name %s", driver, driver->name);
2280 supplicant_remove(connection, NULL);
2282 driver_list = g_slist_remove(driver_list, driver);
2284 supplicant_destroy();