blob: 86c836fa21459d3446d7b6d0acf9069b4f43da37 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002#define pr_fmt(fmt) "IPsec: " fmt
3
4#include <crypto/aead.h>
5#include <crypto/authenc.h>
6#include <linux/err.h>
7#include <linux/module.h>
8#include <net/ip.h>
9#include <net/xfrm.h>
10#include <net/esp.h>
11#include <linux/scatterlist.h>
12#include <linux/kernel.h>
13#include <linux/pfkeyv2.h>
14#include <linux/rtnetlink.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/in6.h>
18#include <net/icmp.h>
19#include <net/protocol.h>
20#include <net/udp.h>
21
22#include <linux/highmem.h>
23
24struct esp_skb_cb {
25 struct xfrm_skb_cb xfrm;
26 void *tmp;
27};
28
29struct esp_output_extra {
30 __be32 seqhi;
31 u32 esphoff;
32};
33
34#define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
35
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036/*
37 * Allocate an AEAD request structure with extra space for SG and IV.
38 *
39 * For alignment considerations the IV is placed at the front, followed
40 * by the request and finally the SG list.
41 *
42 * TODO: Use spare space in skb for this where possible.
43 */
44static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
45{
46 unsigned int len;
47
48 len = extralen;
49
50 len += crypto_aead_ivsize(aead);
51
52 if (len) {
53 len += crypto_aead_alignmask(aead) &
54 ~(crypto_tfm_ctx_alignment() - 1);
55 len = ALIGN(len, crypto_tfm_ctx_alignment());
56 }
57
58 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
59 len = ALIGN(len, __alignof__(struct scatterlist));
60
61 len += sizeof(struct scatterlist) * nfrags;
62
63 return kmalloc(len, GFP_ATOMIC);
64}
65
66static inline void *esp_tmp_extra(void *tmp)
67{
68 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
69}
70
71static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
72{
73 return crypto_aead_ivsize(aead) ?
74 PTR_ALIGN((u8 *)tmp + extralen,
75 crypto_aead_alignmask(aead) + 1) : tmp + extralen;
76}
77
78static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
79{
80 struct aead_request *req;
81
82 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
83 crypto_tfm_ctx_alignment());
84 aead_request_set_tfm(req, aead);
85 return req;
86}
87
88static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
89 struct aead_request *req)
90{
91 return (void *)ALIGN((unsigned long)(req + 1) +
92 crypto_aead_reqsize(aead),
93 __alignof__(struct scatterlist));
94}
95
96static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
97{
98 struct esp_output_extra *extra = esp_tmp_extra(tmp);
99 struct crypto_aead *aead = x->data;
100 int extralen = 0;
101 u8 *iv;
102 struct aead_request *req;
103 struct scatterlist *sg;
104
105 if (x->props.flags & XFRM_STATE_ESN)
106 extralen += sizeof(*extra);
107
108 extra = esp_tmp_extra(tmp);
109 iv = esp_tmp_iv(aead, tmp, extralen);
110 req = esp_tmp_req(aead, iv);
111
112 /* Unref skb_frag_pages in the src scatterlist if necessary.
113 * Skip the first sg which comes from skb->data.
114 */
115 if (req->src != req->dst)
116 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
117 put_page(sg_page(sg));
118}
119
120static void esp_output_done(struct crypto_async_request *base, int err)
121{
122 struct sk_buff *skb = base->data;
123 struct xfrm_offload *xo = xfrm_offload(skb);
124 void *tmp;
125 struct xfrm_state *x;
126
David Brazdil0f672f62019-12-10 10:32:29 +0000127 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
128 struct sec_path *sp = skb_sec_path(skb);
129
130 x = sp->xvec[sp->len - 1];
131 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 x = skb_dst(skb)->xfrm;
David Brazdil0f672f62019-12-10 10:32:29 +0000133 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134
135 tmp = ESP_SKB_CB(skb)->tmp;
136 esp_ssg_unref(x, tmp);
137 kfree(tmp);
138
139 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
140 if (err) {
141 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
142 kfree_skb(skb);
143 return;
144 }
145
146 skb_push(skb, skb->data - skb_mac_header(skb));
147 secpath_reset(skb);
148 xfrm_dev_resume(skb);
149 } else {
150 xfrm_output_resume(skb, err);
151 }
152}
153
154/* Move ESP header back into place. */
155static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
156{
157 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
158 void *tmp = ESP_SKB_CB(skb)->tmp;
159 __be32 *seqhi = esp_tmp_extra(tmp);
160
161 esph->seq_no = esph->spi;
162 esph->spi = *seqhi;
163}
164
165static void esp_output_restore_header(struct sk_buff *skb)
166{
167 void *tmp = ESP_SKB_CB(skb)->tmp;
168 struct esp_output_extra *extra = esp_tmp_extra(tmp);
169
170 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
171 sizeof(__be32));
172}
173
174static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
175 struct xfrm_state *x,
176 struct ip_esp_hdr *esph,
177 struct esp_output_extra *extra)
178{
179 /* For ESN we move the header forward by 4 bytes to
180 * accomodate the high bits. We will move it back after
181 * encryption.
182 */
183 if ((x->props.flags & XFRM_STATE_ESN)) {
184 __u32 seqhi;
185 struct xfrm_offload *xo = xfrm_offload(skb);
186
187 if (xo)
188 seqhi = xo->seq.hi;
189 else
190 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
191
192 extra->esphoff = (unsigned char *)esph -
193 skb_transport_header(skb);
194 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
195 extra->seqhi = esph->spi;
196 esph->seq_no = htonl(seqhi);
197 }
198
199 esph->spi = x->id.spi;
200
201 return esph;
202}
203
204static void esp_output_done_esn(struct crypto_async_request *base, int err)
205{
206 struct sk_buff *skb = base->data;
207
208 esp_output_restore_header(skb);
209 esp_output_done(base, err);
210}
211
212static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
213{
214 /* Fill padding... */
215 if (tfclen) {
216 memset(tail, 0, tfclen);
217 tail += tfclen;
218 }
219 do {
220 int i;
221 for (i = 0; i < plen - 2; i++)
222 tail[i] = i + 1;
223 } while (0);
224 tail[plen - 2] = plen - 2;
225 tail[plen - 1] = proto;
226}
227
David Brazdil0f672f62019-12-10 10:32:29 +0000228static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229{
230 int encap_type;
231 struct udphdr *uh;
232 __be32 *udpdata32;
233 __be16 sport, dport;
234 struct xfrm_encap_tmpl *encap = x->encap;
235 struct ip_esp_hdr *esph = esp->esph;
David Brazdil0f672f62019-12-10 10:32:29 +0000236 unsigned int len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237
238 spin_lock_bh(&x->lock);
239 sport = encap->encap_sport;
240 dport = encap->encap_dport;
241 encap_type = encap->encap_type;
242 spin_unlock_bh(&x->lock);
243
David Brazdil0f672f62019-12-10 10:32:29 +0000244 len = skb->len + esp->tailen - skb_transport_offset(skb);
245 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
246 return -EMSGSIZE;
247
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000248 uh = (struct udphdr *)esph;
249 uh->source = sport;
250 uh->dest = dport;
David Brazdil0f672f62019-12-10 10:32:29 +0000251 uh->len = htons(len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252 uh->check = 0;
253
254 switch (encap_type) {
255 default:
256 case UDP_ENCAP_ESPINUDP:
257 esph = (struct ip_esp_hdr *)(uh + 1);
258 break;
259 case UDP_ENCAP_ESPINUDP_NON_IKE:
260 udpdata32 = (__be32 *)(uh + 1);
261 udpdata32[0] = udpdata32[1] = 0;
262 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
263 break;
264 }
265
266 *skb_mac_header(skb) = IPPROTO_UDP;
267 esp->esph = esph;
David Brazdil0f672f62019-12-10 10:32:29 +0000268
269 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270}
271
272int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
273{
274 u8 *tail;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275 int nfrags;
276 int esph_offset;
277 struct page *page;
278 struct sk_buff *trailer;
279 int tailen = esp->tailen;
280
281 /* this is non-NULL only with UDP Encapsulation */
David Brazdil0f672f62019-12-10 10:32:29 +0000282 if (x->encap) {
283 int err = esp_output_udp_encap(x, skb, esp);
284
285 if (err < 0)
286 return err;
287 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288
289 if (!skb_cloned(skb)) {
290 if (tailen <= skb_tailroom(skb)) {
291 nfrags = 1;
292 trailer = skb;
293 tail = skb_tail_pointer(trailer);
294
295 goto skip_cow;
296 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
297 && !skb_has_frag_list(skb)) {
298 int allocsize;
299 struct sock *sk = skb->sk;
300 struct page_frag *pfrag = &x->xfrag;
301
302 esp->inplace = false;
303
304 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
305
306 spin_lock_bh(&x->lock);
307
308 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
309 spin_unlock_bh(&x->lock);
310 goto cow;
311 }
312
313 page = pfrag->page;
314 get_page(page);
315
Olivier Deprez0e641232021-09-23 10:07:05 +0200316 tail = page_address(page) + pfrag->offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317
318 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
319
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000320 nfrags = skb_shinfo(skb)->nr_frags;
321
322 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
323 tailen);
324 skb_shinfo(skb)->nr_frags = ++nfrags;
325
326 pfrag->offset = pfrag->offset + allocsize;
327
328 spin_unlock_bh(&x->lock);
329
330 nfrags++;
331
332 skb->len += tailen;
333 skb->data_len += tailen;
334 skb->truesize += tailen;
David Brazdil0f672f62019-12-10 10:32:29 +0000335 if (sk && sk_fullsock(sk))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336 refcount_add(tailen, &sk->sk_wmem_alloc);
337
338 goto out;
339 }
340 }
341
342cow:
343 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
344
345 nfrags = skb_cow_data(skb, tailen, &trailer);
346 if (nfrags < 0)
347 goto out;
348 tail = skb_tail_pointer(trailer);
349 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
350
351skip_cow:
352 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
353 pskb_put(skb, trailer, tailen);
354
355out:
356 return nfrags;
357}
358EXPORT_SYMBOL_GPL(esp_output_head);
359
360int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
361{
362 u8 *iv;
363 int alen;
364 void *tmp;
365 int ivlen;
366 int assoclen;
367 int extralen;
368 struct page *page;
369 struct ip_esp_hdr *esph;
370 struct crypto_aead *aead;
371 struct aead_request *req;
372 struct scatterlist *sg, *dsg;
373 struct esp_output_extra *extra;
374 int err = -ENOMEM;
375
376 assoclen = sizeof(struct ip_esp_hdr);
377 extralen = 0;
378
379 if (x->props.flags & XFRM_STATE_ESN) {
380 extralen += sizeof(*extra);
381 assoclen += sizeof(__be32);
382 }
383
384 aead = x->data;
385 alen = crypto_aead_authsize(aead);
386 ivlen = crypto_aead_ivsize(aead);
387
388 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
389 if (!tmp)
390 goto error;
391
392 extra = esp_tmp_extra(tmp);
393 iv = esp_tmp_iv(aead, tmp, extralen);
394 req = esp_tmp_req(aead, iv);
395 sg = esp_req_sg(aead, req);
396
397 if (esp->inplace)
398 dsg = sg;
399 else
400 dsg = &sg[esp->nfrags];
401
402 esph = esp_output_set_extra(skb, x, esp->esph, extra);
403 esp->esph = esph;
404
405 sg_init_table(sg, esp->nfrags);
406 err = skb_to_sgvec(skb, sg,
407 (unsigned char *)esph - skb->data,
408 assoclen + ivlen + esp->clen + alen);
409 if (unlikely(err < 0))
410 goto error_free;
411
412 if (!esp->inplace) {
413 int allocsize;
414 struct page_frag *pfrag = &x->xfrag;
415
416 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
417
418 spin_lock_bh(&x->lock);
419 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
420 spin_unlock_bh(&x->lock);
421 goto error_free;
422 }
423
424 skb_shinfo(skb)->nr_frags = 1;
425
426 page = pfrag->page;
427 get_page(page);
428 /* replace page frags in skb with new page */
429 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
430 pfrag->offset = pfrag->offset + allocsize;
431 spin_unlock_bh(&x->lock);
432
433 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
434 err = skb_to_sgvec(skb, dsg,
435 (unsigned char *)esph - skb->data,
436 assoclen + ivlen + esp->clen + alen);
437 if (unlikely(err < 0))
438 goto error_free;
439 }
440
441 if ((x->props.flags & XFRM_STATE_ESN))
442 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
443 else
444 aead_request_set_callback(req, 0, esp_output_done, skb);
445
446 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
447 aead_request_set_ad(req, assoclen);
448
449 memset(iv, 0, ivlen);
450 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
451 min(ivlen, 8));
452
453 ESP_SKB_CB(skb)->tmp = tmp;
454 err = crypto_aead_encrypt(req);
455
456 switch (err) {
457 case -EINPROGRESS:
458 goto error;
459
460 case -ENOSPC:
461 err = NET_XMIT_DROP;
462 break;
463
464 case 0:
465 if ((x->props.flags & XFRM_STATE_ESN))
466 esp_output_restore_header(skb);
467 }
468
469 if (sg != dsg)
470 esp_ssg_unref(x, tmp);
471
472error_free:
473 kfree(tmp);
474error:
475 return err;
476}
477EXPORT_SYMBOL_GPL(esp_output_tail);
478
479static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
480{
481 int alen;
482 int blksize;
483 struct ip_esp_hdr *esph;
484 struct crypto_aead *aead;
485 struct esp_info esp;
486
487 esp.inplace = true;
488
489 esp.proto = *skb_mac_header(skb);
490 *skb_mac_header(skb) = IPPROTO_ESP;
491
492 /* skb is pure payload to encrypt */
493
494 aead = x->data;
495 alen = crypto_aead_authsize(aead);
496
497 esp.tfclen = 0;
498 if (x->tfcpad) {
499 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
500 u32 padto;
501
Olivier Deprez0e641232021-09-23 10:07:05 +0200502 padto = min(x->tfcpad, __xfrm_state_mtu(x, dst->child_mtu_cached));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 if (skb->len < padto)
504 esp.tfclen = padto - skb->len;
505 }
506 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
507 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
508 esp.plen = esp.clen - skb->len - esp.tfclen;
509 esp.tailen = esp.tfclen + esp.plen + alen;
510
511 esp.esph = ip_esp_hdr(skb);
512
513 esp.nfrags = esp_output_head(x, skb, &esp);
514 if (esp.nfrags < 0)
515 return esp.nfrags;
516
517 esph = esp.esph;
518 esph->spi = x->id.spi;
519
520 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
521 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
522 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
523
524 skb_push(skb, -skb_network_offset(skb));
525
526 return esp_output_tail(x, skb, &esp);
527}
528
529static inline int esp_remove_trailer(struct sk_buff *skb)
530{
531 struct xfrm_state *x = xfrm_input_state(skb);
532 struct xfrm_offload *xo = xfrm_offload(skb);
533 struct crypto_aead *aead = x->data;
534 int alen, hlen, elen;
535 int padlen, trimlen;
536 __wsum csumdiff;
537 u8 nexthdr[2];
538 int ret;
539
540 alen = crypto_aead_authsize(aead);
541 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
542 elen = skb->len - hlen;
543
544 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
545 ret = xo->proto;
546 goto out;
547 }
548
549 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
550 BUG();
551
552 ret = -EINVAL;
553 padlen = nexthdr[0];
554 if (padlen + 2 + alen >= elen) {
555 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
556 padlen + 2, elen - alen);
557 goto out;
558 }
559
560 trimlen = alen + padlen + 2;
561 if (skb->ip_summed == CHECKSUM_COMPLETE) {
562 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
563 skb->csum = csum_block_sub(skb->csum, csumdiff,
564 skb->len - trimlen);
565 }
566 pskb_trim(skb, skb->len - trimlen);
567
568 ret = nexthdr[1];
569
570out:
571 return ret;
572}
573
574int esp_input_done2(struct sk_buff *skb, int err)
575{
576 const struct iphdr *iph;
577 struct xfrm_state *x = xfrm_input_state(skb);
578 struct xfrm_offload *xo = xfrm_offload(skb);
579 struct crypto_aead *aead = x->data;
580 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
581 int ihl;
582
583 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
584 kfree(ESP_SKB_CB(skb)->tmp);
585
586 if (unlikely(err))
587 goto out;
588
589 err = esp_remove_trailer(skb);
590 if (unlikely(err < 0))
591 goto out;
592
593 iph = ip_hdr(skb);
594 ihl = iph->ihl * 4;
595
596 if (x->encap) {
597 struct xfrm_encap_tmpl *encap = x->encap;
598 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
599
600 /*
601 * 1) if the NAT-T peer's IP or port changed then
602 * advertize the change to the keying daemon.
603 * This is an inbound SA, so just compare
604 * SRC ports.
605 */
606 if (iph->saddr != x->props.saddr.a4 ||
607 uh->source != encap->encap_sport) {
608 xfrm_address_t ipaddr;
609
610 ipaddr.a4 = iph->saddr;
611 km_new_mapping(x, &ipaddr, uh->source);
612
613 /* XXX: perhaps add an extra
614 * policy check here, to see
615 * if we should allow or
616 * reject a packet from a
617 * different source
618 * address/port.
619 */
620 }
621
622 /*
623 * 2) ignore UDP/TCP checksums in case
624 * of NAT-T in Transport Mode, or
625 * perform other post-processing fixes
626 * as per draft-ietf-ipsec-udp-encaps-06,
627 * section 3.1.2
628 */
629 if (x->props.mode == XFRM_MODE_TRANSPORT)
630 skb->ip_summed = CHECKSUM_UNNECESSARY;
631 }
632
633 skb_pull_rcsum(skb, hlen);
634 if (x->props.mode == XFRM_MODE_TUNNEL)
635 skb_reset_transport_header(skb);
636 else
637 skb_set_transport_header(skb, -ihl);
638
639 /* RFC4303: Drop dummy packets without any error */
640 if (err == IPPROTO_NONE)
641 err = -EINVAL;
642
643out:
644 return err;
645}
646EXPORT_SYMBOL_GPL(esp_input_done2);
647
648static void esp_input_done(struct crypto_async_request *base, int err)
649{
650 struct sk_buff *skb = base->data;
651
652 xfrm_input_resume(skb, esp_input_done2(skb, err));
653}
654
655static void esp_input_restore_header(struct sk_buff *skb)
656{
657 esp_restore_header(skb, 0);
658 __skb_pull(skb, 4);
659}
660
661static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
662{
663 struct xfrm_state *x = xfrm_input_state(skb);
664 struct ip_esp_hdr *esph;
665
666 /* For ESN we move the header forward by 4 bytes to
667 * accomodate the high bits. We will move it back after
668 * decryption.
669 */
670 if ((x->props.flags & XFRM_STATE_ESN)) {
671 esph = skb_push(skb, 4);
672 *seqhi = esph->spi;
673 esph->spi = esph->seq_no;
674 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
675 }
676}
677
678static void esp_input_done_esn(struct crypto_async_request *base, int err)
679{
680 struct sk_buff *skb = base->data;
681
682 esp_input_restore_header(skb);
683 esp_input_done(base, err);
684}
685
686/*
687 * Note: detecting truncated vs. non-truncated authentication data is very
688 * expensive, so we only support truncated data, which is the recommended
689 * and common case.
690 */
691static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
692{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693 struct crypto_aead *aead = x->data;
694 struct aead_request *req;
695 struct sk_buff *trailer;
696 int ivlen = crypto_aead_ivsize(aead);
David Brazdil0f672f62019-12-10 10:32:29 +0000697 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698 int nfrags;
699 int assoclen;
700 int seqhilen;
701 __be32 *seqhi;
702 void *tmp;
703 u8 *iv;
704 struct scatterlist *sg;
705 int err = -EINVAL;
706
David Brazdil0f672f62019-12-10 10:32:29 +0000707 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708 goto out;
709
710 if (elen <= 0)
711 goto out;
712
David Brazdil0f672f62019-12-10 10:32:29 +0000713 assoclen = sizeof(struct ip_esp_hdr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000714 seqhilen = 0;
715
716 if (x->props.flags & XFRM_STATE_ESN) {
717 seqhilen += sizeof(__be32);
718 assoclen += seqhilen;
719 }
720
721 if (!skb_cloned(skb)) {
722 if (!skb_is_nonlinear(skb)) {
723 nfrags = 1;
724
725 goto skip_cow;
726 } else if (!skb_has_frag_list(skb)) {
727 nfrags = skb_shinfo(skb)->nr_frags;
728 nfrags++;
729
730 goto skip_cow;
731 }
732 }
733
734 err = skb_cow_data(skb, 0, &trailer);
735 if (err < 0)
736 goto out;
737
738 nfrags = err;
739
740skip_cow:
741 err = -ENOMEM;
742 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
743 if (!tmp)
744 goto out;
745
746 ESP_SKB_CB(skb)->tmp = tmp;
747 seqhi = esp_tmp_extra(tmp);
748 iv = esp_tmp_iv(aead, tmp, seqhilen);
749 req = esp_tmp_req(aead, iv);
750 sg = esp_req_sg(aead, req);
751
752 esp_input_set_header(skb, seqhi);
753
754 sg_init_table(sg, nfrags);
755 err = skb_to_sgvec(skb, sg, 0, skb->len);
756 if (unlikely(err < 0)) {
757 kfree(tmp);
758 goto out;
759 }
760
761 skb->ip_summed = CHECKSUM_NONE;
762
763 if ((x->props.flags & XFRM_STATE_ESN))
764 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
765 else
766 aead_request_set_callback(req, 0, esp_input_done, skb);
767
768 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
769 aead_request_set_ad(req, assoclen);
770
771 err = crypto_aead_decrypt(req);
772 if (err == -EINPROGRESS)
773 goto out;
774
775 if ((x->props.flags & XFRM_STATE_ESN))
776 esp_input_restore_header(skb);
777
778 err = esp_input_done2(skb, err);
779
780out:
781 return err;
782}
783
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000784static int esp4_err(struct sk_buff *skb, u32 info)
785{
786 struct net *net = dev_net(skb->dev);
787 const struct iphdr *iph = (const struct iphdr *)skb->data;
788 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
789 struct xfrm_state *x;
790
791 switch (icmp_hdr(skb)->type) {
792 case ICMP_DEST_UNREACH:
793 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
794 return 0;
795 case ICMP_REDIRECT:
796 break;
797 default:
798 return 0;
799 }
800
801 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
802 esph->spi, IPPROTO_ESP, AF_INET);
803 if (!x)
804 return 0;
805
806 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
David Brazdil0f672f62019-12-10 10:32:29 +0000807 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000808 else
David Brazdil0f672f62019-12-10 10:32:29 +0000809 ipv4_redirect(skb, net, 0, IPPROTO_ESP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000810 xfrm_state_put(x);
811
812 return 0;
813}
814
815static void esp_destroy(struct xfrm_state *x)
816{
817 struct crypto_aead *aead = x->data;
818
819 if (!aead)
820 return;
821
822 crypto_free_aead(aead);
823}
824
825static int esp_init_aead(struct xfrm_state *x)
826{
827 char aead_name[CRYPTO_MAX_ALG_NAME];
828 struct crypto_aead *aead;
829 int err;
830
831 err = -ENAMETOOLONG;
832 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
833 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
834 goto error;
835
836 aead = crypto_alloc_aead(aead_name, 0, 0);
837 err = PTR_ERR(aead);
838 if (IS_ERR(aead))
839 goto error;
840
841 x->data = aead;
842
843 err = crypto_aead_setkey(aead, x->aead->alg_key,
844 (x->aead->alg_key_len + 7) / 8);
845 if (err)
846 goto error;
847
848 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
849 if (err)
850 goto error;
851
852error:
853 return err;
854}
855
856static int esp_init_authenc(struct xfrm_state *x)
857{
858 struct crypto_aead *aead;
859 struct crypto_authenc_key_param *param;
860 struct rtattr *rta;
861 char *key;
862 char *p;
863 char authenc_name[CRYPTO_MAX_ALG_NAME];
864 unsigned int keylen;
865 int err;
866
867 err = -EINVAL;
868 if (!x->ealg)
869 goto error;
870
871 err = -ENAMETOOLONG;
872
873 if ((x->props.flags & XFRM_STATE_ESN)) {
874 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
875 "%s%sauthencesn(%s,%s)%s",
876 x->geniv ?: "", x->geniv ? "(" : "",
877 x->aalg ? x->aalg->alg_name : "digest_null",
878 x->ealg->alg_name,
879 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
880 goto error;
881 } else {
882 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
883 "%s%sauthenc(%s,%s)%s",
884 x->geniv ?: "", x->geniv ? "(" : "",
885 x->aalg ? x->aalg->alg_name : "digest_null",
886 x->ealg->alg_name,
887 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
888 goto error;
889 }
890
891 aead = crypto_alloc_aead(authenc_name, 0, 0);
892 err = PTR_ERR(aead);
893 if (IS_ERR(aead))
894 goto error;
895
896 x->data = aead;
897
898 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
899 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
900 err = -ENOMEM;
901 key = kmalloc(keylen, GFP_KERNEL);
902 if (!key)
903 goto error;
904
905 p = key;
906 rta = (void *)p;
907 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
908 rta->rta_len = RTA_LENGTH(sizeof(*param));
909 param = RTA_DATA(rta);
910 p += RTA_SPACE(sizeof(*param));
911
912 if (x->aalg) {
913 struct xfrm_algo_desc *aalg_desc;
914
915 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
916 p += (x->aalg->alg_key_len + 7) / 8;
917
918 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
919 BUG_ON(!aalg_desc);
920
921 err = -EINVAL;
922 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
923 crypto_aead_authsize(aead)) {
924 pr_info("ESP: %s digestsize %u != %hu\n",
925 x->aalg->alg_name,
926 crypto_aead_authsize(aead),
927 aalg_desc->uinfo.auth.icv_fullbits / 8);
928 goto free_key;
929 }
930
931 err = crypto_aead_setauthsize(
932 aead, x->aalg->alg_trunc_len / 8);
933 if (err)
934 goto free_key;
935 }
936
937 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
938 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
939
940 err = crypto_aead_setkey(aead, key, keylen);
941
942free_key:
943 kfree(key);
944
945error:
946 return err;
947}
948
949static int esp_init_state(struct xfrm_state *x)
950{
951 struct crypto_aead *aead;
952 u32 align;
953 int err;
954
955 x->data = NULL;
956
957 if (x->aead)
958 err = esp_init_aead(x);
959 else
960 err = esp_init_authenc(x);
961
962 if (err)
963 goto error;
964
965 aead = x->data;
966
967 x->props.header_len = sizeof(struct ip_esp_hdr) +
968 crypto_aead_ivsize(aead);
969 if (x->props.mode == XFRM_MODE_TUNNEL)
970 x->props.header_len += sizeof(struct iphdr);
971 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
972 x->props.header_len += IPV4_BEET_PHMAXLEN;
973 if (x->encap) {
974 struct xfrm_encap_tmpl *encap = x->encap;
975
976 switch (encap->encap_type) {
977 default:
978 err = -EINVAL;
979 goto error;
980 case UDP_ENCAP_ESPINUDP:
981 x->props.header_len += sizeof(struct udphdr);
982 break;
983 case UDP_ENCAP_ESPINUDP_NON_IKE:
984 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
985 break;
986 }
987 }
988
989 align = ALIGN(crypto_aead_blocksize(aead), 4);
990 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
991
992error:
993 return err;
994}
995
996static int esp4_rcv_cb(struct sk_buff *skb, int err)
997{
998 return 0;
999}
1000
1001static const struct xfrm_type esp_type =
1002{
1003 .description = "ESP4",
1004 .owner = THIS_MODULE,
1005 .proto = IPPROTO_ESP,
1006 .flags = XFRM_TYPE_REPLAY_PROT,
1007 .init_state = esp_init_state,
1008 .destructor = esp_destroy,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009 .input = esp_input,
1010 .output = esp_output,
1011};
1012
1013static struct xfrm4_protocol esp4_protocol = {
1014 .handler = xfrm4_rcv,
1015 .input_handler = xfrm_input,
1016 .cb_handler = esp4_rcv_cb,
1017 .err_handler = esp4_err,
1018 .priority = 0,
1019};
1020
1021static int __init esp4_init(void)
1022{
1023 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
1024 pr_info("%s: can't add xfrm type\n", __func__);
1025 return -EAGAIN;
1026 }
1027 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
1028 pr_info("%s: can't add protocol\n", __func__);
1029 xfrm_unregister_type(&esp_type, AF_INET);
1030 return -EAGAIN;
1031 }
1032 return 0;
1033}
1034
1035static void __exit esp4_fini(void)
1036{
1037 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
1038 pr_info("%s: can't remove protocol\n", __func__);
David Brazdil0f672f62019-12-10 10:32:29 +00001039 xfrm_unregister_type(&esp_type, AF_INET);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001040}
1041
1042module_init(esp4_init);
1043module_exit(esp4_fini);
1044MODULE_LICENSE("GPL");
1045MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);