blob: 3848140313f50dad60759e5fd55a7923fa46f5ff [file] [log] [blame]
Johannes Bergc2d15602007-07-27 15:43:23 +02001/*
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>
6 *
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.
10 *
11 * utilities for mac80211
12 */
13
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>
Johannes Bergd91f36d2009-04-16 13:17:26 +020023#include <linux/crc32.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070024#include <net/net_namespace.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020025#include <net/cfg80211.h>
Johannes Bergdabeb342007-11-09 01:57:29 +010026#include <net/rtnetlink.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020027
28#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010031#include "mesh.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020032#include "wme.h"
Johannes Bergf2753dd2009-04-14 10:09:24 +020033#include "led.h"
Johannes Bergfffd0932009-07-08 14:22:54 +020034#include "wep.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020035
36/* privid for wiphys to determine whether they belong to us or not */
37void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
38
Luis R. Rodriguez9a953712009-01-22 15:05:53 -080039struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
40{
41 struct ieee80211_local *local;
42 BUG_ON(!wiphy);
43
44 local = wiphy_priv(wiphy);
45 return &local->hw;
46}
47EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
Johannes Bergc2d15602007-07-27 15:43:23 +020048
Ron Rindjunsky71364712007-12-25 17:00:36 +020049u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020050 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020051{
Harvey Harrisona494bb12008-06-11 14:21:58 -070052 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020053
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020054 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
55 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020056 return NULL;
57
Harvey Harrisona494bb12008-06-11 14:21:58 -070058 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020059 if (len < 24) /* drop incorrect hdr len (data) */
60 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070061
62 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020063 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070064 if (ieee80211_has_tods(fc))
65 return hdr->addr1;
66 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020067 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070068
69 return hdr->addr3;
70 }
71
72 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020073 if (len < 24) /* drop incorrect hdr len (mgmt) */
74 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020075 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070076 }
77
78 if (ieee80211_is_ctl(fc)) {
79 if(ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020080 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070081
82 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020083 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020084 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020085 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020086 case NL80211_IFTYPE_AP:
87 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020088 return hdr->addr1;
89 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070090 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020091 }
92 }
Johannes Bergc2d15602007-07-27 15:43:23 +020093 }
94
95 return NULL;
96}
97
Johannes Berg5cf121c2008-02-25 16:27:43 +010098void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +020099{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100100 struct sk_buff *skb = tx->skb;
101 struct ieee80211_hdr *hdr;
Johannes Bergc2d15602007-07-27 15:43:23 +0200102
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100103 do {
104 hdr = (struct ieee80211_hdr *) skb->data;
105 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
106 } while ((skb = skb->next));
Johannes Bergc2d15602007-07-27 15:43:23 +0200107}
108
109int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
110 int rate, int erp, int short_preamble)
111{
112 int dur;
113
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.
118 *
119 * rate is in 100 kbps, so divident is multiplied by 10 in the
120 * DIV_ROUND_UP() operations.
121 */
122
Johannes Berg8318d782008-01-24 19:38:38 +0100123 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200124 /*
125 * OFDM:
126 *
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
131 *
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
136 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200137 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 {
143 /*
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.
147 *
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
152 */
153 dur = 10; /* aSIFSTime = 10 usec */
154 dur += short_preamble ? (72 + 24) : (144 + 48);
155
156 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
157 }
158
159 return dur;
160}
161
162/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100163__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
164 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100165 size_t frame_len,
166 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200167{
168 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg25d834e2008-09-12 22:52:47 +0200169 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200170 u16 dur;
171 int erp;
Johannes Berg25d834e2008-09-12 22:52:47 +0200172 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200173
Johannes Berg8318d782008-01-24 19:38:38 +0100174 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200175 if (vif) {
176 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200177 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200178 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
179 erp = rate->flags & IEEE80211_RATE_ERP_G;
180 }
Johannes Berg8318d782008-01-24 19:38:38 +0100181
182 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg25d834e2008-09-12 22:52:47 +0200183 short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200184
185 return cpu_to_le16(dur);
186}
187EXPORT_SYMBOL(ieee80211_generic_frame_duration);
188
Johannes Berg32bfd352007-12-19 01:31:26 +0100189__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
190 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200191 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200192{
193 struct ieee80211_local *local = hw_to_local(hw);
194 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200195 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100196 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200197 int erp;
198 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200199 struct ieee80211_supported_band *sband;
200
201 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200202
Johannes Berg25d834e2008-09-12 22:52:47 +0200203 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200204
Johannes Berge039fa42008-05-15 12:55:29 +0200205 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100206
207 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200208 if (vif) {
209 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200210 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200211 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
212 erp = rate->flags & IEEE80211_RATE_ERP_G;
213 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200214
215 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100216 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200217 erp, short_preamble);
218 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100219 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200220 erp, short_preamble);
221 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100222 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200223 erp, short_preamble);
224
225 return cpu_to_le16(dur);
226}
227EXPORT_SYMBOL(ieee80211_rts_duration);
228
Johannes Berg32bfd352007-12-19 01:31:26 +0100229__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
230 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200231 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200232 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200233{
234 struct ieee80211_local *local = hw_to_local(hw);
235 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200236 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100237 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200238 int erp;
239 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200240 struct ieee80211_supported_band *sband;
241
242 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200243
Johannes Berg25d834e2008-09-12 22:52:47 +0200244 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200245
Johannes Berge039fa42008-05-15 12:55:29 +0200246 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100247 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200248 if (vif) {
249 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200250 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200251 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
252 erp = rate->flags & IEEE80211_RATE_ERP_G;
253 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200254
255 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100256 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200257 erp, short_preamble);
Johannes Berge039fa42008-05-15 12:55:29 +0200258 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200259 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100260 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200261 erp, short_preamble);
262 }
263
Johannes Bergc2d15602007-07-27 15:43:23 +0200264 return cpu_to_le16(dur);
265}
266EXPORT_SYMBOL(ieee80211_ctstoself_duration);
267
Kalle Valoce7c9112008-12-18 23:35:20 +0200268static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
269 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200270{
271 struct ieee80211_local *local = hw_to_local(hw);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100272 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200273
Johannes Berge4e72fb2009-03-23 17:28:42 +0100274 if (WARN_ON(queue >= hw->queues))
275 return;
Kalle Valoce7c9112008-12-18 23:35:20 +0200276
Johannes Berg96f5e662009-02-12 00:51:53 +0100277 __clear_bit(reason, &local->queue_stop_reasons[queue]);
278
279 if (local->queue_stop_reasons[queue] != 0)
280 /* someone still has this queue stopped */
281 return;
282
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200283 if (!skb_queue_empty(&local->pending[queue]))
284 tasklet_schedule(&local->tx_pending_tasklet);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100285
286 rcu_read_lock();
287 list_for_each_entry_rcu(sdata, &local->interfaces, list)
288 netif_tx_wake_queue(netdev_get_tx_queue(sdata->dev, queue));
289 rcu_read_unlock();
Johannes Bergc2d15602007-07-27 15:43:23 +0200290}
Kalle Valoce7c9112008-12-18 23:35:20 +0200291
Johannes Berg96f5e662009-02-12 00:51:53 +0100292void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
293 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200294{
295 struct ieee80211_local *local = hw_to_local(hw);
296 unsigned long flags;
297
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);
301}
302
303void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
304{
305 ieee80211_wake_queue_by_reason(hw, queue,
306 IEEE80211_QUEUE_STOP_REASON_DRIVER);
307}
Johannes Bergc2d15602007-07-27 15:43:23 +0200308EXPORT_SYMBOL(ieee80211_wake_queue);
309
Kalle Valoce7c9112008-12-18 23:35:20 +0200310static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
311 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200312{
313 struct ieee80211_local *local = hw_to_local(hw);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100314 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200315
Johannes Berge4e72fb2009-03-23 17:28:42 +0100316 if (WARN_ON(queue >= hw->queues))
317 return;
Johannes Berg96f5e662009-02-12 00:51:53 +0100318
Johannes Berg2a577d92009-03-23 17:28:37 +0100319 __set_bit(reason, &local->queue_stop_reasons[queue]);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100320
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();
Johannes Bergc2d15602007-07-27 15:43:23 +0200325}
Kalle Valoce7c9112008-12-18 23:35:20 +0200326
Johannes Berg96f5e662009-02-12 00:51:53 +0100327void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
328 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200329{
330 struct ieee80211_local *local = hw_to_local(hw);
331 unsigned long flags;
332
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);
336}
337
338void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
339{
340 ieee80211_stop_queue_by_reason(hw, queue,
341 IEEE80211_QUEUE_STOP_REASON_DRIVER);
342}
Johannes Bergc2d15602007-07-27 15:43:23 +0200343EXPORT_SYMBOL(ieee80211_stop_queue);
344
Johannes Berg8f77f382009-06-07 21:58:37 +0200345void ieee80211_add_pending_skb(struct ieee80211_local *local,
346 struct sk_buff *skb)
347{
348 struct ieee80211_hw *hw = &local->hw;
349 unsigned long flags;
350 int queue = skb_get_queue_mapping(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200351 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
352
353 if (WARN_ON(!info->control.vif)) {
Roel Kluin08196632009-10-06 15:52:35 +0200354 kfree_skb(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200355 return;
356 }
Johannes Berg8f77f382009-06-07 21:58:37 +0200357
358 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
359 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200360 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200361 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
362 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
363}
364
365int ieee80211_add_pending_skbs(struct ieee80211_local *local,
366 struct sk_buff_head *skbs)
367{
368 struct ieee80211_hw *hw = &local->hw;
369 struct sk_buff *skb;
370 unsigned long flags;
371 int queue, ret = 0, i;
372
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);
377
378 while ((skb = skb_dequeue(skbs))) {
Johannes Berga7bc3762009-07-27 10:33:31 +0200379 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
380
381 if (WARN_ON(!info->control.vif)) {
Roel Kluin08196632009-10-06 15:52:35 +0200382 kfree_skb(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200383 continue;
384 }
385
Johannes Berg8f77f382009-06-07 21:58:37 +0200386 ret++;
387 queue = skb_get_queue_mapping(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200388 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200389 }
390
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200391 for (i = 0; i < hw->queues; i++)
Johannes Berg8f77f382009-06-07 21:58:37 +0200392 __ieee80211_wake_queue(hw, i,
393 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
Johannes Berg8f77f382009-06-07 21:58:37 +0200394 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
395
396 return ret;
397}
398
Kalle Valoce7c9112008-12-18 23:35:20 +0200399void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
400 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200401{
Kalle Valoce7c9112008-12-18 23:35:20 +0200402 struct ieee80211_local *local = hw_to_local(hw);
403 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200404 int i;
405
Kalle Valoce7c9112008-12-18 23:35:20 +0200406 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
407
Johannes Berg96f5e662009-02-12 00:51:53 +0100408 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200409 __ieee80211_stop_queue(hw, i, reason);
410
411 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
412}
413
414void ieee80211_stop_queues(struct ieee80211_hw *hw)
415{
416 ieee80211_stop_queues_by_reason(hw,
417 IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200418}
419EXPORT_SYMBOL(ieee80211_stop_queues);
420
Tomas Winkler92ab8532008-07-24 21:02:04 +0300421int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
422{
423 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200424 unsigned long flags;
425 int ret;
Johannes Berg96f5e662009-02-12 00:51:53 +0100426
Johannes Berge4e72fb2009-03-23 17:28:42 +0100427 if (WARN_ON(queue >= hw->queues))
428 return true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100429
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200430 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;
Tomas Winkler92ab8532008-07-24 21:02:04 +0300434}
435EXPORT_SYMBOL(ieee80211_queue_stopped);
436
Kalle Valoce7c9112008-12-18 23:35:20 +0200437void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
438 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200439{
Kalle Valoce7c9112008-12-18 23:35:20 +0200440 struct ieee80211_local *local = hw_to_local(hw);
441 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200442 int i;
443
Kalle Valoce7c9112008-12-18 23:35:20 +0200444 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
445
Johannes Berge4e72fb2009-03-23 17:28:42 +0100446 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200447 __ieee80211_wake_queue(hw, i, reason);
448
449 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
450}
451
452void ieee80211_wake_queues(struct ieee80211_hw *hw)
453{
454 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200455}
456EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100457
Johannes Berg32bfd352007-12-19 01:31:26 +0100458void ieee80211_iterate_active_interfaces(
459 struct ieee80211_hw *hw,
460 void (*iterator)(void *data, u8 *mac,
461 struct ieee80211_vif *vif),
462 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100463{
464 struct ieee80211_local *local = hw_to_local(hw);
465 struct ieee80211_sub_if_data *sdata;
466
Johannes Bergc771c9d2009-01-23 22:54:03 +0100467 mutex_lock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200468
469 list_for_each_entry(sdata, &local->interfaces, list) {
470 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200471 case __NL80211_IFTYPE_AFTER_LAST:
472 case NL80211_IFTYPE_UNSPECIFIED:
473 case NL80211_IFTYPE_MONITOR:
474 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200475 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200476 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:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200481 break;
482 }
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200483 if (netif_running(sdata->dev))
484 iterator(data, sdata->dev->dev_addr,
485 &sdata->vif);
486 }
487
Johannes Bergc771c9d2009-01-23 22:54:03 +0100488 mutex_unlock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200489}
490EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
491
492void 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)
497{
498 struct ieee80211_local *local = hw_to_local(hw);
499 struct ieee80211_sub_if_data *sdata;
500
Johannes Berge38bad42007-11-28 10:55:32 +0100501 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100502
Johannes Berge38bad42007-11-28 10:55:32 +0100503 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100504 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200505 case __NL80211_IFTYPE_AFTER_LAST:
506 case NL80211_IFTYPE_UNSPECIFIED:
507 case NL80211_IFTYPE_MONITOR:
508 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100509 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200510 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:
Johannes Bergdabeb342007-11-09 01:57:29 +0100515 break;
516 }
Johannes Bergdabeb342007-11-09 01:57:29 +0100517 if (netif_running(sdata->dev))
518 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100519 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100520 }
Johannes Berge38bad42007-11-28 10:55:32 +0100521
522 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100523}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200524EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200525
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400526/*
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.
532 */
533static bool ieee80211_can_queue_work(struct ieee80211_local *local)
534{
Johannes Bergceb99fe2009-11-19 14:29:39 +0100535 if (WARN(local->suspended && !local->resuming,
536 "queueing ieee80211 work while going to suspend\n"))
537 return false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400538
539 return true;
540}
541
542void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
543{
544 struct ieee80211_local *local = hw_to_local(hw);
545
546 if (!ieee80211_can_queue_work(local))
547 return;
548
549 queue_work(local->workqueue, work);
550}
551EXPORT_SYMBOL(ieee80211_queue_work);
552
553void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
554 struct delayed_work *dwork,
555 unsigned long delay)
556{
557 struct ieee80211_local *local = hw_to_local(hw);
558
559 if (!ieee80211_can_queue_work(local))
560 return;
561
562 queue_delayed_work(local->workqueue, dwork, delay);
563}
564EXPORT_SYMBOL(ieee80211_queue_delayed_work);
565
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200566void ieee802_11_parse_elems(u8 *start, size_t len,
567 struct ieee802_11_elems *elems)
568{
Johannes Bergd91f36d2009-04-16 13:17:26 +0200569 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
570}
571
572u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
573 struct ieee802_11_elems *elems,
574 u64 filter, u32 crc)
575{
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200576 size_t left = len;
577 u8 *pos = start;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200578 bool calc_crc = filter != 0;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200579
580 memset(elems, 0, sizeof(*elems));
581 elems->ie_start = start;
582 elems->total_len = len;
583
584 while (left >= 2) {
585 u8 id, elen;
586
587 id = *pos++;
588 elen = *pos++;
589 left -= 2;
590
591 if (elen > left)
Johannes Bergd91f36d2009-04-16 13:17:26 +0200592 break;
593
Vasanthakumar Thiagarajan18140772009-12-04 17:41:34 +0530594 if (calc_crc && id < 64 && (filter & (1ULL << id)))
Johannes Bergd91f36d2009-04-16 13:17:26 +0200595 crc = crc32_be(crc, pos - 2, elen + 2);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200596
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:
Johannes Berge7ec86f2009-04-18 17:33:24 +0200619 if (elen >= sizeof(struct ieee80211_tim_ie)) {
620 elems->tim = (void *)pos;
621 elems->tim_len = elen;
622 }
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200623 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;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200632 case WLAN_EID_VENDOR_SPECIFIC:
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200633 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
634 pos[2] == 0xf2) {
635 /* Microsoft OUI (00:50:F2) */
Johannes Bergd91f36d2009-04-16 13:17:26 +0200636
637 if (calc_crc)
638 crc = crc32_be(crc, pos - 2, elen + 2);
639
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200640 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) {
Johannes Bergd91f36d2009-04-16 13:17:26 +0200645 /* OUI Type 2 - WMM IE */
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200646 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;
652 }
653 }
654 }
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:
Johannes Berg09914812008-10-07 19:31:17 +0200669 if (elen >= sizeof(struct ieee80211_ht_cap))
670 elems->ht_cap_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200671 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200672 case WLAN_EID_HT_INFORMATION:
673 if (elen >= sizeof(struct ieee80211_ht_info))
Johannes Berg09914812008-10-07 19:31:17 +0200674 elems->ht_info_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200675 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:
Rui Paulo136cfa22009-11-18 18:40:00 +0000681 if (elen >= sizeof(struct ieee80211_meshconf_ie))
682 elems->mesh_config = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200683 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;
Rui Paulo90a5e162009-11-11 00:01:31 +0000700 case WLAN_EID_RANN:
701 if (elen >= sizeof(struct ieee80211_rann_ie))
702 elems->rann = (void *)pos;
703 break;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200704 case WLAN_EID_CHANNEL_SWITCH:
705 elems->ch_switch_elem = pos;
706 elems->ch_switch_elem_len = elen;
707 break;
708 case WLAN_EID_QUIET:
709 if (!elems->quiet_elem) {
710 elems->quiet_elem = pos;
711 elems->quiet_elem_len = elen;
712 }
713 elems->num_of_quiet_elem++;
714 break;
715 case WLAN_EID_COUNTRY:
716 elems->country_elem = pos;
717 elems->country_elem_len = elen;
718 break;
719 case WLAN_EID_PWR_CONSTRAINT:
720 elems->pwr_constr_elem = pos;
721 elems->pwr_constr_elem_len = elen;
722 break;
Jouni Malinenf797eb72009-01-19 18:48:46 +0200723 case WLAN_EID_TIMEOUT_INTERVAL:
724 elems->timeout_int = pos;
725 elems->timeout_int_len = elen;
Jouni Malinen63a5ab82009-01-08 13:32:09 +0200726 break;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200727 default:
728 break;
729 }
730
731 left -= elen;
732 pos += elen;
733 }
Johannes Bergd91f36d2009-04-16 13:17:26 +0200734
735 return crc;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200736}
Johannes Berg5825fe102008-09-09 12:56:01 +0200737
738void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
739{
740 struct ieee80211_local *local = sdata->local;
741 struct ieee80211_tx_queue_params qparam;
Johannes Bergaa837e12009-05-07 16:16:24 +0200742 int queue;
743 bool use_11b;
744 int aCWmin, aCWmax;
Johannes Berg5825fe102008-09-09 12:56:01 +0200745
746 if (!local->ops->conf_tx)
747 return;
748
749 memset(&qparam, 0, sizeof(qparam));
750
Johannes Bergaa837e12009-05-07 16:16:24 +0200751 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
752 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
Johannes Berg5825fe102008-09-09 12:56:01 +0200753
Johannes Bergaa837e12009-05-07 16:16:24 +0200754 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
755 /* Set defaults according to 802.11-2007 Table 7-37 */
756 aCWmax = 1023;
757 if (use_11b)
758 aCWmin = 31;
759 else
760 aCWmin = 15;
Johannes Berg5825fe102008-09-09 12:56:01 +0200761
Johannes Bergaa837e12009-05-07 16:16:24 +0200762 switch (queue) {
763 case 3: /* AC_BK */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200764 qparam.cw_max = aCWmax;
765 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200766 qparam.txop = 0;
767 qparam.aifs = 7;
768 break;
769 default: /* never happens but let's not leave undefined */
770 case 2: /* AC_BE */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200771 qparam.cw_max = aCWmax;
772 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200773 qparam.txop = 0;
774 qparam.aifs = 3;
775 break;
776 case 1: /* AC_VI */
777 qparam.cw_max = aCWmin;
778 qparam.cw_min = (aCWmin + 1) / 2 - 1;
779 if (use_11b)
780 qparam.txop = 6016/32;
781 else
782 qparam.txop = 3008/32;
783 qparam.aifs = 2;
784 break;
785 case 0: /* AC_VO */
786 qparam.cw_max = (aCWmin + 1) / 2 - 1;
787 qparam.cw_min = (aCWmin + 1) / 4 - 1;
788 if (use_11b)
789 qparam.txop = 3264/32;
790 else
791 qparam.txop = 1504/32;
792 qparam.aifs = 2;
793 break;
794 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200795
Johannes Bergaa837e12009-05-07 16:16:24 +0200796 drv_conf_tx(local, queue, &qparam);
797 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200798}
Johannes Berge50db652008-09-09 15:07:09 +0200799
Johannes Berg46900292009-02-15 12:44:28 +0100800void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
801 const size_t supp_rates_len,
802 const u8 *supp_rates)
803{
804 struct ieee80211_local *local = sdata->local;
805 int i, have_higher_than_11mbit = 0;
806
807 /* cf. IEEE 802.11 9.2.12 */
808 for (i = 0; i < supp_rates_len; i++)
809 if ((supp_rates[i] & 0x7f) * 5 > 110)
810 have_higher_than_11mbit = 1;
811
812 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
813 have_higher_than_11mbit)
814 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
815 else
816 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
817
818 ieee80211_set_wmm_default(sdata);
819}
820
Johannes Berg881d9482009-01-21 15:13:48 +0100821u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
Johannes Berg96dd22a2008-09-11 00:01:57 +0200822 enum ieee80211_band band)
823{
824 struct ieee80211_supported_band *sband;
825 struct ieee80211_rate *bitrates;
Johannes Berg881d9482009-01-21 15:13:48 +0100826 u32 mandatory_rates;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200827 enum ieee80211_rate_flags mandatory_flag;
828 int i;
829
830 sband = local->hw.wiphy->bands[band];
831 if (!sband) {
832 WARN_ON(1);
833 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
834 }
835
836 if (band == IEEE80211_BAND_2GHZ)
837 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
838 else
839 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
840
841 bitrates = sband->bitrates;
842 mandatory_rates = 0;
843 for (i = 0; i < sband->n_bitrates; i++)
844 if (bitrates[i].flags & mandatory_flag)
845 mandatory_rates |= BIT(i);
846 return mandatory_rates;
847}
Johannes Berg46900292009-02-15 12:44:28 +0100848
849void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
850 u16 transaction, u16 auth_alg,
Johannes Bergfffd0932009-07-08 14:22:54 +0200851 u8 *extra, size_t extra_len, const u8 *bssid,
852 const u8 *key, u8 key_len, u8 key_idx)
Johannes Berg46900292009-02-15 12:44:28 +0100853{
854 struct ieee80211_local *local = sdata->local;
855 struct sk_buff *skb;
856 struct ieee80211_mgmt *mgmt;
Johannes Bergfffd0932009-07-08 14:22:54 +0200857 int err;
Johannes Berg46900292009-02-15 12:44:28 +0100858
859 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200860 sizeof(*mgmt) + 6 + extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100861 if (!skb) {
862 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
863 "frame\n", sdata->dev->name);
864 return;
865 }
866 skb_reserve(skb, local->hw.extra_tx_headroom);
867
868 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
869 memset(mgmt, 0, 24 + 6);
870 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
871 IEEE80211_STYPE_AUTH);
Johannes Berg46900292009-02-15 12:44:28 +0100872 memcpy(mgmt->da, bssid, ETH_ALEN);
873 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
874 memcpy(mgmt->bssid, bssid, ETH_ALEN);
875 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
876 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
877 mgmt->u.auth.status_code = cpu_to_le16(0);
878 if (extra)
879 memcpy(skb_put(skb, extra_len), extra, extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100880
Johannes Bergfffd0932009-07-08 14:22:54 +0200881 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
882 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
883 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
884 WARN_ON(err);
885 }
886
Johannes Berg62ae67b2009-11-18 18:42:05 +0100887 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
888 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100889}
890
Johannes Bergde95a542009-04-01 11:58:36 +0200891int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
Johannes Berg4d36ec582009-10-27 20:59:55 +0100892 const u8 *ie, size_t ie_len,
893 enum ieee80211_band band)
Johannes Bergde95a542009-04-01 11:58:36 +0200894{
895 struct ieee80211_supported_band *sband;
896 u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
897 int i;
898
Johannes Berg4d36ec582009-10-27 20:59:55 +0100899 sband = local->hw.wiphy->bands[band];
Johannes Bergde95a542009-04-01 11:58:36 +0200900
901 pos = buffer;
902
903 *pos++ = WLAN_EID_SUPP_RATES;
904 supp_rates_len = pos;
905 *pos++ = 0;
906
907 for (i = 0; i < sband->n_bitrates; i++) {
908 struct ieee80211_rate *rate = &sband->bitrates[i];
909
910 if (esupp_rates_len) {
911 *esupp_rates_len += 1;
912 } else if (*supp_rates_len == 8) {
913 *pos++ = WLAN_EID_EXT_SUPP_RATES;
914 esupp_rates_len = pos;
915 *pos++ = 1;
916 } else
917 *supp_rates_len += 1;
918
919 *pos++ = rate->bitrate / 5;
920 }
921
Johannes Berg5ef2d412009-03-31 12:12:07 +0200922 if (sband->ht_cap.ht_supported) {
923 __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
924
925 *pos++ = WLAN_EID_HT_CAPABILITY;
926 *pos++ = sizeof(struct ieee80211_ht_cap);
927 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
928 memcpy(pos, &tmp, sizeof(u16));
929 pos += sizeof(u16);
930 /* TODO: needs a define here for << 2 */
931 *pos++ = sband->ht_cap.ampdu_factor |
932 (sband->ht_cap.ampdu_density << 2);
933 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
934 pos += sizeof(sband->ht_cap.mcs);
935 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
936 }
937
Johannes Bergde95a542009-04-01 11:58:36 +0200938 /*
939 * If adding more here, adjust code in main.c
940 * that calculates local->scan_ies_len.
941 */
942
943 if (ie) {
944 memcpy(pos, ie, ie_len);
945 pos += ie_len;
946 }
947
948 return pos - buffer;
949}
950
Johannes Berg46900292009-02-15 12:44:28 +0100951void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
Johannes Bergde95a542009-04-01 11:58:36 +0200952 const u8 *ssid, size_t ssid_len,
953 const u8 *ie, size_t ie_len)
Johannes Berg46900292009-02-15 12:44:28 +0100954{
955 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100956 struct sk_buff *skb;
957 struct ieee80211_mgmt *mgmt;
Johannes Bergde95a542009-04-01 11:58:36 +0200958 u8 *pos;
Johannes Berg46900292009-02-15 12:44:28 +0100959
960 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200961 ie_len);
Johannes Berg46900292009-02-15 12:44:28 +0100962 if (!skb) {
963 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
964 "request\n", sdata->dev->name);
965 return;
966 }
967 skb_reserve(skb, local->hw.extra_tx_headroom);
968
969 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
970 memset(mgmt, 0, 24);
971 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
972 IEEE80211_STYPE_PROBE_REQ);
973 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
974 if (dst) {
975 memcpy(mgmt->da, dst, ETH_ALEN);
976 memcpy(mgmt->bssid, dst, ETH_ALEN);
977 } else {
978 memset(mgmt->da, 0xff, ETH_ALEN);
979 memset(mgmt->bssid, 0xff, ETH_ALEN);
980 }
981 pos = skb_put(skb, 2 + ssid_len);
982 *pos++ = WLAN_EID_SSID;
983 *pos++ = ssid_len;
984 memcpy(pos, ssid, ssid_len);
Johannes Bergde95a542009-04-01 11:58:36 +0200985 pos += ssid_len;
Johannes Berg46900292009-02-15 12:44:28 +0100986
Johannes Berg4d36ec582009-10-27 20:59:55 +0100987 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len,
988 local->hw.conf.channel->band));
Johannes Berg46900292009-02-15 12:44:28 +0100989
Johannes Berg62ae67b2009-11-18 18:42:05 +0100990 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
991 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100992}
993
994u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
995 struct ieee802_11_elems *elems,
996 enum ieee80211_band band)
997{
998 struct ieee80211_supported_band *sband;
999 struct ieee80211_rate *bitrates;
1000 size_t num_rates;
1001 u32 supp_rates;
1002 int i, j;
1003 sband = local->hw.wiphy->bands[band];
1004
1005 if (!sband) {
1006 WARN_ON(1);
1007 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1008 }
1009
1010 bitrates = sband->bitrates;
1011 num_rates = sband->n_bitrates;
1012 supp_rates = 0;
1013 for (i = 0; i < elems->supp_rates_len +
1014 elems->ext_supp_rates_len; i++) {
1015 u8 rate = 0;
1016 int own_rate;
1017 if (i < elems->supp_rates_len)
1018 rate = elems->supp_rates[i];
1019 else if (elems->ext_supp_rates)
1020 rate = elems->ext_supp_rates
1021 [i - elems->supp_rates_len];
1022 own_rate = 5 * (rate & 0x7f);
1023 for (j = 0; j < num_rates; j++)
1024 if (bitrates[j].bitrate == own_rate)
1025 supp_rates |= BIT(j);
1026 }
1027 return supp_rates;
1028}
Johannes Bergf2753dd2009-04-14 10:09:24 +02001029
Johannes Berg84f6a012009-08-20 20:02:20 +02001030void ieee80211_stop_device(struct ieee80211_local *local)
1031{
1032 ieee80211_led_radio(local, false);
1033
1034 cancel_work_sync(&local->reconfig_filter);
1035 drv_stop(local);
1036
1037 flush_workqueue(local->workqueue);
1038}
1039
Johannes Bergf2753dd2009-04-14 10:09:24 +02001040int ieee80211_reconfig(struct ieee80211_local *local)
1041{
1042 struct ieee80211_hw *hw = &local->hw;
1043 struct ieee80211_sub_if_data *sdata;
1044 struct ieee80211_if_init_conf conf;
1045 struct sta_info *sta;
1046 unsigned long flags;
1047 int res;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001048
Johannes Bergceb99fe2009-11-19 14:29:39 +01001049 if (local->suspended)
1050 local->resuming = true;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001051
1052 /* restart hardware */
1053 if (local->open_count) {
Luis R. Rodriguez24feda02009-12-24 15:38:22 -05001054 /*
1055 * Upon resume hardware can sometimes be goofy due to
1056 * various platform / driver / bus issues, so restarting
1057 * the device may at times not work immediately. Propagate
1058 * the error.
1059 */
Johannes Berg24487982009-04-23 18:52:52 +02001060 res = drv_start(local);
Luis R. Rodriguez24feda02009-12-24 15:38:22 -05001061 if (res) {
1062 WARN(local->suspended, "Harware became unavailable "
1063 "upon resume. This is could be a software issue"
1064 "prior to suspend or a harware issue\n");
1065 return res;
1066 }
Johannes Bergf2753dd2009-04-14 10:09:24 +02001067
Johannes Berg1f87f7d2009-06-02 13:01:41 +02001068 ieee80211_led_radio(local, true);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001069 }
1070
1071 /* add interfaces */
1072 list_for_each_entry(sdata, &local->interfaces, list) {
1073 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1074 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1075 netif_running(sdata->dev)) {
1076 conf.vif = &sdata->vif;
1077 conf.type = sdata->vif.type;
1078 conf.mac_addr = sdata->dev->dev_addr;
Johannes Berg24487982009-04-23 18:52:52 +02001079 res = drv_add_interface(local, &conf);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001080 }
1081 }
1082
1083 /* add STAs back */
1084 if (local->ops->sta_notify) {
1085 spin_lock_irqsave(&local->sta_lock, flags);
1086 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg5bb644a2009-05-17 11:40:42 +02001087 sdata = sta->sdata;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001088 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1089 sdata = container_of(sdata->bss,
1090 struct ieee80211_sub_if_data,
1091 u.ap);
1092
Johannes Berg24487982009-04-23 18:52:52 +02001093 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
1094 &sta->sta);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001095 }
1096 spin_unlock_irqrestore(&local->sta_lock, flags);
1097 }
1098
1099 /* Clear Suspend state so that ADDBA requests can be processed */
1100
1101 rcu_read_lock();
1102
1103 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1104 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1105 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1106 }
1107 }
1108
1109 rcu_read_unlock();
1110
1111 /* setup RTS threshold */
Johannes Berg24487982009-04-23 18:52:52 +02001112 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001113
1114 /* reconfigure hardware */
1115 ieee80211_hw_config(local, ~0);
1116
Johannes Bergf2753dd2009-04-14 10:09:24 +02001117 ieee80211_configure_filter(local);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001118
1119 /* Finally also reconfigure all the BSS information */
1120 list_for_each_entry(sdata, &local->interfaces, list) {
1121 u32 changed = ~0;
1122 if (!netif_running(sdata->dev))
1123 continue;
1124 switch (sdata->vif.type) {
1125 case NL80211_IFTYPE_STATION:
1126 /* disable beacon change bits */
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001127 changed &= ~(BSS_CHANGED_BEACON |
1128 BSS_CHANGED_BEACON_ENABLED);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001129 /* fall through */
1130 case NL80211_IFTYPE_ADHOC:
1131 case NL80211_IFTYPE_AP:
1132 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001133 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001134 break;
1135 case NL80211_IFTYPE_WDS:
1136 break;
1137 case NL80211_IFTYPE_AP_VLAN:
1138 case NL80211_IFTYPE_MONITOR:
1139 /* ignore virtual */
1140 break;
1141 case NL80211_IFTYPE_UNSPECIFIED:
1142 case __NL80211_IFTYPE_AFTER_LAST:
1143 WARN_ON(1);
1144 break;
1145 }
1146 }
1147
1148 /* add back keys */
1149 list_for_each_entry(sdata, &local->interfaces, list)
1150 if (netif_running(sdata->dev))
1151 ieee80211_enable_keys(sdata);
1152
1153 ieee80211_wake_queues_by_reason(hw,
1154 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1155
Johannes Berg5bb644a2009-05-17 11:40:42 +02001156 /*
1157 * If this is for hw restart things are still running.
1158 * We may want to change that later, however.
1159 */
Johannes Bergceb99fe2009-11-19 14:29:39 +01001160 if (!local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001161 return 0;
1162
1163#ifdef CONFIG_PM
Johannes Bergceb99fe2009-11-19 14:29:39 +01001164 /* first set suspended false, then resuming */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001165 local->suspended = false;
Johannes Bergceb99fe2009-11-19 14:29:39 +01001166 mb();
1167 local->resuming = false;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001168
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;
1182 }
1183 }
1184
1185 add_timer(&local->sta_cleanup);
1186
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
Johannes Bergf2753dd2009-04-14 10:09:24 +02001194 return 0;
1195}
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001196