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 <drv_types.h>
10 #include <rtw_debug.h>
11 #include <linux/jiffies.h>
12 #include <rtw_recv.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 u8 rtw_rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
18 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
19 u8 rtw_bridge_tunnel_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
20 
21 static void rtw_signal_stat_timer_hdl(struct timer_list *t);
22 
_rtw_init_sta_recv_priv(struct sta_recv_priv * psta_recvpriv)23 void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
24 {
25 	memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
26 
27 	spin_lock_init(&psta_recvpriv->lock);
28 
29 	/* for (i = 0; i<MAX_RX_NUMBLKS; i++) */
30 	/* 	_rtw_init_queue(&psta_recvpriv->blk_strms[i]); */
31 
32 	_rtw_init_queue(&psta_recvpriv->defrag_q);
33 }
34 
_rtw_init_recv_priv(struct recv_priv * precvpriv,struct adapter * padapter)35 sint _rtw_init_recv_priv(struct recv_priv *precvpriv, struct adapter *padapter)
36 {
37 	sint i;
38 	union recv_frame *precvframe;
39 	sint	res = _SUCCESS;
40 
41 	spin_lock_init(&precvpriv->lock);
42 
43 	_rtw_init_queue(&precvpriv->free_recv_queue);
44 	_rtw_init_queue(&precvpriv->recv_pending_queue);
45 	_rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
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 == NULL) {
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 		res = 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 
_rtw_free_recv_priv(struct recv_priv * precvpriv)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 	if (precvpriv->pallocated_frame_buf)
101 		vfree(precvpriv->pallocated_frame_buf);
102 
103 	rtw_hal_free_recv_priv(padapter);
104 }
105 
_rtw_alloc_recvframe(struct __queue * pfree_recv_queue)106 union recv_frame *_rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
107 {
108 
109 	union recv_frame  *precvframe;
110 	struct list_head	*plist, *phead;
111 	struct adapter *padapter;
112 	struct recv_priv *precvpriv;
113 
114 	if (list_empty(&pfree_recv_queue->queue))
115 		precvframe = NULL;
116 	else{
117 		phead = get_list_head(pfree_recv_queue);
118 
119 		plist = get_next(phead);
120 
121 		precvframe = (union recv_frame *)plist;
122 
123 		list_del_init(&precvframe->u.hdr.list);
124 		padapter = precvframe->u.hdr.adapter;
125 		if (padapter != NULL) {
126 			precvpriv = &padapter->recvpriv;
127 			if (pfree_recv_queue == &precvpriv->free_recv_queue)
128 				precvpriv->free_recvframe_cnt--;
129 		}
130 	}
131 	return precvframe;
132 }
133 
rtw_alloc_recvframe(struct __queue * pfree_recv_queue)134 union recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
135 {
136 	union recv_frame  *precvframe;
137 
138 	spin_lock_bh(&pfree_recv_queue->lock);
139 
140 	precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
141 
142 	spin_unlock_bh(&pfree_recv_queue->lock);
143 
144 	return precvframe;
145 }
146 
rtw_free_recvframe(union recv_frame * precvframe,struct __queue * pfree_recv_queue)147 int rtw_free_recvframe(union recv_frame *precvframe, struct __queue *pfree_recv_queue)
148 {
149 	struct adapter *padapter = precvframe->u.hdr.adapter;
150 	struct recv_priv *precvpriv = &padapter->recvpriv;
151 
152 	rtw_os_free_recvframe(precvframe);
153 
154 
155 	spin_lock_bh(&pfree_recv_queue->lock);
156 
157 	list_del_init(&(precvframe->u.hdr.list));
158 
159 	precvframe->u.hdr.len = 0;
160 
161 	list_add_tail(&(precvframe->u.hdr.list), get_list_head(pfree_recv_queue));
162 
163 	if (padapter != NULL) {
164 		if (pfree_recv_queue == &precvpriv->free_recv_queue)
165 				precvpriv->free_recvframe_cnt++;
166 	}
167 	spin_unlock_bh(&pfree_recv_queue->lock);
168 	return _SUCCESS;
169 }
170 
171 
172 
173 
_rtw_enqueue_recvframe(union recv_frame * precvframe,struct __queue * queue)174 sint _rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
175 {
176 
177 	struct adapter *padapter = precvframe->u.hdr.adapter;
178 	struct recv_priv *precvpriv = &padapter->recvpriv;
179 
180 	/* INIT_LIST_HEAD(&(precvframe->u.hdr.list)); */
181 	list_del_init(&(precvframe->u.hdr.list));
182 
183 
184 	list_add_tail(&(precvframe->u.hdr.list), get_list_head(queue));
185 
186 	if (padapter != NULL)
187 		if (queue == &precvpriv->free_recv_queue)
188 			precvpriv->free_recvframe_cnt++;
189 
190 	return _SUCCESS;
191 }
192 
rtw_enqueue_recvframe(union recv_frame * precvframe,struct __queue * queue)193 sint rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
194 {
195 	sint ret;
196 
197 	/* _spinlock(&pfree_recv_queue->lock); */
198 	spin_lock_bh(&queue->lock);
199 	ret = _rtw_enqueue_recvframe(precvframe, queue);
200 	/* spin_unlock(&pfree_recv_queue->lock); */
201 	spin_unlock_bh(&queue->lock);
202 
203 	return ret;
204 }
205 
206 /*
207 sint	rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
208 {
209 	return rtw_free_recvframe(precvframe, queue);
210 }
211 */
212 
213 
214 
215 
216 /*
217 caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
218 pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
219 
220 using spinlock to protect
221 
222 */
223 
rtw_free_recvframe_queue(struct __queue * pframequeue,struct __queue * pfree_recv_queue)224 void rtw_free_recvframe_queue(struct __queue *pframequeue,  struct __queue *pfree_recv_queue)
225 {
226 	union	recv_frame	*precvframe;
227 	struct list_head	*plist, *phead;
228 
229 	spin_lock(&pframequeue->lock);
230 
231 	phead = get_list_head(pframequeue);
232 	plist = get_next(phead);
233 
234 	while (phead != plist) {
235 		precvframe = (union recv_frame *)plist;
236 
237 		plist = get_next(plist);
238 
239 		rtw_free_recvframe(precvframe, pfree_recv_queue);
240 	}
241 
242 	spin_unlock(&pframequeue->lock);
243 }
244 
rtw_free_uc_swdec_pending_queue(struct adapter * adapter)245 u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter)
246 {
247 	u32 cnt = 0;
248 	union recv_frame *pending_frame;
249 	while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
250 		rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
251 		cnt++;
252 	}
253 
254 	if (cnt)
255 		DBG_871X(FUNC_ADPT_FMT" dequeue %d\n", FUNC_ADPT_ARG(adapter), cnt);
256 
257 	return cnt;
258 }
259 
260 
rtw_enqueue_recvbuf_to_head(struct recv_buf * precvbuf,struct __queue * queue)261 sint rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, struct __queue *queue)
262 {
263 	spin_lock_bh(&queue->lock);
264 
265 	list_del_init(&precvbuf->list);
266 	list_add(&precvbuf->list, get_list_head(queue));
267 
268 	spin_unlock_bh(&queue->lock);
269 
270 	return _SUCCESS;
271 }
272 
rtw_enqueue_recvbuf(struct recv_buf * precvbuf,struct __queue * queue)273 sint rtw_enqueue_recvbuf(struct recv_buf *precvbuf, struct __queue *queue)
274 {
275 	spin_lock_bh(&queue->lock);
276 
277 	list_del_init(&precvbuf->list);
278 
279 	list_add_tail(&precvbuf->list, get_list_head(queue));
280 	spin_unlock_bh(&queue->lock);
281 	return _SUCCESS;
282 
283 }
284 
rtw_dequeue_recvbuf(struct __queue * queue)285 struct recv_buf *rtw_dequeue_recvbuf(struct __queue *queue)
286 {
287 	struct recv_buf *precvbuf;
288 	struct list_head	*plist, *phead;
289 
290 	spin_lock_bh(&queue->lock);
291 
292 	if (list_empty(&queue->queue))
293 		precvbuf = NULL;
294 	else{
295 		phead = get_list_head(queue);
296 
297 		plist = get_next(phead);
298 
299 		precvbuf = LIST_CONTAINOR(plist, struct recv_buf, list);
300 
301 		list_del_init(&precvbuf->list);
302 
303 	}
304 
305 	spin_unlock_bh(&queue->lock);
306 
307 	return precvbuf;
308 
309 }
310 
311 sint recvframe_chkmic(struct adapter *adapter,  union recv_frame *precvframe);
recvframe_chkmic(struct adapter * adapter,union recv_frame * precvframe)312 sint recvframe_chkmic(struct adapter *adapter,  union recv_frame *precvframe)
313 {
314 
315 	sint	i, res = _SUCCESS;
316 	u32 datalen;
317 	u8 miccode[8];
318 	u8 bmic_err = false, brpt_micerror = true;
319 	u8 *pframe, *payload, *pframemic;
320 	u8 *mickey;
321 	/* u8 *iv, rxdata_key_idx = 0; */
322 	struct	sta_info 	*stainfo;
323 	struct	rx_pkt_attrib	*prxattrib = &precvframe->u.hdr.attrib;
324 	struct	security_priv *psecuritypriv = &adapter->securitypriv;
325 
326 	struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
327 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
328 
329 	stainfo = rtw_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
330 
331 	if (prxattrib->encrypt == _TKIP_) {
332 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:prxattrib->encrypt == _TKIP_\n"));
333 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:da = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
334 			prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2], prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5]));
335 
336 		/* calculate mic code */
337 		if (stainfo != NULL) {
338 			if (IS_MCAST(prxattrib->ra)) {
339 				/* mickey =&psecuritypriv->dot118021XGrprxmickey.skey[0]; */
340 				/* iv = precvframe->u.hdr.rx_data+prxattrib->hdrlen; */
341 				/* rxdata_key_idx =(((iv[3])>>6)&0x3) ; */
342 				mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
343 
344 				RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic: bcmc key\n"));
345 				/* DBG_871X("\n recvframe_chkmic: bcmc key psecuritypriv->dot118021XGrpKeyid(%d), pmlmeinfo->key_index(%d) , recv key_id(%d)\n", */
346 				/* 								psecuritypriv->dot118021XGrpKeyid, pmlmeinfo->key_index, rxdata_key_idx); */
347 
348 				if (psecuritypriv->binstallGrpkey == false) {
349 					res = _FAIL;
350 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n"));
351 					DBG_871X("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
352 					goto exit;
353 				}
354 			} else {
355 				mickey = &stainfo->dot11tkiprxmickey.skey[0];
356 				RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic: unicast key\n"));
357 			}
358 
359 			datalen = precvframe->u.hdr.len-prxattrib->hdrlen-prxattrib->iv_len-prxattrib->icv_len-8;/* icv_len included the mic code */
360 			pframe = precvframe->u.hdr.rx_data;
361 			payload = pframe+prxattrib->hdrlen+prxattrib->iv_len;
362 
363 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n prxattrib->iv_len =%d prxattrib->icv_len =%d\n", prxattrib->iv_len, prxattrib->icv_len));
364 
365 
366 			rtw_seccalctkipmic(mickey, pframe, payload, datalen, &miccode[0], (unsigned char)prxattrib->priority); /* care the length of the data */
367 
368 			pframemic = payload+datalen;
369 
370 			bmic_err = false;
371 
372 			for (i = 0; i < 8; i++) {
373 				if (miccode[i] != *(pframemic+i)) {
374 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic:miccode[%d](%02x) != *(pframemic+%d)(%02x) ", i, miccode[i], i, *(pframemic+i)));
375 					bmic_err = true;
376 				}
377 			}
378 
379 
380 			if (bmic_err == true) {
381 
382 				RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n *(pframemic-8)-*(pframemic-1) = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
383 					*(pframemic-8), *(pframemic-7), *(pframemic-6), *(pframemic-5), *(pframemic-4), *(pframemic-3), *(pframemic-2), *(pframemic-1)));
384 				RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n *(pframemic-16)-*(pframemic-9) = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
385 					*(pframemic-16), *(pframemic-15), *(pframemic-14), *(pframemic-13), *(pframemic-12), *(pframemic-11), *(pframemic-10), *(pframemic-9)));
386 
387 				{
388 					uint i;
389 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n ======demp packet (len =%d) ======\n", precvframe->u.hdr.len));
390 					for (i = 0; i < precvframe->u.hdr.len; i = i+8) {
391 						RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
392 							*(precvframe->u.hdr.rx_data+i), *(precvframe->u.hdr.rx_data+i+1),
393 							*(precvframe->u.hdr.rx_data+i+2), *(precvframe->u.hdr.rx_data+i+3),
394 							*(precvframe->u.hdr.rx_data+i+4), *(precvframe->u.hdr.rx_data+i+5),
395 							*(precvframe->u.hdr.rx_data+i+6), *(precvframe->u.hdr.rx_data+i+7)));
396 					}
397 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n ======demp packet end [len =%d]======\n", precvframe->u.hdr.len));
398 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n hrdlen =%d,\n", prxattrib->hdrlen));
399 				}
400 
401 				RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ra = 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x psecuritypriv->binstallGrpkey =%d ",
402 					prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2],
403 					prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5], psecuritypriv->binstallGrpkey));
404 
405 				/*  double check key_index for some timing issue , */
406 				/*  cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
407 				if ((IS_MCAST(prxattrib->ra) == true)  && (prxattrib->key_index != pmlmeinfo->key_index))
408 					brpt_micerror = false;
409 
410 				if ((prxattrib->bdecrypted == true) && (brpt_micerror == true)) {
411 					rtw_handle_tkip_mic_err(adapter, (u8)IS_MCAST(prxattrib->ra));
412 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted =%d ", prxattrib->bdecrypted));
413 					DBG_871X(" mic error :prxattrib->bdecrypted =%d\n", prxattrib->bdecrypted);
414 				} else{
415 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted =%d ", prxattrib->bdecrypted));
416 					DBG_871X(" mic error :prxattrib->bdecrypted =%d\n", prxattrib->bdecrypted);
417 				}
418 
419 				res = _FAIL;
420 
421 			} else {
422 				/* mic checked ok */
423 				if ((psecuritypriv->bcheck_grpkey == false) && (IS_MCAST(prxattrib->ra) == true)) {
424 					psecuritypriv->bcheck_grpkey = true;
425 					RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("psecuritypriv->bcheck_grpkey =true"));
426 				}
427 			}
428 
429 		} else
430 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic: rtw_get_stainfo == NULL!!!\n"));
431 
432 		recvframe_pull_tail(precvframe, 8);
433 
434 	}
435 
436 exit:
437 	return res;
438 
439 }
440 
441 /* decrypt and set the ivlen, icvlen of the recv_frame */
442 union recv_frame *decryptor(struct adapter *padapter, union recv_frame *precv_frame);
decryptor(struct adapter * padapter,union recv_frame * precv_frame)443 union recv_frame *decryptor(struct adapter *padapter, union recv_frame *precv_frame)
444 {
445 
446 	struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
447 	struct security_priv *psecuritypriv = &padapter->securitypriv;
448 	union recv_frame *return_packet = precv_frame;
449 	u32  res = _SUCCESS;
450 
451 	DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt);
452 
453 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("prxstat->decrypted =%x prxattrib->encrypt = 0x%03x\n", prxattrib->bdecrypted, prxattrib->encrypt));
454 
455 	if (prxattrib->encrypt > 0) {
456 		u8 *iv = precv_frame->u.hdr.rx_data+prxattrib->hdrlen;
457 		prxattrib->key_index = (((iv[3])>>6)&0x3);
458 
459 		if (prxattrib->key_index > WEP_KEYS) {
460 			DBG_871X("prxattrib->key_index(%d) > WEP_KEYS\n", prxattrib->key_index);
461 
462 			switch (prxattrib->encrypt) {
463 			case _WEP40_:
464 			case _WEP104_:
465 				prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
466 				break;
467 			case _TKIP_:
468 			case _AES_:
469 			default:
470 				prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
471 				break;
472 			}
473 		}
474 	}
475 
476 	if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) || (psecuritypriv->sw_decrypt == true))) {
477 		psecuritypriv->hw_decrypted = false;
478 
479 		#ifdef DBG_RX_DECRYPTOR
480 		DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
481 			__func__,
482 			__LINE__,
483 			prxattrib->bdecrypted,
484 			prxattrib->encrypt,
485 			psecuritypriv->hw_decrypted);
486 		#endif
487 
488 		switch (prxattrib->encrypt) {
489 		case _WEP40_:
490 		case _WEP104_:
491 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_wep);
492 			rtw_wep_decrypt(padapter, (u8 *)precv_frame);
493 			break;
494 		case _TKIP_:
495 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_tkip);
496 			res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
497 			break;
498 		case _AES_:
499 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_aes);
500 			res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
501 			break;
502 		default:
503 				break;
504 		}
505 	} else if (prxattrib->bdecrypted == 1
506 		&& prxattrib->encrypt > 0
507 		&& (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_)
508 		) {
509 		DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_hw);
510 
511 		psecuritypriv->hw_decrypted = true;
512 		#ifdef DBG_RX_DECRYPTOR
513 		DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
514 			__func__,
515 			__LINE__,
516 			prxattrib->bdecrypted,
517 			prxattrib->encrypt,
518 			psecuritypriv->hw_decrypted);
519 
520 		#endif
521 	} else {
522 		DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_unknown);
523 		#ifdef DBG_RX_DECRYPTOR
524 		DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
525 			__func__,
526 			__LINE__,
527 			prxattrib->bdecrypted,
528 			prxattrib->encrypt,
529 			psecuritypriv->hw_decrypted);
530 		#endif
531 	}
532 
533 	if (res == _FAIL) {
534 		rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
535 		return_packet = NULL;
536 	} else
537 		prxattrib->bdecrypted = true;
538 
539 	return return_packet;
540 }
541 
542 /* set the security information in the recv_frame */
543 union recv_frame *portctrl(struct adapter *adapter, union recv_frame *precv_frame);
portctrl(struct adapter * adapter,union recv_frame * precv_frame)544 union recv_frame *portctrl(struct adapter *adapter, union recv_frame *precv_frame)
545 {
546 	u8 *psta_addr = NULL;
547 	u8 *ptr;
548 	uint  auth_alg;
549 	struct recv_frame_hdr *pfhdr;
550 	struct sta_info *psta;
551 	struct sta_priv *pstapriv;
552 	union recv_frame *prtnframe;
553 	u16 ether_type = 0;
554 	u16  eapol_type = 0x888e;/* for Funia BD's WPA issue */
555 	struct rx_pkt_attrib *pattrib;
556 
557 	pstapriv = &adapter->stapriv;
558 
559 	auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
560 
561 	ptr = get_recvframe_data(precv_frame);
562 	pfhdr = &precv_frame->u.hdr;
563 	pattrib = &pfhdr->attrib;
564 	psta_addr = pattrib->ta;
565 
566 	prtnframe = NULL;
567 
568 	psta = rtw_get_stainfo(pstapriv, psta_addr);
569 
570 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:adapter->securitypriv.dot11AuthAlgrthm =%d\n", adapter->securitypriv.dot11AuthAlgrthm));
571 
572 	if (auth_alg == 2) {
573 		if ((psta != NULL) && (psta->ieee8021x_blocked)) {
574 			__be16 be_tmp;
575 
576 			/* blocked */
577 			/* only accept EAPOL frame */
578 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked == 1\n"));
579 
580 			prtnframe = precv_frame;
581 
582 			/* get ether_type */
583 			ptr = ptr+pfhdr->attrib.hdrlen+pfhdr->attrib.iv_len+LLC_HEADER_SIZE;
584 			memcpy(&be_tmp, ptr, 2);
585 			ether_type = ntohs(be_tmp);
586 
587 			if (ether_type == eapol_type)
588 				prtnframe = precv_frame;
589 			else {
590 				/* free this frame */
591 				rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
592 				prtnframe = NULL;
593 			}
594 		} else{
595 			/* allowed */
596 			/* check decryption status, and decrypt the frame if needed */
597 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked == 0\n"));
598 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:precv_frame->hdr.attrib.privacy =%x\n", precv_frame->u.hdr.attrib.privacy));
599 
600 			if (pattrib->bdecrypted == 0)
601 				RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:prxstat->decrypted =%x\n", pattrib->bdecrypted));
602 
603 			prtnframe = precv_frame;
604 			/* check is the EAPOL frame or not (Rekey) */
605 			/* if (ether_type == eapol_type) { */
606 			/* 	RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("########portctrl:ether_type == 0x888e\n")); */
607 				/* check Rekey */
608 
609 			/* 	prtnframe =precv_frame; */
610 			/*  */
611 			/* else { */
612 			/* 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:ether_type = 0x%04x\n", ether_type)); */
613 			/*  */
614 		}
615 	} else
616 		prtnframe = precv_frame;
617 
618 	return prtnframe;
619 }
620 
621 sint recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache);
recv_decache(union recv_frame * precv_frame,u8 bretry,struct stainfo_rxcache * prxcache)622 sint recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache)
623 {
624 	sint tid = precv_frame->u.hdr.attrib.priority;
625 
626 	u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num&0xffff) << 4) |
627 		(precv_frame->u.hdr.attrib.frag_num & 0xf);
628 
629 	if (tid > 15) {
630 		RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, (tid>15)! seq_ctrl = 0x%x, tid = 0x%x\n", seq_ctrl, tid));
631 
632 		return _FAIL;
633 	}
634 
635 	if (1) { /* if (bretry) */
636 		if (seq_ctrl == prxcache->tid_rxseq[tid]) {
637 			RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, seq_ctrl = 0x%x, tid = 0x%x, tid_rxseq = 0x%x\n", seq_ctrl, tid, prxcache->tid_rxseq[tid]));
638 
639 			return _FAIL;
640 		}
641 	}
642 
643 	prxcache->tid_rxseq[tid] = seq_ctrl;
644 
645 	return _SUCCESS;
646 
647 }
648 
649 void process_pwrbit_data(struct adapter *padapter, union recv_frame *precv_frame);
process_pwrbit_data(struct adapter * padapter,union recv_frame * precv_frame)650 void process_pwrbit_data(struct adapter *padapter, union recv_frame *precv_frame)
651 {
652 	unsigned char pwrbit;
653 	u8 *ptr = precv_frame->u.hdr.rx_data;
654 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
655 	struct sta_priv *pstapriv = &padapter->stapriv;
656 	struct sta_info *psta = NULL;
657 
658 	psta = rtw_get_stainfo(pstapriv, pattrib->src);
659 
660 	pwrbit = GetPwrMgt(ptr);
661 
662 	if (psta) {
663 		if (pwrbit) {
664 			if (!(psta->state & WIFI_SLEEP_STATE)) {
665 				/* psta->state |= WIFI_SLEEP_STATE; */
666 				/* pstapriv->sta_dz_bitmap |= BIT(psta->aid); */
667 
668 				stop_sta_xmit(padapter, psta);
669 
670 				/* DBG_871X("to sleep, sta_dz_bitmap =%x\n", pstapriv->sta_dz_bitmap); */
671 			}
672 		} else{
673 			if (psta->state & WIFI_SLEEP_STATE) {
674 				/* psta->state ^= WIFI_SLEEP_STATE; */
675 				/* pstapriv->sta_dz_bitmap &= ~BIT(psta->aid); */
676 
677 				wakeup_sta_to_xmit(padapter, psta);
678 
679 				/* DBG_871X("to wakeup, sta_dz_bitmap =%x\n", pstapriv->sta_dz_bitmap); */
680 			}
681 		}
682 
683 	}
684 }
685 
686 void process_wmmps_data(struct adapter *padapter, union recv_frame *precv_frame);
process_wmmps_data(struct adapter * padapter,union recv_frame * precv_frame)687 void process_wmmps_data(struct adapter *padapter, union recv_frame *precv_frame)
688 {
689 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
690 	struct sta_priv *pstapriv = &padapter->stapriv;
691 	struct sta_info *psta = NULL;
692 
693 	psta = rtw_get_stainfo(pstapriv, pattrib->src);
694 
695 	if (!psta)
696 		return;
697 
698 	if (!psta->qos_option)
699 		return;
700 
701 	if (!(psta->qos_info&0xf))
702 		return;
703 
704 	if (psta->state&WIFI_SLEEP_STATE) {
705 		u8 wmmps_ac = 0;
706 
707 		switch (pattrib->priority) {
708 		case 1:
709 		case 2:
710 			wmmps_ac = psta->uapsd_bk&BIT(1);
711 			break;
712 		case 4:
713 		case 5:
714 			wmmps_ac = psta->uapsd_vi&BIT(1);
715 			break;
716 		case 6:
717 		case 7:
718 			wmmps_ac = psta->uapsd_vo&BIT(1);
719 			break;
720 		case 0:
721 		case 3:
722 		default:
723 			wmmps_ac = psta->uapsd_be&BIT(1);
724 			break;
725 		}
726 
727 		if (wmmps_ac) {
728 			if (psta->sleepq_ac_len > 0)
729 				/* process received triggered frame */
730 				xmit_delivery_enabled_frames(padapter, psta);
731 			else
732 				/* issue one qos null frame with More data bit = 0 and the EOSP bit set (= 1) */
733 				issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
734 		}
735 	}
736 }
737 
738 void count_rx_stats(struct adapter *padapter, union recv_frame *prframe, struct sta_info *sta);
count_rx_stats(struct adapter * padapter,union recv_frame * prframe,struct sta_info * sta)739 void count_rx_stats(struct adapter *padapter, union recv_frame *prframe, struct sta_info *sta)
740 {
741 	int	sz;
742 	struct sta_info 	*psta = NULL;
743 	struct stainfo_stats	*pstats = NULL;
744 	struct rx_pkt_attrib	*pattrib = &prframe->u.hdr.attrib;
745 	struct recv_priv 	*precvpriv = &padapter->recvpriv;
746 
747 	sz = get_recvframe_len(prframe);
748 	precvpriv->rx_bytes += sz;
749 
750 	padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
751 
752 	if ((!MacAddr_isBcst(pattrib->dst)) && (!IS_MCAST(pattrib->dst))) {
753 		padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
754 	}
755 
756 	if (sta)
757 		psta = sta;
758 	else
759 		psta = prframe->u.hdr.psta;
760 
761 	if (psta) {
762 		pstats = &psta->sta_stats;
763 
764 		pstats->rx_data_pkts++;
765 		pstats->rx_bytes += sz;
766 	}
767 
768 	traffic_check_for_leave_lps(padapter, false, 0);
769 }
770 
771 sint sta2sta_data_frame(
772 	struct adapter *adapter,
773 	union recv_frame *precv_frame,
774 	struct sta_info **psta
775 );
sta2sta_data_frame(struct adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)776 sint sta2sta_data_frame(
777 	struct adapter *adapter,
778 	union recv_frame *precv_frame,
779 	struct sta_info **psta
780 )
781 {
782 	u8 *ptr = precv_frame->u.hdr.rx_data;
783 	sint ret = _SUCCESS;
784 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
785 	struct	sta_priv 	*pstapriv = &adapter->stapriv;
786 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
787 	u8 *mybssid  = get_bssid(pmlmepriv);
788 	u8 *myhwaddr = myid(&adapter->eeprompriv);
789 	u8 *sta_addr = NULL;
790 	sint bmcast = IS_MCAST(pattrib->dst);
791 
792 	/* DBG_871X("[%s] %d, seqnum:%d\n", __func__, __LINE__, pattrib->seq_num); */
793 
794 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
795 		(check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
796 
797 		/*  filter packets that SA is myself or multicast or broadcast */
798 		if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
799 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA ==myself\n"));
800 			ret = _FAIL;
801 			goto exit;
802 		}
803 
804 		if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN))	&& (!bmcast)) {
805 			ret = _FAIL;
806 			goto exit;
807 		}
808 
809 		if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
810 		   !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
811 		   (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
812 			ret = _FAIL;
813 			goto exit;
814 		}
815 
816 		sta_addr = pattrib->src;
817 
818 	} else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
819 		/*  For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
820 		if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN)) {
821 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("bssid != TA under STATION_MODE; drop pkt\n"));
822 			ret = _FAIL;
823 			goto exit;
824 		}
825 
826 		sta_addr = pattrib->bssid;
827 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
828 		if (bmcast) {
829 			/*  For AP mode, if DA == MCAST, then BSSID should be also MCAST */
830 			if (!IS_MCAST(pattrib->bssid)) {
831 					ret = _FAIL;
832 					goto exit;
833 			}
834 		} else{ /*  not mc-frame */
835 			/*  For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
836 			if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
837 				ret = _FAIL;
838 				goto exit;
839 			}
840 
841 			sta_addr = pattrib->src;
842 		}
843 
844 	} else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
845 		memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
846 		memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
847 		memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
848 		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
849 		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
850 
851 		sta_addr = mybssid;
852 	} else
853 		ret  = _FAIL;
854 
855 
856 
857 	if (bmcast)
858 		*psta = rtw_get_bcmc_stainfo(adapter);
859 	else
860 		*psta = rtw_get_stainfo(pstapriv, sta_addr); /*  get ap_info */
861 
862 	if (*psta == NULL) {
863 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under sta2sta_data_frame ; drop pkt\n"));
864 		ret = _FAIL;
865 		goto exit;
866 	}
867 
868 exit:
869 	return ret;
870 }
871 
872 sint ap2sta_data_frame(
873 	struct adapter *adapter,
874 	union recv_frame *precv_frame,
875 	struct sta_info **psta);
ap2sta_data_frame(struct adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)876 sint ap2sta_data_frame(
877 	struct adapter *adapter,
878 	union recv_frame *precv_frame,
879 	struct sta_info **psta)
880 {
881 	u8 *ptr = precv_frame->u.hdr.rx_data;
882 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
883 	sint ret = _SUCCESS;
884 	struct	sta_priv 	*pstapriv = &adapter->stapriv;
885 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
886 	u8 *mybssid  = get_bssid(pmlmepriv);
887 	u8 *myhwaddr = myid(&adapter->eeprompriv);
888 	sint bmcast = IS_MCAST(pattrib->dst);
889 
890 	if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)
891 		&& (check_fwstate(pmlmepriv, _FW_LINKED) == true
892 			|| check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true)
893 		) {
894 
895 		/*  filter packets that SA is myself or multicast or broadcast */
896 		if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
897 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA ==myself\n"));
898 			#ifdef DBG_RX_DROP_FRAME
899 			DBG_871X("DBG_RX_DROP_FRAME %s SA ="MAC_FMT", myhwaddr ="MAC_FMT"\n",
900 				__func__, MAC_ARG(pattrib->src), MAC_ARG(myhwaddr));
901 			#endif
902 			ret = _FAIL;
903 			goto exit;
904 		}
905 
906 		/*  da should be for me */
907 		if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
908 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
909 				(" ap2sta_data_frame:  compare DA fail; DA ="MAC_FMT"\n", MAC_ARG(pattrib->dst)));
910 			#ifdef DBG_RX_DROP_FRAME
911 			DBG_871X("DBG_RX_DROP_FRAME %s DA ="MAC_FMT"\n", __func__, MAC_ARG(pattrib->dst));
912 			#endif
913 			ret = _FAIL;
914 			goto exit;
915 		}
916 
917 
918 		/*  check BSSID */
919 		if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
920 		     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
921 		     (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
922 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
923 				(" ap2sta_data_frame:  compare BSSID fail ; BSSID ="MAC_FMT"\n", MAC_ARG(pattrib->bssid)));
924 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("mybssid ="MAC_FMT"\n", MAC_ARG(mybssid)));
925 			#ifdef DBG_RX_DROP_FRAME
926 			DBG_871X("DBG_RX_DROP_FRAME %s BSSID ="MAC_FMT", mybssid ="MAC_FMT"\n",
927 				__func__, MAC_ARG(pattrib->bssid), MAC_ARG(mybssid));
928 			DBG_871X("this adapter = %d, buddy adapter = %d\n", adapter->adapter_type, adapter->pbuddystruct adapter->adapter_type);
929 			#endif
930 
931 			if (!bmcast) {
932 				DBG_871X("issue_deauth to the nonassociated ap =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
933 				issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
934 			}
935 
936 			ret = _FAIL;
937 			goto exit;
938 		}
939 
940 		if (bmcast)
941 			*psta = rtw_get_bcmc_stainfo(adapter);
942 		else
943 			*psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get ap_info */
944 
945 		if (*psta == NULL) {
946 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ap2sta: can't get psta under STATION_MODE ; drop pkt\n"));
947 			#ifdef DBG_RX_DROP_FRAME
948 			DBG_871X("DBG_RX_DROP_FRAME %s can't get psta under STATION_MODE ; drop pkt\n", __func__);
949 			#endif
950 			ret = _FAIL;
951 			goto exit;
952 		}
953 
954 		if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
955 		}
956 
957 		if (GetFrameSubType(ptr) & BIT(6)) {
958 			/* No data, will not indicate to upper layer, temporily count it here */
959 			count_rx_stats(adapter, precv_frame, *psta);
960 			ret = RTW_RX_HANDLED;
961 			goto exit;
962 		}
963 
964 	} else if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) &&
965 		     (check_fwstate(pmlmepriv, _FW_LINKED) == true)) {
966 		memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
967 		memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
968 		memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
969 		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
970 		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
971 
972 		/*  */
973 		memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
974 
975 
976 		*psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
977 		if (*psta == NULL) {
978 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under MP_MODE ; drop pkt\n"));
979 			#ifdef DBG_RX_DROP_FRAME
980 			DBG_871X("DBG_RX_DROP_FRAME %s can't get psta under WIFI_MP_STATE ; drop pkt\n", __func__);
981 			#endif
982 			ret = _FAIL;
983 			goto exit;
984 		}
985 
986 
987 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
988 		/* Special case */
989 		ret = RTW_RX_HANDLED;
990 		goto exit;
991 	} else{
992 		if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
993 			*psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
994 			if (*psta == NULL) {
995 
996 				/* for AP multicast issue , modify by yiwei */
997 				static unsigned long send_issue_deauth_time;
998 
999 				/* DBG_871X("After send deauth , %u ms has elapsed.\n", jiffies_to_msecs(jiffies - send_issue_deauth_time)); */
1000 
1001 				if (jiffies_to_msecs(jiffies - send_issue_deauth_time) > 10000 || send_issue_deauth_time == 0) {
1002 					send_issue_deauth_time = jiffies;
1003 
1004 					DBG_871X("issue_deauth to the ap =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
1005 
1006 					issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1007 				}
1008 			}
1009 		}
1010 
1011 		ret = _FAIL;
1012 		#ifdef DBG_RX_DROP_FRAME
1013 		DBG_871X("DBG_RX_DROP_FRAME %s fw_state:0x%x\n", __func__, get_fwstate(pmlmepriv));
1014 		#endif
1015 	}
1016 
1017 exit:
1018 	return ret;
1019 }
1020 
1021 sint sta2ap_data_frame(
1022 	struct adapter *adapter,
1023 	union recv_frame *precv_frame,
1024 	struct sta_info **psta);
sta2ap_data_frame(struct adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)1025 sint sta2ap_data_frame(
1026 	struct adapter *adapter,
1027 	union recv_frame *precv_frame,
1028 	struct sta_info **psta)
1029 {
1030 	u8 *ptr = precv_frame->u.hdr.rx_data;
1031 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1032 	struct	sta_priv 	*pstapriv = &adapter->stapriv;
1033 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
1034 	unsigned char *mybssid  = get_bssid(pmlmepriv);
1035 	sint ret = _SUCCESS;
1036 
1037 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
1038 		/* For AP mode, RA =BSSID, TX =STA(SRC_ADDR), A3 =DST_ADDR */
1039 		if (memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
1040 			ret = _FAIL;
1041 			goto exit;
1042 		}
1043 
1044 		*psta = rtw_get_stainfo(pstapriv, pattrib->src);
1045 		if (*psta == NULL) {
1046 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under AP_MODE; drop pkt\n"));
1047 			DBG_871X("issue_deauth to sta =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1048 
1049 			issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1050 
1051 			ret = RTW_RX_HANDLED;
1052 			goto exit;
1053 		}
1054 
1055 		process_pwrbit_data(adapter, precv_frame);
1056 
1057 		if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
1058 			process_wmmps_data(adapter, precv_frame);
1059 		}
1060 
1061 		if (GetFrameSubType(ptr) & BIT(6)) {
1062 			/* No data, will not indicate to upper layer, temporily count it here */
1063 			count_rx_stats(adapter, precv_frame, *psta);
1064 			ret = RTW_RX_HANDLED;
1065 			goto exit;
1066 		}
1067 	} else {
1068 		u8 *myhwaddr = myid(&adapter->eeprompriv);
1069 		if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
1070 			ret = RTW_RX_HANDLED;
1071 			goto exit;
1072 		}
1073 		DBG_871X("issue_deauth to sta =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1074 		issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1075 		ret = RTW_RX_HANDLED;
1076 		goto exit;
1077 	}
1078 
1079 exit:
1080 	return ret;
1081 }
1082 
1083 sint validate_recv_ctrl_frame(struct adapter *padapter, union recv_frame *precv_frame);
validate_recv_ctrl_frame(struct adapter * padapter,union recv_frame * precv_frame)1084 sint validate_recv_ctrl_frame(struct adapter *padapter, union recv_frame *precv_frame)
1085 {
1086 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1087 	struct sta_priv *pstapriv = &padapter->stapriv;
1088 	u8 *pframe = precv_frame->u.hdr.rx_data;
1089 	struct sta_info *psta = NULL;
1090 	/* uint len = precv_frame->u.hdr.len; */
1091 
1092 	/* DBG_871X("+validate_recv_ctrl_frame\n"); */
1093 
1094 	if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
1095 		return _FAIL;
1096 
1097 	/* receive the frames that ra(a1) is my address */
1098 	if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
1099 		return _FAIL;
1100 
1101 	psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1102 	if (psta == NULL)
1103 		return _FAIL;
1104 
1105 	/* for rx pkt statistics */
1106 	psta->sta_stats.rx_ctrl_pkts++;
1107 
1108 	/* only handle ps-poll */
1109 	if (GetFrameSubType(pframe) == WIFI_PSPOLL) {
1110 		u16 aid;
1111 		u8 wmmps_ac = 0;
1112 
1113 		aid = GetAid(pframe);
1114 		if (psta->aid != aid)
1115 			return _FAIL;
1116 
1117 		switch (pattrib->priority) {
1118 		case 1:
1119 		case 2:
1120 			wmmps_ac = psta->uapsd_bk&BIT(0);
1121 			break;
1122 		case 4:
1123 		case 5:
1124 			wmmps_ac = psta->uapsd_vi&BIT(0);
1125 			break;
1126 		case 6:
1127 		case 7:
1128 			wmmps_ac = psta->uapsd_vo&BIT(0);
1129 			break;
1130 		case 0:
1131 		case 3:
1132 		default:
1133 			wmmps_ac = psta->uapsd_be&BIT(0);
1134 			break;
1135 		}
1136 
1137 		if (wmmps_ac)
1138 			return _FAIL;
1139 
1140 		if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1141 			DBG_871X("%s alive check-rx ps-poll\n", __func__);
1142 			psta->expire_to = pstapriv->expire_to;
1143 			psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1144 		}
1145 
1146 		if ((psta->state&WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap&BIT(psta->aid))) {
1147 			struct list_head	*xmitframe_plist, *xmitframe_phead;
1148 			struct xmit_frame *pxmitframe = NULL;
1149 			struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1150 
1151 			/* spin_lock_bh(&psta->sleep_q.lock); */
1152 			spin_lock_bh(&pxmitpriv->lock);
1153 
1154 			xmitframe_phead = get_list_head(&psta->sleep_q);
1155 			xmitframe_plist = get_next(xmitframe_phead);
1156 
1157 			if (xmitframe_phead != xmitframe_plist) {
1158 				pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
1159 
1160 				xmitframe_plist = get_next(xmitframe_plist);
1161 
1162 				list_del_init(&pxmitframe->list);
1163 
1164 				psta->sleepq_len--;
1165 
1166 				if (psta->sleepq_len > 0)
1167 					pxmitframe->attrib.mdata = 1;
1168 				else
1169 					pxmitframe->attrib.mdata = 0;
1170 
1171 				pxmitframe->attrib.triggered = 1;
1172 
1173 				/* DBG_871X("handling ps-poll, q_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
1174 
1175 				rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
1176 
1177 				if (psta->sleepq_len == 0) {
1178 					pstapriv->tim_bitmap &= ~BIT(psta->aid);
1179 
1180 					/* DBG_871X("after handling ps-poll, tim =%x\n", pstapriv->tim_bitmap); */
1181 
1182 					/* upate BCN for TIM IE */
1183 					/* update_BCNTIM(padapter); */
1184 					update_beacon(padapter, _TIM_IE_, NULL, true);
1185 				}
1186 
1187 				/* spin_unlock_bh(&psta->sleep_q.lock); */
1188 				spin_unlock_bh(&pxmitpriv->lock);
1189 
1190 			} else{
1191 				/* spin_unlock_bh(&psta->sleep_q.lock); */
1192 				spin_unlock_bh(&pxmitpriv->lock);
1193 
1194 				/* DBG_871X("no buffered packets to xmit\n"); */
1195 				if (pstapriv->tim_bitmap&BIT(psta->aid)) {
1196 					if (psta->sleepq_len == 0) {
1197 						DBG_871X("no buffered packets to xmit\n");
1198 
1199 						/* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
1200 						issue_nulldata_in_interrupt(padapter, psta->hwaddr);
1201 					} else{
1202 						DBG_871X("error!psta->sleepq_len =%d\n", psta->sleepq_len);
1203 						psta->sleepq_len = 0;
1204 					}
1205 
1206 					pstapriv->tim_bitmap &= ~BIT(psta->aid);
1207 
1208 					/* upate BCN for TIM IE */
1209 					/* update_BCNTIM(padapter); */
1210 					update_beacon(padapter, _TIM_IE_, NULL, true);
1211 				}
1212 			}
1213 		}
1214 	}
1215 
1216 	return _FAIL;
1217 
1218 }
1219 
1220 union recv_frame *recvframe_chk_defrag(struct adapter *padapter, union recv_frame *precv_frame);
1221 sint validate_recv_mgnt_frame(struct adapter *padapter, union recv_frame *precv_frame);
validate_recv_mgnt_frame(struct adapter * padapter,union recv_frame * precv_frame)1222 sint validate_recv_mgnt_frame(struct adapter *padapter, union recv_frame *precv_frame)
1223 {
1224 	/* struct mlme_priv *pmlmepriv = &adapter->mlmepriv; */
1225 
1226 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+validate_recv_mgnt_frame\n"));
1227 
1228 	precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1229 	if (precv_frame == NULL) {
1230 		RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("%s: fragment packet\n", __func__));
1231 		return _SUCCESS;
1232 	}
1233 
1234 	{
1235 		/* for rx pkt statistics */
1236 		struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(precv_frame->u.hdr.rx_data));
1237 		if (psta) {
1238 			psta->sta_stats.rx_mgnt_pkts++;
1239 			if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_BEACON)
1240 				psta->sta_stats.rx_beacon_pkts++;
1241 			else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBEREQ)
1242 				psta->sta_stats.rx_probereq_pkts++;
1243 			else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBERSP) {
1244 				if (!memcmp(padapter->eeprompriv.mac_addr, GetAddr1Ptr(precv_frame->u.hdr.rx_data), ETH_ALEN))
1245 					psta->sta_stats.rx_probersp_pkts++;
1246 				else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data))
1247 					|| is_multicast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data)))
1248 					psta->sta_stats.rx_probersp_bm_pkts++;
1249 				else
1250 					psta->sta_stats.rx_probersp_uo_pkts++;
1251 			}
1252 		}
1253 	}
1254 
1255 	mgt_dispatcher(padapter, precv_frame);
1256 
1257 	return _SUCCESS;
1258 
1259 }
1260 
1261 sint validate_recv_data_frame(struct adapter *adapter, union recv_frame *precv_frame);
validate_recv_data_frame(struct adapter * adapter,union recv_frame * precv_frame)1262 sint validate_recv_data_frame(struct adapter *adapter, union recv_frame *precv_frame)
1263 {
1264 	u8 bretry;
1265 	u8 *psa, *pda, *pbssid;
1266 	struct sta_info *psta = NULL;
1267 	u8 *ptr = precv_frame->u.hdr.rx_data;
1268 	struct rx_pkt_attrib	*pattrib = &precv_frame->u.hdr.attrib;
1269 	struct security_priv *psecuritypriv = &adapter->securitypriv;
1270 	sint ret = _SUCCESS;
1271 
1272 	bretry = GetRetry(ptr);
1273 	pda = get_da(ptr);
1274 	psa = get_sa(ptr);
1275 	pbssid = get_hdr_bssid(ptr);
1276 
1277 	if (pbssid == NULL) {
1278 		#ifdef DBG_RX_DROP_FRAME
1279 		DBG_871X("DBG_RX_DROP_FRAME %s pbssid == NULL\n", __func__);
1280 		#endif
1281 		ret = _FAIL;
1282 		goto exit;
1283 	}
1284 
1285 	memcpy(pattrib->dst, pda, ETH_ALEN);
1286 	memcpy(pattrib->src, psa, ETH_ALEN);
1287 
1288 	memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1289 
1290 	switch (pattrib->to_fr_ds) {
1291 	case 0:
1292 		memcpy(pattrib->ra, pda, ETH_ALEN);
1293 		memcpy(pattrib->ta, psa, ETH_ALEN);
1294 		ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1295 		break;
1296 
1297 	case 1:
1298 		memcpy(pattrib->ra, pda, ETH_ALEN);
1299 		memcpy(pattrib->ta, pbssid, ETH_ALEN);
1300 		ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1301 		break;
1302 
1303 	case 2:
1304 		memcpy(pattrib->ra, pbssid, ETH_ALEN);
1305 		memcpy(pattrib->ta, psa, ETH_ALEN);
1306 		ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1307 		break;
1308 
1309 	case 3:
1310 		memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1311 		memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1312 		ret = _FAIL;
1313 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" case 3\n"));
1314 		break;
1315 
1316 	default:
1317 		ret = _FAIL;
1318 		break;
1319 
1320 	}
1321 
1322 	if (ret == _FAIL) {
1323 		#ifdef DBG_RX_DROP_FRAME
1324 		DBG_871X("DBG_RX_DROP_FRAME %s case:%d, res:%d\n", __func__, pattrib->to_fr_ds, ret);
1325 		#endif
1326 		goto exit;
1327 	} else if (ret == RTW_RX_HANDLED) {
1328 		goto exit;
1329 	}
1330 
1331 
1332 	if (psta == NULL) {
1333 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" after to_fr_ds_chk; psta == NULL\n"));
1334 		#ifdef DBG_RX_DROP_FRAME
1335 		DBG_871X("DBG_RX_DROP_FRAME %s psta == NULL\n", __func__);
1336 		#endif
1337 		ret = _FAIL;
1338 		goto exit;
1339 	}
1340 
1341 	/* psta->rssi = prxcmd->rssi; */
1342 	/* psta->signal_quality = prxcmd->sq; */
1343 	precv_frame->u.hdr.psta = psta;
1344 
1345 
1346 	pattrib->amsdu = 0;
1347 	pattrib->ack_policy = 0;
1348 	/* parsing QC field */
1349 	if (pattrib->qos == 1) {
1350 		pattrib->priority = GetPriority((ptr + 24));
1351 		pattrib->ack_policy = GetAckpolicy((ptr + 24));
1352 		pattrib->amsdu = GetAMsdu((ptr + 24));
1353 		pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
1354 
1355 		if (pattrib->priority != 0 && pattrib->priority != 3)
1356 			adapter->recvpriv.bIsAnyNonBEPkts = true;
1357 
1358 	} else{
1359 		pattrib->priority = 0;
1360 		pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 30 : 24;
1361 	}
1362 
1363 
1364 	if (pattrib->order)/* HT-CTRL 11n */
1365 		pattrib->hdrlen += 4;
1366 
1367 	precv_frame->u.hdr.preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1368 
1369 	/*  decache, drop duplicate recv packets */
1370 	if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL) {
1371 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decache : drop pkt\n"));
1372 		#ifdef DBG_RX_DROP_FRAME
1373 		DBG_871X("DBG_RX_DROP_FRAME %s recv_decache return _FAIL\n", __func__);
1374 		#endif
1375 		ret = _FAIL;
1376 		goto exit;
1377 	}
1378 
1379 	if (pattrib->privacy) {
1380 
1381 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("validate_recv_data_frame:pattrib->privacy =%x\n", pattrib->privacy));
1382 		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)));
1383 
1384 		GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
1385 
1386 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n pattrib->encrypt =%d\n", pattrib->encrypt));
1387 
1388 		SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1389 	} else{
1390 		pattrib->encrypt = 0;
1391 		pattrib->iv_len = pattrib->icv_len = 0;
1392 	}
1393 
1394 exit:
1395 	return ret;
1396 }
1397 
validate_80211w_mgmt(struct adapter * adapter,union recv_frame * precv_frame)1398 static sint validate_80211w_mgmt(struct adapter *adapter, union recv_frame *precv_frame)
1399 {
1400 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1401 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1402 	u8 *ptr = precv_frame->u.hdr.rx_data;
1403 	u8 type;
1404 	u8 subtype;
1405 
1406 	type =  GetFrameType(ptr);
1407 	subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1408 
1409 	/* only support station mode */
1410 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED)
1411 		&& adapter->securitypriv.binstallBIPkey == true) {
1412 		/* unicast management frame decrypt */
1413 		if (pattrib->privacy && !(IS_MCAST(GetAddr1Ptr(ptr))) &&
1414 			(subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC || subtype == WIFI_ACTION)) {
1415 			u8 *ppp, *mgmt_DATA;
1416 			u32 data_len = 0;
1417 			ppp = GetAddr2Ptr(ptr);
1418 
1419 			pattrib->bdecrypted = 0;
1420 			pattrib->encrypt = _AES_;
1421 			pattrib->hdrlen = sizeof(struct ieee80211_hdr_3addr);
1422 			/* set iv and icv length */
1423 			SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1424 			memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1425 			memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1426 			/* actual management data frame body */
1427 			data_len = pattrib->pkt_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
1428 			mgmt_DATA = rtw_zmalloc(data_len);
1429 			if (mgmt_DATA == NULL) {
1430 				DBG_871X("%s mgmt allocate fail  !!!!!!!!!\n", __func__);
1431 				goto validate_80211w_fail;
1432 			}
1433 			precv_frame = decryptor(adapter, precv_frame);
1434 			/* save actual management data frame body */
1435 			memcpy(mgmt_DATA, ptr+pattrib->hdrlen+pattrib->iv_len, data_len);
1436 			/* overwrite the iv field */
1437 			memcpy(ptr+pattrib->hdrlen, mgmt_DATA, data_len);
1438 			/* remove the iv and icv length */
1439 			pattrib->pkt_len = pattrib->pkt_len - pattrib->iv_len - pattrib->icv_len;
1440 			kfree(mgmt_DATA);
1441 			if (!precv_frame) {
1442 				DBG_871X("%s mgmt descrypt fail  !!!!!!!!!\n", __func__);
1443 				goto validate_80211w_fail;
1444 			}
1445 		} else if (IS_MCAST(GetAddr1Ptr(ptr)) &&
1446 			(subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC)) {
1447 			sint BIP_ret = _SUCCESS;
1448 			/* verify BIP MME IE of broadcast/multicast de-auth/disassoc packet */
1449 			BIP_ret = rtw_BIP_verify(adapter, (u8 *)precv_frame);
1450 			if (BIP_ret == _FAIL) {
1451 				/* DBG_871X("802.11w BIP verify fail\n"); */
1452 				goto validate_80211w_fail;
1453 			} else if (BIP_ret == RTW_RX_HANDLED) {
1454 				/* DBG_871X("802.11w recv none protected packet\n"); */
1455 				/* issue sa query request */
1456 				issue_action_SA_Query(adapter, NULL, 0, 0);
1457 				goto validate_80211w_fail;
1458 			}
1459 		} else { /* 802.11w protect */
1460 			if (subtype == WIFI_ACTION) {
1461 				/* according 802.11-2012 standard, these five types are not robust types */
1462 				if (ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_PUBLIC          &&
1463 					ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_HT              &&
1464 					ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_UNPROTECTED_WNM &&
1465 					ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_SELF_PROTECTED  &&
1466 					ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_P2P) {
1467 					DBG_871X("action frame category =%d should robust\n", ptr[WLAN_HDR_A3_LEN]);
1468 					goto validate_80211w_fail;
1469 				}
1470 			} else if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC) {
1471 				DBG_871X("802.11w recv none protected packet\n");
1472 				/* issue sa query request */
1473 				issue_action_SA_Query(adapter, NULL, 0, 0);
1474 				goto validate_80211w_fail;
1475 			}
1476 		}
1477 	}
1478 	return _SUCCESS;
1479 
1480 validate_80211w_fail:
1481 	return _FAIL;
1482 
1483 }
1484 
dump_rx_packet(u8 * ptr)1485 static inline void dump_rx_packet(u8 *ptr)
1486 {
1487 	int i;
1488 
1489 	DBG_871X("#############################\n");
1490 	for (i = 0; i < 64; i = i+8)
1491 		DBG_871X("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1492 		*(ptr+i+1), *(ptr+i+2), *(ptr+i+3), *(ptr+i+4), *(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1493 	DBG_871X("#############################\n");
1494 }
1495 
1496 sint validate_recv_frame(struct adapter *adapter, union recv_frame *precv_frame);
validate_recv_frame(struct adapter * adapter,union recv_frame * precv_frame)1497 sint validate_recv_frame(struct adapter *adapter, union recv_frame *precv_frame)
1498 {
1499 	/* shall check frame subtype, to / from ds, da, bssid */
1500 
1501 	/* then call check if rx seq/frag. duplicated. */
1502 
1503 	u8 type;
1504 	u8 subtype;
1505 	sint retval = _SUCCESS;
1506 	u8 bDumpRxPkt;
1507 
1508 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1509 
1510 	u8 *ptr = precv_frame->u.hdr.rx_data;
1511 	u8  ver = (unsigned char) (*ptr)&0x3;
1512 
1513 	/* add version chk */
1514 	if (ver != 0) {
1515 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! (ver!= 0)\n"));
1516 		retval = _FAIL;
1517 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_ver_err);
1518 		goto exit;
1519 	}
1520 
1521 	type =  GetFrameType(ptr);
1522 	subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1523 
1524 	pattrib->to_fr_ds = get_tofr_ds(ptr);
1525 
1526 	pattrib->frag_num = GetFragNum(ptr);
1527 	pattrib->seq_num = GetSequence(ptr);
1528 
1529 	pattrib->pw_save = GetPwrMgt(ptr);
1530 	pattrib->mfrag = GetMFrag(ptr);
1531 	pattrib->mdata = GetMData(ptr);
1532 	pattrib->privacy = GetPrivacy(ptr);
1533 	pattrib->order = GetOrder(ptr);
1534 	rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1535 	if (bDumpRxPkt == 1) /* dump all rx packets */
1536 		dump_rx_packet(ptr);
1537 	else if ((bDumpRxPkt == 2) && (type == WIFI_MGT_TYPE))
1538 		dump_rx_packet(ptr);
1539 	else if ((bDumpRxPkt == 3) && (type == WIFI_DATA_TYPE))
1540 		dump_rx_packet(ptr);
1541 
1542 	switch (type) {
1543 	case WIFI_MGT_TYPE: /* mgnt */
1544 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt);
1545 		if (validate_80211w_mgmt(adapter, precv_frame) == _FAIL) {
1546 			retval = _FAIL;
1547 			DBG_COUNTER(padapter->rx_logs.core_rx_pre_mgmt_err_80211w);
1548 			break;
1549 		}
1550 
1551 		retval = validate_recv_mgnt_frame(adapter, precv_frame);
1552 		if (retval == _FAIL) {
1553 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_mgnt_frame fail\n"));
1554 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt_err);
1555 		}
1556 		retval = _FAIL; /*  only data frame return _SUCCESS */
1557 		break;
1558 	case WIFI_CTRL_TYPE: /* ctrl */
1559 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl);
1560 		retval = validate_recv_ctrl_frame(adapter, precv_frame);
1561 		if (retval == _FAIL) {
1562 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_ctrl_frame fail\n"));
1563 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl_err);
1564 		}
1565 		retval = _FAIL; /*  only data frame return _SUCCESS */
1566 		break;
1567 	case WIFI_DATA_TYPE: /* data */
1568 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_data);
1569 
1570 		pattrib->qos = (subtype & BIT(7)) ? 1:0;
1571 		retval = validate_recv_data_frame(adapter, precv_frame);
1572 		if (retval == _FAIL) {
1573 			struct recv_priv *precvpriv = &adapter->recvpriv;
1574 			/* RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail\n")); */
1575 			precvpriv->rx_drop++;
1576 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_err);
1577 		} else if (retval == _SUCCESS) {
1578 #ifdef DBG_RX_DUMP_EAP
1579 			u8 bDumpRxPkt;
1580 			u16 eth_type;
1581 
1582 			/*  dump eapol */
1583 			rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1584 			/*  get ether_type */
1585 			memcpy(&eth_type, ptr + pattrib->hdrlen + pattrib->iv_len + LLC_HEADER_SIZE, 2);
1586 			eth_type = ntohs((unsigned short) eth_type);
1587 			if ((bDumpRxPkt == 4) && (eth_type == 0x888e))
1588 				dump_rx_packet(ptr);
1589 #endif
1590 		} else
1591 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_handled);
1592 		break;
1593 	default:
1594 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_unknown);
1595 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! type = 0x%x\n", type));
1596 		#ifdef DBG_RX_DROP_FRAME
1597 		DBG_871X("DBG_RX_DROP_FRAME validate_recv_data_frame fail! type = 0x%x\n", type);
1598 		#endif
1599 		retval = _FAIL;
1600 		break;
1601 	}
1602 
1603 exit:
1604 	return retval;
1605 }
1606 
1607 
1608 /* remove the wlanhdr and add the eth_hdr */
1609 sint wlanhdr_to_ethhdr(union recv_frame *precvframe);
wlanhdr_to_ethhdr(union recv_frame * precvframe)1610 sint wlanhdr_to_ethhdr(union recv_frame *precvframe)
1611 {
1612 	sint	rmv_len;
1613 	u16 eth_type, len;
1614 	u8 bsnaphdr;
1615 	u8 *psnap_type;
1616 	struct ieee80211_snap_hdr	*psnap;
1617 	__be16 be_tmp;
1618 	sint ret = _SUCCESS;
1619 	struct adapter			*adapter = precvframe->u.hdr.adapter;
1620 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1621 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
1622 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
1623 
1624 	if (pattrib->encrypt) {
1625 		recvframe_pull_tail(precvframe, pattrib->icv_len);
1626 	}
1627 
1628 	psnap = (struct ieee80211_snap_hdr	*)(ptr+pattrib->hdrlen + pattrib->iv_len);
1629 	psnap_type = ptr+pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
1630 	/* convert hdr + possible LLC headers into Ethernet header */
1631 	/* eth_type = (psnap_type[0] << 8) | psnap_type[1]; */
1632 	if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
1633 		(memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2)) &&
1634 		(memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
1635 		/* eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) || */
1636 		 !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
1637 		/* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1638 		bsnaphdr = true;
1639 	} else
1640 		/* Leave Ethernet header part of hdr and full payload */
1641 		bsnaphdr = false;
1642 
1643 	rmv_len = pattrib->hdrlen + pattrib->iv_len + (bsnaphdr?SNAP_SIZE:0);
1644 	len = precvframe->u.hdr.len - rmv_len;
1645 
1646 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n ===pattrib->hdrlen: %x,  pattrib->iv_len:%x ===\n\n", pattrib->hdrlen,  pattrib->iv_len));
1647 
1648 	memcpy(&be_tmp, ptr+rmv_len, 2);
1649 	eth_type = ntohs(be_tmp); /* pattrib->ether_type */
1650 	pattrib->eth_type = eth_type;
1651 
1652 #ifdef CONFIG_AUTO_AP_MODE
1653 	if (0x8899 == pattrib->eth_type) {
1654 		struct sta_info *psta = precvframe->u.hdr.psta;
1655 
1656 		DBG_871X("wlan rx: got eth_type = 0x%x\n", pattrib->eth_type);
1657 
1658 		if (psta && psta->isrc && psta->pid > 0) {
1659 			u16 rx_pid;
1660 
1661 			rx_pid = *(u16 *)(ptr+rmv_len+2);
1662 
1663 			DBG_871X("wlan rx(pid = 0x%x): sta("MAC_FMT") pid = 0x%x\n",
1664 				rx_pid, MAC_ARG(psta->hwaddr), psta->pid);
1665 
1666 			if (rx_pid == psta->pid) {
1667 				int i;
1668 				u16 len = *(u16 *)(ptr+rmv_len+4);
1669 				/* u16 ctrl_type = *(u16*)(ptr+rmv_len+6); */
1670 
1671 				/* DBG_871X("RC: len = 0x%x, ctrl_type = 0x%x\n", len, ctrl_type); */
1672 				DBG_871X("RC: len = 0x%x\n", len);
1673 
1674 				for (i = 0; i < len ; i++)
1675 					DBG_871X("0x%x\n", *(ptr+rmv_len+6+i));
1676 					/* DBG_871X("0x%x\n", *(ptr+rmv_len+8+i)); */
1677 
1678 				DBG_871X("RC-end\n");
1679 			}
1680 		}
1681 	}
1682 #endif /* CONFIG_AUTO_AP_MODE */
1683 
1684 	if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)) {
1685 		ptr += rmv_len;
1686 		*ptr = 0x87;
1687 		*(ptr+1) = 0x12;
1688 
1689 		eth_type = 0x8712;
1690 		/*  append rx status for mp test packets */
1691 		ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr)+2)-24);
1692 		memcpy(ptr, get_rxmem(precvframe), 24);
1693 		ptr += 24;
1694 	} else
1695 		ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr) + (bsnaphdr?2:0)));
1696 
1697 	memcpy(ptr, pattrib->dst, ETH_ALEN);
1698 	memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
1699 
1700 	if (!bsnaphdr) {
1701 		be_tmp = htons(len);
1702 		memcpy(ptr+12, &be_tmp, 2);
1703 	}
1704 
1705 	return ret;
1706 }
1707 
1708 /* perform defrag */
recvframe_defrag(struct adapter * adapter,struct __queue * defrag_q)1709 static union recv_frame *recvframe_defrag(struct adapter *adapter,
1710 					  struct __queue *defrag_q)
1711 {
1712 	struct list_head	 *plist, *phead;
1713 	u8 *data, wlanhdr_offset;
1714 	u8 curfragnum;
1715 	struct recv_frame_hdr *pfhdr, *pnfhdr;
1716 	union recv_frame *prframe, *pnextrframe;
1717 	struct __queue	*pfree_recv_queue;
1718 
1719 	curfragnum = 0;
1720 	pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
1721 
1722 	phead = get_list_head(defrag_q);
1723 	plist = get_next(phead);
1724 	prframe = (union recv_frame *)plist;
1725 	pfhdr = &prframe->u.hdr;
1726 	list_del_init(&(prframe->u.list));
1727 
1728 	if (curfragnum != pfhdr->attrib.frag_num) {
1729 		/* the first fragment number must be 0 */
1730 		/* free the whole queue */
1731 		rtw_free_recvframe(prframe, pfree_recv_queue);
1732 		rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1733 
1734 		return NULL;
1735 	}
1736 
1737 	curfragnum++;
1738 
1739 	plist = get_list_head(defrag_q);
1740 
1741 	plist = get_next(plist);
1742 
1743 	data = get_recvframe_data(prframe);
1744 
1745 	while (phead != plist) {
1746 		pnextrframe = (union recv_frame *)plist;
1747 		pnfhdr = &pnextrframe->u.hdr;
1748 
1749 
1750 		/* check the fragment sequence  (2nd ~n fragment frame) */
1751 
1752 		if (curfragnum != pnfhdr->attrib.frag_num) {
1753 			/* the fragment number must be increasing  (after decache) */
1754 			/* release the defrag_q & prframe */
1755 			rtw_free_recvframe(prframe, pfree_recv_queue);
1756 			rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1757 			return NULL;
1758 		}
1759 
1760 		curfragnum++;
1761 
1762 		/* copy the 2nd~n fragment frame's payload to the first fragment */
1763 		/* get the 2nd~last fragment frame's payload */
1764 
1765 		wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
1766 
1767 		recvframe_pull(pnextrframe, wlanhdr_offset);
1768 
1769 		/* append  to first fragment frame's tail (if privacy frame, pull the ICV) */
1770 		recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
1771 
1772 		/* memcpy */
1773 		memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
1774 
1775 		recvframe_put(prframe, pnfhdr->len);
1776 
1777 		pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
1778 		plist = get_next(plist);
1779 
1780 	}
1781 
1782 	/* free the defrag_q queue and return the prframe */
1783 	rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1784 
1785 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Performance defrag!!!!!\n"));
1786 
1787 	return prframe;
1788 }
1789 
1790 /* check if need to defrag, if needed queue the frame to defrag_q */
recvframe_chk_defrag(struct adapter * padapter,union recv_frame * precv_frame)1791 union recv_frame *recvframe_chk_defrag(struct adapter *padapter, union recv_frame *precv_frame)
1792 {
1793 	u8 ismfrag;
1794 	u8 fragnum;
1795 	u8 *psta_addr;
1796 	struct recv_frame_hdr *pfhdr;
1797 	struct sta_info *psta;
1798 	struct sta_priv *pstapriv;
1799 	struct list_head *phead;
1800 	union recv_frame *prtnframe = NULL;
1801 	struct __queue *pfree_recv_queue, *pdefrag_q;
1802 
1803 	pstapriv = &padapter->stapriv;
1804 
1805 	pfhdr = &precv_frame->u.hdr;
1806 
1807 	pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1808 
1809 	/* need to define struct of wlan header frame ctrl */
1810 	ismfrag = pfhdr->attrib.mfrag;
1811 	fragnum = pfhdr->attrib.frag_num;
1812 
1813 	psta_addr = pfhdr->attrib.ta;
1814 	psta = rtw_get_stainfo(pstapriv, psta_addr);
1815 	if (psta == NULL) {
1816 		u8 type = GetFrameType(pfhdr->rx_data);
1817 		if (type != WIFI_DATA_TYPE) {
1818 			psta = rtw_get_bcmc_stainfo(padapter);
1819 			pdefrag_q = &psta->sta_recvpriv.defrag_q;
1820 		} else
1821 			pdefrag_q = NULL;
1822 	} else
1823 		pdefrag_q = &psta->sta_recvpriv.defrag_q;
1824 
1825 	if ((ismfrag == 0) && (fragnum == 0))
1826 		prtnframe = precv_frame;/* isn't a fragment frame */
1827 
1828 	if (ismfrag == 1) {
1829 		/* 0~(n-1) fragment frame */
1830 		/* enqueue to defraf_g */
1831 		if (pdefrag_q != NULL) {
1832 			if (fragnum == 0)
1833 				/* the first fragment */
1834 				if (!list_empty(&pdefrag_q->queue))
1835 					/* free current defrag_q */
1836 					rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
1837 
1838 
1839 			/* Then enqueue the 0~(n-1) fragment into the defrag_q */
1840 
1841 			/* spin_lock(&pdefrag_q->lock); */
1842 			phead = get_list_head(pdefrag_q);
1843 			list_add_tail(&pfhdr->list, phead);
1844 			/* spin_unlock(&pdefrag_q->lock); */
1845 
1846 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Enqueuq: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1847 
1848 			prtnframe = NULL;
1849 
1850 		} else{
1851 			/* can't find this ta's defrag_queue, so free this recv_frame */
1852 			rtw_free_recvframe(precv_frame, pfree_recv_queue);
1853 			prtnframe = NULL;
1854 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q == NULL: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1855 		}
1856 
1857 	}
1858 
1859 	if ((ismfrag == 0) && (fragnum != 0)) {
1860 		/* the last fragment frame */
1861 		/* enqueue the last fragment */
1862 		if (pdefrag_q != NULL) {
1863 			/* spin_lock(&pdefrag_q->lock); */
1864 			phead = get_list_head(pdefrag_q);
1865 			list_add_tail(&pfhdr->list, phead);
1866 			/* spin_unlock(&pdefrag_q->lock); */
1867 
1868 			/* call recvframe_defrag to defrag */
1869 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("defrag: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1870 			precv_frame = recvframe_defrag(padapter, pdefrag_q);
1871 			prtnframe = precv_frame;
1872 
1873 		} else{
1874 			/* can't find this ta's defrag_queue, so free this recv_frame */
1875 			rtw_free_recvframe(precv_frame, pfree_recv_queue);
1876 			prtnframe = NULL;
1877 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q == NULL: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1878 		}
1879 
1880 	}
1881 
1882 
1883 	if ((prtnframe != NULL) && (prtnframe->u.hdr.attrib.privacy)) {
1884 		/* after defrag we must check tkip mic code */
1885 		if (recvframe_chkmic(padapter,  prtnframe) == _FAIL) {
1886 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic(padapter,  prtnframe) == _FAIL\n"));
1887 			rtw_free_recvframe(prtnframe, pfree_recv_queue);
1888 			prtnframe = NULL;
1889 		}
1890 	}
1891 	return prtnframe;
1892 }
1893 
amsdu_to_msdu(struct adapter * padapter,union recv_frame * prframe)1894 static int amsdu_to_msdu(struct adapter *padapter, union recv_frame *prframe)
1895 {
1896 	int	a_len, padding_len;
1897 	u16 nSubframe_Length;
1898 	u8 nr_subframes, i;
1899 	u8 *pdata;
1900 	_pkt *sub_pkt, *subframes[MAX_SUBFRAME_COUNT];
1901 	struct recv_priv *precvpriv = &padapter->recvpriv;
1902 	struct __queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
1903 	int	ret = _SUCCESS;
1904 
1905 	nr_subframes = 0;
1906 
1907 	recvframe_pull(prframe, prframe->u.hdr.attrib.hdrlen);
1908 
1909 	if (prframe->u.hdr.attrib.iv_len > 0)
1910 		recvframe_pull(prframe, prframe->u.hdr.attrib.iv_len);
1911 
1912 	a_len = prframe->u.hdr.len;
1913 
1914 	pdata = prframe->u.hdr.rx_data;
1915 
1916 	while (a_len > ETH_HLEN) {
1917 
1918 		/* Offset 12 denote 2 mac address */
1919 		nSubframe_Length = RTW_GET_BE16(pdata + 12);
1920 
1921 		if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1922 			DBG_871X("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
1923 			break;
1924 		}
1925 
1926 		sub_pkt = rtw_os_alloc_msdu_pkt(prframe, nSubframe_Length, pdata);
1927 		if (sub_pkt == NULL) {
1928 			DBG_871X("%s(): allocate sub packet fail !!!\n", __func__);
1929 			break;
1930 		}
1931 
1932 		/* move the data point to data content */
1933 		pdata += ETH_HLEN;
1934 		a_len -= ETH_HLEN;
1935 
1936 		subframes[nr_subframes++] = sub_pkt;
1937 
1938 		if (nr_subframes >= MAX_SUBFRAME_COUNT) {
1939 			DBG_871X("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1940 			break;
1941 		}
1942 
1943 		pdata += nSubframe_Length;
1944 		a_len -= nSubframe_Length;
1945 		if (a_len != 0) {
1946 			padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4-1));
1947 			if (padding_len == 4) {
1948 				padding_len = 0;
1949 			}
1950 
1951 			if (a_len < padding_len) {
1952 				DBG_871X("ParseSubframe(): a_len < padding_len !\n");
1953 				break;
1954 			}
1955 			pdata += padding_len;
1956 			a_len -= padding_len;
1957 		}
1958 	}
1959 
1960 	for (i = 0; i < nr_subframes; i++) {
1961 		sub_pkt = subframes[i];
1962 
1963 		/* Indicat the packets to upper layer */
1964 		if (sub_pkt) {
1965 			rtw_os_recv_indicate_pkt(padapter, sub_pkt, &prframe->u.hdr.attrib);
1966 		}
1967 	}
1968 
1969 	prframe->u.hdr.len = 0;
1970 	rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
1971 
1972 	return ret;
1973 }
1974 
1975 int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num);
check_indicate_seq(struct recv_reorder_ctrl * preorder_ctrl,u16 seq_num)1976 int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
1977 {
1978 	struct adapter *padapter = preorder_ctrl->padapter;
1979 	struct dvobj_priv *psdpriv = padapter->dvobj;
1980 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1981 	u8 wsize = preorder_ctrl->wsize_b;
1982 	u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/*  4096; */
1983 
1984 	/*  Rx Reorder initialize condition. */
1985 	if (preorder_ctrl->indicate_seq == 0xFFFF) {
1986 		preorder_ctrl->indicate_seq = seq_num;
1987 		#ifdef DBG_RX_SEQ
1988 		DBG_871X("DBG_RX_SEQ %s:%d init IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
1989 			preorder_ctrl->indicate_seq, seq_num);
1990 		#endif
1991 
1992 		/* DbgPrint("check_indicate_seq, 1st->indicate_seq =%d\n", precvpriv->indicate_seq); */
1993 	}
1994 
1995 	/* DbgPrint("enter->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
1996 
1997 	/*  Drop out the packet which SeqNum is smaller than WinStart */
1998 	if (SN_LESS(seq_num, preorder_ctrl->indicate_seq)) {
1999 		/* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2000 		/* DbgPrint("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2001 
2002 		#ifdef DBG_RX_DROP_FRAME
2003 		DBG_871X("%s IndicateSeq: %d > NewSeq: %d\n", __func__,
2004 			preorder_ctrl->indicate_seq, seq_num);
2005 		#endif
2006 
2007 
2008 		return false;
2009 	}
2010 
2011 	/*  */
2012 	/*  Sliding window manipulation. Conditions includes: */
2013 	/*  1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
2014 	/*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
2015 	/*  */
2016 	if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
2017 		preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2018 
2019 		#ifdef DBG_RX_SEQ
2020 		DBG_871X("DBG_RX_SEQ %s:%d SN_EQUAL IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2021 			preorder_ctrl->indicate_seq, seq_num);
2022 		#endif
2023 	} else if (SN_LESS(wend, seq_num)) {
2024 		/* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2025 		/* DbgPrint("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2026 
2027 		/*  boundary situation, when seq_num cross 0xFFF */
2028 		if (seq_num >= (wsize - 1))
2029 			preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
2030 		else
2031 			preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
2032 		pdbgpriv->dbg_rx_ampdu_window_shift_cnt++;
2033 		#ifdef DBG_RX_SEQ
2034 		DBG_871X("DBG_RX_SEQ %s:%d SN_LESS(wend, seq_num) IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2035 			preorder_ctrl->indicate_seq, seq_num);
2036 		#endif
2037 	}
2038 
2039 	/* DbgPrint("exit->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2040 
2041 	return true;
2042 }
2043 
2044 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe);
enqueue_reorder_recvframe(struct recv_reorder_ctrl * preorder_ctrl,union recv_frame * prframe)2045 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe)
2046 {
2047 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2048 	struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2049 	struct list_head	*phead, *plist;
2050 	union recv_frame *pnextrframe;
2051 	struct rx_pkt_attrib *pnextattrib;
2052 
2053 	/* DbgPrint("+enqueue_reorder_recvframe()\n"); */
2054 
2055 	/* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2056 	/* spin_lock(&ppending_recvframe_queue->lock); */
2057 
2058 
2059 	phead = get_list_head(ppending_recvframe_queue);
2060 	plist = get_next(phead);
2061 
2062 	while (phead != plist) {
2063 		pnextrframe = (union recv_frame *)plist;
2064 		pnextattrib = &pnextrframe->u.hdr.attrib;
2065 
2066 		if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
2067 			plist = get_next(plist);
2068 		else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
2069 			/* Duplicate entry is found!! Do not insert current entry. */
2070 			/* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Duplicate packet is dropped!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2071 			/* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2072 			return false;
2073 		else
2074 			break;
2075 
2076 		/* DbgPrint("enqueue_reorder_recvframe():while\n"); */
2077 
2078 	}
2079 
2080 
2081 	/* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2082 	/* spin_lock(&ppending_recvframe_queue->lock); */
2083 
2084 	list_del_init(&(prframe->u.hdr.list));
2085 
2086 	list_add_tail(&(prframe->u.hdr.list), plist);
2087 
2088 	/* spin_unlock(&ppending_recvframe_queue->lock); */
2089 	/* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2090 
2091 
2092 	/* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Pkt insert into buffer!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2093 	return true;
2094 
2095 }
2096 
2097 void recv_indicatepkts_pkt_loss_cnt(struct debug_priv *pdbgpriv, u64 prev_seq, u64 current_seq);
recv_indicatepkts_pkt_loss_cnt(struct debug_priv * pdbgpriv,u64 prev_seq,u64 current_seq)2098 void recv_indicatepkts_pkt_loss_cnt(struct debug_priv *pdbgpriv, u64 prev_seq, u64 current_seq)
2099 {
2100 	if (current_seq < prev_seq)
2101 		pdbgpriv->dbg_rx_ampdu_loss_count += (4096 + current_seq - prev_seq);
2102 	else
2103 		pdbgpriv->dbg_rx_ampdu_loss_count += (current_seq - prev_seq);
2104 
2105 }
2106 int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced);
recv_indicatepkts_in_order(struct adapter * padapter,struct recv_reorder_ctrl * preorder_ctrl,int bforced)2107 int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
2108 {
2109 	struct list_head	*phead, *plist;
2110 	union recv_frame *prframe;
2111 	struct rx_pkt_attrib *pattrib;
2112 	/* u8 index = 0; */
2113 	int bPktInBuf = false;
2114 	struct recv_priv *precvpriv = &padapter->recvpriv;
2115 	struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2116 	struct dvobj_priv *psdpriv = padapter->dvobj;
2117 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
2118 
2119 	DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_in_oder);
2120 
2121 	/* DbgPrint("+recv_indicatepkts_in_order\n"); */
2122 
2123 	/* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2124 	/* spin_lock(&ppending_recvframe_queue->lock); */
2125 
2126 	phead =		get_list_head(ppending_recvframe_queue);
2127 	plist = get_next(phead);
2128 
2129 	/*  Handling some condition for forced indicate case. */
2130 	if (bforced == true) {
2131 		pdbgpriv->dbg_rx_ampdu_forced_indicate_count++;
2132 		if (list_empty(phead)) {
2133 			/*  spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2134 			/* spin_unlock(&ppending_recvframe_queue->lock); */
2135 			return true;
2136 		}
2137 
2138 		prframe = (union recv_frame *)plist;
2139 		pattrib = &prframe->u.hdr.attrib;
2140 
2141 		#ifdef DBG_RX_SEQ
2142 		DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2143 			preorder_ctrl->indicate_seq, pattrib->seq_num);
2144 		#endif
2145 		recv_indicatepkts_pkt_loss_cnt(pdbgpriv, preorder_ctrl->indicate_seq, pattrib->seq_num);
2146 		preorder_ctrl->indicate_seq = pattrib->seq_num;
2147 
2148 	}
2149 
2150 	/*  Prepare indication list and indication. */
2151 	/*  Check if there is any packet need indicate. */
2152 	while (!list_empty(phead)) {
2153 
2154 		prframe = (union recv_frame *)plist;
2155 		pattrib = &prframe->u.hdr.attrib;
2156 
2157 		if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
2158 			RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2159 				 ("recv_indicatepkts_in_order: indicate =%d seq =%d amsdu =%d\n",
2160 				  preorder_ctrl->indicate_seq, pattrib->seq_num, pattrib->amsdu));
2161 
2162 			plist = get_next(plist);
2163 			list_del_init(&(prframe->u.hdr.list));
2164 
2165 			if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
2166 				preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2167 				#ifdef DBG_RX_SEQ
2168 				DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2169 					preorder_ctrl->indicate_seq, pattrib->seq_num);
2170 				#endif
2171 			}
2172 
2173 			/* Set this as a lock to make sure that only one thread is indicating packet. */
2174 			/* pTS->RxIndicateState = RXTS_INDICATE_PROCESSING; */
2175 
2176 			/*  Indicate packets */
2177 			/* RT_ASSERT((index<=REORDER_WIN_SIZE), ("RxReorderIndicatePacket(): Rx Reorder buffer full!!\n")); */
2178 
2179 
2180 			/* indicate this recv_frame */
2181 			/* DbgPrint("recv_indicatepkts_in_order, indicate_seq =%d, seq_num =%d\n", precvpriv->indicate_seq, pattrib->seq_num); */
2182 			if (!pattrib->amsdu) {
2183 				/* DBG_871X("recv_indicatepkts_in_order, amsdu!= 1, indicate_seq =%d, seq_num =%d\n", preorder_ctrl->indicate_seq, pattrib->seq_num); */
2184 
2185 				if ((padapter->bDriverStopped == false) &&
2186 				    (padapter->bSurpriseRemoved == false))
2187 					rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
2188 
2189 			} else if (pattrib->amsdu == 1) {
2190 				if (amsdu_to_msdu(padapter, prframe) != _SUCCESS)
2191 					rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
2192 
2193 			} else{
2194 				/* error condition; */
2195 			}
2196 
2197 
2198 			/* Update local variables. */
2199 			bPktInBuf = false;
2200 
2201 		} else{
2202 			bPktInBuf = true;
2203 			break;
2204 		}
2205 
2206 		/* DbgPrint("recv_indicatepkts_in_order():while\n"); */
2207 
2208 	}
2209 
2210 	/* spin_unlock(&ppending_recvframe_queue->lock); */
2211 	/* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2212 
2213 	return bPktInBuf;
2214 }
2215 
2216 int recv_indicatepkt_reorder(struct adapter *padapter, union recv_frame *prframe);
recv_indicatepkt_reorder(struct adapter * padapter,union recv_frame * prframe)2217 int recv_indicatepkt_reorder(struct adapter *padapter, union recv_frame *prframe)
2218 {
2219 	int retval = _SUCCESS;
2220 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2221 	struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
2222 	struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2223 	struct dvobj_priv *psdpriv = padapter->dvobj;
2224 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
2225 
2226 	DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_reoder);
2227 
2228 	if (!pattrib->amsdu) {
2229 		/* s1. */
2230 		wlanhdr_to_ethhdr(prframe);
2231 
2232 		if (pattrib->qos != 1) {
2233 			if ((padapter->bDriverStopped == false) &&
2234 			    (padapter->bSurpriseRemoved == false)) {
2235 				RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@  recv_indicatepkt_reorder -recv_func recv_indicatepkt\n"));
2236 
2237 				rtw_recv_indicatepkt(padapter, prframe);
2238 				return _SUCCESS;
2239 
2240 			}
2241 
2242 			#ifdef DBG_RX_DROP_FRAME
2243 			DBG_871X("DBG_RX_DROP_FRAME %s pattrib->qos != 1\n", __func__);
2244 			#endif
2245 
2246 			return _FAIL;
2247 
2248 		}
2249 
2250 		if (preorder_ctrl->enable == false) {
2251 			/* indicate this recv_frame */
2252 			preorder_ctrl->indicate_seq = pattrib->seq_num;
2253 			#ifdef DBG_RX_SEQ
2254 			DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2255 				preorder_ctrl->indicate_seq, pattrib->seq_num);
2256 			#endif
2257 
2258 			rtw_recv_indicatepkt(padapter, prframe);
2259 
2260 			preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2261 			#ifdef DBG_RX_SEQ
2262 			DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2263 				preorder_ctrl->indicate_seq, pattrib->seq_num);
2264 			#endif
2265 
2266 			return _SUCCESS;
2267 		}
2268 	} else if (pattrib->amsdu == 1) { /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
2269 		if (preorder_ctrl->enable == false) {
2270 			preorder_ctrl->indicate_seq = pattrib->seq_num;
2271 			#ifdef DBG_RX_SEQ
2272 			DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2273 				preorder_ctrl->indicate_seq, pattrib->seq_num);
2274 			#endif
2275 
2276 			retval = amsdu_to_msdu(padapter, prframe);
2277 
2278 			preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2279 			#ifdef DBG_RX_SEQ
2280 			DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2281 				preorder_ctrl->indicate_seq, pattrib->seq_num);
2282 			#endif
2283 
2284 			if (retval != _SUCCESS) {
2285 				#ifdef DBG_RX_DROP_FRAME
2286 				DBG_871X("DBG_RX_DROP_FRAME %s amsdu_to_msdu fail\n", __func__);
2287 				#endif
2288 			}
2289 
2290 			return retval;
2291 		}
2292 	}
2293 
2294 	spin_lock_bh(&ppending_recvframe_queue->lock);
2295 
2296 	RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2297 		 ("recv_indicatepkt_reorder: indicate =%d seq =%d\n",
2298 		  preorder_ctrl->indicate_seq, pattrib->seq_num));
2299 
2300 	/* s2. check if winstart_b(indicate_seq) needs to been updated */
2301 	if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num)) {
2302 		pdbgpriv->dbg_rx_ampdu_drop_count++;
2303 		#ifdef DBG_RX_DROP_FRAME
2304 		DBG_871X("DBG_RX_DROP_FRAME %s check_indicate_seq fail\n", __func__);
2305 		#endif
2306 		goto _err_exit;
2307 	}
2308 
2309 
2310 	/* s3. Insert all packet into Reorder Queue to maintain its ordering. */
2311 	if (!enqueue_reorder_recvframe(preorder_ctrl, prframe)) {
2312 		/* DbgPrint("recv_indicatepkt_reorder, enqueue_reorder_recvframe fail!\n"); */
2313 		/* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2314 		/* return _FAIL; */
2315 		#ifdef DBG_RX_DROP_FRAME
2316 		DBG_871X("DBG_RX_DROP_FRAME %s enqueue_reorder_recvframe fail\n", __func__);
2317 		#endif
2318 		goto _err_exit;
2319 	}
2320 
2321 
2322 	/* s4. */
2323 	/*  Indication process. */
2324 	/*  After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
2325 	/*  with the SeqNum smaller than latest WinStart and buffer other packets. */
2326 	/*  */
2327 	/*  For Rx Reorder condition: */
2328 	/*  1. All packets with SeqNum smaller than WinStart => Indicate */
2329 	/*  2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
2330 	/*  */
2331 
2332 	/* recv_indicatepkts_in_order(padapter, preorder_ctrl, true); */
2333 	if (recv_indicatepkts_in_order(padapter, preorder_ctrl, false) == true) {
2334 		_set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2335 		spin_unlock_bh(&ppending_recvframe_queue->lock);
2336 	} else{
2337 		spin_unlock_bh(&ppending_recvframe_queue->lock);
2338 		del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
2339 	}
2340 
2341 	return _SUCCESS;
2342 
2343 _err_exit:
2344 	spin_unlock_bh(&ppending_recvframe_queue->lock);
2345 
2346 	return _FAIL;
2347 }
2348 
2349 
rtw_reordering_ctrl_timeout_handler(struct timer_list * t)2350 void rtw_reordering_ctrl_timeout_handler(struct timer_list *t)
2351 {
2352 	struct recv_reorder_ctrl *preorder_ctrl =
2353 		from_timer(preorder_ctrl, t, reordering_ctrl_timer);
2354 	struct adapter *padapter = preorder_ctrl->padapter;
2355 	struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2356 
2357 
2358 	if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
2359 		return;
2360 
2361 	/* DBG_871X("+rtw_reordering_ctrl_timeout_handler() =>\n"); */
2362 
2363 	spin_lock_bh(&ppending_recvframe_queue->lock);
2364 
2365 	if (recv_indicatepkts_in_order(padapter, preorder_ctrl, true) == true)
2366 		_set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2367 
2368 	spin_unlock_bh(&ppending_recvframe_queue->lock);
2369 
2370 }
2371 
2372 int process_recv_indicatepkts(struct adapter *padapter, union recv_frame *prframe);
process_recv_indicatepkts(struct adapter * padapter,union recv_frame * prframe)2373 int process_recv_indicatepkts(struct adapter *padapter, union recv_frame *prframe)
2374 {
2375 	int retval = _SUCCESS;
2376 	/* struct recv_priv *precvpriv = &padapter->recvpriv; */
2377 	/* struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib; */
2378 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2379 	struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2380 
2381 	DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate);
2382 
2383 	if (phtpriv->ht_option == true) { /* B/G/N Mode */
2384 		/* prframe->u.hdr.preorder_ctrl = &precvpriv->recvreorder_ctrl[pattrib->priority]; */
2385 
2386 		if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) { /*  including perform A-MPDU Rx Ordering Buffer Control */
2387 			#ifdef DBG_RX_DROP_FRAME
2388 			DBG_871X("DBG_RX_DROP_FRAME %s recv_indicatepkt_reorder error!\n", __func__);
2389 			#endif
2390 
2391 			if ((padapter->bDriverStopped == false) &&
2392 			    (padapter->bSurpriseRemoved == false)) {
2393 				retval = _FAIL;
2394 				return retval;
2395 			}
2396 		}
2397 	} else { /* B/G mode */
2398 		retval = wlanhdr_to_ethhdr(prframe);
2399 		if (retval != _SUCCESS) {
2400 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("wlanhdr_to_ethhdr: drop pkt\n"));
2401 			#ifdef DBG_RX_DROP_FRAME
2402 			DBG_871X("DBG_RX_DROP_FRAME %s wlanhdr_to_ethhdr error!\n", __func__);
2403 			#endif
2404 			return retval;
2405 		}
2406 
2407 		if ((padapter->bDriverStopped == false) && (padapter->bSurpriseRemoved == false)) {
2408 			/* indicate this recv_frame */
2409 			RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func recv_indicatepkt\n"));
2410 			rtw_recv_indicatepkt(padapter, prframe);
2411 
2412 
2413 		} else{
2414 			RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func free_indicatepkt\n"));
2415 
2416 			RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
2417 			retval = _FAIL;
2418 			return retval;
2419 		}
2420 
2421 	}
2422 
2423 	return retval;
2424 
2425 }
2426 
recv_func_prehandle(struct adapter * padapter,union recv_frame * rframe)2427 static int recv_func_prehandle(struct adapter *padapter, union recv_frame *rframe)
2428 {
2429 	int ret = _SUCCESS;
2430 	struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2431 
2432 	DBG_COUNTER(padapter->rx_logs.core_rx_pre);
2433 
2434 	/* check the frame crtl field and decache */
2435 	ret = validate_recv_frame(padapter, rframe);
2436 	if (ret != _SUCCESS) {
2437 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recv_func: validate_recv_frame fail! drop pkt\n"));
2438 		rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
2439 		goto exit;
2440 	}
2441 
2442 exit:
2443 	return ret;
2444 }
2445 
recv_func_posthandle(struct adapter * padapter,union recv_frame * prframe)2446 static int recv_func_posthandle(struct adapter *padapter, union recv_frame *prframe)
2447 {
2448 	int ret = _SUCCESS;
2449 	union recv_frame *orig_prframe = prframe;
2450 	struct recv_priv *precvpriv = &padapter->recvpriv;
2451 	struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2452 
2453 	DBG_COUNTER(padapter->rx_logs.core_rx_post);
2454 
2455 	prframe = decryptor(padapter, prframe);
2456 	if (prframe == NULL) {
2457 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decryptor: drop pkt\n"));
2458 		#ifdef DBG_RX_DROP_FRAME
2459 		DBG_871X("DBG_RX_DROP_FRAME %s decryptor: drop pkt\n", __func__);
2460 		#endif
2461 		ret = _FAIL;
2462 		DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_err);
2463 		goto _recv_data_drop;
2464 	}
2465 
2466 	prframe = recvframe_chk_defrag(padapter, prframe);
2467 	if (prframe == NULL)	{
2468 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chk_defrag: drop pkt\n"));
2469 		#ifdef DBG_RX_DROP_FRAME
2470 		DBG_871X("DBG_RX_DROP_FRAME %s recvframe_chk_defrag: drop pkt\n", __func__);
2471 		#endif
2472 		DBG_COUNTER(padapter->rx_logs.core_rx_post_defrag_err);
2473 		goto _recv_data_drop;
2474 	}
2475 
2476 	prframe = portctrl(padapter, prframe);
2477 	if (prframe == NULL) {
2478 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("portctrl: drop pkt\n"));
2479 		#ifdef DBG_RX_DROP_FRAME
2480 		DBG_871X("DBG_RX_DROP_FRAME %s portctrl: drop pkt\n", __func__);
2481 		#endif
2482 		ret = _FAIL;
2483 		DBG_COUNTER(padapter->rx_logs.core_rx_post_portctrl_err);
2484 		goto _recv_data_drop;
2485 	}
2486 
2487 	count_rx_stats(padapter, prframe, NULL);
2488 
2489 	ret = process_recv_indicatepkts(padapter, prframe);
2490 	if (ret != _SUCCESS) {
2491 		RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recv_func: process_recv_indicatepkts fail!\n"));
2492 		#ifdef DBG_RX_DROP_FRAME
2493 		DBG_871X("DBG_RX_DROP_FRAME %s process_recv_indicatepkts fail!\n", __func__);
2494 		#endif
2495 		rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
2496 		DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_err);
2497 		goto _recv_data_drop;
2498 	}
2499 
2500 _recv_data_drop:
2501 	precvpriv->rx_drop++;
2502 	return ret;
2503 }
2504 
2505 
2506 int recv_func(struct adapter *padapter, union recv_frame *rframe);
recv_func(struct adapter * padapter,union recv_frame * rframe)2507 int recv_func(struct adapter *padapter, union recv_frame *rframe)
2508 {
2509 	int ret;
2510 	struct rx_pkt_attrib *prxattrib = &rframe->u.hdr.attrib;
2511 	struct recv_priv *recvpriv = &padapter->recvpriv;
2512 	struct security_priv *psecuritypriv = &padapter->securitypriv;
2513 	struct mlme_priv *mlmepriv = &padapter->mlmepriv;
2514 
2515 	/* check if need to handle uc_swdec_pending_queue*/
2516 	if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey) {
2517 		union recv_frame *pending_frame;
2518 		int cnt = 0;
2519 
2520 		while ((pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
2521 			cnt++;
2522 			DBG_COUNTER(padapter->rx_logs.core_rx_dequeue);
2523 			recv_func_posthandle(padapter, pending_frame);
2524 		}
2525 
2526 		if (cnt)
2527 			DBG_871X(FUNC_ADPT_FMT" dequeue %d from uc_swdec_pending_queue\n",
2528 				FUNC_ADPT_ARG(padapter), cnt);
2529 	}
2530 
2531 	DBG_COUNTER(padapter->rx_logs.core_rx);
2532 	ret = recv_func_prehandle(padapter, rframe);
2533 
2534 	if (ret == _SUCCESS) {
2535 
2536 		/* check if need to enqueue into uc_swdec_pending_queue*/
2537 		if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
2538 			!IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
2539 			(prxattrib->bdecrypted == 0 || psecuritypriv->sw_decrypt == true) &&
2540 			psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPAPSK &&
2541 			!psecuritypriv->busetkipkey) {
2542 			DBG_COUNTER(padapter->rx_logs.core_rx_enqueue);
2543 			rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
2544 			/* DBG_871X("%s: no key, enqueue uc_swdec_pending_queue\n", __func__); */
2545 
2546 			if (recvpriv->free_recvframe_cnt < NR_RECVFRAME/4) {
2547 				/* to prevent from recvframe starvation, get recvframe from uc_swdec_pending_queue to free_recvframe_cnt  */
2548 				rframe = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue);
2549 				if (rframe)
2550 					goto do_posthandle;
2551 			}
2552 			goto exit;
2553 		}
2554 
2555 do_posthandle:
2556 		ret = recv_func_posthandle(padapter, rframe);
2557 	}
2558 
2559 exit:
2560 	return ret;
2561 }
2562 
2563 
rtw_recv_entry(union recv_frame * precvframe)2564 s32 rtw_recv_entry(union recv_frame *precvframe)
2565 {
2566 	struct adapter *padapter;
2567 	struct recv_priv *precvpriv;
2568 	s32 ret = _SUCCESS;
2569 
2570 /* 	RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+rtw_recv_entry\n")); */
2571 
2572 	padapter = precvframe->u.hdr.adapter;
2573 
2574 	precvpriv = &padapter->recvpriv;
2575 
2576 	ret = recv_func(padapter, precvframe);
2577 	if (ret == _FAIL) {
2578 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("rtw_recv_entry: recv_func return fail!!!\n"));
2579 		goto _recv_entry_drop;
2580 	}
2581 
2582 
2583 	precvpriv->rx_pkts++;
2584 
2585 	return ret;
2586 
2587 _recv_entry_drop:
2588 
2589 	/* RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("_recv_entry_drop\n")); */
2590 
2591 	return ret;
2592 }
2593 
rtw_signal_stat_timer_hdl(struct timer_list * t)2594 static void rtw_signal_stat_timer_hdl(struct timer_list *t)
2595 {
2596 	struct adapter *adapter =
2597 		from_timer(adapter, t, recvpriv.signal_stat_timer);
2598 	struct recv_priv *recvpriv = &adapter->recvpriv;
2599 
2600 	u32 tmp_s, tmp_q;
2601 	u8 avg_signal_strength = 0;
2602 	u8 avg_signal_qual = 0;
2603 	u32 num_signal_strength = 0;
2604 	u32 num_signal_qual = 0;
2605 	u8 _alpha = 5; /*  this value is based on converging_constant = 5000 and sampling_interval = 1000 */
2606 
2607 	if (adapter->recvpriv.is_signal_dbg) {
2608 		/* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
2609 		adapter->recvpriv.signal_strength = adapter->recvpriv.signal_strength_dbg;
2610 		adapter->recvpriv.rssi = (s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
2611 	} else {
2612 
2613 		if (recvpriv->signal_strength_data.update_req == 0) {/*  update_req is clear, means we got rx */
2614 			avg_signal_strength = recvpriv->signal_strength_data.avg_val;
2615 			num_signal_strength = recvpriv->signal_strength_data.total_num;
2616 			/*  after avg_vals are accquired, we can re-stat the signal values */
2617 			recvpriv->signal_strength_data.update_req = 1;
2618 		}
2619 
2620 		if (recvpriv->signal_qual_data.update_req == 0) {/*  update_req is clear, means we got rx */
2621 			avg_signal_qual = recvpriv->signal_qual_data.avg_val;
2622 			num_signal_qual = recvpriv->signal_qual_data.total_num;
2623 			/*  after avg_vals are accquired, we can re-stat the signal values */
2624 			recvpriv->signal_qual_data.update_req = 1;
2625 		}
2626 
2627 		if (num_signal_strength == 0) {
2628 			if (rtw_get_on_cur_ch_time(adapter) == 0
2629 				|| jiffies_to_msecs(jiffies - rtw_get_on_cur_ch_time(adapter)) < 2 * adapter->mlmeextpriv.mlmext_info.bcn_interval
2630 			) {
2631 				goto set_timer;
2632 			}
2633 		}
2634 
2635 		if (check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY) == true
2636 			|| check_fwstate(&adapter->mlmepriv, _FW_LINKED) == false
2637 		) {
2638 			goto set_timer;
2639 		}
2640 
2641 		/* update value of signal_strength, rssi, signal_qual */
2642 		tmp_s = (avg_signal_strength+(_alpha-1)*recvpriv->signal_strength);
2643 		if (tmp_s % _alpha)
2644 			tmp_s = tmp_s/_alpha + 1;
2645 		else
2646 			tmp_s = tmp_s/_alpha;
2647 		if (tmp_s > 100)
2648 			tmp_s = 100;
2649 
2650 		tmp_q = (avg_signal_qual+(_alpha-1)*recvpriv->signal_qual);
2651 		if (tmp_q % _alpha)
2652 			tmp_q = tmp_q/_alpha + 1;
2653 		else
2654 			tmp_q = tmp_q/_alpha;
2655 		if (tmp_q > 100)
2656 			tmp_q = 100;
2657 
2658 		recvpriv->signal_strength = tmp_s;
2659 		recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
2660 		recvpriv->signal_qual = tmp_q;
2661 
2662 		#if defined(DBG_RX_SIGNAL_DISPLAY_PROCESSING) && 1
2663 		DBG_871X(FUNC_ADPT_FMT" signal_strength:%3u, rssi:%3d, signal_qual:%3u"
2664 			", num_signal_strength:%u, num_signal_qual:%u"
2665 			", on_cur_ch_ms:%d"
2666 			"\n"
2667 			, FUNC_ADPT_ARG(adapter)
2668 			, recvpriv->signal_strength
2669 			, recvpriv->rssi
2670 			, recvpriv->signal_qual
2671 			, num_signal_strength, num_signal_qual
2672 			, rtw_get_on_cur_ch_time(adapter) ? jiffies_to_msecs(jiffies - rtw_get_on_cur_ch_time(adapter)) : 0
2673 		);
2674 		#endif
2675 	}
2676 
2677 set_timer:
2678 	rtw_set_signal_stat_timer(recvpriv);
2679 
2680 }
2681