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