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