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> 90a51b27eSJohannes Berg * 100a51b27eSJohannes Berg * This program is free software; you can redistribute it and/or modify 110a51b27eSJohannes Berg * it under the terms of the GNU General Public License version 2 as 120a51b27eSJohannes Berg * published by the Free Software Foundation. 130a51b27eSJohannes Berg */ 140a51b27eSJohannes Berg 150a51b27eSJohannes Berg #include <linux/if_arp.h> 16888d04dfSFelix Fietkau #include <linux/etherdevice.h> 17078e1e60SJohannes Berg #include <linux/rtnetlink.h> 18e8db0be1SJean Pihet #include <linux/pm_qos.h> 19df13cce5SHelmut Schaa #include <net/sch_generic.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 21bc3b2d7fSPaul Gortmaker #include <linux/export.h> 220a51b27eSJohannes Berg #include <net/mac80211.h> 230a51b27eSJohannes Berg 240a51b27eSJohannes Berg #include "ieee80211_i.h" 2524487981SJohannes Berg #include "driver-ops.h" 265484e237SJohannes Berg #include "mesh.h" 270a51b27eSJohannes Berg 280a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33) 290a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33) 303f892b61SStanislaw Gruszka #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9) 310a51b27eSJohannes Berg 325484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local, 33c2b13452SJohannes Berg struct ieee80211_bss *bss) 345484e237SJohannes Berg { 350c1ad2caSJohannes Berg if (!bss) 360c1ad2caSJohannes Berg return; 375b112d3dSJohannes Berg cfg80211_put_bss(local->hw.wiphy, 385b112d3dSJohannes Berg container_of((void *)bss, struct cfg80211_bss, priv)); 395484e237SJohannes Berg } 405484e237SJohannes Berg 41ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems) 42ab13315aSKalle Valo { 43ab13315aSKalle Valo u8 qos_info; 44ab13315aSKalle Valo 45ab13315aSKalle Valo if (elems->wmm_info && elems->wmm_info_len == 7 46ab13315aSKalle Valo && elems->wmm_info[5] == 1) 47ab13315aSKalle Valo qos_info = elems->wmm_info[6]; 48ab13315aSKalle Valo else if (elems->wmm_param && elems->wmm_param_len == 24 49ab13315aSKalle Valo && elems->wmm_param[5] == 1) 50ab13315aSKalle Valo qos_info = elems->wmm_param[6]; 51ab13315aSKalle Valo else 52ab13315aSKalle Valo /* no valid wmm information or parameter element found */ 53ab13315aSKalle Valo return false; 54ab13315aSKalle Valo 55ab13315aSKalle Valo return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD; 56ab13315aSKalle Valo } 57ab13315aSKalle Valo 58c2b13452SJohannes Berg struct ieee80211_bss * 595484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local, 605484e237SJohannes Berg struct ieee80211_rx_status *rx_status, 61d45c4172SEmmanuel Grumbach struct ieee80211_mgmt *mgmt, size_t len, 625484e237SJohannes Berg struct ieee802_11_elems *elems, 63d45c4172SEmmanuel Grumbach struct ieee80211_channel *channel) 645484e237SJohannes Berg { 65d45c4172SEmmanuel Grumbach bool beacon = ieee80211_is_beacon(mgmt->frame_control); 660c1ad2caSJohannes Berg struct cfg80211_bss *cbss; 67c2b13452SJohannes Berg struct ieee80211_bss *bss; 68f0b058b6SStanislaw Gruszka int clen, srlen; 69*7ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width; 702a519311SJohannes Berg s32 signal = 0; 712a519311SJohannes Berg 7277965c97SJohannes Berg if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) 732a519311SJohannes Berg signal = rx_status->signal * 100; 7477965c97SJohannes Berg else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) 752a519311SJohannes Berg signal = (rx_status->signal * 100) / local->hw.max_signal; 762a519311SJohannes Berg 77*7ca15a0aSSimon Wunderlich scan_width = NL80211_BSS_CHAN_WIDTH_20; 78*7ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_5MHZ) 79*7ca15a0aSSimon Wunderlich scan_width = NL80211_BSS_CHAN_WIDTH_5; 80*7ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_10MHZ) 81*7ca15a0aSSimon Wunderlich scan_width = NL80211_BSS_CHAN_WIDTH_10; 82*7ca15a0aSSimon Wunderlich 83*7ca15a0aSSimon Wunderlich cbss = cfg80211_inform_bss_width_frame(local->hw.wiphy, channel, 84*7ca15a0aSSimon Wunderlich scan_width, mgmt, len, signal, 85*7ca15a0aSSimon Wunderlich GFP_ATOMIC); 860c1ad2caSJohannes Berg if (!cbss) 875484e237SJohannes Berg return NULL; 8800d3f14cSJohannes Berg 890c1ad2caSJohannes Berg bss = (void *)cbss->priv; 905484e237SJohannes Berg 91ef429dadSJohannes Berg if (beacon) 92ef429dadSJohannes Berg bss->device_ts_beacon = rx_status->device_timestamp; 93ef429dadSJohannes Berg else 94ef429dadSJohannes Berg bss->device_ts_presp = rx_status->device_timestamp; 958c358bcdSJohannes Berg 96fcff4f10SPaul Stewart if (elems->parse_error) { 97fcff4f10SPaul Stewart if (beacon) 98fcff4f10SPaul Stewart bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON; 99fcff4f10SPaul Stewart else 100fcff4f10SPaul Stewart bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP; 101fcff4f10SPaul Stewart } else { 102fcff4f10SPaul Stewart if (beacon) 103fcff4f10SPaul Stewart bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON; 104fcff4f10SPaul Stewart else 105fcff4f10SPaul Stewart bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP; 1065484e237SJohannes Berg } 1075484e237SJohannes Berg 108fcff4f10SPaul Stewart /* save the ERP value so that it is available at association time */ 1091946bed9SJohannes Berg if (elems->erp_info && (!elems->parse_error || 110fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) { 111fcff4f10SPaul Stewart bss->erp_value = elems->erp_info[0]; 112fcff4f10SPaul Stewart bss->has_erp_value = true; 113fcff4f10SPaul Stewart if (!elems->parse_error) 114fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_ERP; 115fcff4f10SPaul Stewart } 116fcff4f10SPaul Stewart 117f0b058b6SStanislaw Gruszka /* replace old supported rates if we get new values */ 118fcff4f10SPaul Stewart if (!elems->parse_error || 119fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) { 120f0b058b6SStanislaw Gruszka srlen = 0; 1215484e237SJohannes Berg if (elems->supp_rates) { 122f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES; 1235484e237SJohannes Berg if (clen > elems->supp_rates_len) 1245484e237SJohannes Berg clen = elems->supp_rates_len; 125f0b058b6SStanislaw Gruszka memcpy(bss->supp_rates, elems->supp_rates, clen); 126f0b058b6SStanislaw Gruszka srlen += clen; 1275484e237SJohannes Berg } 1285484e237SJohannes Berg if (elems->ext_supp_rates) { 129f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES - srlen; 1305484e237SJohannes Berg if (clen > elems->ext_supp_rates_len) 1315484e237SJohannes Berg clen = elems->ext_supp_rates_len; 132fcff4f10SPaul Stewart memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, 133fcff4f10SPaul Stewart clen); 134f0b058b6SStanislaw Gruszka srlen += clen; 1355484e237SJohannes Berg } 136fcff4f10SPaul Stewart if (srlen) { 137f0b058b6SStanislaw Gruszka bss->supp_rates_len = srlen; 138fcff4f10SPaul Stewart if (!elems->parse_error) 139fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_RATES; 140fcff4f10SPaul Stewart } 141fcff4f10SPaul Stewart } 1425484e237SJohannes Berg 143fcff4f10SPaul Stewart if (!elems->parse_error || 144fcff4f10SPaul Stewart !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) { 1455484e237SJohannes Berg bss->wmm_used = elems->wmm_param || elems->wmm_info; 146ab13315aSKalle Valo bss->uapsd_supported = is_uapsd_supported(elems); 147fcff4f10SPaul Stewart if (!elems->parse_error) 148fcff4f10SPaul Stewart bss->valid_data |= IEEE80211_BSS_VALID_WMM; 149fcff4f10SPaul Stewart } 1505484e237SJohannes Berg 151817cee76SAlexander Bondar if (beacon) { 152817cee76SAlexander Bondar struct ieee80211_supported_band *sband = 153817cee76SAlexander Bondar local->hw.wiphy->bands[rx_status->band]; 154817cee76SAlexander Bondar if (!(rx_status->flag & RX_FLAG_HT) && 155817cee76SAlexander Bondar !(rx_status->flag & RX_FLAG_VHT)) 156817cee76SAlexander Bondar bss->beacon_rate = 157817cee76SAlexander Bondar &sband->bitrates[rx_status->rate_idx]; 158817cee76SAlexander Bondar } 159817cee76SAlexander Bondar 1605484e237SJohannes Berg return bss; 1615484e237SJohannes Berg } 1620a51b27eSJohannes Berg 163d48b2968SJohannes Berg void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb) 16498c8fccfSJohannes Berg { 165f1d58c25SJohannes Berg struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 166d48b2968SJohannes Berg struct ieee80211_sub_if_data *sdata1, *sdata2; 167d48b2968SJohannes Berg struct ieee80211_mgmt *mgmt = (void *)skb->data; 168c2b13452SJohannes Berg struct ieee80211_bss *bss; 16998c8fccfSJohannes Berg u8 *elements; 17098c8fccfSJohannes Berg struct ieee80211_channel *channel; 17198c8fccfSJohannes Berg size_t baselen; 17298c8fccfSJohannes Berg struct ieee802_11_elems elems; 17398c8fccfSJohannes Berg 174d48b2968SJohannes Berg if (skb->len < 24 || 175d48b2968SJohannes Berg (!ieee80211_is_probe_resp(mgmt->frame_control) && 176d48b2968SJohannes Berg !ieee80211_is_beacon(mgmt->frame_control))) 177d48b2968SJohannes Berg return; 17898c8fccfSJohannes Berg 179d48b2968SJohannes Berg sdata1 = rcu_dereference(local->scan_sdata); 180d48b2968SJohannes Berg sdata2 = rcu_dereference(local->sched_scan_sdata); 18198c8fccfSJohannes Berg 182d48b2968SJohannes Berg if (likely(!sdata1 && !sdata2)) 183d48b2968SJohannes Berg return; 18498c8fccfSJohannes Berg 185d48b2968SJohannes Berg if (ieee80211_is_probe_resp(mgmt->frame_control)) { 18698c8fccfSJohannes Berg /* ignore ProbeResp to foreign address */ 187d48b2968SJohannes Berg if ((!sdata1 || !ether_addr_equal(mgmt->da, sdata1->vif.addr)) && 188d48b2968SJohannes Berg (!sdata2 || !ether_addr_equal(mgmt->da, sdata2->vif.addr))) 189d48b2968SJohannes Berg return; 19098c8fccfSJohannes Berg 19198c8fccfSJohannes Berg elements = mgmt->u.probe_resp.variable; 19298c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); 19398c8fccfSJohannes Berg } else { 19498c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 19598c8fccfSJohannes Berg elements = mgmt->u.beacon.variable; 19698c8fccfSJohannes Berg } 19798c8fccfSJohannes Berg 19898c8fccfSJohannes Berg if (baselen > skb->len) 199d48b2968SJohannes Berg return; 20098c8fccfSJohannes Berg 201b2e506bfSJohannes Berg ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems); 20298c8fccfSJohannes Berg 2030172bb75SJohannes Berg channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 20498c8fccfSJohannes Berg 20598c8fccfSJohannes Berg if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 206d48b2968SJohannes Berg return; 20798c8fccfSJohannes Berg 208d48b2968SJohannes Berg bss = ieee80211_bss_info_update(local, rx_status, 20998c8fccfSJohannes Berg mgmt, skb->len, &elems, 210d45c4172SEmmanuel Grumbach channel); 211d048e503SJouni Malinen if (bss) 212d48b2968SJohannes Berg ieee80211_rx_bss_put(local, bss); 21398c8fccfSJohannes Berg } 21498c8fccfSJohannes Berg 2152103dec1SSimon Wunderlich static void 2162103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef, 2172103dec1SSimon Wunderlich enum nl80211_bss_scan_width scan_width) 2182103dec1SSimon Wunderlich { 2192103dec1SSimon Wunderlich memset(chandef, 0, sizeof(*chandef)); 2202103dec1SSimon Wunderlich switch (scan_width) { 2212103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 2222103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_5; 2232103dec1SSimon Wunderlich break; 2242103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 2252103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_10; 2262103dec1SSimon Wunderlich break; 2272103dec1SSimon Wunderlich default: 2282103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 2292103dec1SSimon Wunderlich break; 2302103dec1SSimon Wunderlich } 2312103dec1SSimon Wunderlich } 2322103dec1SSimon Wunderlich 2334d36ec58SJohannes Berg /* return false if no more work */ 2344d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 2354d36ec58SJohannes Berg { 2364d36ec58SJohannes Berg struct cfg80211_scan_request *req = local->scan_req; 2372103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 2384d36ec58SJohannes Berg enum ieee80211_band band; 2394d36ec58SJohannes Berg int i, ielen, n_chans; 2404d36ec58SJohannes Berg 2414d36ec58SJohannes Berg do { 2424d36ec58SJohannes Berg if (local->hw_scan_band == IEEE80211_NUM_BANDS) 2434d36ec58SJohannes Berg return false; 2444d36ec58SJohannes Berg 2454d36ec58SJohannes Berg band = local->hw_scan_band; 2464d36ec58SJohannes Berg n_chans = 0; 2474d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 2484d36ec58SJohannes Berg if (req->channels[i]->band == band) { 2494d36ec58SJohannes Berg local->hw_scan_req->channels[n_chans] = 2504d36ec58SJohannes Berg req->channels[i]; 2514d36ec58SJohannes Berg n_chans++; 2524d36ec58SJohannes Berg } 2534d36ec58SJohannes Berg } 2544d36ec58SJohannes Berg 2554d36ec58SJohannes Berg local->hw_scan_band++; 2564d36ec58SJohannes Berg } while (!n_chans); 2574d36ec58SJohannes Berg 2584d36ec58SJohannes Berg local->hw_scan_req->n_channels = n_chans; 2592103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 2604d36ec58SJohannes Berg 2614d36ec58SJohannes Berg ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie, 262c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, 2638ee31080SJohannes Berg req->ie, req->ie_len, band, 2642103dec1SSimon Wunderlich req->rates[band], &chandef); 2654d36ec58SJohannes Berg local->hw_scan_req->ie_len = ielen; 266dcd83976SJohannes Berg local->hw_scan_req->no_cck = req->no_cck; 2674d36ec58SJohannes Berg 2684d36ec58SJohannes Berg return true; 2694d36ec58SJohannes Berg } 2704d36ec58SJohannes Berg 271d07bfd8bSJohannes Berg static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted, 272e229f844SStanislaw Gruszka bool was_hw_scan) 273f3b85252SJohannes Berg { 274f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 275f3b85252SJohannes Berg 276e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 277f3b85252SJohannes Berg 2786d3560d4SJohannes Berg /* 2796d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 2806d3560d4SJohannes Berg * we have one at all will be verified by checking 2816d3560d4SJohannes Berg * local->scan_req next), but not to complete it 2826d3560d4SJohannes Berg * successfully. 2836d3560d4SJohannes Berg */ 2846d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 2856d3560d4SJohannes Berg aborted = true; 286f3b85252SJohannes Berg 287e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 288d07bfd8bSJohannes Berg return; 289f3b85252SJohannes Berg 2904d36ec58SJohannes Berg if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) { 291e2fd5dbcSJohannes Berg int rc; 292e2fd5dbcSJohannes Berg 293e2fd5dbcSJohannes Berg rc = drv_hw_scan(local, 294e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 295e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)), 296e2fd5dbcSJohannes Berg local->hw_scan_req); 297e2fd5dbcSJohannes Berg 2986eb11a9aSStanislaw Gruszka if (rc == 0) 299d07bfd8bSJohannes Berg return; 3004d36ec58SJohannes Berg } 3014d36ec58SJohannes Berg 3024d36ec58SJohannes Berg kfree(local->hw_scan_req); 3034d36ec58SJohannes Berg local->hw_scan_req = NULL; 304f3b85252SJohannes Berg 3055ba63533SJohannes Berg if (local->scan_req != local->int_scan_req) 3062a519311SJohannes Berg cfg80211_scan_done(local->scan_req, aborted); 3072a519311SJohannes Berg local->scan_req = NULL; 3083aa569c3SJohannes Berg rcu_assign_pointer(local->scan_sdata, NULL); 3092a519311SJohannes Berg 310fbe9c429SHelmut Schaa local->scanning = 0; 311*7ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 312f3b85252SJohannes Berg 31359bdf3b0SBen Greear /* Set power back to normal operating levels. */ 31459bdf3b0SBen Greear ieee80211_hw_config(local, 0); 315b23b025fSBen Greear 316b23b025fSBen Greear if (!was_hw_scan) { 317b23b025fSBen Greear ieee80211_configure_filter(local); 318b23b025fSBen Greear drv_sw_scan_complete(local); 319aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 320b23b025fSBen Greear } 321b23b025fSBen Greear 3225cff20e6SJohannes Berg ieee80211_recalc_idle(local); 323e229f844SStanislaw Gruszka 3240a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 32546900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 326472dbc45SJohannes Berg ieee80211_mesh_notify_scan_completed(local); 3272eb278e0SJohannes Berg ieee80211_start_next_roc(local); 3280a51b27eSJohannes Berg } 3298789d459SJohannes Berg 3308789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 3318789d459SJohannes Berg { 3328789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3338789d459SJohannes Berg 3348789d459SJohannes Berg trace_api_scan_completed(local, aborted); 3358789d459SJohannes Berg 3368789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 3378789d459SJohannes Berg if (aborted) 3388789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 3398789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 3408789d459SJohannes Berg } 3410a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 3420a51b27eSJohannes Berg 343f3b85252SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local) 344f3b85252SJohannes Berg { 345fe57d9f5SJohannes Berg /* Software scan is not supported in multi-channel cases */ 346fe57d9f5SJohannes Berg if (local->use_chanctx) 347fe57d9f5SJohannes Berg return -EOPNOTSUPP; 348fe57d9f5SJohannes Berg 349f3b85252SJohannes Berg /* 350f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 351f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 352f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 353f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 354f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 355f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 356f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 357f3b85252SJohannes Berg * 358f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 359f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 360f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 361f3b85252SJohannes Berg */ 36224487981SJohannes Berg drv_sw_scan_start(local); 363f3b85252SJohannes Berg 364de312db3SRajkumar Manoharan local->leave_oper_channel_time = jiffies; 365977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 366f3b85252SJohannes Berg local->scan_channel_idx = 0; 367f3b85252SJohannes Berg 368aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 369a80f7c0bSJohannes Berg 3709c35d7d2SSeth Forshee /* ensure nullfunc is transmitted before leaving operating channel */ 37139ecc01dSJohannes Berg ieee80211_flush_queues(local, NULL); 3729c35d7d2SSeth Forshee 3733ac64beeSJohannes Berg ieee80211_configure_filter(local); 374f3b85252SJohannes Berg 37559bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 37659bdf3b0SBen Greear ieee80211_hw_config(local, 0); 37759bdf3b0SBen Greear 37842935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 37907ef03eeSJohannes Berg &local->scan_work, 0); 380f3b85252SJohannes Berg 381f3b85252SJohannes Berg return 0; 382f3b85252SJohannes Berg } 383f3b85252SJohannes Berg 384133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local, 385133d40f9SStanislaw Gruszka struct ieee80211_sub_if_data *sdata) 386133d40f9SStanislaw Gruszka { 387164eb02dSSimon Wunderlich if (local->radar_detect_enabled) 388164eb02dSSimon Wunderlich return false; 389164eb02dSSimon Wunderlich 3902eb278e0SJohannes Berg if (!list_empty(&local->roc_list)) 391133d40f9SStanislaw Gruszka return false; 392133d40f9SStanislaw Gruszka 393133d40f9SStanislaw Gruszka if (sdata->vif.type == NL80211_IFTYPE_STATION && 394133d40f9SStanislaw Gruszka sdata->u.mgd.flags & (IEEE80211_STA_BEACON_POLL | 395133d40f9SStanislaw Gruszka IEEE80211_STA_CONNECTION_POLL)) 396133d40f9SStanislaw Gruszka return false; 397133d40f9SStanislaw Gruszka 398133d40f9SStanislaw Gruszka return true; 399133d40f9SStanislaw Gruszka } 400133d40f9SStanislaw Gruszka 401133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local) 402133d40f9SStanislaw Gruszka { 403133d40f9SStanislaw Gruszka lockdep_assert_held(&local->mtx); 404133d40f9SStanislaw Gruszka 405133d40f9SStanislaw Gruszka if (!local->scan_req || local->scanning) 406133d40f9SStanislaw Gruszka return; 407133d40f9SStanislaw Gruszka 408e2fd5dbcSJohannes Berg if (!ieee80211_can_scan(local, 409e2fd5dbcSJohannes Berg rcu_dereference_protected( 410e2fd5dbcSJohannes Berg local->scan_sdata, 411e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)))) 412133d40f9SStanislaw Gruszka return; 413133d40f9SStanislaw Gruszka 414133d40f9SStanislaw Gruszka ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 415133d40f9SStanislaw Gruszka round_jiffies_relative(0)); 416133d40f9SStanislaw Gruszka } 417f3b85252SJohannes Berg 4188a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 4198a690674SBen Greear unsigned long *next_delay) 4208a690674SBen Greear { 4218a690674SBen Greear int i; 422e2fd5dbcSJohannes Berg struct ieee80211_sub_if_data *sdata; 423675a0b04SKarl Beldan enum ieee80211_band band = local->hw.conf.chandef.chan->band; 4246c17b77bSSeth Forshee u32 tx_flags; 4256c17b77bSSeth Forshee 4266c17b77bSSeth Forshee tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 4276c17b77bSSeth Forshee if (local->scan_req->no_cck) 4286c17b77bSSeth Forshee tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 4298a690674SBen Greear 430e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 431316b6b5dSPeter Senna Tschudin lockdep_is_held(&local->mtx)); 432e2fd5dbcSJohannes Berg 4338a690674SBen Greear for (i = 0; i < local->scan_req->n_ssids; i++) 4348a690674SBen Greear ieee80211_send_probe_req( 4358a690674SBen Greear sdata, NULL, 4368a690674SBen Greear local->scan_req->ssids[i].ssid, 4378a690674SBen Greear local->scan_req->ssids[i].ssid_len, 4388a690674SBen Greear local->scan_req->ie, local->scan_req->ie_len, 4398a690674SBen Greear local->scan_req->rates[band], false, 440675a0b04SKarl Beldan tx_flags, local->hw.conf.chandef.chan, true); 4418a690674SBen Greear 4428a690674SBen Greear /* 4438a690674SBen Greear * After sending probe requests, wait for probe responses 4448a690674SBen Greear * on the channel. 4458a690674SBen Greear */ 4468a690674SBen Greear *next_delay = IEEE80211_CHANNEL_TIME; 4478a690674SBen Greear local->next_scan_state = SCAN_DECISION; 4488a690674SBen Greear } 4498a690674SBen Greear 450f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 451f3b85252SJohannes Berg struct cfg80211_scan_request *req) 452f3b85252SJohannes Berg { 453f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 454f3b85252SJohannes Berg int rc; 455f3b85252SJohannes Berg 456e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 457e229f844SStanislaw Gruszka 458f3b85252SJohannes Berg if (local->scan_req) 459f3b85252SJohannes Berg return -EBUSY; 460f3b85252SJohannes Berg 461133d40f9SStanislaw Gruszka if (!ieee80211_can_scan(local, sdata)) { 4626e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 463c0ce77b8SJohannes Berg local->scan_req = req; 464e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 465c0ce77b8SJohannes Berg return 0; 466c0ce77b8SJohannes Berg } 467c0ce77b8SJohannes Berg 468f3b85252SJohannes Berg if (local->ops->hw_scan) { 469f3b85252SJohannes Berg u8 *ies; 470f3b85252SJohannes Berg 471c604b9f2SJohannes Berg local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN + 472c604b9f2SJohannes Berg local->scan_ies_len + 473c604b9f2SJohannes Berg req->ie_len; 4744d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 4754d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 4764d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 477c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, GFP_KERNEL); 4784d36ec58SJohannes Berg if (!local->hw_scan_req) 479f3b85252SJohannes Berg return -ENOMEM; 480f3b85252SJohannes Berg 4814d36ec58SJohannes Berg local->hw_scan_req->ssids = req->ssids; 4824d36ec58SJohannes Berg local->hw_scan_req->n_ssids = req->n_ssids; 4834d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 4844d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 4854d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 4864d36ec58SJohannes Berg local->hw_scan_req->ie = ies; 487cd2bb512SSam Leffler local->hw_scan_req->flags = req->flags; 4884d36ec58SJohannes Berg 4894d36ec58SJohannes Berg local->hw_scan_band = 0; 4906e7e6213SJohn W. Linville 4916e7e6213SJohn W. Linville /* 4926e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 4936e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 4946e7e6213SJohn W. Linville * anything that might be changed here and leave 4956e7e6213SJohn W. Linville * this function early must not go after this 4966e7e6213SJohn W. Linville * allocation. 4976e7e6213SJohn W. Linville */ 498f3b85252SJohannes Berg } 499f3b85252SJohannes Berg 500f3b85252SJohannes Berg local->scan_req = req; 501e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 502f3b85252SJohannes Berg 5038a690674SBen Greear if (local->ops->hw_scan) { 504fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 5058a690674SBen Greear } else if ((req->n_channels == 1) && 506675a0b04SKarl Beldan (req->channels[0] == local->_oper_chandef.chan)) { 5079b864870SJohannes Berg /* 5089b864870SJohannes Berg * If we are scanning only on the operating channel 5099b864870SJohannes Berg * then we do not need to stop normal activities 5108a690674SBen Greear */ 5118a690674SBen Greear unsigned long next_delay; 5128a690674SBen Greear 5138a690674SBen Greear __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 5148a690674SBen Greear 5158a690674SBen Greear ieee80211_recalc_idle(local); 5168a690674SBen Greear 5178a690674SBen Greear /* Notify driver scan is starting, keep order of operations 5188a690674SBen Greear * same as normal software scan, in case that matters. */ 5198a690674SBen Greear drv_sw_scan_start(local); 5208a690674SBen Greear 5218a690674SBen Greear ieee80211_configure_filter(local); /* accept probe-responses */ 5228a690674SBen Greear 5238a690674SBen Greear /* We need to ensure power level is at max for scanning. */ 5248a690674SBen Greear ieee80211_hw_config(local, 0); 5258a690674SBen Greear 5268a690674SBen Greear if ((req->channels[0]->flags & 5278a690674SBen Greear IEEE80211_CHAN_PASSIVE_SCAN) || 5288a690674SBen Greear !local->scan_req->n_ssids) { 5298a690674SBen Greear next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 5308a690674SBen Greear } else { 5318a690674SBen Greear ieee80211_scan_state_send_probe(local, &next_delay); 5328a690674SBen Greear next_delay = IEEE80211_CHANNEL_TIME; 5338a690674SBen Greear } 5348a690674SBen Greear 5358a690674SBen Greear /* Now, just wait a bit and we are all done! */ 5368a690674SBen Greear ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 5378a690674SBen Greear next_delay); 5388a690674SBen Greear return 0; 5398a690674SBen Greear } else { 5408a690674SBen Greear /* Do normal software scan */ 541fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 5428a690674SBen Greear } 5436e7e6213SJohn W. Linville 5445cff20e6SJohannes Berg ieee80211_recalc_idle(local); 545f3b85252SJohannes Berg 5464d36ec58SJohannes Berg if (local->ops->hw_scan) { 5474d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 548a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 5494d36ec58SJohannes Berg } else 550f3b85252SJohannes Berg rc = ieee80211_start_sw_scan(local); 551f3b85252SJohannes Berg 552f3b85252SJohannes Berg if (rc) { 5534d36ec58SJohannes Berg kfree(local->hw_scan_req); 5544d36ec58SJohannes Berg local->hw_scan_req = NULL; 555fbe9c429SHelmut Schaa local->scanning = 0; 556f3b85252SJohannes Berg 5575cff20e6SJohannes Berg ieee80211_recalc_idle(local); 5585cff20e6SJohannes Berg 559f3b85252SJohannes Berg local->scan_req = NULL; 560e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, NULL); 561f3b85252SJohannes Berg } 562f3b85252SJohannes Berg 563f3b85252SJohannes Berg return rc; 564f3b85252SJohannes Berg } 565f3b85252SJohannes Berg 566df13cce5SHelmut Schaa static unsigned long 567df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 568df13cce5SHelmut Schaa { 569df13cce5SHelmut Schaa /* 570df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 571df13cce5SHelmut Schaa * add that delay as well to get a better estimation 572df13cce5SHelmut Schaa */ 573df13cce5SHelmut Schaa if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) 574df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 575df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 576df13cce5SHelmut Schaa } 577df13cce5SHelmut Schaa 578e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 5792fb3f028SHelmut Schaa unsigned long *next_delay) 5802fb3f028SHelmut Schaa { 581142b9f50SHelmut Schaa bool associated = false; 582df13cce5SHelmut Schaa bool tx_empty = true; 583df13cce5SHelmut Schaa bool bad_latency; 584142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 585df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 586cd2bb512SSam Leffler enum mac80211_scan_state next_scan_state; 587142b9f50SHelmut Schaa 588df13cce5SHelmut Schaa /* 589df13cce5SHelmut Schaa * check if at least one STA interface is associated, 590df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 591df13cce5SHelmut Schaa * and grab the lowest used beacon interval 592df13cce5SHelmut Schaa */ 593142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 594142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 5959607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 596142b9f50SHelmut Schaa continue; 597142b9f50SHelmut Schaa 598142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 599142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 600142b9f50SHelmut Schaa associated = true; 601df13cce5SHelmut Schaa 602df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 603df13cce5SHelmut Schaa tx_empty = false; 604142b9f50SHelmut Schaa break; 605142b9f50SHelmut Schaa } 606142b9f50SHelmut Schaa } 607142b9f50SHelmut Schaa } 608df13cce5SHelmut Schaa } 609142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 610142b9f50SHelmut Schaa 611b23b025fSBen Greear next_chan = local->scan_req->channels[local->scan_channel_idx]; 612b23b025fSBen Greear 613142b9f50SHelmut Schaa /* 614142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 615df13cce5SHelmut Schaa * see if we can scan another channel without interfering 616df13cce5SHelmut Schaa * with the current traffic situation. 617df13cce5SHelmut Schaa * 6183f892b61SStanislaw Gruszka * Keep good latency, do not stay off-channel more than 125 ms. 619142b9f50SHelmut Schaa */ 620df13cce5SHelmut Schaa 621df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 622df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 6233f892b61SStanislaw Gruszka local->leave_oper_channel_time + HZ / 8); 624df13cce5SHelmut Schaa 625cd2bb512SSam Leffler if (associated && !tx_empty) { 626cd2bb512SSam Leffler if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 627cd2bb512SSam Leffler next_scan_state = SCAN_ABORT; 628142b9f50SHelmut Schaa else 629cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 6303f892b61SStanislaw Gruszka } else if (associated && bad_latency) { 631cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 632cd2bb512SSam Leffler } else { 633cd2bb512SSam Leffler next_scan_state = SCAN_SET_CHANNEL; 634cd2bb512SSam Leffler } 635cd2bb512SSam Leffler 636cd2bb512SSam Leffler local->next_scan_state = next_scan_state; 637142b9f50SHelmut Schaa 638142b9f50SHelmut Schaa *next_delay = 0; 6392fb3f028SHelmut Schaa } 6402fb3f028SHelmut Schaa 6412fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 6427d3be3ccSHelmut Schaa unsigned long *next_delay) 6437d3be3ccSHelmut Schaa { 6447d3be3ccSHelmut Schaa int skip; 6457d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 646*7ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width oper_scan_width; 6477d3be3ccSHelmut Schaa 6487d3be3ccSHelmut Schaa skip = 0; 6497d3be3ccSHelmut Schaa chan = local->scan_req->channels[local->scan_channel_idx]; 6507d3be3ccSHelmut Schaa 651*7ca15a0aSSimon Wunderlich local->scan_chandef.chan = chan; 652*7ca15a0aSSimon Wunderlich local->scan_chandef.center_freq1 = chan->center_freq; 653*7ca15a0aSSimon Wunderlich local->scan_chandef.center_freq2 = 0; 654*7ca15a0aSSimon Wunderlich switch (local->scan_req->scan_width) { 655*7ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 656*7ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 657*7ca15a0aSSimon Wunderlich break; 658*7ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 659*7ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 660*7ca15a0aSSimon Wunderlich break; 661*7ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_20: 662*7ca15a0aSSimon Wunderlich /* If scanning on oper channel, use whatever channel-type 663*7ca15a0aSSimon Wunderlich * is currently in use. 664*7ca15a0aSSimon Wunderlich */ 665*7ca15a0aSSimon Wunderlich oper_scan_width = cfg80211_chandef_to_scan_width( 666*7ca15a0aSSimon Wunderlich &local->_oper_chandef); 667*7ca15a0aSSimon Wunderlich if (chan == local->_oper_chandef.chan && 668*7ca15a0aSSimon Wunderlich oper_scan_width == local->scan_req->scan_width) 669*7ca15a0aSSimon Wunderlich local->scan_chandef = local->_oper_chandef; 670*7ca15a0aSSimon Wunderlich else 671*7ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 672*7ca15a0aSSimon Wunderlich break; 673*7ca15a0aSSimon Wunderlich } 674b23b025fSBen Greear 675584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 6767d3be3ccSHelmut Schaa skip = 1; 6777d3be3ccSHelmut Schaa 6787d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 6797d3be3ccSHelmut Schaa local->scan_channel_idx++; 6807d3be3ccSHelmut Schaa 6810ee9c13cSHelmut Schaa if (skip) { 6820ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 6830ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 6842fb3f028SHelmut Schaa return; 6850ee9c13cSHelmut Schaa } 6867d3be3ccSHelmut Schaa 6877d3be3ccSHelmut Schaa /* 6887d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 6897d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 6907d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 6917d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 6927d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 6937d3be3ccSHelmut Schaa * received. 6947d3be3ccSHelmut Schaa * 6957d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 6967d3be3ccSHelmut Schaa */ 6977d3be3ccSHelmut Schaa if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN || 6987d3be3ccSHelmut Schaa !local->scan_req->n_ssids) { 6997d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 700977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 7012fb3f028SHelmut Schaa return; 7027d3be3ccSHelmut Schaa } 7037d3be3ccSHelmut Schaa 7042fb3f028SHelmut Schaa /* active scan, send probes */ 7057d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 706977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 7077d3be3ccSHelmut Schaa } 7087d3be3ccSHelmut Schaa 70907ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 71007ef03eeSJohannes Berg unsigned long *next_delay) 71107ef03eeSJohannes Berg { 71207ef03eeSJohannes Berg /* switch back to the operating channel */ 713*7ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 71407ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 71507ef03eeSJohannes Berg 716aacde9eeSStanislaw Gruszka /* disable PS */ 717aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 71807ef03eeSJohannes Berg 71907ef03eeSJohannes Berg *next_delay = HZ / 5; 72007ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 72107ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 72207ef03eeSJohannes Berg } 72307ef03eeSJohannes Berg 72407ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 72507ef03eeSJohannes Berg unsigned long *next_delay) 72607ef03eeSJohannes Berg { 727aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 72807ef03eeSJohannes Berg 72907ef03eeSJohannes Berg if (local->ops->flush) { 73039ecc01dSJohannes Berg ieee80211_flush_queues(local, NULL); 73107ef03eeSJohannes Berg *next_delay = 0; 73207ef03eeSJohannes Berg } else 73307ef03eeSJohannes Berg *next_delay = HZ / 10; 73407ef03eeSJohannes Berg 73507ef03eeSJohannes Berg /* remember when we left the operating channel */ 73607ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 73707ef03eeSJohannes Berg 73807ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 739de2ee84dSMohammed Shafi Shajakhan local->next_scan_state = SCAN_SET_CHANNEL; 74007ef03eeSJohannes Berg } 74107ef03eeSJohannes Berg 742c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 7430a51b27eSJohannes Berg { 7440a51b27eSJohannes Berg struct ieee80211_local *local = 7450a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 746d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 7470a51b27eSJohannes Berg unsigned long next_delay = 0; 748d07bfd8bSJohannes Berg bool aborted, hw_scan; 7498789d459SJohannes Berg 750259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 751259b62e3SStanislaw Gruszka 752e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 753e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)); 754d07bfd8bSJohannes Berg 7558a690674SBen Greear /* When scanning on-channel, the first-callback means completed. */ 7568a690674SBen Greear if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 7578a690674SBen Greear aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 7588a690674SBen Greear goto out_complete; 7598a690674SBen Greear } 7608a690674SBen Greear 761259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 7628789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 763259b62e3SStanislaw Gruszka goto out_complete; 7648789d459SJohannes Berg } 7658789d459SJohannes Berg 766259b62e3SStanislaw Gruszka if (!sdata || !local->scan_req) 767259b62e3SStanislaw Gruszka goto out; 768f3b85252SJohannes Berg 769fbe9c429SHelmut Schaa if (local->scan_req && !local->scanning) { 770f3b85252SJohannes Berg struct cfg80211_scan_request *req = local->scan_req; 771f3b85252SJohannes Berg int rc; 772f3b85252SJohannes Berg 773f3b85252SJohannes Berg local->scan_req = NULL; 774e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, NULL); 775f3b85252SJohannes Berg 776f3b85252SJohannes Berg rc = __ieee80211_start_scan(sdata, req); 777259b62e3SStanislaw Gruszka if (rc) { 7783aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 7793aed49efSStanislaw Gruszka local->scan_req = req; 780259b62e3SStanislaw Gruszka aborted = true; 781259b62e3SStanislaw Gruszka goto out_complete; 782259b62e3SStanislaw Gruszka } else 783259b62e3SStanislaw Gruszka goto out; 784f3b85252SJohannes Berg } 785f3b85252SJohannes Berg 7865bc75728SJohannes Berg /* 7875bc75728SJohannes Berg * Avoid re-scheduling when the sdata is going away. 7885bc75728SJohannes Berg */ 7899607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) { 790259b62e3SStanislaw Gruszka aborted = true; 791259b62e3SStanislaw Gruszka goto out_complete; 792f3b85252SJohannes Berg } 7930a51b27eSJohannes Berg 794f502d09bSHelmut Schaa /* 795f502d09bSHelmut Schaa * as long as no delay is required advance immediately 796f502d09bSHelmut Schaa * without scheduling a new work 797f502d09bSHelmut Schaa */ 798f502d09bSHelmut Schaa do { 799c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 800c29acf20SRajkumar Manoharan aborted = true; 801c29acf20SRajkumar Manoharan goto out_complete; 802c29acf20SRajkumar Manoharan } 803c29acf20SRajkumar Manoharan 804977923b0SHelmut Schaa switch (local->next_scan_state) { 8052fb3f028SHelmut Schaa case SCAN_DECISION: 806e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 807e229f844SStanislaw Gruszka if (local->scan_channel_idx >= local->scan_req->n_channels) { 808e229f844SStanislaw Gruszka aborted = false; 809e229f844SStanislaw Gruszka goto out_complete; 810e229f844SStanislaw Gruszka } 811e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 8120a51b27eSJohannes Berg break; 8132fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 8142fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 8152fb3f028SHelmut Schaa break; 8160a51b27eSJohannes Berg case SCAN_SEND_PROBE: 8177d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 8180a51b27eSJohannes Berg break; 81907ef03eeSJohannes Berg case SCAN_SUSPEND: 82007ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 821142b9f50SHelmut Schaa break; 82207ef03eeSJohannes Berg case SCAN_RESUME: 82307ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 824142b9f50SHelmut Schaa break; 825cd2bb512SSam Leffler case SCAN_ABORT: 826cd2bb512SSam Leffler aborted = true; 827cd2bb512SSam Leffler goto out_complete; 8280a51b27eSJohannes Berg } 829f502d09bSHelmut Schaa } while (next_delay == 0); 8300a51b27eSJohannes Berg 83142935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 832d07bfd8bSJohannes Berg goto out; 833259b62e3SStanislaw Gruszka 834259b62e3SStanislaw Gruszka out_complete: 835e229f844SStanislaw Gruszka hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning); 836d07bfd8bSJohannes Berg __ieee80211_scan_completed(&local->hw, aborted, hw_scan); 837259b62e3SStanislaw Gruszka out: 838259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 8390a51b27eSJohannes Berg } 8400a51b27eSJohannes Berg 841c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 8422a519311SJohannes Berg struct cfg80211_scan_request *req) 8430a51b27eSJohannes Berg { 844f3b85252SJohannes Berg int res; 8450a51b27eSJohannes Berg 846a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 847f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 848a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 8492a519311SJohannes Berg 850f3b85252SJohannes Berg return res; 8510a51b27eSJohannes Berg } 8520a51b27eSJohannes Berg 85334bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 854be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 855*7ca15a0aSSimon Wunderlich struct ieee80211_channel *chan, 856*7ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width) 857f3b85252SJohannes Berg { 858f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 859f3b85252SJohannes Berg int ret = -EBUSY; 860fb63bc41SGertjan van Wingerde enum ieee80211_band band; 8619116dd01SJohannes Berg 862a1699b75SJohannes Berg mutex_lock(&local->mtx); 863f3b85252SJohannes Berg 864f3b85252SJohannes Berg /* busy scanning */ 865f3b85252SJohannes Berg if (local->scan_req) 866f3b85252SJohannes Berg goto unlock; 867f3b85252SJohannes Berg 868be4a4b6aSJohannes Berg /* fill internal scan request */ 869be4a4b6aSJohannes Berg if (!chan) { 87034bcf715SStanislaw Gruszka int i, max_n; 87134bcf715SStanislaw Gruszka int n_ch = 0; 872be4a4b6aSJohannes Berg 873be4a4b6aSJohannes Berg for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 874be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 875be4a4b6aSJohannes Berg continue; 87634bcf715SStanislaw Gruszka 87734bcf715SStanislaw Gruszka max_n = local->hw.wiphy->bands[band]->n_channels; 87834bcf715SStanislaw Gruszka for (i = 0; i < max_n; i++) { 87934bcf715SStanislaw Gruszka struct ieee80211_channel *tmp_ch = 880be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 88134bcf715SStanislaw Gruszka 88234bcf715SStanislaw Gruszka if (tmp_ch->flags & (IEEE80211_CHAN_NO_IBSS | 88334bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED)) 88434bcf715SStanislaw Gruszka continue; 88534bcf715SStanislaw Gruszka 88634bcf715SStanislaw Gruszka local->int_scan_req->channels[n_ch] = tmp_ch; 88734bcf715SStanislaw Gruszka n_ch++; 888be4a4b6aSJohannes Berg } 889be4a4b6aSJohannes Berg } 890be4a4b6aSJohannes Berg 89134bcf715SStanislaw Gruszka if (WARN_ON_ONCE(n_ch == 0)) 89234bcf715SStanislaw Gruszka goto unlock; 89334bcf715SStanislaw Gruszka 89434bcf715SStanislaw Gruszka local->int_scan_req->n_channels = n_ch; 895be4a4b6aSJohannes Berg } else { 89634bcf715SStanislaw Gruszka if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IBSS | 89734bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED))) 89834bcf715SStanislaw Gruszka goto unlock; 89934bcf715SStanislaw Gruszka 900be4a4b6aSJohannes Berg local->int_scan_req->channels[0] = chan; 901be4a4b6aSJohannes Berg local->int_scan_req->n_channels = 1; 902be4a4b6aSJohannes Berg } 903be4a4b6aSJohannes Berg 904be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 905be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 906*7ca15a0aSSimon Wunderlich local->int_scan_req->scan_width = scan_width; 9075ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 9085ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 909f3b85252SJohannes Berg 9105ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 911f3b85252SJohannes Berg unlock: 912a1699b75SJohannes Berg mutex_unlock(&local->mtx); 913f3b85252SJohannes Berg return ret; 9140a51b27eSJohannes Berg } 9155bb644a0SJohannes Berg 9164136c422SStanislaw Gruszka /* 9174136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 9184136c422SStanislaw Gruszka */ 9195bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 9205bb644a0SJohannes Berg { 9215bb644a0SJohannes Berg /* 922b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 9234136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 9244136c422SStanislaw Gruszka * 9254136c422SStanislaw Gruszka * Regarding hardware scan: 9264136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 9274136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 9284136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 9294136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 9304136c422SStanislaw Gruszka * 9314136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 9324136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 9334136c422SStanislaw Gruszka * 934b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 935b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 936b856439bSEliad Peller * after the scan was completed/aborted. 9375bb644a0SJohannes Berg */ 9384136c422SStanislaw Gruszka 939a1699b75SJohannes Berg mutex_lock(&local->mtx); 940b856439bSEliad Peller if (!local->scan_req) 941b856439bSEliad Peller goto out; 942b856439bSEliad Peller 943b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 944b856439bSEliad Peller if (local->ops->cancel_hw_scan) 945e2fd5dbcSJohannes Berg drv_cancel_hw_scan(local, 946e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 947e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx))); 948b856439bSEliad Peller goto out; 949b856439bSEliad Peller } 950b856439bSEliad Peller 951d07bfd8bSJohannes Berg /* 952d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 953d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 954d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 955d07bfd8bSJohannes Berg */ 956d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 957d07bfd8bSJohannes Berg /* and clean up */ 958d07bfd8bSJohannes Berg __ieee80211_scan_completed(&local->hw, true, false); 959b856439bSEliad Peller out: 960d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 9615bb644a0SJohannes Berg } 96279f460caSLuciano Coelho 96379f460caSLuciano Coelho int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 96479f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 96579f460caSLuciano Coelho { 96679f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 967bca1e29fSDavid Spinadel struct ieee80211_sched_scan_ies sched_scan_ies = {}; 9682103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 969c604b9f2SJohannes Berg int ret, i, iebufsz; 970c604b9f2SJohannes Berg 971c604b9f2SJohannes Berg iebufsz = 2 + IEEE80211_MAX_SSID_LEN + 972c604b9f2SJohannes Berg local->scan_ies_len + req->ie_len; 97379f460caSLuciano Coelho 9745260a5b2SJohannes Berg mutex_lock(&local->mtx); 97579f460caSLuciano Coelho 9765260a5b2SJohannes Berg if (rcu_access_pointer(local->sched_scan_sdata)) { 97779f460caSLuciano Coelho ret = -EBUSY; 97879f460caSLuciano Coelho goto out; 97979f460caSLuciano Coelho } 98079f460caSLuciano Coelho 98179f460caSLuciano Coelho if (!local->ops->sched_scan_start) { 98279f460caSLuciano Coelho ret = -ENOTSUPP; 98379f460caSLuciano Coelho goto out; 98479f460caSLuciano Coelho } 98579f460caSLuciano Coelho 98679f460caSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) { 987d811b3d5SArik Nemtsov if (!local->hw.wiphy->bands[i]) 988d811b3d5SArik Nemtsov continue; 989d811b3d5SArik Nemtsov 990c604b9f2SJohannes Berg sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL); 99130dd3edfSJohannes Berg if (!sched_scan_ies.ie[i]) { 99279f460caSLuciano Coelho ret = -ENOMEM; 99379f460caSLuciano Coelho goto out_free; 99479f460caSLuciano Coelho } 99579f460caSLuciano Coelho 9962103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 9972103dec1SSimon Wunderlich 99830dd3edfSJohannes Berg sched_scan_ies.len[i] = 99930dd3edfSJohannes Berg ieee80211_build_preq_ies(local, sched_scan_ies.ie[i], 1000c604b9f2SJohannes Berg iebufsz, req->ie, req->ie_len, 10012103dec1SSimon Wunderlich i, (u32) -1, &chandef); 100279f460caSLuciano Coelho } 100379f460caSLuciano Coelho 100430dd3edfSJohannes Berg ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 100530dd3edfSJohannes Berg if (ret == 0) 10065260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, sdata); 100779f460caSLuciano Coelho 100879f460caSLuciano Coelho out_free: 100979f460caSLuciano Coelho while (i > 0) 101030dd3edfSJohannes Berg kfree(sched_scan_ies.ie[--i]); 101179f460caSLuciano Coelho out: 10125260a5b2SJohannes Berg mutex_unlock(&local->mtx); 101379f460caSLuciano Coelho return ret; 101479f460caSLuciano Coelho } 101579f460caSLuciano Coelho 101685a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata) 101779f460caSLuciano Coelho { 101879f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 101930dd3edfSJohannes Berg int ret = 0; 102079f460caSLuciano Coelho 10215260a5b2SJohannes Berg mutex_lock(&local->mtx); 102279f460caSLuciano Coelho 102379f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 102479f460caSLuciano Coelho ret = -ENOTSUPP; 102579f460caSLuciano Coelho goto out; 102679f460caSLuciano Coelho } 102779f460caSLuciano Coelho 102830dd3edfSJohannes Berg if (rcu_access_pointer(local->sched_scan_sdata)) 102979f460caSLuciano Coelho drv_sched_scan_stop(local, sdata); 103030dd3edfSJohannes Berg 103179f460caSLuciano Coelho out: 10325260a5b2SJohannes Berg mutex_unlock(&local->mtx); 103379f460caSLuciano Coelho 103479f460caSLuciano Coelho return ret; 103579f460caSLuciano Coelho } 103679f460caSLuciano Coelho 103779f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 103879f460caSLuciano Coelho { 103979f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 104079f460caSLuciano Coelho 104179f460caSLuciano Coelho trace_api_sched_scan_results(local); 104279f460caSLuciano Coelho 104379f460caSLuciano Coelho cfg80211_sched_scan_results(hw->wiphy); 104479f460caSLuciano Coelho } 104579f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 104679f460caSLuciano Coelho 104785a9994aSLuciano Coelho void ieee80211_sched_scan_stopped_work(struct work_struct *work) 104885a9994aSLuciano Coelho { 104985a9994aSLuciano Coelho struct ieee80211_local *local = 105085a9994aSLuciano Coelho container_of(work, struct ieee80211_local, 105185a9994aSLuciano Coelho sched_scan_stopped_work); 105285a9994aSLuciano Coelho 105385a9994aSLuciano Coelho mutex_lock(&local->mtx); 105485a9994aSLuciano Coelho 10555260a5b2SJohannes Berg if (!rcu_access_pointer(local->sched_scan_sdata)) { 105685a9994aSLuciano Coelho mutex_unlock(&local->mtx); 105785a9994aSLuciano Coelho return; 105885a9994aSLuciano Coelho } 105985a9994aSLuciano Coelho 10605260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, NULL); 106185a9994aSLuciano Coelho 106285a9994aSLuciano Coelho mutex_unlock(&local->mtx); 106385a9994aSLuciano Coelho 106485a9994aSLuciano Coelho cfg80211_sched_scan_stopped(local->hw.wiphy); 106585a9994aSLuciano Coelho } 106685a9994aSLuciano Coelho 106779f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 106879f460caSLuciano Coelho { 106979f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 107079f460caSLuciano Coelho 107179f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 107279f460caSLuciano Coelho 107385a9994aSLuciano Coelho ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work); 107479f460caSLuciano Coelho } 107579f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1076