linux/drivers/staging/rtl8723bs/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 <drv_types.h>
  18#include <rtw_debug.h>
  19#include <linux/jiffies.h>
  20#include <rtw_recv.h>
  21
  22static u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
  23static u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
  24
  25u8 rtw_rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  26/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  27u8 rtw_bridge_tunnel_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  28
  29static void rtw_signal_stat_timer_hdl(struct timer_list *t);
  30
  31void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
  32{
  33        memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
  34
  35        spin_lock_init(&psta_recvpriv->lock);
  36
  37        /* for (i = 0; i<MAX_RX_NUMBLKS; i++) */
  38        /*      _rtw_init_queue(&psta_recvpriv->blk_strms[i]); */
  39
  40        _rtw_init_queue(&psta_recvpriv->defrag_q);
  41}
  42
  43sint _rtw_init_recv_priv(struct recv_priv *precvpriv, struct adapter *padapter)
  44{
  45        sint i;
  46        union recv_frame *precvframe;
  47        sint    res = _SUCCESS;
  48
  49        spin_lock_init(&precvpriv->lock);
  50
  51        _rtw_init_queue(&precvpriv->free_recv_queue);
  52        _rtw_init_queue(&precvpriv->recv_pending_queue);
  53        _rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
  54
  55        precvpriv->adapter = padapter;
  56
  57        precvpriv->free_recvframe_cnt = NR_RECVFRAME;
  58
  59        precvpriv->pallocated_frame_buf = vzalloc(NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
  60
  61        if (precvpriv->pallocated_frame_buf == NULL) {
  62                res = _FAIL;
  63                goto exit;
  64        }
  65
  66        precvpriv->precv_frame_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(precvpriv->pallocated_frame_buf), RXFRAME_ALIGN_SZ);
  67        /* precvpriv->precv_frame_buf = precvpriv->pallocated_frame_buf + RXFRAME_ALIGN_SZ - */
  68        /*                                              ((SIZE_PTR) (precvpriv->pallocated_frame_buf) &(RXFRAME_ALIGN_SZ-1)); */
  69
  70        precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
  71
  72
  73        for (i = 0; i < NR_RECVFRAME; i++) {
  74                INIT_LIST_HEAD(&(precvframe->u.list));
  75
  76                list_add_tail(&(precvframe->u.list), &(precvpriv->free_recv_queue.queue));
  77
  78                res = rtw_os_recv_resource_alloc(padapter, precvframe);
  79
  80                precvframe->u.hdr.len = 0;
  81
  82                precvframe->u.hdr.adapter = padapter;
  83                precvframe++;
  84
  85        }
  86
  87        res = rtw_hal_init_recv_priv(padapter);
  88
  89        timer_setup(&precvpriv->signal_stat_timer, rtw_signal_stat_timer_hdl,
  90                    0);
  91
  92        precvpriv->signal_stat_sampling_interval = 2000; /* ms */
  93
  94        rtw_set_signal_stat_timer(precvpriv);
  95
  96exit:
  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        rtw_os_recv_resource_free(precvpriv);
 107
 108        if (precvpriv->pallocated_frame_buf)
 109                vfree(precvpriv->pallocated_frame_buf);
 110
 111        rtw_hal_free_recv_priv(padapter);
 112}
 113
 114union recv_frame *_rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
 115{
 116
 117        union recv_frame  *precvframe;
 118        struct list_head        *plist, *phead;
 119        struct adapter *padapter;
 120        struct recv_priv *precvpriv;
 121
 122        if (list_empty(&pfree_recv_queue->queue))
 123                precvframe = NULL;
 124        else{
 125                phead = get_list_head(pfree_recv_queue);
 126
 127                plist = get_next(phead);
 128
 129                precvframe = (union recv_frame *)plist;
 130
 131                list_del_init(&precvframe->u.hdr.list);
 132                padapter = precvframe->u.hdr.adapter;
 133                if (padapter != NULL) {
 134                        precvpriv = &padapter->recvpriv;
 135                        if (pfree_recv_queue == &precvpriv->free_recv_queue)
 136                                precvpriv->free_recvframe_cnt--;
 137                }
 138        }
 139        return precvframe;
 140}
 141
 142union recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
 143{
 144        union recv_frame  *precvframe;
 145
 146        spin_lock_bh(&pfree_recv_queue->lock);
 147
 148        precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
 149
 150        spin_unlock_bh(&pfree_recv_queue->lock);
 151
 152        return precvframe;
 153}
 154
 155int rtw_free_recvframe(union recv_frame *precvframe, struct __queue *pfree_recv_queue)
 156{
 157        struct adapter *padapter = precvframe->u.hdr.adapter;
 158        struct recv_priv *precvpriv = &padapter->recvpriv;
 159
 160        rtw_os_free_recvframe(precvframe);
 161
 162
 163        spin_lock_bh(&pfree_recv_queue->lock);
 164
 165        list_del_init(&(precvframe->u.hdr.list));
 166
 167        precvframe->u.hdr.len = 0;
 168
 169        list_add_tail(&(precvframe->u.hdr.list), get_list_head(pfree_recv_queue));
 170
 171        if (padapter != NULL) {
 172                if (pfree_recv_queue == &precvpriv->free_recv_queue)
 173                                precvpriv->free_recvframe_cnt++;
 174        }
 175        spin_unlock_bh(&pfree_recv_queue->lock);
 176        return _SUCCESS;
 177}
 178
 179
 180
 181
 182sint _rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
 183{
 184
 185        struct adapter *padapter = precvframe->u.hdr.adapter;
 186        struct recv_priv *precvpriv = &padapter->recvpriv;
 187
 188        /* INIT_LIST_HEAD(&(precvframe->u.hdr.list)); */
 189        list_del_init(&(precvframe->u.hdr.list));
 190
 191
 192        list_add_tail(&(precvframe->u.hdr.list), get_list_head(queue));
 193
 194        if (padapter != NULL)
 195                if (queue == &precvpriv->free_recv_queue)
 196                        precvpriv->free_recvframe_cnt++;
 197
 198        return _SUCCESS;
 199}
 200
 201sint rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
 202{
 203        sint ret;
 204
 205        /* _spinlock(&pfree_recv_queue->lock); */
 206        spin_lock_bh(&queue->lock);
 207        ret = _rtw_enqueue_recvframe(precvframe, queue);
 208        /* spin_unlock(&pfree_recv_queue->lock); */
 209        spin_unlock_bh(&queue->lock);
 210
 211        return ret;
 212}
 213
 214/*
 215sint    rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
 216{
 217        return rtw_free_recvframe(precvframe, queue);
 218}
 219*/
 220
 221
 222
 223
 224/*
 225caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
 226pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
 227
 228using spinlock to protect
 229
 230*/
 231
 232void rtw_free_recvframe_queue(struct __queue *pframequeue,  struct __queue *pfree_recv_queue)
 233{
 234        union   recv_frame      *precvframe;
 235        struct list_head        *plist, *phead;
 236
 237        spin_lock(&pframequeue->lock);
 238
 239        phead = get_list_head(pframequeue);
 240        plist = get_next(phead);
 241
 242        while (phead != plist) {
 243                precvframe = (union recv_frame *)plist;
 244
 245                plist = get_next(plist);
 246
 247                rtw_free_recvframe(precvframe, pfree_recv_queue);
 248        }
 249
 250        spin_unlock(&pframequeue->lock);
 251}
 252
 253u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter)
 254{
 255        u32 cnt = 0;
 256        union recv_frame *pending_frame;
 257        while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
 258                rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
 259                cnt++;
 260        }
 261
 262        if (cnt)
 263                DBG_871X(FUNC_ADPT_FMT" dequeue %d\n", FUNC_ADPT_ARG(adapter), cnt);
 264
 265        return cnt;
 266}
 267
 268
 269sint rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, struct __queue *queue)
 270{
 271        spin_lock_bh(&queue->lock);
 272
 273        list_del_init(&precvbuf->list);
 274        list_add(&precvbuf->list, get_list_head(queue));
 275
 276        spin_unlock_bh(&queue->lock);
 277
 278        return _SUCCESS;
 279}
 280
 281sint rtw_enqueue_recvbuf(struct recv_buf *precvbuf, struct __queue *queue)
 282{
 283        spin_lock_bh(&queue->lock);
 284
 285        list_del_init(&precvbuf->list);
 286
 287        list_add_tail(&precvbuf->list, get_list_head(queue));
 288        spin_unlock_bh(&queue->lock);
 289        return _SUCCESS;
 290
 291}
 292
 293struct recv_buf *rtw_dequeue_recvbuf(struct __queue *queue)
 294{
 295        struct recv_buf *precvbuf;
 296        struct list_head        *plist, *phead;
 297
 298        spin_lock_bh(&queue->lock);
 299
 300        if (list_empty(&queue->queue))
 301                precvbuf = NULL;
 302        else{
 303                phead = get_list_head(queue);
 304
 305                plist = get_next(phead);
 306
 307                precvbuf = LIST_CONTAINOR(plist, struct recv_buf, list);
 308
 309                list_del_init(&precvbuf->list);
 310
 311        }
 312
 313        spin_unlock_bh(&queue->lock);
 314
 315        return precvbuf;
 316
 317}
 318
 319sint recvframe_chkmic(struct adapter *adapter,  union recv_frame *precvframe);
 320sint recvframe_chkmic(struct adapter *adapter,  union recv_frame *precvframe)
 321{
 322
 323        sint    i, res = _SUCCESS;
 324        u32 datalen;
 325        u8 miccode[8];
 326        u8 bmic_err = false, brpt_micerror = true;
 327        u8 *pframe, *payload, *pframemic;
 328        u8 *mickey;
 329        /* u8 *iv, rxdata_key_idx = 0; */
 330        struct  sta_info        *stainfo;
 331        struct  rx_pkt_attrib   *prxattrib = &precvframe->u.hdr.attrib;
 332        struct  security_priv *psecuritypriv = &adapter->securitypriv;
 333
 334        struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
 335        struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
 336
 337        stainfo = rtw_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
 338
 339        if (prxattrib->encrypt == _TKIP_) {
 340                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:prxattrib->encrypt == _TKIP_\n"));
 341                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",
 342                        prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2], prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5]));
 343
 344                /* calculate mic code */
 345                if (stainfo != NULL) {
 346                        if (IS_MCAST(prxattrib->ra)) {
 347                                /* mickey =&psecuritypriv->dot118021XGrprxmickey.skey[0]; */
 348                                /* iv = precvframe->u.hdr.rx_data+prxattrib->hdrlen; */
 349                                /* rxdata_key_idx =(((iv[3])>>6)&0x3) ; */
 350                                mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
 351
 352                                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic: bcmc key\n"));
 353                                /* DBG_871X("\n recvframe_chkmic: bcmc key psecuritypriv->dot118021XGrpKeyid(%d), pmlmeinfo->key_index(%d) , recv key_id(%d)\n", */
 354                                /*                                                              psecuritypriv->dot118021XGrpKeyid, pmlmeinfo->key_index, rxdata_key_idx); */
 355
 356                                if (psecuritypriv->binstallGrpkey == false) {
 357                                        res = _FAIL;
 358                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n"));
 359                                        DBG_871X("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
 360                                        goto exit;
 361                                }
 362                        } else {
 363                                mickey = &stainfo->dot11tkiprxmickey.skey[0];
 364                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic: unicast key\n"));
 365                        }
 366
 367                        datalen = precvframe->u.hdr.len-prxattrib->hdrlen-prxattrib->iv_len-prxattrib->icv_len-8;/* icv_len included the mic code */
 368                        pframe = precvframe->u.hdr.rx_data;
 369                        payload = pframe+prxattrib->hdrlen+prxattrib->iv_len;
 370
 371                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n prxattrib->iv_len =%d prxattrib->icv_len =%d\n", prxattrib->iv_len, prxattrib->icv_len));
 372
 373
 374                        rtw_seccalctkipmic(mickey, pframe, payload, datalen, &miccode[0], (unsigned char)prxattrib->priority); /* care the length of the data */
 375
 376                        pframemic = payload+datalen;
 377
 378                        bmic_err = false;
 379
 380                        for (i = 0; i < 8; i++) {
 381                                if (miccode[i] != *(pframemic+i)) {
 382                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic:miccode[%d](%02x) != *(pframemic+%d)(%02x) ", i, miccode[i], i, *(pframemic+i)));
 383                                        bmic_err = true;
 384                                }
 385                        }
 386
 387
 388                        if (bmic_err == true) {
 389
 390                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n *(pframemic-8)-*(pframemic-1) = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
 391                                        *(pframemic-8), *(pframemic-7), *(pframemic-6), *(pframemic-5), *(pframemic-4), *(pframemic-3), *(pframemic-2), *(pframemic-1)));
 392                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n *(pframemic-16)-*(pframemic-9) = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
 393                                        *(pframemic-16), *(pframemic-15), *(pframemic-14), *(pframemic-13), *(pframemic-12), *(pframemic-11), *(pframemic-10), *(pframemic-9)));
 394
 395                                {
 396                                        uint i;
 397                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n ======demp packet (len =%d) ======\n", precvframe->u.hdr.len));
 398                                        for (i = 0; i < precvframe->u.hdr.len; i = i+8) {
 399                                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
 400                                                        *(precvframe->u.hdr.rx_data+i), *(precvframe->u.hdr.rx_data+i+1),
 401                                                        *(precvframe->u.hdr.rx_data+i+2), *(precvframe->u.hdr.rx_data+i+3),
 402                                                        *(precvframe->u.hdr.rx_data+i+4), *(precvframe->u.hdr.rx_data+i+5),
 403                                                        *(precvframe->u.hdr.rx_data+i+6), *(precvframe->u.hdr.rx_data+i+7)));
 404                                        }
 405                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n ======demp packet end [len =%d]======\n", precvframe->u.hdr.len));
 406                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n hrdlen =%d,\n", prxattrib->hdrlen));
 407                                }
 408
 409                                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ra = 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x psecuritypriv->binstallGrpkey =%d ",
 410                                        prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2],
 411                                        prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5], psecuritypriv->binstallGrpkey));
 412
 413                                /*  double check key_index for some timing issue , */
 414                                /*  cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
 415                                if ((IS_MCAST(prxattrib->ra) == true)  && (prxattrib->key_index != pmlmeinfo->key_index))
 416                                        brpt_micerror = false;
 417
 418                                if ((prxattrib->bdecrypted == true) && (brpt_micerror == true)) {
 419                                        rtw_handle_tkip_mic_err(adapter, (u8)IS_MCAST(prxattrib->ra));
 420                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted =%d ", prxattrib->bdecrypted));
 421                                        DBG_871X(" mic error :prxattrib->bdecrypted =%d\n", prxattrib->bdecrypted);
 422                                } else{
 423                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted =%d ", prxattrib->bdecrypted));
 424                                        DBG_871X(" mic error :prxattrib->bdecrypted =%d\n", prxattrib->bdecrypted);
 425                                }
 426
 427                                res = _FAIL;
 428
 429                        } else {
 430                                /* mic checked ok */
 431                                if ((psecuritypriv->bcheck_grpkey == false) && (IS_MCAST(prxattrib->ra) == true)) {
 432                                        psecuritypriv->bcheck_grpkey = true;
 433                                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("psecuritypriv->bcheck_grpkey =true"));
 434                                }
 435                        }
 436
 437                } else
 438                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic: rtw_get_stainfo == NULL!!!\n"));
 439
 440                recvframe_pull_tail(precvframe, 8);
 441
 442        }
 443
 444exit:
 445        return res;
 446
 447}
 448
 449/* decrypt and set the ivlen, icvlen of the recv_frame */
 450union recv_frame *decryptor(struct adapter *padapter, union recv_frame *precv_frame);
 451union recv_frame *decryptor(struct adapter *padapter, union recv_frame *precv_frame)
 452{
 453
 454        struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
 455        struct security_priv *psecuritypriv = &padapter->securitypriv;
 456        union recv_frame *return_packet = precv_frame;
 457        u32  res = _SUCCESS;
 458
 459        DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt);
 460
 461        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("prxstat->decrypted =%x prxattrib->encrypt = 0x%03x\n", prxattrib->bdecrypted, prxattrib->encrypt));
 462
 463        if (prxattrib->encrypt > 0) {
 464                u8 *iv = precv_frame->u.hdr.rx_data+prxattrib->hdrlen;
 465                prxattrib->key_index = (((iv[3])>>6)&0x3);
 466
 467                if (prxattrib->key_index > WEP_KEYS) {
 468                        DBG_871X("prxattrib->key_index(%d) > WEP_KEYS\n", prxattrib->key_index);
 469
 470                        switch (prxattrib->encrypt) {
 471                        case _WEP40_:
 472                        case _WEP104_:
 473                                prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
 474                                break;
 475                        case _TKIP_:
 476                        case _AES_:
 477                        default:
 478                                prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
 479                                break;
 480                        }
 481                }
 482        }
 483
 484        if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) || (psecuritypriv->sw_decrypt == true))) {
 485                psecuritypriv->hw_decrypted = false;
 486
 487                #ifdef DBG_RX_DECRYPTOR
 488                DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
 489                        __func__,
 490                        __LINE__,
 491                        prxattrib->bdecrypted,
 492                        prxattrib->encrypt,
 493                        psecuritypriv->hw_decrypted);
 494                #endif
 495
 496                switch (prxattrib->encrypt) {
 497                case _WEP40_:
 498                case _WEP104_:
 499                        DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_wep);
 500                        rtw_wep_decrypt(padapter, (u8 *)precv_frame);
 501                        break;
 502                case _TKIP_:
 503                        DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_tkip);
 504                        res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
 505                        break;
 506                case _AES_:
 507                        DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_aes);
 508                        res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
 509                        break;
 510                default:
 511                                break;
 512                }
 513        } else if (prxattrib->bdecrypted == 1
 514                && prxattrib->encrypt > 0
 515                && (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_)
 516                ) {
 517                DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_hw);
 518
 519                psecuritypriv->hw_decrypted = true;
 520                #ifdef DBG_RX_DECRYPTOR
 521                DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
 522                        __func__,
 523                        __LINE__,
 524                        prxattrib->bdecrypted,
 525                        prxattrib->encrypt,
 526                        psecuritypriv->hw_decrypted);
 527
 528                #endif
 529        } else {
 530                DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_unknown);
 531                #ifdef DBG_RX_DECRYPTOR
 532                DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
 533                        __func__,
 534                        __LINE__,
 535                        prxattrib->bdecrypted,
 536                        prxattrib->encrypt,
 537                        psecuritypriv->hw_decrypted);
 538                #endif
 539        }
 540
 541        if (res == _FAIL) {
 542                rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
 543                return_packet = NULL;
 544        } else
 545                prxattrib->bdecrypted = true;
 546
 547        return return_packet;
 548}
 549
 550/* set the security information in the recv_frame */
 551union recv_frame *portctrl(struct adapter *adapter, union recv_frame *precv_frame);
 552union recv_frame *portctrl(struct adapter *adapter, union recv_frame *precv_frame)
 553{
 554        u8 *psta_addr = NULL;
 555        u8 *ptr;
 556        uint  auth_alg;
 557        struct recv_frame_hdr *pfhdr;
 558        struct sta_info *psta;
 559        struct sta_priv *pstapriv;
 560        union recv_frame *prtnframe;
 561        u16 ether_type = 0;
 562        u16  eapol_type = 0x888e;/* for Funia BD's WPA issue */
 563        struct rx_pkt_attrib *pattrib;
 564
 565        pstapriv = &adapter->stapriv;
 566
 567        auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
 568
 569        ptr = get_recvframe_data(precv_frame);
 570        pfhdr = &precv_frame->u.hdr;
 571        pattrib = &pfhdr->attrib;
 572        psta_addr = pattrib->ta;
 573
 574        prtnframe = NULL;
 575
 576        psta = rtw_get_stainfo(pstapriv, psta_addr);
 577
 578        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:adapter->securitypriv.dot11AuthAlgrthm =%d\n", adapter->securitypriv.dot11AuthAlgrthm));
 579
 580        if (auth_alg == 2) {
 581                if ((psta != NULL) && (psta->ieee8021x_blocked)) {
 582                        __be16 be_tmp;
 583
 584                        /* blocked */
 585                        /* only accept EAPOL frame */
 586                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked == 1\n"));
 587
 588                        prtnframe = precv_frame;
 589
 590                        /* get ether_type */
 591                        ptr = ptr+pfhdr->attrib.hdrlen+pfhdr->attrib.iv_len+LLC_HEADER_SIZE;
 592                        memcpy(&be_tmp, ptr, 2);
 593                        ether_type = ntohs(be_tmp);
 594
 595                        if (ether_type == eapol_type)
 596                                prtnframe = precv_frame;
 597                        else {
 598                                /* free this frame */
 599                                rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
 600                                prtnframe = NULL;
 601                        }
 602                } else{
 603                        /* allowed */
 604                        /* check decryption status, and decrypt the frame if needed */
 605                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked == 0\n"));
 606                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:precv_frame->hdr.attrib.privacy =%x\n", precv_frame->u.hdr.attrib.privacy));
 607
 608                        if (pattrib->bdecrypted == 0)
 609                                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:prxstat->decrypted =%x\n", pattrib->bdecrypted));
 610
 611                        prtnframe = precv_frame;
 612                        /* check is the EAPOL frame or not (Rekey) */
 613                        /* if (ether_type == eapol_type) { */
 614                        /*      RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("########portctrl:ether_type == 0x888e\n")); */
 615                                /* check Rekey */
 616
 617                        /*      prtnframe =precv_frame; */
 618                        /*  */
 619                        /* else { */
 620                        /*      RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:ether_type = 0x%04x\n", ether_type)); */
 621                        /*  */
 622                }
 623        } else
 624                prtnframe = precv_frame;
 625
 626        return prtnframe;
 627}
 628
 629sint recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache);
 630sint recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache)
 631{
 632        sint tid = precv_frame->u.hdr.attrib.priority;
 633
 634        u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num&0xffff) << 4) |
 635                (precv_frame->u.hdr.attrib.frag_num & 0xf);
 636
 637        if (tid > 15) {
 638                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, (tid>15)! seq_ctrl = 0x%x, tid = 0x%x\n", seq_ctrl, tid));
 639
 640                return _FAIL;
 641        }
 642
 643        if (1) { /* if (bretry) */
 644                if (seq_ctrl == prxcache->tid_rxseq[tid]) {
 645                        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]));
 646
 647                        return _FAIL;
 648                }
 649        }
 650
 651        prxcache->tid_rxseq[tid] = seq_ctrl;
 652
 653        return _SUCCESS;
 654
 655}
 656
 657void process_pwrbit_data(struct adapter *padapter, union recv_frame *precv_frame);
 658void process_pwrbit_data(struct adapter *padapter, union recv_frame *precv_frame)
 659{
 660        unsigned char pwrbit;
 661        u8 *ptr = precv_frame->u.hdr.rx_data;
 662        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
 663        struct sta_priv *pstapriv = &padapter->stapriv;
 664        struct sta_info *psta = NULL;
 665
 666        psta = rtw_get_stainfo(pstapriv, pattrib->src);
 667
 668        pwrbit = GetPwrMgt(ptr);
 669
 670        if (psta) {
 671                if (pwrbit) {
 672                        if (!(psta->state & WIFI_SLEEP_STATE)) {
 673                                /* psta->state |= WIFI_SLEEP_STATE; */
 674                                /* pstapriv->sta_dz_bitmap |= BIT(psta->aid); */
 675
 676                                stop_sta_xmit(padapter, psta);
 677
 678                                /* DBG_871X("to sleep, sta_dz_bitmap =%x\n", pstapriv->sta_dz_bitmap); */
 679                        }
 680                } else{
 681                        if (psta->state & WIFI_SLEEP_STATE) {
 682                                /* psta->state ^= WIFI_SLEEP_STATE; */
 683                                /* pstapriv->sta_dz_bitmap &= ~BIT(psta->aid); */
 684
 685                                wakeup_sta_to_xmit(padapter, psta);
 686
 687                                /* DBG_871X("to wakeup, sta_dz_bitmap =%x\n", pstapriv->sta_dz_bitmap); */
 688                        }
 689                }
 690
 691        }
 692}
 693
 694void process_wmmps_data(struct adapter *padapter, union recv_frame *precv_frame);
 695void process_wmmps_data(struct adapter *padapter, union recv_frame *precv_frame)
 696{
 697        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
 698        struct sta_priv *pstapriv = &padapter->stapriv;
 699        struct sta_info *psta = NULL;
 700
 701        psta = rtw_get_stainfo(pstapriv, pattrib->src);
 702
 703        if (!psta)
 704                return;
 705
 706        if (!psta->qos_option)
 707                return;
 708
 709        if (!(psta->qos_info&0xf))
 710                return;
 711
 712        if (psta->state&WIFI_SLEEP_STATE) {
 713                u8 wmmps_ac = 0;
 714
 715                switch (pattrib->priority) {
 716                case 1:
 717                case 2:
 718                        wmmps_ac = psta->uapsd_bk&BIT(1);
 719                        break;
 720                case 4:
 721                case 5:
 722                        wmmps_ac = psta->uapsd_vi&BIT(1);
 723                        break;
 724                case 6:
 725                case 7:
 726                        wmmps_ac = psta->uapsd_vo&BIT(1);
 727                        break;
 728                case 0:
 729                case 3:
 730                default:
 731                        wmmps_ac = psta->uapsd_be&BIT(1);
 732                        break;
 733                }
 734
 735                if (wmmps_ac) {
 736                        if (psta->sleepq_ac_len > 0)
 737                                /* process received triggered frame */
 738                                xmit_delivery_enabled_frames(padapter, psta);
 739                        else
 740                                /* issue one qos null frame with More data bit = 0 and the EOSP bit set (= 1) */
 741                                issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
 742                }
 743        }
 744}
 745
 746void count_rx_stats(struct adapter *padapter, union recv_frame *prframe, struct sta_info *sta);
 747void count_rx_stats(struct adapter *padapter, union recv_frame *prframe, struct sta_info *sta)
 748{
 749        int     sz;
 750        struct sta_info         *psta = NULL;
 751        struct stainfo_stats    *pstats = NULL;
 752        struct rx_pkt_attrib    *pattrib = &prframe->u.hdr.attrib;
 753        struct recv_priv        *precvpriv = &padapter->recvpriv;
 754
 755        sz = get_recvframe_len(prframe);
 756        precvpriv->rx_bytes += sz;
 757
 758        padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
 759
 760        if ((!MacAddr_isBcst(pattrib->dst)) && (!IS_MCAST(pattrib->dst))) {
 761                padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
 762        }
 763
 764        if (sta)
 765                psta = sta;
 766        else
 767                psta = prframe->u.hdr.psta;
 768
 769        if (psta) {
 770                pstats = &psta->sta_stats;
 771
 772                pstats->rx_data_pkts++;
 773                pstats->rx_bytes += sz;
 774        }
 775
 776        traffic_check_for_leave_lps(padapter, false, 0);
 777}
 778
 779sint sta2sta_data_frame(
 780        struct adapter *adapter,
 781        union recv_frame *precv_frame,
 782        struct sta_info **psta
 783);
 784sint sta2sta_data_frame(
 785        struct adapter *adapter,
 786        union recv_frame *precv_frame,
 787        struct sta_info **psta
 788)
 789{
 790        u8 *ptr = precv_frame->u.hdr.rx_data;
 791        sint ret = _SUCCESS;
 792        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
 793        struct  sta_priv        *pstapriv = &adapter->stapriv;
 794        struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
 795        u8 *mybssid  = get_bssid(pmlmepriv);
 796        u8 *myhwaddr = myid(&adapter->eeprompriv);
 797        u8 *sta_addr = NULL;
 798        sint bmcast = IS_MCAST(pattrib->dst);
 799
 800        /* DBG_871X("[%s] %d, seqnum:%d\n", __func__, __LINE__, pattrib->seq_num); */
 801
 802        if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
 803                (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
 804
 805                /*  filter packets that SA is myself or multicast or broadcast */
 806                if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
 807                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA ==myself\n"));
 808                        ret = _FAIL;
 809                        goto exit;
 810                }
 811
 812                if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN))  && (!bmcast)) {
 813                        ret = _FAIL;
 814                        goto exit;
 815                }
 816
 817                if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 818                   !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 819                   (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
 820                        ret = _FAIL;
 821                        goto exit;
 822                }
 823
 824                sta_addr = pattrib->src;
 825
 826        } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
 827                /*  For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
 828                if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN)) {
 829                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("bssid != TA under STATION_MODE; drop pkt\n"));
 830                        ret = _FAIL;
 831                        goto exit;
 832                }
 833
 834                sta_addr = pattrib->bssid;
 835        } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
 836                if (bmcast) {
 837                        /*  For AP mode, if DA == MCAST, then BSSID should be also MCAST */
 838                        if (!IS_MCAST(pattrib->bssid)) {
 839                                        ret = _FAIL;
 840                                        goto exit;
 841                        }
 842                } else{ /*  not mc-frame */
 843                        /*  For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
 844                        if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
 845                                ret = _FAIL;
 846                                goto exit;
 847                        }
 848
 849                        sta_addr = pattrib->src;
 850                }
 851
 852        } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
 853                memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
 854                memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
 855                memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
 856                memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
 857                memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
 858
 859                sta_addr = mybssid;
 860        } else
 861                ret  = _FAIL;
 862
 863
 864
 865        if (bmcast)
 866                *psta = rtw_get_bcmc_stainfo(adapter);
 867        else
 868                *psta = rtw_get_stainfo(pstapriv, sta_addr); /*  get ap_info */
 869
 870        if (*psta == NULL) {
 871                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under sta2sta_data_frame ; drop pkt\n"));
 872                ret = _FAIL;
 873                goto exit;
 874        }
 875
 876exit:
 877        return ret;
 878}
 879
 880sint ap2sta_data_frame(
 881        struct adapter *adapter,
 882        union recv_frame *precv_frame,
 883        struct sta_info **psta);
 884sint ap2sta_data_frame(
 885        struct adapter *adapter,
 886        union recv_frame *precv_frame,
 887        struct sta_info **psta)
 888{
 889        u8 *ptr = precv_frame->u.hdr.rx_data;
 890        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
 891        sint ret = _SUCCESS;
 892        struct  sta_priv        *pstapriv = &adapter->stapriv;
 893        struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
 894        u8 *mybssid  = get_bssid(pmlmepriv);
 895        u8 *myhwaddr = myid(&adapter->eeprompriv);
 896        sint bmcast = IS_MCAST(pattrib->dst);
 897
 898        if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)
 899                && (check_fwstate(pmlmepriv, _FW_LINKED) == true
 900                        || check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true)
 901                ) {
 902
 903                /*  filter packets that SA is myself or multicast or broadcast */
 904                if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
 905                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA ==myself\n"));
 906                        #ifdef DBG_RX_DROP_FRAME
 907                        DBG_871X("DBG_RX_DROP_FRAME %s SA ="MAC_FMT", myhwaddr ="MAC_FMT"\n",
 908                                __func__, MAC_ARG(pattrib->src), MAC_ARG(myhwaddr));
 909                        #endif
 910                        ret = _FAIL;
 911                        goto exit;
 912                }
 913
 914                /*  da should be for me */
 915                if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
 916                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
 917                                (" ap2sta_data_frame:  compare DA fail; DA ="MAC_FMT"\n", MAC_ARG(pattrib->dst)));
 918                        #ifdef DBG_RX_DROP_FRAME
 919                        DBG_871X("DBG_RX_DROP_FRAME %s DA ="MAC_FMT"\n", __func__, MAC_ARG(pattrib->dst));
 920                        #endif
 921                        ret = _FAIL;
 922                        goto exit;
 923                }
 924
 925
 926                /*  check BSSID */
 927                if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 928                     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
 929                     (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
 930                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
 931                                (" ap2sta_data_frame:  compare BSSID fail ; BSSID ="MAC_FMT"\n", MAC_ARG(pattrib->bssid)));
 932                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("mybssid ="MAC_FMT"\n", MAC_ARG(mybssid)));
 933                        #ifdef DBG_RX_DROP_FRAME
 934                        DBG_871X("DBG_RX_DROP_FRAME %s BSSID ="MAC_FMT", mybssid ="MAC_FMT"\n",
 935                                __func__, MAC_ARG(pattrib->bssid), MAC_ARG(mybssid));
 936                        DBG_871X("this adapter = %d, buddy adapter = %d\n", adapter->adapter_type, adapter->pbuddystruct adapter->adapter_type);
 937                        #endif
 938
 939                        if (!bmcast) {
 940                                DBG_871X("issue_deauth to the nonassociated ap =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
 941                                issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
 942                        }
 943
 944                        ret = _FAIL;
 945                        goto exit;
 946                }
 947
 948                if (bmcast)
 949                        *psta = rtw_get_bcmc_stainfo(adapter);
 950                else
 951                        *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get ap_info */
 952
 953                if (*psta == NULL) {
 954                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ap2sta: can't get psta under STATION_MODE ; drop pkt\n"));
 955                        #ifdef DBG_RX_DROP_FRAME
 956                        DBG_871X("DBG_RX_DROP_FRAME %s can't get psta under STATION_MODE ; drop pkt\n", __func__);
 957                        #endif
 958                        ret = _FAIL;
 959                        goto exit;
 960                }
 961
 962                if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
 963                }
 964
 965                if (GetFrameSubType(ptr) & BIT(6)) {
 966                        /* No data, will not indicate to upper layer, temporily count it here */
 967                        count_rx_stats(adapter, precv_frame, *psta);
 968                        ret = RTW_RX_HANDLED;
 969                        goto exit;
 970                }
 971
 972        } else if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) &&
 973                     (check_fwstate(pmlmepriv, _FW_LINKED) == true)) {
 974                memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
 975                memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
 976                memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
 977                memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
 978                memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
 979
 980                /*  */
 981                memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
 982
 983
 984                *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
 985                if (*psta == NULL) {
 986                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under MP_MODE ; drop pkt\n"));
 987                        #ifdef DBG_RX_DROP_FRAME
 988                        DBG_871X("DBG_RX_DROP_FRAME %s can't get psta under WIFI_MP_STATE ; drop pkt\n", __func__);
 989                        #endif
 990                        ret = _FAIL;
 991                        goto exit;
 992                }
 993
 994
 995        } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
 996                /* Special case */
 997                ret = RTW_RX_HANDLED;
 998                goto exit;
 999        } else{
1000                if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
1001                        *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
1002                        if (*psta == NULL) {
1003
1004                                /* for AP multicast issue , modify by yiwei */
1005                                static unsigned long send_issue_deauth_time;
1006
1007                                /* DBG_871X("After send deauth , %u ms has elapsed.\n", jiffies_to_msecs(jiffies - send_issue_deauth_time)); */
1008
1009                                if (jiffies_to_msecs(jiffies - send_issue_deauth_time) > 10000 || send_issue_deauth_time == 0) {
1010                                        send_issue_deauth_time = jiffies;
1011
1012                                        DBG_871X("issue_deauth to the ap =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
1013
1014                                        issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1015                                }
1016                        }
1017                }
1018
1019                ret = _FAIL;
1020                #ifdef DBG_RX_DROP_FRAME
1021                DBG_871X("DBG_RX_DROP_FRAME %s fw_state:0x%x\n", __func__, get_fwstate(pmlmepriv));
1022                #endif
1023        }
1024
1025exit:
1026        return ret;
1027}
1028
1029sint sta2ap_data_frame(
1030        struct adapter *adapter,
1031        union recv_frame *precv_frame,
1032        struct sta_info **psta);
1033sint sta2ap_data_frame(
1034        struct adapter *adapter,
1035        union recv_frame *precv_frame,
1036        struct sta_info **psta)
1037{
1038        u8 *ptr = precv_frame->u.hdr.rx_data;
1039        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1040        struct  sta_priv        *pstapriv = &adapter->stapriv;
1041        struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
1042        unsigned char *mybssid  = get_bssid(pmlmepriv);
1043        sint ret = _SUCCESS;
1044
1045        if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
1046                /* For AP mode, RA =BSSID, TX =STA(SRC_ADDR), A3 =DST_ADDR */
1047                if (memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
1048                        ret = _FAIL;
1049                        goto exit;
1050                }
1051
1052                *psta = rtw_get_stainfo(pstapriv, pattrib->src);
1053                if (*psta == NULL) {
1054                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under AP_MODE; drop pkt\n"));
1055                        DBG_871X("issue_deauth to sta =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1056
1057                        issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1058
1059                        ret = RTW_RX_HANDLED;
1060                        goto exit;
1061                }
1062
1063                process_pwrbit_data(adapter, precv_frame);
1064
1065                if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
1066                        process_wmmps_data(adapter, precv_frame);
1067                }
1068
1069                if (GetFrameSubType(ptr) & BIT(6)) {
1070                        /* No data, will not indicate to upper layer, temporily count it here */
1071                        count_rx_stats(adapter, precv_frame, *psta);
1072                        ret = RTW_RX_HANDLED;
1073                        goto exit;
1074                }
1075        } else {
1076                u8 *myhwaddr = myid(&adapter->eeprompriv);
1077                if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
1078                        ret = RTW_RX_HANDLED;
1079                        goto exit;
1080                }
1081                DBG_871X("issue_deauth to sta =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1082                issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1083                ret = RTW_RX_HANDLED;
1084                goto exit;
1085        }
1086
1087exit:
1088        return ret;
1089}
1090
1091sint validate_recv_ctrl_frame(struct adapter *padapter, union recv_frame *precv_frame);
1092sint validate_recv_ctrl_frame(struct adapter *padapter, union recv_frame *precv_frame)
1093{
1094        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1095        struct sta_priv *pstapriv = &padapter->stapriv;
1096        u8 *pframe = precv_frame->u.hdr.rx_data;
1097        struct sta_info *psta = NULL;
1098        /* uint len = precv_frame->u.hdr.len; */
1099
1100        /* DBG_871X("+validate_recv_ctrl_frame\n"); */
1101
1102        if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
1103                return _FAIL;
1104
1105        /* receive the frames that ra(a1) is my address */
1106        if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
1107                return _FAIL;
1108
1109        psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1110        if (psta == NULL)
1111                return _FAIL;
1112
1113        /* for rx pkt statistics */
1114        psta->sta_stats.rx_ctrl_pkts++;
1115
1116        /* only handle ps-poll */
1117        if (GetFrameSubType(pframe) == WIFI_PSPOLL) {
1118                u16 aid;
1119                u8 wmmps_ac = 0;
1120
1121                aid = GetAid(pframe);
1122                if (psta->aid != aid)
1123                        return _FAIL;
1124
1125                switch (pattrib->priority) {
1126                case 1:
1127                case 2:
1128                        wmmps_ac = psta->uapsd_bk&BIT(0);
1129                        break;
1130                case 4:
1131                case 5:
1132                        wmmps_ac = psta->uapsd_vi&BIT(0);
1133                        break;
1134                case 6:
1135                case 7:
1136                        wmmps_ac = psta->uapsd_vo&BIT(0);
1137                        break;
1138                case 0:
1139                case 3:
1140                default:
1141                        wmmps_ac = psta->uapsd_be&BIT(0);
1142                        break;
1143                }
1144
1145                if (wmmps_ac)
1146                        return _FAIL;
1147
1148                if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1149                        DBG_871X("%s alive check-rx ps-poll\n", __func__);
1150                        psta->expire_to = pstapriv->expire_to;
1151                        psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1152                }
1153
1154                if ((psta->state&WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap&BIT(psta->aid))) {
1155                        struct list_head        *xmitframe_plist, *xmitframe_phead;
1156                        struct xmit_frame *pxmitframe = NULL;
1157                        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1158
1159                        /* spin_lock_bh(&psta->sleep_q.lock); */
1160                        spin_lock_bh(&pxmitpriv->lock);
1161
1162                        xmitframe_phead = get_list_head(&psta->sleep_q);
1163                        xmitframe_plist = get_next(xmitframe_phead);
1164
1165                        if (xmitframe_phead != xmitframe_plist) {
1166                                pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
1167
1168                                xmitframe_plist = get_next(xmitframe_plist);
1169
1170                                list_del_init(&pxmitframe->list);
1171
1172                                psta->sleepq_len--;
1173
1174                                if (psta->sleepq_len > 0)
1175                                        pxmitframe->attrib.mdata = 1;
1176                                else
1177                                        pxmitframe->attrib.mdata = 0;
1178
1179                                pxmitframe->attrib.triggered = 1;
1180
1181                                /* DBG_871X("handling ps-poll, q_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
1182
1183                                rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
1184
1185                                if (psta->sleepq_len == 0) {
1186                                        pstapriv->tim_bitmap &= ~BIT(psta->aid);
1187
1188                                        /* DBG_871X("after handling ps-poll, tim =%x\n", pstapriv->tim_bitmap); */
1189
1190                                        /* upate BCN for TIM IE */
1191                                        /* update_BCNTIM(padapter); */
1192                                        update_beacon(padapter, _TIM_IE_, NULL, true);
1193                                }
1194
1195                                /* spin_unlock_bh(&psta->sleep_q.lock); */
1196                                spin_unlock_bh(&pxmitpriv->lock);
1197
1198                        } else{
1199                                /* spin_unlock_bh(&psta->sleep_q.lock); */
1200                                spin_unlock_bh(&pxmitpriv->lock);
1201
1202                                /* DBG_871X("no buffered packets to xmit\n"); */
1203                                if (pstapriv->tim_bitmap&BIT(psta->aid)) {
1204                                        if (psta->sleepq_len == 0) {
1205                                                DBG_871X("no buffered packets to xmit\n");
1206
1207                                                /* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
1208                                                issue_nulldata_in_interrupt(padapter, psta->hwaddr);
1209                                        } else{
1210                                                DBG_871X("error!psta->sleepq_len =%d\n", psta->sleepq_len);
1211                                                psta->sleepq_len = 0;
1212                                        }
1213
1214                                        pstapriv->tim_bitmap &= ~BIT(psta->aid);
1215
1216                                        /* upate BCN for TIM IE */
1217                                        /* update_BCNTIM(padapter); */
1218                                        update_beacon(padapter, _TIM_IE_, NULL, true);
1219                                }
1220                        }
1221                }
1222        }
1223
1224        return _FAIL;
1225
1226}
1227
1228union recv_frame *recvframe_chk_defrag(struct adapter *padapter, union recv_frame *precv_frame);
1229sint validate_recv_mgnt_frame(struct adapter *padapter, union recv_frame *precv_frame);
1230sint validate_recv_mgnt_frame(struct adapter *padapter, union recv_frame *precv_frame)
1231{
1232        /* struct mlme_priv *pmlmepriv = &adapter->mlmepriv; */
1233
1234        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+validate_recv_mgnt_frame\n"));
1235
1236        precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1237        if (precv_frame == NULL) {
1238                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("%s: fragment packet\n", __func__));
1239                return _SUCCESS;
1240        }
1241
1242        {
1243                /* for rx pkt statistics */
1244                struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(precv_frame->u.hdr.rx_data));
1245                if (psta) {
1246                        psta->sta_stats.rx_mgnt_pkts++;
1247                        if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_BEACON)
1248                                psta->sta_stats.rx_beacon_pkts++;
1249                        else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBEREQ)
1250                                psta->sta_stats.rx_probereq_pkts++;
1251                        else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBERSP) {
1252                                if (!memcmp(padapter->eeprompriv.mac_addr, GetAddr1Ptr(precv_frame->u.hdr.rx_data), ETH_ALEN))
1253                                        psta->sta_stats.rx_probersp_pkts++;
1254                                else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data))
1255                                        || is_multicast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data)))
1256                                        psta->sta_stats.rx_probersp_bm_pkts++;
1257                                else
1258                                        psta->sta_stats.rx_probersp_uo_pkts++;
1259                        }
1260                }
1261        }
1262
1263        mgt_dispatcher(padapter, precv_frame);
1264
1265        return _SUCCESS;
1266
1267}
1268
1269sint validate_recv_data_frame(struct adapter *adapter, union recv_frame *precv_frame);
1270sint validate_recv_data_frame(struct adapter *adapter, union recv_frame *precv_frame)
1271{
1272        u8 bretry;
1273        u8 *psa, *pda, *pbssid;
1274        struct sta_info *psta = NULL;
1275        u8 *ptr = precv_frame->u.hdr.rx_data;
1276        struct rx_pkt_attrib    *pattrib = &precv_frame->u.hdr.attrib;
1277        struct security_priv *psecuritypriv = &adapter->securitypriv;
1278        sint ret = _SUCCESS;
1279
1280        bretry = GetRetry(ptr);
1281        pda = get_da(ptr);
1282        psa = get_sa(ptr);
1283        pbssid = get_hdr_bssid(ptr);
1284
1285        if (pbssid == NULL) {
1286                #ifdef DBG_RX_DROP_FRAME
1287                DBG_871X("DBG_RX_DROP_FRAME %s pbssid == NULL\n", __func__);
1288                #endif
1289                ret = _FAIL;
1290                goto exit;
1291        }
1292
1293        memcpy(pattrib->dst, pda, ETH_ALEN);
1294        memcpy(pattrib->src, psa, ETH_ALEN);
1295
1296        memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1297
1298        switch (pattrib->to_fr_ds) {
1299        case 0:
1300                memcpy(pattrib->ra, pda, ETH_ALEN);
1301                memcpy(pattrib->ta, psa, ETH_ALEN);
1302                ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1303                break;
1304
1305        case 1:
1306                memcpy(pattrib->ra, pda, ETH_ALEN);
1307                memcpy(pattrib->ta, pbssid, ETH_ALEN);
1308                ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1309                break;
1310
1311        case 2:
1312                memcpy(pattrib->ra, pbssid, ETH_ALEN);
1313                memcpy(pattrib->ta, psa, ETH_ALEN);
1314                ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1315                break;
1316
1317        case 3:
1318                memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1319                memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1320                ret = _FAIL;
1321                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" case 3\n"));
1322                break;
1323
1324        default:
1325                ret = _FAIL;
1326                break;
1327
1328        }
1329
1330        if (ret == _FAIL) {
1331                #ifdef DBG_RX_DROP_FRAME
1332                DBG_871X("DBG_RX_DROP_FRAME %s case:%d, res:%d\n", __func__, pattrib->to_fr_ds, ret);
1333                #endif
1334                goto exit;
1335        } else if (ret == RTW_RX_HANDLED) {
1336                goto exit;
1337        }
1338
1339
1340        if (psta == NULL) {
1341                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" after to_fr_ds_chk; psta == NULL\n"));
1342                #ifdef DBG_RX_DROP_FRAME
1343                DBG_871X("DBG_RX_DROP_FRAME %s psta == NULL\n", __func__);
1344                #endif
1345                ret = _FAIL;
1346                goto exit;
1347        }
1348
1349        /* psta->rssi = prxcmd->rssi; */
1350        /* psta->signal_quality = prxcmd->sq; */
1351        precv_frame->u.hdr.psta = psta;
1352
1353
1354        pattrib->amsdu = 0;
1355        pattrib->ack_policy = 0;
1356        /* parsing QC field */
1357        if (pattrib->qos == 1) {
1358                pattrib->priority = GetPriority((ptr + 24));
1359                pattrib->ack_policy = GetAckpolicy((ptr + 24));
1360                pattrib->amsdu = GetAMsdu((ptr + 24));
1361                pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
1362
1363                if (pattrib->priority != 0 && pattrib->priority != 3)
1364                        adapter->recvpriv.bIsAnyNonBEPkts = true;
1365
1366        } else{
1367                pattrib->priority = 0;
1368                pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 30 : 24;
1369        }
1370
1371
1372        if (pattrib->order)/* HT-CTRL 11n */
1373                pattrib->hdrlen += 4;
1374
1375        precv_frame->u.hdr.preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1376
1377        /*  decache, drop duplicate recv packets */
1378        if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL) {
1379                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decache : drop pkt\n"));
1380                #ifdef DBG_RX_DROP_FRAME
1381                DBG_871X("DBG_RX_DROP_FRAME %s recv_decache return _FAIL\n", __func__);
1382                #endif
1383                ret = _FAIL;
1384                goto exit;
1385        }
1386
1387        if (pattrib->privacy) {
1388
1389                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("validate_recv_data_frame:pattrib->privacy =%x\n", pattrib->privacy));
1390                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)));
1391
1392                GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
1393
1394                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n pattrib->encrypt =%d\n", pattrib->encrypt));
1395
1396                SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1397        } else{
1398                pattrib->encrypt = 0;
1399                pattrib->iv_len = pattrib->icv_len = 0;
1400        }
1401
1402exit:
1403        return ret;
1404}
1405
1406static sint validate_80211w_mgmt(struct adapter *adapter, union recv_frame *precv_frame)
1407{
1408        struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1409        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1410        u8 *ptr = precv_frame->u.hdr.rx_data;
1411        u8 type;
1412        u8 subtype;
1413
1414        type =  GetFrameType(ptr);
1415        subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1416
1417        /* only support station mode */
1418        if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED)
1419                && adapter->securitypriv.binstallBIPkey == true) {
1420                /* unicast management frame decrypt */
1421                if (pattrib->privacy && !(IS_MCAST(GetAddr1Ptr(ptr))) &&
1422                        (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC || subtype == WIFI_ACTION)) {
1423                        u8 *ppp, *mgmt_DATA;
1424                        u32 data_len = 0;
1425                        ppp = GetAddr2Ptr(ptr);
1426
1427                        pattrib->bdecrypted = 0;
1428                        pattrib->encrypt = _AES_;
1429                        pattrib->hdrlen = sizeof(struct ieee80211_hdr_3addr);
1430                        /* set iv and icv length */
1431                        SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1432                        memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1433                        memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1434                        /* actual management data frame body */
1435                        data_len = pattrib->pkt_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
1436                        mgmt_DATA = rtw_zmalloc(data_len);
1437                        if (mgmt_DATA == NULL) {
1438                                DBG_871X("%s mgmt allocate fail  !!!!!!!!!\n", __func__);
1439                                goto validate_80211w_fail;
1440                        }
1441                        precv_frame = decryptor(adapter, precv_frame);
1442                        /* save actual management data frame body */
1443                        memcpy(mgmt_DATA, ptr+pattrib->hdrlen+pattrib->iv_len, data_len);
1444                        /* overwrite the iv field */
1445                        memcpy(ptr+pattrib->hdrlen, mgmt_DATA, data_len);
1446                        /* remove the iv and icv length */
1447                        pattrib->pkt_len = pattrib->pkt_len - pattrib->iv_len - pattrib->icv_len;
1448                        kfree(mgmt_DATA);
1449                        if (!precv_frame) {
1450                                DBG_871X("%s mgmt descrypt fail  !!!!!!!!!\n", __func__);
1451                                goto validate_80211w_fail;
1452                        }
1453                } else if (IS_MCAST(GetAddr1Ptr(ptr)) &&
1454                        (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC)) {
1455                        sint BIP_ret = _SUCCESS;
1456                        /* verify BIP MME IE of broadcast/multicast de-auth/disassoc packet */
1457                        BIP_ret = rtw_BIP_verify(adapter, (u8 *)precv_frame);
1458                        if (BIP_ret == _FAIL) {
1459                                /* DBG_871X("802.11w BIP verify fail\n"); */
1460                                goto validate_80211w_fail;
1461                        } else if (BIP_ret == RTW_RX_HANDLED) {
1462                                /* DBG_871X("802.11w recv none protected packet\n"); */
1463                                /* issue sa query request */
1464                                issue_action_SA_Query(adapter, NULL, 0, 0);
1465                                goto validate_80211w_fail;
1466                        }
1467                } else { /* 802.11w protect */
1468                        if (subtype == WIFI_ACTION) {
1469                                /* according 802.11-2012 standard, these five types are not robust types */
1470                                if (ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_PUBLIC          &&
1471                                        ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_HT              &&
1472                                        ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_UNPROTECTED_WNM &&
1473                                        ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_SELF_PROTECTED  &&
1474                                        ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_P2P) {
1475                                        DBG_871X("action frame category =%d should robust\n", ptr[WLAN_HDR_A3_LEN]);
1476                                        goto validate_80211w_fail;
1477                                }
1478                        } else if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC) {
1479                                DBG_871X("802.11w recv none protected packet\n");
1480                                /* issue sa query request */
1481                                issue_action_SA_Query(adapter, NULL, 0, 0);
1482                                goto validate_80211w_fail;
1483                        }
1484                }
1485        }
1486        return _SUCCESS;
1487
1488validate_80211w_fail:
1489        return _FAIL;
1490
1491}
1492
1493static inline void dump_rx_packet(u8 *ptr)
1494{
1495        int i;
1496
1497        DBG_871X("#############################\n");
1498        for (i = 0; i < 64; i = i+8)
1499                DBG_871X("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1500                *(ptr+i+1), *(ptr+i+2), *(ptr+i+3), *(ptr+i+4), *(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1501        DBG_871X("#############################\n");
1502}
1503
1504sint validate_recv_frame(struct adapter *adapter, union recv_frame *precv_frame);
1505sint validate_recv_frame(struct adapter *adapter, union recv_frame *precv_frame)
1506{
1507        /* shall check frame subtype, to / from ds, da, bssid */
1508
1509        /* then call check if rx seq/frag. duplicated. */
1510
1511        u8 type;
1512        u8 subtype;
1513        sint retval = _SUCCESS;
1514        u8 bDumpRxPkt;
1515
1516        struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1517
1518        u8 *ptr = precv_frame->u.hdr.rx_data;
1519        u8  ver = (unsigned char) (*ptr)&0x3;
1520
1521        /* add version chk */
1522        if (ver != 0) {
1523                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! (ver!= 0)\n"));
1524                retval = _FAIL;
1525                DBG_COUNTER(adapter->rx_logs.core_rx_pre_ver_err);
1526                goto exit;
1527        }
1528
1529        type =  GetFrameType(ptr);
1530        subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1531
1532        pattrib->to_fr_ds = get_tofr_ds(ptr);
1533
1534        pattrib->frag_num = GetFragNum(ptr);
1535        pattrib->seq_num = GetSequence(ptr);
1536
1537        pattrib->pw_save = GetPwrMgt(ptr);
1538        pattrib->mfrag = GetMFrag(ptr);
1539        pattrib->mdata = GetMData(ptr);
1540        pattrib->privacy = GetPrivacy(ptr);
1541        pattrib->order = GetOrder(ptr);
1542        rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1543        if (bDumpRxPkt == 1) /* dump all rx packets */
1544                dump_rx_packet(ptr);
1545        else if ((bDumpRxPkt == 2) && (type == WIFI_MGT_TYPE))
1546                dump_rx_packet(ptr);
1547        else if ((bDumpRxPkt == 3) && (type == WIFI_DATA_TYPE))
1548                dump_rx_packet(ptr);
1549
1550        switch (type) {
1551        case WIFI_MGT_TYPE: /* mgnt */
1552                DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt);
1553                if (validate_80211w_mgmt(adapter, precv_frame) == _FAIL) {
1554                        retval = _FAIL;
1555                        DBG_COUNTER(padapter->rx_logs.core_rx_pre_mgmt_err_80211w);
1556                        break;
1557                }
1558
1559                retval = validate_recv_mgnt_frame(adapter, precv_frame);
1560                if (retval == _FAIL) {
1561                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_mgnt_frame fail\n"));
1562                        DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt_err);
1563                }
1564                retval = _FAIL; /*  only data frame return _SUCCESS */
1565                break;
1566        case WIFI_CTRL_TYPE: /* ctrl */
1567                DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl);
1568                retval = validate_recv_ctrl_frame(adapter, precv_frame);
1569                if (retval == _FAIL) {
1570                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_ctrl_frame fail\n"));
1571                        DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl_err);
1572                }
1573                retval = _FAIL; /*  only data frame return _SUCCESS */
1574                break;
1575        case WIFI_DATA_TYPE: /* data */
1576                DBG_COUNTER(adapter->rx_logs.core_rx_pre_data);
1577
1578                pattrib->qos = (subtype & BIT(7)) ? 1:0;
1579                retval = validate_recv_data_frame(adapter, precv_frame);
1580                if (retval == _FAIL) {
1581                        struct recv_priv *precvpriv = &adapter->recvpriv;
1582                        /* RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail\n")); */
1583                        precvpriv->rx_drop++;
1584                        DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_err);
1585                } else if (retval == _SUCCESS) {
1586#ifdef DBG_RX_DUMP_EAP
1587                        u8 bDumpRxPkt;
1588                        u16 eth_type;
1589
1590                        /*  dump eapol */
1591                        rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1592                        /*  get ether_type */
1593                        memcpy(&eth_type, ptr + pattrib->hdrlen + pattrib->iv_len + LLC_HEADER_SIZE, 2);
1594                        eth_type = ntohs((unsigned short) eth_type);
1595                        if ((bDumpRxPkt == 4) && (eth_type == 0x888e))
1596                                dump_rx_packet(ptr);
1597#endif
1598                } else
1599                        DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_handled);
1600                break;
1601        default:
1602                DBG_COUNTER(adapter->rx_logs.core_rx_pre_unknown);
1603                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! type = 0x%x\n", type));
1604                #ifdef DBG_RX_DROP_FRAME
1605                DBG_871X("DBG_RX_DROP_FRAME validate_recv_data_frame fail! type = 0x%x\n", type);
1606                #endif
1607                retval = _FAIL;
1608                break;
1609        }
1610
1611exit:
1612        return retval;
1613}
1614
1615
1616/* remove the wlanhdr and add the eth_hdr */
1617sint wlanhdr_to_ethhdr(union recv_frame *precvframe);
1618sint wlanhdr_to_ethhdr(union recv_frame *precvframe)
1619{
1620        sint    rmv_len;
1621        u16 eth_type, len;
1622        u8 bsnaphdr;
1623        u8 *psnap_type;
1624        struct ieee80211_snap_hdr       *psnap;
1625        __be16 be_tmp;
1626        sint ret = _SUCCESS;
1627        struct adapter                  *adapter = precvframe->u.hdr.adapter;
1628        struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1629        u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
1630        struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
1631
1632        if (pattrib->encrypt) {
1633                recvframe_pull_tail(precvframe, pattrib->icv_len);
1634        }
1635
1636        psnap = (struct ieee80211_snap_hdr      *)(ptr+pattrib->hdrlen + pattrib->iv_len);
1637        psnap_type = ptr+pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
1638        /* convert hdr + possible LLC headers into Ethernet header */
1639        /* eth_type = (psnap_type[0] << 8) | psnap_type[1]; */
1640        if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
1641                (memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2)) &&
1642                (memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
1643                /* eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) || */
1644                 !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
1645                /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1646                bsnaphdr = true;
1647        } else
1648                /* Leave Ethernet header part of hdr and full payload */
1649                bsnaphdr = false;
1650
1651        rmv_len = pattrib->hdrlen + pattrib->iv_len + (bsnaphdr?SNAP_SIZE:0);
1652        len = precvframe->u.hdr.len - rmv_len;
1653
1654        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n ===pattrib->hdrlen: %x,  pattrib->iv_len:%x ===\n\n", pattrib->hdrlen,  pattrib->iv_len));
1655
1656        memcpy(&be_tmp, ptr+rmv_len, 2);
1657        eth_type = ntohs(be_tmp); /* pattrib->ether_type */
1658        pattrib->eth_type = eth_type;
1659
1660#ifdef CONFIG_AUTO_AP_MODE
1661        if (0x8899 == pattrib->eth_type) {
1662                struct sta_info *psta = precvframe->u.hdr.psta;
1663
1664                DBG_871X("wlan rx: got eth_type = 0x%x\n", pattrib->eth_type);
1665
1666                if (psta && psta->isrc && psta->pid > 0) {
1667                        u16 rx_pid;
1668
1669                        rx_pid = *(u16 *)(ptr+rmv_len+2);
1670
1671                        DBG_871X("wlan rx(pid = 0x%x): sta("MAC_FMT") pid = 0x%x\n",
1672                                rx_pid, MAC_ARG(psta->hwaddr), psta->pid);
1673
1674                        if (rx_pid == psta->pid) {
1675                                int i;
1676                                u16 len = *(u16 *)(ptr+rmv_len+4);
1677                                /* u16 ctrl_type = *(u16*)(ptr+rmv_len+6); */
1678
1679                                /* DBG_871X("RC: len = 0x%x, ctrl_type = 0x%x\n", len, ctrl_type); */
1680                                DBG_871X("RC: len = 0x%x\n", len);
1681
1682                                for (i = 0; i < len ; i++)
1683                                        DBG_871X("0x%x\n", *(ptr+rmv_len+6+i));
1684                                        /* DBG_871X("0x%x\n", *(ptr+rmv_len+8+i)); */
1685
1686                                DBG_871X("RC-end\n");
1687                        }
1688                }
1689        }
1690#endif /* CONFIG_AUTO_AP_MODE */
1691
1692        if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)) {
1693                ptr += rmv_len;
1694                *ptr = 0x87;
1695                *(ptr+1) = 0x12;
1696
1697                eth_type = 0x8712;
1698                /*  append rx status for mp test packets */
1699                ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr)+2)-24);
1700                memcpy(ptr, get_rxmem(precvframe), 24);
1701                ptr += 24;
1702        } else
1703                ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr) + (bsnaphdr?2:0)));
1704
1705        memcpy(ptr, pattrib->dst, ETH_ALEN);
1706        memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
1707
1708        if (!bsnaphdr) {
1709                be_tmp = htons(len);
1710                memcpy(ptr+12, &be_tmp, 2);
1711        }
1712
1713        return ret;
1714}
1715
1716/* perform defrag */
1717static union recv_frame *recvframe_defrag(struct adapter *adapter,
1718                                          struct __queue *defrag_q)
1719{
1720        struct list_head         *plist, *phead;
1721        u8 *data, wlanhdr_offset;
1722        u8 curfragnum;
1723        struct recv_frame_hdr *pfhdr, *pnfhdr;
1724        union recv_frame *prframe, *pnextrframe;
1725        struct __queue  *pfree_recv_queue;
1726
1727        curfragnum = 0;
1728        pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
1729
1730        phead = get_list_head(defrag_q);
1731        plist = get_next(phead);
1732        prframe = (union recv_frame *)plist;
1733        pfhdr = &prframe->u.hdr;
1734        list_del_init(&(prframe->u.list));
1735
1736        if (curfragnum != pfhdr->attrib.frag_num) {
1737                /* the first fragment number must be 0 */
1738                /* free the whole queue */
1739                rtw_free_recvframe(prframe, pfree_recv_queue);
1740                rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1741
1742                return NULL;
1743        }
1744
1745        curfragnum++;
1746
1747        plist = get_list_head(defrag_q);
1748
1749        plist = get_next(plist);
1750
1751        data = get_recvframe_data(prframe);
1752
1753        while (phead != plist) {
1754                pnextrframe = (union recv_frame *)plist;
1755                pnfhdr = &pnextrframe->u.hdr;
1756
1757
1758                /* check the fragment sequence  (2nd ~n fragment frame) */
1759
1760                if (curfragnum != pnfhdr->attrib.frag_num) {
1761                        /* the fragment number must be increasing  (after decache) */
1762                        /* release the defrag_q & prframe */
1763                        rtw_free_recvframe(prframe, pfree_recv_queue);
1764                        rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1765                        return NULL;
1766                }
1767
1768                curfragnum++;
1769
1770                /* copy the 2nd~n fragment frame's payload to the first fragment */
1771                /* get the 2nd~last fragment frame's payload */
1772
1773                wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
1774
1775                recvframe_pull(pnextrframe, wlanhdr_offset);
1776
1777                /* append  to first fragment frame's tail (if privacy frame, pull the ICV) */
1778                recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
1779
1780                /* memcpy */
1781                memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
1782
1783                recvframe_put(prframe, pnfhdr->len);
1784
1785                pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
1786                plist = get_next(plist);
1787
1788        }
1789
1790        /* free the defrag_q queue and return the prframe */
1791        rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1792
1793        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Performance defrag!!!!!\n"));
1794
1795        return prframe;
1796}
1797
1798/* check if need to defrag, if needed queue the frame to defrag_q */
1799union recv_frame *recvframe_chk_defrag(struct adapter *padapter, union recv_frame *precv_frame)
1800{
1801        u8 ismfrag;
1802        u8 fragnum;
1803        u8 *psta_addr;
1804        struct recv_frame_hdr *pfhdr;
1805        struct sta_info *psta;
1806        struct sta_priv *pstapriv;
1807        struct list_head *phead;
1808        union recv_frame *prtnframe = NULL;
1809        struct __queue *pfree_recv_queue, *pdefrag_q;
1810
1811        pstapriv = &padapter->stapriv;
1812
1813        pfhdr = &precv_frame->u.hdr;
1814
1815        pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1816
1817        /* need to define struct of wlan header frame ctrl */
1818        ismfrag = pfhdr->attrib.mfrag;
1819        fragnum = pfhdr->attrib.frag_num;
1820
1821        psta_addr = pfhdr->attrib.ta;
1822        psta = rtw_get_stainfo(pstapriv, psta_addr);
1823        if (psta == NULL) {
1824                u8 type = GetFrameType(pfhdr->rx_data);
1825                if (type != WIFI_DATA_TYPE) {
1826                        psta = rtw_get_bcmc_stainfo(padapter);
1827                        pdefrag_q = &psta->sta_recvpriv.defrag_q;
1828                } else
1829                        pdefrag_q = NULL;
1830        } else
1831                pdefrag_q = &psta->sta_recvpriv.defrag_q;
1832
1833        if ((ismfrag == 0) && (fragnum == 0))
1834                prtnframe = precv_frame;/* isn't a fragment frame */
1835
1836        if (ismfrag == 1) {
1837                /* 0~(n-1) fragment frame */
1838                /* enqueue to defraf_g */
1839                if (pdefrag_q != NULL) {
1840                        if (fragnum == 0)
1841                                /* the first fragment */
1842                                if (!list_empty(&pdefrag_q->queue))
1843                                        /* free current defrag_q */
1844                                        rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
1845
1846
1847                        /* Then enqueue the 0~(n-1) fragment into the defrag_q */
1848
1849                        /* spin_lock(&pdefrag_q->lock); */
1850                        phead = get_list_head(pdefrag_q);
1851                        list_add_tail(&pfhdr->list, phead);
1852                        /* spin_unlock(&pdefrag_q->lock); */
1853
1854                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Enqueuq: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1855
1856                        prtnframe = NULL;
1857
1858                } else{
1859                        /* can't find this ta's defrag_queue, so free this recv_frame */
1860                        rtw_free_recvframe(precv_frame, pfree_recv_queue);
1861                        prtnframe = NULL;
1862                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q == NULL: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1863                }
1864
1865        }
1866
1867        if ((ismfrag == 0) && (fragnum != 0)) {
1868                /* the last fragment frame */
1869                /* enqueue the last fragment */
1870                if (pdefrag_q != NULL) {
1871                        /* spin_lock(&pdefrag_q->lock); */
1872                        phead = get_list_head(pdefrag_q);
1873                        list_add_tail(&pfhdr->list, phead);
1874                        /* spin_unlock(&pdefrag_q->lock); */
1875
1876                        /* call recvframe_defrag to defrag */
1877                        RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("defrag: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1878                        precv_frame = recvframe_defrag(padapter, pdefrag_q);
1879                        prtnframe = precv_frame;
1880
1881                } else{
1882                        /* can't find this ta's defrag_queue, so free this recv_frame */
1883                        rtw_free_recvframe(precv_frame, pfree_recv_queue);
1884                        prtnframe = NULL;
1885                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q == NULL: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1886                }
1887
1888        }
1889
1890
1891        if ((prtnframe != NULL) && (prtnframe->u.hdr.attrib.privacy)) {
1892                /* after defrag we must check tkip mic code */
1893                if (recvframe_chkmic(padapter,  prtnframe) == _FAIL) {
1894                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic(padapter,  prtnframe) == _FAIL\n"));
1895                        rtw_free_recvframe(prtnframe, pfree_recv_queue);
1896                        prtnframe = NULL;
1897                }
1898        }
1899        return prtnframe;
1900}
1901
1902static int amsdu_to_msdu(struct adapter *padapter, union recv_frame *prframe)
1903{
1904        int     a_len, padding_len;
1905        u16 nSubframe_Length;
1906        u8 nr_subframes, i;
1907        u8 *pdata;
1908        _pkt *sub_pkt, *subframes[MAX_SUBFRAME_COUNT];
1909        struct recv_priv *precvpriv = &padapter->recvpriv;
1910        struct __queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
1911        int     ret = _SUCCESS;
1912
1913        nr_subframes = 0;
1914
1915        recvframe_pull(prframe, prframe->u.hdr.attrib.hdrlen);
1916
1917        if (prframe->u.hdr.attrib.iv_len > 0)
1918                recvframe_pull(prframe, prframe->u.hdr.attrib.iv_len);
1919
1920        a_len = prframe->u.hdr.len;
1921
1922        pdata = prframe->u.hdr.rx_data;
1923
1924        while (a_len > ETH_HLEN) {
1925
1926                /* Offset 12 denote 2 mac address */
1927                nSubframe_Length = RTW_GET_BE16(pdata + 12);
1928
1929                if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1930                        DBG_871X("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
1931                        break;
1932                }
1933
1934                sub_pkt = rtw_os_alloc_msdu_pkt(prframe, nSubframe_Length, pdata);
1935                if (sub_pkt == NULL) {
1936                        DBG_871X("%s(): allocate sub packet fail !!!\n", __func__);
1937                        break;
1938                }
1939
1940                /* move the data point to data content */
1941                pdata += ETH_HLEN;
1942                a_len -= ETH_HLEN;
1943
1944                subframes[nr_subframes++] = sub_pkt;
1945
1946                if (nr_subframes >= MAX_SUBFRAME_COUNT) {
1947                        DBG_871X("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1948                        break;
1949                }
1950
1951                pdata += nSubframe_Length;
1952                a_len -= nSubframe_Length;
1953                if (a_len != 0) {
1954                        padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4-1));
1955                        if (padding_len == 4) {
1956                                padding_len = 0;
1957                        }
1958
1959                        if (a_len < padding_len) {
1960                                DBG_871X("ParseSubframe(): a_len < padding_len !\n");
1961                                break;
1962                        }
1963                        pdata += padding_len;
1964                        a_len -= padding_len;
1965                }
1966        }
1967
1968        for (i = 0; i < nr_subframes; i++) {
1969                sub_pkt = subframes[i];
1970
1971                /* Indicat the packets to upper layer */
1972                if (sub_pkt) {
1973                        rtw_os_recv_indicate_pkt(padapter, sub_pkt, &prframe->u.hdr.attrib);
1974                }
1975        }
1976
1977        prframe->u.hdr.len = 0;
1978        rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
1979
1980        return ret;
1981}
1982
1983int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num);
1984int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
1985{
1986        struct adapter *padapter = preorder_ctrl->padapter;
1987        struct dvobj_priv *psdpriv = padapter->dvobj;
1988        struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1989        u8 wsize = preorder_ctrl->wsize_b;
1990        u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/*  4096; */
1991
1992        /*  Rx Reorder initialize condition. */
1993        if (preorder_ctrl->indicate_seq == 0xFFFF) {
1994                preorder_ctrl->indicate_seq = seq_num;
1995                #ifdef DBG_RX_SEQ
1996                DBG_871X("DBG_RX_SEQ %s:%d init IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
1997                        preorder_ctrl->indicate_seq, seq_num);
1998                #endif
1999
2000                /* DbgPrint("check_indicate_seq, 1st->indicate_seq =%d\n", precvpriv->indicate_seq); */
2001        }
2002
2003        /* DbgPrint("enter->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2004
2005        /*  Drop out the packet which SeqNum is smaller than WinStart */
2006        if (SN_LESS(seq_num, preorder_ctrl->indicate_seq)) {
2007                /* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2008                /* DbgPrint("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2009
2010                #ifdef DBG_RX_DROP_FRAME
2011                DBG_871X("%s IndicateSeq: %d > NewSeq: %d\n", __func__,
2012                        preorder_ctrl->indicate_seq, seq_num);
2013                #endif
2014
2015
2016                return false;
2017        }
2018
2019        /*  */
2020        /*  Sliding window manipulation. Conditions includes: */
2021        /*  1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
2022        /*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
2023        /*  */
2024        if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
2025                preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2026
2027                #ifdef DBG_RX_SEQ
2028                DBG_871X("DBG_RX_SEQ %s:%d SN_EQUAL IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2029                        preorder_ctrl->indicate_seq, seq_num);
2030                #endif
2031        } else if (SN_LESS(wend, seq_num)) {
2032                /* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2033                /* DbgPrint("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2034
2035                /*  boundary situation, when seq_num cross 0xFFF */
2036                if (seq_num >= (wsize - 1))
2037                        preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
2038                else
2039                        preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
2040                pdbgpriv->dbg_rx_ampdu_window_shift_cnt++;
2041                #ifdef DBG_RX_SEQ
2042                DBG_871X("DBG_RX_SEQ %s:%d SN_LESS(wend, seq_num) IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2043                        preorder_ctrl->indicate_seq, seq_num);
2044                #endif
2045        }
2046
2047        /* DbgPrint("exit->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2048
2049        return true;
2050}
2051
2052int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe);
2053int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe)
2054{
2055        struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2056        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2057        struct list_head        *phead, *plist;
2058        union recv_frame *pnextrframe;
2059        struct rx_pkt_attrib *pnextattrib;
2060
2061        /* DbgPrint("+enqueue_reorder_recvframe()\n"); */
2062
2063        /* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2064        /* spin_lock(&ppending_recvframe_queue->lock); */
2065
2066
2067        phead = get_list_head(ppending_recvframe_queue);
2068        plist = get_next(phead);
2069
2070        while (phead != plist) {
2071                pnextrframe = (union recv_frame *)plist;
2072                pnextattrib = &pnextrframe->u.hdr.attrib;
2073
2074                if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
2075                        plist = get_next(plist);
2076                else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
2077                        /* Duplicate entry is found!! Do not insert current entry. */
2078                        /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Duplicate packet is dropped!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2079                        /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2080                        return false;
2081                else
2082                        break;
2083
2084                /* DbgPrint("enqueue_reorder_recvframe():while\n"); */
2085
2086        }
2087
2088
2089        /* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2090        /* spin_lock(&ppending_recvframe_queue->lock); */
2091
2092        list_del_init(&(prframe->u.hdr.list));
2093
2094        list_add_tail(&(prframe->u.hdr.list), plist);
2095
2096        /* spin_unlock(&ppending_recvframe_queue->lock); */
2097        /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2098
2099
2100        /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Pkt insert into buffer!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2101        return true;
2102
2103}
2104
2105void recv_indicatepkts_pkt_loss_cnt(struct debug_priv *pdbgpriv, u64 prev_seq, u64 current_seq);
2106void recv_indicatepkts_pkt_loss_cnt(struct debug_priv *pdbgpriv, u64 prev_seq, u64 current_seq)
2107{
2108        if (current_seq < prev_seq)
2109                pdbgpriv->dbg_rx_ampdu_loss_count += (4096 + current_seq - prev_seq);
2110        else
2111                pdbgpriv->dbg_rx_ampdu_loss_count += (current_seq - prev_seq);
2112
2113}
2114int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced);
2115int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
2116{
2117        struct list_head        *phead, *plist;
2118        union recv_frame *prframe;
2119        struct rx_pkt_attrib *pattrib;
2120        /* u8 index = 0; */
2121        int bPktInBuf = false;
2122        struct recv_priv *precvpriv = &padapter->recvpriv;
2123        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2124        struct dvobj_priv *psdpriv = padapter->dvobj;
2125        struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
2126
2127        DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_in_oder);
2128
2129        /* DbgPrint("+recv_indicatepkts_in_order\n"); */
2130
2131        /* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2132        /* spin_lock(&ppending_recvframe_queue->lock); */
2133
2134        phead =         get_list_head(ppending_recvframe_queue);
2135        plist = get_next(phead);
2136
2137        /*  Handling some condition for forced indicate case. */
2138        if (bforced == true) {
2139                pdbgpriv->dbg_rx_ampdu_forced_indicate_count++;
2140                if (list_empty(phead)) {
2141                        /*  spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2142                        /* spin_unlock(&ppending_recvframe_queue->lock); */
2143                        return true;
2144                }
2145
2146                prframe = (union recv_frame *)plist;
2147                pattrib = &prframe->u.hdr.attrib;
2148
2149                #ifdef DBG_RX_SEQ
2150                DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2151                        preorder_ctrl->indicate_seq, pattrib->seq_num);
2152                #endif
2153                recv_indicatepkts_pkt_loss_cnt(pdbgpriv, preorder_ctrl->indicate_seq, pattrib->seq_num);
2154                preorder_ctrl->indicate_seq = pattrib->seq_num;
2155
2156        }
2157
2158        /*  Prepare indication list and indication. */
2159        /*  Check if there is any packet need indicate. */
2160        while (!list_empty(phead)) {
2161
2162                prframe = (union recv_frame *)plist;
2163                pattrib = &prframe->u.hdr.attrib;
2164
2165                if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
2166                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2167                                 ("recv_indicatepkts_in_order: indicate =%d seq =%d amsdu =%d\n",
2168                                  preorder_ctrl->indicate_seq, pattrib->seq_num, pattrib->amsdu));
2169
2170                        plist = get_next(plist);
2171                        list_del_init(&(prframe->u.hdr.list));
2172
2173                        if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
2174                                preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2175                                #ifdef DBG_RX_SEQ
2176                                DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2177                                        preorder_ctrl->indicate_seq, pattrib->seq_num);
2178                                #endif
2179                        }
2180
2181                        /* Set this as a lock to make sure that only one thread is indicating packet. */
2182                        /* pTS->RxIndicateState = RXTS_INDICATE_PROCESSING; */
2183
2184                        /*  Indicate packets */
2185                        /* RT_ASSERT((index<=REORDER_WIN_SIZE), ("RxReorderIndicatePacket(): Rx Reorder buffer full!!\n")); */
2186
2187
2188                        /* indicate this recv_frame */
2189                        /* DbgPrint("recv_indicatepkts_in_order, indicate_seq =%d, seq_num =%d\n", precvpriv->indicate_seq, pattrib->seq_num); */
2190                        if (!pattrib->amsdu) {
2191                                /* DBG_871X("recv_indicatepkts_in_order, amsdu!= 1, indicate_seq =%d, seq_num =%d\n", preorder_ctrl->indicate_seq, pattrib->seq_num); */
2192
2193                                if ((padapter->bDriverStopped == false) &&
2194                                    (padapter->bSurpriseRemoved == false))
2195                                        rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
2196
2197                        } else if (pattrib->amsdu == 1) {
2198                                if (amsdu_to_msdu(padapter, prframe) != _SUCCESS)
2199                                        rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
2200
2201                        } else{
2202                                /* error condition; */
2203                        }
2204
2205
2206                        /* Update local variables. */
2207                        bPktInBuf = false;
2208
2209                } else{
2210                        bPktInBuf = true;
2211                        break;
2212                }
2213
2214                /* DbgPrint("recv_indicatepkts_in_order():while\n"); */
2215
2216        }
2217
2218        /* spin_unlock(&ppending_recvframe_queue->lock); */
2219        /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2220
2221        return bPktInBuf;
2222}
2223
2224int recv_indicatepkt_reorder(struct adapter *padapter, union recv_frame *prframe);
2225int recv_indicatepkt_reorder(struct adapter *padapter, union recv_frame *prframe)
2226{
2227        int retval = _SUCCESS;
2228        struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2229        struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
2230        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2231        struct dvobj_priv *psdpriv = padapter->dvobj;
2232        struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
2233
2234        DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_reoder);
2235
2236        if (!pattrib->amsdu) {
2237                /* s1. */
2238                wlanhdr_to_ethhdr(prframe);
2239
2240                if (pattrib->qos != 1) {
2241                        if ((padapter->bDriverStopped == false) &&
2242                            (padapter->bSurpriseRemoved == false)) {
2243                                RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@  recv_indicatepkt_reorder -recv_func recv_indicatepkt\n"));
2244
2245                                rtw_recv_indicatepkt(padapter, prframe);
2246                                return _SUCCESS;
2247
2248                        }
2249
2250                        #ifdef DBG_RX_DROP_FRAME
2251                        DBG_871X("DBG_RX_DROP_FRAME %s pattrib->qos != 1\n", __func__);
2252                        #endif
2253
2254                        return _FAIL;
2255
2256                }
2257
2258                if (preorder_ctrl->enable == false) {
2259                        /* indicate this recv_frame */
2260                        preorder_ctrl->indicate_seq = pattrib->seq_num;
2261                        #ifdef DBG_RX_SEQ
2262                        DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2263                                preorder_ctrl->indicate_seq, pattrib->seq_num);
2264                        #endif
2265
2266                        rtw_recv_indicatepkt(padapter, prframe);
2267
2268                        preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2269                        #ifdef DBG_RX_SEQ
2270                        DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2271                                preorder_ctrl->indicate_seq, pattrib->seq_num);
2272                        #endif
2273
2274                        return _SUCCESS;
2275                }
2276        } else if (pattrib->amsdu == 1) { /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
2277                if (preorder_ctrl->enable == false) {
2278                        preorder_ctrl->indicate_seq = pattrib->seq_num;
2279                        #ifdef DBG_RX_SEQ
2280                        DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2281                                preorder_ctrl->indicate_seq, pattrib->seq_num);
2282                        #endif
2283
2284                        retval = amsdu_to_msdu(padapter, prframe);
2285
2286                        preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2287                        #ifdef DBG_RX_SEQ
2288                        DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2289                                preorder_ctrl->indicate_seq, pattrib->seq_num);
2290                        #endif
2291
2292                        if (retval != _SUCCESS) {
2293                                #ifdef DBG_RX_DROP_FRAME
2294                                DBG_871X("DBG_RX_DROP_FRAME %s amsdu_to_msdu fail\n", __func__);
2295                                #endif
2296                        }
2297
2298                        return retval;
2299                }
2300        }
2301
2302        spin_lock_bh(&ppending_recvframe_queue->lock);
2303
2304        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2305                 ("recv_indicatepkt_reorder: indicate =%d seq =%d\n",
2306                  preorder_ctrl->indicate_seq, pattrib->seq_num));
2307
2308        /* s2. check if winstart_b(indicate_seq) needs to been updated */
2309        if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num)) {
2310                pdbgpriv->dbg_rx_ampdu_drop_count++;
2311                #ifdef DBG_RX_DROP_FRAME
2312                DBG_871X("DBG_RX_DROP_FRAME %s check_indicate_seq fail\n", __func__);
2313                #endif
2314                goto _err_exit;
2315        }
2316
2317
2318        /* s3. Insert all packet into Reorder Queue to maintain its ordering. */
2319        if (!enqueue_reorder_recvframe(preorder_ctrl, prframe)) {
2320                /* DbgPrint("recv_indicatepkt_reorder, enqueue_reorder_recvframe fail!\n"); */
2321                /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2322                /* return _FAIL; */
2323                #ifdef DBG_RX_DROP_FRAME
2324                DBG_871X("DBG_RX_DROP_FRAME %s enqueue_reorder_recvframe fail\n", __func__);
2325                #endif
2326                goto _err_exit;
2327        }
2328
2329
2330        /* s4. */
2331        /*  Indication process. */
2332        /*  After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
2333        /*  with the SeqNum smaller than latest WinStart and buffer other packets. */
2334        /*  */
2335        /*  For Rx Reorder condition: */
2336        /*  1. All packets with SeqNum smaller than WinStart => Indicate */
2337        /*  2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
2338        /*  */
2339
2340        /* recv_indicatepkts_in_order(padapter, preorder_ctrl, true); */
2341        if (recv_indicatepkts_in_order(padapter, preorder_ctrl, false) == true) {
2342                _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2343                spin_unlock_bh(&ppending_recvframe_queue->lock);
2344        } else{
2345                spin_unlock_bh(&ppending_recvframe_queue->lock);
2346                del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
2347        }
2348
2349        return _SUCCESS;
2350
2351_err_exit:
2352        spin_unlock_bh(&ppending_recvframe_queue->lock);
2353
2354        return _FAIL;
2355}
2356
2357
2358void rtw_reordering_ctrl_timeout_handler(struct timer_list *t)
2359{
2360        struct recv_reorder_ctrl *preorder_ctrl =
2361                from_timer(preorder_ctrl, t, reordering_ctrl_timer);
2362        struct adapter *padapter = preorder_ctrl->padapter;
2363        struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2364
2365
2366        if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
2367                return;
2368
2369        /* DBG_871X("+rtw_reordering_ctrl_timeout_handler() =>\n"); */
2370
2371        spin_lock_bh(&ppending_recvframe_queue->lock);
2372
2373        if (recv_indicatepkts_in_order(padapter, preorder_ctrl, true) == true)
2374                _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2375
2376        spin_unlock_bh(&ppending_recvframe_queue->lock);
2377
2378}
2379
2380int process_recv_indicatepkts(struct adapter *padapter, union recv_frame *prframe);
2381int process_recv_indicatepkts(struct adapter *padapter, union recv_frame *prframe)
2382{
2383        int retval = _SUCCESS;
2384        /* struct recv_priv *precvpriv = &padapter->recvpriv; */
2385        /* struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib; */
2386        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2387        struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2388
2389        DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate);
2390
2391        if (phtpriv->ht_option == true) { /* B/G/N Mode */
2392                /* prframe->u.hdr.preorder_ctrl = &precvpriv->recvreorder_ctrl[pattrib->priority]; */
2393
2394                if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) { /*  including perform A-MPDU Rx Ordering Buffer Control */
2395                        #ifdef DBG_RX_DROP_FRAME
2396                        DBG_871X("DBG_RX_DROP_FRAME %s recv_indicatepkt_reorder error!\n", __func__);
2397                        #endif
2398
2399                        if ((padapter->bDriverStopped == false) &&
2400                            (padapter->bSurpriseRemoved == false)) {
2401                                retval = _FAIL;
2402                                return retval;
2403                        }
2404                }
2405        } else { /* B/G mode */
2406                retval = wlanhdr_to_ethhdr(prframe);
2407                if (retval != _SUCCESS) {
2408                        RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("wlanhdr_to_ethhdr: drop pkt\n"));
2409                        #ifdef DBG_RX_DROP_FRAME
2410                        DBG_871X("DBG_RX_DROP_FRAME %s wlanhdr_to_ethhdr error!\n", __func__);
2411                        #endif
2412                        return retval;
2413                }
2414
2415                if ((padapter->bDriverStopped == false) && (padapter->bSurpriseRemoved == false)) {
2416                        /* indicate this recv_frame */
2417                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func recv_indicatepkt\n"));
2418                        rtw_recv_indicatepkt(padapter, prframe);
2419
2420
2421                } else{
2422                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func free_indicatepkt\n"));
2423
2424                        RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
2425                        retval = _FAIL;
2426                        return retval;
2427                }
2428
2429        }
2430
2431        return retval;
2432
2433}
2434
2435static int recv_func_prehandle(struct adapter *padapter, union recv_frame *rframe)
2436{
2437        int ret = _SUCCESS;
2438        struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2439
2440        DBG_COUNTER(padapter->rx_logs.core_rx_pre);
2441
2442        /* check the frame crtl field and decache */
2443        ret = validate_recv_frame(padapter, rframe);
2444        if (ret != _SUCCESS) {
2445                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recv_func: validate_recv_frame fail! drop pkt\n"));
2446                rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
2447                goto exit;
2448        }
2449
2450exit:
2451        return ret;
2452}
2453
2454static int recv_func_posthandle(struct adapter *padapter, union recv_frame *prframe)
2455{
2456        int ret = _SUCCESS;
2457        union recv_frame *orig_prframe = prframe;
2458        struct recv_priv *precvpriv = &padapter->recvpriv;
2459        struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2460
2461        DBG_COUNTER(padapter->rx_logs.core_rx_post);
2462
2463        prframe = decryptor(padapter, prframe);
2464        if (prframe == NULL) {
2465                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decryptor: drop pkt\n"));
2466                #ifdef DBG_RX_DROP_FRAME
2467                DBG_871X("DBG_RX_DROP_FRAME %s decryptor: drop pkt\n", __func__);
2468                #endif
2469                ret = _FAIL;
2470                DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_err);
2471                goto _recv_data_drop;
2472        }
2473
2474        prframe = recvframe_chk_defrag(padapter, prframe);
2475        if (prframe == NULL)    {
2476                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chk_defrag: drop pkt\n"));
2477                #ifdef DBG_RX_DROP_FRAME
2478                DBG_871X("DBG_RX_DROP_FRAME %s recvframe_chk_defrag: drop pkt\n", __func__);
2479                #endif
2480                DBG_COUNTER(padapter->rx_logs.core_rx_post_defrag_err);
2481                goto _recv_data_drop;
2482        }
2483
2484        prframe = portctrl(padapter, prframe);
2485        if (prframe == NULL) {
2486                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("portctrl: drop pkt\n"));
2487                #ifdef DBG_RX_DROP_FRAME
2488                DBG_871X("DBG_RX_DROP_FRAME %s portctrl: drop pkt\n", __func__);
2489                #endif
2490                ret = _FAIL;
2491                DBG_COUNTER(padapter->rx_logs.core_rx_post_portctrl_err);
2492                goto _recv_data_drop;
2493        }
2494
2495        count_rx_stats(padapter, prframe, NULL);
2496
2497        ret = process_recv_indicatepkts(padapter, prframe);
2498        if (ret != _SUCCESS) {
2499                RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recv_func: process_recv_indicatepkts fail!\n"));
2500                #ifdef DBG_RX_DROP_FRAME
2501                DBG_871X("DBG_RX_DROP_FRAME %s process_recv_indicatepkts fail!\n", __func__);
2502                #endif
2503                rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
2504                DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_err);
2505                goto _recv_data_drop;
2506        }
2507
2508_recv_data_drop:
2509        precvpriv->rx_drop++;
2510        return ret;
2511}
2512
2513
2514int recv_func(struct adapter *padapter, union recv_frame *rframe);
2515int recv_func(struct adapter *padapter, union recv_frame *rframe)
2516{
2517        int ret;
2518        struct rx_pkt_attrib *prxattrib = &rframe->u.hdr.attrib;
2519        struct recv_priv *recvpriv = &padapter->recvpriv;
2520        struct security_priv *psecuritypriv = &padapter->securitypriv;
2521        struct mlme_priv *mlmepriv = &padapter->mlmepriv;
2522
2523        /* check if need to handle uc_swdec_pending_queue*/
2524        if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey) {
2525                union recv_frame *pending_frame;
2526                int cnt = 0;
2527
2528                while ((pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
2529                        cnt++;
2530                        DBG_COUNTER(padapter->rx_logs.core_rx_dequeue);
2531                        recv_func_posthandle(padapter, pending_frame);
2532                }
2533
2534                if (cnt)
2535                        DBG_871X(FUNC_ADPT_FMT" dequeue %d from uc_swdec_pending_queue\n",
2536                                FUNC_ADPT_ARG(padapter), cnt);
2537        }
2538
2539        DBG_COUNTER(padapter->rx_logs.core_rx);
2540        ret = recv_func_prehandle(padapter, rframe);
2541
2542        if (ret == _SUCCESS) {
2543
2544                /* check if need to enqueue into uc_swdec_pending_queue*/
2545                if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
2546                        !IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
2547                        (prxattrib->bdecrypted == 0 || psecuritypriv->sw_decrypt == true) &&
2548                        psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPAPSK &&
2549                        !psecuritypriv->busetkipkey) {
2550                        DBG_COUNTER(padapter->rx_logs.core_rx_enqueue);
2551                        rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
2552                        /* DBG_871X("%s: no key, enqueue uc_swdec_pending_queue\n", __func__); */
2553
2554                        if (recvpriv->free_recvframe_cnt < NR_RECVFRAME/4) {
2555                                /* to prevent from recvframe starvation, get recvframe from uc_swdec_pending_queue to free_recvframe_cnt  */
2556                                rframe = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue);
2557                                if (rframe)
2558                                        goto do_posthandle;
2559                        }
2560                        goto exit;
2561                }
2562
2563do_posthandle:
2564                ret = recv_func_posthandle(padapter, rframe);
2565        }
2566
2567exit:
2568        return ret;
2569}
2570
2571
2572s32 rtw_recv_entry(union recv_frame *precvframe)
2573{
2574        struct adapter *padapter;
2575        struct recv_priv *precvpriv;
2576        s32 ret = _SUCCESS;
2577
2578/*      RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+rtw_recv_entry\n")); */
2579
2580        padapter = precvframe->u.hdr.adapter;
2581
2582        precvpriv = &padapter->recvpriv;
2583
2584        ret = recv_func(padapter, precvframe);
2585        if (ret == _FAIL) {
2586                RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("rtw_recv_entry: recv_func return fail!!!\n"));
2587                goto _recv_entry_drop;
2588        }
2589
2590
2591        precvpriv->rx_pkts++;
2592
2593        return ret;
2594
2595_recv_entry_drop:
2596
2597        /* RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("_recv_entry_drop\n")); */
2598
2599        return ret;
2600}
2601
2602static void rtw_signal_stat_timer_hdl(struct timer_list *t)
2603{
2604        struct adapter *adapter =
2605                from_timer(adapter, t, recvpriv.signal_stat_timer);
2606        struct recv_priv *recvpriv = &adapter->recvpriv;
2607
2608        u32 tmp_s, tmp_q;
2609        u8 avg_signal_strength = 0;
2610        u8 avg_signal_qual = 0;
2611        u32 num_signal_strength = 0;
2612        u32 num_signal_qual = 0;
2613        u8 _alpha = 5; /*  this value is based on converging_constant = 5000 and sampling_interval = 1000 */
2614
2615        if (adapter->recvpriv.is_signal_dbg) {
2616                /* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
2617                adapter->recvpriv.signal_strength = adapter->recvpriv.signal_strength_dbg;
2618                adapter->recvpriv.rssi = (s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
2619        } else {
2620
2621                if (recvpriv->signal_strength_data.update_req == 0) {/*  update_req is clear, means we got rx */
2622                        avg_signal_strength = recvpriv->signal_strength_data.avg_val;
2623                        num_signal_strength = recvpriv->signal_strength_data.total_num;
2624                        /*  after avg_vals are accquired, we can re-stat the signal values */
2625                        recvpriv->signal_strength_data.update_req = 1;
2626                }
2627
2628                if (recvpriv->signal_qual_data.update_req == 0) {/*  update_req is clear, means we got rx */
2629                        avg_signal_qual = recvpriv->signal_qual_data.avg_val;
2630                        num_signal_qual = recvpriv->signal_qual_data.total_num;
2631                        /*  after avg_vals are accquired, we can re-stat the signal values */
2632                        recvpriv->signal_qual_data.update_req = 1;
2633                }
2634
2635                if (num_signal_strength == 0) {
2636                        if (rtw_get_on_cur_ch_time(adapter) == 0
2637                                || jiffies_to_msecs(jiffies - rtw_get_on_cur_ch_time(adapter)) < 2 * adapter->mlmeextpriv.mlmext_info.bcn_interval
2638                        ) {
2639                                goto set_timer;
2640                        }
2641                }
2642
2643                if (check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY) == true
2644                        || check_fwstate(&adapter->mlmepriv, _FW_LINKED) == false
2645                ) {
2646                        goto set_timer;
2647                }
2648
2649                /* update value of signal_strength, rssi, signal_qual */
2650                tmp_s = (avg_signal_strength+(_alpha-1)*recvpriv->signal_strength);
2651                if (tmp_s % _alpha)
2652                        tmp_s = tmp_s/_alpha + 1;
2653                else
2654                        tmp_s = tmp_s/_alpha;
2655                if (tmp_s > 100)
2656                        tmp_s = 100;
2657
2658                tmp_q = (avg_signal_qual+(_alpha-1)*recvpriv->signal_qual);
2659                if (tmp_q % _alpha)
2660                        tmp_q = tmp_q/_alpha + 1;
2661                else
2662                        tmp_q = tmp_q/_alpha;
2663                if (tmp_q > 100)
2664                        tmp_q = 100;
2665
2666                recvpriv->signal_strength = tmp_s;
2667                recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
2668                recvpriv->signal_qual = tmp_q;
2669
2670                #if defined(DBG_RX_SIGNAL_DISPLAY_PROCESSING) && 1
2671                DBG_871X(FUNC_ADPT_FMT" signal_strength:%3u, rssi:%3d, signal_qual:%3u"
2672                        ", num_signal_strength:%u, num_signal_qual:%u"
2673                        ", on_cur_ch_ms:%d"
2674                        "\n"
2675                        , FUNC_ADPT_ARG(adapter)
2676                        , recvpriv->signal_strength
2677                        , recvpriv->rssi
2678                        , recvpriv->signal_qual
2679                        , num_signal_strength, num_signal_qual
2680                        , rtw_get_on_cur_ch_time(adapter) ? jiffies_to_msecs(jiffies - rtw_get_on_cur_ch_time(adapter)) : 0
2681                );
2682                #endif
2683        }
2684
2685set_timer:
2686        rtw_set_signal_stat_timer(recvpriv);
2687
2688}
2689