| /* |
| * |
| * WPA supplicant library with GLib integration |
| * |
| * Copyright (C) 2012-2013 Intel Corporation. All rights reserved. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| * |
| */ |
| |
| #ifdef HAVE_CONFIG_H |
| #include <config.h> |
| #endif |
| |
| #include <errno.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdint.h> |
| #include <syslog.h> |
| #include <ctype.h> |
| #include <stdbool.h> |
| #include <netinet/if_ether.h> |
| #include <netinet/in.h> |
| |
| #include <glib.h> |
| #include <gdbus.h> |
| |
| #include "dbus.h" |
| #include "gsupplicant.h" |
| |
| #define IEEE80211_CAP_ESS 0x0001 |
| #define IEEE80211_CAP_IBSS 0x0002 |
| #define IEEE80211_CAP_PRIVACY 0x0010 |
| |
| #define BSS_UNKNOWN_STRENGTH -90 |
| |
| static DBusConnection *connection; |
| |
| static const GSupplicantCallbacks *callbacks_pointer; |
| |
| static dbus_bool_t system_available = FALSE; |
| static dbus_bool_t system_ready = FALSE; |
| |
| static dbus_int32_t debug_level; |
| static dbus_bool_t debug_timestamp = FALSE; |
| static dbus_bool_t debug_showkeys = FALSE; |
| |
| static const char *debug_strings[] = { |
| "msgdump", "debug", "info", "warning", "error", NULL |
| }; |
| |
| static unsigned int eap_methods; |
| |
| struct strvalmap { |
| const char *str; |
| unsigned int val; |
| }; |
| |
| static struct strvalmap eap_method_map[] = { |
| { "MD5", G_SUPPLICANT_EAP_METHOD_MD5 }, |
| { "TLS", G_SUPPLICANT_EAP_METHOD_TLS }, |
| { "MSCHAPV2", G_SUPPLICANT_EAP_METHOD_MSCHAPV2 }, |
| { "PEAP", G_SUPPLICANT_EAP_METHOD_PEAP }, |
| { "TTLS", G_SUPPLICANT_EAP_METHOD_TTLS }, |
| { "GTC", G_SUPPLICANT_EAP_METHOD_GTC }, |
| { "OTP", G_SUPPLICANT_EAP_METHOD_OTP }, |
| { "LEAP", G_SUPPLICANT_EAP_METHOD_LEAP }, |
| { "WSC", G_SUPPLICANT_EAP_METHOD_WSC }, |
| { } |
| }; |
| |
| static struct strvalmap keymgmt_map[] = { |
| { "none", G_SUPPLICANT_KEYMGMT_NONE }, |
| { "ieee8021x", G_SUPPLICANT_KEYMGMT_IEEE8021X }, |
| { "wpa-none", G_SUPPLICANT_KEYMGMT_WPA_NONE }, |
| { "wpa-psk", G_SUPPLICANT_KEYMGMT_WPA_PSK }, |
| { "wpa-psk-sha256", G_SUPPLICANT_KEYMGMT_WPA_PSK_256 }, |
| { "wpa-ft-psk", G_SUPPLICANT_KEYMGMT_WPA_FT_PSK }, |
| { "wpa-ft-eap", G_SUPPLICANT_KEYMGMT_WPA_FT_EAP }, |
| { "wpa-eap", G_SUPPLICANT_KEYMGMT_WPA_EAP }, |
| { "wpa-eap-sha256", G_SUPPLICANT_KEYMGMT_WPA_EAP_256 }, |
| { "wps", G_SUPPLICANT_KEYMGMT_WPS }, |
| { } |
| }; |
| |
| static struct strvalmap authalg_capa_map[] = { |
| { "open", G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN }, |
| { "shared", G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED }, |
| { "leap", G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP }, |
| { } |
| }; |
| |
| static struct strvalmap proto_capa_map[] = { |
| { "wpa", G_SUPPLICANT_CAPABILITY_PROTO_WPA }, |
| { "rsn", G_SUPPLICANT_CAPABILITY_PROTO_RSN }, |
| { } |
| }; |
| |
| static struct strvalmap group_map[] = { |
| { "wep40", G_SUPPLICANT_GROUP_WEP40 }, |
| { "wep104", G_SUPPLICANT_GROUP_WEP104 }, |
| { "tkip", G_SUPPLICANT_GROUP_TKIP }, |
| { "ccmp", G_SUPPLICANT_GROUP_CCMP }, |
| { } |
| }; |
| |
| static struct strvalmap pairwise_map[] = { |
| { "none", G_SUPPLICANT_PAIRWISE_NONE }, |
| { "tkip", G_SUPPLICANT_PAIRWISE_TKIP }, |
| { "ccmp", G_SUPPLICANT_PAIRWISE_CCMP }, |
| { } |
| }; |
| |
| static struct strvalmap scan_capa_map[] = { |
| { "active", G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE }, |
| { "passive", G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE }, |
| { "ssid", G_SUPPLICANT_CAPABILITY_SCAN_SSID }, |
| { } |
| }; |
| |
| static struct strvalmap mode_capa_map[] = { |
| { "infrastructure", G_SUPPLICANT_CAPABILITY_MODE_INFRA }, |
| { "ad-hoc", G_SUPPLICANT_CAPABILITY_MODE_IBSS }, |
| { "ap", G_SUPPLICANT_CAPABILITY_MODE_AP }, |
| { } |
| }; |
| |
| static GHashTable *interface_table; |
| static GHashTable *bss_mapping; |
| static GHashTable *peer_mapping; |
| static GHashTable *group_mapping; |
| static GHashTable *pending_peer_connection; |
| |
| struct _GSupplicantWpsCredentials { |
| unsigned char ssid[32]; |
| unsigned int ssid_len; |
| char *key; |
| }; |
| |
| struct added_network_information { |
| char * ssid; |
| GSupplicantSecurity security; |
| char * passphrase; |
| char * private_passphrase; |
| }; |
| |
| struct _GSupplicantInterface { |
| char *path; |
| char *network_path; |
| unsigned int keymgmt_capa; |
| unsigned int authalg_capa; |
| unsigned int proto_capa; |
| unsigned int group_capa; |
| unsigned int pairwise_capa; |
| unsigned int scan_capa; |
| unsigned int mode_capa; |
| unsigned int max_scan_ssids; |
| bool p2p_support; |
| bool p2p_finding; |
| bool ap_create_in_progress; |
| dbus_bool_t ready; |
| GSupplicantState state; |
| dbus_bool_t scanning; |
| GSupplicantInterfaceCallback scan_callback; |
| void *scan_data; |
| int apscan; |
| char *ifname; |
| char *driver; |
| char *bridge; |
| struct _GSupplicantWpsCredentials wps_cred; |
| GSupplicantWpsState wps_state; |
| GHashTable *network_table; |
| GHashTable *peer_table; |
| GHashTable *group_table; |
| GHashTable *net_mapping; |
| GHashTable *bss_mapping; |
| void *data; |
| const char *pending_peer_path; |
| struct added_network_information network_info; |
| }; |
| |
| struct g_supplicant_bss { |
| GSupplicantInterface *interface; |
| char *path; |
| unsigned char bssid[6]; |
| unsigned char ssid[32]; |
| unsigned int ssid_len; |
| dbus_uint16_t frequency; |
| dbus_uint32_t maxrate; |
| dbus_int16_t signal; |
| GSupplicantMode mode; |
| GSupplicantSecurity security; |
| dbus_bool_t rsn_selected; |
| unsigned int wpa_keymgmt; |
| unsigned int wpa_pairwise; |
| unsigned int wpa_group; |
| unsigned int rsn_keymgmt; |
| unsigned int rsn_pairwise; |
| unsigned int rsn_group; |
| unsigned int keymgmt; |
| dbus_bool_t privacy; |
| dbus_bool_t psk; |
| dbus_bool_t ieee8021x; |
| unsigned int wps_capabilities; |
| }; |
| |
| struct _GSupplicantNetwork { |
| GSupplicantInterface *interface; |
| char *path; |
| char *group; |
| char *name; |
| unsigned char ssid[32]; |
| unsigned int ssid_len; |
| dbus_int16_t signal; |
| dbus_uint16_t frequency; |
| struct g_supplicant_bss *best_bss; |
| GSupplicantMode mode; |
| GSupplicantSecurity security; |
| dbus_bool_t wps; |
| unsigned int wps_capabilities; |
| GHashTable *bss_table; |
| GHashTable *config_table; |
| }; |
| |
| struct _GSupplicantPeer { |
| GSupplicantInterface *interface; |
| char *path; |
| unsigned char device_address[ETH_ALEN]; |
| unsigned char iface_address[ETH_ALEN]; |
| char *name; |
| unsigned char *widi_ies; |
| int widi_ies_length; |
| char *identifier; |
| unsigned int wps_capabilities; |
| GSList *groups; |
| const GSupplicantInterface *current_group_iface; |
| bool connection_requested; |
| }; |
| |
| struct _GSupplicantGroup { |
| GSupplicantInterface *interface; |
| GSupplicantInterface *orig_interface; |
| char *path; |
| int role; |
| GSList *members; |
| }; |
| |
| struct interface_data { |
| GSupplicantInterface *interface; |
| char *path; /* Interface path cannot be taken from interface (above) as |
| * it might have been freed already. |
| */ |
| GSupplicantInterfaceCallback callback; |
| void *user_data; |
| bool network_remove_in_progress; |
| GSupplicantSSID *ssid; |
| }; |
| |
| struct interface_create_data { |
| char *ifname; |
| char *driver; |
| char *bridge; |
| GSupplicantInterface *interface; |
| GSupplicantInterfaceCallback callback; |
| void *user_data; |
| }; |
| |
| struct interface_connect_data { |
| GSupplicantInterface *interface; |
| char *path; |
| GSupplicantInterfaceCallback callback; |
| void *user_data; |
| union { |
| GSupplicantSSID *ssid; |
| GSupplicantPeerParams *peer; |
| }; |
| }; |
| |
| struct interface_scan_data { |
| GSupplicantInterface *interface; |
| char *path; |
| GSupplicantInterfaceCallback callback; |
| GSupplicantScanParams *scan_params; |
| void *user_data; |
| }; |
| |
| static int network_remove(struct interface_data *data); |
| |
| static inline void debug(const char *format, ...) |
| { |
| char str[256]; |
| va_list ap; |
| |
| if (!callbacks_pointer->debug) |
| return; |
| |
| va_start(ap, format); |
| |
| if (vsnprintf(str, sizeof(str), format, ap) > 0) |
| callbacks_pointer->debug(str); |
| |
| va_end(ap); |
| } |
| |
| #define SUPPLICANT_DBG(fmt, arg...) \ |
| debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg); |
| |
| static GSupplicantMode string2mode(const char *mode) |
| { |
| if (!mode) |
| return G_SUPPLICANT_MODE_UNKNOWN; |
| |
| if (g_str_equal(mode, "infrastructure")) |
| return G_SUPPLICANT_MODE_INFRA; |
| else if (g_str_equal(mode, "ad-hoc")) |
| return G_SUPPLICANT_MODE_IBSS; |
| |
| return G_SUPPLICANT_MODE_UNKNOWN; |
| } |
| |
| static const char *mode2string(GSupplicantMode mode) |
| { |
| switch (mode) { |
| case G_SUPPLICANT_MODE_UNKNOWN: |
| break; |
| case G_SUPPLICANT_MODE_INFRA: |
| return "managed"; |
| case G_SUPPLICANT_MODE_IBSS: |
| return "adhoc"; |
| case G_SUPPLICANT_MODE_MASTER: |
| return "ap"; |
| } |
| |
| return NULL; |
| } |
| |
| static const char *security2string(GSupplicantSecurity security) |
| { |
| switch (security) { |
| case G_SUPPLICANT_SECURITY_UNKNOWN: |
| break; |
| case G_SUPPLICANT_SECURITY_NONE: |
| return "none"; |
| case G_SUPPLICANT_SECURITY_WEP: |
| return "wep"; |
| case G_SUPPLICANT_SECURITY_PSK: |
| return "psk"; |
| case G_SUPPLICANT_SECURITY_IEEE8021X: |
| return "ieee8021x"; |
| } |
| |
| return NULL; |
| } |
| |
| static GSupplicantState string2state(const char *state) |
| { |
| if (!state) |
| return G_SUPPLICANT_STATE_UNKNOWN; |
| |
| if (g_str_equal(state, "unknown")) |
| return G_SUPPLICANT_STATE_UNKNOWN; |
| else if (g_str_equal(state, "interface_disabled")) |
| return G_SUPPLICANT_STATE_DISABLED; |
| else if (g_str_equal(state, "disconnected")) |
| return G_SUPPLICANT_STATE_DISCONNECTED; |
| else if (g_str_equal(state, "inactive")) |
| return G_SUPPLICANT_STATE_INACTIVE; |
| else if (g_str_equal(state, "scanning")) |
| return G_SUPPLICANT_STATE_SCANNING; |
| else if (g_str_equal(state, "authenticating")) |
| return G_SUPPLICANT_STATE_AUTHENTICATING; |
| else if (g_str_equal(state, "associating")) |
| return G_SUPPLICANT_STATE_ASSOCIATING; |
| else if (g_str_equal(state, "associated")) |
| return G_SUPPLICANT_STATE_ASSOCIATED; |
| else if (g_str_equal(state, "group_handshake")) |
| return G_SUPPLICANT_STATE_GROUP_HANDSHAKE; |
| else if (g_str_equal(state, "4way_handshake")) |
| return G_SUPPLICANT_STATE_4WAY_HANDSHAKE; |
| else if (g_str_equal(state, "completed")) |
| return G_SUPPLICANT_STATE_COMPLETED; |
| |
| return G_SUPPLICANT_STATE_UNKNOWN; |
| } |
| |
| static bool compare_network_parameters(GSupplicantInterface *interface, GSupplicantSSID *ssid) |
| { |
| if (memcmp(interface->network_info.ssid, ssid->ssid, ssid->ssid_len)) |
| return FALSE; |
| |
| if (interface->network_info.security != ssid->security) |
| return FALSE; |
| |
| if (interface->network_info.passphrase && |
| g_strcmp0(interface->network_info.passphrase, ssid->passphrase) != 0) { |
| return FALSE; |
| } |
| |
| if (interface->network_info.private_passphrase && |
| g_strcmp0(interface->network_info.private_passphrase, ssid->private_key_passphrase) != 0) { |
| return FALSE; |
| } |
| |
| return TRUE; |
| } |
| |
| static void remove_network_information(GSupplicantInterface * interface) |
| { |
| g_free(interface->network_info.ssid); |
| g_free(interface->network_info.passphrase); |
| g_free(interface->network_info.private_passphrase); |
| interface->network_info.ssid = NULL; |
| interface->network_info.passphrase = NULL; |
| interface->network_info.private_passphrase = NULL; |
| } |
| |
| static int store_network_information(GSupplicantInterface * interface, GSupplicantSSID *ssid) |
| { |
| interface->network_info.ssid = g_malloc(ssid->ssid_len + 1); |
| if (interface->network_info.ssid != NULL) { |
| memcpy(interface->network_info.ssid, ssid->ssid, ssid->ssid_len); |
| interface->network_info.ssid[ssid->ssid_len] = '\0'; |
| } else { |
| return -ENOMEM; |
| } |
| |
| interface->network_info.security = ssid->security; |
| |
| if ((ssid->security == G_SUPPLICANT_SECURITY_WEP || |
| ssid->security == G_SUPPLICANT_SECURITY_PSK || |
| ssid->security == G_SUPPLICANT_SECURITY_NONE) && |
| ssid->passphrase) { |
| interface->network_info.passphrase = g_strdup(ssid->passphrase); |
| } |
| |
| if (ssid->security == G_SUPPLICANT_SECURITY_IEEE8021X && ssid->private_key_passphrase) { |
| interface->network_info.private_passphrase = g_strdup(ssid->private_key_passphrase); |
| } |
| |
| return 0; |
| } |
| |
| static void callback_system_ready(void) |
| { |
| if (system_ready) |
| return; |
| |
| system_ready = TRUE; |
| |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->system_ready) |
| return; |
| |
| callbacks_pointer->system_ready(); |
| } |
| |
| static void callback_system_killed(void) |
| { |
| system_ready = FALSE; |
| |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->system_killed) |
| return; |
| |
| callbacks_pointer->system_killed(); |
| } |
| |
| static void callback_interface_added(GSupplicantInterface *interface) |
| { |
| SUPPLICANT_DBG(""); |
| |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->interface_added) |
| return; |
| |
| callbacks_pointer->interface_added(interface); |
| } |
| |
| static void callback_interface_state(GSupplicantInterface *interface) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->interface_state) |
| return; |
| |
| callbacks_pointer->interface_state(interface); |
| } |
| |
| static void callback_interface_removed(GSupplicantInterface *interface) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->interface_removed) |
| return; |
| |
| callbacks_pointer->interface_removed(interface); |
| } |
| |
| static void callback_p2p_support(GSupplicantInterface *interface) |
| { |
| SUPPLICANT_DBG(""); |
| |
| return; |
| |
| if (!interface->p2p_support) |
| return; |
| |
| if (callbacks_pointer && callbacks_pointer->p2p_support) |
| callbacks_pointer->p2p_support(interface); |
| } |
| |
| static void callback_scan_started(GSupplicantInterface *interface) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->scan_started) |
| return; |
| |
| callbacks_pointer->scan_started(interface); |
| } |
| |
| static void callback_ap_create_fail(GSupplicantInterface *interface) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->scan_started) |
| return; |
| |
| callbacks_pointer->ap_create_fail(interface); |
| } |
| |
| static void callback_scan_finished(GSupplicantInterface *interface) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->scan_finished) |
| return; |
| |
| callbacks_pointer->scan_finished(interface); |
| } |
| |
| static void callback_network_added(GSupplicantNetwork *network) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->network_added) |
| return; |
| |
| callbacks_pointer->network_added(network); |
| } |
| |
| static void callback_network_removed(GSupplicantNetwork *network) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->network_removed) |
| return; |
| |
| callbacks_pointer->network_removed(network); |
| } |
| |
| static void callback_network_changed(GSupplicantNetwork *network, |
| const char *property) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->network_changed) |
| return; |
| |
| callbacks_pointer->network_changed(network, property); |
| } |
| |
| static void callback_peer_found(GSupplicantPeer *peer) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->peer_found) |
| return; |
| |
| callbacks_pointer->peer_found(peer); |
| } |
| |
| static void callback_peer_lost(GSupplicantPeer *peer) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->peer_lost) |
| return; |
| |
| callbacks_pointer->peer_lost(peer); |
| } |
| |
| static void callback_peer_changed(GSupplicantPeer *peer, |
| GSupplicantPeerState state) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->peer_changed) |
| return; |
| |
| callbacks_pointer->peer_changed(peer, state); |
| } |
| |
| static void callback_peer_request(GSupplicantPeer *peer) |
| { |
| if (!callbacks_pointer) |
| return; |
| |
| if (!callbacks_pointer->peer_request) |
| return; |
| |
| peer->connection_requested = true; |
| |
| callbacks_pointer->peer_request(peer); |
| } |
| |
| static void remove_group(gpointer data) |
| { |
| GSupplicantGroup *group = data; |
| |
| if (group->members) |
| g_slist_free_full(group->members, g_free); |
| |
| g_free(group->path); |
| g_free(group); |
| } |
| |
| static void remove_interface(gpointer data) |
| { |
| GSupplicantInterface *interface = data; |
| |
| g_hash_table_destroy(interface->bss_mapping); |
| g_hash_table_destroy(interface->net_mapping); |
| g_hash_table_destroy(interface->network_table); |
| g_hash_table_destroy(interface->peer_table); |
| g_hash_table_destroy(interface->group_table); |
| |
| if (interface->scan_callback) { |
| SUPPLICANT_DBG("call interface %p callback %p scanning %d", |
| interface, interface->scan_callback, |
| interface->scanning); |
| |
| interface->scan_callback(-EIO, interface, interface->scan_data); |
| interface->scan_callback = NULL; |
| interface->scan_data = NULL; |
| |
| if (interface->scanning) { |
| interface->scanning = FALSE; |
| callback_scan_finished(interface); |
| } |
| } |
| |
| callback_interface_removed(interface); |
| |
| g_free(interface->wps_cred.key); |
| g_free(interface->path); |
| g_free(interface->network_path); |
| g_free(interface->ifname); |
| g_free(interface->driver); |
| g_free(interface->bridge); |
| remove_network_information(interface); |
| g_free(interface); |
| } |
| |
| static void remove_network(gpointer data) |
| { |
| GSupplicantNetwork *network = data; |
| |
| g_hash_table_destroy(network->bss_table); |
| |
| callback_network_removed(network); |
| |
| g_hash_table_destroy(network->config_table); |
| |
| g_free(network->path); |
| g_free(network->group); |
| g_free(network->name); |
| g_free(network); |
| } |
| |
| static void remove_bss(gpointer data) |
| { |
| struct g_supplicant_bss *bss = data; |
| |
| g_free(bss->path); |
| g_free(bss); |
| } |
| |
| static void remove_peer(gpointer data) |
| { |
| GSupplicantPeer *peer = data; |
| |
| callback_peer_lost(peer); |
| |
| if (peer->groups) |
| g_slist_free_full(peer->groups, g_free); |
| |
| if (peer_mapping) |
| g_hash_table_remove(peer_mapping, peer->path); |
| |
| if (pending_peer_connection) |
| g_hash_table_remove(pending_peer_connection, peer->path); |
| |
| g_free(peer->path); |
| g_free(peer->name); |
| g_free(peer->identifier); |
| |
| g_free(peer); |
| } |
| |
| static void debug_strvalmap(const char *label, struct strvalmap *map, |
| unsigned int val) |
| { |
| int i; |
| |
| for (i = 0; map[i].str; i++) { |
| if (val & map[i].val) |
| SUPPLICANT_DBG("%s: %s", label, map[i].str); |
| } |
| } |
| |
| static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; keymgmt_map[i].str; i++) |
| if (strcmp(str, keymgmt_map[i].str) == 0) { |
| interface->keymgmt_capa |= keymgmt_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability_authalg(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; authalg_capa_map[i].str; i++) |
| if (strcmp(str, authalg_capa_map[i].str) == 0) { |
| interface->authalg_capa |= authalg_capa_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability_proto(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; proto_capa_map[i].str; i++) |
| if (strcmp(str, proto_capa_map[i].str) == 0) { |
| interface->proto_capa |= proto_capa_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability_pairwise(DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; pairwise_map[i].str; i++) |
| if (strcmp(str, pairwise_map[i].str) == 0) { |
| interface->pairwise_capa |= pairwise_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability_group(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; group_map[i].str; i++) |
| if (strcmp(str, group_map[i].str) == 0) { |
| interface->group_capa |= group_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability_scan(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; scan_capa_map[i].str; i++) |
| if (strcmp(str, scan_capa_map[i].str) == 0) { |
| interface->scan_capa |= scan_capa_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability_mode(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; mode_capa_map[i].str; i++) |
| if (strcmp(str, mode_capa_map[i].str) == 0) { |
| interface->mode_capa |= mode_capa_map[i].val; |
| break; |
| } |
| } |
| |
| static void interface_capability(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| |
| if (!key) |
| return; |
| |
| if (g_strcmp0(key, "KeyMgmt") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_keymgmt, interface); |
| else if (g_strcmp0(key, "AuthAlg") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_authalg, interface); |
| else if (g_strcmp0(key, "Protocol") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_proto, interface); |
| else if (g_strcmp0(key, "Pairwise") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_pairwise, interface); |
| else if (g_strcmp0(key, "Group") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_group, interface); |
| else if (g_strcmp0(key, "Scan") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_scan, interface); |
| else if (g_strcmp0(key, "Modes") == 0) |
| supplicant_dbus_array_foreach(iter, |
| interface_capability_mode, interface); |
| else if (g_strcmp0(key, "MaxScanSSID") == 0) { |
| dbus_int32_t max_scan_ssid; |
| |
| dbus_message_iter_get_basic(iter, &max_scan_ssid); |
| if (max_scan_ssid < 2) |
| max_scan_ssid = 0; |
| interface->max_scan_ssids = max_scan_ssid; |
| |
| } else |
| SUPPLICANT_DBG("key %s type %c", |
| key, dbus_message_iter_get_arg_type(iter)); |
| } |
| |
| struct set_apscan_data |
| { |
| unsigned int ap_scan; |
| GSupplicantInterface *interface; |
| }; |
| |
| static void set_apscan(DBusMessageIter *iter, void *user_data) |
| { |
| struct set_apscan_data *data = user_data; |
| unsigned int ap_scan = data->ap_scan; |
| |
| dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan); |
| } |
| |
| static void set_apscan_complete(const char *error, |
| DBusMessageIter *iter, void *user_data) |
| { |
| struct set_apscan_data *data = user_data; |
| GSupplicantInterface *interface = data->interface; |
| |
| if (error) { |
| interface->ap_create_in_progress = false; |
| SUPPLICANT_DBG("Set AP scan error %s", error); |
| goto error; |
| } |
| |
| interface->ap_create_in_progress = true; |
| error: |
| dbus_free(data); |
| } |
| |
| int g_supplicant_interface_set_apscan(GSupplicantInterface *interface, |
| unsigned int ap_scan) |
| { |
| struct set_apscan_data *data; |
| int ret; |
| |
| data = dbus_malloc0(sizeof(*data)); |
| |
| if (!data) |
| return -ENOMEM; |
| |
| data->ap_scan = ap_scan; |
| data->interface = interface; |
| |
| ret = supplicant_dbus_property_set(interface->path, |
| SUPPLICANT_INTERFACE ".Interface", |
| "ApScan", DBUS_TYPE_UINT32_AS_STRING, |
| set_apscan, set_apscan_complete, data, NULL); |
| if (ret < 0) |
| dbus_free(data); |
| |
| return ret; |
| } |
| |
| void g_supplicant_interface_set_data(GSupplicantInterface *interface, |
| void *data) |
| { |
| if (!interface) |
| return; |
| |
| interface->data = data; |
| |
| if (!data) |
| interface->scan_callback = NULL; |
| } |
| |
| void *g_supplicant_interface_get_data(GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return NULL; |
| |
| return interface->data; |
| } |
| |
| const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return NULL; |
| |
| return interface->ifname; |
| } |
| |
| const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return NULL; |
| |
| return interface->driver; |
| } |
| |
| GSupplicantState g_supplicant_interface_get_state( |
| GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return G_SUPPLICANT_STATE_UNKNOWN; |
| |
| return interface->state; |
| } |
| |
| const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return NULL; |
| |
| return (const char *)interface->wps_cred.key; |
| } |
| |
| const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface, |
| unsigned int *ssid_len) |
| { |
| if (!ssid_len) |
| return NULL; |
| |
| if (!interface || interface->wps_cred.ssid_len == 0) { |
| *ssid_len = 0; |
| return NULL; |
| } |
| |
| *ssid_len = interface->wps_cred.ssid_len; |
| return interface->wps_cred.ssid; |
| } |
| |
| GSupplicantWpsState g_supplicant_interface_get_wps_state( |
| GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return G_SUPPLICANT_WPS_STATE_UNKNOWN; |
| |
| return interface->wps_state; |
| } |
| |
| unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return 0; |
| |
| return interface->mode_capa; |
| } |
| |
| unsigned int g_supplicant_interface_get_max_scan_ssids( |
| GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return 0; |
| |
| return interface->max_scan_ssids; |
| } |
| |
| static void set_network_enabled(DBusMessageIter *iter, void *user_data) |
| { |
| dbus_bool_t enable = *(dbus_bool_t *)user_data; |
| |
| dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable); |
| } |
| |
| int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface, |
| dbus_bool_t enable) |
| { |
| if (!interface) |
| return -1; |
| |
| if (!interface->network_path) |
| return -1; |
| |
| SUPPLICANT_DBG(" "); |
| return supplicant_dbus_property_set(interface->network_path, |
| SUPPLICANT_INTERFACE ".Network", |
| "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING, |
| set_network_enabled, NULL, &enable, NULL); |
| } |
| |
| dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface) |
| { |
| if (!interface) |
| return FALSE; |
| |
| return interface->ready; |
| } |
| |
| GSupplicantInterface *g_supplicant_network_get_interface( |
| GSupplicantNetwork *network) |
| { |
| if (!network) |
| return NULL; |
| |
| return network->interface; |
| } |
| |
| const char *g_supplicant_network_get_name(GSupplicantNetwork *network) |
| { |
| if (!network || !network->name) |
| return ""; |
| |
| return network->name; |
| } |
| |
| const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network) |
| { |
| if (!network || !network->group) |
| return ""; |
| |
| return network->group; |
| } |
| |
| const char *g_supplicant_network_get_path(GSupplicantNetwork *network) |
| { |
| if (!network || !network->path) |
| return NULL; |
| |
| return network->path; |
| } |
| |
| const char *g_supplicant_network_get_mode(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return G_SUPPLICANT_MODE_UNKNOWN; |
| |
| return mode2string(network->mode); |
| } |
| |
| const char *g_supplicant_network_get_security(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return G_SUPPLICANT_SECURITY_UNKNOWN; |
| |
| return security2string(network->security); |
| } |
| |
| const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network, |
| unsigned int *ssid_len) |
| { |
| if (!network) { |
| *ssid_len = 0; |
| return NULL; |
| } |
| |
| *ssid_len = network->ssid_len; |
| return network->ssid; |
| } |
| |
| dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return 0; |
| |
| return network->signal; |
| } |
| |
| dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return 0; |
| |
| return network->frequency; |
| } |
| |
| dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return FALSE; |
| |
| return network->wps; |
| } |
| |
| dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return FALSE; |
| |
| if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED) |
| return TRUE; |
| |
| return FALSE; |
| } |
| |
| dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return FALSE; |
| |
| if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC) |
| return TRUE; |
| |
| return FALSE; |
| } |
| |
| dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network) |
| { |
| if (!network) |
| return FALSE; |
| |
| if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR) |
| return TRUE; |
| |
| return FALSE; |
| } |
| |
| GSupplicantInterface *g_supplicant_peer_get_interface(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return peer->interface; |
| } |
| |
| const char *g_supplicant_peer_get_path(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return peer->path; |
| } |
| |
| const char *g_supplicant_peer_get_identifier(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return peer->identifier; |
| } |
| |
| const void *g_supplicant_peer_get_device_address(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return peer->device_address; |
| } |
| |
| const void *g_supplicant_peer_get_iface_address(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return peer->iface_address; |
| } |
| |
| const char *g_supplicant_peer_get_name(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return peer->name; |
| } |
| |
| const unsigned char *g_supplicant_peer_get_widi_ies(GSupplicantPeer *peer, |
| int *length) |
| { |
| if (!peer || !length) |
| return NULL; |
| |
| *length = peer->widi_ies_length; |
| return peer->widi_ies; |
| } |
| |
| bool g_supplicant_peer_is_wps_pbc(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return false; |
| |
| if (peer->wps_capabilities & G_SUPPLICANT_WPS_PBC) |
| return true; |
| |
| return false; |
| } |
| |
| bool g_supplicant_peer_is_wps_pin(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return false; |
| |
| if (peer->wps_capabilities & G_SUPPLICANT_WPS_PIN) |
| return true; |
| |
| return false; |
| } |
| |
| bool g_supplicant_peer_is_in_a_group(GSupplicantPeer *peer) |
| { |
| if (!peer || !peer->groups) |
| return false; |
| |
| return true; |
| } |
| |
| GSupplicantInterface *g_supplicant_peer_get_group_interface(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return NULL; |
| |
| return (GSupplicantInterface *) peer->current_group_iface; |
| } |
| |
| bool g_supplicant_peer_is_client(GSupplicantPeer *peer) |
| { |
| GSupplicantGroup *group; |
| GSList *list; |
| |
| if (!peer) |
| return false; |
| |
| for (list = peer->groups; list; list = list->next) { |
| const char *path = list->data; |
| |
| group = g_hash_table_lookup(group_mapping, path); |
| if (!group) |
| continue; |
| |
| if (group->role != G_SUPPLICANT_GROUP_ROLE_CLIENT || |
| group->orig_interface != peer->interface) |
| continue; |
| |
| if (group->interface == peer->current_group_iface) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| bool g_supplicant_peer_has_requested_connection(GSupplicantPeer *peer) |
| { |
| if (!peer) |
| return false; |
| |
| return peer->connection_requested; |
| } |
| |
| static void merge_network(GSupplicantNetwork *network) |
| { |
| GString *str; |
| const char *ssid, *mode, *key_mgmt; |
| unsigned int i, ssid_len; |
| char *group; |
| |
| ssid = g_hash_table_lookup(network->config_table, "ssid"); |
| mode = g_hash_table_lookup(network->config_table, "mode"); |
| key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt"); |
| |
| SUPPLICANT_DBG("ssid %s mode %s", ssid, mode); |
| |
| if (ssid) |
| ssid_len = strlen(ssid); |
| else |
| ssid_len = 0; |
| |
| str = g_string_sized_new((ssid_len * 2) + 24); |
| if (!str) |
| return; |
| |
| for (i = 0; i < ssid_len; i++) |
| g_string_append_printf(str, "%02x", ssid[i]); |
| |
| if (g_strcmp0(mode, "0") == 0) |
| g_string_append_printf(str, "_managed"); |
| else if (g_strcmp0(mode, "1") == 0) |
| g_string_append_printf(str, "_adhoc"); |
| |
| if (g_strcmp0(key_mgmt, "WPA-PSK") == 0) |
| g_string_append_printf(str, "_psk"); |
| |
| group = g_string_free(str, FALSE); |
| |
| SUPPLICANT_DBG("%s", group); |
| |
| g_free(group); |
| |
| g_hash_table_destroy(network->config_table); |
| |
| g_free(network->path); |
| g_free(network); |
| } |
| |
| static void network_property(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantNetwork *network = user_data; |
| |
| if (!network->interface) |
| return; |
| |
| if (!key) { |
| merge_network(network); |
| return; |
| } |
| |
| if (g_strcmp0(key, "Enabled") == 0) { |
| dbus_bool_t enabled = FALSE; |
| |
| dbus_message_iter_get_basic(iter, &enabled); |
| } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (str) { |
| g_hash_table_replace(network->config_table, |
| g_strdup(key), g_strdup(str)); |
| } |
| } else |
| SUPPLICANT_DBG("key %s type %c", |
| key, dbus_message_iter_get_arg_type(iter)); |
| } |
| |
| static void interface_network_added(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| GSupplicantNetwork *network; |
| const char *path = NULL; |
| |
| SUPPLICANT_DBG(""); |
| |
| dbus_message_iter_get_basic(iter, &path); |
| if (!path) |
| return; |
| |
| if (g_strcmp0(path, "/") == 0) |
| return; |
| |
| network = g_hash_table_lookup(interface->net_mapping, path); |
| if (network) |
| return; |
| |
| network = g_try_new0(GSupplicantNetwork, 1); |
| if (!network) |
| return; |
| |
| network->interface = interface; |
| network->path = g_strdup(path); |
| |
| network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal, |
| g_free, g_free); |
| |
| dbus_message_iter_next(iter); |
| if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) { |
| supplicant_dbus_property_foreach(iter, network_property, |
| network); |
| network_property(NULL, NULL, network); |
| return; |
| } |
| |
| supplicant_dbus_property_get_all(path, |
| SUPPLICANT_INTERFACE ".Network", |
| network_property, network, NULL); |
| } |
| |
| static void interface_network_removed(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| GSupplicantNetwork *network; |
| const char *path = NULL; |
| |
| dbus_message_iter_get_basic(iter, &path); |
| if (!path) |
| return; |
| |
| network = g_hash_table_lookup(interface->net_mapping, path); |
| if (!network) |
| return; |
| |
| g_hash_table_remove(interface->net_mapping, path); |
| } |
| |
| static char *create_name(unsigned char *ssid, int ssid_len) |
| { |
| GString *string; |
| const gchar *remainder, *invalid; |
| int valid_bytes, remaining_bytes; |
| |
| if (ssid_len < 1 || ssid[0] == '\0') |
| return g_strdup(""); |
| |
| string = NULL; |
| remainder = (const gchar *)ssid; |
| remaining_bytes = ssid_len; |
| |
| while (remaining_bytes != 0) { |
| if (g_utf8_validate(remainder, remaining_bytes, |
| &invalid)) { |
| break; |
| } |
| |
| valid_bytes = invalid - remainder; |
| |
| if (!string) |
| string = g_string_sized_new(remaining_bytes); |
| |
| g_string_append_len(string, remainder, valid_bytes); |
| |
| /* append U+FFFD REPLACEMENT CHARACTER */ |
| g_string_append(string, "\357\277\275"); |
| |
| remaining_bytes -= valid_bytes + 1; |
| remainder = invalid + 1; |
| } |
| |
| if (!string) |
| return g_strndup((const gchar *)ssid, ssid_len + 1); |
| |
| g_string_append(string, remainder); |
| |
| return g_string_free(string, FALSE); |
| } |
| |
| static char *create_group(struct g_supplicant_bss *bss) |
| { |
| GString *str; |
| unsigned int i; |
| const char *mode, *security; |
| |
| str = g_string_sized_new((bss->ssid_len * 2) + 24); |
| if (!str) |
| return NULL; |
| |
| if (bss->ssid_len > 0 && bss->ssid[0] != '\0') { |
| for (i = 0; i < bss->ssid_len; i++) |
| g_string_append_printf(str, "%02x", bss->ssid[i]); |
| } else |
| g_string_append_printf(str, "hidden"); |
| |
| mode = mode2string(bss->mode); |
| if (mode) |
| g_string_append_printf(str, "_%s", mode); |
| |
| security = security2string(bss->security); |
| if (security) |
| g_string_append_printf(str, "_%s", security); |
| |
| return g_string_free(str, FALSE); |
| } |
| |
| static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss) |
| { |
| GSupplicantInterface *interface = bss->interface; |
| GSupplicantNetwork *network; |
| char *group; |
| |
| group = create_group(bss); |
| SUPPLICANT_DBG("New group created: %s", group); |
| |
| if (!group) |
| return; |
| |
| network = g_hash_table_lookup(interface->network_table, group); |
| if (network) { |
| g_free(group); |
| SUPPLICANT_DBG("Network %s already exist", network->name); |
| |
| goto done; |
| } |
| |
| network = g_try_new0(GSupplicantNetwork, 1); |
| if (!network) { |
| g_free(group); |
| return; |
| } |
| |
| network->interface = interface; |
| if (!network->path) |
| network->path = g_strdup(bss->path); |
| network->group = group; |
| network->name = create_name(bss->ssid, bss->ssid_len); |
| network->mode = bss->mode; |
| network->security = bss->security; |
| network->ssid_len = bss->ssid_len; |
| memcpy(network->ssid, bss->ssid, bss->ssid_len); |
| network->signal = bss->signal; |
| network->frequency = bss->frequency; |
| network->best_bss = bss; |
| |
| SUPPLICANT_DBG("New network %s created", network->name); |
| |
| network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal, |
| NULL, remove_bss); |
| |
| network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal, |
| g_free, g_free); |
| |
| g_hash_table_replace(interface->network_table, |
| network->group, network); |
| |
| callback_network_added(network); |
| |
| done: |
| /* We update network's WPS properties if only bss provides WPS. */ |
| if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) { |
| network->wps = TRUE; |
| network->wps_capabilities |= bss->wps_capabilities; |
| } |
| |
| if (bss->signal > network->signal) { |
| network->signal = bss->signal; |
| network->best_bss = bss; |
| callback_network_changed(network, "Signal"); |
| } |
| |
| g_hash_table_replace(interface->bss_mapping, bss->path, network); |
| g_hash_table_replace(network->bss_table, bss->path, bss); |
| |
| g_hash_table_replace(bss_mapping, bss->path, interface); |
| } |
| |
| static void bss_rates(DBusMessageIter *iter, void *user_data) |
| { |
| struct g_supplicant_bss *bss = user_data; |
| dbus_uint32_t rate = 0; |
| |
| dbus_message_iter_get_basic(iter, &rate); |
| if (rate == 0) |
| return; |
| |
| if (rate > bss->maxrate) |
| bss->maxrate = rate; |
| } |
| |
| static void bss_keymgmt(DBusMessageIter *iter, void *user_data) |
| { |
| unsigned int *keymgmt = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; keymgmt_map[i].str; i++) |
| if (strcmp(str, keymgmt_map[i].str) == 0) { |
| SUPPLICANT_DBG("Keymgmt: %s", str); |
| *keymgmt |= keymgmt_map[i].val; |
| break; |
| } |
| } |
| |
| static void bss_group(DBusMessageIter *iter, void *user_data) |
| { |
| unsigned int *group = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; group_map[i].str; i++) |
| if (strcmp(str, group_map[i].str) == 0) { |
| SUPPLICANT_DBG("Group: %s", str); |
| *group |= group_map[i].val; |
| break; |
| } |
| } |
| |
| static void bss_pairwise(DBusMessageIter *iter, void *user_data) |
| { |
| unsigned int *pairwise = user_data; |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; pairwise_map[i].str; i++) |
| if (strcmp(str, pairwise_map[i].str) == 0) { |
| SUPPLICANT_DBG("Pairwise: %s", str); |
| *pairwise |= pairwise_map[i].val; |
| break; |
| } |
| } |
| |
| static void bss_wpa(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| struct g_supplicant_bss *bss = user_data; |
| unsigned int value = 0; |
| |
| SUPPLICANT_DBG("Key: %s", key); |
| |
| if (g_strcmp0(key, "KeyMgmt") == 0) { |
| supplicant_dbus_array_foreach(iter, bss_keymgmt, &value); |
| |
| if (bss->rsn_selected) |
| bss->rsn_keymgmt = value; |
| else |
| bss->wpa_keymgmt = value; |
| } else if (g_strcmp0(key, "Group") == 0) { |
| supplicant_dbus_array_foreach(iter, bss_group, &value); |
| |
| if (bss->rsn_selected) |
| bss->rsn_group = value; |
| else |
| bss->wpa_group = value; |
| } else if (g_strcmp0(key, "Pairwise") == 0) { |
| supplicant_dbus_array_foreach(iter, bss_pairwise, &value); |
| |
| if (bss->rsn_selected) |
| bss->rsn_pairwise = value; |
| else |
| bss->wpa_pairwise = value; |
| } |
| } |
| |
| static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size, |
| unsigned int type) |
| { |
| unsigned int len = 0; |
| |
| while (len + 4 < ie_size) { |
| unsigned int hi = ie[len]; |
| unsigned int lo = ie[len + 1]; |
| unsigned int tmp_type = (hi << 8) + lo; |
| unsigned int v_len = 0; |
| |
| /* hi and lo are used to recreate an unsigned int |
| * based on 2 8bits length unsigned int. */ |
| |
| hi = ie[len + 2]; |
| lo = ie[len + 3]; |
| v_len = (hi << 8) + lo; |
| |
| if (tmp_type == type) { |
| unsigned int ret_value = 0; |
| unsigned char *value = (unsigned char *)&ret_value; |
| |
| SUPPLICANT_DBG("IE: match type 0x%x", type); |
| |
| /* Verifying length relevance */ |
| if (v_len > sizeof(unsigned int) || |
| len + 4 + v_len > ie_size) |
| break; |
| |
| memcpy(value, ie + len + 4, v_len); |
| |
| SUPPLICANT_DBG("returning 0x%x", ret_value); |
| return ret_value; |
| } |
| |
| len += v_len + 4; |
| } |
| |
| SUPPLICANT_DBG("returning 0"); |
| return 0; |
| } |
| |
| static void bss_process_ies(DBusMessageIter *iter, void *user_data) |
| { |
| struct g_supplicant_bss *bss = user_data; |
| const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 }; |
| unsigned char *ie, *ie_end; |
| DBusMessageIter array; |
| unsigned int value; |
| int ie_len; |
| |
| #define WMM_WPA1_WPS_INFO 221 |
| #define WPS_INFO_MIN_LEN 6 |
| #define WPS_VERSION_TLV 0x104A |
| #define WPS_STATE_TLV 0x1044 |
| #define WPS_METHODS_TLV 0x1012 |
| #define WPS_REGISTRAR_TLV 0x1041 |
| #define WPS_VERSION 0x10 |
| #define WPS_PBC 0x04 |
| #define WPS_PIN 0x00 |
| #define WPS_CONFIGURED 0x02 |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &ie, &ie_len); |
| |
| if (!ie || ie_len < 2) |
| return; |
| |
| bss->wps_capabilities = 0; |
| bss->keymgmt = 0; |
| |
| for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end; |
| ie += ie[1] + 2) { |
| |
| if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN || |
| memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0) |
| continue; |
| |
| SUPPLICANT_DBG("IE: match WPS_OUI"); |
| |
| value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV); |
| if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION && |
| value != 0) { |
| bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS; |
| |
| if (value == WPS_CONFIGURED) |
| bss->wps_capabilities |= |
| G_SUPPLICANT_WPS_CONFIGURED; |
| } |
| |
| value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV); |
| if (value != 0) { |
| if (GUINT16_FROM_BE(value) == WPS_PBC) |
| bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC; |
| if (GUINT16_FROM_BE(value) == WPS_PIN) |
| bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN; |
| } else |
| bss->wps_capabilities |= |
| G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN; |
| |
| /* If the AP sends this it means it's advertizing |
| * as a registrar and the WPS process is launched |
| * on its side */ |
| if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0) |
| bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR; |
| |
| SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities); |
| } |
| } |
| |
| static void bss_compute_security(struct g_supplicant_bss *bss) |
| { |
| /* |
| * Combining RSN and WPA keymgmt |
| * We combine it since parsing IEs might have set something for WPS. */ |
| bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt; |
| |
| bss->ieee8021x = FALSE; |
| bss->psk = FALSE; |
| |
| if (bss->keymgmt & |
| (G_SUPPLICANT_KEYMGMT_WPA_EAP | |
| G_SUPPLICANT_KEYMGMT_WPA_FT_EAP | |
| G_SUPPLICANT_KEYMGMT_WPA_EAP_256)) |
| bss->ieee8021x = TRUE; |
| |
| if (bss->keymgmt & |
| (G_SUPPLICANT_KEYMGMT_WPA_PSK | |
| G_SUPPLICANT_KEYMGMT_WPA_FT_PSK | |
| G_SUPPLICANT_KEYMGMT_WPA_PSK_256)) |
| bss->psk = TRUE; |
| |
| if (bss->ieee8021x) |
| bss->security = G_SUPPLICANT_SECURITY_IEEE8021X; |
| else if (bss->psk) |
| bss->security = G_SUPPLICANT_SECURITY_PSK; |
| else if (bss->privacy) |
| bss->security = G_SUPPLICANT_SECURITY_WEP; |
| else |
| bss->security = G_SUPPLICANT_SECURITY_NONE; |
| } |
| |
| |
| static void bss_property(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| struct g_supplicant_bss *bss = user_data; |
| |
| if (!bss->interface) |
| return; |
| |
| SUPPLICANT_DBG("key %s", key); |
| |
| if (!key) |
| return; |
| |
| if (g_strcmp0(key, "BSSID") == 0) { |
| DBusMessageIter array; |
| unsigned char *addr; |
| int addr_len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &addr, &addr_len); |
| |
| if (addr_len == 6) |
| memcpy(bss->bssid, addr, addr_len); |
| } else if (g_strcmp0(key, "SSID") == 0) { |
| DBusMessageIter array; |
| unsigned char *ssid; |
| int ssid_len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len); |
| |
| if (ssid_len > 0 && ssid_len < 33) { |
| memcpy(bss->ssid, ssid, ssid_len); |
| bss->ssid_len = ssid_len; |
| } else { |
| memset(bss->ssid, 0, sizeof(bss->ssid)); |
| bss->ssid_len = 0; |
| } |
| } else if (g_strcmp0(key, "Capabilities") == 0) { |
| dbus_uint16_t capabilities = 0x0000; |
| |
| dbus_message_iter_get_basic(iter, &capabilities); |
| |
| if (capabilities & IEEE80211_CAP_ESS) |
| bss->mode = G_SUPPLICANT_MODE_INFRA; |
| else if (capabilities & IEEE80211_CAP_IBSS) |
| bss->mode = G_SUPPLICANT_MODE_IBSS; |
| |
| if (capabilities & IEEE80211_CAP_PRIVACY) |
| bss->privacy = TRUE; |
| } else if (g_strcmp0(key, "Mode") == 0) { |
| const char *mode = NULL; |
| |
| dbus_message_iter_get_basic(iter, &mode); |
| bss->mode = string2mode(mode); |
| } else if (g_strcmp0(key, "Frequency") == 0) { |
| dbus_uint16_t frequency = 0; |
| |
| dbus_message_iter_get_basic(iter, &frequency); |
| bss->frequency = frequency; |
| } else if (g_strcmp0(key, "Signal") == 0) { |
| dbus_int16_t signal = 0; |
| |
| dbus_message_iter_get_basic(iter, &signal); |
| |
| bss->signal = signal; |
| if (!bss->signal) |
| bss->signal = BSS_UNKNOWN_STRENGTH; |
| |
| } else if (g_strcmp0(key, "Level") == 0) { |
| dbus_int32_t level = 0; |
| |
| dbus_message_iter_get_basic(iter, &level); |
| } else if (g_strcmp0(key, "Rates") == 0) { |
| supplicant_dbus_array_foreach(iter, bss_rates, bss); |
| } else if (g_strcmp0(key, "MaxRate") == 0) { |
| dbus_uint32_t maxrate = 0; |
| |
| dbus_message_iter_get_basic(iter, &maxrate); |
| if (maxrate != 0) |
| bss->maxrate = maxrate; |
| } else if (g_strcmp0(key, "Privacy") == 0) { |
| dbus_bool_t privacy = FALSE; |
| |
| dbus_message_iter_get_basic(iter, &privacy); |
| bss->privacy = privacy; |
| } else if (g_strcmp0(key, "RSN") == 0) { |
| bss->rsn_selected = TRUE; |
| |
| supplicant_dbus_property_foreach(iter, bss_wpa, bss); |
| } else if (g_strcmp0(key, "WPA") == 0) { |
| bss->rsn_selected = FALSE; |
| |
| supplicant_dbus_property_foreach(iter, bss_wpa, bss); |
| } else if (g_strcmp0(key, "IEs") == 0) |
| bss_process_ies(iter, bss); |
| else |
| SUPPLICANT_DBG("key %s type %c", |
| key, dbus_message_iter_get_arg_type(iter)); |
| } |
| |
| static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| GSupplicantNetwork *network; |
| struct g_supplicant_bss *bss; |
| const char *path = NULL; |
| |
| SUPPLICANT_DBG(""); |
| |
| dbus_message_iter_get_basic(iter, &path); |
| if (!path) |
| return NULL; |
| |
| if (g_strcmp0(path, "/") == 0) |
| return NULL; |
| |
| SUPPLICANT_DBG("%s", path); |
| |
| network = g_hash_table_lookup(interface->bss_mapping, path); |
| if (network) { |
| bss = g_hash_table_lookup(network->bss_table, path); |
| if (bss) |
| return NULL; |
| } |
| |
| bss = g_try_new0(struct g_supplicant_bss, 1); |
| if (!bss) |
| return NULL; |
| |
| bss->interface = interface; |
| bss->path = g_strdup(path); |
| bss->signal = BSS_UNKNOWN_STRENGTH; |
| |
| return bss; |
| } |
| |
| static void interface_bss_added_with_keys(DBusMessageIter *iter, |
| void *user_data) |
| { |
| struct g_supplicant_bss *bss; |
| |
| SUPPLICANT_DBG(""); |
| |
| bss = interface_bss_added(iter, user_data); |
| if (!bss) |
| return; |
| |
| dbus_message_iter_next(iter); |
| |
| if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID) |
| return; |
| |
| supplicant_dbus_property_foreach(iter, bss_property, bss); |
| |
| bss_compute_security(bss); |
| add_or_replace_bss_to_network(bss); |
| } |
| |
| static void interface_bss_added_without_keys(DBusMessageIter *iter, |
| void *user_data) |
| { |
| struct g_supplicant_bss *bss; |
| |
| SUPPLICANT_DBG(""); |
| |
| bss = interface_bss_added(iter, user_data); |
| if (!bss) |
| return; |
| |
| supplicant_dbus_property_get_all(bss->path, |
| SUPPLICANT_INTERFACE ".BSS", |
| bss_property, bss, NULL); |
| |
| bss_compute_security(bss); |
| add_or_replace_bss_to_network(bss); |
| } |
| |
| static void update_signal(gpointer key, gpointer value, |
| gpointer user_data) |
| { |
| struct g_supplicant_bss *bss = value; |
| GSupplicantNetwork *network = user_data; |
| |
| if (bss->signal > network->signal) { |
| network->signal = bss->signal; |
| network->best_bss = bss; |
| } |
| } |
| |
| static void update_network_signal(GSupplicantNetwork *network) |
| { |
| if (g_hash_table_size(network->bss_table) <= 1 && network->best_bss) |
| return; |
| |
| g_hash_table_foreach(network->bss_table, |
| update_signal, network); |
| |
| SUPPLICANT_DBG("New network signal %d", network->signal); |
| } |
| |
| static void interface_bss_removed(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| GSupplicantNetwork *network; |
| struct g_supplicant_bss *bss = NULL; |
| const char *path = NULL; |
| |
| dbus_message_iter_get_basic(iter, &path); |
| if (!path) |
| return; |
| |
| network = g_hash_table_lookup(interface->bss_mapping, path); |
| if (!network) |
| return; |
| |
| bss = g_hash_table_lookup(network->bss_table, path); |
| if (network->best_bss == bss) { |
| network->best_bss = NULL; |
| network->signal = BSS_UNKNOWN_STRENGTH; |
| } |
| |
| g_hash_table_remove(bss_mapping, path); |
| |
| g_hash_table_remove(interface->bss_mapping, path); |
| g_hash_table_remove(network->bss_table, path); |
| |
| update_network_signal(network); |
| |
| if (g_hash_table_size(network->bss_table) == 0) |
| g_hash_table_remove(interface->network_table, network->group); |
| } |
| |
| static void set_config_methods(DBusMessageIter *iter, void *user_data) |
| { |
| const char *config_methods = "push_button"; |
| |
| dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, |
| &config_methods); |
| } |
| |
| static void interface_property(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| |
| if (!interface) |
| return; |
| |
| SUPPLICANT_DBG("%s", key); |
| |
| if (!key) { |
| debug_strvalmap("KeyMgmt capability", keymgmt_map, |
| interface->keymgmt_capa); |
| debug_strvalmap("AuthAlg capability", authalg_capa_map, |
| interface->authalg_capa); |
| debug_strvalmap("Protocol capability", proto_capa_map, |
| interface->proto_capa); |
| debug_strvalmap("Pairwise capability", pairwise_map, |
| interface->pairwise_capa); |
| debug_strvalmap("Group capability", group_map, |
| interface->group_capa); |
| debug_strvalmap("Scan capability", scan_capa_map, |
| interface->scan_capa); |
| debug_strvalmap("Mode capability", mode_capa_map, |
| interface->mode_capa); |
| |
| |
| supplicant_dbus_property_set(interface->path, |
| SUPPLICANT_INTERFACE ".Interface.WPS", |
| "ConfigMethods", DBUS_TYPE_STRING_AS_STRING, |
| set_config_methods, NULL, NULL, NULL); |
| |
| if (interface->ready) |
| callback_interface_added(interface); |
| |
| return; |
| } |
| |
| if (g_strcmp0(key, "Capabilities") == 0) { |
| supplicant_dbus_property_foreach(iter, interface_capability, |
| interface); |
| if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_P2P) |
| interface->p2p_support = true; |
| } else if (g_strcmp0(key, "State") == 0) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (str) |
| if (string2state(str) != interface->state) { |
| interface->state = string2state(str); |
| callback_interface_state(interface); |
| } |
| |
| if (interface->ap_create_in_progress) { |
| if (interface->state == G_SUPPLICANT_STATE_DISCONNECTED) |
| callback_ap_create_fail(interface); |
| |
| interface->ap_create_in_progress = false; |
| } |
| |
| if (interface->state == G_SUPPLICANT_STATE_DISABLED) |
| interface->ready = FALSE; |
| else |
| interface->ready = TRUE; |
| |
| SUPPLICANT_DBG("state %s (%d)", str, interface->state); |
| } else if (g_strcmp0(key, "Scanning") == 0) { |
| dbus_bool_t scanning = FALSE; |
| |
| dbus_message_iter_get_basic(iter, &scanning); |
| interface->scanning = scanning; |
| |
| if (interface->ready) { |
| if (interface->scanning) |
| callback_scan_started(interface); |
| else |
| callback_scan_finished(interface); |
| } |
| } else if (g_strcmp0(key, "ApScan") == 0) { |
| int apscan = 1; |
| |
| dbus_message_iter_get_basic(iter, &apscan); |
| interface->apscan = apscan; |
| } else if (g_strcmp0(key, "Ifname") == 0) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (str) { |
| g_free(interface->ifname); |
| interface->ifname = g_strdup(str); |
| } |
| } else if (g_strcmp0(key, "Driver") == 0) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (str) { |
| g_free(interface->driver); |
| interface->driver = g_strdup(str); |
| } |
| } else if (g_strcmp0(key, "BridgeIfname") == 0) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (str) { |
| g_free(interface->bridge); |
| interface->bridge = g_strdup(str); |
| } |
| } else if (g_strcmp0(key, "CurrentBSS") == 0) { |
| interface_bss_added_without_keys(iter, interface); |
| } else if (g_strcmp0(key, "CurrentNetwork") == 0) { |
| interface_network_added(iter, interface); |
| } else if (g_strcmp0(key, "BSSs") == 0) { |
| supplicant_dbus_array_foreach(iter, |
| interface_bss_added_without_keys, |
| interface); |
| } else if (g_strcmp0(key, "Blobs") == 0) { |
| /* Nothing */ |
| } else if (g_strcmp0(key, "Networks") == 0) { |
| supplicant_dbus_array_foreach(iter, interface_network_added, |
| interface); |
| } else if (g_strcmp0(key, "DisconnectReason") == 0) { |
| int reason; |
| if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) { |
| dbus_message_iter_get_basic(iter, &reason); |
| if (callbacks_pointer && callbacks_pointer->update_disconnect_reasoncode && reason != 0) { |
| callbacks_pointer->update_disconnect_reasoncode(interface, reason); |
| } |
| } |
| } else if (g_strcmp0(key, "AssocStatusCode") == 0) { |
| int status_code; |
| if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) { |
| dbus_message_iter_get_basic(iter, &status_code); |
| if (callbacks_pointer && callbacks_pointer->update_assoc_status_code) { |
| callbacks_pointer->update_assoc_status_code(interface, status_code); |
| } |
| } |
| } else { |
| SUPPLICANT_DBG("key %s type %c", |
| key, dbus_message_iter_get_arg_type(iter)); |
| } |
| } |
| |
| static void scan_network_update(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| GSupplicantNetwork *network; |
| char *path; |
| |
| if (!iter) |
| return; |
| |
| dbus_message_iter_get_basic(iter, &path); |
| |
| if (!path) |
| return; |
| |
| if (g_strcmp0(path, "/") == 0) |
| return; |
| |
| /* Update the network details based on scan BSS data */ |
| network = g_hash_table_lookup(interface->bss_mapping, path); |
| if (network) |
| callback_network_added(network); |
| } |
| |
| static void scan_bss_data(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| |
| if (iter) |
| supplicant_dbus_array_foreach(iter, scan_network_update, |
| interface); |
| |
| if (interface->scan_callback) |
| interface->scan_callback(0, interface, interface->scan_data); |
| |
| interface->scan_callback = NULL; |
| interface->scan_data = NULL; |
| } |
| |
| static GSupplicantInterface *interface_alloc(const char *path) |
| { |
| GSupplicantInterface *interface; |
| |
| interface = g_try_new0(GSupplicantInterface, 1); |
| if (!interface) |
| return NULL; |
| |
| interface->path = g_strdup(path); |
| |
| interface->network_table = g_hash_table_new_full(g_str_hash, |
| g_str_equal, NULL, remove_network); |
| interface->peer_table = g_hash_table_new_full(g_str_hash, |
| g_str_equal, NULL, remove_peer); |
| interface->group_table = g_hash_table_new_full(g_str_hash, |
| g_str_equal, NULL, remove_group); |
| interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal, |
| NULL, NULL); |
| interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal, |
| NULL, NULL); |
| |
| g_hash_table_replace(interface_table, interface->path, interface); |
| |
| return interface; |
| } |
| |
| static void interface_added(DBusMessageIter *iter, void *user_data) |
| { |
| GSupplicantInterface *interface; |
| const char *path = NULL; |
| |
| SUPPLICANT_DBG(""); |
| |
| dbus_message_iter_get_basic(iter, &path); |
| if (!path) |
| return; |
| |
| if (g_strcmp0(path, "/") == 0) |
| return; |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (interface) |
| return; |
| |
| interface = interface_alloc(path); |
| if (!interface) |
| return; |
| |
| dbus_message_iter_next(iter); |
| if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) { |
| supplicant_dbus_property_foreach(iter, interface_property, |
| interface); |
| interface_property(NULL, NULL, interface); |
| return; |
| } |
| |
| supplicant_dbus_property_get_all(path, |
| SUPPLICANT_INTERFACE ".Interface", |
| interface_property, interface, |
| interface); |
| } |
| |
| static void interface_removed(DBusMessageIter *iter, void *user_data) |
| { |
| const char *path = NULL; |
| GSupplicantInterface *interface = user_data; |
| |
| dbus_message_iter_get_basic(iter, &path); |
| if (!path) |
| return; |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| g_supplicant_interface_cancel(interface); |
| |
| g_hash_table_remove(interface_table, path); |
| } |
| |
| static void eap_method(DBusMessageIter *iter, void *user_data) |
| { |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| for (i = 0; eap_method_map[i].str; i++) |
| if (strcmp(str, eap_method_map[i].str) == 0) { |
| eap_methods |= eap_method_map[i].val; |
| break; |
| } |
| } |
| |
| static void service_property(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| if (!key) { |
| callback_system_ready(); |
| return; |
| } |
| |
| if (g_strcmp0(key, "DebugLevel") == 0) { |
| const char *str = NULL; |
| int i; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| for (i = 0; debug_strings[i]; i++) |
| if (g_strcmp0(debug_strings[i], str) == 0) { |
| debug_level = i; |
| break; |
| } |
| SUPPLICANT_DBG("Debug level %d", debug_level); |
| } else if (g_strcmp0(key, "DebugTimestamp") == 0) { |
| dbus_message_iter_get_basic(iter, &debug_timestamp); |
| SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp); |
| } else if (g_strcmp0(key, "DebugShowKeys") == 0) { |
| dbus_message_iter_get_basic(iter, &debug_showkeys); |
| SUPPLICANT_DBG("Debug show keys %u", debug_showkeys); |
| } else if (g_strcmp0(key, "Interfaces") == 0) { |
| supplicant_dbus_array_foreach(iter, interface_added, NULL); |
| } else if (g_strcmp0(key, "EapMethods") == 0) { |
| supplicant_dbus_array_foreach(iter, eap_method, NULL); |
| debug_strvalmap("EAP method", eap_method_map, eap_methods); |
| } else if (g_strcmp0(key, "Country") == 0) { |
| const char *country = NULL; |
| |
| dbus_message_iter_get_basic(iter, &country); |
| SUPPLICANT_DBG("Country %s", country); |
| } else |
| SUPPLICANT_DBG("key %s type %c", |
| key, dbus_message_iter_get_arg_type(iter)); |
| } |
| |
| static void signal_name_owner_changed(const char *path, DBusMessageIter *iter) |
| { |
| const char *name = NULL, *old = NULL, *new = NULL; |
| |
| SUPPLICANT_DBG(""); |
| |
| if (g_strcmp0(path, DBUS_PATH_DBUS) != 0) |
| return; |
| |
| dbus_message_iter_get_basic(iter, &name); |
| if (!name) |
| return; |
| |
| if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0) |
| return; |
| |
| dbus_message_iter_next(iter); |
| dbus_message_iter_get_basic(iter, &old); |
| dbus_message_iter_next(iter); |
| dbus_message_iter_get_basic(iter, &new); |
| |
| if (!old || !new) |
| return; |
| |
| if (strlen(old) > 0 && strlen(new) == 0) { |
| system_available = FALSE; |
| g_hash_table_remove_all(bss_mapping); |
| g_hash_table_remove_all(peer_mapping); |
| g_hash_table_remove_all(group_mapping); |
| g_hash_table_remove_all(interface_table); |
| callback_system_killed(); |
| } |
| |
| if (strlen(new) > 0 && strlen(old) == 0) { |
| system_available = TRUE; |
| supplicant_dbus_property_get_all(SUPPLICANT_PATH, |
| SUPPLICANT_INTERFACE, |
| service_property, NULL, NULL); |
| } |
| } |
| |
| static void signal_properties_changed(const char *path, DBusMessageIter *iter) |
| { |
| SUPPLICANT_DBG(""); |
| |
| if (g_strcmp0(path, SUPPLICANT_PATH) != 0) |
| return; |
| |
| supplicant_dbus_property_foreach(iter, service_property, NULL); |
| } |
| |
| static void signal_interface_added(const char *path, DBusMessageIter *iter) |
| { |
| SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH); |
| |
| if (g_strcmp0(path, SUPPLICANT_PATH) == 0) |
| interface_added(iter, NULL); |
| } |
| |
| static void signal_interface_removed(const char *path, DBusMessageIter *iter) |
| { |
| SUPPLICANT_DBG(""); |
| |
| if (g_strcmp0(path, SUPPLICANT_PATH) == 0) |
| interface_removed(iter, NULL); |
| } |
| |
| static void signal_interface_changed(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| supplicant_dbus_property_foreach(iter, interface_property, interface); |
| } |
| |
| static void signal_scan_done(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| dbus_bool_t success = FALSE; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| dbus_message_iter_get_basic(iter, &success); |
| |
| if (interface->scanning) { |
| callback_scan_finished(interface); |
| interface->scanning = FALSE; |
| } |
| |
| /* |
| * If scan is unsuccessful return -EIO else get the scanned BSSs |
| * and update the network details accordingly |
| */ |
| if (!success) { |
| if (interface->scan_callback) |
| interface->scan_callback(-EIO, interface, |
| interface->scan_data); |
| |
| interface->scan_callback = NULL; |
| interface->scan_data = NULL; |
| |
| return; |
| } |
| |
| supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface", |
| "BSSs", scan_bss_data, interface, interface); |
| } |
| |
| static void signal_bss_added(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| interface_bss_added_with_keys(iter, interface); |
| } |
| |
| static void signal_bss_removed(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| interface_bss_removed(iter, interface); |
| } |
| |
| static void signal_network_added(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| interface_network_added(iter, interface); |
| } |
| |
| static void signal_network_removed(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| interface_network_removed(iter, interface); |
| } |
| |
| static void signal_bss_changed(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| GSupplicantNetwork *network; |
| GSupplicantSecurity old_security; |
| struct g_supplicant_bss *bss; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(bss_mapping, path); |
| if (!interface) |
| return; |
| |
| network = g_hash_table_lookup(interface->bss_mapping, path); |
| if (!network) |
| return; |
| |
| bss = g_hash_table_lookup(network->bss_table, path); |
| if (!bss) |
| return; |
| |
| supplicant_dbus_property_foreach(iter, bss_property, bss); |
| |
| old_security = network->security; |
| bss_compute_security(bss); |
| |
| if (old_security != bss->security) { |
| struct g_supplicant_bss *new_bss; |
| |
| SUPPLICANT_DBG("New network security for %s", bss->ssid); |
| |
| /* Security change policy: |
| * - we first copy the current bss into a new one with |
| * its own pointer (path) |
| * - we remove the current bss related network which will |
| * tell the plugin about such removal. This is done due |
| * to the fact that a security change means a group change |
| * so a complete network change. |
| * (current bss becomes invalid as well) |
| * - we add the new bss: it adds new network and tell the |
| * plugin about it. */ |
| |
| new_bss = g_try_new0(struct g_supplicant_bss, 1); |
| if (!new_bss) |
| return; |
| |
| memcpy(new_bss, bss, sizeof(struct g_supplicant_bss)); |
| new_bss->path = g_strdup(bss->path); |
| |
| g_hash_table_remove(interface->network_table, network->group); |
| |
| add_or_replace_bss_to_network(new_bss); |
| |
| return; |
| } |
| |
| if (bss->signal == network->signal) |
| return; |
| |
| /* |
| * If the new signal is lower than the SSID signal, we need |
| * to check for the new maximum. |
| */ |
| if (bss->signal < network->signal) { |
| if (bss != network->best_bss) |
| return; |
| network->signal = bss->signal; |
| update_network_signal(network); |
| } else { |
| network->signal = bss->signal; |
| network->best_bss = bss; |
| } |
| |
| SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid, |
| network->signal); |
| |
| callback_network_changed(network, "Signal"); |
| } |
| |
| static void wps_credentials(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| |
| if (!key) |
| return; |
| |
| SUPPLICANT_DBG("key %s", key); |
| |
| if (g_strcmp0(key, "Key") == 0) { |
| DBusMessageIter array; |
| unsigned char *key_val; |
| int key_len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &key_val, &key_len); |
| |
| g_free(interface->wps_cred.key); |
| interface->wps_cred.key = g_try_malloc0( |
| sizeof(char) * key_len + 1); |
| |
| if (!interface->wps_cred.key) |
| return; |
| |
| memcpy(interface->wps_cred.key, key_val, |
| sizeof(char) * key_len); |
| |
| SUPPLICANT_DBG("WPS key present"); |
| } else if (g_strcmp0(key, "SSID") == 0) { |
| DBusMessageIter array; |
| unsigned char *ssid; |
| int ssid_len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len); |
| |
| if (ssid_len > 0 && ssid_len < 33) { |
| memcpy(interface->wps_cred.ssid, ssid, ssid_len); |
| interface->wps_cred.ssid_len = ssid_len; |
| } else { |
| memset(interface->wps_cred.ssid, 0, 32); |
| interface->wps_cred.ssid_len = 0; |
| } |
| } |
| } |
| |
| static void signal_wps_credentials(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| supplicant_dbus_property_foreach(iter, wps_credentials, interface); |
| } |
| |
| static void wps_event_args(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantInterface *interface = user_data; |
| |
| if (!key || !interface) |
| return; |
| |
| SUPPLICANT_DBG("Arg Key %s", key); |
| } |
| |
| static void signal_wps_event(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| const char *name = NULL; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| dbus_message_iter_get_basic(iter, &name); |
| |
| SUPPLICANT_DBG("Name: %s", name); |
| |
| if (g_strcmp0(name, "success") == 0) |
| interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS; |
| else if (g_strcmp0(name, "fail") == 0) |
| interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL; |
| else |
| interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN; |
| |
| if (!dbus_message_iter_has_next(iter)) |
| return; |
| |
| dbus_message_iter_next(iter); |
| |
| supplicant_dbus_property_foreach(iter, wps_event_args, interface); |
| } |
| |
| static void create_peer_identifier(GSupplicantPeer *peer) |
| { |
| const unsigned char test[ETH_ALEN] = {}; |
| |
| if (!peer) |
| return; |
| |
| if (!memcmp(peer->device_address, test, ETH_ALEN)) { |
| peer->identifier = g_strdup(peer->name); |
| return; |
| } |
| |
| peer->identifier = g_malloc0(19); |
| snprintf(peer->identifier, 19, "%02x%02x%02x%02x%02x%02x", |
| peer->device_address[0], |
| peer->device_address[1], |
| peer->device_address[2], |
| peer->device_address[3], |
| peer->device_address[4], |
| peer->device_address[5]); |
| } |
| |
| struct peer_property_data { |
| GSupplicantPeer *peer; |
| GSList *old_groups; |
| bool groups_changed; |
| bool services_changed; |
| }; |
| |
| static void peer_groups_relation(DBusMessageIter *iter, void *user_data) |
| { |
| struct peer_property_data *data = user_data; |
| GSupplicantPeer *peer = data->peer; |
| GSupplicantGroup *group; |
| const char *str = NULL; |
| GSList *elem; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (!str) |
| return; |
| |
| group = g_hash_table_lookup(group_mapping, str); |
| if (!group) |
| return; |
| |
| elem = g_slist_find_custom(data->old_groups, str, (GCompareFunc)g_strcmp0); |
| if (elem) { |
| data->old_groups = g_slist_remove_link(data->old_groups, elem); |
| peer->groups = g_slist_concat(elem, peer->groups); |
| } else { |
| peer->groups = g_slist_prepend(peer->groups, g_strdup(str)); |
| data->groups_changed = true; |
| } |
| } |
| |
| static void peer_property(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| GSupplicantPeer *pending_peer; |
| struct peer_property_data *data = user_data; |
| GSupplicantPeer *peer = data->peer; |
| |
| SUPPLICANT_DBG("key: %s", key); |
| |
| if (!peer->interface) |
| return; |
| |
| if (!key) { |
| if (peer->name) { |
| create_peer_identifier(peer); |
| callback_peer_found(peer); |
| pending_peer = g_hash_table_lookup( |
| pending_peer_connection, peer->path); |
| |
| if (pending_peer && pending_peer == peer) { |
| callback_peer_request(peer); |
| g_hash_table_remove(pending_peer_connection, |
| peer->path); |
| } |
| |
| dbus_free(data); |
| } |
| |
| return; |
| } |
| |
| if (g_strcmp0(key, "DeviceAddress") == 0) { |
| unsigned char *dev_addr; |
| DBusMessageIter array; |
| int len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &dev_addr, &len); |
| |
| if (len == ETH_ALEN) |
| memcpy(peer->device_address, dev_addr, len); |
| } else if (g_strcmp0(key, "DeviceName") == 0) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (str) |
| peer->name = g_strdup(str); |
| } else if (g_strcmp0(key, "config_method") == 0) { |
| uint16_t wps_config; |
| |
| dbus_message_iter_get_basic(iter, &wps_config); |
| |
| if (wps_config & G_SUPPLICANT_WPS_CONFIG_PBC) |
| peer->wps_capabilities |= G_SUPPLICANT_WPS_PBC; |
| if (wps_config & ~G_SUPPLICANT_WPS_CONFIG_PBC) |
| peer->wps_capabilities |= G_SUPPLICANT_WPS_PIN; |
| } else if (g_strcmp0(key, "Groups") == 0) { |
| data->old_groups = peer->groups; |
| peer->groups = NULL; |
| |
| supplicant_dbus_array_foreach(iter, |
| peer_groups_relation, data); |
| if (g_slist_length(data->old_groups) > 0) { |
| g_slist_free_full(data->old_groups, g_free); |
| data->groups_changed = true; |
| } |
| } else if (g_strcmp0(key, "IEs") == 0) { |
| DBusMessageIter array; |
| unsigned char *ie; |
| int ie_len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &ie, &ie_len); |
| |
| if (!ie || ie_len < 2) |
| return; |
| |
| if (peer->widi_ies) { |
| if (memcmp(peer->widi_ies, ie, ie_len) == 0) |
| return; |
| |
| g_free(peer->widi_ies); |
| peer->widi_ies_length = 0; |
| } |
| |
| peer->widi_ies = g_malloc0(ie_len * sizeof(unsigned char)); |
| |
| memcpy(peer->widi_ies, ie, ie_len); |
| peer->widi_ies_length = ie_len; |
| data->services_changed = true; |
| } |
| } |
| |
| static void signal_peer_found(const char *path, DBusMessageIter *iter) |
| { |
| struct peer_property_data *property_data; |
| GSupplicantInterface *interface; |
| const char *obj_path = NULL; |
| GSupplicantPeer *peer; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| dbus_message_iter_get_basic(iter, &obj_path); |
| if (!obj_path || g_strcmp0(obj_path, "/") == 0) |
| return; |
| |
| peer = g_hash_table_lookup(interface->peer_table, obj_path); |
| if (peer) |
| return; |
| |
| peer = g_try_new0(GSupplicantPeer, 1); |
| if (!peer) |
| return; |
| |
| peer->interface = interface; |
| peer->path = g_strdup(obj_path); |
| g_hash_table_insert(interface->peer_table, peer->path, peer); |
| g_hash_table_replace(peer_mapping, peer->path, interface); |
| |
| property_data = dbus_malloc0(sizeof(struct peer_property_data)); |
| property_data->peer = peer; |
| |
| dbus_message_iter_next(iter); |
| if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) { |
| supplicant_dbus_property_foreach(iter, peer_property, |
| property_data); |
| peer_property(NULL, NULL, property_data); |
| return; |
| } |
| |
| supplicant_dbus_property_get_all(obj_path, |
| SUPPLICANT_INTERFACE ".Peer", |
| peer_property, property_data, NULL); |
| } |
| |
| static void signal_peer_lost(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| const char *obj_path = NULL; |
| GSupplicantPeer *peer; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| dbus_message_iter_get_basic(iter, &obj_path); |
| if (!obj_path || g_strcmp0(obj_path, "/") == 0) |
| return; |
| |
| peer = g_hash_table_lookup(interface->peer_table, obj_path); |
| if (!peer) |
| return; |
| |
| g_hash_table_remove(interface->peer_table, obj_path); |
| } |
| |
| static void signal_peer_changed(const char *path, DBusMessageIter *iter) |
| { |
| struct peer_property_data *property_data; |
| GSupplicantInterface *interface; |
| GSupplicantPeer *peer; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(peer_mapping, path); |
| if (!interface) |
| return; |
| |
| peer = g_hash_table_lookup(interface->peer_table, path); |
| if (!peer) { |
| g_hash_table_remove(peer_mapping, path); |
| return; |
| } |
| |
| property_data = dbus_malloc0(sizeof(struct peer_property_data)); |
| property_data->peer = peer; |
| |
| supplicant_dbus_property_foreach(iter, peer_property, property_data); |
| if (property_data->services_changed) |
| callback_peer_changed(peer, |
| G_SUPPLICANT_PEER_SERVICES_CHANGED); |
| |
| if (property_data->groups_changed) |
| callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_CHANGED); |
| |
| dbus_free(property_data); |
| |
| if (!g_supplicant_peer_is_in_a_group(peer)) |
| peer->connection_requested = false; |
| } |
| |
| struct group_sig_data { |
| const char *peer_obj_path; |
| unsigned char iface_address[ETH_ALEN]; |
| const char *interface_obj_path; |
| const char *group_obj_path; |
| int role; |
| }; |
| |
| static void group_sig_property(const char *key, DBusMessageIter *iter, |
| void *user_data) |
| { |
| struct group_sig_data *data = user_data; |
| |
| if (!key) |
| return; |
| |
| if (g_strcmp0(key, "peer_interface_addr") == 0) { |
| unsigned char *dev_addr; |
| DBusMessageIter array; |
| int len; |
| |
| dbus_message_iter_recurse(iter, &array); |
| dbus_message_iter_get_fixed_array(&array, &dev_addr, &len); |
| |
| if (len == ETH_ALEN) |
| memcpy(data->iface_address, dev_addr, len); |
| } else if (g_strcmp0(key, "role") == 0) { |
| const char *str = NULL; |
| |
| dbus_message_iter_get_basic(iter, &str); |
| if (g_strcmp0(str, "GO") == 0) |
| data->role = G_SUPPLICANT_GROUP_ROLE_GO; |
| else |
| data->role = G_SUPPLICANT_GROUP_ROLE_CLIENT; |
| } else if (g_strcmp0(key, "peer_object") == 0) |
| dbus_message_iter_get_basic(iter, &data->peer_obj_path); |
| else if (g_strcmp0(key, "interface_object") == 0) |
| dbus_message_iter_get_basic(iter, &data->interface_obj_path); |
| else if (g_strcmp0(key, "group_object") == 0) |
| dbus_message_iter_get_basic(iter, &data->group_obj_path); |
| |
| } |
| |
| static void signal_group_success(const char *path, DBusMessageIter *iter) |
| { |
| GSupplicantInterface *interface; |
| struct group_sig_data data = {}; |
| GSupplicantPeer *peer; |
| |
| SUPPLICANT_DBG(""); |
| |
| interface = g_hash_table_lookup(interface_table, path); |
| if (!interface) |
| return; |
| |
| |