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