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