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