1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, 2021, The Linux Foundation. All rights reserved.
4 * RMNET Data MAP protocol
7 #include <linux/netdevice.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"
16 #define RMNET_MAP_DEAGGR_SPACING 64
17 #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
19 static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
20 const void *txporthdr)
22 if (protocol == IPPROTO_TCP)
23 return &((struct tcphdr *)txporthdr)->check;
25 if (protocol == IPPROTO_UDP)
26 return &((struct udphdr *)txporthdr)->check;
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)
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;
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.
45 if (ip_fast_csum(ip4h, ip4h->ihl)) {
46 priv->stats.csum_ip4_header_bad++;
50 /* We don't support checksum offload on IPv4 fragments */
51 if (ip_is_fragment(ip4h)) {
52 priv->stats.csum_fragmented_pkt++;
56 /* Checksum offload is only supported for UDP and TCP protocols */
57 csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
59 priv->stats.csum_err_invalid_transport++;
60 return -EPROTONOSUPPORT;
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++;
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.
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.
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.
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.
90 ip_payload_csum = csum_trailer->csum_value;
92 pseudo_csum = csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
93 ntohs(ip4h->tot_len) - ip4h->ihl * 4,
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++;
102 priv->stats.csum_ok++;
106 #if IS_ENABLED(CONFIG_IPV6)
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)
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;
118 /* Checksum offload is only supported for UDP and TCP protocols;
119 * the packet cannot include any IPv6 extension headers
121 csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
123 priv->stats.csum_err_invalid_transport++;
124 return -EPROTONOSUPPORT;
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.
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);
136 pseudo_csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
137 ntohs(ip6h->payload_len),
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()).
145 * The cast is required to ensure only the low 16 bits are
148 if (ip6_payload_csum != (__sum16)~pseudo_csum) {
149 priv->stats.csum_validation_failed++;
153 priv->stats.csum_ok++;
158 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
159 struct rmnet_map_dl_csum_trailer *csum_trailer,
160 struct rmnet_priv *priv)
166 static void rmnet_map_complement_ipv4_txporthdr_csum_field(struct iphdr *ip4h)
171 txphdr = (void *)ip4h + ip4h->ihl * 4;
173 if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
174 csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
180 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
181 struct rmnet_map_ul_csum_header *ul_header,
186 val = MAP_CSUM_UL_ENABLED_FLAG;
187 if (iphdr->protocol == IPPROTO_UDP)
188 val |= MAP_CSUM_UL_UDP_FLAG;
189 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
191 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
192 ul_header->csum_info = htons(val);
194 skb->ip_summed = CHECKSUM_NONE;
196 rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
199 #if IS_ENABLED(CONFIG_IPV6)
201 rmnet_map_complement_ipv6_txporthdr_csum_field(struct ipv6hdr *ip6h)
208 if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
209 csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
215 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
216 struct rmnet_map_ul_csum_header *ul_header,
221 val = MAP_CSUM_UL_ENABLED_FLAG;
222 if (ipv6hdr->nexthdr == IPPROTO_UDP)
223 val |= MAP_CSUM_UL_UDP_FLAG;
224 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
226 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
227 ul_header->csum_info = htons(val);
229 skb->ip_summed = CHECKSUM_NONE;
231 rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
235 rmnet_map_ipv6_ul_csum_header(void *ip6hdr,
236 struct rmnet_map_ul_csum_header *ul_header,
242 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
243 struct rmnet_port *port,
244 struct net_device *orig_dev)
246 struct rmnet_priv *priv = netdev_priv(orig_dev);
247 struct rmnet_map_v5_csum_header *ul_header;
249 ul_header = skb_push(skb, sizeof(*ul_header));
250 memset(ul_header, 0, sizeof(*ul_header));
251 ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
252 MAPV5_HDRINFO_HDR_TYPE_FMASK);
254 if (skb->ip_summed == CHECKSUM_PARTIAL) {
255 void *iph = ip_hdr(skb);
260 if (skb->protocol == htons(ETH_P_IP)) {
261 u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
263 proto = ((struct iphdr *)iph)->protocol;
264 trans = iph + ip_len;
265 } else if (IS_ENABLED(CONFIG_IPV6) &&
266 skb->protocol == htons(ETH_P_IPV6)) {
267 u16 ip_len = sizeof(struct ipv6hdr);
269 proto = ((struct ipv6hdr *)iph)->nexthdr;
270 trans = iph + ip_len;
272 priv->stats.csum_err_invalid_ip_version++;
276 check = rmnet_map_get_csum_field(proto, trans);
278 skb->ip_summed = CHECKSUM_NONE;
279 /* Ask for checksum offloading */
280 ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
281 priv->stats.csum_hw++;
287 priv->stats.csum_sw++;
290 /* Adds MAP header to front of skb->data
291 * Padding is calculated and set appropriately in MAP header. Mux ID is
294 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
296 struct rmnet_port *port,
299 struct rmnet_map_header *map_header;
300 u32 padding, map_datalen;
302 map_datalen = skb->len - hdrlen;
303 map_header = (struct rmnet_map_header *)
304 skb_push(skb, sizeof(struct rmnet_map_header));
305 memset(map_header, 0, sizeof(struct rmnet_map_header));
307 /* Set next_hdr bit for csum offload packets */
308 if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
309 map_header->flags |= MAP_NEXT_HEADER_FLAG;
311 if (pad == RMNET_MAP_NO_PAD_BYTES) {
312 map_header->pkt_len = htons(map_datalen);
316 BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
317 padding = ALIGN(map_datalen, 4) - map_datalen;
322 if (skb_tailroom(skb) < padding)
325 skb_put_zero(skb, padding);
328 map_header->pkt_len = htons(map_datalen + padding);
329 /* This is a data packet, so the CMD bit is 0 */
330 map_header->flags = padding & MAP_PAD_LEN_MASK;
335 /* Deaggregates a single packet
336 * A whole new buffer is allocated for each portion of an aggregated frame.
337 * Caller should keep calling deaggregate() on the source skb until 0 is
338 * returned, indicating that there are no more packets to deaggregate. Caller
339 * is responsible for freeing the original skb.
341 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
342 struct rmnet_port *port)
344 struct rmnet_map_v5_csum_header *next_hdr = NULL;
345 struct rmnet_map_header *maph;
346 void *data = skb->data;
347 struct sk_buff *skbn;
354 maph = (struct rmnet_map_header *)skb->data;
355 packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
357 if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
358 packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
359 } else if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) {
360 if (!(maph->flags & MAP_CMD_FLAG)) {
361 packet_len += sizeof(*next_hdr);
362 if (maph->flags & MAP_NEXT_HEADER_FLAG)
363 next_hdr = data + sizeof(*maph);
365 /* Mapv5 data pkt without csum hdr is invalid */
370 if (((int)skb->len - (int)packet_len) < 0)
373 /* Some hardware can send us empty frames. Catch them */
378 nexthdr_type = u8_get_bits(next_hdr->header_info,
379 MAPV5_HDRINFO_HDR_TYPE_FMASK);
380 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
384 skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
388 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
389 skb_put(skbn, packet_len);
390 memcpy(skbn->data, skb->data, packet_len);
391 skb_pull(skb, packet_len);
396 /* Validates packet checksums. Function takes a pointer to
397 * the beginning of a buffer which contains the IP payload +
398 * padding + checksum trailer.
399 * Only IPv4 and IPv6 are supported along with TCP & UDP.
400 * Fragmented or tunneled packets are not supported.
402 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
404 struct rmnet_priv *priv = netdev_priv(skb->dev);
405 struct rmnet_map_dl_csum_trailer *csum_trailer;
407 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
408 priv->stats.csum_sw++;
412 csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
414 if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
415 priv->stats.csum_valid_unset++;
419 if (skb->protocol == htons(ETH_P_IP))
420 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
422 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
423 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
425 priv->stats.csum_err_invalid_ip_version++;
427 return -EPROTONOSUPPORT;
430 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
431 struct net_device *orig_dev)
433 struct rmnet_priv *priv = netdev_priv(orig_dev);
434 struct rmnet_map_ul_csum_header *ul_header;
437 ul_header = (struct rmnet_map_ul_csum_header *)
438 skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
440 if (unlikely(!(orig_dev->features &
441 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
444 if (skb->ip_summed != CHECKSUM_PARTIAL)
447 iphdr = (char *)ul_header +
448 sizeof(struct rmnet_map_ul_csum_header);
450 if (skb->protocol == htons(ETH_P_IP)) {
451 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
452 priv->stats.csum_hw++;
456 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
457 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
458 priv->stats.csum_hw++;
462 priv->stats.csum_err_invalid_ip_version++;
465 memset(ul_header, 0, sizeof(*ul_header));
467 priv->stats.csum_sw++;
470 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
471 * packets that are supported for UL checksum offload.
473 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
474 struct rmnet_port *port,
475 struct net_device *orig_dev,
479 case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
480 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
482 case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
483 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
490 /* Process a MAPv5 packet header */
491 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
494 struct rmnet_priv *priv = netdev_priv(skb->dev);
495 struct rmnet_map_v5_csum_header *next_hdr;
498 next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
499 sizeof(struct rmnet_map_header));
501 nexthdr_type = u8_get_bits(next_hdr->header_info,
502 MAPV5_HDRINFO_HDR_TYPE_FMASK);
504 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
507 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
508 priv->stats.csum_sw++;
509 } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
510 priv->stats.csum_ok++;
511 skb->ip_summed = CHECKSUM_UNNECESSARY;
513 priv->stats.csum_valid_unset++;
516 /* Pull csum v5 header */
517 skb_pull(skb, sizeof(*next_hdr));