1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
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.
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
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
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
27 ******************************************************************************/
29 #define _RTL871X_RECV_C_
32 #include <linux/slab.h>
33 #include <linux/if_ether.h>
34 #include <linux/kmemleak.h>
35 #include <linux/etherdevice.h>
37 #include "osdep_service.h"
38 #include "drv_types.h"
39 #include "recv_osdep.h"
40 #include "mlme_osdep.h"
45 static const u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
47 /* Datagram Delivery Protocol */
48 static const u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
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};
53 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
54 static const u8 rfc1042_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
56 void _r8712_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
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);
63 sint _r8712_init_recv_priv(struct recv_priv *precvpriv,
64 struct _adapter *padapter)
67 union recv_frame *precvframe;
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,
78 if (precvpriv->pallocated_frame_buf == NULL)
80 kmemleak_not_leak(precvpriv->pallocated_frame_buf);
81 precvpriv->precv_frame_buf = precvpriv->pallocated_frame_buf +
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;
94 precvpriv->rx_pending_cnt = 1;
95 return r8712_init_recv_priv(precvpriv, padapter);
98 void _r8712_free_recv_priv(struct recv_priv *precvpriv)
100 kfree(precvpriv->pallocated_frame_buf);
101 r8712_free_recv_priv(precvpriv);
104 union recv_frame *r8712_alloc_recvframe(struct __queue *pfree_recv_queue)
107 union recv_frame *precvframe;
108 struct _adapter *padapter;
109 struct recv_priv *precvpriv;
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);
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--;
123 spin_unlock_irqrestore(&pfree_recv_queue->lock, irqL);
128 * caller : defrag; recvframe_chk_defrag in recv_thread (passive)
129 * pframequeue: defrag_queue : will be accessed in recv_thread (passive)
130 * using spin_lock to protect
132 void r8712_free_recvframe_queue(struct __queue *pframequeue,
133 struct __queue *pfree_recv_queue)
135 union recv_frame *precvframe;
136 struct list_head *plist, *phead;
138 spin_lock(&pframequeue->lock);
139 phead = &pframequeue->queue;
141 while (!end_of_queue_search(phead, plist)) {
142 precvframe = container_of(plist, union recv_frame, u.list);
144 r8712_free_recvframe(precvframe, pfree_recv_queue);
146 spin_unlock(&pframequeue->lock);
149 sint r8712_recvframe_chkmic(struct _adapter *adapter,
150 union recv_frame *precvframe)
152 sint i, res = _SUCCESS;
156 u8 *pframe, *payload, *pframemic;
157 u8 *mickey, idx, *iv;
158 struct sta_info *stainfo;
159 struct rx_pkt_attrib *prxattrib = &precvframe->u.hdr.attrib;
160 struct security_priv *psecuritypriv = &adapter->securitypriv;
162 stainfo = r8712_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
163 if (prxattrib->encrypt == _TKIP_) {
164 /* calculate mic code */
165 if (stainfo != NULL) {
166 if (IS_MCAST(prxattrib->ra)) {
167 iv = precvframe->u.hdr.rx_data +
170 mickey = &psecuritypriv->XGrprxmickey[(((idx >>
171 6) & 0x3)) - 1].skey[0];
172 if (!psecuritypriv->binstallGrpkey)
175 mickey = &stainfo->tkiprxmickey.skey[0];
177 /*icv_len included the mic code*/
178 datalen = precvframe->u.hdr.len - prxattrib->hdrlen -
179 prxattrib->iv_len - prxattrib->icv_len - 8;
180 pframe = precvframe->u.hdr.rx_data;
181 payload = pframe + prxattrib->hdrlen +
183 seccalctkipmic(mickey, pframe, payload, datalen,
185 (unsigned char)prxattrib->priority);
186 pframemic = payload + datalen;
188 for (i = 0; i < 8; i++) {
189 if (miccode[i] != *(pframemic + i))
193 if (prxattrib->bdecrypted)
194 r8712_handle_tkip_mic_err(adapter,
195 (u8)IS_MCAST(prxattrib->ra));
199 if (!psecuritypriv->bcheck_grpkey &&
200 IS_MCAST(prxattrib->ra))
201 psecuritypriv->bcheck_grpkey = true;
203 recvframe_pull_tail(precvframe, 8);
209 /* decrypt and set the ivlen,icvlen of the recv_frame */
210 union recv_frame *r8712_decryptor(struct _adapter *padapter,
211 union recv_frame *precv_frame)
213 struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
214 struct security_priv *psecuritypriv = &padapter->securitypriv;
215 union recv_frame *return_packet = precv_frame;
217 if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) ||
218 psecuritypriv->sw_decrypt)) {
219 psecuritypriv->hw_decrypted = false;
220 switch (prxattrib->encrypt) {
223 r8712_wep_decrypt(padapter, (u8 *)precv_frame);
226 r8712_tkip_decrypt(padapter, (u8 *)precv_frame);
229 r8712_aes_decrypt(padapter, (u8 *)precv_frame);
234 } else if (prxattrib->bdecrypted == 1) {
235 psecuritypriv->hw_decrypted = true;
237 return return_packet;
239 /*###set the security information in the recv_frame */
240 union recv_frame *r8712_portctrl(struct _adapter *adapter,
241 union recv_frame *precv_frame)
245 struct recv_frame_hdr *pfhdr;
246 struct sta_info *psta;
247 struct sta_priv *pstapriv;
248 union recv_frame *prtnframe;
251 pstapriv = &adapter->stapriv;
252 ptr = get_recvframe_data(precv_frame);
253 pfhdr = &precv_frame->u.hdr;
254 psta_addr = pfhdr->attrib.ta;
255 psta = r8712_get_stainfo(pstapriv, psta_addr);
256 auth_alg = adapter->securitypriv.AuthAlgrthm;
259 ptr = ptr + pfhdr->attrib.hdrlen + LLC_HEADER_SIZE;
260 memcpy(ðer_type, ptr, 2);
261 be16_to_cpus(ðer_type);
263 if ((psta != NULL) && (psta->ieee8021x_blocked)) {
265 * only accept EAPOL frame
267 if (ether_type == 0x888e) {
268 prtnframe = precv_frame;
271 r8712_free_recvframe(precv_frame,
272 &adapter->recvpriv.free_recv_queue);
277 * check decryption status, and decrypt the
280 prtnframe = precv_frame;
281 /* check is the EAPOL frame or not (Rekey) */
282 if (ether_type == 0x888e) {
284 prtnframe = precv_frame;
288 prtnframe = precv_frame;
293 static sint recv_decache(union recv_frame *precv_frame, u8 bretry,
294 struct stainfo_rxcache *prxcache)
296 sint tid = precv_frame->u.hdr.attrib.priority;
297 u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num & 0xffff) << 4) |
298 (precv_frame->u.hdr.attrib.frag_num & 0xf);
302 if (seq_ctrl == prxcache->tid_rxseq[tid])
304 prxcache->tid_rxseq[tid] = seq_ctrl;
308 static sint sta2sta_data_frame(struct _adapter *adapter,
309 union recv_frame *precv_frame,
310 struct sta_info **psta)
312 u8 *ptr = precv_frame->u.hdr.rx_data;
314 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
315 struct sta_priv *pstapriv = &adapter->stapriv;
316 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
317 u8 *mybssid = get_bssid(pmlmepriv);
318 u8 *myhwaddr = myid(&adapter->eeprompriv);
320 sint bmcast = IS_MCAST(pattrib->dst);
322 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
323 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
324 /* filter packets that SA is myself or multicast or broadcast */
325 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
327 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
329 if (is_zero_ether_addr(pattrib->bssid) ||
330 is_zero_ether_addr(mybssid) ||
331 (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
333 sta_addr = pattrib->src;
334 } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
335 /* For Station mode, sa and bssid should always be BSSID,
336 * and DA is my mac-address
338 if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN))
340 sta_addr = pattrib->bssid;
341 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
343 /* For AP mode, if DA == MCAST, then BSSID should
346 if (!IS_MCAST(pattrib->bssid))
348 } else { /* not mc-frame */
349 /* For AP mode, if DA is non-MCAST, then it must be
350 * BSSID, and bssid == BSSID
352 if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN))
354 sta_addr = pattrib->src;
356 } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
357 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
358 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
359 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
360 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
361 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
367 *psta = r8712_get_bcmc_stainfo(adapter);
369 *psta = r8712_get_stainfo(pstapriv, sta_addr); /* get ap_info */
371 if (check_fwstate(pmlmepriv, WIFI_MP_STATE))
372 adapter->mppriv.rx_pktloss++;
378 static sint ap2sta_data_frame(struct _adapter *adapter,
379 union recv_frame *precv_frame,
380 struct sta_info **psta)
382 u8 *ptr = precv_frame->u.hdr.rx_data;
383 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
384 struct sta_priv *pstapriv = &adapter->stapriv;
385 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
386 u8 *mybssid = get_bssid(pmlmepriv);
387 u8 *myhwaddr = myid(&adapter->eeprompriv);
388 sint bmcast = IS_MCAST(pattrib->dst);
390 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
391 check_fwstate(pmlmepriv, _FW_LINKED)) {
392 /* if NULL-frame, drop packet */
393 if ((GetFrameSubType(ptr)) == WIFI_DATA_NULL)
395 /* drop QoS-SubType Data, including QoS NULL,
398 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) ==
399 WIFI_QOS_DATA_TYPE) {
400 if (GetFrameSubType(ptr) & (BIT(4) | BIT(5) | BIT(6)))
404 /* filter packets that SA is myself or multicast or broadcast */
405 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
408 /* da should be for me */
409 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
412 if (is_zero_ether_addr(pattrib->bssid) ||
413 is_zero_ether_addr(mybssid) ||
414 (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
417 *psta = r8712_get_bcmc_stainfo(adapter);
419 *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
422 } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) &&
423 check_fwstate(pmlmepriv, _FW_LINKED)) {
424 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
425 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
426 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
427 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
428 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
429 memcpy(pattrib->bssid, mybssid, ETH_ALEN);
430 *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
439 static sint sta2ap_data_frame(struct _adapter *adapter,
440 union recv_frame *precv_frame,
441 struct sta_info **psta)
443 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
444 struct sta_priv *pstapriv = &adapter->stapriv;
445 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
446 unsigned char *mybssid = get_bssid(pmlmepriv);
448 if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
449 /* For AP mode, if DA is non-MCAST, then it must be BSSID,
451 * For AP mode, RA=BSSID, TX=STA(SRC_ADDR), A3=DST_ADDR
453 if (memcmp(pattrib->bssid, mybssid, ETH_ALEN))
455 *psta = r8712_get_stainfo(pstapriv, pattrib->src);
462 static sint validate_recv_ctrl_frame(struct _adapter *adapter,
463 union recv_frame *precv_frame)
468 static sint validate_recv_mgnt_frame(struct _adapter *adapter,
469 union recv_frame *precv_frame)
475 static sint validate_recv_data_frame(struct _adapter *adapter,
476 union recv_frame *precv_frame)
480 u8 *psa, *pda, *pbssid;
481 struct sta_info *psta = NULL;
482 u8 *ptr = precv_frame->u.hdr.rx_data;
483 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
484 struct security_priv *psecuritypriv = &adapter->securitypriv;
486 bretry = GetRetry(ptr);
489 pbssid = get_hdr_bssid(ptr);
492 memcpy(pattrib->dst, pda, ETH_ALEN);
493 memcpy(pattrib->src, psa, ETH_ALEN);
494 memcpy(pattrib->bssid, pbssid, ETH_ALEN);
495 switch (pattrib->to_fr_ds) {
497 memcpy(pattrib->ra, pda, ETH_ALEN);
498 memcpy(pattrib->ta, psa, ETH_ALEN);
499 res = sta2sta_data_frame(adapter, precv_frame, &psta);
502 memcpy(pattrib->ra, pda, ETH_ALEN);
503 memcpy(pattrib->ta, pbssid, ETH_ALEN);
504 res = ap2sta_data_frame(adapter, precv_frame, &psta);
507 memcpy(pattrib->ra, pbssid, ETH_ALEN);
508 memcpy(pattrib->ta, psa, ETH_ALEN);
509 res = sta2ap_data_frame(adapter, precv_frame, &psta);
512 memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
513 memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
522 precv_frame->u.hdr.psta = psta;
524 /* parsing QC field */
525 if (pattrib->qos == 1) {
526 pattrib->priority = GetPriority((ptr + 24));
527 pattrib->ack_policy = GetAckpolicy((ptr + 24));
528 pattrib->amsdu = GetAMsdu((ptr + 24));
529 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
531 pattrib->priority = 0;
532 pattrib->hdrlen = (pattrib->to_fr_ds == 3) ? 30 : 24;
535 if (pattrib->order)/*HT-CTRL 11n*/
536 pattrib->hdrlen += 4;
537 precv_frame->u.hdr.preorder_ctrl =
538 &psta->recvreorder_ctrl[pattrib->priority];
540 /* decache, drop duplicate recv packets */
541 if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) ==
545 if (pattrib->privacy) {
546 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt,
547 IS_MCAST(pattrib->ra));
548 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len,
551 pattrib->encrypt = 0;
552 pattrib->iv_len = pattrib->icv_len = 0;
557 sint r8712_validate_recv_frame(struct _adapter *adapter,
558 union recv_frame *precv_frame)
560 /*shall check frame subtype, to / from ds, da, bssid */
561 /*then call check if rx seq/frag. duplicated.*/
565 sint retval = _SUCCESS;
566 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
568 u8 *ptr = precv_frame->u.hdr.rx_data;
569 u8 ver = (unsigned char)(*ptr) & 0x3;
574 type = GetFrameType(ptr);
575 subtype = GetFrameSubType(ptr); /*bit(7)~bit(2)*/
576 pattrib->to_fr_ds = get_tofr_ds(ptr);
577 pattrib->frag_num = GetFragNum(ptr);
578 pattrib->seq_num = GetSequence(ptr);
579 pattrib->pw_save = GetPwrMgt(ptr);
580 pattrib->mfrag = GetMFrag(ptr);
581 pattrib->mdata = GetMData(ptr);
582 pattrib->privacy = GetPrivacy(ptr);
583 pattrib->order = GetOrder(ptr);
585 case WIFI_MGT_TYPE: /*mgnt*/
586 retval = validate_recv_mgnt_frame(adapter, precv_frame);
588 case WIFI_CTRL_TYPE:/*ctrl*/
589 retval = validate_recv_ctrl_frame(adapter, precv_frame);
591 case WIFI_DATA_TYPE: /*data*/
592 pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
593 retval = validate_recv_data_frame(adapter, precv_frame);
601 sint r8712_wlanhdr_to_ethhdr(union recv_frame *precvframe)
603 /*remove the wlanhdr and add the eth_hdr*/
608 struct ieee80211_snap_hdr *psnap;
609 struct _adapter *adapter = precvframe->u.hdr.adapter;
610 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
612 u8 *ptr = get_recvframe_data(precvframe); /*point to frame_ctrl field*/
613 struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
615 if (pattrib->encrypt)
616 recvframe_pull_tail(precvframe, pattrib->icv_len);
617 psnap = (struct ieee80211_snap_hdr *)(ptr + pattrib->hdrlen +
619 psnap_type = ptr + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
620 /* convert hdr + possible LLC headers into Ethernet header */
621 if ((!memcmp(psnap, (void *)rfc1042_header, SNAP_SIZE) &&
622 (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_IPX, 2)) &&
623 (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
624 !memcmp(psnap, (void *)bridge_tunnel_header, SNAP_SIZE)) {
625 /* remove RFC1042 or Bridge-Tunnel encapsulation and
630 /* Leave Ethernet header part of hdr and full payload */
633 rmv_len = pattrib->hdrlen + pattrib->iv_len +
634 (bsnaphdr ? SNAP_SIZE : 0);
635 len = precvframe->u.hdr.len - rmv_len;
636 if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
640 /* append rx status for mp test packets */
641 ptr = recvframe_pull(precvframe, (rmv_len -
642 sizeof(struct ethhdr) + 2) - 24);
645 memcpy(ptr, get_rxmem(precvframe), 24);
648 ptr = recvframe_pull(precvframe, (rmv_len -
649 sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
654 memcpy(ptr, pattrib->dst, ETH_ALEN);
655 memcpy(ptr + ETH_ALEN, pattrib->src, ETH_ALEN);
657 __be16 be_tmp = htons(len);
659 memcpy(ptr + 12, &be_tmp, 2);
664 s32 r8712_recv_entry(union recv_frame *precvframe)
666 struct _adapter *padapter;
667 struct recv_priv *precvpriv;
671 padapter = precvframe->u.hdr.adapter;
672 precvpriv = &(padapter->recvpriv);
674 padapter->ledpriv.LedControlHandler(padapter, LED_CTL_RX);
676 ret = recv_func(padapter, precvframe);
678 goto _recv_entry_drop;
679 precvpriv->rx_pkts++;
680 precvpriv->rx_bytes += (uint)(precvframe->u.hdr.rx_tail -
681 precvframe->u.hdr.rx_data);
684 precvpriv->rx_drop++;
685 padapter->mppriv.rx_pktloss = precvpriv->rx_drop;