• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2017 Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  *****************************************************************************/
15 #define _RTW_RECV_C_
16 
17 #include <drv_types.h>
18 #include <hal_data.h>
19 
20 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
21 static void rtw_signal_stat_timer_hdl(void *ctx);
22 
23 enum {
24 	SIGNAL_STAT_CALC_PROFILE_0 = 0,
25 	SIGNAL_STAT_CALC_PROFILE_1,
26 	SIGNAL_STAT_CALC_PROFILE_2,
27 	SIGNAL_STAT_CALC_PROFILE_MAX
28 };
29 
30 u8 signal_stat_calc_profile[SIGNAL_STAT_CALC_PROFILE_MAX][3] = {
31 	{4, 1},	/* Profile 0 => pre_stat : curr_stat = 4 : 1 */
32 	{3, 7},	/* Profile 1 => pre_stat : curr_stat = 3 : 7 */
33 	{0, 10}	/* Profile 2 => pre_stat : curr_stat = 0 : 10 */
34 };
35 
36 #ifndef RTW_SIGNAL_STATE_CALC_PROFILE
37 	#define RTW_SIGNAL_STATE_CALC_PROFILE SIGNAL_STAT_CALC_PROFILE_1
38 #endif
39 
40 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
41 
42 u8 rtw_bridge_tunnel_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
43 u8 rtw_rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
44 static u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
45 static u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
46 #ifdef CONFIG_TDLS
47 static u8 SNAP_ETH_TYPE_TDLS[2] = {0x89, 0x0d};
48 #endif
49 
_rtw_init_sta_recv_priv(struct sta_recv_priv * psta_recvpriv)50 void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
51 {
52 
53 
54 
55 	_rtw_memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
56 
57 	_rtw_spinlock_init(&psta_recvpriv->lock);
58 
59 	/* for(i=0; i<MAX_RX_NUMBLKS; i++) */
60 	/*	_rtw_init_queue(&psta_recvpriv->blk_strms[i]); */
61 
62 	_rtw_init_queue(&psta_recvpriv->defrag_q);
63 
64 
65 }
66 
_rtw_init_recv_priv(struct recv_priv * precvpriv,_adapter * padapter)67 sint _rtw_init_recv_priv(struct recv_priv *precvpriv, _adapter *padapter)
68 {
69 	sint i;
70 
71 	union recv_frame *precvframe;
72 	sint	res = _SUCCESS;
73 
74 
75 	/* We don't need to memset padapter->XXX to zero, because adapter is allocated by rtw_zvmalloc(). */
76 	/* _rtw_memset((unsigned char *)precvpriv, 0, sizeof (struct  recv_priv)); */
77 
78 	_rtw_spinlock_init(&precvpriv->lock);
79 
80 #ifdef CONFIG_RECV_THREAD_MODE
81 	_rtw_init_sema(&precvpriv->recv_sema, 0);
82 
83 #endif
84 
85 	_rtw_init_queue(&precvpriv->free_recv_queue);
86 	_rtw_init_queue(&precvpriv->recv_pending_queue);
87 	_rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
88 
89 	precvpriv->adapter = padapter;
90 
91 	precvpriv->free_recvframe_cnt = NR_RECVFRAME;
92 
93 	precvpriv->sink_udpport = 0;
94 	precvpriv->pre_rtp_rxseq = 0;
95 	precvpriv->cur_rtp_rxseq = 0;
96 
97 #ifdef DBG_RX_SIGNAL_DISPLAY_RAW_DATA
98 	precvpriv->store_law_data_flag = 1;
99 #else
100 	precvpriv->store_law_data_flag = 0;
101 #endif
102 
103 	rtw_os_recv_resource_init(precvpriv, padapter);
104 
105 	precvpriv->pallocated_frame_buf = rtw_zvmalloc(NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
106 
107 	if (precvpriv->pallocated_frame_buf == NULL) {
108 		res = _FAIL;
109 		goto exit;
110 	}
111 	/* _rtw_memset(precvpriv->pallocated_frame_buf, 0, NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ); */
112 
113 	precvpriv->precv_frame_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(precvpriv->pallocated_frame_buf), RXFRAME_ALIGN_SZ);
114 	/* precvpriv->precv_frame_buf = precvpriv->pallocated_frame_buf + RXFRAME_ALIGN_SZ - */
115 	/*						((SIZE_PTR) (precvpriv->pallocated_frame_buf) &(RXFRAME_ALIGN_SZ-1)); */
116 
117 	precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
118 
119 
120 	for (i = 0; i < NR_RECVFRAME ; i++) {
121 		_rtw_init_listhead(&(precvframe->u.list));
122 
123 		rtw_list_insert_tail(&(precvframe->u.list), &(precvpriv->free_recv_queue.queue));
124 
125 		rtw_os_recv_resource_alloc(padapter, precvframe);
126 
127 		precvframe->u.hdr.len = 0;
128 
129 		precvframe->u.hdr.adapter = padapter;
130 		precvframe++;
131 
132 	}
133 
134 #ifdef CONFIG_USB_HCI
135 
136 	ATOMIC_SET(&(precvpriv->rx_pending_cnt), 1);
137 
138 	_rtw_init_sema(&precvpriv->allrxreturnevt, 0);
139 
140 #endif
141 
142 	res = rtw_hal_init_recv_priv(padapter);
143 
144 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
145 	rtw_init_timer(&precvpriv->signal_stat_timer, padapter, rtw_signal_stat_timer_hdl, padapter);
146 
147 	precvpriv->signal_stat_sampling_interval = 2000; /* ms */
148 	/* precvpriv->signal_stat_converging_constant = 5000; */ /* ms */
149 
150 	rtw_set_signal_stat_timer(precvpriv);
151 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
152 
153 	_rtw_memset(&precvpriv->ip_statistic, 0,
154 			sizeof(struct rtw_ip_dbg_cnt_statistic));
155 
156 exit:
157 
158 
159 	return res;
160 
161 }
162 
163 void rtw_mfree_recv_priv_lock(struct recv_priv *precvpriv);
rtw_mfree_recv_priv_lock(struct recv_priv * precvpriv)164 void rtw_mfree_recv_priv_lock(struct recv_priv *precvpriv)
165 {
166 	_rtw_spinlock_free(&precvpriv->lock);
167 #ifdef CONFIG_RECV_THREAD_MODE
168 	_rtw_free_sema(&precvpriv->recv_sema);
169 #endif
170 
171 	_rtw_spinlock_free(&precvpriv->free_recv_queue.lock);
172 	_rtw_spinlock_free(&precvpriv->recv_pending_queue.lock);
173 
174 	_rtw_spinlock_free(&precvpriv->free_recv_buf_queue.lock);
175 
176 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
177 	_rtw_spinlock_free(&precvpriv->recv_buf_pending_queue.lock);
178 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
179 }
180 
_rtw_free_recv_priv(struct recv_priv * precvpriv)181 void _rtw_free_recv_priv(struct recv_priv *precvpriv)
182 {
183 	_adapter	*padapter = precvpriv->adapter;
184 
185 
186 	rtw_free_uc_swdec_pending_queue(padapter);
187 
188 	rtw_mfree_recv_priv_lock(precvpriv);
189 
190 	rtw_os_recv_resource_free(precvpriv);
191 
192 	if (precvpriv->pallocated_frame_buf)
193 		rtw_vmfree(precvpriv->pallocated_frame_buf, NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
194 
195 	rtw_hal_free_recv_priv(padapter);
196 
197 
198 }
199 
rtw_rframe_del_wfd_ie(union recv_frame * rframe,u8 ies_offset)200 bool rtw_rframe_del_wfd_ie(union recv_frame *rframe, u8 ies_offset)
201 {
202 #define DBG_RFRAME_DEL_WFD_IE 0
203 	u8 *ies = rframe->u.hdr.rx_data + sizeof(struct rtw_ieee80211_hdr_3addr) + ies_offset;
204 	uint ies_len_ori = rframe->u.hdr.len - (ies - rframe->u.hdr.rx_data);
205 	uint ies_len;
206 
207 	ies_len = rtw_del_wfd_ie(ies, ies_len_ori, DBG_RFRAME_DEL_WFD_IE ? __func__ : NULL);
208 	rframe->u.hdr.len -= ies_len_ori - ies_len;
209 
210 	return ies_len_ori != ies_len;
211 }
212 
_rtw_alloc_recvframe(_queue * pfree_recv_queue)213 union recv_frame *_rtw_alloc_recvframe(_queue *pfree_recv_queue)
214 {
215 
216 	union recv_frame  *precvframe;
217 	_list	*plist, *phead;
218 	_adapter *padapter;
219 	struct recv_priv *precvpriv;
220 
221 	if (_rtw_queue_empty(pfree_recv_queue) == _TRUE)
222 		precvframe = NULL;
223 	else {
224 		phead = get_list_head(pfree_recv_queue);
225 
226 		plist = get_next(phead);
227 
228 		precvframe = LIST_CONTAINOR(plist, union recv_frame, u);
229 
230 		rtw_list_delete(&precvframe->u.hdr.list);
231 		padapter = precvframe->u.hdr.adapter;
232 		if (padapter != NULL) {
233 			precvpriv = &padapter->recvpriv;
234 			if (pfree_recv_queue == &precvpriv->free_recv_queue)
235 				precvpriv->free_recvframe_cnt--;
236 		}
237 	}
238 
239 
240 	return precvframe;
241 
242 }
243 
rtw_alloc_recvframe(_queue * pfree_recv_queue)244 union recv_frame *rtw_alloc_recvframe(_queue *pfree_recv_queue)
245 {
246 	_irqL irqL;
247 	union recv_frame  *precvframe;
248 
249 	_enter_critical_bh(&pfree_recv_queue->lock, &irqL);
250 
251 	precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
252 
253 	_exit_critical_bh(&pfree_recv_queue->lock, &irqL);
254 
255 	return precvframe;
256 }
257 
rtw_init_recvframe(union recv_frame * precvframe,struct recv_priv * precvpriv)258 void rtw_init_recvframe(union recv_frame *precvframe, struct recv_priv *precvpriv)
259 {
260 	/* Perry: This can be removed */
261 	_rtw_init_listhead(&precvframe->u.hdr.list);
262 
263 	precvframe->u.hdr.len = 0;
264 }
265 
rtw_free_recvframe(union recv_frame * precvframe,_queue * pfree_recv_queue)266 int rtw_free_recvframe(union recv_frame *precvframe, _queue *pfree_recv_queue)
267 {
268 	_irqL irqL;
269 	_adapter *padapter = precvframe->u.hdr.adapter;
270 	struct recv_priv *precvpriv = &padapter->recvpriv;
271 
272 
273 #ifdef CONFIG_CONCURRENT_MODE
274 	padapter = GET_PRIMARY_ADAPTER(padapter);
275 	precvpriv = &padapter->recvpriv;
276 	pfree_recv_queue = &precvpriv->free_recv_queue;
277 	precvframe->u.hdr.adapter = padapter;
278 #endif
279 
280 
281 	rtw_os_free_recvframe(precvframe);
282 
283 
284 	_enter_critical_bh(&pfree_recv_queue->lock, &irqL);
285 
286 	rtw_list_delete(&(precvframe->u.hdr.list));
287 
288 	precvframe->u.hdr.len = 0;
289 	precvframe->u.hdr.attrib.phy_info.physts_rpt_valid = _FALSE;
290 
291 	rtw_list_insert_tail(&(precvframe->u.hdr.list), get_list_head(pfree_recv_queue));
292 
293 	if (padapter != NULL) {
294 		if (pfree_recv_queue == &precvpriv->free_recv_queue)
295 			precvpriv->free_recvframe_cnt++;
296 	}
297 
298 	_exit_critical_bh(&pfree_recv_queue->lock, &irqL);
299 
300 
301 	return _SUCCESS;
302 
303 }
304 
305 
306 
307 
_rtw_enqueue_recvframe(union recv_frame * precvframe,_queue * queue)308 sint _rtw_enqueue_recvframe(union recv_frame *precvframe, _queue *queue)
309 {
310 
311 	_adapter *padapter = precvframe->u.hdr.adapter;
312 	struct recv_priv *precvpriv = &padapter->recvpriv;
313 
314 
315 	/* _rtw_init_listhead(&(precvframe->u.hdr.list)); */
316 	rtw_list_delete(&(precvframe->u.hdr.list));
317 
318 
319 	rtw_list_insert_tail(&(precvframe->u.hdr.list), get_list_head(queue));
320 
321 	if (padapter != NULL) {
322 		if (queue == &precvpriv->free_recv_queue)
323 			precvpriv->free_recvframe_cnt++;
324 	}
325 
326 
327 	return _SUCCESS;
328 }
329 
rtw_enqueue_recvframe(union recv_frame * precvframe,_queue * queue)330 sint rtw_enqueue_recvframe(union recv_frame *precvframe, _queue *queue)
331 {
332 	sint ret;
333 	_irqL irqL;
334 
335 	/* _spinlock(&pfree_recv_queue->lock); */
336 	_enter_critical_bh(&queue->lock, &irqL);
337 	ret = _rtw_enqueue_recvframe(precvframe, queue);
338 	/* _rtw_spinunlock(&pfree_recv_queue->lock); */
339 	_exit_critical_bh(&queue->lock, &irqL);
340 
341 	return ret;
342 }
343 
344 /*
345 sint	rtw_enqueue_recvframe(union recv_frame *precvframe, _queue *queue)
346 {
347 	return rtw_free_recvframe(precvframe, queue);
348 }
349 */
350 
351 
352 
353 
354 /*
355 caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
356 pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
357 
358 using spinlock to protect
359 
360 */
361 
rtw_free_recvframe_queue(_queue * pframequeue,_queue * pfree_recv_queue)362 void rtw_free_recvframe_queue(_queue *pframequeue,  _queue *pfree_recv_queue)
363 {
364 	union	recv_frame	*precvframe;
365 	_list	*plist, *phead;
366 
367 	_rtw_spinlock(&pframequeue->lock);
368 
369 	phead = get_list_head(pframequeue);
370 	plist = get_next(phead);
371 
372 	while (rtw_end_of_queue_search(phead, plist) == _FALSE) {
373 		precvframe = LIST_CONTAINOR(plist, union recv_frame, u);
374 
375 		plist = get_next(plist);
376 
377 		/* rtw_list_delete(&precvframe->u.hdr.list); */ /* will do this in rtw_free_recvframe() */
378 
379 		rtw_free_recvframe(precvframe, pfree_recv_queue);
380 	}
381 
382 	_rtw_spinunlock(&pframequeue->lock);
383 
384 
385 }
386 
rtw_free_uc_swdec_pending_queue(_adapter * adapter)387 u32 rtw_free_uc_swdec_pending_queue(_adapter *adapter)
388 {
389 	u32 cnt = 0;
390 	union recv_frame *pending_frame;
391 	while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
392 		rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
393 		cnt++;
394 	}
395 
396 	if (cnt)
397 		RTW_INFO(FUNC_ADPT_FMT" dequeue %d\n", FUNC_ADPT_ARG(adapter), cnt);
398 
399 	return cnt;
400 }
401 
402 
403 #ifndef CONFIG_RECVBUF_QUEUE_LOCK_BH
404 #ifdef CONFIG_SDIO_HCI
405 #define CONFIG_RECVBUF_QUEUE_LOCK_BH 1
406 #else
407 #define CONFIG_RECVBUF_QUEUE_LOCK_BH 0
408 #endif
409 #endif /* CONFIG_RECVBUF_QUEUE_LOCK_BH */
410 
rtw_enqueue_recvbuf_to_head(struct recv_buf * precvbuf,_queue * queue)411 sint rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, _queue *queue)
412 {
413 	_irqL irqL;
414 
415 #if CONFIG_RECVBUF_QUEUE_LOCK_BH
416 	_enter_critical_bh(&queue->lock, &irqL);
417 #else
418 	_enter_critical_ex(&queue->lock, &irqL);
419 #endif
420 
421 	rtw_list_delete(&precvbuf->list);
422 	rtw_list_insert_head(&precvbuf->list, get_list_head(queue));
423 
424 #if CONFIG_RECVBUF_QUEUE_LOCK_BH
425 	_exit_critical_bh(&queue->lock, &irqL);
426 #else
427 	_exit_critical_ex(&queue->lock, &irqL);
428 #endif
429 
430 	return _SUCCESS;
431 }
432 
rtw_enqueue_recvbuf(struct recv_buf * precvbuf,_queue * queue)433 sint rtw_enqueue_recvbuf(struct recv_buf *precvbuf, _queue *queue)
434 {
435 	_irqL irqL;
436 
437 #if CONFIG_RECVBUF_QUEUE_LOCK_BH
438 	_enter_critical_bh(&queue->lock, &irqL);
439 #else
440 	_enter_critical_ex(&queue->lock, &irqL);
441 #endif
442 
443 	rtw_list_delete(&precvbuf->list);
444 
445 	rtw_list_insert_tail(&precvbuf->list, get_list_head(queue));
446 
447 #if CONFIG_RECVBUF_QUEUE_LOCK_BH
448 	_exit_critical_bh(&queue->lock, &irqL);
449 #else
450 	_exit_critical_ex(&queue->lock, &irqL);
451 #endif
452 
453 	return _SUCCESS;
454 
455 }
456 
rtw_dequeue_recvbuf(_queue * queue)457 struct recv_buf *rtw_dequeue_recvbuf(_queue *queue)
458 {
459 	_irqL irqL;
460 	struct recv_buf *precvbuf;
461 	_list	*plist, *phead;
462 
463 #if CONFIG_RECVBUF_QUEUE_LOCK_BH
464 	_enter_critical_bh(&queue->lock, &irqL);
465 #else
466 	_enter_critical_ex(&queue->lock, &irqL);
467 #endif
468 
469 	if (_rtw_queue_empty(queue) == _TRUE)
470 		precvbuf = NULL;
471 	else {
472 		phead = get_list_head(queue);
473 
474 		plist = get_next(phead);
475 
476 		precvbuf = LIST_CONTAINOR(plist, struct recv_buf, list);
477 
478 		rtw_list_delete(&precvbuf->list);
479 
480 	}
481 
482 #if CONFIG_RECVBUF_QUEUE_LOCK_BH
483 	_exit_critical_bh(&queue->lock, &irqL);
484 #else
485 	_exit_critical_ex(&queue->lock, &irqL);
486 #endif
487 
488 	return precvbuf;
489 
490 }
491 
492 sint recvframe_chkmic(_adapter *adapter,  union recv_frame *precvframe);
recvframe_chkmic(_adapter * adapter,union recv_frame * precvframe)493 sint recvframe_chkmic(_adapter *adapter,  union recv_frame *precvframe)
494 {
495 
496 	sint	i, res = _SUCCESS;
497 	u32	datalen;
498 	u8	miccode[8];
499 	u8	bmic_err = _FALSE, brpt_micerror = _TRUE;
500 	u8	*pframe, *payload, *pframemic;
501 	u8	*mickey;
502 	/* u8	*iv,rxdata_key_idx=0; */
503 	struct	sta_info		*stainfo;
504 	struct	rx_pkt_attrib	*prxattrib = &precvframe->u.hdr.attrib;
505 	struct	security_priv	*psecuritypriv = &adapter->securitypriv;
506 
507 	struct mlme_ext_priv	*pmlmeext = &adapter->mlmeextpriv;
508 	struct mlme_ext_info	*pmlmeinfo = &(pmlmeext->mlmext_info);
509 
510 	stainfo = rtw_get_stainfo(&adapter->stapriv , &prxattrib->ta[0]);
511 
512 	if (prxattrib->encrypt == _TKIP_) {
513 
514 		/* calculate mic code */
515 		if (stainfo != NULL) {
516 			if (IS_MCAST(prxattrib->ra)) {
517 				/* mickey=&psecuritypriv->dot118021XGrprxmickey.skey[0]; */
518 				/* iv = precvframe->u.hdr.rx_data+prxattrib->hdrlen; */
519 				/* rxdata_key_idx =( ((iv[3])>>6)&0x3) ; */
520 				mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
521 
522 				/* RTW_INFO("\n recvframe_chkmic: bcmc key psecuritypriv->dot118021XGrpKeyid(%d),pmlmeinfo->key_index(%d) ,recv key_id(%d)\n", */
523 				/*								psecuritypriv->dot118021XGrpKeyid,pmlmeinfo->key_index,rxdata_key_idx); */
524 
525 				if (psecuritypriv->binstallGrpkey == _FALSE) {
526 					res = _FAIL;
527 					RTW_INFO("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
528 					goto exit;
529 				}
530 			} else {
531 				mickey = &stainfo->dot11tkiprxmickey.skey[0];
532 			}
533 
534 			datalen = precvframe->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len - prxattrib->icv_len - 8; /* icv_len included the mic code */
535 			pframe = precvframe->u.hdr.rx_data;
536 			payload = pframe + prxattrib->hdrlen + prxattrib->iv_len;
537 
538 
539 			/* rtw_seccalctkipmic(&stainfo->dot11tkiprxmickey.skey[0],pframe,payload, datalen ,&miccode[0],(unsigned char)prxattrib->priority); */ /* care the length of the data */
540 
541 			rtw_seccalctkipmic(mickey, pframe, payload, datalen , &miccode[0], (unsigned char)prxattrib->priority); /* care the length of the data */
542 
543 			pframemic = payload + datalen;
544 
545 			bmic_err = _FALSE;
546 
547 			for (i = 0; i < 8; i++) {
548 				if (miccode[i] != *(pframemic + i)) {
549 					bmic_err = _TRUE;
550 				}
551 			}
552 
553 
554 			if (bmic_err == _TRUE) {
555 
556 
557 
558 				/* double check key_index for some timing issue , */
559 				/* cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
560 				if ((IS_MCAST(prxattrib->ra) == _TRUE)  && (prxattrib->key_index != pmlmeinfo->key_index))
561 					brpt_micerror = _FALSE;
562 
563 				if ((prxattrib->bdecrypted == _TRUE) && (brpt_micerror == _TRUE)) {
564 					rtw_handle_tkip_mic_err(adapter, stainfo, (u8)IS_MCAST(prxattrib->ra));
565 					RTW_INFO(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
566 				} else {
567 					RTW_INFO(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
568 				}
569 
570 				res = _FAIL;
571 
572 			} else {
573 				/* mic checked ok */
574 				if ((psecuritypriv->bcheck_grpkey == _FALSE) && (IS_MCAST(prxattrib->ra) == _TRUE)) {
575 					psecuritypriv->bcheck_grpkey = _TRUE;
576 				}
577 			}
578 
579 		}
580 
581 		recvframe_pull_tail(precvframe, 8);
582 
583 	}
584 
585 exit:
586 
587 
588 	return res;
589 
590 }
591 
592 /*#define DBG_RX_SW_DECRYPTOR*/
593 
594 /* decrypt and set the ivlen,icvlen of the recv_frame */
595 union recv_frame *decryptor(_adapter *padapter, union recv_frame *precv_frame);
decryptor(_adapter * padapter,union recv_frame * precv_frame)596 union recv_frame *decryptor(_adapter *padapter, union recv_frame *precv_frame)
597 {
598 
599 	struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
600 	struct security_priv *psecuritypriv = &padapter->securitypriv;
601 	union recv_frame *return_packet = precv_frame;
602 	u32	 res = _SUCCESS;
603 
604 
605 	DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt);
606 
607 
608 	if (prxattrib->encrypt > 0) {
609 		u8 *iv = precv_frame->u.hdr.rx_data + prxattrib->hdrlen;
610 		prxattrib->key_index = (((iv[3]) >> 6) & 0x3) ;
611 
612 		if (prxattrib->key_index > WEP_KEYS) {
613 			RTW_INFO("prxattrib->key_index(%d) > WEP_KEYS\n", prxattrib->key_index);
614 
615 			switch (prxattrib->encrypt) {
616 			case _WEP40_:
617 			case _WEP104_:
618 				prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
619 				break;
620 			case _TKIP_:
621 			case _AES_:
622 			case _GCMP_:
623 			case _GCMP_256_:
624 			case _CCMP_256_:
625 			default:
626 				prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
627 				break;
628 			}
629 		}
630 	}
631 
632 	if (prxattrib->encrypt && !prxattrib->bdecrypted) {
633 		if (GetFrameType(get_recvframe_data(precv_frame)) == WIFI_DATA
634 			#ifdef CONFIG_CONCURRENT_MODE
635 			&& !IS_MCAST(prxattrib->ra) /* bc/mc packets may use sw decryption for concurrent mode */
636 			#endif
637 		)
638 			psecuritypriv->hw_decrypted = _FALSE;
639 
640 #ifdef DBG_RX_SW_DECRYPTOR
641 		RTW_INFO(ADPT_FMT" - sec_type:%s DO SW decryption\n",
642 			ADPT_ARG(padapter), security_type_str(prxattrib->encrypt));
643 #endif
644 
645 #ifdef DBG_RX_DECRYPTOR
646 		RTW_INFO("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
647 			 __FUNCTION__,
648 			 __LINE__,
649 			 prxattrib->bdecrypted,
650 			 prxattrib->encrypt,
651 			 psecuritypriv->hw_decrypted);
652 #endif
653 
654 		switch (prxattrib->encrypt) {
655 		case _WEP40_:
656 		case _WEP104_:
657 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_wep);
658 			rtw_wep_decrypt(padapter, (u8 *)precv_frame);
659 			break;
660 		case _TKIP_:
661 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_tkip);
662 			res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
663 			break;
664 		case _AES_:
665 		case _CCMP_256_:
666 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_aes);
667 			res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
668 			break;
669 		case _GCMP_:
670 		case _GCMP_256_:
671 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_gcmp);
672 			res = rtw_gcmp_decrypt(padapter, (u8 *)precv_frame);
673 			break;
674 #ifdef CONFIG_WAPI_SUPPORT
675 		case _SMS4_:
676 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_wapi);
677 			rtw_sms4_decrypt(padapter, (u8 *)precv_frame);
678 			break;
679 #endif
680 		default:
681 			break;
682 		}
683 	} else if (prxattrib->bdecrypted == 1
684 		   && prxattrib->encrypt > 0
685 		&& (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_)
686 		  ) {
687 #if 0
688 		if ((prxstat->icv == 1) && (prxattrib->encrypt != _AES_)) {
689 			psecuritypriv->hw_decrypted = _FALSE;
690 
691 
692 			rtw_free_recvframe(precv_frame, &padapter->recvpriv.free_recv_queue);
693 
694 			return_packet = NULL;
695 
696 		} else
697 #endif
698 		{
699 			DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_hw);
700 
701 			psecuritypriv->hw_decrypted = _TRUE;
702 #ifdef DBG_RX_DECRYPTOR
703 			RTW_INFO("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
704 				 __FUNCTION__,
705 				 __LINE__,
706 				 prxattrib->bdecrypted,
707 				 prxattrib->encrypt,
708 				 psecuritypriv->hw_decrypted);
709 
710 #endif
711 		}
712 	} else {
713 		DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_unknown);
714 #ifdef DBG_RX_DECRYPTOR
715 		RTW_INFO("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
716 			 __FUNCTION__,
717 			 __LINE__,
718 			 prxattrib->bdecrypted,
719 			 prxattrib->encrypt,
720 			 psecuritypriv->hw_decrypted);
721 #endif
722 	}
723 
724 	#ifdef CONFIG_RTW_MESH
725 	if (res != _FAIL
726 		&& !prxattrib->amsdu
727 		&& prxattrib->mesh_ctrl_present)
728 		res = rtw_mesh_rx_validate_mctrl_non_amsdu(padapter, precv_frame);
729 	#endif
730 
731 	if (res == _FAIL) {
732 		rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
733 		return_packet = NULL;
734 	} else
735 		prxattrib->bdecrypted = _TRUE;
736 	/* recvframe_chkmic(adapter, precv_frame);   */ /* move to recvframme_defrag function */
737 
738 
739 	return return_packet;
740 
741 }
742 /* ###set the security information in the recv_frame */
743 union recv_frame *portctrl(_adapter *adapter, union recv_frame *precv_frame);
portctrl(_adapter * adapter,union recv_frame * precv_frame)744 union recv_frame *portctrl(_adapter *adapter, union recv_frame *precv_frame)
745 {
746 	u8 *psta_addr = NULL;
747 	u8 *ptr;
748 	uint  auth_alg;
749 	struct recv_frame_hdr *pfhdr;
750 	struct sta_info *psta;
751 	struct sta_priv *pstapriv ;
752 	union recv_frame *prtnframe;
753 	u16	ether_type = 0;
754 	u16  eapol_type = 0x888e;/* for Funia BD's WPA issue  */
755 	struct rx_pkt_attrib *pattrib;
756 
757 
758 	pstapriv = &adapter->stapriv;
759 
760 	auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
761 
762 	ptr = get_recvframe_data(precv_frame);
763 	pfhdr = &precv_frame->u.hdr;
764 	pattrib = &pfhdr->attrib;
765 	psta_addr = pattrib->ta;
766 
767 	prtnframe = NULL;
768 
769 	psta = rtw_get_stainfo(pstapriv, psta_addr);
770 
771 
772 	if (auth_alg == dot11AuthAlgrthm_8021X) {
773 		if ((psta != NULL) && (psta->ieee8021x_blocked)) {
774 			/* blocked */
775 			/* only accept EAPOL frame */
776 
777 			prtnframe = precv_frame;
778 
779 			/* get ether_type */
780 			ptr = ptr + pfhdr->attrib.hdrlen + pfhdr->attrib.iv_len + LLC_HEADER_SIZE;
781 			_rtw_memcpy(&ether_type, ptr, 2);
782 			ether_type = ntohs((unsigned short)ether_type);
783 
784 			if (ether_type == eapol_type)
785 				prtnframe = precv_frame;
786 			else {
787 				/* free this frame */
788 				rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
789 				prtnframe = NULL;
790 			}
791 		} else {
792 			/* allowed */
793 			/* check decryption status, and decrypt the frame if needed */
794 
795 
796 			prtnframe = precv_frame;
797 			/* check is the EAPOL frame or not (Rekey) */
798 			/* if(ether_type == eapol_type){ */
799 			/* check Rekey */
800 
801 			/*	prtnframe=precv_frame; */
802 			/* } */
803 		}
804 	} else
805 		prtnframe = precv_frame;
806 
807 
808 	return prtnframe;
809 
810 }
811 
812 /* VALID_PN_CHK
813  * Return true when PN is legal, otherwise false.
814  * Legal PN:
815  *	1. If old PN is 0, any PN is legal
816  *	2. PN > old PN
817  */
818 #define PN_LESS_CHK(a, b)	(((a-b) & 0x800000000000) != 0)
819 #define VALID_PN_CHK(new, old)	(((old) == 0) || PN_LESS_CHK(old, new))
820 #define CCMPH_2_KEYID(ch)	(((ch) & 0x00000000c0000000) >> 30)
821 sint recv_ucast_pn_decache(union recv_frame *precv_frame);
recv_ucast_pn_decache(union recv_frame * precv_frame)822 sint recv_ucast_pn_decache(union recv_frame *precv_frame)
823 {
824 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
825 	struct sta_info *sta = precv_frame->u.hdr.psta;
826 	struct stainfo_rxcache *prxcache = &sta->sta_recvpriv.rxcache;
827 	u8 *pdata = precv_frame->u.hdr.rx_data;
828 	sint tid = precv_frame->u.hdr.attrib.priority;
829 	u64 tmp_iv_hdr = 0;
830 	u64 curr_pn = 0, pkt_pn = 0;
831 
832 	if (tid > 15)
833 		return _FAIL;
834 
835 	if (pattrib->encrypt == _AES_) {
836 		tmp_iv_hdr = le64_to_cpu(*(u64*)(pdata + pattrib->hdrlen));
837 		pkt_pn = CCMPH_2_PN(tmp_iv_hdr);
838 		tmp_iv_hdr = le64_to_cpu(*(u64*)prxcache->iv[tid]);
839 		curr_pn = CCMPH_2_PN(tmp_iv_hdr);
840 
841 		if (!VALID_PN_CHK(pkt_pn, curr_pn)) {
842 			/* return _FAIL; */
843 		} else {
844 			prxcache->last_tid = tid;
845 			_rtw_memcpy(prxcache->iv[tid],
846 				    (pdata + pattrib->hdrlen),
847 				    sizeof(prxcache->iv[tid]));
848 		}
849 	}
850 
851 	return _SUCCESS;
852 }
853 
854 sint recv_bcast_pn_decache(union recv_frame *precv_frame);
recv_bcast_pn_decache(union recv_frame * precv_frame)855 sint recv_bcast_pn_decache(union recv_frame *precv_frame)
856 {
857 	_adapter *padapter = precv_frame->u.hdr.adapter;
858 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
859 	struct security_priv *psecuritypriv = &padapter->securitypriv;
860 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
861 	u8 *pdata = precv_frame->u.hdr.rx_data;
862 	u64 tmp_iv_hdr = 0;
863 	u64 curr_pn = 0, pkt_pn = 0;
864 	u8 key_id;
865 
866 	if ((pattrib->encrypt == _AES_) &&
867 		(check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE)) {
868 
869 		tmp_iv_hdr = le64_to_cpu(*(u64*)(pdata + pattrib->hdrlen));
870 		key_id = CCMPH_2_KEYID(tmp_iv_hdr);
871 		pkt_pn = CCMPH_2_PN(tmp_iv_hdr);
872 
873 		curr_pn = le64_to_cpu(*(u64*)psecuritypriv->iv_seq[key_id]);
874 		curr_pn &= 0x0000ffffffffffff;
875 
876 		if (!VALID_PN_CHK(pkt_pn, curr_pn))
877 			return _FAIL;
878 
879 		*(u64*)psecuritypriv->iv_seq[key_id] = cpu_to_le64(pkt_pn);
880 	}
881 
882 	return _SUCCESS;
883 }
884 
recv_decache(union recv_frame * precv_frame)885 sint recv_decache(union recv_frame *precv_frame)
886 {
887 	struct sta_info *psta = precv_frame->u.hdr.psta;
888 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
889 	_adapter *adapter = psta->padapter;
890 	sint tid = pattrib->priority;
891 	u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num & 0xffff) << 4) |
892 		       (precv_frame->u.hdr.attrib.frag_num & 0xf);
893 	u16 *prxseq;
894 
895 	if (tid > 15)
896 		return _FAIL;
897 
898 	if (pattrib->qos) {
899 		if (IS_MCAST(pattrib->ra))
900 			prxseq = &psta->sta_recvpriv.bmc_tid_rxseq[tid];
901 		else
902 			prxseq = &psta->sta_recvpriv.rxcache.tid_rxseq[tid];
903 	} else {
904 		if (IS_MCAST(pattrib->ra)) {
905 			prxseq = &psta->sta_recvpriv.nonqos_bmc_rxseq;
906 			#ifdef DBG_RX_SEQ
907 			RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" nonqos bmc seq_num:%d\n"
908 				, FUNC_ADPT_ARG(adapter), pattrib->seq_num);
909 			#endif
910 
911 		} else {
912 			prxseq = &psta->sta_recvpriv.nonqos_rxseq;
913 			#ifdef DBG_RX_SEQ
914 			RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" nonqos seq_num:%d\n"
915 				, FUNC_ADPT_ARG(adapter), pattrib->seq_num);
916 			#endif
917 		}
918 	}
919 
920 	if (seq_ctrl == *prxseq) {
921 		/* for non-AMPDU case	*/
922 		psta->sta_stats.duplicate_cnt++;
923 
924 		if (psta->sta_stats.duplicate_cnt % 100 == 0)
925 			RTW_INFO("%s: tid=%u seq=%d frag=%d\n", __func__
926 				, tid, precv_frame->u.hdr.attrib.seq_num
927 				, precv_frame->u.hdr.attrib.frag_num);
928 
929 		#ifdef DBG_RX_DROP_FRAME
930 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" recv_decache _FAIL for sta="MAC_FMT"\n"
931 			, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
932 		#endif
933 		return _FAIL;
934 	}
935 	*prxseq = seq_ctrl;
936 
937 	return _SUCCESS;
938 }
939 
process_pwrbit_data(_adapter * padapter,union recv_frame * precv_frame,struct sta_info * psta)940 void process_pwrbit_data(_adapter *padapter, union recv_frame *precv_frame, struct sta_info *psta)
941 {
942 #ifdef CONFIG_AP_MODE
943 	unsigned char pwrbit;
944 	u8 *ptr = precv_frame->u.hdr.rx_data;
945 
946 	pwrbit = GetPwrMgt(ptr);
947 
948 	if (pwrbit) {
949 		if (!(psta->state & WIFI_SLEEP_STATE)) {
950 			/* psta->state |= WIFI_SLEEP_STATE; */
951 			/* rtw_tim_map_set(padapter, pstapriv->sta_dz_bitmap, BIT(psta->cmn.aid)); */
952 
953 			stop_sta_xmit(padapter, psta);
954 			/* RTW_INFO_DUMP("to sleep, sta_dz_bitmap=", pstapriv->sta_dz_bitmap, pstapriv->aid_bmp_len); */
955 		}
956 	} else {
957 		if (psta->state & WIFI_SLEEP_STATE) {
958 			/* psta->state ^= WIFI_SLEEP_STATE; */
959 			/* rtw_tim_map_clear(padapter, pstapriv->sta_dz_bitmap, BIT(psta->cmn.aid)); */
960 
961 			wakeup_sta_to_xmit(padapter, psta, ALL_FRAME);
962 			/* RTW_INFO_DUMP("to wakeup, sta_dz_bitmap=", pstapriv->sta_dz_bitmap, pstapriv->aid_bmp_len); */
963 		}
964 	}
965 #endif
966 }
967 
process_wmmps_data(_adapter * padapter,union recv_frame * precv_frame,struct sta_info * psta)968 void process_wmmps_data(_adapter *padapter, union recv_frame *precv_frame, struct sta_info *psta)
969 {
970 #ifdef CONFIG_AP_MODE
971 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
972 
973 #ifdef CONFIG_TDLS
974 	if (!(psta->tdls_sta_state & TDLS_LINKED_STATE)) {
975 #endif /* CONFIG_TDLS */
976 
977 		if (!psta->qos_option)
978 			return;
979 
980 		if (!(psta->qos_info & 0xf))
981 			return;
982 
983 #ifdef CONFIG_TDLS
984 	}
985 #endif /* CONFIG_TDLS		 */
986 
987 	if (psta->state & WIFI_SLEEP_STATE) {
988 		u8 wmmps_ac = 0;
989 
990 		switch (pattrib->priority) {
991 		case 1:
992 		case 2:
993 			wmmps_ac = psta->uapsd_bk & BIT(1);
994 			break;
995 		case 4:
996 		case 5:
997 			wmmps_ac = psta->uapsd_vi & BIT(1);
998 			break;
999 		case 6:
1000 		case 7:
1001 			wmmps_ac = psta->uapsd_vo & BIT(1);
1002 			break;
1003 		case 0:
1004 		case 3:
1005 		default:
1006 			wmmps_ac = psta->uapsd_be & BIT(1);
1007 			break;
1008 		}
1009 
1010 		if (wmmps_ac) {
1011 			if (psta->sleepq_ac_len > 0) {
1012 				/* process received triggered frame */
1013 				xmit_delivery_enabled_frames(padapter, psta);
1014 			} else {
1015 				/* issue one qos null frame with More data bit = 0 and the EOSP bit set (=1) */
1016 				issue_qos_nulldata(padapter, psta->cmn.mac_addr, (u16)pattrib->priority, 0, 0, 0);
1017 			}
1018 		}
1019 
1020 	}
1021 
1022 
1023 #endif
1024 
1025 }
1026 
1027 #ifdef CONFIG_TDLS
OnTDLS(_adapter * adapter,union recv_frame * precv_frame)1028 sint OnTDLS(_adapter *adapter, union recv_frame *precv_frame)
1029 {
1030 	struct rx_pkt_attrib	*pattrib = &precv_frame->u.hdr.attrib;
1031 	sint ret = _SUCCESS;
1032 	u8 *paction = get_recvframe_data(precv_frame);
1033 	u8 category_field = 1;
1034 #ifdef CONFIG_WFD
1035 	u8 WFA_OUI[3] = { 0x50, 0x6f, 0x9a };
1036 #endif /* CONFIG_WFD */
1037 	struct tdls_info *ptdlsinfo = &(adapter->tdlsinfo);
1038 	u8 *ptr = precv_frame->u.hdr.rx_data;
1039 	struct sta_priv *pstapriv = &(adapter->stapriv);
1040 	struct sta_info *ptdls_sta = NULL;
1041 
1042 	/* point to action field */
1043 	paction += pattrib->hdrlen
1044 		   + pattrib->iv_len
1045 		   + SNAP_SIZE
1046 		   + ETH_TYPE_LEN
1047 		   + PAYLOAD_TYPE_LEN
1048 		   + category_field;
1049 
1050 	RTW_INFO("[TDLS] Recv %s from "MAC_FMT" with SeqNum = %d\n", rtw_tdls_action_txt(*paction), MAC_ARG(pattrib->src), GetSequence(get_recvframe_data(precv_frame)));
1051 
1052 	if (hal_chk_wl_func(adapter, WL_FUNC_TDLS) == _FALSE) {
1053 		RTW_INFO("Ignore tdls frame since hal doesn't support tdls\n");
1054 		ret = _FAIL;
1055 		return ret;
1056 	}
1057 
1058 	if (rtw_is_tdls_enabled(adapter) == _FALSE) {
1059 		RTW_INFO("recv tdls frame, "
1060 			 "but tdls haven't enabled\n");
1061 		ret = _FAIL;
1062 		return ret;
1063 	}
1064 
1065 	ptdls_sta = rtw_get_stainfo(pstapriv, get_sa(ptr));
1066 	if (ptdls_sta == NULL) {
1067 		switch (*paction) {
1068 		case TDLS_SETUP_REQUEST:
1069 		case TDLS_DISCOVERY_REQUEST:
1070 			break;
1071 		default:
1072 			RTW_INFO("[TDLS] %s - Direct Link Peer = "MAC_FMT" not found for action = %d\n", __func__, MAC_ARG(get_sa(ptr)), *paction);
1073 			ret = _FAIL;
1074 			goto exit;
1075 		}
1076 	}
1077 
1078 	switch (*paction) {
1079 	case TDLS_SETUP_REQUEST:
1080 		ret = On_TDLS_Setup_Req(adapter, precv_frame, ptdls_sta);
1081 		break;
1082 	case TDLS_SETUP_RESPONSE:
1083 		ret = On_TDLS_Setup_Rsp(adapter, precv_frame, ptdls_sta);
1084 		break;
1085 	case TDLS_SETUP_CONFIRM:
1086 		ret = On_TDLS_Setup_Cfm(adapter, precv_frame, ptdls_sta);
1087 		break;
1088 	case TDLS_TEARDOWN:
1089 		ret = On_TDLS_Teardown(adapter, precv_frame, ptdls_sta);
1090 		break;
1091 	case TDLS_DISCOVERY_REQUEST:
1092 		ret = On_TDLS_Dis_Req(adapter, precv_frame);
1093 		break;
1094 	case TDLS_PEER_TRAFFIC_INDICATION:
1095 		ret = On_TDLS_Peer_Traffic_Indication(adapter, precv_frame, ptdls_sta);
1096 		break;
1097 	case TDLS_PEER_TRAFFIC_RESPONSE:
1098 		ret = On_TDLS_Peer_Traffic_Rsp(adapter, precv_frame, ptdls_sta);
1099 		break;
1100 #ifdef CONFIG_TDLS_CH_SW
1101 	case TDLS_CHANNEL_SWITCH_REQUEST:
1102 		ret = On_TDLS_Ch_Switch_Req(adapter, precv_frame, ptdls_sta);
1103 		break;
1104 	case TDLS_CHANNEL_SWITCH_RESPONSE:
1105 		ret = On_TDLS_Ch_Switch_Rsp(adapter, precv_frame, ptdls_sta);
1106 		break;
1107 #endif
1108 #ifdef CONFIG_WFD
1109 	/* First byte of WFA OUI */
1110 	case 0x50:
1111 		if (_rtw_memcmp(WFA_OUI, paction, 3)) {
1112 			/* Probe request frame */
1113 			if (*(paction + 3) == 0x04) {
1114 				/* WFDTDLS: for sigma test, do not setup direct link automatically */
1115 				ptdlsinfo->dev_discovered = _TRUE;
1116 				RTW_INFO("recv tunneled probe request frame\n");
1117 				issue_tunneled_probe_rsp(adapter, precv_frame);
1118 			}
1119 			/* Probe response frame */
1120 			if (*(paction + 3) == 0x05) {
1121 				/* WFDTDLS: for sigma test, do not setup direct link automatically */
1122 				ptdlsinfo->dev_discovered = _TRUE;
1123 				RTW_INFO("recv tunneled probe response frame\n");
1124 			}
1125 		}
1126 		break;
1127 #endif /* CONFIG_WFD */
1128 	default:
1129 		RTW_INFO("receive TDLS frame %d but not support\n", *paction);
1130 		ret = _FAIL;
1131 		break;
1132 	}
1133 
1134 exit:
1135 	return ret;
1136 
1137 }
1138 
rtw_tdls_rx_data_validate_hdr(_adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)1139 sint rtw_tdls_rx_data_validate_hdr(
1140 	_adapter *adapter,
1141 	union recv_frame *precv_frame,
1142 	struct sta_info **psta
1143 )
1144 {
1145 	u8 *ptr = precv_frame->u.hdr.rx_data;
1146 	sint ret = _SUCCESS;
1147 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1148 	struct	sta_priv		*pstapriv = &adapter->stapriv;
1149 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
1150 	u8 *mybssid  = get_bssid(pmlmepriv);
1151 	u8 *myhwaddr = adapter_mac_addr(adapter);
1152 	u8 *sta_addr = pattrib->ta;
1153 	sint bmcast = IS_MCAST(pattrib->dst);
1154 
1155 	struct tdls_info *ptdlsinfo = &adapter->tdlsinfo;
1156 #ifdef CONFIG_TDLS_CH_SW
1157 	struct tdls_ch_switch *pchsw_info = &ptdlsinfo->chsw_info;
1158 #endif
1159 	struct sta_info *ptdls_sta = NULL;
1160 	u8 *psnap_type = ptr + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
1161 	/* frame body located after [+2]: ether-type, [+1]: payload type */
1162 	u8 *pframe_body = psnap_type + 2 + 1;
1163 
1164 	*psta = ptdls_sta = rtw_get_stainfo(pstapriv, pattrib->ta);
1165 	if (ptdls_sta == NULL) {
1166 		ret = _FAIL;
1167 		goto exit;
1168 	} else if (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE) {
1169 		/* filter packets that SA is myself or multicast or broadcast */
1170 		if (_rtw_memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
1171 			ret = _FAIL;
1172 			goto exit;
1173 		}
1174 		/* da should be for me */
1175 		if ((!_rtw_memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
1176 			ret = _FAIL;
1177 			goto exit;
1178 		}
1179 		/* check BSSID */
1180 		if (_rtw_memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1181 		    _rtw_memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1182 		    (!_rtw_memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
1183 			ret = _FAIL;
1184 			goto exit;
1185 		}
1186 
1187 #ifdef CONFIG_TDLS_CH_SW
1188 		if (ATOMIC_READ(&pchsw_info->chsw_on) == _TRUE) {
1189 			if (adapter->mlmeextpriv.cur_channel != rtw_get_oper_ch(adapter)) {
1190 				pchsw_info->ch_sw_state |= TDLS_PEER_AT_OFF_STATE;
1191 				if (!(pchsw_info->ch_sw_state & TDLS_CH_SW_INITIATOR_STATE))
1192 					_cancel_timer_ex(&ptdls_sta->ch_sw_timer);
1193 				/* On_TDLS_Peer_Traffic_Rsp(adapter, precv_frame); */
1194 			}
1195 		}
1196 #endif
1197 
1198 		/* process UAPSD tdls sta */
1199 		process_pwrbit_data(adapter, precv_frame, ptdls_sta);
1200 
1201 		/* if NULL-frame, check pwrbit */
1202 		if ((get_frame_sub_type(ptr) & WIFI_DATA_NULL) == WIFI_DATA_NULL) {
1203 			/* NULL-frame with pwrbit=1, buffer_STA should buffer frames for sleep_STA */
1204 			if (GetPwrMgt(ptr)) {
1205 				/* it would be triggered when we are off channel and receiving NULL DATA */
1206 				/* we can confirm that peer STA is at off channel */
1207 				RTW_INFO("TDLS: recv peer null frame with pwr bit 1\n");
1208 				/* ptdls_sta->tdls_sta_state|=TDLS_PEER_SLEEP_STATE; */
1209 			}
1210 
1211 			/* TODO: Updated BSSID's seq. */
1212 			/* RTW_INFO("drop Null Data\n"); */
1213 			ptdls_sta->tdls_sta_state &= ~(TDLS_WAIT_PTR_STATE);
1214 			ret = _FAIL;
1215 			goto exit;
1216 		}
1217 
1218 		/* receive some of all TDLS management frames, process it at ON_TDLS */
1219 		if (_rtw_memcmp(psnap_type, SNAP_ETH_TYPE_TDLS, 2)) {
1220 			ret = OnTDLS(adapter, precv_frame);
1221 			goto exit;
1222 		}
1223 
1224 		if ((get_frame_sub_type(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE)
1225 			process_wmmps_data(adapter, precv_frame, ptdls_sta);
1226 
1227 		ptdls_sta->tdls_sta_state &= ~(TDLS_WAIT_PTR_STATE);
1228 
1229 	}
1230 
1231 exit:
1232 	return ret;
1233 }
1234 #endif /* CONFIG_TDLS */
1235 
count_rx_stats(_adapter * padapter,union recv_frame * prframe,struct sta_info * sta)1236 void count_rx_stats(_adapter *padapter, union recv_frame *prframe, struct sta_info *sta)
1237 {
1238 	int	sz;
1239 	struct sta_info		*psta = NULL;
1240 	struct stainfo_stats	*pstats = NULL;
1241 	struct rx_pkt_attrib	*pattrib = &prframe->u.hdr.attrib;
1242 	struct recv_priv		*precvpriv = &padapter->recvpriv;
1243 
1244 	sz = get_recvframe_len(prframe);
1245 	precvpriv->rx_bytes += sz;
1246 
1247 	padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
1248 
1249 	if ((!MacAddr_isBcst(pattrib->dst)) && (!IS_MCAST(pattrib->dst)))
1250 		padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
1251 
1252 	if (sta)
1253 		psta = sta;
1254 	else
1255 		psta = prframe->u.hdr.psta;
1256 
1257 	if (psta) {
1258 		u8 is_ra_bmc = IS_MCAST(pattrib->ra);
1259 
1260 		pstats = &psta->sta_stats;
1261 
1262 		pstats->last_rx_time = rtw_get_current_time();
1263 		pstats->rx_data_pkts++;
1264 		pstats->rx_bytes += sz;
1265 		if (is_broadcast_mac_addr(pattrib->ra)) {
1266 			pstats->rx_data_bc_pkts++;
1267 			pstats->rx_bc_bytes += sz;
1268 		} else if (is_ra_bmc) {
1269 			pstats->rx_data_mc_pkts++;
1270 			pstats->rx_mc_bytes += sz;
1271 		}
1272 
1273 		if (!is_ra_bmc) {
1274 			pstats->rxratecnt[pattrib->data_rate]++;
1275 			/*record rx packets for every tid*/
1276 			pstats->rx_data_qos_pkts[pattrib->priority]++;
1277 		}
1278 #ifdef CONFIG_DYNAMIC_SOML
1279 		rtw_dyn_soml_byte_update(padapter, pattrib->data_rate, sz);
1280 #endif
1281 #if defined(CONFIG_CHECK_LEAVE_LPS) && defined(CONFIG_LPS_CHK_BY_TP)
1282 		if (adapter_to_pwrctl(padapter)->lps_chk_by_tp)
1283 			traffic_check_for_leave_lps_by_tp(padapter, _FALSE, psta);
1284 #endif /* CONFIG_LPS */
1285 
1286 	}
1287 
1288 #ifdef CONFIG_CHECK_LEAVE_LPS
1289 #ifdef CONFIG_LPS_CHK_BY_TP
1290 	if (!adapter_to_pwrctl(padapter)->lps_chk_by_tp)
1291 #endif
1292 		traffic_check_for_leave_lps(padapter, _FALSE, 0);
1293 #endif /* CONFIG_CHECK_LEAVE_LPS */
1294 
1295 }
1296 
rtw_sta_rx_data_validate_hdr(_adapter * adapter,union recv_frame * rframe,struct sta_info ** sta)1297 int rtw_sta_rx_data_validate_hdr(_adapter *adapter, union recv_frame *rframe, struct sta_info **sta)
1298 {
1299 	struct sta_priv *stapriv = &adapter->stapriv;
1300 	u8 *mybssid  = get_bssid(&adapter->mlmepriv);
1301 	u8 *myhwaddr = adapter_mac_addr(adapter);
1302 	struct rx_pkt_attrib *rattrib = &rframe->u.hdr.attrib;
1303 	u8 *whdr = get_recvframe_data(rframe);
1304 	u8 is_ra_bmc = IS_MCAST(GetAddr1Ptr(whdr)) ? 1 : 0;
1305 	sint ret = _FAIL;
1306 
1307 	if (rattrib->to_fr_ds == 0) {
1308 		_rtw_memcpy(rattrib->ra, GetAddr1Ptr(whdr), ETH_ALEN);
1309 		_rtw_memcpy(rattrib->ta, get_addr2_ptr(whdr), ETH_ALEN);
1310 		_rtw_memcpy(rattrib->dst, GetAddr1Ptr(whdr), ETH_ALEN);
1311 		_rtw_memcpy(rattrib->src, get_addr2_ptr(whdr), ETH_ALEN);
1312 		_rtw_memcpy(rattrib->bssid, GetAddr3Ptr(whdr), ETH_ALEN);
1313 
1314 		#ifdef CONFIG_TDLS
1315 		if (adapter->tdlsinfo.link_established == _TRUE)
1316 			ret = rtw_tdls_rx_data_validate_hdr(adapter, rframe, sta);
1317 		else
1318 		#endif
1319 		{
1320 			/* For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
1321 			if (!_rtw_memcmp(rattrib->bssid, rattrib->src, ETH_ALEN))
1322 				goto exit;
1323 
1324 			*sta = rtw_get_stainfo(stapriv, get_addr2_ptr(whdr));
1325 			if (*sta)
1326 				ret = _SUCCESS;
1327 		}
1328 		goto exit;
1329 	}
1330 
1331 	if (!(MLME_STATE(adapter) & (WIFI_ASOC_STATE | WIFI_UNDER_LINKING))) {
1332 		if (!is_ra_bmc) {
1333 			/* for AP multicast issue , modify by yiwei */
1334 			static systime send_issue_deauth_time = 0;
1335 
1336 			/* RTW_INFO("After send deauth , %u ms has elapsed.\n", rtw_get_passing_time_ms(send_issue_deauth_time)); */
1337 			if (rtw_get_passing_time_ms(send_issue_deauth_time) > 10000 || send_issue_deauth_time == 0) {
1338 				send_issue_deauth_time = rtw_get_current_time();
1339 				RTW_INFO(FUNC_ADPT_FMT" issue_deauth to "MAC_FMT" with reason(7), mlme_state:0x%x\n"
1340 					, FUNC_ADPT_ARG(adapter), MAC_ARG(get_addr2_ptr(whdr)), MLME_STATE(adapter));
1341 				issue_deauth(adapter, get_addr2_ptr(whdr), WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1342 			}
1343 		}
1344 		#ifdef DBG_RX_DROP_FRAME
1345 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" fw_state:0x%x\n"
1346 			, FUNC_ADPT_ARG(adapter), MLME_STATE(adapter));
1347 		#endif
1348 		goto exit;
1349 	}
1350 
1351 	_rtw_memcpy(rattrib->ra, GetAddr1Ptr(whdr), ETH_ALEN);
1352 	_rtw_memcpy(rattrib->ta, get_addr2_ptr(whdr), ETH_ALEN);
1353 
1354 	switch (rattrib->to_fr_ds) {
1355 	case 2:
1356 		_rtw_memcpy(rattrib->dst, GetAddr1Ptr(whdr), ETH_ALEN);
1357 		_rtw_memcpy(rattrib->src, GetAddr3Ptr(whdr), ETH_ALEN); /* may change after checking AMSDU subframe header */
1358 		_rtw_memcpy(rattrib->bssid, get_addr2_ptr(whdr), ETH_ALEN);
1359 		break;
1360 	case 3:
1361 		_rtw_memcpy(rattrib->dst, GetAddr3Ptr(whdr), ETH_ALEN); /* may change after checking AMSDU subframe header */
1362 		_rtw_memcpy(rattrib->src, GetAddr4Ptr(whdr), ETH_ALEN); /* may change after checking AMSDU subframe header */
1363 		_rtw_memcpy(rattrib->bssid, get_addr2_ptr(whdr), ETH_ALEN);
1364 		break;
1365 	default:
1366 		ret = RTW_RX_HANDLED; /* don't count for drop */
1367 		goto exit;
1368 	}
1369 
1370 	/* filter packets that SA is myself */
1371 	if (!rattrib->amsdu && _rtw_memcmp(myhwaddr, rattrib->src, ETH_ALEN)) {
1372 		#ifdef DBG_RX_DROP_FRAME
1373 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" SA="MAC_FMT", myhwaddr="MAC_FMT"\n"
1374 			, FUNC_ADPT_ARG(adapter), MAC_ARG(rattrib->src), MAC_ARG(myhwaddr));
1375 		#endif
1376 		goto exit;
1377 	}
1378 
1379 	*sta = rtw_get_stainfo(stapriv, rattrib->ta);
1380 	if (*sta == NULL) {
1381 		#ifndef CONFIG_CUSTOMER_ALIBABA_GENERAL
1382 		if (!is_ra_bmc && !IS_RADAR_DETECTED(adapter_to_rfctl(adapter))) {
1383 			RTW_INFO(FUNC_ADPT_FMT" issue_deauth to "MAC_FMT" with reason(7), unknown TA\n"
1384 				, FUNC_ADPT_ARG(adapter), MAC_ARG(rattrib->ta));
1385 			issue_deauth(adapter, rattrib->ta, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1386 		}
1387 		#endif
1388 		#ifdef DBG_RX_DROP_FRAME
1389 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" can't get psta under STATION_MODE ; drop pkt\n"
1390 			, FUNC_ADPT_ARG(adapter));
1391 		#endif
1392 		goto exit;
1393 	}
1394 
1395 #ifdef CONFIG_RTW_WDS_AUTO_EN
1396 	if (rattrib->to_fr_ds == 3 && !(sta->flags & WLAN_STA_WDS))
1397 		sta->flags |= WLAN_STA_WDS;
1398 #endif
1399 
1400 	/*if ((get_frame_sub_type(whdr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
1401 	}
1402 	*/
1403 
1404 	if (get_frame_sub_type(whdr) & BIT(6)) {
1405 		/* No data, will not indicate to upper layer, temporily count it here */
1406 		count_rx_stats(adapter, rframe, *sta);
1407 		ret = RTW_RX_HANDLED;
1408 		goto exit;
1409 	}
1410 
1411 #ifdef CONFIG_RTW_WDS
1412 	if (adapter_use_wds(adapter)
1413 		&& !rattrib->amsdu && IS_MCAST(rattrib->dst)
1414 		&& rtw_rx_wds_gptr_check(adapter, rattrib->src)
1415 	) {
1416 		/* will not indicate to upper layer, temporily count it here */
1417 		count_rx_stats(adapter, rframe, *sta);
1418 		ret = RTW_RX_HANDLED;
1419 		goto exit;
1420 	}
1421 #endif
1422 
1423 	ret = _SUCCESS;
1424 
1425 exit:
1426 	return ret;
1427 }
1428 
rtw_sta_rx_amsdu_act_check(union recv_frame * rframe,const u8 * da,const u8 * sa)1429 int rtw_sta_rx_amsdu_act_check(union recv_frame *rframe
1430 	, const u8 *da, const u8 *sa)
1431 {
1432 	int act = RTW_RX_MSDU_ACT_INDICATE;
1433 
1434 #ifdef CONFIG_RTW_WDS
1435 	_adapter *adapter = rframe->u.hdr.adapter;
1436 
1437 	if (adapter_use_wds(adapter)
1438 		&& IS_MCAST(da)
1439 		&& rtw_rx_wds_gptr_check(adapter, sa)
1440 	) {
1441 		act = 0;
1442 	}
1443 #endif
1444 
1445 	return act;
1446 }
1447 
sta2sta_data_frame(_adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)1448 sint sta2sta_data_frame(
1449 	_adapter *adapter,
1450 	union recv_frame *precv_frame,
1451 	struct sta_info **psta
1452 )
1453 {
1454 	u8 *ptr = precv_frame->u.hdr.rx_data;
1455 	sint ret = _SUCCESS;
1456 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1457 	struct	sta_priv		*pstapriv = &adapter->stapriv;
1458 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
1459 	u8 *mybssid  = get_bssid(pmlmepriv);
1460 	u8 *myhwaddr = adapter_mac_addr(adapter);
1461 	u8 *sta_addr = pattrib->ta;
1462 	sint bmcast = IS_MCAST(pattrib->dst);
1463 
1464 	/* RTW_INFO("[%s] %d, seqnum:%d\n", __FUNCTION__, __LINE__, pattrib->seq_num); */
1465 
1466 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE) ||
1467 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE)) {
1468 
1469 		/* filter packets that SA is myself or multicast or broadcast */
1470 		if (_rtw_memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
1471 			ret = _FAIL;
1472 			goto exit;
1473 		}
1474 
1475 		if ((!_rtw_memcmp(myhwaddr, pattrib->dst, ETH_ALEN))	&& (!bmcast)) {
1476 			ret = _FAIL;
1477 			goto exit;
1478 		}
1479 
1480 		if (_rtw_memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1481 		    _rtw_memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1482 		    (!_rtw_memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
1483 			ret = _FAIL;
1484 			goto exit;
1485 		}
1486 
1487 	} else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE) {
1488 		_rtw_memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
1489 		_rtw_memcpy(pattrib->src, get_addr2_ptr(ptr), ETH_ALEN);
1490 		_rtw_memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
1491 		_rtw_memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
1492 		_rtw_memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
1493 
1494 		sta_addr = mybssid;
1495 	} else
1496 		ret  = _FAIL;
1497 
1498 	*psta = rtw_get_stainfo(pstapriv, sta_addr);
1499 	if (*psta == NULL) {
1500 #ifdef CONFIG_MP_INCLUDED
1501 		if (adapter->registrypriv.mp_mode == 1) {
1502 			if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE)
1503 				adapter->mppriv.rx_pktloss++;
1504 		}
1505 #endif
1506 		ret = _FAIL;
1507 		goto exit;
1508 	}
1509 
1510 exit:
1511 	return ret;
1512 }
1513 
ap2sta_data_frame(_adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)1514 sint ap2sta_data_frame(
1515 	_adapter *adapter,
1516 	union recv_frame *precv_frame,
1517 	struct sta_info **psta)
1518 {
1519 	u8 *ptr = precv_frame->u.hdr.rx_data;
1520 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1521 	sint ret = _SUCCESS;
1522 	struct	sta_priv		*pstapriv = &adapter->stapriv;
1523 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
1524 	u8 *myhwaddr = adapter_mac_addr(adapter);
1525 	sint bmcast = IS_MCAST(pattrib->dst);
1526 
1527 	if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE) &&
1528 		   (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE)) {
1529 		_rtw_memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
1530 		_rtw_memcpy(pattrib->src, get_addr2_ptr(ptr), ETH_ALEN);
1531 		_rtw_memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
1532 		_rtw_memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
1533 		_rtw_memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
1534 
1535 
1536 		*psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /* get sta_info */
1537 		if (*psta == NULL) {
1538 			#ifdef DBG_RX_DROP_FRAME
1539 			RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" can't get psta under WIFI_MP_STATE ; drop pkt\n"
1540 				, FUNC_ADPT_ARG(adapter));
1541 			#endif
1542 			ret = _FAIL;
1543 			goto exit;
1544 		}
1545 
1546 	} else {
1547 		if (_rtw_memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
1548 			*psta = rtw_get_stainfo(pstapriv, pattrib->ta);
1549 			if (*psta == NULL) {
1550 
1551 				/* for AP multicast issue , modify by yiwei */
1552 				static systime send_issue_deauth_time = 0;
1553 
1554 				/* RTW_INFO("After send deauth , %u ms has elapsed.\n", rtw_get_passing_time_ms(send_issue_deauth_time)); */
1555 
1556 				if (rtw_get_passing_time_ms(send_issue_deauth_time) > 10000 || send_issue_deauth_time == 0) {
1557 					send_issue_deauth_time = rtw_get_current_time();
1558 
1559 					RTW_INFO("issue_deauth to the ap=" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
1560 
1561 					issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1562 				}
1563 			}
1564 		}
1565 
1566 		ret = _FAIL;
1567 		#ifdef DBG_RX_DROP_FRAME
1568 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" fw_state:0x%x\n"
1569 			, FUNC_ADPT_ARG(adapter), get_fwstate(pmlmepriv));
1570 		#endif
1571 	}
1572 
1573 exit:
1574 	return ret;
1575 
1576 }
1577 
sta2ap_data_frame(_adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)1578 sint sta2ap_data_frame(
1579 	_adapter *adapter,
1580 	union recv_frame *precv_frame,
1581 	struct sta_info **psta)
1582 {
1583 	u8 *ptr = precv_frame->u.hdr.rx_data;
1584 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1585 	struct	sta_priv		*pstapriv = &adapter->stapriv;
1586 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
1587 	unsigned char *mybssid  = get_bssid(pmlmepriv);
1588 	sint ret = _SUCCESS;
1589 
1590 	if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE) &&
1591 		   (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE)) {
1592 		/* RTW_INFO("%s ,in WIFI_MP_STATE\n",__func__); */
1593 		_rtw_memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
1594 		_rtw_memcpy(pattrib->src, get_addr2_ptr(ptr), ETH_ALEN);
1595 		_rtw_memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
1596 		_rtw_memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
1597 		_rtw_memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
1598 
1599 
1600 		*psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /* get sta_info */
1601 		if (*psta == NULL) {
1602 			#ifdef DBG_RX_DROP_FRAME
1603 			RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" can't get psta under WIFI_MP_STATE ; drop pkt\n"
1604 				, FUNC_ADPT_ARG(adapter));
1605 			#endif
1606 			ret = _FAIL;
1607 			goto exit;
1608 		}
1609 
1610 	} else {
1611 		u8 *myhwaddr = adapter_mac_addr(adapter);
1612 		if (!_rtw_memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
1613 			ret = RTW_RX_HANDLED;
1614 			goto exit;
1615 		}
1616 #ifndef CONFIG_CUSTOMER_ALIBABA_GENERAL
1617 		RTW_INFO("issue_deauth to sta=" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1618 		issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1619 #endif
1620 		ret = RTW_RX_HANDLED;
1621 		goto exit;
1622 	}
1623 
1624 exit:
1625 
1626 
1627 	return ret;
1628 
1629 }
1630 
1631 sint validate_recv_ctrl_frame(_adapter *padapter, union recv_frame *precv_frame);
validate_recv_ctrl_frame(_adapter * padapter,union recv_frame * precv_frame)1632 sint validate_recv_ctrl_frame(_adapter *padapter, union recv_frame *precv_frame)
1633 {
1634 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1635 	struct sta_priv *pstapriv = &padapter->stapriv;
1636 	u8 *pframe = precv_frame->u.hdr.rx_data;
1637 	struct sta_info *psta = NULL;
1638 	u8 dataq_null = _TRUE;
1639 	#ifdef CONFIG_RTW_MGMT_QUEUE
1640 	u8 mgmtq_null = _TRUE;
1641 	#endif
1642 
1643 	/* uint len = precv_frame->u.hdr.len; */
1644 
1645 	/* RTW_INFO("+validate_recv_ctrl_frame\n"); */
1646 
1647 	if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
1648 		return _FAIL;
1649 
1650 	/* receive the frames that ra(a1) is my address */
1651 	if (!_rtw_memcmp(GetAddr1Ptr(pframe), adapter_mac_addr(padapter), ETH_ALEN))
1652 		return _FAIL;
1653 
1654 	psta = rtw_get_stainfo(pstapriv, get_addr2_ptr(pframe));
1655 	if (psta == NULL)
1656 		return _FAIL;
1657 
1658 	/* for rx pkt statistics */
1659 	psta->sta_stats.last_rx_time = rtw_get_current_time();
1660 	psta->sta_stats.rx_ctrl_pkts++;
1661 
1662 	/* only handle ps-poll */
1663 	if (get_frame_sub_type(pframe) == WIFI_PSPOLL) {
1664 #ifdef CONFIG_AP_MODE
1665 		u16 aid;
1666 		u8 wmmps_ac = 0;
1667 
1668 		aid = GetAid(pframe);
1669 		if (psta->cmn.aid != aid)
1670 			return _FAIL;
1671 
1672 		switch (pattrib->priority) {
1673 		case 1:
1674 		case 2:
1675 			wmmps_ac = psta->uapsd_bk & BIT(0);
1676 			break;
1677 		case 4:
1678 		case 5:
1679 			wmmps_ac = psta->uapsd_vi & BIT(0);
1680 			break;
1681 		case 6:
1682 		case 7:
1683 			wmmps_ac = psta->uapsd_vo & BIT(0);
1684 			break;
1685 		case 0:
1686 		case 3:
1687 		default:
1688 			wmmps_ac = psta->uapsd_be & BIT(0);
1689 			break;
1690 		}
1691 
1692 		if (wmmps_ac)
1693 			return _FAIL;
1694 
1695 		if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1696 			RTW_INFO("%s alive check-rx ps-poll\n", __func__);
1697 			psta->expire_to = pstapriv->expire_to;
1698 			psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1699 		}
1700 
1701 		if ((psta->state & WIFI_SLEEP_STATE) && (rtw_tim_map_is_set(padapter, pstapriv->sta_dz_bitmap, psta->cmn.aid))) {
1702 			_irqL irqL;
1703 			_list	*xmitframe_plist, *xmitframe_phead;
1704 			struct xmit_frame *pxmitframe = NULL;
1705 			struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1706 
1707 			/* _enter_critical_bh(&psta->sleep_q.lock, &irqL); */
1708 			_enter_critical_bh(&pxmitpriv->lock, &irqL);
1709 
1710 #ifdef CONFIG_RTW_MGMT_QUEUE
1711 			xmitframe_phead = get_list_head(&psta->mgmt_sleep_q);
1712 			xmitframe_plist = get_next(xmitframe_phead);
1713 
1714 			if ((rtw_end_of_queue_search(xmitframe_phead, xmitframe_plist)) == _FALSE) {
1715 				pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
1716 
1717 				xmitframe_plist = get_next(xmitframe_plist);
1718 
1719 				rtw_list_delete(&pxmitframe->list);
1720 
1721 				#ifdef DBG_MGMT_QUEUE
1722 				RTW_INFO("%s seq_num = %u, subtype = 0x%x\n",
1723 						__func__, pxmitframe->attrib.seqnum, pxmitframe->attrib.subtype);
1724 				#endif
1725 
1726 				psta->mgmt_sleepq_len--;
1727 
1728 				if (psta->mgmt_sleepq_len > 0)
1729 					pxmitframe->attrib.mdata = 1;
1730 				else
1731 					pxmitframe->attrib.mdata = 0;
1732 
1733 				pxmitframe->attrib.triggered = 1;
1734 
1735 				rtw_hal_mgmt_xmitframe_enqueue(padapter, pxmitframe);
1736 
1737 				mgmtq_null = _FALSE;
1738 			}
1739 #endif
1740 			xmitframe_phead = get_list_head(&psta->sleep_q);
1741 			xmitframe_plist = get_next(xmitframe_phead);
1742 
1743 			if ((rtw_end_of_queue_search(xmitframe_phead, xmitframe_plist)) == _FALSE) {
1744 				pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
1745 
1746 				xmitframe_plist = get_next(xmitframe_plist);
1747 
1748 				rtw_list_delete(&pxmitframe->list);
1749 
1750 				psta->sleepq_len--;
1751 
1752 				if (psta->sleepq_len > 0)
1753 					pxmitframe->attrib.mdata = 1;
1754 				else
1755 					pxmitframe->attrib.mdata = 0;
1756 
1757 				pxmitframe->attrib.triggered = 1;
1758 
1759 				/* RTW_INFO("handling ps-poll, q_len=%d\n", psta->sleepq_len); */
1760 				/* RTW_INFO_DUMP("handling, tim=", pstapriv->tim_bitmap, pstapriv->aid_bmp_len); */
1761 
1762 #if 0
1763 				_exit_critical_bh(&psta->sleep_q.lock, &irqL);
1764 				if (rtw_hal_xmit(padapter, pxmitframe) == _TRUE)
1765 					rtw_os_xmit_complete(padapter, pxmitframe);
1766 				_enter_critical_bh(&psta->sleep_q.lock, &irqL);
1767 #endif
1768 				rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
1769 
1770 				dataq_null = _FALSE;
1771 			}
1772 
1773 			_exit_critical_bh(&pxmitpriv->lock, &irqL);
1774 			/* _exit_critical_bh(&psta->sleep_q.lock, &irqL); */
1775 
1776 			/* Error handler : TIM is setted, but data queue and management queue is empty. */
1777 			if (dataq_null
1778 				#ifdef CONFIG_RTW_MGMT_QUEUE
1779 				&& mgmtq_null
1780 				#endif
1781 				&& rtw_tim_map_is_set(padapter, pstapriv->tim_bitmap, psta->cmn.aid)) {
1782 
1783 				if (psta->sleepq_len == 0
1784 					#ifdef CONFIG_RTW_MGMT_QUEUE
1785 					&& psta->mgmt_sleepq_len == 0
1786 					#endif
1787 					) {
1788 					RTW_INFO("No buffered packets to xmit\n");
1789 
1790 					/* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
1791 					issue_nulldata(padapter, psta->cmn.mac_addr, 0, 0, 0);
1792 				} else {
1793 					if (psta->sleepq_len != 0) {
1794 						RTW_INFO("error!psta->sleepq_len=%d\n", psta->sleepq_len);
1795 						psta->sleepq_len = 0;
1796 					}
1797 					#ifdef CONFIG_RTW_MGMT_QUEUE
1798 					if (psta->mgmt_sleepq_len != 0) {
1799 						RTW_INFO("error!psta->mgmt_sleepq_len=%d\n", psta->mgmt_sleepq_len);
1800 						psta->mgmt_sleepq_len = 0;
1801 					}
1802 					#endif
1803 				}
1804 			}
1805 
1806 			if (psta->sleepq_len == 0
1807 			#ifdef CONFIG_RTW_MGMT_QUEUE
1808 			&& psta->mgmt_sleepq_len == 0
1809 			#endif
1810 			) {
1811 				rtw_tim_map_clear(padapter, pstapriv->tim_bitmap, psta->cmn.aid);
1812 
1813 				/* upate BCN for TIM IE */
1814 				update_beacon(padapter, _TIM_IE_, NULL, _TRUE, 0);
1815 			}
1816 		}
1817 #endif /* CONFIG_AP_MODE */
1818 	} else if (get_frame_sub_type(pframe) == WIFI_NDPA) {
1819 #ifdef CONFIG_BEAMFORMING
1820 		rtw_beamforming_get_ndpa_frame(padapter, precv_frame);
1821 #endif/*CONFIG_BEAMFORMING*/
1822 	} else if (get_frame_sub_type(pframe) == WIFI_BAR) {
1823 		rtw_process_bar_frame(padapter, precv_frame);
1824 	}
1825 
1826 	return _FAIL;
1827 
1828 }
1829 
1830 #if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH)
validate_mgmt_protect(_adapter * adapter,union recv_frame * precv_frame)1831 static sint validate_mgmt_protect(_adapter *adapter, union recv_frame *precv_frame)
1832 {
1833 #define DBG_VALIDATE_MGMT_PROTECT 0
1834 #define DBG_VALIDATE_MGMT_DEC 0
1835 
1836 	struct security_priv *sec = &adapter->securitypriv;
1837 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1838 	struct sta_info	*psta = precv_frame->u.hdr.psta;
1839 	u8 *ptr;
1840 	u8 type;
1841 	u8 subtype;
1842 	u8 is_bmc;
1843 	u8 category = 0xFF;
1844 
1845 #ifdef CONFIG_IEEE80211W
1846 	const u8 *igtk;
1847 	u16 igtk_id;
1848 	u64* ipn;
1849 	enum security_type bip_cipher;
1850 #endif
1851 
1852 	u8 *mgmt_DATA;
1853 	u32 data_len = 0;
1854 
1855 	sint ret;
1856 
1857 #ifdef CONFIG_RTW_MESH
1858 	if (MLME_IS_MESH(adapter)) {
1859 		if (!adapter->mesh_info.mesh_auth_id)
1860 			return pattrib->privacy ? _FAIL : _SUCCESS;
1861 	} else
1862 #endif
1863 	if (SEC_IS_BIP_KEY_INSTALLED(sec) == _FALSE)
1864 		return _SUCCESS;
1865 
1866 	ptr = precv_frame->u.hdr.rx_data;
1867 	type = GetFrameType(ptr);
1868 	subtype = get_frame_sub_type(ptr); /* bit(7)~bit(2) */
1869 	is_bmc = IS_MCAST(GetAddr1Ptr(ptr));
1870 
1871 #if DBG_VALIDATE_MGMT_PROTECT
1872 	if (subtype == WIFI_DEAUTH) {
1873 		RTW_INFO(FUNC_ADPT_FMT" bmc:%u, deauth, privacy:%u, encrypt:%u, bdecrypted:%u\n"
1874 			, FUNC_ADPT_ARG(adapter)
1875 			, is_bmc, pattrib->privacy, pattrib->encrypt, pattrib->bdecrypted);
1876 	} else if (subtype == WIFI_DISASSOC) {
1877 		RTW_INFO(FUNC_ADPT_FMT" bmc:%u, disassoc, privacy:%u, encrypt:%u, bdecrypted:%u\n"
1878 			, FUNC_ADPT_ARG(adapter)
1879 			, is_bmc, pattrib->privacy, pattrib->encrypt, pattrib->bdecrypted);
1880 	} if (subtype == WIFI_ACTION) {
1881 		if (pattrib->privacy) {
1882 			RTW_INFO(FUNC_ADPT_FMT" bmc:%u, action(?), privacy:%u, encrypt:%u, bdecrypted:%u\n"
1883 				, FUNC_ADPT_ARG(adapter)
1884 				, is_bmc, pattrib->privacy, pattrib->encrypt, pattrib->bdecrypted);
1885 		} else {
1886 			RTW_INFO(FUNC_ADPT_FMT" bmc:%u, action(%u), privacy:%u, encrypt:%u, bdecrypted:%u\n"
1887 				, FUNC_ADPT_ARG(adapter), is_bmc
1888 				, *(ptr + sizeof(struct rtw_ieee80211_hdr_3addr))
1889 				, pattrib->privacy, pattrib->encrypt, pattrib->bdecrypted);
1890 		}
1891 	}
1892 #endif
1893 
1894 	if (!pattrib->privacy) {
1895 		if (!psta || !(psta->flags & WLAN_STA_MFP)) {
1896 			/* peer is not MFP capable, no need to check */
1897 			goto exit;
1898 		}
1899 
1900 		if (subtype == WIFI_ACTION)
1901 			category = *(ptr + sizeof(struct rtw_ieee80211_hdr_3addr));
1902 
1903 		if (is_bmc) {
1904 			/* broadcast cases */
1905 			if (subtype == WIFI_ACTION) {
1906 				if (CATEGORY_IS_GROUP_PRIVACY(category)) {
1907 					/* drop broadcast group privacy action frame without encryption */
1908 					#if DBG_VALIDATE_MGMT_PROTECT
1909 					RTW_INFO(FUNC_ADPT_FMT" broadcast gp action(%u) w/o encrypt\n"
1910 						, FUNC_ADPT_ARG(adapter), category);
1911 					#endif
1912 					goto fail;
1913 				}
1914 				if (CATEGORY_IS_ROBUST(category)) {
1915 					/* broadcast robust action frame need BIP check */
1916 					goto bip_verify;
1917 				}
1918 			}
1919 			if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC) {
1920 				/* broadcast deauth or disassoc frame need BIP check */
1921 				goto bip_verify;
1922 			}
1923 			goto exit;
1924 
1925 		} else {
1926 			/* unicast cases */
1927 			#ifdef CONFIG_IEEE80211W
1928 			if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC) {
1929 				if (!MLME_IS_MESH(adapter)) {
1930 					unsigned short reason = le16_to_cpu(*(unsigned short *)(ptr + WLAN_HDR_A3_LEN));
1931 
1932 					#if DBG_VALIDATE_MGMT_PROTECT
1933 					RTW_INFO(FUNC_ADPT_FMT" unicast %s, reason=%d w/o encrypt\n"
1934 						, FUNC_ADPT_ARG(adapter), subtype == WIFI_DEAUTH ? "deauth" : "disassoc", reason);
1935 					#endif
1936 					if (reason == 6 || reason == 7) {
1937 						/* issue sa query request */
1938 						issue_action_SA_Query(adapter, psta->cmn.mac_addr, 0, 0, IEEE80211W_RIGHT_KEY);
1939 					}
1940 				}
1941 				goto fail;
1942 			}
1943 			#endif
1944 
1945 			if (subtype == WIFI_ACTION && CATEGORY_IS_ROBUST(category)) {
1946 				if (psta->bpairwise_key_installed == _TRUE) {
1947 					#if DBG_VALIDATE_MGMT_PROTECT
1948 					RTW_INFO(FUNC_ADPT_FMT" unicast robust action(%d) w/o encrypt\n"
1949 						, FUNC_ADPT_ARG(adapter), category);
1950 					#endif
1951 					goto fail;
1952 				}
1953 			}
1954 			goto exit;
1955 		}
1956 
1957 bip_verify:
1958 #ifdef CONFIG_IEEE80211W
1959 		#ifdef CONFIG_RTW_MESH
1960 		if (MLME_IS_MESH(adapter)) {
1961 			if (psta->igtk_bmp) {
1962 				bip_cipher = psta->dot11wCipher;
1963 				igtk = psta->igtk.skey;
1964 				igtk_id = psta->igtk_id;
1965 				ipn = &psta->igtk_pn.val;
1966 			} else {
1967 				/* mesh MFP without IGTK */
1968 				goto exit;
1969 			}
1970 		} else
1971 		#endif
1972 		{
1973 			bip_cipher = sec->dot11wCipher;
1974 			igtk = sec->dot11wBIPKey[sec->dot11wBIPKeyid].skey;
1975 			igtk_id = sec->dot11wBIPKeyid;
1976 			ipn = &sec->dot11wBIPrxpn.val;
1977 		}
1978 
1979 		/* verify BIP MME IE */
1980 		ret = rtw_bip_verify(bip_cipher, pattrib->pkt_len,
1981 			get_recvframe_data(precv_frame),
1982 			get_recvframe_len(precv_frame),
1983 			igtk, igtk_id, ipn);
1984 
1985 		if (ret == _FAIL) {
1986 			/* RTW_INFO("802.11w BIP verify fail\n"); */
1987 			goto fail;
1988 
1989 		} else if (ret == RTW_RX_HANDLED) {
1990 			#if DBG_VALIDATE_MGMT_PROTECT
1991 			RTW_INFO(FUNC_ADPT_FMT" none protected packet\n", FUNC_ADPT_ARG(adapter));
1992 			#endif
1993 			goto fail;
1994 		}
1995 #endif /* CONFIG_IEEE80211W */
1996 		goto exit;
1997 	}
1998 
1999 	if (!psta || !(psta->flags & WLAN_STA_MFP)) {
2000 		/* not peer or peer is not MFP capable, drop it */
2001 		goto fail;
2002 	}
2003 
2004 	/* cases to decrypt mgmt frame */
2005 	pattrib->bdecrypted = 0;
2006 #ifdef CONFIG_RTW_MESH
2007 	if (is_bmc)
2008 		pattrib->encrypt = psta->group_privacy;
2009 	else
2010 #endif
2011 	pattrib->encrypt = psta->dot118021XPrivacy;
2012 	pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
2013 
2014 	/* set iv and icv length */
2015 	SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
2016 	_rtw_memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
2017 	_rtw_memcpy(pattrib->ta, get_addr2_ptr(ptr), ETH_ALEN);
2018 
2019 	/* actual management data frame body */
2020 	data_len = pattrib->pkt_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
2021 	mgmt_DATA = rtw_zmalloc(data_len);
2022 	if (mgmt_DATA == NULL) {
2023 		RTW_INFO(FUNC_ADPT_FMT" mgmt allocate fail  !!!!!!!!!\n", FUNC_ADPT_ARG(adapter));
2024 		goto fail;
2025 	}
2026 
2027 #if DBG_VALIDATE_MGMT_DEC
2028 	/* dump the packet content before decrypt */
2029 	{
2030 		int pp;
2031 
2032 		printk("pattrib->pktlen = %d =>", pattrib->pkt_len);
2033 		for (pp = 0; pp < pattrib->pkt_len; pp++)
2034 		printk(" %02x ", ptr[pp]);
2035 		printk("\n");
2036 	}
2037 #endif
2038 
2039 	precv_frame = decryptor(adapter, precv_frame);
2040 	/* save actual management data frame body */
2041 	_rtw_memcpy(mgmt_DATA, ptr + pattrib->hdrlen + pattrib->iv_len, data_len);
2042 	/* overwrite the iv field */
2043 	_rtw_memcpy(ptr + pattrib->hdrlen, mgmt_DATA, data_len);
2044 	/* remove the iv and icv length */
2045 	pattrib->pkt_len = pattrib->pkt_len - pattrib->iv_len - pattrib->icv_len;
2046 	rtw_mfree(mgmt_DATA, data_len);
2047 
2048 #if DBG_VALIDATE_MGMT_DEC
2049 	/* print packet content after decryption */
2050 	{
2051 		int pp;
2052 
2053 		printk("after decryption pattrib->pktlen = %d @@=>", pattrib->pkt_len);
2054 		for (pp = 0; pp < pattrib->pkt_len; pp++)
2055 		printk(" %02x ", ptr[pp]);
2056 		printk("\n");
2057 	}
2058 #endif
2059 
2060 	if (!precv_frame) {
2061 		#if DBG_VALIDATE_MGMT_PROTECT
2062 		RTW_INFO(FUNC_ADPT_FMT" mgmt descrypt fail  !!!!!!!!!\n", FUNC_ADPT_ARG(adapter));
2063 		#endif
2064 		goto fail;
2065 	}
2066 
2067 exit:
2068 	return _SUCCESS;
2069 
2070 fail:
2071 	return _FAIL;
2072 
2073 }
2074 #endif /* defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH) */
2075 
2076 union recv_frame *recvframe_chk_defrag(PADAPTER padapter, union recv_frame *precv_frame);
2077 
validate_recv_mgnt_frame(PADAPTER padapter,union recv_frame * precv_frame)2078 sint validate_recv_mgnt_frame(PADAPTER padapter, union recv_frame *precv_frame)
2079 {
2080 	struct sta_info *psta = precv_frame->u.hdr.psta
2081 		= rtw_get_stainfo(&padapter->stapriv, get_addr2_ptr(precv_frame->u.hdr.rx_data));
2082 
2083 #if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH)
2084 	if (validate_mgmt_protect(padapter, precv_frame) == _FAIL) {
2085 		DBG_COUNTER(padapter->rx_logs.core_rx_pre_mgmt_err_80211w);
2086 		goto exit;
2087 	}
2088 #endif
2089 
2090 	precv_frame = recvframe_chk_defrag(padapter, precv_frame);
2091 	if (precv_frame == NULL)
2092 		return _SUCCESS;
2093 
2094 	/* for rx pkt statistics */
2095 	if (psta) {
2096 		psta->sta_stats.last_rx_time = rtw_get_current_time();
2097 		psta->sta_stats.rx_mgnt_pkts++;
2098 		if (get_frame_sub_type(precv_frame->u.hdr.rx_data) == WIFI_BEACON)
2099 			psta->sta_stats.rx_beacon_pkts++;
2100 		else if (get_frame_sub_type(precv_frame->u.hdr.rx_data) == WIFI_PROBEREQ)
2101 			psta->sta_stats.rx_probereq_pkts++;
2102 		else if (get_frame_sub_type(precv_frame->u.hdr.rx_data) == WIFI_PROBERSP) {
2103 			if (_rtw_memcmp(adapter_mac_addr(padapter), GetAddr1Ptr(precv_frame->u.hdr.rx_data), ETH_ALEN) == _TRUE)
2104 				psta->sta_stats.rx_probersp_pkts++;
2105 			else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data))
2106 				|| is_multicast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data)))
2107 				psta->sta_stats.rx_probersp_bm_pkts++;
2108 			else
2109 				psta->sta_stats.rx_probersp_uo_pkts++;
2110 		}
2111 	}
2112 
2113 	mgt_dispatcher(padapter, precv_frame);
2114 
2115 #if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH)
2116 exit:
2117 #endif
2118 	return _SUCCESS;
2119 
2120 }
2121 
validate_recv_data_frame(_adapter * adapter,union recv_frame * precv_frame)2122 sint validate_recv_data_frame(_adapter *adapter, union recv_frame *precv_frame)
2123 {
2124 	u8 bretry, a4_shift;
2125 	struct sta_info *psta = NULL;
2126 	u8 *ptr = precv_frame->u.hdr.rx_data;
2127 	struct rx_pkt_attrib	*pattrib = &precv_frame->u.hdr.attrib;
2128 	struct security_priv	*psecuritypriv = &adapter->securitypriv;
2129 	sint ret = _SUCCESS;
2130 
2131 	bretry = GetRetry(ptr);
2132 	a4_shift = (pattrib->to_fr_ds == 3) ? ETH_ALEN : 0;
2133 
2134 	/* some address fields are different when using AMSDU */
2135 	if (pattrib->qos)
2136 		pattrib->amsdu = GetAMsdu(ptr + WLAN_HDR_A3_LEN + a4_shift);
2137 	else
2138 		pattrib->amsdu = 0;
2139 
2140 #ifdef CONFIG_RTW_MESH
2141 	if (MLME_IS_MESH(adapter)) {
2142 		ret = rtw_mesh_rx_data_validate_hdr(adapter, precv_frame, &psta);
2143 		goto pre_validate_status_chk;
2144 	} else
2145 #endif
2146 #ifdef CONFIG_AP_MODE
2147 	if (MLME_IS_AP(adapter)) {
2148 		ret = rtw_ap_rx_data_validate_hdr(adapter, precv_frame, &psta);
2149 		goto pre_validate_status_chk;
2150 	} else
2151 #endif
2152 	if (MLME_IS_STA(adapter)) {
2153 		ret = rtw_sta_rx_data_validate_hdr(adapter, precv_frame, &psta);
2154 		goto pre_validate_status_chk;
2155 	}
2156 
2157 	switch (pattrib->to_fr_ds) {
2158 	case 0:
2159 		_rtw_memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
2160 		_rtw_memcpy(pattrib->ta, get_addr2_ptr(ptr), ETH_ALEN);
2161 		_rtw_memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
2162 		_rtw_memcpy(pattrib->src, get_addr2_ptr(ptr), ETH_ALEN);
2163 		_rtw_memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
2164 		ret = sta2sta_data_frame(adapter, precv_frame, &psta);
2165 		break;
2166 
2167 	case 1:
2168 		_rtw_memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
2169 		_rtw_memcpy(pattrib->ta, get_addr2_ptr(ptr), ETH_ALEN);
2170 		_rtw_memcpy(pattrib->dst, GetAddr3Ptr(ptr), ETH_ALEN);
2171 		_rtw_memcpy(pattrib->src, get_addr2_ptr(ptr), ETH_ALEN);
2172 		_rtw_memcpy(pattrib->bssid, GetAddr1Ptr(ptr), ETH_ALEN);
2173 		ret = sta2ap_data_frame(adapter, precv_frame, &psta);
2174 		break;
2175 
2176 	case 2:
2177 		_rtw_memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
2178 		_rtw_memcpy(pattrib->ta, get_addr2_ptr(ptr), ETH_ALEN);
2179 		_rtw_memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
2180 		_rtw_memcpy(pattrib->src, GetAddr3Ptr(ptr), ETH_ALEN);
2181 		_rtw_memcpy(pattrib->bssid, get_addr2_ptr(ptr), ETH_ALEN);
2182 		ret = ap2sta_data_frame(adapter, precv_frame, &psta);
2183 		break;
2184 
2185 	case 3:
2186 	default:
2187 		/* WDS is not supported */
2188 		ret = _FAIL;
2189 		break;
2190 	}
2191 
2192 pre_validate_status_chk:
2193 
2194 	if (ret == _FAIL) {
2195 		#ifdef DBG_RX_DROP_FRAME
2196 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" case:%d, res:%d, ra="MAC_FMT", ta="MAC_FMT"\n"
2197 			, FUNC_ADPT_ARG(adapter), pattrib->to_fr_ds, ret, MAC_ARG(GetAddr1Ptr(ptr)), MAC_ARG(get_addr2_ptr(ptr)));
2198 		#endif
2199 		goto exit;
2200 	} else if (ret == RTW_RX_HANDLED)
2201 		goto exit;
2202 
2203 
2204 	if (psta == NULL) {
2205 		#ifdef DBG_RX_DROP_FRAME
2206 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" psta == NULL, ra="MAC_FMT", ta="MAC_FMT"\n"
2207 			, FUNC_ADPT_ARG(adapter), MAC_ARG(GetAddr1Ptr(ptr)), MAC_ARG(get_addr2_ptr(ptr)));
2208 		#endif
2209 		ret = _FAIL;
2210 		goto exit;
2211 	}
2212 
2213 	if ((psta->flags & WLAN_STA_AMSDU_DISABLE) && pattrib->amsdu) {
2214 		#ifdef DBG_RX_DROP_FRAME
2215 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" amsdu not allowed"MAC_FMT"\n"
2216 			, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2217 		#endif
2218 		ret = _FAIL;
2219 		goto exit;
2220 
2221 	}
2222 
2223 	precv_frame->u.hdr.psta = psta;
2224 	precv_frame->u.hdr.preorder_ctrl = NULL;
2225 	pattrib->ack_policy = 0;
2226 
2227 	/* parsing QC field */
2228 	if (pattrib->qos == 1) {
2229 		pattrib->priority = GetPriority((ptr + WLAN_HDR_A3_LEN + a4_shift)); /* point to Qos field*/
2230 		pattrib->ack_policy = GetAckpolicy((ptr + WLAN_HDR_A3_LEN + a4_shift));
2231 		pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN + a4_shift;
2232 		if (pattrib->priority != 0 && pattrib->priority != 3)
2233 			adapter->recvpriv.is_any_non_be_pkts = _TRUE;
2234 		else
2235 			adapter->recvpriv.is_any_non_be_pkts = _FALSE;
2236 	} else {
2237 		pattrib->priority = 0;
2238 		pattrib->hdrlen = WLAN_HDR_A3_LEN + a4_shift;
2239 	}
2240 
2241 	if (pattrib->order) /* HT-CTRL 11n */
2242 		pattrib->hdrlen += 4;
2243 
2244 	/* decache, drop duplicate recv packets */
2245 	ret = recv_decache(precv_frame);
2246 	if (ret  == _FAIL)
2247 		goto exit;
2248 
2249 	if (!IS_MCAST(pattrib->ra)) {
2250 
2251 		if (pattrib->qos)
2252 			precv_frame->u.hdr.preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
2253 
2254 		if (recv_ucast_pn_decache(precv_frame) == _FAIL) {
2255 			#ifdef DBG_RX_DROP_FRAME
2256 			RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" recv_ucast_pn_decache return _FAIL for sta="MAC_FMT"\n"
2257 				, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2258 			#endif
2259 			ret = _FAIL;
2260 			goto exit;
2261 		}
2262 	} else {
2263 		if (recv_bcast_pn_decache(precv_frame) == _FAIL) {
2264 			#ifdef DBG_RX_DROP_FRAME
2265 			RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" recv_bcast_pn_decache return _FAIL for sta="MAC_FMT"\n"
2266 				, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2267 			#endif
2268 			ret = _FAIL;
2269 			goto exit;
2270 		}
2271 	}
2272 
2273 	if (pattrib->privacy) {
2274 #ifdef CONFIG_TDLS
2275 		if ((psta->tdls_sta_state & TDLS_LINKED_STATE) && (psta->dot118021XPrivacy == _AES_))
2276 			pattrib->encrypt = psta->dot118021XPrivacy;
2277 		else
2278 #endif /* CONFIG_TDLS */
2279 			GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
2280 
2281 
2282 		SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
2283 	} else {
2284 		pattrib->encrypt = 0;
2285 		pattrib->iv_len = pattrib->icv_len = 0;
2286 	}
2287 
2288 	/* drop unprotected frame in protected network. */
2289 	if (psecuritypriv->dot11PrivacyAlgrthm != _NO_PRIVACY_ ) {
2290 		if (IS_MCAST(pattrib->ra)) {
2291 			if (!pattrib->privacy) {
2292 				#ifdef DBG_RX_DROP_FRAME
2293 				RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT"recv plaintext bmc packet for sta="MAC_FMT"\n"
2294 					, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2295 				#endif
2296 				ret = _FAIL;
2297 				goto exit;
2298 			}
2299 		} else {
2300 			/* unicast */
2301 			u16 ether_type;
2302 			u8* ether_ptr = NULL;
2303 			u16  eapol_type = 0x888e;
2304 			ether_ptr = ptr + pattrib->hdrlen + pattrib->iv_len + RATTRIB_GET_MCTRL_LEN(pattrib) + LLC_HEADER_SIZE;
2305 			_rtw_memcpy(&ether_type, ether_ptr, 2);
2306 			ether_type = ntohs((unsigned short)ether_type);
2307 
2308 			if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
2309 				/* CVE-2020-26140, CVE-2020-26143, CVE-2020-26147,  let eapol packet go through*/
2310 				if (!pattrib->privacy && ether_type != eapol_type ) {
2311 					#ifdef DBG_RX_DROP_FRAME
2312 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT"recv plaintext unicast packet for sta="MAC_FMT"\n"
2313 					, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2314 					#endif
2315 					ret = _FAIL;
2316 					goto exit;
2317 				}
2318 				/* CVE-2020-26144, pevernt plaintext A-MSDU */
2319 				/* This can prevent plantext A-MSDU cloacked as an EAPOL frame */
2320 				if (!pattrib->privacy && pattrib->amsdu) {
2321 					#ifdef DBG_RX_DROP_FRAME
2322 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT"recv plaintext A-MSDU for sta="MAC_FMT"\n"
2323 					, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2324 					#endif
2325 					ret = _FAIL;
2326 					goto exit;
2327 				}
2328 				/* CVE-2020-26139,  Drop any forwarding eapol packet until 4-way has done.	*/
2329 				if ((ether_type == eapol_type)
2330 					&& (MLME_IS_AP(adapter) || MLME_IS_MESH(adapter))
2331 					&& (psta->dot118021XPrivacy == _NO_PRIVACY_)
2332 					&& (!_rtw_memcmp( adapter_mac_addr(adapter), pattrib->dst, ETH_ALEN))) {
2333 					#ifdef DBG_RX_DROP_FRAME
2334 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" recv eapol packet forwarding(dst:"MAC_FMT") before 4-way finish.\n"
2335 						, FUNC_ADPT_ARG(adapter), MAC_ARG(pattrib->dst));
2336 					#endif
2337 					ret = _FAIL;
2338 					goto exit;
2339 				}
2340 			} else {
2341 				/* CVE-2020-26140, CVE-2020-26143, CVE-2020-26147 */
2342 				if (!pattrib->privacy
2343 					#ifdef CONFIG_WAPI_SUPPORT
2344 					&& (0x88b4 != ether_type) /* Let WAI packet pass here */
2345 					#endif
2346 				) {
2347 					#ifdef DBG_RX_DROP_FRAME
2348 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT"recv plaintext packet for sta="MAC_FMT"\n"
2349 						, FUNC_ADPT_ARG(adapter), MAC_ARG(psta->cmn.mac_addr));
2350 					#endif
2351 					ret = _FAIL;
2352 					goto exit;
2353 				}
2354 			}
2355 		}
2356 	}
2357 
2358 #ifdef CONFIG_RTW_MESH
2359 	if (!pattrib->amsdu
2360 		&& pattrib->mesh_ctrl_present
2361 		&& (!pattrib->encrypt || pattrib->bdecrypted))
2362 		ret = rtw_mesh_rx_validate_mctrl_non_amsdu(adapter, precv_frame);
2363 #endif
2364 
2365 exit:
2366 	return ret;
2367 }
2368 
dump_rx_packet(u8 * ptr)2369 static inline void dump_rx_packet(u8 *ptr)
2370 {
2371 	int i;
2372 
2373 	RTW_INFO("#############################\n");
2374 	for (i = 0; i < 64; i = i + 8)
2375 		RTW_INFO("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr + i),
2376 			*(ptr + i + 1), *(ptr + i + 2) , *(ptr + i + 3) , *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
2377 	RTW_INFO("#############################\n");
2378 }
2379 
2380 sint validate_recv_frame(_adapter *adapter, union recv_frame *precv_frame);
validate_recv_frame(_adapter * adapter,union recv_frame * precv_frame)2381 sint validate_recv_frame(_adapter *adapter, union recv_frame *precv_frame)
2382 {
2383 	/* shall check frame subtype, to / from ds, da, bssid */
2384 
2385 	/* then call check if rx seq/frag. duplicated. */
2386 
2387 	u8 type;
2388 	u8 subtype;
2389 	sint retval = _SUCCESS;
2390 
2391 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
2392 	struct recv_priv  *precvpriv = &adapter->recvpriv;
2393 
2394 	u8 *ptr = precv_frame->u.hdr.rx_data;
2395 	u8  ver = (unsigned char)(*ptr) & 0x3 ;
2396 #ifdef CONFIG_FIND_BEST_CHANNEL
2397 	struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter);
2398 	struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
2399 #endif
2400 
2401 #ifdef CONFIG_TDLS
2402 	struct tdls_info *ptdlsinfo = &adapter->tdlsinfo;
2403 #endif /* CONFIG_TDLS */
2404 #ifdef CONFIG_WAPI_SUPPORT
2405 	PRT_WAPI_T	pWapiInfo = &adapter->wapiInfo;
2406 	struct recv_frame_hdr *phdr = &precv_frame->u.hdr;
2407 	u8 wai_pkt = 0;
2408 	u16 sc;
2409 	u8	external_len = 0;
2410 #endif
2411 
2412 
2413 #ifdef CONFIG_FIND_BEST_CHANNEL
2414 	if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
2415 		int ch_set_idx = rtw_chset_search_ch(rfctl->channel_set, rtw_get_oper_ch(adapter));
2416 		if (ch_set_idx >= 0)
2417 			rfctl->channel_set[ch_set_idx].rx_count++;
2418 	}
2419 #endif
2420 
2421 #ifdef CONFIG_TDLS
2422 	if (ptdlsinfo->ch_sensing == 1 && ptdlsinfo->cur_channel != 0)
2423 		ptdlsinfo->collect_pkt_num[ptdlsinfo->cur_channel - 1]++;
2424 #endif /* CONFIG_TDLS */
2425 
2426 #ifdef RTK_DMP_PLATFORM
2427 	if (0) {
2428 		RTW_INFO("++\n");
2429 		{
2430 			int i;
2431 			for (i = 0; i < 64; i = i + 8)
2432 				RTW_INFO("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:", *(ptr + i),
2433 					*(ptr + i + 1), *(ptr + i + 2) , *(ptr + i + 3) , *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
2434 
2435 		}
2436 		RTW_INFO("--\n");
2437 	}
2438 #endif /* RTK_DMP_PLATFORM */
2439 
2440 	/* add version chk */
2441 	if (ver != 0) {
2442 		retval = _FAIL;
2443 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_ver_err);
2444 		goto exit;
2445 	}
2446 
2447 	type =  GetFrameType(ptr);
2448 	subtype = get_frame_sub_type(ptr); /* bit(7)~bit(2) */
2449 
2450 	pattrib->to_fr_ds = get_tofr_ds(ptr);
2451 
2452 	pattrib->frag_num = GetFragNum(ptr);
2453 	pattrib->seq_num = GetSequence(ptr);
2454 
2455 	pattrib->pw_save = GetPwrMgt(ptr);
2456 	pattrib->mfrag = GetMFrag(ptr);
2457 	pattrib->mdata = GetMData(ptr);
2458 	pattrib->privacy = GetPrivacy(ptr);
2459 	pattrib->order = GetOrder(ptr);
2460 #ifdef CONFIG_WAPI_SUPPORT
2461 	sc = (pattrib->seq_num << 4) | pattrib->frag_num;
2462 #endif
2463 
2464 #if 1 /* Dump rx packets */
2465 	{
2466 		u8 bDumpRxPkt = 0;
2467 
2468 		rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
2469 		if (bDumpRxPkt == 1) /* dump all rx packets */
2470 			dump_rx_packet(ptr);
2471 		else if ((bDumpRxPkt == 2) && (type == WIFI_MGT_TYPE))
2472 			dump_rx_packet(ptr);
2473 		else if ((bDumpRxPkt == 3) && (type == WIFI_DATA_TYPE))
2474 			dump_rx_packet(ptr);
2475 	}
2476 #endif
2477 	switch (type) {
2478 	case WIFI_MGT_TYPE: /* mgnt */
2479 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt);
2480 		retval = validate_recv_mgnt_frame(adapter, precv_frame);
2481 		if (retval == _FAIL) {
2482 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt_err);
2483 		}
2484 		retval = _FAIL; /* only data frame return _SUCCESS */
2485 		break;
2486 	case WIFI_CTRL_TYPE: /* ctrl */
2487 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl);
2488 		retval = validate_recv_ctrl_frame(adapter, precv_frame);
2489 		if (retval == _FAIL) {
2490 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl_err);
2491 		}
2492 		retval = _FAIL; /* only data frame return _SUCCESS */
2493 		break;
2494 	case WIFI_DATA_TYPE: /* data */
2495 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_data);
2496 #ifdef CONFIG_WAPI_SUPPORT
2497 		if (pattrib->qos)
2498 			external_len = 2;
2499 		else
2500 			external_len = 0;
2501 
2502 		wai_pkt = rtw_wapi_is_wai_packet(adapter, ptr);
2503 
2504 		phdr->bIsWaiPacket = wai_pkt;
2505 
2506 		if (wai_pkt != 0) {
2507 			if (sc != adapter->wapiInfo.wapiSeqnumAndFragNum)
2508 				adapter->wapiInfo.wapiSeqnumAndFragNum = sc;
2509 			else {
2510 				retval = _FAIL;
2511 				DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_wapi_seq_err);
2512 				break;
2513 			}
2514 		} else {
2515 
2516 			if (rtw_wapi_drop_for_key_absent(adapter, get_addr2_ptr(ptr))) {
2517 				retval = _FAIL;
2518 				WAPI_TRACE(WAPI_RX, "drop for key absent for rx\n");
2519 				DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_wapi_key_err);
2520 				break;
2521 			}
2522 		}
2523 
2524 #endif
2525 
2526 		pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
2527 		retval = validate_recv_data_frame(adapter, precv_frame);
2528 		if (retval == _FAIL) {
2529 			precvpriv->dbg_rx_drop_count++;
2530 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_err);
2531 		} else if (retval == _SUCCESS) {
2532 			#ifdef DBG_RX_DUMP_EAP
2533 			if (!pattrib->encrypt || pattrib->bdecrypted) {
2534 				u8 bDumpRxPkt;
2535 				u16 eth_type;
2536 
2537 				/* dump eapol */
2538 				rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
2539 				/* get ether_type */
2540 				_rtw_memcpy(&eth_type, ptr + pattrib->hdrlen + pattrib->iv_len + RATTRIB_GET_MCTRL_LEN(pattrib) + LLC_HEADER_SIZE, 2);
2541 				eth_type = ntohs((unsigned short) eth_type);
2542 				if ((bDumpRxPkt == 4) && (eth_type == 0x888e))
2543 					dump_rx_packet(ptr);
2544 			}
2545 			#endif
2546 		} else
2547 			DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_handled);
2548 		break;
2549 	default:
2550 		DBG_COUNTER(adapter->rx_logs.core_rx_pre_unknown);
2551 		#ifdef DBG_RX_DROP_FRAME
2552 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" fail! type=0x%x\n"
2553 			, FUNC_ADPT_ARG(adapter), type);
2554 		#endif
2555 		retval = _FAIL;
2556 		break;
2557 	}
2558 
2559 exit:
2560 
2561 
2562 	return retval;
2563 }
2564 
2565 /* Reture expected handling for LLC */
rtw_recv_llc_parse(u8 * msdu,u16 msdu_len)2566 enum rtw_rx_llc_hdl rtw_recv_llc_parse(u8 *msdu, u16 msdu_len)
2567 {
2568 	u16	eth_type;
2569 
2570 	if (msdu_len < 8)
2571 		return RTW_RX_LLC_KEEP;
2572 
2573 	eth_type = RTW_GET_BE16(msdu + SNAP_SIZE);
2574 
2575 	if ((_rtw_memcmp(msdu, rtw_rfc1042_header, SNAP_SIZE)
2576 			&& eth_type != ETH_P_AARP && eth_type != ETH_P_IPX)
2577 		|| _rtw_memcmp(msdu, rtw_bridge_tunnel_header, SNAP_SIZE)) {
2578 		/* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
2579 		return RTW_RX_LLC_REMOVE;
2580 	} else {
2581 		/* Leave Ethernet header part of hdr and full payload */
2582 		return RTW_RX_LLC_KEEP;
2583 	}
2584 
2585 	/* TODO: VLAN tagged */
2586 }
2587 
2588 /* remove the wlanhdr and add the eth_hdr */
wlanhdr_to_ethhdr(union recv_frame * precvframe,enum rtw_rx_llc_hdl llc_hdl)2589 sint wlanhdr_to_ethhdr(union recv_frame *precvframe, enum rtw_rx_llc_hdl llc_hdl)
2590 {
2591 	u8	*ptr = get_recvframe_data(precvframe) ; /* point to frame_ctrl field */
2592 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
2593 	sint rmv_len;
2594 	u16	eth_type, len;
2595 	sint ret = _SUCCESS;
2596 
2597 	if (pattrib->encrypt)
2598 		recvframe_pull_tail(precvframe, pattrib->icv_len);
2599 
2600 	rmv_len = pattrib->hdrlen + pattrib->iv_len + RATTRIB_GET_MCTRL_LEN(pattrib) + (llc_hdl ? SNAP_SIZE : 0);
2601 	len = precvframe->u.hdr.len - rmv_len;
2602 
2603 #ifdef CONFIG_RTL8822CS_WIFI_HDF
2604 	_rtw_memcpy(&eth_type, ptr + rmv_len, 2);
2605 	eth_type = ntohs((unsigned short)eth_type); /* pattrib->ether_type */
2606 	pattrib->eth_type = eth_type;
2607 #endif
2608 
2609 	ptr = recvframe_pull(precvframe, (rmv_len - sizeof(struct ethhdr) + (llc_hdl ? 2 : 0)));
2610 	if (!ptr) {
2611 		ret = _FAIL;
2612 		goto exiting;
2613 	}
2614 
2615 	_rtw_memcpy(ptr, pattrib->dst, ETH_ALEN);
2616 	_rtw_memcpy(ptr + ETH_ALEN, pattrib->src, ETH_ALEN);
2617 
2618 	if (!llc_hdl) {
2619 		len = htons(len);
2620 		_rtw_memcpy(ptr + 12, &len, 2);
2621 	}
2622 
2623 	rtw_rframe_set_os_pkt(precvframe);
2624 
2625 exiting:
2626 	return ret;
2627 }
2628 
2629 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2630 #ifndef CONFIG_SDIO_RX_COPY
2631 #ifdef PLATFORM_LINUX
recvframe_expand_pkt(PADAPTER padapter,union recv_frame * prframe)2632 static void recvframe_expand_pkt(
2633 	PADAPTER padapter,
2634 	union recv_frame *prframe)
2635 {
2636 	struct recv_frame_hdr *pfhdr;
2637 	_pkt *ppkt;
2638 	u8 shift_sz;
2639 	u32 alloc_sz;
2640 	u8 *ptr;
2641 
2642 
2643 	pfhdr = &prframe->u.hdr;
2644 
2645 	/*	6 is for IP header 8 bytes alignment in QoS packet case. */
2646 	if (pfhdr->attrib.qos)
2647 		shift_sz = 6;
2648 	else
2649 		shift_sz = 0;
2650 
2651 	/* for first fragment packet, need to allocate */
2652 	/* (1536 + RXDESC_SIZE + drvinfo_sz) to reassemble packet */
2653 	/*	8 is for skb->data 8 bytes alignment.
2654 	*	alloc_sz = _RND(1536 + RXDESC_SIZE + pfhdr->attrib.drvinfosize + shift_sz + 8, 128); */
2655 	alloc_sz = 1664; /* round (1536 + 24 + 32 + shift_sz + 8) to 128 bytes alignment */
2656 
2657 	/* 3 1. alloc new skb */
2658 	/* prepare extra space for 4 bytes alignment */
2659 	ppkt = rtw_skb_alloc(alloc_sz);
2660 
2661 	if (!ppkt)
2662 		return; /* no way to expand */
2663 
2664 	/* 3 2. Prepare new skb to replace & release old skb */
2665 	/* force ppkt->data at 8-byte alignment address */
2666 	skb_reserve(ppkt, 8 - ((SIZE_PTR)ppkt->data & 7));
2667 	/* force ip_hdr at 8-byte alignment address according to shift_sz */
2668 	skb_reserve(ppkt, shift_sz);
2669 
2670 	/* copy data to new pkt */
2671 	ptr = skb_put(ppkt, pfhdr->len);
2672 	if (ptr)
2673 		_rtw_memcpy(ptr, pfhdr->rx_data, pfhdr->len);
2674 
2675 	rtw_skb_free(pfhdr->pkt);
2676 
2677 	/* attach new pkt to recvframe */
2678 	pfhdr->pkt = ppkt;
2679 	pfhdr->rx_head = ppkt->head;
2680 	pfhdr->rx_data = ppkt->data;
2681 	pfhdr->rx_tail = skb_tail_pointer(ppkt);
2682 	pfhdr->rx_end = skb_end_pointer(ppkt);
2683 }
2684 #else /*!= PLATFORM_LINUX*/
2685 #warning "recvframe_expand_pkt not implement, defrag may crash system"
2686 #endif
2687 #endif /*#ifndef CONFIG_SDIO_RX_COPY*/
2688 #endif
2689 
2690 /* perform defrag */
2691 union recv_frame *recvframe_defrag(_adapter *adapter, _queue *defrag_q);
recvframe_defrag(_adapter * adapter,_queue * defrag_q)2692 union recv_frame *recvframe_defrag(_adapter *adapter, _queue *defrag_q)
2693 {
2694 	_list	*plist, *phead;
2695 	u8	*data, wlanhdr_offset;
2696 	u8	curfragnum;
2697 	struct recv_frame_hdr *pfhdr, *pnfhdr;
2698 	union recv_frame *prframe, *pnextrframe;
2699 	_queue	*pfree_recv_queue;
2700 	u8 *pdata = NULL;
2701 	u64 tmp_iv_hdr = 0;
2702 	u64 pkt_pn = 0, cur_pn = 0;
2703 	struct rx_pkt_attrib *pattrib = NULL;
2704 
2705 	curfragnum = 0;
2706 	pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
2707 
2708 	phead = get_list_head(defrag_q);
2709 	plist = get_next(phead);
2710 	prframe = LIST_CONTAINOR(plist, union recv_frame, u);
2711 	/* CVE-2020-26146 */
2712 	pattrib = &prframe->u.hdr.attrib;
2713 	if (pattrib->encrypt == _AES_  || pattrib->encrypt == _CCMP_256_
2714 		|| pattrib->encrypt == _GCMP_ || pattrib->encrypt == _GCMP_256_ ) {
2715 		pdata = prframe->u.hdr.rx_data;
2716 		tmp_iv_hdr = le64_to_cpu(*(u64*)(pdata + pattrib->hdrlen));
2717 		/* get the first frame's PN. */
2718 		cur_pn = CCMPH_2_PN(tmp_iv_hdr);
2719 	}
2720 	pfhdr = &prframe->u.hdr;
2721 	rtw_list_delete(&(prframe->u.list));
2722 
2723 	if (curfragnum != pfhdr->attrib.frag_num) {
2724 		/* the first fragment number must be 0 */
2725 		/* free the whole queue */
2726 		rtw_free_recvframe(prframe, pfree_recv_queue);
2727 		rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2728 
2729 		return NULL;
2730 	}
2731 
2732 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2733 #ifndef CONFIG_SDIO_RX_COPY
2734 	recvframe_expand_pkt(adapter, prframe);
2735 #endif
2736 #endif
2737 
2738 	curfragnum++;
2739 
2740 	plist = get_list_head(defrag_q);
2741 
2742 	plist = get_next(plist);
2743 
2744 	data = get_recvframe_data(prframe);
2745 
2746 	while (rtw_end_of_queue_search(phead, plist) == _FALSE) {
2747 		pnextrframe = LIST_CONTAINOR(plist, union recv_frame , u);
2748 		pnfhdr = &pnextrframe->u.hdr;
2749 		/* CVE-2020-26146, check whether the PN is consecutive. */
2750 		pattrib = &pnextrframe->u.hdr.attrib;
2751 		if (pattrib->encrypt == _AES_  || pattrib->encrypt == _CCMP_256_
2752 			|| pattrib->encrypt == _GCMP_ || pattrib->encrypt == _GCMP_256_ ) {
2753 			pdata = pnextrframe->u.hdr.rx_data;
2754 			tmp_iv_hdr = le64_to_cpu(*(u64*)(pdata + pattrib->hdrlen));
2755 			pkt_pn = CCMPH_2_PN(tmp_iv_hdr);
2756 			if (pkt_pn != cur_pn + 1) {
2757 				RTW_INFO("%s non-consective PN! old:%llu, new:%llu\n",
2758 					__func__, cur_pn, pkt_pn);
2759 				/* PN must be consecutive */
2760 				/* release the defrag_q & prframe */
2761 				rtw_free_recvframe(prframe, pfree_recv_queue);
2762 				rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2763 				return NULL;
2764 			} else {
2765 				cur_pn = pkt_pn;
2766 			}
2767 		}
2768 
2769 		/* CVE-2020-24587, The keytrack of the fragment is supposed to be the same with other's	*/
2770 		if (pfhdr->keytrack != pnfhdr->keytrack) {
2771 			RTW_INFO("Inconsistent key track, drop fragmented frame!\n");
2772 			rtw_free_recvframe(prframe, pfree_recv_queue);
2773 			rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2774 			return NULL;
2775 		}
2776 
2777 		/* check the fragment sequence  (2nd ~n fragment frame) */
2778 
2779 		if (curfragnum != pnfhdr->attrib.frag_num) {
2780 			/* the fragment number must be increasing  (after decache) */
2781 			/* release the defrag_q & prframe */
2782 			rtw_free_recvframe(prframe, pfree_recv_queue);
2783 			rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2784 			return NULL;
2785 		}
2786 
2787 		/* copy the 2nd~n fragment frame's payload to the first fragment */
2788 		/* get the 2nd~last fragment frame's payload */
2789 
2790 		wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
2791 
2792 		recvframe_pull(pnextrframe, wlanhdr_offset);
2793 
2794 		if ((pfhdr->rx_end - pfhdr->rx_tail) < pnfhdr->len) {
2795 			RTW_INFO("Not enough buffer space, drop fragmented frame!\n");
2796 			rtw_free_recvframe(prframe, pfree_recv_queue);
2797 			rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2798 			return NULL;
2799 		}
2800 
2801 		curfragnum++;
2802 
2803 		/* append  to first fragment frame's tail (if privacy frame, pull the ICV) */
2804 		recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
2805 
2806 		/* memcpy */
2807 		_rtw_memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
2808 
2809 		recvframe_put(prframe, pnfhdr->len);
2810 
2811 		pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
2812 		plist = get_next(plist);
2813 
2814 	};
2815 
2816 	/* free the defrag_q queue and return the prframe */
2817 	rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2818 
2819 
2820 
2821 	return prframe;
2822 }
2823 
2824 /* check if need to defrag, if needed queue the frame to defrag_q */
recvframe_chk_defrag(PADAPTER padapter,union recv_frame * precv_frame)2825 union recv_frame *recvframe_chk_defrag(PADAPTER padapter, union recv_frame *precv_frame)
2826 {
2827 	u8	ismfrag;
2828 	u8	fragnum;
2829 	u8	*psta_addr;
2830 	struct recv_frame_hdr *pfhdr;
2831 	struct sta_info *psta;
2832 	struct sta_priv *pstapriv;
2833 	_list *phead;
2834 	union recv_frame *prtnframe = NULL;
2835 	_queue *pfree_recv_queue, *pdefrag_q = NULL;
2836 
2837 
2838 	pstapriv = &padapter->stapriv;
2839 
2840 	pfhdr = &precv_frame->u.hdr;
2841 
2842 	pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2843 
2844 	/* need to define struct of wlan header frame ctrl */
2845 	ismfrag = pfhdr->attrib.mfrag;
2846 	fragnum = pfhdr->attrib.frag_num;
2847 
2848 	psta_addr = pfhdr->attrib.ta;
2849 	psta = rtw_get_stainfo(pstapriv, psta_addr);
2850 	if (psta == NULL) {
2851 		u8 type = GetFrameType(pfhdr->rx_data);
2852 		if (type != WIFI_DATA_TYPE) {
2853 			psta = rtw_get_bcmc_stainfo(padapter);
2854 			if (psta)
2855 				pdefrag_q = &psta->sta_recvpriv.defrag_q;
2856 		} else
2857 			pdefrag_q = NULL;
2858 	} else
2859 		pdefrag_q = &psta->sta_recvpriv.defrag_q;
2860 
2861 	if ((ismfrag == 0) && (fragnum == 0)) {
2862 		prtnframe = precv_frame;/* isn't a fragment frame */
2863 	} else {
2864 		/* CVE-2020-26145, group addressed frame cannot use fragmentation!! */
2865 		if (IS_MCAST(pfhdr->attrib.ra)) {
2866 			RTW_INFO("DROP group addressed fragment!\n");
2867 			rtw_free_recvframe(precv_frame, pfree_recv_queue);
2868 			return NULL;
2869 		}
2870 		/* CVE-2020-24587 */
2871 		if ((psta) && (pdefrag_q))
2872 			precv_frame->u.hdr.keytrack = ATOMIC_READ(&psta->keytrack);
2873 	}
2874 
2875 	if (ismfrag == 1) {
2876 		/* 0~(n-1) fragment frame */
2877 		/* enqueue to defraf_g */
2878 		if (pdefrag_q != NULL) {
2879 			if (fragnum == 0) {
2880 				/* the first fragment */
2881 				if (_rtw_queue_empty(pdefrag_q) == _FALSE) {
2882 					/* free current defrag_q */
2883 					rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
2884 				}
2885 			}
2886 
2887 
2888 			/* Then enqueue the 0~(n-1) fragment into the defrag_q */
2889 
2890 			/* _rtw_spinlock(&pdefrag_q->lock); */
2891 			phead = get_list_head(pdefrag_q);
2892 			rtw_list_insert_tail(&pfhdr->list, phead);
2893 			/* _rtw_spinunlock(&pdefrag_q->lock); */
2894 
2895 
2896 			prtnframe = NULL;
2897 
2898 		} else {
2899 			/* can't find this ta's defrag_queue, so free this recv_frame */
2900 			rtw_free_recvframe(precv_frame, pfree_recv_queue);
2901 			prtnframe = NULL;
2902 		}
2903 
2904 	}
2905 
2906 	if ((ismfrag == 0) && (fragnum != 0)) {
2907 		/* the last fragment frame */
2908 		/* enqueue the last fragment */
2909 		if (pdefrag_q != NULL) {
2910 			/* _rtw_spinlock(&pdefrag_q->lock); */
2911 			phead = get_list_head(pdefrag_q);
2912 			rtw_list_insert_tail(&pfhdr->list, phead);
2913 			/* _rtw_spinunlock(&pdefrag_q->lock); */
2914 
2915 			/* call recvframe_defrag to defrag */
2916 			precv_frame = recvframe_defrag(padapter, pdefrag_q);
2917 			prtnframe = precv_frame;
2918 
2919 		} else {
2920 			/* can't find this ta's defrag_queue, so free this recv_frame */
2921 			rtw_free_recvframe(precv_frame, pfree_recv_queue);
2922 			prtnframe = NULL;
2923 		}
2924 
2925 	}
2926 
2927 
2928 	if ((prtnframe != NULL) && (prtnframe->u.hdr.attrib.privacy)) {
2929 		/* after defrag we must check tkip mic code */
2930 		if (recvframe_chkmic(padapter,  prtnframe) == _FAIL) {
2931 			rtw_free_recvframe(prtnframe, pfree_recv_queue);
2932 			prtnframe = NULL;
2933 		}
2934 	}
2935 
2936 
2937 	return prtnframe;
2938 
2939 }
2940 
rtw_recv_indicatepkt_check(union recv_frame * rframe,u8 * ehdr_pos,u32 pkt_len)2941 static int rtw_recv_indicatepkt_check(union recv_frame *rframe, u8 *ehdr_pos, u32 pkt_len)
2942 {
2943 	_adapter *adapter = rframe->u.hdr.adapter;
2944 	struct recv_priv *recvpriv = &adapter->recvpriv;
2945 	struct ethhdr *ehdr = (struct ethhdr *)ehdr_pos;
2946 	struct rx_pkt_attrib *pattrib = &rframe->u.hdr.attrib;
2947 #ifdef DBG_IP_R_MONITOR
2948 	int i;
2949 	struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
2950 	struct mlme_priv	*pmlmepriv = &adapter->mlmepriv;
2951 	struct wlan_network *cur_network = &(pmlmepriv->cur_network);
2952 #endif/*DBG_IP_R_MONITOR*/
2953 	enum eap_type eapol_type;
2954 	int ret = _FAIL;
2955 
2956 #ifdef CONFIG_WAPI_SUPPORT
2957 	if (rtw_wapi_check_for_drop(adapter, rframe, ehdr_pos)) {
2958 		#ifdef DBG_RX_DROP_FRAME
2959 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" rtw_wapi_check_for_drop\n"
2960 			, FUNC_ADPT_ARG(adapter));
2961 		#endif
2962 		goto exit;
2963 	}
2964 #endif
2965 
2966 	if (rframe->u.hdr.psta)
2967 		rtw_st_ctl_rx(rframe->u.hdr.psta, ehdr_pos);
2968 
2969 	if (ntohs(ehdr->h_proto) == 0x888e) {
2970 		eapol_type = parsing_eapol_packet(adapter, ehdr_pos + ETH_HLEN, rframe->u.hdr.psta, 0);
2971 		if ((eapol_type == EAPOL_1_4 || eapol_type == EAPOL_3_4) && pattrib->encrypt == 0) {
2972 			rframe->u.hdr.psta->resp_nonenc_eapol_key_starttime = rtw_get_current_time();
2973 			RTW_INFO("Receive unencrypted eapol key\n");
2974 		}
2975 	}
2976 #ifdef DBG_ARP_DUMP
2977 	else if (ntohs(ehdr->h_proto) == ETH_P_ARP)
2978 		dump_arp_pkt(RTW_DBGDUMP, ehdr->h_dest, ehdr->h_source, ehdr_pos + ETH_HLEN, 0);
2979 #endif
2980 
2981 	if (recvpriv->sink_udpport > 0)
2982 		rtw_sink_rtp_seq_dbg(adapter, ehdr_pos);
2983 
2984 #ifdef DBG_UDP_PKT_LOSE_11AC
2985 	#define PAYLOAD_LEN_LOC_OF_IP_HDR 0x10 /*ethernet payload length location of ip header (DA + SA+eth_type+(version&hdr_len)) */
2986 
2987 	if (ntohs(ehdr->h_proto) == ETH_P_ARP) {
2988 		/* ARP Payload length will be 42bytes or 42+18(tailer)=60bytes*/
2989 		if (pkt_len != 42 && pkt_len != 60)
2990 			RTW_INFO("Error !!%s,ARP Payload length %u not correct\n" , __func__ , pkt_len);
2991 	} else if (ntohs(ehdr->h_proto) == ETH_P_IP) {
2992 		if (be16_to_cpu(*((u16 *)(ehdr_pos + PAYLOAD_LEN_LOC_OF_IP_HDR))) != (pkt_len) - ETH_HLEN) {
2993 			RTW_INFO("Error !!%s,Payload length not correct\n" , __func__);
2994 			RTW_INFO("%s, IP header describe Total length=%u\n" , __func__ , be16_to_cpu(*((u16 *)(ehdr_pos + PAYLOAD_LEN_LOC_OF_IP_HDR))));
2995 			RTW_INFO("%s, Pkt real length=%u\n" , __func__ , (pkt_len) - ETH_HLEN);
2996 		}
2997 	}
2998 #endif
2999 
3000 #ifdef DBG_IP_R_MONITOR
3001 	#define LEN_ARP_OP_HDR 7 /*ARP OERATION */
3002 	if (ntohs(ehdr->h_proto) == ETH_P_ARP) {
3003 
3004 		if(check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE){
3005 			if(ehdr_pos[ETHERNET_HEADER_SIZE+LEN_ARP_OP_HDR] == 2) {
3006 
3007 				RTW_INFO("%s,[DBG_ARP] Rx ARP RSP Packet;SeqNum = %d !\n",
3008 					__FUNCTION__, pattrib->seq_num);
3009 
3010 				dump_arp_pkt(RTW_DBGDUMP, ehdr->h_dest, ehdr->h_source, ehdr_pos + ETH_HLEN, 0);
3011 
3012 			}
3013 		}
3014 	}
3015 #endif/*DBG_IP_R_MONITOR*/
3016 
3017 #ifdef CONFIG_AUTO_AP_MODE
3018 	if (ntohs(ehdr->h_proto) == 0x8899)
3019 		rtw_auto_ap_rx_msg_dump(adapter, rframe, ehdr_pos);
3020 #endif
3021 
3022 	ret = _SUCCESS;
3023 
3024 #ifdef CONFIG_WAPI_SUPPORT
3025 exit:
3026 #endif
3027 	return ret;
3028 }
3029 
3030 #if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
recv_free_fwd_resource(_adapter * adapter,struct xmit_frame * fwd_frame,_list * f_list)3031 static void recv_free_fwd_resource(_adapter *adapter, struct xmit_frame *fwd_frame, _list *f_list)
3032 {
3033 	struct xmit_priv *xmitpriv = &adapter->xmitpriv;
3034 
3035 	if (fwd_frame)
3036 		rtw_free_xmitframe(xmitpriv, fwd_frame);
3037 
3038 #if defined(CONFIG_RTW_WDS) || CONFIG_RTW_DATA_BMC_TO_UC
3039 	if (!rtw_is_list_empty(f_list)) {
3040 		struct xmit_frame *fframe;
3041 		_list *list;
3042 
3043 		list = get_next(f_list);
3044 		while (rtw_end_of_queue_search(f_list, list) == _FALSE) {
3045 			fframe = LIST_CONTAINOR(list, struct xmit_frame, list);
3046 			list = get_next(list);
3047 			rtw_list_delete(&fframe->list);
3048 			rtw_free_xmitframe(xmitpriv, fframe);
3049 		}
3050 	}
3051 #endif
3052 }
3053 
recv_fwd_pkt_hdl(_adapter * adapter,_pkt * pkt,u8 act,struct xmit_frame * fwd_frame,_list * f_list)3054 static void recv_fwd_pkt_hdl(_adapter *adapter, _pkt *pkt
3055 	, u8 act, struct xmit_frame *fwd_frame, _list *f_list)
3056 {
3057 	struct xmit_priv *xmitpriv = &adapter->xmitpriv;
3058 	_pkt *fwd_pkt = pkt;
3059 
3060 	if (act & RTW_RX_MSDU_ACT_INDICATE) {
3061 		fwd_pkt = rtw_os_pkt_copy(pkt);
3062 		if (!fwd_pkt) {
3063 			#ifdef DBG_TX_DROP_FRAME
3064 			RTW_INFO("DBG_TX_DROP_FRAME %s rtw_os_pkt_copy fail\n", __func__);
3065 			#endif
3066 			recv_free_fwd_resource(adapter, fwd_frame, f_list);
3067 			goto exit;
3068 		}
3069 	}
3070 
3071 #if defined(CONFIG_RTW_WDS) || CONFIG_RTW_DATA_BMC_TO_UC
3072 	if (!rtw_is_list_empty(f_list)) {
3073 		_list *list = get_next(f_list);
3074 		struct xmit_frame *fframe;
3075 
3076 		while (rtw_end_of_queue_search(f_list, list) == _FALSE) {
3077 			fframe = LIST_CONTAINOR(list, struct xmit_frame, list);
3078 			list = get_next(list);
3079 			rtw_list_delete(&fframe->list);
3080 
3081 			if (!fwd_frame && rtw_is_list_empty(f_list)) /* the last fwd_pkt */
3082 				fframe->pkt = fwd_pkt;
3083 			else
3084 				fframe->pkt = rtw_os_pkt_copy(fwd_pkt);
3085 			if (!fframe->pkt) {
3086 				rtw_free_xmitframe(xmitpriv, fframe);
3087 				continue;
3088 			}
3089 
3090 			rtw_xmit_posthandle(adapter, fframe, fframe->pkt);
3091 		}
3092 	}
3093 #endif
3094 
3095 	if (fwd_frame) {
3096 		fwd_frame->pkt = fwd_pkt;
3097 		if (rtw_xmit_posthandle(adapter, fwd_frame, fwd_pkt) < 0) {
3098 			#ifdef DBG_TX_DROP_FRAME
3099 			RTW_INFO("DBG_TX_DROP_FRAME %s rtw_xmit_posthandle fail\n", __func__);
3100 			#endif
3101 			xmitpriv->tx_drop++;
3102 		}
3103 	}
3104 
3105 exit:
3106 	return;
3107 }
3108 #endif /* defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH) */
3109 
3110 /*
3111  * From WFA suggestion:						*
3112  * If first subframe meets one of the following condition,	*
3113  * the whole received AMSDU should drop.			*
3114  * 1. subframe's DA is not the same as RA in From DS case.	*
3115  * 2. subframe's SA is not the same as TA in To DS case.	*
3116  * 3. subframe's DA is AA:AA:03:00:00:00			*
3117 								*/
validate_amsdu_content(_adapter * padapter,union recv_frame * prframe,const u8 * da,const u8 * sa)3118 static u8 validate_amsdu_content(_adapter *padapter, union recv_frame *prframe,
3119 	const u8 *da, const u8 *sa)
3120 {
3121 	struct rx_pkt_attrib	*pattrib = &prframe->u.hdr.attrib;
3122 	u8 ret = _SUCCESS;
3123 
3124 	/* Use the recommendation method form Wi-Fi alliance to check subframe */
3125 	/* in protected network */
3126 	if (padapter->registrypriv.amsdu_mode == RTW_AMSDU_MODE_NON_SPP &&
3127 		padapter->securitypriv.dot11PrivacyAlgrthm != _NO_PRIVACY_) {
3128 
3129 		/* 1.check From DS */
3130 		if (pattrib->to_fr_ds == 2) {
3131 			if (_rtw_memcmp(da, pattrib->ra, ETH_ALEN) == _FALSE)
3132 			ret = _FAIL;
3133 		}
3134 
3135 		/* 2.check To DS */
3136 		if (pattrib->to_fr_ds == 1) {
3137 			if (_rtw_memcmp(sa, pattrib->ta, ETH_ALEN) == _FALSE)
3138 			ret = _FAIL;
3139 		}
3140 
3141 		/* 3.Check whether DA is AA:AA:03:00:00:00 */
3142 		if (_rtw_memcmp(da, rtw_rfc1042_header, ETH_ALEN) == _TRUE)
3143 			ret = _FAIL;
3144 
3145 	}
3146 
3147 	return ret;
3148 
3149 }
3150 
amsdu_to_msdu(_adapter * padapter,union recv_frame * prframe)3151 int amsdu_to_msdu(_adapter *padapter, union recv_frame *prframe)
3152 {
3153 	struct rx_pkt_attrib *rattrib = &prframe->u.hdr.attrib;
3154 	int	a_len, padding_len;
3155 	u16	nSubframe_Length;
3156 	u8	nr_subframes, i;
3157 	u8	*pdata;
3158 	_pkt *sub_pkt, *subframes[MAX_SUBFRAME_COUNT];
3159 	struct recv_priv *precvpriv = &padapter->recvpriv;
3160 	_queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
3161 	const u8 *da, *sa;
3162 	int act;
3163 #if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3164 	struct xmit_frame *fwd_frame;
3165 	_list f_list;
3166 #endif
3167 	enum rtw_rx_llc_hdl llc_hdl;
3168 	u8 mctrl_len = 0;
3169 	int	ret = _SUCCESS;
3170 
3171 	nr_subframes = 0;
3172 
3173 	recvframe_pull(prframe, rattrib->hdrlen);
3174 
3175 	if (rattrib->iv_len > 0)
3176 		recvframe_pull(prframe, rattrib->iv_len);
3177 	if (rattrib->encrypt)
3178 		recvframe_pull_tail(prframe, rattrib->icv_len);
3179 
3180 	a_len = prframe->u.hdr.len;
3181 	pdata = prframe->u.hdr.rx_data;
3182 
3183 	while (a_len > ETH_HLEN) {
3184 		/* Offset 12 denote 2 mac address */
3185 		nSubframe_Length = RTW_GET_BE16(pdata + 12);
3186 		if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
3187 			RTW_INFO("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
3188 			break;
3189 		}
3190 
3191 		act = RTW_RX_MSDU_ACT_INDICATE;
3192 		#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3193 		fwd_frame = NULL;
3194 		#endif
3195 
3196 		#ifdef CONFIG_RTW_MESH
3197 		if (MLME_IS_MESH(padapter)) {
3198 			u8 *mda = pdata, *msa = pdata + ETH_ALEN;
3199 			struct rtw_ieee80211s_hdr *mctrl = (struct rtw_ieee80211s_hdr *)(pdata + ETH_HLEN);
3200 			int v_ret;
3201 
3202 			v_ret = rtw_mesh_rx_data_validate_mctrl(padapter, prframe
3203 				, mctrl, mda, msa, &mctrl_len, &da, &sa);
3204 
3205 			if (validate_amsdu_content(padapter, prframe, da, sa) == _FAIL) {
3206 				RTW_INFO("%s check subframe content fail!\n", __func__);
3207 				break;
3208 			}
3209 
3210 			if (v_ret != _SUCCESS)
3211 				goto move_to_next;
3212 
3213 			llc_hdl = rtw_recv_llc_parse(pdata + ETH_HLEN + mctrl_len, nSubframe_Length - mctrl_len);
3214 			act = rtw_mesh_rx_msdu_act_check(prframe
3215 				, mda, msa, da, sa, mctrl
3216 				, pdata + ETH_HLEN + mctrl_len, llc_hdl
3217 				, &fwd_frame, &f_list);
3218 		} else
3219 		#endif
3220 		{
3221 			da = pdata;
3222 			sa = pdata + ETH_ALEN;
3223 
3224 			if (validate_amsdu_content(padapter, prframe, da, sa) == _FAIL) {
3225 				RTW_INFO("%s check subframe content fail!\n", __func__);
3226 				break;
3227 			}
3228 
3229 			llc_hdl = rtw_recv_llc_parse(pdata + ETH_HLEN, nSubframe_Length);
3230 			#ifdef CONFIG_AP_MODE
3231 			if (MLME_IS_AP(padapter)) {
3232 				act = rtw_ap_rx_msdu_act_check(prframe, da, sa
3233 					, pdata + ETH_HLEN, llc_hdl, &fwd_frame, &f_list);
3234 			} else
3235 			#endif
3236 			if (MLME_IS_STA(padapter))
3237 				act = rtw_sta_rx_amsdu_act_check(prframe, da, sa);
3238 		}
3239 
3240 		if (!act)
3241 			goto move_to_next;
3242 
3243 		rtw_led_rx_control(padapter, da);
3244 
3245 		sub_pkt = rtw_os_alloc_msdu_pkt(prframe, da, sa
3246 			, pdata + ETH_HLEN + mctrl_len, nSubframe_Length - mctrl_len, llc_hdl);
3247 		if (sub_pkt == NULL) {
3248 			if (act & RTW_RX_MSDU_ACT_INDICATE) {
3249 				#ifdef DBG_RX_DROP_FRAME
3250 				RTW_INFO("DBG_RX_DROP_FRAME %s rtw_os_alloc_msdu_pkt fail\n", __func__);
3251 				#endif
3252 			}
3253 			#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3254 			if (act & RTW_RX_MSDU_ACT_FORWARD) {
3255 				#ifdef DBG_TX_DROP_FRAME
3256 				RTW_INFO("DBG_TX_DROP_FRAME %s rtw_os_alloc_msdu_pkt fail\n", __func__);
3257 				#endif
3258 				recv_free_fwd_resource(padapter, fwd_frame, &f_list);
3259 			}
3260 			#endif
3261 			break;
3262 		}
3263 
3264 		#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3265 		if (act & RTW_RX_MSDU_ACT_FORWARD) {
3266 			recv_fwd_pkt_hdl(padapter, sub_pkt, act, fwd_frame, &f_list);
3267 			if (!(act & RTW_RX_MSDU_ACT_INDICATE))
3268 				goto move_to_next;
3269 		}
3270 		#endif
3271 
3272 		if (rtw_recv_indicatepkt_check(prframe, rtw_os_pkt_data(sub_pkt), rtw_os_pkt_len(sub_pkt)) == _SUCCESS)
3273 			subframes[nr_subframes++] = sub_pkt;
3274 		else
3275 			rtw_os_pkt_free(sub_pkt);
3276 
3277 move_to_next:
3278 		/* move the data point to data content */
3279 		pdata += ETH_HLEN;
3280 		a_len -= ETH_HLEN;
3281 
3282 		if (nr_subframes >= MAX_SUBFRAME_COUNT) {
3283 			RTW_WARN("ParseSubframe(): Too many Subframes! Packets dropped!\n");
3284 			break;
3285 		}
3286 
3287 		pdata += nSubframe_Length;
3288 		a_len -= nSubframe_Length;
3289 		if (a_len != 0) {
3290 			padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4 - 1));
3291 			if (padding_len == 4)
3292 				padding_len = 0;
3293 
3294 			if (a_len < padding_len) {
3295 				RTW_INFO("ParseSubframe(): a_len < padding_len !\n");
3296 				break;
3297 			}
3298 			pdata += padding_len;
3299 			a_len -= padding_len;
3300 		}
3301 	}
3302 
3303 	for (i = 0; i < nr_subframes; i++) {
3304 		sub_pkt = subframes[i];
3305 
3306 		/* Indicat the packets to upper layer */
3307 		if (sub_pkt)
3308 			rtw_os_recv_indicate_pkt(padapter, sub_pkt, prframe);
3309 	}
3310 
3311 	prframe->u.hdr.len = 0;
3312 	rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
3313 
3314 	return ret;
3315 }
3316 
recv_process_mpdu(_adapter * padapter,union recv_frame * prframe)3317 static int recv_process_mpdu(_adapter *padapter, union recv_frame *prframe)
3318 {
3319 	_queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
3320 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
3321 	int ret;
3322 
3323 	if (pattrib->amsdu) {
3324 		ret = amsdu_to_msdu(padapter, prframe);
3325 		if (ret != _SUCCESS) {
3326 			#ifdef DBG_RX_DROP_FRAME
3327 			RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" amsdu_to_msdu fail\n"
3328 				, FUNC_ADPT_ARG(padapter));
3329 			#endif
3330 			rtw_free_recvframe(prframe, pfree_recv_queue);
3331 			goto exit;
3332 		}
3333 	} else {
3334 		u8 *msdu = get_recvframe_data(prframe)
3335 			+ pattrib->hdrlen + pattrib->iv_len + RATTRIB_GET_MCTRL_LEN(pattrib);
3336 		u16 msdu_len = prframe->u.hdr.len
3337 			- pattrib->hdrlen - pattrib->iv_len - RATTRIB_GET_MCTRL_LEN(pattrib)
3338 			- (pattrib->encrypt ? pattrib->icv_len : 0);
3339 		enum rtw_rx_llc_hdl llc_hdl = rtw_recv_llc_parse(msdu, msdu_len);
3340 		int act = RTW_RX_MSDU_ACT_INDICATE;
3341 
3342 		#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3343 		struct xmit_frame *fwd_frame = NULL;
3344 		_list f_list;
3345 
3346 		#ifdef CONFIG_RTW_MESH
3347 		if (MLME_IS_MESH(padapter)) {
3348 			if (pattrib->mesh_ctrl_present)
3349 				act = rtw_mesh_rx_msdu_act_check(prframe
3350 					, pattrib->mda, pattrib->msa
3351 					, pattrib->dst, pattrib->src
3352 					, (struct rtw_ieee80211s_hdr *)(msdu - RATTRIB_GET_MCTRL_LEN(pattrib))
3353 					, msdu, llc_hdl
3354 					, &fwd_frame, &f_list);
3355 		} else
3356 		#endif
3357 		if (MLME_IS_AP(padapter))
3358 			act = rtw_ap_rx_msdu_act_check(prframe, pattrib->dst, pattrib->src
3359 					, msdu, llc_hdl, &fwd_frame, &f_list);
3360 		#endif
3361 
3362 		#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3363 		if (!act) {
3364 			rtw_free_recvframe(prframe, pfree_recv_queue);
3365 			ret = _FAIL;
3366 			goto exit;
3367 		}
3368 		#endif
3369 
3370 		rtw_led_rx_control(padapter, pattrib->dst);
3371 
3372 		ret = wlanhdr_to_ethhdr(prframe, llc_hdl);
3373 		if (ret != _SUCCESS) {
3374 			if (act & RTW_RX_MSDU_ACT_INDICATE) {
3375 				#ifdef DBG_RX_DROP_FRAME
3376 				RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" wlanhdr_to_ethhdr: drop pkt\n"
3377 					, FUNC_ADPT_ARG(padapter));
3378 				#endif
3379 			}
3380 			#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3381 			if (act & RTW_RX_MSDU_ACT_FORWARD) {
3382 				#ifdef DBG_TX_DROP_FRAME
3383 				RTW_INFO("DBG_TX_DROP_FRAME %s wlanhdr_to_ethhdr fail\n", __func__);
3384 				#endif
3385 				recv_free_fwd_resource(padapter, fwd_frame, &f_list);
3386 			}
3387 			#endif
3388 			rtw_free_recvframe(prframe, pfree_recv_queue);
3389 			goto exit;
3390 		}
3391 
3392 		#if defined(CONFIG_AP_MODE) || defined(CONFIG_RTW_MESH)
3393 		if (act & RTW_RX_MSDU_ACT_FORWARD) {
3394 			recv_fwd_pkt_hdl(padapter, prframe->u.hdr.pkt, act, fwd_frame, &f_list);
3395 			if (!(act & RTW_RX_MSDU_ACT_INDICATE)) {
3396 				prframe->u.hdr.pkt = NULL;
3397 				rtw_free_recvframe(prframe, pfree_recv_queue);
3398 				goto exit;
3399 			}
3400 		}
3401 		#endif
3402 
3403 		if (!RTW_CANNOT_RUN(padapter)) {
3404 			ret = rtw_recv_indicatepkt_check(prframe
3405 				, get_recvframe_data(prframe), get_recvframe_len(prframe));
3406 			if (ret != _SUCCESS) {
3407 				rtw_free_recvframe(prframe, pfree_recv_queue);
3408 				goto exit;
3409 			}
3410 
3411 			/* indicate this recv_frame */
3412 			ret = rtw_recv_indicatepkt(padapter, prframe);
3413 			if (ret != _SUCCESS) {
3414 				#ifdef DBG_RX_DROP_FRAME
3415 				RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" rtw_recv_indicatepkt fail!\n"
3416 					, FUNC_ADPT_ARG(padapter));
3417 				#endif
3418 				goto exit;
3419 			}
3420 		} else {
3421 			#ifdef DBG_RX_DROP_FRAME
3422 			RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" DS:%u SR:%u\n"
3423 				, FUNC_ADPT_ARG(padapter)
3424 				, rtw_is_drv_stopped(padapter)
3425 				, rtw_is_surprise_removed(padapter));
3426 			#endif
3427 			ret = _SUCCESS; /* don't count as packet drop */
3428 			rtw_free_recvframe(prframe, pfree_recv_queue);
3429 		}
3430 	}
3431 
3432 exit:
3433 	return ret;
3434 }
3435 
3436 #if defined(CONFIG_80211N_HT) && defined(CONFIG_RECV_REORDERING_CTRL)
check_indicate_seq(struct recv_reorder_ctrl * preorder_ctrl,u16 seq_num)3437 static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
3438 {
3439 	PADAPTER padapter = preorder_ctrl->padapter;
3440 	struct recv_priv  *precvpriv = &padapter->recvpriv;
3441 	u8	wsize = preorder_ctrl->wsize_b;
3442 	u16	wend;
3443 
3444 	/* Rx Reorder initialize condition. */
3445 	if (preorder_ctrl->indicate_seq == 0xFFFF) {
3446 		preorder_ctrl->indicate_seq = seq_num;
3447 		#ifdef DBG_RX_SEQ
3448 		RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" tid:%u SN_INIT indicate_seq:%d, seq_num:%d\n"
3449 			, FUNC_ADPT_ARG(padapter), preorder_ctrl->tid, preorder_ctrl->indicate_seq, seq_num);
3450 		#endif
3451 	}
3452 	wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF; /* % 4096; */
3453 
3454 	/* Drop out the packet which SeqNum is smaller than WinStart */
3455 	if (SN_LESS(seq_num, preorder_ctrl->indicate_seq)) {
3456 		#ifdef DBG_RX_DROP_FRAME
3457 		RTW_INFO(FUNC_ADPT_FMT" tid:%u indicate_seq:%d > seq_num:%d\n"
3458 			, FUNC_ADPT_ARG(padapter), preorder_ctrl->tid, preorder_ctrl->indicate_seq, seq_num);
3459 		#endif
3460 		return _FALSE;
3461 	}
3462 
3463 	/*
3464 	* Sliding window manipulation. Conditions includes:
3465 	* 1. Incoming SeqNum is equal to WinStart =>Window shift 1
3466 	* 2. Incoming SeqNum is larger than the WinEnd => Window shift N
3467 	*/
3468 	if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
3469 		preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
3470 		#ifdef DBG_RX_SEQ
3471 		RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" tid:%u SN_EQUAL indicate_seq:%d, seq_num:%d\n"
3472 			, FUNC_ADPT_ARG(padapter), preorder_ctrl->tid, preorder_ctrl->indicate_seq, seq_num);
3473 		#endif
3474 
3475 	} else if (SN_LESS(wend, seq_num)) {
3476 		/* boundary situation, when seq_num cross 0xFFF */
3477 		if (seq_num >= (wsize - 1))
3478 			preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
3479 		else
3480 			preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
3481 
3482 		precvpriv->dbg_rx_ampdu_window_shift_cnt++;
3483 		#ifdef DBG_RX_SEQ
3484 		RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" tid:%u SN_LESS(wend, seq_num) indicate_seq:%d, seq_num:%d\n"
3485 			, FUNC_ADPT_ARG(padapter), preorder_ctrl->tid, preorder_ctrl->indicate_seq, seq_num);
3486 		#endif
3487 	}
3488 
3489 	return _TRUE;
3490 }
3491 
enqueue_reorder_recvframe(struct recv_reorder_ctrl * preorder_ctrl,union recv_frame * prframe)3492 static int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe)
3493 {
3494 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
3495 	_queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
3496 	_list	*phead, *plist;
3497 	union recv_frame *pnextrframe;
3498 	struct rx_pkt_attrib *pnextattrib;
3499 
3500 	/* DbgPrint("+enqueue_reorder_recvframe()\n"); */
3501 
3502 	/* _enter_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3503 	/* _rtw_spinlock_ex(&ppending_recvframe_queue->lock); */
3504 
3505 
3506 	phead = get_list_head(ppending_recvframe_queue);
3507 	plist = get_next(phead);
3508 
3509 	while (rtw_end_of_queue_search(phead, plist) == _FALSE) {
3510 		pnextrframe = LIST_CONTAINOR(plist, union recv_frame, u);
3511 		pnextattrib = &pnextrframe->u.hdr.attrib;
3512 
3513 		if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
3514 			plist = get_next(plist);
3515 		else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num)) {
3516 			/* Duplicate entry is found!! Do not insert current entry. */
3517 
3518 			/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3519 
3520 			return _FALSE;
3521 		} else
3522 			break;
3523 
3524 		/* DbgPrint("enqueue_reorder_recvframe():while\n"); */
3525 
3526 	}
3527 
3528 
3529 	/* _enter_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3530 	/* _rtw_spinlock_ex(&ppending_recvframe_queue->lock); */
3531 
3532 	rtw_list_delete(&(prframe->u.hdr.list));
3533 
3534 	rtw_list_insert_tail(&(prframe->u.hdr.list), plist);
3535 
3536 	/* _rtw_spinunlock_ex(&ppending_recvframe_queue->lock); */
3537 	/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3538 
3539 
3540 	return _TRUE;
3541 
3542 }
3543 
recv_indicatepkts_pkt_loss_cnt(_adapter * padapter,u64 prev_seq,u64 current_seq)3544 static void recv_indicatepkts_pkt_loss_cnt(_adapter *padapter, u64 prev_seq, u64 current_seq)
3545 {
3546 	struct recv_priv *precvpriv = &padapter->recvpriv;
3547 
3548 	if (current_seq < prev_seq) {
3549 		precvpriv->dbg_rx_ampdu_loss_count += (4096 + current_seq - prev_seq);
3550 		precvpriv->rx_drop += (4096 + current_seq - prev_seq);
3551 	} else {
3552 		precvpriv->dbg_rx_ampdu_loss_count += (current_seq - prev_seq);
3553 		precvpriv->rx_drop += (current_seq - prev_seq);
3554 	}
3555 }
3556 
recv_indicatepkts_in_order(_adapter * padapter,struct recv_reorder_ctrl * preorder_ctrl,int bforced)3557 static int recv_indicatepkts_in_order(_adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
3558 {
3559 	/* _irqL irql; */
3560 	_list	*phead, *plist;
3561 	union recv_frame *prframe;
3562 	struct rx_pkt_attrib *pattrib;
3563 	/* u8 index = 0; */
3564 	int bPktInBuf = _FALSE;
3565 	struct recv_priv *precvpriv = &padapter->recvpriv;
3566 	_queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
3567 
3568 	DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_in_oder);
3569 
3570 	/* DbgPrint("+recv_indicatepkts_in_order\n"); */
3571 
3572 	/* _enter_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3573 	/* _rtw_spinlock_ex(&ppending_recvframe_queue->lock); */
3574 
3575 	phead =	get_list_head(ppending_recvframe_queue);
3576 	plist = get_next(phead);
3577 
3578 #if 0
3579 	/* Check if there is any other indication thread running. */
3580 	if (pTS->RxIndicateState == RXTS_INDICATE_PROCESSING)
3581 		return;
3582 #endif
3583 
3584 	/* Handling some condition for forced indicate case. */
3585 	if (bforced == _TRUE) {
3586 		precvpriv->dbg_rx_ampdu_forced_indicate_count++;
3587 		if (rtw_is_list_empty(phead)) {
3588 			/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3589 			/* _rtw_spinunlock_ex(&ppending_recvframe_queue->lock); */
3590 			return _TRUE;
3591 		}
3592 
3593 		prframe = LIST_CONTAINOR(plist, union recv_frame, u);
3594 		pattrib = &prframe->u.hdr.attrib;
3595 
3596 		#ifdef DBG_RX_SEQ
3597 		RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" tid:%u FORCE indicate_seq:%d, seq_num:%d\n"
3598 			, FUNC_ADPT_ARG(padapter), preorder_ctrl->tid, preorder_ctrl->indicate_seq, pattrib->seq_num);
3599 		#endif
3600 		recv_indicatepkts_pkt_loss_cnt(padapter, preorder_ctrl->indicate_seq, pattrib->seq_num);
3601 		preorder_ctrl->indicate_seq = pattrib->seq_num;
3602 	}
3603 
3604 	/* Prepare indication list and indication. */
3605 	/* Check if there is any packet need indicate. */
3606 	while (!rtw_is_list_empty(phead)) {
3607 
3608 		prframe = LIST_CONTAINOR(plist, union recv_frame, u);
3609 		pattrib = &prframe->u.hdr.attrib;
3610 
3611 		if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
3612 
3613 #if 0
3614 			/* This protect buffer from overflow. */
3615 			if (index >= REORDER_WIN_SIZE) {
3616 				RT_ASSERT(FALSE, ("IndicateRxReorderList(): Buffer overflow!!\n"));
3617 				bPktInBuf = TRUE;
3618 				break;
3619 			}
3620 #endif
3621 
3622 			plist = get_next(plist);
3623 			rtw_list_delete(&(prframe->u.hdr.list));
3624 
3625 			if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
3626 				preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
3627 				#ifdef DBG_RX_SEQ
3628 				RTW_INFO("DBG_RX_SEQ "FUNC_ADPT_FMT" tid:%u SN_EQUAL indicate_seq:%d, seq_num:%d\n"
3629 					, FUNC_ADPT_ARG(padapter), preorder_ctrl->tid, preorder_ctrl->indicate_seq, pattrib->seq_num);
3630 				#endif
3631 			}
3632 
3633 #if 0
3634 			index++;
3635 			if (index == 1) {
3636 				/* Cancel previous pending timer. */
3637 				/* PlatformCancelTimer(Adapter, &pTS->RxPktPendingTimer); */
3638 				if (bforced != _TRUE) {
3639 					/* RTW_INFO("_cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);\n"); */
3640 					_cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
3641 				}
3642 			}
3643 #endif
3644 
3645 			/* Set this as a lock to make sure that only one thread is indicating packet. */
3646 			/* pTS->RxIndicateState = RXTS_INDICATE_PROCESSING; */
3647 
3648 			/* Indicate packets */
3649 			/* RT_ASSERT((index<=REORDER_WIN_SIZE), ("RxReorderIndicatePacket(): Rx Reorder buffer full!!\n")); */
3650 
3651 
3652 			/* indicate this recv_frame */
3653 			/* DbgPrint("recv_indicatepkts_in_order, indicate_seq=%d, seq_num=%d\n", precvpriv->indicate_seq, pattrib->seq_num); */
3654 			if (recv_process_mpdu(padapter, prframe) != _SUCCESS)
3655 				precvpriv->dbg_rx_drop_count++;
3656 
3657 			/* Update local variables. */
3658 			bPktInBuf = _FALSE;
3659 
3660 		} else {
3661 			bPktInBuf = _TRUE;
3662 			break;
3663 		}
3664 
3665 		/* DbgPrint("recv_indicatepkts_in_order():while\n"); */
3666 
3667 	}
3668 
3669 	/* _rtw_spinunlock_ex(&ppending_recvframe_queue->lock); */
3670 	/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3671 
3672 #if 0
3673 	/* Release the indication lock and set to new indication step. */
3674 	if (bPktInBuf) {
3675 		/*  Set new pending timer. */
3676 		/* pTS->RxIndicateState = RXTS_INDICATE_REORDER; */
3677 		/* PlatformSetTimer(Adapter, &pTS->RxPktPendingTimer, pHTInfo->RxReorderPendingTime); */
3678 
3679 		_set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
3680 	} else {
3681 		/* pTS->RxIndicateState = RXTS_INDICATE_IDLE; */
3682 	}
3683 #endif
3684 	/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3685 
3686 	/* return _TRUE; */
3687 	return bPktInBuf;
3688 
3689 }
3690 
recv_indicatepkt_reorder(_adapter * padapter,union recv_frame * prframe)3691 static int recv_indicatepkt_reorder(_adapter *padapter, union recv_frame *prframe)
3692 {
3693 	_irqL irql;
3694 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
3695 	struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
3696 	_queue *ppending_recvframe_queue = preorder_ctrl ? &preorder_ctrl->pending_recvframe_queue : NULL;
3697 	struct recv_priv  *precvpriv = &padapter->recvpriv;
3698 
3699 	if (!pattrib->qos || !preorder_ctrl || preorder_ctrl->enable == _FALSE)
3700 		goto _success_exit;
3701 
3702 
3703 	DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_reoder);
3704 
3705 	_enter_critical_bh(&ppending_recvframe_queue->lock, &irql);
3706 
3707 
3708 	if(rtw_test_and_clear_bit(RTW_RECV_ACK_OR_TIMEOUT, &preorder_ctrl->rec_abba_rsp_ack))
3709 		preorder_ctrl->indicate_seq = 0xFFFF;
3710 	if(rtw_test_and_clear_bit(RTW_RECV_REORDER_WOW, &preorder_ctrl->rec_abba_rsp_ack)) {
3711 		preorder_ctrl->indicate_seq = 0xFFFF;
3712 		RTW_INFO("DBG_RX_SEQ %s:preorder_ctrl->rec_abba_rsp_ack = %lu,indicate_seq = %d\n"
3713 		, __func__
3714 		, preorder_ctrl->rec_abba_rsp_ack
3715 		, preorder_ctrl->indicate_seq);
3716 	}
3717 	#ifdef DBG_RX_SEQ
3718 	RTW_INFO("DBG_RX_SEQ %s:preorder_ctrl->rec_abba_rsp_ack = %lu,indicate_seq = %d\n"
3719 		, __func__
3720 		, preorder_ctrl->rec_abba_rsp_ack
3721 		, preorder_ctrl->indicate_seq);
3722 	#endif
3723 
3724 	/* s2. check if winstart_b(indicate_seq) needs to been updated */
3725 	if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num)) {
3726 		precvpriv->dbg_rx_ampdu_drop_count++;
3727 		/* pHTInfo->RxReorderDropCounter++; */
3728 		/* ReturnRFDList(Adapter, pRfd); */
3729 		/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3730 		/* return _FAIL; */
3731 
3732 		#ifdef DBG_RX_DROP_FRAME
3733 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" check_indicate_seq fail\n"
3734 			, FUNC_ADPT_ARG(padapter));
3735 		#endif
3736 #if 0
3737 		rtw_recv_indicatepkt(padapter, prframe);
3738 
3739 		_exit_critical_bh(&ppending_recvframe_queue->lock, &irql);
3740 
3741 		goto _success_exit;
3742 #else
3743 		goto _err_exit;
3744 #endif
3745 	}
3746 
3747 
3748 	/* s3. Insert all packet into Reorder Queue to maintain its ordering. */
3749 	if (!enqueue_reorder_recvframe(preorder_ctrl, prframe)) {
3750 		/* DbgPrint("recv_indicatepkt_reorder, enqueue_reorder_recvframe fail!\n"); */
3751 		/* _exit_critical_ex(&ppending_recvframe_queue->lock, &irql); */
3752 		/* return _FAIL; */
3753 		#ifdef DBG_RX_DROP_FRAME
3754 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" enqueue_reorder_recvframe fail\n"
3755 			, FUNC_ADPT_ARG(padapter));
3756 		#endif
3757 		goto _err_exit;
3758 	}
3759 
3760 
3761 	/* s4. */
3762 	/* Indication process. */
3763 	/* After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
3764 	/* with the SeqNum smaller than latest WinStart and buffer other packets. */
3765 	/*  */
3766 	/* For Rx Reorder condition: */
3767 	/* 1. All packets with SeqNum smaller than WinStart => Indicate */
3768 	/* 2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
3769 	/*  */
3770 
3771 	/* recv_indicatepkts_in_order(padapter, preorder_ctrl, _TRUE); */
3772 	if (recv_indicatepkts_in_order(padapter, preorder_ctrl, _FALSE) == _TRUE) {
3773 		if (!preorder_ctrl->bReorderWaiting) {
3774 			preorder_ctrl->bReorderWaiting = _TRUE;
3775 			_set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
3776 		}
3777 		_exit_critical_bh(&ppending_recvframe_queue->lock, &irql);
3778 	} else {
3779 		preorder_ctrl->bReorderWaiting = _FALSE;
3780 		_exit_critical_bh(&ppending_recvframe_queue->lock, &irql);
3781 		_cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
3782 	}
3783 
3784 	return RTW_RX_HANDLED;
3785 
3786 _success_exit:
3787 
3788 	return _SUCCESS;
3789 
3790 _err_exit:
3791 
3792 	_exit_critical_bh(&ppending_recvframe_queue->lock, &irql);
3793 
3794 	return _FAIL;
3795 }
3796 
3797 
rtw_reordering_ctrl_timeout_handler(void * pcontext)3798 void rtw_reordering_ctrl_timeout_handler(void *pcontext)
3799 {
3800 	_irqL irql;
3801 	struct recv_reorder_ctrl *preorder_ctrl = (struct recv_reorder_ctrl *)pcontext;
3802 	_adapter *padapter = NULL;
3803 	_queue *ppending_recvframe_queue = NULL;
3804 
3805 
3806 	if (preorder_ctrl == NULL)
3807 		return;
3808 
3809 	padapter = preorder_ctrl->padapter;
3810 	if (RTW_CANNOT_RUN(padapter))
3811 		return;
3812 
3813 	ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
3814 
3815 	/* RTW_INFO("+rtw_reordering_ctrl_timeout_handler()=>\n"); */
3816 
3817 	_enter_critical_bh(&ppending_recvframe_queue->lock, &irql);
3818 
3819 	preorder_ctrl->bReorderWaiting = _FALSE;
3820 
3821 	if (recv_indicatepkts_in_order(padapter, preorder_ctrl, _TRUE) == _TRUE)
3822 		_set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
3823 
3824 	_exit_critical_bh(&ppending_recvframe_queue->lock, &irql);
3825 
3826 }
3827 #endif /* defined(CONFIG_80211N_HT) && defined(CONFIG_RECV_REORDERING_CTRL) */
3828 
recv_set_iseq_before_mpdu_process(union recv_frame * rframe,u16 seq_num,const char * caller)3829 static void recv_set_iseq_before_mpdu_process(union recv_frame *rframe, u16 seq_num, const char *caller)
3830 {
3831 #if defined(CONFIG_80211N_HT) && defined(CONFIG_RECV_REORDERING_CTRL)
3832 	struct recv_reorder_ctrl *reorder_ctrl = rframe->u.hdr.preorder_ctrl;
3833 
3834 	if (reorder_ctrl) {
3835 		reorder_ctrl->indicate_seq = seq_num;
3836 		#ifdef DBG_RX_SEQ
3837 		RTW_INFO("DBG_RX_SEQ %s("ADPT_FMT")-B tid:%u indicate_seq:%d, seq_num:%d\n"
3838 			, caller, ADPT_ARG(reorder_ctrl->padapter)
3839 			, reorder_ctrl->tid, reorder_ctrl->indicate_seq, seq_num);
3840 		#endif
3841 	}
3842 #endif
3843 }
3844 
recv_set_iseq_after_mpdu_process(union recv_frame * rframe,u16 seq_num,const char * caller)3845 static void recv_set_iseq_after_mpdu_process(union recv_frame *rframe, u16 seq_num, const char *caller)
3846 {
3847 #if defined(CONFIG_80211N_HT) && defined(CONFIG_RECV_REORDERING_CTRL)
3848 	struct recv_reorder_ctrl *reorder_ctrl = rframe->u.hdr.preorder_ctrl;
3849 
3850 	if (reorder_ctrl) {
3851 		reorder_ctrl->indicate_seq = (reorder_ctrl->indicate_seq + 1) % 4096;
3852 		#ifdef DBG_RX_SEQ
3853 		RTW_INFO("DBG_RX_SEQ %s("ADPT_FMT")-A tid:%u indicate_seq:%d, seq_num:%d\n"
3854 			, caller, ADPT_ARG(reorder_ctrl->padapter)
3855 			, reorder_ctrl->tid, reorder_ctrl->indicate_seq, seq_num);
3856 		#endif
3857 	}
3858 #endif
3859 }
3860 
3861 #ifdef CONFIG_MP_INCLUDED
validate_mp_recv_frame(_adapter * adapter,union recv_frame * precv_frame)3862 int validate_mp_recv_frame(_adapter *adapter, union recv_frame *precv_frame)
3863 {
3864 	int ret = _SUCCESS;
3865 	u8 *ptr = precv_frame->u.hdr.rx_data;
3866 	u8 type, subtype;
3867 	struct mp_priv *pmppriv = &adapter->mppriv;
3868 	struct mp_tx		*pmptx;
3869 	unsigned char	*sa , *da, *bs;
3870 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
3871 	u32 i = 0;
3872 	u8 rtk_prefix[]={0x52, 0x65, 0x61, 0x6C, 0x4C, 0x6F, 0x76, 0x65, 0x54, 0x65, 0x6B};
3873 	u8 *prx_data;
3874 	pmptx = &pmppriv->tx;
3875 
3876 
3877 	if (pmppriv->mplink_brx == _FALSE) {
3878 
3879 		u8 bDumpRxPkt = 0;
3880 		type =  GetFrameType(ptr);
3881 		subtype = get_frame_sub_type(ptr); /* bit(7)~bit(2)	 */
3882 
3883 		RTW_DBG("hdr len = %d iv_len=%d \n", pattrib->hdrlen , pattrib->iv_len);
3884 		prx_data = ptr + pattrib->hdrlen + pattrib->iv_len;
3885 
3886 		for (i = 0; i < precv_frame->u.hdr.len; i++) {
3887 			if (precv_frame->u.hdr.len < (11 + i))
3888 				break;
3889 
3890 			if (_rtw_memcmp(prx_data + i, (void *)&rtk_prefix, 11) == _FALSE) {
3891 				bDumpRxPkt = 0;
3892 				RTW_DBG("prx_data = %02X != rtk_prefix[%d] = %02X \n", *(prx_data + i), i , rtk_prefix[i]);
3893 				} else {
3894 				bDumpRxPkt = 1;
3895 				RTW_DBG("prx_data = %02X = rtk_prefix[%d] = %02X \n", *(prx_data + i), i , rtk_prefix[i]);
3896 				break;
3897 				}
3898 		}
3899 
3900 		if (bDumpRxPkt == 1) { /* dump all rx packets */
3901 			int i;
3902 			RTW_INFO("############ type:0x%02x subtype:0x%02x #################\n", type, subtype);
3903 
3904 			for (i = 0; i < precv_frame->u.hdr.len; i = i + 8)
3905 				RTW_INFO("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr + i),
3906 					*(ptr + i + 1), *(ptr + i + 2) , *(ptr + i + 3) , *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
3907 				RTW_INFO("#############################\n");
3908 				_rtw_memset(pmppriv->mplink_buf, '\0' , sizeof(pmppriv->mplink_buf));
3909 				_rtw_memcpy(pmppriv->mplink_buf, ptr, precv_frame->u.hdr.len);
3910 				pmppriv->mplink_rx_len = precv_frame->u.hdr.len;
3911 				pmppriv->mplink_brx =_TRUE;
3912 		}
3913 	}
3914 	if (pmppriv->bloopback) {
3915 		if (_rtw_memcmp(ptr + 24, pmptx->buf + 24, precv_frame->u.hdr.len - 24) == _FALSE) {
3916 			RTW_INFO("Compare payload content Fail !!!\n");
3917 			ret = _FAIL;
3918 		}
3919 	}
3920  	if (pmppriv->bSetRxBssid == _TRUE) {
3921 
3922 		sa = get_addr2_ptr(ptr);
3923 		da = GetAddr1Ptr(ptr);
3924 		bs = GetAddr3Ptr(ptr);
3925 		type =	GetFrameType(ptr);
3926 		subtype = get_frame_sub_type(ptr); /* bit(7)~bit(2)  */
3927 
3928 		if (_rtw_memcmp(bs, adapter->mppriv.network_macaddr, ETH_ALEN) == _FALSE)
3929 			ret = _FAIL;
3930 
3931 		RTW_DBG("############ type:0x%02x subtype:0x%02x #################\n", type, subtype);
3932 		RTW_DBG("A2 sa %02X:%02X:%02X:%02X:%02X:%02X \n", *(sa) , *(sa + 1), *(sa+ 2), *(sa + 3), *(sa + 4), *(sa + 5));
3933 		RTW_DBG("A1 da %02X:%02X:%02X:%02X:%02X:%02X \n", *(da) , *(da + 1), *(da+ 2), *(da + 3), *(da + 4), *(da + 5));
3934 		RTW_DBG("A3 bs %02X:%02X:%02X:%02X:%02X:%02X \n --------------------------\n", *(bs) , *(bs + 1), *(bs+ 2), *(bs + 3), *(bs + 4), *(bs + 5));
3935 	}
3936 
3937 	if (!adapter->mppriv.bmac_filter)
3938 		return ret;
3939 
3940 	if (_rtw_memcmp(get_addr2_ptr(ptr), adapter->mppriv.mac_filter, ETH_ALEN) == _FALSE)
3941 		ret = _FAIL;
3942 
3943 	return ret;
3944 }
3945 
MPwlanhdr_to_ethhdr(union recv_frame * precvframe)3946 static sint MPwlanhdr_to_ethhdr(union recv_frame *precvframe)
3947 {
3948 	sint	rmv_len;
3949 	u16 len;
3950 #ifdef CONFIG_RTL8822CS_WIFI_HDF
3951 	u16 eth_type;
3952 #endif
3953 	u8 mcastheadermac[] = {0x01, 0x00, 0x5e};
3954 	sint ret = _SUCCESS;
3955 	_adapter			*adapter = precvframe->u.hdr.adapter;
3956 
3957 	u8	*ptr = get_recvframe_data(precvframe) ; /* point to frame_ctrl field */
3958 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
3959 	enum rtw_rx_llc_hdl llc_hdl;
3960 
3961 
3962 	if (pattrib->encrypt)
3963 		recvframe_pull_tail(precvframe, pattrib->icv_len);
3964 
3965 	llc_hdl = rtw_recv_llc_parse(ptr + pattrib->hdrlen + pattrib->iv_len
3966 				, precvframe->u.hdr.len - pattrib->hdrlen - pattrib->iv_len);
3967 
3968 	rmv_len = pattrib->hdrlen + pattrib->iv_len + (llc_hdl ? SNAP_SIZE : 0);
3969 	len = precvframe->u.hdr.len - rmv_len;
3970 
3971 #ifdef CONFIG_RTL8822CS_WIFI_HDF
3972 	_rtw_memcpy(&eth_type, ptr + rmv_len, 2);
3973 	eth_type = ntohs((unsigned short)eth_type); /* pattrib->ether_type */
3974 	pattrib->eth_type = eth_type;
3975 #endif
3976 
3977 	ptr = recvframe_pull(precvframe, (rmv_len - sizeof(struct ethhdr) + (llc_hdl ? 2 : 0)));
3978 
3979 	_rtw_memcpy(ptr, pattrib->dst, ETH_ALEN);
3980 	_rtw_memcpy(ptr + ETH_ALEN, pattrib->src, ETH_ALEN);
3981 
3982 	if (!llc_hdl) {
3983 		len = htons(len);
3984 		_rtw_memcpy(ptr + 12, &len, 2);
3985 	}
3986 
3987 
3988 	len = htons(pattrib->seq_num);
3989 	/* RTW_INFO("wlan seq = %d ,seq_num =%x\n",len,pattrib->seq_num); */
3990 	_rtw_memcpy(ptr + 12, &len, 2);
3991 	if (adapter->mppriv.bRTWSmbCfg == _TRUE) {
3992 		/* if(_rtw_memcmp(mcastheadermac, pattrib->dst, 3) == _TRUE) */ /* SimpleConfig Dest. */
3993 		/*			_rtw_memcpy(ptr+ETH_ALEN, pattrib->bssid, ETH_ALEN); */
3994 
3995 		if (_rtw_memcmp(mcastheadermac, pattrib->bssid, 3) == _TRUE) /* SimpleConfig Dest. */
3996 			_rtw_memcpy(ptr, pattrib->bssid, ETH_ALEN);
3997 
3998 	}
3999 
4000 
4001 	return ret;
4002 
4003 }
4004 
4005 
mp_recv_frame(_adapter * padapter,union recv_frame * rframe)4006 int mp_recv_frame(_adapter *padapter, union recv_frame *rframe)
4007 {
4008 	int ret = _SUCCESS;
4009 	struct rx_pkt_attrib *pattrib = &rframe->u.hdr.attrib;
4010 	_queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
4011 #ifdef CONFIG_MP_INCLUDED
4012 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
4013 	struct mp_priv *pmppriv = &padapter->mppriv;
4014 #endif /* CONFIG_MP_INCLUDED */
4015 	u8 type;
4016 	u8 *ptr = rframe->u.hdr.rx_data;
4017 	u8 *psa, *pda, *pbssid;
4018 	struct sta_info *psta = NULL;
4019 	DBG_COUNTER(padapter->rx_logs.core_rx_pre);
4020 
4021 	if ((check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE)) { /* &&(padapter->mppriv.check_mp_pkt == 0)) */
4022 		if (pattrib->crc_err == 1)
4023 			padapter->mppriv.rx_crcerrpktcount++;
4024 		else {
4025 			if (_SUCCESS == validate_mp_recv_frame(padapter, rframe))
4026 				padapter->mppriv.rx_pktcount++;
4027 			else
4028 				padapter->mppriv.rx_pktcount_filter_out++;
4029 		}
4030 
4031 		if (pmppriv->rx_bindicatePkt == _FALSE) {
4032 			ret = _FAIL;
4033 			rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
4034 			goto exit;
4035 		} else {
4036 			type =	GetFrameType(ptr);
4037 			pattrib->to_fr_ds = get_tofr_ds(ptr);
4038 			pattrib->frag_num = GetFragNum(ptr);
4039 			pattrib->seq_num = GetSequence(ptr);
4040 			pattrib->pw_save = GetPwrMgt(ptr);
4041 			pattrib->mfrag = GetMFrag(ptr);
4042 			pattrib->mdata = GetMData(ptr);
4043 			pattrib->privacy = GetPrivacy(ptr);
4044 			pattrib->order = GetOrder(ptr);
4045 
4046 			if (type == WIFI_DATA_TYPE) {
4047 				pda = get_da(ptr);
4048 				psa = get_sa(ptr);
4049 				pbssid = get_hdr_bssid(ptr);
4050 
4051 				_rtw_memcpy(pattrib->dst, pda, ETH_ALEN);
4052 				_rtw_memcpy(pattrib->src, psa, ETH_ALEN);
4053 				_rtw_memcpy(pattrib->bssid, pbssid, ETH_ALEN);
4054 
4055 				switch (pattrib->to_fr_ds) {
4056 				case 0:
4057 					_rtw_memcpy(pattrib->ra, pda, ETH_ALEN);
4058 					_rtw_memcpy(pattrib->ta, psa, ETH_ALEN);
4059 					ret = sta2sta_data_frame(padapter, rframe, &psta);
4060 					break;
4061 				case 1:
4062 					_rtw_memcpy(pattrib->ra, pbssid, ETH_ALEN);
4063 					_rtw_memcpy(pattrib->ta, psa, ETH_ALEN);
4064 					ret = sta2ap_data_frame(padapter, rframe, &psta);
4065 					break;
4066 				case 2:
4067 					_rtw_memcpy(pattrib->ra, pda, ETH_ALEN);
4068 					_rtw_memcpy(pattrib->ta, pbssid, ETH_ALEN);
4069 					ret = ap2sta_data_frame(padapter, rframe, &psta);
4070 					break;
4071 				case 3:
4072 					_rtw_memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
4073 					_rtw_memcpy(pattrib->ta, get_addr2_ptr(ptr), ETH_ALEN);
4074 					ret = _FAIL;
4075 					break;
4076 				default:
4077 					ret = _FAIL;
4078 					break;
4079 				}
4080 
4081 				if (ret != _SUCCESS) {
4082 #ifdef DBG_RX_DROP_FRAME
4083 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" 2_data_frame fail: drop pkt\n"
4084 															, FUNC_ADPT_ARG(padapter));
4085 #endif
4086 					ret = _FAIL;
4087 					goto exit;
4088 				}
4089 
4090 				ret = MPwlanhdr_to_ethhdr(rframe);
4091 
4092 				if (ret != _SUCCESS) {
4093 					#ifdef DBG_RX_DROP_FRAME
4094 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" wlanhdr_to_ethhdr: drop pkt\n"
4095 						, FUNC_ADPT_ARG(padapter));
4096 					#endif
4097 					ret = _FAIL;
4098 					goto exit;
4099 				}
4100 				if (!RTW_CANNOT_RUN(padapter)) {
4101 					/* indicate this recv_frame */
4102 					ret = rtw_recv_indicatepkt(padapter, rframe);
4103 					if (ret != _SUCCESS) {
4104 						#ifdef DBG_RX_DROP_FRAME
4105 						RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" rtw_recv_indicatepkt fail!\n"
4106 							, FUNC_ADPT_ARG(padapter));
4107 						#endif
4108 						ret = _FAIL;
4109 						goto exit;
4110 					}
4111 				} else {
4112 					#ifdef DBG_RX_DROP_FRAME
4113 					RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" bDriverStopped(%s) OR bSurpriseRemoved(%s)\n"
4114 						, FUNC_ADPT_ARG(padapter)
4115 						, rtw_is_drv_stopped(padapter) ? "True" : "False"
4116 						, rtw_is_surprise_removed(padapter) ? "True" : "False");
4117 					#endif
4118 					ret = _FAIL;
4119 					goto exit;
4120 				}
4121 
4122 			}
4123 		}
4124 	}
4125 exit:
4126 	rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
4127 	ret = _FAIL;
4128 	return ret;
4129 
4130 }
4131 #endif
4132 
4133 
4134 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
recv_frame_monitor(_adapter * padapter,union recv_frame * rframe)4135 int recv_frame_monitor(_adapter *padapter, union recv_frame *rframe)
4136 {
4137 	int ret = _SUCCESS;
4138 	_queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
4139 
4140 #ifdef CONFIG_WIFI_MONITOR
4141 	struct net_device *ndev = padapter->pnetdev;
4142 	_pkt *pskb = NULL;
4143 
4144 	if (rframe == NULL)
4145 		goto exit;
4146 
4147 	/* read skb information from recv frame */
4148 	pskb = rframe->u.hdr.pkt;
4149 	pskb->len = rframe->u.hdr.len;
4150 	pskb->data = rframe->u.hdr.rx_data;
4151 	skb_set_tail_pointer(pskb, rframe->u.hdr.len);
4152 
4153 	if (ndev->type == ARPHRD_IEEE80211_RADIOTAP) {
4154 		/* fill radiotap header */
4155 		if (rtw_fill_radiotap_hdr(padapter, &rframe->u.hdr.attrib, (u8 *)pskb) == _FAIL) {
4156 			ret = _FAIL;
4157 			goto exit;
4158 		}
4159 	}
4160 
4161 	/* write skb information to recv frame */
4162 	skb_reset_mac_header(pskb);
4163 	rframe->u.hdr.len = pskb->len;
4164 	rframe->u.hdr.rx_data = pskb->data;
4165 	rframe->u.hdr.rx_head = pskb->head;
4166 	rframe->u.hdr.rx_tail = skb_tail_pointer(pskb);
4167 	rframe->u.hdr.rx_end = skb_end_pointer(pskb);
4168 
4169 	if (!RTW_CANNOT_RUN(padapter)) {
4170 		/* indicate this recv_frame */
4171 		ret = rtw_recv_monitor(padapter, rframe);
4172 	} else
4173 		ret = _FAIL;
4174 
4175 exit:
4176 #endif /* CONFIG_WIFI_MONITOR */
4177 
4178 	if (rframe) /* free this recv_frame */
4179 		rtw_free_recvframe(rframe, pfree_recv_queue);
4180 
4181 	return ret;
4182 }
4183 #endif
recv_func_prehandle(_adapter * padapter,union recv_frame * rframe)4184 int recv_func_prehandle(_adapter *padapter, union recv_frame *rframe)
4185 {
4186 	int ret = _SUCCESS;
4187 #ifdef DBG_RX_COUNTER_DUMP
4188 	struct rx_pkt_attrib *pattrib = &rframe->u.hdr.attrib;
4189 #endif
4190 	_queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
4191 
4192 #ifdef DBG_RX_COUNTER_DUMP
4193 	if (padapter->dump_rx_cnt_mode & DUMP_DRV_RX_COUNTER) {
4194 		if (pattrib->crc_err == 1)
4195 			padapter->drv_rx_cnt_crcerror++;
4196 		else
4197 			padapter->drv_rx_cnt_ok++;
4198 	}
4199 #endif
4200 
4201 #ifdef CONFIG_MP_INCLUDED
4202 	if (padapter->registrypriv.mp_mode == 1 || padapter->mppriv.bRTWSmbCfg == _TRUE) {
4203 		mp_recv_frame(padapter, rframe);
4204 		ret = _FAIL;
4205 		goto exit;
4206 	} else
4207 #endif
4208 	{
4209 		/* check the frame crtl field and decache */
4210 		ret = validate_recv_frame(padapter, rframe);
4211 		if (ret != _SUCCESS) {
4212 			rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
4213 			goto exit;
4214 		}
4215 	}
4216 exit:
4217 	return ret;
4218 }
4219 
4220 /*#define DBG_RX_BMC_FRAME*/
recv_func_posthandle(_adapter * padapter,union recv_frame * prframe)4221 int recv_func_posthandle(_adapter *padapter, union recv_frame *prframe)
4222 {
4223 	int ret = _SUCCESS;
4224 	union recv_frame *orig_prframe = prframe;
4225 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
4226 	struct recv_priv *precvpriv = &padapter->recvpriv;
4227 	_queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
4228 #ifdef CONFIG_TDLS
4229 	u8 *psnap_type, *pcategory;
4230 #endif /* CONFIG_TDLS */
4231 
4232 	DBG_COUNTER(padapter->rx_logs.core_rx_post);
4233 
4234 	prframe = decryptor(padapter, prframe);
4235 	if (prframe == NULL) {
4236 		#ifdef DBG_RX_DROP_FRAME
4237 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" decryptor: drop pkt\n"
4238 			, FUNC_ADPT_ARG(padapter));
4239 		#endif
4240 		ret = _FAIL;
4241 		DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_err);
4242 		goto _recv_data_drop;
4243 	}
4244 
4245 #ifdef DBG_RX_BMC_FRAME
4246 	if (IS_MCAST(pattrib->ra))
4247 		RTW_INFO("%s =>"ADPT_FMT" Rx BC/MC from "MAC_FMT"\n", __func__, ADPT_ARG(padapter), MAC_ARG(pattrib->ta));
4248 #endif
4249 
4250 #if 0
4251 	if (is_primary_adapter(padapter)) {
4252 		RTW_INFO("+++\n");
4253 		{
4254 			int i;
4255 			u8	*ptr = get_recvframe_data(prframe);
4256 			for (i = 0; i < 140; i = i + 8)
4257 				RTW_INFO("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:", *(ptr + i),
4258 					*(ptr + i + 1), *(ptr + i + 2) , *(ptr + i + 3) , *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
4259 
4260 		}
4261 		RTW_INFO("---\n");
4262 	}
4263 #endif
4264 
4265 #ifdef CONFIG_TDLS
4266 	/* check TDLS frame */
4267 	psnap_type = get_recvframe_data(orig_prframe) + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
4268 	pcategory = psnap_type + ETH_TYPE_LEN + PAYLOAD_TYPE_LEN;
4269 
4270 	if ((_rtw_memcmp(psnap_type, SNAP_ETH_TYPE_TDLS, ETH_TYPE_LEN)) &&
4271 	    ((*pcategory == RTW_WLAN_CATEGORY_TDLS) || (*pcategory == RTW_WLAN_CATEGORY_P2P))) {
4272 		ret = OnTDLS(padapter, prframe);
4273 		if (ret == _FAIL)
4274 			goto _exit_recv_func;
4275 	}
4276 #endif /* CONFIG_TDLS */
4277 
4278 	prframe = recvframe_chk_defrag(padapter, prframe);
4279 	if (prframe == NULL)	{
4280 		#ifdef DBG_RX_DROP_FRAME
4281 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" recvframe_chk_defrag: drop pkt\n"
4282 			, FUNC_ADPT_ARG(padapter));
4283 		#endif
4284 		DBG_COUNTER(padapter->rx_logs.core_rx_post_defrag_err);
4285 		goto _recv_data_drop;
4286 	}
4287 
4288 	prframe = portctrl(padapter, prframe);
4289 	if (prframe == NULL) {
4290 		#ifdef DBG_RX_DROP_FRAME
4291 		RTW_INFO("DBG_RX_DROP_FRAME "FUNC_ADPT_FMT" portctrl: drop pkt\n"
4292 			, FUNC_ADPT_ARG(padapter));
4293 		#endif
4294 		ret = _FAIL;
4295 		DBG_COUNTER(padapter->rx_logs.core_rx_post_portctrl_err);
4296 		goto _recv_data_drop;
4297 	}
4298 
4299 	count_rx_stats(padapter, prframe, NULL);
4300 
4301 #ifdef CONFIG_WAPI_SUPPORT
4302 	rtw_wapi_update_info(padapter, prframe);
4303 #endif
4304 
4305 #if defined(CONFIG_80211N_HT) && defined(CONFIG_RECV_REORDERING_CTRL)
4306 	/* including perform A-MPDU Rx Ordering Buffer Control */
4307 	ret = recv_indicatepkt_reorder(padapter, prframe);
4308 	if (ret == _FAIL) {
4309 		rtw_free_recvframe(orig_prframe, pfree_recv_queue);
4310 		goto _recv_data_drop;
4311 	} else if (ret == RTW_RX_HANDLED) /* queued OR indicated in order */
4312 		goto _exit_recv_func;
4313 #endif
4314 
4315 	recv_set_iseq_before_mpdu_process(prframe, pattrib->seq_num, __func__);
4316 	ret = recv_process_mpdu(padapter, prframe);
4317 	recv_set_iseq_after_mpdu_process(prframe, pattrib->seq_num, __func__);
4318 	if (ret == _FAIL)
4319 		goto _recv_data_drop;
4320 
4321 _exit_recv_func:
4322 	return ret;
4323 
4324 _recv_data_drop:
4325 	precvpriv->dbg_rx_drop_count++;
4326 	return ret;
4327 }
4328 
recv_func(_adapter * padapter,union recv_frame * rframe)4329 int recv_func(_adapter *padapter, union recv_frame *rframe)
4330 {
4331 	int ret;
4332 	struct rx_pkt_attrib *prxattrib = &rframe->u.hdr.attrib;
4333 	struct recv_priv *recvpriv = &padapter->recvpriv;
4334 	struct security_priv *psecuritypriv = &padapter->securitypriv;
4335 	struct mlme_priv *mlmepriv = &padapter->mlmepriv;
4336 	u8 *ptr = rframe->u.hdr.rx_data;
4337 #ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL
4338 	u8 type;
4339 #endif
4340 
4341 	if (check_fwstate(mlmepriv, WIFI_MONITOR_STATE)
4342 #ifdef RTW_SIMPLE_CONFIG
4343 		|| (check_fwstate(mlmepriv, WIFI_AP_STATE) && padapter->rtw_simple_config == _TRUE && IS_MCAST(get_ra(ptr)))
4344 #endif
4345 		) {
4346 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
4347 		recv_frame_monitor(padapter, rframe);
4348 #endif
4349 		ret = _SUCCESS;
4350 		goto exit;
4351 	}
4352 
4353 #ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL
4354 	type = GetFrameType(ptr);
4355 	if ((type == WIFI_DATA_TYPE)&& check_fwstate(mlmepriv, WIFI_STATION_STATE)) {
4356 		struct wlan_network *cur_network = &(mlmepriv->cur_network);
4357 		if ( _rtw_memcmp(get_addr2_ptr(ptr), cur_network->network.MacAddress, ETH_ALEN)==0) {
4358 			recv_frame_monitor(padapter, rframe);
4359 			ret = _SUCCESS;
4360 			goto exit;
4361 		}
4362 	}
4363 #endif
4364 		/* check if need to handle uc_swdec_pending_queue*/
4365 		if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey) {
4366 			union recv_frame *pending_frame;
4367 			int cnt = 0;
4368 
4369 			while ((pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
4370 				cnt++;
4371 				DBG_COUNTER(padapter->rx_logs.core_rx_dequeue);
4372 				recv_func_posthandle(padapter, pending_frame);
4373 			}
4374 
4375 			if (cnt)
4376 				RTW_INFO(FUNC_ADPT_FMT" dequeue %d from uc_swdec_pending_queue\n",
4377 					 FUNC_ADPT_ARG(padapter), cnt);
4378 		}
4379 
4380 	DBG_COUNTER(padapter->rx_logs.core_rx);
4381 	ret = recv_func_prehandle(padapter, rframe);
4382 
4383 	if (ret == _SUCCESS) {
4384 
4385 		/* check if need to enqueue into uc_swdec_pending_queue*/
4386 		if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
4387 		    !IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
4388 		    (prxattrib->bdecrypted == 0 || psecuritypriv->sw_decrypt == _TRUE) &&
4389 		    psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPAPSK &&
4390 		    !psecuritypriv->busetkipkey) {
4391 			DBG_COUNTER(padapter->rx_logs.core_rx_enqueue);
4392 			rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
4393 			/* RTW_INFO("%s: no key, enqueue uc_swdec_pending_queue\n", __func__); */
4394 
4395 			if (recvpriv->free_recvframe_cnt < NR_RECVFRAME / 4) {
4396 				/* to prevent from recvframe starvation, get recvframe from uc_swdec_pending_queue to free_recvframe_cnt */
4397 				rframe = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue);
4398 				if (rframe)
4399 					goto do_posthandle;
4400 			}
4401 			goto exit;
4402 		}
4403 
4404 do_posthandle:
4405 		ret = recv_func_posthandle(padapter, rframe);
4406 	}
4407 
4408 exit:
4409 	return ret;
4410 }
4411 
4412 
rtw_recv_entry(union recv_frame * precvframe)4413 s32 rtw_recv_entry(union recv_frame *precvframe)
4414 {
4415 	_adapter *padapter;
4416 	struct recv_priv *precvpriv;
4417 	s32 ret = _SUCCESS;
4418 
4419 
4420 
4421 	padapter = precvframe->u.hdr.adapter;
4422 
4423 	precvpriv = &padapter->recvpriv;
4424 
4425 
4426 	ret = recv_func(padapter, precvframe);
4427 	if (ret == _FAIL) {
4428 		goto _recv_entry_drop;
4429 	}
4430 
4431 
4432 	precvpriv->rx_pkts++;
4433 
4434 
4435 	return ret;
4436 
4437 _recv_entry_drop:
4438 
4439 #ifdef CONFIG_MP_INCLUDED
4440 	if (padapter->registrypriv.mp_mode == 1)
4441 		padapter->mppriv.rx_pktloss = precvpriv->rx_drop;
4442 #endif
4443 
4444 
4445 
4446 	return ret;
4447 }
4448 
4449 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
rtw_signal_stat_timer_hdl(void * ctx)4450 static void rtw_signal_stat_timer_hdl(void *ctx)
4451 {
4452 	_adapter *adapter = (_adapter *)ctx;
4453 	struct recv_priv *recvpriv = &adapter->recvpriv;
4454 
4455 	u32 tmp_s, tmp_q;
4456 	u8 avg_signal_strength = 0;
4457 	u8 avg_signal_qual = 0;
4458 	u32 num_signal_strength = 0;
4459 	u32 num_signal_qual = 0;
4460 	u8 ratio_pre_stat = 0, ratio_curr_stat = 0, ratio_total = 0, ratio_profile = SIGNAL_STAT_CALC_PROFILE_0;
4461 
4462 	if (adapter->recvpriv.is_signal_dbg) {
4463 		/* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
4464 		adapter->recvpriv.signal_strength = adapter->recvpriv.signal_strength_dbg;
4465 		adapter->recvpriv.rssi = (s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
4466 	} else {
4467 
4468 		if (recvpriv->signal_strength_data.update_req == 0) { /* update_req is clear, means we got rx */
4469 			avg_signal_strength = recvpriv->signal_strength_data.avg_val;
4470 			num_signal_strength = recvpriv->signal_strength_data.total_num;
4471 			/* after avg_vals are accquired, we can re-stat the signal values */
4472 			recvpriv->signal_strength_data.update_req = 1;
4473 		}
4474 
4475 		if (recvpriv->signal_qual_data.update_req == 0) { /* update_req is clear, means we got rx */
4476 			avg_signal_qual = recvpriv->signal_qual_data.avg_val;
4477 			num_signal_qual = recvpriv->signal_qual_data.total_num;
4478 			/* after avg_vals are accquired, we can re-stat the signal values */
4479 			recvpriv->signal_qual_data.update_req = 1;
4480 		}
4481 
4482 		if (num_signal_strength == 0) {
4483 			if (rtw_get_on_cur_ch_time(adapter) == 0
4484 			    || rtw_get_passing_time_ms(rtw_get_on_cur_ch_time(adapter)) < 2 * adapter->mlmeextpriv.mlmext_info.bcn_interval
4485 			   )
4486 				goto set_timer;
4487 		}
4488 
4489 		if (check_fwstate(&adapter->mlmepriv, WIFI_UNDER_SURVEY) == _TRUE
4490 		    || check_fwstate(&adapter->mlmepriv, WIFI_ASOC_STATE) == _FALSE
4491 		   )
4492 			goto set_timer;
4493 
4494 #ifdef CONFIG_CONCURRENT_MODE
4495 		if (rtw_mi_buddy_check_fwstate(adapter, WIFI_UNDER_SURVEY) == _TRUE)
4496 			goto set_timer;
4497 #endif
4498 		if (adapter->registrypriv.mp_mode == 1)
4499 			ratio_profile = SIGNAL_STAT_CALC_PROFILE_2;
4500 		else if (RTW_SIGNAL_STATE_CALC_PROFILE < SIGNAL_STAT_CALC_PROFILE_MAX)
4501 			ratio_profile = RTW_SIGNAL_STATE_CALC_PROFILE;
4502 
4503 		ratio_pre_stat = signal_stat_calc_profile[ratio_profile][0];
4504 		ratio_curr_stat = signal_stat_calc_profile[ratio_profile][1];
4505 		ratio_total = ratio_pre_stat + ratio_curr_stat;
4506 
4507 		/* update value of signal_strength, rssi, signal_qual */
4508 		tmp_s = (ratio_curr_stat * avg_signal_strength + ratio_pre_stat * recvpriv->signal_strength);
4509 		if (tmp_s % ratio_total)
4510 			tmp_s = tmp_s / ratio_total + 1;
4511 		else
4512 			tmp_s = tmp_s / ratio_total;
4513 		if (tmp_s > 100)
4514 			tmp_s = 100;
4515 
4516 		tmp_q = (ratio_curr_stat * avg_signal_qual + ratio_pre_stat * recvpriv->signal_qual);
4517 		if (tmp_q % ratio_total)
4518 			tmp_q = tmp_q / ratio_total + 1;
4519 		else
4520 			tmp_q = tmp_q / ratio_total;
4521 		if (tmp_q > 100)
4522 			tmp_q = 100;
4523 
4524 		recvpriv->signal_strength = tmp_s;
4525 		recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
4526 		recvpriv->signal_qual = tmp_q;
4527 
4528 #if defined(DBG_RX_SIGNAL_DISPLAY_PROCESSING) && 1
4529 		RTW_INFO(FUNC_ADPT_FMT" signal_strength:%3u, rssi:%3d, signal_qual:%3u"
4530 			 ", num_signal_strength:%u, num_signal_qual:%u"
4531 			 ", on_cur_ch_ms:%d"
4532 			 "\n"
4533 			 , FUNC_ADPT_ARG(adapter)
4534 			 , recvpriv->signal_strength
4535 			 , recvpriv->rssi
4536 			 , recvpriv->signal_qual
4537 			 , num_signal_strength, num_signal_qual
4538 			, rtw_get_on_cur_ch_time(adapter) ? rtw_get_passing_time_ms(rtw_get_on_cur_ch_time(adapter)) : 0
4539 			);
4540 #endif
4541 	}
4542 
4543 set_timer:
4544 	rtw_set_signal_stat_timer(recvpriv);
4545 
4546 }
4547 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4548 
rx_process_rssi(_adapter * padapter,union recv_frame * prframe)4549 static void rx_process_rssi(_adapter *padapter, union recv_frame *prframe)
4550 {
4551 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
4552 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
4553 	struct signal_stat *signal_stat = &padapter->recvpriv.signal_strength_data;
4554 #else /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4555 	u32 last_rssi, tmp_val;
4556 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4557 
4558 	/* RTW_INFO("process_rssi=> pattrib->rssil(%d) signal_strength(%d)\n ",pattrib->recv_signal_power,pattrib->signal_strength); */
4559 	/* if(pRfd->Status.bPacketToSelf || pRfd->Status.bPacketBeacon) */
4560 	{
4561 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
4562 		if (signal_stat->update_req) {
4563 			signal_stat->total_num = 0;
4564 			signal_stat->total_val = 0;
4565 			signal_stat->update_req = 0;
4566 		}
4567 
4568 		signal_stat->total_num++;
4569 		signal_stat->total_val  += pattrib->phy_info.signal_strength;
4570 		signal_stat->avg_val = signal_stat->total_val / signal_stat->total_num;
4571 #else /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4572 
4573 		/* Adapter->RxStats.RssiCalculateCnt++;	 */ /* For antenna Test */
4574 		if (padapter->recvpriv.signal_strength_data.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
4575 			padapter->recvpriv.signal_strength_data.total_num = PHY_RSSI_SLID_WIN_MAX;
4576 			last_rssi = padapter->recvpriv.signal_strength_data.elements[padapter->recvpriv.signal_strength_data.index];
4577 			padapter->recvpriv.signal_strength_data.total_val -= last_rssi;
4578 		}
4579 		padapter->recvpriv.signal_strength_data.total_val  += pattrib->phy_info.signal_strength;
4580 
4581 		padapter->recvpriv.signal_strength_data.elements[padapter->recvpriv.signal_strength_data.index++] = pattrib->phy_info.signal_strength;
4582 		if (padapter->recvpriv.signal_strength_data.index >= PHY_RSSI_SLID_WIN_MAX)
4583 			padapter->recvpriv.signal_strength_data.index = 0;
4584 
4585 
4586 		tmp_val = padapter->recvpriv.signal_strength_data.total_val / padapter->recvpriv.signal_strength_data.total_num;
4587 
4588 		if (padapter->recvpriv.is_signal_dbg) {
4589 			padapter->recvpriv.signal_strength = padapter->recvpriv.signal_strength_dbg;
4590 			padapter->recvpriv.rssi = (s8)translate_percentage_to_dbm(padapter->recvpriv.signal_strength_dbg);
4591 		} else {
4592 			padapter->recvpriv.signal_strength = tmp_val;
4593 			padapter->recvpriv.rssi = (s8)translate_percentage_to_dbm(tmp_val);
4594 		}
4595 
4596 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4597 	}
4598 }
4599 
rx_process_link_qual(_adapter * padapter,union recv_frame * prframe)4600 static void rx_process_link_qual(_adapter *padapter, union recv_frame *prframe)
4601 {
4602 	struct rx_pkt_attrib *pattrib;
4603 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
4604 	struct signal_stat *signal_stat;
4605 #else /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4606 	u32 last_evm = 0, tmpVal;
4607 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4608 
4609 	if (prframe == NULL || padapter == NULL)
4610 		return;
4611 
4612 	pattrib = &prframe->u.hdr.attrib;
4613 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
4614 	signal_stat = &padapter->recvpriv.signal_qual_data;
4615 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4616 
4617 	/* RTW_INFO("process_link_qual=> pattrib->signal_qual(%d)\n ",pattrib->signal_qual); */
4618 
4619 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
4620 	if (signal_stat->update_req) {
4621 		signal_stat->total_num = 0;
4622 		signal_stat->total_val = 0;
4623 		signal_stat->update_req = 0;
4624 	}
4625 
4626 	signal_stat->total_num++;
4627 	signal_stat->total_val  += pattrib->phy_info.signal_quality;
4628 	signal_stat->avg_val = signal_stat->total_val / signal_stat->total_num;
4629 
4630 #else /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4631 	if (pattrib->phy_info.signal_quality != 0) {
4632 		/*  */
4633 		/* 1. Record the general EVM to the sliding window. */
4634 		/*  */
4635 		if (padapter->recvpriv.signal_qual_data.total_num++ >= PHY_LINKQUALITY_SLID_WIN_MAX) {
4636 			padapter->recvpriv.signal_qual_data.total_num = PHY_LINKQUALITY_SLID_WIN_MAX;
4637 			last_evm = padapter->recvpriv.signal_qual_data.elements[padapter->recvpriv.signal_qual_data.index];
4638 			padapter->recvpriv.signal_qual_data.total_val -= last_evm;
4639 		}
4640 		padapter->recvpriv.signal_qual_data.total_val += pattrib->phy_info.signal_quality;
4641 
4642 		padapter->recvpriv.signal_qual_data.elements[padapter->recvpriv.signal_qual_data.index++] = pattrib->phy_info.signal_quality;
4643 		if (padapter->recvpriv.signal_qual_data.index >= PHY_LINKQUALITY_SLID_WIN_MAX)
4644 			padapter->recvpriv.signal_qual_data.index = 0;
4645 
4646 
4647 		/* <1> Showed on UI for user, in percentage. */
4648 		tmpVal = padapter->recvpriv.signal_qual_data.total_val / padapter->recvpriv.signal_qual_data.total_num;
4649 		padapter->recvpriv.signal_qual = (u8)tmpVal;
4650 
4651 	}
4652 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
4653 }
4654 
rx_process_phy_info(_adapter * padapter,union recv_frame * rframe)4655 void rx_process_phy_info(_adapter *padapter, union recv_frame *rframe)
4656 {
4657 	/* Check RSSI */
4658 	rx_process_rssi(padapter, rframe);
4659 
4660 	/* Check PWDB */
4661 	/* process_PWDB(padapter, rframe); */
4662 
4663 	/* UpdateRxSignalStatistics8192C(Adapter, pRfd); */
4664 
4665 	/* Check EVM */
4666 	rx_process_link_qual(padapter, rframe);
4667 	rtw_store_phy_info(padapter, rframe);
4668 }
4669 
rx_query_phy_status(union recv_frame * precvframe,u8 * pphy_status)4670 void rx_query_phy_status(
4671 	union recv_frame	*precvframe,
4672 	u8 *pphy_status)
4673 {
4674 	PADAPTER			padapter = precvframe->u.hdr.adapter;
4675 	struct rx_pkt_attrib	*pattrib = &precvframe->u.hdr.attrib;
4676 	HAL_DATA_TYPE		*pHalData = GET_HAL_DATA(padapter);
4677 	struct phydm_phyinfo_struct *p_phy_info = &pattrib->phy_info;
4678 	u8					*wlanhdr;
4679 	struct phydm_perpkt_info_struct pkt_info;
4680 	u8 *ta, *ra;
4681 	u8 is_ra_bmc;
4682 	struct sta_priv *pstapriv;
4683 	struct sta_info *psta = NULL;
4684 	struct recv_priv  *precvpriv = &padapter->recvpriv;
4685 	/* _irqL		irqL; */
4686 
4687 	pkt_info.is_packet_match_bssid = _FALSE;
4688 	pkt_info.is_packet_to_self = _FALSE;
4689 	pkt_info.is_packet_beacon = _FALSE;
4690 	pkt_info.ppdu_cnt = pattrib->ppdu_cnt;
4691 	pkt_info.station_id = 0xFF;
4692 
4693 	wlanhdr = get_recvframe_data(precvframe);
4694 
4695 	ta = get_ta(wlanhdr);
4696 	ra = get_ra(wlanhdr);
4697 	is_ra_bmc = IS_MCAST(ra);
4698 
4699 	if (_rtw_memcmp(adapter_mac_addr(padapter), ta, ETH_ALEN) == _TRUE) {
4700 		static systime start_time = 0;
4701 
4702 #if 0 /*For debug */
4703 		if (IsFrameTypeCtrl(wlanhdr)) {
4704 			RTW_INFO("-->Control frame: Y\n");
4705 			RTW_INFO("-->pkt_len: %d\n", pattrib->pkt_len);
4706 			RTW_INFO("-->Sub Type = 0x%X\n", get_frame_sub_type(wlanhdr));
4707 		}
4708 
4709 		/* Dump first 40 bytes of header */
4710 		int i = 0;
4711 
4712 		for (i = 0; i < 40; i++)
4713 			RTW_INFO("%d: %X\n", i, *((u8 *)wlanhdr + i));
4714 
4715 		RTW_INFO("\n");
4716 #endif
4717 
4718 		if ((start_time == 0) || (rtw_get_passing_time_ms(start_time) > 5000)) {
4719 			RTW_PRINT("Warning!!! %s: Confilc mac addr!!\n", __func__);
4720 			start_time = rtw_get_current_time();
4721 		}
4722 		precvpriv->dbg_rx_conflic_mac_addr_cnt++;
4723 	} else {
4724 		pstapriv = &padapter->stapriv;
4725 		psta = rtw_get_stainfo(pstapriv, ta);
4726 		if (psta)
4727 			pkt_info.station_id = psta->cmn.mac_id;
4728 	}
4729 
4730 	pkt_info.is_packet_match_bssid = (!IsFrameTypeCtrl(wlanhdr))
4731 		&& (!pattrib->icv_err) && (!pattrib->crc_err)
4732 		&& ((!MLME_IS_MESH(padapter) && _rtw_memcmp(get_hdr_bssid(wlanhdr), get_bssid(&padapter->mlmepriv), ETH_ALEN))
4733 			|| (MLME_IS_MESH(padapter) && psta));
4734 
4735 	pkt_info.is_to_self = (!pattrib->icv_err) && (!pattrib->crc_err)
4736 		&& _rtw_memcmp(ra, adapter_mac_addr(padapter), ETH_ALEN);
4737 
4738 	pkt_info.is_packet_to_self = pkt_info.is_packet_match_bssid
4739 		&& _rtw_memcmp(ra, adapter_mac_addr(padapter), ETH_ALEN);
4740 
4741 	pkt_info.is_packet_beacon = pkt_info.is_packet_match_bssid
4742 				 && (get_frame_sub_type(wlanhdr) == WIFI_BEACON);
4743 
4744 	if (psta && IsFrameTypeData(wlanhdr)
4745 		&& !(get_frame_sub_type(wlanhdr) & BIT(6)) /* don't count NULL data */
4746 	) {
4747 		if (is_ra_bmc)
4748 			psta->curr_rx_rate_bmc = pattrib->data_rate;
4749 		else
4750 			psta->curr_rx_rate = pattrib->data_rate;
4751 	}
4752 	pkt_info.data_rate = pattrib->data_rate;
4753 
4754 	odm_phy_status_query(&pHalData->odmpriv, p_phy_info, pphy_status, &pkt_info);
4755 
4756 	/* If bw is initial value, get from phy status */
4757 	if (pattrib->bw == CHANNEL_WIDTH_MAX)
4758 		pattrib->bw = p_phy_info->band_width;
4759 
4760 	if (p_phy_info->physts_rpt_valid == _TRUE) {
4761 		precvframe->u.hdr.psta = NULL;
4762 		if (padapter->registrypriv.mp_mode != 1) {
4763 			if ((!MLME_IS_MESH(padapter) && pkt_info.is_packet_match_bssid)
4764 				|| (MLME_IS_MESH(padapter) && psta)) {
4765 				if (psta) {
4766 					precvframe->u.hdr.psta = psta;
4767 					rx_process_phy_info(padapter, precvframe);
4768 				}
4769 			} else if (pkt_info.is_packet_to_self || pkt_info.is_packet_beacon) {
4770 				if (psta)
4771 					precvframe->u.hdr.psta = psta;
4772 				rx_process_phy_info(padapter, precvframe);
4773 			}
4774 		} else {
4775 #ifdef CONFIG_MP_INCLUDED
4776 			if (padapter->mppriv.brx_filter_beacon == _TRUE) {
4777 				if (pkt_info.is_packet_beacon) {
4778 					RTW_INFO("in MP Rx is_packet_beacon\n");
4779 					if (psta)
4780 						precvframe->u.hdr.psta = psta;
4781 					rx_process_phy_info(padapter, precvframe);
4782 				}
4783 			} else
4784 #endif
4785 			{
4786 					if (psta)
4787 						precvframe->u.hdr.psta = psta;
4788 					rx_process_phy_info(padapter, precvframe);
4789 			}
4790 		}
4791 	}
4792 
4793 	rtw_odm_parse_rx_phy_status_chinfo(precvframe, pphy_status);
4794 }
4795 /*
4796 * Increase and check if the continual_no_rx_packet of this @param pmlmepriv is larger than MAX_CONTINUAL_NORXPACKET_COUNT
4797 * @return _TRUE:
4798 * @return _FALSE:
4799 */
rtw_inc_and_chk_continual_no_rx_packet(struct sta_info * sta,int tid_index)4800 int rtw_inc_and_chk_continual_no_rx_packet(struct sta_info *sta, int tid_index)
4801 {
4802 
4803 	int ret = _FALSE;
4804 	int value = ATOMIC_INC_RETURN(&sta->continual_no_rx_packet[tid_index]);
4805 
4806 	if (value >= MAX_CONTINUAL_NORXPACKET_COUNT)
4807 		ret = _TRUE;
4808 
4809 	return ret;
4810 }
4811 
4812 /*
4813 * Set the continual_no_rx_packet of this @param pmlmepriv to 0
4814 */
rtw_reset_continual_no_rx_packet(struct sta_info * sta,int tid_index)4815 void rtw_reset_continual_no_rx_packet(struct sta_info *sta, int tid_index)
4816 {
4817 	ATOMIC_SET(&sta->continual_no_rx_packet[tid_index], 0);
4818 }
4819 
adapter_allow_bmc_data_rx(_adapter * adapter)4820 u8 adapter_allow_bmc_data_rx(_adapter *adapter)
4821 {
4822 	if (check_fwstate(&adapter->mlmepriv, WIFI_MONITOR_STATE | WIFI_MP_STATE) == _TRUE)
4823 		return 1;
4824 
4825 #ifdef RTW_SIMPLE_CONFIG
4826 	/* allow AP to receive multicast packet for RtwSimpleConfigV4 */
4827 	if (MLME_IS_AP(adapter) && adapter->rtw_simple_config)
4828 		return 1;
4829 #endif
4830 
4831 	if (MLME_IS_AP(adapter))
4832 		return 0;
4833 
4834 	if (rtw_linked_check(adapter) == _FALSE)
4835 		return 0;
4836 
4837 	return 1;
4838 }
4839 
pre_recv_entry(union recv_frame * precvframe,u8 * pphy_status)4840 s32 pre_recv_entry(union recv_frame *precvframe, u8 *pphy_status)
4841 {
4842 	s32 ret = _SUCCESS;
4843 	u8 *pbuf = precvframe->u.hdr.rx_data;
4844 	u8 *ra = get_ra(pbuf);
4845 	u8 ra_is_bmc = IS_MCAST(ra);
4846 	bool phy_queried = 0;
4847 	_adapter *primary_padapter = precvframe->u.hdr.adapter;
4848 	_adapter *iface = NULL;
4849 
4850 #ifdef CONFIG_MP_INCLUDED
4851 	if (rtw_mp_mode_check(primary_padapter))
4852 		goto query_phy_status;
4853 #endif
4854 #ifdef CONFIG_WIFI_MONITOR
4855 	if (MLME_IS_MONITOR(primary_padapter))
4856 		goto query_phy_status;
4857 #endif
4858 
4859 	if (ra_is_bmc == _FALSE) {
4860 		/* UC frame */
4861 		iface = rtw_get_iface_by_macddr(primary_padapter , ra);
4862 		if (!iface) {
4863 			#if defined(CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI) || defined(CONFIG_RTW_SCAN_RAND)
4864 			if (_rtw_memcmp(ra, adapter_pno_mac_addr(primary_padapter), ETH_ALEN))
4865 				goto query_phy_status;
4866 			#endif
4867 
4868 			#ifdef CONFIG_RTW_MULTI_AP
4869 			/* unasoc STA RCPI */
4870 			if (rtw_unassoc_sta_src_chk(primary_padapter, UNASOC_STA_SRC_RX_NMY_UC)) {
4871 				if (pphy_status) {
4872 					rx_query_phy_status(precvframe, pphy_status);
4873 					rtw_rx_add_unassoc_sta(primary_padapter, UNASOC_STA_SRC_RX_NMY_UC, get_ta(pbuf)
4874 						, precvframe->u.hdr.attrib.phy_info.recv_signal_power);
4875 				}
4876 			} else
4877 			#endif
4878 				RTW_INFO("%s [WARN] Cannot find appropriate adapter - mac_addr : "MAC_FMT"\n"
4879 					, __func__, MAC_ARG(ra));
4880 
4881 			rtw_free_recvframe(precvframe, &precvframe->u.hdr.adapter->recvpriv.free_recv_queue);
4882 			goto exit;
4883 		}
4884 		#ifdef CONFIG_CONCURRENT_MODE
4885 		else
4886 			precvframe->u.hdr.adapter = iface;
4887 		#endif
4888 
4889 	} else {
4890 		/* BMC frame */
4891 		#ifdef CONFIG_CONCURRENT_MODE
4892 		rtw_mi_buddy_clone_bcmc_packet(primary_padapter, precvframe, pphy_status);
4893 		#endif
4894 
4895 		#ifdef CONFIG_RTW_MULTI_AP
4896 		/* unasoc STA RCPI */
4897 		if (pphy_status
4898 			&& rtw_unassoc_sta_src_chk(primary_padapter, UNASOC_STA_SRC_RX_BMC)
4899 		) {
4900 			phy_queried = 1;
4901 			rx_query_phy_status(precvframe, pphy_status);
4902 			rtw_rx_add_unassoc_sta(primary_padapter, UNASOC_STA_SRC_RX_BMC, get_ta(pbuf)
4903 				, precvframe->u.hdr.attrib.phy_info.recv_signal_power);
4904 		}
4905 		#endif
4906 
4907 		/* skip unnecessary BMC data frame for primary adapter */
4908 		if (GetFrameType(pbuf) == WIFI_DATA_TYPE
4909 			&& !adapter_allow_bmc_data_rx(precvframe->u.hdr.adapter)
4910 		) {
4911 			rtw_free_recvframe(precvframe, &precvframe->u.hdr.adapter->recvpriv.free_recv_queue);
4912 			goto exit;
4913 		}
4914 	}
4915 #if defined(CONFIG_MP_INCLUDED) || defined(CONFIG_WIFI_MONITOR) || defined(CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI) || defined(CONFIG_RTW_SCAN_RAND)
4916 query_phy_status:
4917 #endif
4918 	if (pphy_status) {
4919 		if (!phy_queried)
4920 			rx_query_phy_status(precvframe, pphy_status);
4921 		#ifdef CONFIG_WIFI_MONITOR
4922 		if (MLME_IS_MONITOR(primary_padapter))
4923 			rx_query_moinfo(&precvframe->u.hdr.attrib, pphy_status);
4924 		#endif
4925 	}
4926 
4927 	ret = rtw_recv_entry(precvframe);
4928 
4929 exit:
4930 	return ret;
4931 }
4932 
4933 #ifdef CONFIG_RECV_THREAD_MODE
rtw_recv_thread(thread_context context)4934 thread_return rtw_recv_thread(thread_context context)
4935 {
4936 	_adapter *adapter = (_adapter *)context;
4937 	struct recv_priv *recvpriv = &adapter->recvpriv;
4938 	s32 err = _SUCCESS;
4939 #ifdef RTW_RECV_THREAD_HIGH_PRIORITY
4940 #ifdef PLATFORM_LINUX
4941 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 9, 0))
4942 	sched_set_fifo_low(current);
4943 #else
4944 	struct sched_param param = { .sched_priority = 1 };
4945 
4946 	sched_setscheduler(current, SCHED_FIFO, &param);
4947 #endif
4948 #endif /* PLATFORM_LINUX */
4949 #endif /*RTW_RECV_THREAD_HIGH_PRIORITY*/
4950 	thread_enter("RTW_RECV_THREAD");
4951 
4952 	RTW_INFO(FUNC_ADPT_FMT" enter\n", FUNC_ADPT_ARG(adapter));
4953 
4954 	do {
4955 		err = _rtw_down_sema(&recvpriv->recv_sema);
4956 		if (_FAIL == err) {
4957 			RTW_ERR(FUNC_ADPT_FMT" down recv_sema fail!\n", FUNC_ADPT_ARG(adapter));
4958 			goto exit;
4959 		}
4960 
4961 		if (RTW_CANNOT_RUN(adapter)) {
4962 			RTW_DBG(FUNC_ADPT_FMT "- bDriverStopped(%s) bSurpriseRemoved(%s)\n",
4963 				FUNC_ADPT_ARG(adapter),
4964 				rtw_is_drv_stopped(adapter) ? "True" : "False",
4965 				rtw_is_surprise_removed(adapter) ? "True" : "False");
4966 			goto exit;
4967 		}
4968 
4969 		err = rtw_hal_recv_hdl(adapter);
4970 
4971 		if (err == RTW_RFRAME_UNAVAIL
4972 			|| err == RTW_RFRAME_PKT_UNAVAIL
4973 		) {
4974 			rtw_msleep_os(1);
4975 			_rtw_up_sema(&recvpriv->recv_sema);
4976 		}
4977 
4978 		flush_signals_thread();
4979 
4980 	} while (err != _FAIL);
4981 
4982 exit:
4983 
4984 	RTW_INFO(FUNC_ADPT_FMT " Exit\n", FUNC_ADPT_ARG(adapter));
4985 
4986 	rtw_thread_wait_stop();
4987 
4988 	return 0;
4989 }
4990 #endif /* CONFIG_RECV_THREAD_MODE */
4991 
4992 #if DBG_RX_BH_TRACKING
rx_bh_tk_set_stage(struct recv_priv * recv,u32 s)4993 void rx_bh_tk_set_stage(struct recv_priv *recv, u32 s)
4994 {
4995 	recv->rx_bh_stage = s;
4996 }
4997 
rx_bh_tk_set_buf(struct recv_priv * recv,void * buf,void * data,u32 dlen)4998 void rx_bh_tk_set_buf(struct recv_priv *recv, void *buf, void *data, u32 dlen)
4999 {
5000 	if (recv->rx_bh_cbuf)
5001 		recv->rx_bh_lbuf = recv->rx_bh_cbuf;
5002 	recv->rx_bh_cbuf = buf;
5003 	if (buf) {
5004 		recv->rx_bh_cbuf_data = data;
5005 		recv->rx_bh_cbuf_dlen = dlen;
5006 		recv->rx_bh_buf_dq_cnt++;
5007 	} else {
5008 		recv->rx_bh_cbuf_data = NULL;
5009 		recv->rx_bh_cbuf_dlen = 0;
5010 	}
5011 }
5012 
rx_bh_tk_set_buf_pos(struct recv_priv * recv,void * pos)5013 void rx_bh_tk_set_buf_pos(struct recv_priv *recv, void *pos)
5014 {
5015 	if (recv->rx_bh_cbuf) {
5016 		recv->rx_bh_cbuf_pos = pos - recv->rx_bh_cbuf_data;
5017 	} else {
5018 		rtw_warn_on(1);
5019 		recv->rx_bh_cbuf_pos = 0;
5020 	}
5021 }
5022 
rx_bh_tk_set_frame(struct recv_priv * recv,void * frame)5023 void rx_bh_tk_set_frame(struct recv_priv *recv, void *frame)
5024 {
5025 	recv->rx_bh_cframe = frame;
5026 }
5027 
dump_rx_bh_tk(void * sel,struct recv_priv * recv)5028 void dump_rx_bh_tk(void *sel, struct recv_priv *recv)
5029 {
5030 	RTW_PRINT_SEL(sel, "[RXBHTK]s:%u, buf_dqc:%u, lbuf:%p, cbuf:%p, dlen:%u, pos:%u, cframe:%p\n"
5031 		, recv->rx_bh_stage
5032 		, recv->rx_bh_buf_dq_cnt
5033 		, recv->rx_bh_lbuf
5034 		, recv->rx_bh_cbuf
5035 		, recv->rx_bh_cbuf_dlen
5036 		, recv->rx_bh_cbuf_pos
5037 		, recv->rx_bh_cframe
5038 	);
5039 }
5040 #endif /* DBG_RX_BH_TRACKING */
5041 
5042