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