1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3 *
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 *
6 ******************************************************************************/
7 #define _RECV_OSDEP_C_
8
9 #include <drv_types.h>
10 #include <rtw_debug.h>
11 #include <linux/jiffies.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 {
17 dev_kfree_skb_any(precvframe->u.hdr.pkt);/* free skb by driver */
18
19 precvframe->u.hdr.pkt = NULL;
20 }
21 }
22
23 /* alloc os related resource in union recv_frame */
rtw_os_recv_resource_alloc(struct adapter * padapter,union recv_frame * precvframe)24 int rtw_os_recv_resource_alloc(struct adapter *padapter, union recv_frame *precvframe)
25 {
26 int res = _SUCCESS;
27
28 precvframe->u.hdr.pkt_newalloc = precvframe->u.hdr.pkt = NULL;
29
30 return res;
31 }
32
33 /* free os related resource in union recv_frame */
rtw_os_recv_resource_free(struct recv_priv * precvpriv)34 void rtw_os_recv_resource_free(struct recv_priv *precvpriv)
35 {
36 sint i;
37 union recv_frame *precvframe;
38
39 precvframe = (union recv_frame*) precvpriv->precv_frame_buf;
40
41 for (i = 0; i < NR_RECVFRAME; i++)
42 {
43 if (precvframe->u.hdr.pkt)
44 {
45 dev_kfree_skb_any(precvframe->u.hdr.pkt);/* free skb by driver */
46 precvframe->u.hdr.pkt = NULL;
47 }
48 precvframe++;
49 }
50 }
51
52 /* free os related resource in struct recv_buf */
rtw_os_recvbuf_resource_free(struct adapter * padapter,struct recv_buf * precvbuf)53 int rtw_os_recvbuf_resource_free(struct adapter *padapter, struct recv_buf *precvbuf)
54 {
55 int ret = _SUCCESS;
56
57 if (precvbuf->pskb)
58 {
59 dev_kfree_skb_any(precvbuf->pskb);
60 }
61 return ret;
62
63 }
64
rtw_os_alloc_msdu_pkt(union recv_frame * prframe,u16 nSubframe_Length,u8 * pdata)65 _pkt *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, u16 nSubframe_Length, u8 *pdata)
66 {
67 u16 eth_type;
68 _pkt *sub_skb;
69 struct rx_pkt_attrib *pattrib;
70
71 pattrib = &prframe->u.hdr.attrib;
72
73 sub_skb = rtw_skb_alloc(nSubframe_Length + 12);
74 if (sub_skb)
75 {
76 skb_reserve(sub_skb, 12);
77 skb_put_data(sub_skb, (pdata + ETH_HLEN), nSubframe_Length);
78 }
79 else
80 {
81 sub_skb = rtw_skb_clone(prframe->u.hdr.pkt);
82 if (sub_skb)
83 {
84 sub_skb->data = pdata + ETH_HLEN;
85 sub_skb->len = nSubframe_Length;
86 skb_set_tail_pointer(sub_skb, nSubframe_Length);
87 }
88 else
89 {
90 DBG_871X("%s(): rtw_skb_clone() Fail!!!\n", __func__);
91 return NULL;
92 }
93 }
94
95 eth_type = RTW_GET_BE16(&sub_skb->data[6]);
96
97 if (sub_skb->len >= 8 &&
98 ((!memcmp(sub_skb->data, rtw_rfc1042_header, SNAP_SIZE) &&
99 eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
100 !memcmp(sub_skb->data, rtw_bridge_tunnel_header, SNAP_SIZE))) {
101 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
102 skb_pull(sub_skb, SNAP_SIZE);
103 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
104 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
105 } else {
106 __be16 len;
107 /* Leave Ethernet header part of hdr and full payload */
108 len = htons(sub_skb->len);
109 memcpy(skb_push(sub_skb, 2), &len, 2);
110 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
111 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
112 }
113
114 return sub_skb;
115 }
116
rtw_os_recv_indicate_pkt(struct adapter * padapter,_pkt * pkt,struct rx_pkt_attrib * pattrib)117 void rtw_os_recv_indicate_pkt(struct adapter *padapter, _pkt *pkt, struct rx_pkt_attrib *pattrib)
118 {
119 struct mlme_priv*pmlmepriv = &padapter->mlmepriv;
120 int ret;
121
122 /* Indicat the packets to upper layer */
123 if (pkt) {
124 if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
125 {
126 _pkt *pskb2 = NULL;
127 struct sta_info *psta = NULL;
128 struct sta_priv *pstapriv = &padapter->stapriv;
129 int bmcast = IS_MCAST(pattrib->dst);
130
131 /* DBG_871X("bmcast =%d\n", bmcast); */
132
133 if (memcmp(pattrib->dst, myid(&padapter->eeprompriv), ETH_ALEN))
134 {
135 /* DBG_871X("not ap psta =%p, addr =%pM\n", psta, pattrib->dst); */
136
137 if (bmcast)
138 {
139 psta = rtw_get_bcmc_stainfo(padapter);
140 pskb2 = rtw_skb_clone(pkt);
141 } else {
142 psta = rtw_get_stainfo(pstapriv, pattrib->dst);
143 }
144
145 if (psta)
146 {
147 struct net_device *pnetdev = (struct net_device*)padapter->pnetdev;
148
149 /* DBG_871X("directly forwarding to the rtw_xmit_entry\n"); */
150
151 /* skb->ip_summed = CHECKSUM_NONE; */
152 pkt->dev = pnetdev;
153 skb_set_queue_mapping(pkt, rtw_recv_select_queue(pkt));
154
155 _rtw_xmit_entry(pkt, pnetdev);
156
157 if (bmcast && (pskb2 != NULL)) {
158 pkt = pskb2;
159 DBG_COUNTER(padapter->rx_logs.os_indicate_ap_mcast);
160 } else {
161 DBG_COUNTER(padapter->rx_logs.os_indicate_ap_forward);
162 return;
163 }
164 }
165 }
166 else/* to APself */
167 {
168 /* DBG_871X("to APSelf\n"); */
169 DBG_COUNTER(padapter->rx_logs.os_indicate_ap_self);
170 }
171 }
172
173 pkt->protocol = eth_type_trans(pkt, padapter->pnetdev);
174 pkt->dev = padapter->pnetdev;
175
176 #ifdef CONFIG_TCP_CSUM_OFFLOAD_RX
177 if ((pattrib->tcpchk_valid == 1) && (pattrib->tcp_chkrpt == 1))
178 pkt->ip_summed = CHECKSUM_UNNECESSARY;
179 else
180 pkt->ip_summed = CHECKSUM_NONE;
181
182 #else /* !CONFIG_TCP_CSUM_OFFLOAD_RX */
183 pkt->ip_summed = CHECKSUM_NONE;
184 #endif /* CONFIG_TCP_CSUM_OFFLOAD_RX */
185
186 ret = rtw_netif_rx(padapter->pnetdev, pkt);
187 if (ret == NET_RX_SUCCESS)
188 DBG_COUNTER(padapter->rx_logs.os_netif_ok);
189 else
190 DBG_COUNTER(padapter->rx_logs.os_netif_err);
191 }
192 }
193
rtw_handle_tkip_mic_err(struct adapter * padapter,u8 bgroup)194 void rtw_handle_tkip_mic_err(struct adapter *padapter, u8 bgroup)
195 {
196 enum nl80211_key_type key_type = 0;
197 union iwreq_data wrqu;
198 struct iw_michaelmicfailure ev;
199 struct mlme_priv* pmlmepriv = &padapter->mlmepriv;
200 struct security_priv *psecuritypriv = &padapter->securitypriv;
201 unsigned long cur_time = 0;
202
203 if (psecuritypriv->last_mic_err_time == 0)
204 {
205 psecuritypriv->last_mic_err_time = jiffies;
206 }
207 else
208 {
209 cur_time = jiffies;
210
211 if (cur_time - psecuritypriv->last_mic_err_time < 60*HZ)
212 {
213 psecuritypriv->btkip_countermeasure = true;
214 psecuritypriv->last_mic_err_time = 0;
215 psecuritypriv->btkip_countermeasure_time = cur_time;
216 }
217 else
218 {
219 psecuritypriv->last_mic_err_time = jiffies;
220 }
221 }
222
223 if (bgroup)
224 {
225 key_type |= NL80211_KEYTYPE_GROUP;
226 }
227 else
228 {
229 key_type |= NL80211_KEYTYPE_PAIRWISE;
230 }
231
232 cfg80211_michael_mic_failure(padapter->pnetdev, (u8 *)&pmlmepriv->assoc_bssid[ 0 ], key_type, -1,
233 NULL, GFP_ATOMIC);
234
235 memset(&ev, 0x00, sizeof(ev));
236 if (bgroup)
237 {
238 ev.flags |= IW_MICFAILURE_GROUP;
239 }
240 else
241 {
242 ev.flags |= IW_MICFAILURE_PAIRWISE;
243 }
244
245 ev.src_addr.sa_family = ARPHRD_ETHER;
246 memcpy(ev.src_addr.sa_data, &pmlmepriv->assoc_bssid[ 0 ], ETH_ALEN);
247
248 memset(&wrqu, 0x00, sizeof(wrqu));
249 wrqu.data.length = sizeof(ev);
250 }
251
252 #ifdef CONFIG_AUTO_AP_MODE
rtw_os_ksocket_send(struct adapter * padapter,union recv_frame * precv_frame)253 static void rtw_os_ksocket_send(struct adapter *padapter, union recv_frame *precv_frame)
254 {
255 _pkt *skb = precv_frame->u.hdr.pkt;
256 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
257 struct sta_info *psta = precv_frame->u.hdr.psta;
258
259 DBG_871X("eth rx: got eth_type = 0x%x\n", pattrib->eth_type);
260
261 if (psta && psta->isrc && psta->pid>0)
262 {
263 u16 rx_pid;
264
265 rx_pid = *(u16*)(skb->data+ETH_HLEN);
266
267 DBG_871X("eth rx(pid = 0x%x): sta("MAC_FMT") pid = 0x%x\n",
268 rx_pid, MAC_ARG(psta->hwaddr), psta->pid);
269
270 if (rx_pid == psta->pid)
271 {
272 int i;
273 u16 len = *(u16*)(skb->data+ETH_HLEN+2);
274 /* u16 ctrl_type = *(u16*)(skb->data+ETH_HLEN+4); */
275
276 /* DBG_871X("eth, RC: len = 0x%x, ctrl_type = 0x%x\n", len, ctrl_type); */
277 DBG_871X("eth, RC: len = 0x%x\n", len);
278
279 for (i = 0;i<len;i++)
280 DBG_871X("0x%x\n", *(skb->data+ETH_HLEN+4+i));
281 /* DBG_871X("0x%x\n", *(skb->data+ETH_HLEN+6+i)); */
282
283 DBG_871X("eth, RC-end\n");
284 }
285
286 }
287
288 }
289 #endif /* CONFIG_AUTO_AP_MODE */
290
rtw_recv_indicatepkt(struct adapter * padapter,union recv_frame * precv_frame)291 int rtw_recv_indicatepkt(struct adapter *padapter, union recv_frame *precv_frame)
292 {
293 struct recv_priv *precvpriv;
294 struct __queue *pfree_recv_queue;
295 _pkt *skb;
296 struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
297
298 DBG_COUNTER(padapter->rx_logs.os_indicate);
299
300 precvpriv = &(padapter->recvpriv);
301 pfree_recv_queue = &(precvpriv->free_recv_queue);
302
303 skb = precv_frame->u.hdr.pkt;
304 if (skb == NULL)
305 {
306 RT_TRACE(_module_recv_osdep_c_, _drv_err_, ("rtw_recv_indicatepkt():skb == NULL something wrong!!!!\n"));
307 goto _recv_indicatepkt_drop;
308 }
309
310 RT_TRACE(_module_recv_osdep_c_, _drv_info_, ("rtw_recv_indicatepkt():skb != NULL !!!\n"));
311 RT_TRACE(_module_recv_osdep_c_, _drv_info_, ("rtw_recv_indicatepkt():precv_frame->u.hdr.rx_head =%p precv_frame->hdr.rx_data =%p\n", precv_frame->u.hdr.rx_head, precv_frame->u.hdr.rx_data));
312 RT_TRACE(_module_recv_osdep_c_, _drv_info_, ("precv_frame->hdr.rx_tail =%p precv_frame->u.hdr.rx_end =%p precv_frame->hdr.len =%d\n", precv_frame->u.hdr.rx_tail, precv_frame->u.hdr.rx_end, precv_frame->u.hdr.len));
313
314 skb->data = precv_frame->u.hdr.rx_data;
315
316 skb_set_tail_pointer(skb, precv_frame->u.hdr.len);
317
318 skb->len = precv_frame->u.hdr.len;
319
320 RT_TRACE(_module_recv_osdep_c_, _drv_info_, ("\n skb->head =%p skb->data =%p skb->tail =%p skb->end =%p skb->len =%d\n", skb->head, skb->data, skb_tail_pointer(skb), skb_end_pointer(skb), skb->len));
321
322 #ifdef CONFIG_AUTO_AP_MODE
323 if (0x8899 == pattrib->eth_type)
324 {
325 rtw_os_ksocket_send(padapter, precv_frame);
326
327 /* goto _recv_indicatepkt_drop; */
328 }
329 #endif /* CONFIG_AUTO_AP_MODE */
330
331 rtw_os_recv_indicate_pkt(padapter, skb, pattrib);
332
333 precv_frame->u.hdr.pkt = NULL; /* pointers to NULL before rtw_free_recvframe() */
334
335 rtw_free_recvframe(precv_frame, pfree_recv_queue);
336
337 RT_TRACE(_module_recv_osdep_c_, _drv_info_, ("\n rtw_recv_indicatepkt :after rtw_os_recv_indicate_pkt!!!!\n"));
338
339 return _SUCCESS;
340
341 _recv_indicatepkt_drop:
342
343 /* enqueue back to free_recv_queue */
344 rtw_free_recvframe(precv_frame, pfree_recv_queue);
345
346 DBG_COUNTER(padapter->rx_logs.os_indicate_err);
347 return _FAIL;
348 }
349
rtw_init_recv_timer(struct recv_reorder_ctrl * preorder_ctrl)350 void rtw_init_recv_timer(struct recv_reorder_ctrl *preorder_ctrl)
351 {
352 timer_setup(&preorder_ctrl->reordering_ctrl_timer,
353 rtw_reordering_ctrl_timeout_handler, 0);
354
355 }
356