blob: 8502936e531457c7dee2775f5bd5cf53f8aacc6d [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);
272
Johannes Berge4e72fb2009-03-23 17:28:42 +0100273 if (WARN_ON(queue >= hw->queues))
274 return;
Kalle Valoce7c9112008-12-18 23:35:20 +0200275
Johannes Berg96f5e662009-02-12 00:51:53 +0100276 __clear_bit(reason, &local->queue_stop_reasons[queue]);
277
278 if (local->queue_stop_reasons[queue] != 0)
279 /* someone still has this queue stopped */
280 return;
281
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200282 if (!skb_queue_empty(&local->pending[queue]))
283 tasklet_schedule(&local->tx_pending_tasklet);
Johannes Bergc2d15602007-07-27 15:43:23 +0200284}
Kalle Valoce7c9112008-12-18 23:35:20 +0200285
Johannes Berg96f5e662009-02-12 00:51:53 +0100286void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
287 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200288{
289 struct ieee80211_local *local = hw_to_local(hw);
290 unsigned long flags;
291
292 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
293 __ieee80211_wake_queue(hw, queue, reason);
294 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
295}
296
297void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
298{
299 ieee80211_wake_queue_by_reason(hw, queue,
300 IEEE80211_QUEUE_STOP_REASON_DRIVER);
301}
Johannes Bergc2d15602007-07-27 15:43:23 +0200302EXPORT_SYMBOL(ieee80211_wake_queue);
303
Kalle Valoce7c9112008-12-18 23:35:20 +0200304static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
305 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200306{
307 struct ieee80211_local *local = hw_to_local(hw);
308
Johannes Berge4e72fb2009-03-23 17:28:42 +0100309 if (WARN_ON(queue >= hw->queues))
310 return;
Johannes Berg96f5e662009-02-12 00:51:53 +0100311
Johannes Berg2a577d92009-03-23 17:28:37 +0100312 __set_bit(reason, &local->queue_stop_reasons[queue]);
Johannes Bergc2d15602007-07-27 15:43:23 +0200313}
Kalle Valoce7c9112008-12-18 23:35:20 +0200314
Johannes Berg96f5e662009-02-12 00:51:53 +0100315void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
316 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200317{
318 struct ieee80211_local *local = hw_to_local(hw);
319 unsigned long flags;
320
321 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
322 __ieee80211_stop_queue(hw, queue, reason);
323 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
324}
325
326void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
327{
328 ieee80211_stop_queue_by_reason(hw, queue,
329 IEEE80211_QUEUE_STOP_REASON_DRIVER);
330}
Johannes Bergc2d15602007-07-27 15:43:23 +0200331EXPORT_SYMBOL(ieee80211_stop_queue);
332
Johannes Berg8f77f382009-06-07 21:58:37 +0200333void ieee80211_add_pending_skb(struct ieee80211_local *local,
334 struct sk_buff *skb)
335{
336 struct ieee80211_hw *hw = &local->hw;
337 unsigned long flags;
338 int queue = skb_get_queue_mapping(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200339 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
340
341 if (WARN_ON(!info->control.vif)) {
342 kfree(skb);
343 return;
344 }
Johannes Berg8f77f382009-06-07 21:58:37 +0200345
346 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
347 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200348 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200349 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
350 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
351}
352
353int ieee80211_add_pending_skbs(struct ieee80211_local *local,
354 struct sk_buff_head *skbs)
355{
356 struct ieee80211_hw *hw = &local->hw;
357 struct sk_buff *skb;
358 unsigned long flags;
359 int queue, ret = 0, i;
360
361 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
362 for (i = 0; i < hw->queues; i++)
363 __ieee80211_stop_queue(hw, i,
364 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
365
366 while ((skb = skb_dequeue(skbs))) {
Johannes Berga7bc3762009-07-27 10:33:31 +0200367 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
368
369 if (WARN_ON(!info->control.vif)) {
370 kfree(skb);
371 continue;
372 }
373
Johannes Berg8f77f382009-06-07 21:58:37 +0200374 ret++;
375 queue = skb_get_queue_mapping(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200376 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200377 }
378
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200379 for (i = 0; i < hw->queues; i++)
Johannes Berg8f77f382009-06-07 21:58:37 +0200380 __ieee80211_wake_queue(hw, i,
381 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
Johannes Berg8f77f382009-06-07 21:58:37 +0200382 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
383
384 return ret;
385}
386
Kalle Valoce7c9112008-12-18 23:35:20 +0200387void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
388 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200389{
Kalle Valoce7c9112008-12-18 23:35:20 +0200390 struct ieee80211_local *local = hw_to_local(hw);
391 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200392 int i;
393
Kalle Valoce7c9112008-12-18 23:35:20 +0200394 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
395
Johannes Berg96f5e662009-02-12 00:51:53 +0100396 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200397 __ieee80211_stop_queue(hw, i, reason);
398
399 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
400}
401
402void ieee80211_stop_queues(struct ieee80211_hw *hw)
403{
404 ieee80211_stop_queues_by_reason(hw,
405 IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200406}
407EXPORT_SYMBOL(ieee80211_stop_queues);
408
Tomas Winkler92ab8532008-07-24 21:02:04 +0300409int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
410{
411 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200412 unsigned long flags;
413 int ret;
Johannes Berg96f5e662009-02-12 00:51:53 +0100414
Johannes Berge4e72fb2009-03-23 17:28:42 +0100415 if (WARN_ON(queue >= hw->queues))
416 return true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100417
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200418 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
419 ret = !!local->queue_stop_reasons[queue];
420 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
421 return ret;
Tomas Winkler92ab8532008-07-24 21:02:04 +0300422}
423EXPORT_SYMBOL(ieee80211_queue_stopped);
424
Kalle Valoce7c9112008-12-18 23:35:20 +0200425void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
426 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200427{
Kalle Valoce7c9112008-12-18 23:35:20 +0200428 struct ieee80211_local *local = hw_to_local(hw);
429 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200430 int i;
431
Kalle Valoce7c9112008-12-18 23:35:20 +0200432 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
433
Johannes Berge4e72fb2009-03-23 17:28:42 +0100434 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200435 __ieee80211_wake_queue(hw, i, reason);
436
437 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
438}
439
440void ieee80211_wake_queues(struct ieee80211_hw *hw)
441{
442 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200443}
444EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100445
Johannes Berg32bfd352007-12-19 01:31:26 +0100446void ieee80211_iterate_active_interfaces(
447 struct ieee80211_hw *hw,
448 void (*iterator)(void *data, u8 *mac,
449 struct ieee80211_vif *vif),
450 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100451{
452 struct ieee80211_local *local = hw_to_local(hw);
453 struct ieee80211_sub_if_data *sdata;
454
Johannes Bergc771c9d2009-01-23 22:54:03 +0100455 mutex_lock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200456
457 list_for_each_entry(sdata, &local->interfaces, list) {
458 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200459 case __NL80211_IFTYPE_AFTER_LAST:
460 case NL80211_IFTYPE_UNSPECIFIED:
461 case NL80211_IFTYPE_MONITOR:
462 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200463 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200464 case NL80211_IFTYPE_AP:
465 case NL80211_IFTYPE_STATION:
466 case NL80211_IFTYPE_ADHOC:
467 case NL80211_IFTYPE_WDS:
468 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200469 break;
470 }
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200471 if (netif_running(sdata->dev))
472 iterator(data, sdata->dev->dev_addr,
473 &sdata->vif);
474 }
475
Johannes Bergc771c9d2009-01-23 22:54:03 +0100476 mutex_unlock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200477}
478EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
479
480void ieee80211_iterate_active_interfaces_atomic(
481 struct ieee80211_hw *hw,
482 void (*iterator)(void *data, u8 *mac,
483 struct ieee80211_vif *vif),
484 void *data)
485{
486 struct ieee80211_local *local = hw_to_local(hw);
487 struct ieee80211_sub_if_data *sdata;
488
Johannes Berge38bad42007-11-28 10:55:32 +0100489 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100490
Johannes Berge38bad42007-11-28 10:55:32 +0100491 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100492 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200493 case __NL80211_IFTYPE_AFTER_LAST:
494 case NL80211_IFTYPE_UNSPECIFIED:
495 case NL80211_IFTYPE_MONITOR:
496 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100497 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200498 case NL80211_IFTYPE_AP:
499 case NL80211_IFTYPE_STATION:
500 case NL80211_IFTYPE_ADHOC:
501 case NL80211_IFTYPE_WDS:
502 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergdabeb342007-11-09 01:57:29 +0100503 break;
504 }
Johannes Bergdabeb342007-11-09 01:57:29 +0100505 if (netif_running(sdata->dev))
506 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100507 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100508 }
Johannes Berge38bad42007-11-28 10:55:32 +0100509
510 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100511}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200512EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200513
514void ieee802_11_parse_elems(u8 *start, size_t len,
515 struct ieee802_11_elems *elems)
516{
Johannes Bergd91f36d2009-04-16 13:17:26 +0200517 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
518}
519
520u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
521 struct ieee802_11_elems *elems,
522 u64 filter, u32 crc)
523{
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200524 size_t left = len;
525 u8 *pos = start;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200526 bool calc_crc = filter != 0;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200527
528 memset(elems, 0, sizeof(*elems));
529 elems->ie_start = start;
530 elems->total_len = len;
531
532 while (left >= 2) {
533 u8 id, elen;
534
535 id = *pos++;
536 elen = *pos++;
537 left -= 2;
538
539 if (elen > left)
Johannes Bergd91f36d2009-04-16 13:17:26 +0200540 break;
541
542 if (calc_crc && id < 64 && (filter & BIT(id)))
543 crc = crc32_be(crc, pos - 2, elen + 2);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200544
545 switch (id) {
546 case WLAN_EID_SSID:
547 elems->ssid = pos;
548 elems->ssid_len = elen;
549 break;
550 case WLAN_EID_SUPP_RATES:
551 elems->supp_rates = pos;
552 elems->supp_rates_len = elen;
553 break;
554 case WLAN_EID_FH_PARAMS:
555 elems->fh_params = pos;
556 elems->fh_params_len = elen;
557 break;
558 case WLAN_EID_DS_PARAMS:
559 elems->ds_params = pos;
560 elems->ds_params_len = elen;
561 break;
562 case WLAN_EID_CF_PARAMS:
563 elems->cf_params = pos;
564 elems->cf_params_len = elen;
565 break;
566 case WLAN_EID_TIM:
Johannes Berge7ec86f2009-04-18 17:33:24 +0200567 if (elen >= sizeof(struct ieee80211_tim_ie)) {
568 elems->tim = (void *)pos;
569 elems->tim_len = elen;
570 }
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200571 break;
572 case WLAN_EID_IBSS_PARAMS:
573 elems->ibss_params = pos;
574 elems->ibss_params_len = elen;
575 break;
576 case WLAN_EID_CHALLENGE:
577 elems->challenge = pos;
578 elems->challenge_len = elen;
579 break;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200580 case WLAN_EID_VENDOR_SPECIFIC:
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200581 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
582 pos[2] == 0xf2) {
583 /* Microsoft OUI (00:50:F2) */
Johannes Bergd91f36d2009-04-16 13:17:26 +0200584
585 if (calc_crc)
586 crc = crc32_be(crc, pos - 2, elen + 2);
587
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200588 if (pos[3] == 1) {
589 /* OUI Type 1 - WPA IE */
590 elems->wpa = pos;
591 elems->wpa_len = elen;
592 } else if (elen >= 5 && pos[3] == 2) {
Johannes Bergd91f36d2009-04-16 13:17:26 +0200593 /* OUI Type 2 - WMM IE */
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200594 if (pos[4] == 0) {
595 elems->wmm_info = pos;
596 elems->wmm_info_len = elen;
597 } else if (pos[4] == 1) {
598 elems->wmm_param = pos;
599 elems->wmm_param_len = elen;
600 }
601 }
602 }
603 break;
604 case WLAN_EID_RSN:
605 elems->rsn = pos;
606 elems->rsn_len = elen;
607 break;
608 case WLAN_EID_ERP_INFO:
609 elems->erp_info = pos;
610 elems->erp_info_len = elen;
611 break;
612 case WLAN_EID_EXT_SUPP_RATES:
613 elems->ext_supp_rates = pos;
614 elems->ext_supp_rates_len = elen;
615 break;
616 case WLAN_EID_HT_CAPABILITY:
Johannes Berg09914812008-10-07 19:31:17 +0200617 if (elen >= sizeof(struct ieee80211_ht_cap))
618 elems->ht_cap_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200619 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200620 case WLAN_EID_HT_INFORMATION:
621 if (elen >= sizeof(struct ieee80211_ht_info))
Johannes Berg09914812008-10-07 19:31:17 +0200622 elems->ht_info_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200623 break;
624 case WLAN_EID_MESH_ID:
625 elems->mesh_id = pos;
626 elems->mesh_id_len = elen;
627 break;
628 case WLAN_EID_MESH_CONFIG:
629 elems->mesh_config = pos;
630 elems->mesh_config_len = elen;
631 break;
632 case WLAN_EID_PEER_LINK:
633 elems->peer_link = pos;
634 elems->peer_link_len = elen;
635 break;
636 case WLAN_EID_PREQ:
637 elems->preq = pos;
638 elems->preq_len = elen;
639 break;
640 case WLAN_EID_PREP:
641 elems->prep = pos;
642 elems->prep_len = elen;
643 break;
644 case WLAN_EID_PERR:
645 elems->perr = pos;
646 elems->perr_len = elen;
647 break;
648 case WLAN_EID_CHANNEL_SWITCH:
649 elems->ch_switch_elem = pos;
650 elems->ch_switch_elem_len = elen;
651 break;
652 case WLAN_EID_QUIET:
653 if (!elems->quiet_elem) {
654 elems->quiet_elem = pos;
655 elems->quiet_elem_len = elen;
656 }
657 elems->num_of_quiet_elem++;
658 break;
659 case WLAN_EID_COUNTRY:
660 elems->country_elem = pos;
661 elems->country_elem_len = elen;
662 break;
663 case WLAN_EID_PWR_CONSTRAINT:
664 elems->pwr_constr_elem = pos;
665 elems->pwr_constr_elem_len = elen;
666 break;
Jouni Malinenf797eb72009-01-19 18:48:46 +0200667 case WLAN_EID_TIMEOUT_INTERVAL:
668 elems->timeout_int = pos;
669 elems->timeout_int_len = elen;
Jouni Malinen63a5ab82009-01-08 13:32:09 +0200670 break;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200671 default:
672 break;
673 }
674
675 left -= elen;
676 pos += elen;
677 }
Johannes Bergd91f36d2009-04-16 13:17:26 +0200678
679 return crc;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200680}
Johannes Berg5825fe102008-09-09 12:56:01 +0200681
682void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
683{
684 struct ieee80211_local *local = sdata->local;
685 struct ieee80211_tx_queue_params qparam;
Johannes Bergaa837e12009-05-07 16:16:24 +0200686 int queue;
687 bool use_11b;
688 int aCWmin, aCWmax;
Johannes Berg5825fe102008-09-09 12:56:01 +0200689
690 if (!local->ops->conf_tx)
691 return;
692
693 memset(&qparam, 0, sizeof(qparam));
694
Johannes Bergaa837e12009-05-07 16:16:24 +0200695 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
696 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
Johannes Berg5825fe102008-09-09 12:56:01 +0200697
Johannes Bergaa837e12009-05-07 16:16:24 +0200698 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
699 /* Set defaults according to 802.11-2007 Table 7-37 */
700 aCWmax = 1023;
701 if (use_11b)
702 aCWmin = 31;
703 else
704 aCWmin = 15;
Johannes Berg5825fe102008-09-09 12:56:01 +0200705
Johannes Bergaa837e12009-05-07 16:16:24 +0200706 switch (queue) {
707 case 3: /* AC_BK */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200708 qparam.cw_max = aCWmax;
709 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200710 qparam.txop = 0;
711 qparam.aifs = 7;
712 break;
713 default: /* never happens but let's not leave undefined */
714 case 2: /* AC_BE */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200715 qparam.cw_max = aCWmax;
716 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200717 qparam.txop = 0;
718 qparam.aifs = 3;
719 break;
720 case 1: /* AC_VI */
721 qparam.cw_max = aCWmin;
722 qparam.cw_min = (aCWmin + 1) / 2 - 1;
723 if (use_11b)
724 qparam.txop = 6016/32;
725 else
726 qparam.txop = 3008/32;
727 qparam.aifs = 2;
728 break;
729 case 0: /* AC_VO */
730 qparam.cw_max = (aCWmin + 1) / 2 - 1;
731 qparam.cw_min = (aCWmin + 1) / 4 - 1;
732 if (use_11b)
733 qparam.txop = 3264/32;
734 else
735 qparam.txop = 1504/32;
736 qparam.aifs = 2;
737 break;
738 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200739
Johannes Bergaa837e12009-05-07 16:16:24 +0200740 drv_conf_tx(local, queue, &qparam);
741 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200742}
Johannes Berge50db652008-09-09 15:07:09 +0200743
Johannes Berg46900292009-02-15 12:44:28 +0100744void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
745 const size_t supp_rates_len,
746 const u8 *supp_rates)
747{
748 struct ieee80211_local *local = sdata->local;
749 int i, have_higher_than_11mbit = 0;
750
751 /* cf. IEEE 802.11 9.2.12 */
752 for (i = 0; i < supp_rates_len; i++)
753 if ((supp_rates[i] & 0x7f) * 5 > 110)
754 have_higher_than_11mbit = 1;
755
756 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
757 have_higher_than_11mbit)
758 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
759 else
760 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
761
762 ieee80211_set_wmm_default(sdata);
763}
764
Johannes Berg881d9482009-01-21 15:13:48 +0100765u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
Johannes Berg96dd22a2008-09-11 00:01:57 +0200766 enum ieee80211_band band)
767{
768 struct ieee80211_supported_band *sband;
769 struct ieee80211_rate *bitrates;
Johannes Berg881d9482009-01-21 15:13:48 +0100770 u32 mandatory_rates;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200771 enum ieee80211_rate_flags mandatory_flag;
772 int i;
773
774 sband = local->hw.wiphy->bands[band];
775 if (!sband) {
776 WARN_ON(1);
777 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
778 }
779
780 if (band == IEEE80211_BAND_2GHZ)
781 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
782 else
783 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
784
785 bitrates = sband->bitrates;
786 mandatory_rates = 0;
787 for (i = 0; i < sband->n_bitrates; i++)
788 if (bitrates[i].flags & mandatory_flag)
789 mandatory_rates |= BIT(i);
790 return mandatory_rates;
791}
Johannes Berg46900292009-02-15 12:44:28 +0100792
793void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
794 u16 transaction, u16 auth_alg,
Johannes Bergfffd0932009-07-08 14:22:54 +0200795 u8 *extra, size_t extra_len, const u8 *bssid,
796 const u8 *key, u8 key_len, u8 key_idx)
Johannes Berg46900292009-02-15 12:44:28 +0100797{
798 struct ieee80211_local *local = sdata->local;
799 struct sk_buff *skb;
800 struct ieee80211_mgmt *mgmt;
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 int err;
Johannes Berg46900292009-02-15 12:44:28 +0100802
803 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200804 sizeof(*mgmt) + 6 + extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100805 if (!skb) {
806 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
807 "frame\n", sdata->dev->name);
808 return;
809 }
810 skb_reserve(skb, local->hw.extra_tx_headroom);
811
812 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
813 memset(mgmt, 0, 24 + 6);
814 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
815 IEEE80211_STYPE_AUTH);
Johannes Berg46900292009-02-15 12:44:28 +0100816 memcpy(mgmt->da, bssid, ETH_ALEN);
817 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
818 memcpy(mgmt->bssid, bssid, ETH_ALEN);
819 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
820 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
821 mgmt->u.auth.status_code = cpu_to_le16(0);
822 if (extra)
823 memcpy(skb_put(skb, extra_len), extra, extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100824
Johannes Bergfffd0932009-07-08 14:22:54 +0200825 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
826 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
827 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
828 WARN_ON(err);
829 }
830
831 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100832}
833
Johannes Bergde95a54b2009-04-01 11:58:36 +0200834int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
835 const u8 *ie, size_t ie_len)
836{
837 struct ieee80211_supported_band *sband;
838 u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
839 int i;
840
841 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
842
843 pos = buffer;
844
845 *pos++ = WLAN_EID_SUPP_RATES;
846 supp_rates_len = pos;
847 *pos++ = 0;
848
849 for (i = 0; i < sband->n_bitrates; i++) {
850 struct ieee80211_rate *rate = &sband->bitrates[i];
851
852 if (esupp_rates_len) {
853 *esupp_rates_len += 1;
854 } else if (*supp_rates_len == 8) {
855 *pos++ = WLAN_EID_EXT_SUPP_RATES;
856 esupp_rates_len = pos;
857 *pos++ = 1;
858 } else
859 *supp_rates_len += 1;
860
861 *pos++ = rate->bitrate / 5;
862 }
863
Johannes Berg5ef2d412009-03-31 12:12:07 +0200864 if (sband->ht_cap.ht_supported) {
865 __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
866
867 *pos++ = WLAN_EID_HT_CAPABILITY;
868 *pos++ = sizeof(struct ieee80211_ht_cap);
869 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
870 memcpy(pos, &tmp, sizeof(u16));
871 pos += sizeof(u16);
872 /* TODO: needs a define here for << 2 */
873 *pos++ = sband->ht_cap.ampdu_factor |
874 (sband->ht_cap.ampdu_density << 2);
875 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
876 pos += sizeof(sband->ht_cap.mcs);
877 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
878 }
879
Johannes Bergde95a54b2009-04-01 11:58:36 +0200880 /*
881 * If adding more here, adjust code in main.c
882 * that calculates local->scan_ies_len.
883 */
884
885 if (ie) {
886 memcpy(pos, ie, ie_len);
887 pos += ie_len;
888 }
889
890 return pos - buffer;
891}
892
Johannes Berg46900292009-02-15 12:44:28 +0100893void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
Johannes Bergde95a54b2009-04-01 11:58:36 +0200894 const u8 *ssid, size_t ssid_len,
895 const u8 *ie, size_t ie_len)
Johannes Berg46900292009-02-15 12:44:28 +0100896{
897 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100898 struct sk_buff *skb;
899 struct ieee80211_mgmt *mgmt;
Johannes Bergde95a54b2009-04-01 11:58:36 +0200900 u8 *pos;
Johannes Berg46900292009-02-15 12:44:28 +0100901
902 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200903 ie_len);
Johannes Berg46900292009-02-15 12:44:28 +0100904 if (!skb) {
905 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
906 "request\n", sdata->dev->name);
907 return;
908 }
909 skb_reserve(skb, local->hw.extra_tx_headroom);
910
911 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
912 memset(mgmt, 0, 24);
913 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
914 IEEE80211_STYPE_PROBE_REQ);
915 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
916 if (dst) {
917 memcpy(mgmt->da, dst, ETH_ALEN);
918 memcpy(mgmt->bssid, dst, ETH_ALEN);
919 } else {
920 memset(mgmt->da, 0xff, ETH_ALEN);
921 memset(mgmt->bssid, 0xff, ETH_ALEN);
922 }
923 pos = skb_put(skb, 2 + ssid_len);
924 *pos++ = WLAN_EID_SSID;
925 *pos++ = ssid_len;
926 memcpy(pos, ssid, ssid_len);
Johannes Bergde95a54b2009-04-01 11:58:36 +0200927 pos += ssid_len;
Johannes Berg46900292009-02-15 12:44:28 +0100928
Johannes Bergde95a54b2009-04-01 11:58:36 +0200929 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
Johannes Berg46900292009-02-15 12:44:28 +0100930
931 ieee80211_tx_skb(sdata, skb, 0);
932}
933
934u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
935 struct ieee802_11_elems *elems,
936 enum ieee80211_band band)
937{
938 struct ieee80211_supported_band *sband;
939 struct ieee80211_rate *bitrates;
940 size_t num_rates;
941 u32 supp_rates;
942 int i, j;
943 sband = local->hw.wiphy->bands[band];
944
945 if (!sband) {
946 WARN_ON(1);
947 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
948 }
949
950 bitrates = sband->bitrates;
951 num_rates = sband->n_bitrates;
952 supp_rates = 0;
953 for (i = 0; i < elems->supp_rates_len +
954 elems->ext_supp_rates_len; i++) {
955 u8 rate = 0;
956 int own_rate;
957 if (i < elems->supp_rates_len)
958 rate = elems->supp_rates[i];
959 else if (elems->ext_supp_rates)
960 rate = elems->ext_supp_rates
961 [i - elems->supp_rates_len];
962 own_rate = 5 * (rate & 0x7f);
963 for (j = 0; j < num_rates; j++)
964 if (bitrates[j].bitrate == own_rate)
965 supp_rates |= BIT(j);
966 }
967 return supp_rates;
968}
Johannes Bergf2753dd2009-04-14 10:09:24 +0200969
970int ieee80211_reconfig(struct ieee80211_local *local)
971{
972 struct ieee80211_hw *hw = &local->hw;
973 struct ieee80211_sub_if_data *sdata;
974 struct ieee80211_if_init_conf conf;
975 struct sta_info *sta;
976 unsigned long flags;
977 int res;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200978 bool from_suspend = local->suspended;
979
980 /*
981 * We're going to start the hardware, at that point
982 * we are no longer suspended and can RX frames.
983 */
984 local->suspended = false;
Johannes Bergf2753dd2009-04-14 10:09:24 +0200985
986 /* restart hardware */
987 if (local->open_count) {
Johannes Berg24487982009-04-23 18:52:52 +0200988 res = drv_start(local);
Johannes Bergf2753dd2009-04-14 10:09:24 +0200989
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200990 ieee80211_led_radio(local, true);
Johannes Bergf2753dd2009-04-14 10:09:24 +0200991 }
992
993 /* add interfaces */
994 list_for_each_entry(sdata, &local->interfaces, list) {
995 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
996 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
997 netif_running(sdata->dev)) {
998 conf.vif = &sdata->vif;
999 conf.type = sdata->vif.type;
1000 conf.mac_addr = sdata->dev->dev_addr;
Johannes Berg24487982009-04-23 18:52:52 +02001001 res = drv_add_interface(local, &conf);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001002 }
1003 }
1004
1005 /* add STAs back */
1006 if (local->ops->sta_notify) {
1007 spin_lock_irqsave(&local->sta_lock, flags);
1008 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg5bb644a2009-05-17 11:40:42 +02001009 sdata = sta->sdata;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001010 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1011 sdata = container_of(sdata->bss,
1012 struct ieee80211_sub_if_data,
1013 u.ap);
1014
Johannes Berg24487982009-04-23 18:52:52 +02001015 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
1016 &sta->sta);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001017 }
1018 spin_unlock_irqrestore(&local->sta_lock, flags);
1019 }
1020
1021 /* Clear Suspend state so that ADDBA requests can be processed */
1022
1023 rcu_read_lock();
1024
1025 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1026 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1027 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1028 }
1029 }
1030
1031 rcu_read_unlock();
1032
1033 /* setup RTS threshold */
Johannes Berg24487982009-04-23 18:52:52 +02001034 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001035
1036 /* reconfigure hardware */
1037 ieee80211_hw_config(local, ~0);
1038
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001039 spin_lock_bh(&local->filter_lock);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001040 ieee80211_configure_filter(local);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001041 spin_unlock_bh(&local->filter_lock);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001042
1043 /* Finally also reconfigure all the BSS information */
1044 list_for_each_entry(sdata, &local->interfaces, list) {
1045 u32 changed = ~0;
1046 if (!netif_running(sdata->dev))
1047 continue;
1048 switch (sdata->vif.type) {
1049 case NL80211_IFTYPE_STATION:
1050 /* disable beacon change bits */
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001051 changed &= ~(BSS_CHANGED_BEACON |
1052 BSS_CHANGED_BEACON_ENABLED);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001053 /* fall through */
1054 case NL80211_IFTYPE_ADHOC:
1055 case NL80211_IFTYPE_AP:
1056 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001057 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001058 break;
1059 case NL80211_IFTYPE_WDS:
1060 break;
1061 case NL80211_IFTYPE_AP_VLAN:
1062 case NL80211_IFTYPE_MONITOR:
1063 /* ignore virtual */
1064 break;
1065 case NL80211_IFTYPE_UNSPECIFIED:
1066 case __NL80211_IFTYPE_AFTER_LAST:
1067 WARN_ON(1);
1068 break;
1069 }
1070 }
1071
1072 /* add back keys */
1073 list_for_each_entry(sdata, &local->interfaces, list)
1074 if (netif_running(sdata->dev))
1075 ieee80211_enable_keys(sdata);
1076
1077 ieee80211_wake_queues_by_reason(hw,
1078 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1079
Johannes Berg5bb644a2009-05-17 11:40:42 +02001080 /*
1081 * If this is for hw restart things are still running.
1082 * We may want to change that later, however.
1083 */
1084 if (!from_suspend)
1085 return 0;
1086
1087#ifdef CONFIG_PM
1088 local->suspended = false;
1089
1090 list_for_each_entry(sdata, &local->interfaces, list) {
1091 switch(sdata->vif.type) {
1092 case NL80211_IFTYPE_STATION:
1093 ieee80211_sta_restart(sdata);
1094 break;
1095 case NL80211_IFTYPE_ADHOC:
1096 ieee80211_ibss_restart(sdata);
1097 break;
1098 case NL80211_IFTYPE_MESH_POINT:
1099 ieee80211_mesh_restart(sdata);
1100 break;
1101 default:
1102 break;
1103 }
1104 }
1105
1106 add_timer(&local->sta_cleanup);
1107
1108 spin_lock_irqsave(&local->sta_lock, flags);
1109 list_for_each_entry(sta, &local->sta_list, list)
1110 mesh_plink_restart(sta);
1111 spin_unlock_irqrestore(&local->sta_lock, flags);
1112#else
1113 WARN_ON(1);
1114#endif
Johannes Bergf2753dd2009-04-14 10:09:24 +02001115 return 0;
1116}