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