1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright (C) 2015-2017 Intel Deutschland GmbH
9 * Copyright (C) 2018-2022 Intel Corporation
11 * utilities for mac80211
14 #include <net/mac80211.h>
15 #include <linux/netdevice.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/etherdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/bitmap.h>
23 #include <linux/crc32.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/rtnetlink.h>
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
36 /* privid for wiphys to determine whether they belong to us or not */
37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
41 struct ieee80211_local *local;
43 local = wiphy_priv(wiphy);
46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
49 enum nl80211_iftype type)
51 __le16 fc = hdr->frame_control;
53 if (ieee80211_is_data(fc)) {
54 if (len < 24) /* drop incorrect hdr len (data) */
57 if (ieee80211_has_a4(fc))
59 if (ieee80211_has_tods(fc))
61 if (ieee80211_has_fromds(fc))
67 if (ieee80211_is_s1g_beacon(fc)) {
68 struct ieee80211_ext *ext = (void *) hdr;
70 return ext->u.s1g_beacon.sa;
73 if (ieee80211_is_mgmt(fc)) {
74 if (len < 24) /* drop incorrect hdr len (mgmt) */
79 if (ieee80211_is_ctl(fc)) {
80 if (ieee80211_is_pspoll(fc))
83 if (ieee80211_is_back_req(fc)) {
85 case NL80211_IFTYPE_STATION:
87 case NL80211_IFTYPE_AP:
88 case NL80211_IFTYPE_AP_VLAN:
91 break; /* fall through to the return */
98 EXPORT_SYMBOL(ieee80211_get_bssid);
100 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
103 struct ieee80211_hdr *hdr;
105 skb_queue_walk(&tx->skbs, skb) {
106 hdr = (struct ieee80211_hdr *) skb->data;
107 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
111 int ieee80211_frame_duration(enum nl80211_band band, size_t len,
112 int rate, int erp, int short_preamble,
117 /* calculate duration (in microseconds, rounded up to next higher
118 * integer if it includes a fractional microsecond) to send frame of
119 * len bytes (does not include FCS) at the given rate. Duration will
122 * rate is in 100 kbps, so divident is multiplied by 10 in the
123 * DIV_ROUND_UP() operations.
125 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
126 * is assumed to be 0 otherwise.
129 if (band == NL80211_BAND_5GHZ || erp) {
133 * N_DBPS = DATARATE x 4
134 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
135 * (16 = SIGNAL time, 6 = tail bits)
136 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
139 * 802.11a - 18.5.2: aSIFSTime = 16 usec
140 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
141 * signal ext = 6 usec
143 dur = 16; /* SIFS + signal ext */
144 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
145 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
147 /* IEEE 802.11-2012 18.3.2.4: all values above are:
148 * * times 4 for 5 MHz
149 * * times 2 for 10 MHz
153 /* rates should already consider the channel bandwidth,
154 * don't apply divisor again.
156 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
157 4 * rate); /* T_SYM x N_SYM */
160 * 802.11b or 802.11g with 802.11b compatibility:
161 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
162 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
164 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
165 * aSIFSTime = 10 usec
166 * aPreambleLength = 144 usec or 72 usec with short preamble
167 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
169 dur = 10; /* aSIFSTime = 10 usec */
170 dur += short_preamble ? (72 + 24) : (144 + 48);
172 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
178 /* Exported duration function for driver use */
179 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
180 struct ieee80211_vif *vif,
181 enum nl80211_band band,
183 struct ieee80211_rate *rate)
185 struct ieee80211_sub_if_data *sdata;
188 bool short_preamble = false;
192 sdata = vif_to_sdata(vif);
193 short_preamble = sdata->vif.bss_conf.use_short_preamble;
194 if (sdata->deflink.operating_11g_mode)
195 erp = rate->flags & IEEE80211_RATE_ERP_G;
196 shift = ieee80211_vif_get_shift(vif);
199 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
200 short_preamble, shift);
202 return cpu_to_le16(dur);
204 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
206 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
207 struct ieee80211_vif *vif, size_t frame_len,
208 const struct ieee80211_tx_info *frame_txctl)
210 struct ieee80211_local *local = hw_to_local(hw);
211 struct ieee80211_rate *rate;
212 struct ieee80211_sub_if_data *sdata;
214 int erp, shift = 0, bitrate;
216 struct ieee80211_supported_band *sband;
218 sband = local->hw.wiphy->bands[frame_txctl->band];
220 short_preamble = false;
222 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
226 sdata = vif_to_sdata(vif);
227 short_preamble = sdata->vif.bss_conf.use_short_preamble;
228 if (sdata->deflink.operating_11g_mode)
229 erp = rate->flags & IEEE80211_RATE_ERP_G;
230 shift = ieee80211_vif_get_shift(vif);
233 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
236 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
237 erp, short_preamble, shift);
238 /* Data frame duration */
239 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
240 erp, short_preamble, shift);
242 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
243 erp, short_preamble, shift);
245 return cpu_to_le16(dur);
247 EXPORT_SYMBOL(ieee80211_rts_duration);
249 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
250 struct ieee80211_vif *vif,
252 const struct ieee80211_tx_info *frame_txctl)
254 struct ieee80211_local *local = hw_to_local(hw);
255 struct ieee80211_rate *rate;
256 struct ieee80211_sub_if_data *sdata;
258 int erp, shift = 0, bitrate;
260 struct ieee80211_supported_band *sband;
262 sband = local->hw.wiphy->bands[frame_txctl->band];
264 short_preamble = false;
266 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
269 sdata = vif_to_sdata(vif);
270 short_preamble = sdata->vif.bss_conf.use_short_preamble;
271 if (sdata->deflink.operating_11g_mode)
272 erp = rate->flags & IEEE80211_RATE_ERP_G;
273 shift = ieee80211_vif_get_shift(vif);
276 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
278 /* Data frame duration */
279 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
280 erp, short_preamble, shift);
281 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
283 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
284 erp, short_preamble, shift);
287 return cpu_to_le16(dur);
289 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
291 static void wake_tx_push_queue(struct ieee80211_local *local,
292 struct ieee80211_sub_if_data *sdata,
293 struct ieee80211_txq *queue)
295 int q = sdata->vif.hw_queue[queue->ac];
296 struct ieee80211_tx_control control = {
304 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
305 q_stopped = local->queue_stop_reasons[q];
306 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
311 skb = ieee80211_tx_dequeue(&local->hw, queue);
315 drv_tx(local, &control, skb);
319 /* wake_tx_queue handler for driver not implementing a custom one*/
320 void ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
321 struct ieee80211_txq *txq)
323 struct ieee80211_local *local = hw_to_local(hw);
324 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
325 struct ieee80211_txq *queue;
327 /* Use ieee80211_next_txq() for airtime fairness accounting */
328 ieee80211_txq_schedule_start(hw, txq->ac);
329 while ((queue = ieee80211_next_txq(hw, txq->ac))) {
330 wake_tx_push_queue(local, sdata, queue);
331 ieee80211_return_txq(hw, queue, false);
333 ieee80211_txq_schedule_end(hw, txq->ac);
335 EXPORT_SYMBOL(ieee80211_handle_wake_tx_queue);
337 static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
339 struct ieee80211_local *local = sdata->local;
340 struct ieee80211_vif *vif = &sdata->vif;
341 struct fq *fq = &local->fq;
342 struct ps_data *ps = NULL;
343 struct txq_info *txqi;
344 struct sta_info *sta;
348 spin_lock(&fq->lock);
350 sdata->vif.txqs_stopped[ac] = false;
352 if (!test_bit(SDATA_STATE_RUNNING, &sdata->state))
355 if (sdata->vif.type == NL80211_IFTYPE_AP)
356 ps = &sdata->bss->ps;
358 list_for_each_entry_rcu(sta, &local->sta_list, list) {
359 if (sdata != sta->sdata)
362 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
363 struct ieee80211_txq *txq = sta->sta.txq[i];
368 txqi = to_txq_info(txq);
373 if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX,
377 spin_unlock(&fq->lock);
378 drv_wake_tx_queue(local, txqi);
379 spin_lock(&fq->lock);
386 txqi = to_txq_info(vif->txq);
388 if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags) ||
389 (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
392 spin_unlock(&fq->lock);
394 drv_wake_tx_queue(local, txqi);
398 spin_unlock(&fq->lock);
403 __releases(&local->queue_stop_reason_lock)
404 __acquires(&local->queue_stop_reason_lock)
405 _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
407 struct ieee80211_sub_if_data *sdata;
408 int n_acs = IEEE80211_NUM_ACS;
413 if (local->hw.queues < IEEE80211_NUM_ACS)
416 for (i = 0; i < local->hw.queues; i++) {
417 if (local->queue_stop_reasons[i])
420 spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
421 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
424 for (ac = 0; ac < n_acs; ac++) {
425 int ac_queue = sdata->vif.hw_queue[ac];
428 sdata->vif.cab_queue == i)
429 __ieee80211_wake_txqs(sdata, ac);
432 spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
438 void ieee80211_wake_txqs(struct tasklet_struct *t)
440 struct ieee80211_local *local = from_tasklet(local, t,
444 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
445 _ieee80211_wake_txqs(local, &flags);
446 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
449 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
450 enum queue_stop_reason reason,
452 unsigned long *flags)
454 struct ieee80211_local *local = hw_to_local(hw);
456 trace_wake_queue(local, queue, reason);
458 if (WARN_ON(queue >= hw->queues))
461 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
465 local->q_stop_reasons[queue][reason] = 0;
467 local->q_stop_reasons[queue][reason]--;
468 if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
469 local->q_stop_reasons[queue][reason] = 0;
472 if (local->q_stop_reasons[queue][reason] == 0)
473 __clear_bit(reason, &local->queue_stop_reasons[queue]);
475 if (local->queue_stop_reasons[queue] != 0)
476 /* someone still has this queue stopped */
479 if (!skb_queue_empty(&local->pending[queue]))
480 tasklet_schedule(&local->tx_pending_tasklet);
483 * Calling _ieee80211_wake_txqs here can be a problem because it may
484 * release queue_stop_reason_lock which has been taken by
485 * __ieee80211_wake_queue's caller. It is certainly not very nice to
486 * release someone's lock, but it is fine because all the callers of
487 * __ieee80211_wake_queue call it right before releasing the lock.
489 if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
490 tasklet_schedule(&local->wake_txqs_tasklet);
492 _ieee80211_wake_txqs(local, flags);
495 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
496 enum queue_stop_reason reason,
499 struct ieee80211_local *local = hw_to_local(hw);
502 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
503 __ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
504 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
507 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
509 ieee80211_wake_queue_by_reason(hw, queue,
510 IEEE80211_QUEUE_STOP_REASON_DRIVER,
513 EXPORT_SYMBOL(ieee80211_wake_queue);
515 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
516 enum queue_stop_reason reason,
519 struct ieee80211_local *local = hw_to_local(hw);
520 struct ieee80211_sub_if_data *sdata;
521 int n_acs = IEEE80211_NUM_ACS;
523 trace_stop_queue(local, queue, reason);
525 if (WARN_ON(queue >= hw->queues))
529 local->q_stop_reasons[queue][reason] = 1;
531 local->q_stop_reasons[queue][reason]++;
533 if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
536 if (local->hw.queues < IEEE80211_NUM_ACS)
540 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
546 for (ac = 0; ac < n_acs; ac++) {
547 if (sdata->vif.hw_queue[ac] == queue ||
548 sdata->vif.cab_queue == queue) {
549 spin_lock(&local->fq.lock);
550 sdata->vif.txqs_stopped[ac] = true;
551 spin_unlock(&local->fq.lock);
558 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
559 enum queue_stop_reason reason,
562 struct ieee80211_local *local = hw_to_local(hw);
565 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
566 __ieee80211_stop_queue(hw, queue, reason, refcounted);
567 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
570 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
572 ieee80211_stop_queue_by_reason(hw, queue,
573 IEEE80211_QUEUE_STOP_REASON_DRIVER,
576 EXPORT_SYMBOL(ieee80211_stop_queue);
578 void ieee80211_add_pending_skb(struct ieee80211_local *local,
581 struct ieee80211_hw *hw = &local->hw;
583 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
584 int queue = info->hw_queue;
586 if (WARN_ON(!info->control.vif)) {
587 ieee80211_free_txskb(&local->hw, skb);
591 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
592 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
594 __skb_queue_tail(&local->pending[queue], skb);
595 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
597 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
600 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
601 struct sk_buff_head *skbs)
603 struct ieee80211_hw *hw = &local->hw;
608 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
609 while ((skb = skb_dequeue(skbs))) {
610 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
612 if (WARN_ON(!info->control.vif)) {
613 ieee80211_free_txskb(&local->hw, skb);
617 queue = info->hw_queue;
619 __ieee80211_stop_queue(hw, queue,
620 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
623 __skb_queue_tail(&local->pending[queue], skb);
626 for (i = 0; i < hw->queues; i++)
627 __ieee80211_wake_queue(hw, i,
628 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
630 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
633 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
634 unsigned long queues,
635 enum queue_stop_reason reason,
638 struct ieee80211_local *local = hw_to_local(hw);
642 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
644 for_each_set_bit(i, &queues, hw->queues)
645 __ieee80211_stop_queue(hw, i, reason, refcounted);
647 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
650 void ieee80211_stop_queues(struct ieee80211_hw *hw)
652 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
653 IEEE80211_QUEUE_STOP_REASON_DRIVER,
656 EXPORT_SYMBOL(ieee80211_stop_queues);
658 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
660 struct ieee80211_local *local = hw_to_local(hw);
664 if (WARN_ON(queue >= hw->queues))
667 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
668 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
669 &local->queue_stop_reasons[queue]);
670 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
673 EXPORT_SYMBOL(ieee80211_queue_stopped);
675 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
676 unsigned long queues,
677 enum queue_stop_reason reason,
680 struct ieee80211_local *local = hw_to_local(hw);
684 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
686 for_each_set_bit(i, &queues, hw->queues)
687 __ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
689 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
692 void ieee80211_wake_queues(struct ieee80211_hw *hw)
694 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
695 IEEE80211_QUEUE_STOP_REASON_DRIVER,
698 EXPORT_SYMBOL(ieee80211_wake_queues);
701 ieee80211_get_vif_queues(struct ieee80211_local *local,
702 struct ieee80211_sub_if_data *sdata)
706 if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
711 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
712 queues |= BIT(sdata->vif.hw_queue[ac]);
713 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
714 queues |= BIT(sdata->vif.cab_queue);
717 queues = BIT(local->hw.queues) - 1;
723 void __ieee80211_flush_queues(struct ieee80211_local *local,
724 struct ieee80211_sub_if_data *sdata,
725 unsigned int queues, bool drop)
727 if (!local->ops->flush)
731 * If no queue was set, or if the HW doesn't support
732 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
734 if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
735 queues = ieee80211_get_vif_queues(local, sdata);
737 ieee80211_stop_queues_by_reason(&local->hw, queues,
738 IEEE80211_QUEUE_STOP_REASON_FLUSH,
741 drv_flush(local, sdata, queues, drop);
743 ieee80211_wake_queues_by_reason(&local->hw, queues,
744 IEEE80211_QUEUE_STOP_REASON_FLUSH,
748 void ieee80211_flush_queues(struct ieee80211_local *local,
749 struct ieee80211_sub_if_data *sdata, bool drop)
751 __ieee80211_flush_queues(local, sdata, 0, drop);
754 void ieee80211_stop_vif_queues(struct ieee80211_local *local,
755 struct ieee80211_sub_if_data *sdata,
756 enum queue_stop_reason reason)
758 ieee80211_stop_queues_by_reason(&local->hw,
759 ieee80211_get_vif_queues(local, sdata),
763 void ieee80211_wake_vif_queues(struct ieee80211_local *local,
764 struct ieee80211_sub_if_data *sdata,
765 enum queue_stop_reason reason)
767 ieee80211_wake_queues_by_reason(&local->hw,
768 ieee80211_get_vif_queues(local, sdata),
772 static void __iterate_interfaces(struct ieee80211_local *local,
774 void (*iterator)(void *data, u8 *mac,
775 struct ieee80211_vif *vif),
778 struct ieee80211_sub_if_data *sdata;
779 bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
781 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
782 switch (sdata->vif.type) {
783 case NL80211_IFTYPE_MONITOR:
784 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
787 case NL80211_IFTYPE_AP_VLAN:
792 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
793 active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
795 if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) &&
796 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
798 if (ieee80211_sdata_running(sdata) || !active_only)
799 iterator(data, sdata->vif.addr,
803 sdata = rcu_dereference_check(local->monitor_sdata,
804 lockdep_is_held(&local->iflist_mtx) ||
805 lockdep_is_held(&local->hw.wiphy->mtx));
807 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
808 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
809 iterator(data, sdata->vif.addr, &sdata->vif);
812 void ieee80211_iterate_interfaces(
813 struct ieee80211_hw *hw, u32 iter_flags,
814 void (*iterator)(void *data, u8 *mac,
815 struct ieee80211_vif *vif),
818 struct ieee80211_local *local = hw_to_local(hw);
820 mutex_lock(&local->iflist_mtx);
821 __iterate_interfaces(local, iter_flags, iterator, data);
822 mutex_unlock(&local->iflist_mtx);
824 EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
826 void ieee80211_iterate_active_interfaces_atomic(
827 struct ieee80211_hw *hw, u32 iter_flags,
828 void (*iterator)(void *data, u8 *mac,
829 struct ieee80211_vif *vif),
832 struct ieee80211_local *local = hw_to_local(hw);
835 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
839 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
841 void ieee80211_iterate_active_interfaces_mtx(
842 struct ieee80211_hw *hw, u32 iter_flags,
843 void (*iterator)(void *data, u8 *mac,
844 struct ieee80211_vif *vif),
847 struct ieee80211_local *local = hw_to_local(hw);
849 lockdep_assert_wiphy(hw->wiphy);
851 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
854 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx);
856 static void __iterate_stations(struct ieee80211_local *local,
857 void (*iterator)(void *data,
858 struct ieee80211_sta *sta),
861 struct sta_info *sta;
863 list_for_each_entry_rcu(sta, &local->sta_list, list) {
867 iterator(data, &sta->sta);
871 void ieee80211_iterate_stations(struct ieee80211_hw *hw,
872 void (*iterator)(void *data,
873 struct ieee80211_sta *sta),
876 struct ieee80211_local *local = hw_to_local(hw);
878 mutex_lock(&local->sta_mtx);
879 __iterate_stations(local, iterator, data);
880 mutex_unlock(&local->sta_mtx);
882 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations);
884 void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
885 void (*iterator)(void *data,
886 struct ieee80211_sta *sta),
889 struct ieee80211_local *local = hw_to_local(hw);
892 __iterate_stations(local, iterator, data);
895 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
897 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
899 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
901 if (!ieee80211_sdata_running(sdata) ||
902 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
906 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
908 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
913 return &vif_to_sdata(vif)->wdev;
915 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
918 * Nothing should have been stuffed into the workqueue during
919 * the suspend->resume cycle. Since we can't check each caller
920 * of this function if we are already quiescing / suspended,
921 * check here and don't WARN since this can actually happen when
922 * the rx path (for example) is racing against __ieee80211_suspend
923 * and suspending / quiescing was set after the rx path checked
926 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
928 if (local->quiescing || (local->suspended && !local->resuming)) {
929 pr_warn("queueing ieee80211 work while going to suspend\n");
936 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
938 struct ieee80211_local *local = hw_to_local(hw);
940 if (!ieee80211_can_queue_work(local))
943 queue_work(local->workqueue, work);
945 EXPORT_SYMBOL(ieee80211_queue_work);
947 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
948 struct delayed_work *dwork,
951 struct ieee80211_local *local = hw_to_local(hw);
953 if (!ieee80211_can_queue_work(local))
956 queue_delayed_work(local->workqueue, dwork, delay);
958 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
961 ieee80211_parse_extension_element(u32 *crc,
962 const struct element *elem,
963 struct ieee802_11_elems *elems,
964 struct ieee80211_elems_parse_params *params)
966 const void *data = elem->data + 1;
972 len = elem->datalen - 1;
974 switch (elem->data[0]) {
975 case WLAN_EID_EXT_HE_MU_EDCA:
976 if (len >= sizeof(*elems->mu_edca_param_set)) {
977 elems->mu_edca_param_set = data;
979 *crc = crc32_be(*crc, (void *)elem,
983 case WLAN_EID_EXT_HE_CAPABILITY:
984 if (ieee80211_he_capa_size_ok(data, len)) {
985 elems->he_cap = data;
986 elems->he_cap_len = len;
989 case WLAN_EID_EXT_HE_OPERATION:
990 if (len >= sizeof(*elems->he_operation) &&
991 len >= ieee80211_he_oper_size(data) - 1) {
993 *crc = crc32_be(*crc, (void *)elem,
995 elems->he_operation = data;
998 case WLAN_EID_EXT_UORA:
1000 elems->uora_element = data;
1002 case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
1004 elems->max_channel_switch_time = data;
1006 case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
1007 if (len >= sizeof(*elems->mbssid_config_ie))
1008 elems->mbssid_config_ie = data;
1010 case WLAN_EID_EXT_HE_SPR:
1011 if (len >= sizeof(*elems->he_spr) &&
1012 len >= ieee80211_he_spr_size(data))
1013 elems->he_spr = data;
1015 case WLAN_EID_EXT_HE_6GHZ_CAPA:
1016 if (len >= sizeof(*elems->he_6ghz_capa))
1017 elems->he_6ghz_capa = data;
1019 case WLAN_EID_EXT_EHT_CAPABILITY:
1020 if (ieee80211_eht_capa_size_ok(elems->he_cap,
1023 elems->eht_cap = data;
1024 elems->eht_cap_len = len;
1027 case WLAN_EID_EXT_EHT_OPERATION:
1028 if (ieee80211_eht_oper_size_ok(data, len))
1029 elems->eht_operation = data;
1031 case WLAN_EID_EXT_EHT_MULTI_LINK:
1032 if (ieee80211_mle_size_ok(data, len)) {
1033 elems->multi_link = (void *)data;
1034 elems->multi_link_len = len;
1041 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
1042 struct ieee802_11_elems *elems,
1043 const struct element *check_inherit)
1045 const struct element *elem;
1046 bool calc_crc = params->filter != 0;
1047 DECLARE_BITMAP(seen_elems, 256);
1048 u32 crc = params->crc;
1051 bitmap_zero(seen_elems, 256);
1053 for_each_element(elem, params->start, params->len) {
1054 bool elem_parse_failed;
1056 u8 elen = elem->datalen;
1057 const u8 *pos = elem->data;
1059 if (check_inherit &&
1060 !cfg80211_is_element_inherited(elem,
1066 case WLAN_EID_SUPP_RATES:
1067 case WLAN_EID_FH_PARAMS:
1068 case WLAN_EID_DS_PARAMS:
1069 case WLAN_EID_CF_PARAMS:
1071 case WLAN_EID_IBSS_PARAMS:
1072 case WLAN_EID_CHALLENGE:
1074 case WLAN_EID_ERP_INFO:
1075 case WLAN_EID_EXT_SUPP_RATES:
1076 case WLAN_EID_HT_CAPABILITY:
1077 case WLAN_EID_HT_OPERATION:
1078 case WLAN_EID_VHT_CAPABILITY:
1079 case WLAN_EID_VHT_OPERATION:
1080 case WLAN_EID_MESH_ID:
1081 case WLAN_EID_MESH_CONFIG:
1082 case WLAN_EID_PEER_MGMT:
1087 case WLAN_EID_CHANNEL_SWITCH:
1088 case WLAN_EID_EXT_CHANSWITCH_ANN:
1089 case WLAN_EID_COUNTRY:
1090 case WLAN_EID_PWR_CONSTRAINT:
1091 case WLAN_EID_TIMEOUT_INTERVAL:
1092 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1093 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1094 case WLAN_EID_CHAN_SWITCH_PARAM:
1095 case WLAN_EID_EXT_CAPABILITY:
1096 case WLAN_EID_CHAN_SWITCH_TIMING:
1097 case WLAN_EID_LINK_ID:
1098 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1100 case WLAN_EID_S1G_BCN_COMPAT:
1101 case WLAN_EID_S1G_CAPABILITIES:
1102 case WLAN_EID_S1G_OPERATION:
1103 case WLAN_EID_AID_RESPONSE:
1104 case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
1106 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1107 * that if the content gets bigger it might be needed more than once
1109 if (test_bit(id, seen_elems)) {
1110 elems->parse_error = true;
1116 if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
1117 crc = crc32_be(crc, pos - 2, elen + 2);
1119 elem_parse_failed = false;
1122 case WLAN_EID_LINK_ID:
1123 if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
1124 elem_parse_failed = true;
1127 elems->lnk_id = (void *)(pos - 2);
1129 case WLAN_EID_CHAN_SWITCH_TIMING:
1130 if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
1131 elem_parse_failed = true;
1134 elems->ch_sw_timing = (void *)pos;
1136 case WLAN_EID_EXT_CAPABILITY:
1137 elems->ext_capab = pos;
1138 elems->ext_capab_len = elen;
1142 elems->ssid_len = elen;
1144 case WLAN_EID_SUPP_RATES:
1145 elems->supp_rates = pos;
1146 elems->supp_rates_len = elen;
1148 case WLAN_EID_DS_PARAMS:
1150 elems->ds_params = pos;
1152 elem_parse_failed = true;
1155 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1156 elems->tim = (void *)pos;
1157 elems->tim_len = elen;
1159 elem_parse_failed = true;
1161 case WLAN_EID_VENDOR_SPECIFIC:
1162 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1164 /* Microsoft OUI (00:50:F2) */
1167 crc = crc32_be(crc, pos - 2, elen + 2);
1169 if (elen >= 5 && pos[3] == 2) {
1170 /* OUI Type 2 - WMM IE */
1172 elems->wmm_info = pos;
1173 elems->wmm_info_len = elen;
1174 } else if (pos[4] == 1) {
1175 elems->wmm_param = pos;
1176 elems->wmm_param_len = elen;
1183 elems->rsn_len = elen;
1185 case WLAN_EID_ERP_INFO:
1187 elems->erp_info = pos;
1189 elem_parse_failed = true;
1191 case WLAN_EID_EXT_SUPP_RATES:
1192 elems->ext_supp_rates = pos;
1193 elems->ext_supp_rates_len = elen;
1195 case WLAN_EID_HT_CAPABILITY:
1196 if (elen >= sizeof(struct ieee80211_ht_cap))
1197 elems->ht_cap_elem = (void *)pos;
1199 elem_parse_failed = true;
1201 case WLAN_EID_HT_OPERATION:
1202 if (elen >= sizeof(struct ieee80211_ht_operation))
1203 elems->ht_operation = (void *)pos;
1205 elem_parse_failed = true;
1207 case WLAN_EID_VHT_CAPABILITY:
1208 if (elen >= sizeof(struct ieee80211_vht_cap))
1209 elems->vht_cap_elem = (void *)pos;
1211 elem_parse_failed = true;
1213 case WLAN_EID_VHT_OPERATION:
1214 if (elen >= sizeof(struct ieee80211_vht_operation)) {
1215 elems->vht_operation = (void *)pos;
1217 crc = crc32_be(crc, pos - 2, elen + 2);
1220 elem_parse_failed = true;
1222 case WLAN_EID_OPMODE_NOTIF:
1224 elems->opmode_notif = pos;
1226 crc = crc32_be(crc, pos - 2, elen + 2);
1229 elem_parse_failed = true;
1231 case WLAN_EID_MESH_ID:
1232 elems->mesh_id = pos;
1233 elems->mesh_id_len = elen;
1235 case WLAN_EID_MESH_CONFIG:
1236 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1237 elems->mesh_config = (void *)pos;
1239 elem_parse_failed = true;
1241 case WLAN_EID_PEER_MGMT:
1242 elems->peering = pos;
1243 elems->peering_len = elen;
1245 case WLAN_EID_MESH_AWAKE_WINDOW:
1247 elems->awake_window = (void *)pos;
1251 elems->preq_len = elen;
1255 elems->prep_len = elen;
1259 elems->perr_len = elen;
1262 if (elen >= sizeof(struct ieee80211_rann_ie))
1263 elems->rann = (void *)pos;
1265 elem_parse_failed = true;
1267 case WLAN_EID_CHANNEL_SWITCH:
1268 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
1269 elem_parse_failed = true;
1272 elems->ch_switch_ie = (void *)pos;
1274 case WLAN_EID_EXT_CHANSWITCH_ANN:
1275 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1276 elem_parse_failed = true;
1279 elems->ext_chansw_ie = (void *)pos;
1281 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1282 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1283 elem_parse_failed = true;
1286 elems->sec_chan_offs = (void *)pos;
1288 case WLAN_EID_CHAN_SWITCH_PARAM:
1290 sizeof(*elems->mesh_chansw_params_ie)) {
1291 elem_parse_failed = true;
1294 elems->mesh_chansw_params_ie = (void *)pos;
1296 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1297 if (!params->action ||
1298 elen < sizeof(*elems->wide_bw_chansw_ie)) {
1299 elem_parse_failed = true;
1302 elems->wide_bw_chansw_ie = (void *)pos;
1304 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1305 if (params->action) {
1306 elem_parse_failed = true;
1310 * This is a bit tricky, but as we only care about
1311 * the wide bandwidth channel switch element, so
1312 * just parse it out manually.
1314 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1317 if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
1318 elems->wide_bw_chansw_ie =
1321 elem_parse_failed = true;
1324 case WLAN_EID_COUNTRY:
1325 elems->country_elem = pos;
1326 elems->country_elem_len = elen;
1328 case WLAN_EID_PWR_CONSTRAINT:
1330 elem_parse_failed = true;
1333 elems->pwr_constr_elem = pos;
1335 case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1336 /* Lots of different options exist, but we only care
1337 * about the Dynamic Transmit Power Control element.
1338 * First check for the Cisco OUI, then for the DTPC
1342 elem_parse_failed = true;
1346 if (pos[0] != 0x00 || pos[1] != 0x40 ||
1347 pos[2] != 0x96 || pos[3] != 0x00)
1351 elem_parse_failed = true;
1356 crc = crc32_be(crc, pos - 2, elen + 2);
1358 elems->cisco_dtpc_elem = pos;
1360 case WLAN_EID_ADDBA_EXT:
1361 if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
1362 elem_parse_failed = true;
1365 elems->addba_ext_ie = (void *)pos;
1367 case WLAN_EID_TIMEOUT_INTERVAL:
1368 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1369 elems->timeout_int = (void *)pos;
1371 elem_parse_failed = true;
1373 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1374 if (elen >= sizeof(*elems->max_idle_period_ie))
1375 elems->max_idle_period_ie = (void *)pos;
1379 elems->rsnx_len = elen;
1381 case WLAN_EID_TX_POWER_ENVELOPE:
1383 elen > sizeof(struct ieee80211_tx_pwr_env))
1386 if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
1389 elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
1390 elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
1391 elems->tx_pwr_env_num++;
1393 case WLAN_EID_EXTENSION:
1394 ieee80211_parse_extension_element(calc_crc ?
1396 elem, elems, params);
1398 case WLAN_EID_S1G_CAPABILITIES:
1399 if (elen >= sizeof(*elems->s1g_capab))
1400 elems->s1g_capab = (void *)pos;
1402 elem_parse_failed = true;
1404 case WLAN_EID_S1G_OPERATION:
1405 if (elen == sizeof(*elems->s1g_oper))
1406 elems->s1g_oper = (void *)pos;
1408 elem_parse_failed = true;
1410 case WLAN_EID_S1G_BCN_COMPAT:
1411 if (elen == sizeof(*elems->s1g_bcn_compat))
1412 elems->s1g_bcn_compat = (void *)pos;
1414 elem_parse_failed = true;
1416 case WLAN_EID_AID_RESPONSE:
1417 if (elen == sizeof(struct ieee80211_aid_response_ie))
1418 elems->aid_resp = (void *)pos;
1420 elem_parse_failed = true;
1426 if (elem_parse_failed)
1427 elems->parse_error = true;
1429 __set_bit(id, seen_elems);
1432 if (!for_each_element_completed(elem, params->start, params->len))
1433 elems->parse_error = true;
1438 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1439 struct ieee802_11_elems *elems,
1440 struct cfg80211_bss *bss,
1441 u8 *nontransmitted_profile)
1443 const struct element *elem, *sub;
1444 size_t profile_len = 0;
1447 if (!bss || !bss->transmitted_bss)
1450 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1451 if (elem->datalen < 2)
1453 if (elem->data[0] < 1 || elem->data[0] > 8)
1456 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1457 u8 new_bssid[ETH_ALEN];
1460 if (sub->id != 0 || sub->datalen < 4) {
1461 /* not a valid BSS profile */
1465 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1466 sub->data[1] != 2) {
1467 /* The first element of the
1468 * Nontransmitted BSSID Profile is not
1469 * the Nontransmitted BSSID Capability
1475 memset(nontransmitted_profile, 0, len);
1476 profile_len = cfg80211_merge_profile(start, len,
1479 nontransmitted_profile,
1482 /* found a Nontransmitted BSSID Profile */
1483 index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
1484 nontransmitted_profile,
1486 if (!index || index[1] < 1 || index[2] == 0) {
1487 /* Invalid MBSSID Index element */
1491 cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
1495 if (ether_addr_equal(new_bssid, bss->bssid)) {
1497 elems->bssid_index_len = index[1];
1498 elems->bssid_index = (void *)&index[2];
1504 return found ? profile_len : 0;
1507 static void ieee80211_defragment_element(struct ieee802_11_elems *elems,
1508 void **elem_ptr, size_t *len,
1509 size_t total_len, u8 frag_id)
1511 u8 *data = *elem_ptr, *pos, *start;
1512 const struct element *elem;
1515 * Since 'data' points to the data of the element, not the element
1516 * itself, allow 254 in case it was an extended element where the
1517 * extended ID isn't part of the data we see here and thus not part of
1520 if (!data || (*len != 254 && *len != 255))
1523 start = elems->scratch_pos;
1525 if (WARN_ON(*len > (elems->scratch + elems->scratch_len -
1526 elems->scratch_pos)))
1529 memcpy(elems->scratch_pos, data, *len);
1530 elems->scratch_pos += *len;
1534 for_each_element(elem, pos, total_len) {
1535 if (elem->id != frag_id)
1538 if (WARN_ON(elem->datalen >
1539 (elems->scratch + elems->scratch_len -
1540 elems->scratch_pos)))
1543 memcpy(elems->scratch_pos, elem->data, elem->datalen);
1544 elems->scratch_pos += elem->datalen;
1546 *len += elem->datalen;
1552 static void ieee80211_mle_get_sta_prof(struct ieee802_11_elems *elems,
1555 const struct ieee80211_multi_link_elem *ml = elems->multi_link;
1556 size_t ml_len = elems->multi_link_len;
1557 const struct element *sub;
1562 if (le16_get_bits(ml->control, IEEE80211_ML_CONTROL_TYPE) !=
1563 IEEE80211_ML_CONTROL_TYPE_BASIC)
1566 for_each_mle_subelement(sub, (u8 *)ml, ml_len) {
1567 struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
1570 if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
1573 if (!ieee80211_mle_sta_prof_size_ok(sub->data, sub->datalen))
1576 control = le16_to_cpu(prof->control);
1578 if (link_id != u16_get_bits(control,
1579 IEEE80211_MLE_STA_CONTROL_LINK_ID))
1582 if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
1586 elems->sta_prof_len = sub->datalen;
1588 /* the sub element can be fragmented */
1589 ieee80211_defragment_element(elems, (void **)&elems->prof,
1590 &elems->sta_prof_len,
1591 ml_len - (sub->data - (u8 *)ml),
1592 IEEE80211_MLE_SUBELEM_FRAGMENT);
1597 static void ieee80211_mle_parse_link(struct ieee802_11_elems *elems,
1598 struct ieee80211_elems_parse_params *params)
1600 struct ieee80211_mle_per_sta_profile *prof;
1601 struct ieee80211_elems_parse_params sub = {
1602 .action = params->action,
1603 .from_ap = params->from_ap,
1606 const struct element *non_inherit = NULL;
1609 if (params->link_id == -1)
1612 ieee80211_defragment_element(elems, (void **)&elems->multi_link,
1613 &elems->multi_link_len,
1614 elems->total_len - ((u8 *)elems->multi_link -
1618 ieee80211_mle_get_sta_prof(elems, params->link_id);
1624 /* check if we have the 4 bytes for the fixed part in assoc response */
1625 if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) {
1627 elems->sta_prof_len = 0;
1632 * Skip the capability information and the status code that are expected
1633 * as part of the station profile in association response frames. Note
1634 * the -1 is because the 'sta_info_len' is accounted to as part of the
1635 * per-STA profile, but not part of the 'u8 variable[]' portion.
1637 sub.start = prof->variable + prof->sta_info_len - 1 + 4;
1638 end = (const u8 *)prof + elems->sta_prof_len;
1639 sub.len = end - sub.start;
1641 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1642 sub.start, sub.len);
1643 _ieee802_11_parse_elems_full(&sub, elems, non_inherit);
1646 struct ieee802_11_elems *
1647 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
1649 struct ieee802_11_elems *elems;
1650 const struct element *non_inherit = NULL;
1651 u8 *nontransmitted_profile;
1652 int nontransmitted_profile_len = 0;
1653 size_t scratch_len = params->scratch_len ?: 3 * params->len;
1655 elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1658 elems->ie_start = params->start;
1659 elems->total_len = params->len;
1660 elems->scratch_len = scratch_len;
1661 elems->scratch_pos = elems->scratch;
1663 nontransmitted_profile = elems->scratch_pos;
1664 nontransmitted_profile_len =
1665 ieee802_11_find_bssid_profile(params->start, params->len,
1667 nontransmitted_profile);
1668 elems->scratch_pos += nontransmitted_profile_len;
1669 elems->scratch_len -= nontransmitted_profile_len;
1670 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1671 nontransmitted_profile,
1672 nontransmitted_profile_len);
1674 elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
1676 /* Override with nontransmitted profile, if found */
1677 if (nontransmitted_profile_len) {
1678 struct ieee80211_elems_parse_params sub = {
1679 .start = nontransmitted_profile,
1680 .len = nontransmitted_profile_len,
1681 .action = params->action,
1682 .link_id = params->link_id,
1685 _ieee802_11_parse_elems_full(&sub, elems, NULL);
1688 ieee80211_mle_parse_link(elems, params);
1690 if (elems->tim && !elems->parse_error) {
1691 const struct ieee80211_tim_ie *tim_ie = elems->tim;
1693 elems->dtim_period = tim_ie->dtim_period;
1694 elems->dtim_count = tim_ie->dtim_count;
1697 /* Override DTIM period and count if needed */
1698 if (elems->bssid_index &&
1699 elems->bssid_index_len >=
1700 offsetofend(struct ieee80211_bssid_index, dtim_period))
1701 elems->dtim_period = elems->bssid_index->dtim_period;
1703 if (elems->bssid_index &&
1704 elems->bssid_index_len >=
1705 offsetofend(struct ieee80211_bssid_index, dtim_count))
1706 elems->dtim_count = elems->bssid_index->dtim_count;
1711 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1712 struct ieee80211_tx_queue_params
1715 struct ieee80211_chanctx_conf *chanctx_conf;
1716 const struct ieee80211_reg_rule *rrule;
1717 const struct ieee80211_wmm_ac *wmm_ac;
1718 u16 center_freq = 0;
1720 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1721 sdata->vif.type != NL80211_IFTYPE_STATION)
1725 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1727 center_freq = chanctx_conf->def.chan->center_freq;
1734 rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1736 if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1741 if (sdata->vif.type == NL80211_IFTYPE_AP)
1742 wmm_ac = &rrule->wmm_rule.ap[ac];
1744 wmm_ac = &rrule->wmm_rule.client[ac];
1745 qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1746 qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1747 qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1748 qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1752 void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
1753 bool bss_notify, bool enable_qos)
1755 struct ieee80211_sub_if_data *sdata = link->sdata;
1756 struct ieee80211_local *local = sdata->local;
1757 struct ieee80211_tx_queue_params qparam;
1758 struct ieee80211_chanctx_conf *chanctx_conf;
1761 bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1764 if (!local->ops->conf_tx)
1767 if (local->hw.queues < IEEE80211_NUM_ACS)
1770 memset(&qparam, 0, sizeof(qparam));
1773 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
1774 use_11b = (chanctx_conf &&
1775 chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1776 !link->operating_11g_mode;
1779 is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1781 /* Set defaults according to 802.11-2007 Table 7-37 */
1788 /* Confiure old 802.11b/g medium access rules. */
1789 qparam.cw_max = aCWmax;
1790 qparam.cw_min = aCWmin;
1794 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1795 /* Update if QoS is enabled. */
1798 case IEEE80211_AC_BK:
1799 qparam.cw_max = aCWmax;
1800 qparam.cw_min = aCWmin;
1807 /* never happens but let's not leave undefined */
1809 case IEEE80211_AC_BE:
1810 qparam.cw_max = aCWmax;
1811 qparam.cw_min = aCWmin;
1818 case IEEE80211_AC_VI:
1819 qparam.cw_max = aCWmin;
1820 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1824 qparam.txop = 6016/32;
1826 qparam.txop = 3008/32;
1833 case IEEE80211_AC_VO:
1834 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1835 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1839 qparam.txop = 3264/32;
1841 qparam.txop = 1504/32;
1846 ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
1848 qparam.uapsd = false;
1850 link->tx_conf[ac] = qparam;
1851 drv_conf_tx(local, link, ac, &qparam);
1854 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1855 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1856 sdata->vif.type != NL80211_IFTYPE_NAN) {
1857 link->conf->qos = enable_qos;
1859 ieee80211_link_info_change_notify(sdata, link,
1864 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1865 u16 transaction, u16 auth_alg, u16 status,
1866 const u8 *extra, size_t extra_len, const u8 *da,
1867 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1870 struct ieee80211_local *local = sdata->local;
1871 struct sk_buff *skb;
1872 struct ieee80211_mgmt *mgmt;
1873 bool multi_link = sdata->vif.valid_links;
1878 struct ieee80211_multi_link_elem ml;
1879 struct ieee80211_mle_basic_common_info basic;
1881 .id = WLAN_EID_EXTENSION,
1882 .len = sizeof(mle) - 2,
1883 .ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
1884 .ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
1885 .basic.len = sizeof(mle.basic),
1889 memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1891 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1892 skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1893 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
1894 multi_link * sizeof(mle));
1898 skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
1900 mgmt = skb_put_zero(skb, 24 + 6);
1901 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1902 IEEE80211_STYPE_AUTH);
1903 memcpy(mgmt->da, da, ETH_ALEN);
1904 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1905 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1906 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1907 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1908 mgmt->u.auth.status_code = cpu_to_le16(status);
1910 skb_put_data(skb, extra, extra_len);
1912 skb_put_data(skb, &mle, sizeof(mle));
1914 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1915 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1916 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1923 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1925 ieee80211_tx_skb(sdata, skb);
1928 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1929 const u8 *da, const u8 *bssid,
1930 u16 stype, u16 reason,
1931 bool send_frame, u8 *frame_buf)
1933 struct ieee80211_local *local = sdata->local;
1934 struct sk_buff *skb;
1935 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1938 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1939 mgmt->duration = 0; /* initialize only */
1940 mgmt->seq_ctrl = 0; /* initialize only */
1941 memcpy(mgmt->da, da, ETH_ALEN);
1942 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1943 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1944 /* u.deauth.reason_code == u.disassoc.reason_code */
1945 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1948 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1949 IEEE80211_DEAUTH_FRAME_LEN);
1953 skb_reserve(skb, local->hw.extra_tx_headroom);
1956 skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1958 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1959 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1960 IEEE80211_SKB_CB(skb)->flags |=
1961 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1963 ieee80211_tx_skb(sdata, skb);
1967 static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
1969 if ((end - pos) < 5)
1972 *pos++ = WLAN_EID_EXTENSION;
1973 *pos++ = 1 + sizeof(cap);
1974 *pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
1975 memcpy(pos, &cap, sizeof(cap));
1980 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
1981 u8 *buffer, size_t buffer_len,
1982 const u8 *ie, size_t ie_len,
1983 enum nl80211_band band,
1985 struct cfg80211_chan_def *chandef,
1986 size_t *offset, u32 flags)
1988 struct ieee80211_local *local = sdata->local;
1989 struct ieee80211_supported_band *sband;
1990 const struct ieee80211_sta_he_cap *he_cap;
1991 const struct ieee80211_sta_eht_cap *eht_cap;
1992 u8 *pos = buffer, *end = buffer + buffer_len;
1994 int supp_rates_len, i;
2000 bool have_80mhz = false;
2004 sband = local->hw.wiphy->bands[band];
2005 if (WARN_ON_ONCE(!sband))
2008 rate_flags = ieee80211_chandef_rate_flags(chandef);
2009 shift = ieee80211_chandef_get_shift(chandef);
2012 for (i = 0; i < sband->n_bitrates; i++) {
2013 if ((BIT(i) & rate_mask) == 0)
2014 continue; /* skip rate */
2015 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2018 rates[num_rates++] =
2019 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
2023 supp_rates_len = min_t(int, num_rates, 8);
2025 if (end - pos < 2 + supp_rates_len)
2027 *pos++ = WLAN_EID_SUPP_RATES;
2028 *pos++ = supp_rates_len;
2029 memcpy(pos, rates, supp_rates_len);
2030 pos += supp_rates_len;
2032 /* insert "request information" if in custom IEs */
2034 static const u8 before_extrates[] = {
2036 WLAN_EID_SUPP_RATES,
2039 noffset = ieee80211_ie_split(ie, ie_len,
2041 ARRAY_SIZE(before_extrates),
2043 if (end - pos < noffset - *offset)
2045 memcpy(pos, ie + *offset, noffset - *offset);
2046 pos += noffset - *offset;
2050 ext_rates_len = num_rates - supp_rates_len;
2051 if (ext_rates_len > 0) {
2052 if (end - pos < 2 + ext_rates_len)
2054 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2055 *pos++ = ext_rates_len;
2056 memcpy(pos, rates + supp_rates_len, ext_rates_len);
2057 pos += ext_rates_len;
2060 if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
2063 *pos++ = WLAN_EID_DS_PARAMS;
2065 *pos++ = ieee80211_frequency_to_channel(
2066 chandef->chan->center_freq);
2069 if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
2072 /* insert custom IEs that go before HT */
2074 static const u8 before_ht[] = {
2076 * no need to list the ones split off already
2077 * (or generated here)
2080 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2082 noffset = ieee80211_ie_split(ie, ie_len,
2083 before_ht, ARRAY_SIZE(before_ht),
2085 if (end - pos < noffset - *offset)
2087 memcpy(pos, ie + *offset, noffset - *offset);
2088 pos += noffset - *offset;
2092 if (sband->ht_cap.ht_supported) {
2093 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
2095 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
2099 /* insert custom IEs that go before VHT */
2101 static const u8 before_vht[] = {
2103 * no need to list the ones split off already
2104 * (or generated here)
2106 WLAN_EID_BSS_COEX_2040,
2107 WLAN_EID_EXT_CAPABILITY,
2109 WLAN_EID_CHANNEL_USAGE,
2110 WLAN_EID_INTERWORKING,
2112 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
2114 noffset = ieee80211_ie_split(ie, ie_len,
2115 before_vht, ARRAY_SIZE(before_vht),
2117 if (end - pos < noffset - *offset)
2119 memcpy(pos, ie + *offset, noffset - *offset);
2120 pos += noffset - *offset;
2124 /* Check if any channel in this sband supports at least 80 MHz */
2125 for (i = 0; i < sband->n_channels; i++) {
2126 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
2127 IEEE80211_CHAN_NO_80MHZ))
2134 if (sband->vht_cap.vht_supported && have_80mhz) {
2135 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
2137 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
2138 sband->vht_cap.cap);
2141 /* insert custom IEs that go before HE */
2143 static const u8 before_he[] = {
2145 * no need to list the ones split off before VHT
2148 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
2150 /* TODO: add 11ah/11aj/11ak elements */
2152 noffset = ieee80211_ie_split(ie, ie_len,
2153 before_he, ARRAY_SIZE(before_he),
2155 if (end - pos < noffset - *offset)
2157 memcpy(pos, ie + *offset, noffset - *offset);
2158 pos += noffset - *offset;
2162 he_cap = ieee80211_get_he_iftype_cap(sband,
2163 ieee80211_vif_type_p2p(&sdata->vif));
2165 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2166 IEEE80211_CHAN_NO_HE)) {
2167 pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
2172 eht_cap = ieee80211_get_eht_iftype_cap(sband,
2173 ieee80211_vif_type_p2p(&sdata->vif));
2176 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2177 IEEE80211_CHAN_NO_HE |
2178 IEEE80211_CHAN_NO_EHT)) {
2179 pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
2180 sdata->vif.type == NL80211_IFTYPE_AP);
2185 if (cfg80211_any_usable_channels(local->hw.wiphy,
2186 BIT(NL80211_BAND_6GHZ),
2187 IEEE80211_CHAN_NO_HE)) {
2188 struct ieee80211_supported_band *sband6;
2190 sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2191 he_cap = ieee80211_get_he_iftype_cap(sband6,
2192 ieee80211_vif_type_p2p(&sdata->vif));
2195 enum nl80211_iftype iftype =
2196 ieee80211_vif_type_p2p(&sdata->vif);
2197 __le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
2199 pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
2204 * If adding more here, adjust code in main.c
2205 * that calculates local->scan_ies_len.
2208 return pos - buffer;
2210 WARN_ONCE(1, "not enough space for preq IEs\n");
2212 return pos - buffer;
2215 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
2217 struct ieee80211_scan_ies *ie_desc,
2218 const u8 *ie, size_t ie_len,
2219 u8 bands_used, u32 *rate_masks,
2220 struct cfg80211_chan_def *chandef,
2223 size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
2226 memset(ie_desc, 0, sizeof(*ie_desc));
2228 for (i = 0; i < NUM_NL80211_BANDS; i++) {
2229 if (bands_used & BIT(i)) {
2230 pos += ieee80211_build_preq_ies_band(sdata,
2238 ie_desc->ies[i] = buffer + old_pos;
2239 ie_desc->len[i] = pos - old_pos;
2244 /* add any remaining custom IEs */
2246 if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
2247 "not enough space for preq custom IEs\n"))
2249 memcpy(buffer + pos, ie + custom_ie_offset,
2250 ie_len - custom_ie_offset);
2251 ie_desc->common_ies = buffer + pos;
2252 ie_desc->common_ie_len = ie_len - custom_ie_offset;
2253 pos += ie_len - custom_ie_offset;
2259 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
2260 const u8 *src, const u8 *dst,
2262 struct ieee80211_channel *chan,
2263 const u8 *ssid, size_t ssid_len,
2264 const u8 *ie, size_t ie_len,
2267 struct ieee80211_local *local = sdata->local;
2268 struct cfg80211_chan_def chandef;
2269 struct sk_buff *skb;
2270 struct ieee80211_mgmt *mgmt;
2272 u32 rate_masks[NUM_NL80211_BANDS] = {};
2273 struct ieee80211_scan_ies dummy_ie_desc;
2276 * Do not send DS Channel parameter for directed probe requests
2277 * in order to maximize the chance that we get a response. Some
2278 * badly-behaved APs don't respond when this parameter is included.
2280 chandef.width = sdata->vif.bss_conf.chandef.width;
2281 if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2282 chandef.chan = NULL;
2284 chandef.chan = chan;
2286 skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
2287 local->scan_ies_len + ie_len);
2291 rate_masks[chan->band] = ratemask;
2292 ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
2293 skb_tailroom(skb), &dummy_ie_desc,
2294 ie, ie_len, BIT(chan->band),
2295 rate_masks, &chandef, flags);
2296 skb_put(skb, ies_len);
2299 mgmt = (struct ieee80211_mgmt *) skb->data;
2300 memcpy(mgmt->da, dst, ETH_ALEN);
2301 memcpy(mgmt->bssid, dst, ETH_ALEN);
2304 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2309 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
2310 struct ieee802_11_elems *elems,
2311 enum nl80211_band band, u32 *basic_rates)
2313 struct ieee80211_supported_band *sband;
2315 u32 supp_rates, rate_flags;
2318 sband = sdata->local->hw.wiphy->bands[band];
2319 if (WARN_ON(!sband))
2322 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2323 shift = ieee80211_vif_get_shift(&sdata->vif);
2325 num_rates = sband->n_bitrates;
2327 for (i = 0; i < elems->supp_rates_len +
2328 elems->ext_supp_rates_len; i++) {
2332 if (i < elems->supp_rates_len)
2333 rate = elems->supp_rates[i];
2334 else if (elems->ext_supp_rates)
2335 rate = elems->ext_supp_rates
2336 [i - elems->supp_rates_len];
2337 own_rate = 5 * (rate & 0x7f);
2338 is_basic = !!(rate & 0x80);
2340 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2343 for (j = 0; j < num_rates; j++) {
2345 if ((rate_flags & sband->bitrates[j].flags)
2349 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
2352 if (brate == own_rate) {
2353 supp_rates |= BIT(j);
2354 if (basic_rates && is_basic)
2355 *basic_rates |= BIT(j);
2362 void ieee80211_stop_device(struct ieee80211_local *local)
2364 ieee80211_led_radio(local, false);
2365 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
2367 cancel_work_sync(&local->reconfig_filter);
2369 flush_workqueue(local->workqueue);
2373 static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
2376 /* It's possible that we don't handle the scan completion in
2377 * time during suspend, so if it's still marked as completed
2378 * here, queue the work and flush it to clean things up.
2379 * Instead of calling the worker function directly here, we
2380 * really queue it to avoid potential races with other flows
2381 * scheduling the same work.
2383 if (test_bit(SCAN_COMPLETED, &local->scanning)) {
2384 /* If coming from reconfiguration failure, abort the scan so
2385 * we don't attempt to continue a partial HW scan - which is
2386 * possible otherwise if (e.g.) the 2.4 GHz portion was the
2387 * completed scan, and a 5 GHz portion is still pending.
2390 set_bit(SCAN_ABORTED, &local->scanning);
2391 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
2392 flush_delayed_work(&local->scan_work);
2396 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2398 struct ieee80211_sub_if_data *sdata;
2399 struct ieee80211_chanctx *ctx;
2402 * We get here if during resume the device can't be restarted properly.
2403 * We might also get here if this happens during HW reset, which is a
2404 * slightly different situation and we need to drop all connections in
2407 * Ask cfg80211 to turn off all interfaces, this will result in more
2408 * warnings but at least we'll then get into a clean stopped state.
2411 local->resuming = false;
2412 local->suspended = false;
2413 local->in_reconfig = false;
2415 ieee80211_flush_completed_scan(local, true);
2417 /* scheduled scan clearly can't be running any more, but tell
2418 * cfg80211 and clear local state
2420 ieee80211_sched_scan_end(local);
2422 list_for_each_entry(sdata, &local->interfaces, list)
2423 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2425 /* Mark channel contexts as not being in the driver any more to avoid
2426 * removing them from the driver during the shutdown process...
2428 mutex_lock(&local->chanctx_mtx);
2429 list_for_each_entry(ctx, &local->chanctx_list, list)
2430 ctx->driver_present = false;
2431 mutex_unlock(&local->chanctx_mtx);
2434 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2435 struct ieee80211_sub_if_data *sdata,
2436 struct ieee80211_link_data *link)
2438 struct ieee80211_chanctx_conf *conf;
2439 struct ieee80211_chanctx *ctx;
2441 if (!local->use_chanctx)
2444 mutex_lock(&local->chanctx_mtx);
2445 conf = rcu_dereference_protected(link->conf->chanctx_conf,
2446 lockdep_is_held(&local->chanctx_mtx));
2448 ctx = container_of(conf, struct ieee80211_chanctx, conf);
2449 drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
2451 mutex_unlock(&local->chanctx_mtx);
2454 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
2456 struct ieee80211_local *local = sdata->local;
2457 struct sta_info *sta;
2460 mutex_lock(&local->sta_mtx);
2461 list_for_each_entry(sta, &local->sta_list, list) {
2462 enum ieee80211_sta_state state;
2464 if (!sta->uploaded || sta->sdata != sdata)
2467 for (state = IEEE80211_STA_NOTEXIST;
2468 state < sta->sta_state; state++)
2469 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2472 mutex_unlock(&local->sta_mtx);
2475 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2477 struct cfg80211_nan_func *func, **funcs;
2480 res = drv_start_nan(sdata->local, sdata,
2481 &sdata->u.nan.conf);
2485 funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
2491 /* Add all the functions:
2492 * This is a little bit ugly. We need to call a potentially sleeping
2493 * callback for each NAN function, so we can't hold the spinlock.
2495 spin_lock_bh(&sdata->u.nan.func_lock);
2497 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2500 spin_unlock_bh(&sdata->u.nan.func_lock);
2502 for (i = 0; funcs[i]; i++) {
2503 res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2505 ieee80211_nan_func_terminated(&sdata->vif,
2506 funcs[i]->instance_id,
2507 NL80211_NAN_FUNC_TERM_REASON_ERROR,
2516 int ieee80211_reconfig(struct ieee80211_local *local)
2518 struct ieee80211_hw *hw = &local->hw;
2519 struct ieee80211_sub_if_data *sdata;
2520 struct ieee80211_chanctx *ctx;
2521 struct sta_info *sta;
2523 bool reconfig_due_to_wowlan = false;
2524 struct ieee80211_sub_if_data *sched_scan_sdata;
2525 struct cfg80211_sched_scan_request *sched_scan_req;
2526 bool sched_scan_stopped = false;
2527 bool suspended = local->suspended;
2528 bool in_reconfig = false;
2530 /* nothing to do if HW shouldn't run */
2531 if (!local->open_count)
2536 local->resuming = true;
2538 if (local->wowlan) {
2540 * In the wowlan case, both mac80211 and the device
2541 * are functional when the resume op is called, so
2542 * clear local->suspended so the device could operate
2543 * normally (e.g. pass rx frames).
2545 local->suspended = false;
2546 res = drv_resume(local);
2547 local->wowlan = false;
2549 local->resuming = false;
2556 * res is 1, which means the driver requested
2557 * to go through a regular reset on wakeup.
2558 * restore local->suspended in this case.
2560 reconfig_due_to_wowlan = true;
2561 local->suspended = true;
2566 * In case of hw_restart during suspend (without wowlan),
2567 * cancel restart work, as we are reconfiguring the device
2569 * Note that restart_work is scheduled on a frozen workqueue,
2570 * so we can't deadlock in this case.
2572 if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
2573 cancel_work_sync(&local->restart_work);
2575 local->started = false;
2578 * Upon resume hardware can sometimes be goofy due to
2579 * various platform / driver / bus issues, so restarting
2580 * the device may at times not work immediately. Propagate
2583 res = drv_start(local);
2586 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2588 WARN(1, "Hardware became unavailable during restart.\n");
2589 ieee80211_handle_reconfig_failure(local);
2593 /* setup fragmentation threshold */
2594 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
2596 /* setup RTS threshold */
2597 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
2599 /* reset coverage class */
2600 drv_set_coverage_class(local, hw->wiphy->coverage_class);
2602 ieee80211_led_radio(local, true);
2603 ieee80211_mod_tpt_led_trig(local,
2604 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2606 /* add interfaces */
2607 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
2609 /* in HW restart it exists already */
2610 WARN_ON(local->resuming);
2611 res = drv_add_interface(local, sdata);
2613 RCU_INIT_POINTER(local->monitor_sdata, NULL);
2619 list_for_each_entry(sdata, &local->interfaces, list) {
2620 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2621 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2622 ieee80211_sdata_running(sdata)) {
2623 res = drv_add_interface(local, sdata);
2629 /* If adding any of the interfaces failed above, roll back and
2633 list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2635 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2636 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2637 ieee80211_sdata_running(sdata))
2638 drv_remove_interface(local, sdata);
2639 ieee80211_handle_reconfig_failure(local);
2643 /* add channel contexts */
2644 if (local->use_chanctx) {
2645 mutex_lock(&local->chanctx_mtx);
2646 list_for_each_entry(ctx, &local->chanctx_list, list)
2647 if (ctx->replace_state !=
2648 IEEE80211_CHANCTX_REPLACES_OTHER)
2649 WARN_ON(drv_add_chanctx(local, ctx));
2650 mutex_unlock(&local->chanctx_mtx);
2652 sdata = wiphy_dereference(local->hw.wiphy,
2653 local->monitor_sdata);
2654 if (sdata && ieee80211_sdata_running(sdata))
2655 ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
2658 /* reconfigure hardware */
2659 ieee80211_hw_config(local, ~0);
2661 ieee80211_configure_filter(local);
2663 /* Finally also reconfigure all the BSS information */
2664 list_for_each_entry(sdata, &local->interfaces, list) {
2665 unsigned int link_id;
2668 if (!ieee80211_sdata_running(sdata))
2673 link_id < ARRAY_SIZE(sdata->vif.link_conf);
2675 struct ieee80211_link_data *link;
2677 link = sdata_dereference(sdata->link[link_id], sdata);
2679 ieee80211_assign_chanctx(local, sdata, link);
2682 switch (sdata->vif.type) {
2683 case NL80211_IFTYPE_AP_VLAN:
2684 case NL80211_IFTYPE_MONITOR:
2686 case NL80211_IFTYPE_ADHOC:
2687 if (sdata->vif.cfg.ibss_joined)
2688 WARN_ON(drv_join_ibss(local, sdata));
2691 ieee80211_reconfig_stations(sdata);
2693 case NL80211_IFTYPE_AP: /* AP stations are handled later */
2694 for (i = 0; i < IEEE80211_NUM_ACS; i++)
2695 drv_conf_tx(local, &sdata->deflink, i,
2696 &sdata->deflink.tx_conf[i]);
2699 sdata_unlock(sdata);
2701 /* common change flags for all interface types */
2702 changed = BSS_CHANGED_ERP_CTS_PROT |
2703 BSS_CHANGED_ERP_PREAMBLE |
2704 BSS_CHANGED_ERP_SLOT |
2706 BSS_CHANGED_BASIC_RATES |
2707 BSS_CHANGED_BEACON_INT |
2712 BSS_CHANGED_TXPOWER |
2713 BSS_CHANGED_MCAST_RATE;
2715 if (sdata->vif.bss_conf.mu_mimo_owner)
2716 changed |= BSS_CHANGED_MU_GROUPS;
2718 switch (sdata->vif.type) {
2719 case NL80211_IFTYPE_STATION:
2720 changed |= BSS_CHANGED_ASSOC |
2721 BSS_CHANGED_ARP_FILTER |
2724 /* Re-send beacon info report to the driver */
2725 if (sdata->deflink.u.mgd.have_beacon)
2726 changed |= BSS_CHANGED_BEACON_INFO;
2728 if (sdata->vif.bss_conf.max_idle_period ||
2729 sdata->vif.bss_conf.protected_keep_alive)
2730 changed |= BSS_CHANGED_KEEP_ALIVE;
2733 ieee80211_bss_info_change_notify(sdata, changed);
2734 sdata_unlock(sdata);
2736 case NL80211_IFTYPE_OCB:
2737 changed |= BSS_CHANGED_OCB;
2738 ieee80211_bss_info_change_notify(sdata, changed);
2740 case NL80211_IFTYPE_ADHOC:
2741 changed |= BSS_CHANGED_IBSS;
2743 case NL80211_IFTYPE_AP:
2744 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2746 if (sdata->vif.bss_conf.ftm_responder == 1 &&
2747 wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2748 NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2749 changed |= BSS_CHANGED_FTM_RESPONDER;
2751 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2752 changed |= BSS_CHANGED_AP_PROBE_RESP;
2754 if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2755 drv_start_ap(local, sdata,
2756 sdata->deflink.conf);
2759 case NL80211_IFTYPE_MESH_POINT:
2760 if (sdata->vif.bss_conf.enable_beacon) {
2761 changed |= BSS_CHANGED_BEACON |
2762 BSS_CHANGED_BEACON_ENABLED;
2763 ieee80211_bss_info_change_notify(sdata, changed);
2766 case NL80211_IFTYPE_NAN:
2767 res = ieee80211_reconfig_nan(sdata);
2769 ieee80211_handle_reconfig_failure(local);
2773 case NL80211_IFTYPE_AP_VLAN:
2774 case NL80211_IFTYPE_MONITOR:
2775 case NL80211_IFTYPE_P2P_DEVICE:
2778 case NL80211_IFTYPE_UNSPECIFIED:
2779 case NUM_NL80211_IFTYPES:
2780 case NL80211_IFTYPE_P2P_CLIENT:
2781 case NL80211_IFTYPE_P2P_GO:
2782 case NL80211_IFTYPE_WDS:
2788 ieee80211_recalc_ps(local);
2791 * The sta might be in psm against the ap (e.g. because
2792 * this was the state before a hw restart), so we
2793 * explicitly send a null packet in order to make sure
2794 * it'll sync against the ap (and get out of psm).
2796 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2797 list_for_each_entry(sdata, &local->interfaces, list) {
2798 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2800 if (!sdata->u.mgd.associated)
2803 ieee80211_send_nullfunc(local, sdata, false);
2807 /* APs are now beaconing, add back stations */
2808 list_for_each_entry(sdata, &local->interfaces, list) {
2809 if (!ieee80211_sdata_running(sdata))
2813 switch (sdata->vif.type) {
2814 case NL80211_IFTYPE_AP_VLAN:
2815 case NL80211_IFTYPE_AP:
2816 ieee80211_reconfig_stations(sdata);
2821 sdata_unlock(sdata);
2825 list_for_each_entry(sdata, &local->interfaces, list)
2826 ieee80211_reenable_keys(sdata);
2828 /* Reconfigure sched scan if it was interrupted by FW restart */
2829 mutex_lock(&local->mtx);
2830 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2831 lockdep_is_held(&local->mtx));
2832 sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2833 lockdep_is_held(&local->mtx));
2834 if (sched_scan_sdata && sched_scan_req)
2836 * Sched scan stopped, but we don't want to report it. Instead,
2837 * we're trying to reschedule. However, if more than one scan
2838 * plan was set, we cannot reschedule since we don't know which
2839 * scan plan was currently running (and some scan plans may have
2840 * already finished).
2842 if (sched_scan_req->n_scan_plans > 1 ||
2843 __ieee80211_request_sched_scan_start(sched_scan_sdata,
2845 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2846 RCU_INIT_POINTER(local->sched_scan_req, NULL);
2847 sched_scan_stopped = true;
2849 mutex_unlock(&local->mtx);
2851 if (sched_scan_stopped)
2852 cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2856 if (local->monitors == local->open_count && local->monitors > 0)
2857 ieee80211_add_virtual_monitor(local);
2860 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2861 * sessions can be established after a resume.
2863 * Also tear down aggregation sessions since reconfiguring
2864 * them in a hardware restart scenario is not easily done
2865 * right now, and the hardware will have lost information
2866 * about the sessions, but we and the AP still think they
2867 * are active. This is really a workaround though.
2869 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2870 mutex_lock(&local->sta_mtx);
2872 list_for_each_entry(sta, &local->sta_list, list) {
2873 if (!local->resuming)
2874 ieee80211_sta_tear_down_BA_sessions(
2875 sta, AGG_STOP_LOCAL_REQUEST);
2876 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2879 mutex_unlock(&local->sta_mtx);
2883 * If this is for hw restart things are still running.
2884 * We may want to change that later, however.
2886 if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2887 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2889 if (local->in_reconfig) {
2890 in_reconfig = local->in_reconfig;
2891 local->in_reconfig = false;
2894 /* Restart deferred ROCs */
2895 mutex_lock(&local->mtx);
2896 ieee80211_start_next_roc(local);
2897 mutex_unlock(&local->mtx);
2899 /* Requeue all works */
2900 list_for_each_entry(sdata, &local->interfaces, list)
2901 ieee80211_queue_work(&local->hw, &sdata->work);
2904 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2905 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2909 list_for_each_entry(sdata, &local->interfaces, list) {
2910 if (!ieee80211_sdata_running(sdata))
2912 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2913 ieee80211_sta_restart(sdata);
2921 /* first set suspended false, then resuming */
2922 local->suspended = false;
2924 local->resuming = false;
2926 ieee80211_flush_completed_scan(local, false);
2928 if (local->open_count && !reconfig_due_to_wowlan)
2929 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2931 list_for_each_entry(sdata, &local->interfaces, list) {
2932 if (!ieee80211_sdata_running(sdata))
2934 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2935 ieee80211_sta_restart(sdata);
2938 mod_timer(&local->sta_cleanup, jiffies + 1);
2946 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2948 struct ieee80211_sub_if_data *sdata;
2949 struct ieee80211_local *local;
2950 struct ieee80211_key *key;
2955 sdata = vif_to_sdata(vif);
2956 local = sdata->local;
2958 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2962 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2963 !local->in_reconfig))
2966 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2969 sdata->flags |= flag;
2971 mutex_lock(&local->key_mtx);
2972 list_for_each_entry(key, &sdata->key_list, list)
2973 key->flags |= KEY_FLAG_TAINTED;
2974 mutex_unlock(&local->key_mtx);
2977 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
2979 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
2981 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
2983 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2985 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
2987 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2989 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
2990 struct ieee80211_link_data *link)
2992 struct ieee80211_local *local = sdata->local;
2993 struct ieee80211_chanctx_conf *chanctx_conf;
2994 struct ieee80211_chanctx *chanctx;
2996 mutex_lock(&local->chanctx_mtx);
2998 chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
2999 lockdep_is_held(&local->chanctx_mtx));
3002 * This function can be called from a work, thus it may be possible
3003 * that the chanctx_conf is removed (due to a disconnection, for
3005 * So nothing should be done in such case.
3010 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
3011 ieee80211_recalc_smps_chanctx(local, chanctx);
3013 mutex_unlock(&local->chanctx_mtx);
3016 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
3019 struct ieee80211_local *local = sdata->local;
3020 struct ieee80211_chanctx_conf *chanctx_conf;
3021 struct ieee80211_chanctx *chanctx;
3024 mutex_lock(&local->chanctx_mtx);
3026 for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
3027 struct ieee80211_bss_conf *bss_conf;
3029 if (link_id >= 0 && link_id != i)
3033 bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
3039 chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
3040 lockdep_is_held(&local->chanctx_mtx));
3042 * Since we hold the chanctx_mtx (checked above)
3043 * we can take the chanctx_conf pointer out of the
3044 * RCU critical section, it cannot go away without
3045 * the mutex. Just the way we reached it could - in
3046 * theory - go away, but we don't really care and
3047 * it really shouldn't happen anyway.
3054 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
3056 ieee80211_recalc_chanctx_min_def(local, chanctx);
3059 mutex_unlock(&local->chanctx_mtx);
3062 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
3064 size_t pos = offset;
3066 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
3067 pos += 2 + ies[pos + 1];
3072 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3077 *pos++ = WLAN_EID_HT_CAPABILITY;
3078 *pos++ = sizeof(struct ieee80211_ht_cap);
3079 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
3081 /* capability flags */
3082 tmp = cpu_to_le16(cap);
3083 memcpy(pos, &tmp, sizeof(u16));
3086 /* AMPDU parameters */
3087 *pos++ = ht_cap->ampdu_factor |
3088 (ht_cap->ampdu_density <<
3089 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
3092 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
3093 pos += sizeof(ht_cap->mcs);
3095 /* extended capabilities */
3096 pos += sizeof(__le16);
3098 /* BF capabilities */
3099 pos += sizeof(__le32);
3101 /* antenna selection */
3107 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3112 *pos++ = WLAN_EID_VHT_CAPABILITY;
3113 *pos++ = sizeof(struct ieee80211_vht_cap);
3114 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
3116 /* capability flags */
3117 tmp = cpu_to_le32(cap);
3118 memcpy(pos, &tmp, sizeof(u32));
3122 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
3123 pos += sizeof(vht_cap->vht_mcs);
3128 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
3130 const struct ieee80211_sta_he_cap *he_cap;
3131 struct ieee80211_supported_band *sband;
3134 sband = ieee80211_get_sband(sdata);
3138 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3142 n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
3144 sizeof(he_cap->he_cap_elem) + n +
3145 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3146 he_cap->he_cap_elem.phy_cap_info);
3149 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
3150 const struct ieee80211_sta_he_cap *he_cap,
3153 struct ieee80211_he_cap_elem elem;
3158 /* Make sure we have place for the IE */
3160 * TODO: the 1 added is because this temporarily is under the EXTENSION
3161 * IE. Get rid of it when it moves.
3166 /* modify on stack first to calculate 'n' and 'ie_len' correctly */
3167 elem = he_cap->he_cap_elem;
3169 if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
3170 elem.phy_cap_info[0] &=
3171 ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3172 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
3174 if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
3175 elem.phy_cap_info[0] &=
3176 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3178 if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
3179 elem.phy_cap_info[0] &=
3180 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3182 n = ieee80211_he_mcs_nss_size(&elem);
3184 sizeof(he_cap->he_cap_elem) + n +
3185 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3186 he_cap->he_cap_elem.phy_cap_info);
3188 if ((end - pos) < ie_len)
3191 *pos++ = WLAN_EID_EXTENSION;
3192 pos++; /* We'll set the size later below */
3193 *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
3196 memcpy(pos, &elem, sizeof(elem));
3197 pos += sizeof(elem);
3199 memcpy(pos, &he_cap->he_mcs_nss_supp, n);
3202 /* Check if PPE Threshold should be present */
3203 if ((he_cap->he_cap_elem.phy_cap_info[6] &
3204 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
3208 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
3209 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
3211 n = hweight8(he_cap->ppe_thres[0] &
3212 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
3213 n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
3214 IEEE80211_PPE_THRES_NSS_POS));
3217 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
3220 n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
3221 n = DIV_ROUND_UP(n, 8);
3223 /* Copy PPE Thresholds */
3224 memcpy(pos, &he_cap->ppe_thres, n);
3228 orig_pos[1] = (pos - orig_pos) - 2;
3232 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
3233 enum ieee80211_smps_mode smps_mode,
3234 struct sk_buff *skb)
3236 struct ieee80211_supported_band *sband;
3237 const struct ieee80211_sband_iftype_data *iftd;
3238 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3242 if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
3243 BIT(NL80211_BAND_6GHZ),
3244 IEEE80211_CHAN_NO_HE))
3247 sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3249 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
3253 /* Check for device HE 6 GHz capability before adding element */
3254 if (!iftd->he_6ghz_capa.capa)
3257 cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
3258 cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
3260 switch (smps_mode) {
3261 case IEEE80211_SMPS_AUTOMATIC:
3262 case IEEE80211_SMPS_NUM_MODES:
3265 case IEEE80211_SMPS_OFF:
3266 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
3267 IEEE80211_HE_6GHZ_CAP_SM_PS);
3269 case IEEE80211_SMPS_STATIC:
3270 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
3271 IEEE80211_HE_6GHZ_CAP_SM_PS);
3273 case IEEE80211_SMPS_DYNAMIC:
3274 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
3275 IEEE80211_HE_6GHZ_CAP_SM_PS);
3279 pos = skb_put(skb, 2 + 1 + sizeof(cap));
3280 ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
3281 pos + 2 + 1 + sizeof(cap));
3284 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3285 const struct cfg80211_chan_def *chandef,
3286 u16 prot_mode, bool rifs_mode)
3288 struct ieee80211_ht_operation *ht_oper;
3289 /* Build HT Information */
3290 *pos++ = WLAN_EID_HT_OPERATION;
3291 *pos++ = sizeof(struct ieee80211_ht_operation);
3292 ht_oper = (struct ieee80211_ht_operation *)pos;
3293 ht_oper->primary_chan = ieee80211_frequency_to_channel(
3294 chandef->chan->center_freq);
3295 switch (chandef->width) {
3296 case NL80211_CHAN_WIDTH_160:
3297 case NL80211_CHAN_WIDTH_80P80:
3298 case NL80211_CHAN_WIDTH_80:
3299 case NL80211_CHAN_WIDTH_40:
3300 if (chandef->center_freq1 > chandef->chan->center_freq)
3301 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3303 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3305 case NL80211_CHAN_WIDTH_320:
3306 /* HT information element should not be included on 6GHz */
3310 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
3313 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
3314 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
3315 chandef->width != NL80211_CHAN_WIDTH_20)
3316 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
3319 ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
3321 ht_oper->operation_mode = cpu_to_le16(prot_mode);
3322 ht_oper->stbc_param = 0x0000;
3324 /* It seems that Basic MCS set and Supported MCS set
3325 are identical for the first 10 bytes */
3326 memset(&ht_oper->basic_set, 0, 16);
3327 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
3329 return pos + sizeof(struct ieee80211_ht_operation);
3332 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
3333 const struct cfg80211_chan_def *chandef)
3335 *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
3336 *pos++ = 3; /* IE length */
3337 /* New channel width */
3338 switch (chandef->width) {
3339 case NL80211_CHAN_WIDTH_80:
3340 *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
3342 case NL80211_CHAN_WIDTH_160:
3343 *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
3345 case NL80211_CHAN_WIDTH_80P80:
3346 *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
3348 case NL80211_CHAN_WIDTH_320:
3349 /* The behavior is not defined for 320 MHz channels */
3353 *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
3356 /* new center frequency segment 0 */
3357 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
3358 /* new center frequency segment 1 */
3359 if (chandef->center_freq2)
3360 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
3365 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3366 const struct cfg80211_chan_def *chandef)
3368 struct ieee80211_vht_operation *vht_oper;
3370 *pos++ = WLAN_EID_VHT_OPERATION;
3371 *pos++ = sizeof(struct ieee80211_vht_operation);
3372 vht_oper = (struct ieee80211_vht_operation *)pos;
3373 vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
3374 chandef->center_freq1);
3375 if (chandef->center_freq2)
3376 vht_oper->center_freq_seg1_idx =
3377 ieee80211_frequency_to_channel(chandef->center_freq2);
3379 vht_oper->center_freq_seg1_idx = 0x00;
3381 switch (chandef->width) {
3382 case NL80211_CHAN_WIDTH_160:
3384 * Convert 160 MHz channel width to new style as interop
3387 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3388 vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
3389 if (chandef->chan->center_freq < chandef->center_freq1)
3390 vht_oper->center_freq_seg0_idx -= 8;
3392 vht_oper->center_freq_seg0_idx += 8;
3394 case NL80211_CHAN_WIDTH_80P80:
3396 * Convert 80+80 MHz channel width to new style as interop
3399 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3401 case NL80211_CHAN_WIDTH_80:
3402 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3404 case NL80211_CHAN_WIDTH_320:
3405 /* VHT information element should not be included on 6GHz */
3409 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
3413 /* don't require special VHT peer rates */
3414 vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
3416 return pos + sizeof(struct ieee80211_vht_operation);
3419 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
3421 struct ieee80211_he_operation *he_oper;
3422 struct ieee80211_he_6ghz_oper *he_6ghz_op;
3424 u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
3426 if (chandef->chan->band == NL80211_BAND_6GHZ)
3427 ie_len += sizeof(struct ieee80211_he_6ghz_oper);
3429 *pos++ = WLAN_EID_EXTENSION;
3431 *pos++ = WLAN_EID_EXT_HE_OPERATION;
3434 he_oper_params |= u32_encode_bits(1023, /* disabled */
3435 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3436 he_oper_params |= u32_encode_bits(1,
3437 IEEE80211_HE_OPERATION_ER_SU_DISABLE);
3438 he_oper_params |= u32_encode_bits(1,
3439 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3440 if (chandef->chan->band == NL80211_BAND_6GHZ)
3441 he_oper_params |= u32_encode_bits(1,
3442 IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
3444 he_oper = (struct ieee80211_he_operation *)pos;
3445 he_oper->he_oper_params = cpu_to_le32(he_oper_params);
3447 /* don't require special HE peer rates */
3448 he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
3449 pos += sizeof(struct ieee80211_he_operation);
3451 if (chandef->chan->band != NL80211_BAND_6GHZ)
3454 /* TODO add VHT operational */
3455 he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
3456 he_6ghz_op->minrate = 6; /* 6 Mbps */
3457 he_6ghz_op->primary =
3458 ieee80211_frequency_to_channel(chandef->chan->center_freq);
3460 ieee80211_frequency_to_channel(chandef->center_freq1);
3461 if (chandef->center_freq2)
3463 ieee80211_frequency_to_channel(chandef->center_freq2);
3465 he_6ghz_op->ccfs1 = 0;
3467 switch (chandef->width) {
3468 case NL80211_CHAN_WIDTH_320:
3470 * TODO: mesh operation is not defined over 6GHz 320 MHz
3475 case NL80211_CHAN_WIDTH_160:
3476 /* Convert 160 MHz channel width to new style as interop
3479 he_6ghz_op->control =
3480 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3481 he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
3482 if (chandef->chan->center_freq < chandef->center_freq1)
3483 he_6ghz_op->ccfs0 -= 8;
3485 he_6ghz_op->ccfs0 += 8;
3487 case NL80211_CHAN_WIDTH_80P80:
3488 he_6ghz_op->control =
3489 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3491 case NL80211_CHAN_WIDTH_80:
3492 he_6ghz_op->control =
3493 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
3495 case NL80211_CHAN_WIDTH_40:
3496 he_6ghz_op->control =
3497 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3500 he_6ghz_op->control =
3501 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3505 pos += sizeof(struct ieee80211_he_6ghz_oper);
3511 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3512 struct cfg80211_chan_def *chandef)
3514 enum nl80211_channel_type channel_type;
3519 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3520 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3521 channel_type = NL80211_CHAN_HT20;
3523 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3524 channel_type = NL80211_CHAN_HT40PLUS;
3526 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3527 channel_type = NL80211_CHAN_HT40MINUS;
3533 cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3537 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3538 const struct ieee80211_vht_operation *oper,
3539 const struct ieee80211_ht_operation *htop,
3540 struct cfg80211_chan_def *chandef)
3542 struct cfg80211_chan_def new = *chandef;
3544 int ccfs0, ccfs1, ccfs2;
3547 bool support_80_80 = false;
3548 bool support_160 = false;
3549 u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3550 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3551 u8 supp_chwidth = u32_get_bits(vht_cap_info,
3552 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3557 vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3558 support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3559 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3560 support_80_80 = ((vht_cap &
3561 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3562 (vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3563 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3564 ((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3565 IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3566 ccfs0 = oper->center_freq_seg0_idx;
3567 ccfs1 = oper->center_freq_seg1_idx;
3568 ccfs2 = (le16_to_cpu(htop->operation_mode) &
3569 IEEE80211_HT_OP_MODE_CCFS2_MASK)
3570 >> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3574 /* if not supported, parse as though we didn't understand it */
3575 if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3576 ext_nss_bw_supp = 0;
3579 * Cf. IEEE 802.11 Table 9-250
3581 * We really just consider that because it's inefficient to connect
3582 * at a higher bandwidth than we'll actually be able to use.
3584 switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3588 support_160 = false;
3589 support_80_80 = false;
3592 support_80_80 = false;
3615 cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3616 cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3618 switch (oper->chan_width) {
3619 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3620 /* just use HT information directly */
3622 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3623 new.width = NL80211_CHAN_WIDTH_80;
3624 new.center_freq1 = cf0;
3625 /* If needed, adjust based on the newer interop workaround. */
3629 diff = abs(ccf1 - ccf0);
3630 if ((diff == 8) && support_160) {
3631 new.width = NL80211_CHAN_WIDTH_160;
3632 new.center_freq1 = cf1;
3633 } else if ((diff > 8) && support_80_80) {
3634 new.width = NL80211_CHAN_WIDTH_80P80;
3635 new.center_freq2 = cf1;
3639 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3640 /* deprecated encoding */
3641 new.width = NL80211_CHAN_WIDTH_160;
3642 new.center_freq1 = cf0;
3644 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3645 /* deprecated encoding */
3646 new.width = NL80211_CHAN_WIDTH_80P80;
3647 new.center_freq1 = cf0;
3648 new.center_freq2 = cf1;
3654 if (!cfg80211_chandef_valid(&new))
3661 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
3662 bool support_160, bool support_320,
3663 struct cfg80211_chan_def *chandef)
3665 struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
3667 chandef->center_freq1 =
3668 ieee80211_channel_to_frequency(info->ccfs0,
3669 chandef->chan->band);
3671 switch (u8_get_bits(info->control,
3672 IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3673 case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3674 chandef->width = NL80211_CHAN_WIDTH_20;
3676 case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3677 chandef->width = NL80211_CHAN_WIDTH_40;
3679 case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3680 chandef->width = NL80211_CHAN_WIDTH_80;
3682 case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3684 chandef->width = NL80211_CHAN_WIDTH_160;
3685 chandef->center_freq1 =
3686 ieee80211_channel_to_frequency(info->ccfs1,
3687 chandef->chan->band);
3689 chandef->width = NL80211_CHAN_WIDTH_80;
3692 case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3694 chandef->width = NL80211_CHAN_WIDTH_320;
3695 chandef->center_freq1 =
3696 ieee80211_channel_to_frequency(info->ccfs1,
3697 chandef->chan->band);
3698 } else if (support_160) {
3699 chandef->width = NL80211_CHAN_WIDTH_160;
3701 chandef->width = NL80211_CHAN_WIDTH_80;
3703 if (chandef->center_freq1 > chandef->chan->center_freq)
3704 chandef->center_freq1 -= 40;
3706 chandef->center_freq1 += 40;
3712 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
3713 const struct ieee80211_he_operation *he_oper,
3714 const struct ieee80211_eht_operation *eht_oper,
3715 struct cfg80211_chan_def *chandef)
3717 struct ieee80211_local *local = sdata->local;
3718 struct ieee80211_supported_band *sband;
3719 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3720 const struct ieee80211_sta_he_cap *he_cap;
3721 const struct ieee80211_sta_eht_cap *eht_cap;
3722 struct cfg80211_chan_def he_chandef = *chandef;
3723 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3724 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3725 bool support_80_80, support_160, support_320;
3726 u8 he_phy_cap, eht_phy_cap;
3729 if (chandef->chan->band != NL80211_BAND_6GHZ)
3732 sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3734 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3736 sdata_info(sdata, "Missing iftype sband data/HE cap");
3740 he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
3743 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3746 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3750 "HE is not advertised on (on %d MHz), expect issues\n",
3751 chandef->chan->center_freq);
3755 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
3757 sdata_info(sdata, "Missing iftype sband data/EHT cap");
3761 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3763 if (!he_6ghz_oper) {
3765 "HE 6GHz operation missing (on %d MHz), expect issues\n",
3766 chandef->chan->center_freq);
3771 * The EHT operation IE does not contain the primary channel so the
3772 * primary channel frequency should be taken from the 6 GHz operation
3775 freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3777 he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
3779 switch (u8_get_bits(he_6ghz_oper->control,
3780 IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
3781 case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
3782 bss_conf->power_type = IEEE80211_REG_LPI_AP;
3784 case IEEE80211_6GHZ_CTRL_REG_SP_AP:
3785 bss_conf->power_type = IEEE80211_REG_SP_AP;
3788 bss_conf->power_type = IEEE80211_REG_UNSET_AP;
3793 !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3794 switch (u8_get_bits(he_6ghz_oper->control,
3795 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3796 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3797 he_chandef.width = NL80211_CHAN_WIDTH_20;
3799 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3800 he_chandef.width = NL80211_CHAN_WIDTH_40;
3802 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3803 he_chandef.width = NL80211_CHAN_WIDTH_80;
3805 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3806 he_chandef.width = NL80211_CHAN_WIDTH_80;
3807 if (!he_6ghz_oper->ccfs1)
3809 if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
3811 he_chandef.width = NL80211_CHAN_WIDTH_160;
3814 he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3819 if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3820 he_chandef.center_freq1 =
3821 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3824 he_chandef.center_freq1 =
3825 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3827 if (support_80_80 || support_160)
3828 he_chandef.center_freq2 =
3829 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3833 eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
3835 eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
3837 ieee80211_chandef_eht_oper(eht_oper, support_160,
3838 support_320, &he_chandef);
3841 if (!cfg80211_chandef_valid(&he_chandef)) {
3843 "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
3844 he_chandef.chan ? he_chandef.chan->center_freq : 0,
3846 he_chandef.center_freq1,
3847 he_chandef.center_freq2);
3851 *chandef = he_chandef;
3856 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
3857 struct cfg80211_chan_def *chandef)
3864 switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3865 case IEEE80211_S1G_CHANWIDTH_1MHZ:
3866 chandef->width = NL80211_CHAN_WIDTH_1;
3868 case IEEE80211_S1G_CHANWIDTH_2MHZ:
3869 chandef->width = NL80211_CHAN_WIDTH_2;
3871 case IEEE80211_S1G_CHANWIDTH_4MHZ:
3872 chandef->width = NL80211_CHAN_WIDTH_4;
3874 case IEEE80211_S1G_CHANWIDTH_8MHZ:
3875 chandef->width = NL80211_CHAN_WIDTH_8;
3877 case IEEE80211_S1G_CHANWIDTH_16MHZ:
3878 chandef->width = NL80211_CHAN_WIDTH_16;
3884 oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
3885 NL80211_BAND_S1GHZ);
3886 chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
3887 chandef->freq1_offset = oper_freq % 1000;
3892 int ieee80211_parse_bitrates(enum nl80211_chan_width width,
3893 const struct ieee80211_supported_band *sband,
3894 const u8 *srates, int srates_len, u32 *rates)
3896 u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
3897 int shift = ieee80211_chanwidth_get_shift(width);
3898 struct ieee80211_rate *br;
3899 int brate, rate, i, j, count = 0;
3903 for (i = 0; i < srates_len; i++) {
3904 rate = srates[i] & 0x7f;
3906 for (j = 0; j < sband->n_bitrates; j++) {
3907 br = &sband->bitrates[j];
3908 if ((rate_flags & br->flags) != rate_flags)
3911 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3912 if (brate == rate) {
3922 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
3923 struct sk_buff *skb, bool need_basic,
3924 enum nl80211_band band)
3926 struct ieee80211_local *local = sdata->local;
3927 struct ieee80211_supported_band *sband;
3930 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3933 shift = ieee80211_vif_get_shift(&sdata->vif);
3934 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3935 sband = local->hw.wiphy->bands[band];
3937 for (i = 0; i < sband->n_bitrates; i++) {
3938 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3945 if (skb_tailroom(skb) < rates + 2)
3948 pos = skb_put(skb, rates + 2);
3949 *pos++ = WLAN_EID_SUPP_RATES;
3951 for (i = 0; i < rates; i++) {
3953 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3956 if (need_basic && basic_rates & BIT(i))
3958 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
3960 *pos++ = basic | (u8) rate;
3966 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
3967 struct sk_buff *skb, bool need_basic,
3968 enum nl80211_band band)
3970 struct ieee80211_local *local = sdata->local;
3971 struct ieee80211_supported_band *sband;
3973 u8 i, exrates, *pos;
3974 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3977 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3978 shift = ieee80211_vif_get_shift(&sdata->vif);
3980 sband = local->hw.wiphy->bands[band];
3982 for (i = 0; i < sband->n_bitrates; i++) {
3983 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3993 if (skb_tailroom(skb) < exrates + 2)
3997 pos = skb_put(skb, exrates + 2);
3998 *pos++ = WLAN_EID_EXT_SUPP_RATES;
4000 for (i = 8; i < sband->n_bitrates; i++) {
4002 if ((rate_flags & sband->bitrates[i].flags)
4005 if (need_basic && basic_rates & BIT(i))
4007 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4009 *pos++ = basic | (u8) rate;
4015 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
4017 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4019 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
4022 return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
4024 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
4026 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
4031 /* TODO: consider rx_highest */
4033 if (mcs->rx_mask[3])
4035 if (mcs->rx_mask[2])
4037 if (mcs->rx_mask[1])
4043 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
4044 * @local: mac80211 hw info struct
4045 * @status: RX status
4046 * @mpdu_len: total MPDU length (including FCS)
4047 * @mpdu_offset: offset into MPDU to calculate timestamp at
4049 * This function calculates the RX timestamp at the given MPDU offset, taking
4050 * into account what the RX timestamp was. An offset of 0 will just normalize
4051 * the timestamp to TSF at beginning of MPDU reception.
4053 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
4054 struct ieee80211_rx_status *status,
4055 unsigned int mpdu_len,
4056 unsigned int mpdu_offset)
4058 u64 ts = status->mactime;
4059 struct rate_info ri;
4063 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
4066 memset(&ri, 0, sizeof(ri));
4070 /* Fill cfg80211 rate info */
4071 switch (status->encoding) {
4073 ri.flags |= RATE_INFO_FLAGS_HE_MCS;
4074 ri.mcs = status->rate_idx;
4075 ri.nss = status->nss;
4076 ri.he_ru_alloc = status->he_ru;
4077 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4078 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4081 * See P802.11ax_D6.0, section 27.3.4 for
4084 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4090 * For HE MU PPDU, add the HE-SIG-B.
4091 * For HE ER PPDU, add 8us for the HE-SIG-A.
4092 * For HE TB PPDU, add 4us for the HE-STF.
4093 * Add the HE-LTF durations - variable.
4099 ri.mcs = status->rate_idx;
4100 ri.flags |= RATE_INFO_FLAGS_MCS;
4101 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4102 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4105 * See P802.11REVmd_D3.0, section 19.3.2 for
4108 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4110 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
4116 * Add Data HT-LTFs per streams
4117 * TODO: add Extension HT-LTFs, 4us per LTF
4119 n_ltf = ((ri.mcs >> 3) & 3) + 1;
4120 n_ltf = n_ltf == 3 ? 4 : n_ltf;
4126 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
4127 ri.mcs = status->rate_idx;
4128 ri.nss = status->nss;
4129 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4130 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4133 * See P802.11REVmd_D3.0, section 21.3.2 for
4136 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4141 * Add VHT-LTFs per streams
4143 n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
4144 ri.nss + 1 : ri.nss;
4152 case RX_ENC_LEGACY: {
4153 struct ieee80211_supported_band *sband;
4157 switch (status->bw) {
4158 case RATE_INFO_BW_10:
4161 case RATE_INFO_BW_5:
4166 sband = local->hw.wiphy->bands[status->band];
4167 bitrate = sband->bitrates[status->rate_idx].bitrate;
4168 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
4170 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4171 if (status->band == NL80211_BAND_5GHZ) {
4174 } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
4184 rate = cfg80211_calculate_bitrate(&ri);
4185 if (WARN_ONCE(!rate,
4186 "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
4187 (unsigned long long)status->flag, status->rate_idx,
4191 /* rewind from end of MPDU */
4192 if (status->flag & RX_FLAG_MACTIME_END)
4193 ts -= mpdu_len * 8 * 10 / rate;
4195 ts += mpdu_offset * 8 * 10 / rate;
4200 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
4202 struct ieee80211_sub_if_data *sdata;
4203 struct cfg80211_chan_def chandef;
4205 /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
4206 lockdep_assert_wiphy(local->hw.wiphy);
4208 mutex_lock(&local->mtx);
4209 list_for_each_entry(sdata, &local->interfaces, list) {
4210 /* it might be waiting for the local->mtx, but then
4211 * by the time it gets it, sdata->wdev.cac_started
4212 * will no longer be true
4214 cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
4216 if (sdata->wdev.cac_started) {
4217 chandef = sdata->vif.bss_conf.chandef;
4218 ieee80211_link_release_channel(&sdata->deflink);
4219 cfg80211_cac_event(sdata->dev,
4221 NL80211_RADAR_CAC_ABORTED,
4225 mutex_unlock(&local->mtx);
4228 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
4230 struct ieee80211_local *local =
4231 container_of(work, struct ieee80211_local, radar_detected_work);
4232 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
4233 struct ieee80211_chanctx *ctx;
4234 int num_chanctx = 0;
4236 mutex_lock(&local->chanctx_mtx);
4237 list_for_each_entry(ctx, &local->chanctx_list, list) {
4238 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
4242 chandef = ctx->conf.def;
4244 mutex_unlock(&local->chanctx_mtx);
4246 wiphy_lock(local->hw.wiphy);
4247 ieee80211_dfs_cac_cancel(local);
4248 wiphy_unlock(local->hw.wiphy);
4250 if (num_chanctx > 1)
4251 /* XXX: multi-channel is not supported yet */
4254 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
4257 void ieee80211_radar_detected(struct ieee80211_hw *hw)
4259 struct ieee80211_local *local = hw_to_local(hw);
4261 trace_api_radar_detected(local);
4263 schedule_work(&local->radar_detected_work);
4265 EXPORT_SYMBOL(ieee80211_radar_detected);
4267 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
4269 ieee80211_conn_flags_t ret;
4273 case NL80211_CHAN_WIDTH_20:
4274 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4275 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4277 case NL80211_CHAN_WIDTH_40:
4278 c->width = NL80211_CHAN_WIDTH_20;
4279 c->center_freq1 = c->chan->center_freq;
4280 ret = IEEE80211_CONN_DISABLE_40MHZ |
4281 IEEE80211_CONN_DISABLE_VHT;
4283 case NL80211_CHAN_WIDTH_80:
4284 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
4288 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
4289 c->width = NL80211_CHAN_WIDTH_40;
4290 ret = IEEE80211_CONN_DISABLE_VHT;
4292 case NL80211_CHAN_WIDTH_80P80:
4293 c->center_freq2 = 0;
4294 c->width = NL80211_CHAN_WIDTH_80;
4295 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4296 IEEE80211_CONN_DISABLE_160MHZ;
4298 case NL80211_CHAN_WIDTH_160:
4300 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
4303 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
4304 c->width = NL80211_CHAN_WIDTH_80;
4305 ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4306 IEEE80211_CONN_DISABLE_160MHZ;
4308 case NL80211_CHAN_WIDTH_320:
4310 tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
4313 c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
4314 c->width = NL80211_CHAN_WIDTH_160;
4315 ret = IEEE80211_CONN_DISABLE_320MHZ;
4318 case NL80211_CHAN_WIDTH_20_NOHT:
4320 c->width = NL80211_CHAN_WIDTH_20_NOHT;
4321 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4323 case NL80211_CHAN_WIDTH_1:
4324 case NL80211_CHAN_WIDTH_2:
4325 case NL80211_CHAN_WIDTH_4:
4326 case NL80211_CHAN_WIDTH_8:
4327 case NL80211_CHAN_WIDTH_16:
4328 case NL80211_CHAN_WIDTH_5:
4329 case NL80211_CHAN_WIDTH_10:
4332 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4336 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
4342 * Returns true if smps_mode_new is strictly more restrictive than
4345 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
4346 enum ieee80211_smps_mode smps_mode_new)
4348 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
4349 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
4352 switch (smps_mode_old) {
4353 case IEEE80211_SMPS_STATIC:
4355 case IEEE80211_SMPS_DYNAMIC:
4356 return smps_mode_new == IEEE80211_SMPS_STATIC;
4357 case IEEE80211_SMPS_OFF:
4358 return smps_mode_new != IEEE80211_SMPS_OFF;
4366 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
4367 struct cfg80211_csa_settings *csa_settings)
4369 struct sk_buff *skb;
4370 struct ieee80211_mgmt *mgmt;
4371 struct ieee80211_local *local = sdata->local;
4373 int hdr_len = offsetofend(struct ieee80211_mgmt,
4374 u.action.u.chan_switch);
4377 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4378 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4381 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
4382 5 + /* channel switch announcement element */
4383 3 + /* secondary channel offset element */
4384 5 + /* wide bandwidth channel switch announcement */
4385 8); /* mesh channel switch parameters element */
4389 skb_reserve(skb, local->tx_headroom);
4390 mgmt = skb_put_zero(skb, hdr_len);
4391 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4392 IEEE80211_STYPE_ACTION);
4394 eth_broadcast_addr(mgmt->da);
4395 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
4396 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4397 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
4399 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4400 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
4402 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
4403 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
4404 pos = skb_put(skb, 5);
4405 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
4406 *pos++ = 3; /* IE length */
4407 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
4408 freq = csa_settings->chandef.chan->center_freq;
4409 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
4410 *pos++ = csa_settings->count; /* count */
4412 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
4413 enum nl80211_channel_type ch_type;
4416 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
4417 *pos++ = 1; /* IE length */
4418 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
4419 if (ch_type == NL80211_CHAN_HT40PLUS)
4420 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
4422 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
4425 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4426 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4429 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
4430 *pos++ = 6; /* IE length */
4431 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
4432 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
4433 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
4434 *pos++ |= csa_settings->block_tx ?
4435 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
4436 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
4438 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
4442 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
4443 csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
4444 csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
4446 ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
4449 ieee80211_tx_skb(sdata, skb);
4454 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
4456 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
4463 if (data->count[i] == 1)
4466 if (data->desc[i].interval == 0)
4469 /* End time is in the past, check for repetitions */
4470 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
4471 if (data->count[i] < 255) {
4472 if (data->count[i] <= skip) {
4477 data->count[i] -= skip;
4480 data->desc[i].start += skip * data->desc[i].interval;
4486 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
4492 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4495 if (!data->count[i])
4498 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
4501 cur = data->desc[i].start - tsf;
4505 cur = data->desc[i].start + data->desc[i].duration - tsf;
4514 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
4519 * arbitrary limit, used to avoid infinite loops when combined NoA
4520 * descriptors cover the full time period.
4524 ieee80211_extend_absent_time(data, tsf, &offset);
4526 if (!ieee80211_extend_absent_time(data, tsf, &offset))
4530 } while (tries < max_tries);
4535 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4537 u32 next_offset = BIT(31) - 1;
4541 data->has_next_tsf = false;
4542 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4545 if (!data->count[i])
4548 ieee80211_extend_noa_desc(data, tsf, i);
4549 start = data->desc[i].start - tsf;
4551 data->absent |= BIT(i);
4553 if (next_offset > start)
4554 next_offset = start;
4556 data->has_next_tsf = true;
4560 next_offset = ieee80211_get_noa_absent_time(data, tsf);
4562 data->next_tsf = tsf + next_offset;
4564 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4566 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4567 struct ieee80211_noa_data *data, u32 tsf)
4572 memset(data, 0, sizeof(*data));
4574 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4575 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4577 if (!desc->count || !desc->duration)
4580 data->count[i] = desc->count;
4581 data->desc[i].start = le32_to_cpu(desc->start_time);
4582 data->desc[i].duration = le32_to_cpu(desc->duration);
4583 data->desc[i].interval = le32_to_cpu(desc->interval);
4585 if (data->count[i] > 1 &&
4586 data->desc[i].interval < data->desc[i].duration)
4589 ieee80211_extend_noa_desc(data, tsf, i);
4594 ieee80211_update_p2p_noa(data, tsf);
4598 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4600 void ieee80211_recalc_dtim(struct ieee80211_local *local,
4601 struct ieee80211_sub_if_data *sdata)
4603 u64 tsf = drv_get_tsf(local, sdata);
4605 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4606 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4610 if (tsf == -1ULL || !beacon_int || !dtim_period)
4613 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4614 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4618 ps = &sdata->bss->ps;
4619 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4620 ps = &sdata->u.mesh.ps;
4626 * actually finds last dtim_count, mac80211 will update in
4627 * __beacon_add_tim().
4628 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4630 do_div(tsf, beacon_int);
4631 bcns_from_dtim = do_div(tsf, dtim_period);
4632 /* just had a DTIM */
4633 if (!bcns_from_dtim)
4636 dtim_count = dtim_period - bcns_from_dtim;
4638 ps->dtim_count = dtim_count;
4641 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4642 struct ieee80211_chanctx *ctx)
4644 struct ieee80211_link_data *link;
4645 u8 radar_detect = 0;
4647 lockdep_assert_held(&local->chanctx_mtx);
4649 if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4652 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
4653 if (link->reserved_radar_required)
4654 radar_detect |= BIT(link->reserved_chandef.width);
4657 * An in-place reservation context should not have any assigned vifs
4658 * until it replaces the other context.
4660 WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
4661 !list_empty(&ctx->assigned_links));
4663 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
4664 if (!link->radar_required)
4668 BIT(link->conf->chandef.width);
4671 return radar_detect;
4674 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4675 const struct cfg80211_chan_def *chandef,
4676 enum ieee80211_chanctx_mode chanmode,
4679 struct ieee80211_local *local = sdata->local;
4680 struct ieee80211_sub_if_data *sdata_iter;
4681 enum nl80211_iftype iftype = sdata->wdev.iftype;
4682 struct ieee80211_chanctx *ctx;
4684 struct iface_combination_params params = {
4685 .radar_detect = radar_detect,
4688 lockdep_assert_held(&local->chanctx_mtx);
4690 if (WARN_ON(hweight32(radar_detect) > 1))
4693 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4697 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4700 if (sdata->vif.type == NL80211_IFTYPE_AP ||
4701 sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4703 * always passing this is harmless, since it'll be the
4704 * same value that cfg80211 finds if it finds the same
4705 * interface ... and that's always allowed
4707 params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4710 /* Always allow software iftypes */
4711 if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4718 params.num_different_channels = 1;
4720 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4721 params.iftype_num[iftype] = 1;
4723 list_for_each_entry(ctx, &local->chanctx_list, list) {
4724 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4726 params.radar_detect |=
4727 ieee80211_chanctx_radar_detect(local, ctx);
4728 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
4729 params.num_different_channels++;
4732 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4733 cfg80211_chandef_compatible(chandef,
4736 params.num_different_channels++;
4739 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
4740 struct wireless_dev *wdev_iter;
4742 wdev_iter = &sdata_iter->wdev;
4744 if (sdata_iter == sdata ||
4745 !ieee80211_sdata_running(sdata_iter) ||
4746 cfg80211_iftype_allowed(local->hw.wiphy,
4747 wdev_iter->iftype, 0, 1))
4750 params.iftype_num[wdev_iter->iftype]++;
4754 if (total == 1 && !params.radar_detect)
4757 return cfg80211_check_combinations(local->hw.wiphy, ¶ms);
4761 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4764 u32 *max_num_different_channels = data;
4766 *max_num_different_channels = max(*max_num_different_channels,
4767 c->num_different_channels);
4770 int ieee80211_max_num_channels(struct ieee80211_local *local)
4772 struct ieee80211_sub_if_data *sdata;
4773 struct ieee80211_chanctx *ctx;
4774 u32 max_num_different_channels = 1;
4776 struct iface_combination_params params = {0};
4778 lockdep_assert_held(&local->chanctx_mtx);
4780 list_for_each_entry(ctx, &local->chanctx_list, list) {
4781 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4784 params.num_different_channels++;
4786 params.radar_detect |=
4787 ieee80211_chanctx_radar_detect(local, ctx);
4790 list_for_each_entry_rcu(sdata, &local->interfaces, list)
4791 params.iftype_num[sdata->wdev.iftype]++;
4793 err = cfg80211_iter_combinations(local->hw.wiphy, ¶ms,
4794 ieee80211_iter_max_chans,
4795 &max_num_different_channels);
4799 return max_num_different_channels;
4802 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4803 struct ieee80211_sta_s1g_cap *caps,
4804 struct sk_buff *skb)
4806 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4807 struct ieee80211_s1g_cap s1g_capab;
4811 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4817 memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4818 memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4820 /* override the capability info */
4821 for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4822 u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4824 s1g_capab.capab_info[i] &= ~mask;
4825 s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4828 /* then MCS and NSS set */
4829 for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4830 u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4832 s1g_capab.supp_mcs_nss[i] &= ~mask;
4833 s1g_capab.supp_mcs_nss[i] |=
4834 ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4837 pos = skb_put(skb, 2 + sizeof(s1g_capab));
4838 *pos++ = WLAN_EID_S1G_CAPABILITIES;
4839 *pos++ = sizeof(s1g_capab);
4841 memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4844 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4845 struct sk_buff *skb)
4847 u8 *pos = skb_put(skb, 3);
4849 *pos++ = WLAN_EID_AID_REQUEST;
4854 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4856 *buf++ = WLAN_EID_VENDOR_SPECIFIC;
4857 *buf++ = 7; /* len */
4858 *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4861 *buf++ = 2; /* WME */
4862 *buf++ = 0; /* WME info */
4863 *buf++ = 1; /* WME ver */
4864 *buf++ = qosinfo; /* U-APSD no in use */
4869 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4870 unsigned long *frame_cnt,
4871 unsigned long *byte_cnt)
4873 struct txq_info *txqi = to_txq_info(txq);
4874 u32 frag_cnt = 0, frag_bytes = 0;
4875 struct sk_buff *skb;
4877 skb_queue_walk(&txqi->frags, skb) {
4879 frag_bytes += skb->len;
4883 *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4886 *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4888 EXPORT_SYMBOL(ieee80211_txq_get_depth);
4890 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4891 IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4892 IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4893 IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4894 IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4897 u16 ieee80211_encode_usf(int listen_interval)
4899 static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
4902 /* find greatest USF */
4903 while (usf < IEEE80211_MAX_USF) {
4904 if (listen_interval % listen_int_usf[usf + 1])
4908 ui = listen_interval / listen_int_usf[usf];
4910 /* error if there is a remainder. Should've been checked by user */
4911 WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
4912 listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
4913 FIELD_PREP(LISTEN_INT_UI, ui);
4915 return (u16) listen_interval;
4918 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
4920 const struct ieee80211_sta_he_cap *he_cap;
4921 const struct ieee80211_sta_eht_cap *eht_cap;
4922 struct ieee80211_supported_band *sband;
4926 sband = ieee80211_get_sband(sdata);
4930 he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
4931 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
4932 if (!he_cap || !eht_cap)
4935 is_ap = iftype == NL80211_IFTYPE_AP ||
4936 iftype == NL80211_IFTYPE_P2P_GO;
4938 n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4939 &eht_cap->eht_cap_elem,
4942 sizeof(he_cap->he_cap_elem) + n +
4943 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4944 eht_cap->eht_cap_elem.phy_cap_info);
4948 u8 *ieee80211_ie_build_eht_cap(u8 *pos,
4949 const struct ieee80211_sta_he_cap *he_cap,
4950 const struct ieee80211_sta_eht_cap *eht_cap,
4954 u8 mcs_nss_len, ppet_len;
4958 /* Make sure we have place for the IE */
4959 if (!he_cap || !eht_cap)
4962 mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4963 &eht_cap->eht_cap_elem,
4965 ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4966 eht_cap->eht_cap_elem.phy_cap_info);
4968 ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
4969 if ((end - pos) < ie_len)
4972 *pos++ = WLAN_EID_EXTENSION;
4973 *pos++ = ie_len - 2;
4974 *pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
4977 memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
4978 pos += sizeof(eht_cap->eht_cap_elem);
4980 memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
4984 memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
4991 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos)
4993 unsigned int elem_len;
4998 elem_len = skb->data + skb->len - len_pos - 1;
5000 while (elem_len > 255) {
5001 /* this one is 255 */
5003 /* remaining data gets smaller */
5005 /* make space for the fragment ID/len in SKB */
5007 /* shift back the remaining data to place fragment ID/len */
5008 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
5009 /* place the fragment ID */
5011 *len_pos = WLAN_EID_FRAGMENT;
5012 /* and point to fragment length to update later */
5016 *len_pos = elem_len;