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;
40 __sum16 csum_value_final;
42 /* Computing the checksum over just the IPv4 header--including its
43 * checksum field--should yield 0. If it doesn't, the IP header
44 * is bad, so return an error and let the IP layer drop it.
46 if (ip_fast_csum(ip4h, ip4h->ihl)) {
47 priv->stats.csum_ip4_header_bad++;
51 /* We don't support checksum offload on IPv4 fragments */
52 if (ip_is_fragment(ip4h)) {
53 priv->stats.csum_fragmented_pkt++;
57 /* Checksum offload is only supported for UDP and TCP protocols */
58 csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
60 priv->stats.csum_err_invalid_transport++;
61 return -EPROTONOSUPPORT;
64 /* RFC 768: UDP checksum is optional for IPv4, and is 0 if unused */
65 if (!*csum_field && ip4h->protocol == IPPROTO_UDP) {
66 priv->stats.csum_skipped++;
70 /* The checksum value in the trailer is computed over the entire
71 * IP packet, including the IP header and payload. To derive the
72 * transport checksum from this, we first subract the contribution
73 * of the IP header from the trailer checksum. We then add the
74 * checksum computed over the pseudo header.
76 * We verified above that the IP header contributes zero to the
77 * trailer checksum. Therefore the checksum in the trailer is
78 * just the checksum computed over the IP payload.
80 ip_payload_csum = (__force __sum16)~csum_trailer->csum_value;
82 pseudo_csum = ~csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
83 ntohs(ip4h->tot_len) - ip4h->ihl * 4,
85 pseudo_csum = csum16_add(ip_payload_csum, (__force __be16)pseudo_csum);
87 csum_value_final = ~csum16_sub(pseudo_csum, (__force __be16)*csum_field);
89 if (unlikely(!csum_value_final)) {
90 switch (ip4h->protocol) {
92 /* RFC 768 - DL4 1's complement rule for UDP csum 0 */
93 csum_value_final = ~csum_value_final;
97 /* DL4 Non-RFC compliant TCP checksum found */
98 if (*csum_field == (__force __sum16)0xFFFF)
99 csum_value_final = ~csum_value_final;
104 if (csum_value_final == *csum_field) {
105 priv->stats.csum_ok++;
108 priv->stats.csum_validation_failed++;
113 #if IS_ENABLED(CONFIG_IPV6)
115 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
116 struct rmnet_map_dl_csum_trailer *csum_trailer,
117 struct rmnet_priv *priv)
119 struct ipv6hdr *ip6h = (struct ipv6hdr *)skb->data;
120 void *txporthdr = skb->data + sizeof(*ip6h);
121 __sum16 *csum_field, pseudo_csum;
122 __sum16 ip6_payload_csum;
123 __be16 ip_header_csum;
124 __sum16 csum_value_final;
127 /* Checksum offload is only supported for UDP and TCP protocols;
128 * the packet cannot include any IPv6 extension headers
130 csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
132 priv->stats.csum_err_invalid_transport++;
133 return -EPROTONOSUPPORT;
136 /* The checksum value in the trailer is computed over the entire
137 * IP packet, including the IP header and payload. To derive the
138 * transport checksum from this, we first subract the contribution
139 * of the IP header from the trailer checksum. We then add the
140 * checksum computed over the pseudo header.
142 ip_header_csum = (__force __be16)ip_fast_csum(ip6h, sizeof(*ip6h) / 4);
143 ip6_payload_csum = ~csum16_sub((__force __sum16)csum_trailer->csum_value,
146 length = (ip6h->nexthdr == IPPROTO_UDP) ?
147 ntohs(((struct udphdr *)txporthdr)->len) :
148 ntohs(ip6h->payload_len);
149 pseudo_csum = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
150 length, ip6h->nexthdr, 0);
151 pseudo_csum = csum16_add(ip6_payload_csum, (__force __be16)pseudo_csum);
153 csum_value_final = ~csum16_sub(pseudo_csum, (__force __be16)*csum_field);
155 if (unlikely(csum_value_final == 0)) {
156 switch (ip6h->nexthdr) {
158 /* RFC 2460 section 8.1
159 * DL6 One's complement rule for UDP checksum 0
161 csum_value_final = ~csum_value_final;
165 /* DL6 Non-RFC compliant TCP checksum found */
166 if (*csum_field == (__force __sum16)0xFFFF)
167 csum_value_final = ~csum_value_final;
172 if (csum_value_final == *csum_field) {
173 priv->stats.csum_ok++;
176 priv->stats.csum_validation_failed++;
182 static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
184 struct iphdr *ip4h = (struct iphdr *)iphdr;
188 txphdr = iphdr + ip4h->ihl * 4;
190 if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
191 csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
197 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
198 struct rmnet_map_ul_csum_header *ul_header,
203 val = MAP_CSUM_UL_ENABLED_FLAG;
204 if (iphdr->protocol == IPPROTO_UDP)
205 val |= MAP_CSUM_UL_UDP_FLAG;
206 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
208 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
209 ul_header->csum_info = htons(val);
211 skb->ip_summed = CHECKSUM_NONE;
213 rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
216 #if IS_ENABLED(CONFIG_IPV6)
217 static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
219 struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
223 txphdr = ip6hdr + sizeof(struct ipv6hdr);
225 if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
226 csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
232 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
233 struct rmnet_map_ul_csum_header *ul_header,
238 val = MAP_CSUM_UL_ENABLED_FLAG;
239 if (ipv6hdr->nexthdr == IPPROTO_UDP)
240 val |= MAP_CSUM_UL_UDP_FLAG;
241 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
243 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
244 ul_header->csum_info = htons(val);
246 skb->ip_summed = CHECKSUM_NONE;
248 rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
252 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
253 struct rmnet_port *port,
254 struct net_device *orig_dev)
256 struct rmnet_priv *priv = netdev_priv(orig_dev);
257 struct rmnet_map_v5_csum_header *ul_header;
259 ul_header = skb_push(skb, sizeof(*ul_header));
260 memset(ul_header, 0, sizeof(*ul_header));
261 ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
262 MAPV5_HDRINFO_HDR_TYPE_FMASK);
264 if (skb->ip_summed == CHECKSUM_PARTIAL) {
265 void *iph = ip_hdr(skb);
270 if (skb->protocol == htons(ETH_P_IP)) {
271 u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
273 proto = ((struct iphdr *)iph)->protocol;
274 trans = iph + ip_len;
275 } else if (IS_ENABLED(CONFIG_IPV6) &&
276 skb->protocol == htons(ETH_P_IPV6)) {
277 u16 ip_len = sizeof(struct ipv6hdr);
279 proto = ((struct ipv6hdr *)iph)->nexthdr;
280 trans = iph + ip_len;
282 priv->stats.csum_err_invalid_ip_version++;
286 check = rmnet_map_get_csum_field(proto, trans);
288 skb->ip_summed = CHECKSUM_NONE;
289 /* Ask for checksum offloading */
290 ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
291 priv->stats.csum_hw++;
297 priv->stats.csum_sw++;
300 /* Adds MAP header to front of skb->data
301 * Padding is calculated and set appropriately in MAP header. Mux ID is
304 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
306 struct rmnet_port *port,
309 struct rmnet_map_header *map_header;
310 u32 padding, map_datalen;
313 map_datalen = skb->len - hdrlen;
314 map_header = (struct rmnet_map_header *)
315 skb_push(skb, sizeof(struct rmnet_map_header));
316 memset(map_header, 0, sizeof(struct rmnet_map_header));
318 /* Set next_hdr bit for csum offload packets */
319 if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
320 map_header->flags |= MAP_NEXT_HEADER_FLAG;
322 if (pad == RMNET_MAP_NO_PAD_BYTES) {
323 map_header->pkt_len = htons(map_datalen);
327 BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
328 padding = ALIGN(map_datalen, 4) - map_datalen;
333 if (skb_tailroom(skb) < padding)
336 padbytes = (u8 *)skb_put(skb, padding);
337 memset(padbytes, 0, padding);
340 map_header->pkt_len = htons(map_datalen + padding);
341 /* This is a data packet, so the CMD bit is 0 */
342 map_header->flags = padding & MAP_PAD_LEN_MASK;
347 /* Deaggregates a single packet
348 * A whole new buffer is allocated for each portion of an aggregated frame.
349 * Caller should keep calling deaggregate() on the source skb until 0 is
350 * returned, indicating that there are no more packets to deaggregate. Caller
351 * is responsible for freeing the original skb.
353 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
354 struct rmnet_port *port)
356 struct rmnet_map_v5_csum_header *next_hdr = NULL;
357 struct rmnet_map_header *maph;
358 void *data = skb->data;
359 struct sk_buff *skbn;
366 maph = (struct rmnet_map_header *)skb->data;
367 packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
369 if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
370 packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
371 } else if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) {
372 if (!(maph->flags & MAP_CMD_FLAG)) {
373 packet_len += sizeof(*next_hdr);
374 if (maph->flags & MAP_NEXT_HEADER_FLAG)
375 next_hdr = data + sizeof(*maph);
377 /* Mapv5 data pkt without csum hdr is invalid */
382 if (((int)skb->len - (int)packet_len) < 0)
385 /* Some hardware can send us empty frames. Catch them */
390 nexthdr_type = u8_get_bits(next_hdr->header_info,
391 MAPV5_HDRINFO_HDR_TYPE_FMASK);
392 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
396 skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
400 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
401 skb_put(skbn, packet_len);
402 memcpy(skbn->data, skb->data, packet_len);
403 skb_pull(skb, packet_len);
408 /* Validates packet checksums. Function takes a pointer to
409 * the beginning of a buffer which contains the IP payload +
410 * padding + checksum trailer.
411 * Only IPv4 and IPv6 are supported along with TCP & UDP.
412 * Fragmented or tunneled packets are not supported.
414 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
416 struct rmnet_priv *priv = netdev_priv(skb->dev);
417 struct rmnet_map_dl_csum_trailer *csum_trailer;
419 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
420 priv->stats.csum_sw++;
424 csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
426 if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
427 priv->stats.csum_valid_unset++;
431 if (skb->protocol == htons(ETH_P_IP))
432 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
434 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
435 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
437 priv->stats.csum_err_invalid_ip_version++;
439 return -EPROTONOSUPPORT;
442 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
443 struct net_device *orig_dev)
445 struct rmnet_priv *priv = netdev_priv(orig_dev);
446 struct rmnet_map_ul_csum_header *ul_header;
449 ul_header = (struct rmnet_map_ul_csum_header *)
450 skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
452 if (unlikely(!(orig_dev->features &
453 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
456 if (skb->ip_summed != CHECKSUM_PARTIAL)
459 iphdr = (char *)ul_header +
460 sizeof(struct rmnet_map_ul_csum_header);
462 if (skb->protocol == htons(ETH_P_IP)) {
463 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
464 priv->stats.csum_hw++;
468 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
469 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
470 priv->stats.csum_hw++;
474 priv->stats.csum_err_invalid_ip_version++;
477 memset(ul_header, 0, sizeof(*ul_header));
479 priv->stats.csum_sw++;
482 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
483 * packets that are supported for UL checksum offload.
485 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
486 struct rmnet_port *port,
487 struct net_device *orig_dev,
491 case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
492 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
494 case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
495 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
502 /* Process a MAPv5 packet header */
503 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
506 struct rmnet_priv *priv = netdev_priv(skb->dev);
507 struct rmnet_map_v5_csum_header *next_hdr;
510 next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
511 sizeof(struct rmnet_map_header));
513 nexthdr_type = u8_get_bits(next_hdr->header_info,
514 MAPV5_HDRINFO_HDR_TYPE_FMASK);
516 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
519 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
520 priv->stats.csum_sw++;
521 } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
522 priv->stats.csum_ok++;
523 skb->ip_summed = CHECKSUM_UNNECESSARY;
525 priv->stats.csum_valid_unset++;
528 /* Pull csum v5 header */
529 skb_pull(skb, sizeof(*next_hdr));