1 // SPDX-License-Identifier: GPL-2.0
3 * Wireless utility functions
5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2017 Intel Deutschland GmbH
8 * Copyright (C) 2018-2020 Intel Corporation
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
27 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,
47 enum nl80211_bss_scan_width scan_width)
49 struct ieee80211_rate *bitrates;
50 u32 mandatory_rates = 0;
51 enum ieee80211_rate_flags mandatory_flag;
57 if (sband->band == NL80211_BAND_2GHZ) {
58 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
59 scan_width == NL80211_BSS_CHAN_WIDTH_10)
60 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
62 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
64 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
67 bitrates = sband->bitrates;
68 for (i = 0; i < sband->n_bitrates; i++)
69 if (bitrates[i].flags & mandatory_flag)
70 mandatory_rates |= BIT(i);
71 return mandatory_rates;
73 EXPORT_SYMBOL(ieee80211_mandatory_rates);
75 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
77 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
80 return 0; /* not supported */
82 case NL80211_BAND_2GHZ:
84 return MHZ_TO_KHZ(2484);
86 return MHZ_TO_KHZ(2407 + chan * 5);
88 case NL80211_BAND_5GHZ:
89 if (chan >= 182 && chan <= 196)
90 return MHZ_TO_KHZ(4000 + chan * 5);
92 return MHZ_TO_KHZ(5000 + chan * 5);
94 case NL80211_BAND_6GHZ:
95 /* see 802.11ax D6.1 27.3.23.2 */
97 return MHZ_TO_KHZ(5935);
99 return MHZ_TO_KHZ(5950 + chan * 5);
101 case NL80211_BAND_60GHZ:
103 return MHZ_TO_KHZ(56160 + chan * 2160);
105 case NL80211_BAND_S1GHZ:
106 return 902000 + chan * 500;
110 return 0; /* not supported */
112 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
114 enum nl80211_chan_width
115 ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
117 if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
118 return NL80211_CHAN_WIDTH_20_NOHT;
120 /*S1G defines a single allowed channel width per channel.
121 * Extract that width here.
123 if (chan->flags & IEEE80211_CHAN_1MHZ)
124 return NL80211_CHAN_WIDTH_1;
125 else if (chan->flags & IEEE80211_CHAN_2MHZ)
126 return NL80211_CHAN_WIDTH_2;
127 else if (chan->flags & IEEE80211_CHAN_4MHZ)
128 return NL80211_CHAN_WIDTH_4;
129 else if (chan->flags & IEEE80211_CHAN_8MHZ)
130 return NL80211_CHAN_WIDTH_8;
131 else if (chan->flags & IEEE80211_CHAN_16MHZ)
132 return NL80211_CHAN_WIDTH_16;
134 pr_err("unknown channel width for channel at %dKHz?\n",
135 ieee80211_channel_to_khz(chan));
137 return NL80211_CHAN_WIDTH_1;
139 EXPORT_SYMBOL(ieee80211_s1g_channel_width);
141 int ieee80211_freq_khz_to_channel(u32 freq)
143 /* TODO: just handle MHz for now */
144 freq = KHZ_TO_MHZ(freq);
146 /* see 802.11 17.3.8.3.2 and Annex J */
149 else if (freq < 2484)
150 return (freq - 2407) / 5;
151 else if (freq >= 4910 && freq <= 4980)
152 return (freq - 4000) / 5;
153 else if (freq < 5925)
154 return (freq - 5000) / 5;
155 else if (freq == 5935)
157 else if (freq <= 45000) /* DMG band lower limit */
158 /* see 802.11ax D6.1 27.3.22.2 */
159 return (freq - 5950) / 5;
160 else if (freq >= 58320 && freq <= 70200)
161 return (freq - 56160) / 2160;
165 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
167 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
170 enum nl80211_band band;
171 struct ieee80211_supported_band *sband;
174 for (band = 0; band < NUM_NL80211_BANDS; band++) {
175 sband = wiphy->bands[band];
180 for (i = 0; i < sband->n_channels; i++) {
181 struct ieee80211_channel *chan = &sband->channels[i];
183 if (ieee80211_channel_to_khz(chan) == freq)
190 EXPORT_SYMBOL(ieee80211_get_channel_khz);
192 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
196 switch (sband->band) {
197 case NL80211_BAND_5GHZ:
198 case NL80211_BAND_6GHZ:
200 for (i = 0; i < sband->n_bitrates; i++) {
201 if (sband->bitrates[i].bitrate == 60 ||
202 sband->bitrates[i].bitrate == 120 ||
203 sband->bitrates[i].bitrate == 240) {
204 sband->bitrates[i].flags |=
205 IEEE80211_RATE_MANDATORY_A;
211 case NL80211_BAND_2GHZ:
213 for (i = 0; i < sband->n_bitrates; i++) {
214 switch (sband->bitrates[i].bitrate) {
219 sband->bitrates[i].flags |=
220 IEEE80211_RATE_MANDATORY_B |
221 IEEE80211_RATE_MANDATORY_G;
227 sband->bitrates[i].flags |=
228 IEEE80211_RATE_MANDATORY_G;
232 sband->bitrates[i].flags |=
233 IEEE80211_RATE_ERP_G;
237 WARN_ON(want != 0 && want != 3);
239 case NL80211_BAND_60GHZ:
240 /* check for mandatory HT MCS 1..4 */
241 WARN_ON(!sband->ht_cap.ht_supported);
242 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
244 case NL80211_BAND_S1GHZ:
245 /* Figure 9-589bd: 3 means unsupported, so != 3 means at least
248 WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
250 case NUM_NL80211_BANDS:
257 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
259 enum nl80211_band band;
261 for (band = 0; band < NUM_NL80211_BANDS; band++)
262 if (wiphy->bands[band])
263 set_mandatory_flags_band(wiphy->bands[band]);
266 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
269 for (i = 0; i < wiphy->n_cipher_suites; i++)
270 if (cipher == wiphy->cipher_suites[i])
276 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
278 struct wiphy *wiphy = &rdev->wiphy;
281 for (i = 0; i < wiphy->n_cipher_suites; i++) {
282 switch (wiphy->cipher_suites[i]) {
283 case WLAN_CIPHER_SUITE_AES_CMAC:
284 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
285 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
286 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
294 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
295 int key_idx, bool pairwise)
301 else if (wiphy_ext_feature_isset(&rdev->wiphy,
302 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
303 wiphy_ext_feature_isset(&rdev->wiphy,
304 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
306 else if (cfg80211_igtk_cipher_supported(rdev))
311 if (key_idx < 0 || key_idx > max_key_idx)
317 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
318 struct key_params *params, int key_idx,
319 bool pairwise, const u8 *mac_addr)
321 if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
324 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
327 if (pairwise && !mac_addr)
330 switch (params->cipher) {
331 case WLAN_CIPHER_SUITE_TKIP:
332 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
333 if ((pairwise && key_idx) ||
334 params->mode != NL80211_KEY_RX_TX)
337 case WLAN_CIPHER_SUITE_CCMP:
338 case WLAN_CIPHER_SUITE_CCMP_256:
339 case WLAN_CIPHER_SUITE_GCMP:
340 case WLAN_CIPHER_SUITE_GCMP_256:
341 /* IEEE802.11-2016 allows only 0 and - when supporting
342 * Extended Key ID - 1 as index for pairwise keys.
343 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
344 * the driver supports Extended Key ID.
345 * @NL80211_KEY_SET_TX can't be set when installing and
348 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
349 params->mode == NL80211_KEY_SET_TX)
351 if (wiphy_ext_feature_isset(&rdev->wiphy,
352 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
353 if (pairwise && (key_idx < 0 || key_idx > 1))
355 } else if (pairwise && key_idx) {
359 case WLAN_CIPHER_SUITE_AES_CMAC:
360 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
361 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
362 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
363 /* Disallow BIP (group-only) cipher as pairwise cipher */
369 case WLAN_CIPHER_SUITE_WEP40:
370 case WLAN_CIPHER_SUITE_WEP104:
378 switch (params->cipher) {
379 case WLAN_CIPHER_SUITE_WEP40:
380 if (params->key_len != WLAN_KEY_LEN_WEP40)
383 case WLAN_CIPHER_SUITE_TKIP:
384 if (params->key_len != WLAN_KEY_LEN_TKIP)
387 case WLAN_CIPHER_SUITE_CCMP:
388 if (params->key_len != WLAN_KEY_LEN_CCMP)
391 case WLAN_CIPHER_SUITE_CCMP_256:
392 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
395 case WLAN_CIPHER_SUITE_GCMP:
396 if (params->key_len != WLAN_KEY_LEN_GCMP)
399 case WLAN_CIPHER_SUITE_GCMP_256:
400 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
403 case WLAN_CIPHER_SUITE_WEP104:
404 if (params->key_len != WLAN_KEY_LEN_WEP104)
407 case WLAN_CIPHER_SUITE_AES_CMAC:
408 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
411 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
412 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
415 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
416 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
419 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
420 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
425 * We don't know anything about this algorithm,
426 * allow using it -- but the driver must check
427 * all parameters! We still check below whether
428 * or not the driver supports this algorithm,
435 switch (params->cipher) {
436 case WLAN_CIPHER_SUITE_WEP40:
437 case WLAN_CIPHER_SUITE_WEP104:
438 /* These ciphers do not use key sequence */
440 case WLAN_CIPHER_SUITE_TKIP:
441 case WLAN_CIPHER_SUITE_CCMP:
442 case WLAN_CIPHER_SUITE_CCMP_256:
443 case WLAN_CIPHER_SUITE_GCMP:
444 case WLAN_CIPHER_SUITE_GCMP_256:
445 case WLAN_CIPHER_SUITE_AES_CMAC:
446 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
447 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
448 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
449 if (params->seq_len != 6)
455 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
461 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
463 unsigned int hdrlen = 24;
465 if (ieee80211_is_ext(fc)) {
470 if (ieee80211_is_data(fc)) {
471 if (ieee80211_has_a4(fc))
473 if (ieee80211_is_data_qos(fc)) {
474 hdrlen += IEEE80211_QOS_CTL_LEN;
475 if (ieee80211_has_order(fc))
476 hdrlen += IEEE80211_HT_CTL_LEN;
481 if (ieee80211_is_mgmt(fc)) {
482 if (ieee80211_has_order(fc))
483 hdrlen += IEEE80211_HT_CTL_LEN;
487 if (ieee80211_is_ctl(fc)) {
489 * ACK and CTS are 10 bytes, all others 16. To see how
490 * to get this condition consider
491 * subtype mask: 0b0000000011110000 (0x00F0)
492 * ACK subtype: 0b0000000011010000 (0x00D0)
493 * CTS subtype: 0b0000000011000000 (0x00C0)
494 * bits that matter: ^^^ (0x00E0)
495 * value of those: 0b0000000011000000 (0x00C0)
497 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
505 EXPORT_SYMBOL(ieee80211_hdrlen);
507 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
509 const struct ieee80211_hdr *hdr =
510 (const struct ieee80211_hdr *)skb->data;
513 if (unlikely(skb->len < 10))
515 hdrlen = ieee80211_hdrlen(hdr->frame_control);
516 if (unlikely(hdrlen > skb->len))
520 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
522 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
524 int ae = flags & MESH_FLAGS_AE;
525 /* 802.11-2012, 8.2.4.7.3 */
530 case MESH_FLAGS_AE_A4:
532 case MESH_FLAGS_AE_A5_A6:
537 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
539 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
541 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
543 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
544 const u8 *addr, enum nl80211_iftype iftype,
545 u8 data_offset, bool is_amsdu)
547 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
549 u8 hdr[ETH_ALEN] __aligned(2);
556 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
559 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
560 if (skb->len < hdrlen + 8)
563 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
565 * IEEE 802.11 address fields:
566 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
567 * 0 0 DA SA BSSID n/a
568 * 0 1 DA BSSID SA n/a
569 * 1 0 BSSID SA DA n/a
572 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
573 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
575 if (iftype == NL80211_IFTYPE_MESH_POINT)
576 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
578 mesh_flags &= MESH_FLAGS_AE;
580 switch (hdr->frame_control &
581 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
582 case cpu_to_le16(IEEE80211_FCTL_TODS):
583 if (unlikely(iftype != NL80211_IFTYPE_AP &&
584 iftype != NL80211_IFTYPE_AP_VLAN &&
585 iftype != NL80211_IFTYPE_P2P_GO))
588 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
589 if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
590 iftype != NL80211_IFTYPE_AP_VLAN &&
591 iftype != NL80211_IFTYPE_STATION))
593 if (iftype == NL80211_IFTYPE_MESH_POINT) {
594 if (mesh_flags == MESH_FLAGS_AE_A4)
596 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
597 skb_copy_bits(skb, hdrlen +
598 offsetof(struct ieee80211s_hdr, eaddr1),
599 tmp.h_dest, 2 * ETH_ALEN);
601 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
604 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
605 if ((iftype != NL80211_IFTYPE_STATION &&
606 iftype != NL80211_IFTYPE_P2P_CLIENT &&
607 iftype != NL80211_IFTYPE_MESH_POINT) ||
608 (is_multicast_ether_addr(tmp.h_dest) &&
609 ether_addr_equal(tmp.h_source, addr)))
611 if (iftype == NL80211_IFTYPE_MESH_POINT) {
612 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
614 if (mesh_flags == MESH_FLAGS_AE_A4)
615 skb_copy_bits(skb, hdrlen +
616 offsetof(struct ieee80211s_hdr, eaddr1),
617 tmp.h_source, ETH_ALEN);
618 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
622 if (iftype != NL80211_IFTYPE_ADHOC &&
623 iftype != NL80211_IFTYPE_STATION &&
624 iftype != NL80211_IFTYPE_OCB)
629 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
630 tmp.h_proto = payload.proto;
632 if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
633 tmp.h_proto != htons(ETH_P_AARP) &&
634 tmp.h_proto != htons(ETH_P_IPX)) ||
635 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
636 /* remove RFC1042 or Bridge-Tunnel encapsulation and
637 * replace EtherType */
638 hdrlen += ETH_ALEN + 2;
640 tmp.h_proto = htons(skb->len - hdrlen);
642 pskb_pull(skb, hdrlen);
645 ehdr = skb_push(skb, sizeof(struct ethhdr));
646 memcpy(ehdr, &tmp, sizeof(tmp));
650 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
653 __frame_add_frag(struct sk_buff *skb, struct page *page,
654 void *ptr, int len, int size)
656 struct skb_shared_info *sh = skb_shinfo(skb);
660 page_offset = ptr - page_address(page);
661 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
665 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
668 struct skb_shared_info *sh = skb_shinfo(skb);
669 const skb_frag_t *frag = &sh->frags[0];
670 struct page *frag_page;
672 int frag_len, frag_size;
673 int head_size = skb->len - skb->data_len;
676 frag_page = virt_to_head_page(skb->head);
677 frag_ptr = skb->data;
678 frag_size = head_size;
680 while (offset >= frag_size) {
682 frag_page = skb_frag_page(frag);
683 frag_ptr = skb_frag_address(frag);
684 frag_size = skb_frag_size(frag);
689 frag_len = frag_size - offset;
691 cur_len = min(len, frag_len);
693 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
697 frag_len = skb_frag_size(frag);
698 cur_len = min(len, frag_len);
699 __frame_add_frag(frame, skb_frag_page(frag),
700 skb_frag_address(frag), cur_len, frag_len);
706 static struct sk_buff *
707 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
708 int offset, int len, bool reuse_frag)
710 struct sk_buff *frame;
713 if (skb->len - offset < len)
717 * When reusing framents, copy some data to the head to simplify
718 * ethernet header handling and speed up protocol header processing
719 * in the stack later.
722 cur_len = min_t(int, len, 32);
725 * Allocate and reserve two bytes more for payload
726 * alignment since sizeof(struct ethhdr) is 14.
728 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
732 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
733 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
740 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
745 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
746 const u8 *addr, enum nl80211_iftype iftype,
747 const unsigned int extra_headroom,
748 const u8 *check_da, const u8 *check_sa)
750 unsigned int hlen = ALIGN(extra_headroom, 4);
751 struct sk_buff *frame = NULL;
754 int offset = 0, remaining;
756 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
757 bool reuse_skb = false;
761 unsigned int subframe_len;
765 skb_copy_bits(skb, offset, ð, sizeof(eth));
766 len = ntohs(eth.h_proto);
767 subframe_len = sizeof(struct ethhdr) + len;
768 padding = (4 - subframe_len) & 0x3;
770 /* the last MSDU has no padding */
771 remaining = skb->len - offset;
772 if (subframe_len > remaining)
774 /* mitigate A-MSDU aggregation injection attacks */
775 if (ether_addr_equal(eth.h_dest, rfc1042_header))
778 offset += sizeof(struct ethhdr);
779 last = remaining <= subframe_len + padding;
781 /* FIXME: should we really accept multicast DA? */
782 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
783 !ether_addr_equal(check_da, eth.h_dest)) ||
784 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
785 offset += len + padding;
789 /* reuse skb for the last subframe */
790 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
791 skb_pull(skb, offset);
795 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
800 offset += len + padding;
803 skb_reset_network_header(frame);
804 frame->dev = skb->dev;
805 frame->priority = skb->priority;
807 payload = frame->data;
808 ethertype = (payload[6] << 8) | payload[7];
809 if (likely((ether_addr_equal(payload, rfc1042_header) &&
810 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
811 ether_addr_equal(payload, bridge_tunnel_header))) {
812 eth.h_proto = htons(ethertype);
813 skb_pull(frame, ETH_ALEN + 2);
816 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
817 __skb_queue_tail(list, frame);
826 __skb_queue_purge(list);
829 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
831 /* Given a data frame determine the 802.1p/1d tag to use. */
832 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
833 struct cfg80211_qos_map *qos_map)
836 unsigned char vlan_priority;
839 /* skb->priority values from 256->263 are magic values to
840 * directly indicate a specific 802.1d priority. This is used
841 * to allow 802.1d priority to be passed directly in from VLAN
844 if (skb->priority >= 256 && skb->priority <= 263) {
845 ret = skb->priority - 256;
849 if (skb_vlan_tag_present(skb)) {
850 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
852 if (vlan_priority > 0) {
858 switch (skb->protocol) {
859 case htons(ETH_P_IP):
860 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
862 case htons(ETH_P_IPV6):
863 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
865 case htons(ETH_P_MPLS_UC):
866 case htons(ETH_P_MPLS_MC): {
867 struct mpls_label mpls_tmp, *mpls;
869 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
870 sizeof(*mpls), &mpls_tmp);
874 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
878 case htons(ETH_P_80221):
879 /* 802.21 is always network control traffic */
886 unsigned int i, tmp_dscp = dscp >> 2;
888 for (i = 0; i < qos_map->num_des; i++) {
889 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
890 ret = qos_map->dscp_exception[i].up;
895 for (i = 0; i < 8; i++) {
896 if (tmp_dscp >= qos_map->up[i].low &&
897 tmp_dscp <= qos_map->up[i].high) {
906 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
908 EXPORT_SYMBOL(cfg80211_classify8021d);
910 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
912 const struct cfg80211_bss_ies *ies;
914 ies = rcu_dereference(bss->ies);
918 return cfg80211_find_elem(id, ies->data, ies->len);
920 EXPORT_SYMBOL(ieee80211_bss_get_elem);
922 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
924 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
925 struct net_device *dev = wdev->netdev;
928 if (!wdev->connect_keys)
931 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
932 if (!wdev->connect_keys->params[i].cipher)
934 if (rdev_add_key(rdev, dev, i, false, NULL,
935 &wdev->connect_keys->params[i])) {
936 netdev_err(dev, "failed to set key %d\n", i);
939 if (wdev->connect_keys->def == i &&
940 rdev_set_default_key(rdev, dev, i, true, true)) {
941 netdev_err(dev, "failed to set defkey %d\n", i);
946 kfree_sensitive(wdev->connect_keys);
947 wdev->connect_keys = NULL;
950 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
952 struct cfg80211_event *ev;
955 spin_lock_irqsave(&wdev->event_lock, flags);
956 while (!list_empty(&wdev->event_list)) {
957 ev = list_first_entry(&wdev->event_list,
958 struct cfg80211_event, list);
960 spin_unlock_irqrestore(&wdev->event_lock, flags);
964 case EVENT_CONNECT_RESULT:
965 __cfg80211_connect_result(
968 ev->cr.status == WLAN_STATUS_SUCCESS);
971 __cfg80211_roamed(wdev, &ev->rm);
973 case EVENT_DISCONNECTED:
974 __cfg80211_disconnected(wdev->netdev,
975 ev->dc.ie, ev->dc.ie_len,
977 !ev->dc.locally_generated);
979 case EVENT_IBSS_JOINED:
980 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
984 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
986 case EVENT_PORT_AUTHORIZED:
987 __cfg80211_port_authorized(wdev, ev->pa.bssid);
994 spin_lock_irqsave(&wdev->event_lock, flags);
996 spin_unlock_irqrestore(&wdev->event_lock, flags);
999 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1001 struct wireless_dev *wdev;
1003 lockdep_assert_held(&rdev->wiphy.mtx);
1005 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1006 cfg80211_process_wdev_events(wdev);
1009 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1010 struct net_device *dev, enum nl80211_iftype ntype,
1011 struct vif_params *params)
1014 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1016 lockdep_assert_held(&rdev->wiphy.mtx);
1018 /* don't support changing VLANs, you just re-create them */
1019 if (otype == NL80211_IFTYPE_AP_VLAN)
1022 /* cannot change into P2P device or NAN */
1023 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1024 ntype == NL80211_IFTYPE_NAN)
1027 if (!rdev->ops->change_virtual_intf ||
1028 !(rdev->wiphy.interface_modes & (1 << ntype)))
1031 if (ntype != otype) {
1032 /* if it's part of a bridge, reject changing type to station/ibss */
1033 if (netif_is_bridge_port(dev) &&
1034 (ntype == NL80211_IFTYPE_ADHOC ||
1035 ntype == NL80211_IFTYPE_STATION ||
1036 ntype == NL80211_IFTYPE_P2P_CLIENT))
1039 dev->ieee80211_ptr->use_4addr = false;
1040 dev->ieee80211_ptr->mesh_id_up_len = 0;
1041 wdev_lock(dev->ieee80211_ptr);
1042 rdev_set_qos_map(rdev, dev, NULL);
1043 wdev_unlock(dev->ieee80211_ptr);
1046 case NL80211_IFTYPE_AP:
1047 case NL80211_IFTYPE_P2P_GO:
1048 cfg80211_stop_ap(rdev, dev, true);
1050 case NL80211_IFTYPE_ADHOC:
1051 cfg80211_leave_ibss(rdev, dev, false);
1053 case NL80211_IFTYPE_STATION:
1054 case NL80211_IFTYPE_P2P_CLIENT:
1055 wdev_lock(dev->ieee80211_ptr);
1056 cfg80211_disconnect(rdev, dev,
1057 WLAN_REASON_DEAUTH_LEAVING, true);
1058 wdev_unlock(dev->ieee80211_ptr);
1060 case NL80211_IFTYPE_MESH_POINT:
1061 /* mesh should be handled? */
1063 case NL80211_IFTYPE_OCB:
1064 cfg80211_leave_ocb(rdev, dev);
1070 cfg80211_process_rdev_events(rdev);
1071 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1074 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1076 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1078 if (!err && params && params->use_4addr != -1)
1079 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1082 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1084 case NL80211_IFTYPE_STATION:
1085 if (dev->ieee80211_ptr->use_4addr)
1088 case NL80211_IFTYPE_OCB:
1089 case NL80211_IFTYPE_P2P_CLIENT:
1090 case NL80211_IFTYPE_ADHOC:
1091 dev->priv_flags |= IFF_DONT_BRIDGE;
1093 case NL80211_IFTYPE_P2P_GO:
1094 case NL80211_IFTYPE_AP:
1095 case NL80211_IFTYPE_AP_VLAN:
1096 case NL80211_IFTYPE_MESH_POINT:
1099 case NL80211_IFTYPE_MONITOR:
1100 /* monitor can't bridge anyway */
1102 case NL80211_IFTYPE_UNSPECIFIED:
1103 case NUM_NL80211_IFTYPES:
1106 case NL80211_IFTYPE_P2P_DEVICE:
1107 case NL80211_IFTYPE_WDS:
1108 case NL80211_IFTYPE_NAN:
1114 if (!err && ntype != otype && netif_running(dev)) {
1115 cfg80211_update_iface_num(rdev, ntype, 1);
1116 cfg80211_update_iface_num(rdev, otype, -1);
1122 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1124 int modulation, streams, bitrate;
1126 /* the formula below does only work for MCS values smaller than 32 */
1127 if (WARN_ON_ONCE(rate->mcs >= 32))
1130 modulation = rate->mcs & 7;
1131 streams = (rate->mcs >> 3) + 1;
1133 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1136 bitrate *= (modulation + 1);
1137 else if (modulation == 4)
1138 bitrate *= (modulation + 2);
1140 bitrate *= (modulation + 3);
1144 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1145 bitrate = (bitrate / 9) * 10;
1147 /* do NOT round down here */
1148 return (bitrate + 50000) / 100000;
1151 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1153 static const u32 __mcs2bitrate[] = {
1161 [5] = 12512, /* 1251.25 mbps */
1171 [14] = 8662, /* 866.25 mbps */
1181 [24] = 67568, /* 6756.75 mbps */
1192 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1195 return __mcs2bitrate[rate->mcs];
1198 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1200 static const u32 __mcs2bitrate[] = {
1201 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1202 [7 - 6] = 50050, /* MCS 12.1 */
1210 /* Extended SC MCS not defined for base MCS below 6 or above 12 */
1211 if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1214 return __mcs2bitrate[rate->mcs - 6];
1217 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1219 static const u32 __mcs2bitrate[] = {
1227 [5] = 12512, /* 1251.25 mbps */
1245 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1248 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1251 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1253 static const u32 base[4][12] = {
1263 /* not in the spec, but some devices use this: */
1315 case RATE_INFO_BW_160:
1318 case RATE_INFO_BW_80:
1321 case RATE_INFO_BW_40:
1324 case RATE_INFO_BW_5:
1325 case RATE_INFO_BW_10:
1328 case RATE_INFO_BW_20:
1332 bitrate = base[idx][rate->mcs];
1333 bitrate *= rate->nss;
1335 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1336 bitrate = (bitrate / 9) * 10;
1338 /* do NOT round down here */
1339 return (bitrate + 50000) / 100000;
1341 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1342 rate->bw, rate->mcs, rate->nss);
1346 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1349 u32 mcs_divisors[14] = {
1350 102399, /* 16.666666... */
1351 51201, /* 8.333333... */
1352 34134, /* 5.555555... */
1353 25599, /* 4.166666... */
1354 17067, /* 2.777777... */
1355 12801, /* 2.083333... */
1356 11377, /* 1.851725... */
1357 10239, /* 1.666666... */
1358 8532, /* 1.388888... */
1359 7680, /* 1.250000... */
1360 6828, /* 1.111111... */
1361 6144, /* 1.000000... */
1362 5690, /* 0.926106... */
1363 5120, /* 0.833333... */
1365 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1366 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1367 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1368 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1369 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1370 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1371 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1375 if (WARN_ON_ONCE(rate->mcs > 13))
1378 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1380 if (WARN_ON_ONCE(rate->he_ru_alloc >
1381 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1383 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1386 if (rate->bw == RATE_INFO_BW_160)
1387 result = rates_160M[rate->he_gi];
1388 else if (rate->bw == RATE_INFO_BW_80 ||
1389 (rate->bw == RATE_INFO_BW_HE_RU &&
1390 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1391 result = rates_969[rate->he_gi];
1392 else if (rate->bw == RATE_INFO_BW_40 ||
1393 (rate->bw == RATE_INFO_BW_HE_RU &&
1394 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1395 result = rates_484[rate->he_gi];
1396 else if (rate->bw == RATE_INFO_BW_20 ||
1397 (rate->bw == RATE_INFO_BW_HE_RU &&
1398 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1399 result = rates_242[rate->he_gi];
1400 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1401 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1402 result = rates_106[rate->he_gi];
1403 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1404 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1405 result = rates_52[rate->he_gi];
1406 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1407 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1408 result = rates_26[rate->he_gi];
1410 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1411 rate->bw, rate->he_ru_alloc);
1415 /* now scale to the appropriate MCS */
1418 do_div(tmp, mcs_divisors[rate->mcs]);
1421 /* and take NSS, DCM into account */
1422 result = (result * rate->nss) / 8;
1426 return result / 10000;
1429 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1431 if (rate->flags & RATE_INFO_FLAGS_MCS)
1432 return cfg80211_calculate_bitrate_ht(rate);
1433 if (rate->flags & RATE_INFO_FLAGS_DMG)
1434 return cfg80211_calculate_bitrate_dmg(rate);
1435 if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1436 return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1437 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1438 return cfg80211_calculate_bitrate_edmg(rate);
1439 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1440 return cfg80211_calculate_bitrate_vht(rate);
1441 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1442 return cfg80211_calculate_bitrate_he(rate);
1444 return rate->legacy;
1446 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1448 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1449 enum ieee80211_p2p_attr_id attr,
1450 u8 *buf, unsigned int bufsize)
1453 u16 attr_remaining = 0;
1454 bool desired_attr = false;
1455 u16 desired_len = 0;
1458 unsigned int iedatalen;
1465 if (iedatalen + 2 > len)
1468 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1476 /* check WFA OUI, P2P subtype */
1477 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1478 iedata[2] != 0x9a || iedata[3] != 0x09)
1484 /* check attribute continuation into this IE */
1485 copy = min_t(unsigned int, attr_remaining, iedatalen);
1486 if (copy && desired_attr) {
1487 desired_len += copy;
1489 memcpy(out, iedata, min(bufsize, copy));
1490 out += min(bufsize, copy);
1491 bufsize -= min(bufsize, copy);
1495 if (copy == attr_remaining)
1499 attr_remaining -= copy;
1506 while (iedatalen > 0) {
1509 /* P2P attribute ID & size must fit */
1512 desired_attr = iedata[0] == attr;
1513 attr_len = get_unaligned_le16(iedata + 1);
1517 copy = min_t(unsigned int, attr_len, iedatalen);
1520 desired_len += copy;
1522 memcpy(out, iedata, min(bufsize, copy));
1523 out += min(bufsize, copy);
1524 bufsize -= min(bufsize, copy);
1527 if (copy == attr_len)
1533 attr_remaining = attr_len - copy;
1541 if (attr_remaining && desired_attr)
1546 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1548 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1552 /* Make sure array values are legal */
1553 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1558 if (ids[i] == WLAN_EID_EXTENSION) {
1559 if (id_ext && (ids[i + 1] == id))
1566 if (ids[i] == id && !id_ext)
1574 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1576 /* we assume a validly formed IEs buffer */
1577 u8 len = ies[pos + 1];
1581 /* the IE itself must have 255 bytes for fragments to follow */
1585 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1593 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1594 const u8 *ids, int n_ids,
1595 const u8 *after_ric, int n_after_ric,
1598 size_t pos = offset;
1600 while (pos < ielen) {
1603 if (ies[pos] == WLAN_EID_EXTENSION)
1605 if ((pos + ext) >= ielen)
1608 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1609 ies[pos] == WLAN_EID_EXTENSION))
1612 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1613 pos = skip_ie(ies, ielen, pos);
1615 while (pos < ielen) {
1616 if (ies[pos] == WLAN_EID_EXTENSION)
1621 if ((pos + ext) >= ielen)
1624 if (!ieee80211_id_in_list(after_ric,
1628 pos = skip_ie(ies, ielen, pos);
1633 pos = skip_ie(ies, ielen, pos);
1639 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1641 bool ieee80211_operating_class_to_band(u8 operating_class,
1642 enum nl80211_band *band)
1644 switch (operating_class) {
1648 *band = NL80211_BAND_5GHZ;
1651 *band = NL80211_BAND_6GHZ;
1657 *band = NL80211_BAND_2GHZ;
1660 *band = NL80211_BAND_60GHZ;
1666 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1668 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1672 u32 freq = chandef->center_freq1;
1674 if (freq >= 2412 && freq <= 2472) {
1675 if (chandef->width > NL80211_CHAN_WIDTH_40)
1678 /* 2.407 GHz, channels 1..13 */
1679 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1680 if (freq > chandef->chan->center_freq)
1681 *op_class = 83; /* HT40+ */
1683 *op_class = 84; /* HT40- */
1692 /* channel 14 is only for IEEE 802.11b */
1693 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
1696 *op_class = 82; /* channel 14 */
1700 switch (chandef->width) {
1701 case NL80211_CHAN_WIDTH_80:
1704 case NL80211_CHAN_WIDTH_160:
1707 case NL80211_CHAN_WIDTH_80P80:
1710 case NL80211_CHAN_WIDTH_10:
1711 case NL80211_CHAN_WIDTH_5:
1712 return false; /* unsupported for now */
1718 /* 5 GHz, channels 36..48 */
1719 if (freq >= 5180 && freq <= 5240) {
1721 *op_class = vht_opclass;
1722 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1723 if (freq > chandef->chan->center_freq)
1734 /* 5 GHz, channels 52..64 */
1735 if (freq >= 5260 && freq <= 5320) {
1737 *op_class = vht_opclass;
1738 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1739 if (freq > chandef->chan->center_freq)
1750 /* 5 GHz, channels 100..144 */
1751 if (freq >= 5500 && freq <= 5720) {
1753 *op_class = vht_opclass;
1754 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1755 if (freq > chandef->chan->center_freq)
1766 /* 5 GHz, channels 149..169 */
1767 if (freq >= 5745 && freq <= 5845) {
1769 *op_class = vht_opclass;
1770 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1771 if (freq > chandef->chan->center_freq)
1775 } else if (freq <= 5805) {
1784 /* 56.16 GHz, channel 1..4 */
1785 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1786 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1793 /* not supported yet */
1796 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1798 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1799 u32 *beacon_int_gcd,
1800 bool *beacon_int_different)
1802 struct wireless_dev *wdev;
1804 *beacon_int_gcd = 0;
1805 *beacon_int_different = false;
1807 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1808 if (!wdev->beacon_interval)
1811 if (!*beacon_int_gcd) {
1812 *beacon_int_gcd = wdev->beacon_interval;
1816 if (wdev->beacon_interval == *beacon_int_gcd)
1819 *beacon_int_different = true;
1820 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1823 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1824 if (*beacon_int_gcd)
1825 *beacon_int_different = true;
1826 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1830 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1831 enum nl80211_iftype iftype, u32 beacon_int)
1834 * This is just a basic pre-condition check; if interface combinations
1835 * are possible the driver must already be checking those with a call
1836 * to cfg80211_check_combinations(), in which case we'll validate more
1837 * through the cfg80211_calculate_bi_data() call and code in
1838 * cfg80211_iter_combinations().
1841 if (beacon_int < 10 || beacon_int > 10000)
1847 int cfg80211_iter_combinations(struct wiphy *wiphy,
1848 struct iface_combination_params *params,
1849 void (*iter)(const struct ieee80211_iface_combination *c,
1853 const struct ieee80211_regdomain *regdom;
1854 enum nl80211_dfs_regions region = 0;
1856 int num_interfaces = 0;
1857 u32 used_iftypes = 0;
1859 bool beacon_int_different;
1862 * This is a bit strange, since the iteration used to rely only on
1863 * the data given by the driver, but here it now relies on context,
1864 * in form of the currently operating interfaces.
1865 * This is OK for all current users, and saves us from having to
1866 * push the GCD calculations into all the drivers.
1867 * In the future, this should probably rely more on data that's in
1868 * cfg80211 already - the only thing not would appear to be any new
1869 * interfaces (while being brought up) and channel/radar data.
1871 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1872 &beacon_int_gcd, &beacon_int_different);
1874 if (params->radar_detect) {
1876 regdom = rcu_dereference(cfg80211_regdomain);
1878 region = regdom->dfs_region;
1882 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1883 num_interfaces += params->iftype_num[iftype];
1884 if (params->iftype_num[iftype] > 0 &&
1885 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1886 used_iftypes |= BIT(iftype);
1889 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1890 const struct ieee80211_iface_combination *c;
1891 struct ieee80211_iface_limit *limits;
1892 u32 all_iftypes = 0;
1894 c = &wiphy->iface_combinations[i];
1896 if (num_interfaces > c->max_interfaces)
1898 if (params->num_different_channels > c->num_different_channels)
1901 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1906 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1907 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1909 for (j = 0; j < c->n_limits; j++) {
1910 all_iftypes |= limits[j].types;
1911 if (!(limits[j].types & BIT(iftype)))
1913 if (limits[j].max < params->iftype_num[iftype])
1915 limits[j].max -= params->iftype_num[iftype];
1919 if (params->radar_detect !=
1920 (c->radar_detect_widths & params->radar_detect))
1923 if (params->radar_detect && c->radar_detect_regions &&
1924 !(c->radar_detect_regions & BIT(region)))
1927 /* Finally check that all iftypes that we're currently
1928 * using are actually part of this combination. If they
1929 * aren't then we can't use this combination and have
1930 * to continue to the next.
1932 if ((all_iftypes & used_iftypes) != used_iftypes)
1935 if (beacon_int_gcd) {
1936 if (c->beacon_int_min_gcd &&
1937 beacon_int_gcd < c->beacon_int_min_gcd)
1939 if (!c->beacon_int_min_gcd && beacon_int_different)
1943 /* This combination covered all interface types and
1944 * supported the requested numbers, so we're good.
1954 EXPORT_SYMBOL(cfg80211_iter_combinations);
1957 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1964 int cfg80211_check_combinations(struct wiphy *wiphy,
1965 struct iface_combination_params *params)
1969 err = cfg80211_iter_combinations(wiphy, params,
1970 cfg80211_iter_sum_ifcombs, &num);
1978 EXPORT_SYMBOL(cfg80211_check_combinations);
1980 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1981 const u8 *rates, unsigned int n_rates,
1989 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1994 for (i = 0; i < n_rates; i++) {
1995 int rate = (rates[i] & 0x7f) * 5;
1998 for (j = 0; j < sband->n_bitrates; j++) {
1999 if (sband->bitrates[j].bitrate == rate) {
2010 * mask must have at least one bit set here since we
2011 * didn't accept a 0-length rates array nor allowed
2012 * entries in the array that didn't exist
2018 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2020 enum nl80211_band band;
2021 unsigned int n_channels = 0;
2023 for (band = 0; band < NUM_NL80211_BANDS; band++)
2024 if (wiphy->bands[band])
2025 n_channels += wiphy->bands[band]->n_channels;
2029 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2031 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2032 struct station_info *sinfo)
2034 struct cfg80211_registered_device *rdev;
2035 struct wireless_dev *wdev;
2037 wdev = dev->ieee80211_ptr;
2041 rdev = wiphy_to_rdev(wdev->wiphy);
2042 if (!rdev->ops->get_station)
2045 memset(sinfo, 0, sizeof(*sinfo));
2047 return rdev_get_station(rdev, dev, mac_addr, sinfo);
2049 EXPORT_SYMBOL(cfg80211_get_station);
2051 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2058 kfree(f->serv_spec_info);
2061 for (i = 0; i < f->num_rx_filters; i++)
2062 kfree(f->rx_filters[i].filter);
2064 for (i = 0; i < f->num_tx_filters; i++)
2065 kfree(f->tx_filters[i].filter);
2067 kfree(f->rx_filters);
2068 kfree(f->tx_filters);
2071 EXPORT_SYMBOL(cfg80211_free_nan_func);
2073 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2074 u32 center_freq_khz, u32 bw_khz)
2076 u32 start_freq_khz, end_freq_khz;
2078 start_freq_khz = center_freq_khz - (bw_khz / 2);
2079 end_freq_khz = center_freq_khz + (bw_khz / 2);
2081 if (start_freq_khz >= freq_range->start_freq_khz &&
2082 end_freq_khz <= freq_range->end_freq_khz)
2088 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2090 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2091 sizeof(*(sinfo->pertid)),
2098 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2100 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2101 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2102 const unsigned char rfc1042_header[] __aligned(2) =
2103 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2104 EXPORT_SYMBOL(rfc1042_header);
2106 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2107 const unsigned char bridge_tunnel_header[] __aligned(2) =
2108 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2109 EXPORT_SYMBOL(bridge_tunnel_header);
2111 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2112 struct iapp_layer2_update {
2113 u8 da[ETH_ALEN]; /* broadcast */
2114 u8 sa[ETH_ALEN]; /* STA addr */
2122 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2124 struct iapp_layer2_update *msg;
2125 struct sk_buff *skb;
2127 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2130 skb = dev_alloc_skb(sizeof(*msg));
2133 msg = skb_put(skb, sizeof(*msg));
2135 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2136 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2138 eth_broadcast_addr(msg->da);
2139 ether_addr_copy(msg->sa, addr);
2140 msg->len = htons(6);
2142 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2143 msg->control = 0xaf; /* XID response lsb.1111F101.
2144 * F=0 (no poll command; unsolicited frame) */
2145 msg->xid_info[0] = 0x81; /* XID format identifier */
2146 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2147 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2150 skb->protocol = eth_type_trans(skb, dev);
2151 memset(skb->cb, 0, sizeof(skb->cb));
2154 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2156 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2157 enum ieee80211_vht_chanwidth bw,
2158 int mcs, bool ext_nss_bw_capable,
2159 unsigned int max_vht_nss)
2161 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2164 int i, mcs_encoding;
2169 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2179 /* find max_vht_nss for the given MCS */
2180 for (i = 7; i >= 0; i--) {
2181 int supp = (map >> (2 * i)) & 3;
2186 if (supp >= mcs_encoding) {
2187 max_vht_nss = i + 1;
2193 if (!(cap->supp_mcs.tx_mcs_map &
2194 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2197 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2198 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2199 supp_width = le32_get_bits(cap->vht_cap_info,
2200 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2202 /* if not capable, treat ext_nss_bw as 0 */
2203 if (!ext_nss_bw_capable)
2206 /* This is invalid */
2207 if (supp_width == 3)
2210 /* This is an invalid combination so pretend nothing is supported */
2211 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2215 * Cover all the special cases according to IEEE 802.11-2016
2216 * Table 9-250. All other cases are either factor of 1 or not
2220 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2221 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2222 if ((supp_width == 1 || supp_width == 2) &&
2224 return 2 * max_vht_nss;
2226 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2227 if (supp_width == 0 &&
2228 (ext_nss_bw == 1 || ext_nss_bw == 2))
2229 return max_vht_nss / 2;
2230 if (supp_width == 0 &&
2232 return (3 * max_vht_nss) / 4;
2233 if (supp_width == 1 &&
2235 return 2 * max_vht_nss;
2237 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2238 if (supp_width == 0 && ext_nss_bw == 1)
2239 return 0; /* not possible */
2240 if (supp_width == 0 &&
2242 return max_vht_nss / 2;
2243 if (supp_width == 0 &&
2245 return (3 * max_vht_nss) / 4;
2246 if (supp_width == 1 &&
2248 return 0; /* not possible */
2249 if (supp_width == 1 &&
2251 return max_vht_nss / 2;
2252 if (supp_width == 1 &&
2254 return (3 * max_vht_nss) / 4;
2258 /* not covered or invalid combination received */
2261 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2263 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2264 bool is_4addr, u8 check_swif)
2267 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2269 switch (check_swif) {
2271 if (is_vlan && is_4addr)
2272 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2273 return wiphy->interface_modes & BIT(iftype);
2275 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2276 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2277 return wiphy->software_iftypes & BIT(iftype);
2284 EXPORT_SYMBOL(cfg80211_iftype_allowed);