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 ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header,
90 		     sizeof(bridge_tunnel_header))) ||
91 	    (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header,
92 		     sizeof(rfc1042_header)) &&
93 	     ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_AARP &&
94 	     ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_IPX)) {
95 		/*
96 		 *  Replace the 803 header and rfc1042 header (llc/snap) with an
97 		 *    EthernetII header, keep the src/dst and snap_type
98 		 *    (ethertype).
99 		 *  The firmware only passes up SNAP frames converting
100 		 *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
101 		 *  To create the Ethernet II, just move the src, dst address
102 		 *    right before the snap_type.
103 		 */
104 		eth = (struct ethhdr *)
105 			((u8 *) &rx_pkt_hdr->eth803_hdr
106 			 + sizeof(rx_pkt_hdr->eth803_hdr) +
107 			 sizeof(rx_pkt_hdr->rfc1042_hdr)
108 			 - sizeof(rx_pkt_hdr->eth803_hdr.h_dest)
109 			 - sizeof(rx_pkt_hdr->eth803_hdr.h_source)
110 			 - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type));
111 
112 		memcpy(eth->h_source, rx_pkt_hdr->eth803_hdr.h_source,
113 		       sizeof(eth->h_source));
114 		memcpy(eth->h_dest, rx_pkt_hdr->eth803_hdr.h_dest,
115 		       sizeof(eth->h_dest));
116 
117 		/* Chop off the rxpd + the excess memory from the 802.2/llc/snap
118 		   header that was removed. */
119 		hdr_chop = (u8 *) eth - (u8 *) local_rx_pd;
120 	} else {
121 		/* Chop off the rxpd */
122 		hdr_chop = (u8 *) &rx_pkt_hdr->eth803_hdr -
123 			(u8 *) local_rx_pd;
124 	}
125 
126 	/* Chop off the leading header bytes so the it points to the start of
127 	   either the reconstructed EthII frame or the 802.2/llc/snap frame */
128 	skb_pull(skb, hdr_chop);
129 
130 	if (priv->hs2_enabled &&
131 	    mwifiex_discard_gratuitous_arp(priv, skb)) {
132 		mwifiex_dbg(priv->adapter, INFO, "Bypassed Gratuitous ARP\n");
133 		dev_kfree_skb_any(skb);
134 		return 0;
135 	}
136 
137 	if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
138 	    ntohs(rx_pkt_hdr->eth803_hdr.h_proto) == ETH_P_TDLS) {
139 		offset = (u8 *)local_rx_pd + rx_pkt_off;
140 		mwifiex_process_tdls_action_frame(priv, offset, rx_pkt_len);
141 	}
142 
143 	/* Only stash RX bitrate for unicast packets. */
144 	if (likely(!is_multicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest))) {
145 		priv->rxpd_rate = local_rx_pd->rx_rate;
146 		priv->rxpd_htinfo = local_rx_pd->ht_info;
147 	}
148 
149 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
150 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
151 		adj_rx_rate = mwifiex_adjust_data_rate(priv,
152 						       local_rx_pd->rx_rate,
153 						       local_rx_pd->ht_info);
154 		mwifiex_hist_data_add(priv, adj_rx_rate, local_rx_pd->snr,
155 				      local_rx_pd->nf);
156 	}
157 
158 	ret = mwifiex_recv_packet(priv, skb);
159 	if (ret == -1)
160 		mwifiex_dbg(priv->adapter, ERROR,
161 			    "recv packet failed\n");
162 
163 	return ret;
164 }
165 
166 /*
167  * This function processes the received buffer.
168  *
169  * The function looks into the RxPD and performs sanity tests on the
170  * received buffer to ensure its a valid packet, before processing it
171  * further. If the packet is determined to be aggregated, it is
172  * de-aggregated accordingly. Non-unicast packets are sent directly to
173  * the kernel/upper layers. Unicast packets are handed over to the
174  * Rx reordering routine if 11n is enabled.
175  *
176  * The completion callback is called after processing in complete.
177  */
178 int mwifiex_process_sta_rx_packet(struct mwifiex_private *priv,
179 				  struct sk_buff *skb)
180 {
181 	struct mwifiex_adapter *adapter = priv->adapter;
182 	int ret = 0;
183 	struct rxpd *local_rx_pd;
184 	struct rx_packet_hdr *rx_pkt_hdr;
185 	u8 ta[ETH_ALEN];
186 	u16 rx_pkt_type, rx_pkt_offset, rx_pkt_length, seq_num;
187 	struct mwifiex_sta_node *sta_ptr;
188 
189 	local_rx_pd = (struct rxpd *) (skb->data);
190 	rx_pkt_type = le16_to_cpu(local_rx_pd->rx_pkt_type);
191 	rx_pkt_offset = le16_to_cpu(local_rx_pd->rx_pkt_offset);
192 	rx_pkt_length = le16_to_cpu(local_rx_pd->rx_pkt_length);
193 	seq_num = le16_to_cpu(local_rx_pd->seq_num);
194 
195 	rx_pkt_hdr = (void *)local_rx_pd + rx_pkt_offset;
196 
197 	if ((rx_pkt_offset + rx_pkt_length) > (u16) skb->len) {
198 		mwifiex_dbg(adapter, ERROR,
199 			    "wrong rx packet: len=%d, rx_pkt_offset=%d, rx_pkt_length=%d\n",
200 			    skb->len, rx_pkt_offset, rx_pkt_length);
201 		priv->stats.rx_dropped++;
202 		dev_kfree_skb_any(skb);
203 		return ret;
204 	}
205 
206 	if (rx_pkt_type == PKT_TYPE_MGMT) {
207 		ret = mwifiex_process_mgmt_packet(priv, skb);
208 		if (ret)
209 			mwifiex_dbg(adapter, DATA, "Rx of mgmt packet failed");
210 		dev_kfree_skb_any(skb);
211 		return ret;
212 	}
213 
214 	/*
215 	 * If the packet is not an unicast packet then send the packet
216 	 * directly to os. Don't pass thru rx reordering
217 	 */
218 	if ((!IS_11N_ENABLED(priv) &&
219 	     !(ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
220 	       !(local_rx_pd->flags & MWIFIEX_RXPD_FLAGS_TDLS_PACKET))) ||
221 	    !ether_addr_equal_unaligned(priv->curr_addr, rx_pkt_hdr->eth803_hdr.h_dest)) {
222 		mwifiex_process_rx_packet(priv, skb);
223 		return ret;
224 	}
225 
226 	if (mwifiex_queuing_ra_based(priv) ||
227 	    (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
228 	     local_rx_pd->flags & MWIFIEX_RXPD_FLAGS_TDLS_PACKET)) {
229 		memcpy(ta, rx_pkt_hdr->eth803_hdr.h_source, ETH_ALEN);
230 		if (local_rx_pd->flags & MWIFIEX_RXPD_FLAGS_TDLS_PACKET &&
231 		    local_rx_pd->priority < MAX_NUM_TID) {
232 			sta_ptr = mwifiex_get_sta_entry(priv, ta);
233 			if (sta_ptr)
234 				sta_ptr->rx_seq[local_rx_pd->priority] =
235 					      le16_to_cpu(local_rx_pd->seq_num);
236 			mwifiex_auto_tdls_update_peer_signal(priv, ta,
237 							     local_rx_pd->snr,
238 							     local_rx_pd->nf);
239 		}
240 	} else {
241 		if (rx_pkt_type != PKT_TYPE_BAR &&
242 		    local_rx_pd->priority < MAX_NUM_TID)
243 			priv->rx_seq[local_rx_pd->priority] = seq_num;
244 		memcpy(ta, priv->curr_bss_params.bss_descriptor.mac_address,
245 		       ETH_ALEN);
246 	}
247 
248 	/* Reorder and send to OS */
249 	ret = mwifiex_11n_rx_reorder_pkt(priv, seq_num, local_rx_pd->priority,
250 					 ta, (u8) rx_pkt_type, skb);
251 
252 	if (ret || (rx_pkt_type == PKT_TYPE_BAR))
253 		dev_kfree_skb_any(skb);
254 
255 	if (ret)
256 		priv->stats.rx_dropped++;
257 
258 	return ret;
259 }
260