linux/net/wireless/util.c
<<
>>
Prefs
   1/*
   2 * Wireless utility functions
   3 *
   4 * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
   5 */
   6#include <linux/bitops.h>
   7#include <linux/etherdevice.h>
   8#include <linux/slab.h>
   9#include <net/cfg80211.h>
  10#include <net/ip.h>
  11#include "core.h"
  12
  13struct ieee80211_rate *
  14ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  15                            u32 basic_rates, int bitrate)
  16{
  17        struct ieee80211_rate *result = &sband->bitrates[0];
  18        int i;
  19
  20        for (i = 0; i < sband->n_bitrates; i++) {
  21                if (!(basic_rates & BIT(i)))
  22                        continue;
  23                if (sband->bitrates[i].bitrate > bitrate)
  24                        continue;
  25                result = &sband->bitrates[i];
  26        }
  27
  28        return result;
  29}
  30EXPORT_SYMBOL(ieee80211_get_response_rate);
  31
  32int ieee80211_channel_to_frequency(int chan)
  33{
  34        if (chan < 14)
  35                return 2407 + chan * 5;
  36
  37        if (chan == 14)
  38                return 2484;
  39
  40        /* FIXME: 802.11j 17.3.8.3.2 */
  41        return (chan + 1000) * 5;
  42}
  43EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  44
  45int ieee80211_frequency_to_channel(int freq)
  46{
  47        if (freq == 2484)
  48                return 14;
  49
  50        if (freq < 2484)
  51                return (freq - 2407) / 5;
  52
  53        /* FIXME: 802.11j 17.3.8.3.2 */
  54        return freq/5 - 1000;
  55}
  56EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  57
  58struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
  59                                                  int freq)
  60{
  61        enum ieee80211_band band;
  62        struct ieee80211_supported_band *sband;
  63        int i;
  64
  65        for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  66                sband = wiphy->bands[band];
  67
  68                if (!sband)
  69                        continue;
  70
  71                for (i = 0; i < sband->n_channels; i++) {
  72                        if (sband->channels[i].center_freq == freq)
  73                                return &sband->channels[i];
  74                }
  75        }
  76
  77        return NULL;
  78}
  79EXPORT_SYMBOL(__ieee80211_get_channel);
  80
  81static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
  82                                     enum ieee80211_band band)
  83{
  84        int i, want;
  85
  86        switch (band) {
  87        case IEEE80211_BAND_5GHZ:
  88                want = 3;
  89                for (i = 0; i < sband->n_bitrates; i++) {
  90                        if (sband->bitrates[i].bitrate == 60 ||
  91                            sband->bitrates[i].bitrate == 120 ||
  92                            sband->bitrates[i].bitrate == 240) {
  93                                sband->bitrates[i].flags |=
  94                                        IEEE80211_RATE_MANDATORY_A;
  95                                want--;
  96                        }
  97                }
  98                WARN_ON(want);
  99                break;
 100        case IEEE80211_BAND_2GHZ:
 101                want = 7;
 102                for (i = 0; i < sband->n_bitrates; i++) {
 103                        if (sband->bitrates[i].bitrate == 10) {
 104                                sband->bitrates[i].flags |=
 105                                        IEEE80211_RATE_MANDATORY_B |
 106                                        IEEE80211_RATE_MANDATORY_G;
 107                                want--;
 108                        }
 109
 110                        if (sband->bitrates[i].bitrate == 20 ||
 111                            sband->bitrates[i].bitrate == 55 ||
 112                            sband->bitrates[i].bitrate == 110 ||
 113                            sband->bitrates[i].bitrate == 60 ||
 114                            sband->bitrates[i].bitrate == 120 ||
 115                            sband->bitrates[i].bitrate == 240) {
 116                                sband->bitrates[i].flags |=
 117                                        IEEE80211_RATE_MANDATORY_G;
 118                                want--;
 119                        }
 120
 121                        if (sband->bitrates[i].bitrate != 10 &&
 122                            sband->bitrates[i].bitrate != 20 &&
 123                            sband->bitrates[i].bitrate != 55 &&
 124                            sband->bitrates[i].bitrate != 110)
 125                                sband->bitrates[i].flags |=
 126                                        IEEE80211_RATE_ERP_G;
 127                }
 128                WARN_ON(want != 0 && want != 3 && want != 6);
 129                break;
 130        case IEEE80211_NUM_BANDS:
 131                WARN_ON(1);
 132                break;
 133        }
 134}
 135
 136void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
 137{
 138        enum ieee80211_band band;
 139
 140        for (band = 0; band < IEEE80211_NUM_BANDS; band++)
 141                if (wiphy->bands[band])
 142                        set_mandatory_flags_band(wiphy->bands[band], band);
 143}
 144
 145int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
 146                                   struct key_params *params, int key_idx,
 147                                   bool pairwise, const u8 *mac_addr)
 148{
 149        int i;
 150
 151        if (key_idx > 5)
 152                return -EINVAL;
 153
 154        if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
 155                return -EINVAL;
 156
 157        if (pairwise && !mac_addr)
 158                return -EINVAL;
 159
 160        /*
 161         * Disallow pairwise keys with non-zero index unless it's WEP
 162         * (because current deployments use pairwise WEP keys with
 163         * non-zero indizes but 802.11i clearly specifies to use zero)
 164         */
 165        if (pairwise && key_idx &&
 166            params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
 167            params->cipher != WLAN_CIPHER_SUITE_WEP104)
 168                return -EINVAL;
 169
 170        switch (params->cipher) {
 171        case WLAN_CIPHER_SUITE_WEP40:
 172                if (params->key_len != WLAN_KEY_LEN_WEP40)
 173                        return -EINVAL;
 174                break;
 175        case WLAN_CIPHER_SUITE_TKIP:
 176                if (params->key_len != WLAN_KEY_LEN_TKIP)
 177                        return -EINVAL;
 178                break;
 179        case WLAN_CIPHER_SUITE_CCMP:
 180                if (params->key_len != WLAN_KEY_LEN_CCMP)
 181                        return -EINVAL;
 182                break;
 183        case WLAN_CIPHER_SUITE_WEP104:
 184                if (params->key_len != WLAN_KEY_LEN_WEP104)
 185                        return -EINVAL;
 186                break;
 187        case WLAN_CIPHER_SUITE_AES_CMAC:
 188                if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
 189                        return -EINVAL;
 190                break;
 191        default:
 192                /*
 193                 * We don't know anything about this algorithm,
 194                 * allow using it -- but the driver must check
 195                 * all parameters! We still check below whether
 196                 * or not the driver supports this algorithm,
 197                 * of course.
 198                 */
 199                break;
 200        }
 201
 202        if (params->seq) {
 203                switch (params->cipher) {
 204                case WLAN_CIPHER_SUITE_WEP40:
 205                case WLAN_CIPHER_SUITE_WEP104:
 206                        /* These ciphers do not use key sequence */
 207                        return -EINVAL;
 208                case WLAN_CIPHER_SUITE_TKIP:
 209                case WLAN_CIPHER_SUITE_CCMP:
 210                case WLAN_CIPHER_SUITE_AES_CMAC:
 211                        if (params->seq_len != 6)
 212                                return -EINVAL;
 213                        break;
 214                }
 215        }
 216
 217        for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
 218                if (params->cipher == rdev->wiphy.cipher_suites[i])
 219                        break;
 220        if (i == rdev->wiphy.n_cipher_suites)
 221                return -EINVAL;
 222
 223        return 0;
 224}
 225
 226/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
 227/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
 228const unsigned char rfc1042_header[] __aligned(2) =
 229        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
 230EXPORT_SYMBOL(rfc1042_header);
 231
 232/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
 233const unsigned char bridge_tunnel_header[] __aligned(2) =
 234        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
 235EXPORT_SYMBOL(bridge_tunnel_header);
 236
 237unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
 238{
 239        unsigned int hdrlen = 24;
 240
 241        if (ieee80211_is_data(fc)) {
 242                if (ieee80211_has_a4(fc))
 243                        hdrlen = 30;
 244                if (ieee80211_is_data_qos(fc)) {
 245                        hdrlen += IEEE80211_QOS_CTL_LEN;
 246                        if (ieee80211_has_order(fc))
 247                                hdrlen += IEEE80211_HT_CTL_LEN;
 248                }
 249                goto out;
 250        }
 251
 252        if (ieee80211_is_ctl(fc)) {
 253                /*
 254                 * ACK and CTS are 10 bytes, all others 16. To see how
 255                 * to get this condition consider
 256                 *   subtype mask:   0b0000000011110000 (0x00F0)
 257                 *   ACK subtype:    0b0000000011010000 (0x00D0)
 258                 *   CTS subtype:    0b0000000011000000 (0x00C0)
 259                 *   bits that matter:         ^^^      (0x00E0)
 260                 *   value of those: 0b0000000011000000 (0x00C0)
 261                 */
 262                if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
 263                        hdrlen = 10;
 264                else
 265                        hdrlen = 16;
 266        }
 267out:
 268        return hdrlen;
 269}
 270EXPORT_SYMBOL(ieee80211_hdrlen);
 271
 272unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
 273{
 274        const struct ieee80211_hdr *hdr =
 275                        (const struct ieee80211_hdr *)skb->data;
 276        unsigned int hdrlen;
 277
 278        if (unlikely(skb->len < 10))
 279                return 0;
 280        hdrlen = ieee80211_hdrlen(hdr->frame_control);
 281        if (unlikely(hdrlen > skb->len))
 282                return 0;
 283        return hdrlen;
 284}
 285EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
 286
 287static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
 288{
 289        int ae = meshhdr->flags & MESH_FLAGS_AE;
 290        /* 7.1.3.5a.2 */
 291        switch (ae) {
 292        case 0:
 293                return 6;
 294        case MESH_FLAGS_AE_A4:
 295                return 12;
 296        case MESH_FLAGS_AE_A5_A6:
 297                return 18;
 298        case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
 299                return 24;
 300        default:
 301                return 6;
 302        }
 303}
 304
 305int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
 306                           enum nl80211_iftype iftype)
 307{
 308        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 309        u16 hdrlen, ethertype;
 310        u8 *payload;
 311        u8 dst[ETH_ALEN];
 312        u8 src[ETH_ALEN] __aligned(2);
 313
 314        if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
 315                return -1;
 316
 317        hdrlen = ieee80211_hdrlen(hdr->frame_control);
 318
 319        /* convert IEEE 802.11 header + possible LLC headers into Ethernet
 320         * header
 321         * IEEE 802.11 address fields:
 322         * ToDS FromDS Addr1 Addr2 Addr3 Addr4
 323         *   0     0   DA    SA    BSSID n/a
 324         *   0     1   DA    BSSID SA    n/a
 325         *   1     0   BSSID SA    DA    n/a
 326         *   1     1   RA    TA    DA    SA
 327         */
 328        memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
 329        memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
 330
 331        switch (hdr->frame_control &
 332                cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
 333        case cpu_to_le16(IEEE80211_FCTL_TODS):
 334                if (unlikely(iftype != NL80211_IFTYPE_AP &&
 335                             iftype != NL80211_IFTYPE_AP_VLAN &&
 336                             iftype != NL80211_IFTYPE_P2P_GO))
 337                        return -1;
 338                break;
 339        case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
 340                if (unlikely(iftype != NL80211_IFTYPE_WDS &&
 341                             iftype != NL80211_IFTYPE_MESH_POINT &&
 342                             iftype != NL80211_IFTYPE_AP_VLAN &&
 343                             iftype != NL80211_IFTYPE_STATION))
 344                        return -1;
 345                if (iftype == NL80211_IFTYPE_MESH_POINT) {
 346                        struct ieee80211s_hdr *meshdr =
 347                                (struct ieee80211s_hdr *) (skb->data + hdrlen);
 348                        /* make sure meshdr->flags is on the linear part */
 349                        if (!pskb_may_pull(skb, hdrlen + 1))
 350                                return -1;
 351                        if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
 352                                skb_copy_bits(skb, hdrlen +
 353                                        offsetof(struct ieee80211s_hdr, eaddr1),
 354                                        dst, ETH_ALEN);
 355                                skb_copy_bits(skb, hdrlen +
 356                                        offsetof(struct ieee80211s_hdr, eaddr2),
 357                                        src, ETH_ALEN);
 358                        }
 359                        hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
 360                }
 361                break;
 362        case cpu_to_le16(IEEE80211_FCTL_FROMDS):
 363                if ((iftype != NL80211_IFTYPE_STATION &&
 364                     iftype != NL80211_IFTYPE_P2P_CLIENT &&
 365                     iftype != NL80211_IFTYPE_MESH_POINT) ||
 366                    (is_multicast_ether_addr(dst) &&
 367                     !compare_ether_addr(src, addr)))
 368                        return -1;
 369                if (iftype == NL80211_IFTYPE_MESH_POINT) {
 370                        struct ieee80211s_hdr *meshdr =
 371                                (struct ieee80211s_hdr *) (skb->data + hdrlen);
 372                        /* make sure meshdr->flags is on the linear part */
 373                        if (!pskb_may_pull(skb, hdrlen + 1))
 374                                return -1;
 375                        if (meshdr->flags & MESH_FLAGS_AE_A4)
 376                                skb_copy_bits(skb, hdrlen +
 377                                        offsetof(struct ieee80211s_hdr, eaddr1),
 378                                        src, ETH_ALEN);
 379                        hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
 380                }
 381                break;
 382        case cpu_to_le16(0):
 383                if (iftype != NL80211_IFTYPE_ADHOC)
 384                        return -1;
 385                break;
 386        }
 387
 388        if (!pskb_may_pull(skb, hdrlen + 8))
 389                return -1;
 390
 391        payload = skb->data + hdrlen;
 392        ethertype = (payload[6] << 8) | payload[7];
 393
 394        if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
 395                    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 396                   compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
 397                /* remove RFC1042 or Bridge-Tunnel encapsulation and
 398                 * replace EtherType */
 399                skb_pull(skb, hdrlen + 6);
 400                memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
 401                memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
 402        } else {
 403                struct ethhdr *ehdr;
 404                __be16 len;
 405
 406                skb_pull(skb, hdrlen);
 407                len = htons(skb->len);
 408                ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
 409                memcpy(ehdr->h_dest, dst, ETH_ALEN);
 410                memcpy(ehdr->h_source, src, ETH_ALEN);
 411                ehdr->h_proto = len;
 412        }
 413        return 0;
 414}
 415EXPORT_SYMBOL(ieee80211_data_to_8023);
 416
 417int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
 418                             enum nl80211_iftype iftype, u8 *bssid, bool qos)
 419{
 420        struct ieee80211_hdr hdr;
 421        u16 hdrlen, ethertype;
 422        __le16 fc;
 423        const u8 *encaps_data;
 424        int encaps_len, skip_header_bytes;
 425        int nh_pos, h_pos;
 426        int head_need;
 427
 428        if (unlikely(skb->len < ETH_HLEN))
 429                return -EINVAL;
 430
 431        nh_pos = skb_network_header(skb) - skb->data;
 432        h_pos = skb_transport_header(skb) - skb->data;
 433
 434        /* convert Ethernet header to proper 802.11 header (based on
 435         * operation mode) */
 436        ethertype = (skb->data[12] << 8) | skb->data[13];
 437        fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
 438
 439        switch (iftype) {
 440        case NL80211_IFTYPE_AP:
 441        case NL80211_IFTYPE_AP_VLAN:
 442        case NL80211_IFTYPE_P2P_GO:
 443                fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
 444                /* DA BSSID SA */
 445                memcpy(hdr.addr1, skb->data, ETH_ALEN);
 446                memcpy(hdr.addr2, addr, ETH_ALEN);
 447                memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
 448                hdrlen = 24;
 449                break;
 450        case NL80211_IFTYPE_STATION:
 451        case NL80211_IFTYPE_P2P_CLIENT:
 452                fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
 453                /* BSSID SA DA */
 454                memcpy(hdr.addr1, bssid, ETH_ALEN);
 455                memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 456                memcpy(hdr.addr3, skb->data, ETH_ALEN);
 457                hdrlen = 24;
 458                break;
 459        case NL80211_IFTYPE_ADHOC:
 460                /* DA SA BSSID */
 461                memcpy(hdr.addr1, skb->data, ETH_ALEN);
 462                memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 463                memcpy(hdr.addr3, bssid, ETH_ALEN);
 464                hdrlen = 24;
 465                break;
 466        default:
 467                return -EOPNOTSUPP;
 468        }
 469
 470        if (qos) {
 471                fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
 472                hdrlen += 2;
 473        }
 474
 475        hdr.frame_control = fc;
 476        hdr.duration_id = 0;
 477        hdr.seq_ctrl = 0;
 478
 479        skip_header_bytes = ETH_HLEN;
 480        if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
 481                encaps_data = bridge_tunnel_header;
 482                encaps_len = sizeof(bridge_tunnel_header);
 483                skip_header_bytes -= 2;
 484        } else if (ethertype > 0x600) {
 485                encaps_data = rfc1042_header;
 486                encaps_len = sizeof(rfc1042_header);
 487                skip_header_bytes -= 2;
 488        } else {
 489                encaps_data = NULL;
 490                encaps_len = 0;
 491        }
 492
 493        skb_pull(skb, skip_header_bytes);
 494        nh_pos -= skip_header_bytes;
 495        h_pos -= skip_header_bytes;
 496
 497        head_need = hdrlen + encaps_len - skb_headroom(skb);
 498
 499        if (head_need > 0 || skb_cloned(skb)) {
 500                head_need = max(head_need, 0);
 501                if (head_need)
 502                        skb_orphan(skb);
 503
 504                if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
 505                        pr_err("failed to reallocate Tx buffer\n");
 506                        return -ENOMEM;
 507                }
 508                skb->truesize += head_need;
 509        }
 510
 511        if (encaps_data) {
 512                memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
 513                nh_pos += encaps_len;
 514                h_pos += encaps_len;
 515        }
 516
 517        memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
 518
 519        nh_pos += hdrlen;
 520        h_pos += hdrlen;
 521
 522        /* Update skb pointers to various headers since this modified frame
 523         * is going to go through Linux networking code that may potentially
 524         * need things like pointer to IP header. */
 525        skb_set_mac_header(skb, 0);
 526        skb_set_network_header(skb, nh_pos);
 527        skb_set_transport_header(skb, h_pos);
 528
 529        return 0;
 530}
 531EXPORT_SYMBOL(ieee80211_data_from_8023);
 532
 533
 534void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
 535                              const u8 *addr, enum nl80211_iftype iftype,
 536                              const unsigned int extra_headroom)
 537{
 538        struct sk_buff *frame = NULL;
 539        u16 ethertype;
 540        u8 *payload;
 541        const struct ethhdr *eth;
 542        int remaining, err;
 543        u8 dst[ETH_ALEN], src[ETH_ALEN];
 544
 545        err = ieee80211_data_to_8023(skb, addr, iftype);
 546        if (err)
 547                goto out;
 548
 549        /* skip the wrapping header */
 550        eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
 551        if (!eth)
 552                goto out;
 553
 554        while (skb != frame) {
 555                u8 padding;
 556                __be16 len = eth->h_proto;
 557                unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
 558
 559                remaining = skb->len;
 560                memcpy(dst, eth->h_dest, ETH_ALEN);
 561                memcpy(src, eth->h_source, ETH_ALEN);
 562
 563                padding = (4 - subframe_len) & 0x3;
 564                /* the last MSDU has no padding */
 565                if (subframe_len > remaining)
 566                        goto purge;
 567
 568                skb_pull(skb, sizeof(struct ethhdr));
 569                /* reuse skb for the last subframe */
 570                if (remaining <= subframe_len + padding)
 571                        frame = skb;
 572                else {
 573                        unsigned int hlen = ALIGN(extra_headroom, 4);
 574                        /*
 575                         * Allocate and reserve two bytes more for payload
 576                         * alignment since sizeof(struct ethhdr) is 14.
 577                         */
 578                        frame = dev_alloc_skb(hlen + subframe_len + 2);
 579                        if (!frame)
 580                                goto purge;
 581
 582                        skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
 583                        memcpy(skb_put(frame, ntohs(len)), skb->data,
 584                                ntohs(len));
 585
 586                        eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
 587                                                        padding);
 588                        if (!eth) {
 589                                dev_kfree_skb(frame);
 590                                goto purge;
 591                        }
 592                }
 593
 594                skb_reset_network_header(frame);
 595                frame->dev = skb->dev;
 596                frame->priority = skb->priority;
 597
 598                payload = frame->data;
 599                ethertype = (payload[6] << 8) | payload[7];
 600
 601                if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
 602                            ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 603                           compare_ether_addr(payload,
 604                                              bridge_tunnel_header) == 0)) {
 605                        /* remove RFC1042 or Bridge-Tunnel
 606                         * encapsulation and replace EtherType */
 607                        skb_pull(frame, 6);
 608                        memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
 609                        memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
 610                } else {
 611                        memcpy(skb_push(frame, sizeof(__be16)), &len,
 612                                sizeof(__be16));
 613                        memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
 614                        memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
 615                }
 616                __skb_queue_tail(list, frame);
 617        }
 618
 619        return;
 620
 621 purge:
 622        __skb_queue_purge(list);
 623 out:
 624        dev_kfree_skb(skb);
 625}
 626EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
 627
 628/* Given a data frame determine the 802.1p/1d tag to use. */
 629unsigned int cfg80211_classify8021d(struct sk_buff *skb)
 630{
 631        unsigned int dscp;
 632
 633        /* skb->priority values from 256->263 are magic values to
 634         * directly indicate a specific 802.1d priority.  This is used
 635         * to allow 802.1d priority to be passed directly in from VLAN
 636         * tags, etc.
 637         */
 638        if (skb->priority >= 256 && skb->priority <= 263)
 639                return skb->priority - 256;
 640
 641        switch (skb->protocol) {
 642        case htons(ETH_P_IP):
 643                dscp = ip_hdr(skb)->tos & 0xfc;
 644                break;
 645        default:
 646                return 0;
 647        }
 648
 649        return dscp >> 5;
 650}
 651EXPORT_SYMBOL(cfg80211_classify8021d);
 652
 653const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
 654{
 655        u8 *end, *pos;
 656
 657        pos = bss->information_elements;
 658        if (pos == NULL)
 659                return NULL;
 660        end = pos + bss->len_information_elements;
 661
 662        while (pos + 1 < end) {
 663                if (pos + 2 + pos[1] > end)
 664                        break;
 665                if (pos[0] == ie)
 666                        return pos;
 667                pos += 2 + pos[1];
 668        }
 669
 670        return NULL;
 671}
 672EXPORT_SYMBOL(ieee80211_bss_get_ie);
 673
 674void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
 675{
 676        struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 677        struct net_device *dev = wdev->netdev;
 678        int i;
 679
 680        if (!wdev->connect_keys)
 681                return;
 682
 683        for (i = 0; i < 6; i++) {
 684                if (!wdev->connect_keys->params[i].cipher)
 685                        continue;
 686                if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
 687                                        &wdev->connect_keys->params[i])) {
 688                        netdev_err(dev, "failed to set key %d\n", i);
 689                        continue;
 690                }
 691                if (wdev->connect_keys->def == i)
 692                        if (rdev->ops->set_default_key(wdev->wiphy, dev,
 693                                                       i, true, true)) {
 694                                netdev_err(dev, "failed to set defkey %d\n", i);
 695                                continue;
 696                        }
 697                if (wdev->connect_keys->defmgmt == i)
 698                        if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
 699                                netdev_err(dev, "failed to set mgtdef %d\n", i);
 700        }
 701
 702        kfree(wdev->connect_keys);
 703        wdev->connect_keys = NULL;
 704}
 705
 706static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
 707{
 708        struct cfg80211_event *ev;
 709        unsigned long flags;
 710        const u8 *bssid = NULL;
 711
 712        spin_lock_irqsave(&wdev->event_lock, flags);
 713        while (!list_empty(&wdev->event_list)) {
 714                ev = list_first_entry(&wdev->event_list,
 715                                      struct cfg80211_event, list);
 716                list_del(&ev->list);
 717                spin_unlock_irqrestore(&wdev->event_lock, flags);
 718
 719                wdev_lock(wdev);
 720                switch (ev->type) {
 721                case EVENT_CONNECT_RESULT:
 722                        if (!is_zero_ether_addr(ev->cr.bssid))
 723                                bssid = ev->cr.bssid;
 724                        __cfg80211_connect_result(
 725                                wdev->netdev, bssid,
 726                                ev->cr.req_ie, ev->cr.req_ie_len,
 727                                ev->cr.resp_ie, ev->cr.resp_ie_len,
 728                                ev->cr.status,
 729                                ev->cr.status == WLAN_STATUS_SUCCESS,
 730                                NULL);
 731                        break;
 732                case EVENT_ROAMED:
 733                        __cfg80211_roamed(wdev, ev->rm.bssid,
 734                                          ev->rm.req_ie, ev->rm.req_ie_len,
 735                                          ev->rm.resp_ie, ev->rm.resp_ie_len);
 736                        break;
 737                case EVENT_DISCONNECTED:
 738                        __cfg80211_disconnected(wdev->netdev,
 739                                                ev->dc.ie, ev->dc.ie_len,
 740                                                ev->dc.reason, true);
 741                        break;
 742                case EVENT_IBSS_JOINED:
 743                        __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
 744                        break;
 745                }
 746                wdev_unlock(wdev);
 747
 748                kfree(ev);
 749
 750                spin_lock_irqsave(&wdev->event_lock, flags);
 751        }
 752        spin_unlock_irqrestore(&wdev->event_lock, flags);
 753}
 754
 755void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
 756{
 757        struct wireless_dev *wdev;
 758
 759        ASSERT_RTNL();
 760        ASSERT_RDEV_LOCK(rdev);
 761
 762        mutex_lock(&rdev->devlist_mtx);
 763
 764        list_for_each_entry(wdev, &rdev->netdev_list, list)
 765                cfg80211_process_wdev_events(wdev);
 766
 767        mutex_unlock(&rdev->devlist_mtx);
 768}
 769
 770int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
 771                          struct net_device *dev, enum nl80211_iftype ntype,
 772                          u32 *flags, struct vif_params *params)
 773{
 774        int err;
 775        enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
 776
 777        ASSERT_RDEV_LOCK(rdev);
 778
 779        /* don't support changing VLANs, you just re-create them */
 780        if (otype == NL80211_IFTYPE_AP_VLAN)
 781                return -EOPNOTSUPP;
 782
 783        if (!rdev->ops->change_virtual_intf ||
 784            !(rdev->wiphy.interface_modes & (1 << ntype)))
 785                return -EOPNOTSUPP;
 786
 787        /* if it's part of a bridge, reject changing type to station/ibss */
 788        if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
 789            (ntype == NL80211_IFTYPE_ADHOC ||
 790             ntype == NL80211_IFTYPE_STATION ||
 791             ntype == NL80211_IFTYPE_P2P_CLIENT))
 792                return -EBUSY;
 793
 794        if (ntype != otype) {
 795                dev->ieee80211_ptr->use_4addr = false;
 796                dev->ieee80211_ptr->mesh_id_up_len = 0;
 797
 798                switch (otype) {
 799                case NL80211_IFTYPE_ADHOC:
 800                        cfg80211_leave_ibss(rdev, dev, false);
 801                        break;
 802                case NL80211_IFTYPE_STATION:
 803                case NL80211_IFTYPE_P2P_CLIENT:
 804                        cfg80211_disconnect(rdev, dev,
 805                                            WLAN_REASON_DEAUTH_LEAVING, true);
 806                        break;
 807                case NL80211_IFTYPE_MESH_POINT:
 808                        /* mesh should be handled? */
 809                        break;
 810                default:
 811                        break;
 812                }
 813
 814                cfg80211_process_rdev_events(rdev);
 815        }
 816
 817        err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
 818                                             ntype, flags, params);
 819
 820        WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
 821
 822        if (!err && params && params->use_4addr != -1)
 823                dev->ieee80211_ptr->use_4addr = params->use_4addr;
 824
 825        if (!err) {
 826                dev->priv_flags &= ~IFF_DONT_BRIDGE;
 827                switch (ntype) {
 828                case NL80211_IFTYPE_STATION:
 829                        if (dev->ieee80211_ptr->use_4addr)
 830                                break;
 831                        /* fall through */
 832                case NL80211_IFTYPE_P2P_CLIENT:
 833                case NL80211_IFTYPE_ADHOC:
 834                        dev->priv_flags |= IFF_DONT_BRIDGE;
 835                        break;
 836                case NL80211_IFTYPE_P2P_GO:
 837                case NL80211_IFTYPE_AP:
 838                case NL80211_IFTYPE_AP_VLAN:
 839                case NL80211_IFTYPE_WDS:
 840                case NL80211_IFTYPE_MESH_POINT:
 841                        /* bridging OK */
 842                        break;
 843                case NL80211_IFTYPE_MONITOR:
 844                        /* monitor can't bridge anyway */
 845                        break;
 846                case NL80211_IFTYPE_UNSPECIFIED:
 847                case NUM_NL80211_IFTYPES:
 848                        /* not happening */
 849                        break;
 850                }
 851        }
 852
 853        return err;
 854}
 855
 856u16 cfg80211_calculate_bitrate(struct rate_info *rate)
 857{
 858        int modulation, streams, bitrate;
 859
 860        if (!(rate->flags & RATE_INFO_FLAGS_MCS))
 861                return rate->legacy;
 862
 863        /* the formula below does only work for MCS values smaller than 32 */
 864        if (rate->mcs >= 32)
 865                return 0;
 866
 867        modulation = rate->mcs & 7;
 868        streams = (rate->mcs >> 3) + 1;
 869
 870        bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
 871                        13500000 : 6500000;
 872
 873        if (modulation < 4)
 874                bitrate *= (modulation + 1);
 875        else if (modulation == 4)
 876                bitrate *= (modulation + 2);
 877        else
 878                bitrate *= (modulation + 3);
 879
 880        bitrate *= streams;
 881
 882        if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
 883                bitrate = (bitrate / 9) * 10;
 884
 885        /* do NOT round down here */
 886        return (bitrate + 50000) / 100000;
 887}
 888