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