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>
25 #include <net/rtnetlink.h>
27 #include "ieee80211_i.h"
28 #include "ieee80211_rate.h"
31 /* privid for wiphys to determine whether they belong to us or not */
32 void *mac80211_wiphy_privid
= &mac80211_wiphy_privid
;
34 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
35 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
36 const unsigned char rfc1042_header
[] =
37 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
39 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
40 const unsigned char bridge_tunnel_header
[] =
41 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
43 /* No encapsulation header if EtherType < 0x600 (=length) */
44 static const unsigned char eapol_header
[] =
45 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
48 static int rate_list_match(const int *rate_list
, int rate
)
55 for (i
= 0; rate_list
[i
] >= 0; i
++)
56 if (rate_list
[i
] == rate
)
62 void ieee80211_prepare_rates(struct ieee80211_local
*local
,
63 struct ieee80211_hw_mode
*mode
)
67 for (i
= 0; i
< mode
->num_rates
; i
++) {
68 struct ieee80211_rate
*rate
= &mode
->rates
[i
];
70 rate
->flags
&= ~(IEEE80211_RATE_SUPPORTED
|
71 IEEE80211_RATE_BASIC
);
73 if (local
->supp_rates
[mode
->mode
]) {
74 if (!rate_list_match(local
->supp_rates
[mode
->mode
],
79 rate
->flags
|= IEEE80211_RATE_SUPPORTED
;
81 /* Use configured basic rate set if it is available. If not,
82 * use defaults that are sane for most cases. */
83 if (local
->basic_rates
[mode
->mode
]) {
84 if (rate_list_match(local
->basic_rates
[mode
->mode
],
86 rate
->flags
|= IEEE80211_RATE_BASIC
;
87 } else switch (mode
->mode
) {
89 if (rate
->rate
== 60 || rate
->rate
== 120 ||
91 rate
->flags
|= IEEE80211_RATE_BASIC
;
94 if (rate
->rate
== 10 || rate
->rate
== 20)
95 rate
->flags
|= IEEE80211_RATE_BASIC
;
98 if (rate
->rate
== 10 || rate
->rate
== 20 ||
99 rate
->rate
== 55 || rate
->rate
== 110)
100 rate
->flags
|= IEEE80211_RATE_BASIC
;
102 case NUM_IEEE80211_MODES
:
107 /* Set ERP and MANDATORY flags based on phymode */
108 switch (mode
->mode
) {
109 case MODE_IEEE80211A
:
110 if (rate
->rate
== 60 || rate
->rate
== 120 ||
112 rate
->flags
|= IEEE80211_RATE_MANDATORY
;
114 case MODE_IEEE80211B
:
115 if (rate
->rate
== 10)
116 rate
->flags
|= IEEE80211_RATE_MANDATORY
;
118 case MODE_IEEE80211G
:
119 if (rate
->rate
== 10 || rate
->rate
== 20 ||
120 rate
->rate
== 55 || rate
->rate
== 110 ||
121 rate
->rate
== 60 || rate
->rate
== 120 ||
123 rate
->flags
|= IEEE80211_RATE_MANDATORY
;
125 case NUM_IEEE80211_MODES
:
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
)
138 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
142 fc
= le16_to_cpu(hdr
->frame_control
);
144 switch (fc
& IEEE80211_FCTL_FTYPE
) {
145 case IEEE80211_FTYPE_DATA
:
146 if (len
< 24) /* drop incorrect hdr len (data) */
148 switch (fc
& (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
149 case IEEE80211_FCTL_TODS
:
151 case (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
153 case IEEE80211_FCTL_FROMDS
:
159 case IEEE80211_FTYPE_MGMT
:
160 if (len
< 24) /* drop incorrect hdr len (mgmt) */
163 case IEEE80211_FTYPE_CTL
:
164 if ((fc
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_PSPOLL
)
173 int ieee80211_get_hdrlen(u16 fc
)
177 switch (fc
& IEEE80211_FCTL_FTYPE
) {
178 case IEEE80211_FTYPE_DATA
:
179 if ((fc
& IEEE80211_FCTL_FROMDS
) && (fc
& IEEE80211_FCTL_TODS
))
180 hdrlen
= 30; /* Addr4 */
182 * The QoS Control field is two bytes and its presence is
183 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
184 * hdrlen if that bit is set.
185 * This works by masking out the bit and shifting it to
186 * bit position 1 so the result has the value 0 or 2.
188 hdrlen
+= (fc
& IEEE80211_STYPE_QOS_DATA
)
189 >> (ilog2(IEEE80211_STYPE_QOS_DATA
)-1);
191 case IEEE80211_FTYPE_CTL
:
193 * ACK and CTS are 10 bytes, all others 16. To see how
194 * to get this condition consider
195 * subtype mask: 0b0000000011110000 (0x00F0)
196 * ACK subtype: 0b0000000011010000 (0x00D0)
197 * CTS subtype: 0b0000000011000000 (0x00C0)
198 * bits that matter: ^^^ (0x00E0)
199 * value of those: 0b0000000011000000 (0x00C0)
201 if ((fc
& 0xE0) == 0xC0)
210 EXPORT_SYMBOL(ieee80211_get_hdrlen
);
212 int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
214 const struct ieee80211_hdr
*hdr
= (const struct ieee80211_hdr
*) skb
->data
;
217 if (unlikely(skb
->len
< 10))
219 hdrlen
= ieee80211_get_hdrlen(le16_to_cpu(hdr
->frame_control
));
220 if (unlikely(hdrlen
> skb
->len
))
224 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
226 int ieee80211_is_eapol(const struct sk_buff
*skb
, int hdrlen
)
228 if (unlikely(skb
->len
< 10))
231 if (unlikely(skb
->len
>= hdrlen
+ sizeof(eapol_header
) &&
232 memcmp(skb
->data
+ hdrlen
, eapol_header
,
233 sizeof(eapol_header
)) == 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
;
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
)
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
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
) {
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
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 */
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
);
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
;
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
);
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
,
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
;
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
);
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
);
359 dur
+= ieee80211_frame_duration(local
, 10, rate
->rate
,
360 erp
, short_preamble
);
363 return cpu_to_le16(dur
);
365 EXPORT_SYMBOL(ieee80211_rts_duration
);
367 __le16
ieee80211_ctstoself_duration(struct ieee80211_hw
*hw
, int if_id
,
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
;
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
)) {
393 dur
+= ieee80211_frame_duration(local
, 10, rate
->rate
,
394 erp
, short_preamble
);
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
;
408 list_for_each_entry(mode
, &local
->modes_list
, list
) {
409 if (mode
->mode
!= phymode
)
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
))
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
);
433 if (!ieee80211_qdisc_installed(local
->mdev
)) {
435 netif_wake_queue(local
->mdev
);
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
);
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
)
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
)
477 for (i
= 0; i
< hw
->queues
; i
++)
478 ieee80211_wake_queue(hw
, i
);
480 EXPORT_SYMBOL(ieee80211_wake_queues
);
482 void ieee80211_iterate_active_interfaces(struct ieee80211_hw
*hw
,
483 void (*iterator
)(void *data
, u8
*mac
,
487 struct ieee80211_local
*local
= hw_to_local(hw
);
488 struct ieee80211_sub_if_data
*sdata
;
492 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
493 switch (sdata
->type
) {
494 case IEEE80211_IF_TYPE_INVALID
:
495 case IEEE80211_IF_TYPE_MNTR
:
496 case IEEE80211_IF_TYPE_VLAN
:
498 case IEEE80211_IF_TYPE_AP
:
499 case IEEE80211_IF_TYPE_STA
:
500 case IEEE80211_IF_TYPE_IBSS
:
501 case IEEE80211_IF_TYPE_WDS
:
504 if (sdata
->dev
== local
->mdev
)
506 if (netif_running(sdata
->dev
))
507 iterator(data
, sdata
->dev
->dev_addr
,
508 sdata
->dev
->ifindex
);
513 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces
);