linux/drivers/staging/rtl8192e/rtllib_rx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Original code based Host AP (software wireless LAN access point) driver
   4 * for Intersil Prism2/2.5/3 - hostap.o module, common routines
   5 *
   6 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
   7 * <jkmaline@cc.hut.fi>
   8 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
   9 * Copyright (c) 2004, Intel Corporation
  10 *
  11 * Few modifications for Realtek's Wi-Fi drivers by
  12 * Andrea Merello <andrea.merello@gmail.com>
  13 *
  14 * A special thanks goes to Realtek for their support !
  15 */
  16#include <linux/compiler.h>
  17#include <linux/errno.h>
  18#include <linux/if_arp.h>
  19#include <linux/in6.h>
  20#include <linux/in.h>
  21#include <linux/ip.h>
  22#include <linux/kernel.h>
  23#include <linux/module.h>
  24#include <linux/netdevice.h>
  25#include <linux/pci.h>
  26#include <linux/proc_fs.h>
  27#include <linux/skbuff.h>
  28#include <linux/slab.h>
  29#include <linux/tcp.h>
  30#include <linux/types.h>
  31#include <linux/wireless.h>
  32#include <linux/etherdevice.h>
  33#include <linux/uaccess.h>
  34#include <linux/ctype.h>
  35
  36#include "rtllib.h"
  37#include "dot11d.h"
  38
  39static void rtllib_rx_mgt(struct rtllib_device *ieee, struct sk_buff *skb,
  40                          struct rtllib_rx_stats *stats);
  41
  42static inline void rtllib_monitor_rx(struct rtllib_device *ieee,
  43                                     struct sk_buff *skb,
  44                                     struct rtllib_rx_stats *rx_status,
  45                                     size_t hdr_length)
  46{
  47        skb->dev = ieee->dev;
  48        skb_reset_mac_header(skb);
  49        skb_pull(skb, hdr_length);
  50        skb->pkt_type = PACKET_OTHERHOST;
  51        skb->protocol = htons(ETH_P_80211_RAW);
  52        memset(skb->cb, 0, sizeof(skb->cb));
  53        netif_rx(skb);
  54}
  55
  56/* Called only as a tasklet (software IRQ) */
  57static struct rtllib_frag_entry *
  58rtllib_frag_cache_find(struct rtllib_device *ieee, unsigned int seq,
  59                          unsigned int frag, u8 tid, u8 *src, u8 *dst)
  60{
  61        struct rtllib_frag_entry *entry;
  62        int i;
  63
  64        for (i = 0; i < RTLLIB_FRAG_CACHE_LEN; i++) {
  65                entry = &ieee->frag_cache[tid][i];
  66                if (entry->skb != NULL &&
  67                    time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  68                        netdev_dbg(ieee->dev,
  69                                   "expiring fragment cache entry seq=%u last_frag=%u\n",
  70                                   entry->seq, entry->last_frag);
  71                        dev_kfree_skb_any(entry->skb);
  72                        entry->skb = NULL;
  73                }
  74
  75                if (entry->skb != NULL && entry->seq == seq &&
  76                    (entry->last_frag + 1 == frag || frag == -1) &&
  77                    memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
  78                    memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
  79                        return entry;
  80        }
  81
  82        return NULL;
  83}
  84
  85/* Called only as a tasklet (software IRQ) */
  86static struct sk_buff *
  87rtllib_frag_cache_get(struct rtllib_device *ieee,
  88                         struct rtllib_hdr_4addr *hdr)
  89{
  90        struct sk_buff *skb = NULL;
  91        u16 fc = le16_to_cpu(hdr->frame_ctl);
  92        u16 sc = le16_to_cpu(hdr->seq_ctl);
  93        unsigned int frag = WLAN_GET_SEQ_FRAG(sc);
  94        unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
  95        struct rtllib_frag_entry *entry;
  96        struct rtllib_hdr_3addrqos *hdr_3addrqos;
  97        struct rtllib_hdr_4addrqos *hdr_4addrqos;
  98        u8 tid;
  99
 100        if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS) &&
 101            RTLLIB_QOS_HAS_SEQ(fc)) {
 102                hdr_4addrqos = (struct rtllib_hdr_4addrqos *)hdr;
 103                tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
 104                tid = UP2AC(tid);
 105                tid++;
 106        } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
 107                hdr_3addrqos = (struct rtllib_hdr_3addrqos *)hdr;
 108                tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
 109                tid = UP2AC(tid);
 110                tid++;
 111        } else {
 112                tid = 0;
 113        }
 114
 115        if (frag == 0) {
 116                /* Reserve enough space to fit maximum frame length */
 117                skb = dev_alloc_skb(ieee->dev->mtu +
 118                                    sizeof(struct rtllib_hdr_4addr) +
 119                                    8 /* LLC */ +
 120                                    2 /* alignment */ +
 121                                    8 /* WEP */ +
 122                                    ETH_ALEN /* WDS */ +
 123                                    /* QOS Control */
 124                                    (RTLLIB_QOS_HAS_SEQ(fc) ? 2 : 0));
 125                if (!skb)
 126                        return NULL;
 127
 128                entry = &ieee->frag_cache[tid][ieee->frag_next_idx[tid]];
 129                ieee->frag_next_idx[tid]++;
 130                if (ieee->frag_next_idx[tid] >= RTLLIB_FRAG_CACHE_LEN)
 131                        ieee->frag_next_idx[tid] = 0;
 132
 133                if (entry->skb != NULL)
 134                        dev_kfree_skb_any(entry->skb);
 135
 136                entry->first_frag_time = jiffies;
 137                entry->seq = seq;
 138                entry->last_frag = frag;
 139                entry->skb = skb;
 140                ether_addr_copy(entry->src_addr, hdr->addr2);
 141                ether_addr_copy(entry->dst_addr, hdr->addr1);
 142        } else {
 143                /* received a fragment of a frame for which the head fragment
 144                 * should have already been received
 145                 */
 146                entry = rtllib_frag_cache_find(ieee, seq, frag, tid, hdr->addr2,
 147                                                  hdr->addr1);
 148                if (entry != NULL) {
 149                        entry->last_frag = frag;
 150                        skb = entry->skb;
 151                }
 152        }
 153
 154        return skb;
 155}
 156
 157
 158/* Called only as a tasklet (software IRQ) */
 159static int rtllib_frag_cache_invalidate(struct rtllib_device *ieee,
 160                                           struct rtllib_hdr_4addr *hdr)
 161{
 162        u16 fc = le16_to_cpu(hdr->frame_ctl);
 163        u16 sc = le16_to_cpu(hdr->seq_ctl);
 164        unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
 165        struct rtllib_frag_entry *entry;
 166        struct rtllib_hdr_3addrqos *hdr_3addrqos;
 167        struct rtllib_hdr_4addrqos *hdr_4addrqos;
 168        u8 tid;
 169
 170        if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS) &&
 171            RTLLIB_QOS_HAS_SEQ(fc)) {
 172                hdr_4addrqos = (struct rtllib_hdr_4addrqos *)hdr;
 173                tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
 174                tid = UP2AC(tid);
 175                tid++;
 176        } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
 177                hdr_3addrqos = (struct rtllib_hdr_3addrqos *)hdr;
 178                tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
 179                tid = UP2AC(tid);
 180                tid++;
 181        } else {
 182                tid = 0;
 183        }
 184
 185        entry = rtllib_frag_cache_find(ieee, seq, -1, tid, hdr->addr2,
 186                                          hdr->addr1);
 187
 188        if (entry == NULL) {
 189                netdev_dbg(ieee->dev,
 190                           "Couldn't invalidate fragment cache entry (seq=%u)\n",
 191                           seq);
 192                return -1;
 193        }
 194
 195        entry->skb = NULL;
 196        return 0;
 197}
 198
 199/* rtllib_rx_frame_mgtmt
 200 *
 201 * Responsible for handling management control frames
 202 *
 203 * Called by rtllib_rx
 204 */
 205static inline int
 206rtllib_rx_frame_mgmt(struct rtllib_device *ieee, struct sk_buff *skb,
 207                        struct rtllib_rx_stats *rx_stats, u16 type,
 208                        u16 stype)
 209{
 210        /* On the struct stats definition there is written that
 211         * this is not mandatory.... but seems that the probe
 212         * response parser uses it
 213         */
 214        struct rtllib_hdr_3addr *hdr = (struct rtllib_hdr_3addr *)skb->data;
 215
 216        rx_stats->len = skb->len;
 217        rtllib_rx_mgt(ieee, skb, rx_stats);
 218        if ((memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN))) {
 219                dev_kfree_skb_any(skb);
 220                return 0;
 221        }
 222        rtllib_rx_frame_softmac(ieee, skb, rx_stats, type, stype);
 223
 224        dev_kfree_skb_any(skb);
 225
 226        return 0;
 227}
 228
 229/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation
 230 * Ethernet-II snap header (RFC1042 for most EtherTypes)
 231 */
 232static unsigned char rfc1042_header[] = {
 233        0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
 234};
 235/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
 236static unsigned char bridge_tunnel_header[] = {
 237        0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8
 238};
 239/* No encapsulation header if EtherType < 0x600 (=length) */
 240
 241/* Called by rtllib_rx_frame_decrypt */
 242static int rtllib_is_eapol_frame(struct rtllib_device *ieee,
 243                                    struct sk_buff *skb, size_t hdrlen)
 244{
 245        struct net_device *dev = ieee->dev;
 246        u16 fc, ethertype;
 247        struct rtllib_hdr_4addr *hdr;
 248        u8 *pos;
 249
 250        if (skb->len < 24)
 251                return 0;
 252
 253        hdr = (struct rtllib_hdr_4addr *) skb->data;
 254        fc = le16_to_cpu(hdr->frame_ctl);
 255
 256        /* check that the frame is unicast frame to us */
 257        if ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
 258            RTLLIB_FCTL_TODS &&
 259            memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
 260            memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
 261                /* ToDS frame with own addr BSSID and DA */
 262        } else if ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
 263                   RTLLIB_FCTL_FROMDS &&
 264                   memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
 265                /* FromDS frame with own addr as DA */
 266        } else
 267                return 0;
 268
 269        if (skb->len < 24 + 8)
 270                return 0;
 271
 272        /* check for port access entity Ethernet type */
 273        pos = skb->data + hdrlen;
 274        ethertype = (pos[6] << 8) | pos[7];
 275        if (ethertype == ETH_P_PAE)
 276                return 1;
 277
 278        return 0;
 279}
 280
 281/* Called only as a tasklet (software IRQ), by rtllib_rx */
 282static inline int
 283rtllib_rx_frame_decrypt(struct rtllib_device *ieee, struct sk_buff *skb,
 284                        struct lib80211_crypt_data *crypt)
 285{
 286        struct rtllib_hdr_4addr *hdr;
 287        int res, hdrlen;
 288
 289        if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
 290                return 0;
 291
 292        if (ieee->hwsec_active) {
 293                struct cb_desc *tcb_desc = (struct cb_desc *)
 294                                                (skb->cb + MAX_DEV_ADDR_SIZE);
 295
 296                tcb_desc->bHwSec = 1;
 297
 298                if (ieee->need_sw_enc)
 299                        tcb_desc->bHwSec = 0;
 300        }
 301
 302        hdr = (struct rtllib_hdr_4addr *) skb->data;
 303        hdrlen = rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
 304
 305        atomic_inc(&crypt->refcnt);
 306        res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
 307        atomic_dec(&crypt->refcnt);
 308        if (res < 0) {
 309                netdev_dbg(ieee->dev, "decryption failed (SA= %pM) res=%d\n",
 310                           hdr->addr2, res);
 311                if (res == -2)
 312                        netdev_dbg(ieee->dev,
 313                                   "Decryption failed ICV mismatch (key %d)\n",
 314                                   skb->data[hdrlen + 3] >> 6);
 315                return -1;
 316        }
 317
 318        return res;
 319}
 320
 321
 322/* Called only as a tasklet (software IRQ), by rtllib_rx */
 323static inline int
 324rtllib_rx_frame_decrypt_msdu(struct rtllib_device *ieee, struct sk_buff *skb,
 325                             int keyidx, struct lib80211_crypt_data *crypt)
 326{
 327        struct rtllib_hdr_4addr *hdr;
 328        int res, hdrlen;
 329
 330        if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
 331                return 0;
 332        if (ieee->hwsec_active) {
 333                struct cb_desc *tcb_desc = (struct cb_desc *)
 334                                                (skb->cb + MAX_DEV_ADDR_SIZE);
 335
 336                tcb_desc->bHwSec = 1;
 337
 338                if (ieee->need_sw_enc)
 339                        tcb_desc->bHwSec = 0;
 340        }
 341
 342        hdr = (struct rtllib_hdr_4addr *) skb->data;
 343        hdrlen = rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
 344
 345        atomic_inc(&crypt->refcnt);
 346        res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
 347        atomic_dec(&crypt->refcnt);
 348        if (res < 0) {
 349                netdev_dbg(ieee->dev,
 350                           "MSDU decryption/MIC verification failed (SA= %pM keyidx=%d)\n",
 351                           hdr->addr2, keyidx);
 352                return -1;
 353        }
 354
 355        return 0;
 356}
 357
 358
 359/* this function is stolen from ipw2200 driver*/
 360#define IEEE_PACKET_RETRY_TIME (5*HZ)
 361static int is_duplicate_packet(struct rtllib_device *ieee,
 362                                      struct rtllib_hdr_4addr *header)
 363{
 364        u16 fc = le16_to_cpu(header->frame_ctl);
 365        u16 sc = le16_to_cpu(header->seq_ctl);
 366        u16 seq = WLAN_GET_SEQ_SEQ(sc);
 367        u16 frag = WLAN_GET_SEQ_FRAG(sc);
 368        u16 *last_seq, *last_frag;
 369        unsigned long *last_time;
 370        struct rtllib_hdr_3addrqos *hdr_3addrqos;
 371        struct rtllib_hdr_4addrqos *hdr_4addrqos;
 372        u8 tid;
 373
 374        if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS) &&
 375            RTLLIB_QOS_HAS_SEQ(fc)) {
 376                hdr_4addrqos = (struct rtllib_hdr_4addrqos *)header;
 377                tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
 378                tid = UP2AC(tid);
 379                tid++;
 380        } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
 381                hdr_3addrqos = (struct rtllib_hdr_3addrqos *)header;
 382                tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
 383                tid = UP2AC(tid);
 384                tid++;
 385        } else {
 386                tid = 0;
 387        }
 388
 389        switch (ieee->iw_mode) {
 390        case IW_MODE_ADHOC:
 391        {
 392                struct list_head *p;
 393                struct ieee_ibss_seq *entry = NULL;
 394                u8 *mac = header->addr2;
 395                int index = mac[5] % IEEE_IBSS_MAC_HASH_SIZE;
 396
 397                list_for_each(p, &ieee->ibss_mac_hash[index]) {
 398                        entry = list_entry(p, struct ieee_ibss_seq, list);
 399                        if (!memcmp(entry->mac, mac, ETH_ALEN))
 400                                break;
 401                }
 402                if (p == &ieee->ibss_mac_hash[index]) {
 403                        entry = kmalloc(sizeof(struct ieee_ibss_seq),
 404                                        GFP_ATOMIC);
 405                        if (!entry)
 406                                return 0;
 407
 408                        ether_addr_copy(entry->mac, mac);
 409                        entry->seq_num[tid] = seq;
 410                        entry->frag_num[tid] = frag;
 411                        entry->packet_time[tid] = jiffies;
 412                        list_add(&entry->list, &ieee->ibss_mac_hash[index]);
 413                        return 0;
 414                }
 415                last_seq = &entry->seq_num[tid];
 416                last_frag = &entry->frag_num[tid];
 417                last_time = &entry->packet_time[tid];
 418                break;
 419        }
 420
 421        case IW_MODE_INFRA:
 422                last_seq = &ieee->last_rxseq_num[tid];
 423                last_frag = &ieee->last_rxfrag_num[tid];
 424                last_time = &ieee->last_packet_time[tid];
 425                break;
 426        default:
 427                return 0;
 428        }
 429
 430        if ((*last_seq == seq) &&
 431            time_after(*last_time + IEEE_PACKET_RETRY_TIME, jiffies)) {
 432                if (*last_frag == frag)
 433                        goto drop;
 434                if (*last_frag + 1 != frag)
 435                        /* out-of-order fragment */
 436                        goto drop;
 437        } else
 438                *last_seq = seq;
 439
 440        *last_frag = frag;
 441        *last_time = jiffies;
 442        return 0;
 443
 444drop:
 445
 446        return 1;
 447}
 448
 449static bool AddReorderEntry(struct rx_ts_record *pTS,
 450                            struct rx_reorder_entry *pReorderEntry)
 451{
 452        struct list_head *pList = &pTS->RxPendingPktList;
 453
 454        while (pList->next != &pTS->RxPendingPktList) {
 455                if (SN_LESS(pReorderEntry->SeqNum, ((struct rx_reorder_entry *)
 456                    list_entry(pList->next, struct rx_reorder_entry,
 457                    List))->SeqNum))
 458                        pList = pList->next;
 459                else if (SN_EQUAL(pReorderEntry->SeqNum,
 460                        ((struct rx_reorder_entry *)list_entry(pList->next,
 461                        struct rx_reorder_entry, List))->SeqNum))
 462                        return false;
 463                else
 464                        break;
 465        }
 466        pReorderEntry->List.next = pList->next;
 467        pReorderEntry->List.next->prev = &pReorderEntry->List;
 468        pReorderEntry->List.prev = pList;
 469        pList->next = &pReorderEntry->List;
 470
 471        return true;
 472}
 473
 474void rtllib_indicate_packets(struct rtllib_device *ieee,
 475                             struct rtllib_rxb **prxbIndicateArray, u8 index)
 476{
 477        struct net_device_stats *stats = &ieee->stats;
 478        u8 i = 0, j = 0;
 479        u16 ethertype;
 480
 481        for (j = 0; j < index; j++) {
 482                struct rtllib_rxb *prxb = prxbIndicateArray[j];
 483
 484                for (i = 0; i < prxb->nr_subframes; i++) {
 485                        struct sk_buff *sub_skb = prxb->subframes[i];
 486
 487                /* convert hdr + possible LLC headers into Ethernet header */
 488                        ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
 489                        if (sub_skb->len >= 8 &&
 490                            ((memcmp(sub_skb->data, rfc1042_header,
 491                                     SNAP_SIZE) == 0 &&
 492                              ethertype != ETH_P_AARP &&
 493                              ethertype != ETH_P_IPX) ||
 494                            memcmp(sub_skb->data, bridge_tunnel_header,
 495                                   SNAP_SIZE) == 0)) {
 496                                /* remove RFC1042 or Bridge-Tunnel encapsulation
 497                                 * and replace EtherType
 498                                 */
 499                                skb_pull(sub_skb, SNAP_SIZE);
 500                                memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
 501                                memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
 502                        } else {
 503                                u16 len;
 504                        /* Leave Ethernet header part of hdr and full payload */
 505                                len = sub_skb->len;
 506                                memcpy(skb_push(sub_skb, 2), &len, 2);
 507                                memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
 508                                memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
 509                        }
 510
 511                        /* Indicate the packets to upper layer */
 512                        if (sub_skb) {
 513                                stats->rx_packets++;
 514                                stats->rx_bytes += sub_skb->len;
 515
 516                                memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
 517                                sub_skb->protocol = eth_type_trans(sub_skb,
 518                                                                   ieee->dev);
 519                                sub_skb->dev = ieee->dev;
 520                                sub_skb->dev->stats.rx_packets++;
 521                                sub_skb->dev->stats.rx_bytes += sub_skb->len;
 522                                /* 802.11 crc not sufficient */
 523                                sub_skb->ip_summed = CHECKSUM_NONE;
 524                                ieee->last_rx_ps_time = jiffies;
 525                                netif_rx(sub_skb);
 526                        }
 527                }
 528                kfree(prxb);
 529                prxb = NULL;
 530        }
 531}
 532
 533void rtllib_FlushRxTsPendingPkts(struct rtllib_device *ieee,
 534                                 struct rx_ts_record *pTS)
 535{
 536        struct rx_reorder_entry *pRxReorderEntry;
 537        u8 RfdCnt = 0;
 538
 539        del_timer_sync(&pTS->RxPktPendingTimer);
 540        while (!list_empty(&pTS->RxPendingPktList)) {
 541                if (RfdCnt >= REORDER_WIN_SIZE) {
 542                        netdev_info(ieee->dev,
 543                                    "-------------->%s() error! RfdCnt >= REORDER_WIN_SIZE\n",
 544                                    __func__);
 545                        break;
 546                }
 547
 548                pRxReorderEntry = (struct rx_reorder_entry *)
 549                                  list_entry(pTS->RxPendingPktList.prev,
 550                                             struct rx_reorder_entry, List);
 551                netdev_dbg(ieee->dev, "%s(): Indicate SeqNum %d!\n", __func__,
 552                           pRxReorderEntry->SeqNum);
 553                list_del_init(&pRxReorderEntry->List);
 554
 555                ieee->RfdArray[RfdCnt] = pRxReorderEntry->prxb;
 556
 557                RfdCnt = RfdCnt + 1;
 558                list_add_tail(&pRxReorderEntry->List,
 559                              &ieee->RxReorder_Unused_List);
 560        }
 561        rtllib_indicate_packets(ieee, ieee->RfdArray, RfdCnt);
 562
 563        pTS->RxIndicateSeq = 0xffff;
 564}
 565
 566static void RxReorderIndicatePacket(struct rtllib_device *ieee,
 567                                    struct rtllib_rxb *prxb,
 568                                    struct rx_ts_record *pTS, u16 SeqNum)
 569{
 570        struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
 571        struct rx_reorder_entry *pReorderEntry = NULL;
 572        u8 WinSize = pHTInfo->RxReorderWinSize;
 573        u16 WinEnd = 0;
 574        u8 index = 0;
 575        bool bMatchWinStart = false, bPktInBuf = false;
 576        unsigned long flags;
 577
 578        netdev_dbg(ieee->dev,
 579                   "%s(): Seq is %d, pTS->RxIndicateSeq is %d, WinSize is %d\n",
 580                   __func__, SeqNum, pTS->RxIndicateSeq, WinSize);
 581
 582        spin_lock_irqsave(&(ieee->reorder_spinlock), flags);
 583
 584        WinEnd = (pTS->RxIndicateSeq + WinSize - 1) % 4096;
 585        /* Rx Reorder initialize condition.*/
 586        if (pTS->RxIndicateSeq == 0xffff)
 587                pTS->RxIndicateSeq = SeqNum;
 588
 589        /* Drop out the packet which SeqNum is smaller than WinStart */
 590        if (SN_LESS(SeqNum, pTS->RxIndicateSeq)) {
 591                netdev_dbg(ieee->dev,
 592                           "Packet Drop! IndicateSeq: %d, NewSeq: %d\n",
 593                           pTS->RxIndicateSeq, SeqNum);
 594                pHTInfo->RxReorderDropCounter++;
 595                {
 596                        int i;
 597
 598                        for (i = 0; i < prxb->nr_subframes; i++)
 599                                dev_kfree_skb(prxb->subframes[i]);
 600                        kfree(prxb);
 601                        prxb = NULL;
 602                }
 603                spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
 604                return;
 605        }
 606
 607        /* Sliding window manipulation. Conditions includes:
 608         * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
 609         * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
 610         */
 611        if (SN_EQUAL(SeqNum, pTS->RxIndicateSeq)) {
 612                pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) % 4096;
 613                bMatchWinStart = true;
 614        } else if (SN_LESS(WinEnd, SeqNum)) {
 615                if (SeqNum >= (WinSize - 1))
 616                        pTS->RxIndicateSeq = SeqNum + 1 - WinSize;
 617                else
 618                        pTS->RxIndicateSeq = 4095 -
 619                                             (WinSize - (SeqNum + 1)) + 1;
 620                netdev_dbg(ieee->dev,
 621                           "Window Shift! IndicateSeq: %d, NewSeq: %d\n",
 622                           pTS->RxIndicateSeq, SeqNum);
 623        }
 624
 625        /* Indication process.
 626         * After Packet dropping and Sliding Window shifting as above, we can
 627         * now just indicate the packets with the SeqNum smaller than latest
 628         * WinStart and struct buffer other packets.
 629         *
 630         * For Rx Reorder condition:
 631         * 1. All packets with SeqNum smaller than WinStart => Indicate
 632         * 2. All packets with SeqNum larger than or equal to
 633         *       WinStart => Buffer it.
 634         */
 635        if (bMatchWinStart) {
 636                /* Current packet is going to be indicated.*/
 637                netdev_dbg(ieee->dev,
 638                           "Packets indication! IndicateSeq: %d, NewSeq: %d\n",
 639                           pTS->RxIndicateSeq, SeqNum);
 640                ieee->prxbIndicateArray[0] = prxb;
 641                index = 1;
 642        } else {
 643                /* Current packet is going to be inserted into pending list.*/
 644                if (!list_empty(&ieee->RxReorder_Unused_List)) {
 645                        pReorderEntry = (struct rx_reorder_entry *)
 646                                        list_entry(ieee->RxReorder_Unused_List.next,
 647                                        struct rx_reorder_entry, List);
 648                        list_del_init(&pReorderEntry->List);
 649
 650                        /* Make a reorder entry and insert
 651                         * into a the packet list.
 652                         */
 653                        pReorderEntry->SeqNum = SeqNum;
 654                        pReorderEntry->prxb = prxb;
 655
 656                        if (!AddReorderEntry(pTS, pReorderEntry)) {
 657                                int i;
 658
 659                                netdev_dbg(ieee->dev,
 660                                           "%s(): Duplicate packet is dropped. IndicateSeq: %d, NewSeq: %d\n",
 661                                           __func__, pTS->RxIndicateSeq,
 662                                           SeqNum);
 663                                list_add_tail(&pReorderEntry->List,
 664                                              &ieee->RxReorder_Unused_List);
 665
 666                                for (i = 0; i < prxb->nr_subframes; i++)
 667                                        dev_kfree_skb(prxb->subframes[i]);
 668                                kfree(prxb);
 669                                prxb = NULL;
 670                        } else {
 671                                netdev_dbg(ieee->dev,
 672                                           "Pkt insert into struct buffer. IndicateSeq: %d, NewSeq: %d\n",
 673                                           pTS->RxIndicateSeq, SeqNum);
 674                        }
 675                } else {
 676                        /* Packets are dropped if there are not enough reorder
 677                         * entries. This part should be modified!! We can just
 678                         * indicate all the packets in struct buffer and get
 679                         * reorder entries.
 680                         */
 681                        netdev_err(ieee->dev,
 682                                   "%s(): There is no reorder entry! Packet is dropped!\n",
 683                                   __func__);
 684                        {
 685                                int i;
 686
 687                                for (i = 0; i < prxb->nr_subframes; i++)
 688                                        dev_kfree_skb(prxb->subframes[i]);
 689                                kfree(prxb);
 690                                prxb = NULL;
 691                        }
 692                }
 693        }
 694
 695        /* Check if there is any packet need indicate.*/
 696        while (!list_empty(&pTS->RxPendingPktList)) {
 697                netdev_dbg(ieee->dev, "%s(): start RREORDER indicate\n",
 698                           __func__);
 699
 700                pReorderEntry = (struct rx_reorder_entry *)
 701                                        list_entry(pTS->RxPendingPktList.prev,
 702                                                   struct rx_reorder_entry,
 703                                                   List);
 704                if (SN_LESS(pReorderEntry->SeqNum, pTS->RxIndicateSeq) ||
 705                    SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq)) {
 706                        /* This protect struct buffer from overflow. */
 707                        if (index >= REORDER_WIN_SIZE) {
 708                                netdev_err(ieee->dev,
 709                                           "%s(): Buffer overflow!\n",
 710                                           __func__);
 711                                bPktInBuf = true;
 712                                break;
 713                        }
 714
 715                        list_del_init(&pReorderEntry->List);
 716
 717                        if (SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq))
 718                                pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) %
 719                                                     4096;
 720
 721                        ieee->prxbIndicateArray[index] = pReorderEntry->prxb;
 722                        netdev_dbg(ieee->dev, "%s(): Indicate SeqNum %d!\n",
 723                                   __func__, pReorderEntry->SeqNum);
 724                        index++;
 725
 726                        list_add_tail(&pReorderEntry->List,
 727                                      &ieee->RxReorder_Unused_List);
 728                } else {
 729                        bPktInBuf = true;
 730                        break;
 731                }
 732        }
 733
 734        /* Handling pending timer. Set this timer to prevent from long time
 735         * Rx buffering.
 736         */
 737        if (index > 0) {
 738                if (timer_pending(&pTS->RxPktPendingTimer))
 739                        del_timer_sync(&pTS->RxPktPendingTimer);
 740                pTS->RxTimeoutIndicateSeq = 0xffff;
 741
 742                if (index > REORDER_WIN_SIZE) {
 743                        netdev_err(ieee->dev,
 744                                   "%s(): Rx Reorder struct buffer full!\n",
 745                                   __func__);
 746                        spin_unlock_irqrestore(&(ieee->reorder_spinlock),
 747                                               flags);
 748                        return;
 749                }
 750                rtllib_indicate_packets(ieee, ieee->prxbIndicateArray, index);
 751                bPktInBuf = false;
 752        }
 753
 754        if (bPktInBuf && pTS->RxTimeoutIndicateSeq == 0xffff) {
 755                netdev_dbg(ieee->dev, "%s(): SET rx timeout timer\n", __func__);
 756                pTS->RxTimeoutIndicateSeq = pTS->RxIndicateSeq;
 757                mod_timer(&pTS->RxPktPendingTimer, jiffies +
 758                          msecs_to_jiffies(pHTInfo->RxReorderPendingTime));
 759        }
 760        spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
 761}
 762
 763static u8 parse_subframe(struct rtllib_device *ieee, struct sk_buff *skb,
 764                         struct rtllib_rx_stats *rx_stats,
 765                         struct rtllib_rxb *rxb, u8 *src, u8 *dst)
 766{
 767        struct rtllib_hdr_3addr  *hdr = (struct rtllib_hdr_3addr *)skb->data;
 768        u16             fc = le16_to_cpu(hdr->frame_ctl);
 769
 770        u16             LLCOffset = sizeof(struct rtllib_hdr_3addr);
 771        u16             ChkLength;
 772        bool            bIsAggregateFrame = false;
 773        u16             nSubframe_Length;
 774        u8              nPadding_Length = 0;
 775        u16             SeqNum = 0;
 776        struct sk_buff *sub_skb;
 777        /* just for debug purpose */
 778        SeqNum = WLAN_GET_SEQ_SEQ(le16_to_cpu(hdr->seq_ctl));
 779        if ((RTLLIB_QOS_HAS_SEQ(fc)) &&
 780           (((union frameqos *)(skb->data + RTLLIB_3ADDR_LEN))->field.reserved))
 781                bIsAggregateFrame = true;
 782
 783        if (RTLLIB_QOS_HAS_SEQ(fc))
 784                LLCOffset += 2;
 785        if (rx_stats->bContainHTC)
 786                LLCOffset += sHTCLng;
 787
 788        ChkLength = LLCOffset;
 789
 790        if (skb->len <= ChkLength)
 791                return 0;
 792
 793        skb_pull(skb, LLCOffset);
 794        ieee->bIsAggregateFrame = bIsAggregateFrame;
 795        if (!bIsAggregateFrame) {
 796                rxb->nr_subframes = 1;
 797
 798                /* altered by clark 3/30/2010
 799                 * The struct buffer size of the skb indicated to upper layer
 800                 * must be less than 5000, or the defraged IP datagram
 801                 * in the IP layer will exceed "ipfrag_high_tresh" and be
 802                 * discarded. so there must not use the function
 803                 * "skb_copy" and "skb_clone" for "skb".
 804                 */
 805
 806                /* Allocate new skb for releasing to upper layer */
 807                sub_skb = dev_alloc_skb(RTLLIB_SKBBUFFER_SIZE);
 808                if (!sub_skb)
 809                        return 0;
 810                skb_reserve(sub_skb, 12);
 811                skb_put_data(sub_skb, skb->data, skb->len);
 812                sub_skb->dev = ieee->dev;
 813
 814                rxb->subframes[0] = sub_skb;
 815
 816                memcpy(rxb->src, src, ETH_ALEN);
 817                memcpy(rxb->dst, dst, ETH_ALEN);
 818                rxb->subframes[0]->dev = ieee->dev;
 819                return 1;
 820        }
 821
 822        rxb->nr_subframes = 0;
 823        memcpy(rxb->src, src, ETH_ALEN);
 824        memcpy(rxb->dst, dst, ETH_ALEN);
 825        while (skb->len > ETHERNET_HEADER_SIZE) {
 826                /* Offset 12 denote 2 mac address */
 827                nSubframe_Length = *((u16 *)(skb->data + 12));
 828                nSubframe_Length = (nSubframe_Length >> 8) +
 829                                   (nSubframe_Length << 8);
 830
 831                if (skb->len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
 832                        netdev_info(ieee->dev,
 833                                    "%s: A-MSDU parse error!! pRfd->nTotalSubframe : %d\n",
 834                                    __func__, rxb->nr_subframes);
 835                        netdev_info(ieee->dev,
 836                                    "%s: A-MSDU parse error!! Subframe Length: %d\n",
 837                                    __func__, nSubframe_Length);
 838                        netdev_info(ieee->dev,
 839                                    "nRemain_Length is %d and nSubframe_Length is : %d\n",
 840                                    skb->len, nSubframe_Length);
 841                        netdev_info(ieee->dev,
 842                                    "The Packet SeqNum is %d\n",
 843                                    SeqNum);
 844                        return 0;
 845                }
 846
 847                /* move the data point to data content */
 848                skb_pull(skb, ETHERNET_HEADER_SIZE);
 849
 850                /* altered by clark 3/30/2010
 851                 * The struct buffer size of the skb indicated to upper layer
 852                 * must be less than 5000, or the defraged IP datagram
 853                 * in the IP layer will exceed "ipfrag_high_tresh" and be
 854                 * discarded. so there must not use the function
 855                 * "skb_copy" and "skb_clone" for "skb".
 856                 */
 857
 858                /* Allocate new skb for releasing to upper layer */
 859                sub_skb = dev_alloc_skb(nSubframe_Length + 12);
 860                if (!sub_skb)
 861                        return 0;
 862                skb_reserve(sub_skb, 12);
 863                skb_put_data(sub_skb, skb->data, nSubframe_Length);
 864
 865                sub_skb->dev = ieee->dev;
 866                rxb->subframes[rxb->nr_subframes++] = sub_skb;
 867                if (rxb->nr_subframes >= MAX_SUBFRAME_COUNT) {
 868                        netdev_dbg(ieee->dev,
 869                                   "ParseSubframe(): Too many Subframes! Packets dropped!\n");
 870                        break;
 871                }
 872                skb_pull(skb, nSubframe_Length);
 873
 874                if (skb->len != 0) {
 875                        nPadding_Length = 4 - ((nSubframe_Length +
 876                                          ETHERNET_HEADER_SIZE) % 4);
 877                        if (nPadding_Length == 4)
 878                                nPadding_Length = 0;
 879
 880                        if (skb->len < nPadding_Length)
 881                                return 0;
 882
 883                        skb_pull(skb, nPadding_Length);
 884                }
 885        }
 886
 887        return rxb->nr_subframes;
 888}
 889
 890
 891static size_t rtllib_rx_get_hdrlen(struct rtllib_device *ieee,
 892                                   struct sk_buff *skb,
 893                                   struct rtllib_rx_stats *rx_stats)
 894{
 895        struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
 896        u16 fc = le16_to_cpu(hdr->frame_ctl);
 897        size_t hdrlen;
 898
 899        hdrlen = rtllib_get_hdrlen(fc);
 900        if (HTCCheck(ieee, skb->data)) {
 901                if (net_ratelimit())
 902                        netdev_info(ieee->dev, "%s: find HTCControl!\n",
 903                                    __func__);
 904                hdrlen += 4;
 905                rx_stats->bContainHTC = true;
 906        }
 907
 908        if (RTLLIB_QOS_HAS_SEQ(fc))
 909                rx_stats->bIsQosData = true;
 910
 911        return hdrlen;
 912}
 913
 914static int rtllib_rx_check_duplicate(struct rtllib_device *ieee,
 915                                     struct sk_buff *skb, u8 multicast)
 916{
 917        struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
 918        u16 fc, sc;
 919        u8 frag, type, stype;
 920
 921        fc = le16_to_cpu(hdr->frame_ctl);
 922        type = WLAN_FC_GET_TYPE(fc);
 923        stype = WLAN_FC_GET_STYPE(fc);
 924        sc = le16_to_cpu(hdr->seq_ctl);
 925        frag = WLAN_GET_SEQ_FRAG(sc);
 926
 927        if ((ieee->pHTInfo->bCurRxReorderEnable == false) ||
 928                !ieee->current_network.qos_data.active ||
 929                !IsDataFrame(skb->data) ||
 930                IsLegacyDataFrame(skb->data)) {
 931                if (!((type == RTLLIB_FTYPE_MGMT) &&
 932                      (stype == RTLLIB_STYPE_BEACON))) {
 933                        if (is_duplicate_packet(ieee, hdr))
 934                                return -1;
 935                }
 936        } else {
 937                struct rx_ts_record *pRxTS = NULL;
 938
 939                if (GetTs(ieee, (struct ts_common_info **) &pRxTS, hdr->addr2,
 940                        (u8)Frame_QoSTID((u8 *)(skb->data)), RX_DIR, true)) {
 941                        if ((fc & (1<<11)) && (frag == pRxTS->RxLastFragNum) &&
 942                            (WLAN_GET_SEQ_SEQ(sc) == pRxTS->RxLastSeqNum))
 943                                return -1;
 944                        pRxTS->RxLastFragNum = frag;
 945                        pRxTS->RxLastSeqNum = WLAN_GET_SEQ_SEQ(sc);
 946                } else {
 947                        netdev_warn(ieee->dev, "%s(): No TS! Skip the check!\n",
 948                                    __func__);
 949                        return -1;
 950                }
 951        }
 952
 953        return 0;
 954}
 955
 956static void rtllib_rx_extract_addr(struct rtllib_device *ieee,
 957                                   struct rtllib_hdr_4addr *hdr, u8 *dst,
 958                                   u8 *src, u8 *bssid)
 959{
 960        u16 fc = le16_to_cpu(hdr->frame_ctl);
 961
 962        switch (fc & (RTLLIB_FCTL_FROMDS | RTLLIB_FCTL_TODS)) {
 963        case RTLLIB_FCTL_FROMDS:
 964                ether_addr_copy(dst, hdr->addr1);
 965                ether_addr_copy(src, hdr->addr3);
 966                ether_addr_copy(bssid, hdr->addr2);
 967                break;
 968        case RTLLIB_FCTL_TODS:
 969                ether_addr_copy(dst, hdr->addr3);
 970                ether_addr_copy(src, hdr->addr2);
 971                ether_addr_copy(bssid, hdr->addr1);
 972                break;
 973        case RTLLIB_FCTL_FROMDS | RTLLIB_FCTL_TODS:
 974                ether_addr_copy(dst, hdr->addr3);
 975                ether_addr_copy(src, hdr->addr4);
 976                ether_addr_copy(bssid, ieee->current_network.bssid);
 977                break;
 978        default:
 979                ether_addr_copy(dst, hdr->addr1);
 980                ether_addr_copy(src, hdr->addr2);
 981                ether_addr_copy(bssid, hdr->addr3);
 982                break;
 983        }
 984}
 985
 986static int rtllib_rx_data_filter(struct rtllib_device *ieee, u16 fc,
 987                                 u8 *dst, u8 *src, u8 *bssid, u8 *addr2)
 988{
 989        u8 type, stype;
 990
 991        type = WLAN_FC_GET_TYPE(fc);
 992        stype = WLAN_FC_GET_STYPE(fc);
 993
 994        /* Filter frames from different BSS */
 995        if (((fc & RTLLIB_FCTL_DSTODS) != RTLLIB_FCTL_DSTODS) &&
 996            !ether_addr_equal(ieee->current_network.bssid, bssid) &&
 997            !is_zero_ether_addr(ieee->current_network.bssid)) {
 998                return -1;
 999        }
1000
1001        /* Filter packets sent by an STA that will be forwarded by AP */
1002        if (ieee->IntelPromiscuousModeInfo.bPromiscuousOn  &&
1003                ieee->IntelPromiscuousModeInfo.bFilterSourceStationFrame) {
1004                if ((fc & RTLLIB_FCTL_TODS) && !(fc & RTLLIB_FCTL_FROMDS) &&
1005                    !ether_addr_equal(dst, ieee->current_network.bssid) &&
1006                    ether_addr_equal(bssid, ieee->current_network.bssid)) {
1007                        return -1;
1008                }
1009        }
1010
1011        /* Nullfunc frames may have PS-bit set, so they must be passed to
1012         * hostap_handle_sta_rx() before being dropped here.
1013         */
1014        if (!ieee->IntelPromiscuousModeInfo.bPromiscuousOn) {
1015                if (stype != RTLLIB_STYPE_DATA &&
1016                    stype != RTLLIB_STYPE_DATA_CFACK &&
1017                    stype != RTLLIB_STYPE_DATA_CFPOLL &&
1018                    stype != RTLLIB_STYPE_DATA_CFACKPOLL &&
1019                    stype != RTLLIB_STYPE_QOS_DATA) {
1020                        if (stype != RTLLIB_STYPE_NULLFUNC)
1021                                netdev_dbg(ieee->dev,
1022                                           "RX: dropped data frame with no data (type=0x%02x, subtype=0x%02x)\n",
1023                                           type, stype);
1024                        return -1;
1025                }
1026        }
1027
1028        if (ieee->iw_mode != IW_MODE_MESH) {
1029                /* packets from our adapter are dropped (echo) */
1030                if (!memcmp(src, ieee->dev->dev_addr, ETH_ALEN))
1031                        return -1;
1032
1033                /* {broad,multi}cast packets to our BSS go through */
1034                if (is_multicast_ether_addr(dst)) {
1035                        if (memcmp(bssid, ieee->current_network.bssid,
1036                                   ETH_ALEN))
1037                                return -1;
1038                }
1039        }
1040        return 0;
1041}
1042
1043static int rtllib_rx_get_crypt(struct rtllib_device *ieee, struct sk_buff *skb,
1044                        struct lib80211_crypt_data **crypt, size_t hdrlen)
1045{
1046        struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1047        u16 fc = le16_to_cpu(hdr->frame_ctl);
1048        int idx = 0;
1049
1050        if (ieee->host_decrypt) {
1051                if (skb->len >= hdrlen + 3)
1052                        idx = skb->data[hdrlen + 3] >> 6;
1053
1054                *crypt = ieee->crypt_info.crypt[idx];
1055                /* allow NULL decrypt to indicate an station specific override
1056                 * for default encryption
1057                 */
1058                if (*crypt && ((*crypt)->ops == NULL ||
1059                              (*crypt)->ops->decrypt_mpdu == NULL))
1060                        *crypt = NULL;
1061
1062                if (!*crypt && (fc & RTLLIB_FCTL_WEP)) {
1063                        /* This seems to be triggered by some (multicast?)
1064                         * frames from other than current BSS, so just drop the
1065                         * frames silently instead of filling system log with
1066                         * these reports.
1067                         */
1068                        netdev_dbg(ieee->dev,
1069                                   "Decryption failed (not set) (SA= %pM)\n",
1070                                   hdr->addr2);
1071                        return -1;
1072                }
1073        }
1074
1075        return 0;
1076}
1077
1078static int rtllib_rx_decrypt(struct rtllib_device *ieee, struct sk_buff *skb,
1079                      struct rtllib_rx_stats *rx_stats,
1080                      struct lib80211_crypt_data *crypt, size_t hdrlen)
1081{
1082        struct rtllib_hdr_4addr *hdr;
1083        int keyidx = 0;
1084        u16 fc, sc;
1085        u8 frag;
1086
1087        hdr = (struct rtllib_hdr_4addr *)skb->data;
1088        fc = le16_to_cpu(hdr->frame_ctl);
1089        sc = le16_to_cpu(hdr->seq_ctl);
1090        frag = WLAN_GET_SEQ_FRAG(sc);
1091
1092        if ((!rx_stats->Decrypted))
1093                ieee->need_sw_enc = 1;
1094        else
1095                ieee->need_sw_enc = 0;
1096
1097        keyidx = rtllib_rx_frame_decrypt(ieee, skb, crypt);
1098        if (ieee->host_decrypt && (fc & RTLLIB_FCTL_WEP) && (keyidx < 0)) {
1099                netdev_info(ieee->dev, "%s: decrypt frame error\n", __func__);
1100                return -1;
1101        }
1102
1103        hdr = (struct rtllib_hdr_4addr *) skb->data;
1104        if ((frag != 0 || (fc & RTLLIB_FCTL_MOREFRAGS))) {
1105                int flen;
1106                struct sk_buff *frag_skb = rtllib_frag_cache_get(ieee, hdr);
1107
1108                netdev_dbg(ieee->dev, "Rx Fragment received (%u)\n", frag);
1109
1110                if (!frag_skb) {
1111                        netdev_dbg(ieee->dev,
1112                                   "Rx cannot get skb from fragment cache (morefrag=%d seq=%u frag=%u)\n",
1113                                   (fc & RTLLIB_FCTL_MOREFRAGS) != 0,
1114                                   WLAN_GET_SEQ_SEQ(sc), frag);
1115                        return -1;
1116                }
1117                flen = skb->len;
1118                if (frag != 0)
1119                        flen -= hdrlen;
1120
1121                if (frag_skb->tail + flen > frag_skb->end) {
1122                        netdev_warn(ieee->dev,
1123                                    "%s: host decrypted and reassembled frame did not fit skb\n",
1124                                    __func__);
1125                        rtllib_frag_cache_invalidate(ieee, hdr);
1126                        return -1;
1127                }
1128
1129                if (frag == 0) {
1130                        /* copy first fragment (including full headers) into
1131                         * beginning of the fragment cache skb
1132                         */
1133                        skb_put_data(frag_skb, skb->data, flen);
1134                } else {
1135                        /* append frame payload to the end of the fragment
1136                         * cache skb
1137                         */
1138                        skb_put_data(frag_skb, skb->data + hdrlen, flen);
1139                }
1140                dev_kfree_skb_any(skb);
1141                skb = NULL;
1142
1143                if (fc & RTLLIB_FCTL_MOREFRAGS) {
1144                        /* more fragments expected - leave the skb in fragment
1145                         * cache for now; it will be delivered to upper layers
1146                         * after all fragments have been received
1147                         */
1148                        return -2;
1149                }
1150
1151                /* this was the last fragment and the frame will be
1152                 * delivered, so remove skb from fragment cache
1153                 */
1154                skb = frag_skb;
1155                hdr = (struct rtllib_hdr_4addr *) skb->data;
1156                rtllib_frag_cache_invalidate(ieee, hdr);
1157        }
1158
1159        /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
1160         * encrypted/authenticated
1161         */
1162        if (ieee->host_decrypt && (fc & RTLLIB_FCTL_WEP) &&
1163                rtllib_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt)) {
1164                netdev_info(ieee->dev, "%s: ==>decrypt msdu error\n", __func__);
1165                return -1;
1166        }
1167
1168        hdr = (struct rtllib_hdr_4addr *) skb->data;
1169        if (crypt && !(fc & RTLLIB_FCTL_WEP) && !ieee->open_wep) {
1170                if (/*ieee->ieee802_1x &&*/
1171                    rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1172
1173                        /* pass unencrypted EAPOL frames even if encryption is
1174                         * configured
1175                         */
1176                        struct eapol *eap = (struct eapol *)(skb->data +
1177                                24);
1178                        netdev_dbg(ieee->dev,
1179                                   "RX: IEEE 802.1X EAPOL frame: %s\n",
1180                                   eap_get_type(eap->type));
1181                } else {
1182                        netdev_dbg(ieee->dev,
1183                                   "encryption configured, but RX frame not encrypted (SA= %pM)\n",
1184                                   hdr->addr2);
1185                        return -1;
1186                }
1187        }
1188
1189        if (crypt && !(fc & RTLLIB_FCTL_WEP) &&
1190            rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1191                struct eapol *eap = (struct eapol *)(skb->data + 24);
1192
1193                netdev_dbg(ieee->dev, "RX: IEEE 802.1X EAPOL frame: %s\n",
1194                           eap_get_type(eap->type));
1195        }
1196
1197        if (crypt && !(fc & RTLLIB_FCTL_WEP) && !ieee->open_wep &&
1198            !rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1199                netdev_dbg(ieee->dev,
1200                           "dropped unencrypted RX data frame from %pM (drop_unencrypted=1)\n",
1201                           hdr->addr2);
1202                return -1;
1203        }
1204
1205        return 0;
1206}
1207
1208static void rtllib_rx_check_leave_lps(struct rtllib_device *ieee, u8 unicast,
1209                                      u8 nr_subframes)
1210{
1211        if (unicast) {
1212
1213                if (ieee->state == RTLLIB_LINKED) {
1214                        if (((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +
1215                            ieee->LinkDetectInfo.NumTxOkInPeriod) > 8) ||
1216                            (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2)) {
1217                                if (ieee->LeisurePSLeave)
1218                                        ieee->LeisurePSLeave(ieee->dev);
1219                        }
1220                }
1221        }
1222        ieee->last_rx_ps_time = jiffies;
1223}
1224
1225static void rtllib_rx_indicate_pkt_legacy(struct rtllib_device *ieee,
1226                struct rtllib_rx_stats *rx_stats,
1227                struct rtllib_rxb *rxb,
1228                u8 *dst,
1229                u8 *src)
1230{
1231        struct net_device *dev = ieee->dev;
1232        u16 ethertype;
1233        int i = 0;
1234
1235        if (rxb == NULL) {
1236                netdev_info(dev, "%s: rxb is NULL!!\n", __func__);
1237                return;
1238        }
1239
1240        for (i = 0; i < rxb->nr_subframes; i++) {
1241                struct sk_buff *sub_skb = rxb->subframes[i];
1242
1243                if (sub_skb) {
1244                        /* convert hdr + possible LLC headers
1245                         * into Ethernet header
1246                         */
1247                        ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
1248                        if (sub_skb->len >= 8 &&
1249                                ((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
1250                                ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1251                                memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
1252                                /* remove RFC1042 or Bridge-Tunnel encapsulation
1253                                 * and replace EtherType
1254                                 */
1255                                skb_pull(sub_skb, SNAP_SIZE);
1256                                ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1257                                                src);
1258                                ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1259                                                dst);
1260                        } else {
1261                                u16 len;
1262                                /* Leave Ethernet header part of hdr
1263                                 * and full payload
1264                                 */
1265                                len = sub_skb->len;
1266                                memcpy(skb_push(sub_skb, 2), &len, 2);
1267                                ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1268                                                src);
1269                                ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1270                                                dst);
1271                        }
1272
1273                        ieee->stats.rx_packets++;
1274                        ieee->stats.rx_bytes += sub_skb->len;
1275
1276                        if (is_multicast_ether_addr(dst))
1277                                ieee->stats.multicast++;
1278
1279                        /* Indicate the packets to upper layer */
1280                        memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
1281                        sub_skb->protocol = eth_type_trans(sub_skb, dev);
1282                        sub_skb->dev = dev;
1283                        sub_skb->dev->stats.rx_packets++;
1284                        sub_skb->dev->stats.rx_bytes += sub_skb->len;
1285                        /* 802.11 crc not sufficient */
1286                        sub_skb->ip_summed = CHECKSUM_NONE;
1287                        netif_rx(sub_skb);
1288                }
1289        }
1290        kfree(rxb);
1291}
1292
1293static int rtllib_rx_InfraAdhoc(struct rtllib_device *ieee, struct sk_buff *skb,
1294                 struct rtllib_rx_stats *rx_stats)
1295{
1296        struct net_device *dev = ieee->dev;
1297        struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1298        struct lib80211_crypt_data *crypt = NULL;
1299        struct rtllib_rxb *rxb = NULL;
1300        struct rx_ts_record *pTS = NULL;
1301        u16 fc, sc, SeqNum = 0;
1302        u8 type, stype, multicast = 0, unicast = 0, nr_subframes = 0, TID = 0;
1303        u8 *payload;
1304        u8 dst[ETH_ALEN];
1305        u8 src[ETH_ALEN];
1306        u8 bssid[ETH_ALEN] = {0};
1307
1308        size_t hdrlen = 0;
1309        bool bToOtherSTA = false;
1310        int ret = 0, i = 0;
1311
1312        fc = le16_to_cpu(hdr->frame_ctl);
1313        type = WLAN_FC_GET_TYPE(fc);
1314        stype = WLAN_FC_GET_STYPE(fc);
1315        sc = le16_to_cpu(hdr->seq_ctl);
1316
1317        /*Filter pkt not to me*/
1318        multicast = is_multicast_ether_addr(hdr->addr1);
1319        unicast = !multicast;
1320        if (unicast && !ether_addr_equal(dev->dev_addr, hdr->addr1)) {
1321                if (ieee->bNetPromiscuousMode)
1322                        bToOtherSTA = true;
1323                else
1324                        goto rx_dropped;
1325        }
1326
1327        /*Filter pkt has too small length */
1328        hdrlen = rtllib_rx_get_hdrlen(ieee, skb, rx_stats);
1329        if (skb->len < hdrlen) {
1330                netdev_info(dev,
1331                            "%s():ERR!!! skb->len is smaller than hdrlen\n",
1332                            __func__);
1333                goto rx_dropped;
1334        }
1335
1336        /* Filter Duplicate pkt */
1337        ret = rtllib_rx_check_duplicate(ieee, skb, multicast);
1338        if (ret < 0)
1339                goto rx_dropped;
1340
1341        /* Filter CTRL Frame */
1342        if (type == RTLLIB_FTYPE_CTL)
1343                goto rx_dropped;
1344
1345        /* Filter MGNT Frame */
1346        if (type == RTLLIB_FTYPE_MGMT) {
1347                if (bToOtherSTA)
1348                        goto rx_dropped;
1349                if (rtllib_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
1350                        goto rx_dropped;
1351                else
1352                        goto rx_exit;
1353        }
1354
1355        /* Filter WAPI DATA Frame */
1356
1357        /* Update statstics for AP roaming */
1358        if (!bToOtherSTA) {
1359                ieee->LinkDetectInfo.NumRecvDataInPeriod++;
1360                ieee->LinkDetectInfo.NumRxOkInPeriod++;
1361        }
1362
1363        /* Data frame - extract src/dst addresses */
1364        rtllib_rx_extract_addr(ieee, hdr, dst, src, bssid);
1365
1366        /* Filter Data frames */
1367        ret = rtllib_rx_data_filter(ieee, fc, dst, src, bssid, hdr->addr2);
1368        if (ret < 0)
1369                goto rx_dropped;
1370
1371        if (skb->len == hdrlen)
1372                goto rx_dropped;
1373
1374        /* Send pspoll based on moredata */
1375        if ((ieee->iw_mode == IW_MODE_INFRA)  &&
1376            (ieee->sta_sleep == LPS_IS_SLEEP) &&
1377            (ieee->polling) && (!bToOtherSTA)) {
1378                if (WLAN_FC_MORE_DATA(fc)) {
1379                        /* more data bit is set, let's request a new frame
1380                         * from the AP
1381                         */
1382                        rtllib_sta_ps_send_pspoll_frame(ieee);
1383                } else {
1384                        ieee->polling =  false;
1385                }
1386        }
1387
1388        /* Get crypt if encrypted */
1389        ret = rtllib_rx_get_crypt(ieee, skb, &crypt, hdrlen);
1390        if (ret == -1)
1391                goto rx_dropped;
1392
1393        /* Decrypt data frame (including reassemble) */
1394        ret = rtllib_rx_decrypt(ieee, skb, rx_stats, crypt, hdrlen);
1395        if (ret == -1)
1396                goto rx_dropped;
1397        else if (ret == -2)
1398                goto rx_exit;
1399
1400        /* Get TS for Rx Reorder  */
1401        hdr = (struct rtllib_hdr_4addr *) skb->data;
1402        if (ieee->current_network.qos_data.active && IsQoSDataFrame(skb->data)
1403                && !is_multicast_ether_addr(hdr->addr1)
1404                && (!bToOtherSTA)) {
1405                TID = Frame_QoSTID(skb->data);
1406                SeqNum = WLAN_GET_SEQ_SEQ(sc);
1407                GetTs(ieee, (struct ts_common_info **) &pTS, hdr->addr2, TID,
1408                      RX_DIR, true);
1409                if (TID != 0 && TID != 3)
1410                        ieee->bis_any_nonbepkts = true;
1411        }
1412
1413        /* Parse rx data frame (For AMSDU) */
1414        /* skb: hdr + (possible reassembled) full plaintext payload */
1415        payload = skb->data + hdrlen;
1416        rxb = kmalloc(sizeof(struct rtllib_rxb), GFP_ATOMIC);
1417        if (!rxb)
1418                goto rx_dropped;
1419
1420        /* to parse amsdu packets */
1421        /* qos data packets & reserved bit is 1 */
1422        if (parse_subframe(ieee, skb, rx_stats, rxb, src, dst) == 0) {
1423                /* only to free rxb, and not submit the packets
1424                 * to upper layer
1425                 */
1426                for (i = 0; i < rxb->nr_subframes; i++)
1427                        dev_kfree_skb(rxb->subframes[i]);
1428                kfree(rxb);
1429                rxb = NULL;
1430                goto rx_dropped;
1431        }
1432
1433        /* Update WAPI PN */
1434
1435        /* Check if leave LPS */
1436        if (!bToOtherSTA) {
1437                if (ieee->bIsAggregateFrame)
1438                        nr_subframes = rxb->nr_subframes;
1439                else
1440                        nr_subframes = 1;
1441                if (unicast)
1442                        ieee->LinkDetectInfo.NumRxUnicastOkInPeriod += nr_subframes;
1443                rtllib_rx_check_leave_lps(ieee, unicast, nr_subframes);
1444        }
1445
1446        /* Indicate packets to upper layer or Rx Reorder */
1447        if (ieee->pHTInfo->bCurRxReorderEnable == false || pTS == NULL ||
1448            bToOtherSTA)
1449                rtllib_rx_indicate_pkt_legacy(ieee, rx_stats, rxb, dst, src);
1450        else
1451                RxReorderIndicatePacket(ieee, rxb, pTS, SeqNum);
1452
1453        dev_kfree_skb(skb);
1454
1455 rx_exit:
1456        return 1;
1457
1458 rx_dropped:
1459        ieee->stats.rx_dropped++;
1460
1461        /* Returning 0 indicates to caller that we have not handled the SKB--
1462         * so it is still allocated and can be used again by underlying
1463         * hardware as a DMA target
1464         */
1465        return 0;
1466}
1467
1468static int rtllib_rx_Master(struct rtllib_device *ieee, struct sk_buff *skb,
1469                 struct rtllib_rx_stats *rx_stats)
1470{
1471        return 0;
1472}
1473
1474static int rtllib_rx_Monitor(struct rtllib_device *ieee, struct sk_buff *skb,
1475                 struct rtllib_rx_stats *rx_stats)
1476{
1477        struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1478        u16 fc = le16_to_cpu(hdr->frame_ctl);
1479        size_t hdrlen = rtllib_get_hdrlen(fc);
1480
1481        if (skb->len < hdrlen) {
1482                netdev_info(ieee->dev,
1483                            "%s():ERR!!! skb->len is smaller than hdrlen\n",
1484                            __func__);
1485                return 0;
1486        }
1487
1488        if (HTCCheck(ieee, skb->data)) {
1489                if (net_ratelimit())
1490                        netdev_info(ieee->dev, "%s: Find HTCControl!\n",
1491                                    __func__);
1492                hdrlen += 4;
1493        }
1494
1495        rtllib_monitor_rx(ieee, skb, rx_stats, hdrlen);
1496        ieee->stats.rx_packets++;
1497        ieee->stats.rx_bytes += skb->len;
1498
1499        return 1;
1500}
1501
1502static int rtllib_rx_Mesh(struct rtllib_device *ieee, struct sk_buff *skb,
1503                 struct rtllib_rx_stats *rx_stats)
1504{
1505        return 0;
1506}
1507
1508/* All received frames are sent to this function. @skb contains the frame in
1509 * IEEE 802.11 format, i.e., in the format it was sent over air.
1510 * This function is called only as a tasklet (software IRQ).
1511 */
1512int rtllib_rx(struct rtllib_device *ieee, struct sk_buff *skb,
1513                 struct rtllib_rx_stats *rx_stats)
1514{
1515        int ret = 0;
1516
1517        if (!ieee || !skb || !rx_stats) {
1518                pr_info("%s: Input parameters NULL!\n", __func__);
1519                goto rx_dropped;
1520        }
1521        if (skb->len < 10) {
1522                netdev_info(ieee->dev, "%s: SKB length < 10\n", __func__);
1523                goto rx_dropped;
1524        }
1525
1526        switch (ieee->iw_mode) {
1527        case IW_MODE_ADHOC:
1528        case IW_MODE_INFRA:
1529                ret = rtllib_rx_InfraAdhoc(ieee, skb, rx_stats);
1530                break;
1531        case IW_MODE_MASTER:
1532        case IW_MODE_REPEAT:
1533                ret = rtllib_rx_Master(ieee, skb, rx_stats);
1534                break;
1535        case IW_MODE_MONITOR:
1536                ret = rtllib_rx_Monitor(ieee, skb, rx_stats);
1537                break;
1538        case IW_MODE_MESH:
1539                ret = rtllib_rx_Mesh(ieee, skb, rx_stats);
1540                break;
1541        default:
1542                netdev_info(ieee->dev, "%s: ERR iw mode!!!\n", __func__);
1543                break;
1544        }
1545
1546        return ret;
1547
1548 rx_dropped:
1549        if (ieee)
1550                ieee->stats.rx_dropped++;
1551        return 0;
1552}
1553EXPORT_SYMBOL(rtllib_rx);
1554
1555static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
1556
1557/* Make ther structure we read from the beacon packet has the right values */
1558static int rtllib_verify_qos_info(struct rtllib_qos_information_element
1559                                     *info_element, int sub_type)
1560{
1561
1562        if (info_element->qui_subtype != sub_type)
1563                return -1;
1564        if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
1565                return -1;
1566        if (info_element->qui_type != QOS_OUI_TYPE)
1567                return -1;
1568        if (info_element->version != QOS_VERSION_1)
1569                return -1;
1570
1571        return 0;
1572}
1573
1574
1575/* Parse a QoS parameter element */
1576static int rtllib_read_qos_param_element(struct rtllib_qos_parameter_info
1577                                                        *element_param,
1578                                         struct rtllib_info_element
1579                                                        *info_element)
1580{
1581        int ret = 0;
1582        u16 size = sizeof(struct rtllib_qos_parameter_info) - 2;
1583
1584        if ((info_element == NULL) || (element_param == NULL))
1585                return -1;
1586
1587        if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
1588                memcpy(element_param->info_element.qui, info_element->data,
1589                       info_element->len);
1590                element_param->info_element.elementID = info_element->id;
1591                element_param->info_element.length = info_element->len;
1592        } else
1593                ret = -1;
1594        if (ret == 0)
1595                ret = rtllib_verify_qos_info(&element_param->info_element,
1596                                                QOS_OUI_PARAM_SUB_TYPE);
1597        return ret;
1598}
1599
1600/* Parse a QoS information element */
1601static int rtllib_read_qos_info_element(struct rtllib_qos_information_element
1602                                                        *element_info,
1603                                        struct rtllib_info_element
1604                                                        *info_element)
1605{
1606        int ret = 0;
1607        u16 size = sizeof(struct rtllib_qos_information_element) - 2;
1608
1609        if (element_info == NULL)
1610                return -1;
1611        if (info_element == NULL)
1612                return -1;
1613
1614        if ((info_element->id == QOS_ELEMENT_ID) &&
1615            (info_element->len == size)) {
1616                memcpy(element_info->qui, info_element->data,
1617                       info_element->len);
1618                element_info->elementID = info_element->id;
1619                element_info->length = info_element->len;
1620        } else
1621                ret = -1;
1622
1623        if (ret == 0)
1624                ret = rtllib_verify_qos_info(element_info,
1625                                             QOS_OUI_INFO_SUB_TYPE);
1626        return ret;
1627}
1628
1629
1630/* Write QoS parameters from the ac parameters. */
1631static int rtllib_qos_convert_ac_to_parameters(struct rtllib_qos_parameter_info *param_elm,
1632                                               struct rtllib_qos_data *qos_data)
1633{
1634        struct rtllib_qos_ac_parameter *ac_params;
1635        struct rtllib_qos_parameters *qos_param = &(qos_data->parameters);
1636        int i;
1637        u8 aci;
1638        u8 acm;
1639
1640        qos_data->wmm_acm = 0;
1641        for (i = 0; i < QOS_QUEUE_NUM; i++) {
1642                ac_params = &(param_elm->ac_params_record[i]);
1643
1644                aci = (ac_params->aci_aifsn & 0x60) >> 5;
1645                acm = (ac_params->aci_aifsn & 0x10) >> 4;
1646
1647                if (aci >= QOS_QUEUE_NUM)
1648                        continue;
1649                switch (aci) {
1650                case 1:
1651                        /* BIT(0) | BIT(3) */
1652                        if (acm)
1653                                qos_data->wmm_acm |= (0x01<<0)|(0x01<<3);
1654                        break;
1655                case 2:
1656                        /* BIT(4) | BIT(5) */
1657                        if (acm)
1658                                qos_data->wmm_acm |= (0x01<<4)|(0x01<<5);
1659                        break;
1660                case 3:
1661                        /* BIT(6) | BIT(7) */
1662                        if (acm)
1663                                qos_data->wmm_acm |= (0x01<<6)|(0x01<<7);
1664                        break;
1665                case 0:
1666                default:
1667                        /* BIT(1) | BIT(2) */
1668                        if (acm)
1669                                qos_data->wmm_acm |= (0x01<<1)|(0x01<<2);
1670                        break;
1671                }
1672
1673                qos_param->aifs[aci] = (ac_params->aci_aifsn) & 0x0f;
1674
1675                /* WMM spec P.11: The minimum value for AIFSN shall be 2 */
1676                qos_param->aifs[aci] = max_t(u8, qos_param->aifs[aci], 2);
1677
1678                qos_param->cw_min[aci] = cpu_to_le16(ac_params->ecw_min_max &
1679                                                     0x0F);
1680
1681                qos_param->cw_max[aci] = cpu_to_le16((ac_params->ecw_min_max &
1682                                                      0xF0) >> 4);
1683
1684                qos_param->flag[aci] =
1685                    (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
1686                qos_param->tx_op_limit[aci] = ac_params->tx_op_limit;
1687        }
1688        return 0;
1689}
1690
1691/* we have a generic data element which it may contain QoS information or
1692 * parameters element. check the information element length to decide
1693 * which type to read
1694 */
1695static int rtllib_parse_qos_info_param_IE(struct rtllib_device *ieee,
1696                                          struct rtllib_info_element
1697                                             *info_element,
1698                                          struct rtllib_network *network)
1699{
1700        int rc = 0;
1701        struct rtllib_qos_information_element qos_info_element;
1702
1703        rc = rtllib_read_qos_info_element(&qos_info_element, info_element);
1704
1705        if (rc == 0) {
1706                network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
1707                network->flags |= NETWORK_HAS_QOS_INFORMATION;
1708        } else {
1709                struct rtllib_qos_parameter_info param_element;
1710
1711                rc = rtllib_read_qos_param_element(&param_element,
1712                                                      info_element);
1713                if (rc == 0) {
1714                        rtllib_qos_convert_ac_to_parameters(&param_element,
1715                                                               &(network->qos_data));
1716                        network->flags |= NETWORK_HAS_QOS_PARAMETERS;
1717                        network->qos_data.param_count =
1718                            param_element.info_element.ac_info & 0x0F;
1719                }
1720        }
1721
1722        if (rc == 0) {
1723                netdev_dbg(ieee->dev, "QoS is supported\n");
1724                network->qos_data.supported = 1;
1725        }
1726        return rc;
1727}
1728
1729static const char *get_info_element_string(u16 id)
1730{
1731        switch (id) {
1732        case MFIE_TYPE_SSID:
1733                return "SSID";
1734        case MFIE_TYPE_RATES:
1735                return "RATES";
1736        case MFIE_TYPE_FH_SET:
1737                return "FH_SET";
1738        case MFIE_TYPE_DS_SET:
1739                return "DS_SET";
1740        case MFIE_TYPE_CF_SET:
1741                return "CF_SET";
1742        case MFIE_TYPE_TIM:
1743                return "TIM";
1744        case MFIE_TYPE_IBSS_SET:
1745                return "IBSS_SET";
1746        case MFIE_TYPE_COUNTRY:
1747                return "COUNTRY";
1748        case MFIE_TYPE_HOP_PARAMS:
1749                return "HOP_PARAMS";
1750        case MFIE_TYPE_HOP_TABLE:
1751                return "HOP_TABLE";
1752        case MFIE_TYPE_REQUEST:
1753                return "REQUEST";
1754        case MFIE_TYPE_CHALLENGE:
1755                return "CHALLENGE";
1756        case MFIE_TYPE_POWER_CONSTRAINT:
1757                return "POWER_CONSTRAINT";
1758        case MFIE_TYPE_POWER_CAPABILITY:
1759                return "POWER_CAPABILITY";
1760        case MFIE_TYPE_TPC_REQUEST:
1761                return "TPC_REQUEST";
1762        case MFIE_TYPE_TPC_REPORT:
1763                return "TPC_REPORT";
1764        case MFIE_TYPE_SUPP_CHANNELS:
1765                return "SUPP_CHANNELS";
1766        case MFIE_TYPE_CSA:
1767                return "CSA";
1768        case MFIE_TYPE_MEASURE_REQUEST:
1769                return "MEASURE_REQUEST";
1770        case MFIE_TYPE_MEASURE_REPORT:
1771                return "MEASURE_REPORT";
1772        case MFIE_TYPE_QUIET:
1773                return "QUIET";
1774        case MFIE_TYPE_IBSS_DFS:
1775                return "IBSS_DFS";
1776        case MFIE_TYPE_RSN:
1777                return "RSN";
1778        case MFIE_TYPE_RATES_EX:
1779                return "RATES_EX";
1780        case MFIE_TYPE_GENERIC:
1781                return "GENERIC";
1782        case MFIE_TYPE_QOS_PARAMETER:
1783                return "QOS_PARAMETER";
1784        default:
1785                return "UNKNOWN";
1786        }
1787}
1788
1789static inline void rtllib_extract_country_ie(
1790        struct rtllib_device *ieee,
1791        struct rtllib_info_element *info_element,
1792        struct rtllib_network *network,
1793        u8 *addr2)
1794{
1795        if (IS_DOT11D_ENABLE(ieee)) {
1796                if (info_element->len != 0) {
1797                        memcpy(network->CountryIeBuf, info_element->data,
1798                               info_element->len);
1799                        network->CountryIeLen = info_element->len;
1800
1801                        if (!IS_COUNTRY_IE_VALID(ieee)) {
1802                                if (rtllib_act_scanning(ieee, false) &&
1803                                    ieee->FirstIe_InScan)
1804                                        netdev_info(ieee->dev,
1805                                                    "Received beacon ContryIE, SSID: <%s>\n",
1806                                                    network->ssid);
1807                                dot11d_update_country(ieee, addr2,
1808                                                       info_element->len,
1809                                                       info_element->data);
1810                        }
1811                }
1812
1813                if (IS_EQUAL_CIE_SRC(ieee, addr2))
1814                        UPDATE_CIE_WATCHDOG(ieee);
1815        }
1816}
1817
1818static void rtllib_parse_mife_generic(struct rtllib_device *ieee,
1819                                      struct rtllib_info_element *info_element,
1820                                      struct rtllib_network *network,
1821                                      u16 *tmp_htcap_len,
1822                                      u16 *tmp_htinfo_len)
1823{
1824        u16 ht_realtek_agg_len = 0;
1825        u8  ht_realtek_agg_buf[MAX_IE_LEN];
1826
1827        if (!rtllib_parse_qos_info_param_IE(ieee, info_element, network))
1828                return;
1829        if (info_element->len >= 4 &&
1830            info_element->data[0] == 0x00 &&
1831            info_element->data[1] == 0x50 &&
1832            info_element->data[2] == 0xf2 &&
1833            info_element->data[3] == 0x01) {
1834                network->wpa_ie_len = min(info_element->len + 2,
1835                                          MAX_WPA_IE_LEN);
1836                memcpy(network->wpa_ie, info_element, network->wpa_ie_len);
1837                return;
1838        }
1839        if (info_element->len == 7 &&
1840            info_element->data[0] == 0x00 &&
1841            info_element->data[1] == 0xe0 &&
1842            info_element->data[2] == 0x4c &&
1843            info_element->data[3] == 0x01 &&
1844            info_element->data[4] == 0x02)
1845                network->Turbo_Enable = 1;
1846
1847        if (*tmp_htcap_len == 0) {
1848                if (info_element->len >= 4 &&
1849                    info_element->data[0] == 0x00 &&
1850                    info_element->data[1] == 0x90 &&
1851                    info_element->data[2] == 0x4c &&
1852                    info_element->data[3] == 0x033) {
1853                        *tmp_htcap_len = min_t(u8, info_element->len,
1854                                               MAX_IE_LEN);
1855                        if (*tmp_htcap_len != 0) {
1856                                network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
1857                                network->bssht.bdHTCapLen = min_t(u16, *tmp_htcap_len, sizeof(network->bssht.bdHTCapBuf));
1858                                memcpy(network->bssht.bdHTCapBuf,
1859                                       info_element->data,
1860                                       network->bssht.bdHTCapLen);
1861                        }
1862                }
1863                if (*tmp_htcap_len != 0) {
1864                        network->bssht.bdSupportHT = true;
1865                        network->bssht.bdHT1R = ((((struct ht_capab_ele *)(network->bssht.bdHTCapBuf))->MCS[1]) == 0);
1866                } else {
1867                        network->bssht.bdSupportHT = false;
1868                        network->bssht.bdHT1R = false;
1869                }
1870        }
1871
1872
1873        if (*tmp_htinfo_len == 0) {
1874                if (info_element->len >= 4 &&
1875                    info_element->data[0] == 0x00 &&
1876                    info_element->data[1] == 0x90 &&
1877                    info_element->data[2] == 0x4c &&
1878                    info_element->data[3] == 0x034) {
1879                        *tmp_htinfo_len = min_t(u8, info_element->len,
1880                                                MAX_IE_LEN);
1881                        if (*tmp_htinfo_len != 0) {
1882                                network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
1883                                network->bssht.bdHTInfoLen = min_t(u16, *tmp_htinfo_len, sizeof(network->bssht.bdHTInfoBuf));
1884                                memcpy(network->bssht.bdHTInfoBuf,
1885                                       info_element->data,
1886                                       network->bssht.bdHTInfoLen);
1887                        }
1888                }
1889        }
1890
1891        if (network->bssht.bdSupportHT) {
1892                if (info_element->len >= 4 &&
1893                    info_element->data[0] == 0x00 &&
1894                    info_element->data[1] == 0xe0 &&
1895                    info_element->data[2] == 0x4c &&
1896                    info_element->data[3] == 0x02) {
1897                        ht_realtek_agg_len = min_t(u8, info_element->len,
1898                                                   MAX_IE_LEN);
1899                        memcpy(ht_realtek_agg_buf, info_element->data,
1900                               info_element->len);
1901                }
1902                if (ht_realtek_agg_len >= 5) {
1903                        network->realtek_cap_exit = true;
1904                        network->bssht.bdRT2RTAggregation = true;
1905
1906                        if ((ht_realtek_agg_buf[4] == 1) &&
1907                            (ht_realtek_agg_buf[5] & 0x02))
1908                                network->bssht.bdRT2RTLongSlotTime = true;
1909
1910                        if ((ht_realtek_agg_buf[4] == 1) &&
1911                            (ht_realtek_agg_buf[5] & RT_HT_CAP_USE_92SE))
1912                                network->bssht.RT2RT_HT_Mode |= RT_HT_CAP_USE_92SE;
1913                }
1914        }
1915        if (ht_realtek_agg_len >= 5) {
1916                if ((ht_realtek_agg_buf[5] & RT_HT_CAP_USE_SOFTAP))
1917                        network->bssht.RT2RT_HT_Mode |= RT_HT_CAP_USE_SOFTAP;
1918        }
1919
1920        if ((info_element->len >= 3 &&
1921             info_element->data[0] == 0x00 &&
1922             info_element->data[1] == 0x05 &&
1923             info_element->data[2] == 0xb5) ||
1924             (info_element->len >= 3 &&
1925             info_element->data[0] == 0x00 &&
1926             info_element->data[1] == 0x0a &&
1927             info_element->data[2] == 0xf7) ||
1928             (info_element->len >= 3 &&
1929             info_element->data[0] == 0x00 &&
1930             info_element->data[1] == 0x10 &&
1931             info_element->data[2] == 0x18)) {
1932                network->broadcom_cap_exist = true;
1933        }
1934        if (info_element->len >= 3 &&
1935            info_element->data[0] == 0x00 &&
1936            info_element->data[1] == 0x0c &&
1937            info_element->data[2] == 0x43)
1938                network->ralink_cap_exist = true;
1939        if ((info_element->len >= 3 &&
1940             info_element->data[0] == 0x00 &&
1941             info_element->data[1] == 0x03 &&
1942             info_element->data[2] == 0x7f) ||
1943             (info_element->len >= 3 &&
1944             info_element->data[0] == 0x00 &&
1945             info_element->data[1] == 0x13 &&
1946             info_element->data[2] == 0x74))
1947                network->atheros_cap_exist = true;
1948
1949        if ((info_element->len >= 3 &&
1950             info_element->data[0] == 0x00 &&
1951             info_element->data[1] == 0x50 &&
1952             info_element->data[2] == 0x43))
1953                network->marvell_cap_exist = true;
1954        if (info_element->len >= 3 &&
1955            info_element->data[0] == 0x00 &&
1956            info_element->data[1] == 0x40 &&
1957            info_element->data[2] == 0x96)
1958                network->cisco_cap_exist = true;
1959
1960
1961        if (info_element->len >= 3 &&
1962            info_element->data[0] == 0x00 &&
1963            info_element->data[1] == 0x0a &&
1964            info_element->data[2] == 0xf5)
1965                network->airgo_cap_exist = true;
1966
1967        if (info_element->len > 4 &&
1968            info_element->data[0] == 0x00 &&
1969            info_element->data[1] == 0x40 &&
1970            info_element->data[2] == 0x96 &&
1971            info_element->data[3] == 0x01) {
1972                if (info_element->len == 6) {
1973                        memcpy(network->CcxRmState, &info_element[4], 2);
1974                        if (network->CcxRmState[0] != 0)
1975                                network->bCcxRmEnable = true;
1976                        else
1977                                network->bCcxRmEnable = false;
1978                        network->MBssidMask = network->CcxRmState[1] & 0x07;
1979                        if (network->MBssidMask != 0) {
1980                                network->bMBssidValid = true;
1981                                network->MBssidMask = 0xff <<
1982                                                      (network->MBssidMask);
1983                                ether_addr_copy(network->MBssid,
1984                                                network->bssid);
1985                                network->MBssid[5] &= network->MBssidMask;
1986                        } else {
1987                                network->bMBssidValid = false;
1988                        }
1989                } else {
1990                        network->bCcxRmEnable = false;
1991                }
1992        }
1993        if (info_element->len > 4  &&
1994            info_element->data[0] == 0x00 &&
1995            info_element->data[1] == 0x40 &&
1996            info_element->data[2] == 0x96 &&
1997            info_element->data[3] == 0x03) {
1998                if (info_element->len == 5) {
1999                        network->bWithCcxVerNum = true;
2000                        network->BssCcxVerNumber = info_element->data[4];
2001                } else {
2002                        network->bWithCcxVerNum = false;
2003                        network->BssCcxVerNumber = 0;
2004                }
2005        }
2006        if (info_element->len > 4  &&
2007            info_element->data[0] == 0x00 &&
2008            info_element->data[1] == 0x50 &&
2009            info_element->data[2] == 0xf2 &&
2010            info_element->data[3] == 0x04) {
2011                netdev_dbg(ieee->dev, "MFIE_TYPE_WZC: %d bytes\n",
2012                           info_element->len);
2013                network->wzc_ie_len = min(info_element->len+2, MAX_WZC_IE_LEN);
2014                memcpy(network->wzc_ie, info_element, network->wzc_ie_len);
2015        }
2016}
2017
2018static void rtllib_parse_mfie_ht_cap(struct rtllib_info_element *info_element,
2019                                     struct rtllib_network *network,
2020                                     u16 *tmp_htcap_len)
2021{
2022        struct bss_ht *ht = &network->bssht;
2023
2024        *tmp_htcap_len = min_t(u8, info_element->len, MAX_IE_LEN);
2025        if (*tmp_htcap_len != 0) {
2026                ht->bdHTSpecVer = HT_SPEC_VER_EWC;
2027                ht->bdHTCapLen = min_t(u16, *tmp_htcap_len,
2028                                       sizeof(ht->bdHTCapBuf));
2029                memcpy(ht->bdHTCapBuf, info_element->data, ht->bdHTCapLen);
2030
2031                ht->bdSupportHT = true;
2032                ht->bdHT1R = ((((struct ht_capab_ele *)
2033                                ht->bdHTCapBuf))->MCS[1]) == 0;
2034
2035                ht->bdBandWidth = (enum ht_channel_width)
2036                                             (((struct ht_capab_ele *)
2037                                             (ht->bdHTCapBuf))->ChlWidth);
2038        } else {
2039                ht->bdSupportHT = false;
2040                ht->bdHT1R = false;
2041                ht->bdBandWidth = HT_CHANNEL_WIDTH_20;
2042        }
2043}
2044
2045int rtllib_parse_info_param(struct rtllib_device *ieee,
2046                struct rtllib_info_element *info_element,
2047                u16 length,
2048                struct rtllib_network *network,
2049                struct rtllib_rx_stats *stats)
2050{
2051        u8 i;
2052        short offset;
2053        u16     tmp_htcap_len = 0;
2054        u16     tmp_htinfo_len = 0;
2055        char rates_str[64];
2056        char *p;
2057
2058        while (length >= sizeof(*info_element)) {
2059                if (sizeof(*info_element) + info_element->len > length) {
2060                        netdev_dbg(ieee->dev,
2061                                   "Info elem: parse failed: info_element->len + 2 > left : info_element->len+2=%zd left=%d, id=%d.\n",
2062                                   info_element->len + sizeof(*info_element),
2063                                   length, info_element->id);
2064                        /* We stop processing but don't return an error here
2065                         * because some misbehaviour APs break this rule. ie.
2066                         * Orinoco AP1000.
2067                         */
2068                        break;
2069                }
2070
2071                switch (info_element->id) {
2072                case MFIE_TYPE_SSID:
2073                        if (rtllib_is_empty_essid(info_element->data,
2074                                                     info_element->len)) {
2075                                network->flags |= NETWORK_EMPTY_ESSID;
2076                                break;
2077                        }
2078
2079                        network->ssid_len = min(info_element->len,
2080                                                (u8) IW_ESSID_MAX_SIZE);
2081                        memcpy(network->ssid, info_element->data,
2082                               network->ssid_len);
2083                        if (network->ssid_len < IW_ESSID_MAX_SIZE)
2084                                memset(network->ssid + network->ssid_len, 0,
2085                                       IW_ESSID_MAX_SIZE - network->ssid_len);
2086
2087                        netdev_dbg(ieee->dev, "MFIE_TYPE_SSID: '%s' len=%d.\n",
2088                                   network->ssid, network->ssid_len);
2089                        break;
2090
2091                case MFIE_TYPE_RATES:
2092                        p = rates_str;
2093                        network->rates_len = min(info_element->len,
2094                                                 MAX_RATES_LENGTH);
2095                        for (i = 0; i < network->rates_len; i++) {
2096                                network->rates[i] = info_element->data[i];
2097                                p += snprintf(p, sizeof(rates_str) -
2098                                              (p - rates_str), "%02X ",
2099                                              network->rates[i]);
2100                                if (rtllib_is_ofdm_rate
2101                                    (info_element->data[i])) {
2102                                        network->flags |= NETWORK_HAS_OFDM;
2103                                        if (info_element->data[i] &
2104                                            RTLLIB_BASIC_RATE_MASK)
2105                                                network->flags &=
2106                                                    ~NETWORK_HAS_CCK;
2107                                }
2108
2109                                if (rtllib_is_cck_rate
2110                                    (info_element->data[i])) {
2111                                        network->flags |= NETWORK_HAS_CCK;
2112                                }
2113                        }
2114
2115                        netdev_dbg(ieee->dev, "MFIE_TYPE_RATES: '%s' (%d)\n",
2116                                   rates_str, network->rates_len);
2117                        break;
2118
2119                case MFIE_TYPE_RATES_EX:
2120                        p = rates_str;
2121                        network->rates_ex_len = min(info_element->len,
2122                                                    MAX_RATES_EX_LENGTH);
2123                        for (i = 0; i < network->rates_ex_len; i++) {
2124                                network->rates_ex[i] = info_element->data[i];
2125                                p += snprintf(p, sizeof(rates_str) -
2126                                              (p - rates_str), "%02X ",
2127                                              network->rates_ex[i]);
2128                                if (rtllib_is_ofdm_rate
2129                                    (info_element->data[i])) {
2130                                        network->flags |= NETWORK_HAS_OFDM;
2131                                        if (info_element->data[i] &
2132                                            RTLLIB_BASIC_RATE_MASK)
2133                                                network->flags &=
2134                                                    ~NETWORK_HAS_CCK;
2135                                }
2136                        }
2137
2138                        netdev_dbg(ieee->dev, "MFIE_TYPE_RATES_EX: '%s' (%d)\n",
2139                                   rates_str, network->rates_ex_len);
2140                        break;
2141
2142                case MFIE_TYPE_DS_SET:
2143                        netdev_dbg(ieee->dev, "MFIE_TYPE_DS_SET: %d\n",
2144                                   info_element->data[0]);
2145                        network->channel = info_element->data[0];
2146                        break;
2147
2148                case MFIE_TYPE_FH_SET:
2149                        netdev_dbg(ieee->dev, "MFIE_TYPE_FH_SET: ignored\n");
2150                        break;
2151
2152                case MFIE_TYPE_CF_SET:
2153                        netdev_dbg(ieee->dev, "MFIE_TYPE_CF_SET: ignored\n");
2154                        break;
2155
2156                case MFIE_TYPE_TIM:
2157                        if (info_element->len < 4)
2158                                break;
2159
2160                        network->tim.tim_count = info_element->data[0];
2161                        network->tim.tim_period = info_element->data[1];
2162
2163                        network->dtim_period = info_element->data[1];
2164                        if (ieee->state != RTLLIB_LINKED)
2165                                break;
2166                        network->last_dtim_sta_time = jiffies;
2167
2168                        network->dtim_data = RTLLIB_DTIM_VALID;
2169
2170
2171                        if (info_element->data[2] & 1)
2172                                network->dtim_data |= RTLLIB_DTIM_MBCAST;
2173
2174                        offset = (info_element->data[2] >> 1)*2;
2175
2176
2177                        if (ieee->assoc_id < 8*offset ||
2178                            ieee->assoc_id > 8*(offset + info_element->len - 3))
2179                                break;
2180
2181                        offset = (ieee->assoc_id / 8) - offset;
2182                        if (info_element->data[3 + offset] &
2183                           (1 << (ieee->assoc_id % 8)))
2184                                network->dtim_data |= RTLLIB_DTIM_UCAST;
2185
2186                        network->listen_interval = network->dtim_period;
2187                        break;
2188
2189                case MFIE_TYPE_ERP:
2190                        network->erp_value = info_element->data[0];
2191                        network->flags |= NETWORK_HAS_ERP_VALUE;
2192                        netdev_dbg(ieee->dev, "MFIE_TYPE_ERP_SET: %d\n",
2193                                   network->erp_value);
2194                        break;
2195                case MFIE_TYPE_IBSS_SET:
2196                        network->atim_window = info_element->data[0];
2197                        netdev_dbg(ieee->dev, "MFIE_TYPE_IBSS_SET: %d\n",
2198                                   network->atim_window);
2199                        break;
2200
2201                case MFIE_TYPE_CHALLENGE:
2202                        netdev_dbg(ieee->dev, "MFIE_TYPE_CHALLENGE: ignored\n");
2203                        break;
2204
2205                case MFIE_TYPE_GENERIC:
2206                        netdev_dbg(ieee->dev, "MFIE_TYPE_GENERIC: %d bytes\n",
2207                                   info_element->len);
2208
2209                        rtllib_parse_mife_generic(ieee, info_element, network,
2210                                                  &tmp_htcap_len,
2211                                                  &tmp_htinfo_len);
2212                        break;
2213
2214                case MFIE_TYPE_RSN:
2215                        netdev_dbg(ieee->dev, "MFIE_TYPE_RSN: %d bytes\n",
2216                                   info_element->len);
2217                        network->rsn_ie_len = min(info_element->len + 2,
2218                                                  MAX_WPA_IE_LEN);
2219                        memcpy(network->rsn_ie, info_element,
2220                               network->rsn_ie_len);
2221                        break;
2222
2223                case MFIE_TYPE_HT_CAP:
2224                        netdev_dbg(ieee->dev, "MFIE_TYPE_HT_CAP: %d bytes\n",
2225                                   info_element->len);
2226
2227                        rtllib_parse_mfie_ht_cap(info_element, network,
2228                                                 &tmp_htcap_len);
2229                        break;
2230
2231
2232                case MFIE_TYPE_HT_INFO:
2233                        netdev_dbg(ieee->dev, "MFIE_TYPE_HT_INFO: %d bytes\n",
2234                                   info_element->len);
2235                        tmp_htinfo_len = min_t(u8, info_element->len,
2236                                               MAX_IE_LEN);
2237                        if (tmp_htinfo_len) {
2238                                network->bssht.bdHTSpecVer = HT_SPEC_VER_IEEE;
2239                                network->bssht.bdHTInfoLen = tmp_htinfo_len >
2240                                        sizeof(network->bssht.bdHTInfoBuf) ?
2241                                        sizeof(network->bssht.bdHTInfoBuf) :
2242                                        tmp_htinfo_len;
2243                                memcpy(network->bssht.bdHTInfoBuf,
2244                                       info_element->data,
2245                                       network->bssht.bdHTInfoLen);
2246                        }
2247                        break;
2248
2249                case MFIE_TYPE_AIRONET:
2250                        netdev_dbg(ieee->dev, "MFIE_TYPE_AIRONET: %d bytes\n",
2251                                   info_element->len);
2252                        if (info_element->len > IE_CISCO_FLAG_POSITION) {
2253                                network->bWithAironetIE = true;
2254
2255                                if ((info_element->data[IE_CISCO_FLAG_POSITION]
2256                                     & SUPPORT_CKIP_MIC) ||
2257                                     (info_element->data[IE_CISCO_FLAG_POSITION]
2258                                     & SUPPORT_CKIP_PK))
2259                                        network->bCkipSupported = true;
2260                                else
2261                                        network->bCkipSupported = false;
2262                        } else {
2263                                network->bWithAironetIE = false;
2264                                network->bCkipSupported = false;
2265                        }
2266                        break;
2267                case MFIE_TYPE_QOS_PARAMETER:
2268                        netdev_err(ieee->dev,
2269                                   "QoS Error need to parse QOS_PARAMETER IE\n");
2270                        break;
2271
2272                case MFIE_TYPE_COUNTRY:
2273                        netdev_dbg(ieee->dev, "MFIE_TYPE_COUNTRY: %d bytes\n",
2274                                   info_element->len);
2275                        rtllib_extract_country_ie(ieee, info_element, network,
2276                                                  network->bssid);
2277                        break;
2278/* TODO */
2279                default:
2280                        netdev_dbg(ieee->dev,
2281                                   "Unsupported info element: %s (%d)\n",
2282                                   get_info_element_string(info_element->id),
2283                                   info_element->id);
2284                        break;
2285                }
2286
2287                length -= sizeof(*info_element) + info_element->len;
2288                info_element =
2289                    (struct rtllib_info_element *)&info_element->
2290                    data[info_element->len];
2291        }
2292
2293        if (!network->atheros_cap_exist && !network->broadcom_cap_exist &&
2294            !network->cisco_cap_exist && !network->ralink_cap_exist &&
2295            !network->bssht.bdRT2RTAggregation)
2296                network->unknown_cap_exist = true;
2297        else
2298                network->unknown_cap_exist = false;
2299        return 0;
2300}
2301
2302static long rtllib_translate_todbm(u8 signal_strength_index)
2303{
2304        long    signal_power;
2305
2306        signal_power = (long)((signal_strength_index + 1) >> 1);
2307        signal_power -= 95;
2308
2309        return signal_power;
2310}
2311
2312static inline int rtllib_network_init(
2313        struct rtllib_device *ieee,
2314        struct rtllib_probe_response *beacon,
2315        struct rtllib_network *network,
2316        struct rtllib_rx_stats *stats)
2317{
2318        memset(&network->qos_data, 0, sizeof(struct rtllib_qos_data));
2319
2320        /* Pull out fixed field data */
2321        ether_addr_copy(network->bssid, beacon->header.addr3);
2322        network->capability = le16_to_cpu(beacon->capability);
2323        network->last_scanned = jiffies;
2324        network->time_stamp[0] = beacon->time_stamp[0];
2325        network->time_stamp[1] = beacon->time_stamp[1];
2326        network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
2327        /* Where to pull this? beacon->listen_interval;*/
2328        network->listen_interval = 0x0A;
2329        network->rates_len = network->rates_ex_len = 0;
2330        network->ssid_len = 0;
2331        network->hidden_ssid_len = 0;
2332        memset(network->hidden_ssid, 0, sizeof(network->hidden_ssid));
2333        network->flags = 0;
2334        network->atim_window = 0;
2335        network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
2336            0x3 : 0x0;
2337        network->berp_info_valid = false;
2338        network->broadcom_cap_exist = false;
2339        network->ralink_cap_exist = false;
2340        network->atheros_cap_exist = false;
2341        network->cisco_cap_exist = false;
2342        network->unknown_cap_exist = false;
2343        network->realtek_cap_exit = false;
2344        network->marvell_cap_exist = false;
2345        network->airgo_cap_exist = false;
2346        network->Turbo_Enable = 0;
2347        network->SignalStrength = stats->SignalStrength;
2348        network->RSSI = stats->SignalStrength;
2349        network->CountryIeLen = 0;
2350        memset(network->CountryIeBuf, 0, MAX_IE_LEN);
2351        HTInitializeBssDesc(&network->bssht);
2352        if (stats->freq == RTLLIB_52GHZ_BAND) {
2353                /* for A band (No DS info) */
2354                network->channel = stats->received_channel;
2355        } else
2356                network->flags |= NETWORK_HAS_CCK;
2357
2358        network->wpa_ie_len = 0;
2359        network->rsn_ie_len = 0;
2360        network->wzc_ie_len = 0;
2361
2362        if (rtllib_parse_info_param(ieee,
2363                        beacon->info_element,
2364                        (stats->len - sizeof(*beacon)),
2365                        network,
2366                        stats))
2367                return 1;
2368
2369        network->mode = 0;
2370        if (stats->freq == RTLLIB_52GHZ_BAND)
2371                network->mode = IEEE_A;
2372        else {
2373                if (network->flags & NETWORK_HAS_OFDM)
2374                        network->mode |= IEEE_G;
2375                if (network->flags & NETWORK_HAS_CCK)
2376                        network->mode |= IEEE_B;
2377        }
2378
2379        if (network->mode == 0) {
2380                netdev_dbg(ieee->dev, "Filtered out '%s (%pM)' network.\n",
2381                           escape_essid(network->ssid, network->ssid_len),
2382                           network->bssid);
2383                return 1;
2384        }
2385
2386        if (network->bssht.bdSupportHT) {
2387                if (network->mode == IEEE_A)
2388                        network->mode = IEEE_N_5G;
2389                else if (network->mode & (IEEE_G | IEEE_B))
2390                        network->mode = IEEE_N_24G;
2391        }
2392        if (rtllib_is_empty_essid(network->ssid, network->ssid_len))
2393                network->flags |= NETWORK_EMPTY_ESSID;
2394        stats->signal = 30 + (stats->SignalStrength * 70) / 100;
2395        stats->noise = rtllib_translate_todbm((u8)(100-stats->signal)) - 25;
2396
2397        memcpy(&network->stats, stats, sizeof(network->stats));
2398
2399        return 0;
2400}
2401
2402static inline int is_same_network(struct rtllib_network *src,
2403                                  struct rtllib_network *dst, u8 ssidbroad)
2404{
2405        /* A network is only a duplicate if the channel, BSSID, ESSID
2406         * and the capability field (in particular IBSS and BSS) all match.
2407         * We treat all <hidden> with the same BSSID and channel
2408         * as one network
2409         */
2410        return (((src->ssid_len == dst->ssid_len) || (!ssidbroad)) &&
2411                (src->channel == dst->channel) &&
2412                !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
2413                (!memcmp(src->ssid, dst->ssid, src->ssid_len) ||
2414                (!ssidbroad)) &&
2415                ((src->capability & WLAN_CAPABILITY_IBSS) ==
2416                (dst->capability & WLAN_CAPABILITY_IBSS)) &&
2417                ((src->capability & WLAN_CAPABILITY_ESS) ==
2418                (dst->capability & WLAN_CAPABILITY_ESS)));
2419}
2420
2421
2422static inline void update_network(struct rtllib_device *ieee,
2423                                  struct rtllib_network *dst,
2424                                  struct rtllib_network *src)
2425{
2426        int qos_active;
2427        u8 old_param;
2428
2429        memcpy(&dst->stats, &src->stats, sizeof(struct rtllib_rx_stats));
2430        dst->capability = src->capability;
2431        memcpy(dst->rates, src->rates, src->rates_len);
2432        dst->rates_len = src->rates_len;
2433        memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
2434        dst->rates_ex_len = src->rates_ex_len;
2435        if (src->ssid_len > 0) {
2436                if (dst->ssid_len == 0) {
2437                        memset(dst->hidden_ssid, 0, sizeof(dst->hidden_ssid));
2438                        dst->hidden_ssid_len = src->ssid_len;
2439                        memcpy(dst->hidden_ssid, src->ssid, src->ssid_len);
2440                } else {
2441                        memset(dst->ssid, 0, dst->ssid_len);
2442                        dst->ssid_len = src->ssid_len;
2443                        memcpy(dst->ssid, src->ssid, src->ssid_len);
2444                }
2445        }
2446        dst->mode = src->mode;
2447        dst->flags = src->flags;
2448        dst->time_stamp[0] = src->time_stamp[0];
2449        dst->time_stamp[1] = src->time_stamp[1];
2450        if (src->flags & NETWORK_HAS_ERP_VALUE) {
2451                dst->erp_value = src->erp_value;
2452                dst->berp_info_valid = src->berp_info_valid = true;
2453        }
2454        dst->beacon_interval = src->beacon_interval;
2455        dst->listen_interval = src->listen_interval;
2456        dst->atim_window = src->atim_window;
2457        dst->dtim_period = src->dtim_period;
2458        dst->dtim_data = src->dtim_data;
2459        dst->last_dtim_sta_time = src->last_dtim_sta_time;
2460        memcpy(&dst->tim, &src->tim, sizeof(struct rtllib_tim_parameters));
2461
2462        dst->bssht.bdSupportHT = src->bssht.bdSupportHT;
2463        dst->bssht.bdRT2RTAggregation = src->bssht.bdRT2RTAggregation;
2464        dst->bssht.bdHTCapLen = src->bssht.bdHTCapLen;
2465        memcpy(dst->bssht.bdHTCapBuf, src->bssht.bdHTCapBuf,
2466               src->bssht.bdHTCapLen);
2467        dst->bssht.bdHTInfoLen = src->bssht.bdHTInfoLen;
2468        memcpy(dst->bssht.bdHTInfoBuf, src->bssht.bdHTInfoBuf,
2469               src->bssht.bdHTInfoLen);
2470        dst->bssht.bdHTSpecVer = src->bssht.bdHTSpecVer;
2471        dst->bssht.bdRT2RTLongSlotTime = src->bssht.bdRT2RTLongSlotTime;
2472        dst->broadcom_cap_exist = src->broadcom_cap_exist;
2473        dst->ralink_cap_exist = src->ralink_cap_exist;
2474        dst->atheros_cap_exist = src->atheros_cap_exist;
2475        dst->realtek_cap_exit = src->realtek_cap_exit;
2476        dst->marvell_cap_exist = src->marvell_cap_exist;
2477        dst->cisco_cap_exist = src->cisco_cap_exist;
2478        dst->airgo_cap_exist = src->airgo_cap_exist;
2479        dst->unknown_cap_exist = src->unknown_cap_exist;
2480        memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
2481        dst->wpa_ie_len = src->wpa_ie_len;
2482        memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
2483        dst->rsn_ie_len = src->rsn_ie_len;
2484        memcpy(dst->wzc_ie, src->wzc_ie, src->wzc_ie_len);
2485        dst->wzc_ie_len = src->wzc_ie_len;
2486
2487        dst->last_scanned = jiffies;
2488        /* qos related parameters */
2489        qos_active = dst->qos_data.active;
2490        old_param = dst->qos_data.param_count;
2491        dst->qos_data.supported = src->qos_data.supported;
2492        if (dst->flags & NETWORK_HAS_QOS_PARAMETERS)
2493                memcpy(&dst->qos_data, &src->qos_data,
2494                       sizeof(struct rtllib_qos_data));
2495        if (dst->qos_data.supported == 1) {
2496                if (dst->ssid_len)
2497                        netdev_dbg(ieee->dev,
2498                                   "QoS the network %s is QoS supported\n",
2499                                   dst->ssid);
2500                else
2501                        netdev_dbg(ieee->dev,
2502                                   "QoS the network is QoS supported\n");
2503        }
2504        dst->qos_data.active = qos_active;
2505        dst->qos_data.old_param_count = old_param;
2506
2507        dst->wmm_info = src->wmm_info;
2508        if (src->wmm_param[0].ac_aci_acm_aifsn ||
2509           src->wmm_param[1].ac_aci_acm_aifsn ||
2510           src->wmm_param[2].ac_aci_acm_aifsn ||
2511           src->wmm_param[3].ac_aci_acm_aifsn)
2512                memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
2513
2514        dst->SignalStrength = src->SignalStrength;
2515        dst->RSSI = src->RSSI;
2516        dst->Turbo_Enable = src->Turbo_Enable;
2517
2518        dst->CountryIeLen = src->CountryIeLen;
2519        memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
2520
2521        dst->bWithAironetIE = src->bWithAironetIE;
2522        dst->bCkipSupported = src->bCkipSupported;
2523        memcpy(dst->CcxRmState, src->CcxRmState, 2);
2524        dst->bCcxRmEnable = src->bCcxRmEnable;
2525        dst->MBssidMask = src->MBssidMask;
2526        dst->bMBssidValid = src->bMBssidValid;
2527        memcpy(dst->MBssid, src->MBssid, 6);
2528        dst->bWithCcxVerNum = src->bWithCcxVerNum;
2529        dst->BssCcxVerNumber = src->BssCcxVerNumber;
2530}
2531
2532static inline int is_beacon(u16 fc)
2533{
2534        return (WLAN_FC_GET_STYPE(fc) == RTLLIB_STYPE_BEACON);
2535}
2536
2537static int IsPassiveChannel(struct rtllib_device *rtllib, u8 channel)
2538{
2539        if (channel > MAX_CHANNEL_NUMBER) {
2540                netdev_info(rtllib->dev, "%s(): Invalid Channel\n", __func__);
2541                return 0;
2542        }
2543
2544        if (rtllib->active_channel_map[channel] == 2)
2545                return 1;
2546
2547        return 0;
2548}
2549
2550int rtllib_legal_channel(struct rtllib_device *rtllib, u8 channel)
2551{
2552        if (channel > MAX_CHANNEL_NUMBER) {
2553                netdev_info(rtllib->dev, "%s(): Invalid Channel\n", __func__);
2554                return 0;
2555        }
2556        if (rtllib->active_channel_map[channel] > 0)
2557                return 1;
2558
2559        return 0;
2560}
2561EXPORT_SYMBOL(rtllib_legal_channel);
2562
2563static inline void rtllib_process_probe_response(
2564        struct rtllib_device *ieee,
2565        struct rtllib_probe_response *beacon,
2566        struct rtllib_rx_stats *stats)
2567{
2568        struct rtllib_network *target;
2569        struct rtllib_network *oldest = NULL;
2570        struct rtllib_info_element *info_element = &beacon->info_element[0];
2571        unsigned long flags;
2572        short renew;
2573        struct rtllib_network *network = kzalloc(sizeof(struct rtllib_network),
2574                                                 GFP_ATOMIC);
2575        u16 frame_ctl = le16_to_cpu(beacon->header.frame_ctl);
2576
2577        if (!network)
2578                return;
2579
2580        netdev_dbg(ieee->dev,
2581                   "'%s' ( %pM ): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
2582                   escape_essid(info_element->data, info_element->len),
2583                   beacon->header.addr3,
2584                   (le16_to_cpu(beacon->capability) & (1<<0xf)) ? '1' : '0',
2585                   (le16_to_cpu(beacon->capability) & (1<<0xe)) ? '1' : '0',
2586                   (le16_to_cpu(beacon->capability) & (1<<0xd)) ? '1' : '0',
2587                   (le16_to_cpu(beacon->capability) & (1<<0xc)) ? '1' : '0',
2588                   (le16_to_cpu(beacon->capability) & (1<<0xb)) ? '1' : '0',
2589                   (le16_to_cpu(beacon->capability) & (1<<0xa)) ? '1' : '0',
2590                   (le16_to_cpu(beacon->capability) & (1<<0x9)) ? '1' : '0',
2591                   (le16_to_cpu(beacon->capability) & (1<<0x8)) ? '1' : '0',
2592                   (le16_to_cpu(beacon->capability) & (1<<0x7)) ? '1' : '0',
2593                   (le16_to_cpu(beacon->capability) & (1<<0x6)) ? '1' : '0',
2594                   (le16_to_cpu(beacon->capability) & (1<<0x5)) ? '1' : '0',
2595                   (le16_to_cpu(beacon->capability) & (1<<0x4)) ? '1' : '0',
2596                   (le16_to_cpu(beacon->capability) & (1<<0x3)) ? '1' : '0',
2597                   (le16_to_cpu(beacon->capability) & (1<<0x2)) ? '1' : '0',
2598                   (le16_to_cpu(beacon->capability) & (1<<0x1)) ? '1' : '0',
2599                   (le16_to_cpu(beacon->capability) & (1<<0x0)) ? '1' : '0');
2600
2601        if (rtllib_network_init(ieee, beacon, network, stats)) {
2602                netdev_dbg(ieee->dev, "Dropped '%s' ( %pM) via %s.\n",
2603                           escape_essid(info_element->data, info_element->len),
2604                           beacon->header.addr3,
2605                           is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
2606                goto free_network;
2607        }
2608
2609
2610        if (!rtllib_legal_channel(ieee, network->channel))
2611                goto free_network;
2612
2613        if (WLAN_FC_GET_STYPE(frame_ctl) == RTLLIB_STYPE_PROBE_RESP) {
2614                if (IsPassiveChannel(ieee, network->channel)) {
2615                        netdev_info(ieee->dev,
2616                                    "GetScanInfo(): For Global Domain, filter probe response at channel(%d).\n",
2617                                    network->channel);
2618                        goto free_network;
2619                }
2620        }
2621
2622        /* The network parsed correctly -- so now we scan our known networks
2623         * to see if we can find it in our list.
2624         *
2625         * NOTE:  This search is definitely not optimized.  Once its doing
2626         *      the "right thing" we'll optimize it for efficiency if
2627         *      necessary
2628         */
2629
2630        /* Search for this entry in the list and update it if it is
2631         * already there.
2632         */
2633
2634        spin_lock_irqsave(&ieee->lock, flags);
2635        if (is_same_network(&ieee->current_network, network,
2636           (network->ssid_len ? 1 : 0))) {
2637                update_network(ieee, &ieee->current_network, network);
2638                if ((ieee->current_network.mode == IEEE_N_24G ||
2639                     ieee->current_network.mode == IEEE_G) &&
2640                    ieee->current_network.berp_info_valid) {
2641                        if (ieee->current_network.erp_value & ERP_UseProtection)
2642                                ieee->current_network.buseprotection = true;
2643                        else
2644                                ieee->current_network.buseprotection = false;
2645                }
2646                if (is_beacon(frame_ctl)) {
2647                        if (ieee->state >= RTLLIB_LINKED)
2648                                ieee->LinkDetectInfo.NumRecvBcnInPeriod++;
2649                }
2650        }
2651        list_for_each_entry(target, &ieee->network_list, list) {
2652                if (is_same_network(target, network,
2653                   (target->ssid_len ? 1 : 0)))
2654                        break;
2655                if ((oldest == NULL) ||
2656                    (target->last_scanned < oldest->last_scanned))
2657                        oldest = target;
2658        }
2659
2660        /* If we didn't find a match, then get a new network slot to initialize
2661         * with this beacon's information
2662         */
2663        if (&target->list == &ieee->network_list) {
2664                if (list_empty(&ieee->network_free_list)) {
2665                        /* If there are no more slots, expire the oldest */
2666                        list_del(&oldest->list);
2667                        target = oldest;
2668                        netdev_dbg(ieee->dev,
2669                                   "Expired '%s' ( %pM) from network list.\n",
2670                                   escape_essid(target->ssid, target->ssid_len),
2671                                   target->bssid);
2672                } else {
2673                        /* Otherwise just pull from the free list */
2674                        target = list_entry(ieee->network_free_list.next,
2675                                            struct rtllib_network, list);
2676                        list_del(ieee->network_free_list.next);
2677                }
2678
2679                netdev_dbg(ieee->dev, "Adding '%s' ( %pM) via %s.\n",
2680                           escape_essid(network->ssid, network->ssid_len),
2681                           network->bssid,
2682                           is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
2683
2684                memcpy(target, network, sizeof(*target));
2685                list_add_tail(&target->list, &ieee->network_list);
2686                if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
2687                        rtllib_softmac_new_net(ieee, network);
2688        } else {
2689                netdev_dbg(ieee->dev, "Updating '%s' ( %pM) via %s.\n",
2690                           escape_essid(target->ssid, target->ssid_len),
2691                           target->bssid,
2692                           is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
2693
2694                /* we have an entry and we are going to update it. But this
2695                 *  entry may be already expired. In this case we do the same
2696                 * as we found a new net and call the new_net handler
2697                 */
2698                renew = !time_after(target->last_scanned + ieee->scan_age,
2699                                    jiffies);
2700                if ((!target->ssid_len) &&
2701                    (((network->ssid_len > 0) && (target->hidden_ssid_len == 0))
2702                    || ((ieee->current_network.ssid_len == network->ssid_len) &&
2703                    (strncmp(ieee->current_network.ssid, network->ssid,
2704                    network->ssid_len) == 0) &&
2705                    (ieee->state == RTLLIB_NOLINK))))
2706                        renew = 1;
2707                update_network(ieee, target, network);
2708                if (renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
2709                        rtllib_softmac_new_net(ieee, network);
2710        }
2711
2712        spin_unlock_irqrestore(&ieee->lock, flags);
2713        if (is_beacon(frame_ctl) &&
2714            is_same_network(&ieee->current_network, network,
2715            (network->ssid_len ? 1 : 0)) &&
2716            (ieee->state == RTLLIB_LINKED)) {
2717                if (ieee->handle_beacon != NULL)
2718                        ieee->handle_beacon(ieee->dev, beacon,
2719                                            &ieee->current_network);
2720        }
2721free_network:
2722        kfree(network);
2723}
2724
2725static void rtllib_rx_mgt(struct rtllib_device *ieee,
2726                          struct sk_buff *skb,
2727                          struct rtllib_rx_stats *stats)
2728{
2729        struct rtllib_hdr_4addr *header = (struct rtllib_hdr_4addr *)skb->data;
2730
2731        if ((WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) !=
2732            RTLLIB_STYPE_PROBE_RESP) &&
2733            (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) !=
2734            RTLLIB_STYPE_BEACON))
2735                ieee->last_rx_ps_time = jiffies;
2736
2737        switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
2738
2739        case RTLLIB_STYPE_BEACON:
2740                netdev_dbg(ieee->dev, "received BEACON (%d)\n",
2741                           WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)));
2742                rtllib_process_probe_response(
2743                                ieee, (struct rtllib_probe_response *)header,
2744                                stats);
2745
2746                if (ieee->sta_sleep || (ieee->ps != RTLLIB_PS_DISABLED &&
2747                    ieee->iw_mode == IW_MODE_INFRA &&
2748                    ieee->state == RTLLIB_LINKED))
2749                        tasklet_schedule(&ieee->ps_task);
2750
2751                break;
2752
2753        case RTLLIB_STYPE_PROBE_RESP:
2754                netdev_dbg(ieee->dev, "received PROBE RESPONSE (%d)\n",
2755                           WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)));
2756                rtllib_process_probe_response(ieee,
2757                              (struct rtllib_probe_response *)header, stats);
2758                break;
2759        case RTLLIB_STYPE_PROBE_REQ:
2760                netdev_dbg(ieee->dev, "received PROBE RESQUEST (%d)\n",
2761                           WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)));
2762                if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
2763                    ((ieee->iw_mode == IW_MODE_ADHOC ||
2764                    ieee->iw_mode == IW_MODE_MASTER) &&
2765                    ieee->state == RTLLIB_LINKED))
2766                        rtllib_rx_probe_rq(ieee, skb);
2767                break;
2768        }
2769}
2770