blob: fb065a8937ea28788f43a3906cbe08e3c0fc8c0d [file] [log] [blame]
Joe Perchesafd465032012-03-12 07:03:32 +00001#define pr_fmt(fmt) "IPsec: " fmt
2
Herbert Xu38320c72008-01-28 19:35:05 -08003#include <crypto/aead.h>
4#include <crypto/authenc.h>
Herbert Xu6b7326c2006-07-30 15:41:01 +10005#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/module.h>
7#include <net/ip.h>
8#include <net/xfrm.h>
9#include <net/esp.h>
Adrian Bunk72998d82007-10-26 22:53:58 -070010#include <linux/scatterlist.h>
Herbert Xua02a6422005-10-10 21:11:08 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/pfkeyv2.h>
Herbert Xu38320c72008-01-28 19:35:05 -080013#include <linux/rtnetlink.h>
14#include <linux/slab.h>
Herbert Xub7c6538c2007-10-09 13:33:35 -070015#include <linux/spinlock.h>
Thomas Graf2017a722007-12-10 16:53:05 -080016#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020018#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/udp.h>
20
Steffen Klassertcac26612017-01-17 10:22:57 +010021#include <linux/highmem.h>
22
Herbert Xu38320c72008-01-28 19:35:05 -080023struct esp_skb_cb {
24 struct xfrm_skb_cb xfrm;
25 void *tmp;
26};
27
Herbert Xu962fcef2016-06-18 13:03:36 +080028struct esp_output_extra {
29 __be32 seqhi;
30 u32 esphoff;
31};
32
Herbert Xu38320c72008-01-28 19:35:05 -080033#define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
34
Martin Willid979e202010-12-08 04:37:50 +000035static u32 esp4_get_mtu(struct xfrm_state *x, int mtu);
36
Herbert Xu38320c72008-01-28 19:35:05 -080037/*
38 * Allocate an AEAD request structure with extra space for SG and IV.
39 *
40 * For alignment considerations the IV is placed at the front, followed
41 * by the request and finally the SG list.
42 *
43 * TODO: Use spare space in skb for this where possible.
44 */
Herbert Xu962fcef2016-06-18 13:03:36 +080045static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
Herbert Xu38320c72008-01-28 19:35:05 -080046{
47 unsigned int len;
48
Herbert Xu962fcef2016-06-18 13:03:36 +080049 len = extralen;
Steffen Klassert0dc49e92011-03-08 00:07:14 +000050
51 len += crypto_aead_ivsize(aead);
52
Herbert Xu38320c72008-01-28 19:35:05 -080053 if (len) {
54 len += crypto_aead_alignmask(aead) &
55 ~(crypto_tfm_ctx_alignment() - 1);
56 len = ALIGN(len, crypto_tfm_ctx_alignment());
57 }
58
Herbert Xu7021b2e2015-05-27 16:03:46 +080059 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
Herbert Xu38320c72008-01-28 19:35:05 -080060 len = ALIGN(len, __alignof__(struct scatterlist));
61
62 len += sizeof(struct scatterlist) * nfrags;
63
64 return kmalloc(len, GFP_ATOMIC);
65}
66
Herbert Xu962fcef2016-06-18 13:03:36 +080067static inline void *esp_tmp_extra(void *tmp)
Steffen Klassert0dc49e92011-03-08 00:07:14 +000068{
Herbert Xu962fcef2016-06-18 13:03:36 +080069 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
Steffen Klassert0dc49e92011-03-08 00:07:14 +000070}
Herbert Xu962fcef2016-06-18 13:03:36 +080071
72static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
Herbert Xu38320c72008-01-28 19:35:05 -080073{
74 return crypto_aead_ivsize(aead) ?
Herbert Xu962fcef2016-06-18 13:03:36 +080075 PTR_ALIGN((u8 *)tmp + extralen,
76 crypto_aead_alignmask(aead) + 1) : tmp + extralen;
Herbert Xu38320c72008-01-28 19:35:05 -080077}
78
Herbert Xu38320c72008-01-28 19:35:05 -080079static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
80{
81 struct aead_request *req;
82
83 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
84 crypto_tfm_ctx_alignment());
85 aead_request_set_tfm(req, aead);
86 return req;
87}
88
89static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
90 struct aead_request *req)
91{
92 return (void *)ALIGN((unsigned long)(req + 1) +
93 crypto_aead_reqsize(aead),
94 __alignof__(struct scatterlist));
95}
96
Steffen Klassertcac26612017-01-17 10:22:57 +010097static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
98{
99 struct esp_output_extra *extra = esp_tmp_extra(tmp);
100 struct crypto_aead *aead = x->data;
101 int extralen = 0;
102 u8 *iv;
103 struct aead_request *req;
104 struct scatterlist *sg;
105
106 if (x->props.flags & XFRM_STATE_ESN)
107 extralen += sizeof(*extra);
108
109 extra = esp_tmp_extra(tmp);
110 iv = esp_tmp_iv(aead, tmp, extralen);
111 req = esp_tmp_req(aead, iv);
112
113 /* Unref skb_frag_pages in the src scatterlist if necessary.
114 * Skip the first sg which comes from skb->data.
115 */
116 if (req->src != req->dst)
117 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
118 put_page(sg_page(sg));
119}
120
Herbert Xu38320c72008-01-28 19:35:05 -0800121static void esp_output_done(struct crypto_async_request *base, int err)
122{
123 struct sk_buff *skb = base->data;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100124 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassertcac26612017-01-17 10:22:57 +0100125 void *tmp;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100126 struct xfrm_state *x;
127
Florian Westphal2294be0f2018-12-18 17:15:20 +0100128 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
129 struct sec_path *sp = skb_sec_path(skb);
130
131 x = sp->xvec[sp->len - 1];
132 } else {
Steffen Klassertf53c7232017-12-20 10:41:36 +0100133 x = skb_dst(skb)->xfrm;
Florian Westphal2294be0f2018-12-18 17:15:20 +0100134 }
Herbert Xu38320c72008-01-28 19:35:05 -0800135
Steffen Klassertcac26612017-01-17 10:22:57 +0100136 tmp = ESP_SKB_CB(skb)->tmp;
137 esp_ssg_unref(x, tmp);
138 kfree(tmp);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100139
140 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
141 if (err) {
142 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
143 kfree_skb(skb);
144 return;
145 }
146
147 skb_push(skb, skb->data - skb_mac_header(skb));
148 secpath_reset(skb);
149 xfrm_dev_resume(skb);
150 } else {
151 xfrm_output_resume(skb, err);
152 }
Herbert Xu38320c72008-01-28 19:35:05 -0800153}
154
Herbert Xu7021b2e2015-05-27 16:03:46 +0800155/* Move ESP header back into place. */
156static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
157{
158 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
159 void *tmp = ESP_SKB_CB(skb)->tmp;
Herbert Xu962fcef2016-06-18 13:03:36 +0800160 __be32 *seqhi = esp_tmp_extra(tmp);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800161
162 esph->seq_no = esph->spi;
163 esph->spi = *seqhi;
164}
165
166static void esp_output_restore_header(struct sk_buff *skb)
167{
Herbert Xu962fcef2016-06-18 13:03:36 +0800168 void *tmp = ESP_SKB_CB(skb)->tmp;
169 struct esp_output_extra *extra = esp_tmp_extra(tmp);
170
171 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
172 sizeof(__be32));
Herbert Xu7021b2e2015-05-27 16:03:46 +0800173}
174
Steffen Klassertcac26612017-01-17 10:22:57 +0100175static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200176 struct xfrm_state *x,
Steffen Klassertcac26612017-01-17 10:22:57 +0100177 struct ip_esp_hdr *esph,
178 struct esp_output_extra *extra)
179{
Steffen Klassertcac26612017-01-17 10:22:57 +0100180 /* For ESN we move the header forward by 4 bytes to
181 * accomodate the high bits. We will move it back after
182 * encryption.
183 */
184 if ((x->props.flags & XFRM_STATE_ESN)) {
Steffen Klassert7862b402017-04-14 10:06:50 +0200185 __u32 seqhi;
186 struct xfrm_offload *xo = xfrm_offload(skb);
187
188 if (xo)
189 seqhi = xo->seq.hi;
190 else
191 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
192
Steffen Klassertcac26612017-01-17 10:22:57 +0100193 extra->esphoff = (unsigned char *)esph -
194 skb_transport_header(skb);
195 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
196 extra->seqhi = esph->spi;
Steffen Klassert7862b402017-04-14 10:06:50 +0200197 esph->seq_no = htonl(seqhi);
Steffen Klassertcac26612017-01-17 10:22:57 +0100198 }
199
200 esph->spi = x->id.spi;
201
202 return esph;
203}
204
Herbert Xu7021b2e2015-05-27 16:03:46 +0800205static void esp_output_done_esn(struct crypto_async_request *base, int err)
206{
207 struct sk_buff *skb = base->data;
208
209 esp_output_restore_header(skb);
210 esp_output_done(base, err);
211}
212
Steffen Klasserteb758c82017-01-17 10:23:08 +0100213static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
214{
215 /* Fill padding... */
216 if (tfclen) {
217 memset(tail, 0, tfclen);
218 tail += tfclen;
219 }
220 do {
221 int i;
222 for (i = 0; i < plen - 2; i++)
223 tail[i] = i + 1;
224 } while (0);
225 tail[plen - 2] = plen - 2;
226 tail[plen - 1] = proto;
227}
228
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100229static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200231 int encap_type;
232 struct udphdr *uh;
233 __be32 *udpdata32;
234 __be16 sport, dport;
235 struct xfrm_encap_tmpl *encap = x->encap;
236 struct ip_esp_hdr *esph = esp->esph;
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100237 unsigned int len;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200238
239 spin_lock_bh(&x->lock);
240 sport = encap->encap_sport;
241 dport = encap->encap_dport;
242 encap_type = encap->encap_type;
243 spin_unlock_bh(&x->lock);
244
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100245 len = skb->len + esp->tailen - skb_transport_offset(skb);
246 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
247 return -EMSGSIZE;
248
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200249 uh = (struct udphdr *)esph;
250 uh->source = sport;
251 uh->dest = dport;
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100252 uh->len = htons(len);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200253 uh->check = 0;
254
255 switch (encap_type) {
256 default:
257 case UDP_ENCAP_ESPINUDP:
258 esph = (struct ip_esp_hdr *)(uh + 1);
259 break;
260 case UDP_ENCAP_ESPINUDP_NON_IKE:
261 udpdata32 = (__be32 *)(uh + 1);
262 udpdata32[0] = udpdata32[1] = 0;
263 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
264 break;
265 }
266
267 *skb_mac_header(skb) = IPPROTO_UDP;
268 esp->esph = esph;
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100269
270 return 0;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200271}
272
273int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
274{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700275 u8 *tail;
Steffen Klassertcac26612017-01-17 10:22:57 +0100276 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int nfrags;
Steffen Klassert0e78a872017-05-03 08:44:27 +0200278 int esph_offset;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200279 struct page *page;
280 struct sk_buff *trailer;
281 int tailen = esp->tailen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* this is non-NULL only with UDP Encapsulation */
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100284 if (x->encap) {
285 int err = esp_output_udp_encap(x, skb, esp);
286
287 if (err < 0)
288 return err;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Steffen Klassertcac26612017-01-17 10:22:57 +0100291 if (!skb_cloned(skb)) {
Steffen Klassert54ffd792017-08-25 07:34:35 +0200292 if (tailen <= skb_tailroom(skb)) {
Steffen Klassertcac26612017-01-17 10:22:57 +0100293 nfrags = 1;
294 trailer = skb;
295 tail = skb_tail_pointer(trailer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Steffen Klassertcac26612017-01-17 10:22:57 +0100297 goto skip_cow;
298 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
299 && !skb_has_frag_list(skb)) {
300 int allocsize;
301 struct sock *sk = skb->sk;
302 struct page_frag *pfrag = &x->xfrag;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800303
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200304 esp->inplace = false;
305
Steffen Klassertcac26612017-01-17 10:22:57 +0100306 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
307
308 spin_lock_bh(&x->lock);
309
310 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
311 spin_unlock_bh(&x->lock);
312 goto cow;
313 }
314
315 page = pfrag->page;
316 get_page(page);
317
318 vaddr = kmap_atomic(page);
319
320 tail = vaddr + pfrag->offset;
321
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200322 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
Steffen Klassertcac26612017-01-17 10:22:57 +0100323
324 kunmap_atomic(vaddr);
325
326 nfrags = skb_shinfo(skb)->nr_frags;
327
328 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
329 tailen);
330 skb_shinfo(skb)->nr_frags = ++nfrags;
331
332 pfrag->offset = pfrag->offset + allocsize;
Steffen Klassert36ff0dd2017-08-25 07:16:07 +0200333
334 spin_unlock_bh(&x->lock);
335
Steffen Klassertcac26612017-01-17 10:22:57 +0100336 nfrags++;
337
338 skb->len += tailen;
339 skb->data_len += tailen;
340 skb->truesize += tailen;
Martin Willi09db5122019-01-28 09:35:35 +0100341 if (sk && sk_fullsock(sk))
Reshetova, Elena14afee42017-06-30 13:08:00 +0300342 refcount_add(tailen, &sk->sk_wmem_alloc);
Steffen Klassertcac26612017-01-17 10:22:57 +0100343
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200344 goto out;
Steffen Klassertcac26612017-01-17 10:22:57 +0100345 }
Herbert Xu7021b2e2015-05-27 16:03:46 +0800346 }
347
Steffen Klassertcac26612017-01-17 10:22:57 +0100348cow:
Steffen Klassert0e78a872017-05-03 08:44:27 +0200349 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
350
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200351 nfrags = skb_cow_data(skb, tailen, &trailer);
352 if (nfrags < 0)
353 goto out;
Steffen Klassertcac26612017-01-17 10:22:57 +0100354 tail = skb_tail_pointer(trailer);
Steffen Klassert0e78a872017-05-03 08:44:27 +0200355 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
Steffen Klassertcac26612017-01-17 10:22:57 +0100356
357skip_cow:
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200358 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
359 pskb_put(skb, trailer, tailen);
Steffen Klassertcac26612017-01-17 10:22:57 +0100360
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200361out:
362 return nfrags;
363}
364EXPORT_SYMBOL_GPL(esp_output_head);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800365
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200366int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
367{
368 u8 *iv;
369 int alen;
370 void *tmp;
371 int ivlen;
372 int assoclen;
373 int extralen;
374 struct page *page;
375 struct ip_esp_hdr *esph;
376 struct crypto_aead *aead;
377 struct aead_request *req;
378 struct scatterlist *sg, *dsg;
379 struct esp_output_extra *extra;
380 int err = -ENOMEM;
381
382 assoclen = sizeof(struct ip_esp_hdr);
383 extralen = 0;
384
385 if (x->props.flags & XFRM_STATE_ESN) {
386 extralen += sizeof(*extra);
387 assoclen += sizeof(__be32);
388 }
389
390 aead = x->data;
391 alen = crypto_aead_authsize(aead);
392 ivlen = crypto_aead_ivsize(aead);
393
394 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
Steffen Klasserte892d2d2017-04-24 07:33:56 +0200395 if (!tmp)
Steffen Klassertcac26612017-01-17 10:22:57 +0100396 goto error;
Steffen Klassertcac26612017-01-17 10:22:57 +0100397
398 extra = esp_tmp_extra(tmp);
399 iv = esp_tmp_iv(aead, tmp, extralen);
400 req = esp_tmp_req(aead, iv);
401 sg = esp_req_sg(aead, req);
Steffen Klassertcac26612017-01-17 10:22:57 +0100402
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200403 if (esp->inplace)
404 dsg = sg;
405 else
406 dsg = &sg[esp->nfrags];
Steffen Klassertcac26612017-01-17 10:22:57 +0100407
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200408 esph = esp_output_set_extra(skb, x, esp->esph, extra);
409 esp->esph = esph;
410
411 sg_init_table(sg, esp->nfrags);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200412 err = skb_to_sgvec(skb, sg,
413 (unsigned char *)esph - skb->data,
414 assoclen + ivlen + esp->clen + alen);
415 if (unlikely(err < 0))
Steffen Klasserte6194922017-07-13 09:13:30 +0200416 goto error_free;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000417
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200418 if (!esp->inplace) {
419 int allocsize;
420 struct page_frag *pfrag = &x->xfrag;
421
422 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
423
424 spin_lock_bh(&x->lock);
425 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
426 spin_unlock_bh(&x->lock);
Steffen Klasserte6194922017-07-13 09:13:30 +0200427 goto error_free;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200428 }
429
430 skb_shinfo(skb)->nr_frags = 1;
431
432 page = pfrag->page;
433 get_page(page);
434 /* replace page frags in skb with new page */
435 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
436 pfrag->offset = pfrag->offset + allocsize;
437 spin_unlock_bh(&x->lock);
438
439 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200440 err = skb_to_sgvec(skb, dsg,
441 (unsigned char *)esph - skb->data,
442 assoclen + ivlen + esp->clen + alen);
443 if (unlikely(err < 0))
Steffen Klasserte6194922017-07-13 09:13:30 +0200444 goto error_free;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200445 }
446
Steffen Klassertcac26612017-01-17 10:22:57 +0100447 if ((x->props.flags & XFRM_STATE_ESN))
448 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
449 else
450 aead_request_set_callback(req, 0, esp_output_done, skb);
451
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200452 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800453 aead_request_set_ad(req, assoclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Herbert Xu7021b2e2015-05-27 16:03:46 +0800455 memset(iv, 0, ivlen);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200456 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
Herbert Xu7021b2e2015-05-27 16:03:46 +0800457 min(ivlen, 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Herbert Xu38320c72008-01-28 19:35:05 -0800459 ESP_SKB_CB(skb)->tmp = tmp;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800460 err = crypto_aead_encrypt(req);
461
462 switch (err) {
463 case -EINPROGRESS:
Herbert Xu38320c72008-01-28 19:35:05 -0800464 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Gilad Ben-Yossef068c2e72017-10-18 08:00:35 +0100466 case -ENOSPC:
Herbert Xu38320c72008-01-28 19:35:05 -0800467 err = NET_XMIT_DROP;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800468 break;
469
470 case 0:
471 if ((x->props.flags & XFRM_STATE_ESN))
472 esp_output_restore_header(skb);
473 }
Herbert Xu6b7326c2006-07-30 15:41:01 +1000474
Steffen Klassertcac26612017-01-17 10:22:57 +0100475 if (sg != dsg)
476 esp_ssg_unref(x, tmp);
Herbert Xub7c6538c2007-10-09 13:33:35 -0700477
Steffen Klasserte6194922017-07-13 09:13:30 +0200478error_free:
479 kfree(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480error:
481 return err;
482}
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200483EXPORT_SYMBOL_GPL(esp_output_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200485static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
486{
487 int alen;
488 int blksize;
489 struct ip_esp_hdr *esph;
490 struct crypto_aead *aead;
491 struct esp_info esp;
492
493 esp.inplace = true;
494
495 esp.proto = *skb_mac_header(skb);
496 *skb_mac_header(skb) = IPPROTO_ESP;
497
498 /* skb is pure payload to encrypt */
499
500 aead = x->data;
501 alen = crypto_aead_authsize(aead);
502
503 esp.tfclen = 0;
504 if (x->tfcpad) {
505 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
506 u32 padto;
507
508 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
509 if (skb->len < padto)
510 esp.tfclen = padto - skb->len;
511 }
512 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
513 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
514 esp.plen = esp.clen - skb->len - esp.tfclen;
515 esp.tailen = esp.tfclen + esp.plen + alen;
516
517 esp.esph = ip_esp_hdr(skb);
518
519 esp.nfrags = esp_output_head(x, skb, &esp);
520 if (esp.nfrags < 0)
521 return esp.nfrags;
522
523 esph = esp.esph;
524 esph->spi = x->id.spi;
525
526 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
527 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
528 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
529
530 skb_push(skb, -skb_network_offset(skb));
531
532 return esp_output_tail(x, skb, &esp);
533}
534
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300535static inline int esp_remove_trailer(struct sk_buff *skb)
536{
537 struct xfrm_state *x = xfrm_input_state(skb);
538 struct xfrm_offload *xo = xfrm_offload(skb);
539 struct crypto_aead *aead = x->data;
540 int alen, hlen, elen;
541 int padlen, trimlen;
542 __wsum csumdiff;
543 u8 nexthdr[2];
544 int ret;
545
546 alen = crypto_aead_authsize(aead);
547 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
548 elen = skb->len - hlen;
549
550 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
551 ret = xo->proto;
552 goto out;
553 }
554
555 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
556 BUG();
557
558 ret = -EINVAL;
559 padlen = nexthdr[0];
560 if (padlen + 2 + alen >= elen) {
561 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
562 padlen + 2, elen - alen);
563 goto out;
564 }
565
566 trimlen = alen + padlen + 2;
567 if (skb->ip_summed == CHECKSUM_COMPLETE) {
568 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
569 skb->csum = csum_block_sub(skb->csum, csumdiff,
570 skb->len - trimlen);
571 }
572 pskb_trim(skb, skb->len - trimlen);
573
574 ret = nexthdr[1];
575
576out:
577 return ret;
578}
579
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200580int esp_input_done2(struct sk_buff *skb, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000582 const struct iphdr *iph;
Herbert Xu38320c72008-01-28 19:35:05 -0800583 struct xfrm_state *x = xfrm_input_state(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200584 struct xfrm_offload *xo = xfrm_offload(skb);
Mathias Krause1c5ad132013-10-18 12:09:05 +0200585 struct crypto_aead *aead = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800586 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700587 int ihl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200589 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
590 kfree(ESP_SKB_CB(skb)->tmp);
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800591
Herbert Xu6b7326c2006-07-30 15:41:01 +1000592 if (unlikely(err))
Herbert Xu668dc8a2007-12-16 15:55:02 -0800593 goto out;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800594
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300595 err = esp_remove_trailer(skb);
596 if (unlikely(err < 0))
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800597 goto out;
598
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700599 iph = ip_hdr(skb);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700600 ihl = iph->ihl * 4;
601
Herbert Xu752c1f42006-02-27 13:00:40 -0800602 if (x->encap) {
603 struct xfrm_encap_tmpl *encap = x->encap;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700604 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
Herbert Xu752c1f42006-02-27 13:00:40 -0800605
606 /*
607 * 1) if the NAT-T peer's IP or port changed then
608 * advertize the change to the keying daemon.
609 * This is an inbound SA, so just compare
610 * SRC ports.
611 */
612 if (iph->saddr != x->props.saddr.a4 ||
613 uh->source != encap->encap_sport) {
614 xfrm_address_t ipaddr;
615
616 ipaddr.a4 = iph->saddr;
617 km_new_mapping(x, &ipaddr, uh->source);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900618
Herbert Xu752c1f42006-02-27 13:00:40 -0800619 /* XXX: perhaps add an extra
620 * policy check here, to see
621 * if we should allow or
622 * reject a packet from a
623 * different source
624 * address/port.
625 */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800626 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900627
Herbert Xu752c1f42006-02-27 13:00:40 -0800628 /*
629 * 2) ignore UDP/TCP checksums in case
630 * of NAT-T in Transport Mode, or
631 * perform other post-processing fixes
632 * as per draft-ietf-ipsec-udp-encaps-06,
633 * section 3.1.2
634 */
Herbert Xu8bd17072007-10-10 15:41:41 -0700635 if (x->props.mode == XFRM_MODE_TRANSPORT)
Herbert Xu752c1f42006-02-27 13:00:40 -0800636 skb->ip_summed = CHECKSUM_UNNECESSARY;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800637 }
638
Ilan Tayariec9567a2017-08-01 12:49:04 +0300639 skb_pull_rcsum(skb, hlen);
Li RongQing7143dfa2012-12-28 16:07:16 +0800640 if (x->props.mode == XFRM_MODE_TUNNEL)
641 skb_reset_transport_header(skb);
642 else
643 skb_set_transport_header(skb, -ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Herbert Xu38320c72008-01-28 19:35:05 -0800645 /* RFC4303: Drop dummy packets without any error */
646 if (err == IPPROTO_NONE)
647 err = -EINVAL;
648
649out:
650 return err;
651}
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200652EXPORT_SYMBOL_GPL(esp_input_done2);
Herbert Xu38320c72008-01-28 19:35:05 -0800653
654static void esp_input_done(struct crypto_async_request *base, int err)
655{
656 struct sk_buff *skb = base->data;
657
658 xfrm_input_resume(skb, esp_input_done2(skb, err));
659}
660
Herbert Xu7021b2e2015-05-27 16:03:46 +0800661static void esp_input_restore_header(struct sk_buff *skb)
662{
663 esp_restore_header(skb, 0);
664 __skb_pull(skb, 4);
665}
666
Steffen Klassertcac26612017-01-17 10:22:57 +0100667static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
668{
669 struct xfrm_state *x = xfrm_input_state(skb);
Colin Ian King60aa8042018-01-30 14:53:48 +0000670 struct ip_esp_hdr *esph;
Steffen Klassertcac26612017-01-17 10:22:57 +0100671
672 /* For ESN we move the header forward by 4 bytes to
673 * accomodate the high bits. We will move it back after
674 * decryption.
675 */
676 if ((x->props.flags & XFRM_STATE_ESN)) {
Johannes Bergd58ff352017-06-16 14:29:23 +0200677 esph = skb_push(skb, 4);
Steffen Klassertcac26612017-01-17 10:22:57 +0100678 *seqhi = esph->spi;
679 esph->spi = esph->seq_no;
680 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
681 }
682}
683
Herbert Xu7021b2e2015-05-27 16:03:46 +0800684static void esp_input_done_esn(struct crypto_async_request *base, int err)
685{
686 struct sk_buff *skb = base->data;
687
688 esp_input_restore_header(skb);
689 esp_input_done(base, err);
690}
691
Herbert Xu38320c72008-01-28 19:35:05 -0800692/*
693 * Note: detecting truncated vs. non-truncated authentication data is very
694 * expensive, so we only support truncated data, which is the recommended
695 * and common case.
696 */
697static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
698{
Mathias Krause1c5ad132013-10-18 12:09:05 +0200699 struct crypto_aead *aead = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800700 struct aead_request *req;
701 struct sk_buff *trailer;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800702 int ivlen = crypto_aead_ivsize(aead);
Haishuang Yan0c05f982018-08-17 15:51:00 +0800703 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
Herbert Xu38320c72008-01-28 19:35:05 -0800704 int nfrags;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000705 int assoclen;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000706 int seqhilen;
707 __be32 *seqhi;
Herbert Xu38320c72008-01-28 19:35:05 -0800708 void *tmp;
709 u8 *iv;
710 struct scatterlist *sg;
Herbert Xu38320c72008-01-28 19:35:05 -0800711 int err = -EINVAL;
712
Haishuang Yan0c05f982018-08-17 15:51:00 +0800713 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
Herbert Xu38320c72008-01-28 19:35:05 -0800714 goto out;
715
716 if (elen <= 0)
717 goto out;
718
Haishuang Yan0c05f982018-08-17 15:51:00 +0800719 assoclen = sizeof(struct ip_esp_hdr);
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000720 seqhilen = 0;
721
722 if (x->props.flags & XFRM_STATE_ESN) {
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000723 seqhilen += sizeof(__be32);
724 assoclen += seqhilen;
725 }
726
Steffen Klassertcac26612017-01-17 10:22:57 +0100727 if (!skb_cloned(skb)) {
728 if (!skb_is_nonlinear(skb)) {
729 nfrags = 1;
730
731 goto skip_cow;
732 } else if (!skb_has_frag_list(skb)) {
733 nfrags = skb_shinfo(skb)->nr_frags;
734 nfrags++;
735
736 goto skip_cow;
737 }
738 }
739
740 err = skb_cow_data(skb, 0, &trailer);
741 if (err < 0)
742 goto out;
743
744 nfrags = err;
745
746skip_cow:
Herbert Xu38320c72008-01-28 19:35:05 -0800747 err = -ENOMEM;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800748 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
Herbert Xu38320c72008-01-28 19:35:05 -0800749 if (!tmp)
750 goto out;
751
752 ESP_SKB_CB(skb)->tmp = tmp;
Herbert Xu962fcef2016-06-18 13:03:36 +0800753 seqhi = esp_tmp_extra(tmp);
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000754 iv = esp_tmp_iv(aead, tmp, seqhilen);
Herbert Xu38320c72008-01-28 19:35:05 -0800755 req = esp_tmp_req(aead, iv);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800756 sg = esp_req_sg(aead, req);
Herbert Xu38320c72008-01-28 19:35:05 -0800757
Steffen Klassertcac26612017-01-17 10:22:57 +0100758 esp_input_set_header(skb, seqhi);
Herbert Xu38320c72008-01-28 19:35:05 -0800759
760 sg_init_table(sg, nfrags);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200761 err = skb_to_sgvec(skb, sg, 0, skb->len);
Steffen Klasserte6194922017-07-13 09:13:30 +0200762 if (unlikely(err < 0)) {
763 kfree(tmp);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200764 goto out;
Steffen Klasserte6194922017-07-13 09:13:30 +0200765 }
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000766
Steffen Klassertcac26612017-01-17 10:22:57 +0100767 skb->ip_summed = CHECKSUM_NONE;
768
769 if ((x->props.flags & XFRM_STATE_ESN))
770 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
771 else
772 aead_request_set_callback(req, 0, esp_input_done, skb);
773
Herbert Xu7021b2e2015-05-27 16:03:46 +0800774 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
775 aead_request_set_ad(req, assoclen);
Herbert Xu38320c72008-01-28 19:35:05 -0800776
777 err = crypto_aead_decrypt(req);
778 if (err == -EINPROGRESS)
779 goto out;
780
Herbert Xu7021b2e2015-05-27 16:03:46 +0800781 if ((x->props.flags & XFRM_STATE_ESN))
782 esp_input_restore_header(skb);
783
Herbert Xu38320c72008-01-28 19:35:05 -0800784 err = esp_input_done2(skb, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786out:
Herbert Xu668dc8a2007-12-16 15:55:02 -0800787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Patrick McHardyc5c25232007-04-09 11:47:18 -0700790static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Mathias Krause1c5ad132013-10-18 12:09:05 +0200792 struct crypto_aead *aead = x->data;
793 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
Benjamin Poirier91657ea2012-05-24 11:32:38 +0000794 unsigned int net_adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Diego Beltrami0a69452c2006-10-03 23:47:05 -0700796 switch (x->props.mode) {
Benjamin Poirier91657ea2012-05-24 11:32:38 +0000797 case XFRM_MODE_TRANSPORT:
798 case XFRM_MODE_BEET:
799 net_adj = sizeof(struct iphdr);
800 break;
Diego Beltrami0a69452c2006-10-03 23:47:05 -0700801 case XFRM_MODE_TUNNEL:
Benjamin Poirier91657ea2012-05-24 11:32:38 +0000802 net_adj = 0;
Diego Beltrami0a69452c2006-10-03 23:47:05 -0700803 break;
804 default:
Benjamin Poirier91657ea2012-05-24 11:32:38 +0000805 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Diego Beltrami0a69452c2006-10-03 23:47:05 -0700807
Mathias Krause1c5ad132013-10-18 12:09:05 +0200808 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
Mathias Krause123b0d12013-10-18 12:09:04 +0200809 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Steffen Klassert827789c2014-02-21 08:41:08 +0100812static int esp4_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800814 struct net *net = dev_net(skb->dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000815 const struct iphdr *iph = (const struct iphdr *)skb->data;
Jianjun Kongd93191002008-11-03 00:23:42 -0800816 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct xfrm_state *x;
818
David S. Miller55be7a92012-07-11 21:27:49 -0700819 switch (icmp_hdr(skb)->type) {
820 case ICMP_DEST_UNREACH:
821 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
Steffen Klassert827789c2014-02-21 08:41:08 +0100822 return 0;
David S. Miller55be7a92012-07-11 21:27:49 -0700823 case ICMP_REDIRECT:
824 break;
825 default:
Steffen Klassert827789c2014-02-21 08:41:08 +0100826 return 0;
David S. Miller55be7a92012-07-11 21:27:49 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000829 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
830 esph->spi, IPPROTO_ESP, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!x)
Steffen Klassert827789c2014-02-21 08:41:08 +0100832 return 0;
David S. Miller55be7a92012-07-11 21:27:49 -0700833
Timo Teräs387aa652013-05-27 20:46:31 +0000834 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700835 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
Timo Teräs387aa652013-05-27 20:46:31 +0000836 else
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700837 ipv4_redirect(skb, net, 0, IPPROTO_ESP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 xfrm_state_put(x);
Steffen Klassert827789c2014-02-21 08:41:08 +0100839
840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843static void esp_destroy(struct xfrm_state *x)
844{
Mathias Krause1c5ad132013-10-18 12:09:05 +0200845 struct crypto_aead *aead = x->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Mathias Krause1c5ad132013-10-18 12:09:05 +0200847 if (!aead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return;
849
Mathias Krause1c5ad132013-10-18 12:09:05 +0200850 crypto_free_aead(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Herbert Xu1a6509d2008-01-28 19:37:29 -0800853static int esp_init_aead(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Herbert Xu7021b2e2015-05-27 16:03:46 +0800855 char aead_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu1a6509d2008-01-28 19:37:29 -0800856 struct crypto_aead *aead;
857 int err;
858
Herbert Xu7021b2e2015-05-27 16:03:46 +0800859 err = -ENAMETOOLONG;
860 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
861 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
862 goto error;
863
Steffen Klassertf58869c2017-12-20 10:41:53 +0100864 aead = crypto_alloc_aead(aead_name, 0, 0);
Herbert Xu1a6509d2008-01-28 19:37:29 -0800865 err = PTR_ERR(aead);
866 if (IS_ERR(aead))
867 goto error;
868
Mathias Krause1c5ad132013-10-18 12:09:05 +0200869 x->data = aead;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800870
871 err = crypto_aead_setkey(aead, x->aead->alg_key,
872 (x->aead->alg_key_len + 7) / 8);
873 if (err)
874 goto error;
875
876 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
877 if (err)
878 goto error;
879
880error:
881 return err;
882}
883
884static int esp_init_authenc(struct xfrm_state *x)
885{
Herbert Xu38320c72008-01-28 19:35:05 -0800886 struct crypto_aead *aead;
887 struct crypto_authenc_key_param *param;
888 struct rtattr *rta;
889 char *key;
890 char *p;
891 char authenc_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu38320c72008-01-28 19:35:05 -0800892 unsigned int keylen;
893 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Herbert Xu1a6509d2008-01-28 19:37:29 -0800895 err = -EINVAL;
Ian Morris51456b22015-04-03 09:17:26 +0100896 if (!x->ealg)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800897 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800898
Herbert Xu1a6509d2008-01-28 19:37:29 -0800899 err = -ENAMETOOLONG;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000900
901 if ((x->props.flags & XFRM_STATE_ESN)) {
902 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7021b2e2015-05-27 16:03:46 +0800903 "%s%sauthencesn(%s,%s)%s",
904 x->geniv ?: "", x->geniv ? "(" : "",
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000905 x->aalg ? x->aalg->alg_name : "digest_null",
Herbert Xu7021b2e2015-05-27 16:03:46 +0800906 x->ealg->alg_name,
907 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000908 goto error;
909 } else {
910 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7021b2e2015-05-27 16:03:46 +0800911 "%s%sauthenc(%s,%s)%s",
912 x->geniv ?: "", x->geniv ? "(" : "",
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000913 x->aalg ? x->aalg->alg_name : "digest_null",
Herbert Xu7021b2e2015-05-27 16:03:46 +0800914 x->ealg->alg_name,
915 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000916 goto error;
917 }
Herbert Xu38320c72008-01-28 19:35:05 -0800918
Steffen Klassertf58869c2017-12-20 10:41:53 +0100919 aead = crypto_alloc_aead(authenc_name, 0, 0);
Herbert Xu38320c72008-01-28 19:35:05 -0800920 err = PTR_ERR(aead);
921 if (IS_ERR(aead))
922 goto error;
923
Mathias Krause1c5ad132013-10-18 12:09:05 +0200924 x->data = aead;
Herbert Xu38320c72008-01-28 19:35:05 -0800925
926 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
927 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
928 err = -ENOMEM;
929 key = kmalloc(keylen, GFP_KERNEL);
930 if (!key)
931 goto error;
932
933 p = key;
934 rta = (void *)p;
935 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
936 rta->rta_len = RTA_LENGTH(sizeof(*param));
937 param = RTA_DATA(rta);
938 p += RTA_SPACE(sizeof(*param));
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (x->aalg) {
941 struct xfrm_algo_desc *aalg_desc;
942
Herbert Xu38320c72008-01-28 19:35:05 -0800943 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
944 p += (x->aalg->alg_key_len + 7) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
947 BUG_ON(!aalg_desc);
948
Herbert Xu38320c72008-01-28 19:35:05 -0800949 err = -EINVAL;
Joe Perches45083492014-11-05 15:36:08 -0800950 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
Herbert Xu38320c72008-01-28 19:35:05 -0800951 crypto_aead_authsize(aead)) {
Joe Perches45083492014-11-05 15:36:08 -0800952 pr_info("ESP: %s digestsize %u != %hu\n",
953 x->aalg->alg_name,
954 crypto_aead_authsize(aead),
955 aalg_desc->uinfo.auth.icv_fullbits / 8);
Herbert Xu38320c72008-01-28 19:35:05 -0800956 goto free_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
Herbert Xu38320c72008-01-28 19:35:05 -0800959 err = crypto_aead_setauthsize(
Martin Willi8f8a0882009-11-25 00:29:53 +0000960 aead, x->aalg->alg_trunc_len / 8);
Herbert Xu38320c72008-01-28 19:35:05 -0800961 if (err)
962 goto free_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Herbert Xu4b7137f2007-10-08 17:13:44 -0700964
Herbert Xu38320c72008-01-28 19:35:05 -0800965 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
966 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
967
968 err = crypto_aead_setkey(aead, key, keylen);
969
970free_key:
971 kfree(key);
972
Herbert Xu1a6509d2008-01-28 19:37:29 -0800973error:
974 return err;
975}
976
977static int esp_init_state(struct xfrm_state *x)
978{
Herbert Xu1a6509d2008-01-28 19:37:29 -0800979 struct crypto_aead *aead;
980 u32 align;
981 int err;
982
Mathias Krause1c5ad132013-10-18 12:09:05 +0200983 x->data = NULL;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800984
985 if (x->aead)
986 err = esp_init_aead(x);
987 else
988 err = esp_init_authenc(x);
989
Herbert Xu38320c72008-01-28 19:35:05 -0800990 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800992
Mathias Krause1c5ad132013-10-18 12:09:05 +0200993 aead = x->data;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800994
Herbert Xu38320c72008-01-28 19:35:05 -0800995 x->props.header_len = sizeof(struct ip_esp_hdr) +
996 crypto_aead_ivsize(aead);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700997 if (x->props.mode == XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 x->props.header_len += sizeof(struct iphdr);
Joakim Koskelaeb49e632008-08-06 02:39:30 -0700999 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
Patrick McHardyac758e32007-04-09 11:47:58 -07001000 x->props.header_len += IPV4_BEET_PHMAXLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (x->encap) {
1002 struct xfrm_encap_tmpl *encap = x->encap;
1003
1004 switch (encap->encap_type) {
1005 default:
Herbert Xubcfd09f2018-01-05 22:12:32 +11001006 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto error;
1008 case UDP_ENCAP_ESPINUDP:
1009 x->props.header_len += sizeof(struct udphdr);
1010 break;
1011 case UDP_ENCAP_ESPINUDP_NON_IKE:
1012 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1013 break;
1014 }
1015 }
Herbert Xu38320c72008-01-28 19:35:05 -08001016
1017 align = ALIGN(crypto_aead_blocksize(aead), 4);
Mathias Krause1c5ad132013-10-18 12:09:05 +02001018 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020error:
Herbert Xu38320c72008-01-28 19:35:05 -08001021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Steffen Klassert827789c2014-02-21 08:41:08 +01001024static int esp4_rcv_cb(struct sk_buff *skb, int err)
1025{
1026 return 0;
1027}
1028
Eric Dumazet533cb5b2008-01-30 19:11:50 -08001029static const struct xfrm_type esp_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 .description = "ESP4",
1032 .owner = THIS_MODULE,
1033 .proto = IPPROTO_ESP,
Herbert Xu436a0a42007-10-08 17:25:53 -07001034 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 .init_state = esp_init_state,
1036 .destructor = esp_destroy,
Patrick McHardyc5c25232007-04-09 11:47:18 -07001037 .get_mtu = esp4_get_mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .input = esp_input,
Steffen Klassertfca11eb2017-04-14 10:06:33 +02001039 .output = esp_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040};
1041
Steffen Klassert827789c2014-02-21 08:41:08 +01001042static struct xfrm4_protocol esp4_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .handler = xfrm4_rcv,
Steffen Klassert827789c2014-02-21 08:41:08 +01001044 .input_handler = xfrm_input,
1045 .cb_handler = esp4_rcv_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 .err_handler = esp4_err,
Steffen Klassert827789c2014-02-21 08:41:08 +01001047 .priority = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048};
1049
1050static int __init esp4_init(void)
1051{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001053 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EAGAIN;
1055 }
Steffen Klassert827789c2014-02-21 08:41:08 +01001056 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001057 pr_info("%s: can't add protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 xfrm_unregister_type(&esp_type, AF_INET);
1059 return -EAGAIN;
1060 }
1061 return 0;
1062}
1063
1064static void __exit esp4_fini(void)
1065{
Steffen Klassert827789c2014-02-21 08:41:08 +01001066 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001067 pr_info("%s: can't remove protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001069 pr_info("%s: can't remove xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072module_init(esp4_init);
1073module_exit(esp4_fini);
1074MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -07001075MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);