xform_ipcomp.c revision 156756
1105197Ssam/*	$FreeBSD: head/sys/netipsec/xform_ipcomp.c 156756 2006-03-15 21:11:11Z sam $	*/
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>
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) {
105120585Ssam		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
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
144120585Ssam	IPSEC_SPLASSERT_SOFTNET(__func__);
145105197Ssam
146105197Ssam	/* Get crypto descriptors */
147105197Ssam	crp = crypto_getreq(1);
148105197Ssam	if (crp == NULL) {
149105197Ssam		m_freem(m);
150120585Ssam		DPRINTF(("%s: no crypto descriptors\n", __func__));
151105197Ssam		ipcompstat.ipcomps_crypto++;
152105197Ssam		return ENOBUFS;
153105197Ssam	}
154105197Ssam	/* Get IPsec-specific opaque pointer */
155105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
156105197Ssam	if (tc == NULL) {
157105197Ssam		m_freem(m);
158105197Ssam		crypto_freereq(crp);
159120585Ssam		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
160105197Ssam		ipcompstat.ipcomps_crypto++;
161105197Ssam		return ENOBUFS;
162105197Ssam	}
163105197Ssam	crdc = crp->crp_desc;
164105197Ssam
165105197Ssam	crdc->crd_skip = skip + hlen;
166105197Ssam	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
167105197Ssam	crdc->crd_inject = skip;
168105197Ssam
169105197Ssam	tc->tc_ptr = 0;
170105197Ssam
171105197Ssam	/* Decompression operation */
172105197Ssam	crdc->crd_alg = sav->tdb_compalgxform->type;
173105197Ssam
174105197Ssam	/* Crypto operation descriptor */
175105197Ssam	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
176117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
177105197Ssam	crp->crp_buf = (caddr_t) m;
178105197Ssam	crp->crp_callback = ipcomp_input_cb;
179105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
180105197Ssam	crp->crp_opaque = (caddr_t) tc;
181105197Ssam
182105197Ssam	/* These are passed as-is to the callback */
183105197Ssam	tc->tc_spi = sav->spi;
184105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
185105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
186105197Ssam	tc->tc_protoff = protoff;
187105197Ssam	tc->tc_skip = skip;
188105197Ssam
189105197Ssam	return crypto_dispatch(crp);
190105197Ssam}
191105197Ssam
192105197Ssam#ifdef INET6
193105197Ssam#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
194105197Ssam	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
195105197Ssam		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
196105197Ssam	} else {							     \
197105197Ssam		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
198105197Ssam	}								     \
199105197Ssam} while (0)
200105197Ssam#else
201105197Ssam#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
202105197Ssam	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
203105197Ssam#endif
204105197Ssam
205105197Ssam/*
206105197Ssam * IPComp input callback from the crypto driver.
207105197Ssam */
208105197Ssamstatic int
209105197Ssamipcomp_input_cb(struct cryptop *crp)
210105197Ssam{
211105197Ssam	struct cryptodesc *crd;
212105197Ssam	struct tdb_crypto *tc;
213105197Ssam	int skip, protoff;
214105197Ssam	struct mtag *mtag;
215105197Ssam	struct mbuf *m;
216105197Ssam	struct secasvar *sav;
217105197Ssam	struct secasindex *saidx;
218119643Ssam	int hlen = IPCOMP_HLENGTH, error, clen;
219105197Ssam	u_int8_t nproto;
220105197Ssam	caddr_t addr;
221105197Ssam
222105197Ssam	crd = crp->crp_desc;
223105197Ssam
224105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
225120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
226105197Ssam	skip = tc->tc_skip;
227105197Ssam	protoff = tc->tc_protoff;
228105197Ssam	mtag = (struct mtag *) tc->tc_ptr;
229105197Ssam	m = (struct mbuf *) crp->crp_buf;
230105197Ssam
231105197Ssam	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
232105197Ssam	if (sav == NULL) {
233105197Ssam		ipcompstat.ipcomps_notdb++;
234120585Ssam		DPRINTF(("%s: SA expired while in crypto\n", __func__));
235105197Ssam		error = ENOBUFS;		/*XXX*/
236105197Ssam		goto bad;
237105197Ssam	}
238105197Ssam
239105197Ssam	saidx = &sav->sah->saidx;
240120585Ssam	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
241105197Ssam		saidx->dst.sa.sa_family == AF_INET6,
242120585Ssam		("unexpected protocol family %u", saidx->dst.sa.sa_family));
243105197Ssam
244105197Ssam	/* Check for crypto errors */
245105197Ssam	if (crp->crp_etype) {
246105197Ssam		/* Reset the session ID */
247105197Ssam		if (sav->tdb_cryptoid != 0)
248105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
249105197Ssam
250105197Ssam		if (crp->crp_etype == EAGAIN) {
251105197Ssam			KEY_FREESAV(&sav);
252105197Ssam			return crypto_dispatch(crp);
253105197Ssam		}
254105197Ssam
255105197Ssam		ipcompstat.ipcomps_noxform++;
256120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
257105197Ssam		error = crp->crp_etype;
258105197Ssam		goto bad;
259105197Ssam	}
260105197Ssam	/* Shouldn't happen... */
261105197Ssam	if (m == NULL) {
262105197Ssam		ipcompstat.ipcomps_crypto++;
263120585Ssam		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
264105197Ssam		error = EINVAL;
265105197Ssam		goto bad;
266105197Ssam	}
267105197Ssam	ipcompstat.ipcomps_hist[sav->alg_comp]++;
268105197Ssam
269105197Ssam	clen = crp->crp_olen;		/* Length of data after processing */
270105197Ssam
271105197Ssam	/* Release the crypto descriptors */
272105197Ssam	free(tc, M_XDATA), tc = NULL;
273105197Ssam	crypto_freereq(crp), crp = NULL;
274105197Ssam
275105197Ssam	/* In case it's not done already, adjust the size of the mbuf chain */
276105197Ssam	m->m_pkthdr.len = clen + hlen + skip;
277105197Ssam
278105197Ssam	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
279105197Ssam		ipcompstat.ipcomps_hdrops++;		/*XXX*/
280120585Ssam		DPRINTF(("%s: m_pullup failed\n", __func__));
281105197Ssam		error = EINVAL;				/*XXX*/
282105197Ssam		goto bad;
283105197Ssam	}
284105197Ssam
285105197Ssam	/* Keep the next protocol field */
286105197Ssam	addr = (caddr_t) mtod(m, struct ip *) + skip;
287105197Ssam	nproto = ((struct ipcomp *) addr)->comp_nxt;
288105197Ssam
289105197Ssam	/* Remove the IPCOMP header */
290105197Ssam	error = m_striphdr(m, skip, hlen);
291105197Ssam	if (error) {
292105197Ssam		ipcompstat.ipcomps_hdrops++;
293120585Ssam		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
294105197Ssam			 ipsec_address(&sav->sah->saidx.dst),
295105197Ssam			 (u_long) ntohl(sav->spi)));
296105197Ssam		goto bad;
297105197Ssam	}
298105197Ssam
299105197Ssam	/* Restore the Next Protocol field */
300105197Ssam	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
301105197Ssam
302105197Ssam	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
303105197Ssam
304105197Ssam	KEY_FREESAV(&sav);
305105197Ssam	return error;
306105197Ssambad:
307105197Ssam	if (sav)
308105197Ssam		KEY_FREESAV(&sav);
309105197Ssam	if (m)
310105197Ssam		m_freem(m);
311105197Ssam	if (tc != NULL)
312105197Ssam		free(tc, M_XDATA);
313105197Ssam	if (crp)
314105197Ssam		crypto_freereq(crp);
315105197Ssam	return error;
316105197Ssam}
317105197Ssam
318105197Ssam/*
319105197Ssam * IPComp output routine, called by ipsec[46]_process_packet()
320105197Ssam */
321105197Ssamstatic int
322105197Ssamipcomp_output(
323105197Ssam	struct mbuf *m,
324105197Ssam	struct ipsecrequest *isr,
325105197Ssam	struct mbuf **mp,
326105197Ssam	int skip,
327105197Ssam	int protoff
328105197Ssam)
329105197Ssam{
330105197Ssam	struct secasvar *sav;
331105197Ssam	struct comp_algo *ipcompx;
332105197Ssam	int error, ralen, hlen, maxpacketsize, roff;
333105197Ssam	u_int8_t prot;
334105197Ssam	struct cryptodesc *crdc;
335105197Ssam	struct cryptop *crp;
336105197Ssam	struct tdb_crypto *tc;
337105197Ssam	struct mbuf *mo;
338105197Ssam	struct ipcomp *ipcomp;
339105197Ssam
340120585Ssam	IPSEC_SPLASSERT_SOFTNET(__func__);
341105197Ssam
342105197Ssam	sav = isr->sav;
343120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
344105197Ssam	ipcompx = sav->tdb_compalgxform;
345120585Ssam	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
346105197Ssam
347105197Ssam	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
348105197Ssam	hlen = IPCOMP_HLENGTH;
349105197Ssam
350105197Ssam	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:
356105197Ssam		maxpacketsize =  IP_MAXPACKET;
357105197Ssam		break;
358105197Ssam#endif /* INET */
359105197Ssam#ifdef INET6
360105197Ssam	case AF_INET6:
361105197Ssam		maxpacketsize =  IPV6_MAXPACKET;
362105197Ssam		break;
363105197Ssam#endif /* INET6 */
364105197Ssam	default:
365105197Ssam		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	}
374105197Ssam	if (skip + hlen + ralen > maxpacketsize) {
375105197Ssam		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),
380105197Ssam		    skip + hlen + ralen, maxpacketsize));
381105197Ssam		error = EMSGSIZE;
382105197Ssam		goto bad;
383105197Ssam	}
384105197Ssam
385105197Ssam	/* Update the counters */
386105197Ssam	ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
387105197Ssam
388156756Ssam	m = m_unshare(m, M_NOWAIT);
389105197Ssam	if (m == NULL) {
390105197Ssam		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
398105197Ssam	/* Inject IPCOMP header */
399105197Ssam	mo = m_makespace(m, skip, hlen, &roff);
400105197Ssam	if (mo == NULL) {
401105197Ssam		ipcompstat.ipcomps_wrap++;
402120585Ssam		DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
403120585Ssam		    __func__, ipsec_address(&sav->sah->saidx.dst),
404105197Ssam		    (u_long) ntohl(sav->spi)));
405105197Ssam		error = ENOBUFS;
406105197Ssam		goto bad;
407105197Ssam	}
408105197Ssam	ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
409105197Ssam
410105197Ssam	/* Initialize the IPCOMP header */
411105197Ssam	/* XXX alignment always correct? */
412105197Ssam	switch (sav->sah->saidx.dst.sa.sa_family) {
413105197Ssam#ifdef INET
414105197Ssam	case AF_INET:
415105197Ssam		ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
416105197Ssam		break;
417105197Ssam#endif /* INET */
418105197Ssam#ifdef INET6
419105197Ssam	case AF_INET6:
420105197Ssam		ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
421105197Ssam		break;
422105197Ssam#endif
423105197Ssam	}
424105197Ssam	ipcomp->comp_flags = 0;
425105197Ssam	ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
426105197Ssam
427105197Ssam	/* Fix Next Protocol in IPv4/IPv6 header */
428105197Ssam	prot = IPPROTO_IPCOMP;
429105197Ssam	m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
430105197Ssam
431105197Ssam	/* Ok now, we can pass to the crypto processing */
432105197Ssam
433105197Ssam	/* Get crypto descriptors */
434105197Ssam	crp = crypto_getreq(1);
435105197Ssam	if (crp == NULL) {
436105197Ssam		ipcompstat.ipcomps_crypto++;
437120585Ssam		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
438105197Ssam		error = ENOBUFS;
439105197Ssam		goto bad;
440105197Ssam	}
441105197Ssam	crdc = crp->crp_desc;
442105197Ssam
443105197Ssam	/* Compression descriptor */
444105197Ssam	crdc->crd_skip = skip + hlen;
445105197Ssam	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
446105197Ssam	crdc->crd_flags = CRD_F_COMP;
447105197Ssam	crdc->crd_inject = skip + hlen;
448105197Ssam
449105197Ssam	/* Compression operation */
450105197Ssam	crdc->crd_alg = ipcompx->type;
451105197Ssam
452105197Ssam	/* IPsec-specific opaque crypto info */
453105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
454105197Ssam		M_XDATA, M_NOWAIT|M_ZERO);
455105197Ssam	if (tc == NULL) {
456105197Ssam		ipcompstat.ipcomps_crypto++;
457120585Ssam		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
458105197Ssam		crypto_freereq(crp);
459105197Ssam		error = ENOBUFS;
460105197Ssam		goto bad;
461105197Ssam	}
462105197Ssam
463105197Ssam	tc->tc_isr = isr;
464105197Ssam	tc->tc_spi = sav->spi;
465105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
466105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
467105197Ssam	tc->tc_skip = skip + hlen;
468105197Ssam
469105197Ssam	/* Crypto operation descriptor */
470105197Ssam	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
471117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
472105197Ssam	crp->crp_buf = (caddr_t) m;
473105197Ssam	crp->crp_callback = ipcomp_output_cb;
474105197Ssam	crp->crp_opaque = (caddr_t) tc;
475105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
476105197Ssam
477105197Ssam	return crypto_dispatch(crp);
478105197Ssambad:
479105197Ssam	if (m)
480105197Ssam		m_freem(m);
481105197Ssam	return (error);
482105197Ssam}
483105197Ssam
484105197Ssam/*
485105197Ssam * IPComp output callback from the crypto driver.
486105197Ssam */
487105197Ssamstatic int
488105197Ssamipcomp_output_cb(struct cryptop *crp)
489105197Ssam{
490105197Ssam	struct tdb_crypto *tc;
491105197Ssam	struct ipsecrequest *isr;
492105197Ssam	struct secasvar *sav;
493105197Ssam	struct mbuf *m;
494119643Ssam	int error, skip, rlen;
495105197Ssam
496105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
497120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
498105197Ssam	m = (struct mbuf *) crp->crp_buf;
499105197Ssam	skip = tc->tc_skip;
500105197Ssam	rlen = crp->crp_ilen - skip;
501105197Ssam
502105197Ssam	isr = tc->tc_isr;
503120585Ssam	IPSECREQUEST_LOCK(isr);
504105197Ssam	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
505105197Ssam	if (sav == NULL) {
506105197Ssam		ipcompstat.ipcomps_notdb++;
507120585Ssam		DPRINTF(("%s: SA expired while in crypto\n", __func__));
508105197Ssam		error = ENOBUFS;		/*XXX*/
509105197Ssam		goto bad;
510105197Ssam	}
511120585Ssam	IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
512105197Ssam
513105197Ssam	/* Check for crypto errors */
514105197Ssam	if (crp->crp_etype) {
515105197Ssam		/* Reset session ID */
516105197Ssam		if (sav->tdb_cryptoid != 0)
517105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
518105197Ssam
519105197Ssam		if (crp->crp_etype == EAGAIN) {
520105197Ssam			KEY_FREESAV(&sav);
521120585Ssam			IPSECREQUEST_UNLOCK(isr);
522105197Ssam			return crypto_dispatch(crp);
523105197Ssam		}
524105197Ssam		ipcompstat.ipcomps_noxform++;
525120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
526105197Ssam		error = crp->crp_etype;
527105197Ssam		goto bad;
528105197Ssam	}
529105197Ssam	/* Shouldn't happen... */
530105197Ssam	if (m == NULL) {
531105197Ssam		ipcompstat.ipcomps_crypto++;
532120585Ssam		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
533105197Ssam		error = EINVAL;
534105197Ssam		goto bad;
535105197Ssam	}
536105197Ssam	ipcompstat.ipcomps_hist[sav->alg_comp]++;
537105197Ssam
538105197Ssam	if (rlen > crp->crp_olen) {
539105197Ssam		/* Adjust the length in the IP header */
540105197Ssam		switch (sav->sah->saidx.dst.sa.sa_family) {
541105197Ssam#ifdef INET
542105197Ssam		case AF_INET:
543105197Ssam			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
544105197Ssam			break;
545105197Ssam#endif /* INET */
546105197Ssam#ifdef INET6
547105197Ssam		case AF_INET6:
548105197Ssam			mtod(m, struct ip6_hdr *)->ip6_plen =
549105197Ssam				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
550105197Ssam			break;
551105197Ssam#endif /* INET6 */
552105197Ssam		default:
553105197Ssam			ipcompstat.ipcomps_nopf++;
554120585Ssam			DPRINTF(("%s: unknown/unsupported protocol "
555120585Ssam			    "family %d, IPCA %s/%08lx\n", __func__,
556105197Ssam			    sav->sah->saidx.dst.sa.sa_family,
557105197Ssam			    ipsec_address(&sav->sah->saidx.dst),
558105197Ssam			    (u_long) ntohl(sav->spi)));
559105197Ssam			error = EPFNOSUPPORT;
560105197Ssam			goto bad;
561105197Ssam		}
562105197Ssam	} else {
563105197Ssam		/* compression was useless, we have lost time */
564105197Ssam		/* XXX add statistic */
565105197Ssam	}
566105197Ssam
567105197Ssam	/* Release the crypto descriptor */
568105197Ssam	free(tc, M_XDATA);
569105197Ssam	crypto_freereq(crp);
570105197Ssam
571105197Ssam	/* NB: m is reclaimed by ipsec_process_done. */
572105197Ssam	error = ipsec_process_done(m, isr);
573105197Ssam	KEY_FREESAV(&sav);
574120585Ssam	IPSECREQUEST_UNLOCK(isr);
575119643Ssam
576105197Ssam	return error;
577105197Ssambad:
578105197Ssam	if (sav)
579105197Ssam		KEY_FREESAV(&sav);
580120585Ssam	IPSECREQUEST_UNLOCK(isr);
581105197Ssam	if (m)
582105197Ssam		m_freem(m);
583105197Ssam	free(tc, M_XDATA);
584105197Ssam	crypto_freereq(crp);
585105197Ssam	return error;
586105197Ssam}
587105197Ssam
588105197Ssamstatic struct xformsw ipcomp_xformsw = {
589105197Ssam	XF_IPCOMP,		XFT_COMP,		"IPcomp",
590105197Ssam	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
591105197Ssam	ipcomp_output
592105197Ssam};
593105197Ssam
594105197Ssamstatic void
595105197Ssamipcomp_attach(void)
596105197Ssam{
597105197Ssam	xform_register(&ipcomp_xformsw);
598105197Ssam}
599125099SsamSYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
600