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