esp4.c 21.7 KB
Newer Older
1 2
#define pr_fmt(fmt) "IPsec: " fmt

3 4
#include <crypto/aead.h>
#include <crypto/authenc.h>
5
#include <linux/err.h>
L
Linus Torvalds 已提交
6 7 8 9
#include <linux/module.h>
#include <net/ip.h>
#include <net/xfrm.h>
#include <net/esp.h>
10
#include <linux/scatterlist.h>
H
Herbert Xu 已提交
11
#include <linux/kernel.h>
L
Linus Torvalds 已提交
12
#include <linux/pfkeyv2.h>
13 14
#include <linux/rtnetlink.h>
#include <linux/slab.h>
15
#include <linux/spinlock.h>
16
#include <linux/in6.h>
L
Linus Torvalds 已提交
17
#include <net/icmp.h>
18
#include <net/protocol.h>
L
Linus Torvalds 已提交
19 20
#include <net/udp.h>

21 22
#include <linux/highmem.h>

23 24 25 26 27
struct esp_skb_cb {
	struct xfrm_skb_cb xfrm;
	void *tmp;
};

28 29 30 31 32
struct esp_output_extra {
	__be32 seqhi;
	u32 esphoff;
};

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 40 41 42 43 44
/*
 * Allocate an AEAD request structure with extra space for SG and IV.
 *
 * For alignment considerations the IV is placed at the front, followed
 * by the request and finally the SG list.
 *
 * TODO: Use spare space in skb for this where possible.
 */
45
static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
46 47 48
{
	unsigned int len;

49
	len = extralen;
50 51 52

	len += crypto_aead_ivsize(aead);

53 54 55 56 57 58
	if (len) {
		len += crypto_aead_alignmask(aead) &
		       ~(crypto_tfm_ctx_alignment() - 1);
		len = ALIGN(len, crypto_tfm_ctx_alignment());
	}

H
Herbert Xu 已提交
59
	len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
60 61 62 63 64 65 66
	len = ALIGN(len, __alignof__(struct scatterlist));

	len += sizeof(struct scatterlist) * nfrags;

	return kmalloc(len, GFP_ATOMIC);
}

67
static inline void *esp_tmp_extra(void *tmp)
68
{
69
	return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
70
}
71 72

static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
73 74
{
	return crypto_aead_ivsize(aead) ?
75 76
	       PTR_ALIGN((u8 *)tmp + extralen,
			 crypto_aead_alignmask(aead) + 1) : tmp + extralen;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
{
	struct aead_request *req;

	req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
				crypto_tfm_ctx_alignment());
	aead_request_set_tfm(req, aead);
	return req;
}

static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
					     struct aead_request *req)
{
	return (void *)ALIGN((unsigned long)(req + 1) +
			     crypto_aead_reqsize(aead),
			     __alignof__(struct scatterlist));
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
{
	struct esp_output_extra *extra = esp_tmp_extra(tmp);
	struct crypto_aead *aead = x->data;
	int extralen = 0;
	u8 *iv;
	struct aead_request *req;
	struct scatterlist *sg;

	if (x->props.flags & XFRM_STATE_ESN)
		extralen += sizeof(*extra);

	extra = esp_tmp_extra(tmp);
	iv = esp_tmp_iv(aead, tmp, extralen);
	req = esp_tmp_req(aead, iv);

	/* Unref skb_frag_pages in the src scatterlist if necessary.
	 * Skip the first sg which comes from skb->data.
	 */
	if (req->src != req->dst)
		for (sg = sg_next(req->src); sg; sg = sg_next(sg))
			put_page(sg_page(sg));
}

121 122 123
static void esp_output_done(struct crypto_async_request *base, int err)
{
	struct sk_buff *skb = base->data;
124 125 126
	void *tmp;
	struct dst_entry *dst = skb_dst(skb);
	struct xfrm_state *x = dst->xfrm;
127

128 129 130
	tmp = ESP_SKB_CB(skb)->tmp;
	esp_ssg_unref(x, tmp);
	kfree(tmp);
131 132 133
	xfrm_output_resume(skb, err);
}

H
Herbert Xu 已提交
134 135 136 137 138
/* Move ESP header back into place. */
static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
{
	struct ip_esp_hdr *esph = (void *)(skb->data + offset);
	void *tmp = ESP_SKB_CB(skb)->tmp;
139
	__be32 *seqhi = esp_tmp_extra(tmp);
H
Herbert Xu 已提交
140 141 142 143 144 145 146

	esph->seq_no = esph->spi;
	esph->spi = *seqhi;
}

static void esp_output_restore_header(struct sk_buff *skb)
{
147 148 149 150 151
	void *tmp = ESP_SKB_CB(skb)->tmp;
	struct esp_output_extra *extra = esp_tmp_extra(tmp);

	esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
				sizeof(__be32));
H
Herbert Xu 已提交
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
					       struct ip_esp_hdr *esph,
					       struct esp_output_extra *extra)
{
	struct xfrm_state *x = skb_dst(skb)->xfrm;

