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