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