xform_ipcomp.c revision 199899
1/*	$FreeBSD: head/sys/netipsec/xform_ipcomp.c 199899 2009-11-29 10:53:34Z bz $	*/
2/* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3
4/*-
5 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *   notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *   notice, this list of conditions and the following disclaimer in the
15 *   documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *   derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* IP payload compression protocol (IPComp), see RFC 2393 */
32#include "opt_inet.h"
33#include "opt_inet6.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/mbuf.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/socket.h>
41#include <sys/kernel.h>
42#include <sys/protosw.h>
43#include <sys/sysctl.h>
44
45#include <netinet/in.h>
46#include <netinet/in_systm.h>
47#include <netinet/ip.h>
48#include <netinet/ip_var.h>
49
50#include <net/route.h>
51#include <net/vnet.h>
52
53#include <netipsec/ipsec.h>
54#include <netipsec/xform.h>
55
56#ifdef INET6
57#include <netinet/ip6.h>
58#include <netipsec/ipsec6.h>
59#endif
60
61#include <netipsec/ipcomp.h>
62#include <netipsec/ipcomp_var.h>
63
64#include <netipsec/key.h>
65#include <netipsec/key_debug.h>
66
67#include <opencrypto/cryptodev.h>
68#include <opencrypto/deflate.h>
69#include <opencrypto/xform.h>
70
71VNET_DEFINE(int, ipcomp_enable) = 0;
72VNET_DEFINE(struct ipcompstat, ipcompstat);
73
74SYSCTL_DECL(_net_inet_ipcomp);
75SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
76	ipcomp_enable,	CTLFLAG_RW,	&VNET_NAME(ipcomp_enable),	0, "");
77SYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
78	stats,		CTLFLAG_RD,	&VNET_NAME(ipcompstat),	ipcompstat, "");
79
80static int ipcomp_input_cb(struct cryptop *crp);
81static int ipcomp_output_cb(struct cryptop *crp);
82
83struct comp_algo *
84ipcomp_algorithm_lookup(int alg)
85{
86	if (alg >= IPCOMP_ALG_MAX)
87		return NULL;
88	switch (alg) {
89	case SADB_X_CALG_DEFLATE:
90		return &comp_algo_deflate;
91	}
92	return NULL;
93}
94
95/*
96 * ipcomp_init() is called when an CPI is being set up.
97 */
98static int
99ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
100{
101	struct comp_algo *tcomp;
102	struct cryptoini cric;
103
104	/* NB: algorithm really comes in alg_enc and not alg_comp! */
105	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
106	if (tcomp == NULL) {
107		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
108			 sav->alg_comp));
109		return EINVAL;
110	}
111	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
112	sav->tdb_xform = xsp;
113	sav->tdb_compalgxform = tcomp;
114
115	/* Initialize crypto session */
116	bzero(&cric, sizeof (cric));
117	cric.cri_alg = sav->tdb_compalgxform->type;
118
119	return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
120}
121
122/*
123 * ipcomp_zeroize() used when IPCA is deleted
124 */
125static int
126ipcomp_zeroize(struct secasvar *sav)
127{
128	int err;
129
130	err = crypto_freesession(sav->tdb_cryptoid);
131	sav->tdb_cryptoid = 0;
132	return err;
133}
134
135/*
136 * ipcomp_input() gets called to uncompress an input packet
137 */
138static int
139ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
140{
141	struct tdb_crypto *tc;
142	struct cryptodesc *crdc;
143	struct cryptop *crp;
144	int hlen = IPCOMP_HLENGTH;
145
146	/* Get crypto descriptors */
147	crp = crypto_getreq(1);
148	if (crp == NULL) {
149		m_freem(m);
150		DPRINTF(("%s: no crypto descriptors\n", __func__));
151		V_ipcompstat.ipcomps_crypto++;
152		return ENOBUFS;
153	}
154	/* Get IPsec-specific opaque pointer */
155	tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
156	if (tc == NULL) {
157		m_freem(m);
158		crypto_freereq(crp);
159		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
160		V_ipcompstat.ipcomps_crypto++;
161		return ENOBUFS;
162	}
163	crdc = crp->crp_desc;
164
165	crdc->crd_skip = skip + hlen;
166	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
167	crdc->crd_inject = skip;
168
169	tc->tc_ptr = 0;
170
171	/* Decompression operation */
172	crdc->crd_alg = sav->tdb_compalgxform->type;
173
174	/* Crypto operation descriptor */
175	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
176	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
177	crp->crp_buf = (caddr_t) m;
178	crp->crp_callback = ipcomp_input_cb;
179	crp->crp_sid = sav->tdb_cryptoid;
180	crp->crp_opaque = (caddr_t) tc;
181
182	/* These are passed as-is to the callback */
183	tc->tc_spi = sav->spi;
184	tc->tc_dst = sav->sah->saidx.dst;
185	tc->tc_proto = sav->sah->saidx.proto;
186	tc->tc_protoff = protoff;
187	tc->tc_skip = skip;
188
189	return crypto_dispatch(crp);
190}
191
192#ifdef INET6
193#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
194	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
195		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
196	} else {							     \
197		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
198	}								     \
199} while (0)
200#else
201#define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
202	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
203#endif
204
205/*
206 * IPComp input callback from the crypto driver.
207 */
208static int
209ipcomp_input_cb(struct cryptop *crp)
210{
211	struct cryptodesc *crd;
212	struct tdb_crypto *tc;
213	int skip, protoff;
214	struct mtag *mtag;
215	struct mbuf *m;
216	struct secasvar *sav;
217	struct secasindex *saidx;
218	int hlen = IPCOMP_HLENGTH, error, clen;
219	u_int8_t nproto;
220	caddr_t addr;
221
222	crd = crp->crp_desc;
223
224	tc = (struct tdb_crypto *) crp->crp_opaque;
225	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
226	skip = tc->tc_skip;
227	protoff = tc->tc_protoff;
228	mtag = (struct mtag *) tc->tc_ptr;
229	m = (struct mbuf *) crp->crp_buf;
230
231	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
232	if (sav == NULL) {
233		V_ipcompstat.ipcomps_notdb++;
234		DPRINTF(("%s: SA expired while in crypto\n", __func__));
235		error = ENOBUFS;		/*XXX*/
236		goto bad;
237	}
238
239	saidx = &sav->sah->saidx;
240	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
241		saidx->dst.sa.sa_family == AF_INET6,
242		("unexpected protocol family %u", saidx->dst.sa.sa_family));
243
244	/* Check for crypto errors */
245	if (crp->crp_etype) {
246		/* Reset the session ID */
247		if (sav->tdb_cryptoid != 0)
248			sav->tdb_cryptoid = crp->crp_sid;
249
250		if (crp->crp_etype == EAGAIN) {
251			KEY_FREESAV(&sav);
252			error = crypto_dispatch(crp);
253			return error;
254		}
255
256		V_ipcompstat.ipcomps_noxform++;
257		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
258		error = crp->crp_etype;
259		goto bad;
260	}
261	/* Shouldn't happen... */
262	if (m == NULL) {
263		V_ipcompstat.ipcomps_crypto++;
264		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
265		error = EINVAL;
266		goto bad;
267	}
268	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
269
270	clen = crp->crp_olen;		/* Length of data after processing */
271
272	/* Release the crypto descriptors */
273	free(tc, M_XDATA), tc = NULL;
274	crypto_freereq(crp), crp = NULL;
275
276	/* In case it's not done already, adjust the size of the mbuf chain */
277	m->m_pkthdr.len = clen + hlen + skip;
278
279	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
280		V_ipcompstat.ipcomps_hdrops++;		/*XXX*/
281		DPRINTF(("%s: m_pullup failed\n", __func__));
282		error = EINVAL;				/*XXX*/
283		goto bad;
284	}
285
286	/* Keep the next protocol field */
287	addr = (caddr_t) mtod(m, struct ip *) + skip;
288	nproto = ((struct ipcomp *) addr)->comp_nxt;
289
290	/* Remove the IPCOMP header */
291	error = m_striphdr(m, skip, hlen);
292	if (error) {
293		V_ipcompstat.ipcomps_hdrops++;
294		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
295			 ipsec_address(&sav->sah->saidx.dst),
296			 (u_long) ntohl(sav->spi)));
297		goto bad;
298	}
299
300	/* Restore the Next Protocol field */
301	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
302
303	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
304
305	KEY_FREESAV(&sav);
306	return error;
307bad:
308	if (sav)
309		KEY_FREESAV(&sav);
310	if (m)
311		m_freem(m);
312	if (tc != NULL)
313		free(tc, M_XDATA);
314	if (crp)
315		crypto_freereq(crp);
316	return error;
317}
318
319/*
320 * IPComp output routine, called by ipsec[46]_process_packet()
321 */
322static int
323ipcomp_output(
324	struct mbuf *m,
325	struct ipsecrequest *isr,
326	struct mbuf **mp,
327	int skip,
328	int protoff
329)
330{
331	struct secasvar *sav;
332	struct comp_algo *ipcompx;
333	int error, ralen, maxpacketsize;
334	struct cryptodesc *crdc;
335	struct cryptop *crp;
336	struct tdb_crypto *tc;
337
338	sav = isr->sav;
339	IPSEC_ASSERT(sav != NULL, ("null SA"));
340	ipcompx = sav->tdb_compalgxform;
341	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
342
343	/*
344	 * Do not touch the packet in case our payload to compress
345	 * is lower than the minimal threshold of the compression
346	 * alogrithm.  We will just send out the data uncompressed.
347	 * See RFC 3173, 2.2. Non-Expansion Policy.
348	 */
349	if (m->m_pkthdr.len <= ipcompx->minlen) {
350		/* XXX-BZ V_ipcompstat.threshold++; */
351		return ipsec_process_done(m, isr);
352	}
353
354	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
355	V_ipcompstat.ipcomps_output++;
356
357	/* Check for maximum packet size violations. */
358	switch (sav->sah->saidx.dst.sa.sa_family) {
359#ifdef INET
360	case AF_INET:
361		maxpacketsize = IP_MAXPACKET;
362		break;
363#endif /* INET */
364#ifdef INET6
365	case AF_INET6:
366		maxpacketsize = IPV6_MAXPACKET;
367		break;
368#endif /* INET6 */
369	default:
370		V_ipcompstat.ipcomps_nopf++;
371		DPRINTF(("%s: unknown/unsupported protocol family %d, "
372		    "IPCA %s/%08lx\n", __func__,
373		    sav->sah->saidx.dst.sa.sa_family,
374		    ipsec_address(&sav->sah->saidx.dst),
375		    (u_long) ntohl(sav->spi)));
376		error = EPFNOSUPPORT;
377		goto bad;
378	}
379	if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
380		V_ipcompstat.ipcomps_toobig++;
381		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
382		    "(len %u, max len %u)\n", __func__,
383		    ipsec_address(&sav->sah->saidx.dst),
384		    (u_long) ntohl(sav->spi),
385		    ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
386		error = EMSGSIZE;
387		goto bad;
388	}
389
390	/* Update the counters */
391	V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
392
393	m = m_unshare(m, M_NOWAIT);
394	if (m == NULL) {
395		V_ipcompstat.ipcomps_hdrops++;
396		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
397		    __func__, ipsec_address(&sav->sah->saidx.dst),
398		    (u_long) ntohl(sav->spi)));
399		error = ENOBUFS;
400		goto bad;
401	}
402
403	/* Ok now, we can pass to the crypto processing. */
404
405	/* Get crypto descriptors */
406	crp = crypto_getreq(1);
407	if (crp == NULL) {
408		V_ipcompstat.ipcomps_crypto++;
409		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
410		error = ENOBUFS;
411		goto bad;
412	}
413	crdc = crp->crp_desc;
414
415	/* Compression descriptor */
416	crdc->crd_skip = skip;
417	crdc->crd_len = ralen;
418	crdc->crd_flags = CRD_F_COMP;
419	crdc->crd_inject = skip;
420
421	/* Compression operation */
422	crdc->crd_alg = ipcompx->type;
423
424	/* IPsec-specific opaque crypto info */
425	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
426		M_XDATA, M_NOWAIT|M_ZERO);
427	if (tc == NULL) {
428		V_ipcompstat.ipcomps_crypto++;
429		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
430		crypto_freereq(crp);
431		error = ENOBUFS;
432		goto bad;
433	}
434
435	tc->tc_isr = isr;
436	tc->tc_spi = sav->spi;
437	tc->tc_dst = sav->sah->saidx.dst;
438	tc->tc_proto = sav->sah->saidx.proto;
439	tc->tc_protoff = protoff;
440	tc->tc_skip = skip;
441
442	/* Crypto operation descriptor */
443	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
444	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
445	crp->crp_buf = (caddr_t) m;
446	crp->crp_callback = ipcomp_output_cb;
447	crp->crp_opaque = (caddr_t) tc;
448	crp->crp_sid = sav->tdb_cryptoid;
449
450	return crypto_dispatch(crp);
451bad:
452	if (m)
453		m_freem(m);
454	return (error);
455}
456
457/*
458 * IPComp output callback from the crypto driver.
459 */
460static int
461ipcomp_output_cb(struct cryptop *crp)
462{
463	struct tdb_crypto *tc;
464	struct ipsecrequest *isr;
465	struct secasvar *sav;
466	struct mbuf *m;
467	int error, skip;
468
469	tc = (struct tdb_crypto *) crp->crp_opaque;
470	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
471	m = (struct mbuf *) crp->crp_buf;
472	skip = tc->tc_skip;
473
474	isr = tc->tc_isr;
475	IPSECREQUEST_LOCK(isr);
476	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
477	if (sav == NULL) {
478		V_ipcompstat.ipcomps_notdb++;
479		DPRINTF(("%s: SA expired while in crypto\n", __func__));
480		error = ENOBUFS;		/*XXX*/
481		goto bad;
482	}
483	IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
484
485	/* Check for crypto errors */
486	if (crp->crp_etype) {
487		/* Reset session ID */
488		if (sav->tdb_cryptoid != 0)
489			sav->tdb_cryptoid = crp->crp_sid;
490
491		if (crp->crp_etype == EAGAIN) {
492			KEY_FREESAV(&sav);
493			IPSECREQUEST_UNLOCK(isr);
494			return crypto_dispatch(crp);
495		}
496		V_ipcompstat.ipcomps_noxform++;
497		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
498		error = crp->crp_etype;
499		goto bad;
500	}
501	/* Shouldn't happen... */
502	if (m == NULL) {
503		V_ipcompstat.ipcomps_crypto++;
504		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
505		error = EINVAL;
506		goto bad;
507	}
508	V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
509
510	if (crp->crp_ilen - skip > crp->crp_olen) {
511		struct mbuf *mo;
512		struct ipcomp *ipcomp;
513		int roff;
514		uint8_t prot;
515
516		/* Compression helped, inject IPCOMP header. */
517		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
518		if (mo == NULL) {
519			V_ipcompstat.ipcomps_wrap++;
520			DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
521			    __func__, ipsec_address(&sav->sah->saidx.dst),
522			    (u_long) ntohl(sav->spi)));
523			error = ENOBUFS;
524			goto bad;
525		}
526		ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
527
528		/* Initialize the IPCOMP header */
529		/* XXX alignment always correct? */
530		switch (sav->sah->saidx.dst.sa.sa_family) {
531#ifdef INET
532		case AF_INET:
533			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
534			break;
535#endif /* INET */
536#ifdef INET6
537		case AF_INET6:
538			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
539			break;
540#endif
541		}
542		ipcomp->comp_flags = 0;
543		ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
544
545		/* Fix Next Protocol in IPv4/IPv6 header */
546		prot = IPPROTO_IPCOMP;
547		m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
548		    (u_char *)&prot);
549
550		/* Adjust the length in the IP header */
551		switch (sav->sah->saidx.dst.sa.sa_family) {
552#ifdef INET
553		case AF_INET:
554			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
555			break;
556#endif /* INET */
557#ifdef INET6
558		case AF_INET6:
559			mtod(m, struct ip6_hdr *)->ip6_plen =
560				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
561			break;
562#endif /* INET6 */
563		default:
564			V_ipcompstat.ipcomps_nopf++;
565			DPRINTF(("%s: unknown/unsupported protocol "
566			    "family %d, IPCA %s/%08lx\n", __func__,
567			    sav->sah->saidx.dst.sa.sa_family,
568			    ipsec_address(&sav->sah->saidx.dst),
569			    (u_long) ntohl(sav->spi)));
570			error = EPFNOSUPPORT;
571			goto bad;
572		}
573	} else {
574		/* compression was useless, we have lost time */
575		/* XXX add statistic */
576		/* XXX remember state to not compress the next couple
577		 *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
578	}
579
580	/* Release the crypto descriptor */
581	free(tc, M_XDATA);
582	crypto_freereq(crp);
583
584	/* NB: m is reclaimed by ipsec_process_done. */
585	error = ipsec_process_done(m, isr);
586	KEY_FREESAV(&sav);
587	IPSECREQUEST_UNLOCK(isr);
588	return error;
589bad:
590	if (sav)
591		KEY_FREESAV(&sav);
592	IPSECREQUEST_UNLOCK(isr);
593	if (m)
594		m_freem(m);
595	free(tc, M_XDATA);
596	crypto_freereq(crp);
597	return error;
598}
599
600static struct xformsw ipcomp_xformsw = {
601	XF_IPCOMP,		XFT_COMP,		"IPcomp",
602	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
603	ipcomp_output
604};
605
606static void
607ipcomp_attach(void)
608{
609
610	xform_register(&ipcomp_xformsw);
611}
612
613SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
614