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; 697ca15a0aSSimon 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 777ca15a0aSSimon Wunderlich scan_width = NL80211_BSS_CHAN_WIDTH_20; 787ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_5MHZ) 797ca15a0aSSimon Wunderlich scan_width = NL80211_BSS_CHAN_WIDTH_5; 807ca15a0aSSimon Wunderlich if (rx_status->flag & RX_FLAG_10MHZ) 817ca15a0aSSimon Wunderlich scan_width = NL80211_BSS_CHAN_WIDTH_10; 827ca15a0aSSimon Wunderlich 837ca15a0aSSimon Wunderlich cbss = cfg80211_inform_bss_width_frame(local->hw.wiphy, channel, 847ca15a0aSSimon Wunderlich scan_width, mgmt, len, signal, 857ca15a0aSSimon 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 241a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 242a754055aSEmmanuel Grumbach return false; 243a754055aSEmmanuel Grumbach 2444d36ec58SJohannes Berg do { 2454d36ec58SJohannes Berg if (local->hw_scan_band == IEEE80211_NUM_BANDS) 2464d36ec58SJohannes Berg return false; 2474d36ec58SJohannes Berg 2484d36ec58SJohannes Berg band = local->hw_scan_band; 2494d36ec58SJohannes Berg n_chans = 0; 2504d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 2514d36ec58SJohannes Berg if (req->channels[i]->band == band) { 2524d36ec58SJohannes Berg local->hw_scan_req->channels[n_chans] = 2534d36ec58SJohannes Berg req->channels[i]; 2544d36ec58SJohannes Berg n_chans++; 2554d36ec58SJohannes Berg } 2564d36ec58SJohannes Berg } 2574d36ec58SJohannes Berg 2584d36ec58SJohannes Berg local->hw_scan_band++; 2594d36ec58SJohannes Berg } while (!n_chans); 2604d36ec58SJohannes Berg 2614d36ec58SJohannes Berg local->hw_scan_req->n_channels = n_chans; 2622103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 2634d36ec58SJohannes Berg 2644d36ec58SJohannes Berg ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie, 265c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, 2668ee31080SJohannes Berg req->ie, req->ie_len, band, 2672103dec1SSimon Wunderlich req->rates[band], &chandef); 2684d36ec58SJohannes Berg local->hw_scan_req->ie_len = ielen; 269dcd83976SJohannes Berg local->hw_scan_req->no_cck = req->no_cck; 2704d36ec58SJohannes Berg 2714d36ec58SJohannes Berg return true; 2724d36ec58SJohannes Berg } 2734d36ec58SJohannes Berg 274d07bfd8bSJohannes Berg static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted, 275e229f844SStanislaw Gruszka bool was_hw_scan) 276f3b85252SJohannes Berg { 277f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 278f3b85252SJohannes Berg 279e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 280f3b85252SJohannes Berg 2816d3560d4SJohannes Berg /* 2826d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 2836d3560d4SJohannes Berg * we have one at all will be verified by checking 2846d3560d4SJohannes Berg * local->scan_req next), but not to complete it 2856d3560d4SJohannes Berg * successfully. 2866d3560d4SJohannes Berg */ 2876d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 2886d3560d4SJohannes Berg aborted = true; 289f3b85252SJohannes Berg 290e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 291d07bfd8bSJohannes Berg return; 292f3b85252SJohannes Berg 2934d36ec58SJohannes Berg if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) { 294e2fd5dbcSJohannes Berg int rc; 295e2fd5dbcSJohannes Berg 296e2fd5dbcSJohannes Berg rc = drv_hw_scan(local, 297e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 298e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)), 299e2fd5dbcSJohannes Berg local->hw_scan_req); 300e2fd5dbcSJohannes Berg 3016eb11a9aSStanislaw Gruszka if (rc == 0) 302d07bfd8bSJohannes Berg return; 3034d36ec58SJohannes Berg } 3044d36ec58SJohannes Berg 3054d36ec58SJohannes Berg kfree(local->hw_scan_req); 3064d36ec58SJohannes Berg local->hw_scan_req = NULL; 307f3b85252SJohannes Berg 3085ba63533SJohannes Berg if (local->scan_req != local->int_scan_req) 3092a519311SJohannes Berg cfg80211_scan_done(local->scan_req, aborted); 3102a519311SJohannes Berg local->scan_req = NULL; 3113aa569c3SJohannes Berg rcu_assign_pointer(local->scan_sdata, NULL); 3122a519311SJohannes Berg 313fbe9c429SHelmut Schaa local->scanning = 0; 3147ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 315f3b85252SJohannes Berg 31659bdf3b0SBen Greear /* Set power back to normal operating levels. */ 31759bdf3b0SBen Greear ieee80211_hw_config(local, 0); 318b23b025fSBen Greear 319b23b025fSBen Greear if (!was_hw_scan) { 320b23b025fSBen Greear ieee80211_configure_filter(local); 321b23b025fSBen Greear drv_sw_scan_complete(local); 322aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 323b23b025fSBen Greear } 324b23b025fSBen Greear 3255cff20e6SJohannes Berg ieee80211_recalc_idle(local); 326e229f844SStanislaw Gruszka 3270a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 32846900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 329472dbc45SJohannes Berg ieee80211_mesh_notify_scan_completed(local); 3302eb278e0SJohannes Berg ieee80211_start_next_roc(local); 3310a51b27eSJohannes Berg } 3328789d459SJohannes Berg 3338789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 3348789d459SJohannes Berg { 3358789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3368789d459SJohannes Berg 3378789d459SJohannes Berg trace_api_scan_completed(local, aborted); 3388789d459SJohannes Berg 3398789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 3408789d459SJohannes Berg if (aborted) 3418789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 3428789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 3438789d459SJohannes Berg } 3440a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 3450a51b27eSJohannes Berg 346f3b85252SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local) 347f3b85252SJohannes Berg { 348fe57d9f5SJohannes Berg /* Software scan is not supported in multi-channel cases */ 349fe57d9f5SJohannes Berg if (local->use_chanctx) 350fe57d9f5SJohannes Berg return -EOPNOTSUPP; 351fe57d9f5SJohannes Berg 352f3b85252SJohannes Berg /* 353f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 354f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 355f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 356f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 357f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 358f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 359f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 360f3b85252SJohannes Berg * 361f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 362f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 363f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 364f3b85252SJohannes Berg */ 36524487981SJohannes Berg drv_sw_scan_start(local); 366f3b85252SJohannes Berg 367de312db3SRajkumar Manoharan local->leave_oper_channel_time = jiffies; 368977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 369f3b85252SJohannes Berg local->scan_channel_idx = 0; 370f3b85252SJohannes Berg 371aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 372a80f7c0bSJohannes Berg 3739c35d7d2SSeth Forshee /* ensure nullfunc is transmitted before leaving operating channel */ 37439ecc01dSJohannes Berg ieee80211_flush_queues(local, NULL); 3759c35d7d2SSeth Forshee 3763ac64beeSJohannes Berg ieee80211_configure_filter(local); 377f3b85252SJohannes Berg 37859bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 37959bdf3b0SBen Greear ieee80211_hw_config(local, 0); 38059bdf3b0SBen Greear 38142935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 38207ef03eeSJohannes Berg &local->scan_work, 0); 383f3b85252SJohannes Berg 384f3b85252SJohannes Berg return 0; 385f3b85252SJohannes Berg } 386f3b85252SJohannes Berg 387133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local, 388133d40f9SStanislaw Gruszka struct ieee80211_sub_if_data *sdata) 389133d40f9SStanislaw Gruszka { 390164eb02dSSimon Wunderlich if (local->radar_detect_enabled) 391164eb02dSSimon Wunderlich return false; 392164eb02dSSimon Wunderlich 3932eb278e0SJohannes Berg if (!list_empty(&local->roc_list)) 394133d40f9SStanislaw Gruszka return false; 395133d40f9SStanislaw Gruszka 396133d40f9SStanislaw Gruszka if (sdata->vif.type == NL80211_IFTYPE_STATION && 397392b9ffbSStanislaw Gruszka sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 398133d40f9SStanislaw Gruszka return false; 399133d40f9SStanislaw Gruszka 400133d40f9SStanislaw Gruszka return true; 401133d40f9SStanislaw Gruszka } 402133d40f9SStanislaw Gruszka 403133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local) 404133d40f9SStanislaw Gruszka { 405133d40f9SStanislaw Gruszka lockdep_assert_held(&local->mtx); 406133d40f9SStanislaw Gruszka 407133d40f9SStanislaw Gruszka if (!local->scan_req || local->scanning) 408133d40f9SStanislaw Gruszka return; 409133d40f9SStanislaw Gruszka 410e2fd5dbcSJohannes Berg if (!ieee80211_can_scan(local, 411e2fd5dbcSJohannes Berg rcu_dereference_protected( 412e2fd5dbcSJohannes Berg local->scan_sdata, 413e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)))) 414133d40f9SStanislaw Gruszka return; 415133d40f9SStanislaw Gruszka 416133d40f9SStanislaw Gruszka ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 417133d40f9SStanislaw Gruszka round_jiffies_relative(0)); 418133d40f9SStanislaw Gruszka } 419f3b85252SJohannes Berg 4208a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 4218a690674SBen Greear unsigned long *next_delay) 4228a690674SBen Greear { 4238a690674SBen Greear int i; 424e2fd5dbcSJohannes Berg struct ieee80211_sub_if_data *sdata; 425675a0b04SKarl Beldan enum ieee80211_band band = local->hw.conf.chandef.chan->band; 4266c17b77bSSeth Forshee u32 tx_flags; 4276c17b77bSSeth Forshee 4286c17b77bSSeth Forshee tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 4296c17b77bSSeth Forshee if (local->scan_req->no_cck) 4306c17b77bSSeth Forshee tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 4318a690674SBen Greear 432e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 433316b6b5dSPeter Senna Tschudin lockdep_is_held(&local->mtx)); 434e2fd5dbcSJohannes Berg 4358a690674SBen Greear for (i = 0; i < local->scan_req->n_ssids; i++) 4368a690674SBen Greear ieee80211_send_probe_req( 4378a690674SBen Greear sdata, NULL, 4388a690674SBen Greear local->scan_req->ssids[i].ssid, 4398a690674SBen Greear local->scan_req->ssids[i].ssid_len, 4408a690674SBen Greear local->scan_req->ie, local->scan_req->ie_len, 4418a690674SBen Greear local->scan_req->rates[band], false, 442675a0b04SKarl Beldan tx_flags, local->hw.conf.chandef.chan, true); 4438a690674SBen Greear 4448a690674SBen Greear /* 4458a690674SBen Greear * After sending probe requests, wait for probe responses 4468a690674SBen Greear * on the channel. 4478a690674SBen Greear */ 4488a690674SBen Greear *next_delay = IEEE80211_CHANNEL_TIME; 4498a690674SBen Greear local->next_scan_state = SCAN_DECISION; 4508a690674SBen Greear } 4518a690674SBen Greear 452f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 453f3b85252SJohannes Berg struct cfg80211_scan_request *req) 454f3b85252SJohannes Berg { 455f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 456f3b85252SJohannes Berg int rc; 457f3b85252SJohannes Berg 458e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 459e229f844SStanislaw Gruszka 460f3b85252SJohannes Berg if (local->scan_req) 461f3b85252SJohannes Berg return -EBUSY; 462f3b85252SJohannes Berg 463133d40f9SStanislaw Gruszka if (!ieee80211_can_scan(local, sdata)) { 4646e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 465c0ce77b8SJohannes Berg local->scan_req = req; 466e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 467c0ce77b8SJohannes Berg return 0; 468c0ce77b8SJohannes Berg } 469c0ce77b8SJohannes Berg 470f3b85252SJohannes Berg if (local->ops->hw_scan) { 471f3b85252SJohannes Berg u8 *ies; 472f3b85252SJohannes Berg 473c604b9f2SJohannes Berg local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN + 474c604b9f2SJohannes Berg local->scan_ies_len + 475c604b9f2SJohannes Berg req->ie_len; 4764d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 4774d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 4784d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 479c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, GFP_KERNEL); 4804d36ec58SJohannes Berg if (!local->hw_scan_req) 481f3b85252SJohannes Berg return -ENOMEM; 482f3b85252SJohannes Berg 4834d36ec58SJohannes Berg local->hw_scan_req->ssids = req->ssids; 4844d36ec58SJohannes Berg local->hw_scan_req->n_ssids = req->n_ssids; 4854d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 4864d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 4874d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 4884d36ec58SJohannes Berg local->hw_scan_req->ie = ies; 489cd2bb512SSam Leffler local->hw_scan_req->flags = req->flags; 4904d36ec58SJohannes Berg 4914d36ec58SJohannes Berg local->hw_scan_band = 0; 4926e7e6213SJohn W. Linville 4936e7e6213SJohn W. Linville /* 4946e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 4956e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 4966e7e6213SJohn W. Linville * anything that might be changed here and leave 4976e7e6213SJohn W. Linville * this function early must not go after this 4986e7e6213SJohn W. Linville * allocation. 4996e7e6213SJohn W. Linville */ 500f3b85252SJohannes Berg } 501f3b85252SJohannes Berg 502f3b85252SJohannes Berg local->scan_req = req; 503e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 504f3b85252SJohannes Berg 5058a690674SBen Greear if (local->ops->hw_scan) { 506fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 5078a690674SBen Greear } else if ((req->n_channels == 1) && 508675a0b04SKarl Beldan (req->channels[0] == local->_oper_chandef.chan)) { 5099b864870SJohannes Berg /* 5109b864870SJohannes Berg * If we are scanning only on the operating channel 5119b864870SJohannes Berg * then we do not need to stop normal activities 5128a690674SBen Greear */ 5138a690674SBen Greear unsigned long next_delay; 5148a690674SBen Greear 5158a690674SBen Greear __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 5168a690674SBen Greear 5178a690674SBen Greear ieee80211_recalc_idle(local); 5188a690674SBen Greear 5198a690674SBen Greear /* Notify driver scan is starting, keep order of operations 5208a690674SBen Greear * same as normal software scan, in case that matters. */ 5218a690674SBen Greear drv_sw_scan_start(local); 5228a690674SBen Greear 5238a690674SBen Greear ieee80211_configure_filter(local); /* accept probe-responses */ 5248a690674SBen Greear 5258a690674SBen Greear /* We need to ensure power level is at max for scanning. */ 5268a690674SBen Greear ieee80211_hw_config(local, 0); 5278a690674SBen Greear 5288a690674SBen Greear if ((req->channels[0]->flags & 529*8fe02e16SLuis R. Rodriguez IEEE80211_CHAN_NO_IR) || 5308a690674SBen Greear !local->scan_req->n_ssids) { 5318a690674SBen Greear next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 5328a690674SBen Greear } else { 5338a690674SBen Greear ieee80211_scan_state_send_probe(local, &next_delay); 5348a690674SBen Greear next_delay = IEEE80211_CHANNEL_TIME; 5358a690674SBen Greear } 5368a690674SBen Greear 5378a690674SBen Greear /* Now, just wait a bit and we are all done! */ 5388a690674SBen Greear ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 5398a690674SBen Greear next_delay); 5408a690674SBen Greear return 0; 5418a690674SBen Greear } else { 5428a690674SBen Greear /* Do normal software scan */ 543fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 5448a690674SBen Greear } 5456e7e6213SJohn W. Linville 5465cff20e6SJohannes Berg ieee80211_recalc_idle(local); 547f3b85252SJohannes Berg 5484d36ec58SJohannes Berg if (local->ops->hw_scan) { 5494d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 550a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 5514d36ec58SJohannes Berg } else 552f3b85252SJohannes Berg rc = ieee80211_start_sw_scan(local); 553f3b85252SJohannes Berg 554f3b85252SJohannes Berg if (rc) { 5554d36ec58SJohannes Berg kfree(local->hw_scan_req); 5564d36ec58SJohannes Berg local->hw_scan_req = NULL; 557fbe9c429SHelmut Schaa local->scanning = 0; 558f3b85252SJohannes Berg 5595cff20e6SJohannes Berg ieee80211_recalc_idle(local); 5605cff20e6SJohannes Berg 561f3b85252SJohannes Berg local->scan_req = NULL; 562e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, NULL); 563f3b85252SJohannes Berg } 564f3b85252SJohannes Berg 565f3b85252SJohannes Berg return rc; 566f3b85252SJohannes Berg } 567f3b85252SJohannes Berg 568df13cce5SHelmut Schaa static unsigned long 569df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 570df13cce5SHelmut Schaa { 571df13cce5SHelmut Schaa /* 572df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 573df13cce5SHelmut Schaa * add that delay as well to get a better estimation 574df13cce5SHelmut Schaa */ 575*8fe02e16SLuis R. Rodriguez if (chan->flags & IEEE80211_CHAN_NO_IR) 576df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 577df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 578df13cce5SHelmut Schaa } 579df13cce5SHelmut Schaa 580e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 5812fb3f028SHelmut Schaa unsigned long *next_delay) 5822fb3f028SHelmut Schaa { 583142b9f50SHelmut Schaa bool associated = false; 584df13cce5SHelmut Schaa bool tx_empty = true; 585df13cce5SHelmut Schaa bool bad_latency; 586142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 587df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 588cd2bb512SSam Leffler enum mac80211_scan_state next_scan_state; 589142b9f50SHelmut Schaa 590df13cce5SHelmut Schaa /* 591df13cce5SHelmut Schaa * check if at least one STA interface is associated, 592df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 593df13cce5SHelmut Schaa * and grab the lowest used beacon interval 594df13cce5SHelmut Schaa */ 595142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 596142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 5979607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 598142b9f50SHelmut Schaa continue; 599142b9f50SHelmut Schaa 600142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 601142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 602142b9f50SHelmut Schaa associated = true; 603df13cce5SHelmut Schaa 604df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 605df13cce5SHelmut Schaa tx_empty = false; 606142b9f50SHelmut Schaa break; 607142b9f50SHelmut Schaa } 608142b9f50SHelmut Schaa } 609142b9f50SHelmut Schaa } 610df13cce5SHelmut Schaa } 611142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 612142b9f50SHelmut Schaa 613b23b025fSBen Greear next_chan = local->scan_req->channels[local->scan_channel_idx]; 614b23b025fSBen Greear 615142b9f50SHelmut Schaa /* 616142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 617df13cce5SHelmut Schaa * see if we can scan another channel without interfering 618df13cce5SHelmut Schaa * with the current traffic situation. 619df13cce5SHelmut Schaa * 6203f892b61SStanislaw Gruszka * Keep good latency, do not stay off-channel more than 125 ms. 621142b9f50SHelmut Schaa */ 622df13cce5SHelmut Schaa 623df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 624df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 6253f892b61SStanislaw Gruszka local->leave_oper_channel_time + HZ / 8); 626df13cce5SHelmut Schaa 627cd2bb512SSam Leffler if (associated && !tx_empty) { 628cd2bb512SSam Leffler if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 629cd2bb512SSam Leffler next_scan_state = SCAN_ABORT; 630142b9f50SHelmut Schaa else 631cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 6323f892b61SStanislaw Gruszka } else if (associated && bad_latency) { 633cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 634cd2bb512SSam Leffler } else { 635cd2bb512SSam Leffler next_scan_state = SCAN_SET_CHANNEL; 636cd2bb512SSam Leffler } 637cd2bb512SSam Leffler 638cd2bb512SSam Leffler local->next_scan_state = next_scan_state; 639142b9f50SHelmut Schaa 640142b9f50SHelmut Schaa *next_delay = 0; 6412fb3f028SHelmut Schaa } 6422fb3f028SHelmut Schaa 6432fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 6447d3be3ccSHelmut Schaa unsigned long *next_delay) 6457d3be3ccSHelmut Schaa { 6467d3be3ccSHelmut Schaa int skip; 6477d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 6487ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width oper_scan_width; 6497d3be3ccSHelmut Schaa 6507d3be3ccSHelmut Schaa skip = 0; 6517d3be3ccSHelmut Schaa chan = local->scan_req->channels[local->scan_channel_idx]; 6527d3be3ccSHelmut Schaa 6537ca15a0aSSimon Wunderlich local->scan_chandef.chan = chan; 6547ca15a0aSSimon Wunderlich local->scan_chandef.center_freq1 = chan->center_freq; 6557ca15a0aSSimon Wunderlich local->scan_chandef.center_freq2 = 0; 6567ca15a0aSSimon Wunderlich switch (local->scan_req->scan_width) { 6577ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 6587ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 6597ca15a0aSSimon Wunderlich break; 6607ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 6617ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 6627ca15a0aSSimon Wunderlich break; 6637ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_20: 6647ca15a0aSSimon Wunderlich /* If scanning on oper channel, use whatever channel-type 6657ca15a0aSSimon Wunderlich * is currently in use. 6667ca15a0aSSimon Wunderlich */ 6677ca15a0aSSimon Wunderlich oper_scan_width = cfg80211_chandef_to_scan_width( 6687ca15a0aSSimon Wunderlich &local->_oper_chandef); 6697ca15a0aSSimon Wunderlich if (chan == local->_oper_chandef.chan && 6707ca15a0aSSimon Wunderlich oper_scan_width == local->scan_req->scan_width) 6717ca15a0aSSimon Wunderlich local->scan_chandef = local->_oper_chandef; 6727ca15a0aSSimon Wunderlich else 6737ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 6747ca15a0aSSimon Wunderlich break; 6757ca15a0aSSimon Wunderlich } 676b23b025fSBen Greear 677584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 6787d3be3ccSHelmut Schaa skip = 1; 6797d3be3ccSHelmut Schaa 6807d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 6817d3be3ccSHelmut Schaa local->scan_channel_idx++; 6827d3be3ccSHelmut Schaa 6830ee9c13cSHelmut Schaa if (skip) { 6840ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 6850ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 6862fb3f028SHelmut Schaa return; 6870ee9c13cSHelmut Schaa } 6887d3be3ccSHelmut Schaa 6897d3be3ccSHelmut Schaa /* 6907d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 6917d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 6927d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 6937d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 6947d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 6957d3be3ccSHelmut Schaa * received. 6967d3be3ccSHelmut Schaa * 6977d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 6987d3be3ccSHelmut Schaa */ 699*8fe02e16SLuis R. Rodriguez if (chan->flags & IEEE80211_CHAN_NO_IR || 7007d3be3ccSHelmut Schaa !local->scan_req->n_ssids) { 7017d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 702977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 7032fb3f028SHelmut Schaa return; 7047d3be3ccSHelmut Schaa } 7057d3be3ccSHelmut Schaa 7062fb3f028SHelmut Schaa /* active scan, send probes */ 7077d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 708977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 7097d3be3ccSHelmut Schaa } 7107d3be3ccSHelmut Schaa 71107ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 71207ef03eeSJohannes Berg unsigned long *next_delay) 71307ef03eeSJohannes Berg { 71407ef03eeSJohannes Berg /* switch back to the operating channel */ 7157ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 71607ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 71707ef03eeSJohannes Berg 718aacde9eeSStanislaw Gruszka /* disable PS */ 719aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 72007ef03eeSJohannes Berg 72107ef03eeSJohannes Berg *next_delay = HZ / 5; 72207ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 72307ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 72407ef03eeSJohannes Berg } 72507ef03eeSJohannes Berg 72607ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 72707ef03eeSJohannes Berg unsigned long *next_delay) 72807ef03eeSJohannes Berg { 729aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 73007ef03eeSJohannes Berg 73107ef03eeSJohannes Berg if (local->ops->flush) { 73239ecc01dSJohannes Berg ieee80211_flush_queues(local, NULL); 73307ef03eeSJohannes Berg *next_delay = 0; 73407ef03eeSJohannes Berg } else 73507ef03eeSJohannes Berg *next_delay = HZ / 10; 73607ef03eeSJohannes Berg 73707ef03eeSJohannes Berg /* remember when we left the operating channel */ 73807ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 73907ef03eeSJohannes Berg 74007ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 741de2ee84dSMohammed Shafi Shajakhan local->next_scan_state = SCAN_SET_CHANNEL; 74207ef03eeSJohannes Berg } 74307ef03eeSJohannes Berg 744c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 7450a51b27eSJohannes Berg { 7460a51b27eSJohannes Berg struct ieee80211_local *local = 7470a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 748d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 7490a51b27eSJohannes Berg unsigned long next_delay = 0; 750d07bfd8bSJohannes Berg bool aborted, hw_scan; 7518789d459SJohannes Berg 752259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 753259b62e3SStanislaw Gruszka 754e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 755e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)); 756d07bfd8bSJohannes Berg 7578a690674SBen Greear /* When scanning on-channel, the first-callback means completed. */ 7588a690674SBen Greear if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 7598a690674SBen Greear aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 7608a690674SBen Greear goto out_complete; 7618a690674SBen Greear } 7628a690674SBen Greear 763259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 7648789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 765259b62e3SStanislaw Gruszka goto out_complete; 7668789d459SJohannes Berg } 7678789d459SJohannes Berg 768259b62e3SStanislaw Gruszka if (!sdata || !local->scan_req) 769259b62e3SStanislaw Gruszka goto out; 770f3b85252SJohannes Berg 771fbe9c429SHelmut Schaa if (local->scan_req && !local->scanning) { 772f3b85252SJohannes Berg struct cfg80211_scan_request *req = local->scan_req; 773f3b85252SJohannes Berg int rc; 774f3b85252SJohannes Berg 775f3b85252SJohannes Berg local->scan_req = NULL; 776e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, NULL); 777f3b85252SJohannes Berg 778f3b85252SJohannes Berg rc = __ieee80211_start_scan(sdata, req); 779259b62e3SStanislaw Gruszka if (rc) { 7803aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 7813aed49efSStanislaw Gruszka local->scan_req = req; 782259b62e3SStanislaw Gruszka aborted = true; 783259b62e3SStanislaw Gruszka goto out_complete; 784259b62e3SStanislaw Gruszka } else 785259b62e3SStanislaw Gruszka goto out; 786f3b85252SJohannes Berg } 787f3b85252SJohannes Berg 7885bc75728SJohannes Berg /* 7895bc75728SJohannes Berg * Avoid re-scheduling when the sdata is going away. 7905bc75728SJohannes Berg */ 7919607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) { 792259b62e3SStanislaw Gruszka aborted = true; 793259b62e3SStanislaw Gruszka goto out_complete; 794f3b85252SJohannes Berg } 7950a51b27eSJohannes Berg 796f502d09bSHelmut Schaa /* 797f502d09bSHelmut Schaa * as long as no delay is required advance immediately 798f502d09bSHelmut Schaa * without scheduling a new work 799f502d09bSHelmut Schaa */ 800f502d09bSHelmut Schaa do { 801c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 802c29acf20SRajkumar Manoharan aborted = true; 803c29acf20SRajkumar Manoharan goto out_complete; 804c29acf20SRajkumar Manoharan } 805c29acf20SRajkumar Manoharan 806977923b0SHelmut Schaa switch (local->next_scan_state) { 8072fb3f028SHelmut Schaa case SCAN_DECISION: 808e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 809e229f844SStanislaw Gruszka if (local->scan_channel_idx >= local->scan_req->n_channels) { 810e229f844SStanislaw Gruszka aborted = false; 811e229f844SStanislaw Gruszka goto out_complete; 812e229f844SStanislaw Gruszka } 813e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 8140a51b27eSJohannes Berg break; 8152fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 8162fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 8172fb3f028SHelmut Schaa break; 8180a51b27eSJohannes Berg case SCAN_SEND_PROBE: 8197d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 8200a51b27eSJohannes Berg break; 82107ef03eeSJohannes Berg case SCAN_SUSPEND: 82207ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 823142b9f50SHelmut Schaa break; 82407ef03eeSJohannes Berg case SCAN_RESUME: 82507ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 826142b9f50SHelmut Schaa break; 827cd2bb512SSam Leffler case SCAN_ABORT: 828cd2bb512SSam Leffler aborted = true; 829cd2bb512SSam Leffler goto out_complete; 8300a51b27eSJohannes Berg } 831f502d09bSHelmut Schaa } while (next_delay == 0); 8320a51b27eSJohannes Berg 83342935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 834d07bfd8bSJohannes Berg goto out; 835259b62e3SStanislaw Gruszka 836259b62e3SStanislaw Gruszka out_complete: 837e229f844SStanislaw Gruszka hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning); 838d07bfd8bSJohannes Berg __ieee80211_scan_completed(&local->hw, aborted, hw_scan); 839259b62e3SStanislaw Gruszka out: 840259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 8410a51b27eSJohannes Berg } 8420a51b27eSJohannes Berg 843c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 8442a519311SJohannes Berg struct cfg80211_scan_request *req) 8450a51b27eSJohannes Berg { 846f3b85252SJohannes Berg int res; 8470a51b27eSJohannes Berg 848a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 849f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 850a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 8512a519311SJohannes Berg 852f3b85252SJohannes Berg return res; 8530a51b27eSJohannes Berg } 8540a51b27eSJohannes Berg 85534bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 856be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 8577ca15a0aSSimon Wunderlich struct ieee80211_channel *chan, 8587ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width) 859f3b85252SJohannes Berg { 860f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 861f3b85252SJohannes Berg int ret = -EBUSY; 862fb63bc41SGertjan van Wingerde enum ieee80211_band band; 8639116dd01SJohannes Berg 864a1699b75SJohannes Berg mutex_lock(&local->mtx); 865f3b85252SJohannes Berg 866f3b85252SJohannes Berg /* busy scanning */ 867f3b85252SJohannes Berg if (local->scan_req) 868f3b85252SJohannes Berg goto unlock; 869f3b85252SJohannes Berg 870be4a4b6aSJohannes Berg /* fill internal scan request */ 871be4a4b6aSJohannes Berg if (!chan) { 87234bcf715SStanislaw Gruszka int i, max_n; 87334bcf715SStanislaw Gruszka int n_ch = 0; 874be4a4b6aSJohannes Berg 875be4a4b6aSJohannes Berg for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 876be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 877be4a4b6aSJohannes Berg continue; 87834bcf715SStanislaw Gruszka 87934bcf715SStanislaw Gruszka max_n = local->hw.wiphy->bands[band]->n_channels; 88034bcf715SStanislaw Gruszka for (i = 0; i < max_n; i++) { 88134bcf715SStanislaw Gruszka struct ieee80211_channel *tmp_ch = 882be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 88334bcf715SStanislaw Gruszka 884*8fe02e16SLuis R. Rodriguez if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 88534bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED)) 88634bcf715SStanislaw Gruszka continue; 88734bcf715SStanislaw Gruszka 88834bcf715SStanislaw Gruszka local->int_scan_req->channels[n_ch] = tmp_ch; 88934bcf715SStanislaw Gruszka n_ch++; 890be4a4b6aSJohannes Berg } 891be4a4b6aSJohannes Berg } 892be4a4b6aSJohannes Berg 89334bcf715SStanislaw Gruszka if (WARN_ON_ONCE(n_ch == 0)) 89434bcf715SStanislaw Gruszka goto unlock; 89534bcf715SStanislaw Gruszka 89634bcf715SStanislaw Gruszka local->int_scan_req->n_channels = n_ch; 897be4a4b6aSJohannes Berg } else { 898*8fe02e16SLuis R. Rodriguez if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IR | 89934bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED))) 90034bcf715SStanislaw Gruszka goto unlock; 90134bcf715SStanislaw Gruszka 902be4a4b6aSJohannes Berg local->int_scan_req->channels[0] = chan; 903be4a4b6aSJohannes Berg local->int_scan_req->n_channels = 1; 904be4a4b6aSJohannes Berg } 905be4a4b6aSJohannes Berg 906be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 907be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 9087ca15a0aSSimon Wunderlich local->int_scan_req->scan_width = scan_width; 9095ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 9105ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 911f3b85252SJohannes Berg 9125ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 913f3b85252SJohannes Berg unlock: 914a1699b75SJohannes Berg mutex_unlock(&local->mtx); 915f3b85252SJohannes Berg return ret; 9160a51b27eSJohannes Berg } 9175bb644a0SJohannes Berg 9184136c422SStanislaw Gruszka /* 9194136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 9204136c422SStanislaw Gruszka */ 9215bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 9225bb644a0SJohannes Berg { 9235bb644a0SJohannes Berg /* 924b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 9254136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 9264136c422SStanislaw Gruszka * 9274136c422SStanislaw Gruszka * Regarding hardware scan: 9284136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 9294136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 9304136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 9314136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 9324136c422SStanislaw Gruszka * 9334136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 9344136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 9354136c422SStanislaw Gruszka * 936b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 937b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 938b856439bSEliad Peller * after the scan was completed/aborted. 9395bb644a0SJohannes Berg */ 9404136c422SStanislaw Gruszka 941a1699b75SJohannes Berg mutex_lock(&local->mtx); 942b856439bSEliad Peller if (!local->scan_req) 943b856439bSEliad Peller goto out; 944b856439bSEliad Peller 945a754055aSEmmanuel Grumbach /* 946a754055aSEmmanuel Grumbach * We have a scan running and the driver already reported completion, 947a754055aSEmmanuel Grumbach * but the worker hasn't run yet or is stuck on the mutex - mark it as 948a754055aSEmmanuel Grumbach * cancelled. 949a754055aSEmmanuel Grumbach */ 950a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 951a754055aSEmmanuel Grumbach test_bit(SCAN_COMPLETED, &local->scanning)) { 952a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 953a754055aSEmmanuel Grumbach goto out; 954a754055aSEmmanuel Grumbach } 955a754055aSEmmanuel Grumbach 956b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 957a754055aSEmmanuel Grumbach /* 958a754055aSEmmanuel Grumbach * Make sure that __ieee80211_scan_completed doesn't trigger a 959a754055aSEmmanuel Grumbach * scan on another band. 960a754055aSEmmanuel Grumbach */ 961a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 962b856439bSEliad Peller if (local->ops->cancel_hw_scan) 963e2fd5dbcSJohannes Berg drv_cancel_hw_scan(local, 964e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 965e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx))); 966b856439bSEliad Peller goto out; 967b856439bSEliad Peller } 968b856439bSEliad Peller 969d07bfd8bSJohannes Berg /* 970d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 971d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 972d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 973d07bfd8bSJohannes Berg */ 974d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 975d07bfd8bSJohannes Berg /* and clean up */ 976d07bfd8bSJohannes Berg __ieee80211_scan_completed(&local->hw, true, false); 977b856439bSEliad Peller out: 978d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 9795bb644a0SJohannes Berg } 98079f460caSLuciano Coelho 98179f460caSLuciano Coelho int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 98279f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 98379f460caSLuciano Coelho { 98479f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 985bca1e29fSDavid Spinadel struct ieee80211_sched_scan_ies sched_scan_ies = {}; 9862103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 987c604b9f2SJohannes Berg int ret, i, iebufsz; 988c604b9f2SJohannes Berg 989c604b9f2SJohannes Berg iebufsz = 2 + IEEE80211_MAX_SSID_LEN + 990c604b9f2SJohannes Berg local->scan_ies_len + req->ie_len; 99179f460caSLuciano Coelho 9925260a5b2SJohannes Berg mutex_lock(&local->mtx); 99379f460caSLuciano Coelho 9945260a5b2SJohannes Berg if (rcu_access_pointer(local->sched_scan_sdata)) { 99579f460caSLuciano Coelho ret = -EBUSY; 99679f460caSLuciano Coelho goto out; 99779f460caSLuciano Coelho } 99879f460caSLuciano Coelho 99979f460caSLuciano Coelho if (!local->ops->sched_scan_start) { 100079f460caSLuciano Coelho ret = -ENOTSUPP; 100179f460caSLuciano Coelho goto out; 100279f460caSLuciano Coelho } 100379f460caSLuciano Coelho 100479f460caSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) { 1005d811b3d5SArik Nemtsov if (!local->hw.wiphy->bands[i]) 1006d811b3d5SArik Nemtsov continue; 1007d811b3d5SArik Nemtsov 1008c604b9f2SJohannes Berg sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL); 100930dd3edfSJohannes Berg if (!sched_scan_ies.ie[i]) { 101079f460caSLuciano Coelho ret = -ENOMEM; 101179f460caSLuciano Coelho goto out_free; 101279f460caSLuciano Coelho } 101379f460caSLuciano Coelho 10142103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 10152103dec1SSimon Wunderlich 101630dd3edfSJohannes Berg sched_scan_ies.len[i] = 101730dd3edfSJohannes Berg ieee80211_build_preq_ies(local, sched_scan_ies.ie[i], 1018c604b9f2SJohannes Berg iebufsz, req->ie, req->ie_len, 10192103dec1SSimon Wunderlich i, (u32) -1, &chandef); 102079f460caSLuciano Coelho } 102179f460caSLuciano Coelho 102230dd3edfSJohannes Berg ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 102330dd3edfSJohannes Berg if (ret == 0) 10245260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, sdata); 102579f460caSLuciano Coelho 102679f460caSLuciano Coelho out_free: 102779f460caSLuciano Coelho while (i > 0) 102830dd3edfSJohannes Berg kfree(sched_scan_ies.ie[--i]); 102979f460caSLuciano Coelho out: 10305260a5b2SJohannes Berg mutex_unlock(&local->mtx); 103179f460caSLuciano Coelho return ret; 103279f460caSLuciano Coelho } 103379f460caSLuciano Coelho 103485a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata) 103579f460caSLuciano Coelho { 103679f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 103730dd3edfSJohannes Berg int ret = 0; 103879f460caSLuciano Coelho 10395260a5b2SJohannes Berg mutex_lock(&local->mtx); 104079f460caSLuciano Coelho 104179f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 104279f460caSLuciano Coelho ret = -ENOTSUPP; 104379f460caSLuciano Coelho goto out; 104479f460caSLuciano Coelho } 104579f460caSLuciano Coelho 104630dd3edfSJohannes Berg if (rcu_access_pointer(local->sched_scan_sdata)) 104779f460caSLuciano Coelho drv_sched_scan_stop(local, sdata); 104830dd3edfSJohannes Berg 104979f460caSLuciano Coelho out: 10505260a5b2SJohannes Berg mutex_unlock(&local->mtx); 105179f460caSLuciano Coelho 105279f460caSLuciano Coelho return ret; 105379f460caSLuciano Coelho } 105479f460caSLuciano Coelho 105579f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 105679f460caSLuciano Coelho { 105779f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 105879f460caSLuciano Coelho 105979f460caSLuciano Coelho trace_api_sched_scan_results(local); 106079f460caSLuciano Coelho 106179f460caSLuciano Coelho cfg80211_sched_scan_results(hw->wiphy); 106279f460caSLuciano Coelho } 106379f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 106479f460caSLuciano Coelho 106585a9994aSLuciano Coelho void ieee80211_sched_scan_stopped_work(struct work_struct *work) 106685a9994aSLuciano Coelho { 106785a9994aSLuciano Coelho struct ieee80211_local *local = 106885a9994aSLuciano Coelho container_of(work, struct ieee80211_local, 106985a9994aSLuciano Coelho sched_scan_stopped_work); 107085a9994aSLuciano Coelho 107185a9994aSLuciano Coelho mutex_lock(&local->mtx); 107285a9994aSLuciano Coelho 10735260a5b2SJohannes Berg if (!rcu_access_pointer(local->sched_scan_sdata)) { 107485a9994aSLuciano Coelho mutex_unlock(&local->mtx); 107585a9994aSLuciano Coelho return; 107685a9994aSLuciano Coelho } 107785a9994aSLuciano Coelho 10785260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, NULL); 107985a9994aSLuciano Coelho 108085a9994aSLuciano Coelho mutex_unlock(&local->mtx); 108185a9994aSLuciano Coelho 108285a9994aSLuciano Coelho cfg80211_sched_scan_stopped(local->hw.wiphy); 108385a9994aSLuciano Coelho } 108485a9994aSLuciano Coelho 108579f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 108679f460caSLuciano Coelho { 108779f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 108879f460caSLuciano Coelho 108979f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 109079f460caSLuciano Coelho 109185a9994aSLuciano Coelho ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work); 109279f460caSLuciano Coelho } 109379f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1094