1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
5 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6 * Linux device driver for RTL8192SU
8 * Modifications for inclusion into the Linux staging tree are
9 * Copyright(c) 2010 Larry Finger. All rights reserved.
11 * Contact information:
15 ******************************************************************************/
17 #define _RTL871X_RECV_C_
20 #include <linux/slab.h>
21 #include <linux/if_ether.h>
22 #include <linux/kmemleak.h>
23 #include <linux/etherdevice.h>
25 #include "osdep_service.h"
26 #include "drv_types.h"
27 #include "recv_osdep.h"
28 #include "mlme_osdep.h"
33 static const u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
35 /* Datagram Delivery Protocol */
36 static const u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
38 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
39 static const u8 bridge_tunnel_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
41 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
42 static const u8 rfc1042_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
44 void _r8712_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
46 memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
47 spin_lock_init(&psta_recvpriv->lock);
48 _init_queue(&psta_recvpriv->defrag_q);
51 void _r8712_init_recv_priv(struct recv_priv *precvpriv,
52 struct _adapter *padapter)
55 union recv_frame *precvframe;
57 memset((unsigned char *)precvpriv, 0, sizeof(struct recv_priv));
58 spin_lock_init(&precvpriv->lock);
59 _init_queue(&precvpriv->free_recv_queue);
60 _init_queue(&precvpriv->recv_pending_queue);
61 precvpriv->adapter = padapter;
62 precvpriv->free_recvframe_cnt = NR_RECVFRAME;
63 precvpriv->pallocated_frame_buf = kzalloc(NR_RECVFRAME *
64 sizeof(union recv_frame) + RXFRAME_ALIGN_SZ,
66 if (precvpriv->pallocated_frame_buf == NULL)
68 kmemleak_not_leak(precvpriv->pallocated_frame_buf);
69 precvpriv->precv_frame_buf = precvpriv->pallocated_frame_buf +
71 ((addr_t)(precvpriv->pallocated_frame_buf) &
72 (RXFRAME_ALIGN_SZ - 1));
73 precvframe = (union recv_frame *)precvpriv->precv_frame_buf;
74 for (i = 0; i < NR_RECVFRAME; i++) {
75 INIT_LIST_HEAD(&(precvframe->u.list));
76 list_add_tail(&(precvframe->u.list),
77 &(precvpriv->free_recv_queue.queue));
78 r8712_os_recv_resource_alloc(padapter, precvframe);
79 precvframe->u.hdr.adapter = padapter;
82 precvpriv->rx_pending_cnt = 1;
83 r8712_init_recv_priv(precvpriv, padapter);
86 void _r8712_free_recv_priv(struct recv_priv *precvpriv)
88 kfree(precvpriv->pallocated_frame_buf);
89 r8712_free_recv_priv(precvpriv);
92 union recv_frame *r8712_alloc_recvframe(struct __queue *pfree_recv_queue)
95 union recv_frame *precvframe;
96 struct _adapter *padapter;
97 struct recv_priv *precvpriv;
99 spin_lock_irqsave(&pfree_recv_queue->lock, irqL);
100 precvframe = list_first_entry_or_null(&pfree_recv_queue->queue,
101 union recv_frame, u.hdr.list);
103 list_del_init(&precvframe->u.hdr.list);
104 padapter = precvframe->u.hdr.adapter;
105 if (padapter != NULL) {
106 precvpriv = &padapter->recvpriv;
107 if (pfree_recv_queue == &precvpriv->free_recv_queue)
108 precvpriv->free_recvframe_cnt--;
111 spin_unlock_irqrestore(&pfree_recv_queue->lock, irqL);
116 * caller : defrag; recvframe_chk_defrag in recv_thread (passive)
117 * pframequeue: defrag_queue : will be accessed in recv_thread (passive)
118 * using spin_lock to protect
120 void r8712_free_recvframe_queue(struct __queue *pframequeue,
121 struct __queue *pfree_recv_queue)
123 union recv_frame *precvframe;
124 struct list_head *plist, *phead;
126 spin_lock(&pframequeue->lock);
127 phead = &pframequeue->queue;
129 while (!end_of_queue_search(phead, plist)) {
130 precvframe = container_of(plist, union recv_frame, u.list);
132 r8712_free_recvframe(precvframe, pfree_recv_queue);
134 spin_unlock(&pframequeue->lock);
137 sint r8712_recvframe_chkmic(struct _adapter *adapter,
138 union recv_frame *precvframe)
140 sint i, res = _SUCCESS;
144 u8 *pframe, *payload, *pframemic;
145 u8 *mickey, idx, *iv;
146 struct sta_info *stainfo;
147 struct rx_pkt_attrib *prxattrib = &precvframe->u.hdr.attrib;
148 struct security_priv *psecuritypriv = &adapter->securitypriv;
150 stainfo = r8712_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
151 if (prxattrib->encrypt == _TKIP_) {
152 /* calculate mic code */
153 if (stainfo != NULL) {
154 if (is_multicast_ether_addr(prxattrib->ra)) {
155 iv = precvframe->u.hdr.rx_data +
158 mickey = &psecuritypriv->XGrprxmickey[(((idx >>
159 6) & 0x3)) - 1].skey[0];
160 if (!psecuritypriv->binstallGrpkey)
163 mickey = &stainfo->tkiprxmickey.skey[0];
165 /*icv_len included the mic code*/
166 datalen = precvframe->u.hdr.len - prxattrib->hdrlen -
167 prxattrib->iv_len - prxattrib->icv_len - 8;
168 pframe = precvframe->u.hdr.rx_data;
169 payload = pframe + prxattrib->hdrlen +
171 seccalctkipmic(mickey, pframe, payload, datalen,
173 (unsigned char)prxattrib->priority);
174 pframemic = payload + datalen;
176 for (i = 0; i < 8; i++) {
177 if (miccode[i] != *(pframemic + i))
181 if (prxattrib->bdecrypted)
182 r8712_handle_tkip_mic_err(adapter,
183 (u8)is_multicast_ether_addr(prxattrib->ra));
187 if (!psecuritypriv->bcheck_grpkey &&
188 is_multicast_ether_addr(prxattrib->ra))
189 psecuritypriv->bcheck_grpkey = true;
191 recvframe_pull_tail(precvframe, 8);
197 /* decrypt and set the ivlen,icvlen of the recv_frame */
198 union recv_frame *r8712_decryptor(struct _adapter *padapter,
199 union recv_frame *precv_frame)
201 struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
202 struct security_priv *psecuritypriv = &padapter->securitypriv;
203 union recv_frame *return_packet = precv_frame;
205 if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) ||
206 psecuritypriv->sw_decrypt)) {
207 psecuritypriv->hw_decrypted = false;
208 switch (prxattrib->encrypt) {
211 r8712_wep_decrypt(padapter, (u8 *)precv_frame);
214 r8712_tkip_decrypt(padapter, (u8 *)precv_frame);
217 r8712_aes_decrypt(padapter, (u8 *)precv_frame);
222 } else if (prxattrib->bdecrypted == 1) {
223 psecuritypriv->hw_decrypted = true;
225 return return_packet;
227 /*###set the security information in the recv_frame */
228 union recv_frame *r8712_portctrl(struct _adapter *adapter,
229 union recv_frame *precv_frame)
233 struct recv_frame_hdr *pfhdr;
234 struct sta_info *psta;
235 struct sta_priv *pstapriv;
236 union recv_frame *prtnframe;
239 pstapriv = &adapter->stapriv;
240 ptr = get_recvframe_data(precv_frame);
241 pfhdr = &precv_frame->u.hdr;
242 psta_addr = pfhdr->attrib.ta;
243 psta = r8712_get_stainfo(pstapriv, psta_addr);
244 auth_alg = adapter->securitypriv.AuthAlgrthm;
247 ptr = ptr + pfhdr->attrib.hdrlen + LLC_HEADER_SIZE;
248 ether_type = get_unaligned_be16(ptr);
250 if ((psta != NULL) && (psta->ieee8021x_blocked)) {
252 * only accept EAPOL frame
254 if (ether_type == 0x888e) {
255 prtnframe = precv_frame;
258 r8712_free_recvframe(precv_frame,
259 &adapter->recvpriv.free_recv_queue);
264 * check decryption status, and decrypt the
267 prtnframe = precv_frame;
268 /* check is the EAPOL frame or not (Rekey) */
269 if (ether_type == 0x888e) {
271 prtnframe = precv_frame;
275 prtnframe = precv_frame;
280 static sint recv_decache(union recv_frame *precv_frame, u8 bretry,
281 struct stainfo_rxcache *prxcache)
283 sint tid = precv_frame->u.hdr.attrib.priority;
284 u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num & 0xffff) << 4) |
285 (precv_frame->u.hdr.attrib.frag_num & 0xf);
289 if (seq_ctrl == prxcache->tid_rxseq[tid])
291 prxcache->tid_rxseq[tid] = seq_ctrl;
295 static sint sta2sta_data_frame(struct _adapter *adapter,
296 union recv_frame *precv_frame,
297 struct sta_info **psta)
299 u8 *ptr = precv_frame->u.hdr.rx_data;
301 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
302 struct sta_priv *pstapriv = &adapter->stapriv;
303 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
304 u8 *mybssid = get_bssid(pmlmepriv);
305 u8 *myhwaddr = myid(&adapter->eeprompriv);
307 bool bmcast = is_multicast_ether_addr(pattrib->dst);
309 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
310 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
311 /* filter packets that SA is myself or multicast or broadcast */
312 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
314 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
316 if (is_zero_ether_addr(pattrib->bssid) ||
317 is_zero_ether_addr(mybssid) ||
318 (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
320 sta_addr = pattrib->src;
321 } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
322 /* For Station mode, sa and bssid should always be BSSID,
323 * and DA is my mac-address
325 if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN))
327 sta_addr = pattrib->bssid;
328 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
330 /* For AP mode, if DA == MCAST, then BSSID should
333 if (!is_multicast_ether_addr(pattrib->bssid))
335 } else { /* not mc-frame */
336 /* For AP mode, if DA is non-MCAST, then it must be
337 * BSSID, and bssid == BSSID
339 if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN))
341 sta_addr = pattrib->src;
343 } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
344 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
345 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
346 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
347 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
348 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
354 *psta = r8712_get_bcmc_stainfo(adapter);
356 *psta = r8712_get_stainfo(pstapriv, sta_addr); /* get ap_info */
358 if (check_fwstate(pmlmepriv, WIFI_MP_STATE))
359 adapter->mppriv.rx_pktloss++;
365 static sint ap2sta_data_frame(struct _adapter *adapter,
366 union recv_frame *precv_frame,
367 struct sta_info **psta)
369 u8 *ptr = precv_frame->u.hdr.rx_data;
370 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
371 struct sta_priv *pstapriv = &adapter->stapriv;
372 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
373 u8 *mybssid = get_bssid(pmlmepriv);
374 u8 *myhwaddr = myid(&adapter->eeprompriv);
375 bool bmcast = is_multicast_ether_addr(pattrib->dst);
377 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
378 check_fwstate(pmlmepriv, _FW_LINKED)) {
379 /* if NULL-frame, drop packet */
380 if ((GetFrameSubType(ptr)) == WIFI_DATA_NULL)
382 /* drop QoS-SubType Data, including QoS NULL,
385 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) ==
386 WIFI_QOS_DATA_TYPE) {
387 if (GetFrameSubType(ptr) & (BIT(4) | BIT(5) | BIT(6)))
391 /* filter packets that SA is myself or multicast or broadcast */
392 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN))
395 /* da should be for me */
396 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast))
399 if (is_zero_ether_addr(pattrib->bssid) ||
400 is_zero_ether_addr(mybssid) ||
401 (memcmp(pattrib->bssid, mybssid, ETH_ALEN)))
404 *psta = r8712_get_bcmc_stainfo(adapter);
406 *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
409 } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) &&
410 check_fwstate(pmlmepriv, _FW_LINKED)) {
411 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
412 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
413 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
414 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
415 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
416 memcpy(pattrib->bssid, mybssid, ETH_ALEN);
417 *psta = r8712_get_stainfo(pstapriv, pattrib->bssid);
426 static sint sta2ap_data_frame(struct _adapter *adapter,
427 union recv_frame *precv_frame,
428 struct sta_info **psta)
430 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
431 struct sta_priv *pstapriv = &adapter->stapriv;
432 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
433 unsigned char *mybssid = get_bssid(pmlmepriv);
435 if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
436 /* For AP mode, if DA is non-MCAST, then it must be BSSID,
438 * For AP mode, RA=BSSID, TX=STA(SRC_ADDR), A3=DST_ADDR
440 if (memcmp(pattrib->bssid, mybssid, ETH_ALEN))
442 *psta = r8712_get_stainfo(pstapriv, pattrib->src);
449 static sint validate_recv_ctrl_frame(struct _adapter *adapter,
450 union recv_frame *precv_frame)
455 static sint validate_recv_mgnt_frame(struct _adapter *adapter,
456 union recv_frame *precv_frame)
462 static sint validate_recv_data_frame(struct _adapter *adapter,
463 union recv_frame *precv_frame)
467 u8 *psa, *pda, *pbssid;
468 struct sta_info *psta = NULL;
469 u8 *ptr = precv_frame->u.hdr.rx_data;
470 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
471 struct security_priv *psecuritypriv = &adapter->securitypriv;
473 bretry = GetRetry(ptr);
476 pbssid = get_hdr_bssid(ptr);
479 memcpy(pattrib->dst, pda, ETH_ALEN);
480 memcpy(pattrib->src, psa, ETH_ALEN);
481 memcpy(pattrib->bssid, pbssid, ETH_ALEN);
482 switch (pattrib->to_fr_ds) {
484 memcpy(pattrib->ra, pda, ETH_ALEN);
485 memcpy(pattrib->ta, psa, ETH_ALEN);
486 res = sta2sta_data_frame(adapter, precv_frame, &psta);
489 memcpy(pattrib->ra, pda, ETH_ALEN);
490 memcpy(pattrib->ta, pbssid, ETH_ALEN);
491 res = ap2sta_data_frame(adapter, precv_frame, &psta);
494 memcpy(pattrib->ra, pbssid, ETH_ALEN);
495 memcpy(pattrib->ta, psa, ETH_ALEN);
496 res = sta2ap_data_frame(adapter, precv_frame, &psta);
499 memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
500 memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
509 precv_frame->u.hdr.psta = psta;
511 /* parsing QC field */
512 if (pattrib->qos == 1) {
513 pattrib->priority = GetPriority((ptr + 24));
514 pattrib->ack_policy = GetAckpolicy((ptr + 24));
515 pattrib->amsdu = GetAMsdu((ptr + 24));
516 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
518 pattrib->priority = 0;
519 pattrib->hdrlen = (pattrib->to_fr_ds == 3) ? 30 : 24;
522 if (pattrib->order)/*HT-CTRL 11n*/
523 pattrib->hdrlen += 4;
524 precv_frame->u.hdr.preorder_ctrl =
525 &psta->recvreorder_ctrl[pattrib->priority];
527 /* decache, drop duplicate recv packets */
528 if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) ==
532 if (pattrib->privacy) {
533 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt,
534 is_multicast_ether_addr(pattrib->ra));
535 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len,
538 pattrib->encrypt = 0;
539 pattrib->iv_len = pattrib->icv_len = 0;
544 sint r8712_validate_recv_frame(struct _adapter *adapter,
545 union recv_frame *precv_frame)
547 /*shall check frame subtype, to / from ds, da, bssid */
548 /*then call check if rx seq/frag. duplicated.*/
552 sint retval = _SUCCESS;
553 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
555 u8 *ptr = precv_frame->u.hdr.rx_data;
556 u8 ver = (unsigned char)(*ptr) & 0x3;
561 type = GetFrameType(ptr);
562 subtype = GetFrameSubType(ptr); /*bit(7)~bit(2)*/
563 pattrib->to_fr_ds = get_tofr_ds(ptr);
564 pattrib->frag_num = GetFragNum(ptr);
565 pattrib->seq_num = GetSequence(ptr);
566 pattrib->pw_save = GetPwrMgt(ptr);
567 pattrib->mfrag = GetMFrag(ptr);
568 pattrib->mdata = GetMData(ptr);
569 pattrib->privacy = GetPrivacy(ptr);
570 pattrib->order = GetOrder(ptr);
572 case WIFI_MGT_TYPE: /*mgnt*/
573 retval = validate_recv_mgnt_frame(adapter, precv_frame);
575 case WIFI_CTRL_TYPE:/*ctrl*/
576 retval = validate_recv_ctrl_frame(adapter, precv_frame);
578 case WIFI_DATA_TYPE: /*data*/
579 pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
580 retval = validate_recv_data_frame(adapter, precv_frame);
588 int r8712_wlanhdr_to_ethhdr(union recv_frame *precvframe)
590 /*remove the wlanhdr and add the eth_hdr*/
595 struct ieee80211_snap_hdr *psnap;
596 struct _adapter *adapter = precvframe->u.hdr.adapter;
597 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
599 u8 *ptr = get_recvframe_data(precvframe); /*point to frame_ctrl field*/
600 struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
602 if (pattrib->encrypt)
603 recvframe_pull_tail(precvframe, pattrib->icv_len);
604 psnap = (struct ieee80211_snap_hdr *)(ptr + pattrib->hdrlen +
606 psnap_type = ptr + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
607 /* convert hdr + possible LLC headers into Ethernet header */
608 if ((!memcmp(psnap, (void *)rfc1042_header, SNAP_SIZE) &&
609 (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_IPX, 2)) &&
610 (memcmp(psnap_type, (void *)SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
611 !memcmp(psnap, (void *)bridge_tunnel_header, SNAP_SIZE)) {
612 /* remove RFC1042 or Bridge-Tunnel encapsulation and
617 /* Leave Ethernet header part of hdr and full payload */
620 rmv_len = pattrib->hdrlen + pattrib->iv_len +
621 (bsnaphdr ? SNAP_SIZE : 0);
622 len = precvframe->u.hdr.len - rmv_len;
623 if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
627 /* append rx status for mp test packets */
628 ptr = recvframe_pull(precvframe, (rmv_len -
629 sizeof(struct ethhdr) + 2) - 24);
632 memcpy(ptr, get_rxmem(precvframe), 24);
635 ptr = recvframe_pull(precvframe, (rmv_len -
636 sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
641 memcpy(ptr, pattrib->dst, ETH_ALEN);
642 memcpy(ptr + ETH_ALEN, pattrib->src, ETH_ALEN);
644 __be16 be_tmp = htons(len);
646 memcpy(ptr + 12, &be_tmp, 2);
651 void r8712_recv_entry(union recv_frame *precvframe)
653 struct _adapter *padapter;
654 struct recv_priv *precvpriv;
658 padapter = precvframe->u.hdr.adapter;
659 precvpriv = &(padapter->recvpriv);
661 padapter->ledpriv.LedControlHandler(padapter, LED_CTL_RX);
663 ret = recv_func(padapter, precvframe);
665 goto _recv_entry_drop;
666 precvpriv->rx_pkts++;
667 precvpriv->rx_bytes += (uint)(precvframe->u.hdr.rx_tail -
668 precvframe->u.hdr.rx_data);
671 precvpriv->rx_drop++;
672 padapter->mppriv.rx_pktloss = precvpriv->rx_drop;