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