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 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>
26 struct ieee80211_rate
*
27 ieee80211_get_response_rate(struct ieee80211_supported_band
*sband
,
28 u32 basic_rates
, int bitrate
)
30 struct ieee80211_rate
*result
= &sband
->bitrates
[0];
33 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
34 if (!(basic_rates
& BIT(i
)))
36 if (sband
->bitrates
[i
].bitrate
> bitrate
)
38 result
= &sband
->bitrates
[i
];
43 EXPORT_SYMBOL(ieee80211_get_response_rate
);
45 u32
ieee80211_mandatory_rates(struct ieee80211_supported_band
*sband
,
46 enum nl80211_bss_scan_width scan_width
)
48 struct ieee80211_rate
*bitrates
;
49 u32 mandatory_rates
= 0;
50 enum ieee80211_rate_flags mandatory_flag
;
56 if (sband
->band
== NL80211_BAND_2GHZ
) {
57 if (scan_width
== NL80211_BSS_CHAN_WIDTH_5
||
58 scan_width
== NL80211_BSS_CHAN_WIDTH_10
)
59 mandatory_flag
= IEEE80211_RATE_MANDATORY_G
;
61 mandatory_flag
= IEEE80211_RATE_MANDATORY_B
;
63 mandatory_flag
= IEEE80211_RATE_MANDATORY_A
;
66 bitrates
= sband
->bitrates
;
67 for (i
= 0; i
< sband
->n_bitrates
; i
++)
68 if (bitrates
[i
].flags
& mandatory_flag
)
69 mandatory_rates
|= BIT(i
);
70 return mandatory_rates
;
72 EXPORT_SYMBOL(ieee80211_mandatory_rates
);
74 int ieee80211_channel_to_frequency(int chan
, enum nl80211_band band
)
76 /* see 802.11 17.3.8.3.2 and Annex J
77 * there are overlapping channel numbers in 5GHz and 2GHz bands */
79 return 0; /* not supported */
81 case NL80211_BAND_2GHZ
:
85 return 2407 + chan
* 5;
87 case NL80211_BAND_5GHZ
:
88 if (chan
>= 182 && chan
<= 196)
89 return 4000 + chan
* 5;
91 return 5000 + chan
* 5;
93 case NL80211_BAND_60GHZ
:
95 return 56160 + chan
* 2160;
100 return 0; /* not supported */
102 EXPORT_SYMBOL(ieee80211_channel_to_frequency
);
104 int ieee80211_frequency_to_channel(int freq
)
106 /* see 802.11 17.3.8.3.2 and Annex J */
109 else if (freq
< 2484)
110 return (freq
- 2407) / 5;
111 else if (freq
>= 4910 && freq
<= 4980)
112 return (freq
- 4000) / 5;
113 else if (freq
<= 45000) /* DMG band lower limit */
114 return (freq
- 5000) / 5;
115 else if (freq
>= 58320 && freq
<= 70200)
116 return (freq
- 56160) / 2160;
120 EXPORT_SYMBOL(ieee80211_frequency_to_channel
);
122 struct ieee80211_channel
*ieee80211_get_channel(struct wiphy
*wiphy
, int freq
)
124 enum nl80211_band band
;
125 struct ieee80211_supported_band
*sband
;
128 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
129 sband
= wiphy
->bands
[band
];
134 for (i
= 0; i
< sband
->n_channels
; i
++) {
135 if (sband
->channels
[i
].center_freq
== freq
)
136 return &sband
->channels
[i
];
142 EXPORT_SYMBOL(ieee80211_get_channel
);
144 static void set_mandatory_flags_band(struct ieee80211_supported_band
*sband
)
148 switch (sband
->band
) {
149 case NL80211_BAND_5GHZ
:
151 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
152 if (sband
->bitrates
[i
].bitrate
== 60 ||
153 sband
->bitrates
[i
].bitrate
== 120 ||
154 sband
->bitrates
[i
].bitrate
== 240) {
155 sband
->bitrates
[i
].flags
|=
156 IEEE80211_RATE_MANDATORY_A
;
162 case NL80211_BAND_2GHZ
:
164 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
165 switch (sband
->bitrates
[i
].bitrate
) {
170 sband
->bitrates
[i
].flags
|=
171 IEEE80211_RATE_MANDATORY_B
|
172 IEEE80211_RATE_MANDATORY_G
;
178 sband
->bitrates
[i
].flags
|=
179 IEEE80211_RATE_MANDATORY_G
;
183 sband
->bitrates
[i
].flags
|=
184 IEEE80211_RATE_ERP_G
;
188 WARN_ON(want
!= 0 && want
!= 3);
190 case NL80211_BAND_60GHZ
:
191 /* check for mandatory HT MCS 1..4 */
192 WARN_ON(!sband
->ht_cap
.ht_supported
);
193 WARN_ON((sband
->ht_cap
.mcs
.rx_mask
[0] & 0x1e) != 0x1e);
195 case NUM_NL80211_BANDS
:
202 void ieee80211_set_bitrate_flags(struct wiphy
*wiphy
)
204 enum nl80211_band band
;
206 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++)
207 if (wiphy
->bands
[band
])
208 set_mandatory_flags_band(wiphy
->bands
[band
]);
211 bool cfg80211_supported_cipher_suite(struct wiphy
*wiphy
, u32 cipher
)
214 for (i
= 0; i
< wiphy
->n_cipher_suites
; i
++)
215 if (cipher
== wiphy
->cipher_suites
[i
])
220 int cfg80211_validate_key_settings(struct cfg80211_registered_device
*rdev
,
221 struct key_params
*params
, int key_idx
,
222 bool pairwise
, const u8
*mac_addr
)
224 if (key_idx
< 0 || key_idx
> 5)
227 if (!pairwise
&& mac_addr
&& !(rdev
->wiphy
.flags
& WIPHY_FLAG_IBSS_RSN
))
230 if (pairwise
&& !mac_addr
)
233 switch (params
->cipher
) {
234 case WLAN_CIPHER_SUITE_TKIP
:
235 case WLAN_CIPHER_SUITE_CCMP
:
236 case WLAN_CIPHER_SUITE_CCMP_256
:
237 case WLAN_CIPHER_SUITE_GCMP
:
238 case WLAN_CIPHER_SUITE_GCMP_256
:
239 /* Disallow pairwise keys with non-zero index unless it's WEP
240 * or a vendor specific cipher (because current deployments use
241 * pairwise WEP keys with non-zero indices and for vendor
242 * specific ciphers this should be validated in the driver or
243 * hardware level - but 802.11i clearly specifies to use zero)
245 if (pairwise
&& key_idx
)
248 case WLAN_CIPHER_SUITE_AES_CMAC
:
249 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
250 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
251 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
252 /* Disallow BIP (group-only) cipher as pairwise cipher */
258 case WLAN_CIPHER_SUITE_WEP40
:
259 case WLAN_CIPHER_SUITE_WEP104
:
266 switch (params
->cipher
) {
267 case WLAN_CIPHER_SUITE_WEP40
:
268 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
271 case WLAN_CIPHER_SUITE_TKIP
:
272 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
275 case WLAN_CIPHER_SUITE_CCMP
:
276 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
279 case WLAN_CIPHER_SUITE_CCMP_256
:
280 if (params
->key_len
!= WLAN_KEY_LEN_CCMP_256
)
283 case WLAN_CIPHER_SUITE_GCMP
:
284 if (params
->key_len
!= WLAN_KEY_LEN_GCMP
)
287 case WLAN_CIPHER_SUITE_GCMP_256
:
288 if (params
->key_len
!= WLAN_KEY_LEN_GCMP_256
)
291 case WLAN_CIPHER_SUITE_WEP104
:
292 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
295 case WLAN_CIPHER_SUITE_AES_CMAC
:
296 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
299 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
300 if (params
->key_len
!= WLAN_KEY_LEN_BIP_CMAC_256
)
303 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
304 if (params
->key_len
!= WLAN_KEY_LEN_BIP_GMAC_128
)
307 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
308 if (params
->key_len
!= WLAN_KEY_LEN_BIP_GMAC_256
)
313 * We don't know anything about this algorithm,
314 * allow using it -- but the driver must check
315 * all parameters! We still check below whether
316 * or not the driver supports this algorithm,
323 switch (params
->cipher
) {
324 case WLAN_CIPHER_SUITE_WEP40
:
325 case WLAN_CIPHER_SUITE_WEP104
:
326 /* These ciphers do not use key sequence */
328 case WLAN_CIPHER_SUITE_TKIP
:
329 case WLAN_CIPHER_SUITE_CCMP
:
330 case WLAN_CIPHER_SUITE_CCMP_256
:
331 case WLAN_CIPHER_SUITE_GCMP
:
332 case WLAN_CIPHER_SUITE_GCMP_256
:
333 case WLAN_CIPHER_SUITE_AES_CMAC
:
334 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
335 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
336 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
337 if (params
->seq_len
!= 6)
343 if (!cfg80211_supported_cipher_suite(&rdev
->wiphy
, params
->cipher
))
349 unsigned int __attribute_const__
ieee80211_hdrlen(__le16 fc
)
351 unsigned int hdrlen
= 24;
353 if (ieee80211_is_data(fc
)) {
354 if (ieee80211_has_a4(fc
))
356 if (ieee80211_is_data_qos(fc
)) {
357 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
358 if (ieee80211_has_order(fc
))
359 hdrlen
+= IEEE80211_HT_CTL_LEN
;
364 if (ieee80211_is_mgmt(fc
)) {
365 if (ieee80211_has_order(fc
))
366 hdrlen
+= IEEE80211_HT_CTL_LEN
;
370 if (ieee80211_is_ctl(fc
)) {
372 * ACK and CTS are 10 bytes, all others 16. To see how
373 * to get this condition consider
374 * subtype mask: 0b0000000011110000 (0x00F0)
375 * ACK subtype: 0b0000000011010000 (0x00D0)
376 * CTS subtype: 0b0000000011000000 (0x00C0)
377 * bits that matter: ^^^ (0x00E0)
378 * value of those: 0b0000000011000000 (0x00C0)
380 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
388 EXPORT_SYMBOL(ieee80211_hdrlen
);
390 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
392 const struct ieee80211_hdr
*hdr
=
393 (const struct ieee80211_hdr
*)skb
->data
;
396 if (unlikely(skb
->len
< 10))
398 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
399 if (unlikely(hdrlen
> skb
->len
))
403 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
405 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags
)
407 int ae
= flags
& MESH_FLAGS_AE
;
408 /* 802.11-2012, 8.2.4.7.3 */
413 case MESH_FLAGS_AE_A4
:
415 case MESH_FLAGS_AE_A5_A6
:
420 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
422 return __ieee80211_get_mesh_hdrlen(meshhdr
->flags
);
424 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen
);
426 int ieee80211_data_to_8023_exthdr(struct sk_buff
*skb
, struct ethhdr
*ehdr
,
427 const u8
*addr
, enum nl80211_iftype iftype
,
430 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
432 u8 hdr
[ETH_ALEN
] __aligned(2);
439 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
442 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
) + data_offset
;
443 if (skb
->len
< hdrlen
+ 8)
446 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
448 * IEEE 802.11 address fields:
449 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
450 * 0 0 DA SA BSSID n/a
451 * 0 1 DA BSSID SA n/a
452 * 1 0 BSSID SA DA n/a
455 memcpy(tmp
.h_dest
, ieee80211_get_DA(hdr
), ETH_ALEN
);
456 memcpy(tmp
.h_source
, ieee80211_get_SA(hdr
), ETH_ALEN
);
458 if (iftype
== NL80211_IFTYPE_MESH_POINT
)
459 skb_copy_bits(skb
, hdrlen
, &mesh_flags
, 1);
461 mesh_flags
&= MESH_FLAGS_AE
;
463 switch (hdr
->frame_control
&
464 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
465 case cpu_to_le16(IEEE80211_FCTL_TODS
):
466 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
467 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
468 iftype
!= NL80211_IFTYPE_P2P_GO
))
471 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
472 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
473 iftype
!= NL80211_IFTYPE_MESH_POINT
&&
474 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
475 iftype
!= NL80211_IFTYPE_STATION
))
477 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
478 if (mesh_flags
== MESH_FLAGS_AE_A4
)
480 if (mesh_flags
== MESH_FLAGS_AE_A5_A6
) {
481 skb_copy_bits(skb
, hdrlen
+
482 offsetof(struct ieee80211s_hdr
, eaddr1
),
483 tmp
.h_dest
, 2 * ETH_ALEN
);
485 hdrlen
+= __ieee80211_get_mesh_hdrlen(mesh_flags
);
488 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
489 if ((iftype
!= NL80211_IFTYPE_STATION
&&
490 iftype
!= NL80211_IFTYPE_P2P_CLIENT
&&
491 iftype
!= NL80211_IFTYPE_MESH_POINT
) ||
492 (is_multicast_ether_addr(tmp
.h_dest
) &&
493 ether_addr_equal(tmp
.h_source
, addr
)))
495 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
496 if (mesh_flags
== MESH_FLAGS_AE_A5_A6
)
498 if (mesh_flags
== MESH_FLAGS_AE_A4
)
499 skb_copy_bits(skb
, hdrlen
+
500 offsetof(struct ieee80211s_hdr
, eaddr1
),
501 tmp
.h_source
, ETH_ALEN
);
502 hdrlen
+= __ieee80211_get_mesh_hdrlen(mesh_flags
);
506 if (iftype
!= NL80211_IFTYPE_ADHOC
&&
507 iftype
!= NL80211_IFTYPE_STATION
&&
508 iftype
!= NL80211_IFTYPE_OCB
)
513 skb_copy_bits(skb
, hdrlen
, &payload
, sizeof(payload
));
514 tmp
.h_proto
= payload
.proto
;
516 if (likely((ether_addr_equal(payload
.hdr
, rfc1042_header
) &&
517 tmp
.h_proto
!= htons(ETH_P_AARP
) &&
518 tmp
.h_proto
!= htons(ETH_P_IPX
)) ||
519 ether_addr_equal(payload
.hdr
, bridge_tunnel_header
)))
520 /* remove RFC1042 or Bridge-Tunnel encapsulation and
521 * replace EtherType */
522 hdrlen
+= ETH_ALEN
+ 2;
524 tmp
.h_proto
= htons(skb
->len
- hdrlen
);
526 pskb_pull(skb
, hdrlen
);
529 ehdr
= skb_push(skb
, sizeof(struct ethhdr
));
530 memcpy(ehdr
, &tmp
, sizeof(tmp
));
534 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr
);
537 __frame_add_frag(struct sk_buff
*skb
, struct page
*page
,
538 void *ptr
, int len
, int size
)
540 struct skb_shared_info
*sh
= skb_shinfo(skb
);
544 page_offset
= ptr
- page_address(page
);
545 skb_add_rx_frag(skb
, sh
->nr_frags
, page
, page_offset
, len
, size
);
549 __ieee80211_amsdu_copy_frag(struct sk_buff
*skb
, struct sk_buff
*frame
,
552 struct skb_shared_info
*sh
= skb_shinfo(skb
);
553 const skb_frag_t
*frag
= &sh
->frags
[0];
554 struct page
*frag_page
;
556 int frag_len
, frag_size
;
557 int head_size
= skb
->len
- skb
->data_len
;
560 frag_page
= virt_to_head_page(skb
->head
);
561 frag_ptr
= skb
->data
;
562 frag_size
= head_size
;
564 while (offset
>= frag_size
) {
566 frag_page
= skb_frag_page(frag
);
567 frag_ptr
= skb_frag_address(frag
);
568 frag_size
= skb_frag_size(frag
);
573 frag_len
= frag_size
- offset
;
575 cur_len
= min(len
, frag_len
);
577 __frame_add_frag(frame
, frag_page
, frag_ptr
, cur_len
, frag_size
);
581 frag_len
= skb_frag_size(frag
);
582 cur_len
= min(len
, frag_len
);
583 __frame_add_frag(frame
, skb_frag_page(frag
),
584 skb_frag_address(frag
), cur_len
, frag_len
);
590 static struct sk_buff
*
591 __ieee80211_amsdu_copy(struct sk_buff
*skb
, unsigned int hlen
,
592 int offset
, int len
, bool reuse_frag
)
594 struct sk_buff
*frame
;
597 if (skb
->len
- offset
< len
)
601 * When reusing framents, copy some data to the head to simplify
602 * ethernet header handling and speed up protocol header processing
603 * in the stack later.
606 cur_len
= min_t(int, len
, 32);
609 * Allocate and reserve two bytes more for payload
610 * alignment since sizeof(struct ethhdr) is 14.
612 frame
= dev_alloc_skb(hlen
+ sizeof(struct ethhdr
) + 2 + cur_len
);
616 skb_reserve(frame
, hlen
+ sizeof(struct ethhdr
) + 2);
617 skb_copy_bits(skb
, offset
, skb_put(frame
, cur_len
), cur_len
);
624 __ieee80211_amsdu_copy_frag(skb
, frame
, offset
, len
);
629 void ieee80211_amsdu_to_8023s(struct sk_buff
*skb
, struct sk_buff_head
*list
,
630 const u8
*addr
, enum nl80211_iftype iftype
,
631 const unsigned int extra_headroom
,
632 const u8
*check_da
, const u8
*check_sa
)
634 unsigned int hlen
= ALIGN(extra_headroom
, 4);
635 struct sk_buff
*frame
= NULL
;
638 int offset
= 0, remaining
;
640 bool reuse_frag
= skb
->head_frag
&& !skb_has_frag_list(skb
);
641 bool reuse_skb
= false;
645 unsigned int subframe_len
;
649 skb_copy_bits(skb
, offset
, ð
, sizeof(eth
));
650 len
= ntohs(eth
.h_proto
);
651 subframe_len
= sizeof(struct ethhdr
) + len
;
652 padding
= (4 - subframe_len
) & 0x3;
654 /* the last MSDU has no padding */
655 remaining
= skb
->len
- offset
;
656 if (subframe_len
> remaining
)
659 offset
+= sizeof(struct ethhdr
);
660 last
= remaining
<= subframe_len
+ padding
;
662 /* FIXME: should we really accept multicast DA? */
663 if ((check_da
&& !is_multicast_ether_addr(eth
.h_dest
) &&
664 !ether_addr_equal(check_da
, eth
.h_dest
)) ||
665 (check_sa
&& !ether_addr_equal(check_sa
, eth
.h_source
))) {
666 offset
+= len
+ padding
;
670 /* reuse skb for the last subframe */
671 if (!skb_is_nonlinear(skb
) && !reuse_frag
&& last
) {
672 skb_pull(skb
, offset
);
676 frame
= __ieee80211_amsdu_copy(skb
, hlen
, offset
, len
,
681 offset
+= len
+ padding
;
684 skb_reset_network_header(frame
);
685 frame
->dev
= skb
->dev
;
686 frame
->priority
= skb
->priority
;
688 payload
= frame
->data
;
689 ethertype
= (payload
[6] << 8) | payload
[7];
690 if (likely((ether_addr_equal(payload
, rfc1042_header
) &&
691 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
692 ether_addr_equal(payload
, bridge_tunnel_header
))) {
693 eth
.h_proto
= htons(ethertype
);
694 skb_pull(frame
, ETH_ALEN
+ 2);
697 memcpy(skb_push(frame
, sizeof(eth
)), ð
, sizeof(eth
));
698 __skb_queue_tail(list
, frame
);
707 __skb_queue_purge(list
);
710 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s
);
712 /* Given a data frame determine the 802.1p/1d tag to use. */
713 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
,
714 struct cfg80211_qos_map
*qos_map
)
717 unsigned char vlan_priority
;
719 /* skb->priority values from 256->263 are magic values to
720 * directly indicate a specific 802.1d priority. This is used
721 * to allow 802.1d priority to be passed directly in from VLAN
724 if (skb
->priority
>= 256 && skb
->priority
<= 263)
725 return skb
->priority
- 256;
727 if (skb_vlan_tag_present(skb
)) {
728 vlan_priority
= (skb_vlan_tag_get(skb
) & VLAN_PRIO_MASK
)
730 if (vlan_priority
> 0)
731 return vlan_priority
;
734 switch (skb
->protocol
) {
735 case htons(ETH_P_IP
):
736 dscp
= ipv4_get_dsfield(ip_hdr(skb
)) & 0xfc;
738 case htons(ETH_P_IPV6
):
739 dscp
= ipv6_get_dsfield(ipv6_hdr(skb
)) & 0xfc;
741 case htons(ETH_P_MPLS_UC
):
742 case htons(ETH_P_MPLS_MC
): {
743 struct mpls_label mpls_tmp
, *mpls
;
745 mpls
= skb_header_pointer(skb
, sizeof(struct ethhdr
),
746 sizeof(*mpls
), &mpls_tmp
);
750 return (ntohl(mpls
->entry
) & MPLS_LS_TC_MASK
)
753 case htons(ETH_P_80221
):
754 /* 802.21 is always network control traffic */
761 unsigned int i
, tmp_dscp
= dscp
>> 2;
763 for (i
= 0; i
< qos_map
->num_des
; i
++) {
764 if (tmp_dscp
== qos_map
->dscp_exception
[i
].dscp
)
765 return qos_map
->dscp_exception
[i
].up
;
768 for (i
= 0; i
< 8; i
++) {
769 if (tmp_dscp
>= qos_map
->up
[i
].low
&&
770 tmp_dscp
<= qos_map
->up
[i
].high
)
777 EXPORT_SYMBOL(cfg80211_classify8021d
);
779 const u8
*ieee80211_bss_get_ie(struct cfg80211_bss
*bss
, u8 ie
)
781 const struct cfg80211_bss_ies
*ies
;
783 ies
= rcu_dereference(bss
->ies
);
787 return cfg80211_find_ie(ie
, ies
->data
, ies
->len
);
789 EXPORT_SYMBOL(ieee80211_bss_get_ie
);
791 void cfg80211_upload_connect_keys(struct wireless_dev
*wdev
)
793 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
794 struct net_device
*dev
= wdev
->netdev
;
797 if (!wdev
->connect_keys
)
800 for (i
= 0; i
< CFG80211_MAX_WEP_KEYS
; i
++) {
801 if (!wdev
->connect_keys
->params
[i
].cipher
)
803 if (rdev_add_key(rdev
, dev
, i
, false, NULL
,
804 &wdev
->connect_keys
->params
[i
])) {
805 netdev_err(dev
, "failed to set key %d\n", i
);
808 if (wdev
->connect_keys
->def
== i
&&
809 rdev_set_default_key(rdev
, dev
, i
, true, true)) {
810 netdev_err(dev
, "failed to set defkey %d\n", i
);
815 kzfree(wdev
->connect_keys
);
816 wdev
->connect_keys
= NULL
;
819 void cfg80211_process_wdev_events(struct wireless_dev
*wdev
)
821 struct cfg80211_event
*ev
;
824 spin_lock_irqsave(&wdev
->event_lock
, flags
);
825 while (!list_empty(&wdev
->event_list
)) {
826 ev
= list_first_entry(&wdev
->event_list
,
827 struct cfg80211_event
, list
);
829 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
833 case EVENT_CONNECT_RESULT
:
834 __cfg80211_connect_result(
837 ev
->cr
.status
== WLAN_STATUS_SUCCESS
);
840 __cfg80211_roamed(wdev
, &ev
->rm
);
842 case EVENT_DISCONNECTED
:
843 __cfg80211_disconnected(wdev
->netdev
,
844 ev
->dc
.ie
, ev
->dc
.ie_len
,
846 !ev
->dc
.locally_generated
);
848 case EVENT_IBSS_JOINED
:
849 __cfg80211_ibss_joined(wdev
->netdev
, ev
->ij
.bssid
,
853 __cfg80211_leave(wiphy_to_rdev(wdev
->wiphy
), wdev
);
855 case EVENT_PORT_AUTHORIZED
:
856 __cfg80211_port_authorized(wdev
, ev
->pa
.bssid
);
863 spin_lock_irqsave(&wdev
->event_lock
, flags
);
865 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
868 void cfg80211_process_rdev_events(struct cfg80211_registered_device
*rdev
)
870 struct wireless_dev
*wdev
;
874 list_for_each_entry(wdev
, &rdev
->wiphy
.wdev_list
, list
)
875 cfg80211_process_wdev_events(wdev
);
878 int cfg80211_change_iface(struct cfg80211_registered_device
*rdev
,
879 struct net_device
*dev
, enum nl80211_iftype ntype
,
880 struct vif_params
*params
)
883 enum nl80211_iftype otype
= dev
->ieee80211_ptr
->iftype
;
887 /* don't support changing VLANs, you just re-create them */
888 if (otype
== NL80211_IFTYPE_AP_VLAN
)
891 /* cannot change into P2P device or NAN */
892 if (ntype
== NL80211_IFTYPE_P2P_DEVICE
||
893 ntype
== NL80211_IFTYPE_NAN
)
896 if (!rdev
->ops
->change_virtual_intf
||
897 !(rdev
->wiphy
.interface_modes
& (1 << ntype
)))
900 /* if it's part of a bridge, reject changing type to station/ibss */
901 if ((dev
->priv_flags
& IFF_BRIDGE_PORT
) &&
902 (ntype
== NL80211_IFTYPE_ADHOC
||
903 ntype
== NL80211_IFTYPE_STATION
||
904 ntype
== NL80211_IFTYPE_P2P_CLIENT
))
907 if (ntype
!= otype
) {
908 dev
->ieee80211_ptr
->use_4addr
= false;
909 dev
->ieee80211_ptr
->mesh_id_up_len
= 0;
910 wdev_lock(dev
->ieee80211_ptr
);
911 rdev_set_qos_map(rdev
, dev
, NULL
);
912 wdev_unlock(dev
->ieee80211_ptr
);
915 case NL80211_IFTYPE_AP
:
916 cfg80211_stop_ap(rdev
, dev
, true);
918 case NL80211_IFTYPE_ADHOC
:
919 cfg80211_leave_ibss(rdev
, dev
, false);
921 case NL80211_IFTYPE_STATION
:
922 case NL80211_IFTYPE_P2P_CLIENT
:
923 wdev_lock(dev
->ieee80211_ptr
);
924 cfg80211_disconnect(rdev
, dev
,
925 WLAN_REASON_DEAUTH_LEAVING
, true);
926 wdev_unlock(dev
->ieee80211_ptr
);
928 case NL80211_IFTYPE_MESH_POINT
:
929 /* mesh should be handled? */
935 cfg80211_process_rdev_events(rdev
);
938 err
= rdev_change_virtual_intf(rdev
, dev
, ntype
, params
);
940 WARN_ON(!err
&& dev
->ieee80211_ptr
->iftype
!= ntype
);
942 if (!err
&& params
&& params
->use_4addr
!= -1)
943 dev
->ieee80211_ptr
->use_4addr
= params
->use_4addr
;
946 dev
->priv_flags
&= ~IFF_DONT_BRIDGE
;
948 case NL80211_IFTYPE_STATION
:
949 if (dev
->ieee80211_ptr
->use_4addr
)
952 case NL80211_IFTYPE_OCB
:
953 case NL80211_IFTYPE_P2P_CLIENT
:
954 case NL80211_IFTYPE_ADHOC
:
955 dev
->priv_flags
|= IFF_DONT_BRIDGE
;
957 case NL80211_IFTYPE_P2P_GO
:
958 case NL80211_IFTYPE_AP
:
959 case NL80211_IFTYPE_AP_VLAN
:
960 case NL80211_IFTYPE_WDS
:
961 case NL80211_IFTYPE_MESH_POINT
:
964 case NL80211_IFTYPE_MONITOR
:
965 /* monitor can't bridge anyway */
967 case NL80211_IFTYPE_UNSPECIFIED
:
968 case NUM_NL80211_IFTYPES
:
971 case NL80211_IFTYPE_P2P_DEVICE
:
972 case NL80211_IFTYPE_NAN
:
978 if (!err
&& ntype
!= otype
&& netif_running(dev
)) {
979 cfg80211_update_iface_num(rdev
, ntype
, 1);
980 cfg80211_update_iface_num(rdev
, otype
, -1);
986 static u32
cfg80211_calculate_bitrate_ht(struct rate_info
*rate
)
988 int modulation
, streams
, bitrate
;
990 /* the formula below does only work for MCS values smaller than 32 */
991 if (WARN_ON_ONCE(rate
->mcs
>= 32))
994 modulation
= rate
->mcs
& 7;
995 streams
= (rate
->mcs
>> 3) + 1;
997 bitrate
= (rate
->bw
== RATE_INFO_BW_40
) ? 13500000 : 6500000;
1000 bitrate
*= (modulation
+ 1);
1001 else if (modulation
== 4)
1002 bitrate
*= (modulation
+ 2);
1004 bitrate
*= (modulation
+ 3);
1008 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1009 bitrate
= (bitrate
/ 9) * 10;
1011 /* do NOT round down here */
1012 return (bitrate
+ 50000) / 100000;
1015 static u32
cfg80211_calculate_bitrate_60g(struct rate_info
*rate
)
1017 static const u32 __mcs2bitrate
[] = {
1025 [5] = 12512, /* 1251.25 mbps */
1035 [14] = 8662, /* 866.25 mbps */
1045 [24] = 67568, /* 6756.75 mbps */
1056 if (WARN_ON_ONCE(rate
->mcs
>= ARRAY_SIZE(__mcs2bitrate
)))
1059 return __mcs2bitrate
[rate
->mcs
];
1062 static u32
cfg80211_calculate_bitrate_vht(struct rate_info
*rate
)
1064 static const u32 base
[4][10] = {
1074 /* not in the spec, but some devices use this: */
1118 case RATE_INFO_BW_160
:
1121 case RATE_INFO_BW_80
:
1124 case RATE_INFO_BW_40
:
1127 case RATE_INFO_BW_5
:
1128 case RATE_INFO_BW_10
:
1131 case RATE_INFO_BW_20
:
1135 bitrate
= base
[idx
][rate
->mcs
];
1136 bitrate
*= rate
->nss
;
1138 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1139 bitrate
= (bitrate
/ 9) * 10;
1141 /* do NOT round down here */
1142 return (bitrate
+ 50000) / 100000;
1144 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1145 rate
->bw
, rate
->mcs
, rate
->nss
);
1149 static u32
cfg80211_calculate_bitrate_he(struct rate_info
*rate
)
1152 u16 mcs_divisors
[12] = {
1153 34133, /* 16.666666... */
1154 17067, /* 8.333333... */
1155 11378, /* 5.555555... */
1156 8533, /* 4.166666... */
1157 5689, /* 2.777777... */
1158 4267, /* 2.083333... */
1159 3923, /* 1.851851... */
1160 3413, /* 1.666666... */
1161 2844, /* 1.388888... */
1162 2560, /* 1.250000... */
1163 2276, /* 1.111111... */
1164 2048, /* 1.000000... */
1166 u32 rates_160M
[3] = { 960777777, 907400000, 816666666 };
1167 u32 rates_969
[3] = { 480388888, 453700000, 408333333 };
1168 u32 rates_484
[3] = { 229411111, 216666666, 195000000 };
1169 u32 rates_242
[3] = { 114711111, 108333333, 97500000 };
1170 u32 rates_106
[3] = { 40000000, 37777777, 34000000 };
1171 u32 rates_52
[3] = { 18820000, 17777777, 16000000 };
1172 u32 rates_26
[3] = { 9411111, 8888888, 8000000 };
1176 if (WARN_ON_ONCE(rate
->mcs
> 11))
1179 if (WARN_ON_ONCE(rate
->he_gi
> NL80211_RATE_INFO_HE_GI_3_2
))
1181 if (WARN_ON_ONCE(rate
->he_ru_alloc
>
1182 NL80211_RATE_INFO_HE_RU_ALLOC_2x996
))
1184 if (WARN_ON_ONCE(rate
->nss
< 1 || rate
->nss
> 8))
1187 if (rate
->bw
== RATE_INFO_BW_160
)
1188 result
= rates_160M
[rate
->he_gi
];
1189 else if (rate
->bw
== RATE_INFO_BW_80
||
1190 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1191 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_996
))
1192 result
= rates_969
[rate
->he_gi
];
1193 else if (rate
->bw
== RATE_INFO_BW_40
||
1194 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1195 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_484
))
1196 result
= rates_484
[rate
->he_gi
];
1197 else if (rate
->bw
== RATE_INFO_BW_20
||
1198 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1199 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_242
))
1200 result
= rates_242
[rate
->he_gi
];
1201 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1202 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_106
)
1203 result
= rates_106
[rate
->he_gi
];
1204 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1205 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_52
)
1206 result
= rates_52
[rate
->he_gi
];
1207 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1208 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_26
)
1209 result
= rates_26
[rate
->he_gi
];
1210 else if (WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1211 rate
->bw
, rate
->he_ru_alloc
))
1214 /* now scale to the appropriate MCS */
1217 do_div(tmp
, mcs_divisors
[rate
->mcs
]);
1220 /* and take NSS, DCM into account */
1221 result
= (result
* rate
->nss
) / 8;
1228 u32
cfg80211_calculate_bitrate(struct rate_info
*rate
)
1230 if (rate
->flags
& RATE_INFO_FLAGS_MCS
)
1231 return cfg80211_calculate_bitrate_ht(rate
);
1232 if (rate
->flags
& RATE_INFO_FLAGS_60G
)
1233 return cfg80211_calculate_bitrate_60g(rate
);
1234 if (rate
->flags
& RATE_INFO_FLAGS_VHT_MCS
)
1235 return cfg80211_calculate_bitrate_vht(rate
);
1236 if (rate
->flags
& RATE_INFO_FLAGS_HE_MCS
)
1237 return cfg80211_calculate_bitrate_he(rate
);
1239 return rate
->legacy
;
1241 EXPORT_SYMBOL(cfg80211_calculate_bitrate
);
1243 int cfg80211_get_p2p_attr(const u8
*ies
, unsigned int len
,
1244 enum ieee80211_p2p_attr_id attr
,
1245 u8
*buf
, unsigned int bufsize
)
1248 u16 attr_remaining
= 0;
1249 bool desired_attr
= false;
1250 u16 desired_len
= 0;
1253 unsigned int iedatalen
;
1260 if (iedatalen
+ 2 > len
)
1263 if (ies
[0] != WLAN_EID_VENDOR_SPECIFIC
)
1271 /* check WFA OUI, P2P subtype */
1272 if (iedata
[0] != 0x50 || iedata
[1] != 0x6f ||
1273 iedata
[2] != 0x9a || iedata
[3] != 0x09)
1279 /* check attribute continuation into this IE */
1280 copy
= min_t(unsigned int, attr_remaining
, iedatalen
);
1281 if (copy
&& desired_attr
) {
1282 desired_len
+= copy
;
1284 memcpy(out
, iedata
, min(bufsize
, copy
));
1285 out
+= min(bufsize
, copy
);
1286 bufsize
-= min(bufsize
, copy
);
1290 if (copy
== attr_remaining
)
1294 attr_remaining
-= copy
;
1301 while (iedatalen
> 0) {
1304 /* P2P attribute ID & size must fit */
1307 desired_attr
= iedata
[0] == attr
;
1308 attr_len
= get_unaligned_le16(iedata
+ 1);
1312 copy
= min_t(unsigned int, attr_len
, iedatalen
);
1315 desired_len
+= copy
;
1317 memcpy(out
, iedata
, min(bufsize
, copy
));
1318 out
+= min(bufsize
, copy
);
1319 bufsize
-= min(bufsize
, copy
);
1322 if (copy
== attr_len
)
1328 attr_remaining
= attr_len
- copy
;
1336 if (attr_remaining
&& desired_attr
)
1341 EXPORT_SYMBOL(cfg80211_get_p2p_attr
);
1343 static bool ieee80211_id_in_list(const u8
*ids
, int n_ids
, u8 id
, bool id_ext
)
1347 /* Make sure array values are legal */
1348 if (WARN_ON(ids
[n_ids
- 1] == WLAN_EID_EXTENSION
))
1353 if (ids
[i
] == WLAN_EID_EXTENSION
) {
1354 if (id_ext
&& (ids
[i
+ 1] == id
))
1361 if (ids
[i
] == id
&& !id_ext
)
1369 static size_t skip_ie(const u8
*ies
, size_t ielen
, size_t pos
)
1371 /* we assume a validly formed IEs buffer */
1372 u8 len
= ies
[pos
+ 1];
1376 /* the IE itself must have 255 bytes for fragments to follow */
1380 while (pos
< ielen
&& ies
[pos
] == WLAN_EID_FRAGMENT
) {
1388 size_t ieee80211_ie_split_ric(const u8
*ies
, size_t ielen
,
1389 const u8
*ids
, int n_ids
,
1390 const u8
*after_ric
, int n_after_ric
,
1393 size_t pos
= offset
;
1395 while (pos
< ielen
) {
1398 if (ies
[pos
] == WLAN_EID_EXTENSION
)
1400 if ((pos
+ ext
) >= ielen
)
1403 if (!ieee80211_id_in_list(ids
, n_ids
, ies
[pos
+ ext
],
1404 ies
[pos
] == WLAN_EID_EXTENSION
))
1407 if (ies
[pos
] == WLAN_EID_RIC_DATA
&& n_after_ric
) {
1408 pos
= skip_ie(ies
, ielen
, pos
);
1410 while (pos
< ielen
) {
1411 if (ies
[pos
] == WLAN_EID_EXTENSION
)
1416 if ((pos
+ ext
) >= ielen
)
1419 if (!ieee80211_id_in_list(after_ric
,
1423 pos
= skip_ie(ies
, ielen
, pos
);
1428 pos
= skip_ie(ies
, ielen
, pos
);
1434 EXPORT_SYMBOL(ieee80211_ie_split_ric
);
1436 bool ieee80211_operating_class_to_band(u8 operating_class
,
1437 enum nl80211_band
*band
)
1439 switch (operating_class
) {
1443 *band
= NL80211_BAND_5GHZ
;
1449 *band
= NL80211_BAND_2GHZ
;
1452 *band
= NL80211_BAND_60GHZ
;
1458 EXPORT_SYMBOL(ieee80211_operating_class_to_band
);
1460 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def
*chandef
,
1464 u32 freq
= chandef
->center_freq1
;
1466 if (freq
>= 2412 && freq
<= 2472) {
1467 if (chandef
->width
> NL80211_CHAN_WIDTH_40
)
1470 /* 2.407 GHz, channels 1..13 */
1471 if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1472 if (freq
> chandef
->chan
->center_freq
)
1473 *op_class
= 83; /* HT40+ */
1475 *op_class
= 84; /* HT40- */
1484 if (chandef
->width
> NL80211_CHAN_WIDTH_40
)
1487 *op_class
= 82; /* channel 14 */
1491 switch (chandef
->width
) {
1492 case NL80211_CHAN_WIDTH_80
:
1495 case NL80211_CHAN_WIDTH_160
:
1498 case NL80211_CHAN_WIDTH_80P80
:
1501 case NL80211_CHAN_WIDTH_10
:
1502 case NL80211_CHAN_WIDTH_5
:
1503 return false; /* unsupported for now */
1509 /* 5 GHz, channels 36..48 */
1510 if (freq
>= 5180 && freq
<= 5240) {
1512 *op_class
= vht_opclass
;
1513 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1514 if (freq
> chandef
->chan
->center_freq
)
1525 /* 5 GHz, channels 52..64 */
1526 if (freq
>= 5260 && freq
<= 5320) {
1528 *op_class
= vht_opclass
;
1529 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1530 if (freq
> chandef
->chan
->center_freq
)
1541 /* 5 GHz, channels 100..144 */
1542 if (freq
>= 5500 && freq
<= 5720) {
1544 *op_class
= vht_opclass
;
1545 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1546 if (freq
> chandef
->chan
->center_freq
)
1557 /* 5 GHz, channels 149..169 */
1558 if (freq
>= 5745 && freq
<= 5845) {
1560 *op_class
= vht_opclass
;
1561 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1562 if (freq
> chandef
->chan
->center_freq
)
1566 } else if (freq
<= 5805) {
1575 /* 56.16 GHz, channel 1..4 */
1576 if (freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6) {
1577 if (chandef
->width
>= NL80211_CHAN_WIDTH_40
)
1584 /* not supported yet */
1587 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class
);
1589 static void cfg80211_calculate_bi_data(struct wiphy
*wiphy
, u32 new_beacon_int
,
1590 u32
*beacon_int_gcd
,
1591 bool *beacon_int_different
)
1593 struct wireless_dev
*wdev
;
1595 *beacon_int_gcd
= 0;
1596 *beacon_int_different
= false;
1598 list_for_each_entry(wdev
, &wiphy
->wdev_list
, list
) {
1599 if (!wdev
->beacon_interval
)
1602 if (!*beacon_int_gcd
) {
1603 *beacon_int_gcd
= wdev
->beacon_interval
;
1607 if (wdev
->beacon_interval
== *beacon_int_gcd
)
1610 *beacon_int_different
= true;
1611 *beacon_int_gcd
= gcd(*beacon_int_gcd
, wdev
->beacon_interval
);
1614 if (new_beacon_int
&& *beacon_int_gcd
!= new_beacon_int
) {
1615 if (*beacon_int_gcd
)
1616 *beacon_int_different
= true;
1617 *beacon_int_gcd
= gcd(*beacon_int_gcd
, new_beacon_int
);
1621 int cfg80211_validate_beacon_int(struct cfg80211_registered_device
*rdev
,
1622 enum nl80211_iftype iftype
, u32 beacon_int
)
1625 * This is just a basic pre-condition check; if interface combinations
1626 * are possible the driver must already be checking those with a call
1627 * to cfg80211_check_combinations(), in which case we'll validate more
1628 * through the cfg80211_calculate_bi_data() call and code in
1629 * cfg80211_iter_combinations().
1632 if (beacon_int
< 10 || beacon_int
> 10000)
1638 int cfg80211_iter_combinations(struct wiphy
*wiphy
,
1639 struct iface_combination_params
*params
,
1640 void (*iter
)(const struct ieee80211_iface_combination
*c
,
1644 const struct ieee80211_regdomain
*regdom
;
1645 enum nl80211_dfs_regions region
= 0;
1647 int num_interfaces
= 0;
1648 u32 used_iftypes
= 0;
1650 bool beacon_int_different
;
1653 * This is a bit strange, since the iteration used to rely only on
1654 * the data given by the driver, but here it now relies on context,
1655 * in form of the currently operating interfaces.
1656 * This is OK for all current users, and saves us from having to
1657 * push the GCD calculations into all the drivers.
1658 * In the future, this should probably rely more on data that's in
1659 * cfg80211 already - the only thing not would appear to be any new
1660 * interfaces (while being brought up) and channel/radar data.
1662 cfg80211_calculate_bi_data(wiphy
, params
->new_beacon_int
,
1663 &beacon_int_gcd
, &beacon_int_different
);
1665 if (params
->radar_detect
) {
1667 regdom
= rcu_dereference(cfg80211_regdomain
);
1669 region
= regdom
->dfs_region
;
1673 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1674 num_interfaces
+= params
->iftype_num
[iftype
];
1675 if (params
->iftype_num
[iftype
] > 0 &&
1676 !(wiphy
->software_iftypes
& BIT(iftype
)))
1677 used_iftypes
|= BIT(iftype
);
1680 for (i
= 0; i
< wiphy
->n_iface_combinations
; i
++) {
1681 const struct ieee80211_iface_combination
*c
;
1682 struct ieee80211_iface_limit
*limits
;
1683 u32 all_iftypes
= 0;
1685 c
= &wiphy
->iface_combinations
[i
];
1687 if (num_interfaces
> c
->max_interfaces
)
1689 if (params
->num_different_channels
> c
->num_different_channels
)
1692 limits
= kmemdup(c
->limits
, sizeof(limits
[0]) * c
->n_limits
,
1697 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1698 if (wiphy
->software_iftypes
& BIT(iftype
))
1700 for (j
= 0; j
< c
->n_limits
; j
++) {
1701 all_iftypes
|= limits
[j
].types
;
1702 if (!(limits
[j
].types
& BIT(iftype
)))
1704 if (limits
[j
].max
< params
->iftype_num
[iftype
])
1706 limits
[j
].max
-= params
->iftype_num
[iftype
];
1710 if (params
->radar_detect
!=
1711 (c
->radar_detect_widths
& params
->radar_detect
))
1714 if (params
->radar_detect
&& c
->radar_detect_regions
&&
1715 !(c
->radar_detect_regions
& BIT(region
)))
1718 /* Finally check that all iftypes that we're currently
1719 * using are actually part of this combination. If they
1720 * aren't then we can't use this combination and have
1721 * to continue to the next.
1723 if ((all_iftypes
& used_iftypes
) != used_iftypes
)
1726 if (beacon_int_gcd
) {
1727 if (c
->beacon_int_min_gcd
&&
1728 beacon_int_gcd
< c
->beacon_int_min_gcd
)
1730 if (!c
->beacon_int_min_gcd
&& beacon_int_different
)
1734 /* This combination covered all interface types and
1735 * supported the requested numbers, so we're good.
1745 EXPORT_SYMBOL(cfg80211_iter_combinations
);
1748 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination
*c
,
1755 int cfg80211_check_combinations(struct wiphy
*wiphy
,
1756 struct iface_combination_params
*params
)
1760 err
= cfg80211_iter_combinations(wiphy
, params
,
1761 cfg80211_iter_sum_ifcombs
, &num
);
1769 EXPORT_SYMBOL(cfg80211_check_combinations
);
1771 int ieee80211_get_ratemask(struct ieee80211_supported_band
*sband
,
1772 const u8
*rates
, unsigned int n_rates
,
1780 if (n_rates
== 0 || n_rates
> NL80211_MAX_SUPP_RATES
)
1785 for (i
= 0; i
< n_rates
; i
++) {
1786 int rate
= (rates
[i
] & 0x7f) * 5;
1789 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
1790 if (sband
->bitrates
[j
].bitrate
== rate
) {
1801 * mask must have at least one bit set here since we
1802 * didn't accept a 0-length rates array nor allowed
1803 * entries in the array that didn't exist
1809 unsigned int ieee80211_get_num_supported_channels(struct wiphy
*wiphy
)
1811 enum nl80211_band band
;
1812 unsigned int n_channels
= 0;
1814 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++)
1815 if (wiphy
->bands
[band
])
1816 n_channels
+= wiphy
->bands
[band
]->n_channels
;
1820 EXPORT_SYMBOL(ieee80211_get_num_supported_channels
);
1822 int cfg80211_get_station(struct net_device
*dev
, const u8
*mac_addr
,
1823 struct station_info
*sinfo
)
1825 struct cfg80211_registered_device
*rdev
;
1826 struct wireless_dev
*wdev
;
1828 wdev
= dev
->ieee80211_ptr
;
1832 rdev
= wiphy_to_rdev(wdev
->wiphy
);
1833 if (!rdev
->ops
->get_station
)
1836 memset(sinfo
, 0, sizeof(*sinfo
));
1838 return rdev_get_station(rdev
, dev
, mac_addr
, sinfo
);
1840 EXPORT_SYMBOL(cfg80211_get_station
);
1842 void cfg80211_free_nan_func(struct cfg80211_nan_func
*f
)
1849 kfree(f
->serv_spec_info
);
1852 for (i
= 0; i
< f
->num_rx_filters
; i
++)
1853 kfree(f
->rx_filters
[i
].filter
);
1855 for (i
= 0; i
< f
->num_tx_filters
; i
++)
1856 kfree(f
->tx_filters
[i
].filter
);
1858 kfree(f
->rx_filters
);
1859 kfree(f
->tx_filters
);
1862 EXPORT_SYMBOL(cfg80211_free_nan_func
);
1864 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range
*freq_range
,
1865 u32 center_freq_khz
, u32 bw_khz
)
1867 u32 start_freq_khz
, end_freq_khz
;
1869 start_freq_khz
= center_freq_khz
- (bw_khz
/ 2);
1870 end_freq_khz
= center_freq_khz
+ (bw_khz
/ 2);
1872 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
1873 end_freq_khz
<= freq_range
->end_freq_khz
)
1879 int cfg80211_sinfo_alloc_tid_stats(struct station_info
*sinfo
, gfp_t gfp
)
1881 sinfo
->pertid
= kcalloc(IEEE80211_NUM_TIDS
+ 1,
1882 sizeof(*(sinfo
->pertid
)),
1889 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats
);
1891 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1892 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1893 const unsigned char rfc1042_header
[] __aligned(2) =
1894 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1895 EXPORT_SYMBOL(rfc1042_header
);
1897 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1898 const unsigned char bridge_tunnel_header
[] __aligned(2) =
1899 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1900 EXPORT_SYMBOL(bridge_tunnel_header
);
1902 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1903 struct iapp_layer2_update
{
1904 u8 da
[ETH_ALEN
]; /* broadcast */
1905 u8 sa
[ETH_ALEN
]; /* STA addr */
1913 void cfg80211_send_layer2_update(struct net_device
*dev
, const u8
*addr
)
1915 struct iapp_layer2_update
*msg
;
1916 struct sk_buff
*skb
;
1918 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1921 skb
= dev_alloc_skb(sizeof(*msg
));
1924 msg
= skb_put(skb
, sizeof(*msg
));
1926 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1927 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1929 eth_broadcast_addr(msg
->da
);
1930 ether_addr_copy(msg
->sa
, addr
);
1931 msg
->len
= htons(6);
1933 msg
->ssap
= 0x01; /* NULL LSAP, CR Bit: Response */
1934 msg
->control
= 0xaf; /* XID response lsb.1111F101.
1935 * F=0 (no poll command; unsolicited frame) */
1936 msg
->xid_info
[0] = 0x81; /* XID format identifier */
1937 msg
->xid_info
[1] = 1; /* LLC types/classes: Type 1 LLC */
1938 msg
->xid_info
[2] = 0; /* XID sender's receive window size (RW) */
1941 skb
->protocol
= eth_type_trans(skb
, dev
);
1942 memset(skb
->cb
, 0, sizeof(skb
->cb
));
1945 EXPORT_SYMBOL(cfg80211_send_layer2_update
);
1947 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap
*cap
,
1948 enum ieee80211_vht_chanwidth bw
,
1949 int mcs
, bool ext_nss_bw_capable
)
1951 u16 map
= le16_to_cpu(cap
->supp_mcs
.rx_mcs_map
);
1952 int max_vht_nss
= 0;
1955 int i
, mcs_encoding
;
1960 if (WARN_ON(mcs
> 9))
1969 /* find max_vht_nss for the given MCS */
1970 for (i
= 7; i
>= 0; i
--) {
1971 int supp
= (map
>> (2 * i
)) & 3;
1976 if (supp
>= mcs_encoding
) {
1982 if (!(cap
->supp_mcs
.tx_mcs_map
&
1983 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE
)))
1986 ext_nss_bw
= le32_get_bits(cap
->vht_cap_info
,
1987 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK
);
1988 supp_width
= le32_get_bits(cap
->vht_cap_info
,
1989 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK
);
1991 /* if not capable, treat ext_nss_bw as 0 */
1992 if (!ext_nss_bw_capable
)
1995 /* This is invalid */
1996 if (supp_width
== 3)
1999 /* This is an invalid combination so pretend nothing is supported */
2000 if (supp_width
== 2 && (ext_nss_bw
== 1 || ext_nss_bw
== 2))
2004 * Cover all the special cases according to IEEE 802.11-2016
2005 * Table 9-250. All other cases are either factor of 1 or not
2009 case IEEE80211_VHT_CHANWIDTH_USE_HT
:
2010 case IEEE80211_VHT_CHANWIDTH_80MHZ
:
2011 if ((supp_width
== 1 || supp_width
== 2) &&
2013 return 2 * max_vht_nss
;
2015 case IEEE80211_VHT_CHANWIDTH_160MHZ
:
2016 if (supp_width
== 0 &&
2017 (ext_nss_bw
== 1 || ext_nss_bw
== 2))
2018 return max_vht_nss
/ 2;
2019 if (supp_width
== 0 &&
2021 return (3 * max_vht_nss
) / 4;
2022 if (supp_width
== 1 &&
2024 return 2 * max_vht_nss
;
2026 case IEEE80211_VHT_CHANWIDTH_80P80MHZ
:
2027 if (supp_width
== 0 && ext_nss_bw
== 1)
2028 return 0; /* not possible */
2029 if (supp_width
== 0 &&
2031 return max_vht_nss
/ 2;
2032 if (supp_width
== 0 &&
2034 return (3 * max_vht_nss
) / 4;
2035 if (supp_width
== 1 &&
2037 return 0; /* not possible */
2038 if (supp_width
== 1 &&
2040 return max_vht_nss
/ 2;
2041 if (supp_width
== 1 &&
2043 return (3 * max_vht_nss
) / 4;
2047 /* not covered or invalid combination received */
2050 EXPORT_SYMBOL(ieee80211_get_vht_max_nss
);