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