2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* IP payload compression protocol (IPComp), see RFC 2393 */
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/mutex.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/protosw.h>
46 #include <sys/sysctl.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/ip_var.h>
52 #include <netinet/ip_encap.h>
54 #include <net/netisr.h>
56 #include <net/if.h> /* XXXGL: net_epoch should move out there */
57 #include <net/if_var.h> /* XXXGL: net_epoch should move out there */
59 #include <netipsec/ipsec.h>
60 #include <netipsec/xform.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netipsec/ipsec6.h>
68 #include <netipsec/ipcomp.h>
69 #include <netipsec/ipcomp_var.h>
71 #include <netipsec/key.h>
72 #include <netipsec/key_debug.h>
74 #include <opencrypto/cryptodev.h>
75 #include <opencrypto/deflate.h>
76 #include <opencrypto/xform.h>
78 VNET_DEFINE(int, ipcomp_enable) = 1;
79 VNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
80 VNET_PCPUSTAT_SYSINIT(ipcompstat);
83 VNET_PCPUSTAT_SYSUNINIT(ipcompstat);
86 SYSCTL_DECL(_net_inet_ipcomp);
87 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable,
88 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
89 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
90 struct ipcompstat, ipcompstat,
91 "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
93 static int ipcomp_input_cb(struct cryptop *crp);
94 static int ipcomp_output_cb(struct cryptop *crp);
97 * RFC 3173 p 2.2. Non-Expansion Policy:
98 * If the total size of a compressed payload and the IPComp header, as
99 * defined in section 3, is not smaller than the size of the original
100 * payload, the IP datagram MUST be sent in the original non-compressed
103 * When we use IPComp in tunnel mode, for small packets we will receive
104 * encapsulated IP-IP datagrams without any compression and without IPComp
108 ipcomp_encapcheck(union sockaddr_union *src, union sockaddr_union *dst)
110 struct secasvar *sav;
112 sav = key_allocsa_tunnel(src, dst, IPPROTO_IPCOMP);
117 if (src->sa.sa_family == AF_INET)
118 return (sizeof(struct in_addr) << 4);
120 return (sizeof(struct in6_addr) << 4);
124 ipcomp_nonexp_input(struct mbuf *m, int off, int proto, void *arg __unused)
142 IPCOMPSTAT_INC(ipcomps_nopf);
144 return (IPPROTO_DONE);
147 IPCOMPSTAT_ADD(ipcomps_ibytes, m->m_pkthdr.len);
148 IPCOMPSTAT_INC(ipcomps_input);
149 netisr_dispatch(isr, m);
150 return (IPPROTO_DONE);
154 * ipcomp_init() is called when an CPI is being set up.
157 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
159 const struct comp_algo *tcomp;
160 struct crypto_session_params csp;
162 /* NB: algorithm really comes in alg_enc and not alg_comp! */
163 tcomp = comp_algorithm_lookup(sav->alg_enc);
165 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
169 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
170 sav->tdb_xform = xsp;
171 sav->tdb_compalgxform = tcomp;
173 /* Initialize crypto session */
174 memset(&csp, 0, sizeof(csp));
175 csp.csp_mode = CSP_MODE_COMPRESS;
176 csp.csp_cipher_alg = sav->tdb_compalgxform->type;
178 return crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
182 * ipcomp_zeroize() used when IPCA is deleted
185 ipcomp_zeroize(struct secasvar *sav)
188 crypto_freesession(sav->tdb_cryptoid);
189 sav->tdb_cryptoid = NULL;
194 * ipcomp_input() gets called to uncompress an input packet
197 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
199 struct xform_data *xd;
201 struct ipcomp *ipcomp;
202 crypto_session_t cryptoid;
204 int error, hlen = IPCOMP_HLENGTH;
207 * Check that the next header of the IPComp is not IPComp again, before
208 * doing any real work. Given it is not possible to do double
209 * compression it means someone is playing tricks on us.
212 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
213 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/
214 DPRINTF(("%s: m_pullup failed\n", __func__));
218 addr = (caddr_t) mtod(m, struct ip *) + skip;
219 ipcomp = (struct ipcomp *)addr;
220 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
221 IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */
222 DPRINTF(("%s: recursive compression detected\n", __func__));
228 cryptoid = sav->tdb_cryptoid;
229 SECASVAR_UNLOCK(sav);
231 /* Get crypto descriptors */
232 crp = crypto_getreq(cryptoid, M_NOWAIT);
234 DPRINTF(("%s: no crypto descriptors\n", __func__));
235 IPCOMPSTAT_INC(ipcomps_crypto);
238 /* Get IPsec-specific opaque pointer */
239 xd = malloc(sizeof(*xd), M_XDATA, M_NOWAIT | M_ZERO);
241 DPRINTF(("%s: cannot allocate xform_data\n", __func__));
242 IPCOMPSTAT_INC(ipcomps_crypto);
247 /* Decompression operation */
248 crp->crp_op = CRYPTO_OP_DECOMPRESS;
249 crp->crp_payload_start = skip + hlen;
250 crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen);
252 /* Crypto operation descriptor */
253 crp->crp_flags = CRYPTO_F_CBIFSYNC;
254 crypto_use_mbuf(crp, m);
255 crp->crp_callback = ipcomp_input_cb;
256 crp->crp_opaque = xd;
258 /* These are passed as-is to the callback */
260 xd->protoff = protoff;
263 xd->cryptoid = cryptoid;
266 crp->crp_session = xd->cryptoid = sav->tdb_cryptoid;
267 SECASVAR_UNLOCK(sav);
269 return crypto_dispatch(crp);
277 * IPComp input callback from the crypto driver.
280 ipcomp_input_cb(struct cryptop *crp)
282 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
283 struct xform_data *xd;
285 struct secasvar *sav;
286 struct secasindex *saidx;
288 crypto_session_t cryptoid;
289 int hlen = IPCOMP_HLENGTH, error, clen;
293 m = crp->crp_buf.cb_mbuf;
294 xd = crp->crp_opaque;
295 CURVNET_SET(xd->vnet);
298 protoff = xd->protoff;
299 cryptoid = xd->cryptoid;
300 saidx = &sav->sah->saidx;
301 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
302 saidx->dst.sa.sa_family == AF_INET6,
303 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
305 /* Check for crypto errors */
306 if (crp->crp_etype) {
307 if (crp->crp_etype == EAGAIN) {
308 /* Reset the session ID */
309 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
310 crypto_freesession(cryptoid);
311 xd->cryptoid = crp->crp_session;
313 return (crypto_dispatch(crp));
315 IPCOMPSTAT_INC(ipcomps_noxform);
316 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
317 error = crp->crp_etype;
320 /* Shouldn't happen... */
322 IPCOMPSTAT_INC(ipcomps_crypto);
323 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
327 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
329 clen = crp->crp_olen; /* Length of data after processing */
331 /* Release the crypto descriptors */
332 free(xd, M_XDATA), xd = NULL;
333 crypto_freereq(crp), crp = NULL;
335 /* In case it's not done already, adjust the size of the mbuf chain */
336 m->m_pkthdr.len = clen + hlen + skip;
338 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
339 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/
340 DPRINTF(("%s: m_pullup failed\n", __func__));
341 error = EINVAL; /*XXX*/
345 /* Keep the next protocol field */
346 addr = (caddr_t) mtod(m, struct ip *) + skip;
347 nproto = ((struct ipcomp *) addr)->comp_nxt;
349 /* Remove the IPCOMP header */
350 error = m_striphdr(m, skip, hlen);
352 IPCOMPSTAT_INC(ipcomps_hdrops);
353 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
354 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
355 (u_long) ntohl(sav->spi)));
359 /* Restore the Next Protocol field */
360 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
362 switch (saidx->dst.sa.sa_family) {
365 error = ipsec6_common_input_cb(m, sav, skip, protoff);
370 error = ipsec4_common_input_cb(m, sav, skip, protoff);
374 panic("%s: Unexpected address family: %d saidx=%p", __func__,
375 saidx->dst.sa.sa_family, saidx);
393 * IPComp output routine, called by ipsec[46]_perform_request()
396 ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
397 u_int idx, int skip, int protoff)
399 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
400 const struct comp_algo *ipcompx;
402 struct xform_data *xd;
403 crypto_session_t cryptoid;
404 int error, ralen, maxpacketsize;
406 IPSEC_ASSERT(sav != NULL, ("null SA"));
407 ipcompx = sav->tdb_compalgxform;
408 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
411 * Do not touch the packet in case our payload to compress
412 * is lower than the minimal threshold of the compression
413 * alogrithm. We will just send out the data uncompressed.
414 * See RFC 3173, 2.2. Non-Expansion Policy.
416 if (m->m_pkthdr.len <= ipcompx->minlen) {
417 IPCOMPSTAT_INC(ipcomps_threshold);
418 return ipsec_process_done(m, sp, sav, idx);
421 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
422 IPCOMPSTAT_INC(ipcomps_output);
424 /* Check for maximum packet size violations. */
425 switch (sav->sah->saidx.dst.sa.sa_family) {
428 maxpacketsize = IP_MAXPACKET;
433 maxpacketsize = IPV6_MAXPACKET;
437 IPCOMPSTAT_INC(ipcomps_nopf);
438 DPRINTF(("%s: unknown/unsupported protocol family %d, "
439 "IPCA %s/%08lx\n", __func__,
440 sav->sah->saidx.dst.sa.sa_family,
441 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
442 (u_long) ntohl(sav->spi)));
443 error = EPFNOSUPPORT;
446 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
447 IPCOMPSTAT_INC(ipcomps_toobig);
448 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
449 "(len %u, max len %u)\n", __func__,
450 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
451 (u_long) ntohl(sav->spi),
452 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
457 /* Update the counters */
458 IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
460 m = m_unshare(m, M_NOWAIT);
462 IPCOMPSTAT_INC(ipcomps_hdrops);
463 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
464 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
465 sizeof(buf)), (u_long) ntohl(sav->spi)));
470 /* Ok now, we can pass to the crypto processing. */
472 cryptoid = sav->tdb_cryptoid;
473 SECASVAR_UNLOCK(sav);
475 /* Get crypto descriptors */
476 crp = crypto_getreq(cryptoid, M_NOWAIT);
478 IPCOMPSTAT_INC(ipcomps_crypto);
479 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
484 /* Compression descriptor */
485 crp->crp_op = CRYPTO_OP_COMPRESS;
486 crp->crp_payload_start = skip;
487 crp->crp_payload_length = ralen;
489 /* IPsec-specific opaque crypto info */
490 xd = malloc(sizeof(struct xform_data), M_XDATA, M_NOWAIT | M_ZERO);
492 IPCOMPSTAT_INC(ipcomps_crypto);
493 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
503 xd->protoff = protoff;
505 xd->cryptoid = cryptoid;
507 /* Crypto operation descriptor */
508 crp->crp_flags = CRYPTO_F_CBIFSYNC;
509 crypto_use_mbuf(crp, m);
510 crp->crp_callback = ipcomp_output_cb;
511 crp->crp_opaque = xd;
513 return crypto_dispatch(crp);
523 * IPComp output callback from the crypto driver.
526 ipcomp_output_cb(struct cryptop *crp)
528 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
529 struct xform_data *xd;
530 struct secpolicy *sp;
531 struct secasvar *sav;
533 crypto_session_t cryptoid;
535 int error, skip, protoff;
537 m = crp->crp_buf.cb_mbuf;
538 xd = crp->crp_opaque;
539 CURVNET_SET(xd->vnet);
544 protoff = xd->protoff;
545 cryptoid = xd->cryptoid;
547 /* Check for crypto errors */
548 if (crp->crp_etype) {
549 if (crp->crp_etype == EAGAIN) {
550 /* Reset the session ID */
551 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
552 crypto_freesession(cryptoid);
553 xd->cryptoid = crp->crp_session;
555 return (crypto_dispatch(crp));
557 IPCOMPSTAT_INC(ipcomps_noxform);
558 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
559 error = crp->crp_etype;
562 /* Shouldn't happen... */
564 IPCOMPSTAT_INC(ipcomps_crypto);
565 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
569 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
571 if (crp->crp_payload_length > crp->crp_olen) {
573 struct ipcomp *ipcomp;
577 /* Compression helped, inject IPCOMP header. */
578 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
580 IPCOMPSTAT_INC(ipcomps_wrap);
581 DPRINTF(("%s: IPCOMP header inject failed "
582 "for IPCA %s/%08lx\n",
583 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
584 sizeof(buf)), (u_long) ntohl(sav->spi)));
588 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
590 /* Initialize the IPCOMP header */
591 /* XXX alignment always correct? */
592 switch (sav->sah->saidx.dst.sa.sa_family) {
595 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
600 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
604 ipcomp->comp_flags = 0;
605 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
607 /* Fix Next Protocol in IPv4/IPv6 header */
608 prot = IPPROTO_IPCOMP;
609 m_copyback(m, protoff, sizeof(u_int8_t),
612 /* Adjust the length in the IP header */
613 switch (sav->sah->saidx.dst.sa.sa_family) {
616 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
621 mtod(m, struct ip6_hdr *)->ip6_plen =
622 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
626 IPCOMPSTAT_INC(ipcomps_nopf);
627 DPRINTF(("%s: unknown/unsupported protocol "
628 "family %d, IPCA %s/%08lx\n", __func__,
629 sav->sah->saidx.dst.sa.sa_family,
630 ipsec_address(&sav->sah->saidx.dst, buf,
631 sizeof(buf)), (u_long) ntohl(sav->spi)));
632 error = EPFNOSUPPORT;
636 /* Compression was useless, we have lost time. */
637 IPCOMPSTAT_INC(ipcomps_uncompr);
638 DPRINTF(("%s: compressions was useless %d <= %d\n",
639 __func__, crp->crp_payload_length, crp->crp_olen));
640 /* XXX remember state to not compress the next couple
641 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
644 /* Release the crypto descriptor */
648 /* NB: m is reclaimed by ipsec_process_done. */
649 error = ipsec_process_done(m, sp, sav, idx);
665 ipcomp4_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
668 union sockaddr_union src, dst;
671 if (V_ipcomp_enable == 0)
673 if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
675 bzero(&src, sizeof(src));
676 bzero(&dst, sizeof(dst));
677 src.sa.sa_family = dst.sa.sa_family = AF_INET;
678 src.sin.sin_len = dst.sin.sin_len = sizeof(struct sockaddr_in);
679 ip = mtod(m, const struct ip *);
680 src.sin.sin_addr = ip->ip_src;
681 dst.sin.sin_addr = ip->ip_dst;
682 return (ipcomp_encapcheck(&src, &dst));
685 static const struct encaptab *ipe4_cookie = NULL;
686 static const struct encap_config ipv4_encap_cfg = {
688 .min_length = sizeof(struct ip),
689 .exact_match = sizeof(in_addr_t) << 4,
690 .check = ipcomp4_nonexp_encapcheck,
691 .input = ipcomp_nonexp_input
696 ipcomp6_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
699 union sockaddr_union src, dst;
700 const struct ip6_hdr *ip6;
702 if (V_ipcomp_enable == 0)
704 if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
706 bzero(&src, sizeof(src));
707 bzero(&dst, sizeof(dst));
708 src.sa.sa_family = dst.sa.sa_family = AF_INET;
709 src.sin6.sin6_len = dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
710 ip6 = mtod(m, const struct ip6_hdr *);
711 src.sin6.sin6_addr = ip6->ip6_src;
712 dst.sin6.sin6_addr = ip6->ip6_dst;
713 if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6.sin6_addr)) {
714 /* XXX: sa6_recoverscope() */
715 src.sin6.sin6_scope_id =
716 ntohs(src.sin6.sin6_addr.s6_addr16[1]);
717 src.sin6.sin6_addr.s6_addr16[1] = 0;
719 if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6.sin6_addr)) {
720 /* XXX: sa6_recoverscope() */
721 dst.sin6.sin6_scope_id =
722 ntohs(dst.sin6.sin6_addr.s6_addr16[1]);
723 dst.sin6.sin6_addr.s6_addr16[1] = 0;
725 return (ipcomp_encapcheck(&src, &dst));
728 static const struct encaptab *ipe6_cookie = NULL;
729 static const struct encap_config ipv6_encap_cfg = {
731 .min_length = sizeof(struct ip6_hdr),
732 .exact_match = sizeof(struct in6_addr) << 4,
733 .check = ipcomp6_nonexp_encapcheck,
734 .input = ipcomp_nonexp_input
738 static struct xformsw ipcomp_xformsw = {
739 .xf_type = XF_IPCOMP,
741 .xf_init = ipcomp_init,
742 .xf_zeroize = ipcomp_zeroize,
743 .xf_input = ipcomp_input,
744 .xf_output = ipcomp_output,
752 ipe4_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
755 ipe6_cookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
757 xform_attach(&ipcomp_xformsw);
765 ip_encap_detach(ipe4_cookie);
768 ip6_encap_detach(ipe6_cookie);
770 xform_detach(&ipcomp_xformsw);
773 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
774 ipcomp_attach, NULL);
775 SYSUNINIT(ipcomp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
776 ipcomp_detach, NULL);