[IWLWIFI]: add iwlwifi wireless drivers
[linux-2.6/mini2440.git] / net / mac80211 / util.c
blob29c0a0e1f52c172974629e494d3ec187e386555c
1 /*
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>
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.
11 * utilities for mac80211
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>
23 #include <net/net_namespace.h>
24 #include <net/cfg80211.h>
26 #include "ieee80211_i.h"
27 #include "ieee80211_rate.h"
28 #include "wme.h"
30 /* privid for wiphys to determine whether they belong to us or not */
31 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
33 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
34 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
35 const unsigned char rfc1042_header[] =
36 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
38 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
39 const unsigned char bridge_tunnel_header[] =
40 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
42 /* No encapsulation header if EtherType < 0x600 (=length) */
43 static const unsigned char eapol_header[] =
44 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
47 static int rate_list_match(const int *rate_list, int rate)
49 int i;
51 if (!rate_list)
52 return 0;
54 for (i = 0; rate_list[i] >= 0; i++)
55 if (rate_list[i] == rate)
56 return 1;
58 return 0;
61 void ieee80211_prepare_rates(struct ieee80211_local *local,
62 struct ieee80211_hw_mode *mode)
64 int i;
66 for (i = 0; i < mode->num_rates; i++) {
67 struct ieee80211_rate *rate = &mode->rates[i];
69 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
70 IEEE80211_RATE_BASIC);
72 if (local->supp_rates[mode->mode]) {
73 if (!rate_list_match(local->supp_rates[mode->mode],
74 rate->rate))
75 continue;
78 rate->flags |= IEEE80211_RATE_SUPPORTED;
80 /* Use configured basic rate set if it is available. If not,
81 * use defaults that are sane for most cases. */
82 if (local->basic_rates[mode->mode]) {
83 if (rate_list_match(local->basic_rates[mode->mode],
84 rate->rate))
85 rate->flags |= IEEE80211_RATE_BASIC;
86 } else switch (mode->mode) {
87 case MODE_IEEE80211A:
88 if (rate->rate == 60 || rate->rate == 120 ||
89 rate->rate == 240)
90 rate->flags |= IEEE80211_RATE_BASIC;
91 break;
92 case MODE_IEEE80211B:
93 if (rate->rate == 10 || rate->rate == 20)
94 rate->flags |= IEEE80211_RATE_BASIC;
95 break;
96 case MODE_IEEE80211G:
97 if (rate->rate == 10 || rate->rate == 20 ||
98 rate->rate == 55 || rate->rate == 110)
99 rate->flags |= IEEE80211_RATE_BASIC;
100 break;
103 /* Set ERP and MANDATORY flags based on phymode */
104 switch (mode->mode) {
105 case MODE_IEEE80211A:
106 if (rate->rate == 60 || rate->rate == 120 ||
107 rate->rate == 240)
108 rate->flags |= IEEE80211_RATE_MANDATORY;
109 break;
110 case MODE_IEEE80211B:
111 if (rate->rate == 10)
112 rate->flags |= IEEE80211_RATE_MANDATORY;
113 break;
114 case MODE_IEEE80211G:
115 if (rate->rate == 10 || rate->rate == 20 ||
116 rate->rate == 55 || rate->rate == 110 ||
117 rate->rate == 60 || rate->rate == 120 ||
118 rate->rate == 240)
119 rate->flags |= IEEE80211_RATE_MANDATORY;
120 break;
122 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
123 rate->flags |= IEEE80211_RATE_ERP;
127 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
129 u16 fc;
131 if (len < 24)
132 return NULL;
134 fc = le16_to_cpu(hdr->frame_control);
136 switch (fc & IEEE80211_FCTL_FTYPE) {
137 case IEEE80211_FTYPE_DATA:
138 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
139 case IEEE80211_FCTL_TODS:
140 return hdr->addr1;
141 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
142 return NULL;
143 case IEEE80211_FCTL_FROMDS:
144 return hdr->addr2;
145 case 0:
146 return hdr->addr3;
148 break;
149 case IEEE80211_FTYPE_MGMT:
150 return hdr->addr3;
151 case IEEE80211_FTYPE_CTL:
152 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
153 return hdr->addr1;
154 else
155 return NULL;
158 return NULL;
161 int ieee80211_get_hdrlen(u16 fc)
163 int hdrlen = 24;
165 switch (fc & IEEE80211_FCTL_FTYPE) {
166 case IEEE80211_FTYPE_DATA:
167 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
168 hdrlen = 30; /* Addr4 */
170 * The QoS Control field is two bytes and its presence is
171 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
172 * hdrlen if that bit is set.
173 * This works by masking out the bit and shifting it to
174 * bit position 1 so the result has the value 0 or 2.
176 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
177 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
178 break;
179 case IEEE80211_FTYPE_CTL:
181 * ACK and CTS are 10 bytes, all others 16. To see how
182 * to get this condition consider
183 * subtype mask: 0b0000000011110000 (0x00F0)
184 * ACK subtype: 0b0000000011010000 (0x00D0)
185 * CTS subtype: 0b0000000011000000 (0x00C0)
186 * bits that matter: ^^^ (0x00E0)
187 * value of those: 0b0000000011000000 (0x00C0)
189 if ((fc & 0xE0) == 0xC0)
190 hdrlen = 10;
191 else
192 hdrlen = 16;
193 break;
196 return hdrlen;
198 EXPORT_SYMBOL(ieee80211_get_hdrlen);
200 int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
202 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
203 int hdrlen;
205 if (unlikely(skb->len < 10))
206 return 0;
207 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
208 if (unlikely(hdrlen > skb->len))
209 return 0;
210 return hdrlen;
212 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
214 int ieee80211_is_eapol(const struct sk_buff *skb)
216 const struct ieee80211_hdr *hdr;
217 u16 fc;
218 int hdrlen;
220 if (unlikely(skb->len < 10))
221 return 0;
223 hdr = (const struct ieee80211_hdr *) skb->data;
224 fc = le16_to_cpu(hdr->frame_control);
226 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
227 return 0;
229 hdrlen = ieee80211_get_hdrlen(fc);
231 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
232 memcmp(skb->data + hdrlen, eapol_header,
233 sizeof(eapol_header)) == 0))
234 return 1;
236 return 0;
239 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
241 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
243 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
244 if (tx->u.tx.extra_frag) {
245 struct ieee80211_hdr *fhdr;
246 int i;
247 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
248 fhdr = (struct ieee80211_hdr *)
249 tx->u.tx.extra_frag[i]->data;
250 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
255 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
256 int rate, int erp, int short_preamble)
258 int dur;
260 /* calculate duration (in microseconds, rounded up to next higher
261 * integer if it includes a fractional microsecond) to send frame of
262 * len bytes (does not include FCS) at the given rate. Duration will
263 * also include SIFS.
265 * rate is in 100 kbps, so divident is multiplied by 10 in the
266 * DIV_ROUND_UP() operations.
269 if (local->hw.conf.phymode == MODE_IEEE80211A || erp) {
271 * OFDM:
273 * N_DBPS = DATARATE x 4
274 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
275 * (16 = SIGNAL time, 6 = tail bits)
276 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
278 * T_SYM = 4 usec
279 * 802.11a - 17.5.2: aSIFSTime = 16 usec
280 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
281 * signal ext = 6 usec
283 dur = 16; /* SIFS + signal ext */
284 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
285 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
286 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
287 4 * rate); /* T_SYM x N_SYM */
288 } else {
290 * 802.11b or 802.11g with 802.11b compatibility:
291 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
292 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
294 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
295 * aSIFSTime = 10 usec
296 * aPreambleLength = 144 usec or 72 usec with short preamble
297 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
299 dur = 10; /* aSIFSTime = 10 usec */
300 dur += short_preamble ? (72 + 24) : (144 + 48);
302 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
305 return dur;
308 /* Exported duration function for driver use */
309 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw, int if_id,
310 size_t frame_len, int rate)
312 struct ieee80211_local *local = hw_to_local(hw);
313 struct net_device *bdev = dev_get_by_index(&init_net, if_id);
314 struct ieee80211_sub_if_data *sdata;
315 u16 dur;
316 int erp;
318 if (unlikely(!bdev))
319 return 0;
321 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
322 erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
323 dur = ieee80211_frame_duration(local, frame_len, rate,
324 erp, sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE);
326 dev_put(bdev);
327 return cpu_to_le16(dur);
329 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
331 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw, int if_id,
332 size_t frame_len,
333 const struct ieee80211_tx_control *frame_txctl)
335 struct ieee80211_local *local = hw_to_local(hw);
336 struct ieee80211_rate *rate;
337 struct net_device *bdev = dev_get_by_index(&init_net, if_id);
338 struct ieee80211_sub_if_data *sdata;
339 int short_preamble;
340 int erp;
341 u16 dur;
343 if (unlikely(!bdev))
344 return 0;
346 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
347 short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
349 rate = frame_txctl->rts_rate;
350 erp = !!(rate->flags & IEEE80211_RATE_ERP);
352 /* CTS duration */
353 dur = ieee80211_frame_duration(local, 10, rate->rate,
354 erp, short_preamble);
355 /* Data frame duration */
356 dur += ieee80211_frame_duration(local, frame_len, rate->rate,
357 erp, short_preamble);
358 /* ACK duration */
359 dur += ieee80211_frame_duration(local, 10, rate->rate,
360 erp, short_preamble);
362 dev_put(bdev);
363 return cpu_to_le16(dur);
365 EXPORT_SYMBOL(ieee80211_rts_duration);
367 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw, int if_id,
368 size_t frame_len,
369 const struct ieee80211_tx_control *frame_txctl)
371 struct ieee80211_local *local = hw_to_local(hw);
372 struct ieee80211_rate *rate;
373 struct net_device *bdev = dev_get_by_index(&init_net, if_id);
374 struct ieee80211_sub_if_data *sdata;
375 int short_preamble;
376 int erp;
377 u16 dur;
379 if (unlikely(!bdev))
380 return 0;
382 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
383 short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
385 rate = frame_txctl->rts_rate;
386 erp = !!(rate->flags & IEEE80211_RATE_ERP);
388 /* Data frame duration */
389 dur = ieee80211_frame_duration(local, frame_len, rate->rate,
390 erp, short_preamble);
391 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
392 /* ACK duration */
393 dur += ieee80211_frame_duration(local, 10, rate->rate,
394 erp, short_preamble);
397 dev_put(bdev);
398 return cpu_to_le16(dur);
400 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
402 struct ieee80211_rate *
403 ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
405 struct ieee80211_hw_mode *mode;
406 int r;
408 list_for_each_entry(mode, &local->modes_list, list) {
409 if (mode->mode != phymode)
410 continue;
411 for (r = 0; r < mode->num_rates; r++) {
412 struct ieee80211_rate *rate = &mode->rates[r];
413 if (rate->val == hw_rate ||
414 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
415 rate->val2 == hw_rate))
416 return rate;
420 return NULL;
423 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
425 struct ieee80211_local *local = hw_to_local(hw);
427 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
428 &local->state[queue])) {
429 if (test_bit(IEEE80211_LINK_STATE_PENDING,
430 &local->state[queue]))
431 tasklet_schedule(&local->tx_pending_tasklet);
432 else
433 if (!ieee80211_qdisc_installed(local->mdev)) {
434 if (queue == 0)
435 netif_wake_queue(local->mdev);
436 } else
437 __netif_schedule(local->mdev);
440 EXPORT_SYMBOL(ieee80211_wake_queue);
442 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
444 struct ieee80211_local *local = hw_to_local(hw);
446 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
447 netif_stop_queue(local->mdev);
448 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
450 EXPORT_SYMBOL(ieee80211_stop_queue);
452 void ieee80211_start_queues(struct ieee80211_hw *hw)
454 struct ieee80211_local *local = hw_to_local(hw);
455 int i;
457 for (i = 0; i < local->hw.queues; i++)
458 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
459 if (!ieee80211_qdisc_installed(local->mdev))
460 netif_start_queue(local->mdev);
462 EXPORT_SYMBOL(ieee80211_start_queues);
464 void ieee80211_stop_queues(struct ieee80211_hw *hw)
466 int i;
468 for (i = 0; i < hw->queues; i++)
469 ieee80211_stop_queue(hw, i);
471 EXPORT_SYMBOL(ieee80211_stop_queues);
473 void ieee80211_wake_queues(struct ieee80211_hw *hw)
475 int i;
477 for (i = 0; i < hw->queues; i++)
478 ieee80211_wake_queue(hw, i);
480 EXPORT_SYMBOL(ieee80211_wake_queues);