blob: 31284c984e38538b2b5b2c1592127313ee78def2 [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 Bergc2d15602007-07-27 15:43:23 +020034
35/* privid for wiphys to determine whether they belong to us or not */
36void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
37
Luis R. Rodriguez9a953712009-01-22 15:05:53 -080038struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
39{
40 struct ieee80211_local *local;
41 BUG_ON(!wiphy);
42
43 local = wiphy_priv(wiphy);
44 return &local->hw;
45}
46EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
Johannes Bergc2d15602007-07-27 15:43:23 +020047
Ron Rindjunsky71364712007-12-25 17:00:36 +020048u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020049 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020050{
Harvey Harrisona494bb12008-06-11 14:21:58 -070051 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020052
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020053 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
54 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020055 return NULL;
56
Harvey Harrisona494bb12008-06-11 14:21:58 -070057 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020058 if (len < 24) /* drop incorrect hdr len (data) */
59 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070060
61 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020062 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070063 if (ieee80211_has_tods(fc))
64 return hdr->addr1;
65 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020066 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070067
68 return hdr->addr3;
69 }
70
71 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020072 if (len < 24) /* drop incorrect hdr len (mgmt) */
73 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020074 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070075 }
76
77 if (ieee80211_is_ctl(fc)) {
78 if(ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020079 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070080
81 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020082 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020083 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020084 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020085 case NL80211_IFTYPE_AP:
86 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020087 return hdr->addr1;
88 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070089 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020090 }
91 }
Johannes Bergc2d15602007-07-27 15:43:23 +020092 }
93
94 return NULL;
95}
96
Johannes Berg5cf121c2008-02-25 16:27:43 +010097void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +020098{
Johannes Berg2de8e0d2009-03-23 17:28:35 +010099 struct sk_buff *skb = tx->skb;
100 struct ieee80211_hdr *hdr;
Johannes Bergc2d15602007-07-27 15:43:23 +0200101
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100102 do {
103 hdr = (struct ieee80211_hdr *) skb->data;
104 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
105 } while ((skb = skb->next));
Johannes Bergc2d15602007-07-27 15:43:23 +0200106}
107
108int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
109 int rate, int erp, int short_preamble)
110{
111 int dur;
112
113 /* calculate duration (in microseconds, rounded up to next higher
114 * integer if it includes a fractional microsecond) to send frame of
115 * len bytes (does not include FCS) at the given rate. Duration will
116 * also include SIFS.
117 *
118 * rate is in 100 kbps, so divident is multiplied by 10 in the
119 * DIV_ROUND_UP() operations.
120 */
121
Johannes Berg8318d782008-01-24 19:38:38 +0100122 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200123 /*
124 * OFDM:
125 *
126 * N_DBPS = DATARATE x 4
127 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
128 * (16 = SIGNAL time, 6 = tail bits)
129 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
130 *
131 * T_SYM = 4 usec
132 * 802.11a - 17.5.2: aSIFSTime = 16 usec
133 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
134 * signal ext = 6 usec
135 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200136 dur = 16; /* SIFS + signal ext */
137 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
138 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
139 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
140 4 * rate); /* T_SYM x N_SYM */
141 } else {
142 /*
143 * 802.11b or 802.11g with 802.11b compatibility:
144 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
145 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
146 *
147 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
148 * aSIFSTime = 10 usec
149 * aPreambleLength = 144 usec or 72 usec with short preamble
150 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
151 */
152 dur = 10; /* aSIFSTime = 10 usec */
153 dur += short_preamble ? (72 + 24) : (144 + 48);
154
155 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
156 }
157
158 return dur;
159}
160
161/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100162__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
163 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100164 size_t frame_len,
165 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200166{
167 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg25d834e2008-09-12 22:52:47 +0200168 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200169 u16 dur;
170 int erp;
Johannes Berg25d834e2008-09-12 22:52:47 +0200171 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200172
Johannes Berg8318d782008-01-24 19:38:38 +0100173 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200174 if (vif) {
175 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200176 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200177 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
178 erp = rate->flags & IEEE80211_RATE_ERP_G;
179 }
Johannes Berg8318d782008-01-24 19:38:38 +0100180
181 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg25d834e2008-09-12 22:52:47 +0200182 short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200183
184 return cpu_to_le16(dur);
185}
186EXPORT_SYMBOL(ieee80211_generic_frame_duration);
187
Johannes Berg32bfd352007-12-19 01:31:26 +0100188__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
189 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200190 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200191{
192 struct ieee80211_local *local = hw_to_local(hw);
193 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200194 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100195 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200196 int erp;
197 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200198 struct ieee80211_supported_band *sband;
199
200 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200201
Johannes Berg25d834e2008-09-12 22:52:47 +0200202 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200203
Johannes Berge039fa42008-05-15 12:55:29 +0200204 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100205
206 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200207 if (vif) {
208 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200209 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200210 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
211 erp = rate->flags & IEEE80211_RATE_ERP_G;
212 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200213
214 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100215 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200216 erp, short_preamble);
217 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100218 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200219 erp, short_preamble);
220 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100221 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200222 erp, short_preamble);
223
224 return cpu_to_le16(dur);
225}
226EXPORT_SYMBOL(ieee80211_rts_duration);
227
Johannes Berg32bfd352007-12-19 01:31:26 +0100228__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
229 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200230 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200231 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200232{
233 struct ieee80211_local *local = hw_to_local(hw);
234 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200235 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100236 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200237 int erp;
238 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200239 struct ieee80211_supported_band *sband;
240
241 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200242
Johannes Berg25d834e2008-09-12 22:52:47 +0200243 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200244
Johannes Berge039fa42008-05-15 12:55:29 +0200245 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100246 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200247 if (vif) {
248 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200249 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200250 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
251 erp = rate->flags & IEEE80211_RATE_ERP_G;
252 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200253
254 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100255 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200256 erp, short_preamble);
Johannes Berge039fa42008-05-15 12:55:29 +0200257 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200258 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100259 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200260 erp, short_preamble);
261 }
262
Johannes Bergc2d15602007-07-27 15:43:23 +0200263 return cpu_to_le16(dur);
264}
265EXPORT_SYMBOL(ieee80211_ctstoself_duration);
266
Kalle Valoce7c9112008-12-18 23:35:20 +0200267static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
268 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200269{
270 struct ieee80211_local *local = hw_to_local(hw);
271
Johannes Berge4e72fb2009-03-23 17:28:42 +0100272 if (WARN_ON(queue >= hw->queues))
273 return;
Kalle Valoce7c9112008-12-18 23:35:20 +0200274
Johannes Berg96f5e662009-02-12 00:51:53 +0100275 __clear_bit(reason, &local->queue_stop_reasons[queue]);
276
Johannes Berg2a577d92009-03-23 17:28:37 +0100277 if (!skb_queue_empty(&local->pending[queue]) &&
278 local->queue_stop_reasons[queue] ==
279 BIT(IEEE80211_QUEUE_STOP_REASON_PENDING))
280 tasklet_schedule(&local->tx_pending_tasklet);
281
Johannes Berg96f5e662009-02-12 00:51:53 +0100282 if (local->queue_stop_reasons[queue] != 0)
283 /* someone still has this queue stopped */
284 return;
285
Johannes Berg2a577d92009-03-23 17:28:37 +0100286 netif_wake_subqueue(local->mdev, queue);
Johannes Bergc2d15602007-07-27 15:43:23 +0200287}
Kalle Valoce7c9112008-12-18 23:35:20 +0200288
Johannes Berg96f5e662009-02-12 00:51:53 +0100289void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
290 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200291{
292 struct ieee80211_local *local = hw_to_local(hw);
293 unsigned long flags;
294
295 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
296 __ieee80211_wake_queue(hw, queue, reason);
297 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
298}
299
300void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
301{
302 ieee80211_wake_queue_by_reason(hw, queue,
303 IEEE80211_QUEUE_STOP_REASON_DRIVER);
304}
Johannes Bergc2d15602007-07-27 15:43:23 +0200305EXPORT_SYMBOL(ieee80211_wake_queue);
306
Kalle Valoce7c9112008-12-18 23:35:20 +0200307static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
308 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200309{
310 struct ieee80211_local *local = hw_to_local(hw);
311
Johannes Berge4e72fb2009-03-23 17:28:42 +0100312 if (WARN_ON(queue >= hw->queues))
313 return;
Johannes Berg96f5e662009-02-12 00:51:53 +0100314
Johannes Berg2a577d92009-03-23 17:28:37 +0100315 /*
316 * Only stop if it was previously running, this is necessary
317 * for correct pending packets handling because there we may
318 * start (but not wake) the queue and rely on that.
319 */
320 if (!local->queue_stop_reasons[queue])
321 netif_stop_subqueue(local->mdev, queue);
Kalle Valoce7c9112008-12-18 23:35:20 +0200322
Johannes Berg2a577d92009-03-23 17:28:37 +0100323 __set_bit(reason, &local->queue_stop_reasons[queue]);
Johannes Bergc2d15602007-07-27 15:43:23 +0200324}
Kalle Valoce7c9112008-12-18 23:35:20 +0200325
Johannes Berg96f5e662009-02-12 00:51:53 +0100326void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
327 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200328{
329 struct ieee80211_local *local = hw_to_local(hw);
330 unsigned long flags;
331
332 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
333 __ieee80211_stop_queue(hw, queue, reason);
334 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
335}
336
337void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
338{
339 ieee80211_stop_queue_by_reason(hw, queue,
340 IEEE80211_QUEUE_STOP_REASON_DRIVER);
341}
Johannes Bergc2d15602007-07-27 15:43:23 +0200342EXPORT_SYMBOL(ieee80211_stop_queue);
343
Kalle Valoce7c9112008-12-18 23:35:20 +0200344void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
345 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200346{
Kalle Valoce7c9112008-12-18 23:35:20 +0200347 struct ieee80211_local *local = hw_to_local(hw);
348 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200349 int i;
350
Kalle Valoce7c9112008-12-18 23:35:20 +0200351 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
352
Johannes Berg96f5e662009-02-12 00:51:53 +0100353 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200354 __ieee80211_stop_queue(hw, i, reason);
355
356 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
357}
358
359void ieee80211_stop_queues(struct ieee80211_hw *hw)
360{
361 ieee80211_stop_queues_by_reason(hw,
362 IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200363}
364EXPORT_SYMBOL(ieee80211_stop_queues);
365
Tomas Winkler92ab8532008-07-24 21:02:04 +0300366int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
367{
368 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg96f5e662009-02-12 00:51:53 +0100369
Johannes Berge4e72fb2009-03-23 17:28:42 +0100370 if (WARN_ON(queue >= hw->queues))
371 return true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100372
Tomas Winkler92ab8532008-07-24 21:02:04 +0300373 return __netif_subqueue_stopped(local->mdev, queue);
374}
375EXPORT_SYMBOL(ieee80211_queue_stopped);
376
Kalle Valoce7c9112008-12-18 23:35:20 +0200377void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
378 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200379{
Kalle Valoce7c9112008-12-18 23:35:20 +0200380 struct ieee80211_local *local = hw_to_local(hw);
381 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200382 int i;
383
Kalle Valoce7c9112008-12-18 23:35:20 +0200384 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
385
Johannes Berge4e72fb2009-03-23 17:28:42 +0100386 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200387 __ieee80211_wake_queue(hw, i, reason);
388
389 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
390}
391
392void ieee80211_wake_queues(struct ieee80211_hw *hw)
393{
394 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200395}
396EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100397
Johannes Berg32bfd352007-12-19 01:31:26 +0100398void ieee80211_iterate_active_interfaces(
399 struct ieee80211_hw *hw,
400 void (*iterator)(void *data, u8 *mac,
401 struct ieee80211_vif *vif),
402 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100403{
404 struct ieee80211_local *local = hw_to_local(hw);
405 struct ieee80211_sub_if_data *sdata;
406
Johannes Bergc771c9d2009-01-23 22:54:03 +0100407 mutex_lock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200408
409 list_for_each_entry(sdata, &local->interfaces, list) {
410 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200411 case __NL80211_IFTYPE_AFTER_LAST:
412 case NL80211_IFTYPE_UNSPECIFIED:
413 case NL80211_IFTYPE_MONITOR:
414 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200415 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200416 case NL80211_IFTYPE_AP:
417 case NL80211_IFTYPE_STATION:
418 case NL80211_IFTYPE_ADHOC:
419 case NL80211_IFTYPE_WDS:
420 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200421 break;
422 }
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200423 if (netif_running(sdata->dev))
424 iterator(data, sdata->dev->dev_addr,
425 &sdata->vif);
426 }
427
Johannes Bergc771c9d2009-01-23 22:54:03 +0100428 mutex_unlock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200429}
430EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
431
432void ieee80211_iterate_active_interfaces_atomic(
433 struct ieee80211_hw *hw,
434 void (*iterator)(void *data, u8 *mac,
435 struct ieee80211_vif *vif),
436 void *data)
437{
438 struct ieee80211_local *local = hw_to_local(hw);
439 struct ieee80211_sub_if_data *sdata;
440
Johannes Berge38bad42007-11-28 10:55:32 +0100441 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100442
Johannes Berge38bad42007-11-28 10:55:32 +0100443 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100444 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200445 case __NL80211_IFTYPE_AFTER_LAST:
446 case NL80211_IFTYPE_UNSPECIFIED:
447 case NL80211_IFTYPE_MONITOR:
448 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100449 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200450 case NL80211_IFTYPE_AP:
451 case NL80211_IFTYPE_STATION:
452 case NL80211_IFTYPE_ADHOC:
453 case NL80211_IFTYPE_WDS:
454 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergdabeb342007-11-09 01:57:29 +0100455 break;
456 }
Johannes Bergdabeb342007-11-09 01:57:29 +0100457 if (netif_running(sdata->dev))
458 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100459 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100460 }
Johannes Berge38bad42007-11-28 10:55:32 +0100461
462 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100463}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200464EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200465
466void ieee802_11_parse_elems(u8 *start, size_t len,
467 struct ieee802_11_elems *elems)
468{
Johannes Bergd91f36d2009-04-16 13:17:26 +0200469 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
470}
471
472u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
473 struct ieee802_11_elems *elems,
474 u64 filter, u32 crc)
475{
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200476 size_t left = len;
477 u8 *pos = start;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200478 bool calc_crc = filter != 0;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200479
480 memset(elems, 0, sizeof(*elems));
481 elems->ie_start = start;
482 elems->total_len = len;
483
484 while (left >= 2) {
485 u8 id, elen;
486
487 id = *pos++;
488 elen = *pos++;
489 left -= 2;
490
491 if (elen > left)
Johannes Bergd91f36d2009-04-16 13:17:26 +0200492 break;
493
494 if (calc_crc && id < 64 && (filter & BIT(id)))
495 crc = crc32_be(crc, pos - 2, elen + 2);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200496
497 switch (id) {
498 case WLAN_EID_SSID:
499 elems->ssid = pos;
500 elems->ssid_len = elen;
501 break;
502 case WLAN_EID_SUPP_RATES:
503 elems->supp_rates = pos;
504 elems->supp_rates_len = elen;
505 break;
506 case WLAN_EID_FH_PARAMS:
507 elems->fh_params = pos;
508 elems->fh_params_len = elen;
509 break;
510 case WLAN_EID_DS_PARAMS:
511 elems->ds_params = pos;
512 elems->ds_params_len = elen;
513 break;
514 case WLAN_EID_CF_PARAMS:
515 elems->cf_params = pos;
516 elems->cf_params_len = elen;
517 break;
518 case WLAN_EID_TIM:
Johannes Berge7ec86f2009-04-18 17:33:24 +0200519 if (elen >= sizeof(struct ieee80211_tim_ie)) {
520 elems->tim = (void *)pos;
521 elems->tim_len = elen;
522 }
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200523 break;
524 case WLAN_EID_IBSS_PARAMS:
525 elems->ibss_params = pos;
526 elems->ibss_params_len = elen;
527 break;
528 case WLAN_EID_CHALLENGE:
529 elems->challenge = pos;
530 elems->challenge_len = elen;
531 break;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200532 case WLAN_EID_VENDOR_SPECIFIC:
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200533 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
534 pos[2] == 0xf2) {
535 /* Microsoft OUI (00:50:F2) */
Johannes Bergd91f36d2009-04-16 13:17:26 +0200536
537 if (calc_crc)
538 crc = crc32_be(crc, pos - 2, elen + 2);
539
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200540 if (pos[3] == 1) {
541 /* OUI Type 1 - WPA IE */
542 elems->wpa = pos;
543 elems->wpa_len = elen;
544 } else if (elen >= 5 && pos[3] == 2) {
Johannes Bergd91f36d2009-04-16 13:17:26 +0200545 /* OUI Type 2 - WMM IE */
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200546 if (pos[4] == 0) {
547 elems->wmm_info = pos;
548 elems->wmm_info_len = elen;
549 } else if (pos[4] == 1) {
550 elems->wmm_param = pos;
551 elems->wmm_param_len = elen;
552 }
553 }
554 }
555 break;
556 case WLAN_EID_RSN:
557 elems->rsn = pos;
558 elems->rsn_len = elen;
559 break;
560 case WLAN_EID_ERP_INFO:
561 elems->erp_info = pos;
562 elems->erp_info_len = elen;
563 break;
564 case WLAN_EID_EXT_SUPP_RATES:
565 elems->ext_supp_rates = pos;
566 elems->ext_supp_rates_len = elen;
567 break;
568 case WLAN_EID_HT_CAPABILITY:
Johannes Berg09914812008-10-07 19:31:17 +0200569 if (elen >= sizeof(struct ieee80211_ht_cap))
570 elems->ht_cap_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200571 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200572 case WLAN_EID_HT_INFORMATION:
573 if (elen >= sizeof(struct ieee80211_ht_info))
Johannes Berg09914812008-10-07 19:31:17 +0200574 elems->ht_info_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200575 break;
576 case WLAN_EID_MESH_ID:
577 elems->mesh_id = pos;
578 elems->mesh_id_len = elen;
579 break;
580 case WLAN_EID_MESH_CONFIG:
581 elems->mesh_config = pos;
582 elems->mesh_config_len = elen;
583 break;
584 case WLAN_EID_PEER_LINK:
585 elems->peer_link = pos;
586 elems->peer_link_len = elen;
587 break;
588 case WLAN_EID_PREQ:
589 elems->preq = pos;
590 elems->preq_len = elen;
591 break;
592 case WLAN_EID_PREP:
593 elems->prep = pos;
594 elems->prep_len = elen;
595 break;
596 case WLAN_EID_PERR:
597 elems->perr = pos;
598 elems->perr_len = elen;
599 break;
600 case WLAN_EID_CHANNEL_SWITCH:
601 elems->ch_switch_elem = pos;
602 elems->ch_switch_elem_len = elen;
603 break;
604 case WLAN_EID_QUIET:
605 if (!elems->quiet_elem) {
606 elems->quiet_elem = pos;
607 elems->quiet_elem_len = elen;
608 }
609 elems->num_of_quiet_elem++;
610 break;
611 case WLAN_EID_COUNTRY:
612 elems->country_elem = pos;
613 elems->country_elem_len = elen;
614 break;
615 case WLAN_EID_PWR_CONSTRAINT:
616 elems->pwr_constr_elem = pos;
617 elems->pwr_constr_elem_len = elen;
618 break;
Jouni Malinenf797eb72009-01-19 18:48:46 +0200619 case WLAN_EID_TIMEOUT_INTERVAL:
620 elems->timeout_int = pos;
621 elems->timeout_int_len = elen;
Jouni Malinen63a5ab82009-01-08 13:32:09 +0200622 break;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200623 default:
624 break;
625 }
626
627 left -= elen;
628 pos += elen;
629 }
Johannes Bergd91f36d2009-04-16 13:17:26 +0200630
631 return crc;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200632}
Johannes Berg5825fe102008-09-09 12:56:01 +0200633
634void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
635{
636 struct ieee80211_local *local = sdata->local;
637 struct ieee80211_tx_queue_params qparam;
Johannes Bergaa837e12009-05-07 16:16:24 +0200638 int queue;
639 bool use_11b;
640 int aCWmin, aCWmax;
Johannes Berg5825fe102008-09-09 12:56:01 +0200641
642 if (!local->ops->conf_tx)
643 return;
644
645 memset(&qparam, 0, sizeof(qparam));
646
Johannes Bergaa837e12009-05-07 16:16:24 +0200647 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
648 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
Johannes Berg5825fe102008-09-09 12:56:01 +0200649
Johannes Bergaa837e12009-05-07 16:16:24 +0200650 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
651 /* Set defaults according to 802.11-2007 Table 7-37 */
652 aCWmax = 1023;
653 if (use_11b)
654 aCWmin = 31;
655 else
656 aCWmin = 15;
Johannes Berg5825fe102008-09-09 12:56:01 +0200657
Johannes Bergaa837e12009-05-07 16:16:24 +0200658 switch (queue) {
659 case 3: /* AC_BK */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200660 qparam.cw_max = aCWmax;
661 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200662 qparam.txop = 0;
663 qparam.aifs = 7;
664 break;
665 default: /* never happens but let's not leave undefined */
666 case 2: /* AC_BE */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200667 qparam.cw_max = aCWmax;
668 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200669 qparam.txop = 0;
670 qparam.aifs = 3;
671 break;
672 case 1: /* AC_VI */
673 qparam.cw_max = aCWmin;
674 qparam.cw_min = (aCWmin + 1) / 2 - 1;
675 if (use_11b)
676 qparam.txop = 6016/32;
677 else
678 qparam.txop = 3008/32;
679 qparam.aifs = 2;
680 break;
681 case 0: /* AC_VO */
682 qparam.cw_max = (aCWmin + 1) / 2 - 1;
683 qparam.cw_min = (aCWmin + 1) / 4 - 1;
684 if (use_11b)
685 qparam.txop = 3264/32;
686 else
687 qparam.txop = 1504/32;
688 qparam.aifs = 2;
689 break;
690 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200691
Johannes Bergaa837e12009-05-07 16:16:24 +0200692 drv_conf_tx(local, queue, &qparam);
693 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200694}
Johannes Berge50db652008-09-09 15:07:09 +0200695
Johannes Berg46900292009-02-15 12:44:28 +0100696void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
697 const size_t supp_rates_len,
698 const u8 *supp_rates)
699{
700 struct ieee80211_local *local = sdata->local;
701 int i, have_higher_than_11mbit = 0;
702
703 /* cf. IEEE 802.11 9.2.12 */
704 for (i = 0; i < supp_rates_len; i++)
705 if ((supp_rates[i] & 0x7f) * 5 > 110)
706 have_higher_than_11mbit = 1;
707
708 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
709 have_higher_than_11mbit)
710 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
711 else
712 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
713
714 ieee80211_set_wmm_default(sdata);
715}
716
Johannes Berge50db652008-09-09 15:07:09 +0200717void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
718 int encrypt)
719{
720 skb->dev = sdata->local->mdev;
721 skb_set_mac_header(skb, 0);
722 skb_set_network_header(skb, 0);
723 skb_set_transport_header(skb, 0);
724
725 skb->iif = sdata->dev->ifindex;
726 skb->do_not_encrypt = !encrypt;
727
728 dev_queue_xmit(skb);
729}
Johannes Berge16751c2008-09-11 00:01:53 +0200730
731int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freqMHz)
732{
733 int ret = -EINVAL;
734 struct ieee80211_channel *chan;
735 struct ieee80211_local *local = sdata->local;
736
737 chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
738
739 if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200740 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Bergd73782f2008-10-07 12:04:34 +0200741 chan->flags & IEEE80211_CHAN_NO_IBSS)
Johannes Berge16751c2008-09-11 00:01:53 +0200742 return ret;
Johannes Berge16751c2008-09-11 00:01:53 +0200743 local->oper_channel = chan;
Sujith094d05d2008-12-12 11:57:43 +0530744 local->oper_channel_type = NL80211_CHAN_NO_HT;
Johannes Berge16751c2008-09-11 00:01:53 +0200745
Johannes Bergc2b13452008-09-11 00:01:55 +0200746 if (local->sw_scanning || local->hw_scanning)
Johannes Berge16751c2008-09-11 00:01:53 +0200747 ret = 0;
748 else
Johannes Berge8975582008-10-09 12:18:51 +0200749 ret = ieee80211_hw_config(
750 local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berge16751c2008-09-11 00:01:53 +0200751 }
752
753 return ret;
754}
Johannes Berg96dd22a2008-09-11 00:01:57 +0200755
Johannes Berg881d9482009-01-21 15:13:48 +0100756u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
Johannes Berg96dd22a2008-09-11 00:01:57 +0200757 enum ieee80211_band band)
758{
759 struct ieee80211_supported_band *sband;
760 struct ieee80211_rate *bitrates;
Johannes Berg881d9482009-01-21 15:13:48 +0100761 u32 mandatory_rates;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200762 enum ieee80211_rate_flags mandatory_flag;
763 int i;
764
765 sband = local->hw.wiphy->bands[band];
766 if (!sband) {
767 WARN_ON(1);
768 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
769 }
770
771 if (band == IEEE80211_BAND_2GHZ)
772 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
773 else
774 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
775
776 bitrates = sband->bitrates;
777 mandatory_rates = 0;
778 for (i = 0; i < sband->n_bitrates; i++)
779 if (bitrates[i].flags & mandatory_flag)
780 mandatory_rates |= BIT(i);
781 return mandatory_rates;
782}
Johannes Berg46900292009-02-15 12:44:28 +0100783
784void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
785 u16 transaction, u16 auth_alg,
786 u8 *extra, size_t extra_len,
787 const u8 *bssid, int encrypt)
788{
789 struct ieee80211_local *local = sdata->local;
790 struct sk_buff *skb;
791 struct ieee80211_mgmt *mgmt;
Johannes Berg46900292009-02-15 12:44:28 +0100792
793 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200794 sizeof(*mgmt) + 6 + extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100795 if (!skb) {
796 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
797 "frame\n", sdata->dev->name);
798 return;
799 }
800 skb_reserve(skb, local->hw.extra_tx_headroom);
801
802 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
803 memset(mgmt, 0, 24 + 6);
804 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
805 IEEE80211_STYPE_AUTH);
806 if (encrypt)
807 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
808 memcpy(mgmt->da, bssid, ETH_ALEN);
809 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
810 memcpy(mgmt->bssid, bssid, ETH_ALEN);
811 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
812 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
813 mgmt->u.auth.status_code = cpu_to_le16(0);
814 if (extra)
815 memcpy(skb_put(skb, extra_len), extra, extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100816
817 ieee80211_tx_skb(sdata, skb, encrypt);
818}
819
Johannes Bergde95a542009-04-01 11:58:36 +0200820int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
821 const u8 *ie, size_t ie_len)
822{
823 struct ieee80211_supported_band *sband;
824 u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
825 int i;
826
827 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
828
829 pos = buffer;
830
831 *pos++ = WLAN_EID_SUPP_RATES;
832 supp_rates_len = pos;
833 *pos++ = 0;
834
835 for (i = 0; i < sband->n_bitrates; i++) {
836 struct ieee80211_rate *rate = &sband->bitrates[i];
837
838 if (esupp_rates_len) {
839 *esupp_rates_len += 1;
840 } else if (*supp_rates_len == 8) {
841 *pos++ = WLAN_EID_EXT_SUPP_RATES;
842 esupp_rates_len = pos;
843 *pos++ = 1;
844 } else
845 *supp_rates_len += 1;
846
847 *pos++ = rate->bitrate / 5;
848 }
849
Johannes Berg5ef2d412009-03-31 12:12:07 +0200850 if (sband->ht_cap.ht_supported) {
851 __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
852
853 *pos++ = WLAN_EID_HT_CAPABILITY;
854 *pos++ = sizeof(struct ieee80211_ht_cap);
855 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
856 memcpy(pos, &tmp, sizeof(u16));
857 pos += sizeof(u16);
858 /* TODO: needs a define here for << 2 */
859 *pos++ = sband->ht_cap.ampdu_factor |
860 (sband->ht_cap.ampdu_density << 2);
861 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
862 pos += sizeof(sband->ht_cap.mcs);
863 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
864 }
865
Johannes Bergde95a542009-04-01 11:58:36 +0200866 /*
867 * If adding more here, adjust code in main.c
868 * that calculates local->scan_ies_len.
869 */
870
871 if (ie) {
872 memcpy(pos, ie, ie_len);
873 pos += ie_len;
874 }
875
876 return pos - buffer;
877}
878
Johannes Berg46900292009-02-15 12:44:28 +0100879void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
Johannes Bergde95a542009-04-01 11:58:36 +0200880 const u8 *ssid, size_t ssid_len,
881 const u8 *ie, size_t ie_len)
Johannes Berg46900292009-02-15 12:44:28 +0100882{
883 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100884 struct sk_buff *skb;
885 struct ieee80211_mgmt *mgmt;
Johannes Bergde95a542009-04-01 11:58:36 +0200886 u8 *pos;
Johannes Berg46900292009-02-15 12:44:28 +0100887
888 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200889 ie_len);
Johannes Berg46900292009-02-15 12:44:28 +0100890 if (!skb) {
891 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
892 "request\n", sdata->dev->name);
893 return;
894 }
895 skb_reserve(skb, local->hw.extra_tx_headroom);
896
897 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
898 memset(mgmt, 0, 24);
899 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
900 IEEE80211_STYPE_PROBE_REQ);
901 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
902 if (dst) {
903 memcpy(mgmt->da, dst, ETH_ALEN);
904 memcpy(mgmt->bssid, dst, ETH_ALEN);
905 } else {
906 memset(mgmt->da, 0xff, ETH_ALEN);
907 memset(mgmt->bssid, 0xff, ETH_ALEN);
908 }
909 pos = skb_put(skb, 2 + ssid_len);
910 *pos++ = WLAN_EID_SSID;
911 *pos++ = ssid_len;
912 memcpy(pos, ssid, ssid_len);
Johannes Bergde95a542009-04-01 11:58:36 +0200913 pos += ssid_len;
Johannes Berg46900292009-02-15 12:44:28 +0100914
Johannes Bergde95a542009-04-01 11:58:36 +0200915 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
Johannes Berg46900292009-02-15 12:44:28 +0100916
917 ieee80211_tx_skb(sdata, skb, 0);
918}
919
920u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
921 struct ieee802_11_elems *elems,
922 enum ieee80211_band band)
923{
924 struct ieee80211_supported_band *sband;
925 struct ieee80211_rate *bitrates;
926 size_t num_rates;
927 u32 supp_rates;
928 int i, j;
929 sband = local->hw.wiphy->bands[band];
930
931 if (!sband) {
932 WARN_ON(1);
933 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
934 }
935
936 bitrates = sband->bitrates;
937 num_rates = sband->n_bitrates;
938 supp_rates = 0;
939 for (i = 0; i < elems->supp_rates_len +
940 elems->ext_supp_rates_len; i++) {
941 u8 rate = 0;
942 int own_rate;
943 if (i < elems->supp_rates_len)
944 rate = elems->supp_rates[i];
945 else if (elems->ext_supp_rates)
946 rate = elems->ext_supp_rates
947 [i - elems->supp_rates_len];
948 own_rate = 5 * (rate & 0x7f);
949 for (j = 0; j < num_rates; j++)
950 if (bitrates[j].bitrate == own_rate)
951 supp_rates |= BIT(j);
952 }
953 return supp_rates;
954}
Johannes Bergf2753dd2009-04-14 10:09:24 +0200955
956int ieee80211_reconfig(struct ieee80211_local *local)
957{
958 struct ieee80211_hw *hw = &local->hw;
959 struct ieee80211_sub_if_data *sdata;
960 struct ieee80211_if_init_conf conf;
961 struct sta_info *sta;
962 unsigned long flags;
963 int res;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200964 bool from_suspend = local->suspended;
965
966 /*
967 * We're going to start the hardware, at that point
968 * we are no longer suspended and can RX frames.
969 */
970 local->suspended = false;
Johannes Bergf2753dd2009-04-14 10:09:24 +0200971
972 /* restart hardware */
973 if (local->open_count) {
Johannes Berg24487982009-04-23 18:52:52 +0200974 res = drv_start(local);
Johannes Bergf2753dd2009-04-14 10:09:24 +0200975
976 ieee80211_led_radio(local, hw->conf.radio_enabled);
977 }
978
979 /* add interfaces */
980 list_for_each_entry(sdata, &local->interfaces, list) {
981 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
982 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
983 netif_running(sdata->dev)) {
984 conf.vif = &sdata->vif;
985 conf.type = sdata->vif.type;
986 conf.mac_addr = sdata->dev->dev_addr;
Johannes Berg24487982009-04-23 18:52:52 +0200987 res = drv_add_interface(local, &conf);
Johannes Bergf2753dd2009-04-14 10:09:24 +0200988 }
989 }
990
991 /* add STAs back */
992 if (local->ops->sta_notify) {
993 spin_lock_irqsave(&local->sta_lock, flags);
994 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg5bb644a2009-05-17 11:40:42 +0200995 sdata = sta->sdata;
Johannes Bergf2753dd2009-04-14 10:09:24 +0200996 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
997 sdata = container_of(sdata->bss,
998 struct ieee80211_sub_if_data,
999 u.ap);
1000
Johannes Berg24487982009-04-23 18:52:52 +02001001 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
1002 &sta->sta);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001003 }
1004 spin_unlock_irqrestore(&local->sta_lock, flags);
1005 }
1006
1007 /* Clear Suspend state so that ADDBA requests can be processed */
1008
1009 rcu_read_lock();
1010
1011 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1012 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1013 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1014 }
1015 }
1016
1017 rcu_read_unlock();
1018
1019 /* setup RTS threshold */
Johannes Berg24487982009-04-23 18:52:52 +02001020 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001021
1022 /* reconfigure hardware */
1023 ieee80211_hw_config(local, ~0);
1024
1025 netif_addr_lock_bh(local->mdev);
1026 ieee80211_configure_filter(local);
1027 netif_addr_unlock_bh(local->mdev);
1028
1029 /* Finally also reconfigure all the BSS information */
1030 list_for_each_entry(sdata, &local->interfaces, list) {
1031 u32 changed = ~0;
1032 if (!netif_running(sdata->dev))
1033 continue;
1034 switch (sdata->vif.type) {
1035 case NL80211_IFTYPE_STATION:
1036 /* disable beacon change bits */
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001037 changed &= ~(BSS_CHANGED_BEACON |
1038 BSS_CHANGED_BEACON_ENABLED);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001039 /* fall through */
1040 case NL80211_IFTYPE_ADHOC:
1041 case NL80211_IFTYPE_AP:
1042 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001043 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001044 break;
1045 case NL80211_IFTYPE_WDS:
1046 break;
1047 case NL80211_IFTYPE_AP_VLAN:
1048 case NL80211_IFTYPE_MONITOR:
1049 /* ignore virtual */
1050 break;
1051 case NL80211_IFTYPE_UNSPECIFIED:
1052 case __NL80211_IFTYPE_AFTER_LAST:
1053 WARN_ON(1);
1054 break;
1055 }
1056 }
1057
1058 /* add back keys */
1059 list_for_each_entry(sdata, &local->interfaces, list)
1060 if (netif_running(sdata->dev))
1061 ieee80211_enable_keys(sdata);
1062
1063 ieee80211_wake_queues_by_reason(hw,
1064 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1065
Johannes Berg5bb644a2009-05-17 11:40:42 +02001066 /*
1067 * If this is for hw restart things are still running.
1068 * We may want to change that later, however.
1069 */
1070 if (!from_suspend)
1071 return 0;
1072
1073#ifdef CONFIG_PM
1074 local->suspended = false;
1075
1076 list_for_each_entry(sdata, &local->interfaces, list) {
1077 switch(sdata->vif.type) {
1078 case NL80211_IFTYPE_STATION:
1079 ieee80211_sta_restart(sdata);
1080 break;
1081 case NL80211_IFTYPE_ADHOC:
1082 ieee80211_ibss_restart(sdata);
1083 break;
1084 case NL80211_IFTYPE_MESH_POINT:
1085 ieee80211_mesh_restart(sdata);
1086 break;
1087 default:
1088 break;
1089 }
1090 }
1091
1092 add_timer(&local->sta_cleanup);
1093
1094 spin_lock_irqsave(&local->sta_lock, flags);
1095 list_for_each_entry(sta, &local->sta_list, list)
1096 mesh_plink_restart(sta);
1097 spin_unlock_irqrestore(&local->sta_lock, flags);
1098#else
1099 WARN_ON(1);
1100#endif
Johannes Bergf2753dd2009-04-14 10:09:24 +02001101 return 0;
1102}