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