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