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 /* The trailer checksum *includes* the checksum in the transport
88 * header. Adding that to the pseudo checksum will yield 0xffff
89 * ("negative 0") if the message arrived intact.
91 WARN_ON((__sum16)~pseudo_csum);
92 csum_value_final = ~csum16_sub(pseudo_csum, (__force __be16)*csum_field);
94 if (unlikely(!csum_value_final)) {
95 switch (ip4h->protocol) {
97 /* RFC 768 - DL4 1's complement rule for UDP csum 0 */
98 csum_value_final = ~csum_value_final;
102 /* DL4 Non-RFC compliant TCP checksum found */
103 if (*csum_field == (__force __sum16)0xFFFF)
104 csum_value_final = ~csum_value_final;
109 if (csum_value_final == *csum_field) {
110 priv->stats.csum_ok++;
113 priv->stats.csum_validation_failed++;
118 #if IS_ENABLED(CONFIG_IPV6)
120 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
121 struct rmnet_map_dl_csum_trailer *csum_trailer,
122 struct rmnet_priv *priv)
124 struct ipv6hdr *ip6h = (struct ipv6hdr *)skb->data;
125 void *txporthdr = skb->data + sizeof(*ip6h);
126 __sum16 *csum_field, pseudo_csum;
127 __sum16 ip6_payload_csum;
128 __be16 ip_header_csum;
129 __sum16 csum_value_final;
132 /* Checksum offload is only supported for UDP and TCP protocols;
133 * the packet cannot include any IPv6 extension headers
135 csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
137 priv->stats.csum_err_invalid_transport++;
138 return -EPROTONOSUPPORT;
141 /* The checksum value in the trailer is computed over the entire
142 * IP packet, including the IP header and payload. To derive the
143 * transport checksum from this, we first subract the contribution
144 * of the IP header from the trailer checksum. We then add the
145 * checksum computed over the pseudo header.
147 ip_header_csum = (__force __be16)ip_fast_csum(ip6h, sizeof(*ip6h) / 4);
148 ip6_payload_csum = ~csum16_sub((__force __sum16)csum_trailer->csum_value,
151 length = (ip6h->nexthdr == IPPROTO_UDP) ?
152 ntohs(((struct udphdr *)txporthdr)->len) :
153 ntohs(ip6h->payload_len);
154 pseudo_csum = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
155 length, ip6h->nexthdr, 0);
156 pseudo_csum = csum16_add(ip6_payload_csum, (__force __be16)pseudo_csum);
158 /* Adding the payload checksum to the pseudo checksum yields 0xffff
159 * ("negative 0") if the message arrived intact.
161 WARN_ON((__sum16)~pseudo_csum);
162 csum_value_final = ~csum16_sub(pseudo_csum, (__force __be16)*csum_field);
164 if (unlikely(csum_value_final == 0)) {
165 switch (ip6h->nexthdr) {
167 /* RFC 2460 section 8.1
168 * DL6 One's complement rule for UDP checksum 0
170 csum_value_final = ~csum_value_final;
174 /* DL6 Non-RFC compliant TCP checksum found */
175 if (*csum_field == (__force __sum16)0xFFFF)
176 csum_value_final = ~csum_value_final;
181 if (csum_value_final == *csum_field) {
182 priv->stats.csum_ok++;
185 priv->stats.csum_validation_failed++;
191 static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
193 struct iphdr *ip4h = (struct iphdr *)iphdr;
197 txphdr = iphdr + ip4h->ihl * 4;
199 if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
200 csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
206 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
207 struct rmnet_map_ul_csum_header *ul_header,
212 val = MAP_CSUM_UL_ENABLED_FLAG;
213 if (iphdr->protocol == IPPROTO_UDP)
214 val |= MAP_CSUM_UL_UDP_FLAG;
215 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
217 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
218 ul_header->csum_info = htons(val);
220 skb->ip_summed = CHECKSUM_NONE;
222 rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
225 #if IS_ENABLED(CONFIG_IPV6)
226 static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
228 struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
232 txphdr = ip6hdr + sizeof(struct ipv6hdr);
234 if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
235 csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
241 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
242 struct rmnet_map_ul_csum_header *ul_header,
247 val = MAP_CSUM_UL_ENABLED_FLAG;
248 if (ipv6hdr->nexthdr == IPPROTO_UDP)
249 val |= MAP_CSUM_UL_UDP_FLAG;
250 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
252 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
253 ul_header->csum_info = htons(val);
255 skb->ip_summed = CHECKSUM_NONE;
257 rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
261 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
262 struct rmnet_port *port,
263 struct net_device *orig_dev)
265 struct rmnet_priv *priv = netdev_priv(orig_dev);
266 struct rmnet_map_v5_csum_header *ul_header;
268 ul_header = skb_push(skb, sizeof(*ul_header));
269 memset(ul_header, 0, sizeof(*ul_header));
270 ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
271 MAPV5_HDRINFO_HDR_TYPE_FMASK);
273 if (skb->ip_summed == CHECKSUM_PARTIAL) {
274 void *iph = ip_hdr(skb);
279 if (skb->protocol == htons(ETH_P_IP)) {
280 u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
282 proto = ((struct iphdr *)iph)->protocol;
283 trans = iph + ip_len;
284 } else if (IS_ENABLED(CONFIG_IPV6) &&
285 skb->protocol == htons(ETH_P_IPV6)) {
286 u16 ip_len = sizeof(struct ipv6hdr);
288 proto = ((struct ipv6hdr *)iph)->nexthdr;
289 trans = iph + ip_len;
291 priv->stats.csum_err_invalid_ip_version++;
295 check = rmnet_map_get_csum_field(proto, trans);
297 skb->ip_summed = CHECKSUM_NONE;
298 /* Ask for checksum offloading */
299 ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
300 priv->stats.csum_hw++;
306 priv->stats.csum_sw++;
309 /* Adds MAP header to front of skb->data
310 * Padding is calculated and set appropriately in MAP header. Mux ID is
313 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
315 struct rmnet_port *port,
318 struct rmnet_map_header *map_header;
319 u32 padding, map_datalen;
322 map_datalen = skb->len - hdrlen;
323 map_header = (struct rmnet_map_header *)
324 skb_push(skb, sizeof(struct rmnet_map_header));
325 memset(map_header, 0, sizeof(struct rmnet_map_header));
327 /* Set next_hdr bit for csum offload packets */
328 if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
329 map_header->flags |= MAP_NEXT_HEADER_FLAG;
331 if (pad == RMNET_MAP_NO_PAD_BYTES) {
332 map_header->pkt_len = htons(map_datalen);
336 BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
337 padding = ALIGN(map_datalen, 4) - map_datalen;
342 if (skb_tailroom(skb) < padding)
345 padbytes = (u8 *)skb_put(skb, padding);
346 memset(padbytes, 0, padding);
349 map_header->pkt_len = htons(map_datalen + padding);
350 /* This is a data packet, so the CMD bit is 0 */
351 map_header->flags = padding & MAP_PAD_LEN_MASK;
356 /* Deaggregates a single packet
357 * A whole new buffer is allocated for each portion of an aggregated frame.
358 * Caller should keep calling deaggregate() on the source skb until 0 is
359 * returned, indicating that there are no more packets to deaggregate. Caller
360 * is responsible for freeing the original skb.
362 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
363 struct rmnet_port *port)
365 struct rmnet_map_v5_csum_header *next_hdr = NULL;
366 struct rmnet_map_header *maph;
367 void *data = skb->data;
368 struct sk_buff *skbn;
375 maph = (struct rmnet_map_header *)skb->data;
376 packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
378 if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
379 packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
380 } else if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) {
381 if (!(maph->flags & MAP_CMD_FLAG)) {
382 packet_len += sizeof(*next_hdr);
383 if (maph->flags & MAP_NEXT_HEADER_FLAG)
384 next_hdr = data + sizeof(*maph);
386 /* Mapv5 data pkt without csum hdr is invalid */
391 if (((int)skb->len - (int)packet_len) < 0)
394 /* Some hardware can send us empty frames. Catch them */
399 nexthdr_type = u8_get_bits(next_hdr->header_info,
400 MAPV5_HDRINFO_HDR_TYPE_FMASK);
401 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
405 skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
409 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
410 skb_put(skbn, packet_len);
411 memcpy(skbn->data, skb->data, packet_len);
412 skb_pull(skb, packet_len);
417 /* Validates packet checksums. Function takes a pointer to
418 * the beginning of a buffer which contains the IP payload +
419 * padding + checksum trailer.
420 * Only IPv4 and IPv6 are supported along with TCP & UDP.
421 * Fragmented or tunneled packets are not supported.
423 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
425 struct rmnet_priv *priv = netdev_priv(skb->dev);
426 struct rmnet_map_dl_csum_trailer *csum_trailer;
428 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
429 priv->stats.csum_sw++;
433 csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
435 if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
436 priv->stats.csum_valid_unset++;
440 if (skb->protocol == htons(ETH_P_IP))
441 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
443 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
444 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
446 priv->stats.csum_err_invalid_ip_version++;
448 return -EPROTONOSUPPORT;
451 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
452 struct net_device *orig_dev)
454 struct rmnet_priv *priv = netdev_priv(orig_dev);
455 struct rmnet_map_ul_csum_header *ul_header;
458 ul_header = (struct rmnet_map_ul_csum_header *)
459 skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
461 if (unlikely(!(orig_dev->features &
462 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
465 if (skb->ip_summed != CHECKSUM_PARTIAL)
468 iphdr = (char *)ul_header +
469 sizeof(struct rmnet_map_ul_csum_header);
471 if (skb->protocol == htons(ETH_P_IP)) {
472 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
473 priv->stats.csum_hw++;
477 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
478 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
479 priv->stats.csum_hw++;
483 priv->stats.csum_err_invalid_ip_version++;
486 memset(ul_header, 0, sizeof(*ul_header));
488 priv->stats.csum_sw++;
491 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
492 * packets that are supported for UL checksum offload.
494 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
495 struct rmnet_port *port,
496 struct net_device *orig_dev,
500 case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
501 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
503 case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
504 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
511 /* Process a MAPv5 packet header */
512 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
515 struct rmnet_priv *priv = netdev_priv(skb->dev);
516 struct rmnet_map_v5_csum_header *next_hdr;
519 next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
520 sizeof(struct rmnet_map_header));
522 nexthdr_type = u8_get_bits(next_hdr->header_info,
523 MAPV5_HDRINFO_HDR_TYPE_FMASK);
525 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
528 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
529 priv->stats.csum_sw++;
530 } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
531 priv->stats.csum_ok++;
532 skb->ip_summed = CHECKSUM_UNNECESSARY;
534 priv->stats.csum_valid_unset++;
537 /* Pull csum v5 header */
538 skb_pull(skb, sizeof(*next_hdr));