1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright (C) 2015-2017 Intel Deutschland GmbH
9 * Copyright (C) 2018-2022 Intel Corporation
11 * utilities for mac80211
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>
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
36 /* privid for wiphys to determine whether they belong to us or not */
37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
41 struct ieee80211_local *local;
43 local = wiphy_priv(wiphy);
46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
49 enum nl80211_iftype type)
51 __le16 fc = hdr->frame_control;
53 if (ieee80211_is_data(fc)) {
54 if (len < 24) /* drop incorrect hdr len (data) */
57 if (ieee80211_has_a4(fc))
59 if (ieee80211_has_tods(fc))
61 if (ieee80211_has_fromds(fc))
67 if (ieee80211_is_s1g_beacon(fc)) {
68 struct ieee80211_ext *ext = (void *) hdr;
70 return ext->u.s1g_beacon.sa;
73 if (ieee80211_is_mgmt(fc)) {
74 if (len < 24) /* drop incorrect hdr len (mgmt) */
79 if (ieee80211_is_ctl(fc)) {
80 if (ieee80211_is_pspoll(fc))
83 if (ieee80211_is_back_req(fc)) {
85 case NL80211_IFTYPE_STATION:
87 case NL80211_IFTYPE_AP:
88 case NL80211_IFTYPE_AP_VLAN:
91 break; /* fall through to the return */
98 EXPORT_SYMBOL(ieee80211_get_bssid);
100 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
103 struct ieee80211_hdr *hdr;
105 skb_queue_walk(&tx->skbs, skb) {
106 hdr = (struct ieee80211_hdr *) skb->data;
107 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
111 int ieee80211_frame_duration(enum nl80211_band band, size_t len,
112 int rate, int erp, int short_preamble,
117 /* calculate duration (in microseconds, rounded up to next higher
118 * integer if it includes a fractional microsecond) to send frame of
119 * len bytes (does not include FCS) at the given rate. Duration will
122 * rate is in 100 kbps, so divident is multiplied by 10 in the
123 * DIV_ROUND_UP() operations.
125 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
126 * is assumed to be 0 otherwise.
129 if (band == NL80211_BAND_5GHZ || erp) {
133 * N_DBPS = DATARATE x 4
134 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
135 * (16 = SIGNAL time, 6 = tail bits)
136 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
139 * 802.11a - 18.5.2: aSIFSTime = 16 usec
140 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
141 * signal ext = 6 usec
143 dur = 16; /* SIFS + signal ext */
144 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
145 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
147 /* IEEE 802.11-2012 18.3.2.4: all values above are:
148 * * times 4 for 5 MHz
149 * * times 2 for 10 MHz
153 /* rates should already consider the channel bandwidth,
154 * don't apply divisor again.
156 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
157 4 * rate); /* T_SYM x N_SYM */
160 * 802.11b or 802.11g with 802.11b compatibility:
161 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
162 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
164 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
165 * aSIFSTime = 10 usec
166 * aPreambleLength = 144 usec or 72 usec with short preamble
167 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
169 dur = 10; /* aSIFSTime = 10 usec */
170 dur += short_preamble ? (72 + 24) : (144 + 48);
172 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
178 /* Exported duration function for driver use */
179 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
180 struct ieee80211_vif *vif,
181 enum nl80211_band band,
183 struct ieee80211_rate *rate)
185 struct ieee80211_sub_if_data *sdata;
188 bool short_preamble = false;
192 sdata = vif_to_sdata(vif);
193 short_preamble = sdata->vif.bss_conf.use_short_preamble;
194 if (sdata->deflink.operating_11g_mode)
195 erp = rate->flags & IEEE80211_RATE_ERP_G;
196 shift = ieee80211_vif_get_shift(vif);
199 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
200 short_preamble, shift);
202 return cpu_to_le16(dur);
204 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
206 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
207 struct ieee80211_vif *vif, size_t frame_len,
208 const struct ieee80211_tx_info *frame_txctl)
210 struct ieee80211_local *local = hw_to_local(hw);
211 struct ieee80211_rate *rate;
212 struct ieee80211_sub_if_data *sdata;
214 int erp, shift = 0, bitrate;
216 struct ieee80211_supported_band *sband;
218 sband = local->hw.wiphy->bands[frame_txctl->band];
220 short_preamble = false;
222 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
226 sdata = vif_to_sdata(vif);
227 short_preamble = sdata->vif.bss_conf.use_short_preamble;
228 if (sdata->deflink.operating_11g_mode)
229 erp = rate->flags & IEEE80211_RATE_ERP_G;
230 shift = ieee80211_vif_get_shift(vif);
233 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
236 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
237 erp, short_preamble, shift);
238 /* Data frame duration */
239 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
240 erp, short_preamble, shift);
242 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
243 erp, short_preamble, shift);
245 return cpu_to_le16(dur);
247 EXPORT_SYMBOL(ieee80211_rts_duration);
249 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
250 struct ieee80211_vif *vif,
252 const struct ieee80211_tx_info *frame_txctl)
254 struct ieee80211_local *local = hw_to_local(hw);
255 struct ieee80211_rate *rate;
256 struct ieee80211_sub_if_data *sdata;
258 int erp, shift = 0, bitrate;
260 struct ieee80211_supported_band *sband;
262 sband = local->hw.wiphy->bands[frame_txctl->band];
264 short_preamble = false;
266 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
269 sdata = vif_to_sdata(vif);
270 short_preamble = sdata->vif.bss_conf.use_short_preamble;
271 if (sdata->deflink.operating_11g_mode)
272 erp = rate->flags & IEEE80211_RATE_ERP_G;
273 shift = ieee80211_vif_get_shift(vif);
276 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
278 /* Data frame duration */
279 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
280 erp, short_preamble, shift);
281 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
283 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
284 erp, short_preamble, shift);
287 return cpu_to_le16(dur);
289 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
291 static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
293 struct ieee80211_local *local = sdata->local;
294 struct ieee80211_vif *vif = &sdata->vif;
295 struct fq *fq = &local->fq;
296 struct ps_data *ps = NULL;
297 struct txq_info *txqi;
298 struct sta_info *sta;
302 spin_lock(&fq->lock);
304 if (!test_bit(SDATA_STATE_RUNNING, &sdata->state))
307 if (sdata->vif.type == NL80211_IFTYPE_AP)
308 ps = &sdata->bss->ps;
310 list_for_each_entry_rcu(sta, &local->sta_list, list) {
311 if (sdata != sta->sdata)
314 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
315 struct ieee80211_txq *txq = sta->sta.txq[i];
320 txqi = to_txq_info(txq);
325 if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY,
329 spin_unlock(&fq->lock);
330 drv_wake_tx_queue(local, txqi);
331 spin_lock(&fq->lock);
338 txqi = to_txq_info(vif->txq);
340 if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY, &txqi->flags) ||
341 (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
344 spin_unlock(&fq->lock);
346 drv_wake_tx_queue(local, txqi);
350 spin_unlock(&fq->lock);
355 __releases(&local->queue_stop_reason_lock)
356 __acquires(&local->queue_stop_reason_lock)
357 _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
359 struct ieee80211_sub_if_data *sdata;
360 int n_acs = IEEE80211_NUM_ACS;
365 if (local->hw.queues < IEEE80211_NUM_ACS)
368 for (i = 0; i < local->hw.queues; i++) {
369 if (local->queue_stop_reasons[i])
372 spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
373 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
376 for (ac = 0; ac < n_acs; ac++) {
377 int ac_queue = sdata->vif.hw_queue[ac];
380 sdata->vif.cab_queue == i)
381 __ieee80211_wake_txqs(sdata, ac);
384 spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
390 void ieee80211_wake_txqs(struct tasklet_struct *t)
392 struct ieee80211_local *local = from_tasklet(local, t,
396 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
397 _ieee80211_wake_txqs(local, &flags);
398 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
401 void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
403 struct ieee80211_sub_if_data *sdata;
404 int n_acs = IEEE80211_NUM_ACS;
406 if (local->ops->wake_tx_queue)
409 if (local->hw.queues < IEEE80211_NUM_ACS)
412 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
418 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
419 local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
422 for (ac = 0; ac < n_acs; ac++) {
423 int ac_queue = sdata->vif.hw_queue[ac];
425 if (ac_queue == queue ||
426 (sdata->vif.cab_queue == queue &&
427 local->queue_stop_reasons[ac_queue] == 0 &&
428 skb_queue_empty(&local->pending[ac_queue])))
429 netif_wake_subqueue(sdata->dev, ac);
434 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
435 enum queue_stop_reason reason,
437 unsigned long *flags)
439 struct ieee80211_local *local = hw_to_local(hw);
441 trace_wake_queue(local, queue, reason);
443 if (WARN_ON(queue >= hw->queues))
446 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
450 local->q_stop_reasons[queue][reason] = 0;
452 local->q_stop_reasons[queue][reason]--;
453 if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
454 local->q_stop_reasons[queue][reason] = 0;
457 if (local->q_stop_reasons[queue][reason] == 0)
458 __clear_bit(reason, &local->queue_stop_reasons[queue]);
460 if (local->queue_stop_reasons[queue] != 0)
461 /* someone still has this queue stopped */
464 if (skb_queue_empty(&local->pending[queue])) {
466 ieee80211_propagate_queue_wake(local, queue);
469 tasklet_schedule(&local->tx_pending_tasklet);
472 * Calling _ieee80211_wake_txqs here can be a problem because it may
473 * release queue_stop_reason_lock which has been taken by
474 * __ieee80211_wake_queue's caller. It is certainly not very nice to
475 * release someone's lock, but it is fine because all the callers of
476 * __ieee80211_wake_queue call it right before releasing the lock.
478 if (local->ops->wake_tx_queue) {
479 if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
480 tasklet_schedule(&local->wake_txqs_tasklet);
482 _ieee80211_wake_txqs(local, flags);
486 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
487 enum queue_stop_reason reason,
490 struct ieee80211_local *local = hw_to_local(hw);
493 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
494 __ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
495 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
498 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
500 ieee80211_wake_queue_by_reason(hw, queue,
501 IEEE80211_QUEUE_STOP_REASON_DRIVER,
504 EXPORT_SYMBOL(ieee80211_wake_queue);
506 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
507 enum queue_stop_reason reason,
510 struct ieee80211_local *local = hw_to_local(hw);
511 struct ieee80211_sub_if_data *sdata;
512 int n_acs = IEEE80211_NUM_ACS;
514 trace_stop_queue(local, queue, reason);
516 if (WARN_ON(queue >= hw->queues))
520 local->q_stop_reasons[queue][reason] = 1;
522 local->q_stop_reasons[queue][reason]++;
524 if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
527 if (local->hw.queues < IEEE80211_NUM_ACS)
531 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
537 for (ac = 0; ac < n_acs; ac++) {
538 if (!local->ops->wake_tx_queue &&
539 (sdata->vif.hw_queue[ac] == queue ||
540 sdata->vif.cab_queue == queue))
541 netif_stop_subqueue(sdata->dev, ac);
547 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
548 enum queue_stop_reason reason,
551 struct ieee80211_local *local = hw_to_local(hw);
554 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
555 __ieee80211_stop_queue(hw, queue, reason, refcounted);
556 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
559 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
561 ieee80211_stop_queue_by_reason(hw, queue,
562 IEEE80211_QUEUE_STOP_REASON_DRIVER,
565 EXPORT_SYMBOL(ieee80211_stop_queue);
567 void ieee80211_add_pending_skb(struct ieee80211_local *local,
570 struct ieee80211_hw *hw = &local->hw;
572 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
573 int queue = info->hw_queue;
575 if (WARN_ON(!info->control.vif)) {
576 ieee80211_free_txskb(&local->hw, skb);
580 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
581 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
583 __skb_queue_tail(&local->pending[queue], skb);
584 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
586 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
589 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
590 struct sk_buff_head *skbs)
592 struct ieee80211_hw *hw = &local->hw;
597 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
598 while ((skb = skb_dequeue(skbs))) {
599 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
601 if (WARN_ON(!info->control.vif)) {
602 ieee80211_free_txskb(&local->hw, skb);
606 queue = info->hw_queue;
608 __ieee80211_stop_queue(hw, queue,
609 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
612 __skb_queue_tail(&local->pending[queue], skb);
615 for (i = 0; i < hw->queues; i++)
616 __ieee80211_wake_queue(hw, i,
617 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
619 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
622 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
623 unsigned long queues,
624 enum queue_stop_reason reason,
627 struct ieee80211_local *local = hw_to_local(hw);
631 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
633 for_each_set_bit(i, &queues, hw->queues)
634 __ieee80211_stop_queue(hw, i, reason, refcounted);
636 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
639 void ieee80211_stop_queues(struct ieee80211_hw *hw)
641 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
642 IEEE80211_QUEUE_STOP_REASON_DRIVER,
645 EXPORT_SYMBOL(ieee80211_stop_queues);
647 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
649 struct ieee80211_local *local = hw_to_local(hw);
653 if (WARN_ON(queue >= hw->queues))
656 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
657 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
658 &local->queue_stop_reasons[queue]);
659 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
662 EXPORT_SYMBOL(ieee80211_queue_stopped);
664 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
665 unsigned long queues,
666 enum queue_stop_reason reason,
669 struct ieee80211_local *local = hw_to_local(hw);
673 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
675 for_each_set_bit(i, &queues, hw->queues)
676 __ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
678 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
681 void ieee80211_wake_queues(struct ieee80211_hw *hw)
683 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
684 IEEE80211_QUEUE_STOP_REASON_DRIVER,
687 EXPORT_SYMBOL(ieee80211_wake_queues);
690 ieee80211_get_vif_queues(struct ieee80211_local *local,
691 struct ieee80211_sub_if_data *sdata)
695 if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
700 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
701 queues |= BIT(sdata->vif.hw_queue[ac]);
702 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
703 queues |= BIT(sdata->vif.cab_queue);
706 queues = BIT(local->hw.queues) - 1;
712 void __ieee80211_flush_queues(struct ieee80211_local *local,
713 struct ieee80211_sub_if_data *sdata,
714 unsigned int queues, bool drop)
716 if (!local->ops->flush)
720 * If no queue was set, or if the HW doesn't support
721 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
723 if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
724 queues = ieee80211_get_vif_queues(local, sdata);
726 ieee80211_stop_queues_by_reason(&local->hw, queues,
727 IEEE80211_QUEUE_STOP_REASON_FLUSH,
730 drv_flush(local, sdata, queues, drop);
732 ieee80211_wake_queues_by_reason(&local->hw, queues,
733 IEEE80211_QUEUE_STOP_REASON_FLUSH,
737 void ieee80211_flush_queues(struct ieee80211_local *local,
738 struct ieee80211_sub_if_data *sdata, bool drop)
740 __ieee80211_flush_queues(local, sdata, 0, drop);
743 void ieee80211_stop_vif_queues(struct ieee80211_local *local,
744 struct ieee80211_sub_if_data *sdata,
745 enum queue_stop_reason reason)
747 ieee80211_stop_queues_by_reason(&local->hw,
748 ieee80211_get_vif_queues(local, sdata),
752 void ieee80211_wake_vif_queues(struct ieee80211_local *local,
753 struct ieee80211_sub_if_data *sdata,
754 enum queue_stop_reason reason)
756 ieee80211_wake_queues_by_reason(&local->hw,
757 ieee80211_get_vif_queues(local, sdata),
761 static void __iterate_interfaces(struct ieee80211_local *local,
763 void (*iterator)(void *data, u8 *mac,
764 struct ieee80211_vif *vif),
767 struct ieee80211_sub_if_data *sdata;
768 bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
770 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
771 switch (sdata->vif.type) {
772 case NL80211_IFTYPE_MONITOR:
773 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
776 case NL80211_IFTYPE_AP_VLAN:
781 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
782 active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
784 if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) &&
785 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
787 if (ieee80211_sdata_running(sdata) || !active_only)
788 iterator(data, sdata->vif.addr,
792 sdata = rcu_dereference_check(local->monitor_sdata,
793 lockdep_is_held(&local->iflist_mtx) ||
794 lockdep_is_held(&local->hw.wiphy->mtx));
796 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
797 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
798 iterator(data, sdata->vif.addr, &sdata->vif);
801 void ieee80211_iterate_interfaces(
802 struct ieee80211_hw *hw, u32 iter_flags,
803 void (*iterator)(void *data, u8 *mac,
804 struct ieee80211_vif *vif),
807 struct ieee80211_local *local = hw_to_local(hw);
809 mutex_lock(&local->iflist_mtx);
810 __iterate_interfaces(local, iter_flags, iterator, data);
811 mutex_unlock(&local->iflist_mtx);
813 EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
815 void ieee80211_iterate_active_interfaces_atomic(
816 struct ieee80211_hw *hw, u32 iter_flags,
817 void (*iterator)(void *data, u8 *mac,
818 struct ieee80211_vif *vif),
821 struct ieee80211_local *local = hw_to_local(hw);
824 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
828 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
830 void ieee80211_iterate_active_interfaces_mtx(
831 struct ieee80211_hw *hw, u32 iter_flags,
832 void (*iterator)(void *data, u8 *mac,
833 struct ieee80211_vif *vif),
836 struct ieee80211_local *local = hw_to_local(hw);
838 lockdep_assert_wiphy(hw->wiphy);
840 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
843 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx);
845 static void __iterate_stations(struct ieee80211_local *local,
846 void (*iterator)(void *data,
847 struct ieee80211_sta *sta),
850 struct sta_info *sta;
852 list_for_each_entry_rcu(sta, &local->sta_list, list) {
856 iterator(data, &sta->sta);
860 void ieee80211_iterate_stations(struct ieee80211_hw *hw,
861 void (*iterator)(void *data,
862 struct ieee80211_sta *sta),
865 struct ieee80211_local *local = hw_to_local(hw);
867 mutex_lock(&local->sta_mtx);
868 __iterate_stations(local, iterator, data);
869 mutex_unlock(&local->sta_mtx);
871 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations);
873 void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
874 void (*iterator)(void *data,
875 struct ieee80211_sta *sta),
878 struct ieee80211_local *local = hw_to_local(hw);
881 __iterate_stations(local, iterator, data);
884 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
886 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
888 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
890 if (!ieee80211_sdata_running(sdata) ||
891 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
895 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
897 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
902 return &vif_to_sdata(vif)->wdev;
904 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
907 * Nothing should have been stuffed into the workqueue during
908 * the suspend->resume cycle. Since we can't check each caller
909 * of this function if we are already quiescing / suspended,
910 * check here and don't WARN since this can actually happen when
911 * the rx path (for example) is racing against __ieee80211_suspend
912 * and suspending / quiescing was set after the rx path checked
915 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
917 if (local->quiescing || (local->suspended && !local->resuming)) {
918 pr_warn("queueing ieee80211 work while going to suspend\n");
925 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
927 struct ieee80211_local *local = hw_to_local(hw);
929 if (!ieee80211_can_queue_work(local))
932 queue_work(local->workqueue, work);
934 EXPORT_SYMBOL(ieee80211_queue_work);
936 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
937 struct delayed_work *dwork,
940 struct ieee80211_local *local = hw_to_local(hw);
942 if (!ieee80211_can_queue_work(local))
945 queue_delayed_work(local->workqueue, dwork, delay);
947 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
950 ieee80211_parse_extension_element(u32 *crc,
951 const struct element *elem,
952 struct ieee802_11_elems *elems,
953 struct ieee80211_elems_parse_params *params)
955 const void *data = elem->data + 1;
961 len = elem->datalen - 1;
963 switch (elem->data[0]) {
964 case WLAN_EID_EXT_HE_MU_EDCA:
965 if (len >= sizeof(*elems->mu_edca_param_set)) {
966 elems->mu_edca_param_set = data;
968 *crc = crc32_be(*crc, (void *)elem,
972 case WLAN_EID_EXT_HE_CAPABILITY:
973 if (ieee80211_he_capa_size_ok(data, len)) {
974 elems->he_cap = data;
975 elems->he_cap_len = len;
978 case WLAN_EID_EXT_HE_OPERATION:
979 if (len >= sizeof(*elems->he_operation) &&
980 len >= ieee80211_he_oper_size(data) - 1) {
982 *crc = crc32_be(*crc, (void *)elem,
984 elems->he_operation = data;
987 case WLAN_EID_EXT_UORA:
989 elems->uora_element = data;
991 case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
993 elems->max_channel_switch_time = data;
995 case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
996 if (len >= sizeof(*elems->mbssid_config_ie))
997 elems->mbssid_config_ie = data;
999 case WLAN_EID_EXT_HE_SPR:
1000 if (len >= sizeof(*elems->he_spr) &&
1001 len >= ieee80211_he_spr_size(data))
1002 elems->he_spr = data;
1004 case WLAN_EID_EXT_HE_6GHZ_CAPA:
1005 if (len >= sizeof(*elems->he_6ghz_capa))
1006 elems->he_6ghz_capa = data;
1008 case WLAN_EID_EXT_EHT_CAPABILITY:
1009 if (ieee80211_eht_capa_size_ok(elems->he_cap,
1012 elems->eht_cap = data;
1013 elems->eht_cap_len = len;
1016 case WLAN_EID_EXT_EHT_OPERATION:
1017 if (ieee80211_eht_oper_size_ok(data, len))
1018 elems->eht_operation = data;
1020 case WLAN_EID_EXT_EHT_MULTI_LINK:
1021 if (ieee80211_mle_size_ok(data, len))
1022 elems->multi_link = (void *)data;
1028 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
1029 struct ieee802_11_elems *elems,
1030 const struct element *check_inherit)
1032 const struct element *elem;
1033 bool calc_crc = params->filter != 0;
1034 DECLARE_BITMAP(seen_elems, 256);
1035 u32 crc = params->crc;
1038 bitmap_zero(seen_elems, 256);
1040 for_each_element(elem, params->start, params->len) {
1041 bool elem_parse_failed;
1043 u8 elen = elem->datalen;
1044 const u8 *pos = elem->data;
1046 if (check_inherit &&
1047 !cfg80211_is_element_inherited(elem,
1053 case WLAN_EID_SUPP_RATES:
1054 case WLAN_EID_FH_PARAMS:
1055 case WLAN_EID_DS_PARAMS:
1056 case WLAN_EID_CF_PARAMS:
1058 case WLAN_EID_IBSS_PARAMS:
1059 case WLAN_EID_CHALLENGE:
1061 case WLAN_EID_ERP_INFO:
1062 case WLAN_EID_EXT_SUPP_RATES:
1063 case WLAN_EID_HT_CAPABILITY:
1064 case WLAN_EID_HT_OPERATION:
1065 case WLAN_EID_VHT_CAPABILITY:
1066 case WLAN_EID_VHT_OPERATION:
1067 case WLAN_EID_MESH_ID:
1068 case WLAN_EID_MESH_CONFIG:
1069 case WLAN_EID_PEER_MGMT:
1074 case WLAN_EID_CHANNEL_SWITCH:
1075 case WLAN_EID_EXT_CHANSWITCH_ANN:
1076 case WLAN_EID_COUNTRY:
1077 case WLAN_EID_PWR_CONSTRAINT:
1078 case WLAN_EID_TIMEOUT_INTERVAL:
1079 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1080 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1081 case WLAN_EID_CHAN_SWITCH_PARAM:
1082 case WLAN_EID_EXT_CAPABILITY:
1083 case WLAN_EID_CHAN_SWITCH_TIMING:
1084 case WLAN_EID_LINK_ID:
1085 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1087 case WLAN_EID_S1G_BCN_COMPAT:
1088 case WLAN_EID_S1G_CAPABILITIES:
1089 case WLAN_EID_S1G_OPERATION:
1090 case WLAN_EID_AID_RESPONSE:
1091 case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
1093 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1094 * that if the content gets bigger it might be needed more than once
1096 if (test_bit(id, seen_elems)) {
1097 elems->parse_error = true;
1103 if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
1104 crc = crc32_be(crc, pos - 2, elen + 2);
1106 elem_parse_failed = false;
1109 case WLAN_EID_LINK_ID:
1110 if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
1111 elem_parse_failed = true;
1114 elems->lnk_id = (void *)(pos - 2);
1116 case WLAN_EID_CHAN_SWITCH_TIMING:
1117 if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
1118 elem_parse_failed = true;
1121 elems->ch_sw_timing = (void *)pos;
1123 case WLAN_EID_EXT_CAPABILITY:
1124 elems->ext_capab = pos;
1125 elems->ext_capab_len = elen;
1129 elems->ssid_len = elen;
1131 case WLAN_EID_SUPP_RATES:
1132 elems->supp_rates = pos;
1133 elems->supp_rates_len = elen;
1135 case WLAN_EID_DS_PARAMS:
1137 elems->ds_params = pos;
1139 elem_parse_failed = true;
1142 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1143 elems->tim = (void *)pos;
1144 elems->tim_len = elen;
1146 elem_parse_failed = true;
1148 case WLAN_EID_VENDOR_SPECIFIC:
1149 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1151 /* Microsoft OUI (00:50:F2) */
1154 crc = crc32_be(crc, pos - 2, elen + 2);
1156 if (elen >= 5 && pos[3] == 2) {
1157 /* OUI Type 2 - WMM IE */
1159 elems->wmm_info = pos;
1160 elems->wmm_info_len = elen;
1161 } else if (pos[4] == 1) {
1162 elems->wmm_param = pos;
1163 elems->wmm_param_len = elen;
1170 elems->rsn_len = elen;
1172 case WLAN_EID_ERP_INFO:
1174 elems->erp_info = pos;
1176 elem_parse_failed = true;
1178 case WLAN_EID_EXT_SUPP_RATES:
1179 elems->ext_supp_rates = pos;
1180 elems->ext_supp_rates_len = elen;
1182 case WLAN_EID_HT_CAPABILITY:
1183 if (elen >= sizeof(struct ieee80211_ht_cap))
1184 elems->ht_cap_elem = (void *)pos;
1186 elem_parse_failed = true;
1188 case WLAN_EID_HT_OPERATION:
1189 if (elen >= sizeof(struct ieee80211_ht_operation))
1190 elems->ht_operation = (void *)pos;
1192 elem_parse_failed = true;
1194 case WLAN_EID_VHT_CAPABILITY:
1195 if (elen >= sizeof(struct ieee80211_vht_cap))
1196 elems->vht_cap_elem = (void *)pos;
1198 elem_parse_failed = true;
1200 case WLAN_EID_VHT_OPERATION:
1201 if (elen >= sizeof(struct ieee80211_vht_operation)) {
1202 elems->vht_operation = (void *)pos;
1204 crc = crc32_be(crc, pos - 2, elen + 2);
1207 elem_parse_failed = true;
1209 case WLAN_EID_OPMODE_NOTIF:
1211 elems->opmode_notif = pos;
1213 crc = crc32_be(crc, pos - 2, elen + 2);
1216 elem_parse_failed = true;
1218 case WLAN_EID_MESH_ID:
1219 elems->mesh_id = pos;
1220 elems->mesh_id_len = elen;
1222 case WLAN_EID_MESH_CONFIG:
1223 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1224 elems->mesh_config = (void *)pos;
1226 elem_parse_failed = true;
1228 case WLAN_EID_PEER_MGMT:
1229 elems->peering = pos;
1230 elems->peering_len = elen;
1232 case WLAN_EID_MESH_AWAKE_WINDOW:
1234 elems->awake_window = (void *)pos;
1238 elems->preq_len = elen;
1242 elems->prep_len = elen;
1246 elems->perr_len = elen;
1249 if (elen >= sizeof(struct ieee80211_rann_ie))
1250 elems->rann = (void *)pos;
1252 elem_parse_failed = true;
1254 case WLAN_EID_CHANNEL_SWITCH:
1255 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
1256 elem_parse_failed = true;
1259 elems->ch_switch_ie = (void *)pos;
1261 case WLAN_EID_EXT_CHANSWITCH_ANN:
1262 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1263 elem_parse_failed = true;
1266 elems->ext_chansw_ie = (void *)pos;
1268 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1269 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1270 elem_parse_failed = true;
1273 elems->sec_chan_offs = (void *)pos;
1275 case WLAN_EID_CHAN_SWITCH_PARAM:
1277 sizeof(*elems->mesh_chansw_params_ie)) {
1278 elem_parse_failed = true;
1281 elems->mesh_chansw_params_ie = (void *)pos;
1283 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1284 if (!params->action ||
1285 elen < sizeof(*elems->wide_bw_chansw_ie)) {
1286 elem_parse_failed = true;
1289 elems->wide_bw_chansw_ie = (void *)pos;
1291 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1292 if (params->action) {
1293 elem_parse_failed = true;
1297 * This is a bit tricky, but as we only care about
1298 * the wide bandwidth channel switch element, so
1299 * just parse it out manually.
1301 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1304 if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
1305 elems->wide_bw_chansw_ie =
1308 elem_parse_failed = true;
1311 case WLAN_EID_COUNTRY:
1312 elems->country_elem = pos;
1313 elems->country_elem_len = elen;
1315 case WLAN_EID_PWR_CONSTRAINT:
1317 elem_parse_failed = true;
1320 elems->pwr_constr_elem = pos;
1322 case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1323 /* Lots of different options exist, but we only care
1324 * about the Dynamic Transmit Power Control element.
1325 * First check for the Cisco OUI, then for the DTPC
1329 elem_parse_failed = true;
1333 if (pos[0] != 0x00 || pos[1] != 0x40 ||
1334 pos[2] != 0x96 || pos[3] != 0x00)
1338 elem_parse_failed = true;
1343 crc = crc32_be(crc, pos - 2, elen + 2);
1345 elems->cisco_dtpc_elem = pos;
1347 case WLAN_EID_ADDBA_EXT:
1348 if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
1349 elem_parse_failed = true;
1352 elems->addba_ext_ie = (void *)pos;
1354 case WLAN_EID_TIMEOUT_INTERVAL:
1355 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1356 elems->timeout_int = (void *)pos;
1358 elem_parse_failed = true;
1360 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1361 if (elen >= sizeof(*elems->max_idle_period_ie))
1362 elems->max_idle_period_ie = (void *)pos;
1366 elems->rsnx_len = elen;
1368 case WLAN_EID_TX_POWER_ENVELOPE:
1370 elen > sizeof(struct ieee80211_tx_pwr_env))
1373 if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
1376 elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
1377 elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
1378 elems->tx_pwr_env_num++;
1380 case WLAN_EID_EXTENSION:
1381 ieee80211_parse_extension_element(calc_crc ?
1383 elem, elems, params);
1385 case WLAN_EID_S1G_CAPABILITIES:
1386 if (elen >= sizeof(*elems->s1g_capab))
1387 elems->s1g_capab = (void *)pos;
1389 elem_parse_failed = true;
1391 case WLAN_EID_S1G_OPERATION:
1392 if (elen == sizeof(*elems->s1g_oper))
1393 elems->s1g_oper = (void *)pos;
1395 elem_parse_failed = true;
1397 case WLAN_EID_S1G_BCN_COMPAT:
1398 if (elen == sizeof(*elems->s1g_bcn_compat))
1399 elems->s1g_bcn_compat = (void *)pos;
1401 elem_parse_failed = true;
1403 case WLAN_EID_AID_RESPONSE:
1404 if (elen == sizeof(struct ieee80211_aid_response_ie))
1405 elems->aid_resp = (void *)pos;
1407 elem_parse_failed = true;
1413 if (elem_parse_failed)
1414 elems->parse_error = true;
1416 __set_bit(id, seen_elems);
1419 if (!for_each_element_completed(elem, params->start, params->len))
1420 elems->parse_error = true;
1425 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1426 struct ieee802_11_elems *elems,
1427 struct cfg80211_bss *bss,
1428 u8 *nontransmitted_profile)
1430 const struct element *elem, *sub;
1431 size_t profile_len = 0;
1434 if (!bss || !bss->transmitted_bss)
1437 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1438 if (elem->datalen < 2)
1440 if (elem->data[0] < 1 || elem->data[0] > 8)
1443 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1444 u8 new_bssid[ETH_ALEN];
1447 if (sub->id != 0 || sub->datalen < 4) {
1448 /* not a valid BSS profile */
1452 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1453 sub->data[1] != 2) {
1454 /* The first element of the
1455 * Nontransmitted BSSID Profile is not
1456 * the Nontransmitted BSSID Capability
1462 memset(nontransmitted_profile, 0, len);
1463 profile_len = cfg80211_merge_profile(start, len,
1466 nontransmitted_profile,
1469 /* found a Nontransmitted BSSID Profile */
1470 index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
1471 nontransmitted_profile,
1473 if (!index || index[1] < 1 || index[2] == 0) {
1474 /* Invalid MBSSID Index element */
1478 cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
1482 if (ether_addr_equal(new_bssid, bss->bssid)) {
1484 elems->bssid_index_len = index[1];
1485 elems->bssid_index = (void *)&index[2];
1491 return found ? profile_len : 0;
1494 struct ieee802_11_elems *
1495 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
1497 struct ieee802_11_elems *elems;
1498 const struct element *non_inherit = NULL;
1499 u8 *nontransmitted_profile;
1500 int nontransmitted_profile_len = 0;
1501 size_t scratch_len = params->len;
1503 elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1506 elems->ie_start = params->start;
1507 elems->total_len = params->len;
1508 elems->scratch_len = scratch_len;
1509 elems->scratch_pos = elems->scratch;
1511 nontransmitted_profile = elems->scratch_pos;
1512 nontransmitted_profile_len =
1513 ieee802_11_find_bssid_profile(params->start, params->len,
1515 nontransmitted_profile);
1516 elems->scratch_pos += nontransmitted_profile_len;
1517 elems->scratch_len -= nontransmitted_profile_len;
1518 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1519 nontransmitted_profile,
1520 nontransmitted_profile_len);
1522 elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
1524 /* Override with nontransmitted profile, if found */
1525 if (nontransmitted_profile_len) {
1526 struct ieee80211_elems_parse_params sub = {
1527 .start = nontransmitted_profile,
1528 .len = nontransmitted_profile_len,
1529 .action = params->action,
1530 .link_id = params->link_id,
1533 _ieee802_11_parse_elems_full(&sub, elems, NULL);
1536 if (elems->tim && !elems->parse_error) {
1537 const struct ieee80211_tim_ie *tim_ie = elems->tim;
1539 elems->dtim_period = tim_ie->dtim_period;
1540 elems->dtim_count = tim_ie->dtim_count;
1543 /* Override DTIM period and count if needed */
1544 if (elems->bssid_index &&
1545 elems->bssid_index_len >=
1546 offsetofend(struct ieee80211_bssid_index, dtim_period))
1547 elems->dtim_period = elems->bssid_index->dtim_period;
1549 if (elems->bssid_index &&
1550 elems->bssid_index_len >=
1551 offsetofend(struct ieee80211_bssid_index, dtim_count))
1552 elems->dtim_count = elems->bssid_index->dtim_count;
1557 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1558 struct ieee80211_tx_queue_params
1561 struct ieee80211_chanctx_conf *chanctx_conf;
1562 const struct ieee80211_reg_rule *rrule;
1563 const struct ieee80211_wmm_ac *wmm_ac;
1564 u16 center_freq = 0;
1566 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1567 sdata->vif.type != NL80211_IFTYPE_STATION)
1571 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1573 center_freq = chanctx_conf->def.chan->center_freq;
1580 rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1582 if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1587 if (sdata->vif.type == NL80211_IFTYPE_AP)
1588 wmm_ac = &rrule->wmm_rule.ap[ac];
1590 wmm_ac = &rrule->wmm_rule.client[ac];
1591 qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1592 qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1593 qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1594 qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1598 void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
1599 bool bss_notify, bool enable_qos)
1601 struct ieee80211_sub_if_data *sdata = link->sdata;
1602 struct ieee80211_local *local = sdata->local;
1603 struct ieee80211_tx_queue_params qparam;
1604 struct ieee80211_chanctx_conf *chanctx_conf;
1607 bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1610 if (!local->ops->conf_tx)
1613 if (local->hw.queues < IEEE80211_NUM_ACS)
1616 memset(&qparam, 0, sizeof(qparam));
1619 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
1620 use_11b = (chanctx_conf &&
1621 chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1622 !link->operating_11g_mode;
1625 is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1627 /* Set defaults according to 802.11-2007 Table 7-37 */
1634 /* Confiure old 802.11b/g medium access rules. */
1635 qparam.cw_max = aCWmax;
1636 qparam.cw_min = aCWmin;
1640 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1641 /* Update if QoS is enabled. */
1644 case IEEE80211_AC_BK:
1645 qparam.cw_max = aCWmax;
1646 qparam.cw_min = aCWmin;
1653 /* never happens but let's not leave undefined */
1655 case IEEE80211_AC_BE:
1656 qparam.cw_max = aCWmax;
1657 qparam.cw_min = aCWmin;
1664 case IEEE80211_AC_VI:
1665 qparam.cw_max = aCWmin;
1666 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1670 qparam.txop = 6016/32;
1672 qparam.txop = 3008/32;
1679 case IEEE80211_AC_VO:
1680 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1681 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1685 qparam.txop = 3264/32;
1687 qparam.txop = 1504/32;
1692 ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
1694 qparam.uapsd = false;
1696 link->tx_conf[ac] = qparam;
1697 drv_conf_tx(local, link, ac, &qparam);
1700 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1701 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1702 sdata->vif.type != NL80211_IFTYPE_NAN) {
1703 link->conf->qos = enable_qos;
1705 ieee80211_link_info_change_notify(sdata, link,
1710 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1711 u16 transaction, u16 auth_alg, u16 status,
1712 const u8 *extra, size_t extra_len, const u8 *da,
1713 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1716 struct ieee80211_local *local = sdata->local;
1717 struct sk_buff *skb;
1718 struct ieee80211_mgmt *mgmt;
1719 bool multi_link = sdata->vif.valid_links;
1724 struct ieee80211_multi_link_elem ml;
1725 struct ieee80211_mle_basic_common_info basic;
1727 .id = WLAN_EID_EXTENSION,
1728 .len = sizeof(mle) - 2,
1729 .ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
1730 .ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
1731 .basic.len = sizeof(mle.basic),
1735 memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1737 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1738 skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1739 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
1740 multi_link * sizeof(mle));
1744 skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
1746 mgmt = skb_put_zero(skb, 24 + 6);
1747 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1748 IEEE80211_STYPE_AUTH);
1749 memcpy(mgmt->da, da, ETH_ALEN);
1750 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1751 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1752 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1753 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1754 mgmt->u.auth.status_code = cpu_to_le16(status);
1756 skb_put_data(skb, extra, extra_len);
1758 skb_put_data(skb, &mle, sizeof(mle));
1760 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1761 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1762 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1769 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1771 ieee80211_tx_skb(sdata, skb);
1774 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1775 const u8 *da, const u8 *bssid,
1776 u16 stype, u16 reason,
1777 bool send_frame, u8 *frame_buf)
1779 struct ieee80211_local *local = sdata->local;
1780 struct sk_buff *skb;
1781 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1784 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1785 mgmt->duration = 0; /* initialize only */
1786 mgmt->seq_ctrl = 0; /* initialize only */
1787 memcpy(mgmt->da, da, ETH_ALEN);
1788 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1789 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1790 /* u.deauth.reason_code == u.disassoc.reason_code */
1791 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1794 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1795 IEEE80211_DEAUTH_FRAME_LEN);
1799 skb_reserve(skb, local->hw.extra_tx_headroom);
1802 skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1804 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1805 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1806 IEEE80211_SKB_CB(skb)->flags |=
1807 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1809 ieee80211_tx_skb(sdata, skb);
1813 static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
1815 if ((end - pos) < 5)
1818 *pos++ = WLAN_EID_EXTENSION;
1819 *pos++ = 1 + sizeof(cap);
1820 *pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
1821 memcpy(pos, &cap, sizeof(cap));
1826 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
1827 u8 *buffer, size_t buffer_len,
1828 const u8 *ie, size_t ie_len,
1829 enum nl80211_band band,
1831 struct cfg80211_chan_def *chandef,
1832 size_t *offset, u32 flags)
1834 struct ieee80211_local *local = sdata->local;
1835 struct ieee80211_supported_band *sband;
1836 const struct ieee80211_sta_he_cap *he_cap;
1837 const struct ieee80211_sta_eht_cap *eht_cap;
1838 u8 *pos = buffer, *end = buffer + buffer_len;
1840 int supp_rates_len, i;
1846 bool have_80mhz = false;
1850 sband = local->hw.wiphy->bands[band];
1851 if (WARN_ON_ONCE(!sband))
1854 rate_flags = ieee80211_chandef_rate_flags(chandef);
1855 shift = ieee80211_chandef_get_shift(chandef);
1858 for (i = 0; i < sband->n_bitrates; i++) {
1859 if ((BIT(i) & rate_mask) == 0)
1860 continue; /* skip rate */
1861 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1864 rates[num_rates++] =
1865 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1869 supp_rates_len = min_t(int, num_rates, 8);
1871 if (end - pos < 2 + supp_rates_len)
1873 *pos++ = WLAN_EID_SUPP_RATES;
1874 *pos++ = supp_rates_len;
1875 memcpy(pos, rates, supp_rates_len);
1876 pos += supp_rates_len;
1878 /* insert "request information" if in custom IEs */
1880 static const u8 before_extrates[] = {
1882 WLAN_EID_SUPP_RATES,
1885 noffset = ieee80211_ie_split(ie, ie_len,
1887 ARRAY_SIZE(before_extrates),
1889 if (end - pos < noffset - *offset)
1891 memcpy(pos, ie + *offset, noffset - *offset);
1892 pos += noffset - *offset;
1896 ext_rates_len = num_rates - supp_rates_len;
1897 if (ext_rates_len > 0) {
1898 if (end - pos < 2 + ext_rates_len)
1900 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1901 *pos++ = ext_rates_len;
1902 memcpy(pos, rates + supp_rates_len, ext_rates_len);
1903 pos += ext_rates_len;
1906 if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
1909 *pos++ = WLAN_EID_DS_PARAMS;
1911 *pos++ = ieee80211_frequency_to_channel(
1912 chandef->chan->center_freq);
1915 if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
1918 /* insert custom IEs that go before HT */
1920 static const u8 before_ht[] = {
1922 * no need to list the ones split off already
1923 * (or generated here)
1926 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1928 noffset = ieee80211_ie_split(ie, ie_len,
1929 before_ht, ARRAY_SIZE(before_ht),
1931 if (end - pos < noffset - *offset)
1933 memcpy(pos, ie + *offset, noffset - *offset);
1934 pos += noffset - *offset;
1938 if (sband->ht_cap.ht_supported) {
1939 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1941 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1945 /* insert custom IEs that go before VHT */
1947 static const u8 before_vht[] = {
1949 * no need to list the ones split off already
1950 * (or generated here)
1952 WLAN_EID_BSS_COEX_2040,
1953 WLAN_EID_EXT_CAPABILITY,
1955 WLAN_EID_CHANNEL_USAGE,
1956 WLAN_EID_INTERWORKING,
1958 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
1960 noffset = ieee80211_ie_split(ie, ie_len,
1961 before_vht, ARRAY_SIZE(before_vht),
1963 if (end - pos < noffset - *offset)
1965 memcpy(pos, ie + *offset, noffset - *offset);
1966 pos += noffset - *offset;
1970 /* Check if any channel in this sband supports at least 80 MHz */
1971 for (i = 0; i < sband->n_channels; i++) {
1972 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
1973 IEEE80211_CHAN_NO_80MHZ))
1980 if (sband->vht_cap.vht_supported && have_80mhz) {
1981 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1983 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1984 sband->vht_cap.cap);
1987 /* insert custom IEs that go before HE */
1989 static const u8 before_he[] = {
1991 * no need to list the ones split off before VHT
1994 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
1996 /* TODO: add 11ah/11aj/11ak elements */
1998 noffset = ieee80211_ie_split(ie, ie_len,
1999 before_he, ARRAY_SIZE(before_he),
2001 if (end - pos < noffset - *offset)
2003 memcpy(pos, ie + *offset, noffset - *offset);
2004 pos += noffset - *offset;
2008 he_cap = ieee80211_get_he_iftype_cap(sband,
2009 ieee80211_vif_type_p2p(&sdata->vif));
2011 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2012 IEEE80211_CHAN_NO_HE)) {
2013 pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
2018 eht_cap = ieee80211_get_eht_iftype_cap(sband,
2019 ieee80211_vif_type_p2p(&sdata->vif));
2022 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2023 IEEE80211_CHAN_NO_HE |
2024 IEEE80211_CHAN_NO_EHT)) {
2025 pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
2026 sdata->vif.type == NL80211_IFTYPE_AP);
2031 if (cfg80211_any_usable_channels(local->hw.wiphy,
2032 BIT(NL80211_BAND_6GHZ),
2033 IEEE80211_CHAN_NO_HE)) {
2034 struct ieee80211_supported_band *sband6;
2036 sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2037 he_cap = ieee80211_get_he_iftype_cap(sband6,
2038 ieee80211_vif_type_p2p(&sdata->vif));
2041 enum nl80211_iftype iftype =
2042 ieee80211_vif_type_p2p(&sdata->vif);
2043 __le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
2045 pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
2050 * If adding more here, adjust code in main.c
2051 * that calculates local->scan_ies_len.
2054 return pos - buffer;
2056 WARN_ONCE(1, "not enough space for preq IEs\n");
2058 return pos - buffer;
2061 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
2063 struct ieee80211_scan_ies *ie_desc,
2064 const u8 *ie, size_t ie_len,
2065 u8 bands_used, u32 *rate_masks,
2066 struct cfg80211_chan_def *chandef,
2069 size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
2072 memset(ie_desc, 0, sizeof(*ie_desc));
2074 for (i = 0; i < NUM_NL80211_BANDS; i++) {
2075 if (bands_used & BIT(i)) {
2076 pos += ieee80211_build_preq_ies_band(sdata,
2084 ie_desc->ies[i] = buffer + old_pos;
2085 ie_desc->len[i] = pos - old_pos;
2090 /* add any remaining custom IEs */
2092 if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
2093 "not enough space for preq custom IEs\n"))
2095 memcpy(buffer + pos, ie + custom_ie_offset,
2096 ie_len - custom_ie_offset);
2097 ie_desc->common_ies = buffer + pos;
2098 ie_desc->common_ie_len = ie_len - custom_ie_offset;
2099 pos += ie_len - custom_ie_offset;
2105 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
2106 const u8 *src, const u8 *dst,
2108 struct ieee80211_channel *chan,
2109 const u8 *ssid, size_t ssid_len,
2110 const u8 *ie, size_t ie_len,
2113 struct ieee80211_local *local = sdata->local;
2114 struct cfg80211_chan_def chandef;
2115 struct sk_buff *skb;
2116 struct ieee80211_mgmt *mgmt;
2118 u32 rate_masks[NUM_NL80211_BANDS] = {};
2119 struct ieee80211_scan_ies dummy_ie_desc;
2122 * Do not send DS Channel parameter for directed probe requests
2123 * in order to maximize the chance that we get a response. Some
2124 * badly-behaved APs don't respond when this parameter is included.
2126 chandef.width = sdata->vif.bss_conf.chandef.width;
2127 if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2128 chandef.chan = NULL;
2130 chandef.chan = chan;
2132 skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
2133 local->scan_ies_len + ie_len);
2137 rate_masks[chan->band] = ratemask;
2138 ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
2139 skb_tailroom(skb), &dummy_ie_desc,
2140 ie, ie_len, BIT(chan->band),
2141 rate_masks, &chandef, flags);
2142 skb_put(skb, ies_len);
2145 mgmt = (struct ieee80211_mgmt *) skb->data;
2146 memcpy(mgmt->da, dst, ETH_ALEN);
2147 memcpy(mgmt->bssid, dst, ETH_ALEN);
2150 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2155 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
2156 struct ieee802_11_elems *elems,
2157 enum nl80211_band band, u32 *basic_rates)
2159 struct ieee80211_supported_band *sband;
2161 u32 supp_rates, rate_flags;
2164 sband = sdata->local->hw.wiphy->bands[band];
2165 if (WARN_ON(!sband))
2168 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2169 shift = ieee80211_vif_get_shift(&sdata->vif);
2171 num_rates = sband->n_bitrates;
2173 for (i = 0; i < elems->supp_rates_len +
2174 elems->ext_supp_rates_len; i++) {
2178 if (i < elems->supp_rates_len)
2179 rate = elems->supp_rates[i];
2180 else if (elems->ext_supp_rates)
2181 rate = elems->ext_supp_rates
2182 [i - elems->supp_rates_len];
2183 own_rate = 5 * (rate & 0x7f);
2184 is_basic = !!(rate & 0x80);
2186 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2189 for (j = 0; j < num_rates; j++) {
2191 if ((rate_flags & sband->bitrates[j].flags)
2195 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
2198 if (brate == own_rate) {
2199 supp_rates |= BIT(j);
2200 if (basic_rates && is_basic)
2201 *basic_rates |= BIT(j);
2208 void ieee80211_stop_device(struct ieee80211_local *local)
2210 ieee80211_led_radio(local, false);
2211 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
2213 cancel_work_sync(&local->reconfig_filter);
2215 flush_workqueue(local->workqueue);
2219 static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
2222 /* It's possible that we don't handle the scan completion in
2223 * time during suspend, so if it's still marked as completed
2224 * here, queue the work and flush it to clean things up.
2225 * Instead of calling the worker function directly here, we
2226 * really queue it to avoid potential races with other flows
2227 * scheduling the same work.
2229 if (test_bit(SCAN_COMPLETED, &local->scanning)) {
2230 /* If coming from reconfiguration failure, abort the scan so
2231 * we don't attempt to continue a partial HW scan - which is
2232 * possible otherwise if (e.g.) the 2.4 GHz portion was the
2233 * completed scan, and a 5 GHz portion is still pending.
2236 set_bit(SCAN_ABORTED, &local->scanning);
2237 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);
2238 wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work);
2242 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2244 struct ieee80211_sub_if_data *sdata;
2245 struct ieee80211_chanctx *ctx;
2248 * We get here if during resume the device can't be restarted properly.
2249 * We might also get here if this happens during HW reset, which is a
2250 * slightly different situation and we need to drop all connections in
2253 * Ask cfg80211 to turn off all interfaces, this will result in more
2254 * warnings but at least we'll then get into a clean stopped state.
2257 local->resuming = false;
2258 local->suspended = false;
2259 local->in_reconfig = false;
2261 ieee80211_flush_completed_scan(local, true);
2263 /* scheduled scan clearly can't be running any more, but tell
2264 * cfg80211 and clear local state
2266 ieee80211_sched_scan_end(local);
2268 list_for_each_entry(sdata, &local->interfaces, list)
2269 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2271 /* Mark channel contexts as not being in the driver any more to avoid
2272 * removing them from the driver during the shutdown process...
2274 mutex_lock(&local->chanctx_mtx);
2275 list_for_each_entry(ctx, &local->chanctx_list, list)
2276 ctx->driver_present = false;
2277 mutex_unlock(&local->chanctx_mtx);
2280 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2281 struct ieee80211_sub_if_data *sdata,
2282 struct ieee80211_link_data *link)
2284 struct ieee80211_chanctx_conf *conf;
2285 struct ieee80211_chanctx *ctx;
2287 if (!local->use_chanctx)
2290 mutex_lock(&local->chanctx_mtx);
2291 conf = rcu_dereference_protected(link->conf->chanctx_conf,
2292 lockdep_is_held(&local->chanctx_mtx));
2294 ctx = container_of(conf, struct ieee80211_chanctx, conf);
2295 drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
2297 mutex_unlock(&local->chanctx_mtx);
2300 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
2302 struct ieee80211_local *local = sdata->local;
2303 struct sta_info *sta;
2306 mutex_lock(&local->sta_mtx);
2307 list_for_each_entry(sta, &local->sta_list, list) {
2308 enum ieee80211_sta_state state;
2310 if (!sta->uploaded || sta->sdata != sdata)
2313 for (state = IEEE80211_STA_NOTEXIST;
2314 state < sta->sta_state; state++)
2315 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2318 mutex_unlock(&local->sta_mtx);
2321 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2323 struct cfg80211_nan_func *func, **funcs;
2326 res = drv_start_nan(sdata->local, sdata,
2327 &sdata->u.nan.conf);
2331 funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
2337 /* Add all the functions:
2338 * This is a little bit ugly. We need to call a potentially sleeping
2339 * callback for each NAN function, so we can't hold the spinlock.
2341 spin_lock_bh(&sdata->u.nan.func_lock);
2343 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2346 spin_unlock_bh(&sdata->u.nan.func_lock);
2348 for (i = 0; funcs[i]; i++) {
2349 res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2351 ieee80211_nan_func_terminated(&sdata->vif,
2352 funcs[i]->instance_id,
2353 NL80211_NAN_FUNC_TERM_REASON_ERROR,
2362 int ieee80211_reconfig(struct ieee80211_local *local)
2364 struct ieee80211_hw *hw = &local->hw;
2365 struct ieee80211_sub_if_data *sdata;
2366 struct ieee80211_chanctx *ctx;
2367 struct sta_info *sta;
2369 bool reconfig_due_to_wowlan = false;
2370 struct ieee80211_sub_if_data *sched_scan_sdata;
2371 struct cfg80211_sched_scan_request *sched_scan_req;
2372 bool sched_scan_stopped = false;
2373 bool suspended = local->suspended;
2374 bool in_reconfig = false;
2376 /* nothing to do if HW shouldn't run */
2377 if (!local->open_count)
2382 local->resuming = true;
2384 if (local->wowlan) {
2386 * In the wowlan case, both mac80211 and the device
2387 * are functional when the resume op is called, so
2388 * clear local->suspended so the device could operate
2389 * normally (e.g. pass rx frames).
2391 local->suspended = false;
2392 res = drv_resume(local);
2393 local->wowlan = false;
2395 local->resuming = false;
2402 * res is 1, which means the driver requested
2403 * to go through a regular reset on wakeup.
2404 * restore local->suspended in this case.
2406 reconfig_due_to_wowlan = true;
2407 local->suspended = true;
2412 * In case of hw_restart during suspend (without wowlan),
2413 * cancel restart work, as we are reconfiguring the device
2415 * Note that restart_work is scheduled on a frozen workqueue,
2416 * so we can't deadlock in this case.
2418 if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
2419 cancel_work_sync(&local->restart_work);
2421 local->started = false;
2424 * Upon resume hardware can sometimes be goofy due to
2425 * various platform / driver / bus issues, so restarting
2426 * the device may at times not work immediately. Propagate
2429 res = drv_start(local);
2432 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2434 WARN(1, "Hardware became unavailable during restart.\n");
2435 ieee80211_handle_reconfig_failure(local);
2439 /* setup fragmentation threshold */
2440 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
2442 /* setup RTS threshold */
2443 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
2445 /* reset coverage class */
2446 drv_set_coverage_class(local, hw->wiphy->coverage_class);
2448 ieee80211_led_radio(local, true);
2449 ieee80211_mod_tpt_led_trig(local,
2450 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2452 /* add interfaces */
2453 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
2455 /* in HW restart it exists already */
2456 WARN_ON(local->resuming);
2457 res = drv_add_interface(local, sdata);
2459 RCU_INIT_POINTER(local->monitor_sdata, NULL);
2465 list_for_each_entry(sdata, &local->interfaces, list) {
2466 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2467 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2468 ieee80211_sdata_running(sdata)) {
2469 res = drv_add_interface(local, sdata);
2475 /* If adding any of the interfaces failed above, roll back and
2479 list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2481 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2482 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2483 ieee80211_sdata_running(sdata))
2484 drv_remove_interface(local, sdata);
2485 ieee80211_handle_reconfig_failure(local);
2489 /* add channel contexts */
2490 if (local->use_chanctx) {
2491 mutex_lock(&local->chanctx_mtx);
2492 list_for_each_entry(ctx, &local->chanctx_list, list)
2493 if (ctx->replace_state !=
2494 IEEE80211_CHANCTX_REPLACES_OTHER)
2495 WARN_ON(drv_add_chanctx(local, ctx));
2496 mutex_unlock(&local->chanctx_mtx);
2498 sdata = wiphy_dereference(local->hw.wiphy,
2499 local->monitor_sdata);
2500 if (sdata && ieee80211_sdata_running(sdata))
2501 ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
2504 /* reconfigure hardware */
2505 ieee80211_hw_config(local, ~0);
2507 ieee80211_configure_filter(local);
2509 /* Finally also reconfigure all the BSS information */
2510 list_for_each_entry(sdata, &local->interfaces, list) {
2511 unsigned int link_id;
2514 if (!ieee80211_sdata_running(sdata))
2519 link_id < ARRAY_SIZE(sdata->vif.link_conf);
2521 struct ieee80211_link_data *link;
2523 link = sdata_dereference(sdata->link[link_id], sdata);
2525 ieee80211_assign_chanctx(local, sdata, link);
2528 switch (sdata->vif.type) {
2529 case NL80211_IFTYPE_AP_VLAN:
2530 case NL80211_IFTYPE_MONITOR:
2532 case NL80211_IFTYPE_ADHOC:
2533 if (sdata->vif.cfg.ibss_joined)
2534 WARN_ON(drv_join_ibss(local, sdata));
2537 ieee80211_reconfig_stations(sdata);
2539 case NL80211_IFTYPE_AP: /* AP stations are handled later */
2540 for (i = 0; i < IEEE80211_NUM_ACS; i++)
2541 drv_conf_tx(local, &sdata->deflink, i,
2542 &sdata->deflink.tx_conf[i]);
2545 sdata_unlock(sdata);
2547 /* common change flags for all interface types */
2548 changed = BSS_CHANGED_ERP_CTS_PROT |
2549 BSS_CHANGED_ERP_PREAMBLE |
2550 BSS_CHANGED_ERP_SLOT |
2552 BSS_CHANGED_BASIC_RATES |
2553 BSS_CHANGED_BEACON_INT |
2558 BSS_CHANGED_TXPOWER |
2559 BSS_CHANGED_MCAST_RATE;
2561 if (sdata->vif.bss_conf.mu_mimo_owner)
2562 changed |= BSS_CHANGED_MU_GROUPS;
2564 switch (sdata->vif.type) {
2565 case NL80211_IFTYPE_STATION:
2566 changed |= BSS_CHANGED_ASSOC |
2567 BSS_CHANGED_ARP_FILTER |
2570 /* Re-send beacon info report to the driver */
2571 if (sdata->deflink.u.mgd.have_beacon)
2572 changed |= BSS_CHANGED_BEACON_INFO;
2574 if (sdata->vif.bss_conf.max_idle_period ||
2575 sdata->vif.bss_conf.protected_keep_alive)
2576 changed |= BSS_CHANGED_KEEP_ALIVE;
2579 ieee80211_bss_info_change_notify(sdata, changed);
2580 sdata_unlock(sdata);
2582 case NL80211_IFTYPE_OCB:
2583 changed |= BSS_CHANGED_OCB;
2584 ieee80211_bss_info_change_notify(sdata, changed);
2586 case NL80211_IFTYPE_ADHOC:
2587 changed |= BSS_CHANGED_IBSS;
2589 case NL80211_IFTYPE_AP:
2590 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2592 if (sdata->vif.bss_conf.ftm_responder == 1 &&
2593 wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2594 NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2595 changed |= BSS_CHANGED_FTM_RESPONDER;
2597 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2598 changed |= BSS_CHANGED_AP_PROBE_RESP;
2600 if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2601 drv_start_ap(local, sdata,
2602 sdata->deflink.conf);
2605 case NL80211_IFTYPE_MESH_POINT:
2606 if (sdata->vif.bss_conf.enable_beacon) {
2607 changed |= BSS_CHANGED_BEACON |
2608 BSS_CHANGED_BEACON_ENABLED;
2609 ieee80211_bss_info_change_notify(sdata, changed);
2612 case NL80211_IFTYPE_NAN:
2613 res = ieee80211_reconfig_nan(sdata);
2615 ieee80211_handle_reconfig_failure(local);
2619 case NL80211_IFTYPE_AP_VLAN:
2620 case NL80211_IFTYPE_MONITOR:
2621 case NL80211_IFTYPE_P2P_DEVICE:
2624 case NL80211_IFTYPE_UNSPECIFIED:
2625 case NUM_NL80211_IFTYPES:
2626 case NL80211_IFTYPE_P2P_CLIENT:
2627 case NL80211_IFTYPE_P2P_GO:
2628 case NL80211_IFTYPE_WDS:
2634 ieee80211_recalc_ps(local);
2637 * The sta might be in psm against the ap (e.g. because
2638 * this was the state before a hw restart), so we
2639 * explicitly send a null packet in order to make sure
2640 * it'll sync against the ap (and get out of psm).
2642 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2643 list_for_each_entry(sdata, &local->interfaces, list) {
2644 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2646 if (!sdata->u.mgd.associated)
2649 ieee80211_send_nullfunc(local, sdata, false);
2653 /* APs are now beaconing, add back stations */
2654 list_for_each_entry(sdata, &local->interfaces, list) {
2655 if (!ieee80211_sdata_running(sdata))
2659 switch (sdata->vif.type) {
2660 case NL80211_IFTYPE_AP_VLAN:
2661 case NL80211_IFTYPE_AP:
2662 ieee80211_reconfig_stations(sdata);
2667 sdata_unlock(sdata);
2671 list_for_each_entry(sdata, &local->interfaces, list)
2672 ieee80211_reenable_keys(sdata);
2674 /* Reconfigure sched scan if it was interrupted by FW restart */
2675 mutex_lock(&local->mtx);
2676 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2677 lockdep_is_held(&local->mtx));
2678 sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2679 lockdep_is_held(&local->mtx));
2680 if (sched_scan_sdata && sched_scan_req)
2682 * Sched scan stopped, but we don't want to report it. Instead,
2683 * we're trying to reschedule. However, if more than one scan
2684 * plan was set, we cannot reschedule since we don't know which
2685 * scan plan was currently running (and some scan plans may have
2686 * already finished).
2688 if (sched_scan_req->n_scan_plans > 1 ||
2689 __ieee80211_request_sched_scan_start(sched_scan_sdata,
2691 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2692 RCU_INIT_POINTER(local->sched_scan_req, NULL);
2693 sched_scan_stopped = true;
2695 mutex_unlock(&local->mtx);
2697 if (sched_scan_stopped)
2698 cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2702 if (local->monitors == local->open_count && local->monitors > 0)
2703 ieee80211_add_virtual_monitor(local);
2706 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2707 * sessions can be established after a resume.
2709 * Also tear down aggregation sessions since reconfiguring
2710 * them in a hardware restart scenario is not easily done
2711 * right now, and the hardware will have lost information
2712 * about the sessions, but we and the AP still think they
2713 * are active. This is really a workaround though.
2715 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2716 mutex_lock(&local->sta_mtx);
2718 list_for_each_entry(sta, &local->sta_list, list) {
2719 if (!local->resuming)
2720 ieee80211_sta_tear_down_BA_sessions(
2721 sta, AGG_STOP_LOCAL_REQUEST);
2722 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2725 mutex_unlock(&local->sta_mtx);
2729 * If this is for hw restart things are still running.
2730 * We may want to change that later, however.
2732 if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2733 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2735 if (local->in_reconfig) {
2736 in_reconfig = local->in_reconfig;
2737 local->in_reconfig = false;
2740 /* Restart deferred ROCs */
2741 mutex_lock(&local->mtx);
2742 ieee80211_start_next_roc(local);
2743 mutex_unlock(&local->mtx);
2745 /* Requeue all works */
2746 list_for_each_entry(sdata, &local->interfaces, list)
2747 ieee80211_queue_work(&local->hw, &sdata->work);
2750 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2751 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2755 list_for_each_entry(sdata, &local->interfaces, list) {
2756 if (!ieee80211_sdata_running(sdata))
2758 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2759 ieee80211_sta_restart(sdata);
2767 /* first set suspended false, then resuming */
2768 local->suspended = false;
2770 local->resuming = false;
2772 ieee80211_flush_completed_scan(local, false);
2774 if (local->open_count && !reconfig_due_to_wowlan)
2775 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2777 list_for_each_entry(sdata, &local->interfaces, list) {
2778 if (!ieee80211_sdata_running(sdata))
2780 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2781 ieee80211_sta_restart(sdata);
2784 mod_timer(&local->sta_cleanup, jiffies + 1);
2792 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2794 struct ieee80211_sub_if_data *sdata;
2795 struct ieee80211_local *local;
2796 struct ieee80211_key *key;
2801 sdata = vif_to_sdata(vif);
2802 local = sdata->local;
2804 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2808 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2809 !local->in_reconfig))
2812 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2815 sdata->flags |= flag;
2817 mutex_lock(&local->key_mtx);
2818 list_for_each_entry(key, &sdata->key_list, list)
2819 key->flags |= KEY_FLAG_TAINTED;
2820 mutex_unlock(&local->key_mtx);
2823 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
2825 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
2827 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
2829 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2831 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
2833 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2835 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
2836 struct ieee80211_link_data *link)
2838 struct ieee80211_local *local = sdata->local;
2839 struct ieee80211_chanctx_conf *chanctx_conf;
2840 struct ieee80211_chanctx *chanctx;
2842 mutex_lock(&local->chanctx_mtx);
2844 chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
2845 lockdep_is_held(&local->chanctx_mtx));
2848 * This function can be called from a work, thus it may be possible
2849 * that the chanctx_conf is removed (due to a disconnection, for
2851 * So nothing should be done in such case.
2856 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2857 ieee80211_recalc_smps_chanctx(local, chanctx);
2859 mutex_unlock(&local->chanctx_mtx);
2862 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
2865 struct ieee80211_local *local = sdata->local;
2866 struct ieee80211_chanctx_conf *chanctx_conf;
2867 struct ieee80211_chanctx *chanctx;
2870 mutex_lock(&local->chanctx_mtx);
2872 for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
2873 struct ieee80211_bss_conf *bss_conf;
2875 if (link_id >= 0 && link_id != i)
2879 bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
2885 chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
2886 lockdep_is_held(&local->chanctx_mtx));
2888 * Since we hold the chanctx_mtx (checked above)
2889 * we can take the chanctx_conf pointer out of the
2890 * RCU critical section, it cannot go away without
2891 * the mutex. Just the way we reached it could - in
2892 * theory - go away, but we don't really care and
2893 * it really shouldn't happen anyway.
2900 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
2902 ieee80211_recalc_chanctx_min_def(local, chanctx, NULL);
2905 mutex_unlock(&local->chanctx_mtx);
2908 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
2910 size_t pos = offset;
2912 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
2913 pos += 2 + ies[pos + 1];
2918 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2923 *pos++ = WLAN_EID_HT_CAPABILITY;
2924 *pos++ = sizeof(struct ieee80211_ht_cap);
2925 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
2927 /* capability flags */
2928 tmp = cpu_to_le16(cap);
2929 memcpy(pos, &tmp, sizeof(u16));
2932 /* AMPDU parameters */
2933 *pos++ = ht_cap->ampdu_factor |
2934 (ht_cap->ampdu_density <<
2935 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
2938 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2939 pos += sizeof(ht_cap->mcs);
2941 /* extended capabilities */
2942 pos += sizeof(__le16);
2944 /* BF capabilities */
2945 pos += sizeof(__le32);
2947 /* antenna selection */
2953 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2958 *pos++ = WLAN_EID_VHT_CAPABILITY;
2959 *pos++ = sizeof(struct ieee80211_vht_cap);
2960 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2962 /* capability flags */
2963 tmp = cpu_to_le32(cap);
2964 memcpy(pos, &tmp, sizeof(u32));
2968 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2969 pos += sizeof(vht_cap->vht_mcs);
2974 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
2976 const struct ieee80211_sta_he_cap *he_cap;
2977 struct ieee80211_supported_band *sband;
2980 sband = ieee80211_get_sband(sdata);
2984 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
2988 n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
2990 sizeof(he_cap->he_cap_elem) + n +
2991 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
2992 he_cap->he_cap_elem.phy_cap_info);
2995 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
2996 const struct ieee80211_sta_he_cap *he_cap,
2999 struct ieee80211_he_cap_elem elem;
3004 /* Make sure we have place for the IE */
3006 * TODO: the 1 added is because this temporarily is under the EXTENSION
3007 * IE. Get rid of it when it moves.
3012 /* modify on stack first to calculate 'n' and 'ie_len' correctly */
3013 elem = he_cap->he_cap_elem;
3015 if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
3016 elem.phy_cap_info[0] &=
3017 ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3018 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
3020 if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
3021 elem.phy_cap_info[0] &=
3022 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3024 if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
3025 elem.phy_cap_info[0] &=
3026 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3028 n = ieee80211_he_mcs_nss_size(&elem);
3030 sizeof(he_cap->he_cap_elem) + n +
3031 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3032 he_cap->he_cap_elem.phy_cap_info);
3034 if ((end - pos) < ie_len)
3037 *pos++ = WLAN_EID_EXTENSION;
3038 pos++; /* We'll set the size later below */
3039 *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
3042 memcpy(pos, &elem, sizeof(elem));
3043 pos += sizeof(elem);
3045 memcpy(pos, &he_cap->he_mcs_nss_supp, n);
3048 /* Check if PPE Threshold should be present */
3049 if ((he_cap->he_cap_elem.phy_cap_info[6] &
3050 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
3054 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
3055 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
3057 n = hweight8(he_cap->ppe_thres[0] &
3058 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
3059 n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
3060 IEEE80211_PPE_THRES_NSS_POS));
3063 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
3066 n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
3067 n = DIV_ROUND_UP(n, 8);
3069 /* Copy PPE Thresholds */
3070 memcpy(pos, &he_cap->ppe_thres, n);
3074 orig_pos[1] = (pos - orig_pos) - 2;
3078 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
3079 enum ieee80211_smps_mode smps_mode,
3080 struct sk_buff *skb)
3082 struct ieee80211_supported_band *sband;
3083 const struct ieee80211_sband_iftype_data *iftd;
3084 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3088 if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
3089 BIT(NL80211_BAND_6GHZ),
3090 IEEE80211_CHAN_NO_HE))
3093 sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3095 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
3099 /* Check for device HE 6 GHz capability before adding element */
3100 if (!iftd->he_6ghz_capa.capa)
3103 cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
3104 cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
3106 switch (smps_mode) {
3107 case IEEE80211_SMPS_AUTOMATIC:
3108 case IEEE80211_SMPS_NUM_MODES:
3111 case IEEE80211_SMPS_OFF:
3112 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
3113 IEEE80211_HE_6GHZ_CAP_SM_PS);
3115 case IEEE80211_SMPS_STATIC:
3116 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
3117 IEEE80211_HE_6GHZ_CAP_SM_PS);
3119 case IEEE80211_SMPS_DYNAMIC:
3120 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
3121 IEEE80211_HE_6GHZ_CAP_SM_PS);
3125 pos = skb_put(skb, 2 + 1 + sizeof(cap));
3126 ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
3127 pos + 2 + 1 + sizeof(cap));
3130 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3131 const struct cfg80211_chan_def *chandef,
3132 u16 prot_mode, bool rifs_mode)
3134 struct ieee80211_ht_operation *ht_oper;
3135 /* Build HT Information */
3136 *pos++ = WLAN_EID_HT_OPERATION;
3137 *pos++ = sizeof(struct ieee80211_ht_operation);
3138 ht_oper = (struct ieee80211_ht_operation *)pos;
3139 ht_oper->primary_chan = ieee80211_frequency_to_channel(
3140 chandef->chan->center_freq);
3141 switch (chandef->width) {
3142 case NL80211_CHAN_WIDTH_160:
3143 case NL80211_CHAN_WIDTH_80P80:
3144 case NL80211_CHAN_WIDTH_80:
3145 case NL80211_CHAN_WIDTH_40:
3146 if (chandef->center_freq1 > chandef->chan->center_freq)
3147 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3149 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3151 case NL80211_CHAN_WIDTH_320:
3152 /* HT information element should not be included on 6GHz */
3156 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
3159 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
3160 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
3161 chandef->width != NL80211_CHAN_WIDTH_20)
3162 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
3165 ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
3167 ht_oper->operation_mode = cpu_to_le16(prot_mode);
3168 ht_oper->stbc_param = 0x0000;
3170 /* It seems that Basic MCS set and Supported MCS set
3171 are identical for the first 10 bytes */
3172 memset(&ht_oper->basic_set, 0, 16);
3173 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
3175 return pos + sizeof(struct ieee80211_ht_operation);
3178 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
3179 const struct cfg80211_chan_def *chandef)
3181 *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
3182 *pos++ = 3; /* IE length */
3183 /* New channel width */
3184 switch (chandef->width) {
3185 case NL80211_CHAN_WIDTH_80:
3186 *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
3188 case NL80211_CHAN_WIDTH_160:
3189 *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
3191 case NL80211_CHAN_WIDTH_80P80:
3192 *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
3194 case NL80211_CHAN_WIDTH_320:
3195 /* The behavior is not defined for 320 MHz channels */
3199 *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
3202 /* new center frequency segment 0 */
3203 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
3204 /* new center frequency segment 1 */
3205 if (chandef->center_freq2)
3206 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
3211 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3212 const struct cfg80211_chan_def *chandef)
3214 struct ieee80211_vht_operation *vht_oper;
3216 *pos++ = WLAN_EID_VHT_OPERATION;
3217 *pos++ = sizeof(struct ieee80211_vht_operation);
3218 vht_oper = (struct ieee80211_vht_operation *)pos;
3219 vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
3220 chandef->center_freq1);
3221 if (chandef->center_freq2)
3222 vht_oper->center_freq_seg1_idx =
3223 ieee80211_frequency_to_channel(chandef->center_freq2);
3225 vht_oper->center_freq_seg1_idx = 0x00;
3227 switch (chandef->width) {
3228 case NL80211_CHAN_WIDTH_160:
3230 * Convert 160 MHz channel width to new style as interop
3233 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3234 vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
3235 if (chandef->chan->center_freq < chandef->center_freq1)
3236 vht_oper->center_freq_seg0_idx -= 8;
3238 vht_oper->center_freq_seg0_idx += 8;
3240 case NL80211_CHAN_WIDTH_80P80:
3242 * Convert 80+80 MHz channel width to new style as interop
3245 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3247 case NL80211_CHAN_WIDTH_80:
3248 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3250 case NL80211_CHAN_WIDTH_320:
3251 /* VHT information element should not be included on 6GHz */
3255 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
3259 /* don't require special VHT peer rates */
3260 vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
3262 return pos + sizeof(struct ieee80211_vht_operation);
3265 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
3267 struct ieee80211_he_operation *he_oper;
3268 struct ieee80211_he_6ghz_oper *he_6ghz_op;
3270 u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
3272 if (chandef->chan->band == NL80211_BAND_6GHZ)
3273 ie_len += sizeof(struct ieee80211_he_6ghz_oper);
3275 *pos++ = WLAN_EID_EXTENSION;
3277 *pos++ = WLAN_EID_EXT_HE_OPERATION;
3280 he_oper_params |= u32_encode_bits(1023, /* disabled */
3281 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3282 he_oper_params |= u32_encode_bits(1,
3283 IEEE80211_HE_OPERATION_ER_SU_DISABLE);
3284 he_oper_params |= u32_encode_bits(1,
3285 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3286 if (chandef->chan->band == NL80211_BAND_6GHZ)
3287 he_oper_params |= u32_encode_bits(1,
3288 IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
3290 he_oper = (struct ieee80211_he_operation *)pos;
3291 he_oper->he_oper_params = cpu_to_le32(he_oper_params);
3293 /* don't require special HE peer rates */
3294 he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
3295 pos += sizeof(struct ieee80211_he_operation);
3297 if (chandef->chan->band != NL80211_BAND_6GHZ)
3300 /* TODO add VHT operational */
3301 he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
3302 he_6ghz_op->minrate = 6; /* 6 Mbps */
3303 he_6ghz_op->primary =
3304 ieee80211_frequency_to_channel(chandef->chan->center_freq);
3306 ieee80211_frequency_to_channel(chandef->center_freq1);
3307 if (chandef->center_freq2)
3309 ieee80211_frequency_to_channel(chandef->center_freq2);
3311 he_6ghz_op->ccfs1 = 0;
3313 switch (chandef->width) {
3314 case NL80211_CHAN_WIDTH_320:
3316 * TODO: mesh operation is not defined over 6GHz 320 MHz
3321 case NL80211_CHAN_WIDTH_160:
3322 /* Convert 160 MHz channel width to new style as interop
3325 he_6ghz_op->control =
3326 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3327 he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
3328 if (chandef->chan->center_freq < chandef->center_freq1)
3329 he_6ghz_op->ccfs0 -= 8;
3331 he_6ghz_op->ccfs0 += 8;
3333 case NL80211_CHAN_WIDTH_80P80:
3334 he_6ghz_op->control =
3335 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3337 case NL80211_CHAN_WIDTH_80:
3338 he_6ghz_op->control =
3339 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
3341 case NL80211_CHAN_WIDTH_40:
3342 he_6ghz_op->control =
3343 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3346 he_6ghz_op->control =
3347 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3351 pos += sizeof(struct ieee80211_he_6ghz_oper);
3357 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3358 struct cfg80211_chan_def *chandef)
3360 enum nl80211_channel_type channel_type;
3365 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3366 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3367 channel_type = NL80211_CHAN_HT20;
3369 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3370 channel_type = NL80211_CHAN_HT40PLUS;
3372 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3373 channel_type = NL80211_CHAN_HT40MINUS;
3379 cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3383 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3384 const struct ieee80211_vht_operation *oper,
3385 const struct ieee80211_ht_operation *htop,
3386 struct cfg80211_chan_def *chandef)
3388 struct cfg80211_chan_def new = *chandef;
3390 int ccfs0, ccfs1, ccfs2;
3393 bool support_80_80 = false;
3394 bool support_160 = false;
3395 u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3396 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3397 u8 supp_chwidth = u32_get_bits(vht_cap_info,
3398 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3403 vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3404 support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3405 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3406 support_80_80 = ((vht_cap &
3407 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3408 (vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3409 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3410 ((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3411 IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3412 ccfs0 = oper->center_freq_seg0_idx;
3413 ccfs1 = oper->center_freq_seg1_idx;
3414 ccfs2 = (le16_to_cpu(htop->operation_mode) &
3415 IEEE80211_HT_OP_MODE_CCFS2_MASK)
3416 >> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3420 /* if not supported, parse as though we didn't understand it */
3421 if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3422 ext_nss_bw_supp = 0;
3425 * Cf. IEEE 802.11 Table 9-250
3427 * We really just consider that because it's inefficient to connect
3428 * at a higher bandwidth than we'll actually be able to use.
3430 switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3434 support_160 = false;
3435 support_80_80 = false;
3438 support_80_80 = false;
3461 cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3462 cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3464 switch (oper->chan_width) {
3465 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3466 /* just use HT information directly */
3468 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3469 new.width = NL80211_CHAN_WIDTH_80;
3470 new.center_freq1 = cf0;
3471 /* If needed, adjust based on the newer interop workaround. */
3475 diff = abs(ccf1 - ccf0);
3476 if ((diff == 8) && support_160) {
3477 new.width = NL80211_CHAN_WIDTH_160;
3478 new.center_freq1 = cf1;
3479 } else if ((diff > 8) && support_80_80) {
3480 new.width = NL80211_CHAN_WIDTH_80P80;
3481 new.center_freq2 = cf1;
3485 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3486 /* deprecated encoding */
3487 new.width = NL80211_CHAN_WIDTH_160;
3488 new.center_freq1 = cf0;
3490 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3491 /* deprecated encoding */
3492 new.width = NL80211_CHAN_WIDTH_80P80;
3493 new.center_freq1 = cf0;
3494 new.center_freq2 = cf1;
3500 if (!cfg80211_chandef_valid(&new))
3507 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
3508 bool support_160, bool support_320,
3509 struct cfg80211_chan_def *chandef)
3511 struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
3513 chandef->center_freq1 =
3514 ieee80211_channel_to_frequency(info->ccfs0,
3515 chandef->chan->band);
3517 switch (u8_get_bits(info->control,
3518 IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3519 case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3520 chandef->width = NL80211_CHAN_WIDTH_20;
3522 case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3523 chandef->width = NL80211_CHAN_WIDTH_40;
3525 case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3526 chandef->width = NL80211_CHAN_WIDTH_80;
3528 case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3530 chandef->width = NL80211_CHAN_WIDTH_160;
3531 chandef->center_freq1 =
3532 ieee80211_channel_to_frequency(info->ccfs1,
3533 chandef->chan->band);
3535 chandef->width = NL80211_CHAN_WIDTH_80;
3538 case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3540 chandef->width = NL80211_CHAN_WIDTH_320;
3541 chandef->center_freq1 =
3542 ieee80211_channel_to_frequency(info->ccfs1,
3543 chandef->chan->band);
3544 } else if (support_160) {
3545 chandef->width = NL80211_CHAN_WIDTH_160;
3547 chandef->width = NL80211_CHAN_WIDTH_80;
3549 if (chandef->center_freq1 > chandef->chan->center_freq)
3550 chandef->center_freq1 -= 40;
3552 chandef->center_freq1 += 40;
3558 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
3559 const struct ieee80211_he_operation *he_oper,
3560 const struct ieee80211_eht_operation *eht_oper,
3561 struct cfg80211_chan_def *chandef)
3563 struct ieee80211_local *local = sdata->local;
3564 struct ieee80211_supported_band *sband;
3565 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3566 const struct ieee80211_sta_he_cap *he_cap;
3567 const struct ieee80211_sta_eht_cap *eht_cap;
3568 struct cfg80211_chan_def he_chandef = *chandef;
3569 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3570 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3571 bool support_80_80, support_160, support_320;
3572 u8 he_phy_cap, eht_phy_cap;
3575 if (chandef->chan->band != NL80211_BAND_6GHZ)
3578 sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3580 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3582 sdata_info(sdata, "Missing iftype sband data/HE cap");
3586 he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
3589 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3592 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3596 "HE is not advertised on (on %d MHz), expect issues\n",
3597 chandef->chan->center_freq);
3601 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
3605 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3607 if (!he_6ghz_oper) {
3609 "HE 6GHz operation missing (on %d MHz), expect issues\n",
3610 chandef->chan->center_freq);
3615 * The EHT operation IE does not contain the primary channel so the
3616 * primary channel frequency should be taken from the 6 GHz operation
3619 freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3621 he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
3623 switch (u8_get_bits(he_6ghz_oper->control,
3624 IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
3625 case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
3626 bss_conf->power_type = IEEE80211_REG_LPI_AP;
3628 case IEEE80211_6GHZ_CTRL_REG_SP_AP:
3629 bss_conf->power_type = IEEE80211_REG_SP_AP;
3632 bss_conf->power_type = IEEE80211_REG_UNSET_AP;
3637 !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3638 switch (u8_get_bits(he_6ghz_oper->control,
3639 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3640 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3641 he_chandef.width = NL80211_CHAN_WIDTH_20;
3643 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3644 he_chandef.width = NL80211_CHAN_WIDTH_40;
3646 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3647 he_chandef.width = NL80211_CHAN_WIDTH_80;
3649 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3650 he_chandef.width = NL80211_CHAN_WIDTH_80;
3651 if (!he_6ghz_oper->ccfs1)
3653 if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
3655 he_chandef.width = NL80211_CHAN_WIDTH_160;
3658 he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3663 if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3664 he_chandef.center_freq1 =
3665 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3668 he_chandef.center_freq1 =
3669 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3671 if (support_80_80 || support_160)
3672 he_chandef.center_freq2 =
3673 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3677 eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
3679 eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
3681 ieee80211_chandef_eht_oper(eht_oper, support_160,
3682 support_320, &he_chandef);
3685 if (!cfg80211_chandef_valid(&he_chandef)) {
3687 "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
3688 he_chandef.chan ? he_chandef.chan->center_freq : 0,
3690 he_chandef.center_freq1,
3691 he_chandef.center_freq2);
3695 *chandef = he_chandef;
3700 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
3701 struct cfg80211_chan_def *chandef)
3708 switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3709 case IEEE80211_S1G_CHANWIDTH_1MHZ:
3710 chandef->width = NL80211_CHAN_WIDTH_1;
3712 case IEEE80211_S1G_CHANWIDTH_2MHZ:
3713 chandef->width = NL80211_CHAN_WIDTH_2;
3715 case IEEE80211_S1G_CHANWIDTH_4MHZ:
3716 chandef->width = NL80211_CHAN_WIDTH_4;
3718 case IEEE80211_S1G_CHANWIDTH_8MHZ:
3719 chandef->width = NL80211_CHAN_WIDTH_8;
3721 case IEEE80211_S1G_CHANWIDTH_16MHZ:
3722 chandef->width = NL80211_CHAN_WIDTH_16;
3728 oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
3729 NL80211_BAND_S1GHZ);
3730 chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
3731 chandef->freq1_offset = oper_freq % 1000;
3736 int ieee80211_parse_bitrates(enum nl80211_chan_width width,
3737 const struct ieee80211_supported_band *sband,
3738 const u8 *srates, int srates_len, u32 *rates)
3740 u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
3741 int shift = ieee80211_chanwidth_get_shift(width);
3742 struct ieee80211_rate *br;
3743 int brate, rate, i, j, count = 0;
3747 for (i = 0; i < srates_len; i++) {
3748 rate = srates[i] & 0x7f;
3750 for (j = 0; j < sband->n_bitrates; j++) {
3751 br = &sband->bitrates[j];
3752 if ((rate_flags & br->flags) != rate_flags)
3755 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3756 if (brate == rate) {
3766 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
3767 struct sk_buff *skb, bool need_basic,
3768 enum nl80211_band band)
3770 struct ieee80211_local *local = sdata->local;
3771 struct ieee80211_supported_band *sband;
3774 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3777 shift = ieee80211_vif_get_shift(&sdata->vif);
3778 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3779 sband = local->hw.wiphy->bands[band];
3781 for (i = 0; i < sband->n_bitrates; i++) {
3782 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3789 if (skb_tailroom(skb) < rates + 2)
3792 pos = skb_put(skb, rates + 2);
3793 *pos++ = WLAN_EID_SUPP_RATES;
3795 for (i = 0; i < rates; i++) {
3797 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3800 if (need_basic && basic_rates & BIT(i))
3802 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
3804 *pos++ = basic | (u8) rate;
3810 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
3811 struct sk_buff *skb, bool need_basic,
3812 enum nl80211_band band)
3814 struct ieee80211_local *local = sdata->local;
3815 struct ieee80211_supported_band *sband;
3817 u8 i, exrates, *pos;
3818 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3821 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3822 shift = ieee80211_vif_get_shift(&sdata->vif);
3824 sband = local->hw.wiphy->bands[band];
3826 for (i = 0; i < sband->n_bitrates; i++) {
3827 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3837 if (skb_tailroom(skb) < exrates + 2)
3841 pos = skb_put(skb, exrates + 2);
3842 *pos++ = WLAN_EID_EXT_SUPP_RATES;
3844 for (i = 8; i < sband->n_bitrates; i++) {
3846 if ((rate_flags & sband->bitrates[i].flags)
3849 if (need_basic && basic_rates & BIT(i))
3851 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
3853 *pos++ = basic | (u8) rate;
3859 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
3861 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3863 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
3866 return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
3868 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
3870 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
3875 /* TODO: consider rx_highest */
3877 if (mcs->rx_mask[3])
3879 if (mcs->rx_mask[2])
3881 if (mcs->rx_mask[1])
3887 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
3888 * @local: mac80211 hw info struct
3889 * @status: RX status
3890 * @mpdu_len: total MPDU length (including FCS)
3891 * @mpdu_offset: offset into MPDU to calculate timestamp at
3893 * This function calculates the RX timestamp at the given MPDU offset, taking
3894 * into account what the RX timestamp was. An offset of 0 will just normalize
3895 * the timestamp to TSF at beginning of MPDU reception.
3897 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
3898 struct ieee80211_rx_status *status,
3899 unsigned int mpdu_len,
3900 unsigned int mpdu_offset)
3902 u64 ts = status->mactime;
3903 struct rate_info ri;
3907 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
3910 memset(&ri, 0, sizeof(ri));
3914 /* Fill cfg80211 rate info */
3915 switch (status->encoding) {
3917 ri.flags |= RATE_INFO_FLAGS_HE_MCS;
3918 ri.mcs = status->rate_idx;
3919 ri.nss = status->nss;
3920 ri.he_ru_alloc = status->he_ru;
3921 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3922 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3925 * See P802.11ax_D6.0, section 27.3.4 for
3928 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
3934 * For HE MU PPDU, add the HE-SIG-B.
3935 * For HE ER PPDU, add 8us for the HE-SIG-A.
3936 * For HE TB PPDU, add 4us for the HE-STF.
3937 * Add the HE-LTF durations - variable.
3943 ri.mcs = status->rate_idx;
3944 ri.flags |= RATE_INFO_FLAGS_MCS;
3945 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3946 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3949 * See P802.11REVmd_D3.0, section 19.3.2 for
3952 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
3954 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
3960 * Add Data HT-LTFs per streams
3961 * TODO: add Extension HT-LTFs, 4us per LTF
3963 n_ltf = ((ri.mcs >> 3) & 3) + 1;
3964 n_ltf = n_ltf == 3 ? 4 : n_ltf;
3970 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
3971 ri.mcs = status->rate_idx;
3972 ri.nss = status->nss;
3973 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3974 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3977 * See P802.11REVmd_D3.0, section 21.3.2 for
3980 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
3985 * Add VHT-LTFs per streams
3987 n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
3988 ri.nss + 1 : ri.nss;
3996 case RX_ENC_LEGACY: {
3997 struct ieee80211_supported_band *sband;
4001 switch (status->bw) {
4002 case RATE_INFO_BW_10:
4005 case RATE_INFO_BW_5:
4010 sband = local->hw.wiphy->bands[status->band];
4011 bitrate = sband->bitrates[status->rate_idx].bitrate;
4012 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
4014 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4015 if (status->band == NL80211_BAND_5GHZ) {
4018 } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
4028 rate = cfg80211_calculate_bitrate(&ri);
4029 if (WARN_ONCE(!rate,
4030 "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
4031 (unsigned long long)status->flag, status->rate_idx,
4035 /* rewind from end of MPDU */
4036 if (status->flag & RX_FLAG_MACTIME_END)
4037 ts -= mpdu_len * 8 * 10 / rate;
4039 ts += mpdu_offset * 8 * 10 / rate;
4044 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
4046 struct ieee80211_sub_if_data *sdata;
4047 struct cfg80211_chan_def chandef;
4049 /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
4050 lockdep_assert_wiphy(local->hw.wiphy);
4052 mutex_lock(&local->mtx);
4053 list_for_each_entry(sdata, &local->interfaces, list) {
4054 /* it might be waiting for the local->mtx, but then
4055 * by the time it gets it, sdata->wdev.cac_started
4056 * will no longer be true
4058 cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
4060 if (sdata->wdev.cac_started) {
4061 chandef = sdata->vif.bss_conf.chandef;
4062 ieee80211_link_release_channel(&sdata->deflink);
4063 cfg80211_cac_event(sdata->dev,
4065 NL80211_RADAR_CAC_ABORTED,
4069 mutex_unlock(&local->mtx);
4072 void ieee80211_dfs_radar_detected_work(struct wiphy *wiphy,
4073 struct wiphy_work *work)
4075 struct ieee80211_local *local =
4076 container_of(work, struct ieee80211_local, radar_detected_work);
4077 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
4078 struct ieee80211_chanctx *ctx;
4079 int num_chanctx = 0;
4081 mutex_lock(&local->chanctx_mtx);
4082 list_for_each_entry(ctx, &local->chanctx_list, list) {
4083 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
4087 chandef = ctx->conf.def;
4089 mutex_unlock(&local->chanctx_mtx);
4091 ieee80211_dfs_cac_cancel(local);
4093 if (num_chanctx > 1)
4094 /* XXX: multi-channel is not supported yet */
4097 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
4100 void ieee80211_radar_detected(struct ieee80211_hw *hw)
4102 struct ieee80211_local *local = hw_to_local(hw);
4104 trace_api_radar_detected(local);
4106 wiphy_work_queue(hw->wiphy, &local->radar_detected_work);
4108 EXPORT_SYMBOL(ieee80211_radar_detected);
4110 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
4112 ieee80211_conn_flags_t ret;
4116 case NL80211_CHAN_WIDTH_20:
4117 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4118 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4120 case NL80211_CHAN_WIDTH_40:
4121 c->width = NL80211_CHAN_WIDTH_20;
4122 c->center_freq1 = c->chan->center_freq;
4123 ret = IEEE80211_CONN_DISABLE_40MHZ |
4124 IEEE80211_CONN_DISABLE_VHT;
4126 case NL80211_CHAN_WIDTH_80:
4127 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
4131 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
4132 c->width = NL80211_CHAN_WIDTH_40;
4133 ret = IEEE80211_CONN_DISABLE_VHT;
4135 case NL80211_CHAN_WIDTH_80P80:
4136 c->center_freq2 = 0;
4137 c->width = NL80211_CHAN_WIDTH_80;
4138 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4139 IEEE80211_CONN_DISABLE_160MHZ;
4141 case NL80211_CHAN_WIDTH_160:
4143 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
4146 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
4147 c->width = NL80211_CHAN_WIDTH_80;
4148 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4149 IEEE80211_CONN_DISABLE_160MHZ;
4151 case NL80211_CHAN_WIDTH_320:
4153 tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
4156 c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
4157 c->width = NL80211_CHAN_WIDTH_160;
4158 ret = IEEE80211_CONN_DISABLE_320MHZ;
4161 case NL80211_CHAN_WIDTH_20_NOHT:
4163 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4164 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4166 case NL80211_CHAN_WIDTH_1:
4167 case NL80211_CHAN_WIDTH_2:
4168 case NL80211_CHAN_WIDTH_4:
4169 case NL80211_CHAN_WIDTH_8:
4170 case NL80211_CHAN_WIDTH_16:
4171 case NL80211_CHAN_WIDTH_5:
4172 case NL80211_CHAN_WIDTH_10:
4175 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4179 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
4185 * Returns true if smps_mode_new is strictly more restrictive than
4188 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
4189 enum ieee80211_smps_mode smps_mode_new)
4191 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
4192 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
4195 switch (smps_mode_old) {
4196 case IEEE80211_SMPS_STATIC:
4198 case IEEE80211_SMPS_DYNAMIC:
4199 return smps_mode_new == IEEE80211_SMPS_STATIC;
4200 case IEEE80211_SMPS_OFF:
4201 return smps_mode_new != IEEE80211_SMPS_OFF;
4209 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
4210 struct cfg80211_csa_settings *csa_settings)
4212 struct sk_buff *skb;
4213 struct ieee80211_mgmt *mgmt;
4214 struct ieee80211_local *local = sdata->local;
4216 int hdr_len = offsetofend(struct ieee80211_mgmt,
4217 u.action.u.chan_switch);
4220 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4221 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4224 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
4225 5 + /* channel switch announcement element */
4226 3 + /* secondary channel offset element */
4227 5 + /* wide bandwidth channel switch announcement */
4228 8); /* mesh channel switch parameters element */
4232 skb_reserve(skb, local->tx_headroom);
4233 mgmt = skb_put_zero(skb, hdr_len);
4234 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4235 IEEE80211_STYPE_ACTION);
4237 eth_broadcast_addr(mgmt->da);
4238 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
4239 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4240 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
4242 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4243 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
4245 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
4246 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
4247 pos = skb_put(skb, 5);
4248 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
4249 *pos++ = 3; /* IE length */
4250 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
4251 freq = csa_settings->chandef.chan->center_freq;
4252 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
4253 *pos++ = csa_settings->count; /* count */
4255 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
4256 enum nl80211_channel_type ch_type;
4259 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
4260 *pos++ = 1; /* IE length */
4261 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
4262 if (ch_type == NL80211_CHAN_HT40PLUS)
4263 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
4265 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
4268 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4269 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4272 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
4273 *pos++ = 6; /* IE length */
4274 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
4275 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
4276 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
4277 *pos++ |= csa_settings->block_tx ?
4278 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
4279 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
4281 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
4285 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
4286 csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
4287 csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
4289 ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
4292 ieee80211_tx_skb(sdata, skb);
4297 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
4299 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
4306 if (data->count[i] == 1)
4309 if (data->desc[i].interval == 0)
4312 /* End time is in the past, check for repetitions */
4313 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
4314 if (data->count[i] < 255) {
4315 if (data->count[i] <= skip) {
4320 data->count[i] -= skip;
4323 data->desc[i].start += skip * data->desc[i].interval;
4329 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
4335 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4338 if (!data->count[i])
4341 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
4344 cur = data->desc[i].start - tsf;
4348 cur = data->desc[i].start + data->desc[i].duration - tsf;
4357 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
4362 * arbitrary limit, used to avoid infinite loops when combined NoA
4363 * descriptors cover the full time period.
4367 ieee80211_extend_absent_time(data, tsf, &offset);
4369 if (!ieee80211_extend_absent_time(data, tsf, &offset))
4373 } while (tries < max_tries);
4378 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4380 u32 next_offset = BIT(31) - 1;
4384 data->has_next_tsf = false;
4385 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4388 if (!data->count[i])
4391 ieee80211_extend_noa_desc(data, tsf, i);
4392 start = data->desc[i].start - tsf;
4394 data->absent |= BIT(i);
4396 if (next_offset > start)
4397 next_offset = start;
4399 data->has_next_tsf = true;
4403 next_offset = ieee80211_get_noa_absent_time(data, tsf);
4405 data->next_tsf = tsf + next_offset;
4407 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4409 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4410 struct ieee80211_noa_data *data, u32 tsf)
4415 memset(data, 0, sizeof(*data));
4417 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4418 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4420 if (!desc->count || !desc->duration)
4423 data->count[i] = desc->count;
4424 data->desc[i].start = le32_to_cpu(desc->start_time);
4425 data->desc[i].duration = le32_to_cpu(desc->duration);
4426 data->desc[i].interval = le32_to_cpu(desc->interval);
4428 if (data->count[i] > 1 &&
4429 data->desc[i].interval < data->desc[i].duration)
4432 ieee80211_extend_noa_desc(data, tsf, i);
4437 ieee80211_update_p2p_noa(data, tsf);
4441 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4443 void ieee80211_recalc_dtim(struct ieee80211_local *local,
4444 struct ieee80211_sub_if_data *sdata)
4446 u64 tsf = drv_get_tsf(local, sdata);
4448 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4449 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4453 if (tsf == -1ULL || !beacon_int || !dtim_period)
4456 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4457 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4461 ps = &sdata->bss->ps;
4462 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4463 ps = &sdata->u.mesh.ps;
4469 * actually finds last dtim_count, mac80211 will update in
4470 * __beacon_add_tim().
4471 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4473 do_div(tsf, beacon_int);
4474 bcns_from_dtim = do_div(tsf, dtim_period);
4475 /* just had a DTIM */
4476 if (!bcns_from_dtim)
4479 dtim_count = dtim_period - bcns_from_dtim;
4481 ps->dtim_count = dtim_count;
4484 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4485 struct ieee80211_chanctx *ctx)
4487 struct ieee80211_link_data *link;
4488 u8 radar_detect = 0;
4490 lockdep_assert_held(&local->chanctx_mtx);
4492 if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4495 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
4496 if (link->reserved_radar_required)
4497 radar_detect |= BIT(link->reserved_chandef.width);
4500 * An in-place reservation context should not have any assigned vifs
4501 * until it replaces the other context.
4503 WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
4504 !list_empty(&ctx->assigned_links));
4506 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
4507 if (!link->radar_required)
4511 BIT(link->conf->chandef.width);
4514 return radar_detect;
4517 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4518 const struct cfg80211_chan_def *chandef,
4519 enum ieee80211_chanctx_mode chanmode,
4522 struct ieee80211_local *local = sdata->local;
4523 struct ieee80211_sub_if_data *sdata_iter;
4524 enum nl80211_iftype iftype = sdata->wdev.iftype;
4525 struct ieee80211_chanctx *ctx;
4527 struct iface_combination_params params = {
4528 .radar_detect = radar_detect,
4531 lockdep_assert_held(&local->chanctx_mtx);
4533 if (WARN_ON(hweight32(radar_detect) > 1))
4536 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4540 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4543 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4544 sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4546 * always passing this is harmless, since it'll be the
4547 * same value that cfg80211 finds if it finds the same
4548 * interface ... and that's always allowed
4550 params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4553 /* Always allow software iftypes */
4554 if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4561 params.num_different_channels = 1;
4563 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4564 params.iftype_num[iftype] = 1;
4566 list_for_each_entry(ctx, &local->chanctx_list, list) {
4567 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4569 params.radar_detect |=
4570 ieee80211_chanctx_radar_detect(local, ctx);
4571 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
4572 params.num_different_channels++;
4575 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4576 cfg80211_chandef_compatible(chandef,
4579 params.num_different_channels++;
4582 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
4583 struct wireless_dev *wdev_iter;
4585 wdev_iter = &sdata_iter->wdev;
4587 if (sdata_iter == sdata ||
4588 !ieee80211_sdata_running(sdata_iter) ||
4589 cfg80211_iftype_allowed(local->hw.wiphy,
4590 wdev_iter->iftype, 0, 1))
4593 params.iftype_num[wdev_iter->iftype]++;
4597 if (total == 1 && !params.radar_detect)
4600 return cfg80211_check_combinations(local->hw.wiphy, ¶ms);
4604 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4607 u32 *max_num_different_channels = data;
4609 *max_num_different_channels = max(*max_num_different_channels,
4610 c->num_different_channels);
4613 int ieee80211_max_num_channels(struct ieee80211_local *local)
4615 struct ieee80211_sub_if_data *sdata;
4616 struct ieee80211_chanctx *ctx;
4617 u32 max_num_different_channels = 1;
4619 struct iface_combination_params params = {0};
4621 lockdep_assert_held(&local->chanctx_mtx);
4623 list_for_each_entry(ctx, &local->chanctx_list, list) {
4624 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4627 params.num_different_channels++;
4629 params.radar_detect |=
4630 ieee80211_chanctx_radar_detect(local, ctx);
4633 list_for_each_entry_rcu(sdata, &local->interfaces, list)
4634 params.iftype_num[sdata->wdev.iftype]++;
4636 err = cfg80211_iter_combinations(local->hw.wiphy, ¶ms,
4637 ieee80211_iter_max_chans,
4638 &max_num_different_channels);
4642 return max_num_different_channels;
4645 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4646 struct ieee80211_sta_s1g_cap *caps,
4647 struct sk_buff *skb)
4649 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4650 struct ieee80211_s1g_cap s1g_capab;
4654 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4660 memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4661 memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4663 /* override the capability info */
4664 for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4665 u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4667 s1g_capab.capab_info[i] &= ~mask;
4668 s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4671 /* then MCS and NSS set */
4672 for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4673 u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4675 s1g_capab.supp_mcs_nss[i] &= ~mask;
4676 s1g_capab.supp_mcs_nss[i] |=
4677 ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4680 pos = skb_put(skb, 2 + sizeof(s1g_capab));
4681 *pos++ = WLAN_EID_S1G_CAPABILITIES;
4682 *pos++ = sizeof(s1g_capab);
4684 memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4687 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4688 struct sk_buff *skb)
4690 u8 *pos = skb_put(skb, 3);
4692 *pos++ = WLAN_EID_AID_REQUEST;
4697 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4699 *buf++ = WLAN_EID_VENDOR_SPECIFIC;
4700 *buf++ = 7; /* len */
4701 *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4704 *buf++ = 2; /* WME */
4705 *buf++ = 0; /* WME info */
4706 *buf++ = 1; /* WME ver */
4707 *buf++ = qosinfo; /* U-APSD no in use */
4712 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4713 unsigned long *frame_cnt,
4714 unsigned long *byte_cnt)
4716 struct txq_info *txqi = to_txq_info(txq);
4717 u32 frag_cnt = 0, frag_bytes = 0;
4718 struct sk_buff *skb;
4720 skb_queue_walk(&txqi->frags, skb) {
4722 frag_bytes += skb->len;
4726 *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4729 *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4731 EXPORT_SYMBOL(ieee80211_txq_get_depth);
4733 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4734 IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4735 IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4736 IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4737 IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4740 u16 ieee80211_encode_usf(int listen_interval)
4742 static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
4745 /* find greatest USF */
4746 while (usf < IEEE80211_MAX_USF) {
4747 if (listen_interval % listen_int_usf[usf + 1])
4751 ui = listen_interval / listen_int_usf[usf];
4753 /* error if there is a remainder. Should've been checked by user */
4754 WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
4755 listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
4756 FIELD_PREP(LISTEN_INT_UI, ui);
4758 return (u16) listen_interval;
4761 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
4763 const struct ieee80211_sta_he_cap *he_cap;
4764 const struct ieee80211_sta_eht_cap *eht_cap;
4765 struct ieee80211_supported_band *sband;
4769 sband = ieee80211_get_sband(sdata);
4773 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
4774 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
4775 if (!he_cap || !eht_cap)
4778 is_ap = iftype == NL80211_IFTYPE_AP ||
4779 iftype == NL80211_IFTYPE_P2P_GO;
4781 n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4782 &eht_cap->eht_cap_elem,
4785 sizeof(eht_cap->eht_cap_elem) + n +
4786 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4787 eht_cap->eht_cap_elem.phy_cap_info);
4791 u8 *ieee80211_ie_build_eht_cap(u8 *pos,
4792 const struct ieee80211_sta_he_cap *he_cap,
4793 const struct ieee80211_sta_eht_cap *eht_cap,
4797 u8 mcs_nss_len, ppet_len;
4801 /* Make sure we have place for the IE */
4802 if (!he_cap || !eht_cap)
4805 mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4806 &eht_cap->eht_cap_elem,
4808 ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4809 eht_cap->eht_cap_elem.phy_cap_info);
4811 ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
4812 if ((end - pos) < ie_len)
4815 *pos++ = WLAN_EID_EXTENSION;
4816 *pos++ = ie_len - 2;
4817 *pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
4820 memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
4821 pos += sizeof(eht_cap->eht_cap_elem);
4823 memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
4827 memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
4834 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos)
4836 unsigned int elem_len;
4841 elem_len = skb->data + skb->len - len_pos - 1;
4843 while (elem_len > 255) {
4844 /* this one is 255 */
4846 /* remaining data gets smaller */
4848 /* make space for the fragment ID/len in SKB */
4850 /* shift back the remaining data to place fragment ID/len */
4851 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
4852 /* place the fragment ID */
4854 *len_pos = WLAN_EID_FRAGMENT;
4855 /* and point to fragment length to update later */
4859 *len_pos = elem_len;