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