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