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_128_NIST_GMAC: 132 case CRYPTO_AES_192_NIST_GMAC: 133 case CRYPTO_AES_256_NIST_GMAC: 134 alen = esph->hashsize; 135 break; 136 137 default: 138 alen = AH_HMAC_HASHLEN; 139 break; 140 } 141 142 return alen; 143 } 144 145 size_t 146 ah_hdrsiz(struct secasvar *sav) 147 { 148 size_t size; 149 150 if (sav != NULL) { 151 int authsize, rplen, align; 152 153 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform")); 154 /*XXX not right for null algorithm--does it matter??*/ 155 156 /* RFC4302: use the correct alignment. */ 157 align = sizeof(uint32_t); 158 #ifdef INET6 159 if (sav->sah->saidx.dst.sa.sa_family == AF_INET6) { 160 align = sizeof(uint64_t); 161 } 162 #endif 163 rplen = HDRSIZE(sav); 164 authsize = AUTHSIZE(sav); 165 size = roundup(rplen + authsize, align); 166 } else { 167 /* default guess */ 168 size = sizeof (struct ah) + sizeof (u_int32_t) + 16; 169 } 170 return size; 171 } 172 173 /* 174 * NB: public for use by esp_init. 175 */ 176 int 177 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria) 178 { 179 const struct auth_hash *thash; 180 int keylen; 181 182 thash = auth_algorithm_lookup(sav->alg_auth); 183 if (thash == NULL) { 184 DPRINTF(("%s: unsupported authentication algorithm %u\n", 185 __func__, sav->alg_auth)); 186 return EINVAL; 187 } 188 189 switch (sav->alg_auth) { 190 case SADB_AALG_MD5HMAC: 191 if (ratecheck(&md5warn, &ipsec_warn_interval)) 192 gone_in(13, "MD5-HMAC authenticator for IPsec"); 193 break; 194 case SADB_X_AALG_RIPEMD160HMAC: 195 if (ratecheck(&ripewarn, &ipsec_warn_interval)) 196 gone_in(13, "RIPEMD160-HMAC authenticator for IPsec"); 197 break; 198 case SADB_X_AALG_MD5: 199 if (ratecheck(&kpdkmd5warn, &ipsec_warn_interval)) 200 gone_in(13, "Keyed-MD5 authenticator for IPsec"); 201 break; 202 case SADB_X_AALG_SHA: 203 if (ratecheck(&kpdksha1warn, &ipsec_warn_interval)) 204 gone_in(13, "Keyed-SHA1 authenticator for IPsec"); 205 break; 206 } 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(("%s: replay state block inconsistency, " 216 "%s algorithm %s replay state\n", __func__, 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(("%s: no authentication key for %s algorithm\n", 223 __func__, thash->name)); 224 return EINVAL; 225 } 226 keylen = _KEYLEN(sav->key_auth); 227 if (keylen > thash->keysize && thash->keysize != 0) { 228 DPRINTF(("%s: invalid keylength %d, algorithm %s requires " 229 "keysize less than %d\n", __func__, 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 bzero(cria, sizeof (*cria)); 239 cria->cri_alg = sav->tdb_authalgxform->type; 240 cria->cri_klen = _KEYBITS(sav->key_auth); 241 cria->cri_key = sav->key_auth->key_data; 242 cria->cri_mlen = AUTHSIZE(sav); 243 244 return 0; 245 } 246 247 /* 248 * ah_init() is called when an SPI is being set up. 249 */ 250 static int 251 ah_init(struct secasvar *sav, struct xformsw *xsp) 252 { 253 struct cryptoini cria; 254 int error; 255 256 error = ah_init0(sav, xsp, &cria); 257 return error ? error : 258 crypto_newsession(&sav->tdb_cryptoid, &cria, 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 cryptodesc *crda; 564 struct cryptop *crp; 565 struct xform_data *xd; 566 struct newah *ah; 567 crypto_session_t cryptoid; 568 int hl, rplen, authsize, ahsize, error; 569 570 IPSEC_ASSERT(sav != NULL, ("null SA")); 571 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key")); 572 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, 573 ("null authentication xform")); 574 575 /* Figure out header size. */ 576 rplen = HDRSIZE(sav); 577 578 if (m->m_len < skip + rplen) { 579 m = m_pullup(m, skip + rplen); 580 if (m == NULL) { 581 DPRINTF(("ah_input: cannot pullup header\n")); 582 AHSTAT_INC(ahs_hdrops); /*XXX*/ 583 error = ENOBUFS; 584 goto bad; 585 } 586 } 587 ah = (struct newah *)(mtod(m, caddr_t) + skip); 588 589 /* Check replay window, if applicable. */ 590 SECASVAR_LOCK(sav); 591 if (sav->replay != NULL && sav->replay->wsize != 0 && 592 ipsec_chkreplay(ntohl(ah->ah_seq), sav) == 0) { 593 SECASVAR_UNLOCK(sav); 594 AHSTAT_INC(ahs_replay); 595 DPRINTF(("%s: packet replay failure: %s\n", __func__, 596 ipsec_sa2str(sav, buf, sizeof(buf)))); 597 error = EACCES; 598 goto bad; 599 } 600 cryptoid = sav->tdb_cryptoid; 601 SECASVAR_UNLOCK(sav); 602 603 /* Verify AH header length. */ 604 hl = sizeof(struct ah) + (ah->ah_len * sizeof (u_int32_t)); 605 ahx = sav->tdb_authalgxform; 606 authsize = AUTHSIZE(sav); 607 ahsize = ah_hdrsiz(sav); 608 if (hl != ahsize) { 609 DPRINTF(("%s: bad authenticator length %u (expecting %lu)" 610 " for packet in SA %s/%08lx\n", __func__, hl, 611 (u_long)ahsize, 612 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 613 (u_long) ntohl(sav->spi))); 614 AHSTAT_INC(ahs_badauthl); 615 error = EACCES; 616 goto bad; 617 } 618 if (skip + ahsize > m->m_pkthdr.len) { 619 DPRINTF(("%s: bad mbuf length %u (expecting %lu)" 620 " for packet in SA %s/%08lx\n", __func__, 621 m->m_pkthdr.len, (u_long)(skip + ahsize), 622 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 623 (u_long) ntohl(sav->spi))); 624 AHSTAT_INC(ahs_badauthl); 625 error = EACCES; 626 goto bad; 627 } 628 AHSTAT_ADD(ahs_ibytes, m->m_pkthdr.len - skip - hl); 629 630 /* Get crypto descriptors. */ 631 crp = crypto_getreq(1); 632 if (crp == NULL) { 633 DPRINTF(("%s: failed to acquire crypto descriptor\n", 634 __func__)); 635 AHSTAT_INC(ahs_crypto); 636 error = ENOBUFS; 637 goto bad; 638 } 639 640 crda = crp->crp_desc; 641 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor")); 642 643 crda->crd_skip = 0; 644 crda->crd_len = m->m_pkthdr.len; 645 crda->crd_inject = skip + rplen; 646 647 /* Authentication operation. */ 648 crda->crd_alg = ahx->type; 649 crda->crd_klen = _KEYBITS(sav->key_auth); 650 crda->crd_key = sav->key_auth->key_data; 651 652 /* Allocate IPsec-specific opaque crypto info. */ 653 xd = malloc(sizeof(*xd) + skip + rplen + authsize, M_XDATA, 654 M_NOWAIT | M_ZERO); 655 if (xd == NULL) { 656 DPRINTF(("%s: failed to allocate xform_data\n", __func__)); 657 AHSTAT_INC(ahs_crypto); 658 crypto_freereq(crp); 659 error = ENOBUFS; 660 goto bad; 661 } 662 663 /* 664 * Save the authenticator, the skipped portion of the packet, 665 * and the AH header. 666 */ 667 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(xd + 1)); 668 669 /* Zeroize the authenticator on the packet. */ 670 m_copyback(m, skip + rplen, authsize, ipseczeroes); 671 672 /* Save ah_nxt, since ah pointer can become invalid after "massage" */ 673 hl = ah->ah_nxt; 674 675 /* "Massage" the packet headers for crypto processing. */ 676 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 677 skip, ahx->type, 0); 678 if (error != 0) { 679 /* NB: mbuf is free'd by ah_massage_headers */ 680 AHSTAT_INC(ahs_hdrops); 681 free(xd, M_XDATA); 682 crypto_freereq(crp); 683 key_freesav(&sav); 684 return (error); 685 } 686 687 /* Crypto operation descriptor. */ 688 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 689 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 690 if (V_async_crypto) 691 crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER; 692 crp->crp_buf = (caddr_t) m; 693 crp->crp_callback = ah_input_cb; 694 crp->crp_session = cryptoid; 695 crp->crp_opaque = (caddr_t) xd; 696 697 /* These are passed as-is to the callback. */ 698 xd->sav = sav; 699 xd->nxt = hl; 700 xd->protoff = protoff; 701 xd->skip = skip; 702 xd->cryptoid = cryptoid; 703 xd->vnet = curvnet; 704 return (crypto_dispatch(crp)); 705 bad: 706 m_freem(m); 707 key_freesav(&sav); 708 return (error); 709 } 710 711 /* 712 * AH input callback from the crypto driver. 713 */ 714 static int 715 ah_input_cb(struct cryptop *crp) 716 { 717 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]); 718 unsigned char calc[AH_ALEN_MAX]; 719 struct mbuf *m; 720 struct xform_data *xd; 721 struct secasvar *sav; 722 struct secasindex *saidx; 723 caddr_t ptr; 724 crypto_session_t cryptoid; 725 int authsize, rplen, ahsize, error, skip, protoff; 726 uint8_t nxt; 727 728 m = (struct mbuf *) crp->crp_buf; 729 xd = (struct xform_data *) crp->crp_opaque; 730 CURVNET_SET(xd->vnet); 731 sav = xd->sav; 732 skip = xd->skip; 733 nxt = xd->nxt; 734 protoff = xd->protoff; 735 cryptoid = xd->cryptoid; 736 saidx = &sav->sah->saidx; 737 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 738 saidx->dst.sa.sa_family == AF_INET6, 739 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 740 741 /* Check for crypto errors. */ 742 if (crp->crp_etype) { 743 if (crp->crp_etype == EAGAIN) { 744 /* Reset the session ID */ 745 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0) 746 crypto_freesession(cryptoid); 747 xd->cryptoid = crp->crp_session; 748 CURVNET_RESTORE(); 749 return (crypto_dispatch(crp)); 750 } 751 AHSTAT_INC(ahs_noxform); 752 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 753 error = crp->crp_etype; 754 goto bad; 755 } else { 756 AHSTAT_INC(ahs_hist[sav->alg_auth]); 757 crypto_freereq(crp); /* No longer needed. */ 758 crp = NULL; 759 } 760 761 /* Shouldn't happen... */ 762 if (m == NULL) { 763 AHSTAT_INC(ahs_crypto); 764 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 765 error = EINVAL; 766 goto bad; 767 } 768 769 /* Figure out header size. */ 770 rplen = HDRSIZE(sav); 771 authsize = AUTHSIZE(sav); 772 ahsize = ah_hdrsiz(sav); 773 774 /* Copy authenticator off the packet. */ 775 m_copydata(m, skip + rplen, authsize, calc); 776 777 /* Verify authenticator. */ 778 ptr = (caddr_t) (xd + 1); 779 if (timingsafe_bcmp(ptr + skip + rplen, calc, authsize)) { 780 DPRINTF(("%s: authentication hash mismatch for packet " 781 "in SA %s/%08lx\n", __func__, 782 ipsec_address(&saidx->dst, buf, sizeof(buf)), 783 (u_long) ntohl(sav->spi))); 784 AHSTAT_INC(ahs_badauth); 785 error = EACCES; 786 goto bad; 787 } 788 /* Fix the Next Protocol field. */ 789 ((uint8_t *) ptr)[protoff] = nxt; 790 791 /* Copyback the saved (uncooked) network headers. */ 792 m_copyback(m, 0, skip, ptr); 793 free(xd, M_XDATA), xd = NULL; /* No longer needed */ 794 795 /* 796 * Header is now authenticated. 797 */ 798 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM; 799 800 /* 801 * Update replay sequence number, if appropriate. 802 */ 803 if (sav->replay) { 804 u_int32_t seq; 805 806 m_copydata(m, skip + offsetof(struct newah, ah_seq), 807 sizeof (seq), (caddr_t) &seq); 808 SECASVAR_LOCK(sav); 809 if (ipsec_updatereplay(ntohl(seq), sav)) { 810 SECASVAR_UNLOCK(sav); 811 AHSTAT_INC(ahs_replay); 812 error = EACCES; 813 goto bad; 814 } 815 SECASVAR_UNLOCK(sav); 816 } 817 818 /* 819 * Remove the AH header and authenticator from the mbuf. 820 */ 821 error = m_striphdr(m, skip, ahsize); 822 if (error) { 823 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__, 824 ipsec_address(&saidx->dst, buf, sizeof(buf)), 825 (u_long) ntohl(sav->spi))); 826 AHSTAT_INC(ahs_hdrops); 827 goto bad; 828 } 829 830 switch (saidx->dst.sa.sa_family) { 831 #ifdef INET6 832 case AF_INET6: 833 error = ipsec6_common_input_cb(m, sav, skip, protoff); 834 break; 835 #endif 836 #ifdef INET 837 case AF_INET: 838 error = ipsec4_common_input_cb(m, sav, skip, protoff); 839 break; 840 #endif 841 default: 842 panic("%s: Unexpected address family: %d saidx=%p", __func__, 843 saidx->dst.sa.sa_family, saidx); 844 } 845 CURVNET_RESTORE(); 846 return error; 847 bad: 848 CURVNET_RESTORE(); 849 if (sav) 850 key_freesav(&sav); 851 if (m != NULL) 852 m_freem(m); 853 if (xd != NULL) 854 free(xd, M_XDATA); 855 if (crp != NULL) 856 crypto_freereq(crp); 857 return error; 858 } 859 860 /* 861 * AH output routine, called by ipsec[46]_perform_request(). 862 */ 863 static int 864 ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav, 865 u_int idx, int skip, int protoff) 866 { 867 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]); 868 const struct auth_hash *ahx; 869 struct cryptodesc *crda; 870 struct xform_data *xd; 871 struct mbuf *mi; 872 struct cryptop *crp; 873 struct newah *ah; 874 crypto_session_t cryptoid; 875 uint16_t iplen; 876 int error, rplen, authsize, ahsize, maxpacketsize, roff; 877 uint8_t prot; 878 879 IPSEC_ASSERT(sav != NULL, ("null SA")); 880 ahx = sav->tdb_authalgxform; 881 IPSEC_ASSERT(ahx != NULL, ("null authentication xform")); 882 883 AHSTAT_INC(ahs_output); 884 885 /* Figure out header size. */ 886 rplen = HDRSIZE(sav); 887 authsize = AUTHSIZE(sav); 888 ahsize = ah_hdrsiz(sav); 889 890 /* Check for maximum packet size violations. */ 891 switch (sav->sah->saidx.dst.sa.sa_family) { 892 #ifdef INET 893 case AF_INET: 894 maxpacketsize = IP_MAXPACKET; 895 break; 896 #endif /* INET */ 897 #ifdef INET6 898 case AF_INET6: 899 maxpacketsize = IPV6_MAXPACKET; 900 break; 901 #endif /* INET6 */ 902 default: 903 DPRINTF(("%s: unknown/unsupported protocol family %u, " 904 "SA %s/%08lx\n", __func__, 905 sav->sah->saidx.dst.sa.sa_family, 906 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 907 (u_long) ntohl(sav->spi))); 908 AHSTAT_INC(ahs_nopf); 909 error = EPFNOSUPPORT; 910 goto bad; 911 } 912 if (ahsize + m->m_pkthdr.len > maxpacketsize) { 913 DPRINTF(("%s: packet in SA %s/%08lx got too big " 914 "(len %u, max len %u)\n", __func__, 915 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 916 (u_long) ntohl(sav->spi), 917 ahsize + m->m_pkthdr.len, maxpacketsize)); 918 AHSTAT_INC(ahs_toobig); 919 error = EMSGSIZE; 920 goto bad; 921 } 922 923 /* Update the counters. */ 924 AHSTAT_ADD(ahs_obytes, m->m_pkthdr.len - skip); 925 926 m = m_unshare(m, M_NOWAIT); 927 if (m == NULL) { 928 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__, 929 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 930 (u_long) ntohl(sav->spi))); 931 AHSTAT_INC(ahs_hdrops); 932 error = ENOBUFS; 933 goto bad; 934 } 935 936 /* Inject AH header. */ 937 mi = m_makespace(m, skip, ahsize, &roff); 938 if (mi == NULL) { 939 DPRINTF(("%s: failed to inject %u byte AH header for SA " 940 "%s/%08lx\n", __func__, ahsize, 941 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 942 (u_long) ntohl(sav->spi))); 943 AHSTAT_INC(ahs_hdrops); /*XXX differs from openbsd */ 944 error = ENOBUFS; 945 goto bad; 946 } 947 948 /* 949 * The AH header is guaranteed by m_makespace() to be in 950 * contiguous memory, at roff bytes offset into the returned mbuf. 951 */ 952 ah = (struct newah *)(mtod(mi, caddr_t) + roff); 953 954 /* Initialize the AH header. */ 955 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt); 956 ah->ah_len = (ahsize - sizeof(struct ah)) / sizeof(u_int32_t); 957 ah->ah_reserve = 0; 958 ah->ah_spi = sav->spi; 959 960 /* Zeroize authenticator. */ 961 m_copyback(m, skip + rplen, authsize, ipseczeroes); 962 963 /* Zeroize padding */ 964 m_copyback(m, skip + rplen + authsize, ahsize - (rplen + authsize), 965 ipseczeroes); 966 967 /* Insert packet replay counter, as requested. */ 968 SECASVAR_LOCK(sav); 969 if (sav->replay) { 970 if (sav->replay->count == ~0 && 971 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 972 SECASVAR_UNLOCK(sav); 973 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n", 974 __func__, ipsec_address(&sav->sah->saidx.dst, buf, 975 sizeof(buf)), (u_long) ntohl(sav->spi))); 976 AHSTAT_INC(ahs_wrap); 977 error = EACCES; 978 goto bad; 979 } 980 #ifdef REGRESSION 981 /* Emulate replay attack when ipsec_replay is TRUE. */ 982 if (!V_ipsec_replay) 983 #endif 984 sav->replay->count++; 985 ah->ah_seq = htonl(sav->replay->count); 986 } 987 cryptoid = sav->tdb_cryptoid; 988 SECASVAR_UNLOCK(sav); 989 990 /* Get crypto descriptors. */ 991 crp = crypto_getreq(1); 992 if (crp == NULL) { 993 DPRINTF(("%s: failed to acquire crypto descriptors\n", 994 __func__)); 995 AHSTAT_INC(ahs_crypto); 996 error = ENOBUFS; 997 goto bad; 998 } 999 1000 crda = crp->crp_desc; 1001 crda->crd_skip = 0; 1002 crda->crd_inject = skip + rplen; 1003 crda->crd_len = m->m_pkthdr.len; 1004 1005 /* Authentication operation. */ 1006 crda->crd_alg = ahx->type; 1007 crda->crd_key = sav->key_auth->key_data; 1008 crda->crd_klen = _KEYBITS(sav->key_auth); 1009 1010 /* Allocate IPsec-specific opaque crypto info. */ 1011 xd = malloc(sizeof(struct xform_data) + skip, M_XDATA, 1012 M_NOWAIT | M_ZERO); 1013 if (xd == NULL) { 1014 crypto_freereq(crp); 1015 DPRINTF(("%s: failed to allocate xform_data\n", __func__)); 1016 AHSTAT_INC(ahs_crypto); 1017 error = ENOBUFS; 1018 goto bad; 1019 } 1020 1021 /* Save the skipped portion of the packet. */ 1022 m_copydata(m, 0, skip, (caddr_t) (xd + 1)); 1023 1024 /* 1025 * Fix IP header length on the header used for 1026 * authentication. We don't need to fix the original 1027 * header length as it will be fixed by our caller. 1028 */ 1029 switch (sav->sah->saidx.dst.sa.sa_family) { 1030 #ifdef INET 1031 case AF_INET: 1032 bcopy(((caddr_t)(xd + 1)) + 1033 offsetof(struct ip, ip_len), 1034 (caddr_t) &iplen, sizeof(u_int16_t)); 1035 iplen = htons(ntohs(iplen) + ahsize); 1036 m_copyback(m, offsetof(struct ip, ip_len), 1037 sizeof(u_int16_t), (caddr_t) &iplen); 1038 break; 1039 #endif /* INET */ 1040 1041 #ifdef INET6 1042 case AF_INET6: 1043 bcopy(((caddr_t)(xd + 1)) + 1044 offsetof(struct ip6_hdr, ip6_plen), 1045 (caddr_t) &iplen, sizeof(uint16_t)); 1046 iplen = htons(ntohs(iplen) + ahsize); 1047 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen), 1048 sizeof(uint16_t), (caddr_t) &iplen); 1049 break; 1050 #endif /* INET6 */ 1051 } 1052 1053 /* Fix the Next Header field in saved header. */ 1054 ((uint8_t *) (xd + 1))[protoff] = IPPROTO_AH; 1055 1056 /* Update the Next Protocol field in the IP header. */ 1057 prot = IPPROTO_AH; 1058 m_copyback(m, protoff, sizeof(uint8_t), (caddr_t) &prot); 1059 1060 /* "Massage" the packet headers for crypto processing. */ 1061 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 1062 skip, ahx->type, 1); 1063 if (error != 0) { 1064 m = NULL; /* mbuf was free'd by ah_massage_headers. */ 1065 free(xd, M_XDATA); 1066 crypto_freereq(crp); 1067 goto bad; 1068 } 1069 1070 /* Crypto operation descriptor. */ 1071 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 1072 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 1073 if (V_async_crypto) 1074 crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER; 1075 crp->crp_buf = (caddr_t) m; 1076 crp->crp_callback = ah_output_cb; 1077 crp->crp_session = cryptoid; 1078 crp->crp_opaque = (caddr_t) xd; 1079 1080 /* These are passed as-is to the callback. */ 1081 xd->sp = sp; 1082 xd->sav = sav; 1083 xd->skip = skip; 1084 xd->idx = idx; 1085 xd->cryptoid = cryptoid; 1086 xd->vnet = curvnet; 1087 1088 return crypto_dispatch(crp); 1089 bad: 1090 if (m) 1091 m_freem(m); 1092 key_freesav(&sav); 1093 key_freesp(&sp); 1094 return (error); 1095 } 1096 1097 /* 1098 * AH output callback from the crypto driver. 1099 */ 1100 static int 1101 ah_output_cb(struct cryptop *crp) 1102 { 1103 struct xform_data *xd; 1104 struct secpolicy *sp; 1105 struct secasvar *sav; 1106 struct mbuf *m; 1107 crypto_session_t cryptoid; 1108 caddr_t ptr; 1109 u_int idx; 1110 int skip, error; 1111 1112 m = (struct mbuf *) crp->crp_buf; 1113 xd = (struct xform_data *) crp->crp_opaque; 1114 CURVNET_SET(xd->vnet); 1115 sp = xd->sp; 1116 sav = xd->sav; 1117 skip = xd->skip; 1118 idx = xd->idx; 1119 cryptoid = xd->cryptoid; 1120 ptr = (caddr_t) (xd + 1); 1121 1122 /* Check for crypto errors. */ 1123 if (crp->crp_etype) { 1124 if (crp->crp_etype == EAGAIN) { 1125 /* Reset the session ID */ 1126 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0) 1127 crypto_freesession(cryptoid); 1128 xd->cryptoid = crp->crp_session; 1129 CURVNET_RESTORE(); 1130 return (crypto_dispatch(crp)); 1131 } 1132 AHSTAT_INC(ahs_noxform); 1133 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 1134 error = crp->crp_etype; 1135 m_freem(m); 1136 goto bad; 1137 } 1138 1139 /* Shouldn't happen... */ 1140 if (m == NULL) { 1141 AHSTAT_INC(ahs_crypto); 1142 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 1143 error = EINVAL; 1144 goto bad; 1145 } 1146 /* 1147 * Copy original headers (with the new protocol number) back 1148 * in place. 1149 */ 1150 m_copyback(m, 0, skip, ptr); 1151 1152 free(xd, M_XDATA); 1153 crypto_freereq(crp); 1154 AHSTAT_INC(ahs_hist[sav->alg_auth]); 1155 #ifdef REGRESSION 1156 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */ 1157 if (V_ipsec_integrity) { 1158 int alen; 1159 1160 /* 1161 * Corrupt HMAC if we want to test integrity verification of 1162 * the other side. 1163 */ 1164 alen = AUTHSIZE(sav); 1165 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes); 1166 } 1167 #endif 1168 1169 /* NB: m is reclaimed by ipsec_process_done. */ 1170 error = ipsec_process_done(m, sp, sav, idx); 1171 CURVNET_RESTORE(); 1172 return (error); 1173 bad: 1174 CURVNET_RESTORE(); 1175 free(xd, M_XDATA); 1176 crypto_freereq(crp); 1177 key_freesav(&sav); 1178 key_freesp(&sp); 1179 return (error); 1180 } 1181 1182 static struct xformsw ah_xformsw = { 1183 .xf_type = XF_AH, 1184 .xf_name = "IPsec AH", 1185 .xf_init = ah_init, 1186 .xf_zeroize = ah_zeroize, 1187 .xf_input = ah_input, 1188 .xf_output = ah_output, 1189 }; 1190 1191 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 1192 xform_attach, &ah_xformsw); 1193 SYSUNINIT(ah_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 1194 xform_detach, &ah_xformsw); 1195