xform_ipcomp.c revision 156756
1/*	$FreeBSD: head/sys/netipsec/xform_ipcomp.c 156756 2006-03-15 21:11:11Z sam $	*/
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 <netipsec/ipsec.h>
52#include <netipsec/xform.h>
53
54#ifdef INET6
55#include <netinet/ip6.h>
56#include <netipsec/ipsec6.h>
57#endif
58
59#include <netipsec/ipcomp.h>
60#include <netipsec/ipcomp_var.h>
61
62#include <netipsec/key.h>
63#include <netipsec/key_debug.h>
64
65#include <opencrypto/cryptodev.h>
66#include <opencrypto/deflate.h>
67#include <opencrypto/xform.h>
68
69int	ipcomp_enable = 0;
70struct	ipcompstat ipcompstat;
71
72SYSCTL_DECL(_net_inet_ipcomp);
73SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
74	ipcomp_enable,	CTLFLAG_RW,	&ipcomp_enable,	0, "");
75SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
76	stats,		CTLFLAG_RD,	&ipcompstat,	ipcompstat, "");
77
78static int ipcomp_input_cb(struct cryptop *crp);
79static int ipcomp_output_cb(struct cryptop *crp);
80
81struct comp_algo *
82ipcomp_algorithm_lookup(int alg)
83{
84	if (alg >= IPCOMP_ALG_MAX)
85		return NULL;
86	switch (alg) {
87	case SADB_X_CALG_DEFLATE:
88		return &comp_algo_deflate;
89	}
90	return NULL;
91}
92
93/*
94 * ipcomp_init() is called when an CPI is being set up.
95 */
96static int
97ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
98{
99	struct comp_algo *tcomp;
100	struct cryptoini cric;
101
102	/* NB: algorithm really comes in alg_enc and not alg_comp! */
103	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
104	if (tcomp == NULL) {
105		DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
106			 sav->alg_comp));
107		return EINVAL;
108	}
109	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
110	sav->tdb_xform = xsp;
111	sav->tdb_compalgxform = tcomp;
112
113	/* Initialize crypto session */
114	bzero(&cric, sizeof (cric));
115	cric.cri_alg = sav->tdb_compalgxform->type;
116
117	return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
118}
119
120/*
121 * ipcomp_zeroize() used when IPCA is deleted
122 */
123static int
124ipcomp_zeroize(struct secasvar *sav)
125{
126	int err;
127
128	err = crypto_freesession(sav->tdb_cryptoid);
129	sav->tdb_cryptoid = 0;
130	return err;
131}
132
133/*
134 * ipcomp_input() gets called to uncompress an input packet
135 */
136static int
137ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
138{
139	struct tdb_crypto *tc;
140	struct cryptodesc *crdc;
141	struct cryptop *crp;
142	int hlen = IPCOMP_HLENGTH;
143
144	IPSEC_SPLASSERT_SOFTNET(__func__);
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		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		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		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			return crypto_dispatch(crp);
253		}
254
255		ipcompstat.ipcomps_noxform++;
256		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
257		error = crp->crp_etype;
258		goto bad;
259	}
260	/* Shouldn't happen... */
261	if (m == NULL) {
262		ipcompstat.ipcomps_crypto++;
263		DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
264		error = EINVAL;
265		goto bad;
266	}
267	ipcompstat.ipcomps_hist[sav->alg_comp]++;
268
269	clen = crp->crp_olen;		/* Length of data after processing */
270
271	/* Release the crypto descriptors */
272	free(tc, M_XDATA), tc = NULL;
273	crypto_freereq(crp), crp = NULL;
274
275	/* In case it's not done already, adjust the size of the mbuf chain */
276	m->m_pkthdr.len = clen + hlen + skip;
277
278	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
279		ipcompstat.ipcomps_hdrops++;		/*XXX*/
280		DPRINTF(("%s: m_pullup failed\n", __func__));
281		error = EINVAL;				/*XXX*/
282		goto bad;
283	}
284
285	/* Keep the next protocol field */
286	addr = (caddr_t) mtod(m, struct ip *) + skip;
287	nproto = ((struct ipcomp *) addr)->comp_nxt;
288
289	/* Remove the IPCOMP header */
290	error = m_striphdr(m, skip, hlen);
291	if (error) {
292		ipcompstat.ipcomps_hdrops++;
293		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
294			 ipsec_address(&sav->sah->saidx.dst),
295			 (u_long) ntohl(sav->spi)));
296		goto bad;
297	}
298
299	/* Restore the Next Protocol field */
300	m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
301
302	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
303
304	KEY_FREESAV(&sav);
305	return error;
306bad:
307	if (sav)
308		KEY_FREESAV(&sav);
309	if (m)
310		m_freem(m);
311	if (tc != NULL)
312		free(tc, M_XDATA);
313	if (crp)
314		crypto_freereq(crp);
315	return error;
316}
317
318/*
319 * IPComp output routine, called by ipsec[46]_process_packet()
320 */
321static int
322ipcomp_output(
323	struct mbuf *m,
324	struct ipsecrequest *isr,
325	struct mbuf **mp,
326	int skip,
327	int protoff
328)
329{
330	struct secasvar *sav;
331	struct comp_algo *ipcompx;
332	int error, ralen, hlen, maxpacketsize, roff;
333	u_int8_t prot;
334	struct cryptodesc *crdc;
335	struct cryptop *crp;
336	struct tdb_crypto *tc;
337	struct mbuf *mo;
338	struct ipcomp *ipcomp;
339
340	IPSEC_SPLASSERT_SOFTNET(__func__);
341
342	sav = isr->sav;
343	IPSEC_ASSERT(sav != NULL, ("null SA"));
344	ipcompx = sav->tdb_compalgxform;
345	IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
346
347	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
348	hlen = IPCOMP_HLENGTH;
349
350	ipcompstat.ipcomps_output++;
351
352	/* Check for maximum packet size violations. */
353	switch (sav->sah->saidx.dst.sa.sa_family) {
354#ifdef INET
355	case AF_INET:
356		maxpacketsize =  IP_MAXPACKET;
357		break;
358#endif /* INET */
359#ifdef INET6
360	case AF_INET6:
361		maxpacketsize =  IPV6_MAXPACKET;
362		break;
363#endif /* INET6 */
364	default:
365		ipcompstat.ipcomps_nopf++;
366		DPRINTF(("%s: unknown/unsupported protocol family %d, "
367		    "IPCA %s/%08lx\n", __func__,
368		    sav->sah->saidx.dst.sa.sa_family,
369		    ipsec_address(&sav->sah->saidx.dst),
370		    (u_long) ntohl(sav->spi)));
371		error = EPFNOSUPPORT;
372		goto bad;
373	}
374	if (skip + hlen + ralen > maxpacketsize) {
375		ipcompstat.ipcomps_toobig++;
376		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
377		    "(len %u, max len %u)\n", __func__,
378		    ipsec_address(&sav->sah->saidx.dst),
379		    (u_long) ntohl(sav->spi),
380		    skip + hlen + ralen, maxpacketsize));
381		error = EMSGSIZE;
382		goto bad;
383	}
384
385	/* Update the counters */
386	ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
387
388	m = m_unshare(m, M_NOWAIT);
389	if (m == NULL) {
390		ipcompstat.ipcomps_hdrops++;
391		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
392		    __func__, ipsec_address(&sav->sah->saidx.dst),
393		    (u_long) ntohl(sav->spi)));
394		error = ENOBUFS;
395		goto bad;
396	}
397
398	/* Inject IPCOMP header */
399	mo = m_makespace(m, skip, hlen, &roff);
400	if (mo == NULL) {
401		ipcompstat.ipcomps_wrap++;
402		DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
403		    __func__, ipsec_address(&sav->sah->saidx.dst),
404		    (u_long) ntohl(sav->spi)));
405		error = ENOBUFS;
406		goto bad;
407	}
408	ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
409
410	/* Initialize the IPCOMP header */
411	/* XXX alignment always correct? */
412	switch (sav->sah->saidx.dst.sa.sa_family) {
413#ifdef INET
414	case AF_INET:
415		ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
416		break;
417#endif /* INET */
418#ifdef INET6
419	case AF_INET6:
420		ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
421		break;
422#endif
423	}
424	ipcomp->comp_flags = 0;
425	ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
426
427	/* Fix Next Protocol in IPv4/IPv6 header */
428	prot = IPPROTO_IPCOMP;
429	m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
430
431	/* Ok now, we can pass to the crypto processing */
432
433	/* Get crypto descriptors */
434	crp = crypto_getreq(1);
435	if (crp == NULL) {
436		ipcompstat.ipcomps_crypto++;
437		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
438		error = ENOBUFS;
439		goto bad;
440	}
441	crdc = crp->crp_desc;
442
443	/* Compression descriptor */
444	crdc->crd_skip = skip + hlen;
445	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
446	crdc->crd_flags = CRD_F_COMP;
447	crdc->crd_inject = skip + hlen;
448
449	/* Compression operation */
450	crdc->crd_alg = ipcompx->type;
451
452	/* IPsec-specific opaque crypto info */
453	tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
454		M_XDATA, M_NOWAIT|M_ZERO);
455	if (tc == NULL) {
456		ipcompstat.ipcomps_crypto++;
457		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
458		crypto_freereq(crp);
459		error = ENOBUFS;
460		goto bad;
461	}
462
463	tc->tc_isr = isr;
464	tc->tc_spi = sav->spi;
465	tc->tc_dst = sav->sah->saidx.dst;
466	tc->tc_proto = sav->sah->saidx.proto;
467	tc->tc_skip = skip + hlen;
468
469	/* Crypto operation descriptor */
470	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
471	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
472	crp->crp_buf = (caddr_t) m;
473	crp->crp_callback = ipcomp_output_cb;
474	crp->crp_opaque = (caddr_t) tc;
475	crp->crp_sid = sav->tdb_cryptoid;
476
477	return crypto_dispatch(crp);
478bad:
479	if (m)
480		m_freem(m);
481	return (error);
482}
483
484/*
485 * IPComp output callback from the crypto driver.
486 */
487static int
488ipcomp_output_cb(struct cryptop *crp)
489{
490	struct tdb_crypto *tc;
491	struct ipsecrequest *isr;
492	struct secasvar *sav;
493	struct mbuf *m;
494	int error, skip, rlen;
495
496	tc = (struct tdb_crypto *) crp->crp_opaque;
497	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
498	m = (struct mbuf *) crp->crp_buf;
499	skip = tc->tc_skip;
500	rlen = crp->crp_ilen - skip;
501
502	isr = tc->tc_isr;
503	IPSECREQUEST_LOCK(isr);
504	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
505	if (sav == NULL) {
506		ipcompstat.ipcomps_notdb++;
507		DPRINTF(("%s: SA expired while in crypto\n", __func__));
508		error = ENOBUFS;		/*XXX*/
509		goto bad;
510	}
511	IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
512
513	/* Check for crypto errors */
514	if (crp->crp_etype) {
515		/* Reset session ID */
516		if (sav->tdb_cryptoid != 0)
517			sav->tdb_cryptoid = crp->crp_sid;
518
519		if (crp->crp_etype == EAGAIN) {
520			KEY_FREESAV(&sav);
521			IPSECREQUEST_UNLOCK(isr);
522			return crypto_dispatch(crp);
523		}
524		ipcompstat.ipcomps_noxform++;
525		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
526		error = crp->crp_etype;
527		goto bad;
528	}
529	/* Shouldn't happen... */
530	if (m == NULL) {
531		ipcompstat.ipcomps_crypto++;
532		DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
533		error = EINVAL;
534		goto bad;
535	}
536	ipcompstat.ipcomps_hist[sav->alg_comp]++;
537
538	if (rlen > crp->crp_olen) {
539		/* Adjust the length in the IP header */
540		switch (sav->sah->saidx.dst.sa.sa_family) {
541#ifdef INET
542		case AF_INET:
543			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
544			break;
545#endif /* INET */
546#ifdef INET6
547		case AF_INET6:
548			mtod(m, struct ip6_hdr *)->ip6_plen =
549				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
550			break;
551#endif /* INET6 */
552		default:
553			ipcompstat.ipcomps_nopf++;
554			DPRINTF(("%s: unknown/unsupported protocol "
555			    "family %d, IPCA %s/%08lx\n", __func__,
556			    sav->sah->saidx.dst.sa.sa_family,
557			    ipsec_address(&sav->sah->saidx.dst),
558			    (u_long) ntohl(sav->spi)));
559			error = EPFNOSUPPORT;
560			goto bad;
561		}
562	} else {
563		/* compression was useless, we have lost time */
564		/* XXX add statistic */
565	}
566
567	/* Release the crypto descriptor */
568	free(tc, M_XDATA);
569	crypto_freereq(crp);
570
571	/* NB: m is reclaimed by ipsec_process_done. */
572	error = ipsec_process_done(m, isr);
573	KEY_FREESAV(&sav);
574	IPSECREQUEST_UNLOCK(isr);
575
576	return error;
577bad:
578	if (sav)
579		KEY_FREESAV(&sav);
580	IPSECREQUEST_UNLOCK(isr);
581	if (m)
582		m_freem(m);
583	free(tc, M_XDATA);
584	crypto_freereq(crp);
585	return error;
586}
587
588static struct xformsw ipcomp_xformsw = {
589	XF_IPCOMP,		XFT_COMP,		"IPcomp",
590	ipcomp_init,		ipcomp_zeroize,		ipcomp_input,
591	ipcomp_output
592};
593
594static void
595ipcomp_attach(void)
596{
597	xform_register(&ipcomp_xformsw);
598}
599SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
600