1 /* $NetBSD: xform_ah.c,v 1.109 2019/11/01 04:23:21 knakahara Exp $ */ 2 /* $FreeBSD: xform_ah.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */ 3 /* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */ 4 /* 5 * The authors of this code are John Ioannidis (ji@tla.org), 6 * Angelos D. Keromytis (kermit@csd.uch.gr) and 7 * Niels Provos (provos@physnet.uni-hamburg.de). 8 * 9 * The original version of this code was written by John Ioannidis 10 * for BSD/OS in Athens, Greece, in November 1995. 11 * 12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 13 * by Angelos D. Keromytis. 14 * 15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 16 * and Niels Provos. 17 * 18 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist. 19 * 20 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 21 * Angelos D. Keromytis and Niels Provos. 22 * Copyright (c) 1999 Niklas Hallqvist. 23 * Copyright (c) 2001 Angelos D. Keromytis. 24 * 25 * Permission to use, copy, and modify this software with or without fee 26 * is hereby granted, provided that this entire notice is included in 27 * all copies of any software which is or includes a copy or 28 * modification of this software. 29 * You may use this code under the GNU public license if you so wish. Please 30 * contribute changes back to the authors under this freer than GPL license 31 * so that we may further the use of strong encryption without limitations to 32 * all. 33 * 34 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 35 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 36 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 37 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 38 * PURPOSE. 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: xform_ah.c,v 1.109 2019/11/01 04:23:21 knakahara Exp $"); 43 44 #if defined(_KERNEL_OPT) 45 #include "opt_inet.h" 46 #include "opt_ipsec.h" 47 #endif 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/mbuf.h> 52 #include <sys/socket.h> 53 #include <sys/syslog.h> 54 #include <sys/kernel.h> 55 #include <sys/sysctl.h> 56 #include <sys/pool.h> 57 #include <sys/pserialize.h> 58 #include <sys/kmem.h> 59 60 #include <net/if.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_systm.h> 64 #include <netinet/ip.h> 65 #include <netinet/ip_ecn.h> 66 #include <netinet/ip_var.h> 67 #include <netinet/ip6.h> 68 69 #include <net/route.h> 70 #include <netipsec/ipsec.h> 71 #include <netipsec/ipsec_private.h> 72 #include <netipsec/ah.h> 73 #include <netipsec/ah_var.h> 74 #include <netipsec/xform.h> 75 76 #ifdef INET6 77 #include <netinet6/ip6_var.h> 78 #include <netinet6/scope6_var.h> 79 #include <netipsec/ipsec6.h> 80 #endif 81 82 #include <netipsec/key.h> 83 #include <netipsec/key_debug.h> 84 85 #include <opencrypto/cryptodev.h> 86 87 /* 88 * Return header size in bytes. The old protocol did not support 89 * the replay counter; the new protocol always includes the counter. 90 */ 91 #define HDRSIZE(sav) \ 92 (((sav)->flags & SADB_X_EXT_OLD) ? \ 93 sizeof(struct ah) : sizeof(struct ah) + sizeof(uint32_t)) 94 /* 95 * Return authenticator size in bytes. The old protocol is known 96 * to use a fixed 16-byte authenticator. The new algorithm gets 97 * this size from the xform but is (currently) always 12. 98 */ 99 #define AUTHSIZE(sav) \ 100 ((sav->flags & SADB_X_EXT_OLD) ? 16 : (sav)->tdb_authalgxform->authsize) 101 102 percpu_t *ahstat_percpu; 103 104 int ah_enable = 1; /* control flow of packets with AH */ 105 int ip4_ah_cleartos = 1; /* clear ip_tos when doing AH calc */ 106 107 static const char ipseczeroes[256]; 108 109 int ah_max_authsize; /* max authsize over all algorithms */ 110 111 static int ah_input_cb(struct cryptop *); 112 static int ah_output_cb(struct cryptop *); 113 114 const uint8_t ah_stats[256] = { SADB_AALG_STATS_INIT }; 115 116 static pool_cache_t ah_tdb_crypto_pool_cache; 117 static size_t ah_pool_item_size; 118 119 /* 120 * NB: this is public for use by the PF_KEY support. 121 */ 122 const struct auth_hash * 123 ah_algorithm_lookup(int alg) 124 { 125 126 switch (alg) { 127 case SADB_X_AALG_NULL: 128 return &auth_hash_null; 129 case SADB_AALG_MD5HMAC: 130 return &auth_hash_hmac_md5_96; 131 case SADB_AALG_SHA1HMAC: 132 return &auth_hash_hmac_sha1_96; 133 case SADB_X_AALG_RIPEMD160HMAC: 134 return &auth_hash_hmac_ripemd_160_96; 135 case SADB_X_AALG_MD5: 136 return &auth_hash_key_md5; 137 case SADB_X_AALG_SHA: 138 return &auth_hash_key_sha1; 139 case SADB_X_AALG_SHA2_256: 140 return &auth_hash_hmac_sha2_256; 141 case SADB_X_AALG_SHA2_384: 142 return &auth_hash_hmac_sha2_384; 143 case SADB_X_AALG_SHA2_512: 144 return &auth_hash_hmac_sha2_512; 145 case SADB_X_AALG_AES_XCBC_MAC: 146 return &auth_hash_aes_xcbc_mac_96; 147 } 148 return NULL; 149 } 150 151 size_t 152 ah_authsiz(const struct secasvar *sav) 153 { 154 size_t size; 155 156 if (sav == NULL) { 157 return ah_max_authsize; 158 } 159 160 size = AUTHSIZE(sav); 161 return roundup(size, sizeof(uint32_t)); 162 } 163 164 size_t 165 ah_hdrsiz(const struct secasvar *sav) 166 { 167 size_t size; 168 169 if (sav != NULL) { 170 int authsize, rplen, align; 171 172 KASSERT(sav->tdb_authalgxform != NULL); 173 /*XXX not right for null algorithm--does it matter??*/ 174 175 /* RFC4302: use the correct alignment. */ 176 align = sizeof(uint32_t); 177 #ifdef INET6 178 if (sav->sah->saidx.dst.sa.sa_family == AF_INET6) { 179 align = sizeof(uint64_t); 180 } 181 #endif 182 rplen = HDRSIZE(sav); 183 authsize = AUTHSIZE(sav); 184 size = roundup(rplen + authsize, align); 185 } else { 186 /* default guess */ 187 size = sizeof(struct ah) + sizeof(uint32_t) + ah_max_authsize; 188 } 189 return size; 190 } 191 192 /* 193 * NB: public for use by esp_init. 194 */ 195 int 196 ah_init0(struct secasvar *sav, const struct xformsw *xsp, 197 struct cryptoini *cria) 198 { 199 const struct auth_hash *thash; 200 int keylen; 201 202 thash = ah_algorithm_lookup(sav->alg_auth); 203 if (thash == NULL) { 204 DPRINTF("unsupported authentication algorithm %u\n", 205 sav->alg_auth); 206 return EINVAL; 207 } 208 /* 209 * Verify the replay state block allocation is consistent with 210 * the protocol type. We check here so we can make assumptions 211 * later during protocol processing. 212 */ 213 /* NB: replay state is setup elsewhere (sigh) */ 214 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) { 215 DPRINTF("replay state block inconsistency, " 216 "%s algorithm %s replay state\n", 217 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new", 218 sav->replay == NULL ? "without" : "with"); 219 return EINVAL; 220 } 221 if (sav->key_auth == NULL) { 222 DPRINTF("no authentication key for %s algorithm\n", 223 thash->name); 224 return EINVAL; 225 } 226 keylen = _KEYLEN(sav->key_auth); 227 if (keylen != thash->keysize && thash->keysize != 0) { 228 DPRINTF("invalid keylength %d, algorithm %s requires " 229 "keysize %d\n", 230 keylen, thash->name, thash->keysize); 231 return EINVAL; 232 } 233 234 sav->tdb_xform = xsp; 235 sav->tdb_authalgxform = thash; 236 237 /* Initialize crypto session. */ 238 memset(cria, 0, sizeof(*cria)); 239 cria->cri_alg = sav->tdb_authalgxform->type; 240 cria->cri_klen = _KEYBITS(sav->key_auth); 241 cria->cri_key = _KEYBUF(sav->key_auth); 242 243 return 0; 244 } 245 246 /* 247 * ah_init() is called when an SPI is being set up. 248 */ 249 static int 250 ah_init(struct secasvar *sav, const struct xformsw *xsp) 251 { 252 struct cryptoini cria; 253 int error; 254 255 error = ah_init0(sav, xsp, &cria); 256 if (!error) 257 error = crypto_newsession(&sav->tdb_cryptoid, 258 &cria, crypto_support); 259 return error; 260 } 261 262 /* 263 * Paranoia. 264 * 265 * NB: public for use by esp_zeroize (XXX). 266 */ 267 int 268 ah_zeroize(struct secasvar *sav) 269 { 270 int err; 271 272 if (sav->key_auth) { 273 explicit_memset(_KEYBUF(sav->key_auth), 0, 274 _KEYLEN(sav->key_auth)); 275 } 276 277 err = crypto_freesession(sav->tdb_cryptoid); 278 sav->tdb_cryptoid = 0; 279 sav->tdb_authalgxform = NULL; 280 sav->tdb_xform = NULL; 281 return err; 282 } 283 284 /* 285 * Massage IPv4/IPv6 headers for AH processing. 286 */ 287 static int 288 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out) 289 { 290 struct mbuf *m = *m0; 291 unsigned char *ptr; 292 int off, optlen; 293 #ifdef INET 294 struct ip *ip; 295 #endif 296 #ifdef INET6 297 int count, ip6optlen; 298 struct ip6_ext *ip6e; 299 struct ip6_hdr ip6; 300 int alloc, nxt; 301 #endif 302 303 switch (proto) { 304 #ifdef INET 305 case AF_INET: 306 /* 307 * This is the least painful way of dealing with IPv4 header 308 * and option processing -- just make sure they're in 309 * contiguous memory. 310 */ 311 *m0 = m = m_pullup(m, skip); 312 if (m == NULL) { 313 DPRINTF("m_pullup failed\n"); 314 return ENOBUFS; 315 } 316 317 /* Fix the IP header */ 318 ip = mtod(m, struct ip *); 319 if (ip4_ah_cleartos) 320 ip->ip_tos = 0; 321 ip->ip_ttl = 0; 322 ip->ip_sum = 0; 323 ip->ip_off = htons(ntohs(ip->ip_off) & ip4_ah_offsetmask); 324 325 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 326 ip->ip_off &= htons(IP_DF); 327 else 328 ip->ip_off = 0; 329 330 ptr = mtod(m, unsigned char *); 331 332 /* IPv4 option processing */ 333 for (off = sizeof(struct ip); off < skip;) { 334 if (ptr[off] == IPOPT_EOL) { 335 break; 336 } else if (ptr[off] == IPOPT_NOP) { 337 optlen = 1; 338 } else if (off + 1 < skip) { 339 optlen = ptr[off + 1]; 340 if (optlen < 2 || off + optlen > skip) { 341 m_freem(m); 342 return EINVAL; 343 } 344 } else { 345 m_freem(m); 346 return EINVAL; 347 } 348 349 switch (ptr[off]) { 350 case IPOPT_NOP: 351 case IPOPT_SECURITY: 352 case 0x85: /* Extended security. */ 353 case 0x86: /* Commercial security. */ 354 case 0x94: /* Router alert */ 355 case 0x95: /* RFC1770 */ 356 break; 357 358 case IPOPT_LSRR: 359 case IPOPT_SSRR: 360 /* 361 * On output, if we have either of the 362 * source routing options, we should 363 * swap the destination address of the 364 * IP header with the last address 365 * specified in the option, as that is 366 * what the destination's IP header 367 * will look like. 368 */ 369 if (out) 370 memcpy(&ip->ip_dst, 371 ptr + off + optlen - 372 sizeof(struct in_addr), 373 sizeof(struct in_addr)); 374 /* FALLTHROUGH */ 375 376 default: 377 /* Zeroize all other options. */ 378 memset(ptr + off, 0, optlen); 379 break; 380 } 381 382 off += optlen; 383 384 /* Sanity check. */ 385 if (off > skip) { 386 m_freem(m); 387 return EINVAL; 388 } 389 } 390 391 break; 392 #endif /* INET */ 393 394 #ifdef INET6 395 case AF_INET6: /* Ugly... */ 396 /* Copy and "cook" the IPv6 header. */ 397 m_copydata(m, 0, sizeof(ip6), &ip6); 398 399 /* We don't do IPv6 Jumbograms. */ 400 if (ip6.ip6_plen == 0) { 401 DPRINTF("unsupported IPv6 jumbogram\n"); 402 m_freem(m); 403 return EMSGSIZE; 404 } 405 406 ip6.ip6_flow = 0; 407 ip6.ip6_hlim = 0; 408 ip6.ip6_vfc &= ~IPV6_VERSION_MASK; 409 ip6.ip6_vfc |= IPV6_VERSION; 410 411 /* Scoped address handling. */ 412 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src)) 413 ip6.ip6_src.s6_addr16[1] = 0; 414 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst)) 415 ip6.ip6_dst.s6_addr16[1] = 0; 416 417 /* Done with IPv6 header. */ 418 m_copyback(m, 0, sizeof(struct ip6_hdr), &ip6); 419 420 ip6optlen = skip - sizeof(struct ip6_hdr); 421 422 /* Let's deal with the remaining headers (if any). */ 423 if (ip6optlen > 0) { 424 if (m->m_len <= skip) { 425 ptr = malloc(ip6optlen, M_XDATA, M_NOWAIT); 426 if (ptr == NULL) { 427 DPRINTF("failed to allocate " 428 "memory for IPv6 headers\n"); 429 m_freem(m); 430 return ENOBUFS; 431 } 432 433 /* 434 * Copy all the protocol headers after 435 * the IPv6 header. 436 */ 437 m_copydata(m, sizeof(struct ip6_hdr), 438 ip6optlen, ptr); 439 alloc = 1; 440 } else { 441 /* No need to allocate memory. */ 442 ptr = mtod(m, unsigned char *) + 443 sizeof(struct ip6_hdr); 444 alloc = 0; 445 } 446 } else 447 break; 448 449 nxt = ip6.ip6_nxt & 0xff; /* Next header type. */ 450 451 for (off = 0; off < ip6optlen;) { 452 int noff; 453 454 if (off + sizeof(*ip6e) > ip6optlen) { 455 goto error6; 456 } 457 ip6e = (struct ip6_ext *)(ptr + off); 458 noff = off + ((ip6e->ip6e_len + 1) << 3); 459 if (noff > ip6optlen) { 460 goto error6; 461 } 462 463 switch (nxt) { 464 case IPPROTO_HOPOPTS: 465 case IPPROTO_DSTOPTS: 466 /* Zero out mutable options. */ 467 for (count = off + sizeof(struct ip6_ext); 468 count < noff;) { 469 if (ptr[count] == IP6OPT_PAD1) { 470 count++; 471 continue; 472 } 473 474 if (count + 1 >= noff) { 475 goto error6; 476 } 477 optlen = ptr[count + 1] + 2; 478 479 if (count + optlen > noff) { 480 goto error6; 481 } 482 483 if (ptr[count] & IP6OPT_MUTABLE) { 484 memset(ptr + count, 0, optlen); 485 } 486 487 count += optlen; 488 } 489 if (count != noff) { 490 goto error6; 491 } 492 /* FALLTHROUGH */ 493 494 case IPPROTO_ROUTING: 495 /* Advance. */ 496 off = noff; 497 nxt = ip6e->ip6e_nxt; 498 break; 499 500 default: 501 error6: 502 if (alloc) 503 free(ptr, M_XDATA); 504 m_freem(m); 505 return EINVAL; 506 } 507 } 508 509 /* Copyback and free, if we allocated. */ 510 if (alloc) { 511 m_copyback(m, sizeof(struct ip6_hdr), ip6optlen, ptr); 512 free(ptr, M_XDATA); 513 } 514 515 break; 516 #endif /* INET6 */ 517 } 518 519 return 0; 520 } 521 522 /* 523 * ah_input() gets called to verify that an input packet 524 * passes authentication. 525 */ 526 static int 527 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 528 { 529 const struct auth_hash *ahx; 530 struct tdb_crypto *tc = NULL; 531 struct newah *ah; 532 int hl, rplen, authsize, ahsize, error, stat = AH_STAT_HDROPS; 533 struct cryptodesc *crda; 534 struct cryptop *crp = NULL; 535 bool pool_used; 536 uint8_t nxt; 537 538 KASSERT(sav != NULL); 539 KASSERT(sav->key_auth != NULL); 540 KASSERT(sav->tdb_authalgxform != NULL); 541 542 /* Figure out header size. */ 543 rplen = HDRSIZE(sav); 544 545 /* XXX don't pullup, just copy header */ 546 M_REGION_GET(ah, struct newah *, m, skip, rplen); 547 if (ah == NULL) { 548 /* m already freed */ 549 return ENOBUFS; 550 } 551 552 nxt = ah->ah_nxt; 553 554 /* Check replay window, if applicable. */ 555 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) { 556 char buf[IPSEC_LOGSASTRLEN]; 557 DPRINTF("packet replay failure: %s\n", 558 ipsec_logsastr(sav, buf, sizeof(buf))); 559 stat = AH_STAT_REPLAY; 560 error = EACCES; 561 goto bad; 562 } 563 564 /* Verify AH header length. */ 565 hl = sizeof(struct ah) + (ah->ah_len * sizeof(uint32_t)); 566 ahx = sav->tdb_authalgxform; 567 authsize = AUTHSIZE(sav); 568 ahsize = ah_hdrsiz(sav); 569 if (hl != ahsize) { 570 char buf[IPSEC_ADDRSTRLEN]; 571 DPRINTF("bad authenticator length %u (expecting %lu)" 572 " for packet in SA %s/%08lx\n", 573 hl, (u_long)ahsize, 574 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 575 (u_long) ntohl(sav->spi)); 576 stat = AH_STAT_BADAUTHL; 577 error = EACCES; 578 goto bad; 579 } 580 if (skip + ahsize > m->m_pkthdr.len) { 581 char buf[IPSEC_ADDRSTRLEN]; 582 DPRINTF("bad mbuf length %u (expecting >= %lu)" 583 " for packet in SA %s/%08lx\n", 584 m->m_pkthdr.len, (u_long)(skip + ahsize), 585 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 586 (u_long) ntohl(sav->spi)); 587 stat = AH_STAT_BADAUTHL; 588 error = EACCES; 589 goto bad; 590 } 591 592 AH_STATADD(AH_STAT_IBYTES, m->m_pkthdr.len - skip - hl); 593 594 /* Get crypto descriptors. */ 595 crp = crypto_getreq(1); 596 if (crp == NULL) { 597 DPRINTF("failed to acquire crypto descriptor\n"); 598 stat = AH_STAT_CRYPTO; 599 error = ENOBUFS; 600 goto bad; 601 } 602 603 crda = crp->crp_desc; 604 KASSERT(crda != NULL); 605 606 crda->crd_skip = 0; 607 crda->crd_len = m->m_pkthdr.len; 608 crda->crd_inject = skip + rplen; 609 610 /* Authentication operation. */ 611 crda->crd_alg = ahx->type; 612 crda->crd_key = _KEYBUF(sav->key_auth); 613 crda->crd_klen = _KEYBITS(sav->key_auth); 614 615 /* Allocate IPsec-specific opaque crypto info. */ 616 size_t size = sizeof(*tc); 617 size_t extra = skip + rplen + authsize; 618 size += extra; 619 620 if (__predict_true(size <= ah_pool_item_size)) { 621 tc = pool_cache_get(ah_tdb_crypto_pool_cache, PR_NOWAIT); 622 pool_used = true; 623 } else { 624 /* size can exceed on IPv6 packets with large options. */ 625 tc = kmem_intr_zalloc(size, KM_NOSLEEP); 626 pool_used = false; 627 } 628 if (tc == NULL) { 629 DPRINTF("failed to allocate tdb_crypto\n"); 630 stat = AH_STAT_CRYPTO; 631 error = ENOBUFS; 632 goto bad; 633 } 634 635 error = m_makewritable(&m, 0, extra, M_NOWAIT); 636 if (error) { 637 DPRINTF("failed to m_makewritable\n"); 638 goto bad; 639 } 640 641 /* 642 * Save the authenticator, the skipped portion of the packet, 643 * and the AH header. 644 */ 645 m_copydata(m, 0, extra, (tc + 1)); 646 /* Zeroize the authenticator on the packet. */ 647 m_copyback(m, skip + rplen, authsize, ipseczeroes); 648 649 /* "Massage" the packet headers for crypto processing. */ 650 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 651 skip, ahx->type, 0); 652 if (error != 0) { 653 /* NB: mbuf is free'd by ah_massage_headers */ 654 m = NULL; 655 goto bad; 656 } 657 658 { 659 int s = pserialize_read_enter(); 660 661 /* 662 * Take another reference to the SA for opencrypto callback. 663 */ 664 if (__predict_false(sav->state == SADB_SASTATE_DEAD)) { 665 pserialize_read_exit(s); 666 stat = AH_STAT_NOTDB; 667 error = ENOENT; 668 goto bad; 669 } 670 KEY_SA_REF(sav); 671 pserialize_read_exit(s); 672 } 673 674 /* Crypto operation descriptor. */ 675 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 676 crp->crp_flags = CRYPTO_F_IMBUF; 677 crp->crp_buf = m; 678 crp->crp_callback = ah_input_cb; 679 crp->crp_sid = sav->tdb_cryptoid; 680 crp->crp_opaque = tc; 681 682 /* These are passed as-is to the callback. */ 683 tc->tc_spi = sav->spi; 684 tc->tc_dst = sav->sah->saidx.dst; 685 tc->tc_proto = sav->sah->saidx.proto; 686 tc->tc_nxt = nxt; 687 tc->tc_protoff = protoff; 688 tc->tc_skip = skip; 689 tc->tc_sav = sav; 690 691 DPRINTF("hash over %d bytes, skip %d: " 692 "crda len %d skip %d inject %d\n", 693 crp->crp_ilen, tc->tc_skip, 694 crda->crd_len, crda->crd_skip, crda->crd_inject); 695 696 return crypto_dispatch(crp); 697 698 bad: 699 if (tc != NULL) { 700 if (__predict_true(pool_used)) 701 pool_cache_put(ah_tdb_crypto_pool_cache, tc); 702 else 703 kmem_intr_free(tc, size); 704 } 705 if (crp != NULL) 706 crypto_freereq(crp); 707 if (m != NULL) 708 m_freem(m); 709 AH_STATINC(stat); 710 return error; 711 } 712 713 #ifdef INET6 714 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \ 715 if (saidx->dst.sa.sa_family == AF_INET6) { \ 716 error = ipsec6_common_input_cb(m, sav, skip, protoff); \ 717 } else { \ 718 error = ipsec4_common_input_cb(m, sav, skip, protoff); \ 719 } \ 720 } while (0) 721 #else 722 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \ 723 (error = ipsec4_common_input_cb(m, sav, skip, protoff)) 724 #endif 725 726 /* 727 * AH input callback from the crypto driver. 728 */ 729 static int 730 ah_input_cb(struct cryptop *crp) 731 { 732 char buf[IPSEC_ADDRSTRLEN]; 733 int rplen, ahsize, error, skip, protoff; 734 unsigned char calc[AH_ALEN_MAX]; 735 struct mbuf *m; 736 struct tdb_crypto *tc; 737 struct secasvar *sav; 738 struct secasindex *saidx; 739 uint8_t nxt; 740 char *ptr; 741 int authsize; 742 bool pool_used; 743 size_t size; 744 IPSEC_DECLARE_LOCK_VARIABLE; 745 746 KASSERT(crp->crp_opaque != NULL); 747 tc = crp->crp_opaque; 748 skip = tc->tc_skip; 749 nxt = tc->tc_nxt; 750 protoff = tc->tc_protoff; 751 m = crp->crp_buf; 752 753 IPSEC_ACQUIRE_GLOBAL_LOCKS(); 754 755 sav = tc->tc_sav; 756 saidx = &sav->sah->saidx; 757 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET || 758 saidx->dst.sa.sa_family == AF_INET6, 759 "unexpected protocol family %u", saidx->dst.sa.sa_family); 760 761 /* Figure out header size. */ 762 rplen = HDRSIZE(sav); 763 authsize = AUTHSIZE(sav); 764 ahsize = ah_hdrsiz(sav); 765 766 size = sizeof(*tc) + skip + rplen + authsize; 767 if (__predict_true(size <= ah_pool_item_size)) 768 pool_used = true; 769 else 770 pool_used = false; 771 772 /* Check for crypto errors. */ 773 if (crp->crp_etype) { 774 if (sav->tdb_cryptoid != 0) 775 sav->tdb_cryptoid = crp->crp_sid; 776 777 if (crp->crp_etype == EAGAIN) { 778 IPSEC_RELEASE_GLOBAL_LOCKS(); 779 return crypto_dispatch(crp); 780 } 781 782 AH_STATINC(AH_STAT_NOXFORM); 783 DPRINTF("crypto error %d\n", crp->crp_etype); 784 error = crp->crp_etype; 785 goto bad; 786 } else { 787 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]); 788 crypto_freereq(crp); /* No longer needed. */ 789 crp = NULL; 790 } 791 792 if (ipsec_debug) 793 memset(calc, 0, sizeof(calc)); 794 795 /* Copy authenticator off the packet. */ 796 m_copydata(m, skip + rplen, authsize, calc); 797 798 ptr = (char *)(tc + 1); 799 const uint8_t *pppp = ptr + skip + rplen; 800 801 /* Verify authenticator. */ 802 if (!consttime_memequal(pppp, calc, authsize)) { 803 DPRINTF("authentication hash mismatch " \ 804 "over %d bytes " \ 805 "for packet in SA %s/%08lx:\n" \ 806 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, " \ 807 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", 808 authsize, ipsec_address(&saidx->dst, buf, sizeof(buf)), 809 (u_long) ntohl(sav->spi), 810 calc[0], calc[1], calc[2], calc[3], 811 calc[4], calc[5], calc[6], calc[7], 812 calc[8], calc[9], calc[10], calc[11], 813 pppp[0], pppp[1], pppp[2], pppp[3], 814 pppp[4], pppp[5], pppp[6], pppp[7], 815 pppp[8], pppp[9], pppp[10], pppp[11]); 816 AH_STATINC(AH_STAT_BADAUTH); 817 error = EACCES; 818 goto bad; 819 } 820 821 /* Fix the Next Protocol field. */ 822 ptr[protoff] = nxt; 823 824 /* Copyback the saved (uncooked) network headers. */ 825 m_copyback(m, 0, skip, ptr); 826 827 if (__predict_true(pool_used)) 828 pool_cache_put(ah_tdb_crypto_pool_cache, tc); 829 else 830 kmem_intr_free(tc, size); 831 tc = NULL; 832 833 /* 834 * Header is now authenticated. 835 */ 836 m->m_flags |= M_AUTHIPHDR; 837 838 /* 839 * Update replay sequence number, if appropriate. 840 */ 841 if (sav->replay) { 842 uint32_t seq; 843 844 m_copydata(m, skip + offsetof(struct newah, ah_seq), 845 sizeof(seq), &seq); 846 if (ipsec_updatereplay(ntohl(seq), sav)) { 847 AH_STATINC(AH_STAT_REPLAY); 848 error = EACCES; 849 goto bad; 850 } 851 } 852 853 /* 854 * Remove the AH header and authenticator from the mbuf. 855 */ 856 error = m_striphdr(m, skip, ahsize); 857 if (error) { 858 DPRINTF("mangled mbuf chain for SA %s/%08lx\n", 859 ipsec_address(&saidx->dst, buf, sizeof(buf)), 860 (u_long) ntohl(sav->spi)); 861 862 AH_STATINC(AH_STAT_HDROPS); 863 goto bad; 864 } 865 866 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff); 867 868 KEY_SA_UNREF(&sav); 869 IPSEC_RELEASE_GLOBAL_LOCKS(); 870 return error; 871 872 bad: 873 if (sav) 874 KEY_SA_UNREF(&sav); 875 IPSEC_RELEASE_GLOBAL_LOCKS(); 876 if (m != NULL) 877 m_freem(m); 878 if (tc != NULL) { 879 if (pool_used) 880 pool_cache_put(ah_tdb_crypto_pool_cache, tc); 881 else 882 kmem_intr_free(tc, size); 883 } 884 if (crp != NULL) 885 crypto_freereq(crp); 886 return error; 887 } 888 889 /* 890 * AH output routine, called by ipsec[46]_process_packet(). 891 */ 892 static int 893 ah_output(struct mbuf *m, const struct ipsecrequest *isr, struct secasvar *sav, 894 int skip, int protoff, int flags) 895 { 896 char buf[IPSEC_ADDRSTRLEN]; 897 const struct auth_hash *ahx; 898 struct cryptodesc *crda; 899 struct tdb_crypto *tc; 900 struct mbuf *mi; 901 struct cryptop *crp; 902 uint16_t iplen; 903 int error, rplen, authsize, ahsize, maxpacketsize, roff; 904 uint8_t prot; 905 struct newah *ah; 906 size_t ipoffs; 907 bool pool_used; 908 909 KASSERT(sav != NULL); 910 KASSERT(sav->tdb_authalgxform != NULL); 911 ahx = sav->tdb_authalgxform; 912 913 AH_STATINC(AH_STAT_OUTPUT); 914 915 /* Figure out header size. */ 916 rplen = HDRSIZE(sav); 917 authsize = AUTHSIZE(sav); 918 ahsize = ah_hdrsiz(sav); 919 920 /* Check for maximum packet size violations. */ 921 switch (sav->sah->saidx.dst.sa.sa_family) { 922 #ifdef INET 923 case AF_INET: 924 maxpacketsize = IP_MAXPACKET; 925 ipoffs = offsetof(struct ip, ip_len); 926 break; 927 #endif 928 #ifdef INET6 929 case AF_INET6: 930 maxpacketsize = IPV6_MAXPACKET; 931 ipoffs = offsetof(struct ip6_hdr, ip6_plen); 932 break; 933 #endif 934 default: 935 DPRINTF("unknown/unsupported protocol " 936 "family %u, SA %s/%08lx\n", 937 sav->sah->saidx.dst.sa.sa_family, 938 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 939 (u_long) ntohl(sav->spi)); 940 AH_STATINC(AH_STAT_NOPF); 941 error = EPFNOSUPPORT; 942 goto bad; 943 } 944 if (ahsize + m->m_pkthdr.len > maxpacketsize) { 945 DPRINTF("packet in SA %s/%08lx got too big " 946 "(len %u, max len %u)\n", 947 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 948 (u_long) ntohl(sav->spi), 949 ahsize + m->m_pkthdr.len, maxpacketsize); 950 AH_STATINC(AH_STAT_TOOBIG); 951 error = EMSGSIZE; 952 goto bad; 953 } 954 955 /* Update the counters. */ 956 AH_STATADD(AH_STAT_OBYTES, m->m_pkthdr.len - skip); 957 958 m = m_clone(m); 959 if (m == NULL) { 960 DPRINTF("cannot clone mbuf chain, SA %s/%08lx\n", 961 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 962 (u_long) ntohl(sav->spi)); 963 AH_STATINC(AH_STAT_HDROPS); 964 error = ENOBUFS; 965 goto bad; 966 } 967 968 /* Inject AH header. */ 969 mi = m_makespace(m, skip, ahsize, &roff); 970 if (mi == NULL) { 971 DPRINTF("failed to inject %u byte AH header for SA " 972 "%s/%08lx\n", ahsize, 973 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 974 (u_long) ntohl(sav->spi)); 975 AH_STATINC(AH_STAT_HDROPS); 976 error = ENOBUFS; 977 goto bad; 978 } 979 980 /* 981 * The AH header is guaranteed by m_makespace() to be in 982 * contiguous memory, at roff bytes offset into the returned mbuf. 983 */ 984 ah = (struct newah *)(mtod(mi, char *) + roff); 985 986 /* Initialize the AH header. */ 987 m_copydata(m, protoff, sizeof(uint8_t), &ah->ah_nxt); 988 ah->ah_len = (ahsize - sizeof(struct ah)) / sizeof(uint32_t); 989 ah->ah_reserve = 0; 990 ah->ah_spi = sav->spi; 991 992 /* Zeroize authenticator. */ 993 m_copyback(m, skip + rplen, authsize, ipseczeroes); 994 995 /* Zeroize padding. */ 996 m_copyback(m, skip + rplen + authsize, ahsize - (rplen + authsize), 997 ipseczeroes); 998 999 /* Insert packet replay counter, as requested. */ 1000 if (sav->replay) { 1001 if (sav->replay->count == ~0 && 1002 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 1003 DPRINTF("replay counter wrapped for SA %s/%08lx\n", 1004 ipsec_address(&sav->sah->saidx.dst, buf, 1005 sizeof(buf)), (u_long) ntohl(sav->spi)); 1006 AH_STATINC(AH_STAT_WRAP); 1007 error = EINVAL; 1008 goto bad; 1009 } 1010 #ifdef IPSEC_DEBUG 1011 /* Emulate replay attack when ipsec_replay is TRUE. */ 1012 if (!ipsec_replay) 1013 #endif 1014 sav->replay->count++; 1015 ah->ah_seq = htonl(sav->replay->count); 1016 } 1017 1018 /* Get crypto descriptors. */ 1019 crp = crypto_getreq(1); 1020 if (crp == NULL) { 1021 DPRINTF("failed to acquire crypto descriptors\n"); 1022 AH_STATINC(AH_STAT_CRYPTO); 1023 error = ENOBUFS; 1024 goto bad; 1025 } 1026 1027 crda = crp->crp_desc; 1028 1029 crda->crd_skip = 0; 1030 crda->crd_inject = skip + rplen; 1031 crda->crd_len = m->m_pkthdr.len; 1032 1033 /* Authentication operation. */ 1034 crda->crd_alg = ahx->type; 1035 crda->crd_key = _KEYBUF(sav->key_auth); 1036 crda->crd_klen = _KEYBITS(sav->key_auth); 1037 1038 /* Allocate IPsec-specific opaque crypto info. */ 1039 size_t size = sizeof(*tc) + skip; 1040 1041 if (__predict_true(size <= ah_pool_item_size)) { 1042 tc = pool_cache_get(ah_tdb_crypto_pool_cache, PR_NOWAIT); 1043 pool_used = true; 1044 } else { 1045 /* size can exceed on IPv6 packets with large options. */ 1046 tc = kmem_intr_zalloc(size, KM_NOSLEEP); 1047 pool_used = false; 1048 } 1049 if (tc == NULL) { 1050 DPRINTF("failed to allocate tdb_crypto\n"); 1051 AH_STATINC(AH_STAT_CRYPTO); 1052 error = ENOBUFS; 1053 goto bad_crp; 1054 } 1055 1056 uint8_t *pext = (char *)(tc + 1); 1057 /* Save the skipped portion of the packet. */ 1058 m_copydata(m, 0, skip, pext); 1059 1060 /* 1061 * Fix IP header length on the header used for 1062 * authentication. We don't need to fix the original 1063 * header length as it will be fixed by our caller. 1064 */ 1065 memcpy(&iplen, pext + ipoffs, sizeof(iplen)); 1066 iplen = htons(ntohs(iplen) + ahsize); 1067 m_copyback(m, ipoffs, sizeof(iplen), &iplen); 1068 1069 /* Fix the Next Header field in saved header. */ 1070 pext[protoff] = IPPROTO_AH; 1071 1072 /* Update the Next Protocol field in the IP header. */ 1073 prot = IPPROTO_AH; 1074 m_copyback(m, protoff, sizeof(prot), &prot); 1075 1076 /* "Massage" the packet headers for crypto processing. */ 1077 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 1078 skip, ahx->type, 1); 1079 if (error != 0) { 1080 m = NULL; /* mbuf was free'd by ah_massage_headers. */ 1081 goto bad_tc; 1082 } 1083 1084 { 1085 int s = pserialize_read_enter(); 1086 1087 /* 1088 * Take another reference to the SP and the SA for opencrypto callback. 1089 */ 1090 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD || 1091 sav->state == SADB_SASTATE_DEAD)) { 1092 pserialize_read_exit(s); 1093 AH_STATINC(AH_STAT_NOTDB); 1094 error = ENOENT; 1095 goto bad_tc; 1096 } 1097 KEY_SP_REF(isr->sp); 1098 KEY_SA_REF(sav); 1099 pserialize_read_exit(s); 1100 } 1101 1102 /* Crypto operation descriptor. */ 1103 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 1104 crp->crp_flags = CRYPTO_F_IMBUF; 1105 crp->crp_buf = m; 1106 crp->crp_callback = ah_output_cb; 1107 crp->crp_sid = sav->tdb_cryptoid; 1108 crp->crp_opaque = tc; 1109 1110 /* These are passed as-is to the callback. */ 1111 tc->tc_isr = isr; 1112 tc->tc_spi = sav->spi; 1113 tc->tc_dst = sav->sah->saidx.dst; 1114 tc->tc_proto = sav->sah->saidx.proto; 1115 tc->tc_skip = skip; 1116 tc->tc_protoff = protoff; 1117 tc->tc_flags = flags; 1118 tc->tc_sav = sav; 1119 1120 return crypto_dispatch(crp); 1121 1122 bad_tc: 1123 if (__predict_true(pool_used)) 1124 pool_cache_put(ah_tdb_crypto_pool_cache, tc); 1125 else 1126 kmem_intr_free(tc, size); 1127 bad_crp: 1128 crypto_freereq(crp); 1129 bad: 1130 if (m) 1131 m_freem(m); 1132 return error; 1133 } 1134 1135 /* 1136 * AH output callback from the crypto driver. 1137 */ 1138 static int 1139 ah_output_cb(struct cryptop *crp) 1140 { 1141 int skip, error; 1142 struct tdb_crypto *tc; 1143 const struct ipsecrequest *isr; 1144 struct secasvar *sav; 1145 struct mbuf *m; 1146 void *ptr; 1147 int err, flags; 1148 size_t size; 1149 bool pool_used; 1150 IPSEC_DECLARE_LOCK_VARIABLE; 1151 1152 KASSERT(crp->crp_opaque != NULL); 1153 tc = crp->crp_opaque; 1154 skip = tc->tc_skip; 1155 ptr = (tc + 1); 1156 m = crp->crp_buf; 1157 size = sizeof(*tc) + skip; 1158 pool_used = size <= ah_pool_item_size; 1159 1160 IPSEC_ACQUIRE_GLOBAL_LOCKS(); 1161 1162 isr = tc->tc_isr; 1163 sav = tc->tc_sav; 1164 1165 /* Check for crypto errors. */ 1166 if (crp->crp_etype) { 1167 if (sav->tdb_cryptoid != 0) 1168 sav->tdb_cryptoid = crp->crp_sid; 1169 1170 if (crp->crp_etype == EAGAIN) { 1171 IPSEC_RELEASE_GLOBAL_LOCKS(); 1172 return crypto_dispatch(crp); 1173 } 1174 1175 AH_STATINC(AH_STAT_NOXFORM); 1176 DPRINTF("crypto error %d\n", crp->crp_etype); 1177 error = crp->crp_etype; 1178 goto bad; 1179 } 1180 1181 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]); 1182 1183 /* 1184 * Copy original headers (with the new protocol number) back 1185 * in place. 1186 */ 1187 m_copyback(m, 0, skip, ptr); 1188 1189 flags = tc->tc_flags; 1190 /* No longer needed. */ 1191 if (__predict_true(pool_used)) 1192 pool_cache_put(ah_tdb_crypto_pool_cache, tc); 1193 else 1194 kmem_intr_free(tc, size); 1195 crypto_freereq(crp); 1196 1197 #ifdef IPSEC_DEBUG 1198 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */ 1199 if (ipsec_integrity) { 1200 int alen; 1201 1202 /* 1203 * Corrupt HMAC if we want to test integrity verification of 1204 * the other side. 1205 */ 1206 alen = AUTHSIZE(sav); 1207 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes); 1208 } 1209 #endif 1210 1211 /* NB: m is reclaimed by ipsec_process_done. */ 1212 err = ipsec_process_done(m, isr, sav, flags); 1213 KEY_SA_UNREF(&sav); 1214 KEY_SP_UNREF(&isr->sp); 1215 IPSEC_RELEASE_GLOBAL_LOCKS(); 1216 return err; 1217 bad: 1218 if (sav) 1219 KEY_SA_UNREF(&sav); 1220 KEY_SP_UNREF(&isr->sp); 1221 IPSEC_RELEASE_GLOBAL_LOCKS(); 1222 if (m) 1223 m_freem(m); 1224 if (__predict_true(pool_used)) 1225 pool_cache_put(ah_tdb_crypto_pool_cache, tc); 1226 else 1227 kmem_intr_free(tc, size); 1228 crypto_freereq(crp); 1229 return error; 1230 } 1231 1232 static struct xformsw ah_xformsw = { 1233 .xf_type = XF_AH, 1234 .xf_flags = XFT_AUTH, 1235 .xf_name = "IPsec AH", 1236 .xf_init = ah_init, 1237 .xf_zeroize = ah_zeroize, 1238 .xf_input = ah_input, 1239 .xf_output = ah_output, 1240 .xf_next = NULL, 1241 }; 1242 1243 void 1244 ah_attach(void) 1245 { 1246 ahstat_percpu = percpu_alloc(sizeof(uint64_t) * AH_NSTATS); 1247 1248 #define MAXAUTHSIZE(name) \ 1249 if ((auth_hash_ ## name).authsize > ah_max_authsize) \ 1250 ah_max_authsize = (auth_hash_ ## name).authsize 1251 1252 ah_max_authsize = 0; 1253 MAXAUTHSIZE(null); 1254 MAXAUTHSIZE(md5); 1255 MAXAUTHSIZE(sha1); 1256 MAXAUTHSIZE(key_md5); 1257 MAXAUTHSIZE(key_sha1); 1258 MAXAUTHSIZE(hmac_md5); 1259 MAXAUTHSIZE(hmac_sha1); 1260 MAXAUTHSIZE(hmac_ripemd_160); 1261 MAXAUTHSIZE(hmac_md5_96); 1262 MAXAUTHSIZE(hmac_sha1_96); 1263 MAXAUTHSIZE(hmac_ripemd_160_96); 1264 MAXAUTHSIZE(hmac_sha2_256); 1265 MAXAUTHSIZE(hmac_sha2_384); 1266 MAXAUTHSIZE(hmac_sha2_512); 1267 MAXAUTHSIZE(aes_xcbc_mac_96); 1268 MAXAUTHSIZE(gmac_aes_128); 1269 MAXAUTHSIZE(gmac_aes_192); 1270 MAXAUTHSIZE(gmac_aes_256); 1271 IPSECLOG(LOG_DEBUG, "ah_max_authsize=%d\n", ah_max_authsize); 1272 1273 #undef MAXAUTHSIZE 1274 1275 ah_pool_item_size = sizeof(struct tdb_crypto) + 1276 sizeof(struct ip) + MAX_IPOPTLEN + 1277 sizeof(struct ah) + sizeof(uint32_t) + ah_max_authsize; 1278 ah_tdb_crypto_pool_cache = pool_cache_init(ah_pool_item_size, 1279 coherency_unit, 0, 0, "ah_tdb_crypto", NULL, IPL_SOFTNET, 1280 NULL, NULL, NULL); 1281 1282 xform_register(&ah_xformsw); 1283 } 1284