3 * WPA supplicant library with GLib integration
5 * Copyright (C) 2012 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
38 #include "gsupplicant.h"
42 #define IEEE80211_CAP_ESS 0x0001
43 #define IEEE80211_CAP_IBSS 0x0002
44 #define IEEE80211_CAP_PRIVACY 0x0010
46 static DBusConnection *connection;
48 static const GSupplicantCallbacks *callbacks_pointer;
50 static dbus_bool_t system_available = FALSE;
51 static dbus_bool_t system_ready = FALSE;
53 static dbus_int32_t debug_level;
54 static dbus_bool_t debug_timestamp = FALSE;
55 static dbus_bool_t debug_showkeys = FALSE;
57 static const char *debug_strings[] = {
58 "msgdump", "debug", "info", "warning", "error", NULL
61 static unsigned int eap_methods;
68 static struct strvalmap eap_method_map[] = {
69 { "MD5", G_SUPPLICANT_EAP_METHOD_MD5 },
70 { "TLS", G_SUPPLICANT_EAP_METHOD_TLS },
71 { "MSCHAPV2", G_SUPPLICANT_EAP_METHOD_MSCHAPV2 },
72 { "PEAP", G_SUPPLICANT_EAP_METHOD_PEAP },
73 { "TTLS", G_SUPPLICANT_EAP_METHOD_TTLS },
74 { "GTC", G_SUPPLICANT_EAP_METHOD_GTC },
75 { "OTP", G_SUPPLICANT_EAP_METHOD_OTP },
76 { "LEAP", G_SUPPLICANT_EAP_METHOD_LEAP },
77 { "WSC", G_SUPPLICANT_EAP_METHOD_WSC },
81 static struct strvalmap keymgmt_map[] = {
82 { "none", G_SUPPLICANT_KEYMGMT_NONE },
83 { "ieee8021x", G_SUPPLICANT_KEYMGMT_IEEE8021X },
84 { "wpa-none", G_SUPPLICANT_KEYMGMT_WPA_NONE },
85 { "wpa-psk", G_SUPPLICANT_KEYMGMT_WPA_PSK },
86 { "wpa-psk-sha256", G_SUPPLICANT_KEYMGMT_WPA_PSK_256 },
87 { "wpa-ft-psk", G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
88 { "wpa-ft-eap", G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
89 { "wpa-eap", G_SUPPLICANT_KEYMGMT_WPA_EAP },
90 { "wpa-eap-sha256", G_SUPPLICANT_KEYMGMT_WPA_EAP_256 },
91 { "wps", G_SUPPLICANT_KEYMGMT_WPS },
95 static struct strvalmap authalg_capa_map[] = {
96 { "open", G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN },
97 { "shared", G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED },
98 { "leap", G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP },
102 static struct strvalmap proto_capa_map[] = {
103 { "wpa", G_SUPPLICANT_CAPABILITY_PROTO_WPA },
104 { "rsn", G_SUPPLICANT_CAPABILITY_PROTO_RSN },
108 static struct strvalmap group_map[] = {
109 { "wep40", G_SUPPLICANT_GROUP_WEP40 },
110 { "wep104", G_SUPPLICANT_GROUP_WEP104 },
111 { "tkip", G_SUPPLICANT_GROUP_TKIP },
112 { "ccmp", G_SUPPLICANT_GROUP_CCMP },
116 static struct strvalmap pairwise_map[] = {
117 { "none", G_SUPPLICANT_PAIRWISE_NONE },
118 { "tkip", G_SUPPLICANT_PAIRWISE_TKIP },
119 { "ccmp", G_SUPPLICANT_PAIRWISE_CCMP },
123 static struct strvalmap scan_capa_map[] = {
124 { "active", G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE },
125 { "passive", G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE },
126 { "ssid", G_SUPPLICANT_CAPABILITY_SCAN_SSID },
130 static struct strvalmap mode_capa_map[] = {
131 { "infrastructure", G_SUPPLICANT_CAPABILITY_MODE_INFRA },
132 { "ad-hoc", G_SUPPLICANT_CAPABILITY_MODE_IBSS },
133 { "ap", G_SUPPLICANT_CAPABILITY_MODE_AP },
137 static GHashTable *interface_table;
138 static GHashTable *bss_mapping;
140 struct _GSupplicantWpsCredentials {
141 unsigned char ssid[32];
142 unsigned int ssid_len;
146 struct _GSupplicantInterface {
149 unsigned int keymgmt_capa;
150 unsigned int authalg_capa;
151 unsigned int proto_capa;
152 unsigned int group_capa;
153 unsigned int pairwise_capa;
154 unsigned int scan_capa;
155 unsigned int mode_capa;
156 unsigned int max_scan_ssids;
158 GSupplicantState state;
159 dbus_bool_t scanning;
160 GSupplicantInterfaceCallback scan_callback;
166 struct _GSupplicantWpsCredentials wps_cred;
167 GSupplicantWpsState wps_state;
168 GHashTable *network_table;
169 GHashTable *net_mapping;
170 GHashTable *bss_mapping;
174 struct g_supplicant_bss {
175 GSupplicantInterface *interface;
177 unsigned char bssid[6];
178 unsigned char ssid[32];
179 unsigned int ssid_len;
180 dbus_uint16_t frequency;
181 dbus_uint32_t maxrate;
183 GSupplicantMode mode;
184 GSupplicantSecurity security;
185 unsigned int keymgmt;
186 unsigned int pairwise;
190 dbus_bool_t ieee8021x;
191 unsigned int wps_capabilities;
194 struct _GSupplicantNetwork {
195 GSupplicantInterface *interface;
199 unsigned char ssid[32];
200 unsigned int ssid_len;
202 dbus_uint16_t frequency;
203 struct g_supplicant_bss *best_bss;
204 GSupplicantMode mode;
205 GSupplicantSecurity security;
207 unsigned int wps_capabilities;
208 GHashTable *bss_table;
209 GHashTable *config_table;
212 static inline void debug(const char *format, ...)
217 if (callbacks_pointer->debug == NULL)
220 va_start(ap, format);
222 if (vsnprintf(str, sizeof(str), format, ap) > 0)
223 callbacks_pointer->debug(str);
228 #define SUPPLICANT_DBG(fmt, arg...) \
229 debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
231 static GSupplicantMode string2mode(const char *mode)
234 return G_SUPPLICANT_MODE_UNKNOWN;
236 if (g_str_equal(mode, "infrastructure") == TRUE)
237 return G_SUPPLICANT_MODE_INFRA;
238 else if (g_str_equal(mode, "ad-hoc") == TRUE)
239 return G_SUPPLICANT_MODE_IBSS;
241 return G_SUPPLICANT_MODE_UNKNOWN;
244 static const char *mode2string(GSupplicantMode mode)
247 case G_SUPPLICANT_MODE_UNKNOWN:
249 case G_SUPPLICANT_MODE_INFRA:
251 case G_SUPPLICANT_MODE_IBSS:
253 case G_SUPPLICANT_MODE_MASTER:
260 static const char *security2string(GSupplicantSecurity security)
263 case G_SUPPLICANT_SECURITY_UNKNOWN:
265 case G_SUPPLICANT_SECURITY_NONE:
267 case G_SUPPLICANT_SECURITY_WEP:
269 case G_SUPPLICANT_SECURITY_PSK:
271 case G_SUPPLICANT_SECURITY_IEEE8021X:
278 static GSupplicantState string2state(const char *state)
281 return G_SUPPLICANT_STATE_UNKNOWN;
283 if (g_str_equal(state, "unknown") == TRUE)
284 return G_SUPPLICANT_STATE_UNKNOWN;
285 else if (g_str_equal(state, "disconnected") == TRUE)
286 return G_SUPPLICANT_STATE_DISCONNECTED;
287 else if (g_str_equal(state, "inactive") == TRUE)
288 return G_SUPPLICANT_STATE_INACTIVE;
289 else if (g_str_equal(state, "scanning") == TRUE)
290 return G_SUPPLICANT_STATE_SCANNING;
291 else if (g_str_equal(state, "authenticating") == TRUE)
292 return G_SUPPLICANT_STATE_AUTHENTICATING;
293 else if (g_str_equal(state, "associating") == TRUE)
294 return G_SUPPLICANT_STATE_ASSOCIATING;
295 else if (g_str_equal(state, "associated") == TRUE)
296 return G_SUPPLICANT_STATE_ASSOCIATED;
297 else if (g_str_equal(state, "group_handshake") == TRUE)
298 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
299 else if (g_str_equal(state, "4way_handshake") == TRUE)
300 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
301 else if (g_str_equal(state, "completed") == TRUE)
302 return G_SUPPLICANT_STATE_COMPLETED;
304 return G_SUPPLICANT_STATE_UNKNOWN;
307 static void callback_system_ready(void)
309 if (system_ready == TRUE)
314 if (callbacks_pointer == NULL)
317 if (callbacks_pointer->system_ready == NULL)
320 callbacks_pointer->system_ready();
323 static void callback_system_killed(void)
325 system_ready = FALSE;
327 if (callbacks_pointer == NULL)
330 if (callbacks_pointer->system_killed == NULL)
333 callbacks_pointer->system_killed();
336 static void callback_interface_added(GSupplicantInterface *interface)
340 if (callbacks_pointer == NULL)
343 if (callbacks_pointer->interface_added == NULL)
346 callbacks_pointer->interface_added(interface);
349 static void callback_interface_state(GSupplicantInterface *interface)
351 if (callbacks_pointer == NULL)
354 if (callbacks_pointer->interface_state == NULL)
357 callbacks_pointer->interface_state(interface);
360 static void callback_interface_removed(GSupplicantInterface *interface)
362 if (callbacks_pointer == NULL)
365 if (callbacks_pointer->interface_removed == NULL)
368 callbacks_pointer->interface_removed(interface);
371 static void callback_scan_started(GSupplicantInterface *interface)
373 if (callbacks_pointer == NULL)
376 if (callbacks_pointer->scan_started == NULL)
379 callbacks_pointer->scan_started(interface);
382 static void callback_scan_finished(GSupplicantInterface *interface)
384 if (callbacks_pointer == NULL)
387 if (callbacks_pointer->scan_finished == NULL)
390 callbacks_pointer->scan_finished(interface);
393 static void callback_network_added(GSupplicantNetwork *network)
395 if (callbacks_pointer == NULL)
398 if (callbacks_pointer->network_added == NULL)
401 callbacks_pointer->network_added(network);
404 static void callback_network_removed(GSupplicantNetwork *network)
406 if (callbacks_pointer == NULL)
409 if (callbacks_pointer->network_removed == NULL)
412 callbacks_pointer->network_removed(network);
415 static void callback_network_changed(GSupplicantNetwork *network,
416 const char *property)
418 if (callbacks_pointer == NULL)
421 if (callbacks_pointer->network_changed == NULL)
424 callbacks_pointer->network_changed(network, property);
427 static void remove_interface(gpointer data)
429 GSupplicantInterface *interface = data;
431 g_hash_table_destroy(interface->bss_mapping);
432 g_hash_table_destroy(interface->net_mapping);
433 g_hash_table_destroy(interface->network_table);
435 callback_interface_removed(interface);
437 g_free(interface->wps_cred.key);
438 g_free(interface->path);
439 g_free(interface->network_path);
440 g_free(interface->ifname);
441 g_free(interface->driver);
442 g_free(interface->bridge);
446 static void remove_network(gpointer data)
448 GSupplicantNetwork *network = data;
450 g_hash_table_destroy(network->bss_table);
452 callback_network_removed(network);
454 g_hash_table_destroy(network->config_table);
456 g_free(network->path);
457 g_free(network->group);
458 g_free(network->name);
462 static void remove_bss(gpointer data)
464 struct g_supplicant_bss *bss = data;
470 static void debug_strvalmap(const char *label, struct strvalmap *map,
475 for (i = 0; map[i].str != NULL; i++) {
476 if (val & map[i].val)
477 SUPPLICANT_DBG("%s: %s", label, map[i].str);
481 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
483 GSupplicantInterface *interface = user_data;
484 const char *str = NULL;
487 dbus_message_iter_get_basic(iter, &str);
491 for (i = 0; keymgmt_map[i].str != NULL; i++)
492 if (strcmp(str, keymgmt_map[i].str) == 0) {
493 interface->keymgmt_capa |= keymgmt_map[i].val;
498 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
500 GSupplicantInterface *interface = user_data;
501 const char *str = NULL;
504 dbus_message_iter_get_basic(iter, &str);
508 for (i = 0; authalg_capa_map[i].str != NULL; i++)
509 if (strcmp(str, authalg_capa_map[i].str) == 0) {
510 interface->authalg_capa |= authalg_capa_map[i].val;
515 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
517 GSupplicantInterface *interface = user_data;
518 const char *str = NULL;
521 dbus_message_iter_get_basic(iter, &str);
525 for (i = 0; proto_capa_map[i].str != NULL; i++)
526 if (strcmp(str, proto_capa_map[i].str) == 0) {
527 interface->proto_capa |= proto_capa_map[i].val;
532 static void interface_capability_pairwise(DBusMessageIter *iter,
535 GSupplicantInterface *interface = user_data;
536 const char *str = NULL;
539 dbus_message_iter_get_basic(iter, &str);
543 for (i = 0; pairwise_map[i].str != NULL; i++)
544 if (strcmp(str, pairwise_map[i].str) == 0) {
545 interface->pairwise_capa |= pairwise_map[i].val;
550 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
552 GSupplicantInterface *interface = user_data;
553 const char *str = NULL;
556 dbus_message_iter_get_basic(iter, &str);
560 for (i = 0; group_map[i].str != NULL; i++)
561 if (strcmp(str, group_map[i].str) == 0) {
562 interface->group_capa |= group_map[i].val;
567 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
569 GSupplicantInterface *interface = user_data;
570 const char *str = NULL;
573 dbus_message_iter_get_basic(iter, &str);
577 for (i = 0; scan_capa_map[i].str != NULL; i++)
578 if (strcmp(str, scan_capa_map[i].str) == 0) {
579 interface->scan_capa |= scan_capa_map[i].val;
584 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
586 GSupplicantInterface *interface = user_data;
587 const char *str = NULL;
590 dbus_message_iter_get_basic(iter, &str);
594 for (i = 0; mode_capa_map[i].str != NULL; i++)
595 if (strcmp(str, mode_capa_map[i].str) == 0) {
596 interface->mode_capa |= mode_capa_map[i].val;
601 static void interface_capability(const char *key, DBusMessageIter *iter,
604 GSupplicantInterface *interface = user_data;
609 if (g_strcmp0(key, "KeyMgmt") == 0)
610 supplicant_dbus_array_foreach(iter,
611 interface_capability_keymgmt, interface);
612 else if (g_strcmp0(key, "AuthAlg") == 0)
613 supplicant_dbus_array_foreach(iter,
614 interface_capability_authalg, interface);
615 else if (g_strcmp0(key, "Protocol") == 0)
616 supplicant_dbus_array_foreach(iter,
617 interface_capability_proto, interface);
618 else if (g_strcmp0(key, "Pairwise") == 0)
619 supplicant_dbus_array_foreach(iter,
620 interface_capability_pairwise, interface);
621 else if (g_strcmp0(key, "Group") == 0)
622 supplicant_dbus_array_foreach(iter,
623 interface_capability_group, interface);
624 else if (g_strcmp0(key, "Scan") == 0)
625 supplicant_dbus_array_foreach(iter,
626 interface_capability_scan, interface);
627 else if (g_strcmp0(key, "Modes") == 0)
628 supplicant_dbus_array_foreach(iter,
629 interface_capability_mode, interface);
630 else if (g_strcmp0(key, "MaxScanSSID") == 0) {
631 dbus_int32_t max_scan_ssid;
633 dbus_message_iter_get_basic(iter, &max_scan_ssid);
634 interface->max_scan_ssids = max_scan_ssid;
637 SUPPLICANT_DBG("key %s type %c",
638 key, dbus_message_iter_get_arg_type(iter));
641 static void set_apscan(DBusMessageIter *iter, void *user_data)
643 unsigned int ap_scan = *(unsigned int *)user_data;
645 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
648 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
649 unsigned int ap_scan)
651 return supplicant_dbus_property_set(interface->path,
652 SUPPLICANT_INTERFACE ".Interface",
653 "ApScan", DBUS_TYPE_UINT32_AS_STRING,
654 set_apscan, NULL, &ap_scan);
657 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
660 if (interface == NULL)
663 interface->data = data;
666 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
668 if (interface == NULL)
671 return interface->data;
674 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
676 if (interface == NULL)
679 return interface->ifname;
682 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
684 if (interface == NULL)
687 return interface->driver;
690 GSupplicantState g_supplicant_interface_get_state(
691 GSupplicantInterface *interface)
693 if (interface == NULL)
694 return G_SUPPLICANT_STATE_UNKNOWN;
696 return interface->state;
699 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
701 if (interface == NULL)
704 return (const char *)interface->wps_cred.key;
707 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
708 unsigned int *ssid_len)
710 if (ssid_len == NULL)
713 if (interface == NULL || interface->wps_cred.ssid == NULL) {
718 *ssid_len = interface->wps_cred.ssid_len;
719 return interface->wps_cred.ssid;
722 GSupplicantWpsState g_supplicant_interface_get_wps_state(
723 GSupplicantInterface *interface)
725 if (interface == NULL)
726 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
728 return interface->wps_state;
731 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
733 if (interface == NULL)
736 return interface->mode_capa;
739 unsigned int g_supplicant_interface_get_max_scan_ssids(
740 GSupplicantInterface *interface)
742 if (interface == NULL)
745 if (interface->max_scan_ssids == 0)
746 return WPAS_MAX_SCAN_SSIDS;
748 return interface->max_scan_ssids;
751 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
753 dbus_bool_t enable = *(dbus_bool_t *)user_data;
755 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
758 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
761 if (interface == NULL)
764 if (interface->network_path == NULL)
768 return supplicant_dbus_property_set(interface->network_path,
769 SUPPLICANT_INTERFACE ".Network",
770 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
771 set_network_enabled, NULL, &enable);
774 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
776 if (interface == NULL)
779 return interface->ready;
782 GSupplicantInterface *g_supplicant_network_get_interface(
783 GSupplicantNetwork *network)
788 return network->interface;
791 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
793 if (network == NULL || network->name == NULL)
796 return network->name;
799 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
801 if (network == NULL || network->group == NULL)
804 return network->group;
807 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
809 if (network == NULL || network->path == NULL)
812 return network->path;
815 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
818 return G_SUPPLICANT_MODE_UNKNOWN;
820 return mode2string(network->mode);
823 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
826 return G_SUPPLICANT_SECURITY_UNKNOWN;
828 return security2string(network->security);
831 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
832 unsigned int *ssid_len)
834 if (network == NULL || network->ssid == NULL) {
839 *ssid_len = network->ssid_len;
840 return network->ssid;
843 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
848 return network->signal;
851 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
856 return network->frequency;
859 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
867 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
872 if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
878 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
883 if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
889 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
894 if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
900 static void merge_network(GSupplicantNetwork *network)
903 const char *ssid, *mode, *key_mgmt;
904 unsigned int i, ssid_len;
907 ssid = g_hash_table_lookup(network->config_table, "ssid");
908 mode = g_hash_table_lookup(network->config_table, "mode");
909 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
911 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
914 ssid_len = strlen(ssid);
918 str = g_string_sized_new((ssid_len * 2) + 24);
922 for (i = 0; i < ssid_len; i++)
923 g_string_append_printf(str, "%02x", ssid[i]);
925 if (g_strcmp0(mode, "0") == 0)
926 g_string_append_printf(str, "_managed");
927 else if (g_strcmp0(mode, "1") == 0)
928 g_string_append_printf(str, "_adhoc");
930 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
931 g_string_append_printf(str, "_psk");
933 group = g_string_free(str, FALSE);
935 SUPPLICANT_DBG("%s", group);
939 g_hash_table_destroy(network->config_table);
941 g_free(network->path);
945 static void network_property(const char *key, DBusMessageIter *iter,
948 GSupplicantNetwork *network = user_data;
950 if (network->interface == NULL)
954 merge_network(network);
958 if (g_strcmp0(key, "Enabled") == 0) {
959 dbus_bool_t enabled = FALSE;
961 dbus_message_iter_get_basic(iter, &enabled);
962 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
963 const char *str = NULL;
965 dbus_message_iter_get_basic(iter, &str);
967 g_hash_table_replace(network->config_table,
968 g_strdup(key), g_strdup(str));
971 SUPPLICANT_DBG("key %s type %c",
972 key, dbus_message_iter_get_arg_type(iter));
975 static void interface_network_added(DBusMessageIter *iter, void *user_data)
977 GSupplicantInterface *interface = user_data;
978 GSupplicantNetwork *network;
979 const char *path = NULL;
983 dbus_message_iter_get_basic(iter, &path);
987 if (g_strcmp0(path, "/") == 0)
990 network = g_hash_table_lookup(interface->net_mapping, path);
994 network = g_try_new0(GSupplicantNetwork, 1);
998 network->interface = interface;
999 network->path = g_strdup(path);
1001 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1004 dbus_message_iter_next(iter);
1005 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1006 supplicant_dbus_property_foreach(iter, network_property,
1008 network_property(NULL, NULL, network);
1012 supplicant_dbus_property_get_all(path,
1013 SUPPLICANT_INTERFACE ".Network",
1014 network_property, network);
1017 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1019 GSupplicantInterface *interface = user_data;
1020 GSupplicantNetwork *network;
1021 const char *path = NULL;
1023 dbus_message_iter_get_basic(iter, &path);
1027 network = g_hash_table_lookup(interface->net_mapping, path);
1028 if (network == NULL)
1031 g_hash_table_remove(interface->net_mapping, path);
1034 static char *create_name(unsigned char *ssid, int ssid_len)
1037 const gchar *remainder, *invalid;
1038 int valid_bytes, remaining_bytes;
1040 if (ssid_len < 1 || ssid[0] == '\0')
1041 return g_strdup("");
1044 remainder = (const gchar *)ssid;
1045 remaining_bytes = ssid_len;
1047 while (remaining_bytes != 0) {
1048 if (g_utf8_validate(remainder, remaining_bytes,
1049 &invalid) == TRUE) {
1053 valid_bytes = invalid - remainder;
1056 string = g_string_sized_new(remaining_bytes);
1058 g_string_append_len(string, remainder, valid_bytes);
1060 /* append U+FFFD REPLACEMENT CHARACTER */
1061 g_string_append(string, "\357\277\275");
1063 remaining_bytes -= valid_bytes + 1;
1064 remainder = invalid + 1;
1068 return g_strndup((const gchar *)ssid, ssid_len + 1);
1070 g_string_append(string, remainder);
1072 return g_string_free(string, FALSE);
1075 static char *create_group(struct g_supplicant_bss *bss)
1079 const char *mode, *security;
1081 str = g_string_sized_new((bss->ssid_len * 2) + 24);
1085 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1086 for (i = 0; i < bss->ssid_len; i++)
1087 g_string_append_printf(str, "%02x", bss->ssid[i]);
1089 g_string_append_printf(str, "hidden");
1091 mode = mode2string(bss->mode);
1093 g_string_append_printf(str, "_%s", mode);
1095 security = security2string(bss->security);
1096 if (security != NULL)
1097 g_string_append_printf(str, "_%s", security);
1099 return g_string_free(str, FALSE);
1102 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1104 GSupplicantInterface *interface = bss->interface;
1105 GSupplicantNetwork *network;
1108 group = create_group(bss);
1109 SUPPLICANT_DBG("New group created: %s", group);
1114 network = g_hash_table_lookup(interface->network_table, group);
1115 if (network != NULL) {
1117 SUPPLICANT_DBG("Network %s already exist", network->name);
1122 network = g_try_new0(GSupplicantNetwork, 1);
1123 if (network == NULL) {
1128 network->interface = interface;
1129 if (network->path == NULL)
1130 network->path = g_strdup(bss->path);
1131 network->group = group;
1132 network->name = create_name(bss->ssid, bss->ssid_len);
1133 network->mode = bss->mode;
1134 network->security = bss->security;
1135 network->ssid_len = bss->ssid_len;
1136 memcpy(network->ssid, bss->ssid, bss->ssid_len);
1137 network->signal = bss->signal;
1138 network->frequency = bss->frequency;
1139 network->best_bss = bss;
1141 network->wps = FALSE;
1142 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1143 network->wps = TRUE;
1145 network->wps_capabilities |= bss->wps_capabilities;
1148 SUPPLICANT_DBG("New network %s created", network->name);
1150 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1153 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1156 g_hash_table_replace(interface->network_table,
1157 network->group, network);
1159 callback_network_added(network);
1162 if (bss->signal > network->signal) {
1163 network->signal = bss->signal;
1164 network->best_bss = bss;
1165 callback_network_changed(network, "Signal");
1168 g_hash_table_replace(interface->bss_mapping, bss->path, network);
1169 g_hash_table_replace(network->bss_table, bss->path, bss);
1171 g_hash_table_replace(bss_mapping, bss->path, interface);
1174 static void bss_rates(DBusMessageIter *iter, void *user_data)
1176 struct g_supplicant_bss *bss = user_data;
1177 dbus_uint32_t rate = 0;
1179 dbus_message_iter_get_basic(iter, &rate);
1183 if (rate > bss->maxrate)
1184 bss->maxrate = rate;
1187 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1189 struct g_supplicant_bss *bss = user_data;
1190 const char *str = NULL;
1193 dbus_message_iter_get_basic(iter, &str);
1197 for (i = 0; keymgmt_map[i].str != NULL; i++)
1198 if (strcmp(str, keymgmt_map[i].str) == 0) {
1199 bss->keymgmt |= keymgmt_map[i].val;
1204 static void bss_group(DBusMessageIter *iter, void *user_data)
1206 struct g_supplicant_bss *bss = user_data;
1207 const char *str = NULL;
1210 dbus_message_iter_get_basic(iter, &str);
1214 for (i = 0; group_map[i].str != NULL; i++)
1215 if (strcmp(str, group_map[i].str) == 0) {
1216 bss->group |= group_map[i].val;
1221 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1223 struct g_supplicant_bss *bss = user_data;
1224 const char *str = NULL;
1227 dbus_message_iter_get_basic(iter, &str);
1231 for (i = 0; pairwise_map[i].str != NULL; i++)
1232 if (strcmp(str, pairwise_map[i].str) == 0) {
1233 bss->pairwise |= pairwise_map[i].val;
1238 static void bss_wpa(const char *key, DBusMessageIter *iter,
1241 if (g_strcmp0(key, "KeyMgmt") == 0)
1242 supplicant_dbus_array_foreach(iter, bss_keymgmt, user_data);
1243 else if (g_strcmp0(key, "Group") == 0)
1244 supplicant_dbus_array_foreach(iter, bss_group, user_data);
1245 else if (g_strcmp0(key, "Pairwise") == 0)
1246 supplicant_dbus_array_foreach(iter, bss_pairwise, user_data);
1250 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1253 unsigned int len = 0;
1255 while (len + 4 < ie_size) {
1256 unsigned int hi = ie[len];
1257 unsigned int lo = ie[len + 1];
1258 unsigned int tmp_type = (hi << 8) + lo;
1259 unsigned int v_len = 0;
1261 /* hi and lo are used to recreate an unsigned int
1262 * based on 2 8bits length unsigned int. */
1266 v_len = (hi << 8) + lo;
1268 if (tmp_type == type) {
1269 unsigned int ret_value = 0;
1270 unsigned char *value = (unsigned char *)&ret_value;
1272 SUPPLICANT_DBG("IE: match type 0x%x", type);
1274 /* Verifying length relevance */
1275 if (v_len > sizeof(unsigned int) ||
1276 len + 4 + v_len > ie_size)
1279 memcpy(value, ie + len + 4, v_len);
1281 SUPPLICANT_DBG("returning 0x%x", ret_value);
1288 SUPPLICANT_DBG("returning 0");
1292 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1294 struct g_supplicant_bss *bss = user_data;
1295 const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1296 unsigned char *ie, *ie_end;
1297 DBusMessageIter array;
1301 #define WMM_WPA1_WPS_INFO 221
1302 #define WPS_INFO_MIN_LEN 6
1303 #define WPS_VERSION_TLV 0x104A
1304 #define WPS_STATE_TLV 0x1044
1305 #define WPS_METHODS_TLV 0x1012
1306 #define WPS_REGISTRAR_TLV 0x1041
1307 #define WPS_VERSION 0x10
1308 #define WPS_PBC 0x04
1309 #define WPS_PIN 0x00
1310 #define WPS_CONFIGURED 0x02
1312 dbus_message_iter_recurse(iter, &array);
1313 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1315 if (ie == NULL || ie_len < 2)
1318 for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1321 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1322 memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1325 SUPPLICANT_DBG("IE: match WPS_OUI");
1327 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1328 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1330 bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1332 if (value == WPS_CONFIGURED)
1333 bss->wps_capabilities |=
1334 G_SUPPLICANT_WPS_CONFIGURED;
1337 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1339 if (GUINT16_FROM_BE(value) == WPS_PBC)
1340 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1341 if (GUINT16_FROM_BE(value) == WPS_PIN)
1342 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1344 bss->wps_capabilities |=
1345 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1347 /* If the AP sends this it means it's advertizing
1348 * as a registrar and the WPS process is launched
1350 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1351 bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1353 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1357 static void bss_compute_security(struct g_supplicant_bss *bss)
1359 if (bss->ieee8021x == TRUE)
1360 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1361 else if (bss->psk == TRUE)
1362 bss->security = G_SUPPLICANT_SECURITY_PSK;
1363 else if (bss->privacy == TRUE)
1364 bss->security = G_SUPPLICANT_SECURITY_WEP;
1366 bss->security = G_SUPPLICANT_SECURITY_NONE;
1370 static void bss_property(const char *key, DBusMessageIter *iter,
1373 struct g_supplicant_bss *bss = user_data;
1375 if (bss->interface == NULL)
1378 SUPPLICANT_DBG("key %s", key);
1383 if (g_strcmp0(key, "BSSID") == 0) {
1384 DBusMessageIter array;
1385 unsigned char *addr;
1388 dbus_message_iter_recurse(iter, &array);
1389 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1392 memcpy(bss->bssid, addr, addr_len);
1393 } else if (g_strcmp0(key, "SSID") == 0) {
1394 DBusMessageIter array;
1395 unsigned char *ssid;
1398 dbus_message_iter_recurse(iter, &array);
1399 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1401 if (ssid_len > 0 && ssid_len < 33) {
1402 memcpy(bss->ssid, ssid, ssid_len);
1403 bss->ssid_len = ssid_len;
1405 memset(bss->ssid, 0, sizeof(bss->ssid));
1408 } else if (g_strcmp0(key, "Capabilities") == 0) {
1409 dbus_uint16_t capabilities = 0x0000;
1411 dbus_message_iter_get_basic(iter, &capabilities);
1413 if (capabilities & IEEE80211_CAP_ESS)
1414 bss->mode = G_SUPPLICANT_MODE_INFRA;
1415 else if (capabilities & IEEE80211_CAP_IBSS)
1416 bss->mode = G_SUPPLICANT_MODE_IBSS;
1418 if (capabilities & IEEE80211_CAP_PRIVACY)
1419 bss->privacy = TRUE;
1420 } else if (g_strcmp0(key, "Mode") == 0) {
1421 const char *mode = NULL;
1423 dbus_message_iter_get_basic(iter, &mode);
1424 bss->mode = string2mode(mode);
1425 } else if (g_strcmp0(key, "Frequency") == 0) {
1426 dbus_uint16_t frequency = 0;
1428 dbus_message_iter_get_basic(iter, &frequency);
1429 bss->frequency = frequency;
1430 } else if (g_strcmp0(key, "Signal") == 0) {
1431 dbus_int16_t signal = 0;
1433 dbus_message_iter_get_basic(iter, &signal);
1435 bss->signal = signal;
1436 } else if (g_strcmp0(key, "Level") == 0) {
1437 dbus_int32_t level = 0;
1439 dbus_message_iter_get_basic(iter, &level);
1440 } else if (g_strcmp0(key, "Rates") == 0) {
1441 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1442 } else if (g_strcmp0(key, "MaxRate") == 0) {
1443 dbus_uint32_t maxrate = 0;
1445 dbus_message_iter_get_basic(iter, &maxrate);
1447 bss->maxrate = maxrate;
1448 } else if (g_strcmp0(key, "Privacy") == 0) {
1449 dbus_bool_t privacy = FALSE;
1451 dbus_message_iter_get_basic(iter, &privacy);
1452 bss->privacy = privacy;
1453 } else if ((g_strcmp0(key, "RSN") == 0) ||
1454 (g_strcmp0(key, "WPA") == 0)) {
1455 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1458 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1459 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1460 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1461 bss->ieee8021x = TRUE;
1464 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1465 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1466 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1468 } else if (g_strcmp0(key, "IEs") == 0)
1469 bss_process_ies(iter, bss);
1471 SUPPLICANT_DBG("key %s type %c",
1472 key, dbus_message_iter_get_arg_type(iter));
1475 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1478 GSupplicantInterface *interface = user_data;
1479 GSupplicantNetwork *network;
1480 struct g_supplicant_bss *bss;
1481 const char *path = NULL;
1485 dbus_message_iter_get_basic(iter, &path);
1489 if (g_strcmp0(path, "/") == 0)
1492 SUPPLICANT_DBG("%s", path);
1494 network = g_hash_table_lookup(interface->bss_mapping, path);
1495 if (network != NULL) {
1496 bss = g_hash_table_lookup(network->bss_table, path);
1501 bss = g_try_new0(struct g_supplicant_bss, 1);
1505 bss->interface = interface;
1506 bss->path = g_strdup(path);
1511 static void interface_bss_added_with_keys(DBusMessageIter *iter,
1514 struct g_supplicant_bss *bss;
1518 bss = interface_bss_added(iter, user_data);
1522 dbus_message_iter_next(iter);
1524 if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
1527 supplicant_dbus_property_foreach(iter, bss_property, bss);
1529 bss_compute_security(bss);
1530 add_or_replace_bss_to_network(bss);
1533 static void interface_bss_added_without_keys(DBusMessageIter *iter,
1536 struct g_supplicant_bss *bss;
1540 bss = interface_bss_added(iter, user_data);
1544 supplicant_dbus_property_get_all(bss->path,
1545 SUPPLICANT_INTERFACE ".BSS",
1548 bss_compute_security(bss);
1549 add_or_replace_bss_to_network(bss);
1552 static void update_signal(gpointer key, gpointer value,
1555 struct g_supplicant_bss *bss = value;
1556 GSupplicantNetwork *network = user_data;
1558 if (bss->signal > network->signal) {
1559 network->signal = bss->signal;
1560 network->best_bss = bss;
1564 static void update_network_signal(GSupplicantNetwork *network)
1566 if (g_hash_table_size(network->bss_table) <= 1)
1569 g_hash_table_foreach(network->bss_table,
1570 update_signal, network);
1572 SUPPLICANT_DBG("New network signal %d", network->signal);
1575 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1577 GSupplicantInterface *interface = user_data;
1578 GSupplicantNetwork *network;
1579 const char *path = NULL;
1581 dbus_message_iter_get_basic(iter, &path);
1585 network = g_hash_table_lookup(interface->bss_mapping, path);
1586 if (network == NULL)
1589 g_hash_table_remove(bss_mapping, path);
1591 g_hash_table_remove(interface->bss_mapping, path);
1592 g_hash_table_remove(network->bss_table, path);
1594 update_network_signal(network);
1596 if (g_hash_table_size(network->bss_table) == 0)
1597 g_hash_table_remove(interface->network_table, network->group);
1600 static void interface_property(const char *key, DBusMessageIter *iter,
1603 GSupplicantInterface *interface = user_data;
1605 if (interface == NULL)
1608 SUPPLICANT_DBG("%s", key);
1611 debug_strvalmap("KeyMgmt capability", keymgmt_map,
1612 interface->keymgmt_capa);
1613 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1614 interface->authalg_capa);
1615 debug_strvalmap("Protocol capability", proto_capa_map,
1616 interface->proto_capa);
1617 debug_strvalmap("Pairwise capability", pairwise_map,
1618 interface->pairwise_capa);
1619 debug_strvalmap("Group capability", group_map,
1620 interface->group_capa);
1621 debug_strvalmap("Scan capability", scan_capa_map,
1622 interface->scan_capa);
1623 debug_strvalmap("Mode capability", mode_capa_map,
1624 interface->mode_capa);
1626 interface->ready = TRUE;
1627 callback_interface_added(interface);
1631 if (g_strcmp0(key, "Capabilities") == 0) {
1632 supplicant_dbus_property_foreach(iter, interface_capability,
1634 } else if (g_strcmp0(key, "State") == 0) {
1635 const char *str = NULL;
1637 dbus_message_iter_get_basic(iter, &str);
1639 if (string2state(str) != interface->state) {
1640 interface->state = string2state(str);
1641 callback_interface_state(interface);
1644 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
1645 } else if (g_strcmp0(key, "Scanning") == 0) {
1646 dbus_bool_t scanning = FALSE;
1648 dbus_message_iter_get_basic(iter, &scanning);
1649 interface->scanning = scanning;
1651 if (interface->ready == TRUE) {
1652 if (interface->scanning == TRUE)
1653 callback_scan_started(interface);
1655 callback_scan_finished(interface);
1657 } else if (g_strcmp0(key, "ApScan") == 0) {
1660 dbus_message_iter_get_basic(iter, &apscan);
1661 interface->apscan = apscan;
1662 } else if (g_strcmp0(key, "Ifname") == 0) {
1663 const char *str = NULL;
1665 dbus_message_iter_get_basic(iter, &str);
1667 g_free(interface->ifname);
1668 interface->ifname = g_strdup(str);
1670 } else if (g_strcmp0(key, "Driver") == 0) {
1671 const char *str = NULL;
1673 dbus_message_iter_get_basic(iter, &str);
1675 g_free(interface->driver);
1676 interface->driver = g_strdup(str);
1678 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1679 const char *str = NULL;
1681 dbus_message_iter_get_basic(iter, &str);
1683 g_free(interface->bridge);
1684 interface->bridge = g_strdup(str);
1686 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1687 interface_bss_added_without_keys(iter, interface);
1688 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1689 interface_network_added(iter, interface);
1690 } else if (g_strcmp0(key, "BSSs") == 0) {
1691 supplicant_dbus_array_foreach(iter, interface_bss_added_without_keys,
1693 } else if (g_strcmp0(key, "Blobs") == 0) {
1695 } else if (g_strcmp0(key, "Networks") == 0) {
1696 supplicant_dbus_array_foreach(iter, interface_network_added,
1699 SUPPLICANT_DBG("key %s type %c",
1700 key, dbus_message_iter_get_arg_type(iter));
1703 static void scan_network_update(DBusMessageIter *iter, void *user_data)
1705 GSupplicantInterface *interface = user_data;
1706 GSupplicantNetwork *network;
1712 dbus_message_iter_get_basic(iter, &path);
1717 if (g_strcmp0(path, "/") == 0)
1720 /* Update the network details based on scan BSS data */
1721 network = g_hash_table_lookup(interface->bss_mapping, path);
1722 if (network != NULL)
1723 callback_network_added(network);
1726 static void scan_bss_data(const char *key, DBusMessageIter *iter,
1729 GSupplicantInterface *interface = user_data;
1732 supplicant_dbus_array_foreach(iter, scan_network_update,
1735 if (interface->scan_callback != NULL)
1736 interface->scan_callback(0, interface, interface->scan_data);
1738 interface->scan_callback = NULL;
1739 interface->scan_data = NULL;
1742 static GSupplicantInterface *interface_alloc(const char *path)
1744 GSupplicantInterface *interface;
1746 interface = g_try_new0(GSupplicantInterface, 1);
1747 if (interface == NULL)
1750 interface->path = g_strdup(path);
1752 interface->network_table = g_hash_table_new_full(g_str_hash,
1753 g_str_equal, NULL, remove_network);
1755 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1757 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1760 g_hash_table_replace(interface_table, interface->path, interface);
1765 static void interface_added(DBusMessageIter *iter, void *user_data)
1767 GSupplicantInterface *interface;
1768 const char *path = NULL;
1772 dbus_message_iter_get_basic(iter, &path);
1776 if (g_strcmp0(path, "/") == 0)
1779 interface = g_hash_table_lookup(interface_table, path);
1780 if (interface != NULL)
1783 interface = interface_alloc(path);
1784 if (interface == NULL)
1787 dbus_message_iter_next(iter);
1788 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1789 supplicant_dbus_property_foreach(iter, interface_property,
1791 interface_property(NULL, NULL, interface);
1795 supplicant_dbus_property_get_all(path,
1796 SUPPLICANT_INTERFACE ".Interface",
1797 interface_property, interface);
1800 static void interface_removed(DBusMessageIter *iter, void *user_data)
1802 const char *path = NULL;
1804 dbus_message_iter_get_basic(iter, &path);
1808 g_hash_table_remove(interface_table, path);
1811 static void eap_method(DBusMessageIter *iter, void *user_data)
1813 const char *str = NULL;
1816 dbus_message_iter_get_basic(iter, &str);
1820 for (i = 0; eap_method_map[i].str != NULL; i++)
1821 if (strcmp(str, eap_method_map[i].str) == 0) {
1822 eap_methods |= eap_method_map[i].val;
1827 static void service_property(const char *key, DBusMessageIter *iter,
1831 callback_system_ready();
1835 if (g_strcmp0(key, "DebugLevel") == 0) {
1836 const char *str = NULL;
1839 dbus_message_iter_get_basic(iter, &str);
1840 for (i = 0; debug_strings[i] != NULL; i++)
1841 if (g_strcmp0(debug_strings[i], str) == 0) {
1845 SUPPLICANT_DBG("Debug level %d", debug_level);
1846 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1847 dbus_message_iter_get_basic(iter, &debug_timestamp);
1848 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
1849 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1850 dbus_message_iter_get_basic(iter, &debug_showkeys);
1851 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
1852 } else if (g_strcmp0(key, "Interfaces") == 0) {
1853 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1854 } else if (g_strcmp0(key, "EapMethods") == 0) {
1855 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1856 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1857 } else if (g_strcmp0(key, "Country") == 0) {
1858 const char *country = NULL;
1860 dbus_message_iter_get_basic(iter, &country);
1861 SUPPLICANT_DBG("Country %s", country);
1863 SUPPLICANT_DBG("key %s type %c",
1864 key, dbus_message_iter_get_arg_type(iter));
1867 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1869 const char *name = NULL, *old = NULL, *new = NULL;
1873 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1876 dbus_message_iter_get_basic(iter, &name);
1880 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1883 dbus_message_iter_next(iter);
1884 dbus_message_iter_get_basic(iter, &old);
1885 dbus_message_iter_next(iter);
1886 dbus_message_iter_get_basic(iter, &new);
1888 if (old == NULL || new == NULL)
1891 if (strlen(old) > 0 && strlen(new) == 0) {
1892 system_available = FALSE;
1893 g_hash_table_remove_all(bss_mapping);
1894 g_hash_table_remove_all(interface_table);
1895 callback_system_killed();
1898 if (strlen(new) > 0 && strlen(old) == 0) {
1899 system_available = TRUE;
1900 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1901 SUPPLICANT_INTERFACE,
1902 service_property, NULL);
1906 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1910 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1913 supplicant_dbus_property_foreach(iter, service_property, NULL);
1916 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1918 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
1920 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1921 interface_added(iter, NULL);
1924 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1928 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1929 interface_removed(iter, NULL);
1932 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1934 GSupplicantInterface *interface;
1938 interface = g_hash_table_lookup(interface_table, path);
1939 if (interface == NULL)
1942 supplicant_dbus_property_foreach(iter, interface_property, interface);
1945 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1947 GSupplicantInterface *interface;
1948 dbus_bool_t success = FALSE;
1952 interface = g_hash_table_lookup(interface_table, path);
1953 if (interface == NULL)
1956 dbus_message_iter_get_basic(iter, &success);
1959 * If scan is unsuccessful return -EIO else get the scanned BSSs
1960 * and update the network details accordingly
1962 if (success == FALSE) {
1963 if (interface->scan_callback != NULL)
1964 interface->scan_callback(-EIO, interface,
1965 interface->scan_data);
1967 interface->scan_callback = NULL;
1968 interface->scan_data = NULL;
1973 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
1974 "BSSs", scan_bss_data, interface);
1977 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1979 GSupplicantInterface *interface;
1983 interface = g_hash_table_lookup(interface_table, path);
1984 if (interface == NULL)
1987 interface_bss_added_with_keys(iter, interface);
1990 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1992 GSupplicantInterface *interface;
1996 interface = g_hash_table_lookup(interface_table, path);
1997 if (interface == NULL)
2000 interface_bss_removed(iter, interface);
2003 static void signal_network_added(const char *path, DBusMessageIter *iter)
2005 GSupplicantInterface *interface;
2009 interface = g_hash_table_lookup(interface_table, path);
2010 if (interface == NULL)
2013 interface_network_added(iter, interface);
2016 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2018 GSupplicantInterface *interface;
2022 interface = g_hash_table_lookup(interface_table, path);
2023 if (interface == NULL)
2026 interface_network_removed(iter, interface);
2029 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2031 GSupplicantInterface *interface;
2032 GSupplicantNetwork *network;
2033 GSupplicantSecurity old_security;
2034 struct g_supplicant_bss *bss;
2038 interface = g_hash_table_lookup(bss_mapping, path);
2039 if (interface == NULL)
2042 network = g_hash_table_lookup(interface->bss_mapping, path);
2043 if (network == NULL)
2046 bss = g_hash_table_lookup(network->bss_table, path);
2050 supplicant_dbus_property_foreach(iter, bss_property, bss);
2052 old_security = network->security;
2053 bss_compute_security(bss);
2055 if (old_security != bss->security) {
2056 struct g_supplicant_bss *new_bss;
2058 SUPPLICANT_DBG("New network security for %s", bss->ssid);
2060 /* Security change policy:
2061 * - we first copy the current bss into a new one with
2062 * its own pointer (path)
2063 * - we remove the current bss related nework which will
2064 * tell the plugin about such removal. This is done due
2065 * to the fact that a security change means a group change
2066 * so a complete network change.
2067 * (current bss becomes invalid as well)
2068 * - we add the new bss: it adds new network and tell the
2069 * plugin about it. */
2071 new_bss = g_try_new0(struct g_supplicant_bss, 1);
2072 if (new_bss == NULL)
2075 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
2076 new_bss->path = g_strdup(bss->path);
2078 g_hash_table_remove(interface->network_table, network->group);
2080 add_or_replace_bss_to_network(new_bss);
2085 if (bss->signal == network->signal)
2089 * If the new signal is lower than the SSID signal, we need
2090 * to check for the new maximum.
2092 if (bss->signal < network->signal) {
2093 if (bss != network->best_bss)
2095 network->signal = bss->signal;
2096 update_network_signal(network);
2098 network->signal = bss->signal;
2099 network->best_bss = bss;
2102 SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid, network->signal);
2104 callback_network_changed(network, "Signal");
2107 static void wps_credentials(const char *key, DBusMessageIter *iter,
2110 GSupplicantInterface *interface = user_data;
2115 SUPPLICANT_DBG("key %s", key);
2117 if (g_strcmp0(key, "Key") == 0) {
2118 DBusMessageIter array;
2122 dbus_message_iter_recurse(iter, &array);
2123 dbus_message_iter_get_fixed_array(&array, &key, &key_len);
2125 g_free(interface->wps_cred.key);
2126 interface->wps_cred.key = g_try_malloc0(
2127 sizeof(char) * key_len+1);
2129 if (interface->wps_cred.key == NULL)
2132 memcpy(interface->wps_cred.key, key, sizeof(char) * key_len);
2134 SUPPLICANT_DBG("WPS key present");
2135 } else if (g_strcmp0(key, "SSID") == 0) {
2136 DBusMessageIter array;
2137 unsigned char *ssid;
2140 dbus_message_iter_recurse(iter, &array);
2141 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2143 if (ssid_len > 0 && ssid_len < 33) {
2144 memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2145 interface->wps_cred.ssid_len = ssid_len;
2147 memset(interface->wps_cred.ssid, 0, 32);
2148 interface->wps_cred.ssid_len = 0;
2153 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2155 GSupplicantInterface *interface;
2159 interface = g_hash_table_lookup(interface_table, path);
2160 if (interface == NULL)
2163 supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2166 static void wps_event_args(const char *key, DBusMessageIter *iter,
2169 GSupplicantInterface *interface = user_data;
2171 if (key == NULL || interface == NULL)
2174 SUPPLICANT_DBG("Arg Key %s", key);
2177 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2179 GSupplicantInterface *interface;
2180 const char *name = NULL;
2184 interface = g_hash_table_lookup(interface_table, path);
2185 if (interface == NULL)
2188 dbus_message_iter_get_basic(iter, &name);
2190 SUPPLICANT_DBG("Name: %s", name);
2192 if (g_strcmp0(name, "success") == 0)
2193 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2194 else if (g_strcmp0(name, "failed") == 0)
2195 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2197 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2199 if (!dbus_message_iter_has_next(iter))
2202 dbus_message_iter_next(iter);
2204 supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2208 const char *interface;
2210 void (*function) (const char *path, DBusMessageIter *iter);
2212 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
2214 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
2215 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
2216 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
2217 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
2219 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
2220 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
2221 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
2222 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
2223 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
2224 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
2226 { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed },
2228 { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
2229 { SUPPLICANT_INTERFACE ".Interface.WPS", "Event", signal_wps_event },
2234 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
2235 DBusMessage *message, void *data)
2237 DBusMessageIter iter;
2241 path = dbus_message_get_path(message);
2243 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2245 if (dbus_message_iter_init(message, &iter) == FALSE)
2246 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2248 for (i = 0; signal_map[i].interface != NULL; i++) {
2249 if (dbus_message_has_interface(message,
2250 signal_map[i].interface) == FALSE)
2253 if (dbus_message_has_member(message,
2254 signal_map[i].member) == FALSE)
2257 signal_map[i].function(path, &iter);
2261 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2264 struct supplicant_regdom {
2265 GSupplicantCountryCallback callback;
2266 const void *user_data;
2269 static void country_result(const char *error,
2270 DBusMessageIter *iter, void *user_data)
2272 struct supplicant_regdom *regdom = user_data;
2275 SUPPLICANT_DBG("Country setting result");
2277 if (user_data == NULL)
2280 if (error == NULL) {
2281 alpha2 = (char *)regdom->user_data;
2283 SUPPLICANT_DBG("Country setting failure %s", error);
2287 if (regdom->callback)
2288 regdom->callback(alpha2);
2293 static void country_params(DBusMessageIter *iter, void *user_data)
2295 struct supplicant_regdom *regdom = user_data;
2296 const char *country;
2298 country = regdom->user_data;
2300 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &country);
2303 int g_supplicant_set_country(const char *alpha2,
2304 GSupplicantCountryCallback callback,
2305 const void *user_data)
2307 struct supplicant_regdom *regdom;
2309 SUPPLICANT_DBG("Country setting %s", alpha2);
2311 if (system_available == FALSE)
2314 regdom = dbus_malloc0(sizeof(*regdom));
2318 regdom->callback = callback;
2319 regdom->user_data = user_data;
2321 return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
2322 "Country", DBUS_TYPE_STRING_AS_STRING,
2323 country_params, country_result,
2327 struct interface_data {
2328 GSupplicantInterface *interface;
2329 GSupplicantInterfaceCallback callback;
2333 struct interface_create_data {
2337 GSupplicantInterface *interface;
2338 GSupplicantInterfaceCallback callback;
2342 struct interface_connect_data {
2343 GSupplicantInterface *interface;
2344 GSupplicantInterfaceCallback callback;
2345 GSupplicantSSID *ssid;
2349 struct interface_scan_data {
2350 GSupplicantInterface *interface;
2351 GSupplicantInterfaceCallback callback;
2352 GSupplicantScanParams *scan_params;
2356 static void interface_create_property(const char *key, DBusMessageIter *iter,
2359 struct interface_create_data *data = user_data;
2360 GSupplicantInterface *interface = data->interface;
2363 if (data->callback != NULL)
2364 data->callback(0, data->interface, data->user_data);
2369 interface_property(key, iter, interface);
2372 static void interface_create_result(const char *error,
2373 DBusMessageIter *iter, void *user_data)
2375 struct interface_create_data *data = user_data;
2376 const char *path = NULL;
2381 if (error != NULL) {
2382 g_critical("error %s", error);
2387 dbus_message_iter_get_basic(iter, &path);
2393 if (system_available == FALSE) {
2398 data->interface = g_hash_table_lookup(interface_table, path);
2399 if (data->interface == NULL) {
2400 data->interface = interface_alloc(path);
2401 if (data->interface == NULL) {
2407 err = supplicant_dbus_property_get_all(path,
2408 SUPPLICANT_INTERFACE ".Interface",
2409 interface_create_property, data);
2414 if (data->callback != NULL)
2415 data->callback(err, NULL, data->user_data);
2420 static void interface_create_params(DBusMessageIter *iter, void *user_data)
2422 struct interface_create_data *data = user_data;
2423 DBusMessageIter dict;
2427 supplicant_dbus_dict_open(iter, &dict);
2429 supplicant_dbus_dict_append_basic(&dict, "Ifname",
2430 DBUS_TYPE_STRING, &data->ifname);
2432 if (data->driver != NULL)
2433 supplicant_dbus_dict_append_basic(&dict, "Driver",
2434 DBUS_TYPE_STRING, &data->driver);
2436 if (data->bridge != NULL)
2437 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
2438 DBUS_TYPE_STRING, &data->bridge);
2440 supplicant_dbus_dict_close(iter, &dict);
2443 static void interface_get_result(const char *error,
2444 DBusMessageIter *iter, void *user_data)
2446 struct interface_create_data *data = user_data;
2447 GSupplicantInterface *interface;
2448 const char *path = NULL;
2453 if (error != NULL) {
2454 SUPPLICANT_DBG("Interface not created yet");
2458 dbus_message_iter_get_basic(iter, &path);
2464 interface = g_hash_table_lookup(interface_table, path);
2465 if (interface == NULL) {
2470 if (data->callback != NULL)
2471 data->callback(0, interface, data->user_data);
2478 if (system_available == FALSE) {
2483 SUPPLICANT_DBG("Creating interface");
2485 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
2486 SUPPLICANT_INTERFACE,
2488 interface_create_params,
2489 interface_create_result, data);
2494 if (data->callback != NULL)
2495 data->callback(err, NULL, data->user_data);
2500 static void interface_get_params(DBusMessageIter *iter, void *user_data)
2502 struct interface_create_data *data = user_data;
2506 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
2509 int g_supplicant_interface_create(const char *ifname, const char *driver,
2511 GSupplicantInterfaceCallback callback,
2514 struct interface_create_data *data;
2516 SUPPLICANT_DBG("ifname %s", ifname);
2521 if (system_available == FALSE)
2524 data = dbus_malloc0(sizeof(*data));
2528 data->ifname = ifname;
2529 data->driver = driver;
2530 data->bridge = bridge;
2531 data->callback = callback;
2532 data->user_data = user_data;
2534 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2535 SUPPLICANT_INTERFACE,
2537 interface_get_params,
2538 interface_get_result, data);
2541 static void interface_remove_result(const char *error,
2542 DBusMessageIter *iter, void *user_data)
2544 struct interface_data *data = user_data;
2547 if (error != NULL) {
2552 if (system_available == FALSE) {
2558 * The gsupplicant interface is already freed by the InterfaceRemoved
2559 * signal callback. Simply invoke the interface_data callback.
2564 if (data->callback != NULL)
2565 data->callback(err, NULL, data->user_data);
2571 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
2573 struct interface_data *data = user_data;
2575 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2576 &data->interface->path);
2580 int g_supplicant_interface_remove(GSupplicantInterface *interface,
2581 GSupplicantInterfaceCallback callback,
2584 struct interface_data *data;
2586 if (interface == NULL)
2589 if (system_available == FALSE)
2592 data = dbus_malloc0(sizeof(*data));
2596 data->interface = interface;
2597 data->callback = callback;
2598 data->user_data = user_data;
2600 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2601 SUPPLICANT_INTERFACE,
2603 interface_remove_params,
2604 interface_remove_result, data);
2607 static void interface_scan_result(const char *error,
2608 DBusMessageIter *iter, void *user_data)
2610 struct interface_scan_data *data = user_data;
2612 if (error != NULL) {
2613 SUPPLICANT_DBG("error %s", error);
2615 if (data->callback != NULL)
2616 data->callback(-EIO, data->interface, data->user_data);
2618 data->interface->scan_callback = data->callback;
2619 data->interface->scan_data = data->user_data;
2622 if (data != NULL && data->scan_params != NULL)
2623 g_supplicant_free_scan_params(data->scan_params);
2628 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
2630 DBusMessageIter data;
2631 unsigned int width = 0; /* Not used by wpa_supplicant atm */
2633 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
2635 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
2636 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
2638 dbus_message_iter_close_container(iter, &data);
2641 static void add_scan_frequencies(DBusMessageIter *iter,
2644 GSupplicantScanParams *scan_data = user_data;
2648 for (i = 0; i < scan_data->num_ssids; i++) {
2649 freq = scan_data->freqs[i];
2653 add_scan_frequency(iter, freq);
2657 static void append_ssid(DBusMessageIter *iter,
2658 const void *ssid, unsigned int len)
2660 DBusMessageIter array;
2662 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
2663 DBUS_TYPE_BYTE_AS_STRING, &array);
2665 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
2667 dbus_message_iter_close_container(iter, &array);
2670 static void append_ssids(DBusMessageIter *iter, void *user_data)
2672 GSupplicantScanParams *scan_data = user_data;
2675 for (list = scan_data->ssids; list; list = list->next) {
2676 struct scan_ssid *scan_ssid = list->data;
2678 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
2682 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
2683 supplicant_dbus_array_function function,
2686 GSupplicantScanParams *scan_params = user_data;
2687 DBusMessageIter entry, value, array;
2688 const char *key = "Channels";
2690 if (scan_params->freqs && scan_params->freqs[0] != 0) {
2691 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
2694 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
2696 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
2697 DBUS_TYPE_ARRAY_AS_STRING
2698 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2699 DBUS_TYPE_UINT32_AS_STRING
2700 DBUS_TYPE_UINT32_AS_STRING
2701 DBUS_STRUCT_END_CHAR_AS_STRING,
2704 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
2705 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2706 DBUS_TYPE_UINT32_AS_STRING
2707 DBUS_TYPE_UINT32_AS_STRING
2708 DBUS_STRUCT_END_CHAR_AS_STRING,
2712 function(&array, user_data);
2714 dbus_message_iter_close_container(&value, &array);
2715 dbus_message_iter_close_container(&entry, &value);
2716 dbus_message_iter_close_container(dict, &entry);
2720 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
2722 DBusMessageIter dict;
2723 const char *type = "passive";
2724 struct interface_scan_data *data = user_data;
2726 supplicant_dbus_dict_open(iter, &dict);
2728 if (data && data->scan_params) {
2731 supplicant_dbus_dict_append_basic(&dict, "Type",
2732 DBUS_TYPE_STRING, &type);
2734 supplicant_dbus_dict_append_array(&dict, "SSIDs",
2739 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
2742 supplicant_dbus_dict_append_basic(&dict, "Type",
2743 DBUS_TYPE_STRING, &type);
2745 supplicant_dbus_dict_close(iter, &dict);
2748 int g_supplicant_interface_scan(GSupplicantInterface *interface,
2749 GSupplicantScanParams *scan_data,
2750 GSupplicantInterfaceCallback callback,
2753 struct interface_scan_data *data;
2756 if (interface == NULL)
2759 if (system_available == FALSE)
2762 if (interface->scanning == TRUE)
2765 switch (interface->state) {
2766 case G_SUPPLICANT_STATE_AUTHENTICATING:
2767 case G_SUPPLICANT_STATE_ASSOCIATING:
2768 case G_SUPPLICANT_STATE_ASSOCIATED:
2769 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2770 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2772 case G_SUPPLICANT_STATE_UNKNOWN:
2773 case G_SUPPLICANT_STATE_DISCONNECTED:
2774 case G_SUPPLICANT_STATE_INACTIVE:
2775 case G_SUPPLICANT_STATE_SCANNING:
2776 case G_SUPPLICANT_STATE_COMPLETED:
2780 data = dbus_malloc0(sizeof(*data));
2784 data->interface = interface;
2785 data->callback = callback;
2786 data->user_data = user_data;
2787 data->scan_params = scan_data;
2789 ret = supplicant_dbus_method_call(interface->path,
2790 SUPPLICANT_INTERFACE ".Interface", "Scan",
2791 interface_scan_params, interface_scan_result, data);
2799 static int parse_supplicant_error(DBusMessageIter *iter)
2801 int err = -ECANCELED;
2804 /* If the given passphrase is malformed wpa_s returns
2805 * "invalid message format" but this error should be interpreted as
2808 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
2809 dbus_message_iter_get_basic(iter, &key);
2810 if (strncmp(key, "psk", 3) == 0 ||
2811 strncmp(key, "wep_key", 7) == 0 ||
2812 strcmp(key, "invalid message format") == 0) {
2816 dbus_message_iter_next(iter);
2822 static void interface_select_network_result(const char *error,
2823 DBusMessageIter *iter, void *user_data)
2825 struct interface_connect_data *data = user_data;
2831 if (error != NULL) {
2832 SUPPLICANT_DBG("SelectNetwork error %s", error);
2833 err = parse_supplicant_error(iter);
2836 if (data->callback != NULL)
2837 data->callback(err, data->interface, data->user_data);
2843 static void interface_select_network_params(DBusMessageIter *iter,
2846 struct interface_connect_data *data = user_data;
2847 GSupplicantInterface *interface = data->interface;
2849 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2850 &interface->network_path);
2853 static void interface_add_network_result(const char *error,
2854 DBusMessageIter *iter, void *user_data)
2856 struct interface_connect_data *data = user_data;
2857 GSupplicantInterface *interface = data->interface;
2864 dbus_message_iter_get_basic(iter, &path);
2868 SUPPLICANT_DBG("PATH: %s", path);
2870 g_free(interface->network_path);
2871 interface->network_path = g_strdup(path);
2873 supplicant_dbus_method_call(data->interface->path,
2874 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
2875 interface_select_network_params,
2876 interface_select_network_result, data);
2881 SUPPLICANT_DBG("AddNetwork error %s", error);
2882 err = parse_supplicant_error(iter);
2883 if (data->callback != NULL)
2884 data->callback(err, data->interface, data->user_data);
2886 g_free(interface->network_path);
2887 interface->network_path = NULL;
2892 static void add_network_security_wep(DBusMessageIter *dict,
2893 GSupplicantSSID *ssid)
2895 const char *auth_alg = "OPEN SHARED";
2896 const char *key_index = "0";
2898 supplicant_dbus_dict_append_basic(dict, "auth_alg",
2899 DBUS_TYPE_STRING, &auth_alg);
2901 if (ssid->passphrase) {
2902 int size = strlen(ssid->passphrase);
2903 if (size == 10 || size == 26) {
2904 unsigned char *key = g_try_malloc(13);
2908 memset(tmp, 0, sizeof(tmp));
2912 for (i = 0; i < size / 2; i++) {
2913 memcpy(tmp, ssid->passphrase + (i * 2), 2);
2914 key[i] = (unsigned char) strtol(tmp, NULL, 16);
2917 supplicant_dbus_dict_append_fixed_array(dict,
2922 } else if (size == 5 || size == 13) {
2923 unsigned char *key = g_try_malloc(13);
2929 for (i = 0; i < size; i++)
2930 key[i] = (unsigned char) ssid->passphrase[i];
2932 supplicant_dbus_dict_append_fixed_array(dict,
2938 supplicant_dbus_dict_append_basic(dict,
2943 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
2944 DBUS_TYPE_STRING, &key_index);
2948 static dbus_bool_t is_psk_raw_key(const char *psk)
2952 /* A raw key is always 64 bytes length... */
2953 if (strlen(psk) != 64)
2956 /* ... and its content is in hex representation */
2957 for (i = 0; i < 64; i++)
2958 if (!isxdigit((unsigned char) psk[i]))
2964 static unsigned char hexchar2bin(char c)
2966 if ((c >= '0') && (c <= '9'))
2968 else if ((c >= 'A') && (c <= 'F'))
2969 return (c - 'A' + 10);
2970 else if ((c >= 'a') && (c <= 'f'))
2971 return (c - 'a' + 10);
2976 static void hexstring2bin(const char *string, unsigned char *data, size_t data_len)
2980 if ((data != NULL) && (string != NULL))
2981 for (i = 0; i < data_len; i++)
2982 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
2983 hexchar2bin(string[i * 2 + 1]) << 0);
2986 static void add_network_security_psk(DBusMessageIter *dict,
2987 GSupplicantSSID *ssid)
2989 if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
2990 const char *key = "psk";
2992 if (is_psk_raw_key(ssid->passphrase) == TRUE) {
2993 const size_t size = 32;
2994 unsigned char data[size];
2995 unsigned char *datap = data;
2997 hexstring2bin(ssid->passphrase, datap, size);
2999 supplicant_dbus_dict_append_fixed_array(dict,
3000 key, DBUS_TYPE_BYTE,
3003 supplicant_dbus_dict_append_basic(dict,
3004 key, DBUS_TYPE_STRING,
3009 static void add_network_security_tls(DBusMessageIter *dict,
3010 GSupplicantSSID *ssid)
3013 * For TLS, we at least need:
3014 * The client certificate
3015 * The client private key file
3016 * The client private key file password
3018 * The Authority certificate is optional.
3020 if (ssid->client_cert_path == NULL)
3023 if (ssid->private_key_path == NULL)
3026 if (ssid->private_key_passphrase == NULL)
3029 if (ssid->ca_cert_path)
3030 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3031 DBUS_TYPE_STRING, &ssid->ca_cert_path);
3033 supplicant_dbus_dict_append_basic(dict, "private_key",
3035 &ssid->private_key_path);
3036 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3038 &ssid->private_key_passphrase);
3039 supplicant_dbus_dict_append_basic(dict, "client_cert",
3041 &ssid->client_cert_path);
3044 static void add_network_security_peap(DBusMessageIter *dict,
3045 GSupplicantSSID *ssid)
3050 * For PEAP/TTLS, we at least need
3051 * The authority certificate
3052 * The 2nd phase authentication method
3053 * The 2nd phase passphrase
3055 * The Client certificate is optional although strongly recommended
3056 * When setting it, we need in addition
3057 * The Client private key file
3058 * The Client private key file password
3060 if (ssid->passphrase == NULL)
3063 if (ssid->phase2_auth == NULL)
3066 if (ssid->client_cert_path) {
3067 if (ssid->private_key_path == NULL)
3070 if (ssid->private_key_passphrase == NULL)
3073 supplicant_dbus_dict_append_basic(dict, "client_cert",
3075 &ssid->client_cert_path);
3077 supplicant_dbus_dict_append_basic(dict, "private_key",
3079 &ssid->private_key_path);
3081 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3083 &ssid->private_key_passphrase);
3087 if (g_str_has_prefix(ssid->phase2_auth, "EAP-") == TRUE) {
3088 phase2_auth = g_strdup_printf("autheap=%s",
3089 ssid->phase2_auth + strlen("EAP-"));
3091 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
3093 supplicant_dbus_dict_append_basic(dict, "password",
3097 if (ssid->ca_cert_path)
3098 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3100 &ssid->ca_cert_path);
3102 supplicant_dbus_dict_append_basic(dict, "phase2",
3106 g_free(phase2_auth);
3109 static void add_network_security_eap(DBusMessageIter *dict,
3110 GSupplicantSSID *ssid)
3114 if (ssid->eap == NULL || ssid->identity == NULL)
3117 if (g_strcmp0(ssid->eap, "tls") == 0) {
3118 add_network_security_tls(dict, ssid);
3119 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
3120 g_strcmp0(ssid->eap, "ttls") == 0) {
3121 add_network_security_peap(dict, ssid);
3125 eap_value = g_ascii_strup(ssid->eap, -1);
3127 supplicant_dbus_dict_append_basic(dict, "eap",
3130 supplicant_dbus_dict_append_basic(dict, "identity",
3137 static void add_network_security_ciphers(DBusMessageIter *dict,
3138 GSupplicantSSID *ssid)
3140 unsigned int p_cipher, g_cipher, i;
3141 char *pairwise, *group;
3142 char *pair_ciphers[4];
3143 char *group_ciphers[5];
3145 p_cipher = ssid->pairwise_cipher;
3146 g_cipher = ssid->group_cipher;
3148 if (p_cipher == 0 && g_cipher == 0)
3153 if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
3154 pair_ciphers[i++] = "CCMP";
3156 if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
3157 pair_ciphers[i++] = "TKIP";
3159 if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
3160 pair_ciphers[i++] = "NONE";
3162 pair_ciphers[i] = NULL;
3166 if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
3167 group_ciphers[i++] = "CCMP";
3169 if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
3170 group_ciphers[i++] = "TKIP";
3172 if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
3173 group_ciphers[i++] = "WEP104";
3175 if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
3176 group_ciphers[i++] = "WEP40";
3178 group_ciphers[i] = NULL;
3180 pairwise = g_strjoinv(" ", pair_ciphers);
3181 group = g_strjoinv(" ", group_ciphers);
3183 SUPPLICANT_DBG("cipher %s %s", pairwise, group);
3185 supplicant_dbus_dict_append_basic(dict, "pairwise",
3188 supplicant_dbus_dict_append_basic(dict, "group",
3196 static void add_network_security_proto(DBusMessageIter *dict,
3197 GSupplicantSSID *ssid)
3199 unsigned int protocol, i;
3203 protocol = ssid->protocol;
3210 if (protocol & G_SUPPLICANT_PROTO_RSN)
3211 protos[i++] = "RSN";
3213 if (protocol & G_SUPPLICANT_PROTO_WPA)
3214 protos[i++] = "WPA";
3218 proto = g_strjoinv(" ", protos);
3220 SUPPLICANT_DBG("proto %s", proto);
3222 supplicant_dbus_dict_append_basic(dict, "proto",
3229 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
3233 switch (ssid->security) {
3234 case G_SUPPLICANT_SECURITY_UNKNOWN:
3235 case G_SUPPLICANT_SECURITY_NONE:
3236 case G_SUPPLICANT_SECURITY_WEP:
3238 add_network_security_wep(dict, ssid);
3239 add_network_security_ciphers(dict, ssid);
3241 case G_SUPPLICANT_SECURITY_PSK:
3242 key_mgmt = "WPA-PSK";
3243 add_network_security_psk(dict, ssid);
3244 add_network_security_ciphers(dict, ssid);
3245 add_network_security_proto(dict, ssid);
3247 case G_SUPPLICANT_SECURITY_IEEE8021X:
3248 key_mgmt = "WPA-EAP";
3249 add_network_security_eap(dict, ssid);
3250 add_network_security_ciphers(dict, ssid);
3251 add_network_security_proto(dict, ssid);
3255 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
3256 DBUS_TYPE_STRING, &key_mgmt);
3259 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
3263 switch (ssid->mode) {
3264 case G_SUPPLICANT_MODE_UNKNOWN:
3265 case G_SUPPLICANT_MODE_INFRA:
3268 case G_SUPPLICANT_MODE_IBSS:
3271 case G_SUPPLICANT_MODE_MASTER:
3276 supplicant_dbus_dict_append_basic(dict, "mode",
3277 DBUS_TYPE_UINT32, &mode);
3280 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
3282 DBusMessageIter dict;
3283 struct interface_connect_data *data = user_data;
3284 GSupplicantSSID *ssid = data->ssid;
3286 supplicant_dbus_dict_open(iter, &dict);
3288 if (ssid->scan_ssid)
3289 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
3290 DBUS_TYPE_UINT32, &ssid->scan_ssid);
3293 supplicant_dbus_dict_append_basic(&dict, "frequency",
3294 DBUS_TYPE_UINT32, &ssid->freq);
3296 if (ssid->bgscan != NULL)
3297 supplicant_dbus_dict_append_basic(&dict, "bgscan",
3298 DBUS_TYPE_STRING, &ssid->bgscan);
3300 add_network_mode(&dict, ssid);
3302 add_network_security(&dict, ssid);
3304 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
3305 DBUS_TYPE_BYTE, &ssid->ssid,
3308 supplicant_dbus_dict_close(iter, &dict);
3311 static void interface_wps_start_result(const char *error,
3312 DBusMessageIter *iter, void *user_data)
3314 struct interface_connect_data *data = user_data;
3318 SUPPLICANT_DBG("error: %s", error);
3324 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
3326 struct interface_connect_data *data = user_data;
3327 GSupplicantSSID *ssid = data->ssid;
3328 const char *role = "enrollee", *type;
3329 DBusMessageIter dict;
3333 supplicant_dbus_dict_open(iter, &dict);
3335 supplicant_dbus_dict_append_basic(&dict, "Role",
3336 DBUS_TYPE_STRING, &role);
3339 if (ssid->pin_wps != NULL) {
3341 supplicant_dbus_dict_append_basic(&dict, "Pin",
3342 DBUS_TYPE_STRING, &ssid->pin_wps);
3345 supplicant_dbus_dict_append_basic(&dict, "Type",
3346 DBUS_TYPE_STRING, &type);
3348 supplicant_dbus_dict_close(iter, &dict);
3351 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
3353 struct interface_connect_data *data = user_data;
3357 if (error != NULL) {
3358 SUPPLICANT_DBG("error: %s", error);
3364 supplicant_dbus_method_call(data->interface->path,
3365 SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
3366 interface_add_wps_params,
3367 interface_wps_start_result, data);
3370 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
3372 dbus_bool_t credentials = TRUE;
3376 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
3380 int g_supplicant_interface_connect(GSupplicantInterface *interface,
3381 GSupplicantSSID *ssid,
3382 GSupplicantInterfaceCallback callback,
3385 struct interface_connect_data *data;
3388 if (interface == NULL)
3391 if (system_available == FALSE)
3394 /* TODO: Check if we're already connected and switch */
3396 data = dbus_malloc0(sizeof(*data));
3400 data->interface = interface;
3401 data->callback = callback;
3403 data->user_data = user_data;
3405 if (ssid->use_wps == TRUE) {
3406 g_free(interface->wps_cred.key);
3407 memset(&interface->wps_cred, 0,
3408 sizeof(struct _GSupplicantWpsCredentials));
3410 ret = supplicant_dbus_property_set(interface->path,
3411 SUPPLICANT_INTERFACE ".Interface.WPS",
3412 "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
3413 wps_process_credentials, wps_start, data);
3415 ret = supplicant_dbus_method_call(interface->path,
3416 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
3417 interface_add_network_params,
3418 interface_add_network_result, data);
3423 return -EINPROGRESS;
3426 static void network_remove_result(const char *error,
3427 DBusMessageIter *iter, void *user_data)
3429 struct interface_data *data = user_data;
3437 if (data->callback != NULL)
3438 data->callback(result, data->interface, data->user_data);
3443 static void network_remove_params(DBusMessageIter *iter, void *user_data)
3445 struct interface_data *data = user_data;
3446 const char *path = data->interface->network_path;
3448 SUPPLICANT_DBG("path %s", path);
3450 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
3453 static int network_remove(struct interface_data *data)
3455 GSupplicantInterface *interface = data->interface;
3459 return supplicant_dbus_method_call(interface->path,
3460 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
3461 network_remove_params, network_remove_result, data);
3464 static void interface_disconnect_result(const char *error,
3465 DBusMessageIter *iter, void *user_data)
3467 struct interface_data *data = user_data;
3471 if (error != NULL && data->callback != NULL)
3472 data->callback(-EIO, data->interface, data->user_data);
3474 /* If we are disconnecting from previous WPS successful
3475 * association. i.e.: it did not went through AddNetwork,
3476 * and interface->network_path was never set. */
3477 if (data->interface->network_path == NULL) {
3482 network_remove(data);
3485 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
3486 GSupplicantInterfaceCallback callback,
3489 struct interface_data *data;
3493 if (interface == NULL)
3496 if (system_available == FALSE)
3499 data = dbus_malloc0(sizeof(*data));
3503 data->interface = interface;
3504 data->callback = callback;
3505 data->user_data = user_data;
3507 return supplicant_dbus_method_call(interface->path,
3508 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
3509 NULL, interface_disconnect_result, data);
3513 static const char *g_supplicant_rule0 = "type=signal,"
3514 "path=" DBUS_PATH_DBUS ","
3515 "sender=" DBUS_SERVICE_DBUS ","
3516 "interface=" DBUS_INTERFACE_DBUS ","
3517 "member=NameOwnerChanged,"
3518 "arg0=" SUPPLICANT_SERVICE;
3519 static const char *g_supplicant_rule1 = "type=signal,"
3520 "interface=" SUPPLICANT_INTERFACE;
3521 static const char *g_supplicant_rule2 = "type=signal,"
3522 "interface=" SUPPLICANT_INTERFACE ".Interface";
3523 static const char *g_supplicant_rule3 = "type=signal,"
3524 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
3525 static const char *g_supplicant_rule4 = "type=signal,"
3526 "interface=" SUPPLICANT_INTERFACE ".BSS";
3527 static const char *g_supplicant_rule5 = "type=signal,"
3528 "interface=" SUPPLICANT_INTERFACE ".Network";
3530 static void invoke_introspect_method(void)
3532 DBusMessage *message;
3534 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
3536 DBUS_INTERFACE_INTROSPECTABLE,
3539 if (message == NULL)
3542 dbus_message_set_no_reply(message, TRUE);
3543 dbus_connection_send(connection, message, NULL);
3544 dbus_message_unref(message);
3547 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
3549 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3550 if (connection == NULL)
3553 if (dbus_connection_add_filter(connection,
3554 g_supplicant_filter, NULL, NULL) == FALSE) {
3555 dbus_connection_unref(connection);
3560 callbacks_pointer = callbacks;
3563 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
3564 NULL, remove_interface);
3566 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
3569 supplicant_dbus_setup(connection);
3571 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
3572 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
3573 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
3574 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
3575 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
3576 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
3577 dbus_connection_flush(connection);
3579 if (dbus_bus_name_has_owner(connection,
3580 SUPPLICANT_SERVICE, NULL) == TRUE) {
3581 system_available = TRUE;
3582 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
3583 SUPPLICANT_INTERFACE,
3584 service_property, NULL);
3586 invoke_introspect_method();
3591 static void unregister_interface_remove_params(DBusMessageIter *iter,
3594 const char *path = user_data;
3596 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3601 static void unregister_remove_interface(gpointer key, gpointer value,
3604 GSupplicantInterface *interface = value;
3606 supplicant_dbus_method_call(SUPPLICANT_PATH,
3607 SUPPLICANT_INTERFACE,
3609 unregister_interface_remove_params,
3610 NULL, interface->path);
3613 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
3617 if (connection != NULL) {
3618 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
3619 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
3620 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
3621 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
3622 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
3623 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
3624 dbus_connection_flush(connection);
3626 dbus_connection_remove_filter(connection,
3627 g_supplicant_filter, NULL);
3630 if (bss_mapping != NULL) {
3631 g_hash_table_destroy(bss_mapping);
3635 if (system_available == TRUE)
3636 callback_system_killed();
3638 if (interface_table != NULL) {
3639 g_hash_table_foreach(interface_table,
3640 unregister_remove_interface, NULL);
3641 g_hash_table_destroy(interface_table);
3642 interface_table = NULL;
3645 if (connection != NULL) {
3646 dbus_connection_unref(connection);
3650 callbacks_pointer = NULL;