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