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