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, remaining, subframe_len, padding;
796 for (offset = 0; offset < skb->len; offset += subframe_len + padding) {
803 if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0)
806 len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags,
808 subframe_len = sizeof(struct ethhdr) + len;
809 padding = (4 - subframe_len) & 0x3;
810 remaining = skb->len - offset;
812 if (subframe_len > remaining)
818 EXPORT_SYMBOL(ieee80211_is_valid_amsdu);
820 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
821 const u8 *addr, enum nl80211_iftype iftype,
822 const unsigned int extra_headroom,
823 const u8 *check_da, const u8 *check_sa,
826 unsigned int hlen = ALIGN(extra_headroom, 4);
827 struct sk_buff *frame = NULL;
828 int offset = 0, remaining;
833 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
834 bool reuse_skb = false;
836 int copy_len = sizeof(hdr.eth);
838 if (iftype == NL80211_IFTYPE_MESH_POINT)
839 copy_len = sizeof(hdr);
842 unsigned int subframe_len;
843 int len, mesh_len = 0;
846 skb_copy_bits(skb, offset, &hdr, copy_len);
847 if (iftype == NL80211_IFTYPE_MESH_POINT)
848 mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags);
849 len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags,
851 subframe_len = sizeof(struct ethhdr) + len;
852 padding = (4 - subframe_len) & 0x3;
854 /* the last MSDU has no padding */
855 remaining = skb->len - offset;
856 if (subframe_len > remaining)
858 /* mitigate A-MSDU aggregation injection attacks */
859 if (ether_addr_equal(hdr.eth.h_dest, rfc1042_header))
862 offset += sizeof(struct ethhdr);
863 last = remaining <= subframe_len + padding;
865 /* FIXME: should we really accept multicast DA? */
866 if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) &&
867 !ether_addr_equal(check_da, hdr.eth.h_dest)) ||
868 (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) {
869 offset += len + padding;
873 /* reuse skb for the last subframe */
874 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
875 skb_pull(skb, offset);
879 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
880 reuse_frag, 32 + mesh_len);
884 offset += len + padding;
887 skb_reset_network_header(frame);
888 frame->dev = skb->dev;
889 frame->priority = skb->priority;
891 if (likely(iftype != NL80211_IFTYPE_MESH_POINT &&
892 ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto)))
893 skb_pull(frame, ETH_ALEN + 2);
895 memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth));
896 __skb_queue_tail(list, frame);
905 __skb_queue_purge(list);
908 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
910 /* Given a data frame determine the 802.1p/1d tag to use. */
911 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
912 struct cfg80211_qos_map *qos_map)
915 unsigned char vlan_priority;
918 /* skb->priority values from 256->263 are magic values to
919 * directly indicate a specific 802.1d priority. This is used
920 * to allow 802.1d priority to be passed directly in from VLAN
923 if (skb->priority >= 256 && skb->priority <= 263) {
924 ret = skb->priority - 256;
928 if (skb_vlan_tag_present(skb)) {
929 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
931 if (vlan_priority > 0) {
937 switch (skb->protocol) {
938 case htons(ETH_P_IP):
939 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
941 case htons(ETH_P_IPV6):
942 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
944 case htons(ETH_P_MPLS_UC):
945 case htons(ETH_P_MPLS_MC): {
946 struct mpls_label mpls_tmp, *mpls;
948 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
949 sizeof(*mpls), &mpls_tmp);
953 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
957 case htons(ETH_P_80221):
958 /* 802.21 is always network control traffic */
965 unsigned int i, tmp_dscp = dscp >> 2;
967 for (i = 0; i < qos_map->num_des; i++) {
968 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
969 ret = qos_map->dscp_exception[i].up;
974 for (i = 0; i < 8; i++) {
975 if (tmp_dscp >= qos_map->up[i].low &&
976 tmp_dscp <= qos_map->up[i].high) {
985 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
987 EXPORT_SYMBOL(cfg80211_classify8021d);
989 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
991 const struct cfg80211_bss_ies *ies;
993 ies = rcu_dereference(bss->ies);
997 return cfg80211_find_elem(id, ies->data, ies->len);
999 EXPORT_SYMBOL(ieee80211_bss_get_elem);
1001 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
1003 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1004 struct net_device *dev = wdev->netdev;
1007 if (!wdev->connect_keys)
1010 for (i = 0; i < 4; i++) {
1011 if (!wdev->connect_keys->params[i].cipher)
1013 if (rdev_add_key(rdev, dev, -1, i, false, NULL,
1014 &wdev->connect_keys->params[i])) {
1015 netdev_err(dev, "failed to set key %d\n", i);
1018 if (wdev->connect_keys->def == i &&
1019 rdev_set_default_key(rdev, dev, -1, i, true, true)) {
1020 netdev_err(dev, "failed to set defkey %d\n", i);
1025 kfree_sensitive(wdev->connect_keys);
1026 wdev->connect_keys = NULL;
1029 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
1031 struct cfg80211_event *ev;
1032 unsigned long flags;
1034 spin_lock_irqsave(&wdev->event_lock, flags);
1035 while (!list_empty(&wdev->event_list)) {
1036 ev = list_first_entry(&wdev->event_list,
1037 struct cfg80211_event, list);
1038 list_del(&ev->list);
1039 spin_unlock_irqrestore(&wdev->event_lock, flags);
1042 case EVENT_CONNECT_RESULT:
1043 __cfg80211_connect_result(
1046 ev->cr.status == WLAN_STATUS_SUCCESS);
1049 __cfg80211_roamed(wdev, &ev->rm);
1051 case EVENT_DISCONNECTED:
1052 __cfg80211_disconnected(wdev->netdev,
1053 ev->dc.ie, ev->dc.ie_len,
1055 !ev->dc.locally_generated);
1057 case EVENT_IBSS_JOINED:
1058 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
1062 cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
1064 case EVENT_PORT_AUTHORIZED:
1065 __cfg80211_port_authorized(wdev, ev->pa.peer_addr,
1067 ev->pa.td_bitmap_len);
1073 spin_lock_irqsave(&wdev->event_lock, flags);
1075 spin_unlock_irqrestore(&wdev->event_lock, flags);
1078 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1080 struct wireless_dev *wdev;
1082 lockdep_assert_held(&rdev->wiphy.mtx);
1084 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1085 cfg80211_process_wdev_events(wdev);
1088 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1089 struct net_device *dev, enum nl80211_iftype ntype,
1090 struct vif_params *params)
1093 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1095 lockdep_assert_held(&rdev->wiphy.mtx);
1097 /* don't support changing VLANs, you just re-create them */
1098 if (otype == NL80211_IFTYPE_AP_VLAN)
1101 /* cannot change into P2P device or NAN */
1102 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1103 ntype == NL80211_IFTYPE_NAN)
1106 if (!rdev->ops->change_virtual_intf ||
1107 !(rdev->wiphy.interface_modes & (1 << ntype)))
1110 if (ntype != otype) {
1111 /* if it's part of a bridge, reject changing type to station/ibss */
1112 if (netif_is_bridge_port(dev) &&
1113 (ntype == NL80211_IFTYPE_ADHOC ||
1114 ntype == NL80211_IFTYPE_STATION ||
1115 ntype == NL80211_IFTYPE_P2P_CLIENT))
1118 dev->ieee80211_ptr->use_4addr = false;
1119 rdev_set_qos_map(rdev, dev, NULL);
1122 case NL80211_IFTYPE_AP:
1123 case NL80211_IFTYPE_P2P_GO:
1124 cfg80211_stop_ap(rdev, dev, -1, true);
1126 case NL80211_IFTYPE_ADHOC:
1127 cfg80211_leave_ibss(rdev, dev, false);
1129 case NL80211_IFTYPE_STATION:
1130 case NL80211_IFTYPE_P2P_CLIENT:
1131 cfg80211_disconnect(rdev, dev,
1132 WLAN_REASON_DEAUTH_LEAVING, true);
1134 case NL80211_IFTYPE_MESH_POINT:
1135 /* mesh should be handled? */
1137 case NL80211_IFTYPE_OCB:
1138 cfg80211_leave_ocb(rdev, dev);
1144 cfg80211_process_rdev_events(rdev);
1145 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1147 memset(&dev->ieee80211_ptr->u, 0,
1148 sizeof(dev->ieee80211_ptr->u));
1149 memset(&dev->ieee80211_ptr->links, 0,
1150 sizeof(dev->ieee80211_ptr->links));
1153 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1155 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1157 if (!err && params && params->use_4addr != -1)
1158 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1161 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1163 case NL80211_IFTYPE_STATION:
1164 if (dev->ieee80211_ptr->use_4addr)
1167 case NL80211_IFTYPE_OCB:
1168 case NL80211_IFTYPE_P2P_CLIENT:
1169 case NL80211_IFTYPE_ADHOC:
1170 dev->priv_flags |= IFF_DONT_BRIDGE;
1172 case NL80211_IFTYPE_P2P_GO:
1173 case NL80211_IFTYPE_AP:
1174 case NL80211_IFTYPE_AP_VLAN:
1175 case NL80211_IFTYPE_MESH_POINT:
1178 case NL80211_IFTYPE_MONITOR:
1179 /* monitor can't bridge anyway */
1181 case NL80211_IFTYPE_UNSPECIFIED:
1182 case NUM_NL80211_IFTYPES:
1185 case NL80211_IFTYPE_P2P_DEVICE:
1186 case NL80211_IFTYPE_WDS:
1187 case NL80211_IFTYPE_NAN:
1193 if (!err && ntype != otype && netif_running(dev)) {
1194 cfg80211_update_iface_num(rdev, ntype, 1);
1195 cfg80211_update_iface_num(rdev, otype, -1);
1201 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1203 int modulation, streams, bitrate;
1205 /* the formula below does only work for MCS values smaller than 32 */
1206 if (WARN_ON_ONCE(rate->mcs >= 32))
1209 modulation = rate->mcs & 7;
1210 streams = (rate->mcs >> 3) + 1;
1212 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1215 bitrate *= (modulation + 1);
1216 else if (modulation == 4)
1217 bitrate *= (modulation + 2);
1219 bitrate *= (modulation + 3);
1223 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1224 bitrate = (bitrate / 9) * 10;
1226 /* do NOT round down here */
1227 return (bitrate + 50000) / 100000;
1230 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1232 static const u32 __mcs2bitrate[] = {
1240 [5] = 12512, /* 1251.25 mbps */
1250 [14] = 8662, /* 866.25 mbps */
1260 [24] = 67568, /* 6756.75 mbps */
1271 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1274 return __mcs2bitrate[rate->mcs];
1277 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1279 static const u32 __mcs2bitrate[] = {
1280 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1281 [7 - 6] = 50050, /* MCS 12.1 */
1289 /* Extended SC MCS not defined for base MCS below 6 or above 12 */
1290 if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1293 return __mcs2bitrate[rate->mcs - 6];
1296 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1298 static const u32 __mcs2bitrate[] = {
1306 [5] = 12512, /* 1251.25 mbps */
1324 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1327 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1330 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1332 static const u32 base[4][12] = {
1342 /* not in the spec, but some devices use this: */
1394 case RATE_INFO_BW_160:
1397 case RATE_INFO_BW_80:
1400 case RATE_INFO_BW_40:
1403 case RATE_INFO_BW_5:
1404 case RATE_INFO_BW_10:
1407 case RATE_INFO_BW_20:
1411 bitrate = base[idx][rate->mcs];
1412 bitrate *= rate->nss;
1414 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1415 bitrate = (bitrate / 9) * 10;
1417 /* do NOT round down here */
1418 return (bitrate + 50000) / 100000;
1420 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1421 rate->bw, rate->mcs, rate->nss);
1425 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1428 u32 mcs_divisors[14] = {
1429 102399, /* 16.666666... */
1430 51201, /* 8.333333... */
1431 34134, /* 5.555555... */
1432 25599, /* 4.166666... */
1433 17067, /* 2.777777... */
1434 12801, /* 2.083333... */
1435 11377, /* 1.851725... */
1436 10239, /* 1.666666... */
1437 8532, /* 1.388888... */
1438 7680, /* 1.250000... */
1439 6828, /* 1.111111... */
1440 6144, /* 1.000000... */
1441 5690, /* 0.926106... */
1442 5120, /* 0.833333... */
1444 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1445 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1446 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1447 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1448 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1449 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1450 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1454 if (WARN_ON_ONCE(rate->mcs > 13))
1457 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1459 if (WARN_ON_ONCE(rate->he_ru_alloc >
1460 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1462 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1465 if (rate->bw == RATE_INFO_BW_160)
1466 result = rates_160M[rate->he_gi];
1467 else if (rate->bw == RATE_INFO_BW_80 ||
1468 (rate->bw == RATE_INFO_BW_HE_RU &&
1469 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1470 result = rates_969[rate->he_gi];
1471 else if (rate->bw == RATE_INFO_BW_40 ||
1472 (rate->bw == RATE_INFO_BW_HE_RU &&
1473 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1474 result = rates_484[rate->he_gi];
1475 else if (rate->bw == RATE_INFO_BW_20 ||
1476 (rate->bw == RATE_INFO_BW_HE_RU &&
1477 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1478 result = rates_242[rate->he_gi];
1479 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1480 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1481 result = rates_106[rate->he_gi];
1482 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1483 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1484 result = rates_52[rate->he_gi];
1485 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1486 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1487 result = rates_26[rate->he_gi];
1489 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1490 rate->bw, rate->he_ru_alloc);
1494 /* now scale to the appropriate MCS */
1497 do_div(tmp, mcs_divisors[rate->mcs]);
1500 /* and take NSS, DCM into account */
1501 result = (result * rate->nss) / 8;
1505 return result / 10000;
1508 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1511 static const u32 mcs_divisors[16] = {
1512 102399, /* 16.666666... */
1513 51201, /* 8.333333... */
1514 34134, /* 5.555555... */
1515 25599, /* 4.166666... */
1516 17067, /* 2.777777... */
1517 12801, /* 2.083333... */
1518 11377, /* 1.851725... */
1519 10239, /* 1.666666... */
1520 8532, /* 1.388888... */
1521 7680, /* 1.250000... */
1522 6828, /* 1.111111... */
1523 6144, /* 1.000000... */
1524 5690, /* 0.926106... */
1525 5120, /* 0.833333... */
1526 409600, /* 66.666666... */
1527 204800, /* 33.333333... */
1529 static const u32 rates_996[3] = { 480388888, 453700000, 408333333 };
1530 static const u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1531 static const u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1532 static const u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1533 static const u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1534 static const u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1538 if (WARN_ON_ONCE(rate->mcs > 15))
1540 if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1542 if (WARN_ON_ONCE(rate->eht_ru_alloc >
1543 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1545 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1548 /* Bandwidth checks for MCS 14 */
1549 if (rate->mcs == 14) {
1550 if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1551 rate->bw != RATE_INFO_BW_80 &&
1552 rate->bw != RATE_INFO_BW_160 &&
1553 rate->bw != RATE_INFO_BW_320) ||
1554 (rate->bw == RATE_INFO_BW_EHT_RU &&
1555 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1556 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1557 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1558 WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1559 rate->bw, rate->eht_ru_alloc);
1564 if (rate->bw == RATE_INFO_BW_320 ||
1565 (rate->bw == RATE_INFO_BW_EHT_RU &&
1566 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1567 result = 4 * rates_996[rate->eht_gi];
1568 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1569 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1570 result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1571 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1572 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1573 result = 3 * rates_996[rate->eht_gi];
1574 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1575 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1576 result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1577 else if (rate->bw == RATE_INFO_BW_160 ||
1578 (rate->bw == RATE_INFO_BW_EHT_RU &&
1579 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1580 result = 2 * rates_996[rate->eht_gi];
1581 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1582 rate->eht_ru_alloc ==
1583 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1584 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1585 + rates_242[rate->eht_gi];
1586 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1587 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1588 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1589 else if (rate->bw == RATE_INFO_BW_80 ||
1590 (rate->bw == RATE_INFO_BW_EHT_RU &&
1591 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1592 result = rates_996[rate->eht_gi];
1593 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1594 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1595 result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1596 else if (rate->bw == RATE_INFO_BW_40 ||
1597 (rate->bw == RATE_INFO_BW_EHT_RU &&
1598 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1599 result = rates_484[rate->eht_gi];
1600 else if (rate->bw == RATE_INFO_BW_20 ||
1601 (rate->bw == RATE_INFO_BW_EHT_RU &&
1602 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1603 result = rates_242[rate->eht_gi];
1604 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1605 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1606 result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1607 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1608 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1609 result = rates_106[rate->eht_gi];
1610 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1611 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1612 result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1613 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1614 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1615 result = rates_52[rate->eht_gi];
1616 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1617 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1618 result = rates_26[rate->eht_gi];
1620 WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1621 rate->bw, rate->eht_ru_alloc);
1625 /* now scale to the appropriate MCS */
1628 do_div(tmp, mcs_divisors[rate->mcs]);
1636 return result / 10000;
1639 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
1641 /* For 1, 2, 4, 8 and 16 MHz channels */
1642 static const u32 base[5][11] = {
1653 /* MCS 10 supported in 1 MHz only */
1665 /* MCS 9 not valid */
1702 /* default is 1 MHz index */
1705 if (rate->mcs >= 11)
1709 case RATE_INFO_BW_16:
1712 case RATE_INFO_BW_8:
1715 case RATE_INFO_BW_4:
1718 case RATE_INFO_BW_2:
1721 case RATE_INFO_BW_1:
1724 case RATE_INFO_BW_5:
1725 case RATE_INFO_BW_10:
1726 case RATE_INFO_BW_20:
1727 case RATE_INFO_BW_40:
1728 case RATE_INFO_BW_80:
1729 case RATE_INFO_BW_160:
1734 bitrate = base[idx][rate->mcs];
1735 bitrate *= rate->nss;
1737 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1738 bitrate = (bitrate / 9) * 10;
1739 /* do NOT round down here */
1740 return (bitrate + 50000) / 100000;
1742 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1743 rate->bw, rate->mcs, rate->nss);
1747 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1749 if (rate->flags & RATE_INFO_FLAGS_MCS)
1750 return cfg80211_calculate_bitrate_ht(rate);
1751 if (rate->flags & RATE_INFO_FLAGS_DMG)
1752 return cfg80211_calculate_bitrate_dmg(rate);
1753 if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1754 return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1755 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1756 return cfg80211_calculate_bitrate_edmg(rate);
1757 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1758 return cfg80211_calculate_bitrate_vht(rate);
1759 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1760 return cfg80211_calculate_bitrate_he(rate);
1761 if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1762 return cfg80211_calculate_bitrate_eht(rate);
1763 if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
1764 return cfg80211_calculate_bitrate_s1g(rate);
1766 return rate->legacy;
1768 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1770 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1771 enum ieee80211_p2p_attr_id attr,
1772 u8 *buf, unsigned int bufsize)
1775 u16 attr_remaining = 0;
1776 bool desired_attr = false;
1777 u16 desired_len = 0;
1780 unsigned int iedatalen;
1787 if (iedatalen + 2 > len)
1790 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1798 /* check WFA OUI, P2P subtype */
1799 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1800 iedata[2] != 0x9a || iedata[3] != 0x09)
1806 /* check attribute continuation into this IE */
1807 copy = min_t(unsigned int, attr_remaining, iedatalen);
1808 if (copy && desired_attr) {
1809 desired_len += copy;
1811 memcpy(out, iedata, min(bufsize, copy));
1812 out += min(bufsize, copy);
1813 bufsize -= min(bufsize, copy);
1817 if (copy == attr_remaining)
1821 attr_remaining -= copy;
1828 while (iedatalen > 0) {
1831 /* P2P attribute ID & size must fit */
1834 desired_attr = iedata[0] == attr;
1835 attr_len = get_unaligned_le16(iedata + 1);
1839 copy = min_t(unsigned int, attr_len, iedatalen);
1842 desired_len += copy;
1844 memcpy(out, iedata, min(bufsize, copy));
1845 out += min(bufsize, copy);
1846 bufsize -= min(bufsize, copy);
1849 if (copy == attr_len)
1855 attr_remaining = attr_len - copy;
1863 if (attr_remaining && desired_attr)
1868 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1870 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1874 /* Make sure array values are legal */
1875 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1880 if (ids[i] == WLAN_EID_EXTENSION) {
1881 if (id_ext && (ids[i + 1] == id))
1888 if (ids[i] == id && !id_ext)
1896 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1898 /* we assume a validly formed IEs buffer */
1899 u8 len = ies[pos + 1];
1903 /* the IE itself must have 255 bytes for fragments to follow */
1907 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1915 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1916 const u8 *ids, int n_ids,
1917 const u8 *after_ric, int n_after_ric,
1920 size_t pos = offset;
1922 while (pos < ielen) {
1925 if (ies[pos] == WLAN_EID_EXTENSION)
1927 if ((pos + ext) >= ielen)
1930 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1931 ies[pos] == WLAN_EID_EXTENSION))
1934 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1935 pos = skip_ie(ies, ielen, pos);
1937 while (pos < ielen) {
1938 if (ies[pos] == WLAN_EID_EXTENSION)
1943 if ((pos + ext) >= ielen)
1946 if (!ieee80211_id_in_list(after_ric,
1950 pos = skip_ie(ies, ielen, pos);
1955 pos = skip_ie(ies, ielen, pos);
1961 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1963 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id)
1965 unsigned int elem_len;
1970 elem_len = skb->data + skb->len - len_pos - 1;
1972 while (elem_len > 255) {
1973 /* this one is 255 */
1975 /* remaining data gets smaller */
1977 /* make space for the fragment ID/len in SKB */
1979 /* shift back the remaining data to place fragment ID/len */
1980 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
1981 /* place the fragment ID */
1984 /* and point to fragment length to update later */
1988 *len_pos = elem_len;
1990 EXPORT_SYMBOL(ieee80211_fragment_element);
1992 bool ieee80211_operating_class_to_band(u8 operating_class,
1993 enum nl80211_band *band)
1995 switch (operating_class) {
1999 *band = NL80211_BAND_5GHZ;
2003 *band = NL80211_BAND_6GHZ;
2009 *band = NL80211_BAND_2GHZ;
2012 *band = NL80211_BAND_60GHZ;
2018 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
2020 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
2024 u32 freq = chandef->center_freq1;
2026 if (freq >= 2412 && freq <= 2472) {
2027 if (chandef->width > NL80211_CHAN_WIDTH_40)
2030 /* 2.407 GHz, channels 1..13 */
2031 if (chandef->width == NL80211_CHAN_WIDTH_40) {
2032 if (freq > chandef->chan->center_freq)
2033 *op_class = 83; /* HT40+ */
2035 *op_class = 84; /* HT40- */
2044 /* channel 14 is only for IEEE 802.11b */
2045 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
2048 *op_class = 82; /* channel 14 */
2052 switch (chandef->width) {
2053 case NL80211_CHAN_WIDTH_80:
2056 case NL80211_CHAN_WIDTH_160:
2059 case NL80211_CHAN_WIDTH_80P80:
2062 case NL80211_CHAN_WIDTH_10:
2063 case NL80211_CHAN_WIDTH_5:
2064 return false; /* unsupported for now */
2070 /* 5 GHz, channels 36..48 */
2071 if (freq >= 5180 && freq <= 5240) {
2073 *op_class = vht_opclass;
2074 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2075 if (freq > chandef->chan->center_freq)
2086 /* 5 GHz, channels 52..64 */
2087 if (freq >= 5260 && freq <= 5320) {
2089 *op_class = vht_opclass;
2090 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2091 if (freq > chandef->chan->center_freq)
2102 /* 5 GHz, channels 100..144 */
2103 if (freq >= 5500 && freq <= 5720) {
2105 *op_class = vht_opclass;
2106 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2107 if (freq > chandef->chan->center_freq)
2118 /* 5 GHz, channels 149..169 */
2119 if (freq >= 5745 && freq <= 5845) {
2121 *op_class = vht_opclass;
2122 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2123 if (freq > chandef->chan->center_freq)
2127 } else if (freq <= 5805) {
2136 /* 56.16 GHz, channel 1..4 */
2137 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
2138 if (chandef->width >= NL80211_CHAN_WIDTH_40)
2145 /* not supported yet */
2148 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
2150 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
2152 switch (wdev->iftype) {
2153 case NL80211_IFTYPE_AP:
2154 case NL80211_IFTYPE_P2P_GO:
2155 WARN_ON(wdev->valid_links);
2156 return wdev->links[0].ap.beacon_interval;
2157 case NL80211_IFTYPE_MESH_POINT:
2158 return wdev->u.mesh.beacon_interval;
2159 case NL80211_IFTYPE_ADHOC:
2160 return wdev->u.ibss.beacon_interval;
2168 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
2169 u32 *beacon_int_gcd,
2170 bool *beacon_int_different)
2172 struct wireless_dev *wdev;
2174 *beacon_int_gcd = 0;
2175 *beacon_int_different = false;
2177 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
2180 /* this feature isn't supported with MLO */
2181 if (wdev->valid_links)
2184 wdev_bi = cfg80211_wdev_bi(wdev);
2189 if (!*beacon_int_gcd) {
2190 *beacon_int_gcd = wdev_bi;
2194 if (wdev_bi == *beacon_int_gcd)
2197 *beacon_int_different = true;
2198 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
2201 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
2202 if (*beacon_int_gcd)
2203 *beacon_int_different = true;
2204 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
2208 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2209 enum nl80211_iftype iftype, u32 beacon_int)
2212 * This is just a basic pre-condition check; if interface combinations
2213 * are possible the driver must already be checking those with a call
2214 * to cfg80211_check_combinations(), in which case we'll validate more
2215 * through the cfg80211_calculate_bi_data() call and code in
2216 * cfg80211_iter_combinations().
2219 if (beacon_int < 10 || beacon_int > 10000)
2225 int cfg80211_iter_combinations(struct wiphy *wiphy,
2226 struct iface_combination_params *params,
2227 void (*iter)(const struct ieee80211_iface_combination *c,
2231 const struct ieee80211_regdomain *regdom;
2232 enum nl80211_dfs_regions region = 0;
2234 int num_interfaces = 0;
2235 u32 used_iftypes = 0;
2237 bool beacon_int_different;
2240 * This is a bit strange, since the iteration used to rely only on
2241 * the data given by the driver, but here it now relies on context,
2242 * in form of the currently operating interfaces.
2243 * This is OK for all current users, and saves us from having to
2244 * push the GCD calculations into all the drivers.
2245 * In the future, this should probably rely more on data that's in
2246 * cfg80211 already - the only thing not would appear to be any new
2247 * interfaces (while being brought up) and channel/radar data.
2249 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2250 &beacon_int_gcd, &beacon_int_different);
2252 if (params->radar_detect) {
2254 regdom = rcu_dereference(cfg80211_regdomain);
2256 region = regdom->dfs_region;
2260 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2261 num_interfaces += params->iftype_num[iftype];
2262 if (params->iftype_num[iftype] > 0 &&
2263 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2264 used_iftypes |= BIT(iftype);
2267 for (i = 0; i < wiphy->n_iface_combinations; i++) {
2268 const struct ieee80211_iface_combination *c;
2269 struct ieee80211_iface_limit *limits;
2270 u32 all_iftypes = 0;
2272 c = &wiphy->iface_combinations[i];
2274 if (num_interfaces > c->max_interfaces)
2276 if (params->num_different_channels > c->num_different_channels)
2279 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2284 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2285 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2287 for (j = 0; j < c->n_limits; j++) {
2288 all_iftypes |= limits[j].types;
2289 if (!(limits[j].types & BIT(iftype)))
2291 if (limits[j].max < params->iftype_num[iftype])
2293 limits[j].max -= params->iftype_num[iftype];
2297 if (params->radar_detect !=
2298 (c->radar_detect_widths & params->radar_detect))
2301 if (params->radar_detect && c->radar_detect_regions &&
2302 !(c->radar_detect_regions & BIT(region)))
2305 /* Finally check that all iftypes that we're currently
2306 * using are actually part of this combination. If they
2307 * aren't then we can't use this combination and have
2308 * to continue to the next.
2310 if ((all_iftypes & used_iftypes) != used_iftypes)
2313 if (beacon_int_gcd) {
2314 if (c->beacon_int_min_gcd &&
2315 beacon_int_gcd < c->beacon_int_min_gcd)
2317 if (!c->beacon_int_min_gcd && beacon_int_different)
2321 /* This combination covered all interface types and
2322 * supported the requested numbers, so we're good.
2332 EXPORT_SYMBOL(cfg80211_iter_combinations);
2335 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2342 int cfg80211_check_combinations(struct wiphy *wiphy,
2343 struct iface_combination_params *params)
2347 err = cfg80211_iter_combinations(wiphy, params,
2348 cfg80211_iter_sum_ifcombs, &num);
2356 EXPORT_SYMBOL(cfg80211_check_combinations);
2358 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2359 const u8 *rates, unsigned int n_rates,
2367 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2372 for (i = 0; i < n_rates; i++) {
2373 int rate = (rates[i] & 0x7f) * 5;
2376 for (j = 0; j < sband->n_bitrates; j++) {
2377 if (sband->bitrates[j].bitrate == rate) {
2388 * mask must have at least one bit set here since we
2389 * didn't accept a 0-length rates array nor allowed
2390 * entries in the array that didn't exist
2396 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2398 enum nl80211_band band;
2399 unsigned int n_channels = 0;
2401 for (band = 0; band < NUM_NL80211_BANDS; band++)
2402 if (wiphy->bands[band])
2403 n_channels += wiphy->bands[band]->n_channels;
2407 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2409 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2410 struct station_info *sinfo)
2412 struct cfg80211_registered_device *rdev;
2413 struct wireless_dev *wdev;
2415 wdev = dev->ieee80211_ptr;
2419 rdev = wiphy_to_rdev(wdev->wiphy);
2420 if (!rdev->ops->get_station)
2423 memset(sinfo, 0, sizeof(*sinfo));
2425 return rdev_get_station(rdev, dev, mac_addr, sinfo);
2427 EXPORT_SYMBOL(cfg80211_get_station);
2429 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2436 kfree(f->serv_spec_info);
2439 for (i = 0; i < f->num_rx_filters; i++)
2440 kfree(f->rx_filters[i].filter);
2442 for (i = 0; i < f->num_tx_filters; i++)
2443 kfree(f->tx_filters[i].filter);
2445 kfree(f->rx_filters);
2446 kfree(f->tx_filters);
2449 EXPORT_SYMBOL(cfg80211_free_nan_func);
2451 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2452 u32 center_freq_khz, u32 bw_khz)
2454 u32 start_freq_khz, end_freq_khz;
2456 start_freq_khz = center_freq_khz - (bw_khz / 2);
2457 end_freq_khz = center_freq_khz + (bw_khz / 2);
2459 if (start_freq_khz >= freq_range->start_freq_khz &&
2460 end_freq_khz <= freq_range->end_freq_khz)
2466 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2468 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2469 sizeof(*(sinfo->pertid)),
2476 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2478 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2479 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2480 const unsigned char rfc1042_header[] __aligned(2) =
2481 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2482 EXPORT_SYMBOL(rfc1042_header);
2484 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2485 const unsigned char bridge_tunnel_header[] __aligned(2) =
2486 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2487 EXPORT_SYMBOL(bridge_tunnel_header);
2489 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2490 struct iapp_layer2_update {
2491 u8 da[ETH_ALEN]; /* broadcast */
2492 u8 sa[ETH_ALEN]; /* STA addr */
2500 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2502 struct iapp_layer2_update *msg;
2503 struct sk_buff *skb;
2505 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2508 skb = dev_alloc_skb(sizeof(*msg));
2511 msg = skb_put(skb, sizeof(*msg));
2513 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2514 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2516 eth_broadcast_addr(msg->da);
2517 ether_addr_copy(msg->sa, addr);
2518 msg->len = htons(6);
2520 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2521 msg->control = 0xaf; /* XID response lsb.1111F101.
2522 * F=0 (no poll command; unsolicited frame) */
2523 msg->xid_info[0] = 0x81; /* XID format identifier */
2524 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2525 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2528 skb->protocol = eth_type_trans(skb, dev);
2529 memset(skb->cb, 0, sizeof(skb->cb));
2532 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2534 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2535 enum ieee80211_vht_chanwidth bw,
2536 int mcs, bool ext_nss_bw_capable,
2537 unsigned int max_vht_nss)
2539 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2542 int i, mcs_encoding;
2547 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2557 /* find max_vht_nss for the given MCS */
2558 for (i = 7; i >= 0; i--) {
2559 int supp = (map >> (2 * i)) & 3;
2564 if (supp >= mcs_encoding) {
2565 max_vht_nss = i + 1;
2571 if (!(cap->supp_mcs.tx_mcs_map &
2572 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2575 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2576 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2577 supp_width = le32_get_bits(cap->vht_cap_info,
2578 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2580 /* if not capable, treat ext_nss_bw as 0 */
2581 if (!ext_nss_bw_capable)
2584 /* This is invalid */
2585 if (supp_width == 3)
2588 /* This is an invalid combination so pretend nothing is supported */
2589 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2593 * Cover all the special cases according to IEEE 802.11-2016
2594 * Table 9-250. All other cases are either factor of 1 or not
2598 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2599 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2600 if ((supp_width == 1 || supp_width == 2) &&
2602 return 2 * max_vht_nss;
2604 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2605 if (supp_width == 0 &&
2606 (ext_nss_bw == 1 || ext_nss_bw == 2))
2607 return max_vht_nss / 2;
2608 if (supp_width == 0 &&
2610 return (3 * max_vht_nss) / 4;
2611 if (supp_width == 1 &&
2613 return 2 * max_vht_nss;
2615 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2616 if (supp_width == 0 && ext_nss_bw == 1)
2617 return 0; /* not possible */
2618 if (supp_width == 0 &&
2620 return max_vht_nss / 2;
2621 if (supp_width == 0 &&
2623 return (3 * max_vht_nss) / 4;
2624 if (supp_width == 1 &&
2626 return 0; /* not possible */
2627 if (supp_width == 1 &&
2629 return max_vht_nss / 2;
2630 if (supp_width == 1 &&
2632 return (3 * max_vht_nss) / 4;
2636 /* not covered or invalid combination received */
2639 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2641 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2642 bool is_4addr, u8 check_swif)
2645 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2647 switch (check_swif) {
2649 if (is_vlan && is_4addr)
2650 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2651 return wiphy->interface_modes & BIT(iftype);
2653 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2654 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2655 return wiphy->software_iftypes & BIT(iftype);
2662 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2664 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2666 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2668 lockdep_assert_wiphy(wdev->wiphy);
2670 switch (wdev->iftype) {
2671 case NL80211_IFTYPE_AP:
2672 case NL80211_IFTYPE_P2P_GO:
2673 cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2676 /* per-link not relevant */
2680 wdev->valid_links &= ~BIT(link_id);
2682 rdev_del_intf_link(rdev, wdev, link_id);
2684 eth_zero_addr(wdev->links[link_id].addr);
2687 void cfg80211_remove_links(struct wireless_dev *wdev)
2689 unsigned int link_id;
2692 * links are controlled by upper layers (userspace/cfg)
2693 * only for AP mode, so only remove them here for AP
2695 if (wdev->iftype != NL80211_IFTYPE_AP)
2698 if (wdev->valid_links) {
2699 for_each_valid_link(wdev, link_id)
2700 cfg80211_remove_link(wdev, link_id);
2704 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2705 struct wireless_dev *wdev)
2707 cfg80211_remove_links(wdev);
2709 return rdev_del_virtual_intf(rdev, wdev);
2712 const struct wiphy_iftype_ext_capab *
2713 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2717 for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2718 if (wiphy->iftype_ext_capab[i].iftype == type)
2719 return &wiphy->iftype_ext_capab[i];
2724 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);