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