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