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-2023 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 wake_tx_push_queue(struct ieee80211_local *local,
292 struct ieee80211_sub_if_data *sdata,
293 struct ieee80211_txq *queue)
295 struct ieee80211_tx_control control = {
301 skb = ieee80211_tx_dequeue(&local->hw, queue);
305 drv_tx(local, &control, skb);
309 /* wake_tx_queue handler for driver not implementing a custom one*/
310 void ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
311 struct ieee80211_txq *txq)
313 struct ieee80211_local *local = hw_to_local(hw);
314 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
315 struct ieee80211_txq *queue;
317 spin_lock(&local->handle_wake_tx_queue_lock);
319 /* Use ieee80211_next_txq() for airtime fairness accounting */
320 ieee80211_txq_schedule_start(hw, txq->ac);
321 while ((queue = ieee80211_next_txq(hw, txq->ac))) {
322 wake_tx_push_queue(local, sdata, queue);
323 ieee80211_return_txq(hw, queue, false);
325 ieee80211_txq_schedule_end(hw, txq->ac);
326 spin_unlock(&local->handle_wake_tx_queue_lock);
328 EXPORT_SYMBOL(ieee80211_handle_wake_tx_queue);
330 static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
332 struct ieee80211_local *local = sdata->local;
333 struct ieee80211_vif *vif = &sdata->vif;
334 struct fq *fq = &local->fq;
335 struct ps_data *ps = NULL;
336 struct txq_info *txqi;
337 struct sta_info *sta;
341 spin_lock(&fq->lock);
343 if (!test_bit(SDATA_STATE_RUNNING, &sdata->state))
346 if (sdata->vif.type == NL80211_IFTYPE_AP)
347 ps = &sdata->bss->ps;
349 list_for_each_entry_rcu(sta, &local->sta_list, list) {
350 if (sdata != sta->sdata)
353 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
354 struct ieee80211_txq *txq = sta->sta.txq[i];
359 txqi = to_txq_info(txq);
364 if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY,
368 spin_unlock(&fq->lock);
369 drv_wake_tx_queue(local, txqi);
370 spin_lock(&fq->lock);
377 txqi = to_txq_info(vif->txq);
379 if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY, &txqi->flags) ||
380 (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
383 spin_unlock(&fq->lock);
385 drv_wake_tx_queue(local, txqi);
389 spin_unlock(&fq->lock);
394 __releases(&local->queue_stop_reason_lock)
395 __acquires(&local->queue_stop_reason_lock)
396 _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
398 struct ieee80211_sub_if_data *sdata;
399 int n_acs = IEEE80211_NUM_ACS;
404 if (local->hw.queues < IEEE80211_NUM_ACS)
407 for (i = 0; i < local->hw.queues; i++) {
408 if (local->queue_stop_reasons[i])
411 spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
412 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
415 for (ac = 0; ac < n_acs; ac++) {
416 int ac_queue = sdata->vif.hw_queue[ac];
419 sdata->vif.cab_queue == i)
420 __ieee80211_wake_txqs(sdata, ac);
423 spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
429 void ieee80211_wake_txqs(struct tasklet_struct *t)
431 struct ieee80211_local *local = from_tasklet(local, t,
435 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
436 _ieee80211_wake_txqs(local, &flags);
437 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
440 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
441 enum queue_stop_reason reason,
443 unsigned long *flags)
445 struct ieee80211_local *local = hw_to_local(hw);
447 trace_wake_queue(local, queue, reason);
449 if (WARN_ON(queue >= hw->queues))
452 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
456 local->q_stop_reasons[queue][reason] = 0;
458 local->q_stop_reasons[queue][reason]--;
459 if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
460 local->q_stop_reasons[queue][reason] = 0;
463 if (local->q_stop_reasons[queue][reason] == 0)
464 __clear_bit(reason, &local->queue_stop_reasons[queue]);
466 if (local->queue_stop_reasons[queue] != 0)
467 /* someone still has this queue stopped */
470 if (!skb_queue_empty(&local->pending[queue]))
471 tasklet_schedule(&local->tx_pending_tasklet);
474 * Calling _ieee80211_wake_txqs here can be a problem because it may
475 * release queue_stop_reason_lock which has been taken by
476 * __ieee80211_wake_queue's caller. It is certainly not very nice to
477 * release someone's lock, but it is fine because all the callers of
478 * __ieee80211_wake_queue call it right before releasing the lock.
480 if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
481 tasklet_schedule(&local->wake_txqs_tasklet);
483 _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);
512 trace_stop_queue(local, queue, reason);
514 if (WARN_ON(queue >= hw->queues))
518 local->q_stop_reasons[queue][reason] = 1;
520 local->q_stop_reasons[queue][reason]++;
522 set_bit(reason, &local->queue_stop_reasons[queue]);
525 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
526 enum queue_stop_reason reason,
529 struct ieee80211_local *local = hw_to_local(hw);
532 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
533 __ieee80211_stop_queue(hw, queue, reason, refcounted);
534 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
537 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
539 ieee80211_stop_queue_by_reason(hw, queue,
540 IEEE80211_QUEUE_STOP_REASON_DRIVER,
543 EXPORT_SYMBOL(ieee80211_stop_queue);
545 void ieee80211_add_pending_skb(struct ieee80211_local *local,
548 struct ieee80211_hw *hw = &local->hw;
550 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
551 int queue = info->hw_queue;
553 if (WARN_ON(!info->control.vif)) {
554 ieee80211_free_txskb(&local->hw, skb);
558 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
559 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
561 __skb_queue_tail(&local->pending[queue], skb);
562 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
564 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
567 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
568 struct sk_buff_head *skbs)
570 struct ieee80211_hw *hw = &local->hw;
575 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
576 while ((skb = skb_dequeue(skbs))) {
577 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
579 if (WARN_ON(!info->control.vif)) {
580 ieee80211_free_txskb(&local->hw, skb);
584 queue = info->hw_queue;
586 __ieee80211_stop_queue(hw, queue,
587 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
590 __skb_queue_tail(&local->pending[queue], skb);
593 for (i = 0; i < hw->queues; i++)
594 __ieee80211_wake_queue(hw, i,
595 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
597 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
600 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
601 unsigned long queues,
602 enum queue_stop_reason reason,
605 struct ieee80211_local *local = hw_to_local(hw);
609 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
611 for_each_set_bit(i, &queues, hw->queues)
612 __ieee80211_stop_queue(hw, i, reason, refcounted);
614 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
617 void ieee80211_stop_queues(struct ieee80211_hw *hw)
619 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
620 IEEE80211_QUEUE_STOP_REASON_DRIVER,
623 EXPORT_SYMBOL(ieee80211_stop_queues);
625 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
627 struct ieee80211_local *local = hw_to_local(hw);
631 if (WARN_ON(queue >= hw->queues))
634 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
635 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
636 &local->queue_stop_reasons[queue]);
637 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
640 EXPORT_SYMBOL(ieee80211_queue_stopped);
642 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
643 unsigned long queues,
644 enum queue_stop_reason reason,
647 struct ieee80211_local *local = hw_to_local(hw);
651 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
653 for_each_set_bit(i, &queues, hw->queues)
654 __ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
656 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
659 void ieee80211_wake_queues(struct ieee80211_hw *hw)
661 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
662 IEEE80211_QUEUE_STOP_REASON_DRIVER,
665 EXPORT_SYMBOL(ieee80211_wake_queues);
668 ieee80211_get_vif_queues(struct ieee80211_local *local,
669 struct ieee80211_sub_if_data *sdata)
673 if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
678 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
679 queues |= BIT(sdata->vif.hw_queue[ac]);
680 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
681 queues |= BIT(sdata->vif.cab_queue);
684 queues = BIT(local->hw.queues) - 1;
690 void __ieee80211_flush_queues(struct ieee80211_local *local,
691 struct ieee80211_sub_if_data *sdata,
692 unsigned int queues, bool drop)
694 if (!local->ops->flush)
698 * If no queue was set, or if the HW doesn't support
699 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
701 if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
702 queues = ieee80211_get_vif_queues(local, sdata);
704 ieee80211_stop_queues_by_reason(&local->hw, queues,
705 IEEE80211_QUEUE_STOP_REASON_FLUSH,
708 drv_flush(local, sdata, queues, drop);
710 ieee80211_wake_queues_by_reason(&local->hw, queues,
711 IEEE80211_QUEUE_STOP_REASON_FLUSH,
715 void ieee80211_flush_queues(struct ieee80211_local *local,
716 struct ieee80211_sub_if_data *sdata, bool drop)
718 __ieee80211_flush_queues(local, sdata, 0, drop);
721 void ieee80211_stop_vif_queues(struct ieee80211_local *local,
722 struct ieee80211_sub_if_data *sdata,
723 enum queue_stop_reason reason)
725 ieee80211_stop_queues_by_reason(&local->hw,
726 ieee80211_get_vif_queues(local, sdata),
730 void ieee80211_wake_vif_queues(struct ieee80211_local *local,
731 struct ieee80211_sub_if_data *sdata,
732 enum queue_stop_reason reason)
734 ieee80211_wake_queues_by_reason(&local->hw,
735 ieee80211_get_vif_queues(local, sdata),
739 static void __iterate_interfaces(struct ieee80211_local *local,
741 void (*iterator)(void *data, u8 *mac,
742 struct ieee80211_vif *vif),
745 struct ieee80211_sub_if_data *sdata;
746 bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
748 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
749 switch (sdata->vif.type) {
750 case NL80211_IFTYPE_MONITOR:
751 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
754 case NL80211_IFTYPE_AP_VLAN:
759 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
760 active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
762 if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) &&
763 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
765 if (ieee80211_sdata_running(sdata) || !active_only)
766 iterator(data, sdata->vif.addr,
770 sdata = rcu_dereference_check(local->monitor_sdata,
771 lockdep_is_held(&local->iflist_mtx) ||
772 lockdep_is_held(&local->hw.wiphy->mtx));
774 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
775 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
776 iterator(data, sdata->vif.addr, &sdata->vif);
779 void ieee80211_iterate_interfaces(
780 struct ieee80211_hw *hw, u32 iter_flags,
781 void (*iterator)(void *data, u8 *mac,
782 struct ieee80211_vif *vif),
785 struct ieee80211_local *local = hw_to_local(hw);
787 mutex_lock(&local->iflist_mtx);
788 __iterate_interfaces(local, iter_flags, iterator, data);
789 mutex_unlock(&local->iflist_mtx);
791 EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
793 void ieee80211_iterate_active_interfaces_atomic(
794 struct ieee80211_hw *hw, u32 iter_flags,
795 void (*iterator)(void *data, u8 *mac,
796 struct ieee80211_vif *vif),
799 struct ieee80211_local *local = hw_to_local(hw);
802 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
806 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
808 void ieee80211_iterate_active_interfaces_mtx(
809 struct ieee80211_hw *hw, u32 iter_flags,
810 void (*iterator)(void *data, u8 *mac,
811 struct ieee80211_vif *vif),
814 struct ieee80211_local *local = hw_to_local(hw);
816 lockdep_assert_wiphy(hw->wiphy);
818 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
821 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx);
823 static void __iterate_stations(struct ieee80211_local *local,
824 void (*iterator)(void *data,
825 struct ieee80211_sta *sta),
828 struct sta_info *sta;
830 list_for_each_entry_rcu(sta, &local->sta_list, list) {
834 iterator(data, &sta->sta);
838 void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
839 void (*iterator)(void *data,
840 struct ieee80211_sta *sta),
843 struct ieee80211_local *local = hw_to_local(hw);
846 __iterate_stations(local, iterator, data);
849 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
851 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
853 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
855 if (!ieee80211_sdata_running(sdata) ||
856 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
860 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
862 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
867 return &vif_to_sdata(vif)->wdev;
869 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
872 * Nothing should have been stuffed into the workqueue during
873 * the suspend->resume cycle. Since we can't check each caller
874 * of this function if we are already quiescing / suspended,
875 * check here and don't WARN since this can actually happen when
876 * the rx path (for example) is racing against __ieee80211_suspend
877 * and suspending / quiescing was set after the rx path checked
880 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
882 if (local->quiescing || (local->suspended && !local->resuming)) {
883 pr_warn("queueing ieee80211 work while going to suspend\n");
890 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
892 struct ieee80211_local *local = hw_to_local(hw);
894 if (!ieee80211_can_queue_work(local))
897 queue_work(local->workqueue, work);
899 EXPORT_SYMBOL(ieee80211_queue_work);
901 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
902 struct delayed_work *dwork,
905 struct ieee80211_local *local = hw_to_local(hw);
907 if (!ieee80211_can_queue_work(local))
910 queue_delayed_work(local->workqueue, dwork, delay);
912 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
915 ieee80211_parse_extension_element(u32 *crc,
916 const struct element *elem,
917 struct ieee802_11_elems *elems,
918 struct ieee80211_elems_parse_params *params)
920 const void *data = elem->data + 1;
921 bool calc_crc = false;
927 len = elem->datalen - 1;
929 switch (elem->data[0]) {
930 case WLAN_EID_EXT_HE_MU_EDCA:
932 if (len >= sizeof(*elems->mu_edca_param_set))
933 elems->mu_edca_param_set = data;
935 case WLAN_EID_EXT_HE_CAPABILITY:
936 if (ieee80211_he_capa_size_ok(data, len)) {
937 elems->he_cap = data;
938 elems->he_cap_len = len;
941 case WLAN_EID_EXT_HE_OPERATION:
943 if (len >= sizeof(*elems->he_operation) &&
944 len >= ieee80211_he_oper_size(data) - 1)
945 elems->he_operation = data;
947 case WLAN_EID_EXT_UORA:
949 elems->uora_element = data;
951 case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
953 elems->max_channel_switch_time = data;
955 case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
956 if (len >= sizeof(*elems->mbssid_config_ie))
957 elems->mbssid_config_ie = data;
959 case WLAN_EID_EXT_HE_SPR:
960 if (len >= sizeof(*elems->he_spr) &&
961 len >= ieee80211_he_spr_size(data))
962 elems->he_spr = data;
964 case WLAN_EID_EXT_HE_6GHZ_CAPA:
965 if (len >= sizeof(*elems->he_6ghz_capa))
966 elems->he_6ghz_capa = data;
968 case WLAN_EID_EXT_EHT_CAPABILITY:
969 if (ieee80211_eht_capa_size_ok(elems->he_cap,
972 elems->eht_cap = data;
973 elems->eht_cap_len = len;
976 case WLAN_EID_EXT_EHT_OPERATION:
977 if (ieee80211_eht_oper_size_ok(data, len))
978 elems->eht_operation = data;
981 case WLAN_EID_EXT_EHT_MULTI_LINK:
984 if (ieee80211_mle_size_ok(data, len)) {
985 const struct ieee80211_multi_link_elem *mle =
988 switch (le16_get_bits(mle->control,
989 IEEE80211_ML_CONTROL_TYPE)) {
990 case IEEE80211_ML_CONTROL_TYPE_BASIC:
991 elems->ml_basic_elem = (void *)elem;
992 elems->ml_basic = data;
993 elems->ml_basic_len = len;
995 case IEEE80211_ML_CONTROL_TYPE_RECONF:
996 elems->ml_reconf_elem = (void *)elem;
997 elems->ml_reconf = data;
998 elems->ml_reconf_len = len;
1007 if (crc && calc_crc)
1008 *crc = crc32_be(*crc, (void *)elem, elem->datalen + 2);
1012 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
1013 struct ieee802_11_elems *elems,
1014 const struct element *check_inherit)
1016 const struct element *elem;
1017 bool calc_crc = params->filter != 0;
1018 DECLARE_BITMAP(seen_elems, 256);
1019 u32 crc = params->crc;
1022 bitmap_zero(seen_elems, 256);
1024 for_each_element(elem, params->start, params->len) {
1025 bool elem_parse_failed;
1027 u8 elen = elem->datalen;
1028 const u8 *pos = elem->data;
1030 if (check_inherit &&
1031 !cfg80211_is_element_inherited(elem,
1037 case WLAN_EID_SUPP_RATES:
1038 case WLAN_EID_FH_PARAMS:
1039 case WLAN_EID_DS_PARAMS:
1040 case WLAN_EID_CF_PARAMS:
1042 case WLAN_EID_IBSS_PARAMS:
1043 case WLAN_EID_CHALLENGE:
1045 case WLAN_EID_ERP_INFO:
1046 case WLAN_EID_EXT_SUPP_RATES:
1047 case WLAN_EID_HT_CAPABILITY:
1048 case WLAN_EID_HT_OPERATION:
1049 case WLAN_EID_VHT_CAPABILITY:
1050 case WLAN_EID_VHT_OPERATION:
1051 case WLAN_EID_MESH_ID:
1052 case WLAN_EID_MESH_CONFIG:
1053 case WLAN_EID_PEER_MGMT:
1058 case WLAN_EID_CHANNEL_SWITCH:
1059 case WLAN_EID_EXT_CHANSWITCH_ANN:
1060 case WLAN_EID_COUNTRY:
1061 case WLAN_EID_PWR_CONSTRAINT:
1062 case WLAN_EID_TIMEOUT_INTERVAL:
1063 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1064 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1065 case WLAN_EID_CHAN_SWITCH_PARAM:
1066 case WLAN_EID_EXT_CAPABILITY:
1067 case WLAN_EID_CHAN_SWITCH_TIMING:
1068 case WLAN_EID_LINK_ID:
1069 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1071 case WLAN_EID_S1G_BCN_COMPAT:
1072 case WLAN_EID_S1G_CAPABILITIES:
1073 case WLAN_EID_S1G_OPERATION:
1074 case WLAN_EID_AID_RESPONSE:
1075 case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
1077 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1078 * that if the content gets bigger it might be needed more than once
1080 if (test_bit(id, seen_elems)) {
1081 elems->parse_error = true;
1087 if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
1088 crc = crc32_be(crc, pos - 2, elen + 2);
1090 elem_parse_failed = false;
1093 case WLAN_EID_LINK_ID:
1094 if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
1095 elem_parse_failed = true;
1098 elems->lnk_id = (void *)(pos - 2);
1100 case WLAN_EID_CHAN_SWITCH_TIMING:
1101 if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
1102 elem_parse_failed = true;
1105 elems->ch_sw_timing = (void *)pos;
1107 case WLAN_EID_EXT_CAPABILITY:
1108 elems->ext_capab = pos;
1109 elems->ext_capab_len = elen;
1113 elems->ssid_len = elen;
1115 case WLAN_EID_SUPP_RATES:
1116 elems->supp_rates = pos;
1117 elems->supp_rates_len = elen;
1119 case WLAN_EID_DS_PARAMS:
1121 elems->ds_params = pos;
1123 elem_parse_failed = true;
1126 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1127 elems->tim = (void *)pos;
1128 elems->tim_len = elen;
1130 elem_parse_failed = true;
1132 case WLAN_EID_VENDOR_SPECIFIC:
1133 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1135 /* Microsoft OUI (00:50:F2) */
1138 crc = crc32_be(crc, pos - 2, elen + 2);
1140 if (elen >= 5 && pos[3] == 2) {
1141 /* OUI Type 2 - WMM IE */
1143 elems->wmm_info = pos;
1144 elems->wmm_info_len = elen;
1145 } else if (pos[4] == 1) {
1146 elems->wmm_param = pos;
1147 elems->wmm_param_len = elen;
1154 elems->rsn_len = elen;
1156 case WLAN_EID_ERP_INFO:
1158 elems->erp_info = pos;
1160 elem_parse_failed = true;
1162 case WLAN_EID_EXT_SUPP_RATES:
1163 elems->ext_supp_rates = pos;
1164 elems->ext_supp_rates_len = elen;
1166 case WLAN_EID_HT_CAPABILITY:
1167 if (elen >= sizeof(struct ieee80211_ht_cap))
1168 elems->ht_cap_elem = (void *)pos;
1170 elem_parse_failed = true;
1172 case WLAN_EID_HT_OPERATION:
1173 if (elen >= sizeof(struct ieee80211_ht_operation))
1174 elems->ht_operation = (void *)pos;
1176 elem_parse_failed = true;
1178 case WLAN_EID_VHT_CAPABILITY:
1179 if (elen >= sizeof(struct ieee80211_vht_cap))
1180 elems->vht_cap_elem = (void *)pos;
1182 elem_parse_failed = true;
1184 case WLAN_EID_VHT_OPERATION:
1185 if (elen >= sizeof(struct ieee80211_vht_operation)) {
1186 elems->vht_operation = (void *)pos;
1188 crc = crc32_be(crc, pos - 2, elen + 2);
1191 elem_parse_failed = true;
1193 case WLAN_EID_OPMODE_NOTIF:
1195 elems->opmode_notif = pos;
1197 crc = crc32_be(crc, pos - 2, elen + 2);
1200 elem_parse_failed = true;
1202 case WLAN_EID_MESH_ID:
1203 elems->mesh_id = pos;
1204 elems->mesh_id_len = elen;
1206 case WLAN_EID_MESH_CONFIG:
1207 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1208 elems->mesh_config = (void *)pos;
1210 elem_parse_failed = true;
1212 case WLAN_EID_PEER_MGMT:
1213 elems->peering = pos;
1214 elems->peering_len = elen;
1216 case WLAN_EID_MESH_AWAKE_WINDOW:
1218 elems->awake_window = (void *)pos;
1222 elems->preq_len = elen;
1226 elems->prep_len = elen;
1230 elems->perr_len = elen;
1233 if (elen >= sizeof(struct ieee80211_rann_ie))
1234 elems->rann = (void *)pos;
1236 elem_parse_failed = true;
1238 case WLAN_EID_CHANNEL_SWITCH:
1239 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
1240 elem_parse_failed = true;
1243 elems->ch_switch_ie = (void *)pos;
1245 case WLAN_EID_EXT_CHANSWITCH_ANN:
1246 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1247 elem_parse_failed = true;
1250 elems->ext_chansw_ie = (void *)pos;
1252 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1253 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1254 elem_parse_failed = true;
1257 elems->sec_chan_offs = (void *)pos;
1259 case WLAN_EID_CHAN_SWITCH_PARAM:
1261 sizeof(*elems->mesh_chansw_params_ie)) {
1262 elem_parse_failed = true;
1265 elems->mesh_chansw_params_ie = (void *)pos;
1267 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1268 if (!params->action ||
1269 elen < sizeof(*elems->wide_bw_chansw_ie)) {
1270 elem_parse_failed = true;
1273 elems->wide_bw_chansw_ie = (void *)pos;
1275 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1276 if (params->action) {
1277 elem_parse_failed = true;
1281 * This is a bit tricky, but as we only care about
1282 * the wide bandwidth channel switch element, so
1283 * just parse it out manually.
1285 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1288 if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
1289 elems->wide_bw_chansw_ie =
1292 elem_parse_failed = true;
1295 case WLAN_EID_COUNTRY:
1296 elems->country_elem = pos;
1297 elems->country_elem_len = elen;
1299 case WLAN_EID_PWR_CONSTRAINT:
1301 elem_parse_failed = true;
1304 elems->pwr_constr_elem = pos;
1306 case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1307 /* Lots of different options exist, but we only care
1308 * about the Dynamic Transmit Power Control element.
1309 * First check for the Cisco OUI, then for the DTPC
1313 elem_parse_failed = true;
1317 if (pos[0] != 0x00 || pos[1] != 0x40 ||
1318 pos[2] != 0x96 || pos[3] != 0x00)
1322 elem_parse_failed = true;
1327 crc = crc32_be(crc, pos - 2, elen + 2);
1329 elems->cisco_dtpc_elem = pos;
1331 case WLAN_EID_ADDBA_EXT:
1332 if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
1333 elem_parse_failed = true;
1336 elems->addba_ext_ie = (void *)pos;
1338 case WLAN_EID_TIMEOUT_INTERVAL:
1339 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1340 elems->timeout_int = (void *)pos;
1342 elem_parse_failed = true;
1344 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1345 if (elen >= sizeof(*elems->max_idle_period_ie))
1346 elems->max_idle_period_ie = (void *)pos;
1350 elems->rsnx_len = elen;
1352 case WLAN_EID_TX_POWER_ENVELOPE:
1354 elen > sizeof(struct ieee80211_tx_pwr_env))
1357 if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
1360 elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
1361 elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
1362 elems->tx_pwr_env_num++;
1364 case WLAN_EID_EXTENSION:
1365 ieee80211_parse_extension_element(calc_crc ?
1367 elem, elems, params);
1369 case WLAN_EID_S1G_CAPABILITIES:
1370 if (elen >= sizeof(*elems->s1g_capab))
1371 elems->s1g_capab = (void *)pos;
1373 elem_parse_failed = true;
1375 case WLAN_EID_S1G_OPERATION:
1376 if (elen == sizeof(*elems->s1g_oper))
1377 elems->s1g_oper = (void *)pos;
1379 elem_parse_failed = true;
1381 case WLAN_EID_S1G_BCN_COMPAT:
1382 if (elen == sizeof(*elems->s1g_bcn_compat))
1383 elems->s1g_bcn_compat = (void *)pos;
1385 elem_parse_failed = true;
1387 case WLAN_EID_AID_RESPONSE:
1388 if (elen == sizeof(struct ieee80211_aid_response_ie))
1389 elems->aid_resp = (void *)pos;
1391 elem_parse_failed = true;
1397 if (elem_parse_failed)
1398 elems->parse_error = true;
1400 __set_bit(id, seen_elems);
1403 if (!for_each_element_completed(elem, params->start, params->len))
1404 elems->parse_error = true;
1409 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1410 struct ieee802_11_elems *elems,
1411 struct cfg80211_bss *bss,
1412 u8 *nontransmitted_profile)
1414 const struct element *elem, *sub;
1415 size_t profile_len = 0;
1418 if (!bss || !bss->transmitted_bss)
1421 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1422 if (elem->datalen < 2)
1424 if (elem->data[0] < 1 || elem->data[0] > 8)
1427 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1428 u8 new_bssid[ETH_ALEN];
1431 if (sub->id != 0 || sub->datalen < 4) {
1432 /* not a valid BSS profile */
1436 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1437 sub->data[1] != 2) {
1438 /* The first element of the
1439 * Nontransmitted BSSID Profile is not
1440 * the Nontransmitted BSSID Capability
1446 memset(nontransmitted_profile, 0, len);
1447 profile_len = cfg80211_merge_profile(start, len,
1450 nontransmitted_profile,
1453 /* found a Nontransmitted BSSID Profile */
1454 index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
1455 nontransmitted_profile,
1457 if (!index || index[1] < 1 || index[2] == 0) {
1458 /* Invalid MBSSID Index element */
1462 cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
1466 if (ether_addr_equal(new_bssid, bss->bssid)) {
1468 elems->bssid_index_len = index[1];
1469 elems->bssid_index = (void *)&index[2];
1475 return found ? profile_len : 0;
1478 static void ieee80211_mle_get_sta_prof(struct ieee802_11_elems *elems,
1481 const struct ieee80211_multi_link_elem *ml = elems->ml_basic;
1482 ssize_t ml_len = elems->ml_basic_len;
1483 const struct element *sub;
1488 if (le16_get_bits(ml->control, IEEE80211_ML_CONTROL_TYPE) !=
1489 IEEE80211_ML_CONTROL_TYPE_BASIC)
1492 for_each_mle_subelement(sub, (u8 *)ml, ml_len) {
1493 struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
1494 ssize_t sta_prof_len;
1497 if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
1500 if (!ieee80211_mle_basic_sta_prof_size_ok(sub->data,
1504 control = le16_to_cpu(prof->control);
1506 if (link_id != u16_get_bits(control,
1507 IEEE80211_MLE_STA_CONTROL_LINK_ID))
1510 if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
1513 /* the sub element can be fragmented */
1515 cfg80211_defragment_element(sub,
1519 elems->scratch_len -
1521 IEEE80211_MLE_SUBELEM_FRAGMENT);
1523 if (sta_prof_len < 0)
1526 elems->prof = (void *)elems->scratch_pos;
1527 elems->sta_prof_len = sta_prof_len;
1528 elems->scratch_pos += sta_prof_len;
1534 static void ieee80211_mle_parse_link(struct ieee802_11_elems *elems,
1535 struct ieee80211_elems_parse_params *params)
1537 struct ieee80211_mle_per_sta_profile *prof;
1538 struct ieee80211_elems_parse_params sub = {
1539 .action = params->action,
1540 .from_ap = params->from_ap,
1543 ssize_t ml_len = elems->ml_basic_len;
1544 const struct element *non_inherit = NULL;
1547 if (params->link_id == -1)
1550 ml_len = cfg80211_defragment_element(elems->ml_basic_elem,
1555 elems->scratch_len -
1562 elems->ml_basic = (const void *)elems->scratch_pos;
1563 elems->ml_basic_len = ml_len;
1565 ieee80211_mle_get_sta_prof(elems, params->link_id);
1571 /* check if we have the 4 bytes for the fixed part in assoc response */
1572 if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) {
1574 elems->sta_prof_len = 0;
1579 * Skip the capability information and the status code that are expected
1580 * as part of the station profile in association response frames. Note
1581 * the -1 is because the 'sta_info_len' is accounted to as part of the
1582 * per-STA profile, but not part of the 'u8 variable[]' portion.
1584 sub.start = prof->variable + prof->sta_info_len - 1 + 4;
1585 end = (const u8 *)prof + elems->sta_prof_len;
1586 sub.len = end - sub.start;
1588 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1589 sub.start, sub.len);
1590 _ieee802_11_parse_elems_full(&sub, elems, non_inherit);
1593 struct ieee802_11_elems *
1594 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
1596 struct ieee802_11_elems *elems;
1597 const struct element *non_inherit = NULL;
1598 u8 *nontransmitted_profile;
1599 int nontransmitted_profile_len = 0;
1600 size_t scratch_len = 3 * params->len;
1602 elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1605 elems->ie_start = params->start;
1606 elems->total_len = params->len;
1607 elems->scratch_len = scratch_len;
1608 elems->scratch_pos = elems->scratch;
1610 nontransmitted_profile = elems->scratch_pos;
1611 nontransmitted_profile_len =
1612 ieee802_11_find_bssid_profile(params->start, params->len,
1614 nontransmitted_profile);
1615 elems->scratch_pos += nontransmitted_profile_len;
1616 elems->scratch_len -= nontransmitted_profile_len;
1617 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1618 nontransmitted_profile,
1619 nontransmitted_profile_len);
1621 elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
1623 /* Override with nontransmitted profile, if found */
1624 if (nontransmitted_profile_len) {
1625 struct ieee80211_elems_parse_params sub = {
1626 .start = nontransmitted_profile,
1627 .len = nontransmitted_profile_len,
1628 .action = params->action,
1629 .link_id = params->link_id,
1632 _ieee802_11_parse_elems_full(&sub, elems, NULL);
1635 ieee80211_mle_parse_link(elems, params);
1637 if (elems->tim && !elems->parse_error) {
1638 const struct ieee80211_tim_ie *tim_ie = elems->tim;
1640 elems->dtim_period = tim_ie->dtim_period;
1641 elems->dtim_count = tim_ie->dtim_count;
1644 /* Override DTIM period and count if needed */
1645 if (elems->bssid_index &&
1646 elems->bssid_index_len >=
1647 offsetofend(struct ieee80211_bssid_index, dtim_period))
1648 elems->dtim_period = elems->bssid_index->dtim_period;
1650 if (elems->bssid_index &&
1651 elems->bssid_index_len >=
1652 offsetofend(struct ieee80211_bssid_index, dtim_count))
1653 elems->dtim_count = elems->bssid_index->dtim_count;
1658 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1659 struct ieee80211_tx_queue_params
1662 struct ieee80211_chanctx_conf *chanctx_conf;
1663 const struct ieee80211_reg_rule *rrule;
1664 const struct ieee80211_wmm_ac *wmm_ac;
1665 u16 center_freq = 0;
1667 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1668 sdata->vif.type != NL80211_IFTYPE_STATION)
1672 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1674 center_freq = chanctx_conf->def.chan->center_freq;
1681 rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1683 if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1688 if (sdata->vif.type == NL80211_IFTYPE_AP)
1689 wmm_ac = &rrule->wmm_rule.ap[ac];
1691 wmm_ac = &rrule->wmm_rule.client[ac];
1692 qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1693 qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1694 qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1695 qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1699 void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
1700 bool bss_notify, bool enable_qos)
1702 struct ieee80211_sub_if_data *sdata = link->sdata;
1703 struct ieee80211_local *local = sdata->local;
1704 struct ieee80211_tx_queue_params qparam;
1705 struct ieee80211_chanctx_conf *chanctx_conf;
1708 bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1711 if (!local->ops->conf_tx)
1714 if (local->hw.queues < IEEE80211_NUM_ACS)
1717 memset(&qparam, 0, sizeof(qparam));
1720 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
1721 use_11b = (chanctx_conf &&
1722 chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1723 !link->operating_11g_mode;
1726 is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1728 /* Set defaults according to 802.11-2007 Table 7-37 */
1735 /* Confiure old 802.11b/g medium access rules. */
1736 qparam.cw_max = aCWmax;
1737 qparam.cw_min = aCWmin;
1741 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1742 /* Update if QoS is enabled. */
1745 case IEEE80211_AC_BK:
1746 qparam.cw_max = aCWmax;
1747 qparam.cw_min = aCWmin;
1754 /* never happens but let's not leave undefined */
1756 case IEEE80211_AC_BE:
1757 qparam.cw_max = aCWmax;
1758 qparam.cw_min = aCWmin;
1765 case IEEE80211_AC_VI:
1766 qparam.cw_max = aCWmin;
1767 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1771 qparam.txop = 6016/32;
1773 qparam.txop = 3008/32;
1780 case IEEE80211_AC_VO:
1781 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1782 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1786 qparam.txop = 3264/32;
1788 qparam.txop = 1504/32;
1793 ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
1795 qparam.uapsd = false;
1797 link->tx_conf[ac] = qparam;
1798 drv_conf_tx(local, link, ac, &qparam);
1801 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1802 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1803 sdata->vif.type != NL80211_IFTYPE_NAN) {
1804 link->conf->qos = enable_qos;
1806 ieee80211_link_info_change_notify(sdata, link,
1811 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1812 u16 transaction, u16 auth_alg, u16 status,
1813 const u8 *extra, size_t extra_len, const u8 *da,
1814 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1817 struct ieee80211_local *local = sdata->local;
1818 struct sk_buff *skb;
1819 struct ieee80211_mgmt *mgmt;
1820 bool multi_link = ieee80211_vif_is_mld(&sdata->vif);
1825 struct ieee80211_multi_link_elem ml;
1826 struct ieee80211_mle_basic_common_info basic;
1828 .id = WLAN_EID_EXTENSION,
1829 .len = sizeof(mle) - 2,
1830 .ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
1831 .ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
1832 .basic.len = sizeof(mle.basic),
1836 memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1838 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1839 skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1840 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
1841 multi_link * sizeof(mle));
1845 skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
1847 mgmt = skb_put_zero(skb, 24 + 6);
1848 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1849 IEEE80211_STYPE_AUTH);
1850 memcpy(mgmt->da, da, ETH_ALEN);
1851 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1852 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1853 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1854 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1855 mgmt->u.auth.status_code = cpu_to_le16(status);
1857 skb_put_data(skb, extra, extra_len);
1859 skb_put_data(skb, &mle, sizeof(mle));
1861 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1862 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1863 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1870 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1872 ieee80211_tx_skb(sdata, skb);
1875 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1876 const u8 *da, const u8 *bssid,
1877 u16 stype, u16 reason,
1878 bool send_frame, u8 *frame_buf)
1880 struct ieee80211_local *local = sdata->local;
1881 struct sk_buff *skb;
1882 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1885 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1886 mgmt->duration = 0; /* initialize only */
1887 mgmt->seq_ctrl = 0; /* initialize only */
1888 memcpy(mgmt->da, da, ETH_ALEN);
1889 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1890 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1891 /* u.deauth.reason_code == u.disassoc.reason_code */
1892 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1895 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1896 IEEE80211_DEAUTH_FRAME_LEN);
1900 skb_reserve(skb, local->hw.extra_tx_headroom);
1903 skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1905 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1906 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1907 IEEE80211_SKB_CB(skb)->flags |=
1908 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1910 ieee80211_tx_skb(sdata, skb);
1914 u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
1916 if ((end - pos) < 5)
1919 *pos++ = WLAN_EID_EXTENSION;
1920 *pos++ = 1 + sizeof(cap);
1921 *pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
1922 memcpy(pos, &cap, sizeof(cap));
1927 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
1928 u8 *buffer, size_t buffer_len,
1929 const u8 *ie, size_t ie_len,
1930 enum nl80211_band band,
1932 struct cfg80211_chan_def *chandef,
1933 size_t *offset, u32 flags)
1935 struct ieee80211_local *local = sdata->local;
1936 struct ieee80211_supported_band *sband;
1937 const struct ieee80211_sta_he_cap *he_cap;
1938 const struct ieee80211_sta_eht_cap *eht_cap;
1939 u8 *pos = buffer, *end = buffer + buffer_len;
1941 int supp_rates_len, i;
1947 bool have_80mhz = false;
1951 sband = local->hw.wiphy->bands[band];
1952 if (WARN_ON_ONCE(!sband))
1955 rate_flags = ieee80211_chandef_rate_flags(chandef);
1956 shift = ieee80211_chandef_get_shift(chandef);
1958 /* For direct scan add S1G IE and consider its override bits */
1959 if (band == NL80211_BAND_S1GHZ) {
1960 if (end - pos < 2 + sizeof(struct ieee80211_s1g_cap))
1962 pos = ieee80211_ie_build_s1g_cap(pos, &sband->s1g_cap);
1967 for (i = 0; i < sband->n_bitrates; i++) {
1968 if ((BIT(i) & rate_mask) == 0)
1969 continue; /* skip rate */
1970 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1973 rates[num_rates++] =
1974 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1978 supp_rates_len = min_t(int, num_rates, 8);
1980 if (end - pos < 2 + supp_rates_len)
1982 *pos++ = WLAN_EID_SUPP_RATES;
1983 *pos++ = supp_rates_len;
1984 memcpy(pos, rates, supp_rates_len);
1985 pos += supp_rates_len;
1987 /* insert "request information" if in custom IEs */
1989 static const u8 before_extrates[] = {
1991 WLAN_EID_SUPP_RATES,
1994 noffset = ieee80211_ie_split(ie, ie_len,
1996 ARRAY_SIZE(before_extrates),
1998 if (end - pos < noffset - *offset)
2000 memcpy(pos, ie + *offset, noffset - *offset);
2001 pos += noffset - *offset;
2005 ext_rates_len = num_rates - supp_rates_len;
2006 if (ext_rates_len > 0) {
2007 if (end - pos < 2 + ext_rates_len)
2009 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2010 *pos++ = ext_rates_len;
2011 memcpy(pos, rates + supp_rates_len, ext_rates_len);
2012 pos += ext_rates_len;
2015 if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
2018 *pos++ = WLAN_EID_DS_PARAMS;
2020 *pos++ = ieee80211_frequency_to_channel(
2021 chandef->chan->center_freq);
2024 if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
2027 /* insert custom IEs that go before HT */
2029 static const u8 before_ht[] = {
2031 * no need to list the ones split off already
2032 * (or generated here)
2035 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2037 noffset = ieee80211_ie_split(ie, ie_len,
2038 before_ht, ARRAY_SIZE(before_ht),
2040 if (end - pos < noffset - *offset)
2042 memcpy(pos, ie + *offset, noffset - *offset);
2043 pos += noffset - *offset;
2047 if (sband->ht_cap.ht_supported) {
2048 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
2050 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
2054 /* insert custom IEs that go before VHT */
2056 static const u8 before_vht[] = {
2058 * no need to list the ones split off already
2059 * (or generated here)
2061 WLAN_EID_BSS_COEX_2040,
2062 WLAN_EID_EXT_CAPABILITY,
2064 WLAN_EID_CHANNEL_USAGE,
2065 WLAN_EID_INTERWORKING,
2067 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
2069 noffset = ieee80211_ie_split(ie, ie_len,
2070 before_vht, ARRAY_SIZE(before_vht),
2072 if (end - pos < noffset - *offset)
2074 memcpy(pos, ie + *offset, noffset - *offset);
2075 pos += noffset - *offset;
2079 /* Check if any channel in this sband supports at least 80 MHz */
2080 for (i = 0; i < sband->n_channels; i++) {
2081 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
2082 IEEE80211_CHAN_NO_80MHZ))
2089 if (sband->vht_cap.vht_supported && have_80mhz) {
2090 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
2092 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
2093 sband->vht_cap.cap);
2096 /* insert custom IEs that go before HE */
2098 static const u8 before_he[] = {
2100 * no need to list the ones split off before VHT
2103 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
2105 /* TODO: add 11ah/11aj/11ak elements */
2107 noffset = ieee80211_ie_split(ie, ie_len,
2108 before_he, ARRAY_SIZE(before_he),
2110 if (end - pos < noffset - *offset)
2112 memcpy(pos, ie + *offset, noffset - *offset);
2113 pos += noffset - *offset;
2117 he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
2119 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2120 IEEE80211_CHAN_NO_HE)) {
2121 pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
2126 eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
2129 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2130 IEEE80211_CHAN_NO_HE |
2131 IEEE80211_CHAN_NO_EHT)) {
2132 pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
2133 sdata->vif.type == NL80211_IFTYPE_AP);
2138 if (cfg80211_any_usable_channels(local->hw.wiphy,
2139 BIT(NL80211_BAND_6GHZ),
2140 IEEE80211_CHAN_NO_HE)) {
2141 struct ieee80211_supported_band *sband6;
2143 sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2144 he_cap = ieee80211_get_he_iftype_cap_vif(sband6, &sdata->vif);
2147 enum nl80211_iftype iftype =
2148 ieee80211_vif_type_p2p(&sdata->vif);
2149 __le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
2151 pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
2156 * If adding more here, adjust code in main.c
2157 * that calculates local->scan_ies_len.
2160 return pos - buffer;
2162 WARN_ONCE(1, "not enough space for preq IEs\n");
2164 return pos - buffer;
2167 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
2169 struct ieee80211_scan_ies *ie_desc,
2170 const u8 *ie, size_t ie_len,
2171 u8 bands_used, u32 *rate_masks,
2172 struct cfg80211_chan_def *chandef,
2175 size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
2178 memset(ie_desc, 0, sizeof(*ie_desc));
2180 for (i = 0; i < NUM_NL80211_BANDS; i++) {
2181 if (bands_used & BIT(i)) {
2182 pos += ieee80211_build_preq_ies_band(sdata,
2190 ie_desc->ies[i] = buffer + old_pos;
2191 ie_desc->len[i] = pos - old_pos;
2196 /* add any remaining custom IEs */
2198 if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
2199 "not enough space for preq custom IEs\n"))
2201 memcpy(buffer + pos, ie + custom_ie_offset,
2202 ie_len - custom_ie_offset);
2203 ie_desc->common_ies = buffer + pos;
2204 ie_desc->common_ie_len = ie_len - custom_ie_offset;
2205 pos += ie_len - custom_ie_offset;
2211 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
2212 const u8 *src, const u8 *dst,
2214 struct ieee80211_channel *chan,
2215 const u8 *ssid, size_t ssid_len,
2216 const u8 *ie, size_t ie_len,
2219 struct ieee80211_local *local = sdata->local;
2220 struct cfg80211_chan_def chandef;
2221 struct sk_buff *skb;
2222 struct ieee80211_mgmt *mgmt;
2224 u32 rate_masks[NUM_NL80211_BANDS] = {};
2225 struct ieee80211_scan_ies dummy_ie_desc;
2228 * Do not send DS Channel parameter for directed probe requests
2229 * in order to maximize the chance that we get a response. Some
2230 * badly-behaved APs don't respond when this parameter is included.
2232 chandef.width = sdata->vif.bss_conf.chandef.width;
2233 if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2234 chandef.chan = NULL;
2236 chandef.chan = chan;
2238 skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
2239 local->scan_ies_len + ie_len);
2243 rate_masks[chan->band] = ratemask;
2244 ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
2245 skb_tailroom(skb), &dummy_ie_desc,
2246 ie, ie_len, BIT(chan->band),
2247 rate_masks, &chandef, flags);
2248 skb_put(skb, ies_len);
2251 mgmt = (struct ieee80211_mgmt *) skb->data;
2252 memcpy(mgmt->da, dst, ETH_ALEN);
2253 memcpy(mgmt->bssid, dst, ETH_ALEN);
2256 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2261 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
2262 struct ieee802_11_elems *elems,
2263 enum nl80211_band band, u32 *basic_rates)
2265 struct ieee80211_supported_band *sband;
2267 u32 supp_rates, rate_flags;
2270 sband = sdata->local->hw.wiphy->bands[band];
2271 if (WARN_ON(!sband))
2274 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2275 shift = ieee80211_vif_get_shift(&sdata->vif);
2277 num_rates = sband->n_bitrates;
2279 for (i = 0; i < elems->supp_rates_len +
2280 elems->ext_supp_rates_len; i++) {
2284 if (i < elems->supp_rates_len)
2285 rate = elems->supp_rates[i];
2286 else if (elems->ext_supp_rates)
2287 rate = elems->ext_supp_rates
2288 [i - elems->supp_rates_len];
2289 own_rate = 5 * (rate & 0x7f);
2290 is_basic = !!(rate & 0x80);
2292 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2295 for (j = 0; j < num_rates; j++) {
2297 if ((rate_flags & sband->bitrates[j].flags)
2301 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
2304 if (brate == own_rate) {
2305 supp_rates |= BIT(j);
2306 if (basic_rates && is_basic)
2307 *basic_rates |= BIT(j);
2314 void ieee80211_stop_device(struct ieee80211_local *local)
2316 ieee80211_led_radio(local, false);
2317 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
2319 cancel_work_sync(&local->reconfig_filter);
2321 flush_workqueue(local->workqueue);
2325 static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
2328 /* It's possible that we don't handle the scan completion in
2329 * time during suspend, so if it's still marked as completed
2330 * here, queue the work and flush it to clean things up.
2331 * Instead of calling the worker function directly here, we
2332 * really queue it to avoid potential races with other flows
2333 * scheduling the same work.
2335 if (test_bit(SCAN_COMPLETED, &local->scanning)) {
2336 /* If coming from reconfiguration failure, abort the scan so
2337 * we don't attempt to continue a partial HW scan - which is
2338 * possible otherwise if (e.g.) the 2.4 GHz portion was the
2339 * completed scan, and a 5 GHz portion is still pending.
2342 set_bit(SCAN_ABORTED, &local->scanning);
2343 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);
2344 wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work);
2348 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2350 struct ieee80211_sub_if_data *sdata;
2351 struct ieee80211_chanctx *ctx;
2354 * We get here if during resume the device can't be restarted properly.
2355 * We might also get here if this happens during HW reset, which is a
2356 * slightly different situation and we need to drop all connections in
2359 * Ask cfg80211 to turn off all interfaces, this will result in more
2360 * warnings but at least we'll then get into a clean stopped state.
2363 local->resuming = false;
2364 local->suspended = false;
2365 local->in_reconfig = false;
2366 local->reconfig_failure = true;
2368 ieee80211_flush_completed_scan(local, true);
2370 /* scheduled scan clearly can't be running any more, but tell
2371 * cfg80211 and clear local state
2373 ieee80211_sched_scan_end(local);
2375 list_for_each_entry(sdata, &local->interfaces, list)
2376 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2378 /* Mark channel contexts as not being in the driver any more to avoid
2379 * removing them from the driver during the shutdown process...
2381 mutex_lock(&local->chanctx_mtx);
2382 list_for_each_entry(ctx, &local->chanctx_list, list)
2383 ctx->driver_present = false;
2384 mutex_unlock(&local->chanctx_mtx);
2387 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2388 struct ieee80211_sub_if_data *sdata,
2389 struct ieee80211_link_data *link)
2391 struct ieee80211_chanctx_conf *conf;
2392 struct ieee80211_chanctx *ctx;
2394 if (!local->use_chanctx)
2397 mutex_lock(&local->chanctx_mtx);
2398 conf = rcu_dereference_protected(link->conf->chanctx_conf,
2399 lockdep_is_held(&local->chanctx_mtx));
2401 ctx = container_of(conf, struct ieee80211_chanctx, conf);
2402 drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
2404 mutex_unlock(&local->chanctx_mtx);
2407 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
2409 struct ieee80211_local *local = sdata->local;
2410 struct sta_info *sta;
2413 mutex_lock(&local->sta_mtx);
2414 list_for_each_entry(sta, &local->sta_list, list) {
2415 enum ieee80211_sta_state state;
2417 if (!sta->uploaded || sta->sdata != sdata)
2420 for (state = IEEE80211_STA_NOTEXIST;
2421 state < sta->sta_state; state++)
2422 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2425 mutex_unlock(&local->sta_mtx);
2428 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2430 struct cfg80211_nan_func *func, **funcs;
2433 res = drv_start_nan(sdata->local, sdata,
2434 &sdata->u.nan.conf);
2438 funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
2444 /* Add all the functions:
2445 * This is a little bit ugly. We need to call a potentially sleeping
2446 * callback for each NAN function, so we can't hold the spinlock.
2448 spin_lock_bh(&sdata->u.nan.func_lock);
2450 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2453 spin_unlock_bh(&sdata->u.nan.func_lock);
2455 for (i = 0; funcs[i]; i++) {
2456 res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2458 ieee80211_nan_func_terminated(&sdata->vif,
2459 funcs[i]->instance_id,
2460 NL80211_NAN_FUNC_TERM_REASON_ERROR,
2469 static void ieee80211_reconfig_ap_links(struct ieee80211_local *local,
2470 struct ieee80211_sub_if_data *sdata,
2475 for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
2476 struct ieee80211_link_data *link;
2478 if (!(sdata->vif.active_links & BIT(link_id)))
2481 link = sdata_dereference(sdata->link[link_id], sdata);
2485 if (rcu_access_pointer(link->u.ap.beacon))
2486 drv_start_ap(local, sdata, link->conf);
2488 if (!link->conf->enable_beacon)
2491 changed |= BSS_CHANGED_BEACON |
2492 BSS_CHANGED_BEACON_ENABLED;
2494 ieee80211_link_info_change_notify(sdata, link, changed);
2498 int ieee80211_reconfig(struct ieee80211_local *local)
2500 struct ieee80211_hw *hw = &local->hw;
2501 struct ieee80211_sub_if_data *sdata;
2502 struct ieee80211_chanctx *ctx;
2503 struct sta_info *sta;
2505 bool reconfig_due_to_wowlan = false;
2506 struct ieee80211_sub_if_data *sched_scan_sdata;
2507 struct cfg80211_sched_scan_request *sched_scan_req;
2508 bool sched_scan_stopped = false;
2509 bool suspended = local->suspended;
2510 bool in_reconfig = false;
2512 /* nothing to do if HW shouldn't run */
2513 if (!local->open_count)
2518 local->resuming = true;
2520 if (local->wowlan) {
2522 * In the wowlan case, both mac80211 and the device
2523 * are functional when the resume op is called, so
2524 * clear local->suspended so the device could operate
2525 * normally (e.g. pass rx frames).
2527 local->suspended = false;
2528 res = drv_resume(local);
2529 local->wowlan = false;
2531 local->resuming = false;
2538 * res is 1, which means the driver requested
2539 * to go through a regular reset on wakeup.
2540 * restore local->suspended in this case.
2542 reconfig_due_to_wowlan = true;
2543 local->suspended = true;
2548 * In case of hw_restart during suspend (without wowlan),
2549 * cancel restart work, as we are reconfiguring the device
2551 * Note that restart_work is scheduled on a frozen workqueue,
2552 * so we can't deadlock in this case.
2554 if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
2555 cancel_work_sync(&local->restart_work);
2557 local->started = false;
2560 * Upon resume hardware can sometimes be goofy due to
2561 * various platform / driver / bus issues, so restarting
2562 * the device may at times not work immediately. Propagate
2565 res = drv_start(local);
2568 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2570 WARN(1, "Hardware became unavailable during restart.\n");
2571 ieee80211_handle_reconfig_failure(local);
2575 /* setup fragmentation threshold */
2576 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
2578 /* setup RTS threshold */
2579 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
2581 /* reset coverage class */
2582 drv_set_coverage_class(local, hw->wiphy->coverage_class);
2584 ieee80211_led_radio(local, true);
2585 ieee80211_mod_tpt_led_trig(local,
2586 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2588 /* add interfaces */
2589 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
2591 /* in HW restart it exists already */
2592 WARN_ON(local->resuming);
2593 res = drv_add_interface(local, sdata);
2595 RCU_INIT_POINTER(local->monitor_sdata, NULL);
2601 list_for_each_entry(sdata, &local->interfaces, list) {
2602 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2603 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2604 ieee80211_sdata_running(sdata)) {
2605 res = drv_add_interface(local, sdata);
2611 /* If adding any of the interfaces failed above, roll back and
2615 list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2617 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2618 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2619 ieee80211_sdata_running(sdata))
2620 drv_remove_interface(local, sdata);
2621 ieee80211_handle_reconfig_failure(local);
2625 /* add channel contexts */
2626 if (local->use_chanctx) {
2627 mutex_lock(&local->chanctx_mtx);
2628 list_for_each_entry(ctx, &local->chanctx_list, list)
2629 if (ctx->replace_state !=
2630 IEEE80211_CHANCTX_REPLACES_OTHER)
2631 WARN_ON(drv_add_chanctx(local, ctx));
2632 mutex_unlock(&local->chanctx_mtx);
2634 sdata = wiphy_dereference(local->hw.wiphy,
2635 local->monitor_sdata);
2636 if (sdata && ieee80211_sdata_running(sdata))
2637 ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
2640 /* reconfigure hardware */
2641 ieee80211_hw_config(local, ~0);
2643 ieee80211_configure_filter(local);
2645 /* Finally also reconfigure all the BSS information */
2646 list_for_each_entry(sdata, &local->interfaces, list) {
2647 /* common change flags for all interface types - link only */
2648 u64 changed = BSS_CHANGED_ERP_CTS_PROT |
2649 BSS_CHANGED_ERP_PREAMBLE |
2650 BSS_CHANGED_ERP_SLOT |
2652 BSS_CHANGED_BASIC_RATES |
2653 BSS_CHANGED_BEACON_INT |
2657 BSS_CHANGED_TXPOWER |
2658 BSS_CHANGED_MCAST_RATE;
2659 struct ieee80211_link_data *link = NULL;
2660 unsigned int link_id;
2661 u32 active_links = 0;
2663 if (!ieee80211_sdata_running(sdata))
2667 if (ieee80211_vif_is_mld(&sdata->vif)) {
2668 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS] = {
2669 [0] = &sdata->vif.bss_conf,
2672 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2673 /* start with a single active link */
2674 active_links = sdata->vif.active_links;
2675 link_id = ffs(active_links) - 1;
2676 sdata->vif.active_links = BIT(link_id);
2679 drv_change_vif_links(local, sdata, 0,
2680 sdata->vif.active_links,
2685 link_id < ARRAY_SIZE(sdata->vif.link_conf);
2687 if (ieee80211_vif_is_mld(&sdata->vif) &&
2688 !(sdata->vif.active_links & BIT(link_id)))
2691 link = sdata_dereference(sdata->link[link_id], sdata);
2695 ieee80211_assign_chanctx(local, sdata, link);
2698 switch (sdata->vif.type) {
2699 case NL80211_IFTYPE_AP_VLAN:
2700 case NL80211_IFTYPE_MONITOR:
2702 case NL80211_IFTYPE_ADHOC:
2703 if (sdata->vif.cfg.ibss_joined)
2704 WARN_ON(drv_join_ibss(local, sdata));
2707 ieee80211_reconfig_stations(sdata);
2709 case NL80211_IFTYPE_AP: /* AP stations are handled later */
2710 for (i = 0; i < IEEE80211_NUM_ACS; i++)
2711 drv_conf_tx(local, &sdata->deflink, i,
2712 &sdata->deflink.tx_conf[i]);
2716 if (sdata->vif.bss_conf.mu_mimo_owner)
2717 changed |= BSS_CHANGED_MU_GROUPS;
2719 if (!ieee80211_vif_is_mld(&sdata->vif))
2720 changed |= BSS_CHANGED_IDLE;
2722 switch (sdata->vif.type) {
2723 case NL80211_IFTYPE_STATION:
2724 if (!ieee80211_vif_is_mld(&sdata->vif)) {
2725 changed |= BSS_CHANGED_ASSOC |
2726 BSS_CHANGED_ARP_FILTER |
2729 /* Re-send beacon info report to the driver */
2730 if (sdata->deflink.u.mgd.have_beacon)
2731 changed |= BSS_CHANGED_BEACON_INFO;
2733 if (sdata->vif.bss_conf.max_idle_period ||
2734 sdata->vif.bss_conf.protected_keep_alive)
2735 changed |= BSS_CHANGED_KEEP_ALIVE;
2737 if (sdata->vif.bss_conf.eht_puncturing)
2738 changed |= BSS_CHANGED_EHT_PUNCTURING;
2740 ieee80211_bss_info_change_notify(sdata,
2742 } else if (!WARN_ON(!link)) {
2743 ieee80211_link_info_change_notify(sdata, link,
2745 changed = BSS_CHANGED_ASSOC |
2748 BSS_CHANGED_ARP_FILTER;
2749 ieee80211_vif_cfg_change_notify(sdata, changed);
2752 case NL80211_IFTYPE_OCB:
2753 changed |= BSS_CHANGED_OCB;
2754 ieee80211_bss_info_change_notify(sdata, changed);
2756 case NL80211_IFTYPE_ADHOC:
2757 changed |= BSS_CHANGED_IBSS;
2759 case NL80211_IFTYPE_AP:
2760 changed |= BSS_CHANGED_P2P_PS;
2762 if (ieee80211_vif_is_mld(&sdata->vif))
2763 ieee80211_vif_cfg_change_notify(sdata,
2766 changed |= BSS_CHANGED_SSID;
2768 if (sdata->vif.bss_conf.ftm_responder == 1 &&
2769 wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2770 NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2771 changed |= BSS_CHANGED_FTM_RESPONDER;
2773 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2774 changed |= BSS_CHANGED_AP_PROBE_RESP;
2776 if (ieee80211_vif_is_mld(&sdata->vif)) {
2777 ieee80211_reconfig_ap_links(local,
2783 if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2784 drv_start_ap(local, sdata,
2785 sdata->deflink.conf);
2788 case NL80211_IFTYPE_MESH_POINT:
2789 if (sdata->vif.bss_conf.enable_beacon) {
2790 changed |= BSS_CHANGED_BEACON |
2791 BSS_CHANGED_BEACON_ENABLED;
2792 ieee80211_bss_info_change_notify(sdata, changed);
2795 case NL80211_IFTYPE_NAN:
2796 res = ieee80211_reconfig_nan(sdata);
2798 sdata_unlock(sdata);
2799 ieee80211_handle_reconfig_failure(local);
2803 case NL80211_IFTYPE_AP_VLAN:
2804 case NL80211_IFTYPE_MONITOR:
2805 case NL80211_IFTYPE_P2P_DEVICE:
2808 case NL80211_IFTYPE_UNSPECIFIED:
2809 case NUM_NL80211_IFTYPES:
2810 case NL80211_IFTYPE_P2P_CLIENT:
2811 case NL80211_IFTYPE_P2P_GO:
2812 case NL80211_IFTYPE_WDS:
2816 sdata_unlock(sdata);
2819 ieee80211_set_active_links(&sdata->vif, active_links);
2822 ieee80211_recalc_ps(local);
2825 * The sta might be in psm against the ap (e.g. because
2826 * this was the state before a hw restart), so we
2827 * explicitly send a null packet in order to make sure
2828 * it'll sync against the ap (and get out of psm).
2830 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2831 list_for_each_entry(sdata, &local->interfaces, list) {
2832 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2834 if (!sdata->u.mgd.associated)
2837 ieee80211_send_nullfunc(local, sdata, false);
2841 /* APs are now beaconing, add back stations */
2842 list_for_each_entry(sdata, &local->interfaces, list) {
2843 if (!ieee80211_sdata_running(sdata))
2847 switch (sdata->vif.type) {
2848 case NL80211_IFTYPE_AP_VLAN:
2849 case NL80211_IFTYPE_AP:
2850 ieee80211_reconfig_stations(sdata);
2855 sdata_unlock(sdata);
2859 list_for_each_entry(sdata, &local->interfaces, list)
2860 ieee80211_reenable_keys(sdata);
2862 /* Reconfigure sched scan if it was interrupted by FW restart */
2863 mutex_lock(&local->mtx);
2864 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2865 lockdep_is_held(&local->mtx));
2866 sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2867 lockdep_is_held(&local->mtx));
2868 if (sched_scan_sdata && sched_scan_req)
2870 * Sched scan stopped, but we don't want to report it. Instead,
2871 * we're trying to reschedule. However, if more than one scan
2872 * plan was set, we cannot reschedule since we don't know which
2873 * scan plan was currently running (and some scan plans may have
2874 * already finished).
2876 if (sched_scan_req->n_scan_plans > 1 ||
2877 __ieee80211_request_sched_scan_start(sched_scan_sdata,
2879 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2880 RCU_INIT_POINTER(local->sched_scan_req, NULL);
2881 sched_scan_stopped = true;
2883 mutex_unlock(&local->mtx);
2885 if (sched_scan_stopped)
2886 cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2890 if (local->monitors == local->open_count && local->monitors > 0)
2891 ieee80211_add_virtual_monitor(local);
2894 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2895 * sessions can be established after a resume.
2897 * Also tear down aggregation sessions since reconfiguring
2898 * them in a hardware restart scenario is not easily done
2899 * right now, and the hardware will have lost information
2900 * about the sessions, but we and the AP still think they
2901 * are active. This is really a workaround though.
2903 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2904 mutex_lock(&local->sta_mtx);
2906 list_for_each_entry(sta, &local->sta_list, list) {
2907 if (!local->resuming)
2908 ieee80211_sta_tear_down_BA_sessions(
2909 sta, AGG_STOP_LOCAL_REQUEST);
2910 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2913 mutex_unlock(&local->sta_mtx);
2917 * If this is for hw restart things are still running.
2918 * We may want to change that later, however.
2920 if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2921 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2923 if (local->in_reconfig) {
2924 in_reconfig = local->in_reconfig;
2925 local->in_reconfig = false;
2928 /* Restart deferred ROCs */
2929 mutex_lock(&local->mtx);
2930 ieee80211_start_next_roc(local);
2931 mutex_unlock(&local->mtx);
2933 /* Requeue all works */
2934 list_for_each_entry(sdata, &local->interfaces, list)
2935 wiphy_work_queue(local->hw.wiphy, &sdata->work);
2938 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2939 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2943 list_for_each_entry(sdata, &local->interfaces, list) {
2944 if (!ieee80211_sdata_running(sdata))
2946 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2947 ieee80211_sta_restart(sdata);
2955 /* first set suspended false, then resuming */
2956 local->suspended = false;
2958 local->resuming = false;
2960 ieee80211_flush_completed_scan(local, false);
2962 if (local->open_count && !reconfig_due_to_wowlan)
2963 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2965 list_for_each_entry(sdata, &local->interfaces, list) {
2966 if (!ieee80211_sdata_running(sdata))
2968 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2969 ieee80211_sta_restart(sdata);
2972 mod_timer(&local->sta_cleanup, jiffies + 1);
2980 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2982 struct ieee80211_sub_if_data *sdata;
2983 struct ieee80211_local *local;
2984 struct ieee80211_key *key;
2989 sdata = vif_to_sdata(vif);
2990 local = sdata->local;
2992 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2996 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2997 !local->in_reconfig))
3000 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
3003 sdata->flags |= flag;
3005 mutex_lock(&local->key_mtx);
3006 list_for_each_entry(key, &sdata->key_list, list)
3007 key->flags |= KEY_FLAG_TAINTED;
3008 mutex_unlock(&local->key_mtx);
3011 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
3013 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
3015 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
3017 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
3019 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
3021 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
3023 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
3024 struct ieee80211_link_data *link)
3026 struct ieee80211_local *local = sdata->local;
3027 struct ieee80211_chanctx_conf *chanctx_conf;
3028 struct ieee80211_chanctx *chanctx;
3030 mutex_lock(&local->chanctx_mtx);
3032 chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
3033 lockdep_is_held(&local->chanctx_mtx));
3036 * This function can be called from a work, thus it may be possible
3037 * that the chanctx_conf is removed (due to a disconnection, for
3039 * So nothing should be done in such case.
3044 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
3045 ieee80211_recalc_smps_chanctx(local, chanctx);
3047 mutex_unlock(&local->chanctx_mtx);
3050 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
3053 struct ieee80211_local *local = sdata->local;
3054 struct ieee80211_chanctx_conf *chanctx_conf;
3055 struct ieee80211_chanctx *chanctx;
3058 mutex_lock(&local->chanctx_mtx);
3060 for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
3061 struct ieee80211_bss_conf *bss_conf;
3063 if (link_id >= 0 && link_id != i)
3067 bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
3073 chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
3074 lockdep_is_held(&local->chanctx_mtx));
3076 * Since we hold the chanctx_mtx (checked above)
3077 * we can take the chanctx_conf pointer out of the
3078 * RCU critical section, it cannot go away without
3079 * the mutex. Just the way we reached it could - in
3080 * theory - go away, but we don't really care and
3081 * it really shouldn't happen anyway.
3088 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
3090 ieee80211_recalc_chanctx_min_def(local, chanctx, NULL);
3093 mutex_unlock(&local->chanctx_mtx);
3096 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
3098 size_t pos = offset;
3100 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
3101 pos += 2 + ies[pos + 1];
3106 u8 *ieee80211_ie_build_s1g_cap(u8 *pos, struct ieee80211_sta_s1g_cap *s1g_cap)
3108 *pos++ = WLAN_EID_S1G_CAPABILITIES;
3109 *pos++ = sizeof(struct ieee80211_s1g_cap);
3110 memset(pos, 0, sizeof(struct ieee80211_s1g_cap));
3112 memcpy(pos, &s1g_cap->cap, sizeof(s1g_cap->cap));
3113 pos += sizeof(s1g_cap->cap);
3115 memcpy(pos, &s1g_cap->nss_mcs, sizeof(s1g_cap->nss_mcs));
3116 pos += sizeof(s1g_cap->nss_mcs);
3121 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3126 *pos++ = WLAN_EID_HT_CAPABILITY;
3127 *pos++ = sizeof(struct ieee80211_ht_cap);
3128 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
3130 /* capability flags */
3131 tmp = cpu_to_le16(cap);
3132 memcpy(pos, &tmp, sizeof(u16));
3135 /* AMPDU parameters */
3136 *pos++ = ht_cap->ampdu_factor |
3137 (ht_cap->ampdu_density <<
3138 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
3141 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
3142 pos += sizeof(ht_cap->mcs);
3144 /* extended capabilities */
3145 pos += sizeof(__le16);
3147 /* BF capabilities */
3148 pos += sizeof(__le32);
3150 /* antenna selection */
3156 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3161 *pos++ = WLAN_EID_VHT_CAPABILITY;
3162 *pos++ = sizeof(struct ieee80211_vht_cap);
3163 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
3165 /* capability flags */
3166 tmp = cpu_to_le32(cap);
3167 memcpy(pos, &tmp, sizeof(u32));
3171 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
3172 pos += sizeof(vht_cap->vht_mcs);
3177 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
3179 const struct ieee80211_sta_he_cap *he_cap;
3180 struct ieee80211_supported_band *sband;
3183 sband = ieee80211_get_sband(sdata);
3187 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3191 n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
3193 sizeof(he_cap->he_cap_elem) + n +
3194 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3195 he_cap->he_cap_elem.phy_cap_info);
3198 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
3199 const struct ieee80211_sta_he_cap *he_cap,
3202 struct ieee80211_he_cap_elem elem;
3207 /* Make sure we have place for the IE */
3209 * TODO: the 1 added is because this temporarily is under the EXTENSION
3210 * IE. Get rid of it when it moves.
3215 /* modify on stack first to calculate 'n' and 'ie_len' correctly */
3216 elem = he_cap->he_cap_elem;
3218 if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
3219 elem.phy_cap_info[0] &=
3220 ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3221 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
3223 if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
3224 elem.phy_cap_info[0] &=
3225 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3227 if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
3228 elem.phy_cap_info[0] &=
3229 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3231 n = ieee80211_he_mcs_nss_size(&elem);
3233 sizeof(he_cap->he_cap_elem) + n +
3234 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3235 he_cap->he_cap_elem.phy_cap_info);
3237 if ((end - pos) < ie_len)
3240 *pos++ = WLAN_EID_EXTENSION;
3241 pos++; /* We'll set the size later below */
3242 *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
3245 memcpy(pos, &elem, sizeof(elem));
3246 pos += sizeof(elem);
3248 memcpy(pos, &he_cap->he_mcs_nss_supp, n);
3251 /* Check if PPE Threshold should be present */
3252 if ((he_cap->he_cap_elem.phy_cap_info[6] &
3253 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
3257 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
3258 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
3260 n = hweight8(he_cap->ppe_thres[0] &
3261 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
3262 n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
3263 IEEE80211_PPE_THRES_NSS_POS));
3266 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
3269 n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
3270 n = DIV_ROUND_UP(n, 8);
3272 /* Copy PPE Thresholds */
3273 memcpy(pos, &he_cap->ppe_thres, n);
3277 orig_pos[1] = (pos - orig_pos) - 2;
3281 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
3282 enum ieee80211_smps_mode smps_mode,
3283 struct sk_buff *skb)
3285 struct ieee80211_supported_band *sband;
3286 const struct ieee80211_sband_iftype_data *iftd;
3287 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3291 if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
3292 BIT(NL80211_BAND_6GHZ),
3293 IEEE80211_CHAN_NO_HE))
3296 sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3298 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
3302 /* Check for device HE 6 GHz capability before adding element */
3303 if (!iftd->he_6ghz_capa.capa)
3306 cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
3307 cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
3309 switch (smps_mode) {
3310 case IEEE80211_SMPS_AUTOMATIC:
3311 case IEEE80211_SMPS_NUM_MODES:
3314 case IEEE80211_SMPS_OFF:
3315 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
3316 IEEE80211_HE_6GHZ_CAP_SM_PS);
3318 case IEEE80211_SMPS_STATIC:
3319 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
3320 IEEE80211_HE_6GHZ_CAP_SM_PS);
3322 case IEEE80211_SMPS_DYNAMIC:
3323 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
3324 IEEE80211_HE_6GHZ_CAP_SM_PS);
3328 pos = skb_put(skb, 2 + 1 + sizeof(cap));
3329 ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
3330 pos + 2 + 1 + sizeof(cap));
3333 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3334 const struct cfg80211_chan_def *chandef,
3335 u16 prot_mode, bool rifs_mode)
3337 struct ieee80211_ht_operation *ht_oper;
3338 /* Build HT Information */
3339 *pos++ = WLAN_EID_HT_OPERATION;
3340 *pos++ = sizeof(struct ieee80211_ht_operation);
3341 ht_oper = (struct ieee80211_ht_operation *)pos;
3342 ht_oper->primary_chan = ieee80211_frequency_to_channel(
3343 chandef->chan->center_freq);
3344 switch (chandef->width) {
3345 case NL80211_CHAN_WIDTH_160:
3346 case NL80211_CHAN_WIDTH_80P80:
3347 case NL80211_CHAN_WIDTH_80:
3348 case NL80211_CHAN_WIDTH_40:
3349 if (chandef->center_freq1 > chandef->chan->center_freq)
3350 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3352 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3354 case NL80211_CHAN_WIDTH_320:
3355 /* HT information element should not be included on 6GHz */
3359 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
3362 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
3363 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
3364 chandef->width != NL80211_CHAN_WIDTH_20)
3365 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
3368 ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
3370 ht_oper->operation_mode = cpu_to_le16(prot_mode);
3371 ht_oper->stbc_param = 0x0000;
3373 /* It seems that Basic MCS set and Supported MCS set
3374 are identical for the first 10 bytes */
3375 memset(&ht_oper->basic_set, 0, 16);
3376 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
3378 return pos + sizeof(struct ieee80211_ht_operation);
3381 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
3382 const struct cfg80211_chan_def *chandef)
3384 *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
3385 *pos++ = 3; /* IE length */
3386 /* New channel width */
3387 switch (chandef->width) {
3388 case NL80211_CHAN_WIDTH_80:
3389 *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
3391 case NL80211_CHAN_WIDTH_160:
3392 *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
3394 case NL80211_CHAN_WIDTH_80P80:
3395 *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
3397 case NL80211_CHAN_WIDTH_320:
3398 /* The behavior is not defined for 320 MHz channels */
3402 *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
3405 /* new center frequency segment 0 */
3406 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
3407 /* new center frequency segment 1 */
3408 if (chandef->center_freq2)
3409 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
3414 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3415 const struct cfg80211_chan_def *chandef)
3417 struct ieee80211_vht_operation *vht_oper;
3419 *pos++ = WLAN_EID_VHT_OPERATION;
3420 *pos++ = sizeof(struct ieee80211_vht_operation);
3421 vht_oper = (struct ieee80211_vht_operation *)pos;
3422 vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
3423 chandef->center_freq1);
3424 if (chandef->center_freq2)
3425 vht_oper->center_freq_seg1_idx =
3426 ieee80211_frequency_to_channel(chandef->center_freq2);
3428 vht_oper->center_freq_seg1_idx = 0x00;
3430 switch (chandef->width) {
3431 case NL80211_CHAN_WIDTH_160:
3433 * Convert 160 MHz channel width to new style as interop
3436 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3437 vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
3438 if (chandef->chan->center_freq < chandef->center_freq1)
3439 vht_oper->center_freq_seg0_idx -= 8;
3441 vht_oper->center_freq_seg0_idx += 8;
3443 case NL80211_CHAN_WIDTH_80P80:
3445 * Convert 80+80 MHz channel width to new style as interop
3448 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3450 case NL80211_CHAN_WIDTH_80:
3451 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3453 case NL80211_CHAN_WIDTH_320:
3454 /* VHT information element should not be included on 6GHz */
3458 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
3462 /* don't require special VHT peer rates */
3463 vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
3465 return pos + sizeof(struct ieee80211_vht_operation);
3468 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
3470 struct ieee80211_he_operation *he_oper;
3471 struct ieee80211_he_6ghz_oper *he_6ghz_op;
3473 u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
3475 if (chandef->chan->band == NL80211_BAND_6GHZ)
3476 ie_len += sizeof(struct ieee80211_he_6ghz_oper);
3478 *pos++ = WLAN_EID_EXTENSION;
3480 *pos++ = WLAN_EID_EXT_HE_OPERATION;
3483 he_oper_params |= u32_encode_bits(1023, /* disabled */
3484 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3485 he_oper_params |= u32_encode_bits(1,
3486 IEEE80211_HE_OPERATION_ER_SU_DISABLE);
3487 he_oper_params |= u32_encode_bits(1,
3488 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3489 if (chandef->chan->band == NL80211_BAND_6GHZ)
3490 he_oper_params |= u32_encode_bits(1,
3491 IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
3493 he_oper = (struct ieee80211_he_operation *)pos;
3494 he_oper->he_oper_params = cpu_to_le32(he_oper_params);
3496 /* don't require special HE peer rates */
3497 he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
3498 pos += sizeof(struct ieee80211_he_operation);
3500 if (chandef->chan->band != NL80211_BAND_6GHZ)
3503 /* TODO add VHT operational */
3504 he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
3505 he_6ghz_op->minrate = 6; /* 6 Mbps */
3506 he_6ghz_op->primary =
3507 ieee80211_frequency_to_channel(chandef->chan->center_freq);
3509 ieee80211_frequency_to_channel(chandef->center_freq1);
3510 if (chandef->center_freq2)
3512 ieee80211_frequency_to_channel(chandef->center_freq2);
3514 he_6ghz_op->ccfs1 = 0;
3516 switch (chandef->width) {
3517 case NL80211_CHAN_WIDTH_320:
3519 * TODO: mesh operation is not defined over 6GHz 320 MHz
3524 case NL80211_CHAN_WIDTH_160:
3525 /* Convert 160 MHz channel width to new style as interop
3528 he_6ghz_op->control =
3529 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3530 he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
3531 if (chandef->chan->center_freq < chandef->center_freq1)
3532 he_6ghz_op->ccfs0 -= 8;
3534 he_6ghz_op->ccfs0 += 8;
3536 case NL80211_CHAN_WIDTH_80P80:
3537 he_6ghz_op->control =
3538 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3540 case NL80211_CHAN_WIDTH_80:
3541 he_6ghz_op->control =
3542 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
3544 case NL80211_CHAN_WIDTH_40:
3545 he_6ghz_op->control =
3546 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3549 he_6ghz_op->control =
3550 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3554 pos += sizeof(struct ieee80211_he_6ghz_oper);
3560 u8 *ieee80211_ie_build_eht_oper(u8 *pos, struct cfg80211_chan_def *chandef,
3561 const struct ieee80211_sta_eht_cap *eht_cap)
3564 const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
3565 &eht_cap->eht_mcs_nss_supp.only_20mhz;
3566 struct ieee80211_eht_operation *eht_oper;
3567 struct ieee80211_eht_operation_info *eht_oper_info;
3568 u8 eht_oper_len = offsetof(struct ieee80211_eht_operation, optional);
3569 u8 eht_oper_info_len =
3570 offsetof(struct ieee80211_eht_operation_info, optional);
3573 *pos++ = WLAN_EID_EXTENSION;
3574 *pos++ = 1 + eht_oper_len + eht_oper_info_len;
3575 *pos++ = WLAN_EID_EXT_EHT_OPERATION;
3577 eht_oper = (struct ieee80211_eht_operation *)pos;
3579 memcpy(&eht_oper->basic_mcs_nss, eht_mcs_nss, sizeof(*eht_mcs_nss));
3580 eht_oper->params |= IEEE80211_EHT_OPER_INFO_PRESENT;
3581 pos += eht_oper_len;
3584 (struct ieee80211_eht_operation_info *)eht_oper->optional;
3586 eht_oper_info->ccfs0 =
3587 ieee80211_frequency_to_channel(chandef->center_freq1);
3588 if (chandef->center_freq2)
3589 eht_oper_info->ccfs1 =
3590 ieee80211_frequency_to_channel(chandef->center_freq2);
3592 eht_oper_info->ccfs1 = 0;
3594 switch (chandef->width) {
3595 case NL80211_CHAN_WIDTH_320:
3596 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ;
3597 eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3598 if (chandef->chan->center_freq < chandef->center_freq1)
3599 eht_oper_info->ccfs0 -= 16;
3601 eht_oper_info->ccfs0 += 16;
3603 case NL80211_CHAN_WIDTH_160:
3604 eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3605 if (chandef->chan->center_freq < chandef->center_freq1)
3606 eht_oper_info->ccfs0 -= 8;
3608 eht_oper_info->ccfs0 += 8;
3610 case NL80211_CHAN_WIDTH_80P80:
3611 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ;
3613 case NL80211_CHAN_WIDTH_80:
3614 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ;
3616 case NL80211_CHAN_WIDTH_40:
3617 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ;
3620 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ;
3623 eht_oper_info->control = chan_width;
3624 pos += eht_oper_info_len;
3626 /* TODO: eht_oper_info->optional */
3631 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3632 struct cfg80211_chan_def *chandef)
3634 enum nl80211_channel_type channel_type;
3639 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3640 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3641 channel_type = NL80211_CHAN_HT20;
3643 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3644 channel_type = NL80211_CHAN_HT40PLUS;
3646 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3647 channel_type = NL80211_CHAN_HT40MINUS;
3653 cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3657 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3658 const struct ieee80211_vht_operation *oper,
3659 const struct ieee80211_ht_operation *htop,
3660 struct cfg80211_chan_def *chandef)
3662 struct cfg80211_chan_def new = *chandef;
3664 int ccfs0, ccfs1, ccfs2;
3667 bool support_80_80 = false;
3668 bool support_160 = false;
3669 u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3670 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3671 u8 supp_chwidth = u32_get_bits(vht_cap_info,
3672 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3677 vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3678 support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3679 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3680 support_80_80 = ((vht_cap &
3681 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3682 (vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3683 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3684 ((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3685 IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3686 ccfs0 = oper->center_freq_seg0_idx;
3687 ccfs1 = oper->center_freq_seg1_idx;
3688 ccfs2 = (le16_to_cpu(htop->operation_mode) &
3689 IEEE80211_HT_OP_MODE_CCFS2_MASK)
3690 >> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3694 /* if not supported, parse as though we didn't understand it */
3695 if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3696 ext_nss_bw_supp = 0;
3699 * Cf. IEEE 802.11 Table 9-250
3701 * We really just consider that because it's inefficient to connect
3702 * at a higher bandwidth than we'll actually be able to use.
3704 switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3708 support_160 = false;
3709 support_80_80 = false;
3712 support_80_80 = false;
3735 cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3736 cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3738 switch (oper->chan_width) {
3739 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3740 /* just use HT information directly */
3742 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3743 new.width = NL80211_CHAN_WIDTH_80;
3744 new.center_freq1 = cf0;
3745 /* If needed, adjust based on the newer interop workaround. */
3749 diff = abs(ccf1 - ccf0);
3750 if ((diff == 8) && support_160) {
3751 new.width = NL80211_CHAN_WIDTH_160;
3752 new.center_freq1 = cf1;
3753 } else if ((diff > 8) && support_80_80) {
3754 new.width = NL80211_CHAN_WIDTH_80P80;
3755 new.center_freq2 = cf1;
3759 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3760 /* deprecated encoding */
3761 new.width = NL80211_CHAN_WIDTH_160;
3762 new.center_freq1 = cf0;
3764 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3765 /* deprecated encoding */
3766 new.width = NL80211_CHAN_WIDTH_80P80;
3767 new.center_freq1 = cf0;
3768 new.center_freq2 = cf1;
3774 if (!cfg80211_chandef_valid(&new))
3781 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
3782 bool support_160, bool support_320,
3783 struct cfg80211_chan_def *chandef)
3785 struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
3787 chandef->center_freq1 =
3788 ieee80211_channel_to_frequency(info->ccfs0,
3789 chandef->chan->band);
3791 switch (u8_get_bits(info->control,
3792 IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3793 case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3794 chandef->width = NL80211_CHAN_WIDTH_20;
3796 case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3797 chandef->width = NL80211_CHAN_WIDTH_40;
3799 case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3800 chandef->width = NL80211_CHAN_WIDTH_80;
3802 case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3804 chandef->width = NL80211_CHAN_WIDTH_160;
3805 chandef->center_freq1 =
3806 ieee80211_channel_to_frequency(info->ccfs1,
3807 chandef->chan->band);
3809 chandef->width = NL80211_CHAN_WIDTH_80;
3812 case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3814 chandef->width = NL80211_CHAN_WIDTH_320;
3815 chandef->center_freq1 =
3816 ieee80211_channel_to_frequency(info->ccfs1,
3817 chandef->chan->band);
3818 } else if (support_160) {
3819 chandef->width = NL80211_CHAN_WIDTH_160;
3821 chandef->width = NL80211_CHAN_WIDTH_80;
3823 if (chandef->center_freq1 > chandef->chan->center_freq)
3824 chandef->center_freq1 -= 40;
3826 chandef->center_freq1 += 40;
3832 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
3833 const struct ieee80211_he_operation *he_oper,
3834 const struct ieee80211_eht_operation *eht_oper,
3835 struct cfg80211_chan_def *chandef)
3837 struct ieee80211_local *local = sdata->local;
3838 struct ieee80211_supported_band *sband;
3839 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3840 const struct ieee80211_sta_he_cap *he_cap;
3841 const struct ieee80211_sta_eht_cap *eht_cap;
3842 struct cfg80211_chan_def he_chandef = *chandef;
3843 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3844 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3845 bool support_80_80, support_160, support_320;
3846 u8 he_phy_cap, eht_phy_cap;
3849 if (chandef->chan->band != NL80211_BAND_6GHZ)
3852 sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3854 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3856 sdata_info(sdata, "Missing iftype sband data/HE cap");
3860 he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
3863 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3866 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3870 "HE is not advertised on (on %d MHz), expect issues\n",
3871 chandef->chan->center_freq);
3875 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
3879 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3881 if (!he_6ghz_oper) {
3883 "HE 6GHz operation missing (on %d MHz), expect issues\n",
3884 chandef->chan->center_freq);
3889 * The EHT operation IE does not contain the primary channel so the
3890 * primary channel frequency should be taken from the 6 GHz operation
3893 freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3895 he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
3897 switch (u8_get_bits(he_6ghz_oper->control,
3898 IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
3899 case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
3900 bss_conf->power_type = IEEE80211_REG_LPI_AP;
3902 case IEEE80211_6GHZ_CTRL_REG_SP_AP:
3903 bss_conf->power_type = IEEE80211_REG_SP_AP;
3906 bss_conf->power_type = IEEE80211_REG_UNSET_AP;
3911 !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3912 switch (u8_get_bits(he_6ghz_oper->control,
3913 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3914 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3915 he_chandef.width = NL80211_CHAN_WIDTH_20;
3917 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3918 he_chandef.width = NL80211_CHAN_WIDTH_40;
3920 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3921 he_chandef.width = NL80211_CHAN_WIDTH_80;
3923 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3924 he_chandef.width = NL80211_CHAN_WIDTH_80;
3925 if (!he_6ghz_oper->ccfs1)
3927 if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
3929 he_chandef.width = NL80211_CHAN_WIDTH_160;
3932 he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3937 if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3938 he_chandef.center_freq1 =
3939 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3942 he_chandef.center_freq1 =
3943 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3945 if (support_80_80 || support_160)
3946 he_chandef.center_freq2 =
3947 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3951 eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
3953 eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
3955 ieee80211_chandef_eht_oper(eht_oper, support_160,
3956 support_320, &he_chandef);
3959 if (!cfg80211_chandef_valid(&he_chandef)) {
3961 "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
3962 he_chandef.chan ? he_chandef.chan->center_freq : 0,
3964 he_chandef.center_freq1,
3965 he_chandef.center_freq2);
3969 *chandef = he_chandef;
3974 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
3975 struct cfg80211_chan_def *chandef)
3982 switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3983 case IEEE80211_S1G_CHANWIDTH_1MHZ:
3984 chandef->width = NL80211_CHAN_WIDTH_1;
3986 case IEEE80211_S1G_CHANWIDTH_2MHZ:
3987 chandef->width = NL80211_CHAN_WIDTH_2;
3989 case IEEE80211_S1G_CHANWIDTH_4MHZ:
3990 chandef->width = NL80211_CHAN_WIDTH_4;
3992 case IEEE80211_S1G_CHANWIDTH_8MHZ:
3993 chandef->width = NL80211_CHAN_WIDTH_8;
3995 case IEEE80211_S1G_CHANWIDTH_16MHZ:
3996 chandef->width = NL80211_CHAN_WIDTH_16;
4002 oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
4003 NL80211_BAND_S1GHZ);
4004 chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
4005 chandef->freq1_offset = oper_freq % 1000;
4010 int ieee80211_parse_bitrates(enum nl80211_chan_width width,
4011 const struct ieee80211_supported_band *sband,
4012 const u8 *srates, int srates_len, u32 *rates)
4014 u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
4015 int shift = ieee80211_chanwidth_get_shift(width);
4016 struct ieee80211_rate *br;
4017 int brate, rate, i, j, count = 0;
4021 for (i = 0; i < srates_len; i++) {
4022 rate = srates[i] & 0x7f;
4024 for (j = 0; j < sband->n_bitrates; j++) {
4025 br = &sband->bitrates[j];
4026 if ((rate_flags & br->flags) != rate_flags)
4029 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
4030 if (brate == rate) {
4040 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
4041 struct sk_buff *skb, bool need_basic,
4042 enum nl80211_band band)
4044 struct ieee80211_local *local = sdata->local;
4045 struct ieee80211_supported_band *sband;
4048 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
4051 shift = ieee80211_vif_get_shift(&sdata->vif);
4052 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
4053 sband = local->hw.wiphy->bands[band];
4055 for (i = 0; i < sband->n_bitrates; i++) {
4056 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4063 if (skb_tailroom(skb) < rates + 2)
4066 pos = skb_put(skb, rates + 2);
4067 *pos++ = WLAN_EID_SUPP_RATES;
4069 for (i = 0; i < rates; i++) {
4071 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4074 if (need_basic && basic_rates & BIT(i))
4076 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4078 *pos++ = basic | (u8) rate;
4084 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
4085 struct sk_buff *skb, bool need_basic,
4086 enum nl80211_band band)
4088 struct ieee80211_local *local = sdata->local;
4089 struct ieee80211_supported_band *sband;
4091 u8 i, exrates, *pos;
4092 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
4095 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
4096 shift = ieee80211_vif_get_shift(&sdata->vif);
4098 sband = local->hw.wiphy->bands[band];
4100 for (i = 0; i < sband->n_bitrates; i++) {
4101 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4111 if (skb_tailroom(skb) < exrates + 2)
4115 pos = skb_put(skb, exrates + 2);
4116 *pos++ = WLAN_EID_EXT_SUPP_RATES;
4118 for (i = 8; i < sband->n_bitrates; i++) {
4120 if ((rate_flags & sband->bitrates[i].flags)
4123 if (need_basic && basic_rates & BIT(i))
4125 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4127 *pos++ = basic | (u8) rate;
4133 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
4135 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4137 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
4140 return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
4142 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
4144 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
4149 /* TODO: consider rx_highest */
4151 if (mcs->rx_mask[3])
4153 if (mcs->rx_mask[2])
4155 if (mcs->rx_mask[1])
4161 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
4162 * @local: mac80211 hw info struct
4163 * @status: RX status
4164 * @mpdu_len: total MPDU length (including FCS)
4165 * @mpdu_offset: offset into MPDU to calculate timestamp at
4167 * This function calculates the RX timestamp at the given MPDU offset, taking
4168 * into account what the RX timestamp was. An offset of 0 will just normalize
4169 * the timestamp to TSF at beginning of MPDU reception.
4171 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
4172 struct ieee80211_rx_status *status,
4173 unsigned int mpdu_len,
4174 unsigned int mpdu_offset)
4176 u64 ts = status->mactime;
4177 struct rate_info ri;
4181 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
4184 memset(&ri, 0, sizeof(ri));
4188 /* Fill cfg80211 rate info */
4189 switch (status->encoding) {
4191 ri.flags |= RATE_INFO_FLAGS_EHT_MCS;
4192 ri.mcs = status->rate_idx;
4193 ri.nss = status->nss;
4194 ri.eht_ru_alloc = status->eht.ru;
4195 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4196 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4197 /* TODO/FIXME: is this right? handle other PPDUs */
4198 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4204 ri.flags |= RATE_INFO_FLAGS_HE_MCS;
4205 ri.mcs = status->rate_idx;
4206 ri.nss = status->nss;
4207 ri.he_ru_alloc = status->he_ru;
4208 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4209 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4212 * See P802.11ax_D6.0, section 27.3.4 for
4215 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4221 * For HE MU PPDU, add the HE-SIG-B.
4222 * For HE ER PPDU, add 8us for the HE-SIG-A.
4223 * For HE TB PPDU, add 4us for the HE-STF.
4224 * Add the HE-LTF durations - variable.
4230 ri.mcs = status->rate_idx;
4231 ri.flags |= RATE_INFO_FLAGS_MCS;
4232 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4233 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4236 * See P802.11REVmd_D3.0, section 19.3.2 for
4239 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4241 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
4247 * Add Data HT-LTFs per streams
4248 * TODO: add Extension HT-LTFs, 4us per LTF
4250 n_ltf = ((ri.mcs >> 3) & 3) + 1;
4251 n_ltf = n_ltf == 3 ? 4 : n_ltf;
4257 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
4258 ri.mcs = status->rate_idx;
4259 ri.nss = status->nss;
4260 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4261 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4264 * See P802.11REVmd_D3.0, section 21.3.2 for
4267 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4272 * Add VHT-LTFs per streams
4274 n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
4275 ri.nss + 1 : ri.nss;
4283 case RX_ENC_LEGACY: {
4284 struct ieee80211_supported_band *sband;
4288 switch (status->bw) {
4289 case RATE_INFO_BW_10:
4292 case RATE_INFO_BW_5:
4297 sband = local->hw.wiphy->bands[status->band];
4298 bitrate = sband->bitrates[status->rate_idx].bitrate;
4299 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
4301 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4302 if (status->band == NL80211_BAND_5GHZ) {
4305 } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
4315 rate = cfg80211_calculate_bitrate(&ri);
4316 if (WARN_ONCE(!rate,
4317 "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
4318 (unsigned long long)status->flag, status->rate_idx,
4322 /* rewind from end of MPDU */
4323 if (status->flag & RX_FLAG_MACTIME_END)
4324 ts -= mpdu_len * 8 * 10 / rate;
4326 ts += mpdu_offset * 8 * 10 / rate;
4331 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
4333 struct ieee80211_sub_if_data *sdata;
4334 struct cfg80211_chan_def chandef;
4336 /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
4337 lockdep_assert_wiphy(local->hw.wiphy);
4339 mutex_lock(&local->mtx);
4340 list_for_each_entry(sdata, &local->interfaces, list) {
4341 /* it might be waiting for the local->mtx, but then
4342 * by the time it gets it, sdata->wdev.cac_started
4343 * will no longer be true
4345 cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
4347 if (sdata->wdev.cac_started) {
4348 chandef = sdata->vif.bss_conf.chandef;
4349 ieee80211_link_release_channel(&sdata->deflink);
4350 cfg80211_cac_event(sdata->dev,
4352 NL80211_RADAR_CAC_ABORTED,
4356 mutex_unlock(&local->mtx);
4359 void ieee80211_dfs_radar_detected_work(struct wiphy *wiphy,
4360 struct wiphy_work *work)
4362 struct ieee80211_local *local =
4363 container_of(work, struct ieee80211_local, radar_detected_work);
4364 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
4365 struct ieee80211_chanctx *ctx;
4366 int num_chanctx = 0;
4368 mutex_lock(&local->chanctx_mtx);
4369 list_for_each_entry(ctx, &local->chanctx_list, list) {
4370 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
4374 chandef = ctx->conf.def;
4376 mutex_unlock(&local->chanctx_mtx);
4378 ieee80211_dfs_cac_cancel(local);
4380 if (num_chanctx > 1)
4381 /* XXX: multi-channel is not supported yet */
4384 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
4387 void ieee80211_radar_detected(struct ieee80211_hw *hw)
4389 struct ieee80211_local *local = hw_to_local(hw);
4391 trace_api_radar_detected(local);
4393 wiphy_work_queue(hw->wiphy, &local->radar_detected_work);
4395 EXPORT_SYMBOL(ieee80211_radar_detected);
4397 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
4399 ieee80211_conn_flags_t ret;
4403 case NL80211_CHAN_WIDTH_20:
4404 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4405 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4407 case NL80211_CHAN_WIDTH_40:
4408 c->width = NL80211_CHAN_WIDTH_20;
4409 c->center_freq1 = c->chan->center_freq;
4410 ret = IEEE80211_CONN_DISABLE_40MHZ |
4411 IEEE80211_CONN_DISABLE_VHT;
4413 case NL80211_CHAN_WIDTH_80:
4414 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
4418 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
4419 c->width = NL80211_CHAN_WIDTH_40;
4420 ret = IEEE80211_CONN_DISABLE_VHT;
4422 case NL80211_CHAN_WIDTH_80P80:
4423 c->center_freq2 = 0;
4424 c->width = NL80211_CHAN_WIDTH_80;
4425 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4426 IEEE80211_CONN_DISABLE_160MHZ;
4428 case NL80211_CHAN_WIDTH_160:
4430 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
4433 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
4434 c->width = NL80211_CHAN_WIDTH_80;
4435 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4436 IEEE80211_CONN_DISABLE_160MHZ;
4438 case NL80211_CHAN_WIDTH_320:
4440 tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
4443 c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
4444 c->width = NL80211_CHAN_WIDTH_160;
4445 ret = IEEE80211_CONN_DISABLE_320MHZ;
4448 case NL80211_CHAN_WIDTH_20_NOHT:
4450 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4451 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4453 case NL80211_CHAN_WIDTH_1:
4454 case NL80211_CHAN_WIDTH_2:
4455 case NL80211_CHAN_WIDTH_4:
4456 case NL80211_CHAN_WIDTH_8:
4457 case NL80211_CHAN_WIDTH_16:
4458 case NL80211_CHAN_WIDTH_5:
4459 case NL80211_CHAN_WIDTH_10:
4462 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4466 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
4472 * Returns true if smps_mode_new is strictly more restrictive than
4475 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
4476 enum ieee80211_smps_mode smps_mode_new)
4478 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
4479 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
4482 switch (smps_mode_old) {
4483 case IEEE80211_SMPS_STATIC:
4485 case IEEE80211_SMPS_DYNAMIC:
4486 return smps_mode_new == IEEE80211_SMPS_STATIC;
4487 case IEEE80211_SMPS_OFF:
4488 return smps_mode_new != IEEE80211_SMPS_OFF;
4496 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
4497 struct cfg80211_csa_settings *csa_settings)
4499 struct sk_buff *skb;
4500 struct ieee80211_mgmt *mgmt;
4501 struct ieee80211_local *local = sdata->local;
4503 int hdr_len = offsetofend(struct ieee80211_mgmt,
4504 u.action.u.chan_switch);
4507 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4508 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4511 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
4512 5 + /* channel switch announcement element */
4513 3 + /* secondary channel offset element */
4514 5 + /* wide bandwidth channel switch announcement */
4515 8); /* mesh channel switch parameters element */
4519 skb_reserve(skb, local->tx_headroom);
4520 mgmt = skb_put_zero(skb, hdr_len);
4521 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4522 IEEE80211_STYPE_ACTION);
4524 eth_broadcast_addr(mgmt->da);
4525 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
4526 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4527 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
4529 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4530 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
4532 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
4533 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
4534 pos = skb_put(skb, 5);
4535 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
4536 *pos++ = 3; /* IE length */
4537 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
4538 freq = csa_settings->chandef.chan->center_freq;
4539 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
4540 *pos++ = csa_settings->count; /* count */
4542 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
4543 enum nl80211_channel_type ch_type;
4546 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
4547 *pos++ = 1; /* IE length */
4548 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
4549 if (ch_type == NL80211_CHAN_HT40PLUS)
4550 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
4552 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
4555 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4556 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4559 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
4560 *pos++ = 6; /* IE length */
4561 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
4562 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
4563 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
4564 *pos++ |= csa_settings->block_tx ?
4565 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
4566 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
4568 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
4572 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
4573 csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
4574 csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
4576 ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
4579 ieee80211_tx_skb(sdata, skb);
4584 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
4586 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
4593 if (data->count[i] == 1)
4596 if (data->desc[i].interval == 0)
4599 /* End time is in the past, check for repetitions */
4600 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
4601 if (data->count[i] < 255) {
4602 if (data->count[i] <= skip) {
4607 data->count[i] -= skip;
4610 data->desc[i].start += skip * data->desc[i].interval;
4616 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
4622 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4625 if (!data->count[i])
4628 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
4631 cur = data->desc[i].start - tsf;
4635 cur = data->desc[i].start + data->desc[i].duration - tsf;
4644 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
4649 * arbitrary limit, used to avoid infinite loops when combined NoA
4650 * descriptors cover the full time period.
4654 ieee80211_extend_absent_time(data, tsf, &offset);
4656 if (!ieee80211_extend_absent_time(data, tsf, &offset))
4660 } while (tries < max_tries);
4665 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4667 u32 next_offset = BIT(31) - 1;
4671 data->has_next_tsf = false;
4672 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4675 if (!data->count[i])
4678 ieee80211_extend_noa_desc(data, tsf, i);
4679 start = data->desc[i].start - tsf;
4681 data->absent |= BIT(i);
4683 if (next_offset > start)
4684 next_offset = start;
4686 data->has_next_tsf = true;
4690 next_offset = ieee80211_get_noa_absent_time(data, tsf);
4692 data->next_tsf = tsf + next_offset;
4694 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4696 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4697 struct ieee80211_noa_data *data, u32 tsf)
4702 memset(data, 0, sizeof(*data));
4704 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4705 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4707 if (!desc->count || !desc->duration)
4710 data->count[i] = desc->count;
4711 data->desc[i].start = le32_to_cpu(desc->start_time);
4712 data->desc[i].duration = le32_to_cpu(desc->duration);
4713 data->desc[i].interval = le32_to_cpu(desc->interval);
4715 if (data->count[i] > 1 &&
4716 data->desc[i].interval < data->desc[i].duration)
4719 ieee80211_extend_noa_desc(data, tsf, i);
4724 ieee80211_update_p2p_noa(data, tsf);
4728 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4730 void ieee80211_recalc_dtim(struct ieee80211_local *local,
4731 struct ieee80211_sub_if_data *sdata)
4733 u64 tsf = drv_get_tsf(local, sdata);
4735 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4736 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4740 if (tsf == -1ULL || !beacon_int || !dtim_period)
4743 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4744 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4748 ps = &sdata->bss->ps;
4749 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4750 ps = &sdata->u.mesh.ps;
4756 * actually finds last dtim_count, mac80211 will update in
4757 * __beacon_add_tim().
4758 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4760 do_div(tsf, beacon_int);
4761 bcns_from_dtim = do_div(tsf, dtim_period);
4762 /* just had a DTIM */
4763 if (!bcns_from_dtim)
4766 dtim_count = dtim_period - bcns_from_dtim;
4768 ps->dtim_count = dtim_count;
4771 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4772 struct ieee80211_chanctx *ctx)
4774 struct ieee80211_link_data *link;
4775 u8 radar_detect = 0;
4777 lockdep_assert_held(&local->chanctx_mtx);
4779 if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4782 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
4783 if (link->reserved_radar_required)
4784 radar_detect |= BIT(link->reserved_chandef.width);
4787 * An in-place reservation context should not have any assigned vifs
4788 * until it replaces the other context.
4790 WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
4791 !list_empty(&ctx->assigned_links));
4793 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
4794 if (!link->radar_required)
4798 BIT(link->conf->chandef.width);
4801 return radar_detect;
4804 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4805 const struct cfg80211_chan_def *chandef,
4806 enum ieee80211_chanctx_mode chanmode,
4809 struct ieee80211_local *local = sdata->local;
4810 struct ieee80211_sub_if_data *sdata_iter;
4811 enum nl80211_iftype iftype = sdata->wdev.iftype;
4812 struct ieee80211_chanctx *ctx;
4814 struct iface_combination_params params = {
4815 .radar_detect = radar_detect,
4818 lockdep_assert_held(&local->chanctx_mtx);
4820 if (WARN_ON(hweight32(radar_detect) > 1))
4823 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4827 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4830 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4831 sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4833 * always passing this is harmless, since it'll be the
4834 * same value that cfg80211 finds if it finds the same
4835 * interface ... and that's always allowed
4837 params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4840 /* Always allow software iftypes */
4841 if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4848 params.num_different_channels = 1;
4850 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4851 params.iftype_num[iftype] = 1;
4853 list_for_each_entry(ctx, &local->chanctx_list, list) {
4854 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4856 params.radar_detect |=
4857 ieee80211_chanctx_radar_detect(local, ctx);
4858 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
4859 params.num_different_channels++;
4862 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4863 cfg80211_chandef_compatible(chandef,
4866 params.num_different_channels++;
4869 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
4870 struct wireless_dev *wdev_iter;
4872 wdev_iter = &sdata_iter->wdev;
4874 if (sdata_iter == sdata ||
4875 !ieee80211_sdata_running(sdata_iter) ||
4876 cfg80211_iftype_allowed(local->hw.wiphy,
4877 wdev_iter->iftype, 0, 1))
4880 params.iftype_num[wdev_iter->iftype]++;
4884 if (total == 1 && !params.radar_detect)
4887 return cfg80211_check_combinations(local->hw.wiphy, ¶ms);
4891 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4894 u32 *max_num_different_channels = data;
4896 *max_num_different_channels = max(*max_num_different_channels,
4897 c->num_different_channels);
4900 int ieee80211_max_num_channels(struct ieee80211_local *local)
4902 struct ieee80211_sub_if_data *sdata;
4903 struct ieee80211_chanctx *ctx;
4904 u32 max_num_different_channels = 1;
4906 struct iface_combination_params params = {0};
4908 lockdep_assert_held(&local->chanctx_mtx);
4910 list_for_each_entry(ctx, &local->chanctx_list, list) {
4911 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4914 params.num_different_channels++;
4916 params.radar_detect |=
4917 ieee80211_chanctx_radar_detect(local, ctx);
4920 list_for_each_entry_rcu(sdata, &local->interfaces, list)
4921 params.iftype_num[sdata->wdev.iftype]++;
4923 err = cfg80211_iter_combinations(local->hw.wiphy, ¶ms,
4924 ieee80211_iter_max_chans,
4925 &max_num_different_channels);
4929 return max_num_different_channels;
4932 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4933 struct ieee80211_sta_s1g_cap *caps,
4934 struct sk_buff *skb)
4936 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4937 struct ieee80211_s1g_cap s1g_capab;
4941 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4947 memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4948 memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4950 /* override the capability info */
4951 for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4952 u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4954 s1g_capab.capab_info[i] &= ~mask;
4955 s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4958 /* then MCS and NSS set */
4959 for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4960 u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4962 s1g_capab.supp_mcs_nss[i] &= ~mask;
4963 s1g_capab.supp_mcs_nss[i] |=
4964 ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4967 pos = skb_put(skb, 2 + sizeof(s1g_capab));
4968 *pos++ = WLAN_EID_S1G_CAPABILITIES;
4969 *pos++ = sizeof(s1g_capab);
4971 memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4974 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4975 struct sk_buff *skb)
4977 u8 *pos = skb_put(skb, 3);
4979 *pos++ = WLAN_EID_AID_REQUEST;
4984 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4986 *buf++ = WLAN_EID_VENDOR_SPECIFIC;
4987 *buf++ = 7; /* len */
4988 *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4991 *buf++ = 2; /* WME */
4992 *buf++ = 0; /* WME info */
4993 *buf++ = 1; /* WME ver */
4994 *buf++ = qosinfo; /* U-APSD no in use */
4999 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
5000 unsigned long *frame_cnt,
5001 unsigned long *byte_cnt)
5003 struct txq_info *txqi = to_txq_info(txq);
5004 u32 frag_cnt = 0, frag_bytes = 0;
5005 struct sk_buff *skb;
5007 skb_queue_walk(&txqi->frags, skb) {
5009 frag_bytes += skb->len;
5013 *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
5016 *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
5018 EXPORT_SYMBOL(ieee80211_txq_get_depth);
5020 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
5021 IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
5022 IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
5023 IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
5024 IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
5027 u16 ieee80211_encode_usf(int listen_interval)
5029 static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
5032 /* find greatest USF */
5033 while (usf < IEEE80211_MAX_USF) {
5034 if (listen_interval % listen_int_usf[usf + 1])
5038 ui = listen_interval / listen_int_usf[usf];
5040 /* error if there is a remainder. Should've been checked by user */
5041 WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
5042 listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
5043 FIELD_PREP(LISTEN_INT_UI, ui);
5045 return (u16) listen_interval;
5048 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
5050 const struct ieee80211_sta_he_cap *he_cap;
5051 const struct ieee80211_sta_eht_cap *eht_cap;
5052 struct ieee80211_supported_band *sband;
5056 sband = ieee80211_get_sband(sdata);
5060 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
5061 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
5062 if (!he_cap || !eht_cap)
5065 is_ap = iftype == NL80211_IFTYPE_AP ||
5066 iftype == NL80211_IFTYPE_P2P_GO;
5068 n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
5069 &eht_cap->eht_cap_elem,
5072 sizeof(eht_cap->eht_cap_elem) + n +
5073 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
5074 eht_cap->eht_cap_elem.phy_cap_info);
5078 u8 *ieee80211_ie_build_eht_cap(u8 *pos,
5079 const struct ieee80211_sta_he_cap *he_cap,
5080 const struct ieee80211_sta_eht_cap *eht_cap,
5084 u8 mcs_nss_len, ppet_len;
5088 /* Make sure we have place for the IE */
5089 if (!he_cap || !eht_cap)
5092 mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
5093 &eht_cap->eht_cap_elem,
5095 ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
5096 eht_cap->eht_cap_elem.phy_cap_info);
5098 ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
5099 if ((end - pos) < ie_len)
5102 *pos++ = WLAN_EID_EXTENSION;
5103 *pos++ = ie_len - 2;
5104 *pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
5107 memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
5108 pos += sizeof(eht_cap->eht_cap_elem);
5110 memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
5114 memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
5121 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id)
5123 unsigned int elem_len;
5128 elem_len = skb->data + skb->len - len_pos - 1;
5130 while (elem_len > 255) {
5131 /* this one is 255 */
5133 /* remaining data gets smaller */
5135 /* make space for the fragment ID/len in SKB */
5137 /* shift back the remaining data to place fragment ID/len */
5138 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
5139 /* place the fragment ID */
5142 /* and point to fragment length to update later */
5146 *len_pos = elem_len;