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