3 * WPA supplicant library with GLib integration
5 * Copyright (C) 2012-2013 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 <netinet/if_ether.h>
35 #include <netinet/in.h>
41 #include "gsupplicant.h"
43 #define IEEE80211_CAP_ESS 0x0001
44 #define IEEE80211_CAP_IBSS 0x0002
45 #define IEEE80211_CAP_PRIVACY 0x0010
47 #define BSS_UNKNOWN_STRENGTH -90
49 static DBusConnection *connection;
51 static const GSupplicantCallbacks *callbacks_pointer;
53 static dbus_bool_t system_available = FALSE;
54 static dbus_bool_t system_ready = FALSE;
56 static dbus_int32_t debug_level;
57 static dbus_bool_t debug_timestamp = FALSE;
58 static dbus_bool_t debug_showkeys = FALSE;
60 static const char *debug_strings[] = {
61 "msgdump", "debug", "info", "warning", "error", NULL
64 static unsigned int eap_methods;
71 static struct strvalmap eap_method_map[] = {
72 { "MD5", G_SUPPLICANT_EAP_METHOD_MD5 },
73 { "TLS", G_SUPPLICANT_EAP_METHOD_TLS },
74 { "MSCHAPV2", G_SUPPLICANT_EAP_METHOD_MSCHAPV2 },
75 { "PEAP", G_SUPPLICANT_EAP_METHOD_PEAP },
76 { "TTLS", G_SUPPLICANT_EAP_METHOD_TTLS },
77 { "GTC", G_SUPPLICANT_EAP_METHOD_GTC },
78 { "OTP", G_SUPPLICANT_EAP_METHOD_OTP },
79 { "LEAP", G_SUPPLICANT_EAP_METHOD_LEAP },
80 { "WSC", G_SUPPLICANT_EAP_METHOD_WSC },
84 static struct strvalmap keymgmt_map[] = {
85 { "none", G_SUPPLICANT_KEYMGMT_NONE },
86 { "ieee8021x", G_SUPPLICANT_KEYMGMT_IEEE8021X },
87 { "wpa-none", G_SUPPLICANT_KEYMGMT_WPA_NONE },
88 { "wpa-psk", G_SUPPLICANT_KEYMGMT_WPA_PSK },
89 { "wpa-psk-sha256", G_SUPPLICANT_KEYMGMT_WPA_PSK_256 },
90 { "wpa-ft-psk", G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
91 { "wpa-ft-eap", G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
92 { "wpa-eap", G_SUPPLICANT_KEYMGMT_WPA_EAP },
93 { "wpa-eap-sha256", G_SUPPLICANT_KEYMGMT_WPA_EAP_256 },
94 { "wps", G_SUPPLICANT_KEYMGMT_WPS },
98 static struct strvalmap authalg_capa_map[] = {
99 { "open", G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN },
100 { "shared", G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED },
101 { "leap", G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP },
105 static struct strvalmap proto_capa_map[] = {
106 { "wpa", G_SUPPLICANT_CAPABILITY_PROTO_WPA },
107 { "rsn", G_SUPPLICANT_CAPABILITY_PROTO_RSN },
111 static struct strvalmap group_map[] = {
112 { "wep40", G_SUPPLICANT_GROUP_WEP40 },
113 { "wep104", G_SUPPLICANT_GROUP_WEP104 },
114 { "tkip", G_SUPPLICANT_GROUP_TKIP },
115 { "ccmp", G_SUPPLICANT_GROUP_CCMP },
119 static struct strvalmap pairwise_map[] = {
120 { "none", G_SUPPLICANT_PAIRWISE_NONE },
121 { "tkip", G_SUPPLICANT_PAIRWISE_TKIP },
122 { "ccmp", G_SUPPLICANT_PAIRWISE_CCMP },
126 static struct strvalmap scan_capa_map[] = {
127 { "active", G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE },
128 { "passive", G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE },
129 { "ssid", G_SUPPLICANT_CAPABILITY_SCAN_SSID },
133 static struct strvalmap mode_capa_map[] = {
134 { "infrastructure", G_SUPPLICANT_CAPABILITY_MODE_INFRA },
135 { "ad-hoc", G_SUPPLICANT_CAPABILITY_MODE_IBSS },
136 { "ap", G_SUPPLICANT_CAPABILITY_MODE_AP },
137 { "p2p", G_SUPPLICANT_CAPABILITY_MODE_P2P },
141 static GHashTable *interface_table;
142 static GHashTable *bss_mapping;
143 static GHashTable *peer_mapping;
144 static GHashTable *group_mapping;
145 static GHashTable *pending_peer_connection;
147 struct _GSupplicantWpsCredentials {
148 unsigned char ssid[32];
149 unsigned int ssid_len;
153 struct _GSupplicantInterface {
156 unsigned int keymgmt_capa;
157 unsigned int authalg_capa;
158 unsigned int proto_capa;
159 unsigned int group_capa;
160 unsigned int pairwise_capa;
161 unsigned int scan_capa;
162 unsigned int mode_capa;
163 unsigned int max_scan_ssids;
167 GSupplicantState state;
168 dbus_bool_t scanning;
169 GSupplicantInterfaceCallback scan_callback;
175 struct _GSupplicantWpsCredentials wps_cred;
176 GSupplicantWpsState wps_state;
177 GHashTable *network_table;
178 GHashTable *peer_table;
179 GHashTable *group_table;
180 GHashTable *net_mapping;
181 GHashTable *bss_mapping;
183 const char *pending_peer_path;
186 struct g_supplicant_bss {
187 GSupplicantInterface *interface;
189 unsigned char bssid[6];
190 unsigned char ssid[32];
191 unsigned int ssid_len;
192 dbus_uint16_t frequency;
193 dbus_uint32_t maxrate;
195 GSupplicantMode mode;
196 GSupplicantSecurity security;
197 dbus_bool_t rsn_selected;
198 unsigned int wpa_keymgmt;
199 unsigned int wpa_pairwise;
200 unsigned int wpa_group;
201 unsigned int rsn_keymgmt;
202 unsigned int rsn_pairwise;
203 unsigned int rsn_group;
204 unsigned int keymgmt;
207 dbus_bool_t ieee8021x;
208 unsigned int wps_capabilities;
211 struct _GSupplicantNetwork {
212 GSupplicantInterface *interface;
216 unsigned char ssid[32];
217 unsigned int ssid_len;
219 dbus_uint16_t frequency;
220 struct g_supplicant_bss *best_bss;
221 GSupplicantMode mode;
222 GSupplicantSecurity security;
224 unsigned int wps_capabilities;
225 GHashTable *bss_table;
226 GHashTable *config_table;
227 #if defined TIZEN_EXT
228 unsigned int isHS20AP;
235 struct _GSupplicantPeer {
236 GSupplicantInterface *interface;
238 unsigned char device_address[ETH_ALEN];
239 unsigned char iface_address[ETH_ALEN];
241 unsigned char *widi_ies;
244 unsigned int wps_capabilities;
246 const GSupplicantInterface *current_group_iface;
247 bool connection_requested;
250 struct _GSupplicantGroup {
251 GSupplicantInterface *interface;
252 GSupplicantInterface *orig_interface;
258 static inline void debug(const char *format, ...)
263 if (!callbacks_pointer->debug)
266 va_start(ap, format);
268 if (vsnprintf(str, sizeof(str), format, ap) > 0)
269 callbacks_pointer->debug(str);
274 #define SUPPLICANT_DBG(fmt, arg...) \
275 debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
277 static GSupplicantMode string2mode(const char *mode)
280 return G_SUPPLICANT_MODE_UNKNOWN;
282 if (g_str_equal(mode, "infrastructure"))
283 return G_SUPPLICANT_MODE_INFRA;
284 else if (g_str_equal(mode, "ad-hoc"))
285 return G_SUPPLICANT_MODE_IBSS;
287 return G_SUPPLICANT_MODE_UNKNOWN;
290 static const char *mode2string(GSupplicantMode mode)
293 case G_SUPPLICANT_MODE_UNKNOWN:
295 case G_SUPPLICANT_MODE_INFRA:
297 case G_SUPPLICANT_MODE_IBSS:
299 case G_SUPPLICANT_MODE_MASTER:
306 static const char *security2string(GSupplicantSecurity security)
309 case G_SUPPLICANT_SECURITY_UNKNOWN:
311 case G_SUPPLICANT_SECURITY_NONE:
313 case G_SUPPLICANT_SECURITY_WEP:
315 case G_SUPPLICANT_SECURITY_PSK:
317 case G_SUPPLICANT_SECURITY_IEEE8021X:
324 static GSupplicantState string2state(const char *state)
327 return G_SUPPLICANT_STATE_UNKNOWN;
329 if (g_str_equal(state, "unknown"))
330 return G_SUPPLICANT_STATE_UNKNOWN;
331 else if (g_str_equal(state, "interface_disabled"))
332 return G_SUPPLICANT_STATE_DISABLED;
333 else if (g_str_equal(state, "disconnected"))
334 return G_SUPPLICANT_STATE_DISCONNECTED;
335 else if (g_str_equal(state, "inactive"))
336 return G_SUPPLICANT_STATE_INACTIVE;
337 else if (g_str_equal(state, "scanning"))
338 return G_SUPPLICANT_STATE_SCANNING;
339 else if (g_str_equal(state, "authenticating"))
340 return G_SUPPLICANT_STATE_AUTHENTICATING;
341 else if (g_str_equal(state, "associating"))
342 return G_SUPPLICANT_STATE_ASSOCIATING;
343 else if (g_str_equal(state, "associated"))
344 return G_SUPPLICANT_STATE_ASSOCIATED;
345 else if (g_str_equal(state, "group_handshake"))
346 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
347 else if (g_str_equal(state, "4way_handshake"))
348 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
349 else if (g_str_equal(state, "completed"))
350 return G_SUPPLICANT_STATE_COMPLETED;
352 return G_SUPPLICANT_STATE_UNKNOWN;
355 static void callback_system_ready(void)
362 if (!callbacks_pointer)
365 if (!callbacks_pointer->system_ready)
368 callbacks_pointer->system_ready();
371 static void callback_system_killed(void)
373 system_ready = FALSE;
375 if (!callbacks_pointer)
378 if (!callbacks_pointer->system_killed)
381 callbacks_pointer->system_killed();
384 static void callback_interface_added(GSupplicantInterface *interface)
388 if (!callbacks_pointer)
391 if (!callbacks_pointer->interface_added)
394 callbacks_pointer->interface_added(interface);
397 static void callback_interface_state(GSupplicantInterface *interface)
399 if (!callbacks_pointer)
402 if (!callbacks_pointer->interface_state)
405 callbacks_pointer->interface_state(interface);
408 static void callback_interface_removed(GSupplicantInterface *interface)
410 if (!callbacks_pointer)
413 if (!callbacks_pointer->interface_removed)
416 callbacks_pointer->interface_removed(interface);
419 #if !defined TIZEN_EXT
420 static void callback_p2p_support(GSupplicantInterface *interface)
424 if (!interface->p2p_support)
427 if (callbacks_pointer && callbacks_pointer->p2p_support)
428 callbacks_pointer->p2p_support(interface);
432 static void callback_scan_started(GSupplicantInterface *interface)
434 if (!callbacks_pointer)
437 if (!callbacks_pointer->scan_started)
440 callbacks_pointer->scan_started(interface);
443 static void callback_scan_finished(GSupplicantInterface *interface)
445 if (!callbacks_pointer)
448 if (!callbacks_pointer->scan_finished)
451 callbacks_pointer->scan_finished(interface);
454 static void callback_network_added(GSupplicantNetwork *network)
456 if (!callbacks_pointer)
459 if (!callbacks_pointer->network_added)
462 callbacks_pointer->network_added(network);
465 static void callback_network_removed(GSupplicantNetwork *network)
467 if (!callbacks_pointer)
470 if (!callbacks_pointer->network_removed)
473 callbacks_pointer->network_removed(network);
476 #if defined TIZEN_EXT
477 static void callback_network_merged(GSupplicantNetwork *network)
479 if (!callbacks_pointer)
482 if (!callbacks_pointer->network_merged)
485 callbacks_pointer->network_merged(network);
489 static void callback_network_changed(GSupplicantNetwork *network,
490 const char *property)
492 if (!callbacks_pointer)
495 if (!callbacks_pointer->network_changed)
498 callbacks_pointer->network_changed(network, property);
501 static void callback_peer_found(GSupplicantPeer *peer)
503 if (!callbacks_pointer)
506 if (!callbacks_pointer->peer_found)
509 callbacks_pointer->peer_found(peer);
512 static void callback_peer_lost(GSupplicantPeer *peer)
514 if (!callbacks_pointer)
517 if (!callbacks_pointer->peer_lost)
520 callbacks_pointer->peer_lost(peer);
523 static void callback_peer_changed(GSupplicantPeer *peer,
524 GSupplicantPeerState state)
526 if (!callbacks_pointer)
529 if (!callbacks_pointer->peer_changed)
532 callbacks_pointer->peer_changed(peer, state);
535 static void callback_peer_request(GSupplicantPeer *peer)
537 if (!callbacks_pointer)
540 if (!callbacks_pointer->peer_request)
543 peer->connection_requested = true;
545 callbacks_pointer->peer_request(peer);
548 static void remove_group(gpointer data)
550 GSupplicantGroup *group = data;
553 g_slist_free_full(group->members, g_free);
559 static void remove_interface(gpointer data)
561 GSupplicantInterface *interface = data;
563 g_hash_table_destroy(interface->bss_mapping);
564 g_hash_table_destroy(interface->net_mapping);
565 g_hash_table_destroy(interface->network_table);
566 g_hash_table_destroy(interface->peer_table);
567 g_hash_table_destroy(interface->group_table);
569 if (interface->scan_callback) {
570 SUPPLICANT_DBG("call interface %p callback %p scanning %d",
571 interface, interface->scan_callback,
572 interface->scanning);
574 interface->scan_callback(-EIO, interface, interface->scan_data);
575 interface->scan_callback = NULL;
576 interface->scan_data = NULL;
578 if (interface->scanning) {
579 interface->scanning = FALSE;
580 callback_scan_finished(interface);
584 callback_interface_removed(interface);
586 g_free(interface->wps_cred.key);
587 g_free(interface->path);
588 g_free(interface->network_path);
589 #if defined TIZEN_EXT
590 interface->network_path = NULL;
592 g_free(interface->ifname);
593 g_free(interface->driver);
594 g_free(interface->bridge);
598 static void remove_network(gpointer data)
600 GSupplicantNetwork *network = data;
602 g_hash_table_destroy(network->bss_table);
604 callback_network_removed(network);
606 g_hash_table_destroy(network->config_table);
608 g_free(network->path);
609 g_free(network->group);
610 g_free(network->name);
611 #if defined TIZEN_EXT
612 g_free(network->eap);
613 g_free(network->identity);
614 g_free(network->phase2);
619 static void remove_bss(gpointer data)
621 struct g_supplicant_bss *bss = data;
627 static void remove_peer(gpointer data)
629 GSupplicantPeer *peer = data;
631 callback_peer_lost(peer);
634 g_slist_free_full(peer->groups, g_free);
637 g_hash_table_remove(peer_mapping, peer->path);
639 if (pending_peer_connection)
640 g_hash_table_remove(pending_peer_connection, peer->path);
644 g_free(peer->identifier);
649 static void debug_strvalmap(const char *label, struct strvalmap *map,
654 for (i = 0; map[i].str; i++) {
655 if (val & map[i].val)
656 SUPPLICANT_DBG("%s: %s", label, map[i].str);
660 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
662 GSupplicantInterface *interface = user_data;
663 const char *str = NULL;
666 dbus_message_iter_get_basic(iter, &str);
670 for (i = 0; keymgmt_map[i].str; i++)
671 if (strcmp(str, keymgmt_map[i].str) == 0) {
672 interface->keymgmt_capa |= keymgmt_map[i].val;
677 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
679 GSupplicantInterface *interface = user_data;
680 const char *str = NULL;
683 dbus_message_iter_get_basic(iter, &str);
687 for (i = 0; authalg_capa_map[i].str; i++)
688 if (strcmp(str, authalg_capa_map[i].str) == 0) {
689 interface->authalg_capa |= authalg_capa_map[i].val;
694 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
696 GSupplicantInterface *interface = user_data;
697 const char *str = NULL;
700 dbus_message_iter_get_basic(iter, &str);
704 for (i = 0; proto_capa_map[i].str; i++)
705 if (strcmp(str, proto_capa_map[i].str) == 0) {
706 interface->proto_capa |= proto_capa_map[i].val;
711 static void interface_capability_pairwise(DBusMessageIter *iter,
714 GSupplicantInterface *interface = user_data;
715 const char *str = NULL;
718 dbus_message_iter_get_basic(iter, &str);
722 for (i = 0; pairwise_map[i].str; i++)
723 if (strcmp(str, pairwise_map[i].str) == 0) {
724 interface->pairwise_capa |= pairwise_map[i].val;
729 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
731 GSupplicantInterface *interface = user_data;
732 const char *str = NULL;
735 dbus_message_iter_get_basic(iter, &str);
739 for (i = 0; group_map[i].str; i++)
740 if (strcmp(str, group_map[i].str) == 0) {
741 interface->group_capa |= group_map[i].val;
746 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
748 GSupplicantInterface *interface = user_data;
749 const char *str = NULL;
752 dbus_message_iter_get_basic(iter, &str);
756 for (i = 0; scan_capa_map[i].str; i++)
757 if (strcmp(str, scan_capa_map[i].str) == 0) {
758 interface->scan_capa |= scan_capa_map[i].val;
763 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
765 GSupplicantInterface *interface = user_data;
766 const char *str = NULL;
769 dbus_message_iter_get_basic(iter, &str);
773 for (i = 0; mode_capa_map[i].str; i++)
774 if (strcmp(str, mode_capa_map[i].str) == 0) {
775 interface->mode_capa |= mode_capa_map[i].val;
780 static void interface_capability(const char *key, DBusMessageIter *iter,
783 GSupplicantInterface *interface = user_data;
788 if (g_strcmp0(key, "KeyMgmt") == 0)
789 supplicant_dbus_array_foreach(iter,
790 interface_capability_keymgmt, interface);
791 else if (g_strcmp0(key, "AuthAlg") == 0)
792 supplicant_dbus_array_foreach(iter,
793 interface_capability_authalg, interface);
794 else if (g_strcmp0(key, "Protocol") == 0)
795 supplicant_dbus_array_foreach(iter,
796 interface_capability_proto, interface);
797 else if (g_strcmp0(key, "Pairwise") == 0)
798 supplicant_dbus_array_foreach(iter,
799 interface_capability_pairwise, interface);
800 else if (g_strcmp0(key, "Group") == 0)
801 supplicant_dbus_array_foreach(iter,
802 interface_capability_group, interface);
803 else if (g_strcmp0(key, "Scan") == 0)
804 supplicant_dbus_array_foreach(iter,
805 interface_capability_scan, interface);
806 else if (g_strcmp0(key, "Modes") == 0)
807 supplicant_dbus_array_foreach(iter,
808 interface_capability_mode, interface);
809 else if (g_strcmp0(key, "MaxScanSSID") == 0) {
810 dbus_int32_t max_scan_ssid;
812 dbus_message_iter_get_basic(iter, &max_scan_ssid);
813 if (max_scan_ssid < 2)
815 interface->max_scan_ssids = max_scan_ssid;
818 SUPPLICANT_DBG("key %s type %c",
819 key, dbus_message_iter_get_arg_type(iter));
822 static void set_apscan(DBusMessageIter *iter, void *user_data)
824 unsigned int ap_scan = *(unsigned int *)user_data;
826 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
829 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
830 unsigned int ap_scan)
832 return supplicant_dbus_property_set(interface->path,
833 SUPPLICANT_INTERFACE ".Interface",
834 "ApScan", DBUS_TYPE_UINT32_AS_STRING,
835 set_apscan, NULL, &ap_scan, NULL);
838 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
844 interface->data = data;
847 interface->scan_callback = NULL;
850 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
855 return interface->data;
858 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
863 return interface->ifname;
866 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
871 return interface->driver;
874 GSupplicantState g_supplicant_interface_get_state(
875 GSupplicantInterface *interface)
878 return G_SUPPLICANT_STATE_UNKNOWN;
880 return interface->state;
883 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
888 return (const char *)interface->wps_cred.key;
891 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
892 unsigned int *ssid_len)
897 if (!interface || interface->wps_cred.ssid_len == 0) {
902 *ssid_len = interface->wps_cred.ssid_len;
903 return interface->wps_cred.ssid;
906 GSupplicantWpsState g_supplicant_interface_get_wps_state(
907 GSupplicantInterface *interface)
910 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
912 return interface->wps_state;
915 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
920 return interface->mode_capa;
923 unsigned int g_supplicant_interface_get_max_scan_ssids(
924 GSupplicantInterface *interface)
929 if (interface->max_scan_ssids == 0)
930 return WPAS_MAX_SCAN_SSIDS;
932 return interface->max_scan_ssids;
935 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
937 dbus_bool_t enable = *(dbus_bool_t *)user_data;
939 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
942 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
948 if (!interface->network_path)
952 return supplicant_dbus_property_set(interface->network_path,
953 SUPPLICANT_INTERFACE ".Network",
954 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
955 set_network_enabled, NULL, &enable, NULL);
958 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
963 return interface->ready;
966 GSupplicantInterface *g_supplicant_network_get_interface(
967 GSupplicantNetwork *network)
972 return network->interface;
975 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
977 if (!network || !network->name)
980 return network->name;
983 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
985 if (!network || !network->group)
988 return network->group;
991 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
993 if (!network || !network->path)
996 return network->path;
999 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
1002 return G_SUPPLICANT_MODE_UNKNOWN;
1004 return mode2string(network->mode);
1007 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
1010 return G_SUPPLICANT_SECURITY_UNKNOWN;
1012 return security2string(network->security);
1015 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
1016 unsigned int *ssid_len)
1023 *ssid_len = network->ssid_len;
1024 return network->ssid;
1027 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
1032 return network->signal;
1035 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
1040 return network->frequency;
1043 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
1048 return network->wps;
1051 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
1056 if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
1062 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
1067 if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1073 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
1078 if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
1084 GSupplicantInterface *g_supplicant_peer_get_interface(GSupplicantPeer *peer)
1089 return peer->interface;
1092 const char *g_supplicant_peer_get_path(GSupplicantPeer *peer)
1100 const char *g_supplicant_peer_get_identifier(GSupplicantPeer *peer)
1105 return peer->identifier;
1108 const void *g_supplicant_peer_get_device_address(GSupplicantPeer *peer)
1113 return peer->device_address;
1116 const void *g_supplicant_peer_get_iface_address(GSupplicantPeer *peer)
1121 return peer->iface_address;
1124 const char *g_supplicant_peer_get_name(GSupplicantPeer *peer)
1132 #if defined TIZEN_EXT
1133 unsigned int g_supplicant_network_is_hs20AP(GSupplicantNetwork *network)
1138 return network->isHS20AP;
1141 const char *g_supplicant_network_get_eap(GSupplicantNetwork *network)
1143 if (!network || !network->eap)
1146 return network->eap;
1149 const char *g_supplicant_network_get_identity(GSupplicantNetwork *network)
1151 if (!network || !network->identity)
1154 return network->identity;
1157 const char *g_supplicant_network_get_phase2(GSupplicantNetwork *network)
1159 if (!network || !network->phase2)
1162 return network->phase2;
1166 const unsigned char *g_supplicant_peer_get_widi_ies(GSupplicantPeer *peer,
1169 if (!peer || !length)
1172 *length = peer->widi_ies_length;
1173 return peer->widi_ies;
1176 bool g_supplicant_peer_is_wps_pbc(GSupplicantPeer *peer)
1181 if (peer->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1187 bool g_supplicant_peer_is_wps_pin(GSupplicantPeer *peer)
1192 if (peer->wps_capabilities & G_SUPPLICANT_WPS_PIN)
1198 bool g_supplicant_peer_is_in_a_group(GSupplicantPeer *peer)
1200 if (!peer || !peer->groups)
1206 GSupplicantInterface *g_supplicant_peer_get_group_interface(GSupplicantPeer *peer)
1211 return (GSupplicantInterface *) peer->current_group_iface;
1214 bool g_supplicant_peer_is_client(GSupplicantPeer *peer)
1216 GSupplicantGroup *group;
1222 for (list = peer->groups; list; list = list->next) {
1223 const char *path = list->data;
1225 group = g_hash_table_lookup(group_mapping, path);
1229 if (group->role != G_SUPPLICANT_GROUP_ROLE_CLIENT ||
1230 group->orig_interface != peer->interface)
1233 if (group->interface == peer->current_group_iface)
1240 bool g_supplicant_peer_has_requested_connection(GSupplicantPeer *peer)
1245 return peer->connection_requested;
1248 #if defined TIZEN_EXT
1250 * Description: Network client requires additional wifi specific info
1252 const unsigned char *g_supplicant_network_get_bssid(GSupplicantNetwork *network)
1254 if (network == NULL || network->best_bss == NULL)
1257 return (const unsigned char *)network->best_bss->bssid;
1260 unsigned int g_supplicant_network_get_maxrate(GSupplicantNetwork *network)
1262 if (network == NULL || network->best_bss == NULL)
1265 return network->best_bss->maxrate;
1268 const char *g_supplicant_network_get_enc_mode(GSupplicantNetwork *network)
1270 if (network == NULL || network->best_bss == NULL)
1273 if (network->best_bss->security == G_SUPPLICANT_SECURITY_PSK ||
1274 network->best_bss->security == G_SUPPLICANT_SECURITY_IEEE8021X) {
1275 unsigned int pairwise;
1277 pairwise = network->best_bss->rsn_pairwise |
1278 network->best_bss->wpa_pairwise;
1280 if ((pairwise & G_SUPPLICANT_PAIRWISE_CCMP) &&
1281 (pairwise & G_SUPPLICANT_PAIRWISE_TKIP))
1283 else if (pairwise & G_SUPPLICANT_PAIRWISE_CCMP)
1285 else if (pairwise & G_SUPPLICANT_PAIRWISE_TKIP)
1288 } else if (network->best_bss->security == G_SUPPLICANT_SECURITY_WEP)
1290 else if (network->best_bss->security == G_SUPPLICANT_SECURITY_NONE)
1296 bool g_supplicant_network_get_rsn_selected(GSupplicantNetwork *network)
1298 if (network == NULL || network->best_bss == NULL)
1301 if (network->best_bss->rsn_selected)
1309 static void merge_network(GSupplicantNetwork *network)
1312 const char *ssid, *mode, *key_mgmt;
1313 #if defined TIZEN_EXT
1314 GSupplicantInterface *interface;
1315 const char *isHS20AP;
1316 const char *eap, *identity, *phase2;
1318 unsigned int i, ssid_len;
1321 ssid = g_hash_table_lookup(network->config_table, "ssid");
1322 mode = g_hash_table_lookup(network->config_table, "mode");
1323 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
1324 #if defined TIZEN_EXT
1325 isHS20AP = g_hash_table_lookup(network->config_table, "isHS20AP");
1326 eap = g_hash_table_lookup(network->config_table, "eap");
1327 identity = g_hash_table_lookup(network->config_table, "identity");
1328 phase2 = g_hash_table_lookup(network->config_table, "phase2");
1329 interface = network->interface;
1332 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
1335 ssid_len = strlen(ssid);
1339 str = g_string_sized_new((ssid_len * 2) + 24);
1343 for (i = 0; i < ssid_len; i++)
1344 #if defined TIZEN_EXT
1348 g_string_append_printf(str, "%02x", ssid[i]);
1349 #if defined TIZEN_EXT
1353 if (g_strcmp0(mode, "0") == 0)
1354 g_string_append_printf(str, "_managed");
1355 else if (g_strcmp0(mode, "1") == 0)
1356 g_string_append_printf(str, "_adhoc");
1358 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
1359 g_string_append_printf(str, "_psk");
1360 #if defined TIZEN_EXT
1361 else if (g_strcmp0(key_mgmt, "WPA-EAP") == 0)
1362 g_string_append_printf(str, "_ieee8021x");
1364 g_string_append_printf(str, "_none");
1367 group = g_string_free(str, FALSE);
1369 SUPPLICANT_DBG("%s", group);
1371 #if defined TIZEN_EXT
1372 if (g_strcmp0(isHS20AP, "1") == 0) {
1373 network->isHS20AP = 1;
1375 g_free(network->eap);
1376 network->eap = g_strdup(eap);
1378 if (network->identity)
1379 g_free(network->identity);
1380 network->identity = g_strdup(identity);
1382 if (network->phase2)
1383 g_free(network->phase2);
1384 network->phase2 = g_strdup(phase2);
1386 network->isHS20AP = 0;
1389 interface->network_path = g_strdup(network->path);
1391 network->group = g_strdup(group);
1392 callback_network_merged(network);
1397 g_hash_table_destroy(network->config_table);
1399 g_free(network->path);
1403 static void network_property(const char *key, DBusMessageIter *iter,
1406 GSupplicantNetwork *network = user_data;
1408 if (!network->interface)
1412 merge_network(network);
1416 if (g_strcmp0(key, "Enabled") == 0) {
1417 dbus_bool_t enabled = FALSE;
1419 dbus_message_iter_get_basic(iter, &enabled);
1420 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
1421 const char *str = NULL;
1423 dbus_message_iter_get_basic(iter, &str);
1425 g_hash_table_replace(network->config_table,
1426 g_strdup(key), g_strdup(str));
1429 SUPPLICANT_DBG("key %s type %c",
1430 key, dbus_message_iter_get_arg_type(iter));
1433 static void interface_network_added(DBusMessageIter *iter, void *user_data)
1435 GSupplicantInterface *interface = user_data;
1436 GSupplicantNetwork *network;
1437 const char *path = NULL;
1441 dbus_message_iter_get_basic(iter, &path);
1445 if (g_strcmp0(path, "/") == 0)
1448 network = g_hash_table_lookup(interface->net_mapping, path);
1452 network = g_try_new0(GSupplicantNetwork, 1);
1456 network->interface = interface;
1457 network->path = g_strdup(path);
1459 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1462 dbus_message_iter_next(iter);
1463 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1464 supplicant_dbus_property_foreach(iter, network_property,
1466 network_property(NULL, NULL, network);
1470 supplicant_dbus_property_get_all(path,
1471 SUPPLICANT_INTERFACE ".Network",
1472 network_property, network, NULL);
1475 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1477 GSupplicantInterface *interface = user_data;
1478 GSupplicantNetwork *network;
1479 const char *path = NULL;
1481 dbus_message_iter_get_basic(iter, &path);
1485 network = g_hash_table_lookup(interface->net_mapping, path);
1489 g_hash_table_remove(interface->net_mapping, path);
1492 static char *create_name(unsigned char *ssid, int ssid_len)
1495 const gchar *remainder, *invalid;
1496 int valid_bytes, remaining_bytes;
1498 if (ssid_len < 1 || ssid[0] == '\0')
1499 return g_strdup("");
1502 remainder = (const gchar *)ssid;
1503 remaining_bytes = ssid_len;
1505 while (remaining_bytes != 0) {
1506 if (g_utf8_validate(remainder, remaining_bytes,
1511 valid_bytes = invalid - remainder;
1514 string = g_string_sized_new(remaining_bytes);
1516 g_string_append_len(string, remainder, valid_bytes);
1518 /* append U+FFFD REPLACEMENT CHARACTER */
1519 g_string_append(string, "\357\277\275");
1521 remaining_bytes -= valid_bytes + 1;
1522 remainder = invalid + 1;
1526 return g_strndup((const gchar *)ssid, ssid_len + 1);
1528 g_string_append(string, remainder);
1530 return g_string_free(string, FALSE);
1533 static char *create_group(struct g_supplicant_bss *bss)
1537 const char *mode, *security;
1539 str = g_string_sized_new((bss->ssid_len * 2) + 24);
1543 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1544 for (i = 0; i < bss->ssid_len; i++)
1545 g_string_append_printf(str, "%02x", bss->ssid[i]);
1547 g_string_append_printf(str, "hidden");
1549 mode = mode2string(bss->mode);
1551 g_string_append_printf(str, "_%s", mode);
1553 security = security2string(bss->security);
1555 g_string_append_printf(str, "_%s", security);
1557 return g_string_free(str, FALSE);
1560 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1562 GSupplicantInterface *interface = bss->interface;
1563 GSupplicantNetwork *network;
1566 group = create_group(bss);
1567 SUPPLICANT_DBG("New group created: %s", group);
1572 network = g_hash_table_lookup(interface->network_table, group);
1575 SUPPLICANT_DBG("Network %s already exist", network->name);
1580 network = g_try_new0(GSupplicantNetwork, 1);
1586 network->interface = interface;
1588 network->path = g_strdup(bss->path);
1589 network->group = group;
1590 network->name = create_name(bss->ssid, bss->ssid_len);
1591 network->mode = bss->mode;
1592 network->security = bss->security;
1593 network->ssid_len = bss->ssid_len;
1594 memcpy(network->ssid, bss->ssid, bss->ssid_len);
1595 network->signal = bss->signal;
1596 network->frequency = bss->frequency;
1597 network->best_bss = bss;
1599 SUPPLICANT_DBG("New network %s created", network->name);
1601 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1604 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1607 g_hash_table_replace(interface->network_table,
1608 network->group, network);
1610 callback_network_added(network);
1613 /* We update network's WPS properties if only bss provides WPS. */
1614 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1615 network->wps = TRUE;
1616 network->wps_capabilities |= bss->wps_capabilities;
1619 if (bss->signal > network->signal) {
1620 network->signal = bss->signal;
1621 network->best_bss = bss;
1622 callback_network_changed(network, "Signal");
1625 g_hash_table_replace(interface->bss_mapping, bss->path, network);
1626 g_hash_table_replace(network->bss_table, bss->path, bss);
1628 g_hash_table_replace(bss_mapping, bss->path, interface);
1631 static void bss_rates(DBusMessageIter *iter, void *user_data)
1633 struct g_supplicant_bss *bss = user_data;
1634 dbus_uint32_t rate = 0;
1636 dbus_message_iter_get_basic(iter, &rate);
1640 if (rate > bss->maxrate)
1641 bss->maxrate = rate;
1644 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1646 unsigned int *keymgmt = user_data;
1647 const char *str = NULL;
1650 dbus_message_iter_get_basic(iter, &str);
1654 for (i = 0; keymgmt_map[i].str; i++)
1655 if (strcmp(str, keymgmt_map[i].str) == 0) {
1656 SUPPLICANT_DBG("Keymgmt: %s", str);
1657 *keymgmt |= keymgmt_map[i].val;
1662 static void bss_group(DBusMessageIter *iter, void *user_data)
1664 unsigned int *group = user_data;
1665 const char *str = NULL;
1668 dbus_message_iter_get_basic(iter, &str);
1672 for (i = 0; group_map[i].str; i++)
1673 if (strcmp(str, group_map[i].str) == 0) {
1674 SUPPLICANT_DBG("Group: %s", str);
1675 *group |= group_map[i].val;
1680 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1682 unsigned int *pairwise = user_data;
1683 const char *str = NULL;
1686 dbus_message_iter_get_basic(iter, &str);
1690 for (i = 0; pairwise_map[i].str; i++)
1691 if (strcmp(str, pairwise_map[i].str) == 0) {
1692 SUPPLICANT_DBG("Pairwise: %s", str);
1693 *pairwise |= pairwise_map[i].val;
1698 static void bss_wpa(const char *key, DBusMessageIter *iter,
1701 struct g_supplicant_bss *bss = user_data;
1702 unsigned int value = 0;
1704 SUPPLICANT_DBG("Key: %s", key);
1706 if (g_strcmp0(key, "KeyMgmt") == 0) {
1707 supplicant_dbus_array_foreach(iter, bss_keymgmt, &value);
1709 if (bss->rsn_selected)
1710 bss->rsn_keymgmt = value;
1712 bss->wpa_keymgmt = value;
1713 } else if (g_strcmp0(key, "Group") == 0) {
1714 supplicant_dbus_array_foreach(iter, bss_group, &value);
1716 if (bss->rsn_selected)
1717 bss->rsn_group = value;
1719 bss->wpa_group = value;
1720 } else if (g_strcmp0(key, "Pairwise") == 0) {
1721 supplicant_dbus_array_foreach(iter, bss_pairwise, &value);
1723 if (bss->rsn_selected)
1724 bss->rsn_pairwise = value;
1726 bss->wpa_pairwise = value;
1730 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1733 unsigned int len = 0;
1735 while (len + 4 < ie_size) {
1736 unsigned int hi = ie[len];
1737 unsigned int lo = ie[len + 1];
1738 unsigned int tmp_type = (hi << 8) + lo;
1739 unsigned int v_len = 0;
1741 /* hi and lo are used to recreate an unsigned int
1742 * based on 2 8bits length unsigned int. */
1746 v_len = (hi << 8) + lo;
1748 if (tmp_type == type) {
1749 unsigned int ret_value = 0;
1750 unsigned char *value = (unsigned char *)&ret_value;
1752 SUPPLICANT_DBG("IE: match type 0x%x", type);
1754 /* Verifying length relevance */
1755 if (v_len > sizeof(unsigned int) ||
1756 len + 4 + v_len > ie_size)
1759 memcpy(value, ie + len + 4, v_len);
1761 SUPPLICANT_DBG("returning 0x%x", ret_value);
1768 SUPPLICANT_DBG("returning 0");
1772 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1774 struct g_supplicant_bss *bss = user_data;
1775 const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1776 unsigned char *ie, *ie_end;
1777 DBusMessageIter array;
1781 #define WMM_WPA1_WPS_INFO 221
1782 #define WPS_INFO_MIN_LEN 6
1783 #define WPS_VERSION_TLV 0x104A
1784 #define WPS_STATE_TLV 0x1044
1785 #define WPS_METHODS_TLV 0x1012
1786 #define WPS_REGISTRAR_TLV 0x1041
1787 #define WPS_VERSION 0x10
1788 #define WPS_PBC 0x04
1789 #define WPS_PIN 0x00
1790 #define WPS_CONFIGURED 0x02
1792 dbus_message_iter_recurse(iter, &array);
1793 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1795 if (!ie || ie_len < 2)
1798 bss->wps_capabilities = 0;
1801 for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1804 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1805 memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1808 SUPPLICANT_DBG("IE: match WPS_OUI");
1810 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1811 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1813 bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1815 if (value == WPS_CONFIGURED)
1816 bss->wps_capabilities |=
1817 G_SUPPLICANT_WPS_CONFIGURED;
1820 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1822 if (GUINT16_FROM_BE(value) == WPS_PBC)
1823 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1824 if (GUINT16_FROM_BE(value) == WPS_PIN)
1825 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1827 bss->wps_capabilities |=
1828 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1830 /* If the AP sends this it means it's advertizing
1831 * as a registrar and the WPS process is launched
1833 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1834 bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1836 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1840 static void bss_compute_security(struct g_supplicant_bss *bss)
1843 * Combining RSN and WPA keymgmt
1844 * We combine it since parsing IEs might have set something for WPS. */
1845 bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt;
1847 bss->ieee8021x = FALSE;
1851 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1852 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1853 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1854 bss->ieee8021x = TRUE;
1857 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1858 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1859 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1863 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1865 bss->security = G_SUPPLICANT_SECURITY_PSK;
1866 else if (bss->privacy)
1867 bss->security = G_SUPPLICANT_SECURITY_WEP;
1869 bss->security = G_SUPPLICANT_SECURITY_NONE;
1873 static void bss_property(const char *key, DBusMessageIter *iter,
1876 struct g_supplicant_bss *bss = user_data;
1878 if (!bss->interface)
1881 SUPPLICANT_DBG("key %s", key);
1886 if (g_strcmp0(key, "BSSID") == 0) {
1887 DBusMessageIter array;
1888 unsigned char *addr;
1891 dbus_message_iter_recurse(iter, &array);
1892 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1895 memcpy(bss->bssid, addr, addr_len);
1896 } else if (g_strcmp0(key, "SSID") == 0) {
1897 DBusMessageIter array;
1898 unsigned char *ssid;
1901 dbus_message_iter_recurse(iter, &array);
1902 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1904 if (ssid_len > 0 && ssid_len < 33) {
1905 memcpy(bss->ssid, ssid, ssid_len);
1906 bss->ssid_len = ssid_len;
1908 memset(bss->ssid, 0, sizeof(bss->ssid));
1911 } else if (g_strcmp0(key, "Capabilities") == 0) {
1912 dbus_uint16_t capabilities = 0x0000;
1914 dbus_message_iter_get_basic(iter, &capabilities);
1916 if (capabilities & IEEE80211_CAP_ESS)
1917 bss->mode = G_SUPPLICANT_MODE_INFRA;
1918 else if (capabilities & IEEE80211_CAP_IBSS)
1919 bss->mode = G_SUPPLICANT_MODE_IBSS;
1921 if (capabilities & IEEE80211_CAP_PRIVACY)
1922 bss->privacy = TRUE;
1923 } else if (g_strcmp0(key, "Mode") == 0) {
1924 const char *mode = NULL;
1926 dbus_message_iter_get_basic(iter, &mode);
1927 bss->mode = string2mode(mode);
1928 } else if (g_strcmp0(key, "Frequency") == 0) {
1929 dbus_uint16_t frequency = 0;
1931 dbus_message_iter_get_basic(iter, &frequency);
1932 bss->frequency = frequency;
1933 } else if (g_strcmp0(key, "Signal") == 0) {
1934 dbus_int16_t signal = 0;
1936 dbus_message_iter_get_basic(iter, &signal);
1938 bss->signal = signal;
1940 bss->signal = BSS_UNKNOWN_STRENGTH;
1942 } else if (g_strcmp0(key, "Level") == 0) {
1943 dbus_int32_t level = 0;
1945 dbus_message_iter_get_basic(iter, &level);
1946 } else if (g_strcmp0(key, "Rates") == 0) {
1947 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1948 } else if (g_strcmp0(key, "MaxRate") == 0) {
1949 dbus_uint32_t maxrate = 0;
1951 dbus_message_iter_get_basic(iter, &maxrate);
1953 bss->maxrate = maxrate;
1954 } else if (g_strcmp0(key, "Privacy") == 0) {
1955 dbus_bool_t privacy = FALSE;
1957 dbus_message_iter_get_basic(iter, &privacy);
1958 bss->privacy = privacy;
1959 } else if (g_strcmp0(key, "RSN") == 0) {
1960 bss->rsn_selected = TRUE;
1962 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1963 } else if (g_strcmp0(key, "WPA") == 0) {
1964 bss->rsn_selected = FALSE;
1966 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1967 } else if (g_strcmp0(key, "IEs") == 0)
1968 bss_process_ies(iter, bss);
1970 SUPPLICANT_DBG("key %s type %c",
1971 key, dbus_message_iter_get_arg_type(iter));
1974 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1977 GSupplicantInterface *interface = user_data;
1978 GSupplicantNetwork *network;
1979 struct g_supplicant_bss *bss;
1980 const char *path = NULL;
1984 dbus_message_iter_get_basic(iter, &path);
1988 if (g_strcmp0(path, "/") == 0)
1991 SUPPLICANT_DBG("%s", path);
1993 network = g_hash_table_lookup(interface->bss_mapping, path);
1995 bss = g_hash_table_lookup(network->bss_table, path);
2000 bss = g_try_new0(struct g_supplicant_bss, 1);
2004 bss->interface = interface;
2005 bss->path = g_strdup(path);
2006 bss->signal = BSS_UNKNOWN_STRENGTH;
2011 static void interface_bss_added_with_keys(DBusMessageIter *iter,
2014 struct g_supplicant_bss *bss;
2018 bss = interface_bss_added(iter, user_data);
2022 dbus_message_iter_next(iter);
2024 if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
2027 supplicant_dbus_property_foreach(iter, bss_property, bss);
2029 bss_compute_security(bss);
2030 add_or_replace_bss_to_network(bss);
2033 static void interface_bss_added_without_keys(DBusMessageIter *iter,
2036 struct g_supplicant_bss *bss;
2040 bss = interface_bss_added(iter, user_data);
2044 supplicant_dbus_property_get_all(bss->path,
2045 SUPPLICANT_INTERFACE ".BSS",
2046 bss_property, bss, NULL);
2048 bss_compute_security(bss);
2049 add_or_replace_bss_to_network(bss);
2052 static void update_signal(gpointer key, gpointer value,
2055 struct g_supplicant_bss *bss = value;
2056 GSupplicantNetwork *network = user_data;
2058 if (bss->signal > network->signal) {
2059 network->signal = bss->signal;
2060 network->best_bss = bss;
2064 static void update_network_signal(GSupplicantNetwork *network)
2066 if (g_hash_table_size(network->bss_table) <= 1 && network->best_bss)
2069 g_hash_table_foreach(network->bss_table,
2070 update_signal, network);
2072 SUPPLICANT_DBG("New network signal %d", network->signal);
2075 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
2077 GSupplicantInterface *interface = user_data;
2078 GSupplicantNetwork *network;
2079 struct g_supplicant_bss *bss = NULL;
2080 const char *path = NULL;
2082 dbus_message_iter_get_basic(iter, &path);
2086 network = g_hash_table_lookup(interface->bss_mapping, path);
2090 bss = g_hash_table_lookup(network->bss_table, path);
2091 if (network->best_bss == bss) {
2092 network->best_bss = NULL;
2093 network->signal = BSS_UNKNOWN_STRENGTH;
2096 g_hash_table_remove(bss_mapping, path);
2098 g_hash_table_remove(interface->bss_mapping, path);
2099 g_hash_table_remove(network->bss_table, path);
2101 update_network_signal(network);
2103 if (g_hash_table_size(network->bss_table) == 0)
2104 g_hash_table_remove(interface->network_table, network->group);
2107 static void set_config_methods(DBusMessageIter *iter, void *user_data)
2109 const char *config_methods = "push_button";
2111 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2115 static void interface_property(const char *key, DBusMessageIter *iter,
2118 GSupplicantInterface *interface = user_data;
2123 SUPPLICANT_DBG("%s", key);
2126 debug_strvalmap("KeyMgmt capability", keymgmt_map,
2127 interface->keymgmt_capa);
2128 debug_strvalmap("AuthAlg capability", authalg_capa_map,
2129 interface->authalg_capa);
2130 debug_strvalmap("Protocol capability", proto_capa_map,
2131 interface->proto_capa);
2132 debug_strvalmap("Pairwise capability", pairwise_map,
2133 interface->pairwise_capa);
2134 debug_strvalmap("Group capability", group_map,
2135 interface->group_capa);
2136 debug_strvalmap("Scan capability", scan_capa_map,
2137 interface->scan_capa);
2138 debug_strvalmap("Mode capability", mode_capa_map,
2139 interface->mode_capa);
2142 supplicant_dbus_property_set(interface->path,
2143 SUPPLICANT_INTERFACE ".Interface.WPS",
2144 "ConfigMethods", DBUS_TYPE_STRING_AS_STRING,
2145 set_config_methods, NULL, NULL, NULL);
2147 if (interface->ready)
2148 callback_interface_added(interface);
2153 if (g_strcmp0(key, "Capabilities") == 0) {
2154 supplicant_dbus_property_foreach(iter, interface_capability,
2156 #if !defined TIZEN_EXT
2157 if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_P2P)
2158 interface->p2p_support = true;
2160 } else if (g_strcmp0(key, "State") == 0) {
2161 const char *str = NULL;
2163 dbus_message_iter_get_basic(iter, &str);
2165 if (string2state(str) != interface->state) {
2166 interface->state = string2state(str);
2167 callback_interface_state(interface);
2169 if (interface->state == G_SUPPLICANT_STATE_DISABLED)
2170 interface->ready = FALSE;
2172 interface->ready = TRUE;
2174 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
2175 } else if (g_strcmp0(key, "Scanning") == 0) {
2176 dbus_bool_t scanning = FALSE;
2178 dbus_message_iter_get_basic(iter, &scanning);
2179 interface->scanning = scanning;
2181 if (interface->ready) {
2182 if (interface->scanning)
2183 callback_scan_started(interface);
2185 callback_scan_finished(interface);
2187 } else if (g_strcmp0(key, "ApScan") == 0) {
2190 dbus_message_iter_get_basic(iter, &apscan);
2191 interface->apscan = apscan;
2192 } else if (g_strcmp0(key, "Ifname") == 0) {
2193 const char *str = NULL;
2195 dbus_message_iter_get_basic(iter, &str);
2197 g_free(interface->ifname);
2198 interface->ifname = g_strdup(str);
2200 } else if (g_strcmp0(key, "Driver") == 0) {
2201 const char *str = NULL;
2203 dbus_message_iter_get_basic(iter, &str);
2205 g_free(interface->driver);
2206 interface->driver = g_strdup(str);
2208 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
2209 const char *str = NULL;
2211 dbus_message_iter_get_basic(iter, &str);
2213 g_free(interface->bridge);
2214 interface->bridge = g_strdup(str);
2216 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
2217 interface_bss_added_without_keys(iter, interface);
2218 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
2219 interface_network_added(iter, interface);
2220 } else if (g_strcmp0(key, "BSSs") == 0) {
2221 supplicant_dbus_array_foreach(iter,
2222 interface_bss_added_without_keys,
2224 } else if (g_strcmp0(key, "Blobs") == 0) {
2226 } else if (g_strcmp0(key, "Networks") == 0) {
2227 supplicant_dbus_array_foreach(iter, interface_network_added,
2230 SUPPLICANT_DBG("key %s type %c",
2231 key, dbus_message_iter_get_arg_type(iter));
2234 static void scan_network_update(DBusMessageIter *iter, void *user_data)
2236 GSupplicantInterface *interface = user_data;
2237 GSupplicantNetwork *network;
2243 dbus_message_iter_get_basic(iter, &path);
2248 if (g_strcmp0(path, "/") == 0)
2251 /* Update the network details based on scan BSS data */
2252 network = g_hash_table_lookup(interface->bss_mapping, path);
2254 callback_network_added(network);
2257 static void scan_bss_data(const char *key, DBusMessageIter *iter,
2260 GSupplicantInterface *interface = user_data;
2263 supplicant_dbus_array_foreach(iter, scan_network_update,
2266 if (interface->scan_callback)
2267 interface->scan_callback(0, interface, interface->scan_data);
2269 interface->scan_callback = NULL;
2270 interface->scan_data = NULL;
2273 static GSupplicantInterface *interface_alloc(const char *path)
2275 GSupplicantInterface *interface;
2277 interface = g_try_new0(GSupplicantInterface, 1);
2281 interface->path = g_strdup(path);
2283 interface->network_table = g_hash_table_new_full(g_str_hash,
2284 g_str_equal, NULL, remove_network);
2285 interface->peer_table = g_hash_table_new_full(g_str_hash,
2286 g_str_equal, NULL, remove_peer);
2287 interface->group_table = g_hash_table_new_full(g_str_hash,
2288 g_str_equal, NULL, remove_group);
2289 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2291 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2294 g_hash_table_replace(interface_table, interface->path, interface);
2299 static void interface_added(DBusMessageIter *iter, void *user_data)
2301 GSupplicantInterface *interface;
2302 const char *path = NULL;
2306 dbus_message_iter_get_basic(iter, &path);
2310 if (g_strcmp0(path, "/") == 0)
2313 interface = g_hash_table_lookup(interface_table, path);
2317 interface = interface_alloc(path);
2321 dbus_message_iter_next(iter);
2322 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
2323 supplicant_dbus_property_foreach(iter, interface_property,
2325 interface_property(NULL, NULL, interface);
2329 supplicant_dbus_property_get_all(path,
2330 SUPPLICANT_INTERFACE ".Interface",
2331 interface_property, interface,
2335 static void interface_removed(DBusMessageIter *iter, void *user_data)
2337 const char *path = NULL;
2338 GSupplicantInterface *interface = user_data;
2340 dbus_message_iter_get_basic(iter, &path);
2344 interface = g_hash_table_lookup(interface_table, path);
2345 g_supplicant_interface_cancel(interface);
2347 g_hash_table_remove(interface_table, path);
2350 static void eap_method(DBusMessageIter *iter, void *user_data)
2352 const char *str = NULL;
2355 dbus_message_iter_get_basic(iter, &str);
2359 for (i = 0; eap_method_map[i].str; i++)
2360 if (strcmp(str, eap_method_map[i].str) == 0) {
2361 eap_methods |= eap_method_map[i].val;
2366 static void service_property(const char *key, DBusMessageIter *iter,
2370 callback_system_ready();
2374 if (g_strcmp0(key, "DebugLevel") == 0) {
2375 const char *str = NULL;
2378 dbus_message_iter_get_basic(iter, &str);
2379 for (i = 0; debug_strings[i]; i++)
2380 if (g_strcmp0(debug_strings[i], str) == 0) {
2384 SUPPLICANT_DBG("Debug level %d", debug_level);
2385 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
2386 dbus_message_iter_get_basic(iter, &debug_timestamp);
2387 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
2388 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
2389 dbus_message_iter_get_basic(iter, &debug_showkeys);
2390 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
2391 } else if (g_strcmp0(key, "Interfaces") == 0) {
2392 supplicant_dbus_array_foreach(iter, interface_added, NULL);
2393 } else if (g_strcmp0(key, "EapMethods") == 0) {
2394 supplicant_dbus_array_foreach(iter, eap_method, NULL);
2395 debug_strvalmap("EAP method", eap_method_map, eap_methods);
2396 } else if (g_strcmp0(key, "Country") == 0) {
2397 const char *country = NULL;
2399 dbus_message_iter_get_basic(iter, &country);
2400 SUPPLICANT_DBG("Country %s", country);
2402 SUPPLICANT_DBG("key %s type %c",
2403 key, dbus_message_iter_get_arg_type(iter));
2406 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
2408 const char *name = NULL, *old = NULL, *new = NULL;
2412 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
2415 dbus_message_iter_get_basic(iter, &name);
2419 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
2422 dbus_message_iter_next(iter);
2423 dbus_message_iter_get_basic(iter, &old);
2424 dbus_message_iter_next(iter);
2425 dbus_message_iter_get_basic(iter, &new);
2430 if (strlen(old) > 0 && strlen(new) == 0) {
2431 system_available = FALSE;
2432 g_hash_table_remove_all(bss_mapping);
2433 g_hash_table_remove_all(peer_mapping);
2434 g_hash_table_remove_all(group_mapping);
2435 g_hash_table_remove_all(interface_table);
2436 callback_system_killed();
2439 if (strlen(new) > 0 && strlen(old) == 0) {
2440 system_available = TRUE;
2441 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
2442 SUPPLICANT_INTERFACE,
2443 service_property, NULL, NULL);
2447 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
2451 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
2454 supplicant_dbus_property_foreach(iter, service_property, NULL);
2457 static void signal_interface_added(const char *path, DBusMessageIter *iter)
2459 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2461 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2462 interface_added(iter, NULL);
2465 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
2469 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2470 interface_removed(iter, NULL);
2473 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
2475 GSupplicantInterface *interface;
2479 interface = g_hash_table_lookup(interface_table, path);
2483 supplicant_dbus_property_foreach(iter, interface_property, interface);
2486 static void signal_scan_done(const char *path, DBusMessageIter *iter)
2488 GSupplicantInterface *interface;
2489 dbus_bool_t success = FALSE;
2493 interface = g_hash_table_lookup(interface_table, path);
2497 dbus_message_iter_get_basic(iter, &success);
2499 if (interface->scanning) {
2500 callback_scan_finished(interface);
2501 interface->scanning = FALSE;
2505 * If scan is unsuccessful return -EIO else get the scanned BSSs
2506 * and update the network details accordingly
2509 if (interface->scan_callback)
2510 interface->scan_callback(-EIO, interface,
2511 interface->scan_data);
2513 interface->scan_callback = NULL;
2514 interface->scan_data = NULL;
2519 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
2520 "BSSs", scan_bss_data, interface, interface);
2523 static void signal_bss_added(const char *path, DBusMessageIter *iter)
2525 GSupplicantInterface *interface;
2529 interface = g_hash_table_lookup(interface_table, path);
2533 interface_bss_added_with_keys(iter, interface);
2536 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
2538 GSupplicantInterface *interface;
2542 interface = g_hash_table_lookup(interface_table, path);
2546 interface_bss_removed(iter, interface);
2549 static void signal_network_added(const char *path, DBusMessageIter *iter)
2551 GSupplicantInterface *interface;
2555 interface = g_hash_table_lookup(interface_table, path);
2559 interface_network_added(iter, interface);
2562 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2564 GSupplicantInterface *interface;
2568 interface = g_hash_table_lookup(interface_table, path);
2572 interface_network_removed(iter, interface);
2575 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2577 GSupplicantInterface *interface;
2578 GSupplicantNetwork *network;
2579 GSupplicantSecurity old_security;
2580 struct g_supplicant_bss *bss;
2584 interface = g_hash_table_lookup(bss_mapping, path);
2588 network = g_hash_table_lookup(interface->bss_mapping, path);
2592 bss = g_hash_table_lookup(network->bss_table, path);
2596 supplicant_dbus_property_foreach(iter, bss_property, bss);
2598 old_security = network->security;
2599 bss_compute_security(bss);
2601 if (old_security != bss->security) {
2602 struct g_supplicant_bss *new_bss;
2604 SUPPLICANT_DBG("New network security for %s", bss->ssid);
2606 /* Security change policy:
2607 * - we first copy the current bss into a new one with
2608 * its own pointer (path)
2609 * - we remove the current bss related network which will
2610 * tell the plugin about such removal. This is done due
2611 * to the fact that a security change means a group change
2612 * so a complete network change.
2613 * (current bss becomes invalid as well)
2614 * - we add the new bss: it adds new network and tell the
2615 * plugin about it. */
2617 new_bss = g_try_new0(struct g_supplicant_bss, 1);
2621 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
2622 new_bss->path = g_strdup(bss->path);
2624 g_hash_table_remove(interface->network_table, network->group);
2626 add_or_replace_bss_to_network(new_bss);
2631 #if defined TIZEN_EXT
2632 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
2633 network->wps = TRUE;
2634 network->wps_capabilities |= bss->wps_capabilities;
2636 network->wps = FALSE;
2639 if (bss->signal == network->signal)
2644 callback_network_changed(network, "");
2650 * If the new signal is lower than the SSID signal, we need
2651 * to check for the new maximum.
2653 if (bss->signal < network->signal) {
2654 if (bss != network->best_bss)
2659 callback_network_changed(network, "");
2663 network->signal = bss->signal;
2664 update_network_signal(network);
2666 network->signal = bss->signal;
2667 network->best_bss = bss;
2670 SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid,
2673 callback_network_changed(network, "Signal");
2676 static void wps_credentials(const char *key, DBusMessageIter *iter,
2679 GSupplicantInterface *interface = user_data;
2684 SUPPLICANT_DBG("key %s", key);
2686 if (g_strcmp0(key, "Key") == 0) {
2687 DBusMessageIter array;
2688 unsigned char *key_val;
2691 dbus_message_iter_recurse(iter, &array);
2692 dbus_message_iter_get_fixed_array(&array, &key_val, &key_len);
2694 g_free(interface->wps_cred.key);
2695 interface->wps_cred.key = g_try_malloc0(
2696 sizeof(char) * key_len + 1);
2698 if (!interface->wps_cred.key)
2701 memcpy(interface->wps_cred.key, key_val,
2702 sizeof(char) * key_len);
2704 SUPPLICANT_DBG("WPS key present");
2705 } else if (g_strcmp0(key, "SSID") == 0) {
2706 DBusMessageIter array;
2707 unsigned char *ssid;
2710 dbus_message_iter_recurse(iter, &array);
2711 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2713 if (ssid_len > 0 && ssid_len < 33) {
2714 memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2715 interface->wps_cred.ssid_len = ssid_len;
2717 memset(interface->wps_cred.ssid, 0, 32);
2718 interface->wps_cred.ssid_len = 0;
2723 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2725 GSupplicantInterface *interface;
2729 interface = g_hash_table_lookup(interface_table, path);
2733 supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2736 static void wps_event_args(const char *key, DBusMessageIter *iter,
2739 GSupplicantInterface *interface = user_data;
2741 if (!key || !interface)
2744 SUPPLICANT_DBG("Arg Key %s", key);
2747 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2749 GSupplicantInterface *interface;
2750 const char *name = NULL;
2754 interface = g_hash_table_lookup(interface_table, path);
2758 dbus_message_iter_get_basic(iter, &name);
2760 SUPPLICANT_DBG("Name: %s", name);
2762 if (g_strcmp0(name, "success") == 0)
2763 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2764 else if (g_strcmp0(name, "failed") == 0)
2765 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2767 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2769 if (!dbus_message_iter_has_next(iter))
2772 dbus_message_iter_next(iter);
2774 supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2777 #if defined TIZEN_EXT
2778 static void signal_power_off(const char *path, DBusMessageIter *iter)
2780 int poweroff_state = 0;
2782 dbus_message_iter_get_basic(iter, &poweroff_state);
2784 SUPPLICANT_DBG("poweroff_state(%d)", poweroff_state);
2786 /* POWER_OFF_DIRECT 2 && POWER_OFF_RESTART 3 */
2787 if (poweroff_state != 2 && poweroff_state != 3)
2790 if (callbacks_pointer == NULL)
2793 if (callbacks_pointer->system_power_off == NULL)
2796 callbacks_pointer->system_power_off();
2800 static void signal_station_connected(const char *path, DBusMessageIter *iter)
2802 GSupplicantInterface *interface;
2803 const char *sta_mac = NULL;
2805 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2807 if (callbacks_pointer->add_station == NULL)
2810 if (g_strcmp0(path, "/") == 0)
2813 interface = g_hash_table_lookup(interface_table, path);
2814 if (interface == NULL)
2817 dbus_message_iter_get_basic(iter, &sta_mac);
2818 if (sta_mac == NULL)
2821 SUPPLICANT_DBG("New station %s connected", sta_mac);
2822 callbacks_pointer->add_station(sta_mac);
2825 static void signal_station_disconnected(const char *path, DBusMessageIter *iter)
2827 GSupplicantInterface *interface;
2828 const char *sta_mac = NULL;
2830 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2832 if (callbacks_pointer->remove_station == NULL)
2835 if (g_strcmp0(path, "/") == 0)
2838 interface = g_hash_table_lookup(interface_table, path);
2839 if (interface == NULL)
2842 dbus_message_iter_get_basic(iter, &sta_mac);
2843 if (sta_mac == NULL)
2846 SUPPLICANT_DBG("Station %s disconnected", sta_mac);
2847 callbacks_pointer->remove_station(sta_mac);
2850 static void create_peer_identifier(GSupplicantPeer *peer)
2852 const unsigned char test[ETH_ALEN] = {};
2857 if (!memcmp(peer->device_address, test, ETH_ALEN)) {
2858 peer->identifier = g_strdup(peer->name);
2862 peer->identifier = g_malloc0(19);
2863 snprintf(peer->identifier, 19, "%02x%02x%02x%02x%02x%02x",
2864 peer->device_address[0],
2865 peer->device_address[1],
2866 peer->device_address[2],
2867 peer->device_address[3],
2868 peer->device_address[4],
2869 peer->device_address[5]);
2872 struct peer_property_data {
2873 GSupplicantPeer *peer;
2875 bool groups_changed;
2876 bool services_changed;
2879 static void peer_groups_relation(DBusMessageIter *iter, void *user_data)
2881 struct peer_property_data *data = user_data;
2882 GSupplicantPeer *peer = data->peer;
2883 GSupplicantGroup *group;
2884 const char *str = NULL;
2887 dbus_message_iter_get_basic(iter, &str);
2891 group = g_hash_table_lookup(group_mapping, str);
2895 elem = g_slist_find_custom(data->old_groups, str, g_str_equal);
2897 data->old_groups = g_slist_remove_link(data->old_groups, elem);
2898 peer->groups = g_slist_concat(elem, peer->groups);
2900 peer->groups = g_slist_prepend(peer->groups, g_strdup(str));
2901 data->groups_changed = true;
2905 static void peer_property(const char *key, DBusMessageIter *iter,
2908 GSupplicantPeer *pending_peer;
2909 struct peer_property_data *data = user_data;
2910 GSupplicantPeer *peer = data->peer;
2912 SUPPLICANT_DBG("key: %s", key);
2914 if (!peer->interface)
2919 create_peer_identifier(peer);
2920 callback_peer_found(peer);
2921 pending_peer = g_hash_table_lookup(
2922 pending_peer_connection, peer->path);
2924 if (pending_peer && pending_peer == peer) {
2925 callback_peer_request(peer);
2926 g_hash_table_remove(pending_peer_connection,
2936 if (g_strcmp0(key, "DeviceAddress") == 0) {
2937 unsigned char *dev_addr;
2938 DBusMessageIter array;
2941 dbus_message_iter_recurse(iter, &array);
2942 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
2944 if (len == ETH_ALEN)
2945 memcpy(peer->device_address, dev_addr, len);
2946 } else if (g_strcmp0(key, "DeviceName") == 0) {
2947 const char *str = NULL;
2949 dbus_message_iter_get_basic(iter, &str);
2951 peer->name = g_strdup(str);
2952 } else if (g_strcmp0(key, "config_method") == 0) {
2953 uint16_t wps_config;
2955 dbus_message_iter_get_basic(iter, &wps_config);
2957 if (wps_config & G_SUPPLICANT_WPS_CONFIG_PBC)
2958 peer->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
2959 if (wps_config & ~G_SUPPLICANT_WPS_CONFIG_PBC)
2960 peer->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
2961 } else if (g_strcmp0(key, "Groups") == 0) {
2962 data->old_groups = peer->groups;
2963 peer->groups = NULL;
2965 supplicant_dbus_array_foreach(iter,
2966 peer_groups_relation, data);
2967 if (g_slist_length(data->old_groups) > 0) {
2968 g_slist_free_full(data->old_groups, g_free);
2969 data->groups_changed = true;
2971 } else if (g_strcmp0(key, "IEs") == 0) {
2972 DBusMessageIter array;
2976 dbus_message_iter_recurse(iter, &array);
2977 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
2979 if (!ie || ie_len < 2)
2982 if (peer->widi_ies) {
2983 if (memcmp(peer->widi_ies, ie, ie_len) == 0)
2986 g_free(peer->widi_ies);
2987 peer->widi_ies_length = 0;
2990 peer->widi_ies = g_malloc0(ie_len * sizeof(unsigned char));
2992 memcpy(peer->widi_ies, ie, ie_len);
2993 peer->widi_ies_length = ie_len;
2994 data->services_changed = true;
2998 static void signal_peer_found(const char *path, DBusMessageIter *iter)
3000 struct peer_property_data *property_data;
3001 GSupplicantInterface *interface;
3002 const char *obj_path = NULL;
3003 GSupplicantPeer *peer;
3007 interface = g_hash_table_lookup(interface_table, path);
3011 dbus_message_iter_get_basic(iter, &obj_path);
3012 if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3015 peer = g_hash_table_lookup(interface->peer_table, obj_path);
3019 peer = g_try_new0(GSupplicantPeer, 1);
3023 peer->interface = interface;
3024 peer->path = g_strdup(obj_path);
3025 g_hash_table_insert(interface->peer_table, peer->path, peer);
3026 g_hash_table_replace(peer_mapping, peer->path, interface);
3028 property_data = dbus_malloc0(sizeof(struct peer_property_data));
3029 property_data->peer = peer;
3031 dbus_message_iter_next(iter);
3032 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3033 supplicant_dbus_property_foreach(iter, peer_property,
3035 peer_property(NULL, NULL, property_data);
3039 supplicant_dbus_property_get_all(obj_path,
3040 SUPPLICANT_INTERFACE ".Peer",
3041 peer_property, property_data, NULL);
3044 static void signal_peer_lost(const char *path, DBusMessageIter *iter)
3046 GSupplicantInterface *interface;
3047 const char *obj_path = NULL;
3048 GSupplicantPeer *peer;
3052 interface = g_hash_table_lookup(interface_table, path);
3056 dbus_message_iter_get_basic(iter, &obj_path);
3057 if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3060 peer = g_hash_table_lookup(interface->peer_table, obj_path);
3064 g_hash_table_remove(interface->peer_table, obj_path);
3067 static void signal_peer_changed(const char *path, DBusMessageIter *iter)
3069 struct peer_property_data *property_data;
3070 GSupplicantInterface *interface;
3071 GSupplicantPeer *peer;
3075 interface = g_hash_table_lookup(peer_mapping, path);
3079 peer = g_hash_table_lookup(interface->peer_table, path);
3081 g_hash_table_remove(peer_mapping, path);
3085 property_data = dbus_malloc0(sizeof(struct peer_property_data));
3086 property_data->peer = peer;
3088 supplicant_dbus_property_foreach(iter, peer_property, property_data);
3089 if (property_data->services_changed)
3090 callback_peer_changed(peer,
3091 G_SUPPLICANT_PEER_SERVICES_CHANGED);
3093 if (property_data->groups_changed)
3094 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_CHANGED);
3096 dbus_free(property_data);
3098 if (!g_supplicant_peer_is_in_a_group(peer))
3099 peer->connection_requested = false;
3102 struct group_sig_data {
3103 const char *peer_obj_path;
3104 unsigned char iface_address[ETH_ALEN];
3105 const char *interface_obj_path;
3106 const char *group_obj_path;
3110 static void group_sig_property(const char *key, DBusMessageIter *iter,
3113 struct group_sig_data *data = user_data;
3118 if (g_strcmp0(key, "peer_interface_addr") == 0) {
3119 unsigned char *dev_addr;
3120 DBusMessageIter array;
3123 dbus_message_iter_recurse(iter, &array);
3124 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
3126 if (len == ETH_ALEN)
3127 memcpy(data->iface_address, dev_addr, len);
3128 } else if (g_strcmp0(key, "role") == 0) {
3129 const char *str = NULL;
3131 dbus_message_iter_get_basic(iter, &str);
3132 if (g_strcmp0(str, "GO") == 0)
3133 data->role = G_SUPPLICANT_GROUP_ROLE_GO;
3135 data->role = G_SUPPLICANT_GROUP_ROLE_CLIENT;
3136 } else if (g_strcmp0(key, "peer_object") == 0)
3137 dbus_message_iter_get_basic(iter, &data->peer_obj_path);
3138 else if (g_strcmp0(key, "interface_object") == 0)
3139 dbus_message_iter_get_basic(iter, &data->interface_obj_path);
3140 else if (g_strcmp0(key, "group_object") == 0)
3141 dbus_message_iter_get_basic(iter, &data->group_obj_path);
3145 static void signal_group_success(const char *path, DBusMessageIter *iter)
3147 GSupplicantInterface *interface;
3148 struct group_sig_data data = {};
3149 GSupplicantPeer *peer;
3153 interface = g_hash_table_lookup(interface_table, path);
3157 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3158 if (!data.peer_obj_path)
3161 peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3165 memcpy(peer->iface_address, data.iface_address, ETH_ALEN);
3166 interface->pending_peer_path = peer->path;
3169 static void signal_group_failure(const char *path, DBusMessageIter *iter)
3171 GSupplicantInterface *interface;
3172 struct group_sig_data data = {};
3173 GSupplicantPeer *peer;
3177 interface = g_hash_table_lookup(interface_table, path);
3181 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3182 if (!data.peer_obj_path)
3185 peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3189 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_FAILED);
3190 peer->connection_requested = false;
3193 static void signal_group_started(const char *path, DBusMessageIter *iter)
3195 GSupplicantInterface *interface, *g_interface;
3196 struct group_sig_data data = {};
3197 GSupplicantGroup *group;
3198 GSupplicantPeer *peer;
3202 interface = g_hash_table_lookup(interface_table, path);
3206 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3207 if (!data.interface_obj_path || !data.group_obj_path)
3210 peer = g_hash_table_lookup(interface->peer_table,
3211 interface->pending_peer_path);
3212 interface->pending_peer_path = NULL;
3216 g_interface = g_hash_table_lookup(interface_table,
3217 data.interface_obj_path);
3221 group = g_hash_table_lookup(interface->group_table,
3222 data.group_obj_path);
3226 group = g_try_new0(GSupplicantGroup, 1);
3230 group->interface = g_interface;
3231 group->orig_interface = interface;
3232 group->path = g_strdup(data.group_obj_path);
3233 group->role = data.role;
3235 g_hash_table_insert(interface->group_table, group->path, group);
3236 g_hash_table_replace(group_mapping, group->path, group);
3238 peer->current_group_iface = g_interface;
3239 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_STARTED);
3242 static void remove_peer_group_interface(GHashTable *group_table,
3245 GSupplicantGroup *group;
3246 GHashTableIter iter;
3247 gpointer value, key;
3252 group = g_hash_table_lookup(group_table, path);
3254 if (!group || !group->orig_interface)
3257 g_hash_table_iter_init(&iter, group->orig_interface->peer_table);
3259 while (g_hash_table_iter_next(&iter, &key, &value)) {
3260 GSupplicantPeer *peer = value;
3262 if (peer->current_group_iface == group->interface)
3263 peer->current_group_iface = NULL;
3267 static void signal_group_finished(const char *path, DBusMessageIter *iter)
3269 GSupplicantInterface *interface;
3270 struct group_sig_data data = {};
3274 interface = g_hash_table_lookup(interface_table, path);
3278 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3279 if (!data.interface_obj_path || !data.group_obj_path)
3282 remove_peer_group_interface(interface->group_table, data.group_obj_path);
3284 g_hash_table_remove(group_mapping, data.group_obj_path);
3286 g_hash_table_remove(interface->group_table, data.group_obj_path);
3289 static void signal_group_request(const char *path, DBusMessageIter *iter)
3291 GSupplicantInterface *interface;
3292 GSupplicantPeer *peer;
3293 const char *obj_path;
3297 interface = g_hash_table_lookup(interface_table, path);
3301 dbus_message_iter_get_basic(iter, &obj_path);
3302 if (!obj_path || !g_strcmp0(obj_path, "/"))
3305 peer = g_hash_table_lookup(interface->peer_table, obj_path);
3310 * Peer has been previously found and property set,
3311 * otherwise, defer connection to when peer property
3314 if (peer->identifier)
3315 callback_peer_request(peer);
3317 g_hash_table_replace(pending_peer_connection, peer->path, peer);
3320 static void signal_group_peer_joined(const char *path, DBusMessageIter *iter)
3322 const char *peer_path = NULL;
3323 GSupplicantInterface *interface;
3324 GSupplicantGroup *group;
3325 GSupplicantPeer *peer;
3329 group = g_hash_table_lookup(group_mapping, path);
3333 dbus_message_iter_get_basic(iter, &peer_path);
3337 interface = g_hash_table_lookup(peer_mapping, peer_path);
3341 peer = g_hash_table_lookup(interface->peer_table, peer_path);
3345 group->members = g_slist_prepend(group->members, g_strdup(peer_path));
3347 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_JOINED);
3350 static void signal_group_peer_disconnected(const char *path, DBusMessageIter *iter)
3352 const char *peer_path = NULL;
3353 GSupplicantInterface *interface;
3354 GSupplicantGroup *group;
3355 GSupplicantPeer *peer;
3360 group = g_hash_table_lookup(group_mapping, path);
3364 dbus_message_iter_get_basic(iter, &peer_path);
3368 for (elem = group->members; elem; elem = elem->next) {
3369 if (!g_strcmp0(elem->data, peer_path))
3377 group->members = g_slist_delete_link(group->members, elem);
3379 interface = g_hash_table_lookup(peer_mapping, peer_path);
3383 peer = g_hash_table_lookup(interface->peer_table, peer_path);
3387 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_DISCONNECTED);
3388 peer->connection_requested = false;
3392 const char *interface;
3394 void (*function) (const char *path, DBusMessageIter *iter);
3396 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
3398 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
3399 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
3400 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
3401 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
3403 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
3404 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
3405 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
3406 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
3407 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
3408 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
3410 { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed },
3412 { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
3413 { SUPPLICANT_INTERFACE ".Interface.WPS", "Event", signal_wps_event },
3414 #if defined TIZEN_EXT
3415 { "org.tizen.system.deviced.PowerOff", "ChangeState", signal_power_off },
3418 { SUPPLICANT_INTERFACE".Interface", "StaAuthorized", signal_station_connected },
3419 { SUPPLICANT_INTERFACE".Interface", "StaDeauthorized", signal_station_disconnected },
3421 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceFound", signal_peer_found },
3422 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceLost", signal_peer_lost },
3424 { SUPPLICANT_INTERFACE ".Peer", "PropertiesChanged", signal_peer_changed },
3426 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationSuccess", signal_group_success },
3427 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationFailure", signal_group_failure },
3428 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupStarted", signal_group_started },
3429 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupFinished", signal_group_finished },
3430 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationRequest", signal_group_request },
3432 { SUPPLICANT_INTERFACE ".Group", "PeerJoined", signal_group_peer_joined },
3433 { SUPPLICANT_INTERFACE ".Group", "PeerDisconnected", signal_group_peer_disconnected },
3438 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
3439 DBusMessage *message, void *data)
3441 DBusMessageIter iter;
3445 path = dbus_message_get_path(message);
3447 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3449 if (!dbus_message_iter_init(message, &iter))
3450 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3452 for (i = 0; signal_map[i].interface; i++) {
3453 if (!dbus_message_has_interface(message, signal_map[i].interface))
3456 if (!dbus_message_has_member(message, signal_map[i].member))
3459 signal_map[i].function(path, &iter);
3463 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3466 void g_supplicant_interface_cancel(GSupplicantInterface *interface)
3468 SUPPLICANT_DBG("Cancelling any pending DBus calls");
3469 supplicant_dbus_method_call_cancel_all(interface);
3470 supplicant_dbus_property_call_cancel_all(interface);
3473 struct supplicant_regdom {
3474 GSupplicantCountryCallback callback;
3476 const void *user_data;
3479 static void country_result(const char *error,
3480 DBusMessageIter *iter, void *user_data)
3482 struct supplicant_regdom *regdom = user_data;
3485 SUPPLICANT_DBG("Country setting result");
3491 SUPPLICANT_DBG("Country setting failure %s", error);
3495 if (regdom->callback)
3496 regdom->callback(result, regdom->alpha2,
3497 (void *) regdom->user_data);
3502 static void country_params(DBusMessageIter *iter, void *user_data)
3504 struct supplicant_regdom *regdom = user_data;
3506 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
3510 int g_supplicant_set_country(const char *alpha2,
3511 GSupplicantCountryCallback callback,
3512 const void *user_data)
3514 struct supplicant_regdom *regdom;
3516 SUPPLICANT_DBG("Country setting %s", alpha2);
3518 if (!system_available)
3521 regdom = dbus_malloc0(sizeof(*regdom));
3525 regdom->callback = callback;
3526 regdom->alpha2 = alpha2;
3527 regdom->user_data = user_data;
3529 return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
3530 "Country", DBUS_TYPE_STRING_AS_STRING,
3531 country_params, country_result,
3535 int g_supplicant_interface_set_country(GSupplicantInterface *interface,
3536 GSupplicantCountryCallback callback,
3540 struct supplicant_regdom *regdom;
3542 regdom = dbus_malloc0(sizeof(*regdom));
3546 regdom->callback = callback;
3547 regdom->alpha2 = alpha2;
3548 regdom->user_data = user_data;
3550 return supplicant_dbus_property_set(interface->path,
3551 SUPPLICANT_INTERFACE ".Interface",
3552 "Country", DBUS_TYPE_STRING_AS_STRING,
3553 country_params, country_result,
3557 bool g_supplicant_interface_has_p2p(GSupplicantInterface *interface)
3562 return interface->p2p_support;
3565 struct supplicant_p2p_dev_config {
3570 static void p2p_device_config_result(const char *error,
3571 DBusMessageIter *iter, void *user_data)
3573 struct supplicant_p2p_dev_config *config = user_data;
3576 SUPPLICANT_DBG("Unable to set P2P Device configuration: %s",
3579 g_free(config->device_name);
3580 g_free(config->dev_type);
3584 static int dev_type_str2bin(const char *type, unsigned char dev_type[8])
3586 int length, pos, end;
3591 for (length = pos = 0; type[pos] != '\0' && length < 8; length++) {
3598 dev_type[length] = strtol(b, &e, 16);
3599 if (e && *e != '\0')
3608 static void p2p_device_config_params(DBusMessageIter *iter, void *user_data)
3610 struct supplicant_p2p_dev_config *config = user_data;
3611 DBusMessageIter dict;
3613 supplicant_dbus_dict_open(iter, &dict);
3615 supplicant_dbus_dict_append_basic(&dict, "DeviceName",
3616 DBUS_TYPE_STRING, &config->device_name);
3618 if (config->dev_type) {
3619 unsigned char dev_type[8] = {}, *type;
3622 len = dev_type_str2bin(config->dev_type, dev_type);
3625 supplicant_dbus_dict_append_fixed_array(&dict,
3626 "PrimaryDeviceType",
3627 DBUS_TYPE_BYTE, &type, len);
3631 supplicant_dbus_dict_close(iter, &dict);
3634 int g_supplicant_interface_set_p2p_device_config(GSupplicantInterface *interface,
3635 const char *device_name,
3636 const char *primary_dev_type)
3638 struct supplicant_p2p_dev_config *config;
3641 SUPPLICANT_DBG("P2P Device settings %s/%s",
3642 device_name, primary_dev_type);
3644 config = dbus_malloc0(sizeof(*config));
3648 config->device_name = g_strdup(device_name);
3649 config->dev_type = g_strdup(primary_dev_type);
3651 ret = supplicant_dbus_property_set(interface->path,
3652 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
3654 DBUS_TYPE_ARRAY_AS_STRING
3655 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
3656 DBUS_TYPE_STRING_AS_STRING
3657 DBUS_TYPE_VARIANT_AS_STRING
3658 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
3659 p2p_device_config_params,
3660 p2p_device_config_result, config, NULL);
3662 g_free(config->device_name);
3663 g_free(config->dev_type);
3665 SUPPLICANT_DBG("Unable to set P2P Device configuration");
3671 static gboolean peer_lookup_by_identifier(gpointer key, gpointer value,
3674 const GSupplicantPeer *peer = value;
3675 const char *identifier = user_data;
3677 if (!g_strcmp0(identifier, peer->identifier))
3683 GSupplicantPeer *g_supplicant_interface_peer_lookup(GSupplicantInterface *interface,
3684 const char *identifier)
3686 GSupplicantPeer *peer;
3688 peer = g_hash_table_find(interface->peer_table,
3689 peer_lookup_by_identifier,
3690 (void *) identifier);
3694 struct interface_data {
3695 GSupplicantInterface *interface;
3696 char *path; /* Interface path cannot be taken from interface (above) as
3697 * it might have been freed already.
3699 GSupplicantInterfaceCallback callback;
3703 struct interface_create_data {
3707 GSupplicantInterface *interface;
3708 GSupplicantInterfaceCallback callback;
3712 struct interface_connect_data {
3713 GSupplicantInterface *interface;
3715 GSupplicantInterfaceCallback callback;
3717 GSupplicantSSID *ssid;
3718 GSupplicantPeerParams *peer;
3723 struct interface_scan_data {
3724 GSupplicantInterface *interface;
3726 GSupplicantInterfaceCallback callback;
3727 GSupplicantScanParams *scan_params;
3731 static void interface_create_data_free(struct interface_create_data *data)
3733 g_free(data->ifname);
3734 g_free(data->driver);
3735 g_free(data->bridge);
3739 static bool interface_exists(GSupplicantInterface *interface,
3742 GSupplicantInterface *tmp;
3744 tmp = g_hash_table_lookup(interface_table, path);
3745 if (tmp && tmp == interface)
3751 static void interface_create_property(const char *key, DBusMessageIter *iter,
3754 struct interface_create_data *data = user_data;
3755 GSupplicantInterface *interface = data->interface;
3758 if (data->callback) {
3759 data->callback(0, data->interface, data->user_data);
3760 #if !defined TIZEN_EXT
3761 callback_p2p_support(interface);
3765 interface_create_data_free(data);
3768 interface_property(key, iter, interface);
3771 static void interface_create_result(const char *error,
3772 DBusMessageIter *iter, void *user_data)
3774 struct interface_create_data *data = user_data;
3775 const char *path = NULL;
3781 g_warning("error %s", error);
3786 dbus_message_iter_get_basic(iter, &path);
3792 if (!system_available) {
3797 data->interface = g_hash_table_lookup(interface_table, path);
3798 if (!data->interface) {
3799 data->interface = interface_alloc(path);
3800 if (!data->interface) {
3806 err = supplicant_dbus_property_get_all(path,
3807 SUPPLICANT_INTERFACE ".Interface",
3808 interface_create_property, data,
3815 data->callback(err, NULL, data->user_data);
3817 interface_create_data_free(data);
3820 static void interface_create_params(DBusMessageIter *iter, void *user_data)
3822 struct interface_create_data *data = user_data;
3823 DBusMessageIter dict;
3827 supplicant_dbus_dict_open(iter, &dict);
3829 supplicant_dbus_dict_append_basic(&dict, "Ifname",
3830 DBUS_TYPE_STRING, &data->ifname);
3833 supplicant_dbus_dict_append_basic(&dict, "Driver",
3834 DBUS_TYPE_STRING, &data->driver);
3837 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
3838 DBUS_TYPE_STRING, &data->bridge);
3840 supplicant_dbus_dict_close(iter, &dict);
3843 static void interface_get_result(const char *error,
3844 DBusMessageIter *iter, void *user_data)
3846 struct interface_create_data *data = user_data;
3847 GSupplicantInterface *interface;
3848 const char *path = NULL;
3854 SUPPLICANT_DBG("Interface not created yet");
3858 dbus_message_iter_get_basic(iter, &path);
3864 interface = g_hash_table_lookup(interface_table, path);
3870 if (data->callback) {
3871 data->callback(0, interface, data->user_data);
3872 #if !defined TIZEN_EXT
3873 callback_p2p_support(interface);
3877 interface_create_data_free(data);
3882 if (!system_available) {
3887 SUPPLICANT_DBG("Creating interface");
3889 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
3890 SUPPLICANT_INTERFACE,
3892 interface_create_params,
3893 interface_create_result, data,
3900 data->callback(err, NULL, data->user_data);
3902 interface_create_data_free(data);
3905 static void interface_get_params(DBusMessageIter *iter, void *user_data)
3907 struct interface_create_data *data = user_data;
3911 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
3914 int g_supplicant_interface_create(const char *ifname, const char *driver,
3916 GSupplicantInterfaceCallback callback,
3919 struct interface_create_data *data;
3922 SUPPLICANT_DBG("ifname %s", ifname);
3927 if (!system_available)
3930 data = dbus_malloc0(sizeof(*data));
3934 data->ifname = g_strdup(ifname);
3935 data->driver = g_strdup(driver);
3936 data->bridge = g_strdup(bridge);
3937 data->callback = callback;
3938 data->user_data = user_data;
3940 ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
3941 SUPPLICANT_INTERFACE,
3943 interface_get_params,
3944 interface_get_result, data,
3947 interface_create_data_free(data);
3952 static void interface_remove_result(const char *error,
3953 DBusMessageIter *iter, void *user_data)
3955 struct interface_data *data = user_data;
3963 if (!system_available) {
3969 * The gsupplicant interface is already freed by the InterfaceRemoved
3970 * signal callback. Simply invoke the interface_data callback.
3978 data->callback(err, NULL, data->user_data);
3984 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
3986 struct interface_data *data = user_data;
3988 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3989 &data->interface->path);
3993 int g_supplicant_interface_remove(GSupplicantInterface *interface,
3994 GSupplicantInterfaceCallback callback,
3997 struct interface_data *data;
4003 if (!system_available)
4006 g_supplicant_interface_cancel(interface);
4008 data = dbus_malloc0(sizeof(*data));
4012 data->interface = interface;
4013 data->path = g_strdup(interface->path);
4014 data->callback = callback;
4015 data->user_data = user_data;
4017 ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
4018 SUPPLICANT_INTERFACE,
4020 interface_remove_params,
4021 interface_remove_result, data,
4030 static void interface_scan_result(const char *error,
4031 DBusMessageIter *iter, void *user_data)
4033 struct interface_scan_data *data = user_data;
4037 SUPPLICANT_DBG("error %s", error);
4041 /* A non ready interface cannot send/receive anything */
4042 if (interface_exists(data->interface, data->path)) {
4043 if (!data->interface->ready)
4051 data->callback(err, data->interface, data->user_data);
4053 data->interface->scan_callback = data->callback;
4054 data->interface->scan_data = data->user_data;
4057 if (data->scan_params)
4058 g_supplicant_free_scan_params(data->scan_params);
4063 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
4065 DBusMessageIter data;
4066 unsigned int width = 0; /* Not used by wpa_supplicant atm */
4068 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
4070 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
4071 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
4073 dbus_message_iter_close_container(iter, &data);
4076 static void add_scan_frequencies(DBusMessageIter *iter,
4079 GSupplicantScanParams *scan_data = user_data;
4083 for (i = 0; i < scan_data->num_freqs; i++) {
4084 freq = scan_data->freqs[i];
4088 add_scan_frequency(iter, freq);
4092 static void append_ssid(DBusMessageIter *iter,
4093 const void *ssid, unsigned int len)
4095 DBusMessageIter array;
4097 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
4098 DBUS_TYPE_BYTE_AS_STRING, &array);
4100 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
4102 dbus_message_iter_close_container(iter, &array);
4105 static void append_ssids(DBusMessageIter *iter, void *user_data)
4107 GSupplicantScanParams *scan_data = user_data;
4110 for (list = scan_data->ssids; list; list = list->next) {
4111 struct scan_ssid *scan_ssid = list->data;
4113 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
4117 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
4118 supplicant_dbus_array_function function,
4121 GSupplicantScanParams *scan_params = user_data;
4122 DBusMessageIter entry, value, array;
4123 const char *key = "Channels";
4125 if (scan_params->freqs && scan_params->freqs[0] != 0) {
4126 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
4129 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
4131 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
4132 DBUS_TYPE_ARRAY_AS_STRING
4133 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4134 DBUS_TYPE_UINT32_AS_STRING
4135 DBUS_TYPE_UINT32_AS_STRING
4136 DBUS_STRUCT_END_CHAR_AS_STRING,
4139 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
4140 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4141 DBUS_TYPE_UINT32_AS_STRING
4142 DBUS_TYPE_UINT32_AS_STRING
4143 DBUS_STRUCT_END_CHAR_AS_STRING,
4147 function(&array, user_data);
4149 dbus_message_iter_close_container(&value, &array);
4150 dbus_message_iter_close_container(&entry, &value);
4151 dbus_message_iter_close_container(dict, &entry);
4155 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
4157 DBusMessageIter dict;
4158 const char *type = "passive";
4159 struct interface_scan_data *data = user_data;
4161 supplicant_dbus_dict_open(iter, &dict);
4163 if (data && data->scan_params) {
4166 supplicant_dbus_dict_append_basic(&dict, "Type",
4167 DBUS_TYPE_STRING, &type);
4169 supplicant_dbus_dict_append_array(&dict, "SSIDs",
4174 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
4177 supplicant_dbus_dict_append_basic(&dict, "Type",
4178 DBUS_TYPE_STRING, &type);
4180 supplicant_dbus_dict_close(iter, &dict);
4183 static int interface_ready_to_scan(GSupplicantInterface *interface)
4188 if (!system_available)
4191 if (interface->scanning)
4194 switch (interface->state) {
4195 case G_SUPPLICANT_STATE_AUTHENTICATING:
4196 case G_SUPPLICANT_STATE_ASSOCIATING:
4197 case G_SUPPLICANT_STATE_ASSOCIATED:
4198 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4199 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4201 case G_SUPPLICANT_STATE_UNKNOWN:
4202 case G_SUPPLICANT_STATE_DISABLED:
4203 case G_SUPPLICANT_STATE_DISCONNECTED:
4204 case G_SUPPLICANT_STATE_INACTIVE:
4205 case G_SUPPLICANT_STATE_SCANNING:
4206 case G_SUPPLICANT_STATE_COMPLETED:
4213 int g_supplicant_interface_scan(GSupplicantInterface *interface,
4214 GSupplicantScanParams *scan_data,
4215 GSupplicantInterfaceCallback callback,
4218 struct interface_scan_data *data;
4221 ret = interface_ready_to_scan(interface);
4225 data = dbus_malloc0(sizeof(*data));
4229 data->interface = interface;
4230 data->path = g_strdup(interface->path);
4231 #if defined TIZEN_EXT
4232 data->interface->scan_callback = data->callback = callback;
4233 data->interface->scan_data = data->user_data = user_data;
4235 data->callback = callback;
4236 data->user_data = user_data;
4238 data->scan_params = scan_data;
4240 interface->scan_callback = callback;
4241 interface->scan_data = user_data;
4243 ret = supplicant_dbus_method_call(interface->path,
4244 SUPPLICANT_INTERFACE ".Interface", "Scan",
4245 interface_scan_params, interface_scan_result, data,
4256 static int parse_supplicant_error(DBusMessageIter *iter)
4258 int err = -ECANCELED;
4264 /* If the given passphrase is malformed wpa_s returns
4265 * "invalid message format" but this error should be interpreted as
4268 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
4269 dbus_message_iter_get_basic(iter, &key);
4270 if (strncmp(key, "psk", 3) == 0 ||
4271 strncmp(key, "wep_key", 7) == 0 ||
4272 strcmp(key, "invalid message format") == 0) {
4276 dbus_message_iter_next(iter);
4282 static void interface_select_network_result(const char *error,
4283 DBusMessageIter *iter, void *user_data)
4285 struct interface_connect_data *data = user_data;
4292 SUPPLICANT_DBG("SelectNetwork error %s", error);
4293 err = parse_supplicant_error(iter);
4299 data->callback(err, data->interface, data->user_data);
4305 static void interface_select_network_params(DBusMessageIter *iter,
4308 struct interface_connect_data *data = user_data;
4309 GSupplicantInterface *interface = data->interface;
4311 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
4312 &interface->network_path);
4315 static void interface_add_network_result(const char *error,
4316 DBusMessageIter *iter, void *user_data)
4318 struct interface_connect_data *data = user_data;
4319 GSupplicantInterface *interface = data->interface;
4326 dbus_message_iter_get_basic(iter, &path);
4330 SUPPLICANT_DBG("PATH: %s", path);
4332 g_free(interface->network_path);
4333 interface->network_path = g_strdup(path);
4335 supplicant_dbus_method_call(data->interface->path,
4336 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
4337 interface_select_network_params,
4338 interface_select_network_result, data,
4344 SUPPLICANT_DBG("AddNetwork error %s", error);
4346 if (interface_exists(data->interface, data->interface->path)) {
4347 err = parse_supplicant_error(iter);
4349 data->callback(err, data->interface, data->user_data);
4351 g_free(interface->network_path);
4352 interface->network_path = NULL;
4360 static void add_network_security_none(DBusMessageIter *dict)
4362 const char *auth_alg = "OPEN";
4364 supplicant_dbus_dict_append_basic(dict, "auth_alg",
4365 DBUS_TYPE_STRING, &auth_alg);
4368 static void add_network_security_wep(DBusMessageIter *dict,
4369 GSupplicantSSID *ssid)
4371 const char *auth_alg = "OPEN SHARED";
4372 dbus_uint32_t key_index = 0;
4374 supplicant_dbus_dict_append_basic(dict, "auth_alg",
4375 DBUS_TYPE_STRING, &auth_alg);
4377 if (ssid->passphrase) {
4378 int size = strlen(ssid->passphrase);
4379 if (size == 10 || size == 26) {
4380 unsigned char *key = g_try_malloc(13);
4384 memset(tmp, 0, sizeof(tmp));
4388 for (i = 0; i < size / 2; i++) {
4389 memcpy(tmp, ssid->passphrase + (i * 2), 2);
4390 key[i] = (unsigned char) strtol(tmp, NULL, 16);
4393 supplicant_dbus_dict_append_fixed_array(dict,
4398 } else if (size == 5 || size == 13) {
4399 unsigned char *key = g_try_malloc(13);
4405 for (i = 0; i < size; i++)
4406 key[i] = (unsigned char) ssid->passphrase[i];
4408 supplicant_dbus_dict_append_fixed_array(dict,
4414 supplicant_dbus_dict_append_basic(dict,
4419 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
4420 DBUS_TYPE_UINT32, &key_index);
4424 static dbus_bool_t is_psk_raw_key(const char *psk)
4428 /* A raw key is always 64 bytes length... */
4429 if (strlen(psk) != 64)
4432 /* ... and its content is in hex representation */
4433 for (i = 0; i < 64; i++)
4434 if (!isxdigit((unsigned char) psk[i]))
4440 static unsigned char hexchar2bin(char c)
4442 if ((c >= '0') && (c <= '9'))
4444 else if ((c >= 'A') && (c <= 'F'))
4445 return c - 'A' + 10;
4446 else if ((c >= 'a') && (c <= 'f'))
4447 return c - 'a' + 10;
4452 static void hexstring2bin(const char *string, unsigned char *data,
4457 for (i = 0; i < data_len; i++)
4458 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
4459 hexchar2bin(string[i * 2 + 1]) << 0);
4462 static void add_network_security_psk(DBusMessageIter *dict,
4463 GSupplicantSSID *ssid)
4465 if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
4466 const char *key = "psk";
4468 if (is_psk_raw_key(ssid->passphrase)) {
4469 unsigned char data[32];
4470 unsigned char *datap = data;
4472 /* The above pointer alias is required by D-Bus because
4473 * with D-Bus and GCC, non-heap-allocated arrays cannot
4474 * be passed directly by their base pointer. */
4476 hexstring2bin(ssid->passphrase, datap, sizeof(data));
4478 supplicant_dbus_dict_append_fixed_array(dict,
4479 key, DBUS_TYPE_BYTE,
4480 &datap, sizeof(data));
4482 supplicant_dbus_dict_append_basic(dict,
4483 key, DBUS_TYPE_STRING,
4488 static void add_network_security_tls(DBusMessageIter *dict,
4489 GSupplicantSSID *ssid)
4492 * For TLS, we at least need:
4493 * The client certificate
4494 * The client private key file
4495 * The client private key file password
4497 * The Authority certificate is optional.
4499 if (!ssid->client_cert_path)
4502 if (!ssid->private_key_path)
4505 #if !defined TIZEN_EXT
4506 if (!ssid->private_key_passphrase)
4510 if (ssid->ca_cert_path)
4511 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4512 DBUS_TYPE_STRING, &ssid->ca_cert_path);
4514 supplicant_dbus_dict_append_basic(dict, "private_key",
4516 &ssid->private_key_path);
4517 #if !defined TIZEN_EXT
4518 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4520 &ssid->private_key_passphrase);
4522 supplicant_dbus_dict_append_basic(dict, "client_cert",
4524 &ssid->client_cert_path);
4527 static void add_network_security_peap(DBusMessageIter *dict,
4528 GSupplicantSSID *ssid)
4533 * For PEAP/TTLS, we at least need
4534 * The authority certificate
4535 * The 2nd phase authentication method
4536 * The 2nd phase passphrase
4538 * The Client certificate is optional although strongly recommended
4539 * When setting it, we need in addition
4540 * The Client private key file
4541 * The Client private key file password
4543 if (!ssid->passphrase)
4546 if (!ssid->phase2_auth)
4549 if (ssid->client_cert_path) {
4550 if (!ssid->private_key_path)
4553 #if !defined TIZEN_EXT
4554 if (!ssid->private_key_passphrase)
4558 supplicant_dbus_dict_append_basic(dict, "client_cert",
4560 &ssid->client_cert_path);
4562 supplicant_dbus_dict_append_basic(dict, "private_key",
4564 &ssid->private_key_path);
4566 #if !defined TIZEN_EXT
4567 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4569 &ssid->private_key_passphrase);
4574 if (g_str_has_prefix(ssid->phase2_auth, "EAP-")) {
4575 phase2_auth = g_strdup_printf("autheap=%s",
4576 ssid->phase2_auth + strlen("EAP-"));
4578 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
4580 supplicant_dbus_dict_append_basic(dict, "password",
4584 if (ssid->ca_cert_path)
4585 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4587 &ssid->ca_cert_path);
4589 supplicant_dbus_dict_append_basic(dict, "phase2",
4593 g_free(phase2_auth);
4596 static void add_network_security_eap(DBusMessageIter *dict,
4597 GSupplicantSSID *ssid)
4601 #if defined TIZEN_EXT
4604 if (!ssid->eap || !ssid->identity)
4608 if (g_strcmp0(ssid->eap, "tls") == 0) {
4609 add_network_security_tls(dict, ssid);
4610 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
4611 g_strcmp0(ssid->eap, "ttls") == 0) {
4612 #if defined TIZEN_EXT
4613 if (!ssid->identity)
4616 add_network_security_peap(dict, ssid);
4618 #if defined TIZEN_EXT
4619 } else if (g_strcmp0(ssid->eap, "sim") == 0 ||
4620 g_strcmp0(ssid->eap, "aka") == 0) {
4625 eap_value = g_ascii_strup(ssid->eap, -1);
4627 supplicant_dbus_dict_append_basic(dict, "eap",
4630 #if defined TIZEN_EXT
4631 if (ssid->identity != NULL)
4632 supplicant_dbus_dict_append_basic(dict, "identity",
4636 supplicant_dbus_dict_append_basic(dict, "identity",
4644 static void add_network_security_ciphers(DBusMessageIter *dict,
4645 GSupplicantSSID *ssid)
4647 unsigned int p_cipher, g_cipher, i;
4648 char *pairwise, *group;
4649 char *pair_ciphers[4];
4650 char *group_ciphers[5];
4652 p_cipher = ssid->pairwise_cipher;
4653 g_cipher = ssid->group_cipher;
4655 if (p_cipher == 0 && g_cipher == 0)
4660 if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
4661 pair_ciphers[i++] = "CCMP";
4663 if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
4664 pair_ciphers[i++] = "TKIP";
4666 if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
4667 pair_ciphers[i++] = "NONE";
4669 pair_ciphers[i] = NULL;
4673 if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
4674 group_ciphers[i++] = "CCMP";
4676 if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
4677 group_ciphers[i++] = "TKIP";
4679 if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
4680 group_ciphers[i++] = "WEP104";
4682 if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
4683 group_ciphers[i++] = "WEP40";
4685 group_ciphers[i] = NULL;
4687 pairwise = g_strjoinv(" ", pair_ciphers);
4688 group = g_strjoinv(" ", group_ciphers);
4690 SUPPLICANT_DBG("cipher %s %s", pairwise, group);
4692 supplicant_dbus_dict_append_basic(dict, "pairwise",
4695 supplicant_dbus_dict_append_basic(dict, "group",
4703 static void add_network_security_proto(DBusMessageIter *dict,
4704 GSupplicantSSID *ssid)
4706 unsigned int protocol, i;
4710 protocol = ssid->protocol;
4717 if (protocol & G_SUPPLICANT_PROTO_RSN)
4718 protos[i++] = "RSN";
4720 if (protocol & G_SUPPLICANT_PROTO_WPA)
4721 protos[i++] = "WPA";
4725 proto = g_strjoinv(" ", protos);
4727 SUPPLICANT_DBG("proto %s", proto);
4729 supplicant_dbus_dict_append_basic(dict, "proto",
4736 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
4740 switch (ssid->security) {
4741 case G_SUPPLICANT_SECURITY_NONE:
4743 add_network_security_none(dict);
4744 add_network_security_ciphers(dict, ssid);
4746 case G_SUPPLICANT_SECURITY_UNKNOWN:
4747 case G_SUPPLICANT_SECURITY_WEP:
4749 add_network_security_wep(dict, ssid);
4750 add_network_security_ciphers(dict, ssid);
4752 case G_SUPPLICANT_SECURITY_PSK:
4753 key_mgmt = "WPA-PSK";
4754 add_network_security_psk(dict, ssid);
4755 add_network_security_ciphers(dict, ssid);
4756 add_network_security_proto(dict, ssid);
4758 case G_SUPPLICANT_SECURITY_IEEE8021X:
4759 key_mgmt = "WPA-EAP";
4760 add_network_security_eap(dict, ssid);
4761 add_network_security_ciphers(dict, ssid);
4762 add_network_security_proto(dict, ssid);
4766 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
4767 DBUS_TYPE_STRING, &key_mgmt);
4770 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
4774 switch (ssid->mode) {
4775 case G_SUPPLICANT_MODE_UNKNOWN:
4776 case G_SUPPLICANT_MODE_INFRA:
4779 case G_SUPPLICANT_MODE_IBSS:
4782 case G_SUPPLICANT_MODE_MASTER:
4787 supplicant_dbus_dict_append_basic(dict, "mode",
4788 DBUS_TYPE_UINT32, &mode);
4791 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
4793 DBusMessageIter dict;
4794 struct interface_connect_data *data = user_data;
4795 GSupplicantSSID *ssid = data->ssid;
4797 supplicant_dbus_dict_open(iter, &dict);
4799 if (ssid->scan_ssid)
4800 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
4801 DBUS_TYPE_UINT32, &ssid->scan_ssid);
4804 supplicant_dbus_dict_append_basic(&dict, "frequency",
4805 DBUS_TYPE_UINT32, &ssid->freq);
4808 supplicant_dbus_dict_append_basic(&dict, "bgscan",
4809 DBUS_TYPE_STRING, &ssid->bgscan);
4811 add_network_mode(&dict, ssid);
4813 add_network_security(&dict, ssid);
4815 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
4816 DBUS_TYPE_BYTE, &ssid->ssid,
4819 supplicant_dbus_dict_append_basic(&dict, "ignore_broadcast_ssid",
4821 &ssid->ignore_broadcast_ssid);
4823 supplicant_dbus_dict_close(iter, &dict);
4826 static void interface_wps_start_result(const char *error,
4827 DBusMessageIter *iter, void *user_data)
4829 struct interface_connect_data *data = user_data;
4836 SUPPLICANT_DBG("error: %s", error);
4837 err = parse_supplicant_error(iter);
4841 data->callback(err, data->interface, data->user_data);
4848 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
4850 struct interface_connect_data *data = user_data;
4851 GSupplicantSSID *ssid = data->ssid;
4852 const char *role = "enrollee", *type;
4853 DBusMessageIter dict;
4857 supplicant_dbus_dict_open(iter, &dict);
4859 supplicant_dbus_dict_append_basic(&dict, "Role",
4860 DBUS_TYPE_STRING, &role);
4863 if (ssid->pin_wps) {
4865 supplicant_dbus_dict_append_basic(&dict, "Pin",
4866 DBUS_TYPE_STRING, &ssid->pin_wps);
4869 supplicant_dbus_dict_append_basic(&dict, "Type",
4870 DBUS_TYPE_STRING, &type);
4872 #if defined TIZEN_EXT
4874 supplicant_dbus_dict_append_fixed_array(&dict, "Bssid",
4875 DBUS_TYPE_BYTE, &ssid->bssid, 6);
4878 supplicant_dbus_dict_close(iter, &dict);
4881 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
4883 struct interface_connect_data *data = user_data;
4888 SUPPLICANT_DBG("error: %s", error);
4894 #if defined TIZEN_EXT
4895 GSupplicantSSID *ssid = data->ssid;
4896 if (ssid->pin_wps != NULL) {
4897 if (!g_utf8_validate(ssid->pin_wps, 8, NULL)) {
4898 SUPPLICANT_DBG("Invalid characters in WPS_PIN");
4905 supplicant_dbus_method_call(data->interface->path,
4906 SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
4907 interface_add_wps_params,
4908 interface_wps_start_result, data, NULL);
4911 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
4913 dbus_bool_t credentials = TRUE;
4917 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
4921 int g_supplicant_interface_connect(GSupplicantInterface *interface,
4922 GSupplicantSSID *ssid,
4923 GSupplicantInterfaceCallback callback,
4926 struct interface_connect_data *data;
4932 if (!system_available)
4935 /* TODO: Check if we're already connected and switch */
4937 data = dbus_malloc0(sizeof(*data));
4941 data->interface = interface;
4942 data->path = g_strdup(interface->path);
4943 data->callback = callback;
4945 data->user_data = user_data;
4947 if (ssid->use_wps) {
4948 g_free(interface->wps_cred.key);
4949 memset(&interface->wps_cred, 0,
4950 sizeof(struct _GSupplicantWpsCredentials));
4952 ret = supplicant_dbus_property_set(interface->path,
4953 SUPPLICANT_INTERFACE ".Interface.WPS",
4954 "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
4955 wps_process_credentials, wps_start, data, interface);
4957 ret = supplicant_dbus_method_call(interface->path,
4958 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
4959 interface_add_network_params,
4960 interface_add_network_result, data,
4969 return -EINPROGRESS;
4972 static void network_remove_result(const char *error,
4973 DBusMessageIter *iter, void *user_data)
4975 struct interface_data *data = user_data;
4982 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
4984 result = -ECONNABORTED;
4990 data->callback(result, data->interface, data->user_data);
4995 static void network_remove_params(DBusMessageIter *iter, void *user_data)
4997 struct interface_data *data = user_data;
4998 const char *path = data->interface->network_path;
5000 SUPPLICANT_DBG("path %s", path);
5002 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
5005 static int network_remove(struct interface_data *data)
5007 GSupplicantInterface *interface = data->interface;
5011 #if defined TIZEN_EXT
5012 GSupplicantInterface *intf = NULL;
5014 * Check if 'interface' is valid
5016 intf = g_hash_table_lookup(interface_table, interface->path);
5021 return supplicant_dbus_method_call(interface->path,
5022 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
5023 network_remove_params, network_remove_result, data,
5027 static void interface_disconnect_result(const char *error,
5028 DBusMessageIter *iter, void *user_data)
5030 struct interface_data *data = user_data;
5037 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
5039 result = -ECONNABORTED;
5042 if (result < 0 && data->callback) {
5043 data->callback(result, data->interface, data->user_data);
5044 data->callback = NULL;
5047 /* If we are disconnecting from previous WPS successful
5048 * association. i.e.: it did not went through AddNetwork,
5049 * and interface->network_path was never set. */
5050 if (!data->interface->network_path) {
5056 if (result != -ECONNABORTED) {
5057 if (network_remove(data) < 0) {
5067 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
5068 GSupplicantInterfaceCallback callback,
5071 struct interface_data *data;
5079 if (!system_available)
5082 data = dbus_malloc0(sizeof(*data));
5086 data->interface = interface;
5087 data->path = g_strdup(interface->path);
5088 data->callback = callback;
5089 data->user_data = user_data;
5091 ret = supplicant_dbus_method_call(interface->path,
5092 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
5093 NULL, interface_disconnect_result, data,
5104 static void interface_p2p_find_result(const char *error,
5105 DBusMessageIter *iter, void *user_data)
5107 struct interface_scan_data *data = user_data;
5110 SUPPLICANT_DBG("error %s", error);
5115 if (interface_exists(data->interface, data->path)) {
5116 if (!data->interface->ready)
5119 data->interface->p2p_finding = true;
5123 data->callback(err, data->interface, data->user_data);
5129 static void interface_p2p_find_params(DBusMessageIter *iter, void *user_data)
5131 DBusMessageIter dict;
5133 supplicant_dbus_dict_open(iter, &dict);
5134 supplicant_dbus_dict_close(iter, &dict);
5137 int g_supplicant_interface_p2p_find(GSupplicantInterface *interface,
5138 GSupplicantInterfaceCallback callback,
5141 struct interface_scan_data *data;
5144 if (!interface->p2p_support)
5147 ret = interface_ready_to_scan(interface);
5148 if (ret && ret != -EALREADY)
5151 data = dbus_malloc0(sizeof(*data));
5155 data->interface = interface;
5156 data->path = g_strdup(interface->path);
5157 data->callback = callback;
5158 data->user_data = user_data;
5160 ret = supplicant_dbus_method_call(interface->path,
5161 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Find",
5162 interface_p2p_find_params, interface_p2p_find_result,
5172 bool g_supplicant_interface_is_p2p_finding(GSupplicantInterface *interface)
5177 return interface->p2p_finding;
5180 int g_supplicant_interface_p2p_stop_find(GSupplicantInterface *interface)
5182 if (!interface->p2p_finding)
5187 interface->p2p_finding = false;
5189 return supplicant_dbus_method_call(interface->path,
5190 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "StopFind",
5191 NULL, NULL, NULL, NULL);
5194 static void interface_p2p_connect_result(const char *error,
5195 DBusMessageIter *iter, void *user_data)
5197 struct interface_connect_data *data = user_data;
5203 err = parse_supplicant_error(iter);
5206 data->callback(err, data->interface, data->user_data);
5209 g_free(data->peer->wps_pin);
5210 g_free(data->peer->path);
5215 static void interface_p2p_connect_params(DBusMessageIter *iter, void *user_data)
5217 struct interface_connect_data *data = user_data;
5218 const char *wps = "pbc";
5219 DBusMessageIter dict;
5224 supplicant_dbus_dict_open(iter, &dict);
5226 if (data->peer->master)
5229 if (data->peer->wps_pin)
5232 supplicant_dbus_dict_append_basic(&dict, "peer",
5233 DBUS_TYPE_OBJECT_PATH, &data->peer->path);
5234 supplicant_dbus_dict_append_basic(&dict, "wps_method",
5235 DBUS_TYPE_STRING, &wps);
5236 if (data->peer->wps_pin) {
5237 supplicant_dbus_dict_append_basic(&dict, "pin",
5238 DBUS_TYPE_STRING, &data->peer->wps_pin);
5241 supplicant_dbus_dict_append_basic(&dict, "go_intent",
5242 DBUS_TYPE_INT32, &go_intent);
5244 supplicant_dbus_dict_close(iter, &dict);
5247 int g_supplicant_interface_p2p_connect(GSupplicantInterface *interface,
5248 GSupplicantPeerParams *peer_params,
5249 GSupplicantInterfaceCallback callback,
5252 struct interface_connect_data *data;
5257 if (!interface->p2p_support)
5260 data = dbus_malloc0(sizeof(*data));
5261 data->interface = interface;
5262 data->path = g_strdup(interface->path);
5263 data->peer = peer_params;
5264 data->callback = callback;
5265 data->user_data = user_data;
5267 ret = supplicant_dbus_method_call(interface->path,
5268 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Connect",
5269 interface_p2p_connect_params, interface_p2p_connect_result,
5277 return -EINPROGRESS;
5280 int g_supplicant_interface_p2p_disconnect(GSupplicantInterface *interface,
5281 GSupplicantPeerParams *peer_params)
5283 GSupplicantPeer *peer;
5289 if (!interface->p2p_support)
5292 peer = g_hash_table_lookup(interface->peer_table, peer_params->path);
5296 for (list = peer->groups; list; list = list->next, count++) {
5297 const char *group_obj_path = list->data;
5298 GSupplicantInterface *g_interface;
5299 GSupplicantGroup *group;
5301 group = g_hash_table_lookup(group_mapping, group_obj_path);
5302 if (!group || !group->interface)
5305 g_interface = group->interface;
5306 supplicant_dbus_method_call(g_interface->path,
5307 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5308 "Disconnect", NULL, NULL, NULL, g_interface);
5311 if (count == 0 && peer->current_group_iface) {
5312 supplicant_dbus_method_call(peer->current_group_iface->path,
5313 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5314 "Disconnect", NULL, NULL, NULL,
5315 peer->current_group_iface->path);
5318 peer->current_group_iface = NULL;
5320 return -EINPROGRESS;
5323 struct p2p_service_data {
5325 GSupplicantInterface *interface;
5326 GSupplicantP2PServiceParams *service;
5327 GSupplicantInterfaceCallback callback;
5331 static void interface_p2p_service_result(const char *error,
5332 DBusMessageIter *iter, void *user_data)
5334 struct p2p_service_data *data = user_data;
5337 SUPPLICANT_DBG("%s result - %s", data->registration ?
5338 "Registration" : "Deletion",
5339 error ? error : "Success");
5344 data->callback(result, data->interface, data->user_data);
5346 g_free(data->service->query);
5347 g_free(data->service->response);
5348 g_free(data->service->service);
5349 g_free(data->service->wfd_ies);
5350 g_free(data->service);
5354 static void interface_p2p_service_params(DBusMessageIter *iter,
5357 struct p2p_service_data *data = user_data;
5358 GSupplicantP2PServiceParams *service;
5359 DBusMessageIter dict;
5364 service = data->service;
5366 supplicant_dbus_dict_open(iter, &dict);
5368 if (service->query && service->response) {
5370 supplicant_dbus_dict_append_basic(&dict, "service_type",
5371 DBUS_TYPE_STRING, &type);
5372 supplicant_dbus_dict_append_fixed_array(&dict, "query",
5373 DBUS_TYPE_BYTE, &service->query,
5374 service->query_length);
5375 supplicant_dbus_dict_append_fixed_array(&dict, "response",
5376 DBUS_TYPE_BYTE, &service->response,
5377 service->response_length);
5378 } else if (service->version && service->service) {
5380 supplicant_dbus_dict_append_basic(&dict, "service_type",
5381 DBUS_TYPE_STRING, &type);
5382 supplicant_dbus_dict_append_basic(&dict, "version",
5383 DBUS_TYPE_INT32, &service->version);
5384 supplicant_dbus_dict_append_basic(&dict, "service",
5385 DBUS_TYPE_STRING, &service->service);
5388 supplicant_dbus_dict_close(iter, &dict);
5391 int g_supplicant_interface_p2p_add_service(GSupplicantInterface *interface,
5392 GSupplicantInterfaceCallback callback,
5393 GSupplicantP2PServiceParams *p2p_service_params,
5396 struct p2p_service_data *data;
5401 if (!interface->p2p_support)
5404 data = dbus_malloc0(sizeof(*data));
5405 data->registration = true;
5406 data->interface = interface;
5407 data->service = p2p_service_params;
5408 data->callback = callback;
5409 data->user_data = user_data;
5411 ret = supplicant_dbus_method_call(interface->path,
5412 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "AddService",
5413 interface_p2p_service_params, interface_p2p_service_result,
5420 return -EINPROGRESS;
5423 int g_supplicant_interface_p2p_del_service(GSupplicantInterface *interface,
5424 GSupplicantP2PServiceParams *p2p_service_params)
5426 struct p2p_service_data *data;
5431 if (!interface->p2p_support)
5434 data = dbus_malloc0(sizeof(*data));
5435 data->interface = interface;
5436 data->service = p2p_service_params;
5438 ret = supplicant_dbus_method_call(interface->path,
5439 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeleteService",
5440 interface_p2p_service_params, interface_p2p_service_result,
5447 return -EINPROGRESS;
5450 struct p2p_listen_data {
5455 static void interface_p2p_listen_params(DBusMessageIter *iter, void *user_data)
5457 struct p2p_listen_data *params = user_data;
5458 DBusMessageIter dict;
5460 supplicant_dbus_dict_open(iter, &dict);
5462 supplicant_dbus_dict_append_basic(&dict, "period",
5463 DBUS_TYPE_INT32, ¶ms->period);
5464 supplicant_dbus_dict_append_basic(&dict, "interval",
5465 DBUS_TYPE_INT32, ¶ms->interval);
5466 supplicant_dbus_dict_close(iter, &dict);
5469 int g_supplicant_interface_p2p_listen(GSupplicantInterface *interface,
5470 int period, int interval)
5472 struct p2p_listen_data params;
5476 if (!interface->p2p_support)
5479 params.period = period;
5480 params.interval = interval;
5482 return supplicant_dbus_method_call(interface->path,
5483 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5484 "ExtendedListen", interface_p2p_listen_params,
5485 NULL, ¶ms, NULL);
5488 static void widi_ies_params(DBusMessageIter *iter, void *user_data)
5490 struct p2p_service_data *data = user_data;
5491 GSupplicantP2PServiceParams *service = data->service;
5492 DBusMessageIter array;
5494 SUPPLICANT_DBG("%p - %d", service->wfd_ies, service->wfd_ies_length);
5496 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
5497 DBUS_TYPE_BYTE_AS_STRING, &array);
5499 if (service->wfd_ies && service->wfd_ies_length > 0) {
5500 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
5501 &service->wfd_ies, service->wfd_ies_length);
5504 dbus_message_iter_close_container(iter, &array);
5507 int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
5508 GSupplicantInterfaceCallback callback,
5511 struct p2p_service_data *data;
5516 if (!system_available)
5519 data = dbus_malloc0(sizeof(*data));
5520 data->service = p2p_service_params;
5521 data->callback = callback;
5522 data->user_data = user_data;
5524 if (p2p_service_params->wfd_ies)
5525 data->registration = true;
5527 ret = supplicant_dbus_property_set(SUPPLICANT_PATH,
5528 SUPPLICANT_INTERFACE, "WFDIEs",
5529 DBUS_TYPE_ARRAY_AS_STRING
5530 DBUS_TYPE_BYTE_AS_STRING,
5532 interface_p2p_service_result,
5534 if (ret < 0 && ret != -EINPROGRESS) {
5539 return -EINPROGRESS;
5543 static const char *g_supplicant_rule0 = "type=signal,"
5544 "path=" DBUS_PATH_DBUS ","
5545 "sender=" DBUS_SERVICE_DBUS ","
5546 "interface=" DBUS_INTERFACE_DBUS ","
5547 "member=NameOwnerChanged,"
5548 "arg0=" SUPPLICANT_SERVICE;
5549 static const char *g_supplicant_rule1 = "type=signal,"
5550 "interface=" SUPPLICANT_INTERFACE;
5551 static const char *g_supplicant_rule2 = "type=signal,"
5552 "interface=" SUPPLICANT_INTERFACE ".Interface";
5553 static const char *g_supplicant_rule3 = "type=signal,"
5554 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
5555 static const char *g_supplicant_rule4 = "type=signal,"
5556 "interface=" SUPPLICANT_INTERFACE ".BSS";
5557 static const char *g_supplicant_rule5 = "type=signal,"
5558 "interface=" SUPPLICANT_INTERFACE ".Network";
5559 #if !defined TIZEN_EXT
5560 static const char *g_supplicant_rule6 = "type=signal,"
5561 "interface=" SUPPLICANT_INTERFACE ".Interface.P2PDevice";
5562 static const char *g_supplicant_rule7 = "type=signal,"
5563 "interface=" SUPPLICANT_INTERFACE ".Peer";
5564 static const char *g_supplicant_rule8 = "type=signal,"
5565 "interface=" SUPPLICANT_INTERFACE ".Group";
5568 static void invoke_introspect_method(void)
5570 DBusMessage *message;
5572 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
5574 DBUS_INTERFACE_INTROSPECTABLE,
5580 dbus_message_set_no_reply(message, TRUE);
5581 dbus_connection_send(connection, message, NULL);
5582 dbus_message_unref(message);
5585 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
5587 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
5591 if (!dbus_connection_add_filter(connection, g_supplicant_filter,
5593 dbus_connection_unref(connection);
5598 callbacks_pointer = callbacks;
5601 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
5602 NULL, remove_interface);
5604 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5606 peer_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5608 group_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5610 pending_peer_connection = g_hash_table_new_full(g_str_hash, g_str_equal,
5613 supplicant_dbus_setup(connection);
5615 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
5616 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
5617 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
5618 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
5619 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
5620 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
5621 #if defined TIZEN_EXT
5622 dbus_bus_add_match(connection,
5623 "type=signal,interface=org.tizen.system.deviced.PowerOff,"
5624 "member=ChangeState", NULL);
5626 #if !defined TIZEN_EXT
5627 dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
5628 dbus_bus_add_match(connection, g_supplicant_rule7, NULL);
5629 dbus_bus_add_match(connection, g_supplicant_rule8, NULL);
5631 dbus_connection_flush(connection);
5633 if (dbus_bus_name_has_owner(connection,
5634 SUPPLICANT_SERVICE, NULL)) {
5635 system_available = TRUE;
5636 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
5637 SUPPLICANT_INTERFACE,
5638 service_property, NULL, NULL);
5640 invoke_introspect_method();
5645 static void unregister_interface_remove_params(DBusMessageIter *iter,
5648 const char *path = user_data;
5650 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
5655 static void unregister_remove_interface(gpointer key, gpointer value,
5658 GSupplicantInterface *interface = value;
5660 supplicant_dbus_method_call(SUPPLICANT_PATH,
5661 SUPPLICANT_INTERFACE,
5663 unregister_interface_remove_params,
5664 NULL, interface->path, NULL);
5667 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
5672 #if !defined TIZEN_EXT
5673 dbus_bus_remove_match(connection, g_supplicant_rule8, NULL);
5674 dbus_bus_remove_match(connection, g_supplicant_rule7, NULL);
5675 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
5677 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
5678 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
5679 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
5680 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
5681 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
5682 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
5683 dbus_connection_flush(connection);
5685 dbus_connection_remove_filter(connection,
5686 g_supplicant_filter, NULL);
5690 g_hash_table_destroy(bss_mapping);
5695 g_hash_table_destroy(peer_mapping);
5696 peer_mapping = NULL;
5699 if (group_mapping) {
5700 g_hash_table_destroy(group_mapping);
5701 group_mapping = NULL;
5704 if (interface_table) {
5705 g_hash_table_foreach(interface_table,
5706 unregister_remove_interface, NULL);
5707 g_hash_table_destroy(interface_table);
5708 interface_table = NULL;
5711 if (system_available)
5712 callback_system_killed();
5715 dbus_connection_unref(connection);
5719 callbacks_pointer = NULL;