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/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/protosw.h>
43 #include <sys/sysctl.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>
53 #include <netipsec/ipsec.h>
54 #include <netipsec/xform.h>
57 #include <netinet/ip6.h>
58 #include <netipsec/ipsec6.h>
61 #include <netipsec/ipcomp.h>
62 #include <netipsec/ipcomp_var.h>
64 #include <netipsec/key.h>
65 #include <netipsec/key_debug.h>
67 #include <opencrypto/cryptodev.h>
68 #include <opencrypto/deflate.h>
69 #include <opencrypto/xform.h>
71 VNET_DEFINE(int, ipcomp_enable) = 1;
72 VNET_DEFINE(struct ipcompstat, ipcompstat);
74 SYSCTL_DECL(_net_inet_ipcomp);
75 SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
76 ipcomp_enable, CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
77 SYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
78 stats, CTLFLAG_RD, &VNET_NAME(ipcompstat), ipcompstat, "");
80 static int ipcomp_input_cb(struct cryptop *crp);
81 static int ipcomp_output_cb(struct cryptop *crp);
84 ipcomp_algorithm_lookup(int alg)
86 if (alg >= IPCOMP_ALG_MAX)
89 case SADB_X_CALG_DEFLATE:
90 return &comp_algo_deflate;
96 * ipcomp_init() is called when an CPI is being set up.
99 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
101 struct comp_algo *tcomp;
102 struct cryptoini cric;
104 /* NB: algorithm really comes in alg_enc and not alg_comp! */
105 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
107 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
111 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
112 sav->tdb_xform = xsp;
113 sav->tdb_compalgxform = tcomp;
115 /* Initialize crypto session */
116 bzero(&cric, sizeof (cric));
117 cric.cri_alg = sav->tdb_compalgxform->type;
119 return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
123 * ipcomp_zeroize() used when IPCA is deleted
126 ipcomp_zeroize(struct secasvar *sav)
130 err = crypto_freesession(sav->tdb_cryptoid);
131 sav->tdb_cryptoid = 0;
136 * ipcomp_input() gets called to uncompress an input packet
139 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
141 struct tdb_crypto *tc;
142 struct cryptodesc *crdc;
144 int hlen = IPCOMP_HLENGTH;
146 /* Get crypto descriptors */
147 crp = crypto_getreq(1);
150 DPRINTF(("%s: no crypto descriptors\n", __func__));
151 V_ipcompstat.ipcomps_crypto++;
154 /* Get IPsec-specific opaque pointer */
155 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
159 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
160 V_ipcompstat.ipcomps_crypto++;
163 crdc = crp->crp_desc;
165 crdc->crd_skip = skip + hlen;
166 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
167 crdc->crd_inject = skip;
171 /* Decompression operation */
172 crdc->crd_alg = sav->tdb_compalgxform->type;
174 /* Crypto operation descriptor */
175 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
176 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
177 crp->crp_buf = (caddr_t) m;
178 crp->crp_callback = ipcomp_input_cb;
179 crp->crp_sid = sav->tdb_cryptoid;
180 crp->crp_opaque = (caddr_t) tc;
182 /* These are passed as-is to the callback */
183 tc->tc_spi = sav->spi;
184 tc->tc_dst = sav->sah->saidx.dst;
185 tc->tc_proto = sav->sah->saidx.proto;
186 tc->tc_protoff = protoff;
189 return crypto_dispatch(crp);
193 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
194 if (saidx->dst.sa.sa_family == AF_INET6) { \
195 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
197 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
201 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
202 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
206 * IPComp input callback from the crypto driver.
209 ipcomp_input_cb(struct cryptop *crp)
211 struct cryptodesc *crd;
212 struct tdb_crypto *tc;
216 struct secasvar *sav;
217 struct secasindex *saidx;
218 int hlen = IPCOMP_HLENGTH, error, clen;
224 tc = (struct tdb_crypto *) crp->crp_opaque;
225 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
227 protoff = tc->tc_protoff;
228 mtag = (struct mtag *) tc->tc_ptr;
229 m = (struct mbuf *) crp->crp_buf;
231 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
233 V_ipcompstat.ipcomps_notdb++;
234 DPRINTF(("%s: SA expired while in crypto\n", __func__));
235 error = ENOBUFS; /*XXX*/
239 saidx = &sav->sah->saidx;
240 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
241 saidx->dst.sa.sa_family == AF_INET6,
242 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
244 /* Check for crypto errors */
245 if (crp->crp_etype) {
246 /* Reset the session ID */
247 if (sav->tdb_cryptoid != 0)
248 sav->tdb_cryptoid = crp->crp_sid;
250 if (crp->crp_etype == EAGAIN) {
252 return crypto_dispatch(crp);
254 V_ipcompstat.ipcomps_noxform++;
255 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
256 error = crp->crp_etype;
259 /* Shouldn't happen... */
261 V_ipcompstat.ipcomps_crypto++;
262 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
266 V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
268 clen = crp->crp_olen; /* Length of data after processing */
270 /* Release the crypto descriptors */
271 free(tc, M_XDATA), tc = NULL;
272 crypto_freereq(crp), crp = NULL;
274 /* In case it's not done already, adjust the size of the mbuf chain */
275 m->m_pkthdr.len = clen + hlen + skip;
277 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
278 V_ipcompstat.ipcomps_hdrops++; /*XXX*/
279 DPRINTF(("%s: m_pullup failed\n", __func__));
280 error = EINVAL; /*XXX*/
284 /* Keep the next protocol field */
285 addr = (caddr_t) mtod(m, struct ip *) + skip;
286 nproto = ((struct ipcomp *) addr)->comp_nxt;
288 /* Remove the IPCOMP header */
289 error = m_striphdr(m, skip, hlen);
291 V_ipcompstat.ipcomps_hdrops++;
292 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
293 ipsec_address(&sav->sah->saidx.dst),
294 (u_long) ntohl(sav->spi)));
298 /* Restore the Next Protocol field */
299 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
301 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
318 * IPComp output routine, called by ipsec[46]_process_packet()
323 struct ipsecrequest *isr,
329 struct secasvar *sav;
330 struct comp_algo *ipcompx;
331 int error, ralen, maxpacketsize;
332 struct cryptodesc *crdc;
334 struct tdb_crypto *tc;
337 IPSEC_ASSERT(sav != NULL, ("null SA"));
338 ipcompx = sav->tdb_compalgxform;
339 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
342 * Do not touch the packet in case our payload to compress
343 * is lower than the minimal threshold of the compression
344 * alogrithm. We will just send out the data uncompressed.
345 * See RFC 3173, 2.2. Non-Expansion Policy.
347 if (m->m_pkthdr.len <= ipcompx->minlen) {
348 V_ipcompstat.ipcomps_threshold++;
349 return ipsec_process_done(m, isr);
352 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
353 V_ipcompstat.ipcomps_output++;
355 /* Check for maximum packet size violations. */
356 switch (sav->sah->saidx.dst.sa.sa_family) {
359 maxpacketsize = IP_MAXPACKET;
364 maxpacketsize = IPV6_MAXPACKET;
368 V_ipcompstat.ipcomps_nopf++;
369 DPRINTF(("%s: unknown/unsupported protocol family %d, "
370 "IPCA %s/%08lx\n", __func__,
371 sav->sah->saidx.dst.sa.sa_family,
372 ipsec_address(&sav->sah->saidx.dst),
373 (u_long) ntohl(sav->spi)));
374 error = EPFNOSUPPORT;
377 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
378 V_ipcompstat.ipcomps_toobig++;
379 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
380 "(len %u, max len %u)\n", __func__,
381 ipsec_address(&sav->sah->saidx.dst),
382 (u_long) ntohl(sav->spi),
383 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
388 /* Update the counters */
389 V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
391 m = m_unshare(m, M_NOWAIT);
393 V_ipcompstat.ipcomps_hdrops++;
394 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
395 __func__, ipsec_address(&sav->sah->saidx.dst),
396 (u_long) ntohl(sav->spi)));
401 /* Ok now, we can pass to the crypto processing. */
403 /* Get crypto descriptors */
404 crp = crypto_getreq(1);
406 V_ipcompstat.ipcomps_crypto++;
407 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
411 crdc = crp->crp_desc;
413 /* Compression descriptor */
414 crdc->crd_skip = skip;
415 crdc->crd_len = ralen;
416 crdc->crd_flags = CRD_F_COMP;
417 crdc->crd_inject = skip;
419 /* Compression operation */
420 crdc->crd_alg = ipcompx->type;
422 /* IPsec-specific opaque crypto info */
423 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
424 M_XDATA, M_NOWAIT|M_ZERO);
426 V_ipcompstat.ipcomps_crypto++;
427 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
434 tc->tc_spi = sav->spi;
435 tc->tc_dst = sav->sah->saidx.dst;
436 tc->tc_proto = sav->sah->saidx.proto;
437 tc->tc_protoff = protoff;
440 /* Crypto operation descriptor */
441 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
442 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
443 crp->crp_buf = (caddr_t) m;
444 crp->crp_callback = ipcomp_output_cb;
445 crp->crp_opaque = (caddr_t) tc;
446 crp->crp_sid = sav->tdb_cryptoid;
448 return crypto_dispatch(crp);
456 * IPComp output callback from the crypto driver.
459 ipcomp_output_cb(struct cryptop *crp)
461 struct tdb_crypto *tc;
462 struct ipsecrequest *isr;
463 struct secasvar *sav;
467 tc = (struct tdb_crypto *) crp->crp_opaque;
468 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
469 m = (struct mbuf *) crp->crp_buf;
473 IPSECREQUEST_LOCK(isr);
474 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
476 V_ipcompstat.ipcomps_notdb++;
477 DPRINTF(("%s: SA expired while in crypto\n", __func__));
478 error = ENOBUFS; /*XXX*/
481 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
483 /* Check for crypto errors */
484 if (crp->crp_etype) {
485 /* Reset the session ID */
486 if (sav->tdb_cryptoid != 0)
487 sav->tdb_cryptoid = crp->crp_sid;
489 if (crp->crp_etype == EAGAIN) {
491 IPSECREQUEST_UNLOCK(isr);
492 return crypto_dispatch(crp);
494 V_ipcompstat.ipcomps_noxform++;
495 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
496 error = crp->crp_etype;
499 /* Shouldn't happen... */
501 V_ipcompstat.ipcomps_crypto++;
502 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
506 V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
508 if (crp->crp_ilen - skip > crp->crp_olen) {
510 struct ipcomp *ipcomp;
514 /* Compression helped, inject IPCOMP header. */
515 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
517 V_ipcompstat.ipcomps_wrap++;
518 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
519 __func__, ipsec_address(&sav->sah->saidx.dst),
520 (u_long) ntohl(sav->spi)));
524 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
526 /* Initialize the IPCOMP header */
527 /* XXX alignment always correct? */
528 switch (sav->sah->saidx.dst.sa.sa_family) {
531 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
536 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
540 ipcomp->comp_flags = 0;
541 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
543 /* Fix Next Protocol in IPv4/IPv6 header */
544 prot = IPPROTO_IPCOMP;
545 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
548 /* Adjust the length in the IP header */
549 switch (sav->sah->saidx.dst.sa.sa_family) {
552 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
557 mtod(m, struct ip6_hdr *)->ip6_plen =
558 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
562 V_ipcompstat.ipcomps_nopf++;
563 DPRINTF(("%s: unknown/unsupported protocol "
564 "family %d, IPCA %s/%08lx\n", __func__,
565 sav->sah->saidx.dst.sa.sa_family,
566 ipsec_address(&sav->sah->saidx.dst),
567 (u_long) ntohl(sav->spi)));
568 error = EPFNOSUPPORT;
572 /* Compression was useless, we have lost time. */
573 V_ipcompstat.ipcomps_uncompr++;
574 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
575 __func__, crp->crp_ilen, skip, crp->crp_olen));
576 /* XXX remember state to not compress the next couple
577 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
580 /* Release the crypto descriptor */
584 /* NB: m is reclaimed by ipsec_process_done. */
585 error = ipsec_process_done(m, isr);
587 IPSECREQUEST_UNLOCK(isr);
592 IPSECREQUEST_UNLOCK(isr);
600 static struct xformsw ipcomp_xformsw = {
601 XF_IPCOMP, XFT_COMP, "IPcomp",
602 ipcomp_init, ipcomp_zeroize, ipcomp_input,
610 xform_register(&ipcomp_xformsw);
613 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
616 vnet_ipcomp_attach(const void *unused __unused)
619 V_ipcompstat.version = IPCOMPSTAT_VERSION;
622 VNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
623 vnet_ipcomp_attach, NULL);