1 /*
2 * sctp_offload - GRO/GSO Offloading for SCTP
3 *
4 * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/kernel.h>
20 #include <linux/kprobes.h>
21 #include <linux/socket.h>
22 #include <linux/sctp.h>
23 #include <linux/proc_fs.h>
24 #include <linux/vmalloc.h>
25 #include <linux/module.h>
26 #include <linux/kfifo.h>
27 #include <linux/time.h>
28 #include <net/net_namespace.h>
29
30 #include <linux/skbuff.h>
31 #include <net/sctp/sctp.h>
32 #include <net/sctp/checksum.h>
33 #include <net/protocol.h>
34
sctp_gso_make_checksum(struct sk_buff * skb)35 static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
36 {
37 skb->ip_summed = CHECKSUM_NONE;
38 return sctp_compute_cksum(skb, skb_transport_offset(skb));
39 }
40
sctp_gso_segment(struct sk_buff * skb,netdev_features_t features)41 static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
42 netdev_features_t features)
43 {
44 struct sk_buff *segs = ERR_PTR(-EINVAL);
45 struct sctphdr *sh;
46
47 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP))
48 goto out;
49
50 sh = sctp_hdr(skb);
51 if (!pskb_may_pull(skb, sizeof(*sh)))
52 goto out;
53
54 __skb_pull(skb, sizeof(*sh));
55
56 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
57 /* Packet is from an untrusted source, reset gso_segs. */
58 struct skb_shared_info *pinfo = skb_shinfo(skb);
59 struct sk_buff *frag_iter;
60
61 pinfo->gso_segs = 0;
62 if (skb->len != skb->data_len) {
63 /* Means we have chunks in here too */
64 pinfo->gso_segs++;
65 }
66
67 skb_walk_frags(skb, frag_iter)
68 pinfo->gso_segs++;
69
70 segs = NULL;
71 goto out;
72 }
73
74 segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
75 if (IS_ERR(segs))
76 goto out;
77
78 /* All that is left is update SCTP CRC if necessary */
79 if (!(features & NETIF_F_SCTP_CRC)) {
80 for (skb = segs; skb; skb = skb->next) {
81 if (skb->ip_summed == CHECKSUM_PARTIAL) {
82 sh = sctp_hdr(skb);
83 sh->checksum = sctp_gso_make_checksum(skb);
84 }
85 }
86 }
87
88 out:
89 return segs;
90 }
91
92 static const struct net_offload sctp_offload = {
93 .callbacks = {
94 .gso_segment = sctp_gso_segment,
95 },
96 };
97
98 static const struct net_offload sctp6_offload = {
99 .callbacks = {
100 .gso_segment = sctp_gso_segment,
101 },
102 };
103
sctp_offload_init(void)104 int __init sctp_offload_init(void)
105 {
106 int ret;
107
108 ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
109 if (ret)
110 goto out;
111
112 ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
113 if (ret)
114 goto ipv4;
115
116 return ret;
117
118 ipv4:
119 inet_del_offload(&sctp_offload, IPPROTO_SCTP);
120 out:
121 return ret;
122 }
123