5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "supplicant-dbus.h"
35 #include "supplicant.h"
37 #define DBG(fmt, arg...) do { \
38 syslog(LOG_DEBUG, "%s() " fmt, __FUNCTION__ , ## arg); \
43 #define IEEE80211_CAP_ESS 0x0001
44 #define IEEE80211_CAP_IBSS 0x0002
45 #define IEEE80211_CAP_PRIVACY 0x0010
47 static DBusConnection *connection;
49 static const struct supplicant_callbacks *callbacks_pointer;
51 static dbus_bool_t system_available = FALSE;
52 static dbus_bool_t system_ready = FALSE;
54 static dbus_int32_t debug_level = 0;
55 static dbus_bool_t debug_timestamp = FALSE;
56 static dbus_bool_t debug_showkeys = FALSE;
58 static const char *debug_strings[] = {
59 "msgdump", "debug", "info", "warning", "error", NULL
62 static unsigned int eap_methods;
69 static struct strvalmap eap_method_map[] = {
70 { "MD5", SUPPLICANT_EAP_METHOD_MD5 },
71 { "TLS", SUPPLICANT_EAP_METHOD_TLS },
72 { "MSCHAPV2", SUPPLICANT_EAP_METHOD_MSCHAPV2 },
73 { "PEAP", SUPPLICANT_EAP_METHOD_PEAP },
74 { "TTLS", SUPPLICANT_EAP_METHOD_TTLS },
75 { "GTC", SUPPLICANT_EAP_METHOD_GTC },
76 { "OTP", SUPPLICANT_EAP_METHOD_OTP },
77 { "LEAP", SUPPLICANT_EAP_METHOD_LEAP },
78 { "WSC", SUPPLICANT_EAP_METHOD_WSC },
82 static struct strvalmap keymgmt_capa_map[] = {
83 { "none", SUPPLICANT_CAPABILITY_KEYMGMT_NONE },
84 { "ieee8021x", SUPPLICANT_CAPABILITY_KEYMGMT_IEEE8021X },
85 { "wpa-none", SUPPLICANT_CAPABILITY_KEYMGMT_WPA_NONE },
86 { "wpa-psk", SUPPLICANT_CAPABILITY_KEYMGMT_WPA_PSK },
87 { "wpa-eap", SUPPLICANT_CAPABILITY_KEYMGMT_WPA_EAP },
88 { "wps", SUPPLICANT_CAPABILITY_KEYMGMT_WPS },
92 static struct strvalmap authalg_capa_map[] = {
93 { "open", SUPPLICANT_CAPABILITY_AUTHALG_OPEN },
94 { "shared", SUPPLICANT_CAPABILITY_AUTHALG_SHARED },
95 { "leap", SUPPLICANT_CAPABILITY_AUTHALG_LEAP },
99 static struct strvalmap proto_capa_map[] = {
100 { "wpa", SUPPLICANT_CAPABILITY_PROTO_WPA },
101 { "rsn", SUPPLICANT_CAPABILITY_PROTO_RSN },
105 static struct strvalmap group_capa_map[] = {
106 { "wep40", SUPPLICANT_CAPABILITY_GROUP_WEP40 },
107 { "wep104", SUPPLICANT_CAPABILITY_GROUP_WEP104 },
108 { "tkip", SUPPLICANT_CAPABILITY_GROUP_TKIP },
109 { "ccmp", SUPPLICANT_CAPABILITY_GROUP_CCMP },
113 static struct strvalmap pairwise_capa_map[] = {
114 { "none", SUPPLICANT_CAPABILITY_PAIRWISE_NONE },
115 { "tkip", SUPPLICANT_CAPABILITY_PAIRWISE_TKIP },
116 { "ccmp", SUPPLICANT_CAPABILITY_PAIRWISE_CCMP },
120 static struct strvalmap scan_capa_map[] = {
121 { "active", SUPPLICANT_CAPABILITY_SCAN_ACTIVE },
122 { "passive", SUPPLICANT_CAPABILITY_SCAN_PASSIVE },
123 { "ssid", SUPPLICANT_CAPABILITY_SCAN_SSID },
127 static struct strvalmap mode_capa_map[] = {
128 { "infrastructure", SUPPLICANT_CAPABILITY_MODE_INFRA },
129 { "ad-hoc", SUPPLICANT_CAPABILITY_MODE_IBSS },
130 { "ap", SUPPLICANT_CAPABILITY_MODE_AP },
134 static GHashTable *interface_table;
135 static GHashTable *bss_mapping;
137 struct supplicant_interface {
139 unsigned int keymgmt_capa;
140 unsigned int authalg_capa;
141 unsigned int proto_capa;
142 unsigned int group_capa;
143 unsigned int pairwise_capa;
144 unsigned int scan_capa;
145 unsigned int mode_capa;
147 enum supplicant_state state;
148 dbus_bool_t scanning;
149 supplicant_interface_scan_callback scan_callback;
155 GHashTable *network_table;
156 GHashTable *net_mapping;
157 GHashTable *bss_mapping;
160 struct supplicant_network {
161 struct supplicant_interface *interface;
165 enum supplicant_mode mode;
166 GHashTable *bss_table;
167 GHashTable *config_table;
170 struct supplicant_bss {
171 struct supplicant_interface *interface;
173 unsigned char bssid[6];
174 unsigned char ssid[32];
175 unsigned int ssid_len;
176 dbus_uint16_t frequency;
177 dbus_uint32_t maxrate;
178 enum supplicant_mode mode;
179 enum supplicant_security security;
182 dbus_bool_t ieee8021x;
185 static enum supplicant_mode string2mode(const char *mode)
188 return SUPPLICANT_MODE_UNKNOWN;
190 if (g_str_equal(mode, "infrastructure"))
191 return SUPPLICANT_MODE_INFRA;
192 else if (g_str_equal(mode, "ad-hoc"))
193 return SUPPLICANT_MODE_IBSS;
195 return SUPPLICANT_MODE_UNKNOWN;
198 static const char *mode2string(enum supplicant_mode mode)
201 case SUPPLICANT_MODE_UNKNOWN:
203 case SUPPLICANT_MODE_INFRA:
205 case SUPPLICANT_MODE_IBSS:
212 static const char *security2string(enum supplicant_security security)
215 case SUPPLICANT_SECURITY_UNKNOWN:
217 case SUPPLICANT_SECURITY_NONE:
219 case SUPPLICANT_SECURITY_WEP:
221 case SUPPLICANT_SECURITY_PSK:
223 case SUPPLICANT_SECURITY_IEEE8021X:
230 static enum supplicant_state string2state(const char *state)
233 return SUPPLICANT_STATE_UNKNOWN;
235 if (g_str_equal(state, "unknown"))
236 return SUPPLICANT_STATE_UNKNOWN;
237 else if (g_str_equal(state, "disconnected"))
238 return SUPPLICANT_STATE_DISCONNECTED;
239 else if (g_str_equal(state, "inactive"))
240 return SUPPLICANT_STATE_INACTIVE;
241 else if (g_str_equal(state, "scanning"))
242 return SUPPLICANT_STATE_SCANNING;
243 else if (g_str_equal(state, "authenticating"))
244 return SUPPLICANT_STATE_AUTHENTICATING;
245 else if (g_str_equal(state, "associating"))
246 return SUPPLICANT_STATE_ASSOCIATING;
247 else if (g_str_equal(state, "associated"))
248 return SUPPLICANT_STATE_ASSOCIATED;
249 else if (g_str_equal(state, "group_handshake"))
250 return SUPPLICANT_STATE_GROUP_HANDSHAKE;
251 else if (g_str_equal(state, "4way_handshake"))
252 return SUPPLICANT_STATE_4WAY_HANDSHAKE;
253 else if (g_str_equal(state, "completed"))
254 return SUPPLICANT_STATE_COMPLETED;
256 return SUPPLICANT_STATE_UNKNOWN;
259 static void callback_system_ready(void)
266 if (!callbacks_pointer)
269 if (!callbacks_pointer->system_ready)
272 callbacks_pointer->system_ready();
275 static void callback_system_killed(void)
277 system_ready = FALSE;
279 if (!callbacks_pointer)
282 if (!callbacks_pointer->system_killed)
285 callbacks_pointer->system_killed();
288 static void callback_interface_added(struct supplicant_interface *interface)
290 if (!callbacks_pointer)
293 if (!callbacks_pointer->interface_added)
296 callbacks_pointer->interface_added(interface);
299 static void callback_interface_removed(struct supplicant_interface *interface)
301 if (!callbacks_pointer)
304 if (!callbacks_pointer->interface_removed)
307 callbacks_pointer->interface_removed(interface);
310 static void callback_scan_started(struct supplicant_interface *interface)
312 if (!callbacks_pointer)
315 if (!callbacks_pointer->scan_started)
318 callbacks_pointer->scan_started(interface);
321 static void callback_scan_finished(struct supplicant_interface *interface)
323 if (!callbacks_pointer)
326 if (!callbacks_pointer->scan_finished)
329 callbacks_pointer->scan_finished(interface);
332 static void callback_network_added(struct supplicant_network *network)
334 if (!callbacks_pointer)
337 if (!callbacks_pointer->network_added)
340 callbacks_pointer->network_added(network);
343 static void callback_network_removed(struct supplicant_network *network)
345 if (!callbacks_pointer)
348 if (!callbacks_pointer->network_removed)
351 callbacks_pointer->network_removed(network);
354 static void remove_interface(gpointer data)
356 struct supplicant_interface *interface = data;
358 g_hash_table_destroy(interface->bss_mapping);
359 g_hash_table_destroy(interface->net_mapping);
360 g_hash_table_destroy(interface->network_table);
362 callback_interface_removed(interface);
364 g_free(interface->path);
365 g_free(interface->ifname);
366 g_free(interface->driver);
367 g_free(interface->bridge);
371 static void remove_network(gpointer data)
373 struct supplicant_network *network = data;
375 g_hash_table_destroy(network->bss_table);
377 callback_network_removed(network);
379 g_hash_table_destroy(network->config_table);
381 g_free(network->group);
382 g_free(network->name);
386 static void remove_bss(gpointer data)
388 struct supplicant_bss *bss = data;
394 static void debug_strvalmap(const char *label, struct strvalmap *map,
399 for (i = 0; map[i].str; i++) {
400 if (val & map[i].val)
401 DBG("%s: %s", label, map[i].str);
405 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
407 struct supplicant_interface *interface = user_data;
408 const char *str = NULL;
411 dbus_message_iter_get_basic(iter, &str);
415 for (i = 0; keymgmt_capa_map[i].str; i++)
416 if (strcmp(str, keymgmt_capa_map[i].str) == 0) {
417 interface->keymgmt_capa |= keymgmt_capa_map[i].val;
422 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
424 struct supplicant_interface *interface = user_data;
425 const char *str = NULL;
428 dbus_message_iter_get_basic(iter, &str);
432 for (i = 0; authalg_capa_map[i].str; i++)
433 if (strcmp(str, authalg_capa_map[i].str) == 0) {
434 interface->authalg_capa |= authalg_capa_map[i].val;
439 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
441 struct supplicant_interface *interface = user_data;
442 const char *str = NULL;
445 dbus_message_iter_get_basic(iter, &str);
449 for (i = 0; proto_capa_map[i].str; i++)
450 if (strcmp(str, proto_capa_map[i].str) == 0) {
451 interface->proto_capa |= proto_capa_map[i].val;
456 static void interface_capability_pairwise(DBusMessageIter *iter, void *user_data)
458 struct supplicant_interface *interface = user_data;
459 const char *str = NULL;
462 dbus_message_iter_get_basic(iter, &str);
466 for (i = 0; pairwise_capa_map[i].str; i++)
467 if (strcmp(str, pairwise_capa_map[i].str) == 0) {
468 interface->pairwise_capa |= pairwise_capa_map[i].val;
473 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
475 struct supplicant_interface *interface = user_data;
476 const char *str = NULL;
479 dbus_message_iter_get_basic(iter, &str);
483 for (i = 0; group_capa_map[i].str; i++)
484 if (strcmp(str, group_capa_map[i].str) == 0) {
485 interface->group_capa |= group_capa_map[i].val;
490 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
492 struct supplicant_interface *interface = user_data;
493 const char *str = NULL;
496 dbus_message_iter_get_basic(iter, &str);
500 for (i = 0; scan_capa_map[i].str; i++)
501 if (strcmp(str, scan_capa_map[i].str) == 0) {
502 interface->scan_capa |= scan_capa_map[i].val;
507 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
509 struct supplicant_interface *interface = user_data;
510 const char *str = NULL;
513 dbus_message_iter_get_basic(iter, &str);
517 for (i = 0; mode_capa_map[i].str; i++)
518 if (strcmp(str, mode_capa_map[i].str) == 0) {
519 interface->mode_capa |= mode_capa_map[i].val;
524 static void interface_capability(const char *key, DBusMessageIter *iter,
527 struct supplicant_interface *interface = user_data;
532 if (g_strcmp0(key, "KeyMgmt") == 0)
533 supplicant_dbus_array_foreach(iter,
534 interface_capability_keymgmt, interface);
535 else if (g_strcmp0(key, "AuthAlg") == 0)
536 supplicant_dbus_array_foreach(iter,
537 interface_capability_authalg, interface);
538 else if (g_strcmp0(key, "Protocol") == 0)
539 supplicant_dbus_array_foreach(iter,
540 interface_capability_proto, interface);
541 else if (g_strcmp0(key, "Pairwise") == 0)
542 supplicant_dbus_array_foreach(iter,
543 interface_capability_pairwise, interface);
544 else if (g_strcmp0(key, "Group") == 0)
545 supplicant_dbus_array_foreach(iter,
546 interface_capability_group, interface);
547 else if (g_strcmp0(key, "Scan") == 0)
548 supplicant_dbus_array_foreach(iter,
549 interface_capability_scan, interface);
550 else if (g_strcmp0(key, "Modes") == 0)
551 supplicant_dbus_array_foreach(iter,
552 interface_capability_mode, interface);
554 DBG("key %s type %c",
555 key, dbus_message_iter_get_arg_type(iter));
558 const char *supplicant_interface_get_ifname(struct supplicant_interface *interface)
563 return interface->ifname;
566 const char *supplicant_interface_get_driver(struct supplicant_interface *interface)
571 return interface->driver;
574 struct supplicant_interface *supplicant_network_get_interface(struct supplicant_network *network)
579 return network->interface;
582 const char *supplicant_network_get_name(struct supplicant_network *network)
584 if (!network || !network->name)
587 return network->name;
590 const char *supplicant_network_get_identifier(struct supplicant_network *network)
592 if (!network || !network->group)
595 return network->group;
598 enum supplicant_mode supplicant_network_get_mode(struct supplicant_network *network)
601 return SUPPLICANT_MODE_UNKNOWN;
603 return network->mode;
606 static void merge_network(struct supplicant_network *network)
609 const char *ssid, *mode, *key_mgmt;
610 unsigned int i, ssid_len;
613 ssid = g_hash_table_lookup(network->config_table, "ssid");
614 mode = g_hash_table_lookup(network->config_table, "mode");
615 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
617 DBG("ssid %s mode %s", ssid, mode);
620 ssid_len = strlen(ssid);
624 str = g_string_sized_new((ssid_len * 2) + 24);
628 for (i = 0; i < ssid_len; i++)
629 g_string_append_printf(str, "%02x", ssid[i]);
631 if (g_strcmp0(mode, "0") == 0)
632 g_string_append_printf(str, "_infra");
633 else if (g_strcmp0(mode, "1") == 0)
634 g_string_append_printf(str, "_adhoc");
636 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
637 g_string_append_printf(str, "_psk");
639 group = g_string_free(str, FALSE);
645 g_hash_table_destroy(network->config_table);
647 g_free(network->path);
651 static void network_property(const char *key, DBusMessageIter *iter,
654 struct supplicant_network *network = user_data;
656 if (!network->interface)
660 merge_network(network);
664 if (g_strcmp0(key, "Enabled") == 0) {
665 dbus_bool_t enabled = FALSE;
667 dbus_message_iter_get_basic(iter, &enabled);
668 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
669 const char *str = NULL;
671 dbus_message_iter_get_basic(iter, &str);
673 g_hash_table_replace(network->config_table,
674 g_strdup(key), g_strdup(str));
676 DBG("key %s type %c",
677 key, dbus_message_iter_get_arg_type(iter));
680 static void interface_network_added(DBusMessageIter *iter, void *user_data)
682 struct supplicant_interface *interface = user_data;
683 struct supplicant_network *network;
684 const char *path = NULL;
686 dbus_message_iter_get_basic(iter, &path);
690 if (g_strcmp0(path, "/") == 0)
693 network = g_hash_table_lookup(interface->net_mapping, path);
697 network = g_try_new0(struct supplicant_network, 1);
701 network->interface = interface;
702 network->path = g_strdup(path);
704 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
707 dbus_message_iter_next(iter);
708 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
709 supplicant_dbus_property_foreach(iter, network_property,
711 network_property(NULL, NULL, network);
715 supplicant_dbus_property_get_all(path,
716 SUPPLICANT_INTERFACE ".Interface.Network",
717 network_property, network);
720 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
722 struct supplicant_interface *interface = user_data;
723 struct supplicant_network *network;
724 const char *path = NULL;
726 dbus_message_iter_get_basic(iter, &path);
730 network = g_hash_table_lookup(interface->net_mapping, path);
734 g_hash_table_remove(interface->net_mapping, path);
737 static char *create_name(unsigned char *ssid, int ssid_len)
742 if (ssid_len < 1 || ssid[0] == '\0')
745 name = g_try_malloc0(ssid_len + 1);
750 for (i = 0; i < ssid_len; i++) {
751 if (g_ascii_isprint(ssid[i]))
760 static char *create_group(struct supplicant_bss *bss)
764 const char *mode, *security;
766 str = g_string_sized_new((bss->ssid_len * 2) + 24);
770 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
771 for (i = 0; i < bss->ssid_len; i++)
772 g_string_append_printf(str, "%02x", bss->ssid[i]);
774 g_string_append_printf(str, "hidden");
776 mode = mode2string(bss->mode);
778 g_string_append_printf(str, "_%s", mode);
780 security = security2string(bss->security);
782 g_string_append_printf(str, "_%s", security);
784 return g_string_free(str, FALSE);
787 static void add_bss_to_network(struct supplicant_bss *bss)
789 struct supplicant_interface *interface = bss->interface;
790 struct supplicant_network *network;
793 group = create_group(bss);
797 network = g_hash_table_lookup(interface->network_table, group);
803 network = g_try_new0(struct supplicant_network, 1);
809 network->group = group;
810 network->name = create_name(bss->ssid, bss->ssid_len);
811 network->mode = bss->mode;
813 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
816 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
819 g_hash_table_replace(interface->network_table,
820 network->group, network);
822 callback_network_added(network);
825 g_hash_table_replace(interface->bss_mapping, bss->path, network);
826 g_hash_table_replace(network->bss_table, bss->path, bss);
828 g_hash_table_replace(bss_mapping, bss->path, interface);
831 static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
832 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
834 static void extract_rsn(struct supplicant_bss *bss,
835 const unsigned char *buf, int len)
854 /* Pairwise cipher */
858 count = buf[0] | (buf[1] << 8);
859 if (2 + (count * 4) > len)
862 buf += 2 + (count * 4);
863 len -= 2 + (count * 4);
869 count = buf[0] | (buf[1] << 8);
870 if (2 + (count * 4) > len)
873 for (i = 0; i < count; i++) {
874 const unsigned char *ptr = buf + 2 + (i * 4);
876 if (memcmp(ptr, wifi_oui, 3) == 0) {
879 bss->ieee8021x = TRUE;
885 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
888 bss->ieee8021x = TRUE;
899 static void bss_rates(DBusMessageIter *iter, void *user_data)
901 struct supplicant_bss *bss = user_data;
902 dbus_uint32_t rate = 0;
904 dbus_message_iter_get_basic(iter, &rate);
908 if (rate > bss->maxrate)
912 static void bss_property(const char *key, DBusMessageIter *iter,
915 struct supplicant_bss *bss = user_data;
922 bss->security = SUPPLICANT_SECURITY_IEEE8021X;
924 bss->security = SUPPLICANT_SECURITY_PSK;
925 else if (bss->privacy)
926 bss->security = SUPPLICANT_SECURITY_WEP;
928 bss->security = SUPPLICANT_SECURITY_NONE;
930 add_bss_to_network(bss);
934 if (g_strcmp0(key, "BSSID") == 0) {
935 DBusMessageIter array;
939 dbus_message_iter_recurse(iter, &array);
940 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
943 memcpy(bss->bssid, addr, addr_len);
944 } else if (g_strcmp0(key, "SSID") == 0) {
945 DBusMessageIter array;
949 dbus_message_iter_recurse(iter, &array);
950 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
952 if (ssid_len > 0 && ssid_len < 33) {
953 memcpy(bss->ssid, ssid, ssid_len);
954 bss->ssid_len = ssid_len;
956 memset(bss->ssid, 0, sizeof(bss->ssid));
959 } else if (g_strcmp0(key, "Capabilities") == 0) {
960 dbus_uint16_t capabilities = 0x0000;
962 dbus_message_iter_get_basic(iter, &capabilities);
964 if (capabilities & IEEE80211_CAP_ESS)
965 bss->mode = SUPPLICANT_MODE_INFRA;
966 else if (capabilities & IEEE80211_CAP_IBSS)
967 bss->mode = SUPPLICANT_MODE_IBSS;
969 if (capabilities & IEEE80211_CAP_PRIVACY)
971 } else if (g_strcmp0(key, "Mode") == 0) {
972 const char *mode = NULL;
974 dbus_message_iter_get_basic(iter, &mode);
975 bss->mode = string2mode(mode);
976 } else if (g_strcmp0(key, "Frequency") == 0) {
977 dbus_uint16_t frequency = 0;
979 dbus_message_iter_get_basic(iter, &frequency);
980 bss->frequency = frequency;
981 } else if (g_strcmp0(key, "Signal") == 0) {
982 dbus_int16_t signal = 0;
984 dbus_message_iter_get_basic(iter, &signal);
985 } else if (g_strcmp0(key, "Level") == 0) {
986 dbus_int32_t level = 0;
988 dbus_message_iter_get_basic(iter, &level);
989 } else if (g_strcmp0(key, "Rates") == 0) {
990 supplicant_dbus_array_foreach(iter, bss_rates, bss);
991 } else if (g_strcmp0(key, "MaxRate") == 0) {
992 dbus_uint32_t maxrate = 0;
994 dbus_message_iter_get_basic(iter, &maxrate);
996 bss->maxrate =maxrate;
997 } else if (g_strcmp0(key, "Privacy") == 0) {
998 dbus_bool_t privacy = FALSE;
1000 dbus_message_iter_get_basic(iter, &privacy);
1001 bss->privacy = privacy;
1002 } else if (g_strcmp0(key, "RSNIE") == 0) {
1003 DBusMessageIter array;
1007 dbus_message_iter_recurse(iter, &array);
1008 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1011 extract_rsn(bss, ie + 2, ie_len - 2);
1012 } else if (g_strcmp0(key, "WPAIE") == 0) {
1013 DBusMessageIter array;
1017 dbus_message_iter_recurse(iter, &array);
1018 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1021 extract_rsn(bss, ie + 6, ie_len - 6);
1022 } else if (g_strcmp0(key, "WPSIE") == 0) {
1023 DBusMessageIter array;
1027 dbus_message_iter_recurse(iter, &array);
1028 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1030 DBG("key %s type %c",
1031 key, dbus_message_iter_get_arg_type(iter));
1034 static void interface_bss_added(DBusMessageIter *iter, void *user_data)
1036 struct supplicant_interface *interface = user_data;
1037 struct supplicant_network *network;
1038 struct supplicant_bss *bss;
1039 const char *path = NULL;
1041 dbus_message_iter_get_basic(iter, &path);
1045 if (g_strcmp0(path, "/") == 0)
1048 network = g_hash_table_lookup(interface->bss_mapping, path);
1050 bss = g_hash_table_lookup(network->bss_table, path);
1055 bss = g_try_new0(struct supplicant_bss, 1);
1059 bss->interface = interface;
1060 bss->path = g_strdup(path);
1062 dbus_message_iter_next(iter);
1063 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1064 supplicant_dbus_property_foreach(iter, bss_property, bss);
1065 bss_property(NULL, NULL, bss);
1069 supplicant_dbus_property_get_all(path,
1070 SUPPLICANT_INTERFACE ".Interface.BSS",
1074 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1076 struct supplicant_interface *interface = user_data;
1077 struct supplicant_network *network;
1078 const char *path = NULL;
1080 dbus_message_iter_get_basic(iter, &path);
1084 network = g_hash_table_lookup(interface->bss_mapping, path);
1088 g_hash_table_remove(bss_mapping, path);
1090 g_hash_table_remove(interface->bss_mapping, path);
1091 g_hash_table_remove(network->bss_table, path);
1093 if (g_hash_table_size(network->bss_table) == 0)
1094 g_hash_table_remove(interface->network_table, network->group);
1097 static void interface_property(const char *key, DBusMessageIter *iter,
1100 struct supplicant_interface *interface = user_data;
1106 debug_strvalmap("KeyMgmt capability", keymgmt_capa_map,
1107 interface->keymgmt_capa);
1108 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1109 interface->authalg_capa);
1110 debug_strvalmap("Protocol capability", proto_capa_map,
1111 interface->proto_capa);
1112 debug_strvalmap("Pairwise capability", pairwise_capa_map,
1113 interface->pairwise_capa);
1114 debug_strvalmap("Group capability", group_capa_map,
1115 interface->group_capa);
1116 debug_strvalmap("Scan capability", scan_capa_map,
1117 interface->scan_capa);
1118 debug_strvalmap("Mode capability", mode_capa_map,
1119 interface->mode_capa);
1121 interface->ready = TRUE;
1122 callback_interface_added(interface);
1126 if (g_strcmp0(key, "Capabilities") == 0) {
1127 supplicant_dbus_property_foreach(iter, interface_capability,
1129 } else if (g_strcmp0(key, "State") == 0) {
1130 const char *str = NULL;
1132 dbus_message_iter_get_basic(iter, &str);
1134 interface->state = string2state(str);
1136 DBG("state %s (%d)", str, interface->state);
1137 } else if (g_strcmp0(key, "Scanning") == 0) {
1138 dbus_bool_t scanning = FALSE;
1140 dbus_message_iter_get_basic(iter, &scanning);
1141 interface->scanning = scanning;
1143 if (interface->ready) {
1144 if (interface->scanning)
1145 callback_scan_started(interface);
1147 callback_scan_finished(interface);
1149 } else if (g_strcmp0(key, "ApScan") == 0) {
1152 dbus_message_iter_get_basic(iter, &apscan);
1153 interface->apscan = apscan;
1154 } else if (g_strcmp0(key, "Ifname") == 0) {
1155 const char *str = NULL;
1157 dbus_message_iter_get_basic(iter, &str);
1159 interface->ifname = g_strdup(str);
1160 } else if (g_strcmp0(key, "Driver") == 0) {
1161 const char *str = NULL;
1163 dbus_message_iter_get_basic(iter, &str);
1165 interface->driver = g_strdup(str);
1166 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1167 const char *str = NULL;
1169 dbus_message_iter_get_basic(iter, &str);
1171 interface->bridge = g_strdup(str);
1172 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1173 interface_bss_added(iter, interface);
1174 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1175 interface_network_added(iter, interface);
1176 } else if (g_strcmp0(key, "BSSs") == 0) {
1177 supplicant_dbus_array_foreach(iter, interface_bss_added,
1179 } else if (g_strcmp0(key, "Blobs") == 0) {
1180 } else if (g_strcmp0(key, "Networks") == 0) {
1181 supplicant_dbus_array_foreach(iter, interface_network_added,
1184 DBG("key %s type %c",
1185 key, dbus_message_iter_get_arg_type(iter));
1188 static struct supplicant_interface *interface_alloc(const char *path)
1190 struct supplicant_interface *interface;
1192 interface = g_try_new0(struct supplicant_interface, 1);
1196 interface->path = g_strdup(path);
1198 interface->network_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1199 NULL, remove_network);
1201 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1203 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1206 g_hash_table_replace(interface_table, interface->path, interface);
1211 static void interface_added(DBusMessageIter *iter, void *user_data)
1213 struct supplicant_interface *interface;
1214 const char *path = NULL;
1216 dbus_message_iter_get_basic(iter, &path);
1220 if (g_strcmp0(path, "/") == 0)
1223 interface = g_hash_table_lookup(interface_table, path);
1227 interface = interface_alloc(path);
1231 dbus_message_iter_next(iter);
1232 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1233 supplicant_dbus_property_foreach(iter, interface_property,
1235 interface_property(NULL, NULL, interface);
1239 supplicant_dbus_property_get_all(path,
1240 SUPPLICANT_INTERFACE ".Interface",
1241 interface_property, interface);
1244 static void interface_removed(DBusMessageIter *iter, void *user_data)
1246 const char *path = NULL;
1248 dbus_message_iter_get_basic(iter, &path);
1252 g_hash_table_remove(interface_table, path);
1255 static void eap_method(DBusMessageIter *iter, void *user_data)
1257 const char *str = NULL;
1260 dbus_message_iter_get_basic(iter, &str);
1264 for (i = 0; eap_method_map[i].str; i++)
1265 if (strcmp(str, eap_method_map[i].str) == 0) {
1266 eap_methods |= eap_method_map[i].val;
1271 static void service_property(const char *key, DBusMessageIter *iter,
1275 callback_system_ready();
1279 if (g_strcmp0(key, "DebugLevel") == 0) {
1280 const char *str = NULL;
1283 dbus_message_iter_get_basic(iter, &str);
1284 for (i = 0; debug_strings[i]; i++)
1285 if (g_strcmp0(debug_strings[i], str) == 0) {
1289 DBG("Debug level %d", debug_level);
1290 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1291 dbus_message_iter_get_basic(iter, &debug_timestamp);
1292 DBG("Debug timestamp %u", debug_timestamp);
1293 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1294 dbus_message_iter_get_basic(iter, &debug_showkeys);
1295 DBG("Debug show keys %u", debug_showkeys);
1296 } else if (g_strcmp0(key, "Interfaces") == 0) {
1297 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1298 } else if (g_strcmp0(key, "EapMethods") == 0) {
1299 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1300 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1302 DBG("key %s type %c",
1303 key, dbus_message_iter_get_arg_type(iter));
1306 static void supplicant_bootstrap(void)
1308 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1309 SUPPLICANT_INTERFACE,
1310 service_property, NULL);
1313 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1315 const char *name = NULL, *old = NULL, *new = NULL;
1317 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1320 dbus_message_iter_get_basic(iter, &name);
1324 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1327 dbus_message_iter_next(iter);
1328 dbus_message_iter_get_basic(iter, &old);
1329 dbus_message_iter_next(iter);
1330 dbus_message_iter_get_basic(iter, &new);
1335 if (strlen(old) > 0 && strlen(new) == 0) {
1336 system_available = FALSE;
1337 g_hash_table_remove_all(bss_mapping);
1338 g_hash_table_remove_all(interface_table);
1339 callback_system_killed();
1342 if (strlen(new) > 0 && strlen(old) == 0) {
1343 system_available = TRUE;
1344 supplicant_bootstrap();
1348 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1350 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1353 supplicant_dbus_property_foreach(iter, service_property, NULL);
1356 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1358 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1359 interface_added(iter, NULL);
1362 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1364 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1365 interface_removed(iter, NULL);
1368 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1370 struct supplicant_interface *interface;
1372 interface = g_hash_table_lookup(interface_table, path);
1376 supplicant_dbus_property_foreach(iter, interface_property, interface);
1379 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1381 struct supplicant_interface *interface;
1382 dbus_bool_t success = FALSE;
1384 interface = g_hash_table_lookup(interface_table, path);
1388 dbus_message_iter_get_basic(iter, &success);
1390 if (interface->scan_callback) {
1396 interface->scan_callback(result, interface->scan_data);
1399 interface->scan_callback = NULL;
1400 interface->scan_data = NULL;
1403 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1405 struct supplicant_interface *interface;
1407 interface = g_hash_table_lookup(interface_table, path);
1411 interface_bss_added(iter, interface);
1414 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1416 struct supplicant_interface *interface;
1418 interface = g_hash_table_lookup(interface_table, path);
1422 interface_bss_removed(iter, interface);
1425 static void signal_network_added(const char *path, DBusMessageIter *iter)
1427 struct supplicant_interface *interface;
1429 interface = g_hash_table_lookup(interface_table, path);
1433 interface_network_added(iter, interface);
1436 static void signal_network_removed(const char *path, DBusMessageIter *iter)
1438 struct supplicant_interface *interface;
1440 interface = g_hash_table_lookup(interface_table, path);
1444 interface_network_removed(iter, interface);
1447 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
1449 struct supplicant_interface *interface;
1450 struct supplicant_network *network;
1451 struct supplicant_bss *bss;
1453 interface = g_hash_table_lookup(bss_mapping, path);
1457 network = g_hash_table_lookup(interface->bss_mapping, path);
1461 bss = g_hash_table_lookup(network->bss_table, path);
1465 supplicant_dbus_property_foreach(iter, bss_property, bss);
1469 const char *interface;
1471 void (*function) (const char *path, DBusMessageIter *iter);
1473 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
1475 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
1476 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
1477 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
1478 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
1480 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
1481 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
1482 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
1483 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
1484 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
1485 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
1487 { SUPPLICANT_INTERFACE ".Interface.BSS", "PropertiesChanged", signal_bss_changed },
1492 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1493 DBusMessage *message, void *data)
1495 DBusMessageIter iter;
1499 path = dbus_message_get_path(message);
1501 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1503 if (!dbus_message_iter_init(message, &iter))
1504 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1506 for (i = 0; signal_map[i].interface; i++) {
1507 if (!dbus_message_has_interface(message, signal_map[i].interface))
1510 if (!dbus_message_has_member(message, signal_map[i].member))
1513 signal_map[i].function(path, &iter);
1517 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1520 static const char *supplicant_rule0 = "type=signal,"
1521 "path=" DBUS_PATH_DBUS ","
1522 "sender=" DBUS_SERVICE_DBUS ","
1523 "interface=" DBUS_INTERFACE_DBUS ","
1524 "member=NameOwnerChanged,"
1525 "arg0=" SUPPLICANT_SERVICE;
1526 static const char *supplicant_rule1 = "type=signal,"
1527 "interface=" SUPPLICANT_INTERFACE;
1528 static const char *supplicant_rule2 = "type=signal,"
1529 "interface=" SUPPLICANT_INTERFACE ".Interface";
1530 static const char *supplicant_rule3 = "type=signal,"
1531 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
1532 static const char *supplicant_rule4 = "type=signal,"
1533 "interface=" SUPPLICANT_INTERFACE ".Interface.BSS";
1534 static const char *supplicant_rule5 = "type=signal,"
1535 "interface=" SUPPLICANT_INTERFACE ".Interface.Network";
1536 static const char *supplicant_rule6 = "type=signal,"
1537 "interface=" SUPPLICANT_INTERFACE ".Interface.Blob";
1539 int supplicant_register(const struct supplicant_callbacks *callbacks)
1541 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1545 if (!dbus_connection_add_filter(connection, supplicant_filter, NULL, NULL)) {
1546 dbus_connection_unref(connection);
1551 callbacks_pointer = callbacks;
1554 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1555 NULL, remove_interface);
1557 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1560 supplicant_dbus_setup(connection);
1562 dbus_bus_add_match(connection, supplicant_rule0, NULL);
1563 dbus_bus_add_match(connection, supplicant_rule1, NULL);
1564 dbus_bus_add_match(connection, supplicant_rule2, NULL);
1565 dbus_bus_add_match(connection, supplicant_rule3, NULL);
1566 dbus_bus_add_match(connection, supplicant_rule4, NULL);
1567 dbus_bus_add_match(connection, supplicant_rule5, NULL);
1568 dbus_bus_add_match(connection, supplicant_rule6, NULL);
1569 dbus_connection_flush(connection);
1571 if (dbus_bus_name_has_owner(connection,
1572 SUPPLICANT_SERVICE, NULL)) {
1573 system_available = TRUE;
1574 supplicant_bootstrap();
1580 void supplicant_unregister(const struct supplicant_callbacks *callbacks)
1583 dbus_bus_remove_match(connection, supplicant_rule6, NULL);
1584 dbus_bus_remove_match(connection, supplicant_rule5, NULL);
1585 dbus_bus_remove_match(connection, supplicant_rule4, NULL);
1586 dbus_bus_remove_match(connection, supplicant_rule3, NULL);
1587 dbus_bus_remove_match(connection, supplicant_rule2, NULL);
1588 dbus_bus_remove_match(connection, supplicant_rule1, NULL);
1589 dbus_bus_remove_match(connection, supplicant_rule0, NULL);
1590 dbus_connection_flush(connection);
1592 dbus_connection_remove_filter(connection,
1593 supplicant_filter, NULL);
1597 g_hash_table_destroy(bss_mapping);
1601 if (interface_table) {
1602 g_hash_table_destroy(interface_table);
1603 interface_table = NULL;
1606 if (system_available)
1607 callback_system_killed();
1610 dbus_connection_unref(connection);
1614 callbacks_pointer = NULL;
1618 static void debug_level_result(const char *error,
1619 DBusMessageIter *iter, void *user_data)
1622 DBG("debug level failure: %s", error);
1625 static void debug_level_params(DBusMessageIter *iter, void *user_data)
1627 guint level = GPOINTER_TO_UINT(user_data);
1633 str = debug_strings[level];
1635 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str);
1638 void supplicant_set_debug_level(unsigned int level)
1640 if (!system_available)
1643 supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
1644 "DebugLevel", DBUS_TYPE_STRING_AS_STRING,
1645 debug_level_params, debug_level_result,
1646 GUINT_TO_POINTER(level));
1649 struct interface_create_data {
1652 struct supplicant_interface *interface;
1653 supplicant_interface_create_callback callback;
1657 static void interface_create_property(const char *key, DBusMessageIter *iter,
1660 struct interface_create_data *data = user_data;
1661 struct supplicant_interface *interface = data->interface;
1665 data->callback(0, data->interface, data->user_data);
1670 interface_property(key, iter, interface);
1673 static void interface_create_result(const char *error,
1674 DBusMessageIter *iter, void *user_data)
1676 struct interface_create_data *data = user_data;
1677 const char *path = NULL;
1685 dbus_message_iter_get_basic(iter, &path);
1691 if (!system_available) {
1696 data->interface = g_hash_table_lookup(interface_table, path);
1697 if (!data->interface) {
1698 data->interface = interface_alloc(path);
1699 if (!data->interface) {
1705 err = supplicant_dbus_property_get_all(path,
1706 SUPPLICANT_INTERFACE ".Interface",
1707 interface_create_property, data);
1713 data->callback(err, NULL, data->user_data);
1718 static void interface_create_params(DBusMessageIter *iter, void *user_data)
1720 struct interface_create_data *data = user_data;
1721 DBusMessageIter dict;
1723 supplicant_dbus_dict_open(iter, &dict);
1725 supplicant_dbus_dict_append_basic(&dict, "Ifname",
1726 DBUS_TYPE_STRING, &data->ifname);
1729 supplicant_dbus_dict_append_basic(&dict, "Driver",
1730 DBUS_TYPE_STRING, &data->driver);
1732 supplicant_dbus_dict_close(iter, &dict);
1735 static void interface_get_result(const char *error,
1736 DBusMessageIter *iter, void *user_data)
1738 struct interface_create_data *data = user_data;
1739 struct supplicant_interface *interface;
1740 const char *path = NULL;
1746 dbus_message_iter_get_basic(iter, &path);
1752 interface = g_hash_table_lookup(interface_table, path);
1759 data->callback(0, interface, data->user_data);
1766 if (!system_available) {
1771 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
1772 SUPPLICANT_INTERFACE,
1774 interface_create_params,
1775 interface_create_result, data);
1781 data->callback(err, NULL, data->user_data);
1786 static void interface_get_params(DBusMessageIter *iter, void *user_data)
1788 struct interface_create_data *data = user_data;
1790 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
1793 int supplicant_interface_create(const char *ifname, const char *driver,
1794 supplicant_interface_create_callback callback,
1797 struct interface_create_data *data;
1802 if (!system_available)
1805 data = dbus_malloc0(sizeof(*data));
1809 data->ifname = ifname;
1810 data->driver = driver;
1811 data->callback = callback;
1812 data->user_data = user_data;
1814 return supplicant_dbus_method_call(SUPPLICANT_PATH,
1815 SUPPLICANT_INTERFACE,
1817 interface_get_params,
1818 interface_get_result, data);
1821 int supplicant_interface_remove(struct supplicant_interface *interface,
1822 supplicant_interface_remove_callback callback,
1828 if (!system_available)
1834 struct interface_scan_data {
1835 struct supplicant_interface *interface;
1836 supplicant_interface_scan_callback callback;
1840 static void interface_scan_result(const char *error,
1841 DBusMessageIter *iter, void *user_data)
1843 struct interface_scan_data *data = user_data;
1847 data->callback(-EIO, data->user_data);
1849 data->interface->scan_callback = data->callback;
1850 data->interface->scan_data = data->user_data;
1856 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
1858 DBusMessageIter dict;
1859 const char *type = "passive";
1861 supplicant_dbus_dict_open(iter, &dict);
1863 supplicant_dbus_dict_append_basic(&dict, "Type",
1864 DBUS_TYPE_STRING, &type);
1866 supplicant_dbus_dict_close(iter, &dict);
1869 int supplicant_interface_scan(struct supplicant_interface *interface,
1870 supplicant_interface_scan_callback callback,
1873 struct interface_scan_data *data;
1878 if (!system_available)
1881 if (interface->scanning)
1884 data = dbus_malloc0(sizeof(*data));
1888 data->interface = interface;
1889 data->callback = callback;
1890 data->user_data = user_data;
1892 return supplicant_dbus_method_call(interface->path,
1893 SUPPLICANT_INTERFACE ".Interface", "Scan",
1894 interface_scan_params, interface_scan_result, data);
1897 struct interface_disconnect_data {
1898 supplicant_interface_disconnect_callback callback;
1902 static void interface_disconnect_result(const char *error,
1903 DBusMessageIter *iter, void *user_data)
1905 struct interface_disconnect_data *data = user_data;
1912 data->callback(result, data->user_data);
1917 int supplicant_interface_disconnect(struct supplicant_interface *interface,
1918 supplicant_interface_disconnect_callback callback,
1921 struct interface_disconnect_data *data;
1926 if (!system_available)
1929 data = dbus_malloc0(sizeof(*data));
1933 data->callback = callback;
1934 data->user_data = user_data;
1936 return supplicant_dbus_method_call(interface->path,
1937 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
1938 NULL, interface_disconnect_result, data);