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