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