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