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