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