blob: 73c7d7345abd19877e320dc40f559ed7d9180611 [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>
Eric W. Biederman881d9662007-09-17 11:56:21 -070023#include <net/net_namespace.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020024#include <net/cfg80211.h>
Johannes Bergdabeb342007-11-09 01:57:29 +010025#include <net/rtnetlink.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020026
27#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040028#include "rate.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010029#include "mesh.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020030#include "wme.h"
31
32/* privid for wiphys to determine whether they belong to us or not */
33void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
34
35/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
36/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +053037const unsigned char rfc1042_header[] __aligned(2) =
Johannes Bergc2d15602007-07-27 15:43:23 +020038 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
39
40/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +053041const unsigned char bridge_tunnel_header[] __aligned(2) =
Johannes Bergc2d15602007-07-27 15:43:23 +020042 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
43
Luis R. Rodriguez9a953712009-01-22 15:05:53 -080044struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
45{
46 struct ieee80211_local *local;
47 BUG_ON(!wiphy);
48
49 local = wiphy_priv(wiphy);
50 return &local->hw;
51}
52EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
Johannes Bergc2d15602007-07-27 15:43:23 +020053
Ron Rindjunsky71364712007-12-25 17:00:36 +020054u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020055 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020056{
Harvey Harrisona494bb12008-06-11 14:21:58 -070057 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020058
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020059 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
60 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020061 return NULL;
62
Harvey Harrisona494bb12008-06-11 14:21:58 -070063 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020064 if (len < 24) /* drop incorrect hdr len (data) */
65 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070066
67 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020068 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070069 if (ieee80211_has_tods(fc))
70 return hdr->addr1;
71 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020072 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070073
74 return hdr->addr3;
75 }
76
77 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020078 if (len < 24) /* drop incorrect hdr len (mgmt) */
79 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020080 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070081 }
82
83 if (ieee80211_is_ctl(fc)) {
84 if(ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020085 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070086
87 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020088 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020089 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020090 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020091 case NL80211_IFTYPE_AP:
92 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020093 return hdr->addr1;
94 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070095 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020096 }
97 }
Johannes Bergc2d15602007-07-27 15:43:23 +020098 }
99
100 return NULL;
101}
102
Harvey Harrison6693be72008-06-11 14:21:57 -0700103unsigned int ieee80211_hdrlen(__le16 fc)
104{
105 unsigned int hdrlen = 24;
106
107 if (ieee80211_is_data(fc)) {
108 if (ieee80211_has_a4(fc))
109 hdrlen = 30;
110 if (ieee80211_is_data_qos(fc))
111 hdrlen += IEEE80211_QOS_CTL_LEN;
112 goto out;
113 }
114
115 if (ieee80211_is_ctl(fc)) {
116 /*
117 * ACK and CTS are 10 bytes, all others 16. To see how
118 * to get this condition consider
119 * subtype mask: 0b0000000011110000 (0x00F0)
120 * ACK subtype: 0b0000000011010000 (0x00D0)
121 * CTS subtype: 0b0000000011000000 (0x00C0)
122 * bits that matter: ^^^ (0x00E0)
123 * value of those: 0b0000000011000000 (0x00C0)
124 */
125 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
126 hdrlen = 10;
127 else
128 hdrlen = 16;
129 }
130out:
131 return hdrlen;
132}
133EXPORT_SYMBOL(ieee80211_hdrlen);
134
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700135unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
Johannes Bergc2d15602007-07-27 15:43:23 +0200136{
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700137 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *)skb->data;
138 unsigned int hdrlen;
Johannes Bergc2d15602007-07-27 15:43:23 +0200139
140 if (unlikely(skb->len < 10))
141 return 0;
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700142 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Bergc2d15602007-07-27 15:43:23 +0200143 if (unlikely(hdrlen > skb->len))
144 return 0;
145 return hdrlen;
146}
147EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
148
Luis Carlos Coboee385852008-02-23 15:17:11 +0100149int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
150{
151 int ae = meshhdr->flags & IEEE80211S_FLAGS_AE;
152 /* 7.1.3.5a.2 */
153 switch (ae) {
154 case 0:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700155 return 6;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100156 case 1:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700157 return 12;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100158 case 2:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700159 return 18;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100160 case 3:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700161 return 24;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100162 default:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700163 return 6;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100164 }
165}
Luis Carlos Coboee385852008-02-23 15:17:11 +0100166
Johannes Berg5cf121c2008-02-25 16:27:43 +0100167void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +0200168{
169 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
170
171 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100172 if (tx->extra_frag) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200173 struct ieee80211_hdr *fhdr;
174 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100175 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200176 fhdr = (struct ieee80211_hdr *)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100177 tx->extra_frag[i]->data;
Johannes Bergc2d15602007-07-27 15:43:23 +0200178 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
179 }
180 }
181}
182
183int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
184 int rate, int erp, int short_preamble)
185{
186 int dur;
187
188 /* calculate duration (in microseconds, rounded up to next higher
189 * integer if it includes a fractional microsecond) to send frame of
190 * len bytes (does not include FCS) at the given rate. Duration will
191 * also include SIFS.
192 *
193 * rate is in 100 kbps, so divident is multiplied by 10 in the
194 * DIV_ROUND_UP() operations.
195 */
196
Johannes Berg8318d782008-01-24 19:38:38 +0100197 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200198 /*
199 * OFDM:
200 *
201 * N_DBPS = DATARATE x 4
202 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
203 * (16 = SIGNAL time, 6 = tail bits)
204 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
205 *
206 * T_SYM = 4 usec
207 * 802.11a - 17.5.2: aSIFSTime = 16 usec
208 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
209 * signal ext = 6 usec
210 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200211 dur = 16; /* SIFS + signal ext */
212 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
213 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
214 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
215 4 * rate); /* T_SYM x N_SYM */
216 } else {
217 /*
218 * 802.11b or 802.11g with 802.11b compatibility:
219 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
220 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
221 *
222 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
223 * aSIFSTime = 10 usec
224 * aPreambleLength = 144 usec or 72 usec with short preamble
225 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
226 */
227 dur = 10; /* aSIFSTime = 10 usec */
228 dur += short_preamble ? (72 + 24) : (144 + 48);
229
230 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
231 }
232
233 return dur;
234}
235
236/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100237__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
238 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100239 size_t frame_len,
240 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200241{
242 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg25d834e2008-09-12 22:52:47 +0200243 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200244 u16 dur;
245 int erp;
Johannes Berg25d834e2008-09-12 22:52:47 +0200246 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200247
Johannes Berg8318d782008-01-24 19:38:38 +0100248 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200249 if (vif) {
250 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200251 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200252 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
253 erp = rate->flags & IEEE80211_RATE_ERP_G;
254 }
Johannes Berg8318d782008-01-24 19:38:38 +0100255
256 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg25d834e2008-09-12 22:52:47 +0200257 short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200258
259 return cpu_to_le16(dur);
260}
261EXPORT_SYMBOL(ieee80211_generic_frame_duration);
262
Johannes Berg32bfd352007-12-19 01:31:26 +0100263__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
264 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200265 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200266{
267 struct ieee80211_local *local = hw_to_local(hw);
268 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200269 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100270 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200271 int erp;
272 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200273 struct ieee80211_supported_band *sband;
274
275 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200276
Johannes Berg25d834e2008-09-12 22:52:47 +0200277 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200278
Johannes Berge039fa42008-05-15 12:55:29 +0200279 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100280
281 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200282 if (vif) {
283 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200284 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200285 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
286 erp = rate->flags & IEEE80211_RATE_ERP_G;
287 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200288
289 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100290 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200291 erp, short_preamble);
292 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100293 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200294 erp, short_preamble);
295 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100296 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200297 erp, short_preamble);
298
299 return cpu_to_le16(dur);
300}
301EXPORT_SYMBOL(ieee80211_rts_duration);
302
Johannes Berg32bfd352007-12-19 01:31:26 +0100303__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
304 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200305 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200306 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200307{
308 struct ieee80211_local *local = hw_to_local(hw);
309 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200310 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100311 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200312 int erp;
313 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200314 struct ieee80211_supported_band *sband;
315
316 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200317
Johannes Berg25d834e2008-09-12 22:52:47 +0200318 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200319
Johannes Berge039fa42008-05-15 12:55:29 +0200320 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100321 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200322 if (vif) {
323 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200324 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200325 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
326 erp = rate->flags & IEEE80211_RATE_ERP_G;
327 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200328
329 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100330 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200331 erp, short_preamble);
Johannes Berge039fa42008-05-15 12:55:29 +0200332 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200333 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100334 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200335 erp, short_preamble);
336 }
337
Johannes Bergc2d15602007-07-27 15:43:23 +0200338 return cpu_to_le16(dur);
339}
340EXPORT_SYMBOL(ieee80211_ctstoself_duration);
341
Kalle Valoce7c9112008-12-18 23:35:20 +0200342static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
343 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200344{
345 struct ieee80211_local *local = hw_to_local(hw);
346
Kalle Valoce7c9112008-12-18 23:35:20 +0200347 /* we don't need to track ampdu queues */
348 if (queue < ieee80211_num_regular_queues(hw)) {
349 __clear_bit(reason, &local->queue_stop_reasons[queue]);
350
351 if (local->queue_stop_reasons[queue] != 0)
352 /* someone still has this queue stopped */
353 return;
354 }
355
Johannes Berge2530082008-05-17 00:57:14 +0200356 if (test_bit(queue, local->queues_pending)) {
Tomas Winklerf8e79dd2008-07-24 18:46:44 +0300357 set_bit(queue, local->queues_pending_run);
Johannes Berge2530082008-05-17 00:57:14 +0200358 tasklet_schedule(&local->tx_pending_tasklet);
359 } else {
David S. Miller51cb6db2008-07-15 03:34:57 -0700360 netif_wake_subqueue(local->mdev, queue);
Johannes Bergc2d15602007-07-27 15:43:23 +0200361 }
362}
Kalle Valoce7c9112008-12-18 23:35:20 +0200363
Kalle Valob3093662008-12-29 10:02:48 +0200364static void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
365 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200366{
367 struct ieee80211_local *local = hw_to_local(hw);
368 unsigned long flags;
369
370 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
371 __ieee80211_wake_queue(hw, queue, reason);
372 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
373}
374
375void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
376{
377 ieee80211_wake_queue_by_reason(hw, queue,
378 IEEE80211_QUEUE_STOP_REASON_DRIVER);
379}
Johannes Bergc2d15602007-07-27 15:43:23 +0200380EXPORT_SYMBOL(ieee80211_wake_queue);
381
Kalle Valoce7c9112008-12-18 23:35:20 +0200382static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
383 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200384{
385 struct ieee80211_local *local = hw_to_local(hw);
386
Kalle Valoce7c9112008-12-18 23:35:20 +0200387 /* we don't need to track ampdu queues */
388 if (queue < ieee80211_num_regular_queues(hw))
389 __set_bit(reason, &local->queue_stop_reasons[queue]);
390
David S. Miller51cb6db2008-07-15 03:34:57 -0700391 netif_stop_subqueue(local->mdev, queue);
Johannes Bergc2d15602007-07-27 15:43:23 +0200392}
Kalle Valoce7c9112008-12-18 23:35:20 +0200393
Kalle Valob3093662008-12-29 10:02:48 +0200394static void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
395 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200396{
397 struct ieee80211_local *local = hw_to_local(hw);
398 unsigned long flags;
399
400 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
401 __ieee80211_stop_queue(hw, queue, reason);
402 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
403}
404
405void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
406{
407 ieee80211_stop_queue_by_reason(hw, queue,
408 IEEE80211_QUEUE_STOP_REASON_DRIVER);
409}
Johannes Bergc2d15602007-07-27 15:43:23 +0200410EXPORT_SYMBOL(ieee80211_stop_queue);
411
Kalle Valoce7c9112008-12-18 23:35:20 +0200412void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
413 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200414{
Kalle Valoce7c9112008-12-18 23:35:20 +0200415 struct ieee80211_local *local = hw_to_local(hw);
416 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200417 int i;
418
Kalle Valoce7c9112008-12-18 23:35:20 +0200419 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
420
Johannes Berge2530082008-05-17 00:57:14 +0200421 for (i = 0; i < ieee80211_num_queues(hw); i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200422 __ieee80211_stop_queue(hw, i, reason);
423
424 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
425}
426
427void ieee80211_stop_queues(struct ieee80211_hw *hw)
428{
429 ieee80211_stop_queues_by_reason(hw,
430 IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200431}
432EXPORT_SYMBOL(ieee80211_stop_queues);
433
Tomas Winkler92ab8532008-07-24 21:02:04 +0300434int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
435{
436 struct ieee80211_local *local = hw_to_local(hw);
437 return __netif_subqueue_stopped(local->mdev, queue);
438}
439EXPORT_SYMBOL(ieee80211_queue_stopped);
440
Kalle Valoce7c9112008-12-18 23:35:20 +0200441void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
442 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200443{
Kalle Valoce7c9112008-12-18 23:35:20 +0200444 struct ieee80211_local *local = hw_to_local(hw);
445 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200446 int i;
447
Kalle Valoce7c9112008-12-18 23:35:20 +0200448 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
449
Johannes Bergc46804702008-05-15 12:55:25 +0200450 for (i = 0; i < hw->queues + hw->ampdu_queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200451 __ieee80211_wake_queue(hw, i, reason);
452
453 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
454}
455
456void ieee80211_wake_queues(struct ieee80211_hw *hw)
457{
458 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200459}
460EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100461
Johannes Berg32bfd352007-12-19 01:31:26 +0100462void ieee80211_iterate_active_interfaces(
463 struct ieee80211_hw *hw,
464 void (*iterator)(void *data, u8 *mac,
465 struct ieee80211_vif *vif),
466 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100467{
468 struct ieee80211_local *local = hw_to_local(hw);
469 struct ieee80211_sub_if_data *sdata;
470
Johannes Bergc771c9d2009-01-23 22:54:03 +0100471 mutex_lock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200472
473 list_for_each_entry(sdata, &local->interfaces, list) {
474 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200475 case __NL80211_IFTYPE_AFTER_LAST:
476 case NL80211_IFTYPE_UNSPECIFIED:
477 case NL80211_IFTYPE_MONITOR:
478 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200479 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200480 case NL80211_IFTYPE_AP:
481 case NL80211_IFTYPE_STATION:
482 case NL80211_IFTYPE_ADHOC:
483 case NL80211_IFTYPE_WDS:
484 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200485 break;
486 }
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200487 if (netif_running(sdata->dev))
488 iterator(data, sdata->dev->dev_addr,
489 &sdata->vif);
490 }
491
Johannes Bergc771c9d2009-01-23 22:54:03 +0100492 mutex_unlock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200493}
494EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
495
496void ieee80211_iterate_active_interfaces_atomic(
497 struct ieee80211_hw *hw,
498 void (*iterator)(void *data, u8 *mac,
499 struct ieee80211_vif *vif),
500 void *data)
501{
502 struct ieee80211_local *local = hw_to_local(hw);
503 struct ieee80211_sub_if_data *sdata;
504
Johannes Berge38bad42007-11-28 10:55:32 +0100505 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100506
Johannes Berge38bad42007-11-28 10:55:32 +0100507 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100508 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200509 case __NL80211_IFTYPE_AFTER_LAST:
510 case NL80211_IFTYPE_UNSPECIFIED:
511 case NL80211_IFTYPE_MONITOR:
512 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100513 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200514 case NL80211_IFTYPE_AP:
515 case NL80211_IFTYPE_STATION:
516 case NL80211_IFTYPE_ADHOC:
517 case NL80211_IFTYPE_WDS:
518 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergdabeb342007-11-09 01:57:29 +0100519 break;
520 }
Johannes Bergdabeb342007-11-09 01:57:29 +0100521 if (netif_running(sdata->dev))
522 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100523 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100524 }
Johannes Berge38bad42007-11-28 10:55:32 +0100525
526 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100527}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200528EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200529
530void ieee802_11_parse_elems(u8 *start, size_t len,
531 struct ieee802_11_elems *elems)
532{
533 size_t left = len;
534 u8 *pos = start;
535
536 memset(elems, 0, sizeof(*elems));
537 elems->ie_start = start;
538 elems->total_len = len;
539
540 while (left >= 2) {
541 u8 id, elen;
542
543 id = *pos++;
544 elen = *pos++;
545 left -= 2;
546
547 if (elen > left)
548 return;
549
550 switch (id) {
551 case WLAN_EID_SSID:
552 elems->ssid = pos;
553 elems->ssid_len = elen;
554 break;
555 case WLAN_EID_SUPP_RATES:
556 elems->supp_rates = pos;
557 elems->supp_rates_len = elen;
558 break;
559 case WLAN_EID_FH_PARAMS:
560 elems->fh_params = pos;
561 elems->fh_params_len = elen;
562 break;
563 case WLAN_EID_DS_PARAMS:
564 elems->ds_params = pos;
565 elems->ds_params_len = elen;
566 break;
567 case WLAN_EID_CF_PARAMS:
568 elems->cf_params = pos;
569 elems->cf_params_len = elen;
570 break;
571 case WLAN_EID_TIM:
572 elems->tim = pos;
573 elems->tim_len = elen;
574 break;
575 case WLAN_EID_IBSS_PARAMS:
576 elems->ibss_params = pos;
577 elems->ibss_params_len = elen;
578 break;
579 case WLAN_EID_CHALLENGE:
580 elems->challenge = pos;
581 elems->challenge_len = elen;
582 break;
583 case WLAN_EID_WPA:
584 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
585 pos[2] == 0xf2) {
586 /* Microsoft OUI (00:50:F2) */
587 if (pos[3] == 1) {
588 /* OUI Type 1 - WPA IE */
589 elems->wpa = pos;
590 elems->wpa_len = elen;
591 } else if (elen >= 5 && pos[3] == 2) {
592 if (pos[4] == 0) {
593 elems->wmm_info = pos;
594 elems->wmm_info_len = elen;
595 } else if (pos[4] == 1) {
596 elems->wmm_param = pos;
597 elems->wmm_param_len = elen;
598 }
599 }
600 }
601 break;
602 case WLAN_EID_RSN:
603 elems->rsn = pos;
604 elems->rsn_len = elen;
605 break;
606 case WLAN_EID_ERP_INFO:
607 elems->erp_info = pos;
608 elems->erp_info_len = elen;
609 break;
610 case WLAN_EID_EXT_SUPP_RATES:
611 elems->ext_supp_rates = pos;
612 elems->ext_supp_rates_len = elen;
613 break;
614 case WLAN_EID_HT_CAPABILITY:
Johannes Berg09914812008-10-07 19:31:17 +0200615 if (elen >= sizeof(struct ieee80211_ht_cap))
616 elems->ht_cap_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200617 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200618 case WLAN_EID_HT_INFORMATION:
619 if (elen >= sizeof(struct ieee80211_ht_info))
Johannes Berg09914812008-10-07 19:31:17 +0200620 elems->ht_info_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200621 break;
622 case WLAN_EID_MESH_ID:
623 elems->mesh_id = pos;
624 elems->mesh_id_len = elen;
625 break;
626 case WLAN_EID_MESH_CONFIG:
627 elems->mesh_config = pos;
628 elems->mesh_config_len = elen;
629 break;
630 case WLAN_EID_PEER_LINK:
631 elems->peer_link = pos;
632 elems->peer_link_len = elen;
633 break;
634 case WLAN_EID_PREQ:
635 elems->preq = pos;
636 elems->preq_len = elen;
637 break;
638 case WLAN_EID_PREP:
639 elems->prep = pos;
640 elems->prep_len = elen;
641 break;
642 case WLAN_EID_PERR:
643 elems->perr = pos;
644 elems->perr_len = elen;
645 break;
646 case WLAN_EID_CHANNEL_SWITCH:
647 elems->ch_switch_elem = pos;
648 elems->ch_switch_elem_len = elen;
649 break;
650 case WLAN_EID_QUIET:
651 if (!elems->quiet_elem) {
652 elems->quiet_elem = pos;
653 elems->quiet_elem_len = elen;
654 }
655 elems->num_of_quiet_elem++;
656 break;
657 case WLAN_EID_COUNTRY:
658 elems->country_elem = pos;
659 elems->country_elem_len = elen;
660 break;
661 case WLAN_EID_PWR_CONSTRAINT:
662 elems->pwr_constr_elem = pos;
663 elems->pwr_constr_elem_len = elen;
664 break;
Jouni Malinenf797eb72009-01-19 18:48:46 +0200665 case WLAN_EID_TIMEOUT_INTERVAL:
666 elems->timeout_int = pos;
667 elems->timeout_int_len = elen;
Jouni Malinen63a5ab82009-01-08 13:32:09 +0200668 break;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200669 default:
670 break;
671 }
672
673 left -= elen;
674 pos += elen;
675 }
676}
Johannes Berg5825fe102008-09-09 12:56:01 +0200677
678void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
679{
680 struct ieee80211_local *local = sdata->local;
681 struct ieee80211_tx_queue_params qparam;
682 int i;
683
684 if (!local->ops->conf_tx)
685 return;
686
687 memset(&qparam, 0, sizeof(qparam));
688
689 qparam.aifs = 2;
690
691 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
692 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
693 qparam.cw_min = 31;
694 else
695 qparam.cw_min = 15;
696
697 qparam.cw_max = 1023;
698 qparam.txop = 0;
699
700 for (i = 0; i < local_to_hw(local)->queues; i++)
701 local->ops->conf_tx(local_to_hw(local), i, &qparam);
702}
Johannes Berge50db652008-09-09 15:07:09 +0200703
704void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
705 int encrypt)
706{
707 skb->dev = sdata->local->mdev;
708 skb_set_mac_header(skb, 0);
709 skb_set_network_header(skb, 0);
710 skb_set_transport_header(skb, 0);
711
712 skb->iif = sdata->dev->ifindex;
713 skb->do_not_encrypt = !encrypt;
714
715 dev_queue_xmit(skb);
716}
Johannes Berge16751c2008-09-11 00:01:53 +0200717
718int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freqMHz)
719{
720 int ret = -EINVAL;
721 struct ieee80211_channel *chan;
722 struct ieee80211_local *local = sdata->local;
723
724 chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
725
726 if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200727 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Bergd73782f2008-10-07 12:04:34 +0200728 chan->flags & IEEE80211_CHAN_NO_IBSS)
Johannes Berge16751c2008-09-11 00:01:53 +0200729 return ret;
Johannes Berge16751c2008-09-11 00:01:53 +0200730 local->oper_channel = chan;
Sujith094d05d2008-12-12 11:57:43 +0530731 local->oper_channel_type = NL80211_CHAN_NO_HT;
Johannes Berge16751c2008-09-11 00:01:53 +0200732
Johannes Bergc2b13452008-09-11 00:01:55 +0200733 if (local->sw_scanning || local->hw_scanning)
Johannes Berge16751c2008-09-11 00:01:53 +0200734 ret = 0;
735 else
Johannes Berge8975582008-10-09 12:18:51 +0200736 ret = ieee80211_hw_config(
737 local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berge16751c2008-09-11 00:01:53 +0200738 }
739
740 return ret;
741}
Johannes Berg96dd22a2008-09-11 00:01:57 +0200742
Johannes Berg881d9482009-01-21 15:13:48 +0100743u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
Johannes Berg96dd22a2008-09-11 00:01:57 +0200744 enum ieee80211_band band)
745{
746 struct ieee80211_supported_band *sband;
747 struct ieee80211_rate *bitrates;
Johannes Berg881d9482009-01-21 15:13:48 +0100748 u32 mandatory_rates;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200749 enum ieee80211_rate_flags mandatory_flag;
750 int i;
751
752 sband = local->hw.wiphy->bands[band];
753 if (!sband) {
754 WARN_ON(1);
755 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
756 }
757
758 if (band == IEEE80211_BAND_2GHZ)
759 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
760 else
761 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
762
763 bitrates = sband->bitrates;
764 mandatory_rates = 0;
765 for (i = 0; i < sband->n_bitrates; i++)
766 if (bitrates[i].flags & mandatory_flag)
767 mandatory_rates |= BIT(i);
768 return mandatory_rates;
769}