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