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