Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[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 = LIST_CONTAINOR(plist, union recv_frame, u);
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                         if (ether_type == 0x888e) {
270                                 prtnframe = precv_frame;
271                         } else {
272                                 /*free this frame*/
273                                 r8712_free_recvframe(precv_frame,
274                                          &adapter->recvpriv.free_recv_queue);
275                                 prtnframe = NULL;
276                         }
277                 } else {
278                         /* allowed
279                          * check decryption status, and decrypt the
280                          * frame if needed */
281                         prtnframe = precv_frame;
282                         /* check is the EAPOL frame or not (Rekey) */
283                         if (ether_type == 0x888e) {
284                                 /* check Rekey */
285                                 prtnframe = precv_frame;
286                         }
287                 }
288         } else {
289                 prtnframe = precv_frame;
290         }
291         return prtnframe;
292 }
293
294 static sint recv_decache(union recv_frame *precv_frame, u8 bretry,
295                   struct stainfo_rxcache *prxcache)
296 {
297         sint tid = precv_frame->u.hdr.attrib.priority;
298         u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num & 0xffff) << 4) |
299                         (precv_frame->u.hdr.attrib.frag_num & 0xf);
300
301         if (tid > 15)
302                 return _FAIL;
303         if (seq_ctrl == prxcache->tid_rxseq[tid])
304                 return _FAIL;
305         prxcache->tid_rxseq[tid] = seq_ctrl;
306         return _SUCCESS;
307 }
308
309 static sint sta2sta_data_frame(struct _adapter *adapter,
310                                union recv_frame *precv_frame,
311                                struct sta_info **psta)
312 {
313         u8 *ptr = precv_frame->u.hdr.rx_data;
314         sint ret = _SUCCESS;
315         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
316         struct  sta_priv *pstapriv = &adapter->stapriv;
317         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
318         u8 *mybssid  = get_bssid(pmlmepriv);
319         u8 *myhwaddr = myid(&adapter->eeprompriv);
320         u8 *sta_addr = NULL;
321         sint bmcast = IS_MCAST(pattrib->dst);
322
323         if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
324             check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
325                 /* filter packets that SA is myself or multicast or broadcast */
326                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
327                         return _FAIL;
328                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
329                         return _FAIL;
330                 if (is_zero_ether_addr(pattrib->bssid) ||
331                     is_zero_ether_addr(mybssid) ||
332                     (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
333                         return _FAIL;
334                 sta_addr = pattrib->src;
335         } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
336                 /* For Station mode, sa and bssid should always be BSSID,
337                  * and DA is my mac-address */
338                 if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN))
339                         return _FAIL;
340                sta_addr = pattrib->bssid;
341         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
342                 if (bmcast) {
343                         /* For AP mode, if DA == MCAST, then BSSID should
344                          * be also MCAST */
345                         if (!IS_MCAST(pattrib->bssid))
346                                 return _FAIL;
347                 } else { /* not mc-frame */
348                         /* For AP mode, if DA is non-MCAST, then it must be
349                          *  BSSID, and bssid == BSSID */
350                         if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN))
351                                 return _FAIL;
352                         sta_addr = pattrib->src;
353                 }
354         } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
355                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
356                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
357                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
358                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
359                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
360                 sta_addr = mybssid;
361         } else {
362                 ret  = _FAIL;
363         }
364         if (bmcast)
365                 *psta = r8712_get_bcmc_stainfo(adapter);
366         else
367                 *psta = r8712_get_stainfo(pstapriv, sta_addr); /* get ap_info */
368         if (*psta == NULL) {
369                 if (check_fwstate(pmlmepriv, WIFI_MP_STATE))
370                         adapter->mppriv.rx_pktloss++;
371                 return _FAIL;
372         }
373         return ret;
374 }
375
376 static sint ap2sta_data_frame(struct _adapter *adapter,
377                               union recv_frame *precv_frame,
378                               struct sta_info **psta)
379 {
380         u8 *ptr = precv_frame->u.hdr.rx_data;
381         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
382         struct  sta_priv *pstapriv = &adapter->stapriv;
383         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
384         u8 *mybssid  = get_bssid(pmlmepriv);
385         u8 *myhwaddr = myid(&adapter->eeprompriv);
386         sint bmcast = IS_MCAST(pattrib->dst);
387
388         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
389             check_fwstate(pmlmepriv, _FW_LINKED)) {
390                 /* if NULL-frame, drop packet */
391                 if ((GetFrameSubType(ptr)) == WIFI_DATA_NULL)
392                         return _FAIL;
393                 /* drop QoS-SubType Data, including QoS NULL,
394                  * excluding QoS-Data */
395                 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) ==
396                      WIFI_QOS_DATA_TYPE) {
397                         if (GetFrameSubType(ptr) & (BIT(4) | BIT(5) | BIT(6)))
398                                 return _FAIL;
399                 }
400
401                 /* filter packets that SA is myself or multicast or broadcast */
402                if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
403                         return _FAIL;
404
405                 /* da should be for me */
406                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
407                         return _FAIL;
408                 /* check BSSID */
409                 if (is_zero_ether_addr(pattrib->bssid) ||
410                      is_zero_ether_addr(mybssid) ||
411                      (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
412                         return _FAIL;
413                 if (bmcast)
414                         *psta = r8712_get_bcmc_stainfo(adapter);
415                 else
416                        *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
417                 if (*psta == NULL)
418                         return _FAIL;
419         } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) &&
420                    check_fwstate(pmlmepriv, _FW_LINKED)) {
421                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
422                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
423                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
424                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
425                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
426                 memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
427                 *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
428                 if (*psta == NULL)
429                         return _FAIL;
430         } else {
431                 return _FAIL;
432         }
433         return _SUCCESS;
434 }
435
436 static sint sta2ap_data_frame(struct _adapter *adapter,
437                               union recv_frame *precv_frame,
438                               struct sta_info **psta)
439 {
440         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
441         struct  sta_priv *pstapriv = &adapter->stapriv;
442         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
443         unsigned char *mybssid  = get_bssid(pmlmepriv);
444
445         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
446                 /* For AP mode, if DA is non-MCAST, then it must be BSSID,
447                  * and bssid == BSSID
448                  * For AP mode, RA=BSSID, TX=STA(SRC_ADDR), A3=DST_ADDR */
449                 if (memcmp(pattrib->bssid, mybssid, ETH_ALEN))
450                         return _FAIL;
451                 *psta = r8712_get_stainfo(pstapriv, pattrib->src);
452                 if (*psta == NULL)
453                         return _FAIL;
454         }
455         return _SUCCESS;
456 }
457
458 static sint validate_recv_ctrl_frame(struct _adapter *adapter,
459                               union recv_frame *precv_frame)
460 {
461         return _FAIL;
462 }
463
464 static sint validate_recv_mgnt_frame(struct _adapter *adapter,
465                               union recv_frame *precv_frame)
466 {
467         return _FAIL;
468 }
469
470
471 static sint validate_recv_data_frame(struct _adapter *adapter,
472                               union recv_frame *precv_frame)
473 {
474         int res;
475         u8 bretry;
476         u8 *psa, *pda, *pbssid;
477         struct sta_info *psta = NULL;
478         u8 *ptr = precv_frame->u.hdr.rx_data;
479         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
480         struct security_priv *psecuritypriv = &adapter->securitypriv;
481
482         bretry = GetRetry(ptr);
483         pda = get_da(ptr);
484         psa = get_sa(ptr);
485         pbssid = get_hdr_bssid(ptr);
486         if (pbssid == NULL)
487                 return _FAIL;
488         memcpy(pattrib->dst, pda, ETH_ALEN);
489         memcpy(pattrib->src, psa, ETH_ALEN);
490         memcpy(pattrib->bssid, pbssid, ETH_ALEN);
491         switch (pattrib->to_fr_ds) {
492         case 0:
493                 memcpy(pattrib->ra, pda, ETH_ALEN);
494                 memcpy(pattrib->ta, psa, ETH_ALEN);
495                 res = sta2sta_data_frame(adapter, precv_frame, &psta);
496                 break;
497         case 1:
498                 memcpy(pattrib->ra, pda, ETH_ALEN);
499                 memcpy(pattrib->ta, pbssid, ETH_ALEN);
500                 res = ap2sta_data_frame(adapter, precv_frame, &psta);
501                 break;
502         case 2:
503                 memcpy(pattrib->ra, pbssid, ETH_ALEN);
504                 memcpy(pattrib->ta, psa, ETH_ALEN);
505                 res = sta2ap_data_frame(adapter, precv_frame, &psta);
506                 break;
507         case 3:
508                 memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
509                 memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
510                 return _FAIL;
511         default:
512                 return _FAIL;
513         }
514         if (res == _FAIL)
515                 return _FAIL;
516         if (psta == NULL)
517                 return _FAIL;
518         precv_frame->u.hdr.psta = psta;
519         pattrib->amsdu = 0;
520         /* parsing QC field */
521         if (pattrib->qos == 1) {
522                 pattrib->priority = GetPriority((ptr + 24));
523                 pattrib->ack_policy = GetAckpolicy((ptr + 24));
524                 pattrib->amsdu = GetAMsdu((ptr + 24));
525                 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
526         } else {
527                 pattrib->priority = 0;
528                 pattrib->hdrlen = (pattrib->to_fr_ds == 3) ? 30 : 24;
529         }
530
531         if (pattrib->order)/*HT-CTRL 11n*/
532                 pattrib->hdrlen += 4;
533         precv_frame->u.hdr.preorder_ctrl =
534                          &psta->recvreorder_ctrl[pattrib->priority];
535
536         /* decache, drop duplicate recv packets */
537         if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) ==
538             _FAIL)
539                 return _FAIL;
540
541         if (pattrib->privacy) {
542                 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt,
543                                IS_MCAST(pattrib->ra));
544                 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len,
545                                pattrib->encrypt);
546         } else {
547                 pattrib->encrypt = 0;
548                 pattrib->iv_len = pattrib->icv_len = 0;
549         }
550         return _SUCCESS;
551 }
552
553 sint r8712_validate_recv_frame(struct _adapter *adapter,
554                                union recv_frame *precv_frame)
555 {
556         /*shall check frame subtype, to / from ds, da, bssid */
557         /*then call check if rx seq/frag. duplicated.*/
558
559         u8 type;
560         u8 subtype;
561         sint retval = _SUCCESS;
562         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
563
564         u8 *ptr = precv_frame->u.hdr.rx_data;
565         u8  ver = (unsigned char)(*ptr) & 0x3;
566
567         /*add version chk*/
568         if (ver != 0)
569                 return _FAIL;
570         type =  GetFrameType(ptr);
571         subtype = GetFrameSubType(ptr); /*bit(7)~bit(2)*/
572         pattrib->to_fr_ds = get_tofr_ds(ptr);
573         pattrib->frag_num = GetFragNum(ptr);
574         pattrib->seq_num = GetSequence(ptr);
575         pattrib->pw_save = GetPwrMgt(ptr);
576         pattrib->mfrag = GetMFrag(ptr);
577         pattrib->mdata = GetMData(ptr);
578         pattrib->privacy =  GetPrivacy(ptr);
579         pattrib->order = GetOrder(ptr);
580         switch (type) {
581         case WIFI_MGT_TYPE: /*mgnt*/
582                 retval = validate_recv_mgnt_frame(adapter, precv_frame);
583                 break;
584         case WIFI_CTRL_TYPE:/*ctrl*/
585                 retval = validate_recv_ctrl_frame(adapter, precv_frame);
586                 break;
587         case WIFI_DATA_TYPE: /*data*/
588                 pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
589                 retval = validate_recv_data_frame(adapter, precv_frame);
590                 break;
591         default:
592                 return _FAIL;
593         }
594         return retval;
595 }
596
597 sint r8712_wlanhdr_to_ethhdr(union recv_frame *precvframe)
598 {
599         /*remove the wlanhdr and add the eth_hdr*/
600         sint    rmv_len;
601         u16     len;
602         u8      bsnaphdr;
603         u8      *psnap_type;
604         struct ieee80211_snap_hdr *psnap;
605         struct _adapter *adapter = precvframe->u.hdr.adapter;
606         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
607
608         u8 *ptr = get_recvframe_data(precvframe); /*point to frame_ctrl field*/
609         struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
610
611         if (pattrib->encrypt)
612                 recvframe_pull_tail(precvframe, pattrib->icv_len);
613         psnap = (struct ieee80211_snap_hdr *)(ptr + pattrib->hdrlen +
614                  pattrib->iv_len);
615         psnap_type = ptr + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
616         /* convert hdr + possible LLC headers into Ethernet header */
617         if ((!memcmp(psnap, (void *)rfc1042_header, SNAP_SIZE) &&
618             (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_IPX, 2)) &&
619             (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
620              !memcmp(psnap, (void *)bridge_tunnel_header, SNAP_SIZE)) {
621                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
622                  * replace EtherType */
623                 bsnaphdr = true;
624         } else {
625                 /* Leave Ethernet header part of hdr and full payload */
626                 bsnaphdr = false;
627         }
628         rmv_len = pattrib->hdrlen + pattrib->iv_len +
629                   (bsnaphdr ? SNAP_SIZE : 0);
630         len = precvframe->u.hdr.len - rmv_len;
631         if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
632                 ptr += rmv_len;
633                 *ptr = 0x87;
634                 *(ptr + 1) = 0x12;
635                 /* append rx status for mp test packets */
636                 ptr = recvframe_pull(precvframe, (rmv_len -
637                       sizeof(struct ethhdr) + 2) - 24);
638                 memcpy(ptr, get_rxmem(precvframe), 24);
639                 ptr += 24;
640         } else
641                 ptr = recvframe_pull(precvframe, (rmv_len -
642                       sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
643
644         memcpy(ptr, pattrib->dst, ETH_ALEN);
645         memcpy(ptr + ETH_ALEN, pattrib->src, ETH_ALEN);
646         if (!bsnaphdr) {
647                 len = htons(len);
648                 memcpy(ptr + 12, &len, 2);
649         }
650         return _SUCCESS;
651 }
652
653 s32 r8712_recv_entry(union recv_frame *precvframe)
654 {
655         struct _adapter *padapter;
656         struct recv_priv *precvpriv;
657
658         s32 ret = _SUCCESS;
659
660         padapter = precvframe->u.hdr.adapter;
661         precvpriv = &(padapter->recvpriv);
662
663         padapter->ledpriv.LedControlHandler(padapter, LED_CTL_RX);
664
665         ret = recv_func(padapter, precvframe);
666         if (ret == _FAIL)
667                 goto _recv_entry_drop;
668         precvpriv->rx_pkts++;
669         precvpriv->rx_bytes += (uint)(precvframe->u.hdr.rx_tail -
670                                 precvframe->u.hdr.rx_data);
671         return ret;
672 _recv_entry_drop:
673         precvpriv->rx_drop++;
674         padapter->mppriv.rx_pktloss = precvpriv->rx_drop;
675         return ret;
676 }