1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C)2002 USAGI/WIDE Project
4 *
5 * Authors
6 *
7 * Mitsuru KANDA @USAGI : IPv6 Support
8 * Kazunori MIYAZAWA @USAGI :
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 *
11 * This file is derived from net/ipv4/esp.c
12 */
13
14 #define pr_fmt(fmt) "IPv6: " fmt
15
16 #include <crypto/aead.h>
17 #include <crypto/authenc.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
20 #include <net/ip.h>
21 #include <net/xfrm.h>
22 #include <net/esp.h>
23 #include <linux/scatterlist.h>
24 #include <linux/kernel.h>
25 #include <linux/pfkeyv2.h>
26 #include <linux/random.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <net/ip6_checksum.h>
30 #include <net/ip6_route.h>
31 #include <net/icmp.h>
32 #include <net/ipv6.h>
33 #include <net/protocol.h>
34 #include <net/udp.h>
35 #include <linux/icmpv6.h>
36 #include <net/tcp.h>
37 #include <net/espintcp.h>
38 #include <net/inet6_hashtables.h>
39
40 #include <linux/highmem.h>
41
42 struct esp_skb_cb {
43 struct xfrm_skb_cb xfrm;
44 void *tmp;
45 };
46
47 struct esp_output_extra {
48 __be32 seqhi;
49 u32 esphoff;
50 };
51
52 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
53
54 /*
55 * Allocate an AEAD request structure with extra space for SG and IV.
56 *
57 * For alignment considerations the upper 32 bits of the sequence number are
58 * placed at the front, if present. Followed by the IV, the request and finally
59 * the SG list.
60 *
61 * TODO: Use spare space in skb for this where possible.
62 */
esp_alloc_tmp(struct crypto_aead * aead,int nfrags,int seqihlen)63 static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int seqihlen)
64 {
65 unsigned int len;
66
67 len = seqihlen;
68
69 len += crypto_aead_ivsize(aead);
70
71 if (len) {
72 len += crypto_aead_alignmask(aead) &
73 ~(crypto_tfm_ctx_alignment() - 1);
74 len = ALIGN(len, crypto_tfm_ctx_alignment());
75 }
76
77 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
78 len = ALIGN(len, __alignof__(struct scatterlist));
79
80 len += sizeof(struct scatterlist) * nfrags;
81
82 return kmalloc(len, GFP_ATOMIC);
83 }
84
esp_tmp_extra(void * tmp)85 static inline void *esp_tmp_extra(void *tmp)
86 {
87 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
88 }
89
esp_tmp_iv(struct crypto_aead * aead,void * tmp,int seqhilen)90 static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int seqhilen)
91 {
92 return crypto_aead_ivsize(aead) ?
93 PTR_ALIGN((u8 *)tmp + seqhilen,
94 crypto_aead_alignmask(aead) + 1) : tmp + seqhilen;
95 }
96
esp_tmp_req(struct crypto_aead * aead,u8 * iv)97 static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
98 {
99 struct aead_request *req;
100
101 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
102 crypto_tfm_ctx_alignment());
103 aead_request_set_tfm(req, aead);
104 return req;
105 }
106
esp_req_sg(struct crypto_aead * aead,struct aead_request * req)107 static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
108 struct aead_request *req)
109 {
110 return (void *)ALIGN((unsigned long)(req + 1) +
111 crypto_aead_reqsize(aead),
112 __alignof__(struct scatterlist));
113 }
114
esp_ssg_unref(struct xfrm_state * x,void * tmp,struct sk_buff * skb)115 static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb)
116 {
117 struct crypto_aead *aead = x->data;
118 int extralen = 0;
119 u8 *iv;
120 struct aead_request *req;
121 struct scatterlist *sg;
122
123 if (x->props.flags & XFRM_STATE_ESN)
124 extralen += sizeof(struct esp_output_extra);
125
126 iv = esp_tmp_iv(aead, tmp, extralen);
127 req = esp_tmp_req(aead, iv);
128
129 /* Unref skb_frag_pages in the src scatterlist if necessary.
130 * Skip the first sg which comes from skb->data.
131 */
132 if (req->src != req->dst)
133 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
134 skb_page_unref(skb, sg_page(sg), false);
135 }
136
137 #ifdef CONFIG_INET6_ESPINTCP
esp6_find_tcp_sk(struct xfrm_state * x)138 static struct sock *esp6_find_tcp_sk(struct xfrm_state *x)
139 {
140 struct xfrm_encap_tmpl *encap = x->encap;
141 struct net *net = xs_net(x);
142 __be16 sport, dport;
143 struct sock *sk;
144
145 spin_lock_bh(&x->lock);
146 sport = encap->encap_sport;
147 dport = encap->encap_dport;
148 spin_unlock_bh(&x->lock);
149
150 sk = __inet6_lookup_established(net, net->ipv4.tcp_death_row.hashinfo, &x->id.daddr.in6,
151 dport, &x->props.saddr.in6, ntohs(sport), 0, 0);
152 if (!sk)
153 return ERR_PTR(-ENOENT);
154
155 if (!tcp_is_ulp_esp(sk)) {
156 sock_put(sk);
157 return ERR_PTR(-EINVAL);
158 }
159
160 return sk;
161 }
162
esp_output_tcp_finish(struct xfrm_state * x,struct sk_buff * skb)163 static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
164 {
165 struct sock *sk;
166 int err;
167
168 rcu_read_lock();
169
170 sk = esp6_find_tcp_sk(x);
171 err = PTR_ERR_OR_ZERO(sk);
172 if (err) {
173 kfree_skb(skb);
174 goto out;
175 }
176
177 bh_lock_sock(sk);
178 if (sock_owned_by_user(sk))
179 err = espintcp_queue_out(sk, skb);
180 else
181 err = espintcp_push_skb(sk, skb);
182 bh_unlock_sock(sk);
183
184 sock_put(sk);
185
186 out:
187 rcu_read_unlock();
188 return err;
189 }
190
esp_output_tcp_encap_cb(struct net * net,struct sock * sk,struct sk_buff * skb)191 static int esp_output_tcp_encap_cb(struct net *net, struct sock *sk,
192 struct sk_buff *skb)
193 {
194 struct dst_entry *dst = skb_dst(skb);
195 struct xfrm_state *x = dst->xfrm;
196
197 return esp_output_tcp_finish(x, skb);
198 }
199
esp_output_tail_tcp(struct xfrm_state * x,struct sk_buff * skb)200 static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
201 {
202 int err;
203
204 local_bh_disable();
205 err = xfrm_trans_queue_net(xs_net(x), skb, esp_output_tcp_encap_cb);
206 local_bh_enable();
207
208 /* EINPROGRESS just happens to do the right thing. It
209 * actually means that the skb has been consumed and
210 * isn't coming back.
211 */
212 return err ?: -EINPROGRESS;
213 }
214 #else
esp_output_tail_tcp(struct xfrm_state * x,struct sk_buff * skb)215 static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
216 {
217 WARN_ON(1);
218 return -EOPNOTSUPP;
219 }
220 #endif
221
esp_output_encap_csum(struct sk_buff * skb)222 static void esp_output_encap_csum(struct sk_buff *skb)
223 {
224 /* UDP encap with IPv6 requires a valid checksum */
225 if (*skb_mac_header(skb) == IPPROTO_UDP) {
226 struct udphdr *uh = udp_hdr(skb);
227 struct ipv6hdr *ip6h = ipv6_hdr(skb);
228 int len = ntohs(uh->len);
229 unsigned int offset = skb_transport_offset(skb);
230 __wsum csum = skb_checksum(skb, offset, skb->len - offset, 0);
231
232 uh->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
233 len, IPPROTO_UDP, csum);
234 if (uh->check == 0)
235 uh->check = CSUM_MANGLED_0;
236 }
237 }
238
esp_output_done(void * data,int err)239 static void esp_output_done(void *data, int err)
240 {
241 struct sk_buff *skb = data;
242 struct xfrm_offload *xo = xfrm_offload(skb);
243 void *tmp;
244 struct xfrm_state *x;
245
246 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
247 struct sec_path *sp = skb_sec_path(skb);
248
249 x = sp->xvec[sp->len - 1];
250 } else {
251 x = skb_dst(skb)->xfrm;
252 }
253
254 tmp = ESP_SKB_CB(skb)->tmp;
255 esp_ssg_unref(x, tmp, skb);
256 kfree(tmp);
257
258 esp_output_encap_csum(skb);
259
260 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
261 if (err) {
262 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
263 kfree_skb(skb);
264 return;
265 }
266
267 skb_push(skb, skb->data - skb_mac_header(skb));
268 secpath_reset(skb);
269 xfrm_dev_resume(skb);
270 } else {
271 if (!err &&
272 x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
273 esp_output_tail_tcp(x, skb);
274 else
275 xfrm_output_resume(skb->sk, skb, err);
276 }
277 }
278
279 /* Move ESP header back into place. */
esp_restore_header(struct sk_buff * skb,unsigned int offset)280 static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
281 {
282 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
283 void *tmp = ESP_SKB_CB(skb)->tmp;
284 __be32 *seqhi = esp_tmp_extra(tmp);
285
286 esph->seq_no = esph->spi;
287 esph->spi = *seqhi;
288 }
289
esp_output_restore_header(struct sk_buff * skb)290 static void esp_output_restore_header(struct sk_buff *skb)
291 {
292 void *tmp = ESP_SKB_CB(skb)->tmp;
293 struct esp_output_extra *extra = esp_tmp_extra(tmp);
294
295 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
296 sizeof(__be32));
297 }
298
esp_output_set_esn(struct sk_buff * skb,struct xfrm_state * x,struct ip_esp_hdr * esph,struct esp_output_extra * extra)299 static struct ip_esp_hdr *esp_output_set_esn(struct sk_buff *skb,
300 struct xfrm_state *x,
301 struct ip_esp_hdr *esph,
302 struct esp_output_extra *extra)
303 {
304 /* For ESN we move the header forward by 4 bytes to
305 * accommodate the high bits. We will move it back after
306 * encryption.
307 */
308 if ((x->props.flags & XFRM_STATE_ESN)) {
309 __u32 seqhi;
310 struct xfrm_offload *xo = xfrm_offload(skb);
311
312 if (xo)
313 seqhi = xo->seq.hi;
314 else
315 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
316
317 extra->esphoff = (unsigned char *)esph -
318 skb_transport_header(skb);
319 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
320 extra->seqhi = esph->spi;
321 esph->seq_no = htonl(seqhi);
322 }
323
324 esph->spi = x->id.spi;
325
326 return esph;
327 }
328
esp_output_done_esn(void * data,int err)329 static void esp_output_done_esn(void *data, int err)
330 {
331 struct sk_buff *skb = data;
332
333 esp_output_restore_header(skb);
334 esp_output_done(data, err);
335 }
336
esp6_output_udp_encap(struct sk_buff * skb,int encap_type,struct esp_info * esp,__be16 sport,__be16 dport)337 static struct ip_esp_hdr *esp6_output_udp_encap(struct sk_buff *skb,
338 int encap_type,
339 struct esp_info *esp,
340 __be16 sport,
341 __be16 dport)
342 {
343 struct udphdr *uh;
344 __be32 *udpdata32;
345 unsigned int len;
346
347 len = skb->len + esp->tailen - skb_transport_offset(skb);
348 if (len > U16_MAX)
349 return ERR_PTR(-EMSGSIZE);
350
351 uh = (struct udphdr *)esp->esph;
352 uh->source = sport;
353 uh->dest = dport;
354 uh->len = htons(len);
355 uh->check = 0;
356
357 *skb_mac_header(skb) = IPPROTO_UDP;
358
359 if (encap_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
360 udpdata32 = (__be32 *)(uh + 1);
361 udpdata32[0] = udpdata32[1] = 0;
362 return (struct ip_esp_hdr *)(udpdata32 + 2);
363 }
364
365 return (struct ip_esp_hdr *)(uh + 1);
366 }
367
368 #ifdef CONFIG_INET6_ESPINTCP
esp6_output_tcp_encap(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)369 static struct ip_esp_hdr *esp6_output_tcp_encap(struct xfrm_state *x,
370 struct sk_buff *skb,
371 struct esp_info *esp)
372 {
373 __be16 *lenp = (void *)esp->esph;
374 struct ip_esp_hdr *esph;
375 unsigned int len;
376 struct sock *sk;
377
378 len = skb->len + esp->tailen - skb_transport_offset(skb);
379 if (len > IP_MAX_MTU)
380 return ERR_PTR(-EMSGSIZE);
381
382 rcu_read_lock();
383 sk = esp6_find_tcp_sk(x);
384 rcu_read_unlock();
385
386 if (IS_ERR(sk))
387 return ERR_CAST(sk);
388
389 sock_put(sk);
390
391 *lenp = htons(len);
392 esph = (struct ip_esp_hdr *)(lenp + 1);
393
394 return esph;
395 }
396 #else
esp6_output_tcp_encap(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)397 static struct ip_esp_hdr *esp6_output_tcp_encap(struct xfrm_state *x,
398 struct sk_buff *skb,
399 struct esp_info *esp)
400 {
401 return ERR_PTR(-EOPNOTSUPP);
402 }
403 #endif
404
esp6_output_encap(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)405 static int esp6_output_encap(struct xfrm_state *x, struct sk_buff *skb,
406 struct esp_info *esp)
407 {
408 struct xfrm_encap_tmpl *encap = x->encap;
409 struct ip_esp_hdr *esph;
410 __be16 sport, dport;
411 int encap_type;
412
413 spin_lock_bh(&x->lock);
414 sport = encap->encap_sport;
415 dport = encap->encap_dport;
416 encap_type = encap->encap_type;
417 spin_unlock_bh(&x->lock);
418
419 switch (encap_type) {
420 default:
421 case UDP_ENCAP_ESPINUDP:
422 case UDP_ENCAP_ESPINUDP_NON_IKE:
423 esph = esp6_output_udp_encap(skb, encap_type, esp, sport, dport);
424 break;
425 case TCP_ENCAP_ESPINTCP:
426 esph = esp6_output_tcp_encap(x, skb, esp);
427 break;
428 }
429
430 if (IS_ERR(esph))
431 return PTR_ERR(esph);
432
433 esp->esph = esph;
434
435 return 0;
436 }
437
esp6_output_head(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)438 int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
439 {
440 u8 *tail;
441 int nfrags;
442 int esph_offset;
443 struct page *page;
444 struct sk_buff *trailer;
445 int tailen = esp->tailen;
446
447 if (x->encap) {
448 int err = esp6_output_encap(x, skb, esp);
449
450 if (err < 0)
451 return err;
452 }
453
454 if (ALIGN(tailen, L1_CACHE_BYTES) > PAGE_SIZE ||
455 ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE)
456 goto cow;
457
458 if (!skb_cloned(skb)) {
459 if (tailen <= skb_tailroom(skb)) {
460 nfrags = 1;
461 trailer = skb;
462 tail = skb_tail_pointer(trailer);
463
464 goto skip_cow;
465 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
466 && !skb_has_frag_list(skb)) {
467 int allocsize;
468 struct sock *sk = skb->sk;
469 struct page_frag *pfrag = &x->xfrag;
470
471 esp->inplace = false;
472
473 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
474
475 spin_lock_bh(&x->lock);
476
477 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
478 spin_unlock_bh(&x->lock);
479 goto cow;
480 }
481
482 page = pfrag->page;
483 get_page(page);
484
485 tail = page_address(page) + pfrag->offset;
486
487 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
488
489 nfrags = skb_shinfo(skb)->nr_frags;
490
491 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
492 tailen);
493 skb_shinfo(skb)->nr_frags = ++nfrags;
494
495 pfrag->offset = pfrag->offset + allocsize;
496
497 spin_unlock_bh(&x->lock);
498
499 nfrags++;
500
501 skb->len += tailen;
502 skb->data_len += tailen;
503 skb->truesize += tailen;
504 if (sk && sk_fullsock(sk))
505 refcount_add(tailen, &sk->sk_wmem_alloc);
506
507 goto out;
508 }
509 }
510
511 cow:
512 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
513
514 nfrags = skb_cow_data(skb, tailen, &trailer);
515 if (nfrags < 0)
516 goto out;
517 tail = skb_tail_pointer(trailer);
518 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
519
520 skip_cow:
521 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
522 pskb_put(skb, trailer, tailen);
523
524 out:
525 return nfrags;
526 }
527 EXPORT_SYMBOL_GPL(esp6_output_head);
528
esp6_output_tail(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)529 int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
530 {
531 u8 *iv;
532 int alen;
533 void *tmp;
534 int ivlen;
535 int assoclen;
536 int extralen;
537 struct page *page;
538 struct ip_esp_hdr *esph;
539 struct aead_request *req;
540 struct crypto_aead *aead;
541 struct scatterlist *sg, *dsg;
542 struct esp_output_extra *extra;
543 int err = -ENOMEM;
544
545 assoclen = sizeof(struct ip_esp_hdr);
546 extralen = 0;
547
548 if (x->props.flags & XFRM_STATE_ESN) {
549 extralen += sizeof(*extra);
550 assoclen += sizeof(__be32);
551 }
552
553 aead = x->data;
554 alen = crypto_aead_authsize(aead);
555 ivlen = crypto_aead_ivsize(aead);
556
557 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
558 if (!tmp)
559 goto error;
560
561 extra = esp_tmp_extra(tmp);
562 iv = esp_tmp_iv(aead, tmp, extralen);
563 req = esp_tmp_req(aead, iv);
564 sg = esp_req_sg(aead, req);
565
566 if (esp->inplace)
567 dsg = sg;
568 else
569 dsg = &sg[esp->nfrags];
570
571 esph = esp_output_set_esn(skb, x, esp->esph, extra);
572 esp->esph = esph;
573
574 sg_init_table(sg, esp->nfrags);
575 err = skb_to_sgvec(skb, sg,
576 (unsigned char *)esph - skb->data,
577 assoclen + ivlen + esp->clen + alen);
578 if (unlikely(err < 0))
579 goto error_free;
580
581 if (!esp->inplace) {
582 int allocsize;
583 struct page_frag *pfrag = &x->xfrag;
584
585 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
586
587 spin_lock_bh(&x->lock);
588 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
589 spin_unlock_bh(&x->lock);
590 goto error_free;
591 }
592
593 skb_shinfo(skb)->nr_frags = 1;
594
595 page = pfrag->page;
596 get_page(page);
597 /* replace page frags in skb with new page */
598 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
599 pfrag->offset = pfrag->offset + allocsize;
600 spin_unlock_bh(&x->lock);
601
602 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
603 err = skb_to_sgvec(skb, dsg,
604 (unsigned char *)esph - skb->data,
605 assoclen + ivlen + esp->clen + alen);
606 if (unlikely(err < 0))
607 goto error_free;
608 }
609
610 if ((x->props.flags & XFRM_STATE_ESN))
611 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
612 else
613 aead_request_set_callback(req, 0, esp_output_done, skb);
614
615 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
616 aead_request_set_ad(req, assoclen);
617
618 memset(iv, 0, ivlen);
619 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
620 min(ivlen, 8));
621
622 ESP_SKB_CB(skb)->tmp = tmp;
623 err = crypto_aead_encrypt(req);
624
625 switch (err) {
626 case -EINPROGRESS:
627 goto error;
628
629 case -ENOSPC:
630 err = NET_XMIT_DROP;
631 break;
632
633 case 0:
634 if ((x->props.flags & XFRM_STATE_ESN))
635 esp_output_restore_header(skb);
636 esp_output_encap_csum(skb);
637 }
638
639 if (sg != dsg)
640 esp_ssg_unref(x, tmp, skb);
641
642 if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
643 err = esp_output_tail_tcp(x, skb);
644
645 error_free:
646 kfree(tmp);
647 error:
648 return err;
649 }
650 EXPORT_SYMBOL_GPL(esp6_output_tail);
651
esp6_output(struct xfrm_state * x,struct sk_buff * skb)652 static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
653 {
654 int alen;
655 int blksize;
656 struct ip_esp_hdr *esph;
657 struct crypto_aead *aead;
658 struct esp_info esp;
659
660 esp.inplace = true;
661
662 esp.proto = *skb_mac_header(skb);
663 *skb_mac_header(skb) = IPPROTO_ESP;
664
665 /* skb is pure payload to encrypt */
666
667 aead = x->data;
668 alen = crypto_aead_authsize(aead);
669
670 esp.tfclen = 0;
671 if (x->tfcpad) {
672 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
673 u32 padto;
674
675 padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached));
676 if (skb->len < padto)
677 esp.tfclen = padto - skb->len;
678 }
679 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
680 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
681 esp.plen = esp.clen - skb->len - esp.tfclen;
682 esp.tailen = esp.tfclen + esp.plen + alen;
683
684 esp.esph = ip_esp_hdr(skb);
685
686 esp.nfrags = esp6_output_head(x, skb, &esp);
687 if (esp.nfrags < 0)
688 return esp.nfrags;
689
690 esph = esp.esph;
691 esph->spi = x->id.spi;
692
693 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
694 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
695 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
696
697 skb_push(skb, -skb_network_offset(skb));
698
699 return esp6_output_tail(x, skb, &esp);
700 }
701
esp_remove_trailer(struct sk_buff * skb)702 static inline int esp_remove_trailer(struct sk_buff *skb)
703 {
704 struct xfrm_state *x = xfrm_input_state(skb);
705 struct crypto_aead *aead = x->data;
706 int alen, hlen, elen;
707 int padlen, trimlen;
708 __wsum csumdiff;
709 u8 nexthdr[2];
710 int ret;
711
712 alen = crypto_aead_authsize(aead);
713 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
714 elen = skb->len - hlen;
715
716 ret = skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2);
717 BUG_ON(ret);
718
719 ret = -EINVAL;
720 padlen = nexthdr[0];
721 if (padlen + 2 + alen >= elen) {
722 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
723 padlen + 2, elen - alen);
724 goto out;
725 }
726
727 trimlen = alen + padlen + 2;
728 if (skb->ip_summed == CHECKSUM_COMPLETE) {
729 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
730 skb->csum = csum_block_sub(skb->csum, csumdiff,
731 skb->len - trimlen);
732 }
733 ret = pskb_trim(skb, skb->len - trimlen);
734 if (unlikely(ret))
735 return ret;
736
737 ret = nexthdr[1];
738
739 out:
740 return ret;
741 }
742
esp6_input_done2(struct sk_buff * skb,int err)743 int esp6_input_done2(struct sk_buff *skb, int err)
744 {
745 struct xfrm_state *x = xfrm_input_state(skb);
746 struct xfrm_offload *xo = xfrm_offload(skb);
747 struct crypto_aead *aead = x->data;
748 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
749 int hdr_len = skb_network_header_len(skb);
750
751 if (!xo || !(xo->flags & CRYPTO_DONE))
752 kfree(ESP_SKB_CB(skb)->tmp);
753
754 if (unlikely(err))
755 goto out;
756
757 err = esp_remove_trailer(skb);
758 if (unlikely(err < 0))
759 goto out;
760
761 if (x->encap) {
762 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
763 int offset = skb_network_offset(skb) + sizeof(*ip6h);
764 struct xfrm_encap_tmpl *encap = x->encap;
765 u8 nexthdr = ip6h->nexthdr;
766 __be16 frag_off, source;
767 struct udphdr *uh;
768 struct tcphdr *th;
769
770 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
771 if (offset == -1) {
772 err = -EINVAL;
773 goto out;
774 }
775
776 uh = (void *)(skb->data + offset);
777 th = (void *)(skb->data + offset);
778 hdr_len += offset;
779
780 switch (x->encap->encap_type) {
781 case TCP_ENCAP_ESPINTCP:
782 source = th->source;
783 break;
784 case UDP_ENCAP_ESPINUDP:
785 case UDP_ENCAP_ESPINUDP_NON_IKE:
786 source = uh->source;
787 break;
788 default:
789 WARN_ON_ONCE(1);
790 err = -EINVAL;
791 goto out;
792 }
793
794 /*
795 * 1) if the NAT-T peer's IP or port changed then
796 * advertise the change to the keying daemon.
797 * This is an inbound SA, so just compare
798 * SRC ports.
799 */
800 if (!ipv6_addr_equal(&ip6h->saddr, &x->props.saddr.in6) ||
801 source != encap->encap_sport) {
802 xfrm_address_t ipaddr;
803
804 memcpy(&ipaddr.a6, &ip6h->saddr.s6_addr, sizeof(ipaddr.a6));
805 km_new_mapping(x, &ipaddr, source);
806
807 /* XXX: perhaps add an extra
808 * policy check here, to see
809 * if we should allow or
810 * reject a packet from a
811 * different source
812 * address/port.
813 */
814 }
815
816 /*
817 * 2) ignore UDP/TCP checksums in case
818 * of NAT-T in Transport Mode, or
819 * perform other post-processing fixes
820 * as per draft-ietf-ipsec-udp-encaps-06,
821 * section 3.1.2
822 */
823 if (x->props.mode == XFRM_MODE_TRANSPORT)
824 skb->ip_summed = CHECKSUM_UNNECESSARY;
825 }
826
827 skb_postpull_rcsum(skb, skb_network_header(skb),
828 skb_network_header_len(skb));
829 skb_pull_rcsum(skb, hlen);
830 if (x->props.mode == XFRM_MODE_TUNNEL)
831 skb_reset_transport_header(skb);
832 else
833 skb_set_transport_header(skb, -hdr_len);
834
835 /* RFC4303: Drop dummy packets without any error */
836 if (err == IPPROTO_NONE)
837 err = -EINVAL;
838
839 out:
840 return err;
841 }
842 EXPORT_SYMBOL_GPL(esp6_input_done2);
843
esp_input_done(void * data,int err)844 static void esp_input_done(void *data, int err)
845 {
846 struct sk_buff *skb = data;
847
848 xfrm_input_resume(skb, esp6_input_done2(skb, err));
849 }
850
esp_input_restore_header(struct sk_buff * skb)851 static void esp_input_restore_header(struct sk_buff *skb)
852 {
853 esp_restore_header(skb, 0);
854 __skb_pull(skb, 4);
855 }
856
esp_input_set_header(struct sk_buff * skb,__be32 * seqhi)857 static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
858 {
859 struct xfrm_state *x = xfrm_input_state(skb);
860
861 /* For ESN we move the header forward by 4 bytes to
862 * accommodate the high bits. We will move it back after
863 * decryption.
864 */
865 if ((x->props.flags & XFRM_STATE_ESN)) {
866 struct ip_esp_hdr *esph = skb_push(skb, 4);
867
868 *seqhi = esph->spi;
869 esph->spi = esph->seq_no;
870 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
871 }
872 }
873
esp_input_done_esn(void * data,int err)874 static void esp_input_done_esn(void *data, int err)
875 {
876 struct sk_buff *skb = data;
877
878 esp_input_restore_header(skb);
879 esp_input_done(data, err);
880 }
881
esp6_input(struct xfrm_state * x,struct sk_buff * skb)882 static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
883 {
884 struct crypto_aead *aead = x->data;
885 struct aead_request *req;
886 struct sk_buff *trailer;
887 int ivlen = crypto_aead_ivsize(aead);
888 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
889 int nfrags;
890 int assoclen;
891 int seqhilen;
892 int ret = 0;
893 void *tmp;
894 __be32 *seqhi;
895 u8 *iv;
896 struct scatterlist *sg;
897
898 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen)) {
899 ret = -EINVAL;
900 goto out;
901 }
902
903 if (elen <= 0) {
904 ret = -EINVAL;
905 goto out;
906 }
907
908 assoclen = sizeof(struct ip_esp_hdr);
909 seqhilen = 0;
910
911 if (x->props.flags & XFRM_STATE_ESN) {
912 seqhilen += sizeof(__be32);
913 assoclen += seqhilen;
914 }
915
916 if (!skb_cloned(skb)) {
917 if (!skb_is_nonlinear(skb)) {
918 nfrags = 1;
919
920 goto skip_cow;
921 } else if (!skb_has_frag_list(skb)) {
922 nfrags = skb_shinfo(skb)->nr_frags;
923 nfrags++;
924
925 goto skip_cow;
926 }
927 }
928
929 nfrags = skb_cow_data(skb, 0, &trailer);
930 if (nfrags < 0) {
931 ret = -EINVAL;
932 goto out;
933 }
934
935 skip_cow:
936 ret = -ENOMEM;
937 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
938 if (!tmp)
939 goto out;
940
941 ESP_SKB_CB(skb)->tmp = tmp;
942 seqhi = esp_tmp_extra(tmp);
943 iv = esp_tmp_iv(aead, tmp, seqhilen);
944 req = esp_tmp_req(aead, iv);
945 sg = esp_req_sg(aead, req);
946
947 esp_input_set_header(skb, seqhi);
948
949 sg_init_table(sg, nfrags);
950 ret = skb_to_sgvec(skb, sg, 0, skb->len);
951 if (unlikely(ret < 0)) {
952 kfree(tmp);
953 goto out;
954 }
955
956 skb->ip_summed = CHECKSUM_NONE;
957
958 if ((x->props.flags & XFRM_STATE_ESN))
959 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
960 else
961 aead_request_set_callback(req, 0, esp_input_done, skb);
962
963 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
964 aead_request_set_ad(req, assoclen);
965
966 ret = crypto_aead_decrypt(req);
967 if (ret == -EINPROGRESS)
968 goto out;
969
970 if ((x->props.flags & XFRM_STATE_ESN))
971 esp_input_restore_header(skb);
972
973 ret = esp6_input_done2(skb, ret);
974
975 out:
976 return ret;
977 }
978
esp6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)979 static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
980 u8 type, u8 code, int offset, __be32 info)
981 {
982 struct net *net = dev_net(skb->dev);
983 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
984 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
985 struct xfrm_state *x;
986
987 if (type != ICMPV6_PKT_TOOBIG &&
988 type != NDISC_REDIRECT)
989 return 0;
990
991 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
992 esph->spi, IPPROTO_ESP, AF_INET6);
993 if (!x)
994 return 0;
995
996 if (type == NDISC_REDIRECT)
997 ip6_redirect(skb, net, skb->dev->ifindex, 0,
998 sock_net_uid(net, NULL));
999 else
1000 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
1001 xfrm_state_put(x);
1002
1003 return 0;
1004 }
1005
esp6_destroy(struct xfrm_state * x)1006 static void esp6_destroy(struct xfrm_state *x)
1007 {
1008 struct crypto_aead *aead = x->data;
1009
1010 if (!aead)
1011 return;
1012
1013 crypto_free_aead(aead);
1014 }
1015
esp_init_aead(struct xfrm_state * x,struct netlink_ext_ack * extack)1016 static int esp_init_aead(struct xfrm_state *x, struct netlink_ext_ack *extack)
1017 {
1018 char aead_name[CRYPTO_MAX_ALG_NAME];
1019 struct crypto_aead *aead;
1020 int err;
1021
1022 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
1023 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) {
1024 NL_SET_ERR_MSG(extack, "Algorithm name is too long");
1025 return -ENAMETOOLONG;
1026 }
1027
1028 aead = crypto_alloc_aead(aead_name, 0, 0);
1029 err = PTR_ERR(aead);
1030 if (IS_ERR(aead))
1031 goto error;
1032
1033 x->data = aead;
1034
1035 err = crypto_aead_setkey(aead, x->aead->alg_key,
1036 (x->aead->alg_key_len + 7) / 8);
1037 if (err)
1038 goto error;
1039
1040 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
1041 if (err)
1042 goto error;
1043
1044 return 0;
1045
1046 error:
1047 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1048 return err;
1049 }
1050
esp_init_authenc(struct xfrm_state * x,struct netlink_ext_ack * extack)1051 static int esp_init_authenc(struct xfrm_state *x,
1052 struct netlink_ext_ack *extack)
1053 {
1054 struct crypto_aead *aead;
1055 struct crypto_authenc_key_param *param;
1056 struct rtattr *rta;
1057 char *key;
1058 char *p;
1059 char authenc_name[CRYPTO_MAX_ALG_NAME];
1060 unsigned int keylen;
1061 int err;
1062
1063 err = -ENAMETOOLONG;
1064
1065 if ((x->props.flags & XFRM_STATE_ESN)) {
1066 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
1067 "%s%sauthencesn(%s,%s)%s",
1068 x->geniv ?: "", x->geniv ? "(" : "",
1069 x->aalg ? x->aalg->alg_name : "digest_null",
1070 x->ealg->alg_name,
1071 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
1072 NL_SET_ERR_MSG(extack, "Algorithm name is too long");
1073 goto error;
1074 }
1075 } else {
1076 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
1077 "%s%sauthenc(%s,%s)%s",
1078 x->geniv ?: "", x->geniv ? "(" : "",
1079 x->aalg ? x->aalg->alg_name : "digest_null",
1080 x->ealg->alg_name,
1081 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
1082 NL_SET_ERR_MSG(extack, "Algorithm name is too long");
1083 goto error;
1084 }
1085 }
1086
1087 aead = crypto_alloc_aead(authenc_name, 0, 0);
1088 err = PTR_ERR(aead);
1089 if (IS_ERR(aead)) {
1090 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1091 goto error;
1092 }
1093
1094 x->data = aead;
1095
1096 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
1097 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
1098 err = -ENOMEM;
1099 key = kmalloc(keylen, GFP_KERNEL);
1100 if (!key)
1101 goto error;
1102
1103 p = key;
1104 rta = (void *)p;
1105 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1106 rta->rta_len = RTA_LENGTH(sizeof(*param));
1107 param = RTA_DATA(rta);
1108 p += RTA_SPACE(sizeof(*param));
1109
1110 if (x->aalg) {
1111 struct xfrm_algo_desc *aalg_desc;
1112
1113 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
1114 p += (x->aalg->alg_key_len + 7) / 8;
1115
1116 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
1117 BUG_ON(!aalg_desc);
1118
1119 err = -EINVAL;
1120 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
1121 crypto_aead_authsize(aead)) {
1122 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1123 goto free_key;
1124 }
1125
1126 err = crypto_aead_setauthsize(
1127 aead, x->aalg->alg_trunc_len / 8);
1128 if (err) {
1129 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1130 goto free_key;
1131 }
1132 }
1133
1134 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
1135 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
1136
1137 err = crypto_aead_setkey(aead, key, keylen);
1138
1139 free_key:
1140 kfree(key);
1141
1142 error:
1143 return err;
1144 }
1145
esp6_init_state(struct xfrm_state * x,struct netlink_ext_ack * extack)1146 static int esp6_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
1147 {
1148 struct crypto_aead *aead;
1149 u32 align;
1150 int err;
1151
1152 x->data = NULL;
1153
1154 if (x->aead) {
1155 err = esp_init_aead(x, extack);
1156 } else if (x->ealg) {
1157 err = esp_init_authenc(x, extack);
1158 } else {
1159 NL_SET_ERR_MSG(extack, "ESP: AEAD or CRYPT must be provided");
1160 err = -EINVAL;
1161 }
1162
1163 if (err)
1164 goto error;
1165
1166 aead = x->data;
1167
1168 x->props.header_len = sizeof(struct ip_esp_hdr) +
1169 crypto_aead_ivsize(aead);
1170 switch (x->props.mode) {
1171 case XFRM_MODE_BEET:
1172 if (x->sel.family != AF_INET6)
1173 x->props.header_len += IPV4_BEET_PHMAXLEN +
1174 (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
1175 break;
1176 default:
1177 case XFRM_MODE_TRANSPORT:
1178 break;
1179 case XFRM_MODE_TUNNEL:
1180 x->props.header_len += sizeof(struct ipv6hdr);
1181 break;
1182 }
1183
1184 if (x->encap) {
1185 struct xfrm_encap_tmpl *encap = x->encap;
1186
1187 switch (encap->encap_type) {
1188 default:
1189 NL_SET_ERR_MSG(extack, "Unsupported encapsulation type for ESP");
1190 err = -EINVAL;
1191 goto error;
1192 case UDP_ENCAP_ESPINUDP:
1193 x->props.header_len += sizeof(struct udphdr);
1194 break;
1195 case UDP_ENCAP_ESPINUDP_NON_IKE:
1196 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1197 break;
1198 #ifdef CONFIG_INET6_ESPINTCP
1199 case TCP_ENCAP_ESPINTCP:
1200 /* only the length field, TCP encap is done by
1201 * the socket
1202 */
1203 x->props.header_len += 2;
1204 break;
1205 #endif
1206 }
1207 }
1208
1209 align = ALIGN(crypto_aead_blocksize(aead), 4);
1210 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1211
1212 error:
1213 return err;
1214 }
1215
esp6_rcv_cb(struct sk_buff * skb,int err)1216 static int esp6_rcv_cb(struct sk_buff *skb, int err)
1217 {
1218 return 0;
1219 }
1220
1221 static const struct xfrm_type esp6_type = {
1222 .owner = THIS_MODULE,
1223 .proto = IPPROTO_ESP,
1224 .flags = XFRM_TYPE_REPLAY_PROT,
1225 .init_state = esp6_init_state,
1226 .destructor = esp6_destroy,
1227 .input = esp6_input,
1228 .output = esp6_output,
1229 };
1230
1231 static struct xfrm6_protocol esp6_protocol = {
1232 .handler = xfrm6_rcv,
1233 .input_handler = xfrm_input,
1234 .cb_handler = esp6_rcv_cb,
1235 .err_handler = esp6_err,
1236 .priority = 0,
1237 };
1238
esp6_init(void)1239 static int __init esp6_init(void)
1240 {
1241 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
1242 pr_info("%s: can't add xfrm type\n", __func__);
1243 return -EAGAIN;
1244 }
1245 if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
1246 pr_info("%s: can't add protocol\n", __func__);
1247 xfrm_unregister_type(&esp6_type, AF_INET6);
1248 return -EAGAIN;
1249 }
1250
1251 return 0;
1252 }
1253
esp6_fini(void)1254 static void __exit esp6_fini(void)
1255 {
1256 if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
1257 pr_info("%s: can't remove protocol\n", __func__);
1258 xfrm_unregister_type(&esp6_type, AF_INET6);
1259 }
1260
1261 module_init(esp6_init);
1262 module_exit(esp6_fini);
1263
1264 MODULE_LICENSE("GPL");
1265 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);
1266