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"
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/mutex.h>
42 #include <sys/socket.h>
43 #include <sys/kernel.h>
44 #include <sys/protosw.h>
45 #include <sys/sysctl.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip_encap.h>
53 #include <net/netisr.h>
55 #include <net/if.h> /* XXXGL: net_epoch should move out there */
56 #include <net/if_var.h> /* XXXGL: net_epoch should move out there */
58 #include <netipsec/ipsec.h>
59 #include <netipsec/xform.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #include <netipsec/ipsec6.h>
67 #include <netipsec/ipcomp.h>
68 #include <netipsec/ipcomp_var.h>
70 #include <netipsec/key.h>
71 #include <netipsec/key_debug.h>
73 #include <opencrypto/cryptodev.h>
74 #include <opencrypto/deflate.h>
75 #include <opencrypto/xform.h>
77 VNET_DEFINE(int, ipcomp_enable) = 1;
78 VNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
79 VNET_PCPUSTAT_SYSINIT(ipcompstat);
82 VNET_PCPUSTAT_SYSUNINIT(ipcompstat);
85 SYSCTL_DECL(_net_inet_ipcomp);
86 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable,
87 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
88 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
89 struct ipcompstat, ipcompstat,
90 "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
92 static int ipcomp_input_cb(struct cryptop *crp);
93 static int ipcomp_output_cb(struct cryptop *crp);
96 * RFC 3173 p 2.2. Non-Expansion Policy:
97 * If the total size of a compressed payload and the IPComp header, as
98 * defined in section 3, is not smaller than the size of the original
99 * payload, the IP datagram MUST be sent in the original non-compressed
102 * When we use IPComp in tunnel mode, for small packets we will receive
103 * encapsulated IP-IP datagrams without any compression and without IPComp
107 ipcomp_encapcheck(union sockaddr_union *src, union sockaddr_union *dst)
109 struct secasvar *sav;
111 sav = key_allocsa_tunnel(src, dst, IPPROTO_IPCOMP);
116 if (src->sa.sa_family == AF_INET)
117 return (sizeof(struct in_addr) << 4);
119 return (sizeof(struct in6_addr) << 4);
123 ipcomp_nonexp_input(struct mbuf *m, int off, int proto, void *arg __unused)
141 IPCOMPSTAT_INC(ipcomps_nopf);
143 return (IPPROTO_DONE);
146 IPCOMPSTAT_ADD(ipcomps_ibytes, m->m_pkthdr.len);
147 IPCOMPSTAT_INC(ipcomps_input);
148 netisr_dispatch(isr, m);
149 return (IPPROTO_DONE);
153 * ipcomp_init() is called when an CPI is being set up.
156 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
158 const struct comp_algo *tcomp;
159 struct crypto_session_params csp;
161 /* NB: algorithm really comes in alg_enc and not alg_comp! */
162 tcomp = comp_algorithm_lookup(sav->alg_enc);
164 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
168 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
169 sav->tdb_xform = xsp;
170 sav->tdb_compalgxform = tcomp;
172 /* Initialize crypto session */
173 memset(&csp, 0, sizeof(csp));
174 csp.csp_mode = CSP_MODE_COMPRESS;
175 csp.csp_cipher_alg = sav->tdb_compalgxform->type;
177 return crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
181 * ipcomp_zeroize() used when IPCA is deleted
184 ipcomp_zeroize(struct secasvar *sav)
187 crypto_freesession(sav->tdb_cryptoid);
188 sav->tdb_cryptoid = NULL;
193 * ipcomp_input() gets called to uncompress an input packet
196 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
198 struct xform_data *xd;
200 struct ipcomp *ipcomp;
201 crypto_session_t cryptoid;
203 int error, hlen = IPCOMP_HLENGTH;
206 * Check that the next header of the IPComp is not IPComp again, before
207 * doing any real work. Given it is not possible to do double
208 * compression it means someone is playing tricks on us.
211 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
212 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/
213 DPRINTF(("%s: m_pullup failed\n", __func__));
217 addr = (caddr_t) mtod(m, struct ip *) + skip;
218 ipcomp = (struct ipcomp *)addr;
219 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
220 IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */
221 DPRINTF(("%s: recursive compression detected\n", __func__));
227 cryptoid = sav->tdb_cryptoid;
228 SECASVAR_UNLOCK(sav);
230 /* Get crypto descriptors */
231 crp = crypto_getreq(cryptoid, M_NOWAIT);
233 DPRINTF(("%s: no crypto descriptors\n", __func__));
234 IPCOMPSTAT_INC(ipcomps_crypto);
237 /* Get IPsec-specific opaque pointer */
238 xd = malloc(sizeof(*xd), M_XDATA, M_NOWAIT | M_ZERO);
240 DPRINTF(("%s: cannot allocate xform_data\n", __func__));
241 IPCOMPSTAT_INC(ipcomps_crypto);
246 /* Decompression operation */
247 crp->crp_op = CRYPTO_OP_DECOMPRESS;
248 crp->crp_payload_start = skip + hlen;
249 crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen);
251 /* Crypto operation descriptor */
252 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
253 crp->crp_flags = CRYPTO_F_CBIFSYNC;
255 crp->crp_buf_type = CRYPTO_BUF_MBUF;
256 crp->crp_callback = ipcomp_input_cb;
257 crp->crp_opaque = xd;
259 /* These are passed as-is to the callback */
261 xd->protoff = protoff;
264 xd->cryptoid = cryptoid;
267 crp->crp_session = xd->cryptoid = sav->tdb_cryptoid;
268 SECASVAR_UNLOCK(sav);
270 return crypto_dispatch(crp);
278 * IPComp input callback from the crypto driver.
281 ipcomp_input_cb(struct cryptop *crp)
283 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
284 struct xform_data *xd;
286 struct secasvar *sav;
287 struct secasindex *saidx;
289 crypto_session_t cryptoid;
290 int hlen = IPCOMP_HLENGTH, error, clen;
295 xd = crp->crp_opaque;
296 CURVNET_SET(xd->vnet);
299 protoff = xd->protoff;
300 cryptoid = xd->cryptoid;
301 saidx = &sav->sah->saidx;
302 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
303 saidx->dst.sa.sa_family == AF_INET6,
304 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
306 /* Check for crypto errors */
307 if (crp->crp_etype) {
308 if (crp->crp_etype == EAGAIN) {
309 /* Reset the session ID */
310 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
311 crypto_freesession(cryptoid);
312 xd->cryptoid = crp->crp_session;
314 return (crypto_dispatch(crp));
316 IPCOMPSTAT_INC(ipcomps_noxform);
317 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
318 error = crp->crp_etype;
321 /* Shouldn't happen... */
323 IPCOMPSTAT_INC(ipcomps_crypto);
324 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
328 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
330 clen = crp->crp_olen; /* Length of data after processing */
332 /* Release the crypto descriptors */
333 free(xd, M_XDATA), xd = NULL;
334 crypto_freereq(crp), crp = NULL;
336 /* In case it's not done already, adjust the size of the mbuf chain */
337 m->m_pkthdr.len = clen + hlen + skip;
339 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
340 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/
341 DPRINTF(("%s: m_pullup failed\n", __func__));
342 error = EINVAL; /*XXX*/
346 /* Keep the next protocol field */
347 addr = (caddr_t) mtod(m, struct ip *) + skip;
348 nproto = ((struct ipcomp *) addr)->comp_nxt;
350 /* Remove the IPCOMP header */
351 error = m_striphdr(m, skip, hlen);
353 IPCOMPSTAT_INC(ipcomps_hdrops);
354 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
355 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
356 (u_long) ntohl(sav->spi)));
360 /* Restore the Next Protocol field */
361 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
363 switch (saidx->dst.sa.sa_family) {
366 error = ipsec6_common_input_cb(m, sav, skip, protoff);
371 error = ipsec4_common_input_cb(m, sav, skip, protoff);
375 panic("%s: Unexpected address family: %d saidx=%p", __func__,
376 saidx->dst.sa.sa_family, saidx);
394 * IPComp output routine, called by ipsec[46]_perform_request()
397 ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
398 u_int idx, int skip, int protoff)
400 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
401 const struct comp_algo *ipcompx;
403 struct xform_data *xd;
404 crypto_session_t cryptoid;
405 int error, ralen, maxpacketsize;
407 IPSEC_ASSERT(sav != NULL, ("null SA"));
408 ipcompx = sav->tdb_compalgxform;
409 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
412 * Do not touch the packet in case our payload to compress
413 * is lower than the minimal threshold of the compression
414 * alogrithm. We will just send out the data uncompressed.
415 * See RFC 3173, 2.2. Non-Expansion Policy.
417 if (m->m_pkthdr.len <= ipcompx->minlen) {
418 IPCOMPSTAT_INC(ipcomps_threshold);
419 return ipsec_process_done(m, sp, sav, idx);
422 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
423 IPCOMPSTAT_INC(ipcomps_output);
425 /* Check for maximum packet size violations. */
426 switch (sav->sah->saidx.dst.sa.sa_family) {
429 maxpacketsize = IP_MAXPACKET;
434 maxpacketsize = IPV6_MAXPACKET;
438 IPCOMPSTAT_INC(ipcomps_nopf);
439 DPRINTF(("%s: unknown/unsupported protocol family %d, "
440 "IPCA %s/%08lx\n", __func__,
441 sav->sah->saidx.dst.sa.sa_family,
442 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
443 (u_long) ntohl(sav->spi)));
444 error = EPFNOSUPPORT;
447 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
448 IPCOMPSTAT_INC(ipcomps_toobig);
449 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
450 "(len %u, max len %u)\n", __func__,
451 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
452 (u_long) ntohl(sav->spi),
453 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
458 /* Update the counters */
459 IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
461 m = m_unshare(m, M_NOWAIT);
463 IPCOMPSTAT_INC(ipcomps_hdrops);
464 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
465 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
466 sizeof(buf)), (u_long) ntohl(sav->spi)));
471 /* Ok now, we can pass to the crypto processing. */
473 cryptoid = sav->tdb_cryptoid;
474 SECASVAR_UNLOCK(sav);
476 /* Get crypto descriptors */
477 crp = crypto_getreq(cryptoid, M_NOWAIT);
479 IPCOMPSTAT_INC(ipcomps_crypto);
480 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
485 /* Compression descriptor */
486 crp->crp_op = CRYPTO_OP_COMPRESS;
487 crp->crp_payload_start = skip;
488 crp->crp_payload_length = ralen;
490 /* IPsec-specific opaque crypto info */
491 xd = malloc(sizeof(struct xform_data), M_XDATA, M_NOWAIT | M_ZERO);
493 IPCOMPSTAT_INC(ipcomps_crypto);
494 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
504 xd->protoff = protoff;
506 xd->cryptoid = cryptoid;
508 /* Crypto operation descriptor */
509 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
510 crp->crp_flags = CRYPTO_F_CBIFSYNC;
512 crp->crp_buf_type = CRYPTO_BUF_MBUF;
513 crp->crp_callback = ipcomp_output_cb;
514 crp->crp_opaque = xd;
516 return crypto_dispatch(crp);
526 * IPComp output callback from the crypto driver.
529 ipcomp_output_cb(struct cryptop *crp)
531 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
532 struct xform_data *xd;
533 struct secpolicy *sp;
534 struct secasvar *sav;
536 crypto_session_t cryptoid;
538 int error, skip, protoff;
541 xd = crp->crp_opaque;
542 CURVNET_SET(xd->vnet);
547 protoff = xd->protoff;
548 cryptoid = xd->cryptoid;
550 /* Check for crypto errors */
551 if (crp->crp_etype) {
552 if (crp->crp_etype == EAGAIN) {
553 /* Reset the session ID */
554 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
555 crypto_freesession(cryptoid);
556 xd->cryptoid = crp->crp_session;
558 return (crypto_dispatch(crp));
560 IPCOMPSTAT_INC(ipcomps_noxform);
561 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
562 error = crp->crp_etype;
565 /* Shouldn't happen... */
567 IPCOMPSTAT_INC(ipcomps_crypto);
568 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
572 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
574 if (crp->crp_payload_length > crp->crp_olen) {
576 struct ipcomp *ipcomp;
580 /* Compression helped, inject IPCOMP header. */
581 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
583 IPCOMPSTAT_INC(ipcomps_wrap);
584 DPRINTF(("%s: IPCOMP header inject failed "
585 "for IPCA %s/%08lx\n",
586 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
587 sizeof(buf)), (u_long) ntohl(sav->spi)));
591 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
593 /* Initialize the IPCOMP header */
594 /* XXX alignment always correct? */
595 switch (sav->sah->saidx.dst.sa.sa_family) {
598 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
603 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
607 ipcomp->comp_flags = 0;
608 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
610 /* Fix Next Protocol in IPv4/IPv6 header */
611 prot = IPPROTO_IPCOMP;
612 m_copyback(m, protoff, sizeof(u_int8_t),
615 /* Adjust the length in the IP header */
616 switch (sav->sah->saidx.dst.sa.sa_family) {
619 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
624 mtod(m, struct ip6_hdr *)->ip6_plen =
625 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
629 IPCOMPSTAT_INC(ipcomps_nopf);
630 DPRINTF(("%s: unknown/unsupported protocol "
631 "family %d, IPCA %s/%08lx\n", __func__,
632 sav->sah->saidx.dst.sa.sa_family,
633 ipsec_address(&sav->sah->saidx.dst, buf,
634 sizeof(buf)), (u_long) ntohl(sav->spi)));
635 error = EPFNOSUPPORT;
639 /* Compression was useless, we have lost time. */
640 IPCOMPSTAT_INC(ipcomps_uncompr);
641 DPRINTF(("%s: compressions was useless %d <= %d\n",
642 __func__, crp->crp_payload_length, crp->crp_olen));
643 /* XXX remember state to not compress the next couple
644 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
647 /* Release the crypto descriptor */
651 /* NB: m is reclaimed by ipsec_process_done. */
652 error = ipsec_process_done(m, sp, sav, idx);
668 ipcomp4_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
671 union sockaddr_union src, dst;
674 if (V_ipcomp_enable == 0)
676 if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
678 bzero(&src, sizeof(src));
679 bzero(&dst, sizeof(dst));
680 src.sa.sa_family = dst.sa.sa_family = AF_INET;
681 src.sin.sin_len = dst.sin.sin_len = sizeof(struct sockaddr_in);
682 ip = mtod(m, const struct ip *);
683 src.sin.sin_addr = ip->ip_src;
684 dst.sin.sin_addr = ip->ip_dst;
685 return (ipcomp_encapcheck(&src, &dst));
688 static const struct encaptab *ipe4_cookie = NULL;
689 static const struct encap_config ipv4_encap_cfg = {
691 .min_length = sizeof(struct ip),
692 .exact_match = sizeof(in_addr_t) << 4,
693 .check = ipcomp4_nonexp_encapcheck,
694 .input = ipcomp_nonexp_input
699 ipcomp6_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
702 union sockaddr_union src, dst;
703 const struct ip6_hdr *ip6;
705 if (V_ipcomp_enable == 0)
707 if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
709 bzero(&src, sizeof(src));
710 bzero(&dst, sizeof(dst));
711 src.sa.sa_family = dst.sa.sa_family = AF_INET;
712 src.sin6.sin6_len = dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
713 ip6 = mtod(m, const struct ip6_hdr *);
714 src.sin6.sin6_addr = ip6->ip6_src;
715 dst.sin6.sin6_addr = ip6->ip6_dst;
716 if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6.sin6_addr)) {
717 /* XXX: sa6_recoverscope() */
718 src.sin6.sin6_scope_id =
719 ntohs(src.sin6.sin6_addr.s6_addr16[1]);
720 src.sin6.sin6_addr.s6_addr16[1] = 0;
722 if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6.sin6_addr)) {
723 /* XXX: sa6_recoverscope() */
724 dst.sin6.sin6_scope_id =
725 ntohs(dst.sin6.sin6_addr.s6_addr16[1]);
726 dst.sin6.sin6_addr.s6_addr16[1] = 0;
728 return (ipcomp_encapcheck(&src, &dst));
731 static const struct encaptab *ipe6_cookie = NULL;
732 static const struct encap_config ipv6_encap_cfg = {
734 .min_length = sizeof(struct ip6_hdr),
735 .exact_match = sizeof(struct in6_addr) << 4,
736 .check = ipcomp6_nonexp_encapcheck,
737 .input = ipcomp_nonexp_input
741 static struct xformsw ipcomp_xformsw = {
742 .xf_type = XF_IPCOMP,
744 .xf_init = ipcomp_init,
745 .xf_zeroize = ipcomp_zeroize,
746 .xf_input = ipcomp_input,
747 .xf_output = ipcomp_output,
755 ipe4_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
758 ipe6_cookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
760 xform_attach(&ipcomp_xformsw);
768 ip_encap_detach(ipe4_cookie);
771 ip6_encap_detach(ipe6_cookie);
773 xform_detach(&ipcomp_xformsw);
776 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
777 ipcomp_attach, NULL);
778 SYSUNINIT(ipcomp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
779 ipcomp_detach, NULL);