1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
290017accSMarcelo Ricardo Leitner /*
390017accSMarcelo Ricardo Leitner * sctp_offload - GRO/GSO Offloading for SCTP
490017accSMarcelo Ricardo Leitner *
590017accSMarcelo Ricardo Leitner * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
690017accSMarcelo Ricardo Leitner */
790017accSMarcelo Ricardo Leitner
890017accSMarcelo Ricardo Leitner #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
990017accSMarcelo Ricardo Leitner
1090017accSMarcelo Ricardo Leitner #include <linux/kernel.h>
1190017accSMarcelo Ricardo Leitner #include <linux/kprobes.h>
1290017accSMarcelo Ricardo Leitner #include <linux/socket.h>
1390017accSMarcelo Ricardo Leitner #include <linux/sctp.h>
1490017accSMarcelo Ricardo Leitner #include <linux/proc_fs.h>
1590017accSMarcelo Ricardo Leitner #include <linux/vmalloc.h>
1690017accSMarcelo Ricardo Leitner #include <linux/module.h>
1790017accSMarcelo Ricardo Leitner #include <linux/kfifo.h>
1890017accSMarcelo Ricardo Leitner #include <linux/time.h>
1990017accSMarcelo Ricardo Leitner #include <net/net_namespace.h>
2090017accSMarcelo Ricardo Leitner
2190017accSMarcelo Ricardo Leitner #include <linux/skbuff.h>
2290017accSMarcelo Ricardo Leitner #include <net/sctp/sctp.h>
2390017accSMarcelo Ricardo Leitner #include <net/sctp/checksum.h>
2490017accSMarcelo Ricardo Leitner #include <net/protocol.h>
25*d457a0e3SEric Dumazet #include <net/gso.h>
2690017accSMarcelo Ricardo Leitner
sctp_gso_make_checksum(struct sk_buff * skb)2790017accSMarcelo Ricardo Leitner static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
2890017accSMarcelo Ricardo Leitner {
2990017accSMarcelo Ricardo Leitner skb->ip_summed = CHECKSUM_NONE;
30dba00306SDavide Caratti skb->csum_not_inet = 0;
31527beb8eSXin Long /* csum and csum_start in GSO CB may be needed to do the UDP
32527beb8eSXin Long * checksum when it's a UDP tunneling packet.
33527beb8eSXin Long */
34527beb8eSXin Long SKB_GSO_CB(skb)->csum = (__force __wsum)~0;
35527beb8eSXin Long SKB_GSO_CB(skb)->csum_start = skb_headroom(skb) + skb->len;
3690017accSMarcelo Ricardo Leitner return sctp_compute_cksum(skb, skb_transport_offset(skb));
3790017accSMarcelo Ricardo Leitner }
3890017accSMarcelo Ricardo Leitner
sctp_gso_segment(struct sk_buff * skb,netdev_features_t features)3990017accSMarcelo Ricardo Leitner static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
4090017accSMarcelo Ricardo Leitner netdev_features_t features)
4190017accSMarcelo Ricardo Leitner {
4290017accSMarcelo Ricardo Leitner struct sk_buff *segs = ERR_PTR(-EINVAL);
4390017accSMarcelo Ricardo Leitner struct sctphdr *sh;
4490017accSMarcelo Ricardo Leitner
451dd27cdeSDaniel Axtens if (!skb_is_gso_sctp(skb))
46121d57afSWillem de Bruijn goto out;
47121d57afSWillem de Bruijn
4890017accSMarcelo Ricardo Leitner sh = sctp_hdr(skb);
4990017accSMarcelo Ricardo Leitner if (!pskb_may_pull(skb, sizeof(*sh)))
5090017accSMarcelo Ricardo Leitner goto out;
5190017accSMarcelo Ricardo Leitner
5290017accSMarcelo Ricardo Leitner __skb_pull(skb, sizeof(*sh));
5390017accSMarcelo Ricardo Leitner
5490017accSMarcelo Ricardo Leitner if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
5590017accSMarcelo Ricardo Leitner /* Packet is from an untrusted source, reset gso_segs. */
5690017accSMarcelo Ricardo Leitner struct skb_shared_info *pinfo = skb_shinfo(skb);
5790017accSMarcelo Ricardo Leitner struct sk_buff *frag_iter;
5890017accSMarcelo Ricardo Leitner
5990017accSMarcelo Ricardo Leitner pinfo->gso_segs = 0;
6090017accSMarcelo Ricardo Leitner if (skb->len != skb->data_len) {
6190017accSMarcelo Ricardo Leitner /* Means we have chunks in here too */
6290017accSMarcelo Ricardo Leitner pinfo->gso_segs++;
6390017accSMarcelo Ricardo Leitner }
6490017accSMarcelo Ricardo Leitner
6590017accSMarcelo Ricardo Leitner skb_walk_frags(skb, frag_iter)
6690017accSMarcelo Ricardo Leitner pinfo->gso_segs++;
6790017accSMarcelo Ricardo Leitner
6890017accSMarcelo Ricardo Leitner segs = NULL;
6990017accSMarcelo Ricardo Leitner goto out;
7090017accSMarcelo Ricardo Leitner }
7190017accSMarcelo Ricardo Leitner
721fef8544SXin Long segs = skb_segment(skb, (features | NETIF_F_HW_CSUM) & ~NETIF_F_SG);
7390017accSMarcelo Ricardo Leitner if (IS_ERR(segs))
7490017accSMarcelo Ricardo Leitner goto out;
7590017accSMarcelo Ricardo Leitner
7690017accSMarcelo Ricardo Leitner /* All that is left is update SCTP CRC if necessary */
7790017accSMarcelo Ricardo Leitner if (!(features & NETIF_F_SCTP_CRC)) {
7890017accSMarcelo Ricardo Leitner for (skb = segs; skb; skb = skb->next) {
7990017accSMarcelo Ricardo Leitner if (skb->ip_summed == CHECKSUM_PARTIAL) {
8090017accSMarcelo Ricardo Leitner sh = sctp_hdr(skb);
8190017accSMarcelo Ricardo Leitner sh->checksum = sctp_gso_make_checksum(skb);
8290017accSMarcelo Ricardo Leitner }
8390017accSMarcelo Ricardo Leitner }
8490017accSMarcelo Ricardo Leitner }
8590017accSMarcelo Ricardo Leitner
8690017accSMarcelo Ricardo Leitner out:
8790017accSMarcelo Ricardo Leitner return segs;
8890017accSMarcelo Ricardo Leitner }
8990017accSMarcelo Ricardo Leitner
9090017accSMarcelo Ricardo Leitner static const struct net_offload sctp_offload = {
9190017accSMarcelo Ricardo Leitner .callbacks = {
9290017accSMarcelo Ricardo Leitner .gso_segment = sctp_gso_segment,
9390017accSMarcelo Ricardo Leitner },
9490017accSMarcelo Ricardo Leitner };
9590017accSMarcelo Ricardo Leitner
96c5c4e45cSMarcelo Ricardo Leitner static const struct net_offload sctp6_offload = {
97c5c4e45cSMarcelo Ricardo Leitner .callbacks = {
98c5c4e45cSMarcelo Ricardo Leitner .gso_segment = sctp_gso_segment,
99c5c4e45cSMarcelo Ricardo Leitner },
100c5c4e45cSMarcelo Ricardo Leitner };
101c5c4e45cSMarcelo Ricardo Leitner
sctp_offload_init(void)10290017accSMarcelo Ricardo Leitner int __init sctp_offload_init(void)
10390017accSMarcelo Ricardo Leitner {
104c5c4e45cSMarcelo Ricardo Leitner int ret;
105c5c4e45cSMarcelo Ricardo Leitner
106c5c4e45cSMarcelo Ricardo Leitner ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
107c5c4e45cSMarcelo Ricardo Leitner if (ret)
108c5c4e45cSMarcelo Ricardo Leitner goto out;
109c5c4e45cSMarcelo Ricardo Leitner
110c5c4e45cSMarcelo Ricardo Leitner ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
111c5c4e45cSMarcelo Ricardo Leitner if (ret)
112c5c4e45cSMarcelo Ricardo Leitner goto ipv4;
113c5c4e45cSMarcelo Ricardo Leitner
114c3e933a5SMatteo Croce crc32c_csum_stub = &sctp_csum_ops;
115c5c4e45cSMarcelo Ricardo Leitner return ret;
116c5c4e45cSMarcelo Ricardo Leitner
117c5c4e45cSMarcelo Ricardo Leitner ipv4:
118c5c4e45cSMarcelo Ricardo Leitner inet_del_offload(&sctp_offload, IPPROTO_SCTP);
119c5c4e45cSMarcelo Ricardo Leitner out:
120c5c4e45cSMarcelo Ricardo Leitner return ret;
12190017accSMarcelo Ricardo Leitner }
122