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"
15 #include "rmnet_vnd.h"
17 #define RMNET_MAP_DEAGGR_SPACING 64
18 #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
20 static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
21 const void *txporthdr)
23 if (protocol == IPPROTO_TCP)
24 return &((struct tcphdr *)txporthdr)->check;
26 if (protocol == IPPROTO_UDP)
27 return &((struct udphdr *)txporthdr)->check;
33 rmnet_map_ipv4_dl_csum_trailer(struct sk_buff *skb,
34 struct rmnet_map_dl_csum_trailer *csum_trailer,
35 struct rmnet_priv *priv)
37 struct iphdr *ip4h = (struct iphdr *)skb->data;
38 void *txporthdr = skb->data + ip4h->ihl * 4;
39 __sum16 *csum_field, pseudo_csum;
40 __sum16 ip_payload_csum;
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 * If the IP payload arrives intact, adding the pseudo header
81 * checksum to the IP payload checksum will yield 0xffff (negative
82 * zero). This means the trailer checksum and the pseudo checksum
83 * are additive inverses of each other. Put another way, the
84 * message passes the checksum test if the trailer checksum value
85 * is the negated pseudo header checksum.
87 * Knowing this, we don't even need to examine the transport
88 * header checksum value; it is already accounted for in the
89 * checksum value found in the trailer.
91 ip_payload_csum = csum_trailer->csum_value;
93 pseudo_csum = csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
94 ntohs(ip4h->tot_len) - ip4h->ihl * 4,
97 /* The cast is required to ensure only the low 16 bits are examined */
98 if (ip_payload_csum != (__sum16)~pseudo_csum) {
99 priv->stats.csum_validation_failed++;
103 priv->stats.csum_ok++;
107 #if IS_ENABLED(CONFIG_IPV6)
109 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
110 struct rmnet_map_dl_csum_trailer *csum_trailer,
111 struct rmnet_priv *priv)
113 struct ipv6hdr *ip6h = (struct ipv6hdr *)skb->data;
114 void *txporthdr = skb->data + sizeof(*ip6h);
115 __sum16 *csum_field, pseudo_csum;
116 __sum16 ip6_payload_csum;
117 __be16 ip_header_csum;
119 /* Checksum offload is only supported for UDP and TCP protocols;
120 * the packet cannot include any IPv6 extension headers
122 csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
124 priv->stats.csum_err_invalid_transport++;
125 return -EPROTONOSUPPORT;
128 /* The checksum value in the trailer is computed over the entire
129 * IP packet, including the IP header and payload. To derive the
130 * transport checksum from this, we first subract the contribution
131 * of the IP header from the trailer checksum. We then add the
132 * checksum computed over the pseudo header.
134 ip_header_csum = (__force __be16)ip_fast_csum(ip6h, sizeof(*ip6h) / 4);
135 ip6_payload_csum = csum16_sub(csum_trailer->csum_value, ip_header_csum);
137 pseudo_csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
138 ntohs(ip6h->payload_len),
141 /* It's sufficient to compare the IP payload checksum with the
142 * negated pseudo checksum to determine whether the packet
143 * checksum was good. (See further explanation in comments
144 * in rmnet_map_ipv4_dl_csum_trailer()).
146 * The cast is required to ensure only the low 16 bits are
149 if (ip6_payload_csum != (__sum16)~pseudo_csum) {
150 priv->stats.csum_validation_failed++;
154 priv->stats.csum_ok++;
159 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
160 struct rmnet_map_dl_csum_trailer *csum_trailer,
161 struct rmnet_priv *priv)
167 static void rmnet_map_complement_ipv4_txporthdr_csum_field(struct iphdr *ip4h)
172 txphdr = (void *)ip4h + ip4h->ihl * 4;
174 if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
175 csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
181 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
182 struct rmnet_map_ul_csum_header *ul_header,
187 val = MAP_CSUM_UL_ENABLED_FLAG;
188 if (iphdr->protocol == IPPROTO_UDP)
189 val |= MAP_CSUM_UL_UDP_FLAG;
190 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
192 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
193 ul_header->csum_info = htons(val);
195 skb->ip_summed = CHECKSUM_NONE;
197 rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
200 #if IS_ENABLED(CONFIG_IPV6)
202 rmnet_map_complement_ipv6_txporthdr_csum_field(struct ipv6hdr *ip6h)
209 if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
210 csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
216 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
217 struct rmnet_map_ul_csum_header *ul_header,
222 val = MAP_CSUM_UL_ENABLED_FLAG;
223 if (ipv6hdr->nexthdr == IPPROTO_UDP)
224 val |= MAP_CSUM_UL_UDP_FLAG;
225 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
227 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
228 ul_header->csum_info = htons(val);
230 skb->ip_summed = CHECKSUM_NONE;
232 rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
236 rmnet_map_ipv6_ul_csum_header(void *ip6hdr,
237 struct rmnet_map_ul_csum_header *ul_header,
243 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
244 struct rmnet_port *port,
245 struct net_device *orig_dev)
247 struct rmnet_priv *priv = netdev_priv(orig_dev);
248 struct rmnet_map_v5_csum_header *ul_header;
250 ul_header = skb_push(skb, sizeof(*ul_header));
251 memset(ul_header, 0, sizeof(*ul_header));
252 ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
253 MAPV5_HDRINFO_HDR_TYPE_FMASK);
255 if (skb->ip_summed == CHECKSUM_PARTIAL) {
256 void *iph = ip_hdr(skb);
261 if (skb->protocol == htons(ETH_P_IP)) {
262 u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
264 proto = ((struct iphdr *)iph)->protocol;
265 trans = iph + ip_len;
266 } else if (IS_ENABLED(CONFIG_IPV6) &&
267 skb->protocol == htons(ETH_P_IPV6)) {
268 u16 ip_len = sizeof(struct ipv6hdr);
270 proto = ((struct ipv6hdr *)iph)->nexthdr;
271 trans = iph + ip_len;
273 priv->stats.csum_err_invalid_ip_version++;
277 check = rmnet_map_get_csum_field(proto, trans);
279 skb->ip_summed = CHECKSUM_NONE;
280 /* Ask for checksum offloading */
281 ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
282 priv->stats.csum_hw++;
288 priv->stats.csum_sw++;
291 /* Adds MAP header to front of skb->data
292 * Padding is calculated and set appropriately in MAP header. Mux ID is
295 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
297 struct rmnet_port *port,
300 struct rmnet_map_header *map_header;
301 u32 padding, map_datalen;
303 map_datalen = skb->len - hdrlen;
304 map_header = (struct rmnet_map_header *)
305 skb_push(skb, sizeof(struct rmnet_map_header));
306 memset(map_header, 0, sizeof(struct rmnet_map_header));
308 /* Set next_hdr bit for csum offload packets */
309 if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
310 map_header->flags |= MAP_NEXT_HEADER_FLAG;
312 if (pad == RMNET_MAP_NO_PAD_BYTES) {
313 map_header->pkt_len = htons(map_datalen);
317 BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
318 padding = ALIGN(map_datalen, 4) - map_datalen;
323 if (skb_tailroom(skb) < padding)
326 skb_put_zero(skb, padding);
329 map_header->pkt_len = htons(map_datalen + padding);
330 /* This is a data packet, so the CMD bit is 0 */
331 map_header->flags = padding & MAP_PAD_LEN_MASK;
336 /* Deaggregates a single packet
337 * A whole new buffer is allocated for each portion of an aggregated frame.
338 * Caller should keep calling deaggregate() on the source skb until 0 is
339 * returned, indicating that there are no more packets to deaggregate. Caller
340 * is responsible for freeing the original skb.
342 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
343 struct rmnet_port *port)
345 struct rmnet_map_v5_csum_header *next_hdr = NULL;
346 struct rmnet_map_header *maph;
347 void *data = skb->data;
348 struct sk_buff *skbn;
355 maph = (struct rmnet_map_header *)skb->data;
356 packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
358 if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
359 packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
360 } else if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) {
361 if (!(maph->flags & MAP_CMD_FLAG)) {
362 packet_len += sizeof(*next_hdr);
363 if (maph->flags & MAP_NEXT_HEADER_FLAG)
364 next_hdr = data + sizeof(*maph);
366 /* Mapv5 data pkt without csum hdr is invalid */
371 if (((int)skb->len - (int)packet_len) < 0)
374 /* Some hardware can send us empty frames. Catch them */
379 nexthdr_type = u8_get_bits(next_hdr->header_info,
380 MAPV5_HDRINFO_HDR_TYPE_FMASK);
381 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
385 skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
389 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
390 skb_put(skbn, packet_len);
391 memcpy(skbn->data, skb->data, packet_len);
392 skb_pull(skb, packet_len);
397 /* Validates packet checksums. Function takes a pointer to
398 * the beginning of a buffer which contains the IP payload +
399 * padding + checksum trailer.
400 * Only IPv4 and IPv6 are supported along with TCP & UDP.
401 * Fragmented or tunneled packets are not supported.
403 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
405 struct rmnet_priv *priv = netdev_priv(skb->dev);
406 struct rmnet_map_dl_csum_trailer *csum_trailer;
408 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
409 priv->stats.csum_sw++;
413 csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
415 if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
416 priv->stats.csum_valid_unset++;
420 if (skb->protocol == htons(ETH_P_IP))
421 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
423 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
424 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
426 priv->stats.csum_err_invalid_ip_version++;
428 return -EPROTONOSUPPORT;
431 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
432 struct net_device *orig_dev)
434 struct rmnet_priv *priv = netdev_priv(orig_dev);
435 struct rmnet_map_ul_csum_header *ul_header;
438 ul_header = (struct rmnet_map_ul_csum_header *)
439 skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
441 if (unlikely(!(orig_dev->features &
442 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
445 if (skb->ip_summed != CHECKSUM_PARTIAL)
448 iphdr = (char *)ul_header +
449 sizeof(struct rmnet_map_ul_csum_header);
451 if (skb->protocol == htons(ETH_P_IP)) {
452 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
453 priv->stats.csum_hw++;
457 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
458 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
459 priv->stats.csum_hw++;
463 priv->stats.csum_err_invalid_ip_version++;
466 memset(ul_header, 0, sizeof(*ul_header));
468 priv->stats.csum_sw++;
471 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
472 * packets that are supported for UL checksum offload.
474 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
475 struct rmnet_port *port,
476 struct net_device *orig_dev,
480 case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
481 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
483 case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
484 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
491 /* Process a MAPv5 packet header */
492 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
495 struct rmnet_priv *priv = netdev_priv(skb->dev);
496 struct rmnet_map_v5_csum_header *next_hdr;
499 next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
500 sizeof(struct rmnet_map_header));
502 nexthdr_type = u8_get_bits(next_hdr->header_info,
503 MAPV5_HDRINFO_HDR_TYPE_FMASK);
505 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
508 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
509 priv->stats.csum_sw++;
510 } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
511 priv->stats.csum_ok++;
512 skb->ip_summed = CHECKSUM_UNNECESSARY;
514 priv->stats.csum_valid_unset++;
517 /* Pull csum v5 header */
518 skb_pull(skb, sizeof(*next_hdr));
523 #define RMNET_AGG_BYPASS_TIME_NSEC 10000000L
525 static void reset_aggr_params(struct rmnet_port *port)
527 port->skbagg_head = NULL;
530 memset(&port->agg_time, 0, sizeof(struct timespec64));
533 static void rmnet_send_skb(struct rmnet_port *port, struct sk_buff *skb)
535 if (skb_needs_linearize(skb, port->dev->features)) {
536 if (unlikely(__skb_linearize(skb))) {
537 struct rmnet_priv *priv;
539 priv = netdev_priv(port->rmnet_dev);
540 this_cpu_inc(priv->pcpu_stats->stats.tx_drops);
541 dev_kfree_skb_any(skb);
549 static void rmnet_map_flush_tx_packet_work(struct work_struct *work)
551 struct sk_buff *skb = NULL;
552 struct rmnet_port *port;
554 port = container_of(work, struct rmnet_port, agg_wq);
556 spin_lock_bh(&port->agg_lock);
557 if (likely(port->agg_state == -EINPROGRESS)) {
558 /* Buffer may have already been shipped out */
559 if (likely(port->skbagg_head)) {
560 skb = port->skbagg_head;
561 reset_aggr_params(port);
566 spin_unlock_bh(&port->agg_lock);
568 rmnet_send_skb(port, skb);
571 static enum hrtimer_restart rmnet_map_flush_tx_packet_queue(struct hrtimer *t)
573 struct rmnet_port *port;
575 port = container_of(t, struct rmnet_port, hrtimer);
577 schedule_work(&port->agg_wq);
579 return HRTIMER_NORESTART;
582 unsigned int rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port,
583 struct net_device *orig_dev)
585 struct timespec64 diff, last;
586 unsigned int len = skb->len;
587 struct sk_buff *agg_skb;
590 spin_lock_bh(&port->agg_lock);
591 memcpy(&last, &port->agg_last, sizeof(struct timespec64));
592 ktime_get_real_ts64(&port->agg_last);
594 if (!port->skbagg_head) {
595 /* Check to see if we should agg first. If the traffic is very
596 * sparse, don't aggregate.
599 diff = timespec64_sub(port->agg_last, last);
600 size = port->egress_agg_params.bytes - skb->len;
604 spin_unlock_bh(&port->agg_lock);
608 if (diff.tv_sec > 0 || diff.tv_nsec > RMNET_AGG_BYPASS_TIME_NSEC ||
612 port->skbagg_head = skb_copy_expand(skb, 0, size, GFP_ATOMIC);
613 if (!port->skbagg_head)
616 dev_kfree_skb_any(skb);
617 port->skbagg_head->protocol = htons(ETH_P_MAP);
619 ktime_get_real_ts64(&port->agg_time);
620 skb_frag_list_init(port->skbagg_head);
623 diff = timespec64_sub(port->agg_last, port->agg_time);
624 size = port->egress_agg_params.bytes - port->skbagg_head->len;
626 if (skb->len > size) {
627 agg_skb = port->skbagg_head;
628 reset_aggr_params(port);
629 spin_unlock_bh(&port->agg_lock);
630 hrtimer_cancel(&port->hrtimer);
631 rmnet_send_skb(port, agg_skb);
632 spin_lock_bh(&port->agg_lock);
636 if (skb_has_frag_list(port->skbagg_head))
637 port->skbagg_tail->next = skb;
639 skb_shinfo(port->skbagg_head)->frag_list = skb;
641 port->skbagg_head->len += skb->len;
642 port->skbagg_head->data_len += skb->len;
643 port->skbagg_head->truesize += skb->truesize;
644 port->skbagg_tail = skb;
647 if (diff.tv_sec > 0 || diff.tv_nsec > port->egress_agg_params.time_nsec ||
648 port->agg_count >= port->egress_agg_params.count ||
649 port->skbagg_head->len == port->egress_agg_params.bytes) {
650 agg_skb = port->skbagg_head;
651 reset_aggr_params(port);
652 spin_unlock_bh(&port->agg_lock);
653 hrtimer_cancel(&port->hrtimer);
654 rmnet_send_skb(port, agg_skb);
659 if (!hrtimer_active(&port->hrtimer) && port->agg_state != -EINPROGRESS) {
660 port->agg_state = -EINPROGRESS;
661 hrtimer_start(&port->hrtimer,
662 ns_to_ktime(port->egress_agg_params.time_nsec),
665 spin_unlock_bh(&port->agg_lock);
670 spin_unlock_bh(&port->agg_lock);
671 skb->protocol = htons(ETH_P_MAP);
677 void rmnet_map_update_ul_agg_config(struct rmnet_port *port, u32 size,
680 spin_lock_bh(&port->agg_lock);
681 port->egress_agg_params.bytes = size;
682 WRITE_ONCE(port->egress_agg_params.count, count);
683 port->egress_agg_params.time_nsec = time * NSEC_PER_USEC;
684 spin_unlock_bh(&port->agg_lock);
687 void rmnet_map_tx_aggregate_init(struct rmnet_port *port)
689 hrtimer_init(&port->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
690 port->hrtimer.function = rmnet_map_flush_tx_packet_queue;
691 spin_lock_init(&port->agg_lock);
692 rmnet_map_update_ul_agg_config(port, 4096, 1, 800);
693 INIT_WORK(&port->agg_wq, rmnet_map_flush_tx_packet_work);
696 void rmnet_map_tx_aggregate_exit(struct rmnet_port *port)
698 hrtimer_cancel(&port->hrtimer);
699 cancel_work_sync(&port->agg_wq);
701 spin_lock_bh(&port->agg_lock);
702 if (port->agg_state == -EINPROGRESS) {
703 if (port->skbagg_head) {
704 dev_kfree_skb_any(port->skbagg_head);
705 reset_aggr_params(port);
710 spin_unlock_bh(&port->agg_lock);