	/* For ESN we move the header forward by 4 bytes to
	 * accomodate the high bits.  We will move it back after
	 * encryption.
	 */
	if ((x->props.flags & XFRM_STATE_ESN)) {
		extra->esphoff = (unsigned char *)esph -
				 skb_transport_header(skb);
		esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
		extra->seqhi = esph->spi;
		esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
	}

	esph->spi = x->id.spi;

	return esph;
}

H
Herbert Xu 已提交
177 178 179 180 181 182 183 184
static void esp_output_done_esn(struct crypto_async_request *base, int err)
{
	struct sk_buff *skb = base->data;

	esp_output_restore_header(skb);
	esp_output_done(base, err);
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
{
	/* Fill padding... */
	if (tfclen) {
		memset(tail, 0, tfclen);
		tail += tfclen;
	}
	do {
		int i;
		for (i = 0; i < plen - 2; i++)
			tail[i] = i + 1;
	} while (0);
	tail[plen - 2] = plen - 2;
	tail[plen - 1] = proto;
}

L
Linus Torvalds 已提交
201 202
static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
{
203
	struct esp_output_extra *extra;
204
	int err = -ENOMEM;
L
Linus Torvalds 已提交
205
	struct ip_esp_hdr *esph;
206
	struct crypto_aead *aead;
H
Herbert Xu 已提交
207
	struct aead_request *req;
208
	struct scatterlist *sg, *dsg;
L
Linus Torvalds 已提交
209
	struct sk_buff *trailer;
210
	struct page *page;
211 212
	void *tmp;
	u8 *iv;
213
	u8 *tail;
214
	u8 *vaddr;
L
Linus Torvalds 已提交
215 216 217
	int blksize;
	int clen;
	int alen;
218
	int plen;
H
Herbert Xu 已提交
219
	int ivlen;
220
	int tfclen;
L
Linus Torvalds 已提交
221
	int nfrags;
222
	int assoclen;
223
	int extralen;
224
	int tailen;
H
Herbert Xu 已提交
225
	__be64 seqno;
226
	__u8 proto = *skb_mac_header(skb);
L
Linus Torvalds 已提交
227

228
	/* skb is pure payload to encrypt */
L
Linus Torvalds 已提交
229

230
	aead = x->data;
231
	alen = crypto_aead_authsize(aead);
H
Herbert Xu 已提交
232
	ivlen = crypto_aead_ivsize(aead);
233

234 235 236 237 238 239 240 241 242
	tfclen = 0;
	if (x->tfcpad) {
		struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
		u32 padto;

		padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
		if (skb->len < padto)
			tfclen = padto - skb->len;
	}
243
	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
244 245
	clen = ALIGN(skb->len + 2 + tfclen, blksize);
	plen = clen - skb->len - tfclen;
246
	tailen = tfclen + plen + alen;
247
	assoclen = sizeof(*esph);
248
	extralen = 0;
249 250

	if (x->props.flags & XFRM_STATE_ESN) {
251 252
		extralen += sizeof(*extra);
		assoclen += sizeof(__be32);
253 254
	}

255
	*skb_mac_header(skb) = IPPROTO_ESP;
256
	esph = ip_esp_hdr(skb);
L
Linus Torvalds 已提交
257 258 259 260 261

	/* this is non-NULL only with UDP Encapsulation */
	if (x->encap) {
		struct xfrm_encap_tmpl *encap = x->encap;
		struct udphdr *uh;
A
Al Viro 已提交
262
		__be32 *udpdata32;
A
Al Viro 已提交
263
		__be16 sport, dport;
264 265 266 267 268 269 270
		int encap_type;

		spin_lock_bh(&x->lock);
		sport = encap->encap_sport;
		dport = encap->encap_dport;
		encap_type = encap->encap_type;
		spin_unlock_bh(&x->lock);
L
Linus Torvalds 已提交
271 272

		uh = (struct udphdr *)esph;
273 274
		uh->source = sport;
		uh->dest = dport;
275 276
		uh->len = htons(skb->len + tailen
				- skb_transport_offset(skb));
L
Linus Torvalds 已提交
277 278
		uh->check = 0;

279
		switch (encap_type) {
L
Linus Torvalds 已提交
280 281 282 283 284
		default:
		case UDP_ENCAP_ESPINUDP:
			esph = (struct ip_esp_hdr *)(uh + 1);
			break;
		case UDP_ENCAP_ESPINUDP_NON_IKE:
A
Al Viro 已提交
285
			udpdata32 = (__be32 *)(uh + 1);
L
Linus Torvalds 已提交
286 287 288 289 290
			udpdata32[0] = udpdata32[1] = 0;
			esph = (struct ip_esp_hdr *)(udpdata32 + 2);
			break;
		}

291 292
		*skb_mac_header(skb) = IPPROTO_UDP;
	}
L
Linus Torvalds 已提交
293

294 295 296 297 298
	if (!skb_cloned(skb)) {
		if (tailen <= skb_availroom(skb)) {
			nfrags = 1;
			trailer = skb;
			tail = skb_tail_pointer(trailer);
L
Linus Torvalds 已提交
299

300 301 302 303 304 305
			goto skip_cow;
		} else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
			   && !skb_has_frag_list(skb)) {
			int allocsize;
			struct sock *sk = skb->sk;
			struct page_frag *pfrag = &x->xfrag;
H
Herbert Xu 已提交
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
			allocsize = ALIGN(tailen, L1_CACHE_BYTES);

			spin_lock_bh(&x->lock);

			if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
				spin_unlock_bh(&x->lock);
				goto cow;
			}

			page = pfrag->page;
			get_page(page);

			vaddr = kmap_atomic(page);

			tail = vaddr + pfrag->offset;

323
			esp_output_fill_trailer(tail, tfclen, plen, proto);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

			kunmap_atomic(vaddr);

			nfrags = skb_shinfo(skb)->nr_frags;

			__skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
					     tailen);
			skb_shinfo(skb)->nr_frags = ++nfrags;

			pfrag->offset = pfrag->offset + allocsize;
			nfrags++;

			skb->len += tailen;
			skb->data_len += tailen;
			skb->truesize += tailen;
			if (sk)
				atomic_add(tailen, &sk->sk_wmem_alloc);

			skb_push(skb, -skb_network_offset(skb));

			esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
			esph->spi = x->id.spi;

			tmp = esp_alloc_tmp(aead, nfrags + 2, extralen);
			if (!tmp) {
				spin_unlock_bh(&x->lock);
				err = -ENOMEM;
				goto error;
			}

			extra = esp_tmp_extra(tmp);
			iv = esp_tmp_iv(aead, tmp, extralen);
			req = esp_tmp_req(aead, iv);
			sg = esp_req_sg(aead, req);
			dsg = &sg[nfrags];

			esph = esp_output_set_extra(skb, esph, extra);

			sg_init_table(sg, nfrags);
			skb_to_sgvec(skb, sg,
				     (unsigned char *)esph - skb->data,
				     assoclen + ivlen + clen + alen);

			allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);

			if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
				spin_unlock_bh(&x->lock);
				err = -ENOMEM;
				goto error;
			}

			skb_shinfo(skb)->nr_frags = 1;

			page = pfrag->page;
			get_page(page);
			/* replace page frags in skb with new page */
			__skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
			pfrag->offset = pfrag->offset + allocsize;

			sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
			skb_to_sgvec(skb, dsg,
				     (unsigned char *)esph - skb->data,
				     assoclen + ivlen + clen + alen);

			spin_unlock_bh(&x->lock);

			goto skip_cow2;
		}
