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