1 // SPDX-License-Identifier: GPL-2.0
3 * Wireless utility functions
5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2017 Intel Deutschland GmbH
8 * Copyright (C) 2018-2019 Intel Corporation
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
27 struct ieee80211_rate *
28 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
29 u32 basic_rates, int bitrate)
31 struct ieee80211_rate *result = &sband->bitrates[0];
34 for (i = 0; i < sband->n_bitrates; i++) {
35 if (!(basic_rates & BIT(i)))
37 if (sband->bitrates[i].bitrate > bitrate)
39 result = &sband->bitrates[i];
44 EXPORT_SYMBOL(ieee80211_get_response_rate);
46 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
47 enum nl80211_bss_scan_width scan_width)
49 struct ieee80211_rate *bitrates;
50 u32 mandatory_rates = 0;
51 enum ieee80211_rate_flags mandatory_flag;
57 if (sband->band == NL80211_BAND_2GHZ) {
58 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
59 scan_width == NL80211_BSS_CHAN_WIDTH_10)
60 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
62 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
64 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
67 bitrates = sband->bitrates;
68 for (i = 0; i < sband->n_bitrates; i++)
69 if (bitrates[i].flags & mandatory_flag)
70 mandatory_rates |= BIT(i);
71 return mandatory_rates;
73 EXPORT_SYMBOL(ieee80211_mandatory_rates);
75 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
77 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
80 return 0; /* not supported */
82 case NL80211_BAND_2GHZ:
86 return 2407 + chan * 5;
88 case NL80211_BAND_5GHZ:
89 if (chan >= 182 && chan <= 196)
90 return 4000 + chan * 5;
92 return 5000 + chan * 5;
94 case NL80211_BAND_6GHZ:
95 /* see 802.11ax D4.1 27.3.22.2 */
97 return 5940 + chan * 5;
99 case NL80211_BAND_60GHZ:
101 return 56160 + chan * 2160;
106 return 0; /* not supported */
108 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
110 int ieee80211_frequency_to_channel(int freq)
112 /* see 802.11 17.3.8.3.2 and Annex J */
115 else if (freq < 2484)
116 return (freq - 2407) / 5;
117 else if (freq >= 4910 && freq <= 4980)
118 return (freq - 4000) / 5;
119 else if (freq < 5945)
120 return (freq - 5000) / 5;
121 else if (freq <= 45000) /* DMG band lower limit */
122 /* see 802.11ax D4.1 27.3.22.2 */
123 return (freq - 5940) / 5;
124 else if (freq >= 58320 && freq <= 70200)
125 return (freq - 56160) / 2160;
129 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
131 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
133 enum nl80211_band band;
134 struct ieee80211_supported_band *sband;
137 for (band = 0; band < NUM_NL80211_BANDS; band++) {
138 sband = wiphy->bands[band];
143 for (i = 0; i < sband->n_channels; i++) {
144 if (sband->channels[i].center_freq == freq)
145 return &sband->channels[i];
151 EXPORT_SYMBOL(ieee80211_get_channel);
153 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
157 switch (sband->band) {
158 case NL80211_BAND_5GHZ:
159 case NL80211_BAND_6GHZ:
161 for (i = 0; i < sband->n_bitrates; i++) {
162 if (sband->bitrates[i].bitrate == 60 ||
163 sband->bitrates[i].bitrate == 120 ||
164 sband->bitrates[i].bitrate == 240) {
165 sband->bitrates[i].flags |=
166 IEEE80211_RATE_MANDATORY_A;
172 case NL80211_BAND_2GHZ:
174 for (i = 0; i < sband->n_bitrates; i++) {
175 switch (sband->bitrates[i].bitrate) {
180 sband->bitrates[i].flags |=
181 IEEE80211_RATE_MANDATORY_B |
182 IEEE80211_RATE_MANDATORY_G;
188 sband->bitrates[i].flags |=
189 IEEE80211_RATE_MANDATORY_G;
193 sband->bitrates[i].flags |=
194 IEEE80211_RATE_ERP_G;
198 WARN_ON(want != 0 && want != 3);
200 case NL80211_BAND_60GHZ:
201 /* check for mandatory HT MCS 1..4 */
202 WARN_ON(!sband->ht_cap.ht_supported);
203 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
205 case NUM_NL80211_BANDS:
212 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
214 enum nl80211_band band;
216 for (band = 0; band < NUM_NL80211_BANDS; band++)
217 if (wiphy->bands[band])
218 set_mandatory_flags_band(wiphy->bands[band]);
221 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
224 for (i = 0; i < wiphy->n_cipher_suites; i++)
225 if (cipher == wiphy->cipher_suites[i])
230 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
231 struct key_params *params, int key_idx,
232 bool pairwise, const u8 *mac_addr)
234 if (key_idx < 0 || key_idx > 5)
237 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
240 if (pairwise && !mac_addr)
243 switch (params->cipher) {
244 case WLAN_CIPHER_SUITE_TKIP:
245 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
246 if ((pairwise && key_idx) ||
247 params->mode != NL80211_KEY_RX_TX)
250 case WLAN_CIPHER_SUITE_CCMP:
251 case WLAN_CIPHER_SUITE_CCMP_256:
252 case WLAN_CIPHER_SUITE_GCMP:
253 case WLAN_CIPHER_SUITE_GCMP_256:
254 /* IEEE802.11-2016 allows only 0 and - when supporting
255 * Extended Key ID - 1 as index for pairwise keys.
256 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
257 * the driver supports Extended Key ID.
258 * @NL80211_KEY_SET_TX can't be set when installing and
261 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
262 params->mode == NL80211_KEY_SET_TX)
264 if (wiphy_ext_feature_isset(&rdev->wiphy,
265 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
266 if (pairwise && (key_idx < 0 || key_idx > 1))
268 } else if (pairwise && key_idx) {
272 case WLAN_CIPHER_SUITE_AES_CMAC:
273 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
274 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
275 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
276 /* Disallow BIP (group-only) cipher as pairwise cipher */
282 case WLAN_CIPHER_SUITE_WEP40:
283 case WLAN_CIPHER_SUITE_WEP104:
290 switch (params->cipher) {
291 case WLAN_CIPHER_SUITE_WEP40:
292 if (params->key_len != WLAN_KEY_LEN_WEP40)
295 case WLAN_CIPHER_SUITE_TKIP:
296 if (params->key_len != WLAN_KEY_LEN_TKIP)
299 case WLAN_CIPHER_SUITE_CCMP:
300 if (params->key_len != WLAN_KEY_LEN_CCMP)
303 case WLAN_CIPHER_SUITE_CCMP_256:
304 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
307 case WLAN_CIPHER_SUITE_GCMP:
308 if (params->key_len != WLAN_KEY_LEN_GCMP)
311 case WLAN_CIPHER_SUITE_GCMP_256:
312 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
315 case WLAN_CIPHER_SUITE_WEP104:
316 if (params->key_len != WLAN_KEY_LEN_WEP104)
319 case WLAN_CIPHER_SUITE_AES_CMAC:
320 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
323 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
324 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
327 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
328 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
331 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
332 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
337 * We don't know anything about this algorithm,
338 * allow using it -- but the driver must check
339 * all parameters! We still check below whether
340 * or not the driver supports this algorithm,
347 switch (params->cipher) {
348 case WLAN_CIPHER_SUITE_WEP40:
349 case WLAN_CIPHER_SUITE_WEP104:
350 /* These ciphers do not use key sequence */
352 case WLAN_CIPHER_SUITE_TKIP:
353 case WLAN_CIPHER_SUITE_CCMP:
354 case WLAN_CIPHER_SUITE_CCMP_256:
355 case WLAN_CIPHER_SUITE_GCMP:
356 case WLAN_CIPHER_SUITE_GCMP_256:
357 case WLAN_CIPHER_SUITE_AES_CMAC:
358 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
359 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
360 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
361 if (params->seq_len != 6)
367 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
373 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
375 unsigned int hdrlen = 24;
377 if (ieee80211_is_data(fc)) {
378 if (ieee80211_has_a4(fc))
380 if (ieee80211_is_data_qos(fc)) {
381 hdrlen += IEEE80211_QOS_CTL_LEN;
382 if (ieee80211_has_order(fc))
383 hdrlen += IEEE80211_HT_CTL_LEN;
388 if (ieee80211_is_mgmt(fc)) {
389 if (ieee80211_has_order(fc))
390 hdrlen += IEEE80211_HT_CTL_LEN;
394 if (ieee80211_is_ctl(fc)) {
396 * ACK and CTS are 10 bytes, all others 16. To see how
397 * to get this condition consider
398 * subtype mask: 0b0000000011110000 (0x00F0)
399 * ACK subtype: 0b0000000011010000 (0x00D0)
400 * CTS subtype: 0b0000000011000000 (0x00C0)
401 * bits that matter: ^^^ (0x00E0)
402 * value of those: 0b0000000011000000 (0x00C0)
404 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
412 EXPORT_SYMBOL(ieee80211_hdrlen);
414 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
416 const struct ieee80211_hdr *hdr =
417 (const struct ieee80211_hdr *)skb->data;
420 if (unlikely(skb->len < 10))
422 hdrlen = ieee80211_hdrlen(hdr->frame_control);
423 if (unlikely(hdrlen > skb->len))
427 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
429 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
431 int ae = flags & MESH_FLAGS_AE;
432 /* 802.11-2012, 8.2.4.7.3 */
437 case MESH_FLAGS_AE_A4:
439 case MESH_FLAGS_AE_A5_A6:
444 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
446 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
448 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
450 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
451 const u8 *addr, enum nl80211_iftype iftype,
454 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
456 u8 hdr[ETH_ALEN] __aligned(2);
463 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
466 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
467 if (skb->len < hdrlen + 8)
470 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
472 * IEEE 802.11 address fields:
473 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
474 * 0 0 DA SA BSSID n/a
475 * 0 1 DA BSSID SA n/a
476 * 1 0 BSSID SA DA n/a
479 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
480 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
482 if (iftype == NL80211_IFTYPE_MESH_POINT)
483 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
485 mesh_flags &= MESH_FLAGS_AE;
487 switch (hdr->frame_control &
488 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
489 case cpu_to_le16(IEEE80211_FCTL_TODS):
490 if (unlikely(iftype != NL80211_IFTYPE_AP &&
491 iftype != NL80211_IFTYPE_AP_VLAN &&
492 iftype != NL80211_IFTYPE_P2P_GO))
495 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
496 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
497 iftype != NL80211_IFTYPE_MESH_POINT &&
498 iftype != NL80211_IFTYPE_AP_VLAN &&
499 iftype != NL80211_IFTYPE_STATION))
501 if (iftype == NL80211_IFTYPE_MESH_POINT) {
502 if (mesh_flags == MESH_FLAGS_AE_A4)
504 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
505 skb_copy_bits(skb, hdrlen +
506 offsetof(struct ieee80211s_hdr, eaddr1),
507 tmp.h_dest, 2 * ETH_ALEN);
509 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
512 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
513 if ((iftype != NL80211_IFTYPE_STATION &&
514 iftype != NL80211_IFTYPE_P2P_CLIENT &&
515 iftype != NL80211_IFTYPE_MESH_POINT) ||
516 (is_multicast_ether_addr(tmp.h_dest) &&
517 ether_addr_equal(tmp.h_source, addr)))
519 if (iftype == NL80211_IFTYPE_MESH_POINT) {
520 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
522 if (mesh_flags == MESH_FLAGS_AE_A4)
523 skb_copy_bits(skb, hdrlen +
524 offsetof(struct ieee80211s_hdr, eaddr1),
525 tmp.h_source, ETH_ALEN);
526 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
530 if (iftype != NL80211_IFTYPE_ADHOC &&
531 iftype != NL80211_IFTYPE_STATION &&
532 iftype != NL80211_IFTYPE_OCB)
537 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
538 tmp.h_proto = payload.proto;
540 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
541 tmp.h_proto != htons(ETH_P_AARP) &&
542 tmp.h_proto != htons(ETH_P_IPX)) ||
543 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
544 /* remove RFC1042 or Bridge-Tunnel encapsulation and
545 * replace EtherType */
546 hdrlen += ETH_ALEN + 2;
548 tmp.h_proto = htons(skb->len - hdrlen);
550 pskb_pull(skb, hdrlen);
553 ehdr = skb_push(skb, sizeof(struct ethhdr));
554 memcpy(ehdr, &tmp, sizeof(tmp));
558 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
561 __frame_add_frag(struct sk_buff *skb, struct page *page,
562 void *ptr, int len, int size)
564 struct skb_shared_info *sh = skb_shinfo(skb);
568 page_offset = ptr - page_address(page);
569 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
573 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
576 struct skb_shared_info *sh = skb_shinfo(skb);
577 const skb_frag_t *frag = &sh->frags[0];
578 struct page *frag_page;
580 int frag_len, frag_size;
581 int head_size = skb->len - skb->data_len;
584 frag_page = virt_to_head_page(skb->head);
585 frag_ptr = skb->data;
586 frag_size = head_size;
588 while (offset >= frag_size) {
590 frag_page = skb_frag_page(frag);
591 frag_ptr = skb_frag_address(frag);
592 frag_size = skb_frag_size(frag);
597 frag_len = frag_size - offset;
599 cur_len = min(len, frag_len);
601 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
605 frag_len = skb_frag_size(frag);
606 cur_len = min(len, frag_len);
607 __frame_add_frag(frame, skb_frag_page(frag),
608 skb_frag_address(frag), cur_len, frag_len);
614 static struct sk_buff *
615 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
616 int offset, int len, bool reuse_frag)
618 struct sk_buff *frame;
621 if (skb->len - offset < len)
625 * When reusing framents, copy some data to the head to simplify
626 * ethernet header handling and speed up protocol header processing
627 * in the stack later.
630 cur_len = min_t(int, len, 32);
633 * Allocate and reserve two bytes more for payload
634 * alignment since sizeof(struct ethhdr) is 14.
636 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
640 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
641 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
648 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
653 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
654 const u8 *addr, enum nl80211_iftype iftype,
655 const unsigned int extra_headroom,
656 const u8 *check_da, const u8 *check_sa)
658 unsigned int hlen = ALIGN(extra_headroom, 4);
659 struct sk_buff *frame = NULL;
662 int offset = 0, remaining;
664 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
665 bool reuse_skb = false;
669 unsigned int subframe_len;
673 skb_copy_bits(skb, offset, ð, sizeof(eth));
674 len = ntohs(eth.h_proto);
675 subframe_len = sizeof(struct ethhdr) + len;
676 padding = (4 - subframe_len) & 0x3;
678 /* the last MSDU has no padding */
679 remaining = skb->len - offset;
680 if (subframe_len > remaining)
683 offset += sizeof(struct ethhdr);
684 last = remaining <= subframe_len + padding;
686 /* FIXME: should we really accept multicast DA? */
687 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
688 !ether_addr_equal(check_da, eth.h_dest)) ||
689 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
690 offset += len + padding;
694 /* reuse skb for the last subframe */
695 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
696 skb_pull(skb, offset);
700 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
705 offset += len + padding;
708 skb_reset_network_header(frame);
709 frame->dev = skb->dev;
710 frame->priority = skb->priority;
712 payload = frame->data;
713 ethertype = (payload[6] << 8) | payload[7];
714 if (likely((ether_addr_equal(payload, rfc1042_header) &&
715 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
716 ether_addr_equal(payload, bridge_tunnel_header))) {
717 eth.h_proto = htons(ethertype);
718 skb_pull(frame, ETH_ALEN + 2);
721 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
722 __skb_queue_tail(list, frame);
731 __skb_queue_purge(list);
734 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
736 /* Given a data frame determine the 802.1p/1d tag to use. */
737 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
738 struct cfg80211_qos_map *qos_map)
741 unsigned char vlan_priority;
744 /* skb->priority values from 256->263 are magic values to
745 * directly indicate a specific 802.1d priority. This is used
746 * to allow 802.1d priority to be passed directly in from VLAN
749 if (skb->priority >= 256 && skb->priority <= 263) {
750 ret = skb->priority - 256;
754 if (skb_vlan_tag_present(skb)) {
755 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
757 if (vlan_priority > 0) {
763 switch (skb->protocol) {
764 case htons(ETH_P_IP):
765 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
767 case htons(ETH_P_IPV6):
768 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
770 case htons(ETH_P_MPLS_UC):
771 case htons(ETH_P_MPLS_MC): {
772 struct mpls_label mpls_tmp, *mpls;
774 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
775 sizeof(*mpls), &mpls_tmp);
779 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
783 case htons(ETH_P_80221):
784 /* 802.21 is always network control traffic */
791 unsigned int i, tmp_dscp = dscp >> 2;
793 for (i = 0; i < qos_map->num_des; i++) {
794 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
795 ret = qos_map->dscp_exception[i].up;
800 for (i = 0; i < 8; i++) {
801 if (tmp_dscp >= qos_map->up[i].low &&
802 tmp_dscp <= qos_map->up[i].high) {
811 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
813 EXPORT_SYMBOL(cfg80211_classify8021d);
815 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
817 const struct cfg80211_bss_ies *ies;
819 ies = rcu_dereference(bss->ies);
823 return cfg80211_find_elem(id, ies->data, ies->len);
825 EXPORT_SYMBOL(ieee80211_bss_get_elem);
827 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
829 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
830 struct net_device *dev = wdev->netdev;
833 if (!wdev->connect_keys)
836 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
837 if (!wdev->connect_keys->params[i].cipher)
839 if (rdev_add_key(rdev, dev, i, false, NULL,
840 &wdev->connect_keys->params[i])) {
841 netdev_err(dev, "failed to set key %d\n", i);
844 if (wdev->connect_keys->def == i &&
845 rdev_set_default_key(rdev, dev, i, true, true)) {
846 netdev_err(dev, "failed to set defkey %d\n", i);
851 kzfree(wdev->connect_keys);
852 wdev->connect_keys = NULL;
855 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
857 struct cfg80211_event *ev;
860 spin_lock_irqsave(&wdev->event_lock, flags);
861 while (!list_empty(&wdev->event_list)) {
862 ev = list_first_entry(&wdev->event_list,
863 struct cfg80211_event, list);
865 spin_unlock_irqrestore(&wdev->event_lock, flags);
869 case EVENT_CONNECT_RESULT:
870 __cfg80211_connect_result(
873 ev->cr.status == WLAN_STATUS_SUCCESS);
876 __cfg80211_roamed(wdev, &ev->rm);
878 case EVENT_DISCONNECTED:
879 __cfg80211_disconnected(wdev->netdev,
880 ev->dc.ie, ev->dc.ie_len,
882 !ev->dc.locally_generated);
884 case EVENT_IBSS_JOINED:
885 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
889 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
891 case EVENT_PORT_AUTHORIZED:
892 __cfg80211_port_authorized(wdev, ev->pa.bssid);
899 spin_lock_irqsave(&wdev->event_lock, flags);
901 spin_unlock_irqrestore(&wdev->event_lock, flags);
904 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
906 struct wireless_dev *wdev;
910 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
911 cfg80211_process_wdev_events(wdev);
914 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
915 struct net_device *dev, enum nl80211_iftype ntype,
916 struct vif_params *params)
919 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
923 /* don't support changing VLANs, you just re-create them */
924 if (otype == NL80211_IFTYPE_AP_VLAN)
927 /* cannot change into P2P device or NAN */
928 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
929 ntype == NL80211_IFTYPE_NAN)
932 if (!rdev->ops->change_virtual_intf ||
933 !(rdev->wiphy.interface_modes & (1 << ntype)))
936 /* if it's part of a bridge, reject changing type to station/ibss */
937 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
938 (ntype == NL80211_IFTYPE_ADHOC ||
939 ntype == NL80211_IFTYPE_STATION ||
940 ntype == NL80211_IFTYPE_P2P_CLIENT))
943 if (ntype != otype) {
944 dev->ieee80211_ptr->use_4addr = false;
945 dev->ieee80211_ptr->mesh_id_up_len = 0;
946 wdev_lock(dev->ieee80211_ptr);
947 rdev_set_qos_map(rdev, dev, NULL);
948 wdev_unlock(dev->ieee80211_ptr);
951 case NL80211_IFTYPE_AP:
952 cfg80211_stop_ap(rdev, dev, true);
954 case NL80211_IFTYPE_ADHOC:
955 cfg80211_leave_ibss(rdev, dev, false);
957 case NL80211_IFTYPE_STATION:
958 case NL80211_IFTYPE_P2P_CLIENT:
959 wdev_lock(dev->ieee80211_ptr);
960 cfg80211_disconnect(rdev, dev,
961 WLAN_REASON_DEAUTH_LEAVING, true);
962 wdev_unlock(dev->ieee80211_ptr);
964 case NL80211_IFTYPE_MESH_POINT:
965 /* mesh should be handled? */
971 cfg80211_process_rdev_events(rdev);
972 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
975 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
977 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
979 if (!err && params && params->use_4addr != -1)
980 dev->ieee80211_ptr->use_4addr = params->use_4addr;
983 dev->priv_flags &= ~IFF_DONT_BRIDGE;
985 case NL80211_IFTYPE_STATION:
986 if (dev->ieee80211_ptr->use_4addr)
989 case NL80211_IFTYPE_OCB:
990 case NL80211_IFTYPE_P2P_CLIENT:
991 case NL80211_IFTYPE_ADHOC:
992 dev->priv_flags |= IFF_DONT_BRIDGE;
994 case NL80211_IFTYPE_P2P_GO:
995 case NL80211_IFTYPE_AP:
996 case NL80211_IFTYPE_AP_VLAN:
997 case NL80211_IFTYPE_WDS:
998 case NL80211_IFTYPE_MESH_POINT:
1001 case NL80211_IFTYPE_MONITOR:
1002 /* monitor can't bridge anyway */
1004 case NL80211_IFTYPE_UNSPECIFIED:
1005 case NUM_NL80211_IFTYPES:
1008 case NL80211_IFTYPE_P2P_DEVICE:
1009 case NL80211_IFTYPE_NAN:
1015 if (!err && ntype != otype && netif_running(dev)) {
1016 cfg80211_update_iface_num(rdev, ntype, 1);
1017 cfg80211_update_iface_num(rdev, otype, -1);
1023 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1025 int modulation, streams, bitrate;
1027 /* the formula below does only work for MCS values smaller than 32 */
1028 if (WARN_ON_ONCE(rate->mcs >= 32))
1031 modulation = rate->mcs & 7;
1032 streams = (rate->mcs >> 3) + 1;
1034 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1037 bitrate *= (modulation + 1);
1038 else if (modulation == 4)
1039 bitrate *= (modulation + 2);
1041 bitrate *= (modulation + 3);
1045 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1046 bitrate = (bitrate / 9) * 10;
1048 /* do NOT round down here */
1049 return (bitrate + 50000) / 100000;
1052 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1054 static const u32 __mcs2bitrate[] = {
1062 [5] = 12512, /* 1251.25 mbps */
1072 [14] = 8662, /* 866.25 mbps */
1082 [24] = 67568, /* 6756.75 mbps */
1093 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1096 return __mcs2bitrate[rate->mcs];
1099 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1101 static const u32 __mcs2bitrate[] = {
1109 [5] = 12512, /* 1251.25 mbps */
1127 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1130 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1133 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1135 static const u32 base[4][10] = {
1145 /* not in the spec, but some devices use this: */
1189 case RATE_INFO_BW_160:
1192 case RATE_INFO_BW_80:
1195 case RATE_INFO_BW_40:
1198 case RATE_INFO_BW_5:
1199 case RATE_INFO_BW_10:
1202 case RATE_INFO_BW_20:
1206 bitrate = base[idx][rate->mcs];
1207 bitrate *= rate->nss;
1209 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1210 bitrate = (bitrate / 9) * 10;
1212 /* do NOT round down here */
1213 return (bitrate + 50000) / 100000;
1215 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1216 rate->bw, rate->mcs, rate->nss);
1220 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1223 u16 mcs_divisors[12] = {
1224 34133, /* 16.666666... */
1225 17067, /* 8.333333... */
1226 11378, /* 5.555555... */
1227 8533, /* 4.166666... */
1228 5689, /* 2.777777... */
1229 4267, /* 2.083333... */
1230 3923, /* 1.851851... */
1231 3413, /* 1.666666... */
1232 2844, /* 1.388888... */
1233 2560, /* 1.250000... */
1234 2276, /* 1.111111... */
1235 2048, /* 1.000000... */
1237 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1238 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1239 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1240 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1241 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1242 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1243 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1247 if (WARN_ON_ONCE(rate->mcs > 11))
1250 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1252 if (WARN_ON_ONCE(rate->he_ru_alloc >
1253 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1255 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1258 if (rate->bw == RATE_INFO_BW_160)
1259 result = rates_160M[rate->he_gi];
1260 else if (rate->bw == RATE_INFO_BW_80 ||
1261 (rate->bw == RATE_INFO_BW_HE_RU &&
1262 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1263 result = rates_969[rate->he_gi];
1264 else if (rate->bw == RATE_INFO_BW_40 ||
1265 (rate->bw == RATE_INFO_BW_HE_RU &&
1266 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1267 result = rates_484[rate->he_gi];
1268 else if (rate->bw == RATE_INFO_BW_20 ||
1269 (rate->bw == RATE_INFO_BW_HE_RU &&
1270 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1271 result = rates_242[rate->he_gi];
1272 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1273 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1274 result = rates_106[rate->he_gi];
1275 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1276 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1277 result = rates_52[rate->he_gi];
1278 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1279 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1280 result = rates_26[rate->he_gi];
1282 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1283 rate->bw, rate->he_ru_alloc);
1287 /* now scale to the appropriate MCS */
1290 do_div(tmp, mcs_divisors[rate->mcs]);
1293 /* and take NSS, DCM into account */
1294 result = (result * rate->nss) / 8;
1298 return result / 10000;
1301 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1303 if (rate->flags & RATE_INFO_FLAGS_MCS)
1304 return cfg80211_calculate_bitrate_ht(rate);
1305 if (rate->flags & RATE_INFO_FLAGS_DMG)
1306 return cfg80211_calculate_bitrate_dmg(rate);
1307 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1308 return cfg80211_calculate_bitrate_edmg(rate);
1309 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1310 return cfg80211_calculate_bitrate_vht(rate);
1311 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1312 return cfg80211_calculate_bitrate_he(rate);
1314 return rate->legacy;
1316 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1318 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1319 enum ieee80211_p2p_attr_id attr,
1320 u8 *buf, unsigned int bufsize)
1323 u16 attr_remaining = 0;
1324 bool desired_attr = false;
1325 u16 desired_len = 0;
1328 unsigned int iedatalen;
1335 if (iedatalen + 2 > len)
1338 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1346 /* check WFA OUI, P2P subtype */
1347 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1348 iedata[2] != 0x9a || iedata[3] != 0x09)
1354 /* check attribute continuation into this IE */
1355 copy = min_t(unsigned int, attr_remaining, iedatalen);
1356 if (copy && desired_attr) {
1357 desired_len += copy;
1359 memcpy(out, iedata, min(bufsize, copy));
1360 out += min(bufsize, copy);
1361 bufsize -= min(bufsize, copy);
1365 if (copy == attr_remaining)
1369 attr_remaining -= copy;
1376 while (iedatalen > 0) {
1379 /* P2P attribute ID & size must fit */
1382 desired_attr = iedata[0] == attr;
1383 attr_len = get_unaligned_le16(iedata + 1);
1387 copy = min_t(unsigned int, attr_len, iedatalen);
1390 desired_len += copy;
1392 memcpy(out, iedata, min(bufsize, copy));
1393 out += min(bufsize, copy);
1394 bufsize -= min(bufsize, copy);
1397 if (copy == attr_len)
1403 attr_remaining = attr_len - copy;
1411 if (attr_remaining && desired_attr)
1416 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1418 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1422 /* Make sure array values are legal */
1423 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1428 if (ids[i] == WLAN_EID_EXTENSION) {
1429 if (id_ext && (ids[i + 1] == id))
1436 if (ids[i] == id && !id_ext)
1444 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1446 /* we assume a validly formed IEs buffer */
1447 u8 len = ies[pos + 1];
1451 /* the IE itself must have 255 bytes for fragments to follow */
1455 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1463 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1464 const u8 *ids, int n_ids,
1465 const u8 *after_ric, int n_after_ric,
1468 size_t pos = offset;
1470 while (pos < ielen) {
1473 if (ies[pos] == WLAN_EID_EXTENSION)
1475 if ((pos + ext) >= ielen)
1478 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1479 ies[pos] == WLAN_EID_EXTENSION))
1482 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1483 pos = skip_ie(ies, ielen, pos);
1485 while (pos < ielen) {
1486 if (ies[pos] == WLAN_EID_EXTENSION)
1491 if ((pos + ext) >= ielen)
1494 if (!ieee80211_id_in_list(after_ric,
1498 pos = skip_ie(ies, ielen, pos);
1503 pos = skip_ie(ies, ielen, pos);
1509 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1511 bool ieee80211_operating_class_to_band(u8 operating_class,
1512 enum nl80211_band *band)
1514 switch (operating_class) {
1518 *band = NL80211_BAND_5GHZ;
1521 *band = NL80211_BAND_6GHZ;
1527 *band = NL80211_BAND_2GHZ;
1530 *band = NL80211_BAND_60GHZ;
1536 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1538 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1542 u32 freq = chandef->center_freq1;
1544 if (freq >= 2412 && freq <= 2472) {
1545 if (chandef->width > NL80211_CHAN_WIDTH_40)
1548 /* 2.407 GHz, channels 1..13 */
1549 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1550 if (freq > chandef->chan->center_freq)
1551 *op_class = 83; /* HT40+ */
1553 *op_class = 84; /* HT40- */
1562 /* channel 14 is only for IEEE 802.11b */
1563 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
1566 *op_class = 82; /* channel 14 */
1570 switch (chandef->width) {
1571 case NL80211_CHAN_WIDTH_80:
1574 case NL80211_CHAN_WIDTH_160:
1577 case NL80211_CHAN_WIDTH_80P80:
1580 case NL80211_CHAN_WIDTH_10:
1581 case NL80211_CHAN_WIDTH_5:
1582 return false; /* unsupported for now */
1588 /* 5 GHz, channels 36..48 */
1589 if (freq >= 5180 && freq <= 5240) {
1591 *op_class = vht_opclass;
1592 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1593 if (freq > chandef->chan->center_freq)
1604 /* 5 GHz, channels 52..64 */
1605 if (freq >= 5260 && freq <= 5320) {
1607 *op_class = vht_opclass;
1608 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1609 if (freq > chandef->chan->center_freq)
1620 /* 5 GHz, channels 100..144 */
1621 if (freq >= 5500 && freq <= 5720) {
1623 *op_class = vht_opclass;
1624 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1625 if (freq > chandef->chan->center_freq)
1636 /* 5 GHz, channels 149..169 */
1637 if (freq >= 5745 && freq <= 5845) {
1639 *op_class = vht_opclass;
1640 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1641 if (freq > chandef->chan->center_freq)
1645 } else if (freq <= 5805) {
1654 /* 56.16 GHz, channel 1..4 */
1655 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1656 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1663 /* not supported yet */
1666 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1668 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1669 u32 *beacon_int_gcd,
1670 bool *beacon_int_different)
1672 struct wireless_dev *wdev;
1674 *beacon_int_gcd = 0;
1675 *beacon_int_different = false;
1677 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1678 if (!wdev->beacon_interval)
1681 if (!*beacon_int_gcd) {
1682 *beacon_int_gcd = wdev->beacon_interval;
1686 if (wdev->beacon_interval == *beacon_int_gcd)
1689 *beacon_int_different = true;
1690 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1693 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1694 if (*beacon_int_gcd)
1695 *beacon_int_different = true;
1696 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1700 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1701 enum nl80211_iftype iftype, u32 beacon_int)
1704 * This is just a basic pre-condition check; if interface combinations
1705 * are possible the driver must already be checking those with a call
1706 * to cfg80211_check_combinations(), in which case we'll validate more
1707 * through the cfg80211_calculate_bi_data() call and code in
1708 * cfg80211_iter_combinations().
1711 if (beacon_int < 10 || beacon_int > 10000)
1717 int cfg80211_iter_combinations(struct wiphy *wiphy,
1718 struct iface_combination_params *params,
1719 void (*iter)(const struct ieee80211_iface_combination *c,
1723 const struct ieee80211_regdomain *regdom;
1724 enum nl80211_dfs_regions region = 0;
1726 int num_interfaces = 0;
1727 u32 used_iftypes = 0;
1729 bool beacon_int_different;
1732 * This is a bit strange, since the iteration used to rely only on
1733 * the data given by the driver, but here it now relies on context,
1734 * in form of the currently operating interfaces.
1735 * This is OK for all current users, and saves us from having to
1736 * push the GCD calculations into all the drivers.
1737 * In the future, this should probably rely more on data that's in
1738 * cfg80211 already - the only thing not would appear to be any new
1739 * interfaces (while being brought up) and channel/radar data.
1741 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1742 &beacon_int_gcd, &beacon_int_different);
1744 if (params->radar_detect) {
1746 regdom = rcu_dereference(cfg80211_regdomain);
1748 region = regdom->dfs_region;
1752 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1753 num_interfaces += params->iftype_num[iftype];
1754 if (params->iftype_num[iftype] > 0 &&
1755 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1756 used_iftypes |= BIT(iftype);
1759 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1760 const struct ieee80211_iface_combination *c;
1761 struct ieee80211_iface_limit *limits;
1762 u32 all_iftypes = 0;
1764 c = &wiphy->iface_combinations[i];
1766 if (num_interfaces > c->max_interfaces)
1768 if (params->num_different_channels > c->num_different_channels)
1771 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1776 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1777 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1779 for (j = 0; j < c->n_limits; j++) {
1780 all_iftypes |= limits[j].types;
1781 if (!(limits[j].types & BIT(iftype)))
1783 if (limits[j].max < params->iftype_num[iftype])
1785 limits[j].max -= params->iftype_num[iftype];
1789 if (params->radar_detect !=
1790 (c->radar_detect_widths & params->radar_detect))
1793 if (params->radar_detect && c->radar_detect_regions &&
1794 !(c->radar_detect_regions & BIT(region)))
1797 /* Finally check that all iftypes that we're currently
1798 * using are actually part of this combination. If they
1799 * aren't then we can't use this combination and have
1800 * to continue to the next.
1802 if ((all_iftypes & used_iftypes) != used_iftypes)
1805 if (beacon_int_gcd) {
1806 if (c->beacon_int_min_gcd &&
1807 beacon_int_gcd < c->beacon_int_min_gcd)
1809 if (!c->beacon_int_min_gcd && beacon_int_different)
1813 /* This combination covered all interface types and
1814 * supported the requested numbers, so we're good.
1824 EXPORT_SYMBOL(cfg80211_iter_combinations);
1827 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1834 int cfg80211_check_combinations(struct wiphy *wiphy,
1835 struct iface_combination_params *params)
1839 err = cfg80211_iter_combinations(wiphy, params,
1840 cfg80211_iter_sum_ifcombs, &num);
1848 EXPORT_SYMBOL(cfg80211_check_combinations);
1850 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1851 const u8 *rates, unsigned int n_rates,
1859 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1864 for (i = 0; i < n_rates; i++) {
1865 int rate = (rates[i] & 0x7f) * 5;
1868 for (j = 0; j < sband->n_bitrates; j++) {
1869 if (sband->bitrates[j].bitrate == rate) {
1880 * mask must have at least one bit set here since we
1881 * didn't accept a 0-length rates array nor allowed
1882 * entries in the array that didn't exist
1888 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1890 enum nl80211_band band;
1891 unsigned int n_channels = 0;
1893 for (band = 0; band < NUM_NL80211_BANDS; band++)
1894 if (wiphy->bands[band])
1895 n_channels += wiphy->bands[band]->n_channels;
1899 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1901 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1902 struct station_info *sinfo)
1904 struct cfg80211_registered_device *rdev;
1905 struct wireless_dev *wdev;
1907 wdev = dev->ieee80211_ptr;
1911 rdev = wiphy_to_rdev(wdev->wiphy);
1912 if (!rdev->ops->get_station)
1915 memset(sinfo, 0, sizeof(*sinfo));
1917 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1919 EXPORT_SYMBOL(cfg80211_get_station);
1921 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1928 kfree(f->serv_spec_info);
1931 for (i = 0; i < f->num_rx_filters; i++)
1932 kfree(f->rx_filters[i].filter);
1934 for (i = 0; i < f->num_tx_filters; i++)
1935 kfree(f->tx_filters[i].filter);
1937 kfree(f->rx_filters);
1938 kfree(f->tx_filters);
1941 EXPORT_SYMBOL(cfg80211_free_nan_func);
1943 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1944 u32 center_freq_khz, u32 bw_khz)
1946 u32 start_freq_khz, end_freq_khz;
1948 start_freq_khz = center_freq_khz - (bw_khz / 2);
1949 end_freq_khz = center_freq_khz + (bw_khz / 2);
1951 if (start_freq_khz >= freq_range->start_freq_khz &&
1952 end_freq_khz <= freq_range->end_freq_khz)
1958 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1960 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1961 sizeof(*(sinfo->pertid)),
1968 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1970 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1971 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1972 const unsigned char rfc1042_header[] __aligned(2) =
1973 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1974 EXPORT_SYMBOL(rfc1042_header);
1976 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1977 const unsigned char bridge_tunnel_header[] __aligned(2) =
1978 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1979 EXPORT_SYMBOL(bridge_tunnel_header);
1981 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1982 struct iapp_layer2_update {
1983 u8 da[ETH_ALEN]; /* broadcast */
1984 u8 sa[ETH_ALEN]; /* STA addr */
1992 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1994 struct iapp_layer2_update *msg;
1995 struct sk_buff *skb;
1997 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2000 skb = dev_alloc_skb(sizeof(*msg));
2003 msg = skb_put(skb, sizeof(*msg));
2005 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2006 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2008 eth_broadcast_addr(msg->da);
2009 ether_addr_copy(msg->sa, addr);
2010 msg->len = htons(6);
2012 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2013 msg->control = 0xaf; /* XID response lsb.1111F101.
2014 * F=0 (no poll command; unsolicited frame) */
2015 msg->xid_info[0] = 0x81; /* XID format identifier */
2016 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2017 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2020 skb->protocol = eth_type_trans(skb, dev);
2021 memset(skb->cb, 0, sizeof(skb->cb));
2024 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2026 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2027 enum ieee80211_vht_chanwidth bw,
2028 int mcs, bool ext_nss_bw_capable)
2030 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2031 int max_vht_nss = 0;
2034 int i, mcs_encoding;
2039 if (WARN_ON(mcs > 9))
2048 /* find max_vht_nss for the given MCS */
2049 for (i = 7; i >= 0; i--) {
2050 int supp = (map >> (2 * i)) & 3;
2055 if (supp >= mcs_encoding) {
2056 max_vht_nss = i + 1;
2061 if (!(cap->supp_mcs.tx_mcs_map &
2062 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2065 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2066 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2067 supp_width = le32_get_bits(cap->vht_cap_info,
2068 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2070 /* if not capable, treat ext_nss_bw as 0 */
2071 if (!ext_nss_bw_capable)
2074 /* This is invalid */
2075 if (supp_width == 3)
2078 /* This is an invalid combination so pretend nothing is supported */
2079 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2083 * Cover all the special cases according to IEEE 802.11-2016
2084 * Table 9-250. All other cases are either factor of 1 or not
2088 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2089 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2090 if ((supp_width == 1 || supp_width == 2) &&
2092 return 2 * max_vht_nss;
2094 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2095 if (supp_width == 0 &&
2096 (ext_nss_bw == 1 || ext_nss_bw == 2))
2097 return max_vht_nss / 2;
2098 if (supp_width == 0 &&
2100 return (3 * max_vht_nss) / 4;
2101 if (supp_width == 1 &&
2103 return 2 * max_vht_nss;
2105 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2106 if (supp_width == 0 && ext_nss_bw == 1)
2107 return 0; /* not possible */
2108 if (supp_width == 0 &&
2110 return max_vht_nss / 2;
2111 if (supp_width == 0 &&
2113 return (3 * max_vht_nss) / 4;
2114 if (supp_width == 1 &&
2116 return 0; /* not possible */
2117 if (supp_width == 1 &&
2119 return max_vht_nss / 2;
2120 if (supp_width == 1 &&
2122 return (3 * max_vht_nss) / 4;
2126 /* not covered or invalid combination received */
2129 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2131 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2132 bool is_4addr, u8 check_swif)
2135 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2137 switch (check_swif) {
2139 if (is_vlan && is_4addr)
2140 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2141 return wiphy->interface_modes & BIT(iftype);
2143 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2144 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2145 return wiphy->software_iftypes & BIT(iftype);
2152 EXPORT_SYMBOL(cfg80211_iftype_allowed);