1105197Ssam/*	$FreeBSD$	*/
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
51195699Srwatson#include <net/vnet.h>
52195699Srwatson
53105197Ssam#include <netipsec/ipsec.h>
54105197Ssam#include <netipsec/xform.h>
55105197Ssam
56105197Ssam#ifdef INET6
57105197Ssam#include <netinet/ip6.h>
58105197Ssam#include <netipsec/ipsec6.h>
59105197Ssam#endif
60105197Ssam
61105197Ssam#include <netipsec/ipcomp.h>
62105197Ssam#include <netipsec/ipcomp_var.h>
63105197Ssam
64105197Ssam#include <netipsec/key.h>
65105197Ssam#include <netipsec/key_debug.h>
66105197Ssam
67105197Ssam#include <opencrypto/cryptodev.h>
68105197Ssam#include <opencrypto/deflate.h>
69105197Ssam#include <opencrypto/xform.h>
70105197Ssam
71199947SbzVNET_DEFINE(int, ipcomp_enable) = 1;
72253088SaeVNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
73253088SaeVNET_PCPUSTAT_SYSINIT(ipcompstat);
74105197Ssam
75253088Sae#ifdef VIMAGE
76253088SaeVNET_PCPUSTAT_SYSUNINIT(ipcompstat);
77253088Sae#endif /* VIMAGE */
78253088Sae
79105197SsamSYSCTL_DECL(_net_inet_ipcomp);
80195699SrwatsonSYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
81195699Srwatson	ipcomp_enable,	CTLFLAG_RW,	&VNET_NAME(ipcomp_enable),	0, "");
82253088SaeSYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
83253088Sae    struct ipcompstat, ipcompstat,
84253088Sae    "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
85105197Ssam
86105197Ssamstatic int ipcomp_input_cb(struct cryptop *crp);
87105197Ssamstatic int ipcomp_output_cb(struct cryptop *crp);
88105197Ssam
89105197Ssamstruct comp_algo *
90105197Ssamipcomp_algorithm_lookup(int alg)
91105197Ssam{
92105197Ssam	if (alg >= IPCOMP_ALG_MAX)
93105197Ssam		return NULL;
94105197Ssam	switch (alg) {
95105197Ssam	case SADB_X_CALG_DEFLATE:
96105197Ssam		return &comp_algo_deflate;
97105197Ssam	}
98105197Ssam	return NULL;
99105197Ssam}
100105197Ssam
101105197Ssam/*
102105197Ssam * ipcomp_init() is called when an CPI is being set up.
103105197Ssam */
104105197Ssamstatic int
105105197Ssamipcomp_init(struct secasvar *sav, struct xformsw *xsp)
106105197Ssam{
107105197Ssam	struct comp_algo *tcomp;
108105197Ssam	struct cryptoini cric;
109105197Ssam
110105197Ssam	/* NB: algorithm really comes in alg_enc and not alg_comp! */
111105197Ssam	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
112105197Ssam	if (tcomp == NULL) {
113120585Ssam		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
114105197Ssam			 sav->alg_comp));
115105197Ssam		return EINVAL;
116105197Ssam	}
117105197Ssam	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
118105197Ssam	sav->tdb_xform = xsp;
119105197Ssam	sav->tdb_compalgxform = tcomp;
120105197Ssam
121105197Ssam	/* Initialize crypto session */
122105197Ssam	bzero(&cric, sizeof (cric));
123105197Ssam	cric.cri_alg = sav->tdb_compalgxform->type;
124105197Ssam
125181803Sbz	return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
126105197Ssam}
127105197Ssam
128105197Ssam/*
129105197Ssam * ipcomp_zeroize() used when IPCA is deleted
130105197Ssam */
131105197Ssamstatic int
132105197Ssamipcomp_zeroize(struct secasvar *sav)
133105197Ssam{
134105197Ssam	int err;
135105197Ssam
136105197Ssam	err = crypto_freesession(sav->tdb_cryptoid);
137105197Ssam	sav->tdb_cryptoid = 0;
138105197Ssam	return err;
139105197Ssam}
140105197Ssam
141105197Ssam/*
142105197Ssam * ipcomp_input() gets called to uncompress an input packet
143105197Ssam */
144105197Ssamstatic int
145105197Ssamipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
146105197Ssam{
147105197Ssam	struct tdb_crypto *tc;
148105197Ssam	struct cryptodesc *crdc;
149105197Ssam	struct cryptop *crp;
150220247Sbz	struct ipcomp *ipcomp;
151220247Sbz	caddr_t addr;
152105197Ssam	int hlen = IPCOMP_HLENGTH;
153105197Ssam
154220247Sbz	/*
155220247Sbz	 * Check that the next header of the IPComp is not IPComp again, before
156220247Sbz	 * doing any real work.  Given it is not possible to do double
157220247Sbz	 * compression it means someone is playing tricks on us.
158220247Sbz	 */
159220247Sbz	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
160252028Sae		IPCOMPSTAT_INC(ipcomps_hdrops);		/*XXX*/
161220247Sbz		DPRINTF(("%s: m_pullup failed\n", __func__));
162220247Sbz		return (ENOBUFS);
163220247Sbz	}
164220247Sbz	addr = (caddr_t) mtod(m, struct ip *) + skip;
165220247Sbz	ipcomp = (struct ipcomp *)addr;
166220247Sbz	if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
167220247Sbz		m_freem(m);
168252028Sae		IPCOMPSTAT_INC(ipcomps_pdrops);	/* XXX have our own stats? */
169220247Sbz		DPRINTF(("%s: recursive compression detected\n", __func__));
170220247Sbz		return (EINVAL);
171220247Sbz	}
172220247Sbz
173105197Ssam	/* Get crypto descriptors */
174105197Ssam	crp = crypto_getreq(1);
175105197Ssam	if (crp == NULL) {
176105197Ssam		m_freem(m);
177120585Ssam		DPRINTF(("%s: no crypto descriptors\n", __func__));
178252028Sae		IPCOMPSTAT_INC(ipcomps_crypto);
179105197Ssam		return ENOBUFS;
180105197Ssam	}
181105197Ssam	/* Get IPsec-specific opaque pointer */
182105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
183105197Ssam	if (tc == NULL) {
184105197Ssam		m_freem(m);
185105197Ssam		crypto_freereq(crp);
186120585Ssam		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
187252028Sae		IPCOMPSTAT_INC(ipcomps_crypto);
188105197Ssam		return ENOBUFS;
189105197Ssam	}
190105197Ssam	crdc = crp->crp_desc;
191105197Ssam
192105197Ssam	crdc->crd_skip = skip + hlen;
193105197Ssam	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
194105197Ssam	crdc->crd_inject = skip;
195105197Ssam
196105197Ssam	tc->tc_ptr = 0;
197105197Ssam
198105197Ssam	/* Decompression operation */
199105197Ssam	crdc->crd_alg = sav->tdb_compalgxform->type;
200105197Ssam
201105197Ssam	/* Crypto operation descriptor */
202105197Ssam	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
203117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
204105197Ssam	crp->crp_buf = (caddr_t) m;
205105197Ssam	crp->crp_callback = ipcomp_input_cb;
206105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
207105197Ssam	crp->crp_opaque = (caddr_t) tc;
208105197Ssam
209105197Ssam	/* These are passed as-is to the callback */
210105197Ssam	tc->tc_spi = sav->spi;
211105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
212105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
213105197Ssam	tc->tc_protoff = protoff;
214105197Ssam	tc->tc_skip = skip;
215220206Sfabient	KEY_ADDREFSA(sav);
216220206Sfabient	tc->tc_sav = sav;
217105197Ssam
218105197Ssam	return crypto_dispatch(crp);
219105197Ssam}
220105197Ssam
221105197Ssam/*
222105197Ssam * IPComp input callback from the crypto driver.
223105197Ssam */
224105197Ssamstatic int
225105197Ssamipcomp_input_cb(struct cryptop *crp)
226105197Ssam{
227105197Ssam	struct cryptodesc *crd;
228105197Ssam	struct tdb_crypto *tc;
229105197Ssam	int skip, protoff;
230105197Ssam	struct mtag *mtag;
231105197Ssam	struct mbuf *m;
232105197Ssam	struct secasvar *sav;
233105197Ssam	struct secasindex *saidx;
234119643Ssam	int hlen = IPCOMP_HLENGTH, error, clen;
235105197Ssam	u_int8_t nproto;
236105197Ssam	caddr_t addr;
237105197Ssam
238105197Ssam	crd = crp->crp_desc;
239105197Ssam
240105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
241120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
242105197Ssam	skip = tc->tc_skip;
243105197Ssam	protoff = tc->tc_protoff;
244105197Ssam	mtag = (struct mtag *) tc->tc_ptr;
245105197Ssam	m = (struct mbuf *) crp->crp_buf;
246105197Ssam
247220206Sfabient	sav = tc->tc_sav;
248220206Sfabient	IPSEC_ASSERT(sav != NULL, ("null SA!"));
249105197Ssam
250105197Ssam	saidx = &sav->sah->saidx;
251120585Ssam	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
252105197Ssam		saidx->dst.sa.sa_family == AF_INET6,
253120585Ssam		("unexpected protocol family %u", saidx->dst.sa.sa_family));
254105197Ssam
255105197Ssam	/* Check for crypto errors */
256105197Ssam	if (crp->crp_etype) {
257105197Ssam		/* Reset the session ID */
258105197Ssam		if (sav->tdb_cryptoid != 0)
259105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
260105197Ssam
261105197Ssam		if (crp->crp_etype == EAGAIN) {
262199905Sbz			return crypto_dispatch(crp);
263105197Ssam		}
264252028Sae		IPCOMPSTAT_INC(ipcomps_noxform);
265120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
266105197Ssam		error = crp->crp_etype;
267105197Ssam		goto bad;
268105197Ssam	}
269105197Ssam	/* Shouldn't happen... */
270105197Ssam	if (m == NULL) {
271252028Sae		IPCOMPSTAT_INC(ipcomps_crypto);
272120585Ssam		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
273105197Ssam		error = EINVAL;
274105197Ssam		goto bad;
275105197Ssam	}
276252028Sae	IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
277105197Ssam
278105197Ssam	clen = crp->crp_olen;		/* Length of data after processing */
279105197Ssam
280105197Ssam	/* Release the crypto descriptors */
281105197Ssam	free(tc, M_XDATA), tc = NULL;
282105197Ssam	crypto_freereq(crp), crp = NULL;
283105197Ssam
284105197Ssam	/* In case it's not done already, adjust the size of the mbuf chain */
285105197Ssam	m->m_pkthdr.len = clen + hlen + skip;
286105197Ssam
287105197Ssam	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
288252028Sae		IPCOMPSTAT_INC(ipcomps_hdrops);		/*XXX*/
289120585Ssam		DPRINTF(("%s: m_pullup failed\n", __func__));
290105197Ssam		error = EINVAL;				/*XXX*/
291105197Ssam		goto bad;
292105197Ssam	}
293105197Ssam
294105197Ssam	/* Keep the next protocol field */
295105197Ssam	addr = (caddr_t) mtod(m, struct ip *) + skip;
296105197Ssam	nproto = ((struct ipcomp *) addr)->comp_nxt;
297105197Ssam
298105197Ssam	/* Remove the IPCOMP header */
299105197Ssam	error = m_striphdr(m, skip, hlen);
300105197Ssam	if (error) {
301252028Sae		IPCOMPSTAT_INC(ipcomps_hdrops);
302120585Ssam		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
303105197Ssam			 ipsec_address(&sav->sah->saidx.dst),
304105197Ssam			 (u_long) ntohl(sav->spi)));
305105197Ssam		goto bad;
306105197Ssam	}
307105197Ssam
308105197Ssam	/* Restore the Next Protocol field */
309105197Ssam	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
310105197Ssam
311221129Sbz	switch (saidx->dst.sa.sa_family) {
312221129Sbz#ifdef INET6
313221129Sbz	case AF_INET6:
314221129Sbz		error = ipsec6_common_input_cb(m, sav, skip, protoff, NULL);
315221129Sbz		break;
316221129Sbz#endif
317221129Sbz#ifdef INET
318221129Sbz	case AF_INET:
319221129Sbz		error = ipsec4_common_input_cb(m, sav, skip, protoff, NULL);
320221129Sbz		break;
321221129Sbz#endif
322221129Sbz	default:
323221129Sbz		panic("%s: Unexpected address family: %d saidx=%p", __func__,
324221129Sbz		    saidx->dst.sa.sa_family, saidx);
325221129Sbz	}
326105197Ssam
327105197Ssam	KEY_FREESAV(&sav);
328105197Ssam	return error;
329105197Ssambad:
330105197Ssam	if (sav)
331105197Ssam		KEY_FREESAV(&sav);
332105197Ssam	if (m)
333105197Ssam		m_freem(m);
334105197Ssam	if (tc != NULL)
335105197Ssam		free(tc, M_XDATA);
336105197Ssam	if (crp)
337105197Ssam		crypto_freereq(crp);
338105197Ssam	return error;
339105197Ssam}
340105197Ssam
341105197Ssam/*
342105197Ssam * IPComp output routine, called by ipsec[46]_process_packet()
343105197Ssam */
344105197Ssamstatic int
345105197Ssamipcomp_output(
346105197Ssam	struct mbuf *m,
347105197Ssam	struct ipsecrequest *isr,
348105197Ssam	struct mbuf **mp,
349105197Ssam	int skip,
350105197Ssam	int protoff
351105197Ssam)
352105197Ssam{
353105197Ssam	struct secasvar *sav;
354105197Ssam	struct comp_algo *ipcompx;
355199899Sbz	int error, ralen, maxpacketsize;
356105197Ssam	struct cryptodesc *crdc;
357105197Ssam	struct cryptop *crp;
358105197Ssam	struct tdb_crypto *tc;
359105197Ssam
360105197Ssam	sav = isr->sav;
361120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
362105197Ssam	ipcompx = sav->tdb_compalgxform;
363120585Ssam	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
364105197Ssam
365199896Sbz	/*
366199896Sbz	 * Do not touch the packet in case our payload to compress
367199896Sbz	 * is lower than the minimal threshold of the compression
368199896Sbz	 * alogrithm.  We will just send out the data uncompressed.
369199896Sbz	 * See RFC 3173, 2.2. Non-Expansion Policy.
370199896Sbz	 */
371199896Sbz	if (m->m_pkthdr.len <= ipcompx->minlen) {
372252028Sae		IPCOMPSTAT_INC(ipcomps_threshold);
373199896Sbz		return ipsec_process_done(m, isr);
374199896Sbz	}
375199896Sbz
376105197Ssam	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
377252028Sae	IPCOMPSTAT_INC(ipcomps_output);
378105197Ssam
379105197Ssam	/* Check for maximum packet size violations. */
380105197Ssam	switch (sav->sah->saidx.dst.sa.sa_family) {
381105197Ssam#ifdef INET
382105197Ssam	case AF_INET:
383199897Sbz		maxpacketsize = IP_MAXPACKET;
384105197Ssam		break;
385105197Ssam#endif /* INET */
386105197Ssam#ifdef INET6
387105197Ssam	case AF_INET6:
388199897Sbz		maxpacketsize = IPV6_MAXPACKET;
389105197Ssam		break;
390105197Ssam#endif /* INET6 */
391105197Ssam	default:
392252028Sae		IPCOMPSTAT_INC(ipcomps_nopf);
393120585Ssam		DPRINTF(("%s: unknown/unsupported protocol family %d, "
394120585Ssam		    "IPCA %s/%08lx\n", __func__,
395105197Ssam		    sav->sah->saidx.dst.sa.sa_family,
396105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
397105197Ssam		    (u_long) ntohl(sav->spi)));
398105197Ssam		error = EPFNOSUPPORT;
399105197Ssam		goto bad;
400105197Ssam	}
401199899Sbz	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
402252028Sae		IPCOMPSTAT_INC(ipcomps_toobig);
403120585Ssam		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
404120585Ssam		    "(len %u, max len %u)\n", __func__,
405105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
406105197Ssam		    (u_long) ntohl(sav->spi),
407199899Sbz		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
408105197Ssam		error = EMSGSIZE;
409105197Ssam		goto bad;
410105197Ssam	}
411105197Ssam
412105197Ssam	/* Update the counters */
413252028Sae	IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
414105197Ssam
415156756Ssam	m = m_unshare(m, M_NOWAIT);
416105197Ssam	if (m == NULL) {
417252028Sae		IPCOMPSTAT_INC(ipcomps_hdrops);
418120585Ssam		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
419120585Ssam		    __func__, ipsec_address(&sav->sah->saidx.dst),
420105197Ssam		    (u_long) ntohl(sav->spi)));
421105197Ssam		error = ENOBUFS;
422105197Ssam		goto bad;
423105197Ssam	}
424105197Ssam
425199899Sbz	/* Ok now, we can pass to the crypto processing. */
426105197Ssam
427105197Ssam	/* Get crypto descriptors */
428105197Ssam	crp = crypto_getreq(1);
429105197Ssam	if (crp == NULL) {
430252028Sae		IPCOMPSTAT_INC(ipcomps_crypto);
431120585Ssam		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
432105197Ssam		error = ENOBUFS;
433105197Ssam		goto bad;
434105197Ssam	}
435105197Ssam	crdc = crp->crp_desc;
436105197Ssam
437105197Ssam	/* Compression descriptor */
438199899Sbz	crdc->crd_skip = skip;
439199899Sbz	crdc->crd_len = ralen;
440105197Ssam	crdc->crd_flags = CRD_F_COMP;
441199899Sbz	crdc->crd_inject = skip;
442105197Ssam
443105197Ssam	/* Compression operation */
444105197Ssam	crdc->crd_alg = ipcompx->type;
445105197Ssam
446105197Ssam	/* IPsec-specific opaque crypto info */
447105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
448105197Ssam		M_XDATA, M_NOWAIT|M_ZERO);
449105197Ssam	if (tc == NULL) {
450252028Sae		IPCOMPSTAT_INC(ipcomps_crypto);
451120585Ssam		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
452105197Ssam		crypto_freereq(crp);
453105197Ssam		error = ENOBUFS;
454105197Ssam		goto bad;
455105197Ssam	}
456105197Ssam
457105197Ssam	tc->tc_isr = isr;
458220206Sfabient	KEY_ADDREFSA(sav);
459220206Sfabient	tc->tc_sav = sav;
460105197Ssam	tc->tc_spi = sav->spi;
461105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
462105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
463199899Sbz	tc->tc_protoff = protoff;
464199899Sbz	tc->tc_skip = skip;
465105197Ssam
466105197Ssam	/* Crypto operation descriptor */
467105197Ssam	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
468117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
469105197Ssam	crp->crp_buf = (caddr_t) m;
470105197Ssam	crp->crp_callback = ipcomp_output_cb;
471105197Ssam	crp->crp_opaque = (caddr_t) tc;
472105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
473105197Ssam
474105197Ssam	return crypto_dispatch(crp);
475105197Ssambad:
476105197Ssam	if (m)
477105197Ssam		m_freem(m);
478105197Ssam	return (error);
479105197Ssam}
480105197Ssam
481105197Ssam/*
482105197Ssam * IPComp output callback from the crypto driver.
483105197Ssam */
484105197Ssamstatic int
485105197Ssamipcomp_output_cb(struct cryptop *crp)
486105197Ssam{
487105197Ssam	struct tdb_crypto *tc;
488105197Ssam	struct ipsecrequest *isr;
489105197Ssam	struct secasvar *sav;
490105197Ssam	struct mbuf *m;
491199899Sbz	int error, skip;
492105197Ssam
493105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
494120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
495105197Ssam	m = (struct mbuf *) crp->crp_buf;
496105197Ssam	skip = tc->tc_skip;
497105197Ssam
498105197Ssam	isr = tc->tc_isr;
499120585Ssam	IPSECREQUEST_LOCK(isr);
500220206Sfabient	sav = tc->tc_sav;
501220206Sfabient	/* With the isr lock released SA pointer can be updated. */
502220206Sfabient	if (sav != isr->sav) {
503252028Sae		IPCOMPSTAT_INC(ipcomps_notdb);
504120585Ssam		DPRINTF(("%s: SA expired while in crypto\n", __func__));
505105197Ssam		error = ENOBUFS;		/*XXX*/
506105197Ssam		goto bad;
507105197Ssam	}
508105197Ssam
509105197Ssam	/* Check for crypto errors */
510105197Ssam	if (crp->crp_etype) {
511199905Sbz		/* Reset the session ID */
512105197Ssam		if (sav->tdb_cryptoid != 0)
513105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
514105197Ssam
515105197Ssam		if (crp->crp_etype == EAGAIN) {
516120585Ssam			IPSECREQUEST_UNLOCK(isr);
517199899Sbz			return crypto_dispatch(crp);
518105197Ssam		}
519252028Sae		IPCOMPSTAT_INC(ipcomps_noxform);
520120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
521105197Ssam		error = crp->crp_etype;
522105197Ssam		goto bad;
523105197Ssam	}
524105197Ssam	/* Shouldn't happen... */
525105197Ssam	if (m == NULL) {
526252028Sae		IPCOMPSTAT_INC(ipcomps_crypto);
527120585Ssam		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
528105197Ssam		error = EINVAL;
529105197Ssam		goto bad;
530105197Ssam	}
531252028Sae	IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
532105197Ssam
533199899Sbz	if (crp->crp_ilen - skip > crp->crp_olen) {
534199899Sbz		struct mbuf *mo;
535199899Sbz		struct ipcomp *ipcomp;
536199899Sbz		int roff;
537199899Sbz		uint8_t prot;
538199899Sbz
539199899Sbz		/* Compression helped, inject IPCOMP header. */
540199899Sbz		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
541199899Sbz		if (mo == NULL) {
542252028Sae			IPCOMPSTAT_INC(ipcomps_wrap);
543199899Sbz			DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
544199899Sbz			    __func__, ipsec_address(&sav->sah->saidx.dst),
545199899Sbz			    (u_long) ntohl(sav->spi)));
546199899Sbz			error = ENOBUFS;
547199899Sbz			goto bad;
548199899Sbz		}
549199899Sbz		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
550199899Sbz
551199899Sbz		/* Initialize the IPCOMP header */
552199899Sbz		/* XXX alignment always correct? */
553199899Sbz		switch (sav->sah->saidx.dst.sa.sa_family) {
554199899Sbz#ifdef INET
555199899Sbz		case AF_INET:
556199899Sbz			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
557199899Sbz			break;
558199899Sbz#endif /* INET */
559199899Sbz#ifdef INET6
560199899Sbz		case AF_INET6:
561199899Sbz			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
562199899Sbz			break;
563199899Sbz#endif
564199899Sbz		}
565199899Sbz		ipcomp->comp_flags = 0;
566199899Sbz		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
567199899Sbz
568199899Sbz		/* Fix Next Protocol in IPv4/IPv6 header */
569199899Sbz		prot = IPPROTO_IPCOMP;
570199899Sbz		m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
571199899Sbz		    (u_char *)&prot);
572199899Sbz
573105197Ssam		/* Adjust the length in the IP header */
574105197Ssam		switch (sav->sah->saidx.dst.sa.sa_family) {
575105197Ssam#ifdef INET
576105197Ssam		case AF_INET:
577105197Ssam			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
578105197Ssam			break;
579105197Ssam#endif /* INET */
580105197Ssam#ifdef INET6
581105197Ssam		case AF_INET6:
582105197Ssam			mtod(m, struct ip6_hdr *)->ip6_plen =
583105197Ssam				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
584105197Ssam			break;
585105197Ssam#endif /* INET6 */
586105197Ssam		default:
587252028Sae			IPCOMPSTAT_INC(ipcomps_nopf);
588120585Ssam			DPRINTF(("%s: unknown/unsupported protocol "
589120585Ssam			    "family %d, IPCA %s/%08lx\n", __func__,
590105197Ssam			    sav->sah->saidx.dst.sa.sa_family,
591199897Sbz			    ipsec_address(&sav->sah->saidx.dst),
592105197Ssam			    (u_long) ntohl(sav->spi)));
593105197Ssam			error = EPFNOSUPPORT;
594105197Ssam			goto bad;
595105197Ssam		}
596105197Ssam	} else {
597199946Sbz		/* Compression was useless, we have lost time. */
598252028Sae		IPCOMPSTAT_INC(ipcomps_uncompr);
599199946Sbz		DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
600199946Sbz		    __func__, crp->crp_ilen, skip, crp->crp_olen));
601199899Sbz		/* XXX remember state to not compress the next couple
602199899Sbz		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
603105197Ssam	}
604105197Ssam
605105197Ssam	/* Release the crypto descriptor */
606105197Ssam	free(tc, M_XDATA);
607105197Ssam	crypto_freereq(crp);
608105197Ssam
609105197Ssam	/* NB: m is reclaimed by ipsec_process_done. */
610105197Ssam	error = ipsec_process_done(m, isr);
611105197Ssam	KEY_FREESAV(&sav);
612120585Ssam	IPSECREQUEST_UNLOCK(isr);
613105197Ssam	return error;
614105197Ssambad:
615105197Ssam	if (sav)
616105197Ssam		KEY_FREESAV(&sav);
617120585Ssam	IPSECREQUEST_UNLOCK(isr);
618105197Ssam	if (m)
619105197Ssam		m_freem(m);
620105197Ssam	free(tc, M_XDATA);
621105197Ssam	crypto_freereq(crp);
622105197Ssam	return error;
623105197Ssam}
624105197Ssam
625105197Ssamstatic struct xformsw ipcomp_xformsw = {
626105197Ssam	XF_IPCOMP,		XFT_COMP,		"IPcomp",
627105197Ssam	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
628105197Ssam	ipcomp_output
629105197Ssam};
630105197Ssam
631105197Ssamstatic void
632105197Ssamipcomp_attach(void)
633105197Ssam{
634185088Szec
635105197Ssam	xform_register(&ipcomp_xformsw);
636105197Ssam}
637190787Szec
638125099SsamSYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
639