xform_ipcomp.c revision 220206
1105197Ssam/*	$FreeBSD: head/sys/netipsec/xform_ipcomp.c 220206 2011-03-31 15:23:32Z fabient $	*/
2105197Ssam/* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3105197Ssam
4139823Simp/*-
5105197Ssam * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6105197Ssam *
7105197Ssam * Redistribution and use in source and binary forms, with or without
8105197Ssam * modification, are permitted provided that the following conditions
9105197Ssam * are met:
10105197Ssam *
11105197Ssam * 1. Redistributions of source code must retain the above copyright
12105197Ssam *   notice, this list of conditions and the following disclaimer.
13105197Ssam * 2. Redistributions in binary form must reproduce the above copyright
14105197Ssam *   notice, this list of conditions and the following disclaimer in the
15105197Ssam *   documentation and/or other materials provided with the distribution.
16105197Ssam * 3. The name of the author may not be used to endorse or promote products
17105197Ssam *   derived from this software without specific prior written permission.
18105197Ssam *
19105197Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20105197Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21105197Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22105197Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23105197Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24105197Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25105197Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26105197Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27105197Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28105197Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29105197Ssam */
30105197Ssam
31105197Ssam/* IP payload compression protocol (IPComp), see RFC 2393 */
32105197Ssam#include "opt_inet.h"
33105197Ssam#include "opt_inet6.h"
34105197Ssam
35105197Ssam#include <sys/param.h>
36105197Ssam#include <sys/systm.h>
37105197Ssam#include <sys/mbuf.h>
38119643Ssam#include <sys/lock.h>
39119643Ssam#include <sys/mutex.h>
40220206Sfabient#include <sys/rwlock.h>
41105197Ssam#include <sys/socket.h>
42105197Ssam#include <sys/kernel.h>
43105197Ssam#include <sys/protosw.h>
44105197Ssam#include <sys/sysctl.h>
45105197Ssam
46105197Ssam#include <netinet/in.h>
47105197Ssam#include <netinet/in_systm.h>
48105197Ssam#include <netinet/ip.h>
49105197Ssam#include <netinet/ip_var.h>
50105197Ssam
51105197Ssam#include <net/route.h>
52195699Srwatson#include <net/vnet.h>
53195699Srwatson
54105197Ssam#include <netipsec/ipsec.h>
55105197Ssam#include <netipsec/xform.h>
56105197Ssam
57105197Ssam#ifdef INET6
58105197Ssam#include <netinet/ip6.h>
59105197Ssam#include <netipsec/ipsec6.h>
60105197Ssam#endif
61105197Ssam
62105197Ssam#include <netipsec/ipcomp.h>
63105197Ssam#include <netipsec/ipcomp_var.h>
64105197Ssam
65105197Ssam#include <netipsec/key.h>
66105197Ssam#include <netipsec/key_debug.h>
67105197Ssam
68105197Ssam#include <opencrypto/cryptodev.h>
69105197Ssam#include <opencrypto/deflate.h>
70105197Ssam#include <opencrypto/xform.h>
71105197Ssam
72199947SbzVNET_DEFINE(int, ipcomp_enable) = 1;
73195699SrwatsonVNET_DEFINE(struct ipcompstat, ipcompstat);
74105197Ssam
75105197SsamSYSCTL_DECL(_net_inet_ipcomp);
76195699SrwatsonSYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
77195699Srwatson	ipcomp_enable,	CTLFLAG_RW,	&VNET_NAME(ipcomp_enable),	0, "");
78195699SrwatsonSYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
79195699Srwatson	stats,		CTLFLAG_RD,	&VNET_NAME(ipcompstat),	ipcompstat, "");
80105197Ssam
81105197Ssamstatic int ipcomp_input_cb(struct cryptop *crp);
82105197Ssamstatic int ipcomp_output_cb(struct cryptop *crp);
83105197Ssam
84105197Ssamstruct comp_algo *
85105197Ssamipcomp_algorithm_lookup(int alg)
86105197Ssam{
87105197Ssam	if (alg >= IPCOMP_ALG_MAX)
88105197Ssam		return NULL;
89105197Ssam	switch (alg) {
90105197Ssam	case SADB_X_CALG_DEFLATE:
91105197Ssam		return &comp_algo_deflate;
92105197Ssam	}
93105197Ssam	return NULL;
94105197Ssam}
95105197Ssam
96105197Ssam/*
97105197Ssam * ipcomp_init() is called when an CPI is being set up.
98105197Ssam */
99105197Ssamstatic int
100105197Ssamipcomp_init(struct secasvar *sav, struct xformsw *xsp)
101105197Ssam{
102105197Ssam	struct comp_algo *tcomp;
103105197Ssam	struct cryptoini cric;
104105197Ssam
105105197Ssam	/* NB: algorithm really comes in alg_enc and not alg_comp! */
106105197Ssam	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
107105197Ssam	if (tcomp == NULL) {
108120585Ssam		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
109105197Ssam			 sav->alg_comp));
110105197Ssam		return EINVAL;
111105197Ssam	}
112105197Ssam	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
113105197Ssam	sav->tdb_xform = xsp;
114105197Ssam	sav->tdb_compalgxform = tcomp;
115105197Ssam
116105197Ssam	/* Initialize crypto session */
117105197Ssam	bzero(&cric, sizeof (cric));
118105197Ssam	cric.cri_alg = sav->tdb_compalgxform->type;
119105197Ssam
120181803Sbz	return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
121105197Ssam}
122105197Ssam
123105197Ssam/*
124105197Ssam * ipcomp_zeroize() used when IPCA is deleted
125105197Ssam */
126105197Ssamstatic int
127105197Ssamipcomp_zeroize(struct secasvar *sav)
128105197Ssam{
129105197Ssam	int err;
130105197Ssam
131105197Ssam	err = crypto_freesession(sav->tdb_cryptoid);
132105197Ssam	sav->tdb_cryptoid = 0;
133105197Ssam	return err;
134105197Ssam}
135105197Ssam
136105197Ssam/*
137105197Ssam * ipcomp_input() gets called to uncompress an input packet
138105197Ssam */
139105197Ssamstatic int
140105197Ssamipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
141105197Ssam{
142105197Ssam	struct tdb_crypto *tc;
143105197Ssam	struct cryptodesc *crdc;
144105197Ssam	struct cryptop *crp;
145105197Ssam	int hlen = IPCOMP_HLENGTH;
146105197Ssam
147105197Ssam	/* Get crypto descriptors */
148105197Ssam	crp = crypto_getreq(1);
149105197Ssam	if (crp == NULL) {
150105197Ssam		m_freem(m);
151120585Ssam		DPRINTF(("%s: no crypto descriptors\n", __func__));
152181803Sbz		V_ipcompstat.ipcomps_crypto++;
153105197Ssam		return ENOBUFS;
154105197Ssam	}
155105197Ssam	/* Get IPsec-specific opaque pointer */
156105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
157105197Ssam	if (tc == NULL) {
158105197Ssam		m_freem(m);
159105197Ssam		crypto_freereq(crp);
160120585Ssam		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
161181803Sbz		V_ipcompstat.ipcomps_crypto++;
162105197Ssam		return ENOBUFS;
163105197Ssam	}
164105197Ssam	crdc = crp->crp_desc;
165105197Ssam
166105197Ssam	crdc->crd_skip = skip + hlen;
167105197Ssam	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
168105197Ssam	crdc->crd_inject = skip;
169105197Ssam
170105197Ssam	tc->tc_ptr = 0;
171105197Ssam
172105197Ssam	/* Decompression operation */
173105197Ssam	crdc->crd_alg = sav->tdb_compalgxform->type;
174105197Ssam
175105197Ssam	/* Crypto operation descriptor */
176105197Ssam	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
177117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
178105197Ssam	crp->crp_buf = (caddr_t) m;
179105197Ssam	crp->crp_callback = ipcomp_input_cb;
180105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
181105197Ssam	crp->crp_opaque = (caddr_t) tc;
182105197Ssam
183105197Ssam	/* These are passed as-is to the callback */
184105197Ssam	tc->tc_spi = sav->spi;
185105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
186105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
187105197Ssam	tc->tc_protoff = protoff;
188105197Ssam	tc->tc_skip = skip;
189220206Sfabient	KEY_ADDREFSA(sav);
190220206Sfabient	tc->tc_sav = sav;
191105197Ssam
192105197Ssam	return crypto_dispatch(crp);
193105197Ssam}
194105197Ssam
195105197Ssam#ifdef INET6
196105197Ssam#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
197105197Ssam	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
198105197Ssam		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
199105197Ssam	} else {							     \
200105197Ssam		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
201105197Ssam	}								     \
202105197Ssam} while (0)
203105197Ssam#else
204105197Ssam#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
205105197Ssam	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
206105197Ssam#endif
207105197Ssam
208105197Ssam/*
209105197Ssam * IPComp input callback from the crypto driver.
210105197Ssam */
211105197Ssamstatic int
212105197Ssamipcomp_input_cb(struct cryptop *crp)
213105197Ssam{
214105197Ssam	struct cryptodesc *crd;
215105197Ssam	struct tdb_crypto *tc;
216105197Ssam	int skip, protoff;
217105197Ssam	struct mtag *mtag;
218105197Ssam	struct mbuf *m;
219105197Ssam	struct secasvar *sav;
220105197Ssam	struct secasindex *saidx;
221119643Ssam	int hlen = IPCOMP_HLENGTH, error, clen;
222105197Ssam	u_int8_t nproto;
223105197Ssam	caddr_t addr;
224105197Ssam
225105197Ssam	crd = crp->crp_desc;
226105197Ssam
227105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
228120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
229105197Ssam	skip = tc->tc_skip;
230105197Ssam	protoff = tc->tc_protoff;
231105197Ssam	mtag = (struct mtag *) tc->tc_ptr;
232105197Ssam	m = (struct mbuf *) crp->crp_buf;
233105197Ssam
234220206Sfabient	sav = tc->tc_sav;
235220206Sfabient	IPSEC_ASSERT(sav != NULL, ("null SA!"));
236105197Ssam
237105197Ssam	saidx = &sav->sah->saidx;
238120585Ssam	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
239105197Ssam		saidx->dst.sa.sa_family == AF_INET6,
240120585Ssam		("unexpected protocol family %u", saidx->dst.sa.sa_family));
241105197Ssam
242105197Ssam	/* Check for crypto errors */
243105197Ssam	if (crp->crp_etype) {
244105197Ssam		/* Reset the session ID */
245105197Ssam		if (sav->tdb_cryptoid != 0)
246105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
247105197Ssam
248105197Ssam		if (crp->crp_etype == EAGAIN) {
249199905Sbz			return crypto_dispatch(crp);
250105197Ssam		}
251181803Sbz		V_ipcompstat.ipcomps_noxform++;
252120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
253105197Ssam		error = crp->crp_etype;
254105197Ssam		goto bad;
255105197Ssam	}
256105197Ssam	/* Shouldn't happen... */
257105197Ssam	if (m == NULL) {
258181803Sbz		V_ipcompstat.ipcomps_crypto++;
259120585Ssam		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
260105197Ssam		error = EINVAL;
261105197Ssam		goto bad;
262105197Ssam	}
263181803Sbz	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
264105197Ssam
265105197Ssam	clen = crp->crp_olen;		/* Length of data after processing */
266105197Ssam
267105197Ssam	/* Release the crypto descriptors */
268105197Ssam	free(tc, M_XDATA), tc = NULL;
269105197Ssam	crypto_freereq(crp), crp = NULL;
270105197Ssam
271105197Ssam	/* In case it's not done already, adjust the size of the mbuf chain */
272105197Ssam	m->m_pkthdr.len = clen + hlen + skip;
273105197Ssam
274105197Ssam	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
275181803Sbz		V_ipcompstat.ipcomps_hdrops++;		/*XXX*/
276120585Ssam		DPRINTF(("%s: m_pullup failed\n", __func__));
277105197Ssam		error = EINVAL;				/*XXX*/
278105197Ssam		goto bad;
279105197Ssam	}
280105197Ssam
281105197Ssam	/* Keep the next protocol field */
282105197Ssam	addr = (caddr_t) mtod(m, struct ip *) + skip;
283105197Ssam	nproto = ((struct ipcomp *) addr)->comp_nxt;
284105197Ssam
285105197Ssam	/* Remove the IPCOMP header */
286105197Ssam	error = m_striphdr(m, skip, hlen);
287105197Ssam	if (error) {
288181803Sbz		V_ipcompstat.ipcomps_hdrops++;
289120585Ssam		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
290105197Ssam			 ipsec_address(&sav->sah->saidx.dst),
291105197Ssam			 (u_long) ntohl(sav->spi)));
292105197Ssam		goto bad;
293105197Ssam	}
294105197Ssam
295105197Ssam	/* Restore the Next Protocol field */
296105197Ssam	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
297105197Ssam
298105197Ssam	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
299105197Ssam
300105197Ssam	KEY_FREESAV(&sav);
301105197Ssam	return error;
302105197Ssambad:
303105197Ssam	if (sav)
304105197Ssam		KEY_FREESAV(&sav);
305105197Ssam	if (m)
306105197Ssam		m_freem(m);
307105197Ssam	if (tc != NULL)
308105197Ssam		free(tc, M_XDATA);
309105197Ssam	if (crp)
310105197Ssam		crypto_freereq(crp);
311105197Ssam	return error;
312105197Ssam}
313105197Ssam
314105197Ssam/*
315105197Ssam * IPComp output routine, called by ipsec[46]_process_packet()
316105197Ssam */
317105197Ssamstatic int
318105197Ssamipcomp_output(
319105197Ssam	struct mbuf *m,
320105197Ssam	struct ipsecrequest *isr,
321105197Ssam	struct mbuf **mp,
322105197Ssam	int skip,
323105197Ssam	int protoff
324105197Ssam)
325105197Ssam{
326105197Ssam	struct secasvar *sav;
327105197Ssam	struct comp_algo *ipcompx;
328199899Sbz	int error, ralen, maxpacketsize;
329105197Ssam	struct cryptodesc *crdc;
330105197Ssam	struct cryptop *crp;
331105197Ssam	struct tdb_crypto *tc;
332105197Ssam
333105197Ssam	sav = isr->sav;
334120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
335105197Ssam	ipcompx = sav->tdb_compalgxform;
336120585Ssam	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
337105197Ssam
338199896Sbz	/*
339199896Sbz	 * Do not touch the packet in case our payload to compress
340199896Sbz	 * is lower than the minimal threshold of the compression
341199896Sbz	 * alogrithm.  We will just send out the data uncompressed.
342199896Sbz	 * See RFC 3173, 2.2. Non-Expansion Policy.
343199896Sbz	 */
344199896Sbz	if (m->m_pkthdr.len <= ipcompx->minlen) {
345199946Sbz		V_ipcompstat.ipcomps_threshold++;
346199896Sbz		return ipsec_process_done(m, isr);
347199896Sbz	}
348199896Sbz
349105197Ssam	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
350181803Sbz	V_ipcompstat.ipcomps_output++;
351105197Ssam
352105197Ssam	/* Check for maximum packet size violations. */
353105197Ssam	switch (sav->sah->saidx.dst.sa.sa_family) {
354105197Ssam#ifdef INET
355105197Ssam	case AF_INET:
356199897Sbz		maxpacketsize = IP_MAXPACKET;
357105197Ssam		break;
358105197Ssam#endif /* INET */
359105197Ssam#ifdef INET6
360105197Ssam	case AF_INET6:
361199897Sbz		maxpacketsize = IPV6_MAXPACKET;
362105197Ssam		break;
363105197Ssam#endif /* INET6 */
364105197Ssam	default:
365181803Sbz		V_ipcompstat.ipcomps_nopf++;
366120585Ssam		DPRINTF(("%s: unknown/unsupported protocol family %d, "
367120585Ssam		    "IPCA %s/%08lx\n", __func__,
368105197Ssam		    sav->sah->saidx.dst.sa.sa_family,
369105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
370105197Ssam		    (u_long) ntohl(sav->spi)));
371105197Ssam		error = EPFNOSUPPORT;
372105197Ssam		goto bad;
373105197Ssam	}
374199899Sbz	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
375181803Sbz		V_ipcompstat.ipcomps_toobig++;
376120585Ssam		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
377120585Ssam		    "(len %u, max len %u)\n", __func__,
378105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
379105197Ssam		    (u_long) ntohl(sav->spi),
380199899Sbz		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
381105197Ssam		error = EMSGSIZE;
382105197Ssam		goto bad;
383105197Ssam	}
384105197Ssam
385105197Ssam	/* Update the counters */
386181803Sbz	V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
387105197Ssam
388156756Ssam	m = m_unshare(m, M_NOWAIT);
389105197Ssam	if (m == NULL) {
390181803Sbz		V_ipcompstat.ipcomps_hdrops++;
391120585Ssam		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
392120585Ssam		    __func__, ipsec_address(&sav->sah->saidx.dst),
393105197Ssam		    (u_long) ntohl(sav->spi)));
394105197Ssam		error = ENOBUFS;
395105197Ssam		goto bad;
396105197Ssam	}
397105197Ssam
398199899Sbz	/* Ok now, we can pass to the crypto processing. */
399105197Ssam
400105197Ssam	/* Get crypto descriptors */
401105197Ssam	crp = crypto_getreq(1);
402105197Ssam	if (crp == NULL) {
403181803Sbz		V_ipcompstat.ipcomps_crypto++;
404120585Ssam		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
405105197Ssam		error = ENOBUFS;
406105197Ssam		goto bad;
407105197Ssam	}
408105197Ssam	crdc = crp->crp_desc;
409105197Ssam
410105197Ssam	/* Compression descriptor */
411199899Sbz	crdc->crd_skip = skip;
412199899Sbz	crdc->crd_len = ralen;
413105197Ssam	crdc->crd_flags = CRD_F_COMP;
414199899Sbz	crdc->crd_inject = skip;
415105197Ssam
416105197Ssam	/* Compression operation */
417105197Ssam	crdc->crd_alg = ipcompx->type;
418105197Ssam
419105197Ssam	/* IPsec-specific opaque crypto info */
420105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
421105197Ssam		M_XDATA, M_NOWAIT|M_ZERO);
422105197Ssam	if (tc == NULL) {
423181803Sbz		V_ipcompstat.ipcomps_crypto++;
424120585Ssam		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
425105197Ssam		crypto_freereq(crp);
426105197Ssam		error = ENOBUFS;
427105197Ssam		goto bad;
428105197Ssam	}
429105197Ssam
430105197Ssam	tc->tc_isr = isr;
431220206Sfabient	KEY_ADDREFSA(sav);
432220206Sfabient	tc->tc_sav = sav;
433105197Ssam	tc->tc_spi = sav->spi;
434105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
435105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
436199899Sbz	tc->tc_protoff = protoff;
437199899Sbz	tc->tc_skip = skip;
438105197Ssam
439105197Ssam	/* Crypto operation descriptor */
440105197Ssam	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
441117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
442105197Ssam	crp->crp_buf = (caddr_t) m;
443105197Ssam	crp->crp_callback = ipcomp_output_cb;
444105197Ssam	crp->crp_opaque = (caddr_t) tc;
445105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
446105197Ssam
447105197Ssam	return crypto_dispatch(crp);
448105197Ssambad:
449105197Ssam	if (m)
450105197Ssam		m_freem(m);
451105197Ssam	return (error);
452105197Ssam}
453105197Ssam
454105197Ssam/*
455105197Ssam * IPComp output callback from the crypto driver.
456105197Ssam */
457105197Ssamstatic int
458105197Ssamipcomp_output_cb(struct cryptop *crp)
459105197Ssam{
460105197Ssam	struct tdb_crypto *tc;
461105197Ssam	struct ipsecrequest *isr;
462105197Ssam	struct secasvar *sav;
463105197Ssam	struct mbuf *m;
464199899Sbz	int error, skip;
465105197Ssam
466105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
467120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
468105197Ssam	m = (struct mbuf *) crp->crp_buf;
469105197Ssam	skip = tc->tc_skip;
470105197Ssam
471105197Ssam	isr = tc->tc_isr;
472120585Ssam	IPSECREQUEST_LOCK(isr);
473220206Sfabient	sav = tc->tc_sav;
474220206Sfabient	/* With the isr lock released SA pointer can be updated. */
475220206Sfabient	if (sav != isr->sav) {
476181803Sbz		V_ipcompstat.ipcomps_notdb++;
477120585Ssam		DPRINTF(("%s: SA expired while in crypto\n", __func__));
478105197Ssam		error = ENOBUFS;		/*XXX*/
479105197Ssam		goto bad;
480105197Ssam	}
481105197Ssam
482105197Ssam	/* Check for crypto errors */
483105197Ssam	if (crp->crp_etype) {
484199905Sbz		/* Reset the session ID */
485105197Ssam		if (sav->tdb_cryptoid != 0)
486105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
487105197Ssam
488105197Ssam		if (crp->crp_etype == EAGAIN) {
489120585Ssam			IPSECREQUEST_UNLOCK(isr);
490199899Sbz			return crypto_dispatch(crp);
491105197Ssam		}
492181803Sbz		V_ipcompstat.ipcomps_noxform++;
493120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
494105197Ssam		error = crp->crp_etype;
495105197Ssam		goto bad;
496105197Ssam	}
497105197Ssam	/* Shouldn't happen... */
498105197Ssam	if (m == NULL) {
499181803Sbz		V_ipcompstat.ipcomps_crypto++;
500120585Ssam		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
501105197Ssam		error = EINVAL;
502105197Ssam		goto bad;
503105197Ssam	}
504181803Sbz	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
505105197Ssam
506199899Sbz	if (crp->crp_ilen - skip > crp->crp_olen) {
507199899Sbz		struct mbuf *mo;
508199899Sbz		struct ipcomp *ipcomp;
509199899Sbz		int roff;
510199899Sbz		uint8_t prot;
511199899Sbz
512199899Sbz		/* Compression helped, inject IPCOMP header. */
513199899Sbz		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
514199899Sbz		if (mo == NULL) {
515199899Sbz			V_ipcompstat.ipcomps_wrap++;
516199899Sbz			DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
517199899Sbz			    __func__, ipsec_address(&sav->sah->saidx.dst),
518199899Sbz			    (u_long) ntohl(sav->spi)));
519199899Sbz			error = ENOBUFS;
520199899Sbz			goto bad;
521199899Sbz		}
522199899Sbz		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
523199899Sbz
524199899Sbz		/* Initialize the IPCOMP header */
525199899Sbz		/* XXX alignment always correct? */
526199899Sbz		switch (sav->sah->saidx.dst.sa.sa_family) {
527199899Sbz#ifdef INET
528199899Sbz		case AF_INET:
529199899Sbz			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
530199899Sbz			break;
531199899Sbz#endif /* INET */
532199899Sbz#ifdef INET6
533199899Sbz		case AF_INET6:
534199899Sbz			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
535199899Sbz			break;
536199899Sbz#endif
537199899Sbz		}
538199899Sbz		ipcomp->comp_flags = 0;
539199899Sbz		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
540199899Sbz
541199899Sbz		/* Fix Next Protocol in IPv4/IPv6 header */
542199899Sbz		prot = IPPROTO_IPCOMP;
543199899Sbz		m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
544199899Sbz		    (u_char *)&prot);
545199899Sbz
546105197Ssam		/* Adjust the length in the IP header */
547105197Ssam		switch (sav->sah->saidx.dst.sa.sa_family) {
548105197Ssam#ifdef INET
549105197Ssam		case AF_INET:
550105197Ssam			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
551105197Ssam			break;
552105197Ssam#endif /* INET */
553105197Ssam#ifdef INET6
554105197Ssam		case AF_INET6:
555105197Ssam			mtod(m, struct ip6_hdr *)->ip6_plen =
556105197Ssam				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
557105197Ssam			break;
558105197Ssam#endif /* INET6 */
559105197Ssam		default:
560181803Sbz			V_ipcompstat.ipcomps_nopf++;
561120585Ssam			DPRINTF(("%s: unknown/unsupported protocol "
562120585Ssam			    "family %d, IPCA %s/%08lx\n", __func__,
563105197Ssam			    sav->sah->saidx.dst.sa.sa_family,
564199897Sbz			    ipsec_address(&sav->sah->saidx.dst),
565105197Ssam			    (u_long) ntohl(sav->spi)));
566105197Ssam			error = EPFNOSUPPORT;
567105197Ssam			goto bad;
568105197Ssam		}
569105197Ssam	} else {
570199946Sbz		/* Compression was useless, we have lost time. */
571199946Sbz		V_ipcompstat.ipcomps_uncompr++;
572199946Sbz		DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
573199946Sbz		    __func__, crp->crp_ilen, skip, crp->crp_olen));
574199899Sbz		/* XXX remember state to not compress the next couple
575199899Sbz		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
576105197Ssam	}
577105197Ssam
578105197Ssam	/* Release the crypto descriptor */
579105197Ssam	free(tc, M_XDATA);
580105197Ssam	crypto_freereq(crp);
581105197Ssam
582105197Ssam	/* NB: m is reclaimed by ipsec_process_done. */
583105197Ssam	error = ipsec_process_done(m, isr);
584105197Ssam	KEY_FREESAV(&sav);
585120585Ssam	IPSECREQUEST_UNLOCK(isr);
586105197Ssam	return error;
587105197Ssambad:
588105197Ssam	if (sav)
589105197Ssam		KEY_FREESAV(&sav);
590120585Ssam	IPSECREQUEST_UNLOCK(isr);
591105197Ssam	if (m)
592105197Ssam		m_freem(m);
593105197Ssam	free(tc, M_XDATA);
594105197Ssam	crypto_freereq(crp);
595105197Ssam	return error;
596105197Ssam}
597105197Ssam
598105197Ssamstatic struct xformsw ipcomp_xformsw = {
599105197Ssam	XF_IPCOMP,		XFT_COMP,		"IPcomp",
600105197Ssam	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
601105197Ssam	ipcomp_output
602105197Ssam};
603105197Ssam
604105197Ssamstatic void
605105197Ssamipcomp_attach(void)
606105197Ssam{
607185088Szec
608105197Ssam	xform_register(&ipcomp_xformsw);
609105197Ssam}
610190787Szec
611125099SsamSYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
612199946Sbz
613199946Sbzstatic void
614199946Sbzvnet_ipcomp_attach(const void *unused __unused)
615199946Sbz{
616199946Sbz
617199946Sbz	V_ipcompstat.version = IPCOMPSTAT_VERSION;
618199946Sbz}
619199946Sbz
620199946SbzVNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
621199946Sbz    vnet_ipcomp_attach, NULL);
622