3 * WPA supplicant library with GLib integration
5 * Copyright (C) 2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 return interface->max_scan_ssids;
748 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
750 dbus_bool_t enable = *(dbus_bool_t *)user_data;
752 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
755 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
758 if (interface == NULL)
761 if (interface->network_path == NULL)
765 return supplicant_dbus_property_set(interface->network_path,
766 SUPPLICANT_INTERFACE ".Network",
767 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
768 set_network_enabled, NULL, &enable);
771 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
773 if (interface == NULL)
776 return interface->ready;
779 GSupplicantInterface *g_supplicant_network_get_interface(
780 GSupplicantNetwork *network)
785 return network->interface;
788 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
790 if (network == NULL || network->name == NULL)
793 return network->name;
796 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
798 if (network == NULL || network->group == NULL)
801 return network->group;
804 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
806 if (network == NULL || network->path == NULL)
809 return network->path;
812 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
815 return G_SUPPLICANT_MODE_UNKNOWN;
817 return mode2string(network->mode);
820 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
823 return G_SUPPLICANT_SECURITY_UNKNOWN;
825 return security2string(network->security);
828 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
829 unsigned int *ssid_len)
831 if (network == NULL || network->ssid == NULL) {
836 *ssid_len = network->ssid_len;
837 return network->ssid;
840 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
845 return network->signal;
848 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
853 return network->frequency;
856 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
864 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
869 if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
875 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
880 if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
886 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
891 if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
897 static void merge_network(GSupplicantNetwork *network)
900 const char *ssid, *mode, *key_mgmt;
901 unsigned int i, ssid_len;
904 ssid = g_hash_table_lookup(network->config_table, "ssid");
905 mode = g_hash_table_lookup(network->config_table, "mode");
906 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
908 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
911 ssid_len = strlen(ssid);
915 str = g_string_sized_new((ssid_len * 2) + 24);
919 for (i = 0; i < ssid_len; i++)
920 g_string_append_printf(str, "%02x", ssid[i]);
922 if (g_strcmp0(mode, "0") == 0)
923 g_string_append_printf(str, "_managed");
924 else if (g_strcmp0(mode, "1") == 0)
925 g_string_append_printf(str, "_adhoc");
927 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
928 g_string_append_printf(str, "_psk");
930 group = g_string_free(str, FALSE);
932 SUPPLICANT_DBG("%s", group);
936 g_hash_table_destroy(network->config_table);
938 g_free(network->path);
942 static void network_property(const char *key, DBusMessageIter *iter,
945 GSupplicantNetwork *network = user_data;
947 if (network->interface == NULL)
951 merge_network(network);
955 if (g_strcmp0(key, "Enabled") == 0) {
956 dbus_bool_t enabled = FALSE;
958 dbus_message_iter_get_basic(iter, &enabled);
959 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
960 const char *str = NULL;
962 dbus_message_iter_get_basic(iter, &str);
964 g_hash_table_replace(network->config_table,
965 g_strdup(key), g_strdup(str));
968 SUPPLICANT_DBG("key %s type %c",
969 key, dbus_message_iter_get_arg_type(iter));
972 static void interface_network_added(DBusMessageIter *iter, void *user_data)
974 GSupplicantInterface *interface = user_data;
975 GSupplicantNetwork *network;
976 const char *path = NULL;
980 dbus_message_iter_get_basic(iter, &path);
984 if (g_strcmp0(path, "/") == 0)
987 network = g_hash_table_lookup(interface->net_mapping, path);
991 network = g_try_new0(GSupplicantNetwork, 1);
995 network->interface = interface;
996 network->path = g_strdup(path);
998 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1001 dbus_message_iter_next(iter);
1002 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1003 supplicant_dbus_property_foreach(iter, network_property,
1005 network_property(NULL, NULL, network);
1009 supplicant_dbus_property_get_all(path,
1010 SUPPLICANT_INTERFACE ".Network",
1011 network_property, network);
1014 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1016 GSupplicantInterface *interface = user_data;
1017 GSupplicantNetwork *network;
1018 const char *path = NULL;
1020 dbus_message_iter_get_basic(iter, &path);
1024 network = g_hash_table_lookup(interface->net_mapping, path);
1025 if (network == NULL)
1028 g_hash_table_remove(interface->net_mapping, path);
1031 static char *create_name(unsigned char *ssid, int ssid_len)
1034 const gchar *remainder, *invalid;
1035 int valid_bytes, remaining_bytes;
1037 if (ssid_len < 1 || ssid[0] == '\0')
1038 return g_strdup("");
1041 remainder = (const gchar *)ssid;
1042 remaining_bytes = ssid_len;
1044 while (remaining_bytes != 0) {
1045 if (g_utf8_validate(remainder, remaining_bytes,
1046 &invalid) == TRUE) {
1050 valid_bytes = invalid - remainder;
1053 string = g_string_sized_new(remaining_bytes);
1055 g_string_append_len(string, remainder, valid_bytes);
1057 /* append U+FFFD REPLACEMENT CHARACTER */
1058 g_string_append(string, "\357\277\275");
1060 remaining_bytes -= valid_bytes + 1;
1061 remainder = invalid + 1;
1065 return g_strndup((const gchar *)ssid, ssid_len + 1);
1067 g_string_append(string, remainder);
1069 return g_string_free(string, FALSE);
1072 static char *create_group(struct g_supplicant_bss *bss)
1076 const char *mode, *security;
1078 str = g_string_sized_new((bss->ssid_len * 2) + 24);
1082 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1083 for (i = 0; i < bss->ssid_len; i++)
1084 g_string_append_printf(str, "%02x", bss->ssid[i]);
1086 g_string_append_printf(str, "hidden");
1088 mode = mode2string(bss->mode);
1090 g_string_append_printf(str, "_%s", mode);
1092 security = security2string(bss->security);
1093 if (security != NULL)
1094 g_string_append_printf(str, "_%s", security);
1096 return g_string_free(str, FALSE);
1099 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1101 GSupplicantInterface *interface = bss->interface;
1102 GSupplicantNetwork *network;
1105 group = create_group(bss);
1106 SUPPLICANT_DBG("New group created: %s", group);
1111 network = g_hash_table_lookup(interface->network_table, group);
1112 if (network != NULL) {
1114 SUPPLICANT_DBG("Network %s already exist", network->name);
1119 network = g_try_new0(GSupplicantNetwork, 1);
1120 if (network == NULL) {
1125 network->interface = interface;
1126 if (network->path == NULL)
1127 network->path = g_strdup(bss->path);
1128 network->group = group;
1129 network->name = create_name(bss->ssid, bss->ssid_len);
1130 network->mode = bss->mode;
1131 network->security = bss->security;
1132 network->ssid_len = bss->ssid_len;
1133 memcpy(network->ssid, bss->ssid, bss->ssid_len);
1134 network->signal = bss->signal;
1135 network->frequency = bss->frequency;
1136 network->best_bss = bss;
1138 network->wps = FALSE;
1139 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1140 network->wps = TRUE;
1142 network->wps_capabilities |= bss->wps_capabilities;
1145 SUPPLICANT_DBG("New network %s created", network->name);
1147 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1150 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1153 g_hash_table_replace(interface->network_table,
1154 network->group, network);
1156 callback_network_added(network);
1159 if (bss->signal > network->signal) {
1160 network->signal = bss->signal;
1161 network->best_bss = bss;
1162 callback_network_changed(network, "Signal");
1165 g_hash_table_replace(interface->bss_mapping, bss->path, network);
1166 g_hash_table_replace(network->bss_table, bss->path, bss);
1168 g_hash_table_replace(bss_mapping, bss->path, interface);
1171 static void bss_rates(DBusMessageIter *iter, void *user_data)
1173 struct g_supplicant_bss *bss = user_data;
1174 dbus_uint32_t rate = 0;
1176 dbus_message_iter_get_basic(iter, &rate);
1180 if (rate > bss->maxrate)
1181 bss->maxrate = rate;
1184 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1186 struct g_supplicant_bss *bss = user_data;
1187 const char *str = NULL;
1190 dbus_message_iter_get_basic(iter, &str);
1194 for (i = 0; keymgmt_map[i].str != NULL; i++)
1195 if (strcmp(str, keymgmt_map[i].str) == 0) {
1196 bss->keymgmt |= keymgmt_map[i].val;
1201 static void bss_group(DBusMessageIter *iter, void *user_data)
1203 struct g_supplicant_bss *bss = user_data;
1204 const char *str = NULL;
1207 dbus_message_iter_get_basic(iter, &str);
1211 for (i = 0; group_map[i].str != NULL; i++)
1212 if (strcmp(str, group_map[i].str) == 0) {
1213 bss->group |= group_map[i].val;
1218 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1220 struct g_supplicant_bss *bss = user_data;
1221 const char *str = NULL;
1224 dbus_message_iter_get_basic(iter, &str);
1228 for (i = 0; pairwise_map[i].str != NULL; i++)
1229 if (strcmp(str, pairwise_map[i].str) == 0) {
1230 bss->pairwise |= pairwise_map[i].val;
1235 static void bss_wpa(const char *key, DBusMessageIter *iter,
1238 if (g_strcmp0(key, "KeyMgmt") == 0)
1239 supplicant_dbus_array_foreach(iter, bss_keymgmt, user_data);
1240 else if (g_strcmp0(key, "Group") == 0)
1241 supplicant_dbus_array_foreach(iter, bss_group, user_data);
1242 else if (g_strcmp0(key, "Pairwise") == 0)
1243 supplicant_dbus_array_foreach(iter, bss_pairwise, user_data);
1247 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1250 unsigned int len = 0;
1252 while (len + 4 < ie_size) {
1253 unsigned int hi = ie[len];
1254 unsigned int lo = ie[len + 1];
1255 unsigned int tmp_type = (hi << 8) + lo;
1256 unsigned int v_len = 0;
1258 /* hi and lo are used to recreate an unsigned int
1259 * based on 2 8bits length unsigned int. */
1263 v_len = (hi << 8) + lo;
1265 if (tmp_type == type) {
1266 unsigned int ret_value = 0;
1267 unsigned char *value = (unsigned char *)&ret_value;
1269 SUPPLICANT_DBG("IE: match type 0x%x", type);
1271 /* Verifying length relevance */
1272 if (v_len > sizeof(unsigned int) ||
1273 len + 4 + v_len > ie_size)
1276 memcpy(value, ie + len + 4, v_len);
1278 SUPPLICANT_DBG("returning 0x%x", ret_value);
1285 SUPPLICANT_DBG("returning 0");
1289 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1291 struct g_supplicant_bss *bss = user_data;
1292 const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1293 unsigned char *ie, *ie_end;
1294 DBusMessageIter array;
1298 #define WMM_WPA1_WPS_INFO 221
1299 #define WPS_INFO_MIN_LEN 6
1300 #define WPS_VERSION_TLV 0x104A
1301 #define WPS_STATE_TLV 0x1044
1302 #define WPS_METHODS_TLV 0x1012
1303 #define WPS_REGISTRAR_TLV 0x1041
1304 #define WPS_VERSION 0x10
1305 #define WPS_PBC 0x04
1306 #define WPS_PIN 0x00
1307 #define WPS_CONFIGURED 0x02
1309 dbus_message_iter_recurse(iter, &array);
1310 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1312 if (ie == NULL || ie_len < 2)
1315 for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1318 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1319 memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1322 SUPPLICANT_DBG("IE: match WPS_OUI");
1324 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1325 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1327 bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1329 if (value == WPS_CONFIGURED)
1330 bss->wps_capabilities |=
1331 G_SUPPLICANT_WPS_CONFIGURED;
1334 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1336 if (GUINT16_FROM_BE(value) == WPS_PBC)
1337 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1338 if (GUINT16_FROM_BE(value) == WPS_PIN)
1339 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1341 bss->wps_capabilities |=
1342 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1344 /* If the AP sends this it means it's advertizing
1345 * as a registrar and the WPS process is launched
1347 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1348 bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1350 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1354 static void bss_compute_security(struct g_supplicant_bss *bss)
1356 if (bss->ieee8021x == TRUE)
1357 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1358 else if (bss->psk == TRUE)
1359 bss->security = G_SUPPLICANT_SECURITY_PSK;
1360 else if (bss->privacy == TRUE)
1361 bss->security = G_SUPPLICANT_SECURITY_WEP;
1363 bss->security = G_SUPPLICANT_SECURITY_NONE;
1367 static void bss_property(const char *key, DBusMessageIter *iter,
1370 struct g_supplicant_bss *bss = user_data;
1372 if (bss->interface == NULL)
1375 SUPPLICANT_DBG("key %s", key);
1378 bss_compute_security(bss);
1380 add_or_replace_bss_to_network(bss);
1384 if (g_strcmp0(key, "BSSID") == 0) {
1385 DBusMessageIter array;
1386 unsigned char *addr;
1389 dbus_message_iter_recurse(iter, &array);
1390 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1393 memcpy(bss->bssid, addr, addr_len);
1394 } else if (g_strcmp0(key, "SSID") == 0) {
1395 DBusMessageIter array;
1396 unsigned char *ssid;
1399 dbus_message_iter_recurse(iter, &array);
1400 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1402 if (ssid_len > 0 && ssid_len < 33) {
1403 memcpy(bss->ssid, ssid, ssid_len);
1404 bss->ssid_len = ssid_len;
1406 memset(bss->ssid, 0, sizeof(bss->ssid));
1409 } else if (g_strcmp0(key, "Capabilities") == 0) {
1410 dbus_uint16_t capabilities = 0x0000;
1412 dbus_message_iter_get_basic(iter, &capabilities);
1414 if (capabilities & IEEE80211_CAP_ESS)
1415 bss->mode = G_SUPPLICANT_MODE_INFRA;
1416 else if (capabilities & IEEE80211_CAP_IBSS)
1417 bss->mode = G_SUPPLICANT_MODE_IBSS;
1419 if (capabilities & IEEE80211_CAP_PRIVACY)
1420 bss->privacy = TRUE;
1421 } else if (g_strcmp0(key, "Mode") == 0) {
1422 const char *mode = NULL;
1424 dbus_message_iter_get_basic(iter, &mode);
1425 bss->mode = string2mode(mode);
1426 } else if (g_strcmp0(key, "Frequency") == 0) {
1427 dbus_uint16_t frequency = 0;
1429 dbus_message_iter_get_basic(iter, &frequency);
1430 bss->frequency = frequency;
1431 } else if (g_strcmp0(key, "Signal") == 0) {
1432 dbus_int16_t signal = 0;
1434 dbus_message_iter_get_basic(iter, &signal);
1436 bss->signal = signal;
1437 } else if (g_strcmp0(key, "Level") == 0) {
1438 dbus_int32_t level = 0;
1440 dbus_message_iter_get_basic(iter, &level);
1441 } else if (g_strcmp0(key, "Rates") == 0) {
1442 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1443 } else if (g_strcmp0(key, "MaxRate") == 0) {
1444 dbus_uint32_t maxrate = 0;
1446 dbus_message_iter_get_basic(iter, &maxrate);
1448 bss->maxrate = maxrate;
1449 } else if (g_strcmp0(key, "Privacy") == 0) {
1450 dbus_bool_t privacy = FALSE;
1452 dbus_message_iter_get_basic(iter, &privacy);
1453 bss->privacy = privacy;
1454 } else if ((g_strcmp0(key, "RSN") == 0) ||
1455 (g_strcmp0(key, "WPA") == 0)) {
1456 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1459 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1460 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1461 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1462 bss->ieee8021x = TRUE;
1465 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1466 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1467 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1469 } else if (g_strcmp0(key, "IEs") == 0)
1470 bss_process_ies(iter, bss);
1472 SUPPLICANT_DBG("key %s type %c",
1473 key, dbus_message_iter_get_arg_type(iter));
1476 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1479 GSupplicantInterface *interface = user_data;
1480 GSupplicantNetwork *network;
1481 struct g_supplicant_bss *bss;
1482 const char *path = NULL;
1486 dbus_message_iter_get_basic(iter, &path);
1490 if (g_strcmp0(path, "/") == 0)
1493 SUPPLICANT_DBG("%s", path);
1495 network = g_hash_table_lookup(interface->bss_mapping, path);
1496 if (network != NULL) {
1497 bss = g_hash_table_lookup(network->bss_table, path);
1502 bss = g_try_new0(struct g_supplicant_bss, 1);
1506 bss->interface = interface;
1507 bss->path = g_strdup(path);
1512 static void interface_bss_added_with_keys(DBusMessageIter *iter,
1515 struct g_supplicant_bss *bss;
1519 bss = interface_bss_added(iter, user_data);
1523 dbus_message_iter_next(iter);
1525 if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
1528 supplicant_dbus_property_foreach(iter, bss_property, bss);
1529 bss_property(NULL, NULL, bss);
1532 static void interface_bss_added_without_keys(DBusMessageIter *iter,
1535 struct g_supplicant_bss *bss;
1539 bss = interface_bss_added(iter, user_data);
1543 supplicant_dbus_property_get_all(bss->path,
1544 SUPPLICANT_INTERFACE ".BSS",
1548 static void update_signal(gpointer key, gpointer value,
1551 struct g_supplicant_bss *bss = value;
1552 GSupplicantNetwork *network = user_data;
1554 if (bss->signal > network->signal) {
1555 network->signal = bss->signal;
1556 network->best_bss = bss;
1560 static void update_network_signal(GSupplicantNetwork *network)
1562 if (g_hash_table_size(network->bss_table) <= 1)
1565 g_hash_table_foreach(network->bss_table,
1566 update_signal, network);
1568 SUPPLICANT_DBG("New network signal %d", network->signal);
1571 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1573 GSupplicantInterface *interface = user_data;
1574 GSupplicantNetwork *network;
1575 const char *path = NULL;
1577 dbus_message_iter_get_basic(iter, &path);
1581 network = g_hash_table_lookup(interface->bss_mapping, path);
1582 if (network == NULL)
1585 g_hash_table_remove(bss_mapping, path);
1587 g_hash_table_remove(interface->bss_mapping, path);
1588 g_hash_table_remove(network->bss_table, path);
1590 update_network_signal(network);
1592 if (g_hash_table_size(network->bss_table) == 0)
1593 g_hash_table_remove(interface->network_table, network->group);
1596 static void interface_property(const char *key, DBusMessageIter *iter,
1599 GSupplicantInterface *interface = user_data;
1601 if (interface == NULL)
1604 SUPPLICANT_DBG("%s", key);
1607 debug_strvalmap("KeyMgmt capability", keymgmt_map,
1608 interface->keymgmt_capa);
1609 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1610 interface->authalg_capa);
1611 debug_strvalmap("Protocol capability", proto_capa_map,
1612 interface->proto_capa);
1613 debug_strvalmap("Pairwise capability", pairwise_map,
1614 interface->pairwise_capa);
1615 debug_strvalmap("Group capability", group_map,
1616 interface->group_capa);
1617 debug_strvalmap("Scan capability", scan_capa_map,
1618 interface->scan_capa);
1619 debug_strvalmap("Mode capability", mode_capa_map,
1620 interface->mode_capa);
1622 interface->ready = TRUE;
1623 callback_interface_added(interface);
1627 if (g_strcmp0(key, "Capabilities") == 0) {
1628 supplicant_dbus_property_foreach(iter, interface_capability,
1630 } else if (g_strcmp0(key, "State") == 0) {
1631 const char *str = NULL;
1633 dbus_message_iter_get_basic(iter, &str);
1635 if (string2state(str) != interface->state) {
1636 interface->state = string2state(str);
1637 callback_interface_state(interface);
1640 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
1641 } else if (g_strcmp0(key, "Scanning") == 0) {
1642 dbus_bool_t scanning = FALSE;
1644 dbus_message_iter_get_basic(iter, &scanning);
1645 interface->scanning = scanning;
1647 if (interface->ready == TRUE) {
1648 if (interface->scanning == TRUE)
1649 callback_scan_started(interface);
1651 callback_scan_finished(interface);
1653 } else if (g_strcmp0(key, "ApScan") == 0) {
1656 dbus_message_iter_get_basic(iter, &apscan);
1657 interface->apscan = apscan;
1658 } else if (g_strcmp0(key, "Ifname") == 0) {
1659 const char *str = NULL;
1661 dbus_message_iter_get_basic(iter, &str);
1663 g_free(interface->ifname);
1664 interface->ifname = g_strdup(str);
1666 } else if (g_strcmp0(key, "Driver") == 0) {
1667 const char *str = NULL;
1669 dbus_message_iter_get_basic(iter, &str);
1671 g_free(interface->driver);
1672 interface->driver = g_strdup(str);
1674 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1675 const char *str = NULL;
1677 dbus_message_iter_get_basic(iter, &str);
1679 g_free(interface->bridge);
1680 interface->bridge = g_strdup(str);
1682 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1683 interface_bss_added_without_keys(iter, interface);
1684 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1685 interface_network_added(iter, interface);
1686 } else if (g_strcmp0(key, "BSSs") == 0) {
1687 supplicant_dbus_array_foreach(iter, interface_bss_added_without_keys,
1689 } else if (g_strcmp0(key, "Blobs") == 0) {
1691 } else if (g_strcmp0(key, "Networks") == 0) {
1692 supplicant_dbus_array_foreach(iter, interface_network_added,
1695 SUPPLICANT_DBG("key %s type %c",
1696 key, dbus_message_iter_get_arg_type(iter));
1699 static void scan_network_update(DBusMessageIter *iter, void *user_data)
1701 GSupplicantInterface *interface = user_data;
1702 GSupplicantNetwork *network;
1708 dbus_message_iter_get_basic(iter, &path);
1713 if (g_strcmp0(path, "/") == 0)
1716 /* Update the network details based on scan BSS data */
1717 network = g_hash_table_lookup(interface->bss_mapping, path);
1718 if (network != NULL)
1719 callback_network_added(network);
1722 static void scan_bss_data(const char *key, DBusMessageIter *iter,
1725 GSupplicantInterface *interface = user_data;
1728 supplicant_dbus_array_foreach(iter, scan_network_update,
1731 if (interface->scan_callback != NULL)
1732 interface->scan_callback(0, interface, interface->scan_data);
1734 interface->scan_callback = NULL;
1735 interface->scan_data = NULL;
1738 static GSupplicantInterface *interface_alloc(const char *path)
1740 GSupplicantInterface *interface;
1742 interface = g_try_new0(GSupplicantInterface, 1);
1743 if (interface == NULL)
1746 interface->path = g_strdup(path);
1748 interface->network_table = g_hash_table_new_full(g_str_hash,
1749 g_str_equal, NULL, remove_network);
1751 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1753 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1756 g_hash_table_replace(interface_table, interface->path, interface);
1761 static void interface_added(DBusMessageIter *iter, void *user_data)
1763 GSupplicantInterface *interface;
1764 const char *path = NULL;
1768 dbus_message_iter_get_basic(iter, &path);
1772 if (g_strcmp0(path, "/") == 0)
1775 interface = g_hash_table_lookup(interface_table, path);
1776 if (interface != NULL)
1779 interface = interface_alloc(path);
1780 if (interface == NULL)
1783 dbus_message_iter_next(iter);
1784 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1785 supplicant_dbus_property_foreach(iter, interface_property,
1787 interface_property(NULL, NULL, interface);
1791 supplicant_dbus_property_get_all(path,
1792 SUPPLICANT_INTERFACE ".Interface",
1793 interface_property, interface);
1796 static void interface_removed(DBusMessageIter *iter, void *user_data)
1798 const char *path = NULL;
1800 dbus_message_iter_get_basic(iter, &path);
1804 g_hash_table_remove(interface_table, path);
1807 static void eap_method(DBusMessageIter *iter, void *user_data)
1809 const char *str = NULL;
1812 dbus_message_iter_get_basic(iter, &str);
1816 for (i = 0; eap_method_map[i].str != NULL; i++)
1817 if (strcmp(str, eap_method_map[i].str) == 0) {
1818 eap_methods |= eap_method_map[i].val;
1823 static void service_property(const char *key, DBusMessageIter *iter,
1827 callback_system_ready();
1831 if (g_strcmp0(key, "DebugLevel") == 0) {
1832 const char *str = NULL;
1835 dbus_message_iter_get_basic(iter, &str);
1836 for (i = 0; debug_strings[i] != NULL; i++)
1837 if (g_strcmp0(debug_strings[i], str) == 0) {
1841 SUPPLICANT_DBG("Debug level %d", debug_level);
1842 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1843 dbus_message_iter_get_basic(iter, &debug_timestamp);
1844 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
1845 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1846 dbus_message_iter_get_basic(iter, &debug_showkeys);
1847 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
1848 } else if (g_strcmp0(key, "Interfaces") == 0) {
1849 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1850 } else if (g_strcmp0(key, "EapMethods") == 0) {
1851 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1852 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1853 } else if (g_strcmp0(key, "Country") == 0) {
1854 const char *country = NULL;
1856 dbus_message_iter_get_basic(iter, &country);
1857 SUPPLICANT_DBG("Country %s", country);
1859 SUPPLICANT_DBG("key %s type %c",
1860 key, dbus_message_iter_get_arg_type(iter));
1863 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1865 const char *name = NULL, *old = NULL, *new = NULL;
1869 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1872 dbus_message_iter_get_basic(iter, &name);
1876 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1879 dbus_message_iter_next(iter);
1880 dbus_message_iter_get_basic(iter, &old);
1881 dbus_message_iter_next(iter);
1882 dbus_message_iter_get_basic(iter, &new);
1884 if (old == NULL || new == NULL)
1887 if (strlen(old) > 0 && strlen(new) == 0) {
1888 system_available = FALSE;
1889 g_hash_table_remove_all(bss_mapping);
1890 g_hash_table_remove_all(interface_table);
1891 callback_system_killed();
1894 if (strlen(new) > 0 && strlen(old) == 0) {
1895 system_available = TRUE;
1896 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1897 SUPPLICANT_INTERFACE,
1898 service_property, NULL);
1902 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1906 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1909 supplicant_dbus_property_foreach(iter, service_property, NULL);
1912 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1914 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
1916 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1917 interface_added(iter, NULL);
1920 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1924 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1925 interface_removed(iter, NULL);
1928 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1930 GSupplicantInterface *interface;
1934 interface = g_hash_table_lookup(interface_table, path);
1935 if (interface == NULL)
1938 supplicant_dbus_property_foreach(iter, interface_property, interface);
1941 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1943 GSupplicantInterface *interface;
1944 dbus_bool_t success = FALSE;
1948 interface = g_hash_table_lookup(interface_table, path);
1949 if (interface == NULL)
1952 dbus_message_iter_get_basic(iter, &success);
1955 * If scan is unsuccessful return -EIO else get the scanned BSSs
1956 * and update the network details accordingly
1958 if (success == FALSE) {
1959 if (interface->scan_callback != NULL)
1960 interface->scan_callback(-EIO, interface,
1961 interface->scan_data);
1963 interface->scan_callback = NULL;
1964 interface->scan_data = NULL;
1969 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
1970 "BSSs", scan_bss_data, interface);
1973 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1975 GSupplicantInterface *interface;
1979 interface = g_hash_table_lookup(interface_table, path);
1980 if (interface == NULL)
1983 interface_bss_added_with_keys(iter, interface);
1986 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1988 GSupplicantInterface *interface;
1992 interface = g_hash_table_lookup(interface_table, path);
1993 if (interface == NULL)
1996 interface_bss_removed(iter, interface);
1999 static void signal_network_added(const char *path, DBusMessageIter *iter)
2001 GSupplicantInterface *interface;
2005 interface = g_hash_table_lookup(interface_table, path);
2006 if (interface == NULL)
2009 interface_network_added(iter, interface);
2012 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2014 GSupplicantInterface *interface;
2018 interface = g_hash_table_lookup(interface_table, path);
2019 if (interface == NULL)
2022 interface_network_removed(iter, interface);
2025 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2027 GSupplicantInterface *interface;
2028 GSupplicantNetwork *network;
2029 struct g_supplicant_bss *bss;
2033 interface = g_hash_table_lookup(bss_mapping, path);
2034 if (interface == NULL)
2037 network = g_hash_table_lookup(interface->bss_mapping, path);
2038 if (network == NULL)
2041 bss = g_hash_table_lookup(network->bss_table, path);
2045 supplicant_dbus_property_foreach(iter, bss_property, bss);
2047 if (bss->signal == network->signal)
2051 * If the new signal is lower than the SSID signal, we need
2052 * to check for the new maximum.
2054 if (bss->signal < network->signal) {
2055 if (bss != network->best_bss)
2057 network->signal = bss->signal;
2058 update_network_signal(network);
2060 network->signal = bss->signal;
2061 network->best_bss = bss;
2064 SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid, network->signal);
2066 callback_network_changed(network, "Signal");
2069 static void wps_credentials(const char *key, DBusMessageIter *iter,
2072 GSupplicantInterface *interface = user_data;
2077 SUPPLICANT_DBG("key %s", key);
2079 if (g_strcmp0(key, "Key") == 0) {
2080 DBusMessageIter array;
2084 dbus_message_iter_recurse(iter, &array);
2085 dbus_message_iter_get_fixed_array(&array, &key, &key_len);
2087 g_free(interface->wps_cred.key);
2088 interface->wps_cred.key = g_try_malloc0(
2089 sizeof(char) * key_len+1);
2091 if (interface->wps_cred.key == NULL)
2094 memcpy(interface->wps_cred.key, key, sizeof(char) * key_len);
2096 SUPPLICANT_DBG("WPS key present");
2097 } else if (g_strcmp0(key, "SSID") == 0) {
2098 DBusMessageIter array;
2099 unsigned char *ssid;
2102 dbus_message_iter_recurse(iter, &array);
2103 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2105 if (ssid_len > 0 && ssid_len < 33) {
2106 memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2107 interface->wps_cred.ssid_len = ssid_len;
2109 memset(interface->wps_cred.ssid, 0, 32);
2110 interface->wps_cred.ssid_len = 0;
2115 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2117 GSupplicantInterface *interface;
2121 interface = g_hash_table_lookup(interface_table, path);
2122 if (interface == NULL)
2125 supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2128 static void wps_event_args(const char *key, DBusMessageIter *iter,
2131 GSupplicantInterface *interface = user_data;
2133 if (key == NULL || interface == NULL)
2136 SUPPLICANT_DBG("Arg Key %s", key);
2139 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2141 GSupplicantInterface *interface;
2142 const char *name = NULL;
2146 interface = g_hash_table_lookup(interface_table, path);
2147 if (interface == NULL)
2150 dbus_message_iter_get_basic(iter, &name);
2152 SUPPLICANT_DBG("Name: %s", name);
2154 if (g_strcmp0(name, "success") == 0)
2155 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2156 else if (g_strcmp0(name, "failed") == 0)
2157 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2159 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2161 if (!dbus_message_iter_has_next(iter))
2164 dbus_message_iter_next(iter);
2166 supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2170 const char *interface;
2172 void (*function) (const char *path, DBusMessageIter *iter);
2174 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
2176 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
2177 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
2178 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
2179 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
2181 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
2182 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
2183 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
2184 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
2185 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
2186 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
2188 { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed },
2190 { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
2191 { SUPPLICANT_INTERFACE ".Interface.WPS", "Event", signal_wps_event },
2196 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
2197 DBusMessage *message, void *data)
2199 DBusMessageIter iter;
2203 path = dbus_message_get_path(message);
2205 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2207 if (dbus_message_iter_init(message, &iter) == FALSE)
2208 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2210 for (i = 0; signal_map[i].interface != NULL; i++) {
2211 if (dbus_message_has_interface(message,
2212 signal_map[i].interface) == FALSE)
2215 if (dbus_message_has_member(message,
2216 signal_map[i].member) == FALSE)
2219 signal_map[i].function(path, &iter);
2223 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2226 struct supplicant_regdom {
2227 GSupplicantCountryCallback callback;
2228 const void *user_data;
2231 static void country_result(const char *error,
2232 DBusMessageIter *iter, void *user_data)
2234 struct supplicant_regdom *regdom = user_data;
2237 SUPPLICANT_DBG("Country setting result");
2239 if (user_data == NULL)
2242 if (error == NULL) {
2243 alpha2 = (char *)regdom->user_data;
2245 SUPPLICANT_DBG("Country setting failure %s", error);
2249 if (regdom->callback)
2250 regdom->callback(alpha2);
2255 static void country_params(DBusMessageIter *iter, void *user_data)
2257 struct supplicant_regdom *regdom = user_data;
2258 const char *country;
2260 country = regdom->user_data;
2262 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &country);
2265 int g_supplicant_set_country(const char *alpha2,
2266 GSupplicantCountryCallback callback,
2267 const void *user_data)
2269 struct supplicant_regdom *regdom;
2271 SUPPLICANT_DBG("Country setting %s", alpha2);
2273 if (system_available == FALSE)
2276 regdom = dbus_malloc0(sizeof(*regdom));
2280 regdom->callback = callback;
2281 regdom->user_data = user_data;
2283 return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
2284 "Country", DBUS_TYPE_STRING_AS_STRING,
2285 country_params, country_result,
2289 struct interface_data {
2290 GSupplicantInterface *interface;
2291 GSupplicantInterfaceCallback callback;
2295 struct interface_create_data {
2299 GSupplicantInterface *interface;
2300 GSupplicantInterfaceCallback callback;
2304 struct interface_connect_data {
2305 GSupplicantInterface *interface;
2306 GSupplicantInterfaceCallback callback;
2307 GSupplicantSSID *ssid;
2311 struct interface_scan_data {
2312 GSupplicantInterface *interface;
2313 GSupplicantInterfaceCallback callback;
2314 GSupplicantScanParams *scan_params;
2318 static void interface_create_property(const char *key, DBusMessageIter *iter,
2321 struct interface_create_data *data = user_data;
2322 GSupplicantInterface *interface = data->interface;
2325 if (data->callback != NULL)
2326 data->callback(0, data->interface, data->user_data);
2331 interface_property(key, iter, interface);
2334 static void interface_create_result(const char *error,
2335 DBusMessageIter *iter, void *user_data)
2337 struct interface_create_data *data = user_data;
2338 const char *path = NULL;
2343 if (error != NULL) {
2344 g_critical("error %s", error);
2349 dbus_message_iter_get_basic(iter, &path);
2355 if (system_available == FALSE) {
2360 data->interface = g_hash_table_lookup(interface_table, path);
2361 if (data->interface == NULL) {
2362 data->interface = interface_alloc(path);
2363 if (data->interface == NULL) {
2369 err = supplicant_dbus_property_get_all(path,
2370 SUPPLICANT_INTERFACE ".Interface",
2371 interface_create_property, data);
2376 if (data->callback != NULL)
2377 data->callback(err, NULL, data->user_data);
2382 static void interface_create_params(DBusMessageIter *iter, void *user_data)
2384 struct interface_create_data *data = user_data;
2385 DBusMessageIter dict;
2389 supplicant_dbus_dict_open(iter, &dict);
2391 supplicant_dbus_dict_append_basic(&dict, "Ifname",
2392 DBUS_TYPE_STRING, &data->ifname);
2394 if (data->driver != NULL)
2395 supplicant_dbus_dict_append_basic(&dict, "Driver",
2396 DBUS_TYPE_STRING, &data->driver);
2398 if (data->bridge != NULL)
2399 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
2400 DBUS_TYPE_STRING, &data->bridge);
2402 supplicant_dbus_dict_close(iter, &dict);
2405 static void interface_get_result(const char *error,
2406 DBusMessageIter *iter, void *user_data)
2408 struct interface_create_data *data = user_data;
2409 GSupplicantInterface *interface;
2410 const char *path = NULL;
2415 if (error != NULL) {
2416 SUPPLICANT_DBG("Interface not created yet");
2421 dbus_message_iter_get_basic(iter, &path);
2427 interface = g_hash_table_lookup(interface_table, path);
2428 if (interface == NULL) {
2433 if (data->callback != NULL)
2434 data->callback(0, interface, data->user_data);
2441 if (system_available == FALSE) {
2446 SUPPLICANT_DBG("Creating interface");
2448 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
2449 SUPPLICANT_INTERFACE,
2451 interface_create_params,
2452 interface_create_result, data);
2457 if (data->callback != NULL)
2458 data->callback(err, NULL, data->user_data);
2463 static void interface_get_params(DBusMessageIter *iter, void *user_data)
2465 struct interface_create_data *data = user_data;
2469 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
2472 int g_supplicant_interface_create(const char *ifname, const char *driver,
2474 GSupplicantInterfaceCallback callback,
2477 struct interface_create_data *data;
2479 SUPPLICANT_DBG("ifname %s", ifname);
2484 if (system_available == FALSE)
2487 data = dbus_malloc0(sizeof(*data));
2491 data->ifname = ifname;
2492 data->driver = driver;
2493 data->bridge = bridge;
2494 data->callback = callback;
2495 data->user_data = user_data;
2497 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2498 SUPPLICANT_INTERFACE,
2500 interface_get_params,
2501 interface_get_result, data);
2504 static void interface_remove_result(const char *error,
2505 DBusMessageIter *iter, void *user_data)
2507 struct interface_data *data = user_data;
2510 if (error != NULL) {
2515 if (system_available == FALSE) {
2521 * The gsupplicant interface is already freed by the InterfaceRemoved
2522 * signal callback. Simply invoke the interface_data callback.
2527 if (data->callback != NULL)
2528 data->callback(err, NULL, data->user_data);
2534 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
2536 struct interface_data *data = user_data;
2538 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2539 &data->interface->path);
2543 int g_supplicant_interface_remove(GSupplicantInterface *interface,
2544 GSupplicantInterfaceCallback callback,
2547 struct interface_data *data;
2549 if (interface == NULL)
2552 if (system_available == FALSE)
2555 data = dbus_malloc0(sizeof(*data));
2559 data->interface = interface;
2560 data->callback = callback;
2561 data->user_data = user_data;
2563 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2564 SUPPLICANT_INTERFACE,
2566 interface_remove_params,
2567 interface_remove_result, data);
2570 static void interface_scan_result(const char *error,
2571 DBusMessageIter *iter, void *user_data)
2573 struct interface_scan_data *data = user_data;
2575 if (error != NULL) {
2576 SUPPLICANT_DBG("error %s", error);
2578 if (data->callback != NULL)
2579 data->callback(-EIO, data->interface, data->user_data);
2581 data->interface->scan_callback = data->callback;
2582 data->interface->scan_data = data->user_data;
2585 if (data != NULL && data->scan_params != NULL)
2586 g_free(data->scan_params);
2591 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
2593 DBusMessageIter data;
2594 unsigned int width = 0; /* Not used by wpa_supplicant atm */
2596 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
2598 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
2599 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
2601 dbus_message_iter_close_container(iter, &data);
2604 static void add_scan_frequencies(DBusMessageIter *iter,
2607 GSupplicantScanParams *scan_data = user_data;
2611 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
2612 freq = scan_data->freqs[i];
2616 add_scan_frequency(iter, freq);
2620 static void append_ssid(DBusMessageIter *iter,
2621 const void *ssid, unsigned int len)
2623 DBusMessageIter array;
2625 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
2626 DBUS_TYPE_BYTE_AS_STRING, &array);
2628 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
2630 dbus_message_iter_close_container(iter, &array);
2633 static void append_ssids(DBusMessageIter *iter, void *user_data)
2635 GSupplicantScanParams *scan_data = user_data;
2638 for (i = 0; i < scan_data->num_ssids; i++)
2639 append_ssid(iter, scan_data->ssids[i].ssid,
2640 scan_data->ssids[i].ssid_len);
2643 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
2644 supplicant_dbus_array_function function,
2647 GSupplicantScanParams *scan_params = user_data;
2648 DBusMessageIter entry, value, array;
2649 const char *key = "Channels";
2651 if (scan_params->freqs[0] != 0) {
2652 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
2655 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
2657 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
2658 DBUS_TYPE_ARRAY_AS_STRING
2659 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2660 DBUS_TYPE_UINT32_AS_STRING
2661 DBUS_TYPE_UINT32_AS_STRING
2662 DBUS_STRUCT_END_CHAR_AS_STRING,
2665 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
2666 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2667 DBUS_TYPE_UINT32_AS_STRING
2668 DBUS_TYPE_UINT32_AS_STRING
2669 DBUS_STRUCT_END_CHAR_AS_STRING,
2673 function(&array, user_data);
2675 dbus_message_iter_close_container(&value, &array);
2676 dbus_message_iter_close_container(&entry, &value);
2677 dbus_message_iter_close_container(dict, &entry);
2681 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
2683 DBusMessageIter dict;
2684 const char *type = "passive";
2685 struct interface_scan_data *data = user_data;
2687 supplicant_dbus_dict_open(iter, &dict);
2689 if (data && data->scan_params) {
2692 supplicant_dbus_dict_append_basic(&dict, "Type",
2693 DBUS_TYPE_STRING, &type);
2695 supplicant_dbus_dict_append_array(&dict, "SSIDs",
2700 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
2703 supplicant_dbus_dict_append_basic(&dict, "Type",
2704 DBUS_TYPE_STRING, &type);
2706 supplicant_dbus_dict_close(iter, &dict);
2709 int g_supplicant_interface_scan(GSupplicantInterface *interface,
2710 GSupplicantScanParams *scan_data,
2711 GSupplicantInterfaceCallback callback,
2714 struct interface_scan_data *data;
2717 if (interface == NULL)
2720 if (system_available == FALSE)
2723 if (interface->scanning == TRUE)
2726 switch (interface->state) {
2727 case G_SUPPLICANT_STATE_AUTHENTICATING:
2728 case G_SUPPLICANT_STATE_ASSOCIATING:
2729 case G_SUPPLICANT_STATE_ASSOCIATED:
2730 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2731 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2733 case G_SUPPLICANT_STATE_UNKNOWN:
2734 case G_SUPPLICANT_STATE_DISCONNECTED:
2735 case G_SUPPLICANT_STATE_INACTIVE:
2736 case G_SUPPLICANT_STATE_SCANNING:
2737 case G_SUPPLICANT_STATE_COMPLETED:
2741 data = dbus_malloc0(sizeof(*data));
2745 data->interface = interface;
2746 data->callback = callback;
2747 data->user_data = user_data;
2748 data->scan_params = scan_data;
2750 ret = supplicant_dbus_method_call(interface->path,
2751 SUPPLICANT_INTERFACE ".Interface", "Scan",
2752 interface_scan_params, interface_scan_result, data);
2760 static int parse_supplicant_error(DBusMessageIter *iter)
2762 int err = -ECANCELED;
2765 /* If the given passphrase is malformed wpa_s returns
2766 * "invalid message format" but this error should be interpreted as
2769 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
2770 dbus_message_iter_get_basic(iter, &key);
2771 if (strncmp(key, "psk", 3) == 0 ||
2772 strncmp(key, "wep_key", 7) == 0 ||
2773 strcmp(key, "invalid message format") == 0) {
2777 dbus_message_iter_next(iter);
2783 static void interface_select_network_result(const char *error,
2784 DBusMessageIter *iter, void *user_data)
2786 struct interface_connect_data *data = user_data;
2792 if (error != NULL) {
2793 SUPPLICANT_DBG("SelectNetwork error %s", error);
2794 err = parse_supplicant_error(iter);
2797 if (data->callback != NULL)
2798 data->callback(err, data->interface, data->user_data);
2804 static void interface_select_network_params(DBusMessageIter *iter,
2807 struct interface_connect_data *data = user_data;
2808 GSupplicantInterface *interface = data->interface;
2810 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2811 &interface->network_path);
2814 static void interface_add_network_result(const char *error,
2815 DBusMessageIter *iter, void *user_data)
2817 struct interface_connect_data *data = user_data;
2818 GSupplicantInterface *interface = data->interface;
2825 dbus_message_iter_get_basic(iter, &path);
2829 SUPPLICANT_DBG("PATH: %s", path);
2831 g_free(interface->network_path);
2832 interface->network_path = g_strdup(path);
2834 supplicant_dbus_method_call(data->interface->path,
2835 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
2836 interface_select_network_params,
2837 interface_select_network_result, data);
2842 SUPPLICANT_DBG("AddNetwork error %s", error);
2843 err = parse_supplicant_error(iter);
2844 if (data->callback != NULL)
2845 data->callback(err, data->interface, data->user_data);
2847 g_free(interface->network_path);
2848 interface->network_path = NULL;
2853 static void add_network_security_wep(DBusMessageIter *dict,
2854 GSupplicantSSID *ssid)
2856 const char *auth_alg = "OPEN SHARED";
2857 const char *key_index = "0";
2859 supplicant_dbus_dict_append_basic(dict, "auth_alg",
2860 DBUS_TYPE_STRING, &auth_alg);
2862 if (ssid->passphrase) {
2863 int size = strlen(ssid->passphrase);
2864 if (size == 10 || size == 26) {
2865 unsigned char *key = g_try_malloc(13);
2869 memset(tmp, 0, sizeof(tmp));
2873 for (i = 0; i < size / 2; i++) {
2874 memcpy(tmp, ssid->passphrase + (i * 2), 2);
2875 key[i] = (unsigned char) strtol(tmp, NULL, 16);
2878 supplicant_dbus_dict_append_fixed_array(dict,
2883 } else if (size == 5 || size == 13) {
2884 unsigned char *key = g_try_malloc(13);
2890 for (i = 0; i < size; i++)
2891 key[i] = (unsigned char) ssid->passphrase[i];
2893 supplicant_dbus_dict_append_fixed_array(dict,
2899 supplicant_dbus_dict_append_basic(dict,
2904 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
2905 DBUS_TYPE_STRING, &key_index);
2909 static dbus_bool_t is_psk_raw_key(const char *psk)
2913 /* A raw key is always 64 bytes length... */
2914 if (strlen(psk) != 64)
2917 /* ... and its content is in hex representation */
2918 for (i = 0; i < 64; i++)
2919 if (!isxdigit((unsigned char) psk[i]))
2925 static void add_network_security_psk(DBusMessageIter *dict,
2926 GSupplicantSSID *ssid)
2928 if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
2930 if (is_psk_raw_key(ssid->passphrase) == TRUE)
2931 supplicant_dbus_property_append_fixed_array(dict,
2932 "psk", DBUS_TYPE_BYTE,
2933 &ssid->passphrase, 64);
2935 supplicant_dbus_dict_append_basic(dict, "psk",
2941 static void add_network_security_tls(DBusMessageIter *dict,
2942 GSupplicantSSID *ssid)
2945 * For TLS, we at least need:
2946 * The client certificate
2947 * The client private key file
2948 * The client private key file password
2950 * The Authority certificate is optional.
2952 if (ssid->client_cert_path == NULL)
2955 if (ssid->private_key_path == NULL)
2958 if (ssid->private_key_passphrase == NULL)
2961 if (ssid->ca_cert_path)
2962 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2963 DBUS_TYPE_STRING, &ssid->ca_cert_path);
2965 supplicant_dbus_dict_append_basic(dict, "private_key",
2967 &ssid->private_key_path);
2968 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2970 &ssid->private_key_passphrase);
2971 supplicant_dbus_dict_append_basic(dict, "client_cert",
2973 &ssid->client_cert_path);
2976 static void add_network_security_peap(DBusMessageIter *dict,
2977 GSupplicantSSID *ssid)
2982 * For PEAP/TTLS, we at least need
2983 * The authority certificate
2984 * The 2nd phase authentication method
2985 * The 2nd phase passphrase
2987 * The Client certificate is optional although strongly recommended
2988 * When setting it, we need in addition
2989 * The Client private key file
2990 * The Client private key file password
2992 if (ssid->passphrase == NULL)
2995 if (ssid->phase2_auth == NULL)
2998 if (ssid->client_cert_path) {
2999 if (ssid->private_key_path == NULL)
3002 if (ssid->private_key_passphrase == NULL)
3005 supplicant_dbus_dict_append_basic(dict, "client_cert",
3007 &ssid->client_cert_path);
3009 supplicant_dbus_dict_append_basic(dict, "private_key",
3011 &ssid->private_key_path);
3013 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3015 &ssid->private_key_passphrase);
3019 if (g_str_has_prefix(ssid->phase2_auth, "EAP-") == TRUE) {
3020 phase2_auth = g_strdup_printf("autheap=%s",
3021 ssid->phase2_auth + strlen("EAP-"));
3023 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
3025 supplicant_dbus_dict_append_basic(dict, "password",
3029 if (ssid->ca_cert_path)
3030 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3032 &ssid->ca_cert_path);
3034 supplicant_dbus_dict_append_basic(dict, "phase2",
3038 g_free(phase2_auth);
3041 static void add_network_security_eap(DBusMessageIter *dict,
3042 GSupplicantSSID *ssid)
3046 if (ssid->eap == NULL || ssid->identity == NULL)
3049 if (g_strcmp0(ssid->eap, "tls") == 0) {
3050 add_network_security_tls(dict, ssid);
3051 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
3052 g_strcmp0(ssid->eap, "ttls") == 0) {
3053 add_network_security_peap(dict, ssid);
3057 eap_value = g_ascii_strup(ssid->eap, -1);
3059 supplicant_dbus_dict_append_basic(dict, "eap",
3062 supplicant_dbus_dict_append_basic(dict, "identity",
3069 static void add_network_security_ciphers(DBusMessageIter *dict,
3070 GSupplicantSSID *ssid)
3072 unsigned int p_cipher, g_cipher, i;
3073 char *pairwise, *group;
3074 char *pair_ciphers[4];
3075 char *group_ciphers[5];
3077 p_cipher = ssid->pairwise_cipher;
3078 g_cipher = ssid->group_cipher;
3080 if (p_cipher == 0 && g_cipher == 0)
3085 if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
3086 pair_ciphers[i++] = "CCMP";
3088 if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
3089 pair_ciphers[i++] = "TKIP";
3091 if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
3092 pair_ciphers[i++] = "NONE";
3094 pair_ciphers[i] = NULL;
3098 if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
3099 group_ciphers[i++] = "CCMP";
3101 if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
3102 group_ciphers[i++] = "TKIP";
3104 if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
3105 group_ciphers[i++] = "WEP104";
3107 if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
3108 group_ciphers[i++] = "WEP40";
3110 group_ciphers[i] = NULL;
3112 pairwise = g_strjoinv(" ", pair_ciphers);
3113 group = g_strjoinv(" ", group_ciphers);
3115 SUPPLICANT_DBG("cipher %s %s", pairwise, group);
3117 supplicant_dbus_dict_append_basic(dict, "pairwise",
3120 supplicant_dbus_dict_append_basic(dict, "group",
3128 static void add_network_security_proto(DBusMessageIter *dict,
3129 GSupplicantSSID *ssid)
3131 unsigned int protocol, i;
3135 protocol = ssid->protocol;
3142 if (protocol & G_SUPPLICANT_PROTO_RSN)
3143 protos[i++] = "RSN";
3145 if (protocol & G_SUPPLICANT_PROTO_WPA)
3146 protos[i++] = "WPA";
3150 proto = g_strjoinv(" ", protos);
3152 SUPPLICANT_DBG("proto %s", proto);
3154 supplicant_dbus_dict_append_basic(dict, "proto",
3161 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
3165 switch (ssid->security) {
3166 case G_SUPPLICANT_SECURITY_UNKNOWN:
3167 case G_SUPPLICANT_SECURITY_NONE:
3168 case G_SUPPLICANT_SECURITY_WEP:
3170 add_network_security_wep(dict, ssid);
3171 add_network_security_ciphers(dict, ssid);
3173 case G_SUPPLICANT_SECURITY_PSK:
3174 key_mgmt = "WPA-PSK";
3175 add_network_security_psk(dict, ssid);
3176 add_network_security_ciphers(dict, ssid);
3177 add_network_security_proto(dict, ssid);
3179 case G_SUPPLICANT_SECURITY_IEEE8021X:
3180 key_mgmt = "WPA-EAP";
3181 add_network_security_eap(dict, ssid);
3182 add_network_security_ciphers(dict, ssid);
3183 add_network_security_proto(dict, ssid);
3187 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
3188 DBUS_TYPE_STRING, &key_mgmt);
3191 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
3195 switch (ssid->mode) {
3196 case G_SUPPLICANT_MODE_UNKNOWN:
3197 case G_SUPPLICANT_MODE_INFRA:
3200 case G_SUPPLICANT_MODE_IBSS:
3203 case G_SUPPLICANT_MODE_MASTER:
3208 supplicant_dbus_dict_append_basic(dict, "mode",
3209 DBUS_TYPE_UINT32, &mode);
3212 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
3214 DBusMessageIter dict;
3215 struct interface_connect_data *data = user_data;
3216 GSupplicantSSID *ssid = data->ssid;
3218 supplicant_dbus_dict_open(iter, &dict);
3220 if (ssid->scan_ssid)
3221 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
3222 DBUS_TYPE_UINT32, &ssid->scan_ssid);
3225 supplicant_dbus_dict_append_basic(&dict, "frequency",
3226 DBUS_TYPE_UINT32, &ssid->freq);
3228 if (ssid->bgscan != NULL)
3229 supplicant_dbus_dict_append_basic(&dict, "bgscan",
3230 DBUS_TYPE_STRING, &ssid->bgscan);
3232 add_network_mode(&dict, ssid);
3234 add_network_security(&dict, ssid);
3236 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
3237 DBUS_TYPE_BYTE, &ssid->ssid,
3240 supplicant_dbus_dict_close(iter, &dict);
3243 static void interface_wps_start_result(const char *error,
3244 DBusMessageIter *iter, void *user_data)
3246 struct interface_connect_data *data = user_data;
3250 SUPPLICANT_DBG("error: %s", error);
3256 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
3258 struct interface_connect_data *data = user_data;
3259 GSupplicantSSID *ssid = data->ssid;
3260 const char *role = "enrollee", *type;
3261 DBusMessageIter dict;
3265 supplicant_dbus_dict_open(iter, &dict);
3267 supplicant_dbus_dict_append_basic(&dict, "Role",
3268 DBUS_TYPE_STRING, &role);
3271 if (ssid->pin_wps != NULL) {
3273 supplicant_dbus_dict_append_basic(&dict, "Pin",
3274 DBUS_TYPE_STRING, &ssid->pin_wps);
3277 supplicant_dbus_dict_append_basic(&dict, "Type",
3278 DBUS_TYPE_STRING, &type);
3280 supplicant_dbus_dict_close(iter, &dict);
3283 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
3285 struct interface_connect_data *data = user_data;
3289 if (error != NULL) {
3290 SUPPLICANT_DBG("error: %s", error);
3296 supplicant_dbus_method_call(data->interface->path,
3297 SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
3298 interface_add_wps_params,
3299 interface_wps_start_result, data);
3302 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
3304 dbus_bool_t credentials = TRUE;
3308 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
3312 int g_supplicant_interface_connect(GSupplicantInterface *interface,
3313 GSupplicantSSID *ssid,
3314 GSupplicantInterfaceCallback callback,
3317 struct interface_connect_data *data;
3320 if (interface == NULL)
3323 if (system_available == FALSE)
3326 /* TODO: Check if we're already connected and switch */
3328 data = dbus_malloc0(sizeof(*data));
3332 data->interface = interface;
3333 data->callback = callback;
3335 data->user_data = user_data;
3337 if (ssid->use_wps == TRUE) {
3338 g_free(interface->wps_cred.key);
3339 memset(&interface->wps_cred, 0,
3340 sizeof(struct _GSupplicantWpsCredentials));
3342 ret = supplicant_dbus_property_set(interface->path,
3343 SUPPLICANT_INTERFACE ".Interface.WPS",
3344 "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
3345 wps_process_credentials, wps_start, data);
3347 ret = supplicant_dbus_method_call(interface->path,
3348 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
3349 interface_add_network_params,
3350 interface_add_network_result, data);
3355 return -EINPROGRESS;
3358 static void network_remove_result(const char *error,
3359 DBusMessageIter *iter, void *user_data)
3361 struct interface_data *data = user_data;
3369 if (data->callback != NULL)
3370 data->callback(result, data->interface, data->user_data);
3375 static void network_remove_params(DBusMessageIter *iter, void *user_data)
3377 struct interface_data *data = user_data;
3378 const char *path = data->interface->network_path;
3380 SUPPLICANT_DBG("path %s", path);
3382 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
3385 static int network_remove(struct interface_data *data)
3387 GSupplicantInterface *interface = data->interface;
3391 return supplicant_dbus_method_call(interface->path,
3392 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
3393 network_remove_params, network_remove_result, data);
3396 static void interface_disconnect_result(const char *error,
3397 DBusMessageIter *iter, void *user_data)
3399 struct interface_data *data = user_data;
3403 if (error != NULL && data->callback != NULL)
3404 data->callback(-EIO, data->interface, data->user_data);
3406 /* If we are disconnecting from previous WPS successful
3407 * association. i.e.: it did not went through AddNetwork,
3408 * and interface->network_path was never set. */
3409 if (data->interface->network_path == NULL)
3412 network_remove(data);
3415 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
3416 GSupplicantInterfaceCallback callback,
3419 struct interface_data *data;
3423 if (interface == NULL)
3426 if (system_available == FALSE)
3429 data = dbus_malloc0(sizeof(*data));
3433 data->interface = interface;
3434 data->callback = callback;
3435 data->user_data = user_data;
3437 return supplicant_dbus_method_call(interface->path,
3438 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
3439 NULL, interface_disconnect_result, data);
3443 static const char *g_supplicant_rule0 = "type=signal,"
3444 "path=" DBUS_PATH_DBUS ","
3445 "sender=" DBUS_SERVICE_DBUS ","
3446 "interface=" DBUS_INTERFACE_DBUS ","
3447 "member=NameOwnerChanged,"
3448 "arg0=" SUPPLICANT_SERVICE;
3449 static const char *g_supplicant_rule1 = "type=signal,"
3450 "interface=" SUPPLICANT_INTERFACE;
3451 static const char *g_supplicant_rule2 = "type=signal,"
3452 "interface=" SUPPLICANT_INTERFACE ".Interface";
3453 static const char *g_supplicant_rule3 = "type=signal,"
3454 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
3455 static const char *g_supplicant_rule4 = "type=signal,"
3456 "interface=" SUPPLICANT_INTERFACE ".BSS";
3457 static const char *g_supplicant_rule5 = "type=signal,"
3458 "interface=" SUPPLICANT_INTERFACE ".Network";
3460 static void invoke_introspect_method(void)
3462 DBusMessage *message;
3464 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
3466 DBUS_INTERFACE_INTROSPECTABLE,
3469 if (message == NULL)
3472 dbus_message_set_no_reply(message, TRUE);
3473 dbus_connection_send(connection, message, NULL);
3474 dbus_message_unref(message);
3477 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
3479 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3480 if (connection == NULL)
3483 if (dbus_connection_add_filter(connection,
3484 g_supplicant_filter, NULL, NULL) == FALSE) {
3485 dbus_connection_unref(connection);
3490 callbacks_pointer = callbacks;
3493 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
3494 NULL, remove_interface);
3496 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
3499 supplicant_dbus_setup(connection);
3501 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
3502 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
3503 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
3504 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
3505 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
3506 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
3507 dbus_connection_flush(connection);
3509 if (dbus_bus_name_has_owner(connection,
3510 SUPPLICANT_SERVICE, NULL) == TRUE) {
3511 system_available = TRUE;
3512 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
3513 SUPPLICANT_INTERFACE,
3514 service_property, NULL);
3516 invoke_introspect_method();
3521 static void unregister_interface_remove_params(DBusMessageIter *iter,
3524 const char *path = user_data;
3526 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3531 static void unregister_remove_interface(gpointer key, gpointer value,
3534 GSupplicantInterface *interface = value;
3536 supplicant_dbus_method_call(SUPPLICANT_PATH,
3537 SUPPLICANT_INTERFACE,
3539 unregister_interface_remove_params,
3540 NULL, interface->path);
3543 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
3547 if (connection != NULL) {
3548 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
3549 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
3550 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
3551 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
3552 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
3553 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
3554 dbus_connection_flush(connection);
3556 dbus_connection_remove_filter(connection,
3557 g_supplicant_filter, NULL);
3560 if (bss_mapping != NULL) {
3561 g_hash_table_destroy(bss_mapping);
3565 if (system_available == TRUE)
3566 callback_system_killed();
3568 if (interface_table != NULL) {
3569 g_hash_table_foreach(interface_table,
3570 unregister_remove_interface, NULL);
3571 g_hash_table_destroy(interface_table);
3572 interface_table = NULL;
3575 if (connection != NULL) {
3576 dbus_connection_unref(connection);
3580 callbacks_pointer = NULL;