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/export.h>
   7#include <linux/bitops.h>
   8#include <linux/etherdevice.h>
   9#include <linux/slab.h>
  10#include <net/cfg80211.h>
  11#include <net/ip.h>
  12#include <net/dsfield.h>
  13#include "core.h"
  14
  15struct ieee80211_rate *
  16ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  17                            u32 basic_rates, int bitrate)
  18{
  19        struct ieee80211_rate *result = &sband->bitrates[0];
  20        int i;
  21
  22        for (i = 0; i < sband->n_bitrates; i++) {
  23                if (!(basic_rates & BIT(i)))
  24                        continue;
  25                if (sband->bitrates[i].bitrate > bitrate)
  26                        continue;
  27                result = &sband->bitrates[i];
  28        }
  29
  30        return result;
  31}
  32EXPORT_SYMBOL(ieee80211_get_response_rate);
  33
  34int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
  35{
  36        /* see 802.11 17.3.8.3.2 and Annex J
  37         * there are overlapping channel numbers in 5GHz and 2GHz bands */
  38        if (chan <= 0)
  39                return 0; /* not supported */
  40        switch (band) {
  41        case IEEE80211_BAND_2GHZ:
  42                if (chan == 14)
  43                        return 2484;
  44                else if (chan < 14)
  45                        return 2407 + chan * 5;
  46                break;
  47        case IEEE80211_BAND_5GHZ:
  48                if (chan >= 182 && chan <= 196)
  49                        return 4000 + chan * 5;
  50                else
  51                        return 5000 + chan * 5;
  52                break;
  53        case IEEE80211_BAND_60GHZ:
  54                if (chan < 5)
  55                        return 56160 + chan * 2160;
  56                break;
  57        default:
  58                ;
  59        }
  60        return 0; /* not supported */
  61}
  62EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  63
  64int ieee80211_frequency_to_channel(int freq)
  65{
  66        /* see 802.11 17.3.8.3.2 and Annex J */
  67        if (freq == 2484)
  68                return 14;
  69        else if (freq < 2484)
  70                return (freq - 2407) / 5;
  71        else if (freq >= 4910 && freq <= 4980)
  72                return (freq - 4000) / 5;
  73        else if (freq <= 45000) /* DMG band lower limit */
  74                return (freq - 5000) / 5;
  75        else if (freq >= 58320 && freq <= 64800)
  76                return (freq - 56160) / 2160;
  77        else
  78                return 0;
  79}
  80EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  81
  82struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
  83                                                  int freq)
  84{
  85        enum ieee80211_band band;
  86        struct ieee80211_supported_band *sband;
  87        int i;
  88
  89        for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  90                sband = wiphy->bands[band];
  91
  92                if (!sband)
  93                        continue;
  94
  95                for (i = 0; i < sband->n_channels; i++) {
  96                        if (sband->channels[i].center_freq == freq)
  97                                return &sband->channels[i];
  98                }
  99        }
 100
 101        return NULL;
 102}
 103EXPORT_SYMBOL(__ieee80211_get_channel);
 104
 105static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
 106                                     enum ieee80211_band band)
 107{
 108        int i, want;
 109
 110        switch (band) {
 111        case IEEE80211_BAND_5GHZ:
 112                want = 3;
 113                for (i = 0; i < sband->n_bitrates; i++) {
 114                        if (sband->bitrates[i].bitrate == 60 ||
 115                            sband->bitrates[i].bitrate == 120 ||
 116                            sband->bitrates[i].bitrate == 240) {
 117                                sband->bitrates[i].flags |=
 118                                        IEEE80211_RATE_MANDATORY_A;
 119                                want--;
 120                        }
 121                }
 122                WARN_ON(want);
 123                break;
 124        case IEEE80211_BAND_2GHZ:
 125                want = 7;
 126                for (i = 0; i < sband->n_bitrates; i++) {
 127                        if (sband->bitrates[i].bitrate == 10) {
 128                                sband->bitrates[i].flags |=
 129                                        IEEE80211_RATE_MANDATORY_B |
 130                                        IEEE80211_RATE_MANDATORY_G;
 131                                want--;
 132                        }
 133
 134                        if (sband->bitrates[i].bitrate == 20 ||
 135                            sband->bitrates[i].bitrate == 55 ||
 136                            sband->bitrates[i].bitrate == 110 ||
 137                            sband->bitrates[i].bitrate == 60 ||
 138                            sband->bitrates[i].bitrate == 120 ||
 139                            sband->bitrates[i].bitrate == 240) {
 140                                sband->bitrates[i].flags |=
 141                                        IEEE80211_RATE_MANDATORY_G;
 142                                want--;
 143                        }
 144
 145                        if (sband->bitrates[i].bitrate != 10 &&
 146                            sband->bitrates[i].bitrate != 20 &&
 147                            sband->bitrates[i].bitrate != 55 &&
 148                            sband->bitrates[i].bitrate != 110)
 149                                sband->bitrates[i].flags |=
 150                                        IEEE80211_RATE_ERP_G;
 151                }
 152                WARN_ON(want != 0 && want != 3 && want != 6);
 153                break;
 154        case IEEE80211_BAND_60GHZ:
 155                /* check for mandatory HT MCS 1..4 */
 156                WARN_ON(!sband->ht_cap.ht_supported);
 157                WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
 158                break;
 159        case IEEE80211_NUM_BANDS:
 160                WARN_ON(1);
 161                break;
 162        }
 163}
 164
 165void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
 166{
 167        enum ieee80211_band band;
 168
 169        for (band = 0; band < IEEE80211_NUM_BANDS; band++)
 170                if (wiphy->bands[band])
 171                        set_mandatory_flags_band(wiphy->bands[band], band);
 172}
 173
 174bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
 175{
 176        int i;
 177        for (i = 0; i < wiphy->n_cipher_suites; i++)
 178                if (cipher == wiphy->cipher_suites[i])
 179                        return true;
 180        return false;
 181}
 182
 183int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
 184                                   struct key_params *params, int key_idx,
 185                                   bool pairwise, const u8 *mac_addr)
 186{
 187        if (key_idx > 5)
 188                return -EINVAL;
 189
 190        if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
 191                return -EINVAL;
 192
 193        if (pairwise && !mac_addr)
 194                return -EINVAL;
 195
 196        /*
 197         * Disallow pairwise keys with non-zero index unless it's WEP
 198         * or a vendor specific cipher (because current deployments use
 199         * pairwise WEP keys with non-zero indices and for vendor specific
 200         * ciphers this should be validated in the driver or hardware level
 201         * - but 802.11i clearly specifies to use zero)
 202         */
 203        if (pairwise && key_idx &&
 204            ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
 205             (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
 206             (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
 207                return -EINVAL;
 208
 209        switch (params->cipher) {
 210        case WLAN_CIPHER_SUITE_WEP40:
 211                if (params->key_len != WLAN_KEY_LEN_WEP40)
 212                        return -EINVAL;
 213                break;
 214        case WLAN_CIPHER_SUITE_TKIP:
 215                if (params->key_len != WLAN_KEY_LEN_TKIP)
 216                        return -EINVAL;
 217                break;
 218        case WLAN_CIPHER_SUITE_CCMP:
 219                if (params->key_len != WLAN_KEY_LEN_CCMP)
 220                        return -EINVAL;
 221                break;
 222        case WLAN_CIPHER_SUITE_WEP104:
 223                if (params->key_len != WLAN_KEY_LEN_WEP104)
 224                        return -EINVAL;
 225                break;
 226        case WLAN_CIPHER_SUITE_AES_CMAC:
 227                if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
 228                        return -EINVAL;
 229                break;
 230        default:
 231                /*
 232                 * We don't know anything about this algorithm,
 233                 * allow using it -- but the driver must check
 234                 * all parameters! We still check below whether
 235                 * or not the driver supports this algorithm,
 236                 * of course.
 237                 */
 238                break;
 239        }
 240
 241        if (params->seq) {
 242                switch (params->cipher) {
 243                case WLAN_CIPHER_SUITE_WEP40:
 244                case WLAN_CIPHER_SUITE_WEP104:
 245                        /* These ciphers do not use key sequence */
 246                        return -EINVAL;
 247                case WLAN_CIPHER_SUITE_TKIP:
 248                case WLAN_CIPHER_SUITE_CCMP:
 249                case WLAN_CIPHER_SUITE_AES_CMAC:
 250                        if (params->seq_len != 6)
 251                                return -EINVAL;
 252                        break;
 253                }
 254        }
 255
 256        if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
 257                return -EINVAL;
 258
 259        return 0;
 260}
 261
 262unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
 263{
 264        unsigned int hdrlen = 24;
 265
 266        if (ieee80211_is_data(fc)) {
 267                if (ieee80211_has_a4(fc))
 268                        hdrlen = 30;
 269                if (ieee80211_is_data_qos(fc)) {
 270                        hdrlen += IEEE80211_QOS_CTL_LEN;
 271                        if (ieee80211_has_order(fc))
 272                                hdrlen += IEEE80211_HT_CTL_LEN;
 273                }
 274                goto out;
 275        }
 276
 277        if (ieee80211_is_ctl(fc)) {
 278                /*
 279                 * ACK and CTS are 10 bytes, all others 16. To see how
 280                 * to get this condition consider
 281                 *   subtype mask:   0b0000000011110000 (0x00F0)
 282                 *   ACK subtype:    0b0000000011010000 (0x00D0)
 283                 *   CTS subtype:    0b0000000011000000 (0x00C0)
 284                 *   bits that matter:         ^^^      (0x00E0)
 285                 *   value of those: 0b0000000011000000 (0x00C0)
 286                 */
 287                if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
 288                        hdrlen = 10;
 289                else
 290                        hdrlen = 16;
 291        }
 292out:
 293        return hdrlen;
 294}
 295EXPORT_SYMBOL(ieee80211_hdrlen);
 296
 297unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
 298{
 299        const struct ieee80211_hdr *hdr =
 300                        (const struct ieee80211_hdr *)skb->data;
 301        unsigned int hdrlen;
 302
 303        if (unlikely(skb->len < 10))
 304                return 0;
 305        hdrlen = ieee80211_hdrlen(hdr->frame_control);
 306        if (unlikely(hdrlen > skb->len))
 307                return 0;
 308        return hdrlen;
 309}
 310EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
 311
 312unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
 313{
 314        int ae = meshhdr->flags & MESH_FLAGS_AE;
 315        /* 802.11-2012, 8.2.4.7.3 */
 316        switch (ae) {
 317        default:
 318        case 0:
 319                return 6;
 320        case MESH_FLAGS_AE_A4:
 321                return 12;
 322        case MESH_FLAGS_AE_A5_A6:
 323                return 18;
 324        }
 325}
 326EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
 327
 328int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
 329                           enum nl80211_iftype iftype)
 330{
 331        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 332        u16 hdrlen, ethertype;
 333        u8 *payload;
 334        u8 dst[ETH_ALEN];
 335        u8 src[ETH_ALEN] __aligned(2);
 336
 337        if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
 338                return -1;
 339
 340        hdrlen = ieee80211_hdrlen(hdr->frame_control);
 341
 342        /* convert IEEE 802.11 header + possible LLC headers into Ethernet
 343         * header
 344         * IEEE 802.11 address fields:
 345         * ToDS FromDS Addr1 Addr2 Addr3 Addr4
 346         *   0     0   DA    SA    BSSID n/a
 347         *   0     1   DA    BSSID SA    n/a
 348         *   1     0   BSSID SA    DA    n/a
 349         *   1     1   RA    TA    DA    SA
 350         */
 351        memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
 352        memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
 353
 354        switch (hdr->frame_control &
 355                cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
 356        case cpu_to_le16(IEEE80211_FCTL_TODS):
 357                if (unlikely(iftype != NL80211_IFTYPE_AP &&
 358                             iftype != NL80211_IFTYPE_AP_VLAN &&
 359                             iftype != NL80211_IFTYPE_P2P_GO))
 360                        return -1;
 361                break;
 362        case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
 363                if (unlikely(iftype != NL80211_IFTYPE_WDS &&
 364                             iftype != NL80211_IFTYPE_MESH_POINT &&
 365                             iftype != NL80211_IFTYPE_AP_VLAN &&
 366                             iftype != NL80211_IFTYPE_STATION))
 367                        return -1;
 368                if (iftype == NL80211_IFTYPE_MESH_POINT) {
 369                        struct ieee80211s_hdr *meshdr =
 370                                (struct ieee80211s_hdr *) (skb->data + hdrlen);
 371                        /* make sure meshdr->flags is on the linear part */
 372                        if (!pskb_may_pull(skb, hdrlen + 1))
 373                                return -1;
 374                        if (meshdr->flags & MESH_FLAGS_AE_A4)
 375                                return -1;
 376                        if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
 377                                skb_copy_bits(skb, hdrlen +
 378                                        offsetof(struct ieee80211s_hdr, eaddr1),
 379                                        dst, ETH_ALEN);
 380                                skb_copy_bits(skb, hdrlen +
 381                                        offsetof(struct ieee80211s_hdr, eaddr2),
 382                                        src, ETH_ALEN);
 383                        }
 384                        hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
 385                }
 386                break;
 387        case cpu_to_le16(IEEE80211_FCTL_FROMDS):
 388                if ((iftype != NL80211_IFTYPE_STATION &&
 389                     iftype != NL80211_IFTYPE_P2P_CLIENT &&
 390                     iftype != NL80211_IFTYPE_MESH_POINT) ||
 391                    (is_multicast_ether_addr(dst) &&
 392                     ether_addr_equal(src, addr)))
 393                        return -1;
 394                if (iftype == NL80211_IFTYPE_MESH_POINT) {
 395                        struct ieee80211s_hdr *meshdr =
 396                                (struct ieee80211s_hdr *) (skb->data + hdrlen);
 397                        /* make sure meshdr->flags is on the linear part */
 398                        if (!pskb_may_pull(skb, hdrlen + 1))
 399                                return -1;
 400                        if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
 401                                return -1;
 402                        if (meshdr->flags & MESH_FLAGS_AE_A4)
 403                                skb_copy_bits(skb, hdrlen +
 404                                        offsetof(struct ieee80211s_hdr, eaddr1),
 405                                        src, ETH_ALEN);
 406                        hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
 407                }
 408                break;
 409        case cpu_to_le16(0):
 410                if (iftype != NL80211_IFTYPE_ADHOC &&
 411                    iftype != NL80211_IFTYPE_STATION)
 412                                return -1;
 413                break;
 414        }
 415
 416        if (!pskb_may_pull(skb, hdrlen + 8))
 417                return -1;
 418
 419        payload = skb->data + hdrlen;
 420        ethertype = (payload[6] << 8) | payload[7];
 421
 422        if (likely((ether_addr_equal(payload, rfc1042_header) &&
 423                    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 424                   ether_addr_equal(payload, bridge_tunnel_header))) {
 425                /* remove RFC1042 or Bridge-Tunnel encapsulation and
 426                 * replace EtherType */
 427                skb_pull(skb, hdrlen + 6);
 428                memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
 429                memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
 430        } else {
 431                struct ethhdr *ehdr;
 432                __be16 len;
 433
 434                skb_pull(skb, hdrlen);
 435                len = htons(skb->len);
 436                ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
 437                memcpy(ehdr->h_dest, dst, ETH_ALEN);
 438                memcpy(ehdr->h_source, src, ETH_ALEN);
 439                ehdr->h_proto = len;
 440        }
 441        return 0;
 442}
 443EXPORT_SYMBOL(ieee80211_data_to_8023);
 444
 445int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
 446                             enum nl80211_iftype iftype, u8 *bssid, bool qos)
 447{
 448        struct ieee80211_hdr hdr;
 449        u16 hdrlen, ethertype;
 450        __le16 fc;
 451        const u8 *encaps_data;
 452        int encaps_len, skip_header_bytes;
 453        int nh_pos, h_pos;
 454        int head_need;
 455
 456        if (unlikely(skb->len < ETH_HLEN))
 457                return -EINVAL;
 458
 459        nh_pos = skb_network_header(skb) - skb->data;
 460        h_pos = skb_transport_header(skb) - skb->data;
 461
 462        /* convert Ethernet header to proper 802.11 header (based on
 463         * operation mode) */
 464        ethertype = (skb->data[12] << 8) | skb->data[13];
 465        fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
 466
 467        switch (iftype) {
 468        case NL80211_IFTYPE_AP:
 469        case NL80211_IFTYPE_AP_VLAN:
 470        case NL80211_IFTYPE_P2P_GO:
 471                fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
 472                /* DA BSSID SA */
 473                memcpy(hdr.addr1, skb->data, ETH_ALEN);
 474                memcpy(hdr.addr2, addr, ETH_ALEN);
 475                memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
 476                hdrlen = 24;
 477                break;
 478        case NL80211_IFTYPE_STATION:
 479        case NL80211_IFTYPE_P2P_CLIENT:
 480                fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
 481                /* BSSID SA DA */
 482                memcpy(hdr.addr1, bssid, ETH_ALEN);
 483                memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 484                memcpy(hdr.addr3, skb->data, ETH_ALEN);
 485                hdrlen = 24;
 486                break;
 487        case NL80211_IFTYPE_ADHOC:
 488                /* DA SA BSSID */
 489                memcpy(hdr.addr1, skb->data, ETH_ALEN);
 490                memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 491                memcpy(hdr.addr3, bssid, ETH_ALEN);
 492                hdrlen = 24;
 493                break;
 494        default:
 495                return -EOPNOTSUPP;
 496        }
 497
 498        if (qos) {
 499                fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
 500                hdrlen += 2;
 501        }
 502
 503        hdr.frame_control = fc;
 504        hdr.duration_id = 0;
 505        hdr.seq_ctrl = 0;
 506
 507        skip_header_bytes = ETH_HLEN;
 508        if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
 509                encaps_data = bridge_tunnel_header;
 510                encaps_len = sizeof(bridge_tunnel_header);
 511                skip_header_bytes -= 2;
 512        } else if (ethertype > 0x600) {
 513                encaps_data = rfc1042_header;
 514                encaps_len = sizeof(rfc1042_header);
 515                skip_header_bytes -= 2;
 516        } else {
 517                encaps_data = NULL;
 518                encaps_len = 0;
 519        }
 520
 521        skb_pull(skb, skip_header_bytes);
 522        nh_pos -= skip_header_bytes;
 523        h_pos -= skip_header_bytes;
 524
 525        head_need = hdrlen + encaps_len - skb_headroom(skb);
 526
 527        if (head_need > 0 || skb_cloned(skb)) {
 528                head_need = max(head_need, 0);
 529                if (head_need)
 530                        skb_orphan(skb);
 531
 532                if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
 533                        return -ENOMEM;
 534
 535                skb->truesize += head_need;
 536        }
 537
 538        if (encaps_data) {
 539                memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
 540                nh_pos += encaps_len;
 541                h_pos += encaps_len;
 542        }
 543
 544        memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
 545
 546        nh_pos += hdrlen;
 547        h_pos += hdrlen;
 548
 549        /* Update skb pointers to various headers since this modified frame
 550         * is going to go through Linux networking code that may potentially
 551         * need things like pointer to IP header. */
 552        skb_set_mac_header(skb, 0);
 553        skb_set_network_header(skb, nh_pos);
 554        skb_set_transport_header(skb, h_pos);
 555
 556        return 0;
 557}
 558EXPORT_SYMBOL(ieee80211_data_from_8023);
 559
 560
 561void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
 562                              const u8 *addr, enum nl80211_iftype iftype,
 563                              const unsigned int extra_headroom,
 564                              bool has_80211_header)
 565{
 566        struct sk_buff *frame = NULL;
 567        u16 ethertype;
 568        u8 *payload;
 569        const struct ethhdr *eth;
 570        int remaining, err;
 571        u8 dst[ETH_ALEN], src[ETH_ALEN];
 572
 573        if (has_80211_header) {
 574                err = ieee80211_data_to_8023(skb, addr, iftype);
 575                if (err)
 576                        goto out;
 577
 578                /* skip the wrapping header */
 579                eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
 580                if (!eth)
 581                        goto out;
 582        } else {
 583                eth = (struct ethhdr *) skb->data;
 584        }
 585
 586        while (skb != frame) {
 587                u8 padding;
 588                __be16 len = eth->h_proto;
 589                unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
 590
 591                remaining = skb->len;
 592                memcpy(dst, eth->h_dest, ETH_ALEN);
 593                memcpy(src, eth->h_source, ETH_ALEN);
 594
 595                padding = (4 - subframe_len) & 0x3;
 596                /* the last MSDU has no padding */
 597                if (subframe_len > remaining)
 598                        goto purge;
 599
 600                skb_pull(skb, sizeof(struct ethhdr));
 601                /* reuse skb for the last subframe */
 602                if (remaining <= subframe_len + padding)
 603                        frame = skb;
 604                else {
 605                        unsigned int hlen = ALIGN(extra_headroom, 4);
 606                        /*
 607                         * Allocate and reserve two bytes more for payload
 608                         * alignment since sizeof(struct ethhdr) is 14.
 609                         */
 610                        frame = dev_alloc_skb(hlen + subframe_len + 2);
 611                        if (!frame)
 612                                goto purge;
 613
 614                        skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
 615                        memcpy(skb_put(frame, ntohs(len)), skb->data,
 616                                ntohs(len));
 617
 618                        eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
 619                                                        padding);
 620                        if (!eth) {
 621                                dev_kfree_skb(frame);
 622                                goto purge;
 623                        }
 624                }
 625
 626                skb_reset_network_header(frame);
 627                frame->dev = skb->dev;
 628                frame->priority = skb->priority;
 629
 630                payload = frame->data;
 631                ethertype = (payload[6] << 8) | payload[7];
 632
 633                if (likely((ether_addr_equal(payload, rfc1042_header) &&
 634                            ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 635                           ether_addr_equal(payload, bridge_tunnel_header))) {
 636                        /* remove RFC1042 or Bridge-Tunnel
 637                         * encapsulation and replace EtherType */
 638                        skb_pull(frame, 6);
 639                        memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
 640                        memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
 641                } else {
 642                        memcpy(skb_push(frame, sizeof(__be16)), &len,
 643                                sizeof(__be16));
 644                        memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
 645                        memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
 646                }
 647                __skb_queue_tail(list, frame);
 648        }
 649
 650        return;
 651
 652 purge:
 653        __skb_queue_purge(list);
 654 out:
 655        dev_kfree_skb(skb);
 656}
 657EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
 658
 659/* Given a data frame determine the 802.1p/1d tag to use. */
 660unsigned int cfg80211_classify8021d(struct sk_buff *skb)
 661{
 662        unsigned int dscp;
 663
 664        /* skb->priority values from 256->263 are magic values to
 665         * directly indicate a specific 802.1d priority.  This is used
 666         * to allow 802.1d priority to be passed directly in from VLAN
 667         * tags, etc.
 668         */
 669        if (skb->priority >= 256 && skb->priority <= 263)
 670                return skb->priority - 256;
 671
 672        switch (skb->protocol) {
 673        case htons(ETH_P_IP):
 674                dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
 675                break;
 676        case htons(ETH_P_IPV6):
 677                dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
 678                break;
 679        default:
 680                return 0;
 681        }
 682
 683        return dscp >> 5;
 684}
 685EXPORT_SYMBOL(cfg80211_classify8021d);
 686
 687const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
 688{
 689        u8 *end, *pos;
 690
 691        pos = bss->information_elements;
 692        if (pos == NULL)
 693                return NULL;
 694        end = pos + bss->len_information_elements;
 695
 696        while (pos + 1 < end) {
 697                if (pos + 2 + pos[1] > end)
 698                        break;
 699                if (pos[0] == ie)
 700                        return pos;
 701                pos += 2 + pos[1];
 702        }
 703
 704        return NULL;
 705}
 706EXPORT_SYMBOL(ieee80211_bss_get_ie);
 707
 708void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
 709{
 710        struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 711        struct net_device *dev = wdev->netdev;
 712        int i;
 713
 714        if (!wdev->connect_keys)
 715                return;
 716
 717        for (i = 0; i < 6; i++) {
 718                if (!wdev->connect_keys->params[i].cipher)
 719                        continue;
 720                if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
 721                                        &wdev->connect_keys->params[i])) {
 722                        netdev_err(dev, "failed to set key %d\n", i);
 723                        continue;
 724                }
 725                if (wdev->connect_keys->def == i)
 726                        if (rdev->ops->set_default_key(wdev->wiphy, dev,
 727                                                       i, true, true)) {
 728                                netdev_err(dev, "failed to set defkey %d\n", i);
 729                                continue;
 730                        }
 731                if (wdev->connect_keys->defmgmt == i)
 732                        if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
 733                                netdev_err(dev, "failed to set mgtdef %d\n", i);
 734        }
 735
 736        kfree(wdev->connect_keys);
 737        wdev->connect_keys = NULL;
 738}
 739
 740void cfg80211_process_wdev_events(struct wireless_dev *wdev)
 741{
 742        struct cfg80211_event *ev;
 743        unsigned long flags;
 744        const u8 *bssid = NULL;
 745
 746        spin_lock_irqsave(&wdev->event_lock, flags);
 747        while (!list_empty(&wdev->event_list)) {
 748                ev = list_first_entry(&wdev->event_list,
 749                                      struct cfg80211_event, list);
 750                list_del(&ev->list);
 751                spin_unlock_irqrestore(&wdev->event_lock, flags);
 752
 753                wdev_lock(wdev);
 754                switch (ev->type) {
 755                case EVENT_CONNECT_RESULT:
 756                        if (!is_zero_ether_addr(ev->cr.bssid))
 757                                bssid = ev->cr.bssid;
 758                        __cfg80211_connect_result(
 759                                wdev->netdev, bssid,
 760                                ev->cr.req_ie, ev->cr.req_ie_len,
 761                                ev->cr.resp_ie, ev->cr.resp_ie_len,
 762                                ev->cr.status,
 763                                ev->cr.status == WLAN_STATUS_SUCCESS,
 764                                NULL);
 765                        break;
 766                case EVENT_ROAMED:
 767                        __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
 768                                          ev->rm.req_ie_len, ev->rm.resp_ie,
 769                                          ev->rm.resp_ie_len);
 770                        break;
 771                case EVENT_DISCONNECTED:
 772                        __cfg80211_disconnected(wdev->netdev,
 773                                                ev->dc.ie, ev->dc.ie_len,
 774                                                ev->dc.reason, true);
 775                        break;
 776                case EVENT_IBSS_JOINED:
 777                        __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
 778                        break;
 779                }
 780                wdev_unlock(wdev);
 781
 782                kfree(ev);
 783
 784                spin_lock_irqsave(&wdev->event_lock, flags);
 785        }
 786        spin_unlock_irqrestore(&wdev->event_lock, flags);
 787}
 788
 789void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
 790{
 791        struct wireless_dev *wdev;
 792
 793        ASSERT_RTNL();
 794        ASSERT_RDEV_LOCK(rdev);
 795
 796        mutex_lock(&rdev->devlist_mtx);
 797
 798        list_for_each_entry(wdev, &rdev->wdev_list, list)
 799                cfg80211_process_wdev_events(wdev);
 800
 801        mutex_unlock(&rdev->devlist_mtx);
 802}
 803
 804int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
 805                          struct net_device *dev, enum nl80211_iftype ntype,
 806                          u32 *flags, struct vif_params *params)
 807{
 808        int err;
 809        enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
 810
 811        ASSERT_RDEV_LOCK(rdev);
 812
 813        /* don't support changing VLANs, you just re-create them */
 814        if (otype == NL80211_IFTYPE_AP_VLAN)
 815                return -EOPNOTSUPP;
 816
 817        if (!rdev->ops->change_virtual_intf ||
 818            !(rdev->wiphy.interface_modes & (1 << ntype)))
 819                return -EOPNOTSUPP;
 820
 821        /* if it's part of a bridge, reject changing type to station/ibss */
 822        if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
 823            (ntype == NL80211_IFTYPE_ADHOC ||
 824             ntype == NL80211_IFTYPE_STATION ||
 825             ntype == NL80211_IFTYPE_P2P_CLIENT))
 826                return -EBUSY;
 827
 828        if (ntype != otype && netif_running(dev)) {
 829                mutex_lock(&rdev->devlist_mtx);
 830                err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
 831                                                    ntype);
 832                mutex_unlock(&rdev->devlist_mtx);
 833                if (err)
 834                        return err;
 835
 836                dev->ieee80211_ptr->use_4addr = false;
 837                dev->ieee80211_ptr->mesh_id_up_len = 0;
 838
 839                switch (otype) {
 840                case NL80211_IFTYPE_AP:
 841                        cfg80211_stop_ap(rdev, dev);
 842                        break;
 843                case NL80211_IFTYPE_ADHOC:
 844                        cfg80211_leave_ibss(rdev, dev, false);
 845                        break;
 846                case NL80211_IFTYPE_STATION:
 847                case NL80211_IFTYPE_P2P_CLIENT:
 848                        cfg80211_disconnect(rdev, dev,
 849                                            WLAN_REASON_DEAUTH_LEAVING, true);
 850                        break;
 851                case NL80211_IFTYPE_MESH_POINT:
 852                        /* mesh should be handled? */
 853                        break;
 854                default:
 855                        break;
 856                }
 857
 858                cfg80211_process_rdev_events(rdev);
 859        }
 860
 861        err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
 862                                             ntype, flags, params);
 863
 864        WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
 865
 866        if (!err && params && params->use_4addr != -1)
 867                dev->ieee80211_ptr->use_4addr = params->use_4addr;
 868
 869        if (!err) {
 870                dev->priv_flags &= ~IFF_DONT_BRIDGE;
 871                switch (ntype) {
 872                case NL80211_IFTYPE_STATION:
 873                        if (dev->ieee80211_ptr->use_4addr)
 874                                break;
 875                        /* fall through */
 876                case NL80211_IFTYPE_P2P_CLIENT:
 877                case NL80211_IFTYPE_ADHOC:
 878                        dev->priv_flags |= IFF_DONT_BRIDGE;
 879                        break;
 880                case NL80211_IFTYPE_P2P_GO:
 881                case NL80211_IFTYPE_AP:
 882                case NL80211_IFTYPE_AP_VLAN:
 883                case NL80211_IFTYPE_WDS:
 884                case NL80211_IFTYPE_MESH_POINT:
 885                        /* bridging OK */
 886                        break;
 887                case NL80211_IFTYPE_MONITOR:
 888                        /* monitor can't bridge anyway */
 889                        break;
 890                case NL80211_IFTYPE_UNSPECIFIED:
 891                case NUM_NL80211_IFTYPES:
 892                        /* not happening */
 893                        break;
 894                }
 895        }
 896
 897        if (!err && ntype != otype && netif_running(dev)) {
 898                cfg80211_update_iface_num(rdev, ntype, 1);
 899                cfg80211_update_iface_num(rdev, otype, -1);
 900        }
 901
 902        return err;
 903}
 904
 905static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
 906{
 907        static const u32 __mcs2bitrate[] = {
 908                /* control PHY */
 909                [0] =   275,
 910                /* SC PHY */
 911                [1] =  3850,
 912                [2] =  7700,
 913                [3] =  9625,
 914                [4] = 11550,
 915                [5] = 12512, /* 1251.25 mbps */
 916                [6] = 15400,
 917                [7] = 19250,
 918                [8] = 23100,
 919                [9] = 25025,
 920                [10] = 30800,
 921                [11] = 38500,
 922                [12] = 46200,
 923                /* OFDM PHY */
 924                [13] =  6930,
 925                [14] =  8662, /* 866.25 mbps */
 926                [15] = 13860,
 927                [16] = 17325,
 928                [17] = 20790,
 929                [18] = 27720,
 930                [19] = 34650,
 931                [20] = 41580,
 932                [21] = 45045,
 933                [22] = 51975,
 934                [23] = 62370,
 935                [24] = 67568, /* 6756.75 mbps */
 936                /* LP-SC PHY */
 937                [25] =  6260,
 938                [26] =  8340,
 939                [27] = 11120,
 940                [28] = 12510,
 941                [29] = 16680,
 942                [30] = 22240,
 943                [31] = 25030,
 944        };
 945
 946        if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
 947                return 0;
 948
 949        return __mcs2bitrate[rate->mcs];
 950}
 951
 952u32 cfg80211_calculate_bitrate(struct rate_info *rate)
 953{
 954        int modulation, streams, bitrate;
 955
 956        if (!(rate->flags & RATE_INFO_FLAGS_MCS))
 957                return rate->legacy;
 958        if (rate->flags & RATE_INFO_FLAGS_60G)
 959                return cfg80211_calculate_bitrate_60g(rate);
 960
 961        /* the formula below does only work for MCS values smaller than 32 */
 962        if (WARN_ON_ONCE(rate->mcs >= 32))
 963                return 0;
 964
 965        modulation = rate->mcs & 7;
 966        streams = (rate->mcs >> 3) + 1;
 967
 968        bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
 969                        13500000 : 6500000;
 970
 971        if (modulation < 4)
 972                bitrate *= (modulation + 1);
 973        else if (modulation == 4)
 974                bitrate *= (modulation + 2);
 975        else
 976                bitrate *= (modulation + 3);
 977
 978        bitrate *= streams;
 979
 980        if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
 981                bitrate = (bitrate / 9) * 10;
 982
 983        /* do NOT round down here */
 984        return (bitrate + 50000) / 100000;
 985}
 986EXPORT_SYMBOL(cfg80211_calculate_bitrate);
 987
 988int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
 989                                 u32 beacon_int)
 990{
 991        struct wireless_dev *wdev;
 992        int res = 0;
 993
 994        if (!beacon_int)
 995                return -EINVAL;
 996
 997        mutex_lock(&rdev->devlist_mtx);
 998
 999        list_for_each_entry(wdev, &rdev->wdev_list, list) {
1000                if (!wdev->beacon_interval)
1001                        continue;
1002                if (wdev->beacon_interval != beacon_int) {
1003                        res = -EINVAL;
1004                        break;
1005                }
1006        }
1007
1008        mutex_unlock(&rdev->devlist_mtx);
1009
1010        return res;
1011}
1012
1013int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1014                                 struct wireless_dev *wdev,
1015                                 enum nl80211_iftype iftype,
1016                                 struct ieee80211_channel *chan,
1017                                 enum cfg80211_chan_mode chanmode)
1018{
1019        struct wireless_dev *wdev_iter;
1020        u32 used_iftypes = BIT(iftype);
1021        int num[NUM_NL80211_IFTYPES];
1022        struct ieee80211_channel
1023                        *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1024        struct ieee80211_channel *ch;
1025        enum cfg80211_chan_mode chmode;
1026        int num_different_channels = 0;
1027        int total = 1;
1028        int i, j;
1029
1030        ASSERT_RTNL();
1031        lockdep_assert_held(&rdev->devlist_mtx);
1032
1033        /* Always allow software iftypes */
1034        if (rdev->wiphy.software_iftypes & BIT(iftype))
1035                return 0;
1036
1037        memset(num, 0, sizeof(num));
1038        memset(used_channels, 0, sizeof(used_channels));
1039
1040        num[iftype] = 1;
1041
1042        switch (chanmode) {
1043        case CHAN_MODE_UNDEFINED:
1044                break;
1045        case CHAN_MODE_SHARED:
1046                WARN_ON(!chan);
1047                used_channels[0] = chan;
1048                num_different_channels++;
1049                break;
1050        case CHAN_MODE_EXCLUSIVE:
1051                num_different_channels++;
1052                break;
1053        }
1054
1055        list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
1056                if (wdev_iter == wdev)
1057                        continue;
1058                if (!netif_running(wdev_iter->netdev))
1059                        continue;
1060
1061                if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1062                        continue;
1063
1064                /*
1065                 * We may be holding the "wdev" mutex, but now need to lock
1066                 * wdev_iter. This is OK because once we get here wdev_iter
1067                 * is not wdev (tested above), but we need to use the nested
1068                 * locking for lockdep.
1069                 */
1070                mutex_lock_nested(&wdev_iter->mtx, 1);
1071                __acquire(wdev_iter->mtx);
1072                cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1073                wdev_unlock(wdev_iter);
1074
1075                switch (chmode) {
1076                case CHAN_MODE_UNDEFINED:
1077                        break;
1078                case CHAN_MODE_SHARED:
1079                        for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1080                                if (!used_channels[i] || used_channels[i] == ch)
1081                                        break;
1082
1083                        if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
1084                                return -EBUSY;
1085
1086                        if (used_channels[i] == NULL) {
1087                                used_channels[i] = ch;
1088                                num_different_channels++;
1089                        }
1090                        break;
1091                case CHAN_MODE_EXCLUSIVE:
1092                        num_different_channels++;
1093                        break;
1094                }
1095
1096                num[wdev_iter->iftype]++;
1097                total++;
1098                used_iftypes |= BIT(wdev_iter->iftype);
1099        }
1100
1101        if (total == 1)
1102                return 0;
1103
1104        for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1105                const struct ieee80211_iface_combination *c;
1106                struct ieee80211_iface_limit *limits;
1107                u32 all_iftypes = 0;
1108
1109                c = &rdev->wiphy.iface_combinations[i];
1110
1111                if (total > c->max_interfaces)
1112                        continue;
1113                if (num_different_channels > c->num_different_channels)
1114                        continue;
1115
1116                limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1117                                 GFP_KERNEL);
1118                if (!limits)
1119                        return -ENOMEM;
1120
1121                for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1122                        if (rdev->wiphy.software_iftypes & BIT(iftype))
1123                                continue;
1124                        for (j = 0; j < c->n_limits; j++) {
1125                                all_iftypes |= limits[j].types;
1126                                if (!(limits[j].types & BIT(iftype)))
1127                                        continue;
1128                                if (limits[j].max < num[iftype])
1129                                        goto cont;
1130                                limits[j].max -= num[iftype];
1131                        }
1132                }
1133
1134                /*
1135                 * Finally check that all iftypes that we're currently
1136                 * using are actually part of this combination. If they
1137                 * aren't then we can't use this combination and have
1138                 * to continue to the next.
1139                 */
1140                if ((all_iftypes & used_iftypes) != used_iftypes)
1141                        goto cont;
1142
1143                /*
1144                 * This combination covered all interface types and
1145                 * supported the requested numbers, so we're good.
1146                 */
1147                kfree(limits);
1148                return 0;
1149 cont:
1150                kfree(limits);
1151        }
1152
1153        return -EBUSY;
1154}
1155
1156int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1157                           const u8 *rates, unsigned int n_rates,
1158                           u32 *mask)
1159{
1160        int i, j;
1161
1162        if (!sband)
1163                return -EINVAL;
1164
1165        if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1166                return -EINVAL;
1167
1168        *mask = 0;
1169
1170        for (i = 0; i < n_rates; i++) {
1171                int rate = (rates[i] & 0x7f) * 5;
1172                bool found = false;
1173
1174                for (j = 0; j < sband->n_bitrates; j++) {
1175                        if (sband->bitrates[j].bitrate == rate) {
1176                                found = true;
1177                                *mask |= BIT(j);
1178                                break;
1179                        }
1180                }
1181                if (!found)
1182                        return -EINVAL;
1183        }
1184
1185        /*
1186         * mask must have at least one bit set here since we
1187         * didn't accept a 0-length rates array nor allowed
1188         * entries in the array that didn't exist
1189         */
1190
1191        return 0;
1192}
1193
1194/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1195/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1196const unsigned char rfc1042_header[] __aligned(2) =
1197        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1198EXPORT_SYMBOL(rfc1042_header);
1199
1200/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1201const unsigned char bridge_tunnel_header[] __aligned(2) =
1202        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1203EXPORT_SYMBOL(bridge_tunnel_header);
1204
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.