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