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 {
141 unsigned int keymgmt_capa;
142 unsigned int authalg_capa;
143 unsigned int proto_capa;
144 unsigned int group_capa;
145 unsigned int pairwise_capa;
146 unsigned int scan_capa;
147 unsigned int mode_capa;
149 GSupplicantState state;
150 dbus_bool_t scanning;
151 GSupplicantInterfaceCallback scan_callback;
157 GHashTable *network_table;
158 GHashTable *net_mapping;
159 GHashTable *bss_mapping;
163 struct _GSupplicantNetwork {
164 GSupplicantInterface *interface;
168 unsigned char ssid[32];
169 unsigned int ssid_len;
171 GSupplicantMode mode;
172 GSupplicantSecurity security;
173 GHashTable *bss_table;
174 GHashTable *config_table;
177 struct g_supplicant_bss {
178 GSupplicantInterface *interface;
180 unsigned char bssid[6];
181 unsigned char ssid[32];
182 unsigned int ssid_len;
183 dbus_uint16_t frequency;
184 dbus_uint32_t maxrate;
186 GSupplicantMode mode;
187 GSupplicantSecurity security;
188 unsigned int keymgmt;
189 unsigned int pairwise;
193 dbus_bool_t ieee8021x;
196 static inline void debug(const char *format, ...)
201 if (callbacks_pointer->debug == NULL)
204 va_start(ap, format);
206 if (vsnprintf(str, sizeof(str), format, ap) > 0)
207 callbacks_pointer->debug(str);
212 #define SUPPLICANT_DBG(fmt, arg...) \
213 debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
215 static GSupplicantMode string2mode(const char *mode)
218 return G_SUPPLICANT_MODE_UNKNOWN;
220 if (g_str_equal(mode, "infrastructure") == TRUE)
221 return G_SUPPLICANT_MODE_INFRA;
222 else if (g_str_equal(mode, "ad-hoc") == TRUE)
223 return G_SUPPLICANT_MODE_IBSS;
225 return G_SUPPLICANT_MODE_UNKNOWN;
228 static const char *mode2string(GSupplicantMode mode)
231 case G_SUPPLICANT_MODE_UNKNOWN:
233 case G_SUPPLICANT_MODE_INFRA:
235 case G_SUPPLICANT_MODE_IBSS:
242 static const char *security2string(GSupplicantSecurity security)
245 case G_SUPPLICANT_SECURITY_UNKNOWN:
247 case G_SUPPLICANT_SECURITY_NONE:
249 case G_SUPPLICANT_SECURITY_WEP:
251 case G_SUPPLICANT_SECURITY_PSK:
253 case G_SUPPLICANT_SECURITY_IEEE8021X:
260 static GSupplicantState string2state(const char *state)
263 return G_SUPPLICANT_STATE_UNKNOWN;
265 if (g_str_equal(state, "unknown") == TRUE)
266 return G_SUPPLICANT_STATE_UNKNOWN;
267 else if (g_str_equal(state, "disconnected") == TRUE)
268 return G_SUPPLICANT_STATE_DISCONNECTED;
269 else if (g_str_equal(state, "inactive") == TRUE)
270 return G_SUPPLICANT_STATE_INACTIVE;
271 else if (g_str_equal(state, "scanning") == TRUE)
272 return G_SUPPLICANT_STATE_SCANNING;
273 else if (g_str_equal(state, "authenticating") == TRUE)
274 return G_SUPPLICANT_STATE_AUTHENTICATING;
275 else if (g_str_equal(state, "associating") == TRUE)
276 return G_SUPPLICANT_STATE_ASSOCIATING;
277 else if (g_str_equal(state, "associated") == TRUE)
278 return G_SUPPLICANT_STATE_ASSOCIATED;
279 else if (g_str_equal(state, "group_handshake") == TRUE)
280 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
281 else if (g_str_equal(state, "4way_handshake") == TRUE)
282 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
283 else if (g_str_equal(state, "completed") == TRUE)
284 return G_SUPPLICANT_STATE_COMPLETED;
286 return G_SUPPLICANT_STATE_UNKNOWN;
289 static void callback_system_ready(void)
291 if (system_ready == TRUE)
296 if (callbacks_pointer == NULL)
299 if (callbacks_pointer->system_ready == NULL)
302 callbacks_pointer->system_ready();
305 static void callback_system_killed(void)
307 system_ready = FALSE;
309 if (callbacks_pointer == NULL)
312 if (callbacks_pointer->system_killed == NULL)
315 callbacks_pointer->system_killed();
318 static void callback_interface_added(GSupplicantInterface *interface)
322 if (callbacks_pointer == NULL)
325 if (callbacks_pointer->interface_added == NULL)
328 callbacks_pointer->interface_added(interface);
331 static void callback_interface_state(GSupplicantInterface *interface)
333 if (callbacks_pointer == NULL)
336 if (callbacks_pointer->interface_state == NULL)
339 callbacks_pointer->interface_state(interface);
342 static void callback_interface_removed(GSupplicantInterface *interface)
344 if (callbacks_pointer == NULL)
347 if (callbacks_pointer->interface_removed == NULL)
350 callbacks_pointer->interface_removed(interface);
353 static void callback_scan_started(GSupplicantInterface *interface)
355 if (callbacks_pointer == NULL)
358 if (callbacks_pointer->scan_started == NULL)
361 callbacks_pointer->scan_started(interface);
364 static void callback_scan_finished(GSupplicantInterface *interface)
366 if (callbacks_pointer == NULL)
369 if (callbacks_pointer->scan_finished == NULL)
372 callbacks_pointer->scan_finished(interface);
375 static void callback_network_added(GSupplicantNetwork *network)
377 if (callbacks_pointer == NULL)
380 if (callbacks_pointer->network_added == NULL)
383 callbacks_pointer->network_added(network);
386 static void callback_network_removed(GSupplicantNetwork *network)
388 if (callbacks_pointer == NULL)
391 if (callbacks_pointer->network_removed == NULL)
394 callbacks_pointer->network_removed(network);
397 static void remove_interface(gpointer data)
399 GSupplicantInterface *interface = data;
401 g_hash_table_destroy(interface->bss_mapping);
402 g_hash_table_destroy(interface->net_mapping);
403 g_hash_table_destroy(interface->network_table);
405 callback_interface_removed(interface);
407 g_free(interface->path);
408 g_free(interface->ifname);
409 g_free(interface->driver);
410 g_free(interface->bridge);
414 static void remove_network(gpointer data)
416 GSupplicantNetwork *network = data;
418 g_hash_table_destroy(network->bss_table);
420 callback_network_removed(network);
422 g_hash_table_destroy(network->config_table);
424 g_free(network->group);
425 g_free(network->name);
429 static void remove_bss(gpointer data)
431 struct g_supplicant_bss *bss = data;
437 static void debug_strvalmap(const char *label, struct strvalmap *map,
442 for (i = 0; map[i].str != NULL; i++) {
443 if (val & map[i].val)
444 SUPPLICANT_DBG("%s: %s", label, map[i].str);
448 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
450 GSupplicantInterface *interface = user_data;
451 const char *str = NULL;
454 dbus_message_iter_get_basic(iter, &str);
458 for (i = 0; keymgmt_map[i].str != NULL; i++)
459 if (strcmp(str, keymgmt_map[i].str) == 0) {
460 interface->keymgmt_capa |= keymgmt_map[i].val;
465 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
467 GSupplicantInterface *interface = user_data;
468 const char *str = NULL;
471 dbus_message_iter_get_basic(iter, &str);
475 for (i = 0; authalg_capa_map[i].str != NULL; i++)
476 if (strcmp(str, authalg_capa_map[i].str) == 0) {
477 interface->authalg_capa |= authalg_capa_map[i].val;
482 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
484 GSupplicantInterface *interface = user_data;
485 const char *str = NULL;
488 dbus_message_iter_get_basic(iter, &str);
492 for (i = 0; proto_capa_map[i].str != NULL; i++)
493 if (strcmp(str, proto_capa_map[i].str) == 0) {
494 interface->proto_capa |= proto_capa_map[i].val;
499 static void interface_capability_pairwise(DBusMessageIter *iter,
502 GSupplicantInterface *interface = user_data;
503 const char *str = NULL;
506 dbus_message_iter_get_basic(iter, &str);
510 for (i = 0; pairwise_map[i].str != NULL; i++)
511 if (strcmp(str, pairwise_map[i].str) == 0) {
512 interface->pairwise_capa |= pairwise_map[i].val;
517 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
519 GSupplicantInterface *interface = user_data;
520 const char *str = NULL;
523 dbus_message_iter_get_basic(iter, &str);
527 for (i = 0; group_map[i].str != NULL; i++)
528 if (strcmp(str, group_map[i].str) == 0) {
529 interface->group_capa |= group_map[i].val;
534 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
536 GSupplicantInterface *interface = user_data;
537 const char *str = NULL;
540 dbus_message_iter_get_basic(iter, &str);
544 for (i = 0; scan_capa_map[i].str != NULL; i++)
545 if (strcmp(str, scan_capa_map[i].str) == 0) {
546 interface->scan_capa |= scan_capa_map[i].val;
551 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
553 GSupplicantInterface *interface = user_data;
554 const char *str = NULL;
557 dbus_message_iter_get_basic(iter, &str);
561 for (i = 0; mode_capa_map[i].str != NULL; i++)
562 if (strcmp(str, mode_capa_map[i].str) == 0) {
563 interface->mode_capa |= mode_capa_map[i].val;
568 static void interface_capability(const char *key, DBusMessageIter *iter,
571 GSupplicantInterface *interface = user_data;
576 if (g_strcmp0(key, "KeyMgmt") == 0)
577 supplicant_dbus_array_foreach(iter,
578 interface_capability_keymgmt, interface);
579 else if (g_strcmp0(key, "AuthAlg") == 0)
580 supplicant_dbus_array_foreach(iter,
581 interface_capability_authalg, interface);
582 else if (g_strcmp0(key, "Protocol") == 0)
583 supplicant_dbus_array_foreach(iter,
584 interface_capability_proto, interface);
585 else if (g_strcmp0(key, "Pairwise") == 0)
586 supplicant_dbus_array_foreach(iter,
587 interface_capability_pairwise, interface);
588 else if (g_strcmp0(key, "Group") == 0)
589 supplicant_dbus_array_foreach(iter,
590 interface_capability_group, interface);
591 else if (g_strcmp0(key, "Scan") == 0)
592 supplicant_dbus_array_foreach(iter,
593 interface_capability_scan, interface);
594 else if (g_strcmp0(key, "Modes") == 0)
595 supplicant_dbus_array_foreach(iter,
596 interface_capability_mode, interface);
598 SUPPLICANT_DBG("key %s type %c",
599 key, dbus_message_iter_get_arg_type(iter));
602 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
605 if (interface == NULL)
608 interface->data = data;
611 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
613 if (interface == NULL)
616 return interface->data;
619 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
621 if (interface == NULL)
624 return interface->ifname;
627 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
629 if (interface == NULL)
632 return interface->driver;
635 GSupplicantState g_supplicant_interface_get_state(
636 GSupplicantInterface *interface)
638 if (interface == NULL)
639 return G_SUPPLICANT_STATE_UNKNOWN;
641 return interface->state;
644 GSupplicantInterface *g_supplicant_network_get_interface(
645 GSupplicantNetwork *network)
650 return network->interface;
653 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
655 if (network == NULL || network->name == NULL)
658 return network->name;
661 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
663 if (network == NULL || network->group == NULL)
666 return network->group;
669 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
671 if (network == NULL || network->path == NULL)
674 return network->path;
677 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
680 return G_SUPPLICANT_MODE_UNKNOWN;
682 return mode2string(network->mode);
685 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
688 return G_SUPPLICANT_SECURITY_UNKNOWN;
690 return security2string(network->security);
693 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
694 unsigned int *ssid_len)
696 if (network == NULL || network->ssid == NULL) {
701 *ssid_len = network->ssid_len;
702 return network->ssid;
705 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
710 return network->signal;
713 static void merge_network(GSupplicantNetwork *network)
716 const char *ssid, *mode, *key_mgmt;
717 unsigned int i, ssid_len;
720 ssid = g_hash_table_lookup(network->config_table, "ssid");
721 mode = g_hash_table_lookup(network->config_table, "mode");
722 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
724 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
727 ssid_len = strlen(ssid);
731 str = g_string_sized_new((ssid_len * 2) + 24);
735 for (i = 0; i < ssid_len; i++)
736 g_string_append_printf(str, "%02x", ssid[i]);
738 if (g_strcmp0(mode, "0") == 0)
739 g_string_append_printf(str, "_managed");
740 else if (g_strcmp0(mode, "1") == 0)
741 g_string_append_printf(str, "_adhoc");
743 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
744 g_string_append_printf(str, "_psk");
746 group = g_string_free(str, FALSE);
748 SUPPLICANT_DBG("%s", group);
752 g_hash_table_destroy(network->config_table);
754 g_free(network->path);
758 static void network_property(const char *key, DBusMessageIter *iter,
761 GSupplicantNetwork *network = user_data;
763 if (network->interface == NULL)
767 merge_network(network);
771 if (g_strcmp0(key, "Enabled") == 0) {
772 dbus_bool_t enabled = FALSE;
774 dbus_message_iter_get_basic(iter, &enabled);
775 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
776 const char *str = NULL;
778 dbus_message_iter_get_basic(iter, &str);
780 g_hash_table_replace(network->config_table,
781 g_strdup(key), g_strdup(str));
784 SUPPLICANT_DBG("key %s type %c",
785 key, dbus_message_iter_get_arg_type(iter));
788 static void interface_network_added(DBusMessageIter *iter, void *user_data)
790 GSupplicantInterface *interface = user_data;
791 GSupplicantNetwork *network;
792 const char *path = NULL;
796 dbus_message_iter_get_basic(iter, &path);
800 if (g_strcmp0(path, "/") == 0)
803 network = g_hash_table_lookup(interface->net_mapping, path);
807 network = g_try_new0(GSupplicantNetwork, 1);
811 network->interface = interface;
812 network->path = g_strdup(path);
814 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
817 dbus_message_iter_next(iter);
818 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
819 supplicant_dbus_property_foreach(iter, network_property,
821 network_property(NULL, NULL, network);
825 supplicant_dbus_property_get_all(path,
826 SUPPLICANT_INTERFACE ".Interface.Network",
827 network_property, network);
830 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
832 GSupplicantInterface *interface = user_data;
833 GSupplicantNetwork *network;
834 const char *path = NULL;
836 dbus_message_iter_get_basic(iter, &path);
840 network = g_hash_table_lookup(interface->net_mapping, path);
844 g_hash_table_remove(interface->net_mapping, path);
847 static char *create_name(unsigned char *ssid, int ssid_len)
852 if (ssid_len < 1 || ssid[0] == '\0')
855 name = g_try_malloc0(ssid_len + 1);
860 for (i = 0; i < ssid_len; i++) {
861 if (g_ascii_isprint(ssid[i]))
870 static char *create_group(struct g_supplicant_bss *bss)
874 const char *mode, *security;
876 str = g_string_sized_new((bss->ssid_len * 2) + 24);
880 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
881 for (i = 0; i < bss->ssid_len; i++)
882 g_string_append_printf(str, "%02x", bss->ssid[i]);
884 g_string_append_printf(str, "hidden");
886 mode = mode2string(bss->mode);
888 g_string_append_printf(str, "_%s", mode);
890 security = security2string(bss->security);
891 if (security != NULL)
892 g_string_append_printf(str, "_%s", security);
894 return g_string_free(str, FALSE);
897 static void add_bss_to_network(struct g_supplicant_bss *bss)
899 GSupplicantInterface *interface = bss->interface;
900 GSupplicantNetwork *network;
903 group = create_group(bss);
907 network = g_hash_table_lookup(interface->network_table, group);
908 if (network != NULL) {
913 network = g_try_new0(GSupplicantNetwork, 1);
914 if (network == NULL) {
919 network->interface = interface;
920 if (network->path == NULL)
921 network->path = g_strdup(bss->path);
922 network->group = group;
923 network->name = create_name(bss->ssid, bss->ssid_len);
924 network->mode = bss->mode;
925 network->security = bss->security;
926 network->ssid_len = bss->ssid_len;
927 memcpy(network->ssid, bss->ssid, bss->ssid_len);
928 network->signal = bss->signal;
930 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
933 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
936 g_hash_table_replace(interface->network_table,
937 network->group, network);
939 callback_network_added(network);
942 g_hash_table_replace(interface->bss_mapping, bss->path, network);
943 g_hash_table_replace(network->bss_table, bss->path, bss);
945 g_hash_table_replace(bss_mapping, bss->path, interface);
948 static void bss_rates(DBusMessageIter *iter, void *user_data)
950 struct g_supplicant_bss *bss = user_data;
951 dbus_uint32_t rate = 0;
953 dbus_message_iter_get_basic(iter, &rate);
957 if (rate > bss->maxrate)
961 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
963 struct g_supplicant_bss *bss = user_data;
964 const char *str = NULL;
967 dbus_message_iter_get_basic(iter, &str);
971 for (i = 0; keymgmt_map[i].str != NULL; i++)
972 if (strcmp(str, keymgmt_map[i].str) == 0) {
973 bss->keymgmt |= keymgmt_map[i].val;
978 static void bss_group(DBusMessageIter *iter, void *user_data)
980 struct g_supplicant_bss *bss = user_data;
981 const char *str = NULL;
984 dbus_message_iter_get_basic(iter, &str);
988 for (i = 0; group_map[i].str != NULL; i++)
989 if (strcmp(str, group_map[i].str) == 0) {
990 bss->group |= group_map[i].val;
995 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
997 struct g_supplicant_bss *bss = user_data;
998 const char *str = NULL;
1001 dbus_message_iter_get_basic(iter, &str);
1005 for (i = 0; pairwise_map[i].str != NULL; i++)
1006 if (strcmp(str, pairwise_map[i].str) == 0) {
1007 bss->pairwise |= pairwise_map[i].val;
1012 static void bss_wpa(const char *key, DBusMessageIter *iter,
1015 if (g_strcmp0(key, "KeyMgmt") == 0)
1016 supplicant_dbus_array_foreach(iter, bss_keymgmt, user_data);
1017 else if (g_strcmp0(key, "Group") == 0)
1018 supplicant_dbus_array_foreach(iter, bss_group, user_data);
1019 else if (g_strcmp0(key, "Pairwise") == 0)
1020 supplicant_dbus_array_foreach(iter, bss_pairwise, user_data);
1025 static void bss_property(const char *key, DBusMessageIter *iter,
1028 struct g_supplicant_bss *bss = user_data;
1030 if (bss->interface == NULL)
1033 SUPPLICANT_DBG("key %s", key);
1036 if (bss->ieee8021x == TRUE)
1037 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1038 else if (bss->psk == TRUE)
1039 bss->security = G_SUPPLICANT_SECURITY_PSK;
1040 else if (bss->privacy == TRUE)
1041 bss->security = G_SUPPLICANT_SECURITY_WEP;
1043 bss->security = G_SUPPLICANT_SECURITY_NONE;
1045 add_bss_to_network(bss);
1049 if (g_strcmp0(key, "BSSID") == 0) {
1050 DBusMessageIter array;
1051 unsigned char *addr;
1054 dbus_message_iter_recurse(iter, &array);
1055 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1058 memcpy(bss->bssid, addr, addr_len);
1059 } else if (g_strcmp0(key, "SSID") == 0) {
1060 DBusMessageIter array;
1061 unsigned char *ssid;
1064 dbus_message_iter_recurse(iter, &array);
1065 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1067 if (ssid_len > 0 && ssid_len < 33) {
1068 memcpy(bss->ssid, ssid, ssid_len);
1069 bss->ssid_len = ssid_len;
1071 memset(bss->ssid, 0, sizeof(bss->ssid));
1074 } else if (g_strcmp0(key, "Capabilities") == 0) {
1075 dbus_uint16_t capabilities = 0x0000;
1077 dbus_message_iter_get_basic(iter, &capabilities);
1079 if (capabilities & IEEE80211_CAP_ESS)
1080 bss->mode = G_SUPPLICANT_MODE_INFRA;
1081 else if (capabilities & IEEE80211_CAP_IBSS)
1082 bss->mode = G_SUPPLICANT_MODE_IBSS;
1084 if (capabilities & IEEE80211_CAP_PRIVACY)
1085 bss->privacy = TRUE;
1086 } else if (g_strcmp0(key, "Mode") == 0) {
1087 const char *mode = NULL;
1089 dbus_message_iter_get_basic(iter, &mode);
1090 bss->mode = string2mode(mode);
1091 } else if (g_strcmp0(key, "Frequency") == 0) {
1092 dbus_uint16_t frequency = 0;
1094 dbus_message_iter_get_basic(iter, &frequency);
1095 bss->frequency = frequency;
1096 } else if (g_strcmp0(key, "Signal") == 0) {
1097 dbus_int16_t signal = 0;
1099 dbus_message_iter_get_basic(iter, &signal);
1101 bss->signal = signal;
1102 } else if (g_strcmp0(key, "Level") == 0) {
1103 dbus_int32_t level = 0;
1105 dbus_message_iter_get_basic(iter, &level);
1106 } else if (g_strcmp0(key, "Rates") == 0) {
1107 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1108 } else if (g_strcmp0(key, "MaxRate") == 0) {
1109 dbus_uint32_t maxrate = 0;
1111 dbus_message_iter_get_basic(iter, &maxrate);
1113 bss->maxrate = maxrate;
1114 } else if (g_strcmp0(key, "Privacy") == 0) {
1115 dbus_bool_t privacy = FALSE;
1117 dbus_message_iter_get_basic(iter, &privacy);
1118 bss->privacy = privacy;
1119 } else if ((g_strcmp0(key, "RSN") == 0) ||
1120 (g_strcmp0(key, "WPA") == 0)) {
1121 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1124 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1125 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1126 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1127 bss->ieee8021x = TRUE;
1130 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1131 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1132 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1135 SUPPLICANT_DBG("key %s type %c",
1136 key, dbus_message_iter_get_arg_type(iter));
1139 static void interface_bss_added(DBusMessageIter *iter, void *user_data)
1141 GSupplicantInterface *interface = user_data;
1142 GSupplicantNetwork *network;
1143 struct g_supplicant_bss *bss;
1144 const char *path = NULL;
1148 dbus_message_iter_get_basic(iter, &path);
1152 if (g_strcmp0(path, "/") == 0)
1155 SUPPLICANT_DBG("%s", path);
1157 network = g_hash_table_lookup(interface->bss_mapping, path);
1158 if (network != NULL) {
1159 bss = g_hash_table_lookup(network->bss_table, path);
1164 bss = g_try_new0(struct g_supplicant_bss, 1);
1168 bss->interface = interface;
1169 bss->path = g_strdup(path);
1171 dbus_message_iter_next(iter);
1172 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1173 supplicant_dbus_property_foreach(iter, bss_property, bss);
1174 bss_property(NULL, NULL, bss);
1178 supplicant_dbus_property_get_all(path,
1179 SUPPLICANT_INTERFACE ".Interface.BSS",
1183 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1185 GSupplicantInterface *interface = user_data;
1186 GSupplicantNetwork *network;
1187 const char *path = NULL;
1189 dbus_message_iter_get_basic(iter, &path);
1193 network = g_hash_table_lookup(interface->bss_mapping, path);
1194 if (network == NULL)
1197 g_hash_table_remove(bss_mapping, path);
1199 g_hash_table_remove(interface->bss_mapping, path);
1200 g_hash_table_remove(network->bss_table, path);
1202 if (g_hash_table_size(network->bss_table) == 0)
1203 g_hash_table_remove(interface->network_table, network->group);
1206 static void interface_property(const char *key, DBusMessageIter *iter,
1209 GSupplicantInterface *interface = user_data;
1211 if (interface == NULL)
1214 SUPPLICANT_DBG("%s", key);
1217 debug_strvalmap("KeyMgmt capability", keymgmt_map,
1218 interface->keymgmt_capa);
1219 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1220 interface->authalg_capa);
1221 debug_strvalmap("Protocol capability", proto_capa_map,
1222 interface->proto_capa);
1223 debug_strvalmap("Pairwise capability", pairwise_map,
1224 interface->pairwise_capa);
1225 debug_strvalmap("Group capability", group_map,
1226 interface->group_capa);
1227 debug_strvalmap("Scan capability", scan_capa_map,
1228 interface->scan_capa);
1229 debug_strvalmap("Mode capability", mode_capa_map,
1230 interface->mode_capa);
1232 interface->ready = TRUE;
1233 callback_interface_added(interface);
1237 if (g_strcmp0(key, "Capabilities") == 0) {
1238 supplicant_dbus_property_foreach(iter, interface_capability,
1240 } else if (g_strcmp0(key, "State") == 0) {
1241 const char *str = NULL;
1243 dbus_message_iter_get_basic(iter, &str);
1245 if (string2state(str) != interface->state) {
1246 interface->state = string2state(str);
1247 callback_interface_state(interface);
1250 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
1251 } else if (g_strcmp0(key, "Scanning") == 0) {
1252 dbus_bool_t scanning = FALSE;
1254 dbus_message_iter_get_basic(iter, &scanning);
1255 interface->scanning = scanning;
1257 if (interface->ready == TRUE) {
1258 if (interface->scanning == TRUE)
1259 callback_scan_started(interface);
1261 callback_scan_finished(interface);
1263 } else if (g_strcmp0(key, "ApScan") == 0) {
1266 dbus_message_iter_get_basic(iter, &apscan);
1267 interface->apscan = apscan;
1268 } else if (g_strcmp0(key, "Ifname") == 0) {
1269 const char *str = NULL;
1271 dbus_message_iter_get_basic(iter, &str);
1273 interface->ifname = g_strdup(str);
1274 } else if (g_strcmp0(key, "Driver") == 0) {
1275 const char *str = NULL;
1277 dbus_message_iter_get_basic(iter, &str);
1279 interface->driver = g_strdup(str);
1280 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1281 const char *str = NULL;
1283 dbus_message_iter_get_basic(iter, &str);
1285 interface->bridge = g_strdup(str);
1286 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1287 interface_bss_added(iter, interface);
1288 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1289 interface_network_added(iter, interface);
1290 } else if (g_strcmp0(key, "BSSs") == 0) {
1291 supplicant_dbus_array_foreach(iter, interface_bss_added,
1293 } else if (g_strcmp0(key, "Blobs") == 0) {
1295 } else if (g_strcmp0(key, "Networks") == 0) {
1296 supplicant_dbus_array_foreach(iter, interface_network_added,
1299 SUPPLICANT_DBG("key %s type %c",
1300 key, dbus_message_iter_get_arg_type(iter));
1303 static void scan_network_update(DBusMessageIter *iter, void *user_data)
1305 GSupplicantInterface *interface = user_data;
1306 GSupplicantNetwork *network;
1312 dbus_message_iter_get_basic(iter, &path);
1317 if (g_strcmp0(path, "/") == 0)
1320 /* Update the network details based on scan BSS data */
1321 network = g_hash_table_lookup(interface->bss_mapping, path);
1322 if (network != NULL)
1323 callback_network_added(network);
1326 static void scan_bss_data(const char *key, DBusMessageIter *iter,
1329 GSupplicantInterface *interface = user_data;
1332 supplicant_dbus_array_foreach(iter, scan_network_update,
1335 if (interface->scan_callback != NULL)
1336 interface->scan_callback(0, interface, interface->scan_data);
1338 interface->scan_callback = NULL;
1339 interface->scan_data = NULL;
1342 static GSupplicantInterface *interface_alloc(const char *path)
1344 GSupplicantInterface *interface;
1346 interface = g_try_new0(GSupplicantInterface, 1);
1347 if (interface == NULL)
1350 interface->path = g_strdup(path);
1352 interface->network_table = g_hash_table_new_full(g_str_hash,
1353 g_str_equal, NULL, remove_network);
1355 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1357 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1360 g_hash_table_replace(interface_table, interface->path, interface);
1365 static void interface_added(DBusMessageIter *iter, void *user_data)
1367 GSupplicantInterface *interface;
1368 const char *path = NULL;
1372 dbus_message_iter_get_basic(iter, &path);
1376 if (g_strcmp0(path, "/") == 0)
1379 interface = g_hash_table_lookup(interface_table, path);
1380 if (interface != NULL)
1383 interface = interface_alloc(path);
1384 if (interface == NULL)
1387 dbus_message_iter_next(iter);
1388 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1389 supplicant_dbus_property_foreach(iter, interface_property,
1391 interface_property(NULL, NULL, interface);
1395 supplicant_dbus_property_get_all(path,
1396 SUPPLICANT_INTERFACE ".Interface",
1397 interface_property, interface);
1400 static void interface_removed(DBusMessageIter *iter, void *user_data)
1402 const char *path = NULL;
1404 dbus_message_iter_get_basic(iter, &path);
1408 g_hash_table_remove(interface_table, path);
1411 static void eap_method(DBusMessageIter *iter, void *user_data)
1413 const char *str = NULL;
1416 dbus_message_iter_get_basic(iter, &str);
1420 for (i = 0; eap_method_map[i].str != NULL; i++)
1421 if (strcmp(str, eap_method_map[i].str) == 0) {
1422 eap_methods |= eap_method_map[i].val;
1427 static void service_property(const char *key, DBusMessageIter *iter,
1431 callback_system_ready();
1435 if (g_strcmp0(key, "DebugLevel") == 0) {
1436 const char *str = NULL;
1439 dbus_message_iter_get_basic(iter, &str);
1440 for (i = 0; debug_strings[i] != NULL; i++)
1441 if (g_strcmp0(debug_strings[i], str) == 0) {
1445 SUPPLICANT_DBG("Debug level %d", debug_level);
1446 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1447 dbus_message_iter_get_basic(iter, &debug_timestamp);
1448 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
1449 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1450 dbus_message_iter_get_basic(iter, &debug_showkeys);
1451 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
1452 } else if (g_strcmp0(key, "Interfaces") == 0) {
1453 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1454 } else if (g_strcmp0(key, "EapMethods") == 0) {
1455 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1456 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1458 SUPPLICANT_DBG("key %s type %c",
1459 key, dbus_message_iter_get_arg_type(iter));
1462 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1464 const char *name = NULL, *old = NULL, *new = NULL;
1468 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1471 dbus_message_iter_get_basic(iter, &name);
1475 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1478 dbus_message_iter_next(iter);
1479 dbus_message_iter_get_basic(iter, &old);
1480 dbus_message_iter_next(iter);
1481 dbus_message_iter_get_basic(iter, &new);
1483 if (old == NULL || new == NULL)
1486 if (strlen(old) > 0 && strlen(new) == 0) {
1487 system_available = FALSE;
1488 g_hash_table_remove_all(bss_mapping);
1489 g_hash_table_remove_all(interface_table);
1490 callback_system_killed();
1493 if (strlen(new) > 0 && strlen(old) == 0) {
1494 system_available = TRUE;
1495 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1496 SUPPLICANT_INTERFACE,
1497 service_property, NULL);
1501 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1505 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1508 supplicant_dbus_property_foreach(iter, service_property, NULL);
1511 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1513 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
1515 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1516 interface_added(iter, NULL);
1519 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1523 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1524 interface_removed(iter, NULL);
1527 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1529 GSupplicantInterface *interface;
1533 interface = g_hash_table_lookup(interface_table, path);
1534 if (interface == NULL)
1537 supplicant_dbus_property_foreach(iter, interface_property, interface);
1540 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1542 GSupplicantInterface *interface;
1543 dbus_bool_t success = FALSE;
1547 interface = g_hash_table_lookup(interface_table, path);
1548 if (interface == NULL)
1551 dbus_message_iter_get_basic(iter, &success);
1554 * If scan is unsuccessful return -EIO else get the scanned BSSs
1555 * and update the network details accordingly
1557 if (success == FALSE) {
1558 if (interface->scan_callback != NULL)
1559 interface->scan_callback(-EIO, interface,
1560 interface->scan_data);
1562 interface->scan_callback = NULL;
1563 interface->scan_data = NULL;
1568 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
1569 "BSSs", scan_bss_data, interface);
1572 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1574 GSupplicantInterface *interface;
1578 interface = g_hash_table_lookup(interface_table, path);
1579 if (interface == NULL)
1582 interface_bss_added(iter, interface);
1585 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1587 GSupplicantInterface *interface;
1591 interface = g_hash_table_lookup(interface_table, path);
1592 if (interface == NULL)
1595 interface_bss_removed(iter, interface);
1598 static void signal_network_added(const char *path, DBusMessageIter *iter)
1600 GSupplicantInterface *interface;
1604 interface = g_hash_table_lookup(interface_table, path);
1605 if (interface == NULL)
1608 interface_network_added(iter, interface);
1611 static void signal_network_removed(const char *path, DBusMessageIter *iter)
1613 GSupplicantInterface *interface;
1617 interface = g_hash_table_lookup(interface_table, path);
1618 if (interface == NULL)
1621 interface_network_removed(iter, interface);
1624 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
1626 GSupplicantInterface *interface;
1627 GSupplicantNetwork *network;
1628 struct g_supplicant_bss *bss;
1632 interface = g_hash_table_lookup(bss_mapping, path);
1633 if (interface == NULL)
1636 network = g_hash_table_lookup(interface->bss_mapping, path);
1637 if (network == NULL)
1640 bss = g_hash_table_lookup(network->bss_table, path);
1644 supplicant_dbus_property_foreach(iter, bss_property, bss);
1648 const char *interface;
1650 void (*function) (const char *path, DBusMessageIter *iter);
1652 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
1654 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
1655 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
1656 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
1657 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
1659 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
1660 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
1661 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
1662 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
1663 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
1664 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
1666 { SUPPLICANT_INTERFACE ".Interface.BSS", "PropertiesChanged", signal_bss_changed },
1671 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
1672 DBusMessage *message, void *data)
1674 DBusMessageIter iter;
1678 path = dbus_message_get_path(message);
1680 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1682 if (dbus_message_iter_init(message, &iter) == FALSE)
1683 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1685 for (i = 0; signal_map[i].interface != NULL; i++) {
1686 if (dbus_message_has_interface(message,
1687 signal_map[i].interface) == FALSE)
1690 if (dbus_message_has_member(message,
1691 signal_map[i].member) == FALSE)
1694 signal_map[i].function(path, &iter);
1698 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1701 struct interface_data {
1702 GSupplicantInterface *interface;
1703 GSupplicantInterfaceCallback callback;
1707 struct interface_create_data {
1710 GSupplicantInterface *interface;
1711 GSupplicantInterfaceCallback callback;
1715 struct interface_connect_data {
1716 GSupplicantInterface *interface;
1717 GSupplicantInterfaceCallback callback;
1718 GSupplicantSSID *ssid;
1722 static void interface_create_property(const char *key, DBusMessageIter *iter,
1725 struct interface_create_data *data = user_data;
1726 GSupplicantInterface *interface = data->interface;
1729 if (data->callback != NULL)
1730 data->callback(0, data->interface, data->user_data);
1735 interface_property(key, iter, interface);
1738 static void interface_create_result(const char *error,
1739 DBusMessageIter *iter, void *user_data)
1741 struct interface_create_data *data = user_data;
1742 const char *path = NULL;
1747 if (error != NULL) {
1748 g_critical("error %s", error);
1753 dbus_message_iter_get_basic(iter, &path);
1759 if (system_available == FALSE) {
1764 data->interface = g_hash_table_lookup(interface_table, path);
1765 if (data->interface == NULL) {
1766 data->interface = interface_alloc(path);
1767 if (data->interface == NULL) {
1773 err = supplicant_dbus_property_get_all(path,
1774 SUPPLICANT_INTERFACE ".Interface",
1775 interface_create_property, data);
1780 if (data->callback != NULL)
1781 data->callback(err, NULL, data->user_data);
1786 static void interface_create_params(DBusMessageIter *iter, void *user_data)
1788 struct interface_create_data *data = user_data;
1789 DBusMessageIter dict;
1793 supplicant_dbus_dict_open(iter, &dict);
1795 supplicant_dbus_dict_append_basic(&dict, "Ifname",
1796 DBUS_TYPE_STRING, &data->ifname);
1798 if (data->driver != NULL)
1799 supplicant_dbus_dict_append_basic(&dict, "Driver",
1800 DBUS_TYPE_STRING, &data->driver);
1802 supplicant_dbus_dict_close(iter, &dict);
1805 static void interface_get_result(const char *error,
1806 DBusMessageIter *iter, void *user_data)
1808 struct interface_create_data *data = user_data;
1809 GSupplicantInterface *interface;
1810 const char *path = NULL;
1815 if (error != NULL) {
1816 g_warning("error %s", error);
1821 dbus_message_iter_get_basic(iter, &path);
1827 interface = g_hash_table_lookup(interface_table, path);
1828 if (interface == NULL) {
1833 if (data->callback != NULL)
1834 data->callback(0, interface, data->user_data);
1841 if (system_available == FALSE) {
1846 SUPPLICANT_DBG("Creating interface");
1848 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
1849 SUPPLICANT_INTERFACE,
1851 interface_create_params,
1852 interface_create_result, data);
1857 if (data->callback != NULL)
1858 data->callback(err, NULL, data->user_data);
1863 static void interface_get_params(DBusMessageIter *iter, void *user_data)
1865 struct interface_create_data *data = user_data;
1869 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
1872 int g_supplicant_interface_create(const char *ifname, const char *driver,
1873 GSupplicantInterfaceCallback callback,
1876 struct interface_create_data *data;
1878 SUPPLICANT_DBG("ifname %s", ifname);
1883 if (system_available == FALSE)
1886 data = dbus_malloc0(sizeof(*data));
1890 data->ifname = ifname;
1891 data->driver = driver;
1892 data->callback = callback;
1893 data->user_data = user_data;
1895 return supplicant_dbus_method_call(SUPPLICANT_PATH,
1896 SUPPLICANT_INTERFACE,
1898 interface_get_params,
1899 interface_get_result, data);
1902 static void interface_remove_result(const char *error,
1903 DBusMessageIter *iter, void *user_data)
1905 struct interface_data *data = user_data;
1908 if (error != NULL) {
1913 if (system_available == FALSE) {
1918 g_hash_table_remove(interface_table, data->interface->path);
1922 if (data->callback != NULL)
1923 data->callback(err, data->interface, data->user_data);
1929 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
1931 struct interface_data *data = user_data;
1933 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
1934 &data->interface->path);
1938 int g_supplicant_interface_remove(GSupplicantInterface *interface,
1939 GSupplicantInterfaceCallback callback,
1942 struct interface_data *data;
1944 if (interface == NULL)
1947 if (system_available == FALSE)
1950 data = dbus_malloc0(sizeof(*data));
1954 data->interface = interface;
1955 data->callback = callback;
1956 data->user_data = user_data;
1958 return supplicant_dbus_method_call(SUPPLICANT_PATH,
1959 SUPPLICANT_INTERFACE,
1961 interface_remove_params,
1962 interface_remove_result, data);
1965 static void interface_scan_result(const char *error,
1966 DBusMessageIter *iter, void *user_data)
1968 struct interface_data *data = user_data;
1970 if (error != NULL) {
1971 if (data->callback != NULL)
1972 data->callback(-EIO, data->interface, data->user_data);
1974 data->interface->scan_callback = data->callback;
1975 data->interface->scan_data = data->user_data;
1981 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
1983 DBusMessageIter dict;
1984 const char *type = "passive";
1986 supplicant_dbus_dict_open(iter, &dict);
1988 supplicant_dbus_dict_append_basic(&dict, "Type",
1989 DBUS_TYPE_STRING, &type);
1991 supplicant_dbus_dict_close(iter, &dict);
1994 int g_supplicant_interface_scan(GSupplicantInterface *interface,
1995 GSupplicantInterfaceCallback callback,
1998 struct interface_data *data;
2000 if (interface == NULL)
2003 if (system_available == FALSE)
2006 if (interface->scanning == TRUE)
2009 data = dbus_malloc0(sizeof(*data));
2013 data->interface = interface;
2014 data->callback = callback;
2015 data->user_data = user_data;
2017 return supplicant_dbus_method_call(interface->path,
2018 SUPPLICANT_INTERFACE ".Interface", "Scan",
2019 interface_scan_params, interface_scan_result, data);
2022 static void interface_select_network_result(const char *error,
2023 DBusMessageIter *iter, void *user_data)
2028 static void interface_select_network_params(DBusMessageIter *iter,
2031 char *path = user_data;
2033 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
2036 static void interface_add_network_result(const char *error,
2037 DBusMessageIter *iter, void *user_data)
2039 struct interface_connect_data *data = user_data;
2045 dbus_message_iter_get_basic(iter, &path);
2049 SUPPLICANT_DBG("PATH: %s", path);
2051 supplicant_dbus_method_call(data->interface->path,
2052 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
2053 interface_select_network_params,
2054 interface_select_network_result, path);
2060 static void add_network_security_wep(DBusMessageIter *dict,
2061 GSupplicantSSID *ssid)
2063 const char *auth_alg = "OPEN SHARED";
2064 const char *key_index = "0";
2066 supplicant_dbus_dict_append_basic(dict, "auth_alg",
2067 DBUS_TYPE_STRING, &auth_alg);
2069 if (ssid->passphrase) {
2070 int size = strlen(ssid->passphrase);
2071 if (size == 10 || size == 26) {
2072 unsigned char *key = g_try_malloc(13);
2076 memset(tmp, 0, sizeof(tmp));
2080 for (i = 0; i < size / 2; i++) {
2081 memcpy(tmp, ssid->passphrase + (i * 2), 2);
2082 key[i] = (unsigned char) strtol(tmp, NULL, 16);
2085 supplicant_dbus_dict_append_fixed_array(dict,
2090 } else if (size == 5 || size == 13) {
2091 unsigned char *key = g_try_malloc(13);
2097 for (i = 0; i < size; i++)
2098 key[i] = (unsigned char) ssid->passphrase[i];
2100 supplicant_dbus_dict_append_fixed_array(dict,
2106 supplicant_dbus_dict_append_basic(dict,
2111 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
2112 DBUS_TYPE_STRING, &key_index);
2116 static void add_network_security_psk(DBusMessageIter *dict,
2117 GSupplicantSSID *ssid)
2119 if (ssid->passphrase && strlen(ssid->passphrase) > 0)
2120 supplicant_dbus_dict_append_basic(dict, "psk",
2125 static void add_network_security_tls(DBusMessageIter *dict,
2126 GSupplicantSSID *ssid)
2129 * For TLS, we at least need:
2130 * The client certificate
2131 * The client private key file
2132 * The client private key file password
2134 * The Authority certificate is optional.
2136 if (ssid->client_cert_path == NULL)
2139 if (ssid->private_key_path == NULL)
2142 if (ssid->private_key_passphrase == NULL)
2145 if (ssid->ca_cert_path)
2146 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2147 DBUS_TYPE_STRING, &ssid->ca_cert_path);
2149 supplicant_dbus_dict_append_basic(dict, "private_key",
2151 &ssid->private_key_path);
2152 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2154 &ssid->private_key_passphrase);
2155 supplicant_dbus_dict_append_basic(dict, "client_cert",
2157 &ssid->client_cert_path);
2160 static void add_network_security_peap(DBusMessageIter *dict,
2161 GSupplicantSSID *ssid)
2166 * For PEAP/TTLS, we at least need
2167 * The authority certificate
2168 * The 2nd phase authentication method
2169 * The 2nd phase passphrase
2171 * The Client certificate is optional although strongly required
2172 * When setting it, we need in addition
2173 * The Client private key file
2174 * The Client private key file password
2176 if (ssid->passphrase == NULL)
2179 if (ssid->ca_cert_path == NULL)
2182 if (ssid->phase2_auth == NULL)
2185 if (ssid->client_cert_path) {
2186 if (ssid->private_key_path == NULL)
2189 if (ssid->private_key_passphrase == NULL)
2192 supplicant_dbus_dict_append_basic(dict, "client_cert",
2194 &ssid->client_cert_path);
2196 supplicant_dbus_dict_append_basic(dict, "private_key",
2198 &ssid->private_key_path);
2200 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
2202 &ssid->private_key_passphrase);
2206 phase2_auth = g_strdup_printf("\"auth=%s\"", ssid->phase2_auth);
2208 supplicant_dbus_dict_append_basic(dict, "password",
2212 supplicant_dbus_dict_append_basic(dict, "ca_cert",
2214 &ssid->ca_cert_path);
2216 supplicant_dbus_dict_append_basic(dict, "phase2",
2218 &ssid->phase2_auth);
2220 g_free(phase2_auth);
2223 static void add_network_security_eap(DBusMessageIter *dict,
2224 GSupplicantSSID *ssid)
2228 if (ssid->eap == NULL || ssid->identity == NULL)
2231 if (g_strcmp0(ssid->eap, "tls") == 0) {
2232 add_network_security_tls(dict, ssid);
2233 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
2234 g_strcmp0(ssid->eap, "ttls") == 0) {
2235 add_network_security_peap(dict, ssid);
2239 eap_value = g_ascii_strup(ssid->eap, -1);
2241 supplicant_dbus_dict_append_basic(dict, "eap",
2244 supplicant_dbus_dict_append_basic(dict, "identity",
2251 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
2255 switch (ssid->security) {
2256 case G_SUPPLICANT_SECURITY_UNKNOWN:
2257 case G_SUPPLICANT_SECURITY_NONE:
2258 case G_SUPPLICANT_SECURITY_WEP:
2260 add_network_security_wep(dict, ssid);
2262 case G_SUPPLICANT_SECURITY_PSK:
2263 key_mgmt = "WPA-PSK";
2264 add_network_security_psk(dict, ssid);
2266 case G_SUPPLICANT_SECURITY_IEEE8021X:
2267 key_mgmt = "WPA-EAP";
2268 add_network_security_eap(dict, ssid);
2272 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
2273 DBUS_TYPE_STRING, &key_mgmt);
2276 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
2278 DBusMessageIter dict;
2279 dbus_uint32_t scan_ssid = 1;
2280 struct interface_connect_data *data = user_data;
2281 GSupplicantSSID *ssid = data->ssid;
2283 supplicant_dbus_dict_open(iter, &dict);
2285 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
2286 DBUS_TYPE_UINT32, &scan_ssid);
2288 add_network_security(&dict, ssid);
2290 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
2291 DBUS_TYPE_BYTE, &ssid->ssid,
2294 supplicant_dbus_dict_close(iter, &dict);
2297 int g_supplicant_interface_connect(GSupplicantInterface *interface,
2298 GSupplicantSSID *ssid,
2299 GSupplicantInterfaceCallback callback,
2302 struct interface_connect_data *data;
2305 if (interface == NULL)
2308 if (system_available == FALSE)
2311 /* TODO: Check if we're already connected and switch */
2313 data = dbus_malloc0(sizeof(*data));
2317 data->interface = interface;
2318 data->callback = callback;
2320 data->user_data = user_data;
2322 ret = supplicant_dbus_method_call(interface->path,
2323 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
2324 interface_add_network_params,
2325 interface_add_network_result, data);
2329 return -EINPROGRESS;
2332 static void interface_disconnect_result(const char *error,
2333 DBusMessageIter *iter, void *user_data)
2335 struct interface_data *data = user_data;
2343 if (data->callback != NULL)
2344 data->callback(result, data->interface, data->user_data);
2349 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
2350 GSupplicantInterfaceCallback callback,
2353 struct interface_data *data;
2357 if (interface == NULL)
2360 if (system_available == FALSE)
2363 data = dbus_malloc0(sizeof(*data));
2367 data->interface = interface;
2368 data->callback = callback;
2369 data->user_data = user_data;
2371 return supplicant_dbus_method_call(interface->path,
2372 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
2373 NULL, interface_disconnect_result, data);
2377 static const char *g_supplicant_rule0 = "type=signal,"
2378 "path=" DBUS_PATH_DBUS ","
2379 "sender=" DBUS_SERVICE_DBUS ","
2380 "interface=" DBUS_INTERFACE_DBUS ","
2381 "member=NameOwnerChanged,"
2382 "arg0=" SUPPLICANT_SERVICE;
2383 static const char *g_supplicant_rule1 = "type=signal,"
2384 "interface=" SUPPLICANT_INTERFACE;
2385 static const char *g_supplicant_rule2 = "type=signal,"
2386 "interface=" SUPPLICANT_INTERFACE ".Interface";
2387 static const char *g_supplicant_rule3 = "type=signal,"
2388 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
2389 static const char *g_supplicant_rule4 = "type=signal,"
2390 "interface=" SUPPLICANT_INTERFACE ".Interface.BSS";
2391 static const char *g_supplicant_rule5 = "type=signal,"
2392 "interface=" SUPPLICANT_INTERFACE ".Interface.Network";
2393 static const char *g_supplicant_rule6 = "type=signal,"
2394 "interface=" SUPPLICANT_INTERFACE ".Interface.Blob";
2396 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
2398 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
2399 if (connection == NULL)
2402 if (dbus_connection_add_filter(connection,
2403 g_supplicant_filter, NULL, NULL) == FALSE) {
2404 dbus_connection_unref(connection);
2409 callbacks_pointer = callbacks;
2412 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2413 NULL, remove_interface);
2415 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2418 supplicant_dbus_setup(connection);
2420 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
2421 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
2422 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
2423 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
2424 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
2425 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
2426 dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
2427 dbus_connection_flush(connection);
2429 if (dbus_bus_name_has_owner(connection,
2430 SUPPLICANT_SERVICE, NULL) == TRUE) {
2431 system_available = TRUE;
2432 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
2433 SUPPLICANT_INTERFACE,
2434 service_property, NULL);
2440 static void unregister_interface_remove_params(DBusMessageIter *iter,
2443 const char *path = user_data;
2445 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2450 static void unregister_remove_interface(gpointer key, gpointer value,
2453 GSupplicantInterface *interface = value;
2455 supplicant_dbus_method_call(SUPPLICANT_PATH,
2456 SUPPLICANT_INTERFACE,
2458 unregister_interface_remove_params,
2459 NULL, interface->path);
2462 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
2466 if (connection != NULL) {
2467 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
2468 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
2469 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
2470 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
2471 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
2472 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
2473 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
2474 dbus_connection_flush(connection);
2476 dbus_connection_remove_filter(connection,
2477 g_supplicant_filter, NULL);
2480 if (bss_mapping != NULL) {
2481 g_hash_table_destroy(bss_mapping);
2485 if (interface_table != NULL) {
2486 g_hash_table_foreach(interface_table,
2487 unregister_remove_interface, NULL);
2488 g_hash_table_destroy(interface_table);
2489 interface_table = NULL;
2492 if (system_available == TRUE)
2493 callback_system_killed();
2495 if (connection != NULL) {
2496 dbus_connection_unref(connection);
2500 callbacks_pointer = NULL;