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