10a51b27eSJohannes Berg /* 25484e237SJohannes Berg * Scanning implementation 35484e237SJohannes Berg * 40a51b27eSJohannes Berg * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> 50a51b27eSJohannes Berg * Copyright 2004, Instant802 Networks, Inc. 60a51b27eSJohannes Berg * Copyright 2005, Devicescape Software, Inc. 70a51b27eSJohannes Berg * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 80a51b27eSJohannes Berg * Copyright 2007, Michael Wu <flamingice@sourmilk.net> 974d803b6SSara Sharon * Copyright 2013-2015 Intel Mobile Communications GmbH 107947d3e0SAvraham Stern * Copyright 2016 Intel Deutschland GmbH 110a51b27eSJohannes Berg * 120a51b27eSJohannes Berg * This program is free software; you can redistribute it and/or modify 130a51b27eSJohannes Berg * it under the terms of the GNU General Public License version 2 as 140a51b27eSJohannes Berg * published by the Free Software Foundation. 150a51b27eSJohannes Berg */ 160a51b27eSJohannes Berg 170a51b27eSJohannes Berg #include <linux/if_arp.h> 18888d04dfSFelix Fietkau #include <linux/etherdevice.h> 19078e1e60SJohannes Berg #include <linux/rtnetlink.h> 20df13cce5SHelmut Schaa #include <net/sch_generic.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 22bc3b2d7fSPaul Gortmaker #include <linux/export.h> 230a51b27eSJohannes Berg #include <net/mac80211.h> 240a51b27eSJohannes Berg 250a51b27eSJohannes Berg #include "ieee80211_i.h" 2624487981SJohannes Berg #include "driver-ops.h" 275484e237SJohannes Berg #include "mesh.h" 280a51b27eSJohannes Berg 290a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33) 300a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33) 313f892b61SStanislaw Gruszka #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9) 320a51b27eSJohannes Berg 335484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local, 34c2b13452SJohannes Berg struct ieee80211_bss *bss) 355484e237SJohannes Berg { 360c1ad2caSJohannes Berg if (!bss) 370c1ad2caSJohannes Berg return; 385b112d3dSJohannes Berg cfg80211_put_bss(local->hw.wiphy, 395b112d3dSJohannes Berg container_of((void *)bss, struct cfg80211_bss, priv)); 405484e237SJohannes Berg } 415484e237SJohannes Berg 42ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems) 43ab13315aSKalle Valo { 44ab13315aSKalle Valo u8 qos_info; 45ab13315aSKalle Valo 46ab13315aSKalle Valo if (elems->wmm_info && elems->wmm_info_len == 7 47ab13315aSKalle Valo && elems->wmm_info[5] == 1) 48ab13315aSKalle Valo qos_info = elems->wmm_info[6]; 49ab13315aSKalle Valo else if (elems->wmm_param && elems->wmm_param_len == 24 50ab13315aSKalle Valo && elems->wmm_param[5] == 1) 51ab13315aSKalle Valo qos_info = elems->wmm_param[6]; 52ab13315aSKalle Valo else 53ab13315aSKalle Valo /* no valid wmm information or parameter element found */ 54ab13315aSKalle Valo return false; 55ab13315aSKalle Valo 56ab13315aSKalle Valo return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD; 57ab13315aSKalle Valo } 58ab13315aSKalle Valo 59c2b13452SJohannes Berg struct ieee80211_bss * 605484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local, 615484e237SJohannes Berg struct ieee80211_rx_status *rx_status, 62d45c4172SEmmanuel Grumbach struct ieee80211_mgmt *mgmt, size_t len, 635484e237SJohannes Berg struct ieee802_11_elems *elems, 64d45c4172SEmmanuel Grumbach struct ieee80211_channel *channel) 655484e237SJohannes Berg { 66d45c4172SEmmanuel Grumbach bool beacon = ieee80211_is_beacon(mgmt->frame_control); 670c1ad2caSJohannes Berg struct cfg80211_bss *cbss; 68c2b13452SJohannes Berg struct ieee80211_bss *bss; 69f0b058b6SStanislaw Gruszka int clen, srlen; 70162dd6a7SJohannes Berg struct cfg80211_inform_bss bss_meta = { 71162dd6a7SJohannes Berg .boottime_ns = rx_status->boottime_ns, 72162dd6a7SJohannes Berg }; 7374d803b6SSara Sharon bool signal_valid; 747947d3e0SAvraham Stern struct ieee80211_sub_if_data *scan_sdata; 752a519311SJohannes Berg 7630686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) 7761f6bba0SJohannes Berg bss_meta.signal = rx_status->signal * 100; 7830686bf7SJohannes Berg else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) 7961f6bba0SJohannes Berg bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal; 802a519311SJohannes Berg 8161f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20; 827ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_5MHZ) 8361f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5; 847ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_10MHZ) 8561f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10; 867ca15a0aSSimon Wunderlich 8761f6bba0SJohannes Berg bss_meta.chan = channel; 887947d3e0SAvraham Stern 897947d3e0SAvraham Stern rcu_read_lock(); 907947d3e0SAvraham Stern scan_sdata = rcu_dereference(local->scan_sdata); 917947d3e0SAvraham Stern if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION && 927947d3e0SAvraham Stern scan_sdata->vif.bss_conf.assoc && 937947d3e0SAvraham Stern ieee80211_have_rx_timestamp(rx_status)) { 947947d3e0SAvraham Stern bss_meta.parent_tsf = 957947d3e0SAvraham Stern ieee80211_calculate_rx_timestamp(local, rx_status, 967947d3e0SAvraham Stern len + FCS_LEN, 24); 977947d3e0SAvraham Stern ether_addr_copy(bss_meta.parent_bssid, 987947d3e0SAvraham Stern scan_sdata->vif.bss_conf.bssid); 997947d3e0SAvraham Stern } 1007947d3e0SAvraham Stern rcu_read_unlock(); 1017947d3e0SAvraham Stern 10261f6bba0SJohannes Berg cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, 10361f6bba0SJohannes Berg mgmt, len, GFP_ATOMIC); 1040c1ad2caSJohannes Berg if (!cbss) 1055484e237SJohannes Berg return NULL; 10674d803b6SSara Sharon /* In case the signal is invalid update the status */ 10774d803b6SSara Sharon signal_valid = abs(channel->center_freq - cbss->channel->center_freq) 10874d803b6SSara Sharon <= local->hw.wiphy->max_adj_channel_rssi_comp; 10974d803b6SSara Sharon if (!signal_valid) 11074d803b6SSara Sharon rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL; 11100d3f14cSJohannes Berg 1120c1ad2caSJohannes Berg bss = (void *)cbss->priv; 1135484e237SJohannes Berg 114ef429dadSJohannes Berg if (beacon) 115ef429dadSJohannes Berg bss->device_ts_beacon = rx_status->device_timestamp; 116ef429dadSJohannes Berg else 117ef429dadSJohannes Berg bss->device_ts_presp = rx_status->device_timestamp; 1188c358bcdSJohannes Berg 119fcff4f10SPaul Stewart if (elems->parse_error) { 120fcff4f10SPaul Stewart if (beacon) 121fcff4f10SPaul Stewart bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON; 122fcff4f10SPaul Stewart else 123fcff4f10SPaul Stewart bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP; 124fcff4f10SPaul Stewart } else { 125fcff4f10SPaul Stewart if (beacon) 126fcff4f10SPaul Stewart bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON; 127fcff4f10SPaul Stewart else 128fcff4f10SPaul Stewart bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP; 1295484e237SJohannes Berg } 1305484e237SJohannes Berg 131fcff4f10SPaul Stewart /* save the ERP value so that it is available at association time */ 1321946bed9SJohannes Berg if (elems->erp_info && (!elems->parse_error || 133fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) { 134fcff4f10SPaul Stewart bss->erp_value = elems->erp_info[0]; 135fcff4f10SPaul Stewart bss->has_erp_value = true; 136fcff4f10SPaul Stewart if (!elems->parse_error) 137fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_ERP; 138fcff4f10SPaul Stewart } 139fcff4f10SPaul Stewart 140f0b058b6SStanislaw Gruszka /* replace old supported rates if we get new values */ 141fcff4f10SPaul Stewart if (!elems->parse_error || 142fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) { 143f0b058b6SStanislaw Gruszka srlen = 0; 1445484e237SJohannes Berg if (elems->supp_rates) { 145f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES; 1465484e237SJohannes Berg if (clen > elems->supp_rates_len) 1475484e237SJohannes Berg clen = elems->supp_rates_len; 148f0b058b6SStanislaw Gruszka memcpy(bss->supp_rates, elems->supp_rates, clen); 149f0b058b6SStanislaw Gruszka srlen += clen; 1505484e237SJohannes Berg } 1515484e237SJohannes Berg if (elems->ext_supp_rates) { 152f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES - srlen; 1535484e237SJohannes Berg if (clen > elems->ext_supp_rates_len) 1545484e237SJohannes Berg clen = elems->ext_supp_rates_len; 155fcff4f10SPaul Stewart memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, 156fcff4f10SPaul Stewart clen); 157f0b058b6SStanislaw Gruszka srlen += clen; 1585484e237SJohannes Berg } 159fcff4f10SPaul Stewart if (srlen) { 160f0b058b6SStanislaw Gruszka bss->supp_rates_len = srlen; 161fcff4f10SPaul Stewart if (!elems->parse_error) 162fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_RATES; 163fcff4f10SPaul Stewart } 164fcff4f10SPaul Stewart } 1655484e237SJohannes Berg 166fcff4f10SPaul Stewart if (!elems->parse_error || 167fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) { 1685484e237SJohannes Berg bss->wmm_used = elems->wmm_param || elems->wmm_info; 169ab13315aSKalle Valo bss->uapsd_supported = is_uapsd_supported(elems); 170fcff4f10SPaul Stewart if (!elems->parse_error) 171fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_WMM; 172fcff4f10SPaul Stewart } 1735484e237SJohannes Berg 174817cee76SAlexander Bondar if (beacon) { 175817cee76SAlexander Bondar struct ieee80211_supported_band *sband = 176817cee76SAlexander Bondar local->hw.wiphy->bands[rx_status->band]; 177817cee76SAlexander Bondar if (!(rx_status->flag & RX_FLAG_HT) && 178817cee76SAlexander Bondar !(rx_status->flag & RX_FLAG_VHT)) 179817cee76SAlexander Bondar bss->beacon_rate = 180817cee76SAlexander Bondar &sband->bitrates[rx_status->rate_idx]; 181817cee76SAlexander Bondar } 182817cee76SAlexander Bondar 1835484e237SJohannes Berg return bss; 1845484e237SJohannes Berg } 1850a51b27eSJohannes Berg 186d48b2968SJohannes Berg void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb) 18798c8fccfSJohannes Berg { 188f1d58c25SJohannes Berg struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 189d48b2968SJohannes Berg struct ieee80211_sub_if_data *sdata1, *sdata2; 190d48b2968SJohannes Berg struct ieee80211_mgmt *mgmt = (void *)skb->data; 191c2b13452SJohannes Berg struct ieee80211_bss *bss; 19298c8fccfSJohannes Berg u8 *elements; 19398c8fccfSJohannes Berg struct ieee80211_channel *channel; 19498c8fccfSJohannes Berg size_t baselen; 19598c8fccfSJohannes Berg struct ieee802_11_elems elems; 19698c8fccfSJohannes Berg 197d48b2968SJohannes Berg if (skb->len < 24 || 198d48b2968SJohannes Berg (!ieee80211_is_probe_resp(mgmt->frame_control) && 199d48b2968SJohannes Berg !ieee80211_is_beacon(mgmt->frame_control))) 200d48b2968SJohannes Berg return; 20198c8fccfSJohannes Berg 202d48b2968SJohannes Berg sdata1 = rcu_dereference(local->scan_sdata); 203d48b2968SJohannes Berg sdata2 = rcu_dereference(local->sched_scan_sdata); 20498c8fccfSJohannes Berg 205d48b2968SJohannes Berg if (likely(!sdata1 && !sdata2)) 206d48b2968SJohannes Berg return; 20798c8fccfSJohannes Berg 208d48b2968SJohannes Berg if (ieee80211_is_probe_resp(mgmt->frame_control)) { 209a344d677SJohannes Berg struct cfg80211_scan_request *scan_req; 210a344d677SJohannes Berg struct cfg80211_sched_scan_request *sched_scan_req; 211a344d677SJohannes Berg 212a344d677SJohannes Berg scan_req = rcu_dereference(local->scan_req); 213a344d677SJohannes Berg sched_scan_req = rcu_dereference(local->sched_scan_req); 214a344d677SJohannes Berg 215a344d677SJohannes Berg /* ignore ProbeResp to foreign address unless scanning 216a344d677SJohannes Berg * with randomised address 217a344d677SJohannes Berg */ 218a344d677SJohannes Berg if (!(sdata1 && 219a344d677SJohannes Berg (ether_addr_equal(mgmt->da, sdata1->vif.addr) || 220a344d677SJohannes Berg scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) && 221a344d677SJohannes Berg !(sdata2 && 222a344d677SJohannes Berg (ether_addr_equal(mgmt->da, sdata2->vif.addr) || 223a344d677SJohannes Berg sched_scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR))) 224d48b2968SJohannes Berg return; 22598c8fccfSJohannes Berg 22698c8fccfSJohannes Berg elements = mgmt->u.probe_resp.variable; 22798c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); 22898c8fccfSJohannes Berg } else { 22998c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 23098c8fccfSJohannes Berg elements = mgmt->u.beacon.variable; 23198c8fccfSJohannes Berg } 23298c8fccfSJohannes Berg 23398c8fccfSJohannes Berg if (baselen > skb->len) 234d48b2968SJohannes Berg return; 23598c8fccfSJohannes Berg 236b2e506bfSJohannes Berg ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems); 23798c8fccfSJohannes Berg 2380172bb75SJohannes Berg channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 23998c8fccfSJohannes Berg 24098c8fccfSJohannes Berg if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 241d48b2968SJohannes Berg return; 24298c8fccfSJohannes Berg 243d48b2968SJohannes Berg bss = ieee80211_bss_info_update(local, rx_status, 24498c8fccfSJohannes Berg mgmt, skb->len, &elems, 245d45c4172SEmmanuel Grumbach channel); 246d048e503SJouni Malinen if (bss) 247d48b2968SJohannes Berg ieee80211_rx_bss_put(local, bss); 24898c8fccfSJohannes Berg } 24998c8fccfSJohannes Berg 2502103dec1SSimon Wunderlich static void 2512103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef, 2522103dec1SSimon Wunderlich enum nl80211_bss_scan_width scan_width) 2532103dec1SSimon Wunderlich { 2542103dec1SSimon Wunderlich memset(chandef, 0, sizeof(*chandef)); 2552103dec1SSimon Wunderlich switch (scan_width) { 2562103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 2572103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_5; 2582103dec1SSimon Wunderlich break; 2592103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 2602103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_10; 2612103dec1SSimon Wunderlich break; 2622103dec1SSimon Wunderlich default: 2632103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 2642103dec1SSimon Wunderlich break; 2652103dec1SSimon Wunderlich } 2662103dec1SSimon Wunderlich } 2672103dec1SSimon Wunderlich 2684d36ec58SJohannes Berg /* return false if no more work */ 2694d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 2704d36ec58SJohannes Berg { 2716ea0a69cSJohannes Berg struct cfg80211_scan_request *req; 2722103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 273c56ef672SDavid Spinadel u8 bands_used = 0; 2744d36ec58SJohannes Berg int i, ielen, n_chans; 2754d36ec58SJohannes Berg 2766ea0a69cSJohannes Berg req = rcu_dereference_protected(local->scan_req, 2776ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 2786ea0a69cSJohannes Berg 279a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 280a754055aSEmmanuel Grumbach return false; 281a754055aSEmmanuel Grumbach 28230686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 283c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 284c56ef672SDavid Spinadel local->hw_scan_req->req.channels[i] = req->channels[i]; 285c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 286c56ef672SDavid Spinadel } 287c56ef672SDavid Spinadel 288c56ef672SDavid Spinadel n_chans = req->n_channels; 289c56ef672SDavid Spinadel } else { 2904d36ec58SJohannes Berg do { 29157fbcce3SJohannes Berg if (local->hw_scan_band == NUM_NL80211_BANDS) 2924d36ec58SJohannes Berg return false; 2934d36ec58SJohannes Berg 2944d36ec58SJohannes Berg n_chans = 0; 295c56ef672SDavid Spinadel 2964d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 297c56ef672SDavid Spinadel if (req->channels[i]->band != 298c56ef672SDavid Spinadel local->hw_scan_band) 299c56ef672SDavid Spinadel continue; 300c56ef672SDavid Spinadel local->hw_scan_req->req.channels[n_chans] = 3014d36ec58SJohannes Berg req->channels[i]; 3024d36ec58SJohannes Berg n_chans++; 303c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 3044d36ec58SJohannes Berg } 3054d36ec58SJohannes Berg 3064d36ec58SJohannes Berg local->hw_scan_band++; 3074d36ec58SJohannes Berg } while (!n_chans); 308c56ef672SDavid Spinadel } 3094d36ec58SJohannes Berg 310c56ef672SDavid Spinadel local->hw_scan_req->req.n_channels = n_chans; 3112103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 3124d36ec58SJohannes Berg 313c56ef672SDavid Spinadel ielen = ieee80211_build_preq_ies(local, 314c56ef672SDavid Spinadel (u8 *)local->hw_scan_req->req.ie, 315c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, 316c56ef672SDavid Spinadel &local->hw_scan_req->ies, 317c56ef672SDavid Spinadel req->ie, req->ie_len, 318c56ef672SDavid Spinadel bands_used, req->rates, &chandef); 319c56ef672SDavid Spinadel local->hw_scan_req->req.ie_len = ielen; 320c56ef672SDavid Spinadel local->hw_scan_req->req.no_cck = req->no_cck; 321a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr); 322a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr_mask, 323a344d677SJohannes Berg req->mac_addr_mask); 324e345f44fSJouni Malinen ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid); 3254d36ec58SJohannes Berg 3264d36ec58SJohannes Berg return true; 3274d36ec58SJohannes Berg } 3284d36ec58SJohannes Berg 3298bd2a248SEliad Peller static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 330f3b85252SJohannes Berg { 331f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3328bd2a248SEliad Peller bool hw_scan = local->ops->hw_scan; 333a2b70e83SEliad Peller bool was_scanning = local->scanning; 3346ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 335a344d677SJohannes Berg struct ieee80211_sub_if_data *scan_sdata; 3364fa11ec7SSachin Kulkarni struct ieee80211_sub_if_data *sdata; 337f3b85252SJohannes Berg 338e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 339f3b85252SJohannes Berg 3406d3560d4SJohannes Berg /* 3416d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 3426d3560d4SJohannes Berg * we have one at all will be verified by checking 3436d3560d4SJohannes Berg * local->scan_req next), but not to complete it 3446d3560d4SJohannes Berg * successfully. 3456d3560d4SJohannes Berg */ 3466d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 3476d3560d4SJohannes Berg aborted = true; 348f3b85252SJohannes Berg 349e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 350d07bfd8bSJohannes Berg return; 351f3b85252SJohannes Berg 352c56ef672SDavid Spinadel if (hw_scan && !aborted && 35330686bf7SJohannes Berg !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) && 354c56ef672SDavid Spinadel ieee80211_prep_hw_scan(local)) { 355e2fd5dbcSJohannes Berg int rc; 356e2fd5dbcSJohannes Berg 357e2fd5dbcSJohannes Berg rc = drv_hw_scan(local, 358e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 359e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)), 360e2fd5dbcSJohannes Berg local->hw_scan_req); 361e2fd5dbcSJohannes Berg 3626eb11a9aSStanislaw Gruszka if (rc == 0) 363d07bfd8bSJohannes Berg return; 3647947d3e0SAvraham Stern 365*7d10f6b1SJohannes Berg /* HW scan failed and is going to be reported as aborted, 366*7d10f6b1SJohannes Berg * so clear old scan info. 3677947d3e0SAvraham Stern */ 3687947d3e0SAvraham Stern memset(&local->scan_info, 0, sizeof(local->scan_info)); 369*7d10f6b1SJohannes Berg aborted = true; 3704d36ec58SJohannes Berg } 3714d36ec58SJohannes Berg 3724d36ec58SJohannes Berg kfree(local->hw_scan_req); 3734d36ec58SJohannes Berg local->hw_scan_req = NULL; 374f3b85252SJohannes Berg 3756ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 3766ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 3776ea0a69cSJohannes Berg 3781d76250bSAvraham Stern if (scan_req != local->int_scan_req) { 3797947d3e0SAvraham Stern local->scan_info.aborted = aborted; 3807947d3e0SAvraham Stern cfg80211_scan_done(scan_req, &local->scan_info); 3811d76250bSAvraham Stern } 3826ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 383a344d677SJohannes Berg 384a344d677SJohannes Berg scan_sdata = rcu_dereference_protected(local->scan_sdata, 385a344d677SJohannes Berg lockdep_is_held(&local->mtx)); 3860c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 3872a519311SJohannes Berg 388fbe9c429SHelmut Schaa local->scanning = 0; 3897ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 390f3b85252SJohannes Berg 39159bdf3b0SBen Greear /* Set power back to normal operating levels. */ 39259bdf3b0SBen Greear ieee80211_hw_config(local, 0); 393b23b025fSBen Greear 3948bd2a248SEliad Peller if (!hw_scan) { 395b23b025fSBen Greear ieee80211_configure_filter(local); 396a344d677SJohannes Berg drv_sw_scan_complete(local, scan_sdata); 397aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 398b23b025fSBen Greear } 399b23b025fSBen Greear 4005cff20e6SJohannes Berg ieee80211_recalc_idle(local); 401e229f844SStanislaw Gruszka 4020a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 40346900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 4044fa11ec7SSachin Kulkarni 4054fa11ec7SSachin Kulkarni /* Requeue all the work that might have been ignored while 4064fa11ec7SSachin Kulkarni * the scan was in progress; if there was none this will 4074fa11ec7SSachin Kulkarni * just be a no-op for the particular interface. 4084fa11ec7SSachin Kulkarni */ 4094fa11ec7SSachin Kulkarni list_for_each_entry_rcu(sdata, &local->interfaces, list) { 4104fa11ec7SSachin Kulkarni if (ieee80211_sdata_running(sdata)) 4114fa11ec7SSachin Kulkarni ieee80211_queue_work(&sdata->local->hw, &sdata->work); 4124fa11ec7SSachin Kulkarni } 4134fa11ec7SSachin Kulkarni 414a2b70e83SEliad Peller if (was_scanning) 4152eb278e0SJohannes Berg ieee80211_start_next_roc(local); 4160a51b27eSJohannes Berg } 4178789d459SJohannes Berg 4187947d3e0SAvraham Stern void ieee80211_scan_completed(struct ieee80211_hw *hw, 4197947d3e0SAvraham Stern struct cfg80211_scan_info *info) 4208789d459SJohannes Berg { 4218789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 4228789d459SJohannes Berg 4237947d3e0SAvraham Stern trace_api_scan_completed(local, info); 4248789d459SJohannes Berg 4258789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 4267947d3e0SAvraham Stern if (info->aborted) 4278789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 4287947d3e0SAvraham Stern 4297947d3e0SAvraham Stern memcpy(&local->scan_info, info, sizeof(*info)); 4307947d3e0SAvraham Stern 4318789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 4328789d459SJohannes Berg } 4330a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 4340a51b27eSJohannes Berg 435a344d677SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local, 436a344d677SJohannes Berg struct ieee80211_sub_if_data *sdata) 437f3b85252SJohannes Berg { 438fe57d9f5SJohannes Berg /* Software scan is not supported in multi-channel cases */ 439fe57d9f5SJohannes Berg if (local->use_chanctx) 440fe57d9f5SJohannes Berg return -EOPNOTSUPP; 441fe57d9f5SJohannes Berg 442f3b85252SJohannes Berg /* 443f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 444f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 445f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 446f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 447f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 448f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 449f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 450f3b85252SJohannes Berg * 451f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 452f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 453f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 454f3b85252SJohannes Berg */ 455a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 456f3b85252SJohannes Berg 457de312db3SRajkumar Manoharan local->leave_oper_channel_time = jiffies; 458977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 459f3b85252SJohannes Berg local->scan_channel_idx = 0; 460f3b85252SJohannes Berg 461aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 462a80f7c0bSJohannes Berg 4639c35d7d2SSeth Forshee /* ensure nullfunc is transmitted before leaving operating channel */ 4643b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 4659c35d7d2SSeth Forshee 4663ac64beeSJohannes Berg ieee80211_configure_filter(local); 467f3b85252SJohannes Berg 46859bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 46959bdf3b0SBen Greear ieee80211_hw_config(local, 0); 47059bdf3b0SBen Greear 47142935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 47207ef03eeSJohannes Berg &local->scan_work, 0); 473f3b85252SJohannes Berg 474f3b85252SJohannes Berg return 0; 475f3b85252SJohannes Berg } 476f3b85252SJohannes Berg 477133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local, 478133d40f9SStanislaw Gruszka struct ieee80211_sub_if_data *sdata) 479133d40f9SStanislaw Gruszka { 4805cbc95a7SEliad Peller if (ieee80211_is_radar_required(local)) 481164eb02dSSimon Wunderlich return false; 482164eb02dSSimon Wunderlich 4832eb278e0SJohannes Berg if (!list_empty(&local->roc_list)) 484133d40f9SStanislaw Gruszka return false; 485133d40f9SStanislaw Gruszka 486133d40f9SStanislaw Gruszka if (sdata->vif.type == NL80211_IFTYPE_STATION && 487392b9ffbSStanislaw Gruszka sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 488133d40f9SStanislaw Gruszka return false; 489133d40f9SStanislaw Gruszka 490133d40f9SStanislaw Gruszka return true; 491133d40f9SStanislaw Gruszka } 492133d40f9SStanislaw Gruszka 493133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local) 494133d40f9SStanislaw Gruszka { 495133d40f9SStanislaw Gruszka lockdep_assert_held(&local->mtx); 496133d40f9SStanislaw Gruszka 497133d40f9SStanislaw Gruszka if (!local->scan_req || local->scanning) 498133d40f9SStanislaw Gruszka return; 499133d40f9SStanislaw Gruszka 500e2fd5dbcSJohannes Berg if (!ieee80211_can_scan(local, 501e2fd5dbcSJohannes Berg rcu_dereference_protected( 502e2fd5dbcSJohannes Berg local->scan_sdata, 503e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)))) 504133d40f9SStanislaw Gruszka return; 505133d40f9SStanislaw Gruszka 506133d40f9SStanislaw Gruszka ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 507133d40f9SStanislaw Gruszka round_jiffies_relative(0)); 508133d40f9SStanislaw Gruszka } 509f3b85252SJohannes Berg 5108a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 5118a690674SBen Greear unsigned long *next_delay) 5128a690674SBen Greear { 5138a690674SBen Greear int i; 514e2fd5dbcSJohannes Berg struct ieee80211_sub_if_data *sdata; 5156ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 51657fbcce3SJohannes Berg enum nl80211_band band = local->hw.conf.chandef.chan->band; 5176c17b77bSSeth Forshee u32 tx_flags; 5186c17b77bSSeth Forshee 5196ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 5206ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 5216ea0a69cSJohannes Berg 5226c17b77bSSeth Forshee tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 5236ea0a69cSJohannes Berg if (scan_req->no_cck) 5246c17b77bSSeth Forshee tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 5258a690674SBen Greear 526e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 527316b6b5dSPeter Senna Tschudin lockdep_is_held(&local->mtx)); 528e2fd5dbcSJohannes Berg 5296ea0a69cSJohannes Berg for (i = 0; i < scan_req->n_ssids; i++) 5308a690674SBen Greear ieee80211_send_probe_req( 531e345f44fSJouni Malinen sdata, local->scan_addr, scan_req->bssid, 5326ea0a69cSJohannes Berg scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len, 5336ea0a69cSJohannes Berg scan_req->ie, scan_req->ie_len, 5346ea0a69cSJohannes Berg scan_req->rates[band], false, 535675a0b04SKarl Beldan tx_flags, local->hw.conf.chandef.chan, true); 5368a690674SBen Greear 5378a690674SBen Greear /* 5388a690674SBen Greear * After sending probe requests, wait for probe responses 5398a690674SBen Greear * on the channel. 5408a690674SBen Greear */ 5418a690674SBen Greear *next_delay = IEEE80211_CHANNEL_TIME; 5428a690674SBen Greear local->next_scan_state = SCAN_DECISION; 5438a690674SBen Greear } 5448a690674SBen Greear 545f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 546f3b85252SJohannes Berg struct cfg80211_scan_request *req) 547f3b85252SJohannes Berg { 548f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 549f3b85252SJohannes Berg int rc; 550f3b85252SJohannes Berg 551e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 552e229f844SStanislaw Gruszka 5532726f23dSEliad Peller if (local->scan_req || ieee80211_is_radar_required(local)) 554f3b85252SJohannes Berg return -EBUSY; 555f3b85252SJohannes Berg 556133d40f9SStanislaw Gruszka if (!ieee80211_can_scan(local, sdata)) { 5576e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 5586ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 559e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 560c0ce77b8SJohannes Berg return 0; 561c0ce77b8SJohannes Berg } 562c0ce77b8SJohannes Berg 563f3b85252SJohannes Berg if (local->ops->hw_scan) { 564f3b85252SJohannes Berg u8 *ies; 565f3b85252SJohannes Berg 566e4dcbb37SDavid Spinadel local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len; 567c56ef672SDavid Spinadel 56830686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 569c56ef672SDavid Spinadel int i, n_bands = 0; 570c56ef672SDavid Spinadel u8 bands_counted = 0; 571c56ef672SDavid Spinadel 572c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 573c56ef672SDavid Spinadel if (bands_counted & BIT(req->channels[i]->band)) 574c56ef672SDavid Spinadel continue; 575c56ef672SDavid Spinadel bands_counted |= BIT(req->channels[i]->band); 576c56ef672SDavid Spinadel n_bands++; 577c56ef672SDavid Spinadel } 578c56ef672SDavid Spinadel 579c56ef672SDavid Spinadel local->hw_scan_ies_bufsize *= n_bands; 580c56ef672SDavid Spinadel } 581c56ef672SDavid Spinadel 5824d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 5834d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 5844d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 585c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, GFP_KERNEL); 5864d36ec58SJohannes Berg if (!local->hw_scan_req) 587f3b85252SJohannes Berg return -ENOMEM; 588f3b85252SJohannes Berg 589c56ef672SDavid Spinadel local->hw_scan_req->req.ssids = req->ssids; 590c56ef672SDavid Spinadel local->hw_scan_req->req.n_ssids = req->n_ssids; 5914d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 5924d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 5934d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 594c56ef672SDavid Spinadel local->hw_scan_req->req.ie = ies; 595c56ef672SDavid Spinadel local->hw_scan_req->req.flags = req->flags; 596e345f44fSJouni Malinen eth_broadcast_addr(local->hw_scan_req->req.bssid); 5977947d3e0SAvraham Stern local->hw_scan_req->req.duration = req->duration; 5987947d3e0SAvraham Stern local->hw_scan_req->req.duration_mandatory = 5997947d3e0SAvraham Stern req->duration_mandatory; 6004d36ec58SJohannes Berg 6014d36ec58SJohannes Berg local->hw_scan_band = 0; 6026e7e6213SJohn W. Linville 6036e7e6213SJohn W. Linville /* 6046e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 6056e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 6066e7e6213SJohn W. Linville * anything that might be changed here and leave 6076e7e6213SJohn W. Linville * this function early must not go after this 6086e7e6213SJohn W. Linville * allocation. 6096e7e6213SJohn W. Linville */ 610f3b85252SJohannes Berg } 611f3b85252SJohannes Berg 6126ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 613e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 614f3b85252SJohannes Berg 615a344d677SJohannes Berg if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 616a344d677SJohannes Berg get_random_mask_addr(local->scan_addr, 617a344d677SJohannes Berg req->mac_addr, 618a344d677SJohannes Berg req->mac_addr_mask); 619a344d677SJohannes Berg else 620a344d677SJohannes Berg memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN); 621a344d677SJohannes Berg 6228a690674SBen Greear if (local->ops->hw_scan) { 623fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 6248a690674SBen Greear } else if ((req->n_channels == 1) && 625675a0b04SKarl Beldan (req->channels[0] == local->_oper_chandef.chan)) { 6269b864870SJohannes Berg /* 6279b864870SJohannes Berg * If we are scanning only on the operating channel 6289b864870SJohannes Berg * then we do not need to stop normal activities 6298a690674SBen Greear */ 6308a690674SBen Greear unsigned long next_delay; 6318a690674SBen Greear 6328a690674SBen Greear __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 6338a690674SBen Greear 6348a690674SBen Greear ieee80211_recalc_idle(local); 6358a690674SBen Greear 6368a690674SBen Greear /* Notify driver scan is starting, keep order of operations 6378a690674SBen Greear * same as normal software scan, in case that matters. */ 638a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 6398a690674SBen Greear 6408a690674SBen Greear ieee80211_configure_filter(local); /* accept probe-responses */ 6418a690674SBen Greear 6428a690674SBen Greear /* We need to ensure power level is at max for scanning. */ 6438a690674SBen Greear ieee80211_hw_config(local, 0); 6448a690674SBen Greear 6454e39ccacSAntonio Quartulli if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR | 6464e39ccacSAntonio Quartulli IEEE80211_CHAN_RADAR)) || 6476ea0a69cSJohannes Berg !req->n_ssids) { 6488a690674SBen Greear next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 6498a690674SBen Greear } else { 6508a690674SBen Greear ieee80211_scan_state_send_probe(local, &next_delay); 6518a690674SBen Greear next_delay = IEEE80211_CHANNEL_TIME; 6528a690674SBen Greear } 6538a690674SBen Greear 6548a690674SBen Greear /* Now, just wait a bit and we are all done! */ 6558a690674SBen Greear ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 6568a690674SBen Greear next_delay); 6578a690674SBen Greear return 0; 6588a690674SBen Greear } else { 6598a690674SBen Greear /* Do normal software scan */ 660fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 6618a690674SBen Greear } 6626e7e6213SJohn W. Linville 6635cff20e6SJohannes Berg ieee80211_recalc_idle(local); 664f3b85252SJohannes Berg 6654d36ec58SJohannes Berg if (local->ops->hw_scan) { 6664d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 667a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 668a344d677SJohannes Berg } else { 669a344d677SJohannes Berg rc = ieee80211_start_sw_scan(local, sdata); 670a344d677SJohannes Berg } 671f3b85252SJohannes Berg 672f3b85252SJohannes Berg if (rc) { 6734d36ec58SJohannes Berg kfree(local->hw_scan_req); 6744d36ec58SJohannes Berg local->hw_scan_req = NULL; 675fbe9c429SHelmut Schaa local->scanning = 0; 676f3b85252SJohannes Berg 6775cff20e6SJohannes Berg ieee80211_recalc_idle(local); 6785cff20e6SJohannes Berg 679f3b85252SJohannes Berg local->scan_req = NULL; 6800c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 681f3b85252SJohannes Berg } 682f3b85252SJohannes Berg 683f3b85252SJohannes Berg return rc; 684f3b85252SJohannes Berg } 685f3b85252SJohannes Berg 686df13cce5SHelmut Schaa static unsigned long 687df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 688df13cce5SHelmut Schaa { 689df13cce5SHelmut Schaa /* 690df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 691df13cce5SHelmut Schaa * add that delay as well to get a better estimation 692df13cce5SHelmut Schaa */ 6934e39ccacSAntonio Quartulli if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) 694df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 695df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 696df13cce5SHelmut Schaa } 697df13cce5SHelmut Schaa 698e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 6992fb3f028SHelmut Schaa unsigned long *next_delay) 7002fb3f028SHelmut Schaa { 701142b9f50SHelmut Schaa bool associated = false; 702df13cce5SHelmut Schaa bool tx_empty = true; 703df13cce5SHelmut Schaa bool bad_latency; 704142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 705df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 706cd2bb512SSam Leffler enum mac80211_scan_state next_scan_state; 7076ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 708142b9f50SHelmut Schaa 709df13cce5SHelmut Schaa /* 710df13cce5SHelmut Schaa * check if at least one STA interface is associated, 711df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 712df13cce5SHelmut Schaa * and grab the lowest used beacon interval 713df13cce5SHelmut Schaa */ 714142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 715142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 7169607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 717142b9f50SHelmut Schaa continue; 718142b9f50SHelmut Schaa 719142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 720142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 721142b9f50SHelmut Schaa associated = true; 722df13cce5SHelmut Schaa 723df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 724df13cce5SHelmut Schaa tx_empty = false; 725142b9f50SHelmut Schaa break; 726142b9f50SHelmut Schaa } 727142b9f50SHelmut Schaa } 728142b9f50SHelmut Schaa } 729df13cce5SHelmut Schaa } 730142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 731142b9f50SHelmut Schaa 7326ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 7336ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 7346ea0a69cSJohannes Berg 7356ea0a69cSJohannes Berg next_chan = scan_req->channels[local->scan_channel_idx]; 736b23b025fSBen Greear 737142b9f50SHelmut Schaa /* 738142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 739df13cce5SHelmut Schaa * see if we can scan another channel without interfering 740df13cce5SHelmut Schaa * with the current traffic situation. 741df13cce5SHelmut Schaa * 7423f892b61SStanislaw Gruszka * Keep good latency, do not stay off-channel more than 125 ms. 743142b9f50SHelmut Schaa */ 744df13cce5SHelmut Schaa 745df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 746df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 7473f892b61SStanislaw Gruszka local->leave_oper_channel_time + HZ / 8); 748df13cce5SHelmut Schaa 749cd2bb512SSam Leffler if (associated && !tx_empty) { 7506ea0a69cSJohannes Berg if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 751cd2bb512SSam Leffler next_scan_state = SCAN_ABORT; 752142b9f50SHelmut Schaa else 753cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 7543f892b61SStanislaw Gruszka } else if (associated && bad_latency) { 755cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 756cd2bb512SSam Leffler } else { 757cd2bb512SSam Leffler next_scan_state = SCAN_SET_CHANNEL; 758cd2bb512SSam Leffler } 759cd2bb512SSam Leffler 760cd2bb512SSam Leffler local->next_scan_state = next_scan_state; 761142b9f50SHelmut Schaa 762142b9f50SHelmut Schaa *next_delay = 0; 7632fb3f028SHelmut Schaa } 7642fb3f028SHelmut Schaa 7652fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 7667d3be3ccSHelmut Schaa unsigned long *next_delay) 7677d3be3ccSHelmut Schaa { 7687d3be3ccSHelmut Schaa int skip; 7697d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 7707ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width oper_scan_width; 7716ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 7726ea0a69cSJohannes Berg 7736ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 7746ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 7757d3be3ccSHelmut Schaa 7767d3be3ccSHelmut Schaa skip = 0; 7776ea0a69cSJohannes Berg chan = scan_req->channels[local->scan_channel_idx]; 7787d3be3ccSHelmut Schaa 7797ca15a0aSSimon Wunderlich local->scan_chandef.chan = chan; 7807ca15a0aSSimon Wunderlich local->scan_chandef.center_freq1 = chan->center_freq; 7817ca15a0aSSimon Wunderlich local->scan_chandef.center_freq2 = 0; 7826ea0a69cSJohannes Berg switch (scan_req->scan_width) { 7837ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 7847ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 7857ca15a0aSSimon Wunderlich break; 7867ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 7877ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 7887ca15a0aSSimon Wunderlich break; 7897ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_20: 7907ca15a0aSSimon Wunderlich /* If scanning on oper channel, use whatever channel-type 7917ca15a0aSSimon Wunderlich * is currently in use. 7927ca15a0aSSimon Wunderlich */ 7937ca15a0aSSimon Wunderlich oper_scan_width = cfg80211_chandef_to_scan_width( 7947ca15a0aSSimon Wunderlich &local->_oper_chandef); 7957ca15a0aSSimon Wunderlich if (chan == local->_oper_chandef.chan && 7966ea0a69cSJohannes Berg oper_scan_width == scan_req->scan_width) 7977ca15a0aSSimon Wunderlich local->scan_chandef = local->_oper_chandef; 7987ca15a0aSSimon Wunderlich else 7997ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 8007ca15a0aSSimon Wunderlich break; 8017ca15a0aSSimon Wunderlich } 802b23b025fSBen Greear 803584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 8047d3be3ccSHelmut Schaa skip = 1; 8057d3be3ccSHelmut Schaa 8067d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 8077d3be3ccSHelmut Schaa local->scan_channel_idx++; 8087d3be3ccSHelmut Schaa 8090ee9c13cSHelmut Schaa if (skip) { 8100ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 8110ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 8122fb3f028SHelmut Schaa return; 8130ee9c13cSHelmut Schaa } 8147d3be3ccSHelmut Schaa 8157d3be3ccSHelmut Schaa /* 8167d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 8177d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 8187d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 8197d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 8207d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 8217d3be3ccSHelmut Schaa * received. 8227d3be3ccSHelmut Schaa * 8237d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 8247d3be3ccSHelmut Schaa */ 8254e39ccacSAntonio Quartulli if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) || 8264e39ccacSAntonio Quartulli !scan_req->n_ssids) { 8277d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 828977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 8292fb3f028SHelmut Schaa return; 8307d3be3ccSHelmut Schaa } 8317d3be3ccSHelmut Schaa 8322fb3f028SHelmut Schaa /* active scan, send probes */ 8337d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 834977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 8357d3be3ccSHelmut Schaa } 8367d3be3ccSHelmut Schaa 83707ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 83807ef03eeSJohannes Berg unsigned long *next_delay) 83907ef03eeSJohannes Berg { 84007ef03eeSJohannes Berg /* switch back to the operating channel */ 8417ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 84207ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 84307ef03eeSJohannes Berg 844aacde9eeSStanislaw Gruszka /* disable PS */ 845aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 84607ef03eeSJohannes Berg 84707ef03eeSJohannes Berg *next_delay = HZ / 5; 84807ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 84907ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 85007ef03eeSJohannes Berg } 85107ef03eeSJohannes Berg 85207ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 85307ef03eeSJohannes Berg unsigned long *next_delay) 85407ef03eeSJohannes Berg { 855aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 85607ef03eeSJohannes Berg 85707ef03eeSJohannes Berg if (local->ops->flush) { 8583b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 85907ef03eeSJohannes Berg *next_delay = 0; 86007ef03eeSJohannes Berg } else 86107ef03eeSJohannes Berg *next_delay = HZ / 10; 86207ef03eeSJohannes Berg 86307ef03eeSJohannes Berg /* remember when we left the operating channel */ 86407ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 86507ef03eeSJohannes Berg 86607ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 867de2ee84dSMohammed Shafi Shajakhan local->next_scan_state = SCAN_SET_CHANNEL; 86807ef03eeSJohannes Berg } 86907ef03eeSJohannes Berg 870c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 8710a51b27eSJohannes Berg { 8720a51b27eSJohannes Berg struct ieee80211_local *local = 8730a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 874d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 8756ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 8760a51b27eSJohannes Berg unsigned long next_delay = 0; 8778bd2a248SEliad Peller bool aborted; 8788789d459SJohannes Berg 879259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 880259b62e3SStanislaw Gruszka 881332ff7feSLuciano Coelho if (!ieee80211_can_run_worker(local)) { 882332ff7feSLuciano Coelho aborted = true; 883332ff7feSLuciano Coelho goto out_complete; 884332ff7feSLuciano Coelho } 885332ff7feSLuciano Coelho 886e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 887e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)); 8886ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 8896ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 890d07bfd8bSJohannes Berg 8918a690674SBen Greear /* When scanning on-channel, the first-callback means completed. */ 8928a690674SBen Greear if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 8938a690674SBen Greear aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 8948a690674SBen Greear goto out_complete; 8958a690674SBen Greear } 8968a690674SBen Greear 897259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 8988789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 899259b62e3SStanislaw Gruszka goto out_complete; 9008789d459SJohannes Berg } 9018789d459SJohannes Berg 9026ea0a69cSJohannes Berg if (!sdata || !scan_req) 903259b62e3SStanislaw Gruszka goto out; 904f3b85252SJohannes Berg 905ff5db439SEliad Peller if (!local->scanning) { 906f3b85252SJohannes Berg int rc; 907f3b85252SJohannes Berg 9086ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 9090c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 910f3b85252SJohannes Berg 9116ea0a69cSJohannes Berg rc = __ieee80211_start_scan(sdata, scan_req); 912259b62e3SStanislaw Gruszka if (rc) { 9133aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 9146ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, scan_req); 915259b62e3SStanislaw Gruszka aborted = true; 916259b62e3SStanislaw Gruszka goto out_complete; 917259b62e3SStanislaw Gruszka } else 918259b62e3SStanislaw Gruszka goto out; 919f3b85252SJohannes Berg } 920f3b85252SJohannes Berg 9215bc75728SJohannes Berg /* 922f502d09bSHelmut Schaa * as long as no delay is required advance immediately 923f502d09bSHelmut Schaa * without scheduling a new work 924f502d09bSHelmut Schaa */ 925f502d09bSHelmut Schaa do { 926c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 927c29acf20SRajkumar Manoharan aborted = true; 928c29acf20SRajkumar Manoharan goto out_complete; 929c29acf20SRajkumar Manoharan } 930c29acf20SRajkumar Manoharan 931977923b0SHelmut Schaa switch (local->next_scan_state) { 9322fb3f028SHelmut Schaa case SCAN_DECISION: 933e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 9346ea0a69cSJohannes Berg if (local->scan_channel_idx >= scan_req->n_channels) { 935e229f844SStanislaw Gruszka aborted = false; 936e229f844SStanislaw Gruszka goto out_complete; 937e229f844SStanislaw Gruszka } 938e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 9390a51b27eSJohannes Berg break; 9402fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 9412fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 9422fb3f028SHelmut Schaa break; 9430a51b27eSJohannes Berg case SCAN_SEND_PROBE: 9447d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 9450a51b27eSJohannes Berg break; 94607ef03eeSJohannes Berg case SCAN_SUSPEND: 94707ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 948142b9f50SHelmut Schaa break; 94907ef03eeSJohannes Berg case SCAN_RESUME: 95007ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 951142b9f50SHelmut Schaa break; 952cd2bb512SSam Leffler case SCAN_ABORT: 953cd2bb512SSam Leffler aborted = true; 954cd2bb512SSam Leffler goto out_complete; 9550a51b27eSJohannes Berg } 956f502d09bSHelmut Schaa } while (next_delay == 0); 9570a51b27eSJohannes Berg 95842935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 959d07bfd8bSJohannes Berg goto out; 960259b62e3SStanislaw Gruszka 961259b62e3SStanislaw Gruszka out_complete: 9628bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, aborted); 963259b62e3SStanislaw Gruszka out: 964259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 9650a51b27eSJohannes Berg } 9660a51b27eSJohannes Berg 967c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 9682a519311SJohannes Berg struct cfg80211_scan_request *req) 9690a51b27eSJohannes Berg { 970f3b85252SJohannes Berg int res; 9710a51b27eSJohannes Berg 972a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 973f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 974a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 9752a519311SJohannes Berg 976f3b85252SJohannes Berg return res; 9770a51b27eSJohannes Berg } 9780a51b27eSJohannes Berg 97934bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 980be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 98176bed0f4SJanusz.Dziedzic@tieto.com struct ieee80211_channel **channels, 98276bed0f4SJanusz.Dziedzic@tieto.com unsigned int n_channels, 9837ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width) 984f3b85252SJohannes Berg { 985f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 98676bed0f4SJanusz.Dziedzic@tieto.com int ret = -EBUSY, i, n_ch = 0; 98757fbcce3SJohannes Berg enum nl80211_band band; 9889116dd01SJohannes Berg 989a1699b75SJohannes Berg mutex_lock(&local->mtx); 990f3b85252SJohannes Berg 991f3b85252SJohannes Berg /* busy scanning */ 992f3b85252SJohannes Berg if (local->scan_req) 993f3b85252SJohannes Berg goto unlock; 994f3b85252SJohannes Berg 995be4a4b6aSJohannes Berg /* fill internal scan request */ 99676bed0f4SJanusz.Dziedzic@tieto.com if (!channels) { 99776bed0f4SJanusz.Dziedzic@tieto.com int max_n; 998be4a4b6aSJohannes Berg 99957fbcce3SJohannes Berg for (band = 0; band < NUM_NL80211_BANDS; band++) { 1000be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 1001be4a4b6aSJohannes Berg continue; 100234bcf715SStanislaw Gruszka 100334bcf715SStanislaw Gruszka max_n = local->hw.wiphy->bands[band]->n_channels; 100434bcf715SStanislaw Gruszka for (i = 0; i < max_n; i++) { 100534bcf715SStanislaw Gruszka struct ieee80211_channel *tmp_ch = 1006be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 100734bcf715SStanislaw Gruszka 10088fe02e16SLuis R. Rodriguez if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 100934bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED)) 101034bcf715SStanislaw Gruszka continue; 101134bcf715SStanislaw Gruszka 101234bcf715SStanislaw Gruszka local->int_scan_req->channels[n_ch] = tmp_ch; 101334bcf715SStanislaw Gruszka n_ch++; 1014be4a4b6aSJohannes Berg } 1015be4a4b6aSJohannes Berg } 1016be4a4b6aSJohannes Berg 101734bcf715SStanislaw Gruszka if (WARN_ON_ONCE(n_ch == 0)) 101834bcf715SStanislaw Gruszka goto unlock; 101934bcf715SStanislaw Gruszka 102034bcf715SStanislaw Gruszka local->int_scan_req->n_channels = n_ch; 1021be4a4b6aSJohannes Berg } else { 102276bed0f4SJanusz.Dziedzic@tieto.com for (i = 0; i < n_channels; i++) { 102376bed0f4SJanusz.Dziedzic@tieto.com if (channels[i]->flags & (IEEE80211_CHAN_NO_IR | 102476bed0f4SJanusz.Dziedzic@tieto.com IEEE80211_CHAN_DISABLED)) 102576bed0f4SJanusz.Dziedzic@tieto.com continue; 102676bed0f4SJanusz.Dziedzic@tieto.com 102776bed0f4SJanusz.Dziedzic@tieto.com local->int_scan_req->channels[n_ch] = channels[i]; 102876bed0f4SJanusz.Dziedzic@tieto.com n_ch++; 102976bed0f4SJanusz.Dziedzic@tieto.com } 103076bed0f4SJanusz.Dziedzic@tieto.com 103176bed0f4SJanusz.Dziedzic@tieto.com if (WARN_ON_ONCE(n_ch == 0)) 103234bcf715SStanislaw Gruszka goto unlock; 103334bcf715SStanislaw Gruszka 103476bed0f4SJanusz.Dziedzic@tieto.com local->int_scan_req->n_channels = n_ch; 1035be4a4b6aSJohannes Berg } 1036be4a4b6aSJohannes Berg 1037be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 1038be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 10397ca15a0aSSimon Wunderlich local->int_scan_req->scan_width = scan_width; 10405ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 10415ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 1042f3b85252SJohannes Berg 10435ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 1044f3b85252SJohannes Berg unlock: 1045a1699b75SJohannes Berg mutex_unlock(&local->mtx); 1046f3b85252SJohannes Berg return ret; 10470a51b27eSJohannes Berg } 10485bb644a0SJohannes Berg 10494136c422SStanislaw Gruszka /* 10504136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 10514136c422SStanislaw Gruszka */ 10525bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 10535bb644a0SJohannes Berg { 10545bb644a0SJohannes Berg /* 1055b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 10564136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 10574136c422SStanislaw Gruszka * 10584136c422SStanislaw Gruszka * Regarding hardware scan: 10594136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 10604136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 10614136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 10624136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 10634136c422SStanislaw Gruszka * 10644136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 10654136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 10664136c422SStanislaw Gruszka * 1067b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 1068b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 1069b856439bSEliad Peller * after the scan was completed/aborted. 10705bb644a0SJohannes Berg */ 10714136c422SStanislaw Gruszka 1072a1699b75SJohannes Berg mutex_lock(&local->mtx); 1073b856439bSEliad Peller if (!local->scan_req) 1074b856439bSEliad Peller goto out; 1075b856439bSEliad Peller 1076a754055aSEmmanuel Grumbach /* 1077a754055aSEmmanuel Grumbach * We have a scan running and the driver already reported completion, 1078a754055aSEmmanuel Grumbach * but the worker hasn't run yet or is stuck on the mutex - mark it as 1079a754055aSEmmanuel Grumbach * cancelled. 1080a754055aSEmmanuel Grumbach */ 1081a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 1082a754055aSEmmanuel Grumbach test_bit(SCAN_COMPLETED, &local->scanning)) { 1083a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1084a754055aSEmmanuel Grumbach goto out; 1085a754055aSEmmanuel Grumbach } 1086a754055aSEmmanuel Grumbach 1087b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 1088a754055aSEmmanuel Grumbach /* 1089a754055aSEmmanuel Grumbach * Make sure that __ieee80211_scan_completed doesn't trigger a 1090a754055aSEmmanuel Grumbach * scan on another band. 1091a754055aSEmmanuel Grumbach */ 1092a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1093b856439bSEliad Peller if (local->ops->cancel_hw_scan) 1094e2fd5dbcSJohannes Berg drv_cancel_hw_scan(local, 1095e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 1096e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx))); 1097b856439bSEliad Peller goto out; 1098b856439bSEliad Peller } 1099b856439bSEliad Peller 1100d07bfd8bSJohannes Berg /* 1101d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 1102d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 1103d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 1104d07bfd8bSJohannes Berg */ 1105d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 1106d07bfd8bSJohannes Berg /* and clean up */ 11077947d3e0SAvraham Stern memset(&local->scan_info, 0, sizeof(local->scan_info)); 11088bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, true); 1109b856439bSEliad Peller out: 1110d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 11115bb644a0SJohannes Berg } 111279f460caSLuciano Coelho 1113d43c6b6eSDavid Spinadel int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 111479f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 111579f460caSLuciano Coelho { 111679f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 1117633e2713SDavid Spinadel struct ieee80211_scan_ies sched_scan_ies = {}; 11182103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 1119633e2713SDavid Spinadel int ret, i, iebufsz, num_bands = 0; 112057fbcce3SJohannes Berg u32 rate_masks[NUM_NL80211_BANDS] = {}; 1121633e2713SDavid Spinadel u8 bands_used = 0; 1122633e2713SDavid Spinadel u8 *ie; 1123633e2713SDavid Spinadel size_t len; 1124c604b9f2SJohannes Berg 1125e4dcbb37SDavid Spinadel iebufsz = local->scan_ies_len + req->ie_len; 112679f460caSLuciano Coelho 1127d43c6b6eSDavid Spinadel lockdep_assert_held(&local->mtx); 112879f460caSLuciano Coelho 1129d43c6b6eSDavid Spinadel if (!local->ops->sched_scan_start) 1130d43c6b6eSDavid Spinadel return -ENOTSUPP; 113179f460caSLuciano Coelho 113257fbcce3SJohannes Berg for (i = 0; i < NUM_NL80211_BANDS; i++) { 1133633e2713SDavid Spinadel if (local->hw.wiphy->bands[i]) { 1134633e2713SDavid Spinadel bands_used |= BIT(i); 1135633e2713SDavid Spinadel rate_masks[i] = (u32) -1; 1136633e2713SDavid Spinadel num_bands++; 1137633e2713SDavid Spinadel } 1138633e2713SDavid Spinadel } 1139c56ef672SDavid Spinadel 1140633e2713SDavid Spinadel ie = kzalloc(num_bands * iebufsz, GFP_KERNEL); 1141633e2713SDavid Spinadel if (!ie) { 114279f460caSLuciano Coelho ret = -ENOMEM; 1143633e2713SDavid Spinadel goto out; 114479f460caSLuciano Coelho } 114579f460caSLuciano Coelho 11462103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 11472103dec1SSimon Wunderlich 1148633e2713SDavid Spinadel len = ieee80211_build_preq_ies(local, ie, num_bands * iebufsz, 1149633e2713SDavid Spinadel &sched_scan_ies, req->ie, 1150633e2713SDavid Spinadel req->ie_len, bands_used, 1151c56ef672SDavid Spinadel rate_masks, &chandef); 115279f460caSLuciano Coelho 115330dd3edfSJohannes Berg ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 1154d43c6b6eSDavid Spinadel if (ret == 0) { 11555260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, sdata); 11566ea0a69cSJohannes Berg rcu_assign_pointer(local->sched_scan_req, req); 1157d43c6b6eSDavid Spinadel } 115879f460caSLuciano Coelho 1159633e2713SDavid Spinadel kfree(ie); 1160d43c6b6eSDavid Spinadel 1161633e2713SDavid Spinadel out: 1162d43c6b6eSDavid Spinadel if (ret) { 1163d43c6b6eSDavid Spinadel /* Clean in case of failure after HW restart or upon resume. */ 11640c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 11656ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1166d43c6b6eSDavid Spinadel } 1167d43c6b6eSDavid Spinadel 1168d43c6b6eSDavid Spinadel return ret; 1169d43c6b6eSDavid Spinadel } 1170d43c6b6eSDavid Spinadel 1171d43c6b6eSDavid Spinadel int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1172d43c6b6eSDavid Spinadel struct cfg80211_sched_scan_request *req) 1173d43c6b6eSDavid Spinadel { 1174d43c6b6eSDavid Spinadel struct ieee80211_local *local = sdata->local; 1175d43c6b6eSDavid Spinadel int ret; 1176d43c6b6eSDavid Spinadel 1177d43c6b6eSDavid Spinadel mutex_lock(&local->mtx); 1178d43c6b6eSDavid Spinadel 1179d43c6b6eSDavid Spinadel if (rcu_access_pointer(local->sched_scan_sdata)) { 1180d43c6b6eSDavid Spinadel mutex_unlock(&local->mtx); 1181d43c6b6eSDavid Spinadel return -EBUSY; 1182d43c6b6eSDavid Spinadel } 1183d43c6b6eSDavid Spinadel 1184d43c6b6eSDavid Spinadel ret = __ieee80211_request_sched_scan_start(sdata, req); 1185d43c6b6eSDavid Spinadel 11865260a5b2SJohannes Berg mutex_unlock(&local->mtx); 118779f460caSLuciano Coelho return ret; 118879f460caSLuciano Coelho } 118979f460caSLuciano Coelho 11900d440ea2SEliad Peller int ieee80211_request_sched_scan_stop(struct ieee80211_local *local) 119179f460caSLuciano Coelho { 11920d440ea2SEliad Peller struct ieee80211_sub_if_data *sched_scan_sdata; 11930d440ea2SEliad Peller int ret = -ENOENT; 119479f460caSLuciano Coelho 11955260a5b2SJohannes Berg mutex_lock(&local->mtx); 119679f460caSLuciano Coelho 119779f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 119879f460caSLuciano Coelho ret = -ENOTSUPP; 119979f460caSLuciano Coelho goto out; 120079f460caSLuciano Coelho } 120179f460caSLuciano Coelho 1202d43c6b6eSDavid Spinadel /* We don't want to restart sched scan anymore. */ 12036ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1204d43c6b6eSDavid Spinadel 12050d440ea2SEliad Peller sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata, 12060d440ea2SEliad Peller lockdep_is_held(&local->mtx)); 12070d440ea2SEliad Peller if (sched_scan_sdata) { 12080d440ea2SEliad Peller ret = drv_sched_scan_stop(local, sched_scan_sdata); 120971228a1eSAlexander Bondar if (!ret) 1210ad053a96SAndreea-Cristina Bernat RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 121171228a1eSAlexander Bondar } 121279f460caSLuciano Coelho out: 12135260a5b2SJohannes Berg mutex_unlock(&local->mtx); 121479f460caSLuciano Coelho 121579f460caSLuciano Coelho return ret; 121679f460caSLuciano Coelho } 121779f460caSLuciano Coelho 121879f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 121979f460caSLuciano Coelho { 122079f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 122179f460caSLuciano Coelho 122279f460caSLuciano Coelho trace_api_sched_scan_results(local); 122379f460caSLuciano Coelho 122479f460caSLuciano Coelho cfg80211_sched_scan_results(hw->wiphy); 122579f460caSLuciano Coelho } 122679f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 122779f460caSLuciano Coelho 1228f6837ba8SJohannes Berg void ieee80211_sched_scan_end(struct ieee80211_local *local) 122985a9994aSLuciano Coelho { 123085a9994aSLuciano Coelho mutex_lock(&local->mtx); 123185a9994aSLuciano Coelho 12325260a5b2SJohannes Berg if (!rcu_access_pointer(local->sched_scan_sdata)) { 123385a9994aSLuciano Coelho mutex_unlock(&local->mtx); 123485a9994aSLuciano Coelho return; 123585a9994aSLuciano Coelho } 123685a9994aSLuciano Coelho 12370c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 123885a9994aSLuciano Coelho 1239d43c6b6eSDavid Spinadel /* If sched scan was aborted by the driver. */ 12406ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1241d43c6b6eSDavid Spinadel 124285a9994aSLuciano Coelho mutex_unlock(&local->mtx); 124385a9994aSLuciano Coelho 124485a9994aSLuciano Coelho cfg80211_sched_scan_stopped(local->hw.wiphy); 124585a9994aSLuciano Coelho } 124685a9994aSLuciano Coelho 1247f6837ba8SJohannes Berg void ieee80211_sched_scan_stopped_work(struct work_struct *work) 1248f6837ba8SJohannes Berg { 1249f6837ba8SJohannes Berg struct ieee80211_local *local = 1250f6837ba8SJohannes Berg container_of(work, struct ieee80211_local, 1251f6837ba8SJohannes Berg sched_scan_stopped_work); 1252f6837ba8SJohannes Berg 1253f6837ba8SJohannes Berg ieee80211_sched_scan_end(local); 1254f6837ba8SJohannes Berg } 1255f6837ba8SJohannes Berg 125679f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 125779f460caSLuciano Coelho { 125879f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 125979f460caSLuciano Coelho 126079f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 126179f460caSLuciano Coelho 12622bc533bdSEliad Peller /* 12632bc533bdSEliad Peller * this shouldn't really happen, so for simplicity 12642bc533bdSEliad Peller * simply ignore it, and let mac80211 reconfigure 12652bc533bdSEliad Peller * the sched scan later on. 12662bc533bdSEliad Peller */ 12672bc533bdSEliad Peller if (local->in_reconfig) 12682bc533bdSEliad Peller return; 12692bc533bdSEliad Peller 127018db594aSJohannes Berg schedule_work(&local->sched_scan_stopped_work); 127179f460caSLuciano Coelho } 127279f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1273