]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/mac80211/scan.c
Merge tag 'spi-fix-v5.9-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
[mirror_ubuntu-jammy-kernel.git] / net / mac80211 / scan.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
0a51b27e 2/*
5484e237
JB
3 * Scanning implementation
4 *
0a51b27e
JB
5 * 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>
74d803b6 10 * Copyright 2013-2015 Intel Mobile Communications GmbH
40b0bd24 11 * Copyright 2016-2017 Intel Deutschland GmbH
4abb52a4 12 * Copyright (C) 2018-2019 Intel Corporation
0a51b27e
JB
13 */
14
0a51b27e 15#include <linux/if_arp.h>
888d04df 16#include <linux/etherdevice.h>
078e1e60 17#include <linux/rtnetlink.h>
df13cce5 18#include <net/sch_generic.h>
5a0e3ad6 19#include <linux/slab.h>
bc3b2d7f 20#include <linux/export.h>
b9771d41 21#include <linux/random.h>
0a51b27e 22#include <net/mac80211.h>
0a51b27e
JB
23
24#include "ieee80211_i.h"
24487981 25#include "driver-ops.h"
5484e237 26#include "mesh.h"
0a51b27e
JB
27
28#define IEEE80211_PROBE_DELAY (HZ / 33)
29#define IEEE80211_CHANNEL_TIME (HZ / 33)
3f892b61 30#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
0a51b27e 31
5484e237 32void ieee80211_rx_bss_put(struct ieee80211_local *local,
c2b13452 33 struct ieee80211_bss *bss)
5484e237 34{
0c1ad2ca
JB
35 if (!bss)
36 return;
5b112d3d
JB
37 cfg80211_put_bss(local->hw.wiphy,
38 container_of((void *)bss, struct cfg80211_bss, priv));
5484e237
JB
39}
40
ab13315a
KV
41static 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
fcea7db5
SS
58static 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 }
2a333a0d
JB
135
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;
fcea7db5
SS
141}
142
c2b13452 143struct ieee80211_bss *
5484e237
JB
144ieee80211_bss_info_update(struct ieee80211_local *local,
145 struct ieee80211_rx_status *rx_status,
d45c4172 146 struct ieee80211_mgmt *mgmt, size_t len,
d45c4172 147 struct ieee80211_channel *channel)
5484e237 148{
d45c4172 149 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
78ac51f8
SS
150 struct cfg80211_bss *cbss, *non_tx_cbss;
151 struct ieee80211_bss *bss, *non_tx_bss;
162dd6a7
JB
152 struct cfg80211_inform_bss bss_meta = {
153 .boottime_ns = rx_status->boottime_ns,
154 };
74d803b6 155 bool signal_valid;
7947d3e0 156 struct ieee80211_sub_if_data *scan_sdata;
4abb52a4
SS
157 struct ieee802_11_elems elems;
158 size_t baselen;
159 u8 *elements;
2a519311 160
1ad22fb5
T
161 if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)
162 bss_meta.signal = 0; /* invalid signal indication */
163 else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
61f6bba0 164 bss_meta.signal = rx_status->signal * 100;
30686bf7 165 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))
61f6bba0 166 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal;
2a519311 167
61f6bba0 168 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20;
da6a4352 169 if (rx_status->bw == RATE_INFO_BW_5)
61f6bba0 170 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5;
da6a4352 171 else if (rx_status->bw == RATE_INFO_BW_10)
61f6bba0 172 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10;
7ca15a0a 173
61f6bba0 174 bss_meta.chan = channel;
7947d3e0
AS
175
176 rcu_read_lock();
177 scan_sdata = rcu_dereference(local->scan_sdata);
178 if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&
179 scan_sdata->vif.bss_conf.assoc &&
180 ieee80211_have_rx_timestamp(rx_status)) {
181 bss_meta.parent_tsf =
182 ieee80211_calculate_rx_timestamp(local, rx_status,
183 len + FCS_LEN, 24);
184 ether_addr_copy(bss_meta.parent_bssid,
185 scan_sdata->vif.bss_conf.bssid);
186 }
187 rcu_read_unlock();
188
61f6bba0
JB
189 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,
190 mgmt, len, GFP_ATOMIC);
0c1ad2ca 191 if (!cbss)
00d3f14c 192 return NULL;
4abb52a4
SS
193
194 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
195 elements = mgmt->u.probe_resp.variable;
196 baselen = offsetof(struct ieee80211_mgmt,
197 u.probe_resp.variable);
198 } else {
199 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
200 elements = mgmt->u.beacon.variable;
201 }
202
203 if (baselen > len)
204 return NULL;
205
206 ieee802_11_parse_elems(elements, len - baselen, false, &elems,
207 mgmt->bssid, cbss->bssid);
208
74d803b6 209 /* In case the signal is invalid update the status */
7bb106eb 210 signal_valid = channel == cbss->channel;
74d803b6
SS
211 if (!signal_valid)
212 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
00d3f14c 213
0c1ad2ca 214 bss = (void *)cbss->priv;
fcea7db5 215 ieee80211_update_bss_from_elems(local, bss, &elems, rx_status, beacon);
817cee76 216
78ac51f8
SS
217 list_for_each_entry(non_tx_cbss, &cbss->nontrans_list, nontrans_list) {
218 non_tx_bss = (void *)non_tx_cbss->priv;
219
220 ieee80211_update_bss_from_elems(local, non_tx_bss, &elems,
221 rx_status, beacon);
222 }
223
5484e237
JB
224 return bss;
225}
0a51b27e 226
40b0bd24
RZ
227static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
228 u32 scan_flags, const u8 *da)
229{
230 if (!sdata)
231 return false;
232 /* accept broadcast for OCE */
233 if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP &&
234 is_broadcast_ether_addr(da))
235 return true;
236 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
237 return true;
238 return ether_addr_equal(da, sdata->vif.addr);
239}
240
d48b2968 241void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
98c8fccf 242{
f1d58c25 243 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
d48b2968
JB
244 struct ieee80211_sub_if_data *sdata1, *sdata2;
245 struct ieee80211_mgmt *mgmt = (void *)skb->data;
c2b13452 246 struct ieee80211_bss *bss;
98c8fccf 247 struct ieee80211_channel *channel;
98c8fccf 248
d48b2968
JB
249 if (skb->len < 24 ||
250 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
251 !ieee80211_is_beacon(mgmt->frame_control)))
252 return;
98c8fccf 253
d48b2968
JB
254 sdata1 = rcu_dereference(local->scan_sdata);
255 sdata2 = rcu_dereference(local->sched_scan_sdata);
98c8fccf 256
d48b2968
JB
257 if (likely(!sdata1 && !sdata2))
258 return;
98c8fccf 259
d48b2968 260 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
a344d677
JB
261 struct cfg80211_scan_request *scan_req;
262 struct cfg80211_sched_scan_request *sched_scan_req;
40b0bd24 263 u32 scan_req_flags = 0, sched_scan_req_flags = 0;
a344d677
JB
264
265 scan_req = rcu_dereference(local->scan_req);
266 sched_scan_req = rcu_dereference(local->sched_scan_req);
267
40b0bd24
RZ
268 if (scan_req)
269 scan_req_flags = scan_req->flags;
270
271 if (sched_scan_req)
272 sched_scan_req_flags = sched_scan_req->flags;
273
274 /* ignore ProbeResp to foreign address or non-bcast (OCE)
275 * unless scanning with randomised address
a344d677 276 */
40b0bd24
RZ
277 if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags,
278 mgmt->da) &&
279 !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags,
280 mgmt->da))
d48b2968 281 return;
98c8fccf
JB
282 }
283
3b23c184
TP
284 channel = ieee80211_get_channel_khz(local->hw.wiphy,
285 ieee80211_rx_status_to_khz(rx_status));
98c8fccf
JB
286
287 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
d48b2968 288 return;
98c8fccf 289
d48b2968 290 bss = ieee80211_bss_info_update(local, rx_status,
4abb52a4 291 mgmt, skb->len,
d45c4172 292 channel);
d048e503 293 if (bss)
d48b2968 294 ieee80211_rx_bss_put(local, bss);
98c8fccf
JB
295}
296
2103dec1
SW
297static void
298ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
299 enum nl80211_bss_scan_width scan_width)
300{
301 memset(chandef, 0, sizeof(*chandef));
302 switch (scan_width) {
303 case NL80211_BSS_CHAN_WIDTH_5:
304 chandef->width = NL80211_CHAN_WIDTH_5;
305 break;
306 case NL80211_BSS_CHAN_WIDTH_10:
307 chandef->width = NL80211_CHAN_WIDTH_10;
308 break;
309 default:
310 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
311 break;
312 }
313}
314
4d36ec58 315/* return false if no more work */
2ad2274c 316static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata)
4d36ec58 317{
2ad2274c 318 struct ieee80211_local *local = sdata->local;
6ea0a69c 319 struct cfg80211_scan_request *req;
2103dec1 320 struct cfg80211_chan_def chandef;
c56ef672 321 u8 bands_used = 0;
4d36ec58 322 int i, ielen, n_chans;
b9771d41 323 u32 flags = 0;
4d36ec58 324
6ea0a69c
JB
325 req = rcu_dereference_protected(local->scan_req,
326 lockdep_is_held(&local->mtx));
327
a754055a
EG
328 if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
329 return false;
330
30686bf7 331 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4d36ec58 332 for (i = 0; i < req->n_channels; i++) {
c56ef672
DS
333 local->hw_scan_req->req.channels[i] = req->channels[i];
334 bands_used |= BIT(req->channels[i]->band);
335 }
336
337 n_chans = req->n_channels;
338 } else {
339 do {
57fbcce3 340 if (local->hw_scan_band == NUM_NL80211_BANDS)
c56ef672
DS
341 return false;
342
343 n_chans = 0;
344
345 for (i = 0; i < req->n_channels; i++) {
346 if (req->channels[i]->band !=
347 local->hw_scan_band)
348 continue;
349 local->hw_scan_req->req.channels[n_chans] =
4d36ec58
JB
350 req->channels[i];
351 n_chans++;
c56ef672 352 bands_used |= BIT(req->channels[i]->band);
4d36ec58 353 }
4d36ec58 354
c56ef672
DS
355 local->hw_scan_band++;
356 } while (!n_chans);
357 }
4d36ec58 358
c56ef672 359 local->hw_scan_req->req.n_channels = n_chans;
2103dec1 360 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
4d36ec58 361
b9771d41
JB
362 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
363 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
364
2ad2274c 365 ielen = ieee80211_build_preq_ies(sdata,
c56ef672 366 (u8 *)local->hw_scan_req->req.ie,
c604b9f2 367 local->hw_scan_ies_bufsize,
c56ef672
DS
368 &local->hw_scan_req->ies,
369 req->ie, req->ie_len,
b9771d41
JB
370 bands_used, req->rates, &chandef,
371 flags);
c56ef672
DS
372 local->hw_scan_req->req.ie_len = ielen;
373 local->hw_scan_req->req.no_cck = req->no_cck;
a344d677
JB
374 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
375 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
376 req->mac_addr_mask);
e345f44f 377 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
4d36ec58
JB
378
379 return true;
380}
381
8bd2a248 382static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
0a51b27e
JB
383{
384 struct ieee80211_local *local = hw_to_local(hw);
e9da68dd 385 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
a2b70e83 386 bool was_scanning = local->scanning;
6ea0a69c 387 struct cfg80211_scan_request *scan_req;
a344d677 388 struct ieee80211_sub_if_data *scan_sdata;
4fa11ec7 389 struct ieee80211_sub_if_data *sdata;
0a51b27e 390
e229f844 391 lockdep_assert_held(&local->mtx);
5bc75728 392
6d3560d4
JB
393 /*
394 * It's ok to abort a not-yet-running scan (that
395 * we have one at all will be verified by checking
396 * local->scan_req next), but not to complete it
397 * successfully.
398 */
399 if (WARN_ON(!local->scanning && !aborted))
400 aborted = true;
5bc75728 401
e229f844 402 if (WARN_ON(!local->scan_req))
d07bfd8b 403 return;
de95a54b 404
2ad2274c
IP
405 scan_sdata = rcu_dereference_protected(local->scan_sdata,
406 lockdep_is_held(&local->mtx));
407
c56ef672 408 if (hw_scan && !aborted &&
30686bf7 409 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
2ad2274c 410 ieee80211_prep_hw_scan(scan_sdata)) {
e2fd5dbc
JB
411 int rc;
412
413 rc = drv_hw_scan(local,
414 rcu_dereference_protected(local->scan_sdata,
415 lockdep_is_held(&local->mtx)),
416 local->hw_scan_req);
417
6eb11a9a 418 if (rc == 0)
d07bfd8b 419 return;
7947d3e0 420
7d10f6b1
JB
421 /* HW scan failed and is going to be reported as aborted,
422 * so clear old scan info.
7947d3e0
AS
423 */
424 memset(&local->scan_info, 0, sizeof(local->scan_info));
7d10f6b1 425 aborted = true;
4d36ec58
JB
426 }
427
428 kfree(local->hw_scan_req);
429 local->hw_scan_req = NULL;
f3b85252 430
6ea0a69c
JB
431 scan_req = rcu_dereference_protected(local->scan_req,
432 lockdep_is_held(&local->mtx));
433
1d76250b 434 if (scan_req != local->int_scan_req) {
7947d3e0
AS
435 local->scan_info.aborted = aborted;
436 cfg80211_scan_done(scan_req, &local->scan_info);
1d76250b 437 }
6ea0a69c 438 RCU_INIT_POINTER(local->scan_req, NULL);
0c2bef46 439 RCU_INIT_POINTER(local->scan_sdata, NULL);
2a519311 440
fbe9c429 441 local->scanning = 0;
7ca15a0a 442 local->scan_chandef.chan = NULL;
f3b85252 443
07ef03ee
JB
444 /* Set power back to normal operating levels. */
445 ieee80211_hw_config(local, 0);
a0daa0e7 446
8bd2a248 447 if (!hw_scan) {
e229f844 448 ieee80211_configure_filter(local);
a344d677 449 drv_sw_scan_complete(local, scan_sdata);
aacde9ee 450 ieee80211_offchannel_return(local);
e229f844 451 }
0a51b27e 452
5cff20e6 453 ieee80211_recalc_idle(local);
e229f844 454
0a51b27e 455 ieee80211_mlme_notify_scan_completed(local);
46900298 456 ieee80211_ibss_notify_scan_completed(local);
4fa11ec7
SK
457
458 /* Requeue all the work that might have been ignored while
459 * the scan was in progress; if there was none this will
460 * just be a no-op for the particular interface.
461 */
462 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
463 if (ieee80211_sdata_running(sdata))
464 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
465 }
466
a2b70e83
EP
467 if (was_scanning)
468 ieee80211_start_next_roc(local);
0a51b27e 469}
8789d459 470
7947d3e0
AS
471void ieee80211_scan_completed(struct ieee80211_hw *hw,
472 struct cfg80211_scan_info *info)
8789d459
JB
473{
474 struct ieee80211_local *local = hw_to_local(hw);
475
58bd7f11 476 trace_api_scan_completed(local, info->aborted);
8789d459
JB
477
478 set_bit(SCAN_COMPLETED, &local->scanning);
7947d3e0 479 if (info->aborted)
8789d459 480 set_bit(SCAN_ABORTED, &local->scanning);
7947d3e0
AS
481
482 memcpy(&local->scan_info, info, sizeof(*info));
483
8789d459
JB
484 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
485}
0a51b27e
JB
486EXPORT_SYMBOL(ieee80211_scan_completed);
487
a344d677
JB
488static int ieee80211_start_sw_scan(struct ieee80211_local *local,
489 struct ieee80211_sub_if_data *sdata)
f3b85252 490{
fe57d9f5
JB
491 /* Software scan is not supported in multi-channel cases */
492 if (local->use_chanctx)
493 return -EOPNOTSUPP;
494
f3b85252
JB
495 /*
496 * Hardware/driver doesn't support hw_scan, so use software
497 * scanning instead. First send a nullfunc frame with power save
498 * bit on so that AP will buffer the frames for us while we are not
499 * listening, then send probe requests to each channel and wait for
500 * the responses. After all channels are scanned, tune back to the
501 * original channel and send a nullfunc frame with power save bit
502 * off to trigger the AP to send us all the buffered frames.
503 *
504 * Note that while local->sw_scanning is true everything else but
505 * nullfunc frames and probe requests will be dropped in
506 * ieee80211_tx_h_check_assoc().
507 */
a344d677 508 drv_sw_scan_start(local, sdata, local->scan_addr);
f3b85252 509
de312db3 510 local->leave_oper_channel_time = jiffies;
977923b0 511 local->next_scan_state = SCAN_DECISION;
f3b85252
JB
512 local->scan_channel_idx = 0;
513
aacde9ee 514 ieee80211_offchannel_stop_vifs(local);
a80f7c0b 515
9c35d7d2 516 /* ensure nullfunc is transmitted before leaving operating channel */
3b24f4c6 517 ieee80211_flush_queues(local, NULL, false);
9c35d7d2 518
3ac64bee 519 ieee80211_configure_filter(local);
f3b85252 520
59bdf3b0
BG
521 /* We need to set power level at maximum rate for scanning. */
522 ieee80211_hw_config(local, 0);
523
42935eca 524 ieee80211_queue_delayed_work(&local->hw,
07ef03ee 525 &local->scan_work, 0);
f3b85252
JB
526
527 return 0;
528}
529
dc0c18ed
AK
530static bool __ieee80211_can_leave_ch(struct ieee80211_sub_if_data *sdata)
531{
532 struct ieee80211_local *local = sdata->local;
533 struct ieee80211_sub_if_data *sdata_iter;
534
535 if (!ieee80211_is_radar_required(local))
536 return true;
537
538 if (!regulatory_pre_cac_allowed(local->hw.wiphy))
539 return false;
540
541 mutex_lock(&local->iflist_mtx);
542 list_for_each_entry(sdata_iter, &local->interfaces, list) {
543 if (sdata_iter->wdev.cac_started) {
544 mutex_unlock(&local->iflist_mtx);
545 return false;
546 }
547 }
548 mutex_unlock(&local->iflist_mtx);
549
550 return true;
551}
552
133d40f9
SG
553static bool ieee80211_can_scan(struct ieee80211_local *local,
554 struct ieee80211_sub_if_data *sdata)
555{
dc0c18ed 556 if (!__ieee80211_can_leave_ch(sdata))
164eb02d
SW
557 return false;
558
2eb278e0 559 if (!list_empty(&local->roc_list))
133d40f9
SG
560 return false;
561
562 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
392b9ffb 563 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
133d40f9
SG
564 return false;
565
566 return true;
567}
568
569void ieee80211_run_deferred_scan(struct ieee80211_local *local)
570{
571 lockdep_assert_held(&local->mtx);
572
573 if (!local->scan_req || local->scanning)
574 return;
575
e2fd5dbc
JB
576 if (!ieee80211_can_scan(local,
577 rcu_dereference_protected(
578 local->scan_sdata,
579 lockdep_is_held(&local->mtx))))
133d40f9
SG
580 return;
581
582 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
583 round_jiffies_relative(0));
584}
f3b85252 585
45ad6834
JB
586static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
587 const u8 *src, const u8 *dst,
588 const u8 *ssid, size_t ssid_len,
589 const u8 *ie, size_t ie_len,
590 u32 ratemask, u32 flags, u32 tx_flags,
591 struct ieee80211_channel *channel)
592{
593 struct sk_buff *skb;
594
595 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
596 ssid, ssid_len,
597 ie, ie_len, flags);
b9771d41 598
45ad6834 599 if (skb) {
b9771d41
JB
600 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
601 struct ieee80211_hdr *hdr = (void *)skb->data;
2b3dab13 602 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
b9771d41
JB
603 u16 sn = get_random_u32();
604
2b3dab13 605 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
b9771d41
JB
606 hdr->seq_ctrl =
607 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
608 }
45ad6834 609 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
08aca29a 610 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
45ad6834
JB
611 }
612}
613
8a690674
BG
614static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
615 unsigned long *next_delay)
616{
617 int i;
e2fd5dbc 618 struct ieee80211_sub_if_data *sdata;
6ea0a69c 619 struct cfg80211_scan_request *scan_req;
57fbcce3 620 enum nl80211_band band = local->hw.conf.chandef.chan->band;
b9771d41 621 u32 flags = 0, tx_flags;
6c17b77b 622
6ea0a69c
JB
623 scan_req = rcu_dereference_protected(local->scan_req,
624 lockdep_is_held(&local->mtx));
625
6c17b77b 626 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
6ea0a69c 627 if (scan_req->no_cck)
6c17b77b 628 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
b9771d41
JB
629 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
630 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
631 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
632 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
8a690674 633
e2fd5dbc 634 sdata = rcu_dereference_protected(local->scan_sdata,
316b6b5d 635 lockdep_is_held(&local->mtx));
e2fd5dbc 636
6ea0a69c 637 for (i = 0; i < scan_req->n_ssids; i++)
45ad6834 638 ieee80211_send_scan_probe_req(
e345f44f 639 sdata, local->scan_addr, scan_req->bssid,
6ea0a69c
JB
640 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
641 scan_req->ie, scan_req->ie_len,
b9771d41 642 scan_req->rates[band], flags,
45ad6834 643 tx_flags, local->hw.conf.chandef.chan);
8a690674
BG
644
645 /*
646 * After sending probe requests, wait for probe responses
647 * on the channel.
648 */
649 *next_delay = IEEE80211_CHANNEL_TIME;
650 local->next_scan_state = SCAN_DECISION;
651}
652
f3b85252
JB
653static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
654 struct cfg80211_scan_request *req)
655{
656 struct ieee80211_local *local = sdata->local;
e9da68dd 657 bool hw_scan = local->ops->hw_scan;
f3b85252
JB
658 int rc;
659
e229f844
SG
660 lockdep_assert_held(&local->mtx);
661
dc0c18ed
AK
662 if (local->scan_req)
663 return -EBUSY;
664
665 if (!__ieee80211_can_leave_ch(sdata))
f3b85252
JB
666 return -EBUSY;
667
133d40f9 668 if (!ieee80211_can_scan(local, sdata)) {
6e7e6213 669 /* wait for the work to finish/time out */
6ea0a69c 670 rcu_assign_pointer(local->scan_req, req);
e2fd5dbc 671 rcu_assign_pointer(local->scan_sdata, sdata);
c0ce77b8
JB
672 return 0;
673 }
674
e9da68dd
JB
675 again:
676 if (hw_scan) {
f3b85252 677 u8 *ies;
f3b85252 678
e4dcbb37 679 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
c56ef672 680
30686bf7 681 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
c56ef672
DS
682 int i, n_bands = 0;
683 u8 bands_counted = 0;
684
685 for (i = 0; i < req->n_channels; i++) {
686 if (bands_counted & BIT(req->channels[i]->band))
687 continue;
688 bands_counted |= BIT(req->channels[i]->band);
689 n_bands++;
690 }
691
692 local->hw_scan_ies_bufsize *= n_bands;
693 }
694
4d36ec58
JB
695 local->hw_scan_req = kmalloc(
696 sizeof(*local->hw_scan_req) +
697 req->n_channels * sizeof(req->channels[0]) +
c604b9f2 698 local->hw_scan_ies_bufsize, GFP_KERNEL);
4d36ec58 699 if (!local->hw_scan_req)
f3b85252
JB
700 return -ENOMEM;
701
c56ef672
DS
702 local->hw_scan_req->req.ssids = req->ssids;
703 local->hw_scan_req->req.n_ssids = req->n_ssids;
4d36ec58
JB
704 ies = (u8 *)local->hw_scan_req +
705 sizeof(*local->hw_scan_req) +
706 req->n_channels * sizeof(req->channels[0]);
c56ef672
DS
707 local->hw_scan_req->req.ie = ies;
708 local->hw_scan_req->req.flags = req->flags;
e345f44f 709 eth_broadcast_addr(local->hw_scan_req->req.bssid);
7947d3e0
AS
710 local->hw_scan_req->req.duration = req->duration;
711 local->hw_scan_req->req.duration_mandatory =
712 req->duration_mandatory;
4d36ec58
JB
713
714 local->hw_scan_band = 0;
6e7e6213
JL
715
716 /*
717 * After allocating local->hw_scan_req, we must
718 * go through until ieee80211_prep_hw_scan(), so
719 * anything that might be changed here and leave
720 * this function early must not go after this
721 * allocation.
722 */
f3b85252
JB
723 }
724
6ea0a69c 725 rcu_assign_pointer(local->scan_req, req);
e2fd5dbc 726 rcu_assign_pointer(local->scan_sdata, sdata);
f3b85252 727
a344d677
JB
728 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
729 get_random_mask_addr(local->scan_addr,
730 req->mac_addr,
731 req->mac_addr_mask);
732 else
733 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
734
e9da68dd 735 if (hw_scan) {
fbe9c429 736 __set_bit(SCAN_HW_SCANNING, &local->scanning);
8a690674 737 } else if ((req->n_channels == 1) &&
675a0b04 738 (req->channels[0] == local->_oper_chandef.chan)) {
9b864870
JB
739 /*
740 * If we are scanning only on the operating channel
741 * then we do not need to stop normal activities
8a690674
BG
742 */
743 unsigned long next_delay;
744
745 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
746
747 ieee80211_recalc_idle(local);
748
749 /* Notify driver scan is starting, keep order of operations
750 * same as normal software scan, in case that matters. */
a344d677 751 drv_sw_scan_start(local, sdata, local->scan_addr);
8a690674
BG
752
753 ieee80211_configure_filter(local); /* accept probe-responses */
754
755 /* We need to ensure power level is at max for scanning. */
756 ieee80211_hw_config(local, 0);
757
4e39ccac
AQ
758 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
759 IEEE80211_CHAN_RADAR)) ||
6ea0a69c 760 !req->n_ssids) {
8a690674
BG
761 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
762 } else {
763 ieee80211_scan_state_send_probe(local, &next_delay);
764 next_delay = IEEE80211_CHANNEL_TIME;
765 }
766
767 /* Now, just wait a bit and we are all done! */
768 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
769 next_delay);
770 return 0;
771 } else {
772 /* Do normal software scan */
fbe9c429 773 __set_bit(SCAN_SW_SCANNING, &local->scanning);
8a690674 774 }
6e7e6213 775
5cff20e6 776 ieee80211_recalc_idle(local);
f3b85252 777
e9da68dd 778 if (hw_scan) {
2ad2274c 779 WARN_ON(!ieee80211_prep_hw_scan(sdata));
a060bbfe 780 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
a344d677
JB
781 } else {
782 rc = ieee80211_start_sw_scan(local, sdata);
783 }
f3b85252 784
f3b85252 785 if (rc) {
4d36ec58
JB
786 kfree(local->hw_scan_req);
787 local->hw_scan_req = NULL;
fbe9c429 788 local->scanning = 0;
f3b85252 789
5cff20e6
JB
790 ieee80211_recalc_idle(local);
791
f3b85252 792 local->scan_req = NULL;
0c2bef46 793 RCU_INIT_POINTER(local->scan_sdata, NULL);
f3b85252
JB
794 }
795
e9da68dd
JB
796 if (hw_scan && rc == 1) {
797 /*
798 * we can't fall back to software for P2P-GO
799 * as it must update NoA etc.
800 */
801 if (ieee80211_vif_type_p2p(&sdata->vif) ==
802 NL80211_IFTYPE_P2P_GO)
803 return -EOPNOTSUPP;
804 hw_scan = false;
805 goto again;
806 }
807
f3b85252
JB
808 return rc;
809}
810
df13cce5
HS
811static unsigned long
812ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
813{
814 /*
815 * TODO: channel switching also consumes quite some time,
816 * add that delay as well to get a better estimation
817 */
4e39ccac 818 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
df13cce5
HS
819 return IEEE80211_PASSIVE_CHANNEL_TIME;
820 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
821}
822
e229f844
SG
823static void ieee80211_scan_state_decision(struct ieee80211_local *local,
824 unsigned long *next_delay)
7d3be3cc 825{
142b9f50 826 bool associated = false;
df13cce5
HS
827 bool tx_empty = true;
828 bool bad_latency;
142b9f50 829 struct ieee80211_sub_if_data *sdata;
df13cce5 830 struct ieee80211_channel *next_chan;
cd2bb512 831 enum mac80211_scan_state next_scan_state;
6ea0a69c 832 struct cfg80211_scan_request *scan_req;
142b9f50 833
df13cce5
HS
834 /*
835 * check if at least one STA interface is associated,
836 * check if at least one STA interface has pending tx frames
837 * and grab the lowest used beacon interval
838 */
142b9f50
HS
839 mutex_lock(&local->iflist_mtx);
840 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 841 if (!ieee80211_sdata_running(sdata))
142b9f50
HS
842 continue;
843
844 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
845 if (sdata->u.mgd.associated) {
846 associated = true;
df13cce5 847
df13cce5
HS
848 if (!qdisc_all_tx_empty(sdata->dev)) {
849 tx_empty = false;
850 break;
851 }
142b9f50
HS
852 }
853 }
854 }
855 mutex_unlock(&local->iflist_mtx);
856
6ea0a69c
JB
857 scan_req = rcu_dereference_protected(local->scan_req,
858 lockdep_is_held(&local->mtx));
859
860 next_chan = scan_req->channels[local->scan_channel_idx];
b23b025f 861
a80f7c0b 862 /*
07ef03ee
JB
863 * we're currently scanning a different channel, let's
864 * see if we can scan another channel without interfering
865 * with the current traffic situation.
866 *
3f892b61 867 * Keep good latency, do not stay off-channel more than 125 ms.
a80f7c0b 868 */
a80f7c0b 869
07ef03ee 870 bad_latency = time_after(jiffies +
3f892b61
SG
871 ieee80211_scan_get_channel_time(next_chan),
872 local->leave_oper_channel_time + HZ / 8);
142b9f50 873
cd2bb512 874 if (associated && !tx_empty) {
6ea0a69c 875 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
cd2bb512
SL
876 next_scan_state = SCAN_ABORT;
877 else
878 next_scan_state = SCAN_SUSPEND;
3f892b61 879 } else if (associated && bad_latency) {
cd2bb512
SL
880 next_scan_state = SCAN_SUSPEND;
881 } else {
882 next_scan_state = SCAN_SET_CHANNEL;
883 }
884
885 local->next_scan_state = next_scan_state;
142b9f50 886
07ef03ee 887 *next_delay = 0;
142b9f50
HS
888}
889
2fb3f028
HS
890static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
891 unsigned long *next_delay)
892{
893 int skip;
894 struct ieee80211_channel *chan;
7ca15a0a 895 enum nl80211_bss_scan_width oper_scan_width;
6ea0a69c
JB
896 struct cfg80211_scan_request *scan_req;
897
898 scan_req = rcu_dereference_protected(local->scan_req,
899 lockdep_is_held(&local->mtx));
2fb3f028 900
7d3be3cc 901 skip = 0;
6ea0a69c 902 chan = scan_req->channels[local->scan_channel_idx];
7d3be3cc 903
7ca15a0a
SW
904 local->scan_chandef.chan = chan;
905 local->scan_chandef.center_freq1 = chan->center_freq;
b6011960 906 local->scan_chandef.freq1_offset = chan->freq_offset;
7ca15a0a 907 local->scan_chandef.center_freq2 = 0;
6ea0a69c 908 switch (scan_req->scan_width) {
7ca15a0a
SW
909 case NL80211_BSS_CHAN_WIDTH_5:
910 local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
911 break;
912 case NL80211_BSS_CHAN_WIDTH_10:
913 local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
914 break;
df78a0c0 915 default:
7ca15a0a
SW
916 case NL80211_BSS_CHAN_WIDTH_20:
917 /* If scanning on oper channel, use whatever channel-type
918 * is currently in use.
919 */
920 oper_scan_width = cfg80211_chandef_to_scan_width(
921 &local->_oper_chandef);
922 if (chan == local->_oper_chandef.chan &&
6ea0a69c 923 oper_scan_width == scan_req->scan_width)
7ca15a0a
SW
924 local->scan_chandef = local->_oper_chandef;
925 else
926 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
927 break;
928 }
b23b025f 929
07ef03ee
JB
930 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
931 skip = 1;
7d3be3cc 932
7d3be3cc
HS
933 /* advance state machine to next channel/band */
934 local->scan_channel_idx++;
935
0ee9c13c
HS
936 if (skip) {
937 /* if we skip this channel return to the decision state */
938 local->next_scan_state = SCAN_DECISION;
2fb3f028 939 return;
0ee9c13c 940 }
7d3be3cc
HS
941
942 /*
943 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
944 * (which unfortunately doesn't say _why_ step a) is done,
945 * but it waits for the probe delay or until a frame is
946 * received - and the received frame would update the NAV).
947 * For now, we do not support waiting until a frame is
948 * received.
949 *
950 * In any case, it is not necessary for a passive scan.
951 */
4e39ccac
AQ
952 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
953 !scan_req->n_ssids) {
7d3be3cc 954 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
977923b0 955 local->next_scan_state = SCAN_DECISION;
2fb3f028 956 return;
7d3be3cc
HS
957 }
958
2fb3f028 959 /* active scan, send probes */
7d3be3cc 960 *next_delay = IEEE80211_PROBE_DELAY;
977923b0 961 local->next_scan_state = SCAN_SEND_PROBE;
7d3be3cc
HS
962}
963
07ef03ee
JB
964static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
965 unsigned long *next_delay)
966{
967 /* switch back to the operating channel */
7ca15a0a 968 local->scan_chandef.chan = NULL;
07ef03ee
JB
969 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
970
aacde9ee
SG
971 /* disable PS */
972 ieee80211_offchannel_return(local);
07ef03ee
JB
973
974 *next_delay = HZ / 5;
975 /* afterwards, resume scan & go to next channel */
976 local->next_scan_state = SCAN_RESUME;
977}
978
979static void ieee80211_scan_state_resume(struct ieee80211_local *local,
980 unsigned long *next_delay)
981{
aacde9ee 982 ieee80211_offchannel_stop_vifs(local);
07ef03ee
JB
983
984 if (local->ops->flush) {
3b24f4c6 985 ieee80211_flush_queues(local, NULL, false);
07ef03ee
JB
986 *next_delay = 0;
987 } else
988 *next_delay = HZ / 10;
989
990 /* remember when we left the operating channel */
991 local->leave_oper_channel_time = jiffies;
992
993 /* advance to the next channel to be scanned */
de2ee84d 994 local->next_scan_state = SCAN_SET_CHANNEL;
07ef03ee
JB
995}
996
c2b13452 997void ieee80211_scan_work(struct work_struct *work)
0a51b27e
JB
998{
999 struct ieee80211_local *local =
1000 container_of(work, struct ieee80211_local, scan_work.work);
d07bfd8b 1001 struct ieee80211_sub_if_data *sdata;
6ea0a69c 1002 struct cfg80211_scan_request *scan_req;
0a51b27e 1003 unsigned long next_delay = 0;
8bd2a248 1004 bool aborted;
0a51b27e 1005
259b62e3 1006 mutex_lock(&local->mtx);
8789d459 1007
332ff7fe
LC
1008 if (!ieee80211_can_run_worker(local)) {
1009 aborted = true;
1010 goto out_complete;
1011 }
1012
e2fd5dbc
JB
1013 sdata = rcu_dereference_protected(local->scan_sdata,
1014 lockdep_is_held(&local->mtx));
6ea0a69c
JB
1015 scan_req = rcu_dereference_protected(local->scan_req,
1016 lockdep_is_held(&local->mtx));
d07bfd8b 1017
8a690674
BG
1018 /* When scanning on-channel, the first-callback means completed. */
1019 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
1020 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
1021 goto out_complete;
1022 }
1023
259b62e3 1024 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
8789d459 1025 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
259b62e3 1026 goto out_complete;
8789d459
JB
1027 }
1028
6ea0a69c 1029 if (!sdata || !scan_req)
259b62e3 1030 goto out;
f3b85252 1031
ff5db439 1032 if (!local->scanning) {
f3b85252
JB
1033 int rc;
1034
6ea0a69c 1035 RCU_INIT_POINTER(local->scan_req, NULL);
0c2bef46 1036 RCU_INIT_POINTER(local->scan_sdata, NULL);
f3b85252 1037
6ea0a69c 1038 rc = __ieee80211_start_scan(sdata, scan_req);
259b62e3 1039 if (rc) {
3aed49ef 1040 /* need to complete scan in cfg80211 */
6ea0a69c 1041 rcu_assign_pointer(local->scan_req, scan_req);
259b62e3
SG
1042 aborted = true;
1043 goto out_complete;
1044 } else
1045 goto out;
f3b85252
JB
1046 }
1047
f502d09b
HS
1048 /*
1049 * as long as no delay is required advance immediately
1050 * without scheduling a new work
1051 */
1052 do {
c29acf20
RM
1053 if (!ieee80211_sdata_running(sdata)) {
1054 aborted = true;
1055 goto out_complete;
1056 }
1057
977923b0 1058 switch (local->next_scan_state) {
2fb3f028 1059 case SCAN_DECISION:
e229f844 1060 /* if no more bands/channels left, complete scan */
6ea0a69c 1061 if (local->scan_channel_idx >= scan_req->n_channels) {
e229f844
SG
1062 aborted = false;
1063 goto out_complete;
1064 }
1065 ieee80211_scan_state_decision(local, &next_delay);
f502d09b 1066 break;
2fb3f028
HS
1067 case SCAN_SET_CHANNEL:
1068 ieee80211_scan_state_set_channel(local, &next_delay);
1069 break;
f502d09b
HS
1070 case SCAN_SEND_PROBE:
1071 ieee80211_scan_state_send_probe(local, &next_delay);
1072 break;
07ef03ee
JB
1073 case SCAN_SUSPEND:
1074 ieee80211_scan_state_suspend(local, &next_delay);
142b9f50 1075 break;
07ef03ee
JB
1076 case SCAN_RESUME:
1077 ieee80211_scan_state_resume(local, &next_delay);
142b9f50 1078 break;
cd2bb512
SL
1079 case SCAN_ABORT:
1080 aborted = true;
1081 goto out_complete;
f502d09b
HS
1082 }
1083 } while (next_delay == 0);
0a51b27e 1084
42935eca 1085 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
d07bfd8b 1086 goto out;
259b62e3
SG
1087
1088out_complete:
8bd2a248 1089 __ieee80211_scan_completed(&local->hw, aborted);
259b62e3
SG
1090out:
1091 mutex_unlock(&local->mtx);
0a51b27e
JB
1092}
1093
f3b85252
JB
1094int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
1095 struct cfg80211_scan_request *req)
0a51b27e 1096{
f3b85252 1097 int res;
de95a54b 1098
a1699b75 1099 mutex_lock(&sdata->local->mtx);
f3b85252 1100 res = __ieee80211_start_scan(sdata, req);
a1699b75 1101 mutex_unlock(&sdata->local->mtx);
0a51b27e 1102
f3b85252 1103 return res;
0a51b27e
JB
1104}
1105
34bcf715
SG
1106int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1107 const u8 *ssid, u8 ssid_len,
76bed0f4
JD
1108 struct ieee80211_channel **channels,
1109 unsigned int n_channels,
7ca15a0a 1110 enum nl80211_bss_scan_width scan_width)
0a51b27e 1111{
0a51b27e 1112 struct ieee80211_local *local = sdata->local;
76bed0f4 1113 int ret = -EBUSY, i, n_ch = 0;
57fbcce3 1114 enum nl80211_band band;
2a519311 1115
a1699b75 1116 mutex_lock(&local->mtx);
0a51b27e 1117
f3b85252
JB
1118 /* busy scanning */
1119 if (local->scan_req)
1120 goto unlock;
9116dd01 1121
be4a4b6a 1122 /* fill internal scan request */
76bed0f4
JD
1123 if (!channels) {
1124 int max_n;
be4a4b6a 1125
57fbcce3 1126 for (band = 0; band < NUM_NL80211_BANDS; band++) {
be4a4b6a
JB
1127 if (!local->hw.wiphy->bands[band])
1128 continue;
34bcf715
SG
1129
1130 max_n = local->hw.wiphy->bands[band]->n_channels;
1131 for (i = 0; i < max_n; i++) {
1132 struct ieee80211_channel *tmp_ch =
be4a4b6a 1133 &local->hw.wiphy->bands[band]->channels[i];
34bcf715 1134
8fe02e16 1135 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
34bcf715
SG
1136 IEEE80211_CHAN_DISABLED))
1137 continue;
1138
1139 local->int_scan_req->channels[n_ch] = tmp_ch;
1140 n_ch++;
be4a4b6a
JB
1141 }
1142 }
1143
34bcf715
SG
1144 if (WARN_ON_ONCE(n_ch == 0))
1145 goto unlock;
1146
1147 local->int_scan_req->n_channels = n_ch;
be4a4b6a 1148 } else {
76bed0f4
JD
1149 for (i = 0; i < n_channels; i++) {
1150 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
1151 IEEE80211_CHAN_DISABLED))
1152 continue;
1153
1154 local->int_scan_req->channels[n_ch] = channels[i];
1155 n_ch++;
1156 }
1157
1158 if (WARN_ON_ONCE(n_ch == 0))
34bcf715
SG
1159 goto unlock;
1160
76bed0f4 1161 local->int_scan_req->n_channels = n_ch;
be4a4b6a
JB
1162 }
1163
1164 local->int_scan_req->ssids = &local->scan_ssid;
1165 local->int_scan_req->n_ssids = 1;
7ca15a0a 1166 local->int_scan_req->scan_width = scan_width;
5ba63533
JB
1167 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
1168 local->int_scan_req->ssids[0].ssid_len = ssid_len;
9116dd01 1169
5ba63533 1170 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
f3b85252 1171 unlock:
a1699b75 1172 mutex_unlock(&local->mtx);
f3b85252 1173 return ret;
0a51b27e 1174}
5bb644a0 1175
4136c422
SG
1176/*
1177 * Only call this function when a scan can't be queued -- under RTNL.
1178 */
5bb644a0
JB
1179void ieee80211_scan_cancel(struct ieee80211_local *local)
1180{
5bb644a0 1181 /*
b856439b 1182 * We are canceling software scan, or deferred scan that was not
4136c422
SG
1183 * yet really started (see __ieee80211_start_scan ).
1184 *
1185 * Regarding hardware scan:
1186 * - we can not call __ieee80211_scan_completed() as when
1187 * SCAN_HW_SCANNING bit is set this function change
1188 * local->hw_scan_req to operate on 5G band, what race with
1189 * driver which can use local->hw_scan_req
1190 *
1191 * - we can not cancel scan_work since driver can schedule it
1192 * by ieee80211_scan_completed(..., true) to finish scan
1193 *
b856439b
EP
1194 * Hence we only call the cancel_hw_scan() callback, but the low-level
1195 * driver is still responsible for calling ieee80211_scan_completed()
1196 * after the scan was completed/aborted.
5bb644a0 1197 */
4136c422 1198
a1699b75 1199 mutex_lock(&local->mtx);
b856439b
EP
1200 if (!local->scan_req)
1201 goto out;
1202
a754055a
EG
1203 /*
1204 * We have a scan running and the driver already reported completion,
1205 * but the worker hasn't run yet or is stuck on the mutex - mark it as
1206 * cancelled.
1207 */
1208 if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
1209 test_bit(SCAN_COMPLETED, &local->scanning)) {
1210 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1211 goto out;
1212 }
1213
b856439b 1214 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
a754055a
EG
1215 /*
1216 * Make sure that __ieee80211_scan_completed doesn't trigger a
1217 * scan on another band.
1218 */
1219 set_bit(SCAN_HW_CANCELLED, &local->scanning);
b856439b 1220 if (local->ops->cancel_hw_scan)
e2fd5dbc
JB
1221 drv_cancel_hw_scan(local,
1222 rcu_dereference_protected(local->scan_sdata,
1223 lockdep_is_held(&local->mtx)));
b856439b 1224 goto out;
4136c422 1225 }
b856439b
EP
1226
1227 /*
1228 * If the work is currently running, it must be blocked on
1229 * the mutex, but we'll set scan_sdata = NULL and it'll
1230 * simply exit once it acquires the mutex.
1231 */
1232 cancel_delayed_work(&local->scan_work);
1233 /* and clean up */
7947d3e0 1234 memset(&local->scan_info, 0, sizeof(local->scan_info));
8bd2a248 1235 __ieee80211_scan_completed(&local->hw, true);
b856439b 1236out:
d07bfd8b 1237 mutex_unlock(&local->mtx);
5bb644a0 1238}
79f460ca 1239
d43c6b6e
DS
1240int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1241 struct cfg80211_sched_scan_request *req)
79f460ca
LC
1242{
1243 struct ieee80211_local *local = sdata->local;
633e2713 1244 struct ieee80211_scan_ies sched_scan_ies = {};
2103dec1 1245 struct cfg80211_chan_def chandef;
633e2713 1246 int ret, i, iebufsz, num_bands = 0;
57fbcce3 1247 u32 rate_masks[NUM_NL80211_BANDS] = {};
633e2713
DS
1248 u8 bands_used = 0;
1249 u8 *ie;
b9771d41 1250 u32 flags = 0;
c604b9f2 1251
e4dcbb37 1252 iebufsz = local->scan_ies_len + req->ie_len;
79f460ca 1253
d43c6b6e 1254 lockdep_assert_held(&local->mtx);
79f460ca 1255
d43c6b6e
DS
1256 if (!local->ops->sched_scan_start)
1257 return -ENOTSUPP;
79f460ca 1258
57fbcce3 1259 for (i = 0; i < NUM_NL80211_BANDS; i++) {
633e2713
DS
1260 if (local->hw.wiphy->bands[i]) {
1261 bands_used |= BIT(i);
1262 rate_masks[i] = (u32) -1;
1263 num_bands++;
79f460ca 1264 }
633e2713 1265 }
79f460ca 1266
b9771d41
JB
1267 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
1268 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
1269
6396bb22 1270 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
633e2713
DS
1271 if (!ie) {
1272 ret = -ENOMEM;
1273 goto out;
79f460ca
LC
1274 }
1275
633e2713
DS
1276 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
1277
2ad2274c 1278 ieee80211_build_preq_ies(sdata, ie, num_bands * iebufsz,
cd5861bd 1279 &sched_scan_ies, req->ie,
00387f32 1280 req->ie_len, bands_used, rate_masks, &chandef,
b9771d41 1281 flags);
633e2713 1282
30dd3edf 1283 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
d43c6b6e 1284 if (ret == 0) {
5260a5b2 1285 rcu_assign_pointer(local->sched_scan_sdata, sdata);
6ea0a69c 1286 rcu_assign_pointer(local->sched_scan_req, req);
d43c6b6e 1287 }
79f460ca 1288
633e2713 1289 kfree(ie);
d43c6b6e 1290
633e2713 1291out:
d43c6b6e
DS
1292 if (ret) {
1293 /* Clean in case of failure after HW restart or upon resume. */
0c2bef46 1294 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
6ea0a69c 1295 RCU_INIT_POINTER(local->sched_scan_req, NULL);
d43c6b6e
DS
1296 }
1297
1298 return ret;
1299}
1300
1301int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1302 struct cfg80211_sched_scan_request *req)
1303{
1304 struct ieee80211_local *local = sdata->local;
1305 int ret;
1306
1307 mutex_lock(&local->mtx);
1308
1309 if (rcu_access_pointer(local->sched_scan_sdata)) {
1310 mutex_unlock(&local->mtx);
1311 return -EBUSY;
1312 }
1313
1314 ret = __ieee80211_request_sched_scan_start(sdata, req);
1315
5260a5b2 1316 mutex_unlock(&local->mtx);
79f460ca
LC
1317 return ret;
1318}
1319
0d440ea2 1320int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
79f460ca 1321{
0d440ea2
EP
1322 struct ieee80211_sub_if_data *sched_scan_sdata;
1323 int ret = -ENOENT;
79f460ca 1324
5260a5b2 1325 mutex_lock(&local->mtx);
79f460ca
LC
1326
1327 if (!local->ops->sched_scan_stop) {
1328 ret = -ENOTSUPP;
1329 goto out;
1330 }
1331
d43c6b6e 1332 /* We don't want to restart sched scan anymore. */
6ea0a69c 1333 RCU_INIT_POINTER(local->sched_scan_req, NULL);
d43c6b6e 1334
0d440ea2
EP
1335 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1336 lockdep_is_held(&local->mtx));
1337 if (sched_scan_sdata) {
1338 ret = drv_sched_scan_stop(local, sched_scan_sdata);
71228a1e 1339 if (!ret)
ad053a96 1340 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
71228a1e 1341 }
79f460ca 1342out:
5260a5b2 1343 mutex_unlock(&local->mtx);
79f460ca
LC
1344
1345 return ret;
1346}
1347
1348void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1349{
1350 struct ieee80211_local *local = hw_to_local(hw);
1351
1352 trace_api_sched_scan_results(local);
1353
b34939b9 1354 cfg80211_sched_scan_results(hw->wiphy, 0);
79f460ca
LC
1355}
1356EXPORT_SYMBOL(ieee80211_sched_scan_results);
1357
f6837ba8 1358void ieee80211_sched_scan_end(struct ieee80211_local *local)
85a9994a 1359{
85a9994a
LC
1360 mutex_lock(&local->mtx);
1361
5260a5b2 1362 if (!rcu_access_pointer(local->sched_scan_sdata)) {
85a9994a
LC
1363 mutex_unlock(&local->mtx);
1364 return;
1365 }
1366
0c2bef46 1367 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
85a9994a 1368
d43c6b6e 1369 /* If sched scan was aborted by the driver. */
6ea0a69c 1370 RCU_INIT_POINTER(local->sched_scan_req, NULL);
d43c6b6e 1371
85a9994a
LC
1372 mutex_unlock(&local->mtx);
1373
b34939b9 1374 cfg80211_sched_scan_stopped(local->hw.wiphy, 0);
85a9994a
LC
1375}
1376
f6837ba8
JB
1377void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1378{
1379 struct ieee80211_local *local =
1380 container_of(work, struct ieee80211_local,
1381 sched_scan_stopped_work);
1382
1383 ieee80211_sched_scan_end(local);
1384}
1385
79f460ca
LC
1386void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1387{
1388 struct ieee80211_local *local = hw_to_local(hw);
1389
1390 trace_api_sched_scan_stopped(local);
1391
2bc533bd
EP
1392 /*
1393 * this shouldn't really happen, so for simplicity
1394 * simply ignore it, and let mac80211 reconfigure
1395 * the sched scan later on.
1396 */
1397 if (local->in_reconfig)
1398 return;
1399
18db594a 1400 schedule_work(&local->sched_scan_stopped_work);
79f460ca
LC
1401}
1402EXPORT_SYMBOL(ieee80211_sched_scan_stopped);