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