linux/net/wireless/util.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Wireless utility functions
   4 *
   5 * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
   6 * Copyright 2013-2014  Intel Mobile Communications GmbH
   7 * Copyright 2017       Intel Deutschland GmbH
   8 * Copyright (C) 2018-2019 Intel Corporation
   9 */
  10#include <linux/export.h>
  11#include <linux/bitops.h>
  12#include <linux/etherdevice.h>
  13#include <linux/slab.h>
  14#include <linux/ieee80211.h>
  15#include <net/cfg80211.h>
  16#include <net/ip.h>
  17#include <net/dsfield.h>
  18#include <linux/if_vlan.h>
  19#include <linux/mpls.h>
  20#include <linux/gcd.h>
  21#include <linux/bitfield.h>
  22#include <linux/nospec.h>
  23#include "core.h"
  24#include "rdev-ops.h"
  25
  26
  27struct ieee80211_rate *
  28ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  29                            u32 basic_rates, int bitrate)
  30{
  31        struct ieee80211_rate *result = &sband->bitrates[0];
  32        int i;
  33
  34        for (i = 0; i < sband->n_bitrates; i++) {
  35                if (!(basic_rates & BIT(i)))
  36                        continue;
  37                if (sband->bitrates[i].bitrate > bitrate)
  38                        continue;
  39                result = &sband->bitrates[i];
  40        }
  41
  42        return result;
  43}
  44EXPORT_SYMBOL(ieee80211_get_response_rate);
  45
  46u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
  47                              enum nl80211_bss_scan_width scan_width)
  48{
  49        struct ieee80211_rate *bitrates;
  50        u32 mandatory_rates = 0;
  51        enum ieee80211_rate_flags mandatory_flag;
  52        int i;
  53
  54        if (WARN_ON(!sband))
  55                return 1;
  56
  57        if (sband->band == NL80211_BAND_2GHZ) {
  58                if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
  59                    scan_width == NL80211_BSS_CHAN_WIDTH_10)
  60                        mandatory_flag = IEEE80211_RATE_MANDATORY_G;
  61                else
  62                        mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  63        } else {
  64                mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  65        }
  66
  67        bitrates = sband->bitrates;
  68        for (i = 0; i < sband->n_bitrates; i++)
  69                if (bitrates[i].flags & mandatory_flag)
  70                        mandatory_rates |= BIT(i);
  71        return mandatory_rates;
  72}
  73EXPORT_SYMBOL(ieee80211_mandatory_rates);
  74
  75int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
  76{
  77        /* see 802.11 17.3.8.3.2 and Annex J
  78         * there are overlapping channel numbers in 5GHz and 2GHz bands */
  79        if (chan <= 0)
  80                return 0; /* not supported */
  81        switch (band) {
  82        case NL80211_BAND_2GHZ:
  83                if (chan == 14)
  84                        return 2484;
  85                else if (chan < 14)
  86                        return 2407 + chan * 5;
  87                break;
  88        case NL80211_BAND_5GHZ:
  89                if (chan >= 182 && chan <= 196)
  90                        return 4000 + chan * 5;
  91                else
  92                        return 5000 + chan * 5;
  93                break;
  94        case NL80211_BAND_60GHZ:
  95                if (chan < 7)
  96                        return 56160 + chan * 2160;
  97                break;
  98        default:
  99                ;
 100        }
 101        return 0; /* not supported */
 102}
 103EXPORT_SYMBOL(ieee80211_channel_to_frequency);
 104
 105int ieee80211_frequency_to_channel(int freq)
 106{
 107        /* see 802.11 17.3.8.3.2 and Annex J */
 108        if (freq == 2484)
 109                return 14;
 110        else if (freq < 2484)
 111                return (freq - 2407) / 5;
 112        else if (freq >= 4910 && freq <= 4980)
 113                return (freq - 4000) / 5;
 114        else if (freq <= 45000) /* DMG band lower limit */
 115                return (freq - 5000) / 5;
 116        else if (freq >= 58320 && freq <= 70200)
 117                return (freq - 56160) / 2160;
 118        else
 119                return 0;
 120}
 121EXPORT_SYMBOL(ieee80211_frequency_to_channel);
 122
 123struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
 124{
 125        enum nl80211_band band;
 126        struct ieee80211_supported_band *sband;
 127        int i;
 128
 129        for (band = 0; band < NUM_NL80211_BANDS; band++) {
 130                sband = wiphy->bands[band];
 131
 132                if (!sband)
 133                        continue;
 134
 135                for (i = 0; i < sband->n_channels; i++) {
 136                        if (sband->channels[i].center_freq == freq)
 137                                return &sband->channels[i];
 138                }
 139        }
 140
 141        return NULL;
 142}
 143EXPORT_SYMBOL(ieee80211_get_channel);
 144
 145static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
 146{
 147        int i, want;
 148
 149        switch (sband->band) {
 150        case NL80211_BAND_5GHZ:
 151                want = 3;
 152                for (i = 0; i < sband->n_bitrates; i++) {
 153                        if (sband->bitrates[i].bitrate == 60 ||
 154                            sband->bitrates[i].bitrate == 120 ||
 155                            sband->bitrates[i].bitrate == 240) {
 156                                sband->bitrates[i].flags |=
 157                                        IEEE80211_RATE_MANDATORY_A;
 158                                want--;
 159                        }
 160                }
 161                WARN_ON(want);
 162                break;
 163        case NL80211_BAND_2GHZ:
 164                want = 7;
 165                for (i = 0; i < sband->n_bitrates; i++) {
 166                        switch (sband->bitrates[i].bitrate) {
 167                        case 10:
 168                        case 20:
 169                        case 55:
 170                        case 110:
 171                                sband->bitrates[i].flags |=
 172                                        IEEE80211_RATE_MANDATORY_B |
 173                                        IEEE80211_RATE_MANDATORY_G;
 174                                want--;
 175                                break;
 176                        case 60:
 177                        case 120:
 178                        case 240:
 179                                sband->bitrates[i].flags |=
 180                                        IEEE80211_RATE_MANDATORY_G;
 181                                want--;
 182                                /* fall through */
 183                        default:
 184                                sband->bitrates[i].flags |=
 185                                        IEEE80211_RATE_ERP_G;
 186                                break;
 187                        }
 188                }
 189                WARN_ON(want != 0 && want != 3);
 190                break;
 191        case NL80211_BAND_60GHZ:
 192                /* check for mandatory HT MCS 1..4 */
 193                WARN_ON(!sband->ht_cap.ht_supported);
 194                WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
 195                break;
 196        case NUM_NL80211_BANDS:
 197        default:
 198                WARN_ON(1);
 199                break;
 200        }
 201}
 202
 203void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
 204{
 205        enum nl80211_band band;
 206
 207        for (band = 0; band < NUM_NL80211_BANDS; band++)
 208                if (wiphy->bands[band])
 209                        set_mandatory_flags_band(wiphy->bands[band]);
 210}
 211
 212bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
 213{
 214        int i;
 215        for (i = 0; i < wiphy->n_cipher_suites; i++)
 216                if (cipher == wiphy->cipher_suites[i])
 217                        return true;
 218        return false;
 219}
 220
 221int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
 222                                   struct key_params *params, int key_idx,
 223                                   bool pairwise, const u8 *mac_addr)
 224{
 225        if (key_idx < 0 || key_idx > 5)
 226                return -EINVAL;
 227
 228        if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
 229                return -EINVAL;
 230
 231        if (pairwise && !mac_addr)
 232                return -EINVAL;
 233
 234        switch (params->cipher) {
 235        case WLAN_CIPHER_SUITE_TKIP:
 236                /* Extended Key ID can only be used with CCMP/GCMP ciphers */
 237                if ((pairwise && key_idx) ||
 238                    params->mode != NL80211_KEY_RX_TX)
 239                        return -EINVAL;
 240                break;
 241        case WLAN_CIPHER_SUITE_CCMP:
 242        case WLAN_CIPHER_SUITE_CCMP_256:
 243        case WLAN_CIPHER_SUITE_GCMP:
 244        case WLAN_CIPHER_SUITE_GCMP_256:
 245                /* IEEE802.11-2016 allows only 0 and - when supporting
 246                 * Extended Key ID - 1 as index for pairwise keys.
 247                 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
 248                 * the driver supports Extended Key ID.
 249                 * @NL80211_KEY_SET_TX can't be set when installing and
 250                 * validating a key.
 251                 */
 252                if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
 253                    params->mode == NL80211_KEY_SET_TX)
 254                        return -EINVAL;
 255                if (wiphy_ext_feature_isset(&rdev->wiphy,
 256                                            NL80211_EXT_FEATURE_EXT_KEY_ID)) {
 257                        if (pairwise && (key_idx < 0 || key_idx > 1))
 258                                return -EINVAL;
 259                } else if (pairwise && key_idx) {
 260                        return -EINVAL;
 261                }
 262                break;
 263        case WLAN_CIPHER_SUITE_AES_CMAC:
 264        case WLAN_CIPHER_SUITE_BIP_CMAC_256:
 265        case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 266        case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 267                /* Disallow BIP (group-only) cipher as pairwise cipher */
 268                if (pairwise)
 269                        return -EINVAL;
 270                if (key_idx < 4)
 271                        return -EINVAL;
 272                break;
 273        case WLAN_CIPHER_SUITE_WEP40:
 274        case WLAN_CIPHER_SUITE_WEP104:
 275                if (key_idx > 3)
 276                        return -EINVAL;
 277        default:
 278                break;
 279        }
 280
 281        switch (params->cipher) {
 282        case WLAN_CIPHER_SUITE_WEP40:
 283                if (params->key_len != WLAN_KEY_LEN_WEP40)
 284                        return -EINVAL;
 285                break;
 286        case WLAN_CIPHER_SUITE_TKIP:
 287                if (params->key_len != WLAN_KEY_LEN_TKIP)
 288                        return -EINVAL;
 289                break;
 290        case WLAN_CIPHER_SUITE_CCMP:
 291                if (params->key_len != WLAN_KEY_LEN_CCMP)
 292                        return -EINVAL;
 293                break;
 294        case WLAN_CIPHER_SUITE_CCMP_256:
 295                if (params->key_len != WLAN_KEY_LEN_CCMP_256)
 296                        return -EINVAL;
 297                break;
 298        case WLAN_CIPHER_SUITE_GCMP:
 299                if (params->key_len != WLAN_KEY_LEN_GCMP)
 300                        return -EINVAL;
 301                break;
 302        case WLAN_CIPHER_SUITE_GCMP_256:
 303                if (params->key_len != WLAN_KEY_LEN_GCMP_256)
 304                        return -EINVAL;
 305                break;
 306        case WLAN_CIPHER_SUITE_WEP104:
 307                if (params->key_len != WLAN_KEY_LEN_WEP104)
 308                        return -EINVAL;
 309                break;
 310        case WLAN_CIPHER_SUITE_AES_CMAC:
 311                if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
 312                        return -EINVAL;
 313                break;
 314        case WLAN_CIPHER_SUITE_BIP_CMAC_256:
 315                if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
 316                        return -EINVAL;
 317                break;
 318        case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 319                if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
 320                        return -EINVAL;
 321                break;
 322        case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 323                if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
 324                        return -EINVAL;
 325                break;
 326        default:
 327                /*
 328                 * We don't know anything about this algorithm,
 329                 * allow using it -- but the driver must check
 330                 * all parameters! We still check below whether
 331                 * or not the driver supports this algorithm,
 332                 * of course.
 333                 */
 334                break;
 335        }
 336
 337        if (params->seq) {
 338                switch (params->cipher) {
 339                case WLAN_CIPHER_SUITE_WEP40:
 340                case WLAN_CIPHER_SUITE_WEP104:
 341                        /* These ciphers do not use key sequence */
 342                        return -EINVAL;
 343                case WLAN_CIPHER_SUITE_TKIP:
 344                case WLAN_CIPHER_SUITE_CCMP:
 345                case WLAN_CIPHER_SUITE_CCMP_256:
 346                case WLAN_CIPHER_SUITE_GCMP:
 347                case WLAN_CIPHER_SUITE_GCMP_256:
 348                case WLAN_CIPHER_SUITE_AES_CMAC:
 349                case WLAN_CIPHER_SUITE_BIP_CMAC_256:
 350                case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 351                case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 352                        if (params->seq_len != 6)
 353                                return -EINVAL;
 354                        break;
 355                }
 356        }
 357
 358        if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
 359                return -EINVAL;
 360
 361        return 0;
 362}
 363
 364unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
 365{
 366        unsigned int hdrlen = 24;
 367
 368        if (ieee80211_is_data(fc)) {
 369                if (ieee80211_has_a4(fc))
 370                        hdrlen = 30;
 371                if (ieee80211_is_data_qos(fc)) {
 372                        hdrlen += IEEE80211_QOS_CTL_LEN;
 373                        if (ieee80211_has_order(fc))
 374                                hdrlen += IEEE80211_HT_CTL_LEN;
 375                }
 376                goto out;
 377        }
 378
 379        if (ieee80211_is_mgmt(fc)) {
 380                if (ieee80211_has_order(fc))
 381                        hdrlen += IEEE80211_HT_CTL_LEN;
 382                goto out;
 383        }
 384
 385        if (ieee80211_is_ctl(fc)) {
 386                /*
 387                 * ACK and CTS are 10 bytes, all others 16. To see how
 388                 * to get this condition consider
 389                 *   subtype mask:   0b0000000011110000 (0x00F0)
 390                 *   ACK subtype:    0b0000000011010000 (0x00D0)
 391                 *   CTS subtype:    0b0000000011000000 (0x00C0)
 392                 *   bits that matter:         ^^^      (0x00E0)
 393                 *   value of those: 0b0000000011000000 (0x00C0)
 394                 */
 395                if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
 396                        hdrlen = 10;
 397                else
 398                        hdrlen = 16;
 399        }
 400out:
 401        return hdrlen;
 402}
 403EXPORT_SYMBOL(ieee80211_hdrlen);
 404
 405unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
 406{
 407        const struct ieee80211_hdr *hdr =
 408                        (const struct ieee80211_hdr *)skb->data;
 409        unsigned int hdrlen;
 410
 411        if (unlikely(skb->len < 10))
 412                return 0;
 413        hdrlen = ieee80211_hdrlen(hdr->frame_control);
 414        if (unlikely(hdrlen > skb->len))
 415                return 0;
 416        return hdrlen;
 417}
 418EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
 419
 420static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
 421{
 422        int ae = flags & MESH_FLAGS_AE;
 423        /* 802.11-2012, 8.2.4.7.3 */
 424        switch (ae) {
 425        default:
 426        case 0:
 427                return 6;
 428        case MESH_FLAGS_AE_A4:
 429                return 12;
 430        case MESH_FLAGS_AE_A5_A6:
 431                return 18;
 432        }
 433}
 434
 435unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
 436{
 437        return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
 438}
 439EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
 440
 441int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
 442                                  const u8 *addr, enum nl80211_iftype iftype,
 443                                  u8 data_offset)
 444{
 445        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 446        struct {
 447                u8 hdr[ETH_ALEN] __aligned(2);
 448                __be16 proto;
 449        } payload;
 450        struct ethhdr tmp;
 451        u16 hdrlen;
 452        u8 mesh_flags = 0;
 453
 454        if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
 455                return -1;
 456
 457        hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
 458        if (skb->len < hdrlen + 8)
 459                return -1;
 460
 461        /* convert IEEE 802.11 header + possible LLC headers into Ethernet
 462         * header
 463         * IEEE 802.11 address fields:
 464         * ToDS FromDS Addr1 Addr2 Addr3 Addr4
 465         *   0     0   DA    SA    BSSID n/a
 466         *   0     1   DA    BSSID SA    n/a
 467         *   1     0   BSSID SA    DA    n/a
 468         *   1     1   RA    TA    DA    SA
 469         */
 470        memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
 471        memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
 472
 473        if (iftype == NL80211_IFTYPE_MESH_POINT)
 474                skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
 475
 476        mesh_flags &= MESH_FLAGS_AE;
 477
 478        switch (hdr->frame_control &
 479                cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
 480        case cpu_to_le16(IEEE80211_FCTL_TODS):
 481                if (unlikely(iftype != NL80211_IFTYPE_AP &&
 482                             iftype != NL80211_IFTYPE_AP_VLAN &&
 483                             iftype != NL80211_IFTYPE_P2P_GO))
 484                        return -1;
 485                break;
 486        case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
 487                if (unlikely(iftype != NL80211_IFTYPE_WDS &&
 488                             iftype != NL80211_IFTYPE_MESH_POINT &&
 489                             iftype != NL80211_IFTYPE_AP_VLAN &&
 490                             iftype != NL80211_IFTYPE_STATION))
 491                        return -1;
 492                if (iftype == NL80211_IFTYPE_MESH_POINT) {
 493                        if (mesh_flags == MESH_FLAGS_AE_A4)
 494                                return -1;
 495                        if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
 496                                skb_copy_bits(skb, hdrlen +
 497                                        offsetof(struct ieee80211s_hdr, eaddr1),
 498                                        tmp.h_dest, 2 * ETH_ALEN);
 499                        }
 500                        hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
 501                }
 502                break;
 503        case cpu_to_le16(IEEE80211_FCTL_FROMDS):
 504                if ((iftype != NL80211_IFTYPE_STATION &&
 505                     iftype != NL80211_IFTYPE_P2P_CLIENT &&
 506                     iftype != NL80211_IFTYPE_MESH_POINT) ||
 507                    (is_multicast_ether_addr(tmp.h_dest) &&
 508                     ether_addr_equal(tmp.h_source, addr)))
 509                        return -1;
 510                if (iftype == NL80211_IFTYPE_MESH_POINT) {
 511                        if (mesh_flags == MESH_FLAGS_AE_A5_A6)
 512                                return -1;
 513                        if (mesh_flags == MESH_FLAGS_AE_A4)
 514                                skb_copy_bits(skb, hdrlen +
 515                                        offsetof(struct ieee80211s_hdr, eaddr1),
 516                                        tmp.h_source, ETH_ALEN);
 517                        hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
 518                }
 519                break;
 520        case cpu_to_le16(0):
 521                if (iftype != NL80211_IFTYPE_ADHOC &&
 522                    iftype != NL80211_IFTYPE_STATION &&
 523                    iftype != NL80211_IFTYPE_OCB)
 524                                return -1;
 525                break;
 526        }
 527
 528        skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
 529        tmp.h_proto = payload.proto;
 530
 531        if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
 532                    tmp.h_proto != htons(ETH_P_AARP) &&
 533                    tmp.h_proto != htons(ETH_P_IPX)) ||
 534                   ether_addr_equal(payload.hdr, bridge_tunnel_header)))
 535                /* remove RFC1042 or Bridge-Tunnel encapsulation and
 536                 * replace EtherType */
 537                hdrlen += ETH_ALEN + 2;
 538        else
 539                tmp.h_proto = htons(skb->len - hdrlen);
 540
 541        pskb_pull(skb, hdrlen);
 542
 543        if (!ehdr)
 544                ehdr = skb_push(skb, sizeof(struct ethhdr));
 545        memcpy(ehdr, &tmp, sizeof(tmp));
 546
 547        return 0;
 548}
 549EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
 550
 551static void
 552__frame_add_frag(struct sk_buff *skb, struct page *page,
 553                 void *ptr, int len, int size)
 554{
 555        struct skb_shared_info *sh = skb_shinfo(skb);
 556        int page_offset;
 557
 558        page_ref_inc(page);
 559        page_offset = ptr - page_address(page);
 560        skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
 561}
 562
 563static void
 564__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
 565                            int offset, int len)
 566{
 567        struct skb_shared_info *sh = skb_shinfo(skb);
 568        const skb_frag_t *frag = &sh->frags[0];
 569        struct page *frag_page;
 570        void *frag_ptr;
 571        int frag_len, frag_size;
 572        int head_size = skb->len - skb->data_len;
 573        int cur_len;
 574
 575        frag_page = virt_to_head_page(skb->head);
 576        frag_ptr = skb->data;
 577        frag_size = head_size;
 578
 579        while (offset >= frag_size) {
 580                offset -= frag_size;
 581                frag_page = skb_frag_page(frag);
 582                frag_ptr = skb_frag_address(frag);
 583                frag_size = skb_frag_size(frag);
 584                frag++;
 585        }
 586
 587        frag_ptr += offset;
 588        frag_len = frag_size - offset;
 589
 590        cur_len = min(len, frag_len);
 591
 592        __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
 593        len -= cur_len;
 594
 595        while (len > 0) {
 596                frag_len = skb_frag_size(frag);
 597                cur_len = min(len, frag_len);
 598                __frame_add_frag(frame, skb_frag_page(frag),
 599                                 skb_frag_address(frag), cur_len, frag_len);
 600                len -= cur_len;
 601                frag++;
 602        }
 603}
 604
 605static struct sk_buff *
 606__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
 607                       int offset, int len, bool reuse_frag)
 608{
 609        struct sk_buff *frame;
 610        int cur_len = len;
 611
 612        if (skb->len - offset < len)
 613                return NULL;
 614
 615        /*
 616         * When reusing framents, copy some data to the head to simplify
 617         * ethernet header handling and speed up protocol header processing
 618         * in the stack later.
 619         */
 620        if (reuse_frag)
 621                cur_len = min_t(int, len, 32);
 622
 623        /*
 624         * Allocate and reserve two bytes more for payload
 625         * alignment since sizeof(struct ethhdr) is 14.
 626         */
 627        frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
 628        if (!frame)
 629                return NULL;
 630
 631        skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
 632        skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
 633
 634        len -= cur_len;
 635        if (!len)
 636                return frame;
 637
 638        offset += cur_len;
 639        __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
 640
 641        return frame;
 642}
 643
 644void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
 645                              const u8 *addr, enum nl80211_iftype iftype,
 646                              const unsigned int extra_headroom,
 647                              const u8 *check_da, const u8 *check_sa)
 648{
 649        unsigned int hlen = ALIGN(extra_headroom, 4);
 650        struct sk_buff *frame = NULL;
 651        u16 ethertype;
 652        u8 *payload;
 653        int offset = 0, remaining;
 654        struct ethhdr eth;
 655        bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
 656        bool reuse_skb = false;
 657        bool last = false;
 658
 659        while (!last) {
 660                unsigned int subframe_len;
 661                int len;
 662                u8 padding;
 663
 664                skb_copy_bits(skb, offset, &eth, sizeof(eth));
 665                len = ntohs(eth.h_proto);
 666                subframe_len = sizeof(struct ethhdr) + len;
 667                padding = (4 - subframe_len) & 0x3;
 668
 669                /* the last MSDU has no padding */
 670                remaining = skb->len - offset;
 671                if (subframe_len > remaining)
 672                        goto purge;
 673
 674                offset += sizeof(struct ethhdr);
 675                last = remaining <= subframe_len + padding;
 676
 677                /* FIXME: should we really accept multicast DA? */
 678                if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
 679                     !ether_addr_equal(check_da, eth.h_dest)) ||
 680                    (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
 681                        offset += len + padding;
 682                        continue;
 683                }
 684
 685                /* reuse skb for the last subframe */
 686                if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
 687                        skb_pull(skb, offset);
 688                        frame = skb;
 689                        reuse_skb = true;
 690                } else {
 691                        frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
 692                                                       reuse_frag);
 693                        if (!frame)
 694                                goto purge;
 695
 696                        offset += len + padding;
 697                }
 698
 699                skb_reset_network_header(frame);
 700                frame->dev = skb->dev;
 701                frame->priority = skb->priority;
 702
 703                payload = frame->data;
 704                ethertype = (payload[6] << 8) | payload[7];
 705                if (likely((ether_addr_equal(payload, rfc1042_header) &&
 706                            ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 707                           ether_addr_equal(payload, bridge_tunnel_header))) {
 708                        eth.h_proto = htons(ethertype);
 709                        skb_pull(frame, ETH_ALEN + 2);
 710                }
 711
 712                memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
 713                __skb_queue_tail(list, frame);
 714        }
 715
 716        if (!reuse_skb)
 717                dev_kfree_skb(skb);
 718
 719        return;
 720
 721 purge:
 722        __skb_queue_purge(list);
 723        dev_kfree_skb(skb);
 724}
 725EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
 726
 727/* Given a data frame determine the 802.1p/1d tag to use. */
 728unsigned int cfg80211_classify8021d(struct sk_buff *skb,
 729                                    struct cfg80211_qos_map *qos_map)
 730{
 731        unsigned int dscp;
 732        unsigned char vlan_priority;
 733        unsigned int ret;
 734
 735        /* skb->priority values from 256->263 are magic values to
 736         * directly indicate a specific 802.1d priority.  This is used
 737         * to allow 802.1d priority to be passed directly in from VLAN
 738         * tags, etc.
 739         */
 740        if (skb->priority >= 256 && skb->priority <= 263) {
 741                ret = skb->priority - 256;
 742                goto out;
 743        }
 744
 745        if (skb_vlan_tag_present(skb)) {
 746                vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
 747                        >> VLAN_PRIO_SHIFT;
 748                if (vlan_priority > 0) {
 749                        ret = vlan_priority;
 750                        goto out;
 751                }
 752        }
 753
 754        switch (skb->protocol) {
 755        case htons(ETH_P_IP):
 756                dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
 757                break;
 758        case htons(ETH_P_IPV6):
 759                dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
 760                break;
 761        case htons(ETH_P_MPLS_UC):
 762        case htons(ETH_P_MPLS_MC): {
 763                struct mpls_label mpls_tmp, *mpls;
 764
 765                mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
 766                                          sizeof(*mpls), &mpls_tmp);
 767                if (!mpls)
 768                        return 0;
 769
 770                ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
 771                        >> MPLS_LS_TC_SHIFT;
 772                goto out;
 773        }
 774        case htons(ETH_P_80221):
 775                /* 802.21 is always network control traffic */
 776                return 7;
 777        default:
 778                return 0;
 779        }
 780
 781        if (qos_map) {
 782                unsigned int i, tmp_dscp = dscp >> 2;
 783
 784                for (i = 0; i < qos_map->num_des; i++) {
 785                        if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
 786                                ret = qos_map->dscp_exception[i].up;
 787                                goto out;
 788                        }
 789                }
 790
 791                for (i = 0; i < 8; i++) {
 792                        if (tmp_dscp >= qos_map->up[i].low &&
 793                            tmp_dscp <= qos_map->up[i].high) {
 794                                ret = i;
 795                                goto out;
 796                        }
 797                }
 798        }
 799
 800        ret = dscp >> 5;
 801out:
 802        return array_index_nospec(ret, IEEE80211_NUM_TIDS);
 803}
 804EXPORT_SYMBOL(cfg80211_classify8021d);
 805
 806const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
 807{
 808        const struct cfg80211_bss_ies *ies;
 809
 810        ies = rcu_dereference(bss->ies);
 811        if (!ies)
 812                return NULL;
 813
 814        return cfg80211_find_elem(id, ies->data, ies->len);
 815}
 816EXPORT_SYMBOL(ieee80211_bss_get_elem);
 817
 818void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
 819{
 820        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 821        struct net_device *dev = wdev->netdev;
 822        int i;
 823
 824        if (!wdev->connect_keys)
 825                return;
 826
 827        for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
 828                if (!wdev->connect_keys->params[i].cipher)
 829                        continue;
 830                if (rdev_add_key(rdev, dev, i, false, NULL,
 831                                 &wdev->connect_keys->params[i])) {
 832                        netdev_err(dev, "failed to set key %d\n", i);
 833                        continue;
 834                }
 835                if (wdev->connect_keys->def == i &&
 836                    rdev_set_default_key(rdev, dev, i, true, true)) {
 837                        netdev_err(dev, "failed to set defkey %d\n", i);
 838                        continue;
 839                }
 840        }
 841
 842        kzfree(wdev->connect_keys);
 843        wdev->connect_keys = NULL;
 844}
 845
 846void cfg80211_process_wdev_events(struct wireless_dev *wdev)
 847{
 848        struct cfg80211_event *ev;
 849        unsigned long flags;
 850
 851        spin_lock_irqsave(&wdev->event_lock, flags);
 852        while (!list_empty(&wdev->event_list)) {
 853                ev = list_first_entry(&wdev->event_list,
 854                                      struct cfg80211_event, list);
 855                list_del(&ev->list);
 856                spin_unlock_irqrestore(&wdev->event_lock, flags);
 857
 858                wdev_lock(wdev);
 859                switch (ev->type) {
 860                case EVENT_CONNECT_RESULT:
 861                        __cfg80211_connect_result(
 862                                wdev->netdev,
 863                                &ev->cr,
 864                                ev->cr.status == WLAN_STATUS_SUCCESS);
 865                        break;
 866                case EVENT_ROAMED:
 867                        __cfg80211_roamed(wdev, &ev->rm);
 868                        break;
 869                case EVENT_DISCONNECTED:
 870                        __cfg80211_disconnected(wdev->netdev,
 871                                                ev->dc.ie, ev->dc.ie_len,
 872                                                ev->dc.reason,
 873                                                !ev->dc.locally_generated);
 874                        break;
 875                case EVENT_IBSS_JOINED:
 876                        __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
 877                                               ev->ij.channel);
 878                        break;
 879                case EVENT_STOPPED:
 880                        __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
 881                        break;
 882                case EVENT_PORT_AUTHORIZED:
 883                        __cfg80211_port_authorized(wdev, ev->pa.bssid);
 884                        break;
 885                }
 886                wdev_unlock(wdev);
 887
 888                kfree(ev);
 889
 890                spin_lock_irqsave(&wdev->event_lock, flags);
 891        }
 892        spin_unlock_irqrestore(&wdev->event_lock, flags);
 893}
 894
 895void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
 896{
 897        struct wireless_dev *wdev;
 898
 899        ASSERT_RTNL();
 900
 901        list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
 902                cfg80211_process_wdev_events(wdev);
 903}
 904
 905int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
 906                          struct net_device *dev, enum nl80211_iftype ntype,
 907                          struct vif_params *params)
 908{
 909        int err;
 910        enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
 911
 912        ASSERT_RTNL();
 913
 914        /* don't support changing VLANs, you just re-create them */
 915        if (otype == NL80211_IFTYPE_AP_VLAN)
 916                return -EOPNOTSUPP;
 917
 918        /* cannot change into P2P device or NAN */
 919        if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
 920            ntype == NL80211_IFTYPE_NAN)
 921                return -EOPNOTSUPP;
 922
 923        if (!rdev->ops->change_virtual_intf ||
 924            !(rdev->wiphy.interface_modes & (1 << ntype)))
 925                return -EOPNOTSUPP;
 926
 927        /* if it's part of a bridge, reject changing type to station/ibss */
 928        if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
 929            (ntype == NL80211_IFTYPE_ADHOC ||
 930             ntype == NL80211_IFTYPE_STATION ||
 931             ntype == NL80211_IFTYPE_P2P_CLIENT))
 932                return -EBUSY;
 933
 934        if (ntype != otype) {
 935                dev->ieee80211_ptr->use_4addr = false;
 936                dev->ieee80211_ptr->mesh_id_up_len = 0;
 937                wdev_lock(dev->ieee80211_ptr);
 938                rdev_set_qos_map(rdev, dev, NULL);
 939                wdev_unlock(dev->ieee80211_ptr);
 940
 941                switch (otype) {
 942                case NL80211_IFTYPE_AP:
 943                        cfg80211_stop_ap(rdev, dev, true);
 944                        break;
 945                case NL80211_IFTYPE_ADHOC:
 946                        cfg80211_leave_ibss(rdev, dev, false);
 947                        break;
 948                case NL80211_IFTYPE_STATION:
 949                case NL80211_IFTYPE_P2P_CLIENT:
 950                        wdev_lock(dev->ieee80211_ptr);
 951                        cfg80211_disconnect(rdev, dev,
 952                                            WLAN_REASON_DEAUTH_LEAVING, true);
 953                        wdev_unlock(dev->ieee80211_ptr);
 954                        break;
 955                case NL80211_IFTYPE_MESH_POINT:
 956                        /* mesh should be handled? */
 957                        break;
 958                default:
 959                        break;
 960                }
 961
 962                cfg80211_process_rdev_events(rdev);
 963        }
 964
 965        err = rdev_change_virtual_intf(rdev, dev, ntype, params);
 966
 967        WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
 968
 969        if (!err && params && params->use_4addr != -1)
 970                dev->ieee80211_ptr->use_4addr = params->use_4addr;
 971
 972        if (!err) {
 973                dev->priv_flags &= ~IFF_DONT_BRIDGE;
 974                switch (ntype) {
 975                case NL80211_IFTYPE_STATION:
 976                        if (dev->ieee80211_ptr->use_4addr)
 977                                break;
 978                        /* fall through */
 979                case NL80211_IFTYPE_OCB:
 980                case NL80211_IFTYPE_P2P_CLIENT:
 981                case NL80211_IFTYPE_ADHOC:
 982                        dev->priv_flags |= IFF_DONT_BRIDGE;
 983                        break;
 984                case NL80211_IFTYPE_P2P_GO:
 985                case NL80211_IFTYPE_AP:
 986                case NL80211_IFTYPE_AP_VLAN:
 987                case NL80211_IFTYPE_WDS:
 988                case NL80211_IFTYPE_MESH_POINT:
 989                        /* bridging OK */
 990                        break;
 991                case NL80211_IFTYPE_MONITOR:
 992                        /* monitor can't bridge anyway */
 993                        break;
 994                case NL80211_IFTYPE_UNSPECIFIED:
 995                case NUM_NL80211_IFTYPES:
 996                        /* not happening */
 997                        break;
 998                case NL80211_IFTYPE_P2P_DEVICE:
 999                case NL80211_IFTYPE_NAN:
1000                        WARN_ON(1);
1001                        break;
1002                }
1003        }
1004
1005        if (!err && ntype != otype && netif_running(dev)) {
1006                cfg80211_update_iface_num(rdev, ntype, 1);
1007                cfg80211_update_iface_num(rdev, otype, -1);
1008        }
1009
1010        return err;
1011}
1012
1013static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1014{
1015        int modulation, streams, bitrate;
1016
1017        /* the formula below does only work for MCS values smaller than 32 */
1018        if (WARN_ON_ONCE(rate->mcs >= 32))
1019                return 0;
1020
1021        modulation = rate->mcs & 7;
1022        streams = (rate->mcs >> 3) + 1;
1023
1024        bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1025
1026        if (modulation < 4)
1027                bitrate *= (modulation + 1);
1028        else if (modulation == 4)
1029                bitrate *= (modulation + 2);
1030        else
1031                bitrate *= (modulation + 3);
1032
1033        bitrate *= streams;
1034
1035        if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1036                bitrate = (bitrate / 9) * 10;
1037
1038        /* do NOT round down here */
1039        return (bitrate + 50000) / 100000;
1040}
1041
1042static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1043{
1044        static const u32 __mcs2bitrate[] = {
1045                /* control PHY */
1046                [0] =   275,
1047                /* SC PHY */
1048                [1] =  3850,
1049                [2] =  7700,
1050                [3] =  9625,
1051                [4] = 11550,
1052                [5] = 12512, /* 1251.25 mbps */
1053                [6] = 15400,
1054                [7] = 19250,
1055                [8] = 23100,
1056                [9] = 25025,
1057                [10] = 30800,
1058                [11] = 38500,
1059                [12] = 46200,
1060                /* OFDM PHY */
1061                [13] =  6930,
1062                [14] =  8662, /* 866.25 mbps */
1063                [15] = 13860,
1064                [16] = 17325,
1065                [17] = 20790,
1066                [18] = 27720,
1067                [19] = 34650,
1068                [20] = 41580,
1069                [21] = 45045,
1070                [22] = 51975,
1071                [23] = 62370,
1072                [24] = 67568, /* 6756.75 mbps */
1073                /* LP-SC PHY */
1074                [25] =  6260,
1075                [26] =  8340,
1076                [27] = 11120,
1077                [28] = 12510,
1078                [29] = 16680,
1079                [30] = 22240,
1080                [31] = 25030,
1081        };
1082
1083        if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1084                return 0;
1085
1086        return __mcs2bitrate[rate->mcs];
1087}
1088
1089static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1090{
1091        static const u32 base[4][10] = {
1092                {   6500000,
1093                   13000000,
1094                   19500000,
1095                   26000000,
1096                   39000000,
1097                   52000000,
1098                   58500000,
1099                   65000000,
1100                   78000000,
1101                /* not in the spec, but some devices use this: */
1102                   86500000,
1103                },
1104                {  13500000,
1105                   27000000,
1106                   40500000,
1107                   54000000,
1108                   81000000,
1109                  108000000,
1110                  121500000,
1111                  135000000,
1112                  162000000,
1113                  180000000,
1114                },
1115                {  29300000,
1116                   58500000,
1117                   87800000,
1118                  117000000,
1119                  175500000,
1120                  234000000,
1121                  263300000,
1122                  292500000,
1123                  351000000,
1124                  390000000,
1125                },
1126                {  58500000,
1127                  117000000,
1128                  175500000,
1129                  234000000,
1130                  351000000,
1131                  468000000,
1132                  526500000,
1133                  585000000,
1134                  702000000,
1135                  780000000,
1136                },
1137        };
1138        u32 bitrate;
1139        int idx;
1140
1141        if (rate->mcs > 9)
1142                goto warn;
1143
1144        switch (rate->bw) {
1145        case RATE_INFO_BW_160:
1146                idx = 3;
1147                break;
1148        case RATE_INFO_BW_80:
1149                idx = 2;
1150                break;
1151        case RATE_INFO_BW_40:
1152                idx = 1;
1153                break;
1154        case RATE_INFO_BW_5:
1155        case RATE_INFO_BW_10:
1156        default:
1157                goto warn;
1158        case RATE_INFO_BW_20:
1159                idx = 0;
1160        }
1161
1162        bitrate = base[idx][rate->mcs];
1163        bitrate *= rate->nss;
1164
1165        if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1166                bitrate = (bitrate / 9) * 10;
1167
1168        /* do NOT round down here */
1169        return (bitrate + 50000) / 100000;
1170 warn:
1171        WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1172                  rate->bw, rate->mcs, rate->nss);
1173        return 0;
1174}
1175
1176static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1177{
1178#define SCALE 2048
1179        u16 mcs_divisors[12] = {
1180                34133, /* 16.666666... */
1181                17067, /*  8.333333... */
1182                11378, /*  5.555555... */
1183                 8533, /*  4.166666... */
1184                 5689, /*  2.777777... */
1185                 4267, /*  2.083333... */
1186                 3923, /*  1.851851... */
1187                 3413, /*  1.666666... */
1188                 2844, /*  1.388888... */
1189                 2560, /*  1.250000... */
1190                 2276, /*  1.111111... */
1191                 2048, /*  1.000000... */
1192        };
1193        u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1194        u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1195        u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1196        u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1197        u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1198        u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1199        u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1200        u64 tmp;
1201        u32 result;
1202
1203        if (WARN_ON_ONCE(rate->mcs > 11))
1204                return 0;
1205
1206        if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1207                return 0;
1208        if (WARN_ON_ONCE(rate->he_ru_alloc >
1209                         NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1210                return 0;
1211        if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1212                return 0;
1213
1214        if (rate->bw == RATE_INFO_BW_160)
1215                result = rates_160M[rate->he_gi];
1216        else if (rate->bw == RATE_INFO_BW_80 ||
1217                 (rate->bw == RATE_INFO_BW_HE_RU &&
1218                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1219                result = rates_969[rate->he_gi];
1220        else if (rate->bw == RATE_INFO_BW_40 ||
1221                 (rate->bw == RATE_INFO_BW_HE_RU &&
1222                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1223                result = rates_484[rate->he_gi];
1224        else if (rate->bw == RATE_INFO_BW_20 ||
1225                 (rate->bw == RATE_INFO_BW_HE_RU &&
1226                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1227                result = rates_242[rate->he_gi];
1228        else if (rate->bw == RATE_INFO_BW_HE_RU &&
1229                 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1230                result = rates_106[rate->he_gi];
1231        else if (rate->bw == RATE_INFO_BW_HE_RU &&
1232                 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1233                result = rates_52[rate->he_gi];
1234        else if (rate->bw == RATE_INFO_BW_HE_RU &&
1235                 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1236                result = rates_26[rate->he_gi];
1237        else {
1238                WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1239                     rate->bw, rate->he_ru_alloc);
1240                return 0;
1241        }
1242
1243        /* now scale to the appropriate MCS */
1244        tmp = result;
1245        tmp *= SCALE;
1246        do_div(tmp, mcs_divisors[rate->mcs]);
1247        result = tmp;
1248
1249        /* and take NSS, DCM into account */
1250        result = (result * rate->nss) / 8;
1251        if (rate->he_dcm)
1252                result /= 2;
1253
1254        return result / 10000;
1255}
1256
1257u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1258{
1259        if (rate->flags & RATE_INFO_FLAGS_MCS)
1260                return cfg80211_calculate_bitrate_ht(rate);
1261        if (rate->flags & RATE_INFO_FLAGS_60G)
1262                return cfg80211_calculate_bitrate_60g(rate);
1263        if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1264                return cfg80211_calculate_bitrate_vht(rate);
1265        if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1266                return cfg80211_calculate_bitrate_he(rate);
1267
1268        return rate->legacy;
1269}
1270EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1271
1272int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1273                          enum ieee80211_p2p_attr_id attr,
1274                          u8 *buf, unsigned int bufsize)
1275{
1276        u8 *out = buf;
1277        u16 attr_remaining = 0;
1278        bool desired_attr = false;
1279        u16 desired_len = 0;
1280
1281        while (len > 0) {
1282                unsigned int iedatalen;
1283                unsigned int copy;
1284                const u8 *iedata;
1285
1286                if (len < 2)
1287                        return -EILSEQ;
1288                iedatalen = ies[1];
1289                if (iedatalen + 2 > len)
1290                        return -EILSEQ;
1291
1292                if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1293                        goto cont;
1294
1295                if (iedatalen < 4)
1296                        goto cont;
1297
1298                iedata = ies + 2;
1299
1300                /* check WFA OUI, P2P subtype */
1301                if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1302                    iedata[2] != 0x9a || iedata[3] != 0x09)
1303                        goto cont;
1304
1305                iedatalen -= 4;
1306                iedata += 4;
1307
1308                /* check attribute continuation into this IE */
1309                copy = min_t(unsigned int, attr_remaining, iedatalen);
1310                if (copy && desired_attr) {
1311                        desired_len += copy;
1312                        if (out) {
1313                                memcpy(out, iedata, min(bufsize, copy));
1314                                out += min(bufsize, copy);
1315                                bufsize -= min(bufsize, copy);
1316                        }
1317
1318
1319                        if (copy == attr_remaining)
1320                                return desired_len;
1321                }
1322
1323                attr_remaining -= copy;
1324                if (attr_remaining)
1325                        goto cont;
1326
1327                iedatalen -= copy;
1328                iedata += copy;
1329
1330                while (iedatalen > 0) {
1331                        u16 attr_len;
1332
1333                        /* P2P attribute ID & size must fit */
1334                        if (iedatalen < 3)
1335                                return -EILSEQ;
1336                        desired_attr = iedata[0] == attr;
1337                        attr_len = get_unaligned_le16(iedata + 1);
1338                        iedatalen -= 3;
1339                        iedata += 3;
1340
1341                        copy = min_t(unsigned int, attr_len, iedatalen);
1342
1343                        if (desired_attr) {
1344                                desired_len += copy;
1345                                if (out) {
1346                                        memcpy(out, iedata, min(bufsize, copy));
1347                                        out += min(bufsize, copy);
1348                                        bufsize -= min(bufsize, copy);
1349                                }
1350
1351                                if (copy == attr_len)
1352                                        return desired_len;
1353                        }
1354
1355                        iedata += copy;
1356                        iedatalen -= copy;
1357                        attr_remaining = attr_len - copy;
1358                }
1359
1360 cont:
1361                len -= ies[1] + 2;
1362                ies += ies[1] + 2;
1363        }
1364
1365        if (attr_remaining && desired_attr)
1366                return -EILSEQ;
1367
1368        return -ENOENT;
1369}
1370EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1371
1372static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1373{
1374        int i;
1375
1376        /* Make sure array values are legal */
1377        if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1378                return false;
1379
1380        i = 0;
1381        while (i < n_ids) {
1382                if (ids[i] == WLAN_EID_EXTENSION) {
1383                        if (id_ext && (ids[i + 1] == id))
1384                                return true;
1385
1386                        i += 2;
1387                        continue;
1388                }
1389
1390                if (ids[i] == id && !id_ext)
1391                        return true;
1392
1393                i++;
1394        }
1395        return false;
1396}
1397
1398static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1399{
1400        /* we assume a validly formed IEs buffer */
1401        u8 len = ies[pos + 1];
1402
1403        pos += 2 + len;
1404
1405        /* the IE itself must have 255 bytes for fragments to follow */
1406        if (len < 255)
1407                return pos;
1408
1409        while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1410                len = ies[pos + 1];
1411                pos += 2 + len;
1412        }
1413
1414        return pos;
1415}
1416
1417size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1418                              const u8 *ids, int n_ids,
1419                              const u8 *after_ric, int n_after_ric,
1420                              size_t offset)
1421{
1422        size_t pos = offset;
1423
1424        while (pos < ielen) {
1425                u8 ext = 0;
1426
1427                if (ies[pos] == WLAN_EID_EXTENSION)
1428                        ext = 2;
1429                if ((pos + ext) >= ielen)
1430                        break;
1431
1432                if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1433                                          ies[pos] == WLAN_EID_EXTENSION))
1434                        break;
1435
1436                if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1437                        pos = skip_ie(ies, ielen, pos);
1438
1439                        while (pos < ielen) {
1440                                if (ies[pos] == WLAN_EID_EXTENSION)
1441                                        ext = 2;
1442                                else
1443                                        ext = 0;
1444
1445                                if ((pos + ext) >= ielen)
1446                                        break;
1447
1448                                if (!ieee80211_id_in_list(after_ric,
1449                                                          n_after_ric,
1450                                                          ies[pos + ext],
1451                                                          ext == 2))
1452                                        pos = skip_ie(ies, ielen, pos);
1453                                else
1454                                        break;
1455                        }
1456                } else {
1457                        pos = skip_ie(ies, ielen, pos);
1458                }
1459        }
1460
1461        return pos;
1462}
1463EXPORT_SYMBOL(ieee80211_ie_split_ric);
1464
1465bool ieee80211_operating_class_to_band(u8 operating_class,
1466                                       enum nl80211_band *band)
1467{
1468        switch (operating_class) {
1469        case 112:
1470        case 115 ... 127:
1471        case 128 ... 130:
1472                *band = NL80211_BAND_5GHZ;
1473                return true;
1474        case 81:
1475        case 82:
1476        case 83:
1477        case 84:
1478                *band = NL80211_BAND_2GHZ;
1479                return true;
1480        case 180:
1481                *band = NL80211_BAND_60GHZ;
1482                return true;
1483        }
1484
1485        return false;
1486}
1487EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1488
1489bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1490                                          u8 *op_class)
1491{
1492        u8 vht_opclass;
1493        u32 freq = chandef->center_freq1;
1494
1495        if (freq >= 2412 && freq <= 2472) {
1496                if (chandef->width > NL80211_CHAN_WIDTH_40)
1497                        return false;
1498
1499                /* 2.407 GHz, channels 1..13 */
1500                if (chandef->width == NL80211_CHAN_WIDTH_40) {
1501                        if (freq > chandef->chan->center_freq)
1502                                *op_class = 83; /* HT40+ */
1503                        else
1504                                *op_class = 84; /* HT40- */
1505                } else {
1506                        *op_class = 81;
1507                }
1508
1509                return true;
1510        }
1511
1512        if (freq == 2484) {
1513                if (chandef->width > NL80211_CHAN_WIDTH_40)
1514                        return false;
1515
1516                *op_class = 82; /* channel 14 */
1517                return true;
1518        }
1519
1520        switch (chandef->width) {
1521        case NL80211_CHAN_WIDTH_80:
1522                vht_opclass = 128;
1523                break;
1524        case NL80211_CHAN_WIDTH_160:
1525                vht_opclass = 129;
1526                break;
1527        case NL80211_CHAN_WIDTH_80P80:
1528                vht_opclass = 130;
1529                break;
1530        case NL80211_CHAN_WIDTH_10:
1531        case NL80211_CHAN_WIDTH_5:
1532                return false; /* unsupported for now */
1533        default:
1534                vht_opclass = 0;
1535                break;
1536        }
1537
1538        /* 5 GHz, channels 36..48 */
1539        if (freq >= 5180 && freq <= 5240) {
1540                if (vht_opclass) {
1541                        *op_class = vht_opclass;
1542                } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1543                        if (freq > chandef->chan->center_freq)
1544                                *op_class = 116;
1545                        else
1546                                *op_class = 117;
1547                } else {
1548                        *op_class = 115;
1549                }
1550
1551                return true;
1552        }
1553
1554        /* 5 GHz, channels 52..64 */
1555        if (freq >= 5260 && freq <= 5320) {
1556                if (vht_opclass) {
1557                        *op_class = vht_opclass;
1558                } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1559                        if (freq > chandef->chan->center_freq)
1560                                *op_class = 119;
1561                        else
1562                                *op_class = 120;
1563                } else {
1564                        *op_class = 118;
1565                }
1566
1567                return true;
1568        }
1569
1570        /* 5 GHz, channels 100..144 */
1571        if (freq >= 5500 && freq <= 5720) {
1572                if (vht_opclass) {
1573                        *op_class = vht_opclass;
1574                } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1575                        if (freq > chandef->chan->center_freq)
1576                                *op_class = 122;
1577                        else
1578                                *op_class = 123;
1579                } else {
1580                        *op_class = 121;
1581                }
1582
1583                return true;
1584        }
1585
1586        /* 5 GHz, channels 149..169 */
1587        if (freq >= 5745 && freq <= 5845) {
1588                if (vht_opclass) {
1589                        *op_class = vht_opclass;
1590                } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1591                        if (freq > chandef->chan->center_freq)
1592                                *op_class = 126;
1593                        else
1594                                *op_class = 127;
1595                } else if (freq <= 5805) {
1596                        *op_class = 124;
1597                } else {
1598                        *op_class = 125;
1599                }
1600
1601                return true;
1602        }
1603
1604        /* 56.16 GHz, channel 1..4 */
1605        if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1606                if (chandef->width >= NL80211_CHAN_WIDTH_40)
1607                        return false;
1608
1609                *op_class = 180;
1610                return true;
1611        }
1612
1613        /* not supported yet */
1614        return false;
1615}
1616EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1617
1618static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1619                                       u32 *beacon_int_gcd,
1620                                       bool *beacon_int_different)
1621{
1622        struct wireless_dev *wdev;
1623
1624        *beacon_int_gcd = 0;
1625        *beacon_int_different = false;
1626
1627        list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1628                if (!wdev->beacon_interval)
1629                        continue;
1630
1631                if (!*beacon_int_gcd) {
1632                        *beacon_int_gcd = wdev->beacon_interval;
1633                        continue;
1634                }
1635
1636                if (wdev->beacon_interval == *beacon_int_gcd)
1637                        continue;
1638
1639                *beacon_int_different = true;
1640                *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1641        }
1642
1643        if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1644                if (*beacon_int_gcd)
1645                        *beacon_int_different = true;
1646                *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1647        }
1648}
1649
1650int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1651                                 enum nl80211_iftype iftype, u32 beacon_int)
1652{
1653        /*
1654         * This is just a basic pre-condition check; if interface combinations
1655         * are possible the driver must already be checking those with a call
1656         * to cfg80211_check_combinations(), in which case we'll validate more
1657         * through the cfg80211_calculate_bi_data() call and code in
1658         * cfg80211_iter_combinations().
1659         */
1660
1661        if (beacon_int < 10 || beacon_int > 10000)
1662                return -EINVAL;
1663
1664        return 0;
1665}
1666
1667int cfg80211_iter_combinations(struct wiphy *wiphy,
1668                               struct iface_combination_params *params,
1669                               void (*iter)(const struct ieee80211_iface_combination *c,
1670                                            void *data),
1671                               void *data)
1672{
1673        const struct ieee80211_regdomain *regdom;
1674        enum nl80211_dfs_regions region = 0;
1675        int i, j, iftype;
1676        int num_interfaces = 0;
1677        u32 used_iftypes = 0;
1678        u32 beacon_int_gcd;
1679        bool beacon_int_different;
1680
1681        /*
1682         * This is a bit strange, since the iteration used to rely only on
1683         * the data given by the driver, but here it now relies on context,
1684         * in form of the currently operating interfaces.
1685         * This is OK for all current users, and saves us from having to
1686         * push the GCD calculations into all the drivers.
1687         * In the future, this should probably rely more on data that's in
1688         * cfg80211 already - the only thing not would appear to be any new
1689         * interfaces (while being brought up) and channel/radar data.
1690         */
1691        cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1692                                   &beacon_int_gcd, &beacon_int_different);
1693
1694        if (params->radar_detect) {
1695                rcu_read_lock();
1696                regdom = rcu_dereference(cfg80211_regdomain);
1697                if (regdom)
1698                        region = regdom->dfs_region;
1699                rcu_read_unlock();
1700        }
1701
1702        for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1703                num_interfaces += params->iftype_num[iftype];
1704                if (params->iftype_num[iftype] > 0 &&
1705                    !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1706                        used_iftypes |= BIT(iftype);
1707        }
1708
1709        for (i = 0; i < wiphy->n_iface_combinations; i++) {
1710                const struct ieee80211_iface_combination *c;
1711                struct ieee80211_iface_limit *limits;
1712                u32 all_iftypes = 0;
1713
1714                c = &wiphy->iface_combinations[i];
1715
1716                if (num_interfaces > c->max_interfaces)
1717                        continue;
1718                if (params->num_different_channels > c->num_different_channels)
1719                        continue;
1720
1721                limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1722                                 GFP_KERNEL);
1723                if (!limits)
1724                        return -ENOMEM;
1725
1726                for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1727                        if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1728                                continue;
1729                        for (j = 0; j < c->n_limits; j++) {
1730                                all_iftypes |= limits[j].types;
1731                                if (!(limits[j].types & BIT(iftype)))
1732                                        continue;
1733                                if (limits[j].max < params->iftype_num[iftype])
1734                                        goto cont;
1735                                limits[j].max -= params->iftype_num[iftype];
1736                        }
1737                }
1738
1739                if (params->radar_detect !=
1740                        (c->radar_detect_widths & params->radar_detect))
1741                        goto cont;
1742
1743                if (params->radar_detect && c->radar_detect_regions &&
1744                    !(c->radar_detect_regions & BIT(region)))
1745                        goto cont;
1746
1747                /* Finally check that all iftypes that we're currently
1748                 * using are actually part of this combination. If they
1749                 * aren't then we can't use this combination and have
1750                 * to continue to the next.
1751                 */
1752                if ((all_iftypes & used_iftypes) != used_iftypes)
1753                        goto cont;
1754
1755                if (beacon_int_gcd) {
1756                        if (c->beacon_int_min_gcd &&
1757                            beacon_int_gcd < c->beacon_int_min_gcd)
1758                                goto cont;
1759                        if (!c->beacon_int_min_gcd && beacon_int_different)
1760                                goto cont;
1761                }
1762
1763                /* This combination covered all interface types and
1764                 * supported the requested numbers, so we're good.
1765                 */
1766
1767                (*iter)(c, data);
1768 cont:
1769                kfree(limits);
1770        }
1771
1772        return 0;
1773}
1774EXPORT_SYMBOL(cfg80211_iter_combinations);
1775
1776static void
1777cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1778                          void *data)
1779{
1780        int *num = data;
1781        (*num)++;
1782}
1783
1784int cfg80211_check_combinations(struct wiphy *wiphy,
1785                                struct iface_combination_params *params)
1786{
1787        int err, num = 0;
1788
1789        err = cfg80211_iter_combinations(wiphy, params,
1790                                         cfg80211_iter_sum_ifcombs, &num);
1791        if (err)
1792                return err;
1793        if (num == 0)
1794                return -EBUSY;
1795
1796        return 0;
1797}
1798EXPORT_SYMBOL(cfg80211_check_combinations);
1799
1800int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1801                           const u8 *rates, unsigned int n_rates,
1802                           u32 *mask)
1803{
1804        int i, j;
1805
1806        if (!sband)
1807                return -EINVAL;
1808
1809        if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1810                return -EINVAL;
1811
1812        *mask = 0;
1813
1814        for (i = 0; i < n_rates; i++) {
1815                int rate = (rates[i] & 0x7f) * 5;
1816                bool found = false;
1817
1818                for (j = 0; j < sband->n_bitrates; j++) {
1819                        if (sband->bitrates[j].bitrate == rate) {
1820                                found = true;
1821                                *mask |= BIT(j);
1822                                break;
1823                        }
1824                }
1825                if (!found)
1826                        return -EINVAL;
1827        }
1828
1829        /*
1830         * mask must have at least one bit set here since we
1831         * didn't accept a 0-length rates array nor allowed
1832         * entries in the array that didn't exist
1833         */
1834
1835        return 0;
1836}
1837
1838unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1839{
1840        enum nl80211_band band;
1841        unsigned int n_channels = 0;
1842
1843        for (band = 0; band < NUM_NL80211_BANDS; band++)
1844                if (wiphy->bands[band])
1845                        n_channels += wiphy->bands[band]->n_channels;
1846
1847        return n_channels;
1848}
1849EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1850
1851int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1852                         struct station_info *sinfo)
1853{
1854        struct cfg80211_registered_device *rdev;
1855        struct wireless_dev *wdev;
1856
1857        wdev = dev->ieee80211_ptr;
1858        if (!wdev)
1859                return -EOPNOTSUPP;
1860
1861        rdev = wiphy_to_rdev(wdev->wiphy);
1862        if (!rdev->ops->get_station)
1863                return -EOPNOTSUPP;
1864
1865        memset(sinfo, 0, sizeof(*sinfo));
1866
1867        return rdev_get_station(rdev, dev, mac_addr, sinfo);
1868}
1869EXPORT_SYMBOL(cfg80211_get_station);
1870
1871void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1872{
1873        int i;
1874
1875        if (!f)
1876                return;
1877
1878        kfree(f->serv_spec_info);
1879        kfree(f->srf_bf);
1880        kfree(f->srf_macs);
1881        for (i = 0; i < f->num_rx_filters; i++)
1882                kfree(f->rx_filters[i].filter);
1883
1884        for (i = 0; i < f->num_tx_filters; i++)
1885                kfree(f->tx_filters[i].filter);
1886
1887        kfree(f->rx_filters);
1888        kfree(f->tx_filters);
1889        kfree(f);
1890}
1891EXPORT_SYMBOL(cfg80211_free_nan_func);
1892
1893bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1894                                u32 center_freq_khz, u32 bw_khz)
1895{
1896        u32 start_freq_khz, end_freq_khz;
1897
1898        start_freq_khz = center_freq_khz - (bw_khz / 2);
1899        end_freq_khz = center_freq_khz + (bw_khz / 2);
1900
1901        if (start_freq_khz >= freq_range->start_freq_khz &&
1902            end_freq_khz <= freq_range->end_freq_khz)
1903                return true;
1904
1905        return false;
1906}
1907
1908int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1909{
1910        sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1911                                sizeof(*(sinfo->pertid)),
1912                                gfp);
1913        if (!sinfo->pertid)
1914                return -ENOMEM;
1915
1916        return 0;
1917}
1918EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1919
1920/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1921/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1922const unsigned char rfc1042_header[] __aligned(2) =
1923        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1924EXPORT_SYMBOL(rfc1042_header);
1925
1926/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1927const unsigned char bridge_tunnel_header[] __aligned(2) =
1928        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1929EXPORT_SYMBOL(bridge_tunnel_header);
1930
1931/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1932struct iapp_layer2_update {
1933        u8 da[ETH_ALEN];        /* broadcast */
1934        u8 sa[ETH_ALEN];        /* STA addr */
1935        __be16 len;             /* 6 */
1936        u8 dsap;                /* 0 */
1937        u8 ssap;                /* 0 */
1938        u8 control;
1939        u8 xid_info[3];
1940} __packed;
1941
1942void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1943{
1944        struct iapp_layer2_update *msg;
1945        struct sk_buff *skb;
1946
1947        /* Send Level 2 Update Frame to update forwarding tables in layer 2
1948         * bridge devices */
1949
1950        skb = dev_alloc_skb(sizeof(*msg));
1951        if (!skb)
1952                return;
1953        msg = skb_put(skb, sizeof(*msg));
1954
1955        /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1956         * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1957
1958        eth_broadcast_addr(msg->da);
1959        ether_addr_copy(msg->sa, addr);
1960        msg->len = htons(6);
1961        msg->dsap = 0;
1962        msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
1963        msg->control = 0xaf;    /* XID response lsb.1111F101.
1964                                 * F=0 (no poll command; unsolicited frame) */
1965        msg->xid_info[0] = 0x81;        /* XID format identifier */
1966        msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
1967        msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
1968
1969        skb->dev = dev;
1970        skb->protocol = eth_type_trans(skb, dev);
1971        memset(skb->cb, 0, sizeof(skb->cb));
1972        netif_rx_ni(skb);
1973}
1974EXPORT_SYMBOL(cfg80211_send_layer2_update);
1975
1976int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1977                              enum ieee80211_vht_chanwidth bw,
1978                              int mcs, bool ext_nss_bw_capable)
1979{
1980        u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1981        int max_vht_nss = 0;
1982        int ext_nss_bw;
1983        int supp_width;
1984        int i, mcs_encoding;
1985
1986        if (map == 0xffff)
1987                return 0;
1988
1989        if (WARN_ON(mcs > 9))
1990                return 0;
1991        if (mcs <= 7)
1992                mcs_encoding = 0;
1993        else if (mcs == 8)
1994                mcs_encoding = 1;
1995        else
1996                mcs_encoding = 2;
1997
1998        /* find max_vht_nss for the given MCS */
1999        for (i = 7; i >= 0; i--) {
2000                int supp = (map >> (2 * i)) & 3;
2001
2002                if (supp == 3)
2003                        continue;
2004
2005                if (supp >= mcs_encoding) {
2006                        max_vht_nss = i + 1;
2007                        break;
2008                }
2009        }
2010
2011        if (!(cap->supp_mcs.tx_mcs_map &
2012                        cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2013                return max_vht_nss;
2014
2015        ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2016                                   IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2017        supp_width = le32_get_bits(cap->vht_cap_info,
2018                                   IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2019
2020        /* if not capable, treat ext_nss_bw as 0 */
2021        if (!ext_nss_bw_capable)
2022                ext_nss_bw = 0;
2023
2024        /* This is invalid */
2025        if (supp_width == 3)
2026                return 0;
2027
2028        /* This is an invalid combination so pretend nothing is supported */
2029        if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2030                return 0;
2031
2032        /*
2033         * Cover all the special cases according to IEEE 802.11-2016
2034         * Table 9-250. All other cases are either factor of 1 or not
2035         * valid/supported.
2036         */
2037        switch (bw) {
2038        case IEEE80211_VHT_CHANWIDTH_USE_HT:
2039        case IEEE80211_VHT_CHANWIDTH_80MHZ:
2040                if ((supp_width == 1 || supp_width == 2) &&
2041                    ext_nss_bw == 3)
2042                        return 2 * max_vht_nss;
2043                break;
2044        case IEEE80211_VHT_CHANWIDTH_160MHZ:
2045                if (supp_width == 0 &&
2046                    (ext_nss_bw == 1 || ext_nss_bw == 2))
2047                        return max_vht_nss / 2;
2048                if (supp_width == 0 &&
2049                    ext_nss_bw == 3)
2050                        return (3 * max_vht_nss) / 4;
2051                if (supp_width == 1 &&
2052                    ext_nss_bw == 3)
2053                        return 2 * max_vht_nss;
2054                break;
2055        case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2056                if (supp_width == 0 && ext_nss_bw == 1)
2057                        return 0; /* not possible */
2058                if (supp_width == 0 &&
2059                    ext_nss_bw == 2)
2060                        return max_vht_nss / 2;
2061                if (supp_width == 0 &&
2062                    ext_nss_bw == 3)
2063                        return (3 * max_vht_nss) / 4;
2064                if (supp_width == 1 &&
2065                    ext_nss_bw == 0)
2066                        return 0; /* not possible */
2067                if (supp_width == 1 &&
2068                    ext_nss_bw == 1)
2069                        return max_vht_nss / 2;
2070                if (supp_width == 1 &&
2071                    ext_nss_bw == 2)
2072                        return (3 * max_vht_nss) / 4;
2073                break;
2074        }
2075
2076        /* not covered or invalid combination received */
2077        return max_vht_nss;
2078}
2079EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2080
2081bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2082                             bool is_4addr, u8 check_swif)
2083
2084{
2085        bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2086
2087        switch (check_swif) {
2088        case 0:
2089                if (is_vlan && is_4addr)
2090                        return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2091                return wiphy->interface_modes & BIT(iftype);
2092        case 1:
2093                if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2094                        return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2095                return wiphy->software_iftypes & BIT(iftype);
2096        default:
2097                break;
2098        }
2099
2100        return false;
2101}
2102EXPORT_SYMBOL(cfg80211_iftype_allowed);
2103