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>
39 #include <sys/mutex.h>
40 #include <sys/rwlock.h>
41 #include <sys/socket.h>
42 #include <sys/kernel.h>
43 #include <sys/protosw.h>
44 #include <sys/sysctl.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_var.h>
51 #include <net/route.h>
54 #include <netipsec/ipsec.h>
55 #include <netipsec/xform.h>
58 #include <netinet/ip6.h>
59 #include <netipsec/ipsec6.h>
62 #include <netipsec/ipcomp.h>
63 #include <netipsec/ipcomp_var.h>
65 #include <netipsec/key.h>
66 #include <netipsec/key_debug.h>
68 #include <opencrypto/cryptodev.h>
69 #include <opencrypto/deflate.h>
70 #include <opencrypto/xform.h>
72 VNET_DEFINE(int, ipcomp_enable) = 1;
73 VNET_DEFINE(struct ipcompstat, ipcompstat);
75 SYSCTL_DECL(_net_inet_ipcomp);
76 SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
77 ipcomp_enable, CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
78 SYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
79 stats, CTLFLAG_RD, &VNET_NAME(ipcompstat), ipcompstat, "");
81 static int ipcomp_input_cb(struct cryptop *crp);
82 static int ipcomp_output_cb(struct cryptop *crp);
85 ipcomp_algorithm_lookup(int alg)
87 if (alg >= IPCOMP_ALG_MAX)
90 case SADB_X_CALG_DEFLATE:
91 return &comp_algo_deflate;
97 * ipcomp_init() is called when an CPI is being set up.
100 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
102 struct comp_algo *tcomp;
103 struct cryptoini cric;
105 /* NB: algorithm really comes in alg_enc and not alg_comp! */
106 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
108 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
112 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
113 sav->tdb_xform = xsp;
114 sav->tdb_compalgxform = tcomp;
116 /* Initialize crypto session */
117 bzero(&cric, sizeof (cric));
118 cric.cri_alg = sav->tdb_compalgxform->type;
120 return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
124 * ipcomp_zeroize() used when IPCA is deleted
127 ipcomp_zeroize(struct secasvar *sav)
131 err = crypto_freesession(sav->tdb_cryptoid);
132 sav->tdb_cryptoid = 0;
137 * ipcomp_input() gets called to uncompress an input packet
140 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
142 struct tdb_crypto *tc;
143 struct cryptodesc *crdc;
145 struct ipcomp *ipcomp;
147 int hlen = IPCOMP_HLENGTH;
150 * Check that the next header of the IPComp is not IPComp again, before
151 * doing any real work. Given it is not possible to do double
152 * compression it means someone is playing tricks on us.
154 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
155 V_ipcompstat.ipcomps_hdrops++; /*XXX*/
156 DPRINTF(("%s: m_pullup failed\n", __func__));
159 addr = (caddr_t) mtod(m, struct ip *) + skip;
160 ipcomp = (struct ipcomp *)addr;
161 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
163 V_ipcompstat.ipcomps_pdrops++; /* XXX have our own stats? */
164 DPRINTF(("%s: recursive compression detected\n", __func__));
168 /* Get crypto descriptors */
169 crp = crypto_getreq(1);
172 DPRINTF(("%s: no crypto descriptors\n", __func__));
173 V_ipcompstat.ipcomps_crypto++;
176 /* Get IPsec-specific opaque pointer */
177 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
181 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
182 V_ipcompstat.ipcomps_crypto++;
185 crdc = crp->crp_desc;
187 crdc->crd_skip = skip + hlen;
188 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
189 crdc->crd_inject = skip;
193 /* Decompression operation */
194 crdc->crd_alg = sav->tdb_compalgxform->type;
196 /* Crypto operation descriptor */
197 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
198 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
199 crp->crp_buf = (caddr_t) m;
200 crp->crp_callback = ipcomp_input_cb;
201 crp->crp_sid = sav->tdb_cryptoid;
202 crp->crp_opaque = (caddr_t) tc;
204 /* These are passed as-is to the callback */
205 tc->tc_spi = sav->spi;
206 tc->tc_dst = sav->sah->saidx.dst;
207 tc->tc_proto = sav->sah->saidx.proto;
208 tc->tc_protoff = protoff;
213 return crypto_dispatch(crp);
217 * IPComp input callback from the crypto driver.
220 ipcomp_input_cb(struct cryptop *crp)
222 struct cryptodesc *crd;
223 struct tdb_crypto *tc;
227 struct secasvar *sav;
228 struct secasindex *saidx;
229 int hlen = IPCOMP_HLENGTH, error, clen;
235 tc = (struct tdb_crypto *) crp->crp_opaque;
236 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
238 protoff = tc->tc_protoff;
239 mtag = (struct mtag *) tc->tc_ptr;
240 m = (struct mbuf *) crp->crp_buf;
243 IPSEC_ASSERT(sav != NULL, ("null SA!"));
245 saidx = &sav->sah->saidx;
246 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
247 saidx->dst.sa.sa_family == AF_INET6,
248 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
250 /* Check for crypto errors */
251 if (crp->crp_etype) {
252 /* Reset the session ID */
253 if (sav->tdb_cryptoid != 0)
254 sav->tdb_cryptoid = crp->crp_sid;
256 if (crp->crp_etype == EAGAIN) {
257 return crypto_dispatch(crp);
259 V_ipcompstat.ipcomps_noxform++;
260 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
261 error = crp->crp_etype;
264 /* Shouldn't happen... */
266 V_ipcompstat.ipcomps_crypto++;
267 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
271 V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
273 clen = crp->crp_olen; /* Length of data after processing */
275 /* Release the crypto descriptors */
276 free(tc, M_XDATA), tc = NULL;
277 crypto_freereq(crp), crp = NULL;
279 /* In case it's not done already, adjust the size of the mbuf chain */
280 m->m_pkthdr.len = clen + hlen + skip;
282 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
283 V_ipcompstat.ipcomps_hdrops++; /*XXX*/
284 DPRINTF(("%s: m_pullup failed\n", __func__));
285 error = EINVAL; /*XXX*/
289 /* Keep the next protocol field */
290 addr = (caddr_t) mtod(m, struct ip *) + skip;
291 nproto = ((struct ipcomp *) addr)->comp_nxt;
293 /* Remove the IPCOMP header */
294 error = m_striphdr(m, skip, hlen);
296 V_ipcompstat.ipcomps_hdrops++;
297 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
298 ipsec_address(&sav->sah->saidx.dst),
299 (u_long) ntohl(sav->spi)));
303 /* Restore the Next Protocol field */
304 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
306 switch (saidx->dst.sa.sa_family) {
309 error = ipsec6_common_input_cb(m, sav, skip, protoff, NULL);
314 error = ipsec4_common_input_cb(m, sav, skip, protoff, NULL);
318 panic("%s: Unexpected address family: %d saidx=%p", __func__,
319 saidx->dst.sa.sa_family, saidx);
337 * IPComp output routine, called by ipsec[46]_process_packet()
342 struct ipsecrequest *isr,
348 struct secasvar *sav;
349 struct comp_algo *ipcompx;
350 int error, ralen, maxpacketsize;
351 struct cryptodesc *crdc;
353 struct tdb_crypto *tc;
356 IPSEC_ASSERT(sav != NULL, ("null SA"));
357 ipcompx = sav->tdb_compalgxform;
358 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
361 * Do not touch the packet in case our payload to compress
362 * is lower than the minimal threshold of the compression
363 * alogrithm. We will just send out the data uncompressed.
364 * See RFC 3173, 2.2. Non-Expansion Policy.
366 if (m->m_pkthdr.len <= ipcompx->minlen) {
367 V_ipcompstat.ipcomps_threshold++;
368 return ipsec_process_done(m, isr);
371 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
372 V_ipcompstat.ipcomps_output++;
374 /* Check for maximum packet size violations. */
375 switch (sav->sah->saidx.dst.sa.sa_family) {
378 maxpacketsize = IP_MAXPACKET;
383 maxpacketsize = IPV6_MAXPACKET;
387 V_ipcompstat.ipcomps_nopf++;
388 DPRINTF(("%s: unknown/unsupported protocol family %d, "
389 "IPCA %s/%08lx\n", __func__,
390 sav->sah->saidx.dst.sa.sa_family,
391 ipsec_address(&sav->sah->saidx.dst),
392 (u_long) ntohl(sav->spi)));
393 error = EPFNOSUPPORT;
396 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
397 V_ipcompstat.ipcomps_toobig++;
398 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
399 "(len %u, max len %u)\n", __func__,
400 ipsec_address(&sav->sah->saidx.dst),
401 (u_long) ntohl(sav->spi),
402 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
407 /* Update the counters */
408 V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
410 m = m_unshare(m, M_NOWAIT);
412 V_ipcompstat.ipcomps_hdrops++;
413 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
414 __func__, ipsec_address(&sav->sah->saidx.dst),
415 (u_long) ntohl(sav->spi)));
420 /* Ok now, we can pass to the crypto processing. */
422 /* Get crypto descriptors */
423 crp = crypto_getreq(1);
425 V_ipcompstat.ipcomps_crypto++;
426 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
430 crdc = crp->crp_desc;
432 /* Compression descriptor */
433 crdc->crd_skip = skip;
434 crdc->crd_len = ralen;
435 crdc->crd_flags = CRD_F_COMP;
436 crdc->crd_inject = skip;
438 /* Compression operation */
439 crdc->crd_alg = ipcompx->type;
441 /* IPsec-specific opaque crypto info */
442 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
443 M_XDATA, M_NOWAIT|M_ZERO);
445 V_ipcompstat.ipcomps_crypto++;
446 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
455 tc->tc_spi = sav->spi;
456 tc->tc_dst = sav->sah->saidx.dst;
457 tc->tc_proto = sav->sah->saidx.proto;
458 tc->tc_protoff = protoff;
461 /* Crypto operation descriptor */
462 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
463 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
464 crp->crp_buf = (caddr_t) m;
465 crp->crp_callback = ipcomp_output_cb;
466 crp->crp_opaque = (caddr_t) tc;
467 crp->crp_sid = sav->tdb_cryptoid;
469 return crypto_dispatch(crp);
477 * IPComp output callback from the crypto driver.
480 ipcomp_output_cb(struct cryptop *crp)
482 struct tdb_crypto *tc;
483 struct ipsecrequest *isr;
484 struct secasvar *sav;
488 tc = (struct tdb_crypto *) crp->crp_opaque;
489 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
490 m = (struct mbuf *) crp->crp_buf;
494 IPSECREQUEST_LOCK(isr);
496 /* With the isr lock released SA pointer can be updated. */
497 if (sav != isr->sav) {
498 V_ipcompstat.ipcomps_notdb++;
499 DPRINTF(("%s: SA expired while in crypto\n", __func__));
500 error = ENOBUFS; /*XXX*/
504 /* Check for crypto errors */
505 if (crp->crp_etype) {
506 /* Reset the session ID */
507 if (sav->tdb_cryptoid != 0)
508 sav->tdb_cryptoid = crp->crp_sid;
510 if (crp->crp_etype == EAGAIN) {
511 IPSECREQUEST_UNLOCK(isr);
512 return crypto_dispatch(crp);
514 V_ipcompstat.ipcomps_noxform++;
515 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
516 error = crp->crp_etype;
519 /* Shouldn't happen... */
521 V_ipcompstat.ipcomps_crypto++;
522 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
526 V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
528 if (crp->crp_ilen - skip > crp->crp_olen) {
530 struct ipcomp *ipcomp;
534 /* Compression helped, inject IPCOMP header. */
535 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
537 V_ipcompstat.ipcomps_wrap++;
538 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
539 __func__, ipsec_address(&sav->sah->saidx.dst),
540 (u_long) ntohl(sav->spi)));
544 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
546 /* Initialize the IPCOMP header */
547 /* XXX alignment always correct? */
548 switch (sav->sah->saidx.dst.sa.sa_family) {
551 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
556 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
560 ipcomp->comp_flags = 0;
561 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
563 /* Fix Next Protocol in IPv4/IPv6 header */
564 prot = IPPROTO_IPCOMP;
565 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
568 /* Adjust the length in the IP header */
569 switch (sav->sah->saidx.dst.sa.sa_family) {
572 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
577 mtod(m, struct ip6_hdr *)->ip6_plen =
578 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
582 V_ipcompstat.ipcomps_nopf++;
583 DPRINTF(("%s: unknown/unsupported protocol "
584 "family %d, IPCA %s/%08lx\n", __func__,
585 sav->sah->saidx.dst.sa.sa_family,
586 ipsec_address(&sav->sah->saidx.dst),
587 (u_long) ntohl(sav->spi)));
588 error = EPFNOSUPPORT;
592 /* Compression was useless, we have lost time. */
593 V_ipcompstat.ipcomps_uncompr++;
594 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
595 __func__, crp->crp_ilen, skip, crp->crp_olen));
596 /* XXX remember state to not compress the next couple
597 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
600 /* Release the crypto descriptor */
604 /* NB: m is reclaimed by ipsec_process_done. */
605 error = ipsec_process_done(m, isr);
607 IPSECREQUEST_UNLOCK(isr);
612 IPSECREQUEST_UNLOCK(isr);
620 static struct xformsw ipcomp_xformsw = {
621 XF_IPCOMP, XFT_COMP, "IPcomp",
622 ipcomp_init, ipcomp_zeroize, ipcomp_input,
630 xform_register(&ipcomp_xformsw);
633 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
636 vnet_ipcomp_attach(const void *unused __unused)
639 V_ipcompstat.version = IPCOMPSTAT_VERSION;
642 VNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
643 vnet_ipcomp_attach, NULL);