~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mac80211/util.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php