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