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
9 #include <linux/export.h>
10 #include <linux/bitops.h>
11 #include <linux/etherdevice.h>
12 #include <linux/slab.h>
13 #include <net/cfg80211.h>
15 #include <net/dsfield.h>
16 #include <linux/if_vlan.h>
17 #include <linux/mpls.h>
18 #include <linux/gcd.h>
23 struct ieee80211_rate *
24 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
25 u32 basic_rates, int bitrate)
27 struct ieee80211_rate *result = &sband->bitrates[0];
30 for (i = 0; i < sband->n_bitrates; i++) {
31 if (!(basic_rates & BIT(i)))
33 if (sband->bitrates[i].bitrate > bitrate)
35 result = &sband->bitrates[i];
40 EXPORT_SYMBOL(ieee80211_get_response_rate);
42 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
43 enum nl80211_bss_scan_width scan_width)
45 struct ieee80211_rate *bitrates;
46 u32 mandatory_rates = 0;
47 enum ieee80211_rate_flags mandatory_flag;
53 if (sband->band == NL80211_BAND_2GHZ) {
54 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
55 scan_width == NL80211_BSS_CHAN_WIDTH_10)
56 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
58 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
60 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
63 bitrates = sband->bitrates;
64 for (i = 0; i < sband->n_bitrates; i++)
65 if (bitrates[i].flags & mandatory_flag)
66 mandatory_rates |= BIT(i);
67 return mandatory_rates;
69 EXPORT_SYMBOL(ieee80211_mandatory_rates);
71 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
73 /* see 802.11 17.3.8.3.2 and Annex J
74 * there are overlapping channel numbers in 5GHz and 2GHz bands */
76 return 0; /* not supported */
78 case NL80211_BAND_2GHZ:
82 return 2407 + chan * 5;
84 case NL80211_BAND_5GHZ:
85 if (chan >= 182 && chan <= 196)
86 return 4000 + chan * 5;
88 return 5000 + chan * 5;
90 case NL80211_BAND_60GHZ:
92 return 56160 + chan * 2160;
97 return 0; /* not supported */
99 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
101 int ieee80211_frequency_to_channel(int freq)
103 /* see 802.11 17.3.8.3.2 and Annex J */
106 else if (freq < 2484)
107 return (freq - 2407) / 5;
108 else if (freq >= 4910 && freq <= 4980)
109 return (freq - 4000) / 5;
110 else if (freq <= 45000) /* DMG band lower limit */
111 return (freq - 5000) / 5;
112 else if (freq >= 58320 && freq <= 64800)
113 return (freq - 56160) / 2160;
117 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
119 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
121 enum nl80211_band band;
122 struct ieee80211_supported_band *sband;
125 for (band = 0; band < NUM_NL80211_BANDS; band++) {
126 sband = wiphy->bands[band];
131 for (i = 0; i < sband->n_channels; i++) {
132 if (sband->channels[i].center_freq == freq)
133 return &sband->channels[i];
139 EXPORT_SYMBOL(ieee80211_get_channel);
141 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
145 switch (sband->band) {
146 case NL80211_BAND_5GHZ:
148 for (i = 0; i < sband->n_bitrates; i++) {
149 if (sband->bitrates[i].bitrate == 60 ||
150 sband->bitrates[i].bitrate == 120 ||
151 sband->bitrates[i].bitrate == 240) {
152 sband->bitrates[i].flags |=
153 IEEE80211_RATE_MANDATORY_A;
159 case NL80211_BAND_2GHZ:
161 for (i = 0; i < sband->n_bitrates; i++) {
162 switch (sband->bitrates[i].bitrate) {
167 sband->bitrates[i].flags |=
168 IEEE80211_RATE_MANDATORY_B |
169 IEEE80211_RATE_MANDATORY_G;
175 sband->bitrates[i].flags |=
176 IEEE80211_RATE_MANDATORY_G;
180 sband->bitrates[i].flags |=
181 IEEE80211_RATE_ERP_G;
185 WARN_ON(want != 0 && want != 3);
187 case NL80211_BAND_60GHZ:
188 /* check for mandatory HT MCS 1..4 */
189 WARN_ON(!sband->ht_cap.ht_supported);
190 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
192 case NUM_NL80211_BANDS:
199 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
201 enum nl80211_band band;
203 for (band = 0; band < NUM_NL80211_BANDS; band++)
204 if (wiphy->bands[band])
205 set_mandatory_flags_band(wiphy->bands[band]);
208 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
211 for (i = 0; i < wiphy->n_cipher_suites; i++)
212 if (cipher == wiphy->cipher_suites[i])
218 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
220 struct wiphy *wiphy = &rdev->wiphy;
223 for (i = 0; i < wiphy->n_cipher_suites; i++) {
224 switch (wiphy->cipher_suites[i]) {
225 case WLAN_CIPHER_SUITE_AES_CMAC:
226 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
227 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
228 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
236 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
237 int key_idx, bool pairwise)
243 else if (cfg80211_igtk_cipher_supported(rdev))
248 if (key_idx < 0 || key_idx > max_key_idx)
254 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
255 struct key_params *params, int key_idx,
256 bool pairwise, const u8 *mac_addr)
258 if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
261 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
264 if (pairwise && !mac_addr)
267 switch (params->cipher) {
268 case WLAN_CIPHER_SUITE_TKIP:
269 case WLAN_CIPHER_SUITE_CCMP:
270 case WLAN_CIPHER_SUITE_CCMP_256:
271 case WLAN_CIPHER_SUITE_GCMP:
272 case WLAN_CIPHER_SUITE_GCMP_256:
273 /* Disallow pairwise keys with non-zero index unless it's WEP
274 * or a vendor specific cipher (because current deployments use
275 * pairwise WEP keys with non-zero indices and for vendor
276 * specific ciphers this should be validated in the driver or
277 * hardware level - but 802.11i clearly specifies to use zero)
279 if (pairwise && key_idx)
282 case WLAN_CIPHER_SUITE_AES_CMAC:
283 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
284 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
285 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
286 /* Disallow BIP (group-only) cipher as pairwise cipher */
292 case WLAN_CIPHER_SUITE_WEP40:
293 case WLAN_CIPHER_SUITE_WEP104:
300 switch (params->cipher) {
301 case WLAN_CIPHER_SUITE_WEP40:
302 if (params->key_len != WLAN_KEY_LEN_WEP40)
305 case WLAN_CIPHER_SUITE_TKIP:
306 if (params->key_len != WLAN_KEY_LEN_TKIP)
309 case WLAN_CIPHER_SUITE_CCMP:
310 if (params->key_len != WLAN_KEY_LEN_CCMP)
313 case WLAN_CIPHER_SUITE_CCMP_256:
314 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
317 case WLAN_CIPHER_SUITE_GCMP:
318 if (params->key_len != WLAN_KEY_LEN_GCMP)
321 case WLAN_CIPHER_SUITE_GCMP_256:
322 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
325 case WLAN_CIPHER_SUITE_WEP104:
326 if (params->key_len != WLAN_KEY_LEN_WEP104)
329 case WLAN_CIPHER_SUITE_AES_CMAC:
330 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
333 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
334 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
337 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
338 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
341 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
342 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
347 * We don't know anything about this algorithm,
348 * allow using it -- but the driver must check
349 * all parameters! We still check below whether
350 * or not the driver supports this algorithm,
357 switch (params->cipher) {
358 case WLAN_CIPHER_SUITE_WEP40:
359 case WLAN_CIPHER_SUITE_WEP104:
360 /* These ciphers do not use key sequence */
362 case WLAN_CIPHER_SUITE_TKIP:
363 case WLAN_CIPHER_SUITE_CCMP:
364 case WLAN_CIPHER_SUITE_CCMP_256:
365 case WLAN_CIPHER_SUITE_GCMP:
366 case WLAN_CIPHER_SUITE_GCMP_256:
367 case WLAN_CIPHER_SUITE_AES_CMAC:
368 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
369 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
370 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
371 if (params->seq_len != 6)
377 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
383 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
385 unsigned int hdrlen = 24;
387 if (ieee80211_is_data(fc)) {
388 if (ieee80211_has_a4(fc))
390 if (ieee80211_is_data_qos(fc)) {
391 hdrlen += IEEE80211_QOS_CTL_LEN;
392 if (ieee80211_has_order(fc))
393 hdrlen += IEEE80211_HT_CTL_LEN;
398 if (ieee80211_is_mgmt(fc)) {
399 if (ieee80211_has_order(fc))
400 hdrlen += IEEE80211_HT_CTL_LEN;
404 if (ieee80211_is_ctl(fc)) {
406 * ACK and CTS are 10 bytes, all others 16. To see how
407 * to get this condition consider
408 * subtype mask: 0b0000000011110000 (0x00F0)
409 * ACK subtype: 0b0000000011010000 (0x00D0)
410 * CTS subtype: 0b0000000011000000 (0x00C0)
411 * bits that matter: ^^^ (0x00E0)
412 * value of those: 0b0000000011000000 (0x00C0)
414 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
422 EXPORT_SYMBOL(ieee80211_hdrlen);
424 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
426 const struct ieee80211_hdr *hdr =
427 (const struct ieee80211_hdr *)skb->data;
430 if (unlikely(skb->len < 10))
432 hdrlen = ieee80211_hdrlen(hdr->frame_control);
433 if (unlikely(hdrlen > skb->len))
437 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
439 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
441 int ae = flags & MESH_FLAGS_AE;
442 /* 802.11-2012, 8.2.4.7.3 */
447 case MESH_FLAGS_AE_A4:
449 case MESH_FLAGS_AE_A5_A6:
454 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
456 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
458 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
460 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
461 const u8 *addr, enum nl80211_iftype iftype,
462 u8 data_offset, bool is_amsdu)
464 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
466 u8 hdr[ETH_ALEN] __aligned(2);
473 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
476 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
477 if (skb->len < hdrlen + 8)
480 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
482 * IEEE 802.11 address fields:
483 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
484 * 0 0 DA SA BSSID n/a
485 * 0 1 DA BSSID SA n/a
486 * 1 0 BSSID SA DA n/a
489 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
490 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
492 if (iftype == NL80211_IFTYPE_MESH_POINT)
493 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
495 mesh_flags &= MESH_FLAGS_AE;
497 switch (hdr->frame_control &
498 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
499 case cpu_to_le16(IEEE80211_FCTL_TODS):
500 if (unlikely(iftype != NL80211_IFTYPE_AP &&
501 iftype != NL80211_IFTYPE_AP_VLAN &&
502 iftype != NL80211_IFTYPE_P2P_GO))
505 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
506 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
507 iftype != NL80211_IFTYPE_MESH_POINT &&
508 iftype != NL80211_IFTYPE_AP_VLAN &&
509 iftype != NL80211_IFTYPE_STATION))
511 if (iftype == NL80211_IFTYPE_MESH_POINT) {
512 if (mesh_flags == MESH_FLAGS_AE_A4)
514 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
515 skb_copy_bits(skb, hdrlen +
516 offsetof(struct ieee80211s_hdr, eaddr1),
517 tmp.h_dest, 2 * ETH_ALEN);
519 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
522 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
523 if ((iftype != NL80211_IFTYPE_STATION &&
524 iftype != NL80211_IFTYPE_P2P_CLIENT &&
525 iftype != NL80211_IFTYPE_MESH_POINT) ||
526 (is_multicast_ether_addr(tmp.h_dest) &&
527 ether_addr_equal(tmp.h_source, addr)))
529 if (iftype == NL80211_IFTYPE_MESH_POINT) {
530 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
532 if (mesh_flags == MESH_FLAGS_AE_A4)
533 skb_copy_bits(skb, hdrlen +
534 offsetof(struct ieee80211s_hdr, eaddr1),
535 tmp.h_source, ETH_ALEN);
536 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
540 if (iftype != NL80211_IFTYPE_ADHOC &&
541 iftype != NL80211_IFTYPE_STATION &&
542 iftype != NL80211_IFTYPE_OCB)
547 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
548 tmp.h_proto = payload.proto;
550 if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
551 tmp.h_proto != htons(ETH_P_AARP) &&
552 tmp.h_proto != htons(ETH_P_IPX)) ||
553 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
554 /* remove RFC1042 or Bridge-Tunnel encapsulation and
555 * replace EtherType */
556 hdrlen += ETH_ALEN + 2;
558 tmp.h_proto = htons(skb->len - hdrlen);
560 pskb_pull(skb, hdrlen);
563 ehdr = skb_push(skb, sizeof(struct ethhdr));
564 memcpy(ehdr, &tmp, sizeof(tmp));
568 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
571 __frame_add_frag(struct sk_buff *skb, struct page *page,
572 void *ptr, int len, int size)
574 struct skb_shared_info *sh = skb_shinfo(skb);
578 page_offset = ptr - page_address(page);
579 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
583 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
586 struct skb_shared_info *sh = skb_shinfo(skb);
587 const skb_frag_t *frag = &sh->frags[0];
588 struct page *frag_page;
590 int frag_len, frag_size;
591 int head_size = skb->len - skb->data_len;
594 frag_page = virt_to_head_page(skb->head);
595 frag_ptr = skb->data;
596 frag_size = head_size;
598 while (offset >= frag_size) {
600 frag_page = skb_frag_page(frag);
601 frag_ptr = skb_frag_address(frag);
602 frag_size = skb_frag_size(frag);
607 frag_len = frag_size - offset;
609 cur_len = min(len, frag_len);
611 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
615 frag_len = skb_frag_size(frag);
616 cur_len = min(len, frag_len);
617 __frame_add_frag(frame, skb_frag_page(frag),
618 skb_frag_address(frag), cur_len, frag_len);
624 static struct sk_buff *
625 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
626 int offset, int len, bool reuse_frag)
628 struct sk_buff *frame;
631 if (skb->len - offset < len)
635 * When reusing framents, copy some data to the head to simplify
636 * ethernet header handling and speed up protocol header processing
637 * in the stack later.
640 cur_len = min_t(int, len, 32);
643 * Allocate and reserve two bytes more for payload
644 * alignment since sizeof(struct ethhdr) is 14.
646 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
650 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
651 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
658 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
663 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
664 const u8 *addr, enum nl80211_iftype iftype,
665 const unsigned int extra_headroom,
666 const u8 *check_da, const u8 *check_sa)
668 unsigned int hlen = ALIGN(extra_headroom, 4);
669 struct sk_buff *frame = NULL;
672 int offset = 0, remaining;
674 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
675 bool reuse_skb = false;
679 unsigned int subframe_len;
683 skb_copy_bits(skb, offset, ð, sizeof(eth));
684 len = ntohs(eth.h_proto);
685 subframe_len = sizeof(struct ethhdr) + len;
686 padding = (4 - subframe_len) & 0x3;
688 /* the last MSDU has no padding */
689 remaining = skb->len - offset;
690 if (subframe_len > remaining)
692 /* mitigate A-MSDU aggregation injection attacks */
693 if (ether_addr_equal(eth.h_dest, rfc1042_header))
696 offset += sizeof(struct ethhdr);
697 last = remaining <= subframe_len + padding;
699 /* FIXME: should we really accept multicast DA? */
700 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
701 !ether_addr_equal(check_da, eth.h_dest)) ||
702 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
703 offset += len + padding;
707 /* reuse skb for the last subframe */
708 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
709 skb_pull(skb, offset);
713 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
718 offset += len + padding;
721 skb_reset_network_header(frame);
722 frame->dev = skb->dev;
723 frame->priority = skb->priority;
725 payload = frame->data;
726 ethertype = (payload[6] << 8) | payload[7];
727 if (likely((ether_addr_equal(payload, rfc1042_header) &&
728 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
729 ether_addr_equal(payload, bridge_tunnel_header))) {
730 eth.h_proto = htons(ethertype);
731 skb_pull(frame, ETH_ALEN + 2);
734 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
735 __skb_queue_tail(list, frame);
744 __skb_queue_purge(list);
747 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
749 /* Given a data frame determine the 802.1p/1d tag to use. */
750 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
751 struct cfg80211_qos_map *qos_map)
754 unsigned char vlan_priority;
756 /* skb->priority values from 256->263 are magic values to
757 * directly indicate a specific 802.1d priority. This is used
758 * to allow 802.1d priority to be passed directly in from VLAN
761 if (skb->priority >= 256 && skb->priority <= 263)
762 return skb->priority - 256;
764 if (skb_vlan_tag_present(skb)) {
765 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
767 if (vlan_priority > 0)
768 return vlan_priority;
771 switch (skb->protocol) {
772 case htons(ETH_P_IP):
773 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
775 case htons(ETH_P_IPV6):
776 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
778 case htons(ETH_P_MPLS_UC):
779 case htons(ETH_P_MPLS_MC): {
780 struct mpls_label mpls_tmp, *mpls;
782 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
783 sizeof(*mpls), &mpls_tmp);
787 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
790 case htons(ETH_P_80221):
791 /* 802.21 is always network control traffic */
798 unsigned int i, tmp_dscp = dscp >> 2;
800 for (i = 0; i < qos_map->num_des; i++) {
801 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
802 return qos_map->dscp_exception[i].up;
805 for (i = 0; i < 8; i++) {
806 if (tmp_dscp >= qos_map->up[i].low &&
807 tmp_dscp <= qos_map->up[i].high)
814 EXPORT_SYMBOL(cfg80211_classify8021d);
816 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
818 const struct cfg80211_bss_ies *ies;
820 ies = rcu_dereference(bss->ies);
824 return cfg80211_find_ie(ie, ies->data, ies->len);
826 EXPORT_SYMBOL(ieee80211_bss_get_ie);
828 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
830 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
831 struct net_device *dev = wdev->netdev;
834 if (!wdev->connect_keys)
837 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
838 if (!wdev->connect_keys->params[i].cipher)
840 if (rdev_add_key(rdev, dev, i, false, NULL,
841 &wdev->connect_keys->params[i])) {
842 netdev_err(dev, "failed to set key %d\n", i);
845 if (wdev->connect_keys->def == i &&
846 rdev_set_default_key(rdev, dev, i, true, true)) {
847 netdev_err(dev, "failed to set defkey %d\n", i);
852 kzfree(wdev->connect_keys);
853 wdev->connect_keys = NULL;
856 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
858 struct cfg80211_event *ev;
861 spin_lock_irqsave(&wdev->event_lock, flags);
862 while (!list_empty(&wdev->event_list)) {
863 ev = list_first_entry(&wdev->event_list,
864 struct cfg80211_event, list);
866 spin_unlock_irqrestore(&wdev->event_lock, flags);
870 case EVENT_CONNECT_RESULT:
871 __cfg80211_connect_result(
874 ev->cr.status == WLAN_STATUS_SUCCESS);
877 __cfg80211_roamed(wdev, &ev->rm);
879 case EVENT_DISCONNECTED:
880 __cfg80211_disconnected(wdev->netdev,
881 ev->dc.ie, ev->dc.ie_len,
883 !ev->dc.locally_generated);
885 case EVENT_IBSS_JOINED:
886 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
890 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
892 case EVENT_PORT_AUTHORIZED:
893 __cfg80211_port_authorized(wdev, ev->pa.bssid);
900 spin_lock_irqsave(&wdev->event_lock, flags);
902 spin_unlock_irqrestore(&wdev->event_lock, flags);
905 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
907 struct wireless_dev *wdev;
911 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
912 cfg80211_process_wdev_events(wdev);
915 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
916 struct net_device *dev, enum nl80211_iftype ntype,
917 struct vif_params *params)
920 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
924 /* don't support changing VLANs, you just re-create them */
925 if (otype == NL80211_IFTYPE_AP_VLAN)
928 /* cannot change into P2P device or NAN */
929 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
930 ntype == NL80211_IFTYPE_NAN)
933 if (!rdev->ops->change_virtual_intf ||
934 !(rdev->wiphy.interface_modes & (1 << ntype)))
937 /* if it's part of a bridge, reject changing type to station/ibss */
938 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
939 (ntype == NL80211_IFTYPE_ADHOC ||
940 ntype == NL80211_IFTYPE_STATION ||
941 ntype == NL80211_IFTYPE_P2P_CLIENT))
944 if (ntype != otype) {
945 dev->ieee80211_ptr->use_4addr = false;
946 dev->ieee80211_ptr->mesh_id_up_len = 0;
947 wdev_lock(dev->ieee80211_ptr);
948 rdev_set_qos_map(rdev, dev, NULL);
949 wdev_unlock(dev->ieee80211_ptr);
952 case NL80211_IFTYPE_AP:
953 cfg80211_stop_ap(rdev, dev, true);
955 case NL80211_IFTYPE_ADHOC:
956 cfg80211_leave_ibss(rdev, dev, false);
958 case NL80211_IFTYPE_STATION:
959 case NL80211_IFTYPE_P2P_CLIENT:
960 wdev_lock(dev->ieee80211_ptr);
961 cfg80211_disconnect(rdev, dev,
962 WLAN_REASON_DEAUTH_LEAVING, true);
963 wdev_unlock(dev->ieee80211_ptr);
965 case NL80211_IFTYPE_MESH_POINT:
966 /* mesh should be handled? */
968 case NL80211_IFTYPE_OCB:
969 cfg80211_leave_ocb(rdev, dev);
975 cfg80211_process_rdev_events(rdev);
976 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
979 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
981 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
983 if (!err && params && params->use_4addr != -1)
984 dev->ieee80211_ptr->use_4addr = params->use_4addr;
987 dev->priv_flags &= ~IFF_DONT_BRIDGE;
989 case NL80211_IFTYPE_STATION:
990 if (dev->ieee80211_ptr->use_4addr)
993 case NL80211_IFTYPE_OCB:
994 case NL80211_IFTYPE_P2P_CLIENT:
995 case NL80211_IFTYPE_ADHOC:
996 dev->priv_flags |= IFF_DONT_BRIDGE;
998 case NL80211_IFTYPE_P2P_GO:
999 case NL80211_IFTYPE_AP:
1000 case NL80211_IFTYPE_AP_VLAN:
1001 case NL80211_IFTYPE_WDS:
1002 case NL80211_IFTYPE_MESH_POINT:
1005 case NL80211_IFTYPE_MONITOR:
1006 /* monitor can't bridge anyway */
1008 case NL80211_IFTYPE_UNSPECIFIED:
1009 case NUM_NL80211_IFTYPES:
1012 case NL80211_IFTYPE_P2P_DEVICE:
1013 case NL80211_IFTYPE_NAN:
1019 if (!err && ntype != otype && netif_running(dev)) {
1020 cfg80211_update_iface_num(rdev, ntype, 1);
1021 cfg80211_update_iface_num(rdev, otype, -1);
1027 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1029 int modulation, streams, bitrate;
1031 /* the formula below does only work for MCS values smaller than 32 */
1032 if (WARN_ON_ONCE(rate->mcs >= 32))
1035 modulation = rate->mcs & 7;
1036 streams = (rate->mcs >> 3) + 1;
1038 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1041 bitrate *= (modulation + 1);
1042 else if (modulation == 4)
1043 bitrate *= (modulation + 2);
1045 bitrate *= (modulation + 3);
1049 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1050 bitrate = (bitrate / 9) * 10;
1052 /* do NOT round down here */
1053 return (bitrate + 50000) / 100000;
1056 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1058 static const u32 __mcs2bitrate[] = {
1066 [5] = 12512, /* 1251.25 mbps */
1076 [14] = 8662, /* 866.25 mbps */
1086 [24] = 67568, /* 6756.75 mbps */
1097 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1100 return __mcs2bitrate[rate->mcs];
1103 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1105 static const u32 base[4][10] = {
1115 /* not in the spec, but some devices use this: */
1159 case RATE_INFO_BW_160:
1162 case RATE_INFO_BW_80:
1165 case RATE_INFO_BW_40:
1168 case RATE_INFO_BW_5:
1169 case RATE_INFO_BW_10:
1172 case RATE_INFO_BW_20:
1176 bitrate = base[idx][rate->mcs];
1177 bitrate *= rate->nss;
1179 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1180 bitrate = (bitrate / 9) * 10;
1182 /* do NOT round down here */
1183 return (bitrate + 50000) / 100000;
1185 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1186 rate->bw, rate->mcs, rate->nss);
1190 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1193 u16 mcs_divisors[12] = {
1194 34133, /* 16.666666... */
1195 17067, /* 8.333333... */
1196 11378, /* 5.555555... */
1197 8533, /* 4.166666... */
1198 5689, /* 2.777777... */
1199 4267, /* 2.083333... */
1200 3923, /* 1.851851... */
1201 3413, /* 1.666666... */
1202 2844, /* 1.388888... */
1203 2560, /* 1.250000... */
1204 2276, /* 1.111111... */
1205 2048, /* 1.000000... */
1207 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1208 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1209 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1210 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1211 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1212 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1213 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1217 if (WARN_ON_ONCE(rate->mcs > 11))
1220 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1222 if (WARN_ON_ONCE(rate->he_ru_alloc >
1223 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1225 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1228 if (rate->bw == RATE_INFO_BW_160)
1229 result = rates_160M[rate->he_gi];
1230 else if (rate->bw == RATE_INFO_BW_80 ||
1231 (rate->bw == RATE_INFO_BW_HE_RU &&
1232 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1233 result = rates_969[rate->he_gi];
1234 else if (rate->bw == RATE_INFO_BW_40 ||
1235 (rate->bw == RATE_INFO_BW_HE_RU &&
1236 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1237 result = rates_484[rate->he_gi];
1238 else if (rate->bw == RATE_INFO_BW_20 ||
1239 (rate->bw == RATE_INFO_BW_HE_RU &&
1240 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1241 result = rates_242[rate->he_gi];
1242 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1243 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1244 result = rates_106[rate->he_gi];
1245 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1246 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1247 result = rates_52[rate->he_gi];
1248 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1249 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1250 result = rates_26[rate->he_gi];
1251 else if (WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1252 rate->bw, rate->he_ru_alloc))
1255 /* now scale to the appropriate MCS */
1258 do_div(tmp, mcs_divisors[rate->mcs]);
1261 /* and take NSS, DCM into account */
1262 result = (result * rate->nss) / 8;
1266 return result / 10000;
1269 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1271 if (rate->flags & RATE_INFO_FLAGS_MCS)
1272 return cfg80211_calculate_bitrate_ht(rate);
1273 if (rate->flags & RATE_INFO_FLAGS_60G)
1274 return cfg80211_calculate_bitrate_60g(rate);
1275 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1276 return cfg80211_calculate_bitrate_vht(rate);
1277 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1278 return cfg80211_calculate_bitrate_he(rate);
1280 return rate->legacy;
1282 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1284 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1285 enum ieee80211_p2p_attr_id attr,
1286 u8 *buf, unsigned int bufsize)
1289 u16 attr_remaining = 0;
1290 bool desired_attr = false;
1291 u16 desired_len = 0;
1294 unsigned int iedatalen;
1301 if (iedatalen + 2 > len)
1304 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1312 /* check WFA OUI, P2P subtype */
1313 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1314 iedata[2] != 0x9a || iedata[3] != 0x09)
1320 /* check attribute continuation into this IE */
1321 copy = min_t(unsigned int, attr_remaining, iedatalen);
1322 if (copy && desired_attr) {
1323 desired_len += copy;
1325 memcpy(out, iedata, min(bufsize, copy));
1326 out += min(bufsize, copy);
1327 bufsize -= min(bufsize, copy);
1331 if (copy == attr_remaining)
1335 attr_remaining -= copy;
1342 while (iedatalen > 0) {
1345 /* P2P attribute ID & size must fit */
1348 desired_attr = iedata[0] == attr;
1349 attr_len = get_unaligned_le16(iedata + 1);
1353 copy = min_t(unsigned int, attr_len, iedatalen);
1356 desired_len += copy;
1358 memcpy(out, iedata, min(bufsize, copy));
1359 out += min(bufsize, copy);
1360 bufsize -= min(bufsize, copy);
1363 if (copy == attr_len)
1369 attr_remaining = attr_len - copy;
1377 if (attr_remaining && desired_attr)
1382 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1384 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1388 /* Make sure array values are legal */
1389 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1394 if (ids[i] == WLAN_EID_EXTENSION) {
1395 if (id_ext && (ids[i + 1] == id))
1402 if (ids[i] == id && !id_ext)
1410 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1412 /* we assume a validly formed IEs buffer */
1413 u8 len = ies[pos + 1];
1417 /* the IE itself must have 255 bytes for fragments to follow */
1421 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1429 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1430 const u8 *ids, int n_ids,
1431 const u8 *after_ric, int n_after_ric,
1434 size_t pos = offset;
1436 while (pos < ielen) {
1439 if (ies[pos] == WLAN_EID_EXTENSION)
1441 if ((pos + ext) >= ielen)
1444 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1445 ies[pos] == WLAN_EID_EXTENSION))
1448 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1449 pos = skip_ie(ies, ielen, pos);
1451 while (pos < ielen) {
1452 if (ies[pos] == WLAN_EID_EXTENSION)
1457 if ((pos + ext) >= ielen)
1460 if (!ieee80211_id_in_list(after_ric,
1464 pos = skip_ie(ies, ielen, pos);
1469 pos = skip_ie(ies, ielen, pos);
1475 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1477 bool ieee80211_operating_class_to_band(u8 operating_class,
1478 enum nl80211_band *band)
1480 switch (operating_class) {
1484 *band = NL80211_BAND_5GHZ;
1490 *band = NL80211_BAND_2GHZ;
1493 *band = NL80211_BAND_60GHZ;
1499 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1501 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1505 u32 freq = chandef->center_freq1;
1507 if (freq >= 2412 && freq <= 2472) {
1508 if (chandef->width > NL80211_CHAN_WIDTH_40)
1511 /* 2.407 GHz, channels 1..13 */
1512 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1513 if (freq > chandef->chan->center_freq)
1514 *op_class = 83; /* HT40+ */
1516 *op_class = 84; /* HT40- */
1525 if (chandef->width > NL80211_CHAN_WIDTH_40)
1528 *op_class = 82; /* channel 14 */
1532 switch (chandef->width) {
1533 case NL80211_CHAN_WIDTH_80:
1536 case NL80211_CHAN_WIDTH_160:
1539 case NL80211_CHAN_WIDTH_80P80:
1542 case NL80211_CHAN_WIDTH_10:
1543 case NL80211_CHAN_WIDTH_5:
1544 return false; /* unsupported for now */
1550 /* 5 GHz, channels 36..48 */
1551 if (freq >= 5180 && freq <= 5240) {
1553 *op_class = vht_opclass;
1554 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1555 if (freq > chandef->chan->center_freq)
1566 /* 5 GHz, channels 52..64 */
1567 if (freq >= 5260 && freq <= 5320) {
1569 *op_class = vht_opclass;
1570 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1571 if (freq > chandef->chan->center_freq)
1582 /* 5 GHz, channels 100..144 */
1583 if (freq >= 5500 && freq <= 5720) {
1585 *op_class = vht_opclass;
1586 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1587 if (freq > chandef->chan->center_freq)
1598 /* 5 GHz, channels 149..169 */
1599 if (freq >= 5745 && freq <= 5845) {
1601 *op_class = vht_opclass;
1602 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1603 if (freq > chandef->chan->center_freq)
1607 } else if (freq <= 5805) {
1616 /* 56.16 GHz, channel 1..4 */
1617 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1618 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1625 /* not supported yet */
1628 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1630 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1631 u32 *beacon_int_gcd,
1632 bool *beacon_int_different)
1634 struct wireless_dev *wdev;
1636 *beacon_int_gcd = 0;
1637 *beacon_int_different = false;
1639 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1640 if (!wdev->beacon_interval)
1643 if (!*beacon_int_gcd) {
1644 *beacon_int_gcd = wdev->beacon_interval;
1648 if (wdev->beacon_interval == *beacon_int_gcd)
1651 *beacon_int_different = true;
1652 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1655 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1656 if (*beacon_int_gcd)
1657 *beacon_int_different = true;
1658 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1662 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1663 enum nl80211_iftype iftype, u32 beacon_int)
1666 * This is just a basic pre-condition check; if interface combinations
1667 * are possible the driver must already be checking those with a call
1668 * to cfg80211_check_combinations(), in which case we'll validate more
1669 * through the cfg80211_calculate_bi_data() call and code in
1670 * cfg80211_iter_combinations().
1673 if (beacon_int < 10 || beacon_int > 10000)
1679 int cfg80211_iter_combinations(struct wiphy *wiphy,
1680 struct iface_combination_params *params,
1681 void (*iter)(const struct ieee80211_iface_combination *c,
1685 const struct ieee80211_regdomain *regdom;
1686 enum nl80211_dfs_regions region = 0;
1688 int num_interfaces = 0;
1689 u32 used_iftypes = 0;
1691 bool beacon_int_different;
1694 * This is a bit strange, since the iteration used to rely only on
1695 * the data given by the driver, but here it now relies on context,
1696 * in form of the currently operating interfaces.
1697 * This is OK for all current users, and saves us from having to
1698 * push the GCD calculations into all the drivers.
1699 * In the future, this should probably rely more on data that's in
1700 * cfg80211 already - the only thing not would appear to be any new
1701 * interfaces (while being brought up) and channel/radar data.
1703 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1704 &beacon_int_gcd, &beacon_int_different);
1706 if (params->radar_detect) {
1708 regdom = rcu_dereference(cfg80211_regdomain);
1710 region = regdom->dfs_region;
1714 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1715 num_interfaces += params->iftype_num[iftype];
1716 if (params->iftype_num[iftype] > 0 &&
1717 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1718 used_iftypes |= BIT(iftype);
1721 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1722 const struct ieee80211_iface_combination *c;
1723 struct ieee80211_iface_limit *limits;
1724 u32 all_iftypes = 0;
1726 c = &wiphy->iface_combinations[i];
1728 if (num_interfaces > c->max_interfaces)
1730 if (params->num_different_channels > c->num_different_channels)
1733 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1738 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1739 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1741 for (j = 0; j < c->n_limits; j++) {
1742 all_iftypes |= limits[j].types;
1743 if (!(limits[j].types & BIT(iftype)))
1745 if (limits[j].max < params->iftype_num[iftype])
1747 limits[j].max -= params->iftype_num[iftype];
1751 if (params->radar_detect !=
1752 (c->radar_detect_widths & params->radar_detect))
1755 if (params->radar_detect && c->radar_detect_regions &&
1756 !(c->radar_detect_regions & BIT(region)))
1759 /* Finally check that all iftypes that we're currently
1760 * using are actually part of this combination. If they
1761 * aren't then we can't use this combination and have
1762 * to continue to the next.
1764 if ((all_iftypes & used_iftypes) != used_iftypes)
1767 if (beacon_int_gcd) {
1768 if (c->beacon_int_min_gcd &&
1769 beacon_int_gcd < c->beacon_int_min_gcd)
1771 if (!c->beacon_int_min_gcd && beacon_int_different)
1775 /* This combination covered all interface types and
1776 * supported the requested numbers, so we're good.
1786 EXPORT_SYMBOL(cfg80211_iter_combinations);
1789 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1796 int cfg80211_check_combinations(struct wiphy *wiphy,
1797 struct iface_combination_params *params)
1801 err = cfg80211_iter_combinations(wiphy, params,
1802 cfg80211_iter_sum_ifcombs, &num);
1810 EXPORT_SYMBOL(cfg80211_check_combinations);
1812 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1813 const u8 *rates, unsigned int n_rates,
1821 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1826 for (i = 0; i < n_rates; i++) {
1827 int rate = (rates[i] & 0x7f) * 5;
1830 for (j = 0; j < sband->n_bitrates; j++) {
1831 if (sband->bitrates[j].bitrate == rate) {
1842 * mask must have at least one bit set here since we
1843 * didn't accept a 0-length rates array nor allowed
1844 * entries in the array that didn't exist
1850 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1852 enum nl80211_band band;
1853 unsigned int n_channels = 0;
1855 for (band = 0; band < NUM_NL80211_BANDS; band++)
1856 if (wiphy->bands[band])
1857 n_channels += wiphy->bands[band]->n_channels;
1861 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1863 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1864 struct station_info *sinfo)
1866 struct cfg80211_registered_device *rdev;
1867 struct wireless_dev *wdev;
1869 wdev = dev->ieee80211_ptr;
1873 rdev = wiphy_to_rdev(wdev->wiphy);
1874 if (!rdev->ops->get_station)
1877 memset(sinfo, 0, sizeof(*sinfo));
1879 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1881 EXPORT_SYMBOL(cfg80211_get_station);
1883 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1890 kfree(f->serv_spec_info);
1893 for (i = 0; i < f->num_rx_filters; i++)
1894 kfree(f->rx_filters[i].filter);
1896 for (i = 0; i < f->num_tx_filters; i++)
1897 kfree(f->tx_filters[i].filter);
1899 kfree(f->rx_filters);
1900 kfree(f->tx_filters);
1903 EXPORT_SYMBOL(cfg80211_free_nan_func);
1905 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1906 u32 center_freq_khz, u32 bw_khz)
1908 u32 start_freq_khz, end_freq_khz;
1910 start_freq_khz = center_freq_khz - (bw_khz / 2);
1911 end_freq_khz = center_freq_khz + (bw_khz / 2);
1913 if (start_freq_khz >= freq_range->start_freq_khz &&
1914 end_freq_khz <= freq_range->end_freq_khz)
1920 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1922 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1923 sizeof(*(sinfo->pertid)),
1930 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1932 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1933 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1934 const unsigned char rfc1042_header[] __aligned(2) =
1935 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1936 EXPORT_SYMBOL(rfc1042_header);
1938 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1939 const unsigned char bridge_tunnel_header[] __aligned(2) =
1940 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1941 EXPORT_SYMBOL(bridge_tunnel_header);
1943 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
1944 bool is_4addr, u8 check_swif)
1947 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
1949 switch (check_swif) {
1951 if (is_vlan && is_4addr)
1952 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
1953 return wiphy->interface_modes & BIT(iftype);
1955 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
1956 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
1957 return wiphy->software_iftypes & BIT(iftype);
1964 EXPORT_SYMBOL(cfg80211_iftype_allowed);
1966 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1967 struct iapp_layer2_update {
1968 u8 da[ETH_ALEN]; /* broadcast */
1969 u8 sa[ETH_ALEN]; /* STA addr */
1977 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1979 struct iapp_layer2_update *msg;
1980 struct sk_buff *skb;
1982 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1985 skb = dev_alloc_skb(sizeof(*msg));
1988 msg = skb_put(skb, sizeof(*msg));
1990 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1991 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1993 eth_broadcast_addr(msg->da);
1994 ether_addr_copy(msg->sa, addr);
1995 msg->len = htons(6);
1997 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1998 msg->control = 0xaf; /* XID response lsb.1111F101.
1999 * F=0 (no poll command; unsolicited frame) */
2000 msg->xid_info[0] = 0x81; /* XID format identifier */
2001 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2002 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2005 skb->protocol = eth_type_trans(skb, dev);
2006 memset(skb->cb, 0, sizeof(skb->cb));
2009 EXPORT_SYMBOL(cfg80211_send_layer2_update);