1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright (C) 2015-2017 Intel Deutschland GmbH
9 * Copyright (C) 2018-2022 Intel Corporation
11 * utilities for mac80211
14 #include <net/mac80211.h>
15 #include <linux/netdevice.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/etherdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/bitmap.h>
23 #include <linux/crc32.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/rtnetlink.h>
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
36 /* privid for wiphys to determine whether they belong to us or not */
37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
41 struct ieee80211_local *local;
43 local = wiphy_priv(wiphy);
46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
49 enum nl80211_iftype type)
51 __le16 fc = hdr->frame_control;
53 if (ieee80211_is_data(fc)) {
54 if (len < 24) /* drop incorrect hdr len (data) */
57 if (ieee80211_has_a4(fc))
59 if (ieee80211_has_tods(fc))
61 if (ieee80211_has_fromds(fc))
67 if (ieee80211_is_s1g_beacon(fc)) {
68 struct ieee80211_ext *ext = (void *) hdr;
70 return ext->u.s1g_beacon.sa;
73 if (ieee80211_is_mgmt(fc)) {
74 if (len < 24) /* drop incorrect hdr len (mgmt) */
79 if (ieee80211_is_ctl(fc)) {
80 if (ieee80211_is_pspoll(fc))
83 if (ieee80211_is_back_req(fc)) {
85 case NL80211_IFTYPE_STATION:
87 case NL80211_IFTYPE_AP:
88 case NL80211_IFTYPE_AP_VLAN:
91 break; /* fall through to the return */
98 EXPORT_SYMBOL(ieee80211_get_bssid);
100 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
103 struct ieee80211_hdr *hdr;
105 skb_queue_walk(&tx->skbs, skb) {
106 hdr = (struct ieee80211_hdr *) skb->data;
107 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
111 int ieee80211_frame_duration(enum nl80211_band band, size_t len,
112 int rate, int erp, int short_preamble,
117 /* calculate duration (in microseconds, rounded up to next higher
118 * integer if it includes a fractional microsecond) to send frame of
119 * len bytes (does not include FCS) at the given rate. Duration will
122 * rate is in 100 kbps, so divident is multiplied by 10 in the
123 * DIV_ROUND_UP() operations.
125 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
126 * is assumed to be 0 otherwise.
129 if (band == NL80211_BAND_5GHZ || erp) {
133 * N_DBPS = DATARATE x 4
134 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
135 * (16 = SIGNAL time, 6 = tail bits)
136 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
139 * 802.11a - 18.5.2: aSIFSTime = 16 usec
140 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
141 * signal ext = 6 usec
143 dur = 16; /* SIFS + signal ext */
144 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
145 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
147 /* IEEE 802.11-2012 18.3.2.4: all values above are:
148 * * times 4 for 5 MHz
149 * * times 2 for 10 MHz
153 /* rates should already consider the channel bandwidth,
154 * don't apply divisor again.
156 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
157 4 * rate); /* T_SYM x N_SYM */
160 * 802.11b or 802.11g with 802.11b compatibility:
161 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
162 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
164 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
165 * aSIFSTime = 10 usec
166 * aPreambleLength = 144 usec or 72 usec with short preamble
167 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
169 dur = 10; /* aSIFSTime = 10 usec */
170 dur += short_preamble ? (72 + 24) : (144 + 48);
172 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
178 /* Exported duration function for driver use */
179 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
180 struct ieee80211_vif *vif,
181 enum nl80211_band band,
183 struct ieee80211_rate *rate)
185 struct ieee80211_sub_if_data *sdata;
188 bool short_preamble = false;
192 sdata = vif_to_sdata(vif);
193 short_preamble = sdata->vif.bss_conf.use_short_preamble;
194 if (sdata->deflink.operating_11g_mode)
195 erp = rate->flags & IEEE80211_RATE_ERP_G;
196 shift = ieee80211_vif_get_shift(vif);
199 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
200 short_preamble, shift);
202 return cpu_to_le16(dur);
204 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
206 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
207 struct ieee80211_vif *vif, size_t frame_len,
208 const struct ieee80211_tx_info *frame_txctl)
210 struct ieee80211_local *local = hw_to_local(hw);
211 struct ieee80211_rate *rate;
212 struct ieee80211_sub_if_data *sdata;
214 int erp, shift = 0, bitrate;
216 struct ieee80211_supported_band *sband;
218 sband = local->hw.wiphy->bands[frame_txctl->band];
220 short_preamble = false;
222 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
226 sdata = vif_to_sdata(vif);
227 short_preamble = sdata->vif.bss_conf.use_short_preamble;
228 if (sdata->deflink.operating_11g_mode)
229 erp = rate->flags & IEEE80211_RATE_ERP_G;
230 shift = ieee80211_vif_get_shift(vif);
233 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
236 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
237 erp, short_preamble, shift);
238 /* Data frame duration */
239 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
240 erp, short_preamble, shift);
242 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
243 erp, short_preamble, shift);
245 return cpu_to_le16(dur);
247 EXPORT_SYMBOL(ieee80211_rts_duration);
249 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
250 struct ieee80211_vif *vif,
252 const struct ieee80211_tx_info *frame_txctl)
254 struct ieee80211_local *local = hw_to_local(hw);
255 struct ieee80211_rate *rate;
256 struct ieee80211_sub_if_data *sdata;
258 int erp, shift = 0, bitrate;
260 struct ieee80211_supported_band *sband;
262 sband = local->hw.wiphy->bands[frame_txctl->band];
264 short_preamble = false;
266 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
269 sdata = vif_to_sdata(vif);
270 short_preamble = sdata->vif.bss_conf.use_short_preamble;
271 if (sdata->deflink.operating_11g_mode)
272 erp = rate->flags & IEEE80211_RATE_ERP_G;
273 shift = ieee80211_vif_get_shift(vif);
276 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
278 /* Data frame duration */
279 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
280 erp, short_preamble, shift);
281 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
283 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
284 erp, short_preamble, shift);
287 return cpu_to_le16(dur);
289 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
291 static void 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;
926 len = elem->datalen - 1;
928 switch (elem->data[0]) {
929 case WLAN_EID_EXT_HE_MU_EDCA:
930 if (len >= sizeof(*elems->mu_edca_param_set)) {
931 elems->mu_edca_param_set = data;
933 *crc = crc32_be(*crc, (void *)elem,
937 case WLAN_EID_EXT_HE_CAPABILITY:
938 if (ieee80211_he_capa_size_ok(data, len)) {
939 elems->he_cap = data;
940 elems->he_cap_len = len;
943 case WLAN_EID_EXT_HE_OPERATION:
944 if (len >= sizeof(*elems->he_operation) &&
945 len >= ieee80211_he_oper_size(data) - 1) {
947 *crc = crc32_be(*crc, (void *)elem,
949 elems->he_operation = data;
952 case WLAN_EID_EXT_UORA:
954 elems->uora_element = data;
956 case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
958 elems->max_channel_switch_time = data;
960 case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
961 if (len >= sizeof(*elems->mbssid_config_ie))
962 elems->mbssid_config_ie = data;
964 case WLAN_EID_EXT_HE_SPR:
965 if (len >= sizeof(*elems->he_spr) &&
966 len >= ieee80211_he_spr_size(data))
967 elems->he_spr = data;
969 case WLAN_EID_EXT_HE_6GHZ_CAPA:
970 if (len >= sizeof(*elems->he_6ghz_capa))
971 elems->he_6ghz_capa = data;
973 case WLAN_EID_EXT_EHT_CAPABILITY:
974 if (ieee80211_eht_capa_size_ok(elems->he_cap,
977 elems->eht_cap = data;
978 elems->eht_cap_len = len;
981 case WLAN_EID_EXT_EHT_OPERATION:
982 if (ieee80211_eht_oper_size_ok(data, len))
983 elems->eht_operation = data;
985 case WLAN_EID_EXT_EHT_MULTI_LINK:
986 if (ieee80211_mle_size_ok(data, len)) {
987 elems->multi_link = (void *)data;
988 elems->multi_link_len = len;
995 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
996 struct ieee802_11_elems *elems,
997 const struct element *check_inherit)
999 const struct element *elem;
1000 bool calc_crc = params->filter != 0;
1001 DECLARE_BITMAP(seen_elems, 256);
1002 u32 crc = params->crc;
1005 bitmap_zero(seen_elems, 256);
1007 for_each_element(elem, params->start, params->len) {
1008 bool elem_parse_failed;
1010 u8 elen = elem->datalen;
1011 const u8 *pos = elem->data;
1013 if (check_inherit &&
1014 !cfg80211_is_element_inherited(elem,
1020 case WLAN_EID_SUPP_RATES:
1021 case WLAN_EID_FH_PARAMS:
1022 case WLAN_EID_DS_PARAMS:
1023 case WLAN_EID_CF_PARAMS:
1025 case WLAN_EID_IBSS_PARAMS:
1026 case WLAN_EID_CHALLENGE:
1028 case WLAN_EID_ERP_INFO:
1029 case WLAN_EID_EXT_SUPP_RATES:
1030 case WLAN_EID_HT_CAPABILITY:
1031 case WLAN_EID_HT_OPERATION:
1032 case WLAN_EID_VHT_CAPABILITY:
1033 case WLAN_EID_VHT_OPERATION:
1034 case WLAN_EID_MESH_ID:
1035 case WLAN_EID_MESH_CONFIG:
1036 case WLAN_EID_PEER_MGMT:
1041 case WLAN_EID_CHANNEL_SWITCH:
1042 case WLAN_EID_EXT_CHANSWITCH_ANN:
1043 case WLAN_EID_COUNTRY:
1044 case WLAN_EID_PWR_CONSTRAINT:
1045 case WLAN_EID_TIMEOUT_INTERVAL:
1046 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1047 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1048 case WLAN_EID_CHAN_SWITCH_PARAM:
1049 case WLAN_EID_EXT_CAPABILITY:
1050 case WLAN_EID_CHAN_SWITCH_TIMING:
1051 case WLAN_EID_LINK_ID:
1052 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1054 case WLAN_EID_S1G_BCN_COMPAT:
1055 case WLAN_EID_S1G_CAPABILITIES:
1056 case WLAN_EID_S1G_OPERATION:
1057 case WLAN_EID_AID_RESPONSE:
1058 case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
1060 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1061 * that if the content gets bigger it might be needed more than once
1063 if (test_bit(id, seen_elems)) {
1064 elems->parse_error = true;
1070 if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
1071 crc = crc32_be(crc, pos - 2, elen + 2);
1073 elem_parse_failed = false;
1076 case WLAN_EID_LINK_ID:
1077 if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
1078 elem_parse_failed = true;
1081 elems->lnk_id = (void *)(pos - 2);
1083 case WLAN_EID_CHAN_SWITCH_TIMING:
1084 if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
1085 elem_parse_failed = true;
1088 elems->ch_sw_timing = (void *)pos;
1090 case WLAN_EID_EXT_CAPABILITY:
1091 elems->ext_capab = pos;
1092 elems->ext_capab_len = elen;
1096 elems->ssid_len = elen;
1098 case WLAN_EID_SUPP_RATES:
1099 elems->supp_rates = pos;
1100 elems->supp_rates_len = elen;
1102 case WLAN_EID_DS_PARAMS:
1104 elems->ds_params = pos;
1106 elem_parse_failed = true;
1109 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1110 elems->tim = (void *)pos;
1111 elems->tim_len = elen;
1113 elem_parse_failed = true;
1115 case WLAN_EID_VENDOR_SPECIFIC:
1116 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1118 /* Microsoft OUI (00:50:F2) */
1121 crc = crc32_be(crc, pos - 2, elen + 2);
1123 if (elen >= 5 && pos[3] == 2) {
1124 /* OUI Type 2 - WMM IE */
1126 elems->wmm_info = pos;
1127 elems->wmm_info_len = elen;
1128 } else if (pos[4] == 1) {
1129 elems->wmm_param = pos;
1130 elems->wmm_param_len = elen;
1137 elems->rsn_len = elen;
1139 case WLAN_EID_ERP_INFO:
1141 elems->erp_info = pos;
1143 elem_parse_failed = true;
1145 case WLAN_EID_EXT_SUPP_RATES:
1146 elems->ext_supp_rates = pos;
1147 elems->ext_supp_rates_len = elen;
1149 case WLAN_EID_HT_CAPABILITY:
1150 if (elen >= sizeof(struct ieee80211_ht_cap))
1151 elems->ht_cap_elem = (void *)pos;
1153 elem_parse_failed = true;
1155 case WLAN_EID_HT_OPERATION:
1156 if (elen >= sizeof(struct ieee80211_ht_operation))
1157 elems->ht_operation = (void *)pos;
1159 elem_parse_failed = true;
1161 case WLAN_EID_VHT_CAPABILITY:
1162 if (elen >= sizeof(struct ieee80211_vht_cap))
1163 elems->vht_cap_elem = (void *)pos;
1165 elem_parse_failed = true;
1167 case WLAN_EID_VHT_OPERATION:
1168 if (elen >= sizeof(struct ieee80211_vht_operation)) {
1169 elems->vht_operation = (void *)pos;
1171 crc = crc32_be(crc, pos - 2, elen + 2);
1174 elem_parse_failed = true;
1176 case WLAN_EID_OPMODE_NOTIF:
1178 elems->opmode_notif = pos;
1180 crc = crc32_be(crc, pos - 2, elen + 2);
1183 elem_parse_failed = true;
1185 case WLAN_EID_MESH_ID:
1186 elems->mesh_id = pos;
1187 elems->mesh_id_len = elen;
1189 case WLAN_EID_MESH_CONFIG:
1190 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1191 elems->mesh_config = (void *)pos;
1193 elem_parse_failed = true;
1195 case WLAN_EID_PEER_MGMT:
1196 elems->peering = pos;
1197 elems->peering_len = elen;
1199 case WLAN_EID_MESH_AWAKE_WINDOW:
1201 elems->awake_window = (void *)pos;
1205 elems->preq_len = elen;
1209 elems->prep_len = elen;
1213 elems->perr_len = elen;
1216 if (elen >= sizeof(struct ieee80211_rann_ie))
1217 elems->rann = (void *)pos;
1219 elem_parse_failed = true;
1221 case WLAN_EID_CHANNEL_SWITCH:
1222 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
1223 elem_parse_failed = true;
1226 elems->ch_switch_ie = (void *)pos;
1228 case WLAN_EID_EXT_CHANSWITCH_ANN:
1229 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1230 elem_parse_failed = true;
1233 elems->ext_chansw_ie = (void *)pos;
1235 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1236 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1237 elem_parse_failed = true;
1240 elems->sec_chan_offs = (void *)pos;
1242 case WLAN_EID_CHAN_SWITCH_PARAM:
1244 sizeof(*elems->mesh_chansw_params_ie)) {
1245 elem_parse_failed = true;
1248 elems->mesh_chansw_params_ie = (void *)pos;
1250 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1251 if (!params->action ||
1252 elen < sizeof(*elems->wide_bw_chansw_ie)) {
1253 elem_parse_failed = true;
1256 elems->wide_bw_chansw_ie = (void *)pos;
1258 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1259 if (params->action) {
1260 elem_parse_failed = true;
1264 * This is a bit tricky, but as we only care about
1265 * the wide bandwidth channel switch element, so
1266 * just parse it out manually.
1268 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1271 if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
1272 elems->wide_bw_chansw_ie =
1275 elem_parse_failed = true;
1278 case WLAN_EID_COUNTRY:
1279 elems->country_elem = pos;
1280 elems->country_elem_len = elen;
1282 case WLAN_EID_PWR_CONSTRAINT:
1284 elem_parse_failed = true;
1287 elems->pwr_constr_elem = pos;
1289 case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1290 /* Lots of different options exist, but we only care
1291 * about the Dynamic Transmit Power Control element.
1292 * First check for the Cisco OUI, then for the DTPC
1296 elem_parse_failed = true;
1300 if (pos[0] != 0x00 || pos[1] != 0x40 ||
1301 pos[2] != 0x96 || pos[3] != 0x00)
1305 elem_parse_failed = true;
1310 crc = crc32_be(crc, pos - 2, elen + 2);
1312 elems->cisco_dtpc_elem = pos;
1314 case WLAN_EID_ADDBA_EXT:
1315 if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
1316 elem_parse_failed = true;
1319 elems->addba_ext_ie = (void *)pos;
1321 case WLAN_EID_TIMEOUT_INTERVAL:
1322 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1323 elems->timeout_int = (void *)pos;
1325 elem_parse_failed = true;
1327 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1328 if (elen >= sizeof(*elems->max_idle_period_ie))
1329 elems->max_idle_period_ie = (void *)pos;
1333 elems->rsnx_len = elen;
1335 case WLAN_EID_TX_POWER_ENVELOPE:
1337 elen > sizeof(struct ieee80211_tx_pwr_env))
1340 if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
1343 elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
1344 elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
1345 elems->tx_pwr_env_num++;
1347 case WLAN_EID_EXTENSION:
1348 ieee80211_parse_extension_element(calc_crc ?
1350 elem, elems, params);
1352 case WLAN_EID_S1G_CAPABILITIES:
1353 if (elen >= sizeof(*elems->s1g_capab))
1354 elems->s1g_capab = (void *)pos;
1356 elem_parse_failed = true;
1358 case WLAN_EID_S1G_OPERATION:
1359 if (elen == sizeof(*elems->s1g_oper))
1360 elems->s1g_oper = (void *)pos;
1362 elem_parse_failed = true;
1364 case WLAN_EID_S1G_BCN_COMPAT:
1365 if (elen == sizeof(*elems->s1g_bcn_compat))
1366 elems->s1g_bcn_compat = (void *)pos;
1368 elem_parse_failed = true;
1370 case WLAN_EID_AID_RESPONSE:
1371 if (elen == sizeof(struct ieee80211_aid_response_ie))
1372 elems->aid_resp = (void *)pos;
1374 elem_parse_failed = true;
1380 if (elem_parse_failed)
1381 elems->parse_error = true;
1383 __set_bit(id, seen_elems);
1386 if (!for_each_element_completed(elem, params->start, params->len))
1387 elems->parse_error = true;
1392 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1393 struct ieee802_11_elems *elems,
1394 struct cfg80211_bss *bss,
1395 u8 *nontransmitted_profile)
1397 const struct element *elem, *sub;
1398 size_t profile_len = 0;
1401 if (!bss || !bss->transmitted_bss)
1404 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1405 if (elem->datalen < 2)
1407 if (elem->data[0] < 1 || elem->data[0] > 8)
1410 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1411 u8 new_bssid[ETH_ALEN];
1414 if (sub->id != 0 || sub->datalen < 4) {
1415 /* not a valid BSS profile */
1419 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1420 sub->data[1] != 2) {
1421 /* The first element of the
1422 * Nontransmitted BSSID Profile is not
1423 * the Nontransmitted BSSID Capability
1429 memset(nontransmitted_profile, 0, len);
1430 profile_len = cfg80211_merge_profile(start, len,
1433 nontransmitted_profile,
1436 /* found a Nontransmitted BSSID Profile */
1437 index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
1438 nontransmitted_profile,
1440 if (!index || index[1] < 1 || index[2] == 0) {
1441 /* Invalid MBSSID Index element */
1445 cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
1449 if (ether_addr_equal(new_bssid, bss->bssid)) {
1451 elems->bssid_index_len = index[1];
1452 elems->bssid_index = (void *)&index[2];
1458 return found ? profile_len : 0;
1461 static void ieee80211_defragment_element(struct ieee802_11_elems *elems,
1462 void **elem_ptr, size_t *len,
1463 size_t total_len, u8 frag_id)
1465 u8 *data = *elem_ptr, *pos, *start;
1466 const struct element *elem;
1469 * Since 'data' points to the data of the element, not the element
1470 * itself, allow 254 in case it was an extended element where the
1471 * extended ID isn't part of the data we see here and thus not part of
1474 if (!data || (*len != 254 && *len != 255))
1477 start = elems->scratch_pos;
1479 if (WARN_ON(*len > (elems->scratch + elems->scratch_len -
1480 elems->scratch_pos)))
1483 memcpy(elems->scratch_pos, data, *len);
1484 elems->scratch_pos += *len;
1488 for_each_element(elem, pos, total_len) {
1489 if (elem->id != frag_id)
1492 if (WARN_ON(elem->datalen >
1493 (elems->scratch + elems->scratch_len -
1494 elems->scratch_pos)))
1497 memcpy(elems->scratch_pos, elem->data, elem->datalen);
1498 elems->scratch_pos += elem->datalen;
1500 *len += elem->datalen;
1506 static void ieee80211_mle_get_sta_prof(struct ieee802_11_elems *elems,
1509 const struct ieee80211_multi_link_elem *ml = elems->multi_link;
1510 size_t ml_len = elems->multi_link_len;
1511 const struct element *sub;
1516 if (le16_get_bits(ml->control, IEEE80211_ML_CONTROL_TYPE) !=
1517 IEEE80211_ML_CONTROL_TYPE_BASIC)
1520 for_each_mle_subelement(sub, (u8 *)ml, ml_len) {
1521 struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
1524 if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
1527 if (!ieee80211_mle_sta_prof_size_ok(sub->data, sub->datalen))
1530 control = le16_to_cpu(prof->control);
1532 if (link_id != u16_get_bits(control,
1533 IEEE80211_MLE_STA_CONTROL_LINK_ID))
1536 if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
1540 elems->sta_prof_len = sub->datalen;
1542 /* the sub element can be fragmented */
1543 ieee80211_defragment_element(elems, (void **)&elems->prof,
1544 &elems->sta_prof_len,
1545 ml_len - (sub->data - (u8 *)ml),
1546 IEEE80211_MLE_SUBELEM_FRAGMENT);
1551 static void ieee80211_mle_parse_link(struct ieee802_11_elems *elems,
1552 struct ieee80211_elems_parse_params *params)
1554 struct ieee80211_mle_per_sta_profile *prof;
1555 struct ieee80211_elems_parse_params sub = {
1556 .action = params->action,
1557 .from_ap = params->from_ap,
1560 const struct element *non_inherit = NULL;
1563 if (params->link_id == -1)
1566 ieee80211_defragment_element(elems, (void **)&elems->multi_link,
1567 &elems->multi_link_len,
1568 elems->total_len - ((u8 *)elems->multi_link -
1572 ieee80211_mle_get_sta_prof(elems, params->link_id);
1578 /* check if we have the 4 bytes for the fixed part in assoc response */
1579 if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) {
1581 elems->sta_prof_len = 0;
1586 * Skip the capability information and the status code that are expected
1587 * as part of the station profile in association response frames. Note
1588 * the -1 is because the 'sta_info_len' is accounted to as part of the
1589 * per-STA profile, but not part of the 'u8 variable[]' portion.
1591 sub.start = prof->variable + prof->sta_info_len - 1 + 4;
1592 end = (const u8 *)prof + elems->sta_prof_len;
1593 sub.len = end - sub.start;
1595 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1596 sub.start, sub.len);
1597 _ieee802_11_parse_elems_full(&sub, elems, non_inherit);
1600 struct ieee802_11_elems *
1601 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
1603 struct ieee802_11_elems *elems;
1604 const struct element *non_inherit = NULL;
1605 u8 *nontransmitted_profile;
1606 int nontransmitted_profile_len = 0;
1607 size_t scratch_len = params->scratch_len ?: 3 * params->len;
1609 elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1612 elems->ie_start = params->start;
1613 elems->total_len = params->len;
1614 elems->scratch_len = scratch_len;
1615 elems->scratch_pos = elems->scratch;
1617 nontransmitted_profile = elems->scratch_pos;
1618 nontransmitted_profile_len =
1619 ieee802_11_find_bssid_profile(params->start, params->len,
1621 nontransmitted_profile);
1622 elems->scratch_pos += nontransmitted_profile_len;
1623 elems->scratch_len -= nontransmitted_profile_len;
1624 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1625 nontransmitted_profile,
1626 nontransmitted_profile_len);
1628 elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
1630 /* Override with nontransmitted profile, if found */
1631 if (nontransmitted_profile_len) {
1632 struct ieee80211_elems_parse_params sub = {
1633 .start = nontransmitted_profile,
1634 .len = nontransmitted_profile_len,
1635 .action = params->action,
1636 .link_id = params->link_id,
1639 _ieee802_11_parse_elems_full(&sub, elems, NULL);
1642 ieee80211_mle_parse_link(elems, params);
1644 if (elems->tim && !elems->parse_error) {
1645 const struct ieee80211_tim_ie *tim_ie = elems->tim;
1647 elems->dtim_period = tim_ie->dtim_period;
1648 elems->dtim_count = tim_ie->dtim_count;
1651 /* Override DTIM period and count if needed */
1652 if (elems->bssid_index &&
1653 elems->bssid_index_len >=
1654 offsetofend(struct ieee80211_bssid_index, dtim_period))
1655 elems->dtim_period = elems->bssid_index->dtim_period;
1657 if (elems->bssid_index &&
1658 elems->bssid_index_len >=
1659 offsetofend(struct ieee80211_bssid_index, dtim_count))
1660 elems->dtim_count = elems->bssid_index->dtim_count;
1665 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1666 struct ieee80211_tx_queue_params
1669 struct ieee80211_chanctx_conf *chanctx_conf;
1670 const struct ieee80211_reg_rule *rrule;
1671 const struct ieee80211_wmm_ac *wmm_ac;
1672 u16 center_freq = 0;
1674 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1675 sdata->vif.type != NL80211_IFTYPE_STATION)
1679 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1681 center_freq = chanctx_conf->def.chan->center_freq;
1688 rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1690 if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1695 if (sdata->vif.type == NL80211_IFTYPE_AP)
1696 wmm_ac = &rrule->wmm_rule.ap[ac];
1698 wmm_ac = &rrule->wmm_rule.client[ac];
1699 qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1700 qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1701 qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1702 qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1706 void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
1707 bool bss_notify, bool enable_qos)
1709 struct ieee80211_sub_if_data *sdata = link->sdata;
1710 struct ieee80211_local *local = sdata->local;
1711 struct ieee80211_tx_queue_params qparam;
1712 struct ieee80211_chanctx_conf *chanctx_conf;
1715 bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1718 if (!local->ops->conf_tx)
1721 if (local->hw.queues < IEEE80211_NUM_ACS)
1724 memset(&qparam, 0, sizeof(qparam));
1727 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
1728 use_11b = (chanctx_conf &&
1729 chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1730 !link->operating_11g_mode;
1733 is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1735 /* Set defaults according to 802.11-2007 Table 7-37 */
1742 /* Confiure old 802.11b/g medium access rules. */
1743 qparam.cw_max = aCWmax;
1744 qparam.cw_min = aCWmin;
1748 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1749 /* Update if QoS is enabled. */
1752 case IEEE80211_AC_BK:
1753 qparam.cw_max = aCWmax;
1754 qparam.cw_min = aCWmin;
1761 /* never happens but let's not leave undefined */
1763 case IEEE80211_AC_BE:
1764 qparam.cw_max = aCWmax;
1765 qparam.cw_min = aCWmin;
1772 case IEEE80211_AC_VI:
1773 qparam.cw_max = aCWmin;
1774 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1778 qparam.txop = 6016/32;
1780 qparam.txop = 3008/32;
1787 case IEEE80211_AC_VO:
1788 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1789 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1793 qparam.txop = 3264/32;
1795 qparam.txop = 1504/32;
1800 ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
1802 qparam.uapsd = false;
1804 link->tx_conf[ac] = qparam;
1805 drv_conf_tx(local, link, ac, &qparam);
1808 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1809 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1810 sdata->vif.type != NL80211_IFTYPE_NAN) {
1811 link->conf->qos = enable_qos;
1813 ieee80211_link_info_change_notify(sdata, link,
1818 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1819 u16 transaction, u16 auth_alg, u16 status,
1820 const u8 *extra, size_t extra_len, const u8 *da,
1821 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1824 struct ieee80211_local *local = sdata->local;
1825 struct sk_buff *skb;
1826 struct ieee80211_mgmt *mgmt;
1827 bool multi_link = sdata->vif.valid_links;
1832 struct ieee80211_multi_link_elem ml;
1833 struct ieee80211_mle_basic_common_info basic;
1835 .id = WLAN_EID_EXTENSION,
1836 .len = sizeof(mle) - 2,
1837 .ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
1838 .ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
1839 .basic.len = sizeof(mle.basic),
1843 memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1845 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1846 skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1847 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
1848 multi_link * sizeof(mle));
1852 skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
1854 mgmt = skb_put_zero(skb, 24 + 6);
1855 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1856 IEEE80211_STYPE_AUTH);
1857 memcpy(mgmt->da, da, ETH_ALEN);
1858 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1859 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1860 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1861 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1862 mgmt->u.auth.status_code = cpu_to_le16(status);
1864 skb_put_data(skb, extra, extra_len);
1866 skb_put_data(skb, &mle, sizeof(mle));
1868 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1869 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1870 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1877 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1879 ieee80211_tx_skb(sdata, skb);
1882 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1883 const u8 *da, const u8 *bssid,
1884 u16 stype, u16 reason,
1885 bool send_frame, u8 *frame_buf)
1887 struct ieee80211_local *local = sdata->local;
1888 struct sk_buff *skb;
1889 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1892 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1893 mgmt->duration = 0; /* initialize only */
1894 mgmt->seq_ctrl = 0; /* initialize only */
1895 memcpy(mgmt->da, da, ETH_ALEN);
1896 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1897 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1898 /* u.deauth.reason_code == u.disassoc.reason_code */
1899 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1902 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1903 IEEE80211_DEAUTH_FRAME_LEN);
1907 skb_reserve(skb, local->hw.extra_tx_headroom);
1910 skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1912 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1913 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1914 IEEE80211_SKB_CB(skb)->flags |=
1915 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1917 ieee80211_tx_skb(sdata, skb);
1921 static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
1923 if ((end - pos) < 5)
1926 *pos++ = WLAN_EID_EXTENSION;
1927 *pos++ = 1 + sizeof(cap);
1928 *pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
1929 memcpy(pos, &cap, sizeof(cap));
1934 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
1935 u8 *buffer, size_t buffer_len,
1936 const u8 *ie, size_t ie_len,
1937 enum nl80211_band band,
1939 struct cfg80211_chan_def *chandef,
1940 size_t *offset, u32 flags)
1942 struct ieee80211_local *local = sdata->local;
1943 struct ieee80211_supported_band *sband;
1944 const struct ieee80211_sta_he_cap *he_cap;
1945 const struct ieee80211_sta_eht_cap *eht_cap;
1946 u8 *pos = buffer, *end = buffer + buffer_len;
1948 int supp_rates_len, i;
1954 bool have_80mhz = false;
1958 sband = local->hw.wiphy->bands[band];
1959 if (WARN_ON_ONCE(!sband))
1962 rate_flags = ieee80211_chandef_rate_flags(chandef);
1963 shift = ieee80211_chandef_get_shift(chandef);
1965 /* For direct scan add S1G IE and consider its override bits */
1966 if (band == NL80211_BAND_S1GHZ) {
1967 if (end - pos < 2 + sizeof(struct ieee80211_s1g_cap))
1969 pos = ieee80211_ie_build_s1g_cap(pos, &sband->s1g_cap);
1974 for (i = 0; i < sband->n_bitrates; i++) {
1975 if ((BIT(i) & rate_mask) == 0)
1976 continue; /* skip rate */
1977 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1980 rates[num_rates++] =
1981 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1985 supp_rates_len = min_t(int, num_rates, 8);
1987 if (end - pos < 2 + supp_rates_len)
1989 *pos++ = WLAN_EID_SUPP_RATES;
1990 *pos++ = supp_rates_len;
1991 memcpy(pos, rates, supp_rates_len);
1992 pos += supp_rates_len;
1994 /* insert "request information" if in custom IEs */
1996 static const u8 before_extrates[] = {
1998 WLAN_EID_SUPP_RATES,
2001 noffset = ieee80211_ie_split(ie, ie_len,
2003 ARRAY_SIZE(before_extrates),
2005 if (end - pos < noffset - *offset)
2007 memcpy(pos, ie + *offset, noffset - *offset);
2008 pos += noffset - *offset;
2012 ext_rates_len = num_rates - supp_rates_len;
2013 if (ext_rates_len > 0) {
2014 if (end - pos < 2 + ext_rates_len)
2016 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2017 *pos++ = ext_rates_len;
2018 memcpy(pos, rates + supp_rates_len, ext_rates_len);
2019 pos += ext_rates_len;
2022 if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
2025 *pos++ = WLAN_EID_DS_PARAMS;
2027 *pos++ = ieee80211_frequency_to_channel(
2028 chandef->chan->center_freq);
2031 if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
2034 /* insert custom IEs that go before HT */
2036 static const u8 before_ht[] = {
2038 * no need to list the ones split off already
2039 * (or generated here)
2042 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2044 noffset = ieee80211_ie_split(ie, ie_len,
2045 before_ht, ARRAY_SIZE(before_ht),
2047 if (end - pos < noffset - *offset)
2049 memcpy(pos, ie + *offset, noffset - *offset);
2050 pos += noffset - *offset;
2054 if (sband->ht_cap.ht_supported) {
2055 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
2057 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
2061 /* insert custom IEs that go before VHT */
2063 static const u8 before_vht[] = {
2065 * no need to list the ones split off already
2066 * (or generated here)
2068 WLAN_EID_BSS_COEX_2040,
2069 WLAN_EID_EXT_CAPABILITY,
2071 WLAN_EID_CHANNEL_USAGE,
2072 WLAN_EID_INTERWORKING,
2074 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
2076 noffset = ieee80211_ie_split(ie, ie_len,
2077 before_vht, ARRAY_SIZE(before_vht),
2079 if (end - pos < noffset - *offset)
2081 memcpy(pos, ie + *offset, noffset - *offset);
2082 pos += noffset - *offset;
2086 /* Check if any channel in this sband supports at least 80 MHz */
2087 for (i = 0; i < sband->n_channels; i++) {
2088 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
2089 IEEE80211_CHAN_NO_80MHZ))
2096 if (sband->vht_cap.vht_supported && have_80mhz) {
2097 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
2099 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
2100 sband->vht_cap.cap);
2103 /* insert custom IEs that go before HE */
2105 static const u8 before_he[] = {
2107 * no need to list the ones split off before VHT
2110 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
2112 /* TODO: add 11ah/11aj/11ak elements */
2114 noffset = ieee80211_ie_split(ie, ie_len,
2115 before_he, ARRAY_SIZE(before_he),
2117 if (end - pos < noffset - *offset)
2119 memcpy(pos, ie + *offset, noffset - *offset);
2120 pos += noffset - *offset;
2124 he_cap = ieee80211_get_he_iftype_cap(sband,
2125 ieee80211_vif_type_p2p(&sdata->vif));
2127 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2128 IEEE80211_CHAN_NO_HE)) {
2129 pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
2134 eht_cap = ieee80211_get_eht_iftype_cap(sband,
2135 ieee80211_vif_type_p2p(&sdata->vif));
2138 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2139 IEEE80211_CHAN_NO_HE |
2140 IEEE80211_CHAN_NO_EHT)) {
2141 pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
2142 sdata->vif.type == NL80211_IFTYPE_AP);
2147 if (cfg80211_any_usable_channels(local->hw.wiphy,
2148 BIT(NL80211_BAND_6GHZ),
2149 IEEE80211_CHAN_NO_HE)) {
2150 struct ieee80211_supported_band *sband6;
2152 sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2153 he_cap = ieee80211_get_he_iftype_cap(sband6,
2154 ieee80211_vif_type_p2p(&sdata->vif));
2157 enum nl80211_iftype iftype =
2158 ieee80211_vif_type_p2p(&sdata->vif);
2159 __le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
2161 pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
2166 * If adding more here, adjust code in main.c
2167 * that calculates local->scan_ies_len.
2170 return pos - buffer;
2172 WARN_ONCE(1, "not enough space for preq IEs\n");
2174 return pos - buffer;
2177 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
2179 struct ieee80211_scan_ies *ie_desc,
2180 const u8 *ie, size_t ie_len,
2181 u8 bands_used, u32 *rate_masks,
2182 struct cfg80211_chan_def *chandef,
2185 size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
2188 memset(ie_desc, 0, sizeof(*ie_desc));
2190 for (i = 0; i < NUM_NL80211_BANDS; i++) {
2191 if (bands_used & BIT(i)) {
2192 pos += ieee80211_build_preq_ies_band(sdata,
2200 ie_desc->ies[i] = buffer + old_pos;
2201 ie_desc->len[i] = pos - old_pos;
2206 /* add any remaining custom IEs */
2208 if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
2209 "not enough space for preq custom IEs\n"))
2211 memcpy(buffer + pos, ie + custom_ie_offset,
2212 ie_len - custom_ie_offset);
2213 ie_desc->common_ies = buffer + pos;
2214 ie_desc->common_ie_len = ie_len - custom_ie_offset;
2215 pos += ie_len - custom_ie_offset;
2221 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
2222 const u8 *src, const u8 *dst,
2224 struct ieee80211_channel *chan,
2225 const u8 *ssid, size_t ssid_len,
2226 const u8 *ie, size_t ie_len,
2229 struct ieee80211_local *local = sdata->local;
2230 struct cfg80211_chan_def chandef;
2231 struct sk_buff *skb;
2232 struct ieee80211_mgmt *mgmt;
2234 u32 rate_masks[NUM_NL80211_BANDS] = {};
2235 struct ieee80211_scan_ies dummy_ie_desc;
2238 * Do not send DS Channel parameter for directed probe requests
2239 * in order to maximize the chance that we get a response. Some
2240 * badly-behaved APs don't respond when this parameter is included.
2242 chandef.width = sdata->vif.bss_conf.chandef.width;
2243 if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2244 chandef.chan = NULL;
2246 chandef.chan = chan;
2248 skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
2249 local->scan_ies_len + ie_len);
2253 rate_masks[chan->band] = ratemask;
2254 ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
2255 skb_tailroom(skb), &dummy_ie_desc,
2256 ie, ie_len, BIT(chan->band),
2257 rate_masks, &chandef, flags);
2258 skb_put(skb, ies_len);
2261 mgmt = (struct ieee80211_mgmt *) skb->data;
2262 memcpy(mgmt->da, dst, ETH_ALEN);
2263 memcpy(mgmt->bssid, dst, ETH_ALEN);
2266 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2271 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
2272 struct ieee802_11_elems *elems,
2273 enum nl80211_band band, u32 *basic_rates)
2275 struct ieee80211_supported_band *sband;
2277 u32 supp_rates, rate_flags;
2280 sband = sdata->local->hw.wiphy->bands[band];
2281 if (WARN_ON(!sband))
2284 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2285 shift = ieee80211_vif_get_shift(&sdata->vif);
2287 num_rates = sband->n_bitrates;
2289 for (i = 0; i < elems->supp_rates_len +
2290 elems->ext_supp_rates_len; i++) {
2294 if (i < elems->supp_rates_len)
2295 rate = elems->supp_rates[i];
2296 else if (elems->ext_supp_rates)
2297 rate = elems->ext_supp_rates
2298 [i - elems->supp_rates_len];
2299 own_rate = 5 * (rate & 0x7f);
2300 is_basic = !!(rate & 0x80);
2302 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2305 for (j = 0; j < num_rates; j++) {
2307 if ((rate_flags & sband->bitrates[j].flags)
2311 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
2314 if (brate == own_rate) {
2315 supp_rates |= BIT(j);
2316 if (basic_rates && is_basic)
2317 *basic_rates |= BIT(j);
2324 void ieee80211_stop_device(struct ieee80211_local *local)
2326 ieee80211_led_radio(local, false);
2327 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
2329 cancel_work_sync(&local->reconfig_filter);
2331 flush_workqueue(local->workqueue);
2335 static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
2338 /* It's possible that we don't handle the scan completion in
2339 * time during suspend, so if it's still marked as completed
2340 * here, queue the work and flush it to clean things up.
2341 * Instead of calling the worker function directly here, we
2342 * really queue it to avoid potential races with other flows
2343 * scheduling the same work.
2345 if (test_bit(SCAN_COMPLETED, &local->scanning)) {
2346 /* If coming from reconfiguration failure, abort the scan so
2347 * we don't attempt to continue a partial HW scan - which is
2348 * possible otherwise if (e.g.) the 2.4 GHz portion was the
2349 * completed scan, and a 5 GHz portion is still pending.
2352 set_bit(SCAN_ABORTED, &local->scanning);
2353 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
2354 flush_delayed_work(&local->scan_work);
2358 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2360 struct ieee80211_sub_if_data *sdata;
2361 struct ieee80211_chanctx *ctx;
2364 * We get here if during resume the device can't be restarted properly.
2365 * We might also get here if this happens during HW reset, which is a
2366 * slightly different situation and we need to drop all connections in
2369 * Ask cfg80211 to turn off all interfaces, this will result in more
2370 * warnings but at least we'll then get into a clean stopped state.
2373 local->resuming = false;
2374 local->suspended = false;
2375 local->in_reconfig = false;
2377 ieee80211_flush_completed_scan(local, true);
2379 /* scheduled scan clearly can't be running any more, but tell
2380 * cfg80211 and clear local state
2382 ieee80211_sched_scan_end(local);
2384 list_for_each_entry(sdata, &local->interfaces, list)
2385 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2387 /* Mark channel contexts as not being in the driver any more to avoid
2388 * removing them from the driver during the shutdown process...
2390 mutex_lock(&local->chanctx_mtx);
2391 list_for_each_entry(ctx, &local->chanctx_list, list)
2392 ctx->driver_present = false;
2393 mutex_unlock(&local->chanctx_mtx);
2396 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2397 struct ieee80211_sub_if_data *sdata,
2398 struct ieee80211_link_data *link)
2400 struct ieee80211_chanctx_conf *conf;
2401 struct ieee80211_chanctx *ctx;
2403 if (!local->use_chanctx)
2406 mutex_lock(&local->chanctx_mtx);
2407 conf = rcu_dereference_protected(link->conf->chanctx_conf,
2408 lockdep_is_held(&local->chanctx_mtx));
2410 ctx = container_of(conf, struct ieee80211_chanctx, conf);
2411 drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
2413 mutex_unlock(&local->chanctx_mtx);
2416 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
2418 struct ieee80211_local *local = sdata->local;
2419 struct sta_info *sta;
2422 mutex_lock(&local->sta_mtx);
2423 list_for_each_entry(sta, &local->sta_list, list) {
2424 enum ieee80211_sta_state state;
2426 if (!sta->uploaded || sta->sdata != sdata)
2429 for (state = IEEE80211_STA_NOTEXIST;
2430 state < sta->sta_state; state++)
2431 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2434 mutex_unlock(&local->sta_mtx);
2437 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2439 struct cfg80211_nan_func *func, **funcs;
2442 res = drv_start_nan(sdata->local, sdata,
2443 &sdata->u.nan.conf);
2447 funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
2453 /* Add all the functions:
2454 * This is a little bit ugly. We need to call a potentially sleeping
2455 * callback for each NAN function, so we can't hold the spinlock.
2457 spin_lock_bh(&sdata->u.nan.func_lock);
2459 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2462 spin_unlock_bh(&sdata->u.nan.func_lock);
2464 for (i = 0; funcs[i]; i++) {
2465 res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2467 ieee80211_nan_func_terminated(&sdata->vif,
2468 funcs[i]->instance_id,
2469 NL80211_NAN_FUNC_TERM_REASON_ERROR,
2478 int ieee80211_reconfig(struct ieee80211_local *local)
2480 struct ieee80211_hw *hw = &local->hw;
2481 struct ieee80211_sub_if_data *sdata;
2482 struct ieee80211_chanctx *ctx;
2483 struct sta_info *sta;
2485 bool reconfig_due_to_wowlan = false;
2486 struct ieee80211_sub_if_data *sched_scan_sdata;
2487 struct cfg80211_sched_scan_request *sched_scan_req;
2488 bool sched_scan_stopped = false;
2489 bool suspended = local->suspended;
2490 bool in_reconfig = false;
2492 /* nothing to do if HW shouldn't run */
2493 if (!local->open_count)
2498 local->resuming = true;
2500 if (local->wowlan) {
2502 * In the wowlan case, both mac80211 and the device
2503 * are functional when the resume op is called, so
2504 * clear local->suspended so the device could operate
2505 * normally (e.g. pass rx frames).
2507 local->suspended = false;
2508 res = drv_resume(local);
2509 local->wowlan = false;
2511 local->resuming = false;
2518 * res is 1, which means the driver requested
2519 * to go through a regular reset on wakeup.
2520 * restore local->suspended in this case.
2522 reconfig_due_to_wowlan = true;
2523 local->suspended = true;
2528 * In case of hw_restart during suspend (without wowlan),
2529 * cancel restart work, as we are reconfiguring the device
2531 * Note that restart_work is scheduled on a frozen workqueue,
2532 * so we can't deadlock in this case.
2534 if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
2535 cancel_work_sync(&local->restart_work);
2537 local->started = false;
2540 * Upon resume hardware can sometimes be goofy due to
2541 * various platform / driver / bus issues, so restarting
2542 * the device may at times not work immediately. Propagate
2545 res = drv_start(local);
2548 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2550 WARN(1, "Hardware became unavailable during restart.\n");
2551 ieee80211_handle_reconfig_failure(local);
2555 /* setup fragmentation threshold */
2556 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
2558 /* setup RTS threshold */
2559 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
2561 /* reset coverage class */
2562 drv_set_coverage_class(local, hw->wiphy->coverage_class);
2564 ieee80211_led_radio(local, true);
2565 ieee80211_mod_tpt_led_trig(local,
2566 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2568 /* add interfaces */
2569 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
2571 /* in HW restart it exists already */
2572 WARN_ON(local->resuming);
2573 res = drv_add_interface(local, sdata);
2575 RCU_INIT_POINTER(local->monitor_sdata, NULL);
2581 list_for_each_entry(sdata, &local->interfaces, list) {
2582 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2583 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2584 ieee80211_sdata_running(sdata)) {
2585 res = drv_add_interface(local, sdata);
2591 /* If adding any of the interfaces failed above, roll back and
2595 list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2597 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2598 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2599 ieee80211_sdata_running(sdata))
2600 drv_remove_interface(local, sdata);
2601 ieee80211_handle_reconfig_failure(local);
2605 /* add channel contexts */
2606 if (local->use_chanctx) {
2607 mutex_lock(&local->chanctx_mtx);
2608 list_for_each_entry(ctx, &local->chanctx_list, list)
2609 if (ctx->replace_state !=
2610 IEEE80211_CHANCTX_REPLACES_OTHER)
2611 WARN_ON(drv_add_chanctx(local, ctx));
2612 mutex_unlock(&local->chanctx_mtx);
2614 sdata = wiphy_dereference(local->hw.wiphy,
2615 local->monitor_sdata);
2616 if (sdata && ieee80211_sdata_running(sdata))
2617 ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
2620 /* reconfigure hardware */
2621 ieee80211_hw_config(local, ~0);
2623 ieee80211_configure_filter(local);
2625 /* Finally also reconfigure all the BSS information */
2626 list_for_each_entry(sdata, &local->interfaces, list) {
2627 unsigned int link_id;
2630 if (!ieee80211_sdata_running(sdata))
2635 link_id < ARRAY_SIZE(sdata->vif.link_conf);
2637 struct ieee80211_link_data *link;
2639 link = sdata_dereference(sdata->link[link_id], sdata);
2641 ieee80211_assign_chanctx(local, sdata, link);
2644 switch (sdata->vif.type) {
2645 case NL80211_IFTYPE_AP_VLAN:
2646 case NL80211_IFTYPE_MONITOR:
2648 case NL80211_IFTYPE_ADHOC:
2649 if (sdata->vif.cfg.ibss_joined)
2650 WARN_ON(drv_join_ibss(local, sdata));
2653 ieee80211_reconfig_stations(sdata);
2655 case NL80211_IFTYPE_AP: /* AP stations are handled later */
2656 for (i = 0; i < IEEE80211_NUM_ACS; i++)
2657 drv_conf_tx(local, &sdata->deflink, i,
2658 &sdata->deflink.tx_conf[i]);
2661 sdata_unlock(sdata);
2663 /* common change flags for all interface types */
2664 changed = BSS_CHANGED_ERP_CTS_PROT |
2665 BSS_CHANGED_ERP_PREAMBLE |
2666 BSS_CHANGED_ERP_SLOT |
2668 BSS_CHANGED_BASIC_RATES |
2669 BSS_CHANGED_BEACON_INT |
2674 BSS_CHANGED_TXPOWER |
2675 BSS_CHANGED_MCAST_RATE;
2677 if (sdata->vif.bss_conf.mu_mimo_owner)
2678 changed |= BSS_CHANGED_MU_GROUPS;
2680 switch (sdata->vif.type) {
2681 case NL80211_IFTYPE_STATION:
2682 changed |= BSS_CHANGED_ASSOC |
2683 BSS_CHANGED_ARP_FILTER |
2686 /* Re-send beacon info report to the driver */
2687 if (sdata->deflink.u.mgd.have_beacon)
2688 changed |= BSS_CHANGED_BEACON_INFO;
2690 if (sdata->vif.bss_conf.max_idle_period ||
2691 sdata->vif.bss_conf.protected_keep_alive)
2692 changed |= BSS_CHANGED_KEEP_ALIVE;
2695 ieee80211_bss_info_change_notify(sdata, changed);
2696 sdata_unlock(sdata);
2698 case NL80211_IFTYPE_OCB:
2699 changed |= BSS_CHANGED_OCB;
2700 ieee80211_bss_info_change_notify(sdata, changed);
2702 case NL80211_IFTYPE_ADHOC:
2703 changed |= BSS_CHANGED_IBSS;
2705 case NL80211_IFTYPE_AP:
2706 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2708 if (sdata->vif.bss_conf.ftm_responder == 1 &&
2709 wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2710 NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2711 changed |= BSS_CHANGED_FTM_RESPONDER;
2713 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2714 changed |= BSS_CHANGED_AP_PROBE_RESP;
2716 if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2717 drv_start_ap(local, sdata,
2718 sdata->deflink.conf);
2721 case NL80211_IFTYPE_MESH_POINT:
2722 if (sdata->vif.bss_conf.enable_beacon) {
2723 changed |= BSS_CHANGED_BEACON |
2724 BSS_CHANGED_BEACON_ENABLED;
2725 ieee80211_bss_info_change_notify(sdata, changed);
2728 case NL80211_IFTYPE_NAN:
2729 res = ieee80211_reconfig_nan(sdata);
2731 ieee80211_handle_reconfig_failure(local);
2735 case NL80211_IFTYPE_AP_VLAN:
2736 case NL80211_IFTYPE_MONITOR:
2737 case NL80211_IFTYPE_P2P_DEVICE:
2740 case NL80211_IFTYPE_UNSPECIFIED:
2741 case NUM_NL80211_IFTYPES:
2742 case NL80211_IFTYPE_P2P_CLIENT:
2743 case NL80211_IFTYPE_P2P_GO:
2744 case NL80211_IFTYPE_WDS:
2750 ieee80211_recalc_ps(local);
2753 * The sta might be in psm against the ap (e.g. because
2754 * this was the state before a hw restart), so we
2755 * explicitly send a null packet in order to make sure
2756 * it'll sync against the ap (and get out of psm).
2758 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2759 list_for_each_entry(sdata, &local->interfaces, list) {
2760 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2762 if (!sdata->u.mgd.associated)
2765 ieee80211_send_nullfunc(local, sdata, false);
2769 /* APs are now beaconing, add back stations */
2770 list_for_each_entry(sdata, &local->interfaces, list) {
2771 if (!ieee80211_sdata_running(sdata))
2775 switch (sdata->vif.type) {
2776 case NL80211_IFTYPE_AP_VLAN:
2777 case NL80211_IFTYPE_AP:
2778 ieee80211_reconfig_stations(sdata);
2783 sdata_unlock(sdata);
2787 list_for_each_entry(sdata, &local->interfaces, list)
2788 ieee80211_reenable_keys(sdata);
2790 /* Reconfigure sched scan if it was interrupted by FW restart */
2791 mutex_lock(&local->mtx);
2792 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2793 lockdep_is_held(&local->mtx));
2794 sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2795 lockdep_is_held(&local->mtx));
2796 if (sched_scan_sdata && sched_scan_req)
2798 * Sched scan stopped, but we don't want to report it. Instead,
2799 * we're trying to reschedule. However, if more than one scan
2800 * plan was set, we cannot reschedule since we don't know which
2801 * scan plan was currently running (and some scan plans may have
2802 * already finished).
2804 if (sched_scan_req->n_scan_plans > 1 ||
2805 __ieee80211_request_sched_scan_start(sched_scan_sdata,
2807 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2808 RCU_INIT_POINTER(local->sched_scan_req, NULL);
2809 sched_scan_stopped = true;
2811 mutex_unlock(&local->mtx);
2813 if (sched_scan_stopped)
2814 cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2818 if (local->monitors == local->open_count && local->monitors > 0)
2819 ieee80211_add_virtual_monitor(local);
2822 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2823 * sessions can be established after a resume.
2825 * Also tear down aggregation sessions since reconfiguring
2826 * them in a hardware restart scenario is not easily done
2827 * right now, and the hardware will have lost information
2828 * about the sessions, but we and the AP still think they
2829 * are active. This is really a workaround though.
2831 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2832 mutex_lock(&local->sta_mtx);
2834 list_for_each_entry(sta, &local->sta_list, list) {
2835 if (!local->resuming)
2836 ieee80211_sta_tear_down_BA_sessions(
2837 sta, AGG_STOP_LOCAL_REQUEST);
2838 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2841 mutex_unlock(&local->sta_mtx);
2845 * If this is for hw restart things are still running.
2846 * We may want to change that later, however.
2848 if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2849 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2851 if (local->in_reconfig) {
2852 in_reconfig = local->in_reconfig;
2853 local->in_reconfig = false;
2856 /* Restart deferred ROCs */
2857 mutex_lock(&local->mtx);
2858 ieee80211_start_next_roc(local);
2859 mutex_unlock(&local->mtx);
2861 /* Requeue all works */
2862 list_for_each_entry(sdata, &local->interfaces, list)
2863 ieee80211_queue_work(&local->hw, &sdata->work);
2866 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2867 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2871 list_for_each_entry(sdata, &local->interfaces, list) {
2872 if (!ieee80211_sdata_running(sdata))
2874 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2875 ieee80211_sta_restart(sdata);
2883 /* first set suspended false, then resuming */
2884 local->suspended = false;
2886 local->resuming = false;
2888 ieee80211_flush_completed_scan(local, false);
2890 if (local->open_count && !reconfig_due_to_wowlan)
2891 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2893 list_for_each_entry(sdata, &local->interfaces, list) {
2894 if (!ieee80211_sdata_running(sdata))
2896 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2897 ieee80211_sta_restart(sdata);
2900 mod_timer(&local->sta_cleanup, jiffies + 1);
2908 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2910 struct ieee80211_sub_if_data *sdata;
2911 struct ieee80211_local *local;
2912 struct ieee80211_key *key;
2917 sdata = vif_to_sdata(vif);
2918 local = sdata->local;
2920 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2924 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2925 !local->in_reconfig))
2928 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2931 sdata->flags |= flag;
2933 mutex_lock(&local->key_mtx);
2934 list_for_each_entry(key, &sdata->key_list, list)
2935 key->flags |= KEY_FLAG_TAINTED;
2936 mutex_unlock(&local->key_mtx);
2939 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
2941 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
2943 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
2945 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2947 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
2949 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2951 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
2952 struct ieee80211_link_data *link)
2954 struct ieee80211_local *local = sdata->local;
2955 struct ieee80211_chanctx_conf *chanctx_conf;
2956 struct ieee80211_chanctx *chanctx;
2958 mutex_lock(&local->chanctx_mtx);
2960 chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
2961 lockdep_is_held(&local->chanctx_mtx));
2964 * This function can be called from a work, thus it may be possible
2965 * that the chanctx_conf is removed (due to a disconnection, for
2967 * So nothing should be done in such case.
2972 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2973 ieee80211_recalc_smps_chanctx(local, chanctx);
2975 mutex_unlock(&local->chanctx_mtx);
2978 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
2981 struct ieee80211_local *local = sdata->local;
2982 struct ieee80211_chanctx_conf *chanctx_conf;
2983 struct ieee80211_chanctx *chanctx;
2986 mutex_lock(&local->chanctx_mtx);
2988 for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
2989 struct ieee80211_bss_conf *bss_conf;
2991 if (link_id >= 0 && link_id != i)
2995 bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
3001 chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
3002 lockdep_is_held(&local->chanctx_mtx));
3004 * Since we hold the chanctx_mtx (checked above)
3005 * we can take the chanctx_conf pointer out of the
3006 * RCU critical section, it cannot go away without
3007 * the mutex. Just the way we reached it could - in
3008 * theory - go away, but we don't really care and
3009 * it really shouldn't happen anyway.
3016 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
3018 ieee80211_recalc_chanctx_min_def(local, chanctx);
3021 mutex_unlock(&local->chanctx_mtx);
3024 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
3026 size_t pos = offset;
3028 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
3029 pos += 2 + ies[pos + 1];
3034 u8 *ieee80211_ie_build_s1g_cap(u8 *pos, struct ieee80211_sta_s1g_cap *s1g_cap)
3036 *pos++ = WLAN_EID_S1G_CAPABILITIES;
3037 *pos++ = sizeof(struct ieee80211_s1g_cap);
3038 memset(pos, 0, sizeof(struct ieee80211_s1g_cap));
3040 memcpy(pos, &s1g_cap->cap, sizeof(s1g_cap->cap));
3041 pos += sizeof(s1g_cap->cap);
3043 memcpy(pos, &s1g_cap->nss_mcs, sizeof(s1g_cap->nss_mcs));
3044 pos += sizeof(s1g_cap->nss_mcs);
3049 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3054 *pos++ = WLAN_EID_HT_CAPABILITY;
3055 *pos++ = sizeof(struct ieee80211_ht_cap);
3056 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
3058 /* capability flags */
3059 tmp = cpu_to_le16(cap);
3060 memcpy(pos, &tmp, sizeof(u16));
3063 /* AMPDU parameters */
3064 *pos++ = ht_cap->ampdu_factor |
3065 (ht_cap->ampdu_density <<
3066 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
3069 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
3070 pos += sizeof(ht_cap->mcs);
3072 /* extended capabilities */
3073 pos += sizeof(__le16);
3075 /* BF capabilities */
3076 pos += sizeof(__le32);
3078 /* antenna selection */
3084 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3089 *pos++ = WLAN_EID_VHT_CAPABILITY;
3090 *pos++ = sizeof(struct ieee80211_vht_cap);
3091 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
3093 /* capability flags */
3094 tmp = cpu_to_le32(cap);
3095 memcpy(pos, &tmp, sizeof(u32));
3099 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
3100 pos += sizeof(vht_cap->vht_mcs);
3105 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
3107 const struct ieee80211_sta_he_cap *he_cap;
3108 struct ieee80211_supported_band *sband;
3111 sband = ieee80211_get_sband(sdata);
3115 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3119 n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
3121 sizeof(he_cap->he_cap_elem) + n +
3122 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3123 he_cap->he_cap_elem.phy_cap_info);
3126 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
3127 const struct ieee80211_sta_he_cap *he_cap,
3130 struct ieee80211_he_cap_elem elem;
3135 /* Make sure we have place for the IE */
3137 * TODO: the 1 added is because this temporarily is under the EXTENSION
3138 * IE. Get rid of it when it moves.
3143 /* modify on stack first to calculate 'n' and 'ie_len' correctly */
3144 elem = he_cap->he_cap_elem;
3146 if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
3147 elem.phy_cap_info[0] &=
3148 ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3149 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
3151 if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
3152 elem.phy_cap_info[0] &=
3153 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3155 if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
3156 elem.phy_cap_info[0] &=
3157 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3159 n = ieee80211_he_mcs_nss_size(&elem);
3161 sizeof(he_cap->he_cap_elem) + n +
3162 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3163 he_cap->he_cap_elem.phy_cap_info);
3165 if ((end - pos) < ie_len)
3168 *pos++ = WLAN_EID_EXTENSION;
3169 pos++; /* We'll set the size later below */
3170 *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
3173 memcpy(pos, &elem, sizeof(elem));
3174 pos += sizeof(elem);
3176 memcpy(pos, &he_cap->he_mcs_nss_supp, n);
3179 /* Check if PPE Threshold should be present */
3180 if ((he_cap->he_cap_elem.phy_cap_info[6] &
3181 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
3185 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
3186 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
3188 n = hweight8(he_cap->ppe_thres[0] &
3189 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
3190 n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
3191 IEEE80211_PPE_THRES_NSS_POS));
3194 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
3197 n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
3198 n = DIV_ROUND_UP(n, 8);
3200 /* Copy PPE Thresholds */
3201 memcpy(pos, &he_cap->ppe_thres, n);
3205 orig_pos[1] = (pos - orig_pos) - 2;
3209 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
3210 enum ieee80211_smps_mode smps_mode,
3211 struct sk_buff *skb)
3213 struct ieee80211_supported_band *sband;
3214 const struct ieee80211_sband_iftype_data *iftd;
3215 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3219 if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
3220 BIT(NL80211_BAND_6GHZ),
3221 IEEE80211_CHAN_NO_HE))
3224 sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3226 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
3230 /* Check for device HE 6 GHz capability before adding element */
3231 if (!iftd->he_6ghz_capa.capa)
3234 cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
3235 cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
3237 switch (smps_mode) {
3238 case IEEE80211_SMPS_AUTOMATIC:
3239 case IEEE80211_SMPS_NUM_MODES:
3242 case IEEE80211_SMPS_OFF:
3243 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
3244 IEEE80211_HE_6GHZ_CAP_SM_PS);
3246 case IEEE80211_SMPS_STATIC:
3247 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
3248 IEEE80211_HE_6GHZ_CAP_SM_PS);
3250 case IEEE80211_SMPS_DYNAMIC:
3251 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
3252 IEEE80211_HE_6GHZ_CAP_SM_PS);
3256 pos = skb_put(skb, 2 + 1 + sizeof(cap));
3257 ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
3258 pos + 2 + 1 + sizeof(cap));
3261 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3262 const struct cfg80211_chan_def *chandef,
3263 u16 prot_mode, bool rifs_mode)
3265 struct ieee80211_ht_operation *ht_oper;
3266 /* Build HT Information */
3267 *pos++ = WLAN_EID_HT_OPERATION;
3268 *pos++ = sizeof(struct ieee80211_ht_operation);
3269 ht_oper = (struct ieee80211_ht_operation *)pos;
3270 ht_oper->primary_chan = ieee80211_frequency_to_channel(
3271 chandef->chan->center_freq);
3272 switch (chandef->width) {
3273 case NL80211_CHAN_WIDTH_160:
3274 case NL80211_CHAN_WIDTH_80P80:
3275 case NL80211_CHAN_WIDTH_80:
3276 case NL80211_CHAN_WIDTH_40:
3277 if (chandef->center_freq1 > chandef->chan->center_freq)
3278 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3280 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3282 case NL80211_CHAN_WIDTH_320:
3283 /* HT information element should not be included on 6GHz */
3287 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
3290 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
3291 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
3292 chandef->width != NL80211_CHAN_WIDTH_20)
3293 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
3296 ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
3298 ht_oper->operation_mode = cpu_to_le16(prot_mode);
3299 ht_oper->stbc_param = 0x0000;
3301 /* It seems that Basic MCS set and Supported MCS set
3302 are identical for the first 10 bytes */
3303 memset(&ht_oper->basic_set, 0, 16);
3304 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
3306 return pos + sizeof(struct ieee80211_ht_operation);
3309 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
3310 const struct cfg80211_chan_def *chandef)
3312 *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
3313 *pos++ = 3; /* IE length */
3314 /* New channel width */
3315 switch (chandef->width) {
3316 case NL80211_CHAN_WIDTH_80:
3317 *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
3319 case NL80211_CHAN_WIDTH_160:
3320 *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
3322 case NL80211_CHAN_WIDTH_80P80:
3323 *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
3325 case NL80211_CHAN_WIDTH_320:
3326 /* The behavior is not defined for 320 MHz channels */
3330 *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
3333 /* new center frequency segment 0 */
3334 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
3335 /* new center frequency segment 1 */
3336 if (chandef->center_freq2)
3337 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
3342 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3343 const struct cfg80211_chan_def *chandef)
3345 struct ieee80211_vht_operation *vht_oper;
3347 *pos++ = WLAN_EID_VHT_OPERATION;
3348 *pos++ = sizeof(struct ieee80211_vht_operation);
3349 vht_oper = (struct ieee80211_vht_operation *)pos;
3350 vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
3351 chandef->center_freq1);
3352 if (chandef->center_freq2)
3353 vht_oper->center_freq_seg1_idx =
3354 ieee80211_frequency_to_channel(chandef->center_freq2);
3356 vht_oper->center_freq_seg1_idx = 0x00;
3358 switch (chandef->width) {
3359 case NL80211_CHAN_WIDTH_160:
3361 * Convert 160 MHz channel width to new style as interop
3364 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3365 vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
3366 if (chandef->chan->center_freq < chandef->center_freq1)
3367 vht_oper->center_freq_seg0_idx -= 8;
3369 vht_oper->center_freq_seg0_idx += 8;
3371 case NL80211_CHAN_WIDTH_80P80:
3373 * Convert 80+80 MHz channel width to new style as interop
3376 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3378 case NL80211_CHAN_WIDTH_80:
3379 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3381 case NL80211_CHAN_WIDTH_320:
3382 /* VHT information element should not be included on 6GHz */
3386 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
3390 /* don't require special VHT peer rates */
3391 vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
3393 return pos + sizeof(struct ieee80211_vht_operation);
3396 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
3398 struct ieee80211_he_operation *he_oper;
3399 struct ieee80211_he_6ghz_oper *he_6ghz_op;
3401 u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
3403 if (chandef->chan->band == NL80211_BAND_6GHZ)
3404 ie_len += sizeof(struct ieee80211_he_6ghz_oper);
3406 *pos++ = WLAN_EID_EXTENSION;
3408 *pos++ = WLAN_EID_EXT_HE_OPERATION;
3411 he_oper_params |= u32_encode_bits(1023, /* disabled */
3412 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3413 he_oper_params |= u32_encode_bits(1,
3414 IEEE80211_HE_OPERATION_ER_SU_DISABLE);
3415 he_oper_params |= u32_encode_bits(1,
3416 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3417 if (chandef->chan->band == NL80211_BAND_6GHZ)
3418 he_oper_params |= u32_encode_bits(1,
3419 IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
3421 he_oper = (struct ieee80211_he_operation *)pos;
3422 he_oper->he_oper_params = cpu_to_le32(he_oper_params);
3424 /* don't require special HE peer rates */
3425 he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
3426 pos += sizeof(struct ieee80211_he_operation);
3428 if (chandef->chan->band != NL80211_BAND_6GHZ)
3431 /* TODO add VHT operational */
3432 he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
3433 he_6ghz_op->minrate = 6; /* 6 Mbps */
3434 he_6ghz_op->primary =
3435 ieee80211_frequency_to_channel(chandef->chan->center_freq);
3437 ieee80211_frequency_to_channel(chandef->center_freq1);
3438 if (chandef->center_freq2)
3440 ieee80211_frequency_to_channel(chandef->center_freq2);
3442 he_6ghz_op->ccfs1 = 0;
3444 switch (chandef->width) {
3445 case NL80211_CHAN_WIDTH_320:
3447 * TODO: mesh operation is not defined over 6GHz 320 MHz
3452 case NL80211_CHAN_WIDTH_160:
3453 /* Convert 160 MHz channel width to new style as interop
3456 he_6ghz_op->control =
3457 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3458 he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
3459 if (chandef->chan->center_freq < chandef->center_freq1)
3460 he_6ghz_op->ccfs0 -= 8;
3462 he_6ghz_op->ccfs0 += 8;
3464 case NL80211_CHAN_WIDTH_80P80:
3465 he_6ghz_op->control =
3466 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3468 case NL80211_CHAN_WIDTH_80:
3469 he_6ghz_op->control =
3470 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
3472 case NL80211_CHAN_WIDTH_40:
3473 he_6ghz_op->control =
3474 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3477 he_6ghz_op->control =
3478 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3482 pos += sizeof(struct ieee80211_he_6ghz_oper);
3488 u8 *ieee80211_ie_build_eht_oper(u8 *pos, struct cfg80211_chan_def *chandef,
3489 const struct ieee80211_sta_eht_cap *eht_cap)
3492 const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
3493 &eht_cap->eht_mcs_nss_supp.only_20mhz;
3494 struct ieee80211_eht_operation *eht_oper;
3495 struct ieee80211_eht_operation_info *eht_oper_info;
3496 u8 eht_oper_len = offsetof(struct ieee80211_eht_operation, optional);
3497 u8 eht_oper_info_len =
3498 offsetof(struct ieee80211_eht_operation_info, optional);
3501 *pos++ = WLAN_EID_EXTENSION;
3502 *pos++ = 1 + eht_oper_len + eht_oper_info_len;
3503 *pos++ = WLAN_EID_EXT_EHT_OPERATION;
3505 eht_oper = (struct ieee80211_eht_operation *)pos;
3507 memcpy(&eht_oper->basic_mcs_nss, eht_mcs_nss, sizeof(*eht_mcs_nss));
3508 eht_oper->params |= IEEE80211_EHT_OPER_INFO_PRESENT;
3509 pos += eht_oper_len;
3512 (struct ieee80211_eht_operation_info *)eht_oper->optional;
3514 eht_oper_info->ccfs0 =
3515 ieee80211_frequency_to_channel(chandef->center_freq1);
3516 if (chandef->center_freq2)
3517 eht_oper_info->ccfs1 =
3518 ieee80211_frequency_to_channel(chandef->center_freq2);
3520 eht_oper_info->ccfs1 = 0;
3522 switch (chandef->width) {
3523 case NL80211_CHAN_WIDTH_320:
3524 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ;
3525 eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3526 if (chandef->chan->center_freq < chandef->center_freq1)
3527 eht_oper_info->ccfs0 -= 16;
3529 eht_oper_info->ccfs0 += 16;
3531 case NL80211_CHAN_WIDTH_160:
3532 eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3533 if (chandef->chan->center_freq < chandef->center_freq1)
3534 eht_oper_info->ccfs0 -= 8;
3536 eht_oper_info->ccfs0 += 8;
3538 case NL80211_CHAN_WIDTH_80P80:
3539 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ;
3541 case NL80211_CHAN_WIDTH_80:
3542 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ;
3544 case NL80211_CHAN_WIDTH_40:
3545 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ;
3548 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ;
3551 eht_oper_info->control = chan_width;
3552 pos += eht_oper_info_len;
3554 /* TODO: eht_oper_info->optional */
3559 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3560 struct cfg80211_chan_def *chandef)
3562 enum nl80211_channel_type channel_type;
3567 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3568 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3569 channel_type = NL80211_CHAN_HT20;
3571 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3572 channel_type = NL80211_CHAN_HT40PLUS;
3574 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3575 channel_type = NL80211_CHAN_HT40MINUS;
3581 cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3585 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3586 const struct ieee80211_vht_operation *oper,
3587 const struct ieee80211_ht_operation *htop,
3588 struct cfg80211_chan_def *chandef)
3590 struct cfg80211_chan_def new = *chandef;
3592 int ccfs0, ccfs1, ccfs2;
3595 bool support_80_80 = false;
3596 bool support_160 = false;
3597 u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3598 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3599 u8 supp_chwidth = u32_get_bits(vht_cap_info,
3600 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3605 vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3606 support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3607 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3608 support_80_80 = ((vht_cap &
3609 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3610 (vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3611 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3612 ((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3613 IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3614 ccfs0 = oper->center_freq_seg0_idx;
3615 ccfs1 = oper->center_freq_seg1_idx;
3616 ccfs2 = (le16_to_cpu(htop->operation_mode) &
3617 IEEE80211_HT_OP_MODE_CCFS2_MASK)
3618 >> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3622 /* if not supported, parse as though we didn't understand it */
3623 if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3624 ext_nss_bw_supp = 0;
3627 * Cf. IEEE 802.11 Table 9-250
3629 * We really just consider that because it's inefficient to connect
3630 * at a higher bandwidth than we'll actually be able to use.
3632 switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3636 support_160 = false;
3637 support_80_80 = false;
3640 support_80_80 = false;
3663 cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3664 cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3666 switch (oper->chan_width) {
3667 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3668 /* just use HT information directly */
3670 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3671 new.width = NL80211_CHAN_WIDTH_80;
3672 new.center_freq1 = cf0;
3673 /* If needed, adjust based on the newer interop workaround. */
3677 diff = abs(ccf1 - ccf0);
3678 if ((diff == 8) && support_160) {
3679 new.width = NL80211_CHAN_WIDTH_160;
3680 new.center_freq1 = cf1;
3681 } else if ((diff > 8) && support_80_80) {
3682 new.width = NL80211_CHAN_WIDTH_80P80;
3683 new.center_freq2 = cf1;
3687 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3688 /* deprecated encoding */
3689 new.width = NL80211_CHAN_WIDTH_160;
3690 new.center_freq1 = cf0;
3692 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3693 /* deprecated encoding */
3694 new.width = NL80211_CHAN_WIDTH_80P80;
3695 new.center_freq1 = cf0;
3696 new.center_freq2 = cf1;
3702 if (!cfg80211_chandef_valid(&new))
3709 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
3710 bool support_160, bool support_320,
3711 struct cfg80211_chan_def *chandef)
3713 struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
3715 chandef->center_freq1 =
3716 ieee80211_channel_to_frequency(info->ccfs0,
3717 chandef->chan->band);
3719 switch (u8_get_bits(info->control,
3720 IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3721 case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3722 chandef->width = NL80211_CHAN_WIDTH_20;
3724 case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3725 chandef->width = NL80211_CHAN_WIDTH_40;
3727 case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3728 chandef->width = NL80211_CHAN_WIDTH_80;
3730 case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3732 chandef->width = NL80211_CHAN_WIDTH_160;
3733 chandef->center_freq1 =
3734 ieee80211_channel_to_frequency(info->ccfs1,
3735 chandef->chan->band);
3737 chandef->width = NL80211_CHAN_WIDTH_80;
3740 case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3742 chandef->width = NL80211_CHAN_WIDTH_320;
3743 chandef->center_freq1 =
3744 ieee80211_channel_to_frequency(info->ccfs1,
3745 chandef->chan->band);
3746 } else if (support_160) {
3747 chandef->width = NL80211_CHAN_WIDTH_160;
3749 chandef->width = NL80211_CHAN_WIDTH_80;
3751 if (chandef->center_freq1 > chandef->chan->center_freq)
3752 chandef->center_freq1 -= 40;
3754 chandef->center_freq1 += 40;
3760 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
3761 const struct ieee80211_he_operation *he_oper,
3762 const struct ieee80211_eht_operation *eht_oper,
3763 struct cfg80211_chan_def *chandef)
3765 struct ieee80211_local *local = sdata->local;
3766 struct ieee80211_supported_band *sband;
3767 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3768 const struct ieee80211_sta_he_cap *he_cap;
3769 const struct ieee80211_sta_eht_cap *eht_cap;
3770 struct cfg80211_chan_def he_chandef = *chandef;
3771 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3772 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3773 bool support_80_80, support_160, support_320;
3774 u8 he_phy_cap, eht_phy_cap;
3777 if (chandef->chan->band != NL80211_BAND_6GHZ)
3780 sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3782 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3784 sdata_info(sdata, "Missing iftype sband data/HE cap");
3788 he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
3791 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3794 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3798 "HE is not advertised on (on %d MHz), expect issues\n",
3799 chandef->chan->center_freq);
3803 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
3805 sdata_info(sdata, "Missing iftype sband data/EHT cap");
3809 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3811 if (!he_6ghz_oper) {
3813 "HE 6GHz operation missing (on %d MHz), expect issues\n",
3814 chandef->chan->center_freq);
3819 * The EHT operation IE does not contain the primary channel so the
3820 * primary channel frequency should be taken from the 6 GHz operation
3823 freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3825 he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
3827 switch (u8_get_bits(he_6ghz_oper->control,
3828 IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
3829 case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
3830 bss_conf->power_type = IEEE80211_REG_LPI_AP;
3832 case IEEE80211_6GHZ_CTRL_REG_SP_AP:
3833 bss_conf->power_type = IEEE80211_REG_SP_AP;
3836 bss_conf->power_type = IEEE80211_REG_UNSET_AP;
3841 !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3842 switch (u8_get_bits(he_6ghz_oper->control,
3843 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3844 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3845 he_chandef.width = NL80211_CHAN_WIDTH_20;
3847 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3848 he_chandef.width = NL80211_CHAN_WIDTH_40;
3850 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3851 he_chandef.width = NL80211_CHAN_WIDTH_80;
3853 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3854 he_chandef.width = NL80211_CHAN_WIDTH_80;
3855 if (!he_6ghz_oper->ccfs1)
3857 if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
3859 he_chandef.width = NL80211_CHAN_WIDTH_160;
3862 he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3867 if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3868 he_chandef.center_freq1 =
3869 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3872 he_chandef.center_freq1 =
3873 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3875 if (support_80_80 || support_160)
3876 he_chandef.center_freq2 =
3877 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3881 eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
3883 eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
3885 ieee80211_chandef_eht_oper(eht_oper, support_160,
3886 support_320, &he_chandef);
3889 if (!cfg80211_chandef_valid(&he_chandef)) {
3891 "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
3892 he_chandef.chan ? he_chandef.chan->center_freq : 0,
3894 he_chandef.center_freq1,
3895 he_chandef.center_freq2);
3899 *chandef = he_chandef;
3904 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
3905 struct cfg80211_chan_def *chandef)
3912 switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3913 case IEEE80211_S1G_CHANWIDTH_1MHZ:
3914 chandef->width = NL80211_CHAN_WIDTH_1;
3916 case IEEE80211_S1G_CHANWIDTH_2MHZ:
3917 chandef->width = NL80211_CHAN_WIDTH_2;
3919 case IEEE80211_S1G_CHANWIDTH_4MHZ:
3920 chandef->width = NL80211_CHAN_WIDTH_4;
3922 case IEEE80211_S1G_CHANWIDTH_8MHZ:
3923 chandef->width = NL80211_CHAN_WIDTH_8;
3925 case IEEE80211_S1G_CHANWIDTH_16MHZ:
3926 chandef->width = NL80211_CHAN_WIDTH_16;
3932 oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
3933 NL80211_BAND_S1GHZ);
3934 chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
3935 chandef->freq1_offset = oper_freq % 1000;
3940 int ieee80211_parse_bitrates(enum nl80211_chan_width width,
3941 const struct ieee80211_supported_band *sband,
3942 const u8 *srates, int srates_len, u32 *rates)
3944 u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
3945 int shift = ieee80211_chanwidth_get_shift(width);
3946 struct ieee80211_rate *br;
3947 int brate, rate, i, j, count = 0;
3951 for (i = 0; i < srates_len; i++) {
3952 rate = srates[i] & 0x7f;
3954 for (j = 0; j < sband->n_bitrates; j++) {
3955 br = &sband->bitrates[j];
3956 if ((rate_flags & br->flags) != rate_flags)
3959 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3960 if (brate == rate) {
3970 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
3971 struct sk_buff *skb, bool need_basic,
3972 enum nl80211_band band)
3974 struct ieee80211_local *local = sdata->local;
3975 struct ieee80211_supported_band *sband;
3978 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3981 shift = ieee80211_vif_get_shift(&sdata->vif);
3982 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3983 sband = local->hw.wiphy->bands[band];
3985 for (i = 0; i < sband->n_bitrates; i++) {
3986 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3993 if (skb_tailroom(skb) < rates + 2)
3996 pos = skb_put(skb, rates + 2);
3997 *pos++ = WLAN_EID_SUPP_RATES;
3999 for (i = 0; i < rates; i++) {
4001 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4004 if (need_basic && basic_rates & BIT(i))
4006 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4008 *pos++ = basic | (u8) rate;
4014 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
4015 struct sk_buff *skb, bool need_basic,
4016 enum nl80211_band band)
4018 struct ieee80211_local *local = sdata->local;
4019 struct ieee80211_supported_band *sband;
4021 u8 i, exrates, *pos;
4022 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
4025 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
4026 shift = ieee80211_vif_get_shift(&sdata->vif);
4028 sband = local->hw.wiphy->bands[band];
4030 for (i = 0; i < sband->n_bitrates; i++) {
4031 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
4041 if (skb_tailroom(skb) < exrates + 2)
4045 pos = skb_put(skb, exrates + 2);
4046 *pos++ = WLAN_EID_EXT_SUPP_RATES;
4048 for (i = 8; i < sband->n_bitrates; i++) {
4050 if ((rate_flags & sband->bitrates[i].flags)
4053 if (need_basic && basic_rates & BIT(i))
4055 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4057 *pos++ = basic | (u8) rate;
4063 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
4065 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4067 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
4070 return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
4072 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
4074 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
4079 /* TODO: consider rx_highest */
4081 if (mcs->rx_mask[3])
4083 if (mcs->rx_mask[2])
4085 if (mcs->rx_mask[1])
4091 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
4092 * @local: mac80211 hw info struct
4093 * @status: RX status
4094 * @mpdu_len: total MPDU length (including FCS)
4095 * @mpdu_offset: offset into MPDU to calculate timestamp at
4097 * This function calculates the RX timestamp at the given MPDU offset, taking
4098 * into account what the RX timestamp was. An offset of 0 will just normalize
4099 * the timestamp to TSF at beginning of MPDU reception.
4101 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
4102 struct ieee80211_rx_status *status,
4103 unsigned int mpdu_len,
4104 unsigned int mpdu_offset)
4106 u64 ts = status->mactime;
4107 struct rate_info ri;
4111 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
4114 memset(&ri, 0, sizeof(ri));
4118 /* Fill cfg80211 rate info */
4119 switch (status->encoding) {
4121 ri.flags |= RATE_INFO_FLAGS_EHT_MCS;
4122 ri.mcs = status->rate_idx;
4123 ri.nss = status->nss;
4124 ri.eht_ru_alloc = status->eht.ru;
4125 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4126 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4127 /* TODO/FIXME: is this right? handle other PPDUs */
4128 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4134 ri.flags |= RATE_INFO_FLAGS_HE_MCS;
4135 ri.mcs = status->rate_idx;
4136 ri.nss = status->nss;
4137 ri.he_ru_alloc = status->he_ru;
4138 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4139 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4142 * See P802.11ax_D6.0, section 27.3.4 for
4145 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4151 * For HE MU PPDU, add the HE-SIG-B.
4152 * For HE ER PPDU, add 8us for the HE-SIG-A.
4153 * For HE TB PPDU, add 4us for the HE-STF.
4154 * Add the HE-LTF durations - variable.
4160 ri.mcs = status->rate_idx;
4161 ri.flags |= RATE_INFO_FLAGS_MCS;
4162 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4163 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4166 * See P802.11REVmd_D3.0, section 19.3.2 for
4169 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4171 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
4177 * Add Data HT-LTFs per streams
4178 * TODO: add Extension HT-LTFs, 4us per LTF
4180 n_ltf = ((ri.mcs >> 3) & 3) + 1;
4181 n_ltf = n_ltf == 3 ? 4 : n_ltf;
4187 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
4188 ri.mcs = status->rate_idx;
4189 ri.nss = status->nss;
4190 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4191 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4194 * See P802.11REVmd_D3.0, section 21.3.2 for
4197 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4202 * Add VHT-LTFs per streams
4204 n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
4205 ri.nss + 1 : ri.nss;
4213 case RX_ENC_LEGACY: {
4214 struct ieee80211_supported_band *sband;
4218 switch (status->bw) {
4219 case RATE_INFO_BW_10:
4222 case RATE_INFO_BW_5:
4227 sband = local->hw.wiphy->bands[status->band];
4228 bitrate = sband->bitrates[status->rate_idx].bitrate;
4229 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
4231 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4232 if (status->band == NL80211_BAND_5GHZ) {
4235 } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
4245 rate = cfg80211_calculate_bitrate(&ri);
4246 if (WARN_ONCE(!rate,
4247 "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
4248 (unsigned long long)status->flag, status->rate_idx,
4252 /* rewind from end of MPDU */
4253 if (status->flag & RX_FLAG_MACTIME_END)
4254 ts -= mpdu_len * 8 * 10 / rate;
4256 ts += mpdu_offset * 8 * 10 / rate;
4261 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
4263 struct ieee80211_sub_if_data *sdata;
4264 struct cfg80211_chan_def chandef;
4266 /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
4267 lockdep_assert_wiphy(local->hw.wiphy);
4269 mutex_lock(&local->mtx);
4270 list_for_each_entry(sdata, &local->interfaces, list) {
4271 /* it might be waiting for the local->mtx, but then
4272 * by the time it gets it, sdata->wdev.cac_started
4273 * will no longer be true
4275 cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
4277 if (sdata->wdev.cac_started) {
4278 chandef = sdata->vif.bss_conf.chandef;
4279 ieee80211_link_release_channel(&sdata->deflink);
4280 cfg80211_cac_event(sdata->dev,
4282 NL80211_RADAR_CAC_ABORTED,
4286 mutex_unlock(&local->mtx);
4289 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
4291 struct ieee80211_local *local =
4292 container_of(work, struct ieee80211_local, radar_detected_work);
4293 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
4294 struct ieee80211_chanctx *ctx;
4295 int num_chanctx = 0;
4297 mutex_lock(&local->chanctx_mtx);
4298 list_for_each_entry(ctx, &local->chanctx_list, list) {
4299 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
4303 chandef = ctx->conf.def;
4305 mutex_unlock(&local->chanctx_mtx);
4307 wiphy_lock(local->hw.wiphy);
4308 ieee80211_dfs_cac_cancel(local);
4309 wiphy_unlock(local->hw.wiphy);
4311 if (num_chanctx > 1)
4312 /* XXX: multi-channel is not supported yet */
4315 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
4318 void ieee80211_radar_detected(struct ieee80211_hw *hw)
4320 struct ieee80211_local *local = hw_to_local(hw);
4322 trace_api_radar_detected(local);
4324 schedule_work(&local->radar_detected_work);
4326 EXPORT_SYMBOL(ieee80211_radar_detected);
4328 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
4330 ieee80211_conn_flags_t ret;
4334 case NL80211_CHAN_WIDTH_20:
4335 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4336 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4338 case NL80211_CHAN_WIDTH_40:
4339 c->width = NL80211_CHAN_WIDTH_20;
4340 c->center_freq1 = c->chan->center_freq;
4341 ret = IEEE80211_CONN_DISABLE_40MHZ |
4342 IEEE80211_CONN_DISABLE_VHT;
4344 case NL80211_CHAN_WIDTH_80:
4345 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
4349 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
4350 c->width = NL80211_CHAN_WIDTH_40;
4351 ret = IEEE80211_CONN_DISABLE_VHT;
4353 case NL80211_CHAN_WIDTH_80P80:
4354 c->center_freq2 = 0;
4355 c->width = NL80211_CHAN_WIDTH_80;
4356 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4357 IEEE80211_CONN_DISABLE_160MHZ;
4359 case NL80211_CHAN_WIDTH_160:
4361 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
4364 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
4365 c->width = NL80211_CHAN_WIDTH_80;
4366 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4367 IEEE80211_CONN_DISABLE_160MHZ;
4369 case NL80211_CHAN_WIDTH_320:
4371 tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
4374 c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
4375 c->width = NL80211_CHAN_WIDTH_160;
4376 ret = IEEE80211_CONN_DISABLE_320MHZ;
4379 case NL80211_CHAN_WIDTH_20_NOHT:
4381 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4382 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4384 case NL80211_CHAN_WIDTH_1:
4385 case NL80211_CHAN_WIDTH_2:
4386 case NL80211_CHAN_WIDTH_4:
4387 case NL80211_CHAN_WIDTH_8:
4388 case NL80211_CHAN_WIDTH_16:
4389 case NL80211_CHAN_WIDTH_5:
4390 case NL80211_CHAN_WIDTH_10:
4393 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4397 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
4403 * Returns true if smps_mode_new is strictly more restrictive than
4406 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
4407 enum ieee80211_smps_mode smps_mode_new)
4409 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
4410 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
4413 switch (smps_mode_old) {
4414 case IEEE80211_SMPS_STATIC:
4416 case IEEE80211_SMPS_DYNAMIC:
4417 return smps_mode_new == IEEE80211_SMPS_STATIC;
4418 case IEEE80211_SMPS_OFF:
4419 return smps_mode_new != IEEE80211_SMPS_OFF;
4427 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
4428 struct cfg80211_csa_settings *csa_settings)
4430 struct sk_buff *skb;
4431 struct ieee80211_mgmt *mgmt;
4432 struct ieee80211_local *local = sdata->local;
4434 int hdr_len = offsetofend(struct ieee80211_mgmt,
4435 u.action.u.chan_switch);
4438 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4439 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4442 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
4443 5 + /* channel switch announcement element */
4444 3 + /* secondary channel offset element */
4445 5 + /* wide bandwidth channel switch announcement */
4446 8); /* mesh channel switch parameters element */
4450 skb_reserve(skb, local->tx_headroom);
4451 mgmt = skb_put_zero(skb, hdr_len);
4452 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4453 IEEE80211_STYPE_ACTION);
4455 eth_broadcast_addr(mgmt->da);
4456 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
4457 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4458 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
4460 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4461 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
4463 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
4464 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
4465 pos = skb_put(skb, 5);
4466 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
4467 *pos++ = 3; /* IE length */
4468 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
4469 freq = csa_settings->chandef.chan->center_freq;
4470 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
4471 *pos++ = csa_settings->count; /* count */
4473 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
4474 enum nl80211_channel_type ch_type;
4477 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
4478 *pos++ = 1; /* IE length */
4479 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
4480 if (ch_type == NL80211_CHAN_HT40PLUS)
4481 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
4483 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
4486 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4487 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4490 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
4491 *pos++ = 6; /* IE length */
4492 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
4493 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
4494 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
4495 *pos++ |= csa_settings->block_tx ?
4496 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
4497 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
4499 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
4503 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
4504 csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
4505 csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
4507 ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
4510 ieee80211_tx_skb(sdata, skb);
4515 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
4517 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
4524 if (data->count[i] == 1)
4527 if (data->desc[i].interval == 0)
4530 /* End time is in the past, check for repetitions */
4531 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
4532 if (data->count[i] < 255) {
4533 if (data->count[i] <= skip) {
4538 data->count[i] -= skip;
4541 data->desc[i].start += skip * data->desc[i].interval;
4547 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
4553 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4556 if (!data->count[i])
4559 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
4562 cur = data->desc[i].start - tsf;
4566 cur = data->desc[i].start + data->desc[i].duration - tsf;
4575 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
4580 * arbitrary limit, used to avoid infinite loops when combined NoA
4581 * descriptors cover the full time period.
4585 ieee80211_extend_absent_time(data, tsf, &offset);
4587 if (!ieee80211_extend_absent_time(data, tsf, &offset))
4591 } while (tries < max_tries);
4596 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4598 u32 next_offset = BIT(31) - 1;
4602 data->has_next_tsf = false;
4603 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4606 if (!data->count[i])
4609 ieee80211_extend_noa_desc(data, tsf, i);
4610 start = data->desc[i].start - tsf;
4612 data->absent |= BIT(i);
4614 if (next_offset > start)
4615 next_offset = start;
4617 data->has_next_tsf = true;
4621 next_offset = ieee80211_get_noa_absent_time(data, tsf);
4623 data->next_tsf = tsf + next_offset;
4625 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4627 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4628 struct ieee80211_noa_data *data, u32 tsf)
4633 memset(data, 0, sizeof(*data));
4635 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4636 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4638 if (!desc->count || !desc->duration)
4641 data->count[i] = desc->count;
4642 data->desc[i].start = le32_to_cpu(desc->start_time);
4643 data->desc[i].duration = le32_to_cpu(desc->duration);
4644 data->desc[i].interval = le32_to_cpu(desc->interval);
4646 if (data->count[i] > 1 &&
4647 data->desc[i].interval < data->desc[i].duration)
4650 ieee80211_extend_noa_desc(data, tsf, i);
4655 ieee80211_update_p2p_noa(data, tsf);
4659 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4661 void ieee80211_recalc_dtim(struct ieee80211_local *local,
4662 struct ieee80211_sub_if_data *sdata)
4664 u64 tsf = drv_get_tsf(local, sdata);
4666 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4667 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4671 if (tsf == -1ULL || !beacon_int || !dtim_period)
4674 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4675 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4679 ps = &sdata->bss->ps;
4680 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4681 ps = &sdata->u.mesh.ps;
4687 * actually finds last dtim_count, mac80211 will update in
4688 * __beacon_add_tim().
4689 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4691 do_div(tsf, beacon_int);
4692 bcns_from_dtim = do_div(tsf, dtim_period);
4693 /* just had a DTIM */
4694 if (!bcns_from_dtim)
4697 dtim_count = dtim_period - bcns_from_dtim;
4699 ps->dtim_count = dtim_count;
4702 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4703 struct ieee80211_chanctx *ctx)
4705 struct ieee80211_link_data *link;
4706 u8 radar_detect = 0;
4708 lockdep_assert_held(&local->chanctx_mtx);
4710 if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4713 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
4714 if (link->reserved_radar_required)
4715 radar_detect |= BIT(link->reserved_chandef.width);
4718 * An in-place reservation context should not have any assigned vifs
4719 * until it replaces the other context.
4721 WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
4722 !list_empty(&ctx->assigned_links));
4724 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
4725 if (!link->radar_required)
4729 BIT(link->conf->chandef.width);
4732 return radar_detect;
4735 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4736 const struct cfg80211_chan_def *chandef,
4737 enum ieee80211_chanctx_mode chanmode,
4740 struct ieee80211_local *local = sdata->local;
4741 struct ieee80211_sub_if_data *sdata_iter;
4742 enum nl80211_iftype iftype = sdata->wdev.iftype;
4743 struct ieee80211_chanctx *ctx;
4745 struct iface_combination_params params = {
4746 .radar_detect = radar_detect,
4749 lockdep_assert_held(&local->chanctx_mtx);
4751 if (WARN_ON(hweight32(radar_detect) > 1))
4754 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4758 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4761 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4762 sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4764 * always passing this is harmless, since it'll be the
4765 * same value that cfg80211 finds if it finds the same
4766 * interface ... and that's always allowed
4768 params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4771 /* Always allow software iftypes */
4772 if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4779 params.num_different_channels = 1;
4781 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4782 params.iftype_num[iftype] = 1;
4784 list_for_each_entry(ctx, &local->chanctx_list, list) {
4785 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4787 params.radar_detect |=
4788 ieee80211_chanctx_radar_detect(local, ctx);
4789 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
4790 params.num_different_channels++;
4793 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4794 cfg80211_chandef_compatible(chandef,
4797 params.num_different_channels++;
4800 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
4801 struct wireless_dev *wdev_iter;
4803 wdev_iter = &sdata_iter->wdev;
4805 if (sdata_iter == sdata ||
4806 !ieee80211_sdata_running(sdata_iter) ||
4807 cfg80211_iftype_allowed(local->hw.wiphy,
4808 wdev_iter->iftype, 0, 1))
4811 params.iftype_num[wdev_iter->iftype]++;
4815 if (total == 1 && !params.radar_detect)
4818 return cfg80211_check_combinations(local->hw.wiphy, ¶ms);
4822 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4825 u32 *max_num_different_channels = data;
4827 *max_num_different_channels = max(*max_num_different_channels,
4828 c->num_different_channels);
4831 int ieee80211_max_num_channels(struct ieee80211_local *local)
4833 struct ieee80211_sub_if_data *sdata;
4834 struct ieee80211_chanctx *ctx;
4835 u32 max_num_different_channels = 1;
4837 struct iface_combination_params params = {0};
4839 lockdep_assert_held(&local->chanctx_mtx);
4841 list_for_each_entry(ctx, &local->chanctx_list, list) {
4842 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4845 params.num_different_channels++;
4847 params.radar_detect |=
4848 ieee80211_chanctx_radar_detect(local, ctx);
4851 list_for_each_entry_rcu(sdata, &local->interfaces, list)
4852 params.iftype_num[sdata->wdev.iftype]++;
4854 err = cfg80211_iter_combinations(local->hw.wiphy, ¶ms,
4855 ieee80211_iter_max_chans,
4856 &max_num_different_channels);
4860 return max_num_different_channels;
4863 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4864 struct ieee80211_sta_s1g_cap *caps,
4865 struct sk_buff *skb)
4867 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4868 struct ieee80211_s1g_cap s1g_capab;
4872 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4878 memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4879 memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4881 /* override the capability info */
4882 for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4883 u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4885 s1g_capab.capab_info[i] &= ~mask;
4886 s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4889 /* then MCS and NSS set */
4890 for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4891 u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4893 s1g_capab.supp_mcs_nss[i] &= ~mask;
4894 s1g_capab.supp_mcs_nss[i] |=
4895 ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4898 pos = skb_put(skb, 2 + sizeof(s1g_capab));
4899 *pos++ = WLAN_EID_S1G_CAPABILITIES;
4900 *pos++ = sizeof(s1g_capab);
4902 memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4905 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4906 struct sk_buff *skb)
4908 u8 *pos = skb_put(skb, 3);
4910 *pos++ = WLAN_EID_AID_REQUEST;
4915 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4917 *buf++ = WLAN_EID_VENDOR_SPECIFIC;
4918 *buf++ = 7; /* len */
4919 *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4922 *buf++ = 2; /* WME */
4923 *buf++ = 0; /* WME info */
4924 *buf++ = 1; /* WME ver */
4925 *buf++ = qosinfo; /* U-APSD no in use */
4930 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4931 unsigned long *frame_cnt,
4932 unsigned long *byte_cnt)
4934 struct txq_info *txqi = to_txq_info(txq);
4935 u32 frag_cnt = 0, frag_bytes = 0;
4936 struct sk_buff *skb;
4938 skb_queue_walk(&txqi->frags, skb) {
4940 frag_bytes += skb->len;
4944 *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4947 *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4949 EXPORT_SYMBOL(ieee80211_txq_get_depth);
4951 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4952 IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4953 IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4954 IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4955 IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4958 u16 ieee80211_encode_usf(int listen_interval)
4960 static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
4963 /* find greatest USF */
4964 while (usf < IEEE80211_MAX_USF) {
4965 if (listen_interval % listen_int_usf[usf + 1])
4969 ui = listen_interval / listen_int_usf[usf];
4971 /* error if there is a remainder. Should've been checked by user */
4972 WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
4973 listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
4974 FIELD_PREP(LISTEN_INT_UI, ui);
4976 return (u16) listen_interval;
4979 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
4981 const struct ieee80211_sta_he_cap *he_cap;
4982 const struct ieee80211_sta_eht_cap *eht_cap;
4983 struct ieee80211_supported_band *sband;
4987 sband = ieee80211_get_sband(sdata);
4991 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
4992 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
4993 if (!he_cap || !eht_cap)
4996 is_ap = iftype == NL80211_IFTYPE_AP ||
4997 iftype == NL80211_IFTYPE_P2P_GO;
4999 n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
5000 &eht_cap->eht_cap_elem,
5003 sizeof(eht_cap->eht_cap_elem) + n +
5004 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
5005 eht_cap->eht_cap_elem.phy_cap_info);
5009 u8 *ieee80211_ie_build_eht_cap(u8 *pos,
5010 const struct ieee80211_sta_he_cap *he_cap,
5011 const struct ieee80211_sta_eht_cap *eht_cap,
5015 u8 mcs_nss_len, ppet_len;
5019 /* Make sure we have place for the IE */
5020 if (!he_cap || !eht_cap)
5023 mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
5024 &eht_cap->eht_cap_elem,
5026 ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
5027 eht_cap->eht_cap_elem.phy_cap_info);
5029 ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
5030 if ((end - pos) < ie_len)
5033 *pos++ = WLAN_EID_EXTENSION;
5034 *pos++ = ie_len - 2;
5035 *pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
5038 memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
5039 pos += sizeof(eht_cap->eht_cap_elem);
5041 memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
5045 memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
5052 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos)
5054 unsigned int elem_len;
5059 elem_len = skb->data + skb->len - len_pos - 1;
5061 while (elem_len > 255) {
5062 /* this one is 255 */
5064 /* remaining data gets smaller */
5066 /* make space for the fragment ID/len in SKB */
5068 /* shift back the remaining data to place fragment ID/len */
5069 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
5070 /* place the fragment ID */
5072 *len_pos = WLAN_EID_FRAGMENT;
5073 /* and point to fragment length to update later */
5077 *len_pos = elem_len;