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