10a51b27eSJohannes Berg /* 25484e237SJohannes Berg * Scanning implementation 35484e237SJohannes Berg * 40a51b27eSJohannes Berg * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> 50a51b27eSJohannes Berg * Copyright 2004, Instant802 Networks, Inc. 60a51b27eSJohannes Berg * Copyright 2005, Devicescape Software, Inc. 70a51b27eSJohannes Berg * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 80a51b27eSJohannes Berg * Copyright 2007, Michael Wu <flamingice@sourmilk.net> 974d803b6SSara Sharon * Copyright 2013-2015 Intel Mobile Communications GmbH 1040b0bd24SRoee Zamir * Copyright 2016-2017 Intel Deutschland GmbH 114abb52a4SSara Sharon * Copyright (C) 2018-2019 Intel Corporation 120a51b27eSJohannes Berg * 130a51b27eSJohannes Berg * This program is free software; you can redistribute it and/or modify 140a51b27eSJohannes Berg * it under the terms of the GNU General Public License version 2 as 150a51b27eSJohannes Berg * published by the Free Software Foundation. 160a51b27eSJohannes Berg */ 170a51b27eSJohannes Berg 180a51b27eSJohannes Berg #include <linux/if_arp.h> 19888d04dfSFelix Fietkau #include <linux/etherdevice.h> 20078e1e60SJohannes Berg #include <linux/rtnetlink.h> 21df13cce5SHelmut Schaa #include <net/sch_generic.h> 225a0e3ad6STejun Heo #include <linux/slab.h> 23bc3b2d7fSPaul Gortmaker #include <linux/export.h> 24b9771d41SJohannes Berg #include <linux/random.h> 250a51b27eSJohannes Berg #include <net/mac80211.h> 260a51b27eSJohannes Berg 270a51b27eSJohannes Berg #include "ieee80211_i.h" 2824487981SJohannes Berg #include "driver-ops.h" 295484e237SJohannes Berg #include "mesh.h" 300a51b27eSJohannes Berg 310a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33) 320a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33) 333f892b61SStanislaw Gruszka #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9) 340a51b27eSJohannes Berg 355484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local, 36c2b13452SJohannes Berg struct ieee80211_bss *bss) 375484e237SJohannes Berg { 380c1ad2caSJohannes Berg if (!bss) 390c1ad2caSJohannes Berg return; 405b112d3dSJohannes Berg cfg80211_put_bss(local->hw.wiphy, 415b112d3dSJohannes Berg container_of((void *)bss, struct cfg80211_bss, priv)); 425484e237SJohannes Berg } 435484e237SJohannes Berg 44ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems) 45ab13315aSKalle Valo { 46ab13315aSKalle Valo u8 qos_info; 47ab13315aSKalle Valo 48ab13315aSKalle Valo if (elems->wmm_info && elems->wmm_info_len == 7 49ab13315aSKalle Valo && elems->wmm_info[5] == 1) 50ab13315aSKalle Valo qos_info = elems->wmm_info[6]; 51ab13315aSKalle Valo else if (elems->wmm_param && elems->wmm_param_len == 24 52ab13315aSKalle Valo && elems->wmm_param[5] == 1) 53ab13315aSKalle Valo qos_info = elems->wmm_param[6]; 54ab13315aSKalle Valo else 55ab13315aSKalle Valo /* no valid wmm information or parameter element found */ 56ab13315aSKalle Valo return false; 57ab13315aSKalle Valo 58ab13315aSKalle Valo return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD; 59ab13315aSKalle Valo } 60ab13315aSKalle Valo 61fcea7db5SSara Sharon static void 62fcea7db5SSara Sharon ieee80211_update_bss_from_elems(struct ieee80211_local *local, 63fcea7db5SSara Sharon struct ieee80211_bss *bss, 64fcea7db5SSara Sharon struct ieee802_11_elems *elems, 65fcea7db5SSara Sharon struct ieee80211_rx_status *rx_status, 66fcea7db5SSara Sharon bool beacon) 67fcea7db5SSara Sharon { 68fcea7db5SSara Sharon int clen, srlen; 69fcea7db5SSara Sharon 70fcea7db5SSara Sharon if (beacon) 71fcea7db5SSara Sharon bss->device_ts_beacon = rx_status->device_timestamp; 72fcea7db5SSara Sharon else 73fcea7db5SSara Sharon bss->device_ts_presp = rx_status->device_timestamp; 74fcea7db5SSara Sharon 75fcea7db5SSara Sharon if (elems->parse_error) { 76fcea7db5SSara Sharon if (beacon) 77fcea7db5SSara Sharon bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON; 78fcea7db5SSara Sharon else 79fcea7db5SSara Sharon bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP; 80fcea7db5SSara Sharon } else { 81fcea7db5SSara Sharon if (beacon) 82fcea7db5SSara Sharon bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON; 83fcea7db5SSara Sharon else 84fcea7db5SSara Sharon bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP; 85fcea7db5SSara Sharon } 86fcea7db5SSara Sharon 87fcea7db5SSara Sharon /* save the ERP value so that it is available at association time */ 88fcea7db5SSara Sharon if (elems->erp_info && (!elems->parse_error || 89fcea7db5SSara Sharon !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) { 90fcea7db5SSara Sharon bss->erp_value = elems->erp_info[0]; 91fcea7db5SSara Sharon bss->has_erp_value = true; 92fcea7db5SSara Sharon if (!elems->parse_error) 93fcea7db5SSara Sharon bss->valid_data |= IEEE80211_BSS_VALID_ERP; 94fcea7db5SSara Sharon } 95fcea7db5SSara Sharon 96fcea7db5SSara Sharon /* replace old supported rates if we get new values */ 97fcea7db5SSara Sharon if (!elems->parse_error || 98fcea7db5SSara Sharon !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) { 99fcea7db5SSara Sharon srlen = 0; 100fcea7db5SSara Sharon if (elems->supp_rates) { 101fcea7db5SSara Sharon clen = IEEE80211_MAX_SUPP_RATES; 102fcea7db5SSara Sharon if (clen > elems->supp_rates_len) 103fcea7db5SSara Sharon clen = elems->supp_rates_len; 104fcea7db5SSara Sharon memcpy(bss->supp_rates, elems->supp_rates, clen); 105fcea7db5SSara Sharon srlen += clen; 106fcea7db5SSara Sharon } 107fcea7db5SSara Sharon if (elems->ext_supp_rates) { 108fcea7db5SSara Sharon clen = IEEE80211_MAX_SUPP_RATES - srlen; 109fcea7db5SSara Sharon if (clen > elems->ext_supp_rates_len) 110fcea7db5SSara Sharon clen = elems->ext_supp_rates_len; 111fcea7db5SSara Sharon memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, 112fcea7db5SSara Sharon clen); 113fcea7db5SSara Sharon srlen += clen; 114fcea7db5SSara Sharon } 115fcea7db5SSara Sharon if (srlen) { 116fcea7db5SSara Sharon bss->supp_rates_len = srlen; 117fcea7db5SSara Sharon if (!elems->parse_error) 118fcea7db5SSara Sharon bss->valid_data |= IEEE80211_BSS_VALID_RATES; 119fcea7db5SSara Sharon } 120fcea7db5SSara Sharon } 121fcea7db5SSara Sharon 122fcea7db5SSara Sharon if (!elems->parse_error || 123fcea7db5SSara Sharon !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) { 124fcea7db5SSara Sharon bss->wmm_used = elems->wmm_param || elems->wmm_info; 125fcea7db5SSara Sharon bss->uapsd_supported = is_uapsd_supported(elems); 126fcea7db5SSara Sharon if (!elems->parse_error) 127fcea7db5SSara Sharon bss->valid_data |= IEEE80211_BSS_VALID_WMM; 128fcea7db5SSara Sharon } 129fcea7db5SSara Sharon 130fcea7db5SSara Sharon if (beacon) { 131fcea7db5SSara Sharon struct ieee80211_supported_band *sband = 132fcea7db5SSara Sharon local->hw.wiphy->bands[rx_status->band]; 133fcea7db5SSara Sharon if (!(rx_status->encoding == RX_ENC_HT) && 134fcea7db5SSara Sharon !(rx_status->encoding == RX_ENC_VHT)) 135fcea7db5SSara Sharon bss->beacon_rate = 136fcea7db5SSara Sharon &sband->bitrates[rx_status->rate_idx]; 137fcea7db5SSara Sharon } 138fcea7db5SSara Sharon } 139fcea7db5SSara Sharon 140c2b13452SJohannes Berg struct ieee80211_bss * 1415484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local, 1425484e237SJohannes Berg struct ieee80211_rx_status *rx_status, 143d45c4172SEmmanuel Grumbach struct ieee80211_mgmt *mgmt, size_t len, 144d45c4172SEmmanuel Grumbach struct ieee80211_channel *channel) 1455484e237SJohannes Berg { 146d45c4172SEmmanuel Grumbach bool beacon = ieee80211_is_beacon(mgmt->frame_control); 147*78ac51f8SSara Sharon struct cfg80211_bss *cbss, *non_tx_cbss; 148*78ac51f8SSara Sharon struct ieee80211_bss *bss, *non_tx_bss; 149162dd6a7SJohannes Berg struct cfg80211_inform_bss bss_meta = { 150162dd6a7SJohannes Berg .boottime_ns = rx_status->boottime_ns, 151162dd6a7SJohannes Berg }; 15274d803b6SSara Sharon bool signal_valid; 1537947d3e0SAvraham Stern struct ieee80211_sub_if_data *scan_sdata; 1544abb52a4SSara Sharon struct ieee802_11_elems elems; 1554abb52a4SSara Sharon size_t baselen; 1564abb52a4SSara Sharon u8 *elements; 1572a519311SJohannes Berg 1581ad22fb5STosoni if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL) 1591ad22fb5STosoni bss_meta.signal = 0; /* invalid signal indication */ 1601ad22fb5STosoni else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) 16161f6bba0SJohannes Berg bss_meta.signal = rx_status->signal * 100; 16230686bf7SJohannes Berg else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) 16361f6bba0SJohannes Berg bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal; 1642a519311SJohannes Berg 16561f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20; 166da6a4352SJohannes Berg if (rx_status->bw == RATE_INFO_BW_5) 16761f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5; 168da6a4352SJohannes Berg else if (rx_status->bw == RATE_INFO_BW_10) 16961f6bba0SJohannes Berg bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10; 1707ca15a0aSSimon Wunderlich 17161f6bba0SJohannes Berg bss_meta.chan = channel; 1727947d3e0SAvraham Stern 1737947d3e0SAvraham Stern rcu_read_lock(); 1747947d3e0SAvraham Stern scan_sdata = rcu_dereference(local->scan_sdata); 1757947d3e0SAvraham Stern if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION && 1767947d3e0SAvraham Stern scan_sdata->vif.bss_conf.assoc && 1777947d3e0SAvraham Stern ieee80211_have_rx_timestamp(rx_status)) { 1787947d3e0SAvraham Stern bss_meta.parent_tsf = 1797947d3e0SAvraham Stern ieee80211_calculate_rx_timestamp(local, rx_status, 1807947d3e0SAvraham Stern len + FCS_LEN, 24); 1817947d3e0SAvraham Stern ether_addr_copy(bss_meta.parent_bssid, 1827947d3e0SAvraham Stern scan_sdata->vif.bss_conf.bssid); 1837947d3e0SAvraham Stern } 1847947d3e0SAvraham Stern rcu_read_unlock(); 1857947d3e0SAvraham Stern 18661f6bba0SJohannes Berg cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, 18761f6bba0SJohannes Berg mgmt, len, GFP_ATOMIC); 1880c1ad2caSJohannes Berg if (!cbss) 1895484e237SJohannes Berg return NULL; 1904abb52a4SSara Sharon 1914abb52a4SSara Sharon if (ieee80211_is_probe_resp(mgmt->frame_control)) { 1924abb52a4SSara Sharon elements = mgmt->u.probe_resp.variable; 1934abb52a4SSara Sharon baselen = offsetof(struct ieee80211_mgmt, 1944abb52a4SSara Sharon u.probe_resp.variable); 1954abb52a4SSara Sharon } else { 1964abb52a4SSara Sharon baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 1974abb52a4SSara Sharon elements = mgmt->u.beacon.variable; 1984abb52a4SSara Sharon } 1994abb52a4SSara Sharon 2004abb52a4SSara Sharon if (baselen > len) 2014abb52a4SSara Sharon return NULL; 2024abb52a4SSara Sharon 2034abb52a4SSara Sharon ieee802_11_parse_elems(elements, len - baselen, false, &elems, 2044abb52a4SSara Sharon mgmt->bssid, cbss->bssid); 2054abb52a4SSara Sharon 20674d803b6SSara Sharon /* In case the signal is invalid update the status */ 20774d803b6SSara Sharon signal_valid = abs(channel->center_freq - cbss->channel->center_freq) 20874d803b6SSara Sharon <= local->hw.wiphy->max_adj_channel_rssi_comp; 20974d803b6SSara Sharon if (!signal_valid) 21074d803b6SSara Sharon rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL; 21100d3f14cSJohannes Berg 2120c1ad2caSJohannes Berg bss = (void *)cbss->priv; 213fcea7db5SSara Sharon ieee80211_update_bss_from_elems(local, bss, &elems, rx_status, beacon); 214817cee76SAlexander Bondar 215*78ac51f8SSara Sharon list_for_each_entry(non_tx_cbss, &cbss->nontrans_list, nontrans_list) { 216*78ac51f8SSara Sharon non_tx_bss = (void *)non_tx_cbss->priv; 217*78ac51f8SSara Sharon 218*78ac51f8SSara Sharon ieee80211_update_bss_from_elems(local, non_tx_bss, &elems, 219*78ac51f8SSara Sharon rx_status, beacon); 220*78ac51f8SSara Sharon } 221*78ac51f8SSara Sharon 2225484e237SJohannes Berg return bss; 2235484e237SJohannes Berg } 2240a51b27eSJohannes Berg 22540b0bd24SRoee Zamir static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata, 22640b0bd24SRoee Zamir u32 scan_flags, const u8 *da) 22740b0bd24SRoee Zamir { 22840b0bd24SRoee Zamir if (!sdata) 22940b0bd24SRoee Zamir return false; 23040b0bd24SRoee Zamir /* accept broadcast for OCE */ 23140b0bd24SRoee Zamir if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP && 23240b0bd24SRoee Zamir is_broadcast_ether_addr(da)) 23340b0bd24SRoee Zamir return true; 23440b0bd24SRoee Zamir if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 23540b0bd24SRoee Zamir return true; 23640b0bd24SRoee Zamir return ether_addr_equal(da, sdata->vif.addr); 23740b0bd24SRoee Zamir } 23840b0bd24SRoee Zamir 239d48b2968SJohannes Berg void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb) 24098c8fccfSJohannes Berg { 241f1d58c25SJohannes Berg struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 242d48b2968SJohannes Berg struct ieee80211_sub_if_data *sdata1, *sdata2; 243d48b2968SJohannes Berg struct ieee80211_mgmt *mgmt = (void *)skb->data; 244c2b13452SJohannes Berg struct ieee80211_bss *bss; 24598c8fccfSJohannes Berg struct ieee80211_channel *channel; 24698c8fccfSJohannes Berg 247d48b2968SJohannes Berg if (skb->len < 24 || 248d48b2968SJohannes Berg (!ieee80211_is_probe_resp(mgmt->frame_control) && 249d48b2968SJohannes Berg !ieee80211_is_beacon(mgmt->frame_control))) 250d48b2968SJohannes Berg return; 25198c8fccfSJohannes Berg 252d48b2968SJohannes Berg sdata1 = rcu_dereference(local->scan_sdata); 253d48b2968SJohannes Berg sdata2 = rcu_dereference(local->sched_scan_sdata); 25498c8fccfSJohannes Berg 255d48b2968SJohannes Berg if (likely(!sdata1 && !sdata2)) 256d48b2968SJohannes Berg return; 25798c8fccfSJohannes Berg 258d48b2968SJohannes Berg if (ieee80211_is_probe_resp(mgmt->frame_control)) { 259a344d677SJohannes Berg struct cfg80211_scan_request *scan_req; 260a344d677SJohannes Berg struct cfg80211_sched_scan_request *sched_scan_req; 26140b0bd24SRoee Zamir u32 scan_req_flags = 0, sched_scan_req_flags = 0; 262a344d677SJohannes Berg 263a344d677SJohannes Berg scan_req = rcu_dereference(local->scan_req); 264a344d677SJohannes Berg sched_scan_req = rcu_dereference(local->sched_scan_req); 265a344d677SJohannes Berg 26640b0bd24SRoee Zamir if (scan_req) 26740b0bd24SRoee Zamir scan_req_flags = scan_req->flags; 26840b0bd24SRoee Zamir 26940b0bd24SRoee Zamir if (sched_scan_req) 27040b0bd24SRoee Zamir sched_scan_req_flags = sched_scan_req->flags; 27140b0bd24SRoee Zamir 27240b0bd24SRoee Zamir /* ignore ProbeResp to foreign address or non-bcast (OCE) 27340b0bd24SRoee Zamir * unless scanning with randomised address 274a344d677SJohannes Berg */ 27540b0bd24SRoee Zamir if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags, 27640b0bd24SRoee Zamir mgmt->da) && 27740b0bd24SRoee Zamir !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags, 27840b0bd24SRoee Zamir mgmt->da)) 279d48b2968SJohannes Berg return; 28098c8fccfSJohannes Berg } 28198c8fccfSJohannes Berg 2820172bb75SJohannes Berg channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 28398c8fccfSJohannes Berg 28498c8fccfSJohannes Berg if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 285d48b2968SJohannes Berg return; 28698c8fccfSJohannes Berg 287d48b2968SJohannes Berg bss = ieee80211_bss_info_update(local, rx_status, 2884abb52a4SSara Sharon mgmt, skb->len, 289d45c4172SEmmanuel Grumbach channel); 290d048e503SJouni Malinen if (bss) 291d48b2968SJohannes Berg ieee80211_rx_bss_put(local, bss); 29298c8fccfSJohannes Berg } 29398c8fccfSJohannes Berg 2942103dec1SSimon Wunderlich static void 2952103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef, 2962103dec1SSimon Wunderlich enum nl80211_bss_scan_width scan_width) 2972103dec1SSimon Wunderlich { 2982103dec1SSimon Wunderlich memset(chandef, 0, sizeof(*chandef)); 2992103dec1SSimon Wunderlich switch (scan_width) { 3002103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 3012103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_5; 3022103dec1SSimon Wunderlich break; 3032103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 3042103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_10; 3052103dec1SSimon Wunderlich break; 3062103dec1SSimon Wunderlich default: 3072103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 3082103dec1SSimon Wunderlich break; 3092103dec1SSimon Wunderlich } 3102103dec1SSimon Wunderlich } 3112103dec1SSimon Wunderlich 3124d36ec58SJohannes Berg /* return false if no more work */ 3134d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 3144d36ec58SJohannes Berg { 3156ea0a69cSJohannes Berg struct cfg80211_scan_request *req; 3162103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 317c56ef672SDavid Spinadel u8 bands_used = 0; 3184d36ec58SJohannes Berg int i, ielen, n_chans; 319b9771d41SJohannes Berg u32 flags = 0; 3204d36ec58SJohannes Berg 3216ea0a69cSJohannes Berg req = rcu_dereference_protected(local->scan_req, 3226ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 3236ea0a69cSJohannes Berg 324a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 325a754055aSEmmanuel Grumbach return false; 326a754055aSEmmanuel Grumbach 32730686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 328c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 329c56ef672SDavid Spinadel local->hw_scan_req->req.channels[i] = req->channels[i]; 330c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 331c56ef672SDavid Spinadel } 332c56ef672SDavid Spinadel 333c56ef672SDavid Spinadel n_chans = req->n_channels; 334c56ef672SDavid Spinadel } else { 3354d36ec58SJohannes Berg do { 33657fbcce3SJohannes Berg if (local->hw_scan_band == NUM_NL80211_BANDS) 3374d36ec58SJohannes Berg return false; 3384d36ec58SJohannes Berg 3394d36ec58SJohannes Berg n_chans = 0; 340c56ef672SDavid Spinadel 3414d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 342c56ef672SDavid Spinadel if (req->channels[i]->band != 343c56ef672SDavid Spinadel local->hw_scan_band) 344c56ef672SDavid Spinadel continue; 345c56ef672SDavid Spinadel local->hw_scan_req->req.channels[n_chans] = 3464d36ec58SJohannes Berg req->channels[i]; 3474d36ec58SJohannes Berg n_chans++; 348c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 3494d36ec58SJohannes Berg } 3504d36ec58SJohannes Berg 3514d36ec58SJohannes Berg local->hw_scan_band++; 3524d36ec58SJohannes Berg } while (!n_chans); 353c56ef672SDavid Spinadel } 3544d36ec58SJohannes Berg 355c56ef672SDavid Spinadel local->hw_scan_req->req.n_channels = n_chans; 3562103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 3574d36ec58SJohannes Berg 358b9771d41SJohannes Berg if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT) 359b9771d41SJohannes Berg flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 360b9771d41SJohannes Berg 361c56ef672SDavid Spinadel ielen = ieee80211_build_preq_ies(local, 362c56ef672SDavid Spinadel (u8 *)local->hw_scan_req->req.ie, 363c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, 364c56ef672SDavid Spinadel &local->hw_scan_req->ies, 365c56ef672SDavid Spinadel req->ie, req->ie_len, 366b9771d41SJohannes Berg bands_used, req->rates, &chandef, 367b9771d41SJohannes Berg flags); 368c56ef672SDavid Spinadel local->hw_scan_req->req.ie_len = ielen; 369c56ef672SDavid Spinadel local->hw_scan_req->req.no_cck = req->no_cck; 370a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr); 371a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr_mask, 372a344d677SJohannes Berg req->mac_addr_mask); 373e345f44fSJouni Malinen ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid); 3744d36ec58SJohannes Berg 3754d36ec58SJohannes Berg return true; 3764d36ec58SJohannes Berg } 3774d36ec58SJohannes Berg 3788bd2a248SEliad Peller static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 379f3b85252SJohannes Berg { 380f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 381e9da68ddSJohannes Berg bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning); 382a2b70e83SEliad Peller bool was_scanning = local->scanning; 3836ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 384a344d677SJohannes Berg struct ieee80211_sub_if_data *scan_sdata; 3854fa11ec7SSachin Kulkarni struct ieee80211_sub_if_data *sdata; 386f3b85252SJohannes Berg 387e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 388f3b85252SJohannes Berg 3896d3560d4SJohannes Berg /* 3906d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 3916d3560d4SJohannes Berg * we have one at all will be verified by checking 3926d3560d4SJohannes Berg * local->scan_req next), but not to complete it 3936d3560d4SJohannes Berg * successfully. 3946d3560d4SJohannes Berg */ 3956d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 3966d3560d4SJohannes Berg aborted = true; 397f3b85252SJohannes Berg 398e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 399d07bfd8bSJohannes Berg return; 400f3b85252SJohannes Berg 401c56ef672SDavid Spinadel if (hw_scan && !aborted && 40230686bf7SJohannes Berg !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) && 403c56ef672SDavid Spinadel ieee80211_prep_hw_scan(local)) { 404e2fd5dbcSJohannes Berg int rc; 405e2fd5dbcSJohannes Berg 406e2fd5dbcSJohannes Berg rc = drv_hw_scan(local, 407e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 408e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)), 409e2fd5dbcSJohannes Berg local->hw_scan_req); 410e2fd5dbcSJohannes Berg 4116eb11a9aSStanislaw Gruszka if (rc == 0) 412d07bfd8bSJohannes Berg return; 4137947d3e0SAvraham Stern 4147d10f6b1SJohannes Berg /* HW scan failed and is going to be reported as aborted, 4157d10f6b1SJohannes Berg * so clear old scan info. 4167947d3e0SAvraham Stern */ 4177947d3e0SAvraham Stern memset(&local->scan_info, 0, sizeof(local->scan_info)); 4187d10f6b1SJohannes Berg aborted = true; 4194d36ec58SJohannes Berg } 4204d36ec58SJohannes Berg 4214d36ec58SJohannes Berg kfree(local->hw_scan_req); 4224d36ec58SJohannes Berg local->hw_scan_req = NULL; 423f3b85252SJohannes Berg 4246ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 4256ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 4266ea0a69cSJohannes Berg 4271d76250bSAvraham Stern if (scan_req != local->int_scan_req) { 4287947d3e0SAvraham Stern local->scan_info.aborted = aborted; 4297947d3e0SAvraham Stern cfg80211_scan_done(scan_req, &local->scan_info); 4301d76250bSAvraham Stern } 4316ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 432a344d677SJohannes Berg 433a344d677SJohannes Berg scan_sdata = rcu_dereference_protected(local->scan_sdata, 434a344d677SJohannes Berg lockdep_is_held(&local->mtx)); 4350c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 4362a519311SJohannes Berg 437fbe9c429SHelmut Schaa local->scanning = 0; 4387ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 439f3b85252SJohannes Berg 44059bdf3b0SBen Greear /* Set power back to normal operating levels. */ 44159bdf3b0SBen Greear ieee80211_hw_config(local, 0); 442b23b025fSBen Greear 4438bd2a248SEliad Peller if (!hw_scan) { 444b23b025fSBen Greear ieee80211_configure_filter(local); 445a344d677SJohannes Berg drv_sw_scan_complete(local, scan_sdata); 446aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 447b23b025fSBen Greear } 448b23b025fSBen Greear 4495cff20e6SJohannes Berg ieee80211_recalc_idle(local); 450e229f844SStanislaw Gruszka 4510a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 45246900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 4534fa11ec7SSachin Kulkarni 4544fa11ec7SSachin Kulkarni /* Requeue all the work that might have been ignored while 4554fa11ec7SSachin Kulkarni * the scan was in progress; if there was none this will 4564fa11ec7SSachin Kulkarni * just be a no-op for the particular interface. 4574fa11ec7SSachin Kulkarni */ 4584fa11ec7SSachin Kulkarni list_for_each_entry_rcu(sdata, &local->interfaces, list) { 4594fa11ec7SSachin Kulkarni if (ieee80211_sdata_running(sdata)) 4604fa11ec7SSachin Kulkarni ieee80211_queue_work(&sdata->local->hw, &sdata->work); 4614fa11ec7SSachin Kulkarni } 4624fa11ec7SSachin Kulkarni 463a2b70e83SEliad Peller if (was_scanning) 4642eb278e0SJohannes Berg ieee80211_start_next_roc(local); 4650a51b27eSJohannes Berg } 4668789d459SJohannes Berg 4677947d3e0SAvraham Stern void ieee80211_scan_completed(struct ieee80211_hw *hw, 4687947d3e0SAvraham Stern struct cfg80211_scan_info *info) 4698789d459SJohannes Berg { 4708789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 4718789d459SJohannes Berg 47258bd7f11SJohannes Berg trace_api_scan_completed(local, info->aborted); 4738789d459SJohannes Berg 4748789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 4757947d3e0SAvraham Stern if (info->aborted) 4768789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 4777947d3e0SAvraham Stern 4787947d3e0SAvraham Stern memcpy(&local->scan_info, info, sizeof(*info)); 4797947d3e0SAvraham Stern 4808789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 4818789d459SJohannes Berg } 4820a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 4830a51b27eSJohannes Berg 484a344d677SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local, 485a344d677SJohannes Berg struct ieee80211_sub_if_data *sdata) 486f3b85252SJohannes Berg { 487fe57d9f5SJohannes Berg /* Software scan is not supported in multi-channel cases */ 488fe57d9f5SJohannes Berg if (local->use_chanctx) 489fe57d9f5SJohannes Berg return -EOPNOTSUPP; 490fe57d9f5SJohannes Berg 491f3b85252SJohannes Berg /* 492f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 493f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 494f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 495f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 496f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 497f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 498f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 499f3b85252SJohannes Berg * 500f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 501f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 502f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 503f3b85252SJohannes Berg */ 504a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 505f3b85252SJohannes Berg 506de312db3SRajkumar Manoharan local->leave_oper_channel_time = jiffies; 507977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 508f3b85252SJohannes Berg local->scan_channel_idx = 0; 509f3b85252SJohannes Berg 510aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 511a80f7c0bSJohannes Berg 5129c35d7d2SSeth Forshee /* ensure nullfunc is transmitted before leaving operating channel */ 5133b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 5149c35d7d2SSeth Forshee 5153ac64beeSJohannes Berg ieee80211_configure_filter(local); 516f3b85252SJohannes Berg 51759bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 51859bdf3b0SBen Greear ieee80211_hw_config(local, 0); 51959bdf3b0SBen Greear 52042935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 52107ef03eeSJohannes Berg &local->scan_work, 0); 522f3b85252SJohannes Berg 523f3b85252SJohannes Berg return 0; 524f3b85252SJohannes Berg } 525f3b85252SJohannes Berg 526133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local, 527133d40f9SStanislaw Gruszka struct ieee80211_sub_if_data *sdata) 528133d40f9SStanislaw Gruszka { 5295cbc95a7SEliad Peller if (ieee80211_is_radar_required(local)) 530164eb02dSSimon Wunderlich return false; 531164eb02dSSimon Wunderlich 5322eb278e0SJohannes Berg if (!list_empty(&local->roc_list)) 533133d40f9SStanislaw Gruszka return false; 534133d40f9SStanislaw Gruszka 535133d40f9SStanislaw Gruszka if (sdata->vif.type == NL80211_IFTYPE_STATION && 536392b9ffbSStanislaw Gruszka sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 537133d40f9SStanislaw Gruszka return false; 538133d40f9SStanislaw Gruszka 539133d40f9SStanislaw Gruszka return true; 540133d40f9SStanislaw Gruszka } 541133d40f9SStanislaw Gruszka 542133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local) 543133d40f9SStanislaw Gruszka { 544133d40f9SStanislaw Gruszka lockdep_assert_held(&local->mtx); 545133d40f9SStanislaw Gruszka 546133d40f9SStanislaw Gruszka if (!local->scan_req || local->scanning) 547133d40f9SStanislaw Gruszka return; 548133d40f9SStanislaw Gruszka 549e2fd5dbcSJohannes Berg if (!ieee80211_can_scan(local, 550e2fd5dbcSJohannes Berg rcu_dereference_protected( 551e2fd5dbcSJohannes Berg local->scan_sdata, 552e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)))) 553133d40f9SStanislaw Gruszka return; 554133d40f9SStanislaw Gruszka 555133d40f9SStanislaw Gruszka ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 556133d40f9SStanislaw Gruszka round_jiffies_relative(0)); 557133d40f9SStanislaw Gruszka } 558f3b85252SJohannes Berg 55945ad6834SJohannes Berg static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata, 56045ad6834SJohannes Berg const u8 *src, const u8 *dst, 56145ad6834SJohannes Berg const u8 *ssid, size_t ssid_len, 56245ad6834SJohannes Berg const u8 *ie, size_t ie_len, 56345ad6834SJohannes Berg u32 ratemask, u32 flags, u32 tx_flags, 56445ad6834SJohannes Berg struct ieee80211_channel *channel) 56545ad6834SJohannes Berg { 56645ad6834SJohannes Berg struct sk_buff *skb; 567b9771d41SJohannes Berg u32 txdata_flags = 0; 56845ad6834SJohannes Berg 56945ad6834SJohannes Berg skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel, 57045ad6834SJohannes Berg ssid, ssid_len, 57145ad6834SJohannes Berg ie, ie_len, flags); 572b9771d41SJohannes Berg 57345ad6834SJohannes Berg if (skb) { 574b9771d41SJohannes Berg if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) { 575b9771d41SJohannes Berg struct ieee80211_hdr *hdr = (void *)skb->data; 576b9771d41SJohannes Berg u16 sn = get_random_u32(); 577b9771d41SJohannes Berg 578b9771d41SJohannes Berg txdata_flags |= IEEE80211_TX_NO_SEQNO; 579b9771d41SJohannes Berg hdr->seq_ctrl = 580b9771d41SJohannes Berg cpu_to_le16(IEEE80211_SN_TO_SEQ(sn)); 581b9771d41SJohannes Berg } 58245ad6834SJohannes Berg IEEE80211_SKB_CB(skb)->flags |= tx_flags; 583b9771d41SJohannes Berg ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band, 584b9771d41SJohannes Berg txdata_flags); 58545ad6834SJohannes Berg } 58645ad6834SJohannes Berg } 58745ad6834SJohannes Berg 5888a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 5898a690674SBen Greear unsigned long *next_delay) 5908a690674SBen Greear { 5918a690674SBen Greear int i; 592e2fd5dbcSJohannes Berg struct ieee80211_sub_if_data *sdata; 5936ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 59457fbcce3SJohannes Berg enum nl80211_band band = local->hw.conf.chandef.chan->band; 595b9771d41SJohannes Berg u32 flags = 0, tx_flags; 5966c17b77bSSeth Forshee 5976ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 5986ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 5996ea0a69cSJohannes Berg 6006c17b77bSSeth Forshee tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 6016ea0a69cSJohannes Berg if (scan_req->no_cck) 6026c17b77bSSeth Forshee tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 603b9771d41SJohannes Berg if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT) 604b9771d41SJohannes Berg flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 605b9771d41SJohannes Berg if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN) 606b9771d41SJohannes Berg flags |= IEEE80211_PROBE_FLAG_RANDOM_SN; 6078a690674SBen Greear 608e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 609316b6b5dSPeter Senna Tschudin lockdep_is_held(&local->mtx)); 610e2fd5dbcSJohannes Berg 6116ea0a69cSJohannes Berg for (i = 0; i < scan_req->n_ssids; i++) 61245ad6834SJohannes Berg ieee80211_send_scan_probe_req( 613e345f44fSJouni Malinen sdata, local->scan_addr, scan_req->bssid, 6146ea0a69cSJohannes Berg scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len, 6156ea0a69cSJohannes Berg scan_req->ie, scan_req->ie_len, 616b9771d41SJohannes Berg scan_req->rates[band], flags, 61745ad6834SJohannes Berg tx_flags, local->hw.conf.chandef.chan); 6188a690674SBen Greear 6198a690674SBen Greear /* 6208a690674SBen Greear * After sending probe requests, wait for probe responses 6218a690674SBen Greear * on the channel. 6228a690674SBen Greear */ 6238a690674SBen Greear *next_delay = IEEE80211_CHANNEL_TIME; 6248a690674SBen Greear local->next_scan_state = SCAN_DECISION; 6258a690674SBen Greear } 6268a690674SBen Greear 627f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 628f3b85252SJohannes Berg struct cfg80211_scan_request *req) 629f3b85252SJohannes Berg { 630f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 631e9da68ddSJohannes Berg bool hw_scan = local->ops->hw_scan; 632f3b85252SJohannes Berg int rc; 633f3b85252SJohannes Berg 634e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 635e229f844SStanislaw Gruszka 6362726f23dSEliad Peller if (local->scan_req || ieee80211_is_radar_required(local)) 637f3b85252SJohannes Berg return -EBUSY; 638f3b85252SJohannes Berg 639133d40f9SStanislaw Gruszka if (!ieee80211_can_scan(local, sdata)) { 6406e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 6416ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 642e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 643c0ce77b8SJohannes Berg return 0; 644c0ce77b8SJohannes Berg } 645c0ce77b8SJohannes Berg 646e9da68ddSJohannes Berg again: 647e9da68ddSJohannes Berg if (hw_scan) { 648f3b85252SJohannes Berg u8 *ies; 649f3b85252SJohannes Berg 650e4dcbb37SDavid Spinadel local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len; 651c56ef672SDavid Spinadel 65230686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 653c56ef672SDavid Spinadel int i, n_bands = 0; 654c56ef672SDavid Spinadel u8 bands_counted = 0; 655c56ef672SDavid Spinadel 656c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 657c56ef672SDavid Spinadel if (bands_counted & BIT(req->channels[i]->band)) 658c56ef672SDavid Spinadel continue; 659c56ef672SDavid Spinadel bands_counted |= BIT(req->channels[i]->band); 660c56ef672SDavid Spinadel n_bands++; 661c56ef672SDavid Spinadel } 662c56ef672SDavid Spinadel 663c56ef672SDavid Spinadel local->hw_scan_ies_bufsize *= n_bands; 664c56ef672SDavid Spinadel } 665c56ef672SDavid Spinadel 6664d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 6674d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 6684d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 669c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, GFP_KERNEL); 6704d36ec58SJohannes Berg if (!local->hw_scan_req) 671f3b85252SJohannes Berg return -ENOMEM; 672f3b85252SJohannes Berg 673c56ef672SDavid Spinadel local->hw_scan_req->req.ssids = req->ssids; 674c56ef672SDavid Spinadel local->hw_scan_req->req.n_ssids = req->n_ssids; 6754d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 6764d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 6774d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 678c56ef672SDavid Spinadel local->hw_scan_req->req.ie = ies; 679c56ef672SDavid Spinadel local->hw_scan_req->req.flags = req->flags; 680e345f44fSJouni Malinen eth_broadcast_addr(local->hw_scan_req->req.bssid); 6817947d3e0SAvraham Stern local->hw_scan_req->req.duration = req->duration; 6827947d3e0SAvraham Stern local->hw_scan_req->req.duration_mandatory = 6837947d3e0SAvraham Stern req->duration_mandatory; 6844d36ec58SJohannes Berg 6854d36ec58SJohannes Berg local->hw_scan_band = 0; 6866e7e6213SJohn W. Linville 6876e7e6213SJohn W. Linville /* 6886e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 6896e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 6906e7e6213SJohn W. Linville * anything that might be changed here and leave 6916e7e6213SJohn W. Linville * this function early must not go after this 6926e7e6213SJohn W. Linville * allocation. 6936e7e6213SJohn W. Linville */ 694f3b85252SJohannes Berg } 695f3b85252SJohannes Berg 6966ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 697e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 698f3b85252SJohannes Berg 699a344d677SJohannes Berg if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 700a344d677SJohannes Berg get_random_mask_addr(local->scan_addr, 701a344d677SJohannes Berg req->mac_addr, 702a344d677SJohannes Berg req->mac_addr_mask); 703a344d677SJohannes Berg else 704a344d677SJohannes Berg memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN); 705a344d677SJohannes Berg 706e9da68ddSJohannes Berg if (hw_scan) { 707fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 7088a690674SBen Greear } else if ((req->n_channels == 1) && 709675a0b04SKarl Beldan (req->channels[0] == local->_oper_chandef.chan)) { 7109b864870SJohannes Berg /* 7119b864870SJohannes Berg * If we are scanning only on the operating channel 7129b864870SJohannes Berg * then we do not need to stop normal activities 7138a690674SBen Greear */ 7148a690674SBen Greear unsigned long next_delay; 7158a690674SBen Greear 7168a690674SBen Greear __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 7178a690674SBen Greear 7188a690674SBen Greear ieee80211_recalc_idle(local); 7198a690674SBen Greear 7208a690674SBen Greear /* Notify driver scan is starting, keep order of operations 7218a690674SBen Greear * same as normal software scan, in case that matters. */ 722a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 7238a690674SBen Greear 7248a690674SBen Greear ieee80211_configure_filter(local); /* accept probe-responses */ 7258a690674SBen Greear 7268a690674SBen Greear /* We need to ensure power level is at max for scanning. */ 7278a690674SBen Greear ieee80211_hw_config(local, 0); 7288a690674SBen Greear 7294e39ccacSAntonio Quartulli if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR | 7304e39ccacSAntonio Quartulli IEEE80211_CHAN_RADAR)) || 7316ea0a69cSJohannes Berg !req->n_ssids) { 7328a690674SBen Greear next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 7338a690674SBen Greear } else { 7348a690674SBen Greear ieee80211_scan_state_send_probe(local, &next_delay); 7358a690674SBen Greear next_delay = IEEE80211_CHANNEL_TIME; 7368a690674SBen Greear } 7378a690674SBen Greear 7388a690674SBen Greear /* Now, just wait a bit and we are all done! */ 7398a690674SBen Greear ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 7408a690674SBen Greear next_delay); 7418a690674SBen Greear return 0; 7428a690674SBen Greear } else { 7438a690674SBen Greear /* Do normal software scan */ 744fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 7458a690674SBen Greear } 7466e7e6213SJohn W. Linville 7475cff20e6SJohannes Berg ieee80211_recalc_idle(local); 748f3b85252SJohannes Berg 749e9da68ddSJohannes Berg if (hw_scan) { 7504d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 751a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 752a344d677SJohannes Berg } else { 753a344d677SJohannes Berg rc = ieee80211_start_sw_scan(local, sdata); 754a344d677SJohannes Berg } 755f3b85252SJohannes Berg 756f3b85252SJohannes Berg if (rc) { 7574d36ec58SJohannes Berg kfree(local->hw_scan_req); 7584d36ec58SJohannes Berg local->hw_scan_req = NULL; 759fbe9c429SHelmut Schaa local->scanning = 0; 760f3b85252SJohannes Berg 7615cff20e6SJohannes Berg ieee80211_recalc_idle(local); 7625cff20e6SJohannes Berg 763f3b85252SJohannes Berg local->scan_req = NULL; 7640c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 765f3b85252SJohannes Berg } 766f3b85252SJohannes Berg 767e9da68ddSJohannes Berg if (hw_scan && rc == 1) { 768e9da68ddSJohannes Berg /* 769e9da68ddSJohannes Berg * we can't fall back to software for P2P-GO 770e9da68ddSJohannes Berg * as it must update NoA etc. 771e9da68ddSJohannes Berg */ 772e9da68ddSJohannes Berg if (ieee80211_vif_type_p2p(&sdata->vif) == 773e9da68ddSJohannes Berg NL80211_IFTYPE_P2P_GO) 774e9da68ddSJohannes Berg return -EOPNOTSUPP; 775e9da68ddSJohannes Berg hw_scan = false; 776e9da68ddSJohannes Berg goto again; 777e9da68ddSJohannes Berg } 778e9da68ddSJohannes Berg 779f3b85252SJohannes Berg return rc; 780f3b85252SJohannes Berg } 781f3b85252SJohannes Berg 782df13cce5SHelmut Schaa static unsigned long 783df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 784df13cce5SHelmut Schaa { 785df13cce5SHelmut Schaa /* 786df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 787df13cce5SHelmut Schaa * add that delay as well to get a better estimation 788df13cce5SHelmut Schaa */ 7894e39ccacSAntonio Quartulli if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) 790df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 791df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 792df13cce5SHelmut Schaa } 793df13cce5SHelmut Schaa 794e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 7952fb3f028SHelmut Schaa unsigned long *next_delay) 7962fb3f028SHelmut Schaa { 797142b9f50SHelmut Schaa bool associated = false; 798df13cce5SHelmut Schaa bool tx_empty = true; 799df13cce5SHelmut Schaa bool bad_latency; 800142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 801df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 802cd2bb512SSam Leffler enum mac80211_scan_state next_scan_state; 8036ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 804142b9f50SHelmut Schaa 805df13cce5SHelmut Schaa /* 806df13cce5SHelmut Schaa * check if at least one STA interface is associated, 807df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 808df13cce5SHelmut Schaa * and grab the lowest used beacon interval 809df13cce5SHelmut Schaa */ 810142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 811142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 8129607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 813142b9f50SHelmut Schaa continue; 814142b9f50SHelmut Schaa 815142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 816142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 817142b9f50SHelmut Schaa associated = true; 818df13cce5SHelmut Schaa 819df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 820df13cce5SHelmut Schaa tx_empty = false; 821142b9f50SHelmut Schaa break; 822142b9f50SHelmut Schaa } 823142b9f50SHelmut Schaa } 824142b9f50SHelmut Schaa } 825df13cce5SHelmut Schaa } 826142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 827142b9f50SHelmut Schaa 8286ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 8296ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 8306ea0a69cSJohannes Berg 8316ea0a69cSJohannes Berg next_chan = scan_req->channels[local->scan_channel_idx]; 832b23b025fSBen Greear 833142b9f50SHelmut Schaa /* 834142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 835df13cce5SHelmut Schaa * see if we can scan another channel without interfering 836df13cce5SHelmut Schaa * with the current traffic situation. 837df13cce5SHelmut Schaa * 8383f892b61SStanislaw Gruszka * Keep good latency, do not stay off-channel more than 125 ms. 839142b9f50SHelmut Schaa */ 840df13cce5SHelmut Schaa 841df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 842df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 8433f892b61SStanislaw Gruszka local->leave_oper_channel_time + HZ / 8); 844df13cce5SHelmut Schaa 845cd2bb512SSam Leffler if (associated && !tx_empty) { 8466ea0a69cSJohannes Berg if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 847cd2bb512SSam Leffler next_scan_state = SCAN_ABORT; 848142b9f50SHelmut Schaa else 849cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 8503f892b61SStanislaw Gruszka } else if (associated && bad_latency) { 851cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 852cd2bb512SSam Leffler } else { 853cd2bb512SSam Leffler next_scan_state = SCAN_SET_CHANNEL; 854cd2bb512SSam Leffler } 855cd2bb512SSam Leffler 856cd2bb512SSam Leffler local->next_scan_state = next_scan_state; 857142b9f50SHelmut Schaa 858142b9f50SHelmut Schaa *next_delay = 0; 8592fb3f028SHelmut Schaa } 8602fb3f028SHelmut Schaa 8612fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 8627d3be3ccSHelmut Schaa unsigned long *next_delay) 8637d3be3ccSHelmut Schaa { 8647d3be3ccSHelmut Schaa int skip; 8657d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 8667ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width oper_scan_width; 8676ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 8686ea0a69cSJohannes Berg 8696ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 8706ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 8717d3be3ccSHelmut Schaa 8727d3be3ccSHelmut Schaa skip = 0; 8736ea0a69cSJohannes Berg chan = scan_req->channels[local->scan_channel_idx]; 8747d3be3ccSHelmut Schaa 8757ca15a0aSSimon Wunderlich local->scan_chandef.chan = chan; 8767ca15a0aSSimon Wunderlich local->scan_chandef.center_freq1 = chan->center_freq; 8777ca15a0aSSimon Wunderlich local->scan_chandef.center_freq2 = 0; 8786ea0a69cSJohannes Berg switch (scan_req->scan_width) { 8797ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 8807ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 8817ca15a0aSSimon Wunderlich break; 8827ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 8837ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 8847ca15a0aSSimon Wunderlich break; 8857ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_20: 8867ca15a0aSSimon Wunderlich /* If scanning on oper channel, use whatever channel-type 8877ca15a0aSSimon Wunderlich * is currently in use. 8887ca15a0aSSimon Wunderlich */ 8897ca15a0aSSimon Wunderlich oper_scan_width = cfg80211_chandef_to_scan_width( 8907ca15a0aSSimon Wunderlich &local->_oper_chandef); 8917ca15a0aSSimon Wunderlich if (chan == local->_oper_chandef.chan && 8926ea0a69cSJohannes Berg oper_scan_width == scan_req->scan_width) 8937ca15a0aSSimon Wunderlich local->scan_chandef = local->_oper_chandef; 8947ca15a0aSSimon Wunderlich else 8957ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 8967ca15a0aSSimon Wunderlich break; 8977ca15a0aSSimon Wunderlich } 898b23b025fSBen Greear 899584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 9007d3be3ccSHelmut Schaa skip = 1; 9017d3be3ccSHelmut Schaa 9027d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 9037d3be3ccSHelmut Schaa local->scan_channel_idx++; 9047d3be3ccSHelmut Schaa 9050ee9c13cSHelmut Schaa if (skip) { 9060ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 9070ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 9082fb3f028SHelmut Schaa return; 9090ee9c13cSHelmut Schaa } 9107d3be3ccSHelmut Schaa 9117d3be3ccSHelmut Schaa /* 9127d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 9137d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 9147d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 9157d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 9167d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 9177d3be3ccSHelmut Schaa * received. 9187d3be3ccSHelmut Schaa * 9197d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 9207d3be3ccSHelmut Schaa */ 9214e39ccacSAntonio Quartulli if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) || 9224e39ccacSAntonio Quartulli !scan_req->n_ssids) { 9237d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 924977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 9252fb3f028SHelmut Schaa return; 9267d3be3ccSHelmut Schaa } 9277d3be3ccSHelmut Schaa 9282fb3f028SHelmut Schaa /* active scan, send probes */ 9297d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 930977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 9317d3be3ccSHelmut Schaa } 9327d3be3ccSHelmut Schaa 93307ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 93407ef03eeSJohannes Berg unsigned long *next_delay) 93507ef03eeSJohannes Berg { 93607ef03eeSJohannes Berg /* switch back to the operating channel */ 9377ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 93807ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 93907ef03eeSJohannes Berg 940aacde9eeSStanislaw Gruszka /* disable PS */ 941aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 94207ef03eeSJohannes Berg 94307ef03eeSJohannes Berg *next_delay = HZ / 5; 94407ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 94507ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 94607ef03eeSJohannes Berg } 94707ef03eeSJohannes Berg 94807ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 94907ef03eeSJohannes Berg unsigned long *next_delay) 95007ef03eeSJohannes Berg { 951aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 95207ef03eeSJohannes Berg 95307ef03eeSJohannes Berg if (local->ops->flush) { 9543b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 95507ef03eeSJohannes Berg *next_delay = 0; 95607ef03eeSJohannes Berg } else 95707ef03eeSJohannes Berg *next_delay = HZ / 10; 95807ef03eeSJohannes Berg 95907ef03eeSJohannes Berg /* remember when we left the operating channel */ 96007ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 96107ef03eeSJohannes Berg 96207ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 963de2ee84dSMohammed Shafi Shajakhan local->next_scan_state = SCAN_SET_CHANNEL; 96407ef03eeSJohannes Berg } 96507ef03eeSJohannes Berg 966c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 9670a51b27eSJohannes Berg { 9680a51b27eSJohannes Berg struct ieee80211_local *local = 9690a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 970d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 9716ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 9720a51b27eSJohannes Berg unsigned long next_delay = 0; 9738bd2a248SEliad Peller bool aborted; 9748789d459SJohannes Berg 975259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 976259b62e3SStanislaw Gruszka 977332ff7feSLuciano Coelho if (!ieee80211_can_run_worker(local)) { 978332ff7feSLuciano Coelho aborted = true; 979332ff7feSLuciano Coelho goto out_complete; 980332ff7feSLuciano Coelho } 981332ff7feSLuciano Coelho 982e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 983e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)); 9846ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 9856ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 986d07bfd8bSJohannes Berg 9878a690674SBen Greear /* When scanning on-channel, the first-callback means completed. */ 9888a690674SBen Greear if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 9898a690674SBen Greear aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 9908a690674SBen Greear goto out_complete; 9918a690674SBen Greear } 9928a690674SBen Greear 993259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 9948789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 995259b62e3SStanislaw Gruszka goto out_complete; 9968789d459SJohannes Berg } 9978789d459SJohannes Berg 9986ea0a69cSJohannes Berg if (!sdata || !scan_req) 999259b62e3SStanislaw Gruszka goto out; 1000f3b85252SJohannes Berg 1001ff5db439SEliad Peller if (!local->scanning) { 1002f3b85252SJohannes Berg int rc; 1003f3b85252SJohannes Berg 10046ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 10050c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 1006f3b85252SJohannes Berg 10076ea0a69cSJohannes Berg rc = __ieee80211_start_scan(sdata, scan_req); 1008259b62e3SStanislaw Gruszka if (rc) { 10093aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 10106ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, scan_req); 1011259b62e3SStanislaw Gruszka aborted = true; 1012259b62e3SStanislaw Gruszka goto out_complete; 1013259b62e3SStanislaw Gruszka } else 1014259b62e3SStanislaw Gruszka goto out; 1015f3b85252SJohannes Berg } 1016f3b85252SJohannes Berg 10175bc75728SJohannes Berg /* 1018f502d09bSHelmut Schaa * as long as no delay is required advance immediately 1019f502d09bSHelmut Schaa * without scheduling a new work 1020f502d09bSHelmut Schaa */ 1021f502d09bSHelmut Schaa do { 1022c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 1023c29acf20SRajkumar Manoharan aborted = true; 1024c29acf20SRajkumar Manoharan goto out_complete; 1025c29acf20SRajkumar Manoharan } 1026c29acf20SRajkumar Manoharan 1027977923b0SHelmut Schaa switch (local->next_scan_state) { 10282fb3f028SHelmut Schaa case SCAN_DECISION: 1029e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 10306ea0a69cSJohannes Berg if (local->scan_channel_idx >= scan_req->n_channels) { 1031e229f844SStanislaw Gruszka aborted = false; 1032e229f844SStanislaw Gruszka goto out_complete; 1033e229f844SStanislaw Gruszka } 1034e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 10350a51b27eSJohannes Berg break; 10362fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 10372fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 10382fb3f028SHelmut Schaa break; 10390a51b27eSJohannes Berg case SCAN_SEND_PROBE: 10407d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 10410a51b27eSJohannes Berg break; 104207ef03eeSJohannes Berg case SCAN_SUSPEND: 104307ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 1044142b9f50SHelmut Schaa break; 104507ef03eeSJohannes Berg case SCAN_RESUME: 104607ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 1047142b9f50SHelmut Schaa break; 1048cd2bb512SSam Leffler case SCAN_ABORT: 1049cd2bb512SSam Leffler aborted = true; 1050cd2bb512SSam Leffler goto out_complete; 10510a51b27eSJohannes Berg } 1052f502d09bSHelmut Schaa } while (next_delay == 0); 10530a51b27eSJohannes Berg 105442935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 1055d07bfd8bSJohannes Berg goto out; 1056259b62e3SStanislaw Gruszka 1057259b62e3SStanislaw Gruszka out_complete: 10588bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, aborted); 1059259b62e3SStanislaw Gruszka out: 1060259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 10610a51b27eSJohannes Berg } 10620a51b27eSJohannes Berg 1063c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 10642a519311SJohannes Berg struct cfg80211_scan_request *req) 10650a51b27eSJohannes Berg { 1066f3b85252SJohannes Berg int res; 10670a51b27eSJohannes Berg 1068a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 1069f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 1070a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 10712a519311SJohannes Berg 1072f3b85252SJohannes Berg return res; 10730a51b27eSJohannes Berg } 10740a51b27eSJohannes Berg 107534bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 1076be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 107776bed0f4SJanusz.Dziedzic@tieto.com struct ieee80211_channel **channels, 107876bed0f4SJanusz.Dziedzic@tieto.com unsigned int n_channels, 10797ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width) 1080f3b85252SJohannes Berg { 1081f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 108276bed0f4SJanusz.Dziedzic@tieto.com int ret = -EBUSY, i, n_ch = 0; 108357fbcce3SJohannes Berg enum nl80211_band band; 10849116dd01SJohannes Berg 1085a1699b75SJohannes Berg mutex_lock(&local->mtx); 1086f3b85252SJohannes Berg 1087f3b85252SJohannes Berg /* busy scanning */ 1088f3b85252SJohannes Berg if (local->scan_req) 1089f3b85252SJohannes Berg goto unlock; 1090f3b85252SJohannes Berg 1091be4a4b6aSJohannes Berg /* fill internal scan request */ 109276bed0f4SJanusz.Dziedzic@tieto.com if (!channels) { 109376bed0f4SJanusz.Dziedzic@tieto.com int max_n; 1094be4a4b6aSJohannes Berg 109557fbcce3SJohannes Berg for (band = 0; band < NUM_NL80211_BANDS; band++) { 1096be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 1097be4a4b6aSJohannes Berg continue; 109834bcf715SStanislaw Gruszka 109934bcf715SStanislaw Gruszka max_n = local->hw.wiphy->bands[band]->n_channels; 110034bcf715SStanislaw Gruszka for (i = 0; i < max_n; i++) { 110134bcf715SStanislaw Gruszka struct ieee80211_channel *tmp_ch = 1102be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 110334bcf715SStanislaw Gruszka 11048fe02e16SLuis R. Rodriguez if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 110534bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED)) 110634bcf715SStanislaw Gruszka continue; 110734bcf715SStanislaw Gruszka 110834bcf715SStanislaw Gruszka local->int_scan_req->channels[n_ch] = tmp_ch; 110934bcf715SStanislaw Gruszka n_ch++; 1110be4a4b6aSJohannes Berg } 1111be4a4b6aSJohannes Berg } 1112be4a4b6aSJohannes Berg 111334bcf715SStanislaw Gruszka if (WARN_ON_ONCE(n_ch == 0)) 111434bcf715SStanislaw Gruszka goto unlock; 111534bcf715SStanislaw Gruszka 111634bcf715SStanislaw Gruszka local->int_scan_req->n_channels = n_ch; 1117be4a4b6aSJohannes Berg } else { 111876bed0f4SJanusz.Dziedzic@tieto.com for (i = 0; i < n_channels; i++) { 111976bed0f4SJanusz.Dziedzic@tieto.com if (channels[i]->flags & (IEEE80211_CHAN_NO_IR | 112076bed0f4SJanusz.Dziedzic@tieto.com IEEE80211_CHAN_DISABLED)) 112176bed0f4SJanusz.Dziedzic@tieto.com continue; 112276bed0f4SJanusz.Dziedzic@tieto.com 112376bed0f4SJanusz.Dziedzic@tieto.com local->int_scan_req->channels[n_ch] = channels[i]; 112476bed0f4SJanusz.Dziedzic@tieto.com n_ch++; 112576bed0f4SJanusz.Dziedzic@tieto.com } 112676bed0f4SJanusz.Dziedzic@tieto.com 112776bed0f4SJanusz.Dziedzic@tieto.com if (WARN_ON_ONCE(n_ch == 0)) 112834bcf715SStanislaw Gruszka goto unlock; 112934bcf715SStanislaw Gruszka 113076bed0f4SJanusz.Dziedzic@tieto.com local->int_scan_req->n_channels = n_ch; 1131be4a4b6aSJohannes Berg } 1132be4a4b6aSJohannes Berg 1133be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 1134be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 11357ca15a0aSSimon Wunderlich local->int_scan_req->scan_width = scan_width; 11365ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 11375ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 1138f3b85252SJohannes Berg 11395ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 1140f3b85252SJohannes Berg unlock: 1141a1699b75SJohannes Berg mutex_unlock(&local->mtx); 1142f3b85252SJohannes Berg return ret; 11430a51b27eSJohannes Berg } 11445bb644a0SJohannes Berg 11454136c422SStanislaw Gruszka /* 11464136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 11474136c422SStanislaw Gruszka */ 11485bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 11495bb644a0SJohannes Berg { 11505bb644a0SJohannes Berg /* 1151b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 11524136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 11534136c422SStanislaw Gruszka * 11544136c422SStanislaw Gruszka * Regarding hardware scan: 11554136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 11564136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 11574136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 11584136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 11594136c422SStanislaw Gruszka * 11604136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 11614136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 11624136c422SStanislaw Gruszka * 1163b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 1164b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 1165b856439bSEliad Peller * after the scan was completed/aborted. 11665bb644a0SJohannes Berg */ 11674136c422SStanislaw Gruszka 1168a1699b75SJohannes Berg mutex_lock(&local->mtx); 1169b856439bSEliad Peller if (!local->scan_req) 1170b856439bSEliad Peller goto out; 1171b856439bSEliad Peller 1172a754055aSEmmanuel Grumbach /* 1173a754055aSEmmanuel Grumbach * We have a scan running and the driver already reported completion, 1174a754055aSEmmanuel Grumbach * but the worker hasn't run yet or is stuck on the mutex - mark it as 1175a754055aSEmmanuel Grumbach * cancelled. 1176a754055aSEmmanuel Grumbach */ 1177a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 1178a754055aSEmmanuel Grumbach test_bit(SCAN_COMPLETED, &local->scanning)) { 1179a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1180a754055aSEmmanuel Grumbach goto out; 1181a754055aSEmmanuel Grumbach } 1182a754055aSEmmanuel Grumbach 1183b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 1184a754055aSEmmanuel Grumbach /* 1185a754055aSEmmanuel Grumbach * Make sure that __ieee80211_scan_completed doesn't trigger a 1186a754055aSEmmanuel Grumbach * scan on another band. 1187a754055aSEmmanuel Grumbach */ 1188a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1189b856439bSEliad Peller if (local->ops->cancel_hw_scan) 1190e2fd5dbcSJohannes Berg drv_cancel_hw_scan(local, 1191e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 1192e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx))); 1193b856439bSEliad Peller goto out; 1194b856439bSEliad Peller } 1195b856439bSEliad Peller 1196d07bfd8bSJohannes Berg /* 1197d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 1198d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 1199d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 1200d07bfd8bSJohannes Berg */ 1201d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 1202d07bfd8bSJohannes Berg /* and clean up */ 12037947d3e0SAvraham Stern memset(&local->scan_info, 0, sizeof(local->scan_info)); 12048bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, true); 1205b856439bSEliad Peller out: 1206d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 12075bb644a0SJohannes Berg } 120879f460caSLuciano Coelho 1209d43c6b6eSDavid Spinadel int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 121079f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 121179f460caSLuciano Coelho { 121279f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 1213633e2713SDavid Spinadel struct ieee80211_scan_ies sched_scan_ies = {}; 12142103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 1215633e2713SDavid Spinadel int ret, i, iebufsz, num_bands = 0; 121657fbcce3SJohannes Berg u32 rate_masks[NUM_NL80211_BANDS] = {}; 1217633e2713SDavid Spinadel u8 bands_used = 0; 1218633e2713SDavid Spinadel u8 *ie; 1219b9771d41SJohannes Berg u32 flags = 0; 1220c604b9f2SJohannes Berg 1221e4dcbb37SDavid Spinadel iebufsz = local->scan_ies_len + req->ie_len; 122279f460caSLuciano Coelho 1223d43c6b6eSDavid Spinadel lockdep_assert_held(&local->mtx); 122479f460caSLuciano Coelho 1225d43c6b6eSDavid Spinadel if (!local->ops->sched_scan_start) 1226d43c6b6eSDavid Spinadel return -ENOTSUPP; 122779f460caSLuciano Coelho 122857fbcce3SJohannes Berg for (i = 0; i < NUM_NL80211_BANDS; i++) { 1229633e2713SDavid Spinadel if (local->hw.wiphy->bands[i]) { 1230633e2713SDavid Spinadel bands_used |= BIT(i); 1231633e2713SDavid Spinadel rate_masks[i] = (u32) -1; 1232633e2713SDavid Spinadel num_bands++; 1233633e2713SDavid Spinadel } 1234633e2713SDavid Spinadel } 1235c56ef672SDavid Spinadel 1236b9771d41SJohannes Berg if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT) 1237b9771d41SJohannes Berg flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 1238b9771d41SJohannes Berg 12396396bb22SKees Cook ie = kcalloc(iebufsz, num_bands, GFP_KERNEL); 1240633e2713SDavid Spinadel if (!ie) { 124179f460caSLuciano Coelho ret = -ENOMEM; 1242633e2713SDavid Spinadel goto out; 124379f460caSLuciano Coelho } 124479f460caSLuciano Coelho 12452103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 12462103dec1SSimon Wunderlich 1247cd5861bdSKirtika Ruchandani ieee80211_build_preq_ies(local, ie, num_bands * iebufsz, 1248633e2713SDavid Spinadel &sched_scan_ies, req->ie, 124900387f32SJohannes Berg req->ie_len, bands_used, rate_masks, &chandef, 1250b9771d41SJohannes Berg flags); 125179f460caSLuciano Coelho 125230dd3edfSJohannes Berg ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 1253d43c6b6eSDavid Spinadel if (ret == 0) { 12545260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, sdata); 12556ea0a69cSJohannes Berg rcu_assign_pointer(local->sched_scan_req, req); 1256d43c6b6eSDavid Spinadel } 125779f460caSLuciano Coelho 1258633e2713SDavid Spinadel kfree(ie); 1259d43c6b6eSDavid Spinadel 1260633e2713SDavid Spinadel out: 1261d43c6b6eSDavid Spinadel if (ret) { 1262d43c6b6eSDavid Spinadel /* Clean in case of failure after HW restart or upon resume. */ 12630c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 12646ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1265d43c6b6eSDavid Spinadel } 1266d43c6b6eSDavid Spinadel 1267d43c6b6eSDavid Spinadel return ret; 1268d43c6b6eSDavid Spinadel } 1269d43c6b6eSDavid Spinadel 1270d43c6b6eSDavid Spinadel int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1271d43c6b6eSDavid Spinadel struct cfg80211_sched_scan_request *req) 1272d43c6b6eSDavid Spinadel { 1273d43c6b6eSDavid Spinadel struct ieee80211_local *local = sdata->local; 1274d43c6b6eSDavid Spinadel int ret; 1275d43c6b6eSDavid Spinadel 1276d43c6b6eSDavid Spinadel mutex_lock(&local->mtx); 1277d43c6b6eSDavid Spinadel 1278d43c6b6eSDavid Spinadel if (rcu_access_pointer(local->sched_scan_sdata)) { 1279d43c6b6eSDavid Spinadel mutex_unlock(&local->mtx); 1280d43c6b6eSDavid Spinadel return -EBUSY; 1281d43c6b6eSDavid Spinadel } 1282d43c6b6eSDavid Spinadel 1283d43c6b6eSDavid Spinadel ret = __ieee80211_request_sched_scan_start(sdata, req); 1284d43c6b6eSDavid Spinadel 12855260a5b2SJohannes Berg mutex_unlock(&local->mtx); 128679f460caSLuciano Coelho return ret; 128779f460caSLuciano Coelho } 128879f460caSLuciano Coelho 12890d440ea2SEliad Peller int ieee80211_request_sched_scan_stop(struct ieee80211_local *local) 129079f460caSLuciano Coelho { 12910d440ea2SEliad Peller struct ieee80211_sub_if_data *sched_scan_sdata; 12920d440ea2SEliad Peller int ret = -ENOENT; 129379f460caSLuciano Coelho 12945260a5b2SJohannes Berg mutex_lock(&local->mtx); 129579f460caSLuciano Coelho 129679f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 129779f460caSLuciano Coelho ret = -ENOTSUPP; 129879f460caSLuciano Coelho goto out; 129979f460caSLuciano Coelho } 130079f460caSLuciano Coelho 1301d43c6b6eSDavid Spinadel /* We don't want to restart sched scan anymore. */ 13026ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1303d43c6b6eSDavid Spinadel 13040d440ea2SEliad Peller sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata, 13050d440ea2SEliad Peller lockdep_is_held(&local->mtx)); 13060d440ea2SEliad Peller if (sched_scan_sdata) { 13070d440ea2SEliad Peller ret = drv_sched_scan_stop(local, sched_scan_sdata); 130871228a1eSAlexander Bondar if (!ret) 1309ad053a96SAndreea-Cristina Bernat RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 131071228a1eSAlexander Bondar } 131179f460caSLuciano Coelho out: 13125260a5b2SJohannes Berg mutex_unlock(&local->mtx); 131379f460caSLuciano Coelho 131479f460caSLuciano Coelho return ret; 131579f460caSLuciano Coelho } 131679f460caSLuciano Coelho 131779f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 131879f460caSLuciano Coelho { 131979f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 132079f460caSLuciano Coelho 132179f460caSLuciano Coelho trace_api_sched_scan_results(local); 132279f460caSLuciano Coelho 1323b34939b9SArend Van Spriel cfg80211_sched_scan_results(hw->wiphy, 0); 132479f460caSLuciano Coelho } 132579f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 132679f460caSLuciano Coelho 1327f6837ba8SJohannes Berg void ieee80211_sched_scan_end(struct ieee80211_local *local) 132885a9994aSLuciano Coelho { 132985a9994aSLuciano Coelho mutex_lock(&local->mtx); 133085a9994aSLuciano Coelho 13315260a5b2SJohannes Berg if (!rcu_access_pointer(local->sched_scan_sdata)) { 133285a9994aSLuciano Coelho mutex_unlock(&local->mtx); 133385a9994aSLuciano Coelho return; 133485a9994aSLuciano Coelho } 133585a9994aSLuciano Coelho 13360c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 133785a9994aSLuciano Coelho 1338d43c6b6eSDavid Spinadel /* If sched scan was aborted by the driver. */ 13396ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1340d43c6b6eSDavid Spinadel 134185a9994aSLuciano Coelho mutex_unlock(&local->mtx); 134285a9994aSLuciano Coelho 1343b34939b9SArend Van Spriel cfg80211_sched_scan_stopped(local->hw.wiphy, 0); 134485a9994aSLuciano Coelho } 134585a9994aSLuciano Coelho 1346f6837ba8SJohannes Berg void ieee80211_sched_scan_stopped_work(struct work_struct *work) 1347f6837ba8SJohannes Berg { 1348f6837ba8SJohannes Berg struct ieee80211_local *local = 1349f6837ba8SJohannes Berg container_of(work, struct ieee80211_local, 1350f6837ba8SJohannes Berg sched_scan_stopped_work); 1351f6837ba8SJohannes Berg 1352f6837ba8SJohannes Berg ieee80211_sched_scan_end(local); 1353f6837ba8SJohannes Berg } 1354f6837ba8SJohannes Berg 135579f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 135679f460caSLuciano Coelho { 135779f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 135879f460caSLuciano Coelho 135979f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 136079f460caSLuciano Coelho 13612bc533bdSEliad Peller /* 13622bc533bdSEliad Peller * this shouldn't really happen, so for simplicity 13632bc533bdSEliad Peller * simply ignore it, and let mac80211 reconfigure 13642bc533bdSEliad Peller * the sched scan later on. 13652bc533bdSEliad Peller */ 13662bc533bdSEliad Peller if (local->in_reconfig) 13672bc533bdSEliad Peller return; 13682bc533bdSEliad Peller 136918db594aSJohannes Berg schedule_work(&local->sched_scan_stopped_work); 137079f460caSLuciano Coelho } 137179f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1372