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