xref: /freebsd/sys/netipsec/xform_ipcomp.c (revision e91afc1cda50cbcb8fffa3f52cc0f8c595a392a3)
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_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
74 VNET_PCPUSTAT_SYSINIT(ipcompstat);
75 
76 #ifdef VIMAGE
77 VNET_PCPUSTAT_SYSUNINIT(ipcompstat);
78 #endif /* VIMAGE */
79 
80 SYSCTL_DECL(_net_inet_ipcomp);
81 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable,
82 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
83 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
84     struct ipcompstat, ipcompstat,
85     "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
86 
87 static int ipcomp_input_cb(struct cryptop *crp);
88 static int ipcomp_output_cb(struct cryptop *crp);
89 
90 struct comp_algo *
91 ipcomp_algorithm_lookup(int alg)
92 {
93 	if (alg >= IPCOMP_ALG_MAX)
94 		return NULL;
95 	switch (alg) {
96 	case SADB_X_CALG_DEFLATE:
97 		return &comp_algo_deflate;
98 	}
99 	return NULL;
100 }
101 
102 /*
103  * ipcomp_init() is called when an CPI is being set up.
104  */
105 static int
106 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
107 {
108 	struct comp_algo *tcomp;
109 	struct cryptoini cric;
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(("%s: unsupported compression algorithm %d\n", __func__,
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 	return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
127 }
128 
129 /*
130  * ipcomp_zeroize() used when IPCA is deleted
131  */
132 static int
133 ipcomp_zeroize(struct secasvar *sav)
134 {
135 	int err;
136 
137 	err = crypto_freesession(sav->tdb_cryptoid);
138 	sav->tdb_cryptoid = 0;
139 	return err;
140 }
141 
142 /*
143  * ipcomp_input() gets called to uncompress an input packet
144  */
145 static int
146 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
147 {
148 	struct tdb_crypto *tc;
149 	struct cryptodesc *crdc;
150 	struct cryptop *crp;
151 	struct ipcomp *ipcomp;
152 	caddr_t addr;
153 	int hlen = IPCOMP_HLENGTH;
154 
155 	/*
156 	 * Check that the next header of the IPComp is not IPComp again, before
157 	 * doing any real work.  Given it is not possible to do double
158 	 * compression it means someone is playing tricks on us.
159 	 */
160 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
161 		IPCOMPSTAT_INC(ipcomps_hdrops);		/*XXX*/
162 		DPRINTF(("%s: m_pullup failed\n", __func__));
163 		return (ENOBUFS);
164 	}
165 	addr = (caddr_t) mtod(m, struct ip *) + skip;
166 	ipcomp = (struct ipcomp *)addr;
167 	if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
168 		m_freem(m);
169 		IPCOMPSTAT_INC(ipcomps_pdrops);	/* XXX have our own stats? */
170 		DPRINTF(("%s: recursive compression detected\n", __func__));
171 		return (EINVAL);
172 	}
173 
174 	/* Get crypto descriptors */
175 	crp = crypto_getreq(1);
176 	if (crp == NULL) {
177 		m_freem(m);
178 		DPRINTF(("%s: no crypto descriptors\n", __func__));
179 		IPCOMPSTAT_INC(ipcomps_crypto);
180 		return ENOBUFS;
181 	}
182 	/* Get IPsec-specific opaque pointer */
183 	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
184 	if (tc == NULL) {
185 		m_freem(m);
186 		crypto_freereq(crp);
187 		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
188 		IPCOMPSTAT_INC(ipcomps_crypto);
189 		return ENOBUFS;
190 	}
191 	crdc = crp->crp_desc;
192 
193 	crdc->crd_skip = skip + hlen;
194 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
195 	crdc->crd_inject = skip;
196 
197 	tc->tc_ptr = 0;
198 
199 	/* Decompression operation */
200 	crdc->crd_alg = sav->tdb_compalgxform->type;
201 
202 	/* Crypto operation descriptor */
203 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
204 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
205 	crp->crp_buf = (caddr_t) m;
206 	crp->crp_callback = ipcomp_input_cb;
207 	crp->crp_sid = sav->tdb_cryptoid;
208 	crp->crp_opaque = (caddr_t) tc;
209 
210 	/* These are passed as-is to the callback */
211 	tc->tc_spi = sav->spi;
212 	tc->tc_dst = sav->sah->saidx.dst;
213 	tc->tc_proto = sav->sah->saidx.proto;
214 	tc->tc_protoff = protoff;
215 	tc->tc_skip = skip;
216 	KEY_ADDREFSA(sav);
217 	tc->tc_sav = sav;
218 
219 	return crypto_dispatch(crp);
220 }
221 
222 /*
223  * IPComp input callback from the crypto driver.
224  */
225 static int
226 ipcomp_input_cb(struct cryptop *crp)
227 {
228 	struct cryptodesc *crd;
229 	struct tdb_crypto *tc;
230 	int skip, protoff;
231 	struct mtag *mtag;
232 	struct mbuf *m;
233 	struct secasvar *sav;
234 	struct secasindex *saidx;
235 	int hlen = IPCOMP_HLENGTH, error, clen;
236 	u_int8_t nproto;
237 	caddr_t addr;
238 
239 	crd = crp->crp_desc;
240 
241 	tc = (struct tdb_crypto *) crp->crp_opaque;
242 	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
243 	skip = tc->tc_skip;
244 	protoff = tc->tc_protoff;
245 	mtag = (struct mtag *) tc->tc_ptr;
246 	m = (struct mbuf *) crp->crp_buf;
247 
248 	sav = tc->tc_sav;
249 	IPSEC_ASSERT(sav != NULL, ("null SA!"));
250 
251 	saidx = &sav->sah->saidx;
252 	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
253 		saidx->dst.sa.sa_family == AF_INET6,
254 		("unexpected protocol family %u", saidx->dst.sa.sa_family));
255 
256 	/* Check for crypto errors */
257 	if (crp->crp_etype) {
258 		/* Reset the session ID */
259 		if (sav->tdb_cryptoid != 0)
260 			sav->tdb_cryptoid = crp->crp_sid;
261 
262 		if (crp->crp_etype == EAGAIN) {
263 			return crypto_dispatch(crp);
264 		}
265 		IPCOMPSTAT_INC(ipcomps_noxform);
266 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
267 		error = crp->crp_etype;
268 		goto bad;
269 	}
270 	/* Shouldn't happen... */
271 	if (m == NULL) {
272 		IPCOMPSTAT_INC(ipcomps_crypto);
273 		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
274 		error = EINVAL;
275 		goto bad;
276 	}
277 	IPCOMPSTAT_INC(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_INC(ipcomps_hdrops);		/*XXX*/
290 		DPRINTF(("%s: m_pullup failed\n", __func__));
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_INC(ipcomps_hdrops);
303 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
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 	switch (saidx->dst.sa.sa_family) {
313 #ifdef INET6
314 	case AF_INET6:
315 		error = ipsec6_common_input_cb(m, sav, skip, protoff, NULL);
316 		break;
317 #endif
318 #ifdef INET
319 	case AF_INET:
320 		error = ipsec4_common_input_cb(m, sav, skip, protoff, NULL);
321 		break;
322 #endif
323 	default:
324 		panic("%s: Unexpected address family: %d saidx=%p", __func__,
325 		    saidx->dst.sa.sa_family, saidx);
326 	}
327 
328 	KEY_FREESAV(&sav);
329 	return error;
330 bad:
331 	if (sav)
332 		KEY_FREESAV(&sav);
333 	if (m)
334 		m_freem(m);
335 	if (tc != NULL)
336 		free(tc, M_XDATA);
337 	if (crp)
338 		crypto_freereq(crp);
339 	return error;
340 }
341 
342 /*
343  * IPComp output routine, called by ipsec[46]_process_packet()
344  */
345 static int
346 ipcomp_output(
347 	struct mbuf *m,
348 	struct ipsecrequest *isr,
349 	struct mbuf **mp,
350 	int skip,
351 	int protoff
352 )
353 {
354 	struct secasvar *sav;
355 	struct comp_algo *ipcompx;
356 	int error, ralen, maxpacketsize;
357 	struct cryptodesc *crdc;
358 	struct cryptop *crp;
359 	struct tdb_crypto *tc;
360 
361 	sav = isr->sav;
362 	IPSEC_ASSERT(sav != NULL, ("null SA"));
363 	ipcompx = sav->tdb_compalgxform;
364 	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
365 
366 	/*
367 	 * Do not touch the packet in case our payload to compress
368 	 * is lower than the minimal threshold of the compression
369 	 * alogrithm.  We will just send out the data uncompressed.
370 	 * See RFC 3173, 2.2. Non-Expansion Policy.
371 	 */
372 	if (m->m_pkthdr.len <= ipcompx->minlen) {
373 		IPCOMPSTAT_INC(ipcomps_threshold);
374 		return ipsec_process_done(m, isr);
375 	}
376 
377 	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
378 	IPCOMPSTAT_INC(ipcomps_output);
379 
380 	/* Check for maximum packet size violations. */
381 	switch (sav->sah->saidx.dst.sa.sa_family) {
382 #ifdef INET
383 	case AF_INET:
384 		maxpacketsize = IP_MAXPACKET;
385 		break;
386 #endif /* INET */
387 #ifdef INET6
388 	case AF_INET6:
389 		maxpacketsize = IPV6_MAXPACKET;
390 		break;
391 #endif /* INET6 */
392 	default:
393 		IPCOMPSTAT_INC(ipcomps_nopf);
394 		DPRINTF(("%s: unknown/unsupported protocol family %d, "
395 		    "IPCA %s/%08lx\n", __func__,
396 		    sav->sah->saidx.dst.sa.sa_family,
397 		    ipsec_address(&sav->sah->saidx.dst),
398 		    (u_long) ntohl(sav->spi)));
399 		error = EPFNOSUPPORT;
400 		goto bad;
401 	}
402 	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
403 		IPCOMPSTAT_INC(ipcomps_toobig);
404 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
405 		    "(len %u, max len %u)\n", __func__,
406 		    ipsec_address(&sav->sah->saidx.dst),
407 		    (u_long) ntohl(sav->spi),
408 		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
409 		error = EMSGSIZE;
410 		goto bad;
411 	}
412 
413 	/* Update the counters */
414 	IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
415 
416 	m = m_unshare(m, M_NOWAIT);
417 	if (m == NULL) {
418 		IPCOMPSTAT_INC(ipcomps_hdrops);
419 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
420 		    __func__, ipsec_address(&sav->sah->saidx.dst),
421 		    (u_long) ntohl(sav->spi)));
422 		error = ENOBUFS;
423 		goto bad;
424 	}
425 
426 	/* Ok now, we can pass to the crypto processing. */
427 
428 	/* Get crypto descriptors */
429 	crp = crypto_getreq(1);
430 	if (crp == NULL) {
431 		IPCOMPSTAT_INC(ipcomps_crypto);
432 		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
433 		error = ENOBUFS;
434 		goto bad;
435 	}
436 	crdc = crp->crp_desc;
437 
438 	/* Compression descriptor */
439 	crdc->crd_skip = skip;
440 	crdc->crd_len = ralen;
441 	crdc->crd_flags = CRD_F_COMP;
442 	crdc->crd_inject = skip;
443 
444 	/* Compression operation */
445 	crdc->crd_alg = ipcompx->type;
446 
447 	/* IPsec-specific opaque crypto info */
448 	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
449 		M_XDATA, M_NOWAIT|M_ZERO);
450 	if (tc == NULL) {
451 		IPCOMPSTAT_INC(ipcomps_crypto);
452 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
453 		crypto_freereq(crp);
454 		error = ENOBUFS;
455 		goto bad;
456 	}
457 
458 	tc->tc_isr = isr;
459 	KEY_ADDREFSA(sav);
460 	tc->tc_sav = sav;
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_protoff = protoff;
465 	tc->tc_skip = skip;
466 
467 	/* Crypto operation descriptor */
468 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
469 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
470 	crp->crp_buf = (caddr_t) m;
471 	crp->crp_callback = ipcomp_output_cb;
472 	crp->crp_opaque = (caddr_t) 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;
492 	int error, skip;
493 
494 	tc = (struct tdb_crypto *) crp->crp_opaque;
495 	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
496 	m = (struct mbuf *) crp->crp_buf;
497 	skip = tc->tc_skip;
498 
499 	isr = tc->tc_isr;
500 	IPSECREQUEST_LOCK(isr);
501 	sav = tc->tc_sav;
502 	/* With the isr lock released SA pointer can be updated. */
503 	if (sav != isr->sav) {
504 		IPCOMPSTAT_INC(ipcomps_notdb);
505 		DPRINTF(("%s: SA expired while in crypto\n", __func__));
506 		error = ENOBUFS;		/*XXX*/
507 		goto bad;
508 	}
509 
510 	/* Check for crypto errors */
511 	if (crp->crp_etype) {
512 		/* Reset the session ID */
513 		if (sav->tdb_cryptoid != 0)
514 			sav->tdb_cryptoid = crp->crp_sid;
515 
516 		if (crp->crp_etype == EAGAIN) {
517 			IPSECREQUEST_UNLOCK(isr);
518 			return crypto_dispatch(crp);
519 		}
520 		IPCOMPSTAT_INC(ipcomps_noxform);
521 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
522 		error = crp->crp_etype;
523 		goto bad;
524 	}
525 	/* Shouldn't happen... */
526 	if (m == NULL) {
527 		IPCOMPSTAT_INC(ipcomps_crypto);
528 		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
529 		error = EINVAL;
530 		goto bad;
531 	}
532 	IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
533 
534 	if (crp->crp_ilen - skip > crp->crp_olen) {
535 		struct mbuf *mo;
536 		struct ipcomp *ipcomp;
537 		int roff;
538 		uint8_t prot;
539 
540 		/* Compression helped, inject IPCOMP header. */
541 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
542 		if (mo == NULL) {
543 			IPCOMPSTAT_INC(ipcomps_wrap);
544 			DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
545 			    __func__, ipsec_address(&sav->sah->saidx.dst),
546 			    (u_long) ntohl(sav->spi)));
547 			error = ENOBUFS;
548 			goto bad;
549 		}
550 		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
551 
552 		/* Initialize the IPCOMP header */
553 		/* XXX alignment always correct? */
554 		switch (sav->sah->saidx.dst.sa.sa_family) {
555 #ifdef INET
556 		case AF_INET:
557 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
558 			break;
559 #endif /* INET */
560 #ifdef INET6
561 		case AF_INET6:
562 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
563 			break;
564 #endif
565 		}
566 		ipcomp->comp_flags = 0;
567 		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
568 
569 		/* Fix Next Protocol in IPv4/IPv6 header */
570 		prot = IPPROTO_IPCOMP;
571 		m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
572 		    (u_char *)&prot);
573 
574 		/* Adjust the length in the IP header */
575 		switch (sav->sah->saidx.dst.sa.sa_family) {
576 #ifdef INET
577 		case AF_INET:
578 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
579 			break;
580 #endif /* INET */
581 #ifdef INET6
582 		case AF_INET6:
583 			mtod(m, struct ip6_hdr *)->ip6_plen =
584 				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
585 			break;
586 #endif /* INET6 */
587 		default:
588 			IPCOMPSTAT_INC(ipcomps_nopf);
589 			DPRINTF(("%s: unknown/unsupported protocol "
590 			    "family %d, IPCA %s/%08lx\n", __func__,
591 			    sav->sah->saidx.dst.sa.sa_family,
592 			    ipsec_address(&sav->sah->saidx.dst),
593 			    (u_long) ntohl(sav->spi)));
594 			error = EPFNOSUPPORT;
595 			goto bad;
596 		}
597 	} else {
598 		/* Compression was useless, we have lost time. */
599 		IPCOMPSTAT_INC(ipcomps_uncompr);
600 		DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
601 		    __func__, crp->crp_ilen, skip, crp->crp_olen));
602 		/* XXX remember state to not compress the next couple
603 		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
604 	}
605 
606 	/* Release the crypto descriptor */
607 	free(tc, M_XDATA);
608 	crypto_freereq(crp);
609 
610 	/* NB: m is reclaimed by ipsec_process_done. */
611 	error = ipsec_process_done(m, isr);
612 	KEY_FREESAV(&sav);
613 	IPSECREQUEST_UNLOCK(isr);
614 	return error;
615 bad:
616 	if (sav)
617 		KEY_FREESAV(&sav);
618 	IPSECREQUEST_UNLOCK(isr);
619 	if (m)
620 		m_freem(m);
621 	free(tc, M_XDATA);
622 	crypto_freereq(crp);
623 	return error;
624 }
625 
626 static struct xformsw ipcomp_xformsw = {
627 	XF_IPCOMP,		XFT_COMP,		"IPcomp",
628 	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
629 	ipcomp_output
630 };
631 
632 static void
633 ipcomp_attach(void)
634 {
635 
636 	xform_register(&ipcomp_xformsw);
637 }
638 
639 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
640