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