Merge remote-tracking branch 'sysctl/master'
[linux-2.6/next.git] / net / mac80211 / util.c
blobce916ff6ef089d54b0e98f31126e054f88b86992
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/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"
28 #include "rate.h"
29 #include "mesh.h"
30 #include "wme.h"
31 #include "led.h"
32 #include "wep.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;
40 BUG_ON(!wiphy);
42 local = wiphy_priv(wiphy);
43 return &local->hw;
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) */
53 if (len < 16)
54 return NULL;
56 if (ieee80211_is_data(fc)) {
57 if (len < 24) /* drop incorrect hdr len (data) */
58 return NULL;
60 if (ieee80211_has_a4(fc))
61 return NULL;
62 if (ieee80211_has_tods(fc))
63 return hdr->addr1;
64 if (ieee80211_has_fromds(fc))
65 return hdr->addr2;
67 return hdr->addr3;
70 if (ieee80211_is_mgmt(fc)) {
71 if (len < 24) /* drop incorrect hdr len (mgmt) */
72 return NULL;
73 return hdr->addr3;
76 if (ieee80211_is_ctl(fc)) {
77 if(ieee80211_is_pspoll(fc))
78 return hdr->addr1;
80 if (ieee80211_is_back_req(fc)) {
81 switch (type) {
82 case NL80211_IFTYPE_STATION:
83 return hdr->addr2;
84 case NL80211_IFTYPE_AP:
85 case NL80211_IFTYPE_AP_VLAN:
86 return hdr->addr1;
87 default:
88 break; /* fall through to the return */
93 return NULL;
96 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
98 struct sk_buff *skb = tx->skb;
99 struct ieee80211_hdr *hdr;
101 do {
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)
110 int dur;
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
115 * also include SIFS.
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) {
123 * OFDM:
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
130 * T_SYM = 4 usec
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 */
140 } else {
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);
157 return dur;
160 /* Exported duration function for driver use */
161 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
162 struct ieee80211_vif *vif,
163 size_t frame_len,
164 struct ieee80211_rate *rate)
166 struct ieee80211_local *local = hw_to_local(hw);
167 struct ieee80211_sub_if_data *sdata;
168 u16 dur;
169 int erp;
170 bool short_preamble = false;
172 erp = 0;
173 if (vif) {
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,
181 short_preamble);
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;
194 bool short_preamble;
195 int erp;
196 u16 dur;
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];
205 erp = 0;
206 if (vif) {
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;
213 /* CTS duration */
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);
219 /* ACK duration */
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,
229 size_t frame_len,
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;
235 bool short_preamble;
236 int erp;
237 u16 dur;
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];
245 erp = 0;
246 if (vif) {
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)) {
257 /* ACK duration */
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))
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 if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
287 continue;
288 netif_wake_subqueue(sdata->dev, queue);
290 rcu_read_unlock();
291 } else
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);
299 unsigned long flags;
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))
322 return;
324 __set_bit(reason, &local->queue_stop_reasons[queue]);
326 rcu_read_lock();
327 list_for_each_entry_rcu(sdata, &local->interfaces, list)
328 netif_stop_subqueue(sdata->dev, queue);
329 rcu_read_unlock();
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);
336 unsigned long flags;
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,
351 struct sk_buff *skb)
353 struct ieee80211_hw *hw = &local->hw;
354 unsigned long flags;
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)) {
359 kfree_skb(skb);
360 return;
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 int 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;
375 struct sk_buff *skb;
376 unsigned long flags;
377 int queue, ret = 0, i;
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)) {
388 kfree_skb(skb);
389 continue;
392 ret++;
393 queue = skb_get_queue_mapping(skb);
394 __skb_queue_tail(&local->pending[queue], skb);
397 if (fn)
398 fn(data);
400 for (i = 0; i < hw->queues; i++)
401 __ieee80211_wake_queue(hw, i,
402 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
403 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
405 return ret;
408 int ieee80211_add_pending_skbs(struct ieee80211_local *local,
409 struct sk_buff_head *skbs)
411 return ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
414 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
415 enum queue_stop_reason reason)
417 struct ieee80211_local *local = hw_to_local(hw);
418 unsigned long flags;
419 int i;
421 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
423 for (i = 0; i < hw->queues; i++)
424 __ieee80211_stop_queue(hw, i, reason);
426 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
429 void ieee80211_stop_queues(struct ieee80211_hw *hw)
431 ieee80211_stop_queues_by_reason(hw,
432 IEEE80211_QUEUE_STOP_REASON_DRIVER);
434 EXPORT_SYMBOL(ieee80211_stop_queues);
436 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
438 struct ieee80211_local *local = hw_to_local(hw);
439 unsigned long flags;
440 int ret;
442 if (WARN_ON(queue >= hw->queues))
443 return true;
445 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
446 ret = !!local->queue_stop_reasons[queue];
447 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
448 return ret;
450 EXPORT_SYMBOL(ieee80211_queue_stopped);
452 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
453 enum queue_stop_reason reason)
455 struct ieee80211_local *local = hw_to_local(hw);
456 unsigned long flags;
457 int i;
459 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
461 for (i = 0; i < hw->queues; i++)
462 __ieee80211_wake_queue(hw, i, reason);
464 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
467 void ieee80211_wake_queues(struct ieee80211_hw *hw)
469 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
471 EXPORT_SYMBOL(ieee80211_wake_queues);
473 void ieee80211_iterate_active_interfaces(
474 struct ieee80211_hw *hw,
475 void (*iterator)(void *data, u8 *mac,
476 struct ieee80211_vif *vif),
477 void *data)
479 struct ieee80211_local *local = hw_to_local(hw);
480 struct ieee80211_sub_if_data *sdata;
482 mutex_lock(&local->iflist_mtx);
484 list_for_each_entry(sdata, &local->interfaces, list) {
485 switch (sdata->vif.type) {
486 case NL80211_IFTYPE_MONITOR:
487 case NL80211_IFTYPE_AP_VLAN:
488 continue;
489 default:
490 break;
492 if (ieee80211_sdata_running(sdata))
493 iterator(data, sdata->vif.addr,
494 &sdata->vif);
497 mutex_unlock(&local->iflist_mtx);
499 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
501 void ieee80211_iterate_active_interfaces_atomic(
502 struct ieee80211_hw *hw,
503 void (*iterator)(void *data, u8 *mac,
504 struct ieee80211_vif *vif),
505 void *data)
507 struct ieee80211_local *local = hw_to_local(hw);
508 struct ieee80211_sub_if_data *sdata;
510 rcu_read_lock();
512 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
513 switch (sdata->vif.type) {
514 case NL80211_IFTYPE_MONITOR:
515 case NL80211_IFTYPE_AP_VLAN:
516 continue;
517 default:
518 break;
520 if (ieee80211_sdata_running(sdata))
521 iterator(data, sdata->vif.addr,
522 &sdata->vif);
525 rcu_read_unlock();
527 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
530 * Nothing should have been stuffed into the workqueue during
531 * the suspend->resume cycle. If this WARN is seen then there
532 * is a bug with either the driver suspend or something in
533 * mac80211 stuffing into the workqueue which we haven't yet
534 * cleared during mac80211's suspend cycle.
536 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
538 if (WARN(local->suspended && !local->resuming,
539 "queueing ieee80211 work while going to suspend\n"))
540 return false;
542 return true;
545 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
547 struct ieee80211_local *local = hw_to_local(hw);
549 if (!ieee80211_can_queue_work(local))
550 return;
552 queue_work(local->workqueue, work);
554 EXPORT_SYMBOL(ieee80211_queue_work);
556 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
557 struct delayed_work *dwork,
558 unsigned long delay)
560 struct ieee80211_local *local = hw_to_local(hw);
562 if (!ieee80211_can_queue_work(local))
563 return;
565 queue_delayed_work(local->workqueue, dwork, delay);
567 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
569 void ieee802_11_parse_elems(u8 *start, size_t len,
570 struct ieee802_11_elems *elems)
572 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
575 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
577 struct ieee80211_local *local = sdata->local;
578 struct ieee80211_tx_queue_params qparam;
579 int queue;
580 bool use_11b;
581 int aCWmin, aCWmax;
583 if (!local->ops->conf_tx)
584 return;
586 memset(&qparam, 0, sizeof(qparam));
588 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
589 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
591 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
592 /* Set defaults according to 802.11-2007 Table 7-37 */
593 aCWmax = 1023;
594 if (use_11b)
595 aCWmin = 31;
596 else
597 aCWmin = 15;
599 switch (queue) {
600 case 3: /* AC_BK */
601 qparam.cw_max = aCWmax;
602 qparam.cw_min = aCWmin;
603 qparam.txop = 0;
604 qparam.aifs = 7;
605 break;
606 default: /* never happens but let's not leave undefined */
607 case 2: /* AC_BE */
608 qparam.cw_max = aCWmax;
609 qparam.cw_min = aCWmin;
610 qparam.txop = 0;
611 qparam.aifs = 3;
612 break;
613 case 1: /* AC_VI */
614 qparam.cw_max = aCWmin;
615 qparam.cw_min = (aCWmin + 1) / 2 - 1;
616 if (use_11b)
617 qparam.txop = 6016/32;
618 else
619 qparam.txop = 3008/32;
620 qparam.aifs = 2;
621 break;
622 case 0: /* AC_VO */
623 qparam.cw_max = (aCWmin + 1) / 2 - 1;
624 qparam.cw_min = (aCWmin + 1) / 4 - 1;
625 if (use_11b)
626 qparam.txop = 3264/32;
627 else
628 qparam.txop = 1504/32;
629 qparam.aifs = 2;
630 break;
633 qparam.uapsd = false;
635 local->tx_conf[queue] = qparam;
636 drv_conf_tx(local, queue, &qparam);
639 /* after reinitialize QoS TX queues setting to default,
640 * disable QoS at all */
642 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
643 sdata->vif.bss_conf.qos =
644 sdata->vif.type != NL80211_IFTYPE_STATION;
645 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
649 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
650 const size_t supp_rates_len,
651 const u8 *supp_rates)
653 struct ieee80211_local *local = sdata->local;
654 int i, have_higher_than_11mbit = 0;
656 /* cf. IEEE 802.11 9.2.12 */
657 for (i = 0; i < supp_rates_len; i++)
658 if ((supp_rates[i] & 0x7f) * 5 > 110)
659 have_higher_than_11mbit = 1;
661 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
662 have_higher_than_11mbit)
663 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
664 else
665 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
667 ieee80211_set_wmm_default(sdata);
670 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
671 enum ieee80211_band band)
673 struct ieee80211_supported_band *sband;
674 struct ieee80211_rate *bitrates;
675 u32 mandatory_rates;
676 enum ieee80211_rate_flags mandatory_flag;
677 int i;
679 sband = local->hw.wiphy->bands[band];
680 if (!sband) {
681 WARN_ON(1);
682 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
685 if (band == IEEE80211_BAND_2GHZ)
686 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
687 else
688 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
690 bitrates = sband->bitrates;
691 mandatory_rates = 0;
692 for (i = 0; i < sband->n_bitrates; i++)
693 if (bitrates[i].flags & mandatory_flag)
694 mandatory_rates |= BIT(i);
695 return mandatory_rates;
698 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
699 u16 transaction, u16 auth_alg,
700 u8 *extra, size_t extra_len, const u8 *bssid,
701 const u8 *key, u8 key_len, u8 key_idx)
703 struct ieee80211_local *local = sdata->local;
704 struct sk_buff *skb;
705 struct ieee80211_mgmt *mgmt;
706 int err;
708 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
709 sizeof(*mgmt) + 6 + extra_len);
710 if (!skb) {
711 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
712 "frame\n", sdata->name);
713 return;
715 skb_reserve(skb, local->hw.extra_tx_headroom);
717 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
718 memset(mgmt, 0, 24 + 6);
719 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
720 IEEE80211_STYPE_AUTH);
721 memcpy(mgmt->da, bssid, ETH_ALEN);
722 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
723 memcpy(mgmt->bssid, bssid, ETH_ALEN);
724 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
725 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
726 mgmt->u.auth.status_code = cpu_to_le16(0);
727 if (extra)
728 memcpy(skb_put(skb, extra_len), extra, extra_len);
730 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
731 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
732 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
733 WARN_ON(err);
736 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
737 ieee80211_tx_skb(sdata, skb);
740 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
741 const u8 *ie, size_t ie_len,
742 enum ieee80211_band band, u32 rate_mask,
743 u8 channel)
745 struct ieee80211_supported_band *sband;
746 u8 *pos;
747 size_t offset = 0, noffset;
748 int supp_rates_len, i;
749 u8 rates[32];
750 int num_rates;
751 int ext_rates_len;
753 sband = local->hw.wiphy->bands[band];
755 pos = buffer;
757 num_rates = 0;
758 for (i = 0; i < sband->n_bitrates; i++) {
759 if ((BIT(i) & rate_mask) == 0)
760 continue; /* skip rate */
761 rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
764 supp_rates_len = min_t(int, num_rates, 8);
766 *pos++ = WLAN_EID_SUPP_RATES;
767 *pos++ = supp_rates_len;
768 memcpy(pos, rates, supp_rates_len);
769 pos += supp_rates_len;
771 /* insert "request information" if in custom IEs */
772 if (ie && ie_len) {
773 static const u8 before_extrates[] = {
774 WLAN_EID_SSID,
775 WLAN_EID_SUPP_RATES,
776 WLAN_EID_REQUEST,
778 noffset = ieee80211_ie_split(ie, ie_len,
779 before_extrates,
780 ARRAY_SIZE(before_extrates),
781 offset);
782 memcpy(pos, ie + offset, noffset - offset);
783 pos += noffset - offset;
784 offset = noffset;
787 ext_rates_len = num_rates - supp_rates_len;
788 if (ext_rates_len > 0) {
789 *pos++ = WLAN_EID_EXT_SUPP_RATES;
790 *pos++ = ext_rates_len;
791 memcpy(pos, rates + supp_rates_len, ext_rates_len);
792 pos += ext_rates_len;
795 if (channel && sband->band == IEEE80211_BAND_2GHZ) {
796 *pos++ = WLAN_EID_DS_PARAMS;
797 *pos++ = 1;
798 *pos++ = channel;
801 /* insert custom IEs that go before HT */
802 if (ie && ie_len) {
803 static const u8 before_ht[] = {
804 WLAN_EID_SSID,
805 WLAN_EID_SUPP_RATES,
806 WLAN_EID_REQUEST,
807 WLAN_EID_EXT_SUPP_RATES,
808 WLAN_EID_DS_PARAMS,
809 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
811 noffset = ieee80211_ie_split(ie, ie_len,
812 before_ht, ARRAY_SIZE(before_ht),
813 offset);
814 memcpy(pos, ie + offset, noffset - offset);
815 pos += noffset - offset;
816 offset = noffset;
819 if (sband->ht_cap.ht_supported) {
820 u16 cap = sband->ht_cap.cap;
821 __le16 tmp;
823 *pos++ = WLAN_EID_HT_CAPABILITY;
824 *pos++ = sizeof(struct ieee80211_ht_cap);
825 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
826 tmp = cpu_to_le16(cap);
827 memcpy(pos, &tmp, sizeof(u16));
828 pos += sizeof(u16);
829 *pos++ = sband->ht_cap.ampdu_factor |
830 (sband->ht_cap.ampdu_density <<
831 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
832 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
833 pos += sizeof(sband->ht_cap.mcs);
834 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
838 * If adding more here, adjust code in main.c
839 * that calculates local->scan_ies_len.
842 /* add any remaining custom IEs */
843 if (ie && ie_len) {
844 noffset = ie_len;
845 memcpy(pos, ie + offset, noffset - offset);
846 pos += noffset - offset;
849 return pos - buffer;
852 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
853 u8 *dst, u32 ratemask,
854 const u8 *ssid, size_t ssid_len,
855 const u8 *ie, size_t ie_len,
856 bool directed)
858 struct ieee80211_local *local = sdata->local;
859 struct sk_buff *skb;
860 struct ieee80211_mgmt *mgmt;
861 size_t buf_len;
862 u8 *buf;
863 u8 chan;
865 /* FIXME: come up with a proper value */
866 buf = kmalloc(200 + ie_len, GFP_KERNEL);
867 if (!buf) {
868 printk(KERN_DEBUG "%s: failed to allocate temporary IE "
869 "buffer\n", sdata->name);
870 return NULL;
874 * Do not send DS Channel parameter for directed probe requests
875 * in order to maximize the chance that we get a response. Some
876 * badly-behaved APs don't respond when this parameter is included.
878 if (directed)
879 chan = 0;
880 else
881 chan = ieee80211_frequency_to_channel(
882 local->hw.conf.channel->center_freq);
884 buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
885 local->hw.conf.channel->band,
886 ratemask, chan);
888 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
889 ssid, ssid_len,
890 buf, buf_len);
892 if (dst) {
893 mgmt = (struct ieee80211_mgmt *) skb->data;
894 memcpy(mgmt->da, dst, ETH_ALEN);
895 memcpy(mgmt->bssid, dst, ETH_ALEN);
898 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
899 kfree(buf);
901 return skb;
904 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
905 const u8 *ssid, size_t ssid_len,
906 const u8 *ie, size_t ie_len,
907 u32 ratemask, bool directed)
909 struct sk_buff *skb;
911 skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
912 ie, ie_len, directed);
913 if (skb)
914 ieee80211_tx_skb(sdata, skb);
917 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
918 struct ieee802_11_elems *elems,
919 enum ieee80211_band band)
921 struct ieee80211_supported_band *sband;
922 struct ieee80211_rate *bitrates;
923 size_t num_rates;
924 u32 supp_rates;
925 int i, j;
926 sband = local->hw.wiphy->bands[band];
928 if (!sband) {
929 WARN_ON(1);
930 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
933 bitrates = sband->bitrates;
934 num_rates = sband->n_bitrates;
935 supp_rates = 0;
936 for (i = 0; i < elems->supp_rates_len +
937 elems->ext_supp_rates_len; i++) {
938 u8 rate = 0;
939 int own_rate;
940 if (i < elems->supp_rates_len)
941 rate = elems->supp_rates[i];
942 else if (elems->ext_supp_rates)
943 rate = elems->ext_supp_rates
944 [i - elems->supp_rates_len];
945 own_rate = 5 * (rate & 0x7f);
946 for (j = 0; j < num_rates; j++)
947 if (bitrates[j].bitrate == own_rate)
948 supp_rates |= BIT(j);
950 return supp_rates;
953 void ieee80211_stop_device(struct ieee80211_local *local)
955 ieee80211_led_radio(local, false);
956 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
958 cancel_work_sync(&local->reconfig_filter);
960 flush_workqueue(local->workqueue);
961 drv_stop(local);
964 int ieee80211_reconfig(struct ieee80211_local *local)
966 struct ieee80211_hw *hw = &local->hw;
967 struct ieee80211_sub_if_data *sdata;
968 struct sta_info *sta;
969 int res, i;
971 #ifdef CONFIG_PM
972 if (local->suspended)
973 local->resuming = true;
975 if (local->wowlan) {
976 local->wowlan = false;
977 res = drv_resume(local);
978 if (res < 0) {
979 local->resuming = false;
980 return res;
982 if (res == 0)
983 goto wake_up;
984 WARN_ON(res > 1);
986 * res is 1, which means the driver requested
987 * to go through a regular reset on wakeup.
990 #endif
992 /* setup fragmentation threshold */
993 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
995 /* setup RTS threshold */
996 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
998 /* reset coverage class */
999 drv_set_coverage_class(local, hw->wiphy->coverage_class);
1001 /* everything else happens only if HW was up & running */
1002 if (!local->open_count)
1003 goto wake_up;
1006 * Upon resume hardware can sometimes be goofy due to
1007 * various platform / driver / bus issues, so restarting
1008 * the device may at times not work immediately. Propagate
1009 * the error.
1011 res = drv_start(local);
1012 if (res) {
1013 WARN(local->suspended, "Hardware became unavailable "
1014 "upon resume. This could be a software issue "
1015 "prior to suspend or a hardware issue.\n");
1016 return res;
1019 ieee80211_led_radio(local, true);
1020 ieee80211_mod_tpt_led_trig(local,
1021 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1023 /* add interfaces */
1024 list_for_each_entry(sdata, &local->interfaces, list) {
1025 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1026 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1027 ieee80211_sdata_running(sdata))
1028 res = drv_add_interface(local, &sdata->vif);
1031 /* add STAs back */
1032 mutex_lock(&local->sta_mtx);
1033 list_for_each_entry(sta, &local->sta_list, list) {
1034 if (sta->uploaded) {
1035 sdata = sta->sdata;
1036 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1037 sdata = container_of(sdata->bss,
1038 struct ieee80211_sub_if_data,
1039 u.ap);
1041 memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
1042 WARN_ON(drv_sta_add(local, sdata, &sta->sta));
1045 mutex_unlock(&local->sta_mtx);
1047 /* reconfigure tx conf */
1048 for (i = 0; i < hw->queues; i++)
1049 drv_conf_tx(local, i, &local->tx_conf[i]);
1051 /* reconfigure hardware */
1052 ieee80211_hw_config(local, ~0);
1054 ieee80211_configure_filter(local);
1056 /* Finally also reconfigure all the BSS information */
1057 list_for_each_entry(sdata, &local->interfaces, list) {
1058 u32 changed;
1060 if (!ieee80211_sdata_running(sdata))
1061 continue;
1063 /* common change flags for all interface types */
1064 changed = BSS_CHANGED_ERP_CTS_PROT |
1065 BSS_CHANGED_ERP_PREAMBLE |
1066 BSS_CHANGED_ERP_SLOT |
1067 BSS_CHANGED_HT |
1068 BSS_CHANGED_BASIC_RATES |
1069 BSS_CHANGED_BEACON_INT |
1070 BSS_CHANGED_BSSID |
1071 BSS_CHANGED_CQM |
1072 BSS_CHANGED_QOS;
1074 switch (sdata->vif.type) {
1075 case NL80211_IFTYPE_STATION:
1076 changed |= BSS_CHANGED_ASSOC;
1077 mutex_lock(&sdata->u.mgd.mtx);
1078 ieee80211_bss_info_change_notify(sdata, changed);
1079 mutex_unlock(&sdata->u.mgd.mtx);
1080 break;
1081 case NL80211_IFTYPE_ADHOC:
1082 changed |= BSS_CHANGED_IBSS;
1083 /* fall through */
1084 case NL80211_IFTYPE_AP:
1085 case NL80211_IFTYPE_MESH_POINT:
1086 changed |= BSS_CHANGED_BEACON |
1087 BSS_CHANGED_BEACON_ENABLED;
1088 ieee80211_bss_info_change_notify(sdata, changed);
1089 break;
1090 case NL80211_IFTYPE_WDS:
1091 break;
1092 case NL80211_IFTYPE_AP_VLAN:
1093 case NL80211_IFTYPE_MONITOR:
1094 /* ignore virtual */
1095 break;
1096 case NL80211_IFTYPE_UNSPECIFIED:
1097 case NUM_NL80211_IFTYPES:
1098 case NL80211_IFTYPE_P2P_CLIENT:
1099 case NL80211_IFTYPE_P2P_GO:
1100 WARN_ON(1);
1101 break;
1106 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1107 * sessions can be established after a resume.
1109 * Also tear down aggregation sessions since reconfiguring
1110 * them in a hardware restart scenario is not easily done
1111 * right now, and the hardware will have lost information
1112 * about the sessions, but we and the AP still think they
1113 * are active. This is really a workaround though.
1115 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1116 mutex_lock(&local->sta_mtx);
1118 list_for_each_entry(sta, &local->sta_list, list) {
1119 ieee80211_sta_tear_down_BA_sessions(sta, true);
1120 clear_sta_flags(sta, WLAN_STA_BLOCK_BA);
1123 mutex_unlock(&local->sta_mtx);
1126 /* add back keys */
1127 list_for_each_entry(sdata, &local->interfaces, list)
1128 if (ieee80211_sdata_running(sdata))
1129 ieee80211_enable_keys(sdata);
1131 wake_up:
1132 ieee80211_wake_queues_by_reason(hw,
1133 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1136 * If this is for hw restart things are still running.
1137 * We may want to change that later, however.
1139 if (!local->suspended)
1140 return 0;
1142 #ifdef CONFIG_PM
1143 /* first set suspended false, then resuming */
1144 local->suspended = false;
1145 mb();
1146 local->resuming = false;
1148 list_for_each_entry(sdata, &local->interfaces, list) {
1149 switch(sdata->vif.type) {
1150 case NL80211_IFTYPE_STATION:
1151 ieee80211_sta_restart(sdata);
1152 break;
1153 case NL80211_IFTYPE_ADHOC:
1154 ieee80211_ibss_restart(sdata);
1155 break;
1156 case NL80211_IFTYPE_MESH_POINT:
1157 ieee80211_mesh_restart(sdata);
1158 break;
1159 default:
1160 break;
1164 mod_timer(&local->sta_cleanup, jiffies + 1);
1166 mutex_lock(&local->sta_mtx);
1167 list_for_each_entry(sta, &local->sta_list, list)
1168 mesh_plink_restart(sta);
1169 mutex_unlock(&local->sta_mtx);
1170 #else
1171 WARN_ON(1);
1172 #endif
1173 return 0;
1176 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1178 struct ieee80211_sub_if_data *sdata;
1179 struct ieee80211_local *local;
1180 struct ieee80211_key *key;
1182 if (WARN_ON(!vif))
1183 return;
1185 sdata = vif_to_sdata(vif);
1186 local = sdata->local;
1188 if (WARN_ON(!local->resuming))
1189 return;
1191 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1192 return;
1194 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1196 mutex_lock(&local->key_mtx);
1197 list_for_each_entry(key, &sdata->key_list, list)
1198 key->flags |= KEY_FLAG_TAINTED;
1199 mutex_unlock(&local->key_mtx);
1201 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1203 static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1204 enum ieee80211_smps_mode *smps_mode)
1206 if (ifmgd->associated) {
1207 *smps_mode = ifmgd->ap_smps;
1209 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1210 if (ifmgd->powersave)
1211 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1212 else
1213 *smps_mode = IEEE80211_SMPS_OFF;
1216 return 1;
1219 return 0;
1222 /* must hold iflist_mtx */
1223 void ieee80211_recalc_smps(struct ieee80211_local *local)
1225 struct ieee80211_sub_if_data *sdata;
1226 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1227 int count = 0;
1229 lockdep_assert_held(&local->iflist_mtx);
1232 * This function could be improved to handle multiple
1233 * interfaces better, but right now it makes any
1234 * non-station interfaces force SM PS to be turned
1235 * off. If there are multiple station interfaces it
1236 * could also use the best possible mode, e.g. if
1237 * one is in static and the other in dynamic then
1238 * dynamic is ok.
1241 list_for_each_entry(sdata, &local->interfaces, list) {
1242 if (!ieee80211_sdata_running(sdata))
1243 continue;
1244 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1245 goto set;
1247 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1249 if (count > 1) {
1250 smps_mode = IEEE80211_SMPS_OFF;
1251 break;
1255 if (smps_mode == local->smps_mode)
1256 return;
1258 set:
1259 local->smps_mode = smps_mode;
1260 /* changed flag is auto-detected for this */
1261 ieee80211_hw_config(local, 0);
1264 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1266 int i;
1268 for (i = 0; i < n_ids; i++)
1269 if (ids[i] == id)
1270 return true;
1271 return false;
1275 * ieee80211_ie_split - split an IE buffer according to ordering
1277 * @ies: the IE buffer
1278 * @ielen: the length of the IE buffer
1279 * @ids: an array with element IDs that are allowed before
1280 * the split
1281 * @n_ids: the size of the element ID array
1282 * @offset: offset where to start splitting in the buffer
1284 * This function splits an IE buffer by updating the @offset
1285 * variable to point to the location where the buffer should be
1286 * split.
1288 * It assumes that the given IE buffer is well-formed, this
1289 * has to be guaranteed by the caller!
1291 * It also assumes that the IEs in the buffer are ordered
1292 * correctly, if not the result of using this function will not
1293 * be ordered correctly either, i.e. it does no reordering.
1295 * The function returns the offset where the next part of the
1296 * buffer starts, which may be @ielen if the entire (remainder)
1297 * of the buffer should be used.
1299 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1300 const u8 *ids, int n_ids, size_t offset)
1302 size_t pos = offset;
1304 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1305 pos += 2 + ies[pos + 1];
1307 return pos;
1310 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1312 size_t pos = offset;
1314 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1315 pos += 2 + ies[pos + 1];
1317 return pos;
1320 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
1321 int rssi_min_thold,
1322 int rssi_max_thold)
1324 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
1326 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1327 return;
1330 * Scale up threshold values before storing it, as the RSSI averaging
1331 * algorithm uses a scaled up value as well. Change this scaling
1332 * factor if the RSSI averaging algorithm changes.
1334 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
1335 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
1338 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
1339 int rssi_min_thold,
1340 int rssi_max_thold)
1342 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1344 WARN_ON(rssi_min_thold == rssi_max_thold ||
1345 rssi_min_thold > rssi_max_thold);
1347 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
1348 rssi_max_thold);
1350 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
1352 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
1354 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1356 _ieee80211_enable_rssi_reports(sdata, 0, 0);
1358 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);