]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv4/esp4_offload.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / esp4_offload.c
1 /*
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
31 static 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
41 if (!pskb_pull(skb, offset))
42 return NULL;
43
44 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
45 goto out;
46
47 xo = xfrm_offload(skb);
48 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 }
70 }
71
72 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);
84 out:
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
92 static 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
109 static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
110 netdev_features_t features)
111 {
112 __u32 seq;
113 int err = 0;
114 struct sk_buff *skb2;
115 struct xfrm_state *x;
116 struct ip_esp_hdr *esph;
117 struct crypto_aead *aead;
118 struct sk_buff *segs = ERR_PTR(-EINVAL);
119 netdev_features_t esp_features = features;
120 struct xfrm_offload *xo = xfrm_offload(skb);
121
122 if (!xo)
123 goto out;
124
125 seq = xo->seq.low;
126
127 x = skb->sp->xvec[skb->sp->len - 1];
128 aead = x->data;
129 esph = ip_esp_hdr(skb);
130
131 if (esph->spi != x->id.spi)
132 goto out;
133
134 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
135 goto out;
136
137 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
138
139 skb->encap_hdr_csum = 1;
140
141 if (!(features & NETIF_F_HW_ESP))
142 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
143
144 segs = x->outer_mode->gso_segment(x, skb, esp_features);
145 if (IS_ERR_OR_NULL(segs))
146 goto out;
147
148 __skb_pull(skb, skb->data - skb_mac_header(skb));
149
150 skb2 = segs;
151 do {
152 struct sk_buff *nskb = skb2->next;
153
154 xo = xfrm_offload(skb2);
155 xo->flags |= XFRM_GSO_SEGMENT;
156 xo->seq.low = seq;
157 xo->seq.hi = xfrm_replay_seqhi(x, seq);
158
159 if(!(features & NETIF_F_HW_ESP))
160 xo->flags |= CRYPTO_FALLBACK;
161
162 x->outer_mode->xmit(x, skb2);
163
164 err = x->type_offload->xmit(x, skb2, esp_features);
165 if (err) {
166 kfree_skb_list(segs);
167 return ERR_PTR(err);
168 }
169
170 if (!skb_is_gso(skb2))
171 seq++;
172 else
173 seq += skb_shinfo(skb2)->gso_segs;
174
175 skb_push(skb2, skb2->mac_len);
176 skb2 = nskb;
177 } while (skb2);
178
179 out:
180 return segs;
181 }
182
183 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
184 {
185 struct crypto_aead *aead = x->data;
186 struct xfrm_offload *xo = xfrm_offload(skb);
187
188 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
189 return -EINVAL;
190
191 if (!(xo->flags & CRYPTO_DONE))
192 skb->ip_summed = CHECKSUM_NONE;
193
194 return esp_input_done2(skb, 0);
195 }
196
197 static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
198 {
199 int err;
200 int alen;
201 int blksize;
202 struct xfrm_offload *xo;
203 struct ip_esp_hdr *esph;
204 struct crypto_aead *aead;
205 struct esp_info esp;
206 bool hw_offload = true;
207
208 esp.inplace = true;
209
210 xo = xfrm_offload(skb);
211
212 if (!xo)
213 return -EINVAL;
214
215 if (!(features & NETIF_F_HW_ESP) || !x->xso.offload_handle ||
216 (x->xso.dev != skb->dev)) {
217 xo->flags |= CRYPTO_FALLBACK;
218 hw_offload = false;
219 }
220
221 esp.proto = xo->proto;
222
223 /* skb is pure payload to encrypt */
224
225 aead = x->data;
226 alen = crypto_aead_authsize(aead);
227
228 esp.tfclen = 0;
229 /* XXX: Add support for tfc padding here. */
230
231 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
232 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
233 esp.plen = esp.clen - skb->len - esp.tfclen;
234 esp.tailen = esp.tfclen + esp.plen + alen;
235
236 esp.esph = ip_esp_hdr(skb);
237
238
239 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
240 esp.nfrags = esp_output_head(x, skb, &esp);
241 if (esp.nfrags < 0)
242 return esp.nfrags;
243 }
244
245 esph = esp.esph;
246 esph->spi = x->id.spi;
247
248 skb_push(skb, -skb_network_offset(skb));
249
250 if (xo->flags & XFRM_GSO_SEGMENT) {
251 esph->seq_no = htonl(xo->seq.low);
252 } else {
253 ip_hdr(skb)->tot_len = htons(skb->len);
254 ip_send_check(ip_hdr(skb));
255 }
256
257 if (hw_offload)
258 return 0;
259
260 esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32));
261
262 err = esp_output_tail(x, skb, &esp);
263 if (err)
264 return err;
265
266 secpath_reset(skb);
267
268 return 0;
269 }
270
271 static const struct net_offload esp4_offload = {
272 .callbacks = {
273 .gro_receive = esp4_gro_receive,
274 .gso_segment = esp4_gso_segment,
275 },
276 };
277
278 static const struct xfrm_type_offload esp_type_offload = {
279 .description = "ESP4 OFFLOAD",
280 .owner = THIS_MODULE,
281 .proto = IPPROTO_ESP,
282 .input_tail = esp_input_tail,
283 .xmit = esp_xmit,
284 .encap = esp4_gso_encap,
285 };
286
287 static int __init esp4_offload_init(void)
288 {
289 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
290 pr_info("%s: can't add xfrm type offload\n", __func__);
291 return -EAGAIN;
292 }
293
294 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
295 }
296
297 static void __exit esp4_offload_exit(void)
298 {
299 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
300 pr_info("%s: can't remove xfrm type offload\n", __func__);
301
302 inet_del_offload(&esp4_offload, IPPROTO_ESP);
303 }
304
305 module_init(esp4_offload_init);
306 module_exit(esp4_offload_exit);
307 MODULE_LICENSE("GPL");
308 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
309 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);