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