Home | History | Annotate | Line # | Download | only in netipsec
xform_esp.c revision 1.1
      1  1.1  jonathan /*	$NetBSD: xform_esp.c,v 1.1 2003/08/13 20:06:51 jonathan Exp $	*/
      2  1.1  jonathan /*	$FreeBSD: src/sys/netipsec/xform_esp.c,v 1.2.2.1 2003/01/24 05:11:36 sam Exp $	*/
      3  1.1  jonathan /*	$OpenBSD: ip_esp.c,v 1.69 2001/06/26 06:18:59 angelos Exp $ */
      4  1.1  jonathan 
      5  1.1  jonathan /*
      6  1.1  jonathan  * The authors of this code are John Ioannidis (ji (at) tla.org),
      7  1.1  jonathan  * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
      8  1.1  jonathan  * Niels Provos (provos (at) physnet.uni-hamburg.de).
      9  1.1  jonathan  *
     10  1.1  jonathan  * The original version of this code was written by John Ioannidis
     11  1.1  jonathan  * for BSD/OS in Athens, Greece, in November 1995.
     12  1.1  jonathan  *
     13  1.1  jonathan  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
     14  1.1  jonathan  * by Angelos D. Keromytis.
     15  1.1  jonathan  *
     16  1.1  jonathan  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
     17  1.1  jonathan  * and Niels Provos.
     18  1.1  jonathan  *
     19  1.1  jonathan  * Additional features in 1999 by Angelos D. Keromytis.
     20  1.1  jonathan  *
     21  1.1  jonathan  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
     22  1.1  jonathan  * Angelos D. Keromytis and Niels Provos.
     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.1  jonathan __KERNEL_RCSID(0, "$NetBSD: xform_esp.c,v 1.1 2003/08/13 20:06:51 jonathan Exp $");
     43  1.1  jonathan 
     44  1.1  jonathan #include "opt_inet.h"
     45  1.1  jonathan #include "opt_inet6.h"
     46  1.1  jonathan 
     47  1.1  jonathan #include <sys/param.h>
     48  1.1  jonathan #include <sys/systm.h>
     49  1.1  jonathan #include <sys/mbuf.h>
     50  1.1  jonathan #include <sys/socket.h>
     51  1.1  jonathan #include <sys/syslog.h>
     52  1.1  jonathan #include <sys/kernel.h>
     53  1.1  jonathan /*#include <sys/random.h>*/
     54  1.1  jonathan #include <sys/sysctl.h>
     55  1.1  jonathan 
     56  1.1  jonathan #include <net/if.h>
     57  1.1  jonathan 
     58  1.1  jonathan #include <netinet/in.h>
     59  1.1  jonathan #include <netinet/in_systm.h>
     60  1.1  jonathan #include <netinet/ip.h>
     61  1.1  jonathan #include <netinet/ip_ecn.h>
     62  1.1  jonathan #include <netinet/ip6.h>
     63  1.1  jonathan 
     64  1.1  jonathan #include <net/route.h>
     65  1.1  jonathan #include <netipsec/ipsec.h>
     66  1.1  jonathan #include <netipsec/ah.h>
     67  1.1  jonathan #include <netipsec/ah_var.h>
     68  1.1  jonathan #include <netipsec/esp.h>
     69  1.1  jonathan #include <netipsec/esp_var.h>
     70  1.1  jonathan #include <netipsec/xform.h>
     71  1.1  jonathan 
     72  1.1  jonathan #ifdef INET6
     73  1.1  jonathan #include <netinet6/ip6_var.h>
     74  1.1  jonathan #include <netipsec/ipsec6.h>
     75  1.1  jonathan #include <netinet6/ip6_ecn.h>
     76  1.1  jonathan #endif
     77  1.1  jonathan 
     78  1.1  jonathan #include <netipsec/key.h>
     79  1.1  jonathan #include <netipsec/key_debug.h>
     80  1.1  jonathan 
     81  1.1  jonathan #include <netipsec/ipsec_osdep.h>
     82  1.1  jonathan 
     83  1.1  jonathan #include <opencrypto/cryptodev.h>
     84  1.1  jonathan #include <opencrypto/xform.h>
     85  1.1  jonathan 
     86  1.1  jonathan int	esp_enable = 1;
     87  1.1  jonathan struct	espstat espstat;
     88  1.1  jonathan 
     89  1.1  jonathan #ifdef __FreeBSD__
     90  1.1  jonathan SYSCTL_DECL(_net_inet_esp);
     91  1.1  jonathan SYSCTL_INT(_net_inet_esp, OID_AUTO,
     92  1.1  jonathan 	esp_enable,	CTLFLAG_RW,	&esp_enable,	0, "");
     93  1.1  jonathan SYSCTL_STRUCT(_net_inet_esp, IPSECCTL_STATS,
     94  1.1  jonathan 	stats,		CTLFLAG_RD,	&espstat,	espstat, "");
     95  1.1  jonathan #endif __FreeBSD__
     96  1.1  jonathan 
     97  1.1  jonathan static	int esp_max_ivlen;		/* max iv length over all algorithms */
     98  1.1  jonathan 
     99  1.1  jonathan static int esp_input_cb(struct cryptop *op);
    100  1.1  jonathan static int esp_output_cb(struct cryptop *crp);
    101  1.1  jonathan 
    102  1.1  jonathan /*
    103  1.1  jonathan  * NB: this is public for use by the PF_KEY support.
    104  1.1  jonathan  * NB: if you add support here; be sure to add code to esp_attach below!
    105  1.1  jonathan  */
    106  1.1  jonathan struct enc_xform *
    107  1.1  jonathan esp_algorithm_lookup(int alg)
    108  1.1  jonathan {
    109  1.1  jonathan 	if (alg >= ESP_ALG_MAX)
    110  1.1  jonathan 		return NULL;
    111  1.1  jonathan 	switch (alg) {
    112  1.1  jonathan 	case SADB_EALG_DESCBC:
    113  1.1  jonathan 		return &enc_xform_des;
    114  1.1  jonathan 	case SADB_EALG_3DESCBC:
    115  1.1  jonathan 		return &enc_xform_3des;
    116  1.1  jonathan 	case SADB_X_EALG_AES:
    117  1.1  jonathan 		return &enc_xform_rijndael128;
    118  1.1  jonathan 	case SADB_X_EALG_BLOWFISHCBC:
    119  1.1  jonathan 		return &enc_xform_blf;
    120  1.1  jonathan 	case SADB_X_EALG_CAST128CBC:
    121  1.1  jonathan 		return &enc_xform_cast5;
    122  1.1  jonathan 	case SADB_X_EALG_SKIPJACK:
    123  1.1  jonathan 		return &enc_xform_skipjack;
    124  1.1  jonathan 	case SADB_EALG_NULL:
    125  1.1  jonathan 		return &enc_xform_null;
    126  1.1  jonathan 	}
    127  1.1  jonathan 	return NULL;
    128  1.1  jonathan }
    129  1.1  jonathan 
    130  1.1  jonathan size_t
    131  1.1  jonathan esp_hdrsiz(struct secasvar *sav)
    132  1.1  jonathan {
    133  1.1  jonathan 	size_t size;
    134  1.1  jonathan 
    135  1.1  jonathan 	if (sav != NULL) {
    136  1.1  jonathan 		/*XXX not right for null algorithm--does it matter??*/
    137  1.1  jonathan 		IPSEC_ASSERT(sav->tdb_encalgxform != NULL,
    138  1.1  jonathan 			("esp_hdrsiz: SA with null xform"));
    139  1.1  jonathan 		if (sav->flags & SADB_X_EXT_OLD)
    140  1.1  jonathan 			size = sizeof (struct esp);
    141  1.1  jonathan 		else
    142  1.1  jonathan 			size = sizeof (struct newesp);
    143  1.1  jonathan 		size += sav->tdb_encalgxform->blocksize + 9;
    144  1.1  jonathan 		/*XXX need alg check???*/
    145  1.1  jonathan 		if (sav->tdb_authalgxform != NULL && sav->replay)
    146  1.1  jonathan 			size += ah_hdrsiz(sav);
    147  1.1  jonathan 	} else {
    148  1.1  jonathan 		/*
    149  1.1  jonathan 		 *   base header size
    150  1.1  jonathan 		 * + max iv length for CBC mode
    151  1.1  jonathan 		 * + max pad length
    152  1.1  jonathan 		 * + sizeof (pad length field)
    153  1.1  jonathan 		 * + sizeof (next header field)
    154  1.1  jonathan 		 * + max icv supported.
    155  1.1  jonathan 		 */
    156  1.1  jonathan 		size = sizeof (struct newesp) + esp_max_ivlen + 9 + 16;
    157  1.1  jonathan 	}
    158  1.1  jonathan 	return size;
    159  1.1  jonathan }
    160  1.1  jonathan 
    161  1.1  jonathan /*
    162  1.1  jonathan  * esp_init() is called when an SPI is being set up.
    163  1.1  jonathan  */
    164  1.1  jonathan static int
    165  1.1  jonathan esp_init(struct secasvar *sav, struct xformsw *xsp)
    166  1.1  jonathan {
    167  1.1  jonathan 	struct enc_xform *txform;
    168  1.1  jonathan 	struct cryptoini cria, crie;
    169  1.1  jonathan 	int keylen;
    170  1.1  jonathan 	int error;
    171  1.1  jonathan 
    172  1.1  jonathan 	txform = esp_algorithm_lookup(sav->alg_enc);
    173  1.1  jonathan 	if (txform == NULL) {
    174  1.1  jonathan 		DPRINTF(("esp_init: unsupported encryption algorithm %d\n",
    175  1.1  jonathan 			sav->alg_enc));
    176  1.1  jonathan 		return EINVAL;
    177  1.1  jonathan 	}
    178  1.1  jonathan 	if (sav->key_enc == NULL) {
    179  1.1  jonathan 		DPRINTF(("esp_init: no encoding key for %s algorithm\n",
    180  1.1  jonathan 			 txform->name));
    181  1.1  jonathan 		return EINVAL;
    182  1.1  jonathan 	}
    183  1.1  jonathan 	if ((sav->flags&(SADB_X_EXT_OLD|SADB_X_EXT_IV4B)) == SADB_X_EXT_IV4B) {
    184  1.1  jonathan 		DPRINTF(("esp_init: 4-byte IV not supported with protocol\n"));
    185  1.1  jonathan 		return EINVAL;
    186  1.1  jonathan 	}
    187  1.1  jonathan 	keylen = _KEYLEN(sav->key_enc);
    188  1.1  jonathan 	if (txform->minkey > keylen || keylen > txform->maxkey) {
    189  1.1  jonathan 		DPRINTF(("esp_init: invalid key length %u, must be in "
    190  1.1  jonathan 			"the range [%u..%u] for algorithm %s\n",
    191  1.1  jonathan 			keylen, txform->minkey, txform->maxkey,
    192  1.1  jonathan 			txform->name));
    193  1.1  jonathan 		return EINVAL;
    194  1.1  jonathan 	}
    195  1.1  jonathan 
    196  1.1  jonathan 	/*
    197  1.1  jonathan 	 * NB: The null xform needs a non-zero blocksize to keep the
    198  1.1  jonathan 	 *      crypto code happy but if we use it to set ivlen then
    199  1.1  jonathan 	 *      the ESP header will be processed incorrectly.  The
    200  1.1  jonathan 	 *      compromise is to force it to zero here.
    201  1.1  jonathan 	 */
    202  1.1  jonathan 	sav->ivlen = (txform == &enc_xform_null ? 0 : txform->blocksize);
    203  1.1  jonathan 	sav->iv = (caddr_t) malloc(sav->ivlen, M_XDATA, M_WAITOK);
    204  1.1  jonathan 	if (sav->iv == NULL) {
    205  1.1  jonathan 		DPRINTF(("esp_init: no memory for IV\n"));
    206  1.1  jonathan 		return EINVAL;
    207  1.1  jonathan 	}
    208  1.1  jonathan 	key_randomfill(sav->iv, sav->ivlen);	/*XXX*/
    209  1.1  jonathan 
    210  1.1  jonathan 	/*
    211  1.1  jonathan 	 * Setup AH-related state.
    212  1.1  jonathan 	 */
    213  1.1  jonathan 	if (sav->alg_auth != 0) {
    214  1.1  jonathan 		error = ah_init0(sav, xsp, &cria);
    215  1.1  jonathan 		if (error)
    216  1.1  jonathan 			return error;
    217  1.1  jonathan 	}
    218  1.1  jonathan 
    219  1.1  jonathan 	/* NB: override anything set in ah_init0 */
    220  1.1  jonathan 	sav->tdb_xform = xsp;
    221  1.1  jonathan 	sav->tdb_encalgxform = txform;
    222  1.1  jonathan 
    223  1.1  jonathan 	/* Initialize crypto session. */
    224  1.1  jonathan 	bzero(&crie, sizeof (crie));
    225  1.1  jonathan 	crie.cri_alg = sav->tdb_encalgxform->type;
    226  1.1  jonathan 	crie.cri_klen = _KEYBITS(sav->key_enc);
    227  1.1  jonathan 	crie.cri_key = _KEYBUF(sav->key_enc);
    228  1.1  jonathan 	/* XXX Rounds ? */
    229  1.1  jonathan 
    230  1.1  jonathan 	if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
    231  1.1  jonathan 		/* init both auth & enc */
    232  1.1  jonathan 		crie.cri_next = &cria;
    233  1.1  jonathan 		error = crypto_newsession(&sav->tdb_cryptoid,
    234  1.1  jonathan 					  &crie, crypto_support);
    235  1.1  jonathan 	} else if (sav->tdb_encalgxform) {
    236  1.1  jonathan 		error = crypto_newsession(&sav->tdb_cryptoid,
    237  1.1  jonathan 					  &crie, crypto_support);
    238  1.1  jonathan 	} else if (sav->tdb_authalgxform) {
    239  1.1  jonathan 		error = crypto_newsession(&sav->tdb_cryptoid,
    240  1.1  jonathan 					  &cria, crypto_support);
    241  1.1  jonathan 	} else {
    242  1.1  jonathan 		/* XXX cannot happen? */
    243  1.1  jonathan 		DPRINTF(("esp_init: no encoding OR authentication xform!\n"));
    244  1.1  jonathan 		error = EINVAL;
    245  1.1  jonathan 	}
    246  1.1  jonathan 	return error;
    247  1.1  jonathan }
    248  1.1  jonathan 
    249  1.1  jonathan /*
    250  1.1  jonathan  * Paranoia.
    251  1.1  jonathan  */
    252  1.1  jonathan static int
    253  1.1  jonathan esp_zeroize(struct secasvar *sav)
    254  1.1  jonathan {
    255  1.1  jonathan 	/* NB: ah_zerorize free's the crypto session state */
    256  1.1  jonathan 	int error = ah_zeroize(sav);
    257  1.1  jonathan 
    258  1.1  jonathan 	if (sav->key_enc)
    259  1.1  jonathan 		bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
    260  1.1  jonathan 	/* NB: sav->iv is freed elsewhere, even though we malloc it! */
    261  1.1  jonathan 	sav->tdb_encalgxform = NULL;
    262  1.1  jonathan 	sav->tdb_xform = NULL;
    263  1.1  jonathan 	return error;
    264  1.1  jonathan }
    265  1.1  jonathan 
    266  1.1  jonathan /*
    267  1.1  jonathan  * ESP input processing, called (eventually) through the protocol switch.
    268  1.1  jonathan  */
    269  1.1  jonathan static int
    270  1.1  jonathan esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    271  1.1  jonathan {
    272  1.1  jonathan 	struct auth_hash *esph;
    273  1.1  jonathan 	struct enc_xform *espx;
    274  1.1  jonathan 	struct tdb_ident *tdbi;
    275  1.1  jonathan 	struct tdb_crypto *tc;
    276  1.1  jonathan 	int plen, alen, hlen;
    277  1.1  jonathan 	struct m_tag *mtag;
    278  1.1  jonathan 	struct newesp *esp;
    279  1.1  jonathan 
    280  1.1  jonathan 	struct cryptodesc *crde;
    281  1.1  jonathan 	struct cryptop *crp;
    282  1.1  jonathan 
    283  1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("esp_input");
    284  1.1  jonathan 
    285  1.1  jonathan 	IPSEC_ASSERT(sav != NULL, ("esp_input: null SA"));
    286  1.1  jonathan 	IPSEC_ASSERT(sav->tdb_encalgxform != NULL,
    287  1.1  jonathan 		("esp_input: null encoding xform"));
    288  1.1  jonathan 	IPSEC_ASSERT((skip&3) == 0 && (m->m_pkthdr.len&3) == 0,
    289  1.1  jonathan 		("esp_input: misaligned packet, skip %u pkt len %u",
    290  1.1  jonathan 			skip, m->m_pkthdr.len));
    291  1.1  jonathan 
    292  1.1  jonathan 	/* XXX don't pullup, just copy header */
    293  1.1  jonathan 	IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof (struct newesp));
    294  1.1  jonathan 
    295  1.1  jonathan 	esph = sav->tdb_authalgxform;
    296  1.1  jonathan 	espx = sav->tdb_encalgxform;
    297  1.1  jonathan 
    298  1.1  jonathan 	/* Determine the ESP header length */
    299  1.1  jonathan 	if (sav->flags & SADB_X_EXT_OLD)
    300  1.1  jonathan 		hlen = sizeof (struct esp) + sav->ivlen;
    301  1.1  jonathan 	else
    302  1.1  jonathan 		hlen = sizeof (struct newesp) + sav->ivlen;
    303  1.1  jonathan 	/* Authenticator hash size */
    304  1.1  jonathan 	alen = esph ? AH_HMAC_HASHLEN : 0;
    305  1.1  jonathan 
    306  1.1  jonathan 	/*
    307  1.1  jonathan 	 * Verify payload length is multiple of encryption algorithm
    308  1.1  jonathan 	 * block size.
    309  1.1  jonathan 	 *
    310  1.1  jonathan 	 * NB: This works for the null algorithm because the blocksize
    311  1.1  jonathan 	 *     is 4 and all packets must be 4-byte aligned regardless
    312  1.1  jonathan 	 *     of the algorithm.
    313  1.1  jonathan 	 */
    314  1.1  jonathan 	plen = m->m_pkthdr.len - (skip + hlen + alen);
    315  1.1  jonathan 	if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
    316  1.1  jonathan 		DPRINTF(("esp_input: "
    317  1.1  jonathan 		    "payload of %d octets not a multiple of %d octets,"
    318  1.1  jonathan 		    "  SA %s/%08lx\n",
    319  1.1  jonathan 		    plen, espx->blocksize,
    320  1.1  jonathan 		    ipsec_address(&sav->sah->saidx.dst),
    321  1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    322  1.1  jonathan 		espstat.esps_badilen++;
    323  1.1  jonathan 		m_freem(m);
    324  1.1  jonathan 		return EINVAL;
    325  1.1  jonathan 	}
    326  1.1  jonathan 
    327  1.1  jonathan 	/*
    328  1.1  jonathan 	 * Check sequence number.
    329  1.1  jonathan 	 */
    330  1.1  jonathan 	if (esph && sav->replay && !ipsec_chkreplay(ntohl(esp->esp_seq), sav)) {
    331  1.1  jonathan 		DPRINTF(("esp_input: packet replay check for %s\n",
    332  1.1  jonathan 		    ipsec_logsastr(sav)));	/*XXX*/
    333  1.1  jonathan 		espstat.esps_replay++;
    334  1.1  jonathan 		m_freem(m);
    335  1.1  jonathan 		return ENOBUFS;		/*XXX*/
    336  1.1  jonathan 	}
    337  1.1  jonathan 
    338  1.1  jonathan 	/* Update the counters */
    339  1.1  jonathan 	espstat.esps_ibytes += m->m_pkthdr.len - skip - hlen - alen;
    340  1.1  jonathan 
    341  1.1  jonathan 	/* Find out if we've already done crypto */
    342  1.1  jonathan 	for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
    343  1.1  jonathan 	     mtag != NULL;
    344  1.1  jonathan 	     mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
    345  1.1  jonathan 		tdbi = (struct tdb_ident *) (mtag + 1);
    346  1.1  jonathan 		if (tdbi->proto == sav->sah->saidx.proto &&
    347  1.1  jonathan 		    tdbi->spi == sav->spi &&
    348  1.1  jonathan 		    !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
    349  1.1  jonathan 			  sizeof(union sockaddr_union)))
    350  1.1  jonathan 			break;
    351  1.1  jonathan 	}
    352  1.1  jonathan 
    353  1.1  jonathan 	/* Get crypto descriptors */
    354  1.1  jonathan 	crp = crypto_getreq(esph && espx ? 2 : 1);
    355  1.1  jonathan 	if (crp == NULL) {
    356  1.1  jonathan 		DPRINTF(("esp_input: failed to acquire crypto descriptors\n"));
    357  1.1  jonathan 		espstat.esps_crypto++;
    358  1.1  jonathan 		m_freem(m);
    359  1.1  jonathan 		return ENOBUFS;
    360  1.1  jonathan 	}
    361  1.1  jonathan 
    362  1.1  jonathan 	/* Get IPsec-specific opaque pointer */
    363  1.1  jonathan 	if (esph == NULL || mtag != NULL)
    364  1.1  jonathan 		tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
    365  1.1  jonathan 		    M_XDATA, M_NOWAIT|M_ZERO);
    366  1.1  jonathan 	else
    367  1.1  jonathan 		tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto) + alen,
    368  1.1  jonathan 		    M_XDATA, M_NOWAIT|M_ZERO);
    369  1.1  jonathan 	if (tc == NULL) {
    370  1.1  jonathan 		crypto_freereq(crp);
    371  1.1  jonathan 		DPRINTF(("esp_input: failed to allocate tdb_crypto\n"));
    372  1.1  jonathan 		espstat.esps_crypto++;
    373  1.1  jonathan 		m_freem(m);
    374  1.1  jonathan 		return ENOBUFS;
    375  1.1  jonathan 	}
    376  1.1  jonathan 
    377  1.1  jonathan 	tc->tc_ptr = (caddr_t) mtag;
    378  1.1  jonathan 
    379  1.1  jonathan 	if (esph) {
    380  1.1  jonathan 		struct cryptodesc *crda = crp->crp_desc;
    381  1.1  jonathan 
    382  1.1  jonathan 		IPSEC_ASSERT(crda != NULL, ("esp_input: null ah crypto descriptor"));
    383  1.1  jonathan 
    384  1.1  jonathan 		/* Authentication descriptor */
    385  1.1  jonathan 		crda->crd_skip = skip;
    386  1.1  jonathan 		crda->crd_len = m->m_pkthdr.len - (skip + alen);
    387  1.1  jonathan 		crda->crd_inject = m->m_pkthdr.len - alen;
    388  1.1  jonathan 
    389  1.1  jonathan 		crda->crd_alg = esph->type;
    390  1.1  jonathan 		crda->crd_key = _KEYBUF(sav->key_auth);
    391  1.1  jonathan 		crda->crd_klen = _KEYBITS(sav->key_auth);
    392  1.1  jonathan 
    393  1.1  jonathan 		/* Copy the authenticator */
    394  1.1  jonathan 		if (mtag == NULL)
    395  1.1  jonathan 			m_copydata(m, m->m_pkthdr.len - alen, alen,
    396  1.1  jonathan 				   (caddr_t) (tc + 1));
    397  1.1  jonathan 
    398  1.1  jonathan 		/* Chain authentication request */
    399  1.1  jonathan 		crde = crda->crd_next;
    400  1.1  jonathan 	} else {
    401  1.1  jonathan 		crde = crp->crp_desc;
    402  1.1  jonathan 	}
    403  1.1  jonathan 
    404  1.1  jonathan 	/* Crypto operation descriptor */
    405  1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
    406  1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    407  1.1  jonathan 	crp->crp_buf = (caddr_t) m;
    408  1.1  jonathan 	crp->crp_callback = esp_input_cb;
    409  1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    410  1.1  jonathan 	crp->crp_opaque = (caddr_t) tc;
    411  1.1  jonathan 
    412  1.1  jonathan 	/* These are passed as-is to the callback */
    413  1.1  jonathan 	tc->tc_spi = sav->spi;
    414  1.1  jonathan 	tc->tc_dst = sav->sah->saidx.dst;
    415  1.1  jonathan 	tc->tc_proto = sav->sah->saidx.proto;
    416  1.1  jonathan 	tc->tc_protoff = protoff;
    417  1.1  jonathan 	tc->tc_skip = skip;
    418  1.1  jonathan 
    419  1.1  jonathan 	/* Decryption descriptor */
    420  1.1  jonathan 	if (espx) {
    421  1.1  jonathan 		IPSEC_ASSERT(crde != NULL, ("esp_input: null esp crypto descriptor"));
    422  1.1  jonathan 		crde->crd_skip = skip + hlen;
    423  1.1  jonathan 		crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
    424  1.1  jonathan 		crde->crd_inject = skip + hlen - sav->ivlen;
    425  1.1  jonathan 
    426  1.1  jonathan 		crde->crd_alg = espx->type;
    427  1.1  jonathan 		crde->crd_key = _KEYBUF(sav->key_enc);
    428  1.1  jonathan 		crde->crd_klen = _KEYBITS(sav->key_enc);
    429  1.1  jonathan 		/* XXX Rounds ? */
    430  1.1  jonathan 	}
    431  1.1  jonathan 
    432  1.1  jonathan 	if (mtag == NULL)
    433  1.1  jonathan 		return crypto_dispatch(crp);
    434  1.1  jonathan 	else
    435  1.1  jonathan 		return esp_input_cb(crp);
    436  1.1  jonathan }
    437  1.1  jonathan 
    438  1.1  jonathan #ifdef INET6
    439  1.1  jonathan #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
    440  1.1  jonathan 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    441  1.1  jonathan 		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
    442  1.1  jonathan 	} else {							     \
    443  1.1  jonathan 		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
    444  1.1  jonathan 	}								     \
    445  1.1  jonathan } while (0)
    446  1.1  jonathan #else
    447  1.1  jonathan #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
    448  1.1  jonathan 	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
    449  1.1  jonathan #endif
    450  1.1  jonathan 
    451  1.1  jonathan /*
    452  1.1  jonathan  * ESP input callback from the crypto driver.
    453  1.1  jonathan  */
    454  1.1  jonathan static int
    455  1.1  jonathan esp_input_cb(struct cryptop *crp)
    456  1.1  jonathan {
    457  1.1  jonathan 	u_int8_t lastthree[3], aalg[AH_HMAC_HASHLEN];
    458  1.1  jonathan 	int s, hlen, skip, protoff, error;
    459  1.1  jonathan 	struct mbuf *m;
    460  1.1  jonathan 	struct cryptodesc *crd;
    461  1.1  jonathan 	struct auth_hash *esph;
    462  1.1  jonathan 	struct enc_xform *espx;
    463  1.1  jonathan 	struct tdb_crypto *tc;
    464  1.1  jonathan 	struct m_tag *mtag;
    465  1.1  jonathan 	struct secasvar *sav;
    466  1.1  jonathan 	struct secasindex *saidx;
    467  1.1  jonathan 	caddr_t ptr;
    468  1.1  jonathan 
    469  1.1  jonathan 	crd = crp->crp_desc;
    470  1.1  jonathan 	IPSEC_ASSERT(crd != NULL, ("esp_input_cb: null crypto descriptor!"));
    471  1.1  jonathan 
    472  1.1  jonathan 	tc = (struct tdb_crypto *) crp->crp_opaque;
    473  1.1  jonathan 	IPSEC_ASSERT(tc != NULL, ("esp_input_cb: null opaque crypto data area!"));
    474  1.1  jonathan 	skip = tc->tc_skip;
    475  1.1  jonathan 	protoff = tc->tc_protoff;
    476  1.1  jonathan 	mtag = (struct m_tag *) tc->tc_ptr;
    477  1.1  jonathan 	m = (struct mbuf *) crp->crp_buf;
    478  1.1  jonathan 
    479  1.1  jonathan 	s = splsoftnet();
    480  1.1  jonathan 
    481  1.1  jonathan 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
    482  1.1  jonathan 	if (sav == NULL) {
    483  1.1  jonathan 		espstat.esps_notdb++;
    484  1.1  jonathan 		DPRINTF(("esp_input_cb: SA expired while in crypto "
    485  1.1  jonathan 		    "(SA %s/%08lx proto %u)\n", ipsec_address(&tc->tc_dst),
    486  1.1  jonathan 		    (u_long) ntohl(tc->tc_spi), tc->tc_proto));
    487  1.1  jonathan 		error = ENOBUFS;		/*XXX*/
    488  1.1  jonathan 		goto bad;
    489  1.1  jonathan 	}
    490  1.1  jonathan 
    491  1.1  jonathan 	saidx = &sav->sah->saidx;
    492  1.1  jonathan 	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
    493  1.1  jonathan 		saidx->dst.sa.sa_family == AF_INET6,
    494  1.1  jonathan 		("ah_input_cb: unexpected protocol family %u",
    495  1.1  jonathan 		 saidx->dst.sa.sa_family));
    496  1.1  jonathan 
    497  1.1  jonathan 	esph = sav->tdb_authalgxform;
    498  1.1  jonathan 	espx = sav->tdb_encalgxform;
    499  1.1  jonathan 
    500  1.1  jonathan 	/* Check for crypto errors */
    501  1.1  jonathan 	if (crp->crp_etype) {
    502  1.1  jonathan 		/* Reset the session ID */
    503  1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    504  1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    505  1.1  jonathan 
    506  1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    507  1.1  jonathan 			KEY_FREESAV(&sav);
    508  1.1  jonathan 			splx(s);
    509  1.1  jonathan 			return crypto_dispatch(crp);
    510  1.1  jonathan 		}
    511  1.1  jonathan 
    512  1.1  jonathan 		espstat.esps_noxform++;
    513  1.1  jonathan 		DPRINTF(("esp_input_cb: crypto error %d\n", crp->crp_etype));
    514  1.1  jonathan 		error = crp->crp_etype;
    515  1.1  jonathan 		goto bad;
    516  1.1  jonathan 	}
    517  1.1  jonathan 
    518  1.1  jonathan 	/* Shouldn't happen... */
    519  1.1  jonathan 	if (m == NULL) {
    520  1.1  jonathan 		espstat.esps_crypto++;
    521  1.1  jonathan 		DPRINTF(("esp_input_cb: bogus returned buffer from crypto\n"));
    522  1.1  jonathan 		error = EINVAL;
    523  1.1  jonathan 		goto bad;
    524  1.1  jonathan 	}
    525  1.1  jonathan 	espstat.esps_hist[sav->alg_enc]++;
    526  1.1  jonathan 
    527  1.1  jonathan 	/* If authentication was performed, check now. */
    528  1.1  jonathan 	if (esph != NULL) {
    529  1.1  jonathan 		/*
    530  1.1  jonathan 		 * If we have a tag, it means an IPsec-aware NIC did
    531  1.1  jonathan 		 * the verification for us.  Otherwise we need to
    532  1.1  jonathan 		 * check the authentication calculation.
    533  1.1  jonathan 		 */
    534  1.1  jonathan 		ahstat.ahs_hist[sav->alg_auth]++;
    535  1.1  jonathan 		if (mtag == NULL) {
    536  1.1  jonathan 			/* Copy the authenticator from the packet */
    537  1.1  jonathan 			m_copydata(m, m->m_pkthdr.len - esph->authsize,
    538  1.1  jonathan 				esph->authsize, aalg);
    539  1.1  jonathan 
    540  1.1  jonathan 			ptr = (caddr_t) (tc + 1);
    541  1.1  jonathan 
    542  1.1  jonathan 			/* Verify authenticator */
    543  1.1  jonathan 			if (bcmp(ptr, aalg, esph->authsize) != 0) {
    544  1.1  jonathan 				DPRINTF(("esp_input_cb: "
    545  1.1  jonathan 		    "authentication hash mismatch for packet in SA %s/%08lx\n",
    546  1.1  jonathan 				    ipsec_address(&saidx->dst),
    547  1.1  jonathan 				    (u_long) ntohl(sav->spi)));
    548  1.1  jonathan 				espstat.esps_badauth++;
    549  1.1  jonathan 				error = EACCES;
    550  1.1  jonathan 				goto bad;
    551  1.1  jonathan 			}
    552  1.1  jonathan 		}
    553  1.1  jonathan 
    554  1.1  jonathan 		/* Remove trailing authenticator */
    555  1.1  jonathan 		m_adj(m, -(esph->authsize));
    556  1.1  jonathan 	}
    557  1.1  jonathan 
    558  1.1  jonathan 	/* Release the crypto descriptors */
    559  1.1  jonathan 	free(tc, M_XDATA), tc = NULL;
    560  1.1  jonathan 	crypto_freereq(crp), crp = NULL;
    561  1.1  jonathan 
    562  1.1  jonathan 	/*
    563  1.1  jonathan 	 * Packet is now decrypted.
    564  1.1  jonathan 	 */
    565  1.1  jonathan 	m->m_flags |= M_DECRYPTED;
    566  1.1  jonathan 
    567  1.1  jonathan 	/* Determine the ESP header length */
    568  1.1  jonathan 	if (sav->flags & SADB_X_EXT_OLD)
    569  1.1  jonathan 		hlen = sizeof (struct esp) + sav->ivlen;
    570  1.1  jonathan 	else
    571  1.1  jonathan 		hlen = sizeof (struct newesp) + sav->ivlen;
    572  1.1  jonathan 
    573  1.1  jonathan 	/* Remove the ESP header and IV from the mbuf. */
    574  1.1  jonathan 	error = m_striphdr(m, skip, hlen);
    575  1.1  jonathan 	if (error) {
    576  1.1  jonathan 		espstat.esps_hdrops++;
    577  1.1  jonathan 		DPRINTF(("esp_input_cb: bad mbuf chain, SA %s/%08lx\n",
    578  1.1  jonathan 		    ipsec_address(&sav->sah->saidx.dst),
    579  1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    580  1.1  jonathan 		goto bad;
    581  1.1  jonathan 	}
    582  1.1  jonathan 
    583  1.1  jonathan 	/* Save the last three bytes of decrypted data */
    584  1.1  jonathan 	m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
    585  1.1  jonathan 
    586  1.1  jonathan 	/* Verify pad length */
    587  1.1  jonathan 	if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
    588  1.1  jonathan 		espstat.esps_badilen++;
    589  1.1  jonathan 		DPRINTF(("esp_input_cb: invalid padding length %d "
    590  1.1  jonathan 			 "for %u byte packet in SA %s/%08lx\n",
    591  1.1  jonathan 			 lastthree[1], m->m_pkthdr.len - skip,
    592  1.1  jonathan 			 ipsec_address(&sav->sah->saidx.dst),
    593  1.1  jonathan 			 (u_long) ntohl(sav->spi)));
    594  1.1  jonathan 		error = EINVAL;
    595  1.1  jonathan 		goto bad;
    596  1.1  jonathan 	}
    597  1.1  jonathan 
    598  1.1  jonathan 	/* Verify correct decryption by checking the last padding bytes */
    599  1.1  jonathan 	if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
    600  1.1  jonathan 		if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
    601  1.1  jonathan 			espstat.esps_badenc++;
    602  1.1  jonathan 			DPRINTF(("esp_input_cb: decryption failed "
    603  1.1  jonathan 				"for packet in SA %s/%08lx\n",
    604  1.1  jonathan 				ipsec_address(&sav->sah->saidx.dst),
    605  1.1  jonathan 				(u_long) ntohl(sav->spi)));
    606  1.1  jonathan DPRINTF(("esp_input_cb: %x %x\n", lastthree[0], lastthree[1]));
    607  1.1  jonathan 			error = EINVAL;
    608  1.1  jonathan 			goto bad;
    609  1.1  jonathan 		}
    610  1.1  jonathan 	}
    611  1.1  jonathan 
    612  1.1  jonathan 	/* Trim the mbuf chain to remove trailing authenticator and padding */
    613  1.1  jonathan 	m_adj(m, -(lastthree[1] + 2));
    614  1.1  jonathan 
    615  1.1  jonathan 	/* Restore the Next Protocol field */
    616  1.1  jonathan 	m_copyback(m, protoff, sizeof (u_int8_t), lastthree + 2);
    617  1.1  jonathan 
    618  1.1  jonathan 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
    619  1.1  jonathan 
    620  1.1  jonathan 	KEY_FREESAV(&sav);
    621  1.1  jonathan 	splx(s);
    622  1.1  jonathan 	return error;
    623  1.1  jonathan bad:
    624  1.1  jonathan 	if (sav)
    625  1.1  jonathan 		KEY_FREESAV(&sav);
    626  1.1  jonathan 	splx(s);
    627  1.1  jonathan 	if (m != NULL)
    628  1.1  jonathan 		m_freem(m);
    629  1.1  jonathan 	if (tc != NULL)
    630  1.1  jonathan 		free(tc, M_XDATA);
    631  1.1  jonathan 	if (crp != NULL)
    632  1.1  jonathan 		crypto_freereq(crp);
    633  1.1  jonathan 	return error;
    634  1.1  jonathan }
    635  1.1  jonathan 
    636  1.1  jonathan /*
    637  1.1  jonathan  * ESP output routine, called by ipsec[46]_process_packet().
    638  1.1  jonathan  */
    639  1.1  jonathan static int
    640  1.1  jonathan esp_output(
    641  1.1  jonathan 	struct mbuf *m,
    642  1.1  jonathan 	struct ipsecrequest *isr,
    643  1.1  jonathan 	struct mbuf **mp,
    644  1.1  jonathan 	int skip,
    645  1.1  jonathan 	int protoff
    646  1.1  jonathan )
    647  1.1  jonathan {
    648  1.1  jonathan 	struct enc_xform *espx;
    649  1.1  jonathan 	struct auth_hash *esph;
    650  1.1  jonathan 	int hlen, rlen, plen, padding, blks, alen, i, roff;
    651  1.1  jonathan 	struct mbuf *mo = (struct mbuf *) NULL;
    652  1.1  jonathan 	struct tdb_crypto *tc;
    653  1.1  jonathan 	struct secasvar *sav;
    654  1.1  jonathan 	struct secasindex *saidx;
    655  1.1  jonathan 	unsigned char *pad;
    656  1.1  jonathan 	u_int8_t prot;
    657  1.1  jonathan 	int error, maxpacketsize;
    658  1.1  jonathan 
    659  1.1  jonathan 	struct cryptodesc *crde = NULL, *crda = NULL;
    660  1.1  jonathan 	struct cryptop *crp;
    661  1.1  jonathan 
    662  1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("esp_output");
    663  1.1  jonathan 
    664  1.1  jonathan 	sav = isr->sav;
    665  1.1  jonathan 	IPSEC_ASSERT(sav != NULL, ("esp_output: null SA"));
    666  1.1  jonathan 	esph = sav->tdb_authalgxform;
    667  1.1  jonathan 	espx = sav->tdb_encalgxform;
    668  1.1  jonathan 	IPSEC_ASSERT(espx != NULL, ("esp_output: null encoding xform"));
    669  1.1  jonathan 
    670  1.1  jonathan 	if (sav->flags & SADB_X_EXT_OLD)
    671  1.1  jonathan 		hlen = sizeof (struct esp) + sav->ivlen;
    672  1.1  jonathan 	else
    673  1.1  jonathan 		hlen = sizeof (struct newesp) + sav->ivlen;
    674  1.1  jonathan 
    675  1.1  jonathan 	rlen = m->m_pkthdr.len - skip;	/* Raw payload length. */
    676  1.1  jonathan 	/*
    677  1.1  jonathan 	 * NB: The null encoding transform has a blocksize of 4
    678  1.1  jonathan 	 *     so that headers are properly aligned.
    679  1.1  jonathan 	 */
    680  1.1  jonathan 	blks = espx->blocksize;		/* IV blocksize */
    681  1.1  jonathan 
    682  1.1  jonathan 	/* XXX clamp padding length a la KAME??? */
    683  1.1  jonathan 	padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
    684  1.1  jonathan 	plen = rlen + padding;		/* Padded payload length. */
    685  1.1  jonathan 
    686  1.1  jonathan 	if (esph)
    687  1.1  jonathan 		alen = AH_HMAC_HASHLEN;
    688  1.1  jonathan 	else
    689  1.1  jonathan 		alen = 0;
    690  1.1  jonathan 
    691  1.1  jonathan 	espstat.esps_output++;
    692  1.1  jonathan 
    693  1.1  jonathan 	saidx = &sav->sah->saidx;
    694  1.1  jonathan 	/* Check for maximum packet size violations. */
    695  1.1  jonathan 	switch (saidx->dst.sa.sa_family) {
    696  1.1  jonathan #ifdef INET
    697  1.1  jonathan 	case AF_INET:
    698  1.1  jonathan 		maxpacketsize = IP_MAXPACKET;
    699  1.1  jonathan 		break;
    700  1.1  jonathan #endif /* INET */
    701  1.1  jonathan #ifdef INET6
    702  1.1  jonathan 	case AF_INET6:
    703  1.1  jonathan 		maxpacketsize = IPV6_MAXPACKET;
    704  1.1  jonathan 		break;
    705  1.1  jonathan #endif /* INET6 */
    706  1.1  jonathan 	default:
    707  1.1  jonathan 		DPRINTF(("esp_output: unknown/unsupported protocol "
    708  1.1  jonathan 		    "family %d, SA %s/%08lx\n",
    709  1.1  jonathan 		    saidx->dst.sa.sa_family, ipsec_address(&saidx->dst),
    710  1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    711  1.1  jonathan 		espstat.esps_nopf++;
    712  1.1  jonathan 		error = EPFNOSUPPORT;
    713  1.1  jonathan 		goto bad;
    714  1.1  jonathan 	}
    715  1.1  jonathan 	if (skip + hlen + rlen + padding + alen > maxpacketsize) {
    716  1.1  jonathan 		DPRINTF(("esp_output: packet in SA %s/%08lx got too big "
    717  1.1  jonathan 		    "(len %u, max len %u)\n",
    718  1.1  jonathan 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi),
    719  1.1  jonathan 		    skip + hlen + rlen + padding + alen, maxpacketsize));
    720  1.1  jonathan 		espstat.esps_toobig++;
    721  1.1  jonathan 		error = EMSGSIZE;
    722  1.1  jonathan 		goto bad;
    723  1.1  jonathan 	}
    724  1.1  jonathan 
    725  1.1  jonathan 	/* Update the counters. */
    726  1.1  jonathan 	espstat.esps_obytes += m->m_pkthdr.len - skip;
    727  1.1  jonathan 
    728  1.1  jonathan 	m = m_clone(m);
    729  1.1  jonathan 	if (m == NULL) {
    730  1.1  jonathan 		DPRINTF(("esp_output: cannot clone mbuf chain, SA %s/%08lx\n",
    731  1.1  jonathan 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
    732  1.1  jonathan 		espstat.esps_hdrops++;
    733  1.1  jonathan 		error = ENOBUFS;
    734  1.1  jonathan 		goto bad;
    735  1.1  jonathan 	}
    736  1.1  jonathan 
    737  1.1  jonathan 	/* Inject ESP header. */
    738  1.1  jonathan 	mo = m_makespace(m, skip, hlen, &roff);
    739  1.1  jonathan 	if (mo == NULL) {
    740  1.1  jonathan 		DPRINTF(("esp_output: failed to inject %u byte ESP hdr for SA "
    741  1.1  jonathan 		    "%s/%08lx\n",
    742  1.1  jonathan 		    hlen, ipsec_address(&saidx->dst),
    743  1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    744  1.1  jonathan 		espstat.esps_hdrops++;		/* XXX diffs from openbsd */
    745  1.1  jonathan 		error = ENOBUFS;
    746  1.1  jonathan 		goto bad;
    747  1.1  jonathan 	}
    748  1.1  jonathan 
    749  1.1  jonathan 	/* Initialize ESP header. */
    750  1.1  jonathan 	bcopy((caddr_t) &sav->spi, mtod(mo, caddr_t) + roff, sizeof(u_int32_t));
    751  1.1  jonathan 	if (sav->replay) {
    752  1.1  jonathan 		u_int32_t replay = htonl(++(sav->replay->count));
    753  1.1  jonathan 		bcopy((caddr_t) &replay,
    754  1.1  jonathan 		    mtod(mo, caddr_t) + roff + sizeof(u_int32_t),
    755  1.1  jonathan 		    sizeof(u_int32_t));
    756  1.1  jonathan 	}
    757  1.1  jonathan 
    758  1.1  jonathan 	/*
    759  1.1  jonathan 	 * Add padding -- better to do it ourselves than use the crypto engine,
    760  1.1  jonathan 	 * although if/when we support compression, we'd have to do that.
    761  1.1  jonathan 	 */
    762  1.1  jonathan 	pad = (u_char *) m_pad(m, padding + alen);
    763  1.1  jonathan 	if (pad == NULL) {
    764  1.1  jonathan 		DPRINTF(("esp_output: m_pad failed for SA %s/%08lx\n",
    765  1.1  jonathan 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
    766  1.1  jonathan 		m = NULL;		/* NB: free'd by m_pad */
    767  1.1  jonathan 		error = ENOBUFS;
    768  1.1  jonathan 		goto bad;
    769  1.1  jonathan 	}
    770  1.1  jonathan 
    771  1.1  jonathan 	/*
    772  1.1  jonathan 	 * Add padding: random, zero, or self-describing.
    773  1.1  jonathan 	 * XXX catch unexpected setting
    774  1.1  jonathan 	 */
    775  1.1  jonathan 	switch (sav->flags & SADB_X_EXT_PMASK) {
    776  1.1  jonathan 	case SADB_X_EXT_PRAND:
    777  1.1  jonathan 		(void) read_random(pad, padding - 2);
    778  1.1  jonathan 		break;
    779  1.1  jonathan 	case SADB_X_EXT_PZERO:
    780  1.1  jonathan 		bzero(pad, padding - 2);
    781  1.1  jonathan 		break;
    782  1.1  jonathan 	case SADB_X_EXT_PSEQ:
    783  1.1  jonathan 		for (i = 0; i < padding - 2; i++)
    784  1.1  jonathan 			pad[i] = i+1;
    785  1.1  jonathan 		break;
    786  1.1  jonathan 	}
    787  1.1  jonathan 
    788  1.1  jonathan 	/* Fix padding length and Next Protocol in padding itself. */
    789  1.1  jonathan 	pad[padding - 2] = padding - 2;
    790  1.1  jonathan 	m_copydata(m, protoff, sizeof(u_int8_t), pad + padding - 1);
    791  1.1  jonathan 
    792  1.1  jonathan 	/* Fix Next Protocol in IPv4/IPv6 header. */
    793  1.1  jonathan 	prot = IPPROTO_ESP;
    794  1.1  jonathan 	m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
    795  1.1  jonathan 
    796  1.1  jonathan 	/* Get crypto descriptors. */
    797  1.1  jonathan 	crp = crypto_getreq(esph && espx ? 2 : 1);
    798  1.1  jonathan 	if (crp == NULL) {
    799  1.1  jonathan 		DPRINTF(("esp_output: failed to acquire crypto descriptors\n"));
    800  1.1  jonathan 		espstat.esps_crypto++;
    801  1.1  jonathan 		error = ENOBUFS;
    802  1.1  jonathan 		goto bad;
    803  1.1  jonathan 	}
    804  1.1  jonathan 
    805  1.1  jonathan 	if (espx) {
    806  1.1  jonathan 		crde = crp->crp_desc;
    807  1.1  jonathan 		crda = crde->crd_next;
    808  1.1  jonathan 
    809  1.1  jonathan 		/* Encryption descriptor. */
    810  1.1  jonathan 		crde->crd_skip = skip + hlen;
    811  1.1  jonathan 		crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
    812  1.1  jonathan 		crde->crd_flags = CRD_F_ENCRYPT;
    813  1.1  jonathan 		crde->crd_inject = skip + hlen - sav->ivlen;
    814  1.1  jonathan 
    815  1.1  jonathan 		/* Encryption operation. */
    816  1.1  jonathan 		crde->crd_alg = espx->type;
    817  1.1  jonathan 		crde->crd_key = _KEYBUF(sav->key_enc);
    818  1.1  jonathan 		crde->crd_klen = _KEYBITS(sav->key_enc);
    819  1.1  jonathan 		/* XXX Rounds ? */
    820  1.1  jonathan 	} else
    821  1.1  jonathan 		crda = crp->crp_desc;
    822  1.1  jonathan 
    823  1.1  jonathan 	/* IPsec-specific opaque crypto info. */
    824  1.1  jonathan 	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
    825  1.1  jonathan 		M_XDATA, M_NOWAIT|M_ZERO);
    826  1.1  jonathan 	if (tc == NULL) {
    827  1.1  jonathan 		crypto_freereq(crp);
    828  1.1  jonathan 		DPRINTF(("esp_output: failed to allocate tdb_crypto\n"));
    829  1.1  jonathan 		espstat.esps_crypto++;
    830  1.1  jonathan 		error = ENOBUFS;
    831  1.1  jonathan 		goto bad;
    832  1.1  jonathan 	}
    833  1.1  jonathan 
    834  1.1  jonathan 	/* Callback parameters */
    835  1.1  jonathan 	tc->tc_isr = isr;
    836  1.1  jonathan 	tc->tc_spi = sav->spi;
    837  1.1  jonathan 	tc->tc_dst = saidx->dst;
    838  1.1  jonathan 	tc->tc_proto = saidx->proto;
    839  1.1  jonathan 
    840  1.1  jonathan 	/* Crypto operation descriptor. */
    841  1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
    842  1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    843  1.1  jonathan 	crp->crp_buf = (caddr_t) m;
    844  1.1  jonathan 	crp->crp_callback = esp_output_cb;
    845  1.1  jonathan 	crp->crp_opaque = (caddr_t) tc;
    846  1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    847  1.1  jonathan 
    848  1.1  jonathan 	if (esph) {
    849  1.1  jonathan 		/* Authentication descriptor. */
    850  1.1  jonathan 		crda->crd_skip = skip;
    851  1.1  jonathan 		crda->crd_len = m->m_pkthdr.len - (skip + alen);
    852  1.1  jonathan 		crda->crd_inject = m->m_pkthdr.len - alen;
    853  1.1  jonathan 
    854  1.1  jonathan 		/* Authentication operation. */
    855  1.1  jonathan 		crda->crd_alg = esph->type;
    856  1.1  jonathan 		crda->crd_key = _KEYBUF(sav->key_auth);
    857  1.1  jonathan 		crda->crd_klen = _KEYBITS(sav->key_auth);
    858  1.1  jonathan 	}
    859  1.1  jonathan 
    860  1.1  jonathan 	return crypto_dispatch(crp);
    861  1.1  jonathan bad:
    862  1.1  jonathan 	if (m)
    863  1.1  jonathan 		m_freem(m);
    864  1.1  jonathan 	return (error);
    865  1.1  jonathan }
    866  1.1  jonathan 
    867  1.1  jonathan /*
    868  1.1  jonathan  * ESP output callback from the crypto driver.
    869  1.1  jonathan  */
    870  1.1  jonathan static int
    871  1.1  jonathan esp_output_cb(struct cryptop *crp)
    872  1.1  jonathan {
    873  1.1  jonathan 	struct tdb_crypto *tc;
    874  1.1  jonathan 	struct ipsecrequest *isr;
    875  1.1  jonathan 	struct secasvar *sav;
    876  1.1  jonathan 	struct mbuf *m;
    877  1.1  jonathan 	int s, err, error;
    878  1.1  jonathan 
    879  1.1  jonathan 	tc = (struct tdb_crypto *) crp->crp_opaque;
    880  1.1  jonathan 	IPSEC_ASSERT(tc != NULL, ("esp_output_cb: null opaque data area!"));
    881  1.1  jonathan 	m = (struct mbuf *) crp->crp_buf;
    882  1.1  jonathan 
    883  1.1  jonathan 	s = splsoftnet();
    884  1.1  jonathan 
    885  1.1  jonathan 	isr = tc->tc_isr;
    886  1.1  jonathan 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
    887  1.1  jonathan 	if (sav == NULL) {
    888  1.1  jonathan 		espstat.esps_notdb++;
    889  1.1  jonathan 		DPRINTF(("esp_output_cb: SA expired while in crypto "
    890  1.1  jonathan 		    "(SA %s/%08lx proto %u)\n", ipsec_address(&tc->tc_dst),
    891  1.1  jonathan 		    (u_long) ntohl(tc->tc_spi), tc->tc_proto));
    892  1.1  jonathan 		error = ENOBUFS;		/*XXX*/
    893  1.1  jonathan 		goto bad;
    894  1.1  jonathan 	}
    895  1.1  jonathan 	IPSEC_ASSERT(isr->sav == sav,
    896  1.1  jonathan 		("esp_output_cb: SA changed was %p now %p\n", isr->sav, sav));
    897  1.1  jonathan 
    898  1.1  jonathan 	/* Check for crypto errors. */
    899  1.1  jonathan 	if (crp->crp_etype) {
    900  1.1  jonathan 		/* Reset session ID. */
    901  1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    902  1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    903  1.1  jonathan 
    904  1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    905  1.1  jonathan 			KEY_FREESAV(&sav);
    906  1.1  jonathan 			splx(s);
    907  1.1  jonathan 			return crypto_dispatch(crp);
    908  1.1  jonathan 		}
    909  1.1  jonathan 
    910  1.1  jonathan 		espstat.esps_noxform++;
    911  1.1  jonathan 		DPRINTF(("esp_output_cb: crypto error %d\n", crp->crp_etype));
    912  1.1  jonathan 		error = crp->crp_etype;
    913  1.1  jonathan 		goto bad;
    914  1.1  jonathan 	}
    915  1.1  jonathan 
    916  1.1  jonathan 	/* Shouldn't happen... */
    917  1.1  jonathan 	if (m == NULL) {
    918  1.1  jonathan 		espstat.esps_crypto++;
    919  1.1  jonathan 		DPRINTF(("esp_output_cb: bogus returned buffer from crypto\n"));
    920  1.1  jonathan 		error = EINVAL;
    921  1.1  jonathan 		goto bad;
    922  1.1  jonathan 	}
    923  1.1  jonathan 	espstat.esps_hist[sav->alg_enc]++;
    924  1.1  jonathan 	if (sav->tdb_authalgxform != NULL)
    925  1.1  jonathan 		ahstat.ahs_hist[sav->alg_auth]++;
    926  1.1  jonathan 
    927  1.1  jonathan 	/* Release crypto descriptors. */
    928  1.1  jonathan 	free(tc, M_XDATA);
    929  1.1  jonathan 	crypto_freereq(crp);
    930  1.1  jonathan 
    931  1.1  jonathan 	/* NB: m is reclaimed by ipsec_process_done. */
    932  1.1  jonathan 	err = ipsec_process_done(m, isr);
    933  1.1  jonathan 	KEY_FREESAV(&sav);
    934  1.1  jonathan 	splx(s);
    935  1.1  jonathan 	return err;
    936  1.1  jonathan bad:
    937  1.1  jonathan 	if (sav)
    938  1.1  jonathan 		KEY_FREESAV(&sav);
    939  1.1  jonathan 	splx(s);
    940  1.1  jonathan 	if (m)
    941  1.1  jonathan 		m_freem(m);
    942  1.1  jonathan 	free(tc, M_XDATA);
    943  1.1  jonathan 	crypto_freereq(crp);
    944  1.1  jonathan 	return error;
    945  1.1  jonathan }
    946  1.1  jonathan 
    947  1.1  jonathan static struct xformsw esp_xformsw = {
    948  1.1  jonathan 	XF_ESP,		XFT_CONF|XFT_AUTH,	"IPsec ESP",
    949  1.1  jonathan 	esp_init,	esp_zeroize,		esp_input,
    950  1.1  jonathan 	esp_output
    951  1.1  jonathan };
    952  1.1  jonathan 
    953  1.1  jonathan INITFN void
    954  1.1  jonathan esp_attach(void)
    955  1.1  jonathan {
    956  1.1  jonathan #define	MAXIV(xform)					\
    957  1.1  jonathan 	if (xform.blocksize > esp_max_ivlen)		\
    958  1.1  jonathan 		esp_max_ivlen = xform.blocksize		\
    959  1.1  jonathan 
    960  1.1  jonathan 	esp_max_ivlen = 0;
    961  1.1  jonathan 	MAXIV(enc_xform_des);		/* SADB_EALG_DESCBC */
    962  1.1  jonathan 	MAXIV(enc_xform_3des);		/* SADB_EALG_3DESCBC */
    963  1.1  jonathan 	MAXIV(enc_xform_rijndael128);	/* SADB_X_EALG_AES */
    964  1.1  jonathan 	MAXIV(enc_xform_blf);		/* SADB_X_EALG_BLOWFISHCBC */
    965  1.1  jonathan 	MAXIV(enc_xform_cast5);		/* SADB_X_EALG_CAST128CBC */
    966  1.1  jonathan 	MAXIV(enc_xform_skipjack);	/* SADB_X_EALG_SKIPJACK */
    967  1.1  jonathan 	MAXIV(enc_xform_null);		/* SADB_EALG_NULL */
    968  1.1  jonathan 
    969  1.1  jonathan 	xform_register(&esp_xformsw);
    970  1.1  jonathan #undef MAXIV
    971  1.1  jonathan }
    972  1.1  jonathan #ifdef __FreeBSD__
    973  1.1  jonathan SYSINIT(esp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, esp_attach, NULL)
    974  1.1  jonathan #else
    975  1.1  jonathan #endif
    976