Home | History | Annotate | Line # | Download | only in netipsec
xform_ipcomp.c revision 1.57
      1 /*	$NetBSD: xform_ipcomp.c,v 1.57 2018/02/15 13:51:32 maxv Exp $	*/
      2 /*	$FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $	*/
      3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
      4 
      5 /*
      6  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org)
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * 1. Redistributions of source code must retain the above copyright
     13  *   notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *   notice, this list of conditions and the following disclaimer in the
     16  *   documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote products
     18  *   derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.57 2018/02/15 13:51:32 maxv Exp $");
     34 
     35 /* IP payload compression protocol (IPComp), see RFC 2393 */
     36 #if defined(_KERNEL_OPT)
     37 #include "opt_inet.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/socket.h>
     44 #include <sys/kernel.h>
     45 #include <sys/protosw.h>
     46 #include <sys/sysctl.h>
     47 #include <sys/pool.h>
     48 #include <sys/pserialize.h>
     49 
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/ip.h>
     53 #include <netinet/ip_var.h>
     54 
     55 #include <net/route.h>
     56 #include <netipsec/ipsec.h>
     57 #include <netipsec/ipsec_private.h>
     58 #include <netipsec/xform.h>
     59 
     60 #ifdef INET6
     61 #include <netinet/ip6.h>
     62 #include <netipsec/ipsec6.h>
     63 #endif
     64 
     65 #include <netipsec/ipcomp.h>
     66 #include <netipsec/ipcomp_var.h>
     67 
     68 #include <netipsec/key.h>
     69 #include <netipsec/key_debug.h>
     70 
     71 #include <opencrypto/cryptodev.h>
     72 #include <opencrypto/deflate.h>
     73 #include <opencrypto/xform.h>
     74 
     75 percpu_t *ipcompstat_percpu;
     76 
     77 int ipcomp_enable = 1;
     78 
     79 #ifdef __FreeBSD__
     80 SYSCTL_DECL(_net_inet_ipcomp);
     81 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
     82 	ipcomp_enable,	CTLFLAG_RW,	&ipcomp_enable,	0, "");
     83 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
     84 	stats,		CTLFLAG_RD,	&ipcompstat,	ipcompstat, "");
     85 #endif /* __FreeBSD__ */
     86 
     87 static int ipcomp_input_cb(struct cryptop *crp);
     88 static int ipcomp_output_cb(struct cryptop *crp);
     89 
     90 const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
     91 
     92 static pool_cache_t ipcomp_tdb_crypto_pool_cache;
     93 
     94 const struct comp_algo *
     95 ipcomp_algorithm_lookup(int alg)
     96 {
     97 	switch (alg) {
     98 	case SADB_X_CALG_DEFLATE:
     99 		return &comp_algo_deflate_nogrow;
    100 	}
    101 	return NULL;
    102 }
    103 
    104 /*
    105  * ipcomp_init() is called when an CPI is being set up.
    106  */
    107 static int
    108 ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
    109 {
    110 	const struct comp_algo *tcomp;
    111 	struct cryptoini cric;
    112 	int ses;
    113 
    114 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
    115 	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
    116 	if (tcomp == NULL) {
    117 		DPRINTF(("%s: unsupported compression algorithm %d\n",
    118 		    __func__, sav->alg_comp));
    119 		return EINVAL;
    120 	}
    121 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
    122 	sav->tdb_xform = xsp;
    123 	sav->tdb_compalgxform = tcomp;
    124 
    125 	/* Initialize crypto session */
    126 	memset(&cric, 0, sizeof(cric));
    127 	cric.cri_alg = sav->tdb_compalgxform->type;
    128 
    129 	ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
    130 	return ses;
    131 }
    132 
    133 /*
    134  * ipcomp_zeroize() used when IPCA is deleted
    135  */
    136 static int
    137 ipcomp_zeroize(struct secasvar *sav)
    138 {
    139 	int err;
    140 
    141 	err = crypto_freesession(sav->tdb_cryptoid);
    142 	sav->tdb_cryptoid = 0;
    143 	return err;
    144 }
    145 
    146 /*
    147  * ipcomp_input() gets called to uncompress an input packet
    148  */
    149 static int
    150 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    151 {
    152 	struct tdb_crypto *tc;
    153 	struct cryptodesc *crdc;
    154 	struct cryptop *crp;
    155 	int error, hlen = IPCOMP_HLENGTH, stat = IPCOMP_STAT_CRYPTO;
    156 
    157 	IPSEC_SPLASSERT_SOFTNET(__func__);
    158 
    159 	/* Get crypto descriptors */
    160 	crp = crypto_getreq(1);
    161 	if (crp == NULL) {
    162 		DPRINTF(("%s: no crypto descriptors\n", __func__));
    163 		error = ENOBUFS;
    164 		goto error_m;
    165 	}
    166 	/* Get IPsec-specific opaque pointer */
    167 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    168 	if (tc == NULL) {
    169 		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
    170 		error = ENOBUFS;
    171 		goto error_crp;
    172 	}
    173 
    174 	error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
    175 	if (error) {
    176 		DPRINTF(("%s: m_makewritable failed\n", __func__));
    177 		goto error_tc;
    178 	}
    179 
    180     {
    181 	int s = pserialize_read_enter();
    182 
    183 	/*
    184 	 * Take another reference to the SA for opencrypto callback.
    185 	 */
    186 	if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
    187 		pserialize_read_exit(s);
    188 		stat = IPCOMP_STAT_NOTDB;
    189 		error = ENOENT;
    190 		goto error_tc;
    191 	}
    192 	KEY_SA_REF(sav);
    193 	pserialize_read_exit(s);
    194     }
    195 
    196 	crdc = crp->crp_desc;
    197 
    198 	crdc->crd_skip = skip + hlen;
    199 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
    200 	crdc->crd_inject = 0; /* unused */
    201 
    202 	/* Decompression operation */
    203 	crdc->crd_alg = sav->tdb_compalgxform->type;
    204 
    205 	/* Crypto operation descriptor */
    206 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
    207 	crp->crp_olen = MCLBYTES; /* hint to decompression code */
    208 	crp->crp_flags = CRYPTO_F_IMBUF;
    209 	crp->crp_buf = m;
    210 	crp->crp_callback = ipcomp_input_cb;
    211 	crp->crp_sid = sav->tdb_cryptoid;
    212 	crp->crp_opaque = tc;
    213 
    214 	/* These are passed as-is to the callback */
    215 	tc->tc_spi = sav->spi;
    216 	tc->tc_dst = sav->sah->saidx.dst;
    217 	tc->tc_proto = sav->sah->saidx.proto;
    218 	tc->tc_protoff = protoff;
    219 	tc->tc_skip = skip;
    220 	tc->tc_sav = sav;
    221 
    222 	return crypto_dispatch(crp);
    223 
    224 error_tc:
    225 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    226 error_crp:
    227 	crypto_freereq(crp);
    228 error_m:
    229 	m_freem(m);
    230 	IPCOMP_STATINC(stat);
    231 	return error;
    232 }
    233 
    234 #ifdef INET6
    235 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do {		     \
    236 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    237 		error = ipsec6_common_input_cb(m, sav, skip, protoff);	     \
    238 	} else {							     \
    239 		error = ipsec4_common_input_cb(m, sav, skip, protoff);       \
    240 	}								     \
    241 } while (0)
    242 #else
    243 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff)			     \
    244 	(error = ipsec4_common_input_cb(m, sav, skip, protoff))
    245 #endif
    246 
    247 /*
    248  * IPComp input callback from the crypto driver.
    249  */
    250 static int
    251 ipcomp_input_cb(struct cryptop *crp)
    252 {
    253 	char buf[IPSEC_ADDRSTRLEN];
    254 	struct tdb_crypto *tc;
    255 	int skip, protoff;
    256 	struct mbuf *m;
    257 	struct secasvar *sav;
    258 	struct secasindex *saidx __diagused;
    259 	int hlen = IPCOMP_HLENGTH, error, clen;
    260 	uint8_t nproto;
    261 	struct ipcomp *ipc;
    262 	uint16_t dport;
    263 	uint16_t sport;
    264 	IPSEC_DECLARE_LOCK_VARIABLE;
    265 
    266 	KASSERT(crp->crp_opaque != NULL);
    267 	tc = crp->crp_opaque;
    268 	skip = tc->tc_skip;
    269 	protoff = tc->tc_protoff;
    270 	m = crp->crp_buf;
    271 
    272 	/* find the source port for NAT-T */
    273 	nat_t_ports_get(m, &dport, &sport);
    274 
    275 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    276 
    277 	sav = tc->tc_sav;
    278 	saidx = &sav->sah->saidx;
    279 	KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
    280 	    saidx->dst.sa.sa_family == AF_INET6,
    281 	    "unexpected protocol family %u", saidx->dst.sa.sa_family);
    282 
    283 	/* Check for crypto errors */
    284 	if (crp->crp_etype) {
    285 		/* Reset the session ID */
    286 		if (sav->tdb_cryptoid != 0)
    287 			sav->tdb_cryptoid = crp->crp_sid;
    288 
    289 		if (crp->crp_etype == EAGAIN) {
    290 			KEY_SA_UNREF(&sav);
    291 			IPSEC_RELEASE_GLOBAL_LOCKS();
    292 			return crypto_dispatch(crp);
    293 		}
    294 
    295 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    296 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    297 		error = crp->crp_etype;
    298 		goto bad;
    299 	}
    300 
    301 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    302 
    303 	/* Update the counters */
    304 	IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
    305 
    306 	/* Length of data after processing */
    307 	clen = crp->crp_olen;
    308 
    309 	/* Release the crypto descriptors */
    310 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    311 	tc = NULL;
    312 	crypto_freereq(crp);
    313 	crp = NULL;
    314 
    315 	/* In case it's not done already, adjust the size of the mbuf chain */
    316 	m->m_pkthdr.len = clen + hlen + skip;
    317 
    318 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
    319 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    320 		DPRINTF(("%s: m_pullup failed\n", __func__));
    321 		error = EINVAL;
    322 		goto bad;
    323 	}
    324 
    325 	/* Keep the next protocol field */
    326 	ipc = (struct ipcomp *)(mtod(m, uint8_t *) + skip);
    327 	nproto = ipc->comp_nxt;
    328 	switch (nproto) {
    329 	case IPPROTO_IPCOMP:
    330 	case IPPROTO_AH:
    331 	case IPPROTO_ESP:
    332 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    333 		DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
    334 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    335 		    (u_long) ntohl(sav->spi)));
    336 		error = EINVAL;
    337 		goto bad;
    338 	default:
    339 		break;
    340 	}
    341 
    342 	/* Remove the IPCOMP header */
    343 	error = m_striphdr(m, skip, hlen);
    344 	if (error) {
    345 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    346 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
    347 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    348 		    (u_long) ntohl(sav->spi)));
    349 		goto bad;
    350 	}
    351 
    352 	/* Restore the Next Protocol field */
    353 	m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *)&nproto);
    354 
    355 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
    356 
    357 	KEY_SA_UNREF(&sav);
    358 	IPSEC_RELEASE_GLOBAL_LOCKS();
    359 	return error;
    360 
    361 bad:
    362 	if (sav)
    363 		KEY_SA_UNREF(&sav);
    364 	IPSEC_RELEASE_GLOBAL_LOCKS();
    365 	if (m)
    366 		m_freem(m);
    367 	if (tc != NULL)
    368 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    369 	if (crp)
    370 		crypto_freereq(crp);
    371 	return error;
    372 }
    373 
    374 /*
    375  * IPComp output routine, called by ipsec[46]_process_packet()
    376  */
    377 static int
    378 ipcomp_output(struct mbuf *m, const struct ipsecrequest *isr,
    379     struct secasvar *sav, struct mbuf **mp, int skip, int protoff)
    380 {
    381 	char buf[IPSEC_ADDRSTRLEN];
    382 	const struct comp_algo *ipcompx;
    383 	int error, ralen, hlen, maxpacketsize;
    384 	struct cryptodesc *crdc;
    385 	struct cryptop *crp;
    386 	struct tdb_crypto *tc;
    387 
    388 	IPSEC_SPLASSERT_SOFTNET(__func__);
    389 	KASSERT(sav != NULL);
    390 	KASSERT(sav->tdb_compalgxform != NULL);
    391 	ipcompx = sav->tdb_compalgxform;
    392 
    393 	/* Raw payload length before comp. */
    394 	ralen = m->m_pkthdr.len - skip;
    395 
    396 	/* Don't process the packet if it is too short */
    397 	if (ralen < ipcompx->minlen) {
    398 		IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
    399 		return ipsec_process_done(m, isr, sav);
    400 	}
    401 
    402 	hlen = IPCOMP_HLENGTH;
    403 
    404 	IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
    405 
    406 	/* Check for maximum packet size violations. */
    407 	switch (sav->sah->saidx.dst.sa.sa_family) {
    408 #ifdef INET
    409 	case AF_INET:
    410 		maxpacketsize = IP_MAXPACKET;
    411 		break;
    412 #endif
    413 #ifdef INET6
    414 	case AF_INET6:
    415 		maxpacketsize = IPV6_MAXPACKET;
    416 		break;
    417 #endif
    418 	default:
    419 		IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    420 		DPRINTF(("%s: unknown/unsupported protocol family %d"
    421 		    ", IPCA %s/%08lx\n", __func__,
    422 		    sav->sah->saidx.dst.sa.sa_family,
    423 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    424 		    (u_long) ntohl(sav->spi)));
    425 		error = EPFNOSUPPORT;
    426 		goto bad;
    427 	}
    428 	if (skip + hlen + ralen > maxpacketsize) {
    429 		IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
    430 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
    431 		    "(len %u, max len %u)\n", __func__,
    432 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    433 		    (u_long) ntohl(sav->spi),
    434 		    skip + hlen + ralen, maxpacketsize));
    435 		error = EMSGSIZE;
    436 		goto bad;
    437 	}
    438 
    439 	/* Update the counters */
    440 	IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
    441 
    442 	m = m_clone(m);
    443 	if (m == NULL) {
    444 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    445 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
    446 		    __func__,
    447 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    448 		    (u_long) ntohl(sav->spi)));
    449 		error = ENOBUFS;
    450 		goto bad;
    451 	}
    452 
    453 	/* Ok now, we can pass to the crypto processing */
    454 
    455 	/* Get crypto descriptors */
    456 	crp = crypto_getreq(1);
    457 	if (crp == NULL) {
    458 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    459 		DPRINTF(("%s: failed to acquire crypto descriptor\n",
    460 		    __func__));
    461 		error = ENOBUFS;
    462 		goto bad;
    463 	}
    464 	crdc = crp->crp_desc;
    465 
    466 	/* Compression descriptor */
    467 	crdc->crd_skip = skip;
    468 	crdc->crd_len = m->m_pkthdr.len - skip;
    469 	crdc->crd_flags = CRD_F_COMP;
    470 	crdc->crd_inject = skip;
    471 
    472 	/* Compression operation */
    473 	crdc->crd_alg = ipcompx->type;
    474 
    475 	/* IPsec-specific opaque crypto info */
    476 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    477 	if (tc == NULL) {
    478 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    479 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
    480 		crypto_freereq(crp);
    481 		error = ENOBUFS;
    482 		goto bad;
    483 	}
    484 
    485     {
    486 	int s = pserialize_read_enter();
    487 
    488 	/*
    489 	 * Take another reference to the SP and the SA for opencrypto callback.
    490 	 */
    491 	if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
    492 	    sav->state == SADB_SASTATE_DEAD)) {
    493 		pserialize_read_exit(s);
    494 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    495 		crypto_freereq(crp);
    496 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    497 		error = ENOENT;
    498 		goto bad;
    499 	}
    500 	KEY_SP_REF(isr->sp);
    501 	KEY_SA_REF(sav);
    502 	pserialize_read_exit(s);
    503     }
    504 
    505 	tc->tc_isr = isr;
    506 	tc->tc_spi = sav->spi;
    507 	tc->tc_dst = sav->sah->saidx.dst;
    508 	tc->tc_proto = sav->sah->saidx.proto;
    509 	tc->tc_skip = skip;
    510 	tc->tc_protoff = protoff;
    511 	tc->tc_sav = sav;
    512 
    513 	/* Crypto operation descriptor */
    514 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
    515 	crp->crp_flags = CRYPTO_F_IMBUF;
    516 	crp->crp_buf = m;
    517 	crp->crp_callback = ipcomp_output_cb;
    518 	crp->crp_opaque = tc;
    519 	crp->crp_sid = sav->tdb_cryptoid;
    520 
    521 	return crypto_dispatch(crp);
    522 
    523 bad:
    524 	if (m)
    525 		m_freem(m);
    526 	return error;
    527 }
    528 
    529 /*
    530  * IPComp output callback from the crypto driver.
    531  */
    532 static int
    533 ipcomp_output_cb(struct cryptop *crp)
    534 {
    535 	char buf[IPSEC_ADDRSTRLEN];
    536 	struct tdb_crypto *tc;
    537 	const struct ipsecrequest *isr;
    538 	struct secasvar *sav;
    539 	struct mbuf *m, *mo;
    540 	int error, skip, rlen, roff;
    541 	uint8_t prot;
    542 	uint16_t cpi;
    543 	struct ipcomp * ipcomp;
    544 	IPSEC_DECLARE_LOCK_VARIABLE;
    545 
    546 	KASSERT(crp->crp_opaque != NULL);
    547 	tc = crp->crp_opaque;
    548 	m = crp->crp_buf;
    549 	skip = tc->tc_skip;
    550 	rlen = crp->crp_ilen - skip;
    551 
    552 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    553 
    554 	isr = tc->tc_isr;
    555 	sav = tc->tc_sav;
    556 
    557 	/* Check for crypto errors */
    558 	if (crp->crp_etype) {
    559 		/* Reset session ID */
    560 		if (sav->tdb_cryptoid != 0)
    561 			sav->tdb_cryptoid = crp->crp_sid;
    562 
    563 		if (crp->crp_etype == EAGAIN) {
    564 			IPSEC_RELEASE_GLOBAL_LOCKS();
    565 			return crypto_dispatch(crp);
    566 		}
    567 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    568 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    569 		error = crp->crp_etype;
    570 		goto bad;
    571 	}
    572 
    573 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    574 
    575 	if (rlen > crp->crp_olen) {
    576 		/* Inject IPCOMP header */
    577 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
    578 		if (mo == NULL) {
    579 			IPCOMP_STATINC(IPCOMP_STAT_WRAP);
    580 			DPRINTF(("%s: failed to inject IPCOMP header for "
    581 			    "IPCA %s/%08lx\n", __func__,
    582 			    ipsec_address(&sav->sah->saidx.dst, buf,
    583 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    584 			error = ENOBUFS;
    585 			goto bad;
    586 		}
    587 		ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
    588 
    589 		/* Initialize the IPCOMP header */
    590 		/* XXX alignment always correct? */
    591 		switch (sav->sah->saidx.dst.sa.sa_family) {
    592 #ifdef INET
    593 		case AF_INET:
    594 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
    595 			break;
    596 #endif
    597 #ifdef INET6
    598 		case AF_INET6:
    599 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
    600 			break;
    601 #endif
    602 		}
    603 		ipcomp->comp_flags = 0;
    604 
    605 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
    606 			cpi = sav->alg_enc;
    607 		else
    608 			cpi = ntohl(sav->spi) & 0xffff;
    609 		ipcomp->comp_cpi = htons(cpi);
    610 
    611 		/* Fix Next Protocol in IPv4/IPv6 header */
    612 		prot = IPPROTO_IPCOMP;
    613 		m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
    614 
    615 		/* Adjust the length in the IP header */
    616 		switch (sav->sah->saidx.dst.sa.sa_family) {
    617 #ifdef INET
    618 		case AF_INET:
    619 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    620 			break;
    621 #endif
    622 #ifdef INET6
    623 		case AF_INET6:
    624 			mtod(m, struct ip6_hdr *)->ip6_plen =
    625 			    htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
    626 			break;
    627 #endif
    628 		default:
    629 			IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    630 			DPRINTF(("ipcomp_output: unknown/unsupported protocol "
    631 			    "family %d, IPCA %s/%08lx\n",
    632 			    sav->sah->saidx.dst.sa.sa_family,
    633 			    ipsec_address(&sav->sah->saidx.dst, buf,
    634 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    635 			error = EPFNOSUPPORT;
    636 			goto bad;
    637 		}
    638 	} else {
    639 		/* compression was useless, we have lost time */
    640 		IPCOMP_STATINC(IPCOMP_STAT_USELESS);
    641 		DPRINTF(("ipcomp_output_cb: compression was useless: initial"
    642 		    "size was %d and compressed size is %d\n", rlen,
    643 		    crp->crp_olen));
    644 	}
    645 
    646 	/* Release the crypto descriptor */
    647 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    648 	crypto_freereq(crp);
    649 
    650 	/* NB: m is reclaimed by ipsec_process_done. */
    651 	error = ipsec_process_done(m, isr, sav);
    652 	KEY_SA_UNREF(&sav);
    653 	KEY_SP_UNREF(&isr->sp);
    654 	IPSEC_RELEASE_GLOBAL_LOCKS();
    655 	return error;
    656 
    657 bad:
    658 	if (sav)
    659 		KEY_SA_UNREF(&sav);
    660 	KEY_SP_UNREF(&isr->sp);
    661 	IPSEC_RELEASE_GLOBAL_LOCKS();
    662 	if (m)
    663 		m_freem(m);
    664 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    665 	crypto_freereq(crp);
    666 	return error;
    667 }
    668 
    669 static struct xformsw ipcomp_xformsw = {
    670 	.xf_type	= XF_IPCOMP,
    671 	.xf_flags	= XFT_COMP,
    672 	.xf_name	= "IPcomp",
    673 	.xf_init	= ipcomp_init,
    674 	.xf_zeroize	= ipcomp_zeroize,
    675 	.xf_input	= ipcomp_input,
    676 	.xf_output	= ipcomp_output,
    677 	.xf_next	= NULL,
    678 };
    679 
    680 void
    681 ipcomp_attach(void)
    682 {
    683 	ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
    684 	ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
    685 	    coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
    686 	    NULL, NULL, NULL);
    687 	xform_register(&ipcomp_xformsw);
    688 }
    689