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