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_60GHZ:
96 return 56160 + chan * 2160;
101 return 0; /* not supported */
103 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
105 int ieee80211_frequency_to_channel(int freq)
107 /* see 802.11 17.3.8.3.2 and Annex J */
110 else if (freq < 2484)
111 return (freq - 2407) / 5;
112 else if (freq >= 4910 && freq <= 4980)
113 return (freq - 4000) / 5;
114 else if (freq <= 45000) /* DMG band lower limit */
115 return (freq - 5000) / 5;
116 else if (freq >= 58320 && freq <= 70200)
117 return (freq - 56160) / 2160;
121 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
123 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
125 enum nl80211_band band;
126 struct ieee80211_supported_band *sband;
129 for (band = 0; band < NUM_NL80211_BANDS; band++) {
130 sband = wiphy->bands[band];
135 for (i = 0; i < sband->n_channels; i++) {
136 if (sband->channels[i].center_freq == freq)
137 return &sband->channels[i];
143 EXPORT_SYMBOL(ieee80211_get_channel);
145 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
149 switch (sband->band) {
150 case NL80211_BAND_5GHZ:
152 for (i = 0; i < sband->n_bitrates; i++) {
153 if (sband->bitrates[i].bitrate == 60 ||
154 sband->bitrates[i].bitrate == 120 ||
155 sband->bitrates[i].bitrate == 240) {
156 sband->bitrates[i].flags |=
157 IEEE80211_RATE_MANDATORY_A;
163 case NL80211_BAND_2GHZ:
165 for (i = 0; i < sband->n_bitrates; i++) {
166 switch (sband->bitrates[i].bitrate) {
171 sband->bitrates[i].flags |=
172 IEEE80211_RATE_MANDATORY_B |
173 IEEE80211_RATE_MANDATORY_G;
179 sband->bitrates[i].flags |=
180 IEEE80211_RATE_MANDATORY_G;
184 sband->bitrates[i].flags |=
185 IEEE80211_RATE_ERP_G;
189 WARN_ON(want != 0 && want != 3);
191 case NL80211_BAND_60GHZ:
192 /* check for mandatory HT MCS 1..4 */
193 WARN_ON(!sband->ht_cap.ht_supported);
194 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
196 case NUM_NL80211_BANDS:
203 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
205 enum nl80211_band band;
207 for (band = 0; band < NUM_NL80211_BANDS; band++)
208 if (wiphy->bands[band])
209 set_mandatory_flags_band(wiphy->bands[band]);
212 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
215 for (i = 0; i < wiphy->n_cipher_suites; i++)
216 if (cipher == wiphy->cipher_suites[i])
221 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
222 struct key_params *params, int key_idx,
223 bool pairwise, const u8 *mac_addr)
225 if (key_idx < 0 || key_idx > 5)
228 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
231 if (pairwise && !mac_addr)
234 switch (params->cipher) {
235 case WLAN_CIPHER_SUITE_TKIP:
236 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
237 if ((pairwise && key_idx) ||
238 params->mode != NL80211_KEY_RX_TX)
241 case WLAN_CIPHER_SUITE_CCMP:
242 case WLAN_CIPHER_SUITE_CCMP_256:
243 case WLAN_CIPHER_SUITE_GCMP:
244 case WLAN_CIPHER_SUITE_GCMP_256:
245 /* IEEE802.11-2016 allows only 0 and - when supporting
246 * Extended Key ID - 1 as index for pairwise keys.
247 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
248 * the driver supports Extended Key ID.
249 * @NL80211_KEY_SET_TX can't be set when installing and
252 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
253 params->mode == NL80211_KEY_SET_TX)
255 if (wiphy_ext_feature_isset(&rdev->wiphy,
256 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
257 if (pairwise && (key_idx < 0 || key_idx > 1))
259 } else if (pairwise && key_idx) {
263 case WLAN_CIPHER_SUITE_AES_CMAC:
264 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
265 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
266 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
267 /* Disallow BIP (group-only) cipher as pairwise cipher */
273 case WLAN_CIPHER_SUITE_WEP40:
274 case WLAN_CIPHER_SUITE_WEP104:
281 switch (params->cipher) {
282 case WLAN_CIPHER_SUITE_WEP40:
283 if (params->key_len != WLAN_KEY_LEN_WEP40)
286 case WLAN_CIPHER_SUITE_TKIP:
287 if (params->key_len != WLAN_KEY_LEN_TKIP)
290 case WLAN_CIPHER_SUITE_CCMP:
291 if (params->key_len != WLAN_KEY_LEN_CCMP)
294 case WLAN_CIPHER_SUITE_CCMP_256:
295 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
298 case WLAN_CIPHER_SUITE_GCMP:
299 if (params->key_len != WLAN_KEY_LEN_GCMP)
302 case WLAN_CIPHER_SUITE_GCMP_256:
303 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
306 case WLAN_CIPHER_SUITE_WEP104:
307 if (params->key_len != WLAN_KEY_LEN_WEP104)
310 case WLAN_CIPHER_SUITE_AES_CMAC:
311 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
314 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
315 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
318 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
319 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
322 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
323 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
328 * We don't know anything about this algorithm,
329 * allow using it -- but the driver must check
330 * all parameters! We still check below whether
331 * or not the driver supports this algorithm,
338 switch (params->cipher) {
339 case WLAN_CIPHER_SUITE_WEP40:
340 case WLAN_CIPHER_SUITE_WEP104:
341 /* These ciphers do not use key sequence */
343 case WLAN_CIPHER_SUITE_TKIP:
344 case WLAN_CIPHER_SUITE_CCMP:
345 case WLAN_CIPHER_SUITE_CCMP_256:
346 case WLAN_CIPHER_SUITE_GCMP:
347 case WLAN_CIPHER_SUITE_GCMP_256:
348 case WLAN_CIPHER_SUITE_AES_CMAC:
349 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
350 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
351 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
352 if (params->seq_len != 6)
358 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
364 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
366 unsigned int hdrlen = 24;
368 if (ieee80211_is_data(fc)) {
369 if (ieee80211_has_a4(fc))
371 if (ieee80211_is_data_qos(fc)) {
372 hdrlen += IEEE80211_QOS_CTL_LEN;
373 if (ieee80211_has_order(fc))
374 hdrlen += IEEE80211_HT_CTL_LEN;
379 if (ieee80211_is_mgmt(fc)) {
380 if (ieee80211_has_order(fc))
381 hdrlen += IEEE80211_HT_CTL_LEN;
385 if (ieee80211_is_ctl(fc)) {
387 * ACK and CTS are 10 bytes, all others 16. To see how
388 * to get this condition consider
389 * subtype mask: 0b0000000011110000 (0x00F0)
390 * ACK subtype: 0b0000000011010000 (0x00D0)
391 * CTS subtype: 0b0000000011000000 (0x00C0)
392 * bits that matter: ^^^ (0x00E0)
393 * value of those: 0b0000000011000000 (0x00C0)
395 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
403 EXPORT_SYMBOL(ieee80211_hdrlen);
405 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
407 const struct ieee80211_hdr *hdr =
408 (const struct ieee80211_hdr *)skb->data;
411 if (unlikely(skb->len < 10))
413 hdrlen = ieee80211_hdrlen(hdr->frame_control);
414 if (unlikely(hdrlen > skb->len))
418 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
420 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
422 int ae = flags & MESH_FLAGS_AE;
423 /* 802.11-2012, 8.2.4.7.3 */
428 case MESH_FLAGS_AE_A4:
430 case MESH_FLAGS_AE_A5_A6:
435 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
437 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
439 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
441 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
442 const u8 *addr, enum nl80211_iftype iftype,
445 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
447 u8 hdr[ETH_ALEN] __aligned(2);
454 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
457 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
458 if (skb->len < hdrlen + 8)
461 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
463 * IEEE 802.11 address fields:
464 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
465 * 0 0 DA SA BSSID n/a
466 * 0 1 DA BSSID SA n/a
467 * 1 0 BSSID SA DA n/a
470 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
471 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
473 if (iftype == NL80211_IFTYPE_MESH_POINT)
474 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
476 mesh_flags &= MESH_FLAGS_AE;
478 switch (hdr->frame_control &
479 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
480 case cpu_to_le16(IEEE80211_FCTL_TODS):
481 if (unlikely(iftype != NL80211_IFTYPE_AP &&
482 iftype != NL80211_IFTYPE_AP_VLAN &&
483 iftype != NL80211_IFTYPE_P2P_GO))
486 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
487 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
488 iftype != NL80211_IFTYPE_MESH_POINT &&
489 iftype != NL80211_IFTYPE_AP_VLAN &&
490 iftype != NL80211_IFTYPE_STATION))
492 if (iftype == NL80211_IFTYPE_MESH_POINT) {
493 if (mesh_flags == MESH_FLAGS_AE_A4)
495 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
496 skb_copy_bits(skb, hdrlen +
497 offsetof(struct ieee80211s_hdr, eaddr1),
498 tmp.h_dest, 2 * ETH_ALEN);
500 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
503 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
504 if ((iftype != NL80211_IFTYPE_STATION &&
505 iftype != NL80211_IFTYPE_P2P_CLIENT &&
506 iftype != NL80211_IFTYPE_MESH_POINT) ||
507 (is_multicast_ether_addr(tmp.h_dest) &&
508 ether_addr_equal(tmp.h_source, addr)))
510 if (iftype == NL80211_IFTYPE_MESH_POINT) {
511 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
513 if (mesh_flags == MESH_FLAGS_AE_A4)
514 skb_copy_bits(skb, hdrlen +
515 offsetof(struct ieee80211s_hdr, eaddr1),
516 tmp.h_source, ETH_ALEN);
517 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
521 if (iftype != NL80211_IFTYPE_ADHOC &&
522 iftype != NL80211_IFTYPE_STATION &&
523 iftype != NL80211_IFTYPE_OCB)
528 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
529 tmp.h_proto = payload.proto;
531 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
532 tmp.h_proto != htons(ETH_P_AARP) &&
533 tmp.h_proto != htons(ETH_P_IPX)) ||
534 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
535 /* remove RFC1042 or Bridge-Tunnel encapsulation and
536 * replace EtherType */
537 hdrlen += ETH_ALEN + 2;
539 tmp.h_proto = htons(skb->len - hdrlen);
541 pskb_pull(skb, hdrlen);
544 ehdr = skb_push(skb, sizeof(struct ethhdr));
545 memcpy(ehdr, &tmp, sizeof(tmp));
549 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
552 __frame_add_frag(struct sk_buff *skb, struct page *page,
553 void *ptr, int len, int size)
555 struct skb_shared_info *sh = skb_shinfo(skb);
559 page_offset = ptr - page_address(page);
560 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
564 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
567 struct skb_shared_info *sh = skb_shinfo(skb);
568 const skb_frag_t *frag = &sh->frags[0];
569 struct page *frag_page;
571 int frag_len, frag_size;
572 int head_size = skb->len - skb->data_len;
575 frag_page = virt_to_head_page(skb->head);
576 frag_ptr = skb->data;
577 frag_size = head_size;
579 while (offset >= frag_size) {
581 frag_page = skb_frag_page(frag);
582 frag_ptr = skb_frag_address(frag);
583 frag_size = skb_frag_size(frag);
588 frag_len = frag_size - offset;
590 cur_len = min(len, frag_len);
592 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
596 frag_len = skb_frag_size(frag);
597 cur_len = min(len, frag_len);
598 __frame_add_frag(frame, skb_frag_page(frag),
599 skb_frag_address(frag), cur_len, frag_len);
605 static struct sk_buff *
606 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
607 int offset, int len, bool reuse_frag)
609 struct sk_buff *frame;
612 if (skb->len - offset < len)
616 * When reusing framents, copy some data to the head to simplify
617 * ethernet header handling and speed up protocol header processing
618 * in the stack later.
621 cur_len = min_t(int, len, 32);
624 * Allocate and reserve two bytes more for payload
625 * alignment since sizeof(struct ethhdr) is 14.
627 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
631 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
632 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
639 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
644 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
645 const u8 *addr, enum nl80211_iftype iftype,
646 const unsigned int extra_headroom,
647 const u8 *check_da, const u8 *check_sa)
649 unsigned int hlen = ALIGN(extra_headroom, 4);
650 struct sk_buff *frame = NULL;
653 int offset = 0, remaining;
655 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
656 bool reuse_skb = false;
660 unsigned int subframe_len;
664 skb_copy_bits(skb, offset, ð, sizeof(eth));
665 len = ntohs(eth.h_proto);
666 subframe_len = sizeof(struct ethhdr) + len;
667 padding = (4 - subframe_len) & 0x3;
669 /* the last MSDU has no padding */
670 remaining = skb->len - offset;
671 if (subframe_len > remaining)
674 offset += sizeof(struct ethhdr);
675 last = remaining <= subframe_len + padding;
677 /* FIXME: should we really accept multicast DA? */
678 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
679 !ether_addr_equal(check_da, eth.h_dest)) ||
680 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
681 offset += len + padding;
685 /* reuse skb for the last subframe */
686 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
687 skb_pull(skb, offset);
691 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
696 offset += len + padding;
699 skb_reset_network_header(frame);
700 frame->dev = skb->dev;
701 frame->priority = skb->priority;
703 payload = frame->data;
704 ethertype = (payload[6] << 8) | payload[7];
705 if (likely((ether_addr_equal(payload, rfc1042_header) &&
706 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
707 ether_addr_equal(payload, bridge_tunnel_header))) {
708 eth.h_proto = htons(ethertype);
709 skb_pull(frame, ETH_ALEN + 2);
712 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
713 __skb_queue_tail(list, frame);
722 __skb_queue_purge(list);
725 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
727 /* Given a data frame determine the 802.1p/1d tag to use. */
728 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
729 struct cfg80211_qos_map *qos_map)
732 unsigned char vlan_priority;
735 /* skb->priority values from 256->263 are magic values to
736 * directly indicate a specific 802.1d priority. This is used
737 * to allow 802.1d priority to be passed directly in from VLAN
740 if (skb->priority >= 256 && skb->priority <= 263) {
741 ret = skb->priority - 256;
745 if (skb_vlan_tag_present(skb)) {
746 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
748 if (vlan_priority > 0) {
754 switch (skb->protocol) {
755 case htons(ETH_P_IP):
756 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
758 case htons(ETH_P_IPV6):
759 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
761 case htons(ETH_P_MPLS_UC):
762 case htons(ETH_P_MPLS_MC): {
763 struct mpls_label mpls_tmp, *mpls;
765 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
766 sizeof(*mpls), &mpls_tmp);
770 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
774 case htons(ETH_P_80221):
775 /* 802.21 is always network control traffic */
782 unsigned int i, tmp_dscp = dscp >> 2;
784 for (i = 0; i < qos_map->num_des; i++) {
785 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
786 ret = qos_map->dscp_exception[i].up;
791 for (i = 0; i < 8; i++) {
792 if (tmp_dscp >= qos_map->up[i].low &&
793 tmp_dscp <= qos_map->up[i].high) {
802 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
804 EXPORT_SYMBOL(cfg80211_classify8021d);
806 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
808 const struct cfg80211_bss_ies *ies;
810 ies = rcu_dereference(bss->ies);
814 return cfg80211_find_elem(id, ies->data, ies->len);
816 EXPORT_SYMBOL(ieee80211_bss_get_elem);
818 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
820 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
821 struct net_device *dev = wdev->netdev;
824 if (!wdev->connect_keys)
827 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
828 if (!wdev->connect_keys->params[i].cipher)
830 if (rdev_add_key(rdev, dev, i, false, NULL,
831 &wdev->connect_keys->params[i])) {
832 netdev_err(dev, "failed to set key %d\n", i);
835 if (wdev->connect_keys->def == i &&
836 rdev_set_default_key(rdev, dev, i, true, true)) {
837 netdev_err(dev, "failed to set defkey %d\n", i);
842 kzfree(wdev->connect_keys);
843 wdev->connect_keys = NULL;
846 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
848 struct cfg80211_event *ev;
851 spin_lock_irqsave(&wdev->event_lock, flags);
852 while (!list_empty(&wdev->event_list)) {
853 ev = list_first_entry(&wdev->event_list,
854 struct cfg80211_event, list);
856 spin_unlock_irqrestore(&wdev->event_lock, flags);
860 case EVENT_CONNECT_RESULT:
861 __cfg80211_connect_result(
864 ev->cr.status == WLAN_STATUS_SUCCESS);
867 __cfg80211_roamed(wdev, &ev->rm);
869 case EVENT_DISCONNECTED:
870 __cfg80211_disconnected(wdev->netdev,
871 ev->dc.ie, ev->dc.ie_len,
873 !ev->dc.locally_generated);
875 case EVENT_IBSS_JOINED:
876 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
880 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
882 case EVENT_PORT_AUTHORIZED:
883 __cfg80211_port_authorized(wdev, ev->pa.bssid);
890 spin_lock_irqsave(&wdev->event_lock, flags);
892 spin_unlock_irqrestore(&wdev->event_lock, flags);
895 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
897 struct wireless_dev *wdev;
901 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
902 cfg80211_process_wdev_events(wdev);
905 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
906 struct net_device *dev, enum nl80211_iftype ntype,
907 struct vif_params *params)
910 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
914 /* don't support changing VLANs, you just re-create them */
915 if (otype == NL80211_IFTYPE_AP_VLAN)
918 /* cannot change into P2P device or NAN */
919 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
920 ntype == NL80211_IFTYPE_NAN)
923 if (!rdev->ops->change_virtual_intf ||
924 !(rdev->wiphy.interface_modes & (1 << ntype)))
927 /* if it's part of a bridge, reject changing type to station/ibss */
928 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
929 (ntype == NL80211_IFTYPE_ADHOC ||
930 ntype == NL80211_IFTYPE_STATION ||
931 ntype == NL80211_IFTYPE_P2P_CLIENT))
934 if (ntype != otype) {
935 dev->ieee80211_ptr->use_4addr = false;
936 dev->ieee80211_ptr->mesh_id_up_len = 0;
937 wdev_lock(dev->ieee80211_ptr);
938 rdev_set_qos_map(rdev, dev, NULL);
939 wdev_unlock(dev->ieee80211_ptr);
942 case NL80211_IFTYPE_AP:
943 cfg80211_stop_ap(rdev, dev, true);
945 case NL80211_IFTYPE_ADHOC:
946 cfg80211_leave_ibss(rdev, dev, false);
948 case NL80211_IFTYPE_STATION:
949 case NL80211_IFTYPE_P2P_CLIENT:
950 wdev_lock(dev->ieee80211_ptr);
951 cfg80211_disconnect(rdev, dev,
952 WLAN_REASON_DEAUTH_LEAVING, true);
953 wdev_unlock(dev->ieee80211_ptr);
955 case NL80211_IFTYPE_MESH_POINT:
956 /* mesh should be handled? */
962 cfg80211_process_rdev_events(rdev);
965 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
967 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
969 if (!err && params && params->use_4addr != -1)
970 dev->ieee80211_ptr->use_4addr = params->use_4addr;
973 dev->priv_flags &= ~IFF_DONT_BRIDGE;
975 case NL80211_IFTYPE_STATION:
976 if (dev->ieee80211_ptr->use_4addr)
979 case NL80211_IFTYPE_OCB:
980 case NL80211_IFTYPE_P2P_CLIENT:
981 case NL80211_IFTYPE_ADHOC:
982 dev->priv_flags |= IFF_DONT_BRIDGE;
984 case NL80211_IFTYPE_P2P_GO:
985 case NL80211_IFTYPE_AP:
986 case NL80211_IFTYPE_AP_VLAN:
987 case NL80211_IFTYPE_WDS:
988 case NL80211_IFTYPE_MESH_POINT:
991 case NL80211_IFTYPE_MONITOR:
992 /* monitor can't bridge anyway */
994 case NL80211_IFTYPE_UNSPECIFIED:
995 case NUM_NL80211_IFTYPES:
998 case NL80211_IFTYPE_P2P_DEVICE:
999 case NL80211_IFTYPE_NAN:
1005 if (!err && ntype != otype && netif_running(dev)) {
1006 cfg80211_update_iface_num(rdev, ntype, 1);
1007 cfg80211_update_iface_num(rdev, otype, -1);
1013 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1015 int modulation, streams, bitrate;
1017 /* the formula below does only work for MCS values smaller than 32 */
1018 if (WARN_ON_ONCE(rate->mcs >= 32))
1021 modulation = rate->mcs & 7;
1022 streams = (rate->mcs >> 3) + 1;
1024 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1027 bitrate *= (modulation + 1);
1028 else if (modulation == 4)
1029 bitrate *= (modulation + 2);
1031 bitrate *= (modulation + 3);
1035 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1036 bitrate = (bitrate / 9) * 10;
1038 /* do NOT round down here */
1039 return (bitrate + 50000) / 100000;
1042 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1044 static const u32 __mcs2bitrate[] = {
1052 [5] = 12512, /* 1251.25 mbps */
1062 [14] = 8662, /* 866.25 mbps */
1072 [24] = 67568, /* 6756.75 mbps */
1083 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1086 return __mcs2bitrate[rate->mcs];
1089 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1091 static const u32 base[4][10] = {
1101 /* not in the spec, but some devices use this: */
1145 case RATE_INFO_BW_160:
1148 case RATE_INFO_BW_80:
1151 case RATE_INFO_BW_40:
1154 case RATE_INFO_BW_5:
1155 case RATE_INFO_BW_10:
1158 case RATE_INFO_BW_20:
1162 bitrate = base[idx][rate->mcs];
1163 bitrate *= rate->nss;
1165 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1166 bitrate = (bitrate / 9) * 10;
1168 /* do NOT round down here */
1169 return (bitrate + 50000) / 100000;
1171 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1172 rate->bw, rate->mcs, rate->nss);
1176 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1179 u16 mcs_divisors[12] = {
1180 34133, /* 16.666666... */
1181 17067, /* 8.333333... */
1182 11378, /* 5.555555... */
1183 8533, /* 4.166666... */
1184 5689, /* 2.777777... */
1185 4267, /* 2.083333... */
1186 3923, /* 1.851851... */
1187 3413, /* 1.666666... */
1188 2844, /* 1.388888... */
1189 2560, /* 1.250000... */
1190 2276, /* 1.111111... */
1191 2048, /* 1.000000... */
1193 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1194 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1195 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1196 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1197 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1198 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1199 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1203 if (WARN_ON_ONCE(rate->mcs > 11))
1206 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1208 if (WARN_ON_ONCE(rate->he_ru_alloc >
1209 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1211 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1214 if (rate->bw == RATE_INFO_BW_160)
1215 result = rates_160M[rate->he_gi];
1216 else if (rate->bw == RATE_INFO_BW_80 ||
1217 (rate->bw == RATE_INFO_BW_HE_RU &&
1218 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1219 result = rates_969[rate->he_gi];
1220 else if (rate->bw == RATE_INFO_BW_40 ||
1221 (rate->bw == RATE_INFO_BW_HE_RU &&
1222 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1223 result = rates_484[rate->he_gi];
1224 else if (rate->bw == RATE_INFO_BW_20 ||
1225 (rate->bw == RATE_INFO_BW_HE_RU &&
1226 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1227 result = rates_242[rate->he_gi];
1228 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1229 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1230 result = rates_106[rate->he_gi];
1231 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1232 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1233 result = rates_52[rate->he_gi];
1234 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1235 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1236 result = rates_26[rate->he_gi];
1238 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1239 rate->bw, rate->he_ru_alloc);
1243 /* now scale to the appropriate MCS */
1246 do_div(tmp, mcs_divisors[rate->mcs]);
1249 /* and take NSS, DCM into account */
1250 result = (result * rate->nss) / 8;
1254 return result / 10000;
1257 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1259 if (rate->flags & RATE_INFO_FLAGS_MCS)
1260 return cfg80211_calculate_bitrate_ht(rate);
1261 if (rate->flags & RATE_INFO_FLAGS_60G)
1262 return cfg80211_calculate_bitrate_60g(rate);
1263 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1264 return cfg80211_calculate_bitrate_vht(rate);
1265 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1266 return cfg80211_calculate_bitrate_he(rate);
1268 return rate->legacy;
1270 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1272 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1273 enum ieee80211_p2p_attr_id attr,
1274 u8 *buf, unsigned int bufsize)
1277 u16 attr_remaining = 0;
1278 bool desired_attr = false;
1279 u16 desired_len = 0;
1282 unsigned int iedatalen;
1289 if (iedatalen + 2 > len)
1292 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1300 /* check WFA OUI, P2P subtype */
1301 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1302 iedata[2] != 0x9a || iedata[3] != 0x09)
1308 /* check attribute continuation into this IE */
1309 copy = min_t(unsigned int, attr_remaining, iedatalen);
1310 if (copy && desired_attr) {
1311 desired_len += copy;
1313 memcpy(out, iedata, min(bufsize, copy));
1314 out += min(bufsize, copy);
1315 bufsize -= min(bufsize, copy);
1319 if (copy == attr_remaining)
1323 attr_remaining -= copy;
1330 while (iedatalen > 0) {
1333 /* P2P attribute ID & size must fit */
1336 desired_attr = iedata[0] == attr;
1337 attr_len = get_unaligned_le16(iedata + 1);
1341 copy = min_t(unsigned int, attr_len, iedatalen);
1344 desired_len += copy;
1346 memcpy(out, iedata, min(bufsize, copy));
1347 out += min(bufsize, copy);
1348 bufsize -= min(bufsize, copy);
1351 if (copy == attr_len)
1357 attr_remaining = attr_len - copy;
1365 if (attr_remaining && desired_attr)
1370 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1372 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1376 /* Make sure array values are legal */
1377 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1382 if (ids[i] == WLAN_EID_EXTENSION) {
1383 if (id_ext && (ids[i + 1] == id))
1390 if (ids[i] == id && !id_ext)
1398 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1400 /* we assume a validly formed IEs buffer */
1401 u8 len = ies[pos + 1];
1405 /* the IE itself must have 255 bytes for fragments to follow */
1409 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1417 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1418 const u8 *ids, int n_ids,
1419 const u8 *after_ric, int n_after_ric,
1422 size_t pos = offset;
1424 while (pos < ielen) {
1427 if (ies[pos] == WLAN_EID_EXTENSION)
1429 if ((pos + ext) >= ielen)
1432 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1433 ies[pos] == WLAN_EID_EXTENSION))
1436 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1437 pos = skip_ie(ies, ielen, pos);
1439 while (pos < ielen) {
1440 if (ies[pos] == WLAN_EID_EXTENSION)
1445 if ((pos + ext) >= ielen)
1448 if (!ieee80211_id_in_list(after_ric,
1452 pos = skip_ie(ies, ielen, pos);
1457 pos = skip_ie(ies, ielen, pos);
1463 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1465 bool ieee80211_operating_class_to_band(u8 operating_class,
1466 enum nl80211_band *band)
1468 switch (operating_class) {
1472 *band = NL80211_BAND_5GHZ;
1478 *band = NL80211_BAND_2GHZ;
1481 *band = NL80211_BAND_60GHZ;
1487 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1489 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1493 u32 freq = chandef->center_freq1;
1495 if (freq >= 2412 && freq <= 2472) {
1496 if (chandef->width > NL80211_CHAN_WIDTH_40)
1499 /* 2.407 GHz, channels 1..13 */
1500 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1501 if (freq > chandef->chan->center_freq)
1502 *op_class = 83; /* HT40+ */
1504 *op_class = 84; /* HT40- */
1513 if (chandef->width > NL80211_CHAN_WIDTH_40)
1516 *op_class = 82; /* channel 14 */
1520 switch (chandef->width) {
1521 case NL80211_CHAN_WIDTH_80:
1524 case NL80211_CHAN_WIDTH_160:
1527 case NL80211_CHAN_WIDTH_80P80:
1530 case NL80211_CHAN_WIDTH_10:
1531 case NL80211_CHAN_WIDTH_5:
1532 return false; /* unsupported for now */
1538 /* 5 GHz, channels 36..48 */
1539 if (freq >= 5180 && freq <= 5240) {
1541 *op_class = vht_opclass;
1542 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1543 if (freq > chandef->chan->center_freq)
1554 /* 5 GHz, channels 52..64 */
1555 if (freq >= 5260 && freq <= 5320) {
1557 *op_class = vht_opclass;
1558 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1559 if (freq > chandef->chan->center_freq)
1570 /* 5 GHz, channels 100..144 */
1571 if (freq >= 5500 && freq <= 5720) {
1573 *op_class = vht_opclass;
1574 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1575 if (freq > chandef->chan->center_freq)
1586 /* 5 GHz, channels 149..169 */
1587 if (freq >= 5745 && freq <= 5845) {
1589 *op_class = vht_opclass;
1590 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1591 if (freq > chandef->chan->center_freq)
1595 } else if (freq <= 5805) {
1604 /* 56.16 GHz, channel 1..4 */
1605 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1606 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1613 /* not supported yet */
1616 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1618 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1619 u32 *beacon_int_gcd,
1620 bool *beacon_int_different)
1622 struct wireless_dev *wdev;
1624 *beacon_int_gcd = 0;
1625 *beacon_int_different = false;
1627 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1628 if (!wdev->beacon_interval)
1631 if (!*beacon_int_gcd) {
1632 *beacon_int_gcd = wdev->beacon_interval;
1636 if (wdev->beacon_interval == *beacon_int_gcd)
1639 *beacon_int_different = true;
1640 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1643 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1644 if (*beacon_int_gcd)
1645 *beacon_int_different = true;
1646 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1650 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1651 enum nl80211_iftype iftype, u32 beacon_int)
1654 * This is just a basic pre-condition check; if interface combinations
1655 * are possible the driver must already be checking those with a call
1656 * to cfg80211_check_combinations(), in which case we'll validate more
1657 * through the cfg80211_calculate_bi_data() call and code in
1658 * cfg80211_iter_combinations().
1661 if (beacon_int < 10 || beacon_int > 10000)
1667 int cfg80211_iter_combinations(struct wiphy *wiphy,
1668 struct iface_combination_params *params,
1669 void (*iter)(const struct ieee80211_iface_combination *c,
1673 const struct ieee80211_regdomain *regdom;
1674 enum nl80211_dfs_regions region = 0;
1676 int num_interfaces = 0;
1677 u32 used_iftypes = 0;
1679 bool beacon_int_different;
1682 * This is a bit strange, since the iteration used to rely only on
1683 * the data given by the driver, but here it now relies on context,
1684 * in form of the currently operating interfaces.
1685 * This is OK for all current users, and saves us from having to
1686 * push the GCD calculations into all the drivers.
1687 * In the future, this should probably rely more on data that's in
1688 * cfg80211 already - the only thing not would appear to be any new
1689 * interfaces (while being brought up) and channel/radar data.
1691 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1692 &beacon_int_gcd, &beacon_int_different);
1694 if (params->radar_detect) {
1696 regdom = rcu_dereference(cfg80211_regdomain);
1698 region = regdom->dfs_region;
1702 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1703 num_interfaces += params->iftype_num[iftype];
1704 if (params->iftype_num[iftype] > 0 &&
1705 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1706 used_iftypes |= BIT(iftype);
1709 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1710 const struct ieee80211_iface_combination *c;
1711 struct ieee80211_iface_limit *limits;
1712 u32 all_iftypes = 0;
1714 c = &wiphy->iface_combinations[i];
1716 if (num_interfaces > c->max_interfaces)
1718 if (params->num_different_channels > c->num_different_channels)
1721 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1726 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1727 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1729 for (j = 0; j < c->n_limits; j++) {
1730 all_iftypes |= limits[j].types;
1731 if (!(limits[j].types & BIT(iftype)))
1733 if (limits[j].max < params->iftype_num[iftype])
1735 limits[j].max -= params->iftype_num[iftype];
1739 if (params->radar_detect !=
1740 (c->radar_detect_widths & params->radar_detect))
1743 if (params->radar_detect && c->radar_detect_regions &&
1744 !(c->radar_detect_regions & BIT(region)))
1747 /* Finally check that all iftypes that we're currently
1748 * using are actually part of this combination. If they
1749 * aren't then we can't use this combination and have
1750 * to continue to the next.
1752 if ((all_iftypes & used_iftypes) != used_iftypes)
1755 if (beacon_int_gcd) {
1756 if (c->beacon_int_min_gcd &&
1757 beacon_int_gcd < c->beacon_int_min_gcd)
1759 if (!c->beacon_int_min_gcd && beacon_int_different)
1763 /* This combination covered all interface types and
1764 * supported the requested numbers, so we're good.
1774 EXPORT_SYMBOL(cfg80211_iter_combinations);
1777 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1784 int cfg80211_check_combinations(struct wiphy *wiphy,
1785 struct iface_combination_params *params)
1789 err = cfg80211_iter_combinations(wiphy, params,
1790 cfg80211_iter_sum_ifcombs, &num);
1798 EXPORT_SYMBOL(cfg80211_check_combinations);
1800 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1801 const u8 *rates, unsigned int n_rates,
1809 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1814 for (i = 0; i < n_rates; i++) {
1815 int rate = (rates[i] & 0x7f) * 5;
1818 for (j = 0; j < sband->n_bitrates; j++) {
1819 if (sband->bitrates[j].bitrate == rate) {
1830 * mask must have at least one bit set here since we
1831 * didn't accept a 0-length rates array nor allowed
1832 * entries in the array that didn't exist
1838 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1840 enum nl80211_band band;
1841 unsigned int n_channels = 0;
1843 for (band = 0; band < NUM_NL80211_BANDS; band++)
1844 if (wiphy->bands[band])
1845 n_channels += wiphy->bands[band]->n_channels;
1849 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1851 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1852 struct station_info *sinfo)
1854 struct cfg80211_registered_device *rdev;
1855 struct wireless_dev *wdev;
1857 wdev = dev->ieee80211_ptr;
1861 rdev = wiphy_to_rdev(wdev->wiphy);
1862 if (!rdev->ops->get_station)
1865 memset(sinfo, 0, sizeof(*sinfo));
1867 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1869 EXPORT_SYMBOL(cfg80211_get_station);
1871 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1878 kfree(f->serv_spec_info);
1881 for (i = 0; i < f->num_rx_filters; i++)
1882 kfree(f->rx_filters[i].filter);
1884 for (i = 0; i < f->num_tx_filters; i++)
1885 kfree(f->tx_filters[i].filter);
1887 kfree(f->rx_filters);
1888 kfree(f->tx_filters);
1891 EXPORT_SYMBOL(cfg80211_free_nan_func);
1893 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1894 u32 center_freq_khz, u32 bw_khz)
1896 u32 start_freq_khz, end_freq_khz;
1898 start_freq_khz = center_freq_khz - (bw_khz / 2);
1899 end_freq_khz = center_freq_khz + (bw_khz / 2);
1901 if (start_freq_khz >= freq_range->start_freq_khz &&
1902 end_freq_khz <= freq_range->end_freq_khz)
1908 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1910 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1911 sizeof(*(sinfo->pertid)),
1918 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1920 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1921 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1922 const unsigned char rfc1042_header[] __aligned(2) =
1923 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1924 EXPORT_SYMBOL(rfc1042_header);
1926 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1927 const unsigned char bridge_tunnel_header[] __aligned(2) =
1928 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1929 EXPORT_SYMBOL(bridge_tunnel_header);
1931 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1932 struct iapp_layer2_update {
1933 u8 da[ETH_ALEN]; /* broadcast */
1934 u8 sa[ETH_ALEN]; /* STA addr */
1942 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1944 struct iapp_layer2_update *msg;
1945 struct sk_buff *skb;
1947 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1950 skb = dev_alloc_skb(sizeof(*msg));
1953 msg = skb_put(skb, sizeof(*msg));
1955 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1956 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1958 eth_broadcast_addr(msg->da);
1959 ether_addr_copy(msg->sa, addr);
1960 msg->len = htons(6);
1962 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1963 msg->control = 0xaf; /* XID response lsb.1111F101.
1964 * F=0 (no poll command; unsolicited frame) */
1965 msg->xid_info[0] = 0x81; /* XID format identifier */
1966 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1967 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1970 skb->protocol = eth_type_trans(skb, dev);
1971 memset(skb->cb, 0, sizeof(skb->cb));
1974 EXPORT_SYMBOL(cfg80211_send_layer2_update);
1976 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1977 enum ieee80211_vht_chanwidth bw,
1978 int mcs, bool ext_nss_bw_capable)
1980 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1981 int max_vht_nss = 0;
1984 int i, mcs_encoding;
1989 if (WARN_ON(mcs > 9))
1998 /* find max_vht_nss for the given MCS */
1999 for (i = 7; i >= 0; i--) {
2000 int supp = (map >> (2 * i)) & 3;
2005 if (supp >= mcs_encoding) {
2006 max_vht_nss = i + 1;
2011 if (!(cap->supp_mcs.tx_mcs_map &
2012 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2015 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2016 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2017 supp_width = le32_get_bits(cap->vht_cap_info,
2018 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2020 /* if not capable, treat ext_nss_bw as 0 */
2021 if (!ext_nss_bw_capable)
2024 /* This is invalid */
2025 if (supp_width == 3)
2028 /* This is an invalid combination so pretend nothing is supported */
2029 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2033 * Cover all the special cases according to IEEE 802.11-2016
2034 * Table 9-250. All other cases are either factor of 1 or not
2038 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2039 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2040 if ((supp_width == 1 || supp_width == 2) &&
2042 return 2 * max_vht_nss;
2044 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2045 if (supp_width == 0 &&
2046 (ext_nss_bw == 1 || ext_nss_bw == 2))
2047 return max_vht_nss / 2;
2048 if (supp_width == 0 &&
2050 return (3 * max_vht_nss) / 4;
2051 if (supp_width == 1 &&
2053 return 2 * max_vht_nss;
2055 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2056 if (supp_width == 0 && ext_nss_bw == 1)
2057 return 0; /* not possible */
2058 if (supp_width == 0 &&
2060 return max_vht_nss / 2;
2061 if (supp_width == 0 &&
2063 return (3 * max_vht_nss) / 4;
2064 if (supp_width == 1 &&
2066 return 0; /* not possible */
2067 if (supp_width == 1 &&
2069 return max_vht_nss / 2;
2070 if (supp_width == 1 &&
2072 return (3 * max_vht_nss) / 4;
2076 /* not covered or invalid combination received */
2079 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2081 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2082 bool is_4addr, u8 check_swif)
2085 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2087 switch (check_swif) {
2089 if (is_vlan && is_4addr)
2090 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2091 return wiphy->interface_modes & BIT(iftype);
2093 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2094 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2095 return wiphy->software_iftypes & BIT(iftype);
2102 EXPORT_SYMBOL(cfg80211_iftype_allowed);