1 // SPDX-License-Identifier: GPL-2.0
3 * Wireless utility functions
5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2017 Intel Deutschland GmbH
8 * Copyright (C) 2018-2020 Intel Corporation
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
27 struct ieee80211_rate
*
28 ieee80211_get_response_rate(struct ieee80211_supported_band
*sband
,
29 u32 basic_rates
, int bitrate
)
31 struct ieee80211_rate
*result
= &sband
->bitrates
[0];
34 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
35 if (!(basic_rates
& BIT(i
)))
37 if (sband
->bitrates
[i
].bitrate
> bitrate
)
39 result
= &sband
->bitrates
[i
];
44 EXPORT_SYMBOL(ieee80211_get_response_rate
);
46 u32
ieee80211_mandatory_rates(struct ieee80211_supported_band
*sband
,
47 enum nl80211_bss_scan_width scan_width
)
49 struct ieee80211_rate
*bitrates
;
50 u32 mandatory_rates
= 0;
51 enum ieee80211_rate_flags mandatory_flag
;
57 if (sband
->band
== NL80211_BAND_2GHZ
) {
58 if (scan_width
== NL80211_BSS_CHAN_WIDTH_5
||
59 scan_width
== NL80211_BSS_CHAN_WIDTH_10
)
60 mandatory_flag
= IEEE80211_RATE_MANDATORY_G
;
62 mandatory_flag
= IEEE80211_RATE_MANDATORY_B
;
64 mandatory_flag
= IEEE80211_RATE_MANDATORY_A
;
67 bitrates
= sband
->bitrates
;
68 for (i
= 0; i
< sband
->n_bitrates
; i
++)
69 if (bitrates
[i
].flags
& mandatory_flag
)
70 mandatory_rates
|= BIT(i
);
71 return mandatory_rates
;
73 EXPORT_SYMBOL(ieee80211_mandatory_rates
);
75 u32
ieee80211_channel_to_freq_khz(int chan
, enum nl80211_band band
)
77 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
80 return 0; /* not supported */
82 case NL80211_BAND_2GHZ
:
84 return MHZ_TO_KHZ(2484);
86 return MHZ_TO_KHZ(2407 + chan
* 5);
88 case NL80211_BAND_5GHZ
:
89 if (chan
>= 182 && chan
<= 196)
90 return MHZ_TO_KHZ(4000 + chan
* 5);
92 return MHZ_TO_KHZ(5000 + chan
* 5);
94 case NL80211_BAND_6GHZ
:
95 /* see 802.11ax D6.1 27.3.23.2 */
97 return MHZ_TO_KHZ(5935);
99 return MHZ_TO_KHZ(5950 + chan
* 5);
101 case NL80211_BAND_60GHZ
:
103 return MHZ_TO_KHZ(56160 + chan
* 2160);
108 return 0; /* not supported */
110 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz
);
112 int ieee80211_freq_khz_to_channel(u32 freq
)
114 /* TODO: just handle MHz for now */
115 freq
= KHZ_TO_MHZ(freq
);
117 /* see 802.11 17.3.8.3.2 and Annex J */
120 else if (freq
< 2484)
121 return (freq
- 2407) / 5;
122 else if (freq
>= 4910 && freq
<= 4980)
123 return (freq
- 4000) / 5;
124 else if (freq
< 5945)
125 return (freq
- 5000) / 5;
126 else if (freq
<= 45000) /* DMG band lower limit */
127 /* see 802.11ax D4.1 27.3.22.2 */
128 return (freq
- 5940) / 5;
129 else if (freq
>= 58320 && freq
<= 70200)
130 return (freq
- 56160) / 2160;
134 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel
);
136 struct ieee80211_channel
*ieee80211_get_channel_khz(struct wiphy
*wiphy
,
139 enum nl80211_band band
;
140 struct ieee80211_supported_band
*sband
;
143 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
144 sband
= wiphy
->bands
[band
];
149 for (i
= 0; i
< sband
->n_channels
; i
++) {
150 struct ieee80211_channel
*chan
= &sband
->channels
[i
];
152 if (ieee80211_channel_to_khz(chan
) == freq
)
159 EXPORT_SYMBOL(ieee80211_get_channel_khz
);
161 static void set_mandatory_flags_band(struct ieee80211_supported_band
*sband
)
165 switch (sband
->band
) {
166 case NL80211_BAND_5GHZ
:
167 case NL80211_BAND_6GHZ
:
169 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
170 if (sband
->bitrates
[i
].bitrate
== 60 ||
171 sband
->bitrates
[i
].bitrate
== 120 ||
172 sband
->bitrates
[i
].bitrate
== 240) {
173 sband
->bitrates
[i
].flags
|=
174 IEEE80211_RATE_MANDATORY_A
;
180 case NL80211_BAND_2GHZ
:
182 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
183 switch (sband
->bitrates
[i
].bitrate
) {
188 sband
->bitrates
[i
].flags
|=
189 IEEE80211_RATE_MANDATORY_B
|
190 IEEE80211_RATE_MANDATORY_G
;
196 sband
->bitrates
[i
].flags
|=
197 IEEE80211_RATE_MANDATORY_G
;
201 sband
->bitrates
[i
].flags
|=
202 IEEE80211_RATE_ERP_G
;
206 WARN_ON(want
!= 0 && want
!= 3);
208 case NL80211_BAND_60GHZ
:
209 /* check for mandatory HT MCS 1..4 */
210 WARN_ON(!sband
->ht_cap
.ht_supported
);
211 WARN_ON((sband
->ht_cap
.mcs
.rx_mask
[0] & 0x1e) != 0x1e);
213 case NUM_NL80211_BANDS
:
220 void ieee80211_set_bitrate_flags(struct wiphy
*wiphy
)
222 enum nl80211_band band
;
224 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++)
225 if (wiphy
->bands
[band
])
226 set_mandatory_flags_band(wiphy
->bands
[band
]);
229 bool cfg80211_supported_cipher_suite(struct wiphy
*wiphy
, u32 cipher
)
232 for (i
= 0; i
< wiphy
->n_cipher_suites
; i
++)
233 if (cipher
== wiphy
->cipher_suites
[i
])
238 int cfg80211_validate_key_settings(struct cfg80211_registered_device
*rdev
,
239 struct key_params
*params
, int key_idx
,
240 bool pairwise
, const u8
*mac_addr
)
244 if (wiphy_ext_feature_isset(&rdev
->wiphy
,
245 NL80211_EXT_FEATURE_BEACON_PROTECTION
) ||
246 wiphy_ext_feature_isset(&rdev
->wiphy
,
247 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT
))
249 if (key_idx
< 0 || key_idx
> max_key_idx
)
252 if (!pairwise
&& mac_addr
&& !(rdev
->wiphy
.flags
& WIPHY_FLAG_IBSS_RSN
))
255 if (pairwise
&& !mac_addr
)
258 switch (params
->cipher
) {
259 case WLAN_CIPHER_SUITE_TKIP
:
260 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
261 if ((pairwise
&& key_idx
) ||
262 params
->mode
!= NL80211_KEY_RX_TX
)
265 case WLAN_CIPHER_SUITE_CCMP
:
266 case WLAN_CIPHER_SUITE_CCMP_256
:
267 case WLAN_CIPHER_SUITE_GCMP
:
268 case WLAN_CIPHER_SUITE_GCMP_256
:
269 /* IEEE802.11-2016 allows only 0 and - when supporting
270 * Extended Key ID - 1 as index for pairwise keys.
271 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
272 * the driver supports Extended Key ID.
273 * @NL80211_KEY_SET_TX can't be set when installing and
276 if ((params
->mode
== NL80211_KEY_NO_TX
&& !pairwise
) ||
277 params
->mode
== NL80211_KEY_SET_TX
)
279 if (wiphy_ext_feature_isset(&rdev
->wiphy
,
280 NL80211_EXT_FEATURE_EXT_KEY_ID
)) {
281 if (pairwise
&& (key_idx
< 0 || key_idx
> 1))
283 } else if (pairwise
&& key_idx
) {
287 case WLAN_CIPHER_SUITE_AES_CMAC
:
288 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
289 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
290 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
291 /* Disallow BIP (group-only) cipher as pairwise cipher */
297 case WLAN_CIPHER_SUITE_WEP40
:
298 case WLAN_CIPHER_SUITE_WEP104
:
305 switch (params
->cipher
) {
306 case WLAN_CIPHER_SUITE_WEP40
:
307 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
310 case WLAN_CIPHER_SUITE_TKIP
:
311 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
314 case WLAN_CIPHER_SUITE_CCMP
:
315 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
318 case WLAN_CIPHER_SUITE_CCMP_256
:
319 if (params
->key_len
!= WLAN_KEY_LEN_CCMP_256
)
322 case WLAN_CIPHER_SUITE_GCMP
:
323 if (params
->key_len
!= WLAN_KEY_LEN_GCMP
)
326 case WLAN_CIPHER_SUITE_GCMP_256
:
327 if (params
->key_len
!= WLAN_KEY_LEN_GCMP_256
)
330 case WLAN_CIPHER_SUITE_WEP104
:
331 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
334 case WLAN_CIPHER_SUITE_AES_CMAC
:
335 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
338 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
339 if (params
->key_len
!= WLAN_KEY_LEN_BIP_CMAC_256
)
342 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
343 if (params
->key_len
!= WLAN_KEY_LEN_BIP_GMAC_128
)
346 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
347 if (params
->key_len
!= WLAN_KEY_LEN_BIP_GMAC_256
)
352 * We don't know anything about this algorithm,
353 * allow using it -- but the driver must check
354 * all parameters! We still check below whether
355 * or not the driver supports this algorithm,
362 switch (params
->cipher
) {
363 case WLAN_CIPHER_SUITE_WEP40
:
364 case WLAN_CIPHER_SUITE_WEP104
:
365 /* These ciphers do not use key sequence */
367 case WLAN_CIPHER_SUITE_TKIP
:
368 case WLAN_CIPHER_SUITE_CCMP
:
369 case WLAN_CIPHER_SUITE_CCMP_256
:
370 case WLAN_CIPHER_SUITE_GCMP
:
371 case WLAN_CIPHER_SUITE_GCMP_256
:
372 case WLAN_CIPHER_SUITE_AES_CMAC
:
373 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
374 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
375 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
376 if (params
->seq_len
!= 6)
382 if (!cfg80211_supported_cipher_suite(&rdev
->wiphy
, params
->cipher
))
388 unsigned int __attribute_const__
ieee80211_hdrlen(__le16 fc
)
390 unsigned int hdrlen
= 24;
392 if (ieee80211_is_data(fc
)) {
393 if (ieee80211_has_a4(fc
))
395 if (ieee80211_is_data_qos(fc
)) {
396 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
397 if (ieee80211_has_order(fc
))
398 hdrlen
+= IEEE80211_HT_CTL_LEN
;
403 if (ieee80211_is_mgmt(fc
)) {
404 if (ieee80211_has_order(fc
))
405 hdrlen
+= IEEE80211_HT_CTL_LEN
;
409 if (ieee80211_is_ctl(fc
)) {
411 * ACK and CTS are 10 bytes, all others 16. To see how
412 * to get this condition consider
413 * subtype mask: 0b0000000011110000 (0x00F0)
414 * ACK subtype: 0b0000000011010000 (0x00D0)
415 * CTS subtype: 0b0000000011000000 (0x00C0)
416 * bits that matter: ^^^ (0x00E0)
417 * value of those: 0b0000000011000000 (0x00C0)
419 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
427 EXPORT_SYMBOL(ieee80211_hdrlen
);
429 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
431 const struct ieee80211_hdr
*hdr
=
432 (const struct ieee80211_hdr
*)skb
->data
;
435 if (unlikely(skb
->len
< 10))
437 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
438 if (unlikely(hdrlen
> skb
->len
))
442 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
444 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags
)
446 int ae
= flags
& MESH_FLAGS_AE
;
447 /* 802.11-2012, 8.2.4.7.3 */
452 case MESH_FLAGS_AE_A4
:
454 case MESH_FLAGS_AE_A5_A6
:
459 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
461 return __ieee80211_get_mesh_hdrlen(meshhdr
->flags
);
463 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen
);
465 int ieee80211_data_to_8023_exthdr(struct sk_buff
*skb
, struct ethhdr
*ehdr
,
466 const u8
*addr
, enum nl80211_iftype iftype
,
469 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
471 u8 hdr
[ETH_ALEN
] __aligned(2);
478 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
481 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
) + data_offset
;
482 if (skb
->len
< hdrlen
+ 8)
485 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
487 * IEEE 802.11 address fields:
488 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
489 * 0 0 DA SA BSSID n/a
490 * 0 1 DA BSSID SA n/a
491 * 1 0 BSSID SA DA n/a
494 memcpy(tmp
.h_dest
, ieee80211_get_DA(hdr
), ETH_ALEN
);
495 memcpy(tmp
.h_source
, ieee80211_get_SA(hdr
), ETH_ALEN
);
497 if (iftype
== NL80211_IFTYPE_MESH_POINT
)
498 skb_copy_bits(skb
, hdrlen
, &mesh_flags
, 1);
500 mesh_flags
&= MESH_FLAGS_AE
;
502 switch (hdr
->frame_control
&
503 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
504 case cpu_to_le16(IEEE80211_FCTL_TODS
):
505 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
506 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
507 iftype
!= NL80211_IFTYPE_P2P_GO
))
510 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
511 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
512 iftype
!= NL80211_IFTYPE_MESH_POINT
&&
513 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
514 iftype
!= NL80211_IFTYPE_STATION
))
516 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
517 if (mesh_flags
== MESH_FLAGS_AE_A4
)
519 if (mesh_flags
== MESH_FLAGS_AE_A5_A6
) {
520 skb_copy_bits(skb
, hdrlen
+
521 offsetof(struct ieee80211s_hdr
, eaddr1
),
522 tmp
.h_dest
, 2 * ETH_ALEN
);
524 hdrlen
+= __ieee80211_get_mesh_hdrlen(mesh_flags
);
527 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
528 if ((iftype
!= NL80211_IFTYPE_STATION
&&
529 iftype
!= NL80211_IFTYPE_P2P_CLIENT
&&
530 iftype
!= NL80211_IFTYPE_MESH_POINT
) ||
531 (is_multicast_ether_addr(tmp
.h_dest
) &&
532 ether_addr_equal(tmp
.h_source
, addr
)))
534 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
535 if (mesh_flags
== MESH_FLAGS_AE_A5_A6
)
537 if (mesh_flags
== MESH_FLAGS_AE_A4
)
538 skb_copy_bits(skb
, hdrlen
+
539 offsetof(struct ieee80211s_hdr
, eaddr1
),
540 tmp
.h_source
, ETH_ALEN
);
541 hdrlen
+= __ieee80211_get_mesh_hdrlen(mesh_flags
);
545 if (iftype
!= NL80211_IFTYPE_ADHOC
&&
546 iftype
!= NL80211_IFTYPE_STATION
&&
547 iftype
!= NL80211_IFTYPE_OCB
)
552 skb_copy_bits(skb
, hdrlen
, &payload
, sizeof(payload
));
553 tmp
.h_proto
= payload
.proto
;
555 if (likely((ether_addr_equal(payload
.hdr
, rfc1042_header
) &&
556 tmp
.h_proto
!= htons(ETH_P_AARP
) &&
557 tmp
.h_proto
!= htons(ETH_P_IPX
)) ||
558 ether_addr_equal(payload
.hdr
, bridge_tunnel_header
)))
559 /* remove RFC1042 or Bridge-Tunnel encapsulation and
560 * replace EtherType */
561 hdrlen
+= ETH_ALEN
+ 2;
563 tmp
.h_proto
= htons(skb
->len
- hdrlen
);
565 pskb_pull(skb
, hdrlen
);
568 ehdr
= skb_push(skb
, sizeof(struct ethhdr
));
569 memcpy(ehdr
, &tmp
, sizeof(tmp
));
573 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr
);
576 __frame_add_frag(struct sk_buff
*skb
, struct page
*page
,
577 void *ptr
, int len
, int size
)
579 struct skb_shared_info
*sh
= skb_shinfo(skb
);
583 page_offset
= ptr
- page_address(page
);
584 skb_add_rx_frag(skb
, sh
->nr_frags
, page
, page_offset
, len
, size
);
588 __ieee80211_amsdu_copy_frag(struct sk_buff
*skb
, struct sk_buff
*frame
,
591 struct skb_shared_info
*sh
= skb_shinfo(skb
);
592 const skb_frag_t
*frag
= &sh
->frags
[0];
593 struct page
*frag_page
;
595 int frag_len
, frag_size
;
596 int head_size
= skb
->len
- skb
->data_len
;
599 frag_page
= virt_to_head_page(skb
->head
);
600 frag_ptr
= skb
->data
;
601 frag_size
= head_size
;
603 while (offset
>= frag_size
) {
605 frag_page
= skb_frag_page(frag
);
606 frag_ptr
= skb_frag_address(frag
);
607 frag_size
= skb_frag_size(frag
);
612 frag_len
= frag_size
- offset
;
614 cur_len
= min(len
, frag_len
);
616 __frame_add_frag(frame
, frag_page
, frag_ptr
, cur_len
, frag_size
);
620 frag_len
= skb_frag_size(frag
);
621 cur_len
= min(len
, frag_len
);
622 __frame_add_frag(frame
, skb_frag_page(frag
),
623 skb_frag_address(frag
), cur_len
, frag_len
);
629 static struct sk_buff
*
630 __ieee80211_amsdu_copy(struct sk_buff
*skb
, unsigned int hlen
,
631 int offset
, int len
, bool reuse_frag
)
633 struct sk_buff
*frame
;
636 if (skb
->len
- offset
< len
)
640 * When reusing framents, copy some data to the head to simplify
641 * ethernet header handling and speed up protocol header processing
642 * in the stack later.
645 cur_len
= min_t(int, len
, 32);
648 * Allocate and reserve two bytes more for payload
649 * alignment since sizeof(struct ethhdr) is 14.
651 frame
= dev_alloc_skb(hlen
+ sizeof(struct ethhdr
) + 2 + cur_len
);
655 skb_reserve(frame
, hlen
+ sizeof(struct ethhdr
) + 2);
656 skb_copy_bits(skb
, offset
, skb_put(frame
, cur_len
), cur_len
);
663 __ieee80211_amsdu_copy_frag(skb
, frame
, offset
, len
);
668 void ieee80211_amsdu_to_8023s(struct sk_buff
*skb
, struct sk_buff_head
*list
,
669 const u8
*addr
, enum nl80211_iftype iftype
,
670 const unsigned int extra_headroom
,
671 const u8
*check_da
, const u8
*check_sa
)
673 unsigned int hlen
= ALIGN(extra_headroom
, 4);
674 struct sk_buff
*frame
= NULL
;
677 int offset
= 0, remaining
;
679 bool reuse_frag
= skb
->head_frag
&& !skb_has_frag_list(skb
);
680 bool reuse_skb
= false;
684 unsigned int subframe_len
;
688 skb_copy_bits(skb
, offset
, ð
, sizeof(eth
));
689 len
= ntohs(eth
.h_proto
);
690 subframe_len
= sizeof(struct ethhdr
) + len
;
691 padding
= (4 - subframe_len
) & 0x3;
693 /* the last MSDU has no padding */
694 remaining
= skb
->len
- offset
;
695 if (subframe_len
> remaining
)
698 offset
+= sizeof(struct ethhdr
);
699 last
= remaining
<= subframe_len
+ padding
;
701 /* FIXME: should we really accept multicast DA? */
702 if ((check_da
&& !is_multicast_ether_addr(eth
.h_dest
) &&
703 !ether_addr_equal(check_da
, eth
.h_dest
)) ||
704 (check_sa
&& !ether_addr_equal(check_sa
, eth
.h_source
))) {
705 offset
+= len
+ padding
;
709 /* reuse skb for the last subframe */
710 if (!skb_is_nonlinear(skb
) && !reuse_frag
&& last
) {
711 skb_pull(skb
, offset
);
715 frame
= __ieee80211_amsdu_copy(skb
, hlen
, offset
, len
,
720 offset
+= len
+ padding
;
723 skb_reset_network_header(frame
);
724 frame
->dev
= skb
->dev
;
725 frame
->priority
= skb
->priority
;
727 payload
= frame
->data
;
728 ethertype
= (payload
[6] << 8) | payload
[7];
729 if (likely((ether_addr_equal(payload
, rfc1042_header
) &&
730 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
731 ether_addr_equal(payload
, bridge_tunnel_header
))) {
732 eth
.h_proto
= htons(ethertype
);
733 skb_pull(frame
, ETH_ALEN
+ 2);
736 memcpy(skb_push(frame
, sizeof(eth
)), ð
, sizeof(eth
));
737 __skb_queue_tail(list
, frame
);
746 __skb_queue_purge(list
);
749 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s
);
751 /* Given a data frame determine the 802.1p/1d tag to use. */
752 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
,
753 struct cfg80211_qos_map
*qos_map
)
756 unsigned char vlan_priority
;
759 /* skb->priority values from 256->263 are magic values to
760 * directly indicate a specific 802.1d priority. This is used
761 * to allow 802.1d priority to be passed directly in from VLAN
764 if (skb
->priority
>= 256 && skb
->priority
<= 263) {
765 ret
= skb
->priority
- 256;
769 if (skb_vlan_tag_present(skb
)) {
770 vlan_priority
= (skb_vlan_tag_get(skb
) & VLAN_PRIO_MASK
)
772 if (vlan_priority
> 0) {
778 switch (skb
->protocol
) {
779 case htons(ETH_P_IP
):
780 dscp
= ipv4_get_dsfield(ip_hdr(skb
)) & 0xfc;
782 case htons(ETH_P_IPV6
):
783 dscp
= ipv6_get_dsfield(ipv6_hdr(skb
)) & 0xfc;
785 case htons(ETH_P_MPLS_UC
):
786 case htons(ETH_P_MPLS_MC
): {
787 struct mpls_label mpls_tmp
, *mpls
;
789 mpls
= skb_header_pointer(skb
, sizeof(struct ethhdr
),
790 sizeof(*mpls
), &mpls_tmp
);
794 ret
= (ntohl(mpls
->entry
) & MPLS_LS_TC_MASK
)
798 case htons(ETH_P_80221
):
799 /* 802.21 is always network control traffic */
806 unsigned int i
, tmp_dscp
= dscp
>> 2;
808 for (i
= 0; i
< qos_map
->num_des
; i
++) {
809 if (tmp_dscp
== qos_map
->dscp_exception
[i
].dscp
) {
810 ret
= qos_map
->dscp_exception
[i
].up
;
815 for (i
= 0; i
< 8; i
++) {
816 if (tmp_dscp
>= qos_map
->up
[i
].low
&&
817 tmp_dscp
<= qos_map
->up
[i
].high
) {
826 return array_index_nospec(ret
, IEEE80211_NUM_TIDS
);
828 EXPORT_SYMBOL(cfg80211_classify8021d
);
830 const struct element
*ieee80211_bss_get_elem(struct cfg80211_bss
*bss
, u8 id
)
832 const struct cfg80211_bss_ies
*ies
;
834 ies
= rcu_dereference(bss
->ies
);
838 return cfg80211_find_elem(id
, ies
->data
, ies
->len
);
840 EXPORT_SYMBOL(ieee80211_bss_get_elem
);
842 void cfg80211_upload_connect_keys(struct wireless_dev
*wdev
)
844 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
845 struct net_device
*dev
= wdev
->netdev
;
848 if (!wdev
->connect_keys
)
851 for (i
= 0; i
< CFG80211_MAX_WEP_KEYS
; i
++) {
852 if (!wdev
->connect_keys
->params
[i
].cipher
)
854 if (rdev_add_key(rdev
, dev
, i
, false, NULL
,
855 &wdev
->connect_keys
->params
[i
])) {
856 netdev_err(dev
, "failed to set key %d\n", i
);
859 if (wdev
->connect_keys
->def
== i
&&
860 rdev_set_default_key(rdev
, dev
, i
, true, true)) {
861 netdev_err(dev
, "failed to set defkey %d\n", i
);
866 kzfree(wdev
->connect_keys
);
867 wdev
->connect_keys
= NULL
;
870 void cfg80211_process_wdev_events(struct wireless_dev
*wdev
)
872 struct cfg80211_event
*ev
;
875 spin_lock_irqsave(&wdev
->event_lock
, flags
);
876 while (!list_empty(&wdev
->event_list
)) {
877 ev
= list_first_entry(&wdev
->event_list
,
878 struct cfg80211_event
, list
);
880 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
884 case EVENT_CONNECT_RESULT
:
885 __cfg80211_connect_result(
888 ev
->cr
.status
== WLAN_STATUS_SUCCESS
);
891 __cfg80211_roamed(wdev
, &ev
->rm
);
893 case EVENT_DISCONNECTED
:
894 __cfg80211_disconnected(wdev
->netdev
,
895 ev
->dc
.ie
, ev
->dc
.ie_len
,
897 !ev
->dc
.locally_generated
);
899 case EVENT_IBSS_JOINED
:
900 __cfg80211_ibss_joined(wdev
->netdev
, ev
->ij
.bssid
,
904 __cfg80211_leave(wiphy_to_rdev(wdev
->wiphy
), wdev
);
906 case EVENT_PORT_AUTHORIZED
:
907 __cfg80211_port_authorized(wdev
, ev
->pa
.bssid
);
914 spin_lock_irqsave(&wdev
->event_lock
, flags
);
916 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
919 void cfg80211_process_rdev_events(struct cfg80211_registered_device
*rdev
)
921 struct wireless_dev
*wdev
;
925 list_for_each_entry(wdev
, &rdev
->wiphy
.wdev_list
, list
)
926 cfg80211_process_wdev_events(wdev
);
929 int cfg80211_change_iface(struct cfg80211_registered_device
*rdev
,
930 struct net_device
*dev
, enum nl80211_iftype ntype
,
931 struct vif_params
*params
)
934 enum nl80211_iftype otype
= dev
->ieee80211_ptr
->iftype
;
938 /* don't support changing VLANs, you just re-create them */
939 if (otype
== NL80211_IFTYPE_AP_VLAN
)
942 /* cannot change into P2P device or NAN */
943 if (ntype
== NL80211_IFTYPE_P2P_DEVICE
||
944 ntype
== NL80211_IFTYPE_NAN
)
947 if (!rdev
->ops
->change_virtual_intf
||
948 !(rdev
->wiphy
.interface_modes
& (1 << ntype
)))
951 /* if it's part of a bridge, reject changing type to station/ibss */
952 if (netif_is_bridge_port(dev
) &&
953 (ntype
== NL80211_IFTYPE_ADHOC
||
954 ntype
== NL80211_IFTYPE_STATION
||
955 ntype
== NL80211_IFTYPE_P2P_CLIENT
))
958 if (ntype
!= otype
) {
959 dev
->ieee80211_ptr
->use_4addr
= false;
960 dev
->ieee80211_ptr
->mesh_id_up_len
= 0;
961 wdev_lock(dev
->ieee80211_ptr
);
962 rdev_set_qos_map(rdev
, dev
, NULL
);
963 wdev_unlock(dev
->ieee80211_ptr
);
966 case NL80211_IFTYPE_AP
:
967 cfg80211_stop_ap(rdev
, dev
, true);
969 case NL80211_IFTYPE_ADHOC
:
970 cfg80211_leave_ibss(rdev
, dev
, false);
972 case NL80211_IFTYPE_STATION
:
973 case NL80211_IFTYPE_P2P_CLIENT
:
974 wdev_lock(dev
->ieee80211_ptr
);
975 cfg80211_disconnect(rdev
, dev
,
976 WLAN_REASON_DEAUTH_LEAVING
, true);
977 wdev_unlock(dev
->ieee80211_ptr
);
979 case NL80211_IFTYPE_MESH_POINT
:
980 /* mesh should be handled? */
986 cfg80211_process_rdev_events(rdev
);
987 cfg80211_mlme_purge_registrations(dev
->ieee80211_ptr
);
990 err
= rdev_change_virtual_intf(rdev
, dev
, ntype
, params
);
992 WARN_ON(!err
&& dev
->ieee80211_ptr
->iftype
!= ntype
);
994 if (!err
&& params
&& params
->use_4addr
!= -1)
995 dev
->ieee80211_ptr
->use_4addr
= params
->use_4addr
;
998 dev
->priv_flags
&= ~IFF_DONT_BRIDGE
;
1000 case NL80211_IFTYPE_STATION
:
1001 if (dev
->ieee80211_ptr
->use_4addr
)
1004 case NL80211_IFTYPE_OCB
:
1005 case NL80211_IFTYPE_P2P_CLIENT
:
1006 case NL80211_IFTYPE_ADHOC
:
1007 dev
->priv_flags
|= IFF_DONT_BRIDGE
;
1009 case NL80211_IFTYPE_P2P_GO
:
1010 case NL80211_IFTYPE_AP
:
1011 case NL80211_IFTYPE_AP_VLAN
:
1012 case NL80211_IFTYPE_WDS
:
1013 case NL80211_IFTYPE_MESH_POINT
:
1016 case NL80211_IFTYPE_MONITOR
:
1017 /* monitor can't bridge anyway */
1019 case NL80211_IFTYPE_UNSPECIFIED
:
1020 case NUM_NL80211_IFTYPES
:
1023 case NL80211_IFTYPE_P2P_DEVICE
:
1024 case NL80211_IFTYPE_NAN
:
1030 if (!err
&& ntype
!= otype
&& netif_running(dev
)) {
1031 cfg80211_update_iface_num(rdev
, ntype
, 1);
1032 cfg80211_update_iface_num(rdev
, otype
, -1);
1038 static u32
cfg80211_calculate_bitrate_ht(struct rate_info
*rate
)
1040 int modulation
, streams
, bitrate
;
1042 /* the formula below does only work for MCS values smaller than 32 */
1043 if (WARN_ON_ONCE(rate
->mcs
>= 32))
1046 modulation
= rate
->mcs
& 7;
1047 streams
= (rate
->mcs
>> 3) + 1;
1049 bitrate
= (rate
->bw
== RATE_INFO_BW_40
) ? 13500000 : 6500000;
1052 bitrate
*= (modulation
+ 1);
1053 else if (modulation
== 4)
1054 bitrate
*= (modulation
+ 2);
1056 bitrate
*= (modulation
+ 3);
1060 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1061 bitrate
= (bitrate
/ 9) * 10;
1063 /* do NOT round down here */
1064 return (bitrate
+ 50000) / 100000;
1067 static u32
cfg80211_calculate_bitrate_dmg(struct rate_info
*rate
)
1069 static const u32 __mcs2bitrate
[] = {
1077 [5] = 12512, /* 1251.25 mbps */
1087 [14] = 8662, /* 866.25 mbps */
1097 [24] = 67568, /* 6756.75 mbps */
1108 if (WARN_ON_ONCE(rate
->mcs
>= ARRAY_SIZE(__mcs2bitrate
)))
1111 return __mcs2bitrate
[rate
->mcs
];
1114 static u32
cfg80211_calculate_bitrate_edmg(struct rate_info
*rate
)
1116 static const u32 __mcs2bitrate
[] = {
1124 [5] = 12512, /* 1251.25 mbps */
1142 if (WARN_ON_ONCE(rate
->mcs
>= ARRAY_SIZE(__mcs2bitrate
)))
1145 return __mcs2bitrate
[rate
->mcs
] * rate
->n_bonded_ch
;
1148 static u32
cfg80211_calculate_bitrate_vht(struct rate_info
*rate
)
1150 static const u32 base
[4][10] = {
1160 /* not in the spec, but some devices use this: */
1204 case RATE_INFO_BW_160
:
1207 case RATE_INFO_BW_80
:
1210 case RATE_INFO_BW_40
:
1213 case RATE_INFO_BW_5
:
1214 case RATE_INFO_BW_10
:
1217 case RATE_INFO_BW_20
:
1221 bitrate
= base
[idx
][rate
->mcs
];
1222 bitrate
*= rate
->nss
;
1224 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1225 bitrate
= (bitrate
/ 9) * 10;
1227 /* do NOT round down here */
1228 return (bitrate
+ 50000) / 100000;
1230 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1231 rate
->bw
, rate
->mcs
, rate
->nss
);
1235 static u32
cfg80211_calculate_bitrate_he(struct rate_info
*rate
)
1238 u16 mcs_divisors
[12] = {
1239 34133, /* 16.666666... */
1240 17067, /* 8.333333... */
1241 11378, /* 5.555555... */
1242 8533, /* 4.166666... */
1243 5689, /* 2.777777... */
1244 4267, /* 2.083333... */
1245 3923, /* 1.851851... */
1246 3413, /* 1.666666... */
1247 2844, /* 1.388888... */
1248 2560, /* 1.250000... */
1249 2276, /* 1.111111... */
1250 2048, /* 1.000000... */
1252 u32 rates_160M
[3] = { 960777777, 907400000, 816666666 };
1253 u32 rates_969
[3] = { 480388888, 453700000, 408333333 };
1254 u32 rates_484
[3] = { 229411111, 216666666, 195000000 };
1255 u32 rates_242
[3] = { 114711111, 108333333, 97500000 };
1256 u32 rates_106
[3] = { 40000000, 37777777, 34000000 };
1257 u32 rates_52
[3] = { 18820000, 17777777, 16000000 };
1258 u32 rates_26
[3] = { 9411111, 8888888, 8000000 };
1262 if (WARN_ON_ONCE(rate
->mcs
> 11))
1265 if (WARN_ON_ONCE(rate
->he_gi
> NL80211_RATE_INFO_HE_GI_3_2
))
1267 if (WARN_ON_ONCE(rate
->he_ru_alloc
>
1268 NL80211_RATE_INFO_HE_RU_ALLOC_2x996
))
1270 if (WARN_ON_ONCE(rate
->nss
< 1 || rate
->nss
> 8))
1273 if (rate
->bw
== RATE_INFO_BW_160
)
1274 result
= rates_160M
[rate
->he_gi
];
1275 else if (rate
->bw
== RATE_INFO_BW_80
||
1276 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1277 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_996
))
1278 result
= rates_969
[rate
->he_gi
];
1279 else if (rate
->bw
== RATE_INFO_BW_40
||
1280 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1281 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_484
))
1282 result
= rates_484
[rate
->he_gi
];
1283 else if (rate
->bw
== RATE_INFO_BW_20
||
1284 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1285 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_242
))
1286 result
= rates_242
[rate
->he_gi
];
1287 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1288 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_106
)
1289 result
= rates_106
[rate
->he_gi
];
1290 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1291 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_52
)
1292 result
= rates_52
[rate
->he_gi
];
1293 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1294 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_26
)
1295 result
= rates_26
[rate
->he_gi
];
1297 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1298 rate
->bw
, rate
->he_ru_alloc
);
1302 /* now scale to the appropriate MCS */
1305 do_div(tmp
, mcs_divisors
[rate
->mcs
]);
1308 /* and take NSS, DCM into account */
1309 result
= (result
* rate
->nss
) / 8;
1313 return result
/ 10000;
1316 u32
cfg80211_calculate_bitrate(struct rate_info
*rate
)
1318 if (rate
->flags
& RATE_INFO_FLAGS_MCS
)
1319 return cfg80211_calculate_bitrate_ht(rate
);
1320 if (rate
->flags
& RATE_INFO_FLAGS_DMG
)
1321 return cfg80211_calculate_bitrate_dmg(rate
);
1322 if (rate
->flags
& RATE_INFO_FLAGS_EDMG
)
1323 return cfg80211_calculate_bitrate_edmg(rate
);
1324 if (rate
->flags
& RATE_INFO_FLAGS_VHT_MCS
)
1325 return cfg80211_calculate_bitrate_vht(rate
);
1326 if (rate
->flags
& RATE_INFO_FLAGS_HE_MCS
)
1327 return cfg80211_calculate_bitrate_he(rate
);
1329 return rate
->legacy
;
1331 EXPORT_SYMBOL(cfg80211_calculate_bitrate
);
1333 int cfg80211_get_p2p_attr(const u8
*ies
, unsigned int len
,
1334 enum ieee80211_p2p_attr_id attr
,
1335 u8
*buf
, unsigned int bufsize
)
1338 u16 attr_remaining
= 0;
1339 bool desired_attr
= false;
1340 u16 desired_len
= 0;
1343 unsigned int iedatalen
;
1350 if (iedatalen
+ 2 > len
)
1353 if (ies
[0] != WLAN_EID_VENDOR_SPECIFIC
)
1361 /* check WFA OUI, P2P subtype */
1362 if (iedata
[0] != 0x50 || iedata
[1] != 0x6f ||
1363 iedata
[2] != 0x9a || iedata
[3] != 0x09)
1369 /* check attribute continuation into this IE */
1370 copy
= min_t(unsigned int, attr_remaining
, iedatalen
);
1371 if (copy
&& desired_attr
) {
1372 desired_len
+= copy
;
1374 memcpy(out
, iedata
, min(bufsize
, copy
));
1375 out
+= min(bufsize
, copy
);
1376 bufsize
-= min(bufsize
, copy
);
1380 if (copy
== attr_remaining
)
1384 attr_remaining
-= copy
;
1391 while (iedatalen
> 0) {
1394 /* P2P attribute ID & size must fit */
1397 desired_attr
= iedata
[0] == attr
;
1398 attr_len
= get_unaligned_le16(iedata
+ 1);
1402 copy
= min_t(unsigned int, attr_len
, iedatalen
);
1405 desired_len
+= copy
;
1407 memcpy(out
, iedata
, min(bufsize
, copy
));
1408 out
+= min(bufsize
, copy
);
1409 bufsize
-= min(bufsize
, copy
);
1412 if (copy
== attr_len
)
1418 attr_remaining
= attr_len
- copy
;
1426 if (attr_remaining
&& desired_attr
)
1431 EXPORT_SYMBOL(cfg80211_get_p2p_attr
);
1433 static bool ieee80211_id_in_list(const u8
*ids
, int n_ids
, u8 id
, bool id_ext
)
1437 /* Make sure array values are legal */
1438 if (WARN_ON(ids
[n_ids
- 1] == WLAN_EID_EXTENSION
))
1443 if (ids
[i
] == WLAN_EID_EXTENSION
) {
1444 if (id_ext
&& (ids
[i
+ 1] == id
))
1451 if (ids
[i
] == id
&& !id_ext
)
1459 static size_t skip_ie(const u8
*ies
, size_t ielen
, size_t pos
)
1461 /* we assume a validly formed IEs buffer */
1462 u8 len
= ies
[pos
+ 1];
1466 /* the IE itself must have 255 bytes for fragments to follow */
1470 while (pos
< ielen
&& ies
[pos
] == WLAN_EID_FRAGMENT
) {
1478 size_t ieee80211_ie_split_ric(const u8
*ies
, size_t ielen
,
1479 const u8
*ids
, int n_ids
,
1480 const u8
*after_ric
, int n_after_ric
,
1483 size_t pos
= offset
;
1485 while (pos
< ielen
) {
1488 if (ies
[pos
] == WLAN_EID_EXTENSION
)
1490 if ((pos
+ ext
) >= ielen
)
1493 if (!ieee80211_id_in_list(ids
, n_ids
, ies
[pos
+ ext
],
1494 ies
[pos
] == WLAN_EID_EXTENSION
))
1497 if (ies
[pos
] == WLAN_EID_RIC_DATA
&& n_after_ric
) {
1498 pos
= skip_ie(ies
, ielen
, pos
);
1500 while (pos
< ielen
) {
1501 if (ies
[pos
] == WLAN_EID_EXTENSION
)
1506 if ((pos
+ ext
) >= ielen
)
1509 if (!ieee80211_id_in_list(after_ric
,
1513 pos
= skip_ie(ies
, ielen
, pos
);
1518 pos
= skip_ie(ies
, ielen
, pos
);
1524 EXPORT_SYMBOL(ieee80211_ie_split_ric
);
1526 bool ieee80211_operating_class_to_band(u8 operating_class
,
1527 enum nl80211_band
*band
)
1529 switch (operating_class
) {
1533 *band
= NL80211_BAND_5GHZ
;
1536 *band
= NL80211_BAND_6GHZ
;
1542 *band
= NL80211_BAND_2GHZ
;
1545 *band
= NL80211_BAND_60GHZ
;
1551 EXPORT_SYMBOL(ieee80211_operating_class_to_band
);
1553 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def
*chandef
,
1557 u32 freq
= chandef
->center_freq1
;
1559 if (freq
>= 2412 && freq
<= 2472) {
1560 if (chandef
->width
> NL80211_CHAN_WIDTH_40
)
1563 /* 2.407 GHz, channels 1..13 */
1564 if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1565 if (freq
> chandef
->chan
->center_freq
)
1566 *op_class
= 83; /* HT40+ */
1568 *op_class
= 84; /* HT40- */
1577 /* channel 14 is only for IEEE 802.11b */
1578 if (chandef
->width
!= NL80211_CHAN_WIDTH_20_NOHT
)
1581 *op_class
= 82; /* channel 14 */
1585 switch (chandef
->width
) {
1586 case NL80211_CHAN_WIDTH_80
:
1589 case NL80211_CHAN_WIDTH_160
:
1592 case NL80211_CHAN_WIDTH_80P80
:
1595 case NL80211_CHAN_WIDTH_10
:
1596 case NL80211_CHAN_WIDTH_5
:
1597 return false; /* unsupported for now */
1603 /* 5 GHz, channels 36..48 */
1604 if (freq
>= 5180 && freq
<= 5240) {
1606 *op_class
= vht_opclass
;
1607 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1608 if (freq
> chandef
->chan
->center_freq
)
1619 /* 5 GHz, channels 52..64 */
1620 if (freq
>= 5260 && freq
<= 5320) {
1622 *op_class
= vht_opclass
;
1623 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1624 if (freq
> chandef
->chan
->center_freq
)
1635 /* 5 GHz, channels 100..144 */
1636 if (freq
>= 5500 && freq
<= 5720) {
1638 *op_class
= vht_opclass
;
1639 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1640 if (freq
> chandef
->chan
->center_freq
)
1651 /* 5 GHz, channels 149..169 */
1652 if (freq
>= 5745 && freq
<= 5845) {
1654 *op_class
= vht_opclass
;
1655 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1656 if (freq
> chandef
->chan
->center_freq
)
1660 } else if (freq
<= 5805) {
1669 /* 56.16 GHz, channel 1..4 */
1670 if (freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6) {
1671 if (chandef
->width
>= NL80211_CHAN_WIDTH_40
)
1678 /* not supported yet */
1681 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class
);
1683 static void cfg80211_calculate_bi_data(struct wiphy
*wiphy
, u32 new_beacon_int
,
1684 u32
*beacon_int_gcd
,
1685 bool *beacon_int_different
)
1687 struct wireless_dev
*wdev
;
1689 *beacon_int_gcd
= 0;
1690 *beacon_int_different
= false;
1692 list_for_each_entry(wdev
, &wiphy
->wdev_list
, list
) {
1693 if (!wdev
->beacon_interval
)
1696 if (!*beacon_int_gcd
) {
1697 *beacon_int_gcd
= wdev
->beacon_interval
;
1701 if (wdev
->beacon_interval
== *beacon_int_gcd
)
1704 *beacon_int_different
= true;
1705 *beacon_int_gcd
= gcd(*beacon_int_gcd
, wdev
->beacon_interval
);
1708 if (new_beacon_int
&& *beacon_int_gcd
!= new_beacon_int
) {
1709 if (*beacon_int_gcd
)
1710 *beacon_int_different
= true;
1711 *beacon_int_gcd
= gcd(*beacon_int_gcd
, new_beacon_int
);
1715 int cfg80211_validate_beacon_int(struct cfg80211_registered_device
*rdev
,
1716 enum nl80211_iftype iftype
, u32 beacon_int
)
1719 * This is just a basic pre-condition check; if interface combinations
1720 * are possible the driver must already be checking those with a call
1721 * to cfg80211_check_combinations(), in which case we'll validate more
1722 * through the cfg80211_calculate_bi_data() call and code in
1723 * cfg80211_iter_combinations().
1726 if (beacon_int
< 10 || beacon_int
> 10000)
1732 int cfg80211_iter_combinations(struct wiphy
*wiphy
,
1733 struct iface_combination_params
*params
,
1734 void (*iter
)(const struct ieee80211_iface_combination
*c
,
1738 const struct ieee80211_regdomain
*regdom
;
1739 enum nl80211_dfs_regions region
= 0;
1741 int num_interfaces
= 0;
1742 u32 used_iftypes
= 0;
1744 bool beacon_int_different
;
1747 * This is a bit strange, since the iteration used to rely only on
1748 * the data given by the driver, but here it now relies on context,
1749 * in form of the currently operating interfaces.
1750 * This is OK for all current users, and saves us from having to
1751 * push the GCD calculations into all the drivers.
1752 * In the future, this should probably rely more on data that's in
1753 * cfg80211 already - the only thing not would appear to be any new
1754 * interfaces (while being brought up) and channel/radar data.
1756 cfg80211_calculate_bi_data(wiphy
, params
->new_beacon_int
,
1757 &beacon_int_gcd
, &beacon_int_different
);
1759 if (params
->radar_detect
) {
1761 regdom
= rcu_dereference(cfg80211_regdomain
);
1763 region
= regdom
->dfs_region
;
1767 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1768 num_interfaces
+= params
->iftype_num
[iftype
];
1769 if (params
->iftype_num
[iftype
] > 0 &&
1770 !cfg80211_iftype_allowed(wiphy
, iftype
, 0, 1))
1771 used_iftypes
|= BIT(iftype
);
1774 for (i
= 0; i
< wiphy
->n_iface_combinations
; i
++) {
1775 const struct ieee80211_iface_combination
*c
;
1776 struct ieee80211_iface_limit
*limits
;
1777 u32 all_iftypes
= 0;
1779 c
= &wiphy
->iface_combinations
[i
];
1781 if (num_interfaces
> c
->max_interfaces
)
1783 if (params
->num_different_channels
> c
->num_different_channels
)
1786 limits
= kmemdup(c
->limits
, sizeof(limits
[0]) * c
->n_limits
,
1791 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1792 if (cfg80211_iftype_allowed(wiphy
, iftype
, 0, 1))
1794 for (j
= 0; j
< c
->n_limits
; j
++) {
1795 all_iftypes
|= limits
[j
].types
;
1796 if (!(limits
[j
].types
& BIT(iftype
)))
1798 if (limits
[j
].max
< params
->iftype_num
[iftype
])
1800 limits
[j
].max
-= params
->iftype_num
[iftype
];
1804 if (params
->radar_detect
!=
1805 (c
->radar_detect_widths
& params
->radar_detect
))
1808 if (params
->radar_detect
&& c
->radar_detect_regions
&&
1809 !(c
->radar_detect_regions
& BIT(region
)))
1812 /* Finally check that all iftypes that we're currently
1813 * using are actually part of this combination. If they
1814 * aren't then we can't use this combination and have
1815 * to continue to the next.
1817 if ((all_iftypes
& used_iftypes
) != used_iftypes
)
1820 if (beacon_int_gcd
) {
1821 if (c
->beacon_int_min_gcd
&&
1822 beacon_int_gcd
< c
->beacon_int_min_gcd
)
1824 if (!c
->beacon_int_min_gcd
&& beacon_int_different
)
1828 /* This combination covered all interface types and
1829 * supported the requested numbers, so we're good.
1839 EXPORT_SYMBOL(cfg80211_iter_combinations
);
1842 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination
*c
,
1849 int cfg80211_check_combinations(struct wiphy
*wiphy
,
1850 struct iface_combination_params
*params
)
1854 err
= cfg80211_iter_combinations(wiphy
, params
,
1855 cfg80211_iter_sum_ifcombs
, &num
);
1863 EXPORT_SYMBOL(cfg80211_check_combinations
);
1865 int ieee80211_get_ratemask(struct ieee80211_supported_band
*sband
,
1866 const u8
*rates
, unsigned int n_rates
,
1874 if (n_rates
== 0 || n_rates
> NL80211_MAX_SUPP_RATES
)
1879 for (i
= 0; i
< n_rates
; i
++) {
1880 int rate
= (rates
[i
] & 0x7f) * 5;
1883 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
1884 if (sband
->bitrates
[j
].bitrate
== rate
) {
1895 * mask must have at least one bit set here since we
1896 * didn't accept a 0-length rates array nor allowed
1897 * entries in the array that didn't exist
1903 unsigned int ieee80211_get_num_supported_channels(struct wiphy
*wiphy
)
1905 enum nl80211_band band
;
1906 unsigned int n_channels
= 0;
1908 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++)
1909 if (wiphy
->bands
[band
])
1910 n_channels
+= wiphy
->bands
[band
]->n_channels
;
1914 EXPORT_SYMBOL(ieee80211_get_num_supported_channels
);
1916 int cfg80211_get_station(struct net_device
*dev
, const u8
*mac_addr
,
1917 struct station_info
*sinfo
)
1919 struct cfg80211_registered_device
*rdev
;
1920 struct wireless_dev
*wdev
;
1922 wdev
= dev
->ieee80211_ptr
;
1926 rdev
= wiphy_to_rdev(wdev
->wiphy
);
1927 if (!rdev
->ops
->get_station
)
1930 memset(sinfo
, 0, sizeof(*sinfo
));
1932 return rdev_get_station(rdev
, dev
, mac_addr
, sinfo
);
1934 EXPORT_SYMBOL(cfg80211_get_station
);
1936 void cfg80211_free_nan_func(struct cfg80211_nan_func
*f
)
1943 kfree(f
->serv_spec_info
);
1946 for (i
= 0; i
< f
->num_rx_filters
; i
++)
1947 kfree(f
->rx_filters
[i
].filter
);
1949 for (i
= 0; i
< f
->num_tx_filters
; i
++)
1950 kfree(f
->tx_filters
[i
].filter
);
1952 kfree(f
->rx_filters
);
1953 kfree(f
->tx_filters
);
1956 EXPORT_SYMBOL(cfg80211_free_nan_func
);
1958 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range
*freq_range
,
1959 u32 center_freq_khz
, u32 bw_khz
)
1961 u32 start_freq_khz
, end_freq_khz
;
1963 start_freq_khz
= center_freq_khz
- (bw_khz
/ 2);
1964 end_freq_khz
= center_freq_khz
+ (bw_khz
/ 2);
1966 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
1967 end_freq_khz
<= freq_range
->end_freq_khz
)
1973 int cfg80211_sinfo_alloc_tid_stats(struct station_info
*sinfo
, gfp_t gfp
)
1975 sinfo
->pertid
= kcalloc(IEEE80211_NUM_TIDS
+ 1,
1976 sizeof(*(sinfo
->pertid
)),
1983 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats
);
1985 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1986 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1987 const unsigned char rfc1042_header
[] __aligned(2) =
1988 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1989 EXPORT_SYMBOL(rfc1042_header
);
1991 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1992 const unsigned char bridge_tunnel_header
[] __aligned(2) =
1993 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1994 EXPORT_SYMBOL(bridge_tunnel_header
);
1996 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1997 struct iapp_layer2_update
{
1998 u8 da
[ETH_ALEN
]; /* broadcast */
1999 u8 sa
[ETH_ALEN
]; /* STA addr */
2007 void cfg80211_send_layer2_update(struct net_device
*dev
, const u8
*addr
)
2009 struct iapp_layer2_update
*msg
;
2010 struct sk_buff
*skb
;
2012 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2015 skb
= dev_alloc_skb(sizeof(*msg
));
2018 msg
= skb_put(skb
, sizeof(*msg
));
2020 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2021 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2023 eth_broadcast_addr(msg
->da
);
2024 ether_addr_copy(msg
->sa
, addr
);
2025 msg
->len
= htons(6);
2027 msg
->ssap
= 0x01; /* NULL LSAP, CR Bit: Response */
2028 msg
->control
= 0xaf; /* XID response lsb.1111F101.
2029 * F=0 (no poll command; unsolicited frame) */
2030 msg
->xid_info
[0] = 0x81; /* XID format identifier */
2031 msg
->xid_info
[1] = 1; /* LLC types/classes: Type 1 LLC */
2032 msg
->xid_info
[2] = 0; /* XID sender's receive window size (RW) */
2035 skb
->protocol
= eth_type_trans(skb
, dev
);
2036 memset(skb
->cb
, 0, sizeof(skb
->cb
));
2039 EXPORT_SYMBOL(cfg80211_send_layer2_update
);
2041 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap
*cap
,
2042 enum ieee80211_vht_chanwidth bw
,
2043 int mcs
, bool ext_nss_bw_capable
,
2044 unsigned int max_vht_nss
)
2046 u16 map
= le16_to_cpu(cap
->supp_mcs
.rx_mcs_map
);
2049 int i
, mcs_encoding
;
2054 if (WARN_ON(mcs
> 9 || max_vht_nss
> 8))
2064 /* find max_vht_nss for the given MCS */
2065 for (i
= 7; i
>= 0; i
--) {
2066 int supp
= (map
>> (2 * i
)) & 3;
2071 if (supp
>= mcs_encoding
) {
2072 max_vht_nss
= i
+ 1;
2078 if (!(cap
->supp_mcs
.tx_mcs_map
&
2079 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE
)))
2082 ext_nss_bw
= le32_get_bits(cap
->vht_cap_info
,
2083 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK
);
2084 supp_width
= le32_get_bits(cap
->vht_cap_info
,
2085 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK
);
2087 /* if not capable, treat ext_nss_bw as 0 */
2088 if (!ext_nss_bw_capable
)
2091 /* This is invalid */
2092 if (supp_width
== 3)
2095 /* This is an invalid combination so pretend nothing is supported */
2096 if (supp_width
== 2 && (ext_nss_bw
== 1 || ext_nss_bw
== 2))
2100 * Cover all the special cases according to IEEE 802.11-2016
2101 * Table 9-250. All other cases are either factor of 1 or not
2105 case IEEE80211_VHT_CHANWIDTH_USE_HT
:
2106 case IEEE80211_VHT_CHANWIDTH_80MHZ
:
2107 if ((supp_width
== 1 || supp_width
== 2) &&
2109 return 2 * max_vht_nss
;
2111 case IEEE80211_VHT_CHANWIDTH_160MHZ
:
2112 if (supp_width
== 0 &&
2113 (ext_nss_bw
== 1 || ext_nss_bw
== 2))
2114 return max_vht_nss
/ 2;
2115 if (supp_width
== 0 &&
2117 return (3 * max_vht_nss
) / 4;
2118 if (supp_width
== 1 &&
2120 return 2 * max_vht_nss
;
2122 case IEEE80211_VHT_CHANWIDTH_80P80MHZ
:
2123 if (supp_width
== 0 && ext_nss_bw
== 1)
2124 return 0; /* not possible */
2125 if (supp_width
== 0 &&
2127 return max_vht_nss
/ 2;
2128 if (supp_width
== 0 &&
2130 return (3 * max_vht_nss
) / 4;
2131 if (supp_width
== 1 &&
2133 return 0; /* not possible */
2134 if (supp_width
== 1 &&
2136 return max_vht_nss
/ 2;
2137 if (supp_width
== 1 &&
2139 return (3 * max_vht_nss
) / 4;
2143 /* not covered or invalid combination received */
2146 EXPORT_SYMBOL(ieee80211_get_vht_max_nss
);
2148 bool cfg80211_iftype_allowed(struct wiphy
*wiphy
, enum nl80211_iftype iftype
,
2149 bool is_4addr
, u8 check_swif
)
2152 bool is_vlan
= iftype
== NL80211_IFTYPE_AP_VLAN
;
2154 switch (check_swif
) {
2156 if (is_vlan
&& is_4addr
)
2157 return wiphy
->flags
& WIPHY_FLAG_4ADDR_AP
;
2158 return wiphy
->interface_modes
& BIT(iftype
);
2160 if (!(wiphy
->software_iftypes
& BIT(iftype
)) && is_vlan
)
2161 return wiphy
->flags
& WIPHY_FLAG_4ADDR_AP
;
2162 return wiphy
->software_iftypes
& BIT(iftype
);
2169 EXPORT_SYMBOL(cfg80211_iftype_allowed
);