xref: /freebsd/sys/netipsec/xform_ipcomp.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*	$FreeBSD$	*/
2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *   notice, this list of conditions and the following disclaimer in the
17  *   documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *   derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* IP payload compression protocol (IPComp), see RFC 2393 */
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.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 #include <netinet/ip_encap.h>
54 
55 #include <net/netisr.h>
56 #include <net/vnet.h>
57 #include <net/if.h>	/* XXXGL: net_epoch should move out there */
58 #include <net/if_var.h>	/* XXXGL: net_epoch should move out there */
59 
60 #include <netipsec/ipsec.h>
61 #include <netipsec/xform.h>
62 
63 #ifdef INET6
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netipsec/ipsec6.h>
67 #endif
68 
69 #include <netipsec/ipcomp.h>
70 #include <netipsec/ipcomp_var.h>
71 
72 #include <netipsec/key.h>
73 #include <netipsec/key_debug.h>
74 
75 #include <opencrypto/cryptodev.h>
76 #include <opencrypto/deflate.h>
77 #include <opencrypto/xform.h>
78 
79 VNET_DEFINE(int, ipcomp_enable) = 1;
80 VNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
81 VNET_PCPUSTAT_SYSINIT(ipcompstat);
82 
83 #ifdef VIMAGE
84 VNET_PCPUSTAT_SYSUNINIT(ipcompstat);
85 #endif /* VIMAGE */
86 
87 SYSCTL_DECL(_net_inet_ipcomp);
88 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable,
89 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
90 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
91     struct ipcompstat, ipcompstat,
92     "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
93 
94 static MALLOC_DEFINE(M_IPCOMP, "ipcomp", "IPCOMP");
95 
96 static int ipcomp_input_cb(struct cryptop *crp);
97 static int ipcomp_output_cb(struct cryptop *crp);
98 
99 /*
100  * RFC 3173 p 2.2. Non-Expansion Policy:
101  * If the total size of a compressed payload and the IPComp header, as
102  * defined in section 3, is not smaller than the size of the original
103  * payload, the IP datagram MUST be sent in the original non-compressed
104  * form.
105  *
106  * When we use IPComp in tunnel mode, for small packets we will receive
107  * encapsulated IP-IP datagrams without any compression and without IPComp
108  * header.
109  */
110 static int
111 ipcomp_encapcheck(union sockaddr_union *src, union sockaddr_union *dst)
112 {
113 	struct secasvar *sav;
114 
115 	sav = key_allocsa_tunnel(src, dst, IPPROTO_IPCOMP);
116 	if (sav == NULL)
117 		return (0);
118 	key_freesav(&sav);
119 
120 	if (src->sa.sa_family == AF_INET)
121 		return (sizeof(struct in_addr) << 4);
122 	else
123 		return (sizeof(struct in6_addr) << 4);
124 }
125 
126 static int
127 ipcomp_nonexp_input(struct mbuf *m, int off, int proto, void *arg __unused)
128 {
129 	int isr;
130 
131 	NET_EPOCH_ASSERT();
132 
133 	switch (proto) {
134 #ifdef INET
135 	case IPPROTO_IPV4:
136 		isr = NETISR_IP;
137 		break;
138 #endif
139 #ifdef INET6
140 	case IPPROTO_IPV6:
141 		isr = NETISR_IPV6;
142 		break;
143 #endif
144 	default:
145 		IPCOMPSTAT_INC(ipcomps_nopf);
146 		m_freem(m);
147 		return (IPPROTO_DONE);
148 	}
149 	m_adj(m, off);
150 	IPCOMPSTAT_ADD(ipcomps_ibytes, m->m_pkthdr.len);
151 	IPCOMPSTAT_INC(ipcomps_input);
152 	netisr_dispatch(isr, m);
153 	return (IPPROTO_DONE);
154 }
155 
156 /*
157  * ipcomp_init() is called when an CPI is being set up.
158  */
159 static int
160 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
161 {
162 	const struct comp_algo *tcomp;
163 	struct crypto_session_params csp;
164 
165 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
166 	tcomp = comp_algorithm_lookup(sav->alg_enc);
167 	if (tcomp == NULL) {
168 		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
169 			 sav->alg_comp));
170 		return EINVAL;
171 	}
172 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
173 	sav->tdb_xform = xsp;
174 	sav->tdb_compalgxform = tcomp;
175 
176 	/* Initialize crypto session */
177 	memset(&csp, 0, sizeof(csp));
178 	csp.csp_mode = CSP_MODE_COMPRESS;
179 	csp.csp_cipher_alg = sav->tdb_compalgxform->type;
180 
181 	return crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
182 }
183 
184 /*
185  * ipcomp_cleanup() used when IPCA is deleted
186  */
187 static void
188 ipcomp_cleanup(struct secasvar *sav)
189 {
190 
191 	crypto_freesession(sav->tdb_cryptoid);
192 	sav->tdb_cryptoid = NULL;
193 }
194 
195 /*
196  * ipcomp_input() gets called to uncompress an input packet
197  */
198 static int
199 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
200 {
201 	struct xform_data *xd;
202 	struct cryptop *crp;
203 	struct ipcomp *ipcomp;
204 	crypto_session_t cryptoid;
205 	caddr_t addr;
206 	int error, hlen = IPCOMP_HLENGTH;
207 
208 	SECASVAR_RLOCK_TRACKER;
209 
210 	/*
211 	 * Check that the next header of the IPComp is not IPComp again, before
212 	 * doing any real work.  Given it is not possible to do double
213 	 * compression it means someone is playing tricks on us.
214 	 */
215 	error = ENOBUFS;
216 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
217 		IPCOMPSTAT_INC(ipcomps_hdrops);		/*XXX*/
218 		DPRINTF(("%s: m_pullup failed\n", __func__));
219 		key_freesav(&sav);
220 		return (error);
221 	}
222 	addr = (caddr_t) mtod(m, struct ip *) + skip;
223 	ipcomp = (struct ipcomp *)addr;
224 	if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
225 		IPCOMPSTAT_INC(ipcomps_pdrops);	/* XXX have our own stats? */
226 		DPRINTF(("%s: recursive compression detected\n", __func__));
227 		error = EINVAL;
228 		goto bad;
229 	}
230 
231 	SECASVAR_RLOCK(sav);
232 	cryptoid = sav->tdb_cryptoid;
233 	SECASVAR_RUNLOCK(sav);
234 
235 	/* Get crypto descriptors */
236 	crp = crypto_getreq(cryptoid, M_NOWAIT);
237 	if (crp == NULL) {
238 		DPRINTF(("%s: no crypto descriptors\n", __func__));
239 		IPCOMPSTAT_INC(ipcomps_crypto);
240 		goto bad;
241 	}
242 	/* Get IPsec-specific opaque pointer */
243 	xd = malloc(sizeof(*xd), M_IPCOMP, M_NOWAIT | M_ZERO);
244 	if (xd == NULL) {
245 		DPRINTF(("%s: cannot allocate xform_data\n", __func__));
246 		IPCOMPSTAT_INC(ipcomps_crypto);
247 		crypto_freereq(crp);
248 		goto bad;
249 	}
250 
251 	/* Decompression operation */
252 	crp->crp_op = CRYPTO_OP_DECOMPRESS;
253 	crp->crp_payload_start = skip + hlen;
254 	crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen);
255 
256 	/* Crypto operation descriptor */
257 	crp->crp_flags = CRYPTO_F_CBIFSYNC;
258 	crypto_use_mbuf(crp, m);
259 	crp->crp_callback = ipcomp_input_cb;
260 	crp->crp_opaque = xd;
261 
262 	/* These are passed as-is to the callback */
263 	xd->sav = sav;
264 	xd->protoff = protoff;
265 	xd->skip = skip;
266 	xd->vnet = curvnet;
267 	xd->cryptoid = cryptoid;
268 
269 	SECASVAR_RLOCK(sav);
270 	crp->crp_session = xd->cryptoid = sav->tdb_cryptoid;
271 	SECASVAR_RUNLOCK(sav);
272 
273 	return crypto_dispatch(crp);
274 bad:
275 	m_freem(m);
276 	key_freesav(&sav);
277 	return (error);
278 }
279 
280 /*
281  * IPComp input callback from the crypto driver.
282  */
283 static int
284 ipcomp_input_cb(struct cryptop *crp)
285 {
286 	IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
287 	struct xform_data *xd;
288 	struct mbuf *m;
289 	struct secasvar *sav;
290 	struct secasindex *saidx;
291 	caddr_t addr;
292 	crypto_session_t cryptoid;
293 	int hlen = IPCOMP_HLENGTH, error, clen;
294 	int skip, protoff;
295 	uint8_t nproto;
296 
297 	m = crp->crp_buf.cb_mbuf;
298 	xd = crp->crp_opaque;
299 	CURVNET_SET(xd->vnet);
300 	sav = xd->sav;
301 	skip = xd->skip;
302 	protoff = xd->protoff;
303 	cryptoid = xd->cryptoid;
304 	saidx = &sav->sah->saidx;
305 	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
306 		saidx->dst.sa.sa_family == AF_INET6,
307 		("unexpected protocol family %u", saidx->dst.sa.sa_family));
308 
309 	/* Check for crypto errors */
310 	if (crp->crp_etype) {
311 		if (crp->crp_etype == EAGAIN) {
312 			/* Reset the session ID */
313 			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
314 				crypto_freesession(cryptoid);
315 			xd->cryptoid = crp->crp_session;
316 			CURVNET_RESTORE();
317 			return (crypto_dispatch(crp));
318 		}
319 		IPCOMPSTAT_INC(ipcomps_noxform);
320 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
321 		error = crp->crp_etype;
322 		goto bad;
323 	}
324 	/* Shouldn't happen... */
325 	if (m == NULL) {
326 		IPCOMPSTAT_INC(ipcomps_crypto);
327 		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
328 		error = EINVAL;
329 		goto bad;
330 	}
331 	IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
332 
333 	clen = crp->crp_olen;		/* Length of data after processing */
334 
335 	/* Release the crypto descriptors */
336 	free(xd, M_IPCOMP), xd = NULL;
337 	crypto_freereq(crp), crp = NULL;
338 
339 	/* In case it's not done already, adjust the size of the mbuf chain */
340 	m->m_pkthdr.len = clen + hlen + skip;
341 
342 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
343 		IPCOMPSTAT_INC(ipcomps_hdrops);		/*XXX*/
344 		DPRINTF(("%s: m_pullup failed\n", __func__));
345 		error = EINVAL;				/*XXX*/
346 		goto bad;
347 	}
348 
349 	/* Keep the next protocol field */
350 	addr = (caddr_t) mtod(m, struct ip *) + skip;
351 	nproto = ((struct ipcomp *) addr)->comp_nxt;
352 
353 	/* Remove the IPCOMP header */
354 	error = m_striphdr(m, skip, hlen);
355 	if (error) {
356 		IPCOMPSTAT_INC(ipcomps_hdrops);
357 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
358 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
359 		    (u_long) ntohl(sav->spi)));
360 		goto bad;
361 	}
362 
363 	/* Restore the Next Protocol field */
364 	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
365 
366 	switch (saidx->dst.sa.sa_family) {
367 #ifdef INET6
368 	case AF_INET6:
369 		error = ipsec6_common_input_cb(m, sav, skip, protoff);
370 		break;
371 #endif
372 #ifdef INET
373 	case AF_INET:
374 		error = ipsec4_common_input_cb(m, sav, skip, protoff);
375 		break;
376 #endif
377 	default:
378 		panic("%s: Unexpected address family: %d saidx=%p", __func__,
379 		    saidx->dst.sa.sa_family, saidx);
380 	}
381 	CURVNET_RESTORE();
382 	return error;
383 bad:
384 	CURVNET_RESTORE();
385 	if (sav != NULL)
386 		key_freesav(&sav);
387 	if (m != NULL)
388 		m_freem(m);
389 	if (xd != NULL)
390 		free(xd, M_IPCOMP);
391 	if (crp != NULL)
392 		crypto_freereq(crp);
393 	return error;
394 }
395 
396 /*
397  * IPComp output routine, called by ipsec[46]_perform_request()
398  */
399 static int
400 ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
401     u_int idx, int skip, int protoff)
402 {
403 	IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
404 	const struct comp_algo *ipcompx;
405 	struct cryptop *crp;
406 	struct xform_data *xd;
407 	crypto_session_t cryptoid;
408 	int error, ralen, maxpacketsize;
409 
410 	SECASVAR_RLOCK_TRACKER;
411 
412 	IPSEC_ASSERT(sav != NULL, ("null SA"));
413 	ipcompx = sav->tdb_compalgxform;
414 	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
415 
416 	/*
417 	 * Do not touch the packet in case our payload to compress
418 	 * is lower than the minimal threshold of the compression
419 	 * alogrithm.  We will just send out the data uncompressed.
420 	 * See RFC 3173, 2.2. Non-Expansion Policy.
421 	 */
422 	if (m->m_pkthdr.len <= ipcompx->minlen) {
423 		IPCOMPSTAT_INC(ipcomps_threshold);
424 		return ipsec_process_done(m, sp, sav, idx);
425 	}
426 
427 	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
428 	IPCOMPSTAT_INC(ipcomps_output);
429 
430 	/* Check for maximum packet size violations. */
431 	switch (sav->sah->saidx.dst.sa.sa_family) {
432 #ifdef INET
433 	case AF_INET:
434 		maxpacketsize = IP_MAXPACKET;
435 		break;
436 #endif /* INET */
437 #ifdef INET6
438 	case AF_INET6:
439 		maxpacketsize = IPV6_MAXPACKET;
440 		break;
441 #endif /* INET6 */
442 	default:
443 		IPCOMPSTAT_INC(ipcomps_nopf);
444 		DPRINTF(("%s: unknown/unsupported protocol family %d, "
445 		    "IPCA %s/%08lx\n", __func__,
446 		    sav->sah->saidx.dst.sa.sa_family,
447 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
448 		    (u_long) ntohl(sav->spi)));
449 		error = EPFNOSUPPORT;
450 		goto bad;
451 	}
452 	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
453 		IPCOMPSTAT_INC(ipcomps_toobig);
454 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
455 		    "(len %u, max len %u)\n", __func__,
456 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
457 		    (u_long) ntohl(sav->spi),
458 		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
459 		error = EMSGSIZE;
460 		goto bad;
461 	}
462 
463 	/* Update the counters */
464 	IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
465 
466 	m = m_unshare(m, M_NOWAIT);
467 	if (m == NULL) {
468 		IPCOMPSTAT_INC(ipcomps_hdrops);
469 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
470 		    __func__, ipsec_address(&sav->sah->saidx.dst, buf,
471 		    sizeof(buf)), (u_long) ntohl(sav->spi)));
472 		error = ENOBUFS;
473 		goto bad;
474 	}
475 
476 	/* Ok now, we can pass to the crypto processing. */
477 	SECASVAR_RLOCK(sav);
478 	cryptoid = sav->tdb_cryptoid;
479 	SECASVAR_RUNLOCK(sav);
480 
481 	/* Get crypto descriptors */
482 	crp = crypto_getreq(cryptoid, M_NOWAIT);
483 	if (crp == NULL) {
484 		IPCOMPSTAT_INC(ipcomps_crypto);
485 		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
486 		error = ENOBUFS;
487 		goto bad;
488 	}
489 
490 	/* Compression descriptor */
491 	crp->crp_op = CRYPTO_OP_COMPRESS;
492 	crp->crp_payload_start = skip;
493 	crp->crp_payload_length = ralen;
494 
495 	/* IPsec-specific opaque crypto info */
496 	xd =  malloc(sizeof(struct xform_data), M_IPCOMP, M_NOWAIT | M_ZERO);
497 	if (xd == NULL) {
498 		IPCOMPSTAT_INC(ipcomps_crypto);
499 		DPRINTF(("%s: failed to allocate xform_data\n", __func__));
500 		crypto_freereq(crp);
501 		error = ENOBUFS;
502 		goto bad;
503 	}
504 
505 	xd->sp = sp;
506 	xd->sav = sav;
507 	xd->idx = idx;
508 	xd->skip = skip;
509 	xd->protoff = protoff;
510 	xd->vnet = curvnet;
511 	xd->cryptoid = cryptoid;
512 
513 	/* Crypto operation descriptor */
514 	crp->crp_flags = CRYPTO_F_CBIFSYNC;
515 	crypto_use_mbuf(crp, m);
516 	crp->crp_callback = ipcomp_output_cb;
517 	crp->crp_opaque = xd;
518 
519 	return crypto_dispatch(crp);
520 bad:
521 	if (m)
522 		m_freem(m);
523 	key_freesav(&sav);
524 	key_freesp(&sp);
525 	return (error);
526 }
527 
528 /*
529  * IPComp output callback from the crypto driver.
530  */
531 static int
532 ipcomp_output_cb(struct cryptop *crp)
533 {
534 	IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
535 	struct xform_data *xd;
536 	struct secpolicy *sp;
537 	struct secasvar *sav;
538 	struct mbuf *m;
539 	crypto_session_t cryptoid;
540 	u_int idx;
541 	int error, skip, protoff;
542 
543 	m = crp->crp_buf.cb_mbuf;
544 	xd = crp->crp_opaque;
545 	CURVNET_SET(xd->vnet);
546 	idx = xd->idx;
547 	sp = xd->sp;
548 	sav = xd->sav;
549 	skip = xd->skip;
550 	protoff = xd->protoff;
551 	cryptoid = xd->cryptoid;
552 
553 	/* Check for crypto errors */
554 	if (crp->crp_etype) {
555 		if (crp->crp_etype == EAGAIN) {
556 			/* Reset the session ID */
557 			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
558 				crypto_freesession(cryptoid);
559 			xd->cryptoid = crp->crp_session;
560 			CURVNET_RESTORE();
561 			return (crypto_dispatch(crp));
562 		}
563 		IPCOMPSTAT_INC(ipcomps_noxform);
564 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
565 		error = crp->crp_etype;
566 		goto bad;
567 	}
568 	/* Shouldn't happen... */
569 	if (m == NULL) {
570 		IPCOMPSTAT_INC(ipcomps_crypto);
571 		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
572 		error = EINVAL;
573 		goto bad;
574 	}
575 	IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
576 
577 	if (crp->crp_payload_length > crp->crp_olen) {
578 		struct mbuf *mo;
579 		struct ipcomp *ipcomp;
580 		int roff;
581 		uint8_t prot;
582 
583 		/* Compression helped, inject IPCOMP header. */
584 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
585 		if (mo == NULL) {
586 			IPCOMPSTAT_INC(ipcomps_wrap);
587 			DPRINTF(("%s: IPCOMP header inject failed "
588 			    "for IPCA %s/%08lx\n",
589 			    __func__, ipsec_address(&sav->sah->saidx.dst, buf,
590 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
591 			error = ENOBUFS;
592 			goto bad;
593 		}
594 		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
595 
596 		/* Initialize the IPCOMP header */
597 		/* XXX alignment always correct? */
598 		switch (sav->sah->saidx.dst.sa.sa_family) {
599 #ifdef INET
600 		case AF_INET:
601 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
602 			break;
603 #endif /* INET */
604 #ifdef INET6
605 		case AF_INET6:
606 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
607 			break;
608 #endif
609 		}
610 		ipcomp->comp_flags = 0;
611 		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
612 
613 		/* Fix Next Protocol in IPv4/IPv6 header */
614 		prot = IPPROTO_IPCOMP;
615 		m_copyback(m, protoff, sizeof(u_int8_t),
616 		    (u_char *)&prot);
617 
618 		/* Adjust the length in the IP header */
619 		switch (sav->sah->saidx.dst.sa.sa_family) {
620 #ifdef INET
621 		case AF_INET:
622 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
623 			break;
624 #endif /* INET */
625 #ifdef INET6
626 		case AF_INET6:
627 			mtod(m, struct ip6_hdr *)->ip6_plen =
628 				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
629 			break;
630 #endif /* INET6 */
631 		default:
632 			IPCOMPSTAT_INC(ipcomps_nopf);
633 			DPRINTF(("%s: unknown/unsupported protocol "
634 			    "family %d, IPCA %s/%08lx\n", __func__,
635 			    sav->sah->saidx.dst.sa.sa_family,
636 			    ipsec_address(&sav->sah->saidx.dst, buf,
637 				sizeof(buf)), (u_long) ntohl(sav->spi)));
638 			error = EPFNOSUPPORT;
639 			goto bad;
640 		}
641 	} else {
642 		/* Compression was useless, we have lost time. */
643 		IPCOMPSTAT_INC(ipcomps_uncompr);
644 		DPRINTF(("%s: compressions was useless %d <= %d\n",
645 		    __func__, crp->crp_payload_length, crp->crp_olen));
646 		/* XXX remember state to not compress the next couple
647 		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
648 	}
649 
650 	/* Release the crypto descriptor */
651 	free(xd, M_IPCOMP);
652 	crypto_freereq(crp);
653 
654 	/* NB: m is reclaimed by ipsec_process_done. */
655 	error = ipsec_process_done(m, sp, sav, idx);
656 	CURVNET_RESTORE();
657 	return (error);
658 bad:
659 	if (m)
660 		m_freem(m);
661 	CURVNET_RESTORE();
662 	free(xd, M_IPCOMP);
663 	crypto_freereq(crp);
664 	key_freesav(&sav);
665 	key_freesp(&sp);
666 	return (error);
667 }
668 
669 #ifdef INET
670 static int
671 ipcomp4_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
672     void *arg __unused)
673 {
674 	union sockaddr_union src, dst;
675 	const struct ip *ip;
676 
677 	if (V_ipcomp_enable == 0)
678 		return (0);
679 	if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
680 		return (0);
681 	bzero(&src, sizeof(src));
682 	bzero(&dst, sizeof(dst));
683 	src.sa.sa_family = dst.sa.sa_family = AF_INET;
684 	src.sin.sin_len = dst.sin.sin_len = sizeof(struct sockaddr_in);
685 	ip = mtod(m, const struct ip *);
686 	src.sin.sin_addr = ip->ip_src;
687 	dst.sin.sin_addr = ip->ip_dst;
688 	return (ipcomp_encapcheck(&src, &dst));
689 }
690 
691 static const struct encaptab *ipe4_cookie = NULL;
692 static const struct encap_config ipv4_encap_cfg = {
693 	.proto = -1,
694 	.min_length = sizeof(struct ip),
695 	.exact_match = sizeof(in_addr_t) << 4,
696 	.check = ipcomp4_nonexp_encapcheck,
697 	.input = ipcomp_nonexp_input
698 };
699 #endif
700 #ifdef INET6
701 static int
702 ipcomp6_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
703     void *arg __unused)
704 {
705 	union sockaddr_union src, dst;
706 	const struct ip6_hdr *ip6;
707 
708 	if (V_ipcomp_enable == 0)
709 		return (0);
710 	if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
711 		return (0);
712 	bzero(&src, sizeof(src));
713 	bzero(&dst, sizeof(dst));
714 	src.sa.sa_family = dst.sa.sa_family = AF_INET;
715 	src.sin6.sin6_len = dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
716 	ip6 = mtod(m, const struct ip6_hdr *);
717 	src.sin6.sin6_addr = ip6->ip6_src;
718 	dst.sin6.sin6_addr = ip6->ip6_dst;
719 	if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6.sin6_addr)) {
720 		/* XXX: sa6_recoverscope() */
721 		src.sin6.sin6_scope_id =
722 		    ntohs(src.sin6.sin6_addr.s6_addr16[1]);
723 		src.sin6.sin6_addr.s6_addr16[1] = 0;
724 	}
725 	if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6.sin6_addr)) {
726 		/* XXX: sa6_recoverscope() */
727 		dst.sin6.sin6_scope_id =
728 		    ntohs(dst.sin6.sin6_addr.s6_addr16[1]);
729 		dst.sin6.sin6_addr.s6_addr16[1] = 0;
730 	}
731 	return (ipcomp_encapcheck(&src, &dst));
732 }
733 
734 static const struct encaptab *ipe6_cookie = NULL;
735 static const struct encap_config ipv6_encap_cfg = {
736 	.proto = -1,
737 	.min_length = sizeof(struct ip6_hdr),
738 	.exact_match = sizeof(struct in6_addr) << 4,
739 	.check = ipcomp6_nonexp_encapcheck,
740 	.input = ipcomp_nonexp_input
741 };
742 #endif
743 
744 static struct xformsw ipcomp_xformsw = {
745 	.xf_type =	XF_IPCOMP,
746 	.xf_name =	"IPcomp",
747 	.xf_init =	ipcomp_init,
748 	.xf_cleanup =	ipcomp_cleanup,
749 	.xf_input =	ipcomp_input,
750 	.xf_output =	ipcomp_output,
751 };
752 
753 static void
754 ipcomp_attach(void)
755 {
756 
757 #ifdef INET
758 	ipe4_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
759 #endif
760 #ifdef INET6
761 	ipe6_cookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
762 #endif
763 	xform_attach(&ipcomp_xformsw);
764 }
765 
766 static void
767 ipcomp_detach(void)
768 {
769 
770 #ifdef INET
771 	ip_encap_detach(ipe4_cookie);
772 #endif
773 #ifdef INET6
774 	ip6_encap_detach(ipe6_cookie);
775 #endif
776 	xform_detach(&ipcomp_xformsw);
777 }
778 
779 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
780     ipcomp_attach, NULL);
781 SYSUNINIT(ipcomp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
782     ipcomp_detach, NULL);
783