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 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])
275 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
276 struct key_params *params, int key_idx,
277 bool pairwise, const u8 *mac_addr)
281 if (wiphy_ext_feature_isset(&rdev->wiphy,
282 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
283 wiphy_ext_feature_isset(&rdev->wiphy,
284 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
286 if (key_idx < 0 || key_idx > max_key_idx)
289 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
292 if (pairwise && !mac_addr)
295 switch (params->cipher) {
296 case WLAN_CIPHER_SUITE_TKIP:
297 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
298 if ((pairwise && key_idx) ||
299 params->mode != NL80211_KEY_RX_TX)
302 case WLAN_CIPHER_SUITE_CCMP:
303 case WLAN_CIPHER_SUITE_CCMP_256:
304 case WLAN_CIPHER_SUITE_GCMP:
305 case WLAN_CIPHER_SUITE_GCMP_256:
306 /* IEEE802.11-2016 allows only 0 and - when supporting
307 * Extended Key ID - 1 as index for pairwise keys.
308 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
309 * the driver supports Extended Key ID.
310 * @NL80211_KEY_SET_TX can't be set when installing and
313 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
314 params->mode == NL80211_KEY_SET_TX)
316 if (wiphy_ext_feature_isset(&rdev->wiphy,
317 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
318 if (pairwise && (key_idx < 0 || key_idx > 1))
320 } else if (pairwise && key_idx) {
324 case WLAN_CIPHER_SUITE_AES_CMAC:
325 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
326 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
327 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
328 /* Disallow BIP (group-only) cipher as pairwise cipher */
334 case WLAN_CIPHER_SUITE_WEP40:
335 case WLAN_CIPHER_SUITE_WEP104:
342 switch (params->cipher) {
343 case WLAN_CIPHER_SUITE_WEP40:
344 if (params->key_len != WLAN_KEY_LEN_WEP40)
347 case WLAN_CIPHER_SUITE_TKIP:
348 if (params->key_len != WLAN_KEY_LEN_TKIP)
351 case WLAN_CIPHER_SUITE_CCMP:
352 if (params->key_len != WLAN_KEY_LEN_CCMP)
355 case WLAN_CIPHER_SUITE_CCMP_256:
356 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
359 case WLAN_CIPHER_SUITE_GCMP:
360 if (params->key_len != WLAN_KEY_LEN_GCMP)
363 case WLAN_CIPHER_SUITE_GCMP_256:
364 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
367 case WLAN_CIPHER_SUITE_WEP104:
368 if (params->key_len != WLAN_KEY_LEN_WEP104)
371 case WLAN_CIPHER_SUITE_AES_CMAC:
372 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
375 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
376 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
379 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
380 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
383 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
384 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
389 * We don't know anything about this algorithm,
390 * allow using it -- but the driver must check
391 * all parameters! We still check below whether
392 * or not the driver supports this algorithm,
399 switch (params->cipher) {
400 case WLAN_CIPHER_SUITE_WEP40:
401 case WLAN_CIPHER_SUITE_WEP104:
402 /* These ciphers do not use key sequence */
404 case WLAN_CIPHER_SUITE_TKIP:
405 case WLAN_CIPHER_SUITE_CCMP:
406 case WLAN_CIPHER_SUITE_CCMP_256:
407 case WLAN_CIPHER_SUITE_GCMP:
408 case WLAN_CIPHER_SUITE_GCMP_256:
409 case WLAN_CIPHER_SUITE_AES_CMAC:
410 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
411 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
412 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
413 if (params->seq_len != 6)
419 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
425 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
427 unsigned int hdrlen = 24;
429 if (ieee80211_is_ext(fc)) {
434 if (ieee80211_is_data(fc)) {
435 if (ieee80211_has_a4(fc))
437 if (ieee80211_is_data_qos(fc)) {
438 hdrlen += IEEE80211_QOS_CTL_LEN;
439 if (ieee80211_has_order(fc))
440 hdrlen += IEEE80211_HT_CTL_LEN;
445 if (ieee80211_is_mgmt(fc)) {
446 if (ieee80211_has_order(fc))
447 hdrlen += IEEE80211_HT_CTL_LEN;
451 if (ieee80211_is_ctl(fc)) {
453 * ACK and CTS are 10 bytes, all others 16. To see how
454 * to get this condition consider
455 * subtype mask: 0b0000000011110000 (0x00F0)
456 * ACK subtype: 0b0000000011010000 (0x00D0)
457 * CTS subtype: 0b0000000011000000 (0x00C0)
458 * bits that matter: ^^^ (0x00E0)
459 * value of those: 0b0000000011000000 (0x00C0)
461 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
469 EXPORT_SYMBOL(ieee80211_hdrlen);
471 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
473 const struct ieee80211_hdr *hdr =
474 (const struct ieee80211_hdr *)skb->data;
477 if (unlikely(skb->len < 10))
479 hdrlen = ieee80211_hdrlen(hdr->frame_control);
480 if (unlikely(hdrlen > skb->len))
484 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
486 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
488 int ae = flags & MESH_FLAGS_AE;
489 /* 802.11-2012, 8.2.4.7.3 */
494 case MESH_FLAGS_AE_A4:
496 case MESH_FLAGS_AE_A5_A6:
501 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
503 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
505 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
507 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
508 const u8 *addr, enum nl80211_iftype iftype,
511 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
513 u8 hdr[ETH_ALEN] __aligned(2);
520 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
523 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
524 if (skb->len < hdrlen + 8)
527 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
529 * IEEE 802.11 address fields:
530 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
531 * 0 0 DA SA BSSID n/a
532 * 0 1 DA BSSID SA n/a
533 * 1 0 BSSID SA DA n/a
536 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
537 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
539 if (iftype == NL80211_IFTYPE_MESH_POINT)
540 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
542 mesh_flags &= MESH_FLAGS_AE;
544 switch (hdr->frame_control &
545 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
546 case cpu_to_le16(IEEE80211_FCTL_TODS):
547 if (unlikely(iftype != NL80211_IFTYPE_AP &&
548 iftype != NL80211_IFTYPE_AP_VLAN &&
549 iftype != NL80211_IFTYPE_P2P_GO))
552 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
553 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
554 iftype != NL80211_IFTYPE_MESH_POINT &&
555 iftype != NL80211_IFTYPE_AP_VLAN &&
556 iftype != NL80211_IFTYPE_STATION))
558 if (iftype == NL80211_IFTYPE_MESH_POINT) {
559 if (mesh_flags == MESH_FLAGS_AE_A4)
561 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
562 skb_copy_bits(skb, hdrlen +
563 offsetof(struct ieee80211s_hdr, eaddr1),
564 tmp.h_dest, 2 * ETH_ALEN);
566 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
569 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
570 if ((iftype != NL80211_IFTYPE_STATION &&
571 iftype != NL80211_IFTYPE_P2P_CLIENT &&
572 iftype != NL80211_IFTYPE_MESH_POINT) ||
573 (is_multicast_ether_addr(tmp.h_dest) &&
574 ether_addr_equal(tmp.h_source, addr)))
576 if (iftype == NL80211_IFTYPE_MESH_POINT) {
577 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
579 if (mesh_flags == MESH_FLAGS_AE_A4)
580 skb_copy_bits(skb, hdrlen +
581 offsetof(struct ieee80211s_hdr, eaddr1),
582 tmp.h_source, ETH_ALEN);
583 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
587 if (iftype != NL80211_IFTYPE_ADHOC &&
588 iftype != NL80211_IFTYPE_STATION &&
589 iftype != NL80211_IFTYPE_OCB)
594 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
595 tmp.h_proto = payload.proto;
597 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
598 tmp.h_proto != htons(ETH_P_AARP) &&
599 tmp.h_proto != htons(ETH_P_IPX)) ||
600 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
601 /* remove RFC1042 or Bridge-Tunnel encapsulation and
602 * replace EtherType */
603 hdrlen += ETH_ALEN + 2;
605 tmp.h_proto = htons(skb->len - hdrlen);
607 pskb_pull(skb, hdrlen);
610 ehdr = skb_push(skb, sizeof(struct ethhdr));
611 memcpy(ehdr, &tmp, sizeof(tmp));
615 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
618 __frame_add_frag(struct sk_buff *skb, struct page *page,
619 void *ptr, int len, int size)
621 struct skb_shared_info *sh = skb_shinfo(skb);
625 page_offset = ptr - page_address(page);
626 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
630 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
633 struct skb_shared_info *sh = skb_shinfo(skb);
634 const skb_frag_t *frag = &sh->frags[0];
635 struct page *frag_page;
637 int frag_len, frag_size;
638 int head_size = skb->len - skb->data_len;
641 frag_page = virt_to_head_page(skb->head);
642 frag_ptr = skb->data;
643 frag_size = head_size;
645 while (offset >= frag_size) {
647 frag_page = skb_frag_page(frag);
648 frag_ptr = skb_frag_address(frag);
649 frag_size = skb_frag_size(frag);
654 frag_len = frag_size - offset;
656 cur_len = min(len, frag_len);
658 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
662 frag_len = skb_frag_size(frag);
663 cur_len = min(len, frag_len);
664 __frame_add_frag(frame, skb_frag_page(frag),
665 skb_frag_address(frag), cur_len, frag_len);
671 static struct sk_buff *
672 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
673 int offset, int len, bool reuse_frag)
675 struct sk_buff *frame;
678 if (skb->len - offset < len)
682 * When reusing framents, copy some data to the head to simplify
683 * ethernet header handling and speed up protocol header processing
684 * in the stack later.
687 cur_len = min_t(int, len, 32);
690 * Allocate and reserve two bytes more for payload
691 * alignment since sizeof(struct ethhdr) is 14.
693 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
697 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
698 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
705 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
710 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
711 const u8 *addr, enum nl80211_iftype iftype,
712 const unsigned int extra_headroom,
713 const u8 *check_da, const u8 *check_sa)
715 unsigned int hlen = ALIGN(extra_headroom, 4);
716 struct sk_buff *frame = NULL;
719 int offset = 0, remaining;
721 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
722 bool reuse_skb = false;
726 unsigned int subframe_len;
730 skb_copy_bits(skb, offset, ð, sizeof(eth));
731 len = ntohs(eth.h_proto);
732 subframe_len = sizeof(struct ethhdr) + len;
733 padding = (4 - subframe_len) & 0x3;
735 /* the last MSDU has no padding */
736 remaining = skb->len - offset;
737 if (subframe_len > remaining)
740 offset += sizeof(struct ethhdr);
741 last = remaining <= subframe_len + padding;
743 /* FIXME: should we really accept multicast DA? */
744 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
745 !ether_addr_equal(check_da, eth.h_dest)) ||
746 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
747 offset += len + padding;
751 /* reuse skb for the last subframe */
752 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
753 skb_pull(skb, offset);
757 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
762 offset += len + padding;
765 skb_reset_network_header(frame);
766 frame->dev = skb->dev;
767 frame->priority = skb->priority;
769 payload = frame->data;
770 ethertype = (payload[6] << 8) | payload[7];
771 if (likely((ether_addr_equal(payload, rfc1042_header) &&
772 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
773 ether_addr_equal(payload, bridge_tunnel_header))) {
774 eth.h_proto = htons(ethertype);
775 skb_pull(frame, ETH_ALEN + 2);
778 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
779 __skb_queue_tail(list, frame);
788 __skb_queue_purge(list);
791 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
793 /* Given a data frame determine the 802.1p/1d tag to use. */
794 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
795 struct cfg80211_qos_map *qos_map)
798 unsigned char vlan_priority;
801 /* skb->priority values from 256->263 are magic values to
802 * directly indicate a specific 802.1d priority. This is used
803 * to allow 802.1d priority to be passed directly in from VLAN
806 if (skb->priority >= 256 && skb->priority <= 263) {
807 ret = skb->priority - 256;
811 if (skb_vlan_tag_present(skb)) {
812 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
814 if (vlan_priority > 0) {
820 switch (skb->protocol) {
821 case htons(ETH_P_IP):
822 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
824 case htons(ETH_P_IPV6):
825 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
827 case htons(ETH_P_MPLS_UC):
828 case htons(ETH_P_MPLS_MC): {
829 struct mpls_label mpls_tmp, *mpls;
831 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
832 sizeof(*mpls), &mpls_tmp);
836 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
840 case htons(ETH_P_80221):
841 /* 802.21 is always network control traffic */
848 unsigned int i, tmp_dscp = dscp >> 2;
850 for (i = 0; i < qos_map->num_des; i++) {
851 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
852 ret = qos_map->dscp_exception[i].up;
857 for (i = 0; i < 8; i++) {
858 if (tmp_dscp >= qos_map->up[i].low &&
859 tmp_dscp <= qos_map->up[i].high) {
868 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
870 EXPORT_SYMBOL(cfg80211_classify8021d);
872 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
874 const struct cfg80211_bss_ies *ies;
876 ies = rcu_dereference(bss->ies);
880 return cfg80211_find_elem(id, ies->data, ies->len);
882 EXPORT_SYMBOL(ieee80211_bss_get_elem);
884 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
886 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
887 struct net_device *dev = wdev->netdev;
890 if (!wdev->connect_keys)
893 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
894 if (!wdev->connect_keys->params[i].cipher)
896 if (rdev_add_key(rdev, dev, i, false, NULL,
897 &wdev->connect_keys->params[i])) {
898 netdev_err(dev, "failed to set key %d\n", i);
901 if (wdev->connect_keys->def == i &&
902 rdev_set_default_key(rdev, dev, i, true, true)) {
903 netdev_err(dev, "failed to set defkey %d\n", i);
908 kfree_sensitive(wdev->connect_keys);
909 wdev->connect_keys = NULL;
912 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
914 struct cfg80211_event *ev;
917 spin_lock_irqsave(&wdev->event_lock, flags);
918 while (!list_empty(&wdev->event_list)) {
919 ev = list_first_entry(&wdev->event_list,
920 struct cfg80211_event, list);
922 spin_unlock_irqrestore(&wdev->event_lock, flags);
926 case EVENT_CONNECT_RESULT:
927 __cfg80211_connect_result(
930 ev->cr.status == WLAN_STATUS_SUCCESS);
933 __cfg80211_roamed(wdev, &ev->rm);
935 case EVENT_DISCONNECTED:
936 __cfg80211_disconnected(wdev->netdev,
937 ev->dc.ie, ev->dc.ie_len,
939 !ev->dc.locally_generated);
941 case EVENT_IBSS_JOINED:
942 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
946 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
948 case EVENT_PORT_AUTHORIZED:
949 __cfg80211_port_authorized(wdev, ev->pa.bssid);
956 spin_lock_irqsave(&wdev->event_lock, flags);
958 spin_unlock_irqrestore(&wdev->event_lock, flags);
961 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
963 struct wireless_dev *wdev;
967 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
968 cfg80211_process_wdev_events(wdev);
971 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
972 struct net_device *dev, enum nl80211_iftype ntype,
973 struct vif_params *params)
976 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
980 /* don't support changing VLANs, you just re-create them */
981 if (otype == NL80211_IFTYPE_AP_VLAN)
984 /* cannot change into P2P device or NAN */
985 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
986 ntype == NL80211_IFTYPE_NAN)
989 if (!rdev->ops->change_virtual_intf ||
990 !(rdev->wiphy.interface_modes & (1 << ntype)))
993 /* if it's part of a bridge, reject changing type to station/ibss */
994 if (netif_is_bridge_port(dev) &&
995 (ntype == NL80211_IFTYPE_ADHOC ||
996 ntype == NL80211_IFTYPE_STATION ||
997 ntype == NL80211_IFTYPE_P2P_CLIENT))
1000 if (ntype != otype) {
1001 dev->ieee80211_ptr->use_4addr = false;
1002 dev->ieee80211_ptr->mesh_id_up_len = 0;
1003 wdev_lock(dev->ieee80211_ptr);
1004 rdev_set_qos_map(rdev, dev, NULL);
1005 wdev_unlock(dev->ieee80211_ptr);
1008 case NL80211_IFTYPE_AP:
1009 cfg80211_stop_ap(rdev, dev, true);
1011 case NL80211_IFTYPE_ADHOC:
1012 cfg80211_leave_ibss(rdev, dev, false);
1014 case NL80211_IFTYPE_STATION:
1015 case NL80211_IFTYPE_P2P_CLIENT:
1016 wdev_lock(dev->ieee80211_ptr);
1017 cfg80211_disconnect(rdev, dev,
1018 WLAN_REASON_DEAUTH_LEAVING, true);
1019 wdev_unlock(dev->ieee80211_ptr);
1021 case NL80211_IFTYPE_MESH_POINT:
1022 /* mesh should be handled? */
1028 cfg80211_process_rdev_events(rdev);
1029 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1032 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1034 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1036 if (!err && params && params->use_4addr != -1)
1037 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1040 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1042 case NL80211_IFTYPE_STATION:
1043 if (dev->ieee80211_ptr->use_4addr)
1046 case NL80211_IFTYPE_OCB:
1047 case NL80211_IFTYPE_P2P_CLIENT:
1048 case NL80211_IFTYPE_ADHOC:
1049 dev->priv_flags |= IFF_DONT_BRIDGE;
1051 case NL80211_IFTYPE_P2P_GO:
1052 case NL80211_IFTYPE_AP:
1053 case NL80211_IFTYPE_AP_VLAN:
1054 case NL80211_IFTYPE_WDS:
1055 case NL80211_IFTYPE_MESH_POINT:
1058 case NL80211_IFTYPE_MONITOR:
1059 /* monitor can't bridge anyway */
1061 case NL80211_IFTYPE_UNSPECIFIED:
1062 case NUM_NL80211_IFTYPES:
1065 case NL80211_IFTYPE_P2P_DEVICE:
1066 case NL80211_IFTYPE_NAN:
1072 if (!err && ntype != otype && netif_running(dev)) {
1073 cfg80211_update_iface_num(rdev, ntype, 1);
1074 cfg80211_update_iface_num(rdev, otype, -1);
1080 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1082 int modulation, streams, bitrate;
1084 /* the formula below does only work for MCS values smaller than 32 */
1085 if (WARN_ON_ONCE(rate->mcs >= 32))
1088 modulation = rate->mcs & 7;
1089 streams = (rate->mcs >> 3) + 1;
1091 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1094 bitrate *= (modulation + 1);
1095 else if (modulation == 4)
1096 bitrate *= (modulation + 2);
1098 bitrate *= (modulation + 3);
1102 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1103 bitrate = (bitrate / 9) * 10;
1105 /* do NOT round down here */
1106 return (bitrate + 50000) / 100000;
1109 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1111 static const u32 __mcs2bitrate[] = {
1119 [5] = 12512, /* 1251.25 mbps */
1129 [14] = 8662, /* 866.25 mbps */
1139 [24] = 67568, /* 6756.75 mbps */
1150 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1153 return __mcs2bitrate[rate->mcs];
1156 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1158 static const u32 __mcs2bitrate[] = {
1166 [5] = 12512, /* 1251.25 mbps */
1184 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1187 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1190 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1192 static const u32 base[4][10] = {
1202 /* not in the spec, but some devices use this: */
1246 case RATE_INFO_BW_160:
1249 case RATE_INFO_BW_80:
1252 case RATE_INFO_BW_40:
1255 case RATE_INFO_BW_5:
1256 case RATE_INFO_BW_10:
1259 case RATE_INFO_BW_20:
1263 bitrate = base[idx][rate->mcs];
1264 bitrate *= rate->nss;
1266 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1267 bitrate = (bitrate / 9) * 10;
1269 /* do NOT round down here */
1270 return (bitrate + 50000) / 100000;
1272 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1273 rate->bw, rate->mcs, rate->nss);
1277 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1280 u16 mcs_divisors[12] = {
1281 34133, /* 16.666666... */
1282 17067, /* 8.333333... */
1283 11378, /* 5.555555... */
1284 8533, /* 4.166666... */
1285 5689, /* 2.777777... */
1286 4267, /* 2.083333... */
1287 3923, /* 1.851851... */
1288 3413, /* 1.666666... */
1289 2844, /* 1.388888... */
1290 2560, /* 1.250000... */
1291 2276, /* 1.111111... */
1292 2048, /* 1.000000... */
1294 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1295 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1296 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1297 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1298 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1299 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1300 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1304 if (WARN_ON_ONCE(rate->mcs > 11))
1307 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1309 if (WARN_ON_ONCE(rate->he_ru_alloc >
1310 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1312 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1315 if (rate->bw == RATE_INFO_BW_160)
1316 result = rates_160M[rate->he_gi];
1317 else if (rate->bw == RATE_INFO_BW_80 ||
1318 (rate->bw == RATE_INFO_BW_HE_RU &&
1319 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1320 result = rates_969[rate->he_gi];
1321 else if (rate->bw == RATE_INFO_BW_40 ||
1322 (rate->bw == RATE_INFO_BW_HE_RU &&
1323 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1324 result = rates_484[rate->he_gi];
1325 else if (rate->bw == RATE_INFO_BW_20 ||
1326 (rate->bw == RATE_INFO_BW_HE_RU &&
1327 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1328 result = rates_242[rate->he_gi];
1329 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1330 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1331 result = rates_106[rate->he_gi];
1332 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1333 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1334 result = rates_52[rate->he_gi];
1335 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1336 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1337 result = rates_26[rate->he_gi];
1339 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1340 rate->bw, rate->he_ru_alloc);
1344 /* now scale to the appropriate MCS */
1347 do_div(tmp, mcs_divisors[rate->mcs]);
1350 /* and take NSS, DCM into account */
1351 result = (result * rate->nss) / 8;
1355 return result / 10000;
1358 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1360 if (rate->flags & RATE_INFO_FLAGS_MCS)
1361 return cfg80211_calculate_bitrate_ht(rate);
1362 if (rate->flags & RATE_INFO_FLAGS_DMG)
1363 return cfg80211_calculate_bitrate_dmg(rate);
1364 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1365 return cfg80211_calculate_bitrate_edmg(rate);
1366 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1367 return cfg80211_calculate_bitrate_vht(rate);
1368 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1369 return cfg80211_calculate_bitrate_he(rate);
1371 return rate->legacy;
1373 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1375 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1376 enum ieee80211_p2p_attr_id attr,
1377 u8 *buf, unsigned int bufsize)
1380 u16 attr_remaining = 0;
1381 bool desired_attr = false;
1382 u16 desired_len = 0;
1385 unsigned int iedatalen;
1392 if (iedatalen + 2 > len)
1395 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1403 /* check WFA OUI, P2P subtype */
1404 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1405 iedata[2] != 0x9a || iedata[3] != 0x09)
1411 /* check attribute continuation into this IE */
1412 copy = min_t(unsigned int, attr_remaining, iedatalen);
1413 if (copy && desired_attr) {
1414 desired_len += copy;
1416 memcpy(out, iedata, min(bufsize, copy));
1417 out += min(bufsize, copy);
1418 bufsize -= min(bufsize, copy);
1422 if (copy == attr_remaining)
1426 attr_remaining -= copy;
1433 while (iedatalen > 0) {
1436 /* P2P attribute ID & size must fit */
1439 desired_attr = iedata[0] == attr;
1440 attr_len = get_unaligned_le16(iedata + 1);
1444 copy = min_t(unsigned int, attr_len, iedatalen);
1447 desired_len += copy;
1449 memcpy(out, iedata, min(bufsize, copy));
1450 out += min(bufsize, copy);
1451 bufsize -= min(bufsize, copy);
1454 if (copy == attr_len)
1460 attr_remaining = attr_len - copy;
1468 if (attr_remaining && desired_attr)
1473 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1475 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1479 /* Make sure array values are legal */
1480 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1485 if (ids[i] == WLAN_EID_EXTENSION) {
1486 if (id_ext && (ids[i + 1] == id))
1493 if (ids[i] == id && !id_ext)
1501 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1503 /* we assume a validly formed IEs buffer */
1504 u8 len = ies[pos + 1];
1508 /* the IE itself must have 255 bytes for fragments to follow */
1512 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1520 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1521 const u8 *ids, int n_ids,
1522 const u8 *after_ric, int n_after_ric,
1525 size_t pos = offset;
1527 while (pos < ielen) {
1530 if (ies[pos] == WLAN_EID_EXTENSION)
1532 if ((pos + ext) >= ielen)
1535 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1536 ies[pos] == WLAN_EID_EXTENSION))
1539 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1540 pos = skip_ie(ies, ielen, pos);
1542 while (pos < ielen) {
1543 if (ies[pos] == WLAN_EID_EXTENSION)
1548 if ((pos + ext) >= ielen)
1551 if (!ieee80211_id_in_list(after_ric,
1555 pos = skip_ie(ies, ielen, pos);
1560 pos = skip_ie(ies, ielen, pos);
1566 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1568 bool ieee80211_operating_class_to_band(u8 operating_class,
1569 enum nl80211_band *band)
1571 switch (operating_class) {
1575 *band = NL80211_BAND_5GHZ;
1578 *band = NL80211_BAND_6GHZ;
1584 *band = NL80211_BAND_2GHZ;
1587 *band = NL80211_BAND_60GHZ;
1593 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1595 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1599 u32 freq = chandef->center_freq1;
1601 if (freq >= 2412 && freq <= 2472) {
1602 if (chandef->width > NL80211_CHAN_WIDTH_40)
1605 /* 2.407 GHz, channels 1..13 */
1606 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1607 if (freq > chandef->chan->center_freq)
1608 *op_class = 83; /* HT40+ */
1610 *op_class = 84; /* HT40- */
1619 /* channel 14 is only for IEEE 802.11b */
1620 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
1623 *op_class = 82; /* channel 14 */
1627 switch (chandef->width) {
1628 case NL80211_CHAN_WIDTH_80:
1631 case NL80211_CHAN_WIDTH_160:
1634 case NL80211_CHAN_WIDTH_80P80:
1637 case NL80211_CHAN_WIDTH_10:
1638 case NL80211_CHAN_WIDTH_5:
1639 return false; /* unsupported for now */
1645 /* 5 GHz, channels 36..48 */
1646 if (freq >= 5180 && freq <= 5240) {
1648 *op_class = vht_opclass;
1649 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1650 if (freq > chandef->chan->center_freq)
1661 /* 5 GHz, channels 52..64 */
1662 if (freq >= 5260 && freq <= 5320) {
1664 *op_class = vht_opclass;
1665 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1666 if (freq > chandef->chan->center_freq)
1677 /* 5 GHz, channels 100..144 */
1678 if (freq >= 5500 && freq <= 5720) {
1680 *op_class = vht_opclass;
1681 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1682 if (freq > chandef->chan->center_freq)
1693 /* 5 GHz, channels 149..169 */
1694 if (freq >= 5745 && freq <= 5845) {
1696 *op_class = vht_opclass;
1697 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1698 if (freq > chandef->chan->center_freq)
1702 } else if (freq <= 5805) {
1711 /* 56.16 GHz, channel 1..4 */
1712 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1713 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1720 /* not supported yet */
1723 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1725 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1726 u32 *beacon_int_gcd,
1727 bool *beacon_int_different)
1729 struct wireless_dev *wdev;
1731 *beacon_int_gcd = 0;
1732 *beacon_int_different = false;
1734 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1735 if (!wdev->beacon_interval)
1738 if (!*beacon_int_gcd) {
1739 *beacon_int_gcd = wdev->beacon_interval;
1743 if (wdev->beacon_interval == *beacon_int_gcd)
1746 *beacon_int_different = true;
1747 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1750 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1751 if (*beacon_int_gcd)
1752 *beacon_int_different = true;
1753 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1757 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1758 enum nl80211_iftype iftype, u32 beacon_int)
1761 * This is just a basic pre-condition check; if interface combinations
1762 * are possible the driver must already be checking those with a call
1763 * to cfg80211_check_combinations(), in which case we'll validate more
1764 * through the cfg80211_calculate_bi_data() call and code in
1765 * cfg80211_iter_combinations().
1768 if (beacon_int < 10 || beacon_int > 10000)
1774 int cfg80211_iter_combinations(struct wiphy *wiphy,
1775 struct iface_combination_params *params,
1776 void (*iter)(const struct ieee80211_iface_combination *c,
1780 const struct ieee80211_regdomain *regdom;
1781 enum nl80211_dfs_regions region = 0;
1783 int num_interfaces = 0;
1784 u32 used_iftypes = 0;
1786 bool beacon_int_different;
1789 * This is a bit strange, since the iteration used to rely only on
1790 * the data given by the driver, but here it now relies on context,
1791 * in form of the currently operating interfaces.
1792 * This is OK for all current users, and saves us from having to
1793 * push the GCD calculations into all the drivers.
1794 * In the future, this should probably rely more on data that's in
1795 * cfg80211 already - the only thing not would appear to be any new
1796 * interfaces (while being brought up) and channel/radar data.
1798 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1799 &beacon_int_gcd, &beacon_int_different);
1801 if (params->radar_detect) {
1803 regdom = rcu_dereference(cfg80211_regdomain);
1805 region = regdom->dfs_region;
1809 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1810 num_interfaces += params->iftype_num[iftype];
1811 if (params->iftype_num[iftype] > 0 &&
1812 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1813 used_iftypes |= BIT(iftype);
1816 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1817 const struct ieee80211_iface_combination *c;
1818 struct ieee80211_iface_limit *limits;
1819 u32 all_iftypes = 0;
1821 c = &wiphy->iface_combinations[i];
1823 if (num_interfaces > c->max_interfaces)
1825 if (params->num_different_channels > c->num_different_channels)
1828 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1833 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1834 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1836 for (j = 0; j < c->n_limits; j++) {
1837 all_iftypes |= limits[j].types;
1838 if (!(limits[j].types & BIT(iftype)))
1840 if (limits[j].max < params->iftype_num[iftype])
1842 limits[j].max -= params->iftype_num[iftype];
1846 if (params->radar_detect !=
1847 (c->radar_detect_widths & params->radar_detect))
1850 if (params->radar_detect && c->radar_detect_regions &&
1851 !(c->radar_detect_regions & BIT(region)))
1854 /* Finally check that all iftypes that we're currently
1855 * using are actually part of this combination. If they
1856 * aren't then we can't use this combination and have
1857 * to continue to the next.
1859 if ((all_iftypes & used_iftypes) != used_iftypes)
1862 if (beacon_int_gcd) {
1863 if (c->beacon_int_min_gcd &&
1864 beacon_int_gcd < c->beacon_int_min_gcd)
1866 if (!c->beacon_int_min_gcd && beacon_int_different)
1870 /* This combination covered all interface types and
1871 * supported the requested numbers, so we're good.
1881 EXPORT_SYMBOL(cfg80211_iter_combinations);
1884 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1891 int cfg80211_check_combinations(struct wiphy *wiphy,
1892 struct iface_combination_params *params)
1896 err = cfg80211_iter_combinations(wiphy, params,
1897 cfg80211_iter_sum_ifcombs, &num);
1905 EXPORT_SYMBOL(cfg80211_check_combinations);
1907 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1908 const u8 *rates, unsigned int n_rates,
1916 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1921 for (i = 0; i < n_rates; i++) {
1922 int rate = (rates[i] & 0x7f) * 5;
1925 for (j = 0; j < sband->n_bitrates; j++) {
1926 if (sband->bitrates[j].bitrate == rate) {
1937 * mask must have at least one bit set here since we
1938 * didn't accept a 0-length rates array nor allowed
1939 * entries in the array that didn't exist
1945 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1947 enum nl80211_band band;
1948 unsigned int n_channels = 0;
1950 for (band = 0; band < NUM_NL80211_BANDS; band++)
1951 if (wiphy->bands[band])
1952 n_channels += wiphy->bands[band]->n_channels;
1956 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1958 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1959 struct station_info *sinfo)
1961 struct cfg80211_registered_device *rdev;
1962 struct wireless_dev *wdev;
1964 wdev = dev->ieee80211_ptr;
1968 rdev = wiphy_to_rdev(wdev->wiphy);
1969 if (!rdev->ops->get_station)
1972 memset(sinfo, 0, sizeof(*sinfo));
1974 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1976 EXPORT_SYMBOL(cfg80211_get_station);
1978 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1985 kfree(f->serv_spec_info);
1988 for (i = 0; i < f->num_rx_filters; i++)
1989 kfree(f->rx_filters[i].filter);
1991 for (i = 0; i < f->num_tx_filters; i++)
1992 kfree(f->tx_filters[i].filter);
1994 kfree(f->rx_filters);
1995 kfree(f->tx_filters);
1998 EXPORT_SYMBOL(cfg80211_free_nan_func);
2000 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2001 u32 center_freq_khz, u32 bw_khz)
2003 u32 start_freq_khz, end_freq_khz;
2005 start_freq_khz = center_freq_khz - (bw_khz / 2);
2006 end_freq_khz = center_freq_khz + (bw_khz / 2);
2008 if (start_freq_khz >= freq_range->start_freq_khz &&
2009 end_freq_khz <= freq_range->end_freq_khz)
2015 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2017 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2018 sizeof(*(sinfo->pertid)),
2025 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2027 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2028 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2029 const unsigned char rfc1042_header[] __aligned(2) =
2030 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2031 EXPORT_SYMBOL(rfc1042_header);
2033 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2034 const unsigned char bridge_tunnel_header[] __aligned(2) =
2035 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2036 EXPORT_SYMBOL(bridge_tunnel_header);
2038 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2039 struct iapp_layer2_update {
2040 u8 da[ETH_ALEN]; /* broadcast */
2041 u8 sa[ETH_ALEN]; /* STA addr */
2049 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2051 struct iapp_layer2_update *msg;
2052 struct sk_buff *skb;
2054 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2057 skb = dev_alloc_skb(sizeof(*msg));
2060 msg = skb_put(skb, sizeof(*msg));
2062 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2063 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2065 eth_broadcast_addr(msg->da);
2066 ether_addr_copy(msg->sa, addr);
2067 msg->len = htons(6);
2069 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2070 msg->control = 0xaf; /* XID response lsb.1111F101.
2071 * F=0 (no poll command; unsolicited frame) */
2072 msg->xid_info[0] = 0x81; /* XID format identifier */
2073 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2074 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2077 skb->protocol = eth_type_trans(skb, dev);
2078 memset(skb->cb, 0, sizeof(skb->cb));
2081 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2083 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2084 enum ieee80211_vht_chanwidth bw,
2085 int mcs, bool ext_nss_bw_capable,
2086 unsigned int max_vht_nss)
2088 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2091 int i, mcs_encoding;
2096 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2106 /* find max_vht_nss for the given MCS */
2107 for (i = 7; i >= 0; i--) {
2108 int supp = (map >> (2 * i)) & 3;
2113 if (supp >= mcs_encoding) {
2114 max_vht_nss = i + 1;
2120 if (!(cap->supp_mcs.tx_mcs_map &
2121 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2124 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2125 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2126 supp_width = le32_get_bits(cap->vht_cap_info,
2127 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2129 /* if not capable, treat ext_nss_bw as 0 */
2130 if (!ext_nss_bw_capable)
2133 /* This is invalid */
2134 if (supp_width == 3)
2137 /* This is an invalid combination so pretend nothing is supported */
2138 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2142 * Cover all the special cases according to IEEE 802.11-2016
2143 * Table 9-250. All other cases are either factor of 1 or not
2147 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2148 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2149 if ((supp_width == 1 || supp_width == 2) &&
2151 return 2 * max_vht_nss;
2153 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2154 if (supp_width == 0 &&
2155 (ext_nss_bw == 1 || ext_nss_bw == 2))
2156 return max_vht_nss / 2;
2157 if (supp_width == 0 &&
2159 return (3 * max_vht_nss) / 4;
2160 if (supp_width == 1 &&
2162 return 2 * max_vht_nss;
2164 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2165 if (supp_width == 0 && ext_nss_bw == 1)
2166 return 0; /* not possible */
2167 if (supp_width == 0 &&
2169 return max_vht_nss / 2;
2170 if (supp_width == 0 &&
2172 return (3 * max_vht_nss) / 4;
2173 if (supp_width == 1 &&
2175 return 0; /* not possible */
2176 if (supp_width == 1 &&
2178 return max_vht_nss / 2;
2179 if (supp_width == 1 &&
2181 return (3 * max_vht_nss) / 4;
2185 /* not covered or invalid combination received */
2188 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2190 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2191 bool is_4addr, u8 check_swif)
2194 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2196 switch (check_swif) {
2198 if (is_vlan && is_4addr)
2199 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2200 return wiphy->interface_modes & BIT(iftype);
2202 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2203 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2204 return wiphy->software_iftypes & BIT(iftype);
2211 EXPORT_SYMBOL(cfg80211_iftype_allowed);