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-2023 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 const 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)
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 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
59 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
61 bitrates = sband->bitrates;
62 for (i = 0; i < sband->n_bitrates; i++)
63 if (bitrates[i].flags & mandatory_flag)
64 mandatory_rates |= BIT(i);
65 return mandatory_rates;
67 EXPORT_SYMBOL(ieee80211_mandatory_rates);
69 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
71 /* see 802.11 17.3.8.3.2 and Annex J
72 * there are overlapping channel numbers in 5GHz and 2GHz bands */
74 return 0; /* not supported */
76 case NL80211_BAND_2GHZ:
79 return MHZ_TO_KHZ(2484);
81 return MHZ_TO_KHZ(2407 + chan * 5);
83 case NL80211_BAND_5GHZ:
84 if (chan >= 182 && chan <= 196)
85 return MHZ_TO_KHZ(4000 + chan * 5);
87 return MHZ_TO_KHZ(5000 + chan * 5);
89 case NL80211_BAND_6GHZ:
90 /* see 802.11ax D6.1 27.3.23.2 */
92 return MHZ_TO_KHZ(5935);
94 return MHZ_TO_KHZ(5950 + chan * 5);
96 case NL80211_BAND_60GHZ:
98 return MHZ_TO_KHZ(56160 + chan * 2160);
100 case NL80211_BAND_S1GHZ:
101 return 902000 + chan * 500;
105 return 0; /* not supported */
107 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
109 enum nl80211_chan_width
110 ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
112 if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
113 return NL80211_CHAN_WIDTH_20_NOHT;
115 /*S1G defines a single allowed channel width per channel.
116 * Extract that width here.
118 if (chan->flags & IEEE80211_CHAN_1MHZ)
119 return NL80211_CHAN_WIDTH_1;
120 else if (chan->flags & IEEE80211_CHAN_2MHZ)
121 return NL80211_CHAN_WIDTH_2;
122 else if (chan->flags & IEEE80211_CHAN_4MHZ)
123 return NL80211_CHAN_WIDTH_4;
124 else if (chan->flags & IEEE80211_CHAN_8MHZ)
125 return NL80211_CHAN_WIDTH_8;
126 else if (chan->flags & IEEE80211_CHAN_16MHZ)
127 return NL80211_CHAN_WIDTH_16;
129 pr_err("unknown channel width for channel at %dKHz?\n",
130 ieee80211_channel_to_khz(chan));
132 return NL80211_CHAN_WIDTH_1;
134 EXPORT_SYMBOL(ieee80211_s1g_channel_width);
136 int ieee80211_freq_khz_to_channel(u32 freq)
138 /* TODO: just handle MHz for now */
139 freq = KHZ_TO_MHZ(freq);
141 /* see 802.11 17.3.8.3.2 and Annex J */
144 else if (freq < 2484)
145 return (freq - 2407) / 5;
146 else if (freq >= 4910 && freq <= 4980)
147 return (freq - 4000) / 5;
148 else if (freq < 5925)
149 return (freq - 5000) / 5;
150 else if (freq == 5935)
152 else if (freq <= 45000) /* DMG band lower limit */
153 /* see 802.11ax D6.1 27.3.22.2 */
154 return (freq - 5950) / 5;
155 else if (freq >= 58320 && freq <= 70200)
156 return (freq - 56160) / 2160;
160 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
162 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
165 enum nl80211_band band;
166 struct ieee80211_supported_band *sband;
169 for (band = 0; band < NUM_NL80211_BANDS; band++) {
170 sband = wiphy->bands[band];
175 for (i = 0; i < sband->n_channels; i++) {
176 struct ieee80211_channel *chan = &sband->channels[i];
178 if (ieee80211_channel_to_khz(chan) == freq)
185 EXPORT_SYMBOL(ieee80211_get_channel_khz);
187 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
191 switch (sband->band) {
192 case NL80211_BAND_5GHZ:
193 case NL80211_BAND_6GHZ:
195 for (i = 0; i < sband->n_bitrates; i++) {
196 if (sband->bitrates[i].bitrate == 60 ||
197 sband->bitrates[i].bitrate == 120 ||
198 sband->bitrates[i].bitrate == 240) {
199 sband->bitrates[i].flags |=
200 IEEE80211_RATE_MANDATORY_A;
206 case NL80211_BAND_2GHZ:
207 case NL80211_BAND_LC:
209 for (i = 0; i < sband->n_bitrates; i++) {
210 switch (sband->bitrates[i].bitrate) {
215 sband->bitrates[i].flags |=
216 IEEE80211_RATE_MANDATORY_B |
217 IEEE80211_RATE_MANDATORY_G;
223 sband->bitrates[i].flags |=
224 IEEE80211_RATE_MANDATORY_G;
228 sband->bitrates[i].flags |=
229 IEEE80211_RATE_ERP_G;
233 WARN_ON(want != 0 && want != 3);
235 case NL80211_BAND_60GHZ:
236 /* check for mandatory HT MCS 1..4 */
237 WARN_ON(!sband->ht_cap.ht_supported);
238 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
240 case NL80211_BAND_S1GHZ:
241 /* Figure 9-589bd: 3 means unsupported, so != 3 means at least
244 WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
246 case NUM_NL80211_BANDS:
253 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
255 enum nl80211_band band;
257 for (band = 0; band < NUM_NL80211_BANDS; band++)
258 if (wiphy->bands[band])
259 set_mandatory_flags_band(wiphy->bands[band]);
262 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
265 for (i = 0; i < wiphy->n_cipher_suites; i++)
266 if (cipher == wiphy->cipher_suites[i])
272 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
274 struct wiphy *wiphy = &rdev->wiphy;
277 for (i = 0; i < wiphy->n_cipher_suites; i++) {
278 switch (wiphy->cipher_suites[i]) {
279 case WLAN_CIPHER_SUITE_AES_CMAC:
280 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
281 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
282 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
290 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
291 int key_idx, bool pairwise)
297 else if (wiphy_ext_feature_isset(&rdev->wiphy,
298 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
299 wiphy_ext_feature_isset(&rdev->wiphy,
300 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
302 else if (cfg80211_igtk_cipher_supported(rdev))
307 if (key_idx < 0 || key_idx > max_key_idx)
313 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
314 struct key_params *params, int key_idx,
315 bool pairwise, const u8 *mac_addr)
317 if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
320 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
323 if (pairwise && !mac_addr)
326 switch (params->cipher) {
327 case WLAN_CIPHER_SUITE_TKIP:
328 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
329 if ((pairwise && key_idx) ||
330 params->mode != NL80211_KEY_RX_TX)
333 case WLAN_CIPHER_SUITE_CCMP:
334 case WLAN_CIPHER_SUITE_CCMP_256:
335 case WLAN_CIPHER_SUITE_GCMP:
336 case WLAN_CIPHER_SUITE_GCMP_256:
337 /* IEEE802.11-2016 allows only 0 and - when supporting
338 * Extended Key ID - 1 as index for pairwise keys.
339 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
340 * the driver supports Extended Key ID.
341 * @NL80211_KEY_SET_TX can't be set when installing and
344 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
345 params->mode == NL80211_KEY_SET_TX)
347 if (wiphy_ext_feature_isset(&rdev->wiphy,
348 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
349 if (pairwise && (key_idx < 0 || key_idx > 1))
351 } else if (pairwise && key_idx) {
355 case WLAN_CIPHER_SUITE_AES_CMAC:
356 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
357 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
358 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
359 /* Disallow BIP (group-only) cipher as pairwise cipher */
365 case WLAN_CIPHER_SUITE_WEP40:
366 case WLAN_CIPHER_SUITE_WEP104:
374 switch (params->cipher) {
375 case WLAN_CIPHER_SUITE_WEP40:
376 if (params->key_len != WLAN_KEY_LEN_WEP40)
379 case WLAN_CIPHER_SUITE_TKIP:
380 if (params->key_len != WLAN_KEY_LEN_TKIP)
383 case WLAN_CIPHER_SUITE_CCMP:
384 if (params->key_len != WLAN_KEY_LEN_CCMP)
387 case WLAN_CIPHER_SUITE_CCMP_256:
388 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
391 case WLAN_CIPHER_SUITE_GCMP:
392 if (params->key_len != WLAN_KEY_LEN_GCMP)
395 case WLAN_CIPHER_SUITE_GCMP_256:
396 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
399 case WLAN_CIPHER_SUITE_WEP104:
400 if (params->key_len != WLAN_KEY_LEN_WEP104)
403 case WLAN_CIPHER_SUITE_AES_CMAC:
404 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
407 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
408 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
411 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
412 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
415 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
416 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
421 * We don't know anything about this algorithm,
422 * allow using it -- but the driver must check
423 * all parameters! We still check below whether
424 * or not the driver supports this algorithm,
431 switch (params->cipher) {
432 case WLAN_CIPHER_SUITE_WEP40:
433 case WLAN_CIPHER_SUITE_WEP104:
434 /* These ciphers do not use key sequence */
436 case WLAN_CIPHER_SUITE_TKIP:
437 case WLAN_CIPHER_SUITE_CCMP:
438 case WLAN_CIPHER_SUITE_CCMP_256:
439 case WLAN_CIPHER_SUITE_GCMP:
440 case WLAN_CIPHER_SUITE_GCMP_256:
441 case WLAN_CIPHER_SUITE_AES_CMAC:
442 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
443 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
444 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
445 if (params->seq_len != 6)
451 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
457 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
459 unsigned int hdrlen = 24;
461 if (ieee80211_is_ext(fc)) {
466 if (ieee80211_is_data(fc)) {
467 if (ieee80211_has_a4(fc))
469 if (ieee80211_is_data_qos(fc)) {
470 hdrlen += IEEE80211_QOS_CTL_LEN;
471 if (ieee80211_has_order(fc))
472 hdrlen += IEEE80211_HT_CTL_LEN;
477 if (ieee80211_is_mgmt(fc)) {
478 if (ieee80211_has_order(fc))
479 hdrlen += IEEE80211_HT_CTL_LEN;
483 if (ieee80211_is_ctl(fc)) {
485 * ACK and CTS are 10 bytes, all others 16. To see how
486 * to get this condition consider
487 * subtype mask: 0b0000000011110000 (0x00F0)
488 * ACK subtype: 0b0000000011010000 (0x00D0)
489 * CTS subtype: 0b0000000011000000 (0x00C0)
490 * bits that matter: ^^^ (0x00E0)
491 * value of those: 0b0000000011000000 (0x00C0)
493 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
501 EXPORT_SYMBOL(ieee80211_hdrlen);
503 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
505 const struct ieee80211_hdr *hdr =
506 (const struct ieee80211_hdr *)skb->data;
509 if (unlikely(skb->len < 10))
511 hdrlen = ieee80211_hdrlen(hdr->frame_control);
512 if (unlikely(hdrlen > skb->len))
516 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
518 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
520 int ae = flags & MESH_FLAGS_AE;
521 /* 802.11-2012, 8.2.4.7.3 */
526 case MESH_FLAGS_AE_A4:
528 case MESH_FLAGS_AE_A5_A6:
533 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
535 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
537 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
539 bool ieee80211_get_8023_tunnel_proto(const void *hdr, __be16 *proto)
541 const __be16 *hdr_proto = hdr + ETH_ALEN;
543 if (!(ether_addr_equal(hdr, rfc1042_header) &&
544 *hdr_proto != htons(ETH_P_AARP) &&
545 *hdr_proto != htons(ETH_P_IPX)) &&
546 !ether_addr_equal(hdr, bridge_tunnel_header))
553 EXPORT_SYMBOL(ieee80211_get_8023_tunnel_proto);
555 int ieee80211_strip_8023_mesh_hdr(struct sk_buff *skb)
557 const void *mesh_addr;
565 ret = skb_copy_bits(skb, 0, &payload, sizeof(payload));
569 hdrlen = sizeof(payload.eth) + __ieee80211_get_mesh_hdrlen(payload.flags);
571 if (likely(pskb_may_pull(skb, hdrlen + 8) &&
572 ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
573 &payload.eth.h_proto)))
574 hdrlen += ETH_ALEN + 2;
575 else if (!pskb_may_pull(skb, hdrlen))
578 payload.eth.h_proto = htons(skb->len - hdrlen);
580 mesh_addr = skb->data + sizeof(payload.eth) + ETH_ALEN;
581 switch (payload.flags & MESH_FLAGS_AE) {
582 case MESH_FLAGS_AE_A4:
583 memcpy(&payload.eth.h_source, mesh_addr, ETH_ALEN);
585 case MESH_FLAGS_AE_A5_A6:
586 memcpy(&payload.eth, mesh_addr, 2 * ETH_ALEN);
592 pskb_pull(skb, hdrlen - sizeof(payload.eth));
593 memcpy(skb->data, &payload.eth, sizeof(payload.eth));
597 EXPORT_SYMBOL(ieee80211_strip_8023_mesh_hdr);
599 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
600 const u8 *addr, enum nl80211_iftype iftype,
601 u8 data_offset, bool is_amsdu)
603 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
605 u8 hdr[ETH_ALEN] __aligned(2);
611 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
614 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
615 if (skb->len < hdrlen)
618 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
620 * IEEE 802.11 address fields:
621 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
622 * 0 0 DA SA BSSID n/a
623 * 0 1 DA BSSID SA n/a
624 * 1 0 BSSID SA DA n/a
627 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
628 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
630 switch (hdr->frame_control &
631 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
632 case cpu_to_le16(IEEE80211_FCTL_TODS):
633 if (unlikely(iftype != NL80211_IFTYPE_AP &&
634 iftype != NL80211_IFTYPE_AP_VLAN &&
635 iftype != NL80211_IFTYPE_P2P_GO))
638 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
639 if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
640 iftype != NL80211_IFTYPE_AP_VLAN &&
641 iftype != NL80211_IFTYPE_STATION))
644 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
645 if ((iftype != NL80211_IFTYPE_STATION &&
646 iftype != NL80211_IFTYPE_P2P_CLIENT &&
647 iftype != NL80211_IFTYPE_MESH_POINT) ||
648 (is_multicast_ether_addr(tmp.h_dest) &&
649 ether_addr_equal(tmp.h_source, addr)))
653 if (iftype != NL80211_IFTYPE_ADHOC &&
654 iftype != NL80211_IFTYPE_STATION &&
655 iftype != NL80211_IFTYPE_OCB)
660 if (likely(!is_amsdu && iftype != NL80211_IFTYPE_MESH_POINT &&
661 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 &&
662 ieee80211_get_8023_tunnel_proto(&payload, &tmp.h_proto))) {
663 /* remove RFC1042 or Bridge-Tunnel encapsulation */
664 hdrlen += ETH_ALEN + 2;
665 skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
667 tmp.h_proto = htons(skb->len - hdrlen);
670 pskb_pull(skb, hdrlen);
673 ehdr = skb_push(skb, sizeof(struct ethhdr));
674 memcpy(ehdr, &tmp, sizeof(tmp));
678 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
681 __frame_add_frag(struct sk_buff *skb, struct page *page,
682 void *ptr, int len, int size)
684 struct skb_shared_info *sh = skb_shinfo(skb);
688 page_offset = ptr - page_address(page);
689 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
693 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
696 struct skb_shared_info *sh = skb_shinfo(skb);
697 const skb_frag_t *frag = &sh->frags[0];
698 struct page *frag_page;
700 int frag_len, frag_size;
701 int head_size = skb->len - skb->data_len;
704 frag_page = virt_to_head_page(skb->head);
705 frag_ptr = skb->data;
706 frag_size = head_size;
708 while (offset >= frag_size) {
710 frag_page = skb_frag_page(frag);
711 frag_ptr = skb_frag_address(frag);
712 frag_size = skb_frag_size(frag);
717 frag_len = frag_size - offset;
719 cur_len = min(len, frag_len);
721 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
725 frag_len = skb_frag_size(frag);
726 cur_len = min(len, frag_len);
727 __frame_add_frag(frame, skb_frag_page(frag),
728 skb_frag_address(frag), cur_len, frag_len);
734 static struct sk_buff *
735 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
736 int offset, int len, bool reuse_frag,
739 struct sk_buff *frame;
742 if (skb->len - offset < len)
746 * When reusing framents, copy some data to the head to simplify
747 * ethernet header handling and speed up protocol header processing
748 * in the stack later.
751 cur_len = min_t(int, len, min_len);
754 * Allocate and reserve two bytes more for payload
755 * alignment since sizeof(struct ethhdr) is 14.
757 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
761 frame->priority = skb->priority;
762 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
763 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
770 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
776 ieee80211_amsdu_subframe_length(void *field, u8 mesh_flags, u8 hdr_type)
778 __le16 *field_le = field;
779 __be16 *field_be = field;
783 len = le16_to_cpu(*field_le);
785 len = be16_to_cpu(*field_be);
787 len += __ieee80211_get_mesh_hdrlen(mesh_flags);
792 bool ieee80211_is_valid_amsdu(struct sk_buff *skb, u8 mesh_hdr)
794 int offset = 0, subframe_len, padding;
796 for (offset = 0; offset < skb->len; offset += subframe_len + padding) {
797 int remaining = skb->len - offset;
804 if (sizeof(hdr) > remaining)
807 if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0)
810 len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags,
812 subframe_len = sizeof(struct ethhdr) + len;
813 padding = (4 - subframe_len) & 0x3;
815 if (subframe_len > remaining)
821 EXPORT_SYMBOL(ieee80211_is_valid_amsdu);
823 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
824 const u8 *addr, enum nl80211_iftype iftype,
825 const unsigned int extra_headroom,
826 const u8 *check_da, const u8 *check_sa,
829 unsigned int hlen = ALIGN(extra_headroom, 4);
830 struct sk_buff *frame = NULL;
836 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
837 bool reuse_skb = false;
839 int copy_len = sizeof(hdr.eth);
841 if (iftype == NL80211_IFTYPE_MESH_POINT)
842 copy_len = sizeof(hdr);
845 int remaining = skb->len - offset;
846 unsigned int subframe_len;
847 int len, mesh_len = 0;
850 if (copy_len > remaining)
853 skb_copy_bits(skb, offset, &hdr, copy_len);
854 if (iftype == NL80211_IFTYPE_MESH_POINT)
855 mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags);
856 len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags,
858 subframe_len = sizeof(struct ethhdr) + len;
859 padding = (4 - subframe_len) & 0x3;
861 /* the last MSDU has no padding */
862 if (subframe_len > remaining)
864 /* mitigate A-MSDU aggregation injection attacks */
865 if (ether_addr_equal(hdr.eth.h_dest, rfc1042_header))
868 offset += sizeof(struct ethhdr);
869 last = remaining <= subframe_len + padding;
871 /* FIXME: should we really accept multicast DA? */
872 if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) &&
873 !ether_addr_equal(check_da, hdr.eth.h_dest)) ||
874 (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) {
875 offset += len + padding;
879 /* reuse skb for the last subframe */
880 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
881 skb_pull(skb, offset);
885 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
886 reuse_frag, 32 + mesh_len);
890 offset += len + padding;
893 skb_reset_network_header(frame);
894 frame->dev = skb->dev;
895 frame->priority = skb->priority;
897 if (likely(iftype != NL80211_IFTYPE_MESH_POINT &&
898 ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto)))
899 skb_pull(frame, ETH_ALEN + 2);
901 memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth));
902 __skb_queue_tail(list, frame);
911 __skb_queue_purge(list);
914 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
916 /* Given a data frame determine the 802.1p/1d tag to use. */
917 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
918 struct cfg80211_qos_map *qos_map)
921 unsigned char vlan_priority;
924 /* skb->priority values from 256->263 are magic values to
925 * directly indicate a specific 802.1d priority. This is used
926 * to allow 802.1d priority to be passed directly in from VLAN
929 if (skb->priority >= 256 && skb->priority <= 263) {
930 ret = skb->priority - 256;
934 if (skb_vlan_tag_present(skb)) {
935 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
937 if (vlan_priority > 0) {
943 switch (skb->protocol) {
944 case htons(ETH_P_IP):
945 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
947 case htons(ETH_P_IPV6):
948 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
950 case htons(ETH_P_MPLS_UC):
951 case htons(ETH_P_MPLS_MC): {
952 struct mpls_label mpls_tmp, *mpls;
954 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
955 sizeof(*mpls), &mpls_tmp);
959 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
963 case htons(ETH_P_80221):
964 /* 802.21 is always network control traffic */
971 unsigned int i, tmp_dscp = dscp >> 2;
973 for (i = 0; i < qos_map->num_des; i++) {
974 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
975 ret = qos_map->dscp_exception[i].up;
980 for (i = 0; i < 8; i++) {
981 if (tmp_dscp >= qos_map->up[i].low &&
982 tmp_dscp <= qos_map->up[i].high) {
989 /* The default mapping as defined Section 2.3 in RFC8325: The three
990 * Most Significant Bits (MSBs) of the DSCP are used as the
991 * corresponding L2 markings.
995 /* Handle specific DSCP values for which the default mapping (as
996 * described above) doesn't adhere to the intended usage of the DSCP
997 * value. See section 4 in RFC8325. Specifically, for the following
998 * Diffserv Service Classes no update is needed:
1000 * - Low Priority Data: CS1
1001 * - Multimedia Streaming: AF31, AF32, AF33
1002 * - Multimedia Conferencing: AF41, AF42, AF43
1003 * - Network Control Traffic: CS7
1004 * - Real-Time Interactive: CS4
1006 switch (dscp >> 2) {
1010 /* High throughput data: AF11, AF12, AF13 */
1014 /* Operations, Administration, and Maintenance and Provisioning:
1022 /* Low latency data: AF21, AF22, AF23 */
1026 /* Broadcasting video: CS3 */
1030 /* Signaling: CS5 */
1034 /* Voice Admit: VA */
1038 /* Telephony traffic: EF */
1042 /* Network Control Traffic: CS6 */
1047 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
1049 EXPORT_SYMBOL(cfg80211_classify8021d);
1051 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
1053 const struct cfg80211_bss_ies *ies;
1055 ies = rcu_dereference(bss->ies);
1059 return cfg80211_find_elem(id, ies->data, ies->len);
1061 EXPORT_SYMBOL(ieee80211_bss_get_elem);
1063 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
1065 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1066 struct net_device *dev = wdev->netdev;
1069 if (!wdev->connect_keys)
1072 for (i = 0; i < 4; i++) {
1073 if (!wdev->connect_keys->params[i].cipher)
1075 if (rdev_add_key(rdev, dev, -1, i, false, NULL,
1076 &wdev->connect_keys->params[i])) {
1077 netdev_err(dev, "failed to set key %d\n", i);
1080 if (wdev->connect_keys->def == i &&
1081 rdev_set_default_key(rdev, dev, -1, i, true, true)) {
1082 netdev_err(dev, "failed to set defkey %d\n", i);
1087 kfree_sensitive(wdev->connect_keys);
1088 wdev->connect_keys = NULL;
1091 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
1093 struct cfg80211_event *ev;
1094 unsigned long flags;
1096 spin_lock_irqsave(&wdev->event_lock, flags);
1097 while (!list_empty(&wdev->event_list)) {
1098 ev = list_first_entry(&wdev->event_list,
1099 struct cfg80211_event, list);
1100 list_del(&ev->list);
1101 spin_unlock_irqrestore(&wdev->event_lock, flags);
1104 case EVENT_CONNECT_RESULT:
1105 __cfg80211_connect_result(
1108 ev->cr.status == WLAN_STATUS_SUCCESS);
1111 __cfg80211_roamed(wdev, &ev->rm);
1113 case EVENT_DISCONNECTED:
1114 __cfg80211_disconnected(wdev->netdev,
1115 ev->dc.ie, ev->dc.ie_len,
1117 !ev->dc.locally_generated);
1119 case EVENT_IBSS_JOINED:
1120 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
1124 cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
1126 case EVENT_PORT_AUTHORIZED:
1127 __cfg80211_port_authorized(wdev, ev->pa.peer_addr,
1129 ev->pa.td_bitmap_len);
1135 spin_lock_irqsave(&wdev->event_lock, flags);
1137 spin_unlock_irqrestore(&wdev->event_lock, flags);
1140 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1142 struct wireless_dev *wdev;
1144 lockdep_assert_held(&rdev->wiphy.mtx);
1146 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1147 cfg80211_process_wdev_events(wdev);
1150 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1151 struct net_device *dev, enum nl80211_iftype ntype,
1152 struct vif_params *params)
1155 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1157 lockdep_assert_held(&rdev->wiphy.mtx);
1159 /* don't support changing VLANs, you just re-create them */
1160 if (otype == NL80211_IFTYPE_AP_VLAN)
1163 /* cannot change into P2P device or NAN */
1164 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1165 ntype == NL80211_IFTYPE_NAN)
1168 if (!rdev->ops->change_virtual_intf ||
1169 !(rdev->wiphy.interface_modes & (1 << ntype)))
1172 if (ntype != otype) {
1173 /* if it's part of a bridge, reject changing type to station/ibss */
1174 if (netif_is_bridge_port(dev) &&
1175 (ntype == NL80211_IFTYPE_ADHOC ||
1176 ntype == NL80211_IFTYPE_STATION ||
1177 ntype == NL80211_IFTYPE_P2P_CLIENT))
1180 dev->ieee80211_ptr->use_4addr = false;
1181 rdev_set_qos_map(rdev, dev, NULL);
1184 case NL80211_IFTYPE_AP:
1185 case NL80211_IFTYPE_P2P_GO:
1186 cfg80211_stop_ap(rdev, dev, -1, true);
1188 case NL80211_IFTYPE_ADHOC:
1189 cfg80211_leave_ibss(rdev, dev, false);
1191 case NL80211_IFTYPE_STATION:
1192 case NL80211_IFTYPE_P2P_CLIENT:
1193 cfg80211_disconnect(rdev, dev,
1194 WLAN_REASON_DEAUTH_LEAVING, true);
1196 case NL80211_IFTYPE_MESH_POINT:
1197 /* mesh should be handled? */
1199 case NL80211_IFTYPE_OCB:
1200 cfg80211_leave_ocb(rdev, dev);
1206 cfg80211_process_rdev_events(rdev);
1207 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1209 memset(&dev->ieee80211_ptr->u, 0,
1210 sizeof(dev->ieee80211_ptr->u));
1211 memset(&dev->ieee80211_ptr->links, 0,
1212 sizeof(dev->ieee80211_ptr->links));
1215 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1217 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1219 if (!err && params && params->use_4addr != -1)
1220 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1223 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1225 case NL80211_IFTYPE_STATION:
1226 if (dev->ieee80211_ptr->use_4addr)
1229 case NL80211_IFTYPE_OCB:
1230 case NL80211_IFTYPE_P2P_CLIENT:
1231 case NL80211_IFTYPE_ADHOC:
1232 dev->priv_flags |= IFF_DONT_BRIDGE;
1234 case NL80211_IFTYPE_P2P_GO:
1235 case NL80211_IFTYPE_AP:
1236 case NL80211_IFTYPE_AP_VLAN:
1237 case NL80211_IFTYPE_MESH_POINT:
1240 case NL80211_IFTYPE_MONITOR:
1241 /* monitor can't bridge anyway */
1243 case NL80211_IFTYPE_UNSPECIFIED:
1244 case NUM_NL80211_IFTYPES:
1247 case NL80211_IFTYPE_P2P_DEVICE:
1248 case NL80211_IFTYPE_WDS:
1249 case NL80211_IFTYPE_NAN:
1255 if (!err && ntype != otype && netif_running(dev)) {
1256 cfg80211_update_iface_num(rdev, ntype, 1);
1257 cfg80211_update_iface_num(rdev, otype, -1);
1263 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1265 int modulation, streams, bitrate;
1267 /* the formula below does only work for MCS values smaller than 32 */
1268 if (WARN_ON_ONCE(rate->mcs >= 32))
1271 modulation = rate->mcs & 7;
1272 streams = (rate->mcs >> 3) + 1;
1274 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1277 bitrate *= (modulation + 1);
1278 else if (modulation == 4)
1279 bitrate *= (modulation + 2);
1281 bitrate *= (modulation + 3);
1285 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1286 bitrate = (bitrate / 9) * 10;
1288 /* do NOT round down here */
1289 return (bitrate + 50000) / 100000;
1292 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1294 static const u32 __mcs2bitrate[] = {
1302 [5] = 12512, /* 1251.25 mbps */
1312 [14] = 8662, /* 866.25 mbps */
1322 [24] = 67568, /* 6756.75 mbps */
1333 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1336 return __mcs2bitrate[rate->mcs];
1339 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1341 static const u32 __mcs2bitrate[] = {
1342 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1343 [7 - 6] = 50050, /* MCS 12.1 */
1351 /* Extended SC MCS not defined for base MCS below 6 or above 12 */
1352 if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1355 return __mcs2bitrate[rate->mcs - 6];
1358 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1360 static const u32 __mcs2bitrate[] = {
1368 [5] = 12512, /* 1251.25 mbps */
1386 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1389 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1392 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1394 static const u32 base[4][12] = {
1404 /* not in the spec, but some devices use this: */
1456 case RATE_INFO_BW_160:
1459 case RATE_INFO_BW_80:
1462 case RATE_INFO_BW_40:
1465 case RATE_INFO_BW_5:
1466 case RATE_INFO_BW_10:
1469 case RATE_INFO_BW_20:
1473 bitrate = base[idx][rate->mcs];
1474 bitrate *= rate->nss;
1476 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1477 bitrate = (bitrate / 9) * 10;
1479 /* do NOT round down here */
1480 return (bitrate + 50000) / 100000;
1482 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1483 rate->bw, rate->mcs, rate->nss);
1487 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1490 u32 mcs_divisors[14] = {
1491 102399, /* 16.666666... */
1492 51201, /* 8.333333... */
1493 34134, /* 5.555555... */
1494 25599, /* 4.166666... */
1495 17067, /* 2.777777... */
1496 12801, /* 2.083333... */
1497 11377, /* 1.851725... */
1498 10239, /* 1.666666... */
1499 8532, /* 1.388888... */
1500 7680, /* 1.250000... */
1501 6828, /* 1.111111... */
1502 6144, /* 1.000000... */
1503 5690, /* 0.926106... */
1504 5120, /* 0.833333... */
1506 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1507 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1508 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1509 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1510 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1511 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1512 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1516 if (WARN_ON_ONCE(rate->mcs > 13))
1519 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1521 if (WARN_ON_ONCE(rate->he_ru_alloc >
1522 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1524 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1527 if (rate->bw == RATE_INFO_BW_160)
1528 result = rates_160M[rate->he_gi];
1529 else if (rate->bw == RATE_INFO_BW_80 ||
1530 (rate->bw == RATE_INFO_BW_HE_RU &&
1531 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1532 result = rates_969[rate->he_gi];
1533 else if (rate->bw == RATE_INFO_BW_40 ||
1534 (rate->bw == RATE_INFO_BW_HE_RU &&
1535 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1536 result = rates_484[rate->he_gi];
1537 else if (rate->bw == RATE_INFO_BW_20 ||
1538 (rate->bw == RATE_INFO_BW_HE_RU &&
1539 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1540 result = rates_242[rate->he_gi];
1541 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1542 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1543 result = rates_106[rate->he_gi];
1544 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1545 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1546 result = rates_52[rate->he_gi];
1547 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1548 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1549 result = rates_26[rate->he_gi];
1551 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1552 rate->bw, rate->he_ru_alloc);
1556 /* now scale to the appropriate MCS */
1559 do_div(tmp, mcs_divisors[rate->mcs]);
1562 /* and take NSS, DCM into account */
1563 result = (result * rate->nss) / 8;
1567 return result / 10000;
1570 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1573 static const u32 mcs_divisors[16] = {
1574 102399, /* 16.666666... */
1575 51201, /* 8.333333... */
1576 34134, /* 5.555555... */
1577 25599, /* 4.166666... */
1578 17067, /* 2.777777... */
1579 12801, /* 2.083333... */
1580 11377, /* 1.851725... */
1581 10239, /* 1.666666... */
1582 8532, /* 1.388888... */
1583 7680, /* 1.250000... */
1584 6828, /* 1.111111... */
1585 6144, /* 1.000000... */
1586 5690, /* 0.926106... */
1587 5120, /* 0.833333... */
1588 409600, /* 66.666666... */
1589 204800, /* 33.333333... */
1591 static const u32 rates_996[3] = { 480388888, 453700000, 408333333 };
1592 static const u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1593 static const u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1594 static const u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1595 static const u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1596 static const u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1600 if (WARN_ON_ONCE(rate->mcs > 15))
1602 if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1604 if (WARN_ON_ONCE(rate->eht_ru_alloc >
1605 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1607 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1610 /* Bandwidth checks for MCS 14 */
1611 if (rate->mcs == 14) {
1612 if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1613 rate->bw != RATE_INFO_BW_80 &&
1614 rate->bw != RATE_INFO_BW_160 &&
1615 rate->bw != RATE_INFO_BW_320) ||
1616 (rate->bw == RATE_INFO_BW_EHT_RU &&
1617 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1618 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1619 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1620 WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1621 rate->bw, rate->eht_ru_alloc);
1626 if (rate->bw == RATE_INFO_BW_320 ||
1627 (rate->bw == RATE_INFO_BW_EHT_RU &&
1628 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1629 result = 4 * rates_996[rate->eht_gi];
1630 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1631 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1632 result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1633 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1634 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1635 result = 3 * rates_996[rate->eht_gi];
1636 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1637 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1638 result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1639 else if (rate->bw == RATE_INFO_BW_160 ||
1640 (rate->bw == RATE_INFO_BW_EHT_RU &&
1641 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1642 result = 2 * rates_996[rate->eht_gi];
1643 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1644 rate->eht_ru_alloc ==
1645 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1646 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1647 + rates_242[rate->eht_gi];
1648 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1649 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1650 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1651 else if (rate->bw == RATE_INFO_BW_80 ||
1652 (rate->bw == RATE_INFO_BW_EHT_RU &&
1653 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1654 result = rates_996[rate->eht_gi];
1655 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1656 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1657 result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1658 else if (rate->bw == RATE_INFO_BW_40 ||
1659 (rate->bw == RATE_INFO_BW_EHT_RU &&
1660 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1661 result = rates_484[rate->eht_gi];
1662 else if (rate->bw == RATE_INFO_BW_20 ||
1663 (rate->bw == RATE_INFO_BW_EHT_RU &&
1664 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1665 result = rates_242[rate->eht_gi];
1666 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1667 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1668 result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1669 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1670 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1671 result = rates_106[rate->eht_gi];
1672 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1673 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1674 result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1675 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1676 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1677 result = rates_52[rate->eht_gi];
1678 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1679 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1680 result = rates_26[rate->eht_gi];
1682 WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1683 rate->bw, rate->eht_ru_alloc);
1687 /* now scale to the appropriate MCS */
1690 do_div(tmp, mcs_divisors[rate->mcs]);
1698 return result / 10000;
1701 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
1703 /* For 1, 2, 4, 8 and 16 MHz channels */
1704 static const u32 base[5][11] = {
1715 /* MCS 10 supported in 1 MHz only */
1727 /* MCS 9 not valid */
1764 /* default is 1 MHz index */
1767 if (rate->mcs >= 11)
1771 case RATE_INFO_BW_16:
1774 case RATE_INFO_BW_8:
1777 case RATE_INFO_BW_4:
1780 case RATE_INFO_BW_2:
1783 case RATE_INFO_BW_1:
1786 case RATE_INFO_BW_5:
1787 case RATE_INFO_BW_10:
1788 case RATE_INFO_BW_20:
1789 case RATE_INFO_BW_40:
1790 case RATE_INFO_BW_80:
1791 case RATE_INFO_BW_160:
1796 bitrate = base[idx][rate->mcs];
1797 bitrate *= rate->nss;
1799 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1800 bitrate = (bitrate / 9) * 10;
1801 /* do NOT round down here */
1802 return (bitrate + 50000) / 100000;
1804 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1805 rate->bw, rate->mcs, rate->nss);
1809 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1811 if (rate->flags & RATE_INFO_FLAGS_MCS)
1812 return cfg80211_calculate_bitrate_ht(rate);
1813 if (rate->flags & RATE_INFO_FLAGS_DMG)
1814 return cfg80211_calculate_bitrate_dmg(rate);
1815 if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1816 return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1817 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1818 return cfg80211_calculate_bitrate_edmg(rate);
1819 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1820 return cfg80211_calculate_bitrate_vht(rate);
1821 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1822 return cfg80211_calculate_bitrate_he(rate);
1823 if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1824 return cfg80211_calculate_bitrate_eht(rate);
1825 if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
1826 return cfg80211_calculate_bitrate_s1g(rate);
1828 return rate->legacy;
1830 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1832 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1833 enum ieee80211_p2p_attr_id attr,
1834 u8 *buf, unsigned int bufsize)
1837 u16 attr_remaining = 0;
1838 bool desired_attr = false;
1839 u16 desired_len = 0;
1842 unsigned int iedatalen;
1849 if (iedatalen + 2 > len)
1852 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1860 /* check WFA OUI, P2P subtype */
1861 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1862 iedata[2] != 0x9a || iedata[3] != 0x09)
1868 /* check attribute continuation into this IE */
1869 copy = min_t(unsigned int, attr_remaining, iedatalen);
1870 if (copy && desired_attr) {
1871 desired_len += copy;
1873 memcpy(out, iedata, min(bufsize, copy));
1874 out += min(bufsize, copy);
1875 bufsize -= min(bufsize, copy);
1879 if (copy == attr_remaining)
1883 attr_remaining -= copy;
1890 while (iedatalen > 0) {
1893 /* P2P attribute ID & size must fit */
1896 desired_attr = iedata[0] == attr;
1897 attr_len = get_unaligned_le16(iedata + 1);
1901 copy = min_t(unsigned int, attr_len, iedatalen);
1904 desired_len += copy;
1906 memcpy(out, iedata, min(bufsize, copy));
1907 out += min(bufsize, copy);
1908 bufsize -= min(bufsize, copy);
1911 if (copy == attr_len)
1917 attr_remaining = attr_len - copy;
1925 if (attr_remaining && desired_attr)
1930 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1932 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1936 /* Make sure array values are legal */
1937 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1942 if (ids[i] == WLAN_EID_EXTENSION) {
1943 if (id_ext && (ids[i + 1] == id))
1950 if (ids[i] == id && !id_ext)
1958 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1960 /* we assume a validly formed IEs buffer */
1961 u8 len = ies[pos + 1];
1965 /* the IE itself must have 255 bytes for fragments to follow */
1969 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1977 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1978 const u8 *ids, int n_ids,
1979 const u8 *after_ric, int n_after_ric,
1982 size_t pos = offset;
1984 while (pos < ielen) {
1987 if (ies[pos] == WLAN_EID_EXTENSION)
1989 if ((pos + ext) >= ielen)
1992 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1993 ies[pos] == WLAN_EID_EXTENSION))
1996 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1997 pos = skip_ie(ies, ielen, pos);
1999 while (pos < ielen) {
2000 if (ies[pos] == WLAN_EID_EXTENSION)
2005 if ((pos + ext) >= ielen)
2008 if (!ieee80211_id_in_list(after_ric,
2012 pos = skip_ie(ies, ielen, pos);
2017 pos = skip_ie(ies, ielen, pos);
2023 EXPORT_SYMBOL(ieee80211_ie_split_ric);
2025 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id)
2027 unsigned int elem_len;
2032 elem_len = skb->data + skb->len - len_pos - 1;
2034 while (elem_len > 255) {
2035 /* this one is 255 */
2037 /* remaining data gets smaller */
2039 /* make space for the fragment ID/len in SKB */
2041 /* shift back the remaining data to place fragment ID/len */
2042 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
2043 /* place the fragment ID */
2046 /* and point to fragment length to update later */
2050 *len_pos = elem_len;
2052 EXPORT_SYMBOL(ieee80211_fragment_element);
2054 bool ieee80211_operating_class_to_band(u8 operating_class,
2055 enum nl80211_band *band)
2057 switch (operating_class) {
2061 *band = NL80211_BAND_5GHZ;
2065 *band = NL80211_BAND_6GHZ;
2071 *band = NL80211_BAND_2GHZ;
2074 *band = NL80211_BAND_60GHZ;
2080 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
2082 bool ieee80211_operating_class_to_chandef(u8 operating_class,
2083 struct ieee80211_channel *chan,
2084 struct cfg80211_chan_def *chandef)
2086 u32 control_freq, offset = 0;
2087 enum nl80211_band band;
2089 if (!ieee80211_operating_class_to_band(operating_class, &band) ||
2090 !chan || band != chan->band)
2093 control_freq = chan->center_freq;
2094 chandef->chan = chan;
2096 if (control_freq >= 5955)
2097 offset = control_freq - 5955;
2098 else if (control_freq >= 5745)
2099 offset = control_freq - 5745;
2100 else if (control_freq >= 5180)
2101 offset = control_freq - 5180;
2104 switch (operating_class) {
2105 case 81: /* 2 GHz band; 20 MHz; channels 1..13 */
2106 case 82: /* 2 GHz band; 20 MHz; channel 14 */
2107 case 115: /* 5 GHz band; 20 MHz; channels 36,40,44,48 */
2108 case 118: /* 5 GHz band; 20 MHz; channels 52,56,60,64 */
2109 case 121: /* 5 GHz band; 20 MHz; channels 100..144 */
2110 case 124: /* 5 GHz band; 20 MHz; channels 149,153,157,161 */
2111 case 125: /* 5 GHz band; 20 MHz; channels 149..177 */
2112 case 131: /* 6 GHz band; 20 MHz; channels 1..233*/
2113 case 136: /* 6 GHz band; 20 MHz; channel 2 */
2114 chandef->center_freq1 = control_freq;
2115 chandef->width = NL80211_CHAN_WIDTH_20;
2117 case 83: /* 2 GHz band; 40 MHz; channels 1..9 */
2118 case 116: /* 5 GHz band; 40 MHz; channels 36,44 */
2119 case 119: /* 5 GHz band; 40 MHz; channels 52,60 */
2120 case 122: /* 5 GHz band; 40 MHz; channels 100,108,116,124,132,140 */
2121 case 126: /* 5 GHz band; 40 MHz; channels 149,157,165,173 */
2122 chandef->center_freq1 = control_freq + 10;
2123 chandef->width = NL80211_CHAN_WIDTH_40;
2125 case 84: /* 2 GHz band; 40 MHz; channels 5..13 */
2126 case 117: /* 5 GHz band; 40 MHz; channels 40,48 */
2127 case 120: /* 5 GHz band; 40 MHz; channels 56,64 */
2128 case 123: /* 5 GHz band; 40 MHz; channels 104,112,120,128,136,144 */
2129 case 127: /* 5 GHz band; 40 MHz; channels 153,161,169,177 */
2130 chandef->center_freq1 = control_freq - 10;
2131 chandef->width = NL80211_CHAN_WIDTH_40;
2133 case 132: /* 6 GHz band; 40 MHz; channels 1,5,..,229*/
2134 chandef->center_freq1 = control_freq + 10 - (offset & 1) * 20;
2135 chandef->width = NL80211_CHAN_WIDTH_40;
2137 case 128: /* 5 GHz band; 80 MHz; channels 36..64,100..144,149..177 */
2138 case 133: /* 6 GHz band; 80 MHz; channels 1,5,..,229 */
2139 chandef->center_freq1 = control_freq + 30 - (offset & 3) * 20;
2140 chandef->width = NL80211_CHAN_WIDTH_80;
2142 case 129: /* 5 GHz band; 160 MHz; channels 36..64,100..144,149..177 */
2143 case 134: /* 6 GHz band; 160 MHz; channels 1,5,..,229 */
2144 chandef->center_freq1 = control_freq + 70 - (offset & 7) * 20;
2145 chandef->width = NL80211_CHAN_WIDTH_160;
2147 case 130: /* 5 GHz band; 80+80 MHz; channels 36..64,100..144,149..177 */
2148 case 135: /* 6 GHz band; 80+80 MHz; channels 1,5,..,229 */
2149 /* The center_freq2 of 80+80 MHz is unknown */
2150 case 137: /* 6 GHz band; 320 MHz; channels 1,5,..,229 */
2151 /* 320-1 or 320-2 channelization is unknown */
2156 EXPORT_SYMBOL(ieee80211_operating_class_to_chandef);
2158 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
2162 u32 freq = chandef->center_freq1;
2164 if (freq >= 2412 && freq <= 2472) {
2165 if (chandef->width > NL80211_CHAN_WIDTH_40)
2168 /* 2.407 GHz, channels 1..13 */
2169 if (chandef->width == NL80211_CHAN_WIDTH_40) {
2170 if (freq > chandef->chan->center_freq)
2171 *op_class = 83; /* HT40+ */
2173 *op_class = 84; /* HT40- */
2182 /* channel 14 is only for IEEE 802.11b */
2183 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
2186 *op_class = 82; /* channel 14 */
2190 switch (chandef->width) {
2191 case NL80211_CHAN_WIDTH_80:
2194 case NL80211_CHAN_WIDTH_160:
2197 case NL80211_CHAN_WIDTH_80P80:
2200 case NL80211_CHAN_WIDTH_10:
2201 case NL80211_CHAN_WIDTH_5:
2202 return false; /* unsupported for now */
2208 /* 5 GHz, channels 36..48 */
2209 if (freq >= 5180 && freq <= 5240) {
2211 *op_class = vht_opclass;
2212 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2213 if (freq > chandef->chan->center_freq)
2224 /* 5 GHz, channels 52..64 */
2225 if (freq >= 5260 && freq <= 5320) {
2227 *op_class = vht_opclass;
2228 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2229 if (freq > chandef->chan->center_freq)
2240 /* 5 GHz, channels 100..144 */
2241 if (freq >= 5500 && freq <= 5720) {
2243 *op_class = vht_opclass;
2244 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2245 if (freq > chandef->chan->center_freq)
2256 /* 5 GHz, channels 149..169 */
2257 if (freq >= 5745 && freq <= 5845) {
2259 *op_class = vht_opclass;
2260 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2261 if (freq > chandef->chan->center_freq)
2265 } else if (freq <= 5805) {
2274 /* 56.16 GHz, channel 1..4 */
2275 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
2276 if (chandef->width >= NL80211_CHAN_WIDTH_40)
2283 /* not supported yet */
2286 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
2288 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
2290 switch (wdev->iftype) {
2291 case NL80211_IFTYPE_AP:
2292 case NL80211_IFTYPE_P2P_GO:
2293 WARN_ON(wdev->valid_links);
2294 return wdev->links[0].ap.beacon_interval;
2295 case NL80211_IFTYPE_MESH_POINT:
2296 return wdev->u.mesh.beacon_interval;
2297 case NL80211_IFTYPE_ADHOC:
2298 return wdev->u.ibss.beacon_interval;
2306 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
2307 u32 *beacon_int_gcd,
2308 bool *beacon_int_different)
2310 struct wireless_dev *wdev;
2312 *beacon_int_gcd = 0;
2313 *beacon_int_different = false;
2315 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
2318 /* this feature isn't supported with MLO */
2319 if (wdev->valid_links)
2322 wdev_bi = cfg80211_wdev_bi(wdev);
2327 if (!*beacon_int_gcd) {
2328 *beacon_int_gcd = wdev_bi;
2332 if (wdev_bi == *beacon_int_gcd)
2335 *beacon_int_different = true;
2336 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
2339 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
2340 if (*beacon_int_gcd)
2341 *beacon_int_different = true;
2342 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
2346 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2347 enum nl80211_iftype iftype, u32 beacon_int)
2350 * This is just a basic pre-condition check; if interface combinations
2351 * are possible the driver must already be checking those with a call
2352 * to cfg80211_check_combinations(), in which case we'll validate more
2353 * through the cfg80211_calculate_bi_data() call and code in
2354 * cfg80211_iter_combinations().
2357 if (beacon_int < 10 || beacon_int > 10000)
2363 int cfg80211_iter_combinations(struct wiphy *wiphy,
2364 struct iface_combination_params *params,
2365 void (*iter)(const struct ieee80211_iface_combination *c,
2369 const struct ieee80211_regdomain *regdom;
2370 enum nl80211_dfs_regions region = 0;
2372 int num_interfaces = 0;
2373 u32 used_iftypes = 0;
2375 bool beacon_int_different;
2378 * This is a bit strange, since the iteration used to rely only on
2379 * the data given by the driver, but here it now relies on context,
2380 * in form of the currently operating interfaces.
2381 * This is OK for all current users, and saves us from having to
2382 * push the GCD calculations into all the drivers.
2383 * In the future, this should probably rely more on data that's in
2384 * cfg80211 already - the only thing not would appear to be any new
2385 * interfaces (while being brought up) and channel/radar data.
2387 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2388 &beacon_int_gcd, &beacon_int_different);
2390 if (params->radar_detect) {
2392 regdom = rcu_dereference(cfg80211_regdomain);
2394 region = regdom->dfs_region;
2398 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2399 num_interfaces += params->iftype_num[iftype];
2400 if (params->iftype_num[iftype] > 0 &&
2401 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2402 used_iftypes |= BIT(iftype);
2405 for (i = 0; i < wiphy->n_iface_combinations; i++) {
2406 const struct ieee80211_iface_combination *c;
2407 struct ieee80211_iface_limit *limits;
2408 u32 all_iftypes = 0;
2410 c = &wiphy->iface_combinations[i];
2412 if (num_interfaces > c->max_interfaces)
2414 if (params->num_different_channels > c->num_different_channels)
2417 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2422 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2423 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2425 for (j = 0; j < c->n_limits; j++) {
2426 all_iftypes |= limits[j].types;
2427 if (!(limits[j].types & BIT(iftype)))
2429 if (limits[j].max < params->iftype_num[iftype])
2431 limits[j].max -= params->iftype_num[iftype];
2435 if (params->radar_detect !=
2436 (c->radar_detect_widths & params->radar_detect))
2439 if (params->radar_detect && c->radar_detect_regions &&
2440 !(c->radar_detect_regions & BIT(region)))
2443 /* Finally check that all iftypes that we're currently
2444 * using are actually part of this combination. If they
2445 * aren't then we can't use this combination and have
2446 * to continue to the next.
2448 if ((all_iftypes & used_iftypes) != used_iftypes)
2451 if (beacon_int_gcd) {
2452 if (c->beacon_int_min_gcd &&
2453 beacon_int_gcd < c->beacon_int_min_gcd)
2455 if (!c->beacon_int_min_gcd && beacon_int_different)
2459 /* This combination covered all interface types and
2460 * supported the requested numbers, so we're good.
2470 EXPORT_SYMBOL(cfg80211_iter_combinations);
2473 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2480 int cfg80211_check_combinations(struct wiphy *wiphy,
2481 struct iface_combination_params *params)
2485 err = cfg80211_iter_combinations(wiphy, params,
2486 cfg80211_iter_sum_ifcombs, &num);
2494 EXPORT_SYMBOL(cfg80211_check_combinations);
2496 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2497 const u8 *rates, unsigned int n_rates,
2505 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2510 for (i = 0; i < n_rates; i++) {
2511 int rate = (rates[i] & 0x7f) * 5;
2514 for (j = 0; j < sband->n_bitrates; j++) {
2515 if (sband->bitrates[j].bitrate == rate) {
2526 * mask must have at least one bit set here since we
2527 * didn't accept a 0-length rates array nor allowed
2528 * entries in the array that didn't exist
2534 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2536 enum nl80211_band band;
2537 unsigned int n_channels = 0;
2539 for (band = 0; band < NUM_NL80211_BANDS; band++)
2540 if (wiphy->bands[band])
2541 n_channels += wiphy->bands[band]->n_channels;
2545 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2547 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2548 struct station_info *sinfo)
2550 struct cfg80211_registered_device *rdev;
2551 struct wireless_dev *wdev;
2553 wdev = dev->ieee80211_ptr;
2557 rdev = wiphy_to_rdev(wdev->wiphy);
2558 if (!rdev->ops->get_station)
2561 memset(sinfo, 0, sizeof(*sinfo));
2563 return rdev_get_station(rdev, dev, mac_addr, sinfo);
2565 EXPORT_SYMBOL(cfg80211_get_station);
2567 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2574 kfree(f->serv_spec_info);
2577 for (i = 0; i < f->num_rx_filters; i++)
2578 kfree(f->rx_filters[i].filter);
2580 for (i = 0; i < f->num_tx_filters; i++)
2581 kfree(f->tx_filters[i].filter);
2583 kfree(f->rx_filters);
2584 kfree(f->tx_filters);
2587 EXPORT_SYMBOL(cfg80211_free_nan_func);
2589 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2590 u32 center_freq_khz, u32 bw_khz)
2592 u32 start_freq_khz, end_freq_khz;
2594 start_freq_khz = center_freq_khz - (bw_khz / 2);
2595 end_freq_khz = center_freq_khz + (bw_khz / 2);
2597 if (start_freq_khz >= freq_range->start_freq_khz &&
2598 end_freq_khz <= freq_range->end_freq_khz)
2604 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2606 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2607 sizeof(*(sinfo->pertid)),
2614 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2616 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2617 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2618 const unsigned char rfc1042_header[] __aligned(2) =
2619 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2620 EXPORT_SYMBOL(rfc1042_header);
2622 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2623 const unsigned char bridge_tunnel_header[] __aligned(2) =
2624 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2625 EXPORT_SYMBOL(bridge_tunnel_header);
2627 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2628 struct iapp_layer2_update {
2629 u8 da[ETH_ALEN]; /* broadcast */
2630 u8 sa[ETH_ALEN]; /* STA addr */
2638 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2640 struct iapp_layer2_update *msg;
2641 struct sk_buff *skb;
2643 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2646 skb = dev_alloc_skb(sizeof(*msg));
2649 msg = skb_put(skb, sizeof(*msg));
2651 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2652 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2654 eth_broadcast_addr(msg->da);
2655 ether_addr_copy(msg->sa, addr);
2656 msg->len = htons(6);
2658 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2659 msg->control = 0xaf; /* XID response lsb.1111F101.
2660 * F=0 (no poll command; unsolicited frame) */
2661 msg->xid_info[0] = 0x81; /* XID format identifier */
2662 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2663 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2666 skb->protocol = eth_type_trans(skb, dev);
2667 memset(skb->cb, 0, sizeof(skb->cb));
2670 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2672 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2673 enum ieee80211_vht_chanwidth bw,
2674 int mcs, bool ext_nss_bw_capable,
2675 unsigned int max_vht_nss)
2677 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2680 int i, mcs_encoding;
2685 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2695 /* find max_vht_nss for the given MCS */
2696 for (i = 7; i >= 0; i--) {
2697 int supp = (map >> (2 * i)) & 3;
2702 if (supp >= mcs_encoding) {
2703 max_vht_nss = i + 1;
2709 if (!(cap->supp_mcs.tx_mcs_map &
2710 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2713 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2714 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2715 supp_width = le32_get_bits(cap->vht_cap_info,
2716 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2718 /* if not capable, treat ext_nss_bw as 0 */
2719 if (!ext_nss_bw_capable)
2722 /* This is invalid */
2723 if (supp_width == 3)
2726 /* This is an invalid combination so pretend nothing is supported */
2727 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2731 * Cover all the special cases according to IEEE 802.11-2016
2732 * Table 9-250. All other cases are either factor of 1 or not
2736 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2737 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2738 if ((supp_width == 1 || supp_width == 2) &&
2740 return 2 * max_vht_nss;
2742 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2743 if (supp_width == 0 &&
2744 (ext_nss_bw == 1 || ext_nss_bw == 2))
2745 return max_vht_nss / 2;
2746 if (supp_width == 0 &&
2748 return (3 * max_vht_nss) / 4;
2749 if (supp_width == 1 &&
2751 return 2 * max_vht_nss;
2753 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2754 if (supp_width == 0 && ext_nss_bw == 1)
2755 return 0; /* not possible */
2756 if (supp_width == 0 &&
2758 return max_vht_nss / 2;
2759 if (supp_width == 0 &&
2761 return (3 * max_vht_nss) / 4;
2762 if (supp_width == 1 &&
2764 return 0; /* not possible */
2765 if (supp_width == 1 &&
2767 return max_vht_nss / 2;
2768 if (supp_width == 1 &&
2770 return (3 * max_vht_nss) / 4;
2774 /* not covered or invalid combination received */
2777 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2779 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2780 bool is_4addr, u8 check_swif)
2783 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2785 switch (check_swif) {
2787 if (is_vlan && is_4addr)
2788 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2789 return wiphy->interface_modes & BIT(iftype);
2791 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2792 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2793 return wiphy->software_iftypes & BIT(iftype);
2800 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2802 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2804 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2806 lockdep_assert_wiphy(wdev->wiphy);
2808 switch (wdev->iftype) {
2809 case NL80211_IFTYPE_AP:
2810 case NL80211_IFTYPE_P2P_GO:
2811 cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2814 /* per-link not relevant */
2818 wdev->valid_links &= ~BIT(link_id);
2820 rdev_del_intf_link(rdev, wdev, link_id);
2822 eth_zero_addr(wdev->links[link_id].addr);
2825 void cfg80211_remove_links(struct wireless_dev *wdev)
2827 unsigned int link_id;
2830 * links are controlled by upper layers (userspace/cfg)
2831 * only for AP mode, so only remove them here for AP
2833 if (wdev->iftype != NL80211_IFTYPE_AP)
2836 if (wdev->valid_links) {
2837 for_each_valid_link(wdev, link_id)
2838 cfg80211_remove_link(wdev, link_id);
2842 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2843 struct wireless_dev *wdev)
2845 cfg80211_remove_links(wdev);
2847 return rdev_del_virtual_intf(rdev, wdev);
2850 const struct wiphy_iftype_ext_capab *
2851 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2855 for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2856 if (wiphy->iftype_ext_capab[i].iftype == type)
2857 return &wiphy->iftype_ext_capab[i];
2862 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);