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 100a51b27eSJohannes Berg * 110a51b27eSJohannes Berg * This program is free software; you can redistribute it and/or modify 120a51b27eSJohannes Berg * it under the terms of the GNU General Public License version 2 as 130a51b27eSJohannes Berg * published by the Free Software Foundation. 140a51b27eSJohannes Berg */ 150a51b27eSJohannes Berg 160a51b27eSJohannes Berg #include <linux/if_arp.h> 17888d04dfSFelix Fietkau #include <linux/etherdevice.h> 18078e1e60SJohannes Berg #include <linux/rtnetlink.h> 19e8db0be1SJean Pihet #include <linux/pm_qos.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; 70*61f6bba0SJohannes Berg struct cfg80211_inform_bss bss_meta = {}; 7174d803b6SSara Sharon bool signal_valid; 722a519311SJohannes Berg 7330686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) 74*61f6bba0SJohannes Berg bss_meta.signal = rx_status->signal * 100; 7530686bf7SJohannes Berg else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) 76*61f6bba0SJohannes Berg bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal; 772a519311SJohannes Berg 78*61f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20; 797ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_5MHZ) 80*61f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5; 817ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_10MHZ) 82*61f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10; 837ca15a0aSSimon Wunderlich 84*61f6bba0SJohannes Berg bss_meta.chan = channel; 85*61f6bba0SJohannes Berg cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, 86*61f6bba0SJohannes Berg mgmt, len, GFP_ATOMIC); 870c1ad2caSJohannes Berg if (!cbss) 885484e237SJohannes Berg return NULL; 8974d803b6SSara Sharon /* In case the signal is invalid update the status */ 9074d803b6SSara Sharon signal_valid = abs(channel->center_freq - cbss->channel->center_freq) 9174d803b6SSara Sharon <= local->hw.wiphy->max_adj_channel_rssi_comp; 9274d803b6SSara Sharon if (!signal_valid) 9374d803b6SSara Sharon rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL; 9400d3f14cSJohannes Berg 950c1ad2caSJohannes Berg bss = (void *)cbss->priv; 965484e237SJohannes Berg 97ef429dadSJohannes Berg if (beacon) 98ef429dadSJohannes Berg bss->device_ts_beacon = rx_status->device_timestamp; 99ef429dadSJohannes Berg else 100ef429dadSJohannes Berg bss->device_ts_presp = rx_status->device_timestamp; 1018c358bcdSJohannes Berg 102fcff4f10SPaul Stewart if (elems->parse_error) { 103fcff4f10SPaul Stewart if (beacon) 104fcff4f10SPaul Stewart bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON; 105fcff4f10SPaul Stewart else 106fcff4f10SPaul Stewart bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP; 107fcff4f10SPaul Stewart } else { 108fcff4f10SPaul Stewart if (beacon) 109fcff4f10SPaul Stewart bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON; 110fcff4f10SPaul Stewart else 111fcff4f10SPaul Stewart bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP; 1125484e237SJohannes Berg } 1135484e237SJohannes Berg 114fcff4f10SPaul Stewart /* save the ERP value so that it is available at association time */ 1151946bed9SJohannes Berg if (elems->erp_info && (!elems->parse_error || 116fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) { 117fcff4f10SPaul Stewart bss->erp_value = elems->erp_info[0]; 118fcff4f10SPaul Stewart bss->has_erp_value = true; 119fcff4f10SPaul Stewart if (!elems->parse_error) 120fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_ERP; 121fcff4f10SPaul Stewart } 122fcff4f10SPaul Stewart 123f0b058b6SStanislaw Gruszka /* replace old supported rates if we get new values */ 124fcff4f10SPaul Stewart if (!elems->parse_error || 125fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) { 126f0b058b6SStanislaw Gruszka srlen = 0; 1275484e237SJohannes Berg if (elems->supp_rates) { 128f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES; 1295484e237SJohannes Berg if (clen > elems->supp_rates_len) 1305484e237SJohannes Berg clen = elems->supp_rates_len; 131f0b058b6SStanislaw Gruszka memcpy(bss->supp_rates, elems->supp_rates, clen); 132f0b058b6SStanislaw Gruszka srlen += clen; 1335484e237SJohannes Berg } 1345484e237SJohannes Berg if (elems->ext_supp_rates) { 135f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES - srlen; 1365484e237SJohannes Berg if (clen > elems->ext_supp_rates_len) 1375484e237SJohannes Berg clen = elems->ext_supp_rates_len; 138fcff4f10SPaul Stewart memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, 139fcff4f10SPaul Stewart clen); 140f0b058b6SStanislaw Gruszka srlen += clen; 1415484e237SJohannes Berg } 142fcff4f10SPaul Stewart if (srlen) { 143f0b058b6SStanislaw Gruszka bss->supp_rates_len = srlen; 144fcff4f10SPaul Stewart if (!elems->parse_error) 145fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_RATES; 146fcff4f10SPaul Stewart } 147fcff4f10SPaul Stewart } 1485484e237SJohannes Berg 149fcff4f10SPaul Stewart if (!elems->parse_error || 150fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) { 1515484e237SJohannes Berg bss->wmm_used = elems->wmm_param || elems->wmm_info; 152ab13315aSKalle Valo bss->uapsd_supported = is_uapsd_supported(elems); 153fcff4f10SPaul Stewart if (!elems->parse_error) 154fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_WMM; 155fcff4f10SPaul Stewart } 1565484e237SJohannes Berg 157817cee76SAlexander Bondar if (beacon) { 158817cee76SAlexander Bondar struct ieee80211_supported_band *sband = 159817cee76SAlexander Bondar local->hw.wiphy->bands[rx_status->band]; 160817cee76SAlexander Bondar if (!(rx_status->flag & RX_FLAG_HT) && 161817cee76SAlexander Bondar !(rx_status->flag & RX_FLAG_VHT)) 162817cee76SAlexander Bondar bss->beacon_rate = 163817cee76SAlexander Bondar &sband->bitrates[rx_status->rate_idx]; 164817cee76SAlexander Bondar } 165817cee76SAlexander Bondar 1665484e237SJohannes Berg return bss; 1675484e237SJohannes Berg } 1680a51b27eSJohannes Berg 169d48b2968SJohannes Berg void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb) 17098c8fccfSJohannes Berg { 171f1d58c25SJohannes Berg struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 172d48b2968SJohannes Berg struct ieee80211_sub_if_data *sdata1, *sdata2; 173d48b2968SJohannes Berg struct ieee80211_mgmt *mgmt = (void *)skb->data; 174c2b13452SJohannes Berg struct ieee80211_bss *bss; 17598c8fccfSJohannes Berg u8 *elements; 17698c8fccfSJohannes Berg struct ieee80211_channel *channel; 17798c8fccfSJohannes Berg size_t baselen; 17898c8fccfSJohannes Berg struct ieee802_11_elems elems; 17998c8fccfSJohannes Berg 180d48b2968SJohannes Berg if (skb->len < 24 || 181d48b2968SJohannes Berg (!ieee80211_is_probe_resp(mgmt->frame_control) && 182d48b2968SJohannes Berg !ieee80211_is_beacon(mgmt->frame_control))) 183d48b2968SJohannes Berg return; 18498c8fccfSJohannes Berg 185d48b2968SJohannes Berg sdata1 = rcu_dereference(local->scan_sdata); 186d48b2968SJohannes Berg sdata2 = rcu_dereference(local->sched_scan_sdata); 18798c8fccfSJohannes Berg 188d48b2968SJohannes Berg if (likely(!sdata1 && !sdata2)) 189d48b2968SJohannes Berg return; 19098c8fccfSJohannes Berg 191d48b2968SJohannes Berg if (ieee80211_is_probe_resp(mgmt->frame_control)) { 192a344d677SJohannes Berg struct cfg80211_scan_request *scan_req; 193a344d677SJohannes Berg struct cfg80211_sched_scan_request *sched_scan_req; 194a344d677SJohannes Berg 195a344d677SJohannes Berg scan_req = rcu_dereference(local->scan_req); 196a344d677SJohannes Berg sched_scan_req = rcu_dereference(local->sched_scan_req); 197a344d677SJohannes Berg 198a344d677SJohannes Berg /* ignore ProbeResp to foreign address unless scanning 199a344d677SJohannes Berg * with randomised address 200a344d677SJohannes Berg */ 201a344d677SJohannes Berg if (!(sdata1 && 202a344d677SJohannes Berg (ether_addr_equal(mgmt->da, sdata1->vif.addr) || 203a344d677SJohannes Berg scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) && 204a344d677SJohannes Berg !(sdata2 && 205a344d677SJohannes Berg (ether_addr_equal(mgmt->da, sdata2->vif.addr) || 206a344d677SJohannes Berg sched_scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR))) 207d48b2968SJohannes Berg return; 20898c8fccfSJohannes Berg 20998c8fccfSJohannes Berg elements = mgmt->u.probe_resp.variable; 21098c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); 21198c8fccfSJohannes Berg } else { 21298c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 21398c8fccfSJohannes Berg elements = mgmt->u.beacon.variable; 21498c8fccfSJohannes Berg } 21598c8fccfSJohannes Berg 21698c8fccfSJohannes Berg if (baselen > skb->len) 217d48b2968SJohannes Berg return; 21898c8fccfSJohannes Berg 219b2e506bfSJohannes Berg ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems); 22098c8fccfSJohannes Berg 2210172bb75SJohannes Berg channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 22298c8fccfSJohannes Berg 22398c8fccfSJohannes Berg if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 224d48b2968SJohannes Berg return; 22598c8fccfSJohannes Berg 226d48b2968SJohannes Berg bss = ieee80211_bss_info_update(local, rx_status, 22798c8fccfSJohannes Berg mgmt, skb->len, &elems, 228d45c4172SEmmanuel Grumbach channel); 229d048e503SJouni Malinen if (bss) 230d48b2968SJohannes Berg ieee80211_rx_bss_put(local, bss); 23198c8fccfSJohannes Berg } 23298c8fccfSJohannes Berg 2332103dec1SSimon Wunderlich static void 2342103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef, 2352103dec1SSimon Wunderlich enum nl80211_bss_scan_width scan_width) 2362103dec1SSimon Wunderlich { 2372103dec1SSimon Wunderlich memset(chandef, 0, sizeof(*chandef)); 2382103dec1SSimon Wunderlich switch (scan_width) { 2392103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 2402103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_5; 2412103dec1SSimon Wunderlich break; 2422103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 2432103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_10; 2442103dec1SSimon Wunderlich break; 2452103dec1SSimon Wunderlich default: 2462103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 2472103dec1SSimon Wunderlich break; 2482103dec1SSimon Wunderlich } 2492103dec1SSimon Wunderlich } 2502103dec1SSimon Wunderlich 2514d36ec58SJohannes Berg /* return false if no more work */ 2524d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 2534d36ec58SJohannes Berg { 2546ea0a69cSJohannes Berg struct cfg80211_scan_request *req; 2552103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 256c56ef672SDavid Spinadel u8 bands_used = 0; 2574d36ec58SJohannes Berg int i, ielen, n_chans; 2584d36ec58SJohannes Berg 2596ea0a69cSJohannes Berg req = rcu_dereference_protected(local->scan_req, 2606ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 2616ea0a69cSJohannes Berg 262a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 263a754055aSEmmanuel Grumbach return false; 264a754055aSEmmanuel Grumbach 26530686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 266c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 267c56ef672SDavid Spinadel local->hw_scan_req->req.channels[i] = req->channels[i]; 268c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 269c56ef672SDavid Spinadel } 270c56ef672SDavid Spinadel 271c56ef672SDavid Spinadel n_chans = req->n_channels; 272c56ef672SDavid Spinadel } else { 2734d36ec58SJohannes Berg do { 2744d36ec58SJohannes Berg if (local->hw_scan_band == IEEE80211_NUM_BANDS) 2754d36ec58SJohannes Berg return false; 2764d36ec58SJohannes Berg 2774d36ec58SJohannes Berg n_chans = 0; 278c56ef672SDavid Spinadel 2794d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 280c56ef672SDavid Spinadel if (req->channels[i]->band != 281c56ef672SDavid Spinadel local->hw_scan_band) 282c56ef672SDavid Spinadel continue; 283c56ef672SDavid Spinadel local->hw_scan_req->req.channels[n_chans] = 2844d36ec58SJohannes Berg req->channels[i]; 2854d36ec58SJohannes Berg n_chans++; 286c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 2874d36ec58SJohannes Berg } 2884d36ec58SJohannes Berg 2894d36ec58SJohannes Berg local->hw_scan_band++; 2904d36ec58SJohannes Berg } while (!n_chans); 291c56ef672SDavid Spinadel } 2924d36ec58SJohannes Berg 293c56ef672SDavid Spinadel local->hw_scan_req->req.n_channels = n_chans; 2942103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 2954d36ec58SJohannes Berg 296c56ef672SDavid Spinadel ielen = ieee80211_build_preq_ies(local, 297c56ef672SDavid Spinadel (u8 *)local->hw_scan_req->req.ie, 298c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, 299c56ef672SDavid Spinadel &local->hw_scan_req->ies, 300c56ef672SDavid Spinadel req->ie, req->ie_len, 301c56ef672SDavid Spinadel bands_used, req->rates, &chandef); 302c56ef672SDavid Spinadel local->hw_scan_req->req.ie_len = ielen; 303c56ef672SDavid Spinadel local->hw_scan_req->req.no_cck = req->no_cck; 304a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr); 305a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr_mask, 306a344d677SJohannes Berg req->mac_addr_mask); 3074d36ec58SJohannes Berg 3084d36ec58SJohannes Berg return true; 3094d36ec58SJohannes Berg } 3104d36ec58SJohannes Berg 3118bd2a248SEliad Peller static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 312f3b85252SJohannes Berg { 313f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3148bd2a248SEliad Peller bool hw_scan = local->ops->hw_scan; 315a2b70e83SEliad Peller bool was_scanning = local->scanning; 3166ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 317a344d677SJohannes Berg struct ieee80211_sub_if_data *scan_sdata; 318f3b85252SJohannes Berg 319e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 320f3b85252SJohannes Berg 3216d3560d4SJohannes Berg /* 3226d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 3236d3560d4SJohannes Berg * we have one at all will be verified by checking 3246d3560d4SJohannes Berg * local->scan_req next), but not to complete it 3256d3560d4SJohannes Berg * successfully. 3266d3560d4SJohannes Berg */ 3276d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 3286d3560d4SJohannes Berg aborted = true; 329f3b85252SJohannes Berg 330e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 331d07bfd8bSJohannes Berg return; 332f3b85252SJohannes Berg 333c56ef672SDavid Spinadel if (hw_scan && !aborted && 33430686bf7SJohannes Berg !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) && 335c56ef672SDavid Spinadel ieee80211_prep_hw_scan(local)) { 336e2fd5dbcSJohannes Berg int rc; 337e2fd5dbcSJohannes Berg 338e2fd5dbcSJohannes Berg rc = drv_hw_scan(local, 339e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 340e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)), 341e2fd5dbcSJohannes Berg local->hw_scan_req); 342e2fd5dbcSJohannes Berg 3436eb11a9aSStanislaw Gruszka if (rc == 0) 344d07bfd8bSJohannes Berg return; 3454d36ec58SJohannes Berg } 3464d36ec58SJohannes Berg 3474d36ec58SJohannes Berg kfree(local->hw_scan_req); 3484d36ec58SJohannes Berg local->hw_scan_req = NULL; 349f3b85252SJohannes Berg 3506ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 3516ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 3526ea0a69cSJohannes Berg 3536ea0a69cSJohannes Berg if (scan_req != local->int_scan_req) 3546ea0a69cSJohannes Berg cfg80211_scan_done(scan_req, aborted); 3556ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 356a344d677SJohannes Berg 357a344d677SJohannes Berg scan_sdata = rcu_dereference_protected(local->scan_sdata, 358a344d677SJohannes Berg lockdep_is_held(&local->mtx)); 3590c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 3602a519311SJohannes Berg 361fbe9c429SHelmut Schaa local->scanning = 0; 3627ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 363f3b85252SJohannes Berg 36459bdf3b0SBen Greear /* Set power back to normal operating levels. */ 36559bdf3b0SBen Greear ieee80211_hw_config(local, 0); 366b23b025fSBen Greear 3678bd2a248SEliad Peller if (!hw_scan) { 368b23b025fSBen Greear ieee80211_configure_filter(local); 369a344d677SJohannes Berg drv_sw_scan_complete(local, scan_sdata); 370aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 371b23b025fSBen Greear } 372b23b025fSBen Greear 3735cff20e6SJohannes Berg ieee80211_recalc_idle(local); 374e229f844SStanislaw Gruszka 3750a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 37646900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 377472dbc45SJohannes Berg ieee80211_mesh_notify_scan_completed(local); 378a2b70e83SEliad Peller if (was_scanning) 3792eb278e0SJohannes Berg ieee80211_start_next_roc(local); 3800a51b27eSJohannes Berg } 3818789d459SJohannes Berg 3828789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 3838789d459SJohannes Berg { 3848789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3858789d459SJohannes Berg 3868789d459SJohannes Berg trace_api_scan_completed(local, aborted); 3878789d459SJohannes Berg 3888789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 3898789d459SJohannes Berg if (aborted) 3908789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 3918789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 3928789d459SJohannes Berg } 3930a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 3940a51b27eSJohannes Berg 395a344d677SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local, 396a344d677SJohannes Berg struct ieee80211_sub_if_data *sdata) 397f3b85252SJohannes Berg { 398fe57d9f5SJohannes Berg /* Software scan is not supported in multi-channel cases */ 399fe57d9f5SJohannes Berg if (local->use_chanctx) 400fe57d9f5SJohannes Berg return -EOPNOTSUPP; 401fe57d9f5SJohannes Berg 402f3b85252SJohannes Berg /* 403f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 404f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 405f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 406f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 407f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 408f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 409f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 410f3b85252SJohannes Berg * 411f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 412f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 413f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 414f3b85252SJohannes Berg */ 415a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 416f3b85252SJohannes Berg 417de312db3SRajkumar Manoharan local->leave_oper_channel_time = jiffies; 418977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 419f3b85252SJohannes Berg local->scan_channel_idx = 0; 420f3b85252SJohannes Berg 421aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 422a80f7c0bSJohannes Berg 4239c35d7d2SSeth Forshee /* ensure nullfunc is transmitted before leaving operating channel */ 4243b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 4259c35d7d2SSeth Forshee 4263ac64beeSJohannes Berg ieee80211_configure_filter(local); 427f3b85252SJohannes Berg 42859bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 42959bdf3b0SBen Greear ieee80211_hw_config(local, 0); 43059bdf3b0SBen Greear 43142935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 43207ef03eeSJohannes Berg &local->scan_work, 0); 433f3b85252SJohannes Berg 434f3b85252SJohannes Berg return 0; 435f3b85252SJohannes Berg } 436f3b85252SJohannes Berg 437133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local, 438133d40f9SStanislaw Gruszka struct ieee80211_sub_if_data *sdata) 439133d40f9SStanislaw Gruszka { 4405cbc95a7SEliad Peller if (ieee80211_is_radar_required(local)) 441164eb02dSSimon Wunderlich return false; 442164eb02dSSimon Wunderlich 4432eb278e0SJohannes Berg if (!list_empty(&local->roc_list)) 444133d40f9SStanislaw Gruszka return false; 445133d40f9SStanislaw Gruszka 446133d40f9SStanislaw Gruszka if (sdata->vif.type == NL80211_IFTYPE_STATION && 447392b9ffbSStanislaw Gruszka sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 448133d40f9SStanislaw Gruszka return false; 449133d40f9SStanislaw Gruszka 450133d40f9SStanislaw Gruszka return true; 451133d40f9SStanislaw Gruszka } 452133d40f9SStanislaw Gruszka 453133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local) 454133d40f9SStanislaw Gruszka { 455133d40f9SStanislaw Gruszka lockdep_assert_held(&local->mtx); 456133d40f9SStanislaw Gruszka 457133d40f9SStanislaw Gruszka if (!local->scan_req || local->scanning) 458133d40f9SStanislaw Gruszka return; 459133d40f9SStanislaw Gruszka 460e2fd5dbcSJohannes Berg if (!ieee80211_can_scan(local, 461e2fd5dbcSJohannes Berg rcu_dereference_protected( 462e2fd5dbcSJohannes Berg local->scan_sdata, 463e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)))) 464133d40f9SStanislaw Gruszka return; 465133d40f9SStanislaw Gruszka 466133d40f9SStanislaw Gruszka ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 467133d40f9SStanislaw Gruszka round_jiffies_relative(0)); 468133d40f9SStanislaw Gruszka } 469f3b85252SJohannes Berg 4708a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 4718a690674SBen Greear unsigned long *next_delay) 4728a690674SBen Greear { 4738a690674SBen Greear int i; 474e2fd5dbcSJohannes Berg struct ieee80211_sub_if_data *sdata; 4756ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 476675a0b04SKarl Beldan enum ieee80211_band band = local->hw.conf.chandef.chan->band; 4776c17b77bSSeth Forshee u32 tx_flags; 4786c17b77bSSeth Forshee 4796ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 4806ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 4816ea0a69cSJohannes Berg 4826c17b77bSSeth Forshee tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 4836ea0a69cSJohannes Berg if (scan_req->no_cck) 4846c17b77bSSeth Forshee tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 4858a690674SBen Greear 486e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 487316b6b5dSPeter Senna Tschudin lockdep_is_held(&local->mtx)); 488e2fd5dbcSJohannes Berg 4896ea0a69cSJohannes Berg for (i = 0; i < scan_req->n_ssids; i++) 4908a690674SBen Greear ieee80211_send_probe_req( 491a344d677SJohannes Berg sdata, local->scan_addr, NULL, 4926ea0a69cSJohannes Berg scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len, 4936ea0a69cSJohannes Berg scan_req->ie, scan_req->ie_len, 4946ea0a69cSJohannes Berg scan_req->rates[band], false, 495675a0b04SKarl Beldan tx_flags, local->hw.conf.chandef.chan, true); 4968a690674SBen Greear 4978a690674SBen Greear /* 4988a690674SBen Greear * After sending probe requests, wait for probe responses 4998a690674SBen Greear * on the channel. 5008a690674SBen Greear */ 5018a690674SBen Greear *next_delay = IEEE80211_CHANNEL_TIME; 5028a690674SBen Greear local->next_scan_state = SCAN_DECISION; 5038a690674SBen Greear } 5048a690674SBen Greear 505f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 506f3b85252SJohannes Berg struct cfg80211_scan_request *req) 507f3b85252SJohannes Berg { 508f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 509f3b85252SJohannes Berg int rc; 510f3b85252SJohannes Berg 511e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 512e229f844SStanislaw Gruszka 5132726f23dSEliad Peller if (local->scan_req || ieee80211_is_radar_required(local)) 514f3b85252SJohannes Berg return -EBUSY; 515f3b85252SJohannes Berg 516133d40f9SStanislaw Gruszka if (!ieee80211_can_scan(local, sdata)) { 5176e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 5186ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 519e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 520c0ce77b8SJohannes Berg return 0; 521c0ce77b8SJohannes Berg } 522c0ce77b8SJohannes Berg 523f3b85252SJohannes Berg if (local->ops->hw_scan) { 524f3b85252SJohannes Berg u8 *ies; 525f3b85252SJohannes Berg 526e4dcbb37SDavid Spinadel local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len; 527c56ef672SDavid Spinadel 52830686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 529c56ef672SDavid Spinadel int i, n_bands = 0; 530c56ef672SDavid Spinadel u8 bands_counted = 0; 531c56ef672SDavid Spinadel 532c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 533c56ef672SDavid Spinadel if (bands_counted & BIT(req->channels[i]->band)) 534c56ef672SDavid Spinadel continue; 535c56ef672SDavid Spinadel bands_counted |= BIT(req->channels[i]->band); 536c56ef672SDavid Spinadel n_bands++; 537c56ef672SDavid Spinadel } 538c56ef672SDavid Spinadel 539c56ef672SDavid Spinadel local->hw_scan_ies_bufsize *= n_bands; 540c56ef672SDavid Spinadel } 541c56ef672SDavid Spinadel 5424d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 5434d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 5444d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 545c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, GFP_KERNEL); 5464d36ec58SJohannes Berg if (!local->hw_scan_req) 547f3b85252SJohannes Berg return -ENOMEM; 548f3b85252SJohannes Berg 549c56ef672SDavid Spinadel local->hw_scan_req->req.ssids = req->ssids; 550c56ef672SDavid Spinadel local->hw_scan_req->req.n_ssids = req->n_ssids; 5514d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 5524d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 5534d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 554c56ef672SDavid Spinadel local->hw_scan_req->req.ie = ies; 555c56ef672SDavid Spinadel local->hw_scan_req->req.flags = req->flags; 5564d36ec58SJohannes Berg 5574d36ec58SJohannes Berg local->hw_scan_band = 0; 5586e7e6213SJohn W. Linville 5596e7e6213SJohn W. Linville /* 5606e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 5616e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 5626e7e6213SJohn W. Linville * anything that might be changed here and leave 5636e7e6213SJohn W. Linville * this function early must not go after this 5646e7e6213SJohn W. Linville * allocation. 5656e7e6213SJohn W. Linville */ 566f3b85252SJohannes Berg } 567f3b85252SJohannes Berg 5686ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 569e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 570f3b85252SJohannes Berg 571a344d677SJohannes Berg if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 572a344d677SJohannes Berg get_random_mask_addr(local->scan_addr, 573a344d677SJohannes Berg req->mac_addr, 574a344d677SJohannes Berg req->mac_addr_mask); 575a344d677SJohannes Berg else 576a344d677SJohannes Berg memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN); 577a344d677SJohannes Berg 5788a690674SBen Greear if (local->ops->hw_scan) { 579fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 5808a690674SBen Greear } else if ((req->n_channels == 1) && 581675a0b04SKarl Beldan (req->channels[0] == local->_oper_chandef.chan)) { 5829b864870SJohannes Berg /* 5839b864870SJohannes Berg * If we are scanning only on the operating channel 5849b864870SJohannes Berg * then we do not need to stop normal activities 5858a690674SBen Greear */ 5868a690674SBen Greear unsigned long next_delay; 5878a690674SBen Greear 5888a690674SBen Greear __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 5898a690674SBen Greear 5908a690674SBen Greear ieee80211_recalc_idle(local); 5918a690674SBen Greear 5928a690674SBen Greear /* Notify driver scan is starting, keep order of operations 5938a690674SBen Greear * same as normal software scan, in case that matters. */ 594a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 5958a690674SBen Greear 5968a690674SBen Greear ieee80211_configure_filter(local); /* accept probe-responses */ 5978a690674SBen Greear 5988a690674SBen Greear /* We need to ensure power level is at max for scanning. */ 5998a690674SBen Greear ieee80211_hw_config(local, 0); 6008a690674SBen Greear 6018a690674SBen Greear if ((req->channels[0]->flags & 6028fe02e16SLuis R. Rodriguez IEEE80211_CHAN_NO_IR) || 6036ea0a69cSJohannes Berg !req->n_ssids) { 6048a690674SBen Greear next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 6058a690674SBen Greear } else { 6068a690674SBen Greear ieee80211_scan_state_send_probe(local, &next_delay); 6078a690674SBen Greear next_delay = IEEE80211_CHANNEL_TIME; 6088a690674SBen Greear } 6098a690674SBen Greear 6108a690674SBen Greear /* Now, just wait a bit and we are all done! */ 6118a690674SBen Greear ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 6128a690674SBen Greear next_delay); 6138a690674SBen Greear return 0; 6148a690674SBen Greear } else { 6158a690674SBen Greear /* Do normal software scan */ 616fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 6178a690674SBen Greear } 6186e7e6213SJohn W. Linville 6195cff20e6SJohannes Berg ieee80211_recalc_idle(local); 620f3b85252SJohannes Berg 6214d36ec58SJohannes Berg if (local->ops->hw_scan) { 6224d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 623a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 624a344d677SJohannes Berg } else { 625a344d677SJohannes Berg rc = ieee80211_start_sw_scan(local, sdata); 626a344d677SJohannes Berg } 627f3b85252SJohannes Berg 628f3b85252SJohannes Berg if (rc) { 6294d36ec58SJohannes Berg kfree(local->hw_scan_req); 6304d36ec58SJohannes Berg local->hw_scan_req = NULL; 631fbe9c429SHelmut Schaa local->scanning = 0; 632f3b85252SJohannes Berg 6335cff20e6SJohannes Berg ieee80211_recalc_idle(local); 6345cff20e6SJohannes Berg 635f3b85252SJohannes Berg local->scan_req = NULL; 6360c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 637f3b85252SJohannes Berg } 638f3b85252SJohannes Berg 639f3b85252SJohannes Berg return rc; 640f3b85252SJohannes Berg } 641f3b85252SJohannes Berg 642df13cce5SHelmut Schaa static unsigned long 643df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 644df13cce5SHelmut Schaa { 645df13cce5SHelmut Schaa /* 646df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 647df13cce5SHelmut Schaa * add that delay as well to get a better estimation 648df13cce5SHelmut Schaa */ 6498fe02e16SLuis R. Rodriguez if (chan->flags & IEEE80211_CHAN_NO_IR) 650df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 651df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 652df13cce5SHelmut Schaa } 653df13cce5SHelmut Schaa 654e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 6552fb3f028SHelmut Schaa unsigned long *next_delay) 6562fb3f028SHelmut Schaa { 657142b9f50SHelmut Schaa bool associated = false; 658df13cce5SHelmut Schaa bool tx_empty = true; 659df13cce5SHelmut Schaa bool bad_latency; 660142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 661df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 662cd2bb512SSam Leffler enum mac80211_scan_state next_scan_state; 6636ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 664142b9f50SHelmut Schaa 665df13cce5SHelmut Schaa /* 666df13cce5SHelmut Schaa * check if at least one STA interface is associated, 667df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 668df13cce5SHelmut Schaa * and grab the lowest used beacon interval 669df13cce5SHelmut Schaa */ 670142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 671142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 6729607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 673142b9f50SHelmut Schaa continue; 674142b9f50SHelmut Schaa 675142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 676142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 677142b9f50SHelmut Schaa associated = true; 678df13cce5SHelmut Schaa 679df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 680df13cce5SHelmut Schaa tx_empty = false; 681142b9f50SHelmut Schaa break; 682142b9f50SHelmut Schaa } 683142b9f50SHelmut Schaa } 684142b9f50SHelmut Schaa } 685df13cce5SHelmut Schaa } 686142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 687142b9f50SHelmut Schaa 6886ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 6896ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 6906ea0a69cSJohannes Berg 6916ea0a69cSJohannes Berg next_chan = scan_req->channels[local->scan_channel_idx]; 692b23b025fSBen Greear 693142b9f50SHelmut Schaa /* 694142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 695df13cce5SHelmut Schaa * see if we can scan another channel without interfering 696df13cce5SHelmut Schaa * with the current traffic situation. 697df13cce5SHelmut Schaa * 6983f892b61SStanislaw Gruszka * Keep good latency, do not stay off-channel more than 125 ms. 699142b9f50SHelmut Schaa */ 700df13cce5SHelmut Schaa 701df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 702df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 7033f892b61SStanislaw Gruszka local->leave_oper_channel_time + HZ / 8); 704df13cce5SHelmut Schaa 705cd2bb512SSam Leffler if (associated && !tx_empty) { 7066ea0a69cSJohannes Berg if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 707cd2bb512SSam Leffler next_scan_state = SCAN_ABORT; 708142b9f50SHelmut Schaa else 709cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 7103f892b61SStanislaw Gruszka } else if (associated && bad_latency) { 711cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 712cd2bb512SSam Leffler } else { 713cd2bb512SSam Leffler next_scan_state = SCAN_SET_CHANNEL; 714cd2bb512SSam Leffler } 715cd2bb512SSam Leffler 716cd2bb512SSam Leffler local->next_scan_state = next_scan_state; 717142b9f50SHelmut Schaa 718142b9f50SHelmut Schaa *next_delay = 0; 7192fb3f028SHelmut Schaa } 7202fb3f028SHelmut Schaa 7212fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 7227d3be3ccSHelmut Schaa unsigned long *next_delay) 7237d3be3ccSHelmut Schaa { 7247d3be3ccSHelmut Schaa int skip; 7257d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 7267ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width oper_scan_width; 7276ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 7286ea0a69cSJohannes Berg 7296ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 7306ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 7317d3be3ccSHelmut Schaa 7327d3be3ccSHelmut Schaa skip = 0; 7336ea0a69cSJohannes Berg chan = scan_req->channels[local->scan_channel_idx]; 7347d3be3ccSHelmut Schaa 7357ca15a0aSSimon Wunderlich local->scan_chandef.chan = chan; 7367ca15a0aSSimon Wunderlich local->scan_chandef.center_freq1 = chan->center_freq; 7377ca15a0aSSimon Wunderlich local->scan_chandef.center_freq2 = 0; 7386ea0a69cSJohannes Berg switch (scan_req->scan_width) { 7397ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 7407ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 7417ca15a0aSSimon Wunderlich break; 7427ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 7437ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 7447ca15a0aSSimon Wunderlich break; 7457ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_20: 7467ca15a0aSSimon Wunderlich /* If scanning on oper channel, use whatever channel-type 7477ca15a0aSSimon Wunderlich * is currently in use. 7487ca15a0aSSimon Wunderlich */ 7497ca15a0aSSimon Wunderlich oper_scan_width = cfg80211_chandef_to_scan_width( 7507ca15a0aSSimon Wunderlich &local->_oper_chandef); 7517ca15a0aSSimon Wunderlich if (chan == local->_oper_chandef.chan && 7526ea0a69cSJohannes Berg oper_scan_width == scan_req->scan_width) 7537ca15a0aSSimon Wunderlich local->scan_chandef = local->_oper_chandef; 7547ca15a0aSSimon Wunderlich else 7557ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 7567ca15a0aSSimon Wunderlich break; 7577ca15a0aSSimon Wunderlich } 758b23b025fSBen Greear 759584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 7607d3be3ccSHelmut Schaa skip = 1; 7617d3be3ccSHelmut Schaa 7627d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 7637d3be3ccSHelmut Schaa local->scan_channel_idx++; 7647d3be3ccSHelmut Schaa 7650ee9c13cSHelmut Schaa if (skip) { 7660ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 7670ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 7682fb3f028SHelmut Schaa return; 7690ee9c13cSHelmut Schaa } 7707d3be3ccSHelmut Schaa 7717d3be3ccSHelmut Schaa /* 7727d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 7737d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 7747d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 7757d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 7767d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 7777d3be3ccSHelmut Schaa * received. 7787d3be3ccSHelmut Schaa * 7797d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 7807d3be3ccSHelmut Schaa */ 7816ea0a69cSJohannes Berg if (chan->flags & IEEE80211_CHAN_NO_IR || !scan_req->n_ssids) { 7827d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 783977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 7842fb3f028SHelmut Schaa return; 7857d3be3ccSHelmut Schaa } 7867d3be3ccSHelmut Schaa 7872fb3f028SHelmut Schaa /* active scan, send probes */ 7887d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 789977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 7907d3be3ccSHelmut Schaa } 7917d3be3ccSHelmut Schaa 79207ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 79307ef03eeSJohannes Berg unsigned long *next_delay) 79407ef03eeSJohannes Berg { 79507ef03eeSJohannes Berg /* switch back to the operating channel */ 7967ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 79707ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 79807ef03eeSJohannes Berg 799aacde9eeSStanislaw Gruszka /* disable PS */ 800aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 80107ef03eeSJohannes Berg 80207ef03eeSJohannes Berg *next_delay = HZ / 5; 80307ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 80407ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 80507ef03eeSJohannes Berg } 80607ef03eeSJohannes Berg 80707ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 80807ef03eeSJohannes Berg unsigned long *next_delay) 80907ef03eeSJohannes Berg { 810aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 81107ef03eeSJohannes Berg 81207ef03eeSJohannes Berg if (local->ops->flush) { 8133b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 81407ef03eeSJohannes Berg *next_delay = 0; 81507ef03eeSJohannes Berg } else 81607ef03eeSJohannes Berg *next_delay = HZ / 10; 81707ef03eeSJohannes Berg 81807ef03eeSJohannes Berg /* remember when we left the operating channel */ 81907ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 82007ef03eeSJohannes Berg 82107ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 822de2ee84dSMohammed Shafi Shajakhan local->next_scan_state = SCAN_SET_CHANNEL; 82307ef03eeSJohannes Berg } 82407ef03eeSJohannes Berg 825c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 8260a51b27eSJohannes Berg { 8270a51b27eSJohannes Berg struct ieee80211_local *local = 8280a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 829d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 8306ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 8310a51b27eSJohannes Berg unsigned long next_delay = 0; 8328bd2a248SEliad Peller bool aborted; 8338789d459SJohannes Berg 834259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 835259b62e3SStanislaw Gruszka 836332ff7feSLuciano Coelho if (!ieee80211_can_run_worker(local)) { 837332ff7feSLuciano Coelho aborted = true; 838332ff7feSLuciano Coelho goto out_complete; 839332ff7feSLuciano Coelho } 840332ff7feSLuciano Coelho 841e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 842e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)); 8436ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 8446ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 845d07bfd8bSJohannes Berg 8468a690674SBen Greear /* When scanning on-channel, the first-callback means completed. */ 8478a690674SBen Greear if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 8488a690674SBen Greear aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 8498a690674SBen Greear goto out_complete; 8508a690674SBen Greear } 8518a690674SBen Greear 852259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 8538789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 854259b62e3SStanislaw Gruszka goto out_complete; 8558789d459SJohannes Berg } 8568789d459SJohannes Berg 8576ea0a69cSJohannes Berg if (!sdata || !scan_req) 858259b62e3SStanislaw Gruszka goto out; 859f3b85252SJohannes Berg 860ff5db439SEliad Peller if (!local->scanning) { 861f3b85252SJohannes Berg int rc; 862f3b85252SJohannes Berg 8636ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 8640c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 865f3b85252SJohannes Berg 8666ea0a69cSJohannes Berg rc = __ieee80211_start_scan(sdata, scan_req); 867259b62e3SStanislaw Gruszka if (rc) { 8683aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 8696ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, scan_req); 870259b62e3SStanislaw Gruszka aborted = true; 871259b62e3SStanislaw Gruszka goto out_complete; 872259b62e3SStanislaw Gruszka } else 873259b62e3SStanislaw Gruszka goto out; 874f3b85252SJohannes Berg } 875f3b85252SJohannes Berg 8765bc75728SJohannes Berg /* 877f502d09bSHelmut Schaa * as long as no delay is required advance immediately 878f502d09bSHelmut Schaa * without scheduling a new work 879f502d09bSHelmut Schaa */ 880f502d09bSHelmut Schaa do { 881c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 882c29acf20SRajkumar Manoharan aborted = true; 883c29acf20SRajkumar Manoharan goto out_complete; 884c29acf20SRajkumar Manoharan } 885c29acf20SRajkumar Manoharan 886977923b0SHelmut Schaa switch (local->next_scan_state) { 8872fb3f028SHelmut Schaa case SCAN_DECISION: 888e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 8896ea0a69cSJohannes Berg if (local->scan_channel_idx >= scan_req->n_channels) { 890e229f844SStanislaw Gruszka aborted = false; 891e229f844SStanislaw Gruszka goto out_complete; 892e229f844SStanislaw Gruszka } 893e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 8940a51b27eSJohannes Berg break; 8952fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 8962fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 8972fb3f028SHelmut Schaa break; 8980a51b27eSJohannes Berg case SCAN_SEND_PROBE: 8997d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 9000a51b27eSJohannes Berg break; 90107ef03eeSJohannes Berg case SCAN_SUSPEND: 90207ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 903142b9f50SHelmut Schaa break; 90407ef03eeSJohannes Berg case SCAN_RESUME: 90507ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 906142b9f50SHelmut Schaa break; 907cd2bb512SSam Leffler case SCAN_ABORT: 908cd2bb512SSam Leffler aborted = true; 909cd2bb512SSam Leffler goto out_complete; 9100a51b27eSJohannes Berg } 911f502d09bSHelmut Schaa } while (next_delay == 0); 9120a51b27eSJohannes Berg 91342935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 914d07bfd8bSJohannes Berg goto out; 915259b62e3SStanislaw Gruszka 916259b62e3SStanislaw Gruszka out_complete: 9178bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, aborted); 918259b62e3SStanislaw Gruszka out: 919259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 9200a51b27eSJohannes Berg } 9210a51b27eSJohannes Berg 922c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 9232a519311SJohannes Berg struct cfg80211_scan_request *req) 9240a51b27eSJohannes Berg { 925f3b85252SJohannes Berg int res; 9260a51b27eSJohannes Berg 927a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 928f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 929a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 9302a519311SJohannes Berg 931f3b85252SJohannes Berg return res; 9320a51b27eSJohannes Berg } 9330a51b27eSJohannes Berg 93434bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 935be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 93676bed0f4SJanusz.Dziedzic@tieto.com struct ieee80211_channel **channels, 93776bed0f4SJanusz.Dziedzic@tieto.com unsigned int n_channels, 9387ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width) 939f3b85252SJohannes Berg { 940f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 94176bed0f4SJanusz.Dziedzic@tieto.com int ret = -EBUSY, i, n_ch = 0; 942fb63bc41SGertjan van Wingerde enum ieee80211_band band; 9439116dd01SJohannes Berg 944a1699b75SJohannes Berg mutex_lock(&local->mtx); 945f3b85252SJohannes Berg 946f3b85252SJohannes Berg /* busy scanning */ 947f3b85252SJohannes Berg if (local->scan_req) 948f3b85252SJohannes Berg goto unlock; 949f3b85252SJohannes Berg 950be4a4b6aSJohannes Berg /* fill internal scan request */ 95176bed0f4SJanusz.Dziedzic@tieto.com if (!channels) { 95276bed0f4SJanusz.Dziedzic@tieto.com int max_n; 953be4a4b6aSJohannes Berg 954be4a4b6aSJohannes Berg for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 955be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 956be4a4b6aSJohannes Berg continue; 95734bcf715SStanislaw Gruszka 95834bcf715SStanislaw Gruszka max_n = local->hw.wiphy->bands[band]->n_channels; 95934bcf715SStanislaw Gruszka for (i = 0; i < max_n; i++) { 96034bcf715SStanislaw Gruszka struct ieee80211_channel *tmp_ch = 961be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 96234bcf715SStanislaw Gruszka 9638fe02e16SLuis R. Rodriguez if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 96434bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED)) 96534bcf715SStanislaw Gruszka continue; 96634bcf715SStanislaw Gruszka 96734bcf715SStanislaw Gruszka local->int_scan_req->channels[n_ch] = tmp_ch; 96834bcf715SStanislaw Gruszka n_ch++; 969be4a4b6aSJohannes Berg } 970be4a4b6aSJohannes Berg } 971be4a4b6aSJohannes Berg 97234bcf715SStanislaw Gruszka if (WARN_ON_ONCE(n_ch == 0)) 97334bcf715SStanislaw Gruszka goto unlock; 97434bcf715SStanislaw Gruszka 97534bcf715SStanislaw Gruszka local->int_scan_req->n_channels = n_ch; 976be4a4b6aSJohannes Berg } else { 97776bed0f4SJanusz.Dziedzic@tieto.com for (i = 0; i < n_channels; i++) { 97876bed0f4SJanusz.Dziedzic@tieto.com if (channels[i]->flags & (IEEE80211_CHAN_NO_IR | 97976bed0f4SJanusz.Dziedzic@tieto.com IEEE80211_CHAN_DISABLED)) 98076bed0f4SJanusz.Dziedzic@tieto.com continue; 98176bed0f4SJanusz.Dziedzic@tieto.com 98276bed0f4SJanusz.Dziedzic@tieto.com local->int_scan_req->channels[n_ch] = channels[i]; 98376bed0f4SJanusz.Dziedzic@tieto.com n_ch++; 98476bed0f4SJanusz.Dziedzic@tieto.com } 98576bed0f4SJanusz.Dziedzic@tieto.com 98676bed0f4SJanusz.Dziedzic@tieto.com if (WARN_ON_ONCE(n_ch == 0)) 98734bcf715SStanislaw Gruszka goto unlock; 98834bcf715SStanislaw Gruszka 98976bed0f4SJanusz.Dziedzic@tieto.com local->int_scan_req->n_channels = n_ch; 990be4a4b6aSJohannes Berg } 991be4a4b6aSJohannes Berg 992be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 993be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 9947ca15a0aSSimon Wunderlich local->int_scan_req->scan_width = scan_width; 9955ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 9965ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 997f3b85252SJohannes Berg 9985ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 999f3b85252SJohannes Berg unlock: 1000a1699b75SJohannes Berg mutex_unlock(&local->mtx); 1001f3b85252SJohannes Berg return ret; 10020a51b27eSJohannes Berg } 10035bb644a0SJohannes Berg 10044136c422SStanislaw Gruszka /* 10054136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 10064136c422SStanislaw Gruszka */ 10075bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 10085bb644a0SJohannes Berg { 10095bb644a0SJohannes Berg /* 1010b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 10114136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 10124136c422SStanislaw Gruszka * 10134136c422SStanislaw Gruszka * Regarding hardware scan: 10144136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 10154136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 10164136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 10174136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 10184136c422SStanislaw Gruszka * 10194136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 10204136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 10214136c422SStanislaw Gruszka * 1022b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 1023b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 1024b856439bSEliad Peller * after the scan was completed/aborted. 10255bb644a0SJohannes Berg */ 10264136c422SStanislaw Gruszka 1027a1699b75SJohannes Berg mutex_lock(&local->mtx); 1028b856439bSEliad Peller if (!local->scan_req) 1029b856439bSEliad Peller goto out; 1030b856439bSEliad Peller 1031a754055aSEmmanuel Grumbach /* 1032a754055aSEmmanuel Grumbach * We have a scan running and the driver already reported completion, 1033a754055aSEmmanuel Grumbach * but the worker hasn't run yet or is stuck on the mutex - mark it as 1034a754055aSEmmanuel Grumbach * cancelled. 1035a754055aSEmmanuel Grumbach */ 1036a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 1037a754055aSEmmanuel Grumbach test_bit(SCAN_COMPLETED, &local->scanning)) { 1038a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1039a754055aSEmmanuel Grumbach goto out; 1040a754055aSEmmanuel Grumbach } 1041a754055aSEmmanuel Grumbach 1042b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 1043a754055aSEmmanuel Grumbach /* 1044a754055aSEmmanuel Grumbach * Make sure that __ieee80211_scan_completed doesn't trigger a 1045a754055aSEmmanuel Grumbach * scan on another band. 1046a754055aSEmmanuel Grumbach */ 1047a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1048b856439bSEliad Peller if (local->ops->cancel_hw_scan) 1049e2fd5dbcSJohannes Berg drv_cancel_hw_scan(local, 1050e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 1051e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx))); 1052b856439bSEliad Peller goto out; 1053b856439bSEliad Peller } 1054b856439bSEliad Peller 1055d07bfd8bSJohannes Berg /* 1056d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 1057d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 1058d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 1059d07bfd8bSJohannes Berg */ 1060d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 1061d07bfd8bSJohannes Berg /* and clean up */ 10628bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, true); 1063b856439bSEliad Peller out: 1064d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 10655bb644a0SJohannes Berg } 106679f460caSLuciano Coelho 1067d43c6b6eSDavid Spinadel int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 106879f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 106979f460caSLuciano Coelho { 107079f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 1071633e2713SDavid Spinadel struct ieee80211_scan_ies sched_scan_ies = {}; 10722103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 1073633e2713SDavid Spinadel int ret, i, iebufsz, num_bands = 0; 1074633e2713SDavid Spinadel u32 rate_masks[IEEE80211_NUM_BANDS] = {}; 1075633e2713SDavid Spinadel u8 bands_used = 0; 1076633e2713SDavid Spinadel u8 *ie; 1077633e2713SDavid Spinadel size_t len; 1078c604b9f2SJohannes Berg 1079e4dcbb37SDavid Spinadel iebufsz = local->scan_ies_len + req->ie_len; 108079f460caSLuciano Coelho 1081d43c6b6eSDavid Spinadel lockdep_assert_held(&local->mtx); 108279f460caSLuciano Coelho 1083d43c6b6eSDavid Spinadel if (!local->ops->sched_scan_start) 1084d43c6b6eSDavid Spinadel return -ENOTSUPP; 108579f460caSLuciano Coelho 108679f460caSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) { 1087633e2713SDavid Spinadel if (local->hw.wiphy->bands[i]) { 1088633e2713SDavid Spinadel bands_used |= BIT(i); 1089633e2713SDavid Spinadel rate_masks[i] = (u32) -1; 1090633e2713SDavid Spinadel num_bands++; 1091633e2713SDavid Spinadel } 1092633e2713SDavid Spinadel } 1093c56ef672SDavid Spinadel 1094633e2713SDavid Spinadel ie = kzalloc(num_bands * iebufsz, GFP_KERNEL); 1095633e2713SDavid Spinadel if (!ie) { 109679f460caSLuciano Coelho ret = -ENOMEM; 1097633e2713SDavid Spinadel goto out; 109879f460caSLuciano Coelho } 109979f460caSLuciano Coelho 11002103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 11012103dec1SSimon Wunderlich 1102633e2713SDavid Spinadel len = ieee80211_build_preq_ies(local, ie, num_bands * iebufsz, 1103633e2713SDavid Spinadel &sched_scan_ies, req->ie, 1104633e2713SDavid Spinadel req->ie_len, bands_used, 1105c56ef672SDavid Spinadel rate_masks, &chandef); 110679f460caSLuciano Coelho 110730dd3edfSJohannes Berg ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 1108d43c6b6eSDavid Spinadel if (ret == 0) { 11095260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, sdata); 11106ea0a69cSJohannes Berg rcu_assign_pointer(local->sched_scan_req, req); 1111d43c6b6eSDavid Spinadel } 111279f460caSLuciano Coelho 1113633e2713SDavid Spinadel kfree(ie); 1114d43c6b6eSDavid Spinadel 1115633e2713SDavid Spinadel out: 1116d43c6b6eSDavid Spinadel if (ret) { 1117d43c6b6eSDavid Spinadel /* Clean in case of failure after HW restart or upon resume. */ 11180c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 11196ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1120d43c6b6eSDavid Spinadel } 1121d43c6b6eSDavid Spinadel 1122d43c6b6eSDavid Spinadel return ret; 1123d43c6b6eSDavid Spinadel } 1124d43c6b6eSDavid Spinadel 1125d43c6b6eSDavid Spinadel int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1126d43c6b6eSDavid Spinadel struct cfg80211_sched_scan_request *req) 1127d43c6b6eSDavid Spinadel { 1128d43c6b6eSDavid Spinadel struct ieee80211_local *local = sdata->local; 1129d43c6b6eSDavid Spinadel int ret; 1130d43c6b6eSDavid Spinadel 1131d43c6b6eSDavid Spinadel mutex_lock(&local->mtx); 1132d43c6b6eSDavid Spinadel 1133d43c6b6eSDavid Spinadel if (rcu_access_pointer(local->sched_scan_sdata)) { 1134d43c6b6eSDavid Spinadel mutex_unlock(&local->mtx); 1135d43c6b6eSDavid Spinadel return -EBUSY; 1136d43c6b6eSDavid Spinadel } 1137d43c6b6eSDavid Spinadel 1138d43c6b6eSDavid Spinadel ret = __ieee80211_request_sched_scan_start(sdata, req); 1139d43c6b6eSDavid Spinadel 11405260a5b2SJohannes Berg mutex_unlock(&local->mtx); 114179f460caSLuciano Coelho return ret; 114279f460caSLuciano Coelho } 114379f460caSLuciano Coelho 114485a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata) 114579f460caSLuciano Coelho { 114679f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 114730dd3edfSJohannes Berg int ret = 0; 114879f460caSLuciano Coelho 11495260a5b2SJohannes Berg mutex_lock(&local->mtx); 115079f460caSLuciano Coelho 115179f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 115279f460caSLuciano Coelho ret = -ENOTSUPP; 115379f460caSLuciano Coelho goto out; 115479f460caSLuciano Coelho } 115579f460caSLuciano Coelho 1156d43c6b6eSDavid Spinadel /* We don't want to restart sched scan anymore. */ 11576ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1158d43c6b6eSDavid Spinadel 115971228a1eSAlexander Bondar if (rcu_access_pointer(local->sched_scan_sdata)) { 116037e3308cSJohannes Berg ret = drv_sched_scan_stop(local, sdata); 116171228a1eSAlexander Bondar if (!ret) 1162ad053a96SAndreea-Cristina Bernat RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 116371228a1eSAlexander Bondar } 116479f460caSLuciano Coelho out: 11655260a5b2SJohannes Berg mutex_unlock(&local->mtx); 116679f460caSLuciano Coelho 116779f460caSLuciano Coelho return ret; 116879f460caSLuciano Coelho } 116979f460caSLuciano Coelho 117079f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 117179f460caSLuciano Coelho { 117279f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 117379f460caSLuciano Coelho 117479f460caSLuciano Coelho trace_api_sched_scan_results(local); 117579f460caSLuciano Coelho 117679f460caSLuciano Coelho cfg80211_sched_scan_results(hw->wiphy); 117779f460caSLuciano Coelho } 117879f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 117979f460caSLuciano Coelho 1180f6837ba8SJohannes Berg void ieee80211_sched_scan_end(struct ieee80211_local *local) 118185a9994aSLuciano Coelho { 118285a9994aSLuciano Coelho mutex_lock(&local->mtx); 118385a9994aSLuciano Coelho 11845260a5b2SJohannes Berg if (!rcu_access_pointer(local->sched_scan_sdata)) { 118585a9994aSLuciano Coelho mutex_unlock(&local->mtx); 118685a9994aSLuciano Coelho return; 118785a9994aSLuciano Coelho } 118885a9994aSLuciano Coelho 11890c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 119085a9994aSLuciano Coelho 1191d43c6b6eSDavid Spinadel /* If sched scan was aborted by the driver. */ 11926ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1193d43c6b6eSDavid Spinadel 119485a9994aSLuciano Coelho mutex_unlock(&local->mtx); 119585a9994aSLuciano Coelho 119685a9994aSLuciano Coelho cfg80211_sched_scan_stopped(local->hw.wiphy); 119785a9994aSLuciano Coelho } 119885a9994aSLuciano Coelho 1199f6837ba8SJohannes Berg void ieee80211_sched_scan_stopped_work(struct work_struct *work) 1200f6837ba8SJohannes Berg { 1201f6837ba8SJohannes Berg struct ieee80211_local *local = 1202f6837ba8SJohannes Berg container_of(work, struct ieee80211_local, 1203f6837ba8SJohannes Berg sched_scan_stopped_work); 1204f6837ba8SJohannes Berg 1205f6837ba8SJohannes Berg ieee80211_sched_scan_end(local); 1206f6837ba8SJohannes Berg } 1207f6837ba8SJohannes Berg 120879f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 120979f460caSLuciano Coelho { 121079f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 121179f460caSLuciano Coelho 121279f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 121379f460caSLuciano Coelho 121418db594aSJohannes Berg schedule_work(&local->sched_scan_stopped_work); 121579f460caSLuciano Coelho } 121679f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1217