1 // SPDX-License-Identifier: GPL-2.0
3 * Wireless utility functions
5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2017 Intel Deutschland GmbH
8 * Copyright (C) 2018-2019 Intel Corporation
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
27 struct ieee80211_rate *
28 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
29 u32 basic_rates, int bitrate)
31 struct ieee80211_rate *result = &sband->bitrates[0];
34 for (i = 0; i < sband->n_bitrates; i++) {
35 if (!(basic_rates & BIT(i)))
37 if (sband->bitrates[i].bitrate > bitrate)
39 result = &sband->bitrates[i];
44 EXPORT_SYMBOL(ieee80211_get_response_rate);
46 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
47 enum nl80211_bss_scan_width scan_width)
49 struct ieee80211_rate *bitrates;
50 u32 mandatory_rates = 0;
51 enum ieee80211_rate_flags mandatory_flag;
57 if (sband->band == NL80211_BAND_2GHZ) {
58 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
59 scan_width == NL80211_BSS_CHAN_WIDTH_10)
60 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
62 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
64 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
67 bitrates = sband->bitrates;
68 for (i = 0; i < sband->n_bitrates; i++)
69 if (bitrates[i].flags & mandatory_flag)
70 mandatory_rates |= BIT(i);
71 return mandatory_rates;
73 EXPORT_SYMBOL(ieee80211_mandatory_rates);
75 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
77 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
80 return 0; /* not supported */
82 case NL80211_BAND_2GHZ:
86 return 2407 + chan * 5;
88 case NL80211_BAND_5GHZ:
89 if (chan >= 182 && chan <= 196)
90 return 4000 + chan * 5;
92 return 5000 + chan * 5;
94 case NL80211_BAND_6GHZ:
95 /* see 802.11ax D4.1 27.3.22.2 */
97 return 5940 + chan * 5;
99 case NL80211_BAND_60GHZ:
101 return 56160 + chan * 2160;
106 return 0; /* not supported */
108 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
110 int ieee80211_frequency_to_channel(int freq)
112 /* see 802.11 17.3.8.3.2 and Annex J */
115 else if (freq < 2484)
116 return (freq - 2407) / 5;
117 else if (freq >= 4910 && freq <= 4980)
118 return (freq - 4000) / 5;
119 else if (freq < 5940)
120 return (freq - 5000) / 5;
121 else if (freq <= 45000) /* DMG band lower limit */
122 /* see 802.11ax D4.1 27.3.22.2 */
123 return (freq - 5940) / 5;
124 else if (freq >= 58320 && freq <= 70200)
125 return (freq - 56160) / 2160;
129 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
131 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
133 enum nl80211_band band;
134 struct ieee80211_supported_band *sband;
137 for (band = 0; band < NUM_NL80211_BANDS; band++) {
138 sband = wiphy->bands[band];
143 for (i = 0; i < sband->n_channels; i++) {
144 if (sband->channels[i].center_freq == freq)
145 return &sband->channels[i];
151 EXPORT_SYMBOL(ieee80211_get_channel);
153 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
157 switch (sband->band) {
158 case NL80211_BAND_5GHZ:
159 case NL80211_BAND_6GHZ:
161 for (i = 0; i < sband->n_bitrates; i++) {
162 if (sband->bitrates[i].bitrate == 60 ||
163 sband->bitrates[i].bitrate == 120 ||
164 sband->bitrates[i].bitrate == 240) {
165 sband->bitrates[i].flags |=
166 IEEE80211_RATE_MANDATORY_A;
172 case NL80211_BAND_2GHZ:
174 for (i = 0; i < sband->n_bitrates; i++) {
175 switch (sband->bitrates[i].bitrate) {
180 sband->bitrates[i].flags |=
181 IEEE80211_RATE_MANDATORY_B |
182 IEEE80211_RATE_MANDATORY_G;
188 sband->bitrates[i].flags |=
189 IEEE80211_RATE_MANDATORY_G;
193 sband->bitrates[i].flags |=
194 IEEE80211_RATE_ERP_G;
198 WARN_ON(want != 0 && want != 3);
200 case NL80211_BAND_60GHZ:
201 /* check for mandatory HT MCS 1..4 */
202 WARN_ON(!sband->ht_cap.ht_supported);
203 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
205 case NUM_NL80211_BANDS:
212 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
214 enum nl80211_band band;
216 for (band = 0; band < NUM_NL80211_BANDS; band++)
217 if (wiphy->bands[band])
218 set_mandatory_flags_band(wiphy->bands[band]);
221 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
224 for (i = 0; i < wiphy->n_cipher_suites; i++)
225 if (cipher == wiphy->cipher_suites[i])
230 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
231 struct key_params *params, int key_idx,
232 bool pairwise, const u8 *mac_addr)
234 if (key_idx < 0 || key_idx > 5)
237 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
240 if (pairwise && !mac_addr)
243 switch (params->cipher) {
244 case WLAN_CIPHER_SUITE_TKIP:
245 case WLAN_CIPHER_SUITE_CCMP:
246 case WLAN_CIPHER_SUITE_CCMP_256:
247 case WLAN_CIPHER_SUITE_GCMP:
248 case WLAN_CIPHER_SUITE_GCMP_256:
249 /* IEEE802.11-2016 allows only 0 and - when using Extended Key
250 * ID - 1 as index for pairwise keys.
251 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
252 * the driver supports Extended Key ID.
253 * @NL80211_KEY_SET_TX can't be set when installing and
256 if (params->mode == NL80211_KEY_NO_TX) {
257 if (!wiphy_ext_feature_isset(&rdev->wiphy,
258 NL80211_EXT_FEATURE_EXT_KEY_ID))
260 else if (!pairwise || key_idx < 0 || key_idx > 1)
262 } else if ((pairwise && key_idx) ||
263 params->mode == NL80211_KEY_SET_TX) {
267 case WLAN_CIPHER_SUITE_AES_CMAC:
268 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
269 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
270 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
271 /* Disallow BIP (group-only) cipher as pairwise cipher */
277 case WLAN_CIPHER_SUITE_WEP40:
278 case WLAN_CIPHER_SUITE_WEP104:
285 switch (params->cipher) {
286 case WLAN_CIPHER_SUITE_WEP40:
287 if (params->key_len != WLAN_KEY_LEN_WEP40)
290 case WLAN_CIPHER_SUITE_TKIP:
291 if (params->key_len != WLAN_KEY_LEN_TKIP)
294 case WLAN_CIPHER_SUITE_CCMP:
295 if (params->key_len != WLAN_KEY_LEN_CCMP)
298 case WLAN_CIPHER_SUITE_CCMP_256:
299 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
302 case WLAN_CIPHER_SUITE_GCMP:
303 if (params->key_len != WLAN_KEY_LEN_GCMP)
306 case WLAN_CIPHER_SUITE_GCMP_256:
307 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
310 case WLAN_CIPHER_SUITE_WEP104:
311 if (params->key_len != WLAN_KEY_LEN_WEP104)
314 case WLAN_CIPHER_SUITE_AES_CMAC:
315 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
318 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
319 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
322 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
323 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
326 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
327 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
332 * We don't know anything about this algorithm,
333 * allow using it -- but the driver must check
334 * all parameters! We still check below whether
335 * or not the driver supports this algorithm,
342 switch (params->cipher) {
343 case WLAN_CIPHER_SUITE_WEP40:
344 case WLAN_CIPHER_SUITE_WEP104:
345 /* These ciphers do not use key sequence */
347 case WLAN_CIPHER_SUITE_TKIP:
348 case WLAN_CIPHER_SUITE_CCMP:
349 case WLAN_CIPHER_SUITE_CCMP_256:
350 case WLAN_CIPHER_SUITE_GCMP:
351 case WLAN_CIPHER_SUITE_GCMP_256:
352 case WLAN_CIPHER_SUITE_AES_CMAC:
353 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
354 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
355 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
356 if (params->seq_len != 6)
362 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
368 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
370 unsigned int hdrlen = 24;
372 if (ieee80211_is_data(fc)) {
373 if (ieee80211_has_a4(fc))
375 if (ieee80211_is_data_qos(fc)) {
376 hdrlen += IEEE80211_QOS_CTL_LEN;
377 if (ieee80211_has_order(fc))
378 hdrlen += IEEE80211_HT_CTL_LEN;
383 if (ieee80211_is_mgmt(fc)) {
384 if (ieee80211_has_order(fc))
385 hdrlen += IEEE80211_HT_CTL_LEN;
389 if (ieee80211_is_ctl(fc)) {
391 * ACK and CTS are 10 bytes, all others 16. To see how
392 * to get this condition consider
393 * subtype mask: 0b0000000011110000 (0x00F0)
394 * ACK subtype: 0b0000000011010000 (0x00D0)
395 * CTS subtype: 0b0000000011000000 (0x00C0)
396 * bits that matter: ^^^ (0x00E0)
397 * value of those: 0b0000000011000000 (0x00C0)
399 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
407 EXPORT_SYMBOL(ieee80211_hdrlen);
409 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
411 const struct ieee80211_hdr *hdr =
412 (const struct ieee80211_hdr *)skb->data;
415 if (unlikely(skb->len < 10))
417 hdrlen = ieee80211_hdrlen(hdr->frame_control);
418 if (unlikely(hdrlen > skb->len))
422 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
424 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
426 int ae = flags & MESH_FLAGS_AE;
427 /* 802.11-2012, 8.2.4.7.3 */
432 case MESH_FLAGS_AE_A4:
434 case MESH_FLAGS_AE_A5_A6:
439 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
441 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
443 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
445 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
446 const u8 *addr, enum nl80211_iftype iftype,
449 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
451 u8 hdr[ETH_ALEN] __aligned(2);
458 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
461 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
462 if (skb->len < hdrlen + 8)
465 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
467 * IEEE 802.11 address fields:
468 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
469 * 0 0 DA SA BSSID n/a
470 * 0 1 DA BSSID SA n/a
471 * 1 0 BSSID SA DA n/a
474 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
475 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
477 if (iftype == NL80211_IFTYPE_MESH_POINT)
478 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
480 mesh_flags &= MESH_FLAGS_AE;
482 switch (hdr->frame_control &
483 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
484 case cpu_to_le16(IEEE80211_FCTL_TODS):
485 if (unlikely(iftype != NL80211_IFTYPE_AP &&
486 iftype != NL80211_IFTYPE_AP_VLAN &&
487 iftype != NL80211_IFTYPE_P2P_GO))
490 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
491 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
492 iftype != NL80211_IFTYPE_MESH_POINT &&
493 iftype != NL80211_IFTYPE_AP_VLAN &&
494 iftype != NL80211_IFTYPE_STATION))
496 if (iftype == NL80211_IFTYPE_MESH_POINT) {
497 if (mesh_flags == MESH_FLAGS_AE_A4)
499 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
500 skb_copy_bits(skb, hdrlen +
501 offsetof(struct ieee80211s_hdr, eaddr1),
502 tmp.h_dest, 2 * ETH_ALEN);
504 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
507 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
508 if ((iftype != NL80211_IFTYPE_STATION &&
509 iftype != NL80211_IFTYPE_P2P_CLIENT &&
510 iftype != NL80211_IFTYPE_MESH_POINT) ||
511 (is_multicast_ether_addr(tmp.h_dest) &&
512 ether_addr_equal(tmp.h_source, addr)))
514 if (iftype == NL80211_IFTYPE_MESH_POINT) {
515 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
517 if (mesh_flags == MESH_FLAGS_AE_A4)
518 skb_copy_bits(skb, hdrlen +
519 offsetof(struct ieee80211s_hdr, eaddr1),
520 tmp.h_source, ETH_ALEN);
521 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
525 if (iftype != NL80211_IFTYPE_ADHOC &&
526 iftype != NL80211_IFTYPE_STATION &&
527 iftype != NL80211_IFTYPE_OCB)
532 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
533 tmp.h_proto = payload.proto;
535 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
536 tmp.h_proto != htons(ETH_P_AARP) &&
537 tmp.h_proto != htons(ETH_P_IPX)) ||
538 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
539 /* remove RFC1042 or Bridge-Tunnel encapsulation and
540 * replace EtherType */
541 hdrlen += ETH_ALEN + 2;
543 tmp.h_proto = htons(skb->len - hdrlen);
545 pskb_pull(skb, hdrlen);
548 ehdr = skb_push(skb, sizeof(struct ethhdr));
549 memcpy(ehdr, &tmp, sizeof(tmp));
553 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
556 __frame_add_frag(struct sk_buff *skb, struct page *page,
557 void *ptr, int len, int size)
559 struct skb_shared_info *sh = skb_shinfo(skb);
563 page_offset = ptr - page_address(page);
564 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
568 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
571 struct skb_shared_info *sh = skb_shinfo(skb);
572 const skb_frag_t *frag = &sh->frags[0];
573 struct page *frag_page;
575 int frag_len, frag_size;
576 int head_size = skb->len - skb->data_len;
579 frag_page = virt_to_head_page(skb->head);
580 frag_ptr = skb->data;
581 frag_size = head_size;
583 while (offset >= frag_size) {
585 frag_page = skb_frag_page(frag);
586 frag_ptr = skb_frag_address(frag);
587 frag_size = skb_frag_size(frag);
592 frag_len = frag_size - offset;
594 cur_len = min(len, frag_len);
596 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
600 frag_len = skb_frag_size(frag);
601 cur_len = min(len, frag_len);
602 __frame_add_frag(frame, skb_frag_page(frag),
603 skb_frag_address(frag), cur_len, frag_len);
609 static struct sk_buff *
610 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
611 int offset, int len, bool reuse_frag)
613 struct sk_buff *frame;
616 if (skb->len - offset < len)
620 * When reusing framents, copy some data to the head to simplify
621 * ethernet header handling and speed up protocol header processing
622 * in the stack later.
625 cur_len = min_t(int, len, 32);
628 * Allocate and reserve two bytes more for payload
629 * alignment since sizeof(struct ethhdr) is 14.
631 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
635 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
636 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
643 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
648 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
649 const u8 *addr, enum nl80211_iftype iftype,
650 const unsigned int extra_headroom,
651 const u8 *check_da, const u8 *check_sa)
653 unsigned int hlen = ALIGN(extra_headroom, 4);
654 struct sk_buff *frame = NULL;
657 int offset = 0, remaining;
659 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
660 bool reuse_skb = false;
664 unsigned int subframe_len;
668 skb_copy_bits(skb, offset, ð, sizeof(eth));
669 len = ntohs(eth.h_proto);
670 subframe_len = sizeof(struct ethhdr) + len;
671 padding = (4 - subframe_len) & 0x3;
673 /* the last MSDU has no padding */
674 remaining = skb->len - offset;
675 if (subframe_len > remaining)
678 offset += sizeof(struct ethhdr);
679 last = remaining <= subframe_len + padding;
681 /* FIXME: should we really accept multicast DA? */
682 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
683 !ether_addr_equal(check_da, eth.h_dest)) ||
684 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
685 offset += len + padding;
689 /* reuse skb for the last subframe */
690 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
691 skb_pull(skb, offset);
695 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
700 offset += len + padding;
703 skb_reset_network_header(frame);
704 frame->dev = skb->dev;
705 frame->priority = skb->priority;
707 payload = frame->data;
708 ethertype = (payload[6] << 8) | payload[7];
709 if (likely((ether_addr_equal(payload, rfc1042_header) &&
710 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
711 ether_addr_equal(payload, bridge_tunnel_header))) {
712 eth.h_proto = htons(ethertype);
713 skb_pull(frame, ETH_ALEN + 2);
716 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
717 __skb_queue_tail(list, frame);
726 __skb_queue_purge(list);
729 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
731 /* Given a data frame determine the 802.1p/1d tag to use. */
732 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
733 struct cfg80211_qos_map *qos_map)
736 unsigned char vlan_priority;
739 /* skb->priority values from 256->263 are magic values to
740 * directly indicate a specific 802.1d priority. This is used
741 * to allow 802.1d priority to be passed directly in from VLAN
744 if (skb->priority >= 256 && skb->priority <= 263) {
745 ret = skb->priority - 256;
749 if (skb_vlan_tag_present(skb)) {
750 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
752 if (vlan_priority > 0) {
758 switch (skb->protocol) {
759 case htons(ETH_P_IP):
760 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
762 case htons(ETH_P_IPV6):
763 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
765 case htons(ETH_P_MPLS_UC):
766 case htons(ETH_P_MPLS_MC): {
767 struct mpls_label mpls_tmp, *mpls;
769 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
770 sizeof(*mpls), &mpls_tmp);
774 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
778 case htons(ETH_P_80221):
779 /* 802.21 is always network control traffic */
786 unsigned int i, tmp_dscp = dscp >> 2;
788 for (i = 0; i < qos_map->num_des; i++) {
789 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
790 ret = qos_map->dscp_exception[i].up;
795 for (i = 0; i < 8; i++) {
796 if (tmp_dscp >= qos_map->up[i].low &&
797 tmp_dscp <= qos_map->up[i].high) {
806 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
808 EXPORT_SYMBOL(cfg80211_classify8021d);
810 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
812 const struct cfg80211_bss_ies *ies;
814 ies = rcu_dereference(bss->ies);
818 return cfg80211_find_elem(id, ies->data, ies->len);
820 EXPORT_SYMBOL(ieee80211_bss_get_elem);
822 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
824 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
825 struct net_device *dev = wdev->netdev;
828 if (!wdev->connect_keys)
831 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
832 if (!wdev->connect_keys->params[i].cipher)
834 if (rdev_add_key(rdev, dev, i, false, NULL,
835 &wdev->connect_keys->params[i])) {
836 netdev_err(dev, "failed to set key %d\n", i);
839 if (wdev->connect_keys->def == i &&
840 rdev_set_default_key(rdev, dev, i, true, true)) {
841 netdev_err(dev, "failed to set defkey %d\n", i);
846 kzfree(wdev->connect_keys);
847 wdev->connect_keys = NULL;
850 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
852 struct cfg80211_event *ev;
855 spin_lock_irqsave(&wdev->event_lock, flags);
856 while (!list_empty(&wdev->event_list)) {
857 ev = list_first_entry(&wdev->event_list,
858 struct cfg80211_event, list);
860 spin_unlock_irqrestore(&wdev->event_lock, flags);
864 case EVENT_CONNECT_RESULT:
865 __cfg80211_connect_result(
868 ev->cr.status == WLAN_STATUS_SUCCESS);
871 __cfg80211_roamed(wdev, &ev->rm);
873 case EVENT_DISCONNECTED:
874 __cfg80211_disconnected(wdev->netdev,
875 ev->dc.ie, ev->dc.ie_len,
877 !ev->dc.locally_generated);
879 case EVENT_IBSS_JOINED:
880 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
884 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
886 case EVENT_PORT_AUTHORIZED:
887 __cfg80211_port_authorized(wdev, ev->pa.bssid);
894 spin_lock_irqsave(&wdev->event_lock, flags);
896 spin_unlock_irqrestore(&wdev->event_lock, flags);
899 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
901 struct wireless_dev *wdev;
905 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
906 cfg80211_process_wdev_events(wdev);
909 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
910 struct net_device *dev, enum nl80211_iftype ntype,
911 struct vif_params *params)
914 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
918 /* don't support changing VLANs, you just re-create them */
919 if (otype == NL80211_IFTYPE_AP_VLAN)
922 /* cannot change into P2P device or NAN */
923 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
924 ntype == NL80211_IFTYPE_NAN)
927 if (!rdev->ops->change_virtual_intf ||
928 !(rdev->wiphy.interface_modes & (1 << ntype)))
931 /* if it's part of a bridge, reject changing type to station/ibss */
932 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
933 (ntype == NL80211_IFTYPE_ADHOC ||
934 ntype == NL80211_IFTYPE_STATION ||
935 ntype == NL80211_IFTYPE_P2P_CLIENT))
938 if (ntype != otype) {
939 dev->ieee80211_ptr->use_4addr = false;
940 dev->ieee80211_ptr->mesh_id_up_len = 0;
941 wdev_lock(dev->ieee80211_ptr);
942 rdev_set_qos_map(rdev, dev, NULL);
943 wdev_unlock(dev->ieee80211_ptr);
946 case NL80211_IFTYPE_AP:
947 cfg80211_stop_ap(rdev, dev, true);
949 case NL80211_IFTYPE_ADHOC:
950 cfg80211_leave_ibss(rdev, dev, false);
952 case NL80211_IFTYPE_STATION:
953 case NL80211_IFTYPE_P2P_CLIENT:
954 wdev_lock(dev->ieee80211_ptr);
955 cfg80211_disconnect(rdev, dev,
956 WLAN_REASON_DEAUTH_LEAVING, true);
957 wdev_unlock(dev->ieee80211_ptr);
959 case NL80211_IFTYPE_MESH_POINT:
960 /* mesh should be handled? */
966 cfg80211_process_rdev_events(rdev);
969 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
971 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
973 if (!err && params && params->use_4addr != -1)
974 dev->ieee80211_ptr->use_4addr = params->use_4addr;
977 dev->priv_flags &= ~IFF_DONT_BRIDGE;
979 case NL80211_IFTYPE_STATION:
980 if (dev->ieee80211_ptr->use_4addr)
983 case NL80211_IFTYPE_OCB:
984 case NL80211_IFTYPE_P2P_CLIENT:
985 case NL80211_IFTYPE_ADHOC:
986 dev->priv_flags |= IFF_DONT_BRIDGE;
988 case NL80211_IFTYPE_P2P_GO:
989 case NL80211_IFTYPE_AP:
990 case NL80211_IFTYPE_AP_VLAN:
991 case NL80211_IFTYPE_WDS:
992 case NL80211_IFTYPE_MESH_POINT:
995 case NL80211_IFTYPE_MONITOR:
996 /* monitor can't bridge anyway */
998 case NL80211_IFTYPE_UNSPECIFIED:
999 case NUM_NL80211_IFTYPES:
1002 case NL80211_IFTYPE_P2P_DEVICE:
1003 case NL80211_IFTYPE_NAN:
1009 if (!err && ntype != otype && netif_running(dev)) {
1010 cfg80211_update_iface_num(rdev, ntype, 1);
1011 cfg80211_update_iface_num(rdev, otype, -1);
1017 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1019 int modulation, streams, bitrate;
1021 /* the formula below does only work for MCS values smaller than 32 */
1022 if (WARN_ON_ONCE(rate->mcs >= 32))
1025 modulation = rate->mcs & 7;
1026 streams = (rate->mcs >> 3) + 1;
1028 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1031 bitrate *= (modulation + 1);
1032 else if (modulation == 4)
1033 bitrate *= (modulation + 2);
1035 bitrate *= (modulation + 3);
1039 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1040 bitrate = (bitrate / 9) * 10;
1042 /* do NOT round down here */
1043 return (bitrate + 50000) / 100000;
1046 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1048 static const u32 __mcs2bitrate[] = {
1056 [5] = 12512, /* 1251.25 mbps */
1066 [14] = 8662, /* 866.25 mbps */
1076 [24] = 67568, /* 6756.75 mbps */
1087 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1090 return __mcs2bitrate[rate->mcs];
1093 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1095 static const u32 base[4][10] = {
1105 /* not in the spec, but some devices use this: */
1149 case RATE_INFO_BW_160:
1152 case RATE_INFO_BW_80:
1155 case RATE_INFO_BW_40:
1158 case RATE_INFO_BW_5:
1159 case RATE_INFO_BW_10:
1162 case RATE_INFO_BW_20:
1166 bitrate = base[idx][rate->mcs];
1167 bitrate *= rate->nss;
1169 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1170 bitrate = (bitrate / 9) * 10;
1172 /* do NOT round down here */
1173 return (bitrate + 50000) / 100000;
1175 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1176 rate->bw, rate->mcs, rate->nss);
1180 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1183 u16 mcs_divisors[12] = {
1184 34133, /* 16.666666... */
1185 17067, /* 8.333333... */
1186 11378, /* 5.555555... */
1187 8533, /* 4.166666... */
1188 5689, /* 2.777777... */
1189 4267, /* 2.083333... */
1190 3923, /* 1.851851... */
1191 3413, /* 1.666666... */
1192 2844, /* 1.388888... */
1193 2560, /* 1.250000... */
1194 2276, /* 1.111111... */
1195 2048, /* 1.000000... */
1197 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1198 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1199 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1200 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1201 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1202 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1203 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1207 if (WARN_ON_ONCE(rate->mcs > 11))
1210 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1212 if (WARN_ON_ONCE(rate->he_ru_alloc >
1213 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1215 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1218 if (rate->bw == RATE_INFO_BW_160)
1219 result = rates_160M[rate->he_gi];
1220 else if (rate->bw == RATE_INFO_BW_80 ||
1221 (rate->bw == RATE_INFO_BW_HE_RU &&
1222 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1223 result = rates_969[rate->he_gi];
1224 else if (rate->bw == RATE_INFO_BW_40 ||
1225 (rate->bw == RATE_INFO_BW_HE_RU &&
1226 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1227 result = rates_484[rate->he_gi];
1228 else if (rate->bw == RATE_INFO_BW_20 ||
1229 (rate->bw == RATE_INFO_BW_HE_RU &&
1230 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1231 result = rates_242[rate->he_gi];
1232 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1233 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1234 result = rates_106[rate->he_gi];
1235 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1236 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1237 result = rates_52[rate->he_gi];
1238 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1239 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1240 result = rates_26[rate->he_gi];
1242 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1243 rate->bw, rate->he_ru_alloc);
1247 /* now scale to the appropriate MCS */
1250 do_div(tmp, mcs_divisors[rate->mcs]);
1253 /* and take NSS, DCM into account */
1254 result = (result * rate->nss) / 8;
1258 return result / 10000;
1261 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1263 if (rate->flags & RATE_INFO_FLAGS_MCS)
1264 return cfg80211_calculate_bitrate_ht(rate);
1265 if (rate->flags & RATE_INFO_FLAGS_60G)
1266 return cfg80211_calculate_bitrate_60g(rate);
1267 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1268 return cfg80211_calculate_bitrate_vht(rate);
1269 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1270 return cfg80211_calculate_bitrate_he(rate);
1272 return rate->legacy;
1274 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1276 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1277 enum ieee80211_p2p_attr_id attr,
1278 u8 *buf, unsigned int bufsize)
1281 u16 attr_remaining = 0;
1282 bool desired_attr = false;
1283 u16 desired_len = 0;
1286 unsigned int iedatalen;
1293 if (iedatalen + 2 > len)
1296 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1304 /* check WFA OUI, P2P subtype */
1305 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1306 iedata[2] != 0x9a || iedata[3] != 0x09)
1312 /* check attribute continuation into this IE */
1313 copy = min_t(unsigned int, attr_remaining, iedatalen);
1314 if (copy && desired_attr) {
1315 desired_len += copy;
1317 memcpy(out, iedata, min(bufsize, copy));
1318 out += min(bufsize, copy);
1319 bufsize -= min(bufsize, copy);
1323 if (copy == attr_remaining)
1327 attr_remaining -= copy;
1334 while (iedatalen > 0) {
1337 /* P2P attribute ID & size must fit */
1340 desired_attr = iedata[0] == attr;
1341 attr_len = get_unaligned_le16(iedata + 1);
1345 copy = min_t(unsigned int, attr_len, iedatalen);
1348 desired_len += copy;
1350 memcpy(out, iedata, min(bufsize, copy));
1351 out += min(bufsize, copy);
1352 bufsize -= min(bufsize, copy);
1355 if (copy == attr_len)
1361 attr_remaining = attr_len - copy;
1369 if (attr_remaining && desired_attr)
1374 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1376 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1380 /* Make sure array values are legal */
1381 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1386 if (ids[i] == WLAN_EID_EXTENSION) {
1387 if (id_ext && (ids[i + 1] == id))
1394 if (ids[i] == id && !id_ext)
1402 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1404 /* we assume a validly formed IEs buffer */
1405 u8 len = ies[pos + 1];
1409 /* the IE itself must have 255 bytes for fragments to follow */
1413 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1421 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1422 const u8 *ids, int n_ids,
1423 const u8 *after_ric, int n_after_ric,
1426 size_t pos = offset;
1428 while (pos < ielen) {
1431 if (ies[pos] == WLAN_EID_EXTENSION)
1433 if ((pos + ext) >= ielen)
1436 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1437 ies[pos] == WLAN_EID_EXTENSION))
1440 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1441 pos = skip_ie(ies, ielen, pos);
1443 while (pos < ielen) {
1444 if (ies[pos] == WLAN_EID_EXTENSION)
1449 if ((pos + ext) >= ielen)
1452 if (!ieee80211_id_in_list(after_ric,
1456 pos = skip_ie(ies, ielen, pos);
1461 pos = skip_ie(ies, ielen, pos);
1467 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1469 bool ieee80211_operating_class_to_band(u8 operating_class,
1470 enum nl80211_band *band)
1472 switch (operating_class) {
1476 *band = NL80211_BAND_5GHZ;
1479 *band = NL80211_BAND_6GHZ;
1485 *band = NL80211_BAND_2GHZ;
1488 *band = NL80211_BAND_60GHZ;
1494 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1496 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1500 u32 freq = chandef->center_freq1;
1502 if (freq >= 2412 && freq <= 2472) {
1503 if (chandef->width > NL80211_CHAN_WIDTH_40)
1506 /* 2.407 GHz, channels 1..13 */
1507 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1508 if (freq > chandef->chan->center_freq)
1509 *op_class = 83; /* HT40+ */
1511 *op_class = 84; /* HT40- */
1520 if (chandef->width > NL80211_CHAN_WIDTH_40)
1523 *op_class = 82; /* channel 14 */
1527 switch (chandef->width) {
1528 case NL80211_CHAN_WIDTH_80:
1531 case NL80211_CHAN_WIDTH_160:
1534 case NL80211_CHAN_WIDTH_80P80:
1537 case NL80211_CHAN_WIDTH_10:
1538 case NL80211_CHAN_WIDTH_5:
1539 return false; /* unsupported for now */
1545 /* 5 GHz, channels 36..48 */
1546 if (freq >= 5180 && freq <= 5240) {
1548 *op_class = vht_opclass;
1549 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1550 if (freq > chandef->chan->center_freq)
1561 /* 5 GHz, channels 52..64 */
1562 if (freq >= 5260 && freq <= 5320) {
1564 *op_class = vht_opclass;
1565 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1566 if (freq > chandef->chan->center_freq)
1577 /* 5 GHz, channels 100..144 */
1578 if (freq >= 5500 && freq <= 5720) {
1580 *op_class = vht_opclass;
1581 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1582 if (freq > chandef->chan->center_freq)
1593 /* 5 GHz, channels 149..169 */
1594 if (freq >= 5745 && freq <= 5845) {
1596 *op_class = vht_opclass;
1597 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1598 if (freq > chandef->chan->center_freq)
1602 } else if (freq <= 5805) {
1611 /* 56.16 GHz, channel 1..4 */
1612 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1613 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1620 /* not supported yet */
1623 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1625 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1626 u32 *beacon_int_gcd,
1627 bool *beacon_int_different)
1629 struct wireless_dev *wdev;
1631 *beacon_int_gcd = 0;
1632 *beacon_int_different = false;
1634 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1635 if (!wdev->beacon_interval)
1638 if (!*beacon_int_gcd) {
1639 *beacon_int_gcd = wdev->beacon_interval;
1643 if (wdev->beacon_interval == *beacon_int_gcd)
1646 *beacon_int_different = true;
1647 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1650 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1651 if (*beacon_int_gcd)
1652 *beacon_int_different = true;
1653 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1657 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1658 enum nl80211_iftype iftype, u32 beacon_int)
1661 * This is just a basic pre-condition check; if interface combinations
1662 * are possible the driver must already be checking those with a call
1663 * to cfg80211_check_combinations(), in which case we'll validate more
1664 * through the cfg80211_calculate_bi_data() call and code in
1665 * cfg80211_iter_combinations().
1668 if (beacon_int < 10 || beacon_int > 10000)
1674 int cfg80211_iter_combinations(struct wiphy *wiphy,
1675 struct iface_combination_params *params,
1676 void (*iter)(const struct ieee80211_iface_combination *c,
1680 const struct ieee80211_regdomain *regdom;
1681 enum nl80211_dfs_regions region = 0;
1683 int num_interfaces = 0;
1684 u32 used_iftypes = 0;
1686 bool beacon_int_different;
1689 * This is a bit strange, since the iteration used to rely only on
1690 * the data given by the driver, but here it now relies on context,
1691 * in form of the currently operating interfaces.
1692 * This is OK for all current users, and saves us from having to
1693 * push the GCD calculations into all the drivers.
1694 * In the future, this should probably rely more on data that's in
1695 * cfg80211 already - the only thing not would appear to be any new
1696 * interfaces (while being brought up) and channel/radar data.
1698 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1699 &beacon_int_gcd, &beacon_int_different);
1701 if (params->radar_detect) {
1703 regdom = rcu_dereference(cfg80211_regdomain);
1705 region = regdom->dfs_region;
1709 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1710 num_interfaces += params->iftype_num[iftype];
1711 if (params->iftype_num[iftype] > 0 &&
1712 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1713 used_iftypes |= BIT(iftype);
1716 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1717 const struct ieee80211_iface_combination *c;
1718 struct ieee80211_iface_limit *limits;
1719 u32 all_iftypes = 0;
1721 c = &wiphy->iface_combinations[i];
1723 if (num_interfaces > c->max_interfaces)
1725 if (params->num_different_channels > c->num_different_channels)
1728 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1733 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1734 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1736 for (j = 0; j < c->n_limits; j++) {
1737 all_iftypes |= limits[j].types;
1738 if (!(limits[j].types & BIT(iftype)))
1740 if (limits[j].max < params->iftype_num[iftype])
1742 limits[j].max -= params->iftype_num[iftype];
1746 if (params->radar_detect !=
1747 (c->radar_detect_widths & params->radar_detect))
1750 if (params->radar_detect && c->radar_detect_regions &&
1751 !(c->radar_detect_regions & BIT(region)))
1754 /* Finally check that all iftypes that we're currently
1755 * using are actually part of this combination. If they
1756 * aren't then we can't use this combination and have
1757 * to continue to the next.
1759 if ((all_iftypes & used_iftypes) != used_iftypes)
1762 if (beacon_int_gcd) {
1763 if (c->beacon_int_min_gcd &&
1764 beacon_int_gcd < c->beacon_int_min_gcd)
1766 if (!c->beacon_int_min_gcd && beacon_int_different)
1770 /* This combination covered all interface types and
1771 * supported the requested numbers, so we're good.
1781 EXPORT_SYMBOL(cfg80211_iter_combinations);
1784 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1791 int cfg80211_check_combinations(struct wiphy *wiphy,
1792 struct iface_combination_params *params)
1796 err = cfg80211_iter_combinations(wiphy, params,
1797 cfg80211_iter_sum_ifcombs, &num);
1805 EXPORT_SYMBOL(cfg80211_check_combinations);
1807 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1808 const u8 *rates, unsigned int n_rates,
1816 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1821 for (i = 0; i < n_rates; i++) {
1822 int rate = (rates[i] & 0x7f) * 5;
1825 for (j = 0; j < sband->n_bitrates; j++) {
1826 if (sband->bitrates[j].bitrate == rate) {
1837 * mask must have at least one bit set here since we
1838 * didn't accept a 0-length rates array nor allowed
1839 * entries in the array that didn't exist
1845 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1847 enum nl80211_band band;
1848 unsigned int n_channels = 0;
1850 for (band = 0; band < NUM_NL80211_BANDS; band++)
1851 if (wiphy->bands[band])
1852 n_channels += wiphy->bands[band]->n_channels;
1856 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1858 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1859 struct station_info *sinfo)
1861 struct cfg80211_registered_device *rdev;
1862 struct wireless_dev *wdev;
1864 wdev = dev->ieee80211_ptr;
1868 rdev = wiphy_to_rdev(wdev->wiphy);
1869 if (!rdev->ops->get_station)
1872 memset(sinfo, 0, sizeof(*sinfo));
1874 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1876 EXPORT_SYMBOL(cfg80211_get_station);
1878 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1885 kfree(f->serv_spec_info);
1888 for (i = 0; i < f->num_rx_filters; i++)
1889 kfree(f->rx_filters[i].filter);
1891 for (i = 0; i < f->num_tx_filters; i++)
1892 kfree(f->tx_filters[i].filter);
1894 kfree(f->rx_filters);
1895 kfree(f->tx_filters);
1898 EXPORT_SYMBOL(cfg80211_free_nan_func);
1900 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1901 u32 center_freq_khz, u32 bw_khz)
1903 u32 start_freq_khz, end_freq_khz;
1905 start_freq_khz = center_freq_khz - (bw_khz / 2);
1906 end_freq_khz = center_freq_khz + (bw_khz / 2);
1908 if (start_freq_khz >= freq_range->start_freq_khz &&
1909 end_freq_khz <= freq_range->end_freq_khz)
1915 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1917 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1918 sizeof(*(sinfo->pertid)),
1925 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1927 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1928 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1929 const unsigned char rfc1042_header[] __aligned(2) =
1930 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1931 EXPORT_SYMBOL(rfc1042_header);
1933 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1934 const unsigned char bridge_tunnel_header[] __aligned(2) =
1935 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1936 EXPORT_SYMBOL(bridge_tunnel_header);
1938 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1939 struct iapp_layer2_update {
1940 u8 da[ETH_ALEN]; /* broadcast */
1941 u8 sa[ETH_ALEN]; /* STA addr */
1949 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1951 struct iapp_layer2_update *msg;
1952 struct sk_buff *skb;
1954 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1957 skb = dev_alloc_skb(sizeof(*msg));
1960 msg = skb_put(skb, sizeof(*msg));
1962 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1963 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1965 eth_broadcast_addr(msg->da);
1966 ether_addr_copy(msg->sa, addr);
1967 msg->len = htons(6);
1969 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1970 msg->control = 0xaf; /* XID response lsb.1111F101.
1971 * F=0 (no poll command; unsolicited frame) */
1972 msg->xid_info[0] = 0x81; /* XID format identifier */
1973 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1974 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1977 skb->protocol = eth_type_trans(skb, dev);
1978 memset(skb->cb, 0, sizeof(skb->cb));
1981 EXPORT_SYMBOL(cfg80211_send_layer2_update);
1983 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1984 enum ieee80211_vht_chanwidth bw,
1985 int mcs, bool ext_nss_bw_capable)
1987 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1988 int max_vht_nss = 0;
1991 int i, mcs_encoding;
1996 if (WARN_ON(mcs > 9))
2005 /* find max_vht_nss for the given MCS */
2006 for (i = 7; i >= 0; i--) {
2007 int supp = (map >> (2 * i)) & 3;
2012 if (supp >= mcs_encoding) {
2013 max_vht_nss = i + 1;
2018 if (!(cap->supp_mcs.tx_mcs_map &
2019 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2022 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2023 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2024 supp_width = le32_get_bits(cap->vht_cap_info,
2025 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2027 /* if not capable, treat ext_nss_bw as 0 */
2028 if (!ext_nss_bw_capable)
2031 /* This is invalid */
2032 if (supp_width == 3)
2035 /* This is an invalid combination so pretend nothing is supported */
2036 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2040 * Cover all the special cases according to IEEE 802.11-2016
2041 * Table 9-250. All other cases are either factor of 1 or not
2045 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2046 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2047 if ((supp_width == 1 || supp_width == 2) &&
2049 return 2 * max_vht_nss;
2051 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2052 if (supp_width == 0 &&
2053 (ext_nss_bw == 1 || ext_nss_bw == 2))
2054 return max_vht_nss / 2;
2055 if (supp_width == 0 &&
2057 return (3 * max_vht_nss) / 4;
2058 if (supp_width == 1 &&
2060 return 2 * max_vht_nss;
2062 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2063 if (supp_width == 0 && ext_nss_bw == 1)
2064 return 0; /* not possible */
2065 if (supp_width == 0 &&
2067 return max_vht_nss / 2;
2068 if (supp_width == 0 &&
2070 return (3 * max_vht_nss) / 4;
2071 if (supp_width == 1 &&
2073 return 0; /* not possible */
2074 if (supp_width == 1 &&
2076 return max_vht_nss / 2;
2077 if (supp_width == 1 &&
2079 return (3 * max_vht_nss) / 4;
2083 /* not covered or invalid combination received */
2086 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2088 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2089 bool is_4addr, u8 check_swif)
2092 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2094 switch (check_swif) {
2096 if (is_vlan && is_4addr)
2097 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2098 return wiphy->interface_modes & BIT(iftype);
2100 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2101 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2102 return wiphy->software_iftypes & BIT(iftype);
2109 EXPORT_SYMBOL(cfg80211_iftype_allowed);