blob: e31626ffccd18ef2b1a7303ab1c1ef2ef38e9d51 [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Steffen Klassert7785bba2017-02-15 09:40:00 +01002/*
3 * IPV6 GSO/GRO offload support
4 * Linux INET implementation
5 *
6 * Copyright (C) 2016 secunet Security Networks AG
7 * Author: Steffen Klassert <steffen.klassert@secunet.com>
8 *
Steffen Klassert7785bba2017-02-15 09:40:00 +01009 * ESP GRO support
10 */
11
12#include <linux/skbuff.h>
13#include <linux/init.h>
14#include <net/protocol.h>
15#include <crypto/aead.h>
16#include <crypto/authenc.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <net/ip.h>
20#include <net/xfrm.h>
21#include <net/esp.h>
22#include <linux/scatterlist.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <net/ip6_route.h>
27#include <net/ipv6.h>
28#include <linux/icmpv6.h>
29
Yossi Kupermanca3a1b82017-06-22 11:37:11 +030030static __u16 esp6_nexthdr_esp_offset(struct ipv6hdr *ipv6_hdr, int nhlen)
31{
32 int off = sizeof(struct ipv6hdr);
33 struct ipv6_opt_hdr *exthdr;
34
35 if (likely(ipv6_hdr->nexthdr == NEXTHDR_ESP))
36 return offsetof(struct ipv6hdr, nexthdr);
37
38 while (off < nhlen) {
39 exthdr = (void *)ipv6_hdr + off;
40 if (exthdr->nexthdr == NEXTHDR_ESP)
41 return off;
42
43 off += ipv6_optlen(exthdr);
44 }
45
46 return 0;
47}
48
David Millerd4546c22018-06-24 14:13:49 +090049static struct sk_buff *esp6_gro_receive(struct list_head *head,
50 struct sk_buff *skb)
Steffen Klassert7785bba2017-02-15 09:40:00 +010051{
52 int offset = skb_gro_offset(skb);
53 struct xfrm_offload *xo;
54 struct xfrm_state *x;
55 __be32 seq;
56 __be32 spi;
Yossi Kupermanca3a1b82017-06-22 11:37:11 +030057 int nhoff;
Steffen Klassert7785bba2017-02-15 09:40:00 +010058 int err;
59
Steffen Klassert374d1b52018-01-05 08:35:47 +010060 if (!pskb_pull(skb, offset))
61 return NULL;
Steffen Klassert7785bba2017-02-15 09:40:00 +010062
63 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
64 goto out;
65
Steffen Klassert7785bba2017-02-15 09:40:00 +010066 xo = xfrm_offload(skb);
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020067 if (!xo || !(xo->flags & CRYPTO_DONE)) {
Florian Westphal0ca64da2018-12-18 17:15:18 +010068 struct sec_path *sp = secpath_set(skb);
69
70 if (!sp)
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020071 goto out;
72
Florian Westphal0ca64da2018-12-18 17:15:18 +010073 if (sp->len == XFRM_MAX_DEPTH)
Myungho Jung6ed69182019-03-07 10:23:08 +090074 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020075
76 x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
77 (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
78 spi, IPPROTO_ESP, AF_INET6);
79 if (!x)
Myungho Jung6ed69182019-03-07 10:23:08 +090080 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020081
Florian Westphal0ca64da2018-12-18 17:15:18 +010082 sp->xvec[sp->len++] = x;
83 sp->olen++;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020084
85 xo = xfrm_offload(skb);
86 if (!xo) {
87 xfrm_state_put(x);
Myungho Jung6ed69182019-03-07 10:23:08 +090088 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020089 }
Steffen Klassert7785bba2017-02-15 09:40:00 +010090 }
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020091
Steffen Klassert7785bba2017-02-15 09:40:00 +010092 xo->flags |= XFRM_GRO;
93
Yossi Kupermanca3a1b82017-06-22 11:37:11 +030094 nhoff = esp6_nexthdr_esp_offset(ipv6_hdr(skb), offset);
95 if (!nhoff)
96 goto out;
97
98 IP6CB(skb)->nhoff = nhoff;
Steffen Klassert7785bba2017-02-15 09:40:00 +010099 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
100 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
101 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
102 XFRM_SPI_SKB_CB(skb)->seq = seq;
103
104 /* We don't need to handle errors from xfrm_input, it does all
105 * the error handling and frees the resources on error. */
106 xfrm_input(skb, IPPROTO_ESP, spi, -2);
107
108 return ERR_PTR(-EINPROGRESS);
Myungho Jung6ed69182019-03-07 10:23:08 +0900109out_reset:
110 secpath_reset(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +0100111out:
112 skb_push(skb, offset);
113 NAPI_GRO_CB(skb)->same_flow = 0;
114 NAPI_GRO_CB(skb)->flush = 1;
115
116 return NULL;
117}
118
Steffen Klassert7862b402017-04-14 10:06:50 +0200119static void esp6_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
120{
121 struct ip_esp_hdr *esph;
122 struct ipv6hdr *iph = ipv6_hdr(skb);
123 struct xfrm_offload *xo = xfrm_offload(skb);
124 int proto = iph->nexthdr;
125
126 skb_push(skb, -skb_network_offset(skb));
127 esph = ip_esp_hdr(skb);
128 *skb_mac_header(skb) = IPPROTO_ESP;
129
130 esph->spi = x->id.spi;
131 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
132
133 xo->proto = proto;
134}
135
Florian Westphal7613b922019-03-29 21:16:27 +0100136static struct sk_buff *xfrm6_tunnel_gso_segment(struct xfrm_state *x,
137 struct sk_buff *skb,
138 netdev_features_t features)
139{
140 __skb_push(skb, skb->mac_len);
141 return skb_mac_gso_segment(skb, features);
142}
143
144static struct sk_buff *xfrm6_transport_gso_segment(struct xfrm_state *x,
145 struct sk_buff *skb,
146 netdev_features_t features)
147{
148 const struct net_offload *ops;
149 struct sk_buff *segs = ERR_PTR(-EINVAL);
150 struct xfrm_offload *xo = xfrm_offload(skb);
151
152 skb->transport_header += x->props.header_len;
153 ops = rcu_dereference(inet6_offloads[xo->proto]);
154 if (likely(ops && ops->callbacks.gso_segment))
155 segs = ops->callbacks.gso_segment(skb, features);
156
157 return segs;
158}
159
160static struct sk_buff *xfrm6_outer_mode_gso_segment(struct xfrm_state *x,
161 struct sk_buff *skb,
162 netdev_features_t features)
163{
Florian Westphalc9500d72019-03-29 21:16:32 +0100164 switch (x->outer_mode.encap) {
Florian Westphal7613b922019-03-29 21:16:27 +0100165 case XFRM_MODE_TUNNEL:
166 return xfrm6_tunnel_gso_segment(x, skb, features);
167 case XFRM_MODE_TRANSPORT:
168 return xfrm6_transport_gso_segment(x, skb, features);
169 }
170
171 return ERR_PTR(-EOPNOTSUPP);
172}
173
Steffen Klassert7862b402017-04-14 10:06:50 +0200174static struct sk_buff *esp6_gso_segment(struct sk_buff *skb,
175 netdev_features_t features)
176{
Steffen Klassert7862b402017-04-14 10:06:50 +0200177 struct xfrm_state *x;
178 struct ip_esp_hdr *esph;
179 struct crypto_aead *aead;
Steffen Klassert7862b402017-04-14 10:06:50 +0200180 netdev_features_t esp_features = features;
181 struct xfrm_offload *xo = xfrm_offload(skb);
Florian Westphal2294be0f2018-12-18 17:15:20 +0100182 struct sec_path *sp;
Steffen Klassert7862b402017-04-14 10:06:50 +0200183
Colin Ian Kingffa6f572017-04-18 15:06:53 +0100184 if (!xo)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100185 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200186
Willem de Bruijn121d57a2018-01-19 09:29:18 -0500187 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
David S. Miller5ca11442018-01-23 13:49:06 -0500188 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200189
Florian Westphal2294be0f2018-12-18 17:15:20 +0100190 sp = skb_sec_path(skb);
191 x = sp->xvec[sp->len - 1];
Steffen Klassert7862b402017-04-14 10:06:50 +0200192 aead = x->data;
193 esph = ip_esp_hdr(skb);
194
195 if (esph->spi != x->id.spi)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100196 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200197
198 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100199 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200200
201 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
202
203 skb->encap_hdr_csum = 1;
204
Shannon Nelsonfcb662d2018-06-26 14:19:10 -0700205 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev)
Steffen Klassert7862b402017-04-14 10:06:50 +0200206 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
Shannon Nelson5211fcf2018-02-26 14:28:19 -0800207 else if (!(features & NETIF_F_HW_ESP_TX_CSUM))
208 esp_features = features & ~NETIF_F_CSUM_MASK;
Steffen Klassert7862b402017-04-14 10:06:50 +0200209
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100210 xo->flags |= XFRM_GSO_SEGMENT;
Steffen Klassert7862b402017-04-14 10:06:50 +0200211
Florian Westphal7613b922019-03-29 21:16:27 +0100212 return xfrm6_outer_mode_gso_segment(x, skb, esp_features);
Steffen Klassert7862b402017-04-14 10:06:50 +0200213}
214
Steffen Klassert383d0352017-04-14 10:06:42 +0200215static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb)
216{
217 struct crypto_aead *aead = x->data;
Ilan Tayarie51a6472017-08-01 12:49:05 +0300218 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassert383d0352017-04-14 10:06:42 +0200219
220 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
221 return -EINVAL;
222
Ilan Tayarie51a6472017-08-01 12:49:05 +0300223 if (!(xo->flags & CRYPTO_DONE))
224 skb->ip_summed = CHECKSUM_NONE;
Steffen Klassert383d0352017-04-14 10:06:42 +0200225
226 return esp6_input_done2(skb, 0);
227}
228
229static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
230{
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100231 int len;
Steffen Klassert383d0352017-04-14 10:06:42 +0200232 int err;
233 int alen;
234 int blksize;
235 struct xfrm_offload *xo;
236 struct ip_esp_hdr *esph;
237 struct crypto_aead *aead;
238 struct esp_info esp;
239 bool hw_offload = true;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100240 __u32 seq;
Steffen Klassert383d0352017-04-14 10:06:42 +0200241
242 esp.inplace = true;
243
244 xo = xfrm_offload(skb);
245
246 if (!xo)
247 return -EINVAL;
248
Shannon Nelsonfcb662d2018-06-26 14:19:10 -0700249 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) {
Steffen Klassert383d0352017-04-14 10:06:42 +0200250 xo->flags |= CRYPTO_FALLBACK;
Ilan Tayari8f92e032017-04-19 08:41:01 +0300251 hw_offload = false;
Steffen Klassert383d0352017-04-14 10:06:42 +0200252 }
253
254 esp.proto = xo->proto;
255
256 /* skb is pure payload to encrypt */
257
258 aead = x->data;
259 alen = crypto_aead_authsize(aead);
260
261 esp.tfclen = 0;
262 /* XXX: Add support for tfc padding here. */
263
264 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
265 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
266 esp.plen = esp.clen - skb->len - esp.tfclen;
267 esp.tailen = esp.tfclen + esp.plen + alen;
268
269 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
270 esp.nfrags = esp6_output_head(x, skb, &esp);
271 if (esp.nfrags < 0)
272 return esp.nfrags;
273 }
274
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100275 seq = xo->seq.low;
276
Steffen Klassert383d0352017-04-14 10:06:42 +0200277 esph = ip_esp_hdr(skb);
278 esph->spi = x->id.spi;
279
280 skb_push(skb, -skb_network_offset(skb));
281
282 if (xo->flags & XFRM_GSO_SEGMENT) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100283 esph->seq_no = htonl(seq);
Steffen Klassert383d0352017-04-14 10:06:42 +0200284
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100285 if (!skb_is_gso(skb))
286 xo->seq.low++;
287 else
288 xo->seq.low += skb_shinfo(skb)->gso_segs;
Steffen Klassert383d0352017-04-14 10:06:42 +0200289 }
290
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100291 esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32));
292
293 len = skb->len - sizeof(struct ipv6hdr);
294 if (len > IPV6_MAXPLEN)
295 len = 0;
296
297 ipv6_hdr(skb)->payload_len = htons(len);
298
Ilan Tayari8f92e032017-04-19 08:41:01 +0300299 if (hw_offload)
Steffen Klassert383d0352017-04-14 10:06:42 +0200300 return 0;
301
Steffen Klassert383d0352017-04-14 10:06:42 +0200302 err = esp6_output_tail(x, skb, &esp);
Steffen Klassert4ff03082017-08-07 08:31:07 +0200303 if (err)
Steffen Klassert383d0352017-04-14 10:06:42 +0200304 return err;
305
306 secpath_reset(skb);
307
308 return 0;
309}
310
Steffen Klassert7785bba2017-02-15 09:40:00 +0100311static const struct net_offload esp6_offload = {
312 .callbacks = {
313 .gro_receive = esp6_gro_receive,
Steffen Klassert7862b402017-04-14 10:06:50 +0200314 .gso_segment = esp6_gso_segment,
Steffen Klassert7785bba2017-02-15 09:40:00 +0100315 },
316};
317
Steffen Klassert383d0352017-04-14 10:06:42 +0200318static const struct xfrm_type_offload esp6_type_offload = {
319 .description = "ESP6 OFFLOAD",
320 .owner = THIS_MODULE,
321 .proto = IPPROTO_ESP,
322 .input_tail = esp6_input_tail,
323 .xmit = esp6_xmit,
Steffen Klassert7862b402017-04-14 10:06:50 +0200324 .encap = esp6_gso_encap,
Steffen Klassert383d0352017-04-14 10:06:42 +0200325};
326
Steffen Klassert7785bba2017-02-15 09:40:00 +0100327static int __init esp6_offload_init(void)
328{
Steffen Klassert383d0352017-04-14 10:06:42 +0200329 if (xfrm_register_type_offload(&esp6_type_offload, AF_INET6) < 0) {
330 pr_info("%s: can't add xfrm type offload\n", __func__);
331 return -EAGAIN;
332 }
333
Steffen Klassert7785bba2017-02-15 09:40:00 +0100334 return inet6_add_offload(&esp6_offload, IPPROTO_ESP);
335}
336
337static void __exit esp6_offload_exit(void)
338{
Florian Westphal4f518e82019-05-03 17:46:19 +0200339 xfrm_unregister_type_offload(&esp6_type_offload, AF_INET6);
Steffen Klassert7785bba2017-02-15 09:40:00 +0100340 inet6_del_offload(&esp6_offload, IPPROTO_ESP);
341}
342
343module_init(esp6_offload_init);
344module_exit(esp6_offload_exit);
345MODULE_LICENSE("GPL");
346MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
Ilan Tayariffdb5212017-08-01 12:49:08 +0300347MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET6, XFRM_PROTO_ESP);