xform_ipcomp.c revision 119643
1105197Ssam/*	$FreeBSD: head/sys/netipsec/xform_ipcomp.c 119643 2003-09-01 05:35:55Z sam $	*/
2105197Ssam/* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3105197Ssam
4105197Ssam/*
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>
40105197Ssam#include <sys/socket.h>
41105197Ssam#include <sys/kernel.h>
42105197Ssam#include <sys/protosw.h>
43105197Ssam#include <sys/sysctl.h>
44105197Ssam
45105197Ssam#include <netinet/in.h>
46105197Ssam#include <netinet/in_systm.h>
47105197Ssam#include <netinet/ip.h>
48105197Ssam#include <netinet/ip_var.h>
49105197Ssam
50105197Ssam#include <net/route.h>
51105197Ssam#include <netipsec/ipsec.h>
52105197Ssam#include <netipsec/xform.h>
53105197Ssam
54105197Ssam#ifdef INET6
55105197Ssam#include <netinet/ip6.h>
56105197Ssam#include <netipsec/ipsec6.h>
57105197Ssam#endif
58105197Ssam
59105197Ssam#include <netipsec/ipcomp.h>
60105197Ssam#include <netipsec/ipcomp_var.h>
61105197Ssam
62105197Ssam#include <netipsec/key.h>
63105197Ssam#include <netipsec/key_debug.h>
64105197Ssam
65105197Ssam#include <opencrypto/cryptodev.h>
66105197Ssam#include <opencrypto/deflate.h>
67105197Ssam#include <opencrypto/xform.h>
68105197Ssam
69105197Ssamint	ipcomp_enable = 0;
70105197Ssamstruct	ipcompstat ipcompstat;
71105197Ssam
72105197SsamSYSCTL_DECL(_net_inet_ipcomp);
73105197SsamSYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
74105197Ssam	ipcomp_enable,	CTLFLAG_RW,	&ipcomp_enable,	0, "");
75105197SsamSYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
76105197Ssam	stats,		CTLFLAG_RD,	&ipcompstat,	ipcompstat, "");
77105197Ssam
78105197Ssamstatic int ipcomp_input_cb(struct cryptop *crp);
79105197Ssamstatic int ipcomp_output_cb(struct cryptop *crp);
80105197Ssam
81105197Ssamstruct comp_algo *
82105197Ssamipcomp_algorithm_lookup(int alg)
83105197Ssam{
84105197Ssam	if (alg >= IPCOMP_ALG_MAX)
85105197Ssam		return NULL;
86105197Ssam	switch (alg) {
87105197Ssam	case SADB_X_CALG_DEFLATE:
88105197Ssam		return &comp_algo_deflate;
89105197Ssam	}
90105197Ssam	return NULL;
91105197Ssam}
92105197Ssam
93105197Ssam/*
94105197Ssam * ipcomp_init() is called when an CPI is being set up.
95105197Ssam */
96105197Ssamstatic int
97105197Ssamipcomp_init(struct secasvar *sav, struct xformsw *xsp)
98105197Ssam{
99105197Ssam	struct comp_algo *tcomp;
100105197Ssam	struct cryptoini cric;
101105197Ssam
102105197Ssam	/* NB: algorithm really comes in alg_enc and not alg_comp! */
103105197Ssam	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
104105197Ssam	if (tcomp == NULL) {
105105197Ssam		DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
106105197Ssam			 sav->alg_comp));
107105197Ssam		return EINVAL;
108105197Ssam	}
109105197Ssam	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
110105197Ssam	sav->tdb_xform = xsp;
111105197Ssam	sav->tdb_compalgxform = tcomp;
112105197Ssam
113105197Ssam	/* Initialize crypto session */
114105197Ssam	bzero(&cric, sizeof (cric));
115105197Ssam	cric.cri_alg = sav->tdb_compalgxform->type;
116105197Ssam
117105197Ssam	return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
118105197Ssam}
119105197Ssam
120105197Ssam/*
121105197Ssam * ipcomp_zeroize() used when IPCA is deleted
122105197Ssam */
123105197Ssamstatic int
124105197Ssamipcomp_zeroize(struct secasvar *sav)
125105197Ssam{
126105197Ssam	int err;
127105197Ssam
128105197Ssam	err = crypto_freesession(sav->tdb_cryptoid);
129105197Ssam	sav->tdb_cryptoid = 0;
130105197Ssam	return err;
131105197Ssam}
132105197Ssam
133105197Ssam/*
134105197Ssam * ipcomp_input() gets called to uncompress an input packet
135105197Ssam */
136105197Ssamstatic int
137105197Ssamipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
138105197Ssam{
139105197Ssam	struct tdb_crypto *tc;
140105197Ssam	struct cryptodesc *crdc;
141105197Ssam	struct cryptop *crp;
142105197Ssam	int hlen = IPCOMP_HLENGTH;
143105197Ssam
144105197Ssam#if 0
145105197Ssam	SPLASSERT(net, "ipcomp_input");
146105197Ssam#endif
147105197Ssam
148105197Ssam	/* Get crypto descriptors */
149105197Ssam	crp = crypto_getreq(1);
150105197Ssam	if (crp == NULL) {
151105197Ssam		m_freem(m);
152105197Ssam		DPRINTF(("ipcomp_input: no crypto descriptors\n"));
153105197Ssam		ipcompstat.ipcomps_crypto++;
154105197Ssam		return ENOBUFS;
155105197Ssam	}
156105197Ssam	/* Get IPsec-specific opaque pointer */
157105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
158105197Ssam	if (tc == NULL) {
159105197Ssam		m_freem(m);
160105197Ssam		crypto_freereq(crp);
161105197Ssam		DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
162105197Ssam		ipcompstat.ipcomps_crypto++;
163105197Ssam		return ENOBUFS;
164105197Ssam	}
165105197Ssam	crdc = crp->crp_desc;
166105197Ssam
167105197Ssam	crdc->crd_skip = skip + hlen;
168105197Ssam	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
169105197Ssam	crdc->crd_inject = skip;
170105197Ssam
171105197Ssam	tc->tc_ptr = 0;
172105197Ssam
173105197Ssam	/* Decompression operation */
174105197Ssam	crdc->crd_alg = sav->tdb_compalgxform->type;
175105197Ssam
176105197Ssam	/* Crypto operation descriptor */
177105197Ssam	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
178117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
179105197Ssam	crp->crp_buf = (caddr_t) m;
180105197Ssam	crp->crp_callback = ipcomp_input_cb;
181105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
182105197Ssam	crp->crp_opaque = (caddr_t) tc;
183105197Ssam
184105197Ssam	/* These are passed as-is to the callback */
185105197Ssam	tc->tc_spi = sav->spi;
186105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
187105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
188105197Ssam	tc->tc_protoff = protoff;
189105197Ssam	tc->tc_skip = skip;
190105197Ssam
191105197Ssam	return crypto_dispatch(crp);
192105197Ssam}
193105197Ssam
194105197Ssam#ifdef INET6
195105197Ssam#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
196105197Ssam	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
197105197Ssam		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
198105197Ssam	} else {							     \
199105197Ssam		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
200105197Ssam	}								     \
201105197Ssam} while (0)
202105197Ssam#else
203105197Ssam#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
204105197Ssam	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
205105197Ssam#endif
206105197Ssam
207105197Ssam/*
208105197Ssam * IPComp input callback from the crypto driver.
209105197Ssam */
210105197Ssamstatic int
211105197Ssamipcomp_input_cb(struct cryptop *crp)
212105197Ssam{
213105197Ssam	struct cryptodesc *crd;
214105197Ssam	struct tdb_crypto *tc;
215105197Ssam	int skip, protoff;
216105197Ssam	struct mtag *mtag;
217105197Ssam	struct mbuf *m;
218105197Ssam	struct secasvar *sav;
219105197Ssam	struct secasindex *saidx;
220119643Ssam	int hlen = IPCOMP_HLENGTH, error, clen;
221105197Ssam	u_int8_t nproto;
222105197Ssam	caddr_t addr;
223105197Ssam
224105197Ssam	crd = crp->crp_desc;
225105197Ssam
226105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
227105197Ssam	KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
228105197Ssam	skip = tc->tc_skip;
229105197Ssam	protoff = tc->tc_protoff;
230105197Ssam	mtag = (struct mtag *) tc->tc_ptr;
231105197Ssam	m = (struct mbuf *) crp->crp_buf;
232105197Ssam
233105197Ssam	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
234105197Ssam	if (sav == NULL) {
235105197Ssam		ipcompstat.ipcomps_notdb++;
236105197Ssam		DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
237105197Ssam		error = ENOBUFS;		/*XXX*/
238105197Ssam		goto bad;
239105197Ssam	}
240105197Ssam
241105197Ssam	saidx = &sav->sah->saidx;
242105197Ssam	KASSERT(saidx->dst.sa.sa_family == AF_INET ||
243105197Ssam		saidx->dst.sa.sa_family == AF_INET6,
244105197Ssam		("ah_input_cb: unexpected protocol family %u",
245105197Ssam		 saidx->dst.sa.sa_family));
246105197Ssam
247105197Ssam	/* Check for crypto errors */
248105197Ssam	if (crp->crp_etype) {
249105197Ssam		/* Reset the session ID */
250105197Ssam		if (sav->tdb_cryptoid != 0)
251105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
252105197Ssam
253105197Ssam		if (crp->crp_etype == EAGAIN) {
254105197Ssam			KEY_FREESAV(&sav);
255105197Ssam			return crypto_dispatch(crp);
256105197Ssam		}
257105197Ssam
258105197Ssam		ipcompstat.ipcomps_noxform++;
259105197Ssam		DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
260105197Ssam		error = crp->crp_etype;
261105197Ssam		goto bad;
262105197Ssam	}
263105197Ssam	/* Shouldn't happen... */
264105197Ssam	if (m == NULL) {
265105197Ssam		ipcompstat.ipcomps_crypto++;
266105197Ssam		DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
267105197Ssam		error = EINVAL;
268105197Ssam		goto bad;
269105197Ssam	}
270105197Ssam	ipcompstat.ipcomps_hist[sav->alg_comp]++;
271105197Ssam
272105197Ssam	clen = crp->crp_olen;		/* Length of data after processing */
273105197Ssam
274105197Ssam	/* Release the crypto descriptors */
275105197Ssam	free(tc, M_XDATA), tc = NULL;
276105197Ssam	crypto_freereq(crp), crp = NULL;
277105197Ssam
278105197Ssam	/* In case it's not done already, adjust the size of the mbuf chain */
279105197Ssam	m->m_pkthdr.len = clen + hlen + skip;
280105197Ssam
281105197Ssam	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
282105197Ssam		ipcompstat.ipcomps_hdrops++;		/*XXX*/
283105197Ssam		DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
284105197Ssam		error = EINVAL;				/*XXX*/
285105197Ssam		goto bad;
286105197Ssam	}
287105197Ssam
288105197Ssam	/* Keep the next protocol field */
289105197Ssam	addr = (caddr_t) mtod(m, struct ip *) + skip;
290105197Ssam	nproto = ((struct ipcomp *) addr)->comp_nxt;
291105197Ssam
292105197Ssam	/* Remove the IPCOMP header */
293105197Ssam	error = m_striphdr(m, skip, hlen);
294105197Ssam	if (error) {
295105197Ssam		ipcompstat.ipcomps_hdrops++;
296105197Ssam		DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
297105197Ssam			 ipsec_address(&sav->sah->saidx.dst),
298105197Ssam			 (u_long) ntohl(sav->spi)));
299105197Ssam		goto bad;
300105197Ssam	}
301105197Ssam
302105197Ssam	/* Restore the Next Protocol field */
303105197Ssam	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
304105197Ssam
305105197Ssam	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
306105197Ssam
307105197Ssam	KEY_FREESAV(&sav);
308105197Ssam	return error;
309105197Ssambad:
310105197Ssam	if (sav)
311105197Ssam		KEY_FREESAV(&sav);
312105197Ssam	if (m)
313105197Ssam		m_freem(m);
314105197Ssam	if (tc != NULL)
315105197Ssam		free(tc, M_XDATA);
316105197Ssam	if (crp)
317105197Ssam		crypto_freereq(crp);
318105197Ssam	return error;
319105197Ssam}
320105197Ssam
321105197Ssam/*
322105197Ssam * IPComp output routine, called by ipsec[46]_process_packet()
323105197Ssam */
324105197Ssamstatic int
325105197Ssamipcomp_output(
326105197Ssam	struct mbuf *m,
327105197Ssam	struct ipsecrequest *isr,
328105197Ssam	struct mbuf **mp,
329105197Ssam	int skip,
330105197Ssam	int protoff
331105197Ssam)
332105197Ssam{
333105197Ssam	struct secasvar *sav;
334105197Ssam	struct comp_algo *ipcompx;
335105197Ssam	int error, ralen, hlen, maxpacketsize, roff;
336105197Ssam	u_int8_t prot;
337105197Ssam	struct cryptodesc *crdc;
338105197Ssam	struct cryptop *crp;
339105197Ssam	struct tdb_crypto *tc;
340105197Ssam	struct mbuf *mo;
341105197Ssam	struct ipcomp *ipcomp;
342105197Ssam
343105197Ssam#if 0
344105197Ssam	SPLASSERT(net, "ipcomp_output");
345105197Ssam#endif
346105197Ssam
347105197Ssam	sav = isr->sav;
348105197Ssam	KASSERT(sav != NULL, ("ipcomp_output: null SA"));
349105197Ssam	ipcompx = sav->tdb_compalgxform;
350105197Ssam	KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
351105197Ssam
352105197Ssam	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
353105197Ssam	hlen = IPCOMP_HLENGTH;
354105197Ssam
355105197Ssam	ipcompstat.ipcomps_output++;
356105197Ssam
357105197Ssam	/* Check for maximum packet size violations. */
358105197Ssam	switch (sav->sah->saidx.dst.sa.sa_family) {
359105197Ssam#ifdef INET
360105197Ssam	case AF_INET:
361105197Ssam		maxpacketsize =  IP_MAXPACKET;
362105197Ssam		break;
363105197Ssam#endif /* INET */
364105197Ssam#ifdef INET6
365105197Ssam	case AF_INET6:
366105197Ssam		maxpacketsize =  IPV6_MAXPACKET;
367105197Ssam		break;
368105197Ssam#endif /* INET6 */
369105197Ssam	default:
370105197Ssam		ipcompstat.ipcomps_nopf++;
371105197Ssam		DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
372105197Ssam		    ", IPCA %s/%08lx\n",
373105197Ssam		    sav->sah->saidx.dst.sa.sa_family,
374105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
375105197Ssam		    (u_long) ntohl(sav->spi)));
376105197Ssam		error = EPFNOSUPPORT;
377105197Ssam		goto bad;
378105197Ssam	}
379105197Ssam	if (skip + hlen + ralen > maxpacketsize) {
380105197Ssam		ipcompstat.ipcomps_toobig++;
381105197Ssam		DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
382105197Ssam		    "(len %u, max len %u)\n",
383105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
384105197Ssam		    (u_long) ntohl(sav->spi),
385105197Ssam		    skip + hlen + ralen, maxpacketsize));
386105197Ssam		error = EMSGSIZE;
387105197Ssam		goto bad;
388105197Ssam	}
389105197Ssam
390105197Ssam	/* Update the counters */
391105197Ssam	ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
392105197Ssam
393105197Ssam	m = m_clone(m);
394105197Ssam	if (m == NULL) {
395105197Ssam		ipcompstat.ipcomps_hdrops++;
396105197Ssam		DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
397105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
398105197Ssam		    (u_long) ntohl(sav->spi)));
399105197Ssam		error = ENOBUFS;
400105197Ssam		goto bad;
401105197Ssam	}
402105197Ssam
403105197Ssam	/* Inject IPCOMP header */
404105197Ssam	mo = m_makespace(m, skip, hlen, &roff);
405105197Ssam	if (mo == NULL) {
406105197Ssam		ipcompstat.ipcomps_wrap++;
407105197Ssam		DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
408105197Ssam		    "IPCA %s/%08lx\n",
409105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
410105197Ssam		    (u_long) ntohl(sav->spi)));
411105197Ssam		error = ENOBUFS;
412105197Ssam		goto bad;
413105197Ssam	}
414105197Ssam	ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
415105197Ssam
416105197Ssam	/* Initialize the IPCOMP header */
417105197Ssam	/* XXX alignment always correct? */
418105197Ssam	switch (sav->sah->saidx.dst.sa.sa_family) {
419105197Ssam#ifdef INET
420105197Ssam	case AF_INET:
421105197Ssam		ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
422105197Ssam		break;
423105197Ssam#endif /* INET */
424105197Ssam#ifdef INET6
425105197Ssam	case AF_INET6:
426105197Ssam		ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
427105197Ssam		break;
428105197Ssam#endif
429105197Ssam	}
430105197Ssam	ipcomp->comp_flags = 0;
431105197Ssam	ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
432105197Ssam
433105197Ssam	/* Fix Next Protocol in IPv4/IPv6 header */
434105197Ssam	prot = IPPROTO_IPCOMP;
435105197Ssam	m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
436105197Ssam
437105197Ssam	/* Ok now, we can pass to the crypto processing */
438105197Ssam
439105197Ssam	/* Get crypto descriptors */
440105197Ssam	crp = crypto_getreq(1);
441105197Ssam	if (crp == NULL) {
442105197Ssam		ipcompstat.ipcomps_crypto++;
443105197Ssam		DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
444105197Ssam		error = ENOBUFS;
445105197Ssam		goto bad;
446105197Ssam	}
447105197Ssam	crdc = crp->crp_desc;
448105197Ssam
449105197Ssam	/* Compression descriptor */
450105197Ssam	crdc->crd_skip = skip + hlen;
451105197Ssam	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
452105197Ssam	crdc->crd_flags = CRD_F_COMP;
453105197Ssam	crdc->crd_inject = skip + hlen;
454105197Ssam
455105197Ssam	/* Compression operation */
456105197Ssam	crdc->crd_alg = ipcompx->type;
457105197Ssam
458105197Ssam	/* IPsec-specific opaque crypto info */
459105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
460105197Ssam		M_XDATA, M_NOWAIT|M_ZERO);
461105197Ssam	if (tc == NULL) {
462105197Ssam		ipcompstat.ipcomps_crypto++;
463105197Ssam		DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
464105197Ssam		crypto_freereq(crp);
465105197Ssam		error = ENOBUFS;
466105197Ssam		goto bad;
467105197Ssam	}
468105197Ssam
469105197Ssam	tc->tc_isr = isr;
470105197Ssam	tc->tc_spi = sav->spi;
471105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
472105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
473105197Ssam	tc->tc_skip = skip + hlen;
474105197Ssam
475105197Ssam	/* Crypto operation descriptor */
476105197Ssam	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
477117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
478105197Ssam	crp->crp_buf = (caddr_t) m;
479105197Ssam	crp->crp_callback = ipcomp_output_cb;
480105197Ssam	crp->crp_opaque = (caddr_t) tc;
481105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
482105197Ssam
483105197Ssam	return crypto_dispatch(crp);
484105197Ssambad:
485105197Ssam	if (m)
486105197Ssam		m_freem(m);
487105197Ssam	return (error);
488105197Ssam}
489105197Ssam
490105197Ssam/*
491105197Ssam * IPComp output callback from the crypto driver.
492105197Ssam */
493105197Ssamstatic int
494105197Ssamipcomp_output_cb(struct cryptop *crp)
495105197Ssam{
496105197Ssam	struct tdb_crypto *tc;
497105197Ssam	struct ipsecrequest *isr;
498105197Ssam	struct secasvar *sav;
499105197Ssam	struct mbuf *m;
500119643Ssam	int error, skip, rlen;
501105197Ssam
502105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
503105197Ssam	KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
504105197Ssam	m = (struct mbuf *) crp->crp_buf;
505105197Ssam	skip = tc->tc_skip;
506105197Ssam	rlen = crp->crp_ilen - skip;
507105197Ssam
508105197Ssam	isr = tc->tc_isr;
509119643Ssam	mtx_lock(&isr->lock);
510105197Ssam	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
511105197Ssam	if (sav == NULL) {
512105197Ssam		ipcompstat.ipcomps_notdb++;
513105197Ssam		DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
514105197Ssam		error = ENOBUFS;		/*XXX*/
515105197Ssam		goto bad;
516105197Ssam	}
517105197Ssam	KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
518105197Ssam
519105197Ssam	/* Check for crypto errors */
520105197Ssam	if (crp->crp_etype) {
521105197Ssam		/* Reset session ID */
522105197Ssam		if (sav->tdb_cryptoid != 0)
523105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
524105197Ssam
525105197Ssam		if (crp->crp_etype == EAGAIN) {
526105197Ssam			KEY_FREESAV(&sav);
527119643Ssam			mtx_unlock(&isr->lock);
528105197Ssam			return crypto_dispatch(crp);
529105197Ssam		}
530105197Ssam		ipcompstat.ipcomps_noxform++;
531105197Ssam		DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
532105197Ssam		error = crp->crp_etype;
533105197Ssam		goto bad;
534105197Ssam	}
535105197Ssam	/* Shouldn't happen... */
536105197Ssam	if (m == NULL) {
537105197Ssam		ipcompstat.ipcomps_crypto++;
538105197Ssam		DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
539105197Ssam		error = EINVAL;
540105197Ssam		goto bad;
541105197Ssam	}
542105197Ssam	ipcompstat.ipcomps_hist[sav->alg_comp]++;
543105197Ssam
544105197Ssam	if (rlen > crp->crp_olen) {
545105197Ssam		/* Adjust the length in the IP header */
546105197Ssam		switch (sav->sah->saidx.dst.sa.sa_family) {
547105197Ssam#ifdef INET
548105197Ssam		case AF_INET:
549105197Ssam			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
550105197Ssam			break;
551105197Ssam#endif /* INET */
552105197Ssam#ifdef INET6
553105197Ssam		case AF_INET6:
554105197Ssam			mtod(m, struct ip6_hdr *)->ip6_plen =
555105197Ssam				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
556105197Ssam			break;
557105197Ssam#endif /* INET6 */
558105197Ssam		default:
559105197Ssam			ipcompstat.ipcomps_nopf++;
560105197Ssam			DPRINTF(("ipcomp_output: unknown/unsupported protocol "
561105197Ssam			    "family %d, IPCA %s/%08lx\n",
562105197Ssam			    sav->sah->saidx.dst.sa.sa_family,
563105197Ssam			    ipsec_address(&sav->sah->saidx.dst),
564105197Ssam			    (u_long) ntohl(sav->spi)));
565105197Ssam			error = EPFNOSUPPORT;
566105197Ssam			goto bad;
567105197Ssam		}
568105197Ssam	} else {
569105197Ssam		/* compression was useless, we have lost time */
570105197Ssam		/* XXX add statistic */
571105197Ssam	}
572105197Ssam
573105197Ssam	/* Release the crypto descriptor */
574105197Ssam	free(tc, M_XDATA);
575105197Ssam	crypto_freereq(crp);
576105197Ssam
577105197Ssam	/* NB: m is reclaimed by ipsec_process_done. */
578105197Ssam	error = ipsec_process_done(m, isr);
579105197Ssam	KEY_FREESAV(&sav);
580119643Ssam	mtx_unlock(&isr->lock);
581119643Ssam
582105197Ssam	return error;
583105197Ssambad:
584105197Ssam	if (sav)
585105197Ssam		KEY_FREESAV(&sav);
586119643Ssam	mtx_unlock(&isr->lock);
587105197Ssam	if (m)
588105197Ssam		m_freem(m);
589105197Ssam	free(tc, M_XDATA);
590105197Ssam	crypto_freereq(crp);
591105197Ssam	return error;
592105197Ssam}
593105197Ssam
594105197Ssamstatic struct xformsw ipcomp_xformsw = {
595105197Ssam	XF_IPCOMP,		XFT_COMP,		"IPcomp",
596105197Ssam	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
597105197Ssam	ipcomp_output
598105197Ssam};
599105197Ssam
600105197Ssamstatic void
601105197Ssamipcomp_attach(void)
602105197Ssam{
603105197Ssam	xform_register(&ipcomp_xformsw);
604105197Ssam}
605105197SsamSYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)
606