blob: 32fbd9ba3609079c65aa6ac0e48b638e4b2e4d67 [file] [log] [blame]
Steffen Klassert7785bba2017-02-15 09:40:00 +01001/*
2 * IPV4 GSO/GRO offload support
3 * Linux INET implementation
4 *
5 * Copyright (C) 2016 secunet Security Networks AG
6 * Author: Steffen Klassert <steffen.klassert@secunet.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * ESP GRO support
13 */
14
15#include <linux/skbuff.h>
16#include <linux/init.h>
17#include <net/protocol.h>
18#include <crypto/aead.h>
19#include <crypto/authenc.h>
20#include <linux/err.h>
21#include <linux/module.h>
22#include <net/ip.h>
23#include <net/xfrm.h>
24#include <net/esp.h>
25#include <linux/scatterlist.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <net/udp.h>
30
31static struct sk_buff **esp4_gro_receive(struct sk_buff **head,
32 struct sk_buff *skb)
33{
34 int offset = skb_gro_offset(skb);
35 struct xfrm_offload *xo;
36 struct xfrm_state *x;
37 __be32 seq;
38 __be32 spi;
39 int err;
40
Steffen Klassert374d1b52018-01-05 08:35:47 +010041 if (!pskb_pull(skb, offset))
42 return NULL;
Steffen Klassert7785bba2017-02-15 09:40:00 +010043
44 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
45 goto out;
46
Steffen Klassert7785bba2017-02-15 09:40:00 +010047 xo = xfrm_offload(skb);
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020048 if (!xo || !(xo->flags & CRYPTO_DONE)) {
49 err = secpath_set(skb);
50 if (err)
51 goto out;
52
53 if (skb->sp->len == XFRM_MAX_DEPTH)
54 goto out;
55
56 x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
57 (xfrm_address_t *)&ip_hdr(skb)->daddr,
58 spi, IPPROTO_ESP, AF_INET);
59 if (!x)
60 goto out;
61
62 skb->sp->xvec[skb->sp->len++] = x;
63 skb->sp->olen++;
64
65 xo = xfrm_offload(skb);
66 if (!xo) {
67 xfrm_state_put(x);
68 goto out;
69 }
Steffen Klassert7785bba2017-02-15 09:40:00 +010070 }
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020071
Steffen Klassert7785bba2017-02-15 09:40:00 +010072 xo->flags |= XFRM_GRO;
73
74 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
75 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
76 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
77 XFRM_SPI_SKB_CB(skb)->seq = seq;
78
79 /* We don't need to handle errors from xfrm_input, it does all
80 * the error handling and frees the resources on error. */
81 xfrm_input(skb, IPPROTO_ESP, spi, -2);
82
83 return ERR_PTR(-EINPROGRESS);
84out:
85 skb_push(skb, offset);
86 NAPI_GRO_CB(skb)->same_flow = 0;
87 NAPI_GRO_CB(skb)->flush = 1;
88
89 return NULL;
90}
91
Steffen Klassert7862b402017-04-14 10:06:50 +020092static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
93{
94 struct ip_esp_hdr *esph;
95 struct iphdr *iph = ip_hdr(skb);
96 struct xfrm_offload *xo = xfrm_offload(skb);
97 int proto = iph->protocol;
98
99 skb_push(skb, -skb_network_offset(skb));
100 esph = ip_esp_hdr(skb);
101 *skb_mac_header(skb) = IPPROTO_ESP;
102
103 esph->spi = x->id.spi;
104 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
105
106 xo->proto = proto;
107}
108
109static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
110 netdev_features_t features)
111{
Steffen Klassert7862b402017-04-14 10:06:50 +0200112 struct xfrm_state *x;
113 struct ip_esp_hdr *esph;
114 struct crypto_aead *aead;
Steffen Klassert7862b402017-04-14 10:06:50 +0200115 netdev_features_t esp_features = features;
116 struct xfrm_offload *xo = xfrm_offload(skb);
117
118 if (!xo)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100119 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200120
121 x = skb->sp->xvec[skb->sp->len - 1];
122 aead = x->data;
123 esph = ip_esp_hdr(skb);
124
125 if (esph->spi != x->id.spi)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100126 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200127
128 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100129 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200130
131 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
132
133 skb->encap_hdr_csum = 1;
134
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100135 if (!(features & NETIF_F_HW_ESP) || !x->xso.offload_handle ||
136 (x->xso.dev != skb->dev))
Steffen Klassert7862b402017-04-14 10:06:50 +0200137 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
138
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100139 xo->flags |= XFRM_GSO_SEGMENT;
Steffen Klassert7862b402017-04-14 10:06:50 +0200140
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100141 return x->outer_mode->gso_segment(x, skb, esp_features);
Steffen Klassert7862b402017-04-14 10:06:50 +0200142}
143
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200144static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
145{
146 struct crypto_aead *aead = x->data;
Ilan Tayariec9567a2017-08-01 12:49:04 +0300147 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200148
149 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
150 return -EINVAL;
151
Ilan Tayariec9567a2017-08-01 12:49:04 +0300152 if (!(xo->flags & CRYPTO_DONE))
153 skb->ip_summed = CHECKSUM_NONE;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200154
155 return esp_input_done2(skb, 0);
156}
157
158static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
159{
160 int err;
161 int alen;
162 int blksize;
163 struct xfrm_offload *xo;
164 struct ip_esp_hdr *esph;
165 struct crypto_aead *aead;
166 struct esp_info esp;
167 bool hw_offload = true;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100168 __u32 seq;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200169
170 esp.inplace = true;
171
172 xo = xfrm_offload(skb);
173
174 if (!xo)
175 return -EINVAL;
176
Ilan Tayari8f92e032017-04-19 08:41:01 +0300177 if (!(features & NETIF_F_HW_ESP) || !x->xso.offload_handle ||
178 (x->xso.dev != skb->dev)) {
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200179 xo->flags |= CRYPTO_FALLBACK;
180 hw_offload = false;
181 }
182
183 esp.proto = xo->proto;
184
185 /* skb is pure payload to encrypt */
186
187 aead = x->data;
188 alen = crypto_aead_authsize(aead);
189
190 esp.tfclen = 0;
191 /* XXX: Add support for tfc padding here. */
192
193 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
194 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
195 esp.plen = esp.clen - skb->len - esp.tfclen;
196 esp.tailen = esp.tfclen + esp.plen + alen;
197
198 esp.esph = ip_esp_hdr(skb);
199
200
201 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
202 esp.nfrags = esp_output_head(x, skb, &esp);
203 if (esp.nfrags < 0)
204 return esp.nfrags;
205 }
206
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100207 seq = xo->seq.low;
208
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200209 esph = esp.esph;
210 esph->spi = x->id.spi;
211
212 skb_push(skb, -skb_network_offset(skb));
213
214 if (xo->flags & XFRM_GSO_SEGMENT) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100215 esph->seq_no = htonl(seq);
216
217 if (!skb_is_gso(skb))
218 xo->seq.low++;
219 else
220 xo->seq.low += skb_shinfo(skb)->gso_segs;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200221 }
222
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100223 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
224
225 ip_hdr(skb)->tot_len = htons(skb->len);
226 ip_send_check(ip_hdr(skb));
227
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200228 if (hw_offload)
229 return 0;
230
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200231 err = esp_output_tail(x, skb, &esp);
Steffen Klassert4ff03082017-08-07 08:31:07 +0200232 if (err)
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200233 return err;
234
235 secpath_reset(skb);
236
237 return 0;
238}
239
Steffen Klassert7785bba2017-02-15 09:40:00 +0100240static const struct net_offload esp4_offload = {
241 .callbacks = {
242 .gro_receive = esp4_gro_receive,
Steffen Klassert7862b402017-04-14 10:06:50 +0200243 .gso_segment = esp4_gso_segment,
Steffen Klassert7785bba2017-02-15 09:40:00 +0100244 },
245};
246
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200247static const struct xfrm_type_offload esp_type_offload = {
248 .description = "ESP4 OFFLOAD",
249 .owner = THIS_MODULE,
250 .proto = IPPROTO_ESP,
251 .input_tail = esp_input_tail,
252 .xmit = esp_xmit,
Steffen Klassert7862b402017-04-14 10:06:50 +0200253 .encap = esp4_gso_encap,
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200254};
255
Steffen Klassert7785bba2017-02-15 09:40:00 +0100256static int __init esp4_offload_init(void)
257{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200258 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
259 pr_info("%s: can't add xfrm type offload\n", __func__);
260 return -EAGAIN;
261 }
262
Steffen Klassert7785bba2017-02-15 09:40:00 +0100263 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
264}
265
266static void __exit esp4_offload_exit(void)
267{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200268 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
269 pr_info("%s: can't remove xfrm type offload\n", __func__);
270
Steffen Klassert7785bba2017-02-15 09:40:00 +0100271 inet_del_offload(&esp4_offload, IPPROTO_ESP);
272}
273
274module_init(esp4_offload_init);
275module_exit(esp4_offload_exit);
276MODULE_LICENSE("GPL");
277MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
Ilan Tayariffdb5212017-08-01 12:49:08 +0300278MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);