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 cfg80211_registered_device
*rdev
,
145 struct key_params
*params
, int key_idx
,
154 * Disallow pairwise keys with non-zero index unless it's WEP
155 * (because current deployments use pairwise WEP keys with
156 * non-zero indizes but 802.11i clearly specifies to use zero)
158 if (mac_addr
&& key_idx
&&
159 params
->cipher
!= WLAN_CIPHER_SUITE_WEP40
&&
160 params
->cipher
!= WLAN_CIPHER_SUITE_WEP104
)
163 switch (params
->cipher
) {
164 case WLAN_CIPHER_SUITE_WEP40
:
165 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
168 case WLAN_CIPHER_SUITE_TKIP
:
169 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
172 case WLAN_CIPHER_SUITE_CCMP
:
173 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
176 case WLAN_CIPHER_SUITE_WEP104
:
177 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
180 case WLAN_CIPHER_SUITE_AES_CMAC
:
181 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
189 switch (params
->cipher
) {
190 case WLAN_CIPHER_SUITE_WEP40
:
191 case WLAN_CIPHER_SUITE_WEP104
:
192 /* These ciphers do not use key sequence */
194 case WLAN_CIPHER_SUITE_TKIP
:
195 case WLAN_CIPHER_SUITE_CCMP
:
196 case WLAN_CIPHER_SUITE_AES_CMAC
:
197 if (params
->seq_len
!= 6)
203 for (i
= 0; i
< rdev
->wiphy
.n_cipher_suites
; i
++)
204 if (params
->cipher
== rdev
->wiphy
.cipher_suites
[i
])
206 if (i
== rdev
->wiphy
.n_cipher_suites
)
212 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
213 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
214 const unsigned char rfc1042_header
[] __aligned(2) =
215 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
216 EXPORT_SYMBOL(rfc1042_header
);
218 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
219 const unsigned char bridge_tunnel_header
[] __aligned(2) =
220 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
221 EXPORT_SYMBOL(bridge_tunnel_header
);
223 unsigned int ieee80211_hdrlen(__le16 fc
)
225 unsigned int hdrlen
= 24;
227 if (ieee80211_is_data(fc
)) {
228 if (ieee80211_has_a4(fc
))
230 if (ieee80211_is_data_qos(fc
))
231 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
235 if (ieee80211_is_ctl(fc
)) {
237 * ACK and CTS are 10 bytes, all others 16. To see how
238 * to get this condition consider
239 * subtype mask: 0b0000000011110000 (0x00F0)
240 * ACK subtype: 0b0000000011010000 (0x00D0)
241 * CTS subtype: 0b0000000011000000 (0x00C0)
242 * bits that matter: ^^^ (0x00E0)
243 * value of those: 0b0000000011000000 (0x00C0)
245 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
253 EXPORT_SYMBOL(ieee80211_hdrlen
);
255 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
257 const struct ieee80211_hdr
*hdr
=
258 (const struct ieee80211_hdr
*)skb
->data
;
261 if (unlikely(skb
->len
< 10))
263 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
264 if (unlikely(hdrlen
> skb
->len
))
268 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
270 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
272 int ae
= meshhdr
->flags
& MESH_FLAGS_AE
;
277 case MESH_FLAGS_AE_A4
:
279 case MESH_FLAGS_AE_A5_A6
:
281 case (MESH_FLAGS_AE_A4
| MESH_FLAGS_AE_A5_A6
):
288 int ieee80211_data_to_8023(struct sk_buff
*skb
, u8
*addr
,
289 enum nl80211_iftype iftype
)
291 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
292 u16 hdrlen
, ethertype
;
295 u8 src
[ETH_ALEN
] __aligned(2);
297 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
300 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
302 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
304 * IEEE 802.11 address fields:
305 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
306 * 0 0 DA SA BSSID n/a
307 * 0 1 DA BSSID SA n/a
308 * 1 0 BSSID SA DA n/a
311 memcpy(dst
, ieee80211_get_DA(hdr
), ETH_ALEN
);
312 memcpy(src
, ieee80211_get_SA(hdr
), ETH_ALEN
);
314 switch (hdr
->frame_control
&
315 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
316 case cpu_to_le16(IEEE80211_FCTL_TODS
):
317 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
318 iftype
!= NL80211_IFTYPE_AP_VLAN
))
321 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
322 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
323 iftype
!= NL80211_IFTYPE_MESH_POINT
&&
324 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
325 iftype
!= NL80211_IFTYPE_STATION
))
327 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
328 struct ieee80211s_hdr
*meshdr
=
329 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
330 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
331 if (meshdr
->flags
& MESH_FLAGS_AE_A5_A6
) {
332 memcpy(dst
, meshdr
->eaddr1
, ETH_ALEN
);
333 memcpy(src
, meshdr
->eaddr2
, ETH_ALEN
);
337 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
338 if ((iftype
!= NL80211_IFTYPE_STATION
&&
339 iftype
!= NL80211_IFTYPE_MESH_POINT
) ||
340 (is_multicast_ether_addr(dst
) &&
341 !compare_ether_addr(src
, addr
)))
343 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
344 struct ieee80211s_hdr
*meshdr
=
345 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
346 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
347 if (meshdr
->flags
& MESH_FLAGS_AE_A4
)
348 memcpy(src
, meshdr
->eaddr1
, ETH_ALEN
);
352 if (iftype
!= NL80211_IFTYPE_ADHOC
)
357 if (unlikely(skb
->len
- hdrlen
< 8))
360 payload
= skb
->data
+ hdrlen
;
361 ethertype
= (payload
[6] << 8) | payload
[7];
363 if (likely((compare_ether_addr(payload
, rfc1042_header
) == 0 &&
364 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
365 compare_ether_addr(payload
, bridge_tunnel_header
) == 0)) {
366 /* remove RFC1042 or Bridge-Tunnel encapsulation and
367 * replace EtherType */
368 skb_pull(skb
, hdrlen
+ 6);
369 memcpy(skb_push(skb
, ETH_ALEN
), src
, ETH_ALEN
);
370 memcpy(skb_push(skb
, ETH_ALEN
), dst
, ETH_ALEN
);
375 skb_pull(skb
, hdrlen
);
376 len
= htons(skb
->len
);
377 ehdr
= (struct ethhdr
*) skb_push(skb
, sizeof(struct ethhdr
));
378 memcpy(ehdr
->h_dest
, dst
, ETH_ALEN
);
379 memcpy(ehdr
->h_source
, src
, ETH_ALEN
);
384 EXPORT_SYMBOL(ieee80211_data_to_8023
);
386 int ieee80211_data_from_8023(struct sk_buff
*skb
, u8
*addr
,
387 enum nl80211_iftype iftype
, u8
*bssid
, bool qos
)
389 struct ieee80211_hdr hdr
;
390 u16 hdrlen
, ethertype
;
392 const u8
*encaps_data
;
393 int encaps_len
, skip_header_bytes
;
397 if (unlikely(skb
->len
< ETH_HLEN
))
400 nh_pos
= skb_network_header(skb
) - skb
->data
;
401 h_pos
= skb_transport_header(skb
) - skb
->data
;
403 /* convert Ethernet header to proper 802.11 header (based on
405 ethertype
= (skb
->data
[12] << 8) | skb
->data
[13];
406 fc
= cpu_to_le16(IEEE80211_FTYPE_DATA
| IEEE80211_STYPE_DATA
);
409 case NL80211_IFTYPE_AP
:
410 case NL80211_IFTYPE_AP_VLAN
:
411 fc
|= cpu_to_le16(IEEE80211_FCTL_FROMDS
);
413 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
414 memcpy(hdr
.addr2
, addr
, ETH_ALEN
);
415 memcpy(hdr
.addr3
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
418 case NL80211_IFTYPE_STATION
:
419 fc
|= cpu_to_le16(IEEE80211_FCTL_TODS
);
421 memcpy(hdr
.addr1
, bssid
, ETH_ALEN
);
422 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
423 memcpy(hdr
.addr3
, skb
->data
, ETH_ALEN
);
426 case NL80211_IFTYPE_ADHOC
:
428 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
429 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
430 memcpy(hdr
.addr3
, bssid
, ETH_ALEN
);
438 fc
|= cpu_to_le16(IEEE80211_STYPE_QOS_DATA
);
442 hdr
.frame_control
= fc
;
446 skip_header_bytes
= ETH_HLEN
;
447 if (ethertype
== ETH_P_AARP
|| ethertype
== ETH_P_IPX
) {
448 encaps_data
= bridge_tunnel_header
;
449 encaps_len
= sizeof(bridge_tunnel_header
);
450 skip_header_bytes
-= 2;
451 } else if (ethertype
> 0x600) {
452 encaps_data
= rfc1042_header
;
453 encaps_len
= sizeof(rfc1042_header
);
454 skip_header_bytes
-= 2;
460 skb_pull(skb
, skip_header_bytes
);
461 nh_pos
-= skip_header_bytes
;
462 h_pos
-= skip_header_bytes
;
464 head_need
= hdrlen
+ encaps_len
- skb_headroom(skb
);
466 if (head_need
> 0 || skb_cloned(skb
)) {
467 head_need
= max(head_need
, 0);
471 if (pskb_expand_head(skb
, head_need
, 0, GFP_ATOMIC
)) {
472 printk(KERN_ERR
"failed to reallocate Tx buffer\n");
475 skb
->truesize
+= head_need
;
479 memcpy(skb_push(skb
, encaps_len
), encaps_data
, encaps_len
);
480 nh_pos
+= encaps_len
;
484 memcpy(skb_push(skb
, hdrlen
), &hdr
, hdrlen
);
489 /* Update skb pointers to various headers since this modified frame
490 * is going to go through Linux networking code that may potentially
491 * need things like pointer to IP header. */
492 skb_set_mac_header(skb
, 0);
493 skb_set_network_header(skb
, nh_pos
);
494 skb_set_transport_header(skb
, h_pos
);
498 EXPORT_SYMBOL(ieee80211_data_from_8023
);
500 /* Given a data frame determine the 802.1p/1d tag to use. */
501 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
)
505 /* skb->priority values from 256->263 are magic values to
506 * directly indicate a specific 802.1d priority. This is used
507 * to allow 802.1d priority to be passed directly in from VLAN
510 if (skb
->priority
>= 256 && skb
->priority
<= 263)
511 return skb
->priority
- 256;
513 switch (skb
->protocol
) {
514 case htons(ETH_P_IP
):
515 dscp
= ip_hdr(skb
)->tos
& 0xfc;
523 EXPORT_SYMBOL(cfg80211_classify8021d
);
525 const u8
*ieee80211_bss_get_ie(struct cfg80211_bss
*bss
, u8 ie
)
529 pos
= bss
->information_elements
;
532 end
= pos
+ bss
->len_information_elements
;
534 while (pos
+ 1 < end
) {
535 if (pos
+ 2 + pos
[1] > end
)
544 EXPORT_SYMBOL(ieee80211_bss_get_ie
);
546 void cfg80211_upload_connect_keys(struct wireless_dev
*wdev
)
548 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
549 struct net_device
*dev
= wdev
->netdev
;
552 if (!wdev
->connect_keys
)
555 for (i
= 0; i
< 6; i
++) {
556 if (!wdev
->connect_keys
->params
[i
].cipher
)
558 if (rdev
->ops
->add_key(wdev
->wiphy
, dev
, i
, NULL
,
559 &wdev
->connect_keys
->params
[i
])) {
560 printk(KERN_ERR
"%s: failed to set key %d\n",
564 if (wdev
->connect_keys
->def
== i
)
565 if (rdev
->ops
->set_default_key(wdev
->wiphy
, dev
, i
)) {
566 printk(KERN_ERR
"%s: failed to set defkey %d\n",
570 if (wdev
->connect_keys
->defmgmt
== i
)
571 if (rdev
->ops
->set_default_mgmt_key(wdev
->wiphy
, dev
, i
))
572 printk(KERN_ERR
"%s: failed to set mgtdef %d\n",
576 kfree(wdev
->connect_keys
);
577 wdev
->connect_keys
= NULL
;
580 static void cfg80211_process_wdev_events(struct wireless_dev
*wdev
)
582 struct cfg80211_event
*ev
;
584 const u8
*bssid
= NULL
;
586 spin_lock_irqsave(&wdev
->event_lock
, flags
);
587 while (!list_empty(&wdev
->event_list
)) {
588 ev
= list_first_entry(&wdev
->event_list
,
589 struct cfg80211_event
, list
);
591 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
595 case EVENT_CONNECT_RESULT
:
596 if (!is_zero_ether_addr(ev
->cr
.bssid
))
597 bssid
= ev
->cr
.bssid
;
598 __cfg80211_connect_result(
600 ev
->cr
.req_ie
, ev
->cr
.req_ie_len
,
601 ev
->cr
.resp_ie
, ev
->cr
.resp_ie_len
,
603 ev
->cr
.status
== WLAN_STATUS_SUCCESS
,
607 __cfg80211_roamed(wdev
, ev
->rm
.bssid
,
608 ev
->rm
.req_ie
, ev
->rm
.req_ie_len
,
609 ev
->rm
.resp_ie
, ev
->rm
.resp_ie_len
);
611 case EVENT_DISCONNECTED
:
612 __cfg80211_disconnected(wdev
->netdev
,
613 ev
->dc
.ie
, ev
->dc
.ie_len
,
614 ev
->dc
.reason
, true);
616 case EVENT_IBSS_JOINED
:
617 __cfg80211_ibss_joined(wdev
->netdev
, ev
->ij
.bssid
);
624 spin_lock_irqsave(&wdev
->event_lock
, flags
);
626 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
629 void cfg80211_process_rdev_events(struct cfg80211_registered_device
*rdev
)
631 struct wireless_dev
*wdev
;
634 ASSERT_RDEV_LOCK(rdev
);
636 mutex_lock(&rdev
->devlist_mtx
);
638 list_for_each_entry(wdev
, &rdev
->netdev_list
, list
)
639 cfg80211_process_wdev_events(wdev
);
641 mutex_unlock(&rdev
->devlist_mtx
);
644 int cfg80211_change_iface(struct cfg80211_registered_device
*rdev
,
645 struct net_device
*dev
, enum nl80211_iftype ntype
,
646 u32
*flags
, struct vif_params
*params
)
649 enum nl80211_iftype otype
= dev
->ieee80211_ptr
->iftype
;
651 ASSERT_RDEV_LOCK(rdev
);
653 /* don't support changing VLANs, you just re-create them */
654 if (otype
== NL80211_IFTYPE_AP_VLAN
)
657 if (!rdev
->ops
->change_virtual_intf
||
658 !(rdev
->wiphy
.interface_modes
& (1 << ntype
)))
661 /* if it's part of a bridge, reject changing type to station/ibss */
662 if (dev
->br_port
&& (ntype
== NL80211_IFTYPE_ADHOC
||
663 ntype
== NL80211_IFTYPE_STATION
))
666 if (ntype
!= otype
) {
667 dev
->ieee80211_ptr
->use_4addr
= false;
670 case NL80211_IFTYPE_ADHOC
:
671 cfg80211_leave_ibss(rdev
, dev
, false);
673 case NL80211_IFTYPE_STATION
:
674 cfg80211_disconnect(rdev
, dev
,
675 WLAN_REASON_DEAUTH_LEAVING
, true);
677 case NL80211_IFTYPE_MESH_POINT
:
678 /* mesh should be handled? */
684 cfg80211_process_rdev_events(rdev
);
687 err
= rdev
->ops
->change_virtual_intf(&rdev
->wiphy
, dev
,
688 ntype
, flags
, params
);
690 WARN_ON(!err
&& dev
->ieee80211_ptr
->iftype
!= ntype
);
692 if (!err
&& params
&& params
->use_4addr
!= -1)
693 dev
->ieee80211_ptr
->use_4addr
= params
->use_4addr
;
696 dev
->priv_flags
&= ~IFF_DONT_BRIDGE
;
698 case NL80211_IFTYPE_STATION
:
699 if (dev
->ieee80211_ptr
->use_4addr
)
702 case NL80211_IFTYPE_ADHOC
:
703 dev
->priv_flags
|= IFF_DONT_BRIDGE
;
705 case NL80211_IFTYPE_AP
:
706 case NL80211_IFTYPE_AP_VLAN
:
707 case NL80211_IFTYPE_WDS
:
708 case NL80211_IFTYPE_MESH_POINT
:
711 case NL80211_IFTYPE_MONITOR
:
712 /* monitor can't bridge anyway */
714 case NL80211_IFTYPE_UNSPECIFIED
:
715 case __NL80211_IFTYPE_AFTER_LAST
: