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