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