Home | History | Annotate | Line # | Download | only in netipsec
xform_ipcomp.c revision 1.37
      1 /*	$NetBSD: xform_ipcomp.c,v 1.37 2017/04/19 03:39:14 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.37 2017/04/19 03:39:14 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 <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 	struct tdb_crypto *tc;
    234 	int skip, protoff;
    235 	struct mbuf *m;
    236 	struct secasvar *sav;
    237 	struct secasindex *saidx __diagused;
    238 	int s, hlen = IPCOMP_HLENGTH, error, clen;
    239 	uint8_t nproto;
    240 	void *addr;
    241 	uint16_t dport;
    242 	uint16_t sport;
    243 
    244 	KASSERT(crp->crp_opaque != NULL);
    245 	tc = crp->crp_opaque;
    246 	skip = tc->tc_skip;
    247 	protoff = tc->tc_protoff;
    248 	m = crp->crp_buf;
    249 
    250 	/* find the source port for NAT-T */
    251 	nat_t_ports_get(m, &dport, &sport);
    252 
    253 	s = splsoftnet();
    254 	mutex_enter(softnet_lock);
    255 
    256 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, sport, dport);
    257 	if (sav == NULL) {
    258 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    259 		DPRINTF(("%s: SA expired while in crypto\n", __func__));
    260 		error = ENOBUFS;		/*XXX*/
    261 		goto bad;
    262 	}
    263 
    264 	saidx = &sav->sah->saidx;
    265 	KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
    266 	    saidx->dst.sa.sa_family == AF_INET6,
    267 	    "unexpected protocol family %u", saidx->dst.sa.sa_family);
    268 
    269 	/* Check for crypto errors */
    270 	if (crp->crp_etype) {
    271 		/* Reset the session ID */
    272 		if (sav->tdb_cryptoid != 0)
    273 			sav->tdb_cryptoid = crp->crp_sid;
    274 
    275 		if (crp->crp_etype == EAGAIN) {
    276 			KEY_FREESAV(&sav);
    277 			mutex_exit(softnet_lock);
    278 			splx(s);
    279 			return crypto_dispatch(crp);
    280 		}
    281 
    282 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    283 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    284 		error = crp->crp_etype;
    285 		goto bad;
    286 	}
    287 	/* Shouldn't happen... */
    288 	if (m == NULL) {
    289 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    290 		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
    291 		error = EINVAL;
    292 		goto bad;
    293 	}
    294 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    295 
    296 	/* Update the counters */
    297 	IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
    298 
    299 
    300 	clen = crp->crp_olen;		/* Length of data after processing */
    301 
    302 	/* Release the crypto descriptors */
    303 	free(tc, M_XDATA), tc = NULL;
    304 	crypto_freereq(crp), crp = NULL;
    305 
    306 	/* In case it's not done already, adjust the size of the mbuf chain */
    307 	m->m_pkthdr.len = clen + hlen + skip;
    308 
    309 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
    310 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);	/*XXX*/
    311 		DPRINTF(("%s: m_pullup failed\n", __func__));
    312 		error = EINVAL;				/*XXX*/
    313 		goto bad;
    314 	}
    315 
    316 	/* Keep the next protocol field */
    317 	addr = (uint8_t*) mtod(m, struct ip *) + skip;
    318 	nproto = ((struct ipcomp *) addr)->comp_nxt;
    319 	switch (nproto) {
    320 	case IPPROTO_IPCOMP:
    321 	case IPPROTO_AH:
    322 	case IPPROTO_ESP:
    323 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    324 		DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
    325 		    ipsec_address(&sav->sah->saidx.dst),
    326 		    (u_long) ntohl(sav->spi)));
    327 		error = EINVAL;
    328 		goto bad;
    329 	default:
    330 		break;
    331 	}
    332 
    333 	/* Remove the IPCOMP header */
    334 	error = m_striphdr(m, skip, hlen);
    335 	if (error) {
    336 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    337 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
    338 			 ipsec_address(&sav->sah->saidx.dst),
    339 			 (u_long) ntohl(sav->spi)));
    340 		goto bad;
    341 	}
    342 
    343 	/* Restore the Next Protocol field */
    344 	m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *) &nproto);
    345 
    346 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
    347 
    348 	KEY_FREESAV(&sav);
    349 	mutex_exit(softnet_lock);
    350 	splx(s);
    351 	return error;
    352 bad:
    353 	if (sav)
    354 		KEY_FREESAV(&sav);
    355 	mutex_exit(softnet_lock);
    356 	splx(s);
    357 	if (m)
    358 		m_freem(m);
    359 	if (tc != NULL)
    360 		free(tc, M_XDATA);
    361 	if (crp)
    362 		crypto_freereq(crp);
    363 	return error;
    364 }
    365 
    366 /*
    367  * IPComp output routine, called by ipsec[46]_process_packet()
    368  */
    369 static int
    370 ipcomp_output(
    371     struct mbuf *m,
    372     struct ipsecrequest *isr,
    373     struct mbuf **mp,
    374     int skip,
    375     int protoff
    376 )
    377 {
    378 	const struct secasvar *sav;
    379 	const struct comp_algo *ipcompx;
    380 	int error, ralen, hlen, maxpacketsize;
    381 	struct cryptodesc *crdc;
    382 	struct cryptop *crp;
    383 	struct tdb_crypto *tc;
    384 
    385 	IPSEC_SPLASSERT_SOFTNET(__func__);
    386 	KASSERT(isr->sav != NULL);
    387 	sav = isr->sav;
    388 	KASSERT(sav->tdb_compalgxform != NULL);
    389 	ipcompx = sav->tdb_compalgxform;
    390 
    391 	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
    392 
    393     /* Don't process the packet if it is too short */
    394 	if (ralen < ipcompx->minlen) {
    395 		IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
    396 		return ipsec_process_done(m,isr);
    397 	}
    398 
    399 	hlen = IPCOMP_HLENGTH;
    400 
    401 	IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
    402 
    403 	/* Check for maximum packet size violations. */
    404 	switch (sav->sah->saidx.dst.sa.sa_family) {
    405 #ifdef INET
    406 	case AF_INET:
    407 		maxpacketsize =  IP_MAXPACKET;
    408 		break;
    409 #endif /* INET */
    410 #ifdef INET6
    411 	case AF_INET6:
    412 		maxpacketsize =  IPV6_MAXPACKET;
    413 		break;
    414 #endif /* INET6 */
    415 	default:
    416 		IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    417 		DPRINTF(("%s: unknown/unsupported protocol family %d"
    418 		    ", IPCA %s/%08lx\n", __func__,
    419 		    sav->sah->saidx.dst.sa.sa_family,
    420 		    ipsec_address(&sav->sah->saidx.dst),
    421 		    (u_long) ntohl(sav->spi)));
    422 		error = EPFNOSUPPORT;
    423 		goto bad;
    424 	}
    425 	if (skip + hlen + ralen > maxpacketsize) {
    426 		IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
    427 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
    428 		    "(len %u, max len %u)\n", __func__,
    429 		    ipsec_address(&sav->sah->saidx.dst),
    430 		    (u_long) ntohl(sav->spi),
    431 		    skip + hlen + ralen, maxpacketsize));
    432 		error = EMSGSIZE;
    433 		goto bad;
    434 	}
    435 
    436 	/* Update the counters */
    437 	IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
    438 
    439 	m = m_clone(m);
    440 	if (m == NULL) {
    441 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    442 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
    443 		    __func__, ipsec_address(&sav->sah->saidx.dst),
    444 		    (u_long) ntohl(sav->spi)));
    445 		error = ENOBUFS;
    446 		goto bad;
    447 	}
    448 
    449 	/* Ok now, we can pass to the crypto processing */
    450 
    451 	/* Get crypto descriptors */
    452 	crp = crypto_getreq(1);
    453 	if (crp == NULL) {
    454 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    455 		DPRINTF(("%s: failed to acquire crypto descriptor\n",
    456 		    __func__));
    457 		error = ENOBUFS;
    458 		goto bad;
    459 	}
    460 	crdc = crp->crp_desc;
    461 
    462 	/* Compression descriptor */
    463 	crdc->crd_skip = skip;
    464 	crdc->crd_len = m->m_pkthdr.len - skip;
    465 	crdc->crd_flags = CRD_F_COMP;
    466 	crdc->crd_inject = skip;
    467 
    468 	/* Compression operation */
    469 	crdc->crd_alg = ipcompx->type;
    470 
    471 	/* IPsec-specific opaque crypto info */
    472 	tc = malloc(sizeof(*tc), M_XDATA, M_NOWAIT|M_ZERO);
    473 	if (tc == NULL) {
    474 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    475 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
    476 		crypto_freereq(crp);
    477 		error = ENOBUFS;
    478 		goto bad;
    479 	}
    480 
    481 	tc->tc_isr = isr;
    482 	tc->tc_spi = sav->spi;
    483 	tc->tc_dst = sav->sah->saidx.dst;
    484 	tc->tc_proto = sav->sah->saidx.proto;
    485 	tc->tc_skip = skip;
    486 	tc->tc_protoff = protoff;
    487 
    488 	/* Crypto operation descriptor */
    489 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
    490 	crp->crp_flags = CRYPTO_F_IMBUF;
    491 	crp->crp_buf = m;
    492 	crp->crp_callback = ipcomp_output_cb;
    493 	crp->crp_opaque = tc;
    494 	crp->crp_sid = sav->tdb_cryptoid;
    495 
    496 	return crypto_dispatch(crp);
    497 bad:
    498 	if (m)
    499 		m_freem(m);
    500 	return (error);
    501 }
    502 
    503 /*
    504  * IPComp output callback from the crypto driver.
    505  */
    506 static int
    507 ipcomp_output_cb(struct cryptop *crp)
    508 {
    509 	struct tdb_crypto *tc;
    510 	struct ipsecrequest *isr;
    511 	struct secasvar *sav;
    512 	struct mbuf *m, *mo;
    513 	int s, error, skip, rlen, roff;
    514 	uint8_t prot;
    515 	uint16_t cpi;
    516 	struct ipcomp * ipcomp;
    517 
    518 	KASSERT(crp->crp_opaque != NULL);
    519 	tc = crp->crp_opaque;
    520 	m = crp->crp_buf;
    521 	skip = tc->tc_skip;
    522 	rlen = crp->crp_ilen - skip;
    523 
    524 	s = splsoftnet();
    525 	mutex_enter(softnet_lock);
    526 
    527 	isr = tc->tc_isr;
    528 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
    529 	if (sav == NULL) {
    530 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    531 		DPRINTF(("%s: SA expired while in crypto\n", __func__));
    532 		error = ENOBUFS;		/*XXX*/
    533 		goto bad;
    534 	}
    535 	KASSERTMSG(isr->sav == sav, "SA changed");
    536 
    537 	/* Check for crypto errors */
    538 	if (crp->crp_etype) {
    539 		/* Reset session ID */
    540 		if (sav->tdb_cryptoid != 0)
    541 			sav->tdb_cryptoid = crp->crp_sid;
    542 
    543 		if (crp->crp_etype == EAGAIN) {
    544 			KEY_FREESAV(&sav);
    545 			mutex_exit(softnet_lock);
    546 			splx(s);
    547 			return crypto_dispatch(crp);
    548 		}
    549 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    550 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    551 		error = crp->crp_etype;
    552 		goto bad;
    553 	}
    554 	/* Shouldn't happen... */
    555 	if (m == NULL) {
    556 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    557 		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
    558 		error = EINVAL;
    559 		goto bad;
    560 	}
    561 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    562 
    563 	if (rlen > crp->crp_olen) {
    564 		/* Inject IPCOMP header */
    565 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
    566 		if (mo == NULL) {
    567 			IPCOMP_STATINC(IPCOMP_STAT_WRAP);
    568 			DPRINTF(("%s: failed to inject IPCOMP header for "
    569 			    "IPCA %s/%08lx\n", __func__,
    570 			    ipsec_address(&sav->sah->saidx.dst),
    571 			    (u_long) ntohl(sav->spi)));
    572 			error = ENOBUFS;
    573 			goto bad;
    574 		}
    575 		ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
    576 
    577 		/* Initialize the IPCOMP header */
    578 		/* XXX alignment always correct? */
    579 		switch (sav->sah->saidx.dst.sa.sa_family) {
    580 #ifdef INET
    581 		case AF_INET:
    582 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
    583 			 break;
    584 #endif /* INET */
    585 #ifdef INET6
    586 		case AF_INET6:
    587 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
    588 		break;
    589 #endif
    590 		}
    591 		ipcomp->comp_flags = 0;
    592 
    593 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
    594 			 cpi = sav->alg_enc;
    595 		else
    596 			cpi = ntohl(sav->spi) & 0xffff;
    597 		ipcomp->comp_cpi = htons(cpi);
    598 
    599 		/* Fix Next Protocol in IPv4/IPv6 header */
    600 		prot = IPPROTO_IPCOMP;
    601 		m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
    602 
    603 		/* Adjust the length in the IP header */
    604 		switch (sav->sah->saidx.dst.sa.sa_family) {
    605 #ifdef INET
    606 		case AF_INET:
    607 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    608 			break;
    609 #endif /* INET */
    610 #ifdef INET6
    611 		case AF_INET6:
    612 			mtod(m, struct ip6_hdr *)->ip6_plen =
    613 				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
    614 			break;
    615 #endif /* INET6 */
    616 		default:
    617 			IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    618 			DPRINTF(("ipcomp_output: unknown/unsupported protocol "
    619 			    "family %d, IPCA %s/%08lx\n",
    620 			    sav->sah->saidx.dst.sa.sa_family,
    621 			    ipsec_address(&sav->sah->saidx.dst),
    622 			    (u_long) ntohl(sav->spi)));
    623 			error = EPFNOSUPPORT;
    624 			goto bad;
    625 		}
    626 	} else {
    627 		/* compression was useless, we have lost time */
    628 		IPCOMP_STATINC(IPCOMP_STAT_USELESS);
    629 		DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
    630 				   	"and compressed size is %d\n", rlen, crp->crp_olen));
    631 	}
    632 
    633 
    634 	/* Release the crypto descriptor */
    635 	free(tc, M_XDATA);
    636 	crypto_freereq(crp);
    637 
    638 	/* NB: m is reclaimed by ipsec_process_done. */
    639 	error = ipsec_process_done(m, isr);
    640 	KEY_FREESAV(&sav);
    641 	mutex_exit(softnet_lock);
    642 	splx(s);
    643 	return error;
    644 bad:
    645 	if (sav)
    646 		KEY_FREESAV(&sav);
    647 	mutex_exit(softnet_lock);
    648 	splx(s);
    649 	if (m)
    650 		m_freem(m);
    651 	free(tc, M_XDATA);
    652 	crypto_freereq(crp);
    653 	return error;
    654 }
    655 
    656 static struct xformsw ipcomp_xformsw = {
    657 	XF_IPCOMP,		XFT_COMP,		"IPcomp",
    658 	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
    659 	ipcomp_output,
    660 	NULL,
    661 };
    662 
    663 void
    664 ipcomp_attach(void)
    665 {
    666 	ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
    667 	xform_register(&ipcomp_xformsw);
    668 }
    669