H
Herbert Xu 已提交
392 393
	}

394 395 396 397 398 399 400 401 402
cow:
	err = skb_cow_data(skb, tailen, &trailer);
	if (err < 0)
		goto error;
	nfrags = err;
	tail = skb_tail_pointer(trailer);
	esph = ip_esp_hdr(skb);

skip_cow:
403
	esp_output_fill_trailer(tail, tfclen, plen, proto);
404

405
	pskb_put(skb, trailer, clen - skb->len + alen);
406 407
	skb_push(skb, -skb_network_offset(skb));
	esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
H
Herbert Xu 已提交
408 409
	esph->spi = x->id.spi;

410 411 412 413 414 415 416 417 418 419 420 421 422 423
	tmp = esp_alloc_tmp(aead, nfrags, extralen);
	if (!tmp) {
		err = -ENOMEM;
		goto error;
	}

	extra = esp_tmp_extra(tmp);
	iv = esp_tmp_iv(aead, tmp, extralen);
	req = esp_tmp_req(aead, iv);
	sg = esp_req_sg(aead, req);
	dsg = sg;

	esph = esp_output_set_extra(skb, esph, extra);

424 425
	sg_init_table(sg, nfrags);
	skb_to_sgvec(skb, sg,
H
Herbert Xu 已提交
426 427
		     (unsigned char *)esph - skb->data,
		     assoclen + ivlen + clen + alen);
