2 * Wireless utility functions
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 #include <linux/bitops.h>
7 #include <linux/etherdevice.h>
8 #include <net/cfg80211.h>
12 struct ieee80211_rate
*
13 ieee80211_get_response_rate(struct ieee80211_supported_band
*sband
,
14 u32 basic_rates
, int bitrate
)
16 struct ieee80211_rate
*result
= &sband
->bitrates
[0];
19 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
20 if (!(basic_rates
& BIT(i
)))
22 if (sband
->bitrates
[i
].bitrate
> bitrate
)
24 result
= &sband
->bitrates
[i
];
29 EXPORT_SYMBOL(ieee80211_get_response_rate
);
31 int ieee80211_channel_to_frequency(int chan
)
34 return 2407 + chan
* 5;
39 /* FIXME: 802.11j 17.3.8.3.2 */
40 return (chan
+ 1000) * 5;
42 EXPORT_SYMBOL(ieee80211_channel_to_frequency
);
44 int ieee80211_frequency_to_channel(int freq
)
50 return (freq
- 2407) / 5;
52 /* FIXME: 802.11j 17.3.8.3.2 */
55 EXPORT_SYMBOL(ieee80211_frequency_to_channel
);
57 struct ieee80211_channel
*__ieee80211_get_channel(struct wiphy
*wiphy
,
60 enum ieee80211_band band
;
61 struct ieee80211_supported_band
*sband
;
64 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
65 sband
= wiphy
->bands
[band
];
70 for (i
= 0; i
< sband
->n_channels
; i
++) {
71 if (sband
->channels
[i
].center_freq
== freq
)
72 return &sband
->channels
[i
];
78 EXPORT_SYMBOL(__ieee80211_get_channel
);
80 static void set_mandatory_flags_band(struct ieee80211_supported_band
*sband
,
81 enum ieee80211_band band
)
86 case IEEE80211_BAND_5GHZ
:
88 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
89 if (sband
->bitrates
[i
].bitrate
== 60 ||
90 sband
->bitrates
[i
].bitrate
== 120 ||
91 sband
->bitrates
[i
].bitrate
== 240) {
92 sband
->bitrates
[i
].flags
|=
93 IEEE80211_RATE_MANDATORY_A
;
99 case IEEE80211_BAND_2GHZ
:
101 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
102 if (sband
->bitrates
[i
].bitrate
== 10) {
103 sband
->bitrates
[i
].flags
|=
104 IEEE80211_RATE_MANDATORY_B
|
105 IEEE80211_RATE_MANDATORY_G
;
109 if (sband
->bitrates
[i
].bitrate
== 20 ||
110 sband
->bitrates
[i
].bitrate
== 55 ||
111 sband
->bitrates
[i
].bitrate
== 110 ||
112 sband
->bitrates
[i
].bitrate
== 60 ||
113 sband
->bitrates
[i
].bitrate
== 120 ||
114 sband
->bitrates
[i
].bitrate
== 240) {
115 sband
->bitrates
[i
].flags
|=
116 IEEE80211_RATE_MANDATORY_G
;
120 if (sband
->bitrates
[i
].bitrate
!= 10 &&
121 sband
->bitrates
[i
].bitrate
!= 20 &&
122 sband
->bitrates
[i
].bitrate
!= 55 &&
123 sband
->bitrates
[i
].bitrate
!= 110)
124 sband
->bitrates
[i
].flags
|=
125 IEEE80211_RATE_ERP_G
;
127 WARN_ON(want
!= 0 && want
!= 3 && want
!= 6);
129 case IEEE80211_NUM_BANDS
:
135 void ieee80211_set_bitrate_flags(struct wiphy
*wiphy
)
137 enum ieee80211_band band
;
139 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
140 if (wiphy
->bands
[band
])
141 set_mandatory_flags_band(wiphy
->bands
[band
], band
);
144 int cfg80211_validate_key_settings(struct key_params
*params
, int key_idx
,
151 * Disallow pairwise keys with non-zero index unless it's WEP
152 * (because current deployments use pairwise WEP keys with
153 * non-zero indizes but 802.11i clearly specifies to use zero)
155 if (mac_addr
&& key_idx
&&
156 params
->cipher
!= WLAN_CIPHER_SUITE_WEP40
&&
157 params
->cipher
!= WLAN_CIPHER_SUITE_WEP104
)
160 switch (params
->cipher
) {
161 case WLAN_CIPHER_SUITE_WEP40
:
162 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
165 case WLAN_CIPHER_SUITE_TKIP
:
166 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
169 case WLAN_CIPHER_SUITE_CCMP
:
170 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
173 case WLAN_CIPHER_SUITE_WEP104
:
174 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
177 case WLAN_CIPHER_SUITE_AES_CMAC
:
178 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
186 switch (params
->cipher
) {
187 case WLAN_CIPHER_SUITE_WEP40
:
188 case WLAN_CIPHER_SUITE_WEP104
:
189 /* These ciphers do not use key sequence */
191 case WLAN_CIPHER_SUITE_TKIP
:
192 case WLAN_CIPHER_SUITE_CCMP
:
193 case WLAN_CIPHER_SUITE_AES_CMAC
:
194 if (params
->seq_len
!= 6)
203 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
204 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
205 const unsigned char rfc1042_header
[] __aligned(2) =
206 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
207 EXPORT_SYMBOL(rfc1042_header
);
209 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
210 const unsigned char bridge_tunnel_header
[] __aligned(2) =
211 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
212 EXPORT_SYMBOL(bridge_tunnel_header
);
214 unsigned int ieee80211_hdrlen(__le16 fc
)
216 unsigned int hdrlen
= 24;
218 if (ieee80211_is_data(fc
)) {
219 if (ieee80211_has_a4(fc
))
221 if (ieee80211_is_data_qos(fc
))
222 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
226 if (ieee80211_is_ctl(fc
)) {
228 * ACK and CTS are 10 bytes, all others 16. To see how
229 * to get this condition consider
230 * subtype mask: 0b0000000011110000 (0x00F0)
231 * ACK subtype: 0b0000000011010000 (0x00D0)
232 * CTS subtype: 0b0000000011000000 (0x00C0)
233 * bits that matter: ^^^ (0x00E0)
234 * value of those: 0b0000000011000000 (0x00C0)
236 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
244 EXPORT_SYMBOL(ieee80211_hdrlen
);
246 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
248 const struct ieee80211_hdr
*hdr
=
249 (const struct ieee80211_hdr
*)skb
->data
;
252 if (unlikely(skb
->len
< 10))
254 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
255 if (unlikely(hdrlen
> skb
->len
))
259 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
261 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
263 int ae
= meshhdr
->flags
& MESH_FLAGS_AE
;
279 int ieee80211_data_to_8023(struct sk_buff
*skb
, u8
*addr
,
280 enum nl80211_iftype iftype
)
282 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
283 u16 hdrlen
, ethertype
;
286 u8 src
[ETH_ALEN
] __aligned(2);
288 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
291 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
293 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
295 * IEEE 802.11 address fields:
296 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
297 * 0 0 DA SA BSSID n/a
298 * 0 1 DA BSSID SA n/a
299 * 1 0 BSSID SA DA n/a
302 memcpy(dst
, ieee80211_get_DA(hdr
), ETH_ALEN
);
303 memcpy(src
, ieee80211_get_SA(hdr
), ETH_ALEN
);
305 switch (hdr
->frame_control
&
306 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
307 case cpu_to_le16(IEEE80211_FCTL_TODS
):
308 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
309 iftype
!= NL80211_IFTYPE_AP_VLAN
))
312 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
313 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
314 iftype
!= NL80211_IFTYPE_MESH_POINT
))
316 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
317 struct ieee80211s_hdr
*meshdr
=
318 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
319 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
320 if (meshdr
->flags
& MESH_FLAGS_AE_A5_A6
) {
321 memcpy(dst
, meshdr
->eaddr1
, ETH_ALEN
);
322 memcpy(src
, meshdr
->eaddr2
, ETH_ALEN
);
326 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
327 if (iftype
!= NL80211_IFTYPE_STATION
||
328 (is_multicast_ether_addr(dst
) &&
329 !compare_ether_addr(src
, addr
)))
333 if (iftype
!= NL80211_IFTYPE_ADHOC
)
338 if (unlikely(skb
->len
- hdrlen
< 8))
341 payload
= skb
->data
+ hdrlen
;
342 ethertype
= (payload
[6] << 8) | payload
[7];
344 if (likely((compare_ether_addr(payload
, rfc1042_header
) == 0 &&
345 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
346 compare_ether_addr(payload
, bridge_tunnel_header
) == 0)) {
347 /* remove RFC1042 or Bridge-Tunnel encapsulation and
348 * replace EtherType */
349 skb_pull(skb
, hdrlen
+ 6);
350 memcpy(skb_push(skb
, ETH_ALEN
), src
, ETH_ALEN
);
351 memcpy(skb_push(skb
, ETH_ALEN
), dst
, ETH_ALEN
);
356 skb_pull(skb
, hdrlen
);
357 len
= htons(skb
->len
);
358 ehdr
= (struct ethhdr
*) skb_push(skb
, sizeof(struct ethhdr
));
359 memcpy(ehdr
->h_dest
, dst
, ETH_ALEN
);
360 memcpy(ehdr
->h_source
, src
, ETH_ALEN
);
365 EXPORT_SYMBOL(ieee80211_data_to_8023
);
367 int ieee80211_data_from_8023(struct sk_buff
*skb
, u8
*addr
,
368 enum nl80211_iftype iftype
, u8
*bssid
, bool qos
)
370 struct ieee80211_hdr hdr
;
371 u16 hdrlen
, ethertype
;
373 const u8
*encaps_data
;
374 int encaps_len
, skip_header_bytes
;
378 if (unlikely(skb
->len
< ETH_HLEN
))
381 nh_pos
= skb_network_header(skb
) - skb
->data
;
382 h_pos
= skb_transport_header(skb
) - skb
->data
;
384 /* convert Ethernet header to proper 802.11 header (based on
386 ethertype
= (skb
->data
[12] << 8) | skb
->data
[13];
387 fc
= cpu_to_le16(IEEE80211_FTYPE_DATA
| IEEE80211_STYPE_DATA
);
390 case NL80211_IFTYPE_AP
:
391 case NL80211_IFTYPE_AP_VLAN
:
392 fc
|= cpu_to_le16(IEEE80211_FCTL_FROMDS
);
394 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
395 memcpy(hdr
.addr2
, addr
, ETH_ALEN
);
396 memcpy(hdr
.addr3
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
399 case NL80211_IFTYPE_STATION
:
400 fc
|= cpu_to_le16(IEEE80211_FCTL_TODS
);
402 memcpy(hdr
.addr1
, bssid
, ETH_ALEN
);
403 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
404 memcpy(hdr
.addr3
, skb
->data
, ETH_ALEN
);
407 case NL80211_IFTYPE_ADHOC
:
409 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
410 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
411 memcpy(hdr
.addr3
, bssid
, ETH_ALEN
);
419 fc
|= cpu_to_le16(IEEE80211_STYPE_QOS_DATA
);
423 hdr
.frame_control
= fc
;
427 skip_header_bytes
= ETH_HLEN
;
428 if (ethertype
== ETH_P_AARP
|| ethertype
== ETH_P_IPX
) {
429 encaps_data
= bridge_tunnel_header
;
430 encaps_len
= sizeof(bridge_tunnel_header
);
431 skip_header_bytes
-= 2;
432 } else if (ethertype
> 0x600) {
433 encaps_data
= rfc1042_header
;
434 encaps_len
= sizeof(rfc1042_header
);
435 skip_header_bytes
-= 2;
441 skb_pull(skb
, skip_header_bytes
);
442 nh_pos
-= skip_header_bytes
;
443 h_pos
-= skip_header_bytes
;
445 head_need
= hdrlen
+ encaps_len
- skb_headroom(skb
);
447 if (head_need
> 0 || skb_cloned(skb
)) {
448 head_need
= max(head_need
, 0);
452 if (pskb_expand_head(skb
, head_need
, 0, GFP_ATOMIC
)) {
453 printk(KERN_ERR
"failed to reallocate Tx buffer\n");
456 skb
->truesize
+= head_need
;
460 memcpy(skb_push(skb
, encaps_len
), encaps_data
, encaps_len
);
461 nh_pos
+= encaps_len
;
465 memcpy(skb_push(skb
, hdrlen
), &hdr
, hdrlen
);
470 /* Update skb pointers to various headers since this modified frame
471 * is going to go through Linux networking code that may potentially
472 * need things like pointer to IP header. */
473 skb_set_mac_header(skb
, 0);
474 skb_set_network_header(skb
, nh_pos
);
475 skb_set_transport_header(skb
, h_pos
);
479 EXPORT_SYMBOL(ieee80211_data_from_8023
);
481 /* Given a data frame determine the 802.1p/1d tag to use. */
482 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
)
486 /* skb->priority values from 256->263 are magic values to
487 * directly indicate a specific 802.1d priority. This is used
488 * to allow 802.1d priority to be passed directly in from VLAN
491 if (skb
->priority
>= 256 && skb
->priority
<= 263)
492 return skb
->priority
- 256;
494 switch (skb
->protocol
) {
495 case htons(ETH_P_IP
):
496 dscp
= ip_hdr(skb
)->tos
& 0xfc;
504 EXPORT_SYMBOL(cfg80211_classify8021d
);