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/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/bitmap.h>
22 #include <net/net_namespace.h>
23 #include <net/cfg80211.h>
24 #include <net/rtnetlink.h>
26 #include "ieee80211_i.h"
27 #include "driver-ops.h"
34 /* privid for wiphys to determine whether they belong to us or not */
35 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
37 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
39 struct ieee80211_local *local;
42 local = wiphy_priv(wiphy);
45 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
47 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
48 enum nl80211_iftype type)
50 __le16 fc = hdr->frame_control;
52 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
56 if (ieee80211_is_data(fc)) {
57 if (len < 24) /* drop incorrect hdr len (data) */
60 if (ieee80211_has_a4(fc))
62 if (ieee80211_has_tods(fc))
64 if (ieee80211_has_fromds(fc))
70 if (ieee80211_is_mgmt(fc)) {
71 if (len < 24) /* drop incorrect hdr len (mgmt) */
76 if (ieee80211_is_ctl(fc)) {
77 if(ieee80211_is_pspoll(fc))
80 if (ieee80211_is_back_req(fc)) {
82 case NL80211_IFTYPE_STATION:
84 case NL80211_IFTYPE_AP:
85 case NL80211_IFTYPE_AP_VLAN:
88 break; /* fall through to the return */
96 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
98 struct sk_buff *skb = tx->skb;
99 struct ieee80211_hdr *hdr;
102 hdr = (struct ieee80211_hdr *) skb->data;
103 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
104 } while ((skb = skb->next));
107 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
108 int rate, int erp, int short_preamble)
112 /* calculate duration (in microseconds, rounded up to next higher
113 * integer if it includes a fractional microsecond) to send frame of
114 * len bytes (does not include FCS) at the given rate. Duration will
117 * rate is in 100 kbps, so divident is multiplied by 10 in the
118 * DIV_ROUND_UP() operations.
121 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
125 * N_DBPS = DATARATE x 4
126 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
127 * (16 = SIGNAL time, 6 = tail bits)
128 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
131 * 802.11a - 17.5.2: aSIFSTime = 16 usec
132 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
133 * signal ext = 6 usec
135 dur = 16; /* SIFS + signal ext */
136 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
137 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
138 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
139 4 * rate); /* T_SYM x N_SYM */
142 * 802.11b or 802.11g with 802.11b compatibility:
143 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
144 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
146 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
147 * aSIFSTime = 10 usec
148 * aPreambleLength = 144 usec or 72 usec with short preamble
149 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
151 dur = 10; /* aSIFSTime = 10 usec */
152 dur += short_preamble ? (72 + 24) : (144 + 48);
154 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
160 /* Exported duration function for driver use */
161 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
162 struct ieee80211_vif *vif,
164 struct ieee80211_rate *rate)
166 struct ieee80211_local *local = hw_to_local(hw);
167 struct ieee80211_sub_if_data *sdata;
170 bool short_preamble = false;
174 sdata = vif_to_sdata(vif);
175 short_preamble = sdata->vif.bss_conf.use_short_preamble;
176 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
177 erp = rate->flags & IEEE80211_RATE_ERP_G;
180 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
183 return cpu_to_le16(dur);
185 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
187 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
188 struct ieee80211_vif *vif, size_t frame_len,
189 const struct ieee80211_tx_info *frame_txctl)
191 struct ieee80211_local *local = hw_to_local(hw);
192 struct ieee80211_rate *rate;
193 struct ieee80211_sub_if_data *sdata;
197 struct ieee80211_supported_band *sband;
199 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
201 short_preamble = false;
203 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
207 sdata = vif_to_sdata(vif);
208 short_preamble = sdata->vif.bss_conf.use_short_preamble;
209 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
210 erp = rate->flags & IEEE80211_RATE_ERP_G;
214 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
215 erp, short_preamble);
216 /* Data frame duration */
217 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
218 erp, short_preamble);
220 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
221 erp, short_preamble);
223 return cpu_to_le16(dur);
225 EXPORT_SYMBOL(ieee80211_rts_duration);
227 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
228 struct ieee80211_vif *vif,
230 const struct ieee80211_tx_info *frame_txctl)
232 struct ieee80211_local *local = hw_to_local(hw);
233 struct ieee80211_rate *rate;
234 struct ieee80211_sub_if_data *sdata;
238 struct ieee80211_supported_band *sband;
240 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
242 short_preamble = false;
244 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
247 sdata = vif_to_sdata(vif);
248 short_preamble = sdata->vif.bss_conf.use_short_preamble;
249 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
250 erp = rate->flags & IEEE80211_RATE_ERP_G;
253 /* Data frame duration */
254 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
255 erp, short_preamble);
256 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
258 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
259 erp, short_preamble);
262 return cpu_to_le16(dur);
264 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
266 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
267 enum queue_stop_reason reason)
269 struct ieee80211_local *local = hw_to_local(hw);
270 struct ieee80211_sub_if_data *sdata;
272 trace_wake_queue(local, queue, reason);
274 if (WARN_ON(queue >= hw->queues))
277 __clear_bit(reason, &local->queue_stop_reasons[queue]);
279 if (local->queue_stop_reasons[queue] != 0)
280 /* someone still has this queue stopped */
283 if (skb_queue_empty(&local->pending[queue])) {
285 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
286 if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
288 netif_wake_subqueue(sdata->dev, queue);
292 tasklet_schedule(&local->tx_pending_tasklet);
295 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
296 enum queue_stop_reason reason)
298 struct ieee80211_local *local = hw_to_local(hw);
301 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
302 __ieee80211_wake_queue(hw, queue, reason);
303 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
306 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
308 ieee80211_wake_queue_by_reason(hw, queue,
309 IEEE80211_QUEUE_STOP_REASON_DRIVER);
311 EXPORT_SYMBOL(ieee80211_wake_queue);
313 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
314 enum queue_stop_reason reason)
316 struct ieee80211_local *local = hw_to_local(hw);
317 struct ieee80211_sub_if_data *sdata;
319 trace_stop_queue(local, queue, reason);
321 if (WARN_ON(queue >= hw->queues))
324 __set_bit(reason, &local->queue_stop_reasons[queue]);
327 list_for_each_entry_rcu(sdata, &local->interfaces, list)
328 netif_stop_subqueue(sdata->dev, queue);
332 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
333 enum queue_stop_reason reason)
335 struct ieee80211_local *local = hw_to_local(hw);
338 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
339 __ieee80211_stop_queue(hw, queue, reason);
340 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
343 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
345 ieee80211_stop_queue_by_reason(hw, queue,
346 IEEE80211_QUEUE_STOP_REASON_DRIVER);
348 EXPORT_SYMBOL(ieee80211_stop_queue);
350 void ieee80211_add_pending_skb(struct ieee80211_local *local,
353 struct ieee80211_hw *hw = &local->hw;
355 int queue = skb_get_queue_mapping(skb);
356 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
358 if (WARN_ON(!info->control.vif)) {
363 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
364 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
365 __skb_queue_tail(&local->pending[queue], skb);
366 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
367 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
370 void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
371 struct sk_buff_head *skbs,
372 void (*fn)(void *data), void *data)
374 struct ieee80211_hw *hw = &local->hw;
379 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
380 for (i = 0; i < hw->queues; i++)
381 __ieee80211_stop_queue(hw, i,
382 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
384 while ((skb = skb_dequeue(skbs))) {
385 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
387 if (WARN_ON(!info->control.vif)) {
392 queue = skb_get_queue_mapping(skb);
393 __skb_queue_tail(&local->pending[queue], skb);
399 for (i = 0; i < hw->queues; i++)
400 __ieee80211_wake_queue(hw, i,
401 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
402 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
405 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
406 struct sk_buff_head *skbs)
408 ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
411 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
412 enum queue_stop_reason reason)
414 struct ieee80211_local *local = hw_to_local(hw);
418 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
420 for (i = 0; i < hw->queues; i++)
421 __ieee80211_stop_queue(hw, i, reason);
423 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
426 void ieee80211_stop_queues(struct ieee80211_hw *hw)
428 ieee80211_stop_queues_by_reason(hw,
429 IEEE80211_QUEUE_STOP_REASON_DRIVER);
431 EXPORT_SYMBOL(ieee80211_stop_queues);
433 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
435 struct ieee80211_local *local = hw_to_local(hw);
439 if (WARN_ON(queue >= hw->queues))
442 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
443 ret = !!local->queue_stop_reasons[queue];
444 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
447 EXPORT_SYMBOL(ieee80211_queue_stopped);
449 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
450 enum queue_stop_reason reason)
452 struct ieee80211_local *local = hw_to_local(hw);
456 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
458 for (i = 0; i < hw->queues; i++)
459 __ieee80211_wake_queue(hw, i, reason);
461 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
464 void ieee80211_wake_queues(struct ieee80211_hw *hw)
466 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
468 EXPORT_SYMBOL(ieee80211_wake_queues);
470 void ieee80211_iterate_active_interfaces(
471 struct ieee80211_hw *hw,
472 void (*iterator)(void *data, u8 *mac,
473 struct ieee80211_vif *vif),
476 struct ieee80211_local *local = hw_to_local(hw);
477 struct ieee80211_sub_if_data *sdata;
479 mutex_lock(&local->iflist_mtx);
481 list_for_each_entry(sdata, &local->interfaces, list) {
482 switch (sdata->vif.type) {
483 case NL80211_IFTYPE_MONITOR:
484 case NL80211_IFTYPE_AP_VLAN:
489 if (ieee80211_sdata_running(sdata))
490 iterator(data, sdata->vif.addr,
494 mutex_unlock(&local->iflist_mtx);
496 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
498 void ieee80211_iterate_active_interfaces_atomic(
499 struct ieee80211_hw *hw,
500 void (*iterator)(void *data, u8 *mac,
501 struct ieee80211_vif *vif),
504 struct ieee80211_local *local = hw_to_local(hw);
505 struct ieee80211_sub_if_data *sdata;
509 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
510 switch (sdata->vif.type) {
511 case NL80211_IFTYPE_MONITOR:
512 case NL80211_IFTYPE_AP_VLAN:
517 if (ieee80211_sdata_running(sdata))
518 iterator(data, sdata->vif.addr,
524 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
527 * Nothing should have been stuffed into the workqueue during
528 * the suspend->resume cycle. If this WARN is seen then there
529 * is a bug with either the driver suspend or something in
530 * mac80211 stuffing into the workqueue which we haven't yet
531 * cleared during mac80211's suspend cycle.
533 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
535 if (WARN(local->suspended && !local->resuming,
536 "queueing ieee80211 work while going to suspend\n"))
542 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
544 struct ieee80211_local *local = hw_to_local(hw);
546 if (!ieee80211_can_queue_work(local))
549 queue_work(local->workqueue, work);
551 EXPORT_SYMBOL(ieee80211_queue_work);
553 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
554 struct delayed_work *dwork,
557 struct ieee80211_local *local = hw_to_local(hw);
559 if (!ieee80211_can_queue_work(local))
562 queue_delayed_work(local->workqueue, dwork, delay);
564 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
566 void ieee802_11_parse_elems(u8 *start, size_t len,
567 struct ieee802_11_elems *elems)
569 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
572 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
574 struct ieee80211_local *local = sdata->local;
575 struct ieee80211_tx_queue_params qparam;
580 if (!local->ops->conf_tx)
583 memset(&qparam, 0, sizeof(qparam));
585 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
586 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
588 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
589 /* Set defaults according to 802.11-2007 Table 7-37 */
598 qparam.cw_max = aCWmax;
599 qparam.cw_min = aCWmin;
603 default: /* never happens but let's not leave undefined */
605 qparam.cw_max = aCWmax;
606 qparam.cw_min = aCWmin;
611 qparam.cw_max = aCWmin;
612 qparam.cw_min = (aCWmin + 1) / 2 - 1;
614 qparam.txop = 6016/32;
616 qparam.txop = 3008/32;
620 qparam.cw_max = (aCWmin + 1) / 2 - 1;
621 qparam.cw_min = (aCWmin + 1) / 4 - 1;
623 qparam.txop = 3264/32;
625 qparam.txop = 1504/32;
630 qparam.uapsd = false;
632 sdata->tx_conf[queue] = qparam;
633 drv_conf_tx(local, sdata, queue, &qparam);
636 /* after reinitialize QoS TX queues setting to default,
637 * disable QoS at all */
639 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
640 sdata->vif.bss_conf.qos =
641 sdata->vif.type != NL80211_IFTYPE_STATION;
642 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
646 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
647 const size_t supp_rates_len,
648 const u8 *supp_rates)
650 struct ieee80211_local *local = sdata->local;
651 int i, have_higher_than_11mbit = 0;
653 /* cf. IEEE 802.11 9.2.12 */
654 for (i = 0; i < supp_rates_len; i++)
655 if ((supp_rates[i] & 0x7f) * 5 > 110)
656 have_higher_than_11mbit = 1;
658 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
659 have_higher_than_11mbit)
660 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
662 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
664 ieee80211_set_wmm_default(sdata);
667 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
668 enum ieee80211_band band)
670 struct ieee80211_supported_band *sband;
671 struct ieee80211_rate *bitrates;
673 enum ieee80211_rate_flags mandatory_flag;
676 sband = local->hw.wiphy->bands[band];
679 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
682 if (band == IEEE80211_BAND_2GHZ)
683 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
685 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
687 bitrates = sband->bitrates;
689 for (i = 0; i < sband->n_bitrates; i++)
690 if (bitrates[i].flags & mandatory_flag)
691 mandatory_rates |= BIT(i);
692 return mandatory_rates;
695 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
696 u16 transaction, u16 auth_alg,
697 u8 *extra, size_t extra_len, const u8 *bssid,
698 const u8 *key, u8 key_len, u8 key_idx)
700 struct ieee80211_local *local = sdata->local;
702 struct ieee80211_mgmt *mgmt;
705 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
706 sizeof(*mgmt) + 6 + extra_len);
710 skb_reserve(skb, local->hw.extra_tx_headroom);
712 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
713 memset(mgmt, 0, 24 + 6);
714 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
715 IEEE80211_STYPE_AUTH);
716 memcpy(mgmt->da, bssid, ETH_ALEN);
717 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
718 memcpy(mgmt->bssid, bssid, ETH_ALEN);
719 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
720 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
721 mgmt->u.auth.status_code = cpu_to_le16(0);
723 memcpy(skb_put(skb, extra_len), extra, extra_len);
725 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
726 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
727 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
731 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
732 ieee80211_tx_skb(sdata, skb);
735 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
736 const u8 *ie, size_t ie_len,
737 enum ieee80211_band band, u32 rate_mask,
740 struct ieee80211_supported_band *sband;
742 size_t offset = 0, noffset;
743 int supp_rates_len, i;
748 sband = local->hw.wiphy->bands[band];
753 for (i = 0; i < sband->n_bitrates; i++) {
754 if ((BIT(i) & rate_mask) == 0)
755 continue; /* skip rate */
756 rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
759 supp_rates_len = min_t(int, num_rates, 8);
761 *pos++ = WLAN_EID_SUPP_RATES;
762 *pos++ = supp_rates_len;
763 memcpy(pos, rates, supp_rates_len);
764 pos += supp_rates_len;
766 /* insert "request information" if in custom IEs */
768 static const u8 before_extrates[] = {
773 noffset = ieee80211_ie_split(ie, ie_len,
775 ARRAY_SIZE(before_extrates),
777 memcpy(pos, ie + offset, noffset - offset);
778 pos += noffset - offset;
782 ext_rates_len = num_rates - supp_rates_len;
783 if (ext_rates_len > 0) {
784 *pos++ = WLAN_EID_EXT_SUPP_RATES;
785 *pos++ = ext_rates_len;
786 memcpy(pos, rates + supp_rates_len, ext_rates_len);
787 pos += ext_rates_len;
790 if (channel && sband->band == IEEE80211_BAND_2GHZ) {
791 *pos++ = WLAN_EID_DS_PARAMS;
796 /* insert custom IEs that go before HT */
798 static const u8 before_ht[] = {
802 WLAN_EID_EXT_SUPP_RATES,
804 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
806 noffset = ieee80211_ie_split(ie, ie_len,
807 before_ht, ARRAY_SIZE(before_ht),
809 memcpy(pos, ie + offset, noffset - offset);
810 pos += noffset - offset;
814 if (sband->ht_cap.ht_supported)
815 pos = ieee80211_ie_build_ht_cap(pos, sband, sband->ht_cap.cap);
818 * If adding more here, adjust code in main.c
819 * that calculates local->scan_ies_len.
822 /* add any remaining custom IEs */
825 memcpy(pos, ie + offset, noffset - offset);
826 pos += noffset - offset;
832 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
833 u8 *dst, u32 ratemask,
834 const u8 *ssid, size_t ssid_len,
835 const u8 *ie, size_t ie_len,
838 struct ieee80211_local *local = sdata->local;
840 struct ieee80211_mgmt *mgmt;
845 /* FIXME: come up with a proper value */
846 buf = kmalloc(200 + ie_len, GFP_KERNEL);
851 * Do not send DS Channel parameter for directed probe requests
852 * in order to maximize the chance that we get a response. Some
853 * badly-behaved APs don't respond when this parameter is included.
858 chan = ieee80211_frequency_to_channel(
859 local->hw.conf.channel->center_freq);
861 buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
862 local->hw.conf.channel->band,
865 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
870 mgmt = (struct ieee80211_mgmt *) skb->data;
871 memcpy(mgmt->da, dst, ETH_ALEN);
872 memcpy(mgmt->bssid, dst, ETH_ALEN);
875 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
881 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
882 const u8 *ssid, size_t ssid_len,
883 const u8 *ie, size_t ie_len,
884 u32 ratemask, bool directed, bool no_cck)
888 skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
889 ie, ie_len, directed);
892 IEEE80211_SKB_CB(skb)->flags |=
893 IEEE80211_TX_CTL_NO_CCK_RATE;
894 ieee80211_tx_skb(sdata, skb);
898 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
899 struct ieee802_11_elems *elems,
900 enum ieee80211_band band)
902 struct ieee80211_supported_band *sband;
903 struct ieee80211_rate *bitrates;
907 sband = local->hw.wiphy->bands[band];
911 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
914 bitrates = sband->bitrates;
915 num_rates = sband->n_bitrates;
917 for (i = 0; i < elems->supp_rates_len +
918 elems->ext_supp_rates_len; i++) {
921 if (i < elems->supp_rates_len)
922 rate = elems->supp_rates[i];
923 else if (elems->ext_supp_rates)
924 rate = elems->ext_supp_rates
925 [i - elems->supp_rates_len];
926 own_rate = 5 * (rate & 0x7f);
927 for (j = 0; j < num_rates; j++)
928 if (bitrates[j].bitrate == own_rate)
929 supp_rates |= BIT(j);
934 void ieee80211_stop_device(struct ieee80211_local *local)
936 ieee80211_led_radio(local, false);
937 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
939 cancel_work_sync(&local->reconfig_filter);
941 flush_workqueue(local->workqueue);
945 int ieee80211_reconfig(struct ieee80211_local *local)
947 struct ieee80211_hw *hw = &local->hw;
948 struct ieee80211_sub_if_data *sdata;
949 struct sta_info *sta;
953 if (local->suspended)
954 local->resuming = true;
957 local->wowlan = false;
958 res = drv_resume(local);
960 local->resuming = false;
967 * res is 1, which means the driver requested
968 * to go through a regular reset on wakeup.
973 /* setup fragmentation threshold */
974 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
976 /* setup RTS threshold */
977 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
979 /* reset coverage class */
980 drv_set_coverage_class(local, hw->wiphy->coverage_class);
982 /* everything else happens only if HW was up & running */
983 if (!local->open_count)
987 * Upon resume hardware can sometimes be goofy due to
988 * various platform / driver / bus issues, so restarting
989 * the device may at times not work immediately. Propagate
992 res = drv_start(local);
994 WARN(local->suspended, "Hardware became unavailable "
995 "upon resume. This could be a software issue "
996 "prior to suspend or a hardware issue.\n");
1000 ieee80211_led_radio(local, true);
1001 ieee80211_mod_tpt_led_trig(local,
1002 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1004 /* add interfaces */
1005 list_for_each_entry(sdata, &local->interfaces, list) {
1006 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1007 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1008 ieee80211_sdata_running(sdata))
1009 res = drv_add_interface(local, &sdata->vif);
1013 mutex_lock(&local->sta_mtx);
1014 list_for_each_entry(sta, &local->sta_list, list) {
1015 if (sta->uploaded) {
1017 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1018 sdata = container_of(sdata->bss,
1019 struct ieee80211_sub_if_data,
1022 memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
1023 WARN_ON(drv_sta_add(local, sdata, &sta->sta));
1026 mutex_unlock(&local->sta_mtx);
1028 /* reconfigure tx conf */
1029 list_for_each_entry(sdata, &local->interfaces, list) {
1030 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1031 sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1032 !ieee80211_sdata_running(sdata))
1035 for (i = 0; i < hw->queues; i++)
1036 drv_conf_tx(local, sdata, i, &sdata->tx_conf[i]);
1039 /* reconfigure hardware */
1040 ieee80211_hw_config(local, ~0);
1042 ieee80211_configure_filter(local);
1044 /* Finally also reconfigure all the BSS information */
1045 list_for_each_entry(sdata, &local->interfaces, list) {
1048 if (!ieee80211_sdata_running(sdata))
1051 /* common change flags for all interface types */
1052 changed = BSS_CHANGED_ERP_CTS_PROT |
1053 BSS_CHANGED_ERP_PREAMBLE |
1054 BSS_CHANGED_ERP_SLOT |
1056 BSS_CHANGED_BASIC_RATES |
1057 BSS_CHANGED_BEACON_INT |
1062 switch (sdata->vif.type) {
1063 case NL80211_IFTYPE_STATION:
1064 changed |= BSS_CHANGED_ASSOC;
1065 mutex_lock(&sdata->u.mgd.mtx);
1066 ieee80211_bss_info_change_notify(sdata, changed);
1067 mutex_unlock(&sdata->u.mgd.mtx);
1069 case NL80211_IFTYPE_ADHOC:
1070 changed |= BSS_CHANGED_IBSS;
1072 case NL80211_IFTYPE_AP:
1073 changed |= BSS_CHANGED_SSID;
1075 case NL80211_IFTYPE_MESH_POINT:
1076 changed |= BSS_CHANGED_BEACON |
1077 BSS_CHANGED_BEACON_ENABLED;
1078 ieee80211_bss_info_change_notify(sdata, changed);
1080 case NL80211_IFTYPE_WDS:
1082 case NL80211_IFTYPE_AP_VLAN:
1083 case NL80211_IFTYPE_MONITOR:
1084 /* ignore virtual */
1086 case NL80211_IFTYPE_UNSPECIFIED:
1087 case NUM_NL80211_IFTYPES:
1088 case NL80211_IFTYPE_P2P_CLIENT:
1089 case NL80211_IFTYPE_P2P_GO:
1096 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1097 * sessions can be established after a resume.
1099 * Also tear down aggregation sessions since reconfiguring
1100 * them in a hardware restart scenario is not easily done
1101 * right now, and the hardware will have lost information
1102 * about the sessions, but we and the AP still think they
1103 * are active. This is really a workaround though.
1105 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1106 mutex_lock(&local->sta_mtx);
1108 list_for_each_entry(sta, &local->sta_list, list) {
1109 ieee80211_sta_tear_down_BA_sessions(sta, true);
1110 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
1113 mutex_unlock(&local->sta_mtx);
1117 list_for_each_entry(sdata, &local->interfaces, list)
1118 if (ieee80211_sdata_running(sdata))
1119 ieee80211_enable_keys(sdata);
1122 ieee80211_wake_queues_by_reason(hw,
1123 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1126 * If this is for hw restart things are still running.
1127 * We may want to change that later, however.
1129 if (!local->suspended)
1133 /* first set suspended false, then resuming */
1134 local->suspended = false;
1136 local->resuming = false;
1138 list_for_each_entry(sdata, &local->interfaces, list) {
1139 switch(sdata->vif.type) {
1140 case NL80211_IFTYPE_STATION:
1141 ieee80211_sta_restart(sdata);
1143 case NL80211_IFTYPE_ADHOC:
1144 ieee80211_ibss_restart(sdata);
1146 case NL80211_IFTYPE_MESH_POINT:
1147 ieee80211_mesh_restart(sdata);
1154 mod_timer(&local->sta_cleanup, jiffies + 1);
1156 mutex_lock(&local->sta_mtx);
1157 list_for_each_entry(sta, &local->sta_list, list)
1158 mesh_plink_restart(sta);
1159 mutex_unlock(&local->sta_mtx);
1166 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1168 struct ieee80211_sub_if_data *sdata;
1169 struct ieee80211_local *local;
1170 struct ieee80211_key *key;
1175 sdata = vif_to_sdata(vif);
1176 local = sdata->local;
1178 if (WARN_ON(!local->resuming))
1181 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1184 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1186 mutex_lock(&local->key_mtx);
1187 list_for_each_entry(key, &sdata->key_list, list)
1188 key->flags |= KEY_FLAG_TAINTED;
1189 mutex_unlock(&local->key_mtx);
1191 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1193 static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1194 enum ieee80211_smps_mode *smps_mode)
1196 if (ifmgd->associated) {
1197 *smps_mode = ifmgd->ap_smps;
1199 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1200 if (ifmgd->powersave)
1201 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1203 *smps_mode = IEEE80211_SMPS_OFF;
1212 /* must hold iflist_mtx */
1213 void ieee80211_recalc_smps(struct ieee80211_local *local)
1215 struct ieee80211_sub_if_data *sdata;
1216 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1219 lockdep_assert_held(&local->iflist_mtx);
1222 * This function could be improved to handle multiple
1223 * interfaces better, but right now it makes any
1224 * non-station interfaces force SM PS to be turned
1225 * off. If there are multiple station interfaces it
1226 * could also use the best possible mode, e.g. if
1227 * one is in static and the other in dynamic then
1231 list_for_each_entry(sdata, &local->interfaces, list) {
1232 if (!ieee80211_sdata_running(sdata))
1234 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1237 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1240 smps_mode = IEEE80211_SMPS_OFF;
1245 if (smps_mode == local->smps_mode)
1249 local->smps_mode = smps_mode;
1250 /* changed flag is auto-detected for this */
1251 ieee80211_hw_config(local, 0);
1254 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1258 for (i = 0; i < n_ids; i++)
1265 * ieee80211_ie_split - split an IE buffer according to ordering
1267 * @ies: the IE buffer
1268 * @ielen: the length of the IE buffer
1269 * @ids: an array with element IDs that are allowed before
1271 * @n_ids: the size of the element ID array
1272 * @offset: offset where to start splitting in the buffer
1274 * This function splits an IE buffer by updating the @offset
1275 * variable to point to the location where the buffer should be
1278 * It assumes that the given IE buffer is well-formed, this
1279 * has to be guaranteed by the caller!
1281 * It also assumes that the IEs in the buffer are ordered
1282 * correctly, if not the result of using this function will not
1283 * be ordered correctly either, i.e. it does no reordering.
1285 * The function returns the offset where the next part of the
1286 * buffer starts, which may be @ielen if the entire (remainder)
1287 * of the buffer should be used.
1289 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1290 const u8 *ids, int n_ids, size_t offset)
1292 size_t pos = offset;
1294 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1295 pos += 2 + ies[pos + 1];
1300 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1302 size_t pos = offset;
1304 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1305 pos += 2 + ies[pos + 1];
1310 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
1314 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
1316 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1320 * Scale up threshold values before storing it, as the RSSI averaging
1321 * algorithm uses a scaled up value as well. Change this scaling
1322 * factor if the RSSI averaging algorithm changes.
1324 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
1325 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
1328 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
1332 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1334 WARN_ON(rssi_min_thold == rssi_max_thold ||
1335 rssi_min_thold > rssi_max_thold);
1337 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
1340 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
1342 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
1344 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1346 _ieee80211_enable_rssi_reports(sdata, 0, 0);
1348 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
1350 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_supported_band *sband,
1355 *pos++ = WLAN_EID_HT_CAPABILITY;
1356 *pos++ = sizeof(struct ieee80211_ht_cap);
1357 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
1359 /* capability flags */
1360 tmp = cpu_to_le16(cap);
1361 memcpy(pos, &tmp, sizeof(u16));
1364 /* AMPDU parameters */
1365 *pos++ = sband->ht_cap.ampdu_factor |
1366 (sband->ht_cap.ampdu_density <<
1367 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
1370 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
1371 pos += sizeof(sband->ht_cap.mcs);
1373 /* extended capabilities */
1374 pos += sizeof(__le16);
1376 /* BF capabilities */
1377 pos += sizeof(__le32);
1379 /* antenna selection */
1385 u8 *ieee80211_ie_build_ht_info(u8 *pos,
1386 struct ieee80211_sta_ht_cap *ht_cap,
1387 struct ieee80211_channel *channel,
1388 enum nl80211_channel_type channel_type)
1390 struct ieee80211_ht_info *ht_info;
1391 /* Build HT Information */
1392 *pos++ = WLAN_EID_HT_INFORMATION;
1393 *pos++ = sizeof(struct ieee80211_ht_info);
1394 ht_info = (struct ieee80211_ht_info *)pos;
1395 ht_info->control_chan =
1396 ieee80211_frequency_to_channel(channel->center_freq);
1397 switch (channel_type) {
1398 case NL80211_CHAN_HT40MINUS:
1399 ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1401 case NL80211_CHAN_HT40PLUS:
1402 ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1404 case NL80211_CHAN_HT20:
1406 ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1409 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
1410 ht_info->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
1411 ht_info->operation_mode = 0x0000;
1412 ht_info->stbc_param = 0x0000;
1414 /* It seems that Basic MCS set and Supported MCS set
1415 are identical for the first 10 bytes */
1416 memset(&ht_info->basic_set, 0, 16);
1417 memcpy(&ht_info->basic_set, &ht_cap->mcs, 10);
1419 return pos + sizeof(struct ieee80211_ht_info);
1422 enum nl80211_channel_type
1423 ieee80211_ht_info_to_channel_type(struct ieee80211_ht_info *ht_info)
1425 enum nl80211_channel_type channel_type;
1428 return NL80211_CHAN_NO_HT;
1430 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
1431 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1432 channel_type = NL80211_CHAN_HT20;
1434 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1435 channel_type = NL80211_CHAN_HT40PLUS;
1437 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1438 channel_type = NL80211_CHAN_HT40MINUS;
1441 channel_type = NL80211_CHAN_NO_HT;
1444 return channel_type;
1447 int ieee80211_add_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
1449 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1450 struct ieee80211_local *local = sdata->local;
1451 struct ieee80211_supported_band *sband;
1455 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1456 rates = sband->n_bitrates;
1460 if (skb_tailroom(skb) < rates + 2)
1463 pos = skb_put(skb, rates + 2);
1464 *pos++ = WLAN_EID_SUPP_RATES;
1466 for (i = 0; i < rates; i++) {
1467 rate = sband->bitrates[i].bitrate;
1468 *pos++ = (u8) (rate / 5);
1474 int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
1476 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1477 struct ieee80211_local *local = sdata->local;
1478 struct ieee80211_supported_band *sband;
1480 u8 i, exrates, *pos;
1482 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1483 exrates = sband->n_bitrates;
1489 if (skb_tailroom(skb) < exrates + 2)
1493 pos = skb_put(skb, exrates + 2);
1494 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1496 for (i = 8; i < sband->n_bitrates; i++) {
1497 rate = sband->bitrates[i].bitrate;
1498 *pos++ = (u8) (rate / 5);