428

429 430 431 432 433 434 435
skip_cow2:
	if ((x->props.flags & XFRM_STATE_ESN))
		aead_request_set_callback(req, 0, esp_output_done_esn, skb);
	else
		aead_request_set_callback(req, 0, esp_output_done, skb);

	aead_request_set_crypt(req, sg, dsg, ivlen + clen, iv);
H
Herbert Xu 已提交
436 437 438 439 440 441 442 443
	aead_request_set_ad(req, assoclen);

	seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
			    ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));

	memset(iv, 0, ivlen);
	memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&seqno + 8 - min(ivlen, 8),
	       min(ivlen, 8));
444 445

	ESP_SKB_CB(skb)->tmp = tmp;
H
Herbert Xu 已提交
446 447 448 449
	err = crypto_aead_encrypt(req);

	switch (err) {
	case -EINPROGRESS:
450
		goto error;
L
Linus Torvalds 已提交
451

H
Herbert Xu 已提交
452
	case -EBUSY:
453
		err = NET_XMIT_DROP;
H
Herbert Xu 已提交
454 455 456 457 458 459
		break;

	case 0:
		if ((x->props.flags & XFRM_STATE_ESN))
			esp_output_restore_header(skb);
	}
L
Linus Torvalds 已提交
460

461 462
	if (sg != dsg)
		esp_ssg_unref(x, tmp);
463
	kfree(tmp);
464

L
Linus Torvalds 已提交
465 466 467 468
error:
	return err;
}

