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