1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/recv_osdep.h"
9 #include "../include/mlme_osdep.h"
10 #include "../include/ip.h"
11 #include "../include/if_ether.h"
12 #include "../include/ethernet.h"
13 #include "../include/usb_ops.h"
14 #include "../include/wifi.h"
16 static u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
17 static u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
19 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
20 static u8 rtw_bridge_tunnel_header[] = {
21 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8
24 static u8 rtw_rfc1042_header[] = {
25 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
28 void rtw_signal_stat_timer_hdl(struct timer_list *);
30 void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
33 memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
35 spin_lock_init(&psta_recvpriv->lock);
37 _rtw_init_queue(&psta_recvpriv->defrag_q);
41 int _rtw_init_recv_priv(struct recv_priv *precvpriv, struct adapter *padapter)
45 struct recv_frame *precvframe;
49 spin_lock_init(&precvpriv->lock);
51 _rtw_init_queue(&precvpriv->free_recv_queue);
52 _rtw_init_queue(&precvpriv->recv_pending_queue);
53 _rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
55 precvpriv->adapter = padapter;
57 precvpriv->free_recvframe_cnt = NR_RECVFRAME;
59 rtw_os_recv_resource_init(precvpriv, padapter);
61 precvpriv->pallocated_frame_buf = rtw_zvmalloc(NR_RECVFRAME * sizeof(struct recv_frame) + RXFRAME_ALIGN_SZ);
63 if (!precvpriv->pallocated_frame_buf) {
68 precvpriv->precv_frame_buf = (u8 *)N_BYTE_ALIGMENT((size_t)(precvpriv->pallocated_frame_buf), RXFRAME_ALIGN_SZ);
70 precvframe = (struct recv_frame *)precvpriv->precv_frame_buf;
72 for (i = 0; i < NR_RECVFRAME; i++) {
73 INIT_LIST_HEAD(&precvframe->list);
75 list_add_tail(&precvframe->list, &precvpriv->free_recv_queue.queue);
77 res = rtw_os_recv_resource_alloc(padapter, precvframe);
81 precvframe->adapter = padapter;
84 precvpriv->rx_pending_cnt = 1;
86 sema_init(&precvpriv->allrxreturnevt, 0);
88 res = rtw_hal_init_recv_priv(padapter);
90 timer_setup(&precvpriv->signal_stat_timer, rtw_signal_stat_timer_hdl, 0);
91 precvpriv->signal_stat_sampling_interval = 1000; /* ms */
93 rtw_set_signal_stat_timer(precvpriv);
99 void _rtw_free_recv_priv(struct recv_priv *precvpriv)
101 struct adapter *padapter = precvpriv->adapter;
103 rtw_free_uc_swdec_pending_queue(padapter);
105 rtw_os_recv_resource_free(precvpriv);
107 vfree(precvpriv->pallocated_frame_buf);
109 rtw_hal_free_recv_priv(padapter);
112 struct recv_frame *_rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
114 struct recv_frame *hdr;
115 struct list_head *plist, *phead;
116 struct adapter *padapter;
117 struct recv_priv *precvpriv;
119 if (list_empty(&pfree_recv_queue->queue)) {
122 phead = get_list_head(pfree_recv_queue);
126 hdr = container_of(plist, struct recv_frame, list);
128 list_del_init(&hdr->list);
129 padapter = hdr->adapter;
131 precvpriv = &padapter->recvpriv;
132 if (pfree_recv_queue == &precvpriv->free_recv_queue)
133 precvpriv->free_recvframe_cnt--;
137 return (struct recv_frame *)hdr;
140 struct recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
142 struct recv_frame *precvframe;
144 spin_lock_bh(&pfree_recv_queue->lock);
146 precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
148 spin_unlock_bh(&pfree_recv_queue->lock);
153 void rtw_init_recvframe(struct recv_frame *precvframe, struct recv_priv *precvpriv)
155 /* Perry: This can be removed */
156 INIT_LIST_HEAD(&precvframe->list);
161 int rtw_free_recvframe(struct recv_frame *precvframe, struct __queue *pfree_recv_queue)
163 struct adapter *padapter;
164 struct recv_priv *precvpriv;
168 padapter = precvframe->adapter;
169 precvpriv = &padapter->recvpriv;
170 if (precvframe->pkt) {
171 dev_kfree_skb_any(precvframe->pkt);/* free skb by driver */
172 precvframe->pkt = NULL;
175 spin_lock_bh(&pfree_recv_queue->lock);
177 list_del_init(&precvframe->list);
181 list_add_tail(&precvframe->list, get_list_head(pfree_recv_queue));
184 if (pfree_recv_queue == &precvpriv->free_recv_queue)
185 precvpriv->free_recvframe_cnt++;
188 spin_unlock_bh(&pfree_recv_queue->lock);
193 int _rtw_enqueue_recvframe(struct recv_frame *precvframe, struct __queue *queue)
195 struct adapter *padapter = precvframe->adapter;
196 struct recv_priv *precvpriv = &padapter->recvpriv;
198 list_del_init(&precvframe->list);
199 list_add_tail(&precvframe->list, get_list_head(queue));
202 if (queue == &precvpriv->free_recv_queue)
203 precvpriv->free_recvframe_cnt++;
209 int rtw_enqueue_recvframe(struct recv_frame *precvframe, struct __queue *queue)
213 spin_lock_bh(&queue->lock);
214 ret = _rtw_enqueue_recvframe(precvframe, queue);
215 spin_unlock_bh(&queue->lock);
221 caller : defrag ; recvframe_chk_defrag in recv_thread (passive)
222 pframequeue: defrag_queue : will be accessed in recv_thread (passive)
224 using spinlock to protect
228 void rtw_free_recvframe_queue(struct __queue *pframequeue, struct __queue *pfree_recv_queue)
230 struct recv_frame *hdr;
231 struct list_head *plist, *phead;
233 spin_lock(&pframequeue->lock);
235 phead = get_list_head(pframequeue);
238 while (phead != plist) {
239 hdr = container_of(plist, struct recv_frame, list);
243 rtw_free_recvframe((struct recv_frame *)hdr, pfree_recv_queue);
246 spin_unlock(&pframequeue->lock);
250 u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter)
253 struct recv_frame *pending_frame;
254 while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
255 rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
256 DBG_88E("%s: dequeue uc_swdec_pending_queue\n", __func__);
263 int rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, struct __queue *queue)
265 spin_lock_bh(&queue->lock);
267 list_del_init(&precvbuf->list);
268 list_add(&precvbuf->list, get_list_head(queue));
270 spin_unlock_bh(&queue->lock);
275 int rtw_enqueue_recvbuf(struct recv_buf *precvbuf, struct __queue *queue)
279 spin_lock_irqsave(&queue->lock, flags);
281 list_del_init(&precvbuf->list);
283 list_add_tail(&precvbuf->list, get_list_head(queue));
284 spin_unlock_irqrestore(&queue->lock, flags);
288 struct recv_buf *rtw_dequeue_recvbuf(struct __queue *queue)
290 struct recv_buf *precvbuf;
291 struct list_head *plist, *phead;
294 spin_lock_irqsave(&queue->lock, flags);
296 if (list_empty(&queue->queue)) {
299 phead = get_list_head(queue);
303 precvbuf = container_of(plist, struct recv_buf, list);
305 list_del_init(&precvbuf->list);
308 spin_unlock_irqrestore(&queue->lock, flags);
313 static int recvframe_chkmic(struct adapter *adapter, struct recv_frame *precvframe)
315 int i, res = _SUCCESS;
318 u8 bmic_err = false, brpt_micerror = true;
319 u8 *pframe, *payload, *pframemic;
321 struct sta_info *stainfo;
322 struct rx_pkt_attrib *prxattrib = &precvframe->attrib;
323 struct security_priv *psecuritypriv = &adapter->securitypriv;
325 struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
326 struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
328 stainfo = rtw_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
330 if (prxattrib->encrypt == _TKIP_) {
331 /* calculate mic code */
333 if (IS_MCAST(prxattrib->ra)) {
334 mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
336 if (!psecuritypriv) {
338 DBG_88E("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
342 mickey = &stainfo->dot11tkiprxmickey.skey[0];
345 datalen = precvframe->len - prxattrib->hdrlen - prxattrib->iv_len - prxattrib->icv_len - 8;/* icv_len included the mic code */
346 pframe = precvframe->rx_data;
347 payload = pframe + prxattrib->hdrlen + prxattrib->iv_len;
349 rtw_seccalctkipmic(mickey, pframe, payload, datalen, &miccode[0],
350 (unsigned char)prxattrib->priority); /* care the length of the data */
352 pframemic = payload + datalen;
356 for (i = 0; i < 8; i++) {
357 if (miccode[i] != *(pframemic + i))
362 /* double check key_index for some timing issue , */
363 /* cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
364 if (IS_MCAST(prxattrib->ra) && prxattrib->key_index != pmlmeinfo->key_index)
365 brpt_micerror = false;
367 if ((prxattrib->bdecrypted) && (brpt_micerror)) {
368 rtw_handle_tkip_mic_err(adapter, (u8)IS_MCAST(prxattrib->ra));
369 DBG_88E(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
371 DBG_88E(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
376 if ((!psecuritypriv->bcheck_grpkey) && (IS_MCAST(prxattrib->ra)))
377 psecuritypriv->bcheck_grpkey = true;
381 recvframe_pull_tail(precvframe, 8);
389 /* decrypt and set the ivlen, icvlen of the recv_frame */
390 static struct recv_frame *decryptor(struct adapter *padapter, struct recv_frame *precv_frame)
392 struct rx_pkt_attrib *prxattrib = &precv_frame->attrib;
393 struct security_priv *psecuritypriv = &padapter->securitypriv;
394 struct recv_frame *return_packet = precv_frame;
397 if (prxattrib->encrypt > 0) {
398 u8 *iv = precv_frame->rx_data + prxattrib->hdrlen;
399 prxattrib->key_index = (((iv[3]) >> 6) & 0x3);
401 if (prxattrib->key_index > WEP_KEYS) {
402 DBG_88E("prxattrib->key_index(%d)>WEP_KEYS\n", prxattrib->key_index);
404 switch (prxattrib->encrypt) {
407 prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
412 prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
418 if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) || (psecuritypriv->sw_decrypt))) {
419 psecuritypriv->hw_decrypted = false;
421 switch (prxattrib->encrypt) {
424 rtw_wep_decrypt(padapter, (u8 *)precv_frame);
427 res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
430 res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
435 } else if (prxattrib->bdecrypted == 1 && prxattrib->encrypt > 0 &&
436 (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_))
437 psecuritypriv->hw_decrypted = true;
440 rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
441 return_packet = NULL;
443 prxattrib->bdecrypted = true;
446 return return_packet;
449 /* set the security information in the recv_frame */
450 static struct recv_frame *portctrl(struct adapter *adapter, struct recv_frame *precv_frame)
454 struct recv_frame *pfhdr;
455 struct sta_info *psta;
456 struct sta_priv *pstapriv;
457 struct recv_frame *prtnframe;
459 u16 eapol_type = 0x888e;/* for Funia BD's WPA issue */
460 struct rx_pkt_attrib *pattrib;
463 pstapriv = &adapter->stapriv;
465 auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
467 ptr = precv_frame->rx_data;
469 pattrib = &pfhdr->attrib;
470 psta_addr = pattrib->ta;
474 psta = rtw_get_stainfo(pstapriv, psta_addr);
477 if (psta && psta->ieee8021x_blocked) {
479 /* only accept EAPOL frame */
480 prtnframe = precv_frame;
483 ptr = ptr + pfhdr->attrib.hdrlen + pfhdr->attrib.iv_len + LLC_HEADER_SIZE;
484 memcpy(&be_tmp, ptr, 2);
485 ether_type = ntohs(be_tmp);
487 if (ether_type == eapol_type) {
488 prtnframe = precv_frame;
490 /* free this frame */
491 rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
496 /* check decryption status, and decrypt the frame if needed */
497 prtnframe = precv_frame;
500 prtnframe = precv_frame;
506 static int recv_decache(struct recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache)
508 int tid = precv_frame->attrib.priority;
510 u16 seq_ctrl = ((precv_frame->attrib.seq_num & 0xffff) << 4) |
511 (precv_frame->attrib.frag_num & 0xf);
516 if (1) {/* if (bretry) */
517 if (seq_ctrl == prxcache->tid_rxseq[tid])
521 prxcache->tid_rxseq[tid] = seq_ctrl;
526 void process_pwrbit_data(struct adapter *padapter, struct recv_frame *precv_frame);
527 void process_pwrbit_data(struct adapter *padapter, struct recv_frame *precv_frame)
529 #ifdef CONFIG_88EU_AP_MODE
530 unsigned char pwrbit;
531 u8 *ptr = precv_frame->rx_data;
532 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
533 struct sta_priv *pstapriv = &padapter->stapriv;
534 struct sta_info *psta = NULL;
536 psta = rtw_get_stainfo(pstapriv, pattrib->src);
538 pwrbit = GetPwrMgt(ptr);
542 if (!(psta->state & WIFI_SLEEP_STATE))
543 stop_sta_xmit(padapter, psta);
545 if (psta->state & WIFI_SLEEP_STATE)
546 wakeup_sta_to_xmit(padapter, psta);
553 static void process_wmmps_data(struct adapter *padapter, struct recv_frame *precv_frame)
555 #ifdef CONFIG_88EU_AP_MODE
556 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
557 struct sta_priv *pstapriv = &padapter->stapriv;
558 struct sta_info *psta = NULL;
560 psta = rtw_get_stainfo(pstapriv, pattrib->src);
565 if (!psta->qos_option)
568 if (!(psta->qos_info & 0xf))
571 if (psta->state & WIFI_SLEEP_STATE) {
574 switch (pattrib->priority) {
577 wmmps_ac = psta->uapsd_bk & BIT(1);
581 wmmps_ac = psta->uapsd_vi & BIT(1);
585 wmmps_ac = psta->uapsd_vo & BIT(1);
590 wmmps_ac = psta->uapsd_be & BIT(1);
595 if (psta->sleepq_ac_len > 0) {
596 /* process received triggered frame */
597 xmit_delivery_enabled_frames(padapter, psta);
599 /* issue one qos null frame with More data bit = 0 and the EOSP bit set (= 1) */
600 issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
608 static void count_rx_stats(struct adapter *padapter, struct recv_frame *prframe, struct sta_info *sta)
611 struct sta_info *psta = NULL;
612 struct stainfo_stats *pstats = NULL;
613 struct rx_pkt_attrib *pattrib = &prframe->attrib;
614 struct recv_priv *precvpriv = &padapter->recvpriv;
616 sz = get_recvframe_len(prframe);
617 precvpriv->rx_bytes += sz;
619 padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
621 if (!is_broadcast_ether_addr(pattrib->dst) && !IS_MCAST(pattrib->dst))
622 padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
627 psta = prframe->psta;
630 pstats = &psta->sta_stats;
632 pstats->rx_data_pkts++;
633 pstats->rx_bytes += sz;
637 int sta2sta_data_frame(
638 struct adapter *adapter,
639 struct recv_frame *precv_frame,
640 struct sta_info **psta
643 int sta2sta_data_frame(struct adapter *adapter, struct recv_frame *precv_frame, struct sta_info **psta)
645 u8 *ptr = precv_frame->rx_data;
647 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
648 struct sta_priv *pstapriv = &adapter->stapriv;
649 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
650 u8 *mybssid = get_bssid(pmlmepriv);
651 u8 *myhwaddr = myid(&adapter->eeprompriv);
653 int bmcast = IS_MCAST(pattrib->dst);
655 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
656 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
657 /* filter packets that SA is myself or multicast or broadcast */
658 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
663 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
668 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
669 !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
670 memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
675 sta_addr = pattrib->src;
676 } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
677 /* For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
678 if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN)) {
682 sta_addr = pattrib->bssid;
683 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
685 /* For AP mode, if DA == MCAST, then BSSID should be also MCAST */
686 if (!IS_MCAST(pattrib->bssid)) {
690 } else { /* not mc-frame */
691 /* For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
692 if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
697 sta_addr = pattrib->src;
699 } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
700 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
701 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
702 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
703 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
704 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
712 *psta = rtw_get_bcmc_stainfo(adapter);
714 *psta = rtw_get_stainfo(pstapriv, sta_addr); /* get ap_info */
717 if (adapter->registrypriv.mp_mode == 1) {
718 if (check_fwstate(pmlmepriv, WIFI_MP_STATE))
719 adapter->mppriv.rx_pktloss++;
730 static int ap2sta_data_frame(
731 struct adapter *adapter,
732 struct recv_frame *precv_frame,
733 struct sta_info **psta)
735 u8 *ptr = precv_frame->rx_data;
736 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
738 struct sta_priv *pstapriv = &adapter->stapriv;
739 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
740 u8 *mybssid = get_bssid(pmlmepriv);
741 u8 *myhwaddr = myid(&adapter->eeprompriv);
742 int bmcast = IS_MCAST(pattrib->dst);
744 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
745 (check_fwstate(pmlmepriv, _FW_LINKED) ||
746 check_fwstate(pmlmepriv, _FW_UNDER_LINKING))) {
747 /* filter packets that SA is myself or multicast or broadcast */
748 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
753 /* da should be for me */
754 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
760 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
761 !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
762 (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
764 DBG_88E("issue_deauth to the nonassociated ap=%pM for the reason(7)\n", (pattrib->bssid));
765 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
773 *psta = rtw_get_bcmc_stainfo(adapter);
775 *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /* get ap_info */
782 /* if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) { */
785 if (GetFrameSubType(ptr) & BIT(6)) {
786 /* No data, will not indicate to upper layer, temporily count it here */
787 count_rx_stats(adapter, precv_frame, *psta);
788 ret = RTW_RX_HANDLED;
791 } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) &&
792 check_fwstate(pmlmepriv, _FW_LINKED)) {
793 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
794 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
795 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
796 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
797 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
800 memcpy(pattrib->bssid, mybssid, ETH_ALEN);
802 *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /* get sta_info */
807 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
809 ret = RTW_RX_HANDLED;
812 if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
813 *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /* get sta_info */
815 DBG_88E("issue_deauth to the ap =%pM for the reason(7)\n", (pattrib->bssid));
817 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
829 static int sta2ap_data_frame(struct adapter *adapter,
830 struct recv_frame *precv_frame,
831 struct sta_info **psta)
833 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
834 struct sta_priv *pstapriv = &adapter->stapriv;
835 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
836 u8 *ptr = precv_frame->rx_data;
837 unsigned char *mybssid = get_bssid(pmlmepriv);
840 if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
841 /* For AP mode, RA = BSSID, TX = STA(SRC_ADDR), A3 = DST_ADDR */
842 if (memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
847 *psta = rtw_get_stainfo(pstapriv, pattrib->src);
849 DBG_88E("issue_deauth to sta=%pM for the reason(7)\n", (pattrib->src));
851 issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
853 ret = RTW_RX_HANDLED;
857 process_pwrbit_data(adapter, precv_frame);
859 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
860 process_wmmps_data(adapter, precv_frame);
863 if (GetFrameSubType(ptr) & BIT(6)) {
864 /* No data, will not indicate to upper layer, temporily count it here */
865 count_rx_stats(adapter, precv_frame, *psta);
866 ret = RTW_RX_HANDLED;
870 u8 *myhwaddr = myid(&adapter->eeprompriv);
871 if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
872 ret = RTW_RX_HANDLED;
875 DBG_88E("issue_deauth to sta=%pM for the reason(7)\n", (pattrib->src));
876 issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
877 ret = RTW_RX_HANDLED;
886 static int validate_recv_ctrl_frame(struct adapter *padapter,
887 struct recv_frame *precv_frame)
889 #ifdef CONFIG_88EU_AP_MODE
890 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
891 struct sta_priv *pstapriv = &padapter->stapriv;
892 u8 *pframe = precv_frame->rx_data;
893 /* uint len = precv_frame->len; */
895 if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
898 /* receive the frames that ra(a1) is my address */
899 if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
902 /* only handle ps-poll */
903 if (GetFrameSubType(pframe) == WIFI_PSPOLL) {
906 struct sta_info *psta = NULL;
908 aid = GetAid(pframe);
909 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
911 if (!psta || psta->aid != aid)
914 /* for rx pkt statistics */
915 psta->sta_stats.rx_ctrl_pkts++;
917 switch (pattrib->priority) {
920 wmmps_ac = psta->uapsd_bk & BIT(0);
924 wmmps_ac = psta->uapsd_vi & BIT(0);
928 wmmps_ac = psta->uapsd_vo & BIT(0);
933 wmmps_ac = psta->uapsd_be & BIT(0);
940 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
941 DBG_88E("%s alive check-rx ps-poll\n", __func__);
942 psta->expire_to = pstapriv->expire_to;
943 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
946 if ((psta->state & WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap & BIT(psta->aid))) {
947 struct list_head *xmitframe_plist, *xmitframe_phead;
948 struct xmit_frame *pxmitframe = NULL;
949 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
951 spin_lock_bh(&pxmitpriv->lock);
953 xmitframe_phead = get_list_head(&psta->sleep_q);
954 xmitframe_plist = xmitframe_phead->next;
956 if (xmitframe_phead != xmitframe_plist) {
957 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
959 xmitframe_plist = xmitframe_plist->next;
961 list_del_init(&pxmitframe->list);
965 if (psta->sleepq_len > 0)
966 pxmitframe->attrib.mdata = 1;
968 pxmitframe->attrib.mdata = 0;
970 pxmitframe->attrib.triggered = 1;
972 rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
974 if (psta->sleepq_len == 0) {
975 pstapriv->tim_bitmap &= ~BIT(psta->aid);
977 /* upate BCN for TIM IE */
978 /* update_BCNTIM(padapter); */
979 update_beacon(padapter, _TIM_IE_, NULL, false);
982 if (pstapriv->tim_bitmap & BIT(psta->aid)) {
983 if (psta->sleepq_len == 0) {
984 DBG_88E("no buffered packets to xmit\n");
986 /* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
987 issue_nulldata(padapter, psta->hwaddr, 0, 0, 0);
989 DBG_88E("error!psta->sleepq_len=%d\n", psta->sleepq_len);
990 psta->sleepq_len = 0;
993 pstapriv->tim_bitmap &= ~BIT(psta->aid);
995 /* upate BCN for TIM IE */
996 /* update_BCNTIM(padapter); */
997 update_beacon(padapter, _TIM_IE_, NULL, false);
1000 spin_unlock_bh(&pxmitpriv->lock);
1009 struct recv_frame *recvframe_chk_defrag(struct adapter *padapter, struct recv_frame *precv_frame);
1011 static int validate_recv_mgnt_frame(struct adapter *padapter,
1012 struct recv_frame *precv_frame)
1014 struct sta_info *psta;
1016 precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1020 /* for rx pkt statistics */
1021 psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(precv_frame->rx_data));
1023 psta->sta_stats.rx_mgnt_pkts++;
1024 if (GetFrameSubType(precv_frame->rx_data) == WIFI_BEACON) {
1025 psta->sta_stats.rx_beacon_pkts++;
1026 } else if (GetFrameSubType(precv_frame->rx_data) == WIFI_PROBEREQ) {
1027 psta->sta_stats.rx_probereq_pkts++;
1028 } else if (GetFrameSubType(precv_frame->rx_data) == WIFI_PROBERSP) {
1029 if (!memcmp(padapter->eeprompriv.mac_addr, GetAddr1Ptr(precv_frame->rx_data), ETH_ALEN))
1030 psta->sta_stats.rx_probersp_pkts++;
1031 else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->rx_data)) ||
1032 is_multicast_mac_addr(GetAddr1Ptr(precv_frame->rx_data)))
1033 psta->sta_stats.rx_probersp_bm_pkts++;
1035 psta->sta_stats.rx_probersp_uo_pkts++;
1039 mgt_dispatcher(padapter, precv_frame);
1044 static int validate_recv_data_frame(struct adapter *adapter,
1045 struct recv_frame *precv_frame)
1048 u8 *psa, *pda, *pbssid;
1049 struct sta_info *psta = NULL;
1050 u8 *ptr = precv_frame->rx_data;
1051 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
1052 struct security_priv *psecuritypriv = &adapter->securitypriv;
1055 bretry = GetRetry(ptr);
1058 pbssid = get_hdr_bssid(ptr);
1065 memcpy(pattrib->dst, pda, ETH_ALEN);
1066 memcpy(pattrib->src, psa, ETH_ALEN);
1068 memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1070 switch (pattrib->to_fr_ds) {
1072 memcpy(pattrib->ra, pda, ETH_ALEN);
1073 memcpy(pattrib->ta, psa, ETH_ALEN);
1074 ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1077 memcpy(pattrib->ra, pda, ETH_ALEN);
1078 memcpy(pattrib->ta, pbssid, ETH_ALEN);
1079 ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1082 memcpy(pattrib->ra, pbssid, ETH_ALEN);
1083 memcpy(pattrib->ta, psa, ETH_ALEN);
1084 ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1087 memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1088 memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1098 } else if (ret == RTW_RX_HANDLED) {
1107 /* psta->rssi = prxcmd->rssi; */
1108 /* psta->signal_quality = prxcmd->sq; */
1109 precv_frame->psta = psta;
1112 pattrib->ack_policy = 0;
1113 /* parsing QC field */
1114 if (pattrib->qos == 1) {
1115 pattrib->priority = GetPriority((ptr + 24));
1116 pattrib->ack_policy = GetAckpolicy((ptr + 24));
1117 pattrib->amsdu = GetAMsdu((ptr + 24));
1118 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
1120 if (pattrib->priority != 0 && pattrib->priority != 3)
1121 adapter->recvpriv.bIsAnyNonBEPkts = true;
1123 pattrib->priority = 0;
1124 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 30 : 24;
1127 if (pattrib->order)/* HT-CTRL 11n */
1128 pattrib->hdrlen += 4;
1130 precv_frame->preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1132 /* decache, drop duplicate recv packets */
1133 if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL) {
1138 if (pattrib->privacy) {
1139 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
1141 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1143 pattrib->encrypt = 0;
1144 pattrib->iv_len = 0;
1145 pattrib->icv_len = 0;
1153 static int validate_recv_frame(struct adapter *adapter, struct recv_frame *precv_frame)
1155 /* shall check frame subtype, to / from ds, da, bssid */
1157 /* then call check if rx seq/frag. duplicated. */
1161 int retval = _SUCCESS;
1163 struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
1164 u8 *ptr = precv_frame->rx_data;
1165 u8 ver = (unsigned char)(*ptr) & 0x3;
1166 struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1168 if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
1169 int ch_set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, rtw_get_oper_ch(adapter));
1170 if (ch_set_idx >= 0)
1171 pmlmeext->channel_set[ch_set_idx].rx_count++;
1174 /* add version chk */
1180 type = GetFrameType(ptr);
1181 subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1183 pattrib->to_fr_ds = get_tofr_ds(ptr);
1185 pattrib->frag_num = GetFragNum(ptr);
1186 pattrib->seq_num = GetSequence(ptr);
1188 pattrib->pw_save = GetPwrMgt(ptr);
1189 pattrib->mfrag = GetMFrag(ptr);
1190 pattrib->mdata = GetMData(ptr);
1191 pattrib->privacy = GetPrivacy(ptr);
1192 pattrib->order = GetOrder(ptr);
1194 /* Dump rx packets */
1195 rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1196 if (bDumpRxPkt == 1) {/* dump all rx packets */
1198 DBG_88E("#############################\n");
1200 for (i = 0; i < 64; i = i + 8)
1201 DBG_88E("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr + i),
1202 *(ptr + i + 1), *(ptr + i + 2), *(ptr + i + 3), *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
1203 DBG_88E("#############################\n");
1204 } else if (bDumpRxPkt == 2) {
1205 if (type == WIFI_MGT_TYPE) {
1207 DBG_88E("#############################\n");
1209 for (i = 0; i < 64; i = i + 8)
1210 DBG_88E("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr + i),
1211 *(ptr + i + 1), *(ptr + i + 2), *(ptr + i + 3), *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
1212 DBG_88E("#############################\n");
1214 } else if (bDumpRxPkt == 3) {
1215 if (type == WIFI_DATA_TYPE) {
1217 DBG_88E("#############################\n");
1219 for (i = 0; i < 64; i = i + 8)
1220 DBG_88E("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr + i),
1221 *(ptr + i + 1), *(ptr + i + 2), *(ptr + i + 3), *(ptr + i + 4), *(ptr + i + 5), *(ptr + i + 6), *(ptr + i + 7));
1222 DBG_88E("#############################\n");
1226 case WIFI_MGT_TYPE: /* mgnt */
1227 validate_recv_mgnt_frame(adapter, precv_frame);
1228 retval = _FAIL; /* only data frame return _SUCCESS */
1230 case WIFI_CTRL_TYPE: /* ctrl */
1231 validate_recv_ctrl_frame(adapter, precv_frame);
1232 retval = _FAIL; /* only data frame return _SUCCESS */
1234 case WIFI_DATA_TYPE: /* data */
1235 rtw_led_control(adapter, LED_CTL_RX);
1236 pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
1237 retval = validate_recv_data_frame(adapter, precv_frame);
1238 if (retval == _FAIL) {
1239 struct recv_priv *precvpriv = &adapter->recvpriv;
1240 precvpriv->rx_drop++;
1253 /* remove the wlanhdr and add the eth_hdr */
1255 static int wlanhdr_to_ethhdr(struct recv_frame *precvframe)
1262 struct ieee80211_snap_hdr *psnap;
1265 struct adapter *adapter = precvframe->adapter;
1266 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1268 u8 *ptr = get_recvframe_data(precvframe); /* point to frame_ctrl field */
1269 struct rx_pkt_attrib *pattrib = &precvframe->attrib;
1271 if (pattrib->encrypt)
1272 recvframe_pull_tail(precvframe, pattrib->icv_len);
1274 psnap = (struct ieee80211_snap_hdr *)(ptr + pattrib->hdrlen + pattrib->iv_len);
1275 psnap_type = ptr + pattrib->hdrlen + pattrib->iv_len + SNAP_SIZE;
1276 /* convert hdr + possible LLC headers into Ethernet header */
1277 if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
1278 memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2) &&
1279 memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2)) ||
1280 !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
1281 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1284 /* Leave Ethernet header part of hdr and full payload */
1288 rmv_len = pattrib->hdrlen + pattrib->iv_len + (bsnaphdr ? SNAP_SIZE : 0);
1289 len = precvframe->len - rmv_len;
1291 memcpy(&be_tmp, ptr + rmv_len, 2);
1292 eth_type = ntohs(be_tmp); /* pattrib->ether_type */
1293 pattrib->eth_type = eth_type;
1295 if ((check_fwstate(pmlmepriv, WIFI_MP_STATE))) {
1301 /* append rx status for mp test packets */
1302 ptr = recvframe_pull(precvframe, (rmv_len - sizeof(struct ethhdr) + 2) - 24);
1303 memcpy(ptr, get_rxmem(precvframe), 24);
1306 ptr = recvframe_pull(precvframe, (rmv_len - sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
1309 memcpy(ptr, pattrib->dst, ETH_ALEN);
1310 memcpy(ptr + ETH_ALEN, pattrib->src, ETH_ALEN);
1313 be_tmp = htons(len);
1314 memcpy(ptr + 12, &be_tmp, 2);
1320 /* perform defrag */
1321 static struct recv_frame *recvframe_defrag(struct adapter *adapter, struct __queue *defrag_q)
1323 struct list_head *plist, *phead;
1326 struct recv_frame *pfhdr, *pnfhdr;
1327 struct recv_frame *prframe, *pnextrframe;
1328 struct __queue *pfree_recv_queue;
1331 pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
1333 phead = get_list_head(defrag_q);
1334 plist = phead->next;
1335 pfhdr = container_of(plist, struct recv_frame, list);
1336 prframe = (struct recv_frame *)pfhdr;
1337 list_del_init(&prframe->list);
1339 if (curfragnum != pfhdr->attrib.frag_num) {
1340 /* the first fragment number must be 0 */
1341 /* free the whole queue */
1342 rtw_free_recvframe(prframe, pfree_recv_queue);
1343 rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1350 plist = get_list_head(defrag_q);
1351 plist = phead->next;
1352 pfhdr = container_of(plist, struct recv_frame, list);
1353 prframe = (struct recv_frame *)pfhdr;
1354 list_del_init(&prframe->list);
1356 plist = plist->next;
1358 while (phead != plist) {
1359 pnfhdr = container_of(plist, struct recv_frame, list);
1360 pnextrframe = (struct recv_frame *)pnfhdr;
1362 /* check the fragment sequence (2nd ~n fragment frame) */
1364 if (curfragnum != pnfhdr->attrib.frag_num) {
1365 /* the fragment number must be increasing (after decache) */
1366 /* release the defrag_q & prframe */
1367 rtw_free_recvframe(prframe, pfree_recv_queue);
1368 rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1374 /* copy the 2nd~n fragment frame's payload to the first fragment */
1375 /* get the 2nd~last fragment frame's payload */
1377 wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
1379 recvframe_pull(pnextrframe, wlanhdr_offset);
1381 /* append to first fragment frame's tail (if privacy frame, pull the ICV) */
1382 recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
1385 memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
1387 recvframe_put(prframe, pnfhdr->len);
1389 pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
1390 plist = plist->next;
1393 /* free the defrag_q queue and return the prframe */
1394 rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1399 /* check if need to defrag, if needed queue the frame to defrag_q */
1400 struct recv_frame *recvframe_chk_defrag(struct adapter *padapter, struct recv_frame *precv_frame)
1405 struct recv_frame *pfhdr;
1406 struct sta_info *psta;
1407 struct sta_priv *pstapriv;
1408 struct list_head *phead;
1409 struct recv_frame *prtnframe = NULL;
1410 struct __queue *pfree_recv_queue, *pdefrag_q;
1412 pstapriv = &padapter->stapriv;
1414 pfhdr = precv_frame;
1416 pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1418 /* need to define struct of wlan header frame ctrl */
1419 ismfrag = pfhdr->attrib.mfrag;
1420 fragnum = pfhdr->attrib.frag_num;
1422 psta_addr = pfhdr->attrib.ta;
1423 psta = rtw_get_stainfo(pstapriv, psta_addr);
1425 u8 type = GetFrameType(pfhdr->rx_data);
1426 if (type != WIFI_DATA_TYPE) {
1427 psta = rtw_get_bcmc_stainfo(padapter);
1428 pdefrag_q = &psta->sta_recvpriv.defrag_q;
1433 pdefrag_q = &psta->sta_recvpriv.defrag_q;
1436 if ((ismfrag == 0) && (fragnum == 0))
1437 prtnframe = precv_frame;/* isn't a fragment frame */
1440 /* 0~(n-1) fragment frame */
1441 /* enqueue to defraf_g */
1444 /* the first fragment */
1445 if (!list_empty(&pdefrag_q->queue)) {
1446 /* free current defrag_q */
1447 rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
1451 /* Then enqueue the 0~(n-1) fragment into the defrag_q */
1453 phead = get_list_head(pdefrag_q);
1454 list_add_tail(&pfhdr->list, phead);
1458 /* can't find this ta's defrag_queue, so free this recv_frame */
1459 if (precv_frame && pfree_recv_queue)
1460 rtw_free_recvframe(precv_frame, pfree_recv_queue);
1465 if ((ismfrag == 0) && (fragnum != 0)) {
1466 /* the last fragment frame */
1467 /* enqueue the last fragment */
1469 phead = get_list_head(pdefrag_q);
1470 list_add_tail(&pfhdr->list, phead);
1472 /* call recvframe_defrag to defrag */
1473 precv_frame = recvframe_defrag(padapter, pdefrag_q);
1474 prtnframe = precv_frame;
1476 /* can't find this ta's defrag_queue, so free this recv_frame */
1477 if (precv_frame && pfree_recv_queue)
1478 rtw_free_recvframe(precv_frame, pfree_recv_queue);
1483 if (prtnframe && prtnframe->attrib.privacy) {
1484 /* after defrag we must check tkip mic code */
1485 if (recvframe_chkmic(padapter, prtnframe) == _FAIL) {
1486 if (precv_frame && pfree_recv_queue)
1487 rtw_free_recvframe(prtnframe, pfree_recv_queue);
1495 static int amsdu_to_msdu(struct adapter *padapter, struct recv_frame *prframe)
1497 int a_len, padding_len;
1498 u16 eth_type, nSubframe_Length;
1500 unsigned char *pdata;
1501 struct rx_pkt_attrib *pattrib;
1502 unsigned char *data_ptr;
1503 struct sk_buff *sub_skb, *subframes[MAX_SUBFRAME_COUNT];
1504 struct recv_priv *precvpriv = &padapter->recvpriv;
1505 struct __queue *pfree_recv_queue = &precvpriv->free_recv_queue;
1509 pattrib = &prframe->attrib;
1511 recvframe_pull(prframe, prframe->attrib.hdrlen);
1513 if (prframe->attrib.iv_len > 0)
1514 recvframe_pull(prframe, prframe->attrib.iv_len);
1516 a_len = prframe->len;
1518 pdata = prframe->rx_data;
1520 while (a_len > ETH_HLEN) {
1521 /* Offset 12 denote 2 mac address */
1522 nSubframe_Length = RTW_GET_BE16(pdata + 12);
1524 if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1525 DBG_88E("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
1529 /* move the data point to data content */
1533 /* Allocate new skb for releasing to upper layer */
1534 sub_skb = dev_alloc_skb(nSubframe_Length + 12);
1536 skb_reserve(sub_skb, 12);
1537 data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
1538 memcpy(data_ptr, pdata, nSubframe_Length);
1540 sub_skb = skb_clone(prframe->pkt, GFP_ATOMIC);
1542 sub_skb->data = pdata;
1543 sub_skb->len = nSubframe_Length;
1544 skb_set_tail_pointer(sub_skb, nSubframe_Length);
1546 DBG_88E("skb_clone() Fail!!! , nr_subframes=%d\n", nr_subframes);
1551 subframes[nr_subframes++] = sub_skb;
1553 if (nr_subframes >= MAX_SUBFRAME_COUNT) {
1554 DBG_88E("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1558 pdata += nSubframe_Length;
1559 a_len -= nSubframe_Length;
1561 padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4 - 1));
1562 if (padding_len == 4) {
1566 if (a_len < padding_len) {
1569 pdata += padding_len;
1570 a_len -= padding_len;
1574 for (i = 0; i < nr_subframes; i++) {
1575 sub_skb = subframes[i];
1576 /* convert hdr + possible LLC headers into Ethernet header */
1577 eth_type = RTW_GET_BE16(&sub_skb->data[6]);
1578 if (sub_skb->len >= 8 &&
1579 ((!memcmp(sub_skb->data, rtw_rfc1042_header, SNAP_SIZE) &&
1580 eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
1581 !memcmp(sub_skb->data, rtw_bridge_tunnel_header, SNAP_SIZE))) {
1582 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1583 skb_pull(sub_skb, SNAP_SIZE);
1584 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
1585 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
1588 /* Leave Ethernet header part of hdr and full payload */
1589 len = htons(sub_skb->len);
1590 memcpy(skb_push(sub_skb, 2), &len, 2);
1591 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
1592 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
1595 /* Indicate the packets to upper layer */
1596 /* Insert NAT2.5 RX here! */
1597 sub_skb->protocol = eth_type_trans(sub_skb, padapter->pnetdev);
1598 sub_skb->dev = padapter->pnetdev;
1600 sub_skb->ip_summed = CHECKSUM_NONE;
1608 rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
1613 static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
1615 u8 wsize = preorder_ctrl->wsize_b;
1616 u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/* 4096; */
1618 /* Rx Reorder initialize condition. */
1619 if (preorder_ctrl->indicate_seq == 0xFFFF)
1620 preorder_ctrl->indicate_seq = seq_num;
1622 /* Drop out the packet which SeqNum is smaller than WinStart */
1623 if (SN_LESS(seq_num, preorder_ctrl->indicate_seq))
1627 /* Sliding window manipulation. Conditions includes: */
1628 /* 1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
1629 /* 2. Incoming SeqNum is larger than the WinEnd => Window shift N */
1631 if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
1632 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
1633 } else if (SN_LESS(wend, seq_num)) {
1634 if (seq_num >= (wsize - 1))
1635 preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
1637 preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
1643 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, struct recv_frame *prframe);
1644 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, struct recv_frame *prframe)
1646 struct rx_pkt_attrib *pattrib = &prframe->attrib;
1647 struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1648 struct list_head *phead, *plist;
1649 struct recv_frame *hdr;
1650 struct rx_pkt_attrib *pnextattrib;
1652 phead = get_list_head(ppending_recvframe_queue);
1653 plist = phead->next;
1655 while (phead != plist) {
1656 hdr = container_of(plist, struct recv_frame, list);
1657 pnextattrib = &hdr->attrib;
1659 if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
1660 plist = plist->next;
1661 else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
1667 list_del_init(&prframe->list);
1669 list_add_tail(&prframe->list, plist);
1673 static int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
1675 struct list_head *phead, *plist;
1676 struct recv_frame *prframe;
1677 struct rx_pkt_attrib *pattrib;
1678 int bPktInBuf = false;
1679 struct recv_priv *precvpriv = &padapter->recvpriv;
1680 struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1682 phead = get_list_head(ppending_recvframe_queue);
1683 plist = phead->next;
1685 /* Handling some condition for forced indicate case. */
1687 if (list_empty(phead))
1690 prframe = container_of(plist, struct recv_frame, list);
1691 pattrib = &prframe->attrib;
1692 preorder_ctrl->indicate_seq = pattrib->seq_num;
1695 /* Prepare indication list and indication. */
1696 /* Check if there is any packet need indicate. */
1697 while (!list_empty(phead)) {
1698 prframe = container_of(plist, struct recv_frame, list);
1699 pattrib = &prframe->attrib;
1701 if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
1702 plist = plist->next;
1703 list_del_init(&prframe->list);
1705 if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num))
1706 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
1708 /* Set this as a lock to make sure that only one thread is indicating packet. */
1710 /* indicate this recv_frame */
1711 if (!pattrib->amsdu) {
1712 if ((!padapter->bDriverStopped) &&
1713 (!padapter->bSurpriseRemoved))
1714 rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
1715 } else if (pattrib->amsdu == 1) {
1716 if (amsdu_to_msdu(padapter, prframe) != _SUCCESS)
1717 rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
1719 /* error condition; */
1722 /* Update local variables. */
1732 static int recv_indicatepkt_reorder(struct adapter *padapter, struct recv_frame *prframe)
1734 int retval = _SUCCESS;
1735 struct rx_pkt_attrib *pattrib = &prframe->attrib;
1736 struct recv_reorder_ctrl *preorder_ctrl = prframe->preorder_ctrl;
1737 struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1739 if (!pattrib->amsdu) {
1741 wlanhdr_to_ethhdr(prframe);
1743 if (pattrib->qos != 1) {
1744 if (!padapter->bDriverStopped &&
1745 !padapter->bSurpriseRemoved) {
1746 rtw_recv_indicatepkt(padapter, prframe);
1753 if (!preorder_ctrl->enable) {
1754 /* indicate this recv_frame */
1755 preorder_ctrl->indicate_seq = pattrib->seq_num;
1756 rtw_recv_indicatepkt(padapter, prframe);
1758 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) % 4096;
1761 } else if (pattrib->amsdu == 1) { /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
1762 if (!preorder_ctrl->enable) {
1763 preorder_ctrl->indicate_seq = pattrib->seq_num;
1764 retval = amsdu_to_msdu(padapter, prframe);
1766 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) % 4096;
1771 spin_lock_bh(&ppending_recvframe_queue->lock);
1773 /* s2. check if winstart_b(indicate_seq) needs to been updated */
1774 if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num))
1777 /* s3. Insert all packet into Reorder Queue to maintain its ordering. */
1778 if (!enqueue_reorder_recvframe(preorder_ctrl, prframe))
1782 /* Indication process. */
1783 /* After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
1784 /* with the SeqNum smaller than latest WinStart and buffer other packets. */
1786 /* For Rx Reorder condition: */
1787 /* 1. All packets with SeqNum smaller than WinStart => Indicate */
1788 /* 2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
1791 /* recv_indicatepkts_in_order(padapter, preorder_ctrl, true); */
1792 if (recv_indicatepkts_in_order(padapter, preorder_ctrl, false)) {
1793 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
1794 spin_unlock_bh(&ppending_recvframe_queue->lock);
1796 spin_unlock_bh(&ppending_recvframe_queue->lock);
1797 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
1804 spin_unlock_bh(&ppending_recvframe_queue->lock);
1809 void rtw_reordering_ctrl_timeout_handler(void *pcontext)
1811 struct recv_reorder_ctrl *preorder_ctrl = (struct recv_reorder_ctrl *)pcontext;
1812 struct adapter *padapter = preorder_ctrl->padapter;
1813 struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1815 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
1818 spin_lock_bh(&ppending_recvframe_queue->lock);
1820 if (recv_indicatepkts_in_order(padapter, preorder_ctrl, true))
1821 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
1823 spin_unlock_bh(&ppending_recvframe_queue->lock);
1826 static int process_recv_indicatepkts(struct adapter *padapter, struct recv_frame *prframe)
1828 int retval = _SUCCESS;
1829 /* struct recv_priv *precvpriv = &padapter->recvpriv; */
1830 /* struct rx_pkt_attrib *pattrib = &prframe->attrib; */
1831 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1832 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1834 if (phtpriv->ht_option) { /* B/G/N Mode */
1835 /* prframe->preorder_ctrl = &precvpriv->recvreorder_ctrl[pattrib->priority]; */
1837 if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) {
1838 /* including perform A-MPDU Rx Ordering Buffer Control */
1839 if ((!padapter->bDriverStopped) &&
1840 (!padapter->bSurpriseRemoved)) {
1845 } else { /* B/G mode */
1846 retval = wlanhdr_to_ethhdr(prframe);
1847 if (retval != _SUCCESS)
1850 if ((!padapter->bDriverStopped) &&
1851 (!padapter->bSurpriseRemoved)) {
1852 /* indicate this recv_frame */
1853 rtw_recv_indicatepkt(padapter, prframe);
1863 static int recv_func_prehandle(struct adapter *padapter, struct recv_frame *rframe)
1866 struct rx_pkt_attrib *pattrib = &rframe->attrib;
1867 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1868 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1870 if (padapter->registrypriv.mp_mode == 1) {
1871 if (pattrib->crc_err == 1)
1872 padapter->mppriv.rx_crcerrpktcount++;
1874 padapter->mppriv.rx_pktcount++;
1876 if (!check_fwstate(pmlmepriv, WIFI_MP_LPBK_STATE)) {
1878 rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
1883 /* check the frame crtl field and decache */
1884 ret = validate_recv_frame(padapter, rframe);
1885 if (ret != _SUCCESS) {
1886 rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
1894 static int recv_func_posthandle(struct adapter *padapter, struct recv_frame *prframe)
1897 struct recv_frame *orig_prframe = prframe;
1898 struct recv_priv *precvpriv = &padapter->recvpriv;
1899 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1902 rtw_led_control(padapter, LED_CTL_RX);
1904 prframe = decryptor(padapter, prframe);
1907 goto _recv_data_drop;
1910 prframe = recvframe_chk_defrag(padapter, prframe);
1912 goto _recv_data_drop;
1914 prframe = portctrl(padapter, prframe);
1917 goto _recv_data_drop;
1920 count_rx_stats(padapter, prframe, NULL);
1922 ret = process_recv_indicatepkts(padapter, prframe);
1923 if (ret != _SUCCESS) {
1924 rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
1925 goto _recv_data_drop;
1930 precvpriv->rx_drop++;
1934 static int recv_func(struct adapter *padapter, struct recv_frame *rframe)
1937 struct rx_pkt_attrib *prxattrib = &rframe->attrib;
1938 struct security_priv *psecuritypriv = &padapter->securitypriv;
1939 struct mlme_priv *mlmepriv = &padapter->mlmepriv;
1940 struct recv_priv *recvpriv = &padapter->recvpriv;
1942 /* check if need to handle uc_swdec_pending_queue*/
1943 if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
1944 psecuritypriv->busetkipkey) {
1945 struct recv_frame *pending_frame;
1948 pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue);
1949 while (pending_frame) {
1951 recv_func_posthandle(padapter, pending_frame);
1955 ret = recv_func_prehandle(padapter, rframe);
1957 if (ret == _SUCCESS) {
1958 /* check if need to enqueue into uc_swdec_pending_queue*/
1959 if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
1960 !IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
1961 (prxattrib->bdecrypted == 0 || psecuritypriv->sw_decrypt) &&
1962 psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPAPSK &&
1963 !psecuritypriv->busetkipkey) {
1964 rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
1965 DBG_88E("%s: no key, enqueue uc_swdec_pending_queue\n", __func__);
1966 if (recvpriv->free_recvframe_cnt < NR_RECVFRAME / 4) {
1967 /* to prevent from recvframe starvation,
1968 * get recvframe from uc_swdec_pending_queue to
1969 * free_recvframe_cnt */
1970 rframe = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue);
1977 ret = recv_func_posthandle(padapter, rframe);
1984 s32 rtw_recv_entry(struct recv_frame *precvframe)
1986 struct adapter *padapter;
1987 struct recv_priv *precvpriv;
1990 padapter = precvframe->adapter;
1992 precvpriv = &padapter->recvpriv;
1994 ret = recv_func(padapter, precvframe);
1996 goto _recv_entry_drop;
1998 precvpriv->rx_pkts++;
2004 if (padapter->registrypriv.mp_mode == 1)
2005 padapter->mppriv.rx_pktloss = precvpriv->rx_drop;
2010 void rtw_signal_stat_timer_hdl(struct timer_list *t)
2012 struct adapter *adapter = from_timer(adapter, t, recvpriv.signal_stat_timer);
2013 struct recv_priv *recvpriv = &adapter->recvpriv;
2016 u8 avg_signal_strength = 0;
2017 u8 avg_signal_qual = 0;
2018 u8 _alpha = 3; /* this value is based on converging_constant = 5000 and sampling_interval = 1000 */
2020 if (adapter->recvpriv.is_signal_dbg) {
2021 /* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
2022 adapter->recvpriv.signal_strength = adapter->recvpriv.signal_strength_dbg;
2023 adapter->recvpriv.rssi = (s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
2025 if (recvpriv->signal_strength_data.update_req == 0) {/* update_req is clear, means we got rx */
2026 avg_signal_strength = recvpriv->signal_strength_data.avg_val;
2027 /* after avg_vals are accquired, we can re-stat the signal values */
2028 recvpriv->signal_strength_data.update_req = 1;
2031 if (recvpriv->signal_qual_data.update_req == 0) {/* update_req is clear, means we got rx */
2032 avg_signal_qual = recvpriv->signal_qual_data.avg_val;
2033 /* after avg_vals are accquired, we can re-stat the signal values */
2034 recvpriv->signal_qual_data.update_req = 1;
2037 /* update value of signal_strength, rssi, signal_qual */
2038 if (!check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY)) {
2039 tmp_s = (avg_signal_strength + (_alpha - 1) * recvpriv->signal_strength);
2041 tmp_s = tmp_s / _alpha + 1;
2043 tmp_s = tmp_s / _alpha;
2047 tmp_q = (avg_signal_qual + (_alpha - 1) * recvpriv->signal_qual);
2049 tmp_q = tmp_q / _alpha + 1;
2051 tmp_q = tmp_q / _alpha;
2055 recvpriv->signal_strength = tmp_s;
2056 recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
2057 recvpriv->signal_qual = tmp_q;
2060 rtw_set_signal_stat_timer(recvpriv);