469
static int esp_input_done2(struct sk_buff *skb, int err)
L
Linus Torvalds 已提交
470
{
471
	const struct iphdr *iph;
472
	struct xfrm_state *x = xfrm_input_state(skb);
473
	struct crypto_aead *aead = x->data;
474 475 476
	int alen = crypto_aead_authsize(aead);
	int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
	int elen = skb->len - hlen;
477
	int ihl;
478 479
	u8 nexthdr[2];
	int padlen;
L
Linus Torvalds 已提交
480

481
	kfree(ESP_SKB_CB(skb)->tmp);
482

483
	if (unlikely(err))
484
		goto out;
L
Linus Torvalds 已提交
485

486 487
	if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
		BUG();
L
Linus Torvalds 已提交
488

489
	err = -EINVAL;
490
	padlen = nexthdr[0];
491
	if (padlen + 2 + alen >= elen)
492
		goto out;
L
Linus Torvalds 已提交
493

494
	/* ... check padding bits here. Silly. :-) */
L
Linus Torvalds 已提交
495

496
	iph = ip_hdr(skb);
497 498
	ihl = iph->ihl * 4;

499 500
	if (x->encap) {
		struct xfrm_encap_tmpl *encap = x->encap;
501
		struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
502 503 504 505 506 507 508 509 510 511 512 513 514

		/*
		 * 1) if the NAT-T peer's IP or port changed then
		 *    advertize the change to the keying daemon.
		 *    This is an inbound SA, so just compare
		 *    SRC ports.
		 */
		if (iph->saddr != x->props.saddr.a4 ||
		    uh->source != encap->encap_sport) {
			xfrm_address_t ipaddr;

			ipaddr.a4 = iph->saddr;
			km_new_mapping(x, &ipaddr, uh->source);
515

516 517 518 519 520 521 522
			/* XXX: perhaps add an extra
			 * policy check here, to see
			 * if we should allow or
			 * reject a packet from a
			 * different source
			 * address/port.
			 */
L
Linus Torvalds 已提交
523
		}
524

525 526 527 528 529 530 531
		/*
		 * 2) ignore UDP/TCP checksums in case
		 *    of NAT-T in Transport Mode, or
		 *    perform other post-processing fixes
		 *    as per draft-ietf-ipsec-udp-encaps-06,
		 *    section 3.1.2
		 */
532
		if (x->props.mode == XFRM_MODE_TRANSPORT)
533
			skb->ip_summed = CHECKSUM_UNNECESSARY;
L
Linus Torvalds 已提交
534 535
	}

536
	pskb_trim(skb, skb->len - alen - padlen - 2);
537
	__skb_pull(skb, hlen);
538 539 540 541
	if (x->props.mode == XFRM_MODE_TUNNEL)
		skb_reset_transport_header(skb);
	else
		skb_set_transport_header(skb, -ihl);
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	err = nexthdr[1];

	/* RFC4303: Drop dummy packets without any error */
	if (err == IPPROTO_NONE)
		err = -EINVAL;

out:
	return err;
}

static void esp_input_done(struct crypto_async_request *base, int err)
{
	struct sk_buff *skb = base->data;

	xfrm_input_resume(skb, esp_input_done2(skb, err));
}

