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 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
218 if (saidx->dst.sa.sa_family == AF_INET6) { \
219 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
221 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
225 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
226 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
230 * IPComp input callback from the crypto driver.
233 ipcomp_input_cb(struct cryptop *crp)
235 struct cryptodesc *crd;
236 struct tdb_crypto *tc;
240 struct secasvar *sav;
241 struct secasindex *saidx;
242 int hlen = IPCOMP_HLENGTH, error, clen;
248 tc = (struct tdb_crypto *) crp->crp_opaque;
249 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
251 protoff = tc->tc_protoff;
252 mtag = (struct mtag *) tc->tc_ptr;
253 m = (struct mbuf *) crp->crp_buf;
256 IPSEC_ASSERT(sav != NULL, ("null SA!"));
258 saidx = &sav->sah->saidx;
259 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
260 saidx->dst.sa.sa_family == AF_INET6,
261 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
263 /* Check for crypto errors */
264 if (crp->crp_etype) {
265 /* Reset the session ID */
266 if (sav->tdb_cryptoid != 0)
267 sav->tdb_cryptoid = crp->crp_sid;
269 if (crp->crp_etype == EAGAIN) {
270 return crypto_dispatch(crp);
272 V_ipcompstat.ipcomps_noxform++;
273 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
274 error = crp->crp_etype;
277 /* Shouldn't happen... */
279 V_ipcompstat.ipcomps_crypto++;
280 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
284 V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
286 clen = crp->crp_olen; /* Length of data after processing */
288 /* Release the crypto descriptors */
289 free(tc, M_XDATA), tc = NULL;
290 crypto_freereq(crp), crp = NULL;
292 /* In case it's not done already, adjust the size of the mbuf chain */
293 m->m_pkthdr.len = clen + hlen + skip;
295 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
296 V_ipcompstat.ipcomps_hdrops++; /*XXX*/
297 DPRINTF(("%s: m_pullup failed\n", __func__));
298 error = EINVAL; /*XXX*/
302 /* Keep the next protocol field */
303 addr = (caddr_t) mtod(m, struct ip *) + skip;
304 nproto = ((struct ipcomp *) addr)->comp_nxt;
306 /* Remove the IPCOMP header */
307 error = m_striphdr(m, skip, hlen);
309 V_ipcompstat.ipcomps_hdrops++;
310 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
311 ipsec_address(&sav->sah->saidx.dst),
312 (u_long) ntohl(sav->spi)));
316 /* Restore the Next Protocol field */
317 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
319 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
336 * IPComp output routine, called by ipsec[46]_process_packet()
341 struct ipsecrequest *isr,
347 struct secasvar *sav;
348 struct comp_algo *ipcompx;
349 int error, ralen, maxpacketsize;
350 struct cryptodesc *crdc;
352 struct tdb_crypto *tc;
355 IPSEC_ASSERT(sav != NULL, ("null SA"));
356 ipcompx = sav->tdb_compalgxform;
357 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
360 * Do not touch the packet in case our payload to compress
361 * is lower than the minimal threshold of the compression
362 * alogrithm. We will just send out the data uncompressed.
363 * See RFC 3173, 2.2. Non-Expansion Policy.
365 if (m->m_pkthdr.len <= ipcompx->minlen) {
366 V_ipcompstat.ipcomps_threshold++;
367 return ipsec_process_done(m, isr);
370 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
371 V_ipcompstat.ipcomps_output++;
373 /* Check for maximum packet size violations. */
374 switch (sav->sah->saidx.dst.sa.sa_family) {
377 maxpacketsize = IP_MAXPACKET;
382 maxpacketsize = IPV6_MAXPACKET;
386 V_ipcompstat.ipcomps_nopf++;
387 DPRINTF(("%s: unknown/unsupported protocol family %d, "
388 "IPCA %s/%08lx\n", __func__,
389 sav->sah->saidx.dst.sa.sa_family,
390 ipsec_address(&sav->sah->saidx.dst),
391 (u_long) ntohl(sav->spi)));
392 error = EPFNOSUPPORT;
395 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
396 V_ipcompstat.ipcomps_toobig++;
397 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
398 "(len %u, max len %u)\n", __func__,
399 ipsec_address(&sav->sah->saidx.dst),
400 (u_long) ntohl(sav->spi),
401 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
406 /* Update the counters */
407 V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
409 m = m_unshare(m, M_NOWAIT);
411 V_ipcompstat.ipcomps_hdrops++;
412 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
413 __func__, ipsec_address(&sav->sah->saidx.dst),
414 (u_long) ntohl(sav->spi)));
419 /* Ok now, we can pass to the crypto processing. */
421 /* Get crypto descriptors */
422 crp = crypto_getreq(1);
424 V_ipcompstat.ipcomps_crypto++;
425 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
429 crdc = crp->crp_desc;
431 /* Compression descriptor */
432 crdc->crd_skip = skip;
433 crdc->crd_len = ralen;
434 crdc->crd_flags = CRD_F_COMP;
435 crdc->crd_inject = skip;
437 /* Compression operation */
438 crdc->crd_alg = ipcompx->type;
440 /* IPsec-specific opaque crypto info */
441 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
442 M_XDATA, M_NOWAIT|M_ZERO);
444 V_ipcompstat.ipcomps_crypto++;
445 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
454 tc->tc_spi = sav->spi;
455 tc->tc_dst = sav->sah->saidx.dst;
456 tc->tc_proto = sav->sah->saidx.proto;
457 tc->tc_protoff = protoff;
460 /* Crypto operation descriptor */
461 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
462 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
463 crp->crp_buf = (caddr_t) m;
464 crp->crp_callback = ipcomp_output_cb;
465 crp->crp_opaque = (caddr_t) tc;
466 crp->crp_sid = sav->tdb_cryptoid;
468 return crypto_dispatch(crp);
476 * IPComp output callback from the crypto driver.
479 ipcomp_output_cb(struct cryptop *crp)
481 struct tdb_crypto *tc;
482 struct ipsecrequest *isr;
483 struct secasvar *sav;
487 tc = (struct tdb_crypto *) crp->crp_opaque;
488 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
489 m = (struct mbuf *) crp->crp_buf;
493 IPSECREQUEST_LOCK(isr);
495 /* With the isr lock released SA pointer can be updated. */
496 if (sav != isr->sav) {
497 V_ipcompstat.ipcomps_notdb++;
498 DPRINTF(("%s: SA expired while in crypto\n", __func__));
499 error = ENOBUFS; /*XXX*/
503 /* Check for crypto errors */
504 if (crp->crp_etype) {
505 /* Reset the session ID */
506 if (sav->tdb_cryptoid != 0)
507 sav->tdb_cryptoid = crp->crp_sid;
509 if (crp->crp_etype == EAGAIN) {
510 IPSECREQUEST_UNLOCK(isr);
511 return crypto_dispatch(crp);
513 V_ipcompstat.ipcomps_noxform++;
514 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
515 error = crp->crp_etype;
518 /* Shouldn't happen... */
520 V_ipcompstat.ipcomps_crypto++;
521 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
525 V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
527 if (crp->crp_ilen - skip > crp->crp_olen) {
529 struct ipcomp *ipcomp;
533 /* Compression helped, inject IPCOMP header. */
534 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
536 V_ipcompstat.ipcomps_wrap++;
537 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
538 __func__, ipsec_address(&sav->sah->saidx.dst),
539 (u_long) ntohl(sav->spi)));
543 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
545 /* Initialize the IPCOMP header */
546 /* XXX alignment always correct? */
547 switch (sav->sah->saidx.dst.sa.sa_family) {
550 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
555 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
559 ipcomp->comp_flags = 0;
560 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
562 /* Fix Next Protocol in IPv4/IPv6 header */
563 prot = IPPROTO_IPCOMP;
564 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
567 /* Adjust the length in the IP header */
568 switch (sav->sah->saidx.dst.sa.sa_family) {
571 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
576 mtod(m, struct ip6_hdr *)->ip6_plen =
577 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
581 V_ipcompstat.ipcomps_nopf++;
582 DPRINTF(("%s: unknown/unsupported protocol "
583 "family %d, IPCA %s/%08lx\n", __func__,
584 sav->sah->saidx.dst.sa.sa_family,
585 ipsec_address(&sav->sah->saidx.dst),
586 (u_long) ntohl(sav->spi)));
587 error = EPFNOSUPPORT;
591 /* Compression was useless, we have lost time. */
592 V_ipcompstat.ipcomps_uncompr++;
593 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
594 __func__, crp->crp_ilen, skip, crp->crp_olen));
595 /* XXX remember state to not compress the next couple
596 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
599 /* Release the crypto descriptor */
603 /* NB: m is reclaimed by ipsec_process_done. */
604 error = ipsec_process_done(m, isr);
606 IPSECREQUEST_UNLOCK(isr);
611 IPSECREQUEST_UNLOCK(isr);
619 static struct xformsw ipcomp_xformsw = {
620 XF_IPCOMP, XFT_COMP, "IPcomp",
621 ipcomp_init, ipcomp_zeroize, ipcomp_input,
629 xform_register(&ipcomp_xformsw);
632 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
635 vnet_ipcomp_attach(const void *unused __unused)
638 V_ipcompstat.version = IPCOMPSTAT_VERSION;
641 VNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
642 vnet_ipcomp_attach, NULL);