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-2019 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 int ieee80211_channel_to_frequency(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
:
86 return 2407 + chan
* 5;
88 case NL80211_BAND_5GHZ
:
89 if (chan
>= 182 && chan
<= 196)
90 return 4000 + chan
* 5;
92 return 5000 + chan
* 5;
94 case NL80211_BAND_6GHZ
:
95 /* see 802.11ax D4.1 27.3.22.2 */
97 return 5940 + chan
* 5;
99 case NL80211_BAND_60GHZ
:
101 return 56160 + chan
* 2160;
106 return 0; /* not supported */
108 EXPORT_SYMBOL(ieee80211_channel_to_frequency
);
110 int ieee80211_frequency_to_channel(int freq
)
112 /* see 802.11 17.3.8.3.2 and Annex J */
115 else if (freq
< 2484)
116 return (freq
- 2407) / 5;
117 else if (freq
>= 4910 && freq
<= 4980)
118 return (freq
- 4000) / 5;
119 else if (freq
< 5945)
120 return (freq
- 5000) / 5;
121 else if (freq
<= 45000) /* DMG band lower limit */
122 /* see 802.11ax D4.1 27.3.22.2 */
123 return (freq
- 5940) / 5;
124 else if (freq
>= 58320 && freq
<= 70200)
125 return (freq
- 56160) / 2160;
129 EXPORT_SYMBOL(ieee80211_frequency_to_channel
);
131 struct ieee80211_channel
*ieee80211_get_channel(struct wiphy
*wiphy
, int freq
)
133 enum nl80211_band band
;
134 struct ieee80211_supported_band
*sband
;
137 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
138 sband
= wiphy
->bands
[band
];
143 for (i
= 0; i
< sband
->n_channels
; i
++) {
144 if (sband
->channels
[i
].center_freq
== freq
)
145 return &sband
->channels
[i
];
151 EXPORT_SYMBOL(ieee80211_get_channel
);
153 static void set_mandatory_flags_band(struct ieee80211_supported_band
*sband
)
157 switch (sband
->band
) {
158 case NL80211_BAND_5GHZ
:
159 case NL80211_BAND_6GHZ
:
161 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
162 if (sband
->bitrates
[i
].bitrate
== 60 ||
163 sband
->bitrates
[i
].bitrate
== 120 ||
164 sband
->bitrates
[i
].bitrate
== 240) {
165 sband
->bitrates
[i
].flags
|=
166 IEEE80211_RATE_MANDATORY_A
;
172 case NL80211_BAND_2GHZ
:
174 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
175 switch (sband
->bitrates
[i
].bitrate
) {
180 sband
->bitrates
[i
].flags
|=
181 IEEE80211_RATE_MANDATORY_B
|
182 IEEE80211_RATE_MANDATORY_G
;
188 sband
->bitrates
[i
].flags
|=
189 IEEE80211_RATE_MANDATORY_G
;
193 sband
->bitrates
[i
].flags
|=
194 IEEE80211_RATE_ERP_G
;
198 WARN_ON(want
!= 0 && want
!= 3);
200 case NL80211_BAND_60GHZ
:
201 /* check for mandatory HT MCS 1..4 */
202 WARN_ON(!sband
->ht_cap
.ht_supported
);
203 WARN_ON((sband
->ht_cap
.mcs
.rx_mask
[0] & 0x1e) != 0x1e);
205 case NUM_NL80211_BANDS
:
212 void ieee80211_set_bitrate_flags(struct wiphy
*wiphy
)
214 enum nl80211_band band
;
216 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++)
217 if (wiphy
->bands
[band
])
218 set_mandatory_flags_band(wiphy
->bands
[band
]);
221 bool cfg80211_supported_cipher_suite(struct wiphy
*wiphy
, u32 cipher
)
224 for (i
= 0; i
< wiphy
->n_cipher_suites
; i
++)
225 if (cipher
== wiphy
->cipher_suites
[i
])
230 int cfg80211_validate_key_settings(struct cfg80211_registered_device
*rdev
,
231 struct key_params
*params
, int key_idx
,
232 bool pairwise
, const u8
*mac_addr
)
236 if (wiphy_ext_feature_isset(&rdev
->wiphy
,
237 NL80211_EXT_FEATURE_BEACON_PROTECTION
))
239 if (key_idx
< 0 || key_idx
> max_key_idx
)
242 if (!pairwise
&& mac_addr
&& !(rdev
->wiphy
.flags
& WIPHY_FLAG_IBSS_RSN
))
245 if (pairwise
&& !mac_addr
)
248 switch (params
->cipher
) {
249 case WLAN_CIPHER_SUITE_TKIP
:
250 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
251 if ((pairwise
&& key_idx
) ||
252 params
->mode
!= NL80211_KEY_RX_TX
)
255 case WLAN_CIPHER_SUITE_CCMP
:
256 case WLAN_CIPHER_SUITE_CCMP_256
:
257 case WLAN_CIPHER_SUITE_GCMP
:
258 case WLAN_CIPHER_SUITE_GCMP_256
:
259 /* IEEE802.11-2016 allows only 0 and - when supporting
260 * Extended Key ID - 1 as index for pairwise keys.
261 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
262 * the driver supports Extended Key ID.
263 * @NL80211_KEY_SET_TX can't be set when installing and
266 if ((params
->mode
== NL80211_KEY_NO_TX
&& !pairwise
) ||
267 params
->mode
== NL80211_KEY_SET_TX
)
269 if (wiphy_ext_feature_isset(&rdev
->wiphy
,
270 NL80211_EXT_FEATURE_EXT_KEY_ID
)) {
271 if (pairwise
&& (key_idx
< 0 || key_idx
> 1))
273 } else if (pairwise
&& key_idx
) {
277 case WLAN_CIPHER_SUITE_AES_CMAC
:
278 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
279 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
280 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
281 /* Disallow BIP (group-only) cipher as pairwise cipher */
287 case WLAN_CIPHER_SUITE_WEP40
:
288 case WLAN_CIPHER_SUITE_WEP104
:
295 switch (params
->cipher
) {
296 case WLAN_CIPHER_SUITE_WEP40
:
297 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
300 case WLAN_CIPHER_SUITE_TKIP
:
301 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
304 case WLAN_CIPHER_SUITE_CCMP
:
305 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
308 case WLAN_CIPHER_SUITE_CCMP_256
:
309 if (params
->key_len
!= WLAN_KEY_LEN_CCMP_256
)
312 case WLAN_CIPHER_SUITE_GCMP
:
313 if (params
->key_len
!= WLAN_KEY_LEN_GCMP
)
316 case WLAN_CIPHER_SUITE_GCMP_256
:
317 if (params
->key_len
!= WLAN_KEY_LEN_GCMP_256
)
320 case WLAN_CIPHER_SUITE_WEP104
:
321 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
324 case WLAN_CIPHER_SUITE_AES_CMAC
:
325 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
328 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
329 if (params
->key_len
!= WLAN_KEY_LEN_BIP_CMAC_256
)
332 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
333 if (params
->key_len
!= WLAN_KEY_LEN_BIP_GMAC_128
)
336 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
337 if (params
->key_len
!= WLAN_KEY_LEN_BIP_GMAC_256
)
342 * We don't know anything about this algorithm,
343 * allow using it -- but the driver must check
344 * all parameters! We still check below whether
345 * or not the driver supports this algorithm,
352 switch (params
->cipher
) {
353 case WLAN_CIPHER_SUITE_WEP40
:
354 case WLAN_CIPHER_SUITE_WEP104
:
355 /* These ciphers do not use key sequence */
357 case WLAN_CIPHER_SUITE_TKIP
:
358 case WLAN_CIPHER_SUITE_CCMP
:
359 case WLAN_CIPHER_SUITE_CCMP_256
:
360 case WLAN_CIPHER_SUITE_GCMP
:
361 case WLAN_CIPHER_SUITE_GCMP_256
:
362 case WLAN_CIPHER_SUITE_AES_CMAC
:
363 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
364 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
365 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
366 if (params
->seq_len
!= 6)
372 if (!cfg80211_supported_cipher_suite(&rdev
->wiphy
, params
->cipher
))
378 unsigned int __attribute_const__
ieee80211_hdrlen(__le16 fc
)
380 unsigned int hdrlen
= 24;
382 if (ieee80211_is_data(fc
)) {
383 if (ieee80211_has_a4(fc
))
385 if (ieee80211_is_data_qos(fc
)) {
386 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
387 if (ieee80211_has_order(fc
))
388 hdrlen
+= IEEE80211_HT_CTL_LEN
;
393 if (ieee80211_is_mgmt(fc
)) {
394 if (ieee80211_has_order(fc
))
395 hdrlen
+= IEEE80211_HT_CTL_LEN
;
399 if (ieee80211_is_ctl(fc
)) {
401 * ACK and CTS are 10 bytes, all others 16. To see how
402 * to get this condition consider
403 * subtype mask: 0b0000000011110000 (0x00F0)
404 * ACK subtype: 0b0000000011010000 (0x00D0)
405 * CTS subtype: 0b0000000011000000 (0x00C0)
406 * bits that matter: ^^^ (0x00E0)
407 * value of those: 0b0000000011000000 (0x00C0)
409 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
417 EXPORT_SYMBOL(ieee80211_hdrlen
);
419 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
421 const struct ieee80211_hdr
*hdr
=
422 (const struct ieee80211_hdr
*)skb
->data
;
425 if (unlikely(skb
->len
< 10))
427 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
428 if (unlikely(hdrlen
> skb
->len
))
432 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
434 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags
)
436 int ae
= flags
& MESH_FLAGS_AE
;
437 /* 802.11-2012, 8.2.4.7.3 */
442 case MESH_FLAGS_AE_A4
:
444 case MESH_FLAGS_AE_A5_A6
:
449 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
451 return __ieee80211_get_mesh_hdrlen(meshhdr
->flags
);
453 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen
);
455 int ieee80211_data_to_8023_exthdr(struct sk_buff
*skb
, struct ethhdr
*ehdr
,
456 const u8
*addr
, enum nl80211_iftype iftype
,
459 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
461 u8 hdr
[ETH_ALEN
] __aligned(2);
468 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
471 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
) + data_offset
;
472 if (skb
->len
< hdrlen
+ 8)
475 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
477 * IEEE 802.11 address fields:
478 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
479 * 0 0 DA SA BSSID n/a
480 * 0 1 DA BSSID SA n/a
481 * 1 0 BSSID SA DA n/a
484 memcpy(tmp
.h_dest
, ieee80211_get_DA(hdr
), ETH_ALEN
);
485 memcpy(tmp
.h_source
, ieee80211_get_SA(hdr
), ETH_ALEN
);
487 if (iftype
== NL80211_IFTYPE_MESH_POINT
)
488 skb_copy_bits(skb
, hdrlen
, &mesh_flags
, 1);
490 mesh_flags
&= MESH_FLAGS_AE
;
492 switch (hdr
->frame_control
&
493 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
494 case cpu_to_le16(IEEE80211_FCTL_TODS
):
495 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
496 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
497 iftype
!= NL80211_IFTYPE_P2P_GO
))
500 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
501 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
502 iftype
!= NL80211_IFTYPE_MESH_POINT
&&
503 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
504 iftype
!= NL80211_IFTYPE_STATION
))
506 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
507 if (mesh_flags
== MESH_FLAGS_AE_A4
)
509 if (mesh_flags
== MESH_FLAGS_AE_A5_A6
) {
510 skb_copy_bits(skb
, hdrlen
+
511 offsetof(struct ieee80211s_hdr
, eaddr1
),
512 tmp
.h_dest
, 2 * ETH_ALEN
);
514 hdrlen
+= __ieee80211_get_mesh_hdrlen(mesh_flags
);
517 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
518 if ((iftype
!= NL80211_IFTYPE_STATION
&&
519 iftype
!= NL80211_IFTYPE_P2P_CLIENT
&&
520 iftype
!= NL80211_IFTYPE_MESH_POINT
) ||
521 (is_multicast_ether_addr(tmp
.h_dest
) &&
522 ether_addr_equal(tmp
.h_source
, addr
)))
524 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
525 if (mesh_flags
== MESH_FLAGS_AE_A5_A6
)
527 if (mesh_flags
== MESH_FLAGS_AE_A4
)
528 skb_copy_bits(skb
, hdrlen
+
529 offsetof(struct ieee80211s_hdr
, eaddr1
),
530 tmp
.h_source
, ETH_ALEN
);
531 hdrlen
+= __ieee80211_get_mesh_hdrlen(mesh_flags
);
535 if (iftype
!= NL80211_IFTYPE_ADHOC
&&
536 iftype
!= NL80211_IFTYPE_STATION
&&
537 iftype
!= NL80211_IFTYPE_OCB
)
542 skb_copy_bits(skb
, hdrlen
, &payload
, sizeof(payload
));
543 tmp
.h_proto
= payload
.proto
;
545 if (likely((ether_addr_equal(payload
.hdr
, rfc1042_header
) &&
546 tmp
.h_proto
!= htons(ETH_P_AARP
) &&
547 tmp
.h_proto
!= htons(ETH_P_IPX
)) ||
548 ether_addr_equal(payload
.hdr
, bridge_tunnel_header
)))
549 /* remove RFC1042 or Bridge-Tunnel encapsulation and
550 * replace EtherType */
551 hdrlen
+= ETH_ALEN
+ 2;
553 tmp
.h_proto
= htons(skb
->len
- hdrlen
);
555 pskb_pull(skb
, hdrlen
);
558 ehdr
= skb_push(skb
, sizeof(struct ethhdr
));
559 memcpy(ehdr
, &tmp
, sizeof(tmp
));
563 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr
);
566 __frame_add_frag(struct sk_buff
*skb
, struct page
*page
,
567 void *ptr
, int len
, int size
)
569 struct skb_shared_info
*sh
= skb_shinfo(skb
);
573 page_offset
= ptr
- page_address(page
);
574 skb_add_rx_frag(skb
, sh
->nr_frags
, page
, page_offset
, len
, size
);
578 __ieee80211_amsdu_copy_frag(struct sk_buff
*skb
, struct sk_buff
*frame
,
581 struct skb_shared_info
*sh
= skb_shinfo(skb
);
582 const skb_frag_t
*frag
= &sh
->frags
[0];
583 struct page
*frag_page
;
585 int frag_len
, frag_size
;
586 int head_size
= skb
->len
- skb
->data_len
;
589 frag_page
= virt_to_head_page(skb
->head
);
590 frag_ptr
= skb
->data
;
591 frag_size
= head_size
;
593 while (offset
>= frag_size
) {
595 frag_page
= skb_frag_page(frag
);
596 frag_ptr
= skb_frag_address(frag
);
597 frag_size
= skb_frag_size(frag
);
602 frag_len
= frag_size
- offset
;
604 cur_len
= min(len
, frag_len
);
606 __frame_add_frag(frame
, frag_page
, frag_ptr
, cur_len
, frag_size
);
610 frag_len
= skb_frag_size(frag
);
611 cur_len
= min(len
, frag_len
);
612 __frame_add_frag(frame
, skb_frag_page(frag
),
613 skb_frag_address(frag
), cur_len
, frag_len
);
619 static struct sk_buff
*
620 __ieee80211_amsdu_copy(struct sk_buff
*skb
, unsigned int hlen
,
621 int offset
, int len
, bool reuse_frag
)
623 struct sk_buff
*frame
;
626 if (skb
->len
- offset
< len
)
630 * When reusing framents, copy some data to the head to simplify
631 * ethernet header handling and speed up protocol header processing
632 * in the stack later.
635 cur_len
= min_t(int, len
, 32);
638 * Allocate and reserve two bytes more for payload
639 * alignment since sizeof(struct ethhdr) is 14.
641 frame
= dev_alloc_skb(hlen
+ sizeof(struct ethhdr
) + 2 + cur_len
);
645 skb_reserve(frame
, hlen
+ sizeof(struct ethhdr
) + 2);
646 skb_copy_bits(skb
, offset
, skb_put(frame
, cur_len
), cur_len
);
653 __ieee80211_amsdu_copy_frag(skb
, frame
, offset
, len
);
658 void ieee80211_amsdu_to_8023s(struct sk_buff
*skb
, struct sk_buff_head
*list
,
659 const u8
*addr
, enum nl80211_iftype iftype
,
660 const unsigned int extra_headroom
,
661 const u8
*check_da
, const u8
*check_sa
)
663 unsigned int hlen
= ALIGN(extra_headroom
, 4);
664 struct sk_buff
*frame
= NULL
;
667 int offset
= 0, remaining
;
669 bool reuse_frag
= skb
->head_frag
&& !skb_has_frag_list(skb
);
670 bool reuse_skb
= false;
674 unsigned int subframe_len
;
678 skb_copy_bits(skb
, offset
, ð
, sizeof(eth
));
679 len
= ntohs(eth
.h_proto
);
680 subframe_len
= sizeof(struct ethhdr
) + len
;
681 padding
= (4 - subframe_len
) & 0x3;
683 /* the last MSDU has no padding */
684 remaining
= skb
->len
- offset
;
685 if (subframe_len
> remaining
)
688 offset
+= sizeof(struct ethhdr
);
689 last
= remaining
<= subframe_len
+ padding
;
691 /* FIXME: should we really accept multicast DA? */
692 if ((check_da
&& !is_multicast_ether_addr(eth
.h_dest
) &&
693 !ether_addr_equal(check_da
, eth
.h_dest
)) ||
694 (check_sa
&& !ether_addr_equal(check_sa
, eth
.h_source
))) {
695 offset
+= len
+ padding
;
699 /* reuse skb for the last subframe */
700 if (!skb_is_nonlinear(skb
) && !reuse_frag
&& last
) {
701 skb_pull(skb
, offset
);
705 frame
= __ieee80211_amsdu_copy(skb
, hlen
, offset
, len
,
710 offset
+= len
+ padding
;
713 skb_reset_network_header(frame
);
714 frame
->dev
= skb
->dev
;
715 frame
->priority
= skb
->priority
;
717 payload
= frame
->data
;
718 ethertype
= (payload
[6] << 8) | payload
[7];
719 if (likely((ether_addr_equal(payload
, rfc1042_header
) &&
720 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
721 ether_addr_equal(payload
, bridge_tunnel_header
))) {
722 eth
.h_proto
= htons(ethertype
);
723 skb_pull(frame
, ETH_ALEN
+ 2);
726 memcpy(skb_push(frame
, sizeof(eth
)), ð
, sizeof(eth
));
727 __skb_queue_tail(list
, frame
);
736 __skb_queue_purge(list
);
739 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s
);
741 /* Given a data frame determine the 802.1p/1d tag to use. */
742 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
,
743 struct cfg80211_qos_map
*qos_map
)
746 unsigned char vlan_priority
;
749 /* skb->priority values from 256->263 are magic values to
750 * directly indicate a specific 802.1d priority. This is used
751 * to allow 802.1d priority to be passed directly in from VLAN
754 if (skb
->priority
>= 256 && skb
->priority
<= 263) {
755 ret
= skb
->priority
- 256;
759 if (skb_vlan_tag_present(skb
)) {
760 vlan_priority
= (skb_vlan_tag_get(skb
) & VLAN_PRIO_MASK
)
762 if (vlan_priority
> 0) {
768 switch (skb
->protocol
) {
769 case htons(ETH_P_IP
):
770 dscp
= ipv4_get_dsfield(ip_hdr(skb
)) & 0xfc;
772 case htons(ETH_P_IPV6
):
773 dscp
= ipv6_get_dsfield(ipv6_hdr(skb
)) & 0xfc;
775 case htons(ETH_P_MPLS_UC
):
776 case htons(ETH_P_MPLS_MC
): {
777 struct mpls_label mpls_tmp
, *mpls
;
779 mpls
= skb_header_pointer(skb
, sizeof(struct ethhdr
),
780 sizeof(*mpls
), &mpls_tmp
);
784 ret
= (ntohl(mpls
->entry
) & MPLS_LS_TC_MASK
)
788 case htons(ETH_P_80221
):
789 /* 802.21 is always network control traffic */
796 unsigned int i
, tmp_dscp
= dscp
>> 2;
798 for (i
= 0; i
< qos_map
->num_des
; i
++) {
799 if (tmp_dscp
== qos_map
->dscp_exception
[i
].dscp
) {
800 ret
= qos_map
->dscp_exception
[i
].up
;
805 for (i
= 0; i
< 8; i
++) {
806 if (tmp_dscp
>= qos_map
->up
[i
].low
&&
807 tmp_dscp
<= qos_map
->up
[i
].high
) {
816 return array_index_nospec(ret
, IEEE80211_NUM_TIDS
);
818 EXPORT_SYMBOL(cfg80211_classify8021d
);
820 const struct element
*ieee80211_bss_get_elem(struct cfg80211_bss
*bss
, u8 id
)
822 const struct cfg80211_bss_ies
*ies
;
824 ies
= rcu_dereference(bss
->ies
);
828 return cfg80211_find_elem(id
, ies
->data
, ies
->len
);
830 EXPORT_SYMBOL(ieee80211_bss_get_elem
);
832 void cfg80211_upload_connect_keys(struct wireless_dev
*wdev
)
834 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
835 struct net_device
*dev
= wdev
->netdev
;
838 if (!wdev
->connect_keys
)
841 for (i
= 0; i
< CFG80211_MAX_WEP_KEYS
; i
++) {
842 if (!wdev
->connect_keys
->params
[i
].cipher
)
844 if (rdev_add_key(rdev
, dev
, i
, false, NULL
,
845 &wdev
->connect_keys
->params
[i
])) {
846 netdev_err(dev
, "failed to set key %d\n", i
);
849 if (wdev
->connect_keys
->def
== i
&&
850 rdev_set_default_key(rdev
, dev
, i
, true, true)) {
851 netdev_err(dev
, "failed to set defkey %d\n", i
);
856 kzfree(wdev
->connect_keys
);
857 wdev
->connect_keys
= NULL
;
860 void cfg80211_process_wdev_events(struct wireless_dev
*wdev
)
862 struct cfg80211_event
*ev
;
865 spin_lock_irqsave(&wdev
->event_lock
, flags
);
866 while (!list_empty(&wdev
->event_list
)) {
867 ev
= list_first_entry(&wdev
->event_list
,
868 struct cfg80211_event
, list
);
870 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
874 case EVENT_CONNECT_RESULT
:
875 __cfg80211_connect_result(
878 ev
->cr
.status
== WLAN_STATUS_SUCCESS
);
881 __cfg80211_roamed(wdev
, &ev
->rm
);
883 case EVENT_DISCONNECTED
:
884 __cfg80211_disconnected(wdev
->netdev
,
885 ev
->dc
.ie
, ev
->dc
.ie_len
,
887 !ev
->dc
.locally_generated
);
889 case EVENT_IBSS_JOINED
:
890 __cfg80211_ibss_joined(wdev
->netdev
, ev
->ij
.bssid
,
894 __cfg80211_leave(wiphy_to_rdev(wdev
->wiphy
), wdev
);
896 case EVENT_PORT_AUTHORIZED
:
897 __cfg80211_port_authorized(wdev
, ev
->pa
.bssid
);
904 spin_lock_irqsave(&wdev
->event_lock
, flags
);
906 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
909 void cfg80211_process_rdev_events(struct cfg80211_registered_device
*rdev
)
911 struct wireless_dev
*wdev
;
915 list_for_each_entry(wdev
, &rdev
->wiphy
.wdev_list
, list
)
916 cfg80211_process_wdev_events(wdev
);
919 int cfg80211_change_iface(struct cfg80211_registered_device
*rdev
,
920 struct net_device
*dev
, enum nl80211_iftype ntype
,
921 struct vif_params
*params
)
924 enum nl80211_iftype otype
= dev
->ieee80211_ptr
->iftype
;
928 /* don't support changing VLANs, you just re-create them */
929 if (otype
== NL80211_IFTYPE_AP_VLAN
)
932 /* cannot change into P2P device or NAN */
933 if (ntype
== NL80211_IFTYPE_P2P_DEVICE
||
934 ntype
== NL80211_IFTYPE_NAN
)
937 if (!rdev
->ops
->change_virtual_intf
||
938 !(rdev
->wiphy
.interface_modes
& (1 << ntype
)))
941 /* if it's part of a bridge, reject changing type to station/ibss */
942 if (netif_is_bridge_port(dev
) &&
943 (ntype
== NL80211_IFTYPE_ADHOC
||
944 ntype
== NL80211_IFTYPE_STATION
||
945 ntype
== NL80211_IFTYPE_P2P_CLIENT
))
948 if (ntype
!= otype
) {
949 dev
->ieee80211_ptr
->use_4addr
= false;
950 dev
->ieee80211_ptr
->mesh_id_up_len
= 0;
951 wdev_lock(dev
->ieee80211_ptr
);
952 rdev_set_qos_map(rdev
, dev
, NULL
);
953 wdev_unlock(dev
->ieee80211_ptr
);
956 case NL80211_IFTYPE_AP
:
957 cfg80211_stop_ap(rdev
, dev
, true);
959 case NL80211_IFTYPE_ADHOC
:
960 cfg80211_leave_ibss(rdev
, dev
, false);
962 case NL80211_IFTYPE_STATION
:
963 case NL80211_IFTYPE_P2P_CLIENT
:
964 wdev_lock(dev
->ieee80211_ptr
);
965 cfg80211_disconnect(rdev
, dev
,
966 WLAN_REASON_DEAUTH_LEAVING
, true);
967 wdev_unlock(dev
->ieee80211_ptr
);
969 case NL80211_IFTYPE_MESH_POINT
:
970 /* mesh should be handled? */
976 cfg80211_process_rdev_events(rdev
);
977 cfg80211_mlme_purge_registrations(dev
->ieee80211_ptr
);
980 err
= rdev_change_virtual_intf(rdev
, dev
, ntype
, params
);
982 WARN_ON(!err
&& dev
->ieee80211_ptr
->iftype
!= ntype
);
984 if (!err
&& params
&& params
->use_4addr
!= -1)
985 dev
->ieee80211_ptr
->use_4addr
= params
->use_4addr
;
988 dev
->priv_flags
&= ~IFF_DONT_BRIDGE
;
990 case NL80211_IFTYPE_STATION
:
991 if (dev
->ieee80211_ptr
->use_4addr
)
994 case NL80211_IFTYPE_OCB
:
995 case NL80211_IFTYPE_P2P_CLIENT
:
996 case NL80211_IFTYPE_ADHOC
:
997 dev
->priv_flags
|= IFF_DONT_BRIDGE
;
999 case NL80211_IFTYPE_P2P_GO
:
1000 case NL80211_IFTYPE_AP
:
1001 case NL80211_IFTYPE_AP_VLAN
:
1002 case NL80211_IFTYPE_WDS
:
1003 case NL80211_IFTYPE_MESH_POINT
:
1006 case NL80211_IFTYPE_MONITOR
:
1007 /* monitor can't bridge anyway */
1009 case NL80211_IFTYPE_UNSPECIFIED
:
1010 case NUM_NL80211_IFTYPES
:
1013 case NL80211_IFTYPE_P2P_DEVICE
:
1014 case NL80211_IFTYPE_NAN
:
1020 if (!err
&& ntype
!= otype
&& netif_running(dev
)) {
1021 cfg80211_update_iface_num(rdev
, ntype
, 1);
1022 cfg80211_update_iface_num(rdev
, otype
, -1);
1028 static u32
cfg80211_calculate_bitrate_ht(struct rate_info
*rate
)
1030 int modulation
, streams
, bitrate
;
1032 /* the formula below does only work for MCS values smaller than 32 */
1033 if (WARN_ON_ONCE(rate
->mcs
>= 32))
1036 modulation
= rate
->mcs
& 7;
1037 streams
= (rate
->mcs
>> 3) + 1;
1039 bitrate
= (rate
->bw
== RATE_INFO_BW_40
) ? 13500000 : 6500000;
1042 bitrate
*= (modulation
+ 1);
1043 else if (modulation
== 4)
1044 bitrate
*= (modulation
+ 2);
1046 bitrate
*= (modulation
+ 3);
1050 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1051 bitrate
= (bitrate
/ 9) * 10;
1053 /* do NOT round down here */
1054 return (bitrate
+ 50000) / 100000;
1057 static u32
cfg80211_calculate_bitrate_dmg(struct rate_info
*rate
)
1059 static const u32 __mcs2bitrate
[] = {
1067 [5] = 12512, /* 1251.25 mbps */
1077 [14] = 8662, /* 866.25 mbps */
1087 [24] = 67568, /* 6756.75 mbps */
1098 if (WARN_ON_ONCE(rate
->mcs
>= ARRAY_SIZE(__mcs2bitrate
)))
1101 return __mcs2bitrate
[rate
->mcs
];
1104 static u32
cfg80211_calculate_bitrate_edmg(struct rate_info
*rate
)
1106 static const u32 __mcs2bitrate
[] = {
1114 [5] = 12512, /* 1251.25 mbps */
1132 if (WARN_ON_ONCE(rate
->mcs
>= ARRAY_SIZE(__mcs2bitrate
)))
1135 return __mcs2bitrate
[rate
->mcs
] * rate
->n_bonded_ch
;
1138 static u32
cfg80211_calculate_bitrate_vht(struct rate_info
*rate
)
1140 static const u32 base
[4][10] = {
1150 /* not in the spec, but some devices use this: */
1194 case RATE_INFO_BW_160
:
1197 case RATE_INFO_BW_80
:
1200 case RATE_INFO_BW_40
:
1203 case RATE_INFO_BW_5
:
1204 case RATE_INFO_BW_10
:
1207 case RATE_INFO_BW_20
:
1211 bitrate
= base
[idx
][rate
->mcs
];
1212 bitrate
*= rate
->nss
;
1214 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
1215 bitrate
= (bitrate
/ 9) * 10;
1217 /* do NOT round down here */
1218 return (bitrate
+ 50000) / 100000;
1220 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1221 rate
->bw
, rate
->mcs
, rate
->nss
);
1225 static u32
cfg80211_calculate_bitrate_he(struct rate_info
*rate
)
1228 u16 mcs_divisors
[12] = {
1229 34133, /* 16.666666... */
1230 17067, /* 8.333333... */
1231 11378, /* 5.555555... */
1232 8533, /* 4.166666... */
1233 5689, /* 2.777777... */
1234 4267, /* 2.083333... */
1235 3923, /* 1.851851... */
1236 3413, /* 1.666666... */
1237 2844, /* 1.388888... */
1238 2560, /* 1.250000... */
1239 2276, /* 1.111111... */
1240 2048, /* 1.000000... */
1242 u32 rates_160M
[3] = { 960777777, 907400000, 816666666 };
1243 u32 rates_969
[3] = { 480388888, 453700000, 408333333 };
1244 u32 rates_484
[3] = { 229411111, 216666666, 195000000 };
1245 u32 rates_242
[3] = { 114711111, 108333333, 97500000 };
1246 u32 rates_106
[3] = { 40000000, 37777777, 34000000 };
1247 u32 rates_52
[3] = { 18820000, 17777777, 16000000 };
1248 u32 rates_26
[3] = { 9411111, 8888888, 8000000 };
1252 if (WARN_ON_ONCE(rate
->mcs
> 11))
1255 if (WARN_ON_ONCE(rate
->he_gi
> NL80211_RATE_INFO_HE_GI_3_2
))
1257 if (WARN_ON_ONCE(rate
->he_ru_alloc
>
1258 NL80211_RATE_INFO_HE_RU_ALLOC_2x996
))
1260 if (WARN_ON_ONCE(rate
->nss
< 1 || rate
->nss
> 8))
1263 if (rate
->bw
== RATE_INFO_BW_160
)
1264 result
= rates_160M
[rate
->he_gi
];
1265 else if (rate
->bw
== RATE_INFO_BW_80
||
1266 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1267 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_996
))
1268 result
= rates_969
[rate
->he_gi
];
1269 else if (rate
->bw
== RATE_INFO_BW_40
||
1270 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1271 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_484
))
1272 result
= rates_484
[rate
->he_gi
];
1273 else if (rate
->bw
== RATE_INFO_BW_20
||
1274 (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1275 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_242
))
1276 result
= rates_242
[rate
->he_gi
];
1277 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1278 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_106
)
1279 result
= rates_106
[rate
->he_gi
];
1280 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1281 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_52
)
1282 result
= rates_52
[rate
->he_gi
];
1283 else if (rate
->bw
== RATE_INFO_BW_HE_RU
&&
1284 rate
->he_ru_alloc
== NL80211_RATE_INFO_HE_RU_ALLOC_26
)
1285 result
= rates_26
[rate
->he_gi
];
1287 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1288 rate
->bw
, rate
->he_ru_alloc
);
1292 /* now scale to the appropriate MCS */
1295 do_div(tmp
, mcs_divisors
[rate
->mcs
]);
1298 /* and take NSS, DCM into account */
1299 result
= (result
* rate
->nss
) / 8;
1303 return result
/ 10000;
1306 u32
cfg80211_calculate_bitrate(struct rate_info
*rate
)
1308 if (rate
->flags
& RATE_INFO_FLAGS_MCS
)
1309 return cfg80211_calculate_bitrate_ht(rate
);
1310 if (rate
->flags
& RATE_INFO_FLAGS_DMG
)
1311 return cfg80211_calculate_bitrate_dmg(rate
);
1312 if (rate
->flags
& RATE_INFO_FLAGS_EDMG
)
1313 return cfg80211_calculate_bitrate_edmg(rate
);
1314 if (rate
->flags
& RATE_INFO_FLAGS_VHT_MCS
)
1315 return cfg80211_calculate_bitrate_vht(rate
);
1316 if (rate
->flags
& RATE_INFO_FLAGS_HE_MCS
)
1317 return cfg80211_calculate_bitrate_he(rate
);
1319 return rate
->legacy
;
1321 EXPORT_SYMBOL(cfg80211_calculate_bitrate
);
1323 int cfg80211_get_p2p_attr(const u8
*ies
, unsigned int len
,
1324 enum ieee80211_p2p_attr_id attr
,
1325 u8
*buf
, unsigned int bufsize
)
1328 u16 attr_remaining
= 0;
1329 bool desired_attr
= false;
1330 u16 desired_len
= 0;
1333 unsigned int iedatalen
;
1340 if (iedatalen
+ 2 > len
)
1343 if (ies
[0] != WLAN_EID_VENDOR_SPECIFIC
)
1351 /* check WFA OUI, P2P subtype */
1352 if (iedata
[0] != 0x50 || iedata
[1] != 0x6f ||
1353 iedata
[2] != 0x9a || iedata
[3] != 0x09)
1359 /* check attribute continuation into this IE */
1360 copy
= min_t(unsigned int, attr_remaining
, iedatalen
);
1361 if (copy
&& desired_attr
) {
1362 desired_len
+= copy
;
1364 memcpy(out
, iedata
, min(bufsize
, copy
));
1365 out
+= min(bufsize
, copy
);
1366 bufsize
-= min(bufsize
, copy
);
1370 if (copy
== attr_remaining
)
1374 attr_remaining
-= copy
;
1381 while (iedatalen
> 0) {
1384 /* P2P attribute ID & size must fit */
1387 desired_attr
= iedata
[0] == attr
;
1388 attr_len
= get_unaligned_le16(iedata
+ 1);
1392 copy
= min_t(unsigned int, attr_len
, iedatalen
);
1395 desired_len
+= copy
;
1397 memcpy(out
, iedata
, min(bufsize
, copy
));
1398 out
+= min(bufsize
, copy
);
1399 bufsize
-= min(bufsize
, copy
);
1402 if (copy
== attr_len
)
1408 attr_remaining
= attr_len
- copy
;
1416 if (attr_remaining
&& desired_attr
)
1421 EXPORT_SYMBOL(cfg80211_get_p2p_attr
);
1423 static bool ieee80211_id_in_list(const u8
*ids
, int n_ids
, u8 id
, bool id_ext
)
1427 /* Make sure array values are legal */
1428 if (WARN_ON(ids
[n_ids
- 1] == WLAN_EID_EXTENSION
))
1433 if (ids
[i
] == WLAN_EID_EXTENSION
) {
1434 if (id_ext
&& (ids
[i
+ 1] == id
))
1441 if (ids
[i
] == id
&& !id_ext
)
1449 static size_t skip_ie(const u8
*ies
, size_t ielen
, size_t pos
)
1451 /* we assume a validly formed IEs buffer */
1452 u8 len
= ies
[pos
+ 1];
1456 /* the IE itself must have 255 bytes for fragments to follow */
1460 while (pos
< ielen
&& ies
[pos
] == WLAN_EID_FRAGMENT
) {
1468 size_t ieee80211_ie_split_ric(const u8
*ies
, size_t ielen
,
1469 const u8
*ids
, int n_ids
,
1470 const u8
*after_ric
, int n_after_ric
,
1473 size_t pos
= offset
;
1475 while (pos
< ielen
) {
1478 if (ies
[pos
] == WLAN_EID_EXTENSION
)
1480 if ((pos
+ ext
) >= ielen
)
1483 if (!ieee80211_id_in_list(ids
, n_ids
, ies
[pos
+ ext
],
1484 ies
[pos
] == WLAN_EID_EXTENSION
))
1487 if (ies
[pos
] == WLAN_EID_RIC_DATA
&& n_after_ric
) {
1488 pos
= skip_ie(ies
, ielen
, pos
);
1490 while (pos
< ielen
) {
1491 if (ies
[pos
] == WLAN_EID_EXTENSION
)
1496 if ((pos
+ ext
) >= ielen
)
1499 if (!ieee80211_id_in_list(after_ric
,
1503 pos
= skip_ie(ies
, ielen
, pos
);
1508 pos
= skip_ie(ies
, ielen
, pos
);
1514 EXPORT_SYMBOL(ieee80211_ie_split_ric
);
1516 bool ieee80211_operating_class_to_band(u8 operating_class
,
1517 enum nl80211_band
*band
)
1519 switch (operating_class
) {
1523 *band
= NL80211_BAND_5GHZ
;
1526 *band
= NL80211_BAND_6GHZ
;
1532 *band
= NL80211_BAND_2GHZ
;
1535 *band
= NL80211_BAND_60GHZ
;
1541 EXPORT_SYMBOL(ieee80211_operating_class_to_band
);
1543 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def
*chandef
,
1547 u32 freq
= chandef
->center_freq1
;
1549 if (freq
>= 2412 && freq
<= 2472) {
1550 if (chandef
->width
> NL80211_CHAN_WIDTH_40
)
1553 /* 2.407 GHz, channels 1..13 */
1554 if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1555 if (freq
> chandef
->chan
->center_freq
)
1556 *op_class
= 83; /* HT40+ */
1558 *op_class
= 84; /* HT40- */
1567 /* channel 14 is only for IEEE 802.11b */
1568 if (chandef
->width
!= NL80211_CHAN_WIDTH_20_NOHT
)
1571 *op_class
= 82; /* channel 14 */
1575 switch (chandef
->width
) {
1576 case NL80211_CHAN_WIDTH_80
:
1579 case NL80211_CHAN_WIDTH_160
:
1582 case NL80211_CHAN_WIDTH_80P80
:
1585 case NL80211_CHAN_WIDTH_10
:
1586 case NL80211_CHAN_WIDTH_5
:
1587 return false; /* unsupported for now */
1593 /* 5 GHz, channels 36..48 */
1594 if (freq
>= 5180 && freq
<= 5240) {
1596 *op_class
= vht_opclass
;
1597 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1598 if (freq
> chandef
->chan
->center_freq
)
1609 /* 5 GHz, channels 52..64 */
1610 if (freq
>= 5260 && freq
<= 5320) {
1612 *op_class
= vht_opclass
;
1613 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1614 if (freq
> chandef
->chan
->center_freq
)
1625 /* 5 GHz, channels 100..144 */
1626 if (freq
>= 5500 && freq
<= 5720) {
1628 *op_class
= vht_opclass
;
1629 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1630 if (freq
> chandef
->chan
->center_freq
)
1641 /* 5 GHz, channels 149..169 */
1642 if (freq
>= 5745 && freq
<= 5845) {
1644 *op_class
= vht_opclass
;
1645 } else if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
1646 if (freq
> chandef
->chan
->center_freq
)
1650 } else if (freq
<= 5805) {
1659 /* 56.16 GHz, channel 1..4 */
1660 if (freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6) {
1661 if (chandef
->width
>= NL80211_CHAN_WIDTH_40
)
1668 /* not supported yet */
1671 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class
);
1673 static void cfg80211_calculate_bi_data(struct wiphy
*wiphy
, u32 new_beacon_int
,
1674 u32
*beacon_int_gcd
,
1675 bool *beacon_int_different
)
1677 struct wireless_dev
*wdev
;
1679 *beacon_int_gcd
= 0;
1680 *beacon_int_different
= false;
1682 list_for_each_entry(wdev
, &wiphy
->wdev_list
, list
) {
1683 if (!wdev
->beacon_interval
)
1686 if (!*beacon_int_gcd
) {
1687 *beacon_int_gcd
= wdev
->beacon_interval
;
1691 if (wdev
->beacon_interval
== *beacon_int_gcd
)
1694 *beacon_int_different
= true;
1695 *beacon_int_gcd
= gcd(*beacon_int_gcd
, wdev
->beacon_interval
);
1698 if (new_beacon_int
&& *beacon_int_gcd
!= new_beacon_int
) {
1699 if (*beacon_int_gcd
)
1700 *beacon_int_different
= true;
1701 *beacon_int_gcd
= gcd(*beacon_int_gcd
, new_beacon_int
);
1705 int cfg80211_validate_beacon_int(struct cfg80211_registered_device
*rdev
,
1706 enum nl80211_iftype iftype
, u32 beacon_int
)
1709 * This is just a basic pre-condition check; if interface combinations
1710 * are possible the driver must already be checking those with a call
1711 * to cfg80211_check_combinations(), in which case we'll validate more
1712 * through the cfg80211_calculate_bi_data() call and code in
1713 * cfg80211_iter_combinations().
1716 if (beacon_int
< 10 || beacon_int
> 10000)
1722 int cfg80211_iter_combinations(struct wiphy
*wiphy
,
1723 struct iface_combination_params
*params
,
1724 void (*iter
)(const struct ieee80211_iface_combination
*c
,
1728 const struct ieee80211_regdomain
*regdom
;
1729 enum nl80211_dfs_regions region
= 0;
1731 int num_interfaces
= 0;
1732 u32 used_iftypes
= 0;
1734 bool beacon_int_different
;
1737 * This is a bit strange, since the iteration used to rely only on
1738 * the data given by the driver, but here it now relies on context,
1739 * in form of the currently operating interfaces.
1740 * This is OK for all current users, and saves us from having to
1741 * push the GCD calculations into all the drivers.
1742 * In the future, this should probably rely more on data that's in
1743 * cfg80211 already - the only thing not would appear to be any new
1744 * interfaces (while being brought up) and channel/radar data.
1746 cfg80211_calculate_bi_data(wiphy
, params
->new_beacon_int
,
1747 &beacon_int_gcd
, &beacon_int_different
);
1749 if (params
->radar_detect
) {
1751 regdom
= rcu_dereference(cfg80211_regdomain
);
1753 region
= regdom
->dfs_region
;
1757 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1758 num_interfaces
+= params
->iftype_num
[iftype
];
1759 if (params
->iftype_num
[iftype
] > 0 &&
1760 !cfg80211_iftype_allowed(wiphy
, iftype
, 0, 1))
1761 used_iftypes
|= BIT(iftype
);
1764 for (i
= 0; i
< wiphy
->n_iface_combinations
; i
++) {
1765 const struct ieee80211_iface_combination
*c
;
1766 struct ieee80211_iface_limit
*limits
;
1767 u32 all_iftypes
= 0;
1769 c
= &wiphy
->iface_combinations
[i
];
1771 if (num_interfaces
> c
->max_interfaces
)
1773 if (params
->num_different_channels
> c
->num_different_channels
)
1776 limits
= kmemdup(c
->limits
, sizeof(limits
[0]) * c
->n_limits
,
1781 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
1782 if (cfg80211_iftype_allowed(wiphy
, iftype
, 0, 1))
1784 for (j
= 0; j
< c
->n_limits
; j
++) {
1785 all_iftypes
|= limits
[j
].types
;
1786 if (!(limits
[j
].types
& BIT(iftype
)))
1788 if (limits
[j
].max
< params
->iftype_num
[iftype
])
1790 limits
[j
].max
-= params
->iftype_num
[iftype
];
1794 if (params
->radar_detect
!=
1795 (c
->radar_detect_widths
& params
->radar_detect
))
1798 if (params
->radar_detect
&& c
->radar_detect_regions
&&
1799 !(c
->radar_detect_regions
& BIT(region
)))
1802 /* Finally check that all iftypes that we're currently
1803 * using are actually part of this combination. If they
1804 * aren't then we can't use this combination and have
1805 * to continue to the next.
1807 if ((all_iftypes
& used_iftypes
) != used_iftypes
)
1810 if (beacon_int_gcd
) {
1811 if (c
->beacon_int_min_gcd
&&
1812 beacon_int_gcd
< c
->beacon_int_min_gcd
)
1814 if (!c
->beacon_int_min_gcd
&& beacon_int_different
)
1818 /* This combination covered all interface types and
1819 * supported the requested numbers, so we're good.
1829 EXPORT_SYMBOL(cfg80211_iter_combinations
);
1832 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination
*c
,
1839 int cfg80211_check_combinations(struct wiphy
*wiphy
,
1840 struct iface_combination_params
*params
)
1844 err
= cfg80211_iter_combinations(wiphy
, params
,
1845 cfg80211_iter_sum_ifcombs
, &num
);
1853 EXPORT_SYMBOL(cfg80211_check_combinations
);
1855 int ieee80211_get_ratemask(struct ieee80211_supported_band
*sband
,
1856 const u8
*rates
, unsigned int n_rates
,
1864 if (n_rates
== 0 || n_rates
> NL80211_MAX_SUPP_RATES
)
1869 for (i
= 0; i
< n_rates
; i
++) {
1870 int rate
= (rates
[i
] & 0x7f) * 5;
1873 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
1874 if (sband
->bitrates
[j
].bitrate
== rate
) {
1885 * mask must have at least one bit set here since we
1886 * didn't accept a 0-length rates array nor allowed
1887 * entries in the array that didn't exist
1893 unsigned int ieee80211_get_num_supported_channels(struct wiphy
*wiphy
)
1895 enum nl80211_band band
;
1896 unsigned int n_channels
= 0;
1898 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++)
1899 if (wiphy
->bands
[band
])
1900 n_channels
+= wiphy
->bands
[band
]->n_channels
;
1904 EXPORT_SYMBOL(ieee80211_get_num_supported_channels
);
1906 int cfg80211_get_station(struct net_device
*dev
, const u8
*mac_addr
,
1907 struct station_info
*sinfo
)
1909 struct cfg80211_registered_device
*rdev
;
1910 struct wireless_dev
*wdev
;
1912 wdev
= dev
->ieee80211_ptr
;
1916 rdev
= wiphy_to_rdev(wdev
->wiphy
);
1917 if (!rdev
->ops
->get_station
)
1920 memset(sinfo
, 0, sizeof(*sinfo
));
1922 return rdev_get_station(rdev
, dev
, mac_addr
, sinfo
);
1924 EXPORT_SYMBOL(cfg80211_get_station
);
1926 void cfg80211_free_nan_func(struct cfg80211_nan_func
*f
)
1933 kfree(f
->serv_spec_info
);
1936 for (i
= 0; i
< f
->num_rx_filters
; i
++)
1937 kfree(f
->rx_filters
[i
].filter
);
1939 for (i
= 0; i
< f
->num_tx_filters
; i
++)
1940 kfree(f
->tx_filters
[i
].filter
);
1942 kfree(f
->rx_filters
);
1943 kfree(f
->tx_filters
);
1946 EXPORT_SYMBOL(cfg80211_free_nan_func
);
1948 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range
*freq_range
,
1949 u32 center_freq_khz
, u32 bw_khz
)
1951 u32 start_freq_khz
, end_freq_khz
;
1953 start_freq_khz
= center_freq_khz
- (bw_khz
/ 2);
1954 end_freq_khz
= center_freq_khz
+ (bw_khz
/ 2);
1956 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
1957 end_freq_khz
<= freq_range
->end_freq_khz
)
1963 int cfg80211_sinfo_alloc_tid_stats(struct station_info
*sinfo
, gfp_t gfp
)
1965 sinfo
->pertid
= kcalloc(IEEE80211_NUM_TIDS
+ 1,
1966 sizeof(*(sinfo
->pertid
)),
1973 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats
);
1975 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1976 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1977 const unsigned char rfc1042_header
[] __aligned(2) =
1978 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1979 EXPORT_SYMBOL(rfc1042_header
);
1981 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1982 const unsigned char bridge_tunnel_header
[] __aligned(2) =
1983 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1984 EXPORT_SYMBOL(bridge_tunnel_header
);
1986 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1987 struct iapp_layer2_update
{
1988 u8 da
[ETH_ALEN
]; /* broadcast */
1989 u8 sa
[ETH_ALEN
]; /* STA addr */
1997 void cfg80211_send_layer2_update(struct net_device
*dev
, const u8
*addr
)
1999 struct iapp_layer2_update
*msg
;
2000 struct sk_buff
*skb
;
2002 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2005 skb
= dev_alloc_skb(sizeof(*msg
));
2008 msg
= skb_put(skb
, sizeof(*msg
));
2010 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2011 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2013 eth_broadcast_addr(msg
->da
);
2014 ether_addr_copy(msg
->sa
, addr
);
2015 msg
->len
= htons(6);
2017 msg
->ssap
= 0x01; /* NULL LSAP, CR Bit: Response */
2018 msg
->control
= 0xaf; /* XID response lsb.1111F101.
2019 * F=0 (no poll command; unsolicited frame) */
2020 msg
->xid_info
[0] = 0x81; /* XID format identifier */
2021 msg
->xid_info
[1] = 1; /* LLC types/classes: Type 1 LLC */
2022 msg
->xid_info
[2] = 0; /* XID sender's receive window size (RW) */
2025 skb
->protocol
= eth_type_trans(skb
, dev
);
2026 memset(skb
->cb
, 0, sizeof(skb
->cb
));
2029 EXPORT_SYMBOL(cfg80211_send_layer2_update
);
2031 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap
*cap
,
2032 enum ieee80211_vht_chanwidth bw
,
2033 int mcs
, bool ext_nss_bw_capable
)
2035 u16 map
= le16_to_cpu(cap
->supp_mcs
.rx_mcs_map
);
2036 int max_vht_nss
= 0;
2039 int i
, mcs_encoding
;
2044 if (WARN_ON(mcs
> 9))
2053 /* find max_vht_nss for the given MCS */
2054 for (i
= 7; i
>= 0; i
--) {
2055 int supp
= (map
>> (2 * i
)) & 3;
2060 if (supp
>= mcs_encoding
) {
2061 max_vht_nss
= i
+ 1;
2066 if (!(cap
->supp_mcs
.tx_mcs_map
&
2067 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE
)))
2070 ext_nss_bw
= le32_get_bits(cap
->vht_cap_info
,
2071 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK
);
2072 supp_width
= le32_get_bits(cap
->vht_cap_info
,
2073 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK
);
2075 /* if not capable, treat ext_nss_bw as 0 */
2076 if (!ext_nss_bw_capable
)
2079 /* This is invalid */
2080 if (supp_width
== 3)
2083 /* This is an invalid combination so pretend nothing is supported */
2084 if (supp_width
== 2 && (ext_nss_bw
== 1 || ext_nss_bw
== 2))
2088 * Cover all the special cases according to IEEE 802.11-2016
2089 * Table 9-250. All other cases are either factor of 1 or not
2093 case IEEE80211_VHT_CHANWIDTH_USE_HT
:
2094 case IEEE80211_VHT_CHANWIDTH_80MHZ
:
2095 if ((supp_width
== 1 || supp_width
== 2) &&
2097 return 2 * max_vht_nss
;
2099 case IEEE80211_VHT_CHANWIDTH_160MHZ
:
2100 if (supp_width
== 0 &&
2101 (ext_nss_bw
== 1 || ext_nss_bw
== 2))
2102 return max_vht_nss
/ 2;
2103 if (supp_width
== 0 &&
2105 return (3 * max_vht_nss
) / 4;
2106 if (supp_width
== 1 &&
2108 return 2 * max_vht_nss
;
2110 case IEEE80211_VHT_CHANWIDTH_80P80MHZ
:
2111 if (supp_width
== 0 && ext_nss_bw
== 1)
2112 return 0; /* not possible */
2113 if (supp_width
== 0 &&
2115 return max_vht_nss
/ 2;
2116 if (supp_width
== 0 &&
2118 return (3 * max_vht_nss
) / 4;
2119 if (supp_width
== 1 &&
2121 return 0; /* not possible */
2122 if (supp_width
== 1 &&
2124 return max_vht_nss
/ 2;
2125 if (supp_width
== 1 &&
2127 return (3 * max_vht_nss
) / 4;
2131 /* not covered or invalid combination received */
2134 EXPORT_SYMBOL(ieee80211_get_vht_max_nss
);
2136 bool cfg80211_iftype_allowed(struct wiphy
*wiphy
, enum nl80211_iftype iftype
,
2137 bool is_4addr
, u8 check_swif
)
2140 bool is_vlan
= iftype
== NL80211_IFTYPE_AP_VLAN
;
2142 switch (check_swif
) {
2144 if (is_vlan
&& is_4addr
)
2145 return wiphy
->flags
& WIPHY_FLAG_4ADDR_AP
;
2146 return wiphy
->interface_modes
& BIT(iftype
);
2148 if (!(wiphy
->software_iftypes
& BIT(iftype
)) && is_vlan
)
2149 return wiphy
->flags
& WIPHY_FLAG_4ADDR_AP
;
2150 return wiphy
->software_iftypes
& BIT(iftype
);
2157 EXPORT_SYMBOL(cfg80211_iftype_allowed
);