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