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