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