linux/drivers/staging/rtl8188eu/core/rtw_recv.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of version 2 of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12 * more details.
  13 *
  14 ******************************************************************************/
  15#define _RTW_RECV_C_
  16
  17#include <linux/ieee80211.h>
  18
  19#include <osdep_service.h>
  20#include <drv_types.h>
  21#include <recv_osdep.h>
  22#include <mlme_osdep.h>
  23#include <mon.h>
  24#include <wifi.h>
  25#include <linux/vmalloc.h>
  26
  27#define ETHERNET_HEADER_SIZE    14      /*  Ethernet Header Length */
  28#define LLC_HEADER_SIZE                 6       /*  LLC Header Length */
  29
  30static u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
  31static u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
  32
  33/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  34static u8 rtw_bridge_tunnel_header[] = {
  35       0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8
  36};
  37
  38static u8 rtw_rfc1042_header[] = {
  39       0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
  40};
  41
  42static void rtw_signal_stat_timer_hdl(unsigned long data);
  43
  44void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
  45{
  46
  47        memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
  48
  49        spin_lock_init(&psta_recvpriv->lock);
  50
  51        _rtw_init_queue(&psta_recvpriv->defrag_q);
  52
  53}
  54
  55int _rtw_init_recv_priv(struct recv_priv *precvpriv, struct adapter *padapter)
  56{
  57        int i;
  58
  59        struct recv_frame *precvframe;
  60
  61        int     res = _SUCCESS;
  62
  63        _rtw_init_queue(&precvpriv->free_recv_queue);
  64        _rtw_init_queue(&precvpriv->recv_pending_queue);
  65        _rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
  66
  67        precvpriv->adapter = padapter;
  68
  69        precvpriv->pallocated_frame_buf = vzalloc(NR_RECVFRAME * sizeof(struct recv_frame) + RXFRAME_ALIGN_SZ);
  70
  71        if (!precvpriv->pallocated_frame_buf)
  72                return _FAIL;
  73
  74        precvframe = PTR_ALIGN(precvpriv->pallocated_frame_buf, RXFRAME_ALIGN_SZ);
  75
  76        for (i = 0; i < NR_RECVFRAME; i++) {
  77                INIT_LIST_HEAD(&(precvframe->list));
  78
  79                list_add_tail(&(precvframe->list),
  80                                     &(precvpriv->free_recv_queue.queue));
  81
  82                precvframe->pkt = NULL;
  83
  84                precvframe->adapter = padapter;
  85                precvframe++;
  86        }
  87        res = rtw_hal_init_recv_priv(padapter);
  88
  89        setup_timer(&precvpriv->signal_stat_timer,
  90                    rtw_signal_stat_timer_hdl,
  91                    (unsigned long)padapter);
  92
  93        precvpriv->signal_stat_sampling_interval = 1000; /* ms */
  94
  95        rtw_set_signal_stat_timer(precvpriv);
  96
  97        return res;
  98}
  99
 100void _rtw_free_recv_priv(struct recv_priv *precvpriv)
 101{
 102        struct adapter  *padapter = precvpriv->adapter;
 103
 104        rtw_free_uc_swdec_pending_queue(padapter);
 105
 106        vfree(precvpriv->pallocated_frame_buf);
 107
 108        rtw_hal_free_recv_priv(padapter);
 109
 110}
 111
 112struct recv_frame *_rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
 113{
 114        struct recv_frame *hdr;
 115
 116        hdr = list_first_entry_or_null(&pfree_recv_queue->queue,
 117                                       struct recv_frame, list);
 118        if (hdr)
 119                list_del_init(&hdr->list);
 120
 121        return hdr;
 122}
 123
 124struct recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
 125{
 126        struct recv_frame  *precvframe;
 127
 128        spin_lock_bh(&pfree_recv_queue->lock);
 129
 130        precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
 131
 132        spin_unlock_bh(&pfree_recv_queue->lock);
 133
 134        return precvframe;
 135}
 136
 137int rtw_free_recvframe(struct recv_frame *precvframe,
 138                       struct __queue *pfree_recv_queue)
 139{
 140        if (!precvframe)
 141                return _FAIL;
 142        if (precvframe->pkt) {
 143                dev_kfree_skb_any(precvframe->pkt);/* free skb by driver */
 144                precvframe->pkt = NULL;
 145        }
 146
 147        spin_lock_bh(&pfree_recv_queue->lock);
 148
 149        list_del_init(&(precvframe->list));
 150
 151        list_add_tail(&(precvframe->list), get_list_head(pfree_recv_queue));
 152
 153        spin_unlock_bh(&pfree_recv_queue->lock);
 154
 155        return _SUCCESS;
 156}
 157
 158int _rtw_enqueue_recvframe(struct recv_frame *precvframe, struct __queue *queue)
 159{
 160        list_del_init(&(precvframe->list));
 161        list_add_tail(&(precvframe->list), get_list_head(queue));
 162
 163        return _SUCCESS;
 164}
 165
 166int rtw_enqueue_recvframe(struct recv_frame *precvframe, struct __queue *queue)
 167{
 168        int ret;
 169
 170        spin_lock_bh(&queue->lock);
 171        ret = _rtw_enqueue_recvframe(precvframe, queue);
 172        spin_unlock_bh(&queue->lock);
 173
 174        return ret;
 175}
 176
 177/*
 178caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
 179pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
 180
 181using spinlock to protect
 182
 183*/
 184
 185void rtw_free_recvframe_queue(struct __queue *pframequeue,  struct __queue *pfree_recv_queue)
 186{
 187        struct recv_frame *hdr;
 188        struct list_head *plist, *phead;
 189
 190        spin_lock(&pframequeue->lock);
 191
 192        phead = get_list_head(pframequeue);
 193        plist = phead->next;
 194
 195        while (phead != plist) {
 196                hdr = container_of(plist, struct recv_frame, list);
 197
 198                plist = plist->next;
 199
 200                rtw_free_recvframe(hdr, pfree_recv_queue);
 201        }
 202
 203        spin_unlock(&pframequeue->lock);
 204
 205}
 206
 207u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter)
 208{
 209        u32 cnt = 0;
 210        struct recv_frame *pending_frame;
 211
 212        while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
 213                rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
 214                DBG_88E("%s: dequeue uc_swdec_pending_queue\n", __func__);
 215                cnt++;
 216        }
 217
 218        return cnt;
 219}
 220
 221static int recvframe_chkmic(struct adapter *adapter,
 222                            struct recv_frame *precvframe)
 223{
 224        int     i, res = _SUCCESS;
 225        u32     datalen;
 226        u8      miccode[8];
 227        u8      bmic_err = false, brpt_micerror = true;
 228        u8      *pframe, *payload, *pframemic;
 229        u8      *mickey;
 230        struct  sta_info                *stainfo;
 231        struct  rx_pkt_attrib   *prxattrib = &precvframe->attrib;
 232        struct  security_priv   *psecuritypriv = &adapter->securitypriv;
 233
 234        struct mlme_ext_priv    *pmlmeext = &adapter->mlmeextpriv;
 235        struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
 236
 237        stainfo = rtw_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
 238
 239        if (prxattrib->encrypt == _TKIP_) {
 240                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:prxattrib->encrypt==_TKIP_\n"));
 241                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:da=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
 242                         prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2], prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5]));
 243
 244                /* calculate mic code */
 245                if (stainfo != NULL) {
 246                        if (IS_MCAST(prxattrib->ra)) {
 247                                if (!psecuritypriv) {
 248                                        res = _FAIL;
 249                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n"));
 250                                        DBG_88E("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
 251                                        goto exit;
 252                                }
 253                                mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
 254
 255                                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic: bcmc key\n"));
 256                        } else {
 257                                mickey = &stainfo->dot11tkiprxmickey.skey[0];
 258                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic: unicast key\n"));
 259                        }
 260
 261                        /* icv_len included the mic code */
 262                        datalen = precvframe->pkt->len-prxattrib->hdrlen - 8;
 263                        pframe = precvframe->pkt->data;
 264                        payload = pframe+prxattrib->hdrlen;
 265
 266                        rtw_seccalctkipmic(mickey, pframe, payload, datalen, &miccode[0],
 267                                           (unsigned char)prxattrib->priority); /* care the length of the data */
 268
 269                        pframemic = payload+datalen;
 270
 271                        bmic_err = false;
 272
 273                        for (i = 0; i < 8; i++) {
 274                                if (miccode[i] != *(pframemic+i)) {
 275                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
 276                                                 ("recvframe_chkmic:miccode[%d](%02x)!=*(pframemic+%d)(%02x) ",
 277                                                 i, miccode[i], i, *(pframemic+i)));
 278                                        bmic_err = true;
 279                                }
 280                        }
 281
 282                        if (bmic_err) {
 283                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
 284                                         ("\n *(pframemic-8)-*(pframemic-1)=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
 285                                         *(pframemic-8), *(pframemic-7), *(pframemic-6),
 286                                         *(pframemic-5), *(pframemic-4), *(pframemic-3),
 287                                         *(pframemic-2), *(pframemic-1)));
 288                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
 289                                         ("\n *(pframemic-16)-*(pframemic-9)=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
 290                                         *(pframemic-16), *(pframemic-15), *(pframemic-14),
 291                                         *(pframemic-13), *(pframemic-12), *(pframemic-11),
 292                                         *(pframemic-10), *(pframemic-9)));
 293                                {
 294                                        uint i;
 295
 296                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
 297                                                 ("\n ======demp packet (len=%d)======\n",
 298                                                 precvframe->pkt->len));
 299                                        for (i = 0; i < precvframe->pkt->len; i += 8) {
 300                                                RT_TRACE(_module_rtl871x_recv_c_,
 301                                                         _drv_err_,
 302                                                         ("0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
 303                                                         *(precvframe->pkt->data+i),
 304                                                         *(precvframe->pkt->data+i+1),
 305                                                         *(precvframe->pkt->data+i+2),
 306                                                         *(precvframe->pkt->data+i+3),
 307                                                         *(precvframe->pkt->data+i+4),
 308                                                         *(precvframe->pkt->data+i+5),
 309                                                         *(precvframe->pkt->data+i+6),
 310                                                         *(precvframe->pkt->data+i+7)));
 311                                        }
 312                                        RT_TRACE(_module_rtl871x_recv_c_,
 313                                                 _drv_err_,
 314                                                 ("\n ====== demp packet end [len=%d]======\n",
 315                                                 precvframe->pkt->len));
 316                                        RT_TRACE(_module_rtl871x_recv_c_,
 317                                                 _drv_err_,
 318                                                 ("\n hrdlen=%d,\n",
 319                                                 prxattrib->hdrlen));
 320                                }
 321
 322                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
 323                                         ("ra=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x psecuritypriv->binstallGrpkey=%d ",
 324                                         prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2],
 325                                         prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5], psecuritypriv->binstallGrpkey));
 326
 327                                /*  double check key_index for some timing issue , */
 328                                /*  cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
 329                                if ((IS_MCAST(prxattrib->ra) == true)  && (prxattrib->key_index != pmlmeinfo->key_index))
 330                                        brpt_micerror = false;
 331
 332                                if ((prxattrib->bdecrypted) && (brpt_micerror)) {
 333                                        rtw_handle_tkip_mic_err(adapter, (u8)IS_MCAST(prxattrib->ra));
 334                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted=%d ", prxattrib->bdecrypted));
 335                                        DBG_88E(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
 336                                } else {
 337                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted=%d ", prxattrib->bdecrypted));
 338                                        DBG_88E(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
 339                                }
 340                                res = _FAIL;
 341                        } else {
 342                                /* mic checked ok */
 343                                if ((!psecuritypriv->bcheck_grpkey) && (IS_MCAST(prxattrib->ra))) {
 344                                        psecuritypriv->bcheck_grpkey = true;
 345                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("psecuritypriv->bcheck_grpkey = true"));
 346                                }
 347                        }
 348                } else {
 349                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic: rtw_get_stainfo==NULL!!!\n"));
 350                }
 351
 352                skb_trim(precvframe->pkt, precvframe->pkt->len - 8);
 353        }
 354
 355exit:
 356
 357        return res;
 358}
 359
 360/* decrypt and set the ivlen, icvlen of the recv_frame */
 361static struct recv_frame *decryptor(struct adapter *padapter,
 362                                    struct recv_frame *precv_frame)
 363{
 364        struct rx_pkt_attrib *prxattrib = &precv_frame->attrib;
 365        struct security_priv *psecuritypriv = &padapter->securitypriv;
 366        struct recv_frame *return_packet = precv_frame;
 367        u32      res = _SUCCESS;
 368
 369        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("prxstat->decrypted=%x prxattrib->encrypt=0x%03x\n", prxattrib->bdecrypted, prxattrib->encrypt));
 370
 371        if (prxattrib->encrypt > 0) {
 372                u8 *iv = precv_frame->pkt->data+prxattrib->hdrlen;
 373
 374                prxattrib->key_index = (((iv[3])>>6)&0x3);
 375
 376                if (prxattrib->key_index > WEP_KEYS) {
 377                        DBG_88E("prxattrib->key_index(%d)>WEP_KEYS\n", prxattrib->key_index);
 378
 379                        switch (prxattrib->encrypt) {
 380                        case _WEP40_:
 381                        case _WEP104_:
 382                                prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
 383                                break;
 384                        case _TKIP_:
 385                        case _AES_:
 386                        default:
 387                                prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
 388                                break;
 389                        }
 390                }
 391        }
 392
 393        if ((prxattrib->encrypt > 0) && (prxattrib->bdecrypted == 0)) {
 394                psecuritypriv->hw_decrypted = false;
 395
 396                switch (prxattrib->encrypt) {
 397                case _WEP40_:
 398                case _WEP104_:
 399                        rtw_wep_decrypt(padapter, (u8 *)precv_frame);
 400                        break;
 401                case _TKIP_:
 402                        res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
 403                        break;
 404                case _AES_:
 405                        res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
 406                        break;
 407                default:
 408                        break;
 409                }
 410                if (res != _FAIL) {
 411                        memmove(precv_frame->pkt->data + precv_frame->attrib.iv_len, precv_frame->pkt->data, precv_frame->attrib.hdrlen);
 412                        skb_pull(precv_frame->pkt, precv_frame->attrib.iv_len);
 413                        skb_trim(precv_frame->pkt, precv_frame->pkt->len - precv_frame->attrib.icv_len);
 414                }
 415        } else if (prxattrib->bdecrypted == 1 && prxattrib->encrypt > 0 &&
 416                   (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_)) {
 417                psecuritypriv->hw_decrypted = true;
 418        }
 419
 420        if (res == _FAIL) {
 421                rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
 422                return_packet = NULL;
 423        }
 424
 425        return return_packet;
 426}
 427
 428/* set the security information in the recv_frame */
 429static struct recv_frame *portctrl(struct adapter *adapter,
 430                                   struct recv_frame *precv_frame)
 431{
 432        u8   *psta_addr, *ptr;
 433        uint  auth_alg;
 434        struct recv_frame *pfhdr;
 435        struct sta_info *psta;
 436        struct sta_priv *pstapriv;
 437        struct recv_frame *prtnframe;
 438        u16     ether_type;
 439        u16  eapol_type = 0x888e;/* for Funia BD's WPA issue */
 440        struct rx_pkt_attrib *pattrib;
 441        __be16 be_tmp;
 442
 443        pstapriv = &adapter->stapriv;
 444
 445        auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
 446
 447        ptr = precv_frame->pkt->data;
 448        pfhdr = precv_frame;
 449        pattrib = &pfhdr->attrib;
 450        psta_addr = pattrib->ta;
 451        psta = rtw_get_stainfo(pstapriv, psta_addr);
 452
 453        prtnframe = NULL;
 454
 455        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:adapter->securitypriv.dot11AuthAlgrthm=%d\n", adapter->securitypriv.dot11AuthAlgrthm));
 456
 457        if (auth_alg == 2) {
 458                /* get ether_type */
 459                ptr = ptr + pfhdr->attrib.hdrlen + LLC_HEADER_SIZE;
 460                memcpy(&be_tmp, ptr, 2);
 461                ether_type = ntohs(be_tmp);
 462
 463                if ((psta != NULL) && (psta->ieee8021x_blocked)) {
 464                        /* blocked */
 465                        /* only accept EAPOL frame */
 466                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked==1\n"));
 467
 468                        if (ether_type == eapol_type) {
 469                                prtnframe = precv_frame;
 470                        } else {
 471                                /* free this frame */
 472                                rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
 473                                prtnframe = NULL;
 474                        }
 475                } else {
 476                        /* allowed */
 477                        /* check decryption status, and decrypt the frame if needed */
 478                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked==0\n"));
 479                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
 480                                 ("portctrl:precv_frame->hdr.attrib.privacy=%x\n",
 481                                 precv_frame->attrib.privacy));
 482
 483                        if (pattrib->bdecrypted == 0)
 484                                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:prxstat->decrypted=%x\n", pattrib->bdecrypted));
 485
 486                        prtnframe = precv_frame;
 487                        /* check is the EAPOL frame or not (Rekey) */
 488                        if (ether_type == eapol_type) {
 489                                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("########portctrl:ether_type==0x888e\n"));
 490                                /* check Rekey */
 491
 492                                prtnframe = precv_frame;
 493                        } else {
 494                                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:ether_type=0x%04x\n", ether_type));
 495                        }
 496                }
 497        } else {
 498                prtnframe = precv_frame;
 499        }
 500
 501                return prtnframe;
 502}
 503
 504static int recv_decache(struct recv_frame *precv_frame, u8 bretry,
 505                        struct stainfo_rxcache *prxcache)
 506{
 507        int tid = precv_frame->attrib.priority;
 508
 509        u16 seq_ctrl = ((precv_frame->attrib.seq_num&0xffff) << 4) |
 510                (precv_frame->attrib.frag_num & 0xf);
 511
 512        if (tid > 15) {
 513                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, (tid>15)! seq_ctrl=0x%x, tid=0x%x\n", seq_ctrl, tid));
 514
 515                return _FAIL;
 516        }
 517
 518        if (1) {/* if (bretry) */
 519                if (seq_ctrl == prxcache->tid_rxseq[tid]) {
 520                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, seq_ctrl=0x%x, tid=0x%x, tid_rxseq=0x%x\n", seq_ctrl, tid, prxcache->tid_rxseq[tid]));
 521
 522                        return _FAIL;
 523                }
 524        }
 525
 526        prxcache->tid_rxseq[tid] = seq_ctrl;
 527
 528        return _SUCCESS;
 529}
 530
 531static void process_pwrbit_data(struct adapter *padapter,
 532                                struct recv_frame *precv_frame)
 533{
 534#ifdef CONFIG_88EU_AP_MODE
 535        unsigned char pwrbit;
 536        u8 *ptr = precv_frame->pkt->data;
 537        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
 538        struct sta_priv *pstapriv = &padapter->stapriv;
 539        struct sta_info *psta = NULL;
 540
 541        psta = rtw_get_stainfo(pstapriv, pattrib->src);
 542
 543        pwrbit = GetPwrMgt(ptr);
 544
 545        if (psta) {
 546                if (pwrbit) {
 547                        if (!(psta->state & WIFI_SLEEP_STATE))
 548                                stop_sta_xmit(padapter, psta);
 549                } else {
 550                        if (psta->state & WIFI_SLEEP_STATE)
 551                                wakeup_sta_to_xmit(padapter, psta);
 552                }
 553        }
 554
 555#endif
 556}
 557
 558static void process_wmmps_data(struct adapter *padapter,
 559                               struct recv_frame *precv_frame)
 560{
 561#ifdef CONFIG_88EU_AP_MODE
 562        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
 563        struct sta_priv *pstapriv = &padapter->stapriv;
 564        struct sta_info *psta = NULL;
 565
 566        psta = rtw_get_stainfo(pstapriv, pattrib->src);
 567
 568        if (!psta)
 569                return;
 570
 571        if (!psta->qos_option)
 572                return;
 573
 574        if (!(psta->qos_info&0xf))
 575                return;
 576
 577        if (psta->state&WIFI_SLEEP_STATE) {
 578                u8 wmmps_ac = 0;
 579
 580                switch (pattrib->priority) {
 581                case 1:
 582                case 2:
 583                        wmmps_ac = psta->uapsd_bk&BIT(1);
 584                        break;
 585                case 4:
 586                case 5:
 587                        wmmps_ac = psta->uapsd_vi&BIT(1);
 588                        break;
 589                case 6:
 590                case 7:
 591                        wmmps_ac = psta->uapsd_vo&BIT(1);
 592                        break;
 593                case 0:
 594                case 3:
 595                default:
 596                        wmmps_ac = psta->uapsd_be&BIT(1);
 597                        break;
 598                }
 599
 600                if (wmmps_ac) {
 601                        if (psta->sleepq_ac_len > 0) {
 602                                /* process received triggered frame */
 603                                xmit_delivery_enabled_frames(padapter, psta);
 604                        } else {
 605                                /* issue one qos null frame with More data bit = 0 and the EOSP bit set (= 1) */
 606                                issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
 607                        }
 608                }
 609        }
 610
 611#endif
 612}
 613
 614static void count_rx_stats(struct adapter *padapter,
 615                           struct recv_frame *prframe,
 616                           struct sta_info *sta)
 617{
 618        int     sz;
 619        struct sta_info         *psta = NULL;
 620        struct stainfo_stats    *pstats = NULL;
 621        struct rx_pkt_attrib    *pattrib = &prframe->attrib;
 622        struct recv_priv        *precvpriv = &padapter->recvpriv;
 623
 624        sz = prframe->pkt->len;
 625        precvpriv->rx_bytes += sz;
 626
 627        padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
 628
 629        if ((!MacAddr_isBcst(pattrib->dst)) && (!IS_MCAST(pattrib->dst)))
 630                padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
 631
 632        if (sta)
 633                psta = sta;
 634        else
 635                psta = prframe->psta;
 636
 637        if (psta) {
 638                pstats = &psta->sta_stats;
 639
 640                pstats->rx_data_pkts++;
 641                pstats->rx_bytes += sz;
 642        }
 643}
 644
 645int sta2sta_data_frame(
 646        struct adapter *adapter,
 647        struct recv_frame *precv_frame,
 648        struct sta_info **psta
 649);
 650
 651int sta2sta_data_frame(struct adapter *adapter, struct recv_frame *precv_frame,
 652                       struct sta_info **psta)
 653{
 654        int ret = _SUCCESS;
 655        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
 656        struct  sta_priv *pstapriv = &adapter->stapriv;
 657        struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
 658        u8 *mybssid  = get_bssid(pmlmepriv);
 659        u8 *myhwaddr = myid(&adapter->eeprompriv);
 660        u8 *sta_addr = NULL;
 661        int bmcast = IS_MCAST(pattrib->dst);
 662
 663        if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
 664            (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
 665                /*  filter packets that SA is myself or multicast or broadcast */
 666                if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
 667                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA==myself\n"));
 668                        ret = _FAIL;
 669                        goto exit;
 670                }
 671
 672                if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
 673                        ret = _FAIL;
 674                        goto exit;
 675                }
 676
 677                if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 678                    !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 679                    memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
 680                        ret = _FAIL;
 681                        goto exit;
 682                }
 683
 684                sta_addr = pattrib->src;
 685        } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
 686                /*  For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
 687                if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN)) {
 688                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("bssid!=TA under STATION_MODE; drop pkt\n"));
 689                        ret = _FAIL;
 690                        goto exit;
 691                }
 692                sta_addr = pattrib->bssid;
 693        } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
 694                if (bmcast) {
 695                        /*  For AP mode, if DA == MCAST, then BSSID should be also MCAST */
 696                        if (!IS_MCAST(pattrib->bssid)) {
 697                                        ret = _FAIL;
 698                                        goto exit;
 699                        }
 700                } else { /*  not mc-frame */
 701                        /*  For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
 702                        if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
 703                                ret = _FAIL;
 704                                goto exit;
 705                        }
 706
 707                        sta_addr = pattrib->src;
 708                }
 709        } else {
 710                ret  = _FAIL;
 711        }
 712
 713        if (bmcast)
 714                *psta = rtw_get_bcmc_stainfo(adapter);
 715        else
 716                *psta = rtw_get_stainfo(pstapriv, sta_addr); /*  get ap_info */
 717
 718        if (*psta == NULL) {
 719                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under sta2sta_data_frame ; drop pkt\n"));
 720                ret = _FAIL;
 721                goto exit;
 722        }
 723
 724exit:
 725        return ret;
 726}
 727
 728static int ap2sta_data_frame(
 729        struct adapter *adapter,
 730        struct recv_frame *precv_frame,
 731        struct sta_info **psta)
 732{
 733        u8 *ptr = precv_frame->pkt->data;
 734        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
 735        int ret = _SUCCESS;
 736        struct  sta_priv *pstapriv = &adapter->stapriv;
 737        struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
 738        u8 *mybssid  = get_bssid(pmlmepriv);
 739        u8 *myhwaddr = myid(&adapter->eeprompriv);
 740        int bmcast = IS_MCAST(pattrib->dst);
 741
 742        if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) &&
 743            (check_fwstate(pmlmepriv, _FW_LINKED) == true ||
 744            check_fwstate(pmlmepriv, _FW_UNDER_LINKING))) {
 745                /*  filter packets that SA is myself or multicast or broadcast */
 746                if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
 747                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA==myself\n"));
 748                        ret = _FAIL;
 749                        goto exit;
 750                }
 751
 752                /*  da should be for me */
 753                if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
 754                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
 755                                 (" ap2sta_data_frame:  compare DA fail; DA=%pM\n", (pattrib->dst)));
 756                        ret = _FAIL;
 757                        goto exit;
 758                }
 759
 760                /*  check BSSID */
 761                if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 762                    !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 763                     (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
 764                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
 765                                 (" ap2sta_data_frame:  compare BSSID fail ; BSSID=%pM\n", (pattrib->bssid)));
 766                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("mybssid=%pM\n", (mybssid)));
 767
 768                        if (!bmcast) {
 769                                DBG_88E("issue_deauth to the nonassociated ap=%pM for the reason(7)\n", (pattrib->bssid));
 770                                issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
 771                        }
 772
 773                        ret = _FAIL;
 774                        goto exit;
 775                }
 776
 777                if (bmcast)
 778                        *psta = rtw_get_bcmc_stainfo(adapter);
 779                else
 780                        *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get ap_info */
 781
 782                if (*psta == NULL) {
 783                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ap2sta: can't get psta under STATION_MODE ; drop pkt\n"));
 784                        ret = _FAIL;
 785                        goto exit;
 786                }
 787
 788                /* if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) { */
 789                /*  */
 790
 791                if (GetFrameSubType(ptr) & BIT(6)) {
 792                        /* No data, will not indicate to upper layer, temporily count it here */
 793                        count_rx_stats(adapter, precv_frame, *psta);
 794                        ret = RTW_RX_HANDLED;
 795                        goto exit;
 796                }
 797        } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
 798                /* Special case */
 799                ret = RTW_RX_HANDLED;
 800                goto exit;
 801        } else {
 802                if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
 803                        *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
 804                        if (*psta == NULL) {
 805                                DBG_88E("issue_deauth to the ap =%pM for the reason(7)\n", (pattrib->bssid));
 806
 807                                issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
 808                        }
 809                }
 810
 811                ret = _FAIL;
 812        }
 813
 814exit:
 815
 816        return ret;
 817}
 818
 819static int sta2ap_data_frame(struct adapter *adapter,
 820                             struct recv_frame *precv_frame,
 821                             struct sta_info **psta)
 822{
 823        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
 824        struct  sta_priv *pstapriv = &adapter->stapriv;
 825        struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
 826        u8 *ptr = precv_frame->pkt->data;
 827        unsigned char *mybssid  = get_bssid(pmlmepriv);
 828        int ret = _SUCCESS;
 829
 830        if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
 831                /* For AP mode, RA = BSSID, TX = STA(SRC_ADDR), A3 = DST_ADDR */
 832                if (memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
 833                        ret = _FAIL;
 834                        goto exit;
 835                }
 836
 837                *psta = rtw_get_stainfo(pstapriv, pattrib->src);
 838                if (*psta == NULL) {
 839                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under AP_MODE; drop pkt\n"));
 840                        DBG_88E("issue_deauth to sta=%pM for the reason(7)\n", (pattrib->src));
 841
 842                        issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
 843
 844                        ret = RTW_RX_HANDLED;
 845                        goto exit;
 846                }
 847
 848                process_pwrbit_data(adapter, precv_frame);
 849
 850                if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE)
 851                        process_wmmps_data(adapter, precv_frame);
 852
 853                if (GetFrameSubType(ptr) & BIT(6)) {
 854                        /* No data, will not indicate to upper layer, temporily count it here */
 855                        count_rx_stats(adapter, precv_frame, *psta);
 856                        ret = RTW_RX_HANDLED;
 857                        goto exit;
 858                }
 859        } else {
 860                u8 *myhwaddr = myid(&adapter->eeprompriv);
 861
 862                if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
 863                        ret = RTW_RX_HANDLED;
 864                        goto exit;
 865                }
 866                DBG_88E("issue_deauth to sta=%pM for the reason(7)\n", (pattrib->src));
 867                issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
 868                ret = RTW_RX_HANDLED;
 869                goto exit;
 870        }
 871
 872exit:
 873
 874        return ret;
 875}
 876
 877static int validate_recv_ctrl_frame(struct adapter *padapter,
 878                                    struct recv_frame *precv_frame)
 879{
 880#ifdef CONFIG_88EU_AP_MODE
 881        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
 882        struct sta_priv *pstapriv = &padapter->stapriv;
 883        u8 *pframe = precv_frame->pkt->data;
 884
 885        if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
 886                return _FAIL;
 887
 888        /* receive the frames that ra(a1) is my address */
 889        if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
 890                return _FAIL;
 891
 892        /* only handle ps-poll */
 893        if (GetFrameSubType(pframe) == WIFI_PSPOLL) {
 894                u16 aid;
 895                u8 wmmps_ac = 0;
 896                struct sta_info *psta = NULL;
 897
 898                aid = GetAid(pframe);
 899                psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
 900
 901                if ((psta == NULL) || (psta->aid != aid))
 902                        return _FAIL;
 903
 904                /* for rx pkt statistics */
 905                psta->sta_stats.rx_ctrl_pkts++;
 906
 907                switch (pattrib->priority) {
 908                case 1:
 909                case 2:
 910                        wmmps_ac = psta->uapsd_bk&BIT(0);
 911                        break;
 912                case 4:
 913                case 5:
 914                        wmmps_ac = psta->uapsd_vi&BIT(0);
 915                        break;
 916                case 6:
 917                case 7:
 918                        wmmps_ac = psta->uapsd_vo&BIT(0);
 919                        break;
 920                case 0:
 921                case 3:
 922                default:
 923                        wmmps_ac = psta->uapsd_be&BIT(0);
 924                        break;
 925                }
 926
 927                if (wmmps_ac)
 928                        return _FAIL;
 929
 930                if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
 931                        DBG_88E("%s alive check-rx ps-poll\n", __func__);
 932                        psta->expire_to = pstapriv->expire_to;
 933                        psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
 934                }
 935
 936                if ((psta->state&WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap&BIT(psta->aid))) {
 937                        struct list_head *xmitframe_plist, *xmitframe_phead;
 938                        struct xmit_frame *pxmitframe = NULL;
 939
 940                        spin_lock_bh(&psta->sleep_q.lock);
 941
 942                        xmitframe_phead = get_list_head(&psta->sleep_q);
 943                        xmitframe_plist = xmitframe_phead->next;
 944
 945                        if (xmitframe_phead != xmitframe_plist) {
 946                                pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
 947
 948                                xmitframe_plist = xmitframe_plist->next;
 949
 950                                list_del_init(&pxmitframe->list);
 951
 952                                psta->sleepq_len--;
 953
 954                                if (psta->sleepq_len > 0)
 955                                        pxmitframe->attrib.mdata = 1;
 956                                else
 957                                        pxmitframe->attrib.mdata = 0;
 958
 959                                pxmitframe->attrib.triggered = 1;
 960
 961                                spin_unlock_bh(&psta->sleep_q.lock);
 962                                if (rtw_hal_xmit(padapter, pxmitframe) == true)
 963                                        rtw_os_xmit_complete(padapter, pxmitframe);
 964                                spin_lock_bh(&psta->sleep_q.lock);
 965
 966                                if (psta->sleepq_len == 0) {
 967                                        pstapriv->tim_bitmap &= ~BIT(psta->aid);
 968
 969                                        /* update BCN for TIM IE */
 970                                        /* update_BCNTIM(padapter); */
 971                                        update_beacon(padapter, _TIM_IE_, NULL, false);
 972                                }
 973                        } else {
 974                                if (pstapriv->tim_bitmap&BIT(psta->aid)) {
 975                                        if (psta->sleepq_len == 0) {
 976                                                DBG_88E("no buffered packets to xmit\n");
 977
 978                                                /* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
 979                                                issue_nulldata(padapter, psta->hwaddr, 0, 0, 0);
 980                                        } else {
 981                                                DBG_88E("error!psta->sleepq_len=%d\n", psta->sleepq_len);
 982                                                psta->sleepq_len = 0;
 983                                        }
 984
 985                                        pstapriv->tim_bitmap &= ~BIT(psta->aid);
 986
 987                                        /* update BCN for TIM IE */
 988                                        /* update_BCNTIM(padapter); */
 989                                        update_beacon(padapter, _TIM_IE_, NULL, false);
 990                                }
 991                        }
 992
 993                        spin_unlock_bh(&psta->sleep_q.lock);
 994                }
 995        }
 996
 997#endif
 998
 999        return _FAIL;
1000}
1001
1002struct recv_frame *recvframe_chk_defrag(struct adapter *padapter,
1003                                        struct recv_frame *precv_frame);
1004
1005static int validate_recv_mgnt_frame(struct adapter *padapter,
1006                                    struct recv_frame *precv_frame)
1007{
1008        struct sta_info *psta;
1009
1010        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+validate_recv_mgnt_frame\n"));
1011
1012        precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1013        if (precv_frame == NULL) {
1014                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("%s: fragment packet\n", __func__));
1015                return _SUCCESS;
1016        }
1017
1018        /* for rx pkt statistics */
1019        psta = rtw_get_stainfo(&padapter->stapriv,
1020                               GetAddr2Ptr(precv_frame->pkt->data));
1021        if (psta) {
1022                psta->sta_stats.rx_mgnt_pkts++;
1023                if (GetFrameSubType(precv_frame->pkt->data) == WIFI_BEACON) {
1024                        psta->sta_stats.rx_beacon_pkts++;
1025                } else if (GetFrameSubType(precv_frame->pkt->data) == WIFI_PROBEREQ) {
1026                        psta->sta_stats.rx_probereq_pkts++;
1027                } else if (GetFrameSubType(precv_frame->pkt->data) == WIFI_PROBERSP) {
1028                        if (!memcmp(padapter->eeprompriv.mac_addr,
1029                                    GetAddr1Ptr(precv_frame->pkt->data), ETH_ALEN))
1030                                psta->sta_stats.rx_probersp_pkts++;
1031                        else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->pkt->data)) ||
1032                                 is_multicast_mac_addr(GetAddr1Ptr(precv_frame->pkt->data)))
1033                                psta->sta_stats.rx_probersp_bm_pkts++;
1034                        else
1035                                psta->sta_stats.rx_probersp_uo_pkts++;
1036                }
1037        }
1038
1039        mgt_dispatcher(padapter, precv_frame);
1040
1041        return _SUCCESS;
1042}
1043
1044static int validate_recv_data_frame(struct adapter *adapter,
1045                                    struct recv_frame *precv_frame)
1046{
1047        u8 bretry;
1048        u8 *psa, *pda, *pbssid;
1049        struct sta_info *psta = NULL;
1050        u8 *ptr = precv_frame->pkt->data;
1051        struct rx_pkt_attrib    *pattrib = &precv_frame->attrib;
1052        struct security_priv    *psecuritypriv = &adapter->securitypriv;
1053        int ret = _SUCCESS;
1054
1055        bretry = GetRetry(ptr);
1056        pda = get_da(ptr);
1057        psa = get_sa(ptr);
1058        pbssid = get_hdr_bssid(ptr);
1059
1060        if (pbssid == NULL) {
1061                ret = _FAIL;
1062                goto exit;
1063        }
1064
1065        memcpy(pattrib->dst, pda, ETH_ALEN);
1066        memcpy(pattrib->src, psa, ETH_ALEN);
1067
1068        memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1069
1070        switch (pattrib->to_fr_ds) {
1071        case 0:
1072                memcpy(pattrib->ra, pda, ETH_ALEN);
1073                memcpy(pattrib->ta, psa, ETH_ALEN);
1074                ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1075                break;
1076        case 1:
1077                memcpy(pattrib->ra, pda, ETH_ALEN);
1078                memcpy(pattrib->ta, pbssid, ETH_ALEN);
1079                ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1080                break;
1081        case 2:
1082                memcpy(pattrib->ra, pbssid, ETH_ALEN);
1083                memcpy(pattrib->ta, psa, ETH_ALEN);
1084                ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1085                break;
1086        case 3:
1087                memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1088                memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1089                ret = _FAIL;
1090                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" case 3\n"));
1091                break;
1092        default:
1093                ret = _FAIL;
1094                break;
1095        }
1096
1097        if (ret == _FAIL)
1098                goto exit;
1099        else if (ret == RTW_RX_HANDLED)
1100                goto exit;
1101
1102        if (psta == NULL) {
1103                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" after to_fr_ds_chk; psta==NULL\n"));
1104                ret = _FAIL;
1105                goto exit;
1106        }
1107
1108        /* psta->rssi = prxcmd->rssi; */
1109        /* psta->signal_quality = prxcmd->sq; */
1110        precv_frame->psta = psta;
1111
1112        pattrib->amsdu = 0;
1113        pattrib->ack_policy = 0;
1114        /* parsing QC field */
1115        if (pattrib->qos == 1) {
1116                pattrib->priority = GetPriority((ptr + 24));
1117                pattrib->ack_policy = GetAckpolicy((ptr + 24));
1118                pattrib->amsdu = GetAMsdu((ptr + 24));
1119                pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
1120
1121                if (pattrib->priority != 0 && pattrib->priority != 3)
1122                        adapter->recvpriv.bIsAnyNonBEPkts = true;
1123        } else {
1124                pattrib->priority = 0;
1125                pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 30 : 24;
1126        }
1127
1128        if (pattrib->order)/* HT-CTRL 11n */
1129                pattrib->hdrlen += 4;
1130
1131        precv_frame->preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1132
1133        /*  decache, drop duplicate recv packets */
1134        if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL) {
1135                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decache : drop pkt\n"));
1136                ret = _FAIL;
1137                goto exit;
1138        }
1139
1140        if (pattrib->privacy) {
1141                struct sk_buff *skb = precv_frame->pkt;
1142
1143                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("validate_recv_data_frame:pattrib->privacy=%x\n", pattrib->privacy));
1144                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n ^^^^^^^^^^^IS_MCAST(pattrib->ra(0x%02x))=%d^^^^^^^^^^^^^^^6\n", pattrib->ra[0], IS_MCAST(pattrib->ra)));
1145
1146                GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
1147
1148                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n pattrib->encrypt=%d\n", pattrib->encrypt));
1149
1150                SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1151
1152                if (pattrib->bdecrypted == 1 && pattrib->encrypt > 0) {
1153                        memmove(skb->data + pattrib->iv_len,
1154                                skb->data, pattrib->hdrlen);
1155                        skb_pull(skb, pattrib->iv_len);
1156                        skb_trim(skb, skb->len - pattrib->icv_len);
1157                }
1158        } else {
1159                pattrib->encrypt = 0;
1160                pattrib->iv_len = 0;
1161                pattrib->icv_len = 0;
1162        }
1163
1164exit:
1165
1166        return ret;
1167}
1168
1169static int validate_recv_frame(struct adapter *adapter,
1170                               struct recv_frame *precv_frame)
1171{
1172        /* shall check frame subtype, to / from ds, da, bssid */
1173
1174        /* then call check if rx seq/frag. duplicated. */
1175
1176        u8 type;
1177        u8 subtype;
1178        int retval = _SUCCESS;
1179        u8 bDumpRxPkt;
1180        struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
1181        u8 *ptr = precv_frame->pkt->data;
1182        u8  ver = (unsigned char)(*ptr)&0x3;
1183        struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1184
1185        if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
1186                int ch_set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, rtw_get_oper_ch(adapter));
1187
1188                if (ch_set_idx >= 0)
1189                        pmlmeext->channel_set[ch_set_idx].rx_count++;
1190        }
1191
1192        /* add version chk */
1193        if (ver != 0) {
1194                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! (ver!=0)\n"));
1195                retval = _FAIL;
1196                goto exit;
1197        }
1198
1199        type =  GetFrameType(ptr);
1200        subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1201
1202        pattrib->to_fr_ds = get_tofr_ds(ptr);
1203
1204        pattrib->frag_num = GetFragNum(ptr);
1205        pattrib->seq_num = GetSequence(ptr);
1206
1207        pattrib->pw_save = GetPwrMgt(ptr);
1208        pattrib->mfrag = GetMFrag(ptr);
1209        pattrib->mdata = GetMData(ptr);
1210        pattrib->privacy = GetPrivacy(ptr);
1211        pattrib->order = GetOrder(ptr);
1212
1213        /* Dump rx packets */
1214        rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1215        if (bDumpRxPkt == 1) {/* dump all rx packets */
1216                if (_drv_err_ <= GlobalDebugLevel) {
1217                        pr_info(DRIVER_PREFIX "#############################\n");
1218                        print_hex_dump(KERN_INFO, DRIVER_PREFIX, DUMP_PREFIX_NONE,
1219                                        16, 1, ptr, 64, false);
1220                        pr_info(DRIVER_PREFIX "#############################\n");
1221                }
1222        } else if (bDumpRxPkt == 2) {
1223                if ((_drv_err_ <= GlobalDebugLevel) && (type == WIFI_MGT_TYPE)) {
1224                        pr_info(DRIVER_PREFIX "#############################\n");
1225                        print_hex_dump(KERN_INFO, DRIVER_PREFIX, DUMP_PREFIX_NONE,
1226                                        16, 1, ptr, 64, false);
1227                        pr_info(DRIVER_PREFIX "#############################\n");
1228                }
1229        } else if (bDumpRxPkt == 3) {
1230                if ((_drv_err_ <= GlobalDebugLevel) && (type == WIFI_DATA_TYPE)) {
1231                        pr_info(DRIVER_PREFIX "#############################\n");
1232                        print_hex_dump(KERN_INFO, DRIVER_PREFIX, DUMP_PREFIX_NONE,
1233                                        16, 1, ptr, 64, false);
1234                        pr_info(DRIVER_PREFIX "#############################\n");
1235                }
1236        }
1237        switch (type) {
1238        case WIFI_MGT_TYPE: /* mgnt */
1239                retval = validate_recv_mgnt_frame(adapter, precv_frame);
1240                if (retval == _FAIL)
1241                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_mgnt_frame fail\n"));
1242                retval = _FAIL; /*  only data frame return _SUCCESS */
1243                break;
1244        case WIFI_CTRL_TYPE: /* ctrl */
1245                retval = validate_recv_ctrl_frame(adapter, precv_frame);
1246                if (retval == _FAIL)
1247                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_ctrl_frame fail\n"));
1248                retval = _FAIL; /*  only data frame return _SUCCESS */
1249                break;
1250        case WIFI_DATA_TYPE: /* data */
1251                LedControl8188eu(adapter, LED_CTL_RX);
1252                pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
1253                retval = validate_recv_data_frame(adapter, precv_frame);
1254                if (retval == _FAIL) {
1255                        struct recv_priv *precvpriv = &adapter->recvpriv;
1256
1257                        precvpriv->rx_drop++;
1258                }
1259                break;
1260        default:
1261                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! type= 0x%x\n", type));
1262                retval = _FAIL;
1263                break;
1264        }
1265
1266        /*
1267         * This is the last moment before management and control frames get
1268         * discarded. So we need to forward them to the monitor now or never.
1269         *
1270         * At the same time data frames can still be encrypted if software
1271         * decryption is in use. However, decryption can occur not until later
1272         * (see recv_func()).
1273         *
1274         * Hence forward the frame to the monitor anyway to preserve the order
1275         * in which frames were received.
1276         */
1277
1278        rtl88eu_mon_recv_hook(adapter->pmondev, precv_frame);
1279
1280exit:
1281
1282        return retval;
1283}
1284
1285/* remove the wlanhdr and add the eth_hdr */
1286
1287static int wlanhdr_to_ethhdr(struct recv_frame *precvframe)
1288{
1289        int     rmv_len;
1290        u16     eth_type, len;
1291        __be16 be_tmp;
1292        u8      bsnaphdr;
1293        u8      *psnap_type;
1294        struct ieee80211_snap_hdr       *psnap;
1295
1296        u8 *ptr = precvframe->pkt->data;
1297        struct rx_pkt_attrib *pattrib = &precvframe->attrib;
1298
1299        psnap = (struct ieee80211_snap_hdr *)(ptr+pattrib->hdrlen);
1300        psnap_type = ptr+pattrib->hdrlen + SNAP_SIZE;
1301        /* convert hdr + possible LLC headers into Ethernet header */
1302        if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
1303             (!memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2) == false) &&
1304             (!memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2) == false)) ||
1305             !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
1306                /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1307                bsnaphdr = true;
1308        } else {
1309                /* Leave Ethernet header part of hdr and full payload */
1310                bsnaphdr = false;
1311        }
1312
1313        rmv_len = pattrib->hdrlen + (bsnaphdr ? SNAP_SIZE : 0);
1314        len = precvframe->pkt->len - rmv_len;
1315
1316        memcpy(&be_tmp, ptr+rmv_len, 2);
1317        eth_type = ntohs(be_tmp); /* pattrib->ether_type */
1318        pattrib->eth_type = eth_type;
1319
1320        ptr = skb_pull(precvframe->pkt, rmv_len - sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0));
1321        if (!ptr)
1322                return _FAIL;
1323
1324        memcpy(ptr, pattrib->dst, ETH_ALEN);
1325        memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
1326
1327        if (!bsnaphdr) {
1328                be_tmp = htons(len);
1329                memcpy(ptr+12, &be_tmp, 2);
1330        }
1331
1332        return _SUCCESS;
1333}
1334
1335/* perform defrag */
1336static struct recv_frame *recvframe_defrag(struct adapter *adapter,
1337                                           struct __queue *defrag_q)
1338{
1339        struct list_head *plist, *phead;
1340        u8      curfragnum;
1341        struct recv_frame *pfhdr, *pnfhdr;
1342        struct recv_frame *prframe, *pnextrframe;
1343        struct __queue *pfree_recv_queue;
1344
1345        curfragnum = 0;
1346        pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
1347
1348        phead = get_list_head(defrag_q);
1349        plist = phead->next;
1350        pfhdr = container_of(plist, struct recv_frame, list);
1351        prframe = pfhdr;
1352        list_del_init(&(prframe->list));
1353
1354        if (curfragnum != pfhdr->attrib.frag_num) {
1355                /* the first fragment number must be 0 */
1356                /* free the whole queue */
1357                rtw_free_recvframe(prframe, pfree_recv_queue);
1358                rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1359
1360                return NULL;
1361        }
1362
1363        curfragnum++;
1364
1365        plist = get_list_head(defrag_q);
1366
1367        plist = plist->next;
1368
1369        while (phead != plist) {
1370                pnfhdr = container_of(plist, struct recv_frame, list);
1371                pnextrframe = pnfhdr;
1372
1373                /* check the fragment sequence  (2nd ~n fragment frame) */
1374
1375                if (curfragnum != pnfhdr->attrib.frag_num) {
1376                        /* the fragment number must be increasing  (after decache) */
1377                        /* release the defrag_q & prframe */
1378                        rtw_free_recvframe(prframe, pfree_recv_queue);
1379                        rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1380                        return NULL;
1381                }
1382
1383                curfragnum++;
1384
1385                /* copy the 2nd~n fragment frame's payload to the first fragment */
1386                /* get the 2nd~last fragment frame's payload */
1387
1388                skb_pull(pnextrframe->pkt, pnfhdr->attrib.hdrlen);
1389
1390                /* memcpy */
1391                memcpy(skb_tail_pointer(pfhdr->pkt), pnfhdr->pkt->data,
1392                       pnfhdr->pkt->len);
1393
1394                skb_put(prframe->pkt, pnfhdr->pkt->len);
1395
1396                pfhdr->attrib.icv_len = 0;
1397                plist = plist->next;
1398        }
1399
1400        /* free the defrag_q queue and return the prframe */
1401        rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1402
1403        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Performance defrag!!!!!\n"));
1404
1405        return prframe;
1406}
1407
1408/* check if need to defrag, if needed queue the frame to defrag_q */
1409struct recv_frame *recvframe_chk_defrag(struct adapter *padapter,
1410                                        struct recv_frame *precv_frame)
1411{
1412        u8      ismfrag;
1413        u8      fragnum;
1414        u8      *psta_addr;
1415        struct recv_frame *pfhdr;
1416        struct sta_info *psta;
1417        struct sta_priv *pstapriv;
1418        struct list_head *phead;
1419        struct recv_frame *prtnframe = NULL;
1420        struct __queue *pfree_recv_queue, *pdefrag_q;
1421
1422        pstapriv = &padapter->stapriv;
1423
1424        pfhdr = precv_frame;
1425
1426        pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1427
1428        /* need to define struct of wlan header frame ctrl */
1429        ismfrag = pfhdr->attrib.mfrag;
1430        fragnum = pfhdr->attrib.frag_num;
1431
1432        psta_addr = pfhdr->attrib.ta;
1433        psta = rtw_get_stainfo(pstapriv, psta_addr);
1434        if (psta == NULL) {
1435                u8 type = GetFrameType(pfhdr->pkt->data);
1436
1437                if (type != WIFI_DATA_TYPE) {
1438                        psta = rtw_get_bcmc_stainfo(padapter);
1439                        pdefrag_q = &psta->sta_recvpriv.defrag_q;
1440                } else {
1441                        pdefrag_q = NULL;
1442                }
1443        } else {
1444                pdefrag_q = &psta->sta_recvpriv.defrag_q;
1445        }
1446
1447        if ((ismfrag == 0) && (fragnum == 0))
1448                prtnframe = precv_frame;/* isn't a fragment frame */
1449
1450        if (ismfrag == 1) {
1451                /* 0~(n-1) fragment frame */
1452                /* enqueue to defraf_g */
1453                if (pdefrag_q != NULL) {
1454                        if (fragnum == 0) {
1455                                /* the first fragment */
1456                                if (!list_empty(&pdefrag_q->queue))
1457                                        /* free current defrag_q */
1458                                        rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
1459                        }
1460
1461                        /* Then enqueue the 0~(n-1) fragment into the defrag_q */
1462
1463                        phead = get_list_head(pdefrag_q);
1464                        list_add_tail(&pfhdr->list, phead);
1465
1466                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Enqueuq: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1467
1468                        prtnframe = NULL;
1469                } else {
1470                        /* can't find this ta's defrag_queue, so free this recv_frame */
1471                        rtw_free_recvframe(precv_frame, pfree_recv_queue);
1472                        prtnframe = NULL;
1473                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q==NULL: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1474                }
1475        }
1476
1477        if ((ismfrag == 0) && (fragnum != 0)) {
1478                /* the last fragment frame */
1479                /* enqueue the last fragment */
1480                if (pdefrag_q != NULL) {
1481                        phead = get_list_head(pdefrag_q);
1482                        list_add_tail(&pfhdr->list, phead);
1483
1484                        /* call recvframe_defrag to defrag */
1485                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("defrag: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1486                        precv_frame = recvframe_defrag(padapter, pdefrag_q);
1487                        prtnframe = precv_frame;
1488                } else {
1489                        /* can't find this ta's defrag_queue, so free this recv_frame */
1490                        rtw_free_recvframe(precv_frame, pfree_recv_queue);
1491                        prtnframe = NULL;
1492                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q==NULL: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1493                }
1494        }
1495
1496        if ((prtnframe != NULL) && (prtnframe->attrib.privacy)) {
1497                /* after defrag we must check tkip mic code */
1498                if (recvframe_chkmic(padapter,  prtnframe) == _FAIL) {
1499                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic(padapter,  prtnframe)==_FAIL\n"));
1500                        rtw_free_recvframe(prtnframe, pfree_recv_queue);
1501                        prtnframe = NULL;
1502                }
1503        }
1504
1505        return prtnframe;
1506}
1507
1508static int amsdu_to_msdu(struct adapter *padapter, struct recv_frame *prframe)
1509{
1510        int     a_len, padding_len;
1511        u16     eth_type, nSubframe_Length;
1512        u8      nr_subframes, i;
1513        unsigned char *pdata;
1514        struct rx_pkt_attrib *pattrib;
1515        struct sk_buff *sub_skb, *subframes[MAX_SUBFRAME_COUNT];
1516        struct recv_priv *precvpriv = &padapter->recvpriv;
1517        struct __queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
1518
1519        nr_subframes = 0;
1520        pattrib = &prframe->attrib;
1521
1522        a_len = prframe->pkt->len;
1523
1524        pdata = prframe->pkt->data;
1525
1526        while (a_len > ETH_HLEN) {
1527                /* Offset 12 denote 2 mac address */
1528                nSubframe_Length = get_unaligned_be16(pdata + 12);
1529
1530                if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1531                        DBG_88E("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
1532                        goto exit;
1533                }
1534
1535                /* move the data point to data content */
1536                pdata += ETH_HLEN;
1537                a_len -= ETH_HLEN;
1538
1539                /* Allocate new skb for releasing to upper layer */
1540                sub_skb = dev_alloc_skb(nSubframe_Length + 12);
1541                if (sub_skb) {
1542                        skb_reserve(sub_skb, 12);
1543                        skb_put_data(sub_skb, pdata, nSubframe_Length);
1544                } else {
1545                        sub_skb = skb_clone(prframe->pkt, GFP_ATOMIC);
1546                        if (sub_skb) {
1547                                sub_skb->data = pdata;
1548                                sub_skb->len = nSubframe_Length;
1549                                skb_set_tail_pointer(sub_skb, nSubframe_Length);
1550                        } else {
1551                                DBG_88E("skb_clone() Fail!!! , nr_subframes=%d\n", nr_subframes);
1552                                break;
1553                        }
1554                }
1555
1556                subframes[nr_subframes++] = sub_skb;
1557
1558                if (nr_subframes >= MAX_SUBFRAME_COUNT) {
1559                        DBG_88E("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1560                        break;
1561                }
1562
1563                pdata += nSubframe_Length;
1564                a_len -= nSubframe_Length;
1565                if (a_len != 0) {
1566                        padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4-1));
1567                        if (padding_len == 4)
1568                                padding_len = 0;
1569
1570                        if (a_len < padding_len)
1571                                goto exit;
1572
1573                        pdata += padding_len;
1574                        a_len -= padding_len;
1575                }
1576        }
1577
1578        for (i = 0; i < nr_subframes; i++) {
1579                sub_skb = subframes[i];
1580                /* convert hdr + possible LLC headers into Ethernet header */
1581                eth_type = get_unaligned_be16(&sub_skb->data[6]);
1582                if (sub_skb->len >= 8 &&
1583                    ((!memcmp(sub_skb->data, rtw_rfc1042_header, SNAP_SIZE) &&
1584                          eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
1585                         !memcmp(sub_skb->data, rtw_bridge_tunnel_header, SNAP_SIZE))) {
1586                        /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1587                        skb_pull(sub_skb, SNAP_SIZE);
1588                        memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
1589                        memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
1590                } else {
1591                        __be16 len;
1592                        /* Leave Ethernet header part of hdr and full payload */
1593                        len = htons(sub_skb->len);
1594                        memcpy(skb_push(sub_skb, 2), &len, 2);
1595                        memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
1596                        memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
1597                }
1598
1599                /* Indicate the packets to upper layer */
1600                /*  Insert NAT2.5 RX here! */
1601                sub_skb->protocol = eth_type_trans(sub_skb, padapter->pnetdev);
1602                sub_skb->dev = padapter->pnetdev;
1603
1604                sub_skb->ip_summed = CHECKSUM_NONE;
1605
1606                netif_rx(sub_skb);
1607        }
1608
1609exit:
1610        rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
1611
1612        return _SUCCESS;
1613}
1614
1615static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
1616{
1617        u8      wsize = preorder_ctrl->wsize_b;
1618        u16     wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/*  4096; */
1619
1620        /*  Rx Reorder initialize condition. */
1621        if (preorder_ctrl->indicate_seq == 0xFFFF)
1622                preorder_ctrl->indicate_seq = seq_num;
1623
1624        /*  Drop out the packet which SeqNum is smaller than WinStart */
1625        if (SN_LESS(seq_num, preorder_ctrl->indicate_seq))
1626                return false;
1627
1628        /*  */
1629        /*  Sliding window manipulation. Conditions includes: */
1630        /*  1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
1631        /*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
1632        /*  */
1633        if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
1634                preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
1635        } else if (SN_LESS(wend, seq_num)) {
1636                if (seq_num >= (wsize - 1))
1637                        preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
1638                else
1639                        preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
1640        }
1641
1642        return true;
1643}
1644
1645static int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl,
1646                                     struct recv_frame *prframe)
1647{
1648        struct rx_pkt_attrib *pattrib = &prframe->attrib;
1649        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1650        struct list_head *phead, *plist;
1651        struct recv_frame *hdr;
1652        struct rx_pkt_attrib *pnextattrib;
1653
1654        phead = get_list_head(ppending_recvframe_queue);
1655        plist = phead->next;
1656
1657        while (phead != plist) {
1658                hdr = container_of(plist, struct recv_frame, list);
1659                pnextattrib = &hdr->attrib;
1660
1661                if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
1662                        plist = plist->next;
1663                else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
1664                        return false;
1665                else
1666                        break;
1667        }
1668
1669        list_del_init(&(prframe->list));
1670
1671        list_add_tail(&(prframe->list), plist);
1672        return true;
1673}
1674
1675static int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
1676{
1677        struct list_head *phead, *plist;
1678        struct recv_frame *prframe;
1679        struct recv_frame *prhdr;
1680        struct rx_pkt_attrib *pattrib;
1681        int bPktInBuf = false;
1682        struct recv_priv *precvpriv = &padapter->recvpriv;
1683        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1684
1685        phead =         get_list_head(ppending_recvframe_queue);
1686        plist = phead->next;
1687
1688        /*  Handling some condition for forced indicate case. */
1689        if (bforced) {
1690                if (list_empty(phead))
1691                        return true;
1692
1693                prhdr = container_of(plist, struct recv_frame, list);
1694                pattrib = &prhdr->attrib;
1695                preorder_ctrl->indicate_seq = pattrib->seq_num;
1696        }
1697
1698        /*  Prepare indication list and indication. */
1699        /*  Check if there is any packet need indicate. */
1700        while (!list_empty(phead)) {
1701                prhdr = container_of(plist, struct recv_frame, list);
1702                prframe = prhdr;
1703                pattrib = &prframe->attrib;
1704
1705                if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
1706                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
1707                                 ("recv_indicatepkts_in_order: indicate=%d seq=%d amsdu=%d\n",
1708                                  preorder_ctrl->indicate_seq, pattrib->seq_num, pattrib->amsdu));
1709                        plist = plist->next;
1710                        list_del_init(&(prframe->list));
1711
1712                        if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num))
1713                                preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
1714
1715                        /* Set this as a lock to make sure that only one thread is indicating packet. */
1716
1717                        /* indicate this recv_frame */
1718                        if (!pattrib->amsdu) {
1719                                if ((!padapter->bDriverStopped) &&
1720                                    (!padapter->bSurpriseRemoved))
1721                                        rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
1722                        } else if (pattrib->amsdu == 1) {
1723                                if (amsdu_to_msdu(padapter, prframe) != _SUCCESS)
1724                                        rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
1725                        } else {
1726                                /* error condition; */
1727                        }
1728
1729                        /* Update local variables. */
1730                        bPktInBuf = false;
1731                } else {
1732                        bPktInBuf = true;
1733                        break;
1734                }
1735        }
1736        return bPktInBuf;
1737}
1738
1739static int recv_indicatepkt_reorder(struct adapter *padapter,
1740                                    struct recv_frame *prframe)
1741{
1742        int retval = _SUCCESS;
1743        struct rx_pkt_attrib *pattrib = &prframe->attrib;
1744        struct recv_reorder_ctrl *preorder_ctrl = prframe->preorder_ctrl;
1745        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1746
1747        if (!pattrib->amsdu) {
1748                /* s1. */
1749                wlanhdr_to_ethhdr(prframe);
1750
1751                if ((pattrib->qos != 1) || (pattrib->eth_type == 0x0806) ||
1752                    (pattrib->ack_policy != 0)) {
1753                        if ((!padapter->bDriverStopped) &&
1754                            (!padapter->bSurpriseRemoved)) {
1755                                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@  recv_indicatepkt_reorder -recv_func recv_indicatepkt\n"));
1756
1757                                rtw_recv_indicatepkt(padapter, prframe);
1758                                return _SUCCESS;
1759                        }
1760
1761                        return _FAIL;
1762                }
1763
1764                if (!preorder_ctrl->enable) {
1765                        /* indicate this recv_frame */
1766                        preorder_ctrl->indicate_seq = pattrib->seq_num;
1767                        rtw_recv_indicatepkt(padapter, prframe);
1768
1769                        preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
1770                        return _SUCCESS;
1771                }
1772        } else if (pattrib->amsdu == 1) { /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
1773                if (!preorder_ctrl->enable) {
1774                        preorder_ctrl->indicate_seq = pattrib->seq_num;
1775                        retval = amsdu_to_msdu(padapter, prframe);
1776
1777                        preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
1778                        return retval;
1779                }
1780        }
1781
1782        spin_lock_bh(&ppending_recvframe_queue->lock);
1783
1784        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
1785                 ("recv_indicatepkt_reorder: indicate=%d seq=%d\n",
1786                  preorder_ctrl->indicate_seq, pattrib->seq_num));
1787
1788        /* s2. check if winstart_b(indicate_seq) needs to been updated */
1789        if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num)) {
1790                rtw_recv_indicatepkt(padapter, prframe);
1791
1792                spin_unlock_bh(&ppending_recvframe_queue->lock);
1793
1794                goto _success_exit;
1795        }
1796
1797        /* s3. Insert all packet into Reorder Queue to maintain its ordering. */
1798        if (!enqueue_reorder_recvframe(preorder_ctrl, prframe))
1799                goto _err_exit;
1800
1801        /* s4. */
1802        /*  Indication process. */
1803        /*  After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
1804        /*  with the SeqNum smaller than latest WinStart and buffer other packets. */
1805        /*  */
1806        /*  For Rx Reorder condition: */
1807        /*  1. All packets with SeqNum smaller than WinStart => Indicate */
1808        /*  2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
1809        /*  */
1810
1811        /* recv_indicatepkts_in_order(padapter, preorder_ctrl, true); */
1812        if (recv_indicatepkts_in_order(padapter, preorder_ctrl, false)) {
1813                mod_timer(&preorder_ctrl->reordering_ctrl_timer,
1814                          jiffies + msecs_to_jiffies(REORDER_WAIT_TIME));
1815                spin_unlock_bh(&ppending_recvframe_queue->lock);
1816        } else {
1817                spin_unlock_bh(&ppending_recvframe_queue->lock);
1818                del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
1819        }
1820
1821_success_exit:
1822
1823        return _SUCCESS;
1824
1825_err_exit:
1826
1827        spin_unlock_bh(&ppending_recvframe_queue->lock);
1828
1829        return _FAIL;
1830}
1831
1832void rtw_reordering_ctrl_timeout_handler(unsigned long data)
1833{
1834        struct recv_reorder_ctrl *preorder_ctrl = (struct recv_reorder_ctrl *)data;
1835        struct adapter *padapter = preorder_ctrl->padapter;
1836        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1837
1838        if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
1839                return;
1840
1841        spin_lock_bh(&ppending_recvframe_queue->lock);
1842
1843        if (recv_indicatepkts_in_order(padapter, preorder_ctrl, true) == true)
1844                mod_timer(&preorder_ctrl->reordering_ctrl_timer,
1845                          jiffies + msecs_to_jiffies(REORDER_WAIT_TIME));
1846
1847        spin_unlock_bh(&ppending_recvframe_queue->lock);
1848}
1849
1850static int process_recv_indicatepkts(struct adapter *padapter,
1851                                     struct recv_frame *prframe)
1852{
1853        int retval = _SUCCESS;
1854        struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
1855        struct ht_priv  *phtpriv = &pmlmepriv->htpriv;
1856
1857        if (phtpriv->ht_option) {  /* B/G/N Mode */
1858                if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) {
1859                        /*  including perform A-MPDU Rx Ordering Buffer Control */
1860                        if ((!padapter->bDriverStopped) &&
1861                            (!padapter->bSurpriseRemoved)) {
1862                                retval = _FAIL;
1863                                return retval;
1864                        }
1865                }
1866        } else { /* B/G mode */
1867                retval = wlanhdr_to_ethhdr(prframe);
1868                if (retval != _SUCCESS) {
1869                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("wlanhdr_to_ethhdr: drop pkt\n"));
1870                        return retval;
1871                }
1872
1873                if ((!padapter->bDriverStopped) &&
1874                    (!padapter->bSurpriseRemoved)) {
1875                        /* indicate this recv_frame */
1876                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func recv_indicatepkt\n"));
1877                        rtw_recv_indicatepkt(padapter, prframe);
1878                } else {
1879                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func free_indicatepkt\n"));
1880
1881                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
1882                        retval = _FAIL;
1883                        return retval;
1884                }
1885        }
1886
1887        return retval;
1888}
1889
1890static int recv_func_posthandle(struct adapter *padapter,
1891                                struct recv_frame *prframe)
1892{
1893        int ret = _SUCCESS;
1894        struct recv_frame *orig_prframe = prframe;
1895        struct recv_priv *precvpriv = &padapter->recvpriv;
1896        struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1897
1898        /*  DATA FRAME */
1899        LedControl8188eu(padapter, LED_CTL_RX);
1900
1901        prframe = decryptor(padapter, prframe);
1902        if (prframe == NULL) {
1903                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decryptor: drop pkt\n"));
1904                ret = _FAIL;
1905                goto _recv_data_drop;
1906        }
1907
1908        prframe = recvframe_chk_defrag(padapter, prframe);
1909        if (prframe == NULL) {
1910                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chk_defrag: drop pkt\n"));
1911                goto _recv_data_drop;
1912        }
1913
1914        prframe = portctrl(padapter, prframe);
1915        if (prframe == NULL) {
1916                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("portctrl: drop pkt\n"));
1917                ret = _FAIL;
1918                goto _recv_data_drop;
1919        }
1920
1921        count_rx_stats(padapter, prframe, NULL);
1922
1923        ret = process_recv_indicatepkts(padapter, prframe);
1924        if (ret != _SUCCESS) {
1925                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recv_func: process_recv_indicatepkts fail!\n"));
1926                rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
1927                goto _recv_data_drop;
1928        }
1929        return ret;
1930
1931_recv_data_drop:
1932        precvpriv->rx_drop++;
1933        return ret;
1934}
1935
1936static int recv_func(struct adapter *padapter, struct recv_frame *rframe)
1937{
1938        int ret;
1939        struct rx_pkt_attrib *prxattrib = &rframe->attrib;
1940        struct security_priv *psecuritypriv = &padapter->securitypriv;
1941        struct mlme_priv *mlmepriv = &padapter->mlmepriv;
1942        struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1943
1944        /* check if need to handle uc_swdec_pending_queue*/
1945        if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey) {
1946                struct recv_frame *pending_frame;
1947
1948                while ((pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
1949                        if (recv_func_posthandle(padapter, pending_frame) == _SUCCESS)
1950                                DBG_88E("%s: dequeue uc_swdec_pending_queue\n", __func__);
1951                }
1952        }
1953
1954        /* check the frame crtl field and decache */
1955        ret = validate_recv_frame(padapter, rframe);
1956        if (ret != _SUCCESS) {
1957                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recv_func: validate_recv_frame fail! drop pkt\n"));
1958                rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
1959        } else {
1960                /* check if need to enqueue into uc_swdec_pending_queue*/
1961                if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
1962                    !IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
1963                    prxattrib->bdecrypted == 0 &&
1964                    !is_wep_enc(psecuritypriv->dot11PrivacyAlgrthm) &&
1965                    !psecuritypriv->busetkipkey) {
1966                        rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
1967                        DBG_88E("%s: no key, enqueue uc_swdec_pending_queue\n", __func__);
1968                        goto exit;
1969                }
1970
1971                ret = recv_func_posthandle(padapter, rframe);
1972        }
1973
1974exit:
1975        return ret;
1976}
1977
1978s32 rtw_recv_entry(struct recv_frame *precvframe)
1979{
1980        struct adapter *padapter;
1981        struct recv_priv *precvpriv;
1982        s32 ret = _SUCCESS;
1983
1984        padapter = precvframe->adapter;
1985
1986        precvpriv = &padapter->recvpriv;
1987
1988        ret = recv_func(padapter, precvframe);
1989        if (ret == _FAIL) {
1990                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("rtw_recv_entry: recv_func return fail!!!\n"));
1991                goto _recv_entry_drop;
1992        }
1993
1994        precvpriv->rx_pkts++;
1995
1996        return ret;
1997
1998_recv_entry_drop:
1999        return ret;
2000}
2001
2002static void rtw_signal_stat_timer_hdl(unsigned long data)
2003{
2004        struct adapter *adapter = (struct adapter *)data;
2005        struct recv_priv *recvpriv = &adapter->recvpriv;
2006
2007        u32 tmp_s, tmp_q;
2008        u8 avg_signal_strength = 0;
2009        u8 avg_signal_qual = 0;
2010        u8 _alpha = 3; /*  this value is based on converging_constant = 5000 and sampling_interval = 1000 */
2011
2012        if (recvpriv->signal_strength_data.update_req == 0) {
2013                /* update_req is clear, means we got rx */
2014                avg_signal_strength = recvpriv->signal_strength_data.avg_val;
2015                /* after avg_vals are acquired, we can re-stat the signal
2016                 * values
2017                 */
2018                recvpriv->signal_strength_data.update_req = 1;
2019        }
2020
2021        if (recvpriv->signal_qual_data.update_req == 0) {
2022                /* update_req is clear, means we got rx */
2023                avg_signal_qual = recvpriv->signal_qual_data.avg_val;
2024                /* after avg_vals are acquired, we can re-stat the signal
2025                 * values
2026                 */
2027                recvpriv->signal_qual_data.update_req = 1;
2028        }
2029
2030        /* update value of signal_strength, rssi, signal_qual */
2031        if (check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY) == false) {
2032                tmp_s = avg_signal_strength +
2033                        (_alpha - 1) * recvpriv->signal_strength;
2034                tmp_s = DIV_ROUND_UP(tmp_s, _alpha);
2035                if (tmp_s > 100)
2036                        tmp_s = 100;
2037
2038                tmp_q = avg_signal_qual +
2039                        (_alpha - 1) * recvpriv->signal_qual;
2040                tmp_q = DIV_ROUND_UP(tmp_q, _alpha);
2041                if (tmp_q > 100)
2042                        tmp_q = 100;
2043
2044                recvpriv->signal_strength = tmp_s;
2045                recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
2046                recvpriv->signal_qual = tmp_q;
2047        }
2048
2049        rtw_set_signal_stat_timer(recvpriv);
2050}
2051