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