blob: cee4884b9d06215f86b73630362fae1f9ab87168 [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
Johannes Bergc2d15602007-07-27 15:43:23 +020044
Ron Rindjunsky71364712007-12-25 17:00:36 +020045u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020046 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020047{
Harvey Harrisona494bb12008-06-11 14:21:58 -070048 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020049
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020050 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
51 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020052 return NULL;
53
Harvey Harrisona494bb12008-06-11 14:21:58 -070054 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020055 if (len < 24) /* drop incorrect hdr len (data) */
56 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070057
58 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020059 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070060 if (ieee80211_has_tods(fc))
61 return hdr->addr1;
62 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020063 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070064
65 return hdr->addr3;
66 }
67
68 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020069 if (len < 24) /* drop incorrect hdr len (mgmt) */
70 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020071 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070072 }
73
74 if (ieee80211_is_ctl(fc)) {
75 if(ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020076 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070077
78 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020079 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020080 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020081 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020082 case NL80211_IFTYPE_AP:
83 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020084 return hdr->addr1;
85 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070086 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020087 }
88 }
Johannes Bergc2d15602007-07-27 15:43:23 +020089 }
90
91 return NULL;
92}
93
Harvey Harrison6693be72008-06-11 14:21:57 -070094unsigned int ieee80211_hdrlen(__le16 fc)
95{
96 unsigned int hdrlen = 24;
97
98 if (ieee80211_is_data(fc)) {
99 if (ieee80211_has_a4(fc))
100 hdrlen = 30;
101 if (ieee80211_is_data_qos(fc))
102 hdrlen += IEEE80211_QOS_CTL_LEN;
103 goto out;
104 }
105
106 if (ieee80211_is_ctl(fc)) {
107 /*
108 * ACK and CTS are 10 bytes, all others 16. To see how
109 * to get this condition consider
110 * subtype mask: 0b0000000011110000 (0x00F0)
111 * ACK subtype: 0b0000000011010000 (0x00D0)
112 * CTS subtype: 0b0000000011000000 (0x00C0)
113 * bits that matter: ^^^ (0x00E0)
114 * value of those: 0b0000000011000000 (0x00C0)
115 */
116 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
117 hdrlen = 10;
118 else
119 hdrlen = 16;
120 }
121out:
122 return hdrlen;
123}
124EXPORT_SYMBOL(ieee80211_hdrlen);
125
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700126unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
Johannes Bergc2d15602007-07-27 15:43:23 +0200127{
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700128 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *)skb->data;
129 unsigned int hdrlen;
Johannes Bergc2d15602007-07-27 15:43:23 +0200130
131 if (unlikely(skb->len < 10))
132 return 0;
Harvey Harrisonc9c69502008-06-11 14:21:57 -0700133 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Bergc2d15602007-07-27 15:43:23 +0200134 if (unlikely(hdrlen > skb->len))
135 return 0;
136 return hdrlen;
137}
138EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
139
Luis Carlos Coboee385852008-02-23 15:17:11 +0100140int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
141{
142 int ae = meshhdr->flags & IEEE80211S_FLAGS_AE;
143 /* 7.1.3.5a.2 */
144 switch (ae) {
145 case 0:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700146 return 6;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100147 case 1:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700148 return 12;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100149 case 2:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700150 return 18;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100151 case 3:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700152 return 24;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100153 default:
Luis Carlos Coboef269252008-05-05 12:02:35 -0700154 return 6;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100155 }
156}
Luis Carlos Coboee385852008-02-23 15:17:11 +0100157
Johannes Berg5cf121c2008-02-25 16:27:43 +0100158void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +0200159{
160 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
161
162 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100163 if (tx->extra_frag) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200164 struct ieee80211_hdr *fhdr;
165 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100166 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200167 fhdr = (struct ieee80211_hdr *)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100168 tx->extra_frag[i]->data;
Johannes Bergc2d15602007-07-27 15:43:23 +0200169 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
170 }
171 }
172}
173
174int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
175 int rate, int erp, int short_preamble)
176{
177 int dur;
178
179 /* calculate duration (in microseconds, rounded up to next higher
180 * integer if it includes a fractional microsecond) to send frame of
181 * len bytes (does not include FCS) at the given rate. Duration will
182 * also include SIFS.
183 *
184 * rate is in 100 kbps, so divident is multiplied by 10 in the
185 * DIV_ROUND_UP() operations.
186 */
187
Johannes Berg8318d782008-01-24 19:38:38 +0100188 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200189 /*
190 * OFDM:
191 *
192 * N_DBPS = DATARATE x 4
193 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
194 * (16 = SIGNAL time, 6 = tail bits)
195 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
196 *
197 * T_SYM = 4 usec
198 * 802.11a - 17.5.2: aSIFSTime = 16 usec
199 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
200 * signal ext = 6 usec
201 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200202 dur = 16; /* SIFS + signal ext */
203 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
204 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
205 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
206 4 * rate); /* T_SYM x N_SYM */
207 } else {
208 /*
209 * 802.11b or 802.11g with 802.11b compatibility:
210 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
211 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
212 *
213 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
214 * aSIFSTime = 10 usec
215 * aPreambleLength = 144 usec or 72 usec with short preamble
216 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
217 */
218 dur = 10; /* aSIFSTime = 10 usec */
219 dur += short_preamble ? (72 + 24) : (144 + 48);
220
221 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
222 }
223
224 return dur;
225}
226
227/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100228__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
229 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100230 size_t frame_len,
231 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200232{
233 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg25d834e2008-09-12 22:52:47 +0200234 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200235 u16 dur;
236 int erp;
Johannes Berg25d834e2008-09-12 22:52:47 +0200237 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200238
Johannes Berg8318d782008-01-24 19:38:38 +0100239 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200240 if (vif) {
241 sdata = vif_to_sdata(vif);
242 short_preamble = sdata->bss_conf.use_short_preamble;
243 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
244 erp = rate->flags & IEEE80211_RATE_ERP_G;
245 }
Johannes Berg8318d782008-01-24 19:38:38 +0100246
247 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg25d834e2008-09-12 22:52:47 +0200248 short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200249
250 return cpu_to_le16(dur);
251}
252EXPORT_SYMBOL(ieee80211_generic_frame_duration);
253
Johannes Berg32bfd352007-12-19 01:31:26 +0100254__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
255 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200256 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200257{
258 struct ieee80211_local *local = hw_to_local(hw);
259 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200260 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100261 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200262 int erp;
263 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200264 struct ieee80211_supported_band *sband;
265
266 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200267
Johannes Berg25d834e2008-09-12 22:52:47 +0200268 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200269
Johannes Berge039fa42008-05-15 12:55:29 +0200270 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100271
272 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200273 if (vif) {
274 sdata = vif_to_sdata(vif);
275 short_preamble = sdata->bss_conf.use_short_preamble;
276 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
277 erp = rate->flags & IEEE80211_RATE_ERP_G;
278 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200279
280 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100281 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200282 erp, short_preamble);
283 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100284 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200285 erp, short_preamble);
286 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100287 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200288 erp, short_preamble);
289
290 return cpu_to_le16(dur);
291}
292EXPORT_SYMBOL(ieee80211_rts_duration);
293
Johannes Berg32bfd352007-12-19 01:31:26 +0100294__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
295 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200296 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200297 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200298{
299 struct ieee80211_local *local = hw_to_local(hw);
300 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200301 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100302 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200303 int erp;
304 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200305 struct ieee80211_supported_band *sband;
306
307 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200308
Johannes Berg25d834e2008-09-12 22:52:47 +0200309 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200310
Johannes Berge039fa42008-05-15 12:55:29 +0200311 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100312 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200313 if (vif) {
314 sdata = vif_to_sdata(vif);
315 short_preamble = sdata->bss_conf.use_short_preamble;
316 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
317 erp = rate->flags & IEEE80211_RATE_ERP_G;
318 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200319
320 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100321 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200322 erp, short_preamble);
Johannes Berge039fa42008-05-15 12:55:29 +0200323 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200324 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100325 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200326 erp, short_preamble);
327 }
328
Johannes Bergc2d15602007-07-27 15:43:23 +0200329 return cpu_to_le16(dur);
330}
331EXPORT_SYMBOL(ieee80211_ctstoself_duration);
332
Johannes Bergc2d15602007-07-27 15:43:23 +0200333void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
334{
335 struct ieee80211_local *local = hw_to_local(hw);
336
Johannes Berge2530082008-05-17 00:57:14 +0200337 if (test_bit(queue, local->queues_pending)) {
Tomas Winklerf8e79dd2008-07-24 18:46:44 +0300338 set_bit(queue, local->queues_pending_run);
Johannes Berge2530082008-05-17 00:57:14 +0200339 tasklet_schedule(&local->tx_pending_tasklet);
340 } else {
David S. Miller51cb6db2008-07-15 03:34:57 -0700341 netif_wake_subqueue(local->mdev, queue);
Johannes Bergc2d15602007-07-27 15:43:23 +0200342 }
343}
344EXPORT_SYMBOL(ieee80211_wake_queue);
345
346void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
347{
348 struct ieee80211_local *local = hw_to_local(hw);
349
David S. Miller51cb6db2008-07-15 03:34:57 -0700350 netif_stop_subqueue(local->mdev, queue);
Johannes Bergc2d15602007-07-27 15:43:23 +0200351}
352EXPORT_SYMBOL(ieee80211_stop_queue);
353
Johannes Bergc2d15602007-07-27 15:43:23 +0200354void ieee80211_stop_queues(struct ieee80211_hw *hw)
355{
356 int i;
357
Johannes Berge2530082008-05-17 00:57:14 +0200358 for (i = 0; i < ieee80211_num_queues(hw); i++)
Johannes Bergc2d15602007-07-27 15:43:23 +0200359 ieee80211_stop_queue(hw, i);
360}
361EXPORT_SYMBOL(ieee80211_stop_queues);
362
Tomas Winkler92ab8532008-07-24 21:02:04 +0300363int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
364{
365 struct ieee80211_local *local = hw_to_local(hw);
366 return __netif_subqueue_stopped(local->mdev, queue);
367}
368EXPORT_SYMBOL(ieee80211_queue_stopped);
369
Johannes Bergc2d15602007-07-27 15:43:23 +0200370void ieee80211_wake_queues(struct ieee80211_hw *hw)
371{
372 int i;
373
Johannes Bergc46804702008-05-15 12:55:25 +0200374 for (i = 0; i < hw->queues + hw->ampdu_queues; i++)
Johannes Bergc2d15602007-07-27 15:43:23 +0200375 ieee80211_wake_queue(hw, i);
376}
377EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100378
Johannes Berg32bfd352007-12-19 01:31:26 +0100379void ieee80211_iterate_active_interfaces(
380 struct ieee80211_hw *hw,
381 void (*iterator)(void *data, u8 *mac,
382 struct ieee80211_vif *vif),
383 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100384{
385 struct ieee80211_local *local = hw_to_local(hw);
386 struct ieee80211_sub_if_data *sdata;
387
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200388 rtnl_lock();
389
390 list_for_each_entry(sdata, &local->interfaces, list) {
391 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200392 case __NL80211_IFTYPE_AFTER_LAST:
393 case NL80211_IFTYPE_UNSPECIFIED:
394 case NL80211_IFTYPE_MONITOR:
395 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200396 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200397 case NL80211_IFTYPE_AP:
398 case NL80211_IFTYPE_STATION:
399 case NL80211_IFTYPE_ADHOC:
400 case NL80211_IFTYPE_WDS:
401 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200402 break;
403 }
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200404 if (netif_running(sdata->dev))
405 iterator(data, sdata->dev->dev_addr,
406 &sdata->vif);
407 }
408
409 rtnl_unlock();
410}
411EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
412
413void ieee80211_iterate_active_interfaces_atomic(
414 struct ieee80211_hw *hw,
415 void (*iterator)(void *data, u8 *mac,
416 struct ieee80211_vif *vif),
417 void *data)
418{
419 struct ieee80211_local *local = hw_to_local(hw);
420 struct ieee80211_sub_if_data *sdata;
421
Johannes Berge38bad42007-11-28 10:55:32 +0100422 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100423
Johannes Berge38bad42007-11-28 10:55:32 +0100424 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100425 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200426 case __NL80211_IFTYPE_AFTER_LAST:
427 case NL80211_IFTYPE_UNSPECIFIED:
428 case NL80211_IFTYPE_MONITOR:
429 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100430 continue;
Johannes Berg05c914f2008-09-11 00:01:58 +0200431 case NL80211_IFTYPE_AP:
432 case NL80211_IFTYPE_STATION:
433 case NL80211_IFTYPE_ADHOC:
434 case NL80211_IFTYPE_WDS:
435 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergdabeb342007-11-09 01:57:29 +0100436 break;
437 }
Johannes Bergdabeb342007-11-09 01:57:29 +0100438 if (netif_running(sdata->dev))
439 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100440 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100441 }
Johannes Berge38bad42007-11-28 10:55:32 +0100442
443 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100444}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200445EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200446
447void ieee802_11_parse_elems(u8 *start, size_t len,
448 struct ieee802_11_elems *elems)
449{
450 size_t left = len;
451 u8 *pos = start;
452
453 memset(elems, 0, sizeof(*elems));
454 elems->ie_start = start;
455 elems->total_len = len;
456
457 while (left >= 2) {
458 u8 id, elen;
459
460 id = *pos++;
461 elen = *pos++;
462 left -= 2;
463
464 if (elen > left)
465 return;
466
467 switch (id) {
468 case WLAN_EID_SSID:
469 elems->ssid = pos;
470 elems->ssid_len = elen;
471 break;
472 case WLAN_EID_SUPP_RATES:
473 elems->supp_rates = pos;
474 elems->supp_rates_len = elen;
475 break;
476 case WLAN_EID_FH_PARAMS:
477 elems->fh_params = pos;
478 elems->fh_params_len = elen;
479 break;
480 case WLAN_EID_DS_PARAMS:
481 elems->ds_params = pos;
482 elems->ds_params_len = elen;
483 break;
484 case WLAN_EID_CF_PARAMS:
485 elems->cf_params = pos;
486 elems->cf_params_len = elen;
487 break;
488 case WLAN_EID_TIM:
489 elems->tim = pos;
490 elems->tim_len = elen;
491 break;
492 case WLAN_EID_IBSS_PARAMS:
493 elems->ibss_params = pos;
494 elems->ibss_params_len = elen;
495 break;
496 case WLAN_EID_CHALLENGE:
497 elems->challenge = pos;
498 elems->challenge_len = elen;
499 break;
500 case WLAN_EID_WPA:
501 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
502 pos[2] == 0xf2) {
503 /* Microsoft OUI (00:50:F2) */
504 if (pos[3] == 1) {
505 /* OUI Type 1 - WPA IE */
506 elems->wpa = pos;
507 elems->wpa_len = elen;
508 } else if (elen >= 5 && pos[3] == 2) {
509 if (pos[4] == 0) {
510 elems->wmm_info = pos;
511 elems->wmm_info_len = elen;
512 } else if (pos[4] == 1) {
513 elems->wmm_param = pos;
514 elems->wmm_param_len = elen;
515 }
516 }
517 }
518 break;
519 case WLAN_EID_RSN:
520 elems->rsn = pos;
521 elems->rsn_len = elen;
522 break;
523 case WLAN_EID_ERP_INFO:
524 elems->erp_info = pos;
525 elems->erp_info_len = elen;
526 break;
527 case WLAN_EID_EXT_SUPP_RATES:
528 elems->ext_supp_rates = pos;
529 elems->ext_supp_rates_len = elen;
530 break;
531 case WLAN_EID_HT_CAPABILITY:
Johannes Berg09914812008-10-07 19:31:17 +0200532 if (elen >= sizeof(struct ieee80211_ht_cap))
533 elems->ht_cap_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200534 break;
535 case WLAN_EID_HT_EXTRA_INFO:
Johannes Berg09914812008-10-07 19:31:17 +0200536 if (elen >= sizeof(struct ieee80211_ht_addt_info))
537 elems->ht_info_elem = (void *)pos;
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200538 break;
539 case WLAN_EID_MESH_ID:
540 elems->mesh_id = pos;
541 elems->mesh_id_len = elen;
542 break;
543 case WLAN_EID_MESH_CONFIG:
544 elems->mesh_config = pos;
545 elems->mesh_config_len = elen;
546 break;
547 case WLAN_EID_PEER_LINK:
548 elems->peer_link = pos;
549 elems->peer_link_len = elen;
550 break;
551 case WLAN_EID_PREQ:
552 elems->preq = pos;
553 elems->preq_len = elen;
554 break;
555 case WLAN_EID_PREP:
556 elems->prep = pos;
557 elems->prep_len = elen;
558 break;
559 case WLAN_EID_PERR:
560 elems->perr = pos;
561 elems->perr_len = elen;
562 break;
563 case WLAN_EID_CHANNEL_SWITCH:
564 elems->ch_switch_elem = pos;
565 elems->ch_switch_elem_len = elen;
566 break;
567 case WLAN_EID_QUIET:
568 if (!elems->quiet_elem) {
569 elems->quiet_elem = pos;
570 elems->quiet_elem_len = elen;
571 }
572 elems->num_of_quiet_elem++;
573 break;
574 case WLAN_EID_COUNTRY:
575 elems->country_elem = pos;
576 elems->country_elem_len = elen;
577 break;
578 case WLAN_EID_PWR_CONSTRAINT:
579 elems->pwr_constr_elem = pos;
580 elems->pwr_constr_elem_len = elen;
581 break;
582 default:
583 break;
584 }
585
586 left -= elen;
587 pos += elen;
588 }
589}
Johannes Berg5825fe102008-09-09 12:56:01 +0200590
591void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
592{
593 struct ieee80211_local *local = sdata->local;
594 struct ieee80211_tx_queue_params qparam;
595 int i;
596
597 if (!local->ops->conf_tx)
598 return;
599
600 memset(&qparam, 0, sizeof(qparam));
601
602 qparam.aifs = 2;
603
604 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
605 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
606 qparam.cw_min = 31;
607 else
608 qparam.cw_min = 15;
609
610 qparam.cw_max = 1023;
611 qparam.txop = 0;
612
613 for (i = 0; i < local_to_hw(local)->queues; i++)
614 local->ops->conf_tx(local_to_hw(local), i, &qparam);
615}
Johannes Berge50db652008-09-09 15:07:09 +0200616
617void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
618 int encrypt)
619{
620 skb->dev = sdata->local->mdev;
621 skb_set_mac_header(skb, 0);
622 skb_set_network_header(skb, 0);
623 skb_set_transport_header(skb, 0);
624
625 skb->iif = sdata->dev->ifindex;
626 skb->do_not_encrypt = !encrypt;
627
628 dev_queue_xmit(skb);
629}
Johannes Berge16751c2008-09-11 00:01:53 +0200630
631int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freqMHz)
632{
633 int ret = -EINVAL;
634 struct ieee80211_channel *chan;
635 struct ieee80211_local *local = sdata->local;
636
637 chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
638
639 if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200640 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berge16751c2008-09-11 00:01:53 +0200641 chan->flags & IEEE80211_CHAN_NO_IBSS) {
642 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
643 "%d MHz\n", sdata->dev->name, chan->center_freq);
644 return ret;
645 }
646 local->oper_channel = chan;
647
Johannes Bergc2b13452008-09-11 00:01:55 +0200648 if (local->sw_scanning || local->hw_scanning)
Johannes Berge16751c2008-09-11 00:01:53 +0200649 ret = 0;
650 else
651 ret = ieee80211_hw_config(local);
652
653 rate_control_clear(local);
654 }
655
656 return ret;
657}
Johannes Berg96dd22a2008-09-11 00:01:57 +0200658
659u64 ieee80211_mandatory_rates(struct ieee80211_local *local,
660 enum ieee80211_band band)
661{
662 struct ieee80211_supported_band *sband;
663 struct ieee80211_rate *bitrates;
664 u64 mandatory_rates;
665 enum ieee80211_rate_flags mandatory_flag;
666 int i;
667
668 sband = local->hw.wiphy->bands[band];
669 if (!sband) {
670 WARN_ON(1);
671 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
672 }
673
674 if (band == IEEE80211_BAND_2GHZ)
675 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
676 else
677 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
678
679 bitrates = sband->bitrates;
680 mandatory_rates = 0;
681 for (i = 0; i < sband->n_bitrates; i++)
682 if (bitrates[i].flags & mandatory_flag)
683 mandatory_rates |= BIT(i);
684 return mandatory_rates;
685}