Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Wireless utility functions
0004  *
0005  * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
0006  * Copyright 2013-2014  Intel Mobile Communications GmbH
0007  * Copyright 2017   Intel Deutschland GmbH
0008  * Copyright (C) 2018-2022 Intel Corporation
0009  */
0010 #include <linux/export.h>
0011 #include <linux/bitops.h>
0012 #include <linux/etherdevice.h>
0013 #include <linux/slab.h>
0014 #include <linux/ieee80211.h>
0015 #include <net/cfg80211.h>
0016 #include <net/ip.h>
0017 #include <net/dsfield.h>
0018 #include <linux/if_vlan.h>
0019 #include <linux/mpls.h>
0020 #include <linux/gcd.h>
0021 #include <linux/bitfield.h>
0022 #include <linux/nospec.h>
0023 #include "core.h"
0024 #include "rdev-ops.h"
0025 
0026 
0027 const struct ieee80211_rate *
0028 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
0029                 u32 basic_rates, int bitrate)
0030 {
0031     struct ieee80211_rate *result = &sband->bitrates[0];
0032     int i;
0033 
0034     for (i = 0; i < sband->n_bitrates; i++) {
0035         if (!(basic_rates & BIT(i)))
0036             continue;
0037         if (sband->bitrates[i].bitrate > bitrate)
0038             continue;
0039         result = &sband->bitrates[i];
0040     }
0041 
0042     return result;
0043 }
0044 EXPORT_SYMBOL(ieee80211_get_response_rate);
0045 
0046 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
0047                   enum nl80211_bss_scan_width scan_width)
0048 {
0049     struct ieee80211_rate *bitrates;
0050     u32 mandatory_rates = 0;
0051     enum ieee80211_rate_flags mandatory_flag;
0052     int i;
0053 
0054     if (WARN_ON(!sband))
0055         return 1;
0056 
0057     if (sband->band == NL80211_BAND_2GHZ) {
0058         if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
0059             scan_width == NL80211_BSS_CHAN_WIDTH_10)
0060             mandatory_flag = IEEE80211_RATE_MANDATORY_G;
0061         else
0062             mandatory_flag = IEEE80211_RATE_MANDATORY_B;
0063     } else {
0064         mandatory_flag = IEEE80211_RATE_MANDATORY_A;
0065     }
0066 
0067     bitrates = sband->bitrates;
0068     for (i = 0; i < sband->n_bitrates; i++)
0069         if (bitrates[i].flags & mandatory_flag)
0070             mandatory_rates |= BIT(i);
0071     return mandatory_rates;
0072 }
0073 EXPORT_SYMBOL(ieee80211_mandatory_rates);
0074 
0075 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
0076 {
0077     /* see 802.11 17.3.8.3.2 and Annex J
0078      * there are overlapping channel numbers in 5GHz and 2GHz bands */
0079     if (chan <= 0)
0080         return 0; /* not supported */
0081     switch (band) {
0082     case NL80211_BAND_2GHZ:
0083     case NL80211_BAND_LC:
0084         if (chan == 14)
0085             return MHZ_TO_KHZ(2484);
0086         else if (chan < 14)
0087             return MHZ_TO_KHZ(2407 + chan * 5);
0088         break;
0089     case NL80211_BAND_5GHZ:
0090         if (chan >= 182 && chan <= 196)
0091             return MHZ_TO_KHZ(4000 + chan * 5);
0092         else
0093             return MHZ_TO_KHZ(5000 + chan * 5);
0094         break;
0095     case NL80211_BAND_6GHZ:
0096         /* see 802.11ax D6.1 27.3.23.2 */
0097         if (chan == 2)
0098             return MHZ_TO_KHZ(5935);
0099         if (chan <= 233)
0100             return MHZ_TO_KHZ(5950 + chan * 5);
0101         break;
0102     case NL80211_BAND_60GHZ:
0103         if (chan < 7)
0104             return MHZ_TO_KHZ(56160 + chan * 2160);
0105         break;
0106     case NL80211_BAND_S1GHZ:
0107         return 902000 + chan * 500;
0108     default:
0109         ;
0110     }
0111     return 0; /* not supported */
0112 }
0113 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
0114 
0115 enum nl80211_chan_width
0116 ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
0117 {
0118     if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
0119         return NL80211_CHAN_WIDTH_20_NOHT;
0120 
0121     /*S1G defines a single allowed channel width per channel.
0122      * Extract that width here.
0123      */
0124     if (chan->flags & IEEE80211_CHAN_1MHZ)
0125         return NL80211_CHAN_WIDTH_1;
0126     else if (chan->flags & IEEE80211_CHAN_2MHZ)
0127         return NL80211_CHAN_WIDTH_2;
0128     else if (chan->flags & IEEE80211_CHAN_4MHZ)
0129         return NL80211_CHAN_WIDTH_4;
0130     else if (chan->flags & IEEE80211_CHAN_8MHZ)
0131         return NL80211_CHAN_WIDTH_8;
0132     else if (chan->flags & IEEE80211_CHAN_16MHZ)
0133         return NL80211_CHAN_WIDTH_16;
0134 
0135     pr_err("unknown channel width for channel at %dKHz?\n",
0136            ieee80211_channel_to_khz(chan));
0137 
0138     return NL80211_CHAN_WIDTH_1;
0139 }
0140 EXPORT_SYMBOL(ieee80211_s1g_channel_width);
0141 
0142 int ieee80211_freq_khz_to_channel(u32 freq)
0143 {
0144     /* TODO: just handle MHz for now */
0145     freq = KHZ_TO_MHZ(freq);
0146 
0147     /* see 802.11 17.3.8.3.2 and Annex J */
0148     if (freq == 2484)
0149         return 14;
0150     else if (freq < 2484)
0151         return (freq - 2407) / 5;
0152     else if (freq >= 4910 && freq <= 4980)
0153         return (freq - 4000) / 5;
0154     else if (freq < 5925)
0155         return (freq - 5000) / 5;
0156     else if (freq == 5935)
0157         return 2;
0158     else if (freq <= 45000) /* DMG band lower limit */
0159         /* see 802.11ax D6.1 27.3.22.2 */
0160         return (freq - 5950) / 5;
0161     else if (freq >= 58320 && freq <= 70200)
0162         return (freq - 56160) / 2160;
0163     else
0164         return 0;
0165 }
0166 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
0167 
0168 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
0169                             u32 freq)
0170 {
0171     enum nl80211_band band;
0172     struct ieee80211_supported_band *sband;
0173     int i;
0174 
0175     for (band = 0; band < NUM_NL80211_BANDS; band++) {
0176         sband = wiphy->bands[band];
0177 
0178         if (!sband)
0179             continue;
0180 
0181         for (i = 0; i < sband->n_channels; i++) {
0182             struct ieee80211_channel *chan = &sband->channels[i];
0183 
0184             if (ieee80211_channel_to_khz(chan) == freq)
0185                 return chan;
0186         }
0187     }
0188 
0189     return NULL;
0190 }
0191 EXPORT_SYMBOL(ieee80211_get_channel_khz);
0192 
0193 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
0194 {
0195     int i, want;
0196 
0197     switch (sband->band) {
0198     case NL80211_BAND_5GHZ:
0199     case NL80211_BAND_6GHZ:
0200         want = 3;
0201         for (i = 0; i < sband->n_bitrates; i++) {
0202             if (sband->bitrates[i].bitrate == 60 ||
0203                 sband->bitrates[i].bitrate == 120 ||
0204                 sband->bitrates[i].bitrate == 240) {
0205                 sband->bitrates[i].flags |=
0206                     IEEE80211_RATE_MANDATORY_A;
0207                 want--;
0208             }
0209         }
0210         WARN_ON(want);
0211         break;
0212     case NL80211_BAND_2GHZ:
0213     case NL80211_BAND_LC:
0214         want = 7;
0215         for (i = 0; i < sband->n_bitrates; i++) {
0216             switch (sband->bitrates[i].bitrate) {
0217             case 10:
0218             case 20:
0219             case 55:
0220             case 110:
0221                 sband->bitrates[i].flags |=
0222                     IEEE80211_RATE_MANDATORY_B |
0223                     IEEE80211_RATE_MANDATORY_G;
0224                 want--;
0225                 break;
0226             case 60:
0227             case 120:
0228             case 240:
0229                 sband->bitrates[i].flags |=
0230                     IEEE80211_RATE_MANDATORY_G;
0231                 want--;
0232                 fallthrough;
0233             default:
0234                 sband->bitrates[i].flags |=
0235                     IEEE80211_RATE_ERP_G;
0236                 break;
0237             }
0238         }
0239         WARN_ON(want != 0 && want != 3);
0240         break;
0241     case NL80211_BAND_60GHZ:
0242         /* check for mandatory HT MCS 1..4 */
0243         WARN_ON(!sband->ht_cap.ht_supported);
0244         WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
0245         break;
0246     case NL80211_BAND_S1GHZ:
0247         /* Figure 9-589bd: 3 means unsupported, so != 3 means at least
0248          * mandatory is ok.
0249          */
0250         WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
0251         break;
0252     case NUM_NL80211_BANDS:
0253     default:
0254         WARN_ON(1);
0255         break;
0256     }
0257 }
0258 
0259 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
0260 {
0261     enum nl80211_band band;
0262 
0263     for (band = 0; band < NUM_NL80211_BANDS; band++)
0264         if (wiphy->bands[band])
0265             set_mandatory_flags_band(wiphy->bands[band]);
0266 }
0267 
0268 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
0269 {
0270     int i;
0271     for (i = 0; i < wiphy->n_cipher_suites; i++)
0272         if (cipher == wiphy->cipher_suites[i])
0273             return true;
0274     return false;
0275 }
0276 
0277 static bool
0278 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
0279 {
0280     struct wiphy *wiphy = &rdev->wiphy;
0281     int i;
0282 
0283     for (i = 0; i < wiphy->n_cipher_suites; i++) {
0284         switch (wiphy->cipher_suites[i]) {
0285         case WLAN_CIPHER_SUITE_AES_CMAC:
0286         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
0287         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
0288         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
0289             return true;
0290         }
0291     }
0292 
0293     return false;
0294 }
0295 
0296 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
0297                 int key_idx, bool pairwise)
0298 {
0299     int max_key_idx;
0300 
0301     if (pairwise)
0302         max_key_idx = 3;
0303     else if (wiphy_ext_feature_isset(&rdev->wiphy,
0304                      NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
0305          wiphy_ext_feature_isset(&rdev->wiphy,
0306                      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
0307         max_key_idx = 7;
0308     else if (cfg80211_igtk_cipher_supported(rdev))
0309         max_key_idx = 5;
0310     else
0311         max_key_idx = 3;
0312 
0313     if (key_idx < 0 || key_idx > max_key_idx)
0314         return false;
0315 
0316     return true;
0317 }
0318 
0319 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
0320                    struct key_params *params, int key_idx,
0321                    bool pairwise, const u8 *mac_addr)
0322 {
0323     if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
0324         return -EINVAL;
0325 
0326     if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
0327         return -EINVAL;
0328 
0329     if (pairwise && !mac_addr)
0330         return -EINVAL;
0331 
0332     switch (params->cipher) {
0333     case WLAN_CIPHER_SUITE_TKIP:
0334         /* Extended Key ID can only be used with CCMP/GCMP ciphers */
0335         if ((pairwise && key_idx) ||
0336             params->mode != NL80211_KEY_RX_TX)
0337             return -EINVAL;
0338         break;
0339     case WLAN_CIPHER_SUITE_CCMP:
0340     case WLAN_CIPHER_SUITE_CCMP_256:
0341     case WLAN_CIPHER_SUITE_GCMP:
0342     case WLAN_CIPHER_SUITE_GCMP_256:
0343         /* IEEE802.11-2016 allows only 0 and - when supporting
0344          * Extended Key ID - 1 as index for pairwise keys.
0345          * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
0346          * the driver supports Extended Key ID.
0347          * @NL80211_KEY_SET_TX can't be set when installing and
0348          * validating a key.
0349          */
0350         if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
0351             params->mode == NL80211_KEY_SET_TX)
0352             return -EINVAL;
0353         if (wiphy_ext_feature_isset(&rdev->wiphy,
0354                         NL80211_EXT_FEATURE_EXT_KEY_ID)) {
0355             if (pairwise && (key_idx < 0 || key_idx > 1))
0356                 return -EINVAL;
0357         } else if (pairwise && key_idx) {
0358             return -EINVAL;
0359         }
0360         break;
0361     case WLAN_CIPHER_SUITE_AES_CMAC:
0362     case WLAN_CIPHER_SUITE_BIP_CMAC_256:
0363     case WLAN_CIPHER_SUITE_BIP_GMAC_128:
0364     case WLAN_CIPHER_SUITE_BIP_GMAC_256:
0365         /* Disallow BIP (group-only) cipher as pairwise cipher */
0366         if (pairwise)
0367             return -EINVAL;
0368         if (key_idx < 4)
0369             return -EINVAL;
0370         break;
0371     case WLAN_CIPHER_SUITE_WEP40:
0372     case WLAN_CIPHER_SUITE_WEP104:
0373         if (key_idx > 3)
0374             return -EINVAL;
0375         break;
0376     default:
0377         break;
0378     }
0379 
0380     switch (params->cipher) {
0381     case WLAN_CIPHER_SUITE_WEP40:
0382         if (params->key_len != WLAN_KEY_LEN_WEP40)
0383             return -EINVAL;
0384         break;
0385     case WLAN_CIPHER_SUITE_TKIP:
0386         if (params->key_len != WLAN_KEY_LEN_TKIP)
0387             return -EINVAL;
0388         break;
0389     case WLAN_CIPHER_SUITE_CCMP:
0390         if (params->key_len != WLAN_KEY_LEN_CCMP)
0391             return -EINVAL;
0392         break;
0393     case WLAN_CIPHER_SUITE_CCMP_256:
0394         if (params->key_len != WLAN_KEY_LEN_CCMP_256)
0395             return -EINVAL;
0396         break;
0397     case WLAN_CIPHER_SUITE_GCMP:
0398         if (params->key_len != WLAN_KEY_LEN_GCMP)
0399             return -EINVAL;
0400         break;
0401     case WLAN_CIPHER_SUITE_GCMP_256:
0402         if (params->key_len != WLAN_KEY_LEN_GCMP_256)
0403             return -EINVAL;
0404         break;
0405     case WLAN_CIPHER_SUITE_WEP104:
0406         if (params->key_len != WLAN_KEY_LEN_WEP104)
0407             return -EINVAL;
0408         break;
0409     case WLAN_CIPHER_SUITE_AES_CMAC:
0410         if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
0411             return -EINVAL;
0412         break;
0413     case WLAN_CIPHER_SUITE_BIP_CMAC_256:
0414         if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
0415             return -EINVAL;
0416         break;
0417     case WLAN_CIPHER_SUITE_BIP_GMAC_128:
0418         if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
0419             return -EINVAL;
0420         break;
0421     case WLAN_CIPHER_SUITE_BIP_GMAC_256:
0422         if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
0423             return -EINVAL;
0424         break;
0425     default:
0426         /*
0427          * We don't know anything about this algorithm,
0428          * allow using it -- but the driver must check
0429          * all parameters! We still check below whether
0430          * or not the driver supports this algorithm,
0431          * of course.
0432          */
0433         break;
0434     }
0435 
0436     if (params->seq) {
0437         switch (params->cipher) {
0438         case WLAN_CIPHER_SUITE_WEP40:
0439         case WLAN_CIPHER_SUITE_WEP104:
0440             /* These ciphers do not use key sequence */
0441             return -EINVAL;
0442         case WLAN_CIPHER_SUITE_TKIP:
0443         case WLAN_CIPHER_SUITE_CCMP:
0444         case WLAN_CIPHER_SUITE_CCMP_256:
0445         case WLAN_CIPHER_SUITE_GCMP:
0446         case WLAN_CIPHER_SUITE_GCMP_256:
0447         case WLAN_CIPHER_SUITE_AES_CMAC:
0448         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
0449         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
0450         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
0451             if (params->seq_len != 6)
0452                 return -EINVAL;
0453             break;
0454         }
0455     }
0456 
0457     if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
0458         return -EINVAL;
0459 
0460     return 0;
0461 }
0462 
0463 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
0464 {
0465     unsigned int hdrlen = 24;
0466 
0467     if (ieee80211_is_ext(fc)) {
0468         hdrlen = 4;
0469         goto out;
0470     }
0471 
0472     if (ieee80211_is_data(fc)) {
0473         if (ieee80211_has_a4(fc))
0474             hdrlen = 30;
0475         if (ieee80211_is_data_qos(fc)) {
0476             hdrlen += IEEE80211_QOS_CTL_LEN;
0477             if (ieee80211_has_order(fc))
0478                 hdrlen += IEEE80211_HT_CTL_LEN;
0479         }
0480         goto out;
0481     }
0482 
0483     if (ieee80211_is_mgmt(fc)) {
0484         if (ieee80211_has_order(fc))
0485             hdrlen += IEEE80211_HT_CTL_LEN;
0486         goto out;
0487     }
0488 
0489     if (ieee80211_is_ctl(fc)) {
0490         /*
0491          * ACK and CTS are 10 bytes, all others 16. To see how
0492          * to get this condition consider
0493          *   subtype mask:   0b0000000011110000 (0x00F0)
0494          *   ACK subtype:    0b0000000011010000 (0x00D0)
0495          *   CTS subtype:    0b0000000011000000 (0x00C0)
0496          *   bits that matter:         ^^^      (0x00E0)
0497          *   value of those: 0b0000000011000000 (0x00C0)
0498          */
0499         if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
0500             hdrlen = 10;
0501         else
0502             hdrlen = 16;
0503     }
0504 out:
0505     return hdrlen;
0506 }
0507 EXPORT_SYMBOL(ieee80211_hdrlen);
0508 
0509 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
0510 {
0511     const struct ieee80211_hdr *hdr =
0512             (const struct ieee80211_hdr *)skb->data;
0513     unsigned int hdrlen;
0514 
0515     if (unlikely(skb->len < 10))
0516         return 0;
0517     hdrlen = ieee80211_hdrlen(hdr->frame_control);
0518     if (unlikely(hdrlen > skb->len))
0519         return 0;
0520     return hdrlen;
0521 }
0522 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
0523 
0524 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
0525 {
0526     int ae = flags & MESH_FLAGS_AE;
0527     /* 802.11-2012, 8.2.4.7.3 */
0528     switch (ae) {
0529     default:
0530     case 0:
0531         return 6;
0532     case MESH_FLAGS_AE_A4:
0533         return 12;
0534     case MESH_FLAGS_AE_A5_A6:
0535         return 18;
0536     }
0537 }
0538 
0539 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
0540 {
0541     return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
0542 }
0543 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
0544 
0545 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
0546                   const u8 *addr, enum nl80211_iftype iftype,
0547                   u8 data_offset, bool is_amsdu)
0548 {
0549     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
0550     struct {
0551         u8 hdr[ETH_ALEN] __aligned(2);
0552         __be16 proto;
0553     } payload;
0554     struct ethhdr tmp;
0555     u16 hdrlen;
0556     u8 mesh_flags = 0;
0557 
0558     if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
0559         return -1;
0560 
0561     hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
0562     if (skb->len < hdrlen + 8)
0563         return -1;
0564 
0565     /* convert IEEE 802.11 header + possible LLC headers into Ethernet
0566      * header
0567      * IEEE 802.11 address fields:
0568      * ToDS FromDS Addr1 Addr2 Addr3 Addr4
0569      *   0     0   DA    SA    BSSID n/a
0570      *   0     1   DA    BSSID SA    n/a
0571      *   1     0   BSSID SA    DA    n/a
0572      *   1     1   RA    TA    DA    SA
0573      */
0574     memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
0575     memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
0576 
0577     if (iftype == NL80211_IFTYPE_MESH_POINT)
0578         skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
0579 
0580     mesh_flags &= MESH_FLAGS_AE;
0581 
0582     switch (hdr->frame_control &
0583         cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
0584     case cpu_to_le16(IEEE80211_FCTL_TODS):
0585         if (unlikely(iftype != NL80211_IFTYPE_AP &&
0586                  iftype != NL80211_IFTYPE_AP_VLAN &&
0587                  iftype != NL80211_IFTYPE_P2P_GO))
0588             return -1;
0589         break;
0590     case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
0591         if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
0592                  iftype != NL80211_IFTYPE_AP_VLAN &&
0593                  iftype != NL80211_IFTYPE_STATION))
0594             return -1;
0595         if (iftype == NL80211_IFTYPE_MESH_POINT) {
0596             if (mesh_flags == MESH_FLAGS_AE_A4)
0597                 return -1;
0598             if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
0599                 skb_copy_bits(skb, hdrlen +
0600                     offsetof(struct ieee80211s_hdr, eaddr1),
0601                     tmp.h_dest, 2 * ETH_ALEN);
0602             }
0603             hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
0604         }
0605         break;
0606     case cpu_to_le16(IEEE80211_FCTL_FROMDS):
0607         if ((iftype != NL80211_IFTYPE_STATION &&
0608              iftype != NL80211_IFTYPE_P2P_CLIENT &&
0609              iftype != NL80211_IFTYPE_MESH_POINT) ||
0610             (is_multicast_ether_addr(tmp.h_dest) &&
0611              ether_addr_equal(tmp.h_source, addr)))
0612             return -1;
0613         if (iftype == NL80211_IFTYPE_MESH_POINT) {
0614             if (mesh_flags == MESH_FLAGS_AE_A5_A6)
0615                 return -1;
0616             if (mesh_flags == MESH_FLAGS_AE_A4)
0617                 skb_copy_bits(skb, hdrlen +
0618                     offsetof(struct ieee80211s_hdr, eaddr1),
0619                     tmp.h_source, ETH_ALEN);
0620             hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
0621         }
0622         break;
0623     case cpu_to_le16(0):
0624         if (iftype != NL80211_IFTYPE_ADHOC &&
0625             iftype != NL80211_IFTYPE_STATION &&
0626             iftype != NL80211_IFTYPE_OCB)
0627                 return -1;
0628         break;
0629     }
0630 
0631     skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
0632     tmp.h_proto = payload.proto;
0633 
0634     if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
0635             tmp.h_proto != htons(ETH_P_AARP) &&
0636             tmp.h_proto != htons(ETH_P_IPX)) ||
0637            ether_addr_equal(payload.hdr, bridge_tunnel_header))) {
0638         /* remove RFC1042 or Bridge-Tunnel encapsulation and
0639          * replace EtherType */
0640         hdrlen += ETH_ALEN + 2;
0641         skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
0642     } else {
0643         tmp.h_proto = htons(skb->len - hdrlen);
0644     }
0645 
0646     pskb_pull(skb, hdrlen);
0647 
0648     if (!ehdr)
0649         ehdr = skb_push(skb, sizeof(struct ethhdr));
0650     memcpy(ehdr, &tmp, sizeof(tmp));
0651 
0652     return 0;
0653 }
0654 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
0655 
0656 static void
0657 __frame_add_frag(struct sk_buff *skb, struct page *page,
0658          void *ptr, int len, int size)
0659 {
0660     struct skb_shared_info *sh = skb_shinfo(skb);
0661     int page_offset;
0662 
0663     get_page(page);
0664     page_offset = ptr - page_address(page);
0665     skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
0666 }
0667 
0668 static void
0669 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
0670                 int offset, int len)
0671 {
0672     struct skb_shared_info *sh = skb_shinfo(skb);
0673     const skb_frag_t *frag = &sh->frags[0];
0674     struct page *frag_page;
0675     void *frag_ptr;
0676     int frag_len, frag_size;
0677     int head_size = skb->len - skb->data_len;
0678     int cur_len;
0679 
0680     frag_page = virt_to_head_page(skb->head);
0681     frag_ptr = skb->data;
0682     frag_size = head_size;
0683 
0684     while (offset >= frag_size) {
0685         offset -= frag_size;
0686         frag_page = skb_frag_page(frag);
0687         frag_ptr = skb_frag_address(frag);
0688         frag_size = skb_frag_size(frag);
0689         frag++;
0690     }
0691 
0692     frag_ptr += offset;
0693     frag_len = frag_size - offset;
0694 
0695     cur_len = min(len, frag_len);
0696 
0697     __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
0698     len -= cur_len;
0699 
0700     while (len > 0) {
0701         frag_len = skb_frag_size(frag);
0702         cur_len = min(len, frag_len);
0703         __frame_add_frag(frame, skb_frag_page(frag),
0704                  skb_frag_address(frag), cur_len, frag_len);
0705         len -= cur_len;
0706         frag++;
0707     }
0708 }
0709 
0710 static struct sk_buff *
0711 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
0712                int offset, int len, bool reuse_frag)
0713 {
0714     struct sk_buff *frame;
0715     int cur_len = len;
0716 
0717     if (skb->len - offset < len)
0718         return NULL;
0719 
0720     /*
0721      * When reusing framents, copy some data to the head to simplify
0722      * ethernet header handling and speed up protocol header processing
0723      * in the stack later.
0724      */
0725     if (reuse_frag)
0726         cur_len = min_t(int, len, 32);
0727 
0728     /*
0729      * Allocate and reserve two bytes more for payload
0730      * alignment since sizeof(struct ethhdr) is 14.
0731      */
0732     frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
0733     if (!frame)
0734         return NULL;
0735 
0736     skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
0737     skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
0738 
0739     len -= cur_len;
0740     if (!len)
0741         return frame;
0742 
0743     offset += cur_len;
0744     __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
0745 
0746     return frame;
0747 }
0748 
0749 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
0750                   const u8 *addr, enum nl80211_iftype iftype,
0751                   const unsigned int extra_headroom,
0752                   const u8 *check_da, const u8 *check_sa)
0753 {
0754     unsigned int hlen = ALIGN(extra_headroom, 4);
0755     struct sk_buff *frame = NULL;
0756     u16 ethertype;
0757     u8 *payload;
0758     int offset = 0, remaining;
0759     struct ethhdr eth;
0760     bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
0761     bool reuse_skb = false;
0762     bool last = false;
0763 
0764     while (!last) {
0765         unsigned int subframe_len;
0766         int len;
0767         u8 padding;
0768 
0769         skb_copy_bits(skb, offset, &eth, sizeof(eth));
0770         len = ntohs(eth.h_proto);
0771         subframe_len = sizeof(struct ethhdr) + len;
0772         padding = (4 - subframe_len) & 0x3;
0773 
0774         /* the last MSDU has no padding */
0775         remaining = skb->len - offset;
0776         if (subframe_len > remaining)
0777             goto purge;
0778         /* mitigate A-MSDU aggregation injection attacks */
0779         if (ether_addr_equal(eth.h_dest, rfc1042_header))
0780             goto purge;
0781 
0782         offset += sizeof(struct ethhdr);
0783         last = remaining <= subframe_len + padding;
0784 
0785         /* FIXME: should we really accept multicast DA? */
0786         if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
0787              !ether_addr_equal(check_da, eth.h_dest)) ||
0788             (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
0789             offset += len + padding;
0790             continue;
0791         }
0792 
0793         /* reuse skb for the last subframe */
0794         if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
0795             skb_pull(skb, offset);
0796             frame = skb;
0797             reuse_skb = true;
0798         } else {
0799             frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
0800                                reuse_frag);
0801             if (!frame)
0802                 goto purge;
0803 
0804             offset += len + padding;
0805         }
0806 
0807         skb_reset_network_header(frame);
0808         frame->dev = skb->dev;
0809         frame->priority = skb->priority;
0810 
0811         payload = frame->data;
0812         ethertype = (payload[6] << 8) | payload[7];
0813         if (likely((ether_addr_equal(payload, rfc1042_header) &&
0814                 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
0815                ether_addr_equal(payload, bridge_tunnel_header))) {
0816             eth.h_proto = htons(ethertype);
0817             skb_pull(frame, ETH_ALEN + 2);
0818         }
0819 
0820         memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
0821         __skb_queue_tail(list, frame);
0822     }
0823 
0824     if (!reuse_skb)
0825         dev_kfree_skb(skb);
0826 
0827     return;
0828 
0829  purge:
0830     __skb_queue_purge(list);
0831     dev_kfree_skb(skb);
0832 }
0833 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
0834 
0835 /* Given a data frame determine the 802.1p/1d tag to use. */
0836 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
0837                     struct cfg80211_qos_map *qos_map)
0838 {
0839     unsigned int dscp;
0840     unsigned char vlan_priority;
0841     unsigned int ret;
0842 
0843     /* skb->priority values from 256->263 are magic values to
0844      * directly indicate a specific 802.1d priority.  This is used
0845      * to allow 802.1d priority to be passed directly in from VLAN
0846      * tags, etc.
0847      */
0848     if (skb->priority >= 256 && skb->priority <= 263) {
0849         ret = skb->priority - 256;
0850         goto out;
0851     }
0852 
0853     if (skb_vlan_tag_present(skb)) {
0854         vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
0855             >> VLAN_PRIO_SHIFT;
0856         if (vlan_priority > 0) {
0857             ret = vlan_priority;
0858             goto out;
0859         }
0860     }
0861 
0862     switch (skb->protocol) {
0863     case htons(ETH_P_IP):
0864         dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
0865         break;
0866     case htons(ETH_P_IPV6):
0867         dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
0868         break;
0869     case htons(ETH_P_MPLS_UC):
0870     case htons(ETH_P_MPLS_MC): {
0871         struct mpls_label mpls_tmp, *mpls;
0872 
0873         mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
0874                       sizeof(*mpls), &mpls_tmp);
0875         if (!mpls)
0876             return 0;
0877 
0878         ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
0879             >> MPLS_LS_TC_SHIFT;
0880         goto out;
0881     }
0882     case htons(ETH_P_80221):
0883         /* 802.21 is always network control traffic */
0884         return 7;
0885     default:
0886         return 0;
0887     }
0888 
0889     if (qos_map) {
0890         unsigned int i, tmp_dscp = dscp >> 2;
0891 
0892         for (i = 0; i < qos_map->num_des; i++) {
0893             if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
0894                 ret = qos_map->dscp_exception[i].up;
0895                 goto out;
0896             }
0897         }
0898 
0899         for (i = 0; i < 8; i++) {
0900             if (tmp_dscp >= qos_map->up[i].low &&
0901                 tmp_dscp <= qos_map->up[i].high) {
0902                 ret = i;
0903                 goto out;
0904             }
0905         }
0906     }
0907 
0908     ret = dscp >> 5;
0909 out:
0910     return array_index_nospec(ret, IEEE80211_NUM_TIDS);
0911 }
0912 EXPORT_SYMBOL(cfg80211_classify8021d);
0913 
0914 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
0915 {
0916     const struct cfg80211_bss_ies *ies;
0917 
0918     ies = rcu_dereference(bss->ies);
0919     if (!ies)
0920         return NULL;
0921 
0922     return cfg80211_find_elem(id, ies->data, ies->len);
0923 }
0924 EXPORT_SYMBOL(ieee80211_bss_get_elem);
0925 
0926 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
0927 {
0928     struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
0929     struct net_device *dev = wdev->netdev;
0930     int i;
0931 
0932     if (!wdev->connect_keys)
0933         return;
0934 
0935     for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
0936         if (!wdev->connect_keys->params[i].cipher)
0937             continue;
0938         if (rdev_add_key(rdev, dev, i, false, NULL,
0939                  &wdev->connect_keys->params[i])) {
0940             netdev_err(dev, "failed to set key %d\n", i);
0941             continue;
0942         }
0943         if (wdev->connect_keys->def == i &&
0944             rdev_set_default_key(rdev, dev, i, true, true)) {
0945             netdev_err(dev, "failed to set defkey %d\n", i);
0946             continue;
0947         }
0948     }
0949 
0950     kfree_sensitive(wdev->connect_keys);
0951     wdev->connect_keys = NULL;
0952 }
0953 
0954 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
0955 {
0956     struct cfg80211_event *ev;
0957     unsigned long flags;
0958 
0959     spin_lock_irqsave(&wdev->event_lock, flags);
0960     while (!list_empty(&wdev->event_list)) {
0961         ev = list_first_entry(&wdev->event_list,
0962                       struct cfg80211_event, list);
0963         list_del(&ev->list);
0964         spin_unlock_irqrestore(&wdev->event_lock, flags);
0965 
0966         wdev_lock(wdev);
0967         switch (ev->type) {
0968         case EVENT_CONNECT_RESULT:
0969             __cfg80211_connect_result(
0970                 wdev->netdev,
0971                 &ev->cr,
0972                 ev->cr.status == WLAN_STATUS_SUCCESS);
0973             break;
0974         case EVENT_ROAMED:
0975             __cfg80211_roamed(wdev, &ev->rm);
0976             break;
0977         case EVENT_DISCONNECTED:
0978             __cfg80211_disconnected(wdev->netdev,
0979                         ev->dc.ie, ev->dc.ie_len,
0980                         ev->dc.reason,
0981                         !ev->dc.locally_generated);
0982             break;
0983         case EVENT_IBSS_JOINED:
0984             __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
0985                            ev->ij.channel);
0986             break;
0987         case EVENT_STOPPED:
0988             __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
0989             break;
0990         case EVENT_PORT_AUTHORIZED:
0991             __cfg80211_port_authorized(wdev, ev->pa.bssid);
0992             break;
0993         }
0994         wdev_unlock(wdev);
0995 
0996         kfree(ev);
0997 
0998         spin_lock_irqsave(&wdev->event_lock, flags);
0999     }
1000     spin_unlock_irqrestore(&wdev->event_lock, flags);
1001 }
1002 
1003 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1004 {
1005     struct wireless_dev *wdev;
1006 
1007     lockdep_assert_held(&rdev->wiphy.mtx);
1008 
1009     list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1010         cfg80211_process_wdev_events(wdev);
1011 }
1012 
1013 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1014               struct net_device *dev, enum nl80211_iftype ntype,
1015               struct vif_params *params)
1016 {
1017     int err;
1018     enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1019 
1020     lockdep_assert_held(&rdev->wiphy.mtx);
1021 
1022     /* don't support changing VLANs, you just re-create them */
1023     if (otype == NL80211_IFTYPE_AP_VLAN)
1024         return -EOPNOTSUPP;
1025 
1026     /* cannot change into P2P device or NAN */
1027     if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1028         ntype == NL80211_IFTYPE_NAN)
1029         return -EOPNOTSUPP;
1030 
1031     if (!rdev->ops->change_virtual_intf ||
1032         !(rdev->wiphy.interface_modes & (1 << ntype)))
1033         return -EOPNOTSUPP;
1034 
1035     if (ntype != otype) {
1036         /* if it's part of a bridge, reject changing type to station/ibss */
1037         if (netif_is_bridge_port(dev) &&
1038             (ntype == NL80211_IFTYPE_ADHOC ||
1039              ntype == NL80211_IFTYPE_STATION ||
1040              ntype == NL80211_IFTYPE_P2P_CLIENT))
1041             return -EBUSY;
1042 
1043         dev->ieee80211_ptr->use_4addr = false;
1044         wdev_lock(dev->ieee80211_ptr);
1045         rdev_set_qos_map(rdev, dev, NULL);
1046         wdev_unlock(dev->ieee80211_ptr);
1047 
1048         switch (otype) {
1049         case NL80211_IFTYPE_AP:
1050         case NL80211_IFTYPE_P2P_GO:
1051             cfg80211_stop_ap(rdev, dev, -1, true);
1052             break;
1053         case NL80211_IFTYPE_ADHOC:
1054             cfg80211_leave_ibss(rdev, dev, false);
1055             break;
1056         case NL80211_IFTYPE_STATION:
1057         case NL80211_IFTYPE_P2P_CLIENT:
1058             wdev_lock(dev->ieee80211_ptr);
1059             cfg80211_disconnect(rdev, dev,
1060                         WLAN_REASON_DEAUTH_LEAVING, true);
1061             wdev_unlock(dev->ieee80211_ptr);
1062             break;
1063         case NL80211_IFTYPE_MESH_POINT:
1064             /* mesh should be handled? */
1065             break;
1066         case NL80211_IFTYPE_OCB:
1067             cfg80211_leave_ocb(rdev, dev);
1068             break;
1069         default:
1070             break;
1071         }
1072 
1073         cfg80211_process_rdev_events(rdev);
1074         cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1075 
1076         memset(&dev->ieee80211_ptr->u, 0,
1077                sizeof(dev->ieee80211_ptr->u));
1078         memset(&dev->ieee80211_ptr->links, 0,
1079                sizeof(dev->ieee80211_ptr->links));
1080     }
1081 
1082     err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1083 
1084     WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1085 
1086     if (!err && params && params->use_4addr != -1)
1087         dev->ieee80211_ptr->use_4addr = params->use_4addr;
1088 
1089     if (!err) {
1090         dev->priv_flags &= ~IFF_DONT_BRIDGE;
1091         switch (ntype) {
1092         case NL80211_IFTYPE_STATION:
1093             if (dev->ieee80211_ptr->use_4addr)
1094                 break;
1095             fallthrough;
1096         case NL80211_IFTYPE_OCB:
1097         case NL80211_IFTYPE_P2P_CLIENT:
1098         case NL80211_IFTYPE_ADHOC:
1099             dev->priv_flags |= IFF_DONT_BRIDGE;
1100             break;
1101         case NL80211_IFTYPE_P2P_GO:
1102         case NL80211_IFTYPE_AP:
1103         case NL80211_IFTYPE_AP_VLAN:
1104         case NL80211_IFTYPE_MESH_POINT:
1105             /* bridging OK */
1106             break;
1107         case NL80211_IFTYPE_MONITOR:
1108             /* monitor can't bridge anyway */
1109             break;
1110         case NL80211_IFTYPE_UNSPECIFIED:
1111         case NUM_NL80211_IFTYPES:
1112             /* not happening */
1113             break;
1114         case NL80211_IFTYPE_P2P_DEVICE:
1115         case NL80211_IFTYPE_WDS:
1116         case NL80211_IFTYPE_NAN:
1117             WARN_ON(1);
1118             break;
1119         }
1120     }
1121 
1122     if (!err && ntype != otype && netif_running(dev)) {
1123         cfg80211_update_iface_num(rdev, ntype, 1);
1124         cfg80211_update_iface_num(rdev, otype, -1);
1125     }
1126 
1127     return err;
1128 }
1129 
1130 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1131 {
1132     int modulation, streams, bitrate;
1133 
1134     /* the formula below does only work for MCS values smaller than 32 */
1135     if (WARN_ON_ONCE(rate->mcs >= 32))
1136         return 0;
1137 
1138     modulation = rate->mcs & 7;
1139     streams = (rate->mcs >> 3) + 1;
1140 
1141     bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1142 
1143     if (modulation < 4)
1144         bitrate *= (modulation + 1);
1145     else if (modulation == 4)
1146         bitrate *= (modulation + 2);
1147     else
1148         bitrate *= (modulation + 3);
1149 
1150     bitrate *= streams;
1151 
1152     if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1153         bitrate = (bitrate / 9) * 10;
1154 
1155     /* do NOT round down here */
1156     return (bitrate + 50000) / 100000;
1157 }
1158 
1159 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1160 {
1161     static const u32 __mcs2bitrate[] = {
1162         /* control PHY */
1163         [0] =   275,
1164         /* SC PHY */
1165         [1] =  3850,
1166         [2] =  7700,
1167         [3] =  9625,
1168         [4] = 11550,
1169         [5] = 12512, /* 1251.25 mbps */
1170         [6] = 15400,
1171         [7] = 19250,
1172         [8] = 23100,
1173         [9] = 25025,
1174         [10] = 30800,
1175         [11] = 38500,
1176         [12] = 46200,
1177         /* OFDM PHY */
1178         [13] =  6930,
1179         [14] =  8662, /* 866.25 mbps */
1180         [15] = 13860,
1181         [16] = 17325,
1182         [17] = 20790,
1183         [18] = 27720,
1184         [19] = 34650,
1185         [20] = 41580,
1186         [21] = 45045,
1187         [22] = 51975,
1188         [23] = 62370,
1189         [24] = 67568, /* 6756.75 mbps */
1190         /* LP-SC PHY */
1191         [25] =  6260,
1192         [26] =  8340,
1193         [27] = 11120,
1194         [28] = 12510,
1195         [29] = 16680,
1196         [30] = 22240,
1197         [31] = 25030,
1198     };
1199 
1200     if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1201         return 0;
1202 
1203     return __mcs2bitrate[rate->mcs];
1204 }
1205 
1206 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1207 {
1208     static const u32 __mcs2bitrate[] = {
1209         [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1210         [7 - 6] = 50050, /* MCS 12.1 */
1211         [8 - 6] = 53900,
1212         [9 - 6] = 57750,
1213         [10 - 6] = 63900,
1214         [11 - 6] = 75075,
1215         [12 - 6] = 80850,
1216     };
1217 
1218     /* Extended SC MCS not defined for base MCS below 6 or above 12 */
1219     if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1220         return 0;
1221 
1222     return __mcs2bitrate[rate->mcs - 6];
1223 }
1224 
1225 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1226 {
1227     static const u32 __mcs2bitrate[] = {
1228         /* control PHY */
1229         [0] =   275,
1230         /* SC PHY */
1231         [1] =  3850,
1232         [2] =  7700,
1233         [3] =  9625,
1234         [4] = 11550,
1235         [5] = 12512, /* 1251.25 mbps */
1236         [6] = 13475,
1237         [7] = 15400,
1238         [8] = 19250,
1239         [9] = 23100,
1240         [10] = 25025,
1241         [11] = 26950,
1242         [12] = 30800,
1243         [13] = 38500,
1244         [14] = 46200,
1245         [15] = 50050,
1246         [16] = 53900,
1247         [17] = 57750,
1248         [18] = 69300,
1249         [19] = 75075,
1250         [20] = 80850,
1251     };
1252 
1253     if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1254         return 0;
1255 
1256     return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1257 }
1258 
1259 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1260 {
1261     static const u32 base[4][12] = {
1262         {   6500000,
1263            13000000,
1264            19500000,
1265            26000000,
1266            39000000,
1267            52000000,
1268            58500000,
1269            65000000,
1270            78000000,
1271         /* not in the spec, but some devices use this: */
1272            86700000,
1273            97500000,
1274           108300000,
1275         },
1276         {  13500000,
1277            27000000,
1278            40500000,
1279            54000000,
1280            81000000,
1281           108000000,
1282           121500000,
1283           135000000,
1284           162000000,
1285           180000000,
1286           202500000,
1287           225000000,
1288         },
1289         {  29300000,
1290            58500000,
1291            87800000,
1292           117000000,
1293           175500000,
1294           234000000,
1295           263300000,
1296           292500000,
1297           351000000,
1298           390000000,
1299           438800000,
1300           487500000,
1301         },
1302         {  58500000,
1303           117000000,
1304           175500000,
1305           234000000,
1306           351000000,
1307           468000000,
1308           526500000,
1309           585000000,
1310           702000000,
1311           780000000,
1312           877500000,
1313           975000000,
1314         },
1315     };
1316     u32 bitrate;
1317     int idx;
1318 
1319     if (rate->mcs > 11)
1320         goto warn;
1321 
1322     switch (rate->bw) {
1323     case RATE_INFO_BW_160:
1324         idx = 3;
1325         break;
1326     case RATE_INFO_BW_80:
1327         idx = 2;
1328         break;
1329     case RATE_INFO_BW_40:
1330         idx = 1;
1331         break;
1332     case RATE_INFO_BW_5:
1333     case RATE_INFO_BW_10:
1334     default:
1335         goto warn;
1336     case RATE_INFO_BW_20:
1337         idx = 0;
1338     }
1339 
1340     bitrate = base[idx][rate->mcs];
1341     bitrate *= rate->nss;
1342 
1343     if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1344         bitrate = (bitrate / 9) * 10;
1345 
1346     /* do NOT round down here */
1347     return (bitrate + 50000) / 100000;
1348  warn:
1349     WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1350           rate->bw, rate->mcs, rate->nss);
1351     return 0;
1352 }
1353 
1354 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1355 {
1356 #define SCALE 6144
1357     u32 mcs_divisors[14] = {
1358         102399, /* 16.666666... */
1359          51201, /*  8.333333... */
1360          34134, /*  5.555555... */
1361          25599, /*  4.166666... */
1362          17067, /*  2.777777... */
1363          12801, /*  2.083333... */
1364          11377, /*  1.851725... */
1365          10239, /*  1.666666... */
1366           8532, /*  1.388888... */
1367           7680, /*  1.250000... */
1368           6828, /*  1.111111... */
1369           6144, /*  1.000000... */
1370           5690, /*  0.926106... */
1371           5120, /*  0.833333... */
1372     };
1373     u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1374     u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1375     u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1376     u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1377     u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1378     u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1379     u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1380     u64 tmp;
1381     u32 result;
1382 
1383     if (WARN_ON_ONCE(rate->mcs > 13))
1384         return 0;
1385 
1386     if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1387         return 0;
1388     if (WARN_ON_ONCE(rate->he_ru_alloc >
1389              NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1390         return 0;
1391     if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1392         return 0;
1393 
1394     if (rate->bw == RATE_INFO_BW_160)
1395         result = rates_160M[rate->he_gi];
1396     else if (rate->bw == RATE_INFO_BW_80 ||
1397          (rate->bw == RATE_INFO_BW_HE_RU &&
1398           rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1399         result = rates_969[rate->he_gi];
1400     else if (rate->bw == RATE_INFO_BW_40 ||
1401          (rate->bw == RATE_INFO_BW_HE_RU &&
1402           rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1403         result = rates_484[rate->he_gi];
1404     else if (rate->bw == RATE_INFO_BW_20 ||
1405          (rate->bw == RATE_INFO_BW_HE_RU &&
1406           rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1407         result = rates_242[rate->he_gi];
1408     else if (rate->bw == RATE_INFO_BW_HE_RU &&
1409          rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1410         result = rates_106[rate->he_gi];
1411     else if (rate->bw == RATE_INFO_BW_HE_RU &&
1412          rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1413         result = rates_52[rate->he_gi];
1414     else if (rate->bw == RATE_INFO_BW_HE_RU &&
1415          rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1416         result = rates_26[rate->he_gi];
1417     else {
1418         WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1419              rate->bw, rate->he_ru_alloc);
1420         return 0;
1421     }
1422 
1423     /* now scale to the appropriate MCS */
1424     tmp = result;
1425     tmp *= SCALE;
1426     do_div(tmp, mcs_divisors[rate->mcs]);
1427     result = tmp;
1428 
1429     /* and take NSS, DCM into account */
1430     result = (result * rate->nss) / 8;
1431     if (rate->he_dcm)
1432         result /= 2;
1433 
1434     return result / 10000;
1435 }
1436 
1437 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1438 {
1439 #define SCALE 6144
1440     static const u32 mcs_divisors[16] = {
1441         102399, /* 16.666666... */
1442          51201, /*  8.333333... */
1443          34134, /*  5.555555... */
1444          25599, /*  4.166666... */
1445          17067, /*  2.777777... */
1446          12801, /*  2.083333... */
1447          11377, /*  1.851725... */
1448          10239, /*  1.666666... */
1449           8532, /*  1.388888... */
1450           7680, /*  1.250000... */
1451           6828, /*  1.111111... */
1452           6144, /*  1.000000... */
1453           5690, /*  0.926106... */
1454           5120, /*  0.833333... */
1455         409600, /* 66.666666... */
1456         204800, /* 33.333333... */
1457     };
1458     static const u32 rates_996[3] =  { 480388888, 453700000, 408333333 };
1459     static const u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1460     static const u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1461     static const u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1462     static const u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1463     static const u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1464     u64 tmp;
1465     u32 result;
1466 
1467     if (WARN_ON_ONCE(rate->mcs > 15))
1468         return 0;
1469     if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1470         return 0;
1471     if (WARN_ON_ONCE(rate->eht_ru_alloc >
1472              NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1473         return 0;
1474     if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1475         return 0;
1476 
1477     /* Bandwidth checks for MCS 14 */
1478     if (rate->mcs == 14) {
1479         if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1480              rate->bw != RATE_INFO_BW_80 &&
1481              rate->bw != RATE_INFO_BW_160 &&
1482              rate->bw != RATE_INFO_BW_320) ||
1483             (rate->bw == RATE_INFO_BW_EHT_RU &&
1484              rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1485              rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1486              rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1487             WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1488                  rate->bw, rate->eht_ru_alloc);
1489             return 0;
1490         }
1491     }
1492 
1493     if (rate->bw == RATE_INFO_BW_320 ||
1494         (rate->bw == RATE_INFO_BW_EHT_RU &&
1495          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1496         result = 4 * rates_996[rate->eht_gi];
1497     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1498          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1499         result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1500     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1501          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1502         result = 3 * rates_996[rate->eht_gi];
1503     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1504          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1505         result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1506     else if (rate->bw == RATE_INFO_BW_160 ||
1507          (rate->bw == RATE_INFO_BW_EHT_RU &&
1508           rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1509         result = 2 * rates_996[rate->eht_gi];
1510     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1511          rate->eht_ru_alloc ==
1512          NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1513         result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1514              + rates_242[rate->eht_gi];
1515     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1516          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1517         result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1518     else if (rate->bw == RATE_INFO_BW_80 ||
1519          (rate->bw == RATE_INFO_BW_EHT_RU &&
1520           rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1521         result = rates_996[rate->eht_gi];
1522     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1523          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1524         result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1525     else if (rate->bw == RATE_INFO_BW_40 ||
1526          (rate->bw == RATE_INFO_BW_EHT_RU &&
1527           rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1528         result = rates_484[rate->eht_gi];
1529     else if (rate->bw == RATE_INFO_BW_20 ||
1530          (rate->bw == RATE_INFO_BW_EHT_RU &&
1531           rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1532         result = rates_242[rate->eht_gi];
1533     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1534          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1535         result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1536     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1537          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1538         result = rates_106[rate->eht_gi];
1539     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1540          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1541         result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1542     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1543          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1544         result = rates_52[rate->eht_gi];
1545     else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1546          rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1547         result = rates_26[rate->eht_gi];
1548     else {
1549         WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1550              rate->bw, rate->eht_ru_alloc);
1551         return 0;
1552     }
1553 
1554     /* now scale to the appropriate MCS */
1555     tmp = result;
1556     tmp *= SCALE;
1557     do_div(tmp, mcs_divisors[rate->mcs]);
1558     result = tmp;
1559 
1560     /* and take NSS */
1561     result = (result * rate->nss) / 8;
1562 
1563     return result / 10000;
1564 }
1565 
1566 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1567 {
1568     if (rate->flags & RATE_INFO_FLAGS_MCS)
1569         return cfg80211_calculate_bitrate_ht(rate);
1570     if (rate->flags & RATE_INFO_FLAGS_DMG)
1571         return cfg80211_calculate_bitrate_dmg(rate);
1572     if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1573         return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1574     if (rate->flags & RATE_INFO_FLAGS_EDMG)
1575         return cfg80211_calculate_bitrate_edmg(rate);
1576     if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1577         return cfg80211_calculate_bitrate_vht(rate);
1578     if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1579         return cfg80211_calculate_bitrate_he(rate);
1580     if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1581         return cfg80211_calculate_bitrate_eht(rate);
1582 
1583     return rate->legacy;
1584 }
1585 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1586 
1587 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1588               enum ieee80211_p2p_attr_id attr,
1589               u8 *buf, unsigned int bufsize)
1590 {
1591     u8 *out = buf;
1592     u16 attr_remaining = 0;
1593     bool desired_attr = false;
1594     u16 desired_len = 0;
1595 
1596     while (len > 0) {
1597         unsigned int iedatalen;
1598         unsigned int copy;
1599         const u8 *iedata;
1600 
1601         if (len < 2)
1602             return -EILSEQ;
1603         iedatalen = ies[1];
1604         if (iedatalen + 2 > len)
1605             return -EILSEQ;
1606 
1607         if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1608             goto cont;
1609 
1610         if (iedatalen < 4)
1611             goto cont;
1612 
1613         iedata = ies + 2;
1614 
1615         /* check WFA OUI, P2P subtype */
1616         if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1617             iedata[2] != 0x9a || iedata[3] != 0x09)
1618             goto cont;
1619 
1620         iedatalen -= 4;
1621         iedata += 4;
1622 
1623         /* check attribute continuation into this IE */
1624         copy = min_t(unsigned int, attr_remaining, iedatalen);
1625         if (copy && desired_attr) {
1626             desired_len += copy;
1627             if (out) {
1628                 memcpy(out, iedata, min(bufsize, copy));
1629                 out += min(bufsize, copy);
1630                 bufsize -= min(bufsize, copy);
1631             }
1632 
1633 
1634             if (copy == attr_remaining)
1635                 return desired_len;
1636         }
1637 
1638         attr_remaining -= copy;
1639         if (attr_remaining)
1640             goto cont;
1641 
1642         iedatalen -= copy;
1643         iedata += copy;
1644 
1645         while (iedatalen > 0) {
1646             u16 attr_len;
1647 
1648             /* P2P attribute ID & size must fit */
1649             if (iedatalen < 3)
1650                 return -EILSEQ;
1651             desired_attr = iedata[0] == attr;
1652             attr_len = get_unaligned_le16(iedata + 1);
1653             iedatalen -= 3;
1654             iedata += 3;
1655 
1656             copy = min_t(unsigned int, attr_len, iedatalen);
1657 
1658             if (desired_attr) {
1659                 desired_len += copy;
1660                 if (out) {
1661                     memcpy(out, iedata, min(bufsize, copy));
1662                     out += min(bufsize, copy);
1663                     bufsize -= min(bufsize, copy);
1664                 }
1665 
1666                 if (copy == attr_len)
1667                     return desired_len;
1668             }
1669 
1670             iedata += copy;
1671             iedatalen -= copy;
1672             attr_remaining = attr_len - copy;
1673         }
1674 
1675  cont:
1676         len -= ies[1] + 2;
1677         ies += ies[1] + 2;
1678     }
1679 
1680     if (attr_remaining && desired_attr)
1681         return -EILSEQ;
1682 
1683     return -ENOENT;
1684 }
1685 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1686 
1687 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1688 {
1689     int i;
1690 
1691     /* Make sure array values are legal */
1692     if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1693         return false;
1694 
1695     i = 0;
1696     while (i < n_ids) {
1697         if (ids[i] == WLAN_EID_EXTENSION) {
1698             if (id_ext && (ids[i + 1] == id))
1699                 return true;
1700 
1701             i += 2;
1702             continue;
1703         }
1704 
1705         if (ids[i] == id && !id_ext)
1706             return true;
1707 
1708         i++;
1709     }
1710     return false;
1711 }
1712 
1713 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1714 {
1715     /* we assume a validly formed IEs buffer */
1716     u8 len = ies[pos + 1];
1717 
1718     pos += 2 + len;
1719 
1720     /* the IE itself must have 255 bytes for fragments to follow */
1721     if (len < 255)
1722         return pos;
1723 
1724     while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1725         len = ies[pos + 1];
1726         pos += 2 + len;
1727     }
1728 
1729     return pos;
1730 }
1731 
1732 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1733                   const u8 *ids, int n_ids,
1734                   const u8 *after_ric, int n_after_ric,
1735                   size_t offset)
1736 {
1737     size_t pos = offset;
1738 
1739     while (pos < ielen) {
1740         u8 ext = 0;
1741 
1742         if (ies[pos] == WLAN_EID_EXTENSION)
1743             ext = 2;
1744         if ((pos + ext) >= ielen)
1745             break;
1746 
1747         if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1748                       ies[pos] == WLAN_EID_EXTENSION))
1749             break;
1750 
1751         if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1752             pos = skip_ie(ies, ielen, pos);
1753 
1754             while (pos < ielen) {
1755                 if (ies[pos] == WLAN_EID_EXTENSION)
1756                     ext = 2;
1757                 else
1758                     ext = 0;
1759 
1760                 if ((pos + ext) >= ielen)
1761                     break;
1762 
1763                 if (!ieee80211_id_in_list(after_ric,
1764                               n_after_ric,
1765                               ies[pos + ext],
1766                               ext == 2))
1767                     pos = skip_ie(ies, ielen, pos);
1768                 else
1769                     break;
1770             }
1771         } else {
1772             pos = skip_ie(ies, ielen, pos);
1773         }
1774     }
1775 
1776     return pos;
1777 }
1778 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1779 
1780 bool ieee80211_operating_class_to_band(u8 operating_class,
1781                        enum nl80211_band *band)
1782 {
1783     switch (operating_class) {
1784     case 112:
1785     case 115 ... 127:
1786     case 128 ... 130:
1787         *band = NL80211_BAND_5GHZ;
1788         return true;
1789     case 131 ... 135:
1790         *band = NL80211_BAND_6GHZ;
1791         return true;
1792     case 81:
1793     case 82:
1794     case 83:
1795     case 84:
1796         *band = NL80211_BAND_2GHZ;
1797         return true;
1798     case 180:
1799         *band = NL80211_BAND_60GHZ;
1800         return true;
1801     }
1802 
1803     return false;
1804 }
1805 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1806 
1807 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1808                       u8 *op_class)
1809 {
1810     u8 vht_opclass;
1811     u32 freq = chandef->center_freq1;
1812 
1813     if (freq >= 2412 && freq <= 2472) {
1814         if (chandef->width > NL80211_CHAN_WIDTH_40)
1815             return false;
1816 
1817         /* 2.407 GHz, channels 1..13 */
1818         if (chandef->width == NL80211_CHAN_WIDTH_40) {
1819             if (freq > chandef->chan->center_freq)
1820                 *op_class = 83; /* HT40+ */
1821             else
1822                 *op_class = 84; /* HT40- */
1823         } else {
1824             *op_class = 81;
1825         }
1826 
1827         return true;
1828     }
1829 
1830     if (freq == 2484) {
1831         /* channel 14 is only for IEEE 802.11b */
1832         if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
1833             return false;
1834 
1835         *op_class = 82; /* channel 14 */
1836         return true;
1837     }
1838 
1839     switch (chandef->width) {
1840     case NL80211_CHAN_WIDTH_80:
1841         vht_opclass = 128;
1842         break;
1843     case NL80211_CHAN_WIDTH_160:
1844         vht_opclass = 129;
1845         break;
1846     case NL80211_CHAN_WIDTH_80P80:
1847         vht_opclass = 130;
1848         break;
1849     case NL80211_CHAN_WIDTH_10:
1850     case NL80211_CHAN_WIDTH_5:
1851         return false; /* unsupported for now */
1852     default:
1853         vht_opclass = 0;
1854         break;
1855     }
1856 
1857     /* 5 GHz, channels 36..48 */
1858     if (freq >= 5180 && freq <= 5240) {
1859         if (vht_opclass) {
1860             *op_class = vht_opclass;
1861         } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1862             if (freq > chandef->chan->center_freq)
1863                 *op_class = 116;
1864             else
1865                 *op_class = 117;
1866         } else {
1867             *op_class = 115;
1868         }
1869 
1870         return true;
1871     }
1872 
1873     /* 5 GHz, channels 52..64 */
1874     if (freq >= 5260 && freq <= 5320) {
1875         if (vht_opclass) {
1876             *op_class = vht_opclass;
1877         } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1878             if (freq > chandef->chan->center_freq)
1879                 *op_class = 119;
1880             else
1881                 *op_class = 120;
1882         } else {
1883             *op_class = 118;
1884         }
1885 
1886         return true;
1887     }
1888 
1889     /* 5 GHz, channels 100..144 */
1890     if (freq >= 5500 && freq <= 5720) {
1891         if (vht_opclass) {
1892             *op_class = vht_opclass;
1893         } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1894             if (freq > chandef->chan->center_freq)
1895                 *op_class = 122;
1896             else
1897                 *op_class = 123;
1898         } else {
1899             *op_class = 121;
1900         }
1901 
1902         return true;
1903     }
1904 
1905     /* 5 GHz, channels 149..169 */
1906     if (freq >= 5745 && freq <= 5845) {
1907         if (vht_opclass) {
1908             *op_class = vht_opclass;
1909         } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1910             if (freq > chandef->chan->center_freq)
1911                 *op_class = 126;
1912             else
1913                 *op_class = 127;
1914         } else if (freq <= 5805) {
1915             *op_class = 124;
1916         } else {
1917             *op_class = 125;
1918         }
1919 
1920         return true;
1921     }
1922 
1923     /* 56.16 GHz, channel 1..4 */
1924     if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1925         if (chandef->width >= NL80211_CHAN_WIDTH_40)
1926             return false;
1927 
1928         *op_class = 180;
1929         return true;
1930     }
1931 
1932     /* not supported yet */
1933     return false;
1934 }
1935 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1936 
1937 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
1938 {
1939     switch (wdev->iftype) {
1940     case NL80211_IFTYPE_AP:
1941     case NL80211_IFTYPE_P2P_GO:
1942         WARN_ON(wdev->valid_links);
1943         return wdev->links[0].ap.beacon_interval;
1944     case NL80211_IFTYPE_MESH_POINT:
1945         return wdev->u.mesh.beacon_interval;
1946     case NL80211_IFTYPE_ADHOC:
1947         return wdev->u.ibss.beacon_interval;
1948     default:
1949         break;
1950     }
1951 
1952     return 0;
1953 }
1954 
1955 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1956                        u32 *beacon_int_gcd,
1957                        bool *beacon_int_different)
1958 {
1959     struct wireless_dev *wdev;
1960 
1961     *beacon_int_gcd = 0;
1962     *beacon_int_different = false;
1963 
1964     list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1965         int wdev_bi;
1966 
1967         /* this feature isn't supported with MLO */
1968         if (wdev->valid_links)
1969             continue;
1970 
1971         wdev_bi = cfg80211_wdev_bi(wdev);
1972 
1973         if (!wdev_bi)
1974             continue;
1975 
1976         if (!*beacon_int_gcd) {
1977             *beacon_int_gcd = wdev_bi;
1978             continue;
1979         }
1980 
1981         if (wdev_bi == *beacon_int_gcd)
1982             continue;
1983 
1984         *beacon_int_different = true;
1985         *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
1986     }
1987 
1988     if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1989         if (*beacon_int_gcd)
1990             *beacon_int_different = true;
1991         *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1992     }
1993 }
1994 
1995 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1996                  enum nl80211_iftype iftype, u32 beacon_int)
1997 {
1998     /*
1999      * This is just a basic pre-condition check; if interface combinations
2000      * are possible the driver must already be checking those with a call
2001      * to cfg80211_check_combinations(), in which case we'll validate more
2002      * through the cfg80211_calculate_bi_data() call and code in
2003      * cfg80211_iter_combinations().
2004      */
2005 
2006     if (beacon_int < 10 || beacon_int > 10000)
2007         return -EINVAL;
2008 
2009     return 0;
2010 }
2011 
2012 int cfg80211_iter_combinations(struct wiphy *wiphy,
2013                    struct iface_combination_params *params,
2014                    void (*iter)(const struct ieee80211_iface_combination *c,
2015                         void *data),
2016                    void *data)
2017 {
2018     const struct ieee80211_regdomain *regdom;
2019     enum nl80211_dfs_regions region = 0;
2020     int i, j, iftype;
2021     int num_interfaces = 0;
2022     u32 used_iftypes = 0;
2023     u32 beacon_int_gcd;
2024     bool beacon_int_different;
2025 
2026     /*
2027      * This is a bit strange, since the iteration used to rely only on
2028      * the data given by the driver, but here it now relies on context,
2029      * in form of the currently operating interfaces.
2030      * This is OK for all current users, and saves us from having to
2031      * push the GCD calculations into all the drivers.
2032      * In the future, this should probably rely more on data that's in
2033      * cfg80211 already - the only thing not would appear to be any new
2034      * interfaces (while being brought up) and channel/radar data.
2035      */
2036     cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2037                    &beacon_int_gcd, &beacon_int_different);
2038 
2039     if (params->radar_detect) {
2040         rcu_read_lock();
2041         regdom = rcu_dereference(cfg80211_regdomain);
2042         if (regdom)
2043             region = regdom->dfs_region;
2044         rcu_read_unlock();
2045     }
2046 
2047     for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2048         num_interfaces += params->iftype_num[iftype];
2049         if (params->iftype_num[iftype] > 0 &&
2050             !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2051             used_iftypes |= BIT(iftype);
2052     }
2053 
2054     for (i = 0; i < wiphy->n_iface_combinations; i++) {
2055         const struct ieee80211_iface_combination *c;
2056         struct ieee80211_iface_limit *limits;
2057         u32 all_iftypes = 0;
2058 
2059         c = &wiphy->iface_combinations[i];
2060 
2061         if (num_interfaces > c->max_interfaces)
2062             continue;
2063         if (params->num_different_channels > c->num_different_channels)
2064             continue;
2065 
2066         limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2067                  GFP_KERNEL);
2068         if (!limits)
2069             return -ENOMEM;
2070 
2071         for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2072             if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2073                 continue;
2074             for (j = 0; j < c->n_limits; j++) {
2075                 all_iftypes |= limits[j].types;
2076                 if (!(limits[j].types & BIT(iftype)))
2077                     continue;
2078                 if (limits[j].max < params->iftype_num[iftype])
2079                     goto cont;
2080                 limits[j].max -= params->iftype_num[iftype];
2081             }
2082         }
2083 
2084         if (params->radar_detect !=
2085             (c->radar_detect_widths & params->radar_detect))
2086             goto cont;
2087 
2088         if (params->radar_detect && c->radar_detect_regions &&
2089             !(c->radar_detect_regions & BIT(region)))
2090             goto cont;
2091 
2092         /* Finally check that all iftypes that we're currently
2093          * using are actually part of this combination. If they
2094          * aren't then we can't use this combination and have
2095          * to continue to the next.
2096          */
2097         if ((all_iftypes & used_iftypes) != used_iftypes)
2098             goto cont;
2099 
2100         if (beacon_int_gcd) {
2101             if (c->beacon_int_min_gcd &&
2102                 beacon_int_gcd < c->beacon_int_min_gcd)
2103                 goto cont;
2104             if (!c->beacon_int_min_gcd && beacon_int_different)
2105                 goto cont;
2106         }
2107 
2108         /* This combination covered all interface types and
2109          * supported the requested numbers, so we're good.
2110          */
2111 
2112         (*iter)(c, data);
2113  cont:
2114         kfree(limits);
2115     }
2116 
2117     return 0;
2118 }
2119 EXPORT_SYMBOL(cfg80211_iter_combinations);
2120 
2121 static void
2122 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2123               void *data)
2124 {
2125     int *num = data;
2126     (*num)++;
2127 }
2128 
2129 int cfg80211_check_combinations(struct wiphy *wiphy,
2130                 struct iface_combination_params *params)
2131 {
2132     int err, num = 0;
2133 
2134     err = cfg80211_iter_combinations(wiphy, params,
2135                      cfg80211_iter_sum_ifcombs, &num);
2136     if (err)
2137         return err;
2138     if (num == 0)
2139         return -EBUSY;
2140 
2141     return 0;
2142 }
2143 EXPORT_SYMBOL(cfg80211_check_combinations);
2144 
2145 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2146                const u8 *rates, unsigned int n_rates,
2147                u32 *mask)
2148 {
2149     int i, j;
2150 
2151     if (!sband)
2152         return -EINVAL;
2153 
2154     if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2155         return -EINVAL;
2156 
2157     *mask = 0;
2158 
2159     for (i = 0; i < n_rates; i++) {
2160         int rate = (rates[i] & 0x7f) * 5;
2161         bool found = false;
2162 
2163         for (j = 0; j < sband->n_bitrates; j++) {
2164             if (sband->bitrates[j].bitrate == rate) {
2165                 found = true;
2166                 *mask |= BIT(j);
2167                 break;
2168             }
2169         }
2170         if (!found)
2171             return -EINVAL;
2172     }
2173 
2174     /*
2175      * mask must have at least one bit set here since we
2176      * didn't accept a 0-length rates array nor allowed
2177      * entries in the array that didn't exist
2178      */
2179 
2180     return 0;
2181 }
2182 
2183 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2184 {
2185     enum nl80211_band band;
2186     unsigned int n_channels = 0;
2187 
2188     for (band = 0; band < NUM_NL80211_BANDS; band++)
2189         if (wiphy->bands[band])
2190             n_channels += wiphy->bands[band]->n_channels;
2191 
2192     return n_channels;
2193 }
2194 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2195 
2196 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2197              struct station_info *sinfo)
2198 {
2199     struct cfg80211_registered_device *rdev;
2200     struct wireless_dev *wdev;
2201 
2202     wdev = dev->ieee80211_ptr;
2203     if (!wdev)
2204         return -EOPNOTSUPP;
2205 
2206     rdev = wiphy_to_rdev(wdev->wiphy);
2207     if (!rdev->ops->get_station)
2208         return -EOPNOTSUPP;
2209 
2210     memset(sinfo, 0, sizeof(*sinfo));
2211 
2212     return rdev_get_station(rdev, dev, mac_addr, sinfo);
2213 }
2214 EXPORT_SYMBOL(cfg80211_get_station);
2215 
2216 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2217 {
2218     int i;
2219 
2220     if (!f)
2221         return;
2222 
2223     kfree(f->serv_spec_info);
2224     kfree(f->srf_bf);
2225     kfree(f->srf_macs);
2226     for (i = 0; i < f->num_rx_filters; i++)
2227         kfree(f->rx_filters[i].filter);
2228 
2229     for (i = 0; i < f->num_tx_filters; i++)
2230         kfree(f->tx_filters[i].filter);
2231 
2232     kfree(f->rx_filters);
2233     kfree(f->tx_filters);
2234     kfree(f);
2235 }
2236 EXPORT_SYMBOL(cfg80211_free_nan_func);
2237 
2238 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2239                 u32 center_freq_khz, u32 bw_khz)
2240 {
2241     u32 start_freq_khz, end_freq_khz;
2242 
2243     start_freq_khz = center_freq_khz - (bw_khz / 2);
2244     end_freq_khz = center_freq_khz + (bw_khz / 2);
2245 
2246     if (start_freq_khz >= freq_range->start_freq_khz &&
2247         end_freq_khz <= freq_range->end_freq_khz)
2248         return true;
2249 
2250     return false;
2251 }
2252 
2253 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2254 {
2255     sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2256                 sizeof(*(sinfo->pertid)),
2257                 gfp);
2258     if (!sinfo->pertid)
2259         return -ENOMEM;
2260 
2261     return 0;
2262 }
2263 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2264 
2265 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2266 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2267 const unsigned char rfc1042_header[] __aligned(2) =
2268     { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2269 EXPORT_SYMBOL(rfc1042_header);
2270 
2271 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2272 const unsigned char bridge_tunnel_header[] __aligned(2) =
2273     { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2274 EXPORT_SYMBOL(bridge_tunnel_header);
2275 
2276 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2277 struct iapp_layer2_update {
2278     u8 da[ETH_ALEN];    /* broadcast */
2279     u8 sa[ETH_ALEN];    /* STA addr */
2280     __be16 len;     /* 6 */
2281     u8 dsap;        /* 0 */
2282     u8 ssap;        /* 0 */
2283     u8 control;
2284     u8 xid_info[3];
2285 } __packed;
2286 
2287 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2288 {
2289     struct iapp_layer2_update *msg;
2290     struct sk_buff *skb;
2291 
2292     /* Send Level 2 Update Frame to update forwarding tables in layer 2
2293      * bridge devices */
2294 
2295     skb = dev_alloc_skb(sizeof(*msg));
2296     if (!skb)
2297         return;
2298     msg = skb_put(skb, sizeof(*msg));
2299 
2300     /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2301      * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2302 
2303     eth_broadcast_addr(msg->da);
2304     ether_addr_copy(msg->sa, addr);
2305     msg->len = htons(6);
2306     msg->dsap = 0;
2307     msg->ssap = 0x01;   /* NULL LSAP, CR Bit: Response */
2308     msg->control = 0xaf;    /* XID response lsb.1111F101.
2309                  * F=0 (no poll command; unsolicited frame) */
2310     msg->xid_info[0] = 0x81;    /* XID format identifier */
2311     msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
2312     msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
2313 
2314     skb->dev = dev;
2315     skb->protocol = eth_type_trans(skb, dev);
2316     memset(skb->cb, 0, sizeof(skb->cb));
2317     netif_rx(skb);
2318 }
2319 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2320 
2321 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2322                   enum ieee80211_vht_chanwidth bw,
2323                   int mcs, bool ext_nss_bw_capable,
2324                   unsigned int max_vht_nss)
2325 {
2326     u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2327     int ext_nss_bw;
2328     int supp_width;
2329     int i, mcs_encoding;
2330 
2331     if (map == 0xffff)
2332         return 0;
2333 
2334     if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2335         return 0;
2336     if (mcs <= 7)
2337         mcs_encoding = 0;
2338     else if (mcs == 8)
2339         mcs_encoding = 1;
2340     else
2341         mcs_encoding = 2;
2342 
2343     if (!max_vht_nss) {
2344         /* find max_vht_nss for the given MCS */
2345         for (i = 7; i >= 0; i--) {
2346             int supp = (map >> (2 * i)) & 3;
2347 
2348             if (supp == 3)
2349                 continue;
2350 
2351             if (supp >= mcs_encoding) {
2352                 max_vht_nss = i + 1;
2353                 break;
2354             }
2355         }
2356     }
2357 
2358     if (!(cap->supp_mcs.tx_mcs_map &
2359             cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2360         return max_vht_nss;
2361 
2362     ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2363                    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2364     supp_width = le32_get_bits(cap->vht_cap_info,
2365                    IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2366 
2367     /* if not capable, treat ext_nss_bw as 0 */
2368     if (!ext_nss_bw_capable)
2369         ext_nss_bw = 0;
2370 
2371     /* This is invalid */
2372     if (supp_width == 3)
2373         return 0;
2374 
2375     /* This is an invalid combination so pretend nothing is supported */
2376     if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2377         return 0;
2378 
2379     /*
2380      * Cover all the special cases according to IEEE 802.11-2016
2381      * Table 9-250. All other cases are either factor of 1 or not
2382      * valid/supported.
2383      */
2384     switch (bw) {
2385     case IEEE80211_VHT_CHANWIDTH_USE_HT:
2386     case IEEE80211_VHT_CHANWIDTH_80MHZ:
2387         if ((supp_width == 1 || supp_width == 2) &&
2388             ext_nss_bw == 3)
2389             return 2 * max_vht_nss;
2390         break;
2391     case IEEE80211_VHT_CHANWIDTH_160MHZ:
2392         if (supp_width == 0 &&
2393             (ext_nss_bw == 1 || ext_nss_bw == 2))
2394             return max_vht_nss / 2;
2395         if (supp_width == 0 &&
2396             ext_nss_bw == 3)
2397             return (3 * max_vht_nss) / 4;
2398         if (supp_width == 1 &&
2399             ext_nss_bw == 3)
2400             return 2 * max_vht_nss;
2401         break;
2402     case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2403         if (supp_width == 0 && ext_nss_bw == 1)
2404             return 0; /* not possible */
2405         if (supp_width == 0 &&
2406             ext_nss_bw == 2)
2407             return max_vht_nss / 2;
2408         if (supp_width == 0 &&
2409             ext_nss_bw == 3)
2410             return (3 * max_vht_nss) / 4;
2411         if (supp_width == 1 &&
2412             ext_nss_bw == 0)
2413             return 0; /* not possible */
2414         if (supp_width == 1 &&
2415             ext_nss_bw == 1)
2416             return max_vht_nss / 2;
2417         if (supp_width == 1 &&
2418             ext_nss_bw == 2)
2419             return (3 * max_vht_nss) / 4;
2420         break;
2421     }
2422 
2423     /* not covered or invalid combination received */
2424     return max_vht_nss;
2425 }
2426 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2427 
2428 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2429                  bool is_4addr, u8 check_swif)
2430 
2431 {
2432     bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2433 
2434     switch (check_swif) {
2435     case 0:
2436         if (is_vlan && is_4addr)
2437             return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2438         return wiphy->interface_modes & BIT(iftype);
2439     case 1:
2440         if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2441             return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2442         return wiphy->software_iftypes & BIT(iftype);
2443     default:
2444         break;
2445     }
2446 
2447     return false;
2448 }
2449 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2450 
2451 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2452 {
2453     struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2454 
2455     ASSERT_WDEV_LOCK(wdev);
2456 
2457     switch (wdev->iftype) {
2458     case NL80211_IFTYPE_AP:
2459     case NL80211_IFTYPE_P2P_GO:
2460         __cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2461         break;
2462     default:
2463         /* per-link not relevant */
2464         break;
2465     }
2466 
2467     wdev->valid_links &= ~BIT(link_id);
2468 
2469     rdev_del_intf_link(rdev, wdev, link_id);
2470 
2471     eth_zero_addr(wdev->links[link_id].addr);
2472 }
2473 
2474 void cfg80211_remove_links(struct wireless_dev *wdev)
2475 {
2476     unsigned int link_id;
2477 
2478     wdev_lock(wdev);
2479     if (wdev->valid_links) {
2480         for_each_valid_link(wdev, link_id)
2481             cfg80211_remove_link(wdev, link_id);
2482     }
2483     wdev_unlock(wdev);
2484 }
2485 
2486 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2487                  struct wireless_dev *wdev)
2488 {
2489     cfg80211_remove_links(wdev);
2490 
2491     return rdev_del_virtual_intf(rdev, wdev);
2492 }
2493 
2494 const struct wiphy_iftype_ext_capab *
2495 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2496 {
2497     int i;
2498 
2499     for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2500         if (wiphy->iftype_ext_capab[i].iftype == type)
2501             return &wiphy->iftype_ext_capab[i];
2502     }
2503 
2504     return NULL;
2505 }
2506 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);