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 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 /* Disallow pairwise keys with non-zero index unless it's WEP
241 * or a vendor specific cipher (because current deployments use
242 * pairwise WEP keys with non-zero indices and for vendor
243 * specific ciphers this should be validated in the driver or
244 * hardware level - but 802.11i clearly specifies to use zero)
246 if (pairwise && key_idx)
249 case WLAN_CIPHER_SUITE_AES_CMAC:
250 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
251 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
252 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
253 /* Disallow BIP (group-only) cipher as pairwise cipher */
259 case WLAN_CIPHER_SUITE_WEP40:
260 case WLAN_CIPHER_SUITE_WEP104:
267 switch (params->cipher) {
268 case WLAN_CIPHER_SUITE_WEP40:
269 if (params->key_len != WLAN_KEY_LEN_WEP40)
272 case WLAN_CIPHER_SUITE_TKIP:
273 if (params->key_len != WLAN_KEY_LEN_TKIP)
276 case WLAN_CIPHER_SUITE_CCMP:
277 if (params->key_len != WLAN_KEY_LEN_CCMP)
280 case WLAN_CIPHER_SUITE_CCMP_256:
281 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
284 case WLAN_CIPHER_SUITE_GCMP:
285 if (params->key_len != WLAN_KEY_LEN_GCMP)
288 case WLAN_CIPHER_SUITE_GCMP_256:
289 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
292 case WLAN_CIPHER_SUITE_WEP104:
293 if (params->key_len != WLAN_KEY_LEN_WEP104)
296 case WLAN_CIPHER_SUITE_AES_CMAC:
297 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
300 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
301 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
304 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
305 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
308 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
309 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
314 * We don't know anything about this algorithm,
315 * allow using it -- but the driver must check
316 * all parameters! We still check below whether
317 * or not the driver supports this algorithm,
324 switch (params->cipher) {
325 case WLAN_CIPHER_SUITE_WEP40:
326 case WLAN_CIPHER_SUITE_WEP104:
327 /* These ciphers do not use key sequence */
329 case WLAN_CIPHER_SUITE_TKIP:
330 case WLAN_CIPHER_SUITE_CCMP:
331 case WLAN_CIPHER_SUITE_CCMP_256:
332 case WLAN_CIPHER_SUITE_GCMP:
333 case WLAN_CIPHER_SUITE_GCMP_256:
334 case WLAN_CIPHER_SUITE_AES_CMAC:
335 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
336 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
337 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
338 if (params->seq_len != 6)
344 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
350 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
352 unsigned int hdrlen = 24;
354 if (ieee80211_is_data(fc)) {
355 if (ieee80211_has_a4(fc))
357 if (ieee80211_is_data_qos(fc)) {
358 hdrlen += IEEE80211_QOS_CTL_LEN;
359 if (ieee80211_has_order(fc))
360 hdrlen += IEEE80211_HT_CTL_LEN;
365 if (ieee80211_is_mgmt(fc)) {
366 if (ieee80211_has_order(fc))
367 hdrlen += IEEE80211_HT_CTL_LEN;
371 if (ieee80211_is_ctl(fc)) {
373 * ACK and CTS are 10 bytes, all others 16. To see how
374 * to get this condition consider
375 * subtype mask: 0b0000000011110000 (0x00F0)
376 * ACK subtype: 0b0000000011010000 (0x00D0)
377 * CTS subtype: 0b0000000011000000 (0x00C0)
378 * bits that matter: ^^^ (0x00E0)
379 * value of those: 0b0000000011000000 (0x00C0)
381 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
389 EXPORT_SYMBOL(ieee80211_hdrlen);
391 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
393 const struct ieee80211_hdr *hdr =
394 (const struct ieee80211_hdr *)skb->data;
397 if (unlikely(skb->len < 10))
399 hdrlen = ieee80211_hdrlen(hdr->frame_control);
400 if (unlikely(hdrlen > skb->len))
404 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
406 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
408 int ae = flags & MESH_FLAGS_AE;
409 /* 802.11-2012, 8.2.4.7.3 */
414 case MESH_FLAGS_AE_A4:
416 case MESH_FLAGS_AE_A5_A6:
421 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
423 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
425 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
427 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
428 const u8 *addr, enum nl80211_iftype iftype,
431 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
433 u8 hdr[ETH_ALEN] __aligned(2);
440 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
443 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
444 if (skb->len < hdrlen + 8)
447 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
449 * IEEE 802.11 address fields:
450 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
451 * 0 0 DA SA BSSID n/a
452 * 0 1 DA BSSID SA n/a
453 * 1 0 BSSID SA DA n/a
456 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
457 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
459 if (iftype == NL80211_IFTYPE_MESH_POINT)
460 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
462 mesh_flags &= MESH_FLAGS_AE;
464 switch (hdr->frame_control &
465 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
466 case cpu_to_le16(IEEE80211_FCTL_TODS):
467 if (unlikely(iftype != NL80211_IFTYPE_AP &&
468 iftype != NL80211_IFTYPE_AP_VLAN &&
469 iftype != NL80211_IFTYPE_P2P_GO))
472 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
473 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
474 iftype != NL80211_IFTYPE_MESH_POINT &&
475 iftype != NL80211_IFTYPE_AP_VLAN &&
476 iftype != NL80211_IFTYPE_STATION))
478 if (iftype == NL80211_IFTYPE_MESH_POINT) {
479 if (mesh_flags == MESH_FLAGS_AE_A4)
481 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
482 skb_copy_bits(skb, hdrlen +
483 offsetof(struct ieee80211s_hdr, eaddr1),
484 tmp.h_dest, 2 * ETH_ALEN);
486 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
489 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
490 if ((iftype != NL80211_IFTYPE_STATION &&
491 iftype != NL80211_IFTYPE_P2P_CLIENT &&
492 iftype != NL80211_IFTYPE_MESH_POINT) ||
493 (is_multicast_ether_addr(tmp.h_dest) &&
494 ether_addr_equal(tmp.h_source, addr)))
496 if (iftype == NL80211_IFTYPE_MESH_POINT) {
497 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
499 if (mesh_flags == MESH_FLAGS_AE_A4)
500 skb_copy_bits(skb, hdrlen +
501 offsetof(struct ieee80211s_hdr, eaddr1),
502 tmp.h_source, ETH_ALEN);
503 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
507 if (iftype != NL80211_IFTYPE_ADHOC &&
508 iftype != NL80211_IFTYPE_STATION &&
509 iftype != NL80211_IFTYPE_OCB)
514 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
515 tmp.h_proto = payload.proto;
517 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
518 tmp.h_proto != htons(ETH_P_AARP) &&
519 tmp.h_proto != htons(ETH_P_IPX)) ||
520 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
521 /* remove RFC1042 or Bridge-Tunnel encapsulation and
522 * replace EtherType */
523 hdrlen += ETH_ALEN + 2;
525 tmp.h_proto = htons(skb->len - hdrlen);
527 pskb_pull(skb, hdrlen);
530 ehdr = skb_push(skb, sizeof(struct ethhdr));
531 memcpy(ehdr, &tmp, sizeof(tmp));
535 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
538 __frame_add_frag(struct sk_buff *skb, struct page *page,
539 void *ptr, int len, int size)
541 struct skb_shared_info *sh = skb_shinfo(skb);
545 page_offset = ptr - page_address(page);
546 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
550 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
553 struct skb_shared_info *sh = skb_shinfo(skb);
554 const skb_frag_t *frag = &sh->frags[0];
555 struct page *frag_page;
557 int frag_len, frag_size;
558 int head_size = skb->len - skb->data_len;
561 frag_page = virt_to_head_page(skb->head);
562 frag_ptr = skb->data;
563 frag_size = head_size;
565 while (offset >= frag_size) {
567 frag_page = skb_frag_page(frag);
568 frag_ptr = skb_frag_address(frag);
569 frag_size = skb_frag_size(frag);
574 frag_len = frag_size - offset;
576 cur_len = min(len, frag_len);
578 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
582 frag_len = skb_frag_size(frag);
583 cur_len = min(len, frag_len);
584 __frame_add_frag(frame, skb_frag_page(frag),
585 skb_frag_address(frag), cur_len, frag_len);
591 static struct sk_buff *
592 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
593 int offset, int len, bool reuse_frag)
595 struct sk_buff *frame;
598 if (skb->len - offset < len)
602 * When reusing framents, copy some data to the head to simplify
603 * ethernet header handling and speed up protocol header processing
604 * in the stack later.
607 cur_len = min_t(int, len, 32);
610 * Allocate and reserve two bytes more for payload
611 * alignment since sizeof(struct ethhdr) is 14.
613 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
617 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
618 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
625 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
630 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
631 const u8 *addr, enum nl80211_iftype iftype,
632 const unsigned int extra_headroom,
633 const u8 *check_da, const u8 *check_sa)
635 unsigned int hlen = ALIGN(extra_headroom, 4);
636 struct sk_buff *frame = NULL;
639 int offset = 0, remaining;
641 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
642 bool reuse_skb = false;
646 unsigned int subframe_len;
650 skb_copy_bits(skb, offset, ð, sizeof(eth));
651 len = ntohs(eth.h_proto);
652 subframe_len = sizeof(struct ethhdr) + len;
653 padding = (4 - subframe_len) & 0x3;
655 /* the last MSDU has no padding */
656 remaining = skb->len - offset;
657 if (subframe_len > remaining)
660 offset += sizeof(struct ethhdr);
661 last = remaining <= subframe_len + padding;
663 /* FIXME: should we really accept multicast DA? */
664 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
665 !ether_addr_equal(check_da, eth.h_dest)) ||
666 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
667 offset += len + padding;
671 /* reuse skb for the last subframe */
672 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
673 skb_pull(skb, offset);
677 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
682 offset += len + padding;
685 skb_reset_network_header(frame);
686 frame->dev = skb->dev;
687 frame->priority = skb->priority;
689 payload = frame->data;
690 ethertype = (payload[6] << 8) | payload[7];
691 if (likely((ether_addr_equal(payload, rfc1042_header) &&
692 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
693 ether_addr_equal(payload, bridge_tunnel_header))) {
694 eth.h_proto = htons(ethertype);
695 skb_pull(frame, ETH_ALEN + 2);
698 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
699 __skb_queue_tail(list, frame);
708 __skb_queue_purge(list);
711 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
713 /* Given a data frame determine the 802.1p/1d tag to use. */
714 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
715 struct cfg80211_qos_map *qos_map)
718 unsigned char vlan_priority;
721 /* skb->priority values from 256->263 are magic values to
722 * directly indicate a specific 802.1d priority. This is used
723 * to allow 802.1d priority to be passed directly in from VLAN
726 if (skb->priority >= 256 && skb->priority <= 263) {
727 ret = skb->priority - 256;
731 if (skb_vlan_tag_present(skb)) {
732 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
734 if (vlan_priority > 0) {
740 switch (skb->protocol) {
741 case htons(ETH_P_IP):
742 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
744 case htons(ETH_P_IPV6):
745 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
747 case htons(ETH_P_MPLS_UC):
748 case htons(ETH_P_MPLS_MC): {
749 struct mpls_label mpls_tmp, *mpls;
751 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
752 sizeof(*mpls), &mpls_tmp);
756 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
760 case htons(ETH_P_80221):
761 /* 802.21 is always network control traffic */
768 unsigned int i, tmp_dscp = dscp >> 2;
770 for (i = 0; i < qos_map->num_des; i++) {
771 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
772 ret = qos_map->dscp_exception[i].up;
777 for (i = 0; i < 8; i++) {
778 if (tmp_dscp >= qos_map->up[i].low &&
779 tmp_dscp <= qos_map->up[i].high) {
788 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
790 EXPORT_SYMBOL(cfg80211_classify8021d);
792 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
794 const struct cfg80211_bss_ies *ies;
796 ies = rcu_dereference(bss->ies);
800 return cfg80211_find_elem(id, ies->data, ies->len);
802 EXPORT_SYMBOL(ieee80211_bss_get_elem);
804 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
806 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
807 struct net_device *dev = wdev->netdev;
810 if (!wdev->connect_keys)
813 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
814 if (!wdev->connect_keys->params[i].cipher)
816 if (rdev_add_key(rdev, dev, i, false, NULL,
817 &wdev->connect_keys->params[i])) {
818 netdev_err(dev, "failed to set key %d\n", i);
821 if (wdev->connect_keys->def == i &&
822 rdev_set_default_key(rdev, dev, i, true, true)) {
823 netdev_err(dev, "failed to set defkey %d\n", i);
828 kzfree(wdev->connect_keys);
829 wdev->connect_keys = NULL;
832 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
834 struct cfg80211_event *ev;
837 spin_lock_irqsave(&wdev->event_lock, flags);
838 while (!list_empty(&wdev->event_list)) {
839 ev = list_first_entry(&wdev->event_list,
840 struct cfg80211_event, list);
842 spin_unlock_irqrestore(&wdev->event_lock, flags);
846 case EVENT_CONNECT_RESULT:
847 __cfg80211_connect_result(
850 ev->cr.status == WLAN_STATUS_SUCCESS);
853 __cfg80211_roamed(wdev, &ev->rm);
855 case EVENT_DISCONNECTED:
856 __cfg80211_disconnected(wdev->netdev,
857 ev->dc.ie, ev->dc.ie_len,
859 !ev->dc.locally_generated);
861 case EVENT_IBSS_JOINED:
862 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
866 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
868 case EVENT_PORT_AUTHORIZED:
869 __cfg80211_port_authorized(wdev, ev->pa.bssid);
876 spin_lock_irqsave(&wdev->event_lock, flags);
878 spin_unlock_irqrestore(&wdev->event_lock, flags);
881 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
883 struct wireless_dev *wdev;
887 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
888 cfg80211_process_wdev_events(wdev);
891 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
892 struct net_device *dev, enum nl80211_iftype ntype,
893 struct vif_params *params)
896 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
900 /* don't support changing VLANs, you just re-create them */
901 if (otype == NL80211_IFTYPE_AP_VLAN)
904 /* cannot change into P2P device or NAN */
905 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
906 ntype == NL80211_IFTYPE_NAN)
909 if (!rdev->ops->change_virtual_intf ||
910 !(rdev->wiphy.interface_modes & (1 << ntype)))
913 /* if it's part of a bridge, reject changing type to station/ibss */
914 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
915 (ntype == NL80211_IFTYPE_ADHOC ||
916 ntype == NL80211_IFTYPE_STATION ||
917 ntype == NL80211_IFTYPE_P2P_CLIENT))
920 if (ntype != otype) {
921 dev->ieee80211_ptr->use_4addr = false;
922 dev->ieee80211_ptr->mesh_id_up_len = 0;
923 wdev_lock(dev->ieee80211_ptr);
924 rdev_set_qos_map(rdev, dev, NULL);
925 wdev_unlock(dev->ieee80211_ptr);
928 case NL80211_IFTYPE_AP:
929 cfg80211_stop_ap(rdev, dev, true);
931 case NL80211_IFTYPE_ADHOC:
932 cfg80211_leave_ibss(rdev, dev, false);
934 case NL80211_IFTYPE_STATION:
935 case NL80211_IFTYPE_P2P_CLIENT:
936 wdev_lock(dev->ieee80211_ptr);
937 cfg80211_disconnect(rdev, dev,
938 WLAN_REASON_DEAUTH_LEAVING, true);
939 wdev_unlock(dev->ieee80211_ptr);
941 case NL80211_IFTYPE_MESH_POINT:
942 /* mesh should be handled? */
948 cfg80211_process_rdev_events(rdev);
951 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
953 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
955 if (!err && params && params->use_4addr != -1)
956 dev->ieee80211_ptr->use_4addr = params->use_4addr;
959 dev->priv_flags &= ~IFF_DONT_BRIDGE;
961 case NL80211_IFTYPE_STATION:
962 if (dev->ieee80211_ptr->use_4addr)
965 case NL80211_IFTYPE_OCB:
966 case NL80211_IFTYPE_P2P_CLIENT:
967 case NL80211_IFTYPE_ADHOC:
968 dev->priv_flags |= IFF_DONT_BRIDGE;
970 case NL80211_IFTYPE_P2P_GO:
971 case NL80211_IFTYPE_AP:
972 case NL80211_IFTYPE_AP_VLAN:
973 case NL80211_IFTYPE_WDS:
974 case NL80211_IFTYPE_MESH_POINT:
977 case NL80211_IFTYPE_MONITOR:
978 /* monitor can't bridge anyway */
980 case NL80211_IFTYPE_UNSPECIFIED:
981 case NUM_NL80211_IFTYPES:
984 case NL80211_IFTYPE_P2P_DEVICE:
985 case NL80211_IFTYPE_NAN:
991 if (!err && ntype != otype && netif_running(dev)) {
992 cfg80211_update_iface_num(rdev, ntype, 1);
993 cfg80211_update_iface_num(rdev, otype, -1);
999 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1001 int modulation, streams, bitrate;
1003 /* the formula below does only work for MCS values smaller than 32 */
1004 if (WARN_ON_ONCE(rate->mcs >= 32))
1007 modulation = rate->mcs & 7;
1008 streams = (rate->mcs >> 3) + 1;
1010 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1013 bitrate *= (modulation + 1);
1014 else if (modulation == 4)
1015 bitrate *= (modulation + 2);
1017 bitrate *= (modulation + 3);
1021 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1022 bitrate = (bitrate / 9) * 10;
1024 /* do NOT round down here */
1025 return (bitrate + 50000) / 100000;
1028 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1030 static const u32 __mcs2bitrate[] = {
1038 [5] = 12512, /* 1251.25 mbps */
1048 [14] = 8662, /* 866.25 mbps */
1058 [24] = 67568, /* 6756.75 mbps */
1069 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1072 return __mcs2bitrate[rate->mcs];
1075 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1077 static const u32 base[4][10] = {
1087 /* not in the spec, but some devices use this: */
1131 case RATE_INFO_BW_160:
1134 case RATE_INFO_BW_80:
1137 case RATE_INFO_BW_40:
1140 case RATE_INFO_BW_5:
1141 case RATE_INFO_BW_10:
1144 case RATE_INFO_BW_20:
1148 bitrate = base[idx][rate->mcs];
1149 bitrate *= rate->nss;
1151 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1152 bitrate = (bitrate / 9) * 10;
1154 /* do NOT round down here */
1155 return (bitrate + 50000) / 100000;
1157 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1158 rate->bw, rate->mcs, rate->nss);
1162 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1165 u16 mcs_divisors[12] = {
1166 34133, /* 16.666666... */
1167 17067, /* 8.333333... */
1168 11378, /* 5.555555... */
1169 8533, /* 4.166666... */
1170 5689, /* 2.777777... */
1171 4267, /* 2.083333... */
1172 3923, /* 1.851851... */
1173 3413, /* 1.666666... */
1174 2844, /* 1.388888... */
1175 2560, /* 1.250000... */
1176 2276, /* 1.111111... */
1177 2048, /* 1.000000... */
1179 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1180 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1181 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1182 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1183 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1184 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1185 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1189 if (WARN_ON_ONCE(rate->mcs > 11))
1192 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1194 if (WARN_ON_ONCE(rate->he_ru_alloc >
1195 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1197 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1200 if (rate->bw == RATE_INFO_BW_160)
1201 result = rates_160M[rate->he_gi];
1202 else if (rate->bw == RATE_INFO_BW_80 ||
1203 (rate->bw == RATE_INFO_BW_HE_RU &&
1204 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1205 result = rates_969[rate->he_gi];
1206 else if (rate->bw == RATE_INFO_BW_40 ||
1207 (rate->bw == RATE_INFO_BW_HE_RU &&
1208 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1209 result = rates_484[rate->he_gi];
1210 else if (rate->bw == RATE_INFO_BW_20 ||
1211 (rate->bw == RATE_INFO_BW_HE_RU &&
1212 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1213 result = rates_242[rate->he_gi];
1214 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1215 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1216 result = rates_106[rate->he_gi];
1217 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1218 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1219 result = rates_52[rate->he_gi];
1220 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1221 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1222 result = rates_26[rate->he_gi];
1223 else if (WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1224 rate->bw, rate->he_ru_alloc))
1227 /* now scale to the appropriate MCS */
1230 do_div(tmp, mcs_divisors[rate->mcs]);
1233 /* and take NSS, DCM into account */
1234 result = (result * rate->nss) / 8;
1241 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1243 if (rate->flags & RATE_INFO_FLAGS_MCS)
1244 return cfg80211_calculate_bitrate_ht(rate);
1245 if (rate->flags & RATE_INFO_FLAGS_60G)
1246 return cfg80211_calculate_bitrate_60g(rate);
1247 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1248 return cfg80211_calculate_bitrate_vht(rate);
1249 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1250 return cfg80211_calculate_bitrate_he(rate);
1252 return rate->legacy;
1254 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1256 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1257 enum ieee80211_p2p_attr_id attr,
1258 u8 *buf, unsigned int bufsize)
1261 u16 attr_remaining = 0;
1262 bool desired_attr = false;
1263 u16 desired_len = 0;
1266 unsigned int iedatalen;
1273 if (iedatalen + 2 > len)
1276 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1284 /* check WFA OUI, P2P subtype */
1285 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1286 iedata[2] != 0x9a || iedata[3] != 0x09)
1292 /* check attribute continuation into this IE */
1293 copy = min_t(unsigned int, attr_remaining, iedatalen);
1294 if (copy && desired_attr) {
1295 desired_len += copy;
1297 memcpy(out, iedata, min(bufsize, copy));
1298 out += min(bufsize, copy);
1299 bufsize -= min(bufsize, copy);
1303 if (copy == attr_remaining)
1307 attr_remaining -= copy;
1314 while (iedatalen > 0) {
1317 /* P2P attribute ID & size must fit */
1320 desired_attr = iedata[0] == attr;
1321 attr_len = get_unaligned_le16(iedata + 1);
1325 copy = min_t(unsigned int, attr_len, iedatalen);
1328 desired_len += copy;
1330 memcpy(out, iedata, min(bufsize, copy));
1331 out += min(bufsize, copy);
1332 bufsize -= min(bufsize, copy);
1335 if (copy == attr_len)
1341 attr_remaining = attr_len - copy;
1349 if (attr_remaining && desired_attr)
1354 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1356 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1360 /* Make sure array values are legal */
1361 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1366 if (ids[i] == WLAN_EID_EXTENSION) {
1367 if (id_ext && (ids[i + 1] == id))
1374 if (ids[i] == id && !id_ext)
1382 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1384 /* we assume a validly formed IEs buffer */
1385 u8 len = ies[pos + 1];
1389 /* the IE itself must have 255 bytes for fragments to follow */
1393 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1401 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1402 const u8 *ids, int n_ids,
1403 const u8 *after_ric, int n_after_ric,
1406 size_t pos = offset;
1408 while (pos < ielen) {
1411 if (ies[pos] == WLAN_EID_EXTENSION)
1413 if ((pos + ext) >= ielen)
1416 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1417 ies[pos] == WLAN_EID_EXTENSION))
1420 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1421 pos = skip_ie(ies, ielen, pos);
1423 while (pos < ielen) {
1424 if (ies[pos] == WLAN_EID_EXTENSION)
1429 if ((pos + ext) >= ielen)
1432 if (!ieee80211_id_in_list(after_ric,
1436 pos = skip_ie(ies, ielen, pos);
1441 pos = skip_ie(ies, ielen, pos);
1447 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1449 bool ieee80211_operating_class_to_band(u8 operating_class,
1450 enum nl80211_band *band)
1452 switch (operating_class) {
1456 *band = NL80211_BAND_5GHZ;
1462 *band = NL80211_BAND_2GHZ;
1465 *band = NL80211_BAND_60GHZ;
1471 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1473 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1477 u32 freq = chandef->center_freq1;
1479 if (freq >= 2412 && freq <= 2472) {
1480 if (chandef->width > NL80211_CHAN_WIDTH_40)
1483 /* 2.407 GHz, channels 1..13 */
1484 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1485 if (freq > chandef->chan->center_freq)
1486 *op_class = 83; /* HT40+ */
1488 *op_class = 84; /* HT40- */
1497 if (chandef->width > NL80211_CHAN_WIDTH_40)
1500 *op_class = 82; /* channel 14 */
1504 switch (chandef->width) {
1505 case NL80211_CHAN_WIDTH_80:
1508 case NL80211_CHAN_WIDTH_160:
1511 case NL80211_CHAN_WIDTH_80P80:
1514 case NL80211_CHAN_WIDTH_10:
1515 case NL80211_CHAN_WIDTH_5:
1516 return false; /* unsupported for now */
1522 /* 5 GHz, channels 36..48 */
1523 if (freq >= 5180 && freq <= 5240) {
1525 *op_class = vht_opclass;
1526 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1527 if (freq > chandef->chan->center_freq)
1538 /* 5 GHz, channels 52..64 */
1539 if (freq >= 5260 && freq <= 5320) {
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 100..144 */
1555 if (freq >= 5500 && freq <= 5720) {
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 149..169 */
1571 if (freq >= 5745 && freq <= 5845) {
1573 *op_class = vht_opclass;
1574 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1575 if (freq > chandef->chan->center_freq)
1579 } else if (freq <= 5805) {
1588 /* 56.16 GHz, channel 1..4 */
1589 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1590 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1597 /* not supported yet */
1600 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1602 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1603 u32 *beacon_int_gcd,
1604 bool *beacon_int_different)
1606 struct wireless_dev *wdev;
1608 *beacon_int_gcd = 0;
1609 *beacon_int_different = false;
1611 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1612 if (!wdev->beacon_interval)
1615 if (!*beacon_int_gcd) {
1616 *beacon_int_gcd = wdev->beacon_interval;
1620 if (wdev->beacon_interval == *beacon_int_gcd)
1623 *beacon_int_different = true;
1624 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1627 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1628 if (*beacon_int_gcd)
1629 *beacon_int_different = true;
1630 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1634 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1635 enum nl80211_iftype iftype, u32 beacon_int)
1638 * This is just a basic pre-condition check; if interface combinations
1639 * are possible the driver must already be checking those with a call
1640 * to cfg80211_check_combinations(), in which case we'll validate more
1641 * through the cfg80211_calculate_bi_data() call and code in
1642 * cfg80211_iter_combinations().
1645 if (beacon_int < 10 || beacon_int > 10000)
1651 int cfg80211_iter_combinations(struct wiphy *wiphy,
1652 struct iface_combination_params *params,
1653 void (*iter)(const struct ieee80211_iface_combination *c,
1657 const struct ieee80211_regdomain *regdom;
1658 enum nl80211_dfs_regions region = 0;
1660 int num_interfaces = 0;
1661 u32 used_iftypes = 0;
1663 bool beacon_int_different;
1666 * This is a bit strange, since the iteration used to rely only on
1667 * the data given by the driver, but here it now relies on context,
1668 * in form of the currently operating interfaces.
1669 * This is OK for all current users, and saves us from having to
1670 * push the GCD calculations into all the drivers.
1671 * In the future, this should probably rely more on data that's in
1672 * cfg80211 already - the only thing not would appear to be any new
1673 * interfaces (while being brought up) and channel/radar data.
1675 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1676 &beacon_int_gcd, &beacon_int_different);
1678 if (params->radar_detect) {
1680 regdom = rcu_dereference(cfg80211_regdomain);
1682 region = regdom->dfs_region;
1686 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1687 num_interfaces += params->iftype_num[iftype];
1688 if (params->iftype_num[iftype] > 0 &&
1689 !(wiphy->software_iftypes & BIT(iftype)))
1690 used_iftypes |= BIT(iftype);
1693 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1694 const struct ieee80211_iface_combination *c;
1695 struct ieee80211_iface_limit *limits;
1696 u32 all_iftypes = 0;
1698 c = &wiphy->iface_combinations[i];
1700 if (num_interfaces > c->max_interfaces)
1702 if (params->num_different_channels > c->num_different_channels)
1705 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1710 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1711 if (wiphy->software_iftypes & BIT(iftype))
1713 for (j = 0; j < c->n_limits; j++) {
1714 all_iftypes |= limits[j].types;
1715 if (!(limits[j].types & BIT(iftype)))
1717 if (limits[j].max < params->iftype_num[iftype])
1719 limits[j].max -= params->iftype_num[iftype];
1723 if (params->radar_detect !=
1724 (c->radar_detect_widths & params->radar_detect))
1727 if (params->radar_detect && c->radar_detect_regions &&
1728 !(c->radar_detect_regions & BIT(region)))
1731 /* Finally check that all iftypes that we're currently
1732 * using are actually part of this combination. If they
1733 * aren't then we can't use this combination and have
1734 * to continue to the next.
1736 if ((all_iftypes & used_iftypes) != used_iftypes)
1739 if (beacon_int_gcd) {
1740 if (c->beacon_int_min_gcd &&
1741 beacon_int_gcd < c->beacon_int_min_gcd)
1743 if (!c->beacon_int_min_gcd && beacon_int_different)
1747 /* This combination covered all interface types and
1748 * supported the requested numbers, so we're good.
1758 EXPORT_SYMBOL(cfg80211_iter_combinations);
1761 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1768 int cfg80211_check_combinations(struct wiphy *wiphy,
1769 struct iface_combination_params *params)
1773 err = cfg80211_iter_combinations(wiphy, params,
1774 cfg80211_iter_sum_ifcombs, &num);
1782 EXPORT_SYMBOL(cfg80211_check_combinations);
1784 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1785 const u8 *rates, unsigned int n_rates,
1793 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1798 for (i = 0; i < n_rates; i++) {
1799 int rate = (rates[i] & 0x7f) * 5;
1802 for (j = 0; j < sband->n_bitrates; j++) {
1803 if (sband->bitrates[j].bitrate == rate) {
1814 * mask must have at least one bit set here since we
1815 * didn't accept a 0-length rates array nor allowed
1816 * entries in the array that didn't exist
1822 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1824 enum nl80211_band band;
1825 unsigned int n_channels = 0;
1827 for (band = 0; band < NUM_NL80211_BANDS; band++)
1828 if (wiphy->bands[band])
1829 n_channels += wiphy->bands[band]->n_channels;
1833 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1835 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1836 struct station_info *sinfo)
1838 struct cfg80211_registered_device *rdev;
1839 struct wireless_dev *wdev;
1841 wdev = dev->ieee80211_ptr;
1845 rdev = wiphy_to_rdev(wdev->wiphy);
1846 if (!rdev->ops->get_station)
1849 memset(sinfo, 0, sizeof(*sinfo));
1851 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1853 EXPORT_SYMBOL(cfg80211_get_station);
1855 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1862 kfree(f->serv_spec_info);
1865 for (i = 0; i < f->num_rx_filters; i++)
1866 kfree(f->rx_filters[i].filter);
1868 for (i = 0; i < f->num_tx_filters; i++)
1869 kfree(f->tx_filters[i].filter);
1871 kfree(f->rx_filters);
1872 kfree(f->tx_filters);
1875 EXPORT_SYMBOL(cfg80211_free_nan_func);
1877 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1878 u32 center_freq_khz, u32 bw_khz)
1880 u32 start_freq_khz, end_freq_khz;
1882 start_freq_khz = center_freq_khz - (bw_khz / 2);
1883 end_freq_khz = center_freq_khz + (bw_khz / 2);
1885 if (start_freq_khz >= freq_range->start_freq_khz &&
1886 end_freq_khz <= freq_range->end_freq_khz)
1892 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1894 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1895 sizeof(*(sinfo->pertid)),
1902 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1904 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1905 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1906 const unsigned char rfc1042_header[] __aligned(2) =
1907 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1908 EXPORT_SYMBOL(rfc1042_header);
1910 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1911 const unsigned char bridge_tunnel_header[] __aligned(2) =
1912 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1913 EXPORT_SYMBOL(bridge_tunnel_header);
1915 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1916 struct iapp_layer2_update {
1917 u8 da[ETH_ALEN]; /* broadcast */
1918 u8 sa[ETH_ALEN]; /* STA addr */
1926 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1928 struct iapp_layer2_update *msg;
1929 struct sk_buff *skb;
1931 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1934 skb = dev_alloc_skb(sizeof(*msg));
1937 msg = skb_put(skb, sizeof(*msg));
1939 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1940 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1942 eth_broadcast_addr(msg->da);
1943 ether_addr_copy(msg->sa, addr);
1944 msg->len = htons(6);
1946 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1947 msg->control = 0xaf; /* XID response lsb.1111F101.
1948 * F=0 (no poll command; unsolicited frame) */
1949 msg->xid_info[0] = 0x81; /* XID format identifier */
1950 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1951 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1954 skb->protocol = eth_type_trans(skb, dev);
1955 memset(skb->cb, 0, sizeof(skb->cb));
1958 EXPORT_SYMBOL(cfg80211_send_layer2_update);
1960 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1961 enum ieee80211_vht_chanwidth bw,
1962 int mcs, bool ext_nss_bw_capable)
1964 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1965 int max_vht_nss = 0;
1968 int i, mcs_encoding;
1973 if (WARN_ON(mcs > 9))
1982 /* find max_vht_nss for the given MCS */
1983 for (i = 7; i >= 0; i--) {
1984 int supp = (map >> (2 * i)) & 3;
1989 if (supp >= mcs_encoding) {
1995 if (!(cap->supp_mcs.tx_mcs_map &
1996 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
1999 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2000 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2001 supp_width = le32_get_bits(cap->vht_cap_info,
2002 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2004 /* if not capable, treat ext_nss_bw as 0 */
2005 if (!ext_nss_bw_capable)
2008 /* This is invalid */
2009 if (supp_width == 3)
2012 /* This is an invalid combination so pretend nothing is supported */
2013 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2017 * Cover all the special cases according to IEEE 802.11-2016
2018 * Table 9-250. All other cases are either factor of 1 or not
2022 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2023 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2024 if ((supp_width == 1 || supp_width == 2) &&
2026 return 2 * max_vht_nss;
2028 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2029 if (supp_width == 0 &&
2030 (ext_nss_bw == 1 || ext_nss_bw == 2))
2031 return max_vht_nss / 2;
2032 if (supp_width == 0 &&
2034 return (3 * max_vht_nss) / 4;
2035 if (supp_width == 1 &&
2037 return 2 * max_vht_nss;
2039 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2040 if (supp_width == 0 && ext_nss_bw == 1)
2041 return 0; /* not possible */
2042 if (supp_width == 0 &&
2044 return max_vht_nss / 2;
2045 if (supp_width == 0 &&
2047 return (3 * max_vht_nss) / 4;
2048 if (supp_width == 1 &&
2050 return 0; /* not possible */
2051 if (supp_width == 1 &&
2053 return max_vht_nss / 2;
2054 if (supp_width == 1 &&
2056 return (3 * max_vht_nss) / 4;
2060 /* not covered or invalid combination received */
2063 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);