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