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