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
37 #include "gsupplicant.h"
41 #define IEEE80211_CAP_ESS 0x0001
42 #define IEEE80211_CAP_IBSS 0x0002
43 #define IEEE80211_CAP_PRIVACY 0x0010
45 static DBusConnection *connection;
47 static const GSupplicantCallbacks *callbacks_pointer;
49 static dbus_bool_t system_available = FALSE;
50 static dbus_bool_t system_ready = FALSE;
52 static dbus_int32_t debug_level;
53 static dbus_bool_t debug_timestamp = FALSE;
54 static dbus_bool_t debug_showkeys = FALSE;
56 static const char *debug_strings[] = {
57 "msgdump", "debug", "info", "warning", "error", NULL
60 static unsigned int eap_methods;
67 static struct strvalmap eap_method_map[] = {
68 { "MD5", G_SUPPLICANT_EAP_METHOD_MD5 },
69 { "TLS", G_SUPPLICANT_EAP_METHOD_TLS },
70 { "MSCHAPV2", G_SUPPLICANT_EAP_METHOD_MSCHAPV2 },
71 { "PEAP", G_SUPPLICANT_EAP_METHOD_PEAP },
72 { "TTLS", G_SUPPLICANT_EAP_METHOD_TTLS },
73 { "GTC", G_SUPPLICANT_EAP_METHOD_GTC },
74 { "OTP", G_SUPPLICANT_EAP_METHOD_OTP },
75 { "LEAP", G_SUPPLICANT_EAP_METHOD_LEAP },
76 { "WSC", G_SUPPLICANT_EAP_METHOD_WSC },
80 static struct strvalmap keymgmt_map[] = {
81 { "none", G_SUPPLICANT_KEYMGMT_NONE },
82 { "ieee8021x", G_SUPPLICANT_KEYMGMT_IEEE8021X },
83 { "wpa-none", G_SUPPLICANT_KEYMGMT_WPA_NONE },
84 { "wpa-psk", G_SUPPLICANT_KEYMGMT_WPA_PSK },
85 { "wpa-psk-sha256", G_SUPPLICANT_KEYMGMT_WPA_PSK_256 },
86 { "wpa-ft-psk", G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
87 { "wpa-ft-eap", G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
88 { "wpa-eap", G_SUPPLICANT_KEYMGMT_WPA_EAP },
89 { "wpa-eap-sha256", G_SUPPLICANT_KEYMGMT_WPA_EAP_256 },
90 { "wps", G_SUPPLICANT_KEYMGMT_WPS },
94 static struct strvalmap authalg_capa_map[] = {
95 { "open", G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN },
96 { "shared", G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED },
97 { "leap", G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP },
101 static struct strvalmap proto_capa_map[] = {
102 { "wpa", G_SUPPLICANT_CAPABILITY_PROTO_WPA },
103 { "rsn", G_SUPPLICANT_CAPABILITY_PROTO_RSN },
107 static struct strvalmap group_map[] = {
108 { "wep40", G_SUPPLICANT_GROUP_WEP40 },
109 { "wep104", G_SUPPLICANT_GROUP_WEP104 },
110 { "tkip", G_SUPPLICANT_GROUP_TKIP },
111 { "ccmp", G_SUPPLICANT_GROUP_CCMP },
115 static struct strvalmap pairwise_map[] = {
116 { "none", G_SUPPLICANT_PAIRWISE_NONE },
117 { "tkip", G_SUPPLICANT_PAIRWISE_TKIP },
118 { "ccmp", G_SUPPLICANT_PAIRWISE_CCMP },
122 static struct strvalmap scan_capa_map[] = {
123 { "active", G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE },
124 { "passive", G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE },
125 { "ssid", G_SUPPLICANT_CAPABILITY_SCAN_SSID },
129 static struct strvalmap mode_capa_map[] = {
130 { "infrastructure", G_SUPPLICANT_CAPABILITY_MODE_INFRA },
131 { "ad-hoc", G_SUPPLICANT_CAPABILITY_MODE_IBSS },
132 { "ap", G_SUPPLICANT_CAPABILITY_MODE_AP },
136 static GHashTable *interface_table;
137 static GHashTable *bss_mapping;
139 struct _GSupplicantWpsCredentials {
140 unsigned char ssid[32];
141 unsigned int ssid_len;
145 struct _GSupplicantInterface {
148 unsigned int keymgmt_capa;
149 unsigned int authalg_capa;
150 unsigned int proto_capa;
151 unsigned int group_capa;
152 unsigned int pairwise_capa;
153 unsigned int scan_capa;
154 unsigned int mode_capa;
156 GSupplicantState state;
157 dbus_bool_t scanning;
158 GSupplicantInterfaceCallback scan_callback;
164 struct _GSupplicantWpsCredentials wps_cred;
165 GSupplicantWpsState wps_state;
166 GHashTable *network_table;
167 GHashTable *net_mapping;
168 GHashTable *bss_mapping;
172 struct _GSupplicantNetwork {
173 GSupplicantInterface *interface;
177 unsigned char ssid[32];
178 unsigned int ssid_len;
180 GSupplicantMode mode;
181 GSupplicantSecurity security;
183 GHashTable *bss_table;
184 GHashTable *config_table;
187 struct g_supplicant_bss {
188 GSupplicantInterface *interface;
190 unsigned char bssid[6];
191 unsigned char ssid[32];
192 unsigned int ssid_len;
193 dbus_uint16_t frequency;
194 dbus_uint32_t maxrate;
196 GSupplicantMode mode;
197 GSupplicantSecurity security;
198 unsigned int keymgmt;
199 unsigned int pairwise;
203 dbus_bool_t ieee8021x;
206 static inline void debug(const char *format, ...)
211 if (callbacks_pointer->debug == NULL)
214 va_start(ap, format);
216 if (vsnprintf(str, sizeof(str), format, ap) > 0)
217 callbacks_pointer->debug(str);
222 #define SUPPLICANT_DBG(fmt, arg...) \
223 debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
225 static GSupplicantMode string2mode(const char *mode)
228 return G_SUPPLICANT_MODE_UNKNOWN;
230 if (g_str_equal(mode, "infrastructure") == TRUE)
231 return G_SUPPLICANT_MODE_INFRA;
232 else if (g_str_equal(mode, "ad-hoc") == TRUE)
233 return G_SUPPLICANT_MODE_IBSS;
235 return G_SUPPLICANT_MODE_UNKNOWN;
238 static const char *mode2string(GSupplicantMode mode)
241 case G_SUPPLICANT_MODE_UNKNOWN:
243 case G_SUPPLICANT_MODE_INFRA:
245 case G_SUPPLICANT_MODE_IBSS:
247 case G_SUPPLICANT_MODE_MASTER:
254 static const char *security2string(GSupplicantSecurity security)
257 case G_SUPPLICANT_SECURITY_UNKNOWN:
259 case G_SUPPLICANT_SECURITY_NONE:
261 case G_SUPPLICANT_SECURITY_WEP:
263 case G_SUPPLICANT_SECURITY_PSK:
265 case G_SUPPLICANT_SECURITY_IEEE8021X:
272 static GSupplicantState string2state(const char *state)
275 return G_SUPPLICANT_STATE_UNKNOWN;
277 if (g_str_equal(state, "unknown") == TRUE)
278 return G_SUPPLICANT_STATE_UNKNOWN;
279 else if (g_str_equal(state, "disconnected") == TRUE)
280 return G_SUPPLICANT_STATE_DISCONNECTED;
281 else if (g_str_equal(state, "inactive") == TRUE)
282 return G_SUPPLICANT_STATE_INACTIVE;
283 else if (g_str_equal(state, "scanning") == TRUE)
284 return G_SUPPLICANT_STATE_SCANNING;
285 else if (g_str_equal(state, "authenticating") == TRUE)
286 return G_SUPPLICANT_STATE_AUTHENTICATING;
287 else if (g_str_equal(state, "associating") == TRUE)
288 return G_SUPPLICANT_STATE_ASSOCIATING;
289 else if (g_str_equal(state, "associated") == TRUE)
290 return G_SUPPLICANT_STATE_ASSOCIATED;
291 else if (g_str_equal(state, "group_handshake") == TRUE)
292 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
293 else if (g_str_equal(state, "4way_handshake") == TRUE)
294 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
295 else if (g_str_equal(state, "completed") == TRUE)
296 return G_SUPPLICANT_STATE_COMPLETED;
298 return G_SUPPLICANT_STATE_UNKNOWN;
301 static void callback_system_ready(void)
303 if (system_ready == TRUE)
308 if (callbacks_pointer == NULL)
311 if (callbacks_pointer->system_ready == NULL)
314 callbacks_pointer->system_ready();
317 static void callback_system_killed(void)
319 system_ready = FALSE;
321 if (callbacks_pointer == NULL)
324 if (callbacks_pointer->system_killed == NULL)
327 callbacks_pointer->system_killed();
330 static void callback_interface_added(GSupplicantInterface *interface)
334 if (callbacks_pointer == NULL)
337 if (callbacks_pointer->interface_added == NULL)
340 callbacks_pointer->interface_added(interface);
343 static void callback_interface_state(GSupplicantInterface *interface)
345 if (callbacks_pointer == NULL)
348 if (callbacks_pointer->interface_state == NULL)
351 callbacks_pointer->interface_state(interface);
354 static void callback_interface_removed(GSupplicantInterface *interface)
356 if (callbacks_pointer == NULL)
359 if (callbacks_pointer->interface_removed == NULL)
362 callbacks_pointer->interface_removed(interface);
365 static void callback_scan_started(GSupplicantInterface *interface)
367 if (callbacks_pointer == NULL)
370 if (callbacks_pointer->scan_started == NULL)
373 callbacks_pointer->scan_started(interface);
376 static void callback_scan_finished(GSupplicantInterface *interface)
378 if (callbacks_pointer == NULL)
381 if (callbacks_pointer->scan_finished == NULL)
384 callbacks_pointer->scan_finished(interface);
387 static void callback_network_added(GSupplicantNetwork *network)
389 if (callbacks_pointer == NULL)
392 if (callbacks_pointer->network_added == NULL)
395 callbacks_pointer->network_added(network);
398 static void callback_network_removed(GSupplicantNetwork *network)
400 if (callbacks_pointer == NULL)
403 if (callbacks_pointer->network_removed == NULL)
406 callbacks_pointer->network_removed(network);
409 static void remove_interface(gpointer data)
411 GSupplicantInterface *interface = data;
413 g_hash_table_destroy(interface->bss_mapping);
414 g_hash_table_destroy(interface->net_mapping);
415 g_hash_table_destroy(interface->network_table);
417 callback_interface_removed(interface);
419 g_free(interface->wps_cred.key);
420 g_free(interface->path);
421 g_free(interface->network_path);
422 g_free(interface->ifname);
423 g_free(interface->driver);
424 g_free(interface->bridge);
428 static void remove_network(gpointer data)
430 GSupplicantNetwork *network = data;
432 g_hash_table_destroy(network->bss_table);
434 callback_network_removed(network);
436 g_hash_table_destroy(network->config_table);
438 g_free(network->path);
439 g_free(network->group);
440 g_free(network->name);
444 static void remove_bss(gpointer data)
446 struct g_supplicant_bss *bss = data;
452 static void debug_strvalmap(const char *label, struct strvalmap *map,
457 for (i = 0; map[i].str != NULL; i++) {
458 if (val & map[i].val)
459 SUPPLICANT_DBG("%s: %s", label, map[i].str);
463 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
465 GSupplicantInterface *interface = user_data;
466 const char *str = NULL;
469 dbus_message_iter_get_basic(iter, &str);
473 for (i = 0; keymgmt_map[i].str != NULL; i++)
474 if (strcmp(str, keymgmt_map[i].str) == 0) {
475 interface->keymgmt_capa |= keymgmt_map[i].val;
480 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
482 GSupplicantInterface *interface = user_data;
483 const char *str = NULL;
486 dbus_message_iter_get_basic(iter, &str);
490 for (i = 0; authalg_capa_map[i].str != NULL; i++)
491 if (strcmp(str, authalg_capa_map[i].str) == 0) {
492 interface->authalg_capa |= authalg_capa_map[i].val;
497 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
499 GSupplicantInterface *interface = user_data;
500 const char *str = NULL;
503 dbus_message_iter_get_basic(iter, &str);
507 for (i = 0; proto_capa_map[i].str != NULL; i++)
508 if (strcmp(str, proto_capa_map[i].str) == 0) {
509 interface->proto_capa |= proto_capa_map[i].val;
514 static void interface_capability_pairwise(DBusMessageIter *iter,
517 GSupplicantInterface *interface = user_data;
518 const char *str = NULL;
521 dbus_message_iter_get_basic(iter, &str);
525 for (i = 0; pairwise_map[i].str != NULL; i++)
526 if (strcmp(str, pairwise_map[i].str) == 0) {
527 interface->pairwise_capa |= pairwise_map[i].val;
532 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
534 GSupplicantInterface *interface = user_data;
535 const char *str = NULL;
538 dbus_message_iter_get_basic(iter, &str);
542 for (i = 0; group_map[i].str != NULL; i++)
543 if (strcmp(str, group_map[i].str) == 0) {
544 interface->group_capa |= group_map[i].val;
549 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
551 GSupplicantInterface *interface = user_data;
552 const char *str = NULL;
555 dbus_message_iter_get_basic(iter, &str);
559 for (i = 0; scan_capa_map[i].str != NULL; i++)
560 if (strcmp(str, scan_capa_map[i].str) == 0) {
561 interface->scan_capa |= scan_capa_map[i].val;
566 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
568 GSupplicantInterface *interface = user_data;
569 const char *str = NULL;
572 dbus_message_iter_get_basic(iter, &str);
576 for (i = 0; mode_capa_map[i].str != NULL; i++)
577 if (strcmp(str, mode_capa_map[i].str) == 0) {
578 interface->mode_capa |= mode_capa_map[i].val;
583 static void interface_capability(const char *key, DBusMessageIter *iter,
586 GSupplicantInterface *interface = user_data;
591 if (g_strcmp0(key, "KeyMgmt") == 0)
592 supplicant_dbus_array_foreach(iter,
593 interface_capability_keymgmt, interface);
594 else if (g_strcmp0(key, "AuthAlg") == 0)
595 supplicant_dbus_array_foreach(iter,
596 interface_capability_authalg, interface);
597 else if (g_strcmp0(key, "Protocol") == 0)
598 supplicant_dbus_array_foreach(iter,
599 interface_capability_proto, interface);
600 else if (g_strcmp0(key, "Pairwise") == 0)
601 supplicant_dbus_array_foreach(iter,
602 interface_capability_pairwise, interface);
603 else if (g_strcmp0(key, "Group") == 0)
604 supplicant_dbus_array_foreach(iter,
605 interface_capability_group, interface);
606 else if (g_strcmp0(key, "Scan") == 0)
607 supplicant_dbus_array_foreach(iter,
608 interface_capability_scan, interface);
609 else if (g_strcmp0(key, "Modes") == 0)
610 supplicant_dbus_array_foreach(iter,
611 interface_capability_mode, interface);
613 SUPPLICANT_DBG("key %s type %c",
614 key, dbus_message_iter_get_arg_type(iter));
617 static void set_apscan(DBusMessageIter *iter, void *user_data)
619 unsigned int ap_scan = *(unsigned int *)user_data;
621 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
624 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
625 unsigned int ap_scan)
627 return supplicant_dbus_property_set(interface->path,
628 SUPPLICANT_INTERFACE ".Interface",
629 "ApScan", DBUS_TYPE_UINT32_AS_STRING,
630 set_apscan, NULL, &ap_scan);
633 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
636 if (interface == NULL)
639 interface->data = data;
642 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
644 if (interface == NULL)
647 return interface->data;
650 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
652 if (interface == NULL)
655 return interface->ifname;
658 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
660 if (interface == NULL)
663 return interface->driver;
666 GSupplicantState g_supplicant_interface_get_state(
667 GSupplicantInterface *interface)
669 if (interface == NULL)
670 return G_SUPPLICANT_STATE_UNKNOWN;
672 return interface->state;
675 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
677 if (interface == NULL)
680 return (const char *)interface->wps_cred.key;
683 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
684 unsigned int *ssid_len)
686 if (ssid_len == NULL)
689 if (interface == NULL || interface->wps_cred.ssid == NULL) {
694 *ssid_len = interface->wps_cred.ssid_len;
695 return interface->wps_cred.ssid;
698 GSupplicantWpsState g_supplicant_interface_get_wps_state(
699 GSupplicantInterface *interface)
701 if (interface == NULL)
702 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
704 return interface->wps_state;
707 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
709 if (interface == NULL)
712 return interface->mode_capa;
715 GSupplicantInterface *g_supplicant_network_get_interface(
716 GSupplicantNetwork *network)
721 return network->interface;
724 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
726 if (network == NULL || network->name == NULL)
729 return network->name;
732 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
734 if (network == NULL || network->group == NULL)
737 return network->group;
740 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
742 if (network == NULL || network->path == NULL)
745 return network->path;
748 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
751 return G_SUPPLICANT_MODE_UNKNOWN;
753 return mode2string(network->mode);
756 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
759 return G_SUPPLICANT_SECURITY_UNKNOWN;
761 return security2string(network->security);
764 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
765 unsigned int *ssid_len)
767 if (network == NULL || network->ssid == NULL) {
772 *ssid_len = network->ssid_len;
773 return network->ssid;
776 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
781 return network->signal;
784 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
792 static void merge_network(GSupplicantNetwork *network)
795 const char *ssid, *mode, *key_mgmt;
796 unsigned int i, ssid_len;
799 ssid = g_hash_table_lookup(network->config_table, "ssid");
800 mode = g_hash_table_lookup(network->config_table, "mode");
801 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
803 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
806 ssid_len = strlen(ssid);
810 str = g_string_sized_new((ssid_len * 2) + 24);
814 for (i = 0; i < ssid_len; i++)
815 g_string_append_printf(str, "%02x", ssid[i]);
817 if (g_strcmp0(mode, "0") == 0)
818 g_string_append_printf(str, "_managed");
819 else if (g_strcmp0(mode, "1") == 0)
820 g_string_append_printf(str, "_adhoc");
822 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
823 g_string_append_printf(str, "_psk");
825 group = g_string_free(str, FALSE);
827 SUPPLICANT_DBG("%s", group);
831 g_hash_table_destroy(network->config_table);
833 g_free(network->path);
837 static void network_property(const char *key, DBusMessageIter *iter,
840 GSupplicantNetwork *network = user_data;
842 if (network->interface == NULL)
846 merge_network(network);
850 if (g_strcmp0(key, "Enabled") == 0) {
851 dbus_bool_t enabled = FALSE;
853 dbus_message_iter_get_basic(iter, &enabled);
854 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
855 const char *str = NULL;
857 dbus_message_iter_get_basic(iter, &str);
859 g_hash_table_replace(network->config_table,
860 g_strdup(key), g_strdup(str));
863 SUPPLICANT_DBG("key %s type %c",
864 key, dbus_message_iter_get_arg_type(iter));
867 static void interface_network_added(DBusMessageIter *iter, void *user_data)
869 GSupplicantInterface *interface = user_data;
870 GSupplicantNetwork *network;
871 const char *path = NULL;
875 dbus_message_iter_get_basic(iter, &path);
879 if (g_strcmp0(path, "/") == 0)
882 network = g_hash_table_lookup(interface->net_mapping, path);
886 network = g_try_new0(GSupplicantNetwork, 1);
890 network->interface = interface;
891 network->path = g_strdup(path);
893 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
896 dbus_message_iter_next(iter);
897 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
898 supplicant_dbus_property_foreach(iter, network_property,
900 network_property(NULL, NULL, network);
904 supplicant_dbus_property_get_all(path,
905 SUPPLICANT_INTERFACE ".Network",
906 network_property, network);
909 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
911 GSupplicantInterface *interface = user_data;
912 GSupplicantNetwork *network;
913 const char *path = NULL;
915 dbus_message_iter_get_basic(iter, &path);
919 network = g_hash_table_lookup(interface->net_mapping, path);
923 g_hash_table_remove(interface->net_mapping, path);
926 static char *create_name(unsigned char *ssid, int ssid_len)
931 if (ssid_len < 1 || ssid[0] == '\0')
934 name = g_try_malloc0(ssid_len + 1);
939 for (i = 0; i < ssid_len; i++) {
940 if (g_ascii_isprint(ssid[i]))
949 static char *create_group(struct g_supplicant_bss *bss)
953 const char *mode, *security;
955 str = g_string_sized_new((bss->ssid_len * 2) + 24);
959 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
960 for (i = 0; i < bss->ssid_len; i++)
961 g_string_append_printf(str, "%02x", bss->ssid[i]);
963 g_string_append_printf(str, "hidden");
965 mode = mode2string(bss->mode);
967 g_string_append_printf(str, "_%s", mode);
969 security = security2string(bss->security);
970 if (security != NULL)
971 g_string_append_printf(str, "_%s", security);
973 return g_string_free(str, FALSE);
976 static void add_bss_to_network(struct g_supplicant_bss *bss)
978 GSupplicantInterface *interface = bss->interface;
979 GSupplicantNetwork *network;
982 group = create_group(bss);
986 network = g_hash_table_lookup(interface->network_table, group);
987 if (network != NULL) {
992 network = g_try_new0(GSupplicantNetwork, 1);
993 if (network == NULL) {
998 network->interface = interface;
999 if (network->path == NULL)
1000 network->path = g_strdup(bss->path);
1001 network->group = group;
1002 network->name = create_name(bss->ssid, bss->ssid_len);
1003 network->mode = bss->mode;
1004 network->security = bss->security;
1005 network->ssid_len = bss->ssid_len;
1006 memcpy(network->ssid, bss->ssid, bss->ssid_len);
1007 network->signal = bss->signal;
1009 network->wps = FALSE;
1010 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0)
1011 network->wps = TRUE;
1013 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1016 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1019 g_hash_table_replace(interface->network_table,
1020 network->group, network);
1022 callback_network_added(network);
1025 g_hash_table_replace(interface->bss_mapping, bss->path, network);
1026 g_hash_table_replace(network->bss_table, bss->path, bss);
1028 g_hash_table_replace(bss_mapping, bss->path, interface);
1031 static void bss_rates(DBusMessageIter *iter, void *user_data)
1033 struct g_supplicant_bss *bss = user_data;
1034 dbus_uint32_t rate = 0;
1036 dbus_message_iter_get_basic(iter, &rate);
1040 if (rate > bss->maxrate)
1041 bss->maxrate = rate;
1044 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1046 struct g_supplicant_bss *bss = user_data;
1047 const char *str = NULL;
1050 dbus_message_iter_get_basic(iter, &str);
1054 for (i = 0; keymgmt_map[i].str != NULL; i++)
1055 if (strcmp(str, keymgmt_map[i].str) == 0) {
1056 bss->keymgmt |= keymgmt_map[i].val;
1061 static void bss_group(DBusMessageIter *iter, void *user_data)
1063 struct g_supplicant_bss *bss = user_data;
1064 const char *str = NULL;
1067 dbus_message_iter_get_basic(iter, &str);
1071 for (i = 0; group_map[i].str != NULL; i++)
1072 if (strcmp(str, group_map[i].str) == 0) {
1073 bss->group |= group_map[i].val;
1078 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1080 struct g_supplicant_bss *bss = user_data;
1081 const char *str = NULL;
1084 dbus_message_iter_get_basic(iter, &str);
1088 for (i = 0; pairwise_map[i].str != NULL; i++)
1089 if (strcmp(str, pairwise_map[i].str) == 0) {
1090 bss->pairwise |= pairwise_map[i].val;
1095 static void bss_wpa(const char *key, DBusMessageIter *iter,
1098 if (g_strcmp0(key, "KeyMgmt") == 0)
1099 supplicant_dbus_array_foreach(iter, bss_keymgmt, user_data);
1100 else if (g_strcmp0(key, "Group") == 0)
1101 supplicant_dbus_array_foreach(iter, bss_group, user_data);
1102 else if (g_strcmp0(key, "Pairwise") == 0)
1103 supplicant_dbus_array_foreach(iter, bss_pairwise, user_data);
1107 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1110 unsigned int len = 0;
1112 while (len + 4 < ie_size) {
1113 unsigned int hi = ie[len];
1114 unsigned int lo = ie[len + 1];
1115 unsigned int tmp_type = (hi << 8) + lo;
1116 unsigned int v_len = 0;
1118 /* hi and lo are used to recreate an unsigned int
1119 * based on 2 8bits length unsigned int. */
1123 v_len = (hi << 8) + lo;
1125 if (tmp_type == type) {
1126 unsigned int ret_value = 0;
1127 unsigned char *value = (unsigned char *)&ret_value;
1129 SUPPLICANT_DBG("IE: match type 0x%x", type);
1131 /* Verifying length relevance */
1132 if (v_len > sizeof(unsigned int) ||
1133 len + 4 + v_len > ie_size)
1136 memcpy(value, ie + len + 4, v_len);
1138 SUPPLICANT_DBG("returning 0x%x", ret_value);
1145 SUPPLICANT_DBG("returning 0");
1149 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1151 struct g_supplicant_bss *bss = user_data;
1152 const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1153 unsigned char *ie, *ie_end;
1154 DBusMessageIter array;
1157 #define WMM_WPA1_WPS_INFO 221
1158 #define WPS_INFO_MIN_LEN 6
1159 #define WPS_VERSION_TLV 0x104A
1160 #define WPS_STATE_TLV 0x1044
1161 #define WPS_VERSION 0x10
1163 dbus_message_iter_recurse(iter, &array);
1164 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1166 if (ie == NULL || ie_len < 2)
1169 for (ie_end = ie+ie_len; ie+ie[1]+1 <= ie_end; ie += ie[1]+2) {
1170 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1171 memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1174 SUPPLICANT_DBG("IE: match WPS_OUI");
1176 if (get_tlv(&ie[6], ie[1],
1177 WPS_VERSION_TLV) == WPS_VERSION &&
1178 get_tlv(&ie[6], ie[1],
1179 WPS_STATE_TLV) != 0)
1180 bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1184 static void bss_property(const char *key, DBusMessageIter *iter,
1187 struct g_supplicant_bss *bss = user_data;
1189 if (bss->interface == NULL)
1192 SUPPLICANT_DBG("key %s", key);
1195 if (bss->ieee8021x == TRUE)
1196 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1197 else if (bss->psk == TRUE)
1198 bss->security = G_SUPPLICANT_SECURITY_PSK;
1199 else if (bss->privacy == TRUE)
1200 bss->security = G_SUPPLICANT_SECURITY_WEP;
1202 bss->security = G_SUPPLICANT_SECURITY_NONE;
1204 add_bss_to_network(bss);
1208 if (g_strcmp0(key, "BSSID") == 0) {
1209 DBusMessageIter array;
1210 unsigned char *addr;
1213 dbus_message_iter_recurse(iter, &array);
1214 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1217 memcpy(bss->bssid, addr, addr_len);
1218 } else if (g_strcmp0(key, "SSID") == 0) {
1219 DBusMessageIter array;
1220 unsigned char *ssid;
1223 dbus_message_iter_recurse(iter, &array);
1224 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1226 if (ssid_len > 0 && ssid_len < 33) {
1227 memcpy(bss->ssid, ssid, ssid_len);
1228 bss->ssid_len = ssid_len;
1230 memset(bss->ssid, 0, sizeof(bss->ssid));
1233 } else if (g_strcmp0(key, "Capabilities") == 0) {
1234 dbus_uint16_t capabilities = 0x0000;
1236 dbus_message_iter_get_basic(iter, &capabilities);
1238 if (capabilities & IEEE80211_CAP_ESS)
1239 bss->mode = G_SUPPLICANT_MODE_INFRA;
1240 else if (capabilities & IEEE80211_CAP_IBSS)
1241 bss->mode = G_SUPPLICANT_MODE_IBSS;
1243 if (capabilities & IEEE80211_CAP_PRIVACY)
1244 bss->privacy = TRUE;
1245 } else if (g_strcmp0(key, "Mode") == 0) {
1246 const char *mode = NULL;
1248 dbus_message_iter_get_basic(iter, &mode);
1249 bss->mode = string2mode(mode);
1250 } else if (g_strcmp0(key, "Frequency") == 0) {
1251 dbus_uint16_t frequency = 0;
1253 dbus_message_iter_get_basic(iter, &frequency);
1254 bss->frequency = frequency;
1255 } else if (g_strcmp0(key, "Signal") == 0) {
1256 dbus_int16_t signal = 0;
1258 dbus_message_iter_get_basic(iter, &signal);
1260 bss->signal = signal;
1261 } else if (g_strcmp0(key, "Level") == 0) {
1262 dbus_int32_t level = 0;
1264 dbus_message_iter_get_basic(iter, &level);
1265 } else if (g_strcmp0(key, "Rates") == 0) {
1266 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1267 } else if (g_strcmp0(key, "MaxRate") == 0) {
1268 dbus_uint32_t maxrate = 0;
1270 dbus_message_iter_get_basic(iter, &maxrate);
1272 bss->maxrate = maxrate;
1273 } else if (g_strcmp0(key, "Privacy") == 0) {
1274 dbus_bool_t privacy = FALSE;
1276 dbus_message_iter_get_basic(iter, &privacy);
1277 bss->privacy = privacy;
1278 } else if ((g_strcmp0(key, "RSN") == 0) ||
1279 (g_strcmp0(key, "WPA") == 0)) {
1280 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1283 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1284 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1285 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1286 bss->ieee8021x = TRUE;
1289 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1290 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1291 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1293 } else if (g_strcmp0(key, "IEs") == 0)
1294 bss_process_ies(iter, bss);
1296 SUPPLICANT_DBG("key %s type %c",
1297 key, dbus_message_iter_get_arg_type(iter));
1300 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1303 GSupplicantInterface *interface = user_data;
1304 GSupplicantNetwork *network;
1305 struct g_supplicant_bss *bss;
1306 const char *path = NULL;
1310 dbus_message_iter_get_basic(iter, &path);
1314 if (g_strcmp0(path, "/") == 0)
1317 SUPPLICANT_DBG("%s", path);
1319 network = g_hash_table_lookup(interface->bss_mapping, path);
1320 if (network != NULL) {
1321 bss = g_hash_table_lookup(network->bss_table, path);
1326 bss = g_try_new0(struct g_supplicant_bss, 1);
1330 bss->interface = interface;
1331 bss->path = g_strdup(path);
1336 static void interface_bss_added_with_keys(DBusMessageIter *iter,
1339 struct g_supplicant_bss *bss;
1343 bss = interface_bss_added(iter, user_data);
1347 dbus_message_iter_next(iter);
1349 if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
1352 supplicant_dbus_property_foreach(iter, bss_property, bss);
1353 bss_property(NULL, NULL, bss);
1356 static void interface_bss_added_without_keys(DBusMessageIter *iter,
1359 struct g_supplicant_bss *bss;
1363 bss = interface_bss_added(iter, user_data);
1367 supplicant_dbus_property_get_all(bss->path,
1368 SUPPLICANT_INTERFACE ".BSS",
1372 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1374 GSupplicantInterface *interface = user_data;
1375 GSupplicantNetwork *network;
1376 const char *path = NULL;
1378 dbus_message_iter_get_basic(iter, &path);
1382 network = g_hash_table_lookup(interface->bss_mapping, path);
1383 if (network == NULL)
1386 g_hash_table_remove(bss_mapping, path);
1388 g_hash_table_remove(interface->bss_mapping, path);
1389 g_hash_table_remove(network->bss_table, path);
1391 if (g_hash_table_size(network->bss_table) == 0)
1392 g_hash_table_remove(interface->network_table, network->group);
1395 static void interface_property(const char *key, DBusMessageIter *iter,
1398 GSupplicantInterface *interface = user_data;
1400 if (interface == NULL)
1403 SUPPLICANT_DBG("%s", key);
1406 debug_strvalmap("KeyMgmt capability", keymgmt_map,
1407 interface->keymgmt_capa);
1408 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1409 interface->authalg_capa);
1410 debug_strvalmap("Protocol capability", proto_capa_map,
1411 interface->proto_capa);
1412 debug_strvalmap("Pairwise capability", pairwise_map,
1413 interface->pairwise_capa);
1414 debug_strvalmap("Group capability", group_map,
1415 interface->group_capa);
1416 debug_strvalmap("Scan capability", scan_capa_map,
1417 interface->scan_capa);
1418 debug_strvalmap("Mode capability", mode_capa_map,
1419 interface->mode_capa);
1421 interface->ready = TRUE;
1422 callback_interface_added(interface);
1426 if (g_strcmp0(key, "Capabilities") == 0) {
1427 supplicant_dbus_property_foreach(iter, interface_capability,
1429 } else if (g_strcmp0(key, "State") == 0) {
1430 const char *str = NULL;
1432 dbus_message_iter_get_basic(iter, &str);
1434 if (string2state(str) != interface->state) {
1435 interface->state = string2state(str);
1436 callback_interface_state(interface);
1439 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
1440 } else if (g_strcmp0(key, "Scanning") == 0) {
1441 dbus_bool_t scanning = FALSE;
1443 dbus_message_iter_get_basic(iter, &scanning);
1444 interface->scanning = scanning;
1446 if (interface->ready == TRUE) {
1447 if (interface->scanning == TRUE)
1448 callback_scan_started(interface);
1450 callback_scan_finished(interface);
1452 } else if (g_strcmp0(key, "ApScan") == 0) {
1455 dbus_message_iter_get_basic(iter, &apscan);
1456 interface->apscan = apscan;
1457 } else if (g_strcmp0(key, "Ifname") == 0) {
1458 const char *str = NULL;
1460 dbus_message_iter_get_basic(iter, &str);
1462 g_free(interface->ifname);
1463 interface->ifname = g_strdup(str);
1465 } else if (g_strcmp0(key, "Driver") == 0) {
1466 const char *str = NULL;
1468 dbus_message_iter_get_basic(iter, &str);
1470 g_free(interface->driver);
1471 interface->driver = g_strdup(str);
1473 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1474 const char *str = NULL;
1476 dbus_message_iter_get_basic(iter, &str);
1478 g_free(interface->bridge);
1479 interface->bridge = g_strdup(str);
1481 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1482 interface_bss_added_without_keys(iter, interface);
1483 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1484 interface_network_added(iter, interface);
1485 } else if (g_strcmp0(key, "BSSs") == 0) {
1486 supplicant_dbus_array_foreach(iter, interface_bss_added_without_keys,
1488 } else if (g_strcmp0(key, "Blobs") == 0) {
1490 } else if (g_strcmp0(key, "Networks") == 0) {
1491 supplicant_dbus_array_foreach(iter, interface_network_added,
1494 SUPPLICANT_DBG("key %s type %c",
1495 key, dbus_message_iter_get_arg_type(iter));
1498 static void scan_network_update(DBusMessageIter *iter, void *user_data)
1500 GSupplicantInterface *interface = user_data;
1501 GSupplicantNetwork *network;
1507 dbus_message_iter_get_basic(iter, &path);
1512 if (g_strcmp0(path, "/") == 0)
1515 /* Update the network details based on scan BSS data */
1516 network = g_hash_table_lookup(interface->bss_mapping, path);
1517 if (network != NULL)
1518 callback_network_added(network);
1521 static void scan_bss_data(const char *key, DBusMessageIter *iter,
1524 GSupplicantInterface *interface = user_data;
1527 supplicant_dbus_array_foreach(iter, scan_network_update,
1530 if (interface->scan_callback != NULL)
1531 interface->scan_callback(0, interface, interface->scan_data);
1533 interface->scan_callback = NULL;
1534 interface->scan_data = NULL;
1537 static GSupplicantInterface *interface_alloc(const char *path)
1539 GSupplicantInterface *interface;
1541 interface = g_try_new0(GSupplicantInterface, 1);
1542 if (interface == NULL)
1545 interface->path = g_strdup(path);
1547 interface->network_table = g_hash_table_new_full(g_str_hash,
1548 g_str_equal, NULL, remove_network);
1550 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1552 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1555 g_hash_table_replace(interface_table, interface->path, interface);
1560 static void interface_added(DBusMessageIter *iter, void *user_data)
1562 GSupplicantInterface *interface;
1563 const char *path = NULL;
1567 dbus_message_iter_get_basic(iter, &path);
1571 if (g_strcmp0(path, "/") == 0)
1574 interface = g_hash_table_lookup(interface_table, path);
1575 if (interface != NULL)
1578 interface = interface_alloc(path);
1579 if (interface == NULL)
1582 dbus_message_iter_next(iter);
1583 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1584 supplicant_dbus_property_foreach(iter, interface_property,
1586 interface_property(NULL, NULL, interface);
1590 supplicant_dbus_property_get_all(path,
1591 SUPPLICANT_INTERFACE ".Interface",
1592 interface_property, interface);
1595 static void interface_removed(DBusMessageIter *iter, void *user_data)
1597 const char *path = NULL;
1599 dbus_message_iter_get_basic(iter, &path);
1603 g_hash_table_remove(interface_table, path);
1606 static void eap_method(DBusMessageIter *iter, void *user_data)
1608 const char *str = NULL;
1611 dbus_message_iter_get_basic(iter, &str);
1615 for (i = 0; eap_method_map[i].str != NULL; i++)
1616 if (strcmp(str, eap_method_map[i].str) == 0) {
1617 eap_methods |= eap_method_map[i].val;
1622 static void service_property(const char *key, DBusMessageIter *iter,
1626 callback_system_ready();
1630 if (g_strcmp0(key, "DebugLevel") == 0) {
1631 const char *str = NULL;
1634 dbus_message_iter_get_basic(iter, &str);
1635 for (i = 0; debug_strings[i] != NULL; i++)
1636 if (g_strcmp0(debug_strings[i], str) == 0) {
1640 SUPPLICANT_DBG("Debug level %d", debug_level);
1641 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1642 dbus_message_iter_get_basic(iter, &debug_timestamp);
1643 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
1644 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1645 dbus_message_iter_get_basic(iter, &debug_showkeys);
1646 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
1647 } else if (g_strcmp0(key, "Interfaces") == 0) {
1648 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1649 } else if (g_strcmp0(key, "EapMethods") == 0) {
1650 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1651 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1652 } else if (g_strcmp0(key, "Country") == 0) {
1653 const char *country = NULL;
1655 dbus_message_iter_get_basic(iter, &country);
1656 SUPPLICANT_DBG("Country %s", country);
1658 SUPPLICANT_DBG("key %s type %c",
1659 key, dbus_message_iter_get_arg_type(iter));
1662 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1664 const char *name = NULL, *old = NULL, *new = NULL;
1668 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1671 dbus_message_iter_get_basic(iter, &name);
1675 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1678 dbus_message_iter_next(iter);
1679 dbus_message_iter_get_basic(iter, &old);
1680 dbus_message_iter_next(iter);
1681 dbus_message_iter_get_basic(iter, &new);
1683 if (old == NULL || new == NULL)
1686 if (strlen(old) > 0 && strlen(new) == 0) {
1687 system_available = FALSE;
1688 g_hash_table_remove_all(bss_mapping);
1689 g_hash_table_remove_all(interface_table);
1690 callback_system_killed();
1693 if (strlen(new) > 0 && strlen(old) == 0) {
1694 system_available = TRUE;
1695 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1696 SUPPLICANT_INTERFACE,
1697 service_property, NULL);
1701 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1705 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1708 supplicant_dbus_property_foreach(iter, service_property, NULL);
1711 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1713 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
1715 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1716 interface_added(iter, NULL);
1719 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1723 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1724 interface_removed(iter, NULL);
1727 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1729 GSupplicantInterface *interface;
1733 interface = g_hash_table_lookup(interface_table, path);
1734 if (interface == NULL)
1737 supplicant_dbus_property_foreach(iter, interface_property, interface);
1740 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1742 GSupplicantInterface *interface;
1743 dbus_bool_t success = FALSE;
1747 interface = g_hash_table_lookup(interface_table, path);
1748 if (interface == NULL)
1751 dbus_message_iter_get_basic(iter, &success);
1754 * If scan is unsuccessful return -EIO else get the scanned BSSs
1755 * and update the network details accordingly
1757 if (success == FALSE) {
1758 if (interface->scan_callback != NULL)
1759 interface->scan_callback(-EIO, interface,
1760 interface->scan_data);
1762 interface->scan_callback = NULL;
1763 interface->scan_data = NULL;
1768 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
1769 "BSSs", scan_bss_data, interface);
1772 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1774 GSupplicantInterface *interface;
1778 interface = g_hash_table_lookup(interface_table, path);
1779 if (interface == NULL)
1782 interface_bss_added_with_keys(iter, interface);
1785 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1787 GSupplicantInterface *interface;
1791 interface = g_hash_table_lookup(interface_table, path);
1792 if (interface == NULL)
1795 interface_bss_removed(iter, interface);
1798 static void signal_network_added(const char *path, DBusMessageIter *iter)
1800 GSupplicantInterface *interface;
1804 interface = g_hash_table_lookup(interface_table, path);
1805 if (interface == NULL)
1808 interface_network_added(iter, interface);
1811 static void signal_network_removed(const char *path, DBusMessageIter *iter)
1813 GSupplicantInterface *interface;
1817 interface = g_hash_table_lookup(interface_table, path);
1818 if (interface == NULL)
1821 interface_network_removed(iter, interface);
1824 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
1826 GSupplicantInterface *interface;
1827 GSupplicantNetwork *network;
1828 struct g_supplicant_bss *bss;
1832 interface = g_hash_table_lookup(bss_mapping, path);
1833 if (interface == NULL)
1836 network = g_hash_table_lookup(interface->bss_mapping, path);
1837 if (network == NULL)
1840 bss = g_hash_table_lookup(network->bss_table, path);
1844 supplicant_dbus_property_foreach(iter, bss_property, bss);
1847 static void wps_credentials(const char *key, DBusMessageIter *iter,
1850 GSupplicantInterface *interface = user_data;
1855 SUPPLICANT_DBG("key %s", key);
1857 if (g_strcmp0(key, "Key") == 0) {
1858 DBusMessageIter array;
1862 dbus_message_iter_recurse(iter, &array);
1863 dbus_message_iter_get_fixed_array(&array, &key, &key_len);
1865 g_free(interface->wps_cred.key);
1866 interface->wps_cred.key = g_try_malloc0(
1867 sizeof(char) * key_len+1);
1869 if (interface->wps_cred.key == NULL)
1872 memcpy(interface->wps_cred.key, key, sizeof(char) * key_len);
1874 SUPPLICANT_DBG("WPS key present");
1875 } else if (g_strcmp0(key, "SSID") == 0) {
1876 DBusMessageIter array;
1877 unsigned char *ssid;
1880 dbus_message_iter_recurse(iter, &array);
1881 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1883 if (ssid_len > 0 && ssid_len < 33) {
1884 memcpy(interface->wps_cred.ssid, ssid, ssid_len);
1885 interface->wps_cred.ssid_len = ssid_len;
1887 memset(interface->wps_cred.ssid, 0, 32);
1888 interface->wps_cred.ssid_len = 0;
1893 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
1895 GSupplicantInterface *interface;
1899 interface = g_hash_table_lookup(interface_table, path);
1900 if (interface == NULL)
1903 supplicant_dbus_property_foreach(iter, wps_credentials, interface);
1906 static void wps_event_args(const char *key, DBusMessageIter *iter,
1909 GSupplicantInterface *interface = user_data;
1911 if (key == NULL || interface == NULL)
1914 SUPPLICANT_DBG("Arg Key %s", key);
1917 static void signal_wps_event(const char *path, DBusMessageIter *iter)
1919 GSupplicantInterface *interface;
1920 const char *name = NULL;
1924 interface = g_hash_table_lookup(interface_table, path);
1925 if (interface == NULL)
1928 dbus_message_iter_get_basic(iter, &name);
1930 SUPPLICANT_DBG("Name: %s", name);
1932 if (g_strcmp0(name, "success") == 0)
1933 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
1934 else if (g_strcmp0(name, "failed") == 0)
1935 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
1937 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
1939 if (!dbus_message_iter_has_next(iter))
1942 dbus_message_iter_next(iter);
1944 supplicant_dbus_property_foreach(iter, wps_event_args, interface);
1948 const char *interface;
1950 void (*function) (const char *path, DBusMessageIter *iter);
1952 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
1954 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
1955 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
1956 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
1957 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
1959 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
1960 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
1961 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
1962 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
1963 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
1964 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
1966 { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed },
1968 { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
1969 { SUPPLICANT_INTERFACE ".Interface.WPS", "Event", signal_wps_event },
1974 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
1975 DBusMessage *message, void *data)
1977 DBusMessageIter iter;
1981 path = dbus_message_get_path(message);
1983 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1985 if (dbus_message_iter_init(message, &iter) == FALSE)
1986 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1988 for (i = 0; signal_map[i].interface != NULL; i++) {
1989 if (dbus_message_has_interface(message,
1990 signal_map[i].interface) == FALSE)
1993 if (dbus_message_has_member(message,
1994 signal_map[i].member) == FALSE)
1997 signal_map[i].function(path, &iter);
2001 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2004 struct supplicant_regdom {
2005 GSupplicantCountryCallback callback;
2006 const void *user_data;
2009 static void country_result(const char *error,
2010 DBusMessageIter *iter, void *user_data)
2012 struct supplicant_regdom *regdom = user_data;
2015 SUPPLICANT_DBG("Country setting result");
2017 if (user_data == NULL)
2020 if (error == NULL) {
2021 alpha2 = (char *)regdom->user_data;
2023 SUPPLICANT_DBG("Country setting failure %s", error);
2027 if (regdom->callback)
2028 regdom->callback(alpha2);
2033 static void country_params(DBusMessageIter *iter, void *user_data)
2035 struct supplicant_regdom *regdom = user_data;
2036 const char *country;
2038 country = regdom->user_data;
2040 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &country);
2043 int g_supplicant_set_country(const char *alpha2,
2044 GSupplicantCountryCallback callback,
2045 const void *user_data)
2047 struct supplicant_regdom *regdom;
2049 SUPPLICANT_DBG("Country setting %s", alpha2);
2051 if (system_available == FALSE)
2054 regdom = dbus_malloc0(sizeof(*regdom));
2058 regdom->callback = callback;
2059 regdom->user_data = user_data;
2061 return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
2062 "Country", DBUS_TYPE_STRING_AS_STRING,
2063 country_params, country_result,
2067 struct interface_data {
2068 GSupplicantInterface *interface;
2069 GSupplicantInterfaceCallback callback;
2073 struct interface_create_data {
2077 GSupplicantInterface *interface;
2078 GSupplicantInterfaceCallback callback;
2082 struct interface_connect_data {
2083 GSupplicantInterface *interface;
2084 GSupplicantInterfaceCallback callback;
2085 GSupplicantSSID *ssid;
2089 static void interface_create_property(const char *key, DBusMessageIter *iter,
2092 struct interface_create_data *data = user_data;
2093 GSupplicantInterface *interface = data->interface;
2096 if (data->callback != NULL)
2097 data->callback(0, data->interface, data->user_data);
2102 interface_property(key, iter, interface);
2105 static void interface_create_result(const char *error,
2106 DBusMessageIter *iter, void *user_data)
2108 struct interface_create_data *data = user_data;
2109 const char *path = NULL;
2114 if (error != NULL) {
2115 g_critical("error %s", error);
2120 dbus_message_iter_get_basic(iter, &path);
2126 if (system_available == FALSE) {
2131 data->interface = g_hash_table_lookup(interface_table, path);
2132 if (data->interface == NULL) {
2133 data->interface = interface_alloc(path);
2134 if (data->interface == NULL) {
2140 err = supplicant_dbus_property_get_all(path,
2141 SUPPLICANT_INTERFACE ".Interface",
2142 interface_create_property, data);
2147 if (data->callback != NULL)
2148 data->callback(err, NULL, data->user_data);
2153 static void interface_create_params(DBusMessageIter *iter, void *user_data)
2155 struct interface_create_data *data = user_data;
2156 DBusMessageIter dict;
2160 supplicant_dbus_dict_open(iter, &dict);
2162 supplicant_dbus_dict_append_basic(&dict, "Ifname",
2163 DBUS_TYPE_STRING, &data->ifname);
2165 if (data->driver != NULL)
2166 supplicant_dbus_dict_append_basic(&dict, "Driver",
2167 DBUS_TYPE_STRING, &data->driver);
2169 if (data->bridge != NULL)
2170 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
2171 DBUS_TYPE_STRING, &data->bridge);
2173 supplicant_dbus_dict_close(iter, &dict);
2176 static void interface_get_result(const char *error,
2177 DBusMessageIter *iter, void *user_data)
2179 struct interface_create_data *data = user_data;
2180 GSupplicantInterface *interface;
2181 const char *path = NULL;
2186 if (error != NULL) {
2187 SUPPLICANT_DBG("Interface not created yet");
2192 dbus_message_iter_get_basic(iter, &path);
2198 interface = g_hash_table_lookup(interface_table, path);
2199 if (interface == NULL) {
2204 if (data->callback != NULL)
2205 data->callback(0, interface, data->user_data);
2212 if (system_available == FALSE) {
2217 SUPPLICANT_DBG("Creating interface");
2219 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
2220 SUPPLICANT_INTERFACE,
2222 interface_create_params,
2223 interface_create_result, data);
2228 if (data->callback != NULL)
2229 data->callback(err, NULL, data->user_data);
2234 static void interface_get_params(DBusMessageIter *iter, void *user_data)
2236 struct interface_create_data *data = user_data;
2240 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
2243 int g_supplicant_interface_create(const char *ifname, const char *driver,
2245 GSupplicantInterfaceCallback callback,
2248 struct interface_create_data *data;
2250 SUPPLICANT_DBG("ifname %s", ifname);
2255 if (system_available == FALSE)
2258 data = dbus_malloc0(sizeof(*data));
2262 data->ifname = ifname;
2263 data->driver = driver;
2264 data->bridge = bridge;
2265 data->callback = callback;
2266 data->user_data = user_data;
2268 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2269 SUPPLICANT_INTERFACE,
2271 interface_get_params,
2272 interface_get_result, data);
2275 static void interface_remove_result(const char *error,
2276 DBusMessageIter *iter, void *user_data)
2278 struct interface_data *data = user_data;
2281 if (error != NULL) {
2286 if (system_available == FALSE) {
2292 * The gsupplicant interface is already freed by the InterfaceRemoved
2293 * signal callback. Simply invoke the interface_data callback.
2298 if (data->callback != NULL)
2299 data->callback(err, NULL, data->user_data);
2305 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
2307 struct interface_data *data = user_data;
2309 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2310 &data->interface->path);
2314 int g_supplicant_interface_remove(GSupplicantInterface *interface,
2315 GSupplicantInterfaceCallback callback,
2318 struct interface_data *data;
2320 if (interface == NULL)
2323 if (system_available == FALSE)
2326 data = dbus_malloc0(sizeof(*data));
2330 data->interface = interface;
2331 data->callback = callback;
2332 data->user_data = user_data;
2334 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2335 SUPPLICANT_INTERFACE,
2337 interface_remove_params,
2338 interface_remove_result, data);
2341 static void interface_scan_result(const char *error,
2342 DBusMessageIter *iter, void *user_data)
2344 struct interface_data *data = user_data;
2346 if (error != NULL) {
2347 if (data->callback != NULL)
2348 data->callback(-EIO, data->interface, data->user_data);
2350 data->interface->scan_callback = data->callback;
2351 data->interface->scan_data = data->user_data;
2357 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
2359 DBusMessageIter dict;
2360 const char *type = "passive";
2362 supplicant_dbus_dict_open(iter, &dict);
2364 supplicant_dbus_dict_append_basic(&dict, "Type",
2365 DBUS_TYPE_STRING, &type);
2367 supplicant_dbus_dict_close(iter, &dict);
2370 int g_supplicant_interface_scan(GSupplicantInterface *interface,
2371 GSupplicantInterfaceCallback callback,
2374 struct interface_data *data;
2376 if (interface == NULL)
2379 if (system_available == FALSE)
2382 if (interface->scanning == TRUE)
2385 switch (interface->state) {
2386 case G_SUPPLICANT_STATE_AUTHENTICATING:
2387 case G_SUPPLICANT_STATE_ASSOCIATING:
2388 case G_SUPPLICANT_STATE_ASSOCIATED:
2389 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2390 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2392 case G_SUPPLICANT_STATE_UNKNOWN:
2393 case G_SUPPLICANT_STATE_DISCONNECTED:
2394 case G_SUPPLICANT_STATE_INACTIVE:
2395 case G_SUPPLICANT_STATE_SCANNING:
2396 case G_SUPPLICANT_STATE_COMPLETED:
2400 data = dbus_malloc0(sizeof(*data));
2404 data->interface = interface;
2405 data->callback = callback;
2406 data->user_data = user_data;
2408 return supplicant_dbus_method_call(interface->path,
2409 SUPPLICANT_INTERFACE ".Interface", "Scan",
2410 interface_scan_params, interface_scan_result, data);
2413 static void interface_select_network_result(const char *error,
2414 DBusMessageIter *iter, void *user_data)
2416 struct interface_connect_data *data = user_data;
2425 if (data->callback != NULL)
2426 data->callback(err, data->interface, data->user_data);
2432 static void interface_select_network_params(DBusMessageIter *iter,
2435 struct interface_connect_data *data = user_data;
2436 GSupplicantInterface *interface = data->interface;
2438 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2439 &interface->network_path);
2442 static void interface_add_network_result(const char *error,
2443 DBusMessageIter *iter, void *user_data)
2445 struct interface_connect_data *data = user_data;
2446 GSupplicantInterface *interface = data->interface;
2452 dbus_message_iter_get_basic(iter, &path);
2456 SUPPLICANT_DBG("PATH: %s", path);
2458 g_free(interface->network_path);
2459 interface->network_path = g_strdup(path);
2461 supplicant_dbus_method_call(data->interface->path,
2462 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
2463 interface_select_network_params,
2464 interface_select_network_result, data);
2469 g_free(interface->network_path);
2470 interface->network_path = NULL;
2475 static void add_network_security_wep(DBusMessageIter *dict,
2476 GSupplicantSSID *ssid)
2478 const char *auth_alg = "OPEN SHARED";
2479 const char *key_index = "0";
2481 supplicant_dbus_dict_append_basic(dict, "auth_alg",
2482 DBUS_TYPE_STRING, &auth_alg);
2484 if (ssid->passphrase) {
2485 int size = strlen(ssid->passphrase);
2486 if (size == 10 || size == 26) {
2487 unsigned char *key = g_try_malloc(13);
2491 memset(tmp, 0, sizeof(tmp));
2495 for (i = 0; i < size / 2; i++) {
2496 memcpy(tmp, ssid->passphrase + (i * 2), 2);
2497 key[i] = (unsigned char) strtol(tmp, NULL, 16);
2500 supplicant_dbus_dict_append_fixed_array(dict,
2505 } else if (size == 5 || size == 13) {
2506 unsigned char *key = g_try_malloc(13);
2512 for (i = 0; i < size; i++)
2513 key[i] = (unsigned char) ssid->passphrase[i];
2515 supplicant_dbus_dict_append_fixed_array(dict,
2521 supplicant_dbus_dict_append_basic(dict,
2526 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
2527 DBUS_TYPE_STRING, &key_index);
2531 static void add_network_security_psk(DBusMessageIter *dict,
2532 GSupplicantSSID *ssid)
2534 if (ssid->passphrase && strlen(ssid->passphrase) > 0)
2535 supplicant_dbus_dict_append_basic(dict, "psk",
2540 static void add_network_security_tls(DBusMessageIter *dict,
2541 GSupplicantSSID *ssid)
2544 * For TLS, we at least need:
2545 * The client certificate
2546 * The client private key file
2547 * The client private key file password
2549 * The Authority certificate is optional.
2551 if (ssid->client_cert_path == NULL)
2554 if (ssid->private_key_path == NULL)
2557 if (ssid->private_key_passphrase == NULL)
2560 if (ssid->ca_cert_path)
2561 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2562 DBUS_TYPE_STRING, &ssid->ca_cert_path);
2564 supplicant_dbus_dict_append_basic(dict, "private_key",
2566 &ssid->private_key_path);
2567 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2569 &ssid->private_key_passphrase);
2570 supplicant_dbus_dict_append_basic(dict, "client_cert",
2572 &ssid->client_cert_path);
2575 static void add_network_security_peap(DBusMessageIter *dict,
2576 GSupplicantSSID *ssid)
2581 * For PEAP/TTLS, we at least need
2582 * The authority certificate
2583 * The 2nd phase authentication method
2584 * The 2nd phase passphrase
2586 * The Client certificate is optional although strongly required
2587 * When setting it, we need in addition
2588 * The Client private key file
2589 * The Client private key file password
2591 if (ssid->passphrase == NULL)
2594 if (ssid->ca_cert_path == NULL)
2597 if (ssid->phase2_auth == NULL)
2600 if (ssid->client_cert_path) {
2601 if (ssid->private_key_path == NULL)
2604 if (ssid->private_key_passphrase == NULL)
2607 supplicant_dbus_dict_append_basic(dict, "client_cert",
2609 &ssid->client_cert_path);
2611 supplicant_dbus_dict_append_basic(dict, "private_key",
2613 &ssid->private_key_path);
2615 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2617 &ssid->private_key_passphrase);
2621 if (g_str_has_prefix(ssid->phase2_auth, "EAP-") == TRUE) {
2622 phase2_auth = g_strdup_printf("autheap=%s",
2623 ssid->phase2_auth + strlen("EAP-"));
2625 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
2627 supplicant_dbus_dict_append_basic(dict, "password",
2631 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2633 &ssid->ca_cert_path);
2635 supplicant_dbus_dict_append_basic(dict, "phase2",
2639 g_free(phase2_auth);
2642 static void add_network_security_eap(DBusMessageIter *dict,
2643 GSupplicantSSID *ssid)
2647 if (ssid->eap == NULL || ssid->identity == NULL)
2650 if (g_strcmp0(ssid->eap, "tls") == 0) {
2651 add_network_security_tls(dict, ssid);
2652 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
2653 g_strcmp0(ssid->eap, "ttls") == 0) {
2654 add_network_security_peap(dict, ssid);
2658 eap_value = g_ascii_strup(ssid->eap, -1);
2660 supplicant_dbus_dict_append_basic(dict, "eap",
2663 supplicant_dbus_dict_append_basic(dict, "identity",
2670 static void add_network_security_ciphers(DBusMessageIter *dict,
2671 GSupplicantSSID *ssid)
2673 unsigned int p_cipher, g_cipher, i;
2674 char *pairwise, *group;
2675 char *pair_ciphers[4];
2676 char *group_ciphers[5];
2678 p_cipher = ssid->pairwise_cipher;
2679 g_cipher = ssid->group_cipher;
2681 if (p_cipher == 0 && g_cipher == 0)
2686 if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
2687 pair_ciphers[i++] = "CCMP";
2689 if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
2690 pair_ciphers[i++] = "TKIP";
2692 if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
2693 pair_ciphers[i++] = "NONE";
2695 pair_ciphers[i] = NULL;
2699 if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
2700 group_ciphers[i++] = "CCMP";
2702 if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
2703 group_ciphers[i++] = "TKIP";
2705 if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
2706 group_ciphers[i++] = "WEP104";
2708 if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
2709 group_ciphers[i++] = "WEP40";
2711 group_ciphers[i] = NULL;
2713 pairwise = g_strjoinv(" ", pair_ciphers);
2714 group = g_strjoinv(" ", group_ciphers);
2716 SUPPLICANT_DBG("cipher %s %s", pairwise, group);
2718 supplicant_dbus_dict_append_basic(dict, "pairwise",
2721 supplicant_dbus_dict_append_basic(dict, "group",
2729 static void add_network_security_proto(DBusMessageIter *dict,
2730 GSupplicantSSID *ssid)
2732 unsigned int protocol, i;
2736 protocol = ssid->protocol;
2743 if (protocol & G_SUPPLICANT_PROTO_RSN)
2744 protos[i++] = "RSN";
2746 if (protocol & G_SUPPLICANT_PROTO_WPA)
2747 protos[i++] = "WPA";
2751 proto = g_strjoinv(" ", protos);
2753 SUPPLICANT_DBG("proto %s", proto);
2755 supplicant_dbus_dict_append_basic(dict, "proto",
2762 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
2766 switch (ssid->security) {
2767 case G_SUPPLICANT_SECURITY_UNKNOWN:
2768 case G_SUPPLICANT_SECURITY_NONE:
2769 case G_SUPPLICANT_SECURITY_WEP:
2771 add_network_security_wep(dict, ssid);
2773 case G_SUPPLICANT_SECURITY_PSK:
2774 key_mgmt = "WPA-PSK";
2775 add_network_security_psk(dict, ssid);
2776 add_network_security_ciphers(dict, ssid);
2777 add_network_security_proto(dict, ssid);
2779 case G_SUPPLICANT_SECURITY_IEEE8021X:
2780 key_mgmt = "WPA-EAP";
2781 add_network_security_eap(dict, ssid);
2782 add_network_security_ciphers(dict, ssid);
2783 add_network_security_proto(dict, ssid);
2787 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
2788 DBUS_TYPE_STRING, &key_mgmt);
2791 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
2795 switch (ssid->mode) {
2796 case G_SUPPLICANT_MODE_UNKNOWN:
2797 case G_SUPPLICANT_MODE_INFRA:
2800 case G_SUPPLICANT_MODE_IBSS:
2803 case G_SUPPLICANT_MODE_MASTER:
2808 supplicant_dbus_dict_append_basic(dict, "mode",
2809 DBUS_TYPE_UINT32, &mode);
2812 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
2814 DBusMessageIter dict;
2815 struct interface_connect_data *data = user_data;
2816 GSupplicantSSID *ssid = data->ssid;
2818 supplicant_dbus_dict_open(iter, &dict);
2820 if (ssid->scan_ssid)
2821 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
2822 DBUS_TYPE_UINT32, &ssid->scan_ssid);
2825 supplicant_dbus_dict_append_basic(&dict, "frequency",
2826 DBUS_TYPE_UINT32, &ssid->freq);
2828 add_network_mode(&dict, ssid);
2830 add_network_security(&dict, ssid);
2832 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
2833 DBUS_TYPE_BYTE, &ssid->ssid,
2836 supplicant_dbus_dict_close(iter, &dict);
2839 static void interface_wps_start_result(const char *error,
2840 DBusMessageIter *iter, void *user_data)
2842 struct interface_connect_data *data = user_data;
2846 SUPPLICANT_DBG("error: %s", error);
2852 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
2854 struct interface_connect_data *data = user_data;
2855 GSupplicantSSID *ssid = data->ssid;
2856 const char *role = "enrollee", *type;
2857 DBusMessageIter dict;
2861 supplicant_dbus_dict_open(iter, &dict);
2863 supplicant_dbus_dict_append_basic(&dict, "Role",
2864 DBUS_TYPE_STRING, &role);
2867 if (ssid->pin_wps != NULL) {
2869 supplicant_dbus_dict_append_basic(&dict, "Pin",
2870 DBUS_TYPE_STRING, &ssid->pin_wps);
2873 supplicant_dbus_dict_append_basic(&dict, "Type",
2874 DBUS_TYPE_STRING, &type);
2876 supplicant_dbus_dict_close(iter, &dict);
2879 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
2881 struct interface_connect_data *data = user_data;
2885 if (error != NULL) {
2886 SUPPLICANT_DBG("error: %s", error);
2892 supplicant_dbus_method_call(data->interface->path,
2893 SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
2894 interface_add_wps_params,
2895 interface_wps_start_result, data);
2898 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
2900 dbus_bool_t credentials = TRUE;
2904 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
2908 int g_supplicant_interface_connect(GSupplicantInterface *interface,
2909 GSupplicantSSID *ssid,
2910 GSupplicantInterfaceCallback callback,
2913 struct interface_connect_data *data;
2916 if (interface == NULL)
2919 if (system_available == FALSE)
2922 /* TODO: Check if we're already connected and switch */
2924 data = dbus_malloc0(sizeof(*data));
2928 data->interface = interface;
2929 data->callback = callback;
2931 data->user_data = user_data;
2933 if (ssid->use_wps == TRUE) {
2934 g_free(interface->wps_cred.key);
2935 memset(&interface->wps_cred, 0,
2936 sizeof(struct _GSupplicantWpsCredentials));
2938 ret = supplicant_dbus_property_set(interface->path,
2939 SUPPLICANT_INTERFACE ".Interface.WPS",
2940 "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
2941 wps_process_credentials, wps_start, data);
2943 ret = supplicant_dbus_method_call(interface->path,
2944 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
2945 interface_add_network_params,
2946 interface_add_network_result, data);
2951 return -EINPROGRESS;
2954 static void network_remove_result(const char *error,
2955 DBusMessageIter *iter, void *user_data)
2957 struct interface_data *data = user_data;
2965 if (data->callback != NULL)
2966 data->callback(result, data->interface, data->user_data);
2971 static void network_remove_params(DBusMessageIter *iter, void *user_data)
2973 struct interface_data *data = user_data;
2974 const char *path = data->interface->network_path;
2976 SUPPLICANT_DBG("path %s", path);
2978 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
2981 static int network_remove(struct interface_data *data)
2983 GSupplicantInterface *interface = data->interface;
2987 return supplicant_dbus_method_call(interface->path,
2988 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
2989 network_remove_params, network_remove_result, data);
2992 static void interface_disconnect_result(const char *error,
2993 DBusMessageIter *iter, void *user_data)
2995 struct interface_data *data = user_data;
2999 if (error != NULL && data->callback != NULL)
3000 data->callback(-EIO, data->interface, data->user_data);
3002 /* If we are disconnecting from previous WPS successful
3003 * association. i.e.: it did not went through AddNetwork,
3004 * and interface->network_path was never set. */
3005 if (data->interface->network_path == NULL)
3008 network_remove(data);
3011 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
3012 GSupplicantInterfaceCallback callback,
3015 struct interface_data *data;
3019 if (interface == NULL)
3022 if (system_available == FALSE)
3025 data = dbus_malloc0(sizeof(*data));
3029 data->interface = interface;
3030 data->callback = callback;
3031 data->user_data = user_data;
3033 return supplicant_dbus_method_call(interface->path,
3034 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
3035 NULL, interface_disconnect_result, data);
3039 static const char *g_supplicant_rule0 = "type=signal,"
3040 "path=" DBUS_PATH_DBUS ","
3041 "sender=" DBUS_SERVICE_DBUS ","
3042 "interface=" DBUS_INTERFACE_DBUS ","
3043 "member=NameOwnerChanged,"
3044 "arg0=" SUPPLICANT_SERVICE;
3045 static const char *g_supplicant_rule1 = "type=signal,"
3046 "interface=" SUPPLICANT_INTERFACE;
3047 static const char *g_supplicant_rule2 = "type=signal,"
3048 "interface=" SUPPLICANT_INTERFACE ".Interface";
3049 static const char *g_supplicant_rule3 = "type=signal,"
3050 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
3051 static const char *g_supplicant_rule4 = "type=signal,"
3052 "interface=" SUPPLICANT_INTERFACE ".BSS";
3053 static const char *g_supplicant_rule5 = "type=signal,"
3054 "interface=" SUPPLICANT_INTERFACE ".Network";
3056 static void invoke_introspect_method(void)
3058 DBusMessage *message;
3060 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
3062 DBUS_INTERFACE_INTROSPECTABLE,
3065 if (message == NULL)
3068 dbus_message_set_no_reply(message, TRUE);
3069 dbus_connection_send(connection, message, NULL);
3070 dbus_message_unref(message);
3073 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
3075 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3076 if (connection == NULL)
3079 if (dbus_connection_add_filter(connection,
3080 g_supplicant_filter, NULL, NULL) == FALSE) {
3081 dbus_connection_unref(connection);
3086 callbacks_pointer = callbacks;
3089 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
3090 NULL, remove_interface);
3092 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
3095 supplicant_dbus_setup(connection);
3097 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
3098 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
3099 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
3100 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
3101 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
3102 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
3103 dbus_connection_flush(connection);
3105 if (dbus_bus_name_has_owner(connection,
3106 SUPPLICANT_SERVICE, NULL) == TRUE) {
3107 system_available = TRUE;
3108 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
3109 SUPPLICANT_INTERFACE,
3110 service_property, NULL);
3112 invoke_introspect_method();
3117 static void unregister_interface_remove_params(DBusMessageIter *iter,
3120 const char *path = user_data;
3122 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3127 static void unregister_remove_interface(gpointer key, gpointer value,
3130 GSupplicantInterface *interface = value;
3132 supplicant_dbus_method_call(SUPPLICANT_PATH,
3133 SUPPLICANT_INTERFACE,
3135 unregister_interface_remove_params,
3136 NULL, interface->path);
3139 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
3143 if (connection != NULL) {
3144 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
3145 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
3146 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
3147 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
3148 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
3149 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
3150 dbus_connection_flush(connection);
3152 dbus_connection_remove_filter(connection,
3153 g_supplicant_filter, NULL);
3156 if (bss_mapping != NULL) {
3157 g_hash_table_destroy(bss_mapping);
3161 if (system_available == TRUE)
3162 callback_system_killed();
3164 if (interface_table != NULL) {
3165 g_hash_table_foreach(interface_table,
3166 unregister_remove_interface, NULL);
3167 g_hash_table_destroy(interface_table);
3168 interface_table = NULL;
3171 if (connection != NULL) {
3172 dbus_connection_unref(connection);
3176 callbacks_pointer = NULL;