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