blob: 5e6b275afc9e62ff1bcc4952db86ea7ee84bc55c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Berg0a51b272008-09-08 17:44:25 +02002/*
Johannes Berg5484e232008-09-08 17:44:27 +02003 * Scanning implementation
4 *
Johannes Berg0a51b272008-09-08 17:44:25 +02005 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6 * Copyright 2004, Instant802 Networks, Inc.
7 * Copyright 2005, Devicescape Software, Inc.
8 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
9 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
Sara Sharon74d803b2015-06-03 10:44:17 +030010 * Copyright 2013-2015 Intel Mobile Communications GmbH
Roee Zamir40b0bd22017-08-06 11:38:23 +030011 * Copyright 2016-2017 Intel Deutschland GmbH
Johannes Berg5d248282021-09-20 15:40:10 +020012 * Copyright (C) 2018-2021 Intel Corporation
Johannes Berg0a51b272008-09-08 17:44:25 +020013 */
14
Johannes Berg0a51b272008-09-08 17:44:25 +020015#include <linux/if_arp.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010016#include <linux/etherdevice.h>
Johannes Berg078e1e62009-01-22 18:07:31 +010017#include <linux/rtnetlink.h>
Helmut Schaadf13cce2010-02-24 14:19:21 +010018#include <net/sch_generic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040020#include <linux/export.h>
Johannes Bergb9771d42018-05-28 15:47:41 +020021#include <linux/random.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020022#include <net/mac80211.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020023
24#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg5484e232008-09-08 17:44:27 +020026#include "mesh.h"
Johannes Berg0a51b272008-09-08 17:44:25 +020027
28#define IEEE80211_PROBE_DELAY (HZ / 33)
29#define IEEE80211_CHANNEL_TIME (HZ / 33)
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +010030#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
Johannes Berg0a51b272008-09-08 17:44:25 +020031
Johannes Berg5484e232008-09-08 17:44:27 +020032void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020033 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020034{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +010035 if (!bss)
36 return;
Johannes Berg5b112d32013-02-01 01:49:58 +010037 cfg80211_put_bss(local->hw.wiphy,
38 container_of((void *)bss, struct cfg80211_bss, priv));
Johannes Berg5484e232008-09-08 17:44:27 +020039}
40
Kalle Valoab133152010-01-12 10:42:31 +020041static bool is_uapsd_supported(struct ieee802_11_elems *elems)
42{
43 u8 qos_info;
44
45 if (elems->wmm_info && elems->wmm_info_len == 7
46 && elems->wmm_info[5] == 1)
47 qos_info = elems->wmm_info[6];
48 else if (elems->wmm_param && elems->wmm_param_len == 24
49 && elems->wmm_param[5] == 1)
50 qos_info = elems->wmm_param[6];
51 else
52 /* no valid wmm information or parameter element found */
53 return false;
54
55 return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
56}
57
Sara Sharonfcea7db2019-01-16 20:35:38 +020058static void
59ieee80211_update_bss_from_elems(struct ieee80211_local *local,
60 struct ieee80211_bss *bss,
61 struct ieee802_11_elems *elems,
62 struct ieee80211_rx_status *rx_status,
63 bool beacon)
64{
65 int clen, srlen;
66
67 if (beacon)
68 bss->device_ts_beacon = rx_status->device_timestamp;
69 else
70 bss->device_ts_presp = rx_status->device_timestamp;
71
72 if (elems->parse_error) {
73 if (beacon)
74 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
75 else
76 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
77 } else {
78 if (beacon)
79 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
80 else
81 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
82 }
83
84 /* save the ERP value so that it is available at association time */
85 if (elems->erp_info && (!elems->parse_error ||
86 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
87 bss->erp_value = elems->erp_info[0];
88 bss->has_erp_value = true;
89 if (!elems->parse_error)
90 bss->valid_data |= IEEE80211_BSS_VALID_ERP;
91 }
92
93 /* replace old supported rates if we get new values */
94 if (!elems->parse_error ||
95 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
96 srlen = 0;
97 if (elems->supp_rates) {
98 clen = IEEE80211_MAX_SUPP_RATES;
99 if (clen > elems->supp_rates_len)
100 clen = elems->supp_rates_len;
101 memcpy(bss->supp_rates, elems->supp_rates, clen);
102 srlen += clen;
103 }
104 if (elems->ext_supp_rates) {
105 clen = IEEE80211_MAX_SUPP_RATES - srlen;
106 if (clen > elems->ext_supp_rates_len)
107 clen = elems->ext_supp_rates_len;
108 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
109 clen);
110 srlen += clen;
111 }
112 if (srlen) {
113 bss->supp_rates_len = srlen;
114 if (!elems->parse_error)
115 bss->valid_data |= IEEE80211_BSS_VALID_RATES;
116 }
117 }
118
119 if (!elems->parse_error ||
120 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
121 bss->wmm_used = elems->wmm_param || elems->wmm_info;
122 bss->uapsd_supported = is_uapsd_supported(elems);
123 if (!elems->parse_error)
124 bss->valid_data |= IEEE80211_BSS_VALID_WMM;
125 }
126
127 if (beacon) {
128 struct ieee80211_supported_band *sband =
129 local->hw.wiphy->bands[rx_status->band];
130 if (!(rx_status->encoding == RX_ENC_HT) &&
131 !(rx_status->encoding == RX_ENC_VHT))
132 bss->beacon_rate =
133 &sband->bitrates[rx_status->rate_idx];
134 }
Johannes Berg2a333a02020-05-28 21:34:35 +0200135
136 if (elems->vht_cap_elem)
137 bss->vht_cap_info =
138 le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
139 else
140 bss->vht_cap_info = 0;
Sara Sharonfcea7db2019-01-16 20:35:38 +0200141}
142
Johannes Bergc2b13452008-09-11 00:01:55 +0200143struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200144ieee80211_bss_info_update(struct ieee80211_local *local,
145 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200146 struct ieee80211_mgmt *mgmt, size_t len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200147 struct ieee80211_channel *channel)
Johannes Berg5484e232008-09-08 17:44:27 +0200148{
Thomas Pedersencd418ba2020-09-21 19:28:08 -0700149 bool beacon = ieee80211_is_beacon(mgmt->frame_control) ||
150 ieee80211_is_s1g_beacon(mgmt->frame_control);
Sara Sharon78ac51f2019-01-16 18:22:56 +0200151 struct cfg80211_bss *cbss, *non_tx_cbss;
152 struct ieee80211_bss *bss, *non_tx_bss;
Johannes Berg162dd6a2016-02-23 23:05:06 +0200153 struct cfg80211_inform_bss bss_meta = {
154 .boottime_ns = rx_status->boottime_ns,
155 };
Sara Sharon74d803b2015-06-03 10:44:17 +0300156 bool signal_valid;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300157 struct ieee80211_sub_if_data *scan_sdata;
Johannes Berg5d248282021-09-20 15:40:10 +0200158 struct ieee802_11_elems *elems;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200159 size_t baselen;
160 u8 *elements;
Johannes Berg2a519312009-02-10 21:25:55 +0100161
Tosoni1ad22fb2018-03-14 16:58:34 +0000162 if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)
163 bss_meta.signal = 0; /* invalid signal indication */
164 else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Johannes Berg61f6bba2015-10-13 11:36:21 +0200165 bss_meta.signal = rx_status->signal * 100;
Johannes Berg30686bf2015-06-02 21:39:54 +0200166 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))
Johannes Berg61f6bba2015-10-13 11:36:21 +0200167 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100168
Johannes Berg61f6bba2015-10-13 11:36:21 +0200169 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20;
Johannes Bergda6a4352017-04-26 12:14:59 +0200170 if (rx_status->bw == RATE_INFO_BW_5)
Johannes Berg61f6bba2015-10-13 11:36:21 +0200171 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5;
Johannes Bergda6a4352017-04-26 12:14:59 +0200172 else if (rx_status->bw == RATE_INFO_BW_10)
Johannes Berg61f6bba2015-10-13 11:36:21 +0200173 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200174
Johannes Berg61f6bba2015-10-13 11:36:21 +0200175 bss_meta.chan = channel;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300176
177 rcu_read_lock();
178 scan_sdata = rcu_dereference(local->scan_sdata);
179 if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&
180 scan_sdata->vif.bss_conf.assoc &&
181 ieee80211_have_rx_timestamp(rx_status)) {
182 bss_meta.parent_tsf =
183 ieee80211_calculate_rx_timestamp(local, rx_status,
184 len + FCS_LEN, 24);
185 ether_addr_copy(bss_meta.parent_bssid,
186 scan_sdata->vif.bss_conf.bssid);
187 }
188 rcu_read_unlock();
189
Johannes Berg61f6bba2015-10-13 11:36:21 +0200190 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,
191 mgmt, len, GFP_ATOMIC);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100192 if (!cbss)
Johannes Berg00d3f142009-02-10 21:26:00 +0100193 return NULL;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200194
195 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
196 elements = mgmt->u.probe_resp.variable;
197 baselen = offsetof(struct ieee80211_mgmt,
198 u.probe_resp.variable);
Thomas Pedersencd418ba2020-09-21 19:28:08 -0700199 } else if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
200 struct ieee80211_ext *ext = (void *) mgmt;
201
202 baselen = offsetof(struct ieee80211_ext, u.s1g_beacon.variable);
203 elements = ext->u.s1g_beacon.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200204 } else {
205 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
206 elements = mgmt->u.beacon.variable;
207 }
208
209 if (baselen > len)
210 return NULL;
211
Johannes Berg5d248282021-09-20 15:40:10 +0200212 elems = ieee802_11_parse_elems(elements, len - baselen, false,
213 mgmt->bssid, cbss->bssid);
214 if (!elems)
215 return NULL;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200216
Sara Sharon74d803b2015-06-03 10:44:17 +0300217 /* In case the signal is invalid update the status */
Emmanuel Grumbach7bb106e2020-02-14 23:23:38 +0100218 signal_valid = channel == cbss->channel;
Sara Sharon74d803b2015-06-03 10:44:17 +0300219 if (!signal_valid)
220 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
Johannes Berg00d3f142009-02-10 21:26:00 +0100221
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100222 bss = (void *)cbss->priv;
Johannes Berg5d248282021-09-20 15:40:10 +0200223 ieee80211_update_bss_from_elems(local, bss, elems, rx_status, beacon);
Alexander Bondar817cee72013-05-19 14:23:57 +0300224
Sara Sharon78ac51f2019-01-16 18:22:56 +0200225 list_for_each_entry(non_tx_cbss, &cbss->nontrans_list, nontrans_list) {
226 non_tx_bss = (void *)non_tx_cbss->priv;
227
Johannes Berg5d248282021-09-20 15:40:10 +0200228 ieee80211_update_bss_from_elems(local, non_tx_bss, elems,
Sara Sharon78ac51f2019-01-16 18:22:56 +0200229 rx_status, beacon);
230 }
231
Johannes Berg5d248282021-09-20 15:40:10 +0200232 kfree(elems);
233
Johannes Berg5484e232008-09-08 17:44:27 +0200234 return bss;
235}
Johannes Berg0a51b272008-09-08 17:44:25 +0200236
Roee Zamir40b0bd22017-08-06 11:38:23 +0300237static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
238 u32 scan_flags, const u8 *da)
239{
240 if (!sdata)
241 return false;
242 /* accept broadcast for OCE */
243 if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP &&
244 is_broadcast_ether_addr(da))
245 return true;
246 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
247 return true;
248 return ether_addr_equal(da, sdata->vif.addr);
249}
250
Johannes Bergd48b2962012-07-06 22:19:27 +0200251void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200252{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200253 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Bergd48b2962012-07-06 22:19:27 +0200254 struct ieee80211_sub_if_data *sdata1, *sdata2;
255 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Johannes Bergc2b13452008-09-11 00:01:55 +0200256 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200257 struct ieee80211_channel *channel;
Du Chenge298aa32021-05-10 12:16:49 +0800258 size_t min_hdr_len = offsetof(struct ieee80211_mgmt,
259 u.probe_resp.variable);
260
261 if (!ieee80211_is_probe_resp(mgmt->frame_control) &&
262 !ieee80211_is_beacon(mgmt->frame_control) &&
263 !ieee80211_is_s1g_beacon(mgmt->frame_control))
264 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200265
Thomas Pedersencd418ba2020-09-21 19:28:08 -0700266 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
Du Chenge298aa32021-05-10 12:16:49 +0800267 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
268 min_hdr_len = offsetof(struct ieee80211_ext,
269 u.s1g_short_beacon.variable);
270 else
271 min_hdr_len = offsetof(struct ieee80211_ext,
272 u.s1g_beacon);
273 }
274
275 if (skb->len < min_hdr_len)
Johannes Bergd48b2962012-07-06 22:19:27 +0200276 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200277
Johannes Bergd48b2962012-07-06 22:19:27 +0200278 sdata1 = rcu_dereference(local->scan_sdata);
279 sdata2 = rcu_dereference(local->sched_scan_sdata);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200280
Johannes Bergd48b2962012-07-06 22:19:27 +0200281 if (likely(!sdata1 && !sdata2))
282 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200283
Johannes Bergd48b2962012-07-06 22:19:27 +0200284 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Johannes Berga344d672014-06-12 22:24:31 +0200285 struct cfg80211_scan_request *scan_req;
286 struct cfg80211_sched_scan_request *sched_scan_req;
Roee Zamir40b0bd22017-08-06 11:38:23 +0300287 u32 scan_req_flags = 0, sched_scan_req_flags = 0;
Johannes Berga344d672014-06-12 22:24:31 +0200288
289 scan_req = rcu_dereference(local->scan_req);
290 sched_scan_req = rcu_dereference(local->sched_scan_req);
291
Roee Zamir40b0bd22017-08-06 11:38:23 +0300292 if (scan_req)
293 scan_req_flags = scan_req->flags;
294
295 if (sched_scan_req)
296 sched_scan_req_flags = sched_scan_req->flags;
297
298 /* ignore ProbeResp to foreign address or non-bcast (OCE)
299 * unless scanning with randomised address
Johannes Berga344d672014-06-12 22:24:31 +0200300 */
Roee Zamir40b0bd22017-08-06 11:38:23 +0300301 if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags,
302 mgmt->da) &&
303 !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags,
304 mgmt->da))
Johannes Bergd48b2962012-07-06 22:19:27 +0200305 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200306 }
307
Thomas Pedersen3b23c1842020-04-01 18:18:05 -0700308 channel = ieee80211_get_channel_khz(local->hw.wiphy,
309 ieee80211_rx_status_to_khz(rx_status));
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200310
311 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Bergd48b2962012-07-06 22:19:27 +0200312 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200313
Johannes Bergd48b2962012-07-06 22:19:27 +0200314 bss = ieee80211_bss_info_update(local, rx_status,
Sara Sharon4abb52a2019-01-16 12:14:41 +0200315 mgmt, skb->len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200316 channel);
Jouni Malinend048e502008-10-11 03:29:55 +0300317 if (bss)
Johannes Bergd48b2962012-07-06 22:19:27 +0200318 ieee80211_rx_bss_put(local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200319}
320
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200321static void
322ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
323 enum nl80211_bss_scan_width scan_width)
324{
325 memset(chandef, 0, sizeof(*chandef));
326 switch (scan_width) {
327 case NL80211_BSS_CHAN_WIDTH_5:
328 chandef->width = NL80211_CHAN_WIDTH_5;
329 break;
330 case NL80211_BSS_CHAN_WIDTH_10:
331 chandef->width = NL80211_CHAN_WIDTH_10;
332 break;
333 default:
334 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
335 break;
336 }
337}
338
Johannes Berg4d36ec52009-10-27 20:59:55 +0100339/* return false if no more work */
Ilan Peer2ad22742020-05-28 21:34:39 +0200340static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata)
Johannes Berg4d36ec52009-10-27 20:59:55 +0100341{
Ilan Peer2ad22742020-05-28 21:34:39 +0200342 struct ieee80211_local *local = sdata->local;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100343 struct cfg80211_scan_request *req;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200344 struct cfg80211_chan_def chandef;
David Spinadelc56ef672014-02-05 15:21:13 +0200345 u8 bands_used = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100346 int i, ielen, n_chans;
Johannes Bergb9771d42018-05-28 15:47:41 +0200347 u32 flags = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100348
Johannes Berg6ea0a692014-11-19 11:55:49 +0100349 req = rcu_dereference_protected(local->scan_req,
350 lockdep_is_held(&local->mtx));
351
Emmanuel Grumbacha7540552013-09-16 11:12:07 +0300352 if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
353 return false;
354
Johannes Berg30686bf2015-06-02 21:39:54 +0200355 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100356 for (i = 0; i < req->n_channels; i++) {
David Spinadelc56ef672014-02-05 15:21:13 +0200357 local->hw_scan_req->req.channels[i] = req->channels[i];
358 bands_used |= BIT(req->channels[i]->band);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100359 }
360
David Spinadelc56ef672014-02-05 15:21:13 +0200361 n_chans = req->n_channels;
362 } else {
363 do {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200364 if (local->hw_scan_band == NUM_NL80211_BANDS)
David Spinadelc56ef672014-02-05 15:21:13 +0200365 return false;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100366
David Spinadelc56ef672014-02-05 15:21:13 +0200367 n_chans = 0;
368
369 for (i = 0; i < req->n_channels; i++) {
370 if (req->channels[i]->band !=
371 local->hw_scan_band)
372 continue;
373 local->hw_scan_req->req.channels[n_chans] =
374 req->channels[i];
375 n_chans++;
376 bands_used |= BIT(req->channels[i]->band);
377 }
378
379 local->hw_scan_band++;
380 } while (!n_chans);
381 }
382
383 local->hw_scan_req->req.n_channels = n_chans;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200384 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100385
Johannes Bergb9771d42018-05-28 15:47:41 +0200386 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
387 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
388
Ilan Peer2ad22742020-05-28 21:34:39 +0200389 ielen = ieee80211_build_preq_ies(sdata,
David Spinadelc56ef672014-02-05 15:21:13 +0200390 (u8 *)local->hw_scan_req->req.ie,
Johannes Bergc604b9f2012-11-29 12:45:18 +0100391 local->hw_scan_ies_bufsize,
David Spinadelc56ef672014-02-05 15:21:13 +0200392 &local->hw_scan_req->ies,
393 req->ie, req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200394 bands_used, req->rates, &chandef,
395 flags);
David Spinadelc56ef672014-02-05 15:21:13 +0200396 local->hw_scan_req->req.ie_len = ielen;
397 local->hw_scan_req->req.no_cck = req->no_cck;
Johannes Berga344d672014-06-12 22:24:31 +0200398 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
399 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
400 req->mac_addr_mask);
Jouni Malinene345f442016-02-26 22:12:48 +0200401 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100402
403 return true;
404}
405
Eliad Peller8bd2a242013-12-05 11:21:27 +0200406static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200407{
408 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berge9da68d2018-10-18 10:35:47 +0200409 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
Eliad Pellera2b70e82013-12-05 11:21:28 +0200410 bool was_scanning = local->scanning;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100411 struct cfg80211_scan_request *scan_req;
Johannes Berga344d672014-06-12 22:24:31 +0200412 struct ieee80211_sub_if_data *scan_sdata;
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530413 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200414
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200415 lockdep_assert_held(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +0200416
Johannes Berg6d3560d2009-10-31 07:44:08 +0100417 /*
418 * It's ok to abort a not-yet-running scan (that
419 * we have one at all will be verified by checking
420 * local->scan_req next), but not to complete it
421 * successfully.
422 */
423 if (WARN_ON(!local->scanning && !aborted))
424 aborted = true;
Johannes Bergde95a54b2009-04-01 11:58:36 +0200425
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200426 if (WARN_ON(!local->scan_req))
Johannes Bergd07bfd82011-03-07 15:48:41 +0100427 return;
Johannes Bergf3b85252009-04-23 16:01:47 +0200428
Ilan Peer2ad22742020-05-28 21:34:39 +0200429 scan_sdata = rcu_dereference_protected(local->scan_sdata,
430 lockdep_is_held(&local->mtx));
431
David Spinadelc56ef672014-02-05 15:21:13 +0200432 if (hw_scan && !aborted &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200433 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
Ilan Peer2ad22742020-05-28 21:34:39 +0200434 ieee80211_prep_hw_scan(scan_sdata)) {
Johannes Berge2fd5db2012-07-06 21:39:28 +0200435 int rc;
436
437 rc = drv_hw_scan(local,
438 rcu_dereference_protected(local->scan_sdata,
439 lockdep_is_held(&local->mtx)),
440 local->hw_scan_req);
441
Stanislaw Gruszka6eb11a92010-10-06 11:22:11 +0200442 if (rc == 0)
Johannes Bergd07bfd82011-03-07 15:48:41 +0100443 return;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300444
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300445 /* HW scan failed and is going to be reported as aborted,
446 * so clear old scan info.
Avraham Stern7947d3e2016-07-05 15:23:12 +0300447 */
448 memset(&local->scan_info, 0, sizeof(local->scan_info));
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300449 aborted = true;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100450 }
451
452 kfree(local->hw_scan_req);
453 local->hw_scan_req = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200454
Johannes Berg6ea0a692014-11-19 11:55:49 +0100455 scan_req = rcu_dereference_protected(local->scan_req,
456 lockdep_is_held(&local->mtx));
457
Avraham Stern1d762502016-07-05 17:10:13 +0300458 if (scan_req != local->int_scan_req) {
Avraham Stern7947d3e2016-07-05 15:23:12 +0300459 local->scan_info.aborted = aborted;
460 cfg80211_scan_done(scan_req, &local->scan_info);
Avraham Stern1d762502016-07-05 17:10:13 +0300461 }
Johannes Berg6ea0a692014-11-19 11:55:49 +0100462 RCU_INIT_POINTER(local->scan_req, NULL);
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530463 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100464
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200465 local->scanning = 0;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200466 local->scan_chandef.chan = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200467
Johannes Berg07ef03e2011-11-08 16:21:21 +0100468 /* Set power back to normal operating levels. */
469 ieee80211_hw_config(local, 0);
Ben Greearb23b0252011-02-04 11:54:17 -0800470
Eliad Peller8bd2a242013-12-05 11:21:27 +0200471 if (!hw_scan) {
Ben Greearb23b0252011-02-04 11:54:17 -0800472 ieee80211_configure_filter(local);
Johannes Berga344d672014-06-12 22:24:31 +0200473 drv_sw_scan_complete(local, scan_sdata);
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100474 ieee80211_offchannel_return(local);
Ben Greearb23b0252011-02-04 11:54:17 -0800475 }
476
Johannes Berg5cff20e2009-04-29 12:26:17 +0200477 ieee80211_recalc_idle(local);
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200478
Johannes Berg0a51b272008-09-08 17:44:25 +0200479 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg46900292009-02-15 12:44:28 +0100480 ieee80211_ibss_notify_scan_completed(local);
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530481
482 /* Requeue all the work that might have been ignored while
483 * the scan was in progress; if there was none this will
484 * just be a no-op for the particular interface.
485 */
486 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
487 if (ieee80211_sdata_running(sdata))
488 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
489 }
490
Eliad Pellera2b70e82013-12-05 11:21:28 +0200491 if (was_scanning)
492 ieee80211_start_next_roc(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200493}
Johannes Berg8789d452010-08-26 13:30:26 +0200494
Avraham Stern7947d3e2016-07-05 15:23:12 +0300495void ieee80211_scan_completed(struct ieee80211_hw *hw,
496 struct cfg80211_scan_info *info)
Johannes Berg8789d452010-08-26 13:30:26 +0200497{
498 struct ieee80211_local *local = hw_to_local(hw);
499
Johannes Berg58bd7f12016-09-14 09:37:54 +0200500 trace_api_scan_completed(local, info->aborted);
Johannes Berg8789d452010-08-26 13:30:26 +0200501
502 set_bit(SCAN_COMPLETED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300503 if (info->aborted)
Johannes Berg8789d452010-08-26 13:30:26 +0200504 set_bit(SCAN_ABORTED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300505
506 memcpy(&local->scan_info, info, sizeof(*info));
507
Johannes Berg8789d452010-08-26 13:30:26 +0200508 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
509}
Johannes Berg0a51b272008-09-08 17:44:25 +0200510EXPORT_SYMBOL(ieee80211_scan_completed);
511
Johannes Berga344d672014-06-12 22:24:31 +0200512static int ieee80211_start_sw_scan(struct ieee80211_local *local,
513 struct ieee80211_sub_if_data *sdata)
Johannes Bergf3b85252009-04-23 16:01:47 +0200514{
Johannes Bergfe57d9f2012-07-26 14:55:08 +0200515 /* Software scan is not supported in multi-channel cases */
516 if (local->use_chanctx)
517 return -EOPNOTSUPP;
518
Johannes Bergf3b85252009-04-23 16:01:47 +0200519 /*
520 * Hardware/driver doesn't support hw_scan, so use software
521 * scanning instead. First send a nullfunc frame with power save
522 * bit on so that AP will buffer the frames for us while we are not
523 * listening, then send probe requests to each channel and wait for
524 * the responses. After all channels are scanned, tune back to the
525 * original channel and send a nullfunc frame with power save bit
526 * off to trigger the AP to send us all the buffered frames.
527 *
528 * Note that while local->sw_scanning is true everything else but
529 * nullfunc frames and probe requests will be dropped in
530 * ieee80211_tx_h_check_assoc().
531 */
Johannes Berga344d672014-06-12 22:24:31 +0200532 drv_sw_scan_start(local, sdata, local->scan_addr);
Johannes Bergf3b85252009-04-23 16:01:47 +0200533
Rajkumar Manoharande312db2012-03-27 11:01:06 +0530534 local->leave_oper_channel_time = jiffies;
Helmut Schaa977923b2009-07-23 12:14:20 +0200535 local->next_scan_state = SCAN_DECISION;
Johannes Bergf3b85252009-04-23 16:01:47 +0200536 local->scan_channel_idx = 0;
537
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100538 ieee80211_offchannel_stop_vifs(local);
Johannes Berga80f7c02009-12-23 13:15:32 +0100539
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600540 /* ensure nullfunc is transmitted before leaving operating channel */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200541 ieee80211_flush_queues(local, NULL, false);
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600542
Johannes Berg3ac64be2009-08-17 16:16:53 +0200543 ieee80211_configure_filter(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200544
Ben Greear59bdf3b2011-02-07 13:44:38 -0800545 /* We need to set power level at maximum rate for scanning. */
546 ieee80211_hw_config(local, 0);
547
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400548 ieee80211_queue_delayed_work(&local->hw,
Johannes Berg07ef03e2011-11-08 16:21:21 +0100549 &local->scan_work, 0);
Johannes Bergf3b85252009-04-23 16:01:47 +0200550
551 return 0;
552}
553
Aaron Komisardc0c18e2019-10-02 13:59:07 +0000554static bool __ieee80211_can_leave_ch(struct ieee80211_sub_if_data *sdata)
555{
556 struct ieee80211_local *local = sdata->local;
557 struct ieee80211_sub_if_data *sdata_iter;
558
559 if (!ieee80211_is_radar_required(local))
560 return true;
561
562 if (!regulatory_pre_cac_allowed(local->hw.wiphy))
563 return false;
564
565 mutex_lock(&local->iflist_mtx);
566 list_for_each_entry(sdata_iter, &local->interfaces, list) {
567 if (sdata_iter->wdev.cac_started) {
568 mutex_unlock(&local->iflist_mtx);
569 return false;
570 }
571 }
572 mutex_unlock(&local->iflist_mtx);
573
574 return true;
575}
576
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200577static bool ieee80211_can_scan(struct ieee80211_local *local,
578 struct ieee80211_sub_if_data *sdata)
579{
Aaron Komisardc0c18e2019-10-02 13:59:07 +0000580 if (!__ieee80211_can_leave_ch(sdata))
Simon Wunderlich164eb022013-02-08 18:16:20 +0100581 return false;
582
Johannes Berg2eb278e2012-06-05 14:28:42 +0200583 if (!list_empty(&local->roc_list))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200584 return false;
585
586 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +0200587 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200588 return false;
589
590 return true;
591}
592
593void ieee80211_run_deferred_scan(struct ieee80211_local *local)
594{
595 lockdep_assert_held(&local->mtx);
596
597 if (!local->scan_req || local->scanning)
598 return;
599
Johannes Berge2fd5db2012-07-06 21:39:28 +0200600 if (!ieee80211_can_scan(local,
601 rcu_dereference_protected(
602 local->scan_sdata,
603 lockdep_is_held(&local->mtx))))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200604 return;
605
606 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
607 round_jiffies_relative(0));
608}
Johannes Bergf3b85252009-04-23 16:01:47 +0200609
Johannes Berg45ad6832018-05-28 15:47:39 +0200610static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
611 const u8 *src, const u8 *dst,
612 const u8 *ssid, size_t ssid_len,
613 const u8 *ie, size_t ie_len,
614 u32 ratemask, u32 flags, u32 tx_flags,
615 struct ieee80211_channel *channel)
616{
617 struct sk_buff *skb;
618
619 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
620 ssid, ssid_len,
621 ie, ie_len, flags);
Johannes Bergb9771d42018-05-28 15:47:41 +0200622
Johannes Berg45ad6832018-05-28 15:47:39 +0200623 if (skb) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200624 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
625 struct ieee80211_hdr *hdr = (void *)skb->data;
Mathy Vanhoef2b3dab12020-07-23 14:01:51 +0400626 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Bergb9771d42018-05-28 15:47:41 +0200627 u16 sn = get_random_u32();
628
Mathy Vanhoef2b3dab12020-07-23 14:01:51 +0400629 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
Johannes Bergb9771d42018-05-28 15:47:41 +0200630 hdr->seq_ctrl =
631 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
632 }
Johannes Berg45ad6832018-05-28 15:47:39 +0200633 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
Mathy Vanhoef08aca292020-07-23 14:01:52 +0400634 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
Johannes Berg45ad6832018-05-28 15:47:39 +0200635 }
636}
637
Ben Greear8a690672012-04-17 10:54:16 -0700638static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
639 unsigned long *next_delay)
640{
641 int i;
Johannes Berge2fd5db2012-07-06 21:39:28 +0200642 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100643 struct cfg80211_scan_request *scan_req;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200644 enum nl80211_band band = local->hw.conf.chandef.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +0200645 u32 flags = 0, tx_flags;
Seth Forshee6c17b772013-02-11 11:21:07 -0600646
Johannes Berg6ea0a692014-11-19 11:55:49 +0100647 scan_req = rcu_dereference_protected(local->scan_req,
648 lockdep_is_held(&local->mtx));
649
Seth Forshee6c17b772013-02-11 11:21:07 -0600650 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100651 if (scan_req->no_cck)
Seth Forshee6c17b772013-02-11 11:21:07 -0600652 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Bergb9771d42018-05-28 15:47:41 +0200653 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
654 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
655 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
656 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
Ben Greear8a690672012-04-17 10:54:16 -0700657
Johannes Berge2fd5db2012-07-06 21:39:28 +0200658 sdata = rcu_dereference_protected(local->scan_sdata,
Peter Senna Tschudin316b6b52012-09-06 18:09:16 +0200659 lockdep_is_held(&local->mtx));
Johannes Berge2fd5db2012-07-06 21:39:28 +0200660
Johannes Berg6ea0a692014-11-19 11:55:49 +0100661 for (i = 0; i < scan_req->n_ssids; i++)
Johannes Berg45ad6832018-05-28 15:47:39 +0200662 ieee80211_send_scan_probe_req(
Jouni Malinene345f442016-02-26 22:12:48 +0200663 sdata, local->scan_addr, scan_req->bssid,
Johannes Berg6ea0a692014-11-19 11:55:49 +0100664 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
665 scan_req->ie, scan_req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200666 scan_req->rates[band], flags,
Johannes Berg45ad6832018-05-28 15:47:39 +0200667 tx_flags, local->hw.conf.chandef.chan);
Ben Greear8a690672012-04-17 10:54:16 -0700668
669 /*
670 * After sending probe requests, wait for probe responses
671 * on the channel.
672 */
673 *next_delay = IEEE80211_CHANNEL_TIME;
674 local->next_scan_state = SCAN_DECISION;
675}
676
Johannes Bergf3b85252009-04-23 16:01:47 +0200677static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
678 struct cfg80211_scan_request *req)
679{
680 struct ieee80211_local *local = sdata->local;
Johannes Berge9da68d2018-10-18 10:35:47 +0200681 bool hw_scan = local->ops->hw_scan;
Johannes Bergf3b85252009-04-23 16:01:47 +0200682 int rc;
683
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200684 lockdep_assert_held(&local->mtx);
685
Aaron Komisardc0c18e2019-10-02 13:59:07 +0000686 if (local->scan_req)
687 return -EBUSY;
688
689 if (!__ieee80211_can_leave_ch(sdata))
Johannes Bergf3b85252009-04-23 16:01:47 +0200690 return -EBUSY;
691
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200692 if (!ieee80211_can_scan(local, sdata)) {
John W. Linville6e7e6212010-02-08 16:38:38 -0500693 /* wait for the work to finish/time out */
Johannes Berg6ea0a692014-11-19 11:55:49 +0100694 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200695 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergc0ce77b2010-02-03 10:22:31 +0100696 return 0;
697 }
698
Johannes Berge9da68d2018-10-18 10:35:47 +0200699 again:
700 if (hw_scan) {
Johannes Bergf3b85252009-04-23 16:01:47 +0200701 u8 *ies;
Johannes Bergf3b85252009-04-23 16:01:47 +0200702
David Spinadele4dcbb32014-02-11 13:45:41 +0200703 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
David Spinadelc56ef672014-02-05 15:21:13 +0200704
Johannes Berg30686bf2015-06-02 21:39:54 +0200705 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
David Spinadelc56ef672014-02-05 15:21:13 +0200706 int i, n_bands = 0;
707 u8 bands_counted = 0;
708
709 for (i = 0; i < req->n_channels; i++) {
710 if (bands_counted & BIT(req->channels[i]->band))
711 continue;
712 bands_counted |= BIT(req->channels[i]->band);
713 n_bands++;
714 }
715
716 local->hw_scan_ies_bufsize *= n_bands;
717 }
718
Johannes Berg4d36ec52009-10-27 20:59:55 +0100719 local->hw_scan_req = kmalloc(
720 sizeof(*local->hw_scan_req) +
721 req->n_channels * sizeof(req->channels[0]) +
Johannes Bergc604b9f2012-11-29 12:45:18 +0100722 local->hw_scan_ies_bufsize, GFP_KERNEL);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100723 if (!local->hw_scan_req)
Johannes Bergf3b85252009-04-23 16:01:47 +0200724 return -ENOMEM;
725
David Spinadelc56ef672014-02-05 15:21:13 +0200726 local->hw_scan_req->req.ssids = req->ssids;
727 local->hw_scan_req->req.n_ssids = req->n_ssids;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100728 ies = (u8 *)local->hw_scan_req +
729 sizeof(*local->hw_scan_req) +
730 req->n_channels * sizeof(req->channels[0]);
David Spinadelc56ef672014-02-05 15:21:13 +0200731 local->hw_scan_req->req.ie = ies;
732 local->hw_scan_req->req.flags = req->flags;
Jouni Malinene345f442016-02-26 22:12:48 +0200733 eth_broadcast_addr(local->hw_scan_req->req.bssid);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300734 local->hw_scan_req->req.duration = req->duration;
735 local->hw_scan_req->req.duration_mandatory =
736 req->duration_mandatory;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100737
738 local->hw_scan_band = 0;
Tova Mussaic8cb5b82020-09-18 11:33:13 +0200739 local->hw_scan_req->req.n_6ghz_params = req->n_6ghz_params;
740 local->hw_scan_req->req.scan_6ghz_params =
741 req->scan_6ghz_params;
742 local->hw_scan_req->req.scan_6ghz = req->scan_6ghz;
John W. Linville6e7e6212010-02-08 16:38:38 -0500743
744 /*
745 * After allocating local->hw_scan_req, we must
746 * go through until ieee80211_prep_hw_scan(), so
747 * anything that might be changed here and leave
748 * this function early must not go after this
749 * allocation.
750 */
Johannes Bergf3b85252009-04-23 16:01:47 +0200751 }
752
Johannes Berg6ea0a692014-11-19 11:55:49 +0100753 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200754 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergf3b85252009-04-23 16:01:47 +0200755
Johannes Berga344d672014-06-12 22:24:31 +0200756 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
757 get_random_mask_addr(local->scan_addr,
758 req->mac_addr,
759 req->mac_addr_mask);
760 else
761 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
762
Johannes Berge9da68d2018-10-18 10:35:47 +0200763 if (hw_scan) {
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200764 __set_bit(SCAN_HW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700765 } else if ((req->n_channels == 1) &&
Karl Beldan675a0b02013-03-25 16:26:57 +0100766 (req->channels[0] == local->_oper_chandef.chan)) {
Johannes Berg9b864872012-07-26 14:38:32 +0200767 /*
768 * If we are scanning only on the operating channel
769 * then we do not need to stop normal activities
Ben Greear8a690672012-04-17 10:54:16 -0700770 */
771 unsigned long next_delay;
772
773 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
774
775 ieee80211_recalc_idle(local);
776
777 /* Notify driver scan is starting, keep order of operations
778 * same as normal software scan, in case that matters. */
Johannes Berga344d672014-06-12 22:24:31 +0200779 drv_sw_scan_start(local, sdata, local->scan_addr);
Ben Greear8a690672012-04-17 10:54:16 -0700780
781 ieee80211_configure_filter(local); /* accept probe-responses */
782
783 /* We need to ensure power level is at max for scanning. */
784 ieee80211_hw_config(local, 0);
785
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800786 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
787 IEEE80211_CHAN_RADAR)) ||
Johannes Berg6ea0a692014-11-19 11:55:49 +0100788 !req->n_ssids) {
Ben Greear8a690672012-04-17 10:54:16 -0700789 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
790 } else {
791 ieee80211_scan_state_send_probe(local, &next_delay);
792 next_delay = IEEE80211_CHANNEL_TIME;
793 }
794
795 /* Now, just wait a bit and we are all done! */
796 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
797 next_delay);
798 return 0;
799 } else {
800 /* Do normal software scan */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200801 __set_bit(SCAN_SW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700802 }
John W. Linville6e7e6212010-02-08 16:38:38 -0500803
Johannes Berg5cff20e2009-04-29 12:26:17 +0200804 ieee80211_recalc_idle(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200805
Johannes Berge9da68d2018-10-18 10:35:47 +0200806 if (hw_scan) {
Ilan Peer2ad22742020-05-28 21:34:39 +0200807 WARN_ON(!ieee80211_prep_hw_scan(sdata));
Johannes Berga060bbf2010-04-27 11:59:34 +0200808 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
Johannes Berga344d672014-06-12 22:24:31 +0200809 } else {
810 rc = ieee80211_start_sw_scan(local, sdata);
811 }
Johannes Bergf3b85252009-04-23 16:01:47 +0200812
Johannes Bergf3b85252009-04-23 16:01:47 +0200813 if (rc) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100814 kfree(local->hw_scan_req);
815 local->hw_scan_req = NULL;
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200816 local->scanning = 0;
Johannes Bergf3b85252009-04-23 16:01:47 +0200817
Johannes Berg5cff20e2009-04-29 12:26:17 +0200818 ieee80211_recalc_idle(local);
819
Johannes Bergf3b85252009-04-23 16:01:47 +0200820 local->scan_req = NULL;
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530821 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +0200822 }
823
Johannes Berge9da68d2018-10-18 10:35:47 +0200824 if (hw_scan && rc == 1) {
825 /*
826 * we can't fall back to software for P2P-GO
827 * as it must update NoA etc.
828 */
829 if (ieee80211_vif_type_p2p(&sdata->vif) ==
830 NL80211_IFTYPE_P2P_GO)
831 return -EOPNOTSUPP;
832 hw_scan = false;
833 goto again;
834 }
835
Johannes Bergf3b85252009-04-23 16:01:47 +0200836 return rc;
837}
838
Helmut Schaadf13cce2010-02-24 14:19:21 +0100839static unsigned long
840ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
841{
842 /*
843 * TODO: channel switching also consumes quite some time,
844 * add that delay as well to get a better estimation
845 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800846 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
Helmut Schaadf13cce2010-02-24 14:19:21 +0100847 return IEEE80211_PASSIVE_CHANNEL_TIME;
848 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
849}
850
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200851static void ieee80211_scan_state_decision(struct ieee80211_local *local,
852 unsigned long *next_delay)
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200853{
Helmut Schaa142b9f52009-07-23 13:18:01 +0200854 bool associated = false;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100855 bool tx_empty = true;
856 bool bad_latency;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200857 struct ieee80211_sub_if_data *sdata;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100858 struct ieee80211_channel *next_chan;
Sam Lefflercd2bb512012-10-11 21:03:35 -0700859 enum mac80211_scan_state next_scan_state;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100860 struct cfg80211_scan_request *scan_req;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200861
Helmut Schaadf13cce2010-02-24 14:19:21 +0100862 /*
863 * check if at least one STA interface is associated,
864 * check if at least one STA interface has pending tx frames
865 * and grab the lowest used beacon interval
866 */
Helmut Schaa142b9f52009-07-23 13:18:01 +0200867 mutex_lock(&local->iflist_mtx);
868 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +0100869 if (!ieee80211_sdata_running(sdata))
Helmut Schaa142b9f52009-07-23 13:18:01 +0200870 continue;
871
872 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
873 if (sdata->u.mgd.associated) {
874 associated = true;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100875
Helmut Schaadf13cce2010-02-24 14:19:21 +0100876 if (!qdisc_all_tx_empty(sdata->dev)) {
877 tx_empty = false;
878 break;
879 }
Helmut Schaa142b9f52009-07-23 13:18:01 +0200880 }
881 }
882 }
883 mutex_unlock(&local->iflist_mtx);
884
Johannes Berg6ea0a692014-11-19 11:55:49 +0100885 scan_req = rcu_dereference_protected(local->scan_req,
886 lockdep_is_held(&local->mtx));
887
888 next_chan = scan_req->channels[local->scan_channel_idx];
Ben Greearb23b0252011-02-04 11:54:17 -0800889
Johannes Berg07ef03e2011-11-08 16:21:21 +0100890 /*
891 * we're currently scanning a different channel, let's
892 * see if we can scan another channel without interfering
893 * with the current traffic situation.
894 *
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100895 * Keep good latency, do not stay off-channel more than 125 ms.
Johannes Berg07ef03e2011-11-08 16:21:21 +0100896 */
Helmut Schaadf13cce2010-02-24 14:19:21 +0100897
Johannes Berg07ef03e2011-11-08 16:21:21 +0100898 bad_latency = time_after(jiffies +
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100899 ieee80211_scan_get_channel_time(next_chan),
900 local->leave_oper_channel_time + HZ / 8);
Helmut Schaadf13cce2010-02-24 14:19:21 +0100901
Sam Lefflercd2bb512012-10-11 21:03:35 -0700902 if (associated && !tx_empty) {
Johannes Berg6ea0a692014-11-19 11:55:49 +0100903 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
Sam Lefflercd2bb512012-10-11 21:03:35 -0700904 next_scan_state = SCAN_ABORT;
905 else
906 next_scan_state = SCAN_SUSPEND;
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100907 } else if (associated && bad_latency) {
Sam Lefflercd2bb512012-10-11 21:03:35 -0700908 next_scan_state = SCAN_SUSPEND;
909 } else {
910 next_scan_state = SCAN_SET_CHANNEL;
911 }
912
913 local->next_scan_state = next_scan_state;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200914
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200915 *next_delay = 0;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200916}
917
918static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
919 unsigned long *next_delay)
920{
921 int skip;
922 struct ieee80211_channel *chan;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200923 enum nl80211_bss_scan_width oper_scan_width;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100924 struct cfg80211_scan_request *scan_req;
925
926 scan_req = rcu_dereference_protected(local->scan_req,
927 lockdep_is_held(&local->mtx));
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200928
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200929 skip = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100930 chan = scan_req->channels[local->scan_channel_idx];
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200931
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200932 local->scan_chandef.chan = chan;
933 local->scan_chandef.center_freq1 = chan->center_freq;
Thomas Pedersenb60119602020-04-01 18:18:04 -0700934 local->scan_chandef.freq1_offset = chan->freq_offset;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200935 local->scan_chandef.center_freq2 = 0;
Thomas Pedersen75b15932020-09-21 19:28:03 -0700936
937 /* For scanning on the S1G band, ignore scan_width (which is constant
938 * across all channels) for now since channel width is specific to each
939 * channel. Detect the required channel width here and likely revisit
940 * later. Maybe scan_width could be used to build the channel scan list?
941 */
942 if (chan->band == NL80211_BAND_S1GHZ) {
943 local->scan_chandef.width = ieee80211_s1g_channel_width(chan);
944 goto set_channel;
945 }
946
Johannes Berg6ea0a692014-11-19 11:55:49 +0100947 switch (scan_req->scan_width) {
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200948 case NL80211_BSS_CHAN_WIDTH_5:
949 local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
950 break;
951 case NL80211_BSS_CHAN_WIDTH_10:
952 local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
953 break;
Thomas Pedersendf78a0c2020-06-01 23:22:47 -0700954 default:
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200955 case NL80211_BSS_CHAN_WIDTH_20:
956 /* If scanning on oper channel, use whatever channel-type
957 * is currently in use.
958 */
959 oper_scan_width = cfg80211_chandef_to_scan_width(
960 &local->_oper_chandef);
961 if (chan == local->_oper_chandef.chan &&
Johannes Berg6ea0a692014-11-19 11:55:49 +0100962 oper_scan_width == scan_req->scan_width)
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200963 local->scan_chandef = local->_oper_chandef;
964 else
965 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
966 break;
Thomas Pedersen75b15932020-09-21 19:28:03 -0700967 case NL80211_BSS_CHAN_WIDTH_1:
968 case NL80211_BSS_CHAN_WIDTH_2:
969 /* shouldn't get here, S1G handled above */
970 WARN_ON(1);
971 break;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200972 }
Ben Greearb23b0252011-02-04 11:54:17 -0800973
Thomas Pedersen75b15932020-09-21 19:28:03 -0700974set_channel:
Johannes Berg07ef03e2011-11-08 16:21:21 +0100975 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
976 skip = 1;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200977
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200978 /* advance state machine to next channel/band */
979 local->scan_channel_idx++;
980
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200981 if (skip) {
982 /* if we skip this channel return to the decision state */
983 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200984 return;
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200985 }
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200986
987 /*
988 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
989 * (which unfortunately doesn't say _why_ step a) is done,
990 * but it waits for the probe delay or until a frame is
991 * received - and the received frame would update the NAV).
992 * For now, we do not support waiting until a frame is
993 * received.
994 *
995 * In any case, it is not necessary for a passive scan.
996 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800997 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
998 !scan_req->n_ssids) {
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200999 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Helmut Schaa977923b2009-07-23 12:14:20 +02001000 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001001 return;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +02001002 }
1003
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001004 /* active scan, send probes */
Helmut Schaa7d3be3c2009-07-23 12:13:41 +02001005 *next_delay = IEEE80211_PROBE_DELAY;
Helmut Schaa977923b2009-07-23 12:14:20 +02001006 local->next_scan_state = SCAN_SEND_PROBE;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +02001007}
1008
Johannes Berg07ef03e2011-11-08 16:21:21 +01001009static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
1010 unsigned long *next_delay)
1011{
1012 /* switch back to the operating channel */
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001013 local->scan_chandef.chan = NULL;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001014 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
1015
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +01001016 /* disable PS */
1017 ieee80211_offchannel_return(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +01001018
1019 *next_delay = HZ / 5;
1020 /* afterwards, resume scan & go to next channel */
1021 local->next_scan_state = SCAN_RESUME;
1022}
1023
1024static void ieee80211_scan_state_resume(struct ieee80211_local *local,
1025 unsigned long *next_delay)
1026{
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +01001027 ieee80211_offchannel_stop_vifs(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +01001028
1029 if (local->ops->flush) {
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001030 ieee80211_flush_queues(local, NULL, false);
Johannes Berg07ef03e2011-11-08 16:21:21 +01001031 *next_delay = 0;
1032 } else
1033 *next_delay = HZ / 10;
1034
1035 /* remember when we left the operating channel */
1036 local->leave_oper_channel_time = jiffies;
1037
1038 /* advance to the next channel to be scanned */
Mohammed Shafi Shajakhande2ee842011-12-24 18:43:28 +05301039 local->next_scan_state = SCAN_SET_CHANNEL;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001040}
1041
Johannes Bergc2b13452008-09-11 00:01:55 +02001042void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +02001043{
1044 struct ieee80211_local *local =
1045 container_of(work, struct ieee80211_local, scan_work.work);
Johannes Bergd07bfd82011-03-07 15:48:41 +01001046 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +01001047 struct cfg80211_scan_request *scan_req;
Johannes Berg0a51b272008-09-08 17:44:25 +02001048 unsigned long next_delay = 0;
Eliad Peller8bd2a242013-12-05 11:21:27 +02001049 bool aborted;
Johannes Berg8789d452010-08-26 13:30:26 +02001050
Johannes Berga1699b72010-07-30 16:46:07 +02001051 mutex_lock(&local->mtx);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001052
Luciano Coelho332ff7f2015-01-22 23:34:10 +02001053 if (!ieee80211_can_run_worker(local)) {
1054 aborted = true;
1055 goto out_complete;
1056 }
1057
Johannes Berge2fd5db2012-07-06 21:39:28 +02001058 sdata = rcu_dereference_protected(local->scan_sdata,
1059 lockdep_is_held(&local->mtx));
Johannes Berg6ea0a692014-11-19 11:55:49 +01001060 scan_req = rcu_dereference_protected(local->scan_req,
1061 lockdep_is_held(&local->mtx));
Johannes Bergd07bfd82011-03-07 15:48:41 +01001062
Ben Greear8a690672012-04-17 10:54:16 -07001063 /* When scanning on-channel, the first-callback means completed. */
1064 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
1065 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
1066 goto out_complete;
1067 }
1068
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001069 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
1070 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
1071 goto out_complete;
Johannes Bergf3b85252009-04-23 16:01:47 +02001072 }
1073
Johannes Berg6ea0a692014-11-19 11:55:49 +01001074 if (!sdata || !scan_req)
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001075 goto out;
1076
Eliad Pellerff5db432014-11-12 10:08:29 +02001077 if (!local->scanning) {
Johannes Bergf3b85252009-04-23 16:01:47 +02001078 int rc;
1079
Johannes Berg6ea0a692014-11-19 11:55:49 +01001080 RCU_INIT_POINTER(local->scan_req, NULL);
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301081 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +02001082
Johannes Berg6ea0a692014-11-19 11:55:49 +01001083 rc = __ieee80211_start_scan(sdata, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001084 if (rc) {
Stanislaw Gruszka3aed49e2010-10-06 11:22:12 +02001085 /* need to complete scan in cfg80211 */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001086 rcu_assign_pointer(local->scan_req, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001087 aborted = true;
1088 goto out_complete;
1089 } else
1090 goto out;
Johannes Bergf3b85252009-04-23 16:01:47 +02001091 }
1092
Johannes Berg5bc75722008-09-11 00:01:51 +02001093 /*
Helmut Schaaf502d092009-07-23 12:13:48 +02001094 * as long as no delay is required advance immediately
1095 * without scheduling a new work
1096 */
1097 do {
Rajkumar Manoharanc29acf22011-05-14 09:43:28 +05301098 if (!ieee80211_sdata_running(sdata)) {
1099 aborted = true;
1100 goto out_complete;
1101 }
1102
Helmut Schaa977923b2009-07-23 12:14:20 +02001103 switch (local->next_scan_state) {
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001104 case SCAN_DECISION:
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001105 /* if no more bands/channels left, complete scan */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001106 if (local->scan_channel_idx >= scan_req->n_channels) {
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001107 aborted = false;
1108 goto out_complete;
1109 }
1110 ieee80211_scan_state_decision(local, &next_delay);
Helmut Schaaf502d092009-07-23 12:13:48 +02001111 break;
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001112 case SCAN_SET_CHANNEL:
1113 ieee80211_scan_state_set_channel(local, &next_delay);
1114 break;
Helmut Schaaf502d092009-07-23 12:13:48 +02001115 case SCAN_SEND_PROBE:
1116 ieee80211_scan_state_send_probe(local, &next_delay);
1117 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001118 case SCAN_SUSPEND:
1119 ieee80211_scan_state_suspend(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001120 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001121 case SCAN_RESUME:
1122 ieee80211_scan_state_resume(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001123 break;
Sam Lefflercd2bb512012-10-11 21:03:35 -07001124 case SCAN_ABORT:
1125 aborted = true;
1126 goto out_complete;
Helmut Schaaf502d092009-07-23 12:13:48 +02001127 }
1128 } while (next_delay == 0);
Johannes Berg0a51b272008-09-08 17:44:25 +02001129
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001130 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
Johannes Bergd07bfd82011-03-07 15:48:41 +01001131 goto out;
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001132
1133out_complete:
Eliad Peller8bd2a242013-12-05 11:21:27 +02001134 __ieee80211_scan_completed(&local->hw, aborted);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001135out:
1136 mutex_unlock(&local->mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +02001137}
1138
Johannes Bergc2b13452008-09-11 00:01:55 +02001139int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +01001140 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +02001141{
Johannes Bergf3b85252009-04-23 16:01:47 +02001142 int res;
1143
Johannes Berga1699b72010-07-30 16:46:07 +02001144 mutex_lock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001145 res = __ieee80211_start_scan(sdata, req);
Johannes Berga1699b72010-07-30 16:46:07 +02001146 mutex_unlock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001147
1148 return res;
1149}
1150
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001151int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1152 const u8 *ssid, u8 ssid_len,
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001153 struct ieee80211_channel **channels,
1154 unsigned int n_channels,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001155 enum nl80211_bss_scan_width scan_width)
Johannes Bergf3b85252009-04-23 16:01:47 +02001156{
Johannes Berg0a51b272008-09-08 17:44:25 +02001157 struct ieee80211_local *local = sdata->local;
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001158 int ret = -EBUSY, i, n_ch = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001159 enum nl80211_band band;
Johannes Berg0a51b272008-09-08 17:44:25 +02001160
Johannes Berga1699b72010-07-30 16:46:07 +02001161 mutex_lock(&local->mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01001162
Johannes Bergf3b85252009-04-23 16:01:47 +02001163 /* busy scanning */
1164 if (local->scan_req)
1165 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01001166
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001167 /* fill internal scan request */
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001168 if (!channels) {
1169 int max_n;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001170
Johannes Berg57fbcce2016-04-12 15:56:15 +02001171 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Tova Mussaic8cb5b82020-09-18 11:33:13 +02001172 if (!local->hw.wiphy->bands[band] ||
1173 band == NL80211_BAND_6GHZ)
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001174 continue;
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001175
1176 max_n = local->hw.wiphy->bands[band]->n_channels;
1177 for (i = 0; i < max_n; i++) {
1178 struct ieee80211_channel *tmp_ch =
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001179 &local->hw.wiphy->bands[band]->channels[i];
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001180
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001181 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001182 IEEE80211_CHAN_DISABLED))
1183 continue;
1184
1185 local->int_scan_req->channels[n_ch] = tmp_ch;
1186 n_ch++;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001187 }
1188 }
1189
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001190 if (WARN_ON_ONCE(n_ch == 0))
1191 goto unlock;
1192
1193 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001194 } else {
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001195 for (i = 0; i < n_channels; i++) {
1196 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
1197 IEEE80211_CHAN_DISABLED))
1198 continue;
1199
1200 local->int_scan_req->channels[n_ch] = channels[i];
1201 n_ch++;
1202 }
1203
1204 if (WARN_ON_ONCE(n_ch == 0))
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001205 goto unlock;
1206
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001207 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001208 }
1209
1210 local->int_scan_req->ssids = &local->scan_ssid;
1211 local->int_scan_req->n_ssids = 1;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001212 local->int_scan_req->scan_width = scan_width;
Johannes Berg5ba63532009-08-07 17:54:07 +02001213 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
1214 local->int_scan_req->ssids[0].ssid_len = ssid_len;
Johannes Berg2a519312009-02-10 21:25:55 +01001215
Johannes Berg5ba63532009-08-07 17:54:07 +02001216 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
Johannes Bergf3b85252009-04-23 16:01:47 +02001217 unlock:
Johannes Berga1699b72010-07-30 16:46:07 +02001218 mutex_unlock(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001219 return ret;
Johannes Berg0a51b272008-09-08 17:44:25 +02001220}
Johannes Berg5bb644a2009-05-17 11:40:42 +02001221
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001222/*
1223 * Only call this function when a scan can't be queued -- under RTNL.
1224 */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001225void ieee80211_scan_cancel(struct ieee80211_local *local)
1226{
Johannes Berg5bb644a2009-05-17 11:40:42 +02001227 /*
Eliad Pellerb8564392011-06-13 12:47:30 +03001228 * We are canceling software scan, or deferred scan that was not
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001229 * yet really started (see __ieee80211_start_scan ).
1230 *
1231 * Regarding hardware scan:
1232 * - we can not call __ieee80211_scan_completed() as when
1233 * SCAN_HW_SCANNING bit is set this function change
1234 * local->hw_scan_req to operate on 5G band, what race with
1235 * driver which can use local->hw_scan_req
1236 *
1237 * - we can not cancel scan_work since driver can schedule it
1238 * by ieee80211_scan_completed(..., true) to finish scan
1239 *
Eliad Pellerb8564392011-06-13 12:47:30 +03001240 * Hence we only call the cancel_hw_scan() callback, but the low-level
1241 * driver is still responsible for calling ieee80211_scan_completed()
1242 * after the scan was completed/aborted.
Johannes Berg5bb644a2009-05-17 11:40:42 +02001243 */
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001244
Johannes Berga1699b72010-07-30 16:46:07 +02001245 mutex_lock(&local->mtx);
Eliad Pellerb8564392011-06-13 12:47:30 +03001246 if (!local->scan_req)
1247 goto out;
1248
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001249 /*
1250 * We have a scan running and the driver already reported completion,
1251 * but the worker hasn't run yet or is stuck on the mutex - mark it as
1252 * cancelled.
1253 */
1254 if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
1255 test_bit(SCAN_COMPLETED, &local->scanning)) {
1256 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1257 goto out;
1258 }
1259
Eliad Pellerb8564392011-06-13 12:47:30 +03001260 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001261 /*
1262 * Make sure that __ieee80211_scan_completed doesn't trigger a
1263 * scan on another band.
1264 */
1265 set_bit(SCAN_HW_CANCELLED, &local->scanning);
Eliad Pellerb8564392011-06-13 12:47:30 +03001266 if (local->ops->cancel_hw_scan)
Johannes Berge2fd5db2012-07-06 21:39:28 +02001267 drv_cancel_hw_scan(local,
1268 rcu_dereference_protected(local->scan_sdata,
1269 lockdep_is_held(&local->mtx)));
Eliad Pellerb8564392011-06-13 12:47:30 +03001270 goto out;
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001271 }
Eliad Pellerb8564392011-06-13 12:47:30 +03001272
1273 /*
1274 * If the work is currently running, it must be blocked on
1275 * the mutex, but we'll set scan_sdata = NULL and it'll
1276 * simply exit once it acquires the mutex.
1277 */
1278 cancel_delayed_work(&local->scan_work);
1279 /* and clean up */
Avraham Stern7947d3e2016-07-05 15:23:12 +03001280 memset(&local->scan_info, 0, sizeof(local->scan_info));
Eliad Peller8bd2a242013-12-05 11:21:27 +02001281 __ieee80211_scan_completed(&local->hw, true);
Eliad Pellerb8564392011-06-13 12:47:30 +03001282out:
Johannes Bergd07bfd82011-03-07 15:48:41 +01001283 mutex_unlock(&local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001284}
Luciano Coelho79f460c2011-05-11 17:09:36 +03001285
David Spinadeld43c6b62013-12-08 21:48:57 +02001286int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1287 struct cfg80211_sched_scan_request *req)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001288{
1289 struct ieee80211_local *local = sdata->local;
David Spinadel633e2712014-02-06 16:15:23 +02001290 struct ieee80211_scan_ies sched_scan_ies = {};
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001291 struct cfg80211_chan_def chandef;
David Spinadel633e2712014-02-06 16:15:23 +02001292 int ret, i, iebufsz, num_bands = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001293 u32 rate_masks[NUM_NL80211_BANDS] = {};
David Spinadel633e2712014-02-06 16:15:23 +02001294 u8 bands_used = 0;
1295 u8 *ie;
Johannes Bergb9771d42018-05-28 15:47:41 +02001296 u32 flags = 0;
Johannes Bergc604b9f2012-11-29 12:45:18 +01001297
David Spinadele4dcbb32014-02-11 13:45:41 +02001298 iebufsz = local->scan_ies_len + req->ie_len;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001299
David Spinadeld43c6b62013-12-08 21:48:57 +02001300 lockdep_assert_held(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001301
David Spinadeld43c6b62013-12-08 21:48:57 +02001302 if (!local->ops->sched_scan_start)
1303 return -ENOTSUPP;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001304
Johannes Berg57fbcce2016-04-12 15:56:15 +02001305 for (i = 0; i < NUM_NL80211_BANDS; i++) {
David Spinadel633e2712014-02-06 16:15:23 +02001306 if (local->hw.wiphy->bands[i]) {
1307 bands_used |= BIT(i);
1308 rate_masks[i] = (u32) -1;
1309 num_bands++;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001310 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001311 }
1312
Johannes Bergb9771d42018-05-28 15:47:41 +02001313 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
1314 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
1315
Kees Cook6396bb22018-06-12 14:03:40 -07001316 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
David Spinadel633e2712014-02-06 16:15:23 +02001317 if (!ie) {
1318 ret = -ENOMEM;
1319 goto out;
1320 }
1321
1322 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
1323
Ilan Peer2ad22742020-05-28 21:34:39 +02001324 ieee80211_build_preq_ies(sdata, ie, num_bands * iebufsz,
Kirtika Ruchandanicd5861b2016-11-23 20:45:49 -08001325 &sched_scan_ies, req->ie,
Johannes Berg00387f32018-05-28 15:47:38 +02001326 req->ie_len, bands_used, rate_masks, &chandef,
Johannes Bergb9771d42018-05-28 15:47:41 +02001327 flags);
David Spinadel633e2712014-02-06 16:15:23 +02001328
Johannes Berg30dd3ed2012-09-04 19:15:01 +02001329 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
David Spinadeld43c6b62013-12-08 21:48:57 +02001330 if (ret == 0) {
Johannes Berg5260a5b2012-07-06 21:55:11 +02001331 rcu_assign_pointer(local->sched_scan_sdata, sdata);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001332 rcu_assign_pointer(local->sched_scan_req, req);
David Spinadeld43c6b62013-12-08 21:48:57 +02001333 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001334
David Spinadel633e2712014-02-06 16:15:23 +02001335 kfree(ie);
David Spinadeld43c6b62013-12-08 21:48:57 +02001336
David Spinadel633e2712014-02-06 16:15:23 +02001337out:
David Spinadeld43c6b62013-12-08 21:48:57 +02001338 if (ret) {
1339 /* Clean in case of failure after HW restart or upon resume. */
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301340 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001341 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001342 }
1343
1344 return ret;
1345}
1346
1347int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1348 struct cfg80211_sched_scan_request *req)
1349{
1350 struct ieee80211_local *local = sdata->local;
1351 int ret;
1352
1353 mutex_lock(&local->mtx);
1354
1355 if (rcu_access_pointer(local->sched_scan_sdata)) {
1356 mutex_unlock(&local->mtx);
1357 return -EBUSY;
1358 }
1359
1360 ret = __ieee80211_request_sched_scan_start(sdata, req);
1361
Johannes Berg5260a5b2012-07-06 21:55:11 +02001362 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001363 return ret;
1364}
1365
Eliad Peller0d440ea2015-10-25 10:59:33 +02001366int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001367{
Eliad Peller0d440ea2015-10-25 10:59:33 +02001368 struct ieee80211_sub_if_data *sched_scan_sdata;
1369 int ret = -ENOENT;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001370
Johannes Berg5260a5b2012-07-06 21:55:11 +02001371 mutex_lock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001372
1373 if (!local->ops->sched_scan_stop) {
1374 ret = -ENOTSUPP;
1375 goto out;
1376 }
1377
David Spinadeld43c6b62013-12-08 21:48:57 +02001378 /* We don't want to restart sched scan anymore. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001379 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001380
Eliad Peller0d440ea2015-10-25 10:59:33 +02001381 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1382 lockdep_is_held(&local->mtx));
1383 if (sched_scan_sdata) {
1384 ret = drv_sched_scan_stop(local, sched_scan_sdata);
Alexander Bondar71228a12014-03-16 10:49:54 +02001385 if (!ret)
Andreea-Cristina Bernatad053a92014-08-22 16:14:49 +03001386 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Alexander Bondar71228a12014-03-16 10:49:54 +02001387 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001388out:
Johannes Berg5260a5b2012-07-06 21:55:11 +02001389 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001390
1391 return ret;
1392}
1393
1394void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1395{
1396 struct ieee80211_local *local = hw_to_local(hw);
1397
1398 trace_api_sched_scan_results(local);
1399
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001400 cfg80211_sched_scan_results(hw->wiphy, 0);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001401}
1402EXPORT_SYMBOL(ieee80211_sched_scan_results);
1403
Johannes Bergf6837ba82014-04-30 14:19:04 +02001404void ieee80211_sched_scan_end(struct ieee80211_local *local)
Luciano Coelho85a99942011-05-12 16:28:29 +03001405{
Luciano Coelho85a99942011-05-12 16:28:29 +03001406 mutex_lock(&local->mtx);
1407
Johannes Berg5260a5b2012-07-06 21:55:11 +02001408 if (!rcu_access_pointer(local->sched_scan_sdata)) {
Luciano Coelho85a99942011-05-12 16:28:29 +03001409 mutex_unlock(&local->mtx);
1410 return;
1411 }
1412
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301413 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Luciano Coelho85a99942011-05-12 16:28:29 +03001414
David Spinadeld43c6b62013-12-08 21:48:57 +02001415 /* If sched scan was aborted by the driver. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001416 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001417
Luciano Coelho85a99942011-05-12 16:28:29 +03001418 mutex_unlock(&local->mtx);
1419
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001420 cfg80211_sched_scan_stopped(local->hw.wiphy, 0);
Luciano Coelho85a99942011-05-12 16:28:29 +03001421}
1422
Johannes Bergf6837ba82014-04-30 14:19:04 +02001423void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1424{
1425 struct ieee80211_local *local =
1426 container_of(work, struct ieee80211_local,
1427 sched_scan_stopped_work);
1428
1429 ieee80211_sched_scan_end(local);
1430}
1431
Luciano Coelho79f460c2011-05-11 17:09:36 +03001432void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1433{
1434 struct ieee80211_local *local = hw_to_local(hw);
1435
1436 trace_api_sched_scan_stopped(local);
1437
Eliad Peller2bc533b2016-01-05 16:28:06 +02001438 /*
1439 * this shouldn't really happen, so for simplicity
1440 * simply ignore it, and let mac80211 reconfigure
1441 * the sched scan later on.
1442 */
1443 if (local->in_reconfig)
1444 return;
1445
Johannes Berg18db5942013-11-06 10:34:36 +01001446 schedule_work(&local->sched_scan_stopped_work);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001447}
1448EXPORT_SYMBOL(ieee80211_sched_scan_stopped);