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