1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 #include <rtw_debug.h>
9 #include <linux/jiffies.h>
10 #include <net/cfg80211.h>
11 #include <asm/unaligned.h>
12 
rtw_os_free_recvframe(union recv_frame * precvframe)13 void rtw_os_free_recvframe(union recv_frame *precvframe)
14 {
15 	if (precvframe->u.hdr.pkt) {
16 		dev_kfree_skb_any(precvframe->u.hdr.pkt);/* free skb by driver */
17 
18 		precvframe->u.hdr.pkt = NULL;
19 	}
20 }
21 
22 /* alloc os related resource in union recv_frame */
rtw_os_recv_resource_alloc(struct adapter * padapter,union recv_frame * precvframe)23 void rtw_os_recv_resource_alloc(struct adapter *padapter, union recv_frame *precvframe)
24 {
25 	precvframe->u.hdr.pkt_newalloc = precvframe->u.hdr.pkt = NULL;
26 }
27 
28 /* free os related resource in union recv_frame */
rtw_os_recv_resource_free(struct recv_priv * precvpriv)29 void rtw_os_recv_resource_free(struct recv_priv *precvpriv)
30 {
31 	signed int i;
32 	union recv_frame *precvframe;
33 
34 	precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
35 
36 	for (i = 0; i < NR_RECVFRAME; i++) {
37 		if (precvframe->u.hdr.pkt) {
38 			/* free skb by driver */
39 			dev_kfree_skb_any(precvframe->u.hdr.pkt);
40 			precvframe->u.hdr.pkt = NULL;
41 		}
42 		precvframe++;
43 	}
44 }
45 
46 /* free os related resource in struct recv_buf */
rtw_os_recvbuf_resource_free(struct adapter * padapter,struct recv_buf * precvbuf)47 void rtw_os_recvbuf_resource_free(struct adapter *padapter, struct recv_buf *precvbuf)
48 {
49 	if (precvbuf->pskb) {
50 		dev_kfree_skb_any(precvbuf->pskb);
51 	}
52 }
53 
rtw_os_alloc_msdu_pkt(union recv_frame * prframe,u16 nSubframe_Length,u8 * pdata)54 struct sk_buff *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, u16 nSubframe_Length, u8 *pdata)
55 {
56 	u16 eth_type;
57 	struct sk_buff *sub_skb;
58 	struct rx_pkt_attrib *pattrib;
59 
60 	pattrib = &prframe->u.hdr.attrib;
61 
62 	sub_skb = rtw_skb_alloc(nSubframe_Length + 12);
63 	if (!sub_skb)
64 		return NULL;
65 
66 	skb_reserve(sub_skb, 12);
67 	skb_put_data(sub_skb, (pdata + ETH_HLEN), nSubframe_Length);
68 
69 	eth_type = get_unaligned_be16(&sub_skb->data[6]);
70 
71 	if (sub_skb->len >= 8 &&
72 		((!memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) &&
73 		  eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
74 		 !memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE))) {
75 		/*
76 		 * remove RFC1042 or Bridge-Tunnel encapsulation and replace
77 		 * EtherType
78 		 */
79 		skb_pull(sub_skb, SNAP_SIZE);
80 		memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
81 		memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
82 	} else {
83 		__be16 len;
84 		/* Leave Ethernet header part of hdr and full payload */
85 		len = htons(sub_skb->len);
86 		memcpy(skb_push(sub_skb, 2), &len, 2);
87 		memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
88 		memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
89 	}
90 
91 	return sub_skb;
92 }
93 
rtw_os_recv_indicate_pkt(struct adapter * padapter,struct sk_buff * pkt,struct rx_pkt_attrib * pattrib)94 void rtw_os_recv_indicate_pkt(struct adapter *padapter, struct sk_buff *pkt, struct rx_pkt_attrib *pattrib)
95 {
96 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
97 
98 	/* Indicate the packets to upper layer */
99 	if (pkt) {
100 		if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
101 			struct sk_buff *pskb2 = NULL;
102 			struct sta_info *psta = NULL;
103 			struct sta_priv *pstapriv = &padapter->stapriv;
104 			int bmcast = is_multicast_ether_addr(pattrib->dst);
105 
106 			if (memcmp(pattrib->dst, myid(&padapter->eeprompriv), ETH_ALEN)) {
107 				if (bmcast) {
108 					psta = rtw_get_bcmc_stainfo(padapter);
109 					pskb2 = skb_clone(pkt, GFP_ATOMIC);
110 				} else {
111 					psta = rtw_get_stainfo(pstapriv, pattrib->dst);
112 				}
113 
114 				if (psta) {
115 					struct net_device *pnetdev = (struct net_device *)padapter->pnetdev;
116 					/* skb->ip_summed = CHECKSUM_NONE; */
117 					pkt->dev = pnetdev;
118 					skb_set_queue_mapping(pkt, rtw_recv_select_queue(pkt));
119 
120 					_rtw_xmit_entry(pkt, pnetdev);
121 
122 					if (bmcast && pskb2)
123 						pkt = pskb2;
124 					else
125 						return;
126 				}
127 			} else {
128 				/*  to APself */
129 			}
130 		}
131 
132 		pkt->protocol = eth_type_trans(pkt, padapter->pnetdev);
133 		pkt->dev = padapter->pnetdev;
134 
135 		pkt->ip_summed = CHECKSUM_NONE;
136 
137 		rtw_netif_rx(padapter->pnetdev, pkt);
138 	}
139 }
140 
rtw_handle_tkip_mic_err(struct adapter * padapter,u8 bgroup)141 void rtw_handle_tkip_mic_err(struct adapter *padapter, u8 bgroup)
142 {
143 	enum nl80211_key_type key_type = 0;
144 	union iwreq_data wrqu;
145 	struct iw_michaelmicfailure    ev;
146 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
147 	struct security_priv *psecuritypriv = &padapter->securitypriv;
148 	unsigned long cur_time = 0;
149 
150 	if (psecuritypriv->last_mic_err_time == 0) {
151 		psecuritypriv->last_mic_err_time = jiffies;
152 	} else {
153 		cur_time = jiffies;
154 
155 		if (cur_time - psecuritypriv->last_mic_err_time < 60*HZ) {
156 			psecuritypriv->btkip_countermeasure = true;
157 			psecuritypriv->last_mic_err_time = 0;
158 			psecuritypriv->btkip_countermeasure_time = cur_time;
159 		} else {
160 			psecuritypriv->last_mic_err_time = jiffies;
161 		}
162 	}
163 
164 	if (bgroup) {
165 		key_type |= NL80211_KEYTYPE_GROUP;
166 	} else {
167 		key_type |= NL80211_KEYTYPE_PAIRWISE;
168 	}
169 
170 	cfg80211_michael_mic_failure(padapter->pnetdev, (u8 *)&pmlmepriv->assoc_bssid[0], key_type, -1,
171 		NULL, GFP_ATOMIC);
172 
173 	memset(&ev, 0x00, sizeof(ev));
174 	if (bgroup) {
175 		ev.flags |= IW_MICFAILURE_GROUP;
176 	} else {
177 		ev.flags |= IW_MICFAILURE_PAIRWISE;
178 	}
179 
180 	ev.src_addr.sa_family = ARPHRD_ETHER;
181 	memcpy(ev.src_addr.sa_data, &pmlmepriv->assoc_bssid[0], ETH_ALEN);
182 
183 	memset(&wrqu, 0x00, sizeof(wrqu));
184 	wrqu.data.length = sizeof(ev);
185 }
186 
rtw_recv_indicatepkt(struct adapter * padapter,union recv_frame * precv_frame)187 int rtw_recv_indicatepkt(struct adapter *padapter, union recv_frame *precv_frame)
188 {
189 	struct recv_priv *precvpriv;
190 	struct __queue	*pfree_recv_queue;
191 	struct sk_buff *skb;
192 	struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
193 
194 	precvpriv = &(padapter->recvpriv);
195 	pfree_recv_queue = &(precvpriv->free_recv_queue);
196 
197 	skb = precv_frame->u.hdr.pkt;
198 	if (!skb)
199 		goto _recv_indicatepkt_drop;
200 
201 	skb->data = precv_frame->u.hdr.rx_data;
202 
203 	skb_set_tail_pointer(skb, precv_frame->u.hdr.len);
204 
205 	skb->len = precv_frame->u.hdr.len;
206 
207 	rtw_os_recv_indicate_pkt(padapter, skb, pattrib);
208 
209 	/* pointers to NULL before rtw_free_recvframe() */
210 	precv_frame->u.hdr.pkt = NULL;
211 
212 	rtw_free_recvframe(precv_frame, pfree_recv_queue);
213 
214 	return _SUCCESS;
215 
216 _recv_indicatepkt_drop:
217 
218 	/* enqueue back to free_recv_queue */
219 	rtw_free_recvframe(precv_frame, pfree_recv_queue);
220 
221 	return _FAIL;
222 }
223 
rtw_init_recv_timer(struct recv_reorder_ctrl * preorder_ctrl)224 void rtw_init_recv_timer(struct recv_reorder_ctrl *preorder_ctrl)
225 {
226 	timer_setup(&preorder_ctrl->reordering_ctrl_timer,
227 		    rtw_reordering_ctrl_timeout_handler, 0);
228 
229 }
230