1*481d3881Srin /* $NetBSD: xform_ipcomp.c,v 1.76 2024/07/05 04:31:54 rin Exp $ */
2e2c8a664Smaxv /* $FreeBSD: xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */
374029031Sjonathan /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
474029031Sjonathan
574029031Sjonathan /*
674029031Sjonathan * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
774029031Sjonathan *
874029031Sjonathan * Redistribution and use in source and binary forms, with or without
974029031Sjonathan * modification, are permitted provided that the following conditions
1074029031Sjonathan * are met:
1174029031Sjonathan *
1274029031Sjonathan * 1. Redistributions of source code must retain the above copyright
1374029031Sjonathan * notice, this list of conditions and the following disclaimer.
1474029031Sjonathan * 2. Redistributions in binary form must reproduce the above copyright
1574029031Sjonathan * notice, this list of conditions and the following disclaimer in the
1674029031Sjonathan * documentation and/or other materials provided with the distribution.
1774029031Sjonathan * 3. The name of the author may not be used to endorse or promote products
1874029031Sjonathan * derived from this software without specific prior written permission.
1974029031Sjonathan *
2074029031Sjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2174029031Sjonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2274029031Sjonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2374029031Sjonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2474029031Sjonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2574029031Sjonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2674029031Sjonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2774029031Sjonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2874029031Sjonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2974029031Sjonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3074029031Sjonathan */
3174029031Sjonathan
3274029031Sjonathan #include <sys/cdefs.h>
33*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.76 2024/07/05 04:31:54 rin Exp $");
3474029031Sjonathan
3574029031Sjonathan /* IP payload compression protocol (IPComp), see RFC 2393 */
3680d40a78Sozaki-r #if defined(_KERNEL_OPT)
3774029031Sjonathan #include "opt_inet.h"
3880d40a78Sozaki-r #endif
3974029031Sjonathan
4074029031Sjonathan #include <sys/param.h>
4174029031Sjonathan #include <sys/systm.h>
4274029031Sjonathan #include <sys/mbuf.h>
4374029031Sjonathan #include <sys/socket.h>
4474029031Sjonathan #include <sys/kernel.h>
4574029031Sjonathan #include <sys/protosw.h>
4674029031Sjonathan #include <sys/sysctl.h>
478487e35aSozaki-r #include <sys/pool.h>
480c084e85Sozaki-r #include <sys/pserialize.h>
4974029031Sjonathan
5074029031Sjonathan #include <netinet/in.h>
5174029031Sjonathan #include <netinet/in_systm.h>
5274029031Sjonathan #include <netinet/ip.h>
5374029031Sjonathan #include <netinet/ip_var.h>
5474029031Sjonathan
5574029031Sjonathan #include <net/route.h>
5674029031Sjonathan #include <netipsec/ipsec.h>
57caf49ea5Sthorpej #include <netipsec/ipsec_private.h>
5874029031Sjonathan #include <netipsec/xform.h>
5974029031Sjonathan
6074029031Sjonathan #ifdef INET6
6174029031Sjonathan #include <netinet/ip6.h>
6274029031Sjonathan #include <netipsec/ipsec6.h>
6374029031Sjonathan #endif
6474029031Sjonathan
6574029031Sjonathan #include <netipsec/ipcomp.h>
6674029031Sjonathan #include <netipsec/ipcomp_var.h>
6774029031Sjonathan
689355900eStls #include <netipsec/key.h>
699355900eStls #include <netipsec/key_debug.h>
7074029031Sjonathan
7174029031Sjonathan #include <opencrypto/cryptodev.h>
7274029031Sjonathan #include <opencrypto/deflate.h>
7374029031Sjonathan
74caf49ea5Sthorpej percpu_t *ipcompstat_percpu;
75caf49ea5Sthorpej
76e2211411Sdegroote int ipcomp_enable = 1;
7774029031Sjonathan
783ae8d479Sriastradh static void ipcomp_input_cb(struct cryptop *crp);
793ae8d479Sriastradh static void ipcomp_output_cb(struct cryptop *crp);
8074029031Sjonathan
8162446aa1Schristos const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
8262446aa1Schristos
836b225a2fSozaki-r static pool_cache_t ipcomp_tdb_crypto_pool_cache;
848487e35aSozaki-r
85909a8e83Sdrochner const struct comp_algo *
ipcomp_algorithm_lookup(int alg)8674029031Sjonathan ipcomp_algorithm_lookup(int alg)
8774029031Sjonathan {
8874029031Sjonathan switch (alg) {
8974029031Sjonathan case SADB_X_CALG_DEFLATE:
901caa9a52Sdrochner return &comp_algo_deflate_nogrow;
9174029031Sjonathan }
9274029031Sjonathan return NULL;
9374029031Sjonathan }
9474029031Sjonathan
9574029031Sjonathan /*
9674029031Sjonathan * ipcomp_init() is called when an CPI is being set up.
9774029031Sjonathan */
9874029031Sjonathan static int
ipcomp_init(struct secasvar * sav,const struct xformsw * xsp)99909a8e83Sdrochner ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
10074029031Sjonathan {
101909a8e83Sdrochner const struct comp_algo *tcomp;
10274029031Sjonathan struct cryptoini cric;
103e5bd2a12Stls int ses;
10474029031Sjonathan
10574029031Sjonathan /* NB: algorithm really comes in alg_enc and not alg_comp! */
10674029031Sjonathan tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
10774029031Sjonathan if (tcomp == NULL) {
1084a07f437Schristos DPRINTF("unsupported compression algorithm %d\n",
1094a07f437Schristos sav->alg_comp);
11074029031Sjonathan return EINVAL;
11174029031Sjonathan }
11274029031Sjonathan sav->alg_comp = sav->alg_enc; /* set for doing histogram */
11374029031Sjonathan sav->tdb_xform = xsp;
11474029031Sjonathan sav->tdb_compalgxform = tcomp;
11574029031Sjonathan
11674029031Sjonathan /* Initialize crypto session */
117c363a9cbScegger memset(&cric, 0, sizeof(cric));
11874029031Sjonathan cric.cri_alg = sav->tdb_compalgxform->type;
11974029031Sjonathan
120e5bd2a12Stls ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
121e5bd2a12Stls return ses;
12274029031Sjonathan }
12374029031Sjonathan
12474029031Sjonathan /*
12574029031Sjonathan * ipcomp_zeroize() used when IPCA is deleted
12674029031Sjonathan */
12756192e56Sriastradh static void
ipcomp_zeroize(struct secasvar * sav)12874029031Sjonathan ipcomp_zeroize(struct secasvar *sav)
12974029031Sjonathan {
13074029031Sjonathan
131a1f5e1f2Sriastradh crypto_freesession(sav->tdb_cryptoid);
13274029031Sjonathan sav->tdb_cryptoid = 0;
13374029031Sjonathan }
13474029031Sjonathan
13574029031Sjonathan /*
13674029031Sjonathan * ipcomp_input() gets called to uncompress an input packet
13774029031Sjonathan */
13874029031Sjonathan static int
ipcomp_input(struct mbuf * m,struct secasvar * sav,int skip,int protoff)139be5a3d6fSozaki-r ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
14074029031Sjonathan {
14174029031Sjonathan struct tdb_crypto *tc;
14274029031Sjonathan struct cryptodesc *crdc;
14374029031Sjonathan struct cryptop *crp;
1442c1d5309Sozaki-r int error, hlen = IPCOMP_HLENGTH, stat = IPCOMP_STAT_CRYPTO;
14574029031Sjonathan
14696cdd49dSmaxv KASSERT(skip + hlen <= m->m_pkthdr.len);
14774029031Sjonathan
14874029031Sjonathan /* Get crypto descriptors */
14974029031Sjonathan crp = crypto_getreq(1);
15074029031Sjonathan if (crp == NULL) {
1514a07f437Schristos DPRINTF("no crypto descriptors\n");
1522c1d5309Sozaki-r error = ENOBUFS;
1532c1d5309Sozaki-r goto error_m;
15474029031Sjonathan }
15574029031Sjonathan /* Get IPsec-specific opaque pointer */
1566b225a2fSozaki-r tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
15774029031Sjonathan if (tc == NULL) {
1584a07f437Schristos DPRINTF("cannot allocate tdb_crypto\n");
1592c1d5309Sozaki-r error = ENOBUFS;
1602c1d5309Sozaki-r goto error_crp;
16174029031Sjonathan }
16222beeb59Sdrochner
16322beeb59Sdrochner error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
16422beeb59Sdrochner if (error) {
1654a07f437Schristos DPRINTF("m_makewritable failed\n");
1662c1d5309Sozaki-r goto error_tc;
16722beeb59Sdrochner }
16822beeb59Sdrochner
169b2da0132Sozaki-r {
170b2da0132Sozaki-r int s = pserialize_read_enter();
171b2da0132Sozaki-r
172b2da0132Sozaki-r /*
173b2da0132Sozaki-r * Take another reference to the SA for opencrypto callback.
174b2da0132Sozaki-r */
175b2da0132Sozaki-r if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
176b2da0132Sozaki-r pserialize_read_exit(s);
1772c1d5309Sozaki-r stat = IPCOMP_STAT_NOTDB;
1782c1d5309Sozaki-r error = ENOENT;
1792c1d5309Sozaki-r goto error_tc;
180b2da0132Sozaki-r }
181b2da0132Sozaki-r KEY_SA_REF(sav);
182b2da0132Sozaki-r pserialize_read_exit(s);
183b2da0132Sozaki-r }
184b2da0132Sozaki-r
18574029031Sjonathan crdc = crp->crp_desc;
18674029031Sjonathan
18774029031Sjonathan crdc->crd_skip = skip + hlen;
18874029031Sjonathan crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
18922beeb59Sdrochner crdc->crd_inject = 0; /* unused */
19074029031Sjonathan
19174029031Sjonathan /* Decompression operation */
19274029031Sjonathan crdc->crd_alg = sav->tdb_compalgxform->type;
19374029031Sjonathan
19474029031Sjonathan /* Crypto operation descriptor */
19574029031Sjonathan crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
1961caa9a52Sdrochner crp->crp_olen = MCLBYTES; /* hint to decompression code */
19774029031Sjonathan crp->crp_flags = CRYPTO_F_IMBUF;
198dd86ba72Sdegroote crp->crp_buf = m;
19974029031Sjonathan crp->crp_callback = ipcomp_input_cb;
20074029031Sjonathan crp->crp_sid = sav->tdb_cryptoid;
201dd86ba72Sdegroote crp->crp_opaque = tc;
20274029031Sjonathan
20374029031Sjonathan /* These are passed as-is to the callback */
20474029031Sjonathan tc->tc_spi = sav->spi;
20574029031Sjonathan tc->tc_dst = sav->sah->saidx.dst;
20674029031Sjonathan tc->tc_proto = sav->sah->saidx.proto;
20774029031Sjonathan tc->tc_protoff = protoff;
20874029031Sjonathan tc->tc_skip = skip;
209be5a3d6fSozaki-r tc->tc_sav = sav;
21074029031Sjonathan
211893f06d4Sriastradh crypto_dispatch(crp);
212893f06d4Sriastradh return 0;
2132c1d5309Sozaki-r
2142c1d5309Sozaki-r error_tc:
2152c1d5309Sozaki-r pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
2162c1d5309Sozaki-r error_crp:
2172c1d5309Sozaki-r crypto_freereq(crp);
2182c1d5309Sozaki-r error_m:
2192c1d5309Sozaki-r m_freem(m);
2202c1d5309Sozaki-r IPCOMP_STATINC(stat);
2212c1d5309Sozaki-r return error;
22274029031Sjonathan }
22374029031Sjonathan
22474029031Sjonathan #ifdef INET6
2252b6b0bfcSozaki-r #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
22674029031Sjonathan if (saidx->dst.sa.sa_family == AF_INET6) { \
2273ae8d479Sriastradh (void)ipsec6_common_input_cb(m, sav, skip, protoff); \
22874029031Sjonathan } else { \
2293ae8d479Sriastradh (void)ipsec4_common_input_cb(m, sav, skip, protoff); \
23074029031Sjonathan } \
23174029031Sjonathan } while (0)
23274029031Sjonathan #else
2332b6b0bfcSozaki-r #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
2343ae8d479Sriastradh ((void)ipsec4_common_input_cb(m, sav, skip, protoff))
23574029031Sjonathan #endif
23674029031Sjonathan
23774029031Sjonathan /*
23874029031Sjonathan * IPComp input callback from the crypto driver.
23974029031Sjonathan */
2403ae8d479Sriastradh static void
ipcomp_input_cb(struct cryptop * crp)24174029031Sjonathan ipcomp_input_cb(struct cryptop *crp)
24274029031Sjonathan {
243dd8c81f5Sryo char buf[IPSEC_ADDRSTRLEN];
24474029031Sjonathan struct tdb_crypto *tc;
24574029031Sjonathan int skip, protoff;
24674029031Sjonathan struct mbuf *m;
24774029031Sjonathan struct secasvar *sav;
248a370d57cSmrg struct secasindex *saidx __diagused;
2493ae8d479Sriastradh int hlen = IPCOMP_HLENGTH, clen;
2503d6520b3Schristos uint8_t nproto;
25130a99231Smaxv struct ipcomp *ipc;
252e1c9808fSozaki-r IPSEC_DECLARE_LOCK_VARIABLE;
25374029031Sjonathan
2542620e166Sozaki-r KASSERT(crp->crp_opaque != NULL);
2553d6520b3Schristos tc = crp->crp_opaque;
25674029031Sjonathan skip = tc->tc_skip;
25774029031Sjonathan protoff = tc->tc_protoff;
2583d6520b3Schristos m = crp->crp_buf;
25974029031Sjonathan
260e1c9808fSozaki-r IPSEC_ACQUIRE_GLOBAL_LOCKS();
26174029031Sjonathan
262be5a3d6fSozaki-r sav = tc->tc_sav;
26374029031Sjonathan saidx = &sav->sah->saidx;
2642620e166Sozaki-r KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
26574029031Sjonathan saidx->dst.sa.sa_family == AF_INET6,
2662620e166Sozaki-r "unexpected protocol family %u", saidx->dst.sa.sa_family);
26774029031Sjonathan
26874029031Sjonathan /* Check for crypto errors */
26974029031Sjonathan if (crp->crp_etype) {
27074029031Sjonathan /* Reset the session ID */
27174029031Sjonathan if (sav->tdb_cryptoid != 0)
27274029031Sjonathan sav->tdb_cryptoid = crp->crp_sid;
27374029031Sjonathan
274caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
2754a07f437Schristos DPRINTF("crypto error %d\n", crp->crp_etype);
27674029031Sjonathan goto bad;
27774029031Sjonathan }
27880807c77Sozaki-r
27962446aa1Schristos IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
28074029031Sjonathan
281cd960003Sdegroote /* Update the counters */
282cd960003Sdegroote IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
283cd960003Sdegroote
28430a99231Smaxv /* Length of data after processing */
28530a99231Smaxv clen = crp->crp_olen;
28674029031Sjonathan
28774029031Sjonathan /* Release the crypto descriptors */
2886b225a2fSozaki-r pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
2898487e35aSozaki-r tc = NULL;
29030a99231Smaxv crypto_freereq(crp);
29130a99231Smaxv crp = NULL;
29274029031Sjonathan
29374029031Sjonathan /* In case it's not done already, adjust the size of the mbuf chain */
29474029031Sjonathan m->m_pkthdr.len = clen + hlen + skip;
29574029031Sjonathan
29696cdd49dSmaxv /*
29796cdd49dSmaxv * Get the next protocol field.
29896cdd49dSmaxv *
29996cdd49dSmaxv * XXX: Really, we should use m_copydata instead of m_pullup.
30096cdd49dSmaxv */
30174029031Sjonathan if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
30230a99231Smaxv IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
3034a07f437Schristos DPRINTF("m_pullup failed\n");
30474029031Sjonathan goto bad;
30574029031Sjonathan }
30630a99231Smaxv ipc = (struct ipcomp *)(mtod(m, uint8_t *) + skip);
30730a99231Smaxv nproto = ipc->comp_nxt;
30896cdd49dSmaxv
3093d6520b3Schristos switch (nproto) {
3103d6520b3Schristos case IPPROTO_IPCOMP:
3113d6520b3Schristos case IPPROTO_AH:
3123d6520b3Schristos case IPPROTO_ESP:
313749619c9Sspz IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
3144a07f437Schristos DPRINTF("nested ipcomp, IPCA %s/%08lx\n",
315dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
3164a07f437Schristos (u_long) ntohl(sav->spi));
317749619c9Sspz goto bad;
3183d6520b3Schristos default:
3193d6520b3Schristos break;
320749619c9Sspz }
32174029031Sjonathan
32274029031Sjonathan /* Remove the IPCOMP header */
3233ae8d479Sriastradh if (m_striphdr(m, skip, hlen) != 0) {
324caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
3254a07f437Schristos DPRINTF("bad mbuf chain, IPCA %s/%08lx\n",
326dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
3274a07f437Schristos (u_long) ntohl(sav->spi));
32874029031Sjonathan goto bad;
32974029031Sjonathan }
33074029031Sjonathan
33174029031Sjonathan /* Restore the Next Protocol field */
33296cdd49dSmaxv m_copyback(m, protoff, sizeof(nproto), &nproto);
33374029031Sjonathan
3342b6b0bfcSozaki-r IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
33574029031Sjonathan
3368be5cabcSozaki-r KEY_SA_UNREF(&sav);
337e1c9808fSozaki-r IPSEC_RELEASE_GLOBAL_LOCKS();
3383ae8d479Sriastradh return;
33930a99231Smaxv
34074029031Sjonathan bad:
34174029031Sjonathan if (sav)
3428be5cabcSozaki-r KEY_SA_UNREF(&sav);
343e1c9808fSozaki-r IPSEC_RELEASE_GLOBAL_LOCKS();
34474029031Sjonathan m_freem(m);
34574029031Sjonathan if (tc != NULL)
3466b225a2fSozaki-r pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
34774029031Sjonathan if (crp)
34874029031Sjonathan crypto_freereq(crp);
34974029031Sjonathan }
35074029031Sjonathan
35174029031Sjonathan /*
35274029031Sjonathan * IPComp output routine, called by ipsec[46]_process_packet()
35374029031Sjonathan */
35474029031Sjonathan static int
ipcomp_output(struct mbuf * m,const struct ipsecrequest * isr,struct secasvar * sav,int skip,int protoff,int flags)35530a99231Smaxv ipcomp_output(struct mbuf *m, const struct ipsecrequest *isr,
356c535599fSknakahara struct secasvar *sav, int skip, int protoff, int flags)
35774029031Sjonathan {
358dd8c81f5Sryo char buf[IPSEC_ADDRSTRLEN];
359909a8e83Sdrochner const struct comp_algo *ipcompx;
360e2211411Sdegroote int error, ralen, hlen, maxpacketsize;
36174029031Sjonathan struct cryptodesc *crdc;
36274029031Sjonathan struct cryptop *crp;
36374029031Sjonathan struct tdb_crypto *tc;
36474029031Sjonathan
36538b8f795Sozaki-r KASSERT(sav != NULL);
3662620e166Sozaki-r KASSERT(sav->tdb_compalgxform != NULL);
36774029031Sjonathan ipcompx = sav->tdb_compalgxform;
36874029031Sjonathan
36930a99231Smaxv /* Raw payload length before comp. */
37030a99231Smaxv ralen = m->m_pkthdr.len - skip;
371e2211411Sdegroote
372e2211411Sdegroote /* Don't process the packet if it is too short */
373e2211411Sdegroote if (ralen < ipcompx->minlen) {
374caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
375c535599fSknakahara return ipsec_process_done(m, isr, sav, 0);
376e2211411Sdegroote }
377e2211411Sdegroote
37874029031Sjonathan hlen = IPCOMP_HLENGTH;
37974029031Sjonathan
380caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
38174029031Sjonathan
38274029031Sjonathan /* Check for maximum packet size violations. */
38374029031Sjonathan switch (sav->sah->saidx.dst.sa.sa_family) {
38474029031Sjonathan #ifdef INET
38574029031Sjonathan case AF_INET:
38674029031Sjonathan maxpacketsize = IP_MAXPACKET;
38774029031Sjonathan break;
38830a99231Smaxv #endif
38974029031Sjonathan #ifdef INET6
39074029031Sjonathan case AF_INET6:
39174029031Sjonathan maxpacketsize = IPV6_MAXPACKET;
39274029031Sjonathan break;
39330a99231Smaxv #endif
39474029031Sjonathan default:
395caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_NOPF);
3964a07f437Schristos DPRINTF("unknown/unsupported protocol family %d"
3974a07f437Schristos ", IPCA %s/%08lx\n",
39874029031Sjonathan sav->sah->saidx.dst.sa.sa_family,
399dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
4004a07f437Schristos (u_long) ntohl(sav->spi));
40174029031Sjonathan error = EPFNOSUPPORT;
40274029031Sjonathan goto bad;
40374029031Sjonathan }
40474029031Sjonathan if (skip + hlen + ralen > maxpacketsize) {
405caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
4064a07f437Schristos DPRINTF("packet in IPCA %s/%08lx got too big "
4074a07f437Schristos "(len %u, max len %u)\n",
408dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
40974029031Sjonathan (u_long) ntohl(sav->spi),
4104a07f437Schristos skip + hlen + ralen, maxpacketsize);
41174029031Sjonathan error = EMSGSIZE;
41274029031Sjonathan goto bad;
41374029031Sjonathan }
41474029031Sjonathan
41574029031Sjonathan /* Update the counters */
416caf49ea5Sthorpej IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
41774029031Sjonathan
41874029031Sjonathan m = m_clone(m);
41974029031Sjonathan if (m == NULL) {
420caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
4214a07f437Schristos DPRINTF("cannot clone mbuf chain, IPCA %s/%08lx\n",
422dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
4234a07f437Schristos (u_long) ntohl(sav->spi));
42474029031Sjonathan error = ENOBUFS;
42574029031Sjonathan goto bad;
42674029031Sjonathan }
42774029031Sjonathan
42874029031Sjonathan /* Ok now, we can pass to the crypto processing */
42974029031Sjonathan
43074029031Sjonathan /* Get crypto descriptors */
43174029031Sjonathan crp = crypto_getreq(1);
43274029031Sjonathan if (crp == NULL) {
433caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
4344a07f437Schristos DPRINTF("failed to acquire crypto descriptor\n");
43574029031Sjonathan error = ENOBUFS;
43674029031Sjonathan goto bad;
43774029031Sjonathan }
43874029031Sjonathan crdc = crp->crp_desc;
43974029031Sjonathan
44074029031Sjonathan /* Compression descriptor */
441e2211411Sdegroote crdc->crd_skip = skip;
442e2211411Sdegroote crdc->crd_len = m->m_pkthdr.len - skip;
44374029031Sjonathan crdc->crd_flags = CRD_F_COMP;
444e2211411Sdegroote crdc->crd_inject = skip;
44574029031Sjonathan
44674029031Sjonathan /* Compression operation */
44774029031Sjonathan crdc->crd_alg = ipcompx->type;
44874029031Sjonathan
44974029031Sjonathan /* IPsec-specific opaque crypto info */
4506b225a2fSozaki-r tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
45174029031Sjonathan if (tc == NULL) {
452caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
4534a07f437Schristos DPRINTF("failed to allocate tdb_crypto\n");
45474029031Sjonathan crypto_freereq(crp);
45574029031Sjonathan error = ENOBUFS;
45674029031Sjonathan goto bad;
45774029031Sjonathan }
45874029031Sjonathan
4590c084e85Sozaki-r {
4600c084e85Sozaki-r int s = pserialize_read_enter();
4610c084e85Sozaki-r
462b2da0132Sozaki-r /*
463b2da0132Sozaki-r * Take another reference to the SP and the SA for opencrypto callback.
464b2da0132Sozaki-r */
465b2da0132Sozaki-r if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
466b2da0132Sozaki-r sav->state == SADB_SASTATE_DEAD)) {
4670c084e85Sozaki-r pserialize_read_exit(s);
4686b225a2fSozaki-r pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
4690c084e85Sozaki-r crypto_freereq(crp);
4700c084e85Sozaki-r IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
4710c084e85Sozaki-r error = ENOENT;
4720c084e85Sozaki-r goto bad;
4730c084e85Sozaki-r }
4741cf1ea9dSozaki-r KEY_SP_REF(isr->sp);
475b2da0132Sozaki-r KEY_SA_REF(sav);
4760c084e85Sozaki-r pserialize_read_exit(s);
4770c084e85Sozaki-r }
4780c084e85Sozaki-r
4790c084e85Sozaki-r tc->tc_isr = isr;
48074029031Sjonathan tc->tc_spi = sav->spi;
48174029031Sjonathan tc->tc_dst = sav->sah->saidx.dst;
48274029031Sjonathan tc->tc_proto = sav->sah->saidx.proto;
483f76a162cSdegroote tc->tc_skip = skip;
484f76a162cSdegroote tc->tc_protoff = protoff;
485c535599fSknakahara tc->tc_flags = flags;
486be5a3d6fSozaki-r tc->tc_sav = sav;
48774029031Sjonathan
48874029031Sjonathan /* Crypto operation descriptor */
48974029031Sjonathan crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
49074029031Sjonathan crp->crp_flags = CRYPTO_F_IMBUF;
491dd86ba72Sdegroote crp->crp_buf = m;
49274029031Sjonathan crp->crp_callback = ipcomp_output_cb;
493dd86ba72Sdegroote crp->crp_opaque = tc;
49474029031Sjonathan crp->crp_sid = sav->tdb_cryptoid;
49574029031Sjonathan
496893f06d4Sriastradh crypto_dispatch(crp);
497893f06d4Sriastradh return 0;
49830a99231Smaxv
49974029031Sjonathan bad:
50074029031Sjonathan m_freem(m);
50130a99231Smaxv return error;
50274029031Sjonathan }
50374029031Sjonathan
50474029031Sjonathan /*
50574029031Sjonathan * IPComp output callback from the crypto driver.
50674029031Sjonathan */
5073ae8d479Sriastradh static void
ipcomp_output_cb(struct cryptop * crp)50874029031Sjonathan ipcomp_output_cb(struct cryptop *crp)
50974029031Sjonathan {
510dd8c81f5Sryo char buf[IPSEC_ADDRSTRLEN];
51174029031Sjonathan struct tdb_crypto *tc;
51283c2b87aSozaki-r const struct ipsecrequest *isr;
51374029031Sjonathan struct secasvar *sav;
514e2211411Sdegroote struct mbuf *m, *mo;
5153ae8d479Sriastradh int skip, rlen, roff, flags;
5163d6520b3Schristos uint8_t prot;
5173d6520b3Schristos uint16_t cpi;
518e2211411Sdegroote struct ipcomp * ipcomp;
519e1c9808fSozaki-r IPSEC_DECLARE_LOCK_VARIABLE;
520e2211411Sdegroote
5212620e166Sozaki-r KASSERT(crp->crp_opaque != NULL);
5223d6520b3Schristos tc = crp->crp_opaque;
5233d6520b3Schristos m = crp->crp_buf;
52474029031Sjonathan skip = tc->tc_skip;
52574029031Sjonathan rlen = crp->crp_ilen - skip;
52674029031Sjonathan
527e1c9808fSozaki-r IPSEC_ACQUIRE_GLOBAL_LOCKS();
52874029031Sjonathan
52974029031Sjonathan isr = tc->tc_isr;
530be5a3d6fSozaki-r sav = tc->tc_sav;
53174029031Sjonathan
53274029031Sjonathan /* Check for crypto errors */
53374029031Sjonathan if (crp->crp_etype) {
53474029031Sjonathan /* Reset session ID */
53574029031Sjonathan if (sav->tdb_cryptoid != 0)
53674029031Sjonathan sav->tdb_cryptoid = crp->crp_sid;
53774029031Sjonathan
538caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
5394a07f437Schristos DPRINTF("crypto error %d\n", crp->crp_etype);
54074029031Sjonathan goto bad;
54174029031Sjonathan }
54280807c77Sozaki-r
54362446aa1Schristos IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
54474029031Sjonathan
54574029031Sjonathan if (rlen > crp->crp_olen) {
546e2211411Sdegroote /* Inject IPCOMP header */
547e2211411Sdegroote mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
548e2211411Sdegroote if (mo == NULL) {
549caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_WRAP);
5504a07f437Schristos DPRINTF("failed to inject IPCOMP header for "
5514a07f437Schristos "IPCA %s/%08lx\n",
552dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf,
5534a07f437Schristos sizeof(buf)), (u_long) ntohl(sav->spi));
554e2211411Sdegroote goto bad;
555e2211411Sdegroote }
556c252f603Sdegroote ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
557e2211411Sdegroote
558e2211411Sdegroote /* Initialize the IPCOMP header */
559e2211411Sdegroote /* XXX alignment always correct? */
560e2211411Sdegroote switch (sav->sah->saidx.dst.sa.sa_family) {
561e2211411Sdegroote #ifdef INET
562e2211411Sdegroote case AF_INET:
563e2211411Sdegroote ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
564e2211411Sdegroote break;
56530a99231Smaxv #endif
566e2211411Sdegroote #ifdef INET6
567e2211411Sdegroote case AF_INET6:
568e2211411Sdegroote ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
569e2211411Sdegroote break;
570e2211411Sdegroote #endif
571e2211411Sdegroote }
572e2211411Sdegroote ipcomp->comp_flags = 0;
573e2211411Sdegroote
574e2211411Sdegroote cpi = ntohl(sav->spi) & 0xffff;
575e2211411Sdegroote ipcomp->comp_cpi = htons(cpi);
576e2211411Sdegroote
577e2211411Sdegroote /* Fix Next Protocol in IPv4/IPv6 header */
578e2211411Sdegroote prot = IPPROTO_IPCOMP;
57996cdd49dSmaxv m_copyback(m, tc->tc_protoff, sizeof(prot), &prot);
580e2211411Sdegroote
58174029031Sjonathan /* Adjust the length in the IP header */
58274029031Sjonathan switch (sav->sah->saidx.dst.sa.sa_family) {
58374029031Sjonathan #ifdef INET
58474029031Sjonathan case AF_INET:
58574029031Sjonathan mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
58674029031Sjonathan break;
58730a99231Smaxv #endif
58874029031Sjonathan #ifdef INET6
58974029031Sjonathan case AF_INET6:
59074029031Sjonathan mtod(m, struct ip6_hdr *)->ip6_plen =
59100c01d2fSmaxv htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
59274029031Sjonathan break;
59330a99231Smaxv #endif
59474029031Sjonathan default:
595caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_NOPF);
5964a07f437Schristos DPRINTF("unknown/unsupported protocol "
59774029031Sjonathan "family %d, IPCA %s/%08lx\n",
59874029031Sjonathan sav->sah->saidx.dst.sa.sa_family,
599dd8c81f5Sryo ipsec_address(&sav->sah->saidx.dst, buf,
6004a07f437Schristos sizeof(buf)), (u_long) ntohl(sav->spi));
60174029031Sjonathan goto bad;
60274029031Sjonathan }
60374029031Sjonathan } else {
60474029031Sjonathan /* compression was useless, we have lost time */
605caf49ea5Sthorpej IPCOMP_STATINC(IPCOMP_STAT_USELESS);
6064a07f437Schristos DPRINTF("compression was useless: initial size was %d "
6074a07f437Schristos "and compressed size is %d\n", rlen, crp->crp_olen);
60874029031Sjonathan }
60974029031Sjonathan
610c535599fSknakahara flags = tc->tc_flags;
61174029031Sjonathan /* Release the crypto descriptor */
6126b225a2fSozaki-r pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
61374029031Sjonathan crypto_freereq(crp);
61474029031Sjonathan
61574029031Sjonathan /* NB: m is reclaimed by ipsec_process_done. */
6163ae8d479Sriastradh (void)ipsec_process_done(m, isr, sav, flags);
6178be5cabcSozaki-r KEY_SA_UNREF(&sav);
6180c084e85Sozaki-r KEY_SP_UNREF(&isr->sp);
619e1c9808fSozaki-r IPSEC_RELEASE_GLOBAL_LOCKS();
6203ae8d479Sriastradh return;
62130a99231Smaxv
62274029031Sjonathan bad:
62374029031Sjonathan if (sav)
6248be5cabcSozaki-r KEY_SA_UNREF(&sav);
6250c084e85Sozaki-r KEY_SP_UNREF(&isr->sp);
626e1c9808fSozaki-r IPSEC_RELEASE_GLOBAL_LOCKS();
62774029031Sjonathan m_freem(m);
6286b225a2fSozaki-r pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
62974029031Sjonathan crypto_freereq(crp);
63074029031Sjonathan }
63174029031Sjonathan
63274029031Sjonathan static struct xformsw ipcomp_xformsw = {
6338f43f955Sozaki-r .xf_type = XF_IPCOMP,
6348f43f955Sozaki-r .xf_flags = XFT_COMP,
6358f43f955Sozaki-r .xf_name = "IPcomp",
6368f43f955Sozaki-r .xf_init = ipcomp_init,
6378f43f955Sozaki-r .xf_zeroize = ipcomp_zeroize,
6388f43f955Sozaki-r .xf_input = ipcomp_input,
6398f43f955Sozaki-r .xf_output = ipcomp_output,
6408f43f955Sozaki-r .xf_next = NULL,
64174029031Sjonathan };
64274029031Sjonathan
643ef67739aSozaki-r void
ipcomp_attach(void)64474029031Sjonathan ipcomp_attach(void)
64574029031Sjonathan {
646caf49ea5Sthorpej ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
6476b225a2fSozaki-r ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
6486b225a2fSozaki-r coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
6496b225a2fSozaki-r NULL, NULL, NULL);
65074029031Sjonathan xform_register(&ipcomp_xformsw);
65174029031Sjonathan }
652