xref: /freebsd/sys/netipsec/xform_ipcomp.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /*	$FreeBSD$	*/
2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3 
4 /*-
5  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in the
15  *   documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *   derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* IP payload compression protocol (IPComp), see RFC 2393 */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/rwlock.h>
41 #include <sys/socket.h>
42 #include <sys/kernel.h>
43 #include <sys/protosw.h>
44 #include <sys/sysctl.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_var.h>
50 
51 #include <net/route.h>
52 #include <net/vnet.h>
53 
54 #include <netipsec/ipsec.h>
55 #include <netipsec/xform.h>
56 
57 #ifdef INET6
58 #include <netinet/ip6.h>
59 #include <netipsec/ipsec6.h>
60 #endif
61 
62 #include <netipsec/ipcomp.h>
63 #include <netipsec/ipcomp_var.h>
64 
65 #include <netipsec/key.h>
66 #include <netipsec/key_debug.h>
67 
68 #include <opencrypto/cryptodev.h>
69 #include <opencrypto/deflate.h>
70 #include <opencrypto/xform.h>
71 
72 VNET_DEFINE(int, ipcomp_enable) = 1;
73 VNET_DEFINE(struct ipcompstat, ipcompstat);
74 
75 SYSCTL_DECL(_net_inet_ipcomp);
76 SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
77 	ipcomp_enable,	CTLFLAG_RW,	&VNET_NAME(ipcomp_enable),	0, "");
78 SYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
79 	stats,		CTLFLAG_RD,	&VNET_NAME(ipcompstat),	ipcompstat, "");
80 
81 static int ipcomp_input_cb(struct cryptop *crp);
82 static int ipcomp_output_cb(struct cryptop *crp);
83 
84 struct comp_algo *
85 ipcomp_algorithm_lookup(int alg)
86 {
87 	if (alg >= IPCOMP_ALG_MAX)
88 		return NULL;
89 	switch (alg) {
90 	case SADB_X_CALG_DEFLATE:
91 		return &comp_algo_deflate;
92 	}
93 	return NULL;
94 }
95 
96 /*
97  * ipcomp_init() is called when an CPI is being set up.
98  */
99 static int
100 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
101 {
102 	struct comp_algo *tcomp;
103 	struct cryptoini cric;
104 
105 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
106 	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
107 	if (tcomp == NULL) {
108 		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
109 			 sav->alg_comp));
110 		return EINVAL;
111 	}
112 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
113 	sav->tdb_xform = xsp;
114 	sav->tdb_compalgxform = tcomp;
115 
116 	/* Initialize crypto session */
117 	bzero(&cric, sizeof (cric));
118 	cric.cri_alg = sav->tdb_compalgxform->type;
119 
120 	return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
121 }
122 
123 /*
124  * ipcomp_zeroize() used when IPCA is deleted
125  */
126 static int
127 ipcomp_zeroize(struct secasvar *sav)
128 {
129 	int err;
130 
131 	err = crypto_freesession(sav->tdb_cryptoid);
132 	sav->tdb_cryptoid = 0;
133 	return err;
134 }
135 
136 /*
137  * ipcomp_input() gets called to uncompress an input packet
138  */
139 static int
140 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
141 {
142 	struct tdb_crypto *tc;
143 	struct cryptodesc *crdc;
144 	struct cryptop *crp;
145 	struct ipcomp *ipcomp;
146 	caddr_t addr;
147 	int hlen = IPCOMP_HLENGTH;
148 
149 	/*
150 	 * Check that the next header of the IPComp is not IPComp again, before
151 	 * doing any real work.  Given it is not possible to do double
152 	 * compression it means someone is playing tricks on us.
153 	 */
154 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
155 		V_ipcompstat.ipcomps_hdrops++;		/*XXX*/
156 		DPRINTF(("%s: m_pullup failed\n", __func__));
157 		return (ENOBUFS);
158 	}
159 	addr = (caddr_t) mtod(m, struct ip *) + skip;
160 	ipcomp = (struct ipcomp *)addr;
161 	if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
162 		m_freem(m);
163 		V_ipcompstat.ipcomps_pdrops++;	/* XXX have our own stats? */
164 		DPRINTF(("%s: recursive compression detected\n", __func__));
165 		return (EINVAL);
166 	}
167 
168 	/* Get crypto descriptors */
169 	crp = crypto_getreq(1);
170 	if (crp == NULL) {
171 		m_freem(m);
172 		DPRINTF(("%s: no crypto descriptors\n", __func__));
173 		V_ipcompstat.ipcomps_crypto++;
174 		return ENOBUFS;
175 	}
176 	/* Get IPsec-specific opaque pointer */
177 	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
178 	if (tc == NULL) {
179 		m_freem(m);
180 		crypto_freereq(crp);
181 		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
182 		V_ipcompstat.ipcomps_crypto++;
183 		return ENOBUFS;
184 	}
185 	crdc = crp->crp_desc;
186 
187 	crdc->crd_skip = skip + hlen;
188 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
189 	crdc->crd_inject = skip;
190 
191 	tc->tc_ptr = 0;
192 
193 	/* Decompression operation */
194 	crdc->crd_alg = sav->tdb_compalgxform->type;
195 
196 	/* Crypto operation descriptor */
197 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
198 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
199 	crp->crp_buf = (caddr_t) m;
200 	crp->crp_callback = ipcomp_input_cb;
201 	crp->crp_sid = sav->tdb_cryptoid;
202 	crp->crp_opaque = (caddr_t) 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 	KEY_ADDREFSA(sav);
211 	tc->tc_sav = sav;
212 
213 	return crypto_dispatch(crp);
214 }
215 
216 /*
217  * IPComp input callback from the crypto driver.
218  */
219 static int
220 ipcomp_input_cb(struct cryptop *crp)
221 {
222 	struct cryptodesc *crd;
223 	struct tdb_crypto *tc;
224 	int skip, protoff;
225 	struct mtag *mtag;
226 	struct mbuf *m;
227 	struct secasvar *sav;
228 	struct secasindex *saidx;
229 	int hlen = IPCOMP_HLENGTH, error, clen;
230 	u_int8_t nproto;
231 	caddr_t addr;
232 
233 	crd = crp->crp_desc;
234 
235 	tc = (struct tdb_crypto *) crp->crp_opaque;
236 	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
237 	skip = tc->tc_skip;
238 	protoff = tc->tc_protoff;
239 	mtag = (struct mtag *) tc->tc_ptr;
240 	m = (struct mbuf *) crp->crp_buf;
241 
242 	sav = tc->tc_sav;
243 	IPSEC_ASSERT(sav != NULL, ("null SA!"));
244 
245 	saidx = &sav->sah->saidx;
246 	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
247 		saidx->dst.sa.sa_family == AF_INET6,
248 		("unexpected protocol family %u", saidx->dst.sa.sa_family));
249 
250 	/* Check for crypto errors */
251 	if (crp->crp_etype) {
252 		/* Reset the session ID */
253 		if (sav->tdb_cryptoid != 0)
254 			sav->tdb_cryptoid = crp->crp_sid;
255 
256 		if (crp->crp_etype == EAGAIN) {
257 			return crypto_dispatch(crp);
258 		}
259 		V_ipcompstat.ipcomps_noxform++;
260 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
261 		error = crp->crp_etype;
262 		goto bad;
263 	}
264 	/* Shouldn't happen... */
265 	if (m == NULL) {
266 		V_ipcompstat.ipcomps_crypto++;
267 		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
268 		error = EINVAL;
269 		goto bad;
270 	}
271 	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
272 
273 	clen = crp->crp_olen;		/* Length of data after processing */
274 
275 	/* Release the crypto descriptors */
276 	free(tc, M_XDATA), tc = NULL;
277 	crypto_freereq(crp), crp = NULL;
278 
279 	/* In case it's not done already, adjust the size of the mbuf chain */
280 	m->m_pkthdr.len = clen + hlen + skip;
281 
282 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
283 		V_ipcompstat.ipcomps_hdrops++;		/*XXX*/
284 		DPRINTF(("%s: m_pullup failed\n", __func__));
285 		error = EINVAL;				/*XXX*/
286 		goto bad;
287 	}
288 
289 	/* Keep the next protocol field */
290 	addr = (caddr_t) mtod(m, struct ip *) + skip;
291 	nproto = ((struct ipcomp *) addr)->comp_nxt;
292 
293 	/* Remove the IPCOMP header */
294 	error = m_striphdr(m, skip, hlen);
295 	if (error) {
296 		V_ipcompstat.ipcomps_hdrops++;
297 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
298 			 ipsec_address(&sav->sah->saidx.dst),
299 			 (u_long) ntohl(sav->spi)));
300 		goto bad;
301 	}
302 
303 	/* Restore the Next Protocol field */
304 	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
305 
306 	switch (saidx->dst.sa.sa_family) {
307 #ifdef INET6
308 	case AF_INET6:
309 		error = ipsec6_common_input_cb(m, sav, skip, protoff, NULL);
310 		break;
311 #endif
312 #ifdef INET
313 	case AF_INET:
314 		error = ipsec4_common_input_cb(m, sav, skip, protoff, NULL);
315 		break;
316 #endif
317 	default:
318 		panic("%s: Unexpected address family: %d saidx=%p", __func__,
319 		    saidx->dst.sa.sa_family, saidx);
320 	}
321 
322 	KEY_FREESAV(&sav);
323 	return error;
324 bad:
325 	if (sav)
326 		KEY_FREESAV(&sav);
327 	if (m)
328 		m_freem(m);
329 	if (tc != NULL)
330 		free(tc, M_XDATA);
331 	if (crp)
332 		crypto_freereq(crp);
333 	return error;
334 }
335 
336 /*
337  * IPComp output routine, called by ipsec[46]_process_packet()
338  */
339 static int
340 ipcomp_output(
341 	struct mbuf *m,
342 	struct ipsecrequest *isr,
343 	struct mbuf **mp,
344 	int skip,
345 	int protoff
346 )
347 {
348 	struct secasvar *sav;
349 	struct comp_algo *ipcompx;
350 	int error, ralen, maxpacketsize;
351 	struct cryptodesc *crdc;
352 	struct cryptop *crp;
353 	struct tdb_crypto *tc;
354 
355 	sav = isr->sav;
356 	IPSEC_ASSERT(sav != NULL, ("null SA"));
357 	ipcompx = sav->tdb_compalgxform;
358 	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
359 
360 	/*
361 	 * Do not touch the packet in case our payload to compress
362 	 * is lower than the minimal threshold of the compression
363 	 * alogrithm.  We will just send out the data uncompressed.
364 	 * See RFC 3173, 2.2. Non-Expansion Policy.
365 	 */
366 	if (m->m_pkthdr.len <= ipcompx->minlen) {
367 		V_ipcompstat.ipcomps_threshold++;
368 		return ipsec_process_done(m, isr);
369 	}
370 
371 	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
372 	V_ipcompstat.ipcomps_output++;
373 
374 	/* Check for maximum packet size violations. */
375 	switch (sav->sah->saidx.dst.sa.sa_family) {
376 #ifdef INET
377 	case AF_INET:
378 		maxpacketsize = IP_MAXPACKET;
379 		break;
380 #endif /* INET */
381 #ifdef INET6
382 	case AF_INET6:
383 		maxpacketsize = IPV6_MAXPACKET;
384 		break;
385 #endif /* INET6 */
386 	default:
387 		V_ipcompstat.ipcomps_nopf++;
388 		DPRINTF(("%s: unknown/unsupported protocol family %d, "
389 		    "IPCA %s/%08lx\n", __func__,
390 		    sav->sah->saidx.dst.sa.sa_family,
391 		    ipsec_address(&sav->sah->saidx.dst),
392 		    (u_long) ntohl(sav->spi)));
393 		error = EPFNOSUPPORT;
394 		goto bad;
395 	}
396 	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
397 		V_ipcompstat.ipcomps_toobig++;
398 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
399 		    "(len %u, max len %u)\n", __func__,
400 		    ipsec_address(&sav->sah->saidx.dst),
401 		    (u_long) ntohl(sav->spi),
402 		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
403 		error = EMSGSIZE;
404 		goto bad;
405 	}
406 
407 	/* Update the counters */
408 	V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
409 
410 	m = m_unshare(m, M_NOWAIT);
411 	if (m == NULL) {
412 		V_ipcompstat.ipcomps_hdrops++;
413 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
414 		    __func__, ipsec_address(&sav->sah->saidx.dst),
415 		    (u_long) ntohl(sav->spi)));
416 		error = ENOBUFS;
417 		goto bad;
418 	}
419 
420 	/* Ok now, we can pass to the crypto processing. */
421 
422 	/* Get crypto descriptors */
423 	crp = crypto_getreq(1);
424 	if (crp == NULL) {
425 		V_ipcompstat.ipcomps_crypto++;
426 		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
427 		error = ENOBUFS;
428 		goto bad;
429 	}
430 	crdc = crp->crp_desc;
431 
432 	/* Compression descriptor */
433 	crdc->crd_skip = skip;
434 	crdc->crd_len = ralen;
435 	crdc->crd_flags = CRD_F_COMP;
436 	crdc->crd_inject = skip;
437 
438 	/* Compression operation */
439 	crdc->crd_alg = ipcompx->type;
440 
441 	/* IPsec-specific opaque crypto info */
442 	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
443 		M_XDATA, M_NOWAIT|M_ZERO);
444 	if (tc == NULL) {
445 		V_ipcompstat.ipcomps_crypto++;
446 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
447 		crypto_freereq(crp);
448 		error = ENOBUFS;
449 		goto bad;
450 	}
451 
452 	tc->tc_isr = isr;
453 	KEY_ADDREFSA(sav);
454 	tc->tc_sav = sav;
455 	tc->tc_spi = sav->spi;
456 	tc->tc_dst = sav->sah->saidx.dst;
457 	tc->tc_proto = sav->sah->saidx.proto;
458 	tc->tc_protoff = protoff;
459 	tc->tc_skip = skip;
460 
461 	/* Crypto operation descriptor */
462 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
463 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
464 	crp->crp_buf = (caddr_t) m;
465 	crp->crp_callback = ipcomp_output_cb;
466 	crp->crp_opaque = (caddr_t) tc;
467 	crp->crp_sid = sav->tdb_cryptoid;
468 
469 	return crypto_dispatch(crp);
470 bad:
471 	if (m)
472 		m_freem(m);
473 	return (error);
474 }
475 
476 /*
477  * IPComp output callback from the crypto driver.
478  */
479 static int
480 ipcomp_output_cb(struct cryptop *crp)
481 {
482 	struct tdb_crypto *tc;
483 	struct ipsecrequest *isr;
484 	struct secasvar *sav;
485 	struct mbuf *m;
486 	int error, skip;
487 
488 	tc = (struct tdb_crypto *) crp->crp_opaque;
489 	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
490 	m = (struct mbuf *) crp->crp_buf;
491 	skip = tc->tc_skip;
492 
493 	isr = tc->tc_isr;
494 	IPSECREQUEST_LOCK(isr);
495 	sav = tc->tc_sav;
496 	/* With the isr lock released SA pointer can be updated. */
497 	if (sav != isr->sav) {
498 		V_ipcompstat.ipcomps_notdb++;
499 		DPRINTF(("%s: SA expired while in crypto\n", __func__));
500 		error = ENOBUFS;		/*XXX*/
501 		goto bad;
502 	}
503 
504 	/* Check for crypto errors */
505 	if (crp->crp_etype) {
506 		/* Reset the session ID */
507 		if (sav->tdb_cryptoid != 0)
508 			sav->tdb_cryptoid = crp->crp_sid;
509 
510 		if (crp->crp_etype == EAGAIN) {
511 			IPSECREQUEST_UNLOCK(isr);
512 			return crypto_dispatch(crp);
513 		}
514 		V_ipcompstat.ipcomps_noxform++;
515 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
516 		error = crp->crp_etype;
517 		goto bad;
518 	}
519 	/* Shouldn't happen... */
520 	if (m == NULL) {
521 		V_ipcompstat.ipcomps_crypto++;
522 		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
523 		error = EINVAL;
524 		goto bad;
525 	}
526 	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
527 
528 	if (crp->crp_ilen - skip > crp->crp_olen) {
529 		struct mbuf *mo;
530 		struct ipcomp *ipcomp;
531 		int roff;
532 		uint8_t prot;
533 
534 		/* Compression helped, inject IPCOMP header. */
535 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
536 		if (mo == NULL) {
537 			V_ipcompstat.ipcomps_wrap++;
538 			DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
539 			    __func__, ipsec_address(&sav->sah->saidx.dst),
540 			    (u_long) ntohl(sav->spi)));
541 			error = ENOBUFS;
542 			goto bad;
543 		}
544 		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
545 
546 		/* Initialize the IPCOMP header */
547 		/* XXX alignment always correct? */
548 		switch (sav->sah->saidx.dst.sa.sa_family) {
549 #ifdef INET
550 		case AF_INET:
551 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
552 			break;
553 #endif /* INET */
554 #ifdef INET6
555 		case AF_INET6:
556 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
557 			break;
558 #endif
559 		}
560 		ipcomp->comp_flags = 0;
561 		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
562 
563 		/* Fix Next Protocol in IPv4/IPv6 header */
564 		prot = IPPROTO_IPCOMP;
565 		m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
566 		    (u_char *)&prot);
567 
568 		/* Adjust the length in the IP header */
569 		switch (sav->sah->saidx.dst.sa.sa_family) {
570 #ifdef INET
571 		case AF_INET:
572 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
573 			break;
574 #endif /* INET */
575 #ifdef INET6
576 		case AF_INET6:
577 			mtod(m, struct ip6_hdr *)->ip6_plen =
578 				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
579 			break;
580 #endif /* INET6 */
581 		default:
582 			V_ipcompstat.ipcomps_nopf++;
583 			DPRINTF(("%s: unknown/unsupported protocol "
584 			    "family %d, IPCA %s/%08lx\n", __func__,
585 			    sav->sah->saidx.dst.sa.sa_family,
586 			    ipsec_address(&sav->sah->saidx.dst),
587 			    (u_long) ntohl(sav->spi)));
588 			error = EPFNOSUPPORT;
589 			goto bad;
590 		}
591 	} else {
592 		/* Compression was useless, we have lost time. */
593 		V_ipcompstat.ipcomps_uncompr++;
594 		DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
595 		    __func__, crp->crp_ilen, skip, crp->crp_olen));
596 		/* XXX remember state to not compress the next couple
597 		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
598 	}
599 
600 	/* Release the crypto descriptor */
601 	free(tc, M_XDATA);
602 	crypto_freereq(crp);
603 
604 	/* NB: m is reclaimed by ipsec_process_done. */
605 	error = ipsec_process_done(m, isr);
606 	KEY_FREESAV(&sav);
607 	IPSECREQUEST_UNLOCK(isr);
608 	return error;
609 bad:
610 	if (sav)
611 		KEY_FREESAV(&sav);
612 	IPSECREQUEST_UNLOCK(isr);
613 	if (m)
614 		m_freem(m);
615 	free(tc, M_XDATA);
616 	crypto_freereq(crp);
617 	return error;
618 }
619 
620 static struct xformsw ipcomp_xformsw = {
621 	XF_IPCOMP,		XFT_COMP,		"IPcomp",
622 	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
623 	ipcomp_output
624 };
625 
626 static void
627 ipcomp_attach(void)
628 {
629 
630 	xform_register(&ipcomp_xformsw);
631 }
632 
633 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
634 
635 static void
636 vnet_ipcomp_attach(const void *unused __unused)
637 {
638 
639 	V_ipcompstat.version = IPCOMPSTAT_VERSION;
640 }
641 
642 VNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
643     vnet_ipcomp_attach, NULL);
644