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