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