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"
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
)
54 for (i
= 0; rate_list
[i
] >= 0; i
++)
55 if (rate_list
[i
] == rate
)
61 void ieee80211_prepare_rates(struct ieee80211_local
*local
,
62 struct ieee80211_hw_mode
*mode
)
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
],
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
],
85 rate
->flags
|= IEEE80211_RATE_BASIC
;
86 } else switch (mode
->mode
) {
88 if (rate
->rate
== 60 || rate
->rate
== 120 ||
90 rate
->flags
|= IEEE80211_RATE_BASIC
;
93 if (rate
->rate
== 10 || rate
->rate
== 20)
94 rate
->flags
|= IEEE80211_RATE_BASIC
;
96 case MODE_ATHEROS_TURBO
:
97 if (rate
->rate
== 120 || rate
->rate
== 240 ||
99 rate
->flags
|= IEEE80211_RATE_BASIC
;
101 case MODE_IEEE80211G
:
102 if (rate
->rate
== 10 || rate
->rate
== 20 ||
103 rate
->rate
== 55 || rate
->rate
== 110)
104 rate
->flags
|= IEEE80211_RATE_BASIC
;
108 /* Set ERP and MANDATORY flags based on phymode */
109 switch (mode
->mode
) {
110 case MODE_IEEE80211A
:
111 if (rate
->rate
== 60 || rate
->rate
== 120 ||
113 rate
->flags
|= IEEE80211_RATE_MANDATORY
;
115 case MODE_IEEE80211B
:
116 if (rate
->rate
== 10)
117 rate
->flags
|= IEEE80211_RATE_MANDATORY
;
119 case MODE_ATHEROS_TURBO
:
121 case MODE_IEEE80211G
:
122 if (rate
->rate
== 10 || rate
->rate
== 20 ||
123 rate
->rate
== 55 || rate
->rate
== 110 ||
124 rate
->rate
== 60 || rate
->rate
== 120 ||
126 rate
->flags
|= IEEE80211_RATE_MANDATORY
;
129 if (ieee80211_is_erp_rate(mode
->mode
, rate
->rate
))
130 rate
->flags
|= IEEE80211_RATE_ERP
;
134 u8
*ieee80211_get_bssid(struct ieee80211_hdr
*hdr
, size_t len
)
141 fc
= le16_to_cpu(hdr
->frame_control
);
143 switch (fc
& IEEE80211_FCTL_FTYPE
) {
144 case IEEE80211_FTYPE_DATA
:
145 switch (fc
& (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
146 case IEEE80211_FCTL_TODS
:
148 case (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
150 case IEEE80211_FCTL_FROMDS
:
156 case IEEE80211_FTYPE_MGMT
:
158 case IEEE80211_FTYPE_CTL
:
159 if ((fc
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_PSPOLL
)
168 int ieee80211_get_hdrlen(u16 fc
)
172 switch (fc
& IEEE80211_FCTL_FTYPE
) {
173 case IEEE80211_FTYPE_DATA
:
174 if ((fc
& IEEE80211_FCTL_FROMDS
) && (fc
& IEEE80211_FCTL_TODS
))
175 hdrlen
= 30; /* Addr4 */
177 * The QoS Control field is two bytes and its presence is
178 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
179 * hdrlen if that bit is set.
180 * This works by masking out the bit and shifting it to
181 * bit position 1 so the result has the value 0 or 2.
183 hdrlen
+= (fc
& IEEE80211_STYPE_QOS_DATA
)
184 >> (ilog2(IEEE80211_STYPE_QOS_DATA
)-1);
186 case IEEE80211_FTYPE_CTL
:
188 * ACK and CTS are 10 bytes, all others 16. To see how
189 * to get this condition consider
190 * subtype mask: 0b0000000011110000 (0x00F0)
191 * ACK subtype: 0b0000000011010000 (0x00D0)
192 * CTS subtype: 0b0000000011000000 (0x00C0)
193 * bits that matter: ^^^ (0x00E0)
194 * value of those: 0b0000000011000000 (0x00C0)
196 if ((fc
& 0xE0) == 0xC0)
205 EXPORT_SYMBOL(ieee80211_get_hdrlen
);
207 int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
209 const struct ieee80211_hdr
*hdr
= (const struct ieee80211_hdr
*) skb
->data
;
212 if (unlikely(skb
->len
< 10))
214 hdrlen
= ieee80211_get_hdrlen(le16_to_cpu(hdr
->frame_control
));
215 if (unlikely(hdrlen
> skb
->len
))
219 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
221 int ieee80211_is_eapol(const struct sk_buff
*skb
)
223 const struct ieee80211_hdr
*hdr
;
227 if (unlikely(skb
->len
< 10))
230 hdr
= (const struct ieee80211_hdr
*) skb
->data
;
231 fc
= le16_to_cpu(hdr
->frame_control
);
233 if (unlikely(!WLAN_FC_DATA_PRESENT(fc
)))
236 hdrlen
= ieee80211_get_hdrlen(fc
);
238 if (unlikely(skb
->len
>= hdrlen
+ sizeof(eapol_header
) &&
239 memcmp(skb
->data
+ hdrlen
, eapol_header
,
240 sizeof(eapol_header
)) == 0))
246 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data
*tx
)
248 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) tx
->skb
->data
;
250 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
251 if (tx
->u
.tx
.extra_frag
) {
252 struct ieee80211_hdr
*fhdr
;
254 for (i
= 0; i
< tx
->u
.tx
.num_extra_frag
; i
++) {
255 fhdr
= (struct ieee80211_hdr
*)
256 tx
->u
.tx
.extra_frag
[i
]->data
;
257 fhdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
262 int ieee80211_frame_duration(struct ieee80211_local
*local
, size_t len
,
263 int rate
, int erp
, int short_preamble
)
267 /* calculate duration (in microseconds, rounded up to next higher
268 * integer if it includes a fractional microsecond) to send frame of
269 * len bytes (does not include FCS) at the given rate. Duration will
272 * rate is in 100 kbps, so divident is multiplied by 10 in the
273 * DIV_ROUND_UP() operations.
276 if (local
->hw
.conf
.phymode
== MODE_IEEE80211A
|| erp
||
277 local
->hw
.conf
.phymode
== MODE_ATHEROS_TURBO
) {
281 * N_DBPS = DATARATE x 4
282 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
283 * (16 = SIGNAL time, 6 = tail bits)
284 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
287 * 802.11a - 17.5.2: aSIFSTime = 16 usec
288 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
289 * signal ext = 6 usec
291 /* FIX: Atheros Turbo may have different (shorter) duration? */
292 dur
= 16; /* SIFS + signal ext */
293 dur
+= 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
294 dur
+= 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
295 dur
+= 4 * DIV_ROUND_UP((16 + 8 * (len
+ 4) + 6) * 10,
296 4 * rate
); /* T_SYM x N_SYM */
299 * 802.11b or 802.11g with 802.11b compatibility:
300 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
301 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
303 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
304 * aSIFSTime = 10 usec
305 * aPreambleLength = 144 usec or 72 usec with short preamble
306 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
308 dur
= 10; /* aSIFSTime = 10 usec */
309 dur
+= short_preamble
? (72 + 24) : (144 + 48);
311 dur
+= DIV_ROUND_UP(8 * (len
+ 4) * 10, rate
);
317 /* Exported duration function for driver use */
318 __le16
ieee80211_generic_frame_duration(struct ieee80211_hw
*hw
, int if_id
,
319 size_t frame_len
, int rate
)
321 struct ieee80211_local
*local
= hw_to_local(hw
);
322 struct net_device
*bdev
= dev_get_by_index(&init_net
, if_id
);
323 struct ieee80211_sub_if_data
*sdata
;
330 sdata
= IEEE80211_DEV_TO_SUB_IF(bdev
);
331 erp
= ieee80211_is_erp_rate(hw
->conf
.phymode
, rate
);
332 dur
= ieee80211_frame_duration(local
, frame_len
, rate
,
333 erp
, sdata
->flags
& IEEE80211_SDATA_SHORT_PREAMBLE
);
336 return cpu_to_le16(dur
);
338 EXPORT_SYMBOL(ieee80211_generic_frame_duration
);
340 __le16
ieee80211_rts_duration(struct ieee80211_hw
*hw
, int if_id
,
342 const struct ieee80211_tx_control
*frame_txctl
)
344 struct ieee80211_local
*local
= hw_to_local(hw
);
345 struct ieee80211_rate
*rate
;
346 struct net_device
*bdev
= dev_get_by_index(&init_net
, if_id
);
347 struct ieee80211_sub_if_data
*sdata
;
355 sdata
= IEEE80211_DEV_TO_SUB_IF(bdev
);
356 short_preamble
= sdata
->flags
& IEEE80211_SDATA_SHORT_PREAMBLE
;
358 rate
= frame_txctl
->rts_rate
;
359 erp
= !!(rate
->flags
& IEEE80211_RATE_ERP
);
362 dur
= ieee80211_frame_duration(local
, 10, rate
->rate
,
363 erp
, short_preamble
);
364 /* Data frame duration */
365 dur
+= ieee80211_frame_duration(local
, frame_len
, rate
->rate
,
366 erp
, short_preamble
);
368 dur
+= ieee80211_frame_duration(local
, 10, rate
->rate
,
369 erp
, short_preamble
);
372 return cpu_to_le16(dur
);
374 EXPORT_SYMBOL(ieee80211_rts_duration
);
376 __le16
ieee80211_ctstoself_duration(struct ieee80211_hw
*hw
, int if_id
,
378 const struct ieee80211_tx_control
*frame_txctl
)
380 struct ieee80211_local
*local
= hw_to_local(hw
);
381 struct ieee80211_rate
*rate
;
382 struct net_device
*bdev
= dev_get_by_index(&init_net
, if_id
);
383 struct ieee80211_sub_if_data
*sdata
;
391 sdata
= IEEE80211_DEV_TO_SUB_IF(bdev
);
392 short_preamble
= sdata
->flags
& IEEE80211_SDATA_SHORT_PREAMBLE
;
394 rate
= frame_txctl
->rts_rate
;
395 erp
= !!(rate
->flags
& IEEE80211_RATE_ERP
);
397 /* Data frame duration */
398 dur
= ieee80211_frame_duration(local
, frame_len
, rate
->rate
,
399 erp
, short_preamble
);
400 if (!(frame_txctl
->flags
& IEEE80211_TXCTL_NO_ACK
)) {
402 dur
+= ieee80211_frame_duration(local
, 10, rate
->rate
,
403 erp
, short_preamble
);
407 return cpu_to_le16(dur
);
409 EXPORT_SYMBOL(ieee80211_ctstoself_duration
);
411 struct ieee80211_rate
*
412 ieee80211_get_rate(struct ieee80211_local
*local
, int phymode
, int hw_rate
)
414 struct ieee80211_hw_mode
*mode
;
417 list_for_each_entry(mode
, &local
->modes_list
, list
) {
418 if (mode
->mode
!= phymode
)
420 for (r
= 0; r
< mode
->num_rates
; r
++) {
421 struct ieee80211_rate
*rate
= &mode
->rates
[r
];
422 if (rate
->val
== hw_rate
||
423 (rate
->flags
& IEEE80211_RATE_PREAMBLE2
&&
424 rate
->val2
== hw_rate
))
432 void ieee80211_wake_queue(struct ieee80211_hw
*hw
, int queue
)
434 struct ieee80211_local
*local
= hw_to_local(hw
);
436 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF
,
437 &local
->state
[queue
])) {
438 if (test_bit(IEEE80211_LINK_STATE_PENDING
,
439 &local
->state
[queue
]))
440 tasklet_schedule(&local
->tx_pending_tasklet
);
442 if (!ieee80211_qdisc_installed(local
->mdev
)) {
444 netif_wake_queue(local
->mdev
);
446 __netif_schedule(local
->mdev
);
449 EXPORT_SYMBOL(ieee80211_wake_queue
);
451 void ieee80211_stop_queue(struct ieee80211_hw
*hw
, int queue
)
453 struct ieee80211_local
*local
= hw_to_local(hw
);
455 if (!ieee80211_qdisc_installed(local
->mdev
) && queue
== 0)
456 netif_stop_queue(local
->mdev
);
457 set_bit(IEEE80211_LINK_STATE_XOFF
, &local
->state
[queue
]);
459 EXPORT_SYMBOL(ieee80211_stop_queue
);
461 void ieee80211_start_queues(struct ieee80211_hw
*hw
)
463 struct ieee80211_local
*local
= hw_to_local(hw
);
466 for (i
= 0; i
< local
->hw
.queues
; i
++)
467 clear_bit(IEEE80211_LINK_STATE_XOFF
, &local
->state
[i
]);
468 if (!ieee80211_qdisc_installed(local
->mdev
))
469 netif_start_queue(local
->mdev
);
471 EXPORT_SYMBOL(ieee80211_start_queues
);
473 void ieee80211_stop_queues(struct ieee80211_hw
*hw
)
477 for (i
= 0; i
< hw
->queues
; i
++)
478 ieee80211_stop_queue(hw
, i
);
480 EXPORT_SYMBOL(ieee80211_stop_queues
);
482 void ieee80211_wake_queues(struct ieee80211_hw
*hw
)
486 for (i
= 0; i
< hw
->queues
; i
++)
487 ieee80211_wake_queue(hw
, i
);
489 EXPORT_SYMBOL(ieee80211_wake_queues
);