1 /* $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.2 2003/02/26 00:14:06 sam Exp $ */
2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
5 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 /* IP payload compression protocol (IPComp), see RFC 2393 */
33 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
40 #include <sys/protosw.h>
41 #include <sys/sysctl.h>
42 #include <sys/thread2.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 struct ipcomp *ipcomp;
143 int hlen = IPCOMP_HLENGTH;
146 * Check that the next header of the IPComp is not IPComp again, before
147 * doing any real work. Given it is not possible to do double
148 * compression it means someone is playing tricks on us.
150 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
151 ipcompstat.ipcomps_hdrops++; /*XXX*/
152 DPRINTF(("%s: m_pullup failed\n", __func__));
155 addr = (caddr_t) mtod(m, struct ip *) + skip;
156 ipcomp = (struct ipcomp *)addr;
157 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
159 ipcompstat.ipcomps_pdrops++; /* XXX have our own stats? */
160 DPRINTF(("%s: recursive compression detected\n", __func__));
164 /* Get crypto descriptors */
165 crp = crypto_getreq(1);
168 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
169 ipcompstat.ipcomps_crypto++;
172 /* Get IPsec-specific opaque pointer */
173 tc = kmalloc(sizeof (*tc), M_XDATA,
174 M_INTWAIT | M_ZERO | M_NULLOK);
178 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
179 ipcompstat.ipcomps_crypto++;
182 crdc = crp->crp_desc;
184 crdc->crd_skip = skip + hlen;
185 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
186 crdc->crd_inject = skip;
190 /* Decompression operation */
191 crdc->crd_alg = sav->tdb_compalgxform->type;
193 /* Crypto operation descriptor */
194 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
195 crp->crp_flags = CRYPTO_F_IMBUF;
196 crp->crp_buf = (caddr_t) m;
197 crp->crp_callback = ipcomp_input_cb;
198 crp->crp_sid = sav->tdb_cryptoid;
199 crp->crp_opaque = (caddr_t) tc;
201 /* These are passed as-is to the callback */
202 tc->tc_spi = sav->spi;
203 tc->tc_dst = sav->sah->saidx.dst;
204 tc->tc_proto = sav->sah->saidx.proto;
205 tc->tc_protoff = protoff;
208 return crypto_dispatch(crp);
212 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
213 if (saidx->dst.sa.sa_family == AF_INET6) { \
214 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
216 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
220 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
221 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
225 * IPComp input callback from the crypto driver.
228 ipcomp_input_cb(struct cryptop *crp)
230 struct cryptodesc *crd;
231 struct tdb_crypto *tc;
235 struct secasvar *sav;
236 struct secasindex *saidx;
237 int hlen = IPCOMP_HLENGTH, error, clen;
243 tc = (struct tdb_crypto *) crp->crp_opaque;
244 KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
246 protoff = tc->tc_protoff;
247 mtag = (struct mtag *) tc->tc_ptr;
248 m = (struct mbuf *) crp->crp_buf;
252 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
254 ipcompstat.ipcomps_notdb++;
255 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
256 error = ENOBUFS; /*XXX*/
260 saidx = &sav->sah->saidx;
261 KASSERT(saidx->dst.sa.sa_family == AF_INET ||
262 saidx->dst.sa.sa_family == AF_INET6,
263 ("ah_input_cb: unexpected protocol family %u",
264 saidx->dst.sa.sa_family));
266 /* Check for crypto errors */
267 if (crp->crp_etype) {
268 /* Reset the session ID */
269 if (sav->tdb_cryptoid != 0)
270 sav->tdb_cryptoid = crp->crp_sid;
272 if (crp->crp_etype == EAGAIN) {
275 return crypto_dispatch(crp);
278 ipcompstat.ipcomps_noxform++;
279 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
280 error = crp->crp_etype;
283 /* Shouldn't happen... */
285 ipcompstat.ipcomps_crypto++;
286 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
290 ipcompstat.ipcomps_hist[sav->alg_comp]++;
292 clen = crp->crp_olen; /* Length of data after processing */
294 /* Release the crypto descriptors */
295 kfree(tc, M_XDATA), tc = NULL;
296 crypto_freereq(crp), crp = NULL;
298 /* In case it's not done already, adjust the size of the mbuf chain */
299 m->m_pkthdr.len = clen + hlen + skip;
301 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
302 ipcompstat.ipcomps_hdrops++; /*XXX*/
303 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
304 error = EINVAL; /*XXX*/
308 /* Keep the next protocol field */
309 addr = (caddr_t) mtod(m, struct ip *) + skip;
310 nproto = ((struct ipcomp *) addr)->comp_nxt;
312 /* Remove the IPCOMP header */
313 error = m_striphdr(m, skip, hlen);
315 ipcompstat.ipcomps_hdrops++;
316 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
317 ipsec_address(&sav->sah->saidx.dst),
318 (u_long) ntohl(sav->spi)));
322 /* Restore the Next Protocol field */
323 m_copyback(m, protoff, sizeof (u_int8_t), &nproto);
325 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
344 * IPComp output routine, called by ipsec[46]_process_packet()
349 struct ipsecrequest *isr,
355 struct secasvar *sav;
356 struct comp_algo *ipcompx;
357 int error, ralen, hlen, maxpacketsize, roff;
359 struct cryptodesc *crdc;
361 struct tdb_crypto *tc;
363 struct ipcomp *ipcomp;
366 KASSERT(sav != NULL, ("ipcomp_output: null SA"));
367 ipcompx = sav->tdb_compalgxform;
368 KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
370 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
371 hlen = IPCOMP_HLENGTH;
373 ipcompstat.ipcomps_output++;
375 /* Check for maximum packet size violations. */
376 switch (sav->sah->saidx.dst.sa.sa_family) {
379 maxpacketsize = IP_MAXPACKET;
384 maxpacketsize = IPV6_MAXPACKET;
388 ipcompstat.ipcomps_nopf++;
389 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
391 sav->sah->saidx.dst.sa.sa_family,
392 ipsec_address(&sav->sah->saidx.dst),
393 (u_long) ntohl(sav->spi)));
394 error = EPFNOSUPPORT;
397 if (skip + hlen + ralen > maxpacketsize) {
398 ipcompstat.ipcomps_toobig++;
399 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
400 "(len %u, max len %u)\n",
401 ipsec_address(&sav->sah->saidx.dst),
402 (u_long) ntohl(sav->spi),
403 skip + hlen + ralen, maxpacketsize));
408 /* Update the counters */
409 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
413 ipcompstat.ipcomps_hdrops++;
414 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
415 ipsec_address(&sav->sah->saidx.dst),
416 (u_long) ntohl(sav->spi)));
421 /* Inject IPCOMP header */
422 mo = m_makespace(m, skip, hlen, &roff);
424 ipcompstat.ipcomps_wrap++;
425 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
427 ipsec_address(&sav->sah->saidx.dst),
428 (u_long) ntohl(sav->spi)));
432 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
434 /* Initialize the IPCOMP header */
435 /* XXX alignment always correct? */
436 switch (sav->sah->saidx.dst.sa.sa_family) {
439 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
444 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
448 ipcomp->comp_flags = 0;
449 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
451 /* Fix Next Protocol in IPv4/IPv6 header */
452 prot = IPPROTO_IPCOMP;
453 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
455 /* Ok now, we can pass to the crypto processing */
457 /* Get crypto descriptors */
458 crp = crypto_getreq(1);
460 ipcompstat.ipcomps_crypto++;
461 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
465 crdc = crp->crp_desc;
467 /* Compression descriptor */
468 crdc->crd_skip = skip + hlen;
469 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
470 crdc->crd_flags = CRD_F_COMP;
471 crdc->crd_inject = skip + hlen;
473 /* Compression operation */
474 crdc->crd_alg = ipcompx->type;
476 /* IPsec-specific opaque crypto info */
477 tc = kmalloc(sizeof(struct tdb_crypto),
478 M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
480 ipcompstat.ipcomps_crypto++;
481 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
488 tc->tc_spi = sav->spi;
489 tc->tc_dst = sav->sah->saidx.dst;
490 tc->tc_proto = sav->sah->saidx.proto;
491 tc->tc_skip = skip + hlen;
493 /* Crypto operation descriptor */
494 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
495 crp->crp_flags = CRYPTO_F_IMBUF;
496 crp->crp_buf = (caddr_t) m;
497 crp->crp_callback = ipcomp_output_cb;
498 crp->crp_opaque = (caddr_t) tc;
499 crp->crp_sid = sav->tdb_cryptoid;
501 return crypto_dispatch(crp);
509 * IPComp output callback from the crypto driver.
512 ipcomp_output_cb(struct cryptop *crp)
514 struct tdb_crypto *tc;
515 struct ipsecrequest *isr;
516 struct secasvar *sav;
518 int error, skip, rlen;
520 tc = (struct tdb_crypto *) crp->crp_opaque;
521 KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
522 m = (struct mbuf *) crp->crp_buf;
524 rlen = crp->crp_ilen - skip;
529 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
531 ipcompstat.ipcomps_notdb++;
532 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
533 error = ENOBUFS; /*XXX*/
536 KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed"));
538 /* Check for crypto errors */
539 if (crp->crp_etype) {
540 /* Reset session ID */
541 if (sav->tdb_cryptoid != 0)
542 sav->tdb_cryptoid = crp->crp_sid;
544 if (crp->crp_etype == EAGAIN) {
547 return crypto_dispatch(crp);
549 ipcompstat.ipcomps_noxform++;
550 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
551 error = crp->crp_etype;
554 /* Shouldn't happen... */
556 ipcompstat.ipcomps_crypto++;
557 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
561 ipcompstat.ipcomps_hist[sav->alg_comp]++;
563 if (rlen > crp->crp_olen) {
564 /* Adjust the length in the IP header */
565 switch (sav->sah->saidx.dst.sa.sa_family) {
568 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
573 mtod(m, struct ip6_hdr *)->ip6_plen =
574 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
578 ipcompstat.ipcomps_nopf++;
579 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
580 "family %d, IPCA %s/%08lx\n",
581 sav->sah->saidx.dst.sa.sa_family,
582 ipsec_address(&sav->sah->saidx.dst),
583 (u_long) ntohl(sav->spi)));
584 error = EPFNOSUPPORT;
588 /* compression was useless, we have lost time */
589 /* XXX add statistic */
592 /* Release the crypto descriptor */
596 /* NB: m is reclaimed by ipsec_process_done. */
597 error = ipsec_process_done(m, isr);
612 static struct xformsw ipcomp_xformsw = {
613 XF_IPCOMP, XFT_COMP, "IPcomp",
614 ipcomp_init, ipcomp_zeroize, ipcomp_input,
621 xform_register(&ipcomp_xformsw);
623 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)