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