net/9p: Fix the msize calculation.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / util.c
blob31b10850905fced1908ae098ad94dc62a911c3b9
1 /*
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"
30 #include "rate.h"
31 #include "mesh.h"
32 #include "wme.h"
33 #include "led.h"
34 #include "wep.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;
42 BUG_ON(!wiphy);
44 local = wiphy_priv(wiphy);
45 return &local->hw;
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) */
55 if (len < 16)
56 return NULL;
58 if (ieee80211_is_data(fc)) {
59 if (len < 24) /* drop incorrect hdr len (data) */
60 return NULL;
62 if (ieee80211_has_a4(fc))
63 return NULL;
64 if (ieee80211_has_tods(fc))
65 return hdr->addr1;
66 if (ieee80211_has_fromds(fc))
67 return hdr->addr2;
69 return hdr->addr3;
72 if (ieee80211_is_mgmt(fc)) {
73 if (len < 24) /* drop incorrect hdr len (mgmt) */
74 return NULL;
75 return hdr->addr3;
78 if (ieee80211_is_ctl(fc)) {
79 if(ieee80211_is_pspoll(fc))
80 return hdr->addr1;
82 if (ieee80211_is_back_req(fc)) {
83 switch (type) {
84 case NL80211_IFTYPE_STATION:
85 return hdr->addr2;
86 case NL80211_IFTYPE_AP:
87 case NL80211_IFTYPE_AP_VLAN:
88 return hdr->addr1;
89 default:
90 break; /* fall through to the return */
95 return NULL;
98 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
100 struct sk_buff *skb = tx->skb;
101 struct ieee80211_hdr *hdr;
103 do {
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)
112 int dur;
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
117 * also include SIFS.
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) {
125 * OFDM:
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
132 * T_SYM = 4 usec
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 */
142 } else {
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);
159 return dur;
162 /* Exported duration function for driver use */
163 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
164 struct ieee80211_vif *vif,
165 size_t frame_len,
166 struct ieee80211_rate *rate)
168 struct ieee80211_local *local = hw_to_local(hw);
169 struct ieee80211_sub_if_data *sdata;
170 u16 dur;
171 int erp;
172 bool short_preamble = false;
174 erp = 0;
175 if (vif) {
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,
183 short_preamble);
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;
196 bool short_preamble;
197 int erp;
198 u16 dur;
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];
207 erp = 0;
208 if (vif) {
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;
215 /* CTS duration */
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);
221 /* ACK duration */
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,
231 size_t frame_len,
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;
237 bool short_preamble;
238 int erp;
239 u16 dur;
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];
247 erp = 0;
248 if (vif) {
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)) {
259 /* ACK duration */
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);
272 struct ieee80211_sub_if_data *sdata;
274 if (WARN_ON(queue >= hw->queues))
275 return;
277 __clear_bit(reason, &local->queue_stop_reasons[queue]);
279 if (local->queue_stop_reasons[queue] != 0)
280 /* someone still has this queue stopped */
281 return;
283 if (skb_queue_empty(&local->pending[queue])) {
284 rcu_read_lock();
285 list_for_each_entry_rcu(sdata, &local->interfaces, list)
286 netif_tx_wake_queue(netdev_get_tx_queue(sdata->dev, queue));
287 rcu_read_unlock();
288 } else
289 tasklet_schedule(&local->tx_pending_tasklet);
292 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
293 enum queue_stop_reason reason)
295 struct ieee80211_local *local = hw_to_local(hw);
296 unsigned long flags;
298 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
299 __ieee80211_wake_queue(hw, queue, reason);
300 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
303 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
305 ieee80211_wake_queue_by_reason(hw, queue,
306 IEEE80211_QUEUE_STOP_REASON_DRIVER);
308 EXPORT_SYMBOL(ieee80211_wake_queue);
310 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
311 enum queue_stop_reason reason)
313 struct ieee80211_local *local = hw_to_local(hw);
314 struct ieee80211_sub_if_data *sdata;
316 if (WARN_ON(queue >= hw->queues))
317 return;
319 __set_bit(reason, &local->queue_stop_reasons[queue]);
321 rcu_read_lock();
322 list_for_each_entry_rcu(sdata, &local->interfaces, list)
323 netif_tx_stop_queue(netdev_get_tx_queue(sdata->dev, queue));
324 rcu_read_unlock();
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);
331 unsigned long flags;
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,
346 struct sk_buff *skb)
348 struct ieee80211_hw *hw = &local->hw;
349 unsigned long flags;
350 int queue = skb_get_queue_mapping(skb);
351 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
353 if (WARN_ON(!info->control.vif)) {
354 kfree_skb(skb);
355 return;
358 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
359 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
360 __skb_queue_tail(&local->pending[queue], skb);
361 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
362 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
365 int ieee80211_add_pending_skbs(struct ieee80211_local *local,
366 struct sk_buff_head *skbs)
368 struct ieee80211_hw *hw = &local->hw;
369 struct sk_buff *skb;
370 unsigned long flags;
371 int queue, ret = 0, i;
373 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
374 for (i = 0; i < hw->queues; i++)
375 __ieee80211_stop_queue(hw, i,
376 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
378 while ((skb = skb_dequeue(skbs))) {
379 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
381 if (WARN_ON(!info->control.vif)) {
382 kfree_skb(skb);
383 continue;
386 ret++;
387 queue = skb_get_queue_mapping(skb);
388 __skb_queue_tail(&local->pending[queue], skb);
391 for (i = 0; i < hw->queues; i++)
392 __ieee80211_wake_queue(hw, i,
393 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
394 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
396 return ret;
399 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
400 enum queue_stop_reason reason)
402 struct ieee80211_local *local = hw_to_local(hw);
403 unsigned long flags;
404 int i;
406 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
408 for (i = 0; i < hw->queues; i++)
409 __ieee80211_stop_queue(hw, i, reason);
411 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
414 void ieee80211_stop_queues(struct ieee80211_hw *hw)
416 ieee80211_stop_queues_by_reason(hw,
417 IEEE80211_QUEUE_STOP_REASON_DRIVER);
419 EXPORT_SYMBOL(ieee80211_stop_queues);
421 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
423 struct ieee80211_local *local = hw_to_local(hw);
424 unsigned long flags;
425 int ret;
427 if (WARN_ON(queue >= hw->queues))
428 return true;
430 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
431 ret = !!local->queue_stop_reasons[queue];
432 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
433 return ret;
435 EXPORT_SYMBOL(ieee80211_queue_stopped);
437 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
438 enum queue_stop_reason reason)
440 struct ieee80211_local *local = hw_to_local(hw);
441 unsigned long flags;
442 int i;
444 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
446 for (i = 0; i < hw->queues; i++)
447 __ieee80211_wake_queue(hw, i, reason);
449 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
452 void ieee80211_wake_queues(struct ieee80211_hw *hw)
454 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
456 EXPORT_SYMBOL(ieee80211_wake_queues);
458 void ieee80211_iterate_active_interfaces(
459 struct ieee80211_hw *hw,
460 void (*iterator)(void *data, u8 *mac,
461 struct ieee80211_vif *vif),
462 void *data)
464 struct ieee80211_local *local = hw_to_local(hw);
465 struct ieee80211_sub_if_data *sdata;
467 mutex_lock(&local->iflist_mtx);
469 list_for_each_entry(sdata, &local->interfaces, list) {
470 switch (sdata->vif.type) {
471 case __NL80211_IFTYPE_AFTER_LAST:
472 case NL80211_IFTYPE_UNSPECIFIED:
473 case NL80211_IFTYPE_MONITOR:
474 case NL80211_IFTYPE_AP_VLAN:
475 continue;
476 case NL80211_IFTYPE_AP:
477 case NL80211_IFTYPE_STATION:
478 case NL80211_IFTYPE_ADHOC:
479 case NL80211_IFTYPE_WDS:
480 case NL80211_IFTYPE_MESH_POINT:
481 break;
483 if (netif_running(sdata->dev))
484 iterator(data, sdata->dev->dev_addr,
485 &sdata->vif);
488 mutex_unlock(&local->iflist_mtx);
490 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
492 void ieee80211_iterate_active_interfaces_atomic(
493 struct ieee80211_hw *hw,
494 void (*iterator)(void *data, u8 *mac,
495 struct ieee80211_vif *vif),
496 void *data)
498 struct ieee80211_local *local = hw_to_local(hw);
499 struct ieee80211_sub_if_data *sdata;
501 rcu_read_lock();
503 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
504 switch (sdata->vif.type) {
505 case __NL80211_IFTYPE_AFTER_LAST:
506 case NL80211_IFTYPE_UNSPECIFIED:
507 case NL80211_IFTYPE_MONITOR:
508 case NL80211_IFTYPE_AP_VLAN:
509 continue;
510 case NL80211_IFTYPE_AP:
511 case NL80211_IFTYPE_STATION:
512 case NL80211_IFTYPE_ADHOC:
513 case NL80211_IFTYPE_WDS:
514 case NL80211_IFTYPE_MESH_POINT:
515 break;
517 if (netif_running(sdata->dev))
518 iterator(data, sdata->dev->dev_addr,
519 &sdata->vif);
522 rcu_read_unlock();
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"))
537 return false;
539 return true;
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))
547 return;
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,
555 unsigned long delay)
557 struct ieee80211_local *local = hw_to_local(hw);
559 if (!ieee80211_can_queue_work(local))
560 return;
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 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
573 struct ieee802_11_elems *elems,
574 u64 filter, u32 crc)
576 size_t left = len;
577 u8 *pos = start;
578 bool calc_crc = filter != 0;
580 memset(elems, 0, sizeof(*elems));
581 elems->ie_start = start;
582 elems->total_len = len;
584 while (left >= 2) {
585 u8 id, elen;
587 id = *pos++;
588 elen = *pos++;
589 left -= 2;
591 if (elen > left)
592 break;
594 if (calc_crc && id < 64 && (filter & (1ULL << id)))
595 crc = crc32_be(crc, pos - 2, elen + 2);
597 switch (id) {
598 case WLAN_EID_SSID:
599 elems->ssid = pos;
600 elems->ssid_len = elen;
601 break;
602 case WLAN_EID_SUPP_RATES:
603 elems->supp_rates = pos;
604 elems->supp_rates_len = elen;
605 break;
606 case WLAN_EID_FH_PARAMS:
607 elems->fh_params = pos;
608 elems->fh_params_len = elen;
609 break;
610 case WLAN_EID_DS_PARAMS:
611 elems->ds_params = pos;
612 elems->ds_params_len = elen;
613 break;
614 case WLAN_EID_CF_PARAMS:
615 elems->cf_params = pos;
616 elems->cf_params_len = elen;
617 break;
618 case WLAN_EID_TIM:
619 if (elen >= sizeof(struct ieee80211_tim_ie)) {
620 elems->tim = (void *)pos;
621 elems->tim_len = elen;
623 break;
624 case WLAN_EID_IBSS_PARAMS:
625 elems->ibss_params = pos;
626 elems->ibss_params_len = elen;
627 break;
628 case WLAN_EID_CHALLENGE:
629 elems->challenge = pos;
630 elems->challenge_len = elen;
631 break;
632 case WLAN_EID_VENDOR_SPECIFIC:
633 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
634 pos[2] == 0xf2) {
635 /* Microsoft OUI (00:50:F2) */
637 if (calc_crc)
638 crc = crc32_be(crc, pos - 2, elen + 2);
640 if (pos[3] == 1) {
641 /* OUI Type 1 - WPA IE */
642 elems->wpa = pos;
643 elems->wpa_len = elen;
644 } else if (elen >= 5 && pos[3] == 2) {
645 /* OUI Type 2 - WMM IE */
646 if (pos[4] == 0) {
647 elems->wmm_info = pos;
648 elems->wmm_info_len = elen;
649 } else if (pos[4] == 1) {
650 elems->wmm_param = pos;
651 elems->wmm_param_len = elen;
655 break;
656 case WLAN_EID_RSN:
657 elems->rsn = pos;
658 elems->rsn_len = elen;
659 break;
660 case WLAN_EID_ERP_INFO:
661 elems->erp_info = pos;
662 elems->erp_info_len = elen;
663 break;
664 case WLAN_EID_EXT_SUPP_RATES:
665 elems->ext_supp_rates = pos;
666 elems->ext_supp_rates_len = elen;
667 break;
668 case WLAN_EID_HT_CAPABILITY:
669 if (elen >= sizeof(struct ieee80211_ht_cap))
670 elems->ht_cap_elem = (void *)pos;
671 break;
672 case WLAN_EID_HT_INFORMATION:
673 if (elen >= sizeof(struct ieee80211_ht_info))
674 elems->ht_info_elem = (void *)pos;
675 break;
676 case WLAN_EID_MESH_ID:
677 elems->mesh_id = pos;
678 elems->mesh_id_len = elen;
679 break;
680 case WLAN_EID_MESH_CONFIG:
681 elems->mesh_config = pos;
682 elems->mesh_config_len = elen;
683 break;
684 case WLAN_EID_PEER_LINK:
685 elems->peer_link = pos;
686 elems->peer_link_len = elen;
687 break;
688 case WLAN_EID_PREQ:
689 elems->preq = pos;
690 elems->preq_len = elen;
691 break;
692 case WLAN_EID_PREP:
693 elems->prep = pos;
694 elems->prep_len = elen;
695 break;
696 case WLAN_EID_PERR:
697 elems->perr = pos;
698 elems->perr_len = elen;
699 break;
700 case WLAN_EID_CHANNEL_SWITCH:
701 elems->ch_switch_elem = pos;
702 elems->ch_switch_elem_len = elen;
703 break;
704 case WLAN_EID_QUIET:
705 if (!elems->quiet_elem) {
706 elems->quiet_elem = pos;
707 elems->quiet_elem_len = elen;
709 elems->num_of_quiet_elem++;
710 break;
711 case WLAN_EID_COUNTRY:
712 elems->country_elem = pos;
713 elems->country_elem_len = elen;
714 break;
715 case WLAN_EID_PWR_CONSTRAINT:
716 elems->pwr_constr_elem = pos;
717 elems->pwr_constr_elem_len = elen;
718 break;
719 case WLAN_EID_TIMEOUT_INTERVAL:
720 elems->timeout_int = pos;
721 elems->timeout_int_len = elen;
722 break;
723 default:
724 break;
727 left -= elen;
728 pos += elen;
731 return crc;
734 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
736 struct ieee80211_local *local = sdata->local;
737 struct ieee80211_tx_queue_params qparam;
738 int queue;
739 bool use_11b;
740 int aCWmin, aCWmax;
742 if (!local->ops->conf_tx)
743 return;
745 memset(&qparam, 0, sizeof(qparam));
747 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
748 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
750 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
751 /* Set defaults according to 802.11-2007 Table 7-37 */
752 aCWmax = 1023;
753 if (use_11b)
754 aCWmin = 31;
755 else
756 aCWmin = 15;
758 switch (queue) {
759 case 3: /* AC_BK */
760 qparam.cw_max = aCWmax;
761 qparam.cw_min = aCWmin;
762 qparam.txop = 0;
763 qparam.aifs = 7;
764 break;
765 default: /* never happens but let's not leave undefined */
766 case 2: /* AC_BE */
767 qparam.cw_max = aCWmax;
768 qparam.cw_min = aCWmin;
769 qparam.txop = 0;
770 qparam.aifs = 3;
771 break;
772 case 1: /* AC_VI */
773 qparam.cw_max = aCWmin;
774 qparam.cw_min = (aCWmin + 1) / 2 - 1;
775 if (use_11b)
776 qparam.txop = 6016/32;
777 else
778 qparam.txop = 3008/32;
779 qparam.aifs = 2;
780 break;
781 case 0: /* AC_VO */
782 qparam.cw_max = (aCWmin + 1) / 2 - 1;
783 qparam.cw_min = (aCWmin + 1) / 4 - 1;
784 if (use_11b)
785 qparam.txop = 3264/32;
786 else
787 qparam.txop = 1504/32;
788 qparam.aifs = 2;
789 break;
792 drv_conf_tx(local, queue, &qparam);
796 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
797 const size_t supp_rates_len,
798 const u8 *supp_rates)
800 struct ieee80211_local *local = sdata->local;
801 int i, have_higher_than_11mbit = 0;
803 /* cf. IEEE 802.11 9.2.12 */
804 for (i = 0; i < supp_rates_len; i++)
805 if ((supp_rates[i] & 0x7f) * 5 > 110)
806 have_higher_than_11mbit = 1;
808 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
809 have_higher_than_11mbit)
810 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
811 else
812 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
814 ieee80211_set_wmm_default(sdata);
817 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
818 enum ieee80211_band band)
820 struct ieee80211_supported_band *sband;
821 struct ieee80211_rate *bitrates;
822 u32 mandatory_rates;
823 enum ieee80211_rate_flags mandatory_flag;
824 int i;
826 sband = local->hw.wiphy->bands[band];
827 if (!sband) {
828 WARN_ON(1);
829 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
832 if (band == IEEE80211_BAND_2GHZ)
833 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
834 else
835 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
837 bitrates = sband->bitrates;
838 mandatory_rates = 0;
839 for (i = 0; i < sband->n_bitrates; i++)
840 if (bitrates[i].flags & mandatory_flag)
841 mandatory_rates |= BIT(i);
842 return mandatory_rates;
845 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
846 u16 transaction, u16 auth_alg,
847 u8 *extra, size_t extra_len, const u8 *bssid,
848 const u8 *key, u8 key_len, u8 key_idx)
850 struct ieee80211_local *local = sdata->local;
851 struct sk_buff *skb;
852 struct ieee80211_mgmt *mgmt;
853 int err;
855 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
856 sizeof(*mgmt) + 6 + extra_len);
857 if (!skb) {
858 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
859 "frame\n", sdata->dev->name);
860 return;
862 skb_reserve(skb, local->hw.extra_tx_headroom);
864 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
865 memset(mgmt, 0, 24 + 6);
866 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
867 IEEE80211_STYPE_AUTH);
868 memcpy(mgmt->da, bssid, ETH_ALEN);
869 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
870 memcpy(mgmt->bssid, bssid, ETH_ALEN);
871 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
872 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
873 mgmt->u.auth.status_code = cpu_to_le16(0);
874 if (extra)
875 memcpy(skb_put(skb, extra_len), extra, extra_len);
877 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
878 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
879 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
880 WARN_ON(err);
883 ieee80211_tx_skb(sdata, skb, 0);
886 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
887 const u8 *ie, size_t ie_len)
889 struct ieee80211_supported_band *sband;
890 u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
891 int i;
893 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
895 pos = buffer;
897 *pos++ = WLAN_EID_SUPP_RATES;
898 supp_rates_len = pos;
899 *pos++ = 0;
901 for (i = 0; i < sband->n_bitrates; i++) {
902 struct ieee80211_rate *rate = &sband->bitrates[i];
904 if (esupp_rates_len) {
905 *esupp_rates_len += 1;
906 } else if (*supp_rates_len == 8) {
907 *pos++ = WLAN_EID_EXT_SUPP_RATES;
908 esupp_rates_len = pos;
909 *pos++ = 1;
910 } else
911 *supp_rates_len += 1;
913 *pos++ = rate->bitrate / 5;
916 if (sband->ht_cap.ht_supported) {
917 __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
919 *pos++ = WLAN_EID_HT_CAPABILITY;
920 *pos++ = sizeof(struct ieee80211_ht_cap);
921 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
922 memcpy(pos, &tmp, sizeof(u16));
923 pos += sizeof(u16);
924 /* TODO: needs a define here for << 2 */
925 *pos++ = sband->ht_cap.ampdu_factor |
926 (sband->ht_cap.ampdu_density << 2);
927 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
928 pos += sizeof(sband->ht_cap.mcs);
929 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
933 * If adding more here, adjust code in main.c
934 * that calculates local->scan_ies_len.
937 if (ie) {
938 memcpy(pos, ie, ie_len);
939 pos += ie_len;
942 return pos - buffer;
945 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
946 const u8 *ssid, size_t ssid_len,
947 const u8 *ie, size_t ie_len)
949 struct ieee80211_local *local = sdata->local;
950 struct sk_buff *skb;
951 struct ieee80211_mgmt *mgmt;
952 u8 *pos;
954 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
955 ie_len);
956 if (!skb) {
957 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
958 "request\n", sdata->dev->name);
959 return;
961 skb_reserve(skb, local->hw.extra_tx_headroom);
963 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
964 memset(mgmt, 0, 24);
965 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
966 IEEE80211_STYPE_PROBE_REQ);
967 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
968 if (dst) {
969 memcpy(mgmt->da, dst, ETH_ALEN);
970 memcpy(mgmt->bssid, dst, ETH_ALEN);
971 } else {
972 memset(mgmt->da, 0xff, ETH_ALEN);
973 memset(mgmt->bssid, 0xff, ETH_ALEN);
975 pos = skb_put(skb, 2 + ssid_len);
976 *pos++ = WLAN_EID_SSID;
977 *pos++ = ssid_len;
978 memcpy(pos, ssid, ssid_len);
979 pos += ssid_len;
981 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
983 ieee80211_tx_skb(sdata, skb, 0);
986 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
987 struct ieee802_11_elems *elems,
988 enum ieee80211_band band)
990 struct ieee80211_supported_band *sband;
991 struct ieee80211_rate *bitrates;
992 size_t num_rates;
993 u32 supp_rates;
994 int i, j;
995 sband = local->hw.wiphy->bands[band];
997 if (!sband) {
998 WARN_ON(1);
999 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1002 bitrates = sband->bitrates;
1003 num_rates = sband->n_bitrates;
1004 supp_rates = 0;
1005 for (i = 0; i < elems->supp_rates_len +
1006 elems->ext_supp_rates_len; i++) {
1007 u8 rate = 0;
1008 int own_rate;
1009 if (i < elems->supp_rates_len)
1010 rate = elems->supp_rates[i];
1011 else if (elems->ext_supp_rates)
1012 rate = elems->ext_supp_rates
1013 [i - elems->supp_rates_len];
1014 own_rate = 5 * (rate & 0x7f);
1015 for (j = 0; j < num_rates; j++)
1016 if (bitrates[j].bitrate == own_rate)
1017 supp_rates |= BIT(j);
1019 return supp_rates;
1022 void ieee80211_stop_device(struct ieee80211_local *local)
1024 ieee80211_led_radio(local, false);
1026 cancel_work_sync(&local->reconfig_filter);
1027 drv_stop(local);
1029 flush_workqueue(local->workqueue);
1032 int ieee80211_reconfig(struct ieee80211_local *local)
1034 struct ieee80211_hw *hw = &local->hw;
1035 struct ieee80211_sub_if_data *sdata;
1036 struct ieee80211_if_init_conf conf;
1037 struct sta_info *sta;
1038 unsigned long flags;
1039 int res;
1041 if (local->suspended)
1042 local->resuming = true;
1044 /* restart hardware */
1045 if (local->open_count) {
1047 * Upon resume hardware can sometimes be goofy due to
1048 * various platform / driver / bus issues, so restarting
1049 * the device may at times not work immediately. Propagate
1050 * the error.
1052 res = drv_start(local);
1053 if (res) {
1054 WARN(local->suspended, "Harware became unavailable "
1055 "upon resume. This is could be a software issue"
1056 "prior to suspend or a harware issue\n");
1057 return res;
1060 ieee80211_led_radio(local, true);
1063 /* add interfaces */
1064 list_for_each_entry(sdata, &local->interfaces, list) {
1065 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1066 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1067 netif_running(sdata->dev)) {
1068 conf.vif = &sdata->vif;
1069 conf.type = sdata->vif.type;
1070 conf.mac_addr = sdata->dev->dev_addr;
1071 res = drv_add_interface(local, &conf);
1075 /* add STAs back */
1076 if (local->ops->sta_notify) {
1077 spin_lock_irqsave(&local->sta_lock, flags);
1078 list_for_each_entry(sta, &local->sta_list, list) {
1079 sdata = sta->sdata;
1080 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1081 sdata = container_of(sdata->bss,
1082 struct ieee80211_sub_if_data,
1083 u.ap);
1085 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
1086 &sta->sta);
1088 spin_unlock_irqrestore(&local->sta_lock, flags);
1091 /* Clear Suspend state so that ADDBA requests can be processed */
1093 rcu_read_lock();
1095 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1096 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1097 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1101 rcu_read_unlock();
1103 /* setup RTS threshold */
1104 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1106 /* reconfigure hardware */
1107 ieee80211_hw_config(local, ~0);
1109 ieee80211_configure_filter(local);
1111 /* Finally also reconfigure all the BSS information */
1112 list_for_each_entry(sdata, &local->interfaces, list) {
1113 u32 changed = ~0;
1114 if (!netif_running(sdata->dev))
1115 continue;
1116 switch (sdata->vif.type) {
1117 case NL80211_IFTYPE_STATION:
1118 /* disable beacon change bits */
1119 changed &= ~(BSS_CHANGED_BEACON |
1120 BSS_CHANGED_BEACON_ENABLED);
1121 /* fall through */
1122 case NL80211_IFTYPE_ADHOC:
1123 case NL80211_IFTYPE_AP:
1124 case NL80211_IFTYPE_MESH_POINT:
1125 ieee80211_bss_info_change_notify(sdata, changed);
1126 break;
1127 case NL80211_IFTYPE_WDS:
1128 break;
1129 case NL80211_IFTYPE_AP_VLAN:
1130 case NL80211_IFTYPE_MONITOR:
1131 /* ignore virtual */
1132 break;
1133 case NL80211_IFTYPE_UNSPECIFIED:
1134 case __NL80211_IFTYPE_AFTER_LAST:
1135 WARN_ON(1);
1136 break;
1140 rcu_read_lock();
1141 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1142 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1143 ieee80211_sta_tear_down_BA_sessions(sta);
1146 rcu_read_unlock();
1148 /* add back keys */
1149 list_for_each_entry(sdata, &local->interfaces, list)
1150 if (netif_running(sdata->dev))
1151 ieee80211_enable_keys(sdata);
1153 ieee80211_wake_queues_by_reason(hw,
1154 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1157 * If this is for hw restart things are still running.
1158 * We may want to change that later, however.
1160 if (!local->suspended)
1161 return 0;
1163 #ifdef CONFIG_PM
1164 /* first set suspended false, then resuming */
1165 local->suspended = false;
1166 mb();
1167 local->resuming = false;
1169 list_for_each_entry(sdata, &local->interfaces, list) {
1170 switch(sdata->vif.type) {
1171 case NL80211_IFTYPE_STATION:
1172 ieee80211_sta_restart(sdata);
1173 break;
1174 case NL80211_IFTYPE_ADHOC:
1175 ieee80211_ibss_restart(sdata);
1176 break;
1177 case NL80211_IFTYPE_MESH_POINT:
1178 ieee80211_mesh_restart(sdata);
1179 break;
1180 default:
1181 break;
1185 add_timer(&local->sta_cleanup);
1187 spin_lock_irqsave(&local->sta_lock, flags);
1188 list_for_each_entry(sta, &local->sta_list, list)
1189 mesh_plink_restart(sta);
1190 spin_unlock_irqrestore(&local->sta_lock, flags);
1191 #else
1192 WARN_ON(1);
1193 #endif
1194 return 0;