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