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