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