blob: ffb6e88f2ecde96634e89e9031dce4b363dc93c5 [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
38/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
39/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +053040const unsigned char rfc1042_header[] __aligned(2) =
Johannes Bergc2d15602007-07-27 15:43:23 +020041 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
42
43/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +053044const unsigned char bridge_tunnel_header[] __aligned(2) =
Johannes Bergc2d15602007-07-27 15:43:23 +020045 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
46
Luis R. Rodriguez9a953712009-01-22 15:05:53 -080047struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
48{
49 struct ieee80211_local *local;
50 BUG_ON(!wiphy);
51
52 local = wiphy_priv(wiphy);
53 return &local->hw;
54}
55EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
Johannes Bergc2d15602007-07-27 15:43:23 +020056
Ron Rindjunsky71364712007-12-25 17:00:36 +020057u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020058 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020059{
Harvey Harrisona494bb12008-06-11 14:21:58 -070060 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020061
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020062 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
63 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020064 return NULL;
65
Harvey Harrisona494bb12008-06-11 14:21:58 -070066 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020067 if (len < 24) /* drop incorrect hdr len (data) */
68 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070069
70 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020071 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070072 if (ieee80211_has_tods(fc))
73 return hdr->addr1;
74 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020075 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070076
77 return hdr->addr3;
78 }
79
80 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020081 if (len < 24) /* drop incorrect hdr len (mgmt) */
82 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020083 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070084 }
85
86 if (ieee80211_is_ctl(fc)) {
87 if(ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020088 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070089
90 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020091 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020092 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020093 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020094 case NL80211_IFTYPE_AP:
95 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020096 return hdr->addr1;
97 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070098 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020099 }
100 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200101 }
102
103 return NULL;
104}
105
Harvey Harrison6693be72008-06-11 14:21:57 -0700106unsigned int ieee80211_hdrlen(__le16 fc)
107{
108 unsigned int hdrlen = 24;
109
110 if (ieee80211_is_data(fc)) {
111 if (ieee80211_has_a4(fc))
112 hdrlen = 30;
113 if (ieee80211_is_data_qos(fc))
114 hdrlen += IEEE80211_QOS_CTL_LEN;
115 goto out;
116 }
117
118 if (ieee80211_is_ctl(fc)) {
119 /*
120 * ACK and CTS are 10 bytes, all others 16. To see how
121 * to get this condition consider
122 * subtype mask: 0b0000000011110000 (0x00F0)
123 * ACK subtype: 0b0000000011010000 (0x00D0)
124 * CTS subtype: 0b0000000011000000 (0x00C0)
125 * bits that matter: ^^^ (0x00E0)
126 * value of those: 0b0000000011000000 (0x00C0)
127 */
128 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
129 hdrlen = 10;
130 else
131 hdrlen = 16;
132 }
133out:
134 return hdrlen;
135}
136EXPORT_SYMBOL(ieee80211_hdrlen);
137
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700138unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
Johannes Bergc2d15602007-07-27 15:43:23 +0200139{
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700140 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *)skb->data;
141 unsigned int hdrlen;
Johannes Bergc2d15602007-07-27 15:43:23 +0200142
143 if (unlikely(skb->len < 10))
144 return 0;
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700145 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Bergc2d15602007-07-27 15:43:23 +0200146 if (unlikely(hdrlen > skb->len))
147 return 0;
148 return hdrlen;
149}
150EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
151
Luis Carlos Coboee385852008-02-23 15:17:11 +0100152int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
153{
154 int ae = meshhdr->flags & IEEE80211S_FLAGS_AE;
155 /* 7.1.3.5a.2 */
156 switch (ae) {
157 case 0:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700158 return 6;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100159 case 1:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700160 return 12;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100161 case 2:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700162 return 18;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100163 case 3:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700164 return 24;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100165 default:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700166 return 6;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100167 }
168}
Luis Carlos Coboee385852008-02-23 15:17:11 +0100169
Johannes Berg5cf121c2008-02-25 16:27:43 +0100170void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +0200171{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100172 struct sk_buff *skb = tx->skb;
173 struct ieee80211_hdr *hdr;
Johannes Bergc2d15602007-07-27 15:43:23 +0200174
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100175 do {
176 hdr = (struct ieee80211_hdr *) skb->data;
177 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
178 } while ((skb = skb->next));
Johannes Bergc2d15602007-07-27 15:43:23 +0200179}
180
181int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
182 int rate, int erp, int short_preamble)
183{
184 int dur;
185
186 /* calculate duration (in microseconds, rounded up to next higher
187 * integer if it includes a fractional microsecond) to send frame of
188 * len bytes (does not include FCS) at the given rate. Duration will
189 * also include SIFS.
190 *
191 * rate is in 100 kbps, so divident is multiplied by 10 in the
192 * DIV_ROUND_UP() operations.
193 */
194
Johannes Berg8318d782008-01-24 19:38:38 +0100195 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200196 /*
197 * OFDM:
198 *
199 * N_DBPS = DATARATE x 4
200 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
201 * (16 = SIGNAL time, 6 = tail bits)
202 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
203 *
204 * T_SYM = 4 usec
205 * 802.11a - 17.5.2: aSIFSTime = 16 usec
206 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
207 * signal ext = 6 usec
208 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200209 dur = 16; /* SIFS + signal ext */
210 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
211 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
212 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
213 4 * rate); /* T_SYM x N_SYM */
214 } else {
215 /*
216 * 802.11b or 802.11g with 802.11b compatibility:
217 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
218 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
219 *
220 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
221 * aSIFSTime = 10 usec
222 * aPreambleLength = 144 usec or 72 usec with short preamble
223 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
224 */
225 dur = 10; /* aSIFSTime = 10 usec */
226 dur += short_preamble ? (72 + 24) : (144 + 48);
227
228 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
229 }
230
231 return dur;
232}
233
234/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100235__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
236 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100237 size_t frame_len,
238 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200239{
240 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg25d834e2008-09-12 22:52:47 +0200241 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200242 u16 dur;
243 int erp;
Johannes Berg25d834e2008-09-12 22:52:47 +0200244 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200245
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 Berg8318d782008-01-24 19:38:38 +0100253
254 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg25d834e2008-09-12 22:52:47 +0200255 short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200256
257 return cpu_to_le16(dur);
258}
259EXPORT_SYMBOL(ieee80211_generic_frame_duration);
260
Johannes Berg32bfd352007-12-19 01:31:26 +0100261__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
262 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200263 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200264{
265 struct ieee80211_local *local = hw_to_local(hw);
266 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200267 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100268 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200269 int erp;
270 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200271 struct ieee80211_supported_band *sband;
272
273 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200274
Johannes Berg25d834e2008-09-12 22:52:47 +0200275 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200276
Johannes Berge039fa42008-05-15 12:55:29 +0200277 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100278
279 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200280 if (vif) {
281 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200282 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200283 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
284 erp = rate->flags & IEEE80211_RATE_ERP_G;
285 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200286
287 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100288 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200289 erp, short_preamble);
290 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100291 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200292 erp, short_preamble);
293 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100294 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200295 erp, short_preamble);
296
297 return cpu_to_le16(dur);
298}
299EXPORT_SYMBOL(ieee80211_rts_duration);
300
Johannes Berg32bfd352007-12-19 01:31:26 +0100301__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
302 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200303 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200304 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200305{
306 struct ieee80211_local *local = hw_to_local(hw);
307 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200308 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100309 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200310 int erp;
311 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200312 struct ieee80211_supported_band *sband;
313
314 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200315
Johannes Berg25d834e2008-09-12 22:52:47 +0200316 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200317
Johannes Berge039fa42008-05-15 12:55:29 +0200318 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100319 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200320 if (vif) {
321 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200322 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200323 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
324 erp = rate->flags & IEEE80211_RATE_ERP_G;
325 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200326
327 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100328 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200329 erp, short_preamble);
Johannes Berge039fa42008-05-15 12:55:29 +0200330 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200331 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100332 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200333 erp, short_preamble);
334 }
335
Johannes Bergc2d15602007-07-27 15:43:23 +0200336 return cpu_to_le16(dur);
337}
338EXPORT_SYMBOL(ieee80211_ctstoself_duration);
339
Kalle Valoce7c9112008-12-18 23:35:20 +0200340static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
341 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200342{
343 struct ieee80211_local *local = hw_to_local(hw);
344
Johannes Berge4e72fb2009-03-23 17:28:42 +0100345 if (WARN_ON(queue >= hw->queues))
346 return;
Kalle Valoce7c9112008-12-18 23:35:20 +0200347
Johannes Berg96f5e662009-02-12 00:51:53 +0100348 __clear_bit(reason, &local->queue_stop_reasons[queue]);
349
Johannes Berg2a577d92009-03-23 17:28:37 +0100350 if (!skb_queue_empty(&local->pending[queue]) &&
351 local->queue_stop_reasons[queue] ==
352 BIT(IEEE80211_QUEUE_STOP_REASON_PENDING))
353 tasklet_schedule(&local->tx_pending_tasklet);
354
Johannes Berg96f5e662009-02-12 00:51:53 +0100355 if (local->queue_stop_reasons[queue] != 0)
356 /* someone still has this queue stopped */
357 return;
358
Johannes Berg2a577d92009-03-23 17:28:37 +0100359 netif_wake_subqueue(local->mdev, queue);
Johannes Bergc2d15602007-07-27 15:43:23 +0200360}
Kalle Valoce7c9112008-12-18 23:35:20 +0200361
Johannes Berg96f5e662009-02-12 00:51:53 +0100362void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
363 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200364{
365 struct ieee80211_local *local = hw_to_local(hw);
366 unsigned long flags;
367
368 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
369 __ieee80211_wake_queue(hw, queue, reason);
370 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
371}
372
373void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
374{
375 ieee80211_wake_queue_by_reason(hw, queue,
376 IEEE80211_QUEUE_STOP_REASON_DRIVER);
377}
Johannes Bergc2d15602007-07-27 15:43:23 +0200378EXPORT_SYMBOL(ieee80211_wake_queue);
379
Kalle Valoce7c9112008-12-18 23:35:20 +0200380static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
381 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200382{
383 struct ieee80211_local *local = hw_to_local(hw);
384
Johannes Berge4e72fb2009-03-23 17:28:42 +0100385 if (WARN_ON(queue >= hw->queues))
386 return;
Johannes Berg96f5e662009-02-12 00:51:53 +0100387
Johannes Berg2a577d92009-03-23 17:28:37 +0100388 /*
389 * Only stop if it was previously running, this is necessary
390 * for correct pending packets handling because there we may
391 * start (but not wake) the queue and rely on that.
392 */
393 if (!local->queue_stop_reasons[queue])
394 netif_stop_subqueue(local->mdev, queue);
Kalle Valoce7c9112008-12-18 23:35:20 +0200395
Johannes Berg2a577d92009-03-23 17:28:37 +0100396 __set_bit(reason, &local->queue_stop_reasons[queue]);
Johannes Bergc2d15602007-07-27 15:43:23 +0200397}
Kalle Valoce7c9112008-12-18 23:35:20 +0200398
Johannes Berg96f5e662009-02-12 00:51:53 +0100399void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
400 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200401{
402 struct ieee80211_local *local = hw_to_local(hw);
403 unsigned long flags;
404
405 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
406 __ieee80211_stop_queue(hw, queue, reason);
407 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
408}
409
410void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
411{
412 ieee80211_stop_queue_by_reason(hw, queue,
413 IEEE80211_QUEUE_STOP_REASON_DRIVER);
414}
Johannes Bergc2d15602007-07-27 15:43:23 +0200415EXPORT_SYMBOL(ieee80211_stop_queue);
416
Kalle Valoce7c9112008-12-18 23:35:20 +0200417void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
418 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200419{
Kalle Valoce7c9112008-12-18 23:35:20 +0200420 struct ieee80211_local *local = hw_to_local(hw);
421 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200422 int i;
423
Kalle Valoce7c9112008-12-18 23:35:20 +0200424 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
425
Johannes Berg96f5e662009-02-12 00:51:53 +0100426 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200427 __ieee80211_stop_queue(hw, i, reason);
428
429 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
430}
431
432void ieee80211_stop_queues(struct ieee80211_hw *hw)
433{
434 ieee80211_stop_queues_by_reason(hw,
435 IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200436}
437EXPORT_SYMBOL(ieee80211_stop_queues);
438
Tomas Winkler92ab8532008-07-24 21:02:04 +0300439int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
440{
441 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg96f5e662009-02-12 00:51:53 +0100442
Johannes Berge4e72fb2009-03-23 17:28:42 +0100443 if (WARN_ON(queue >= hw->queues))
444 return true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100445
Tomas Winkler92ab8532008-07-24 21:02:04 +0300446 return __netif_subqueue_stopped(local->mdev, queue);
447}
448EXPORT_SYMBOL(ieee80211_queue_stopped);
449
Kalle Valoce7c9112008-12-18 23:35:20 +0200450void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
451 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200452{
Kalle Valoce7c9112008-12-18 23:35:20 +0200453 struct ieee80211_local *local = hw_to_local(hw);
454 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200455 int i;
456
Kalle Valoce7c9112008-12-18 23:35:20 +0200457 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
458
Johannes Berge4e72fb2009-03-23 17:28:42 +0100459 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200460 __ieee80211_wake_queue(hw, i, reason);
461
462 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
463}
464
465void ieee80211_wake_queues(struct ieee80211_hw *hw)
466{
467 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200468}
469EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100470
Johannes Berg32bfd352007-12-19 01:31:26 +0100471void ieee80211_iterate_active_interfaces(
472 struct ieee80211_hw *hw,
473 void (*iterator)(void *data, u8 *mac,
474 struct ieee80211_vif *vif),
475 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100476{
477 struct ieee80211_local *local = hw_to_local(hw);
478 struct ieee80211_sub_if_data *sdata;
479
Johannes Bergc771c9d2009-01-23 22:54:03 +0100480 mutex_lock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200481
482 list_for_each_entry(sdata, &local->interfaces, list) {
483 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200484 case __NL80211_IFTYPE_AFTER_LAST:
485 case NL80211_IFTYPE_UNSPECIFIED:
486 case NL80211_IFTYPE_MONITOR:
487 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200488 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200489 case NL80211_IFTYPE_AP:
490 case NL80211_IFTYPE_STATION:
491 case NL80211_IFTYPE_ADHOC:
492 case NL80211_IFTYPE_WDS:
493 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200494 break;
495 }
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200496 if (netif_running(sdata->dev))
497 iterator(data, sdata->dev->dev_addr,
498 &sdata->vif);
499 }
500
Johannes Bergc771c9d2009-01-23 22:54:03 +0100501 mutex_unlock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200502}
503EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
504
505void ieee80211_iterate_active_interfaces_atomic(
506 struct ieee80211_hw *hw,
507 void (*iterator)(void *data, u8 *mac,
508 struct ieee80211_vif *vif),
509 void *data)
510{
511 struct ieee80211_local *local = hw_to_local(hw);
512 struct ieee80211_sub_if_data *sdata;
513
Johannes Berge38bad42007-11-28 10:55:32 +0100514 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100515
Johannes Berge38bad42007-11-28 10:55:32 +0100516 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100517 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200518 case __NL80211_IFTYPE_AFTER_LAST:
519 case NL80211_IFTYPE_UNSPECIFIED:
520 case NL80211_IFTYPE_MONITOR:
521 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100522 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200523 case NL80211_IFTYPE_AP:
524 case NL80211_IFTYPE_STATION:
525 case NL80211_IFTYPE_ADHOC:
526 case NL80211_IFTYPE_WDS:
527 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergdabeb342007-11-09 01:57:29 +0100528 break;
529 }
Johannes Bergdabeb342007-11-09 01:57:29 +0100530 if (netif_running(sdata->dev))
531 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100532 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100533 }
Johannes Berge38bad42007-11-28 10:55:32 +0100534
535 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100536}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200537EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200538
539void ieee802_11_parse_elems(u8 *start, size_t len,
540 struct ieee802_11_elems *elems)
541{
Johannes Bergd91f36d2009-04-16 13:17:26 +0200542 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
543}
544
545u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
546 struct ieee802_11_elems *elems,
547 u64 filter, u32 crc)
548{
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200549 size_t left = len;
550 u8 *pos = start;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200551 bool calc_crc = filter != 0;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200552
553 memset(elems, 0, sizeof(*elems));
554 elems->ie_start = start;
555 elems->total_len = len;
556
557 while (left >= 2) {
558 u8 id, elen;
559
560 id = *pos++;
561 elen = *pos++;
562 left -= 2;
563
564 if (elen > left)
Johannes Bergd91f36d2009-04-16 13:17:26 +0200565 break;
566
567 if (calc_crc && id < 64 && (filter & BIT(id)))
568 crc = crc32_be(crc, pos - 2, elen + 2);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200569
570 switch (id) {
571 case WLAN_EID_SSID:
572 elems->ssid = pos;
573 elems->ssid_len = elen;
574 break;
575 case WLAN_EID_SUPP_RATES:
576 elems->supp_rates = pos;
577 elems->supp_rates_len = elen;
578 break;
579 case WLAN_EID_FH_PARAMS:
580 elems->fh_params = pos;
581 elems->fh_params_len = elen;
582 break;
583 case WLAN_EID_DS_PARAMS:
584 elems->ds_params = pos;
585 elems->ds_params_len = elen;
586 break;
587 case WLAN_EID_CF_PARAMS:
588 elems->cf_params = pos;
589 elems->cf_params_len = elen;
590 break;
591 case WLAN_EID_TIM:
Johannes Berge7ec86f2009-04-18 17:33:24 +0200592 if (elen >= sizeof(struct ieee80211_tim_ie)) {
593 elems->tim = (void *)pos;
594 elems->tim_len = elen;
595 }
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200596 break;
597 case WLAN_EID_IBSS_PARAMS:
598 elems->ibss_params = pos;
599 elems->ibss_params_len = elen;
600 break;
601 case WLAN_EID_CHALLENGE:
602 elems->challenge = pos;
603 elems->challenge_len = elen;
604 break;
Johannes Bergd91f36d2009-04-16 13:17:26 +0200605 case WLAN_EID_VENDOR_SPECIFIC:
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200606 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
607 pos[2] == 0xf2) {
608 /* Microsoft OUI (00:50:F2) */
Johannes Bergd91f36d2009-04-16 13:17:26 +0200609
610 if (calc_crc)
611 crc = crc32_be(crc, pos - 2, elen + 2);
612
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200613 if (pos[3] == 1) {
614 /* OUI Type 1 - WPA IE */
615 elems->wpa = pos;
616 elems->wpa_len = elen;
617 } else if (elen >= 5 && pos[3] == 2) {
Johannes Bergd91f36d2009-04-16 13:17:26 +0200618 /* OUI Type 2 - WMM IE */
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200619 if (pos[4] == 0) {
620 elems->wmm_info = pos;
621 elems->wmm_info_len = elen;
622 } else if (pos[4] == 1) {
623 elems->wmm_param = pos;
624 elems->wmm_param_len = elen;
625 }
626 }
627 }
628 break;
629 case WLAN_EID_RSN:
630 elems->rsn = pos;
631 elems->rsn_len = elen;
632 break;
633 case WLAN_EID_ERP_INFO:
634 elems->erp_info = pos;
635 elems->erp_info_len = elen;
636 break;
637 case WLAN_EID_EXT_SUPP_RATES:
638 elems->ext_supp_rates = pos;
639 elems->ext_supp_rates_len = elen;
640 break;
641 case WLAN_EID_HT_CAPABILITY:
Johannes Berg09914812008-10-07 19:31:17 +0200642 if (elen >= sizeof(struct ieee80211_ht_cap))
643 elems->ht_cap_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200644 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200645 case WLAN_EID_HT_INFORMATION:
646 if (elen >= sizeof(struct ieee80211_ht_info))
Johannes Berg09914812008-10-07 19:31:17 +0200647 elems->ht_info_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200648 break;
649 case WLAN_EID_MESH_ID:
650 elems->mesh_id = pos;
651 elems->mesh_id_len = elen;
652 break;
653 case WLAN_EID_MESH_CONFIG:
654 elems->mesh_config = pos;
655 elems->mesh_config_len = elen;
656 break;
657 case WLAN_EID_PEER_LINK:
658 elems->peer_link = pos;
659 elems->peer_link_len = elen;
660 break;
661 case WLAN_EID_PREQ:
662 elems->preq = pos;
663 elems->preq_len = elen;
664 break;
665 case WLAN_EID_PREP:
666 elems->prep = pos;
667 elems->prep_len = elen;
668 break;
669 case WLAN_EID_PERR:
670 elems->perr = pos;
671 elems->perr_len = elen;
672 break;
673 case WLAN_EID_CHANNEL_SWITCH:
674 elems->ch_switch_elem = pos;
675 elems->ch_switch_elem_len = elen;
676 break;
677 case WLAN_EID_QUIET:
678 if (!elems->quiet_elem) {
679 elems->quiet_elem = pos;
680 elems->quiet_elem_len = elen;
681 }
682 elems->num_of_quiet_elem++;
683 break;
684 case WLAN_EID_COUNTRY:
685 elems->country_elem = pos;
686 elems->country_elem_len = elen;
687 break;
688 case WLAN_EID_PWR_CONSTRAINT:
689 elems->pwr_constr_elem = pos;
690 elems->pwr_constr_elem_len = elen;
691 break;
Jouni Malinenf797eb72009-01-19 18:48:46 +0200692 case WLAN_EID_TIMEOUT_INTERVAL:
693 elems->timeout_int = pos;
694 elems->timeout_int_len = elen;
Jouni Malinen63a5ab82009-01-08 13:32:09 +0200695 break;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200696 default:
697 break;
698 }
699
700 left -= elen;
701 pos += elen;
702 }
Johannes Bergd91f36d2009-04-16 13:17:26 +0200703
704 return crc;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200705}
Johannes Berg5825fe102008-09-09 12:56:01 +0200706
707void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
708{
709 struct ieee80211_local *local = sdata->local;
710 struct ieee80211_tx_queue_params qparam;
Johannes Bergaa837e12009-05-07 16:16:24 +0200711 int queue;
712 bool use_11b;
713 int aCWmin, aCWmax;
Johannes Berg5825fe102008-09-09 12:56:01 +0200714
715 if (!local->ops->conf_tx)
716 return;
717
718 memset(&qparam, 0, sizeof(qparam));
719
Johannes Bergaa837e12009-05-07 16:16:24 +0200720 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
721 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
Johannes Berg5825fe102008-09-09 12:56:01 +0200722
Johannes Bergaa837e12009-05-07 16:16:24 +0200723 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
724 /* Set defaults according to 802.11-2007 Table 7-37 */
725 aCWmax = 1023;
726 if (use_11b)
727 aCWmin = 31;
728 else
729 aCWmin = 15;
Johannes Berg5825fe102008-09-09 12:56:01 +0200730
Johannes Bergaa837e12009-05-07 16:16:24 +0200731 switch (queue) {
732 case 3: /* AC_BK */
733 qparam.cw_max = aCWmin;
734 qparam.cw_min = aCWmax;
735 qparam.txop = 0;
736 qparam.aifs = 7;
737 break;
738 default: /* never happens but let's not leave undefined */
739 case 2: /* AC_BE */
740 qparam.cw_max = aCWmin;
741 qparam.cw_min = aCWmax;
742 qparam.txop = 0;
743 qparam.aifs = 3;
744 break;
745 case 1: /* AC_VI */
746 qparam.cw_max = aCWmin;
747 qparam.cw_min = (aCWmin + 1) / 2 - 1;
748 if (use_11b)
749 qparam.txop = 6016/32;
750 else
751 qparam.txop = 3008/32;
752 qparam.aifs = 2;
753 break;
754 case 0: /* AC_VO */
755 qparam.cw_max = (aCWmin + 1) / 2 - 1;
756 qparam.cw_min = (aCWmin + 1) / 4 - 1;
757 if (use_11b)
758 qparam.txop = 3264/32;
759 else
760 qparam.txop = 1504/32;
761 qparam.aifs = 2;
762 break;
763 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200764
Johannes Bergaa837e12009-05-07 16:16:24 +0200765 drv_conf_tx(local, queue, &qparam);
766 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200767}
Johannes Berge50db652008-09-09 15:07:09 +0200768
Johannes Berg46900292009-02-15 12:44:28 +0100769void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
770 const size_t supp_rates_len,
771 const u8 *supp_rates)
772{
773 struct ieee80211_local *local = sdata->local;
774 int i, have_higher_than_11mbit = 0;
775
776 /* cf. IEEE 802.11 9.2.12 */
777 for (i = 0; i < supp_rates_len; i++)
778 if ((supp_rates[i] & 0x7f) * 5 > 110)
779 have_higher_than_11mbit = 1;
780
781 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
782 have_higher_than_11mbit)
783 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
784 else
785 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
786
787 ieee80211_set_wmm_default(sdata);
788}
789
Johannes Berge50db652008-09-09 15:07:09 +0200790void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
791 int encrypt)
792{
793 skb->dev = sdata->local->mdev;
794 skb_set_mac_header(skb, 0);
795 skb_set_network_header(skb, 0);
796 skb_set_transport_header(skb, 0);
797
798 skb->iif = sdata->dev->ifindex;
799 skb->do_not_encrypt = !encrypt;
800
801 dev_queue_xmit(skb);
802}
Johannes Berge16751c2008-09-11 00:01:53 +0200803
804int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freqMHz)
805{
806 int ret = -EINVAL;
807 struct ieee80211_channel *chan;
808 struct ieee80211_local *local = sdata->local;
809
810 chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
811
812 if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200813 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Bergd73782f2008-10-07 12:04:34 +0200814 chan->flags & IEEE80211_CHAN_NO_IBSS)
Johannes Berge16751c2008-09-11 00:01:53 +0200815 return ret;
Johannes Berge16751c2008-09-11 00:01:53 +0200816 local->oper_channel = chan;
Sujith094d05d2008-12-12 11:57:43 +0530817 local->oper_channel_type = NL80211_CHAN_NO_HT;
Johannes Berge16751c2008-09-11 00:01:53 +0200818
Johannes Bergc2b13452008-09-11 00:01:55 +0200819 if (local->sw_scanning || local->hw_scanning)
Johannes Berge16751c2008-09-11 00:01:53 +0200820 ret = 0;
821 else
Johannes Berge8975582008-10-09 12:18:51 +0200822 ret = ieee80211_hw_config(
823 local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berge16751c2008-09-11 00:01:53 +0200824 }
825
826 return ret;
827}
Johannes Berg96dd22a2008-09-11 00:01:57 +0200828
Johannes Berg881d9482009-01-21 15:13:48 +0100829u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
Johannes Berg96dd22a2008-09-11 00:01:57 +0200830 enum ieee80211_band band)
831{
832 struct ieee80211_supported_band *sband;
833 struct ieee80211_rate *bitrates;
Johannes Berg881d9482009-01-21 15:13:48 +0100834 u32 mandatory_rates;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200835 enum ieee80211_rate_flags mandatory_flag;
836 int i;
837
838 sband = local->hw.wiphy->bands[band];
839 if (!sband) {
840 WARN_ON(1);
841 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
842 }
843
844 if (band == IEEE80211_BAND_2GHZ)
845 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
846 else
847 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
848
849 bitrates = sband->bitrates;
850 mandatory_rates = 0;
851 for (i = 0; i < sband->n_bitrates; i++)
852 if (bitrates[i].flags & mandatory_flag)
853 mandatory_rates |= BIT(i);
854 return mandatory_rates;
855}
Johannes Berg46900292009-02-15 12:44:28 +0100856
857void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
858 u16 transaction, u16 auth_alg,
859 u8 *extra, size_t extra_len,
860 const u8 *bssid, int encrypt)
861{
862 struct ieee80211_local *local = sdata->local;
863 struct sk_buff *skb;
864 struct ieee80211_mgmt *mgmt;
Johannes Berg46900292009-02-15 12:44:28 +0100865
866 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200867 sizeof(*mgmt) + 6 + extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100868 if (!skb) {
869 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
870 "frame\n", sdata->dev->name);
871 return;
872 }
873 skb_reserve(skb, local->hw.extra_tx_headroom);
874
875 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
876 memset(mgmt, 0, 24 + 6);
877 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
878 IEEE80211_STYPE_AUTH);
879 if (encrypt)
880 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
881 memcpy(mgmt->da, bssid, ETH_ALEN);
882 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
883 memcpy(mgmt->bssid, bssid, ETH_ALEN);
884 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
885 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
886 mgmt->u.auth.status_code = cpu_to_le16(0);
887 if (extra)
888 memcpy(skb_put(skb, extra_len), extra, extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100889
890 ieee80211_tx_skb(sdata, skb, encrypt);
891}
892
Johannes Bergde95a542009-04-01 11:58:36 +0200893int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
894 const u8 *ie, size_t ie_len)
895{
896 struct ieee80211_supported_band *sband;
897 u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
898 int i;
899
900 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
901
902 pos = buffer;
903
904 *pos++ = WLAN_EID_SUPP_RATES;
905 supp_rates_len = pos;
906 *pos++ = 0;
907
908 for (i = 0; i < sband->n_bitrates; i++) {
909 struct ieee80211_rate *rate = &sband->bitrates[i];
910
911 if (esupp_rates_len) {
912 *esupp_rates_len += 1;
913 } else if (*supp_rates_len == 8) {
914 *pos++ = WLAN_EID_EXT_SUPP_RATES;
915 esupp_rates_len = pos;
916 *pos++ = 1;
917 } else
918 *supp_rates_len += 1;
919
920 *pos++ = rate->bitrate / 5;
921 }
922
Johannes Berg5ef2d412009-03-31 12:12:07 +0200923 if (sband->ht_cap.ht_supported) {
924 __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
925
926 *pos++ = WLAN_EID_HT_CAPABILITY;
927 *pos++ = sizeof(struct ieee80211_ht_cap);
928 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
929 memcpy(pos, &tmp, sizeof(u16));
930 pos += sizeof(u16);
931 /* TODO: needs a define here for << 2 */
932 *pos++ = sband->ht_cap.ampdu_factor |
933 (sband->ht_cap.ampdu_density << 2);
934 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
935 pos += sizeof(sband->ht_cap.mcs);
936 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
937 }
938
Johannes Bergde95a542009-04-01 11:58:36 +0200939 /*
940 * If adding more here, adjust code in main.c
941 * that calculates local->scan_ies_len.
942 */
943
944 if (ie) {
945 memcpy(pos, ie, ie_len);
946 pos += ie_len;
947 }
948
949 return pos - buffer;
950}
951
Johannes Berg46900292009-02-15 12:44:28 +0100952void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
Johannes Bergde95a542009-04-01 11:58:36 +0200953 const u8 *ssid, size_t ssid_len,
954 const u8 *ie, size_t ie_len)
Johannes Berg46900292009-02-15 12:44:28 +0100955{
956 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100957 struct sk_buff *skb;
958 struct ieee80211_mgmt *mgmt;
Johannes Bergde95a542009-04-01 11:58:36 +0200959 u8 *pos;
Johannes Berg46900292009-02-15 12:44:28 +0100960
961 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200962 ie_len);
Johannes Berg46900292009-02-15 12:44:28 +0100963 if (!skb) {
964 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
965 "request\n", sdata->dev->name);
966 return;
967 }
968 skb_reserve(skb, local->hw.extra_tx_headroom);
969
970 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
971 memset(mgmt, 0, 24);
972 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
973 IEEE80211_STYPE_PROBE_REQ);
974 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
975 if (dst) {
976 memcpy(mgmt->da, dst, ETH_ALEN);
977 memcpy(mgmt->bssid, dst, ETH_ALEN);
978 } else {
979 memset(mgmt->da, 0xff, ETH_ALEN);
980 memset(mgmt->bssid, 0xff, ETH_ALEN);
981 }
982 pos = skb_put(skb, 2 + ssid_len);
983 *pos++ = WLAN_EID_SSID;
984 *pos++ = ssid_len;
985 memcpy(pos, ssid, ssid_len);
Johannes Bergde95a542009-04-01 11:58:36 +0200986 pos += ssid_len;
Johannes Berg46900292009-02-15 12:44:28 +0100987
Johannes Bergde95a542009-04-01 11:58:36 +0200988 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
Johannes Berg46900292009-02-15 12:44:28 +0100989
990 ieee80211_tx_skb(sdata, skb, 0);
991}
992
993u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
994 struct ieee802_11_elems *elems,
995 enum ieee80211_band band)
996{
997 struct ieee80211_supported_band *sband;
998 struct ieee80211_rate *bitrates;
999 size_t num_rates;
1000 u32 supp_rates;
1001 int i, j;
1002 sband = local->hw.wiphy->bands[band];
1003
1004 if (!sband) {
1005 WARN_ON(1);
1006 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1007 }
1008
1009 bitrates = sband->bitrates;
1010 num_rates = sband->n_bitrates;
1011 supp_rates = 0;
1012 for (i = 0; i < elems->supp_rates_len +
1013 elems->ext_supp_rates_len; i++) {
1014 u8 rate = 0;
1015 int own_rate;
1016 if (i < elems->supp_rates_len)
1017 rate = elems->supp_rates[i];
1018 else if (elems->ext_supp_rates)
1019 rate = elems->ext_supp_rates
1020 [i - elems->supp_rates_len];
1021 own_rate = 5 * (rate & 0x7f);
1022 for (j = 0; j < num_rates; j++)
1023 if (bitrates[j].bitrate == own_rate)
1024 supp_rates |= BIT(j);
1025 }
1026 return supp_rates;
1027}
Johannes Bergf2753dd2009-04-14 10:09:24 +02001028
1029int ieee80211_reconfig(struct ieee80211_local *local)
1030{
1031 struct ieee80211_hw *hw = &local->hw;
1032 struct ieee80211_sub_if_data *sdata;
1033 struct ieee80211_if_init_conf conf;
1034 struct sta_info *sta;
1035 unsigned long flags;
1036 int res;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001037 bool from_suspend = local->suspended;
1038
1039 /*
1040 * We're going to start the hardware, at that point
1041 * we are no longer suspended and can RX frames.
1042 */
1043 local->suspended = false;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001044
1045 /* restart hardware */
1046 if (local->open_count) {
Johannes Berg24487982009-04-23 18:52:52 +02001047 res = drv_start(local);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001048
1049 ieee80211_led_radio(local, hw->conf.radio_enabled);
1050 }
1051
1052 /* add interfaces */
1053 list_for_each_entry(sdata, &local->interfaces, list) {
1054 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1055 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1056 netif_running(sdata->dev)) {
1057 conf.vif = &sdata->vif;
1058 conf.type = sdata->vif.type;
1059 conf.mac_addr = sdata->dev->dev_addr;
Johannes Berg24487982009-04-23 18:52:52 +02001060 res = drv_add_interface(local, &conf);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001061 }
1062 }
1063
1064 /* add STAs back */
1065 if (local->ops->sta_notify) {
1066 spin_lock_irqsave(&local->sta_lock, flags);
1067 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg5bb644a2009-05-17 11:40:42 +02001068 sdata = sta->sdata;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001069 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1070 sdata = container_of(sdata->bss,
1071 struct ieee80211_sub_if_data,
1072 u.ap);
1073
Johannes Berg24487982009-04-23 18:52:52 +02001074 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
1075 &sta->sta);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001076 }
1077 spin_unlock_irqrestore(&local->sta_lock, flags);
1078 }
1079
1080 /* Clear Suspend state so that ADDBA requests can be processed */
1081
1082 rcu_read_lock();
1083
1084 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1085 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1086 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1087 }
1088 }
1089
1090 rcu_read_unlock();
1091
1092 /* setup RTS threshold */
Johannes Berg24487982009-04-23 18:52:52 +02001093 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001094
1095 /* reconfigure hardware */
1096 ieee80211_hw_config(local, ~0);
1097
1098 netif_addr_lock_bh(local->mdev);
1099 ieee80211_configure_filter(local);
1100 netif_addr_unlock_bh(local->mdev);
1101
1102 /* Finally also reconfigure all the BSS information */
1103 list_for_each_entry(sdata, &local->interfaces, list) {
1104 u32 changed = ~0;
1105 if (!netif_running(sdata->dev))
1106 continue;
1107 switch (sdata->vif.type) {
1108 case NL80211_IFTYPE_STATION:
1109 /* disable beacon change bits */
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001110 changed &= ~(BSS_CHANGED_BEACON |
1111 BSS_CHANGED_BEACON_ENABLED);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001112 /* fall through */
1113 case NL80211_IFTYPE_ADHOC:
1114 case NL80211_IFTYPE_AP:
1115 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001116 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001117 break;
1118 case NL80211_IFTYPE_WDS:
1119 break;
1120 case NL80211_IFTYPE_AP_VLAN:
1121 case NL80211_IFTYPE_MONITOR:
1122 /* ignore virtual */
1123 break;
1124 case NL80211_IFTYPE_UNSPECIFIED:
1125 case __NL80211_IFTYPE_AFTER_LAST:
1126 WARN_ON(1);
1127 break;
1128 }
1129 }
1130
1131 /* add back keys */
1132 list_for_each_entry(sdata, &local->interfaces, list)
1133 if (netif_running(sdata->dev))
1134 ieee80211_enable_keys(sdata);
1135
1136 ieee80211_wake_queues_by_reason(hw,
1137 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1138
Johannes Berg5bb644a2009-05-17 11:40:42 +02001139 /*
1140 * If this is for hw restart things are still running.
1141 * We may want to change that later, however.
1142 */
1143 if (!from_suspend)
1144 return 0;
1145
1146#ifdef CONFIG_PM
1147 local->suspended = false;
1148
1149 list_for_each_entry(sdata, &local->interfaces, list) {
1150 switch(sdata->vif.type) {
1151 case NL80211_IFTYPE_STATION:
1152 ieee80211_sta_restart(sdata);
1153 break;
1154 case NL80211_IFTYPE_ADHOC:
1155 ieee80211_ibss_restart(sdata);
1156 break;
1157 case NL80211_IFTYPE_MESH_POINT:
1158 ieee80211_mesh_restart(sdata);
1159 break;
1160 default:
1161 break;
1162 }
1163 }
1164
1165 add_timer(&local->sta_cleanup);
1166
1167 spin_lock_irqsave(&local->sta_lock, flags);
1168 list_for_each_entry(sta, &local->sta_list, list)
1169 mesh_plink_restart(sta);
1170 spin_unlock_irqrestore(&local->sta_lock, flags);
1171#else
1172 WARN_ON(1);
1173#endif
Johannes Bergf2753dd2009-04-14 10:09:24 +02001174 return 0;
1175}