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