H
Herbert Xu 已提交
560 561 562 563 564 565
static void esp_input_restore_header(struct sk_buff *skb)
{
	esp_restore_header(skb, 0);
	__skb_pull(skb, 4);
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
{
	struct xfrm_state *x = xfrm_input_state(skb);
	struct ip_esp_hdr *esph = (struct ip_esp_hdr *)skb->data;

	/* For ESN we move the header forward by 4 bytes to
	 * accomodate the high bits.  We will move it back after
	 * decryption.
	 */
	if ((x->props.flags & XFRM_STATE_ESN)) {
		esph = (void *)skb_push(skb, 4);
		*seqhi = esph->spi;
		esph->spi = esph->seq_no;
		esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
	}
}

H
Herbert Xu 已提交
583 584 585 586 587 588 589 590
static void esp_input_done_esn(struct crypto_async_request *base, int err)
{
	struct sk_buff *skb = base->data;

	esp_input_restore_header(skb);
	esp_input_done(base, err);
}

591 592 593 594 595 596 597 598
/*
 * Note: detecting truncated vs. non-truncated authentication data is very
 * expensive, so we only support truncated data, which is the recommended
 * and common case.
 */
static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
{
	struct ip_esp_hdr *esph;
599
	struct crypto_aead *aead = x->data;
600 601
	struct aead_request *req;
	struct sk_buff *trailer;
H
Herbert Xu 已提交
602 603
	int ivlen = crypto_aead_ivsize(aead);
	int elen = skb->len - sizeof(*esph) - ivlen;
604
	int nfrags;
605 606 607
	int assoclen;
	int seqhilen;
	__be32 *seqhi;
608 609 610 611 612
	void *tmp;
	u8 *iv;
	struct scatterlist *sg;
	int err = -EINVAL;

H
Herbert Xu 已提交
613
	if (!pskb_may_pull(skb, sizeof(*esph) + ivlen))
614 615 616 617 618
		goto out;

	if (elen <= 0)
		goto out;

619 620 621 622 623 624 625 626
	assoclen = sizeof(*esph);
	seqhilen = 0;

	if (x->props.flags & XFRM_STATE_ESN) {
		seqhilen += sizeof(__be32);
		assoclen += seqhilen;
	}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	if (!skb_cloned(skb)) {
		if (!skb_is_nonlinear(skb)) {
			nfrags = 1;

			goto skip_cow;
		} else if (!skb_has_frag_list(skb)) {
			nfrags = skb_shinfo(skb)->nr_frags;
			nfrags++;

			goto skip_cow;
		}
	}

	err = skb_cow_data(skb, 0, &trailer);
	if (err < 0)
		goto out;

	nfrags = err;

skip_cow:
647
	err = -ENOMEM;
H
Herbert Xu 已提交
648
	tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
649 650 651 652
	if (!tmp)
		goto out;

	ESP_SKB_CB(skb)->tmp = tmp;
653
	seqhi = esp_tmp_extra(tmp);
654
	iv = esp_tmp_iv(aead, tmp, seqhilen);
655
	req = esp_tmp_req(aead, iv);
H
Herbert Xu 已提交
656
	sg = esp_req_sg(aead, req);
657

658
	esp_input_set_header(skb, seqhi);
659

660 661
	sg_init_table(sg, nfrags);
	skb_to_sgvec(skb, sg, 0, skb->len);
662

663
	skb->ip_summed = CHECKSUM_NONE;
664

665
	if ((x->props.flags & XFRM_STATE_ESN))
H
Herbert Xu 已提交
666
		aead_request_set_callback(req, 0, esp_input_done_esn, skb);
667 668
	else
		aead_request_set_callback(req, 0, esp_input_done, skb);
H
Herbert Xu 已提交
669 670 671

	aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
	aead_request_set_ad(req, assoclen);
672 673 674 675 676

	err = crypto_aead_decrypt(req);
	if (err == -EINPROGRESS)
		goto out;

H
Herbert Xu 已提交
677 678 679
	if ((x->props.flags & XFRM_STATE_ESN))
		esp_input_restore_header(skb);

680
	err = esp_input_done2(skb, err);
L
Linus Torvalds 已提交
681 682

out:
683
	return err;
L
Linus Torvalds 已提交
684 685
}

686
static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
L
Linus Torvalds 已提交
687
{
688 689
	struct crypto_aead *aead = x->data;
	u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
690
	unsigned int net_adj;
D
Diego Beltrami 已提交
691 692 693 694

	switch (x->props.mode) {
	case XFRM_MODE_TRANSPORT:
	case XFRM_MODE_BEET:
695
		net_adj = sizeof(struct iphdr);
D
Diego Beltrami 已提交
696
		break;
697 698 699 700 701
	case XFRM_MODE_TUNNEL:
		net_adj = 0;
		break;
	default:
		BUG();
L
Linus Torvalds 已提交
702
	}
D
Diego Beltrami 已提交
703

704
	return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
705
		 net_adj) & ~(blksize - 1)) + net_adj - 2;
L
Linus Torvalds 已提交
706 707
}

708
static int esp4_err(struct sk_buff *skb, u32 info)
L
Linus Torvalds 已提交
709
{
A
Alexey Dobriyan 已提交
710
	struct net *net = dev_net(skb->dev);
711
	const struct iphdr *iph = (const struct iphdr *)skb->data;
712
	struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
L
Linus Torvalds 已提交
713 714
	struct xfrm_state *x;

715 716 717
	switch (icmp_hdr(skb)->type) {
	case ICMP_DEST_UNREACH:
		if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
718
			return 0;
719 720 721
	case ICMP_REDIRECT:
		break;
	default:
722
		return 0;
723
	}
L
Linus Torvalds 已提交
724

725 726
	x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
			      esph->spi, IPPROTO_ESP, AF_INET);
L
Linus Torvalds 已提交
727
	if (!x)
728
		return 0;
729

730
	if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
731
		ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ESP, 0);
