esp4.c 22.5 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
static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
S
Steffen Klassert 已提交
155
					       struct xfrm_state *x,
156 157 158 159 160 161 162 163
					       struct ip_esp_hdr *esph,
					       struct esp_output_extra *extra)
{
	/* 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)) {
164 165 166 167 168 169 170 171
		__u32 seqhi;
		struct xfrm_offload *xo = xfrm_offload(skb);

		if (xo)
			seqhi = xo->seq.hi;
		else
			seqhi = XFRM_SKB_CB(skb)->seq.output.hi;

172 173 174 175
		extra->esphoff = (unsigned char *)esph -
				 skb_transport_header(skb);
		esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
		extra->seqhi = esph->spi;
176
		esph->seq_no = htonl(seqhi);
177 178 179 180 181 182 183
	}

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

	return esph;
}

H
Herbert Xu 已提交
184 185 186 187 188 189 190 191
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);
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
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;
}

S
Steffen Klassert 已提交
208
static void esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
L
Linus Torvalds 已提交
209
{
S
Steffen Klassert 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	int encap_type;
	struct udphdr *uh;
	__be32 *udpdata32;
	__be16 sport, dport;
	struct xfrm_encap_tmpl *encap = x->encap;
	struct ip_esp_hdr *esph = esp->esph;

	spin_lock_bh(&x->lock);
	sport = encap->encap_sport;
	dport = encap->encap_dport;
	encap_type = encap->encap_type;
	spin_unlock_bh(&x->lock);

	uh = (struct udphdr *)esph;
	uh->source = sport;
	uh->dest = dport;
	uh->len = htons(skb->len + esp->tailen
		  - skb_transport_offset(skb));
	uh->check = 0;

	switch (encap_type) {
	default:
	case UDP_ENCAP_ESPINUDP:
		esph = (struct ip_esp_hdr *)(uh + 1);
		break;
	case UDP_ENCAP_ESPINUDP_NON_IKE:
		udpdata32 = (__be32 *)(uh + 1);
		udpdata32[0] = udpdata32[1] = 0;
		esph = (struct ip_esp_hdr *)(udpdata32 + 2);
		break;
240
	}
241

S
Steffen Klassert 已提交
242 243 244
	*skb_mac_header(skb) = IPPROTO_UDP;
	esp->esph = esph;
}
245

S
Steffen Klassert 已提交
246 247 248 249 250 251 252 253
int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
{
	u8 *tail;
	u8 *vaddr;
	int nfrags;
	struct page *page;
	struct sk_buff *trailer;
	int tailen = esp->tailen;
L
Linus Torvalds 已提交
254 255

	/* this is non-NULL only with UDP Encapsulation */
S
Steffen Klassert 已提交
256 257
	if (x->encap)
		esp_output_udp_encap(x, skb, esp);
L
Linus Torvalds 已提交
258

259 260 261 262 263
	if (!skb_cloned(skb)) {
		if (tailen <= skb_availroom(skb)) {
			nfrags = 1;
			trailer = skb;
			tail = skb_tail_pointer(trailer);
L
Linus Torvalds 已提交
264

265 266 267 268 269 270
			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 已提交
271

S
Steffen Klassert 已提交
272 273
			esp->inplace = false;

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
			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;

S
Steffen Klassert 已提交
290
			esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
291 292 293

			kunmap_atomic(vaddr);

S
Steffen Klassert 已提交
294 295
			spin_unlock_bh(&x->lock);

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
			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);

S
Steffen Klassert 已提交
311
			goto out;
312
		}
H
Herbert Xu 已提交
313 314
	}

315
cow:
S
Steffen Klassert 已提交
316 317 318
	nfrags = skb_cow_data(skb, tailen, &trailer);
	if (nfrags < 0)
		goto out;
319 320 321
	tail = skb_tail_pointer(trailer);

skip_cow:
S
Steffen Klassert 已提交
322 323
	esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
	pskb_put(skb, trailer, tailen);
324

S
Steffen Klassert 已提交
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
out:
	return nfrags;
}
EXPORT_SYMBOL_GPL(esp_output_head);

