2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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;
44 local = wiphy_priv(wiphy);
47 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
49 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
50 enum nl80211_iftype type)
52 __le16 fc = hdr->frame_control;
54 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
58 if (ieee80211_is_data(fc)) {
59 if (len < 24) /* drop incorrect hdr len (data) */
62 if (ieee80211_has_a4(fc))
64 if (ieee80211_has_tods(fc))
66 if (ieee80211_has_fromds(fc))
72 if (ieee80211_is_mgmt(fc)) {
73 if (len < 24) /* drop incorrect hdr len (mgmt) */
78 if (ieee80211_is_ctl(fc)) {
79 if (ieee80211_is_pspoll(fc))
82 if (ieee80211_is_back_req(fc)) {
84 case NL80211_IFTYPE_STATION:
86 case NL80211_IFTYPE_AP:
87 case NL80211_IFTYPE_AP_VLAN:
90 break; /* fall through to the return */
98 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
101 struct ieee80211_hdr *hdr;
103 skb_queue_walk(&tx->skbs, skb) {
104 hdr = (struct ieee80211_hdr *) skb->data;
105 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
109 int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
110 int rate, int erp, int short_preamble,
115 /* calculate duration (in microseconds, rounded up to next higher
116 * integer if it includes a fractional microsecond) to send frame of
117 * len bytes (does not include FCS) at the given rate. Duration will
120 * rate is in 100 kbps, so divident is multiplied by 10 in the
121 * DIV_ROUND_UP() operations.
123 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
124 * is assumed to be 0 otherwise.
127 if (band == IEEE80211_BAND_5GHZ || erp) {
131 * N_DBPS = DATARATE x 4
132 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
133 * (16 = SIGNAL time, 6 = tail bits)
134 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
137 * 802.11a - 18.5.2: aSIFSTime = 16 usec
138 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
139 * signal ext = 6 usec
141 dur = 16; /* SIFS + signal ext */
142 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
143 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
145 /* IEEE 802.11-2012 18.3.2.4: all values above are:
146 * * times 4 for 5 MHz
147 * * times 2 for 10 MHz
151 /* rates should already consider the channel bandwidth,
152 * don't apply divisor again.
154 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
155 4 * rate); /* T_SYM x N_SYM */
158 * 802.11b or 802.11g with 802.11b compatibility:
159 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
160 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
162 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
163 * aSIFSTime = 10 usec
164 * aPreambleLength = 144 usec or 72 usec with short preamble
165 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
167 dur = 10; /* aSIFSTime = 10 usec */
168 dur += short_preamble ? (72 + 24) : (144 + 48);
170 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
176 /* Exported duration function for driver use */
177 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
178 struct ieee80211_vif *vif,
179 enum ieee80211_band band,
181 struct ieee80211_rate *rate)
183 struct ieee80211_sub_if_data *sdata;
186 bool short_preamble = false;
190 sdata = vif_to_sdata(vif);
191 short_preamble = sdata->vif.bss_conf.use_short_preamble;
192 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
193 erp = rate->flags & IEEE80211_RATE_ERP_G;
194 shift = ieee80211_vif_get_shift(vif);
197 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
198 short_preamble, shift);
200 return cpu_to_le16(dur);
202 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
204 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
205 struct ieee80211_vif *vif, size_t frame_len,
206 const struct ieee80211_tx_info *frame_txctl)
208 struct ieee80211_local *local = hw_to_local(hw);
209 struct ieee80211_rate *rate;
210 struct ieee80211_sub_if_data *sdata;
212 int erp, shift = 0, bitrate;
214 struct ieee80211_supported_band *sband;
216 sband = local->hw.wiphy->bands[frame_txctl->band];
218 short_preamble = false;
220 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
224 sdata = vif_to_sdata(vif);
225 short_preamble = sdata->vif.bss_conf.use_short_preamble;
226 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
227 erp = rate->flags & IEEE80211_RATE_ERP_G;
228 shift = ieee80211_vif_get_shift(vif);
231 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
234 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
235 erp, short_preamble, shift);
236 /* Data frame duration */
237 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
238 erp, short_preamble, shift);
240 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
241 erp, short_preamble, shift);
243 return cpu_to_le16(dur);
245 EXPORT_SYMBOL(ieee80211_rts_duration);
247 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
248 struct ieee80211_vif *vif,
250 const struct ieee80211_tx_info *frame_txctl)
252 struct ieee80211_local *local = hw_to_local(hw);
253 struct ieee80211_rate *rate;
254 struct ieee80211_sub_if_data *sdata;
256 int erp, shift = 0, bitrate;
258 struct ieee80211_supported_band *sband;
260 sband = local->hw.wiphy->bands[frame_txctl->band];
262 short_preamble = false;
264 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
267 sdata = vif_to_sdata(vif);
268 short_preamble = sdata->vif.bss_conf.use_short_preamble;
269 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
270 erp = rate->flags & IEEE80211_RATE_ERP_G;
271 shift = ieee80211_vif_get_shift(vif);
274 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
276 /* Data frame duration */
277 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
278 erp, short_preamble, shift);
279 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
281 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
282 erp, short_preamble, shift);
285 return cpu_to_le16(dur);
287 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
289 void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
291 struct ieee80211_sub_if_data *sdata;
292 int n_acs = IEEE80211_NUM_ACS;
294 if (local->hw.queues < IEEE80211_NUM_ACS)
297 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
303 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
304 local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
307 for (ac = 0; ac < n_acs; ac++) {
308 int ac_queue = sdata->vif.hw_queue[ac];
310 if (ac_queue == queue ||
311 (sdata->vif.cab_queue == queue &&
312 local->queue_stop_reasons[ac_queue] == 0 &&
313 skb_queue_empty(&local->pending[ac_queue])))
314 netif_wake_subqueue(sdata->dev, ac);
319 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
320 enum queue_stop_reason reason,
323 struct ieee80211_local *local = hw_to_local(hw);
325 trace_wake_queue(local, queue, reason);
327 if (WARN_ON(queue >= hw->queues))
330 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
334 local->q_stop_reasons[queue][reason] = 0;
336 local->q_stop_reasons[queue][reason]--;
338 if (local->q_stop_reasons[queue][reason] == 0)
339 __clear_bit(reason, &local->queue_stop_reasons[queue]);
341 if (local->queue_stop_reasons[queue] != 0)
342 /* someone still has this queue stopped */
345 if (skb_queue_empty(&local->pending[queue])) {
347 ieee80211_propagate_queue_wake(local, queue);
350 tasklet_schedule(&local->tx_pending_tasklet);
353 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
354 enum queue_stop_reason reason,
357 struct ieee80211_local *local = hw_to_local(hw);
360 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
361 __ieee80211_wake_queue(hw, queue, reason, refcounted);
362 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
365 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
367 ieee80211_wake_queue_by_reason(hw, queue,
368 IEEE80211_QUEUE_STOP_REASON_DRIVER,
371 EXPORT_SYMBOL(ieee80211_wake_queue);
373 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
374 enum queue_stop_reason reason,
377 struct ieee80211_local *local = hw_to_local(hw);
378 struct ieee80211_sub_if_data *sdata;
379 int n_acs = IEEE80211_NUM_ACS;
381 trace_stop_queue(local, queue, reason);
383 if (WARN_ON(queue >= hw->queues))
387 local->q_stop_reasons[queue][reason] = 1;
389 local->q_stop_reasons[queue][reason]++;
391 if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
394 if (local->hw.queues < IEEE80211_NUM_ACS)
398 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
404 for (ac = 0; ac < n_acs; ac++) {
405 if (sdata->vif.hw_queue[ac] == queue ||
406 sdata->vif.cab_queue == queue)
407 netif_stop_subqueue(sdata->dev, ac);
413 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
414 enum queue_stop_reason reason,
417 struct ieee80211_local *local = hw_to_local(hw);
420 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
421 __ieee80211_stop_queue(hw, queue, reason, refcounted);
422 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
425 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
427 ieee80211_stop_queue_by_reason(hw, queue,
428 IEEE80211_QUEUE_STOP_REASON_DRIVER,
431 EXPORT_SYMBOL(ieee80211_stop_queue);
433 void ieee80211_add_pending_skb(struct ieee80211_local *local,
436 struct ieee80211_hw *hw = &local->hw;
438 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
439 int queue = info->hw_queue;
441 if (WARN_ON(!info->control.vif)) {
442 ieee80211_free_txskb(&local->hw, skb);
446 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
447 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
449 __skb_queue_tail(&local->pending[queue], skb);
450 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
452 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
455 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
456 struct sk_buff_head *skbs)
458 struct ieee80211_hw *hw = &local->hw;
463 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
464 while ((skb = skb_dequeue(skbs))) {
465 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
467 if (WARN_ON(!info->control.vif)) {
468 ieee80211_free_txskb(&local->hw, skb);
472 queue = info->hw_queue;
474 __ieee80211_stop_queue(hw, queue,
475 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
478 __skb_queue_tail(&local->pending[queue], skb);
481 for (i = 0; i < hw->queues; i++)
482 __ieee80211_wake_queue(hw, i,
483 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
485 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
488 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
489 unsigned long queues,
490 enum queue_stop_reason reason,
493 struct ieee80211_local *local = hw_to_local(hw);
497 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
499 for_each_set_bit(i, &queues, hw->queues)
500 __ieee80211_stop_queue(hw, i, reason, refcounted);
502 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
505 void ieee80211_stop_queues(struct ieee80211_hw *hw)
507 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
508 IEEE80211_QUEUE_STOP_REASON_DRIVER,
511 EXPORT_SYMBOL(ieee80211_stop_queues);
513 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
515 struct ieee80211_local *local = hw_to_local(hw);
519 if (WARN_ON(queue >= hw->queues))
522 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
523 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
524 &local->queue_stop_reasons[queue]);
525 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
528 EXPORT_SYMBOL(ieee80211_queue_stopped);
530 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
531 unsigned long queues,
532 enum queue_stop_reason reason,
535 struct ieee80211_local *local = hw_to_local(hw);
539 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
541 for_each_set_bit(i, &queues, hw->queues)
542 __ieee80211_wake_queue(hw, i, reason, refcounted);
544 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
547 void ieee80211_wake_queues(struct ieee80211_hw *hw)
549 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
550 IEEE80211_QUEUE_STOP_REASON_DRIVER,
553 EXPORT_SYMBOL(ieee80211_wake_queues);
555 void ieee80211_flush_queues(struct ieee80211_local *local,
556 struct ieee80211_sub_if_data *sdata)
560 if (!local->ops->flush)
563 if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
568 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
569 queues |= BIT(sdata->vif.hw_queue[ac]);
570 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
571 queues |= BIT(sdata->vif.cab_queue);
574 queues = BIT(local->hw.queues) - 1;
577 ieee80211_stop_queues_by_reason(&local->hw, queues,
578 IEEE80211_QUEUE_STOP_REASON_FLUSH,
581 drv_flush(local, sdata, queues, false);
583 ieee80211_wake_queues_by_reason(&local->hw, queues,
584 IEEE80211_QUEUE_STOP_REASON_FLUSH,
588 static void __iterate_active_interfaces(struct ieee80211_local *local,
590 void (*iterator)(void *data, u8 *mac,
591 struct ieee80211_vif *vif),
594 struct ieee80211_sub_if_data *sdata;
596 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
597 switch (sdata->vif.type) {
598 case NL80211_IFTYPE_MONITOR:
599 if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
602 case NL80211_IFTYPE_AP_VLAN:
607 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
608 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
610 if (ieee80211_sdata_running(sdata))
611 iterator(data, sdata->vif.addr,
615 sdata = rcu_dereference_check(local->monitor_sdata,
616 lockdep_is_held(&local->iflist_mtx) ||
617 lockdep_rtnl_is_held());
619 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
620 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
621 iterator(data, sdata->vif.addr, &sdata->vif);
624 void ieee80211_iterate_active_interfaces(
625 struct ieee80211_hw *hw, u32 iter_flags,
626 void (*iterator)(void *data, u8 *mac,
627 struct ieee80211_vif *vif),
630 struct ieee80211_local *local = hw_to_local(hw);
632 mutex_lock(&local->iflist_mtx);
633 __iterate_active_interfaces(local, iter_flags, iterator, data);
634 mutex_unlock(&local->iflist_mtx);
636 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
638 void ieee80211_iterate_active_interfaces_atomic(
639 struct ieee80211_hw *hw, u32 iter_flags,
640 void (*iterator)(void *data, u8 *mac,
641 struct ieee80211_vif *vif),
644 struct ieee80211_local *local = hw_to_local(hw);
647 __iterate_active_interfaces(local, iter_flags, iterator, data);
650 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
652 void ieee80211_iterate_active_interfaces_rtnl(
653 struct ieee80211_hw *hw, u32 iter_flags,
654 void (*iterator)(void *data, u8 *mac,
655 struct ieee80211_vif *vif),
658 struct ieee80211_local *local = hw_to_local(hw);
662 __iterate_active_interfaces(local, iter_flags, iterator, data);
664 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
666 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
668 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
670 if (!ieee80211_sdata_running(sdata) ||
671 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
675 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
678 * Nothing should have been stuffed into the workqueue during
679 * the suspend->resume cycle. If this WARN is seen then there
680 * is a bug with either the driver suspend or something in
681 * mac80211 stuffing into the workqueue which we haven't yet
682 * cleared during mac80211's suspend cycle.
684 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
686 if (WARN(local->suspended && !local->resuming,
687 "queueing ieee80211 work while going to suspend\n"))
693 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
695 struct ieee80211_local *local = hw_to_local(hw);
697 if (!ieee80211_can_queue_work(local))
700 queue_work(local->workqueue, work);
702 EXPORT_SYMBOL(ieee80211_queue_work);
704 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
705 struct delayed_work *dwork,
708 struct ieee80211_local *local = hw_to_local(hw);
710 if (!ieee80211_can_queue_work(local))
713 queue_delayed_work(local->workqueue, dwork, delay);
715 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
717 u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
718 struct ieee802_11_elems *elems,
722 const u8 *pos = start;
723 bool calc_crc = filter != 0;
724 DECLARE_BITMAP(seen_elems, 256);
727 bitmap_zero(seen_elems, 256);
728 memset(elems, 0, sizeof(*elems));
729 elems->ie_start = start;
730 elems->total_len = len;
734 bool elem_parse_failed;
741 elems->parse_error = true;
747 case WLAN_EID_SUPP_RATES:
748 case WLAN_EID_FH_PARAMS:
749 case WLAN_EID_DS_PARAMS:
750 case WLAN_EID_CF_PARAMS:
752 case WLAN_EID_IBSS_PARAMS:
753 case WLAN_EID_CHALLENGE:
755 case WLAN_EID_ERP_INFO:
756 case WLAN_EID_EXT_SUPP_RATES:
757 case WLAN_EID_HT_CAPABILITY:
758 case WLAN_EID_HT_OPERATION:
759 case WLAN_EID_VHT_CAPABILITY:
760 case WLAN_EID_VHT_OPERATION:
761 case WLAN_EID_MESH_ID:
762 case WLAN_EID_MESH_CONFIG:
763 case WLAN_EID_PEER_MGMT:
768 case WLAN_EID_CHANNEL_SWITCH:
769 case WLAN_EID_EXT_CHANSWITCH_ANN:
770 case WLAN_EID_COUNTRY:
771 case WLAN_EID_PWR_CONSTRAINT:
772 case WLAN_EID_TIMEOUT_INTERVAL:
773 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
774 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
775 case WLAN_EID_CHAN_SWITCH_PARAM:
777 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
778 * that if the content gets bigger it might be needed more than once
780 if (test_bit(id, seen_elems)) {
781 elems->parse_error = true;
789 if (calc_crc && id < 64 && (filter & (1ULL << id)))
790 crc = crc32_be(crc, pos - 2, elen + 2);
792 elem_parse_failed = false;
797 elems->ssid_len = elen;
799 case WLAN_EID_SUPP_RATES:
800 elems->supp_rates = pos;
801 elems->supp_rates_len = elen;
803 case WLAN_EID_DS_PARAMS:
805 elems->ds_params = pos;
807 elem_parse_failed = true;
810 if (elen >= sizeof(struct ieee80211_tim_ie)) {
811 elems->tim = (void *)pos;
812 elems->tim_len = elen;
814 elem_parse_failed = true;
816 case WLAN_EID_CHALLENGE:
817 elems->challenge = pos;
818 elems->challenge_len = elen;
820 case WLAN_EID_VENDOR_SPECIFIC:
821 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
823 /* Microsoft OUI (00:50:F2) */
826 crc = crc32_be(crc, pos - 2, elen + 2);
828 if (elen >= 5 && pos[3] == 2) {
829 /* OUI Type 2 - WMM IE */
831 elems->wmm_info = pos;
832 elems->wmm_info_len = elen;
833 } else if (pos[4] == 1) {
834 elems->wmm_param = pos;
835 elems->wmm_param_len = elen;
842 elems->rsn_len = elen;
844 case WLAN_EID_ERP_INFO:
846 elems->erp_info = pos;
848 elem_parse_failed = true;
850 case WLAN_EID_EXT_SUPP_RATES:
851 elems->ext_supp_rates = pos;
852 elems->ext_supp_rates_len = elen;
854 case WLAN_EID_HT_CAPABILITY:
855 if (elen >= sizeof(struct ieee80211_ht_cap))
856 elems->ht_cap_elem = (void *)pos;
858 elem_parse_failed = true;
860 case WLAN_EID_HT_OPERATION:
861 if (elen >= sizeof(struct ieee80211_ht_operation))
862 elems->ht_operation = (void *)pos;
864 elem_parse_failed = true;
866 case WLAN_EID_VHT_CAPABILITY:
867 if (elen >= sizeof(struct ieee80211_vht_cap))
868 elems->vht_cap_elem = (void *)pos;
870 elem_parse_failed = true;
872 case WLAN_EID_VHT_OPERATION:
873 if (elen >= sizeof(struct ieee80211_vht_operation))
874 elems->vht_operation = (void *)pos;
876 elem_parse_failed = true;
878 case WLAN_EID_OPMODE_NOTIF:
880 elems->opmode_notif = pos;
882 elem_parse_failed = true;
884 case WLAN_EID_MESH_ID:
885 elems->mesh_id = pos;
886 elems->mesh_id_len = elen;
888 case WLAN_EID_MESH_CONFIG:
889 if (elen >= sizeof(struct ieee80211_meshconf_ie))
890 elems->mesh_config = (void *)pos;
892 elem_parse_failed = true;
894 case WLAN_EID_PEER_MGMT:
895 elems->peering = pos;
896 elems->peering_len = elen;
898 case WLAN_EID_MESH_AWAKE_WINDOW:
900 elems->awake_window = (void *)pos;
904 elems->preq_len = elen;
908 elems->prep_len = elen;
912 elems->perr_len = elen;
915 if (elen >= sizeof(struct ieee80211_rann_ie))
916 elems->rann = (void *)pos;
918 elem_parse_failed = true;
920 case WLAN_EID_CHANNEL_SWITCH:
921 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
922 elem_parse_failed = true;
925 elems->ch_switch_ie = (void *)pos;
927 case WLAN_EID_EXT_CHANSWITCH_ANN:
928 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
929 elem_parse_failed = true;
932 elems->ext_chansw_ie = (void *)pos;
934 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
935 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
936 elem_parse_failed = true;
939 elems->sec_chan_offs = (void *)pos;
941 case WLAN_EID_CHAN_SWITCH_PARAM:
943 sizeof(*elems->mesh_chansw_params_ie)) {
944 elem_parse_failed = true;
947 elems->mesh_chansw_params_ie = (void *)pos;
949 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
951 elen != sizeof(*elems->wide_bw_chansw_ie)) {
952 elem_parse_failed = true;
955 elems->wide_bw_chansw_ie = (void *)pos;
957 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
959 elem_parse_failed = true;
963 * This is a bit tricky, but as we only care about
964 * the wide bandwidth channel switch element, so
965 * just parse it out manually.
967 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
970 if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
971 elems->wide_bw_chansw_ie =
974 elem_parse_failed = true;
977 case WLAN_EID_COUNTRY:
978 elems->country_elem = pos;
979 elems->country_elem_len = elen;
981 case WLAN_EID_PWR_CONSTRAINT:
983 elem_parse_failed = true;
986 elems->pwr_constr_elem = pos;
988 case WLAN_EID_TIMEOUT_INTERVAL:
989 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
990 elems->timeout_int = (void *)pos;
992 elem_parse_failed = true;
998 if (elem_parse_failed)
999 elems->parse_error = true;
1001 __set_bit(id, seen_elems);
1008 elems->parse_error = true;
1013 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1016 struct ieee80211_local *local = sdata->local;
1017 struct ieee80211_tx_queue_params qparam;
1018 struct ieee80211_chanctx_conf *chanctx_conf;
1020 bool use_11b, enable_qos;
1023 if (!local->ops->conf_tx)
1026 if (local->hw.queues < IEEE80211_NUM_ACS)
1029 memset(&qparam, 0, sizeof(qparam));
1032 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1033 use_11b = (chanctx_conf &&
1034 chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
1035 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
1039 * By default disable QoS in STA mode for old access points, which do
1040 * not support 802.11e. New APs will provide proper queue parameters,
1041 * that we will configure later.
1043 enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
1045 /* Set defaults according to 802.11-2007 Table 7-37 */
1052 /* Confiure old 802.11b/g medium access rules. */
1053 qparam.cw_max = aCWmax;
1054 qparam.cw_min = aCWmin;
1058 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1059 /* Update if QoS is enabled. */
1062 case IEEE80211_AC_BK:
1063 qparam.cw_max = aCWmax;
1064 qparam.cw_min = aCWmin;
1068 /* never happens but let's not leave undefined */
1070 case IEEE80211_AC_BE:
1071 qparam.cw_max = aCWmax;
1072 qparam.cw_min = aCWmin;
1076 case IEEE80211_AC_VI:
1077 qparam.cw_max = aCWmin;
1078 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1080 qparam.txop = 6016/32;
1082 qparam.txop = 3008/32;
1085 case IEEE80211_AC_VO:
1086 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1087 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1089 qparam.txop = 3264/32;
1091 qparam.txop = 1504/32;
1097 qparam.uapsd = false;
1099 sdata->tx_conf[ac] = qparam;
1100 drv_conf_tx(local, sdata, ac, &qparam);
1103 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1104 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
1105 sdata->vif.bss_conf.qos = enable_qos;
1107 ieee80211_bss_info_change_notify(sdata,
1112 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1113 u16 transaction, u16 auth_alg, u16 status,
1114 const u8 *extra, size_t extra_len, const u8 *da,
1115 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1118 struct ieee80211_local *local = sdata->local;
1119 struct sk_buff *skb;
1120 struct ieee80211_mgmt *mgmt;
1123 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1124 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24 + 6 + extra_len);
1128 skb_reserve(skb, local->hw.extra_tx_headroom);
1130 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
1131 memset(mgmt, 0, 24 + 6);
1132 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1133 IEEE80211_STYPE_AUTH);
1134 memcpy(mgmt->da, da, ETH_ALEN);
1135 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1136 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1137 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1138 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1139 mgmt->u.auth.status_code = cpu_to_le16(status);
1141 memcpy(skb_put(skb, extra_len), extra, extra_len);
1143 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1144 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1145 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1149 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1151 ieee80211_tx_skb(sdata, skb);
1154 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1155 const u8 *bssid, u16 stype, u16 reason,
1156 bool send_frame, u8 *frame_buf)
1158 struct ieee80211_local *local = sdata->local;
1159 struct sk_buff *skb;
1160 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1163 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1164 mgmt->duration = 0; /* initialize only */
1165 mgmt->seq_ctrl = 0; /* initialize only */
1166 memcpy(mgmt->da, bssid, ETH_ALEN);
1167 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1168 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1169 /* u.deauth.reason_code == u.disassoc.reason_code */
1170 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1173 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1174 IEEE80211_DEAUTH_FRAME_LEN);
1178 skb_reserve(skb, local->hw.extra_tx_headroom);
1181 memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
1182 mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1184 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1185 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1186 IEEE80211_SKB_CB(skb)->flags |=
1187 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1189 ieee80211_tx_skb(sdata, skb);
1193 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1194 size_t buffer_len, const u8 *ie, size_t ie_len,
1195 enum ieee80211_band band, u32 rate_mask,
1196 struct cfg80211_chan_def *chandef)
1198 struct ieee80211_supported_band *sband;
1199 u8 *pos = buffer, *end = buffer + buffer_len;
1200 size_t offset = 0, noffset;
1201 int supp_rates_len, i;
1208 sband = local->hw.wiphy->bands[band];
1209 if (WARN_ON_ONCE(!sband))
1212 rate_flags = ieee80211_chandef_rate_flags(chandef);
1213 shift = ieee80211_chandef_get_shift(chandef);
1216 for (i = 0; i < sband->n_bitrates; i++) {
1217 if ((BIT(i) & rate_mask) == 0)
1218 continue; /* skip rate */
1219 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1222 rates[num_rates++] =
1223 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1227 supp_rates_len = min_t(int, num_rates, 8);
1229 if (end - pos < 2 + supp_rates_len)
1231 *pos++ = WLAN_EID_SUPP_RATES;
1232 *pos++ = supp_rates_len;
1233 memcpy(pos, rates, supp_rates_len);
1234 pos += supp_rates_len;
1236 /* insert "request information" if in custom IEs */
1238 static const u8 before_extrates[] = {
1240 WLAN_EID_SUPP_RATES,
1243 noffset = ieee80211_ie_split(ie, ie_len,
1245 ARRAY_SIZE(before_extrates),
1247 if (end - pos < noffset - offset)
1249 memcpy(pos, ie + offset, noffset - offset);
1250 pos += noffset - offset;
1254 ext_rates_len = num_rates - supp_rates_len;
1255 if (ext_rates_len > 0) {
1256 if (end - pos < 2 + ext_rates_len)
1258 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1259 *pos++ = ext_rates_len;
1260 memcpy(pos, rates + supp_rates_len, ext_rates_len);
1261 pos += ext_rates_len;
1264 if (chandef->chan && sband->band == IEEE80211_BAND_2GHZ) {
1267 *pos++ = WLAN_EID_DS_PARAMS;
1269 *pos++ = ieee80211_frequency_to_channel(
1270 chandef->chan->center_freq);
1273 /* insert custom IEs that go before HT */
1275 static const u8 before_ht[] = {
1277 WLAN_EID_SUPP_RATES,
1279 WLAN_EID_EXT_SUPP_RATES,
1281 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1283 noffset = ieee80211_ie_split(ie, ie_len,
1284 before_ht, ARRAY_SIZE(before_ht),
1286 if (end - pos < noffset - offset)
1288 memcpy(pos, ie + offset, noffset - offset);
1289 pos += noffset - offset;
1293 if (sband->ht_cap.ht_supported) {
1294 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1296 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1301 * If adding more here, adjust code in main.c
1302 * that calculates local->scan_ies_len.
1305 /* insert custom IEs that go before VHT */
1307 static const u8 before_vht[] = {
1309 WLAN_EID_SUPP_RATES,
1311 WLAN_EID_EXT_SUPP_RATES,
1313 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1314 WLAN_EID_HT_CAPABILITY,
1315 WLAN_EID_BSS_COEX_2040,
1316 WLAN_EID_EXT_CAPABILITY,
1318 WLAN_EID_CHANNEL_USAGE,
1319 WLAN_EID_INTERWORKING,
1320 /* mesh ID can't happen here */
1321 /* 60 GHz can't happen here right now */
1323 noffset = ieee80211_ie_split(ie, ie_len,
1324 before_vht, ARRAY_SIZE(before_vht),
1326 if (end - pos < noffset - offset)
1328 memcpy(pos, ie + offset, noffset - offset);
1329 pos += noffset - offset;
1333 if (sband->vht_cap.vht_supported) {
1334 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1336 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1337 sband->vht_cap.cap);
1340 /* add any remaining custom IEs */
1343 if (end - pos < noffset - offset)
1345 memcpy(pos, ie + offset, noffset - offset);
1346 pos += noffset - offset;
1349 return pos - buffer;
1351 WARN_ONCE(1, "not enough space for preq IEs\n");
1352 return pos - buffer;
1355 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1356 u8 *dst, u32 ratemask,
1357 struct ieee80211_channel *chan,
1358 const u8 *ssid, size_t ssid_len,
1359 const u8 *ie, size_t ie_len,
1362 struct ieee80211_local *local = sdata->local;
1363 struct cfg80211_chan_def chandef;
1364 struct sk_buff *skb;
1365 struct ieee80211_mgmt *mgmt;
1369 * Do not send DS Channel parameter for directed probe requests
1370 * in order to maximize the chance that we get a response. Some
1371 * badly-behaved APs don't respond when this parameter is included.
1373 chandef.width = sdata->vif.bss_conf.chandef.width;
1375 chandef.chan = NULL;
1377 chandef.chan = chan;
1379 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
1380 ssid, ssid_len, 100 + ie_len);
1384 ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1386 ie, ie_len, chan->band,
1387 ratemask, &chandef);
1388 skb_put(skb, ies_len);
1391 mgmt = (struct ieee80211_mgmt *) skb->data;
1392 memcpy(mgmt->da, dst, ETH_ALEN);
1393 memcpy(mgmt->bssid, dst, ETH_ALEN);
1396 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1401 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1402 const u8 *ssid, size_t ssid_len,
1403 const u8 *ie, size_t ie_len,
1404 u32 ratemask, bool directed, u32 tx_flags,
1405 struct ieee80211_channel *channel, bool scan)
1407 struct sk_buff *skb;
1409 skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
1411 ie, ie_len, directed);
1413 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
1415 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
1417 ieee80211_tx_skb(sdata, skb);
1421 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
1422 struct ieee802_11_elems *elems,
1423 enum ieee80211_band band, u32 *basic_rates)
1425 struct ieee80211_supported_band *sband;
1427 u32 supp_rates, rate_flags;
1429 sband = sdata->local->hw.wiphy->bands[band];
1431 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
1432 shift = ieee80211_vif_get_shift(&sdata->vif);
1434 if (WARN_ON(!sband))
1437 num_rates = sband->n_bitrates;
1439 for (i = 0; i < elems->supp_rates_len +
1440 elems->ext_supp_rates_len; i++) {
1444 if (i < elems->supp_rates_len)
1445 rate = elems->supp_rates[i];
1446 else if (elems->ext_supp_rates)
1447 rate = elems->ext_supp_rates
1448 [i - elems->supp_rates_len];
1449 own_rate = 5 * (rate & 0x7f);
1450 is_basic = !!(rate & 0x80);
1452 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1455 for (j = 0; j < num_rates; j++) {
1457 if ((rate_flags & sband->bitrates[j].flags)
1461 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
1464 if (brate == own_rate) {
1465 supp_rates |= BIT(j);
1466 if (basic_rates && is_basic)
1467 *basic_rates |= BIT(j);
1474 void ieee80211_stop_device(struct ieee80211_local *local)
1476 ieee80211_led_radio(local, false);
1477 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
1479 cancel_work_sync(&local->reconfig_filter);
1481 flush_workqueue(local->workqueue);
1485 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1487 struct ieee80211_sub_if_data *sdata;
1488 struct ieee80211_chanctx *ctx;
1491 * We get here if during resume the device can't be restarted properly.
1492 * We might also get here if this happens during HW reset, which is a
1493 * slightly different situation and we need to drop all connections in
1496 * Ask cfg80211 to turn off all interfaces, this will result in more
1497 * warnings but at least we'll then get into a clean stopped state.
1500 local->resuming = false;
1501 local->suspended = false;
1502 local->started = false;
1504 /* scheduled scan clearly can't be running any more, but tell
1505 * cfg80211 and clear local state
1507 ieee80211_sched_scan_end(local);
1509 list_for_each_entry(sdata, &local->interfaces, list)
1510 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
1512 /* Mark channel contexts as not being in the driver any more to avoid
1513 * removing them from the driver during the shutdown process...
1515 mutex_lock(&local->chanctx_mtx);
1516 list_for_each_entry(ctx, &local->chanctx_list, list)
1517 ctx->driver_present = false;
1518 mutex_unlock(&local->chanctx_mtx);
1520 cfg80211_shutdown_all_interfaces(local->hw.wiphy);
1523 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1524 struct ieee80211_sub_if_data *sdata)
1526 struct ieee80211_chanctx_conf *conf;
1527 struct ieee80211_chanctx *ctx;
1529 if (!local->use_chanctx)
1532 mutex_lock(&local->chanctx_mtx);
1533 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1534 lockdep_is_held(&local->chanctx_mtx));
1536 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1537 drv_assign_vif_chanctx(local, sdata, ctx);
1539 mutex_unlock(&local->chanctx_mtx);
1542 int ieee80211_reconfig(struct ieee80211_local *local)
1544 struct ieee80211_hw *hw = &local->hw;
1545 struct ieee80211_sub_if_data *sdata;
1546 struct ieee80211_chanctx *ctx;
1547 struct sta_info *sta;
1549 bool reconfig_due_to_wowlan = false;
1550 struct ieee80211_sub_if_data *sched_scan_sdata;
1551 bool sched_scan_stopped = false;
1554 if (local->suspended)
1555 local->resuming = true;
1557 if (local->wowlan) {
1558 res = drv_resume(local);
1559 local->wowlan = false;
1561 local->resuming = false;
1568 * res is 1, which means the driver requested
1569 * to go through a regular reset on wakeup.
1571 reconfig_due_to_wowlan = true;
1574 /* everything else happens only if HW was up & running */
1575 if (!local->open_count)
1579 * Upon resume hardware can sometimes be goofy due to
1580 * various platform / driver / bus issues, so restarting
1581 * the device may at times not work immediately. Propagate
1584 res = drv_start(local);
1586 if (local->suspended)
1587 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
1589 WARN(1, "Hardware became unavailable during restart.\n");
1590 ieee80211_handle_reconfig_failure(local);
1594 /* setup fragmentation threshold */
1595 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
1597 /* setup RTS threshold */
1598 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1600 /* reset coverage class */
1601 drv_set_coverage_class(local, hw->wiphy->coverage_class);
1603 ieee80211_led_radio(local, true);
1604 ieee80211_mod_tpt_led_trig(local,
1605 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1607 /* add interfaces */
1608 sdata = rtnl_dereference(local->monitor_sdata);
1610 /* in HW restart it exists already */
1611 WARN_ON(local->resuming);
1612 res = drv_add_interface(local, sdata);
1614 RCU_INIT_POINTER(local->monitor_sdata, NULL);
1620 list_for_each_entry(sdata, &local->interfaces, list) {
1621 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1622 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1623 ieee80211_sdata_running(sdata))
1624 res = drv_add_interface(local, sdata);
1627 /* add channel contexts */
1628 if (local->use_chanctx) {
1629 mutex_lock(&local->chanctx_mtx);
1630 list_for_each_entry(ctx, &local->chanctx_list, list)
1631 WARN_ON(drv_add_chanctx(local, ctx));
1632 mutex_unlock(&local->chanctx_mtx);
1634 list_for_each_entry(sdata, &local->interfaces, list) {
1635 if (!ieee80211_sdata_running(sdata))
1637 ieee80211_assign_chanctx(local, sdata);
1640 sdata = rtnl_dereference(local->monitor_sdata);
1641 if (sdata && ieee80211_sdata_running(sdata))
1642 ieee80211_assign_chanctx(local, sdata);
1646 mutex_lock(&local->sta_mtx);
1647 list_for_each_entry(sta, &local->sta_list, list) {
1648 enum ieee80211_sta_state state;
1653 /* AP-mode stations will be added later */
1654 if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1657 for (state = IEEE80211_STA_NOTEXIST;
1658 state < sta->sta_state; state++)
1659 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1662 mutex_unlock(&local->sta_mtx);
1664 /* reconfigure tx conf */
1665 if (hw->queues >= IEEE80211_NUM_ACS) {
1666 list_for_each_entry(sdata, &local->interfaces, list) {
1667 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1668 sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1669 !ieee80211_sdata_running(sdata))
1672 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1673 drv_conf_tx(local, sdata, i,
1674 &sdata->tx_conf[i]);
1678 /* reconfigure hardware */
1679 ieee80211_hw_config(local, ~0);
1681 ieee80211_configure_filter(local);
1683 /* Finally also reconfigure all the BSS information */
1684 list_for_each_entry(sdata, &local->interfaces, list) {
1687 if (!ieee80211_sdata_running(sdata))
1690 /* common change flags for all interface types */
1691 changed = BSS_CHANGED_ERP_CTS_PROT |
1692 BSS_CHANGED_ERP_PREAMBLE |
1693 BSS_CHANGED_ERP_SLOT |
1695 BSS_CHANGED_BASIC_RATES |
1696 BSS_CHANGED_BEACON_INT |
1701 BSS_CHANGED_TXPOWER;
1703 switch (sdata->vif.type) {
1704 case NL80211_IFTYPE_STATION:
1705 changed |= BSS_CHANGED_ASSOC |
1706 BSS_CHANGED_ARP_FILTER |
1709 /* Re-send beacon info report to the driver */
1710 if (sdata->u.mgd.have_beacon)
1711 changed |= BSS_CHANGED_BEACON_INFO;
1714 ieee80211_bss_info_change_notify(sdata, changed);
1715 sdata_unlock(sdata);
1717 case NL80211_IFTYPE_ADHOC:
1718 changed |= BSS_CHANGED_IBSS;
1720 case NL80211_IFTYPE_AP:
1721 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
1723 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1724 changed |= BSS_CHANGED_AP_PROBE_RESP;
1726 if (rcu_access_pointer(sdata->u.ap.beacon))
1727 drv_start_ap(local, sdata);
1731 case NL80211_IFTYPE_MESH_POINT:
1732 if (sdata->vif.bss_conf.enable_beacon) {
1733 changed |= BSS_CHANGED_BEACON |
1734 BSS_CHANGED_BEACON_ENABLED;
1735 ieee80211_bss_info_change_notify(sdata, changed);
1738 case NL80211_IFTYPE_WDS:
1739 case NL80211_IFTYPE_AP_VLAN:
1740 case NL80211_IFTYPE_MONITOR:
1741 case NL80211_IFTYPE_P2P_DEVICE:
1744 case NL80211_IFTYPE_UNSPECIFIED:
1745 case NUM_NL80211_IFTYPES:
1746 case NL80211_IFTYPE_P2P_CLIENT:
1747 case NL80211_IFTYPE_P2P_GO:
1753 ieee80211_recalc_ps(local, -1);
1756 * The sta might be in psm against the ap (e.g. because
1757 * this was the state before a hw restart), so we
1758 * explicitly send a null packet in order to make sure
1759 * it'll sync against the ap (and get out of psm).
1761 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
1762 list_for_each_entry(sdata, &local->interfaces, list) {
1763 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1765 if (!sdata->u.mgd.associated)
1768 ieee80211_send_nullfunc(local, sdata, 0);
1772 /* APs are now beaconing, add back stations */
1773 mutex_lock(&local->sta_mtx);
1774 list_for_each_entry(sta, &local->sta_list, list) {
1775 enum ieee80211_sta_state state;
1780 if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
1783 for (state = IEEE80211_STA_NOTEXIST;
1784 state < sta->sta_state; state++)
1785 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1788 mutex_unlock(&local->sta_mtx);
1791 list_for_each_entry(sdata, &local->interfaces, list)
1792 if (ieee80211_sdata_running(sdata))
1793 ieee80211_enable_keys(sdata);
1796 local->in_reconfig = false;
1799 if (local->monitors == local->open_count && local->monitors > 0)
1800 ieee80211_add_virtual_monitor(local);
1803 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1804 * sessions can be established after a resume.
1806 * Also tear down aggregation sessions since reconfiguring
1807 * them in a hardware restart scenario is not easily done
1808 * right now, and the hardware will have lost information
1809 * about the sessions, but we and the AP still think they
1810 * are active. This is really a workaround though.
1812 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1813 mutex_lock(&local->sta_mtx);
1815 list_for_each_entry(sta, &local->sta_list, list) {
1816 ieee80211_sta_tear_down_BA_sessions(
1817 sta, AGG_STOP_LOCAL_REQUEST);
1818 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
1821 mutex_unlock(&local->sta_mtx);
1824 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
1825 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
1829 * Reconfigure sched scan if it was interrupted by FW restart or
1832 mutex_lock(&local->mtx);
1833 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1834 lockdep_is_held(&local->mtx));
1835 if (sched_scan_sdata && local->sched_scan_req)
1837 * Sched scan stopped, but we don't want to report it. Instead,
1838 * we're trying to reschedule.
1840 if (__ieee80211_request_sched_scan_start(sched_scan_sdata,
1841 local->sched_scan_req))
1842 sched_scan_stopped = true;
1843 mutex_unlock(&local->mtx);
1845 if (sched_scan_stopped)
1846 cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy);
1849 * If this is for hw restart things are still running.
1850 * We may want to change that later, however.
1852 if (!local->suspended || reconfig_due_to_wowlan)
1853 drv_restart_complete(local);
1855 if (!local->suspended)
1859 /* first set suspended false, then resuming */
1860 local->suspended = false;
1862 local->resuming = false;
1864 list_for_each_entry(sdata, &local->interfaces, list) {
1865 if (!ieee80211_sdata_running(sdata))
1867 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1868 ieee80211_sta_restart(sdata);
1871 mod_timer(&local->sta_cleanup, jiffies + 1);
1879 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1881 struct ieee80211_sub_if_data *sdata;
1882 struct ieee80211_local *local;
1883 struct ieee80211_key *key;
1888 sdata = vif_to_sdata(vif);
1889 local = sdata->local;
1891 if (WARN_ON(!local->resuming))
1894 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1897 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1899 mutex_lock(&local->key_mtx);
1900 list_for_each_entry(key, &sdata->key_list, list)
1901 key->flags |= KEY_FLAG_TAINTED;
1902 mutex_unlock(&local->key_mtx);
1904 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1906 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
1908 struct ieee80211_local *local = sdata->local;
1909 struct ieee80211_chanctx_conf *chanctx_conf;
1910 struct ieee80211_chanctx *chanctx;
1912 mutex_lock(&local->chanctx_mtx);
1914 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1915 lockdep_is_held(&local->chanctx_mtx));
1917 if (WARN_ON_ONCE(!chanctx_conf))
1920 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1921 ieee80211_recalc_smps_chanctx(local, chanctx);
1923 mutex_unlock(&local->chanctx_mtx);
1926 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
1928 struct ieee80211_local *local = sdata->local;
1929 struct ieee80211_chanctx_conf *chanctx_conf;
1930 struct ieee80211_chanctx *chanctx;
1932 mutex_lock(&local->chanctx_mtx);
1934 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1935 lockdep_is_held(&local->chanctx_mtx));
1937 if (WARN_ON_ONCE(!chanctx_conf))
1940 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1941 ieee80211_recalc_chanctx_min_def(local, chanctx);
1943 mutex_unlock(&local->chanctx_mtx);
1946 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1950 for (i = 0; i < n_ids; i++)
1957 * ieee80211_ie_split - split an IE buffer according to ordering
1959 * @ies: the IE buffer
1960 * @ielen: the length of the IE buffer
1961 * @ids: an array with element IDs that are allowed before
1963 * @n_ids: the size of the element ID array
1964 * @offset: offset where to start splitting in the buffer
1966 * This function splits an IE buffer by updating the @offset
1967 * variable to point to the location where the buffer should be
1970 * It assumes that the given IE buffer is well-formed, this
1971 * has to be guaranteed by the caller!
1973 * It also assumes that the IEs in the buffer are ordered
1974 * correctly, if not the result of using this function will not
1975 * be ordered correctly either, i.e. it does no reordering.
1977 * The function returns the offset where the next part of the
1978 * buffer starts, which may be @ielen if the entire (remainder)
1979 * of the buffer should be used.
1981 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1982 const u8 *ids, int n_ids, size_t offset)
1984 size_t pos = offset;
1986 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1987 pos += 2 + ies[pos + 1];
1992 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1994 size_t pos = offset;
1996 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1997 pos += 2 + ies[pos + 1];
2002 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2006 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2008 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2012 * Scale up threshold values before storing it, as the RSSI averaging
2013 * algorithm uses a scaled up value as well. Change this scaling
2014 * factor if the RSSI averaging algorithm changes.
2016 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2017 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2020 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2024 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2026 WARN_ON(rssi_min_thold == rssi_max_thold ||
2027 rssi_min_thold > rssi_max_thold);
2029 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2032 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2034 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2036 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2038 _ieee80211_enable_rssi_reports(sdata, 0, 0);
2040 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
2042 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2047 *pos++ = WLAN_EID_HT_CAPABILITY;
2048 *pos++ = sizeof(struct ieee80211_ht_cap);
2049 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
2051 /* capability flags */
2052 tmp = cpu_to_le16(cap);
2053 memcpy(pos, &tmp, sizeof(u16));
2056 /* AMPDU parameters */
2057 *pos++ = ht_cap->ampdu_factor |
2058 (ht_cap->ampdu_density <<
2059 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
2062 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2063 pos += sizeof(ht_cap->mcs);
2065 /* extended capabilities */
2066 pos += sizeof(__le16);
2068 /* BF capabilities */
2069 pos += sizeof(__le32);
2071 /* antenna selection */
2077 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2082 *pos++ = WLAN_EID_VHT_CAPABILITY;
2083 *pos++ = sizeof(struct ieee80211_vht_cap);
2084 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2086 /* capability flags */
2087 tmp = cpu_to_le32(cap);
2088 memcpy(pos, &tmp, sizeof(u32));
2092 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2093 pos += sizeof(vht_cap->vht_mcs);
2098 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2099 const struct cfg80211_chan_def *chandef,
2102 struct ieee80211_ht_operation *ht_oper;
2103 /* Build HT Information */
2104 *pos++ = WLAN_EID_HT_OPERATION;
2105 *pos++ = sizeof(struct ieee80211_ht_operation);
2106 ht_oper = (struct ieee80211_ht_operation *)pos;
2107 ht_oper->primary_chan = ieee80211_frequency_to_channel(
2108 chandef->chan->center_freq);
2109 switch (chandef->width) {
2110 case NL80211_CHAN_WIDTH_160:
2111 case NL80211_CHAN_WIDTH_80P80:
2112 case NL80211_CHAN_WIDTH_80:
2113 case NL80211_CHAN_WIDTH_40:
2114 if (chandef->center_freq1 > chandef->chan->center_freq)
2115 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2117 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2120 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
2123 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
2124 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
2125 chandef->width != NL80211_CHAN_WIDTH_20)
2126 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2128 ht_oper->operation_mode = cpu_to_le16(prot_mode);
2129 ht_oper->stbc_param = 0x0000;
2131 /* It seems that Basic MCS set and Supported MCS set
2132 are identical for the first 10 bytes */
2133 memset(&ht_oper->basic_set, 0, 16);
2134 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
2136 return pos + sizeof(struct ieee80211_ht_operation);
2139 void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
2140 const struct ieee80211_ht_operation *ht_oper,
2141 struct cfg80211_chan_def *chandef)
2143 enum nl80211_channel_type channel_type;
2146 cfg80211_chandef_create(chandef, control_chan,
2147 NL80211_CHAN_NO_HT);
2151 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
2152 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
2153 channel_type = NL80211_CHAN_HT20;
2155 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
2156 channel_type = NL80211_CHAN_HT40PLUS;
2158 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
2159 channel_type = NL80211_CHAN_HT40MINUS;
2162 channel_type = NL80211_CHAN_NO_HT;
2165 cfg80211_chandef_create(chandef, control_chan, channel_type);
2168 int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
2169 const struct ieee80211_supported_band *sband,
2170 const u8 *srates, int srates_len, u32 *rates)
2172 u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
2173 int shift = ieee80211_chandef_get_shift(chandef);
2174 struct ieee80211_rate *br;
2175 int brate, rate, i, j, count = 0;
2179 for (i = 0; i < srates_len; i++) {
2180 rate = srates[i] & 0x7f;
2182 for (j = 0; j < sband->n_bitrates; j++) {
2183 br = &sband->bitrates[j];
2184 if ((rate_flags & br->flags) != rate_flags)
2187 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2188 if (brate == rate) {
2198 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
2199 struct sk_buff *skb, bool need_basic,
2200 enum ieee80211_band band)
2202 struct ieee80211_local *local = sdata->local;
2203 struct ieee80211_supported_band *sband;
2206 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2209 shift = ieee80211_vif_get_shift(&sdata->vif);
2210 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2211 sband = local->hw.wiphy->bands[band];
2213 for (i = 0; i < sband->n_bitrates; i++) {
2214 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2221 if (skb_tailroom(skb) < rates + 2)
2224 pos = skb_put(skb, rates + 2);
2225 *pos++ = WLAN_EID_SUPP_RATES;
2227 for (i = 0; i < rates; i++) {
2229 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2232 if (need_basic && basic_rates & BIT(i))
2234 rate = sband->bitrates[i].bitrate;
2235 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2237 *pos++ = basic | (u8) rate;
2243 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
2244 struct sk_buff *skb, bool need_basic,
2245 enum ieee80211_band band)
2247 struct ieee80211_local *local = sdata->local;
2248 struct ieee80211_supported_band *sband;
2250 u8 i, exrates, *pos;
2251 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2254 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2255 shift = ieee80211_vif_get_shift(&sdata->vif);
2257 sband = local->hw.wiphy->bands[band];
2259 for (i = 0; i < sband->n_bitrates; i++) {
2260 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2270 if (skb_tailroom(skb) < exrates + 2)
2274 pos = skb_put(skb, exrates + 2);
2275 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2277 for (i = 8; i < sband->n_bitrates; i++) {
2279 if ((rate_flags & sband->bitrates[i].flags)
2282 if (need_basic && basic_rates & BIT(i))
2284 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2286 *pos++ = basic | (u8) rate;
2292 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
2294 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2295 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2297 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2298 /* non-managed type inferfaces */
2301 return ifmgd->ave_beacon_signal / 16;
2303 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
2305 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
2310 /* TODO: consider rx_highest */
2312 if (mcs->rx_mask[3])
2314 if (mcs->rx_mask[2])
2316 if (mcs->rx_mask[1])
2322 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2323 * @local: mac80211 hw info struct
2324 * @status: RX status
2325 * @mpdu_len: total MPDU length (including FCS)
2326 * @mpdu_offset: offset into MPDU to calculate timestamp at
2328 * This function calculates the RX timestamp at the given MPDU offset, taking
2329 * into account what the RX timestamp was. An offset of 0 will just normalize
2330 * the timestamp to TSF at beginning of MPDU reception.
2332 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2333 struct ieee80211_rx_status *status,
2334 unsigned int mpdu_len,
2335 unsigned int mpdu_offset)
2337 u64 ts = status->mactime;
2338 struct rate_info ri;
2341 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2344 memset(&ri, 0, sizeof(ri));
2346 /* Fill cfg80211 rate info */
2347 if (status->flag & RX_FLAG_HT) {
2348 ri.mcs = status->rate_idx;
2349 ri.flags |= RATE_INFO_FLAGS_MCS;
2350 if (status->flag & RX_FLAG_40MHZ)
2351 ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2352 if (status->flag & RX_FLAG_SHORT_GI)
2353 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2354 } else if (status->flag & RX_FLAG_VHT) {
2355 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
2356 ri.mcs = status->rate_idx;
2357 ri.nss = status->vht_nss;
2358 if (status->flag & RX_FLAG_40MHZ)
2359 ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2360 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
2361 ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
2362 if (status->vht_flag & RX_VHT_FLAG_80P80MHZ)
2363 ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
2364 if (status->vht_flag & RX_VHT_FLAG_160MHZ)
2365 ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
2366 if (status->flag & RX_FLAG_SHORT_GI)
2367 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2369 struct ieee80211_supported_band *sband;
2373 if (status->flag & RX_FLAG_10MHZ)
2375 if (status->flag & RX_FLAG_5MHZ)
2378 sband = local->hw.wiphy->bands[status->band];
2379 bitrate = sband->bitrates[status->rate_idx].bitrate;
2380 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
2383 rate = cfg80211_calculate_bitrate(&ri);
2384 if (WARN_ONCE(!rate,
2385 "Invalid bitrate: flags=0x%x, idx=%d, vht_nss=%d\n",
2386 status->flag, status->rate_idx, status->vht_nss))
2389 /* rewind from end of MPDU */
2390 if (status->flag & RX_FLAG_MACTIME_END)
2391 ts -= mpdu_len * 8 * 10 / rate;
2393 ts += mpdu_offset * 8 * 10 / rate;
2398 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2400 struct ieee80211_sub_if_data *sdata;
2401 struct cfg80211_chan_def chandef;
2403 mutex_lock(&local->mtx);
2404 mutex_lock(&local->iflist_mtx);
2405 list_for_each_entry(sdata, &local->interfaces, list) {
2406 /* it might be waiting for the local->mtx, but then
2407 * by the time it gets it, sdata->wdev.cac_started
2408 * will no longer be true
2410 cancel_delayed_work(&sdata->dfs_cac_timer_work);
2412 if (sdata->wdev.cac_started) {
2413 chandef = sdata->vif.bss_conf.chandef;
2414 ieee80211_vif_release_channel(sdata);
2415 cfg80211_cac_event(sdata->dev,
2417 NL80211_RADAR_CAC_ABORTED,
2421 mutex_unlock(&local->iflist_mtx);
2422 mutex_unlock(&local->mtx);
2425 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2427 struct ieee80211_local *local =
2428 container_of(work, struct ieee80211_local, radar_detected_work);
2429 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
2431 ieee80211_dfs_cac_cancel(local);
2433 if (local->use_chanctx)
2434 /* currently not handled */
2437 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
2440 void ieee80211_radar_detected(struct ieee80211_hw *hw)
2442 struct ieee80211_local *local = hw_to_local(hw);
2444 trace_api_radar_detected(local);
2446 ieee80211_queue_work(hw, &local->radar_detected_work);
2448 EXPORT_SYMBOL(ieee80211_radar_detected);
2450 u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2456 case NL80211_CHAN_WIDTH_20:
2457 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2458 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2460 case NL80211_CHAN_WIDTH_40:
2461 c->width = NL80211_CHAN_WIDTH_20;
2462 c->center_freq1 = c->chan->center_freq;
2463 ret = IEEE80211_STA_DISABLE_40MHZ |
2464 IEEE80211_STA_DISABLE_VHT;
2466 case NL80211_CHAN_WIDTH_80:
2467 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2471 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2472 c->width = NL80211_CHAN_WIDTH_40;
2473 ret = IEEE80211_STA_DISABLE_VHT;
2475 case NL80211_CHAN_WIDTH_80P80:
2476 c->center_freq2 = 0;
2477 c->width = NL80211_CHAN_WIDTH_80;
2478 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2479 IEEE80211_STA_DISABLE_160MHZ;
2481 case NL80211_CHAN_WIDTH_160:
2483 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2486 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2487 c->width = NL80211_CHAN_WIDTH_80;
2488 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2489 IEEE80211_STA_DISABLE_160MHZ;
2492 case NL80211_CHAN_WIDTH_20_NOHT:
2494 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2495 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2497 case NL80211_CHAN_WIDTH_5:
2498 case NL80211_CHAN_WIDTH_10:
2501 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2505 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2511 * Returns true if smps_mode_new is strictly more restrictive than
2514 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2515 enum ieee80211_smps_mode smps_mode_new)
2517 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2518 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2521 switch (smps_mode_old) {
2522 case IEEE80211_SMPS_STATIC:
2524 case IEEE80211_SMPS_DYNAMIC:
2525 return smps_mode_new == IEEE80211_SMPS_STATIC;
2526 case IEEE80211_SMPS_OFF:
2527 return smps_mode_new != IEEE80211_SMPS_OFF;
2535 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2536 struct cfg80211_csa_settings *csa_settings)
2538 struct sk_buff *skb;
2539 struct ieee80211_mgmt *mgmt;
2540 struct ieee80211_local *local = sdata->local;
2542 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2543 sizeof(mgmt->u.action.u.chan_switch);
2546 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2547 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2550 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2551 5 + /* channel switch announcement element */
2552 3 + /* secondary channel offset element */
2553 8); /* mesh channel switch parameters element */
2557 skb_reserve(skb, local->tx_headroom);
2558 mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2559 memset(mgmt, 0, hdr_len);
2560 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2561 IEEE80211_STYPE_ACTION);
2563 eth_broadcast_addr(mgmt->da);
2564 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2565 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2566 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2568 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2569 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2571 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2572 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2573 pos = skb_put(skb, 5);
2574 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
2575 *pos++ = 3; /* IE length */
2576 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
2577 freq = csa_settings->chandef.chan->center_freq;
2578 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
2579 *pos++ = csa_settings->count; /* count */
2581 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2582 enum nl80211_channel_type ch_type;
2585 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
2586 *pos++ = 1; /* IE length */
2587 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
2588 if (ch_type == NL80211_CHAN_HT40PLUS)
2589 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2591 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2594 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2595 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2598 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
2599 *pos++ = 6; /* IE length */
2600 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
2601 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
2602 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
2603 *pos++ |= csa_settings->block_tx ?
2604 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
2605 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
2607 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
2611 ieee80211_tx_skb(sdata, skb);
2615 bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
2617 return !(cs == NULL || cs->cipher == 0 ||
2618 cs->hdr_len < cs->pn_len + cs->pn_off ||
2619 cs->hdr_len <= cs->key_idx_off ||
2620 cs->key_idx_shift > 7 ||
2621 cs->key_idx_mask == 0);
2624 bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
2628 /* Ensure we have enough iftype bitmap space for all iftype values */
2629 WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
2631 for (i = 0; i < n; i++)
2632 if (!ieee80211_cs_valid(&cs[i]))
2638 const struct ieee80211_cipher_scheme *
2639 ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
2640 enum nl80211_iftype iftype)
2642 const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
2643 int n = local->hw.n_cipher_schemes;
2645 const struct ieee80211_cipher_scheme *cs = NULL;
2647 for (i = 0; i < n; i++) {
2648 if (l[i].cipher == cipher) {
2654 if (!cs || !(cs->iftype & BIT(iftype)))
2660 int ieee80211_cs_headroom(struct ieee80211_local *local,
2661 struct cfg80211_crypto_settings *crypto,
2662 enum nl80211_iftype iftype)
2664 const struct ieee80211_cipher_scheme *cs;
2665 int headroom = IEEE80211_ENCRYPT_HEADROOM;
2668 for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
2669 cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
2672 if (cs && headroom < cs->hdr_len)
2673 headroom = cs->hdr_len;
2676 cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
2677 if (cs && headroom < cs->hdr_len)
2678 headroom = cs->hdr_len;
2684 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
2686 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
2692 /* End time is in the past, check for repetitions */
2693 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
2694 if (data->count[i] < 255) {
2695 if (data->count[i] <= skip) {
2700 data->count[i] -= skip;
2703 data->desc[i].start += skip * data->desc[i].interval;
2709 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
2715 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2718 if (!data->count[i])
2721 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
2724 cur = data->desc[i].start - tsf;
2728 cur = data->desc[i].start + data->desc[i].duration - tsf;
2737 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
2742 * arbitrary limit, used to avoid infinite loops when combined NoA
2743 * descriptors cover the full time period.
2747 ieee80211_extend_absent_time(data, tsf, &offset);
2749 if (!ieee80211_extend_absent_time(data, tsf, &offset))
2753 } while (tries < max_tries);
2758 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
2760 u32 next_offset = BIT(31) - 1;
2764 data->has_next_tsf = false;
2765 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2768 if (!data->count[i])
2771 ieee80211_extend_noa_desc(data, tsf, i);
2772 start = data->desc[i].start - tsf;
2774 data->absent |= BIT(i);
2776 if (next_offset > start)
2777 next_offset = start;
2779 data->has_next_tsf = true;
2783 next_offset = ieee80211_get_noa_absent_time(data, tsf);
2785 data->next_tsf = tsf + next_offset;
2787 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
2789 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
2790 struct ieee80211_noa_data *data, u32 tsf)
2795 memset(data, 0, sizeof(*data));
2797 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2798 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
2800 if (!desc->count || !desc->duration)
2803 data->count[i] = desc->count;
2804 data->desc[i].start = le32_to_cpu(desc->start_time);
2805 data->desc[i].duration = le32_to_cpu(desc->duration);
2806 data->desc[i].interval = le32_to_cpu(desc->interval);
2808 if (data->count[i] > 1 &&
2809 data->desc[i].interval < data->desc[i].duration)
2812 ieee80211_extend_noa_desc(data, tsf, i);
2817 ieee80211_update_p2p_noa(data, tsf);
2821 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
2823 void ieee80211_recalc_dtim(struct ieee80211_local *local,
2824 struct ieee80211_sub_if_data *sdata)
2826 u64 tsf = drv_get_tsf(local, sdata);
2828 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
2829 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
2833 if (tsf == -1ULL || !beacon_int || !dtim_period)
2836 if (sdata->vif.type == NL80211_IFTYPE_AP ||
2837 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
2841 ps = &sdata->bss->ps;
2842 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2843 ps = &sdata->u.mesh.ps;
2849 * actually finds last dtim_count, mac80211 will update in
2850 * __beacon_add_tim().
2851 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
2853 do_div(tsf, beacon_int);
2854 bcns_from_dtim = do_div(tsf, dtim_period);
2855 /* just had a DTIM */
2856 if (!bcns_from_dtim)
2859 dtim_count = dtim_period - bcns_from_dtim;
2861 ps->dtim_count = dtim_count;
2864 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
2865 const struct cfg80211_chan_def *chandef,
2866 enum ieee80211_chanctx_mode chanmode,
2869 struct ieee80211_local *local = sdata->local;
2870 struct ieee80211_sub_if_data *sdata_iter;
2871 enum nl80211_iftype iftype = sdata->wdev.iftype;
2872 int num[NUM_NL80211_IFTYPES];
2873 struct ieee80211_chanctx *ctx;
2874 int num_different_channels = 0;
2877 lockdep_assert_held(&local->chanctx_mtx);
2879 if (WARN_ON(hweight32(radar_detect) > 1))
2882 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
2887 num_different_channels = 1;
2889 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
2892 /* Always allow software iftypes */
2893 if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
2899 memset(num, 0, sizeof(num));
2901 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
2904 list_for_each_entry(ctx, &local->chanctx_list, list) {
2905 if (ctx->conf.radar_enabled)
2906 radar_detect |= BIT(ctx->conf.def.width);
2907 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
2908 num_different_channels++;
2911 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
2912 cfg80211_chandef_compatible(chandef,
2915 num_different_channels++;
2918 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
2919 struct wireless_dev *wdev_iter;
2921 wdev_iter = &sdata_iter->wdev;
2923 if (sdata_iter == sdata ||
2924 rcu_access_pointer(sdata_iter->vif.chanctx_conf) == NULL ||
2925 local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
2928 num[wdev_iter->iftype]++;
2932 if (total == 1 && !radar_detect)
2935 return cfg80211_check_combinations(local->hw.wiphy,
2936 num_different_channels,
2941 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
2944 u32 *max_num_different_channels = data;
2946 *max_num_different_channels = max(*max_num_different_channels,
2947 c->num_different_channels);
2950 int ieee80211_max_num_channels(struct ieee80211_local *local)
2952 struct ieee80211_sub_if_data *sdata;
2953 int num[NUM_NL80211_IFTYPES] = {};
2954 struct ieee80211_chanctx *ctx;
2955 int num_different_channels = 0;
2956 u8 radar_detect = 0;
2957 u32 max_num_different_channels = 1;
2960 lockdep_assert_held(&local->chanctx_mtx);
2962 list_for_each_entry(ctx, &local->chanctx_list, list) {
2963 num_different_channels++;
2965 if (ctx->conf.radar_enabled)
2966 radar_detect |= BIT(ctx->conf.def.width);
2969 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2970 num[sdata->wdev.iftype]++;
2972 err = cfg80211_iter_combinations(local->hw.wiphy,
2973 num_different_channels, radar_detect,
2974 num, ieee80211_iter_max_chans,
2975 &max_num_different_channels);
2979 return max_num_different_channels;