1 /******************************************************************************
2  * rtl871x_recv.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Modifications for inclusion into the Linux staging tree are
21  * Copyright(c) 2010 Larry Finger. All rights reserved.
22  *
23  * Contact information:
24  * WLAN FAE <wlanfae@realtek.com>
25  * Larry Finger <Larry.Finger@lwfinger.net>
26  *
27  ******************************************************************************/
28 
29 #define _RTL871X_RECV_C_
30 
31 #include <linux/slab.h>
32 #include <linux/kmemleak.h>
33 
34 #include "osdep_service.h"
35 #include "drv_types.h"
36 #include "recv_osdep.h"
37 #include "mlme_osdep.h"
38 #include "ip.h"
39 #include "if_ether.h"
40 #include "ethernet.h"
41 #include "usb_ops.h"
42 #include "wifi.h"
43 
44 static const u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
45 
46 /* Datagram Delivery Protocol */
47 static const u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
48 
49 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
50 static const u8 bridge_tunnel_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
51 
52 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
53 static const u8 rfc1042_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
54 
_r8712_init_sta_recv_priv(struct sta_recv_priv * psta_recvpriv)55 void _r8712_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
56 {
57 	memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
58 	spin_lock_init(&psta_recvpriv->lock);
59 	_init_queue(&psta_recvpriv->defrag_q);
60 }
61 
_r8712_init_recv_priv(struct recv_priv * precvpriv,struct _adapter * padapter)62 sint _r8712_init_recv_priv(struct recv_priv *precvpriv,
63 			   struct _adapter *padapter)
64 {
65 	sint i;
66 	union recv_frame *precvframe;
67 
68 	 memset((unsigned char *)precvpriv, 0, sizeof(struct  recv_priv));
69 	spin_lock_init(&precvpriv->lock);
70 	_init_queue(&precvpriv->free_recv_queue);
71 	_init_queue(&precvpriv->recv_pending_queue);
72 	precvpriv->adapter = padapter;
73 	precvpriv->free_recvframe_cnt = NR_RECVFRAME;
74 	precvpriv->pallocated_frame_buf = _malloc(NR_RECVFRAME *
75 					   sizeof(union recv_frame) +
76 					   RXFRAME_ALIGN_SZ);
77 	if (precvpriv->pallocated_frame_buf == NULL)
78 		return _FAIL;
79 	kmemleak_not_leak(precvpriv->pallocated_frame_buf);
80 	memset(precvpriv->pallocated_frame_buf, 0, NR_RECVFRAME *
81 		sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
82 	precvpriv->precv_frame_buf = precvpriv->pallocated_frame_buf +
83 				    RXFRAME_ALIGN_SZ -
84 				    ((addr_t)(precvpriv->pallocated_frame_buf) &
85 				    (RXFRAME_ALIGN_SZ-1));
86 	precvframe = (union recv_frame *)precvpriv->precv_frame_buf;
87 	for (i = 0; i < NR_RECVFRAME; i++) {
88 		_init_listhead(&(precvframe->u.list));
89 		list_insert_tail(&(precvframe->u.list),
90 				 &(precvpriv->free_recv_queue.queue));
91 		r8712_os_recv_resource_alloc(padapter, precvframe);
92 		precvframe->u.hdr.adapter = padapter;
93 		precvframe++;
94 	}
95 	precvpriv->rx_pending_cnt = 1;
96 	return r8712_init_recv_priv(precvpriv, padapter);
97 }
98 
_r8712_free_recv_priv(struct recv_priv * precvpriv)99 void _r8712_free_recv_priv(struct recv_priv *precvpriv)
100 {
101 	kfree(precvpriv->pallocated_frame_buf);
102 	r8712_free_recv_priv(precvpriv);
103 }
104 
r8712_alloc_recvframe(struct __queue * pfree_recv_queue)105 union recv_frame *r8712_alloc_recvframe(struct  __queue *pfree_recv_queue)
106 {
107 	unsigned long irqL;
108 	union recv_frame  *precvframe;
109 	struct list_head *plist, *phead;
110 	struct _adapter *padapter;
111 	struct recv_priv *precvpriv;
112 
113 	spin_lock_irqsave(&pfree_recv_queue->lock, irqL);
114 	if (_queue_empty(pfree_recv_queue) == true)
115 		precvframe = NULL;
116 	else {
117 		phead = get_list_head(pfree_recv_queue);
118 		plist = get_next(phead);
119 		precvframe = LIST_CONTAINOR(plist, union recv_frame, u);
120 		list_delete(&precvframe->u.hdr.list);
121 		padapter = precvframe->u.hdr.adapter;
122 		if (padapter != NULL) {
123 			precvpriv = &padapter->recvpriv;
124 			if (pfree_recv_queue == &precvpriv->free_recv_queue)
125 				precvpriv->free_recvframe_cnt--;
126 		}
127 	}
128 	spin_unlock_irqrestore(&pfree_recv_queue->lock, irqL);
129 	return precvframe;
130 }
131 
132 /*
133 caller : defrag; recvframe_chk_defrag in recv_thread  (passive)
134 pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
135 
136 using spin_lock to protect
137 
138 */
139 
r8712_free_recvframe_queue(struct __queue * pframequeue,struct __queue * pfree_recv_queue)140 void r8712_free_recvframe_queue(struct  __queue *pframequeue,
141 				struct  __queue *pfree_recv_queue)
142 {
143 	union	recv_frame *precvframe;
144 	struct list_head *plist, *phead;
145 
146 	spin_lock(&pframequeue->lock);
147 	phead = get_list_head(pframequeue);
148 	plist = get_next(phead);
149 	while (end_of_queue_search(phead, plist) == false) {
150 		precvframe = LIST_CONTAINOR(plist, union recv_frame, u);
151 		plist = get_next(plist);
152 		r8712_free_recvframe(precvframe, pfree_recv_queue);
153 	}
154 	spin_unlock(&pframequeue->lock);
155 }
156 
r8712_recvframe_chkmic(struct _adapter * adapter,union recv_frame * precvframe)157 sint r8712_recvframe_chkmic(struct _adapter *adapter,
158 			    union recv_frame *precvframe)
159 {
160 	sint i, res = _SUCCESS;
161 	u32	datalen;
162 	u8 miccode[8];
163 	u8 bmic_err = false;
164 	u8 *pframe, *payload, *pframemic;
165 	u8   *mickey, idx, *iv;
166 	struct	sta_info *stainfo;
167 	struct	rx_pkt_attrib *prxattrib = &precvframe->u.hdr.attrib;
168 	struct	security_priv *psecuritypriv = &adapter->securitypriv;
169 
170 	stainfo = r8712_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
171 	if (prxattrib->encrypt == _TKIP_) {
172 		/* calculate mic code */
173 		if (stainfo != NULL) {
174 			if (IS_MCAST(prxattrib->ra)) {
175 				iv = precvframe->u.hdr.rx_data +
176 				     prxattrib->hdrlen;
177 				idx = iv[3];
178 				mickey = &psecuritypriv->XGrprxmickey[(((idx >>
179 					 6) & 0x3)) - 1].skey[0];
180 				if (psecuritypriv->binstallGrpkey == false)
181 					return _FAIL;
182 			} else
183 				mickey = &stainfo->tkiprxmickey.skey[0];
184 			/*icv_len included the mic code*/
185 			datalen = precvframe->u.hdr.len - prxattrib->hdrlen -
186 				  prxattrib->iv_len - prxattrib->icv_len - 8;
187 			pframe = precvframe->u.hdr.rx_data;
188 			payload = pframe + prxattrib->hdrlen +
189 				  prxattrib->iv_len;
190 			seccalctkipmic(mickey, pframe, payload, datalen,
191 				       &miccode[0],
192 				       (unsigned char)prxattrib->priority);
193 			pframemic = payload + datalen;
194 			bmic_err = false;
195 			for (i = 0; i < 8; i++) {
196 				if (miccode[i] != *(pframemic + i))
197 					bmic_err = true;
198 			}
199 			if (bmic_err == true) {
200 				if (prxattrib->bdecrypted == true)
201 					r8712_handle_tkip_mic_err(adapter,
202 						(u8)IS_MCAST(prxattrib->ra));
203 				res = _FAIL;
204 			} else {
205 				/* mic checked ok */
206 				if ((psecuritypriv->bcheck_grpkey ==
207 				     false) && (IS_MCAST(prxattrib->ra) ==
208 				     true))
209 					psecuritypriv->bcheck_grpkey = true;
210 			}
211 			recvframe_pull_tail(precvframe, 8);
212 		}
213 	}
214 	return res;
215 }
216 
217 /* decrypt and set the ivlen,icvlen of the recv_frame */
r8712_decryptor(struct _adapter * padapter,union recv_frame * precv_frame)218 union recv_frame *r8712_decryptor(struct _adapter *padapter,
219 			    union recv_frame *precv_frame)
220 {
221 	struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
222 	struct security_priv *psecuritypriv = &padapter->securitypriv;
223 	union recv_frame *return_packet = precv_frame;
224 
225 	if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) ||
226 	   (psecuritypriv->sw_decrypt == true))) {
227 		psecuritypriv->hw_decrypted = false;
228 		switch (prxattrib->encrypt) {
229 		case _WEP40_:
230 		case _WEP104_:
231 			r8712_wep_decrypt(padapter, (u8 *)precv_frame);
232 			break;
233 		case _TKIP_:
234 			r8712_tkip_decrypt(padapter, (u8 *)precv_frame);
235 			break;
236 		case _AES_:
237 			r8712_aes_decrypt(padapter, (u8 *)precv_frame);
238 			break;
239 		default:
240 				break;
241 		}
242 	} else if (prxattrib->bdecrypted == 1)
243 		psecuritypriv->hw_decrypted = true;
244 	return return_packet;
245 }
246 /*###set the security information in the recv_frame */
r8712_portctrl(struct _adapter * adapter,union recv_frame * precv_frame)247 union recv_frame *r8712_portctrl(struct _adapter *adapter,
248 				 union recv_frame *precv_frame)
249 {
250 	u8 *psta_addr, *ptr;
251 	uint auth_alg;
252 	struct recv_frame_hdr *pfhdr;
253 	struct sta_info *psta;
254 	struct	sta_priv *pstapriv;
255 	union recv_frame *prtnframe;
256 	u16 ether_type;
257 
258 	pstapriv = &adapter->stapriv;
259 	ptr = get_recvframe_data(precv_frame);
260 	pfhdr = &precv_frame->u.hdr;
261 	psta_addr = pfhdr->attrib.ta;
262 	psta = r8712_get_stainfo(pstapriv, psta_addr);
263 	auth_alg = adapter->securitypriv.AuthAlgrthm;
264 	if (auth_alg == 2) {
265 		/* get ether_type */
266 		ptr = ptr + pfhdr->attrib.hdrlen + LLC_HEADER_SIZE;
267 		memcpy(&ether_type, ptr, 2);
268 		ether_type = ntohs((unsigned short)ether_type);
269 
270 		if ((psta != NULL) && (psta->ieee8021x_blocked)) {
271 			/* blocked
272 			 * only accept EAPOL frame */
273 			if (ether_type == 0x888e)
274 				prtnframe = precv_frame;
275 			else {
276 				/*free this frame*/
277 				r8712_free_recvframe(precv_frame,
278 					 &adapter->recvpriv.free_recv_queue);
279 				prtnframe = NULL;
280 			}
281 		} else {
282 			/* allowed
283 			 * check decryption status, and decrypt the
284 			 * frame if needed */
285 			prtnframe = precv_frame;
286 			/* check is the EAPOL frame or not (Rekey) */
287 			if (ether_type == 0x888e) {
288 				/* check Rekey */
289 				prtnframe = precv_frame;
290 			}
291 		}
292 	} else
293 		prtnframe = precv_frame;
294 	return prtnframe;
295 }
296 
recv_decache(union recv_frame * precv_frame,u8 bretry,struct stainfo_rxcache * prxcache)297 static sint recv_decache(union recv_frame *precv_frame, u8 bretry,
298 		  struct stainfo_rxcache *prxcache)
299 {
300 	sint tid = precv_frame->u.hdr.attrib.priority;
301 	u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num&0xffff) << 4) |
302 			(precv_frame->u.hdr.attrib.frag_num & 0xf);
303 
304 	if (tid > 15)
305 		return _FAIL;
306 	if (seq_ctrl == prxcache->tid_rxseq[tid])
307 		return _FAIL;
308 	prxcache->tid_rxseq[tid] = seq_ctrl;
309 	return _SUCCESS;
310 }
311 
sta2sta_data_frame(struct _adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)312 static sint sta2sta_data_frame(struct _adapter *adapter,
313 			       union recv_frame *precv_frame,
314 			       struct sta_info **psta)
315 {
316 	u8 *ptr = precv_frame->u.hdr.rx_data;
317 	sint ret = _SUCCESS;
318 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
319 	struct	sta_priv *pstapriv = &adapter->stapriv;
320 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
321 	u8 *mybssid  = get_bssid(pmlmepriv);
322 	u8 *myhwaddr = myid(&adapter->eeprompriv);
323 	u8 *sta_addr = NULL;
324 	sint bmcast = IS_MCAST(pattrib->dst);
325 
326 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
327 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
328 		/* filter packets that SA is myself or multicast or broadcast */
329 		if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
330 			return _FAIL;
331 		if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
332 			return _FAIL;
333 		if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
334 		    !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
335 		    (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
336 			return _FAIL;
337 		sta_addr = pattrib->src;
338 	} else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
339 		/* For Station mode, sa and bssid should always be BSSID,
340 		 * and DA is my mac-address */
341 		if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN))
342 			return _FAIL;
343 	       sta_addr = pattrib->bssid;
344 	 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
345 		if (bmcast) {
346 			/* For AP mode, if DA == MCAST, then BSSID should
347 			 * be also MCAST */
348 			if (!IS_MCAST(pattrib->bssid))
349 				return _FAIL;
350 		} else { /* not mc-frame */
351 			/* For AP mode, if DA is non-MCAST, then it must be
352 			 *  BSSID, and bssid == BSSID */
353 			if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN))
354 				return _FAIL;
355 			sta_addr = pattrib->src;
356 		}
357 	  } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
358 		memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
359 		memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
360 		memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
361 		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
362 		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
363 		sta_addr = mybssid;
364 	  } else
365 		ret  = _FAIL;
366 	if (bmcast)
367 		*psta = r8712_get_bcmc_stainfo(adapter);
368 	else
369 		*psta = r8712_get_stainfo(pstapriv, sta_addr); /* get ap_info */
370 	if (*psta == NULL) {
371 		if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)
372 			adapter->mppriv.rx_pktloss++;
373 		return _FAIL;
374 	}
375 	return ret;
376 }
377 
ap2sta_data_frame(struct _adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)378 static sint ap2sta_data_frame(struct _adapter *adapter,
379 			      union recv_frame *precv_frame,
380 			      struct sta_info **psta)
381 {
382 	u8 *ptr = precv_frame->u.hdr.rx_data;
383 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
384 	struct	sta_priv *pstapriv = &adapter->stapriv;
385 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
386 	u8 *mybssid  = get_bssid(pmlmepriv);
387 	u8 *myhwaddr = myid(&adapter->eeprompriv);
388 	sint bmcast = IS_MCAST(pattrib->dst);
389 
390 	if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)
391 	     && (check_fwstate(pmlmepriv, _FW_LINKED) == true)) {
392 		/* if NULL-frame, drop packet */
393 		if ((GetFrameSubType(ptr)) == WIFI_DATA_NULL)
394 			return _FAIL;
395 		/* drop QoS-SubType Data, including QoS NULL,
396 		 * excluding QoS-Data */
397 		if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) ==
398 		     WIFI_QOS_DATA_TYPE) {
399 			if (GetFrameSubType(ptr) & (BIT(4) | BIT(5) | BIT(6)))
400 				return _FAIL;
401 		}
402 
403 		/* filter packets that SA is myself or multicast or broadcast */
404 	       if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
405 			return _FAIL;
406 
407 		/* da should be for me */
408 		if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
409 			return _FAIL;
410 		/* check BSSID */
411 		if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
412 		     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
413 		     (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
414 			return _FAIL;
415 		if (bmcast)
416 			*psta = r8712_get_bcmc_stainfo(adapter);
417 		else
418 		       *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
419 		if (*psta == NULL)
420 			return _FAIL;
421 	} else if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) &&
422 		   (check_fwstate(pmlmepriv, _FW_LINKED) == true)) {
423 		memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
424 		memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
425 		memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
426 		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
427 		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
428 		memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
429 		*psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
430 		if (*psta == NULL)
431 			return _FAIL;
432 	} else
433 		return _FAIL;
434 	return _SUCCESS;
435 }
436 
sta2ap_data_frame(struct _adapter * adapter,union recv_frame * precv_frame,struct sta_info ** psta)437 static sint sta2ap_data_frame(struct _adapter *adapter,
438 			      union recv_frame *precv_frame,
439 			      struct sta_info **psta)
440 {
441 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
442 	struct	sta_priv *pstapriv = &adapter->stapriv;
443 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
444 	unsigned char *mybssid  = get_bssid(pmlmepriv);
445 
446 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
447 		/* For AP mode, if DA is non-MCAST, then it must be BSSID,
448 		 * and bssid == BSSID
449 		 * For AP mode, RA=BSSID, TX=STA(SRC_ADDR), A3=DST_ADDR */
450 		if (memcmp(pattrib->bssid, mybssid, ETH_ALEN))
451 			return _FAIL;
452 		*psta = r8712_get_stainfo(pstapriv, pattrib->src);
453 		if (*psta == NULL)
454 			return _FAIL;
455 	}
456 	return _SUCCESS;
457 }
458 
validate_recv_ctrl_frame(struct _adapter * adapter,union recv_frame * precv_frame)459 static sint validate_recv_ctrl_frame(struct _adapter *adapter,
460 			      union recv_frame *precv_frame)
461 {
462 	return _FAIL;
463 }
464 
validate_recv_mgnt_frame(struct _adapter * adapter,union recv_frame * precv_frame)465 static sint validate_recv_mgnt_frame(struct _adapter *adapter,
466 			      union recv_frame *precv_frame)
467 {
468 	return _FAIL;
469 }
470 
471 
validate_recv_data_frame(struct _adapter * adapter,union recv_frame * precv_frame)472 static sint validate_recv_data_frame(struct _adapter *adapter,
473 			      union recv_frame *precv_frame)
474 {
475 	int res;
476 	u8 bretry;
477 	u8 *psa, *pda, *pbssid;
478 	struct sta_info *psta = NULL;
479 	u8 *ptr = precv_frame->u.hdr.rx_data;
480 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
481 	struct security_priv *psecuritypriv = &adapter->securitypriv;
482 
483 	bretry = GetRetry(ptr);
484 	pda = get_da(ptr);
485 	psa = get_sa(ptr);
486 	pbssid = get_hdr_bssid(ptr);
487 	if (pbssid == NULL)
488 		return _FAIL;
489 	memcpy(pattrib->dst, pda, ETH_ALEN);
490 	memcpy(pattrib->src, psa, ETH_ALEN);
491 	memcpy(pattrib->bssid, pbssid, ETH_ALEN);
492 	switch (pattrib->to_fr_ds) {
493 	case 0:
494 		memcpy(pattrib->ra, pda, ETH_ALEN);
495 		memcpy(pattrib->ta, psa, ETH_ALEN);
496 		res = sta2sta_data_frame(adapter, precv_frame, &psta);
497 		break;
498 	case 1:
499 		memcpy(pattrib->ra, pda, ETH_ALEN);
500 		memcpy(pattrib->ta, pbssid, ETH_ALEN);
501 		res = ap2sta_data_frame(adapter, precv_frame, &psta);
502 		break;
503 	case 2:
504 		memcpy(pattrib->ra, pbssid, ETH_ALEN);
505 		memcpy(pattrib->ta, psa, ETH_ALEN);
506 		res = sta2ap_data_frame(adapter, precv_frame, &psta);
507 		break;
508 	case 3:
509 		memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
510 		memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
511 		return _FAIL;
512 	default:
513 		return _FAIL;
514 	}
515 	if (res == _FAIL)
516 		return _FAIL;
517 	if (psta == NULL)
518 		return _FAIL;
519 	else
520 		precv_frame->u.hdr.psta = psta;
521 	pattrib->amsdu = 0;
522 	/* parsing QC field */
523 	if (pattrib->qos == 1) {
524 		pattrib->priority = GetPriority((ptr + 24));
525 		pattrib->ack_policy = GetAckpolicy((ptr + 24));
526 		pattrib->amsdu = GetAMsdu((ptr + 24));
527 		pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
528 	} else {
529 		pattrib->priority = 0;
530 		pattrib->hdrlen = (pattrib->to_fr_ds == 3) ? 30 : 24;
531 	}
532 
533 	if (pattrib->order)/*HT-CTRL 11n*/
534 		pattrib->hdrlen += 4;
535 	precv_frame->u.hdr.preorder_ctrl =
536 			 &psta->recvreorder_ctrl[pattrib->priority];
537 
538 	/* decache, drop duplicate recv packets */
539 	if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) ==
540 	    _FAIL)
541 		return _FAIL;
542 
543 	if (pattrib->privacy) {
544 		GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt,
545 			       IS_MCAST(pattrib->ra));
546 		SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len,
547 			       pattrib->encrypt);
548 	} else {
549 		pattrib->encrypt = 0;
550 		pattrib->iv_len = pattrib->icv_len = 0;
551 	}
552 	return _SUCCESS;
553 }
554 
r8712_validate_recv_frame(struct _adapter * adapter,union recv_frame * precv_frame)555 sint r8712_validate_recv_frame(struct _adapter *adapter,
556 			       union recv_frame *precv_frame)
557 {
558 	/*shall check frame subtype, to / from ds, da, bssid */
559 	/*then call check if rx seq/frag. duplicated.*/
560 
561 	u8 type;
562 	u8 subtype;
563 	sint retval = _SUCCESS;
564 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
565 
566 	u8 *ptr = precv_frame->u.hdr.rx_data;
567 	u8  ver = (unsigned char)(*ptr) & 0x3;
568 
569 	/*add version chk*/
570 	if (ver != 0)
571 		return _FAIL;
572 	type =  GetFrameType(ptr);
573 	subtype = GetFrameSubType(ptr); /*bit(7)~bit(2)*/
574 	pattrib->to_fr_ds = get_tofr_ds(ptr);
575 	pattrib->frag_num = GetFragNum(ptr);
576 	pattrib->seq_num = GetSequence(ptr);
577 	pattrib->pw_save = GetPwrMgt(ptr);
578 	pattrib->mfrag = GetMFrag(ptr);
579 	pattrib->mdata = GetMData(ptr);
580 	pattrib->privacy =  GetPrivacy(ptr);
581 	pattrib->order = GetOrder(ptr);
582 	switch (type) {
583 	case WIFI_MGT_TYPE: /*mgnt*/
584 		retval = validate_recv_mgnt_frame(adapter, precv_frame);
585 		break;
586 	case WIFI_CTRL_TYPE:/*ctrl*/
587 		retval = validate_recv_ctrl_frame(adapter, precv_frame);
588 		break;
589 	case WIFI_DATA_TYPE: /*data*/
590 		pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
591 		retval = validate_recv_data_frame(adapter, precv_frame);
592 		break;
593 	default:
594 		return _FAIL;
595 	}
596 	return retval;
597 }
598 
r8712_wlanhdr_to_ethhdr(union recv_frame * precvframe)599 sint r8712_wlanhdr_to_ethhdr(union recv_frame *precvframe)
600 {
601 	/*remove the wlanhdr and add the eth_hdr*/
602 	sint	rmv_len;
603 	u16	eth_type, len;
604 	u8	bsnaphdr;
605 	u8	*psnap_type;
606 	struct ieee80211_snap_hdr *psnap;
607 
608 	sint ret = _SUCCESS;
609 	struct _adapter	*adapter = precvframe->u.hdr.adapter;
610 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
611 
612 	u8 *ptr = get_recvframe_data(precvframe); /*point to frame_ctrl field*/
613 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
614 
615 	if (pattrib->encrypt)
616 		recvframe_pull_tail(precvframe, pattrib->icv_len);
617 	psnap = (struct ieee80211_snap_hdr *)(ptr + pattrib->hdrlen +
618 		 pattrib->iv_len);
619 	psnap_type = ptr + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
620 	/* convert hdr + possible LLC headers into Ethernet header */
621 	if ((!memcmp(psnap, (void *)rfc1042_header, SNAP_SIZE) &&
622 	    (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_IPX, 2)) &&
623 	    (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
624 	     !memcmp(psnap, (void *)bridge_tunnel_header, SNAP_SIZE)) {
625 		/* remove RFC1042 or Bridge-Tunnel encapsulation and
626 		 * replace EtherType */
627 		bsnaphdr = true;
628 	} else {
629 		/* Leave Ethernet header part of hdr and full payload */
630 		bsnaphdr = false;
631 	}
632 	rmv_len = pattrib->hdrlen + pattrib->iv_len +
633 		  (bsnaphdr ? SNAP_SIZE : 0);
634 	len = precvframe->u.hdr.len - rmv_len;
635 	if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)) {
636 		ptr += rmv_len;
637 		*ptr = 0x87;
638 		*(ptr+1) = 0x12;
639 		eth_type = 0x8712;
640 		/* append rx status for mp test packets */
641 		ptr = recvframe_pull(precvframe, (rmv_len -
642 		      sizeof(struct ethhdr) + 2) - 24);
643 		memcpy(ptr, get_rxmem(precvframe), 24);
644 		ptr += 24;
645 	} else
646 		ptr = recvframe_pull(precvframe, (rmv_len -
647 		      sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
648 
649 	memcpy(ptr, pattrib->dst, ETH_ALEN);
650 	memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
651 	if (!bsnaphdr) {
652 		len = htons(len);
653 		memcpy(ptr + 12, &len, 2);
654 	}
655 	return ret;
656 }
657 
r8712_recv_entry(union recv_frame * precvframe)658 s32 r8712_recv_entry(union recv_frame *precvframe)
659 {
660 	struct _adapter *padapter;
661 	struct recv_priv *precvpriv;
662 	struct	mlme_priv *pmlmepriv;
663 	struct recv_stat *prxstat;
664 	struct dvobj_priv *pdev;
665 	u8 *phead, *pdata, *ptail, *pend;
666 
667 	struct  __queue *pfree_recv_queue, *ppending_recv_queue;
668 	s32 ret = _SUCCESS;
669 	struct intf_hdl *pintfhdl;
670 
671 	padapter = precvframe->u.hdr.adapter;
672 	pintfhdl = &padapter->pio_queue->intf;
673 	pmlmepriv = &padapter->mlmepriv;
674 	precvpriv = &(padapter->recvpriv);
675 	pdev = &padapter->dvobjpriv;
676 	pfree_recv_queue = &(precvpriv->free_recv_queue);
677 	ppending_recv_queue = &(precvpriv->recv_pending_queue);
678 	phead = precvframe->u.hdr.rx_head;
679 	pdata = precvframe->u.hdr.rx_data;
680 	ptail = precvframe->u.hdr.rx_tail;
681 	pend = precvframe->u.hdr.rx_end;
682 	prxstat = (struct recv_stat *)phead;
683 
684 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_RX);
685 
686 	ret = recv_func(padapter, precvframe);
687 	if (ret == _FAIL)
688 		goto _recv_entry_drop;
689 	precvpriv->rx_pkts++;
690 	precvpriv->rx_bytes += (uint)(precvframe->u.hdr.rx_tail -
691 				precvframe->u.hdr.rx_data);
692 	return ret;
693 _recv_entry_drop:
694 	precvpriv->rx_drop++;
695 	padapter->mppriv.rx_pktloss = precvpriv->rx_drop;
696 	return ret;
697 }
698