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.10 2006/10/12 01:32:51 hsu 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>
43 #include <sys/thread2.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
50 #include <net/route.h>
51 #include <netproto/ipsec/ipsec.h>
52 #include <netproto/ipsec/xform.h>
55 #include <netinet/ip6.h>
56 #include <netproto/ipsec/ipsec6.h>
59 #include <netproto/ipsec/ipcomp.h>
60 #include <netproto/ipsec/ipcomp_var.h>
62 #include <netproto/ipsec/key.h>
63 #include <netproto/ipsec/key_debug.h>
65 #include <opencrypto/cryptodev.h>
66 #include <opencrypto/deflate.h>
67 #include <opencrypto/xform.h>
69 int ipcomp_enable = 0;
70 struct ipcompstat ipcompstat;
72 SYSCTL_DECL(_net_inet_ipcomp);
73 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
74 ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "");
75 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
76 stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "");
78 static int ipcomp_input_cb(struct cryptop *crp);
79 static int ipcomp_output_cb(struct cryptop *crp);
82 ipcomp_algorithm_lookup(int alg)
84 if (alg >= IPCOMP_ALG_MAX)
87 case SADB_X_CALG_DEFLATE:
88 return &comp_algo_deflate;
94 * ipcomp_init() is called when an CPI is being set up.
97 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
99 struct comp_algo *tcomp;
100 struct cryptoini cric;
102 /* NB: algorithm really comes in alg_enc and not alg_comp! */
103 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
105 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
109 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
110 sav->tdb_xform = xsp;
111 sav->tdb_compalgxform = tcomp;
113 /* Initialize crypto session */
114 bzero(&cric, sizeof (cric));
115 cric.cri_alg = sav->tdb_compalgxform->type;
117 return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
121 * ipcomp_zeroize() used when IPCA is deleted
124 ipcomp_zeroize(struct secasvar *sav)
128 err = crypto_freesession(sav->tdb_cryptoid);
129 sav->tdb_cryptoid = 0;
134 * ipcomp_input() gets called to uncompress an input packet
137 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
139 struct tdb_crypto *tc;
140 struct cryptodesc *crdc;
142 struct ipcomp *ipcomp;
144 int hlen = IPCOMP_HLENGTH;
147 * Check that the next header of the IPComp is not IPComp again, before
148 * doing any real work. Given it is not possible to do double
149 * compression it means someone is playing tricks on us.
151 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
152 ipcompstat.ipcomps_hdrops++; /*XXX*/
153 DPRINTF(("%s: m_pullup failed\n", __func__));
156 addr = (caddr_t) mtod(m, struct ip *) + skip;
157 ipcomp = (struct ipcomp *)addr;
158 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
160 ipcompstat.ipcomps_pdrops++; /* XXX have our own stats? */
161 DPRINTF(("%s: recursive compression detected\n", __func__));
165 /* Get crypto descriptors */
166 crp = crypto_getreq(1);
169 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
170 ipcompstat.ipcomps_crypto++;
173 /* Get IPsec-specific opaque pointer */
174 tc = kmalloc(sizeof (*tc), M_XDATA,
175 M_INTWAIT | M_ZERO | M_NULLOK);
179 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
180 ipcompstat.ipcomps_crypto++;
183 crdc = crp->crp_desc;
185 crdc->crd_skip = skip + hlen;
186 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
187 crdc->crd_inject = skip;
191 /* Decompression operation */
192 crdc->crd_alg = sav->tdb_compalgxform->type;
194 /* Crypto operation descriptor */
195 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
196 crp->crp_flags = CRYPTO_F_IMBUF;
197 crp->crp_buf = (caddr_t) m;
198 crp->crp_callback = ipcomp_input_cb;
199 crp->crp_sid = sav->tdb_cryptoid;
200 crp->crp_opaque = (caddr_t) tc;
202 /* These are passed as-is to the callback */
203 tc->tc_spi = sav->spi;
204 tc->tc_dst = sav->sah->saidx.dst;
205 tc->tc_proto = sav->sah->saidx.proto;
206 tc->tc_protoff = protoff;
209 return crypto_dispatch(crp);
213 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
214 if (saidx->dst.sa.sa_family == AF_INET6) { \
215 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
217 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
221 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
222 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
226 * IPComp input callback from the crypto driver.
229 ipcomp_input_cb(struct cryptop *crp)
231 struct cryptodesc *crd;
232 struct tdb_crypto *tc;
236 struct secasvar *sav;
237 struct secasindex *saidx;
238 int hlen = IPCOMP_HLENGTH, error, clen;
244 tc = (struct tdb_crypto *) crp->crp_opaque;
245 KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
247 protoff = tc->tc_protoff;
248 mtag = (struct mtag *) tc->tc_ptr;
249 m = (struct mbuf *) crp->crp_buf;
253 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
255 ipcompstat.ipcomps_notdb++;
256 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
257 error = ENOBUFS; /*XXX*/
261 saidx = &sav->sah->saidx;
262 KASSERT(saidx->dst.sa.sa_family == AF_INET ||
263 saidx->dst.sa.sa_family == AF_INET6,
264 ("ah_input_cb: unexpected protocol family %u",
265 saidx->dst.sa.sa_family));
267 /* Check for crypto errors */
268 if (crp->crp_etype) {
269 /* Reset the session ID */
270 if (sav->tdb_cryptoid != 0)
271 sav->tdb_cryptoid = crp->crp_sid;
273 if (crp->crp_etype == EAGAIN) {
276 return crypto_dispatch(crp);
279 ipcompstat.ipcomps_noxform++;
280 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
281 error = crp->crp_etype;
284 /* Shouldn't happen... */
286 ipcompstat.ipcomps_crypto++;
287 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
291 ipcompstat.ipcomps_hist[sav->alg_comp]++;
293 clen = crp->crp_olen; /* Length of data after processing */
295 /* Release the crypto descriptors */
296 kfree(tc, M_XDATA), tc = NULL;
297 crypto_freereq(crp), crp = NULL;
299 /* In case it's not done already, adjust the size of the mbuf chain */
300 m->m_pkthdr.len = clen + hlen + skip;
302 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
303 ipcompstat.ipcomps_hdrops++; /*XXX*/
304 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
305 error = EINVAL; /*XXX*/
309 /* Keep the next protocol field */
310 addr = (caddr_t) mtod(m, struct ip *) + skip;
311 nproto = ((struct ipcomp *) addr)->comp_nxt;
313 /* Remove the IPCOMP header */
314 error = m_striphdr(m, skip, hlen);
316 ipcompstat.ipcomps_hdrops++;
317 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
318 ipsec_address(&sav->sah->saidx.dst),
319 (u_long) ntohl(sav->spi)));
323 /* Restore the Next Protocol field */
324 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
326 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
345 * IPComp output routine, called by ipsec[46]_process_packet()
350 struct ipsecrequest *isr,
356 struct secasvar *sav;
357 struct comp_algo *ipcompx;
358 int error, ralen, hlen, maxpacketsize, roff;
360 struct cryptodesc *crdc;
362 struct tdb_crypto *tc;
364 struct ipcomp *ipcomp;
367 KASSERT(sav != NULL, ("ipcomp_output: null SA"));
368 ipcompx = sav->tdb_compalgxform;
369 KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
371 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
372 hlen = IPCOMP_HLENGTH;
374 ipcompstat.ipcomps_output++;
376 /* Check for maximum packet size violations. */
377 switch (sav->sah->saidx.dst.sa.sa_family) {
380 maxpacketsize = IP_MAXPACKET;
385 maxpacketsize = IPV6_MAXPACKET;
389 ipcompstat.ipcomps_nopf++;
390 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
392 sav->sah->saidx.dst.sa.sa_family,
393 ipsec_address(&sav->sah->saidx.dst),
394 (u_long) ntohl(sav->spi)));
395 error = EPFNOSUPPORT;
398 if (skip + hlen + ralen > maxpacketsize) {
399 ipcompstat.ipcomps_toobig++;
400 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
401 "(len %u, max len %u)\n",
402 ipsec_address(&sav->sah->saidx.dst),
403 (u_long) ntohl(sav->spi),
404 skip + hlen + ralen, maxpacketsize));
409 /* Update the counters */
410 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
414 ipcompstat.ipcomps_hdrops++;
415 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
416 ipsec_address(&sav->sah->saidx.dst),
417 (u_long) ntohl(sav->spi)));
422 /* Inject IPCOMP header */
423 mo = m_makespace(m, skip, hlen, &roff);
425 ipcompstat.ipcomps_wrap++;
426 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
428 ipsec_address(&sav->sah->saidx.dst),
429 (u_long) ntohl(sav->spi)));
433 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
435 /* Initialize the IPCOMP header */
436 /* XXX alignment always correct? */
437 switch (sav->sah->saidx.dst.sa.sa_family) {
440 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
445 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
449 ipcomp->comp_flags = 0;
450 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
452 /* Fix Next Protocol in IPv4/IPv6 header */
453 prot = IPPROTO_IPCOMP;
454 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
456 /* Ok now, we can pass to the crypto processing */
458 /* Get crypto descriptors */
459 crp = crypto_getreq(1);
461 ipcompstat.ipcomps_crypto++;
462 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
466 crdc = crp->crp_desc;
468 /* Compression descriptor */
469 crdc->crd_skip = skip + hlen;
470 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
471 crdc->crd_flags = CRD_F_COMP;
472 crdc->crd_inject = skip + hlen;
474 /* Compression operation */
475 crdc->crd_alg = ipcompx->type;
477 /* IPsec-specific opaque crypto info */
478 tc = kmalloc(sizeof(struct tdb_crypto),
479 M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
481 ipcompstat.ipcomps_crypto++;
482 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
489 tc->tc_spi = sav->spi;
490 tc->tc_dst = sav->sah->saidx.dst;
491 tc->tc_proto = sav->sah->saidx.proto;
492 tc->tc_skip = skip + hlen;
494 /* Crypto operation descriptor */
495 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
496 crp->crp_flags = CRYPTO_F_IMBUF;
497 crp->crp_buf = (caddr_t) m;
498 crp->crp_callback = ipcomp_output_cb;
499 crp->crp_opaque = (caddr_t) tc;
500 crp->crp_sid = sav->tdb_cryptoid;
502 return crypto_dispatch(crp);
510 * IPComp output callback from the crypto driver.
513 ipcomp_output_cb(struct cryptop *crp)
515 struct tdb_crypto *tc;
516 struct ipsecrequest *isr;
517 struct secasvar *sav;
519 int error, skip, rlen;
521 tc = (struct tdb_crypto *) crp->crp_opaque;
522 KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
523 m = (struct mbuf *) crp->crp_buf;
525 rlen = crp->crp_ilen - skip;
530 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
532 ipcompstat.ipcomps_notdb++;
533 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
534 error = ENOBUFS; /*XXX*/
537 KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
539 /* Check for crypto errors */
540 if (crp->crp_etype) {
541 /* Reset session ID */
542 if (sav->tdb_cryptoid != 0)
543 sav->tdb_cryptoid = crp->crp_sid;
545 if (crp->crp_etype == EAGAIN) {
548 return crypto_dispatch(crp);
550 ipcompstat.ipcomps_noxform++;
551 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
552 error = crp->crp_etype;
555 /* Shouldn't happen... */
557 ipcompstat.ipcomps_crypto++;
558 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
562 ipcompstat.ipcomps_hist[sav->alg_comp]++;
564 if (rlen > crp->crp_olen) {
565 /* Adjust the length in the IP header */
566 switch (sav->sah->saidx.dst.sa.sa_family) {
569 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
574 mtod(m, struct ip6_hdr *)->ip6_plen =
575 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
579 ipcompstat.ipcomps_nopf++;
580 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
581 "family %d, IPCA %s/%08lx\n",
582 sav->sah->saidx.dst.sa.sa_family,
583 ipsec_address(&sav->sah->saidx.dst),
584 (u_long) ntohl(sav->spi)));
585 error = EPFNOSUPPORT;
589 /* compression was useless, we have lost time */
590 /* XXX add statistic */
593 /* Release the crypto descriptor */
597 /* NB: m is reclaimed by ipsec_process_done. */
598 error = ipsec_process_done(m, isr);
613 static struct xformsw ipcomp_xformsw = {
614 XF_IPCOMP, XFT_COMP, "IPcomp",
615 ipcomp_init, ipcomp_zeroize, ipcomp_input,
622 xform_register(&ipcomp_xformsw);
624 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)