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_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
74 VNET_PCPUSTAT_SYSINIT(ipcompstat);
77 VNET_PCPUSTAT_SYSUNINIT(ipcompstat);
80 SYSCTL_DECL(_net_inet_ipcomp);
81 SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
82 ipcomp_enable, CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
83 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
84 struct ipcompstat, ipcompstat,
85 "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
87 static int ipcomp_input_cb(struct cryptop *crp);
88 static int ipcomp_output_cb(struct cryptop *crp);
91 ipcomp_algorithm_lookup(int alg)
93 if (alg >= IPCOMP_ALG_MAX)
96 case SADB_X_CALG_DEFLATE:
97 return &comp_algo_deflate;
103 * ipcomp_init() is called when an CPI is being set up.
106 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
108 struct comp_algo *tcomp;
109 struct cryptoini cric;
111 /* NB: algorithm really comes in alg_enc and not alg_comp! */
112 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
114 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
118 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
119 sav->tdb_xform = xsp;
120 sav->tdb_compalgxform = tcomp;
122 /* Initialize crypto session */
123 bzero(&cric, sizeof (cric));
124 cric.cri_alg = sav->tdb_compalgxform->type;
126 return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
130 * ipcomp_zeroize() used when IPCA is deleted
133 ipcomp_zeroize(struct secasvar *sav)
137 err = crypto_freesession(sav->tdb_cryptoid);
138 sav->tdb_cryptoid = 0;
143 * ipcomp_input() gets called to uncompress an input packet
146 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
148 struct tdb_crypto *tc;
149 struct cryptodesc *crdc;
151 struct ipcomp *ipcomp;
153 int hlen = IPCOMP_HLENGTH;
156 * Check that the next header of the IPComp is not IPComp again, before
157 * doing any real work. Given it is not possible to do double
158 * compression it means someone is playing tricks on us.
160 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
161 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/
162 DPRINTF(("%s: m_pullup failed\n", __func__));
165 addr = (caddr_t) mtod(m, struct ip *) + skip;
166 ipcomp = (struct ipcomp *)addr;
167 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
169 IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */
170 DPRINTF(("%s: recursive compression detected\n", __func__));
174 /* Get crypto descriptors */
175 crp = crypto_getreq(1);
178 DPRINTF(("%s: no crypto descriptors\n", __func__));
179 IPCOMPSTAT_INC(ipcomps_crypto);
182 /* Get IPsec-specific opaque pointer */
183 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
187 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
188 IPCOMPSTAT_INC(ipcomps_crypto);
191 crdc = crp->crp_desc;
193 crdc->crd_skip = skip + hlen;
194 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
195 crdc->crd_inject = skip;
199 /* Decompression operation */
200 crdc->crd_alg = sav->tdb_compalgxform->type;
202 /* Crypto operation descriptor */
203 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
204 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
205 crp->crp_buf = (caddr_t) m;
206 crp->crp_callback = ipcomp_input_cb;
207 crp->crp_sid = sav->tdb_cryptoid;
208 crp->crp_opaque = (caddr_t) tc;
210 /* These are passed as-is to the callback */
211 tc->tc_spi = sav->spi;
212 tc->tc_dst = sav->sah->saidx.dst;
213 tc->tc_proto = sav->sah->saidx.proto;
214 tc->tc_protoff = protoff;
219 return crypto_dispatch(crp);
223 * IPComp input callback from the crypto driver.
226 ipcomp_input_cb(struct cryptop *crp)
228 struct cryptodesc *crd;
229 struct tdb_crypto *tc;
233 struct secasvar *sav;
234 struct secasindex *saidx;
235 int hlen = IPCOMP_HLENGTH, error, clen;
241 tc = (struct tdb_crypto *) crp->crp_opaque;
242 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
244 protoff = tc->tc_protoff;
245 mtag = (struct mtag *) tc->tc_ptr;
246 m = (struct mbuf *) crp->crp_buf;
249 IPSEC_ASSERT(sav != NULL, ("null SA!"));
251 saidx = &sav->sah->saidx;
252 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
253 saidx->dst.sa.sa_family == AF_INET6,
254 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
256 /* Check for crypto errors */
257 if (crp->crp_etype) {
258 /* Reset the session ID */
259 if (sav->tdb_cryptoid != 0)
260 sav->tdb_cryptoid = crp->crp_sid;
262 if (crp->crp_etype == EAGAIN) {
263 return crypto_dispatch(crp);
265 IPCOMPSTAT_INC(ipcomps_noxform);
266 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
267 error = crp->crp_etype;
270 /* Shouldn't happen... */
272 IPCOMPSTAT_INC(ipcomps_crypto);
273 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
277 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
279 clen = crp->crp_olen; /* Length of data after processing */
281 /* Release the crypto descriptors */
282 free(tc, M_XDATA), tc = NULL;
283 crypto_freereq(crp), crp = NULL;
285 /* In case it's not done already, adjust the size of the mbuf chain */
286 m->m_pkthdr.len = clen + hlen + skip;
288 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
289 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/
290 DPRINTF(("%s: m_pullup failed\n", __func__));
291 error = EINVAL; /*XXX*/
295 /* Keep the next protocol field */
296 addr = (caddr_t) mtod(m, struct ip *) + skip;
297 nproto = ((struct ipcomp *) addr)->comp_nxt;
299 /* Remove the IPCOMP header */
300 error = m_striphdr(m, skip, hlen);
302 IPCOMPSTAT_INC(ipcomps_hdrops);
303 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
304 ipsec_address(&sav->sah->saidx.dst),
305 (u_long) ntohl(sav->spi)));
309 /* Restore the Next Protocol field */
310 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
312 switch (saidx->dst.sa.sa_family) {
315 error = ipsec6_common_input_cb(m, sav, skip, protoff, NULL);
320 error = ipsec4_common_input_cb(m, sav, skip, protoff, NULL);
324 panic("%s: Unexpected address family: %d saidx=%p", __func__,
325 saidx->dst.sa.sa_family, saidx);
343 * IPComp output routine, called by ipsec[46]_process_packet()
348 struct ipsecrequest *isr,
354 struct secasvar *sav;
355 struct comp_algo *ipcompx;
356 int error, ralen, maxpacketsize;
357 struct cryptodesc *crdc;
359 struct tdb_crypto *tc;
362 IPSEC_ASSERT(sav != NULL, ("null SA"));
363 ipcompx = sav->tdb_compalgxform;
364 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
367 * Do not touch the packet in case our payload to compress
368 * is lower than the minimal threshold of the compression
369 * alogrithm. We will just send out the data uncompressed.
370 * See RFC 3173, 2.2. Non-Expansion Policy.
372 if (m->m_pkthdr.len <= ipcompx->minlen) {
373 IPCOMPSTAT_INC(ipcomps_threshold);
374 return ipsec_process_done(m, isr);
377 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
378 IPCOMPSTAT_INC(ipcomps_output);
380 /* Check for maximum packet size violations. */
381 switch (sav->sah->saidx.dst.sa.sa_family) {
384 maxpacketsize = IP_MAXPACKET;
389 maxpacketsize = IPV6_MAXPACKET;
393 IPCOMPSTAT_INC(ipcomps_nopf);
394 DPRINTF(("%s: unknown/unsupported protocol family %d, "
395 "IPCA %s/%08lx\n", __func__,
396 sav->sah->saidx.dst.sa.sa_family,
397 ipsec_address(&sav->sah->saidx.dst),
398 (u_long) ntohl(sav->spi)));
399 error = EPFNOSUPPORT;
402 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
403 IPCOMPSTAT_INC(ipcomps_toobig);
404 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
405 "(len %u, max len %u)\n", __func__,
406 ipsec_address(&sav->sah->saidx.dst),
407 (u_long) ntohl(sav->spi),
408 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
413 /* Update the counters */
414 IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
416 m = m_unshare(m, M_NOWAIT);
418 IPCOMPSTAT_INC(ipcomps_hdrops);
419 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
420 __func__, ipsec_address(&sav->sah->saidx.dst),
421 (u_long) ntohl(sav->spi)));
426 /* Ok now, we can pass to the crypto processing. */
428 /* Get crypto descriptors */
429 crp = crypto_getreq(1);
431 IPCOMPSTAT_INC(ipcomps_crypto);
432 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
436 crdc = crp->crp_desc;
438 /* Compression descriptor */
439 crdc->crd_skip = skip;
440 crdc->crd_len = ralen;
441 crdc->crd_flags = CRD_F_COMP;
442 crdc->crd_inject = skip;
444 /* Compression operation */
445 crdc->crd_alg = ipcompx->type;
447 /* IPsec-specific opaque crypto info */
448 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
449 M_XDATA, M_NOWAIT|M_ZERO);
451 IPCOMPSTAT_INC(ipcomps_crypto);
452 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
461 tc->tc_spi = sav->spi;
462 tc->tc_dst = sav->sah->saidx.dst;
463 tc->tc_proto = sav->sah->saidx.proto;
464 tc->tc_protoff = protoff;
467 /* Crypto operation descriptor */
468 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
469 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
470 crp->crp_buf = (caddr_t) m;
471 crp->crp_callback = ipcomp_output_cb;
472 crp->crp_opaque = (caddr_t) tc;
473 crp->crp_sid = sav->tdb_cryptoid;
475 return crypto_dispatch(crp);
483 * IPComp output callback from the crypto driver.
486 ipcomp_output_cb(struct cryptop *crp)
488 struct tdb_crypto *tc;
489 struct ipsecrequest *isr;
490 struct secasvar *sav;
494 tc = (struct tdb_crypto *) crp->crp_opaque;
495 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
496 m = (struct mbuf *) crp->crp_buf;
500 IPSECREQUEST_LOCK(isr);
502 /* With the isr lock released SA pointer can be updated. */
503 if (sav != isr->sav) {
504 IPCOMPSTAT_INC(ipcomps_notdb);
505 DPRINTF(("%s: SA expired while in crypto\n", __func__));
506 error = ENOBUFS; /*XXX*/
510 /* Check for crypto errors */
511 if (crp->crp_etype) {
512 /* Reset the session ID */
513 if (sav->tdb_cryptoid != 0)
514 sav->tdb_cryptoid = crp->crp_sid;
516 if (crp->crp_etype == EAGAIN) {
517 IPSECREQUEST_UNLOCK(isr);
518 return crypto_dispatch(crp);
520 IPCOMPSTAT_INC(ipcomps_noxform);
521 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
522 error = crp->crp_etype;
525 /* Shouldn't happen... */
527 IPCOMPSTAT_INC(ipcomps_crypto);
528 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
532 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
534 if (crp->crp_ilen - skip > crp->crp_olen) {
536 struct ipcomp *ipcomp;
540 /* Compression helped, inject IPCOMP header. */
541 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
543 IPCOMPSTAT_INC(ipcomps_wrap);
544 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
545 __func__, ipsec_address(&sav->sah->saidx.dst),
546 (u_long) ntohl(sav->spi)));
550 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
552 /* Initialize the IPCOMP header */
553 /* XXX alignment always correct? */
554 switch (sav->sah->saidx.dst.sa.sa_family) {
557 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
562 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
566 ipcomp->comp_flags = 0;
567 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
569 /* Fix Next Protocol in IPv4/IPv6 header */
570 prot = IPPROTO_IPCOMP;
571 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
574 /* Adjust the length in the IP header */
575 switch (sav->sah->saidx.dst.sa.sa_family) {
578 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
583 mtod(m, struct ip6_hdr *)->ip6_plen =
584 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
588 IPCOMPSTAT_INC(ipcomps_nopf);
589 DPRINTF(("%s: unknown/unsupported protocol "
590 "family %d, IPCA %s/%08lx\n", __func__,
591 sav->sah->saidx.dst.sa.sa_family,
592 ipsec_address(&sav->sah->saidx.dst),
593 (u_long) ntohl(sav->spi)));
594 error = EPFNOSUPPORT;
598 /* Compression was useless, we have lost time. */
599 IPCOMPSTAT_INC(ipcomps_uncompr);
600 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
601 __func__, crp->crp_ilen, skip, crp->crp_olen));
602 /* XXX remember state to not compress the next couple
603 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
606 /* Release the crypto descriptor */
610 /* NB: m is reclaimed by ipsec_process_done. */
611 error = ipsec_process_done(m, isr);
613 IPSECREQUEST_UNLOCK(isr);
618 IPSECREQUEST_UNLOCK(isr);
626 static struct xformsw ipcomp_xformsw = {
627 XF_IPCOMP, XFT_COMP, "IPcomp",
628 ipcomp_init, ipcomp_zeroize, ipcomp_input,
636 xform_register(&ipcomp_xformsw);
639 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);