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");
2459 dbus_message_iter_get_basic(iter, &path);
2465 interface = g_hash_table_lookup(interface_table, path);
2466 if (interface == NULL) {
2471 if (data->callback != NULL)
2472 data->callback(0, interface, data->user_data);
2479 if (system_available == FALSE) {
2484 SUPPLICANT_DBG("Creating interface");
2486 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
2487 SUPPLICANT_INTERFACE,
2489 interface_create_params,
2490 interface_create_result, data);
2495 if (data->callback != NULL)
2496 data->callback(err, NULL, data->user_data);
2501 static void interface_get_params(DBusMessageIter *iter, void *user_data)
2503 struct interface_create_data *data = user_data;
2507 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
2510 int g_supplicant_interface_create(const char *ifname, const char *driver,
2512 GSupplicantInterfaceCallback callback,
2515 struct interface_create_data *data;
2517 SUPPLICANT_DBG("ifname %s", ifname);
2522 if (system_available == FALSE)
2525 data = dbus_malloc0(sizeof(*data));
2529 data->ifname = ifname;
2530 data->driver = driver;
2531 data->bridge = bridge;
2532 data->callback = callback;
2533 data->user_data = user_data;
2535 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2536 SUPPLICANT_INTERFACE,
2538 interface_get_params,
2539 interface_get_result, data);
2542 static void interface_remove_result(const char *error,
2543 DBusMessageIter *iter, void *user_data)
2545 struct interface_data *data = user_data;
2548 if (error != NULL) {
2553 if (system_available == FALSE) {
2559 * The gsupplicant interface is already freed by the InterfaceRemoved
2560 * signal callback. Simply invoke the interface_data callback.
2565 if (data->callback != NULL)
2566 data->callback(err, NULL, data->user_data);
2572 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
2574 struct interface_data *data = user_data;
2576 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2577 &data->interface->path);
2581 int g_supplicant_interface_remove(GSupplicantInterface *interface,
2582 GSupplicantInterfaceCallback callback,
2585 struct interface_data *data;
2587 if (interface == NULL)
2590 if (system_available == FALSE)
2593 data = dbus_malloc0(sizeof(*data));
2597 data->interface = interface;
2598 data->callback = callback;
2599 data->user_data = user_data;
2601 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2602 SUPPLICANT_INTERFACE,
2604 interface_remove_params,
2605 interface_remove_result, data);
2608 static void interface_scan_result(const char *error,
2609 DBusMessageIter *iter, void *user_data)
2611 struct interface_scan_data *data = user_data;
2613 if (error != NULL) {
2614 SUPPLICANT_DBG("error %s", error);
2616 if (data->callback != NULL)
2617 data->callback(-EIO, data->interface, data->user_data);
2619 data->interface->scan_callback = data->callback;
2620 data->interface->scan_data = data->user_data;
2623 if (data != NULL && data->scan_params != NULL)
2624 g_supplicant_free_scan_params(data->scan_params);
2629 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
2631 DBusMessageIter data;
2632 unsigned int width = 0; /* Not used by wpa_supplicant atm */
2634 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
2636 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
2637 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
2639 dbus_message_iter_close_container(iter, &data);
2642 static void add_scan_frequencies(DBusMessageIter *iter,
2645 GSupplicantScanParams *scan_data = user_data;
2649 for (i = 0; i < scan_data->num_ssids; i++) {
2650 freq = scan_data->freqs[i];
2654 add_scan_frequency(iter, freq);
2658 static void append_ssid(DBusMessageIter *iter,
2659 const void *ssid, unsigned int len)
2661 DBusMessageIter array;
2663 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
2664 DBUS_TYPE_BYTE_AS_STRING, &array);
2666 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
2668 dbus_message_iter_close_container(iter, &array);
2671 static void append_ssids(DBusMessageIter *iter, void *user_data)
2673 GSupplicantScanParams *scan_data = user_data;
2676 for (list = scan_data->ssids; list; list = list->next) {
2677 struct scan_ssid *scan_ssid = list->data;
2679 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
2683 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
2684 supplicant_dbus_array_function function,
2687 GSupplicantScanParams *scan_params = user_data;
2688 DBusMessageIter entry, value, array;
2689 const char *key = "Channels";
2691 if (scan_params->freqs && scan_params->freqs[0] != 0) {
2692 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
2695 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
2697 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
2698 DBUS_TYPE_ARRAY_AS_STRING
2699 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2700 DBUS_TYPE_UINT32_AS_STRING
2701 DBUS_TYPE_UINT32_AS_STRING
2702 DBUS_STRUCT_END_CHAR_AS_STRING,
2705 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
2706 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2707 DBUS_TYPE_UINT32_AS_STRING
2708 DBUS_TYPE_UINT32_AS_STRING
2709 DBUS_STRUCT_END_CHAR_AS_STRING,
2713 function(&array, user_data);
2715 dbus_message_iter_close_container(&value, &array);
2716 dbus_message_iter_close_container(&entry, &value);
2717 dbus_message_iter_close_container(dict, &entry);
2721 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
2723 DBusMessageIter dict;
2724 const char *type = "passive";
2725 struct interface_scan_data *data = user_data;
2727 supplicant_dbus_dict_open(iter, &dict);
2729 if (data && data->scan_params) {
2732 supplicant_dbus_dict_append_basic(&dict, "Type",
2733 DBUS_TYPE_STRING, &type);
2735 supplicant_dbus_dict_append_array(&dict, "SSIDs",
2740 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
2743 supplicant_dbus_dict_append_basic(&dict, "Type",
2744 DBUS_TYPE_STRING, &type);
2746 supplicant_dbus_dict_close(iter, &dict);
2749 int g_supplicant_interface_scan(GSupplicantInterface *interface,
2750 GSupplicantScanParams *scan_data,
2751 GSupplicantInterfaceCallback callback,
2754 struct interface_scan_data *data;
2757 if (interface == NULL)
2760 if (system_available == FALSE)
2763 if (interface->scanning == TRUE)
2766 switch (interface->state) {
2767 case G_SUPPLICANT_STATE_AUTHENTICATING:
2768 case G_SUPPLICANT_STATE_ASSOCIATING:
2769 case G_SUPPLICANT_STATE_ASSOCIATED:
2770 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2771 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2773 case G_SUPPLICANT_STATE_UNKNOWN:
2774 case G_SUPPLICANT_STATE_DISCONNECTED:
2775 case G_SUPPLICANT_STATE_INACTIVE:
2776 case G_SUPPLICANT_STATE_SCANNING:
2777 case G_SUPPLICANT_STATE_COMPLETED:
2781 data = dbus_malloc0(sizeof(*data));
2785 data->interface = interface;
2786 data->callback = callback;
2787 data->user_data = user_data;
2788 data->scan_params = scan_data;
2790 ret = supplicant_dbus_method_call(interface->path,
2791 SUPPLICANT_INTERFACE ".Interface", "Scan",
2792 interface_scan_params, interface_scan_result, data);
2800 static int parse_supplicant_error(DBusMessageIter *iter)
2802 int err = -ECANCELED;
2805 /* If the given passphrase is malformed wpa_s returns
2806 * "invalid message format" but this error should be interpreted as
2809 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
2810 dbus_message_iter_get_basic(iter, &key);
2811 if (strncmp(key, "psk", 3) == 0 ||
2812 strncmp(key, "wep_key", 7) == 0 ||
2813 strcmp(key, "invalid message format") == 0) {
2817 dbus_message_iter_next(iter);
2823 static void interface_select_network_result(const char *error,
2824 DBusMessageIter *iter, void *user_data)
2826 struct interface_connect_data *data = user_data;
2832 if (error != NULL) {
2833 SUPPLICANT_DBG("SelectNetwork error %s", error);
2834 err = parse_supplicant_error(iter);
2837 if (data->callback != NULL)
2838 data->callback(err, data->interface, data->user_data);
2844 static void interface_select_network_params(DBusMessageIter *iter,
2847 struct interface_connect_data *data = user_data;
2848 GSupplicantInterface *interface = data->interface;
2850 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2851 &interface->network_path);
2854 static void interface_add_network_result(const char *error,
2855 DBusMessageIter *iter, void *user_data)
2857 struct interface_connect_data *data = user_data;
2858 GSupplicantInterface *interface = data->interface;
2865 dbus_message_iter_get_basic(iter, &path);
2869 SUPPLICANT_DBG("PATH: %s", path);
2871 g_free(interface->network_path);
2872 interface->network_path = g_strdup(path);
2874 supplicant_dbus_method_call(data->interface->path,
2875 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
2876 interface_select_network_params,
2877 interface_select_network_result, data);
2882 SUPPLICANT_DBG("AddNetwork error %s", error);
2883 err = parse_supplicant_error(iter);
2884 if (data->callback != NULL)
2885 data->callback(err, data->interface, data->user_data);
2887 g_free(interface->network_path);
2888 interface->network_path = NULL;
2893 static void add_network_security_wep(DBusMessageIter *dict,
2894 GSupplicantSSID *ssid)
2896 const char *auth_alg = "OPEN SHARED";
2897 const char *key_index = "0";
2899 supplicant_dbus_dict_append_basic(dict, "auth_alg",
2900 DBUS_TYPE_STRING, &auth_alg);
2902 if (ssid->passphrase) {
2903 int size = strlen(ssid->passphrase);
2904 if (size == 10 || size == 26) {
2905 unsigned char *key = g_try_malloc(13);
2909 memset(tmp, 0, sizeof(tmp));
2913 for (i = 0; i < size / 2; i++) {
2914 memcpy(tmp, ssid->passphrase + (i * 2), 2);
2915 key[i] = (unsigned char) strtol(tmp, NULL, 16);
2918 supplicant_dbus_dict_append_fixed_array(dict,
2923 } else if (size == 5 || size == 13) {
2924 unsigned char *key = g_try_malloc(13);
2930 for (i = 0; i < size; i++)
2931 key[i] = (unsigned char) ssid->passphrase[i];
2933 supplicant_dbus_dict_append_fixed_array(dict,
2939 supplicant_dbus_dict_append_basic(dict,
2944 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
2945 DBUS_TYPE_STRING, &key_index);
2949 static dbus_bool_t is_psk_raw_key(const char *psk)
2953 /* A raw key is always 64 bytes length... */
2954 if (strlen(psk) != 64)
2957 /* ... and its content is in hex representation */
2958 for (i = 0; i < 64; i++)
2959 if (!isxdigit((unsigned char) psk[i]))
2965 static void add_network_security_psk(DBusMessageIter *dict,
2966 GSupplicantSSID *ssid)
2968 if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
2969 if (is_psk_raw_key(ssid->passphrase) == TRUE)
2970 supplicant_dbus_dict_append_fixed_array(dict,
2971 "psk", DBUS_TYPE_BYTE,
2972 &ssid->passphrase, 64);
2974 supplicant_dbus_dict_append_basic(dict, "psk",
2980 static void add_network_security_tls(DBusMessageIter *dict,
2981 GSupplicantSSID *ssid)
2984 * For TLS, we at least need:
2985 * The client certificate
2986 * The client private key file
2987 * The client private key file password
2989 * The Authority certificate is optional.
2991 if (ssid->client_cert_path == NULL)
2994 if (ssid->private_key_path == NULL)
2997 if (ssid->private_key_passphrase == NULL)
3000 if (ssid->ca_cert_path)
3001 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3002 DBUS_TYPE_STRING, &ssid->ca_cert_path);
3004 supplicant_dbus_dict_append_basic(dict, "private_key",
3006 &ssid->private_key_path);
3007 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3009 &ssid->private_key_passphrase);
3010 supplicant_dbus_dict_append_basic(dict, "client_cert",
3012 &ssid->client_cert_path);
3015 static void add_network_security_peap(DBusMessageIter *dict,
3016 GSupplicantSSID *ssid)
3021 * For PEAP/TTLS, we at least need
3022 * The authority certificate
3023 * The 2nd phase authentication method
3024 * The 2nd phase passphrase
3026 * The Client certificate is optional although strongly recommended
3027 * When setting it, we need in addition
3028 * The Client private key file
3029 * The Client private key file password
3031 if (ssid->passphrase == NULL)
3034 if (ssid->phase2_auth == NULL)
3037 if (ssid->client_cert_path) {
3038 if (ssid->private_key_path == NULL)
3041 if (ssid->private_key_passphrase == NULL)
3044 supplicant_dbus_dict_append_basic(dict, "client_cert",
3046 &ssid->client_cert_path);
3048 supplicant_dbus_dict_append_basic(dict, "private_key",
3050 &ssid->private_key_path);
3052 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3054 &ssid->private_key_passphrase);
3058 if (g_str_has_prefix(ssid->phase2_auth, "EAP-") == TRUE) {
3059 phase2_auth = g_strdup_printf("autheap=%s",
3060 ssid->phase2_auth + strlen("EAP-"));
3062 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
3064 supplicant_dbus_dict_append_basic(dict, "password",
3068 if (ssid->ca_cert_path)
3069 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3071 &ssid->ca_cert_path);
3073 supplicant_dbus_dict_append_basic(dict, "phase2",
3077 g_free(phase2_auth);
3080 static void add_network_security_eap(DBusMessageIter *dict,
3081 GSupplicantSSID *ssid)
3085 if (ssid->eap == NULL || ssid->identity == NULL)
3088 if (g_strcmp0(ssid->eap, "tls") == 0) {
3089 add_network_security_tls(dict, ssid);
3090 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
3091 g_strcmp0(ssid->eap, "ttls") == 0) {
3092 add_network_security_peap(dict, ssid);
3096 eap_value = g_ascii_strup(ssid->eap, -1);
3098 supplicant_dbus_dict_append_basic(dict, "eap",
3101 supplicant_dbus_dict_append_basic(dict, "identity",
3108 static void add_network_security_ciphers(DBusMessageIter *dict,
3109 GSupplicantSSID *ssid)
3111 unsigned int p_cipher, g_cipher, i;
3112 char *pairwise, *group;
3113 char *pair_ciphers[4];
3114 char *group_ciphers[5];
3116 p_cipher = ssid->pairwise_cipher;
3117 g_cipher = ssid->group_cipher;
3119 if (p_cipher == 0 && g_cipher == 0)
3124 if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
3125 pair_ciphers[i++] = "CCMP";
3127 if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
3128 pair_ciphers[i++] = "TKIP";
3130 if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
3131 pair_ciphers[i++] = "NONE";
3133 pair_ciphers[i] = NULL;
3137 if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
3138 group_ciphers[i++] = "CCMP";
3140 if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
3141 group_ciphers[i++] = "TKIP";
3143 if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
3144 group_ciphers[i++] = "WEP104";
3146 if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
3147 group_ciphers[i++] = "WEP40";
3149 group_ciphers[i] = NULL;
3151 pairwise = g_strjoinv(" ", pair_ciphers);
3152 group = g_strjoinv(" ", group_ciphers);
3154 SUPPLICANT_DBG("cipher %s %s", pairwise, group);
3156 supplicant_dbus_dict_append_basic(dict, "pairwise",
3159 supplicant_dbus_dict_append_basic(dict, "group",
3167 static void add_network_security_proto(DBusMessageIter *dict,
3168 GSupplicantSSID *ssid)
3170 unsigned int protocol, i;
3174 protocol = ssid->protocol;
3181 if (protocol & G_SUPPLICANT_PROTO_RSN)
3182 protos[i++] = "RSN";
3184 if (protocol & G_SUPPLICANT_PROTO_WPA)
3185 protos[i++] = "WPA";
3189 proto = g_strjoinv(" ", protos);
3191 SUPPLICANT_DBG("proto %s", proto);
3193 supplicant_dbus_dict_append_basic(dict, "proto",
3200 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
3204 switch (ssid->security) {
3205 case G_SUPPLICANT_SECURITY_UNKNOWN:
3206 case G_SUPPLICANT_SECURITY_NONE:
3207 case G_SUPPLICANT_SECURITY_WEP:
3209 add_network_security_wep(dict, ssid);
3210 add_network_security_ciphers(dict, ssid);
3212 case G_SUPPLICANT_SECURITY_PSK:
3213 key_mgmt = "WPA-PSK";
3214 add_network_security_psk(dict, ssid);
3215 add_network_security_ciphers(dict, ssid);
3216 add_network_security_proto(dict, ssid);
3218 case G_SUPPLICANT_SECURITY_IEEE8021X:
3219 key_mgmt = "WPA-EAP";
3220 add_network_security_eap(dict, ssid);
3221 add_network_security_ciphers(dict, ssid);
3222 add_network_security_proto(dict, ssid);
3226 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
3227 DBUS_TYPE_STRING, &key_mgmt);
3230 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
3234 switch (ssid->mode) {
3235 case G_SUPPLICANT_MODE_UNKNOWN:
3236 case G_SUPPLICANT_MODE_INFRA:
3239 case G_SUPPLICANT_MODE_IBSS:
3242 case G_SUPPLICANT_MODE_MASTER:
3247 supplicant_dbus_dict_append_basic(dict, "mode",
3248 DBUS_TYPE_UINT32, &mode);
3251 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
3253 DBusMessageIter dict;
3254 struct interface_connect_data *data = user_data;
3255 GSupplicantSSID *ssid = data->ssid;
3257 supplicant_dbus_dict_open(iter, &dict);
3259 if (ssid->scan_ssid)
3260 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
3261 DBUS_TYPE_UINT32, &ssid->scan_ssid);
3264 supplicant_dbus_dict_append_basic(&dict, "frequency",
3265 DBUS_TYPE_UINT32, &ssid->freq);
3267 if (ssid->bgscan != NULL)
3268 supplicant_dbus_dict_append_basic(&dict, "bgscan",
3269 DBUS_TYPE_STRING, &ssid->bgscan);
3271 add_network_mode(&dict, ssid);
3273 add_network_security(&dict, ssid);
3275 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
3276 DBUS_TYPE_BYTE, &ssid->ssid,
3279 supplicant_dbus_dict_close(iter, &dict);
3282 static void interface_wps_start_result(const char *error,
3283 DBusMessageIter *iter, void *user_data)
3285 struct interface_connect_data *data = user_data;
3289 SUPPLICANT_DBG("error: %s", error);
3295 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
3297 struct interface_connect_data *data = user_data;
3298 GSupplicantSSID *ssid = data->ssid;
3299 const char *role = "enrollee", *type;
3300 DBusMessageIter dict;
3304 supplicant_dbus_dict_open(iter, &dict);
3306 supplicant_dbus_dict_append_basic(&dict, "Role",
3307 DBUS_TYPE_STRING, &role);
3310 if (ssid->pin_wps != NULL) {
3312 supplicant_dbus_dict_append_basic(&dict, "Pin",
3313 DBUS_TYPE_STRING, &ssid->pin_wps);
3316 supplicant_dbus_dict_append_basic(&dict, "Type",
3317 DBUS_TYPE_STRING, &type);
3319 supplicant_dbus_dict_close(iter, &dict);
3322 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
3324 struct interface_connect_data *data = user_data;
3328 if (error != NULL) {
3329 SUPPLICANT_DBG("error: %s", error);
3335 supplicant_dbus_method_call(data->interface->path,
3336 SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
3337 interface_add_wps_params,
3338 interface_wps_start_result, data);
3341 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
3343 dbus_bool_t credentials = TRUE;
3347 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
3351 int g_supplicant_interface_connect(GSupplicantInterface *interface,
3352 GSupplicantSSID *ssid,
3353 GSupplicantInterfaceCallback callback,
3356 struct interface_connect_data *data;
3359 if (interface == NULL)
3362 if (system_available == FALSE)
3365 /* TODO: Check if we're already connected and switch */
3367 data = dbus_malloc0(sizeof(*data));
3371 data->interface = interface;
3372 data->callback = callback;
3374 data->user_data = user_data;
3376 if (ssid->use_wps == TRUE) {
3377 g_free(interface->wps_cred.key);
3378 memset(&interface->wps_cred, 0,
3379 sizeof(struct _GSupplicantWpsCredentials));
3381 ret = supplicant_dbus_property_set(interface->path,
3382 SUPPLICANT_INTERFACE ".Interface.WPS",
3383 "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
3384 wps_process_credentials, wps_start, data);
3386 ret = supplicant_dbus_method_call(interface->path,
3387 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
3388 interface_add_network_params,
3389 interface_add_network_result, data);
3394 return -EINPROGRESS;
3397 static void network_remove_result(const char *error,
3398 DBusMessageIter *iter, void *user_data)
3400 struct interface_data *data = user_data;
3408 if (data->callback != NULL)
3409 data->callback(result, data->interface, data->user_data);
3414 static void network_remove_params(DBusMessageIter *iter, void *user_data)
3416 struct interface_data *data = user_data;
3417 const char *path = data->interface->network_path;
3419 SUPPLICANT_DBG("path %s", path);
3421 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
3424 static int network_remove(struct interface_data *data)
3426 GSupplicantInterface *interface = data->interface;
3430 return supplicant_dbus_method_call(interface->path,
3431 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
3432 network_remove_params, network_remove_result, data);
3435 static void interface_disconnect_result(const char *error,
3436 DBusMessageIter *iter, void *user_data)
3438 struct interface_data *data = user_data;
3442 if (error != NULL && data->callback != NULL)
3443 data->callback(-EIO, data->interface, data->user_data);
3445 /* If we are disconnecting from previous WPS successful
3446 * association. i.e.: it did not went through AddNetwork,
3447 * and interface->network_path was never set. */
3448 if (data->interface->network_path == NULL) {
3453 network_remove(data);
3456 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
3457 GSupplicantInterfaceCallback callback,
3460 struct interface_data *data;
3464 if (interface == NULL)
3467 if (system_available == FALSE)
3470 data = dbus_malloc0(sizeof(*data));
3474 data->interface = interface;
3475 data->callback = callback;
3476 data->user_data = user_data;
3478 return supplicant_dbus_method_call(interface->path,
3479 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
3480 NULL, interface_disconnect_result, data);
3484 static const char *g_supplicant_rule0 = "type=signal,"
3485 "path=" DBUS_PATH_DBUS ","
3486 "sender=" DBUS_SERVICE_DBUS ","
3487 "interface=" DBUS_INTERFACE_DBUS ","
3488 "member=NameOwnerChanged,"
3489 "arg0=" SUPPLICANT_SERVICE;
3490 static const char *g_supplicant_rule1 = "type=signal,"
3491 "interface=" SUPPLICANT_INTERFACE;
3492 static const char *g_supplicant_rule2 = "type=signal,"
3493 "interface=" SUPPLICANT_INTERFACE ".Interface";
3494 static const char *g_supplicant_rule3 = "type=signal,"
3495 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
3496 static const char *g_supplicant_rule4 = "type=signal,"
3497 "interface=" SUPPLICANT_INTERFACE ".BSS";
3498 static const char *g_supplicant_rule5 = "type=signal,"
3499 "interface=" SUPPLICANT_INTERFACE ".Network";
3501 static void invoke_introspect_method(void)
3503 DBusMessage *message;
3505 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
3507 DBUS_INTERFACE_INTROSPECTABLE,
3510 if (message == NULL)
3513 dbus_message_set_no_reply(message, TRUE);
3514 dbus_connection_send(connection, message, NULL);
3515 dbus_message_unref(message);
3518 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
3520 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3521 if (connection == NULL)
3524 if (dbus_connection_add_filter(connection,
3525 g_supplicant_filter, NULL, NULL) == FALSE) {
3526 dbus_connection_unref(connection);
3531 callbacks_pointer = callbacks;
3534 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
3535 NULL, remove_interface);
3537 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
3540 supplicant_dbus_setup(connection);
3542 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
3543 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
3544 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
3545 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
3546 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
3547 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
3548 dbus_connection_flush(connection);
3550 if (dbus_bus_name_has_owner(connection,
3551 SUPPLICANT_SERVICE, NULL) == TRUE) {
3552 system_available = TRUE;
3553 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
3554 SUPPLICANT_INTERFACE,
3555 service_property, NULL);
3557 invoke_introspect_method();
3562 static void unregister_interface_remove_params(DBusMessageIter *iter,
3565 const char *path = user_data;
3567 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3572 static void unregister_remove_interface(gpointer key, gpointer value,
3575 GSupplicantInterface *interface = value;
3577 supplicant_dbus_method_call(SUPPLICANT_PATH,
3578 SUPPLICANT_INTERFACE,
3580 unregister_interface_remove_params,
3581 NULL, interface->path);
3584 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
3588 if (connection != NULL) {
3589 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
3590 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
3591 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
3592 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
3593 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
3594 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
3595 dbus_connection_flush(connection);
3597 dbus_connection_remove_filter(connection,
3598 g_supplicant_filter, NULL);
3601 if (bss_mapping != NULL) {
3602 g_hash_table_destroy(bss_mapping);
3606 if (system_available == TRUE)
3607 callback_system_killed();
3609 if (interface_table != NULL) {
3610 g_hash_table_foreach(interface_table,
3611 unregister_remove_interface, NULL);
3612 g_hash_table_destroy(interface_table);
3613 interface_table = NULL;
3616 if (connection != NULL) {
3617 dbus_connection_unref(connection);
3621 callbacks_pointer = NULL;