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