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