xform_ipcomp.c revision 199947
1105197Ssam/*	$FreeBSD: head/sys/netipsec/xform_ipcomp.c 199947 2009-11-29 20:47:43Z bz $	*/
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>
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;
72195699SrwatsonVNET_DEFINE(struct ipcompstat, ipcompstat);
73105197Ssam
74105197SsamSYSCTL_DECL(_net_inet_ipcomp);
75195699SrwatsonSYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
76195699Srwatson	ipcomp_enable,	CTLFLAG_RW,	&VNET_NAME(ipcomp_enable),	0, "");
77195699SrwatsonSYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
78195699Srwatson	stats,		CTLFLAG_RD,	&VNET_NAME(ipcompstat),	ipcompstat, "");
79105197Ssam
80105197Ssamstatic int ipcomp_input_cb(struct cryptop *crp);
81105197Ssamstatic int ipcomp_output_cb(struct cryptop *crp);
82105197Ssam
83105197Ssamstruct comp_algo *
84105197Ssamipcomp_algorithm_lookup(int alg)
85105197Ssam{
86105197Ssam	if (alg >= IPCOMP_ALG_MAX)
87105197Ssam		return NULL;
88105197Ssam	switch (alg) {
89105197Ssam	case SADB_X_CALG_DEFLATE:
90105197Ssam		return &comp_algo_deflate;
91105197Ssam	}
92105197Ssam	return NULL;
93105197Ssam}
94105197Ssam
95105197Ssam/*
96105197Ssam * ipcomp_init() is called when an CPI is being set up.
97105197Ssam */
98105197Ssamstatic int
99105197Ssamipcomp_init(struct secasvar *sav, struct xformsw *xsp)
100105197Ssam{
101105197Ssam	struct comp_algo *tcomp;
102105197Ssam	struct cryptoini cric;
103105197Ssam
104105197Ssam	/* NB: algorithm really comes in alg_enc and not alg_comp! */
105105197Ssam	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
106105197Ssam	if (tcomp == NULL) {
107120585Ssam		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
108105197Ssam			 sav->alg_comp));
109105197Ssam		return EINVAL;
110105197Ssam	}
111105197Ssam	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
112105197Ssam	sav->tdb_xform = xsp;
113105197Ssam	sav->tdb_compalgxform = tcomp;
114105197Ssam
115105197Ssam	/* Initialize crypto session */
116105197Ssam	bzero(&cric, sizeof (cric));
117105197Ssam	cric.cri_alg = sav->tdb_compalgxform->type;
118105197Ssam
119181803Sbz	return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
120105197Ssam}
121105197Ssam
122105197Ssam/*
123105197Ssam * ipcomp_zeroize() used when IPCA is deleted
124105197Ssam */
125105197Ssamstatic int
126105197Ssamipcomp_zeroize(struct secasvar *sav)
127105197Ssam{
128105197Ssam	int err;
129105197Ssam
130105197Ssam	err = crypto_freesession(sav->tdb_cryptoid);
131105197Ssam	sav->tdb_cryptoid = 0;
132105197Ssam	return err;
133105197Ssam}
134105197Ssam
135105197Ssam/*
136105197Ssam * ipcomp_input() gets called to uncompress an input packet
137105197Ssam */
138105197Ssamstatic int
139105197Ssamipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
140105197Ssam{
141105197Ssam	struct tdb_crypto *tc;
142105197Ssam	struct cryptodesc *crdc;
143105197Ssam	struct cryptop *crp;
144105197Ssam	int hlen = IPCOMP_HLENGTH;
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__));
151181803Sbz		V_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__));
160181803Sbz		V_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) {
233181803Sbz		V_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);
252199905Sbz			return crypto_dispatch(crp);
253105197Ssam		}
254181803Sbz		V_ipcompstat.ipcomps_noxform++;
255120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
256105197Ssam		error = crp->crp_etype;
257105197Ssam		goto bad;
258105197Ssam	}
259105197Ssam	/* Shouldn't happen... */
260105197Ssam	if (m == NULL) {
261181803Sbz		V_ipcompstat.ipcomps_crypto++;
262120585Ssam		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
263105197Ssam		error = EINVAL;
264105197Ssam		goto bad;
265105197Ssam	}
266181803Sbz	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
267105197Ssam
268105197Ssam	clen = crp->crp_olen;		/* Length of data after processing */
269105197Ssam
270105197Ssam	/* Release the crypto descriptors */
271105197Ssam	free(tc, M_XDATA), tc = NULL;
272105197Ssam	crypto_freereq(crp), crp = NULL;
273105197Ssam
274105197Ssam	/* In case it's not done already, adjust the size of the mbuf chain */
275105197Ssam	m->m_pkthdr.len = clen + hlen + skip;
276105197Ssam
277105197Ssam	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
278181803Sbz		V_ipcompstat.ipcomps_hdrops++;		/*XXX*/
279120585Ssam		DPRINTF(("%s: m_pullup failed\n", __func__));
280105197Ssam		error = EINVAL;				/*XXX*/
281105197Ssam		goto bad;
282105197Ssam	}
283105197Ssam
284105197Ssam	/* Keep the next protocol field */
285105197Ssam	addr = (caddr_t) mtod(m, struct ip *) + skip;
286105197Ssam	nproto = ((struct ipcomp *) addr)->comp_nxt;
287105197Ssam
288105197Ssam	/* Remove the IPCOMP header */
289105197Ssam	error = m_striphdr(m, skip, hlen);
290105197Ssam	if (error) {
291181803Sbz		V_ipcompstat.ipcomps_hdrops++;
292120585Ssam		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
293105197Ssam			 ipsec_address(&sav->sah->saidx.dst),
294105197Ssam			 (u_long) ntohl(sav->spi)));
295105197Ssam		goto bad;
296105197Ssam	}
297105197Ssam
298105197Ssam	/* Restore the Next Protocol field */
299105197Ssam	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
300105197Ssam
301105197Ssam	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
302105197Ssam
303105197Ssam	KEY_FREESAV(&sav);
304105197Ssam	return error;
305105197Ssambad:
306105197Ssam	if (sav)
307105197Ssam		KEY_FREESAV(&sav);
308105197Ssam	if (m)
309105197Ssam		m_freem(m);
310105197Ssam	if (tc != NULL)
311105197Ssam		free(tc, M_XDATA);
312105197Ssam	if (crp)
313105197Ssam		crypto_freereq(crp);
314105197Ssam	return error;
315105197Ssam}
316105197Ssam
317105197Ssam/*
318105197Ssam * IPComp output routine, called by ipsec[46]_process_packet()
319105197Ssam */
320105197Ssamstatic int
321105197Ssamipcomp_output(
322105197Ssam	struct mbuf *m,
323105197Ssam	struct ipsecrequest *isr,
324105197Ssam	struct mbuf **mp,
325105197Ssam	int skip,
326105197Ssam	int protoff
327105197Ssam)
328105197Ssam{
329105197Ssam	struct secasvar *sav;
330105197Ssam	struct comp_algo *ipcompx;
331199899Sbz	int error, ralen, maxpacketsize;
332105197Ssam	struct cryptodesc *crdc;
333105197Ssam	struct cryptop *crp;
334105197Ssam	struct tdb_crypto *tc;
335105197Ssam
336105197Ssam	sav = isr->sav;
337120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
338105197Ssam	ipcompx = sav->tdb_compalgxform;
339120585Ssam	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
340105197Ssam
341199896Sbz	/*
342199896Sbz	 * Do not touch the packet in case our payload to compress
343199896Sbz	 * is lower than the minimal threshold of the compression
344199896Sbz	 * alogrithm.  We will just send out the data uncompressed.
345199896Sbz	 * See RFC 3173, 2.2. Non-Expansion Policy.
346199896Sbz	 */
347199896Sbz	if (m->m_pkthdr.len <= ipcompx->minlen) {
348199946Sbz		V_ipcompstat.ipcomps_threshold++;
349199896Sbz		return ipsec_process_done(m, isr);
350199896Sbz	}
351199896Sbz
352105197Ssam	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
353181803Sbz	V_ipcompstat.ipcomps_output++;
354105197Ssam
355105197Ssam	/* Check for maximum packet size violations. */
356105197Ssam	switch (sav->sah->saidx.dst.sa.sa_family) {
357105197Ssam#ifdef INET
358105197Ssam	case AF_INET:
359199897Sbz		maxpacketsize = IP_MAXPACKET;
360105197Ssam		break;
361105197Ssam#endif /* INET */
362105197Ssam#ifdef INET6
363105197Ssam	case AF_INET6:
364199897Sbz		maxpacketsize = IPV6_MAXPACKET;
365105197Ssam		break;
366105197Ssam#endif /* INET6 */
367105197Ssam	default:
368181803Sbz		V_ipcompstat.ipcomps_nopf++;
369120585Ssam		DPRINTF(("%s: unknown/unsupported protocol family %d, "
370120585Ssam		    "IPCA %s/%08lx\n", __func__,
371105197Ssam		    sav->sah->saidx.dst.sa.sa_family,
372105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
373105197Ssam		    (u_long) ntohl(sav->spi)));
374105197Ssam		error = EPFNOSUPPORT;
375105197Ssam		goto bad;
376105197Ssam	}
377199899Sbz	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
378181803Sbz		V_ipcompstat.ipcomps_toobig++;
379120585Ssam		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
380120585Ssam		    "(len %u, max len %u)\n", __func__,
381105197Ssam		    ipsec_address(&sav->sah->saidx.dst),
382105197Ssam		    (u_long) ntohl(sav->spi),
383199899Sbz		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
384105197Ssam		error = EMSGSIZE;
385105197Ssam		goto bad;
386105197Ssam	}
387105197Ssam
388105197Ssam	/* Update the counters */
389181803Sbz	V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
390105197Ssam
391156756Ssam	m = m_unshare(m, M_NOWAIT);
392105197Ssam	if (m == NULL) {
393181803Sbz		V_ipcompstat.ipcomps_hdrops++;
394120585Ssam		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
395120585Ssam		    __func__, ipsec_address(&sav->sah->saidx.dst),
396105197Ssam		    (u_long) ntohl(sav->spi)));
397105197Ssam		error = ENOBUFS;
398105197Ssam		goto bad;
399105197Ssam	}
400105197Ssam
401199899Sbz	/* Ok now, we can pass to the crypto processing. */
402105197Ssam
403105197Ssam	/* Get crypto descriptors */
404105197Ssam	crp = crypto_getreq(1);
405105197Ssam	if (crp == NULL) {
406181803Sbz		V_ipcompstat.ipcomps_crypto++;
407120585Ssam		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
408105197Ssam		error = ENOBUFS;
409105197Ssam		goto bad;
410105197Ssam	}
411105197Ssam	crdc = crp->crp_desc;
412105197Ssam
413105197Ssam	/* Compression descriptor */
414199899Sbz	crdc->crd_skip = skip;
415199899Sbz	crdc->crd_len = ralen;
416105197Ssam	crdc->crd_flags = CRD_F_COMP;
417199899Sbz	crdc->crd_inject = skip;
418105197Ssam
419105197Ssam	/* Compression operation */
420105197Ssam	crdc->crd_alg = ipcompx->type;
421105197Ssam
422105197Ssam	/* IPsec-specific opaque crypto info */
423105197Ssam	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
424105197Ssam		M_XDATA, M_NOWAIT|M_ZERO);
425105197Ssam	if (tc == NULL) {
426181803Sbz		V_ipcompstat.ipcomps_crypto++;
427120585Ssam		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
428105197Ssam		crypto_freereq(crp);
429105197Ssam		error = ENOBUFS;
430105197Ssam		goto bad;
431105197Ssam	}
432105197Ssam
433105197Ssam	tc->tc_isr = isr;
434105197Ssam	tc->tc_spi = sav->spi;
435105197Ssam	tc->tc_dst = sav->sah->saidx.dst;
436105197Ssam	tc->tc_proto = sav->sah->saidx.proto;
437199899Sbz	tc->tc_protoff = protoff;
438199899Sbz	tc->tc_skip = skip;
439105197Ssam
440105197Ssam	/* Crypto operation descriptor */
441105197Ssam	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
442117058Ssam	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
443105197Ssam	crp->crp_buf = (caddr_t) m;
444105197Ssam	crp->crp_callback = ipcomp_output_cb;
445105197Ssam	crp->crp_opaque = (caddr_t) tc;
446105197Ssam	crp->crp_sid = sav->tdb_cryptoid;
447105197Ssam
448105197Ssam	return crypto_dispatch(crp);
449105197Ssambad:
450105197Ssam	if (m)
451105197Ssam		m_freem(m);
452105197Ssam	return (error);
453105197Ssam}
454105197Ssam
455105197Ssam/*
456105197Ssam * IPComp output callback from the crypto driver.
457105197Ssam */
458105197Ssamstatic int
459105197Ssamipcomp_output_cb(struct cryptop *crp)
460105197Ssam{
461105197Ssam	struct tdb_crypto *tc;
462105197Ssam	struct ipsecrequest *isr;
463105197Ssam	struct secasvar *sav;
464105197Ssam	struct mbuf *m;
465199899Sbz	int error, skip;
466105197Ssam
467105197Ssam	tc = (struct tdb_crypto *) crp->crp_opaque;
468120585Ssam	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
469105197Ssam	m = (struct mbuf *) crp->crp_buf;
470105197Ssam	skip = tc->tc_skip;
471105197Ssam
472105197Ssam	isr = tc->tc_isr;
473120585Ssam	IPSECREQUEST_LOCK(isr);
474105197Ssam	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
475105197Ssam	if (sav == NULL) {
476181803Sbz		V_ipcompstat.ipcomps_notdb++;
477120585Ssam		DPRINTF(("%s: SA expired while in crypto\n", __func__));
478105197Ssam		error = ENOBUFS;		/*XXX*/
479105197Ssam		goto bad;
480105197Ssam	}
481120585Ssam	IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
482105197Ssam
483105197Ssam	/* Check for crypto errors */
484105197Ssam	if (crp->crp_etype) {
485199905Sbz		/* Reset the session ID */
486105197Ssam		if (sav->tdb_cryptoid != 0)
487105197Ssam			sav->tdb_cryptoid = crp->crp_sid;
488105197Ssam
489105197Ssam		if (crp->crp_etype == EAGAIN) {
490105197Ssam			KEY_FREESAV(&sav);
491120585Ssam			IPSECREQUEST_UNLOCK(isr);
492199899Sbz			return crypto_dispatch(crp);
493105197Ssam		}
494181803Sbz		V_ipcompstat.ipcomps_noxform++;
495120585Ssam		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
496105197Ssam		error = crp->crp_etype;
497105197Ssam		goto bad;
498105197Ssam	}
499105197Ssam	/* Shouldn't happen... */
500105197Ssam	if (m == NULL) {
501181803Sbz		V_ipcompstat.ipcomps_crypto++;
502120585Ssam		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
503105197Ssam		error = EINVAL;
504105197Ssam		goto bad;
505105197Ssam	}
506181803Sbz	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
507105197Ssam
508199899Sbz	if (crp->crp_ilen - skip > crp->crp_olen) {
509199899Sbz		struct mbuf *mo;
510199899Sbz		struct ipcomp *ipcomp;
511199899Sbz		int roff;
512199899Sbz		uint8_t prot;
513199899Sbz
514199899Sbz		/* Compression helped, inject IPCOMP header. */
515199899Sbz		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
516199899Sbz		if (mo == NULL) {
517199899Sbz			V_ipcompstat.ipcomps_wrap++;
518199899Sbz			DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
519199899Sbz			    __func__, ipsec_address(&sav->sah->saidx.dst),
520199899Sbz			    (u_long) ntohl(sav->spi)));
521199899Sbz			error = ENOBUFS;
522199899Sbz			goto bad;
523199899Sbz		}
524199899Sbz		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
525199899Sbz
526199899Sbz		/* Initialize the IPCOMP header */
527199899Sbz		/* XXX alignment always correct? */
528199899Sbz		switch (sav->sah->saidx.dst.sa.sa_family) {
529199899Sbz#ifdef INET
530199899Sbz		case AF_INET:
531199899Sbz			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
532199899Sbz			break;
533199899Sbz#endif /* INET */
534199899Sbz#ifdef INET6
535199899Sbz		case AF_INET6:
536199899Sbz			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
537199899Sbz			break;
538199899Sbz#endif
539199899Sbz		}
540199899Sbz		ipcomp->comp_flags = 0;
541199899Sbz		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
542199899Sbz
543199899Sbz		/* Fix Next Protocol in IPv4/IPv6 header */
544199899Sbz		prot = IPPROTO_IPCOMP;
545199899Sbz		m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
546199899Sbz		    (u_char *)&prot);
547199899Sbz
548105197Ssam		/* Adjust the length in the IP header */
549105197Ssam		switch (sav->sah->saidx.dst.sa.sa_family) {
550105197Ssam#ifdef INET
551105197Ssam		case AF_INET:
552105197Ssam			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
553105197Ssam			break;
554105197Ssam#endif /* INET */
555105197Ssam#ifdef INET6
556105197Ssam		case AF_INET6:
557105197Ssam			mtod(m, struct ip6_hdr *)->ip6_plen =
558105197Ssam				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
559105197Ssam			break;
560105197Ssam#endif /* INET6 */
561105197Ssam		default:
562181803Sbz			V_ipcompstat.ipcomps_nopf++;
563120585Ssam			DPRINTF(("%s: unknown/unsupported protocol "
564120585Ssam			    "family %d, IPCA %s/%08lx\n", __func__,
565105197Ssam			    sav->sah->saidx.dst.sa.sa_family,
566199897Sbz			    ipsec_address(&sav->sah->saidx.dst),
567105197Ssam			    (u_long) ntohl(sav->spi)));
568105197Ssam			error = EPFNOSUPPORT;
569105197Ssam			goto bad;
570105197Ssam		}
571105197Ssam	} else {
572199946Sbz		/* Compression was useless, we have lost time. */
573199946Sbz		V_ipcompstat.ipcomps_uncompr++;
574199946Sbz		DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
575199946Sbz		    __func__, crp->crp_ilen, skip, crp->crp_olen));
576199899Sbz		/* XXX remember state to not compress the next couple
577199899Sbz		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
578105197Ssam	}
579105197Ssam
580105197Ssam	/* Release the crypto descriptor */
581105197Ssam	free(tc, M_XDATA);
582105197Ssam	crypto_freereq(crp);
583105197Ssam
584105197Ssam	/* NB: m is reclaimed by ipsec_process_done. */
585105197Ssam	error = ipsec_process_done(m, isr);
586105197Ssam	KEY_FREESAV(&sav);
587120585Ssam	IPSECREQUEST_UNLOCK(isr);
588105197Ssam	return error;
589105197Ssambad:
590105197Ssam	if (sav)
591105197Ssam		KEY_FREESAV(&sav);
592120585Ssam	IPSECREQUEST_UNLOCK(isr);
593105197Ssam	if (m)
594105197Ssam		m_freem(m);
595105197Ssam	free(tc, M_XDATA);
596105197Ssam	crypto_freereq(crp);
597105197Ssam	return error;
598105197Ssam}
599105197Ssam
600105197Ssamstatic struct xformsw ipcomp_xformsw = {
601105197Ssam	XF_IPCOMP,		XFT_COMP,		"IPcomp",
602105197Ssam	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
603105197Ssam	ipcomp_output
604105197Ssam};
605105197Ssam
606105197Ssamstatic void
607105197Ssamipcomp_attach(void)
608105197Ssam{
609185088Szec
610105197Ssam	xform_register(&ipcomp_xformsw);
611105197Ssam}
612190787Szec
613125099SsamSYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
614199946Sbz
615199946Sbzstatic void
616199946Sbzvnet_ipcomp_attach(const void *unused __unused)
617199946Sbz{
618199946Sbz
619199946Sbz	V_ipcompstat.version = IPCOMPSTAT_VERSION;
620199946Sbz}
621199946Sbz
622199946SbzVNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
623199946Sbz    vnet_ipcomp_attach, NULL);
624