1 /* $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.2 2003/02/26 00:14:06 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/xform_ipcomp.c,v 1.7 2005/06/10 23:59:31 dillon Exp $ */
3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
6 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 /* IP payload compression protocol (IPComp), see RFC 2393 */
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
42 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
49 #include <net/route.h>
50 #include <netproto/ipsec/ipsec.h>
51 #include <netproto/ipsec/xform.h>
54 #include <netinet/ip6.h>
55 #include <netproto/ipsec/ipsec6.h>
58 #include <netproto/ipsec/ipcomp.h>
59 #include <netproto/ipsec/ipcomp_var.h>
61 #include <netproto/ipsec/key.h>
62 #include <netproto/ipsec/key_debug.h>
64 #include <opencrypto/cryptodev.h>
65 #include <opencrypto/deflate.h>
66 #include <opencrypto/xform.h>
68 int ipcomp_enable = 0;
69 struct ipcompstat ipcompstat;
71 SYSCTL_DECL(_net_inet_ipcomp);
72 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
73 ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "");
74 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
75 stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "");
77 static int ipcomp_input_cb(struct cryptop *crp);
78 static int ipcomp_output_cb(struct cryptop *crp);
81 ipcomp_algorithm_lookup(int alg)
83 if (alg >= IPCOMP_ALG_MAX)
86 case SADB_X_CALG_DEFLATE:
87 return &comp_algo_deflate;
93 * ipcomp_init() is called when an CPI is being set up.
96 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
98 struct comp_algo *tcomp;
99 struct cryptoini cric;
101 /* NB: algorithm really comes in alg_enc and not alg_comp! */
102 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
104 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
108 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
109 sav->tdb_xform = xsp;
110 sav->tdb_compalgxform = tcomp;
112 /* Initialize crypto session */
113 bzero(&cric, sizeof (cric));
114 cric.cri_alg = sav->tdb_compalgxform->type;
116 return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
120 * ipcomp_zeroize() used when IPCA is deleted
123 ipcomp_zeroize(struct secasvar *sav)
127 err = crypto_freesession(sav->tdb_cryptoid);
128 sav->tdb_cryptoid = 0;
133 * ipcomp_input() gets called to uncompress an input packet
136 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
138 struct tdb_crypto *tc;
139 struct cryptodesc *crdc;
141 int hlen = IPCOMP_HLENGTH;
143 /* Get crypto descriptors */
144 crp = crypto_getreq(1);
147 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
148 ipcompstat.ipcomps_crypto++;
151 /* Get IPsec-specific opaque pointer */
152 tc = malloc(sizeof (*tc), M_XDATA,
153 M_INTWAIT | M_ZERO | M_NULLOK);
157 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
158 ipcompstat.ipcomps_crypto++;
161 crdc = crp->crp_desc;
163 crdc->crd_skip = skip + hlen;
164 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
165 crdc->crd_inject = skip;
169 /* Decompression operation */
170 crdc->crd_alg = sav->tdb_compalgxform->type;
172 /* Crypto operation descriptor */
173 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
174 crp->crp_flags = CRYPTO_F_IMBUF;
175 crp->crp_buf = (caddr_t) m;
176 crp->crp_callback = ipcomp_input_cb;
177 crp->crp_sid = sav->tdb_cryptoid;
178 crp->crp_opaque = (caddr_t) tc;
180 /* These are passed as-is to the callback */
181 tc->tc_spi = sav->spi;
182 tc->tc_dst = sav->sah->saidx.dst;
183 tc->tc_proto = sav->sah->saidx.proto;
184 tc->tc_protoff = protoff;
187 return crypto_dispatch(crp);
191 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
192 if (saidx->dst.sa.sa_family == AF_INET6) { \
193 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
195 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
199 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
200 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
204 * IPComp input callback from the crypto driver.
207 ipcomp_input_cb(struct cryptop *crp)
209 struct cryptodesc *crd;
210 struct tdb_crypto *tc;
214 struct secasvar *sav;
215 struct secasindex *saidx;
216 int hlen = IPCOMP_HLENGTH, error, clen;
222 tc = (struct tdb_crypto *) crp->crp_opaque;
223 KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
225 protoff = tc->tc_protoff;
226 mtag = (struct mtag *) tc->tc_ptr;
227 m = (struct mbuf *) crp->crp_buf;
231 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
233 ipcompstat.ipcomps_notdb++;
234 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
235 error = ENOBUFS; /*XXX*/
239 saidx = &sav->sah->saidx;
240 KASSERT(saidx->dst.sa.sa_family == AF_INET ||
241 saidx->dst.sa.sa_family == AF_INET6,
242 ("ah_input_cb: unexpected protocol family %u",
243 saidx->dst.sa.sa_family));
245 /* Check for crypto errors */
246 if (crp->crp_etype) {
247 /* Reset the session ID */
248 if (sav->tdb_cryptoid != 0)
249 sav->tdb_cryptoid = crp->crp_sid;
251 if (crp->crp_etype == EAGAIN) {
254 return crypto_dispatch(crp);
257 ipcompstat.ipcomps_noxform++;
258 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
259 error = crp->crp_etype;
262 /* Shouldn't happen... */
264 ipcompstat.ipcomps_crypto++;
265 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
269 ipcompstat.ipcomps_hist[sav->alg_comp]++;
271 clen = crp->crp_olen; /* Length of data after processing */
273 /* Release the crypto descriptors */
274 free(tc, M_XDATA), tc = NULL;
275 crypto_freereq(crp), crp = NULL;
277 /* In case it's not done already, adjust the size of the mbuf chain */
278 m->m_pkthdr.len = clen + hlen + skip;
280 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
281 ipcompstat.ipcomps_hdrops++; /*XXX*/
282 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
283 error = EINVAL; /*XXX*/
287 /* Keep the next protocol field */
288 addr = (caddr_t) mtod(m, struct ip *) + skip;
289 nproto = ((struct ipcomp *) addr)->comp_nxt;
291 /* Remove the IPCOMP header */
292 error = m_striphdr(m, skip, hlen);
294 ipcompstat.ipcomps_hdrops++;
295 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
296 ipsec_address(&sav->sah->saidx.dst),
297 (u_long) ntohl(sav->spi)));
301 /* Restore the Next Protocol field */
302 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
304 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
323 * IPComp output routine, called by ipsec[46]_process_packet()
328 struct ipsecrequest *isr,
334 struct secasvar *sav;
335 struct comp_algo *ipcompx;
336 int error, ralen, hlen, maxpacketsize, roff;
338 struct cryptodesc *crdc;
340 struct tdb_crypto *tc;
342 struct ipcomp *ipcomp;
345 KASSERT(sav != NULL, ("ipcomp_output: null SA"));
346 ipcompx = sav->tdb_compalgxform;
347 KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
349 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
350 hlen = IPCOMP_HLENGTH;
352 ipcompstat.ipcomps_output++;
354 /* Check for maximum packet size violations. */
355 switch (sav->sah->saidx.dst.sa.sa_family) {
358 maxpacketsize = IP_MAXPACKET;
363 maxpacketsize = IPV6_MAXPACKET;
367 ipcompstat.ipcomps_nopf++;
368 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
370 sav->sah->saidx.dst.sa.sa_family,
371 ipsec_address(&sav->sah->saidx.dst),
372 (u_long) ntohl(sav->spi)));
373 error = EPFNOSUPPORT;
376 if (skip + hlen + ralen > maxpacketsize) {
377 ipcompstat.ipcomps_toobig++;
378 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
379 "(len %u, max len %u)\n",
380 ipsec_address(&sav->sah->saidx.dst),
381 (u_long) ntohl(sav->spi),
382 skip + hlen + ralen, maxpacketsize));
387 /* Update the counters */
388 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
392 ipcompstat.ipcomps_hdrops++;
393 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
394 ipsec_address(&sav->sah->saidx.dst),
395 (u_long) ntohl(sav->spi)));
400 /* Inject IPCOMP header */
401 mo = m_makespace(m, skip, hlen, &roff);
403 ipcompstat.ipcomps_wrap++;
404 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
406 ipsec_address(&sav->sah->saidx.dst),
407 (u_long) ntohl(sav->spi)));
411 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
413 /* Initialize the IPCOMP header */
414 /* XXX alignment always correct? */
415 switch (sav->sah->saidx.dst.sa.sa_family) {
418 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
423 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
427 ipcomp->comp_flags = 0;
428 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
430 /* Fix Next Protocol in IPv4/IPv6 header */
431 prot = IPPROTO_IPCOMP;
432 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
434 /* Ok now, we can pass to the crypto processing */
436 /* Get crypto descriptors */
437 crp = crypto_getreq(1);
439 ipcompstat.ipcomps_crypto++;
440 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
444 crdc = crp->crp_desc;
446 /* Compression descriptor */
447 crdc->crd_skip = skip + hlen;
448 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
449 crdc->crd_flags = CRD_F_COMP;
450 crdc->crd_inject = skip + hlen;
452 /* Compression operation */
453 crdc->crd_alg = ipcompx->type;
455 /* IPsec-specific opaque crypto info */
456 tc = malloc(sizeof(struct tdb_crypto),
457 M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
459 ipcompstat.ipcomps_crypto++;
460 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
467 tc->tc_spi = sav->spi;
468 tc->tc_dst = sav->sah->saidx.dst;
469 tc->tc_proto = sav->sah->saidx.proto;
470 tc->tc_skip = skip + hlen;
472 /* Crypto operation descriptor */
473 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
474 crp->crp_flags = CRYPTO_F_IMBUF;
475 crp->crp_buf = (caddr_t) m;
476 crp->crp_callback = ipcomp_output_cb;
477 crp->crp_opaque = (caddr_t) tc;
478 crp->crp_sid = sav->tdb_cryptoid;
480 return crypto_dispatch(crp);
488 * IPComp output callback from the crypto driver.
491 ipcomp_output_cb(struct cryptop *crp)
493 struct tdb_crypto *tc;
494 struct ipsecrequest *isr;
495 struct secasvar *sav;
497 int error, skip, rlen;
499 tc = (struct tdb_crypto *) crp->crp_opaque;
500 KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
501 m = (struct mbuf *) crp->crp_buf;
503 rlen = crp->crp_ilen - skip;
508 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
510 ipcompstat.ipcomps_notdb++;
511 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
512 error = ENOBUFS; /*XXX*/
515 KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
517 /* Check for crypto errors */
518 if (crp->crp_etype) {
519 /* Reset session ID */
520 if (sav->tdb_cryptoid != 0)
521 sav->tdb_cryptoid = crp->crp_sid;
523 if (crp->crp_etype == EAGAIN) {
526 return crypto_dispatch(crp);
528 ipcompstat.ipcomps_noxform++;
529 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
530 error = crp->crp_etype;
533 /* Shouldn't happen... */
535 ipcompstat.ipcomps_crypto++;
536 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
540 ipcompstat.ipcomps_hist[sav->alg_comp]++;
542 if (rlen > crp->crp_olen) {
543 /* Adjust the length in the IP header */
544 switch (sav->sah->saidx.dst.sa.sa_family) {
547 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
552 mtod(m, struct ip6_hdr *)->ip6_plen =
553 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
557 ipcompstat.ipcomps_nopf++;
558 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
559 "family %d, IPCA %s/%08lx\n",
560 sav->sah->saidx.dst.sa.sa_family,
561 ipsec_address(&sav->sah->saidx.dst),
562 (u_long) ntohl(sav->spi)));
563 error = EPFNOSUPPORT;
567 /* compression was useless, we have lost time */
568 /* XXX add statistic */
571 /* Release the crypto descriptor */
575 /* NB: m is reclaimed by ipsec_process_done. */
576 error = ipsec_process_done(m, isr);
591 static struct xformsw ipcomp_xformsw = {
592 XF_IPCOMP, XFT_COMP, "IPcomp",
593 ipcomp_init, ipcomp_zeroize, ipcomp_input,
600 xform_register(&ipcomp_xformsw);
602 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)