1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sctp_offload - GRO/GSO Offloading for SCTP
5 * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
21 #include <linux/skbuff.h>
22 #include <net/sctp/sctp.h>
23 #include <net/sctp/checksum.h>
24 #include <net/protocol.h>
27 static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
29 skb->ip_summed = CHECKSUM_NONE;
30 skb->csum_not_inet = 0;
31 /* csum and csum_start in GSO CB may be needed to do the UDP
32 * checksum when it's a UDP tunneling packet.
34 SKB_GSO_CB(skb)->csum = (__force __wsum)~0;
35 SKB_GSO_CB(skb)->csum_start = skb_headroom(skb) + skb->len;
36 return sctp_compute_cksum(skb, skb_transport_offset(skb));
39 static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
40 netdev_features_t features)
42 struct sk_buff *segs = ERR_PTR(-EINVAL);
45 if (!skb_is_gso_sctp(skb))
49 if (!pskb_may_pull(skb, sizeof(*sh)))
52 __skb_pull(skb, sizeof(*sh));
54 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
55 /* Packet is from an untrusted source, reset gso_segs. */
56 struct skb_shared_info *pinfo = skb_shinfo(skb);
57 struct sk_buff *frag_iter;
60 if (skb->len != skb->data_len) {
61 /* Means we have chunks in here too */
65 skb_walk_frags(skb, frag_iter)
72 segs = skb_segment(skb, (features | NETIF_F_HW_CSUM) & ~NETIF_F_SG);
76 /* All that is left is update SCTP CRC if necessary */
77 if (!(features & NETIF_F_SCTP_CRC)) {
78 for (skb = segs; skb; skb = skb->next) {
79 if (skb->ip_summed == CHECKSUM_PARTIAL) {
81 sh->checksum = sctp_gso_make_checksum(skb);
90 static const struct net_offload sctp_offload = {
92 .gso_segment = sctp_gso_segment,
96 static const struct net_offload sctp6_offload = {
98 .gso_segment = sctp_gso_segment,
102 int __init sctp_offload_init(void)
106 ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
110 ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
114 crc32c_csum_stub = &sctp_csum_ops;
118 inet_del_offload(&sctp_offload, IPPROTO_SCTP);