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