Home | History | Annotate | Line # | Download | only in netipsec
xform_esp.c revision 1.50
      1  1.50  christos /*	$NetBSD: xform_esp.c,v 1.50 2017/04/13 16:38:32 christos 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.50  christos __KERNEL_RCSID(0, "$NetBSD: xform_esp.c,v 1.50 2017/04/13 16:38:32 christos Exp $");
     43   1.1  jonathan 
     44  1.47     ozaki #if defined(_KERNEL_OPT)
     45   1.1  jonathan #include "opt_inet.h"
     46   1.2  jonathan #ifdef __FreeBSD__
     47   1.1  jonathan #include "opt_inet6.h"
     48   1.2  jonathan #endif
     49  1.46     ozaki #include "opt_ipsec.h"
     50  1.47     ozaki #endif
     51   1.1  jonathan 
     52   1.1  jonathan #include <sys/param.h>
     53   1.1  jonathan #include <sys/systm.h>
     54   1.1  jonathan #include <sys/mbuf.h>
     55   1.1  jonathan #include <sys/socket.h>
     56   1.1  jonathan #include <sys/syslog.h>
     57   1.1  jonathan #include <sys/kernel.h>
     58   1.1  jonathan /*#include <sys/random.h>*/
     59   1.1  jonathan #include <sys/sysctl.h>
     60  1.23  drochner #include <sys/socketvar.h> /* for softnet_lock */
     61   1.1  jonathan 
     62   1.1  jonathan #include <net/if.h>
     63   1.1  jonathan 
     64   1.1  jonathan #include <netinet/in.h>
     65   1.1  jonathan #include <netinet/in_systm.h>
     66   1.1  jonathan #include <netinet/ip.h>
     67   1.1  jonathan #include <netinet/ip_ecn.h>
     68   1.1  jonathan #include <netinet/ip6.h>
     69   1.1  jonathan 
     70   1.1  jonathan #include <net/route.h>
     71   1.1  jonathan #include <netipsec/ipsec.h>
     72  1.18   thorpej #include <netipsec/ipsec_private.h>
     73   1.1  jonathan #include <netipsec/ah.h>
     74   1.1  jonathan #include <netipsec/ah_var.h>
     75   1.1  jonathan #include <netipsec/esp.h>
     76   1.1  jonathan #include <netipsec/esp_var.h>
     77   1.1  jonathan #include <netipsec/xform.h>
     78   1.1  jonathan 
     79   1.1  jonathan #ifdef INET6
     80   1.1  jonathan #include <netinet6/ip6_var.h>
     81   1.1  jonathan #include <netipsec/ipsec6.h>
     82   1.5  jonathan #  ifdef __FreeBSD__
     83   1.5  jonathan #  include <netinet6/ip6_ecn.h>
     84   1.5  jonathan #  endif
     85   1.1  jonathan #endif
     86   1.1  jonathan 
     87   1.4       tls #include <netipsec/key.h>
     88   1.4       tls #include <netipsec/key_debug.h>
     89   1.1  jonathan 
     90   1.1  jonathan #include <netipsec/ipsec_osdep.h>
     91   1.1  jonathan 
     92   1.1  jonathan #include <opencrypto/cryptodev.h>
     93   1.1  jonathan #include <opencrypto/xform.h>
     94   1.1  jonathan 
     95  1.18   thorpej percpu_t *espstat_percpu;
     96  1.18   thorpej 
     97   1.1  jonathan int	esp_enable = 1;
     98   1.1  jonathan 
     99   1.1  jonathan #ifdef __FreeBSD__
    100   1.1  jonathan SYSCTL_DECL(_net_inet_esp);
    101   1.1  jonathan SYSCTL_INT(_net_inet_esp, OID_AUTO,
    102   1.1  jonathan 	esp_enable,	CTLFLAG_RW,	&esp_enable,	0, "");
    103   1.1  jonathan SYSCTL_STRUCT(_net_inet_esp, IPSECCTL_STATS,
    104   1.1  jonathan 	stats,		CTLFLAG_RD,	&espstat,	espstat, "");
    105   1.4       tls #endif /* __FreeBSD__ */
    106   1.1  jonathan 
    107   1.1  jonathan static	int esp_max_ivlen;		/* max iv length over all algorithms */
    108   1.1  jonathan 
    109   1.1  jonathan static int esp_input_cb(struct cryptop *op);
    110   1.1  jonathan static int esp_output_cb(struct cryptop *crp);
    111   1.1  jonathan 
    112  1.50  christos const uint8_t esp_stats[256] = { SADB_EALG_STATS_INIT };
    113  1.50  christos 
    114   1.1  jonathan /*
    115   1.1  jonathan  * NB: this is public for use by the PF_KEY support.
    116   1.1  jonathan  * NB: if you add support here; be sure to add code to esp_attach below!
    117   1.1  jonathan  */
    118  1.28  drochner const struct enc_xform *
    119   1.1  jonathan esp_algorithm_lookup(int alg)
    120   1.1  jonathan {
    121  1.49     ozaki 
    122   1.1  jonathan 	switch (alg) {
    123   1.1  jonathan 	case SADB_EALG_DESCBC:
    124   1.1  jonathan 		return &enc_xform_des;
    125   1.1  jonathan 	case SADB_EALG_3DESCBC:
    126   1.1  jonathan 		return &enc_xform_3des;
    127   1.1  jonathan 	case SADB_X_EALG_AES:
    128   1.1  jonathan 		return &enc_xform_rijndael128;
    129   1.1  jonathan 	case SADB_X_EALG_BLOWFISHCBC:
    130   1.1  jonathan 		return &enc_xform_blf;
    131   1.1  jonathan 	case SADB_X_EALG_CAST128CBC:
    132   1.1  jonathan 		return &enc_xform_cast5;
    133   1.1  jonathan 	case SADB_X_EALG_SKIPJACK:
    134   1.1  jonathan 		return &enc_xform_skipjack;
    135  1.32  drochner 	case SADB_X_EALG_CAMELLIACBC:
    136  1.32  drochner 		return &enc_xform_camellia;
    137  1.36  drochner 	case SADB_X_EALG_AESCTR:
    138  1.36  drochner 		return &enc_xform_aes_ctr;
    139  1.38  drochner 	case SADB_X_EALG_AESGCM16:
    140  1.38  drochner 		return &enc_xform_aes_gcm;
    141  1.38  drochner 	case SADB_X_EALG_AESGMAC:
    142  1.38  drochner 		return &enc_xform_aes_gmac;
    143   1.1  jonathan 	case SADB_EALG_NULL:
    144   1.1  jonathan 		return &enc_xform_null;
    145   1.1  jonathan 	}
    146   1.1  jonathan 	return NULL;
    147   1.1  jonathan }
    148   1.1  jonathan 
    149   1.1  jonathan size_t
    150  1.28  drochner esp_hdrsiz(const struct secasvar *sav)
    151   1.1  jonathan {
    152   1.1  jonathan 	size_t size;
    153   1.1  jonathan 
    154   1.1  jonathan 	if (sav != NULL) {
    155   1.1  jonathan 		/*XXX not right for null algorithm--does it matter??*/
    156   1.1  jonathan 		IPSEC_ASSERT(sav->tdb_encalgxform != NULL,
    157   1.1  jonathan 			("esp_hdrsiz: SA with null xform"));
    158   1.1  jonathan 		if (sav->flags & SADB_X_EXT_OLD)
    159   1.1  jonathan 			size = sizeof (struct esp);
    160   1.1  jonathan 		else
    161   1.1  jonathan 			size = sizeof (struct newesp);
    162  1.35  drochner 		size += sav->tdb_encalgxform->ivsize + 9;
    163   1.1  jonathan 		/*XXX need alg check???*/
    164   1.1  jonathan 		if (sav->tdb_authalgxform != NULL && sav->replay)
    165   1.1  jonathan 			size += ah_hdrsiz(sav);
    166   1.1  jonathan 	} else {
    167   1.1  jonathan 		/*
    168   1.1  jonathan 		 *   base header size
    169   1.1  jonathan 		 * + max iv length for CBC mode
    170   1.1  jonathan 		 * + max pad length
    171   1.1  jonathan 		 * + sizeof (pad length field)
    172   1.1  jonathan 		 * + sizeof (next header field)
    173   1.1  jonathan 		 * + max icv supported.
    174   1.1  jonathan 		 */
    175   1.1  jonathan 		size = sizeof (struct newesp) + esp_max_ivlen + 9 + 16;
    176   1.1  jonathan 	}
    177   1.1  jonathan 	return size;
    178   1.1  jonathan }
    179   1.1  jonathan 
    180   1.1  jonathan /*
    181   1.1  jonathan  * esp_init() is called when an SPI is being set up.
    182   1.1  jonathan  */
    183   1.1  jonathan static int
    184  1.28  drochner esp_init(struct secasvar *sav, const struct xformsw *xsp)
    185   1.1  jonathan {
    186  1.28  drochner 	const struct enc_xform *txform;
    187  1.48  christos 	struct cryptoini cria, crie, *cr;
    188   1.1  jonathan 	int keylen;
    189   1.1  jonathan 	int error;
    190   1.1  jonathan 
    191   1.1  jonathan 	txform = esp_algorithm_lookup(sav->alg_enc);
    192   1.1  jonathan 	if (txform == NULL) {
    193  1.48  christos 		DPRINTF(("%s: unsupported encryption algorithm %d\n", __func__,
    194  1.48  christos 		    sav->alg_enc));
    195   1.1  jonathan 		return EINVAL;
    196   1.1  jonathan 	}
    197   1.1  jonathan 	if (sav->key_enc == NULL) {
    198  1.48  christos 		DPRINTF(("%s: no encoding key for %s algorithm\n", __func__,
    199  1.48  christos 		    txform->name));
    200   1.1  jonathan 		return EINVAL;
    201   1.1  jonathan 	}
    202   1.1  jonathan 	if ((sav->flags&(SADB_X_EXT_OLD|SADB_X_EXT_IV4B)) == SADB_X_EXT_IV4B) {
    203  1.48  christos 		DPRINTF(("%s: 4-byte IV not supported with protocol\n",
    204  1.48  christos 		    __func__));
    205   1.1  jonathan 		return EINVAL;
    206   1.1  jonathan 	}
    207   1.1  jonathan 	keylen = _KEYLEN(sav->key_enc);
    208   1.1  jonathan 	if (txform->minkey > keylen || keylen > txform->maxkey) {
    209  1.48  christos 		DPRINTF(("%s: invalid key length %u, must be in "
    210  1.48  christos 		    "the range [%u..%u] for algorithm %s\n", __func__,
    211  1.48  christos 		    keylen, txform->minkey, txform->maxkey, txform->name));
    212   1.1  jonathan 		return EINVAL;
    213   1.1  jonathan 	}
    214   1.1  jonathan 
    215  1.35  drochner 	sav->ivlen = txform->ivsize;
    216   1.1  jonathan 
    217   1.1  jonathan 	/*
    218   1.1  jonathan 	 * Setup AH-related state.
    219   1.1  jonathan 	 */
    220   1.1  jonathan 	if (sav->alg_auth != 0) {
    221   1.1  jonathan 		error = ah_init0(sav, xsp, &cria);
    222   1.1  jonathan 		if (error)
    223   1.1  jonathan 			return error;
    224   1.1  jonathan 	}
    225   1.1  jonathan 
    226   1.1  jonathan 	/* NB: override anything set in ah_init0 */
    227   1.1  jonathan 	sav->tdb_xform = xsp;
    228   1.1  jonathan 	sav->tdb_encalgxform = txform;
    229   1.1  jonathan 
    230  1.48  christos 	switch (sav->alg_enc) {
    231  1.48  christos 	case SADB_X_EALG_AESGCM16:
    232  1.48  christos 	case SADB_X_EALG_AESGMAC:
    233  1.38  drochner 		switch (keylen) {
    234  1.38  drochner 		case 20:
    235  1.38  drochner 			sav->alg_auth = SADB_X_AALG_AES128GMAC;
    236  1.38  drochner 			sav->tdb_authalgxform = &auth_hash_gmac_aes_128;
    237  1.38  drochner 			break;
    238  1.38  drochner 		case 28:
    239  1.38  drochner 			sav->alg_auth = SADB_X_AALG_AES192GMAC;
    240  1.38  drochner 			sav->tdb_authalgxform = &auth_hash_gmac_aes_192;
    241  1.38  drochner 			break;
    242  1.38  drochner 		case 36:
    243  1.38  drochner 			sav->alg_auth = SADB_X_AALG_AES256GMAC;
    244  1.38  drochner 			sav->tdb_authalgxform = &auth_hash_gmac_aes_256;
    245  1.38  drochner 			break;
    246  1.48  christos 		default:
    247  1.48  christos 			DPRINTF(("%s: invalid key length %u, must be either of "
    248  1.48  christos 				"20, 28 or 36\n", __func__, keylen));
    249  1.48  christos 			return EINVAL;
    250  1.48  christos                 }
    251  1.48  christos 
    252  1.38  drochner 		memset(&cria, 0, sizeof(cria));
    253  1.38  drochner 		cria.cri_alg = sav->tdb_authalgxform->type;
    254  1.38  drochner 		cria.cri_klen = _KEYBITS(sav->key_enc);
    255  1.38  drochner 		cria.cri_key = _KEYBUF(sav->key_enc);
    256  1.48  christos 		break;
    257  1.48  christos 	default:
    258  1.48  christos 		break;
    259  1.38  drochner 	}
    260  1.38  drochner 
    261   1.1  jonathan 	/* Initialize crypto session. */
    262  1.20    cegger 	memset(&crie, 0, sizeof (crie));
    263   1.1  jonathan 	crie.cri_alg = sav->tdb_encalgxform->type;
    264   1.1  jonathan 	crie.cri_klen = _KEYBITS(sav->key_enc);
    265   1.1  jonathan 	crie.cri_key = _KEYBUF(sav->key_enc);
    266   1.1  jonathan 	/* XXX Rounds ? */
    267   1.1  jonathan 
    268   1.1  jonathan 	if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
    269   1.1  jonathan 		/* init both auth & enc */
    270   1.1  jonathan 		crie.cri_next = &cria;
    271  1.48  christos 		cr = &crie;
    272   1.1  jonathan 	} else if (sav->tdb_encalgxform) {
    273  1.48  christos 		cr = &crie;
    274   1.1  jonathan 	} else if (sav->tdb_authalgxform) {
    275  1.48  christos 		cr = &cria;
    276   1.1  jonathan 	} else {
    277   1.1  jonathan 		/* XXX cannot happen? */
    278  1.48  christos 		DPRINTF(("%s: no encoding OR authentication xform!\n",
    279  1.48  christos 		    __func__));
    280  1.48  christos 		return EINVAL;
    281   1.1  jonathan 	}
    282  1.48  christos 
    283  1.48  christos 	return crypto_newsession(&sav->tdb_cryptoid, cr, crypto_support);
    284   1.1  jonathan }
    285   1.1  jonathan 
    286   1.1  jonathan /*
    287   1.1  jonathan  * Paranoia.
    288   1.1  jonathan  */
    289   1.1  jonathan static int
    290   1.1  jonathan esp_zeroize(struct secasvar *sav)
    291   1.1  jonathan {
    292   1.1  jonathan 	/* NB: ah_zerorize free's the crypto session state */
    293   1.1  jonathan 	int error = ah_zeroize(sav);
    294   1.1  jonathan 
    295   1.1  jonathan 	if (sav->key_enc)
    296  1.20    cegger 		memset(_KEYBUF(sav->key_enc), 0, _KEYLEN(sav->key_enc));
    297   1.1  jonathan 	sav->tdb_encalgxform = NULL;
    298   1.1  jonathan 	sav->tdb_xform = NULL;
    299   1.1  jonathan 	return error;
    300   1.1  jonathan }
    301   1.1  jonathan 
    302   1.1  jonathan /*
    303   1.1  jonathan  * ESP input processing, called (eventually) through the protocol switch.
    304   1.1  jonathan  */
    305   1.1  jonathan static int
    306  1.27  drochner esp_input(struct mbuf *m, const struct secasvar *sav, int skip, int protoff)
    307   1.1  jonathan {
    308  1.28  drochner 	const struct auth_hash *esph;
    309  1.28  drochner 	const struct enc_xform *espx;
    310   1.1  jonathan 	struct tdb_ident *tdbi;
    311   1.1  jonathan 	struct tdb_crypto *tc;
    312  1.40  drochner 	int plen, alen, hlen, error;
    313   1.1  jonathan 	struct m_tag *mtag;
    314   1.1  jonathan 	struct newesp *esp;
    315   1.1  jonathan 
    316   1.1  jonathan 	struct cryptodesc *crde;
    317   1.1  jonathan 	struct cryptop *crp;
    318   1.1  jonathan 
    319   1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("esp_input");
    320   1.1  jonathan 
    321   1.1  jonathan 	IPSEC_ASSERT(sav != NULL, ("esp_input: null SA"));
    322   1.1  jonathan 	IPSEC_ASSERT(sav->tdb_encalgxform != NULL,
    323   1.1  jonathan 		("esp_input: null encoding xform"));
    324   1.1  jonathan 	IPSEC_ASSERT((skip&3) == 0 && (m->m_pkthdr.len&3) == 0,
    325   1.1  jonathan 		("esp_input: misaligned packet, skip %u pkt len %u",
    326   1.1  jonathan 			skip, m->m_pkthdr.len));
    327   1.1  jonathan 
    328   1.1  jonathan 	/* XXX don't pullup, just copy header */
    329   1.1  jonathan 	IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof (struct newesp));
    330   1.1  jonathan 
    331   1.1  jonathan 	esph = sav->tdb_authalgxform;
    332   1.1  jonathan 	espx = sav->tdb_encalgxform;
    333   1.1  jonathan 
    334   1.1  jonathan 	/* Determine the ESP header length */
    335   1.1  jonathan 	if (sav->flags & SADB_X_EXT_OLD)
    336   1.1  jonathan 		hlen = sizeof (struct esp) + sav->ivlen;
    337   1.1  jonathan 	else
    338   1.1  jonathan 		hlen = sizeof (struct newesp) + sav->ivlen;
    339   1.1  jonathan 	/* Authenticator hash size */
    340  1.30  drochner 	alen = esph ? esph->authsize : 0;
    341   1.1  jonathan 
    342   1.1  jonathan 	/*
    343   1.1  jonathan 	 * Verify payload length is multiple of encryption algorithm
    344   1.1  jonathan 	 * block size.
    345   1.1  jonathan 	 *
    346   1.1  jonathan 	 * NB: This works for the null algorithm because the blocksize
    347   1.1  jonathan 	 *     is 4 and all packets must be 4-byte aligned regardless
    348   1.1  jonathan 	 *     of the algorithm.
    349   1.1  jonathan 	 */
    350   1.1  jonathan 	plen = m->m_pkthdr.len - (skip + hlen + alen);
    351   1.1  jonathan 	if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
    352   1.1  jonathan 		DPRINTF(("esp_input: "
    353   1.1  jonathan 		    "payload of %d octets not a multiple of %d octets,"
    354   1.1  jonathan 		    "  SA %s/%08lx\n",
    355   1.1  jonathan 		    plen, espx->blocksize,
    356   1.1  jonathan 		    ipsec_address(&sav->sah->saidx.dst),
    357   1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    358  1.18   thorpej 		ESP_STATINC(ESP_STAT_BADILEN);
    359   1.1  jonathan 		m_freem(m);
    360   1.1  jonathan 		return EINVAL;
    361   1.1  jonathan 	}
    362   1.1  jonathan 
    363   1.1  jonathan 	/*
    364   1.1  jonathan 	 * Check sequence number.
    365   1.1  jonathan 	 */
    366   1.1  jonathan 	if (esph && sav->replay && !ipsec_chkreplay(ntohl(esp->esp_seq), sav)) {
    367   1.1  jonathan 		DPRINTF(("esp_input: packet replay check for %s\n",
    368   1.1  jonathan 		    ipsec_logsastr(sav)));	/*XXX*/
    369  1.18   thorpej 		ESP_STATINC(ESP_STAT_REPLAY);
    370   1.1  jonathan 		m_freem(m);
    371   1.1  jonathan 		return ENOBUFS;		/*XXX*/
    372   1.1  jonathan 	}
    373   1.1  jonathan 
    374   1.1  jonathan 	/* Update the counters */
    375  1.18   thorpej 	ESP_STATADD(ESP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen - alen);
    376   1.1  jonathan 
    377   1.1  jonathan 	/* Find out if we've already done crypto */
    378   1.1  jonathan 	for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
    379   1.1  jonathan 	     mtag != NULL;
    380   1.1  jonathan 	     mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
    381   1.1  jonathan 		tdbi = (struct tdb_ident *) (mtag + 1);
    382   1.1  jonathan 		if (tdbi->proto == sav->sah->saidx.proto &&
    383   1.1  jonathan 		    tdbi->spi == sav->spi &&
    384  1.19    cegger 		    !memcmp(&tdbi->dst, &sav->sah->saidx.dst,
    385   1.1  jonathan 			  sizeof(union sockaddr_union)))
    386   1.1  jonathan 			break;
    387   1.1  jonathan 	}
    388   1.1  jonathan 
    389   1.1  jonathan 	/* Get crypto descriptors */
    390   1.1  jonathan 	crp = crypto_getreq(esph && espx ? 2 : 1);
    391   1.1  jonathan 	if (crp == NULL) {
    392   1.1  jonathan 		DPRINTF(("esp_input: failed to acquire crypto descriptors\n"));
    393  1.18   thorpej 		ESP_STATINC(ESP_STAT_CRYPTO);
    394   1.1  jonathan 		m_freem(m);
    395   1.1  jonathan 		return ENOBUFS;
    396   1.1  jonathan 	}
    397   1.1  jonathan 
    398   1.1  jonathan 	/* Get IPsec-specific opaque pointer */
    399   1.1  jonathan 	if (esph == NULL || mtag != NULL)
    400   1.1  jonathan 		tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
    401   1.1  jonathan 		    M_XDATA, M_NOWAIT|M_ZERO);
    402   1.1  jonathan 	else
    403   1.1  jonathan 		tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto) + alen,
    404   1.1  jonathan 		    M_XDATA, M_NOWAIT|M_ZERO);
    405   1.1  jonathan 	if (tc == NULL) {
    406   1.1  jonathan 		crypto_freereq(crp);
    407   1.1  jonathan 		DPRINTF(("esp_input: failed to allocate tdb_crypto\n"));
    408  1.18   thorpej 		ESP_STATINC(ESP_STAT_CRYPTO);
    409   1.1  jonathan 		m_freem(m);
    410   1.1  jonathan 		return ENOBUFS;
    411   1.1  jonathan 	}
    412   1.1  jonathan 
    413  1.40  drochner 	error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
    414  1.40  drochner 	if (error) {
    415  1.40  drochner 		m_freem(m);
    416  1.40  drochner 		free(tc, M_XDATA);
    417  1.40  drochner 		crypto_freereq(crp);
    418  1.40  drochner 		DPRINTF(("esp_input: m_makewritable failed\n"));
    419  1.40  drochner 		ESP_STATINC(ESP_STAT_CRYPTO);
    420  1.40  drochner 		return error;
    421  1.40  drochner 	}
    422  1.40  drochner 
    423  1.15  degroote 	tc->tc_ptr = mtag;
    424   1.1  jonathan 
    425   1.1  jonathan 	if (esph) {
    426   1.1  jonathan 		struct cryptodesc *crda = crp->crp_desc;
    427   1.1  jonathan 
    428   1.1  jonathan 		IPSEC_ASSERT(crda != NULL, ("esp_input: null ah crypto descriptor"));
    429   1.1  jonathan 
    430   1.1  jonathan 		/* Authentication descriptor */
    431   1.1  jonathan 		crda->crd_skip = skip;
    432  1.38  drochner 		if (espx && espx->type == CRYPTO_AES_GCM_16)
    433  1.38  drochner 			crda->crd_len = hlen - sav->ivlen;
    434  1.38  drochner 		else
    435  1.38  drochner 			crda->crd_len = m->m_pkthdr.len - (skip + alen);
    436   1.1  jonathan 		crda->crd_inject = m->m_pkthdr.len - alen;
    437   1.1  jonathan 
    438   1.1  jonathan 		crda->crd_alg = esph->type;
    439  1.38  drochner 		if (espx && (espx->type == CRYPTO_AES_GCM_16 ||
    440  1.38  drochner 			     espx->type == CRYPTO_AES_GMAC)) {
    441  1.38  drochner 			crda->crd_key = _KEYBUF(sav->key_enc);
    442  1.38  drochner 			crda->crd_klen = _KEYBITS(sav->key_enc);
    443  1.38  drochner 		} else {
    444  1.38  drochner 			crda->crd_key = _KEYBUF(sav->key_auth);
    445  1.38  drochner 			crda->crd_klen = _KEYBITS(sav->key_auth);
    446  1.38  drochner 		}
    447   1.1  jonathan 
    448   1.1  jonathan 		/* Copy the authenticator */
    449   1.1  jonathan 		if (mtag == NULL)
    450   1.1  jonathan 			m_copydata(m, m->m_pkthdr.len - alen, alen,
    451  1.15  degroote 				      (tc + 1));
    452   1.1  jonathan 
    453   1.1  jonathan 		/* Chain authentication request */
    454   1.1  jonathan 		crde = crda->crd_next;
    455   1.1  jonathan 	} else {
    456   1.1  jonathan 		crde = crp->crp_desc;
    457   1.1  jonathan 	}
    458   1.1  jonathan 
    459   1.1  jonathan 	/* Crypto operation descriptor */
    460   1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
    461   1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    462  1.15  degroote 	crp->crp_buf = m;
    463   1.1  jonathan 	crp->crp_callback = esp_input_cb;
    464   1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    465  1.15  degroote 	crp->crp_opaque = tc;
    466   1.1  jonathan 
    467   1.1  jonathan 	/* These are passed as-is to the callback */
    468   1.1  jonathan 	tc->tc_spi = sav->spi;
    469   1.1  jonathan 	tc->tc_dst = sav->sah->saidx.dst;
    470   1.1  jonathan 	tc->tc_proto = sav->sah->saidx.proto;
    471   1.1  jonathan 	tc->tc_protoff = protoff;
    472   1.1  jonathan 	tc->tc_skip = skip;
    473   1.1  jonathan 
    474   1.1  jonathan 	/* Decryption descriptor */
    475   1.1  jonathan 	if (espx) {
    476   1.1  jonathan 		IPSEC_ASSERT(crde != NULL, ("esp_input: null esp crypto descriptor"));
    477   1.1  jonathan 		crde->crd_skip = skip + hlen;
    478  1.38  drochner 		if (espx->type == CRYPTO_AES_GMAC)
    479  1.38  drochner 			crde->crd_len = 0;
    480  1.38  drochner 		else
    481  1.38  drochner 			crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
    482   1.1  jonathan 		crde->crd_inject = skip + hlen - sav->ivlen;
    483   1.1  jonathan 
    484   1.1  jonathan 		crde->crd_alg = espx->type;
    485   1.1  jonathan 		crde->crd_key = _KEYBUF(sav->key_enc);
    486   1.1  jonathan 		crde->crd_klen = _KEYBITS(sav->key_enc);
    487   1.1  jonathan 		/* XXX Rounds ? */
    488   1.1  jonathan 	}
    489   1.1  jonathan 
    490   1.1  jonathan 	if (mtag == NULL)
    491   1.1  jonathan 		return crypto_dispatch(crp);
    492   1.1  jonathan 	else
    493   1.1  jonathan 		return esp_input_cb(crp);
    494   1.1  jonathan }
    495   1.1  jonathan 
    496   1.1  jonathan #ifdef INET6
    497   1.1  jonathan #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
    498   1.1  jonathan 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    499   1.1  jonathan 		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
    500   1.1  jonathan 	} else {							     \
    501   1.1  jonathan 		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
    502   1.1  jonathan 	}								     \
    503   1.1  jonathan } while (0)
    504   1.1  jonathan #else
    505   1.1  jonathan #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
    506   1.1  jonathan 	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
    507   1.1  jonathan #endif
    508   1.1  jonathan 
    509   1.1  jonathan /*
    510   1.1  jonathan  * ESP input callback from the crypto driver.
    511   1.1  jonathan  */
    512   1.1  jonathan static int
    513   1.1  jonathan esp_input_cb(struct cryptop *crp)
    514   1.1  jonathan {
    515  1.30  drochner 	u_int8_t lastthree[3], aalg[AH_ALEN_MAX];
    516   1.1  jonathan 	int s, hlen, skip, protoff, error;
    517   1.1  jonathan 	struct mbuf *m;
    518  1.45       mrg 	struct cryptodesc *crd __diagused;
    519  1.28  drochner 	const struct auth_hash *esph;
    520   1.1  jonathan 	struct tdb_crypto *tc;
    521   1.1  jonathan 	struct m_tag *mtag;
    522   1.1  jonathan 	struct secasvar *sav;
    523   1.1  jonathan 	struct secasindex *saidx;
    524  1.13  christos 	void *ptr;
    525  1.42  christos 	u_int16_t dport;
    526  1.42  christos 	u_int16_t sport;
    527   1.1  jonathan 
    528   1.1  jonathan 	crd = crp->crp_desc;
    529   1.1  jonathan 	IPSEC_ASSERT(crd != NULL, ("esp_input_cb: null crypto descriptor!"));
    530   1.1  jonathan 
    531   1.1  jonathan 	tc = (struct tdb_crypto *) crp->crp_opaque;
    532   1.1  jonathan 	IPSEC_ASSERT(tc != NULL, ("esp_input_cb: null opaque crypto data area!"));
    533   1.1  jonathan 	skip = tc->tc_skip;
    534   1.1  jonathan 	protoff = tc->tc_protoff;
    535   1.1  jonathan 	mtag = (struct m_tag *) tc->tc_ptr;
    536   1.1  jonathan 	m = (struct mbuf *) crp->crp_buf;
    537   1.1  jonathan 
    538  1.16  degroote 	/* find the source port for NAT-T */
    539  1.42  christos 	nat_t_ports_get(m, &dport, &sport);
    540  1.16  degroote 
    541  1.24  drochner 	s = splsoftnet();
    542  1.23  drochner 	mutex_enter(softnet_lock);
    543   1.1  jonathan 
    544  1.16  degroote 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, sport, dport);
    545   1.1  jonathan 	if (sav == NULL) {
    546  1.18   thorpej 		ESP_STATINC(ESP_STAT_NOTDB);
    547   1.1  jonathan 		DPRINTF(("esp_input_cb: SA expired while in crypto "
    548   1.1  jonathan 		    "(SA %s/%08lx proto %u)\n", ipsec_address(&tc->tc_dst),
    549   1.1  jonathan 		    (u_long) ntohl(tc->tc_spi), tc->tc_proto));
    550   1.1  jonathan 		error = ENOBUFS;		/*XXX*/
    551   1.1  jonathan 		goto bad;
    552   1.1  jonathan 	}
    553   1.1  jonathan 
    554   1.1  jonathan 	saidx = &sav->sah->saidx;
    555   1.1  jonathan 	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
    556   1.1  jonathan 		saidx->dst.sa.sa_family == AF_INET6,
    557  1.33  drochner 		("esp_input_cb: unexpected protocol family %u",
    558   1.1  jonathan 		 saidx->dst.sa.sa_family));
    559   1.1  jonathan 
    560   1.1  jonathan 	esph = sav->tdb_authalgxform;
    561   1.1  jonathan 
    562   1.1  jonathan 	/* Check for crypto errors */
    563   1.1  jonathan 	if (crp->crp_etype) {
    564   1.1  jonathan 		/* Reset the session ID */
    565   1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    566   1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    567   1.1  jonathan 
    568   1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    569   1.1  jonathan 			KEY_FREESAV(&sav);
    570  1.24  drochner 			mutex_exit(softnet_lock);
    571   1.1  jonathan 			splx(s);
    572   1.1  jonathan 			return crypto_dispatch(crp);
    573   1.1  jonathan 		}
    574   1.1  jonathan 
    575  1.18   thorpej 		ESP_STATINC(ESP_STAT_NOXFORM);
    576   1.1  jonathan 		DPRINTF(("esp_input_cb: crypto error %d\n", crp->crp_etype));
    577   1.1  jonathan 		error = crp->crp_etype;
    578   1.1  jonathan 		goto bad;
    579   1.1  jonathan 	}
    580   1.1  jonathan 
    581   1.1  jonathan 	/* Shouldn't happen... */
    582   1.1  jonathan 	if (m == NULL) {
    583  1.18   thorpej 		ESP_STATINC(ESP_STAT_CRYPTO);
    584   1.1  jonathan 		DPRINTF(("esp_input_cb: bogus returned buffer from crypto\n"));
    585   1.1  jonathan 		error = EINVAL;
    586   1.1  jonathan 		goto bad;
    587   1.1  jonathan 	}
    588  1.50  christos 	ESP_STATINC(ESP_STAT_HIST + esp_stats[sav->alg_enc]);
    589   1.1  jonathan 
    590   1.1  jonathan 	/* If authentication was performed, check now. */
    591   1.1  jonathan 	if (esph != NULL) {
    592   1.1  jonathan 		/*
    593   1.1  jonathan 		 * If we have a tag, it means an IPsec-aware NIC did
    594   1.1  jonathan 		 * the verification for us.  Otherwise we need to
    595   1.1  jonathan 		 * check the authentication calculation.
    596   1.1  jonathan 		 */
    597  1.50  christos 		AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
    598   1.1  jonathan 		if (mtag == NULL) {
    599   1.1  jonathan 			/* Copy the authenticator from the packet */
    600   1.1  jonathan 			m_copydata(m, m->m_pkthdr.len - esph->authsize,
    601   1.1  jonathan 				esph->authsize, aalg);
    602   1.1  jonathan 
    603  1.15  degroote 			ptr = (tc + 1);
    604   1.1  jonathan 
    605   1.1  jonathan 			/* Verify authenticator */
    606  1.44  riastrad 			if (!consttime_memequal(ptr, aalg, esph->authsize)) {
    607   1.1  jonathan 				DPRINTF(("esp_input_cb: "
    608   1.1  jonathan 		    "authentication hash mismatch for packet in SA %s/%08lx\n",
    609   1.1  jonathan 				    ipsec_address(&saidx->dst),
    610   1.1  jonathan 				    (u_long) ntohl(sav->spi)));
    611  1.18   thorpej 				ESP_STATINC(ESP_STAT_BADAUTH);
    612   1.1  jonathan 				error = EACCES;
    613   1.1  jonathan 				goto bad;
    614   1.1  jonathan 			}
    615   1.1  jonathan 		}
    616   1.1  jonathan 
    617   1.1  jonathan 		/* Remove trailing authenticator */
    618   1.1  jonathan 		m_adj(m, -(esph->authsize));
    619   1.1  jonathan 	}
    620   1.1  jonathan 
    621   1.1  jonathan 	/* Release the crypto descriptors */
    622   1.1  jonathan 	free(tc, M_XDATA), tc = NULL;
    623   1.1  jonathan 	crypto_freereq(crp), crp = NULL;
    624   1.1  jonathan 
    625   1.1  jonathan 	/*
    626   1.1  jonathan 	 * Packet is now decrypted.
    627   1.1  jonathan 	 */
    628   1.1  jonathan 	m->m_flags |= M_DECRYPTED;
    629   1.1  jonathan 
    630   1.8    rpaulo 	/*
    631   1.8    rpaulo 	 * Update replay sequence number, if appropriate.
    632   1.8    rpaulo 	 */
    633   1.8    rpaulo 	if (sav->replay) {
    634   1.8    rpaulo 		u_int32_t seq;
    635   1.8    rpaulo 
    636   1.8    rpaulo 		m_copydata(m, skip + offsetof(struct newesp, esp_seq),
    637  1.15  degroote 		    sizeof (seq), &seq);
    638   1.8    rpaulo 		if (ipsec_updatereplay(ntohl(seq), sav)) {
    639   1.8    rpaulo 			DPRINTF(("%s: packet replay check for %s\n", __func__,
    640   1.8    rpaulo 			    ipsec_logsastr(sav)));
    641  1.18   thorpej 			ESP_STATINC(ESP_STAT_REPLAY);
    642   1.8    rpaulo 			error = ENOBUFS;
    643   1.8    rpaulo 			goto bad;
    644   1.8    rpaulo 		}
    645   1.8    rpaulo 	}
    646   1.8    rpaulo 
    647   1.1  jonathan 	/* Determine the ESP header length */
    648   1.1  jonathan 	if (sav->flags & SADB_X_EXT_OLD)
    649   1.1  jonathan 		hlen = sizeof (struct esp) + sav->ivlen;
    650   1.1  jonathan 	else
    651   1.1  jonathan 		hlen = sizeof (struct newesp) + sav->ivlen;
    652   1.1  jonathan 
    653   1.1  jonathan 	/* Remove the ESP header and IV from the mbuf. */
    654   1.1  jonathan 	error = m_striphdr(m, skip, hlen);
    655   1.1  jonathan 	if (error) {
    656  1.18   thorpej 		ESP_STATINC(ESP_STAT_HDROPS);
    657   1.1  jonathan 		DPRINTF(("esp_input_cb: bad mbuf chain, SA %s/%08lx\n",
    658   1.1  jonathan 		    ipsec_address(&sav->sah->saidx.dst),
    659   1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    660   1.1  jonathan 		goto bad;
    661   1.1  jonathan 	}
    662   1.1  jonathan 
    663   1.1  jonathan 	/* Save the last three bytes of decrypted data */
    664   1.1  jonathan 	m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
    665   1.1  jonathan 
    666   1.1  jonathan 	/* Verify pad length */
    667   1.1  jonathan 	if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
    668  1.18   thorpej 		ESP_STATINC(ESP_STAT_BADILEN);
    669   1.1  jonathan 		DPRINTF(("esp_input_cb: invalid padding length %d "
    670   1.1  jonathan 			 "for %u byte packet in SA %s/%08lx\n",
    671   1.1  jonathan 			 lastthree[1], m->m_pkthdr.len - skip,
    672   1.1  jonathan 			 ipsec_address(&sav->sah->saidx.dst),
    673   1.1  jonathan 			 (u_long) ntohl(sav->spi)));
    674   1.1  jonathan 		error = EINVAL;
    675   1.1  jonathan 		goto bad;
    676   1.1  jonathan 	}
    677   1.1  jonathan 
    678   1.1  jonathan 	/* Verify correct decryption by checking the last padding bytes */
    679   1.1  jonathan 	if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
    680   1.1  jonathan 		if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
    681  1.18   thorpej 			ESP_STATINC(ESP_STAT_BADENC);
    682   1.1  jonathan 			DPRINTF(("esp_input_cb: decryption failed "
    683   1.1  jonathan 				"for packet in SA %s/%08lx\n",
    684   1.1  jonathan 				ipsec_address(&sav->sah->saidx.dst),
    685   1.1  jonathan 				(u_long) ntohl(sav->spi)));
    686   1.1  jonathan DPRINTF(("esp_input_cb: %x %x\n", lastthree[0], lastthree[1]));
    687   1.1  jonathan 			error = EINVAL;
    688   1.1  jonathan 			goto bad;
    689   1.1  jonathan 		}
    690   1.1  jonathan 	}
    691   1.1  jonathan 
    692   1.1  jonathan 	/* Trim the mbuf chain to remove trailing authenticator and padding */
    693   1.1  jonathan 	m_adj(m, -(lastthree[1] + 2));
    694   1.1  jonathan 
    695   1.1  jonathan 	/* Restore the Next Protocol field */
    696  1.40  drochner 	m_copyback(m, protoff, sizeof (u_int8_t), lastthree + 2);
    697   1.1  jonathan 
    698   1.1  jonathan 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
    699   1.1  jonathan 
    700   1.1  jonathan 	KEY_FREESAV(&sav);
    701  1.24  drochner 	mutex_exit(softnet_lock);
    702   1.1  jonathan 	splx(s);
    703   1.1  jonathan 	return error;
    704   1.1  jonathan bad:
    705   1.1  jonathan 	if (sav)
    706   1.1  jonathan 		KEY_FREESAV(&sav);
    707  1.24  drochner 	mutex_exit(softnet_lock);
    708   1.1  jonathan 	splx(s);
    709   1.1  jonathan 	if (m != NULL)
    710   1.1  jonathan 		m_freem(m);
    711   1.1  jonathan 	if (tc != NULL)
    712   1.1  jonathan 		free(tc, M_XDATA);
    713   1.1  jonathan 	if (crp != NULL)
    714   1.1  jonathan 		crypto_freereq(crp);
    715   1.1  jonathan 	return error;
    716   1.1  jonathan }
    717   1.1  jonathan 
    718   1.1  jonathan /*
    719   1.1  jonathan  * ESP output routine, called by ipsec[46]_process_packet().
    720   1.1  jonathan  */
    721   1.1  jonathan static int
    722   1.1  jonathan esp_output(
    723  1.11  christos     struct mbuf *m,
    724  1.11  christos     struct ipsecrequest *isr,
    725  1.12  christos     struct mbuf **mp,
    726  1.11  christos     int skip,
    727  1.11  christos     int protoff
    728   1.1  jonathan )
    729   1.1  jonathan {
    730  1.28  drochner 	const struct enc_xform *espx;
    731  1.28  drochner 	const struct auth_hash *esph;
    732  1.45       mrg 	int hlen, rlen, padding, blks, alen, i, roff;
    733  1.39    plunky 	struct mbuf *mo = NULL;
    734   1.1  jonathan 	struct tdb_crypto *tc;
    735  1.27  drochner 	const struct secasvar *sav;
    736   1.1  jonathan 	struct secasindex *saidx;
    737   1.1  jonathan 	unsigned char *pad;
    738   1.1  jonathan 	u_int8_t prot;
    739   1.1  jonathan 	int error, maxpacketsize;
    740   1.1  jonathan 
    741   1.1  jonathan 	struct cryptodesc *crde = NULL, *crda = NULL;
    742   1.1  jonathan 	struct cryptop *crp;
    743   1.1  jonathan 
    744   1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("esp_output");
    745   1.1  jonathan 
    746   1.1  jonathan 	sav = isr->sav;
    747   1.1  jonathan 	IPSEC_ASSERT(sav != NULL, ("esp_output: null SA"));
    748   1.1  jonathan 	esph = sav->tdb_authalgxform;
    749   1.1  jonathan 	espx = sav->tdb_encalgxform;
    750   1.1  jonathan 	IPSEC_ASSERT(espx != NULL, ("esp_output: null encoding xform"));
    751   1.1  jonathan 
    752   1.1  jonathan 	if (sav->flags & SADB_X_EXT_OLD)
    753   1.1  jonathan 		hlen = sizeof (struct esp) + sav->ivlen;
    754   1.1  jonathan 	else
    755   1.1  jonathan 		hlen = sizeof (struct newesp) + sav->ivlen;
    756   1.1  jonathan 
    757   1.1  jonathan 	rlen = m->m_pkthdr.len - skip;	/* Raw payload length. */
    758   1.1  jonathan 	/*
    759   1.1  jonathan 	 * NB: The null encoding transform has a blocksize of 4
    760   1.1  jonathan 	 *     so that headers are properly aligned.
    761   1.1  jonathan 	 */
    762   1.1  jonathan 	blks = espx->blocksize;		/* IV blocksize */
    763   1.1  jonathan 
    764   1.1  jonathan 	/* XXX clamp padding length a la KAME??? */
    765   1.1  jonathan 	padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
    766   1.1  jonathan 
    767   1.1  jonathan 	if (esph)
    768  1.30  drochner 		alen = esph->authsize;
    769   1.1  jonathan 	else
    770   1.1  jonathan 		alen = 0;
    771   1.1  jonathan 
    772  1.18   thorpej 	ESP_STATINC(ESP_STAT_OUTPUT);
    773   1.1  jonathan 
    774   1.1  jonathan 	saidx = &sav->sah->saidx;
    775   1.1  jonathan 	/* Check for maximum packet size violations. */
    776   1.1  jonathan 	switch (saidx->dst.sa.sa_family) {
    777   1.1  jonathan #ifdef INET
    778   1.1  jonathan 	case AF_INET:
    779   1.1  jonathan 		maxpacketsize = IP_MAXPACKET;
    780   1.1  jonathan 		break;
    781   1.1  jonathan #endif /* INET */
    782   1.1  jonathan #ifdef INET6
    783   1.1  jonathan 	case AF_INET6:
    784   1.1  jonathan 		maxpacketsize = IPV6_MAXPACKET;
    785   1.1  jonathan 		break;
    786   1.1  jonathan #endif /* INET6 */
    787   1.1  jonathan 	default:
    788   1.1  jonathan 		DPRINTF(("esp_output: unknown/unsupported protocol "
    789   1.1  jonathan 		    "family %d, SA %s/%08lx\n",
    790   1.1  jonathan 		    saidx->dst.sa.sa_family, ipsec_address(&saidx->dst),
    791   1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    792  1.18   thorpej 		ESP_STATINC(ESP_STAT_NOPF);
    793   1.1  jonathan 		error = EPFNOSUPPORT;
    794   1.1  jonathan 		goto bad;
    795   1.1  jonathan 	}
    796   1.1  jonathan 	if (skip + hlen + rlen + padding + alen > maxpacketsize) {
    797   1.1  jonathan 		DPRINTF(("esp_output: packet in SA %s/%08lx got too big "
    798   1.1  jonathan 		    "(len %u, max len %u)\n",
    799   1.1  jonathan 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi),
    800   1.1  jonathan 		    skip + hlen + rlen + padding + alen, maxpacketsize));
    801  1.18   thorpej 		ESP_STATINC(ESP_STAT_TOOBIG);
    802   1.1  jonathan 		error = EMSGSIZE;
    803   1.1  jonathan 		goto bad;
    804   1.1  jonathan 	}
    805   1.1  jonathan 
    806   1.1  jonathan 	/* Update the counters. */
    807  1.25  drochner 	ESP_STATADD(ESP_STAT_OBYTES, m->m_pkthdr.len - skip);
    808   1.1  jonathan 
    809   1.1  jonathan 	m = m_clone(m);
    810   1.1  jonathan 	if (m == NULL) {
    811   1.1  jonathan 		DPRINTF(("esp_output: cannot clone mbuf chain, SA %s/%08lx\n",
    812   1.1  jonathan 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
    813  1.18   thorpej 		ESP_STATINC(ESP_STAT_HDROPS);
    814   1.1  jonathan 		error = ENOBUFS;
    815   1.1  jonathan 		goto bad;
    816   1.1  jonathan 	}
    817   1.1  jonathan 
    818   1.1  jonathan 	/* Inject ESP header. */
    819   1.1  jonathan 	mo = m_makespace(m, skip, hlen, &roff);
    820   1.1  jonathan 	if (mo == NULL) {
    821   1.1  jonathan 		DPRINTF(("esp_output: failed to inject %u byte ESP hdr for SA "
    822   1.1  jonathan 		    "%s/%08lx\n",
    823   1.1  jonathan 		    hlen, ipsec_address(&saidx->dst),
    824   1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    825  1.18   thorpej 		ESP_STATINC(ESP_STAT_HDROPS);	/* XXX diffs from openbsd */
    826   1.1  jonathan 		error = ENOBUFS;
    827   1.1  jonathan 		goto bad;
    828   1.1  jonathan 	}
    829   1.1  jonathan 
    830   1.1  jonathan 	/* Initialize ESP header. */
    831  1.22    cegger 	memcpy(mtod(mo, char *) + roff, &sav->spi, sizeof(u_int32_t));
    832   1.1  jonathan 	if (sav->replay) {
    833   1.9    rpaulo 		u_int32_t replay;
    834   1.9    rpaulo 
    835   1.9    rpaulo #ifdef IPSEC_DEBUG
    836   1.9    rpaulo 		/* Emulate replay attack when ipsec_replay is TRUE. */
    837   1.9    rpaulo 		if (!ipsec_replay)
    838   1.9    rpaulo #endif
    839   1.9    rpaulo 			sav->replay->count++;
    840   1.9    rpaulo 
    841   1.9    rpaulo 		replay = htonl(sav->replay->count);
    842  1.15  degroote 		bcopy(&replay,
    843  1.14  degroote 		    mtod(mo,char *) + roff + sizeof(u_int32_t),
    844   1.1  jonathan 		    sizeof(u_int32_t));
    845   1.1  jonathan 	}
    846   1.1  jonathan 
    847   1.1  jonathan 	/*
    848   1.1  jonathan 	 * Add padding -- better to do it ourselves than use the crypto engine,
    849   1.1  jonathan 	 * although if/when we support compression, we'd have to do that.
    850   1.1  jonathan 	 */
    851   1.1  jonathan 	pad = (u_char *) m_pad(m, padding + alen);
    852   1.1  jonathan 	if (pad == NULL) {
    853   1.1  jonathan 		DPRINTF(("esp_output: m_pad failed for SA %s/%08lx\n",
    854   1.1  jonathan 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
    855   1.1  jonathan 		m = NULL;		/* NB: free'd by m_pad */
    856   1.1  jonathan 		error = ENOBUFS;
    857   1.1  jonathan 		goto bad;
    858   1.1  jonathan 	}
    859   1.1  jonathan 
    860   1.1  jonathan 	/*
    861   1.1  jonathan 	 * Add padding: random, zero, or self-describing.
    862   1.1  jonathan 	 * XXX catch unexpected setting
    863   1.1  jonathan 	 */
    864   1.1  jonathan 	switch (sav->flags & SADB_X_EXT_PMASK) {
    865   1.1  jonathan 	case SADB_X_EXT_PRAND:
    866   1.1  jonathan 		(void) read_random(pad, padding - 2);
    867   1.1  jonathan 		break;
    868   1.1  jonathan 	case SADB_X_EXT_PZERO:
    869  1.20    cegger 		memset(pad, 0, padding - 2);
    870   1.1  jonathan 		break;
    871   1.1  jonathan 	case SADB_X_EXT_PSEQ:
    872   1.1  jonathan 		for (i = 0; i < padding - 2; i++)
    873   1.1  jonathan 			pad[i] = i+1;
    874   1.1  jonathan 		break;
    875   1.1  jonathan 	}
    876   1.1  jonathan 
    877   1.1  jonathan 	/* Fix padding length and Next Protocol in padding itself. */
    878   1.1  jonathan 	pad[padding - 2] = padding - 2;
    879   1.1  jonathan 	m_copydata(m, protoff, sizeof(u_int8_t), pad + padding - 1);
    880   1.1  jonathan 
    881   1.1  jonathan 	/* Fix Next Protocol in IPv4/IPv6 header. */
    882   1.1  jonathan 	prot = IPPROTO_ESP;
    883   1.1  jonathan 	m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
    884   1.1  jonathan 
    885   1.1  jonathan 	/* Get crypto descriptors. */
    886   1.1  jonathan 	crp = crypto_getreq(esph && espx ? 2 : 1);
    887   1.1  jonathan 	if (crp == NULL) {
    888   1.1  jonathan 		DPRINTF(("esp_output: failed to acquire crypto descriptors\n"));
    889  1.18   thorpej 		ESP_STATINC(ESP_STAT_CRYPTO);
    890   1.1  jonathan 		error = ENOBUFS;
    891   1.1  jonathan 		goto bad;
    892   1.1  jonathan 	}
    893   1.1  jonathan 
    894   1.1  jonathan 	if (espx) {
    895   1.1  jonathan 		crde = crp->crp_desc;
    896   1.1  jonathan 		crda = crde->crd_next;
    897   1.1  jonathan 
    898   1.1  jonathan 		/* Encryption descriptor. */
    899   1.1  jonathan 		crde->crd_skip = skip + hlen;
    900  1.38  drochner 		if (espx->type == CRYPTO_AES_GMAC)
    901  1.38  drochner 			crde->crd_len = 0;
    902  1.38  drochner 		else
    903  1.38  drochner 			crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
    904   1.1  jonathan 		crde->crd_flags = CRD_F_ENCRYPT;
    905   1.1  jonathan 		crde->crd_inject = skip + hlen - sav->ivlen;
    906   1.1  jonathan 
    907   1.1  jonathan 		/* Encryption operation. */
    908   1.1  jonathan 		crde->crd_alg = espx->type;
    909   1.1  jonathan 		crde->crd_key = _KEYBUF(sav->key_enc);
    910   1.1  jonathan 		crde->crd_klen = _KEYBITS(sav->key_enc);
    911   1.1  jonathan 		/* XXX Rounds ? */
    912   1.1  jonathan 	} else
    913   1.1  jonathan 		crda = crp->crp_desc;
    914   1.1  jonathan 
    915   1.1  jonathan 	/* IPsec-specific opaque crypto info. */
    916   1.1  jonathan 	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
    917   1.1  jonathan 		M_XDATA, M_NOWAIT|M_ZERO);
    918   1.1  jonathan 	if (tc == NULL) {
    919   1.1  jonathan 		crypto_freereq(crp);
    920   1.1  jonathan 		DPRINTF(("esp_output: failed to allocate tdb_crypto\n"));
    921  1.18   thorpej 		ESP_STATINC(ESP_STAT_CRYPTO);
    922   1.1  jonathan 		error = ENOBUFS;
    923   1.1  jonathan 		goto bad;
    924   1.1  jonathan 	}
    925   1.1  jonathan 
    926   1.1  jonathan 	/* Callback parameters */
    927   1.1  jonathan 	tc->tc_isr = isr;
    928   1.1  jonathan 	tc->tc_spi = sav->spi;
    929   1.1  jonathan 	tc->tc_dst = saidx->dst;
    930   1.1  jonathan 	tc->tc_proto = saidx->proto;
    931   1.1  jonathan 
    932   1.1  jonathan 	/* Crypto operation descriptor. */
    933   1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
    934   1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    935  1.15  degroote 	crp->crp_buf = m;
    936   1.1  jonathan 	crp->crp_callback = esp_output_cb;
    937  1.15  degroote 	crp->crp_opaque = tc;
    938   1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    939   1.1  jonathan 
    940   1.1  jonathan 	if (esph) {
    941   1.1  jonathan 		/* Authentication descriptor. */
    942   1.1  jonathan 		crda->crd_skip = skip;
    943  1.38  drochner 		if (espx && espx->type == CRYPTO_AES_GCM_16)
    944  1.38  drochner 			crda->crd_len = hlen - sav->ivlen;
    945  1.38  drochner 		else
    946  1.38  drochner 			crda->crd_len = m->m_pkthdr.len - (skip + alen);
    947   1.1  jonathan 		crda->crd_inject = m->m_pkthdr.len - alen;
    948   1.1  jonathan 
    949   1.1  jonathan 		/* Authentication operation. */
    950   1.1  jonathan 		crda->crd_alg = esph->type;
    951  1.38  drochner 		if (espx && (espx->type == CRYPTO_AES_GCM_16 ||
    952  1.38  drochner 			     espx->type == CRYPTO_AES_GMAC)) {
    953  1.38  drochner 			crda->crd_key = _KEYBUF(sav->key_enc);
    954  1.38  drochner 			crda->crd_klen = _KEYBITS(sav->key_enc);
    955  1.38  drochner 		} else {
    956  1.38  drochner 			crda->crd_key = _KEYBUF(sav->key_auth);
    957  1.38  drochner 			crda->crd_klen = _KEYBITS(sav->key_auth);
    958  1.38  drochner 		}
    959   1.1  jonathan 	}
    960   1.1  jonathan 
    961   1.1  jonathan 	return crypto_dispatch(crp);
    962   1.1  jonathan bad:
    963   1.1  jonathan 	if (m)
    964   1.1  jonathan 		m_freem(m);
    965   1.1  jonathan 	return (error);
    966   1.1  jonathan }
    967   1.1  jonathan 
    968   1.1  jonathan /*
    969   1.1  jonathan  * ESP output callback from the crypto driver.
    970   1.1  jonathan  */
    971   1.1  jonathan static int
    972   1.1  jonathan esp_output_cb(struct cryptop *crp)
    973   1.1  jonathan {
    974   1.1  jonathan 	struct tdb_crypto *tc;
    975   1.1  jonathan 	struct ipsecrequest *isr;
    976   1.1  jonathan 	struct secasvar *sav;
    977   1.1  jonathan 	struct mbuf *m;
    978   1.1  jonathan 	int s, err, error;
    979   1.1  jonathan 
    980   1.1  jonathan 	tc = (struct tdb_crypto *) crp->crp_opaque;
    981   1.1  jonathan 	IPSEC_ASSERT(tc != NULL, ("esp_output_cb: null opaque data area!"));
    982   1.1  jonathan 	m = (struct mbuf *) crp->crp_buf;
    983   1.1  jonathan 
    984  1.24  drochner 	s = splsoftnet();
    985  1.23  drochner 	mutex_enter(softnet_lock);
    986   1.1  jonathan 
    987   1.1  jonathan 	isr = tc->tc_isr;
    988  1.16  degroote 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
    989   1.1  jonathan 	if (sav == NULL) {
    990  1.18   thorpej 		ESP_STATINC(ESP_STAT_NOTDB);
    991   1.1  jonathan 		DPRINTF(("esp_output_cb: SA expired while in crypto "
    992   1.1  jonathan 		    "(SA %s/%08lx proto %u)\n", ipsec_address(&tc->tc_dst),
    993   1.1  jonathan 		    (u_long) ntohl(tc->tc_spi), tc->tc_proto));
    994   1.1  jonathan 		error = ENOBUFS;		/*XXX*/
    995   1.1  jonathan 		goto bad;
    996   1.1  jonathan 	}
    997   1.1  jonathan 	IPSEC_ASSERT(isr->sav == sav,
    998   1.1  jonathan 		("esp_output_cb: SA changed was %p now %p\n", isr->sav, sav));
    999   1.1  jonathan 
   1000   1.1  jonathan 	/* Check for crypto errors. */
   1001   1.1  jonathan 	if (crp->crp_etype) {
   1002   1.1  jonathan 		/* Reset session ID. */
   1003   1.1  jonathan 		if (sav->tdb_cryptoid != 0)
   1004   1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
   1005   1.1  jonathan 
   1006   1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
   1007   1.1  jonathan 			KEY_FREESAV(&sav);
   1008  1.24  drochner 			mutex_exit(softnet_lock);
   1009   1.1  jonathan 			splx(s);
   1010   1.1  jonathan 			return crypto_dispatch(crp);
   1011   1.1  jonathan 		}
   1012   1.1  jonathan 
   1013  1.18   thorpej 		ESP_STATINC(ESP_STAT_NOXFORM);
   1014   1.1  jonathan 		DPRINTF(("esp_output_cb: crypto error %d\n", crp->crp_etype));
   1015   1.1  jonathan 		error = crp->crp_etype;
   1016   1.1  jonathan 		goto bad;
   1017   1.1  jonathan 	}
   1018   1.1  jonathan 
   1019   1.1  jonathan 	/* Shouldn't happen... */
   1020   1.1  jonathan 	if (m == NULL) {
   1021  1.18   thorpej 		ESP_STATINC(ESP_STAT_CRYPTO);
   1022   1.1  jonathan 		DPRINTF(("esp_output_cb: bogus returned buffer from crypto\n"));
   1023   1.1  jonathan 		error = EINVAL;
   1024   1.1  jonathan 		goto bad;
   1025   1.1  jonathan 	}
   1026  1.50  christos 	ESP_STATINC(ESP_STAT_HIST + esp_stats[sav->alg_enc]);
   1027   1.1  jonathan 	if (sav->tdb_authalgxform != NULL)
   1028  1.50  christos 		AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
   1029   1.1  jonathan 
   1030   1.1  jonathan 	/* Release crypto descriptors. */
   1031   1.1  jonathan 	free(tc, M_XDATA);
   1032   1.1  jonathan 	crypto_freereq(crp);
   1033   1.1  jonathan 
   1034   1.9    rpaulo #ifdef IPSEC_DEBUG
   1035   1.9    rpaulo 	/* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
   1036   1.9    rpaulo 	if (ipsec_integrity) {
   1037  1.30  drochner 		static unsigned char ipseczeroes[AH_ALEN_MAX];
   1038  1.29  degroote 		const struct auth_hash *esph;
   1039   1.9    rpaulo 
   1040   1.9    rpaulo 		/*
   1041   1.9    rpaulo 		 * Corrupt HMAC if we want to test integrity verification of
   1042   1.9    rpaulo 		 * the other side.
   1043   1.9    rpaulo 		 */
   1044   1.9    rpaulo 		esph = sav->tdb_authalgxform;
   1045   1.9    rpaulo 		if (esph !=  NULL) {
   1046  1.31       spz 			m_copyback(m, m->m_pkthdr.len - esph->authsize,
   1047  1.31       spz 			    esph->authsize, ipseczeroes);
   1048   1.9    rpaulo 		}
   1049   1.9    rpaulo 	}
   1050   1.9    rpaulo #endif
   1051   1.9    rpaulo 
   1052   1.1  jonathan 	/* NB: m is reclaimed by ipsec_process_done. */
   1053   1.1  jonathan 	err = ipsec_process_done(m, isr);
   1054   1.1  jonathan 	KEY_FREESAV(&sav);
   1055  1.24  drochner 	mutex_exit(softnet_lock);
   1056   1.1  jonathan 	splx(s);
   1057   1.1  jonathan 	return err;
   1058   1.1  jonathan bad:
   1059   1.1  jonathan 	if (sav)
   1060   1.1  jonathan 		KEY_FREESAV(&sav);
   1061  1.24  drochner 	mutex_exit(softnet_lock);
   1062   1.1  jonathan 	splx(s);
   1063   1.1  jonathan 	if (m)
   1064   1.1  jonathan 		m_freem(m);
   1065   1.1  jonathan 	free(tc, M_XDATA);
   1066   1.1  jonathan 	crypto_freereq(crp);
   1067   1.1  jonathan 	return error;
   1068   1.1  jonathan }
   1069   1.1  jonathan 
   1070   1.1  jonathan static struct xformsw esp_xformsw = {
   1071   1.1  jonathan 	XF_ESP,		XFT_CONF|XFT_AUTH,	"IPsec ESP",
   1072   1.1  jonathan 	esp_init,	esp_zeroize,		esp_input,
   1073  1.11  christos 	esp_output,
   1074  1.11  christos 	NULL,
   1075   1.1  jonathan };
   1076   1.1  jonathan 
   1077   1.1  jonathan INITFN void
   1078   1.1  jonathan esp_attach(void)
   1079   1.1  jonathan {
   1080  1.18   thorpej 
   1081  1.18   thorpej 	espstat_percpu = percpu_alloc(sizeof(uint64_t) * ESP_NSTATS);
   1082  1.18   thorpej 
   1083   1.1  jonathan #define	MAXIV(xform)					\
   1084  1.35  drochner 	if (xform.ivsize > esp_max_ivlen)		\
   1085  1.35  drochner 		esp_max_ivlen = xform.ivsize		\
   1086   1.1  jonathan 
   1087   1.1  jonathan 	esp_max_ivlen = 0;
   1088   1.1  jonathan 	MAXIV(enc_xform_des);		/* SADB_EALG_DESCBC */
   1089   1.1  jonathan 	MAXIV(enc_xform_3des);		/* SADB_EALG_3DESCBC */
   1090   1.1  jonathan 	MAXIV(enc_xform_rijndael128);	/* SADB_X_EALG_AES */
   1091   1.1  jonathan 	MAXIV(enc_xform_blf);		/* SADB_X_EALG_BLOWFISHCBC */
   1092   1.1  jonathan 	MAXIV(enc_xform_cast5);		/* SADB_X_EALG_CAST128CBC */
   1093   1.1  jonathan 	MAXIV(enc_xform_skipjack);	/* SADB_X_EALG_SKIPJACK */
   1094  1.32  drochner 	MAXIV(enc_xform_camellia);	/* SADB_X_EALG_CAMELLIACBC */
   1095  1.36  drochner 	MAXIV(enc_xform_aes_ctr);	/* SADB_X_EALG_AESCTR */
   1096   1.1  jonathan 	MAXIV(enc_xform_null);		/* SADB_EALG_NULL */
   1097   1.1  jonathan 
   1098   1.1  jonathan 	xform_register(&esp_xformsw);
   1099   1.1  jonathan #undef MAXIV
   1100   1.1  jonathan }
   1101   1.1  jonathan #ifdef __FreeBSD__
   1102   1.1  jonathan SYSINIT(esp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, esp_attach, NULL)
   1103   1.1  jonathan #else
   1104   1.1  jonathan #endif
   1105