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)
1297 static void merge_network(GSupplicantNetwork *network)
1300 const char *ssid, *mode, *key_mgmt;
1301 #if defined TIZEN_EXT
1302 GSupplicantInterface *interface;
1303 const char *isHS20AP;
1304 const char *eap, *identity, *phase2;
1306 unsigned int i, ssid_len;
1309 ssid = g_hash_table_lookup(network->config_table, "ssid");
1310 mode = g_hash_table_lookup(network->config_table, "mode");
1311 key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
1312 #if defined TIZEN_EXT
1313 isHS20AP = g_hash_table_lookup(network->config_table, "isHS20AP");
1314 eap = g_hash_table_lookup(network->config_table, "eap");
1315 identity = g_hash_table_lookup(network->config_table, "identity");
1316 phase2 = g_hash_table_lookup(network->config_table, "phase2");
1317 interface = network->interface;
1320 SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
1323 ssid_len = strlen(ssid);
1327 str = g_string_sized_new((ssid_len * 2) + 24);
1331 for (i = 0; i < ssid_len; i++)
1332 #if defined TIZEN_EXT
1336 g_string_append_printf(str, "%02x", ssid[i]);
1337 #if defined TIZEN_EXT
1341 if (g_strcmp0(mode, "0") == 0)
1342 g_string_append_printf(str, "_managed");
1343 else if (g_strcmp0(mode, "1") == 0)
1344 g_string_append_printf(str, "_adhoc");
1346 if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
1347 g_string_append_printf(str, "_psk");
1348 #if defined TIZEN_EXT
1349 else if (g_strcmp0(key_mgmt, "WPA-EAP") == 0)
1350 g_string_append_printf(str, "_ieee8021x");
1352 g_string_append_printf(str, "_none");
1355 group = g_string_free(str, FALSE);
1357 SUPPLICANT_DBG("%s", group);
1359 #if defined TIZEN_EXT
1360 if (g_strcmp0(isHS20AP, "1") == 0) {
1361 network->isHS20AP = 1;
1363 g_free(network->eap);
1364 network->eap = g_strdup(eap);
1366 if (network->identity)
1367 g_free(network->identity);
1368 network->identity = g_strdup(identity);
1370 if (network->phase2)
1371 g_free(network->phase2);
1372 network->phase2 = g_strdup(phase2);
1374 network->isHS20AP = 0;
1377 interface->network_path = g_strdup(network->path);
1379 network->group = g_strdup(group);
1380 callback_network_merged(network);
1385 g_hash_table_destroy(network->config_table);
1387 g_free(network->path);
1391 static void network_property(const char *key, DBusMessageIter *iter,
1394 GSupplicantNetwork *network = user_data;
1396 if (!network->interface)
1400 merge_network(network);
1404 if (g_strcmp0(key, "Enabled") == 0) {
1405 dbus_bool_t enabled = FALSE;
1407 dbus_message_iter_get_basic(iter, &enabled);
1408 } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
1409 const char *str = NULL;
1411 dbus_message_iter_get_basic(iter, &str);
1413 g_hash_table_replace(network->config_table,
1414 g_strdup(key), g_strdup(str));
1417 SUPPLICANT_DBG("key %s type %c",
1418 key, dbus_message_iter_get_arg_type(iter));
1421 static void interface_network_added(DBusMessageIter *iter, void *user_data)
1423 GSupplicantInterface *interface = user_data;
1424 GSupplicantNetwork *network;
1425 const char *path = NULL;
1429 dbus_message_iter_get_basic(iter, &path);
1433 if (g_strcmp0(path, "/") == 0)
1436 network = g_hash_table_lookup(interface->net_mapping, path);
1440 network = g_try_new0(GSupplicantNetwork, 1);
1444 network->interface = interface;
1445 network->path = g_strdup(path);
1447 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1450 dbus_message_iter_next(iter);
1451 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1452 supplicant_dbus_property_foreach(iter, network_property,
1454 network_property(NULL, NULL, network);
1458 supplicant_dbus_property_get_all(path,
1459 SUPPLICANT_INTERFACE ".Network",
1460 network_property, network, NULL);
1463 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1465 GSupplicantInterface *interface = user_data;
1466 GSupplicantNetwork *network;
1467 const char *path = NULL;
1469 dbus_message_iter_get_basic(iter, &path);
1473 network = g_hash_table_lookup(interface->net_mapping, path);
1477 g_hash_table_remove(interface->net_mapping, path);
1480 static char *create_name(unsigned char *ssid, int ssid_len)
1483 const gchar *remainder, *invalid;
1484 int valid_bytes, remaining_bytes;
1486 if (ssid_len < 1 || ssid[0] == '\0')
1487 return g_strdup("");
1490 remainder = (const gchar *)ssid;
1491 remaining_bytes = ssid_len;
1493 while (remaining_bytes != 0) {
1494 if (g_utf8_validate(remainder, remaining_bytes,
1499 valid_bytes = invalid - remainder;
1502 string = g_string_sized_new(remaining_bytes);
1504 g_string_append_len(string, remainder, valid_bytes);
1506 /* append U+FFFD REPLACEMENT CHARACTER */
1507 g_string_append(string, "\357\277\275");
1509 remaining_bytes -= valid_bytes + 1;
1510 remainder = invalid + 1;
1514 return g_strndup((const gchar *)ssid, ssid_len + 1);
1516 g_string_append(string, remainder);
1518 return g_string_free(string, FALSE);
1521 static char *create_group(struct g_supplicant_bss *bss)
1525 const char *mode, *security;
1527 str = g_string_sized_new((bss->ssid_len * 2) + 24);
1531 if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1532 for (i = 0; i < bss->ssid_len; i++)
1533 g_string_append_printf(str, "%02x", bss->ssid[i]);
1535 g_string_append_printf(str, "hidden");
1537 mode = mode2string(bss->mode);
1539 g_string_append_printf(str, "_%s", mode);
1541 security = security2string(bss->security);
1543 g_string_append_printf(str, "_%s", security);
1545 return g_string_free(str, FALSE);
1548 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1550 GSupplicantInterface *interface = bss->interface;
1551 GSupplicantNetwork *network;
1554 group = create_group(bss);
1555 SUPPLICANT_DBG("New group created: %s", group);
1560 network = g_hash_table_lookup(interface->network_table, group);
1563 SUPPLICANT_DBG("Network %s already exist", network->name);
1568 network = g_try_new0(GSupplicantNetwork, 1);
1574 network->interface = interface;
1576 network->path = g_strdup(bss->path);
1577 network->group = group;
1578 network->name = create_name(bss->ssid, bss->ssid_len);
1579 network->mode = bss->mode;
1580 network->security = bss->security;
1581 network->ssid_len = bss->ssid_len;
1582 memcpy(network->ssid, bss->ssid, bss->ssid_len);
1583 network->signal = bss->signal;
1584 network->frequency = bss->frequency;
1585 network->best_bss = bss;
1587 SUPPLICANT_DBG("New network %s created", network->name);
1589 network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1592 network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1595 g_hash_table_replace(interface->network_table,
1596 network->group, network);
1598 callback_network_added(network);
1601 /* We update network's WPS properties if only bss provides WPS. */
1602 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1603 network->wps = TRUE;
1604 network->wps_capabilities |= bss->wps_capabilities;
1607 if (bss->signal > network->signal) {
1608 network->signal = bss->signal;
1609 network->best_bss = bss;
1610 callback_network_changed(network, "Signal");
1613 g_hash_table_replace(interface->bss_mapping, bss->path, network);
1614 g_hash_table_replace(network->bss_table, bss->path, bss);
1616 g_hash_table_replace(bss_mapping, bss->path, interface);
1619 static void bss_rates(DBusMessageIter *iter, void *user_data)
1621 struct g_supplicant_bss *bss = user_data;
1622 dbus_uint32_t rate = 0;
1624 dbus_message_iter_get_basic(iter, &rate);
1628 if (rate > bss->maxrate)
1629 bss->maxrate = rate;
1632 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1634 unsigned int *keymgmt = user_data;
1635 const char *str = NULL;
1638 dbus_message_iter_get_basic(iter, &str);
1642 for (i = 0; keymgmt_map[i].str; i++)
1643 if (strcmp(str, keymgmt_map[i].str) == 0) {
1644 SUPPLICANT_DBG("Keymgmt: %s", str);
1645 *keymgmt |= keymgmt_map[i].val;
1650 static void bss_group(DBusMessageIter *iter, void *user_data)
1652 unsigned int *group = user_data;
1653 const char *str = NULL;
1656 dbus_message_iter_get_basic(iter, &str);
1660 for (i = 0; group_map[i].str; i++)
1661 if (strcmp(str, group_map[i].str) == 0) {
1662 SUPPLICANT_DBG("Group: %s", str);
1663 *group |= group_map[i].val;
1668 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1670 unsigned int *pairwise = user_data;
1671 const char *str = NULL;
1674 dbus_message_iter_get_basic(iter, &str);
1678 for (i = 0; pairwise_map[i].str; i++)
1679 if (strcmp(str, pairwise_map[i].str) == 0) {
1680 SUPPLICANT_DBG("Pairwise: %s", str);
1681 *pairwise |= pairwise_map[i].val;
1686 static void bss_wpa(const char *key, DBusMessageIter *iter,
1689 struct g_supplicant_bss *bss = user_data;
1690 unsigned int value = 0;
1692 SUPPLICANT_DBG("Key: %s", key);
1694 if (g_strcmp0(key, "KeyMgmt") == 0) {
1695 supplicant_dbus_array_foreach(iter, bss_keymgmt, &value);
1697 if (bss->rsn_selected)
1698 bss->rsn_keymgmt = value;
1700 bss->wpa_keymgmt = value;
1701 } else if (g_strcmp0(key, "Group") == 0) {
1702 supplicant_dbus_array_foreach(iter, bss_group, &value);
1704 if (bss->rsn_selected)
1705 bss->rsn_group = value;
1707 bss->wpa_group = value;
1708 } else if (g_strcmp0(key, "Pairwise") == 0) {
1709 supplicant_dbus_array_foreach(iter, bss_pairwise, &value);
1711 if (bss->rsn_selected)
1712 bss->rsn_pairwise = value;
1714 bss->wpa_pairwise = value;
1718 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1721 unsigned int len = 0;
1723 while (len + 4 < ie_size) {
1724 unsigned int hi = ie[len];
1725 unsigned int lo = ie[len + 1];
1726 unsigned int tmp_type = (hi << 8) + lo;
1727 unsigned int v_len = 0;
1729 /* hi and lo are used to recreate an unsigned int
1730 * based on 2 8bits length unsigned int. */
1734 v_len = (hi << 8) + lo;
1736 if (tmp_type == type) {
1737 unsigned int ret_value = 0;
1738 unsigned char *value = (unsigned char *)&ret_value;
1740 SUPPLICANT_DBG("IE: match type 0x%x", type);
1742 /* Verifying length relevance */
1743 if (v_len > sizeof(unsigned int) ||
1744 len + 4 + v_len > ie_size)
1747 memcpy(value, ie + len + 4, v_len);
1749 SUPPLICANT_DBG("returning 0x%x", ret_value);
1756 SUPPLICANT_DBG("returning 0");
1760 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1762 struct g_supplicant_bss *bss = user_data;
1763 const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1764 unsigned char *ie, *ie_end;
1765 DBusMessageIter array;
1769 #define WMM_WPA1_WPS_INFO 221
1770 #define WPS_INFO_MIN_LEN 6
1771 #define WPS_VERSION_TLV 0x104A
1772 #define WPS_STATE_TLV 0x1044
1773 #define WPS_METHODS_TLV 0x1012
1774 #define WPS_REGISTRAR_TLV 0x1041
1775 #define WPS_VERSION 0x10
1776 #define WPS_PBC 0x04
1777 #define WPS_PIN 0x00
1778 #define WPS_CONFIGURED 0x02
1780 dbus_message_iter_recurse(iter, &array);
1781 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1783 if (!ie || ie_len < 2)
1786 bss->wps_capabilities = 0;
1789 for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1792 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1793 memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1796 SUPPLICANT_DBG("IE: match WPS_OUI");
1798 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1799 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1801 bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1803 if (value == WPS_CONFIGURED)
1804 bss->wps_capabilities |=
1805 G_SUPPLICANT_WPS_CONFIGURED;
1808 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1810 if (GUINT16_FROM_BE(value) == WPS_PBC)
1811 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1812 if (GUINT16_FROM_BE(value) == WPS_PIN)
1813 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1815 bss->wps_capabilities |=
1816 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1818 /* If the AP sends this it means it's advertizing
1819 * as a registrar and the WPS process is launched
1821 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1822 bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1824 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1828 static void bss_compute_security(struct g_supplicant_bss *bss)
1831 * Combining RSN and WPA keymgmt
1832 * We combine it since parsing IEs might have set something for WPS. */
1833 bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt;
1835 bss->ieee8021x = FALSE;
1839 (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1840 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1841 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1842 bss->ieee8021x = TRUE;
1845 (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1846 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1847 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1851 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1853 bss->security = G_SUPPLICANT_SECURITY_PSK;
1854 else if (bss->privacy)
1855 bss->security = G_SUPPLICANT_SECURITY_WEP;
1857 bss->security = G_SUPPLICANT_SECURITY_NONE;
1861 static void bss_property(const char *key, DBusMessageIter *iter,
1864 struct g_supplicant_bss *bss = user_data;
1866 if (!bss->interface)
1869 SUPPLICANT_DBG("key %s", key);
1874 if (g_strcmp0(key, "BSSID") == 0) {
1875 DBusMessageIter array;
1876 unsigned char *addr;
1879 dbus_message_iter_recurse(iter, &array);
1880 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1883 memcpy(bss->bssid, addr, addr_len);
1884 } else if (g_strcmp0(key, "SSID") == 0) {
1885 DBusMessageIter array;
1886 unsigned char *ssid;
1889 dbus_message_iter_recurse(iter, &array);
1890 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1892 if (ssid_len > 0 && ssid_len < 33) {
1893 memcpy(bss->ssid, ssid, ssid_len);
1894 bss->ssid_len = ssid_len;
1896 memset(bss->ssid, 0, sizeof(bss->ssid));
1899 } else if (g_strcmp0(key, "Capabilities") == 0) {
1900 dbus_uint16_t capabilities = 0x0000;
1902 dbus_message_iter_get_basic(iter, &capabilities);
1904 if (capabilities & IEEE80211_CAP_ESS)
1905 bss->mode = G_SUPPLICANT_MODE_INFRA;
1906 else if (capabilities & IEEE80211_CAP_IBSS)
1907 bss->mode = G_SUPPLICANT_MODE_IBSS;
1909 if (capabilities & IEEE80211_CAP_PRIVACY)
1910 bss->privacy = TRUE;
1911 } else if (g_strcmp0(key, "Mode") == 0) {
1912 const char *mode = NULL;
1914 dbus_message_iter_get_basic(iter, &mode);
1915 bss->mode = string2mode(mode);
1916 } else if (g_strcmp0(key, "Frequency") == 0) {
1917 dbus_uint16_t frequency = 0;
1919 dbus_message_iter_get_basic(iter, &frequency);
1920 bss->frequency = frequency;
1921 } else if (g_strcmp0(key, "Signal") == 0) {
1922 dbus_int16_t signal = 0;
1924 dbus_message_iter_get_basic(iter, &signal);
1926 bss->signal = signal;
1928 bss->signal = BSS_UNKNOWN_STRENGTH;
1930 } else if (g_strcmp0(key, "Level") == 0) {
1931 dbus_int32_t level = 0;
1933 dbus_message_iter_get_basic(iter, &level);
1934 } else if (g_strcmp0(key, "Rates") == 0) {
1935 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1936 } else if (g_strcmp0(key, "MaxRate") == 0) {
1937 dbus_uint32_t maxrate = 0;
1939 dbus_message_iter_get_basic(iter, &maxrate);
1941 bss->maxrate = maxrate;
1942 } else if (g_strcmp0(key, "Privacy") == 0) {
1943 dbus_bool_t privacy = FALSE;
1945 dbus_message_iter_get_basic(iter, &privacy);
1946 bss->privacy = privacy;
1947 } else if (g_strcmp0(key, "RSN") == 0) {
1948 bss->rsn_selected = TRUE;
1950 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1951 } else if (g_strcmp0(key, "WPA") == 0) {
1952 bss->rsn_selected = FALSE;
1954 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1955 } else if (g_strcmp0(key, "IEs") == 0)
1956 bss_process_ies(iter, bss);
1958 SUPPLICANT_DBG("key %s type %c",
1959 key, dbus_message_iter_get_arg_type(iter));
1962 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1965 GSupplicantInterface *interface = user_data;
1966 GSupplicantNetwork *network;
1967 struct g_supplicant_bss *bss;
1968 const char *path = NULL;
1972 dbus_message_iter_get_basic(iter, &path);
1976 if (g_strcmp0(path, "/") == 0)
1979 SUPPLICANT_DBG("%s", path);
1981 network = g_hash_table_lookup(interface->bss_mapping, path);
1983 bss = g_hash_table_lookup(network->bss_table, path);
1988 bss = g_try_new0(struct g_supplicant_bss, 1);
1992 bss->interface = interface;
1993 bss->path = g_strdup(path);
1994 bss->signal = BSS_UNKNOWN_STRENGTH;
1999 static void interface_bss_added_with_keys(DBusMessageIter *iter,
2002 struct g_supplicant_bss *bss;
2006 bss = interface_bss_added(iter, user_data);
2010 dbus_message_iter_next(iter);
2012 if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
2015 supplicant_dbus_property_foreach(iter, bss_property, bss);
2017 bss_compute_security(bss);
2018 add_or_replace_bss_to_network(bss);
2021 static void interface_bss_added_without_keys(DBusMessageIter *iter,
2024 struct g_supplicant_bss *bss;
2028 bss = interface_bss_added(iter, user_data);
2032 supplicant_dbus_property_get_all(bss->path,
2033 SUPPLICANT_INTERFACE ".BSS",
2034 bss_property, bss, NULL);
2036 bss_compute_security(bss);
2037 add_or_replace_bss_to_network(bss);
2040 static void update_signal(gpointer key, gpointer value,
2043 struct g_supplicant_bss *bss = value;
2044 GSupplicantNetwork *network = user_data;
2046 if (bss->signal > network->signal) {
2047 network->signal = bss->signal;
2048 network->best_bss = bss;
2052 static void update_network_signal(GSupplicantNetwork *network)
2054 if (g_hash_table_size(network->bss_table) <= 1 && network->best_bss)
2057 g_hash_table_foreach(network->bss_table,
2058 update_signal, network);
2060 SUPPLICANT_DBG("New network signal %d", network->signal);
2063 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
2065 GSupplicantInterface *interface = user_data;
2066 GSupplicantNetwork *network;
2067 struct g_supplicant_bss *bss = NULL;
2068 const char *path = NULL;
2070 dbus_message_iter_get_basic(iter, &path);
2074 network = g_hash_table_lookup(interface->bss_mapping, path);
2078 bss = g_hash_table_lookup(network->bss_table, path);
2079 if (network->best_bss == bss) {
2080 network->best_bss = NULL;
2081 network->signal = BSS_UNKNOWN_STRENGTH;
2084 g_hash_table_remove(bss_mapping, path);
2086 g_hash_table_remove(interface->bss_mapping, path);
2087 g_hash_table_remove(network->bss_table, path);
2089 update_network_signal(network);
2091 if (g_hash_table_size(network->bss_table) == 0)
2092 g_hash_table_remove(interface->network_table, network->group);
2095 static void set_config_methods(DBusMessageIter *iter, void *user_data)
2097 const char *config_methods = "push_button";
2099 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2103 static void interface_property(const char *key, DBusMessageIter *iter,
2106 GSupplicantInterface *interface = user_data;
2111 SUPPLICANT_DBG("%s", key);
2114 debug_strvalmap("KeyMgmt capability", keymgmt_map,
2115 interface->keymgmt_capa);
2116 debug_strvalmap("AuthAlg capability", authalg_capa_map,
2117 interface->authalg_capa);
2118 debug_strvalmap("Protocol capability", proto_capa_map,
2119 interface->proto_capa);
2120 debug_strvalmap("Pairwise capability", pairwise_map,
2121 interface->pairwise_capa);
2122 debug_strvalmap("Group capability", group_map,
2123 interface->group_capa);
2124 debug_strvalmap("Scan capability", scan_capa_map,
2125 interface->scan_capa);
2126 debug_strvalmap("Mode capability", mode_capa_map,
2127 interface->mode_capa);
2130 supplicant_dbus_property_set(interface->path,
2131 SUPPLICANT_INTERFACE ".Interface.WPS",
2132 "ConfigMethods", DBUS_TYPE_STRING_AS_STRING,
2133 set_config_methods, NULL, NULL, NULL);
2135 if (interface->ready)
2136 callback_interface_added(interface);
2141 if (g_strcmp0(key, "Capabilities") == 0) {
2142 supplicant_dbus_property_foreach(iter, interface_capability,
2144 #if !defined TIZEN_EXT
2145 if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_P2P)
2146 interface->p2p_support = true;
2148 } else if (g_strcmp0(key, "State") == 0) {
2149 const char *str = NULL;
2151 dbus_message_iter_get_basic(iter, &str);
2153 if (string2state(str) != interface->state) {
2154 interface->state = string2state(str);
2155 callback_interface_state(interface);
2157 if (interface->state == G_SUPPLICANT_STATE_DISABLED)
2158 interface->ready = FALSE;
2160 interface->ready = TRUE;
2162 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
2163 } else if (g_strcmp0(key, "Scanning") == 0) {
2164 dbus_bool_t scanning = FALSE;
2166 dbus_message_iter_get_basic(iter, &scanning);
2167 interface->scanning = scanning;
2169 if (interface->ready) {
2170 if (interface->scanning)
2171 callback_scan_started(interface);
2173 callback_scan_finished(interface);
2175 } else if (g_strcmp0(key, "ApScan") == 0) {
2178 dbus_message_iter_get_basic(iter, &apscan);
2179 interface->apscan = apscan;
2180 } else if (g_strcmp0(key, "Ifname") == 0) {
2181 const char *str = NULL;
2183 dbus_message_iter_get_basic(iter, &str);
2185 g_free(interface->ifname);
2186 interface->ifname = g_strdup(str);
2188 } else if (g_strcmp0(key, "Driver") == 0) {
2189 const char *str = NULL;
2191 dbus_message_iter_get_basic(iter, &str);
2193 g_free(interface->driver);
2194 interface->driver = g_strdup(str);
2196 } else if (g_strcmp0(key, "BridgeIfname") == 0) {
2197 const char *str = NULL;
2199 dbus_message_iter_get_basic(iter, &str);
2201 g_free(interface->bridge);
2202 interface->bridge = g_strdup(str);
2204 } else if (g_strcmp0(key, "CurrentBSS") == 0) {
2205 interface_bss_added_without_keys(iter, interface);
2206 } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
2207 interface_network_added(iter, interface);
2208 } else if (g_strcmp0(key, "BSSs") == 0) {
2209 supplicant_dbus_array_foreach(iter,
2210 interface_bss_added_without_keys,
2212 } else if (g_strcmp0(key, "Blobs") == 0) {
2214 } else if (g_strcmp0(key, "Networks") == 0) {
2215 supplicant_dbus_array_foreach(iter, interface_network_added,
2218 SUPPLICANT_DBG("key %s type %c",
2219 key, dbus_message_iter_get_arg_type(iter));
2222 static void scan_network_update(DBusMessageIter *iter, void *user_data)
2224 GSupplicantInterface *interface = user_data;
2225 GSupplicantNetwork *network;
2231 dbus_message_iter_get_basic(iter, &path);
2236 if (g_strcmp0(path, "/") == 0)
2239 /* Update the network details based on scan BSS data */
2240 network = g_hash_table_lookup(interface->bss_mapping, path);
2242 callback_network_added(network);
2245 static void scan_bss_data(const char *key, DBusMessageIter *iter,
2248 GSupplicantInterface *interface = user_data;
2251 supplicant_dbus_array_foreach(iter, scan_network_update,
2254 if (interface->scan_callback)
2255 interface->scan_callback(0, interface, interface->scan_data);
2257 interface->scan_callback = NULL;
2258 interface->scan_data = NULL;
2261 static GSupplicantInterface *interface_alloc(const char *path)
2263 GSupplicantInterface *interface;
2265 interface = g_try_new0(GSupplicantInterface, 1);
2269 interface->path = g_strdup(path);
2271 interface->network_table = g_hash_table_new_full(g_str_hash,
2272 g_str_equal, NULL, remove_network);
2273 interface->peer_table = g_hash_table_new_full(g_str_hash,
2274 g_str_equal, NULL, remove_peer);
2275 interface->group_table = g_hash_table_new_full(g_str_hash,
2276 g_str_equal, NULL, remove_group);
2277 interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2279 interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2282 g_hash_table_replace(interface_table, interface->path, interface);
2287 static void interface_added(DBusMessageIter *iter, void *user_data)
2289 GSupplicantInterface *interface;
2290 const char *path = NULL;
2294 dbus_message_iter_get_basic(iter, &path);
2298 if (g_strcmp0(path, "/") == 0)
2301 interface = g_hash_table_lookup(interface_table, path);
2305 interface = interface_alloc(path);
2309 dbus_message_iter_next(iter);
2310 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
2311 supplicant_dbus_property_foreach(iter, interface_property,
2313 interface_property(NULL, NULL, interface);
2317 supplicant_dbus_property_get_all(path,
2318 SUPPLICANT_INTERFACE ".Interface",
2319 interface_property, interface,
2323 static void interface_removed(DBusMessageIter *iter, void *user_data)
2325 const char *path = NULL;
2326 GSupplicantInterface *interface = user_data;
2328 dbus_message_iter_get_basic(iter, &path);
2332 interface = g_hash_table_lookup(interface_table, path);
2333 g_supplicant_interface_cancel(interface);
2335 g_hash_table_remove(interface_table, path);
2338 static void eap_method(DBusMessageIter *iter, void *user_data)
2340 const char *str = NULL;
2343 dbus_message_iter_get_basic(iter, &str);
2347 for (i = 0; eap_method_map[i].str; i++)
2348 if (strcmp(str, eap_method_map[i].str) == 0) {
2349 eap_methods |= eap_method_map[i].val;
2354 static void service_property(const char *key, DBusMessageIter *iter,
2358 callback_system_ready();
2362 if (g_strcmp0(key, "DebugLevel") == 0) {
2363 const char *str = NULL;
2366 dbus_message_iter_get_basic(iter, &str);
2367 for (i = 0; debug_strings[i]; i++)
2368 if (g_strcmp0(debug_strings[i], str) == 0) {
2372 SUPPLICANT_DBG("Debug level %d", debug_level);
2373 } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
2374 dbus_message_iter_get_basic(iter, &debug_timestamp);
2375 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
2376 } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
2377 dbus_message_iter_get_basic(iter, &debug_showkeys);
2378 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
2379 } else if (g_strcmp0(key, "Interfaces") == 0) {
2380 supplicant_dbus_array_foreach(iter, interface_added, NULL);
2381 } else if (g_strcmp0(key, "EapMethods") == 0) {
2382 supplicant_dbus_array_foreach(iter, eap_method, NULL);
2383 debug_strvalmap("EAP method", eap_method_map, eap_methods);
2384 } else if (g_strcmp0(key, "Country") == 0) {
2385 const char *country = NULL;
2387 dbus_message_iter_get_basic(iter, &country);
2388 SUPPLICANT_DBG("Country %s", country);
2390 SUPPLICANT_DBG("key %s type %c",
2391 key, dbus_message_iter_get_arg_type(iter));
2394 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
2396 const char *name = NULL, *old = NULL, *new = NULL;
2400 if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
2403 dbus_message_iter_get_basic(iter, &name);
2407 if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
2410 dbus_message_iter_next(iter);
2411 dbus_message_iter_get_basic(iter, &old);
2412 dbus_message_iter_next(iter);
2413 dbus_message_iter_get_basic(iter, &new);
2418 if (strlen(old) > 0 && strlen(new) == 0) {
2419 system_available = FALSE;
2420 g_hash_table_remove_all(bss_mapping);
2421 g_hash_table_remove_all(peer_mapping);
2422 g_hash_table_remove_all(group_mapping);
2423 g_hash_table_remove_all(interface_table);
2424 callback_system_killed();
2427 if (strlen(new) > 0 && strlen(old) == 0) {
2428 system_available = TRUE;
2429 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
2430 SUPPLICANT_INTERFACE,
2431 service_property, NULL, NULL);
2435 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
2439 if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
2442 supplicant_dbus_property_foreach(iter, service_property, NULL);
2445 static void signal_interface_added(const char *path, DBusMessageIter *iter)
2447 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2449 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2450 interface_added(iter, NULL);
2453 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
2457 if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2458 interface_removed(iter, NULL);
2461 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
2463 GSupplicantInterface *interface;
2467 interface = g_hash_table_lookup(interface_table, path);
2471 supplicant_dbus_property_foreach(iter, interface_property, interface);
2474 static void signal_scan_done(const char *path, DBusMessageIter *iter)
2476 GSupplicantInterface *interface;
2477 dbus_bool_t success = FALSE;
2481 interface = g_hash_table_lookup(interface_table, path);
2485 dbus_message_iter_get_basic(iter, &success);
2487 if (interface->scanning) {
2488 callback_scan_finished(interface);
2489 interface->scanning = FALSE;
2493 * If scan is unsuccessful return -EIO else get the scanned BSSs
2494 * and update the network details accordingly
2497 if (interface->scan_callback)
2498 interface->scan_callback(-EIO, interface,
2499 interface->scan_data);
2501 interface->scan_callback = NULL;
2502 interface->scan_data = NULL;
2507 supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
2508 "BSSs", scan_bss_data, interface, interface);
2511 static void signal_bss_added(const char *path, DBusMessageIter *iter)
2513 GSupplicantInterface *interface;
2517 interface = g_hash_table_lookup(interface_table, path);
2521 interface_bss_added_with_keys(iter, interface);
2524 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
2526 GSupplicantInterface *interface;
2530 interface = g_hash_table_lookup(interface_table, path);
2534 interface_bss_removed(iter, interface);
2537 static void signal_network_added(const char *path, DBusMessageIter *iter)
2539 GSupplicantInterface *interface;
2543 interface = g_hash_table_lookup(interface_table, path);
2547 interface_network_added(iter, interface);
2550 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2552 GSupplicantInterface *interface;
2556 interface = g_hash_table_lookup(interface_table, path);
2560 interface_network_removed(iter, interface);
2563 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2565 GSupplicantInterface *interface;
2566 GSupplicantNetwork *network;
2567 GSupplicantSecurity old_security;
2568 struct g_supplicant_bss *bss;
2572 interface = g_hash_table_lookup(bss_mapping, path);
2576 network = g_hash_table_lookup(interface->bss_mapping, path);
2580 bss = g_hash_table_lookup(network->bss_table, path);
2584 supplicant_dbus_property_foreach(iter, bss_property, bss);
2586 old_security = network->security;
2587 bss_compute_security(bss);
2589 if (old_security != bss->security) {
2590 struct g_supplicant_bss *new_bss;
2592 SUPPLICANT_DBG("New network security for %s", bss->ssid);
2594 /* Security change policy:
2595 * - we first copy the current bss into a new one with
2596 * its own pointer (path)
2597 * - we remove the current bss related network which will
2598 * tell the plugin about such removal. This is done due
2599 * to the fact that a security change means a group change
2600 * so a complete network change.
2601 * (current bss becomes invalid as well)
2602 * - we add the new bss: it adds new network and tell the
2603 * plugin about it. */
2605 new_bss = g_try_new0(struct g_supplicant_bss, 1);
2609 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
2610 new_bss->path = g_strdup(bss->path);
2612 g_hash_table_remove(interface->network_table, network->group);
2614 add_or_replace_bss_to_network(new_bss);
2619 #if defined TIZEN_EXT
2620 if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
2621 network->wps = TRUE;
2622 network->wps_capabilities |= bss->wps_capabilities;
2624 network->wps = FALSE;
2627 if (bss->signal == network->signal)
2632 callback_network_changed(network, "");
2638 * If the new signal is lower than the SSID signal, we need
2639 * to check for the new maximum.
2641 if (bss->signal < network->signal) {
2642 if (bss != network->best_bss)
2647 callback_network_changed(network, "");
2651 network->signal = bss->signal;
2652 update_network_signal(network);
2654 network->signal = bss->signal;
2655 network->best_bss = bss;
2658 SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid,
2661 callback_network_changed(network, "Signal");
2664 static void wps_credentials(const char *key, DBusMessageIter *iter,
2667 GSupplicantInterface *interface = user_data;
2672 SUPPLICANT_DBG("key %s", key);
2674 if (g_strcmp0(key, "Key") == 0) {
2675 DBusMessageIter array;
2676 unsigned char *key_val;
2679 dbus_message_iter_recurse(iter, &array);
2680 dbus_message_iter_get_fixed_array(&array, &key_val, &key_len);
2682 g_free(interface->wps_cred.key);
2683 interface->wps_cred.key = g_try_malloc0(
2684 sizeof(char) * key_len + 1);
2686 if (!interface->wps_cred.key)
2689 memcpy(interface->wps_cred.key, key_val,
2690 sizeof(char) * key_len);
2692 SUPPLICANT_DBG("WPS key present");
2693 } else if (g_strcmp0(key, "SSID") == 0) {
2694 DBusMessageIter array;
2695 unsigned char *ssid;
2698 dbus_message_iter_recurse(iter, &array);
2699 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2701 if (ssid_len > 0 && ssid_len < 33) {
2702 memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2703 interface->wps_cred.ssid_len = ssid_len;
2705 memset(interface->wps_cred.ssid, 0, 32);
2706 interface->wps_cred.ssid_len = 0;
2711 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2713 GSupplicantInterface *interface;
2717 interface = g_hash_table_lookup(interface_table, path);
2721 supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2724 static void wps_event_args(const char *key, DBusMessageIter *iter,
2727 GSupplicantInterface *interface = user_data;
2729 if (!key || !interface)
2732 SUPPLICANT_DBG("Arg Key %s", key);
2735 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2737 GSupplicantInterface *interface;
2738 const char *name = NULL;
2742 interface = g_hash_table_lookup(interface_table, path);
2746 dbus_message_iter_get_basic(iter, &name);
2748 SUPPLICANT_DBG("Name: %s", name);
2750 if (g_strcmp0(name, "success") == 0)
2751 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2752 else if (g_strcmp0(name, "failed") == 0)
2753 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2755 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2757 if (!dbus_message_iter_has_next(iter))
2760 dbus_message_iter_next(iter);
2762 supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2765 #if defined TIZEN_EXT
2766 static void signal_power_off(const char *path, DBusMessageIter *iter)
2768 int poweroff_state = 0;
2770 dbus_message_iter_get_basic(iter, &poweroff_state);
2772 SUPPLICANT_DBG("poweroff_state(%d)", poweroff_state);
2774 /* POWER_OFF_DIRECT 2 && POWER_OFF_RESTART 3 */
2775 if (poweroff_state != 2 && poweroff_state != 3)
2778 if (callbacks_pointer == NULL)
2781 if (callbacks_pointer->system_power_off == NULL)
2784 callbacks_pointer->system_power_off();
2788 static void signal_station_connected(const char *path, DBusMessageIter *iter)
2790 GSupplicantInterface *interface;
2791 const char *sta_mac = NULL;
2793 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2795 if (callbacks_pointer->add_station == NULL)
2798 if (g_strcmp0(path, "/") == 0)
2801 interface = g_hash_table_lookup(interface_table, path);
2802 if (interface == NULL)
2805 dbus_message_iter_get_basic(iter, &sta_mac);
2806 if (sta_mac == NULL)
2809 SUPPLICANT_DBG("New station %s connected", sta_mac);
2810 callbacks_pointer->add_station(sta_mac);
2813 static void signal_station_disconnected(const char *path, DBusMessageIter *iter)
2815 GSupplicantInterface *interface;
2816 const char *sta_mac = NULL;
2818 SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2820 if (callbacks_pointer->remove_station == NULL)
2823 if (g_strcmp0(path, "/") == 0)
2826 interface = g_hash_table_lookup(interface_table, path);
2827 if (interface == NULL)
2830 dbus_message_iter_get_basic(iter, &sta_mac);
2831 if (sta_mac == NULL)
2834 SUPPLICANT_DBG("Station %s disconnected", sta_mac);
2835 callbacks_pointer->remove_station(sta_mac);
2838 static void create_peer_identifier(GSupplicantPeer *peer)
2840 const unsigned char test[ETH_ALEN] = {};
2845 if (!memcmp(peer->device_address, test, ETH_ALEN)) {
2846 peer->identifier = g_strdup(peer->name);
2850 peer->identifier = g_malloc0(19);
2851 snprintf(peer->identifier, 19, "%02x%02x%02x%02x%02x%02x",
2852 peer->device_address[0],
2853 peer->device_address[1],
2854 peer->device_address[2],
2855 peer->device_address[3],
2856 peer->device_address[4],
2857 peer->device_address[5]);
2860 struct peer_property_data {
2861 GSupplicantPeer *peer;
2863 bool groups_changed;
2864 bool services_changed;
2867 static void peer_groups_relation(DBusMessageIter *iter, void *user_data)
2869 struct peer_property_data *data = user_data;
2870 GSupplicantPeer *peer = data->peer;
2871 GSupplicantGroup *group;
2872 const char *str = NULL;
2875 dbus_message_iter_get_basic(iter, &str);
2879 group = g_hash_table_lookup(group_mapping, str);
2883 elem = g_slist_find_custom(data->old_groups, str, g_str_equal);
2885 data->old_groups = g_slist_remove_link(data->old_groups, elem);
2886 peer->groups = g_slist_concat(elem, peer->groups);
2888 peer->groups = g_slist_prepend(peer->groups, g_strdup(str));
2889 data->groups_changed = true;
2893 static void peer_property(const char *key, DBusMessageIter *iter,
2896 GSupplicantPeer *pending_peer;
2897 struct peer_property_data *data = user_data;
2898 GSupplicantPeer *peer = data->peer;
2900 SUPPLICANT_DBG("key: %s", key);
2902 if (!peer->interface)
2907 create_peer_identifier(peer);
2908 callback_peer_found(peer);
2909 pending_peer = g_hash_table_lookup(
2910 pending_peer_connection, peer->path);
2912 if (pending_peer && pending_peer == peer) {
2913 callback_peer_request(peer);
2914 g_hash_table_remove(pending_peer_connection,
2924 if (g_strcmp0(key, "DeviceAddress") == 0) {
2925 unsigned char *dev_addr;
2926 DBusMessageIter array;
2929 dbus_message_iter_recurse(iter, &array);
2930 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
2932 if (len == ETH_ALEN)
2933 memcpy(peer->device_address, dev_addr, len);
2934 } else if (g_strcmp0(key, "DeviceName") == 0) {
2935 const char *str = NULL;
2937 dbus_message_iter_get_basic(iter, &str);
2939 peer->name = g_strdup(str);
2940 } else if (g_strcmp0(key, "config_method") == 0) {
2941 uint16_t wps_config;
2943 dbus_message_iter_get_basic(iter, &wps_config);
2945 if (wps_config & G_SUPPLICANT_WPS_CONFIG_PBC)
2946 peer->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
2947 if (wps_config & ~G_SUPPLICANT_WPS_CONFIG_PBC)
2948 peer->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
2949 } else if (g_strcmp0(key, "Groups") == 0) {
2950 data->old_groups = peer->groups;
2951 peer->groups = NULL;
2953 supplicant_dbus_array_foreach(iter,
2954 peer_groups_relation, data);
2955 if (g_slist_length(data->old_groups) > 0) {
2956 g_slist_free_full(data->old_groups, g_free);
2957 data->groups_changed = true;
2959 } else if (g_strcmp0(key, "IEs") == 0) {
2960 DBusMessageIter array;
2964 dbus_message_iter_recurse(iter, &array);
2965 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
2967 if (!ie || ie_len < 2)
2970 if (peer->widi_ies) {
2971 if (memcmp(peer->widi_ies, ie, ie_len) == 0)
2974 g_free(peer->widi_ies);
2975 peer->widi_ies_length = 0;
2978 peer->widi_ies = g_malloc0(ie_len * sizeof(unsigned char));
2980 memcpy(peer->widi_ies, ie, ie_len);
2981 peer->widi_ies_length = ie_len;
2982 data->services_changed = true;
2986 static void signal_peer_found(const char *path, DBusMessageIter *iter)
2988 struct peer_property_data *property_data;
2989 GSupplicantInterface *interface;
2990 const char *obj_path = NULL;
2991 GSupplicantPeer *peer;
2995 interface = g_hash_table_lookup(interface_table, path);
2999 dbus_message_iter_get_basic(iter, &obj_path);
3000 if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3003 peer = g_hash_table_lookup(interface->peer_table, obj_path);
3007 peer = g_try_new0(GSupplicantPeer, 1);
3011 peer->interface = interface;
3012 peer->path = g_strdup(obj_path);
3013 g_hash_table_insert(interface->peer_table, peer->path, peer);
3014 g_hash_table_replace(peer_mapping, peer->path, interface);
3016 property_data = dbus_malloc0(sizeof(struct peer_property_data));
3017 property_data->peer = peer;
3019 dbus_message_iter_next(iter);
3020 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3021 supplicant_dbus_property_foreach(iter, peer_property,
3023 peer_property(NULL, NULL, property_data);
3027 supplicant_dbus_property_get_all(obj_path,
3028 SUPPLICANT_INTERFACE ".Peer",
3029 peer_property, property_data, NULL);
3032 static void signal_peer_lost(const char *path, DBusMessageIter *iter)
3034 GSupplicantInterface *interface;
3035 const char *obj_path = NULL;
3036 GSupplicantPeer *peer;
3040 interface = g_hash_table_lookup(interface_table, path);
3044 dbus_message_iter_get_basic(iter, &obj_path);
3045 if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3048 peer = g_hash_table_lookup(interface->peer_table, obj_path);
3052 g_hash_table_remove(interface->peer_table, obj_path);
3055 static void signal_peer_changed(const char *path, DBusMessageIter *iter)
3057 struct peer_property_data *property_data;
3058 GSupplicantInterface *interface;
3059 GSupplicantPeer *peer;
3063 interface = g_hash_table_lookup(peer_mapping, path);
3067 peer = g_hash_table_lookup(interface->peer_table, path);
3069 g_hash_table_remove(peer_mapping, path);
3073 property_data = dbus_malloc0(sizeof(struct peer_property_data));
3074 property_data->peer = peer;
3076 supplicant_dbus_property_foreach(iter, peer_property, property_data);
3077 if (property_data->services_changed)
3078 callback_peer_changed(peer,
3079 G_SUPPLICANT_PEER_SERVICES_CHANGED);
3081 if (property_data->groups_changed)
3082 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_CHANGED);
3084 dbus_free(property_data);
3086 if (!g_supplicant_peer_is_in_a_group(peer))
3087 peer->connection_requested = false;
3090 struct group_sig_data {
3091 const char *peer_obj_path;
3092 unsigned char iface_address[ETH_ALEN];
3093 const char *interface_obj_path;
3094 const char *group_obj_path;
3098 static void group_sig_property(const char *key, DBusMessageIter *iter,
3101 struct group_sig_data *data = user_data;
3106 if (g_strcmp0(key, "peer_interface_addr") == 0) {
3107 unsigned char *dev_addr;
3108 DBusMessageIter array;
3111 dbus_message_iter_recurse(iter, &array);
3112 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
3114 if (len == ETH_ALEN)
3115 memcpy(data->iface_address, dev_addr, len);
3116 } else if (g_strcmp0(key, "role") == 0) {
3117 const char *str = NULL;
3119 dbus_message_iter_get_basic(iter, &str);
3120 if (g_strcmp0(str, "GO") == 0)
3121 data->role = G_SUPPLICANT_GROUP_ROLE_GO;
3123 data->role = G_SUPPLICANT_GROUP_ROLE_CLIENT;
3124 } else if (g_strcmp0(key, "peer_object") == 0)
3125 dbus_message_iter_get_basic(iter, &data->peer_obj_path);
3126 else if (g_strcmp0(key, "interface_object") == 0)
3127 dbus_message_iter_get_basic(iter, &data->interface_obj_path);
3128 else if (g_strcmp0(key, "group_object") == 0)
3129 dbus_message_iter_get_basic(iter, &data->group_obj_path);
3133 static void signal_group_success(const char *path, DBusMessageIter *iter)
3135 GSupplicantInterface *interface;
3136 struct group_sig_data data = {};
3137 GSupplicantPeer *peer;
3141 interface = g_hash_table_lookup(interface_table, path);
3145 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3146 if (!data.peer_obj_path)
3149 peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3153 memcpy(peer->iface_address, data.iface_address, ETH_ALEN);
3154 interface->pending_peer_path = peer->path;
3157 static void signal_group_failure(const char *path, DBusMessageIter *iter)
3159 GSupplicantInterface *interface;
3160 struct group_sig_data data = {};
3161 GSupplicantPeer *peer;
3165 interface = g_hash_table_lookup(interface_table, path);
3169 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3170 if (!data.peer_obj_path)
3173 peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3177 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_FAILED);
3178 peer->connection_requested = false;
3181 static void signal_group_started(const char *path, DBusMessageIter *iter)
3183 GSupplicantInterface *interface, *g_interface;
3184 struct group_sig_data data = {};
3185 GSupplicantGroup *group;
3186 GSupplicantPeer *peer;
3190 interface = g_hash_table_lookup(interface_table, path);
3194 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3195 if (!data.interface_obj_path || !data.group_obj_path)
3198 peer = g_hash_table_lookup(interface->peer_table,
3199 interface->pending_peer_path);
3200 interface->pending_peer_path = NULL;
3204 g_interface = g_hash_table_lookup(interface_table,
3205 data.interface_obj_path);
3209 group = g_hash_table_lookup(interface->group_table,
3210 data.group_obj_path);
3214 group = g_try_new0(GSupplicantGroup, 1);
3218 group->interface = g_interface;
3219 group->orig_interface = interface;
3220 group->path = g_strdup(data.group_obj_path);
3221 group->role = data.role;
3223 g_hash_table_insert(interface->group_table, group->path, group);
3224 g_hash_table_replace(group_mapping, group->path, group);
3226 peer->current_group_iface = g_interface;
3227 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_STARTED);
3230 static void remove_peer_group_interface(GHashTable *group_table,
3233 GSupplicantGroup *group;
3234 GHashTableIter iter;
3235 gpointer value, key;
3240 group = g_hash_table_lookup(group_table, path);
3242 if (!group || !group->orig_interface)
3245 g_hash_table_iter_init(&iter, group->orig_interface->peer_table);
3247 while (g_hash_table_iter_next(&iter, &key, &value)) {
3248 GSupplicantPeer *peer = value;
3250 if (peer->current_group_iface == group->interface)
3251 peer->current_group_iface = NULL;
3255 static void signal_group_finished(const char *path, DBusMessageIter *iter)
3257 GSupplicantInterface *interface;
3258 struct group_sig_data data = {};
3262 interface = g_hash_table_lookup(interface_table, path);
3266 supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3267 if (!data.interface_obj_path || !data.group_obj_path)
3270 remove_peer_group_interface(interface->group_table, data.group_obj_path);
3272 g_hash_table_remove(group_mapping, data.group_obj_path);
3274 g_hash_table_remove(interface->group_table, data.group_obj_path);
3277 static void signal_group_request(const char *path, DBusMessageIter *iter)
3279 GSupplicantInterface *interface;
3280 GSupplicantPeer *peer;
3281 const char *obj_path;
3285 interface = g_hash_table_lookup(interface_table, path);
3289 dbus_message_iter_get_basic(iter, &obj_path);
3290 if (!obj_path || !g_strcmp0(obj_path, "/"))
3293 peer = g_hash_table_lookup(interface->peer_table, obj_path);
3298 * Peer has been previously found and property set,
3299 * otherwise, defer connection to when peer property
3302 if (peer->identifier)
3303 callback_peer_request(peer);
3305 g_hash_table_replace(pending_peer_connection, peer->path, peer);
3308 static void signal_group_peer_joined(const char *path, DBusMessageIter *iter)
3310 const char *peer_path = NULL;
3311 GSupplicantInterface *interface;
3312 GSupplicantGroup *group;
3313 GSupplicantPeer *peer;
3317 group = g_hash_table_lookup(group_mapping, path);
3321 dbus_message_iter_get_basic(iter, &peer_path);
3325 interface = g_hash_table_lookup(peer_mapping, peer_path);
3329 peer = g_hash_table_lookup(interface->peer_table, peer_path);
3333 group->members = g_slist_prepend(group->members, g_strdup(peer_path));
3335 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_JOINED);
3338 static void signal_group_peer_disconnected(const char *path, DBusMessageIter *iter)
3340 const char *peer_path = NULL;
3341 GSupplicantInterface *interface;
3342 GSupplicantGroup *group;
3343 GSupplicantPeer *peer;
3348 group = g_hash_table_lookup(group_mapping, path);
3352 dbus_message_iter_get_basic(iter, &peer_path);
3356 for (elem = group->members; elem; elem = elem->next) {
3357 if (!g_strcmp0(elem->data, peer_path))
3365 group->members = g_slist_delete_link(group->members, elem);
3367 interface = g_hash_table_lookup(peer_mapping, peer_path);
3371 peer = g_hash_table_lookup(interface->peer_table, peer_path);
3375 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_DISCONNECTED);
3376 peer->connection_requested = false;
3380 const char *interface;
3382 void (*function) (const char *path, DBusMessageIter *iter);
3384 { DBUS_INTERFACE_DBUS, "NameOwnerChanged", signal_name_owner_changed },
3386 { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
3387 { SUPPLICANT_INTERFACE, "InterfaceAdded", signal_interface_added },
3388 { SUPPLICANT_INTERFACE, "InterfaceCreated", signal_interface_added },
3389 { SUPPLICANT_INTERFACE, "InterfaceRemoved", signal_interface_removed },
3391 { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
3392 { SUPPLICANT_INTERFACE ".Interface", "ScanDone", signal_scan_done },
3393 { SUPPLICANT_INTERFACE ".Interface", "BSSAdded", signal_bss_added },
3394 { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved", signal_bss_removed },
3395 { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded", signal_network_added },
3396 { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved", signal_network_removed },
3398 { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed },
3400 { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
3401 { SUPPLICANT_INTERFACE ".Interface.WPS", "Event", signal_wps_event },
3402 #if defined TIZEN_EXT
3403 { "org.tizen.system.deviced.PowerOff", "ChangeState", signal_power_off },
3406 { SUPPLICANT_INTERFACE".Interface", "StaAuthorized", signal_station_connected },
3407 { SUPPLICANT_INTERFACE".Interface", "StaDeauthorized", signal_station_disconnected },
3409 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceFound", signal_peer_found },
3410 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceLost", signal_peer_lost },
3412 { SUPPLICANT_INTERFACE ".Peer", "PropertiesChanged", signal_peer_changed },
3414 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationSuccess", signal_group_success },
3415 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationFailure", signal_group_failure },
3416 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupStarted", signal_group_started },
3417 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupFinished", signal_group_finished },
3418 { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationRequest", signal_group_request },
3420 { SUPPLICANT_INTERFACE ".Group", "PeerJoined", signal_group_peer_joined },
3421 { SUPPLICANT_INTERFACE ".Group", "PeerDisconnected", signal_group_peer_disconnected },
3426 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
3427 DBusMessage *message, void *data)
3429 DBusMessageIter iter;
3433 path = dbus_message_get_path(message);
3435 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3437 if (!dbus_message_iter_init(message, &iter))
3438 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3440 for (i = 0; signal_map[i].interface; i++) {
3441 if (!dbus_message_has_interface(message, signal_map[i].interface))
3444 if (!dbus_message_has_member(message, signal_map[i].member))
3447 signal_map[i].function(path, &iter);
3451 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3454 void g_supplicant_interface_cancel(GSupplicantInterface *interface)
3456 SUPPLICANT_DBG("Cancelling any pending DBus calls");
3457 supplicant_dbus_method_call_cancel_all(interface);
3458 supplicant_dbus_property_call_cancel_all(interface);
3461 struct supplicant_regdom {
3462 GSupplicantCountryCallback callback;
3464 const void *user_data;
3467 static void country_result(const char *error,
3468 DBusMessageIter *iter, void *user_data)
3470 struct supplicant_regdom *regdom = user_data;
3473 SUPPLICANT_DBG("Country setting result");
3479 SUPPLICANT_DBG("Country setting failure %s", error);
3483 if (regdom->callback)
3484 regdom->callback(result, regdom->alpha2,
3485 (void *) regdom->user_data);
3490 static void country_params(DBusMessageIter *iter, void *user_data)
3492 struct supplicant_regdom *regdom = user_data;
3494 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
3498 int g_supplicant_set_country(const char *alpha2,
3499 GSupplicantCountryCallback callback,
3500 const void *user_data)
3502 struct supplicant_regdom *regdom;
3504 SUPPLICANT_DBG("Country setting %s", alpha2);
3506 if (!system_available)
3509 regdom = dbus_malloc0(sizeof(*regdom));
3513 regdom->callback = callback;
3514 regdom->alpha2 = alpha2;
3515 regdom->user_data = user_data;
3517 return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
3518 "Country", DBUS_TYPE_STRING_AS_STRING,
3519 country_params, country_result,
3523 int g_supplicant_interface_set_country(GSupplicantInterface *interface,
3524 GSupplicantCountryCallback callback,
3528 struct supplicant_regdom *regdom;
3530 regdom = dbus_malloc0(sizeof(*regdom));
3534 regdom->callback = callback;
3535 regdom->alpha2 = alpha2;
3536 regdom->user_data = user_data;
3538 return supplicant_dbus_property_set(interface->path,
3539 SUPPLICANT_INTERFACE ".Interface",
3540 "Country", DBUS_TYPE_STRING_AS_STRING,
3541 country_params, country_result,
3545 bool g_supplicant_interface_has_p2p(GSupplicantInterface *interface)
3550 return interface->p2p_support;
3553 struct supplicant_p2p_dev_config {
3558 static void p2p_device_config_result(const char *error,
3559 DBusMessageIter *iter, void *user_data)
3561 struct supplicant_p2p_dev_config *config = user_data;
3564 SUPPLICANT_DBG("Unable to set P2P Device configuration: %s",
3567 g_free(config->device_name);
3568 g_free(config->dev_type);
3572 static int dev_type_str2bin(const char *type, unsigned char dev_type[8])
3574 int length, pos, end;
3579 for (length = pos = 0; type[pos] != '\0' && length < 8; length++) {
3586 dev_type[length] = strtol(b, &e, 16);
3587 if (e && *e != '\0')
3596 static void p2p_device_config_params(DBusMessageIter *iter, void *user_data)
3598 struct supplicant_p2p_dev_config *config = user_data;
3599 DBusMessageIter dict;
3601 supplicant_dbus_dict_open(iter, &dict);
3603 supplicant_dbus_dict_append_basic(&dict, "DeviceName",
3604 DBUS_TYPE_STRING, &config->device_name);
3606 if (config->dev_type) {
3607 unsigned char dev_type[8] = {}, *type;
3610 len = dev_type_str2bin(config->dev_type, dev_type);
3613 supplicant_dbus_dict_append_fixed_array(&dict,
3614 "PrimaryDeviceType",
3615 DBUS_TYPE_BYTE, &type, len);
3619 supplicant_dbus_dict_close(iter, &dict);
3622 int g_supplicant_interface_set_p2p_device_config(GSupplicantInterface *interface,
3623 const char *device_name,
3624 const char *primary_dev_type)
3626 struct supplicant_p2p_dev_config *config;
3629 SUPPLICANT_DBG("P2P Device settings %s/%s",
3630 device_name, primary_dev_type);
3632 config = dbus_malloc0(sizeof(*config));
3636 config->device_name = g_strdup(device_name);
3637 config->dev_type = g_strdup(primary_dev_type);
3639 ret = supplicant_dbus_property_set(interface->path,
3640 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
3642 DBUS_TYPE_ARRAY_AS_STRING
3643 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
3644 DBUS_TYPE_STRING_AS_STRING
3645 DBUS_TYPE_VARIANT_AS_STRING
3646 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
3647 p2p_device_config_params,
3648 p2p_device_config_result, config, NULL);
3650 g_free(config->device_name);
3651 g_free(config->dev_type);
3653 SUPPLICANT_DBG("Unable to set P2P Device configuration");
3659 static gboolean peer_lookup_by_identifier(gpointer key, gpointer value,
3662 const GSupplicantPeer *peer = value;
3663 const char *identifier = user_data;
3665 if (!g_strcmp0(identifier, peer->identifier))
3671 GSupplicantPeer *g_supplicant_interface_peer_lookup(GSupplicantInterface *interface,
3672 const char *identifier)
3674 GSupplicantPeer *peer;
3676 peer = g_hash_table_find(interface->peer_table,
3677 peer_lookup_by_identifier,
3678 (void *) identifier);
3682 struct interface_data {
3683 GSupplicantInterface *interface;
3684 char *path; /* Interface path cannot be taken from interface (above) as
3685 * it might have been freed already.
3687 GSupplicantInterfaceCallback callback;
3691 struct interface_create_data {
3695 GSupplicantInterface *interface;
3696 GSupplicantInterfaceCallback callback;
3700 struct interface_connect_data {
3701 GSupplicantInterface *interface;
3703 GSupplicantInterfaceCallback callback;
3705 GSupplicantSSID *ssid;
3706 GSupplicantPeerParams *peer;
3711 struct interface_scan_data {
3712 GSupplicantInterface *interface;
3714 GSupplicantInterfaceCallback callback;
3715 GSupplicantScanParams *scan_params;
3719 static void interface_create_data_free(struct interface_create_data *data)
3721 g_free(data->ifname);
3722 g_free(data->driver);
3723 g_free(data->bridge);
3727 static bool interface_exists(GSupplicantInterface *interface,
3730 GSupplicantInterface *tmp;
3732 tmp = g_hash_table_lookup(interface_table, path);
3733 if (tmp && tmp == interface)
3739 static void interface_create_property(const char *key, DBusMessageIter *iter,
3742 struct interface_create_data *data = user_data;
3743 GSupplicantInterface *interface = data->interface;
3746 if (data->callback) {
3747 data->callback(0, data->interface, data->user_data);
3748 #if !defined TIZEN_EXT
3749 callback_p2p_support(interface);
3753 interface_create_data_free(data);
3756 interface_property(key, iter, interface);
3759 static void interface_create_result(const char *error,
3760 DBusMessageIter *iter, void *user_data)
3762 struct interface_create_data *data = user_data;
3763 const char *path = NULL;
3769 g_warning("error %s", error);
3774 dbus_message_iter_get_basic(iter, &path);
3780 if (!system_available) {
3785 data->interface = g_hash_table_lookup(interface_table, path);
3786 if (!data->interface) {
3787 data->interface = interface_alloc(path);
3788 if (!data->interface) {
3794 err = supplicant_dbus_property_get_all(path,
3795 SUPPLICANT_INTERFACE ".Interface",
3796 interface_create_property, data,
3803 data->callback(err, NULL, data->user_data);
3805 interface_create_data_free(data);
3808 static void interface_create_params(DBusMessageIter *iter, void *user_data)
3810 struct interface_create_data *data = user_data;
3811 DBusMessageIter dict;
3815 supplicant_dbus_dict_open(iter, &dict);
3817 supplicant_dbus_dict_append_basic(&dict, "Ifname",
3818 DBUS_TYPE_STRING, &data->ifname);
3821 supplicant_dbus_dict_append_basic(&dict, "Driver",
3822 DBUS_TYPE_STRING, &data->driver);
3825 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
3826 DBUS_TYPE_STRING, &data->bridge);
3828 supplicant_dbus_dict_close(iter, &dict);
3831 static void interface_get_result(const char *error,
3832 DBusMessageIter *iter, void *user_data)
3834 struct interface_create_data *data = user_data;
3835 GSupplicantInterface *interface;
3836 const char *path = NULL;
3842 SUPPLICANT_DBG("Interface not created yet");
3846 dbus_message_iter_get_basic(iter, &path);
3852 interface = g_hash_table_lookup(interface_table, path);
3858 if (data->callback) {
3859 data->callback(0, interface, data->user_data);
3860 #if !defined TIZEN_EXT
3861 callback_p2p_support(interface);
3865 interface_create_data_free(data);
3870 if (!system_available) {
3875 SUPPLICANT_DBG("Creating interface");
3877 err = supplicant_dbus_method_call(SUPPLICANT_PATH,
3878 SUPPLICANT_INTERFACE,
3880 interface_create_params,
3881 interface_create_result, data,
3888 data->callback(err, NULL, data->user_data);
3890 interface_create_data_free(data);
3893 static void interface_get_params(DBusMessageIter *iter, void *user_data)
3895 struct interface_create_data *data = user_data;
3899 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
3902 int g_supplicant_interface_create(const char *ifname, const char *driver,
3904 GSupplicantInterfaceCallback callback,
3907 struct interface_create_data *data;
3910 SUPPLICANT_DBG("ifname %s", ifname);
3915 if (!system_available)
3918 data = dbus_malloc0(sizeof(*data));
3922 data->ifname = g_strdup(ifname);
3923 data->driver = g_strdup(driver);
3924 data->bridge = g_strdup(bridge);
3925 data->callback = callback;
3926 data->user_data = user_data;
3928 ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
3929 SUPPLICANT_INTERFACE,
3931 interface_get_params,
3932 interface_get_result, data,
3935 interface_create_data_free(data);
3940 static void interface_remove_result(const char *error,
3941 DBusMessageIter *iter, void *user_data)
3943 struct interface_data *data = user_data;
3951 if (!system_available) {
3957 * The gsupplicant interface is already freed by the InterfaceRemoved
3958 * signal callback. Simply invoke the interface_data callback.
3966 data->callback(err, NULL, data->user_data);
3972 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
3974 struct interface_data *data = user_data;
3976 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3977 &data->interface->path);
3981 int g_supplicant_interface_remove(GSupplicantInterface *interface,
3982 GSupplicantInterfaceCallback callback,
3985 struct interface_data *data;
3991 if (!system_available)
3994 g_supplicant_interface_cancel(interface);
3996 data = dbus_malloc0(sizeof(*data));
4000 data->interface = interface;
4001 data->path = g_strdup(interface->path);
4002 data->callback = callback;
4003 data->user_data = user_data;
4005 ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
4006 SUPPLICANT_INTERFACE,
4008 interface_remove_params,
4009 interface_remove_result, data,
4018 static void interface_scan_result(const char *error,
4019 DBusMessageIter *iter, void *user_data)
4021 struct interface_scan_data *data = user_data;
4025 SUPPLICANT_DBG("error %s", error);
4029 /* A non ready interface cannot send/receive anything */
4030 if (interface_exists(data->interface, data->path)) {
4031 if (!data->interface->ready)
4039 data->callback(err, data->interface, data->user_data);
4041 data->interface->scan_callback = data->callback;
4042 data->interface->scan_data = data->user_data;
4045 if (data->scan_params)
4046 g_supplicant_free_scan_params(data->scan_params);
4051 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
4053 DBusMessageIter data;
4054 unsigned int width = 0; /* Not used by wpa_supplicant atm */
4056 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
4058 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
4059 dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
4061 dbus_message_iter_close_container(iter, &data);
4064 static void add_scan_frequencies(DBusMessageIter *iter,
4067 GSupplicantScanParams *scan_data = user_data;
4071 for (i = 0; i < scan_data->num_freqs; i++) {
4072 freq = scan_data->freqs[i];
4076 add_scan_frequency(iter, freq);
4080 static void append_ssid(DBusMessageIter *iter,
4081 const void *ssid, unsigned int len)
4083 DBusMessageIter array;
4085 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
4086 DBUS_TYPE_BYTE_AS_STRING, &array);
4088 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
4090 dbus_message_iter_close_container(iter, &array);
4093 static void append_ssids(DBusMessageIter *iter, void *user_data)
4095 GSupplicantScanParams *scan_data = user_data;
4098 for (list = scan_data->ssids; list; list = list->next) {
4099 struct scan_ssid *scan_ssid = list->data;
4101 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
4105 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
4106 supplicant_dbus_array_function function,
4109 GSupplicantScanParams *scan_params = user_data;
4110 DBusMessageIter entry, value, array;
4111 const char *key = "Channels";
4113 if (scan_params->freqs && scan_params->freqs[0] != 0) {
4114 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
4117 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
4119 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
4120 DBUS_TYPE_ARRAY_AS_STRING
4121 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4122 DBUS_TYPE_UINT32_AS_STRING
4123 DBUS_TYPE_UINT32_AS_STRING
4124 DBUS_STRUCT_END_CHAR_AS_STRING,
4127 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
4128 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4129 DBUS_TYPE_UINT32_AS_STRING
4130 DBUS_TYPE_UINT32_AS_STRING
4131 DBUS_STRUCT_END_CHAR_AS_STRING,
4135 function(&array, user_data);
4137 dbus_message_iter_close_container(&value, &array);
4138 dbus_message_iter_close_container(&entry, &value);
4139 dbus_message_iter_close_container(dict, &entry);
4143 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
4145 DBusMessageIter dict;
4146 const char *type = "passive";
4147 struct interface_scan_data *data = user_data;
4149 supplicant_dbus_dict_open(iter, &dict);
4151 if (data && data->scan_params) {
4154 supplicant_dbus_dict_append_basic(&dict, "Type",
4155 DBUS_TYPE_STRING, &type);
4157 supplicant_dbus_dict_append_array(&dict, "SSIDs",
4162 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
4165 supplicant_dbus_dict_append_basic(&dict, "Type",
4166 DBUS_TYPE_STRING, &type);
4168 supplicant_dbus_dict_close(iter, &dict);
4171 static int interface_ready_to_scan(GSupplicantInterface *interface)
4176 if (!system_available)
4179 if (interface->scanning)
4182 switch (interface->state) {
4183 case G_SUPPLICANT_STATE_AUTHENTICATING:
4184 case G_SUPPLICANT_STATE_ASSOCIATING:
4185 case G_SUPPLICANT_STATE_ASSOCIATED:
4186 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4187 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4189 case G_SUPPLICANT_STATE_UNKNOWN:
4190 case G_SUPPLICANT_STATE_DISABLED:
4191 case G_SUPPLICANT_STATE_DISCONNECTED:
4192 case G_SUPPLICANT_STATE_INACTIVE:
4193 case G_SUPPLICANT_STATE_SCANNING:
4194 case G_SUPPLICANT_STATE_COMPLETED:
4201 int g_supplicant_interface_scan(GSupplicantInterface *interface,
4202 GSupplicantScanParams *scan_data,
4203 GSupplicantInterfaceCallback callback,
4206 struct interface_scan_data *data;
4209 ret = interface_ready_to_scan(interface);
4213 data = dbus_malloc0(sizeof(*data));
4217 data->interface = interface;
4218 data->path = g_strdup(interface->path);
4219 #if defined TIZEN_EXT
4220 data->interface->scan_callback = data->callback = callback;
4221 data->interface->scan_data = data->user_data = user_data;
4223 data->callback = callback;
4224 data->user_data = user_data;
4226 data->scan_params = scan_data;
4228 interface->scan_callback = callback;
4229 interface->scan_data = user_data;
4231 ret = supplicant_dbus_method_call(interface->path,
4232 SUPPLICANT_INTERFACE ".Interface", "Scan",
4233 interface_scan_params, interface_scan_result, data,
4244 static int parse_supplicant_error(DBusMessageIter *iter)
4246 int err = -ECANCELED;
4252 /* If the given passphrase is malformed wpa_s returns
4253 * "invalid message format" but this error should be interpreted as
4256 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
4257 dbus_message_iter_get_basic(iter, &key);
4258 if (strncmp(key, "psk", 3) == 0 ||
4259 strncmp(key, "wep_key", 7) == 0 ||
4260 strcmp(key, "invalid message format") == 0) {
4264 dbus_message_iter_next(iter);
4270 static void interface_select_network_result(const char *error,
4271 DBusMessageIter *iter, void *user_data)
4273 struct interface_connect_data *data = user_data;
4280 SUPPLICANT_DBG("SelectNetwork error %s", error);
4281 err = parse_supplicant_error(iter);
4287 data->callback(err, data->interface, data->user_data);
4293 static void interface_select_network_params(DBusMessageIter *iter,
4296 struct interface_connect_data *data = user_data;
4297 GSupplicantInterface *interface = data->interface;
4299 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
4300 &interface->network_path);
4303 static void interface_add_network_result(const char *error,
4304 DBusMessageIter *iter, void *user_data)
4306 struct interface_connect_data *data = user_data;
4307 GSupplicantInterface *interface = data->interface;
4314 dbus_message_iter_get_basic(iter, &path);
4318 SUPPLICANT_DBG("PATH: %s", path);
4320 g_free(interface->network_path);
4321 interface->network_path = g_strdup(path);
4323 supplicant_dbus_method_call(data->interface->path,
4324 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
4325 interface_select_network_params,
4326 interface_select_network_result, data,
4332 SUPPLICANT_DBG("AddNetwork error %s", error);
4334 if (interface_exists(data->interface, data->interface->path)) {
4335 err = parse_supplicant_error(iter);
4337 data->callback(err, data->interface, data->user_data);
4339 g_free(interface->network_path);
4340 interface->network_path = NULL;
4348 static void add_network_security_none(DBusMessageIter *dict)
4350 const char *auth_alg = "OPEN";
4352 supplicant_dbus_dict_append_basic(dict, "auth_alg",
4353 DBUS_TYPE_STRING, &auth_alg);
4356 static void add_network_security_wep(DBusMessageIter *dict,
4357 GSupplicantSSID *ssid)
4359 const char *auth_alg = "OPEN SHARED";
4360 dbus_uint32_t key_index = 0;
4362 supplicant_dbus_dict_append_basic(dict, "auth_alg",
4363 DBUS_TYPE_STRING, &auth_alg);
4365 if (ssid->passphrase) {
4366 int size = strlen(ssid->passphrase);
4367 if (size == 10 || size == 26) {
4368 unsigned char *key = g_try_malloc(13);
4372 memset(tmp, 0, sizeof(tmp));
4376 for (i = 0; i < size / 2; i++) {
4377 memcpy(tmp, ssid->passphrase + (i * 2), 2);
4378 key[i] = (unsigned char) strtol(tmp, NULL, 16);
4381 supplicant_dbus_dict_append_fixed_array(dict,
4386 } else if (size == 5 || size == 13) {
4387 unsigned char *key = g_try_malloc(13);
4393 for (i = 0; i < size; i++)
4394 key[i] = (unsigned char) ssid->passphrase[i];
4396 supplicant_dbus_dict_append_fixed_array(dict,
4402 supplicant_dbus_dict_append_basic(dict,
4407 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
4408 DBUS_TYPE_UINT32, &key_index);
4412 static dbus_bool_t is_psk_raw_key(const char *psk)
4416 /* A raw key is always 64 bytes length... */
4417 if (strlen(psk) != 64)
4420 /* ... and its content is in hex representation */
4421 for (i = 0; i < 64; i++)
4422 if (!isxdigit((unsigned char) psk[i]))
4428 static unsigned char hexchar2bin(char c)
4430 if ((c >= '0') && (c <= '9'))
4432 else if ((c >= 'A') && (c <= 'F'))
4433 return c - 'A' + 10;
4434 else if ((c >= 'a') && (c <= 'f'))
4435 return c - 'a' + 10;
4440 static void hexstring2bin(const char *string, unsigned char *data,
4445 for (i = 0; i < data_len; i++)
4446 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
4447 hexchar2bin(string[i * 2 + 1]) << 0);
4450 static void add_network_security_psk(DBusMessageIter *dict,
4451 GSupplicantSSID *ssid)
4453 if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
4454 const char *key = "psk";
4456 if (is_psk_raw_key(ssid->passphrase)) {
4457 unsigned char data[32];
4458 unsigned char *datap = data;
4460 /* The above pointer alias is required by D-Bus because
4461 * with D-Bus and GCC, non-heap-allocated arrays cannot
4462 * be passed directly by their base pointer. */
4464 hexstring2bin(ssid->passphrase, datap, sizeof(data));
4466 supplicant_dbus_dict_append_fixed_array(dict,
4467 key, DBUS_TYPE_BYTE,
4468 &datap, sizeof(data));
4470 supplicant_dbus_dict_append_basic(dict,
4471 key, DBUS_TYPE_STRING,
4476 static void add_network_security_tls(DBusMessageIter *dict,
4477 GSupplicantSSID *ssid)
4480 * For TLS, we at least need:
4481 * The client certificate
4482 * The client private key file
4483 * The client private key file password
4485 * The Authority certificate is optional.
4487 if (!ssid->client_cert_path)
4490 if (!ssid->private_key_path)
4493 if (!ssid->private_key_passphrase)
4496 if (ssid->ca_cert_path)
4497 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4498 DBUS_TYPE_STRING, &ssid->ca_cert_path);
4500 supplicant_dbus_dict_append_basic(dict, "private_key",
4502 &ssid->private_key_path);
4503 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4505 &ssid->private_key_passphrase);
4506 supplicant_dbus_dict_append_basic(dict, "client_cert",
4508 &ssid->client_cert_path);
4511 static void add_network_security_peap(DBusMessageIter *dict,
4512 GSupplicantSSID *ssid)
4517 * For PEAP/TTLS, we at least need
4518 * The authority certificate
4519 * The 2nd phase authentication method
4520 * The 2nd phase passphrase
4522 * The Client certificate is optional although strongly recommended
4523 * When setting it, we need in addition
4524 * The Client private key file
4525 * The Client private key file password
4527 if (!ssid->passphrase)
4530 if (!ssid->phase2_auth)
4533 if (ssid->client_cert_path) {
4534 if (!ssid->private_key_path)
4537 if (!ssid->private_key_passphrase)
4540 supplicant_dbus_dict_append_basic(dict, "client_cert",
4542 &ssid->client_cert_path);
4544 supplicant_dbus_dict_append_basic(dict, "private_key",
4546 &ssid->private_key_path);
4548 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4550 &ssid->private_key_passphrase);
4554 if (g_str_has_prefix(ssid->phase2_auth, "EAP-")) {
4555 phase2_auth = g_strdup_printf("autheap=%s",
4556 ssid->phase2_auth + strlen("EAP-"));
4558 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
4560 supplicant_dbus_dict_append_basic(dict, "password",
4564 if (ssid->ca_cert_path)
4565 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4567 &ssid->ca_cert_path);
4569 supplicant_dbus_dict_append_basic(dict, "phase2",
4573 g_free(phase2_auth);
4576 static void add_network_security_eap(DBusMessageIter *dict,
4577 GSupplicantSSID *ssid)
4581 #if defined TIZEN_EXT
4584 if (!ssid->eap || !ssid->identity)
4588 if (g_strcmp0(ssid->eap, "tls") == 0) {
4589 add_network_security_tls(dict, ssid);
4590 } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
4591 g_strcmp0(ssid->eap, "ttls") == 0) {
4592 #if defined TIZEN_EXT
4593 if (!ssid->identity)
4596 add_network_security_peap(dict, ssid);
4598 #if defined TIZEN_EXT
4599 } else if (g_strcmp0(ssid->eap, "sim") == 0 ||
4600 g_strcmp0(ssid->eap, "aka") == 0) {
4605 eap_value = g_ascii_strup(ssid->eap, -1);
4607 supplicant_dbus_dict_append_basic(dict, "eap",
4610 #if defined TIZEN_EXT
4611 if (ssid->identity != NULL)
4612 supplicant_dbus_dict_append_basic(dict, "identity",
4616 supplicant_dbus_dict_append_basic(dict, "identity",
4624 static void add_network_security_ciphers(DBusMessageIter *dict,
4625 GSupplicantSSID *ssid)
4627 unsigned int p_cipher, g_cipher, i;
4628 char *pairwise, *group;
4629 char *pair_ciphers[4];
4630 char *group_ciphers[5];
4632 p_cipher = ssid->pairwise_cipher;
4633 g_cipher = ssid->group_cipher;
4635 if (p_cipher == 0 && g_cipher == 0)
4640 if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
4641 pair_ciphers[i++] = "CCMP";
4643 if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
4644 pair_ciphers[i++] = "TKIP";
4646 if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
4647 pair_ciphers[i++] = "NONE";
4649 pair_ciphers[i] = NULL;
4653 if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
4654 group_ciphers[i++] = "CCMP";
4656 if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
4657 group_ciphers[i++] = "TKIP";
4659 if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
4660 group_ciphers[i++] = "WEP104";
4662 if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
4663 group_ciphers[i++] = "WEP40";
4665 group_ciphers[i] = NULL;
4667 pairwise = g_strjoinv(" ", pair_ciphers);
4668 group = g_strjoinv(" ", group_ciphers);
4670 SUPPLICANT_DBG("cipher %s %s", pairwise, group);
4672 supplicant_dbus_dict_append_basic(dict, "pairwise",
4675 supplicant_dbus_dict_append_basic(dict, "group",
4683 static void add_network_security_proto(DBusMessageIter *dict,
4684 GSupplicantSSID *ssid)
4686 unsigned int protocol, i;
4690 protocol = ssid->protocol;
4697 if (protocol & G_SUPPLICANT_PROTO_RSN)
4698 protos[i++] = "RSN";
4700 if (protocol & G_SUPPLICANT_PROTO_WPA)
4701 protos[i++] = "WPA";
4705 proto = g_strjoinv(" ", protos);
4707 SUPPLICANT_DBG("proto %s", proto);
4709 supplicant_dbus_dict_append_basic(dict, "proto",
4716 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
4720 switch (ssid->security) {
4721 case G_SUPPLICANT_SECURITY_NONE:
4723 add_network_security_none(dict);
4724 add_network_security_ciphers(dict, ssid);
4726 case G_SUPPLICANT_SECURITY_UNKNOWN:
4727 case G_SUPPLICANT_SECURITY_WEP:
4729 add_network_security_wep(dict, ssid);
4730 add_network_security_ciphers(dict, ssid);
4732 case G_SUPPLICANT_SECURITY_PSK:
4733 key_mgmt = "WPA-PSK";
4734 add_network_security_psk(dict, ssid);
4735 add_network_security_ciphers(dict, ssid);
4736 add_network_security_proto(dict, ssid);
4738 case G_SUPPLICANT_SECURITY_IEEE8021X:
4739 key_mgmt = "WPA-EAP";
4740 add_network_security_eap(dict, ssid);
4741 add_network_security_ciphers(dict, ssid);
4742 add_network_security_proto(dict, ssid);
4746 supplicant_dbus_dict_append_basic(dict, "key_mgmt",
4747 DBUS_TYPE_STRING, &key_mgmt);
4750 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
4754 switch (ssid->mode) {
4755 case G_SUPPLICANT_MODE_UNKNOWN:
4756 case G_SUPPLICANT_MODE_INFRA:
4759 case G_SUPPLICANT_MODE_IBSS:
4762 case G_SUPPLICANT_MODE_MASTER:
4767 supplicant_dbus_dict_append_basic(dict, "mode",
4768 DBUS_TYPE_UINT32, &mode);
4771 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
4773 DBusMessageIter dict;
4774 struct interface_connect_data *data = user_data;
4775 GSupplicantSSID *ssid = data->ssid;
4777 supplicant_dbus_dict_open(iter, &dict);
4779 if (ssid->scan_ssid)
4780 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
4781 DBUS_TYPE_UINT32, &ssid->scan_ssid);
4784 supplicant_dbus_dict_append_basic(&dict, "frequency",
4785 DBUS_TYPE_UINT32, &ssid->freq);
4788 supplicant_dbus_dict_append_basic(&dict, "bgscan",
4789 DBUS_TYPE_STRING, &ssid->bgscan);
4791 add_network_mode(&dict, ssid);
4793 add_network_security(&dict, ssid);
4795 supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
4796 DBUS_TYPE_BYTE, &ssid->ssid,
4799 supplicant_dbus_dict_append_basic(&dict, "ignore_broadcast_ssid",
4801 &ssid->ignore_broadcast_ssid);
4803 supplicant_dbus_dict_close(iter, &dict);
4806 static void interface_wps_start_result(const char *error,
4807 DBusMessageIter *iter, void *user_data)
4809 struct interface_connect_data *data = user_data;
4816 SUPPLICANT_DBG("error: %s", error);
4817 err = parse_supplicant_error(iter);
4821 data->callback(err, data->interface, data->user_data);
4828 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
4830 struct interface_connect_data *data = user_data;
4831 GSupplicantSSID *ssid = data->ssid;
4832 const char *role = "enrollee", *type;
4833 DBusMessageIter dict;
4837 supplicant_dbus_dict_open(iter, &dict);
4839 supplicant_dbus_dict_append_basic(&dict, "Role",
4840 DBUS_TYPE_STRING, &role);
4843 if (ssid->pin_wps) {
4845 supplicant_dbus_dict_append_basic(&dict, "Pin",
4846 DBUS_TYPE_STRING, &ssid->pin_wps);
4849 supplicant_dbus_dict_append_basic(&dict, "Type",
4850 DBUS_TYPE_STRING, &type);
4852 #if defined TIZEN_EXT
4854 supplicant_dbus_dict_append_fixed_array(&dict, "Bssid",
4855 DBUS_TYPE_BYTE, &ssid->bssid, 6);
4858 supplicant_dbus_dict_close(iter, &dict);
4861 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
4863 struct interface_connect_data *data = user_data;
4868 SUPPLICANT_DBG("error: %s", error);
4874 #if defined TIZEN_EXT
4875 GSupplicantSSID *ssid = data->ssid;
4876 if (ssid->pin_wps != NULL) {
4877 if (!g_utf8_validate(ssid->pin_wps, 8, NULL)) {
4878 SUPPLICANT_DBG("Invalid characters in WPS_PIN");
4885 supplicant_dbus_method_call(data->interface->path,
4886 SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
4887 interface_add_wps_params,
4888 interface_wps_start_result, data, NULL);
4891 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
4893 dbus_bool_t credentials = TRUE;
4897 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
4901 int g_supplicant_interface_connect(GSupplicantInterface *interface,
4902 GSupplicantSSID *ssid,
4903 GSupplicantInterfaceCallback callback,
4906 struct interface_connect_data *data;
4912 if (!system_available)
4915 /* TODO: Check if we're already connected and switch */
4917 data = dbus_malloc0(sizeof(*data));
4921 data->interface = interface;
4922 data->path = g_strdup(interface->path);
4923 data->callback = callback;
4925 data->user_data = user_data;
4927 if (ssid->use_wps) {
4928 g_free(interface->wps_cred.key);
4929 memset(&interface->wps_cred, 0,
4930 sizeof(struct _GSupplicantWpsCredentials));
4932 ret = supplicant_dbus_property_set(interface->path,
4933 SUPPLICANT_INTERFACE ".Interface.WPS",
4934 "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
4935 wps_process_credentials, wps_start, data, interface);
4937 ret = supplicant_dbus_method_call(interface->path,
4938 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
4939 interface_add_network_params,
4940 interface_add_network_result, data,
4949 return -EINPROGRESS;
4952 static void network_remove_result(const char *error,
4953 DBusMessageIter *iter, void *user_data)
4955 struct interface_data *data = user_data;
4962 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
4964 result = -ECONNABORTED;
4970 data->callback(result, data->interface, data->user_data);
4975 static void network_remove_params(DBusMessageIter *iter, void *user_data)
4977 struct interface_data *data = user_data;
4978 const char *path = data->interface->network_path;
4980 SUPPLICANT_DBG("path %s", path);
4982 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
4985 static int network_remove(struct interface_data *data)
4987 GSupplicantInterface *interface = data->interface;
4991 #if defined TIZEN_EXT
4992 GSupplicantInterface *intf = NULL;
4994 * Check if 'interface' is valid
4996 intf = g_hash_table_lookup(interface_table, interface->path);
5001 return supplicant_dbus_method_call(interface->path,
5002 SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
5003 network_remove_params, network_remove_result, data,
5007 static void interface_disconnect_result(const char *error,
5008 DBusMessageIter *iter, void *user_data)
5010 struct interface_data *data = user_data;
5017 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
5019 result = -ECONNABORTED;
5022 if (result < 0 && data->callback) {
5023 data->callback(result, data->interface, data->user_data);
5024 data->callback = NULL;
5027 /* If we are disconnecting from previous WPS successful
5028 * association. i.e.: it did not went through AddNetwork,
5029 * and interface->network_path was never set. */
5030 if (!data->interface->network_path) {
5036 if (result != -ECONNABORTED) {
5037 if (network_remove(data) < 0) {
5047 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
5048 GSupplicantInterfaceCallback callback,
5051 struct interface_data *data;
5059 if (!system_available)
5062 data = dbus_malloc0(sizeof(*data));
5066 data->interface = interface;
5067 data->path = g_strdup(interface->path);
5068 data->callback = callback;
5069 data->user_data = user_data;
5071 ret = supplicant_dbus_method_call(interface->path,
5072 SUPPLICANT_INTERFACE ".Interface", "Disconnect",
5073 NULL, interface_disconnect_result, data,
5084 static void interface_p2p_find_result(const char *error,
5085 DBusMessageIter *iter, void *user_data)
5087 struct interface_scan_data *data = user_data;
5090 SUPPLICANT_DBG("error %s", error);
5095 if (interface_exists(data->interface, data->path)) {
5096 if (!data->interface->ready)
5099 data->interface->p2p_finding = true;
5103 data->callback(err, data->interface, data->user_data);
5109 static void interface_p2p_find_params(DBusMessageIter *iter, void *user_data)
5111 DBusMessageIter dict;
5113 supplicant_dbus_dict_open(iter, &dict);
5114 supplicant_dbus_dict_close(iter, &dict);
5117 int g_supplicant_interface_p2p_find(GSupplicantInterface *interface,
5118 GSupplicantInterfaceCallback callback,
5121 struct interface_scan_data *data;
5124 if (!interface->p2p_support)
5127 ret = interface_ready_to_scan(interface);
5128 if (ret && ret != -EALREADY)
5131 data = dbus_malloc0(sizeof(*data));
5135 data->interface = interface;
5136 data->path = g_strdup(interface->path);
5137 data->callback = callback;
5138 data->user_data = user_data;
5140 ret = supplicant_dbus_method_call(interface->path,
5141 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Find",
5142 interface_p2p_find_params, interface_p2p_find_result,
5152 bool g_supplicant_interface_is_p2p_finding(GSupplicantInterface *interface)
5157 return interface->p2p_finding;
5160 int g_supplicant_interface_p2p_stop_find(GSupplicantInterface *interface)
5162 if (!interface->p2p_finding)
5167 interface->p2p_finding = false;
5169 return supplicant_dbus_method_call(interface->path,
5170 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "StopFind",
5171 NULL, NULL, NULL, NULL);
5174 static void interface_p2p_connect_result(const char *error,
5175 DBusMessageIter *iter, void *user_data)
5177 struct interface_connect_data *data = user_data;
5183 err = parse_supplicant_error(iter);
5186 data->callback(err, data->interface, data->user_data);
5189 g_free(data->peer->wps_pin);
5190 g_free(data->peer->path);
5195 static void interface_p2p_connect_params(DBusMessageIter *iter, void *user_data)
5197 struct interface_connect_data *data = user_data;
5198 const char *wps = "pbc";
5199 DBusMessageIter dict;
5204 supplicant_dbus_dict_open(iter, &dict);
5206 if (data->peer->master)
5209 if (data->peer->wps_pin)
5212 supplicant_dbus_dict_append_basic(&dict, "peer",
5213 DBUS_TYPE_OBJECT_PATH, &data->peer->path);
5214 supplicant_dbus_dict_append_basic(&dict, "wps_method",
5215 DBUS_TYPE_STRING, &wps);
5216 if (data->peer->wps_pin) {
5217 supplicant_dbus_dict_append_basic(&dict, "pin",
5218 DBUS_TYPE_STRING, &data->peer->wps_pin);
5221 supplicant_dbus_dict_append_basic(&dict, "go_intent",
5222 DBUS_TYPE_INT32, &go_intent);
5224 supplicant_dbus_dict_close(iter, &dict);
5227 int g_supplicant_interface_p2p_connect(GSupplicantInterface *interface,
5228 GSupplicantPeerParams *peer_params,
5229 GSupplicantInterfaceCallback callback,
5232 struct interface_connect_data *data;
5237 if (!interface->p2p_support)
5240 data = dbus_malloc0(sizeof(*data));
5241 data->interface = interface;
5242 data->path = g_strdup(interface->path);
5243 data->peer = peer_params;
5244 data->callback = callback;
5245 data->user_data = user_data;
5247 ret = supplicant_dbus_method_call(interface->path,
5248 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Connect",
5249 interface_p2p_connect_params, interface_p2p_connect_result,
5257 return -EINPROGRESS;
5260 int g_supplicant_interface_p2p_disconnect(GSupplicantInterface *interface,
5261 GSupplicantPeerParams *peer_params)
5263 GSupplicantPeer *peer;
5269 if (!interface->p2p_support)
5272 peer = g_hash_table_lookup(interface->peer_table, peer_params->path);
5276 for (list = peer->groups; list; list = list->next, count++) {
5277 const char *group_obj_path = list->data;
5278 GSupplicantInterface *g_interface;
5279 GSupplicantGroup *group;
5281 group = g_hash_table_lookup(group_mapping, group_obj_path);
5282 if (!group || !group->interface)
5285 g_interface = group->interface;
5286 supplicant_dbus_method_call(g_interface->path,
5287 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5288 "Disconnect", NULL, NULL, NULL, g_interface);
5291 if (count == 0 && peer->current_group_iface) {
5292 supplicant_dbus_method_call(peer->current_group_iface->path,
5293 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5294 "Disconnect", NULL, NULL, NULL,
5295 peer->current_group_iface->path);
5298 peer->current_group_iface = NULL;
5300 return -EINPROGRESS;
5303 struct p2p_service_data {
5305 GSupplicantInterface *interface;
5306 GSupplicantP2PServiceParams *service;
5307 GSupplicantInterfaceCallback callback;
5311 static void interface_p2p_service_result(const char *error,
5312 DBusMessageIter *iter, void *user_data)
5314 struct p2p_service_data *data = user_data;
5317 SUPPLICANT_DBG("%s result - %s", data->registration ?
5318 "Registration" : "Deletion",
5319 error ? error : "Success");
5324 data->callback(result, data->interface, data->user_data);
5326 g_free(data->service->query);
5327 g_free(data->service->response);
5328 g_free(data->service->service);
5329 g_free(data->service->wfd_ies);
5330 g_free(data->service);
5334 static void interface_p2p_service_params(DBusMessageIter *iter,
5337 struct p2p_service_data *data = user_data;
5338 GSupplicantP2PServiceParams *service;
5339 DBusMessageIter dict;
5344 service = data->service;
5346 supplicant_dbus_dict_open(iter, &dict);
5348 if (service->query && service->response) {
5350 supplicant_dbus_dict_append_basic(&dict, "service_type",
5351 DBUS_TYPE_STRING, &type);
5352 supplicant_dbus_dict_append_fixed_array(&dict, "query",
5353 DBUS_TYPE_BYTE, &service->query,
5354 service->query_length);
5355 supplicant_dbus_dict_append_fixed_array(&dict, "response",
5356 DBUS_TYPE_BYTE, &service->response,
5357 service->response_length);
5358 } else if (service->version && service->service) {
5360 supplicant_dbus_dict_append_basic(&dict, "service_type",
5361 DBUS_TYPE_STRING, &type);
5362 supplicant_dbus_dict_append_basic(&dict, "version",
5363 DBUS_TYPE_INT32, &service->version);
5364 supplicant_dbus_dict_append_basic(&dict, "service",
5365 DBUS_TYPE_STRING, &service->service);
5368 supplicant_dbus_dict_close(iter, &dict);
5371 int g_supplicant_interface_p2p_add_service(GSupplicantInterface *interface,
5372 GSupplicantInterfaceCallback callback,
5373 GSupplicantP2PServiceParams *p2p_service_params,
5376 struct p2p_service_data *data;
5381 if (!interface->p2p_support)
5384 data = dbus_malloc0(sizeof(*data));
5385 data->registration = true;
5386 data->interface = interface;
5387 data->service = p2p_service_params;
5388 data->callback = callback;
5389 data->user_data = user_data;
5391 ret = supplicant_dbus_method_call(interface->path,
5392 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "AddService",
5393 interface_p2p_service_params, interface_p2p_service_result,
5400 return -EINPROGRESS;
5403 int g_supplicant_interface_p2p_del_service(GSupplicantInterface *interface,
5404 GSupplicantP2PServiceParams *p2p_service_params)
5406 struct p2p_service_data *data;
5411 if (!interface->p2p_support)
5414 data = dbus_malloc0(sizeof(*data));
5415 data->interface = interface;
5416 data->service = p2p_service_params;
5418 ret = supplicant_dbus_method_call(interface->path,
5419 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeleteService",
5420 interface_p2p_service_params, interface_p2p_service_result,
5427 return -EINPROGRESS;
5430 struct p2p_listen_data {
5435 static void interface_p2p_listen_params(DBusMessageIter *iter, void *user_data)
5437 struct p2p_listen_data *params = user_data;
5438 DBusMessageIter dict;
5440 supplicant_dbus_dict_open(iter, &dict);
5442 supplicant_dbus_dict_append_basic(&dict, "period",
5443 DBUS_TYPE_INT32, ¶ms->period);
5444 supplicant_dbus_dict_append_basic(&dict, "interval",
5445 DBUS_TYPE_INT32, ¶ms->interval);
5446 supplicant_dbus_dict_close(iter, &dict);
5449 int g_supplicant_interface_p2p_listen(GSupplicantInterface *interface,
5450 int period, int interval)
5452 struct p2p_listen_data params;
5456 if (!interface->p2p_support)
5459 params.period = period;
5460 params.interval = interval;
5462 return supplicant_dbus_method_call(interface->path,
5463 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5464 "ExtendedListen", interface_p2p_listen_params,
5465 NULL, ¶ms, NULL);
5468 static void widi_ies_params(DBusMessageIter *iter, void *user_data)
5470 struct p2p_service_data *data = user_data;
5471 GSupplicantP2PServiceParams *service = data->service;
5472 DBusMessageIter array;
5474 SUPPLICANT_DBG("%p - %d", service->wfd_ies, service->wfd_ies_length);
5476 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
5477 DBUS_TYPE_BYTE_AS_STRING, &array);
5479 if (service->wfd_ies && service->wfd_ies_length > 0) {
5480 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
5481 &service->wfd_ies, service->wfd_ies_length);
5484 dbus_message_iter_close_container(iter, &array);
5487 int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
5488 GSupplicantInterfaceCallback callback,
5491 struct p2p_service_data *data;
5496 if (!system_available)
5499 data = dbus_malloc0(sizeof(*data));
5500 data->service = p2p_service_params;
5501 data->callback = callback;
5502 data->user_data = user_data;
5504 if (p2p_service_params->wfd_ies)
5505 data->registration = true;
5507 ret = supplicant_dbus_property_set(SUPPLICANT_PATH,
5508 SUPPLICANT_INTERFACE, "WFDIEs",
5509 DBUS_TYPE_ARRAY_AS_STRING
5510 DBUS_TYPE_BYTE_AS_STRING,
5512 interface_p2p_service_result,
5514 if (ret < 0 && ret != -EINPROGRESS) {
5519 return -EINPROGRESS;
5523 static const char *g_supplicant_rule0 = "type=signal,"
5524 "path=" DBUS_PATH_DBUS ","
5525 "sender=" DBUS_SERVICE_DBUS ","
5526 "interface=" DBUS_INTERFACE_DBUS ","
5527 "member=NameOwnerChanged,"
5528 "arg0=" SUPPLICANT_SERVICE;
5529 static const char *g_supplicant_rule1 = "type=signal,"
5530 "interface=" SUPPLICANT_INTERFACE;
5531 static const char *g_supplicant_rule2 = "type=signal,"
5532 "interface=" SUPPLICANT_INTERFACE ".Interface";
5533 static const char *g_supplicant_rule3 = "type=signal,"
5534 "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
5535 static const char *g_supplicant_rule4 = "type=signal,"
5536 "interface=" SUPPLICANT_INTERFACE ".BSS";
5537 static const char *g_supplicant_rule5 = "type=signal,"
5538 "interface=" SUPPLICANT_INTERFACE ".Network";
5539 #if !defined TIZEN_EXT
5540 static const char *g_supplicant_rule6 = "type=signal,"
5541 "interface=" SUPPLICANT_INTERFACE ".Interface.P2PDevice";
5542 static const char *g_supplicant_rule7 = "type=signal,"
5543 "interface=" SUPPLICANT_INTERFACE ".Peer";
5544 static const char *g_supplicant_rule8 = "type=signal,"
5545 "interface=" SUPPLICANT_INTERFACE ".Group";
5548 static void invoke_introspect_method(void)
5550 DBusMessage *message;
5552 message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
5554 DBUS_INTERFACE_INTROSPECTABLE,
5560 dbus_message_set_no_reply(message, TRUE);
5561 dbus_connection_send(connection, message, NULL);
5562 dbus_message_unref(message);
5565 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
5567 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
5571 if (!dbus_connection_add_filter(connection, g_supplicant_filter,
5573 dbus_connection_unref(connection);
5578 callbacks_pointer = callbacks;
5581 interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
5582 NULL, remove_interface);
5584 bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5586 peer_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5588 group_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5590 pending_peer_connection = g_hash_table_new_full(g_str_hash, g_str_equal,
5593 supplicant_dbus_setup(connection);
5595 dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
5596 dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
5597 dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
5598 dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
5599 dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
5600 dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
5601 #if defined TIZEN_EXT
5602 dbus_bus_add_match(connection,
5603 "type=signal,interface=org.tizen.system.deviced.PowerOff,"
5604 "member=ChangeState", NULL);
5606 #if !defined TIZEN_EXT
5607 dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
5608 dbus_bus_add_match(connection, g_supplicant_rule7, NULL);
5609 dbus_bus_add_match(connection, g_supplicant_rule8, NULL);
5611 dbus_connection_flush(connection);
5613 if (dbus_bus_name_has_owner(connection,
5614 SUPPLICANT_SERVICE, NULL)) {
5615 system_available = TRUE;
5616 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
5617 SUPPLICANT_INTERFACE,
5618 service_property, NULL, NULL);
5620 invoke_introspect_method();
5625 static void unregister_interface_remove_params(DBusMessageIter *iter,
5628 const char *path = user_data;
5630 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
5635 static void unregister_remove_interface(gpointer key, gpointer value,
5638 GSupplicantInterface *interface = value;
5640 supplicant_dbus_method_call(SUPPLICANT_PATH,
5641 SUPPLICANT_INTERFACE,
5643 unregister_interface_remove_params,
5644 NULL, interface->path, NULL);
5647 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
5652 #if !defined TIZEN_EXT
5653 dbus_bus_remove_match(connection, g_supplicant_rule8, NULL);
5654 dbus_bus_remove_match(connection, g_supplicant_rule7, NULL);
5655 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
5657 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
5658 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
5659 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
5660 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
5661 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
5662 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
5663 dbus_connection_flush(connection);
5665 dbus_connection_remove_filter(connection,
5666 g_supplicant_filter, NULL);
5670 g_hash_table_destroy(bss_mapping);
5675 g_hash_table_destroy(peer_mapping);
5676 peer_mapping = NULL;
5679 if (group_mapping) {
5680 g_hash_table_destroy(group_mapping);
5681 group_mapping = NULL;
5684 if (interface_table) {
5685 g_hash_table_foreach(interface_table,
5686 unregister_remove_interface, NULL);
5687 g_hash_table_destroy(interface_table);
5688 interface_table = NULL;
5691 if (system_available)
5692 callback_system_killed();
5695 dbus_connection_unref(connection);
5699 callbacks_pointer = NULL;