xref: /linux/drivers/net/wireless/marvell/mwifiex/sta_rx.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP Wireless LAN device driver: station RX data handling
4  *
5  * Copyright 2011-2020 NXP
6  */
7 
8 #include <uapi/linux/ipv6.h>
9 #include <net/ndisc.h>
10 #include "decl.h"
11 #include "ioctl.h"
12 #include "util.h"
13 #include "fw.h"
14 #include "main.h"
15 #include "11n_aggr.h"
16 #include "11n_rxreorder.h"
17 
18 /* This function checks if a frame is IPv4 ARP or IPv6 Neighbour advertisement
19  * frame. If frame has both source and destination mac address as same, this
20  * function drops such gratuitous frames.
21  */
22 static bool
23 mwifiex_discard_gratuitous_arp(struct mwifiex_private *priv,
24 			       struct sk_buff *skb)
25 {
26 	const struct mwifiex_arp_eth_header *arp;
27 	struct ethhdr *eth;
28 	struct ipv6hdr *ipv6;
29 	struct icmp6hdr *icmpv6;
30 
31 	eth = (struct ethhdr *)skb->data;
32 	switch (ntohs(eth->h_proto)) {
33 	case ETH_P_ARP:
34 		arp = (void *)(skb->data + sizeof(struct ethhdr));
35 		if (arp->hdr.ar_op == htons(ARPOP_REPLY) ||
36 		    arp->hdr.ar_op == htons(ARPOP_REQUEST)) {
37 			if (!memcmp(arp->ar_sip, arp->ar_tip, 4))
38 				return true;
39 		}
40 		break;
41 	case ETH_P_IPV6:
42 		ipv6 = (void *)(skb->data + sizeof(struct ethhdr));
43 		icmpv6 = (void *)(skb->data + sizeof(struct ethhdr) +
44 				  sizeof(struct ipv6hdr));
45 		if (NDISC_NEIGHBOUR_ADVERTISEMENT == icmpv6->icmp6_type) {
46 			if (!memcmp(&ipv6->saddr, &ipv6->daddr,
47 				    sizeof(struct in6_addr)))
48 				return true;
49 		}
50 		break;
51 	default:
52 		break;
53 	}
54 
55 	return false;
56 }
57 
58 /*
59  * This function processes the received packet and forwards it
60  * to kernel/upper layer.
61  *
62  * This function parses through the received packet and determines
63  * if it is a debug packet or normal packet.
64  *
65  * For non-debug packets, the function chops off unnecessary leading
66  * header bytes, reconstructs the packet as an ethernet frame or
67  * 802.2/llc/snap frame as required, and sends it to kernel/upper layer.
68  *
69  * The completion callback is called after processing in complete.
70  */
71 int mwifiex_process_rx_packet(struct mwifiex_private *priv,
72 			      struct sk_buff *skb)
73 {
74 	int ret;
75 	struct rx_packet_hdr *rx_pkt_hdr;
76 	struct rxpd *local_rx_pd;
77 	int hdr_chop;
78 	struct ethhdr *eth;
79 	u16 rx_pkt_off, rx_pkt_len;
80 	u8 *offset;
81 	u8 adj_rx_rate = 0;
82 
83 	local_rx_pd = (struct rxpd *) (skb->data);
84 
85 	rx_pkt_off = le16_to_cpu(local_rx_pd->rx_pkt_offset);
86 	rx_pkt_len = le16_to_cpu(local_rx_pd->rx_pkt_length);
87 	rx_pkt_hdr = (void *)local_rx_pd + rx_pkt_off;
88 
89 	if (sizeof(rx_pkt_hdr->eth803_hdr) + sizeof(rfc1042_header) +
90 	    rx_pkt_off > skb->len) {
91 		mwifiex_dbg(priv->adapter, ERROR,
92 			    "wrong rx packet offset: len=%d, rx_pkt_off=%d\n",
93 			    skb->len, rx_pkt_off);
94 		priv->stats.rx_dropped++;
95 		dev_kfree_skb_any(skb);
96 		return -1;
97 	}
98 
99 	if (sizeof(*rx_pkt_hdr) + rx_pkt_off <= skb->len &&
100 	    ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header,
101 		      sizeof(bridge_tunnel_header))) ||
102 	     (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header,
103 		      sizeof(rfc1042_header)) &&
104 	      ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_AARP &&
105 	      ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_IPX))) {
106 		/*
107 		 *  Replace the 803 header and rfc1042 header (llc/snap) with an
108 		 *    EthernetII header, keep the src/dst and snap_type
109 		 *    (ethertype).
110 		 *  The firmware only passes up SNAP frames converting
111 		 *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
112 		 *  To create the Ethernet II, just move the src, dst address
113 		 *    right before the snap_type.
114 		 */
115 		eth = (struct ethhdr *)
116 			((u8 *) &rx_pkt_hdr->eth803_hdr
117 			 + sizeof(rx_pkt_hdr->eth803_hdr) +
118 			 sizeof(rx_pkt_hdr->rfc1042_hdr)
119 			 - sizeof(rx_pkt_hdr->eth803_hdr.h_dest)
120 			 - sizeof(rx_pkt_hdr->eth803_hdr.h_source)
121 			 - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type));
122 
123 		memcpy(eth->h_source, rx_pkt_hdr->eth803_hdr.h_source,
124 		       sizeof(eth->h_source));
125 		memcpy(eth->h_dest, rx_pkt_hdr->eth803_hdr.h_dest,
126 		       sizeof(eth->h_dest));
127 
128 		/* Chop off the rxpd + the excess memory from the 802.2/llc/snap
129 		   header that was removed. */
130 		hdr_chop = (u8 *) eth - (u8 *) local_rx_pd;
131 	} else {
132 		/* Chop off the rxpd */
133 		hdr_chop = (u8 *) &rx_pkt_hdr->eth803_hdr -
134 			(u8 *) local_rx_pd;
135 	}
136 
137 	/* Chop off the leading header bytes so the it points to the start of
138 	   either the reconstructed EthII frame or the 802.2/llc/snap frame */
139 	skb_pull(skb, hdr_chop);
140 
141 	if (priv->hs2_enabled &&
142 	    mwifiex_discard_gratuitous_arp(priv, skb)) {
143 		mwifiex_dbg(priv->adapter, INFO, "Bypassed Gratuitous ARP\n");
144 		dev_kfree_skb_any(skb);
145 		return 0;
146 	}
147 
148 	if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
149 	    ntohs(rx_pkt_hdr->eth803_hdr.h_proto) == ETH_P_TDLS) {
150 		offset = (u8 *)local_rx_pd + rx_pkt_off;
151 		mwifiex_process_tdls_action_frame(priv, offset, rx_pkt_len);
152 	}
153 
154 	/* Only stash RX bitrate for unicast packets. */
155 	if (likely(!is_multicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest))) {
156 		priv->rxpd_rate = local_rx_pd->rx_rate;
157 		priv->rxpd_htinfo = local_rx_pd->ht_info;
158 	}
159 
160 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
161 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
162 		adj_rx_rate = mwifiex_adjust_data_rate(priv,
163 						       local_rx_pd->rx_rate,
164 						       local_rx_pd->ht_info);
165 		mwifiex_hist_data_add(priv, adj_rx_rate, local_rx_pd->snr,
166 				      local_rx_pd->nf);
167 	}
168 
169 	ret = mwifiex_recv_packet(priv, skb);
170 	if (ret == -1)
171 		mwifiex_dbg(priv->adapter, ERROR,
172 			    "recv packet failed\n");
173 
174 	return ret;
175 }
176 
177 /*
178  * This function processes the received buffer.
179  *
180  * The function looks into the RxPD and performs sanity tests on the
181  * received buffer to ensure its a valid packet, before processing it
182  * further. If the packet is determined to be aggregated, it is
183  * de-aggregated accordingly. Non-unicast packets are sent directly to
184  * the kernel/upper layers. Unicast packets are handed over to the
185  * Rx reordering routine if 11n is enabled.
186  *
187  * The completion callback is called after processing in complete.
188  */
189 int mwifiex_process_sta_rx_packet(struct mwifiex_private *priv,
190 				  struct sk_buff *skb)
191 {
192 	struct mwifiex_adapter *adapter = priv->adapter;
193 	int ret = 0;
194 	struct rxpd *local_rx_pd;
195 	struct rx_packet_hdr *rx_pkt_hdr;
196 	u8 ta[ETH_ALEN];
197 	u16 rx_pkt_type, rx_pkt_offset, rx_pkt_length, seq_num;
198 	struct mwifiex_sta_node *sta_ptr;
199 
200 	local_rx_pd = (struct rxpd *) (skb->data);
201 	rx_pkt_type = le16_to_cpu(local_rx_pd->rx_pkt_type);
202 	rx_pkt_offset = le16_to_cpu(local_rx_pd->rx_pkt_offset);
203 	rx_pkt_length = le16_to_cpu(local_rx_pd->rx_pkt_length);
204 	seq_num = le16_to_cpu(local_rx_pd->seq_num);
205 
206 	rx_pkt_hdr = (void *)local_rx_pd + rx_pkt_offset;
207 
208 	if ((rx_pkt_offset + rx_pkt_length) > skb->len ||
209 	    sizeof(rx_pkt_hdr->eth803_hdr) + rx_pkt_offset > skb->len) {
210 		mwifiex_dbg(adapter, ERROR,
211 			    "wrong rx packet: len=%d, rx_pkt_offset=%d, rx_pkt_length=%d\n",
212 			    skb->len, rx_pkt_offset, rx_pkt_length);
213 		priv->stats.rx_dropped++;
214 		dev_kfree_skb_any(skb);
215 		return ret;
216 	}
217 
218 	if (rx_pkt_type == PKT_TYPE_MGMT) {
219 		ret = mwifiex_process_mgmt_packet(priv, skb);
220 		if (ret)
221 			mwifiex_dbg(adapter, DATA, "Rx of mgmt packet failed");
222 		dev_kfree_skb_any(skb);
223 		return ret;
224 	}
225 
226 	/*
227 	 * If the packet is not an unicast packet then send the packet
228 	 * directly to os. Don't pass thru rx reordering
229 	 */
230 	if ((!IS_11N_ENABLED(priv) &&
231 	     !(ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
232 	       !(local_rx_pd->flags & MWIFIEX_RXPD_FLAGS_TDLS_PACKET))) ||
233 	    !ether_addr_equal_unaligned(priv->curr_addr, rx_pkt_hdr->eth803_hdr.h_dest)) {
234 		mwifiex_process_rx_packet(priv, skb);
235 		return ret;
236 	}
237 
238 	if (mwifiex_queuing_ra_based(priv) ||
239 	    (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
240 	     local_rx_pd->flags & MWIFIEX_RXPD_FLAGS_TDLS_PACKET)) {
241 		memcpy(ta, rx_pkt_hdr->eth803_hdr.h_source, ETH_ALEN);
242 		if (local_rx_pd->flags & MWIFIEX_RXPD_FLAGS_TDLS_PACKET &&
243 		    local_rx_pd->priority < MAX_NUM_TID) {
244 			sta_ptr = mwifiex_get_sta_entry(priv, ta);
245 			if (sta_ptr)
246 				sta_ptr->rx_seq[local_rx_pd->priority] =
247 					      le16_to_cpu(local_rx_pd->seq_num);
248 			mwifiex_auto_tdls_update_peer_signal(priv, ta,
249 							     local_rx_pd->snr,
250 							     local_rx_pd->nf);
251 		}
252 	} else {
253 		if (rx_pkt_type != PKT_TYPE_BAR &&
254 		    local_rx_pd->priority < MAX_NUM_TID)
255 			priv->rx_seq[local_rx_pd->priority] = seq_num;
256 		memcpy(ta, priv->curr_bss_params.bss_descriptor.mac_address,
257 		       ETH_ALEN);
258 	}
259 
260 	/* Reorder and send to OS */
261 	ret = mwifiex_11n_rx_reorder_pkt(priv, seq_num, local_rx_pd->priority,
262 					 ta, (u8) rx_pkt_type, skb);
263 
264 	if (ret || (rx_pkt_type == PKT_TYPE_BAR))
265 		dev_kfree_skb_any(skb);
266 
267 	if (ret)
268 		priv->stats.rx_dropped++;
269 
270 	return ret;
271 }
272