732
	else
733
		ipv4_redirect(skb, net, 0, 0, IPPROTO_ESP, 0);
L
Linus Torvalds 已提交
734
	xfrm_state_put(x);
735 736

	return 0;
L
Linus Torvalds 已提交
737 738 739 740
}

static void esp_destroy(struct xfrm_state *x)
{
741
	struct crypto_aead *aead = x->data;
L
Linus Torvalds 已提交
742

743
	if (!aead)
L
Linus Torvalds 已提交
744 745
		return;

746
	crypto_free_aead(aead);
L
Linus Torvalds 已提交
747 748
}

749
static int esp_init_aead(struct xfrm_state *x)
L
Linus Torvalds 已提交
750
{
H
Herbert Xu 已提交
751
	char aead_name[CRYPTO_MAX_ALG_NAME];
752 753 754
	struct crypto_aead *aead;
	int err;

H
Herbert Xu 已提交
755 756 757 758 759 760
	err = -ENAMETOOLONG;
	if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
		     x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
		goto error;

	aead = crypto_alloc_aead(aead_name, 0, 0);
761 762 763 764
	err = PTR_ERR(aead);
	if (IS_ERR(aead))
		goto error;

765
	x->data = aead;
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

	err = crypto_aead_setkey(aead, x->aead->alg_key,
				 (x->aead->alg_key_len + 7) / 8);
	if (err)
		goto error;

	err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
	if (err)
		goto error;

error:
	return err;
}

static int esp_init_authenc(struct xfrm_state *x)
{
782 783 784 785 786 787 788 789
	struct crypto_aead *aead;
	struct crypto_authenc_key_param *param;
	struct rtattr *rta;
	char *key;
	char *p;
	char authenc_name[CRYPTO_MAX_ALG_NAME];
	unsigned int keylen;
	int err;
L
Linus Torvalds 已提交
790

791
	err = -EINVAL;
792
	if (!x->ealg)
793
		goto error;
794

795
	err = -ENAMETOOLONG;
796 797 798

	if ((x->props.flags & XFRM_STATE_ESN)) {
		if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
H
Herbert Xu 已提交
799 800
			     "%s%sauthencesn(%s,%s)%s",
			     x->geniv ?: "", x->geniv ? "(" : "",
801
			     x->aalg ? x->aalg->alg_name : "digest_null",
H
Herbert Xu 已提交
802 803
			     x->ealg->alg_name,
			     x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
804 805 806
			goto error;
	} else {
		if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
H
Herbert Xu 已提交
807 808
			     "%s%sauthenc(%s,%s)%s",
			     x->geniv ?: "", x->geniv ? "(" : "",
809
			     x->aalg ? x->aalg->alg_name : "digest_null",
H
Herbert Xu 已提交
810 811
			     x->ealg->alg_name,
			     x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
812 813
			goto error;
	}
814 815 816 817 818 819

	aead = crypto_alloc_aead(authenc_name, 0, 0);
	err = PTR_ERR(aead);
	if (IS_ERR(aead))
		goto error;

820
	x->data = aead;
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

	keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
		 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
	err = -ENOMEM;
	key = kmalloc(keylen, GFP_KERNEL);
	if (!key)
		goto error;

	p = key;
	rta = (void *)p;
	rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
	rta->rta_len = RTA_LENGTH(sizeof(*param));
	param = RTA_DATA(rta);
	p += RTA_SPACE(sizeof(*param));

L
Linus Torvalds 已提交
836 837 838
	if (x->aalg) {
		struct xfrm_algo_desc *aalg_desc;

839 840
		memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
		p += (x->aalg->alg_key_len + 7) / 8;
L
Linus Torvalds 已提交
841 842 843 844

		aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
		BUG_ON(!aalg_desc);

845
		err = -EINVAL;
846
		if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
847
		    crypto_aead_authsize(aead)) {
848 849 850 851
			pr_info("ESP: %s digestsize %u != %hu\n",
				x->aalg->alg_name,
				crypto_aead_authsize(aead),
				aalg_desc->uinfo.auth.icv_fullbits / 8);
852
			goto free_key;
L
Linus Torvalds 已提交
853 854
		}

855
		err = crypto_aead_setauthsize(
856
			aead, x->aalg->alg_trunc_len / 8);
857 858
		if (err)
			goto free_key;
L
Linus Torvalds 已提交
859
	}