int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
{
	u8 *iv;
	int alen;
	void *tmp;
	int ivlen;
	int assoclen;
	int extralen;
	struct page *page;
	struct ip_esp_hdr *esph;
	struct crypto_aead *aead;
	struct aead_request *req;
	struct scatterlist *sg, *dsg;
	struct esp_output_extra *extra;
	int err = -ENOMEM;

	assoclen = sizeof(struct ip_esp_hdr);
	extralen = 0;

	if (x->props.flags & XFRM_STATE_ESN) {
		extralen += sizeof(*extra);
		assoclen += sizeof(__be32);
	}
H
Herbert Xu 已提交
353

S
Steffen Klassert 已提交
354 355 356 357 358
	aead = x->data;
	alen = crypto_aead_authsize(aead);
	ivlen = crypto_aead_ivsize(aead);

	tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
359
	if (!tmp)
360 361 362 363 364 365 366
		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);

S
Steffen Klassert 已提交
367 368 369 370
	if (esp->inplace)
		dsg = sg;
	else
		dsg = &sg[esp->nfrags];
371

S
Steffen Klassert 已提交
372 373 374 375
	esph = esp_output_set_extra(skb, x, esp->esph, extra);
	esp->esph = esph;

	sg_init_table(sg, esp->nfrags);
376
	skb_to_sgvec(skb, sg,
H
Herbert Xu 已提交
377
		     (unsigned char *)esph - skb->data,
S
Steffen Klassert 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
		     assoclen + ivlen + esp->clen + alen);

	if (!esp->inplace) {
		int allocsize;
		struct page_frag *pfrag = &x->xfrag;

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

		spin_lock_bh(&x->lock);
		if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
			spin_unlock_bh(&x->lock);
			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;
		spin_unlock_bh(&x->lock);

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

407 408 409 410 411
	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);

S
Steffen Klassert 已提交
412
	aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
H
Herbert Xu 已提交
413 414 415
	aead_request_set_ad(req, assoclen);

	memset(iv, 0, ivlen);
S
Steffen Klassert 已提交
416
	memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
H
Herbert Xu 已提交
417
	       min(ivlen, 8));
418 419

	ESP_SKB_CB(skb)->tmp = tmp;
H
Herbert Xu 已提交
420 421 422 423
	err = crypto_aead_encrypt(req);

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

H
Herbert Xu 已提交
426
	case -EBUSY:
427
		err = NET_XMIT_DROP;
H
Herbert Xu 已提交
428 429 430 431 432 433
		break;

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

435 436
	if (sg != dsg)
		esp_ssg_unref(x, tmp);
437
	kfree(tmp);
438

L
Linus Torvalds 已提交
439 440 441
error:
	return err;
}
S
Steffen Klassert 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
EXPORT_SYMBOL_GPL(esp_output_tail);

static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
{
	int alen;
	int blksize;
	struct ip_esp_hdr *esph;
	struct crypto_aead *aead;
	struct esp_info esp;

	esp.inplace = true;

	esp.proto = *skb_mac_header(skb);
	*skb_mac_header(skb) = IPPROTO_ESP;

	/* skb is pure payload to encrypt */

	aead = x->data;
	alen = crypto_aead_authsize(aead);

	esp.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)
			esp.tfclen = padto - skb->len;
	}
	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
	esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
	esp.plen = esp.clen - skb->len - esp.tfclen;
	esp.tailen = esp.tfclen + esp.plen + alen;

	esp.esph = ip_esp_hdr(skb);

	esp.nfrags = esp_output_head(x, skb, &esp);
	if (esp.nfrags < 0)
		return esp.nfrags;

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

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

	skb_push(skb, -skb_network_offset(skb));

	return esp_output_tail(x, skb, &esp);
}
L
Linus Torvalds 已提交
493

