Home | History | Annotate | Line # | Download | only in netipsec
xform_ipcomp.c revision 1.70
      1 /*	$NetBSD: xform_ipcomp.c,v 1.70 2022/05/22 11:30:40 riastradh Exp $	*/
      2 /*	$FreeBSD: 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.70 2022/05/22 11:30:40 riastradh 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 
     74 percpu_t *ipcompstat_percpu;
     75 
     76 int ipcomp_enable = 1;
     77 
     78 static void ipcomp_input_cb(struct cryptop *crp);
     79 static void ipcomp_output_cb(struct cryptop *crp);
     80 
     81 const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
     82 
     83 static pool_cache_t ipcomp_tdb_crypto_pool_cache;
     84 
     85 const struct comp_algo *
     86 ipcomp_algorithm_lookup(int alg)
     87 {
     88 	switch (alg) {
     89 	case SADB_X_CALG_DEFLATE:
     90 		return &comp_algo_deflate_nogrow;
     91 	}
     92 	return NULL;
     93 }
     94 
     95 /*
     96  * ipcomp_init() is called when an CPI is being set up.
     97  */
     98 static int
     99 ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
    100 {
    101 	const struct comp_algo *tcomp;
    102 	struct cryptoini cric;
    103 	int ses;
    104 
    105 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
    106 	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
    107 	if (tcomp == NULL) {
    108 		DPRINTF("unsupported compression algorithm %d\n",
    109 		    sav->alg_comp);
    110 		return EINVAL;
    111 	}
    112 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
    113 	sav->tdb_xform = xsp;
    114 	sav->tdb_compalgxform = tcomp;
    115 
    116 	/* Initialize crypto session */
    117 	memset(&cric, 0, sizeof(cric));
    118 	cric.cri_alg = sav->tdb_compalgxform->type;
    119 
    120 	ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
    121 	return ses;
    122 }
    123 
    124 /*
    125  * ipcomp_zeroize() used when IPCA is deleted
    126  */
    127 static int
    128 ipcomp_zeroize(struct secasvar *sav)
    129 {
    130 	int err;
    131 
    132 	err = crypto_freesession(sav->tdb_cryptoid);
    133 	sav->tdb_cryptoid = 0;
    134 	return err;
    135 }
    136 
    137 /*
    138  * ipcomp_input() gets called to uncompress an input packet
    139  */
    140 static int
    141 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    142 {
    143 	struct tdb_crypto *tc;
    144 	struct cryptodesc *crdc;
    145 	struct cryptop *crp;
    146 	int error, hlen = IPCOMP_HLENGTH, stat = IPCOMP_STAT_CRYPTO;
    147 
    148 	KASSERT(skip + hlen <= m->m_pkthdr.len);
    149 
    150 	/* Get crypto descriptors */
    151 	crp = crypto_getreq(1);
    152 	if (crp == NULL) {
    153 		DPRINTF("no crypto descriptors\n");
    154 		error = ENOBUFS;
    155 		goto error_m;
    156 	}
    157 	/* Get IPsec-specific opaque pointer */
    158 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    159 	if (tc == NULL) {
    160 		DPRINTF("cannot allocate tdb_crypto\n");
    161 		error = ENOBUFS;
    162 		goto error_crp;
    163 	}
    164 
    165 	error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
    166 	if (error) {
    167 		DPRINTF("m_makewritable failed\n");
    168 		goto error_tc;
    169 	}
    170 
    171     {
    172 	int s = pserialize_read_enter();
    173 
    174 	/*
    175 	 * Take another reference to the SA for opencrypto callback.
    176 	 */
    177 	if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
    178 		pserialize_read_exit(s);
    179 		stat = IPCOMP_STAT_NOTDB;
    180 		error = ENOENT;
    181 		goto error_tc;
    182 	}
    183 	KEY_SA_REF(sav);
    184 	pserialize_read_exit(s);
    185     }
    186 
    187 	crdc = crp->crp_desc;
    188 
    189 	crdc->crd_skip = skip + hlen;
    190 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
    191 	crdc->crd_inject = 0; /* unused */
    192 
    193 	/* Decompression operation */
    194 	crdc->crd_alg = sav->tdb_compalgxform->type;
    195 
    196 	/* Crypto operation descriptor */
    197 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
    198 	crp->crp_olen = MCLBYTES; /* hint to decompression code */
    199 	crp->crp_flags = CRYPTO_F_IMBUF;
    200 	crp->crp_buf = m;
    201 	crp->crp_callback = ipcomp_input_cb;
    202 	crp->crp_sid = sav->tdb_cryptoid;
    203 	crp->crp_opaque = tc;
    204 
    205 	/* These are passed as-is to the callback */
    206 	tc->tc_spi = sav->spi;
    207 	tc->tc_dst = sav->sah->saidx.dst;
    208 	tc->tc_proto = sav->sah->saidx.proto;
    209 	tc->tc_protoff = protoff;
    210 	tc->tc_skip = skip;
    211 	tc->tc_sav = sav;
    212 
    213 	return crypto_dispatch(crp);
    214 
    215 error_tc:
    216 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    217 error_crp:
    218 	crypto_freereq(crp);
    219 error_m:
    220 	m_freem(m);
    221 	IPCOMP_STATINC(stat);
    222 	return error;
    223 }
    224 
    225 #ifdef INET6
    226 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do {		     \
    227 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    228 		(void)ipsec6_common_input_cb(m, sav, skip, protoff);	     \
    229 	} else {							     \
    230 		(void)ipsec4_common_input_cb(m, sav, skip, protoff);       \
    231 	}								     \
    232 } while (0)
    233 #else
    234 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff)			     \
    235 	((void)ipsec4_common_input_cb(m, sav, skip, protoff))
    236 #endif
    237 
    238 /*
    239  * IPComp input callback from the crypto driver.
    240  */
    241 static void
    242 ipcomp_input_cb(struct cryptop *crp)
    243 {
    244 	char buf[IPSEC_ADDRSTRLEN];
    245 	struct tdb_crypto *tc;
    246 	int skip, protoff;
    247 	struct mbuf *m;
    248 	struct secasvar *sav;
    249 	struct secasindex *saidx __diagused;
    250 	int hlen = IPCOMP_HLENGTH, clen;
    251 	uint8_t nproto;
    252 	struct ipcomp *ipc;
    253 	IPSEC_DECLARE_LOCK_VARIABLE;
    254 
    255 	KASSERT(crp->crp_opaque != NULL);
    256 	tc = crp->crp_opaque;
    257 	skip = tc->tc_skip;
    258 	protoff = tc->tc_protoff;
    259 	m = crp->crp_buf;
    260 
    261 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    262 
    263 	sav = tc->tc_sav;
    264 	saidx = &sav->sah->saidx;
    265 	KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
    266 	    saidx->dst.sa.sa_family == AF_INET6,
    267 	    "unexpected protocol family %u", saidx->dst.sa.sa_family);
    268 
    269 	/* Check for crypto errors */
    270 	if (crp->crp_etype) {
    271 		/* Reset the session ID */
    272 		if (sav->tdb_cryptoid != 0)
    273 			sav->tdb_cryptoid = crp->crp_sid;
    274 
    275 		if (crp->crp_etype == EAGAIN) {
    276 			KEY_SA_UNREF(&sav);
    277 			IPSEC_RELEASE_GLOBAL_LOCKS();
    278 			(void)crypto_dispatch(crp);
    279 			return;
    280 		}
    281 
    282 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    283 		DPRINTF("crypto error %d\n", crp->crp_etype);
    284 		goto bad;
    285 	}
    286 
    287 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    288 
    289 	/* Update the counters */
    290 	IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
    291 
    292 	/* Length of data after processing */
    293 	clen = crp->crp_olen;
    294 
    295 	/* Release the crypto descriptors */
    296 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    297 	tc = NULL;
    298 	crypto_freereq(crp);
    299 	crp = NULL;
    300 
    301 	/* In case it's not done already, adjust the size of the mbuf chain */
    302 	m->m_pkthdr.len = clen + hlen + skip;
    303 
    304 	/*
    305 	 * Get the next protocol field.
    306 	 *
    307 	 * XXX: Really, we should use m_copydata instead of m_pullup.
    308 	 */
    309 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
    310 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    311 		DPRINTF("m_pullup failed\n");
    312 		goto bad;
    313 	}
    314 	ipc = (struct ipcomp *)(mtod(m, uint8_t *) + skip);
    315 	nproto = ipc->comp_nxt;
    316 
    317 	switch (nproto) {
    318 	case IPPROTO_IPCOMP:
    319 	case IPPROTO_AH:
    320 	case IPPROTO_ESP:
    321 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    322 		DPRINTF("nested ipcomp, IPCA %s/%08lx\n",
    323 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    324 		    (u_long) ntohl(sav->spi));
    325 		goto bad;
    326 	default:
    327 		break;
    328 	}
    329 
    330 	/* Remove the IPCOMP header */
    331 	if (m_striphdr(m, skip, hlen) != 0) {
    332 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    333 		DPRINTF("bad mbuf chain, IPCA %s/%08lx\n",
    334 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    335 		    (u_long) ntohl(sav->spi));
    336 		goto bad;
    337 	}
    338 
    339 	/* Restore the Next Protocol field */
    340 	m_copyback(m, protoff, sizeof(nproto), &nproto);
    341 
    342 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
    343 
    344 	KEY_SA_UNREF(&sav);
    345 	IPSEC_RELEASE_GLOBAL_LOCKS();
    346 	return;
    347 
    348 bad:
    349 	if (sav)
    350 		KEY_SA_UNREF(&sav);
    351 	IPSEC_RELEASE_GLOBAL_LOCKS();
    352 	if (m)
    353 		m_freem(m);
    354 	if (tc != NULL)
    355 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    356 	if (crp)
    357 		crypto_freereq(crp);
    358 }
    359 
    360 /*
    361  * IPComp output routine, called by ipsec[46]_process_packet()
    362  */
    363 static int
    364 ipcomp_output(struct mbuf *m, const struct ipsecrequest *isr,
    365     struct secasvar *sav, int skip, int protoff, int flags)
    366 {
    367 	char buf[IPSEC_ADDRSTRLEN];
    368 	const struct comp_algo *ipcompx;
    369 	int error, ralen, hlen, maxpacketsize;
    370 	struct cryptodesc *crdc;
    371 	struct cryptop *crp;
    372 	struct tdb_crypto *tc;
    373 
    374 	KASSERT(sav != NULL);
    375 	KASSERT(sav->tdb_compalgxform != NULL);
    376 	ipcompx = sav->tdb_compalgxform;
    377 
    378 	/* Raw payload length before comp. */
    379 	ralen = m->m_pkthdr.len - skip;
    380 
    381 	/* Don't process the packet if it is too short */
    382 	if (ralen < ipcompx->minlen) {
    383 		IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
    384 		return ipsec_process_done(m, isr, sav, 0);
    385 	}
    386 
    387 	hlen = IPCOMP_HLENGTH;
    388 
    389 	IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
    390 
    391 	/* Check for maximum packet size violations. */
    392 	switch (sav->sah->saidx.dst.sa.sa_family) {
    393 #ifdef INET
    394 	case AF_INET:
    395 		maxpacketsize = IP_MAXPACKET;
    396 		break;
    397 #endif
    398 #ifdef INET6
    399 	case AF_INET6:
    400 		maxpacketsize = IPV6_MAXPACKET;
    401 		break;
    402 #endif
    403 	default:
    404 		IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    405 		DPRINTF("unknown/unsupported protocol family %d"
    406 		    ", IPCA %s/%08lx\n",
    407 		    sav->sah->saidx.dst.sa.sa_family,
    408 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    409 		    (u_long) ntohl(sav->spi));
    410 		error = EPFNOSUPPORT;
    411 		goto bad;
    412 	}
    413 	if (skip + hlen + ralen > maxpacketsize) {
    414 		IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
    415 		DPRINTF("packet in IPCA %s/%08lx got too big "
    416 		    "(len %u, max len %u)\n",
    417 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    418 		    (u_long) ntohl(sav->spi),
    419 		    skip + hlen + ralen, maxpacketsize);
    420 		error = EMSGSIZE;
    421 		goto bad;
    422 	}
    423 
    424 	/* Update the counters */
    425 	IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
    426 
    427 	m = m_clone(m);
    428 	if (m == NULL) {
    429 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    430 		DPRINTF("cannot clone mbuf chain, IPCA %s/%08lx\n",
    431 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    432 		    (u_long) ntohl(sav->spi));
    433 		error = ENOBUFS;
    434 		goto bad;
    435 	}
    436 
    437 	/* Ok now, we can pass to the crypto processing */
    438 
    439 	/* Get crypto descriptors */
    440 	crp = crypto_getreq(1);
    441 	if (crp == NULL) {
    442 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    443 		DPRINTF("failed to acquire crypto descriptor\n");
    444 		error = ENOBUFS;
    445 		goto bad;
    446 	}
    447 	crdc = crp->crp_desc;
    448 
    449 	/* Compression descriptor */
    450 	crdc->crd_skip = skip;
    451 	crdc->crd_len = m->m_pkthdr.len - skip;
    452 	crdc->crd_flags = CRD_F_COMP;
    453 	crdc->crd_inject = skip;
    454 
    455 	/* Compression operation */
    456 	crdc->crd_alg = ipcompx->type;
    457 
    458 	/* IPsec-specific opaque crypto info */
    459 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    460 	if (tc == NULL) {
    461 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    462 		DPRINTF("failed to allocate tdb_crypto\n");
    463 		crypto_freereq(crp);
    464 		error = ENOBUFS;
    465 		goto bad;
    466 	}
    467 
    468     {
    469 	int s = pserialize_read_enter();
    470 
    471 	/*
    472 	 * Take another reference to the SP and the SA for opencrypto callback.
    473 	 */
    474 	if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
    475 	    sav->state == SADB_SASTATE_DEAD)) {
    476 		pserialize_read_exit(s);
    477 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    478 		crypto_freereq(crp);
    479 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    480 		error = ENOENT;
    481 		goto bad;
    482 	}
    483 	KEY_SP_REF(isr->sp);
    484 	KEY_SA_REF(sav);
    485 	pserialize_read_exit(s);
    486     }
    487 
    488 	tc->tc_isr = isr;
    489 	tc->tc_spi = sav->spi;
    490 	tc->tc_dst = sav->sah->saidx.dst;
    491 	tc->tc_proto = sav->sah->saidx.proto;
    492 	tc->tc_skip = skip;
    493 	tc->tc_protoff = protoff;
    494 	tc->tc_flags = flags;
    495 	tc->tc_sav = sav;
    496 
    497 	/* Crypto operation descriptor */
    498 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
    499 	crp->crp_flags = CRYPTO_F_IMBUF;
    500 	crp->crp_buf = m;
    501 	crp->crp_callback = ipcomp_output_cb;
    502 	crp->crp_opaque = tc;
    503 	crp->crp_sid = sav->tdb_cryptoid;
    504 
    505 	return crypto_dispatch(crp);
    506 
    507 bad:
    508 	if (m)
    509 		m_freem(m);
    510 	return error;
    511 }
    512 
    513 /*
    514  * IPComp output callback from the crypto driver.
    515  */
    516 static void
    517 ipcomp_output_cb(struct cryptop *crp)
    518 {
    519 	char buf[IPSEC_ADDRSTRLEN];
    520 	struct tdb_crypto *tc;
    521 	const struct ipsecrequest *isr;
    522 	struct secasvar *sav;
    523 	struct mbuf *m, *mo;
    524 	int skip, rlen, roff, flags;
    525 	uint8_t prot;
    526 	uint16_t cpi;
    527 	struct ipcomp * ipcomp;
    528 	IPSEC_DECLARE_LOCK_VARIABLE;
    529 
    530 	KASSERT(crp->crp_opaque != NULL);
    531 	tc = crp->crp_opaque;
    532 	m = crp->crp_buf;
    533 	skip = tc->tc_skip;
    534 	rlen = crp->crp_ilen - skip;
    535 
    536 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    537 
    538 	isr = tc->tc_isr;
    539 	sav = tc->tc_sav;
    540 
    541 	/* Check for crypto errors */
    542 	if (crp->crp_etype) {
    543 		/* Reset session ID */
    544 		if (sav->tdb_cryptoid != 0)
    545 			sav->tdb_cryptoid = crp->crp_sid;
    546 
    547 		if (crp->crp_etype == EAGAIN) {
    548 			IPSEC_RELEASE_GLOBAL_LOCKS();
    549 			(void)crypto_dispatch(crp);
    550 			return;
    551 		}
    552 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    553 		DPRINTF("crypto error %d\n", crp->crp_etype);
    554 		goto bad;
    555 	}
    556 
    557 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    558 
    559 	if (rlen > crp->crp_olen) {
    560 		/* Inject IPCOMP header */
    561 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
    562 		if (mo == NULL) {
    563 			IPCOMP_STATINC(IPCOMP_STAT_WRAP);
    564 			DPRINTF("failed to inject IPCOMP header for "
    565 			    "IPCA %s/%08lx\n",
    566 			    ipsec_address(&sav->sah->saidx.dst, buf,
    567 			    sizeof(buf)), (u_long) ntohl(sav->spi));
    568 			goto bad;
    569 		}
    570 		ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
    571 
    572 		/* Initialize the IPCOMP header */
    573 		/* XXX alignment always correct? */
    574 		switch (sav->sah->saidx.dst.sa.sa_family) {
    575 #ifdef INET
    576 		case AF_INET:
    577 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
    578 			break;
    579 #endif
    580 #ifdef INET6
    581 		case AF_INET6:
    582 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
    583 			break;
    584 #endif
    585 		}
    586 		ipcomp->comp_flags = 0;
    587 
    588 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
    589 			cpi = sav->alg_enc;
    590 		else
    591 			cpi = ntohl(sav->spi) & 0xffff;
    592 		ipcomp->comp_cpi = htons(cpi);
    593 
    594 		/* Fix Next Protocol in IPv4/IPv6 header */
    595 		prot = IPPROTO_IPCOMP;
    596 		m_copyback(m, tc->tc_protoff, sizeof(prot), &prot);
    597 
    598 		/* Adjust the length in the IP header */
    599 		switch (sav->sah->saidx.dst.sa.sa_family) {
    600 #ifdef INET
    601 		case AF_INET:
    602 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    603 			break;
    604 #endif
    605 #ifdef INET6
    606 		case AF_INET6:
    607 			mtod(m, struct ip6_hdr *)->ip6_plen =
    608 			    htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    609 			break;
    610 #endif
    611 		default:
    612 			IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    613 			DPRINTF("unknown/unsupported protocol "
    614 			    "family %d, IPCA %s/%08lx\n",
    615 			    sav->sah->saidx.dst.sa.sa_family,
    616 			    ipsec_address(&sav->sah->saidx.dst, buf,
    617 			    sizeof(buf)), (u_long) ntohl(sav->spi));
    618 			goto bad;
    619 		}
    620 	} else {
    621 		/* compression was useless, we have lost time */
    622 		IPCOMP_STATINC(IPCOMP_STAT_USELESS);
    623 		DPRINTF("compression was useless: initial size was %d "
    624 		    "and compressed size is %d\n", rlen, crp->crp_olen);
    625 	}
    626 
    627 	flags = tc->tc_flags;
    628 	/* Release the crypto descriptor */
    629 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    630 	crypto_freereq(crp);
    631 
    632 	/* NB: m is reclaimed by ipsec_process_done. */
    633 	(void)ipsec_process_done(m, isr, sav, flags);
    634 	KEY_SA_UNREF(&sav);
    635 	KEY_SP_UNREF(&isr->sp);
    636 	IPSEC_RELEASE_GLOBAL_LOCKS();
    637 	return;
    638 
    639 bad:
    640 	if (sav)
    641 		KEY_SA_UNREF(&sav);
    642 	KEY_SP_UNREF(&isr->sp);
    643 	IPSEC_RELEASE_GLOBAL_LOCKS();
    644 	if (m)
    645 		m_freem(m);
    646 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    647 	crypto_freereq(crp);
    648 }
    649 
    650 static struct xformsw ipcomp_xformsw = {
    651 	.xf_type	= XF_IPCOMP,
    652 	.xf_flags	= XFT_COMP,
    653 	.xf_name	= "IPcomp",
    654 	.xf_init	= ipcomp_init,
    655 	.xf_zeroize	= ipcomp_zeroize,
    656 	.xf_input	= ipcomp_input,
    657 	.xf_output	= ipcomp_output,
    658 	.xf_next	= NULL,
    659 };
    660 
    661 void
    662 ipcomp_attach(void)
    663 {
    664 	ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
    665 	ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
    666 	    coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
    667 	    NULL, NULL, NULL);
    668 	xform_register(&ipcomp_xformsw);
    669 }
    670