xref: /openbmc/linux/net/sctp/offload.c (revision 3557b3fd)
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 
35 static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
36 {
37 	skb->ip_summed = CHECKSUM_NONE;
38 	skb->csum_not_inet = 0;
39 	gso_reset_checksum(skb, ~0);
40 	return sctp_compute_cksum(skb, skb_transport_offset(skb));
41 }
42 
43 static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
44 					netdev_features_t features)
45 {
46 	struct sk_buff *segs = ERR_PTR(-EINVAL);
47 	struct sctphdr *sh;
48 
49 	if (!skb_is_gso_sctp(skb))
50 		goto out;
51 
52 	sh = sctp_hdr(skb);
53 	if (!pskb_may_pull(skb, sizeof(*sh)))
54 		goto out;
55 
56 	__skb_pull(skb, sizeof(*sh));
57 
58 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
59 		/* Packet is from an untrusted source, reset gso_segs. */
60 		struct skb_shared_info *pinfo = skb_shinfo(skb);
61 		struct sk_buff *frag_iter;
62 
63 		pinfo->gso_segs = 0;
64 		if (skb->len != skb->data_len) {
65 			/* Means we have chunks in here too */
66 			pinfo->gso_segs++;
67 		}
68 
69 		skb_walk_frags(skb, frag_iter)
70 			pinfo->gso_segs++;
71 
72 		segs = NULL;
73 		goto out;
74 	}
75 
76 	segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
77 	if (IS_ERR(segs))
78 		goto out;
79 
80 	/* All that is left is update SCTP CRC if necessary */
81 	if (!(features & NETIF_F_SCTP_CRC)) {
82 		for (skb = segs; skb; skb = skb->next) {
83 			if (skb->ip_summed == CHECKSUM_PARTIAL) {
84 				sh = sctp_hdr(skb);
85 				sh->checksum = sctp_gso_make_checksum(skb);
86 			}
87 		}
88 	}
89 
90 out:
91 	return segs;
92 }
93 
94 static const struct net_offload sctp_offload = {
95 	.callbacks = {
96 		.gso_segment = sctp_gso_segment,
97 	},
98 };
99 
100 static const struct net_offload sctp6_offload = {
101 	.callbacks = {
102 		.gso_segment = sctp_gso_segment,
103 	},
104 };
105 
106 static const struct skb_checksum_ops crc32c_csum_ops = {
107 	.update  = sctp_csum_update,
108 	.combine = sctp_csum_combine,
109 };
110 
111 int __init sctp_offload_init(void)
112 {
113 	int ret;
114 
115 	ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
116 	if (ret)
117 		goto out;
118 
119 	ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
120 	if (ret)
121 		goto ipv4;
122 
123 	crc32c_csum_stub = &crc32c_csum_ops;
124 	return ret;
125 
126 ipv4:
127 	inet_del_offload(&sctp_offload, IPPROTO_SCTP);
128 out:
129 	return ret;
130 }
131