S
Steffen Klassert 已提交
494
int esp_input_done2(struct sk_buff *skb, int err)
L
Linus Torvalds 已提交
495
{
496
	const struct iphdr *iph;
497
	struct xfrm_state *x = xfrm_input_state(skb);
498
	struct xfrm_offload *xo = xfrm_offload(skb);
499
	struct crypto_aead *aead = x->data;
500 501 502
	int alen = crypto_aead_authsize(aead);
	int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
	int elen = skb->len - hlen;
503
	int ihl;
504 505
	u8 nexthdr[2];
	int padlen;
L
Linus Torvalds 已提交
506

507 508
	if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
		kfree(ESP_SKB_CB(skb)->tmp);
509

510
	if (unlikely(err))
511
		goto out;
L
Linus Torvalds 已提交
512

513 514
	if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
		BUG();
L
Linus Torvalds 已提交
515

516
	err = -EINVAL;
517
	padlen = nexthdr[0];
518
	if (padlen + 2 + alen >= elen)
519
		goto out;
L
Linus Torvalds 已提交
520

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

523
	iph = ip_hdr(skb);
524 525
	ihl = iph->ihl * 4;

526 527
	if (x->encap) {
		struct xfrm_encap_tmpl *encap = x->encap;
528
		struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
529 530 531 532 533 534 535 536 537 538 539 540 541

		/*
		 * 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);
542

543 544 545 546 547 548 549
			/* 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 已提交
550
		}
551

552 553 554 555 556 557 558
		/*
		 * 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
		 */
559
		if (x->props.mode == XFRM_MODE_TRANSPORT)
560
			skb->ip_summed = CHECKSUM_UNNECESSARY;
L
Linus Torvalds 已提交
561 562
	}

563
	pskb_trim(skb, skb->len - alen - padlen - 2);
564
	__skb_pull(skb, hlen);
565 566 567 568
	if (x->props.mode == XFRM_MODE_TUNNEL)
		skb_reset_transport_header(skb);
	else
		skb_set_transport_header(skb, -ihl);
569

570 571 572 573 574 575 576 577 578
	err = nexthdr[1];

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

out:
	return err;
}
S
Steffen Klassert 已提交
579
EXPORT_SYMBOL_GPL(esp_input_done2);
580 581 582 583 584 585 586 587

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 已提交
588 589 590 591 592 593
static void esp_input_restore_header(struct sk_buff *skb)
{
	esp_restore_header(skb, 0);
	__skb_pull(skb, 4);
}

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
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 已提交
611 612 613 614 615 616 617 618
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);
}

