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