1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, 2021, The Linux Foundation. All rights reserved.
3  *
4  * RMNET Data MAP protocol
5  */
6 
7 #include <linux/netdevice.h>
8 #include <linux/ip.h>
9 #include <linux/ipv6.h>
10 #include <net/ip6_checksum.h>
11 #include <linux/bitfield.h>
12 #include "rmnet_config.h"
13 #include "rmnet_map.h"
14 #include "rmnet_private.h"
15 
16 #define RMNET_MAP_DEAGGR_SPACING  64
17 #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
18 
19 static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
20 					 const void *txporthdr)
21 {
22 	if (protocol == IPPROTO_TCP)
23 		return &((struct tcphdr *)txporthdr)->check;
24 
25 	if (protocol == IPPROTO_UDP)
26 		return &((struct udphdr *)txporthdr)->check;
27 
28 	return NULL;
29 }
30 
31 static int
32 rmnet_map_ipv4_dl_csum_trailer(struct sk_buff *skb,
33 			       struct rmnet_map_dl_csum_trailer *csum_trailer,
34 			       struct rmnet_priv *priv)
35 {
36 	struct iphdr *ip4h = (struct iphdr *)skb->data;
37 	void *txporthdr = skb->data + ip4h->ihl * 4;
38 	__sum16 *csum_field, pseudo_csum;
39 	__sum16 ip_payload_csum;
40 
41 	/* Computing the checksum over just the IPv4 header--including its
42 	 * checksum field--should yield 0.  If it doesn't, the IP header
43 	 * is bad, so return an error and let the IP layer drop it.
44 	 */
45 	if (ip_fast_csum(ip4h, ip4h->ihl)) {
46 		priv->stats.csum_ip4_header_bad++;
47 		return -EINVAL;
48 	}
49 
50 	/* We don't support checksum offload on IPv4 fragments */
51 	if (ip_is_fragment(ip4h)) {
52 		priv->stats.csum_fragmented_pkt++;
53 		return -EOPNOTSUPP;
54 	}
55 
56 	/* Checksum offload is only supported for UDP and TCP protocols */
57 	csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
58 	if (!csum_field) {
59 		priv->stats.csum_err_invalid_transport++;
60 		return -EPROTONOSUPPORT;
61 	}
62 
63 	/* RFC 768: UDP checksum is optional for IPv4, and is 0 if unused */
64 	if (!*csum_field && ip4h->protocol == IPPROTO_UDP) {
65 		priv->stats.csum_skipped++;
66 		return 0;
67 	}
68 
69 	/* The checksum value in the trailer is computed over the entire
70 	 * IP packet, including the IP header and payload.  To derive the
71 	 * transport checksum from this, we first subract the contribution
72 	 * of the IP header from the trailer checksum.  We then add the
73 	 * checksum computed over the pseudo header.
74 	 *
75 	 * We verified above that the IP header contributes zero to the
76 	 * trailer checksum.  Therefore the checksum in the trailer is
77 	 * just the checksum computed over the IP payload.
78 
79 	 * If the IP payload arrives intact, adding the pseudo header
80 	 * checksum to the IP payload checksum will yield 0xffff (negative
81 	 * zero).  This means the trailer checksum and the pseudo checksum
82 	 * are additive inverses of each other.  Put another way, the
83 	 * message passes the checksum test if the trailer checksum value
84 	 * is the negated pseudo header checksum.
85 	 *
86 	 * Knowing this, we don't even need to examine the transport
87 	 * header checksum value; it is already accounted for in the
88 	 * checksum value found in the trailer.
89 	 */
90 	ip_payload_csum = csum_trailer->csum_value;
91 
92 	pseudo_csum = csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
93 					ntohs(ip4h->tot_len) - ip4h->ihl * 4,
94 					ip4h->protocol, 0);
95 
96 	/* The cast is required to ensure only the low 16 bits are examined */
97 	if (ip_payload_csum != (__sum16)~pseudo_csum) {
98 		priv->stats.csum_validation_failed++;
99 		return -EINVAL;
100 	}
101 
102 	priv->stats.csum_ok++;
103 	return 0;
104 }
105 
106 #if IS_ENABLED(CONFIG_IPV6)
107 static int
108 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
109 			       struct rmnet_map_dl_csum_trailer *csum_trailer,
110 			       struct rmnet_priv *priv)
111 {
112 	struct ipv6hdr *ip6h = (struct ipv6hdr *)skb->data;
113 	void *txporthdr = skb->data + sizeof(*ip6h);
114 	__sum16 *csum_field, pseudo_csum;
115 	__sum16 ip6_payload_csum;
116 	__be16 ip_header_csum;
117 
118 	/* Checksum offload is only supported for UDP and TCP protocols;
119 	 * the packet cannot include any IPv6 extension headers
120 	 */
121 	csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
122 	if (!csum_field) {
123 		priv->stats.csum_err_invalid_transport++;
124 		return -EPROTONOSUPPORT;
125 	}
126 
127 	/* The checksum value in the trailer is computed over the entire
128 	 * IP packet, including the IP header and payload.  To derive the
129 	 * transport checksum from this, we first subract the contribution
130 	 * of the IP header from the trailer checksum.  We then add the
131 	 * checksum computed over the pseudo header.
132 	 */
133 	ip_header_csum = (__force __be16)ip_fast_csum(ip6h, sizeof(*ip6h) / 4);
134 	ip6_payload_csum = csum16_sub(csum_trailer->csum_value, ip_header_csum);
135 
136 	pseudo_csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
137 				      ntohs(ip6h->payload_len),
138 				      ip6h->nexthdr, 0);
139 
140 	/* It's sufficient to compare the IP payload checksum with the
141 	 * negated pseudo checksum to determine whether the packet
142 	 * checksum was good.  (See further explanation in comments
143 	 * in rmnet_map_ipv4_dl_csum_trailer()).
144 	 *
145 	 * The cast is required to ensure only the low 16 bits are
146 	 * examined.
147 	 */
148 	if (ip6_payload_csum != (__sum16)~pseudo_csum) {
149 		priv->stats.csum_validation_failed++;
150 		return -EINVAL;
151 	}
152 
153 	priv->stats.csum_ok++;
154 	return 0;
155 }
156 #endif
157 
158 static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
159 {
160 	struct iphdr *ip4h = (struct iphdr *)iphdr;
161 	void *txphdr;
162 	u16 *csum;
163 
164 	txphdr = iphdr + ip4h->ihl * 4;
165 
166 	if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
167 		csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
168 		*csum = ~(*csum);
169 	}
170 }
171 
172 static void
173 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
174 			      struct rmnet_map_ul_csum_header *ul_header,
175 			      struct sk_buff *skb)
176 {
177 	u16 val;
178 
179 	val = MAP_CSUM_UL_ENABLED_FLAG;
180 	if (iphdr->protocol == IPPROTO_UDP)
181 		val |= MAP_CSUM_UL_UDP_FLAG;
182 	val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
183 
184 	ul_header->csum_start_offset = htons(skb_network_header_len(skb));
185 	ul_header->csum_info = htons(val);
186 
187 	skb->ip_summed = CHECKSUM_NONE;
188 
189 	rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
190 }
191 
192 #if IS_ENABLED(CONFIG_IPV6)
193 static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
194 {
195 	struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
196 	void *txphdr;
197 	u16 *csum;
198 
199 	txphdr = ip6hdr + sizeof(struct ipv6hdr);
200 
201 	if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
202 		csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
203 		*csum = ~(*csum);
204 	}
205 }
206 
207 static void
208 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
209 			      struct rmnet_map_ul_csum_header *ul_header,
210 			      struct sk_buff *skb)
211 {
212 	u16 val;
213 
214 	val = MAP_CSUM_UL_ENABLED_FLAG;
215 	if (ipv6hdr->nexthdr == IPPROTO_UDP)
216 		val |= MAP_CSUM_UL_UDP_FLAG;
217 	val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
218 
219 	ul_header->csum_start_offset = htons(skb_network_header_len(skb));
220 	ul_header->csum_info = htons(val);
221 
222 	skb->ip_summed = CHECKSUM_NONE;
223 
224 	rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
225 }
226 #endif
227 
228 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
229 						struct rmnet_port *port,
230 						struct net_device *orig_dev)
231 {
232 	struct rmnet_priv *priv = netdev_priv(orig_dev);
233 	struct rmnet_map_v5_csum_header *ul_header;
234 
235 	ul_header = skb_push(skb, sizeof(*ul_header));
236 	memset(ul_header, 0, sizeof(*ul_header));
237 	ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
238 						MAPV5_HDRINFO_HDR_TYPE_FMASK);
239 
240 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
241 		void *iph = ip_hdr(skb);
242 		__sum16 *check;
243 		void *trans;
244 		u8 proto;
245 
246 		if (skb->protocol == htons(ETH_P_IP)) {
247 			u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
248 
249 			proto = ((struct iphdr *)iph)->protocol;
250 			trans = iph + ip_len;
251 		} else if (IS_ENABLED(CONFIG_IPV6) &&
252 			   skb->protocol == htons(ETH_P_IPV6)) {
253 			u16 ip_len = sizeof(struct ipv6hdr);
254 
255 			proto = ((struct ipv6hdr *)iph)->nexthdr;
256 			trans = iph + ip_len;
257 		} else {
258 			priv->stats.csum_err_invalid_ip_version++;
259 			goto sw_csum;
260 		}
261 
262 		check = rmnet_map_get_csum_field(proto, trans);
263 		if (check) {
264 			skb->ip_summed = CHECKSUM_NONE;
265 			/* Ask for checksum offloading */
266 			ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
267 			priv->stats.csum_hw++;
268 			return;
269 		}
270 	}
271 
272 sw_csum:
273 	priv->stats.csum_sw++;
274 }
275 
276 /* Adds MAP header to front of skb->data
277  * Padding is calculated and set appropriately in MAP header. Mux ID is
278  * initialized to 0.
279  */
280 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
281 						  int hdrlen,
282 						  struct rmnet_port *port,
283 						  int pad)
284 {
285 	struct rmnet_map_header *map_header;
286 	u32 padding, map_datalen;
287 	u8 *padbytes;
288 
289 	map_datalen = skb->len - hdrlen;
290 	map_header = (struct rmnet_map_header *)
291 			skb_push(skb, sizeof(struct rmnet_map_header));
292 	memset(map_header, 0, sizeof(struct rmnet_map_header));
293 
294 	/* Set next_hdr bit for csum offload packets */
295 	if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
296 		map_header->flags |= MAP_NEXT_HEADER_FLAG;
297 
298 	if (pad == RMNET_MAP_NO_PAD_BYTES) {
299 		map_header->pkt_len = htons(map_datalen);
300 		return map_header;
301 	}
302 
303 	BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
304 	padding = ALIGN(map_datalen, 4) - map_datalen;
305 
306 	if (padding == 0)
307 		goto done;
308 
309 	if (skb_tailroom(skb) < padding)
310 		return NULL;
311 
312 	padbytes = (u8 *)skb_put(skb, padding);
313 	memset(padbytes, 0, padding);
314 
315 done:
316 	map_header->pkt_len = htons(map_datalen + padding);
317 	/* This is a data packet, so the CMD bit is 0 */
318 	map_header->flags = padding & MAP_PAD_LEN_MASK;
319 
320 	return map_header;
321 }
322 
323 /* Deaggregates a single packet
324  * A whole new buffer is allocated for each portion of an aggregated frame.
325  * Caller should keep calling deaggregate() on the source skb until 0 is
326  * returned, indicating that there are no more packets to deaggregate. Caller
327  * is responsible for freeing the original skb.
328  */
329 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
330 				      struct rmnet_port *port)
331 {
332 	struct rmnet_map_v5_csum_header *next_hdr = NULL;
333 	struct rmnet_map_header *maph;
334 	void *data = skb->data;
335 	struct sk_buff *skbn;
336 	u8 nexthdr_type;
337 	u32 packet_len;
338 
339 	if (skb->len == 0)
340 		return NULL;
341 
342 	maph = (struct rmnet_map_header *)skb->data;
343 	packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
344 
345 	if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
346 		packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
347 	} else if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) {
348 		if (!(maph->flags & MAP_CMD_FLAG)) {
349 			packet_len += sizeof(*next_hdr);
350 			if (maph->flags & MAP_NEXT_HEADER_FLAG)
351 				next_hdr = data + sizeof(*maph);
352 			else
353 				/* Mapv5 data pkt without csum hdr is invalid */
354 				return NULL;
355 		}
356 	}
357 
358 	if (((int)skb->len - (int)packet_len) < 0)
359 		return NULL;
360 
361 	/* Some hardware can send us empty frames. Catch them */
362 	if (!maph->pkt_len)
363 		return NULL;
364 
365 	if (next_hdr) {
366 		nexthdr_type = u8_get_bits(next_hdr->header_info,
367 					   MAPV5_HDRINFO_HDR_TYPE_FMASK);
368 		if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
369 			return NULL;
370 	}
371 
372 	skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
373 	if (!skbn)
374 		return NULL;
375 
376 	skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
377 	skb_put(skbn, packet_len);
378 	memcpy(skbn->data, skb->data, packet_len);
379 	skb_pull(skb, packet_len);
380 
381 	return skbn;
382 }
383 
384 /* Validates packet checksums. Function takes a pointer to
385  * the beginning of a buffer which contains the IP payload +
386  * padding + checksum trailer.
387  * Only IPv4 and IPv6 are supported along with TCP & UDP.
388  * Fragmented or tunneled packets are not supported.
389  */
390 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
391 {
392 	struct rmnet_priv *priv = netdev_priv(skb->dev);
393 	struct rmnet_map_dl_csum_trailer *csum_trailer;
394 
395 	if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
396 		priv->stats.csum_sw++;
397 		return -EOPNOTSUPP;
398 	}
399 
400 	csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
401 
402 	if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
403 		priv->stats.csum_valid_unset++;
404 		return -EINVAL;
405 	}
406 
407 	if (skb->protocol == htons(ETH_P_IP))
408 		return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
409 
410 	if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
411 		return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
412 
413 	priv->stats.csum_err_invalid_ip_version++;
414 
415 	return -EPROTONOSUPPORT;
416 }
417 
418 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
419 						struct net_device *orig_dev)
420 {
421 	struct rmnet_priv *priv = netdev_priv(orig_dev);
422 	struct rmnet_map_ul_csum_header *ul_header;
423 	void *iphdr;
424 
425 	ul_header = (struct rmnet_map_ul_csum_header *)
426 		    skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
427 
428 	if (unlikely(!(orig_dev->features &
429 		     (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
430 		goto sw_csum;
431 
432 	if (skb->ip_summed != CHECKSUM_PARTIAL)
433 		goto sw_csum;
434 
435 	iphdr = (char *)ul_header +
436 		sizeof(struct rmnet_map_ul_csum_header);
437 
438 	if (skb->protocol == htons(ETH_P_IP)) {
439 		rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
440 		priv->stats.csum_hw++;
441 		return;
442 	}
443 
444 	if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
445 		rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
446 		priv->stats.csum_hw++;
447 		return;
448 	}
449 
450 	priv->stats.csum_err_invalid_ip_version++;
451 
452 sw_csum:
453 	memset(ul_header, 0, sizeof(*ul_header));
454 
455 	priv->stats.csum_sw++;
456 }
457 
458 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
459  * packets that are supported for UL checksum offload.
460  */
461 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
462 				      struct rmnet_port *port,
463 				      struct net_device *orig_dev,
464 				      int csum_type)
465 {
466 	switch (csum_type) {
467 	case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
468 		rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
469 		break;
470 	case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
471 		rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
472 		break;
473 	default:
474 		break;
475 	}
476 }
477 
478 /* Process a MAPv5 packet header */
479 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
480 				      u16 len)
481 {
482 	struct rmnet_priv *priv = netdev_priv(skb->dev);
483 	struct rmnet_map_v5_csum_header *next_hdr;
484 	u8 nexthdr_type;
485 
486 	next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
487 			sizeof(struct rmnet_map_header));
488 
489 	nexthdr_type = u8_get_bits(next_hdr->header_info,
490 				   MAPV5_HDRINFO_HDR_TYPE_FMASK);
491 
492 	if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
493 		return -EINVAL;
494 
495 	if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
496 		priv->stats.csum_sw++;
497 	} else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
498 		priv->stats.csum_ok++;
499 		skb->ip_summed = CHECKSUM_UNNECESSARY;
500 	} else {
501 		priv->stats.csum_valid_unset++;
502 	}
503 
504 	/* Pull csum v5 header */
505 	skb_pull(skb, sizeof(*next_hdr));
506 
507 	return 0;
508 }
509