Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 2 | /* |
| 3 | * sctp_offload - GRO/GSO Offloading for SCTP |
| 4 | * |
| 5 | * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/kprobes.h> |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/sctp.h> |
| 14 | #include <linux/proc_fs.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kfifo.h> |
| 18 | #include <linux/time.h> |
| 19 | #include <net/net_namespace.h> |
| 20 | |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <net/sctp/sctp.h> |
| 23 | #include <net/sctp/checksum.h> |
| 24 | #include <net/protocol.h> |
| 25 | |
| 26 | static __le32 sctp_gso_make_checksum(struct sk_buff *skb) |
| 27 | { |
| 28 | skb->ip_summed = CHECKSUM_NONE; |
Davide Caratti | dba0030 | 2017-05-18 15:44:40 +0200 | [diff] [blame] | 29 | skb->csum_not_inet = 0; |
Xin Long | 527beb8 | 2020-10-29 15:04:57 +0800 | [diff] [blame] | 30 | /* csum and csum_start in GSO CB may be needed to do the UDP |
| 31 | * checksum when it's a UDP tunneling packet. |
| 32 | */ |
| 33 | SKB_GSO_CB(skb)->csum = (__force __wsum)~0; |
| 34 | SKB_GSO_CB(skb)->csum_start = skb_headroom(skb) + skb->len; |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 35 | return sctp_compute_cksum(skb, skb_transport_offset(skb)); |
| 36 | } |
| 37 | |
| 38 | static struct sk_buff *sctp_gso_segment(struct sk_buff *skb, |
| 39 | netdev_features_t features) |
| 40 | { |
| 41 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 42 | struct sctphdr *sh; |
| 43 | |
Daniel Axtens | 1dd27cd | 2018-03-09 14:06:09 +1100 | [diff] [blame] | 44 | if (!skb_is_gso_sctp(skb)) |
Willem de Bruijn | 121d57a | 2018-01-19 09:29:18 -0500 | [diff] [blame] | 45 | goto out; |
| 46 | |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 47 | sh = sctp_hdr(skb); |
| 48 | if (!pskb_may_pull(skb, sizeof(*sh))) |
| 49 | goto out; |
| 50 | |
| 51 | __skb_pull(skb, sizeof(*sh)); |
| 52 | |
| 53 | if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { |
| 54 | /* Packet is from an untrusted source, reset gso_segs. */ |
| 55 | struct skb_shared_info *pinfo = skb_shinfo(skb); |
| 56 | struct sk_buff *frag_iter; |
| 57 | |
| 58 | pinfo->gso_segs = 0; |
| 59 | if (skb->len != skb->data_len) { |
| 60 | /* Means we have chunks in here too */ |
| 61 | pinfo->gso_segs++; |
| 62 | } |
| 63 | |
| 64 | skb_walk_frags(skb, frag_iter) |
| 65 | pinfo->gso_segs++; |
| 66 | |
| 67 | segs = NULL; |
| 68 | goto out; |
| 69 | } |
| 70 | |
Xin Long | 1fef854 | 2021-01-15 17:36:39 +0800 | [diff] [blame] | 71 | segs = skb_segment(skb, (features | NETIF_F_HW_CSUM) & ~NETIF_F_SG); |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 72 | if (IS_ERR(segs)) |
| 73 | goto out; |
| 74 | |
| 75 | /* All that is left is update SCTP CRC if necessary */ |
| 76 | if (!(features & NETIF_F_SCTP_CRC)) { |
| 77 | for (skb = segs; skb; skb = skb->next) { |
| 78 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 79 | sh = sctp_hdr(skb); |
| 80 | sh->checksum = sctp_gso_make_checksum(skb); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | out: |
| 86 | return segs; |
| 87 | } |
| 88 | |
| 89 | static const struct net_offload sctp_offload = { |
| 90 | .callbacks = { |
| 91 | .gso_segment = sctp_gso_segment, |
| 92 | }, |
| 93 | }; |
| 94 | |
Marcelo Ricardo Leitner | c5c4e45 | 2016-07-15 16:40:02 -0300 | [diff] [blame] | 95 | static const struct net_offload sctp6_offload = { |
| 96 | .callbacks = { |
| 97 | .gso_segment = sctp_gso_segment, |
| 98 | }, |
| 99 | }; |
| 100 | |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 101 | int __init sctp_offload_init(void) |
| 102 | { |
Marcelo Ricardo Leitner | c5c4e45 | 2016-07-15 16:40:02 -0300 | [diff] [blame] | 103 | int ret; |
| 104 | |
| 105 | ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP); |
| 106 | if (ret) |
| 107 | goto out; |
| 108 | |
| 109 | ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP); |
| 110 | if (ret) |
| 111 | goto ipv4; |
| 112 | |
Matteo Croce | c3e933a | 2019-05-29 17:39:41 +0200 | [diff] [blame] | 113 | crc32c_csum_stub = &sctp_csum_ops; |
Marcelo Ricardo Leitner | c5c4e45 | 2016-07-15 16:40:02 -0300 | [diff] [blame] | 114 | return ret; |
| 115 | |
| 116 | ipv4: |
| 117 | inet_del_offload(&sctp_offload, IPPROTO_SCTP); |
| 118 | out: |
| 119 | return ret; |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 120 | } |