2 * Wireless utility functions
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 #include <linux/export.h>
7 #include <linux/bitops.h>
8 #include <linux/etherdevice.h>
9 #include <linux/slab.h>
10 #include <net/cfg80211.h>
12 #include <net/dsfield.h>
13 #include <linux/if_vlan.h>
18 struct ieee80211_rate
*
19 ieee80211_get_response_rate(struct ieee80211_supported_band
*sband
,
20 u32 basic_rates
, int bitrate
)
22 struct ieee80211_rate
*result
= &sband
->bitrates
[0];
25 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
26 if (!(basic_rates
& BIT(i
)))
28 if (sband
->bitrates
[i
].bitrate
> bitrate
)
30 result
= &sband
->bitrates
[i
];
35 EXPORT_SYMBOL(ieee80211_get_response_rate
);
37 u32
ieee80211_mandatory_rates(struct ieee80211_supported_band
*sband
,
38 enum nl80211_bss_scan_width scan_width
)
40 struct ieee80211_rate
*bitrates
;
41 u32 mandatory_rates
= 0;
42 enum ieee80211_rate_flags mandatory_flag
;
48 if (sband
->band
== IEEE80211_BAND_2GHZ
) {
49 if (scan_width
== NL80211_BSS_CHAN_WIDTH_5
||
50 scan_width
== NL80211_BSS_CHAN_WIDTH_10
)
51 mandatory_flag
= IEEE80211_RATE_MANDATORY_G
;
53 mandatory_flag
= IEEE80211_RATE_MANDATORY_B
;
55 mandatory_flag
= IEEE80211_RATE_MANDATORY_A
;
58 bitrates
= sband
->bitrates
;
59 for (i
= 0; i
< sband
->n_bitrates
; i
++)
60 if (bitrates
[i
].flags
& mandatory_flag
)
61 mandatory_rates
|= BIT(i
);
62 return mandatory_rates
;
64 EXPORT_SYMBOL(ieee80211_mandatory_rates
);
66 int ieee80211_channel_to_frequency(int chan
, enum ieee80211_band band
)
68 /* see 802.11 17.3.8.3.2 and Annex J
69 * there are overlapping channel numbers in 5GHz and 2GHz bands */
71 return 0; /* not supported */
73 case IEEE80211_BAND_2GHZ
:
77 return 2407 + chan
* 5;
79 case IEEE80211_BAND_5GHZ
:
80 if (chan
>= 182 && chan
<= 196)
81 return 4000 + chan
* 5;
83 return 5000 + chan
* 5;
85 case IEEE80211_BAND_60GHZ
:
87 return 56160 + chan
* 2160;
92 return 0; /* not supported */
94 EXPORT_SYMBOL(ieee80211_channel_to_frequency
);
96 int ieee80211_frequency_to_channel(int freq
)
98 /* see 802.11 17.3.8.3.2 and Annex J */
101 else if (freq
< 2484)
102 return (freq
- 2407) / 5;
103 else if (freq
>= 4910 && freq
<= 4980)
104 return (freq
- 4000) / 5;
105 else if (freq
<= 45000) /* DMG band lower limit */
106 return (freq
- 5000) / 5;
107 else if (freq
>= 58320 && freq
<= 64800)
108 return (freq
- 56160) / 2160;
112 EXPORT_SYMBOL(ieee80211_frequency_to_channel
);
114 struct ieee80211_channel
*__ieee80211_get_channel(struct wiphy
*wiphy
,
117 enum ieee80211_band band
;
118 struct ieee80211_supported_band
*sband
;
121 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
122 sband
= wiphy
->bands
[band
];
127 for (i
= 0; i
< sband
->n_channels
; i
++) {
128 if (sband
->channels
[i
].center_freq
== freq
)
129 return &sband
->channels
[i
];
135 EXPORT_SYMBOL(__ieee80211_get_channel
);
137 static void set_mandatory_flags_band(struct ieee80211_supported_band
*sband
,
138 enum ieee80211_band band
)
143 case IEEE80211_BAND_5GHZ
:
145 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
146 if (sband
->bitrates
[i
].bitrate
== 60 ||
147 sband
->bitrates
[i
].bitrate
== 120 ||
148 sband
->bitrates
[i
].bitrate
== 240) {
149 sband
->bitrates
[i
].flags
|=
150 IEEE80211_RATE_MANDATORY_A
;
156 case IEEE80211_BAND_2GHZ
:
158 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
159 if (sband
->bitrates
[i
].bitrate
== 10) {
160 sband
->bitrates
[i
].flags
|=
161 IEEE80211_RATE_MANDATORY_B
|
162 IEEE80211_RATE_MANDATORY_G
;
166 if (sband
->bitrates
[i
].bitrate
== 20 ||
167 sband
->bitrates
[i
].bitrate
== 55 ||
168 sband
->bitrates
[i
].bitrate
== 110 ||
169 sband
->bitrates
[i
].bitrate
== 60 ||
170 sband
->bitrates
[i
].bitrate
== 120 ||
171 sband
->bitrates
[i
].bitrate
== 240) {
172 sband
->bitrates
[i
].flags
|=
173 IEEE80211_RATE_MANDATORY_G
;
177 if (sband
->bitrates
[i
].bitrate
!= 10 &&
178 sband
->bitrates
[i
].bitrate
!= 20 &&
179 sband
->bitrates
[i
].bitrate
!= 55 &&
180 sband
->bitrates
[i
].bitrate
!= 110)
181 sband
->bitrates
[i
].flags
|=
182 IEEE80211_RATE_ERP_G
;
184 WARN_ON(want
!= 0 && want
!= 3 && want
!= 6);
186 case IEEE80211_BAND_60GHZ
:
187 /* check for mandatory HT MCS 1..4 */
188 WARN_ON(!sband
->ht_cap
.ht_supported
);
189 WARN_ON((sband
->ht_cap
.mcs
.rx_mask
[0] & 0x1e) != 0x1e);
191 case IEEE80211_NUM_BANDS
:
197 void ieee80211_set_bitrate_flags(struct wiphy
*wiphy
)
199 enum ieee80211_band band
;
201 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
202 if (wiphy
->bands
[band
])
203 set_mandatory_flags_band(wiphy
->bands
[band
], band
);
206 bool cfg80211_supported_cipher_suite(struct wiphy
*wiphy
, u32 cipher
)
209 for (i
= 0; i
< wiphy
->n_cipher_suites
; i
++)
210 if (cipher
== wiphy
->cipher_suites
[i
])
215 int cfg80211_validate_key_settings(struct cfg80211_registered_device
*rdev
,
216 struct key_params
*params
, int key_idx
,
217 bool pairwise
, const u8
*mac_addr
)
222 if (!pairwise
&& mac_addr
&& !(rdev
->wiphy
.flags
& WIPHY_FLAG_IBSS_RSN
))
225 if (pairwise
&& !mac_addr
)
229 * Disallow pairwise keys with non-zero index unless it's WEP
230 * or a vendor specific cipher (because current deployments use
231 * pairwise WEP keys with non-zero indices and for vendor specific
232 * ciphers this should be validated in the driver or hardware level
233 * - but 802.11i clearly specifies to use zero)
235 if (pairwise
&& key_idx
&&
236 ((params
->cipher
== WLAN_CIPHER_SUITE_TKIP
) ||
237 (params
->cipher
== WLAN_CIPHER_SUITE_CCMP
) ||
238 (params
->cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)))
241 switch (params
->cipher
) {
242 case WLAN_CIPHER_SUITE_WEP40
:
243 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
246 case WLAN_CIPHER_SUITE_TKIP
:
247 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
250 case WLAN_CIPHER_SUITE_CCMP
:
251 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
254 case WLAN_CIPHER_SUITE_WEP104
:
255 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
258 case WLAN_CIPHER_SUITE_AES_CMAC
:
259 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
264 * We don't know anything about this algorithm,
265 * allow using it -- but the driver must check
266 * all parameters! We still check below whether
267 * or not the driver supports this algorithm,
274 switch (params
->cipher
) {
275 case WLAN_CIPHER_SUITE_WEP40
:
276 case WLAN_CIPHER_SUITE_WEP104
:
277 /* These ciphers do not use key sequence */
279 case WLAN_CIPHER_SUITE_TKIP
:
280 case WLAN_CIPHER_SUITE_CCMP
:
281 case WLAN_CIPHER_SUITE_AES_CMAC
:
282 if (params
->seq_len
!= 6)
288 if (!cfg80211_supported_cipher_suite(&rdev
->wiphy
, params
->cipher
))
294 unsigned int __attribute_const__
ieee80211_hdrlen(__le16 fc
)
296 unsigned int hdrlen
= 24;
298 if (ieee80211_is_data(fc
)) {
299 if (ieee80211_has_a4(fc
))
301 if (ieee80211_is_data_qos(fc
)) {
302 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
303 if (ieee80211_has_order(fc
))
304 hdrlen
+= IEEE80211_HT_CTL_LEN
;
309 if (ieee80211_is_ctl(fc
)) {
311 * ACK and CTS are 10 bytes, all others 16. To see how
312 * to get this condition consider
313 * subtype mask: 0b0000000011110000 (0x00F0)
314 * ACK subtype: 0b0000000011010000 (0x00D0)
315 * CTS subtype: 0b0000000011000000 (0x00C0)
316 * bits that matter: ^^^ (0x00E0)
317 * value of those: 0b0000000011000000 (0x00C0)
319 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
327 EXPORT_SYMBOL(ieee80211_hdrlen
);
329 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
331 const struct ieee80211_hdr
*hdr
=
332 (const struct ieee80211_hdr
*)skb
->data
;
335 if (unlikely(skb
->len
< 10))
337 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
338 if (unlikely(hdrlen
> skb
->len
))
342 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
344 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
346 int ae
= meshhdr
->flags
& MESH_FLAGS_AE
;
347 /* 802.11-2012, 8.2.4.7.3 */
352 case MESH_FLAGS_AE_A4
:
354 case MESH_FLAGS_AE_A5_A6
:
358 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen
);
360 int ieee80211_data_to_8023(struct sk_buff
*skb
, const u8
*addr
,
361 enum nl80211_iftype iftype
)
363 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
364 u16 hdrlen
, ethertype
;
367 u8 src
[ETH_ALEN
] __aligned(2);
369 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
372 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
374 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
376 * IEEE 802.11 address fields:
377 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
378 * 0 0 DA SA BSSID n/a
379 * 0 1 DA BSSID SA n/a
380 * 1 0 BSSID SA DA n/a
383 memcpy(dst
, ieee80211_get_DA(hdr
), ETH_ALEN
);
384 memcpy(src
, ieee80211_get_SA(hdr
), ETH_ALEN
);
386 switch (hdr
->frame_control
&
387 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
388 case cpu_to_le16(IEEE80211_FCTL_TODS
):
389 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
390 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
391 iftype
!= NL80211_IFTYPE_P2P_GO
))
394 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
395 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
396 iftype
!= NL80211_IFTYPE_MESH_POINT
&&
397 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
398 iftype
!= NL80211_IFTYPE_STATION
))
400 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
401 struct ieee80211s_hdr
*meshdr
=
402 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
403 /* make sure meshdr->flags is on the linear part */
404 if (!pskb_may_pull(skb
, hdrlen
+ 1))
406 if (meshdr
->flags
& MESH_FLAGS_AE_A4
)
408 if (meshdr
->flags
& MESH_FLAGS_AE_A5_A6
) {
409 skb_copy_bits(skb
, hdrlen
+
410 offsetof(struct ieee80211s_hdr
, eaddr1
),
412 skb_copy_bits(skb
, hdrlen
+
413 offsetof(struct ieee80211s_hdr
, eaddr2
),
416 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
419 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
420 if ((iftype
!= NL80211_IFTYPE_STATION
&&
421 iftype
!= NL80211_IFTYPE_P2P_CLIENT
&&
422 iftype
!= NL80211_IFTYPE_MESH_POINT
) ||
423 (is_multicast_ether_addr(dst
) &&
424 ether_addr_equal(src
, addr
)))
426 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
427 struct ieee80211s_hdr
*meshdr
=
428 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
429 /* make sure meshdr->flags is on the linear part */
430 if (!pskb_may_pull(skb
, hdrlen
+ 1))
432 if (meshdr
->flags
& MESH_FLAGS_AE_A5_A6
)
434 if (meshdr
->flags
& MESH_FLAGS_AE_A4
)
435 skb_copy_bits(skb
, hdrlen
+
436 offsetof(struct ieee80211s_hdr
, eaddr1
),
438 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
442 if (iftype
!= NL80211_IFTYPE_ADHOC
&&
443 iftype
!= NL80211_IFTYPE_STATION
)
448 if (!pskb_may_pull(skb
, hdrlen
+ 8))
451 payload
= skb
->data
+ hdrlen
;
452 ethertype
= (payload
[6] << 8) | payload
[7];
454 if (likely((ether_addr_equal(payload
, rfc1042_header
) &&
455 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
456 ether_addr_equal(payload
, bridge_tunnel_header
))) {
457 /* remove RFC1042 or Bridge-Tunnel encapsulation and
458 * replace EtherType */
459 skb_pull(skb
, hdrlen
+ 6);
460 memcpy(skb_push(skb
, ETH_ALEN
), src
, ETH_ALEN
);
461 memcpy(skb_push(skb
, ETH_ALEN
), dst
, ETH_ALEN
);
466 skb_pull(skb
, hdrlen
);
467 len
= htons(skb
->len
);
468 ehdr
= (struct ethhdr
*) skb_push(skb
, sizeof(struct ethhdr
));
469 memcpy(ehdr
->h_dest
, dst
, ETH_ALEN
);
470 memcpy(ehdr
->h_source
, src
, ETH_ALEN
);
475 EXPORT_SYMBOL(ieee80211_data_to_8023
);
477 int ieee80211_data_from_8023(struct sk_buff
*skb
, const u8
*addr
,
478 enum nl80211_iftype iftype
, u8
*bssid
, bool qos
)
480 struct ieee80211_hdr hdr
;
481 u16 hdrlen
, ethertype
;
483 const u8
*encaps_data
;
484 int encaps_len
, skip_header_bytes
;
488 if (unlikely(skb
->len
< ETH_HLEN
))
491 nh_pos
= skb_network_header(skb
) - skb
->data
;
492 h_pos
= skb_transport_header(skb
) - skb
->data
;
494 /* convert Ethernet header to proper 802.11 header (based on
496 ethertype
= (skb
->data
[12] << 8) | skb
->data
[13];
497 fc
= cpu_to_le16(IEEE80211_FTYPE_DATA
| IEEE80211_STYPE_DATA
);
500 case NL80211_IFTYPE_AP
:
501 case NL80211_IFTYPE_AP_VLAN
:
502 case NL80211_IFTYPE_P2P_GO
:
503 fc
|= cpu_to_le16(IEEE80211_FCTL_FROMDS
);
505 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
506 memcpy(hdr
.addr2
, addr
, ETH_ALEN
);
507 memcpy(hdr
.addr3
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
510 case NL80211_IFTYPE_STATION
:
511 case NL80211_IFTYPE_P2P_CLIENT
:
512 fc
|= cpu_to_le16(IEEE80211_FCTL_TODS
);
514 memcpy(hdr
.addr1
, bssid
, ETH_ALEN
);
515 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
516 memcpy(hdr
.addr3
, skb
->data
, ETH_ALEN
);
519 case NL80211_IFTYPE_ADHOC
:
521 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
522 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
523 memcpy(hdr
.addr3
, bssid
, ETH_ALEN
);
531 fc
|= cpu_to_le16(IEEE80211_STYPE_QOS_DATA
);
535 hdr
.frame_control
= fc
;
539 skip_header_bytes
= ETH_HLEN
;
540 if (ethertype
== ETH_P_AARP
|| ethertype
== ETH_P_IPX
) {
541 encaps_data
= bridge_tunnel_header
;
542 encaps_len
= sizeof(bridge_tunnel_header
);
543 skip_header_bytes
-= 2;
544 } else if (ethertype
>= ETH_P_802_3_MIN
) {
545 encaps_data
= rfc1042_header
;
546 encaps_len
= sizeof(rfc1042_header
);
547 skip_header_bytes
-= 2;
553 skb_pull(skb
, skip_header_bytes
);
554 nh_pos
-= skip_header_bytes
;
555 h_pos
-= skip_header_bytes
;
557 head_need
= hdrlen
+ encaps_len
- skb_headroom(skb
);
559 if (head_need
> 0 || skb_cloned(skb
)) {
560 head_need
= max(head_need
, 0);
564 if (pskb_expand_head(skb
, head_need
, 0, GFP_ATOMIC
))
567 skb
->truesize
+= head_need
;
571 memcpy(skb_push(skb
, encaps_len
), encaps_data
, encaps_len
);
572 nh_pos
+= encaps_len
;
576 memcpy(skb_push(skb
, hdrlen
), &hdr
, hdrlen
);
581 /* Update skb pointers to various headers since this modified frame
582 * is going to go through Linux networking code that may potentially
583 * need things like pointer to IP header. */
584 skb_set_mac_header(skb
, 0);
585 skb_set_network_header(skb
, nh_pos
);
586 skb_set_transport_header(skb
, h_pos
);
590 EXPORT_SYMBOL(ieee80211_data_from_8023
);
593 void ieee80211_amsdu_to_8023s(struct sk_buff
*skb
, struct sk_buff_head
*list
,
594 const u8
*addr
, enum nl80211_iftype iftype
,
595 const unsigned int extra_headroom
,
596 bool has_80211_header
)
598 struct sk_buff
*frame
= NULL
;
601 const struct ethhdr
*eth
;
603 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
605 if (has_80211_header
) {
606 err
= ieee80211_data_to_8023(skb
, addr
, iftype
);
610 /* skip the wrapping header */
611 eth
= (struct ethhdr
*) skb_pull(skb
, sizeof(struct ethhdr
));
615 eth
= (struct ethhdr
*) skb
->data
;
618 while (skb
!= frame
) {
620 __be16 len
= eth
->h_proto
;
621 unsigned int subframe_len
= sizeof(struct ethhdr
) + ntohs(len
);
623 remaining
= skb
->len
;
624 memcpy(dst
, eth
->h_dest
, ETH_ALEN
);
625 memcpy(src
, eth
->h_source
, ETH_ALEN
);
627 padding
= (4 - subframe_len
) & 0x3;
628 /* the last MSDU has no padding */
629 if (subframe_len
> remaining
)
632 skb_pull(skb
, sizeof(struct ethhdr
));
633 /* reuse skb for the last subframe */
634 if (remaining
<= subframe_len
+ padding
)
637 unsigned int hlen
= ALIGN(extra_headroom
, 4);
639 * Allocate and reserve two bytes more for payload
640 * alignment since sizeof(struct ethhdr) is 14.
642 frame
= dev_alloc_skb(hlen
+ subframe_len
+ 2);
646 skb_reserve(frame
, hlen
+ sizeof(struct ethhdr
) + 2);
647 memcpy(skb_put(frame
, ntohs(len
)), skb
->data
,
650 eth
= (struct ethhdr
*)skb_pull(skb
, ntohs(len
) +
653 dev_kfree_skb(frame
);
658 skb_reset_network_header(frame
);
659 frame
->dev
= skb
->dev
;
660 frame
->priority
= skb
->priority
;
662 payload
= frame
->data
;
663 ethertype
= (payload
[6] << 8) | payload
[7];
665 if (likely((ether_addr_equal(payload
, rfc1042_header
) &&
666 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
667 ether_addr_equal(payload
, bridge_tunnel_header
))) {
668 /* remove RFC1042 or Bridge-Tunnel
669 * encapsulation and replace EtherType */
671 memcpy(skb_push(frame
, ETH_ALEN
), src
, ETH_ALEN
);
672 memcpy(skb_push(frame
, ETH_ALEN
), dst
, ETH_ALEN
);
674 memcpy(skb_push(frame
, sizeof(__be16
)), &len
,
676 memcpy(skb_push(frame
, ETH_ALEN
), src
, ETH_ALEN
);
677 memcpy(skb_push(frame
, ETH_ALEN
), dst
, ETH_ALEN
);
679 __skb_queue_tail(list
, frame
);
685 __skb_queue_purge(list
);
689 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s
);
691 /* Given a data frame determine the 802.1p/1d tag to use. */
692 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
,
693 struct cfg80211_qos_map
*qos_map
)
696 unsigned char vlan_priority
;
698 /* skb->priority values from 256->263 are magic values to
699 * directly indicate a specific 802.1d priority. This is used
700 * to allow 802.1d priority to be passed directly in from VLAN
703 if (skb
->priority
>= 256 && skb
->priority
<= 263)
704 return skb
->priority
- 256;
706 if (vlan_tx_tag_present(skb
)) {
707 vlan_priority
= (vlan_tx_tag_get(skb
) & VLAN_PRIO_MASK
)
709 if (vlan_priority
> 0)
710 return vlan_priority
;
713 switch (skb
->protocol
) {
714 case htons(ETH_P_IP
):
715 dscp
= ipv4_get_dsfield(ip_hdr(skb
)) & 0xfc;
717 case htons(ETH_P_IPV6
):
718 dscp
= ipv6_get_dsfield(ipv6_hdr(skb
)) & 0xfc;
725 unsigned int i
, tmp_dscp
= dscp
>> 2;
727 for (i
= 0; i
< qos_map
->num_des
; i
++) {
728 if (tmp_dscp
== qos_map
->dscp_exception
[i
].dscp
)
729 return qos_map
->dscp_exception
[i
].up
;
732 for (i
= 0; i
< 8; i
++) {
733 if (tmp_dscp
>= qos_map
->up
[i
].low
&&
734 tmp_dscp
<= qos_map
->up
[i
].high
)
741 EXPORT_SYMBOL(cfg80211_classify8021d
);
743 const u8
*ieee80211_bss_get_ie(struct cfg80211_bss
*bss
, u8 ie
)
745 const struct cfg80211_bss_ies
*ies
;
747 ies
= rcu_dereference(bss
->ies
);
751 return cfg80211_find_ie(ie
, ies
->data
, ies
->len
);
753 EXPORT_SYMBOL(ieee80211_bss_get_ie
);
755 void cfg80211_upload_connect_keys(struct wireless_dev
*wdev
)
757 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
758 struct net_device
*dev
= wdev
->netdev
;
761 if (!wdev
->connect_keys
)
764 for (i
= 0; i
< 6; i
++) {
765 if (!wdev
->connect_keys
->params
[i
].cipher
)
767 if (rdev_add_key(rdev
, dev
, i
, false, NULL
,
768 &wdev
->connect_keys
->params
[i
])) {
769 netdev_err(dev
, "failed to set key %d\n", i
);
772 if (wdev
->connect_keys
->def
== i
)
773 if (rdev_set_default_key(rdev
, dev
, i
, true, true)) {
774 netdev_err(dev
, "failed to set defkey %d\n", i
);
777 if (wdev
->connect_keys
->defmgmt
== i
)
778 if (rdev_set_default_mgmt_key(rdev
, dev
, i
))
779 netdev_err(dev
, "failed to set mgtdef %d\n", i
);
782 kfree(wdev
->connect_keys
);
783 wdev
->connect_keys
= NULL
;
786 void cfg80211_process_wdev_events(struct wireless_dev
*wdev
)
788 struct cfg80211_event
*ev
;
790 const u8
*bssid
= NULL
;
792 spin_lock_irqsave(&wdev
->event_lock
, flags
);
793 while (!list_empty(&wdev
->event_list
)) {
794 ev
= list_first_entry(&wdev
->event_list
,
795 struct cfg80211_event
, list
);
797 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
801 case EVENT_CONNECT_RESULT
:
802 if (!is_zero_ether_addr(ev
->cr
.bssid
))
803 bssid
= ev
->cr
.bssid
;
804 __cfg80211_connect_result(
806 ev
->cr
.req_ie
, ev
->cr
.req_ie_len
,
807 ev
->cr
.resp_ie
, ev
->cr
.resp_ie_len
,
809 ev
->cr
.status
== WLAN_STATUS_SUCCESS
,
813 __cfg80211_roamed(wdev
, ev
->rm
.bss
, ev
->rm
.req_ie
,
814 ev
->rm
.req_ie_len
, ev
->rm
.resp_ie
,
817 case EVENT_DISCONNECTED
:
818 __cfg80211_disconnected(wdev
->netdev
,
819 ev
->dc
.ie
, ev
->dc
.ie_len
,
820 ev
->dc
.reason
, true);
822 case EVENT_IBSS_JOINED
:
823 __cfg80211_ibss_joined(wdev
->netdev
, ev
->ij
.bssid
);
830 spin_lock_irqsave(&wdev
->event_lock
, flags
);
832 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
835 void cfg80211_process_rdev_events(struct cfg80211_registered_device
*rdev
)
837 struct wireless_dev
*wdev
;
840 ASSERT_RDEV_LOCK(rdev
);
842 list_for_each_entry(wdev
, &rdev
->wdev_list
, list
)
843 cfg80211_process_wdev_events(wdev
);
846 int cfg80211_change_iface(struct cfg80211_registered_device
*rdev
,
847 struct net_device
*dev
, enum nl80211_iftype ntype
,
848 u32
*flags
, struct vif_params
*params
)
851 enum nl80211_iftype otype
= dev
->ieee80211_ptr
->iftype
;
853 ASSERT_RDEV_LOCK(rdev
);
855 /* don't support changing VLANs, you just re-create them */
856 if (otype
== NL80211_IFTYPE_AP_VLAN
)
859 /* cannot change into P2P device type */
860 if (ntype
== NL80211_IFTYPE_P2P_DEVICE
)
863 if (!rdev
->ops
->change_virtual_intf
||
864 !(rdev
->wiphy
.interface_modes
& (1 << ntype
)))
867 /* if it's part of a bridge, reject changing type to station/ibss */
868 if ((dev
->priv_flags
& IFF_BRIDGE_PORT
) &&
869 (ntype
== NL80211_IFTYPE_ADHOC
||
870 ntype
== NL80211_IFTYPE_STATION
||
871 ntype
== NL80211_IFTYPE_P2P_CLIENT
))
874 if (ntype
!= otype
&& netif_running(dev
)) {
875 err
= cfg80211_can_change_interface(rdev
, dev
->ieee80211_ptr
,
880 dev
->ieee80211_ptr
->use_4addr
= false;
881 dev
->ieee80211_ptr
->mesh_id_up_len
= 0;
882 wdev_lock(dev
->ieee80211_ptr
);
883 rdev_set_qos_map(rdev
, dev
, NULL
);
884 wdev_unlock(dev
->ieee80211_ptr
);
887 case NL80211_IFTYPE_AP
:
888 cfg80211_stop_ap(rdev
, dev
);
890 case NL80211_IFTYPE_ADHOC
:
891 cfg80211_leave_ibss(rdev
, dev
, false);
893 case NL80211_IFTYPE_STATION
:
894 case NL80211_IFTYPE_P2P_CLIENT
:
895 wdev_lock(dev
->ieee80211_ptr
);
896 cfg80211_disconnect(rdev
, dev
,
897 WLAN_REASON_DEAUTH_LEAVING
, true);
898 wdev_unlock(dev
->ieee80211_ptr
);
900 case NL80211_IFTYPE_MESH_POINT
:
901 /* mesh should be handled? */
907 cfg80211_process_rdev_events(rdev
);
910 err
= rdev_change_virtual_intf(rdev
, dev
, ntype
, flags
, params
);
912 WARN_ON(!err
&& dev
->ieee80211_ptr
->iftype
!= ntype
);
914 if (!err
&& params
&& params
->use_4addr
!= -1)
915 dev
->ieee80211_ptr
->use_4addr
= params
->use_4addr
;
918 dev
->priv_flags
&= ~IFF_DONT_BRIDGE
;
920 case NL80211_IFTYPE_STATION
:
921 if (dev
->ieee80211_ptr
->use_4addr
)
924 case NL80211_IFTYPE_P2P_CLIENT
:
925 case NL80211_IFTYPE_ADHOC
:
926 dev
->priv_flags
|= IFF_DONT_BRIDGE
;
928 case NL80211_IFTYPE_P2P_GO
:
929 case NL80211_IFTYPE_AP
:
930 case NL80211_IFTYPE_AP_VLAN
:
931 case NL80211_IFTYPE_WDS
:
932 case NL80211_IFTYPE_MESH_POINT
:
935 case NL80211_IFTYPE_MONITOR
:
936 /* monitor can't bridge anyway */
938 case NL80211_IFTYPE_UNSPECIFIED
:
939 case NUM_NL80211_IFTYPES
:
942 case NL80211_IFTYPE_P2P_DEVICE
:
948 if (!err
&& ntype
!= otype
&& netif_running(dev
)) {
949 cfg80211_update_iface_num(rdev
, ntype
, 1);
950 cfg80211_update_iface_num(rdev
, otype
, -1);
956 static u32
cfg80211_calculate_bitrate_60g(struct rate_info
*rate
)
958 static const u32 __mcs2bitrate
[] = {
966 [5] = 12512, /* 1251.25 mbps */
976 [14] = 8662, /* 866.25 mbps */
986 [24] = 67568, /* 6756.75 mbps */
997 if (WARN_ON_ONCE(rate
->mcs
>= ARRAY_SIZE(__mcs2bitrate
)))
1000 return __mcs2bitrate
[rate
->mcs
];
1003 static u32
cfg80211_calculate_bitrate_vht(struct rate_info
*rate
)
1005 static const u32 base
[4][10] = {
1054 if (WARN_ON_ONCE(rate
->mcs
> 9))
1057 idx
= rate
->flags
& (RATE_INFO_FLAGS_160_MHZ_WIDTH
|
1058 RATE_INFO_FLAGS_80P80_MHZ_WIDTH
) ? 3 :
1059 rate
->flags
& RATE_INFO_FLAGS_80_MHZ_WIDTH
? 2 :
1060 rate
->flags
& RATE_INFO_FLAGS_40_MHZ_WIDTH
? 1 : 0;
1062 bitrate
= base
[idx
][rate
->mcs
];
1063 bitrate
*= rate
->nss
;
1065 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1066 bitrate
= (bitrate
/ 9) * 10;
1068 /* do NOT round down here */
1069 return (bitrate
+ 50000) / 100000;
1072 u32
cfg80211_calculate_bitrate(struct rate_info
*rate
)
1074 int modulation
, streams
, bitrate
;
1076 if (!(rate
->flags
& RATE_INFO_FLAGS_MCS
) &&
1077 !(rate
->flags
& RATE_INFO_FLAGS_VHT_MCS
))
1078 return rate
->legacy
;
1079 if (rate
->flags
& RATE_INFO_FLAGS_60G
)
1080 return cfg80211_calculate_bitrate_60g(rate
);
1081 if (rate
->flags
& RATE_INFO_FLAGS_VHT_MCS
)
1082 return cfg80211_calculate_bitrate_vht(rate
);
1084 /* the formula below does only work for MCS values smaller than 32 */
1085 if (WARN_ON_ONCE(rate
->mcs
>= 32))
1088 modulation
= rate
->mcs
& 7;
1089 streams
= (rate
->mcs
>> 3) + 1;
1091 bitrate
= (rate
->flags
& RATE_INFO_FLAGS_40_MHZ_WIDTH
) ?
1095 bitrate
*= (modulation
+ 1);
1096 else if (modulation
== 4)
1097 bitrate
*= (modulation
+ 2);
1099 bitrate
*= (modulation
+ 3);
1103 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1104 bitrate
= (bitrate
/ 9) * 10;
1106 /* do NOT round down here */
1107 return (bitrate
+ 50000) / 100000;
1109 EXPORT_SYMBOL(cfg80211_calculate_bitrate
);
1111 int cfg80211_get_p2p_attr(const u8
*ies
, unsigned int len
,
1112 enum ieee80211_p2p_attr_id attr
,
1113 u8
*buf
, unsigned int bufsize
)
1116 u16 attr_remaining
= 0;
1117 bool desired_attr
= false;
1118 u16 desired_len
= 0;
1121 unsigned int iedatalen
;
1128 if (iedatalen
+ 2 > len
)
1131 if (ies
[0] != WLAN_EID_VENDOR_SPECIFIC
)
1139 /* check WFA OUI, P2P subtype */
1140 if (iedata
[0] != 0x50 || iedata
[1] != 0x6f ||
1141 iedata
[2] != 0x9a || iedata
[3] != 0x09)
1147 /* check attribute continuation into this IE */
1148 copy
= min_t(unsigned int, attr_remaining
, iedatalen
);
1149 if (copy
&& desired_attr
) {
1150 desired_len
+= copy
;
1152 memcpy(out
, iedata
, min(bufsize
, copy
));
1153 out
+= min(bufsize
, copy
);
1154 bufsize
-= min(bufsize
, copy
);
1158 if (copy
== attr_remaining
)
1162 attr_remaining
-= copy
;
1169 while (iedatalen
> 0) {
1172 /* P2P attribute ID & size must fit */
1175 desired_attr
= iedata
[0] == attr
;
1176 attr_len
= get_unaligned_le16(iedata
+ 1);
1180 copy
= min_t(unsigned int, attr_len
, iedatalen
);
1183 desired_len
+= copy
;
1185 memcpy(out
, iedata
, min(bufsize
, copy
));
1186 out
+= min(bufsize
, copy
);
1187 bufsize
-= min(bufsize
, copy
);
1190 if (copy
== attr_len
)
1196 attr_remaining
= attr_len
- copy
;
1204 if (attr_remaining
&& desired_attr
)
1209 EXPORT_SYMBOL(cfg80211_get_p2p_attr
);
1211 bool ieee80211_operating_class_to_band(u8 operating_class
,
1212 enum ieee80211_band
*band
)
1214 switch (operating_class
) {
1217 *band
= IEEE80211_BAND_5GHZ
;
1223 *band
= IEEE80211_BAND_2GHZ
;
1226 *band
= IEEE80211_BAND_60GHZ
;
1232 EXPORT_SYMBOL(ieee80211_operating_class_to_band
);
1234 int cfg80211_validate_beacon_int(struct cfg80211_registered_device
*rdev
,
1237 struct wireless_dev
*wdev
;
1243 list_for_each_entry(wdev
, &rdev
->wdev_list
, list
) {
1244 if (!wdev
->beacon_interval
)
1246 if (wdev
->beacon_interval
!= beacon_int
) {
1255 int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device
*rdev
,
1256 struct wireless_dev
*wdev
,
1257 enum nl80211_iftype iftype
,
1258 struct ieee80211_channel
*chan
,
1259 enum cfg80211_chan_mode chanmode
,
1262 struct wireless_dev
*wdev_iter
;
1263 u32 used_iftypes
= BIT(iftype
);
1264 int num
[NUM_NL80211_IFTYPES
];
1265 struct ieee80211_channel
1266 *used_channels
[CFG80211_MAX_NUM_DIFFERENT_CHANNELS
];
1267 struct ieee80211_channel
*ch
;
1268 enum cfg80211_chan_mode chmode
;
1269 int num_different_channels
= 0;
1271 bool radar_required
= false;
1276 if (WARN_ON(hweight32(radar_detect
) > 1))
1280 case NL80211_IFTYPE_ADHOC
:
1281 case NL80211_IFTYPE_AP
:
1282 case NL80211_IFTYPE_AP_VLAN
:
1283 case NL80211_IFTYPE_MESH_POINT
:
1284 case NL80211_IFTYPE_P2P_GO
:
1285 case NL80211_IFTYPE_WDS
:
1286 /* if the interface could potentially choose a DFS channel,
1287 * then mark DFS as required.
1290 if (chanmode
!= CHAN_MODE_UNDEFINED
&& radar_detect
)
1291 radar_required
= true;
1294 radar_required
= !!(chan
->flags
& IEEE80211_CHAN_RADAR
);
1296 case NL80211_IFTYPE_P2P_CLIENT
:
1297 case NL80211_IFTYPE_STATION
:
1298 case NL80211_IFTYPE_P2P_DEVICE
:
1299 case NL80211_IFTYPE_MONITOR
:
1301 case NUM_NL80211_IFTYPES
:
1302 case NL80211_IFTYPE_UNSPECIFIED
:
1307 if (radar_required
&& !radar_detect
)
1310 /* Always allow software iftypes */
1311 if (rdev
->wiphy
.software_iftypes
& BIT(iftype
)) {
1317 memset(num
, 0, sizeof(num
));
1318 memset(used_channels
, 0, sizeof(used_channels
));
1323 case CHAN_MODE_UNDEFINED
:
1325 case CHAN_MODE_SHARED
:
1327 used_channels
[0] = chan
;
1328 num_different_channels
++;
1330 case CHAN_MODE_EXCLUSIVE
:
1331 num_different_channels
++;
1335 list_for_each_entry(wdev_iter
, &rdev
->wdev_list
, list
) {
1336 if (wdev_iter
== wdev
)
1338 if (wdev_iter
->iftype
== NL80211_IFTYPE_P2P_DEVICE
) {
1339 if (!wdev_iter
->p2p_started
)
1341 } else if (wdev_iter
->netdev
) {
1342 if (!netif_running(wdev_iter
->netdev
))
1348 if (rdev
->wiphy
.software_iftypes
& BIT(wdev_iter
->iftype
))
1352 * We may be holding the "wdev" mutex, but now need to lock
1353 * wdev_iter. This is OK because once we get here wdev_iter
1354 * is not wdev (tested above), but we need to use the nested
1355 * locking for lockdep.
1357 mutex_lock_nested(&wdev_iter
->mtx
, 1);
1358 __acquire(wdev_iter
->mtx
);
1359 cfg80211_get_chan_state(wdev_iter
, &ch
, &chmode
);
1360 wdev_unlock(wdev_iter
);
1363 case CHAN_MODE_UNDEFINED
:
1365 case CHAN_MODE_SHARED
:
1366 for (i
= 0; i
< CFG80211_MAX_NUM_DIFFERENT_CHANNELS
; i
++)
1367 if (!used_channels
[i
] || used_channels
[i
] == ch
)
1370 if (i
== CFG80211_MAX_NUM_DIFFERENT_CHANNELS
)
1373 if (used_channels
[i
] == NULL
) {
1374 used_channels
[i
] = ch
;
1375 num_different_channels
++;
1378 case CHAN_MODE_EXCLUSIVE
:
1379 num_different_channels
++;
1383 num
[wdev_iter
->iftype
]++;
1385 used_iftypes
|= BIT(wdev_iter
->iftype
);
1388 if (total
== 1 && !radar_detect
)
1391 for (i
= 0; i
< rdev
->wiphy
.n_iface_combinations
; i
++) {
1392 const struct ieee80211_iface_combination
*c
;
1393 struct ieee80211_iface_limit
*limits
;
1394 u32 all_iftypes
= 0;
1396 c
= &rdev
->wiphy
.iface_combinations
[i
];
1398 if (total
> c
->max_interfaces
)
1400 if (num_different_channels
> c
->num_different_channels
)
1403 limits
= kmemdup(c
->limits
, sizeof(limits
[0]) * c
->n_limits
,
1408 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1409 if (rdev
->wiphy
.software_iftypes
& BIT(iftype
))
1411 for (j
= 0; j
< c
->n_limits
; j
++) {
1412 all_iftypes
|= limits
[j
].types
;
1413 if (!(limits
[j
].types
& BIT(iftype
)))
1415 if (limits
[j
].max
< num
[iftype
])
1417 limits
[j
].max
-= num
[iftype
];
1421 if (radar_detect
&& !(c
->radar_detect_widths
& radar_detect
))
1425 * Finally check that all iftypes that we're currently
1426 * using are actually part of this combination. If they
1427 * aren't then we can't use this combination and have
1428 * to continue to the next.
1430 if ((all_iftypes
& used_iftypes
) != used_iftypes
)
1434 * This combination covered all interface types and
1435 * supported the requested numbers, so we're good.
1446 int ieee80211_get_ratemask(struct ieee80211_supported_band
*sband
,
1447 const u8
*rates
, unsigned int n_rates
,
1455 if (n_rates
== 0 || n_rates
> NL80211_MAX_SUPP_RATES
)
1460 for (i
= 0; i
< n_rates
; i
++) {
1461 int rate
= (rates
[i
] & 0x7f) * 5;
1464 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
1465 if (sband
->bitrates
[j
].bitrate
== rate
) {
1476 * mask must have at least one bit set here since we
1477 * didn't accept a 0-length rates array nor allowed
1478 * entries in the array that didn't exist
1484 unsigned int ieee80211_get_num_supported_channels(struct wiphy
*wiphy
)
1486 enum ieee80211_band band
;
1487 unsigned int n_channels
= 0;
1489 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1490 if (wiphy
->bands
[band
])
1491 n_channels
+= wiphy
->bands
[band
]->n_channels
;
1495 EXPORT_SYMBOL(ieee80211_get_num_supported_channels
);
1497 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1498 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1499 const unsigned char rfc1042_header
[] __aligned(2) =
1500 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1501 EXPORT_SYMBOL(rfc1042_header
);
1503 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1504 const unsigned char bridge_tunnel_header
[] __aligned(2) =
1505 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1506 EXPORT_SYMBOL(bridge_tunnel_header
);