619 620 621 622 623 624 625 626
/*
 * 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;
627
	struct crypto_aead *aead = x->data;
628 629
	struct aead_request *req;
	struct sk_buff *trailer;
H
Herbert Xu 已提交
630 631
	int ivlen = crypto_aead_ivsize(aead);
	int elen = skb->len - sizeof(*esph) - ivlen;
632
	int nfrags;
633 634 635
	int assoclen;
	int seqhilen;
	__be32 *seqhi;
636 637 638 639 640
	void *tmp;
	u8 *iv;
	struct scatterlist *sg;
	int err = -EINVAL;

H
Herbert Xu 已提交
641
	if (!pskb_may_pull(skb, sizeof(*esph) + ivlen))
642 643 644 645 646
		goto out;

	if (elen <= 0)
		goto out;

647 648 649 650 651 652 653 654
	assoclen = sizeof(*esph);
	seqhilen = 0;

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

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
	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:
675
	err = -ENOMEM;
H
Herbert Xu 已提交
676
	tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
677 678 679 680
	if (!tmp)
		goto out;

	ESP_SKB_CB(skb)->tmp = tmp;
681
	seqhi = esp_tmp_extra(tmp);
682
	iv = esp_tmp_iv(aead, tmp, seqhilen);
683
	req = esp_tmp_req(aead, iv);
H
Herbert Xu 已提交
684
	sg = esp_req_sg(aead, req);
685

686
	esp_input_set_header(skb, seqhi);
687

688 689
	sg_init_table(sg, nfrags);
	skb_to_sgvec(skb, sg, 0, skb->len);
690

691
	skb->ip_summed = CHECKSUM_NONE;
692

693
	if ((x->props.flags & XFRM_STATE_ESN))
H
Herbert Xu 已提交
694
		aead_request_set_callback(req, 0, esp_input_done_esn, skb);
695 696
	else
		aead_request_set_callback(req, 0, esp_input_done, skb);
H
Herbert Xu 已提交
697 698 699

	aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
	aead_request_set_ad(req, assoclen);
700 701 702 703 704

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

H
Herbert Xu 已提交
705 706 707
	if ((x->props.flags & XFRM_STATE_ESN))
		esp_input_restore_header(skb);

708
	err = esp_input_done2(skb, err);
L
Linus Torvalds 已提交
709 710

out:
711
	return err;
L
Linus Torvalds 已提交
712 713
}

714
static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
L
Linus Torvalds 已提交
715
{
716 717
	struct crypto_aead *aead = x->data;
	u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
718
	unsigned int net_adj;
D
Diego Beltrami 已提交
719 720 721 722

	switch (x->props.mode) {
	case XFRM_MODE_TRANSPORT:
	case XFRM_MODE_BEET:
723
		net_adj = sizeof(struct iphdr);
D
Diego Beltrami 已提交
724
		break;
725 726 727 728 729
	case XFRM_MODE_TUNNEL:
		net_adj = 0;
		break;
	default:
		BUG();
L
Linus Torvalds 已提交
730
	}
D
Diego Beltrami 已提交
731

732
	return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
733
		 net_adj) & ~(blksize - 1)) + net_adj - 2;
L
Linus Torvalds 已提交
734 735
}

736
static int esp4_err(struct sk_buff *skb, u32 info)
L
Linus Torvalds 已提交
737
{
A
Alexey Dobriyan 已提交
738
	struct net *net = dev_net(skb->dev);
739
	const struct iphdr *iph = (const struct iphdr *)skb->data;
740
	struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
L
Linus Torvalds 已提交
741 742
	struct xfrm_state *x;

743 744 745
	switch (icmp_hdr(skb)->type) {
	case ICMP_DEST_UNREACH:
		if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
746
			return 0;
747 748 749
	case ICMP_REDIRECT:
		break;
	default:
750
		return 0;
751
	}
L
Linus Torvalds 已提交
752

753 754
	x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
			      esph->spi, IPPROTO_ESP, AF_INET);
L
Linus Torvalds 已提交
755
	if (!x)
756
		return 0;
757

758
	if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
759
		ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ESP, 0);
760
	else
761
		ipv4_redirect(skb, net, 0, 0, IPPROTO_ESP, 0);
L
Linus Torvalds 已提交
762
	xfrm_state_put(x);
763 764

	return 0;
L
Linus Torvalds 已提交
765 766 767 768
}

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

771
	if (!aead)
L
Linus Torvalds 已提交
772 773
		return;

774
	crypto_free_aead(aead);
L
Linus Torvalds 已提交
775 776
}

777
static int esp_init_aead(struct xfrm_state *x)
L
Linus Torvalds 已提交
778
{
H
Herbert Xu 已提交
779
	char aead_name[CRYPTO_MAX_ALG_NAME];
780 781
	struct crypto_aead *aead;
	int err;
782
	u32 mask = 0;
783

H
Herbert Xu 已提交
784 785 786 787 788
	err = -ENAMETOOLONG;
	if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
		     x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
		goto error;

789 790 791 792
	if (x->xso.offload_handle)
		mask |= CRYPTO_ALG_ASYNC;

	aead = crypto_alloc_aead(aead_name, 0, mask);
793 794 795 796
	err = PTR_ERR(aead);
	if (IS_ERR(aead))
		goto error;

797
	x->data = aead;
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

	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)
{
814 815 816 817 818 819 820 821
	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;
822
	u32 mask = 0;
L
Linus Torvalds 已提交
823

824
	err = -EINVAL;
825
	if (!x->ealg)
826
		goto error;
827

828
	err = -ENAMETOOLONG;
829 830 831

	if ((x->props.flags & XFRM_STATE_ESN)) {
		if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
H
Herbert Xu 已提交
832 833
			     "%s%sauthencesn(%s,%s)%s",
			     x->geniv ?: "", x->geniv ? "(" : "",
834
			     x->aalg ? x->aalg->alg_name : "digest_null",
H
Herbert Xu 已提交
835 836
			     x->ealg->alg_name,
			     x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
837 838 839
			goto error;
	} else {
		if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
H
Herbert Xu 已提交
840 841
			     "%s%sauthenc(%s,%s)%s",
			     x->geniv ?: "", x->geniv ? "(" : "",
842
			     x->aalg ? x->aalg->alg_name : "digest_null",
H
Herbert Xu 已提交
843 844
			     x->ealg->alg_name,
			     x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
845 846
			goto error;
	}
847

848 849 850 851
	if (x->xso.offload_handle)
		mask |= CRYPTO_ALG_ASYNC;

	aead = crypto_alloc_aead(authenc_name, 0, mask);
852 853 854 855
	err = PTR_ERR(aead);
	if (IS_ERR(aead))
		goto error;

856
	x->data = aead;
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

	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 已提交
872 873 874
	if (x->aalg) {
		struct xfrm_algo_desc *aalg_desc;

875 876
		memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
		p += (x->aalg->alg_key_len + 7) / 8;
L
Linus Torvalds 已提交
877 878 879 880

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

881
		err = -EINVAL;
882
		if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
883
		    crypto_aead_authsize(aead)) {
884 885 886 887
			pr_info("ESP: %s digestsize %u != %hu\n",
				x->aalg->alg_name,
				crypto_aead_authsize(aead),
				aalg_desc->uinfo.auth.icv_fullbits / 8);
888
			goto free_key;
L
Linus Torvalds 已提交
889 890
		}

891
		err = crypto_aead_setauthsize(
892
			aead, x->aalg->alg_trunc_len / 8);
893 894
		if (err)
			goto free_key;
L
Linus Torvalds 已提交
895
	}
896

897 898 899 900 901 902 903 904
	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);

905 906 907 908 909 910 911 912 913 914
error:
	return err;
}

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

915
	x->data = NULL;
916 917 918 919 920 921

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

922
	if (err)
L
Linus Torvalds 已提交
923
		goto error;
924

925
	aead = x->data;
926

927 928
	x->props.header_len = sizeof(struct ip_esp_hdr) +
			      crypto_aead_ivsize(aead);
929
	if (x->props.mode == XFRM_MODE_TUNNEL)
L
Linus Torvalds 已提交
930
		x->props.header_len += sizeof(struct iphdr);
J
Joakim Koskela 已提交
931
	else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
932
		x->props.header_len += IPV4_BEET_PHMAXLEN;
L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946
	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;
		}
	}
947 948

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

error:
952
	return err;
L
Linus Torvalds 已提交
953 954
}

955 956 957 958 959
static int esp4_rcv_cb(struct sk_buff *skb, int err)
{
	return 0;
}

960
static const struct xfrm_type esp_type =
L
Linus Torvalds 已提交
961 962 963 964
{
	.description	= "ESP4",
	.owner		= THIS_MODULE,
	.proto	     	= IPPROTO_ESP,
965
	.flags		= XFRM_TYPE_REPLAY_PROT,
L
Linus Torvalds 已提交
966 967
	.init_state	= esp_init_state,
	.destructor	= esp_destroy,
968
	.get_mtu	= esp4_get_mtu,
L
Linus Torvalds 已提交
969
	.input		= esp_input,
S
Steffen Klassert 已提交
970
	.output		= esp_output,
L
Linus Torvalds 已提交
971 972
};

973
static struct xfrm4_protocol esp4_protocol = {
L
Linus Torvalds 已提交
974
	.handler	=	xfrm4_rcv,
975 976
	.input_handler	=	xfrm_input,
	.cb_handler	=	esp4_rcv_cb,
L
Linus Torvalds 已提交
977
	.err_handler	=	esp4_err,
978
	.priority	=	0,
L
Linus Torvalds 已提交
979 980 981 982 983
};

static int __init esp4_init(void)
{
	if (xfrm_register_type(&esp_type, AF_INET) < 0) {
J
Joe Perches 已提交
984
		pr_info("%s: can't add xfrm type\n", __func__);
L
Linus Torvalds 已提交
985 986
		return -EAGAIN;
	}
987
	if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
J
Joe Perches 已提交
988
		pr_info("%s: can't add protocol\n", __func__);
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996
		xfrm_unregister_type(&esp_type, AF_INET);
		return -EAGAIN;
	}
	return 0;
}

static void __exit esp4_fini(void)
{
997
	if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
J
Joe Perches 已提交
998
		pr_info("%s: can't remove protocol\n", __func__);
L
Linus Torvalds 已提交
999
	if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
J
Joe Perches 已提交
1000
		pr_info("%s: can't remove xfrm type\n", __func__);
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005
}

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