860

861 862 863 864 865 866 867 868
	param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
	memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);

	err = crypto_aead_setkey(aead, key, keylen);

free_key:
	kfree(key);

869 870 871 872 873 874 875 876 877 878
error:
	return err;
}

static int esp_init_state(struct xfrm_state *x)
{
	struct crypto_aead *aead;
	u32 align;
	int err;

879
	x->data = NULL;
880 881 882 883 884 885

	if (x->aead)
		err = esp_init_aead(x);
	else
		err = esp_init_authenc(x);

886
	if (err)
L
Linus Torvalds 已提交
887
		goto error;
888

889
	aead = x->data;
890

891 892
	x->props.header_len = sizeof(struct ip_esp_hdr) +
			      crypto_aead_ivsize(aead);
893
	if (x->props.mode == XFRM_MODE_TUNNEL)
L
Linus Torvalds 已提交
894
		x->props.header_len += sizeof(struct iphdr);
J
Joakim Koskela 已提交
895
	else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
896
		x->props.header_len += IPV4_BEET_PHMAXLEN;
L
Linus Torvalds 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909 910
	if (x->encap) {
		struct xfrm_encap_tmpl *encap = x->encap;

		switch (encap->encap_type) {
		default:
			goto error;
		case UDP_ENCAP_ESPINUDP:
			x->props.header_len += sizeof(struct udphdr);
			break;
		case UDP_ENCAP_ESPINUDP_NON_IKE:
			x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
			break;
		}
	}
911 912

	align = ALIGN(crypto_aead_blocksize(aead), 4);
913
	x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
L
Linus Torvalds 已提交
914 915

error:
916
	return err;
L
Linus Torvalds 已提交
917 918
}

919 920 921 922 923
static int esp4_rcv_cb(struct sk_buff *skb, int err)
{
	return 0;
}

924
static const struct xfrm_type esp_type =
L
Linus Torvalds 已提交
925 926 927 928
{
	.description	= "ESP4",
	.owner		= THIS_MODULE,
	.proto	     	= IPPROTO_ESP,
929
	.flags		= XFRM_TYPE_REPLAY_PROT,
L
Linus Torvalds 已提交
930 931
	.init_state	= esp_init_state,
	.destructor	= esp_destroy,
932
	.get_mtu	= esp4_get_mtu,
L
Linus Torvalds 已提交
933 934 935 936
	.input		= esp_input,
	.output		= esp_output
};

937
static struct xfrm4_protocol esp4_protocol = {
L
Linus Torvalds 已提交
938
	.handler	=	xfrm4_rcv,
939 940
	.input_handler	=	xfrm_input,
	.cb_handler	=	esp4_rcv_cb,
L
Linus Torvalds 已提交
941
	.err_handler	=	esp4_err,
942
	.priority	=	0,
L
Linus Torvalds 已提交
943 944 945 946 947
};

static int __init esp4_init(void)
{
	if (xfrm_register_type(&esp_type, AF_INET) < 0) {
J
Joe Perches 已提交
948
		pr_info("%s: can't add xfrm type\n", __func__);
L
Linus Torvalds 已提交
949 950
		return -EAGAIN;
	}
951
	if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
J
Joe Perches 已提交
952
		pr_info("%s: can't add protocol\n", __func__);
L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960
		xfrm_unregister_type(&esp_type, AF_INET);
		return -EAGAIN;
	}
	return 0;
}

static void __exit esp4_fini(void)
{
961
	if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
J
Joe Perches 已提交
962
		pr_info("%s: can't remove protocol\n", __func__);
L
Linus Torvalds 已提交
963
	if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
J
Joe Perches 已提交
964
		pr_info("%s: can't remove xfrm type\n", __func__);
L
Linus Torvalds 已提交
965 966 967 968 969
}

module_init(esp4_init);
module_exit(esp4_fini);
MODULE_LICENSE("GPL");
970
MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);