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