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/wireless.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 void *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)
100 struct sk_buff *skb = tx->skb;
101 struct ieee80211_hdr *hdr;
104 hdr = (struct ieee80211_hdr *) skb->data;
105 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
106 } while ((skb = skb->next));
109 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
110 int rate, int erp, int short_preamble)
114 /* calculate duration (in microseconds, rounded up to next higher
115 * integer if it includes a fractional microsecond) to send frame of
116 * len bytes (does not include FCS) at the given rate. Duration will
119 * rate is in 100 kbps, so divident is multiplied by 10 in the
120 * DIV_ROUND_UP() operations.
123 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
127 * N_DBPS = DATARATE x 4
128 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
129 * (16 = SIGNAL time, 6 = tail bits)
130 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
133 * 802.11a - 17.5.2: aSIFSTime = 16 usec
134 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
135 * signal ext = 6 usec
137 dur = 16; /* SIFS + signal ext */
138 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
139 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
140 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
141 4 * rate); /* T_SYM x N_SYM */
144 * 802.11b or 802.11g with 802.11b compatibility:
145 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
146 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
148 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
149 * aSIFSTime = 10 usec
150 * aPreambleLength = 144 usec or 72 usec with short preamble
151 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
153 dur = 10; /* aSIFSTime = 10 usec */
154 dur += short_preamble ? (72 + 24) : (144 + 48);
156 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
162 /* Exported duration function for driver use */
163 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
164 struct ieee80211_vif *vif,
166 struct ieee80211_rate *rate)
168 struct ieee80211_local *local = hw_to_local(hw);
169 struct ieee80211_sub_if_data *sdata;
172 bool short_preamble = false;
176 sdata = vif_to_sdata(vif);
177 short_preamble = sdata->vif.bss_conf.use_short_preamble;
178 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
179 erp = rate->flags & IEEE80211_RATE_ERP_G;
182 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
185 return cpu_to_le16(dur);
187 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
189 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
190 struct ieee80211_vif *vif, size_t frame_len,
191 const struct ieee80211_tx_info *frame_txctl)
193 struct ieee80211_local *local = hw_to_local(hw);
194 struct ieee80211_rate *rate;
195 struct ieee80211_sub_if_data *sdata;
199 struct ieee80211_supported_band *sband;
201 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
203 short_preamble = false;
205 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
209 sdata = vif_to_sdata(vif);
210 short_preamble = sdata->vif.bss_conf.use_short_preamble;
211 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
212 erp = rate->flags & IEEE80211_RATE_ERP_G;
216 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
217 erp, short_preamble);
218 /* Data frame duration */
219 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
220 erp, short_preamble);
222 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
223 erp, short_preamble);
225 return cpu_to_le16(dur);
227 EXPORT_SYMBOL(ieee80211_rts_duration);
229 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
230 struct ieee80211_vif *vif,
232 const struct ieee80211_tx_info *frame_txctl)
234 struct ieee80211_local *local = hw_to_local(hw);
235 struct ieee80211_rate *rate;
236 struct ieee80211_sub_if_data *sdata;
240 struct ieee80211_supported_band *sband;
242 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
244 short_preamble = false;
246 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
249 sdata = vif_to_sdata(vif);
250 short_preamble = sdata->vif.bss_conf.use_short_preamble;
251 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
252 erp = rate->flags & IEEE80211_RATE_ERP_G;
255 /* Data frame duration */
256 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
257 erp, short_preamble);
258 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
260 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
261 erp, short_preamble);
264 return cpu_to_le16(dur);
266 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
268 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
269 enum queue_stop_reason reason)
271 struct ieee80211_local *local = hw_to_local(hw);
273 if (WARN_ON(queue >= hw->queues))
276 __clear_bit(reason, &local->queue_stop_reasons[queue]);
278 if (!skb_queue_empty(&local->pending[queue]) &&
279 local->queue_stop_reasons[queue] ==
280 BIT(IEEE80211_QUEUE_STOP_REASON_PENDING))
281 tasklet_schedule(&local->tx_pending_tasklet);
283 if (local->queue_stop_reasons[queue] != 0)
284 /* someone still has this queue stopped */
287 netif_wake_subqueue(local->mdev, queue);
290 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
291 enum queue_stop_reason reason)
293 struct ieee80211_local *local = hw_to_local(hw);
296 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
297 __ieee80211_wake_queue(hw, queue, reason);
298 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
301 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
303 ieee80211_wake_queue_by_reason(hw, queue,
304 IEEE80211_QUEUE_STOP_REASON_DRIVER);
306 EXPORT_SYMBOL(ieee80211_wake_queue);
308 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
309 enum queue_stop_reason reason)
311 struct ieee80211_local *local = hw_to_local(hw);
313 if (WARN_ON(queue >= hw->queues))
317 * Only stop if it was previously running, this is necessary
318 * for correct pending packets handling because there we may
319 * start (but not wake) the queue and rely on that.
321 if (!local->queue_stop_reasons[queue])
322 netif_stop_subqueue(local->mdev, queue);
324 __set_bit(reason, &local->queue_stop_reasons[queue]);
327 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
328 enum queue_stop_reason reason)
330 struct ieee80211_local *local = hw_to_local(hw);
333 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
334 __ieee80211_stop_queue(hw, queue, reason);
335 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
338 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
340 ieee80211_stop_queue_by_reason(hw, queue,
341 IEEE80211_QUEUE_STOP_REASON_DRIVER);
343 EXPORT_SYMBOL(ieee80211_stop_queue);
345 void ieee80211_add_pending_skb(struct ieee80211_local *local,
348 struct ieee80211_hw *hw = &local->hw;
350 int queue = skb_get_queue_mapping(skb);
352 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
353 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
354 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_PENDING);
355 skb_queue_tail(&local->pending[queue], skb);
356 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
357 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
360 int ieee80211_add_pending_skbs(struct ieee80211_local *local,
361 struct sk_buff_head *skbs)
363 struct ieee80211_hw *hw = &local->hw;
366 int queue, ret = 0, i;
368 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
369 for (i = 0; i < hw->queues; i++)
370 __ieee80211_stop_queue(hw, i,
371 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
373 while ((skb = skb_dequeue(skbs))) {
375 queue = skb_get_queue_mapping(skb);
376 skb_queue_tail(&local->pending[queue], skb);
379 for (i = 0; i < hw->queues; i++) {
381 __ieee80211_stop_queue(hw, i,
382 IEEE80211_QUEUE_STOP_REASON_PENDING);
383 __ieee80211_wake_queue(hw, i,
384 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
386 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
391 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
392 enum queue_stop_reason reason)
394 struct ieee80211_local *local = hw_to_local(hw);
398 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
400 for (i = 0; i < hw->queues; i++)
401 __ieee80211_stop_queue(hw, i, reason);
403 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
406 void ieee80211_stop_queues(struct ieee80211_hw *hw)
408 ieee80211_stop_queues_by_reason(hw,
409 IEEE80211_QUEUE_STOP_REASON_DRIVER);
411 EXPORT_SYMBOL(ieee80211_stop_queues);
413 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
415 struct ieee80211_local *local = hw_to_local(hw);
417 if (WARN_ON(queue >= hw->queues))
420 return __netif_subqueue_stopped(local->mdev, queue);
422 EXPORT_SYMBOL(ieee80211_queue_stopped);
424 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
425 enum queue_stop_reason reason)
427 struct ieee80211_local *local = hw_to_local(hw);
431 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
433 for (i = 0; i < hw->queues; i++)
434 __ieee80211_wake_queue(hw, i, reason);
436 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
439 void ieee80211_wake_queues(struct ieee80211_hw *hw)
441 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
443 EXPORT_SYMBOL(ieee80211_wake_queues);
445 void ieee80211_iterate_active_interfaces(
446 struct ieee80211_hw *hw,
447 void (*iterator)(void *data, u8 *mac,
448 struct ieee80211_vif *vif),
451 struct ieee80211_local *local = hw_to_local(hw);
452 struct ieee80211_sub_if_data *sdata;
454 mutex_lock(&local->iflist_mtx);
456 list_for_each_entry(sdata, &local->interfaces, list) {
457 switch (sdata->vif.type) {
458 case __NL80211_IFTYPE_AFTER_LAST:
459 case NL80211_IFTYPE_UNSPECIFIED:
460 case NL80211_IFTYPE_MONITOR:
461 case NL80211_IFTYPE_AP_VLAN:
463 case NL80211_IFTYPE_AP:
464 case NL80211_IFTYPE_STATION:
465 case NL80211_IFTYPE_ADHOC:
466 case NL80211_IFTYPE_WDS:
467 case NL80211_IFTYPE_MESH_POINT:
470 if (netif_running(sdata->dev))
471 iterator(data, sdata->dev->dev_addr,
475 mutex_unlock(&local->iflist_mtx);
477 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
479 void ieee80211_iterate_active_interfaces_atomic(
480 struct ieee80211_hw *hw,
481 void (*iterator)(void *data, u8 *mac,
482 struct ieee80211_vif *vif),
485 struct ieee80211_local *local = hw_to_local(hw);
486 struct ieee80211_sub_if_data *sdata;
490 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
491 switch (sdata->vif.type) {
492 case __NL80211_IFTYPE_AFTER_LAST:
493 case NL80211_IFTYPE_UNSPECIFIED:
494 case NL80211_IFTYPE_MONITOR:
495 case NL80211_IFTYPE_AP_VLAN:
497 case NL80211_IFTYPE_AP:
498 case NL80211_IFTYPE_STATION:
499 case NL80211_IFTYPE_ADHOC:
500 case NL80211_IFTYPE_WDS:
501 case NL80211_IFTYPE_MESH_POINT:
504 if (netif_running(sdata->dev))
505 iterator(data, sdata->dev->dev_addr,
511 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
513 void ieee802_11_parse_elems(u8 *start, size_t len,
514 struct ieee802_11_elems *elems)
516 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
519 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
520 struct ieee802_11_elems *elems,
525 bool calc_crc = filter != 0;
527 memset(elems, 0, sizeof(*elems));
528 elems->ie_start = start;
529 elems->total_len = len;
541 if (calc_crc && id < 64 && (filter & BIT(id)))
542 crc = crc32_be(crc, pos - 2, elen + 2);
547 elems->ssid_len = elen;
549 case WLAN_EID_SUPP_RATES:
550 elems->supp_rates = pos;
551 elems->supp_rates_len = elen;
553 case WLAN_EID_FH_PARAMS:
554 elems->fh_params = pos;
555 elems->fh_params_len = elen;
557 case WLAN_EID_DS_PARAMS:
558 elems->ds_params = pos;
559 elems->ds_params_len = elen;
561 case WLAN_EID_CF_PARAMS:
562 elems->cf_params = pos;
563 elems->cf_params_len = elen;
566 if (elen >= sizeof(struct ieee80211_tim_ie)) {
567 elems->tim = (void *)pos;
568 elems->tim_len = elen;
571 case WLAN_EID_IBSS_PARAMS:
572 elems->ibss_params = pos;
573 elems->ibss_params_len = elen;
575 case WLAN_EID_CHALLENGE:
576 elems->challenge = pos;
577 elems->challenge_len = elen;
579 case WLAN_EID_VENDOR_SPECIFIC:
580 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
582 /* Microsoft OUI (00:50:F2) */
585 crc = crc32_be(crc, pos - 2, elen + 2);
588 /* OUI Type 1 - WPA IE */
590 elems->wpa_len = elen;
591 } else if (elen >= 5 && pos[3] == 2) {
592 /* OUI Type 2 - WMM IE */
594 elems->wmm_info = pos;
595 elems->wmm_info_len = elen;
596 } else if (pos[4] == 1) {
597 elems->wmm_param = pos;
598 elems->wmm_param_len = elen;
605 elems->rsn_len = elen;
607 case WLAN_EID_ERP_INFO:
608 elems->erp_info = pos;
609 elems->erp_info_len = elen;
611 case WLAN_EID_EXT_SUPP_RATES:
612 elems->ext_supp_rates = pos;
613 elems->ext_supp_rates_len = elen;
615 case WLAN_EID_HT_CAPABILITY:
616 if (elen >= sizeof(struct ieee80211_ht_cap))
617 elems->ht_cap_elem = (void *)pos;
619 case WLAN_EID_HT_INFORMATION:
620 if (elen >= sizeof(struct ieee80211_ht_info))
621 elems->ht_info_elem = (void *)pos;
623 case WLAN_EID_MESH_ID:
624 elems->mesh_id = pos;
625 elems->mesh_id_len = elen;
627 case WLAN_EID_MESH_CONFIG:
628 elems->mesh_config = pos;
629 elems->mesh_config_len = elen;
631 case WLAN_EID_PEER_LINK:
632 elems->peer_link = pos;
633 elems->peer_link_len = elen;
637 elems->preq_len = elen;
641 elems->prep_len = elen;
645 elems->perr_len = elen;
647 case WLAN_EID_CHANNEL_SWITCH:
648 elems->ch_switch_elem = pos;
649 elems->ch_switch_elem_len = elen;
652 if (!elems->quiet_elem) {
653 elems->quiet_elem = pos;
654 elems->quiet_elem_len = elen;
656 elems->num_of_quiet_elem++;
658 case WLAN_EID_COUNTRY:
659 elems->country_elem = pos;
660 elems->country_elem_len = elen;
662 case WLAN_EID_PWR_CONSTRAINT:
663 elems->pwr_constr_elem = pos;
664 elems->pwr_constr_elem_len = elen;
666 case WLAN_EID_TIMEOUT_INTERVAL:
667 elems->timeout_int = pos;
668 elems->timeout_int_len = elen;
681 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
683 struct ieee80211_local *local = sdata->local;
684 struct ieee80211_tx_queue_params qparam;
689 if (!local->ops->conf_tx)
692 memset(&qparam, 0, sizeof(qparam));
694 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
695 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
697 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
698 /* Set defaults according to 802.11-2007 Table 7-37 */
707 qparam.cw_max = aCWmax;
708 qparam.cw_min = aCWmin;
712 default: /* never happens but let's not leave undefined */
714 qparam.cw_max = aCWmax;
715 qparam.cw_min = aCWmin;
720 qparam.cw_max = aCWmin;
721 qparam.cw_min = (aCWmin + 1) / 2 - 1;
723 qparam.txop = 6016/32;
725 qparam.txop = 3008/32;
729 qparam.cw_max = (aCWmin + 1) / 2 - 1;
730 qparam.cw_min = (aCWmin + 1) / 4 - 1;
732 qparam.txop = 3264/32;
734 qparam.txop = 1504/32;
739 drv_conf_tx(local, queue, &qparam);
743 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
744 const size_t supp_rates_len,
745 const u8 *supp_rates)
747 struct ieee80211_local *local = sdata->local;
748 int i, have_higher_than_11mbit = 0;
750 /* cf. IEEE 802.11 9.2.12 */
751 for (i = 0; i < supp_rates_len; i++)
752 if ((supp_rates[i] & 0x7f) * 5 > 110)
753 have_higher_than_11mbit = 1;
755 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
756 have_higher_than_11mbit)
757 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
759 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
761 ieee80211_set_wmm_default(sdata);
764 void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
767 skb->dev = sdata->local->mdev;
768 skb_set_mac_header(skb, 0);
769 skb_set_network_header(skb, 0);
770 skb_set_transport_header(skb, 0);
772 skb->iif = sdata->dev->ifindex;
773 skb->do_not_encrypt = !encrypt;
778 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
779 enum ieee80211_band band)
781 struct ieee80211_supported_band *sband;
782 struct ieee80211_rate *bitrates;
784 enum ieee80211_rate_flags mandatory_flag;
787 sband = local->hw.wiphy->bands[band];
790 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
793 if (band == IEEE80211_BAND_2GHZ)
794 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
796 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
798 bitrates = sband->bitrates;
800 for (i = 0; i < sband->n_bitrates; i++)
801 if (bitrates[i].flags & mandatory_flag)
802 mandatory_rates |= BIT(i);
803 return mandatory_rates;
806 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
807 u16 transaction, u16 auth_alg,
808 u8 *extra, size_t extra_len, const u8 *bssid,
809 const u8 *key, u8 key_len, u8 key_idx)
811 struct ieee80211_local *local = sdata->local;
813 struct ieee80211_mgmt *mgmt;
816 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
817 sizeof(*mgmt) + 6 + extra_len);
819 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
820 "frame\n", sdata->dev->name);
823 skb_reserve(skb, local->hw.extra_tx_headroom);
825 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
826 memset(mgmt, 0, 24 + 6);
827 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
828 IEEE80211_STYPE_AUTH);
829 memcpy(mgmt->da, bssid, ETH_ALEN);
830 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
831 memcpy(mgmt->bssid, bssid, ETH_ALEN);
832 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
833 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
834 mgmt->u.auth.status_code = cpu_to_le16(0);
836 memcpy(skb_put(skb, extra_len), extra, extra_len);
838 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
839 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
840 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
844 ieee80211_tx_skb(sdata, skb, 0);
847 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
848 const u8 *ie, size_t ie_len)
850 struct ieee80211_supported_band *sband;
851 u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
854 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
858 *pos++ = WLAN_EID_SUPP_RATES;
859 supp_rates_len = pos;
862 for (i = 0; i < sband->n_bitrates; i++) {
863 struct ieee80211_rate *rate = &sband->bitrates[i];
865 if (esupp_rates_len) {
866 *esupp_rates_len += 1;
867 } else if (*supp_rates_len == 8) {
868 *pos++ = WLAN_EID_EXT_SUPP_RATES;
869 esupp_rates_len = pos;
872 *supp_rates_len += 1;
874 *pos++ = rate->bitrate / 5;
877 if (sband->ht_cap.ht_supported) {
878 __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
880 *pos++ = WLAN_EID_HT_CAPABILITY;
881 *pos++ = sizeof(struct ieee80211_ht_cap);
882 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
883 memcpy(pos, &tmp, sizeof(u16));
885 /* TODO: needs a define here for << 2 */
886 *pos++ = sband->ht_cap.ampdu_factor |
887 (sband->ht_cap.ampdu_density << 2);
888 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
889 pos += sizeof(sband->ht_cap.mcs);
890 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
894 * If adding more here, adjust code in main.c
895 * that calculates local->scan_ies_len.
899 memcpy(pos, ie, ie_len);
906 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
907 const u8 *ssid, size_t ssid_len,
908 const u8 *ie, size_t ie_len)
910 struct ieee80211_local *local = sdata->local;
912 struct ieee80211_mgmt *mgmt;
915 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
918 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
919 "request\n", sdata->dev->name);
922 skb_reserve(skb, local->hw.extra_tx_headroom);
924 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
926 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
927 IEEE80211_STYPE_PROBE_REQ);
928 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
930 memcpy(mgmt->da, dst, ETH_ALEN);
931 memcpy(mgmt->bssid, dst, ETH_ALEN);
933 memset(mgmt->da, 0xff, ETH_ALEN);
934 memset(mgmt->bssid, 0xff, ETH_ALEN);
936 pos = skb_put(skb, 2 + ssid_len);
937 *pos++ = WLAN_EID_SSID;
939 memcpy(pos, ssid, ssid_len);
942 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
944 ieee80211_tx_skb(sdata, skb, 0);
947 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
948 struct ieee802_11_elems *elems,
949 enum ieee80211_band band)
951 struct ieee80211_supported_band *sband;
952 struct ieee80211_rate *bitrates;
956 sband = local->hw.wiphy->bands[band];
960 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
963 bitrates = sband->bitrates;
964 num_rates = sband->n_bitrates;
966 for (i = 0; i < elems->supp_rates_len +
967 elems->ext_supp_rates_len; i++) {
970 if (i < elems->supp_rates_len)
971 rate = elems->supp_rates[i];
972 else if (elems->ext_supp_rates)
973 rate = elems->ext_supp_rates
974 [i - elems->supp_rates_len];
975 own_rate = 5 * (rate & 0x7f);
976 for (j = 0; j < num_rates; j++)
977 if (bitrates[j].bitrate == own_rate)
978 supp_rates |= BIT(j);
983 int ieee80211_reconfig(struct ieee80211_local *local)
985 struct ieee80211_hw *hw = &local->hw;
986 struct ieee80211_sub_if_data *sdata;
987 struct ieee80211_if_init_conf conf;
988 struct sta_info *sta;
991 bool from_suspend = local->suspended;
994 * We're going to start the hardware, at that point
995 * we are no longer suspended and can RX frames.
997 local->suspended = false;
999 /* restart hardware */
1000 if (local->open_count) {
1001 res = drv_start(local);
1003 ieee80211_led_radio(local, true);
1006 /* add interfaces */
1007 list_for_each_entry(sdata, &local->interfaces, list) {
1008 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1009 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1010 netif_running(sdata->dev)) {
1011 conf.vif = &sdata->vif;
1012 conf.type = sdata->vif.type;
1013 conf.mac_addr = sdata->dev->dev_addr;
1014 res = drv_add_interface(local, &conf);
1019 if (local->ops->sta_notify) {
1020 spin_lock_irqsave(&local->sta_lock, flags);
1021 list_for_each_entry(sta, &local->sta_list, list) {
1023 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1024 sdata = container_of(sdata->bss,
1025 struct ieee80211_sub_if_data,
1028 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
1031 spin_unlock_irqrestore(&local->sta_lock, flags);
1034 /* Clear Suspend state so that ADDBA requests can be processed */
1038 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1039 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1040 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1046 /* setup RTS threshold */
1047 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1049 /* reconfigure hardware */
1050 ieee80211_hw_config(local, ~0);
1052 netif_addr_lock_bh(local->mdev);
1053 ieee80211_configure_filter(local);
1054 netif_addr_unlock_bh(local->mdev);
1056 /* Finally also reconfigure all the BSS information */
1057 list_for_each_entry(sdata, &local->interfaces, list) {
1059 if (!netif_running(sdata->dev))
1061 switch (sdata->vif.type) {
1062 case NL80211_IFTYPE_STATION:
1063 /* disable beacon change bits */
1064 changed &= ~(BSS_CHANGED_BEACON |
1065 BSS_CHANGED_BEACON_ENABLED);
1067 case NL80211_IFTYPE_ADHOC:
1068 case NL80211_IFTYPE_AP:
1069 case NL80211_IFTYPE_MESH_POINT:
1070 ieee80211_bss_info_change_notify(sdata, changed);
1072 case NL80211_IFTYPE_WDS:
1074 case NL80211_IFTYPE_AP_VLAN:
1075 case NL80211_IFTYPE_MONITOR:
1076 /* ignore virtual */
1078 case NL80211_IFTYPE_UNSPECIFIED:
1079 case __NL80211_IFTYPE_AFTER_LAST:
1086 list_for_each_entry(sdata, &local->interfaces, list)
1087 if (netif_running(sdata->dev))
1088 ieee80211_enable_keys(sdata);
1090 ieee80211_wake_queues_by_reason(hw,
1091 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1094 * If this is for hw restart things are still running.
1095 * We may want to change that later, however.
1101 local->suspended = false;
1103 list_for_each_entry(sdata, &local->interfaces, list) {
1104 switch(sdata->vif.type) {
1105 case NL80211_IFTYPE_STATION:
1106 ieee80211_sta_restart(sdata);
1108 case NL80211_IFTYPE_ADHOC:
1109 ieee80211_ibss_restart(sdata);
1111 case NL80211_IFTYPE_MESH_POINT:
1112 ieee80211_mesh_restart(sdata);
1119 add_timer(&local->sta_cleanup);
1121 spin_lock_irqsave(&local->sta_lock, flags);
1122 list_for_each_entry(sta, &local->sta_list, list)
1123 mesh_plink_restart(sta);
1124 spin_unlock_irqrestore(&local->sta_lock, flags);