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 <linux/crc32.h>
23 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
24 #include <net/net_namespace.h>
26 #include <net/cfg80211.h>
27 #include <net/rtnetlink.h>
29 #include "ieee80211_i.h"
30 #include "driver-ops.h"
37 /* privid for wiphys to determine whether they belong to us or not */
38 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
40 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
42 struct ieee80211_local *local;
45 local = wiphy_priv(wiphy);
48 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
50 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
51 enum nl80211_iftype type)
53 __le16 fc = hdr->frame_control;
55 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
59 if (ieee80211_is_data(fc)) {
60 if (len < 24) /* drop incorrect hdr len (data) */
63 if (ieee80211_has_a4(fc))
65 if (ieee80211_has_tods(fc))
67 if (ieee80211_has_fromds(fc))
73 if (ieee80211_is_mgmt(fc)) {
74 if (len < 24) /* drop incorrect hdr len (mgmt) */
79 if (ieee80211_is_ctl(fc)) {
80 if(ieee80211_is_pspoll(fc))
83 if (ieee80211_is_back_req(fc)) {
85 case NL80211_IFTYPE_STATION:
87 case NL80211_IFTYPE_AP:
88 case NL80211_IFTYPE_AP_VLAN:
91 break; /* fall through to the return */
99 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
101 struct sk_buff *skb = tx->skb;
102 struct ieee80211_hdr *hdr;
105 hdr = (struct ieee80211_hdr *) skb->data;
106 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
107 } while ((skb = skb->next));
110 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
111 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.
124 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
128 * N_DBPS = DATARATE x 4
129 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
130 * (16 = SIGNAL time, 6 = tail bits)
131 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
134 * 802.11a - 17.5.2: aSIFSTime = 16 usec
135 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
136 * signal ext = 6 usec
138 dur = 16; /* SIFS + signal ext */
139 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
140 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
141 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
142 4 * rate); /* T_SYM x N_SYM */
145 * 802.11b or 802.11g with 802.11b compatibility:
146 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
147 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
149 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
150 * aSIFSTime = 10 usec
151 * aPreambleLength = 144 usec or 72 usec with short preamble
152 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
154 dur = 10; /* aSIFSTime = 10 usec */
155 dur += short_preamble ? (72 + 24) : (144 + 48);
157 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
163 /* Exported duration function for driver use */
164 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
165 struct ieee80211_vif *vif,
167 struct ieee80211_rate *rate)
169 struct ieee80211_local *local = hw_to_local(hw);
170 struct ieee80211_sub_if_data *sdata;
173 bool short_preamble = false;
177 sdata = vif_to_sdata(vif);
178 short_preamble = sdata->vif.bss_conf.use_short_preamble;
179 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
180 erp = rate->flags & IEEE80211_RATE_ERP_G;
183 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
186 return cpu_to_le16(dur);
188 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
190 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
191 struct ieee80211_vif *vif, size_t frame_len,
192 const struct ieee80211_tx_info *frame_txctl)
194 struct ieee80211_local *local = hw_to_local(hw);
195 struct ieee80211_rate *rate;
196 struct ieee80211_sub_if_data *sdata;
200 struct ieee80211_supported_band *sband;
202 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
204 short_preamble = false;
206 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
210 sdata = vif_to_sdata(vif);
211 short_preamble = sdata->vif.bss_conf.use_short_preamble;
212 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
213 erp = rate->flags & IEEE80211_RATE_ERP_G;
217 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
218 erp, short_preamble);
219 /* Data frame duration */
220 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
221 erp, short_preamble);
223 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
224 erp, short_preamble);
226 return cpu_to_le16(dur);
228 EXPORT_SYMBOL(ieee80211_rts_duration);
230 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
231 struct ieee80211_vif *vif,
233 const struct ieee80211_tx_info *frame_txctl)
235 struct ieee80211_local *local = hw_to_local(hw);
236 struct ieee80211_rate *rate;
237 struct ieee80211_sub_if_data *sdata;
241 struct ieee80211_supported_band *sband;
243 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
245 short_preamble = false;
247 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
250 sdata = vif_to_sdata(vif);
251 short_preamble = sdata->vif.bss_conf.use_short_preamble;
252 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
253 erp = rate->flags & IEEE80211_RATE_ERP_G;
256 /* Data frame duration */
257 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
258 erp, short_preamble);
259 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
261 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
262 erp, short_preamble);
265 return cpu_to_le16(dur);
267 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
269 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23))
270 static bool ieee80211_all_queues_started(struct ieee80211_hw *hw)
274 for (queue = 0; queue < hw->queues; queue++)
275 if (ieee80211_queue_stopped(hw, queue))
281 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
282 enum queue_stop_reason reason)
284 struct ieee80211_local *local = hw_to_local(hw);
285 struct ieee80211_sub_if_data *sdata;
287 if (WARN_ON(queue >= hw->queues))
290 __clear_bit(reason, &local->queue_stop_reasons[queue]);
292 if (local->queue_stop_reasons[queue] != 0)
293 /* someone still has this queue stopped */
296 if (!skb_queue_empty(&local->pending[queue]))
297 tasklet_schedule(&local->tx_pending_tasklet);
300 list_for_each_entry_rcu(sdata, &local->interfaces, list)
301 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
302 netif_tx_wake_queue(netdev_get_tx_queue(sdata->dev, queue));
303 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23))
304 netif_start_subqueue(sdata->dev, queue);
306 if (ieee80211_all_queues_started(hw))
307 netif_wake_queue(sdata->dev);
312 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
313 enum queue_stop_reason reason)
315 struct ieee80211_local *local = hw_to_local(hw);
318 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
319 __ieee80211_wake_queue(hw, queue, reason);
320 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
323 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
325 ieee80211_wake_queue_by_reason(hw, queue,
326 IEEE80211_QUEUE_STOP_REASON_DRIVER);
328 EXPORT_SYMBOL(ieee80211_wake_queue);
330 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
331 enum queue_stop_reason reason)
333 struct ieee80211_local *local = hw_to_local(hw);
334 struct ieee80211_sub_if_data *sdata;
336 if (WARN_ON(queue >= hw->queues))
339 __set_bit(reason, &local->queue_stop_reasons[queue]);
342 list_for_each_entry_rcu(sdata, &local->interfaces, list)
343 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
344 netif_tx_stop_queue(netdev_get_tx_queue(sdata->dev, queue));
345 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23))
346 netif_stop_subqueue(sdata->dev, queue);
348 netif_stop_queue(sdata->dev);
353 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
354 enum queue_stop_reason reason)
356 struct ieee80211_local *local = hw_to_local(hw);
359 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
360 __ieee80211_stop_queue(hw, queue, reason);
361 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
364 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
366 ieee80211_stop_queue_by_reason(hw, queue,
367 IEEE80211_QUEUE_STOP_REASON_DRIVER);
369 EXPORT_SYMBOL(ieee80211_stop_queue);
371 void ieee80211_add_pending_skb(struct ieee80211_local *local,
374 struct ieee80211_hw *hw = &local->hw;
376 int queue = skb_get_queue_mapping(skb);
377 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
379 if (WARN_ON(!info->control.vif)) {
384 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
385 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
386 __skb_queue_tail(&local->pending[queue], skb);
387 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
388 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
391 int ieee80211_add_pending_skbs(struct ieee80211_local *local,
392 struct sk_buff_head *skbs)
394 struct ieee80211_hw *hw = &local->hw;
397 int queue, ret = 0, i;
399 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
400 for (i = 0; i < hw->queues; i++)
401 __ieee80211_stop_queue(hw, i,
402 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
404 while ((skb = skb_dequeue(skbs))) {
405 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
407 if (WARN_ON(!info->control.vif)) {
413 queue = skb_get_queue_mapping(skb);
414 __skb_queue_tail(&local->pending[queue], skb);
417 for (i = 0; i < hw->queues; i++)
418 __ieee80211_wake_queue(hw, i,
419 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
420 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
425 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
426 enum queue_stop_reason reason)
428 struct ieee80211_local *local = hw_to_local(hw);
432 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
434 for (i = 0; i < hw->queues; i++)
435 __ieee80211_stop_queue(hw, i, reason);
437 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
440 void ieee80211_stop_queues(struct ieee80211_hw *hw)
442 ieee80211_stop_queues_by_reason(hw,
443 IEEE80211_QUEUE_STOP_REASON_DRIVER);
445 EXPORT_SYMBOL(ieee80211_stop_queues);
447 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
449 struct ieee80211_local *local = hw_to_local(hw);
453 if (WARN_ON(queue >= hw->queues))
456 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
457 ret = !!local->queue_stop_reasons[queue];
458 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
461 EXPORT_SYMBOL(ieee80211_queue_stopped);
463 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
464 enum queue_stop_reason reason)
466 struct ieee80211_local *local = hw_to_local(hw);
470 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
472 for (i = 0; i < hw->queues; i++)
473 __ieee80211_wake_queue(hw, i, reason);
475 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
478 void ieee80211_wake_queues(struct ieee80211_hw *hw)
480 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
482 EXPORT_SYMBOL(ieee80211_wake_queues);
484 void ieee80211_iterate_active_interfaces(
485 struct ieee80211_hw *hw,
486 void (*iterator)(void *data, u8 *mac,
487 struct ieee80211_vif *vif),
490 struct ieee80211_local *local = hw_to_local(hw);
491 struct ieee80211_sub_if_data *sdata;
493 mutex_lock(&local->iflist_mtx);
495 list_for_each_entry(sdata, &local->interfaces, list) {
496 switch (sdata->vif.type) {
497 case __NL80211_IFTYPE_AFTER_LAST:
498 case NL80211_IFTYPE_UNSPECIFIED:
499 case NL80211_IFTYPE_MONITOR:
500 case NL80211_IFTYPE_AP_VLAN:
502 case NL80211_IFTYPE_AP:
503 case NL80211_IFTYPE_STATION:
504 case NL80211_IFTYPE_ADHOC:
505 case NL80211_IFTYPE_WDS:
506 case NL80211_IFTYPE_MESH_POINT:
509 if (ieee80211_sdata_running(sdata))
510 iterator(data, sdata->vif.addr,
514 mutex_unlock(&local->iflist_mtx);
516 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
518 void ieee80211_iterate_active_interfaces_atomic(
519 struct ieee80211_hw *hw,
520 void (*iterator)(void *data, u8 *mac,
521 struct ieee80211_vif *vif),
524 struct ieee80211_local *local = hw_to_local(hw);
525 struct ieee80211_sub_if_data *sdata;
529 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
530 switch (sdata->vif.type) {
531 case __NL80211_IFTYPE_AFTER_LAST:
532 case NL80211_IFTYPE_UNSPECIFIED:
533 case NL80211_IFTYPE_MONITOR:
534 case NL80211_IFTYPE_AP_VLAN:
536 case NL80211_IFTYPE_AP:
537 case NL80211_IFTYPE_STATION:
538 case NL80211_IFTYPE_ADHOC:
539 case NL80211_IFTYPE_WDS:
540 case NL80211_IFTYPE_MESH_POINT:
543 if (ieee80211_sdata_running(sdata))
544 iterator(data, sdata->vif.addr,
550 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
553 * Nothing should have been stuffed into the workqueue during
554 * the suspend->resume cycle. If this WARN is seen then there
555 * is a bug with either the driver suspend or something in
556 * mac80211 stuffing into the workqueue which we haven't yet
557 * cleared during mac80211's suspend cycle.
559 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
561 if (WARN(local->suspended && !local->resuming,
562 "queueing ieee80211 work while going to suspend\n"))
568 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
570 struct ieee80211_local *local = hw_to_local(hw);
572 if (!ieee80211_can_queue_work(local))
575 queue_work(local->workqueue, work);
577 EXPORT_SYMBOL(ieee80211_queue_work);
579 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
580 struct delayed_work *dwork,
583 struct ieee80211_local *local = hw_to_local(hw);
585 if (!ieee80211_can_queue_work(local))
588 queue_delayed_work(local->workqueue, dwork, delay);
590 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
592 void ieee802_11_parse_elems(u8 *start, size_t len,
593 struct ieee802_11_elems *elems)
595 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
598 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
599 struct ieee802_11_elems *elems,
604 bool calc_crc = filter != 0;
606 memset(elems, 0, sizeof(*elems));
607 elems->ie_start = start;
608 elems->total_len = len;
620 if (calc_crc && id < 64 && (filter & (1ULL << id)))
621 crc = crc32_be(crc, pos - 2, elen + 2);
626 elems->ssid_len = elen;
628 case WLAN_EID_SUPP_RATES:
629 elems->supp_rates = pos;
630 elems->supp_rates_len = elen;
632 case WLAN_EID_FH_PARAMS:
633 elems->fh_params = pos;
634 elems->fh_params_len = elen;
636 case WLAN_EID_DS_PARAMS:
637 elems->ds_params = pos;
638 elems->ds_params_len = elen;
640 case WLAN_EID_CF_PARAMS:
641 elems->cf_params = pos;
642 elems->cf_params_len = elen;
645 if (elen >= sizeof(struct ieee80211_tim_ie)) {
646 elems->tim = (void *)pos;
647 elems->tim_len = elen;
650 case WLAN_EID_IBSS_PARAMS:
651 elems->ibss_params = pos;
652 elems->ibss_params_len = elen;
654 case WLAN_EID_CHALLENGE:
655 elems->challenge = pos;
656 elems->challenge_len = elen;
658 case WLAN_EID_VENDOR_SPECIFIC:
659 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
661 /* Microsoft OUI (00:50:F2) */
664 crc = crc32_be(crc, pos - 2, elen + 2);
667 /* OUI Type 1 - WPA IE */
669 elems->wpa_len = elen;
670 } else if (elen >= 5 && pos[3] == 2) {
671 /* OUI Type 2 - WMM IE */
673 elems->wmm_info = pos;
674 elems->wmm_info_len = elen;
675 } else if (pos[4] == 1) {
676 elems->wmm_param = pos;
677 elems->wmm_param_len = elen;
684 elems->rsn_len = elen;
686 case WLAN_EID_ERP_INFO:
687 elems->erp_info = pos;
688 elems->erp_info_len = elen;
690 case WLAN_EID_EXT_SUPP_RATES:
691 elems->ext_supp_rates = pos;
692 elems->ext_supp_rates_len = elen;
694 case WLAN_EID_HT_CAPABILITY:
695 if (elen >= sizeof(struct ieee80211_ht_cap))
696 elems->ht_cap_elem = (void *)pos;
698 case WLAN_EID_HT_INFORMATION:
699 if (elen >= sizeof(struct ieee80211_ht_info))
700 elems->ht_info_elem = (void *)pos;
702 case WLAN_EID_MESH_ID:
703 elems->mesh_id = pos;
704 elems->mesh_id_len = elen;
706 case WLAN_EID_MESH_CONFIG:
707 if (elen >= sizeof(struct ieee80211_meshconf_ie))
708 elems->mesh_config = (void *)pos;
710 case WLAN_EID_PEER_LINK:
711 elems->peer_link = pos;
712 elems->peer_link_len = elen;
716 elems->preq_len = elen;
720 elems->prep_len = elen;
724 elems->perr_len = elen;
727 if (elen >= sizeof(struct ieee80211_rann_ie))
728 elems->rann = (void *)pos;
730 case WLAN_EID_CHANNEL_SWITCH:
731 elems->ch_switch_elem = pos;
732 elems->ch_switch_elem_len = elen;
735 if (!elems->quiet_elem) {
736 elems->quiet_elem = pos;
737 elems->quiet_elem_len = elen;
739 elems->num_of_quiet_elem++;
741 case WLAN_EID_COUNTRY:
742 elems->country_elem = pos;
743 elems->country_elem_len = elen;
745 case WLAN_EID_PWR_CONSTRAINT:
746 elems->pwr_constr_elem = pos;
747 elems->pwr_constr_elem_len = elen;
749 case WLAN_EID_TIMEOUT_INTERVAL:
750 elems->timeout_int = pos;
751 elems->timeout_int_len = elen;
764 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
766 struct ieee80211_local *local = sdata->local;
767 struct ieee80211_tx_queue_params qparam;
772 if (!local->ops->conf_tx)
775 memset(&qparam, 0, sizeof(qparam));
777 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
778 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
780 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
781 /* Set defaults according to 802.11-2007 Table 7-37 */
790 qparam.cw_max = aCWmax;
791 qparam.cw_min = aCWmin;
795 default: /* never happens but let's not leave undefined */
797 qparam.cw_max = aCWmax;
798 qparam.cw_min = aCWmin;
803 qparam.cw_max = aCWmin;
804 qparam.cw_min = (aCWmin + 1) / 2 - 1;
806 qparam.txop = 6016/32;
808 qparam.txop = 3008/32;
812 qparam.cw_max = (aCWmin + 1) / 2 - 1;
813 qparam.cw_min = (aCWmin + 1) / 4 - 1;
815 qparam.txop = 3264/32;
817 qparam.txop = 1504/32;
822 qparam.uapsd = false;
824 drv_conf_tx(local, queue, &qparam);
828 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
829 const size_t supp_rates_len,
830 const u8 *supp_rates)
832 struct ieee80211_local *local = sdata->local;
833 int i, have_higher_than_11mbit = 0;
835 /* cf. IEEE 802.11 9.2.12 */
836 for (i = 0; i < supp_rates_len; i++)
837 if ((supp_rates[i] & 0x7f) * 5 > 110)
838 have_higher_than_11mbit = 1;
840 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
841 have_higher_than_11mbit)
842 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
844 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
846 ieee80211_set_wmm_default(sdata);
849 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
850 enum ieee80211_band band)
852 struct ieee80211_supported_band *sband;
853 struct ieee80211_rate *bitrates;
855 enum ieee80211_rate_flags mandatory_flag;
858 sband = local->hw.wiphy->bands[band];
861 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
864 if (band == IEEE80211_BAND_2GHZ)
865 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
867 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
869 bitrates = sband->bitrates;
871 for (i = 0; i < sband->n_bitrates; i++)
872 if (bitrates[i].flags & mandatory_flag)
873 mandatory_rates |= BIT(i);
874 return mandatory_rates;
877 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
878 u16 transaction, u16 auth_alg,
879 u8 *extra, size_t extra_len, const u8 *bssid,
880 const u8 *key, u8 key_len, u8 key_idx)
882 struct ieee80211_local *local = sdata->local;
884 struct ieee80211_mgmt *mgmt;
887 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
888 sizeof(*mgmt) + 6 + extra_len);
890 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
891 "frame\n", sdata->name);
894 skb_reserve(skb, local->hw.extra_tx_headroom);
896 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
897 memset(mgmt, 0, 24 + 6);
898 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
899 IEEE80211_STYPE_AUTH);
900 memcpy(mgmt->da, bssid, ETH_ALEN);
901 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
902 memcpy(mgmt->bssid, bssid, ETH_ALEN);
903 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
904 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
905 mgmt->u.auth.status_code = cpu_to_le16(0);
907 memcpy(skb_put(skb, extra_len), extra, extra_len);
909 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
910 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
911 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
915 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
916 ieee80211_tx_skb(sdata, skb);
919 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
920 const u8 *ie, size_t ie_len,
921 enum ieee80211_band band)
923 struct ieee80211_supported_band *sband;
925 size_t offset = 0, noffset;
926 int supp_rates_len, i;
928 sband = local->hw.wiphy->bands[band];
932 supp_rates_len = min_t(int, sband->n_bitrates, 8);
934 *pos++ = WLAN_EID_SUPP_RATES;
935 *pos++ = supp_rates_len;
937 for (i = 0; i < supp_rates_len; i++) {
938 int rate = sband->bitrates[i].bitrate;
939 *pos++ = (u8) (rate / 5);
942 /* insert "request information" if in custom IEs */
944 static const u8 before_extrates[] = {
949 noffset = ieee80211_ie_split(ie, ie_len,
951 ARRAY_SIZE(before_extrates),
953 memcpy(pos, ie + offset, noffset - offset);
954 pos += noffset - offset;
958 if (sband->n_bitrates > i) {
959 *pos++ = WLAN_EID_EXT_SUPP_RATES;
960 *pos++ = sband->n_bitrates - i;
962 for (; i < sband->n_bitrates; i++) {
963 int rate = sband->bitrates[i].bitrate;
964 *pos++ = (u8) (rate / 5);
968 /* insert custom IEs that go before HT */
970 static const u8 before_ht[] = {
974 WLAN_EID_EXT_SUPP_RATES,
976 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
978 noffset = ieee80211_ie_split(ie, ie_len,
979 before_ht, ARRAY_SIZE(before_ht),
981 memcpy(pos, ie + offset, noffset - offset);
982 pos += noffset - offset;
986 if (sband->ht_cap.ht_supported) {
987 u16 cap = sband->ht_cap.cap;
990 if (ieee80211_disable_40mhz_24ghz &&
991 sband->band == IEEE80211_BAND_2GHZ) {
992 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
993 cap &= ~IEEE80211_HT_CAP_SGI_40;
996 *pos++ = WLAN_EID_HT_CAPABILITY;
997 *pos++ = sizeof(struct ieee80211_ht_cap);
998 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
999 tmp = cpu_to_le16(cap);
1000 memcpy(pos, &tmp, sizeof(u16));
1002 *pos++ = sband->ht_cap.ampdu_factor |
1003 (sband->ht_cap.ampdu_density <<
1004 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
1005 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
1006 pos += sizeof(sband->ht_cap.mcs);
1007 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
1011 * If adding more here, adjust code in main.c
1012 * that calculates local->scan_ies_len.
1015 /* add any remaining custom IEs */
1018 memcpy(pos, ie + offset, noffset - offset);
1019 pos += noffset - offset;
1022 return pos - buffer;
1025 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1026 const u8 *ssid, size_t ssid_len,
1027 const u8 *ie, size_t ie_len)
1029 struct ieee80211_local *local = sdata->local;
1030 struct sk_buff *skb;
1031 struct ieee80211_mgmt *mgmt;
1035 /* FIXME: come up with a proper value */
1036 buf = kmalloc(200 + ie_len, GFP_KERNEL);
1038 printk(KERN_DEBUG "%s: failed to allocate temporary IE "
1039 "buffer\n", sdata->name);
1043 buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
1044 local->hw.conf.channel->band);
1046 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
1051 mgmt = (struct ieee80211_mgmt *) skb->data;
1052 memcpy(mgmt->da, dst, ETH_ALEN);
1053 memcpy(mgmt->bssid, dst, ETH_ALEN);
1056 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1057 ieee80211_tx_skb(sdata, skb);
1061 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
1062 struct ieee802_11_elems *elems,
1063 enum ieee80211_band band)
1065 struct ieee80211_supported_band *sband;
1066 struct ieee80211_rate *bitrates;
1070 sband = local->hw.wiphy->bands[band];
1074 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1077 bitrates = sband->bitrates;
1078 num_rates = sband->n_bitrates;
1080 for (i = 0; i < elems->supp_rates_len +
1081 elems->ext_supp_rates_len; i++) {
1084 if (i < elems->supp_rates_len)
1085 rate = elems->supp_rates[i];
1086 else if (elems->ext_supp_rates)
1087 rate = elems->ext_supp_rates
1088 [i - elems->supp_rates_len];
1089 own_rate = 5 * (rate & 0x7f);
1090 for (j = 0; j < num_rates; j++)
1091 if (bitrates[j].bitrate == own_rate)
1092 supp_rates |= BIT(j);
1097 void ieee80211_stop_device(struct ieee80211_local *local)
1099 ieee80211_led_radio(local, false);
1101 cancel_work_sync(&local->reconfig_filter);
1103 flush_workqueue(local->workqueue);
1107 int ieee80211_reconfig(struct ieee80211_local *local)
1109 struct ieee80211_hw *hw = &local->hw;
1110 struct ieee80211_sub_if_data *sdata;
1111 struct sta_info *sta;
1114 if (local->suspended)
1115 local->resuming = true;
1117 /* restart hardware */
1118 if (local->open_count) {
1120 * Upon resume hardware can sometimes be goofy due to
1121 * various platform / driver / bus issues, so restarting
1122 * the device may at times not work immediately. Propagate
1125 res = drv_start(local);
1127 WARN(local->suspended, "Harware became unavailable "
1128 "upon resume. This is could be a software issue"
1129 "prior to suspend or a hardware issue\n");
1133 ieee80211_led_radio(local, true);
1136 /* add interfaces */
1137 list_for_each_entry(sdata, &local->interfaces, list) {
1138 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1139 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1140 ieee80211_sdata_running(sdata))
1141 res = drv_add_interface(local, &sdata->vif);
1145 mutex_lock(&local->sta_mtx);
1146 list_for_each_entry(sta, &local->sta_list, list) {
1147 if (sta->uploaded) {
1149 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1150 sdata = container_of(sdata->bss,
1151 struct ieee80211_sub_if_data,
1154 WARN_ON(drv_sta_add(local, sdata, &sta->sta));
1157 mutex_unlock(&local->sta_mtx);
1159 /* Clear Suspend state so that ADDBA requests can be processed */
1163 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1164 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1165 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1171 /* setup RTS threshold */
1172 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1174 /* reconfigure hardware */
1175 ieee80211_hw_config(local, ~0);
1177 ieee80211_configure_filter(local);
1179 /* Finally also reconfigure all the BSS information */
1180 list_for_each_entry(sdata, &local->interfaces, list) {
1182 if (!ieee80211_sdata_running(sdata))
1184 switch (sdata->vif.type) {
1185 case NL80211_IFTYPE_STATION:
1186 /* disable beacon change bits */
1187 changed &= ~(BSS_CHANGED_BEACON |
1188 BSS_CHANGED_BEACON_ENABLED);
1190 case NL80211_IFTYPE_ADHOC:
1191 case NL80211_IFTYPE_AP:
1192 case NL80211_IFTYPE_MESH_POINT:
1193 ieee80211_bss_info_change_notify(sdata, changed);
1195 case NL80211_IFTYPE_WDS:
1197 case NL80211_IFTYPE_AP_VLAN:
1198 case NL80211_IFTYPE_MONITOR:
1199 /* ignore virtual */
1201 case NL80211_IFTYPE_UNSPECIFIED:
1202 case __NL80211_IFTYPE_AFTER_LAST:
1209 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1210 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1211 ieee80211_sta_tear_down_BA_sessions(sta);
1217 list_for_each_entry(sdata, &local->interfaces, list)
1218 if (ieee80211_sdata_running(sdata))
1219 ieee80211_enable_keys(sdata);
1221 ieee80211_wake_queues_by_reason(hw,
1222 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1225 * If this is for hw restart things are still running.
1226 * We may want to change that later, however.
1228 if (!local->suspended)
1232 /* first set suspended false, then resuming */
1233 local->suspended = false;
1235 local->resuming = false;
1237 list_for_each_entry(sdata, &local->interfaces, list) {
1238 switch(sdata->vif.type) {
1239 case NL80211_IFTYPE_STATION:
1240 ieee80211_sta_restart(sdata);
1242 case NL80211_IFTYPE_ADHOC:
1243 ieee80211_ibss_restart(sdata);
1245 case NL80211_IFTYPE_MESH_POINT:
1246 ieee80211_mesh_restart(sdata);
1253 add_timer(&local->sta_cleanup);
1255 mutex_lock(&local->sta_mtx);
1256 list_for_each_entry(sta, &local->sta_list, list)
1257 mesh_plink_restart(sta);
1258 mutex_unlock(&local->sta_mtx);
1265 static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1266 enum ieee80211_smps_mode *smps_mode)
1268 if (ifmgd->associated) {
1269 *smps_mode = ifmgd->ap_smps;
1271 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1272 if (ifmgd->powersave)
1273 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1275 *smps_mode = IEEE80211_SMPS_OFF;
1284 /* must hold iflist_mtx */
1285 void ieee80211_recalc_smps(struct ieee80211_local *local,
1286 struct ieee80211_sub_if_data *forsdata)
1288 struct ieee80211_sub_if_data *sdata;
1289 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1293 WARN_ON(!mutex_is_locked(&forsdata->u.mgd.mtx));
1295 WARN_ON(!mutex_is_locked(&local->iflist_mtx));
1298 * This function could be improved to handle multiple
1299 * interfaces better, but right now it makes any
1300 * non-station interfaces force SM PS to be turned
1301 * off. If there are multiple station interfaces it
1302 * could also use the best possible mode, e.g. if
1303 * one is in static and the other in dynamic then
1307 list_for_each_entry(sdata, &local->interfaces, list) {
1308 if (!netif_running(sdata->dev))
1310 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1312 if (sdata != forsdata) {
1314 * This nested is ok -- we are holding the iflist_mtx
1315 * so can't get here twice or so. But it's required
1316 * since normally we acquire it first and then the
1319 mutex_lock_nested(&sdata->u.mgd.mtx, SINGLE_DEPTH_NESTING);
1320 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1321 mutex_unlock(&sdata->u.mgd.mtx);
1323 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1326 smps_mode = IEEE80211_SMPS_OFF;
1331 if (smps_mode == local->smps_mode)
1335 local->smps_mode = smps_mode;
1336 /* changed flag is auto-detected for this */
1337 ieee80211_hw_config(local, 0);
1340 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1344 for (i = 0; i < n_ids; i++)
1351 * ieee80211_ie_split - split an IE buffer according to ordering
1353 * @ies: the IE buffer
1354 * @ielen: the length of the IE buffer
1355 * @ids: an array with element IDs that are allowed before
1357 * @n_ids: the size of the element ID array
1358 * @offset: offset where to start splitting in the buffer
1360 * This function splits an IE buffer by updating the @offset
1361 * variable to point to the location where the buffer should be
1364 * It assumes that the given IE buffer is well-formed, this
1365 * has to be guaranteed by the caller!
1367 * It also assumes that the IEs in the buffer are ordered
1368 * correctly, if not the result of using this function will not
1369 * be ordered correctly either, i.e. it does no reordering.
1371 * The function returns the offset where the next part of the
1372 * buffer starts, which may be @ielen if the entire (remainder)
1373 * of the buffer should be used.
1375 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1376 const u8 *ids, int n_ids, size_t offset)
1378 size_t pos = offset;
1380 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1381 pos += 2 + ies[pos + 1];
1386 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1388 size_t pos = offset;
1390 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1391 pos += 2 + ies[pos + 1];