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