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 _GSupplicantInterface {
142 unsigned int keymgmt_capa;
143 unsigned int authalg_capa;
144 unsigned int proto_capa;
145 unsigned int group_capa;
146 unsigned int pairwise_capa;
147 unsigned int scan_capa;
148 unsigned int mode_capa;
150 GSupplicantState state;
151 dbus_bool_t scanning;
152 GSupplicantInterfaceCallback scan_callback;
158 GHashTable *network_table;
159 GHashTable *net_mapping;
160 GHashTable *bss_mapping;
164 struct _GSupplicantNetwork {
165 GSupplicantInterface *interface;
169 unsigned char ssid[32];
170 unsigned int ssid_len;
172 GSupplicantMode mode;
173 GSupplicantSecurity security;
174 GHashTable *bss_table;
175 GHashTable *config_table;
178 struct g_supplicant_bss {
179 GSupplicantInterface *interface;
181 unsigned char bssid[6];
182 unsigned char ssid[32];
183 unsigned int ssid_len;
184 dbus_uint16_t frequency;
185 dbus_uint32_t maxrate;
187 GSupplicantMode mode;
188 GSupplicantSecurity security;
189 unsigned int keymgmt;
190 unsigned int pairwise;
194 dbus_bool_t ieee8021x;
197 static inline void debug(const char *format, ...)
202 if (callbacks_pointer->debug == NULL)
205 va_start(ap, format);
207 if (vsnprintf(str, sizeof(str), format, ap) > 0)
208 callbacks_pointer->debug(str);
213 #define SUPPLICANT_DBG(fmt, arg...) \
214 debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
216 static GSupplicantMode string2mode(const char *mode)
219 return G_SUPPLICANT_MODE_UNKNOWN;
221 if (g_str_equal(mode, "infrastructure") == TRUE)
222 return G_SUPPLICANT_MODE_INFRA;
223 else if (g_str_equal(mode, "ad-hoc") == TRUE)
224 return G_SUPPLICANT_MODE_IBSS;
226 return G_SUPPLICANT_MODE_UNKNOWN;
229 static const char *mode2string(GSupplicantMode mode)
232 case G_SUPPLICANT_MODE_UNKNOWN:
234 case G_SUPPLICANT_MODE_INFRA:
236 case G_SUPPLICANT_MODE_IBSS:
243 static const char *security2string(GSupplicantSecurity security)
246 case G_SUPPLICANT_SECURITY_UNKNOWN:
248 case G_SUPPLICANT_SECURITY_NONE:
250 case G_SUPPLICANT_SECURITY_WEP:
252 case G_SUPPLICANT_SECURITY_PSK:
254 case G_SUPPLICANT_SECURITY_IEEE8021X:
261 static GSupplicantState string2state(const char *state)
264 return G_SUPPLICANT_STATE_UNKNOWN;
266 if (g_str_equal(state, "unknown") == TRUE)
267 return G_SUPPLICANT_STATE_UNKNOWN;
268 else if (g_str_equal(state, "disconnected") == TRUE)
269 return G_SUPPLICANT_STATE_DISCONNECTED;
270 else if (g_str_equal(state, "inactive") == TRUE)
271 return G_SUPPLICANT_STATE_INACTIVE;
272 else if (g_str_equal(state, "scanning") == TRUE)
273 return G_SUPPLICANT_STATE_SCANNING;
274 else if (g_str_equal(state, "authenticating") == TRUE)
275 return G_SUPPLICANT_STATE_AUTHENTICATING;
276 else if (g_str_equal(state, "associating") == TRUE)
277 return G_SUPPLICANT_STATE_ASSOCIATING;
278 else if (g_str_equal(state, "associated") == TRUE)
279 return G_SUPPLICANT_STATE_ASSOCIATED;
280 else if (g_str_equal(state, "group_handshake") == TRUE)
281 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
282 else if (g_str_equal(state, "4way_handshake") == TRUE)
283 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
284 else if (g_str_equal(state, "completed") == TRUE)
285 return G_SUPPLICANT_STATE_COMPLETED;
287 return G_SUPPLICANT_STATE_UNKNOWN;
290 static void callback_system_ready(void)
292 if (system_ready == TRUE)
297 if (callbacks_pointer == NULL)
300 if (callbacks_pointer->system_ready == NULL)
303 callbacks_pointer->system_ready();
306 static void callback_system_killed(void)
308 system_ready = FALSE;
310 if (callbacks_pointer == NULL)
313 if (callbacks_pointer->system_killed == NULL)
316 callbacks_pointer->system_killed();
319 static void callback_interface_added(GSupplicantInterface *interface)
323 if (callbacks_pointer == NULL)
326 if (callbacks_pointer->interface_added == NULL)
329 callbacks_pointer->interface_added(interface);
332 static void callback_interface_state(GSupplicantInterface *interface)
334 if (callbacks_pointer == NULL)
337 if (callbacks_pointer->interface_state == NULL)
340 callbacks_pointer->interface_state(interface);
343 static void callback_interface_removed(GSupplicantInterface *interface)
345 if (callbacks_pointer == NULL)
348 if (callbacks_pointer->interface_removed == NULL)
351 callbacks_pointer->interface_removed(interface);
354 static void callback_scan_started(GSupplicantInterface *interface)
356 if (callbacks_pointer == NULL)
359 if (callbacks_pointer->scan_started == NULL)
362 callbacks_pointer->scan_started(interface);
365 static void callback_scan_finished(GSupplicantInterface *interface)
367 if (callbacks_pointer == NULL)
370 if (callbacks_pointer->scan_finished == NULL)
373 callbacks_pointer->scan_finished(interface);
376 static void callback_network_added(GSupplicantNetwork *network)
378 if (callbacks_pointer == NULL)
381 if (callbacks_pointer->network_added == NULL)
384 callbacks_pointer->network_added(network);
387 static void callback_network_removed(GSupplicantNetwork *network)
389 if (callbacks_pointer == NULL)
392 if (callbacks_pointer->network_removed == NULL)
395 callbacks_pointer->network_removed(network);
398 static void remove_interface(gpointer data)
400 GSupplicantInterface *interface = data;
402 g_hash_table_destroy(interface->bss_mapping);
403 g_hash_table_destroy(interface->net_mapping);
404 g_hash_table_destroy(interface->network_table);
406 callback_interface_removed(interface);
408 g_free(interface->path);
409 g_free(interface->ifname);
410 g_free(interface->driver);
411 g_free(interface->bridge);
415 static void remove_network(gpointer data)
417 GSupplicantNetwork *network = data;
419 g_hash_table_destroy(network->bss_table);
421 callback_network_removed(network);
423 g_hash_table_destroy(network->config_table);
425 g_free(network->group);
426 g_free(network->name);
430 static void remove_bss(gpointer data)
432 struct g_supplicant_bss *bss = data;
438 static void debug_strvalmap(const char *label, struct strvalmap *map,
443 for (i = 0; map[i].str != NULL; i++) {
444 if (val & map[i].val)
445 SUPPLICANT_DBG("%s: %s", label, map[i].str);
449 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
451 GSupplicantInterface *interface = user_data;
452 const char *str = NULL;
455 dbus_message_iter_get_basic(iter, &str);
459 for (i = 0; keymgmt_map[i].str != NULL; i++)
460 if (strcmp(str, keymgmt_map[i].str) == 0) {
461 interface->keymgmt_capa |= keymgmt_map[i].val;
466 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
468 GSupplicantInterface *interface = user_data;
469 const char *str = NULL;
472 dbus_message_iter_get_basic(iter, &str);
476 for (i = 0; authalg_capa_map[i].str != NULL; i++)
477 if (strcmp(str, authalg_capa_map[i].str) == 0) {
478 interface->authalg_capa |= authalg_capa_map[i].val;
483 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
485 GSupplicantInterface *interface = user_data;
486 const char *str = NULL;
489 dbus_message_iter_get_basic(iter, &str);
493 for (i = 0; proto_capa_map[i].str != NULL; i++)
494 if (strcmp(str, proto_capa_map[i].str) == 0) {
495 interface->proto_capa |= proto_capa_map[i].val;
500 static void interface_capability_pairwise(DBusMessageIter *iter,
503 GSupplicantInterface *interface = user_data;
504 const char *str = NULL;
507 dbus_message_iter_get_basic(iter, &str);
511 for (i = 0; pairwise_map[i].str != NULL; i++)
512 if (strcmp(str, pairwise_map[i].str) == 0) {
513 interface->pairwise_capa |= pairwise_map[i].val;
518 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
520 GSupplicantInterface *interface = user_data;
521 const char *str = NULL;
524 dbus_message_iter_get_basic(iter, &str);
528 for (i = 0; group_map[i].str != NULL; i++)
529 if (strcmp(str, group_map[i].str) == 0) {
530 interface->group_capa |= group_map[i].val;
535 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
537 GSupplicantInterface *interface = user_data;
538 const char *str = NULL;
541 dbus_message_iter_get_basic(iter, &str);
545 for (i = 0; scan_capa_map[i].str != NULL; i++)
546 if (strcmp(str, scan_capa_map[i].str) == 0) {
547 interface->scan_capa |= scan_capa_map[i].val;
552 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
554 GSupplicantInterface *interface = user_data;
555 const char *str = NULL;
558 dbus_message_iter_get_basic(iter, &str);
562 for (i = 0; mode_capa_map[i].str != NULL; i++)
563 if (strcmp(str, mode_capa_map[i].str) == 0) {
564 interface->mode_capa |= mode_capa_map[i].val;
569 static void interface_capability(const char *key, DBusMessageIter *iter,
572 GSupplicantInterface *interface = user_data;
577 if (g_strcmp0(key, "KeyMgmt") == 0)
578 supplicant_dbus_array_foreach(iter,
579 interface_capability_keymgmt, interface);
580 else if (g_strcmp0(key, "AuthAlg") == 0)
581 supplicant_dbus_array_foreach(iter,
582 interface_capability_authalg, interface);
583 else if (g_strcmp0(key, "Protocol") == 0)
584 supplicant_dbus_array_foreach(iter,
585 interface_capability_proto, interface);
586 else if (g_strcmp0(key, "Pairwise") == 0)
587 supplicant_dbus_array_foreach(iter,
588 interface_capability_pairwise, interface);
589 else if (g_strcmp0(key, "Group") == 0)
590 supplicant_dbus_array_foreach(iter,
591 interface_capability_group, interface);
592 else if (g_strcmp0(key, "Scan") == 0)
593 supplicant_dbus_array_foreach(iter,
594 interface_capability_scan, interface);
595 else if (g_strcmp0(key, "Modes") == 0)
596 supplicant_dbus_array_foreach(iter,
597 interface_capability_mode, interface);
599 SUPPLICANT_DBG("key %s type %c",
600 key, dbus_message_iter_get_arg_type(iter));
603 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
606 if (interface == NULL)
609 interface->data = data;
612 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
614 if (interface == NULL)
617 return interface->data;
620 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
622 if (interface == NULL)
625 return interface->ifname;
628 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
630 if (interface == NULL)
633 return interface->driver;
636 GSupplicantState g_supplicant_interface_get_state(
637 GSupplicantInterface *interface)
639 if (interface == NULL)
640 return G_SUPPLICANT_STATE_UNKNOWN;
642 return interface->state;
645 GSupplicantInterface *g_supplicant_network_get_interface(
646 GSupplicantNetwork *network)
651 return network->interface;
654 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
656 if (network == NULL || network->name == NULL)
659 return network->name;
662 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
664 if (network == NULL || network->group == NULL)
667 return network->group;
670 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
672 if (network == NULL || network->path == NULL)
675 return network->path;
678 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
681 return G_SUPPLICANT_MODE_UNKNOWN;
683 return mode2string(network->mode);
686 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
689 return G_SUPPLICANT_SECURITY_UNKNOWN;
691 return security2string(network->security);
694 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
695 unsigned int *ssid_len)
697 if (network == NULL || network->ssid == NULL) {
702 *ssid_len = network->ssid_len;
703 return network->ssid;
706 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
711 return network->signal;
714 static void merge_network(GSupplicantNetwork *network)
717 const char *ssid, *mode, *key_mgmt;
718 unsigned int i, ssid_len;
721 ssid = g_hash_table_lookup(network->config_table, "ssid");
722 mode = g_hash_table_lookup(network->config_table, "mode");
723 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
725 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
728 ssid_len = strlen(ssid);
732 str = g_string_sized_new((ssid_len * 2) + 24);
736 for (i = 0; i < ssid_len; i++)
737 g_string_append_printf(str, "%02x", ssid[i]);
739 if (g_strcmp0(mode, "0") == 0)
740 g_string_append_printf(str, "_managed");
741 else if (g_strcmp0(mode, "1") == 0)
742 g_string_append_printf(str, "_adhoc");
744 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
745 g_string_append_printf(str, "_psk");
747 group = g_string_free(str, FALSE);
749 SUPPLICANT_DBG("%s", group);
753 g_hash_table_destroy(network->config_table);
755 g_free(network->path);
759 static void network_property(const char *key, DBusMessageIter *iter,
762 GSupplicantNetwork *network = user_data;
764 if (network->interface == NULL)
768 merge_network(network);
772 if (g_strcmp0(key, "Enabled") == 0) {
773 dbus_bool_t enabled = FALSE;
775 dbus_message_iter_get_basic(iter, &enabled);
776 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
777 const char *str = NULL;
779 dbus_message_iter_get_basic(iter, &str);
781 g_hash_table_replace(network->config_table,
782 g_strdup(key), g_strdup(str));
785 SUPPLICANT_DBG("key %s type %c",
786 key, dbus_message_iter_get_arg_type(iter));
789 static void interface_network_added(DBusMessageIter *iter, void *user_data)
791 GSupplicantInterface *interface = user_data;
792 GSupplicantNetwork *network;
793 const char *path = NULL;
797 dbus_message_iter_get_basic(iter, &path);
801 if (g_strcmp0(path, "/") == 0)
804 network = g_hash_table_lookup(interface->net_mapping, path);
808 network = g_try_new0(GSupplicantNetwork, 1);
812 network->interface = interface;
813 network->path = g_strdup(path);
815 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
818 dbus_message_iter_next(iter);
819 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
820 supplicant_dbus_property_foreach(iter, network_property,
822 network_property(NULL, NULL, network);
826 supplicant_dbus_property_get_all(path,
827 SUPPLICANT_INTERFACE ".Network",
828 network_property, network);
831 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
833 GSupplicantInterface *interface = user_data;
834 GSupplicantNetwork *network;
835 const char *path = NULL;
837 dbus_message_iter_get_basic(iter, &path);
841 network = g_hash_table_lookup(interface->net_mapping, path);
845 g_hash_table_remove(interface->net_mapping, path);
848 static char *create_name(unsigned char *ssid, int ssid_len)
853 if (ssid_len < 1 || ssid[0] == '\0')
856 name = g_try_malloc0(ssid_len + 1);
861 for (i = 0; i < ssid_len; i++) {
862 if (g_ascii_isprint(ssid[i]))
871 static char *create_group(struct g_supplicant_bss *bss)
875 const char *mode, *security;
877 str = g_string_sized_new((bss->ssid_len * 2) + 24);
881 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
882 for (i = 0; i < bss->ssid_len; i++)
883 g_string_append_printf(str, "%02x", bss->ssid[i]);
885 g_string_append_printf(str, "hidden");
887 mode = mode2string(bss->mode);
889 g_string_append_printf(str, "_%s", mode);
891 security = security2string(bss->security);
892 if (security != NULL)
893 g_string_append_printf(str, "_%s", security);
895 return g_string_free(str, FALSE);
898 static void add_bss_to_network(struct g_supplicant_bss *bss)
900 GSupplicantInterface *interface = bss->interface;
901 GSupplicantNetwork *network;
904 group = create_group(bss);
908 network = g_hash_table_lookup(interface->network_table, group);
909 if (network != NULL) {
914 network = g_try_new0(GSupplicantNetwork, 1);
915 if (network == NULL) {
920 network->interface = interface;
921 if (network->path == NULL)
922 network->path = g_strdup(bss->path);
923 network->group = group;
924 network->name = create_name(bss->ssid, bss->ssid_len);
925 network->mode = bss->mode;
926 network->security = bss->security;
927 network->ssid_len = bss->ssid_len;
928 memcpy(network->ssid, bss->ssid, bss->ssid_len);
929 network->signal = bss->signal;
931 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
934 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
937 g_hash_table_replace(interface->network_table,
938 network->group, network);
940 callback_network_added(network);
943 g_hash_table_replace(interface->bss_mapping, bss->path, network);
944 g_hash_table_replace(network->bss_table, bss->path, bss);
946 g_hash_table_replace(bss_mapping, bss->path, interface);
949 static void bss_rates(DBusMessageIter *iter, void *user_data)
951 struct g_supplicant_bss *bss = user_data;
952 dbus_uint32_t rate = 0;
954 dbus_message_iter_get_basic(iter, &rate);
958 if (rate > bss->maxrate)
962 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
964 struct g_supplicant_bss *bss = user_data;
965 const char *str = NULL;
968 dbus_message_iter_get_basic(iter, &str);
972 for (i = 0; keymgmt_map[i].str != NULL; i++)
973 if (strcmp(str, keymgmt_map[i].str) == 0) {
974 bss->keymgmt |= keymgmt_map[i].val;
979 static void bss_group(DBusMessageIter *iter, void *user_data)
981 struct g_supplicant_bss *bss = user_data;
982 const char *str = NULL;
985 dbus_message_iter_get_basic(iter, &str);
989 for (i = 0; group_map[i].str != NULL; i++)
990 if (strcmp(str, group_map[i].str) == 0) {
991 bss->group |= group_map[i].val;
996 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
998 struct g_supplicant_bss *bss = user_data;
999 const char *str = NULL;
1002 dbus_message_iter_get_basic(iter, &str);
1006 for (i = 0; pairwise_map[i].str != NULL; i++)
1007 if (strcmp(str, pairwise_map[i].str) == 0) {
1008 bss->pairwise |= pairwise_map[i].val;
1013 static void bss_wpa(const char *key, DBusMessageIter *iter,
1016 if (g_strcmp0(key, "KeyMgmt") == 0)
1017 supplicant_dbus_array_foreach(iter, bss_keymgmt, user_data);
1018 else if (g_strcmp0(key, "Group") == 0)
1019 supplicant_dbus_array_foreach(iter, bss_group, user_data);
1020 else if (g_strcmp0(key, "Pairwise") == 0)
1021 supplicant_dbus_array_foreach(iter, bss_pairwise, user_data);
1026 static void bss_property(const char *key, DBusMessageIter *iter,
1029 struct g_supplicant_bss *bss = user_data;
1031 if (bss->interface == NULL)
1034 SUPPLICANT_DBG("key %s", key);
1037 if (bss->ieee8021x == TRUE)
1038 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1039 else if (bss->psk == TRUE)
1040 bss->security = G_SUPPLICANT_SECURITY_PSK;
1041 else if (bss->privacy == TRUE)
1042 bss->security = G_SUPPLICANT_SECURITY_WEP;
1044 bss->security = G_SUPPLICANT_SECURITY_NONE;
1046 add_bss_to_network(bss);
1050 if (g_strcmp0(key, "BSSID") == 0) {
1051 DBusMessageIter array;
1052 unsigned char *addr;
1055 dbus_message_iter_recurse(iter, &array);
1056 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1059 memcpy(bss->bssid, addr, addr_len);
1060 } else if (g_strcmp0(key, "SSID") == 0) {
1061 DBusMessageIter array;
1062 unsigned char *ssid;
1065 dbus_message_iter_recurse(iter, &array);
1066 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1068 if (ssid_len > 0 && ssid_len < 33) {
1069 memcpy(bss->ssid, ssid, ssid_len);
1070 bss->ssid_len = ssid_len;
1072 memset(bss->ssid, 0, sizeof(bss->ssid));
1075 } else if (g_strcmp0(key, "Capabilities") == 0) {
1076 dbus_uint16_t capabilities = 0x0000;
1078 dbus_message_iter_get_basic(iter, &capabilities);
1080 if (capabilities & IEEE80211_CAP_ESS)
1081 bss->mode = G_SUPPLICANT_MODE_INFRA;
1082 else if (capabilities & IEEE80211_CAP_IBSS)
1083 bss->mode = G_SUPPLICANT_MODE_IBSS;
1085 if (capabilities & IEEE80211_CAP_PRIVACY)
1086 bss->privacy = TRUE;
1087 } else if (g_strcmp0(key, "Mode") == 0) {
1088 const char *mode = NULL;
1090 dbus_message_iter_get_basic(iter, &mode);
1091 bss->mode = string2mode(mode);
1092 } else if (g_strcmp0(key, "Frequency") == 0) {
1093 dbus_uint16_t frequency = 0;
1095 dbus_message_iter_get_basic(iter, &frequency);
1096 bss->frequency = frequency;
1097 } else if (g_strcmp0(key, "Signal") == 0) {
1098 dbus_int16_t signal = 0;
1100 dbus_message_iter_get_basic(iter, &signal);
1102 bss->signal = signal;
1103 } else if (g_strcmp0(key, "Level") == 0) {
1104 dbus_int32_t level = 0;
1106 dbus_message_iter_get_basic(iter, &level);
1107 } else if (g_strcmp0(key, "Rates") == 0) {
1108 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1109 } else if (g_strcmp0(key, "MaxRate") == 0) {
1110 dbus_uint32_t maxrate = 0;
1112 dbus_message_iter_get_basic(iter, &maxrate);
1114 bss->maxrate = maxrate;
1115 } else if (g_strcmp0(key, "Privacy") == 0) {
1116 dbus_bool_t privacy = FALSE;
1118 dbus_message_iter_get_basic(iter, &privacy);
1119 bss->privacy = privacy;
1120 } else if ((g_strcmp0(key, "RSN") == 0) ||
1121 (g_strcmp0(key, "WPA") == 0)) {
1122 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1125 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1126 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1127 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1128 bss->ieee8021x = TRUE;
1131 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1132 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1133 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1136 SUPPLICANT_DBG("key %s type %c",
1137 key, dbus_message_iter_get_arg_type(iter));
1140 static void interface_bss_added(DBusMessageIter *iter, void *user_data)
1142 GSupplicantInterface *interface = user_data;
1143 GSupplicantNetwork *network;
1144 struct g_supplicant_bss *bss;
1145 const char *path = NULL;
1149 dbus_message_iter_get_basic(iter, &path);
1153 if (g_strcmp0(path, "/") == 0)
1156 SUPPLICANT_DBG("%s", path);
1158 network = g_hash_table_lookup(interface->bss_mapping, path);
1159 if (network != NULL) {
1160 bss = g_hash_table_lookup(network->bss_table, path);
1165 bss = g_try_new0(struct g_supplicant_bss, 1);
1169 bss->interface = interface;
1170 bss->path = g_strdup(path);
1172 dbus_message_iter_next(iter);
1173 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1174 supplicant_dbus_property_foreach(iter, bss_property, bss);
1175 bss_property(NULL, NULL, bss);
1179 supplicant_dbus_property_get_all(path,
1180 SUPPLICANT_INTERFACE ".BSS",
1184 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1186 GSupplicantInterface *interface = user_data;
1187 GSupplicantNetwork *network;
1188 const char *path = NULL;
1190 dbus_message_iter_get_basic(iter, &path);
1194 network = g_hash_table_lookup(interface->bss_mapping, path);
1195 if (network == NULL)
1198 g_hash_table_remove(bss_mapping, path);
1200 g_hash_table_remove(interface->bss_mapping, path);
1201 g_hash_table_remove(network->bss_table, path);
1203 if (g_hash_table_size(network->bss_table) == 0)
1204 g_hash_table_remove(interface->network_table, network->group);
1207 static void interface_property(const char *key, DBusMessageIter *iter,
1210 GSupplicantInterface *interface = user_data;
1212 if (interface == NULL)
1215 SUPPLICANT_DBG("%s", key);
1218 debug_strvalmap("KeyMgmt capability", keymgmt_map,
1219 interface->keymgmt_capa);
1220 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1221 interface->authalg_capa);
1222 debug_strvalmap("Protocol capability", proto_capa_map,
1223 interface->proto_capa);
1224 debug_strvalmap("Pairwise capability", pairwise_map,
1225 interface->pairwise_capa);
1226 debug_strvalmap("Group capability", group_map,
1227 interface->group_capa);
1228 debug_strvalmap("Scan capability", scan_capa_map,
1229 interface->scan_capa);
1230 debug_strvalmap("Mode capability", mode_capa_map,
1231 interface->mode_capa);
1233 interface->ready = TRUE;
1234 callback_interface_added(interface);
1238 if (g_strcmp0(key, "Capabilities") == 0) {
1239 supplicant_dbus_property_foreach(iter, interface_capability,
1241 } else if (g_strcmp0(key, "State") == 0) {
1242 const char *str = NULL;
1244 dbus_message_iter_get_basic(iter, &str);
1246 if (string2state(str) != interface->state) {
1247 interface->state = string2state(str);
1248 callback_interface_state(interface);
1251 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
1252 } else if (g_strcmp0(key, "Scanning") == 0) {
1253 dbus_bool_t scanning = FALSE;
1255 dbus_message_iter_get_basic(iter, &scanning);
1256 interface->scanning = scanning;
1258 if (interface->ready == TRUE) {
1259 if (interface->scanning == TRUE)
1260 callback_scan_started(interface);
1262 callback_scan_finished(interface);
1264 } else if (g_strcmp0(key, "ApScan") == 0) {
1267 dbus_message_iter_get_basic(iter, &apscan);
1268 interface->apscan = apscan;
1269 } else if (g_strcmp0(key, "Ifname") == 0) {
1270 const char *str = NULL;
1272 dbus_message_iter_get_basic(iter, &str);
1274 interface->ifname = g_strdup(str);
1275 } else if (g_strcmp0(key, "Driver") == 0) {
1276 const char *str = NULL;
1278 dbus_message_iter_get_basic(iter, &str);
1280 interface->driver = g_strdup(str);
1281 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1282 const char *str = NULL;
1284 dbus_message_iter_get_basic(iter, &str);
1286 interface->bridge = g_strdup(str);
1287 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1288 interface_bss_added(iter, interface);
1289 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1290 interface_network_added(iter, interface);
1291 } else if (g_strcmp0(key, "BSSs") == 0) {
1292 supplicant_dbus_array_foreach(iter, interface_bss_added,
1294 } else if (g_strcmp0(key, "Blobs") == 0) {
1296 } else if (g_strcmp0(key, "Networks") == 0) {
1297 supplicant_dbus_array_foreach(iter, interface_network_added,
1300 SUPPLICANT_DBG("key %s type %c",
1301 key, dbus_message_iter_get_arg_type(iter));
1304 static void scan_network_update(DBusMessageIter *iter, void *user_data)
1306 GSupplicantInterface *interface = user_data;
1307 GSupplicantNetwork *network;
1313 dbus_message_iter_get_basic(iter, &path);
1318 if (g_strcmp0(path, "/") == 0)
1321 /* Update the network details based on scan BSS data */
1322 network = g_hash_table_lookup(interface->bss_mapping, path);
1323 if (network != NULL)
1324 callback_network_added(network);
1327 static void scan_bss_data(const char *key, DBusMessageIter *iter,
1330 GSupplicantInterface *interface = user_data;
1333 supplicant_dbus_array_foreach(iter, scan_network_update,
1336 if (interface->scan_callback != NULL)
1337 interface->scan_callback(0, interface, interface->scan_data);
1339 interface->scan_callback = NULL;
1340 interface->scan_data = NULL;
1343 static GSupplicantInterface *interface_alloc(const char *path)
1345 GSupplicantInterface *interface;
1347 interface = g_try_new0(GSupplicantInterface, 1);
1348 if (interface == NULL)
1351 interface->path = g_strdup(path);
1353 interface->network_table = g_hash_table_new_full(g_str_hash,
1354 g_str_equal, NULL, remove_network);
1356 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1358 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1361 g_hash_table_replace(interface_table, interface->path, interface);
1366 static void interface_added(DBusMessageIter *iter, void *user_data)
1368 GSupplicantInterface *interface;
1369 const char *path = NULL;
1373 dbus_message_iter_get_basic(iter, &path);
1377 if (g_strcmp0(path, "/") == 0)
1380 interface = g_hash_table_lookup(interface_table, path);
1381 if (interface != NULL)
1384 interface = interface_alloc(path);
1385 if (interface == NULL)
1388 dbus_message_iter_next(iter);
1389 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1390 supplicant_dbus_property_foreach(iter, interface_property,
1392 interface_property(NULL, NULL, interface);
1396 supplicant_dbus_property_get_all(path,
1397 SUPPLICANT_INTERFACE ".Interface",
1398 interface_property, interface);
1401 static void interface_removed(DBusMessageIter *iter, void *user_data)
1403 const char *path = NULL;
1405 dbus_message_iter_get_basic(iter, &path);
1409 g_hash_table_remove(interface_table, path);
1412 static void eap_method(DBusMessageIter *iter, void *user_data)
1414 const char *str = NULL;
1417 dbus_message_iter_get_basic(iter, &str);
1421 for (i = 0; eap_method_map[i].str != NULL; i++)
1422 if (strcmp(str, eap_method_map[i].str) == 0) {
1423 eap_methods |= eap_method_map[i].val;
1428 static void service_property(const char *key, DBusMessageIter *iter,
1432 callback_system_ready();
1436 if (g_strcmp0(key, "DebugLevel") == 0) {
1437 const char *str = NULL;
1440 dbus_message_iter_get_basic(iter, &str);
1441 for (i = 0; debug_strings[i] != NULL; i++)
1442 if (g_strcmp0(debug_strings[i], str) == 0) {
1446 SUPPLICANT_DBG("Debug level %d", debug_level);
1447 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1448 dbus_message_iter_get_basic(iter, &debug_timestamp);
1449 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
1450 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1451 dbus_message_iter_get_basic(iter, &debug_showkeys);
1452 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
1453 } else if (g_strcmp0(key, "Interfaces") == 0) {
1454 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1455 } else if (g_strcmp0(key, "EapMethods") == 0) {
1456 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1457 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1458 } else if (g_strcmp0(key, "Country") == 0) {
1459 const char *country = NULL;
1461 dbus_message_iter_get_basic(iter, &country);
1462 SUPPLICANT_DBG("Country %s", country);
1464 SUPPLICANT_DBG("key %s type %c",
1465 key, dbus_message_iter_get_arg_type(iter));
1468 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1470 const char *name = NULL, *old = NULL, *new = NULL;
1474 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1477 dbus_message_iter_get_basic(iter, &name);
1481 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1484 dbus_message_iter_next(iter);
1485 dbus_message_iter_get_basic(iter, &old);
1486 dbus_message_iter_next(iter);
1487 dbus_message_iter_get_basic(iter, &new);
1489 if (old == NULL || new == NULL)
1492 if (strlen(old) > 0 && strlen(new) == 0) {
1493 system_available = FALSE;
1494 g_hash_table_remove_all(bss_mapping);
1495 g_hash_table_remove_all(interface_table);
1496 callback_system_killed();
1499 if (strlen(new) > 0 && strlen(old) == 0) {
1500 system_available = TRUE;
1501 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1502 SUPPLICANT_INTERFACE,
1503 service_property, NULL);
1507 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1511 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1514 supplicant_dbus_property_foreach(iter, service_property, NULL);
1517 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1519 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
1521 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1522 interface_added(iter, NULL);
1525 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1529 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1530 interface_removed(iter, NULL);
1533 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1535 GSupplicantInterface *interface;
1539 interface = g_hash_table_lookup(interface_table, path);
1540 if (interface == NULL)
1543 supplicant_dbus_property_foreach(iter, interface_property, interface);
1546 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1548 GSupplicantInterface *interface;
1549 dbus_bool_t success = FALSE;
1553 interface = g_hash_table_lookup(interface_table, path);
1554 if (interface == NULL)
1557 dbus_message_iter_get_basic(iter, &success);
1560 * If scan is unsuccessful return -EIO else get the scanned BSSs
1561 * and update the network details accordingly
1563 if (success == FALSE) {
1564 if (interface->scan_callback != NULL)
1565 interface->scan_callback(-EIO, interface,
1566 interface->scan_data);
1568 interface->scan_callback = NULL;
1569 interface->scan_data = NULL;
1574 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
1575 "BSSs", scan_bss_data, interface);
1578 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1580 GSupplicantInterface *interface;
1584 interface = g_hash_table_lookup(interface_table, path);
1585 if (interface == NULL)
1588 interface_bss_added(iter, interface);
1591 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1593 GSupplicantInterface *interface;
1597 interface = g_hash_table_lookup(interface_table, path);
1598 if (interface == NULL)
1601 interface_bss_removed(iter, interface);
1604 static void signal_network_added(const char *path, DBusMessageIter *iter)
1606 GSupplicantInterface *interface;
1610 interface = g_hash_table_lookup(interface_table, path);
1611 if (interface == NULL)
1614 interface_network_added(iter, interface);
1617 static void signal_network_removed(const char *path, DBusMessageIter *iter)
1619 GSupplicantInterface *interface;
1623 interface = g_hash_table_lookup(interface_table, path);
1624 if (interface == NULL)
1627 interface_network_removed(iter, interface);
1630 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
1632 GSupplicantInterface *interface;
1633 GSupplicantNetwork *network;
1634 struct g_supplicant_bss *bss;
1638 interface = g_hash_table_lookup(bss_mapping, path);
1639 if (interface == NULL)
1642 network = g_hash_table_lookup(interface->bss_mapping, path);
1643 if (network == NULL)
1646 bss = g_hash_table_lookup(network->bss_table, path);
1650 supplicant_dbus_property_foreach(iter, bss_property, bss);
1654 const char *interface;
1656 void (*function) (const char *path, DBusMessageIter *iter);
1658 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
1660 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
1661 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
1662 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
1663 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
1665 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
1666 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
1667 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
1668 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
1669 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
1670 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
1672 { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed },
1677 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
1678 DBusMessage *message, void *data)
1680 DBusMessageIter iter;
1684 path = dbus_message_get_path(message);
1686 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1688 if (dbus_message_iter_init(message, &iter) == FALSE)
1689 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1691 for (i = 0; signal_map[i].interface != NULL; i++) {
1692 if (dbus_message_has_interface(message,
1693 signal_map[i].interface) == FALSE)
1696 if (dbus_message_has_member(message,
1697 signal_map[i].member) == FALSE)
1700 signal_map[i].function(path, &iter);
1704 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1707 struct supplicant_regdom {
1708 GSupplicantCountryCallback callback;
1709 const void *user_data;
1712 static void country_result(const char *error,
1713 DBusMessageIter *iter, void *user_data)
1715 struct supplicant_regdom *regdom = user_data;
1718 SUPPLICANT_DBG("Country setting result");
1720 if (user_data == NULL)
1723 if (error == NULL) {
1724 alpha2 = (char *)regdom->user_data;
1726 SUPPLICANT_DBG("Country setting failure %s", error);
1730 if (regdom->callback)
1731 regdom->callback(alpha2);
1736 static void country_params(DBusMessageIter *iter, void *user_data)
1738 struct supplicant_regdom *regdom = user_data;
1739 const char *country;
1741 country = regdom->user_data;
1743 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &country);
1746 int g_supplicant_set_country(const char *alpha2,
1747 GSupplicantCountryCallback callback,
1748 const void *user_data)
1750 struct supplicant_regdom *regdom;
1752 SUPPLICANT_DBG("Country setting %s", alpha2);
1754 if (system_available == FALSE)
1757 regdom = dbus_malloc0(sizeof(*regdom));
1761 regdom->callback = callback;
1762 regdom->user_data = user_data;
1764 return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
1765 "Country", DBUS_TYPE_STRING_AS_STRING,
1766 country_params, country_result,
1770 struct interface_data {
1771 GSupplicantInterface *interface;
1772 GSupplicantInterfaceCallback callback;
1776 struct interface_create_data {
1779 GSupplicantInterface *interface;
1780 GSupplicantInterfaceCallback callback;
1784 struct interface_connect_data {
1785 GSupplicantInterface *interface;
1786 GSupplicantInterfaceCallback callback;
1787 GSupplicantSSID *ssid;
1791 static void interface_create_property(const char *key, DBusMessageIter *iter,
1794 struct interface_create_data *data = user_data;
1795 GSupplicantInterface *interface = data->interface;
1798 if (data->callback != NULL)
1799 data->callback(0, data->interface, data->user_data);
1804 interface_property(key, iter, interface);
1807 static void interface_create_result(const char *error,
1808 DBusMessageIter *iter, void *user_data)
1810 struct interface_create_data *data = user_data;
1811 const char *path = NULL;
1816 if (error != NULL) {
1817 g_critical("error %s", error);
1822 dbus_message_iter_get_basic(iter, &path);
1828 if (system_available == FALSE) {
1833 data->interface = g_hash_table_lookup(interface_table, path);
1834 if (data->interface == NULL) {
1835 data->interface = interface_alloc(path);
1836 if (data->interface == NULL) {
1842 err = supplicant_dbus_property_get_all(path,
1843 SUPPLICANT_INTERFACE ".Interface",
1844 interface_create_property, data);
1849 if (data->callback != NULL)
1850 data->callback(err, NULL, data->user_data);
1855 static void interface_create_params(DBusMessageIter *iter, void *user_data)
1857 struct interface_create_data *data = user_data;
1858 DBusMessageIter dict;
1862 supplicant_dbus_dict_open(iter, &dict);
1864 supplicant_dbus_dict_append_basic(&dict, "Ifname",
1865 DBUS_TYPE_STRING, &data->ifname);
1867 if (data->driver != NULL)
1868 supplicant_dbus_dict_append_basic(&dict, "Driver",
1869 DBUS_TYPE_STRING, &data->driver);
1871 supplicant_dbus_dict_close(iter, &dict);
1874 static void interface_get_result(const char *error,
1875 DBusMessageIter *iter, void *user_data)
1877 struct interface_create_data *data = user_data;
1878 GSupplicantInterface *interface;
1879 const char *path = NULL;
1884 if (error != NULL) {
1885 g_warning("error %s", error);
1890 dbus_message_iter_get_basic(iter, &path);
1896 interface = g_hash_table_lookup(interface_table, path);
1897 if (interface == NULL) {
1902 if (data->callback != NULL)
1903 data->callback(0, interface, data->user_data);
1910 if (system_available == FALSE) {
1915 SUPPLICANT_DBG("Creating interface");
1917 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
1918 SUPPLICANT_INTERFACE,
1920 interface_create_params,
1921 interface_create_result, data);
1926 if (data->callback != NULL)
1927 data->callback(err, NULL, data->user_data);
1932 static void interface_get_params(DBusMessageIter *iter, void *user_data)
1934 struct interface_create_data *data = user_data;
1938 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
1941 int g_supplicant_interface_create(const char *ifname, const char *driver,
1942 GSupplicantInterfaceCallback callback,
1945 struct interface_create_data *data;
1947 SUPPLICANT_DBG("ifname %s", ifname);
1952 if (system_available == FALSE)
1955 data = dbus_malloc0(sizeof(*data));
1959 data->ifname = ifname;
1960 data->driver = driver;
1961 data->callback = callback;
1962 data->user_data = user_data;
1964 return supplicant_dbus_method_call(SUPPLICANT_PATH,
1965 SUPPLICANT_INTERFACE,
1967 interface_get_params,
1968 interface_get_result, data);
1971 static void interface_remove_result(const char *error,
1972 DBusMessageIter *iter, void *user_data)
1974 struct interface_data *data = user_data;
1977 if (error != NULL) {
1982 if (system_available == FALSE) {
1987 g_hash_table_remove(interface_table, data->interface->path);
1991 if (data->callback != NULL)
1992 data->callback(err, data->interface, data->user_data);
1998 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
2000 struct interface_data *data = user_data;
2002 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2003 &data->interface->path);
2007 int g_supplicant_interface_remove(GSupplicantInterface *interface,
2008 GSupplicantInterfaceCallback callback,
2011 struct interface_data *data;
2013 if (interface == NULL)
2016 if (system_available == FALSE)
2019 data = dbus_malloc0(sizeof(*data));
2023 data->interface = interface;
2024 data->callback = callback;
2025 data->user_data = user_data;
2027 return supplicant_dbus_method_call(SUPPLICANT_PATH,
2028 SUPPLICANT_INTERFACE,
2030 interface_remove_params,
2031 interface_remove_result, data);
2034 static void interface_scan_result(const char *error,
2035 DBusMessageIter *iter, void *user_data)
2037 struct interface_data *data = user_data;
2039 if (error != NULL) {
2040 if (data->callback != NULL)
2041 data->callback(-EIO, data->interface, data->user_data);
2043 data->interface->scan_callback = data->callback;
2044 data->interface->scan_data = data->user_data;
2050 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
2052 DBusMessageIter dict;
2053 const char *type = "passive";
2055 supplicant_dbus_dict_open(iter, &dict);
2057 supplicant_dbus_dict_append_basic(&dict, "Type",
2058 DBUS_TYPE_STRING, &type);
2060 supplicant_dbus_dict_close(iter, &dict);
2063 int g_supplicant_interface_scan(GSupplicantInterface *interface,
2064 GSupplicantInterfaceCallback callback,
2067 struct interface_data *data;
2069 if (interface == NULL)
2072 if (system_available == FALSE)
2075 if (interface->scanning == TRUE)
2078 switch (interface->state) {
2079 case G_SUPPLICANT_STATE_AUTHENTICATING:
2080 case G_SUPPLICANT_STATE_ASSOCIATING:
2081 case G_SUPPLICANT_STATE_ASSOCIATED:
2082 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2083 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2085 case G_SUPPLICANT_STATE_UNKNOWN:
2086 case G_SUPPLICANT_STATE_DISCONNECTED:
2087 case G_SUPPLICANT_STATE_INACTIVE:
2088 case G_SUPPLICANT_STATE_SCANNING:
2089 case G_SUPPLICANT_STATE_COMPLETED:
2093 data = dbus_malloc0(sizeof(*data));
2097 data->interface = interface;
2098 data->callback = callback;
2099 data->user_data = user_data;
2101 return supplicant_dbus_method_call(interface->path,
2102 SUPPLICANT_INTERFACE ".Interface", "Scan",
2103 interface_scan_params, interface_scan_result, data);
2106 static void interface_select_network_result(const char *error,
2107 DBusMessageIter *iter, void *user_data)
2109 struct interface_connect_data *data = user_data;
2116 static void interface_select_network_params(DBusMessageIter *iter,
2119 struct interface_connect_data *data = user_data;
2120 GSupplicantInterface *interface = data->interface;
2122 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2123 &interface->network_path);
2126 static void interface_add_network_result(const char *error,
2127 DBusMessageIter *iter, void *user_data)
2129 struct interface_connect_data *data = user_data;
2130 GSupplicantInterface *interface = data->interface;
2136 dbus_message_iter_get_basic(iter, &path);
2140 SUPPLICANT_DBG("PATH: %s", path);
2142 g_free(interface->network_path);
2143 interface->network_path = g_strdup(path);
2145 supplicant_dbus_method_call(data->interface->path,
2146 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
2147 interface_select_network_params,
2148 interface_select_network_result, data);
2153 g_free(interface->network_path);
2154 interface->network_path = NULL;
2158 static void add_network_security_wep(DBusMessageIter *dict,
2159 GSupplicantSSID *ssid)
2161 const char *auth_alg = "OPEN SHARED";
2162 const char *key_index = "0";
2164 supplicant_dbus_dict_append_basic(dict, "auth_alg",
2165 DBUS_TYPE_STRING, &auth_alg);
2167 if (ssid->passphrase) {
2168 int size = strlen(ssid->passphrase);
2169 if (size == 10 || size == 26) {
2170 unsigned char *key = g_try_malloc(13);
2174 memset(tmp, 0, sizeof(tmp));
2178 for (i = 0; i < size / 2; i++) {
2179 memcpy(tmp, ssid->passphrase + (i * 2), 2);
2180 key[i] = (unsigned char) strtol(tmp, NULL, 16);
2183 supplicant_dbus_dict_append_fixed_array(dict,
2188 } else if (size == 5 || size == 13) {
2189 unsigned char *key = g_try_malloc(13);
2195 for (i = 0; i < size; i++)
2196 key[i] = (unsigned char) ssid->passphrase[i];
2198 supplicant_dbus_dict_append_fixed_array(dict,
2204 supplicant_dbus_dict_append_basic(dict,
2209 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
2210 DBUS_TYPE_STRING, &key_index);
2214 static void add_network_security_psk(DBusMessageIter *dict,
2215 GSupplicantSSID *ssid)
2217 if (ssid->passphrase && strlen(ssid->passphrase) > 0)
2218 supplicant_dbus_dict_append_basic(dict, "psk",
2223 static void add_network_security_tls(DBusMessageIter *dict,
2224 GSupplicantSSID *ssid)
2227 * For TLS, we at least need:
2228 * The client certificate
2229 * The client private key file
2230 * The client private key file password
2232 * The Authority certificate is optional.
2234 if (ssid->client_cert_path == NULL)
2237 if (ssid->private_key_path == NULL)
2240 if (ssid->private_key_passphrase == NULL)
2243 if (ssid->ca_cert_path)
2244 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2245 DBUS_TYPE_STRING, &ssid->ca_cert_path);
2247 supplicant_dbus_dict_append_basic(dict, "private_key",
2249 &ssid->private_key_path);
2250 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2252 &ssid->private_key_passphrase);
2253 supplicant_dbus_dict_append_basic(dict, "client_cert",
2255 &ssid->client_cert_path);
2258 static void add_network_security_peap(DBusMessageIter *dict,
2259 GSupplicantSSID *ssid)
2264 * For PEAP/TTLS, we at least need
2265 * The authority certificate
2266 * The 2nd phase authentication method
2267 * The 2nd phase passphrase
2269 * The Client certificate is optional although strongly required
2270 * When setting it, we need in addition
2271 * The Client private key file
2272 * The Client private key file password
2274 if (ssid->passphrase == NULL)
2277 if (ssid->ca_cert_path == NULL)
2280 if (ssid->phase2_auth == NULL)
2283 if (ssid->client_cert_path) {
2284 if (ssid->private_key_path == NULL)
2287 if (ssid->private_key_passphrase == NULL)
2290 supplicant_dbus_dict_append_basic(dict, "client_cert",
2292 &ssid->client_cert_path);
2294 supplicant_dbus_dict_append_basic(dict, "private_key",
2296 &ssid->private_key_path);
2298 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2300 &ssid->private_key_passphrase);
2304 phase2_auth = g_strdup_printf("\"auth=%s\"", ssid->phase2_auth);
2306 supplicant_dbus_dict_append_basic(dict, "password",
2310 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2312 &ssid->ca_cert_path);
2314 supplicant_dbus_dict_append_basic(dict, "phase2",
2316 &ssid->phase2_auth);
2318 g_free(phase2_auth);
2321 static void add_network_security_eap(DBusMessageIter *dict,
2322 GSupplicantSSID *ssid)
2326 if (ssid->eap == NULL || ssid->identity == NULL)
2329 if (g_strcmp0(ssid->eap, "tls") == 0) {
2330 add_network_security_tls(dict, ssid);
2331 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
2332 g_strcmp0(ssid->eap, "ttls") == 0) {
2333 add_network_security_peap(dict, ssid);
2337 eap_value = g_ascii_strup(ssid->eap, -1);
2339 supplicant_dbus_dict_append_basic(dict, "eap",
2342 supplicant_dbus_dict_append_basic(dict, "identity",
2349 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
2353 switch (ssid->security) {
2354 case G_SUPPLICANT_SECURITY_UNKNOWN:
2355 case G_SUPPLICANT_SECURITY_NONE:
2356 case G_SUPPLICANT_SECURITY_WEP:
2358 add_network_security_wep(dict, ssid);
2360 case G_SUPPLICANT_SECURITY_PSK:
2361 key_mgmt = "WPA-PSK";
2362 add_network_security_psk(dict, ssid);
2364 case G_SUPPLICANT_SECURITY_IEEE8021X:
2365 key_mgmt = "WPA-EAP";
2366 add_network_security_eap(dict, ssid);
2370 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
2371 DBUS_TYPE_STRING, &key_mgmt);
2374 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
2376 DBusMessageIter dict;
2377 dbus_uint32_t scan_ssid = 1;
2378 struct interface_connect_data *data = user_data;
2379 GSupplicantSSID *ssid = data->ssid;
2381 supplicant_dbus_dict_open(iter, &dict);
2383 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
2384 DBUS_TYPE_UINT32, &scan_ssid);
2386 add_network_security(&dict, ssid);
2388 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
2389 DBUS_TYPE_BYTE, &ssid->ssid,
2392 supplicant_dbus_dict_close(iter, &dict);
2395 int g_supplicant_interface_connect(GSupplicantInterface *interface,
2396 GSupplicantSSID *ssid,
2397 GSupplicantInterfaceCallback callback,
2400 struct interface_connect_data *data;
2403 if (interface == NULL)
2406 if (system_available == FALSE)
2409 /* TODO: Check if we're already connected and switch */
2411 data = dbus_malloc0(sizeof(*data));
2415 data->interface = interface;
2416 data->callback = callback;
2418 data->user_data = user_data;
2420 ret = supplicant_dbus_method_call(interface->path,
2421 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
2422 interface_add_network_params,
2423 interface_add_network_result, data);
2427 return -EINPROGRESS;
2430 static void network_remove_result(const char *error,
2431 DBusMessageIter *iter, void *user_data)
2433 struct interface_data *data = user_data;
2441 if (data->callback != NULL)
2442 data->callback(result, data->interface, data->user_data);
2447 static void network_remove_params(DBusMessageIter *iter, void *user_data)
2449 struct interface_data *data = user_data;
2450 const char *path = data->interface->network_path;
2452 SUPPLICANT_DBG("path %s", path);
2454 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
2457 static int network_remove(struct interface_data *data)
2459 GSupplicantInterface *interface = data->interface;
2463 return supplicant_dbus_method_call(interface->path,
2464 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
2465 network_remove_params, network_remove_result, data);
2468 static void interface_disconnect_result(const char *error,
2469 DBusMessageIter *iter, void *user_data)
2471 struct interface_data *data = user_data;
2475 if (error != NULL && data->callback != NULL) {
2476 data->callback(-EIO, data->interface, data->user_data);
2480 network_remove(data);
2483 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
2484 GSupplicantInterfaceCallback callback,
2487 struct interface_data *data;
2491 if (interface == NULL)
2494 if (system_available == FALSE)
2497 data = dbus_malloc0(sizeof(*data));
2501 data->interface = interface;
2502 data->callback = callback;
2503 data->user_data = user_data;
2505 return supplicant_dbus_method_call(interface->path,
2506 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
2507 NULL, interface_disconnect_result, data);
2511 static const char *g_supplicant_rule0 = "type=signal,"
2512 "path=" DBUS_PATH_DBUS ","
2513 "sender=" DBUS_SERVICE_DBUS ","
2514 "interface=" DBUS_INTERFACE_DBUS ","
2515 "member=NameOwnerChanged,"
2516 "arg0=" SUPPLICANT_SERVICE;
2517 static const char *g_supplicant_rule1 = "type=signal,"
2518 "interface=" SUPPLICANT_INTERFACE;
2519 static const char *g_supplicant_rule2 = "type=signal,"
2520 "interface=" SUPPLICANT_INTERFACE ".Interface";
2521 static const char *g_supplicant_rule3 = "type=signal,"
2522 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
2523 static const char *g_supplicant_rule4 = "type=signal,"
2524 "interface=" SUPPLICANT_INTERFACE ".BSS";
2525 static const char *g_supplicant_rule5 = "type=signal,"
2526 "interface=" SUPPLICANT_INTERFACE ".Network";
2528 static void invoke_introspect_method(void)
2530 DBusMessage *message;
2532 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
2534 DBUS_INTERFACE_INTROSPECTABLE,
2537 if (message == NULL)
2540 dbus_message_set_no_reply(message, TRUE);
2541 dbus_connection_send(connection, message, NULL);
2542 dbus_message_unref(message);
2545 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
2547 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
2548 if (connection == NULL)
2551 if (dbus_connection_add_filter(connection,
2552 g_supplicant_filter, NULL, NULL) == FALSE) {
2553 dbus_connection_unref(connection);
2558 callbacks_pointer = callbacks;
2561 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2562 NULL, remove_interface);
2564 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2567 supplicant_dbus_setup(connection);
2569 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
2570 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
2571 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
2572 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
2573 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
2574 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
2575 dbus_connection_flush(connection);
2577 if (dbus_bus_name_has_owner(connection,
2578 SUPPLICANT_SERVICE, NULL) == TRUE) {
2579 system_available = TRUE;
2580 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
2581 SUPPLICANT_INTERFACE,
2582 service_property, NULL);
2584 invoke_introspect_method();
2589 static void unregister_interface_remove_params(DBusMessageIter *iter,
2592 const char *path = user_data;
2594 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2599 static void unregister_remove_interface(gpointer key, gpointer value,
2602 GSupplicantInterface *interface = value;
2604 supplicant_dbus_method_call(SUPPLICANT_PATH,
2605 SUPPLICANT_INTERFACE,
2607 unregister_interface_remove_params,
2608 NULL, interface->path);
2611 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
2615 if (connection != NULL) {
2616 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
2617 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
2618 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
2619 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
2620 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
2621 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
2622 dbus_connection_flush(connection);
2624 dbus_connection_remove_filter(connection,
2625 g_supplicant_filter, NULL);
2628 if (bss_mapping != NULL) {
2629 g_hash_table_destroy(bss_mapping);
2633 if (interface_table != NULL) {
2634 g_hash_table_foreach(interface_table,
2635 unregister_remove_interface, NULL);
2636 g_hash_table_destroy(interface_table);
2637 interface_table = NULL;
2640 if (system_available == TRUE)
2641 callback_system_killed();
2643 if (connection != NULL) {
2644 dbus_connection_unref(connection);
2648 callbacks_pointer = NULL;