xref: /openbmc/linux/net/mac80211/util.c (revision 06ba8020)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  * Copyright (C) 2015-2017	Intel Deutschland GmbH
9  * Copyright (C) 2018-2022 Intel Corporation
10  *
11  * utilities for mac80211
12  */
13 
14 #include <net/mac80211.h>
15 #include <linux/netdevice.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/etherdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/bitmap.h>
23 #include <linux/crc32.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/rtnetlink.h>
27 
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "mesh.h"
32 #include "wme.h"
33 #include "led.h"
34 #include "wep.h"
35 
36 /* privid for wiphys to determine whether they belong to us or not */
37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
38 
39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
40 {
41 	struct ieee80211_local *local;
42 
43 	local = wiphy_priv(wiphy);
44 	return &local->hw;
45 }
46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
47 
48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
49 			enum nl80211_iftype type)
50 {
51 	__le16 fc = hdr->frame_control;
52 
53 	if (ieee80211_is_data(fc)) {
54 		if (len < 24) /* drop incorrect hdr len (data) */
55 			return NULL;
56 
57 		if (ieee80211_has_a4(fc))
58 			return NULL;
59 		if (ieee80211_has_tods(fc))
60 			return hdr->addr1;
61 		if (ieee80211_has_fromds(fc))
62 			return hdr->addr2;
63 
64 		return hdr->addr3;
65 	}
66 
67 	if (ieee80211_is_s1g_beacon(fc)) {
68 		struct ieee80211_ext *ext = (void *) hdr;
69 
70 		return ext->u.s1g_beacon.sa;
71 	}
72 
73 	if (ieee80211_is_mgmt(fc)) {
74 		if (len < 24) /* drop incorrect hdr len (mgmt) */
75 			return NULL;
76 		return hdr->addr3;
77 	}
78 
79 	if (ieee80211_is_ctl(fc)) {
80 		if (ieee80211_is_pspoll(fc))
81 			return hdr->addr1;
82 
83 		if (ieee80211_is_back_req(fc)) {
84 			switch (type) {
85 			case NL80211_IFTYPE_STATION:
86 				return hdr->addr2;
87 			case NL80211_IFTYPE_AP:
88 			case NL80211_IFTYPE_AP_VLAN:
89 				return hdr->addr1;
90 			default:
91 				break; /* fall through to the return */
92 			}
93 		}
94 	}
95 
96 	return NULL;
97 }
98 EXPORT_SYMBOL(ieee80211_get_bssid);
99 
100 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
101 {
102 	struct sk_buff *skb;
103 	struct ieee80211_hdr *hdr;
104 
105 	skb_queue_walk(&tx->skbs, skb) {
106 		hdr = (struct ieee80211_hdr *) skb->data;
107 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
108 	}
109 }
110 
111 int ieee80211_frame_duration(enum nl80211_band band, size_t len,
112 			     int rate, int erp, int short_preamble,
113 			     int shift)
114 {
115 	int dur;
116 
117 	/* calculate duration (in microseconds, rounded up to next higher
118 	 * integer if it includes a fractional microsecond) to send frame of
119 	 * len bytes (does not include FCS) at the given rate. Duration will
120 	 * also include SIFS.
121 	 *
122 	 * rate is in 100 kbps, so divident is multiplied by 10 in the
123 	 * DIV_ROUND_UP() operations.
124 	 *
125 	 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
126 	 * is assumed to be 0 otherwise.
127 	 */
128 
129 	if (band == NL80211_BAND_5GHZ || erp) {
130 		/*
131 		 * OFDM:
132 		 *
133 		 * N_DBPS = DATARATE x 4
134 		 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
135 		 *	(16 = SIGNAL time, 6 = tail bits)
136 		 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
137 		 *
138 		 * T_SYM = 4 usec
139 		 * 802.11a - 18.5.2: aSIFSTime = 16 usec
140 		 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
141 		 *	signal ext = 6 usec
142 		 */
143 		dur = 16; /* SIFS + signal ext */
144 		dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
145 		dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
146 
147 		/* IEEE 802.11-2012 18.3.2.4: all values above are:
148 		 *  * times 4 for 5 MHz
149 		 *  * times 2 for 10 MHz
150 		 */
151 		dur *= 1 << shift;
152 
153 		/* rates should already consider the channel bandwidth,
154 		 * don't apply divisor again.
155 		 */
156 		dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
157 					4 * rate); /* T_SYM x N_SYM */
158 	} else {
159 		/*
160 		 * 802.11b or 802.11g with 802.11b compatibility:
161 		 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
162 		 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
163 		 *
164 		 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
165 		 * aSIFSTime = 10 usec
166 		 * aPreambleLength = 144 usec or 72 usec with short preamble
167 		 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
168 		 */
169 		dur = 10; /* aSIFSTime = 10 usec */
170 		dur += short_preamble ? (72 + 24) : (144 + 48);
171 
172 		dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
173 	}
174 
175 	return dur;
176 }
177 
178 /* Exported duration function for driver use */
179 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
180 					struct ieee80211_vif *vif,
181 					enum nl80211_band band,
182 					size_t frame_len,
183 					struct ieee80211_rate *rate)
184 {
185 	struct ieee80211_sub_if_data *sdata;
186 	u16 dur;
187 	int erp, shift = 0;
188 	bool short_preamble = false;
189 
190 	erp = 0;
191 	if (vif) {
192 		sdata = vif_to_sdata(vif);
193 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
194 		if (sdata->deflink.operating_11g_mode)
195 			erp = rate->flags & IEEE80211_RATE_ERP_G;
196 		shift = ieee80211_vif_get_shift(vif);
197 	}
198 
199 	dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
200 				       short_preamble, shift);
201 
202 	return cpu_to_le16(dur);
203 }
204 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
205 
206 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
207 			      struct ieee80211_vif *vif, size_t frame_len,
208 			      const struct ieee80211_tx_info *frame_txctl)
209 {
210 	struct ieee80211_local *local = hw_to_local(hw);
211 	struct ieee80211_rate *rate;
212 	struct ieee80211_sub_if_data *sdata;
213 	bool short_preamble;
214 	int erp, shift = 0, bitrate;
215 	u16 dur;
216 	struct ieee80211_supported_band *sband;
217 
218 	sband = local->hw.wiphy->bands[frame_txctl->band];
219 
220 	short_preamble = false;
221 
222 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
223 
224 	erp = 0;
225 	if (vif) {
226 		sdata = vif_to_sdata(vif);
227 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
228 		if (sdata->deflink.operating_11g_mode)
229 			erp = rate->flags & IEEE80211_RATE_ERP_G;
230 		shift = ieee80211_vif_get_shift(vif);
231 	}
232 
233 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
234 
235 	/* CTS duration */
236 	dur = ieee80211_frame_duration(sband->band, 10, bitrate,
237 				       erp, short_preamble, shift);
238 	/* Data frame duration */
239 	dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
240 					erp, short_preamble, shift);
241 	/* ACK duration */
242 	dur += ieee80211_frame_duration(sband->band, 10, bitrate,
243 					erp, short_preamble, shift);
244 
245 	return cpu_to_le16(dur);
246 }
247 EXPORT_SYMBOL(ieee80211_rts_duration);
248 
249 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
250 				    struct ieee80211_vif *vif,
251 				    size_t frame_len,
252 				    const struct ieee80211_tx_info *frame_txctl)
253 {
254 	struct ieee80211_local *local = hw_to_local(hw);
255 	struct ieee80211_rate *rate;
256 	struct ieee80211_sub_if_data *sdata;
257 	bool short_preamble;
258 	int erp, shift = 0, bitrate;
259 	u16 dur;
260 	struct ieee80211_supported_band *sband;
261 
262 	sband = local->hw.wiphy->bands[frame_txctl->band];
263 
264 	short_preamble = false;
265 
266 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
267 	erp = 0;
268 	if (vif) {
269 		sdata = vif_to_sdata(vif);
270 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
271 		if (sdata->deflink.operating_11g_mode)
272 			erp = rate->flags & IEEE80211_RATE_ERP_G;
273 		shift = ieee80211_vif_get_shift(vif);
274 	}
275 
276 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
277 
278 	/* Data frame duration */
279 	dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
280 				       erp, short_preamble, shift);
281 	if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
282 		/* ACK duration */
283 		dur += ieee80211_frame_duration(sband->band, 10, bitrate,
284 						erp, short_preamble, shift);
285 	}
286 
287 	return cpu_to_le16(dur);
288 }
289 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
290 
291 static void wake_tx_push_queue(struct ieee80211_local *local,
292 			       struct ieee80211_sub_if_data *sdata,
293 			       struct ieee80211_txq *queue)
294 {
295 	struct ieee80211_tx_control control = {
296 		.sta = queue->sta,
297 	};
298 	struct sk_buff *skb;
299 
300 	while (1) {
301 		skb = ieee80211_tx_dequeue(&local->hw, queue);
302 		if (!skb)
303 			break;
304 
305 		drv_tx(local, &control, skb);
306 	}
307 }
308 
309 /* wake_tx_queue handler for driver not implementing a custom one*/
310 void ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
311 				    struct ieee80211_txq *txq)
312 {
313 	struct ieee80211_local *local = hw_to_local(hw);
314 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
315 	struct ieee80211_txq *queue;
316 
317 	spin_lock(&local->handle_wake_tx_queue_lock);
318 
319 	/* Use ieee80211_next_txq() for airtime fairness accounting */
320 	ieee80211_txq_schedule_start(hw, txq->ac);
321 	while ((queue = ieee80211_next_txq(hw, txq->ac))) {
322 		wake_tx_push_queue(local, sdata, queue);
323 		ieee80211_return_txq(hw, queue, false);
324 	}
325 	ieee80211_txq_schedule_end(hw, txq->ac);
326 	spin_unlock(&local->handle_wake_tx_queue_lock);
327 }
328 EXPORT_SYMBOL(ieee80211_handle_wake_tx_queue);
329 
330 static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
331 {
332 	struct ieee80211_local *local = sdata->local;
333 	struct ieee80211_vif *vif = &sdata->vif;
334 	struct fq *fq = &local->fq;
335 	struct ps_data *ps = NULL;
336 	struct txq_info *txqi;
337 	struct sta_info *sta;
338 	int i;
339 
340 	local_bh_disable();
341 	spin_lock(&fq->lock);
342 
343 	if (!test_bit(SDATA_STATE_RUNNING, &sdata->state))
344 		goto out;
345 
346 	if (sdata->vif.type == NL80211_IFTYPE_AP)
347 		ps = &sdata->bss->ps;
348 
349 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
350 		if (sdata != sta->sdata)
351 			continue;
352 
353 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
354 			struct ieee80211_txq *txq = sta->sta.txq[i];
355 
356 			if (!txq)
357 				continue;
358 
359 			txqi = to_txq_info(txq);
360 
361 			if (ac != txq->ac)
362 				continue;
363 
364 			if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY,
365 						&txqi->flags))
366 				continue;
367 
368 			spin_unlock(&fq->lock);
369 			drv_wake_tx_queue(local, txqi);
370 			spin_lock(&fq->lock);
371 		}
372 	}
373 
374 	if (!vif->txq)
375 		goto out;
376 
377 	txqi = to_txq_info(vif->txq);
378 
379 	if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY, &txqi->flags) ||
380 	    (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
381 		goto out;
382 
383 	spin_unlock(&fq->lock);
384 
385 	drv_wake_tx_queue(local, txqi);
386 	local_bh_enable();
387 	return;
388 out:
389 	spin_unlock(&fq->lock);
390 	local_bh_enable();
391 }
392 
393 static void
394 __releases(&local->queue_stop_reason_lock)
395 __acquires(&local->queue_stop_reason_lock)
396 _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
397 {
398 	struct ieee80211_sub_if_data *sdata;
399 	int n_acs = IEEE80211_NUM_ACS;
400 	int i;
401 
402 	rcu_read_lock();
403 
404 	if (local->hw.queues < IEEE80211_NUM_ACS)
405 		n_acs = 1;
406 
407 	for (i = 0; i < local->hw.queues; i++) {
408 		if (local->queue_stop_reasons[i])
409 			continue;
410 
411 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
412 		list_for_each_entry_rcu(sdata, &local->interfaces, list) {
413 			int ac;
414 
415 			for (ac = 0; ac < n_acs; ac++) {
416 				int ac_queue = sdata->vif.hw_queue[ac];
417 
418 				if (ac_queue == i ||
419 				    sdata->vif.cab_queue == i)
420 					__ieee80211_wake_txqs(sdata, ac);
421 			}
422 		}
423 		spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
424 	}
425 
426 	rcu_read_unlock();
427 }
428 
429 void ieee80211_wake_txqs(struct tasklet_struct *t)
430 {
431 	struct ieee80211_local *local = from_tasklet(local, t,
432 						     wake_txqs_tasklet);
433 	unsigned long flags;
434 
435 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
436 	_ieee80211_wake_txqs(local, &flags);
437 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
438 }
439 
440 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
441 				   enum queue_stop_reason reason,
442 				   bool refcounted,
443 				   unsigned long *flags)
444 {
445 	struct ieee80211_local *local = hw_to_local(hw);
446 
447 	trace_wake_queue(local, queue, reason);
448 
449 	if (WARN_ON(queue >= hw->queues))
450 		return;
451 
452 	if (!test_bit(reason, &local->queue_stop_reasons[queue]))
453 		return;
454 
455 	if (!refcounted) {
456 		local->q_stop_reasons[queue][reason] = 0;
457 	} else {
458 		local->q_stop_reasons[queue][reason]--;
459 		if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
460 			local->q_stop_reasons[queue][reason] = 0;
461 	}
462 
463 	if (local->q_stop_reasons[queue][reason] == 0)
464 		__clear_bit(reason, &local->queue_stop_reasons[queue]);
465 
466 	if (local->queue_stop_reasons[queue] != 0)
467 		/* someone still has this queue stopped */
468 		return;
469 
470 	if (!skb_queue_empty(&local->pending[queue]))
471 		tasklet_schedule(&local->tx_pending_tasklet);
472 
473 	/*
474 	 * Calling _ieee80211_wake_txqs here can be a problem because it may
475 	 * release queue_stop_reason_lock which has been taken by
476 	 * __ieee80211_wake_queue's caller. It is certainly not very nice to
477 	 * release someone's lock, but it is fine because all the callers of
478 	 * __ieee80211_wake_queue call it right before releasing the lock.
479 	 */
480 	if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
481 		tasklet_schedule(&local->wake_txqs_tasklet);
482 	else
483 		_ieee80211_wake_txqs(local, flags);
484 }
485 
486 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
487 				    enum queue_stop_reason reason,
488 				    bool refcounted)
489 {
490 	struct ieee80211_local *local = hw_to_local(hw);
491 	unsigned long flags;
492 
493 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
494 	__ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
495 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
496 }
497 
498 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
499 {
500 	ieee80211_wake_queue_by_reason(hw, queue,
501 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
502 				       false);
503 }
504 EXPORT_SYMBOL(ieee80211_wake_queue);
505 
506 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
507 				   enum queue_stop_reason reason,
508 				   bool refcounted)
509 {
510 	struct ieee80211_local *local = hw_to_local(hw);
511 
512 	trace_stop_queue(local, queue, reason);
513 
514 	if (WARN_ON(queue >= hw->queues))
515 		return;
516 
517 	if (!refcounted)
518 		local->q_stop_reasons[queue][reason] = 1;
519 	else
520 		local->q_stop_reasons[queue][reason]++;
521 
522 	set_bit(reason, &local->queue_stop_reasons[queue]);
523 }
524 
525 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
526 				    enum queue_stop_reason reason,
527 				    bool refcounted)
528 {
529 	struct ieee80211_local *local = hw_to_local(hw);
530 	unsigned long flags;
531 
532 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
533 	__ieee80211_stop_queue(hw, queue, reason, refcounted);
534 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
535 }
536 
537 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
538 {
539 	ieee80211_stop_queue_by_reason(hw, queue,
540 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
541 				       false);
542 }
543 EXPORT_SYMBOL(ieee80211_stop_queue);
544 
545 void ieee80211_add_pending_skb(struct ieee80211_local *local,
546 			       struct sk_buff *skb)
547 {
548 	struct ieee80211_hw *hw = &local->hw;
549 	unsigned long flags;
550 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
551 	int queue = info->hw_queue;
552 
553 	if (WARN_ON(!info->control.vif)) {
554 		ieee80211_free_txskb(&local->hw, skb);
555 		return;
556 	}
557 
558 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
559 	__ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
560 			       false);
561 	__skb_queue_tail(&local->pending[queue], skb);
562 	__ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
563 			       false, &flags);
564 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
565 }
566 
567 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
568 				struct sk_buff_head *skbs)
569 {
570 	struct ieee80211_hw *hw = &local->hw;
571 	struct sk_buff *skb;
572 	unsigned long flags;
573 	int queue, i;
574 
575 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
576 	while ((skb = skb_dequeue(skbs))) {
577 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
578 
579 		if (WARN_ON(!info->control.vif)) {
580 			ieee80211_free_txskb(&local->hw, skb);
581 			continue;
582 		}
583 
584 		queue = info->hw_queue;
585 
586 		__ieee80211_stop_queue(hw, queue,
587 				IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
588 				false);
589 
590 		__skb_queue_tail(&local->pending[queue], skb);
591 	}
592 
593 	for (i = 0; i < hw->queues; i++)
594 		__ieee80211_wake_queue(hw, i,
595 			IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
596 			false, &flags);
597 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
598 }
599 
600 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
601 				     unsigned long queues,
602 				     enum queue_stop_reason reason,
603 				     bool refcounted)
604 {
605 	struct ieee80211_local *local = hw_to_local(hw);
606 	unsigned long flags;
607 	int i;
608 
609 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
610 
611 	for_each_set_bit(i, &queues, hw->queues)
612 		__ieee80211_stop_queue(hw, i, reason, refcounted);
613 
614 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
615 }
616 
617 void ieee80211_stop_queues(struct ieee80211_hw *hw)
618 {
619 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
620 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
621 					false);
622 }
623 EXPORT_SYMBOL(ieee80211_stop_queues);
624 
625 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
626 {
627 	struct ieee80211_local *local = hw_to_local(hw);
628 	unsigned long flags;
629 	int ret;
630 
631 	if (WARN_ON(queue >= hw->queues))
632 		return true;
633 
634 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
635 	ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
636 		       &local->queue_stop_reasons[queue]);
637 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
638 	return ret;
639 }
640 EXPORT_SYMBOL(ieee80211_queue_stopped);
641 
642 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
643 				     unsigned long queues,
644 				     enum queue_stop_reason reason,
645 				     bool refcounted)
646 {
647 	struct ieee80211_local *local = hw_to_local(hw);
648 	unsigned long flags;
649 	int i;
650 
651 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
652 
653 	for_each_set_bit(i, &queues, hw->queues)
654 		__ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
655 
656 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
657 }
658 
659 void ieee80211_wake_queues(struct ieee80211_hw *hw)
660 {
661 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
662 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
663 					false);
664 }
665 EXPORT_SYMBOL(ieee80211_wake_queues);
666 
667 static unsigned int
668 ieee80211_get_vif_queues(struct ieee80211_local *local,
669 			 struct ieee80211_sub_if_data *sdata)
670 {
671 	unsigned int queues;
672 
673 	if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
674 		int ac;
675 
676 		queues = 0;
677 
678 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
679 			queues |= BIT(sdata->vif.hw_queue[ac]);
680 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
681 			queues |= BIT(sdata->vif.cab_queue);
682 	} else {
683 		/* all queues */
684 		queues = BIT(local->hw.queues) - 1;
685 	}
686 
687 	return queues;
688 }
689 
690 void __ieee80211_flush_queues(struct ieee80211_local *local,
691 			      struct ieee80211_sub_if_data *sdata,
692 			      unsigned int queues, bool drop)
693 {
694 	if (!local->ops->flush)
695 		return;
696 
697 	/*
698 	 * If no queue was set, or if the HW doesn't support
699 	 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
700 	 */
701 	if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
702 		queues = ieee80211_get_vif_queues(local, sdata);
703 
704 	ieee80211_stop_queues_by_reason(&local->hw, queues,
705 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
706 					false);
707 
708 	drv_flush(local, sdata, queues, drop);
709 
710 	ieee80211_wake_queues_by_reason(&local->hw, queues,
711 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
712 					false);
713 }
714 
715 void ieee80211_flush_queues(struct ieee80211_local *local,
716 			    struct ieee80211_sub_if_data *sdata, bool drop)
717 {
718 	__ieee80211_flush_queues(local, sdata, 0, drop);
719 }
720 
721 void ieee80211_stop_vif_queues(struct ieee80211_local *local,
722 			       struct ieee80211_sub_if_data *sdata,
723 			       enum queue_stop_reason reason)
724 {
725 	ieee80211_stop_queues_by_reason(&local->hw,
726 					ieee80211_get_vif_queues(local, sdata),
727 					reason, true);
728 }
729 
730 void ieee80211_wake_vif_queues(struct ieee80211_local *local,
731 			       struct ieee80211_sub_if_data *sdata,
732 			       enum queue_stop_reason reason)
733 {
734 	ieee80211_wake_queues_by_reason(&local->hw,
735 					ieee80211_get_vif_queues(local, sdata),
736 					reason, true);
737 }
738 
739 static void __iterate_interfaces(struct ieee80211_local *local,
740 				 u32 iter_flags,
741 				 void (*iterator)(void *data, u8 *mac,
742 						  struct ieee80211_vif *vif),
743 				 void *data)
744 {
745 	struct ieee80211_sub_if_data *sdata;
746 	bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
747 
748 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
749 		switch (sdata->vif.type) {
750 		case NL80211_IFTYPE_MONITOR:
751 			if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
752 				continue;
753 			break;
754 		case NL80211_IFTYPE_AP_VLAN:
755 			continue;
756 		default:
757 			break;
758 		}
759 		if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
760 		    active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
761 			continue;
762 		if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) &&
763 		    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
764 			continue;
765 		if (ieee80211_sdata_running(sdata) || !active_only)
766 			iterator(data, sdata->vif.addr,
767 				 &sdata->vif);
768 	}
769 
770 	sdata = rcu_dereference_check(local->monitor_sdata,
771 				      lockdep_is_held(&local->iflist_mtx) ||
772 				      lockdep_is_held(&local->hw.wiphy->mtx));
773 	if (sdata &&
774 	    (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
775 	     sdata->flags & IEEE80211_SDATA_IN_DRIVER))
776 		iterator(data, sdata->vif.addr, &sdata->vif);
777 }
778 
779 void ieee80211_iterate_interfaces(
780 	struct ieee80211_hw *hw, u32 iter_flags,
781 	void (*iterator)(void *data, u8 *mac,
782 			 struct ieee80211_vif *vif),
783 	void *data)
784 {
785 	struct ieee80211_local *local = hw_to_local(hw);
786 
787 	mutex_lock(&local->iflist_mtx);
788 	__iterate_interfaces(local, iter_flags, iterator, data);
789 	mutex_unlock(&local->iflist_mtx);
790 }
791 EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
792 
793 void ieee80211_iterate_active_interfaces_atomic(
794 	struct ieee80211_hw *hw, u32 iter_flags,
795 	void (*iterator)(void *data, u8 *mac,
796 			 struct ieee80211_vif *vif),
797 	void *data)
798 {
799 	struct ieee80211_local *local = hw_to_local(hw);
800 
801 	rcu_read_lock();
802 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
803 			     iterator, data);
804 	rcu_read_unlock();
805 }
806 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
807 
808 void ieee80211_iterate_active_interfaces_mtx(
809 	struct ieee80211_hw *hw, u32 iter_flags,
810 	void (*iterator)(void *data, u8 *mac,
811 			 struct ieee80211_vif *vif),
812 	void *data)
813 {
814 	struct ieee80211_local *local = hw_to_local(hw);
815 
816 	lockdep_assert_wiphy(hw->wiphy);
817 
818 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
819 			     iterator, data);
820 }
821 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx);
822 
823 static void __iterate_stations(struct ieee80211_local *local,
824 			       void (*iterator)(void *data,
825 						struct ieee80211_sta *sta),
826 			       void *data)
827 {
828 	struct sta_info *sta;
829 
830 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
831 		if (!sta->uploaded)
832 			continue;
833 
834 		iterator(data, &sta->sta);
835 	}
836 }
837 
838 void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
839 			void (*iterator)(void *data,
840 					 struct ieee80211_sta *sta),
841 			void *data)
842 {
843 	struct ieee80211_local *local = hw_to_local(hw);
844 
845 	rcu_read_lock();
846 	__iterate_stations(local, iterator, data);
847 	rcu_read_unlock();
848 }
849 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
850 
851 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
852 {
853 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
854 
855 	if (!ieee80211_sdata_running(sdata) ||
856 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
857 		return NULL;
858 	return &sdata->vif;
859 }
860 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
861 
862 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
863 {
864 	if (!vif)
865 		return NULL;
866 
867 	return &vif_to_sdata(vif)->wdev;
868 }
869 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
870 
871 /*
872  * Nothing should have been stuffed into the workqueue during
873  * the suspend->resume cycle. Since we can't check each caller
874  * of this function if we are already quiescing / suspended,
875  * check here and don't WARN since this can actually happen when
876  * the rx path (for example) is racing against __ieee80211_suspend
877  * and suspending / quiescing was set after the rx path checked
878  * them.
879  */
880 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
881 {
882 	if (local->quiescing || (local->suspended && !local->resuming)) {
883 		pr_warn("queueing ieee80211 work while going to suspend\n");
884 		return false;
885 	}
886 
887 	return true;
888 }
889 
890 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
891 {
892 	struct ieee80211_local *local = hw_to_local(hw);
893 
894 	if (!ieee80211_can_queue_work(local))
895 		return;
896 
897 	queue_work(local->workqueue, work);
898 }
899 EXPORT_SYMBOL(ieee80211_queue_work);
900 
901 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
902 				  struct delayed_work *dwork,
903 				  unsigned long delay)
904 {
905 	struct ieee80211_local *local = hw_to_local(hw);
906 
907 	if (!ieee80211_can_queue_work(local))
908 		return;
909 
910 	queue_delayed_work(local->workqueue, dwork, delay);
911 }
912 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
913 
914 static void
915 ieee80211_parse_extension_element(u32 *crc,
916 				  const struct element *elem,
917 				  struct ieee802_11_elems *elems,
918 				  struct ieee80211_elems_parse_params *params)
919 {
920 	const void *data = elem->data + 1;
921 	u8 len;
922 
923 	if (!elem->datalen)
924 		return;
925 
926 	len = elem->datalen - 1;
927 
928 	switch (elem->data[0]) {
929 	case WLAN_EID_EXT_HE_MU_EDCA:
930 		if (len >= sizeof(*elems->mu_edca_param_set)) {
931 			elems->mu_edca_param_set = data;
932 			if (crc)
933 				*crc = crc32_be(*crc, (void *)elem,
934 						elem->datalen + 2);
935 		}
936 		break;
937 	case WLAN_EID_EXT_HE_CAPABILITY:
938 		if (ieee80211_he_capa_size_ok(data, len)) {
939 			elems->he_cap = data;
940 			elems->he_cap_len = len;
941 		}
942 		break;
943 	case WLAN_EID_EXT_HE_OPERATION:
944 		if (len >= sizeof(*elems->he_operation) &&
945 		    len >= ieee80211_he_oper_size(data) - 1) {
946 			if (crc)
947 				*crc = crc32_be(*crc, (void *)elem,
948 						elem->datalen + 2);
949 			elems->he_operation = data;
950 		}
951 		break;
952 	case WLAN_EID_EXT_UORA:
953 		if (len >= 1)
954 			elems->uora_element = data;
955 		break;
956 	case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
957 		if (len == 3)
958 			elems->max_channel_switch_time = data;
959 		break;
960 	case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
961 		if (len >= sizeof(*elems->mbssid_config_ie))
962 			elems->mbssid_config_ie = data;
963 		break;
964 	case WLAN_EID_EXT_HE_SPR:
965 		if (len >= sizeof(*elems->he_spr) &&
966 		    len >= ieee80211_he_spr_size(data))
967 			elems->he_spr = data;
968 		break;
969 	case WLAN_EID_EXT_HE_6GHZ_CAPA:
970 		if (len >= sizeof(*elems->he_6ghz_capa))
971 			elems->he_6ghz_capa = data;
972 		break;
973 	case WLAN_EID_EXT_EHT_CAPABILITY:
974 		if (ieee80211_eht_capa_size_ok(elems->he_cap,
975 					       data, len,
976 					       params->from_ap)) {
977 			elems->eht_cap = data;
978 			elems->eht_cap_len = len;
979 		}
980 		break;
981 	case WLAN_EID_EXT_EHT_OPERATION:
982 		if (ieee80211_eht_oper_size_ok(data, len))
983 			elems->eht_operation = data;
984 		break;
985 	case WLAN_EID_EXT_EHT_MULTI_LINK:
986 		if (ieee80211_mle_size_ok(data, len)) {
987 			elems->multi_link = (void *)data;
988 			elems->multi_link_len = len;
989 		}
990 		break;
991 	}
992 }
993 
994 static u32
995 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
996 			     struct ieee802_11_elems *elems,
997 			     const struct element *check_inherit)
998 {
999 	const struct element *elem;
1000 	bool calc_crc = params->filter != 0;
1001 	DECLARE_BITMAP(seen_elems, 256);
1002 	u32 crc = params->crc;
1003 	const u8 *ie;
1004 
1005 	bitmap_zero(seen_elems, 256);
1006 
1007 	for_each_element(elem, params->start, params->len) {
1008 		bool elem_parse_failed;
1009 		u8 id = elem->id;
1010 		u8 elen = elem->datalen;
1011 		const u8 *pos = elem->data;
1012 
1013 		if (check_inherit &&
1014 		    !cfg80211_is_element_inherited(elem,
1015 						   check_inherit))
1016 			continue;
1017 
1018 		switch (id) {
1019 		case WLAN_EID_SSID:
1020 		case WLAN_EID_SUPP_RATES:
1021 		case WLAN_EID_FH_PARAMS:
1022 		case WLAN_EID_DS_PARAMS:
1023 		case WLAN_EID_CF_PARAMS:
1024 		case WLAN_EID_TIM:
1025 		case WLAN_EID_IBSS_PARAMS:
1026 		case WLAN_EID_CHALLENGE:
1027 		case WLAN_EID_RSN:
1028 		case WLAN_EID_ERP_INFO:
1029 		case WLAN_EID_EXT_SUPP_RATES:
1030 		case WLAN_EID_HT_CAPABILITY:
1031 		case WLAN_EID_HT_OPERATION:
1032 		case WLAN_EID_VHT_CAPABILITY:
1033 		case WLAN_EID_VHT_OPERATION:
1034 		case WLAN_EID_MESH_ID:
1035 		case WLAN_EID_MESH_CONFIG:
1036 		case WLAN_EID_PEER_MGMT:
1037 		case WLAN_EID_PREQ:
1038 		case WLAN_EID_PREP:
1039 		case WLAN_EID_PERR:
1040 		case WLAN_EID_RANN:
1041 		case WLAN_EID_CHANNEL_SWITCH:
1042 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1043 		case WLAN_EID_COUNTRY:
1044 		case WLAN_EID_PWR_CONSTRAINT:
1045 		case WLAN_EID_TIMEOUT_INTERVAL:
1046 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1047 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1048 		case WLAN_EID_CHAN_SWITCH_PARAM:
1049 		case WLAN_EID_EXT_CAPABILITY:
1050 		case WLAN_EID_CHAN_SWITCH_TIMING:
1051 		case WLAN_EID_LINK_ID:
1052 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1053 		case WLAN_EID_RSNX:
1054 		case WLAN_EID_S1G_BCN_COMPAT:
1055 		case WLAN_EID_S1G_CAPABILITIES:
1056 		case WLAN_EID_S1G_OPERATION:
1057 		case WLAN_EID_AID_RESPONSE:
1058 		case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
1059 		/*
1060 		 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1061 		 * that if the content gets bigger it might be needed more than once
1062 		 */
1063 			if (test_bit(id, seen_elems)) {
1064 				elems->parse_error = true;
1065 				continue;
1066 			}
1067 			break;
1068 		}
1069 
1070 		if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
1071 			crc = crc32_be(crc, pos - 2, elen + 2);
1072 
1073 		elem_parse_failed = false;
1074 
1075 		switch (id) {
1076 		case WLAN_EID_LINK_ID:
1077 			if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
1078 				elem_parse_failed = true;
1079 				break;
1080 			}
1081 			elems->lnk_id = (void *)(pos - 2);
1082 			break;
1083 		case WLAN_EID_CHAN_SWITCH_TIMING:
1084 			if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
1085 				elem_parse_failed = true;
1086 				break;
1087 			}
1088 			elems->ch_sw_timing = (void *)pos;
1089 			break;
1090 		case WLAN_EID_EXT_CAPABILITY:
1091 			elems->ext_capab = pos;
1092 			elems->ext_capab_len = elen;
1093 			break;
1094 		case WLAN_EID_SSID:
1095 			elems->ssid = pos;
1096 			elems->ssid_len = elen;
1097 			break;
1098 		case WLAN_EID_SUPP_RATES:
1099 			elems->supp_rates = pos;
1100 			elems->supp_rates_len = elen;
1101 			break;
1102 		case WLAN_EID_DS_PARAMS:
1103 			if (elen >= 1)
1104 				elems->ds_params = pos;
1105 			else
1106 				elem_parse_failed = true;
1107 			break;
1108 		case WLAN_EID_TIM:
1109 			if (elen >= sizeof(struct ieee80211_tim_ie)) {
1110 				elems->tim = (void *)pos;
1111 				elems->tim_len = elen;
1112 			} else
1113 				elem_parse_failed = true;
1114 			break;
1115 		case WLAN_EID_VENDOR_SPECIFIC:
1116 			if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1117 			    pos[2] == 0xf2) {
1118 				/* Microsoft OUI (00:50:F2) */
1119 
1120 				if (calc_crc)
1121 					crc = crc32_be(crc, pos - 2, elen + 2);
1122 
1123 				if (elen >= 5 && pos[3] == 2) {
1124 					/* OUI Type 2 - WMM IE */
1125 					if (pos[4] == 0) {
1126 						elems->wmm_info = pos;
1127 						elems->wmm_info_len = elen;
1128 					} else if (pos[4] == 1) {
1129 						elems->wmm_param = pos;
1130 						elems->wmm_param_len = elen;
1131 					}
1132 				}
1133 			}
1134 			break;
1135 		case WLAN_EID_RSN:
1136 			elems->rsn = pos;
1137 			elems->rsn_len = elen;
1138 			break;
1139 		case WLAN_EID_ERP_INFO:
1140 			if (elen >= 1)
1141 				elems->erp_info = pos;
1142 			else
1143 				elem_parse_failed = true;
1144 			break;
1145 		case WLAN_EID_EXT_SUPP_RATES:
1146 			elems->ext_supp_rates = pos;
1147 			elems->ext_supp_rates_len = elen;
1148 			break;
1149 		case WLAN_EID_HT_CAPABILITY:
1150 			if (elen >= sizeof(struct ieee80211_ht_cap))
1151 				elems->ht_cap_elem = (void *)pos;
1152 			else
1153 				elem_parse_failed = true;
1154 			break;
1155 		case WLAN_EID_HT_OPERATION:
1156 			if (elen >= sizeof(struct ieee80211_ht_operation))
1157 				elems->ht_operation = (void *)pos;
1158 			else
1159 				elem_parse_failed = true;
1160 			break;
1161 		case WLAN_EID_VHT_CAPABILITY:
1162 			if (elen >= sizeof(struct ieee80211_vht_cap))
1163 				elems->vht_cap_elem = (void *)pos;
1164 			else
1165 				elem_parse_failed = true;
1166 			break;
1167 		case WLAN_EID_VHT_OPERATION:
1168 			if (elen >= sizeof(struct ieee80211_vht_operation)) {
1169 				elems->vht_operation = (void *)pos;
1170 				if (calc_crc)
1171 					crc = crc32_be(crc, pos - 2, elen + 2);
1172 				break;
1173 			}
1174 			elem_parse_failed = true;
1175 			break;
1176 		case WLAN_EID_OPMODE_NOTIF:
1177 			if (elen > 0) {
1178 				elems->opmode_notif = pos;
1179 				if (calc_crc)
1180 					crc = crc32_be(crc, pos - 2, elen + 2);
1181 				break;
1182 			}
1183 			elem_parse_failed = true;
1184 			break;
1185 		case WLAN_EID_MESH_ID:
1186 			elems->mesh_id = pos;
1187 			elems->mesh_id_len = elen;
1188 			break;
1189 		case WLAN_EID_MESH_CONFIG:
1190 			if (elen >= sizeof(struct ieee80211_meshconf_ie))
1191 				elems->mesh_config = (void *)pos;
1192 			else
1193 				elem_parse_failed = true;
1194 			break;
1195 		case WLAN_EID_PEER_MGMT:
1196 			elems->peering = pos;
1197 			elems->peering_len = elen;
1198 			break;
1199 		case WLAN_EID_MESH_AWAKE_WINDOW:
1200 			if (elen >= 2)
1201 				elems->awake_window = (void *)pos;
1202 			break;
1203 		case WLAN_EID_PREQ:
1204 			elems->preq = pos;
1205 			elems->preq_len = elen;
1206 			break;
1207 		case WLAN_EID_PREP:
1208 			elems->prep = pos;
1209 			elems->prep_len = elen;
1210 			break;
1211 		case WLAN_EID_PERR:
1212 			elems->perr = pos;
1213 			elems->perr_len = elen;
1214 			break;
1215 		case WLAN_EID_RANN:
1216 			if (elen >= sizeof(struct ieee80211_rann_ie))
1217 				elems->rann = (void *)pos;
1218 			else
1219 				elem_parse_failed = true;
1220 			break;
1221 		case WLAN_EID_CHANNEL_SWITCH:
1222 			if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
1223 				elem_parse_failed = true;
1224 				break;
1225 			}
1226 			elems->ch_switch_ie = (void *)pos;
1227 			break;
1228 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1229 			if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1230 				elem_parse_failed = true;
1231 				break;
1232 			}
1233 			elems->ext_chansw_ie = (void *)pos;
1234 			break;
1235 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1236 			if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1237 				elem_parse_failed = true;
1238 				break;
1239 			}
1240 			elems->sec_chan_offs = (void *)pos;
1241 			break;
1242 		case WLAN_EID_CHAN_SWITCH_PARAM:
1243 			if (elen <
1244 			    sizeof(*elems->mesh_chansw_params_ie)) {
1245 				elem_parse_failed = true;
1246 				break;
1247 			}
1248 			elems->mesh_chansw_params_ie = (void *)pos;
1249 			break;
1250 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1251 			if (!params->action ||
1252 			    elen < sizeof(*elems->wide_bw_chansw_ie)) {
1253 				elem_parse_failed = true;
1254 				break;
1255 			}
1256 			elems->wide_bw_chansw_ie = (void *)pos;
1257 			break;
1258 		case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1259 			if (params->action) {
1260 				elem_parse_failed = true;
1261 				break;
1262 			}
1263 			/*
1264 			 * This is a bit tricky, but as we only care about
1265 			 * the wide bandwidth channel switch element, so
1266 			 * just parse it out manually.
1267 			 */
1268 			ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1269 					      pos, elen);
1270 			if (ie) {
1271 				if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
1272 					elems->wide_bw_chansw_ie =
1273 						(void *)(ie + 2);
1274 				else
1275 					elem_parse_failed = true;
1276 			}
1277 			break;
1278 		case WLAN_EID_COUNTRY:
1279 			elems->country_elem = pos;
1280 			elems->country_elem_len = elen;
1281 			break;
1282 		case WLAN_EID_PWR_CONSTRAINT:
1283 			if (elen != 1) {
1284 				elem_parse_failed = true;
1285 				break;
1286 			}
1287 			elems->pwr_constr_elem = pos;
1288 			break;
1289 		case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1290 			/* Lots of different options exist, but we only care
1291 			 * about the Dynamic Transmit Power Control element.
1292 			 * First check for the Cisco OUI, then for the DTPC
1293 			 * tag (0x00).
1294 			 */
1295 			if (elen < 4) {
1296 				elem_parse_failed = true;
1297 				break;
1298 			}
1299 
1300 			if (pos[0] != 0x00 || pos[1] != 0x40 ||
1301 			    pos[2] != 0x96 || pos[3] != 0x00)
1302 				break;
1303 
1304 			if (elen != 6) {
1305 				elem_parse_failed = true;
1306 				break;
1307 			}
1308 
1309 			if (calc_crc)
1310 				crc = crc32_be(crc, pos - 2, elen + 2);
1311 
1312 			elems->cisco_dtpc_elem = pos;
1313 			break;
1314 		case WLAN_EID_ADDBA_EXT:
1315 			if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
1316 				elem_parse_failed = true;
1317 				break;
1318 			}
1319 			elems->addba_ext_ie = (void *)pos;
1320 			break;
1321 		case WLAN_EID_TIMEOUT_INTERVAL:
1322 			if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1323 				elems->timeout_int = (void *)pos;
1324 			else
1325 				elem_parse_failed = true;
1326 			break;
1327 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1328 			if (elen >= sizeof(*elems->max_idle_period_ie))
1329 				elems->max_idle_period_ie = (void *)pos;
1330 			break;
1331 		case WLAN_EID_RSNX:
1332 			elems->rsnx = pos;
1333 			elems->rsnx_len = elen;
1334 			break;
1335 		case WLAN_EID_TX_POWER_ENVELOPE:
1336 			if (elen < 1 ||
1337 			    elen > sizeof(struct ieee80211_tx_pwr_env))
1338 				break;
1339 
1340 			if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
1341 				break;
1342 
1343 			elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
1344 			elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
1345 			elems->tx_pwr_env_num++;
1346 			break;
1347 		case WLAN_EID_EXTENSION:
1348 			ieee80211_parse_extension_element(calc_crc ?
1349 								&crc : NULL,
1350 							  elem, elems, params);
1351 			break;
1352 		case WLAN_EID_S1G_CAPABILITIES:
1353 			if (elen >= sizeof(*elems->s1g_capab))
1354 				elems->s1g_capab = (void *)pos;
1355 			else
1356 				elem_parse_failed = true;
1357 			break;
1358 		case WLAN_EID_S1G_OPERATION:
1359 			if (elen == sizeof(*elems->s1g_oper))
1360 				elems->s1g_oper = (void *)pos;
1361 			else
1362 				elem_parse_failed = true;
1363 			break;
1364 		case WLAN_EID_S1G_BCN_COMPAT:
1365 			if (elen == sizeof(*elems->s1g_bcn_compat))
1366 				elems->s1g_bcn_compat = (void *)pos;
1367 			else
1368 				elem_parse_failed = true;
1369 			break;
1370 		case WLAN_EID_AID_RESPONSE:
1371 			if (elen == sizeof(struct ieee80211_aid_response_ie))
1372 				elems->aid_resp = (void *)pos;
1373 			else
1374 				elem_parse_failed = true;
1375 			break;
1376 		default:
1377 			break;
1378 		}
1379 
1380 		if (elem_parse_failed)
1381 			elems->parse_error = true;
1382 		else
1383 			__set_bit(id, seen_elems);
1384 	}
1385 
1386 	if (!for_each_element_completed(elem, params->start, params->len))
1387 		elems->parse_error = true;
1388 
1389 	return crc;
1390 }
1391 
1392 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1393 					    struct ieee802_11_elems *elems,
1394 					    struct cfg80211_bss *bss,
1395 					    u8 *nontransmitted_profile)
1396 {
1397 	const struct element *elem, *sub;
1398 	size_t profile_len = 0;
1399 	bool found = false;
1400 
1401 	if (!bss || !bss->transmitted_bss)
1402 		return profile_len;
1403 
1404 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1405 		if (elem->datalen < 2)
1406 			continue;
1407 		if (elem->data[0] < 1 || elem->data[0] > 8)
1408 			continue;
1409 
1410 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1411 			u8 new_bssid[ETH_ALEN];
1412 			const u8 *index;
1413 
1414 			if (sub->id != 0 || sub->datalen < 4) {
1415 				/* not a valid BSS profile */
1416 				continue;
1417 			}
1418 
1419 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1420 			    sub->data[1] != 2) {
1421 				/* The first element of the
1422 				 * Nontransmitted BSSID Profile is not
1423 				 * the Nontransmitted BSSID Capability
1424 				 * element.
1425 				 */
1426 				continue;
1427 			}
1428 
1429 			memset(nontransmitted_profile, 0, len);
1430 			profile_len = cfg80211_merge_profile(start, len,
1431 							     elem,
1432 							     sub,
1433 							     nontransmitted_profile,
1434 							     len);
1435 
1436 			/* found a Nontransmitted BSSID Profile */
1437 			index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
1438 						 nontransmitted_profile,
1439 						 profile_len);
1440 			if (!index || index[1] < 1 || index[2] == 0) {
1441 				/* Invalid MBSSID Index element */
1442 				continue;
1443 			}
1444 
1445 			cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
1446 					       elem->data[0],
1447 					       index[2],
1448 					       new_bssid);
1449 			if (ether_addr_equal(new_bssid, bss->bssid)) {
1450 				found = true;
1451 				elems->bssid_index_len = index[1];
1452 				elems->bssid_index = (void *)&index[2];
1453 				break;
1454 			}
1455 		}
1456 	}
1457 
1458 	return found ? profile_len : 0;
1459 }
1460 
1461 static void ieee80211_defragment_element(struct ieee802_11_elems *elems,
1462 					 void **elem_ptr, size_t *len,
1463 					 size_t total_len, u8 frag_id)
1464 {
1465 	u8 *data = *elem_ptr, *pos, *start;
1466 	const struct element *elem;
1467 
1468 	/*
1469 	 * Since 'data' points to the data of the element, not the element
1470 	 * itself, allow 254 in case it was an extended element where the
1471 	 * extended ID isn't part of the data we see here and thus not part of
1472 	 * 'len' either.
1473 	 */
1474 	if (!data || (*len != 254 && *len != 255))
1475 		return;
1476 
1477 	start = elems->scratch_pos;
1478 
1479 	if (WARN_ON(*len > (elems->scratch + elems->scratch_len -
1480 			    elems->scratch_pos)))
1481 		return;
1482 
1483 	memcpy(elems->scratch_pos, data, *len);
1484 	elems->scratch_pos += *len;
1485 
1486 	pos = data + *len;
1487 	total_len -= *len;
1488 	for_each_element(elem, pos, total_len) {
1489 		if (elem->id != frag_id)
1490 			break;
1491 
1492 		if (WARN_ON(elem->datalen >
1493 			    (elems->scratch + elems->scratch_len -
1494 			     elems->scratch_pos)))
1495 			return;
1496 
1497 		memcpy(elems->scratch_pos, elem->data, elem->datalen);
1498 		elems->scratch_pos += elem->datalen;
1499 
1500 		*len += elem->datalen;
1501 	}
1502 
1503 	*elem_ptr = start;
1504 }
1505 
1506 static void ieee80211_mle_get_sta_prof(struct ieee802_11_elems *elems,
1507 				       u8 link_id)
1508 {
1509 	const struct ieee80211_multi_link_elem *ml = elems->multi_link;
1510 	size_t ml_len = elems->multi_link_len;
1511 	const struct element *sub;
1512 
1513 	if (!ml || !ml_len)
1514 		return;
1515 
1516 	if (le16_get_bits(ml->control, IEEE80211_ML_CONTROL_TYPE) !=
1517 	    IEEE80211_ML_CONTROL_TYPE_BASIC)
1518 		return;
1519 
1520 	for_each_mle_subelement(sub, (u8 *)ml, ml_len) {
1521 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
1522 		u16 control;
1523 
1524 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
1525 			continue;
1526 
1527 		if (!ieee80211_mle_sta_prof_size_ok(sub->data, sub->datalen))
1528 			return;
1529 
1530 		control = le16_to_cpu(prof->control);
1531 
1532 		if (link_id != u16_get_bits(control,
1533 					    IEEE80211_MLE_STA_CONTROL_LINK_ID))
1534 			continue;
1535 
1536 		if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
1537 			return;
1538 
1539 		elems->prof = prof;
1540 		elems->sta_prof_len = sub->datalen;
1541 
1542 		/* the sub element can be fragmented */
1543 		ieee80211_defragment_element(elems, (void **)&elems->prof,
1544 					     &elems->sta_prof_len,
1545 					     ml_len - (sub->data - (u8 *)ml),
1546 					     IEEE80211_MLE_SUBELEM_FRAGMENT);
1547 		return;
1548 	}
1549 }
1550 
1551 static void ieee80211_mle_parse_link(struct ieee802_11_elems *elems,
1552 				     struct ieee80211_elems_parse_params *params)
1553 {
1554 	struct ieee80211_mle_per_sta_profile *prof;
1555 	struct ieee80211_elems_parse_params sub = {
1556 		.action = params->action,
1557 		.from_ap = params->from_ap,
1558 		.link_id = -1,
1559 	};
1560 	const struct element *non_inherit = NULL;
1561 	const u8 *end;
1562 
1563 	if (params->link_id == -1)
1564 		return;
1565 
1566 	ieee80211_defragment_element(elems, (void **)&elems->multi_link,
1567 				     &elems->multi_link_len,
1568 				     elems->total_len - ((u8 *)elems->multi_link -
1569 							 elems->ie_start),
1570 				     WLAN_EID_FRAGMENT);
1571 
1572 	ieee80211_mle_get_sta_prof(elems, params->link_id);
1573 	prof = elems->prof;
1574 
1575 	if (!prof)
1576 		return;
1577 
1578 	/* check if we have the 4 bytes for the fixed part in assoc response */
1579 	if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) {
1580 		elems->prof = NULL;
1581 		elems->sta_prof_len = 0;
1582 		return;
1583 	}
1584 
1585 	/*
1586 	 * Skip the capability information and the status code that are expected
1587 	 * as part of the station profile in association response frames. Note
1588 	 * the -1 is because the 'sta_info_len' is accounted to as part of the
1589 	 * per-STA profile, but not part of the 'u8 variable[]' portion.
1590 	 */
1591 	sub.start = prof->variable + prof->sta_info_len - 1 + 4;
1592 	end = (const u8 *)prof + elems->sta_prof_len;
1593 	sub.len = end - sub.start;
1594 
1595 	non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1596 					     sub.start, sub.len);
1597 	_ieee802_11_parse_elems_full(&sub, elems, non_inherit);
1598 }
1599 
1600 struct ieee802_11_elems *
1601 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
1602 {
1603 	struct ieee802_11_elems *elems;
1604 	const struct element *non_inherit = NULL;
1605 	u8 *nontransmitted_profile;
1606 	int nontransmitted_profile_len = 0;
1607 	size_t scratch_len = params->scratch_len ?: 3 * params->len;
1608 
1609 	elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1610 	if (!elems)
1611 		return NULL;
1612 	elems->ie_start = params->start;
1613 	elems->total_len = params->len;
1614 	elems->scratch_len = scratch_len;
1615 	elems->scratch_pos = elems->scratch;
1616 
1617 	nontransmitted_profile = elems->scratch_pos;
1618 	nontransmitted_profile_len =
1619 		ieee802_11_find_bssid_profile(params->start, params->len,
1620 					      elems, params->bss,
1621 					      nontransmitted_profile);
1622 	elems->scratch_pos += nontransmitted_profile_len;
1623 	elems->scratch_len -= nontransmitted_profile_len;
1624 	non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1625 					     nontransmitted_profile,
1626 					     nontransmitted_profile_len);
1627 
1628 	elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
1629 
1630 	/* Override with nontransmitted profile, if found */
1631 	if (nontransmitted_profile_len) {
1632 		struct ieee80211_elems_parse_params sub = {
1633 			.start = nontransmitted_profile,
1634 			.len = nontransmitted_profile_len,
1635 			.action = params->action,
1636 			.link_id = params->link_id,
1637 		};
1638 
1639 		_ieee802_11_parse_elems_full(&sub, elems, NULL);
1640 	}
1641 
1642 	ieee80211_mle_parse_link(elems, params);
1643 
1644 	if (elems->tim && !elems->parse_error) {
1645 		const struct ieee80211_tim_ie *tim_ie = elems->tim;
1646 
1647 		elems->dtim_period = tim_ie->dtim_period;
1648 		elems->dtim_count = tim_ie->dtim_count;
1649 	}
1650 
1651 	/* Override DTIM period and count if needed */
1652 	if (elems->bssid_index &&
1653 	    elems->bssid_index_len >=
1654 	    offsetofend(struct ieee80211_bssid_index, dtim_period))
1655 		elems->dtim_period = elems->bssid_index->dtim_period;
1656 
1657 	if (elems->bssid_index &&
1658 	    elems->bssid_index_len >=
1659 	    offsetofend(struct ieee80211_bssid_index, dtim_count))
1660 		elems->dtim_count = elems->bssid_index->dtim_count;
1661 
1662 	return elems;
1663 }
1664 
1665 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1666 					   struct ieee80211_tx_queue_params
1667 					   *qparam, int ac)
1668 {
1669 	struct ieee80211_chanctx_conf *chanctx_conf;
1670 	const struct ieee80211_reg_rule *rrule;
1671 	const struct ieee80211_wmm_ac *wmm_ac;
1672 	u16 center_freq = 0;
1673 
1674 	if (sdata->vif.type != NL80211_IFTYPE_AP &&
1675 	    sdata->vif.type != NL80211_IFTYPE_STATION)
1676 		return;
1677 
1678 	rcu_read_lock();
1679 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1680 	if (chanctx_conf)
1681 		center_freq = chanctx_conf->def.chan->center_freq;
1682 
1683 	if (!center_freq) {
1684 		rcu_read_unlock();
1685 		return;
1686 	}
1687 
1688 	rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1689 
1690 	if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1691 		rcu_read_unlock();
1692 		return;
1693 	}
1694 
1695 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1696 		wmm_ac = &rrule->wmm_rule.ap[ac];
1697 	else
1698 		wmm_ac = &rrule->wmm_rule.client[ac];
1699 	qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1700 	qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1701 	qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1702 	qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1703 	rcu_read_unlock();
1704 }
1705 
1706 void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
1707 			       bool bss_notify, bool enable_qos)
1708 {
1709 	struct ieee80211_sub_if_data *sdata = link->sdata;
1710 	struct ieee80211_local *local = sdata->local;
1711 	struct ieee80211_tx_queue_params qparam;
1712 	struct ieee80211_chanctx_conf *chanctx_conf;
1713 	int ac;
1714 	bool use_11b;
1715 	bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1716 	int aCWmin, aCWmax;
1717 
1718 	if (!local->ops->conf_tx)
1719 		return;
1720 
1721 	if (local->hw.queues < IEEE80211_NUM_ACS)
1722 		return;
1723 
1724 	memset(&qparam, 0, sizeof(qparam));
1725 
1726 	rcu_read_lock();
1727 	chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
1728 	use_11b = (chanctx_conf &&
1729 		   chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1730 		 !link->operating_11g_mode;
1731 	rcu_read_unlock();
1732 
1733 	is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1734 
1735 	/* Set defaults according to 802.11-2007 Table 7-37 */
1736 	aCWmax = 1023;
1737 	if (use_11b)
1738 		aCWmin = 31;
1739 	else
1740 		aCWmin = 15;
1741 
1742 	/* Confiure old 802.11b/g medium access rules. */
1743 	qparam.cw_max = aCWmax;
1744 	qparam.cw_min = aCWmin;
1745 	qparam.txop = 0;
1746 	qparam.aifs = 2;
1747 
1748 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1749 		/* Update if QoS is enabled. */
1750 		if (enable_qos) {
1751 			switch (ac) {
1752 			case IEEE80211_AC_BK:
1753 				qparam.cw_max = aCWmax;
1754 				qparam.cw_min = aCWmin;
1755 				qparam.txop = 0;
1756 				if (is_ocb)
1757 					qparam.aifs = 9;
1758 				else
1759 					qparam.aifs = 7;
1760 				break;
1761 			/* never happens but let's not leave undefined */
1762 			default:
1763 			case IEEE80211_AC_BE:
1764 				qparam.cw_max = aCWmax;
1765 				qparam.cw_min = aCWmin;
1766 				qparam.txop = 0;
1767 				if (is_ocb)
1768 					qparam.aifs = 6;
1769 				else
1770 					qparam.aifs = 3;
1771 				break;
1772 			case IEEE80211_AC_VI:
1773 				qparam.cw_max = aCWmin;
1774 				qparam.cw_min = (aCWmin + 1) / 2 - 1;
1775 				if (is_ocb)
1776 					qparam.txop = 0;
1777 				else if (use_11b)
1778 					qparam.txop = 6016/32;
1779 				else
1780 					qparam.txop = 3008/32;
1781 
1782 				if (is_ocb)
1783 					qparam.aifs = 3;
1784 				else
1785 					qparam.aifs = 2;
1786 				break;
1787 			case IEEE80211_AC_VO:
1788 				qparam.cw_max = (aCWmin + 1) / 2 - 1;
1789 				qparam.cw_min = (aCWmin + 1) / 4 - 1;
1790 				if (is_ocb)
1791 					qparam.txop = 0;
1792 				else if (use_11b)
1793 					qparam.txop = 3264/32;
1794 				else
1795 					qparam.txop = 1504/32;
1796 				qparam.aifs = 2;
1797 				break;
1798 			}
1799 		}
1800 		ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
1801 
1802 		qparam.uapsd = false;
1803 
1804 		link->tx_conf[ac] = qparam;
1805 		drv_conf_tx(local, link, ac, &qparam);
1806 	}
1807 
1808 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1809 	    sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1810 	    sdata->vif.type != NL80211_IFTYPE_NAN) {
1811 		link->conf->qos = enable_qos;
1812 		if (bss_notify)
1813 			ieee80211_link_info_change_notify(sdata, link,
1814 							  BSS_CHANGED_QOS);
1815 	}
1816 }
1817 
1818 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1819 			 u16 transaction, u16 auth_alg, u16 status,
1820 			 const u8 *extra, size_t extra_len, const u8 *da,
1821 			 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1822 			 u32 tx_flags)
1823 {
1824 	struct ieee80211_local *local = sdata->local;
1825 	struct sk_buff *skb;
1826 	struct ieee80211_mgmt *mgmt;
1827 	bool multi_link = sdata->vif.valid_links;
1828 	struct {
1829 		u8 id;
1830 		u8 len;
1831 		u8 ext_id;
1832 		struct ieee80211_multi_link_elem ml;
1833 		struct ieee80211_mle_basic_common_info basic;
1834 	} __packed mle = {
1835 		.id = WLAN_EID_EXTENSION,
1836 		.len = sizeof(mle) - 2,
1837 		.ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
1838 		.ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
1839 		.basic.len = sizeof(mle.basic),
1840 	};
1841 	int err;
1842 
1843 	memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1844 
1845 	/* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1846 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1847 			    24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
1848 			    multi_link * sizeof(mle));
1849 	if (!skb)
1850 		return;
1851 
1852 	skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
1853 
1854 	mgmt = skb_put_zero(skb, 24 + 6);
1855 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1856 					  IEEE80211_STYPE_AUTH);
1857 	memcpy(mgmt->da, da, ETH_ALEN);
1858 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1859 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
1860 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1861 	mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1862 	mgmt->u.auth.status_code = cpu_to_le16(status);
1863 	if (extra)
1864 		skb_put_data(skb, extra, extra_len);
1865 	if (multi_link)
1866 		skb_put_data(skb, &mle, sizeof(mle));
1867 
1868 	if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1869 		mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1870 		err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1871 		if (WARN_ON(err)) {
1872 			kfree_skb(skb);
1873 			return;
1874 		}
1875 	}
1876 
1877 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1878 					tx_flags;
1879 	ieee80211_tx_skb(sdata, skb);
1880 }
1881 
1882 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1883 				    const u8 *da, const u8 *bssid,
1884 				    u16 stype, u16 reason,
1885 				    bool send_frame, u8 *frame_buf)
1886 {
1887 	struct ieee80211_local *local = sdata->local;
1888 	struct sk_buff *skb;
1889 	struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1890 
1891 	/* build frame */
1892 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1893 	mgmt->duration = 0; /* initialize only */
1894 	mgmt->seq_ctrl = 0; /* initialize only */
1895 	memcpy(mgmt->da, da, ETH_ALEN);
1896 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1897 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
1898 	/* u.deauth.reason_code == u.disassoc.reason_code */
1899 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1900 
1901 	if (send_frame) {
1902 		skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1903 				    IEEE80211_DEAUTH_FRAME_LEN);
1904 		if (!skb)
1905 			return;
1906 
1907 		skb_reserve(skb, local->hw.extra_tx_headroom);
1908 
1909 		/* copy in frame */
1910 		skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1911 
1912 		if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1913 		    !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1914 			IEEE80211_SKB_CB(skb)->flags |=
1915 				IEEE80211_TX_INTFL_DONT_ENCRYPT;
1916 
1917 		ieee80211_tx_skb(sdata, skb);
1918 	}
1919 }
1920 
1921 static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
1922 {
1923 	if ((end - pos) < 5)
1924 		return pos;
1925 
1926 	*pos++ = WLAN_EID_EXTENSION;
1927 	*pos++ = 1 + sizeof(cap);
1928 	*pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
1929 	memcpy(pos, &cap, sizeof(cap));
1930 
1931 	return pos + 2;
1932 }
1933 
1934 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
1935 					 u8 *buffer, size_t buffer_len,
1936 					 const u8 *ie, size_t ie_len,
1937 					 enum nl80211_band band,
1938 					 u32 rate_mask,
1939 					 struct cfg80211_chan_def *chandef,
1940 					 size_t *offset, u32 flags)
1941 {
1942 	struct ieee80211_local *local = sdata->local;
1943 	struct ieee80211_supported_band *sband;
1944 	const struct ieee80211_sta_he_cap *he_cap;
1945 	const struct ieee80211_sta_eht_cap *eht_cap;
1946 	u8 *pos = buffer, *end = buffer + buffer_len;
1947 	size_t noffset;
1948 	int supp_rates_len, i;
1949 	u8 rates[32];
1950 	int num_rates;
1951 	int ext_rates_len;
1952 	int shift;
1953 	u32 rate_flags;
1954 	bool have_80mhz = false;
1955 
1956 	*offset = 0;
1957 
1958 	sband = local->hw.wiphy->bands[band];
1959 	if (WARN_ON_ONCE(!sband))
1960 		return 0;
1961 
1962 	rate_flags = ieee80211_chandef_rate_flags(chandef);
1963 	shift = ieee80211_chandef_get_shift(chandef);
1964 
1965 	/* For direct scan add S1G IE and consider its override bits */
1966 	if (band == NL80211_BAND_S1GHZ) {
1967 		if (end - pos < 2 + sizeof(struct ieee80211_s1g_cap))
1968 			goto out_err;
1969 		pos = ieee80211_ie_build_s1g_cap(pos, &sband->s1g_cap);
1970 		goto done;
1971 	}
1972 
1973 	num_rates = 0;
1974 	for (i = 0; i < sband->n_bitrates; i++) {
1975 		if ((BIT(i) & rate_mask) == 0)
1976 			continue; /* skip rate */
1977 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1978 			continue;
1979 
1980 		rates[num_rates++] =
1981 			(u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1982 					  (1 << shift) * 5);
1983 	}
1984 
1985 	supp_rates_len = min_t(int, num_rates, 8);
1986 
1987 	if (end - pos < 2 + supp_rates_len)
1988 		goto out_err;
1989 	*pos++ = WLAN_EID_SUPP_RATES;
1990 	*pos++ = supp_rates_len;
1991 	memcpy(pos, rates, supp_rates_len);
1992 	pos += supp_rates_len;
1993 
1994 	/* insert "request information" if in custom IEs */
1995 	if (ie && ie_len) {
1996 		static const u8 before_extrates[] = {
1997 			WLAN_EID_SSID,
1998 			WLAN_EID_SUPP_RATES,
1999 			WLAN_EID_REQUEST,
2000 		};
2001 		noffset = ieee80211_ie_split(ie, ie_len,
2002 					     before_extrates,
2003 					     ARRAY_SIZE(before_extrates),
2004 					     *offset);
2005 		if (end - pos < noffset - *offset)
2006 			goto out_err;
2007 		memcpy(pos, ie + *offset, noffset - *offset);
2008 		pos += noffset - *offset;
2009 		*offset = noffset;
2010 	}
2011 
2012 	ext_rates_len = num_rates - supp_rates_len;
2013 	if (ext_rates_len > 0) {
2014 		if (end - pos < 2 + ext_rates_len)
2015 			goto out_err;
2016 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
2017 		*pos++ = ext_rates_len;
2018 		memcpy(pos, rates + supp_rates_len, ext_rates_len);
2019 		pos += ext_rates_len;
2020 	}
2021 
2022 	if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
2023 		if (end - pos < 3)
2024 			goto out_err;
2025 		*pos++ = WLAN_EID_DS_PARAMS;
2026 		*pos++ = 1;
2027 		*pos++ = ieee80211_frequency_to_channel(
2028 				chandef->chan->center_freq);
2029 	}
2030 
2031 	if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
2032 		goto done;
2033 
2034 	/* insert custom IEs that go before HT */
2035 	if (ie && ie_len) {
2036 		static const u8 before_ht[] = {
2037 			/*
2038 			 * no need to list the ones split off already
2039 			 * (or generated here)
2040 			 */
2041 			WLAN_EID_DS_PARAMS,
2042 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2043 		};
2044 		noffset = ieee80211_ie_split(ie, ie_len,
2045 					     before_ht, ARRAY_SIZE(before_ht),
2046 					     *offset);
2047 		if (end - pos < noffset - *offset)
2048 			goto out_err;
2049 		memcpy(pos, ie + *offset, noffset - *offset);
2050 		pos += noffset - *offset;
2051 		*offset = noffset;
2052 	}
2053 
2054 	if (sband->ht_cap.ht_supported) {
2055 		if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
2056 			goto out_err;
2057 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
2058 						sband->ht_cap.cap);
2059 	}
2060 
2061 	/* insert custom IEs that go before VHT */
2062 	if (ie && ie_len) {
2063 		static const u8 before_vht[] = {
2064 			/*
2065 			 * no need to list the ones split off already
2066 			 * (or generated here)
2067 			 */
2068 			WLAN_EID_BSS_COEX_2040,
2069 			WLAN_EID_EXT_CAPABILITY,
2070 			WLAN_EID_SSID_LIST,
2071 			WLAN_EID_CHANNEL_USAGE,
2072 			WLAN_EID_INTERWORKING,
2073 			WLAN_EID_MESH_ID,
2074 			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
2075 		};
2076 		noffset = ieee80211_ie_split(ie, ie_len,
2077 					     before_vht, ARRAY_SIZE(before_vht),
2078 					     *offset);
2079 		if (end - pos < noffset - *offset)
2080 			goto out_err;
2081 		memcpy(pos, ie + *offset, noffset - *offset);
2082 		pos += noffset - *offset;
2083 		*offset = noffset;
2084 	}
2085 
2086 	/* Check if any channel in this sband supports at least 80 MHz */
2087 	for (i = 0; i < sband->n_channels; i++) {
2088 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
2089 						IEEE80211_CHAN_NO_80MHZ))
2090 			continue;
2091 
2092 		have_80mhz = true;
2093 		break;
2094 	}
2095 
2096 	if (sband->vht_cap.vht_supported && have_80mhz) {
2097 		if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
2098 			goto out_err;
2099 		pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
2100 						 sband->vht_cap.cap);
2101 	}
2102 
2103 	/* insert custom IEs that go before HE */
2104 	if (ie && ie_len) {
2105 		static const u8 before_he[] = {
2106 			/*
2107 			 * no need to list the ones split off before VHT
2108 			 * or generated here
2109 			 */
2110 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
2111 			WLAN_EID_AP_CSN,
2112 			/* TODO: add 11ah/11aj/11ak elements */
2113 		};
2114 		noffset = ieee80211_ie_split(ie, ie_len,
2115 					     before_he, ARRAY_SIZE(before_he),
2116 					     *offset);
2117 		if (end - pos < noffset - *offset)
2118 			goto out_err;
2119 		memcpy(pos, ie + *offset, noffset - *offset);
2120 		pos += noffset - *offset;
2121 		*offset = noffset;
2122 	}
2123 
2124 	he_cap = ieee80211_get_he_iftype_cap(sband,
2125 					     ieee80211_vif_type_p2p(&sdata->vif));
2126 	if (he_cap &&
2127 	    cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2128 					 IEEE80211_CHAN_NO_HE)) {
2129 		pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
2130 		if (!pos)
2131 			goto out_err;
2132 	}
2133 
2134 	eht_cap = ieee80211_get_eht_iftype_cap(sband,
2135 					       ieee80211_vif_type_p2p(&sdata->vif));
2136 
2137 	if (eht_cap &&
2138 	    cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2139 					 IEEE80211_CHAN_NO_HE |
2140 					 IEEE80211_CHAN_NO_EHT)) {
2141 		pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
2142 						 sdata->vif.type == NL80211_IFTYPE_AP);
2143 		if (!pos)
2144 			goto out_err;
2145 	}
2146 
2147 	if (cfg80211_any_usable_channels(local->hw.wiphy,
2148 					 BIT(NL80211_BAND_6GHZ),
2149 					 IEEE80211_CHAN_NO_HE)) {
2150 		struct ieee80211_supported_band *sband6;
2151 
2152 		sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2153 		he_cap = ieee80211_get_he_iftype_cap(sband6,
2154 				ieee80211_vif_type_p2p(&sdata->vif));
2155 
2156 		if (he_cap) {
2157 			enum nl80211_iftype iftype =
2158 				ieee80211_vif_type_p2p(&sdata->vif);
2159 			__le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
2160 
2161 			pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
2162 		}
2163 	}
2164 
2165 	/*
2166 	 * If adding more here, adjust code in main.c
2167 	 * that calculates local->scan_ies_len.
2168 	 */
2169 
2170 	return pos - buffer;
2171  out_err:
2172 	WARN_ONCE(1, "not enough space for preq IEs\n");
2173  done:
2174 	return pos - buffer;
2175 }
2176 
2177 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
2178 			     size_t buffer_len,
2179 			     struct ieee80211_scan_ies *ie_desc,
2180 			     const u8 *ie, size_t ie_len,
2181 			     u8 bands_used, u32 *rate_masks,
2182 			     struct cfg80211_chan_def *chandef,
2183 			     u32 flags)
2184 {
2185 	size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
2186 	int i;
2187 
2188 	memset(ie_desc, 0, sizeof(*ie_desc));
2189 
2190 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
2191 		if (bands_used & BIT(i)) {
2192 			pos += ieee80211_build_preq_ies_band(sdata,
2193 							     buffer + pos,
2194 							     buffer_len - pos,
2195 							     ie, ie_len, i,
2196 							     rate_masks[i],
2197 							     chandef,
2198 							     &custom_ie_offset,
2199 							     flags);
2200 			ie_desc->ies[i] = buffer + old_pos;
2201 			ie_desc->len[i] = pos - old_pos;
2202 			old_pos = pos;
2203 		}
2204 	}
2205 
2206 	/* add any remaining custom IEs */
2207 	if (ie && ie_len) {
2208 		if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
2209 			      "not enough space for preq custom IEs\n"))
2210 			return pos;
2211 		memcpy(buffer + pos, ie + custom_ie_offset,
2212 		       ie_len - custom_ie_offset);
2213 		ie_desc->common_ies = buffer + pos;
2214 		ie_desc->common_ie_len = ie_len - custom_ie_offset;
2215 		pos += ie_len - custom_ie_offset;
2216 	}
2217 
2218 	return pos;
2219 };
2220 
2221 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
2222 					  const u8 *src, const u8 *dst,
2223 					  u32 ratemask,
2224 					  struct ieee80211_channel *chan,
2225 					  const u8 *ssid, size_t ssid_len,
2226 					  const u8 *ie, size_t ie_len,
2227 					  u32 flags)
2228 {
2229 	struct ieee80211_local *local = sdata->local;
2230 	struct cfg80211_chan_def chandef;
2231 	struct sk_buff *skb;
2232 	struct ieee80211_mgmt *mgmt;
2233 	int ies_len;
2234 	u32 rate_masks[NUM_NL80211_BANDS] = {};
2235 	struct ieee80211_scan_ies dummy_ie_desc;
2236 
2237 	/*
2238 	 * Do not send DS Channel parameter for directed probe requests
2239 	 * in order to maximize the chance that we get a response.  Some
2240 	 * badly-behaved APs don't respond when this parameter is included.
2241 	 */
2242 	chandef.width = sdata->vif.bss_conf.chandef.width;
2243 	if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2244 		chandef.chan = NULL;
2245 	else
2246 		chandef.chan = chan;
2247 
2248 	skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
2249 				     local->scan_ies_len + ie_len);
2250 	if (!skb)
2251 		return NULL;
2252 
2253 	rate_masks[chan->band] = ratemask;
2254 	ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
2255 					   skb_tailroom(skb), &dummy_ie_desc,
2256 					   ie, ie_len, BIT(chan->band),
2257 					   rate_masks, &chandef, flags);
2258 	skb_put(skb, ies_len);
2259 
2260 	if (dst) {
2261 		mgmt = (struct ieee80211_mgmt *) skb->data;
2262 		memcpy(mgmt->da, dst, ETH_ALEN);
2263 		memcpy(mgmt->bssid, dst, ETH_ALEN);
2264 	}
2265 
2266 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2267 
2268 	return skb;
2269 }
2270 
2271 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
2272 			    struct ieee802_11_elems *elems,
2273 			    enum nl80211_band band, u32 *basic_rates)
2274 {
2275 	struct ieee80211_supported_band *sband;
2276 	size_t num_rates;
2277 	u32 supp_rates, rate_flags;
2278 	int i, j, shift;
2279 
2280 	sband = sdata->local->hw.wiphy->bands[band];
2281 	if (WARN_ON(!sband))
2282 		return 1;
2283 
2284 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2285 	shift = ieee80211_vif_get_shift(&sdata->vif);
2286 
2287 	num_rates = sband->n_bitrates;
2288 	supp_rates = 0;
2289 	for (i = 0; i < elems->supp_rates_len +
2290 		     elems->ext_supp_rates_len; i++) {
2291 		u8 rate = 0;
2292 		int own_rate;
2293 		bool is_basic;
2294 		if (i < elems->supp_rates_len)
2295 			rate = elems->supp_rates[i];
2296 		else if (elems->ext_supp_rates)
2297 			rate = elems->ext_supp_rates
2298 				[i - elems->supp_rates_len];
2299 		own_rate = 5 * (rate & 0x7f);
2300 		is_basic = !!(rate & 0x80);
2301 
2302 		if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2303 			continue;
2304 
2305 		for (j = 0; j < num_rates; j++) {
2306 			int brate;
2307 			if ((rate_flags & sband->bitrates[j].flags)
2308 			    != rate_flags)
2309 				continue;
2310 
2311 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
2312 					     1 << shift);
2313 
2314 			if (brate == own_rate) {
2315 				supp_rates |= BIT(j);
2316 				if (basic_rates && is_basic)
2317 					*basic_rates |= BIT(j);
2318 			}
2319 		}
2320 	}
2321 	return supp_rates;
2322 }
2323 
2324 void ieee80211_stop_device(struct ieee80211_local *local)
2325 {
2326 	ieee80211_led_radio(local, false);
2327 	ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
2328 
2329 	cancel_work_sync(&local->reconfig_filter);
2330 
2331 	flush_workqueue(local->workqueue);
2332 	drv_stop(local);
2333 }
2334 
2335 static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
2336 					   bool aborted)
2337 {
2338 	/* It's possible that we don't handle the scan completion in
2339 	 * time during suspend, so if it's still marked as completed
2340 	 * here, queue the work and flush it to clean things up.
2341 	 * Instead of calling the worker function directly here, we
2342 	 * really queue it to avoid potential races with other flows
2343 	 * scheduling the same work.
2344 	 */
2345 	if (test_bit(SCAN_COMPLETED, &local->scanning)) {
2346 		/* If coming from reconfiguration failure, abort the scan so
2347 		 * we don't attempt to continue a partial HW scan - which is
2348 		 * possible otherwise if (e.g.) the 2.4 GHz portion was the
2349 		 * completed scan, and a 5 GHz portion is still pending.
2350 		 */
2351 		if (aborted)
2352 			set_bit(SCAN_ABORTED, &local->scanning);
2353 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
2354 		flush_delayed_work(&local->scan_work);
2355 	}
2356 }
2357 
2358 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2359 {
2360 	struct ieee80211_sub_if_data *sdata;
2361 	struct ieee80211_chanctx *ctx;
2362 
2363 	/*
2364 	 * We get here if during resume the device can't be restarted properly.
2365 	 * We might also get here if this happens during HW reset, which is a
2366 	 * slightly different situation and we need to drop all connections in
2367 	 * the latter case.
2368 	 *
2369 	 * Ask cfg80211 to turn off all interfaces, this will result in more
2370 	 * warnings but at least we'll then get into a clean stopped state.
2371 	 */
2372 
2373 	local->resuming = false;
2374 	local->suspended = false;
2375 	local->in_reconfig = false;
2376 
2377 	ieee80211_flush_completed_scan(local, true);
2378 
2379 	/* scheduled scan clearly can't be running any more, but tell
2380 	 * cfg80211 and clear local state
2381 	 */
2382 	ieee80211_sched_scan_end(local);
2383 
2384 	list_for_each_entry(sdata, &local->interfaces, list)
2385 		sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2386 
2387 	/* Mark channel contexts as not being in the driver any more to avoid
2388 	 * removing them from the driver during the shutdown process...
2389 	 */
2390 	mutex_lock(&local->chanctx_mtx);
2391 	list_for_each_entry(ctx, &local->chanctx_list, list)
2392 		ctx->driver_present = false;
2393 	mutex_unlock(&local->chanctx_mtx);
2394 }
2395 
2396 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2397 				     struct ieee80211_sub_if_data *sdata,
2398 				     struct ieee80211_link_data *link)
2399 {
2400 	struct ieee80211_chanctx_conf *conf;
2401 	struct ieee80211_chanctx *ctx;
2402 
2403 	if (!local->use_chanctx)
2404 		return;
2405 
2406 	mutex_lock(&local->chanctx_mtx);
2407 	conf = rcu_dereference_protected(link->conf->chanctx_conf,
2408 					 lockdep_is_held(&local->chanctx_mtx));
2409 	if (conf) {
2410 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
2411 		drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
2412 	}
2413 	mutex_unlock(&local->chanctx_mtx);
2414 }
2415 
2416 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
2417 {
2418 	struct ieee80211_local *local = sdata->local;
2419 	struct sta_info *sta;
2420 
2421 	/* add STAs back */
2422 	mutex_lock(&local->sta_mtx);
2423 	list_for_each_entry(sta, &local->sta_list, list) {
2424 		enum ieee80211_sta_state state;
2425 
2426 		if (!sta->uploaded || sta->sdata != sdata)
2427 			continue;
2428 
2429 		for (state = IEEE80211_STA_NOTEXIST;
2430 		     state < sta->sta_state; state++)
2431 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2432 					      state + 1));
2433 	}
2434 	mutex_unlock(&local->sta_mtx);
2435 }
2436 
2437 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2438 {
2439 	struct cfg80211_nan_func *func, **funcs;
2440 	int res, id, i = 0;
2441 
2442 	res = drv_start_nan(sdata->local, sdata,
2443 			    &sdata->u.nan.conf);
2444 	if (WARN_ON(res))
2445 		return res;
2446 
2447 	funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
2448 			sizeof(*funcs),
2449 			GFP_KERNEL);
2450 	if (!funcs)
2451 		return -ENOMEM;
2452 
2453 	/* Add all the functions:
2454 	 * This is a little bit ugly. We need to call a potentially sleeping
2455 	 * callback for each NAN function, so we can't hold the spinlock.
2456 	 */
2457 	spin_lock_bh(&sdata->u.nan.func_lock);
2458 
2459 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2460 		funcs[i++] = func;
2461 
2462 	spin_unlock_bh(&sdata->u.nan.func_lock);
2463 
2464 	for (i = 0; funcs[i]; i++) {
2465 		res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2466 		if (WARN_ON(res))
2467 			ieee80211_nan_func_terminated(&sdata->vif,
2468 						      funcs[i]->instance_id,
2469 						      NL80211_NAN_FUNC_TERM_REASON_ERROR,
2470 						      GFP_KERNEL);
2471 	}
2472 
2473 	kfree(funcs);
2474 
2475 	return 0;
2476 }
2477 
2478 int ieee80211_reconfig(struct ieee80211_local *local)
2479 {
2480 	struct ieee80211_hw *hw = &local->hw;
2481 	struct ieee80211_sub_if_data *sdata;
2482 	struct ieee80211_chanctx *ctx;
2483 	struct sta_info *sta;
2484 	int res, i;
2485 	bool reconfig_due_to_wowlan = false;
2486 	struct ieee80211_sub_if_data *sched_scan_sdata;
2487 	struct cfg80211_sched_scan_request *sched_scan_req;
2488 	bool sched_scan_stopped = false;
2489 	bool suspended = local->suspended;
2490 	bool in_reconfig = false;
2491 
2492 	/* nothing to do if HW shouldn't run */
2493 	if (!local->open_count)
2494 		goto wake_up;
2495 
2496 #ifdef CONFIG_PM
2497 	if (suspended)
2498 		local->resuming = true;
2499 
2500 	if (local->wowlan) {
2501 		/*
2502 		 * In the wowlan case, both mac80211 and the device
2503 		 * are functional when the resume op is called, so
2504 		 * clear local->suspended so the device could operate
2505 		 * normally (e.g. pass rx frames).
2506 		 */
2507 		local->suspended = false;
2508 		res = drv_resume(local);
2509 		local->wowlan = false;
2510 		if (res < 0) {
2511 			local->resuming = false;
2512 			return res;
2513 		}
2514 		if (res == 0)
2515 			goto wake_up;
2516 		WARN_ON(res > 1);
2517 		/*
2518 		 * res is 1, which means the driver requested
2519 		 * to go through a regular reset on wakeup.
2520 		 * restore local->suspended in this case.
2521 		 */
2522 		reconfig_due_to_wowlan = true;
2523 		local->suspended = true;
2524 	}
2525 #endif
2526 
2527 	/*
2528 	 * In case of hw_restart during suspend (without wowlan),
2529 	 * cancel restart work, as we are reconfiguring the device
2530 	 * anyway.
2531 	 * Note that restart_work is scheduled on a frozen workqueue,
2532 	 * so we can't deadlock in this case.
2533 	 */
2534 	if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
2535 		cancel_work_sync(&local->restart_work);
2536 
2537 	local->started = false;
2538 
2539 	/*
2540 	 * Upon resume hardware can sometimes be goofy due to
2541 	 * various platform / driver / bus issues, so restarting
2542 	 * the device may at times not work immediately. Propagate
2543 	 * the error.
2544 	 */
2545 	res = drv_start(local);
2546 	if (res) {
2547 		if (suspended)
2548 			WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2549 		else
2550 			WARN(1, "Hardware became unavailable during restart.\n");
2551 		ieee80211_handle_reconfig_failure(local);
2552 		return res;
2553 	}
2554 
2555 	/* setup fragmentation threshold */
2556 	drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
2557 
2558 	/* setup RTS threshold */
2559 	drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
2560 
2561 	/* reset coverage class */
2562 	drv_set_coverage_class(local, hw->wiphy->coverage_class);
2563 
2564 	ieee80211_led_radio(local, true);
2565 	ieee80211_mod_tpt_led_trig(local,
2566 				   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2567 
2568 	/* add interfaces */
2569 	sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
2570 	if (sdata) {
2571 		/* in HW restart it exists already */
2572 		WARN_ON(local->resuming);
2573 		res = drv_add_interface(local, sdata);
2574 		if (WARN_ON(res)) {
2575 			RCU_INIT_POINTER(local->monitor_sdata, NULL);
2576 			synchronize_net();
2577 			kfree(sdata);
2578 		}
2579 	}
2580 
2581 	list_for_each_entry(sdata, &local->interfaces, list) {
2582 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2583 		    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2584 		    ieee80211_sdata_running(sdata)) {
2585 			res = drv_add_interface(local, sdata);
2586 			if (WARN_ON(res))
2587 				break;
2588 		}
2589 	}
2590 
2591 	/* If adding any of the interfaces failed above, roll back and
2592 	 * report failure.
2593 	 */
2594 	if (res) {
2595 		list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2596 						     list)
2597 			if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2598 			    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2599 			    ieee80211_sdata_running(sdata))
2600 				drv_remove_interface(local, sdata);
2601 		ieee80211_handle_reconfig_failure(local);
2602 		return res;
2603 	}
2604 
2605 	/* add channel contexts */
2606 	if (local->use_chanctx) {
2607 		mutex_lock(&local->chanctx_mtx);
2608 		list_for_each_entry(ctx, &local->chanctx_list, list)
2609 			if (ctx->replace_state !=
2610 			    IEEE80211_CHANCTX_REPLACES_OTHER)
2611 				WARN_ON(drv_add_chanctx(local, ctx));
2612 		mutex_unlock(&local->chanctx_mtx);
2613 
2614 		sdata = wiphy_dereference(local->hw.wiphy,
2615 					  local->monitor_sdata);
2616 		if (sdata && ieee80211_sdata_running(sdata))
2617 			ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
2618 	}
2619 
2620 	/* reconfigure hardware */
2621 	ieee80211_hw_config(local, ~0);
2622 
2623 	ieee80211_configure_filter(local);
2624 
2625 	/* Finally also reconfigure all the BSS information */
2626 	list_for_each_entry(sdata, &local->interfaces, list) {
2627 		unsigned int link_id;
2628 		u32 changed;
2629 
2630 		if (!ieee80211_sdata_running(sdata))
2631 			continue;
2632 
2633 		sdata_lock(sdata);
2634 		for (link_id = 0;
2635 		     link_id < ARRAY_SIZE(sdata->vif.link_conf);
2636 		     link_id++) {
2637 			struct ieee80211_link_data *link;
2638 
2639 			link = sdata_dereference(sdata->link[link_id], sdata);
2640 			if (link)
2641 				ieee80211_assign_chanctx(local, sdata, link);
2642 		}
2643 
2644 		switch (sdata->vif.type) {
2645 		case NL80211_IFTYPE_AP_VLAN:
2646 		case NL80211_IFTYPE_MONITOR:
2647 			break;
2648 		case NL80211_IFTYPE_ADHOC:
2649 			if (sdata->vif.cfg.ibss_joined)
2650 				WARN_ON(drv_join_ibss(local, sdata));
2651 			fallthrough;
2652 		default:
2653 			ieee80211_reconfig_stations(sdata);
2654 			fallthrough;
2655 		case NL80211_IFTYPE_AP: /* AP stations are handled later */
2656 			for (i = 0; i < IEEE80211_NUM_ACS; i++)
2657 				drv_conf_tx(local, &sdata->deflink, i,
2658 					    &sdata->deflink.tx_conf[i]);
2659 			break;
2660 		}
2661 		sdata_unlock(sdata);
2662 
2663 		/* common change flags for all interface types */
2664 		changed = BSS_CHANGED_ERP_CTS_PROT |
2665 			  BSS_CHANGED_ERP_PREAMBLE |
2666 			  BSS_CHANGED_ERP_SLOT |
2667 			  BSS_CHANGED_HT |
2668 			  BSS_CHANGED_BASIC_RATES |
2669 			  BSS_CHANGED_BEACON_INT |
2670 			  BSS_CHANGED_BSSID |
2671 			  BSS_CHANGED_CQM |
2672 			  BSS_CHANGED_QOS |
2673 			  BSS_CHANGED_IDLE |
2674 			  BSS_CHANGED_TXPOWER |
2675 			  BSS_CHANGED_MCAST_RATE;
2676 
2677 		if (sdata->vif.bss_conf.mu_mimo_owner)
2678 			changed |= BSS_CHANGED_MU_GROUPS;
2679 
2680 		switch (sdata->vif.type) {
2681 		case NL80211_IFTYPE_STATION:
2682 			changed |= BSS_CHANGED_ASSOC |
2683 				   BSS_CHANGED_ARP_FILTER |
2684 				   BSS_CHANGED_PS;
2685 
2686 			/* Re-send beacon info report to the driver */
2687 			if (sdata->deflink.u.mgd.have_beacon)
2688 				changed |= BSS_CHANGED_BEACON_INFO;
2689 
2690 			if (sdata->vif.bss_conf.max_idle_period ||
2691 			    sdata->vif.bss_conf.protected_keep_alive)
2692 				changed |= BSS_CHANGED_KEEP_ALIVE;
2693 
2694 			sdata_lock(sdata);
2695 			ieee80211_bss_info_change_notify(sdata, changed);
2696 			sdata_unlock(sdata);
2697 			break;
2698 		case NL80211_IFTYPE_OCB:
2699 			changed |= BSS_CHANGED_OCB;
2700 			ieee80211_bss_info_change_notify(sdata, changed);
2701 			break;
2702 		case NL80211_IFTYPE_ADHOC:
2703 			changed |= BSS_CHANGED_IBSS;
2704 			fallthrough;
2705 		case NL80211_IFTYPE_AP:
2706 			changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2707 
2708 			if (sdata->vif.bss_conf.ftm_responder == 1 &&
2709 			    wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2710 					NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2711 				changed |= BSS_CHANGED_FTM_RESPONDER;
2712 
2713 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2714 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2715 
2716 				if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2717 					drv_start_ap(local, sdata,
2718 						     sdata->deflink.conf);
2719 			}
2720 			fallthrough;
2721 		case NL80211_IFTYPE_MESH_POINT:
2722 			if (sdata->vif.bss_conf.enable_beacon) {
2723 				changed |= BSS_CHANGED_BEACON |
2724 					   BSS_CHANGED_BEACON_ENABLED;
2725 				ieee80211_bss_info_change_notify(sdata, changed);
2726 			}
2727 			break;
2728 		case NL80211_IFTYPE_NAN:
2729 			res = ieee80211_reconfig_nan(sdata);
2730 			if (res < 0) {
2731 				ieee80211_handle_reconfig_failure(local);
2732 				return res;
2733 			}
2734 			break;
2735 		case NL80211_IFTYPE_AP_VLAN:
2736 		case NL80211_IFTYPE_MONITOR:
2737 		case NL80211_IFTYPE_P2P_DEVICE:
2738 			/* nothing to do */
2739 			break;
2740 		case NL80211_IFTYPE_UNSPECIFIED:
2741 		case NUM_NL80211_IFTYPES:
2742 		case NL80211_IFTYPE_P2P_CLIENT:
2743 		case NL80211_IFTYPE_P2P_GO:
2744 		case NL80211_IFTYPE_WDS:
2745 			WARN_ON(1);
2746 			break;
2747 		}
2748 	}
2749 
2750 	ieee80211_recalc_ps(local);
2751 
2752 	/*
2753 	 * The sta might be in psm against the ap (e.g. because
2754 	 * this was the state before a hw restart), so we
2755 	 * explicitly send a null packet in order to make sure
2756 	 * it'll sync against the ap (and get out of psm).
2757 	 */
2758 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2759 		list_for_each_entry(sdata, &local->interfaces, list) {
2760 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
2761 				continue;
2762 			if (!sdata->u.mgd.associated)
2763 				continue;
2764 
2765 			ieee80211_send_nullfunc(local, sdata, false);
2766 		}
2767 	}
2768 
2769 	/* APs are now beaconing, add back stations */
2770 	list_for_each_entry(sdata, &local->interfaces, list) {
2771 		if (!ieee80211_sdata_running(sdata))
2772 			continue;
2773 
2774 		sdata_lock(sdata);
2775 		switch (sdata->vif.type) {
2776 		case NL80211_IFTYPE_AP_VLAN:
2777 		case NL80211_IFTYPE_AP:
2778 			ieee80211_reconfig_stations(sdata);
2779 			break;
2780 		default:
2781 			break;
2782 		}
2783 		sdata_unlock(sdata);
2784 	}
2785 
2786 	/* add back keys */
2787 	list_for_each_entry(sdata, &local->interfaces, list)
2788 		ieee80211_reenable_keys(sdata);
2789 
2790 	/* Reconfigure sched scan if it was interrupted by FW restart */
2791 	mutex_lock(&local->mtx);
2792 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2793 						lockdep_is_held(&local->mtx));
2794 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2795 						lockdep_is_held(&local->mtx));
2796 	if (sched_scan_sdata && sched_scan_req)
2797 		/*
2798 		 * Sched scan stopped, but we don't want to report it. Instead,
2799 		 * we're trying to reschedule. However, if more than one scan
2800 		 * plan was set, we cannot reschedule since we don't know which
2801 		 * scan plan was currently running (and some scan plans may have
2802 		 * already finished).
2803 		 */
2804 		if (sched_scan_req->n_scan_plans > 1 ||
2805 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2806 							 sched_scan_req)) {
2807 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2808 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
2809 			sched_scan_stopped = true;
2810 		}
2811 	mutex_unlock(&local->mtx);
2812 
2813 	if (sched_scan_stopped)
2814 		cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2815 
2816  wake_up:
2817 
2818 	if (local->monitors == local->open_count && local->monitors > 0)
2819 		ieee80211_add_virtual_monitor(local);
2820 
2821 	/*
2822 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2823 	 * sessions can be established after a resume.
2824 	 *
2825 	 * Also tear down aggregation sessions since reconfiguring
2826 	 * them in a hardware restart scenario is not easily done
2827 	 * right now, and the hardware will have lost information
2828 	 * about the sessions, but we and the AP still think they
2829 	 * are active. This is really a workaround though.
2830 	 */
2831 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2832 		mutex_lock(&local->sta_mtx);
2833 
2834 		list_for_each_entry(sta, &local->sta_list, list) {
2835 			if (!local->resuming)
2836 				ieee80211_sta_tear_down_BA_sessions(
2837 						sta, AGG_STOP_LOCAL_REQUEST);
2838 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2839 		}
2840 
2841 		mutex_unlock(&local->sta_mtx);
2842 	}
2843 
2844 	/*
2845 	 * If this is for hw restart things are still running.
2846 	 * We may want to change that later, however.
2847 	 */
2848 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2849 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2850 
2851 	if (local->in_reconfig) {
2852 		in_reconfig = local->in_reconfig;
2853 		local->in_reconfig = false;
2854 		barrier();
2855 
2856 		/* Restart deferred ROCs */
2857 		mutex_lock(&local->mtx);
2858 		ieee80211_start_next_roc(local);
2859 		mutex_unlock(&local->mtx);
2860 
2861 		/* Requeue all works */
2862 		list_for_each_entry(sdata, &local->interfaces, list)
2863 			ieee80211_queue_work(&local->hw, &sdata->work);
2864 	}
2865 
2866 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2867 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2868 					false);
2869 
2870 	if (in_reconfig) {
2871 		list_for_each_entry(sdata, &local->interfaces, list) {
2872 			if (!ieee80211_sdata_running(sdata))
2873 				continue;
2874 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
2875 				ieee80211_sta_restart(sdata);
2876 		}
2877 	}
2878 
2879 	if (!suspended)
2880 		return 0;
2881 
2882 #ifdef CONFIG_PM
2883 	/* first set suspended false, then resuming */
2884 	local->suspended = false;
2885 	mb();
2886 	local->resuming = false;
2887 
2888 	ieee80211_flush_completed_scan(local, false);
2889 
2890 	if (local->open_count && !reconfig_due_to_wowlan)
2891 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2892 
2893 	list_for_each_entry(sdata, &local->interfaces, list) {
2894 		if (!ieee80211_sdata_running(sdata))
2895 			continue;
2896 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2897 			ieee80211_sta_restart(sdata);
2898 	}
2899 
2900 	mod_timer(&local->sta_cleanup, jiffies + 1);
2901 #else
2902 	WARN_ON(1);
2903 #endif
2904 
2905 	return 0;
2906 }
2907 
2908 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2909 {
2910 	struct ieee80211_sub_if_data *sdata;
2911 	struct ieee80211_local *local;
2912 	struct ieee80211_key *key;
2913 
2914 	if (WARN_ON(!vif))
2915 		return;
2916 
2917 	sdata = vif_to_sdata(vif);
2918 	local = sdata->local;
2919 
2920 	if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2921 		    !local->resuming))
2922 		return;
2923 
2924 	if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2925 		    !local->in_reconfig))
2926 		return;
2927 
2928 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2929 		return;
2930 
2931 	sdata->flags |= flag;
2932 
2933 	mutex_lock(&local->key_mtx);
2934 	list_for_each_entry(key, &sdata->key_list, list)
2935 		key->flags |= KEY_FLAG_TAINTED;
2936 	mutex_unlock(&local->key_mtx);
2937 }
2938 
2939 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
2940 {
2941 	ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
2942 }
2943 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
2944 
2945 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2946 {
2947 	ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
2948 }
2949 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2950 
2951 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
2952 			   struct ieee80211_link_data *link)
2953 {
2954 	struct ieee80211_local *local = sdata->local;
2955 	struct ieee80211_chanctx_conf *chanctx_conf;
2956 	struct ieee80211_chanctx *chanctx;
2957 
2958 	mutex_lock(&local->chanctx_mtx);
2959 
2960 	chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
2961 						 lockdep_is_held(&local->chanctx_mtx));
2962 
2963 	/*
2964 	 * This function can be called from a work, thus it may be possible
2965 	 * that the chanctx_conf is removed (due to a disconnection, for
2966 	 * example).
2967 	 * So nothing should be done in such case.
2968 	 */
2969 	if (!chanctx_conf)
2970 		goto unlock;
2971 
2972 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2973 	ieee80211_recalc_smps_chanctx(local, chanctx);
2974  unlock:
2975 	mutex_unlock(&local->chanctx_mtx);
2976 }
2977 
2978 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
2979 				  int link_id)
2980 {
2981 	struct ieee80211_local *local = sdata->local;
2982 	struct ieee80211_chanctx_conf *chanctx_conf;
2983 	struct ieee80211_chanctx *chanctx;
2984 	int i;
2985 
2986 	mutex_lock(&local->chanctx_mtx);
2987 
2988 	for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
2989 		struct ieee80211_bss_conf *bss_conf;
2990 
2991 		if (link_id >= 0 && link_id != i)
2992 			continue;
2993 
2994 		rcu_read_lock();
2995 		bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
2996 		if (!bss_conf) {
2997 			rcu_read_unlock();
2998 			continue;
2999 		}
3000 
3001 		chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
3002 							 lockdep_is_held(&local->chanctx_mtx));
3003 		/*
3004 		 * Since we hold the chanctx_mtx (checked above)
3005 		 * we can take the chanctx_conf pointer out of the
3006 		 * RCU critical section, it cannot go away without
3007 		 * the mutex. Just the way we reached it could - in
3008 		 * theory - go away, but we don't really care and
3009 		 * it really shouldn't happen anyway.
3010 		 */
3011 		rcu_read_unlock();
3012 
3013 		if (!chanctx_conf)
3014 			goto unlock;
3015 
3016 		chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
3017 				       conf);
3018 		ieee80211_recalc_chanctx_min_def(local, chanctx);
3019 	}
3020  unlock:
3021 	mutex_unlock(&local->chanctx_mtx);
3022 }
3023 
3024 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
3025 {
3026 	size_t pos = offset;
3027 
3028 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
3029 		pos += 2 + ies[pos + 1];
3030 
3031 	return pos;
3032 }
3033 
3034 u8 *ieee80211_ie_build_s1g_cap(u8 *pos, struct ieee80211_sta_s1g_cap *s1g_cap)
3035 {
3036 	*pos++ = WLAN_EID_S1G_CAPABILITIES;
3037 	*pos++ = sizeof(struct ieee80211_s1g_cap);
3038 	memset(pos, 0, sizeof(struct ieee80211_s1g_cap));
3039 
3040 	memcpy(pos, &s1g_cap->cap, sizeof(s1g_cap->cap));
3041 	pos += sizeof(s1g_cap->cap);
3042 
3043 	memcpy(pos, &s1g_cap->nss_mcs, sizeof(s1g_cap->nss_mcs));
3044 	pos += sizeof(s1g_cap->nss_mcs);
3045 
3046 	return pos;
3047 }
3048 
3049 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3050 			      u16 cap)
3051 {
3052 	__le16 tmp;
3053 
3054 	*pos++ = WLAN_EID_HT_CAPABILITY;
3055 	*pos++ = sizeof(struct ieee80211_ht_cap);
3056 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
3057 
3058 	/* capability flags */
3059 	tmp = cpu_to_le16(cap);
3060 	memcpy(pos, &tmp, sizeof(u16));
3061 	pos += sizeof(u16);
3062 
3063 	/* AMPDU parameters */
3064 	*pos++ = ht_cap->ampdu_factor |
3065 		 (ht_cap->ampdu_density <<
3066 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
3067 
3068 	/* MCS set */
3069 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
3070 	pos += sizeof(ht_cap->mcs);
3071 
3072 	/* extended capabilities */
3073 	pos += sizeof(__le16);
3074 
3075 	/* BF capabilities */
3076 	pos += sizeof(__le32);
3077 
3078 	/* antenna selection */
3079 	pos += sizeof(u8);
3080 
3081 	return pos;
3082 }
3083 
3084 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3085 			       u32 cap)
3086 {
3087 	__le32 tmp;
3088 
3089 	*pos++ = WLAN_EID_VHT_CAPABILITY;
3090 	*pos++ = sizeof(struct ieee80211_vht_cap);
3091 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
3092 
3093 	/* capability flags */
3094 	tmp = cpu_to_le32(cap);
3095 	memcpy(pos, &tmp, sizeof(u32));
3096 	pos += sizeof(u32);
3097 
3098 	/* VHT MCS set */
3099 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
3100 	pos += sizeof(vht_cap->vht_mcs);
3101 
3102 	return pos;
3103 }
3104 
3105 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
3106 {
3107 	const struct ieee80211_sta_he_cap *he_cap;
3108 	struct ieee80211_supported_band *sband;
3109 	u8 n;
3110 
3111 	sband = ieee80211_get_sband(sdata);
3112 	if (!sband)
3113 		return 0;
3114 
3115 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3116 	if (!he_cap)
3117 		return 0;
3118 
3119 	n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
3120 	return 2 + 1 +
3121 	       sizeof(he_cap->he_cap_elem) + n +
3122 	       ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3123 				     he_cap->he_cap_elem.phy_cap_info);
3124 }
3125 
3126 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
3127 			      const struct ieee80211_sta_he_cap *he_cap,
3128 			      u8 *end)
3129 {
3130 	struct ieee80211_he_cap_elem elem;
3131 	u8 n;
3132 	u8 ie_len;
3133 	u8 *orig_pos = pos;
3134 
3135 	/* Make sure we have place for the IE */
3136 	/*
3137 	 * TODO: the 1 added is because this temporarily is under the EXTENSION
3138 	 * IE. Get rid of it when it moves.
3139 	 */
3140 	if (!he_cap)
3141 		return orig_pos;
3142 
3143 	/* modify on stack first to calculate 'n' and 'ie_len' correctly */
3144 	elem = he_cap->he_cap_elem;
3145 
3146 	if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
3147 		elem.phy_cap_info[0] &=
3148 			~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3149 			  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
3150 
3151 	if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
3152 		elem.phy_cap_info[0] &=
3153 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3154 
3155 	if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
3156 		elem.phy_cap_info[0] &=
3157 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3158 
3159 	n = ieee80211_he_mcs_nss_size(&elem);
3160 	ie_len = 2 + 1 +
3161 		 sizeof(he_cap->he_cap_elem) + n +
3162 		 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3163 				       he_cap->he_cap_elem.phy_cap_info);
3164 
3165 	if ((end - pos) < ie_len)
3166 		return orig_pos;
3167 
3168 	*pos++ = WLAN_EID_EXTENSION;
3169 	pos++; /* We'll set the size later below */
3170 	*pos++ = WLAN_EID_EXT_HE_CAPABILITY;
3171 
3172 	/* Fixed data */
3173 	memcpy(pos, &elem, sizeof(elem));
3174 	pos += sizeof(elem);
3175 
3176 	memcpy(pos, &he_cap->he_mcs_nss_supp, n);
3177 	pos += n;
3178 
3179 	/* Check if PPE Threshold should be present */
3180 	if ((he_cap->he_cap_elem.phy_cap_info[6] &
3181 	     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
3182 		goto end;
3183 
3184 	/*
3185 	 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
3186 	 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
3187 	 */
3188 	n = hweight8(he_cap->ppe_thres[0] &
3189 		     IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
3190 	n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
3191 		   IEEE80211_PPE_THRES_NSS_POS));
3192 
3193 	/*
3194 	 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
3195 	 * total size.
3196 	 */
3197 	n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
3198 	n = DIV_ROUND_UP(n, 8);
3199 
3200 	/* Copy PPE Thresholds */
3201 	memcpy(pos, &he_cap->ppe_thres, n);
3202 	pos += n;
3203 
3204 end:
3205 	orig_pos[1] = (pos - orig_pos) - 2;
3206 	return pos;
3207 }
3208 
3209 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
3210 				    enum ieee80211_smps_mode smps_mode,
3211 				    struct sk_buff *skb)
3212 {
3213 	struct ieee80211_supported_band *sband;
3214 	const struct ieee80211_sband_iftype_data *iftd;
3215 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3216 	u8 *pos;
3217 	u16 cap;
3218 
3219 	if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
3220 					  BIT(NL80211_BAND_6GHZ),
3221 					  IEEE80211_CHAN_NO_HE))
3222 		return;
3223 
3224 	sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3225 
3226 	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
3227 	if (!iftd)
3228 		return;
3229 
3230 	/* Check for device HE 6 GHz capability before adding element */
3231 	if (!iftd->he_6ghz_capa.capa)
3232 		return;
3233 
3234 	cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
3235 	cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
3236 
3237 	switch (smps_mode) {
3238 	case IEEE80211_SMPS_AUTOMATIC:
3239 	case IEEE80211_SMPS_NUM_MODES:
3240 		WARN_ON(1);
3241 		fallthrough;
3242 	case IEEE80211_SMPS_OFF:
3243 		cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
3244 				       IEEE80211_HE_6GHZ_CAP_SM_PS);
3245 		break;
3246 	case IEEE80211_SMPS_STATIC:
3247 		cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
3248 				       IEEE80211_HE_6GHZ_CAP_SM_PS);
3249 		break;
3250 	case IEEE80211_SMPS_DYNAMIC:
3251 		cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
3252 				       IEEE80211_HE_6GHZ_CAP_SM_PS);
3253 		break;
3254 	}
3255 
3256 	pos = skb_put(skb, 2 + 1 + sizeof(cap));
3257 	ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
3258 				    pos + 2 + 1 + sizeof(cap));
3259 }
3260 
3261 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3262 			       const struct cfg80211_chan_def *chandef,
3263 			       u16 prot_mode, bool rifs_mode)
3264 {
3265 	struct ieee80211_ht_operation *ht_oper;
3266 	/* Build HT Information */
3267 	*pos++ = WLAN_EID_HT_OPERATION;
3268 	*pos++ = sizeof(struct ieee80211_ht_operation);
3269 	ht_oper = (struct ieee80211_ht_operation *)pos;
3270 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
3271 					chandef->chan->center_freq);
3272 	switch (chandef->width) {
3273 	case NL80211_CHAN_WIDTH_160:
3274 	case NL80211_CHAN_WIDTH_80P80:
3275 	case NL80211_CHAN_WIDTH_80:
3276 	case NL80211_CHAN_WIDTH_40:
3277 		if (chandef->center_freq1 > chandef->chan->center_freq)
3278 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3279 		else
3280 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3281 		break;
3282 	case NL80211_CHAN_WIDTH_320:
3283 		/* HT information element should not be included on 6GHz */
3284 		WARN_ON(1);
3285 		return pos;
3286 	default:
3287 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
3288 		break;
3289 	}
3290 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
3291 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
3292 	    chandef->width != NL80211_CHAN_WIDTH_20)
3293 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
3294 
3295 	if (rifs_mode)
3296 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
3297 
3298 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
3299 	ht_oper->stbc_param = 0x0000;
3300 
3301 	/* It seems that Basic MCS set and Supported MCS set
3302 	   are identical for the first 10 bytes */
3303 	memset(&ht_oper->basic_set, 0, 16);
3304 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
3305 
3306 	return pos + sizeof(struct ieee80211_ht_operation);
3307 }
3308 
3309 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
3310 				   const struct cfg80211_chan_def *chandef)
3311 {
3312 	*pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH;	/* EID */
3313 	*pos++ = 3;					/* IE length */
3314 	/* New channel width */
3315 	switch (chandef->width) {
3316 	case NL80211_CHAN_WIDTH_80:
3317 		*pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
3318 		break;
3319 	case NL80211_CHAN_WIDTH_160:
3320 		*pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
3321 		break;
3322 	case NL80211_CHAN_WIDTH_80P80:
3323 		*pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
3324 		break;
3325 	case NL80211_CHAN_WIDTH_320:
3326 		/* The behavior is not defined for 320 MHz channels */
3327 		WARN_ON(1);
3328 		fallthrough;
3329 	default:
3330 		*pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
3331 	}
3332 
3333 	/* new center frequency segment 0 */
3334 	*pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
3335 	/* new center frequency segment 1 */
3336 	if (chandef->center_freq2)
3337 		*pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
3338 	else
3339 		*pos++ = 0;
3340 }
3341 
3342 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3343 				const struct cfg80211_chan_def *chandef)
3344 {
3345 	struct ieee80211_vht_operation *vht_oper;
3346 
3347 	*pos++ = WLAN_EID_VHT_OPERATION;
3348 	*pos++ = sizeof(struct ieee80211_vht_operation);
3349 	vht_oper = (struct ieee80211_vht_operation *)pos;
3350 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
3351 							chandef->center_freq1);
3352 	if (chandef->center_freq2)
3353 		vht_oper->center_freq_seg1_idx =
3354 			ieee80211_frequency_to_channel(chandef->center_freq2);
3355 	else
3356 		vht_oper->center_freq_seg1_idx = 0x00;
3357 
3358 	switch (chandef->width) {
3359 	case NL80211_CHAN_WIDTH_160:
3360 		/*
3361 		 * Convert 160 MHz channel width to new style as interop
3362 		 * workaround.
3363 		 */
3364 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3365 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
3366 		if (chandef->chan->center_freq < chandef->center_freq1)
3367 			vht_oper->center_freq_seg0_idx -= 8;
3368 		else
3369 			vht_oper->center_freq_seg0_idx += 8;
3370 		break;
3371 	case NL80211_CHAN_WIDTH_80P80:
3372 		/*
3373 		 * Convert 80+80 MHz channel width to new style as interop
3374 		 * workaround.
3375 		 */
3376 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3377 		break;
3378 	case NL80211_CHAN_WIDTH_80:
3379 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3380 		break;
3381 	case NL80211_CHAN_WIDTH_320:
3382 		/* VHT information element should not be included on 6GHz */
3383 		WARN_ON(1);
3384 		return pos;
3385 	default:
3386 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
3387 		break;
3388 	}
3389 
3390 	/* don't require special VHT peer rates */
3391 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
3392 
3393 	return pos + sizeof(struct ieee80211_vht_operation);
3394 }
3395 
3396 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
3397 {
3398 	struct ieee80211_he_operation *he_oper;
3399 	struct ieee80211_he_6ghz_oper *he_6ghz_op;
3400 	u32 he_oper_params;
3401 	u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
3402 
3403 	if (chandef->chan->band == NL80211_BAND_6GHZ)
3404 		ie_len += sizeof(struct ieee80211_he_6ghz_oper);
3405 
3406 	*pos++ = WLAN_EID_EXTENSION;
3407 	*pos++ = ie_len;
3408 	*pos++ = WLAN_EID_EXT_HE_OPERATION;
3409 
3410 	he_oper_params = 0;
3411 	he_oper_params |= u32_encode_bits(1023, /* disabled */
3412 				IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3413 	he_oper_params |= u32_encode_bits(1,
3414 				IEEE80211_HE_OPERATION_ER_SU_DISABLE);
3415 	he_oper_params |= u32_encode_bits(1,
3416 				IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3417 	if (chandef->chan->band == NL80211_BAND_6GHZ)
3418 		he_oper_params |= u32_encode_bits(1,
3419 				IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
3420 
3421 	he_oper = (struct ieee80211_he_operation *)pos;
3422 	he_oper->he_oper_params = cpu_to_le32(he_oper_params);
3423 
3424 	/* don't require special HE peer rates */
3425 	he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
3426 	pos += sizeof(struct ieee80211_he_operation);
3427 
3428 	if (chandef->chan->band != NL80211_BAND_6GHZ)
3429 		goto out;
3430 
3431 	/* TODO add VHT operational */
3432 	he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
3433 	he_6ghz_op->minrate = 6; /* 6 Mbps */
3434 	he_6ghz_op->primary =
3435 		ieee80211_frequency_to_channel(chandef->chan->center_freq);
3436 	he_6ghz_op->ccfs0 =
3437 		ieee80211_frequency_to_channel(chandef->center_freq1);
3438 	if (chandef->center_freq2)
3439 		he_6ghz_op->ccfs1 =
3440 			ieee80211_frequency_to_channel(chandef->center_freq2);
3441 	else
3442 		he_6ghz_op->ccfs1 = 0;
3443 
3444 	switch (chandef->width) {
3445 	case NL80211_CHAN_WIDTH_320:
3446 		/*
3447 		 * TODO: mesh operation is not defined over 6GHz 320 MHz
3448 		 * channels.
3449 		 */
3450 		WARN_ON(1);
3451 		break;
3452 	case NL80211_CHAN_WIDTH_160:
3453 		/* Convert 160 MHz channel width to new style as interop
3454 		 * workaround.
3455 		 */
3456 		he_6ghz_op->control =
3457 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3458 		he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
3459 		if (chandef->chan->center_freq < chandef->center_freq1)
3460 			he_6ghz_op->ccfs0 -= 8;
3461 		else
3462 			he_6ghz_op->ccfs0 += 8;
3463 		fallthrough;
3464 	case NL80211_CHAN_WIDTH_80P80:
3465 		he_6ghz_op->control =
3466 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3467 		break;
3468 	case NL80211_CHAN_WIDTH_80:
3469 		he_6ghz_op->control =
3470 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
3471 		break;
3472 	case NL80211_CHAN_WIDTH_40:
3473 		he_6ghz_op->control =
3474 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3475 		break;
3476 	default:
3477 		he_6ghz_op->control =
3478 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3479 		break;
3480 	}
3481 
3482 	pos += sizeof(struct ieee80211_he_6ghz_oper);
3483 
3484 out:
3485 	return pos;
3486 }
3487 
3488 u8 *ieee80211_ie_build_eht_oper(u8 *pos, struct cfg80211_chan_def *chandef,
3489 				const struct ieee80211_sta_eht_cap *eht_cap)
3490 
3491 {
3492 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
3493 					&eht_cap->eht_mcs_nss_supp.only_20mhz;
3494 	struct ieee80211_eht_operation *eht_oper;
3495 	struct ieee80211_eht_operation_info *eht_oper_info;
3496 	u8 eht_oper_len = offsetof(struct ieee80211_eht_operation, optional);
3497 	u8 eht_oper_info_len =
3498 		offsetof(struct ieee80211_eht_operation_info, optional);
3499 	u8 chan_width = 0;
3500 
3501 	*pos++ = WLAN_EID_EXTENSION;
3502 	*pos++ = 1 + eht_oper_len + eht_oper_info_len;
3503 	*pos++ = WLAN_EID_EXT_EHT_OPERATION;
3504 
3505 	eht_oper = (struct ieee80211_eht_operation *)pos;
3506 
3507 	memcpy(&eht_oper->basic_mcs_nss, eht_mcs_nss, sizeof(*eht_mcs_nss));
3508 	eht_oper->params |= IEEE80211_EHT_OPER_INFO_PRESENT;
3509 	pos += eht_oper_len;
3510 
3511 	eht_oper_info =
3512 		(struct ieee80211_eht_operation_info *)eht_oper->optional;
3513 
3514 	eht_oper_info->ccfs0 =
3515 		ieee80211_frequency_to_channel(chandef->center_freq1);
3516 	if (chandef->center_freq2)
3517 		eht_oper_info->ccfs1 =
3518 			ieee80211_frequency_to_channel(chandef->center_freq2);
3519 	else
3520 		eht_oper_info->ccfs1 = 0;
3521 
3522 	switch (chandef->width) {
3523 	case NL80211_CHAN_WIDTH_320:
3524 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ;
3525 		eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3526 		if (chandef->chan->center_freq < chandef->center_freq1)
3527 			eht_oper_info->ccfs0 -= 16;
3528 		else
3529 			eht_oper_info->ccfs0 += 16;
3530 		break;
3531 	case NL80211_CHAN_WIDTH_160:
3532 		eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3533 		if (chandef->chan->center_freq < chandef->center_freq1)
3534 			eht_oper_info->ccfs0 -= 8;
3535 		else
3536 			eht_oper_info->ccfs0 += 8;
3537 		fallthrough;
3538 	case NL80211_CHAN_WIDTH_80P80:
3539 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ;
3540 		break;
3541 	case NL80211_CHAN_WIDTH_80:
3542 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ;
3543 		break;
3544 	case NL80211_CHAN_WIDTH_40:
3545 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ;
3546 		break;
3547 	default:
3548 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ;
3549 		break;
3550 	}
3551 	eht_oper_info->control = chan_width;
3552 	pos += eht_oper_info_len;
3553 
3554 	/* TODO: eht_oper_info->optional */
3555 
3556 	return pos;
3557 }
3558 
3559 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3560 			       struct cfg80211_chan_def *chandef)
3561 {
3562 	enum nl80211_channel_type channel_type;
3563 
3564 	if (!ht_oper)
3565 		return false;
3566 
3567 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3568 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3569 		channel_type = NL80211_CHAN_HT20;
3570 		break;
3571 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3572 		channel_type = NL80211_CHAN_HT40PLUS;
3573 		break;
3574 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3575 		channel_type = NL80211_CHAN_HT40MINUS;
3576 		break;
3577 	default:
3578 		return false;
3579 	}
3580 
3581 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3582 	return true;
3583 }
3584 
3585 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3586 				const struct ieee80211_vht_operation *oper,
3587 				const struct ieee80211_ht_operation *htop,
3588 				struct cfg80211_chan_def *chandef)
3589 {
3590 	struct cfg80211_chan_def new = *chandef;
3591 	int cf0, cf1;
3592 	int ccfs0, ccfs1, ccfs2;
3593 	int ccf0, ccf1;
3594 	u32 vht_cap;
3595 	bool support_80_80 = false;
3596 	bool support_160 = false;
3597 	u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3598 					  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3599 	u8 supp_chwidth = u32_get_bits(vht_cap_info,
3600 				       IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3601 
3602 	if (!oper || !htop)
3603 		return false;
3604 
3605 	vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3606 	support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3607 				  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3608 	support_80_80 = ((vht_cap &
3609 			 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3610 			(vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3611 			 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3612 			((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3613 				    IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3614 	ccfs0 = oper->center_freq_seg0_idx;
3615 	ccfs1 = oper->center_freq_seg1_idx;
3616 	ccfs2 = (le16_to_cpu(htop->operation_mode) &
3617 				IEEE80211_HT_OP_MODE_CCFS2_MASK)
3618 			>> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3619 
3620 	ccf0 = ccfs0;
3621 
3622 	/* if not supported, parse as though we didn't understand it */
3623 	if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3624 		ext_nss_bw_supp = 0;
3625 
3626 	/*
3627 	 * Cf. IEEE 802.11 Table 9-250
3628 	 *
3629 	 * We really just consider that because it's inefficient to connect
3630 	 * at a higher bandwidth than we'll actually be able to use.
3631 	 */
3632 	switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3633 	default:
3634 	case 0x00:
3635 		ccf1 = 0;
3636 		support_160 = false;
3637 		support_80_80 = false;
3638 		break;
3639 	case 0x01:
3640 		support_80_80 = false;
3641 		fallthrough;
3642 	case 0x02:
3643 	case 0x03:
3644 		ccf1 = ccfs2;
3645 		break;
3646 	case 0x10:
3647 		ccf1 = ccfs1;
3648 		break;
3649 	case 0x11:
3650 	case 0x12:
3651 		if (!ccfs1)
3652 			ccf1 = ccfs2;
3653 		else
3654 			ccf1 = ccfs1;
3655 		break;
3656 	case 0x13:
3657 	case 0x20:
3658 	case 0x23:
3659 		ccf1 = ccfs1;
3660 		break;
3661 	}
3662 
3663 	cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3664 	cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3665 
3666 	switch (oper->chan_width) {
3667 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
3668 		/* just use HT information directly */
3669 		break;
3670 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
3671 		new.width = NL80211_CHAN_WIDTH_80;
3672 		new.center_freq1 = cf0;
3673 		/* If needed, adjust based on the newer interop workaround. */
3674 		if (ccf1) {
3675 			unsigned int diff;
3676 
3677 			diff = abs(ccf1 - ccf0);
3678 			if ((diff == 8) && support_160) {
3679 				new.width = NL80211_CHAN_WIDTH_160;
3680 				new.center_freq1 = cf1;
3681 			} else if ((diff > 8) && support_80_80) {
3682 				new.width = NL80211_CHAN_WIDTH_80P80;
3683 				new.center_freq2 = cf1;
3684 			}
3685 		}
3686 		break;
3687 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
3688 		/* deprecated encoding */
3689 		new.width = NL80211_CHAN_WIDTH_160;
3690 		new.center_freq1 = cf0;
3691 		break;
3692 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3693 		/* deprecated encoding */
3694 		new.width = NL80211_CHAN_WIDTH_80P80;
3695 		new.center_freq1 = cf0;
3696 		new.center_freq2 = cf1;
3697 		break;
3698 	default:
3699 		return false;
3700 	}
3701 
3702 	if (!cfg80211_chandef_valid(&new))
3703 		return false;
3704 
3705 	*chandef = new;
3706 	return true;
3707 }
3708 
3709 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
3710 				bool support_160, bool support_320,
3711 				struct cfg80211_chan_def *chandef)
3712 {
3713 	struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
3714 
3715 	chandef->center_freq1 =
3716 		ieee80211_channel_to_frequency(info->ccfs0,
3717 					       chandef->chan->band);
3718 
3719 	switch (u8_get_bits(info->control,
3720 			    IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3721 	case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3722 		chandef->width = NL80211_CHAN_WIDTH_20;
3723 		break;
3724 	case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3725 		chandef->width = NL80211_CHAN_WIDTH_40;
3726 		break;
3727 	case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3728 		chandef->width = NL80211_CHAN_WIDTH_80;
3729 		break;
3730 	case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3731 		if (support_160) {
3732 			chandef->width = NL80211_CHAN_WIDTH_160;
3733 			chandef->center_freq1 =
3734 				ieee80211_channel_to_frequency(info->ccfs1,
3735 							       chandef->chan->band);
3736 		} else {
3737 			chandef->width = NL80211_CHAN_WIDTH_80;
3738 		}
3739 		break;
3740 	case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3741 		if (support_320) {
3742 			chandef->width = NL80211_CHAN_WIDTH_320;
3743 			chandef->center_freq1 =
3744 				ieee80211_channel_to_frequency(info->ccfs1,
3745 							       chandef->chan->band);
3746 		} else if (support_160) {
3747 			chandef->width = NL80211_CHAN_WIDTH_160;
3748 		} else {
3749 			chandef->width = NL80211_CHAN_WIDTH_80;
3750 
3751 			if (chandef->center_freq1 > chandef->chan->center_freq)
3752 				chandef->center_freq1 -= 40;
3753 			else
3754 				chandef->center_freq1 += 40;
3755 		}
3756 		break;
3757 	}
3758 }
3759 
3760 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
3761 				    const struct ieee80211_he_operation *he_oper,
3762 				    const struct ieee80211_eht_operation *eht_oper,
3763 				    struct cfg80211_chan_def *chandef)
3764 {
3765 	struct ieee80211_local *local = sdata->local;
3766 	struct ieee80211_supported_band *sband;
3767 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3768 	const struct ieee80211_sta_he_cap *he_cap;
3769 	const struct ieee80211_sta_eht_cap *eht_cap;
3770 	struct cfg80211_chan_def he_chandef = *chandef;
3771 	const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3772 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3773 	bool support_80_80, support_160, support_320;
3774 	u8 he_phy_cap, eht_phy_cap;
3775 	u32 freq;
3776 
3777 	if (chandef->chan->band != NL80211_BAND_6GHZ)
3778 		return true;
3779 
3780 	sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3781 
3782 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3783 	if (!he_cap) {
3784 		sdata_info(sdata, "Missing iftype sband data/HE cap");
3785 		return false;
3786 	}
3787 
3788 	he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
3789 	support_160 =
3790 		he_phy_cap &
3791 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3792 	support_80_80 =
3793 		he_phy_cap &
3794 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3795 
3796 	if (!he_oper) {
3797 		sdata_info(sdata,
3798 			   "HE is not advertised on (on %d MHz), expect issues\n",
3799 			   chandef->chan->center_freq);
3800 		return false;
3801 	}
3802 
3803 	eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
3804 	if (!eht_cap) {
3805 		sdata_info(sdata, "Missing iftype sband data/EHT cap");
3806 		eht_oper = NULL;
3807 	}
3808 
3809 	he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3810 
3811 	if (!he_6ghz_oper) {
3812 		sdata_info(sdata,
3813 			   "HE 6GHz operation missing (on %d MHz), expect issues\n",
3814 			   chandef->chan->center_freq);
3815 		return false;
3816 	}
3817 
3818 	/*
3819 	 * The EHT operation IE does not contain the primary channel so the
3820 	 * primary channel frequency should be taken from the 6 GHz operation
3821 	 * information.
3822 	 */
3823 	freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3824 					      NL80211_BAND_6GHZ);
3825 	he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
3826 
3827 	switch (u8_get_bits(he_6ghz_oper->control,
3828 			    IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
3829 	case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
3830 		bss_conf->power_type = IEEE80211_REG_LPI_AP;
3831 		break;
3832 	case IEEE80211_6GHZ_CTRL_REG_SP_AP:
3833 		bss_conf->power_type = IEEE80211_REG_SP_AP;
3834 		break;
3835 	default:
3836 		bss_conf->power_type = IEEE80211_REG_UNSET_AP;
3837 		break;
3838 	}
3839 
3840 	if (!eht_oper ||
3841 	    !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3842 		switch (u8_get_bits(he_6ghz_oper->control,
3843 				    IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3844 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3845 			he_chandef.width = NL80211_CHAN_WIDTH_20;
3846 			break;
3847 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3848 			he_chandef.width = NL80211_CHAN_WIDTH_40;
3849 			break;
3850 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3851 			he_chandef.width = NL80211_CHAN_WIDTH_80;
3852 			break;
3853 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3854 			he_chandef.width = NL80211_CHAN_WIDTH_80;
3855 			if (!he_6ghz_oper->ccfs1)
3856 				break;
3857 			if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
3858 				if (support_160)
3859 					he_chandef.width = NL80211_CHAN_WIDTH_160;
3860 			} else {
3861 				if (support_80_80)
3862 					he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3863 			}
3864 			break;
3865 		}
3866 
3867 		if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3868 			he_chandef.center_freq1 =
3869 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3870 							       NL80211_BAND_6GHZ);
3871 		} else {
3872 			he_chandef.center_freq1 =
3873 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3874 							       NL80211_BAND_6GHZ);
3875 			if (support_80_80 || support_160)
3876 				he_chandef.center_freq2 =
3877 					ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3878 								       NL80211_BAND_6GHZ);
3879 		}
3880 	} else {
3881 		eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
3882 		support_320 =
3883 			eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
3884 
3885 		ieee80211_chandef_eht_oper(eht_oper, support_160,
3886 					   support_320, &he_chandef);
3887 	}
3888 
3889 	if (!cfg80211_chandef_valid(&he_chandef)) {
3890 		sdata_info(sdata,
3891 			   "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
3892 			   he_chandef.chan ? he_chandef.chan->center_freq : 0,
3893 			   he_chandef.width,
3894 			   he_chandef.center_freq1,
3895 			   he_chandef.center_freq2);
3896 		return false;
3897 	}
3898 
3899 	*chandef = he_chandef;
3900 
3901 	return true;
3902 }
3903 
3904 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
3905 				struct cfg80211_chan_def *chandef)
3906 {
3907 	u32 oper_freq;
3908 
3909 	if (!oper)
3910 		return false;
3911 
3912 	switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3913 	case IEEE80211_S1G_CHANWIDTH_1MHZ:
3914 		chandef->width = NL80211_CHAN_WIDTH_1;
3915 		break;
3916 	case IEEE80211_S1G_CHANWIDTH_2MHZ:
3917 		chandef->width = NL80211_CHAN_WIDTH_2;
3918 		break;
3919 	case IEEE80211_S1G_CHANWIDTH_4MHZ:
3920 		chandef->width = NL80211_CHAN_WIDTH_4;
3921 		break;
3922 	case IEEE80211_S1G_CHANWIDTH_8MHZ:
3923 		chandef->width = NL80211_CHAN_WIDTH_8;
3924 		break;
3925 	case IEEE80211_S1G_CHANWIDTH_16MHZ:
3926 		chandef->width = NL80211_CHAN_WIDTH_16;
3927 		break;
3928 	default:
3929 		return false;
3930 	}
3931 
3932 	oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
3933 						  NL80211_BAND_S1GHZ);
3934 	chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
3935 	chandef->freq1_offset = oper_freq % 1000;
3936 
3937 	return true;
3938 }
3939 
3940 int ieee80211_parse_bitrates(enum nl80211_chan_width width,
3941 			     const struct ieee80211_supported_band *sband,
3942 			     const u8 *srates, int srates_len, u32 *rates)
3943 {
3944 	u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
3945 	int shift = ieee80211_chanwidth_get_shift(width);
3946 	struct ieee80211_rate *br;
3947 	int brate, rate, i, j, count = 0;
3948 
3949 	*rates = 0;
3950 
3951 	for (i = 0; i < srates_len; i++) {
3952 		rate = srates[i] & 0x7f;
3953 
3954 		for (j = 0; j < sband->n_bitrates; j++) {
3955 			br = &sband->bitrates[j];
3956 			if ((rate_flags & br->flags) != rate_flags)
3957 				continue;
3958 
3959 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3960 			if (brate == rate) {
3961 				*rates |= BIT(j);
3962 				count++;
3963 				break;
3964 			}
3965 		}
3966 	}
3967 	return count;
3968 }
3969 
3970 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
3971 			    struct sk_buff *skb, bool need_basic,
3972 			    enum nl80211_band band)
3973 {
3974 	struct ieee80211_local *local = sdata->local;
3975 	struct ieee80211_supported_band *sband;
3976 	int rate, shift;
3977 	u8 i, rates, *pos;
3978 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3979 	u32 rate_flags;
3980 
3981 	shift = ieee80211_vif_get_shift(&sdata->vif);
3982 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3983 	sband = local->hw.wiphy->bands[band];
3984 	rates = 0;
3985 	for (i = 0; i < sband->n_bitrates; i++) {
3986 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3987 			continue;
3988 		rates++;
3989 	}
3990 	if (rates > 8)
3991 		rates = 8;
3992 
3993 	if (skb_tailroom(skb) < rates + 2)
3994 		return -ENOMEM;
3995 
3996 	pos = skb_put(skb, rates + 2);
3997 	*pos++ = WLAN_EID_SUPP_RATES;
3998 	*pos++ = rates;
3999 	for (i = 0; i < rates; i++) {
4000 		u8 basic = 0;
4001 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4002 			continue;
4003 
4004 		if (need_basic && basic_rates & BIT(i))
4005 			basic = 0x80;
4006 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4007 				    5 * (1 << shift));
4008 		*pos++ = basic | (u8) rate;
4009 	}
4010 
4011 	return 0;
4012 }
4013 
4014 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
4015 				struct sk_buff *skb, bool need_basic,
4016 				enum nl80211_band band)
4017 {
4018 	struct ieee80211_local *local = sdata->local;
4019 	struct ieee80211_supported_band *sband;
4020 	int rate, shift;
4021 	u8 i, exrates, *pos;
4022 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
4023 	u32 rate_flags;
4024 
4025 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
4026 	shift = ieee80211_vif_get_shift(&sdata->vif);
4027 
4028 	sband = local->hw.wiphy->bands[band];
4029 	exrates = 0;
4030 	for (i = 0; i < sband->n_bitrates; i++) {
4031 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4032 			continue;
4033 		exrates++;
4034 	}
4035 
4036 	if (exrates > 8)
4037 		exrates -= 8;
4038 	else
4039 		exrates = 0;
4040 
4041 	if (skb_tailroom(skb) < exrates + 2)
4042 		return -ENOMEM;
4043 
4044 	if (exrates) {
4045 		pos = skb_put(skb, exrates + 2);
4046 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
4047 		*pos++ = exrates;
4048 		for (i = 8; i < sband->n_bitrates; i++) {
4049 			u8 basic = 0;
4050 			if ((rate_flags & sband->bitrates[i].flags)
4051 			    != rate_flags)
4052 				continue;
4053 			if (need_basic && basic_rates & BIT(i))
4054 				basic = 0x80;
4055 			rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4056 					    5 * (1 << shift));
4057 			*pos++ = basic | (u8) rate;
4058 		}
4059 	}
4060 	return 0;
4061 }
4062 
4063 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
4064 {
4065 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4066 
4067 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
4068 		return 0;
4069 
4070 	return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
4071 }
4072 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
4073 
4074 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
4075 {
4076 	if (!mcs)
4077 		return 1;
4078 
4079 	/* TODO: consider rx_highest */
4080 
4081 	if (mcs->rx_mask[3])
4082 		return 4;
4083 	if (mcs->rx_mask[2])
4084 		return 3;
4085 	if (mcs->rx_mask[1])
4086 		return 2;
4087 	return 1;
4088 }
4089 
4090 /**
4091  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
4092  * @local: mac80211 hw info struct
4093  * @status: RX status
4094  * @mpdu_len: total MPDU length (including FCS)
4095  * @mpdu_offset: offset into MPDU to calculate timestamp at
4096  *
4097  * This function calculates the RX timestamp at the given MPDU offset, taking
4098  * into account what the RX timestamp was. An offset of 0 will just normalize
4099  * the timestamp to TSF at beginning of MPDU reception.
4100  */
4101 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
4102 				     struct ieee80211_rx_status *status,
4103 				     unsigned int mpdu_len,
4104 				     unsigned int mpdu_offset)
4105 {
4106 	u64 ts = status->mactime;
4107 	struct rate_info ri;
4108 	u16 rate;
4109 	u8 n_ltf;
4110 
4111 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
4112 		return 0;
4113 
4114 	memset(&ri, 0, sizeof(ri));
4115 
4116 	ri.bw = status->bw;
4117 
4118 	/* Fill cfg80211 rate info */
4119 	switch (status->encoding) {
4120 	case RX_ENC_EHT:
4121 		ri.flags |= RATE_INFO_FLAGS_EHT_MCS;
4122 		ri.mcs = status->rate_idx;
4123 		ri.nss = status->nss;
4124 		ri.eht_ru_alloc = status->eht.ru;
4125 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4126 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4127 		/* TODO/FIXME: is this right? handle other PPDUs */
4128 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4129 			mpdu_offset += 2;
4130 			ts += 36;
4131 		}
4132 		break;
4133 	case RX_ENC_HE:
4134 		ri.flags |= RATE_INFO_FLAGS_HE_MCS;
4135 		ri.mcs = status->rate_idx;
4136 		ri.nss = status->nss;
4137 		ri.he_ru_alloc = status->he_ru;
4138 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4139 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4140 
4141 		/*
4142 		 * See P802.11ax_D6.0, section 27.3.4 for
4143 		 * VHT PPDU format.
4144 		 */
4145 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4146 			mpdu_offset += 2;
4147 			ts += 36;
4148 
4149 			/*
4150 			 * TODO:
4151 			 * For HE MU PPDU, add the HE-SIG-B.
4152 			 * For HE ER PPDU, add 8us for the HE-SIG-A.
4153 			 * For HE TB PPDU, add 4us for the HE-STF.
4154 			 * Add the HE-LTF durations - variable.
4155 			 */
4156 		}
4157 
4158 		break;
4159 	case RX_ENC_HT:
4160 		ri.mcs = status->rate_idx;
4161 		ri.flags |= RATE_INFO_FLAGS_MCS;
4162 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4163 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4164 
4165 		/*
4166 		 * See P802.11REVmd_D3.0, section 19.3.2 for
4167 		 * HT PPDU format.
4168 		 */
4169 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4170 			mpdu_offset += 2;
4171 			if (status->enc_flags & RX_ENC_FLAG_HT_GF)
4172 				ts += 24;
4173 			else
4174 				ts += 32;
4175 
4176 			/*
4177 			 * Add Data HT-LTFs per streams
4178 			 * TODO: add Extension HT-LTFs, 4us per LTF
4179 			 */
4180 			n_ltf = ((ri.mcs >> 3) & 3) + 1;
4181 			n_ltf = n_ltf == 3 ? 4 : n_ltf;
4182 			ts += n_ltf * 4;
4183 		}
4184 
4185 		break;
4186 	case RX_ENC_VHT:
4187 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
4188 		ri.mcs = status->rate_idx;
4189 		ri.nss = status->nss;
4190 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4191 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4192 
4193 		/*
4194 		 * See P802.11REVmd_D3.0, section 21.3.2 for
4195 		 * VHT PPDU format.
4196 		 */
4197 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4198 			mpdu_offset += 2;
4199 			ts += 36;
4200 
4201 			/*
4202 			 * Add VHT-LTFs per streams
4203 			 */
4204 			n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
4205 				ri.nss + 1 : ri.nss;
4206 			ts += 4 * n_ltf;
4207 		}
4208 
4209 		break;
4210 	default:
4211 		WARN_ON(1);
4212 		fallthrough;
4213 	case RX_ENC_LEGACY: {
4214 		struct ieee80211_supported_band *sband;
4215 		int shift = 0;
4216 		int bitrate;
4217 
4218 		switch (status->bw) {
4219 		case RATE_INFO_BW_10:
4220 			shift = 1;
4221 			break;
4222 		case RATE_INFO_BW_5:
4223 			shift = 2;
4224 			break;
4225 		}
4226 
4227 		sband = local->hw.wiphy->bands[status->band];
4228 		bitrate = sband->bitrates[status->rate_idx].bitrate;
4229 		ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
4230 
4231 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4232 			if (status->band == NL80211_BAND_5GHZ) {
4233 				ts += 20 << shift;
4234 				mpdu_offset += 2;
4235 			} else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
4236 				ts += 96;
4237 			} else {
4238 				ts += 192;
4239 			}
4240 		}
4241 		break;
4242 		}
4243 	}
4244 
4245 	rate = cfg80211_calculate_bitrate(&ri);
4246 	if (WARN_ONCE(!rate,
4247 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
4248 		      (unsigned long long)status->flag, status->rate_idx,
4249 		      status->nss))
4250 		return 0;
4251 
4252 	/* rewind from end of MPDU */
4253 	if (status->flag & RX_FLAG_MACTIME_END)
4254 		ts -= mpdu_len * 8 * 10 / rate;
4255 
4256 	ts += mpdu_offset * 8 * 10 / rate;
4257 
4258 	return ts;
4259 }
4260 
4261 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
4262 {
4263 	struct ieee80211_sub_if_data *sdata;
4264 	struct cfg80211_chan_def chandef;
4265 
4266 	/* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
4267 	lockdep_assert_wiphy(local->hw.wiphy);
4268 
4269 	mutex_lock(&local->mtx);
4270 	list_for_each_entry(sdata, &local->interfaces, list) {
4271 		/* it might be waiting for the local->mtx, but then
4272 		 * by the time it gets it, sdata->wdev.cac_started
4273 		 * will no longer be true
4274 		 */
4275 		cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
4276 
4277 		if (sdata->wdev.cac_started) {
4278 			chandef = sdata->vif.bss_conf.chandef;
4279 			ieee80211_link_release_channel(&sdata->deflink);
4280 			cfg80211_cac_event(sdata->dev,
4281 					   &chandef,
4282 					   NL80211_RADAR_CAC_ABORTED,
4283 					   GFP_KERNEL);
4284 		}
4285 	}
4286 	mutex_unlock(&local->mtx);
4287 }
4288 
4289 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
4290 {
4291 	struct ieee80211_local *local =
4292 		container_of(work, struct ieee80211_local, radar_detected_work);
4293 	struct cfg80211_chan_def chandef = local->hw.conf.chandef;
4294 	struct ieee80211_chanctx *ctx;
4295 	int num_chanctx = 0;
4296 
4297 	mutex_lock(&local->chanctx_mtx);
4298 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4299 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
4300 			continue;
4301 
4302 		num_chanctx++;
4303 		chandef = ctx->conf.def;
4304 	}
4305 	mutex_unlock(&local->chanctx_mtx);
4306 
4307 	wiphy_lock(local->hw.wiphy);
4308 	ieee80211_dfs_cac_cancel(local);
4309 	wiphy_unlock(local->hw.wiphy);
4310 
4311 	if (num_chanctx > 1)
4312 		/* XXX: multi-channel is not supported yet */
4313 		WARN_ON(1);
4314 	else
4315 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
4316 }
4317 
4318 void ieee80211_radar_detected(struct ieee80211_hw *hw)
4319 {
4320 	struct ieee80211_local *local = hw_to_local(hw);
4321 
4322 	trace_api_radar_detected(local);
4323 
4324 	schedule_work(&local->radar_detected_work);
4325 }
4326 EXPORT_SYMBOL(ieee80211_radar_detected);
4327 
4328 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
4329 {
4330 	ieee80211_conn_flags_t ret;
4331 	int tmp;
4332 
4333 	switch (c->width) {
4334 	case NL80211_CHAN_WIDTH_20:
4335 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
4336 		ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4337 		break;
4338 	case NL80211_CHAN_WIDTH_40:
4339 		c->width = NL80211_CHAN_WIDTH_20;
4340 		c->center_freq1 = c->chan->center_freq;
4341 		ret = IEEE80211_CONN_DISABLE_40MHZ |
4342 		      IEEE80211_CONN_DISABLE_VHT;
4343 		break;
4344 	case NL80211_CHAN_WIDTH_80:
4345 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
4346 		/* n_P40 */
4347 		tmp /= 2;
4348 		/* freq_P40 */
4349 		c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
4350 		c->width = NL80211_CHAN_WIDTH_40;
4351 		ret = IEEE80211_CONN_DISABLE_VHT;
4352 		break;
4353 	case NL80211_CHAN_WIDTH_80P80:
4354 		c->center_freq2 = 0;
4355 		c->width = NL80211_CHAN_WIDTH_80;
4356 		ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4357 		      IEEE80211_CONN_DISABLE_160MHZ;
4358 		break;
4359 	case NL80211_CHAN_WIDTH_160:
4360 		/* n_P20 */
4361 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
4362 		/* n_P80 */
4363 		tmp /= 4;
4364 		c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
4365 		c->width = NL80211_CHAN_WIDTH_80;
4366 		ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4367 		      IEEE80211_CONN_DISABLE_160MHZ;
4368 		break;
4369 	case NL80211_CHAN_WIDTH_320:
4370 		/* n_P20 */
4371 		tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
4372 		/* n_P160 */
4373 		tmp /= 8;
4374 		c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
4375 		c->width = NL80211_CHAN_WIDTH_160;
4376 		ret = IEEE80211_CONN_DISABLE_320MHZ;
4377 		break;
4378 	default:
4379 	case NL80211_CHAN_WIDTH_20_NOHT:
4380 		WARN_ON_ONCE(1);
4381 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
4382 		ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4383 		break;
4384 	case NL80211_CHAN_WIDTH_1:
4385 	case NL80211_CHAN_WIDTH_2:
4386 	case NL80211_CHAN_WIDTH_4:
4387 	case NL80211_CHAN_WIDTH_8:
4388 	case NL80211_CHAN_WIDTH_16:
4389 	case NL80211_CHAN_WIDTH_5:
4390 	case NL80211_CHAN_WIDTH_10:
4391 		WARN_ON_ONCE(1);
4392 		/* keep c->width */
4393 		ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4394 		break;
4395 	}
4396 
4397 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
4398 
4399 	return ret;
4400 }
4401 
4402 /*
4403  * Returns true if smps_mode_new is strictly more restrictive than
4404  * smps_mode_old.
4405  */
4406 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
4407 				   enum ieee80211_smps_mode smps_mode_new)
4408 {
4409 	if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
4410 			 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
4411 		return false;
4412 
4413 	switch (smps_mode_old) {
4414 	case IEEE80211_SMPS_STATIC:
4415 		return false;
4416 	case IEEE80211_SMPS_DYNAMIC:
4417 		return smps_mode_new == IEEE80211_SMPS_STATIC;
4418 	case IEEE80211_SMPS_OFF:
4419 		return smps_mode_new != IEEE80211_SMPS_OFF;
4420 	default:
4421 		WARN_ON(1);
4422 	}
4423 
4424 	return false;
4425 }
4426 
4427 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
4428 			      struct cfg80211_csa_settings *csa_settings)
4429 {
4430 	struct sk_buff *skb;
4431 	struct ieee80211_mgmt *mgmt;
4432 	struct ieee80211_local *local = sdata->local;
4433 	int freq;
4434 	int hdr_len = offsetofend(struct ieee80211_mgmt,
4435 				  u.action.u.chan_switch);
4436 	u8 *pos;
4437 
4438 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4439 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4440 		return -EOPNOTSUPP;
4441 
4442 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
4443 			    5 + /* channel switch announcement element */
4444 			    3 + /* secondary channel offset element */
4445 			    5 + /* wide bandwidth channel switch announcement */
4446 			    8); /* mesh channel switch parameters element */
4447 	if (!skb)
4448 		return -ENOMEM;
4449 
4450 	skb_reserve(skb, local->tx_headroom);
4451 	mgmt = skb_put_zero(skb, hdr_len);
4452 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4453 					  IEEE80211_STYPE_ACTION);
4454 
4455 	eth_broadcast_addr(mgmt->da);
4456 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
4457 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4458 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
4459 	} else {
4460 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4461 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
4462 	}
4463 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
4464 	mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
4465 	pos = skb_put(skb, 5);
4466 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
4467 	*pos++ = 3;						/* IE length */
4468 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
4469 	freq = csa_settings->chandef.chan->center_freq;
4470 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
4471 	*pos++ = csa_settings->count;				/* count */
4472 
4473 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
4474 		enum nl80211_channel_type ch_type;
4475 
4476 		skb_put(skb, 3);
4477 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
4478 		*pos++ = 1;					/* IE length */
4479 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
4480 		if (ch_type == NL80211_CHAN_HT40PLUS)
4481 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
4482 		else
4483 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
4484 	}
4485 
4486 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4487 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4488 
4489 		skb_put(skb, 8);
4490 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
4491 		*pos++ = 6;					/* IE length */
4492 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
4493 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
4494 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
4495 		*pos++ |= csa_settings->block_tx ?
4496 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
4497 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
4498 		pos += 2;
4499 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
4500 		pos += 2;
4501 	}
4502 
4503 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
4504 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
4505 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
4506 		skb_put(skb, 5);
4507 		ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
4508 	}
4509 
4510 	ieee80211_tx_skb(sdata, skb);
4511 	return 0;
4512 }
4513 
4514 static bool
4515 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
4516 {
4517 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
4518 	int skip;
4519 
4520 	if (end > 0)
4521 		return false;
4522 
4523 	/* One shot NOA  */
4524 	if (data->count[i] == 1)
4525 		return false;
4526 
4527 	if (data->desc[i].interval == 0)
4528 		return false;
4529 
4530 	/* End time is in the past, check for repetitions */
4531 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
4532 	if (data->count[i] < 255) {
4533 		if (data->count[i] <= skip) {
4534 			data->count[i] = 0;
4535 			return false;
4536 		}
4537 
4538 		data->count[i] -= skip;
4539 	}
4540 
4541 	data->desc[i].start += skip * data->desc[i].interval;
4542 
4543 	return true;
4544 }
4545 
4546 static bool
4547 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
4548 			     s32 *offset)
4549 {
4550 	bool ret = false;
4551 	int i;
4552 
4553 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4554 		s32 cur;
4555 
4556 		if (!data->count[i])
4557 			continue;
4558 
4559 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
4560 			ret = true;
4561 
4562 		cur = data->desc[i].start - tsf;
4563 		if (cur > *offset)
4564 			continue;
4565 
4566 		cur = data->desc[i].start + data->desc[i].duration - tsf;
4567 		if (cur > *offset)
4568 			*offset = cur;
4569 	}
4570 
4571 	return ret;
4572 }
4573 
4574 static u32
4575 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
4576 {
4577 	s32 offset = 0;
4578 	int tries = 0;
4579 	/*
4580 	 * arbitrary limit, used to avoid infinite loops when combined NoA
4581 	 * descriptors cover the full time period.
4582 	 */
4583 	int max_tries = 5;
4584 
4585 	ieee80211_extend_absent_time(data, tsf, &offset);
4586 	do {
4587 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
4588 			break;
4589 
4590 		tries++;
4591 	} while (tries < max_tries);
4592 
4593 	return offset;
4594 }
4595 
4596 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4597 {
4598 	u32 next_offset = BIT(31) - 1;
4599 	int i;
4600 
4601 	data->absent = 0;
4602 	data->has_next_tsf = false;
4603 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4604 		s32 start;
4605 
4606 		if (!data->count[i])
4607 			continue;
4608 
4609 		ieee80211_extend_noa_desc(data, tsf, i);
4610 		start = data->desc[i].start - tsf;
4611 		if (start <= 0)
4612 			data->absent |= BIT(i);
4613 
4614 		if (next_offset > start)
4615 			next_offset = start;
4616 
4617 		data->has_next_tsf = true;
4618 	}
4619 
4620 	if (data->absent)
4621 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
4622 
4623 	data->next_tsf = tsf + next_offset;
4624 }
4625 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4626 
4627 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4628 			    struct ieee80211_noa_data *data, u32 tsf)
4629 {
4630 	int ret = 0;
4631 	int i;
4632 
4633 	memset(data, 0, sizeof(*data));
4634 
4635 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4636 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4637 
4638 		if (!desc->count || !desc->duration)
4639 			continue;
4640 
4641 		data->count[i] = desc->count;
4642 		data->desc[i].start = le32_to_cpu(desc->start_time);
4643 		data->desc[i].duration = le32_to_cpu(desc->duration);
4644 		data->desc[i].interval = le32_to_cpu(desc->interval);
4645 
4646 		if (data->count[i] > 1 &&
4647 		    data->desc[i].interval < data->desc[i].duration)
4648 			continue;
4649 
4650 		ieee80211_extend_noa_desc(data, tsf, i);
4651 		ret++;
4652 	}
4653 
4654 	if (ret)
4655 		ieee80211_update_p2p_noa(data, tsf);
4656 
4657 	return ret;
4658 }
4659 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4660 
4661 void ieee80211_recalc_dtim(struct ieee80211_local *local,
4662 			   struct ieee80211_sub_if_data *sdata)
4663 {
4664 	u64 tsf = drv_get_tsf(local, sdata);
4665 	u64 dtim_count = 0;
4666 	u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4667 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4668 	struct ps_data *ps;
4669 	u8 bcns_from_dtim;
4670 
4671 	if (tsf == -1ULL || !beacon_int || !dtim_period)
4672 		return;
4673 
4674 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4675 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4676 		if (!sdata->bss)
4677 			return;
4678 
4679 		ps = &sdata->bss->ps;
4680 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4681 		ps = &sdata->u.mesh.ps;
4682 	} else {
4683 		return;
4684 	}
4685 
4686 	/*
4687 	 * actually finds last dtim_count, mac80211 will update in
4688 	 * __beacon_add_tim().
4689 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4690 	 */
4691 	do_div(tsf, beacon_int);
4692 	bcns_from_dtim = do_div(tsf, dtim_period);
4693 	/* just had a DTIM */
4694 	if (!bcns_from_dtim)
4695 		dtim_count = 0;
4696 	else
4697 		dtim_count = dtim_period - bcns_from_dtim;
4698 
4699 	ps->dtim_count = dtim_count;
4700 }
4701 
4702 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4703 					 struct ieee80211_chanctx *ctx)
4704 {
4705 	struct ieee80211_link_data *link;
4706 	u8 radar_detect = 0;
4707 
4708 	lockdep_assert_held(&local->chanctx_mtx);
4709 
4710 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4711 		return 0;
4712 
4713 	list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
4714 		if (link->reserved_radar_required)
4715 			radar_detect |= BIT(link->reserved_chandef.width);
4716 
4717 	/*
4718 	 * An in-place reservation context should not have any assigned vifs
4719 	 * until it replaces the other context.
4720 	 */
4721 	WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
4722 		!list_empty(&ctx->assigned_links));
4723 
4724 	list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
4725 		if (!link->radar_required)
4726 			continue;
4727 
4728 		radar_detect |=
4729 			BIT(link->conf->chandef.width);
4730 	}
4731 
4732 	return radar_detect;
4733 }
4734 
4735 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4736 				 const struct cfg80211_chan_def *chandef,
4737 				 enum ieee80211_chanctx_mode chanmode,
4738 				 u8 radar_detect)
4739 {
4740 	struct ieee80211_local *local = sdata->local;
4741 	struct ieee80211_sub_if_data *sdata_iter;
4742 	enum nl80211_iftype iftype = sdata->wdev.iftype;
4743 	struct ieee80211_chanctx *ctx;
4744 	int total = 1;
4745 	struct iface_combination_params params = {
4746 		.radar_detect = radar_detect,
4747 	};
4748 
4749 	lockdep_assert_held(&local->chanctx_mtx);
4750 
4751 	if (WARN_ON(hweight32(radar_detect) > 1))
4752 		return -EINVAL;
4753 
4754 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4755 		    !chandef->chan))
4756 		return -EINVAL;
4757 
4758 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4759 		return -EINVAL;
4760 
4761 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4762 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4763 		/*
4764 		 * always passing this is harmless, since it'll be the
4765 		 * same value that cfg80211 finds if it finds the same
4766 		 * interface ... and that's always allowed
4767 		 */
4768 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4769 	}
4770 
4771 	/* Always allow software iftypes */
4772 	if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4773 		if (radar_detect)
4774 			return -EINVAL;
4775 		return 0;
4776 	}
4777 
4778 	if (chandef)
4779 		params.num_different_channels = 1;
4780 
4781 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4782 		params.iftype_num[iftype] = 1;
4783 
4784 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4785 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4786 			continue;
4787 		params.radar_detect |=
4788 			ieee80211_chanctx_radar_detect(local, ctx);
4789 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
4790 			params.num_different_channels++;
4791 			continue;
4792 		}
4793 		if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4794 		    cfg80211_chandef_compatible(chandef,
4795 						&ctx->conf.def))
4796 			continue;
4797 		params.num_different_channels++;
4798 	}
4799 
4800 	list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
4801 		struct wireless_dev *wdev_iter;
4802 
4803 		wdev_iter = &sdata_iter->wdev;
4804 
4805 		if (sdata_iter == sdata ||
4806 		    !ieee80211_sdata_running(sdata_iter) ||
4807 		    cfg80211_iftype_allowed(local->hw.wiphy,
4808 					    wdev_iter->iftype, 0, 1))
4809 			continue;
4810 
4811 		params.iftype_num[wdev_iter->iftype]++;
4812 		total++;
4813 	}
4814 
4815 	if (total == 1 && !params.radar_detect)
4816 		return 0;
4817 
4818 	return cfg80211_check_combinations(local->hw.wiphy, &params);
4819 }
4820 
4821 static void
4822 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4823 			 void *data)
4824 {
4825 	u32 *max_num_different_channels = data;
4826 
4827 	*max_num_different_channels = max(*max_num_different_channels,
4828 					  c->num_different_channels);
4829 }
4830 
4831 int ieee80211_max_num_channels(struct ieee80211_local *local)
4832 {
4833 	struct ieee80211_sub_if_data *sdata;
4834 	struct ieee80211_chanctx *ctx;
4835 	u32 max_num_different_channels = 1;
4836 	int err;
4837 	struct iface_combination_params params = {0};
4838 
4839 	lockdep_assert_held(&local->chanctx_mtx);
4840 
4841 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4842 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4843 			continue;
4844 
4845 		params.num_different_channels++;
4846 
4847 		params.radar_detect |=
4848 			ieee80211_chanctx_radar_detect(local, ctx);
4849 	}
4850 
4851 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
4852 		params.iftype_num[sdata->wdev.iftype]++;
4853 
4854 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
4855 					 ieee80211_iter_max_chans,
4856 					 &max_num_different_channels);
4857 	if (err < 0)
4858 		return err;
4859 
4860 	return max_num_different_channels;
4861 }
4862 
4863 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4864 				struct ieee80211_sta_s1g_cap *caps,
4865 				struct sk_buff *skb)
4866 {
4867 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4868 	struct ieee80211_s1g_cap s1g_capab;
4869 	u8 *pos;
4870 	int i;
4871 
4872 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4873 		return;
4874 
4875 	if (!caps->s1g)
4876 		return;
4877 
4878 	memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4879 	memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4880 
4881 	/* override the capability info */
4882 	for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4883 		u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4884 
4885 		s1g_capab.capab_info[i] &= ~mask;
4886 		s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4887 	}
4888 
4889 	/* then MCS and NSS set */
4890 	for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4891 		u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4892 
4893 		s1g_capab.supp_mcs_nss[i] &= ~mask;
4894 		s1g_capab.supp_mcs_nss[i] |=
4895 			ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4896 	}
4897 
4898 	pos = skb_put(skb, 2 + sizeof(s1g_capab));
4899 	*pos++ = WLAN_EID_S1G_CAPABILITIES;
4900 	*pos++ = sizeof(s1g_capab);
4901 
4902 	memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4903 }
4904 
4905 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4906 				  struct sk_buff *skb)
4907 {
4908 	u8 *pos = skb_put(skb, 3);
4909 
4910 	*pos++ = WLAN_EID_AID_REQUEST;
4911 	*pos++ = 1;
4912 	*pos++ = 0;
4913 }
4914 
4915 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4916 {
4917 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
4918 	*buf++ = 7; /* len */
4919 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4920 	*buf++ = 0x50;
4921 	*buf++ = 0xf2;
4922 	*buf++ = 2; /* WME */
4923 	*buf++ = 0; /* WME info */
4924 	*buf++ = 1; /* WME ver */
4925 	*buf++ = qosinfo; /* U-APSD no in use */
4926 
4927 	return buf;
4928 }
4929 
4930 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4931 			     unsigned long *frame_cnt,
4932 			     unsigned long *byte_cnt)
4933 {
4934 	struct txq_info *txqi = to_txq_info(txq);
4935 	u32 frag_cnt = 0, frag_bytes = 0;
4936 	struct sk_buff *skb;
4937 
4938 	skb_queue_walk(&txqi->frags, skb) {
4939 		frag_cnt++;
4940 		frag_bytes += skb->len;
4941 	}
4942 
4943 	if (frame_cnt)
4944 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4945 
4946 	if (byte_cnt)
4947 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4948 }
4949 EXPORT_SYMBOL(ieee80211_txq_get_depth);
4950 
4951 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4952 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4953 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4954 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4955 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4956 };
4957 
4958 u16 ieee80211_encode_usf(int listen_interval)
4959 {
4960 	static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
4961 	u16 ui, usf = 0;
4962 
4963 	/* find greatest USF */
4964 	while (usf < IEEE80211_MAX_USF) {
4965 		if (listen_interval % listen_int_usf[usf + 1])
4966 			break;
4967 		usf += 1;
4968 	}
4969 	ui = listen_interval / listen_int_usf[usf];
4970 
4971 	/* error if there is a remainder. Should've been checked by user */
4972 	WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
4973 	listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
4974 			  FIELD_PREP(LISTEN_INT_UI, ui);
4975 
4976 	return (u16) listen_interval;
4977 }
4978 
4979 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
4980 {
4981 	const struct ieee80211_sta_he_cap *he_cap;
4982 	const struct ieee80211_sta_eht_cap *eht_cap;
4983 	struct ieee80211_supported_band *sband;
4984 	bool is_ap;
4985 	u8 n;
4986 
4987 	sband = ieee80211_get_sband(sdata);
4988 	if (!sband)
4989 		return 0;
4990 
4991 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
4992 	eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
4993 	if (!he_cap || !eht_cap)
4994 		return 0;
4995 
4996 	is_ap = iftype == NL80211_IFTYPE_AP ||
4997 		iftype == NL80211_IFTYPE_P2P_GO;
4998 
4999 	n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
5000 				       &eht_cap->eht_cap_elem,
5001 				       is_ap);
5002 	return 2 + 1 +
5003 	       sizeof(eht_cap->eht_cap_elem) + n +
5004 	       ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
5005 				      eht_cap->eht_cap_elem.phy_cap_info);
5006 	return 0;
5007 }
5008 
5009 u8 *ieee80211_ie_build_eht_cap(u8 *pos,
5010 			       const struct ieee80211_sta_he_cap *he_cap,
5011 			       const struct ieee80211_sta_eht_cap *eht_cap,
5012 			       u8 *end,
5013 			       bool for_ap)
5014 {
5015 	u8 mcs_nss_len, ppet_len;
5016 	u8 ie_len;
5017 	u8 *orig_pos = pos;
5018 
5019 	/* Make sure we have place for the IE */
5020 	if (!he_cap || !eht_cap)
5021 		return orig_pos;
5022 
5023 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
5024 						 &eht_cap->eht_cap_elem,
5025 						 for_ap);
5026 	ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
5027 					  eht_cap->eht_cap_elem.phy_cap_info);
5028 
5029 	ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
5030 	if ((end - pos) < ie_len)
5031 		return orig_pos;
5032 
5033 	*pos++ = WLAN_EID_EXTENSION;
5034 	*pos++ = ie_len - 2;
5035 	*pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
5036 
5037 	/* Fixed data */
5038 	memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
5039 	pos += sizeof(eht_cap->eht_cap_elem);
5040 
5041 	memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
5042 	pos += mcs_nss_len;
5043 
5044 	if (ppet_len) {
5045 		memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
5046 		pos += ppet_len;
5047 	}
5048 
5049 	return pos;
5050 }
5051 
5052 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos)
5053 {
5054 	unsigned int elem_len;
5055 
5056 	if (!len_pos)
5057 		return;
5058 
5059 	elem_len = skb->data + skb->len - len_pos - 1;
5060 
5061 	while (elem_len > 255) {
5062 		/* this one is 255 */
5063 		*len_pos = 255;
5064 		/* remaining data gets smaller */
5065 		elem_len -= 255;
5066 		/* make space for the fragment ID/len in SKB */
5067 		skb_put(skb, 2);
5068 		/* shift back the remaining data to place fragment ID/len */
5069 		memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
5070 		/* place the fragment ID */
5071 		len_pos += 255 + 1;
5072 		*len_pos = WLAN_EID_FRAGMENT;
5073 		/* and point to fragment length to update later */
5074 		len_pos++;
5075 	}
5076 
5077 	*len_pos = elem_len;
5078 }
5079