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"
32 /* privid for wiphys to determine whether they belong to us or not */
33 void *mac80211_wiphy_privid
= &mac80211_wiphy_privid
;
35 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
36 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
37 const unsigned char rfc1042_header
[] __aligned(2) =
38 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
40 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
41 const unsigned char bridge_tunnel_header
[] __aligned(2) =
42 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
45 u8
*ieee80211_get_bssid(struct ieee80211_hdr
*hdr
, size_t len
,
46 enum nl80211_iftype type
)
48 __le16 fc
= hdr
->frame_control
;
50 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
54 if (ieee80211_is_data(fc
)) {
55 if (len
< 24) /* drop incorrect hdr len (data) */
58 if (ieee80211_has_a4(fc
))
60 if (ieee80211_has_tods(fc
))
62 if (ieee80211_has_fromds(fc
))
68 if (ieee80211_is_mgmt(fc
)) {
69 if (len
< 24) /* drop incorrect hdr len (mgmt) */
74 if (ieee80211_is_ctl(fc
)) {
75 if(ieee80211_is_pspoll(fc
))
78 if (ieee80211_is_back_req(fc
)) {
80 case NL80211_IFTYPE_STATION
:
82 case NL80211_IFTYPE_AP
:
83 case NL80211_IFTYPE_AP_VLAN
:
86 break; /* fall through to the return */
94 unsigned int ieee80211_hdrlen(__le16 fc
)
96 unsigned int hdrlen
= 24;
98 if (ieee80211_is_data(fc
)) {
99 if (ieee80211_has_a4(fc
))
101 if (ieee80211_is_data_qos(fc
))
102 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
106 if (ieee80211_is_ctl(fc
)) {
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)
116 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
124 EXPORT_SYMBOL(ieee80211_hdrlen
);
126 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
128 const struct ieee80211_hdr
*hdr
= (const struct ieee80211_hdr
*)skb
->data
;
131 if (unlikely(skb
->len
< 10))
133 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
134 if (unlikely(hdrlen
> skb
->len
))
138 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
140 int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
142 int ae
= meshhdr
->flags
& IEEE80211S_FLAGS_AE
;
158 void ieee80211_tx_set_protected(struct ieee80211_tx_data
*tx
)
160 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) tx
->skb
->data
;
162 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
163 if (tx
->extra_frag
) {
164 struct ieee80211_hdr
*fhdr
;
166 for (i
= 0; i
< tx
->num_extra_frag
; i
++) {
167 fhdr
= (struct ieee80211_hdr
*)
168 tx
->extra_frag
[i
]->data
;
169 fhdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
174 int ieee80211_frame_duration(struct ieee80211_local
*local
, size_t len
,
175 int rate
, int erp
, int short_preamble
)
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
184 * rate is in 100 kbps, so divident is multiplied by 10 in the
185 * DIV_ROUND_UP() operations.
188 if (local
->hw
.conf
.channel
->band
== IEEE80211_BAND_5GHZ
|| erp
) {
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
198 * 802.11a - 17.5.2: aSIFSTime = 16 usec
199 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
200 * signal ext = 6 usec
202 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 */
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.
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
218 dur
= 10; /* aSIFSTime = 10 usec */
219 dur
+= short_preamble
? (72 + 24) : (144 + 48);
221 dur
+= DIV_ROUND_UP(8 * (len
+ 4) * 10, rate
);
227 /* Exported duration function for driver use */
228 __le16
ieee80211_generic_frame_duration(struct ieee80211_hw
*hw
,
229 struct ieee80211_vif
*vif
,
231 struct ieee80211_rate
*rate
)
233 struct ieee80211_local
*local
= hw_to_local(hw
);
234 struct ieee80211_sub_if_data
*sdata
;
237 bool short_preamble
= false;
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
;
247 dur
= ieee80211_frame_duration(local
, frame_len
, rate
->bitrate
, erp
,
250 return cpu_to_le16(dur
);
252 EXPORT_SYMBOL(ieee80211_generic_frame_duration
);
254 __le16
ieee80211_rts_duration(struct ieee80211_hw
*hw
,
255 struct ieee80211_vif
*vif
, size_t frame_len
,
256 const struct ieee80211_tx_info
*frame_txctl
)
258 struct ieee80211_local
*local
= hw_to_local(hw
);
259 struct ieee80211_rate
*rate
;
260 struct ieee80211_sub_if_data
*sdata
;
264 struct ieee80211_supported_band
*sband
;
266 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
268 short_preamble
= false;
270 rate
= &sband
->bitrates
[frame_txctl
->control
.rts_cts_rate_idx
];
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
;
281 dur
= ieee80211_frame_duration(local
, 10, rate
->bitrate
,
282 erp
, short_preamble
);
283 /* Data frame duration */
284 dur
+= ieee80211_frame_duration(local
, frame_len
, rate
->bitrate
,
285 erp
, short_preamble
);
287 dur
+= ieee80211_frame_duration(local
, 10, rate
->bitrate
,
288 erp
, short_preamble
);
290 return cpu_to_le16(dur
);
292 EXPORT_SYMBOL(ieee80211_rts_duration
);
294 __le16
ieee80211_ctstoself_duration(struct ieee80211_hw
*hw
,
295 struct ieee80211_vif
*vif
,
297 const struct ieee80211_tx_info
*frame_txctl
)
299 struct ieee80211_local
*local
= hw_to_local(hw
);
300 struct ieee80211_rate
*rate
;
301 struct ieee80211_sub_if_data
*sdata
;
305 struct ieee80211_supported_band
*sband
;
307 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
309 short_preamble
= false;
311 rate
= &sband
->bitrates
[frame_txctl
->control
.rts_cts_rate_idx
];
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
;
320 /* Data frame duration */
321 dur
= ieee80211_frame_duration(local
, frame_len
, rate
->bitrate
,
322 erp
, short_preamble
);
323 if (!(frame_txctl
->flags
& IEEE80211_TX_CTL_NO_ACK
)) {
325 dur
+= ieee80211_frame_duration(local
, 10, rate
->bitrate
,
326 erp
, short_preamble
);
329 return cpu_to_le16(dur
);
331 EXPORT_SYMBOL(ieee80211_ctstoself_duration
);
333 void ieee80211_wake_queue(struct ieee80211_hw
*hw
, int queue
)
335 struct ieee80211_local
*local
= hw_to_local(hw
);
337 if (test_bit(queue
, local
->queues_pending
)) {
338 set_bit(queue
, local
->queues_pending_run
);
339 tasklet_schedule(&local
->tx_pending_tasklet
);
341 netif_wake_subqueue(local
->mdev
, queue
);
344 EXPORT_SYMBOL(ieee80211_wake_queue
);
346 void ieee80211_stop_queue(struct ieee80211_hw
*hw
, int queue
)
348 struct ieee80211_local
*local
= hw_to_local(hw
);
350 netif_stop_subqueue(local
->mdev
, queue
);
352 EXPORT_SYMBOL(ieee80211_stop_queue
);
354 void ieee80211_stop_queues(struct ieee80211_hw
*hw
)
358 for (i
= 0; i
< ieee80211_num_queues(hw
); i
++)
359 ieee80211_stop_queue(hw
, i
);
361 EXPORT_SYMBOL(ieee80211_stop_queues
);
363 int ieee80211_queue_stopped(struct ieee80211_hw
*hw
, int queue
)
365 struct ieee80211_local
*local
= hw_to_local(hw
);
366 return __netif_subqueue_stopped(local
->mdev
, queue
);
368 EXPORT_SYMBOL(ieee80211_queue_stopped
);
370 void ieee80211_wake_queues(struct ieee80211_hw
*hw
)
374 for (i
= 0; i
< hw
->queues
+ hw
->ampdu_queues
; i
++)
375 ieee80211_wake_queue(hw
, i
);
377 EXPORT_SYMBOL(ieee80211_wake_queues
);
379 void ieee80211_iterate_active_interfaces(
380 struct ieee80211_hw
*hw
,
381 void (*iterator
)(void *data
, u8
*mac
,
382 struct ieee80211_vif
*vif
),
385 struct ieee80211_local
*local
= hw_to_local(hw
);
386 struct ieee80211_sub_if_data
*sdata
;
390 list_for_each_entry(sdata
, &local
->interfaces
, list
) {
391 switch (sdata
->vif
.type
) {
392 case __NL80211_IFTYPE_AFTER_LAST
:
393 case NL80211_IFTYPE_UNSPECIFIED
:
394 case NL80211_IFTYPE_MONITOR
:
395 case NL80211_IFTYPE_AP_VLAN
:
397 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
:
404 if (netif_running(sdata
->dev
))
405 iterator(data
, sdata
->dev
->dev_addr
,
411 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces
);
413 void ieee80211_iterate_active_interfaces_atomic(
414 struct ieee80211_hw
*hw
,
415 void (*iterator
)(void *data
, u8
*mac
,
416 struct ieee80211_vif
*vif
),
419 struct ieee80211_local
*local
= hw_to_local(hw
);
420 struct ieee80211_sub_if_data
*sdata
;
424 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
425 switch (sdata
->vif
.type
) {
426 case __NL80211_IFTYPE_AFTER_LAST
:
427 case NL80211_IFTYPE_UNSPECIFIED
:
428 case NL80211_IFTYPE_MONITOR
:
429 case NL80211_IFTYPE_AP_VLAN
:
431 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
:
438 if (netif_running(sdata
->dev
))
439 iterator(data
, sdata
->dev
->dev_addr
,
445 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic
);
447 void ieee802_11_parse_elems(u8
*start
, size_t len
,
448 struct ieee802_11_elems
*elems
)
453 memset(elems
, 0, sizeof(*elems
));
454 elems
->ie_start
= start
;
455 elems
->total_len
= len
;
470 elems
->ssid_len
= elen
;
472 case WLAN_EID_SUPP_RATES
:
473 elems
->supp_rates
= pos
;
474 elems
->supp_rates_len
= elen
;
476 case WLAN_EID_FH_PARAMS
:
477 elems
->fh_params
= pos
;
478 elems
->fh_params_len
= elen
;
480 case WLAN_EID_DS_PARAMS
:
481 elems
->ds_params
= pos
;
482 elems
->ds_params_len
= elen
;
484 case WLAN_EID_CF_PARAMS
:
485 elems
->cf_params
= pos
;
486 elems
->cf_params_len
= elen
;
490 elems
->tim_len
= elen
;
492 case WLAN_EID_IBSS_PARAMS
:
493 elems
->ibss_params
= pos
;
494 elems
->ibss_params_len
= elen
;
496 case WLAN_EID_CHALLENGE
:
497 elems
->challenge
= pos
;
498 elems
->challenge_len
= elen
;
501 if (elen
>= 4 && pos
[0] == 0x00 && pos
[1] == 0x50 &&
503 /* Microsoft OUI (00:50:F2) */
505 /* OUI Type 1 - WPA IE */
507 elems
->wpa_len
= elen
;
508 } else if (elen
>= 5 && pos
[3] == 2) {
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
;
521 elems
->rsn_len
= elen
;
523 case WLAN_EID_ERP_INFO
:
524 elems
->erp_info
= pos
;
525 elems
->erp_info_len
= elen
;
527 case WLAN_EID_EXT_SUPP_RATES
:
528 elems
->ext_supp_rates
= pos
;
529 elems
->ext_supp_rates_len
= elen
;
531 case WLAN_EID_HT_CAPABILITY
:
532 if (elen
>= sizeof(struct ieee80211_ht_cap
))
533 elems
->ht_cap_elem
= (void *)pos
;
535 case WLAN_EID_HT_EXTRA_INFO
:
536 if (elen
>= sizeof(struct ieee80211_ht_addt_info
))
537 elems
->ht_info_elem
= (void *)pos
;
539 case WLAN_EID_MESH_ID
:
540 elems
->mesh_id
= pos
;
541 elems
->mesh_id_len
= elen
;
543 case WLAN_EID_MESH_CONFIG
:
544 elems
->mesh_config
= pos
;
545 elems
->mesh_config_len
= elen
;
547 case WLAN_EID_PEER_LINK
:
548 elems
->peer_link
= pos
;
549 elems
->peer_link_len
= elen
;
553 elems
->preq_len
= elen
;
557 elems
->prep_len
= elen
;
561 elems
->perr_len
= elen
;
563 case WLAN_EID_CHANNEL_SWITCH
:
564 elems
->ch_switch_elem
= pos
;
565 elems
->ch_switch_elem_len
= elen
;
568 if (!elems
->quiet_elem
) {
569 elems
->quiet_elem
= pos
;
570 elems
->quiet_elem_len
= elen
;
572 elems
->num_of_quiet_elem
++;
574 case WLAN_EID_COUNTRY
:
575 elems
->country_elem
= pos
;
576 elems
->country_elem_len
= elen
;
578 case WLAN_EID_PWR_CONSTRAINT
:
579 elems
->pwr_constr_elem
= pos
;
580 elems
->pwr_constr_elem_len
= elen
;
591 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data
*sdata
)
593 struct ieee80211_local
*local
= sdata
->local
;
594 struct ieee80211_tx_queue_params qparam
;
597 if (!local
->ops
->conf_tx
)
600 memset(&qparam
, 0, sizeof(qparam
));
604 if (local
->hw
.conf
.channel
->band
== IEEE80211_BAND_2GHZ
&&
605 !(sdata
->flags
& IEEE80211_SDATA_OPERATING_GMODE
))
610 qparam
.cw_max
= 1023;
613 for (i
= 0; i
< local_to_hw(local
)->queues
; i
++)
614 local
->ops
->conf_tx(local_to_hw(local
), i
, &qparam
);
617 void ieee80211_tx_skb(struct ieee80211_sub_if_data
*sdata
, struct sk_buff
*skb
,
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);
625 skb
->iif
= sdata
->dev
->ifindex
;
626 skb
->do_not_encrypt
= !encrypt
;
631 int ieee80211_set_freq(struct ieee80211_sub_if_data
*sdata
, int freqMHz
)
634 struct ieee80211_channel
*chan
;
635 struct ieee80211_local
*local
= sdata
->local
;
637 chan
= ieee80211_get_channel(local
->hw
.wiphy
, freqMHz
);
639 if (chan
&& !(chan
->flags
& IEEE80211_CHAN_DISABLED
)) {
640 if (sdata
->vif
.type
== NL80211_IFTYPE_ADHOC
&&
641 chan
->flags
& IEEE80211_CHAN_NO_IBSS
)
643 local
->oper_channel
= chan
;
645 if (local
->sw_scanning
|| local
->hw_scanning
)
648 ret
= ieee80211_hw_config(local
);
650 rate_control_clear(local
);
656 u64
ieee80211_mandatory_rates(struct ieee80211_local
*local
,
657 enum ieee80211_band band
)
659 struct ieee80211_supported_band
*sband
;
660 struct ieee80211_rate
*bitrates
;
662 enum ieee80211_rate_flags mandatory_flag
;
665 sband
= local
->hw
.wiphy
->bands
[band
];
668 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
671 if (band
== IEEE80211_BAND_2GHZ
)
672 mandatory_flag
= IEEE80211_RATE_MANDATORY_B
;
674 mandatory_flag
= IEEE80211_RATE_MANDATORY_A
;
676 bitrates
= sband
->bitrates
;
678 for (i
= 0; i
< sband
->n_bitrates
; i
++)
679 if (bitrates
[i
].flags
& mandatory_flag
)
680 mandatory_rates
|= BIT(i
);
681 return mandatory_rates
;