1 // SPDX-License-Identifier: GPL-2.0
3 * Wireless utility functions
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 case WLAN_CIPHER_SUITE_CCMP:
237 case WLAN_CIPHER_SUITE_CCMP_256:
238 case WLAN_CIPHER_SUITE_GCMP:
239 case WLAN_CIPHER_SUITE_GCMP_256:
240 /* IEEE802.11-2016 allows only 0 and - when using Extended Key
241 * ID - 1 as index for pairwise keys.
242 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
243 * the driver supports Extended Key ID.
244 * @NL80211_KEY_SET_TX can't be set when installing and
247 if (params->mode == NL80211_KEY_NO_TX) {
248 if (!wiphy_ext_feature_isset(&rdev->wiphy,
249 NL80211_EXT_FEATURE_EXT_KEY_ID))
251 else if (!pairwise || key_idx < 0 || key_idx > 1)
253 } else if ((pairwise && key_idx) ||
254 params->mode == NL80211_KEY_SET_TX) {
258 case WLAN_CIPHER_SUITE_AES_CMAC:
259 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
260 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
261 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
262 /* Disallow BIP (group-only) cipher as pairwise cipher */
268 case WLAN_CIPHER_SUITE_WEP40:
269 case WLAN_CIPHER_SUITE_WEP104:
276 switch (params->cipher) {
277 case WLAN_CIPHER_SUITE_WEP40:
278 if (params->key_len != WLAN_KEY_LEN_WEP40)
281 case WLAN_CIPHER_SUITE_TKIP:
282 if (params->key_len != WLAN_KEY_LEN_TKIP)
285 case WLAN_CIPHER_SUITE_CCMP:
286 if (params->key_len != WLAN_KEY_LEN_CCMP)
289 case WLAN_CIPHER_SUITE_CCMP_256:
290 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
293 case WLAN_CIPHER_SUITE_GCMP:
294 if (params->key_len != WLAN_KEY_LEN_GCMP)
297 case WLAN_CIPHER_SUITE_GCMP_256:
298 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
301 case WLAN_CIPHER_SUITE_WEP104:
302 if (params->key_len != WLAN_KEY_LEN_WEP104)
305 case WLAN_CIPHER_SUITE_AES_CMAC:
306 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
309 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
310 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
313 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
314 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
317 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
318 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
323 * We don't know anything about this algorithm,
324 * allow using it -- but the driver must check
325 * all parameters! We still check below whether
326 * or not the driver supports this algorithm,
333 switch (params->cipher) {
334 case WLAN_CIPHER_SUITE_WEP40:
335 case WLAN_CIPHER_SUITE_WEP104:
336 /* These ciphers do not use key sequence */
338 case WLAN_CIPHER_SUITE_TKIP:
339 case WLAN_CIPHER_SUITE_CCMP:
340 case WLAN_CIPHER_SUITE_CCMP_256:
341 case WLAN_CIPHER_SUITE_GCMP:
342 case WLAN_CIPHER_SUITE_GCMP_256:
343 case WLAN_CIPHER_SUITE_AES_CMAC:
344 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
345 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
346 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
347 if (params->seq_len != 6)
353 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
359 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
361 unsigned int hdrlen = 24;
363 if (ieee80211_is_data(fc)) {
364 if (ieee80211_has_a4(fc))
366 if (ieee80211_is_data_qos(fc)) {
367 hdrlen += IEEE80211_QOS_CTL_LEN;
368 if (ieee80211_has_order(fc))
369 hdrlen += IEEE80211_HT_CTL_LEN;
374 if (ieee80211_is_mgmt(fc)) {
375 if (ieee80211_has_order(fc))
376 hdrlen += IEEE80211_HT_CTL_LEN;
380 if (ieee80211_is_ctl(fc)) {
382 * ACK and CTS are 10 bytes, all others 16. To see how
383 * to get this condition consider
384 * subtype mask: 0b0000000011110000 (0x00F0)
385 * ACK subtype: 0b0000000011010000 (0x00D0)
386 * CTS subtype: 0b0000000011000000 (0x00C0)
387 * bits that matter: ^^^ (0x00E0)
388 * value of those: 0b0000000011000000 (0x00C0)
390 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
398 EXPORT_SYMBOL(ieee80211_hdrlen);
400 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
402 const struct ieee80211_hdr *hdr =
403 (const struct ieee80211_hdr *)skb->data;
406 if (unlikely(skb->len < 10))
408 hdrlen = ieee80211_hdrlen(hdr->frame_control);
409 if (unlikely(hdrlen > skb->len))
413 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
415 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
417 int ae = flags & MESH_FLAGS_AE;
418 /* 802.11-2012, 8.2.4.7.3 */
423 case MESH_FLAGS_AE_A4:
425 case MESH_FLAGS_AE_A5_A6:
430 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
432 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
434 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
436 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
437 const u8 *addr, enum nl80211_iftype iftype,
440 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
442 u8 hdr[ETH_ALEN] __aligned(2);
449 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
452 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
453 if (skb->len < hdrlen + 8)
456 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
458 * IEEE 802.11 address fields:
459 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
460 * 0 0 DA SA BSSID n/a
461 * 0 1 DA BSSID SA n/a
462 * 1 0 BSSID SA DA n/a
465 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
466 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
468 if (iftype == NL80211_IFTYPE_MESH_POINT)
469 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
471 mesh_flags &= MESH_FLAGS_AE;
473 switch (hdr->frame_control &
474 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
475 case cpu_to_le16(IEEE80211_FCTL_TODS):
476 if (unlikely(iftype != NL80211_IFTYPE_AP &&
477 iftype != NL80211_IFTYPE_AP_VLAN &&
478 iftype != NL80211_IFTYPE_P2P_GO))
481 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
482 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
483 iftype != NL80211_IFTYPE_MESH_POINT &&
484 iftype != NL80211_IFTYPE_AP_VLAN &&
485 iftype != NL80211_IFTYPE_STATION))
487 if (iftype == NL80211_IFTYPE_MESH_POINT) {
488 if (mesh_flags == MESH_FLAGS_AE_A4)
490 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
491 skb_copy_bits(skb, hdrlen +
492 offsetof(struct ieee80211s_hdr, eaddr1),
493 tmp.h_dest, 2 * ETH_ALEN);
495 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
498 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
499 if ((iftype != NL80211_IFTYPE_STATION &&
500 iftype != NL80211_IFTYPE_P2P_CLIENT &&
501 iftype != NL80211_IFTYPE_MESH_POINT) ||
502 (is_multicast_ether_addr(tmp.h_dest) &&
503 ether_addr_equal(tmp.h_source, addr)))
505 if (iftype == NL80211_IFTYPE_MESH_POINT) {
506 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
508 if (mesh_flags == MESH_FLAGS_AE_A4)
509 skb_copy_bits(skb, hdrlen +
510 offsetof(struct ieee80211s_hdr, eaddr1),
511 tmp.h_source, ETH_ALEN);
512 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
516 if (iftype != NL80211_IFTYPE_ADHOC &&
517 iftype != NL80211_IFTYPE_STATION &&
518 iftype != NL80211_IFTYPE_OCB)
523 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
524 tmp.h_proto = payload.proto;
526 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
527 tmp.h_proto != htons(ETH_P_AARP) &&
528 tmp.h_proto != htons(ETH_P_IPX)) ||
529 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
530 /* remove RFC1042 or Bridge-Tunnel encapsulation and
531 * replace EtherType */
532 hdrlen += ETH_ALEN + 2;
534 tmp.h_proto = htons(skb->len - hdrlen);
536 pskb_pull(skb, hdrlen);
539 ehdr = skb_push(skb, sizeof(struct ethhdr));
540 memcpy(ehdr, &tmp, sizeof(tmp));
544 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
547 __frame_add_frag(struct sk_buff *skb, struct page *page,
548 void *ptr, int len, int size)
550 struct skb_shared_info *sh = skb_shinfo(skb);
554 page_offset = ptr - page_address(page);
555 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
559 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
562 struct skb_shared_info *sh = skb_shinfo(skb);
563 const skb_frag_t *frag = &sh->frags[0];
564 struct page *frag_page;
566 int frag_len, frag_size;
567 int head_size = skb->len - skb->data_len;
570 frag_page = virt_to_head_page(skb->head);
571 frag_ptr = skb->data;
572 frag_size = head_size;
574 while (offset >= frag_size) {
576 frag_page = skb_frag_page(frag);
577 frag_ptr = skb_frag_address(frag);
578 frag_size = skb_frag_size(frag);
583 frag_len = frag_size - offset;
585 cur_len = min(len, frag_len);
587 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
591 frag_len = skb_frag_size(frag);
592 cur_len = min(len, frag_len);
593 __frame_add_frag(frame, skb_frag_page(frag),
594 skb_frag_address(frag), cur_len, frag_len);
600 static struct sk_buff *
601 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
602 int offset, int len, bool reuse_frag)
604 struct sk_buff *frame;
607 if (skb->len - offset < len)
611 * When reusing framents, copy some data to the head to simplify
612 * ethernet header handling and speed up protocol header processing
613 * in the stack later.
616 cur_len = min_t(int, len, 32);
619 * Allocate and reserve two bytes more for payload
620 * alignment since sizeof(struct ethhdr) is 14.
622 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
626 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
627 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
634 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
639 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
640 const u8 *addr, enum nl80211_iftype iftype,
641 const unsigned int extra_headroom,
642 const u8 *check_da, const u8 *check_sa)
644 unsigned int hlen = ALIGN(extra_headroom, 4);
645 struct sk_buff *frame = NULL;
648 int offset = 0, remaining;
650 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
651 bool reuse_skb = false;
655 unsigned int subframe_len;
659 skb_copy_bits(skb, offset, ð, sizeof(eth));
660 len = ntohs(eth.h_proto);
661 subframe_len = sizeof(struct ethhdr) + len;
662 padding = (4 - subframe_len) & 0x3;
664 /* the last MSDU has no padding */
665 remaining = skb->len - offset;
666 if (subframe_len > remaining)
669 offset += sizeof(struct ethhdr);
670 last = remaining <= subframe_len + padding;
672 /* FIXME: should we really accept multicast DA? */
673 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
674 !ether_addr_equal(check_da, eth.h_dest)) ||
675 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
676 offset += len + padding;
680 /* reuse skb for the last subframe */
681 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
682 skb_pull(skb, offset);
686 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
691 offset += len + padding;
694 skb_reset_network_header(frame);
695 frame->dev = skb->dev;
696 frame->priority = skb->priority;
698 payload = frame->data;
699 ethertype = (payload[6] << 8) | payload[7];
700 if (likely((ether_addr_equal(payload, rfc1042_header) &&
701 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
702 ether_addr_equal(payload, bridge_tunnel_header))) {
703 eth.h_proto = htons(ethertype);
704 skb_pull(frame, ETH_ALEN + 2);
707 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
708 __skb_queue_tail(list, frame);
717 __skb_queue_purge(list);
720 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
722 /* Given a data frame determine the 802.1p/1d tag to use. */
723 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
724 struct cfg80211_qos_map *qos_map)
727 unsigned char vlan_priority;
730 /* skb->priority values from 256->263 are magic values to
731 * directly indicate a specific 802.1d priority. This is used
732 * to allow 802.1d priority to be passed directly in from VLAN
735 if (skb->priority >= 256 && skb->priority <= 263) {
736 ret = skb->priority - 256;
740 if (skb_vlan_tag_present(skb)) {
741 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
743 if (vlan_priority > 0) {
749 switch (skb->protocol) {
750 case htons(ETH_P_IP):
751 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
753 case htons(ETH_P_IPV6):
754 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
756 case htons(ETH_P_MPLS_UC):
757 case htons(ETH_P_MPLS_MC): {
758 struct mpls_label mpls_tmp, *mpls;
760 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
761 sizeof(*mpls), &mpls_tmp);
765 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
769 case htons(ETH_P_80221):
770 /* 802.21 is always network control traffic */
777 unsigned int i, tmp_dscp = dscp >> 2;
779 for (i = 0; i < qos_map->num_des; i++) {
780 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
781 ret = qos_map->dscp_exception[i].up;
786 for (i = 0; i < 8; i++) {
787 if (tmp_dscp >= qos_map->up[i].low &&
788 tmp_dscp <= qos_map->up[i].high) {
797 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
799 EXPORT_SYMBOL(cfg80211_classify8021d);
801 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
803 const struct cfg80211_bss_ies *ies;
805 ies = rcu_dereference(bss->ies);
809 return cfg80211_find_elem(id, ies->data, ies->len);
811 EXPORT_SYMBOL(ieee80211_bss_get_elem);
813 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
815 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
816 struct net_device *dev = wdev->netdev;
819 if (!wdev->connect_keys)
822 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
823 if (!wdev->connect_keys->params[i].cipher)
825 if (rdev_add_key(rdev, dev, i, false, NULL,
826 &wdev->connect_keys->params[i])) {
827 netdev_err(dev, "failed to set key %d\n", i);
830 if (wdev->connect_keys->def == i &&
831 rdev_set_default_key(rdev, dev, i, true, true)) {
832 netdev_err(dev, "failed to set defkey %d\n", i);
837 kzfree(wdev->connect_keys);
838 wdev->connect_keys = NULL;
841 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
843 struct cfg80211_event *ev;
846 spin_lock_irqsave(&wdev->event_lock, flags);
847 while (!list_empty(&wdev->event_list)) {
848 ev = list_first_entry(&wdev->event_list,
849 struct cfg80211_event, list);
851 spin_unlock_irqrestore(&wdev->event_lock, flags);
855 case EVENT_CONNECT_RESULT:
856 __cfg80211_connect_result(
859 ev->cr.status == WLAN_STATUS_SUCCESS);
862 __cfg80211_roamed(wdev, &ev->rm);
864 case EVENT_DISCONNECTED:
865 __cfg80211_disconnected(wdev->netdev,
866 ev->dc.ie, ev->dc.ie_len,
868 !ev->dc.locally_generated);
870 case EVENT_IBSS_JOINED:
871 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
875 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
877 case EVENT_PORT_AUTHORIZED:
878 __cfg80211_port_authorized(wdev, ev->pa.bssid);
885 spin_lock_irqsave(&wdev->event_lock, flags);
887 spin_unlock_irqrestore(&wdev->event_lock, flags);
890 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
892 struct wireless_dev *wdev;
896 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
897 cfg80211_process_wdev_events(wdev);
900 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
901 struct net_device *dev, enum nl80211_iftype ntype,
902 struct vif_params *params)
905 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
909 /* don't support changing VLANs, you just re-create them */
910 if (otype == NL80211_IFTYPE_AP_VLAN)
913 /* cannot change into P2P device or NAN */
914 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
915 ntype == NL80211_IFTYPE_NAN)
918 if (!rdev->ops->change_virtual_intf ||
919 !(rdev->wiphy.interface_modes & (1 << ntype)))
922 /* if it's part of a bridge, reject changing type to station/ibss */
923 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
924 (ntype == NL80211_IFTYPE_ADHOC ||
925 ntype == NL80211_IFTYPE_STATION ||
926 ntype == NL80211_IFTYPE_P2P_CLIENT))
929 if (ntype != otype) {
930 dev->ieee80211_ptr->use_4addr = false;
931 dev->ieee80211_ptr->mesh_id_up_len = 0;
932 wdev_lock(dev->ieee80211_ptr);
933 rdev_set_qos_map(rdev, dev, NULL);
934 wdev_unlock(dev->ieee80211_ptr);
937 case NL80211_IFTYPE_AP:
938 cfg80211_stop_ap(rdev, dev, true);
940 case NL80211_IFTYPE_ADHOC:
941 cfg80211_leave_ibss(rdev, dev, false);
943 case NL80211_IFTYPE_STATION:
944 case NL80211_IFTYPE_P2P_CLIENT:
945 wdev_lock(dev->ieee80211_ptr);
946 cfg80211_disconnect(rdev, dev,
947 WLAN_REASON_DEAUTH_LEAVING, true);
948 wdev_unlock(dev->ieee80211_ptr);
950 case NL80211_IFTYPE_MESH_POINT:
951 /* mesh should be handled? */
957 cfg80211_process_rdev_events(rdev);
960 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
962 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
964 if (!err && params && params->use_4addr != -1)
965 dev->ieee80211_ptr->use_4addr = params->use_4addr;
968 dev->priv_flags &= ~IFF_DONT_BRIDGE;
970 case NL80211_IFTYPE_STATION:
971 if (dev->ieee80211_ptr->use_4addr)
974 case NL80211_IFTYPE_OCB:
975 case NL80211_IFTYPE_P2P_CLIENT:
976 case NL80211_IFTYPE_ADHOC:
977 dev->priv_flags |= IFF_DONT_BRIDGE;
979 case NL80211_IFTYPE_P2P_GO:
980 case NL80211_IFTYPE_AP:
981 case NL80211_IFTYPE_AP_VLAN:
982 case NL80211_IFTYPE_WDS:
983 case NL80211_IFTYPE_MESH_POINT:
986 case NL80211_IFTYPE_MONITOR:
987 /* monitor can't bridge anyway */
989 case NL80211_IFTYPE_UNSPECIFIED:
990 case NUM_NL80211_IFTYPES:
993 case NL80211_IFTYPE_P2P_DEVICE:
994 case NL80211_IFTYPE_NAN:
1000 if (!err && ntype != otype && netif_running(dev)) {
1001 cfg80211_update_iface_num(rdev, ntype, 1);
1002 cfg80211_update_iface_num(rdev, otype, -1);
1008 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1010 int modulation, streams, bitrate;
1012 /* the formula below does only work for MCS values smaller than 32 */
1013 if (WARN_ON_ONCE(rate->mcs >= 32))
1016 modulation = rate->mcs & 7;
1017 streams = (rate->mcs >> 3) + 1;
1019 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1022 bitrate *= (modulation + 1);
1023 else if (modulation == 4)
1024 bitrate *= (modulation + 2);
1026 bitrate *= (modulation + 3);
1030 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1031 bitrate = (bitrate / 9) * 10;
1033 /* do NOT round down here */
1034 return (bitrate + 50000) / 100000;
1037 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1039 static const u32 __mcs2bitrate[] = {
1047 [5] = 12512, /* 1251.25 mbps */
1057 [14] = 8662, /* 866.25 mbps */
1067 [24] = 67568, /* 6756.75 mbps */
1078 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1081 return __mcs2bitrate[rate->mcs];
1084 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1086 static const u32 base[4][10] = {
1096 /* not in the spec, but some devices use this: */
1140 case RATE_INFO_BW_160:
1143 case RATE_INFO_BW_80:
1146 case RATE_INFO_BW_40:
1149 case RATE_INFO_BW_5:
1150 case RATE_INFO_BW_10:
1153 case RATE_INFO_BW_20:
1157 bitrate = base[idx][rate->mcs];
1158 bitrate *= rate->nss;
1160 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1161 bitrate = (bitrate / 9) * 10;
1163 /* do NOT round down here */
1164 return (bitrate + 50000) / 100000;
1166 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1167 rate->bw, rate->mcs, rate->nss);
1171 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1174 u16 mcs_divisors[12] = {
1175 34133, /* 16.666666... */
1176 17067, /* 8.333333... */
1177 11378, /* 5.555555... */
1178 8533, /* 4.166666... */
1179 5689, /* 2.777777... */
1180 4267, /* 2.083333... */
1181 3923, /* 1.851851... */
1182 3413, /* 1.666666... */
1183 2844, /* 1.388888... */
1184 2560, /* 1.250000... */
1185 2276, /* 1.111111... */
1186 2048, /* 1.000000... */
1188 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1189 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1190 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1191 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1192 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1193 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1194 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1198 if (WARN_ON_ONCE(rate->mcs > 11))
1201 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1203 if (WARN_ON_ONCE(rate->he_ru_alloc >
1204 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1206 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1209 if (rate->bw == RATE_INFO_BW_160)
1210 result = rates_160M[rate->he_gi];
1211 else if (rate->bw == RATE_INFO_BW_80 ||
1212 (rate->bw == RATE_INFO_BW_HE_RU &&
1213 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1214 result = rates_969[rate->he_gi];
1215 else if (rate->bw == RATE_INFO_BW_40 ||
1216 (rate->bw == RATE_INFO_BW_HE_RU &&
1217 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1218 result = rates_484[rate->he_gi];
1219 else if (rate->bw == RATE_INFO_BW_20 ||
1220 (rate->bw == RATE_INFO_BW_HE_RU &&
1221 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1222 result = rates_242[rate->he_gi];
1223 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1224 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1225 result = rates_106[rate->he_gi];
1226 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1227 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1228 result = rates_52[rate->he_gi];
1229 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1230 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1231 result = rates_26[rate->he_gi];
1233 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1234 rate->bw, rate->he_ru_alloc);
1238 /* now scale to the appropriate MCS */
1241 do_div(tmp, mcs_divisors[rate->mcs]);
1244 /* and take NSS, DCM into account */
1245 result = (result * rate->nss) / 8;
1249 return result / 10000;
1252 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1254 if (rate->flags & RATE_INFO_FLAGS_MCS)
1255 return cfg80211_calculate_bitrate_ht(rate);
1256 if (rate->flags & RATE_INFO_FLAGS_60G)
1257 return cfg80211_calculate_bitrate_60g(rate);
1258 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1259 return cfg80211_calculate_bitrate_vht(rate);
1260 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1261 return cfg80211_calculate_bitrate_he(rate);
1263 return rate->legacy;
1265 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1267 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1268 enum ieee80211_p2p_attr_id attr,
1269 u8 *buf, unsigned int bufsize)
1272 u16 attr_remaining = 0;
1273 bool desired_attr = false;
1274 u16 desired_len = 0;
1277 unsigned int iedatalen;
1284 if (iedatalen + 2 > len)
1287 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1295 /* check WFA OUI, P2P subtype */
1296 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1297 iedata[2] != 0x9a || iedata[3] != 0x09)
1303 /* check attribute continuation into this IE */
1304 copy = min_t(unsigned int, attr_remaining, iedatalen);
1305 if (copy && desired_attr) {
1306 desired_len += copy;
1308 memcpy(out, iedata, min(bufsize, copy));
1309 out += min(bufsize, copy);
1310 bufsize -= min(bufsize, copy);
1314 if (copy == attr_remaining)
1318 attr_remaining -= copy;
1325 while (iedatalen > 0) {
1328 /* P2P attribute ID & size must fit */
1331 desired_attr = iedata[0] == attr;
1332 attr_len = get_unaligned_le16(iedata + 1);
1336 copy = min_t(unsigned int, attr_len, iedatalen);
1339 desired_len += copy;
1341 memcpy(out, iedata, min(bufsize, copy));
1342 out += min(bufsize, copy);
1343 bufsize -= min(bufsize, copy);
1346 if (copy == attr_len)
1352 attr_remaining = attr_len - copy;
1360 if (attr_remaining && desired_attr)
1365 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1367 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1371 /* Make sure array values are legal */
1372 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1377 if (ids[i] == WLAN_EID_EXTENSION) {
1378 if (id_ext && (ids[i + 1] == id))
1385 if (ids[i] == id && !id_ext)
1393 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1395 /* we assume a validly formed IEs buffer */
1396 u8 len = ies[pos + 1];
1400 /* the IE itself must have 255 bytes for fragments to follow */
1404 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1412 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1413 const u8 *ids, int n_ids,
1414 const u8 *after_ric, int n_after_ric,
1417 size_t pos = offset;
1419 while (pos < ielen) {
1422 if (ies[pos] == WLAN_EID_EXTENSION)
1424 if ((pos + ext) >= ielen)
1427 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1428 ies[pos] == WLAN_EID_EXTENSION))
1431 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1432 pos = skip_ie(ies, ielen, pos);
1434 while (pos < ielen) {
1435 if (ies[pos] == WLAN_EID_EXTENSION)
1440 if ((pos + ext) >= ielen)
1443 if (!ieee80211_id_in_list(after_ric,
1447 pos = skip_ie(ies, ielen, pos);
1452 pos = skip_ie(ies, ielen, pos);
1458 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1460 bool ieee80211_operating_class_to_band(u8 operating_class,
1461 enum nl80211_band *band)
1463 switch (operating_class) {
1467 *band = NL80211_BAND_5GHZ;
1473 *band = NL80211_BAND_2GHZ;
1476 *band = NL80211_BAND_60GHZ;
1482 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1484 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1488 u32 freq = chandef->center_freq1;
1490 if (freq >= 2412 && freq <= 2472) {
1491 if (chandef->width > NL80211_CHAN_WIDTH_40)
1494 /* 2.407 GHz, channels 1..13 */
1495 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1496 if (freq > chandef->chan->center_freq)
1497 *op_class = 83; /* HT40+ */
1499 *op_class = 84; /* HT40- */
1508 if (chandef->width > NL80211_CHAN_WIDTH_40)
1511 *op_class = 82; /* channel 14 */
1515 switch (chandef->width) {
1516 case NL80211_CHAN_WIDTH_80:
1519 case NL80211_CHAN_WIDTH_160:
1522 case NL80211_CHAN_WIDTH_80P80:
1525 case NL80211_CHAN_WIDTH_10:
1526 case NL80211_CHAN_WIDTH_5:
1527 return false; /* unsupported for now */
1533 /* 5 GHz, channels 36..48 */
1534 if (freq >= 5180 && freq <= 5240) {
1536 *op_class = vht_opclass;
1537 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1538 if (freq > chandef->chan->center_freq)
1549 /* 5 GHz, channels 52..64 */
1550 if (freq >= 5260 && freq <= 5320) {
1552 *op_class = vht_opclass;
1553 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1554 if (freq > chandef->chan->center_freq)
1565 /* 5 GHz, channels 100..144 */
1566 if (freq >= 5500 && freq <= 5720) {
1568 *op_class = vht_opclass;
1569 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1570 if (freq > chandef->chan->center_freq)
1581 /* 5 GHz, channels 149..169 */
1582 if (freq >= 5745 && freq <= 5845) {
1584 *op_class = vht_opclass;
1585 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1586 if (freq > chandef->chan->center_freq)
1590 } else if (freq <= 5805) {
1599 /* 56.16 GHz, channel 1..4 */
1600 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1601 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1608 /* not supported yet */
1611 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1613 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1614 u32 *beacon_int_gcd,
1615 bool *beacon_int_different)
1617 struct wireless_dev *wdev;
1619 *beacon_int_gcd = 0;
1620 *beacon_int_different = false;
1622 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1623 if (!wdev->beacon_interval)
1626 if (!*beacon_int_gcd) {
1627 *beacon_int_gcd = wdev->beacon_interval;
1631 if (wdev->beacon_interval == *beacon_int_gcd)
1634 *beacon_int_different = true;
1635 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1638 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1639 if (*beacon_int_gcd)
1640 *beacon_int_different = true;
1641 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1645 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1646 enum nl80211_iftype iftype, u32 beacon_int)
1649 * This is just a basic pre-condition check; if interface combinations
1650 * are possible the driver must already be checking those with a call
1651 * to cfg80211_check_combinations(), in which case we'll validate more
1652 * through the cfg80211_calculate_bi_data() call and code in
1653 * cfg80211_iter_combinations().
1656 if (beacon_int < 10 || beacon_int > 10000)
1662 int cfg80211_iter_combinations(struct wiphy *wiphy,
1663 struct iface_combination_params *params,
1664 void (*iter)(const struct ieee80211_iface_combination *c,
1668 const struct ieee80211_regdomain *regdom;
1669 enum nl80211_dfs_regions region = 0;
1671 int num_interfaces = 0;
1672 u32 used_iftypes = 0;
1674 bool beacon_int_different;
1677 * This is a bit strange, since the iteration used to rely only on
1678 * the data given by the driver, but here it now relies on context,
1679 * in form of the currently operating interfaces.
1680 * This is OK for all current users, and saves us from having to
1681 * push the GCD calculations into all the drivers.
1682 * In the future, this should probably rely more on data that's in
1683 * cfg80211 already - the only thing not would appear to be any new
1684 * interfaces (while being brought up) and channel/radar data.
1686 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1687 &beacon_int_gcd, &beacon_int_different);
1689 if (params->radar_detect) {
1691 regdom = rcu_dereference(cfg80211_regdomain);
1693 region = regdom->dfs_region;
1697 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1698 num_interfaces += params->iftype_num[iftype];
1699 if (params->iftype_num[iftype] > 0 &&
1700 !(wiphy->software_iftypes & BIT(iftype)))
1701 used_iftypes |= BIT(iftype);
1704 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1705 const struct ieee80211_iface_combination *c;
1706 struct ieee80211_iface_limit *limits;
1707 u32 all_iftypes = 0;
1709 c = &wiphy->iface_combinations[i];
1711 if (num_interfaces > c->max_interfaces)
1713 if (params->num_different_channels > c->num_different_channels)
1716 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1721 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1722 if (wiphy->software_iftypes & BIT(iftype))
1724 for (j = 0; j < c->n_limits; j++) {
1725 all_iftypes |= limits[j].types;
1726 if (!(limits[j].types & BIT(iftype)))
1728 if (limits[j].max < params->iftype_num[iftype])
1730 limits[j].max -= params->iftype_num[iftype];
1734 if (params->radar_detect !=
1735 (c->radar_detect_widths & params->radar_detect))
1738 if (params->radar_detect && c->radar_detect_regions &&
1739 !(c->radar_detect_regions & BIT(region)))
1742 /* Finally check that all iftypes that we're currently
1743 * using are actually part of this combination. If they
1744 * aren't then we can't use this combination and have
1745 * to continue to the next.
1747 if ((all_iftypes & used_iftypes) != used_iftypes)
1750 if (beacon_int_gcd) {
1751 if (c->beacon_int_min_gcd &&
1752 beacon_int_gcd < c->beacon_int_min_gcd)
1754 if (!c->beacon_int_min_gcd && beacon_int_different)
1758 /* This combination covered all interface types and
1759 * supported the requested numbers, so we're good.
1769 EXPORT_SYMBOL(cfg80211_iter_combinations);
1772 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1779 int cfg80211_check_combinations(struct wiphy *wiphy,
1780 struct iface_combination_params *params)
1784 err = cfg80211_iter_combinations(wiphy, params,
1785 cfg80211_iter_sum_ifcombs, &num);
1793 EXPORT_SYMBOL(cfg80211_check_combinations);
1795 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1796 const u8 *rates, unsigned int n_rates,
1804 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1809 for (i = 0; i < n_rates; i++) {
1810 int rate = (rates[i] & 0x7f) * 5;
1813 for (j = 0; j < sband->n_bitrates; j++) {
1814 if (sband->bitrates[j].bitrate == rate) {
1825 * mask must have at least one bit set here since we
1826 * didn't accept a 0-length rates array nor allowed
1827 * entries in the array that didn't exist
1833 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1835 enum nl80211_band band;
1836 unsigned int n_channels = 0;
1838 for (band = 0; band < NUM_NL80211_BANDS; band++)
1839 if (wiphy->bands[band])
1840 n_channels += wiphy->bands[band]->n_channels;
1844 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1846 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1847 struct station_info *sinfo)
1849 struct cfg80211_registered_device *rdev;
1850 struct wireless_dev *wdev;
1852 wdev = dev->ieee80211_ptr;
1856 rdev = wiphy_to_rdev(wdev->wiphy);
1857 if (!rdev->ops->get_station)
1860 memset(sinfo, 0, sizeof(*sinfo));
1862 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1864 EXPORT_SYMBOL(cfg80211_get_station);
1866 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1873 kfree(f->serv_spec_info);
1876 for (i = 0; i < f->num_rx_filters; i++)
1877 kfree(f->rx_filters[i].filter);
1879 for (i = 0; i < f->num_tx_filters; i++)
1880 kfree(f->tx_filters[i].filter);
1882 kfree(f->rx_filters);
1883 kfree(f->tx_filters);
1886 EXPORT_SYMBOL(cfg80211_free_nan_func);
1888 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1889 u32 center_freq_khz, u32 bw_khz)
1891 u32 start_freq_khz, end_freq_khz;
1893 start_freq_khz = center_freq_khz - (bw_khz / 2);
1894 end_freq_khz = center_freq_khz + (bw_khz / 2);
1896 if (start_freq_khz >= freq_range->start_freq_khz &&
1897 end_freq_khz <= freq_range->end_freq_khz)
1903 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1905 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1906 sizeof(*(sinfo->pertid)),
1913 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1915 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1916 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1917 const unsigned char rfc1042_header[] __aligned(2) =
1918 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1919 EXPORT_SYMBOL(rfc1042_header);
1921 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1922 const unsigned char bridge_tunnel_header[] __aligned(2) =
1923 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1924 EXPORT_SYMBOL(bridge_tunnel_header);
1926 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1927 struct iapp_layer2_update {
1928 u8 da[ETH_ALEN]; /* broadcast */
1929 u8 sa[ETH_ALEN]; /* STA addr */
1937 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1939 struct iapp_layer2_update *msg;
1940 struct sk_buff *skb;
1942 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1945 skb = dev_alloc_skb(sizeof(*msg));
1948 msg = skb_put(skb, sizeof(*msg));
1950 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1951 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1953 eth_broadcast_addr(msg->da);
1954 ether_addr_copy(msg->sa, addr);
1955 msg->len = htons(6);
1957 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1958 msg->control = 0xaf; /* XID response lsb.1111F101.
1959 * F=0 (no poll command; unsolicited frame) */
1960 msg->xid_info[0] = 0x81; /* XID format identifier */
1961 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1962 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1965 skb->protocol = eth_type_trans(skb, dev);
1966 memset(skb->cb, 0, sizeof(skb->cb));
1969 EXPORT_SYMBOL(cfg80211_send_layer2_update);
1971 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1972 enum ieee80211_vht_chanwidth bw,
1973 int mcs, bool ext_nss_bw_capable)
1975 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1976 int max_vht_nss = 0;
1979 int i, mcs_encoding;
1984 if (WARN_ON(mcs > 9))
1993 /* find max_vht_nss for the given MCS */
1994 for (i = 7; i >= 0; i--) {
1995 int supp = (map >> (2 * i)) & 3;
2000 if (supp >= mcs_encoding) {
2001 max_vht_nss = i + 1;
2006 if (!(cap->supp_mcs.tx_mcs_map &
2007 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2010 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2011 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2012 supp_width = le32_get_bits(cap->vht_cap_info,
2013 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2015 /* if not capable, treat ext_nss_bw as 0 */
2016 if (!ext_nss_bw_capable)
2019 /* This is invalid */
2020 if (supp_width == 3)
2023 /* This is an invalid combination so pretend nothing is supported */
2024 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2028 * Cover all the special cases according to IEEE 802.11-2016
2029 * Table 9-250. All other cases are either factor of 1 or not
2033 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2034 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2035 if ((supp_width == 1 || supp_width == 2) &&
2037 return 2 * max_vht_nss;
2039 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2040 if (supp_width == 0 &&
2041 (ext_nss_bw == 1 || ext_nss_bw == 2))
2042 return max_vht_nss / 2;
2043 if (supp_width == 0 &&
2045 return (3 * max_vht_nss) / 4;
2046 if (supp_width == 1 &&
2048 return 2 * max_vht_nss;
2050 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2051 if (supp_width == 0 && ext_nss_bw == 1)
2052 return 0; /* not possible */
2053 if (supp_width == 0 &&
2055 return max_vht_nss / 2;
2056 if (supp_width == 0 &&
2058 return (3 * max_vht_nss) / 4;
2059 if (supp_width == 1 &&
2061 return 0; /* not possible */
2062 if (supp_width == 1 &&
2064 return max_vht_nss / 2;
2065 if (supp_width == 1 &&
2067 return (3 * max_vht_nss) / 4;
2071 /* not covered or invalid combination received */
2074 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);