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