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