diff --git a/src/scan.c b/src/scan.c index 58c014d9..c084ef55 100644 --- a/src/scan.c +++ b/src/scan.c @@ -38,6 +38,17 @@ #include "src/ie.h" #include "src/scan.h" +struct l_genl_family *nl80211 = NULL; +uint32_t scan_id = 0; + +scan_notify_func_t notify = NULL; + +struct scan_results { + uint32_t wiphy; + uint32_t ifindex; + struct l_queue *bss_list; +}; + void scan_start(struct l_genl_family *nl80211, uint32_t ifindex, scan_func_t callback, void *user_data) { @@ -66,17 +77,6 @@ void scan_sched_start(struct l_genl_family *nl80211, uint32_t ifindex, l_error("Starting scheduled scan failed"); } -void scan_get_results(struct l_genl_family *nl80211, uint32_t ifindex, - scan_func_t callback, scan_done_func_t scan_done, - void *user_data) -{ - struct l_genl_msg *msg; - - msg = l_genl_msg_new_sized(NL80211_CMD_GET_SCAN, 8); - l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex); - l_genl_family_dump(nl80211, msg, callback, user_data, scan_done); -} - enum scan_ssid_security scan_get_ssid_security( enum ie_bss_capability bss_capability, const struct ie_rsn_info *info) @@ -100,10 +100,10 @@ enum scan_ssid_security scan_get_ssid_security( } static bool scan_parse_bss_information_elements(struct scan_bss *bss, - const uint8_t **ssid, uint8_t *ssid_len, const void *data, uint16_t len) { struct ie_tlv_iter iter; + bool have_ssid = false; ie_tlv_iter_init(&iter, data, len); @@ -115,8 +115,9 @@ static bool scan_parse_bss_information_elements(struct scan_bss *bss, if (iter.len > 32) return false; - *ssid_len = iter.len; - *ssid = iter.data; + memcpy(bss->ssid, iter.data, iter.len); + bss->ssid_len = iter.len; + have_ssid = true; break; case IE_TYPE_RSN: if (!bss->rsne) @@ -132,12 +133,10 @@ static bool scan_parse_bss_information_elements(struct scan_bss *bss, } } - return true; + return have_ssid; } -static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr, - const uint8_t **ssid, - uint8_t *ssid_len) +static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr) { uint16_t type, len; const void *data; @@ -173,7 +172,7 @@ static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr, break; case NL80211_BSS_INFORMATION_ELEMENTS: if (!scan_parse_bss_information_elements(bss, - ssid, ssid_len, data, len)) + data, len)) goto fail; break; @@ -187,11 +186,9 @@ fail: return NULL; } -struct scan_bss *scan_parse_result(struct l_genl_msg *msg, +static struct scan_bss *scan_parse_result(struct l_genl_msg *msg, uint32_t *out_ifindex, - uint64_t *out_wdev, - const uint8_t **out_ssid, - uint8_t *out_ssid_len) + uint64_t *out_wdev) { struct l_genl_attr attr, nested; uint16_t type, len; @@ -199,8 +196,6 @@ struct scan_bss *scan_parse_result(struct l_genl_msg *msg, uint32_t ifindex; uint64_t wdev; struct scan_bss *bss = NULL; - const uint8_t *ssid = NULL; - uint8_t ssid_len = 0; if (!l_genl_attr_init(&attr, msg)) return NULL; @@ -225,7 +220,7 @@ struct scan_bss *scan_parse_result(struct l_genl_msg *msg, if (!l_genl_attr_recurse(&attr, &nested)) return NULL; - bss = scan_parse_attr_bss(&nested, &ssid, &ssid_len); + bss = scan_parse_attr_bss(&nested); break; } } @@ -233,23 +228,12 @@ struct scan_bss *scan_parse_result(struct l_genl_msg *msg, if (!bss) return NULL; - if (!ssid) { - scan_bss_free(bss); - return NULL; - } - if (out_ifindex) *out_ifindex = ifindex; if (out_wdev) *out_wdev = wdev; - if (out_ssid_len) - *out_ssid_len = ssid_len; - - if (out_ssid) - *out_ssid = ssid; - return bss; } @@ -291,3 +275,150 @@ void bss_get_supported_ciphers(struct scan_bss *bss, *pairwise_ciphers = ie.pairwise_ciphers; *group_ciphers = ie.group_cipher; } + +static void get_scan_callback(struct l_genl_msg *msg, void *user_data) +{ + struct scan_results *results = user_data; + struct scan_bss *bss; + uint32_t ifindex; + + l_debug("get_scan_callback"); + + if (!results->bss_list) + results->bss_list = l_queue_new(); + + bss = scan_parse_result(msg, &ifindex, NULL); + if (!bss) + return; + + if (ifindex != results->ifindex) { + l_warn("ifindex mismatch in get_scan_callback"); + scan_bss_free(bss); + return; + } + + l_queue_push_tail(results->bss_list, bss); +} + +static void get_scan_done(void *user) +{ + struct scan_results *results = user; + bool new_owner = false; + + l_debug("get_scan_done"); + + if (!results->bss_list) + goto done; + + if (notify) + new_owner = notify(results->wiphy, results->ifindex, + results->bss_list); + + if (!new_owner) + l_queue_destroy(results->bss_list, + (l_queue_destroy_func_t) scan_bss_free); + +done: + l_free(results); +} + +static void scan_notify(struct l_genl_msg *msg, void *user_data) +{ + struct l_genl_attr attr; + uint16_t type, len; + const void *data; + uint8_t cmd; + uint32_t uninitialized_var(attr_ifindex); + bool have_ifindex; + uint32_t uninitialized_var(attr_wiphy); + bool have_wiphy; + + cmd = l_genl_msg_get_command(msg); + + l_debug("Scan notification %u", cmd); + + if (!l_genl_attr_init(&attr, msg)) + return; + + while (l_genl_attr_next(&attr, &type, &len, &data)) { + switch (type) { + case NL80211_ATTR_WIPHY: + if (len != sizeof(uint32_t)) { + l_warn("Invalid wiphy attribute"); + return; + } + + have_wiphy = true; + attr_wiphy = *((uint32_t *) data); + break; + case NL80211_ATTR_IFINDEX: + if (len != sizeof(uint32_t)) { + l_warn("Invalid interface index attribute"); + return; + } + + have_ifindex = true; + attr_ifindex = *((uint32_t *) data); + break; + } + } + + if (!have_wiphy) { + l_warn("Scan results do not contain wiphy attribute"); + return; + } + + if (!have_ifindex) { + l_warn("Scan results do not contain ifindex attribute"); + return; + } + + if (!notify) + return; + + switch (cmd) { + case NL80211_CMD_NEW_SCAN_RESULTS: + case NL80211_CMD_SCHED_SCAN_RESULTS: + { + struct l_genl_msg *msg; + struct scan_results *results; + + results = l_new(struct scan_results, 1); + results->wiphy = attr_wiphy; + results->ifindex = attr_ifindex; + + msg = l_genl_msg_new_sized(NL80211_CMD_GET_SCAN, 8); + l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, + &attr_ifindex); + l_genl_family_dump(nl80211, msg, get_scan_callback, results, + get_scan_done); + return; + } + } +} + +bool scan_init(struct l_genl_family *in, scan_notify_func_t func) +{ + nl80211 = in; + scan_id = l_genl_family_register(nl80211, "scan", scan_notify, + NULL, NULL); + + if (!scan_id) { + l_error("Registering for scan notification failed"); + return false; + } + + notify = func; + + return true; +} + +bool scan_free() +{ + if (!nl80211) + return false; + + notify = NULL; + + return l_genl_family_unregister(nl80211, scan_id); +} diff --git a/src/scan.h b/src/scan.h index 6d595c39..27be2840 100644 --- a/src/scan.h +++ b/src/scan.h @@ -28,7 +28,8 @@ enum scan_ssid_security { }; typedef void (*scan_func_t)(struct l_genl_msg *msg, void *user_data); -typedef void (*scan_done_func_t)(void *user_data); +typedef bool (*scan_notify_func_t)(uint32_t wiphy, uint32_t ifindex, + struct l_queue *bss_list); struct scan_bss { uint8_t addr[6]; @@ -37,6 +38,8 @@ struct scan_bss { uint16_t capability; uint8_t *rsne; uint8_t *wpa; + uint8_t ssid[32]; + uint8_t ssid_len; }; void scan_start(struct l_genl_family *nl80211, uint32_t ifindex, @@ -46,20 +49,13 @@ void scan_sched_start(struct l_genl_family *nl80211, uint32_t ifindex, uint32_t scan_interval, scan_func_t callback, void *user_data); -void scan_get_results(struct l_genl_family *nl80211, uint32_t ifindex, - scan_func_t callback, scan_done_func_t scan_done, - void *user_data); - enum scan_ssid_security scan_get_ssid_security(enum ie_bss_capability bss_cap, const struct ie_rsn_info *info); void scan_bss_free(struct scan_bss *bss); -struct scan_bss *scan_parse_result(struct l_genl_msg *msg, - uint32_t *out_ifindex, - uint64_t *out_wdev, - const uint8_t **out_ssid, - uint8_t *out_ssid_len); - void bss_get_supported_ciphers(struct scan_bss *bss, uint16_t *pairwise_ciphers, uint16_t *group_ciphers); + +bool scan_init(struct l_genl_family *in, scan_notify_func_t func); +bool scan_free(); diff --git a/src/wiphy.c b/src/wiphy.c index 87fc0ba4..7e68c82d 100644 --- a/src/wiphy.c +++ b/src/wiphy.c @@ -1488,111 +1488,6 @@ static void network_reset_bss_list(const void *key, void *value, network->bss_list = l_queue_new(); } -static void get_scan_callback(struct l_genl_msg *msg, void *user_data) -{ - struct netdev *netdev = user_data; - struct scan_bss *bss; - uint32_t ifindex; - const uint8_t *ssid; - uint8_t ssid_len; - struct network *network; - enum scan_ssid_security ssid_security; - const char *path; - - l_debug("get_scan_callback"); - - if (!netdev->old_bss_list) { - netdev->old_bss_list = netdev->bss_list; - netdev->bss_list = l_queue_new(); - l_hashmap_foreach(netdev->networks, - network_reset_bss_list, NULL); - } - - bss = scan_parse_result(msg, &ifindex, NULL, &ssid, &ssid_len); - if (!bss) - return; - - if (ifindex != netdev->index) - goto fail; - - if (!util_ssid_is_utf8(ssid_len, ssid)) { - l_warn("Ignoring Network with non-UTF8 SSID '%s'", - util_ssid_to_utf8(ssid_len, ssid)); - goto fail; - } - - /* - * If both an RSN and a WPA elements are present currently - * RSN takes priority and the WPA IE is ignored. - */ - if (bss->rsne) { - struct ie_rsn_info rsne; - int res = ie_parse_rsne_from_data(bss->rsne, bss->rsne[1] + 2, - &rsne); - if (res < 0) { - l_debug("Cannot parse RSN field (%d, %s)", - res, strerror(-res)); - goto fail; - } - - ssid_security = scan_get_ssid_security(bss->capability, &rsne); - } else if (bss->wpa) { - struct ie_rsn_info wpa; - int res = ie_parse_wpa_from_data(bss->wpa, bss->wpa[1] + 2, - &wpa); - if (res < 0) { - l_debug("Cannot parse WPA IE %s (%d, %s)", - util_ssid_to_utf8(ssid_len, ssid), - res, strerror(-res)); - goto fail; - } - - ssid_security = scan_get_ssid_security(bss->capability, &wpa); - } else - ssid_security = scan_get_ssid_security(bss->capability, NULL); - - path = iwd_network_get_path(netdev, ssid, ssid_len, ssid_security); - - network = l_hashmap_lookup(netdev->networks, path); - if (!network) { - network = l_new(struct network, 1); - network->netdev = netdev; - memcpy(network->ssid, ssid, ssid_len); - network->ssid_security = ssid_security; - network->bss_list = l_queue_new(); - network->object_path = strdup(path); - l_hashmap_insert(netdev->networks, - network->object_path, network); - - l_debug("Found new SSID \"%s\" security %s", network->ssid, - ssid_security_to_str(ssid_security)); - - if (!l_dbus_register_interface(dbus_get_bus(), - network->object_path, - IWD_NETWORK_INTERFACE, - setup_network_interface, - network, NULL)) - l_info("Unable to register %s interface", - IWD_NETWORK_INTERFACE); - else - network_emit_added(network); - } - - l_debug("Found BSS '%s' with SSID: %s, freq: %u, " - "strength: %i", - bss_address_to_string(bss), - util_ssid_to_utf8(ssid_len, ssid), - bss->frequency, bss->signal_strength); - - l_queue_insert(network->bss_list, bss, add_bss, NULL); - l_queue_push_head(netdev->bss_list, bss); - return; - -fail: - bss_free(bss); - -} - static bool network_remove_if_lost(const void *key, void *data, void *user_data) { struct network *network = data; @@ -1607,11 +1502,114 @@ static bool network_remove_if_lost(const void *key, void *data, void *user_data) return true; } -static void get_scan_done(void *user) +static void process_bss(struct netdev *netdev, struct scan_bss *bss) { - struct netdev *netdev = user; + struct network *network; + enum scan_ssid_security ssid_security; + const char *path; - l_debug("get_scan_done for netdev: %p", netdev); + l_debug("Found BSS '%s' with SSID: %s, freq: %u, " + "strength: %i", + bss_address_to_string(bss), + util_ssid_to_utf8(bss->ssid_len, bss->ssid), + bss->frequency, bss->signal_strength); + + if (!util_ssid_is_utf8(bss->ssid_len, bss->ssid)) { + l_warn("Ignoring BSS with non-UTF8 SSID"); + return; + } + + /* + * If both an RSN and a WPA elements are present currently + * RSN takes priority and the WPA IE is ignored. + */ + if (bss->rsne) { + struct ie_rsn_info rsne; + int res = ie_parse_rsne_from_data(bss->rsne, bss->rsne[1] + 2, + &rsne); + if (res < 0) { + l_debug("Cannot parse RSN field (%d, %s)", + res, strerror(-res)); + return; + } + + ssid_security = scan_get_ssid_security(bss->capability, &rsne); + } else if (bss->wpa) { + struct ie_rsn_info wpa; + int res = ie_parse_wpa_from_data(bss->wpa, bss->wpa[1] + 2, + &wpa); + if (res < 0) { + l_debug("Cannot parse WPA IE (%d, %s)", + res, strerror(-res)); + return; + } + + ssid_security = scan_get_ssid_security(bss->capability, &wpa); + } else + ssid_security = scan_get_ssid_security(bss->capability, NULL); + + path = iwd_network_get_path(netdev, bss->ssid, bss->ssid_len, + ssid_security); + + network = l_hashmap_lookup(netdev->networks, path); + if (!network) { + network = l_new(struct network, 1); + network->netdev = netdev; + memcpy(network->ssid, bss->ssid, bss->ssid_len); + network->ssid_security = ssid_security; + network->bss_list = l_queue_new(); + network->object_path = strdup(path); + l_hashmap_insert(netdev->networks, + network->object_path, network); + + l_debug("Added new Network \"%s\" security %s", network->ssid, + ssid_security_to_str(ssid_security)); + + if (!l_dbus_register_interface(dbus_get_bus(), + network->object_path, + IWD_NETWORK_INTERFACE, + setup_network_interface, + network, NULL)) + l_info("Unable to register %s interface", + IWD_NETWORK_INTERFACE); + else + network_emit_added(network); + } + + l_queue_insert(network->bss_list, bss, add_bss, NULL); +} + +static bool new_scan_results(uint32_t wiphy_id, uint32_t ifindex, + struct l_queue *bss_list) +{ + struct wiphy *wiphy; + struct netdev *netdev; + const struct l_queue_entry *bss_entry; + + wiphy = l_queue_find(wiphy_list, wiphy_match, + L_UINT_TO_PTR(wiphy_id)); + if (!wiphy) { + l_warn("Scan notification for unknown wiphy"); + return false; + } + + netdev = l_queue_find(wiphy->netdev_list, netdev_match, + L_UINT_TO_PTR(ifindex)); + if (!netdev) { + l_warn("Scan notification for unknown ifindex"); + return false; + } + + netdev->old_bss_list = netdev->bss_list; + netdev->bss_list = bss_list; + l_hashmap_foreach(netdev->networks, network_reset_bss_list, NULL); + + for (bss_entry = l_queue_get_entries(bss_list); bss_entry; + bss_entry = bss_entry->next) { + struct scan_bss *bss = bss_entry->data; + + process_bss(netdev, bss); + } if (netdev->connected_bss) { struct scan_bss *bss; @@ -1636,6 +1634,8 @@ static void get_scan_done(void *user) l_queue_destroy(netdev->old_bss_list, bss_free); netdev->old_bss_list = NULL; + + return true; } static void interface_dump_callback(struct l_genl_msg *msg, void *user_data) @@ -1940,81 +1940,6 @@ static void wiphy_config_notify(struct l_genl_msg *msg, void *user_data) } } -static void wiphy_scan_notify(struct l_genl_msg *msg, void *user_data) -{ - struct wiphy *wiphy = NULL; - struct netdev *netdev = NULL; - struct l_genl_attr attr; - uint16_t type, len; - const void *data; - uint8_t cmd; - uint32_t uninitialized_var(attr_ifindex); - bool have_ifindex; - uint32_t uninitialized_var(attr_wiphy); - bool have_wiphy; - - cmd = l_genl_msg_get_command(msg); - - l_debug("Scan notification %u", cmd); - - if (!l_genl_attr_init(&attr, msg)) - return; - - while (l_genl_attr_next(&attr, &type, &len, &data)) { - switch (type) { - case NL80211_ATTR_WIPHY: - if (len != sizeof(uint32_t)) { - l_warn("Invalid wiphy attribute"); - return; - } - - have_wiphy = true; - attr_wiphy = *((uint32_t *) data); - break; - case NL80211_ATTR_IFINDEX: - if (len != sizeof(uint32_t)) { - l_warn("Invalid interface index attribute"); - return; - } - - have_ifindex = true; - attr_ifindex = *((uint32_t *) data); - break; - } - } - - if (!have_wiphy) { - l_warn("Scan results do not contain wiphy attribute"); - return; - } - - if (!have_ifindex) { - l_warn("Scan results do not contain ifindex attribute"); - return; - } - - wiphy = l_queue_find(wiphy_list, wiphy_match, - L_UINT_TO_PTR(attr_wiphy)); - if (!wiphy) { - l_warn("Scan notification for unknown wiphy"); - return; - } - - netdev = l_queue_find(wiphy->netdev_list, netdev_match, - L_UINT_TO_PTR(attr_ifindex)); - if (!netdev) { - l_warn("Scan notification for unknown ifindex"); - return; - } - - if (cmd == NL80211_CMD_NEW_SCAN_RESULTS || - cmd == NL80211_CMD_SCHED_SCAN_RESULTS) { - scan_get_results(nl80211, netdev->index, get_scan_callback, - get_scan_done, netdev); - return; - } -} - static void wiphy_mlme_notify(struct l_genl_msg *msg, void *user_data) { struct wiphy *wiphy = NULL; @@ -2185,10 +2110,6 @@ static void nl80211_appeared(void *user_data) NULL, NULL)) l_error("Registering for config notification failed"); - if (!l_genl_family_register(nl80211, "scan", wiphy_scan_notify, - NULL, NULL)) - l_error("Registering for scan notification failed"); - if (!l_genl_family_register(nl80211, "mlme", wiphy_mlme_notify, NULL, NULL)) l_error("Registering for MLME notification failed"); @@ -2197,6 +2118,9 @@ static void nl80211_appeared(void *user_data) wiphy_regulatory_notify, NULL, NULL)) l_error("Registering for regulatory notification failed"); + if (!scan_init(nl80211, new_scan_results)) + l_error("Unable to init scan functionality"); + wiphy_list = l_queue_new(); msg = l_genl_msg_new(NL80211_CMD_GET_PROTOCOL_FEATURES);