Home | History | Annotate | Line # | Download | only in netipsec
xform_ipcomp.c revision 1.59.2.4
      1  1.59.2.4  pgoyette /*	$NetBSD: xform_ipcomp.c,v 1.59.2.4 2018/05/21 04:36:16 pgoyette Exp $	*/
      2  1.59.2.2  pgoyette /*	$FreeBSD: xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $	*/
      3       1.1  jonathan /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
      4       1.1  jonathan 
      5       1.1  jonathan /*
      6       1.1  jonathan  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org)
      7       1.1  jonathan  *
      8       1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      9       1.1  jonathan  * modification, are permitted provided that the following conditions
     10       1.1  jonathan  * are met:
     11       1.1  jonathan  *
     12       1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13       1.1  jonathan  *   notice, this list of conditions and the following disclaimer.
     14       1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  jonathan  *   notice, this list of conditions and the following disclaimer in the
     16       1.1  jonathan  *   documentation and/or other materials provided with the distribution.
     17       1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     18       1.1  jonathan  *   derived from this software without specific prior written permission.
     19       1.1  jonathan  *
     20       1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21       1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22       1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23       1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24       1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25       1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26       1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27       1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28       1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29       1.1  jonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30       1.1  jonathan  */
     31       1.1  jonathan 
     32       1.1  jonathan #include <sys/cdefs.h>
     33  1.59.2.4  pgoyette __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.59.2.4 2018/05/21 04:36:16 pgoyette Exp $");
     34       1.1  jonathan 
     35       1.1  jonathan /* IP payload compression protocol (IPComp), see RFC 2393 */
     36      1.32     ozaki #if defined(_KERNEL_OPT)
     37       1.1  jonathan #include "opt_inet.h"
     38      1.32     ozaki #endif
     39       1.1  jonathan 
     40       1.1  jonathan #include <sys/param.h>
     41       1.1  jonathan #include <sys/systm.h>
     42       1.1  jonathan #include <sys/mbuf.h>
     43       1.1  jonathan #include <sys/socket.h>
     44       1.1  jonathan #include <sys/kernel.h>
     45       1.1  jonathan #include <sys/protosw.h>
     46       1.1  jonathan #include <sys/sysctl.h>
     47      1.47     ozaki #include <sys/pool.h>
     48      1.49     ozaki #include <sys/pserialize.h>
     49       1.1  jonathan 
     50       1.1  jonathan #include <netinet/in.h>
     51       1.1  jonathan #include <netinet/in_systm.h>
     52       1.1  jonathan #include <netinet/ip.h>
     53       1.1  jonathan #include <netinet/ip_var.h>
     54       1.1  jonathan 
     55       1.1  jonathan #include <net/route.h>
     56       1.1  jonathan #include <netipsec/ipsec.h>
     57      1.18   thorpej #include <netipsec/ipsec_private.h>
     58       1.1  jonathan #include <netipsec/xform.h>
     59       1.1  jonathan 
     60       1.1  jonathan #ifdef INET6
     61       1.1  jonathan #include <netinet/ip6.h>
     62       1.1  jonathan #include <netipsec/ipsec6.h>
     63       1.1  jonathan #endif
     64       1.1  jonathan 
     65       1.1  jonathan #include <netipsec/ipcomp.h>
     66       1.1  jonathan #include <netipsec/ipcomp_var.h>
     67       1.1  jonathan 
     68       1.4       tls #include <netipsec/key.h>
     69       1.4       tls #include <netipsec/key_debug.h>
     70       1.1  jonathan 
     71       1.1  jonathan #include <opencrypto/cryptodev.h>
     72       1.1  jonathan #include <opencrypto/deflate.h>
     73       1.1  jonathan 
     74      1.18   thorpej percpu_t *ipcompstat_percpu;
     75      1.18   thorpej 
     76      1.57      maxv int ipcomp_enable = 1;
     77       1.1  jonathan 
     78       1.1  jonathan static int ipcomp_input_cb(struct cryptop *crp);
     79       1.1  jonathan static int ipcomp_output_cb(struct cryptop *crp);
     80       1.1  jonathan 
     81      1.33  christos const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
     82      1.33  christos 
     83      1.52     ozaki static pool_cache_t ipcomp_tdb_crypto_pool_cache;
     84      1.47     ozaki 
     85      1.24  drochner const struct comp_algo *
     86       1.1  jonathan ipcomp_algorithm_lookup(int alg)
     87       1.1  jonathan {
     88       1.1  jonathan 	switch (alg) {
     89       1.1  jonathan 	case SADB_X_CALG_DEFLATE:
     90      1.25  drochner 		return &comp_algo_deflate_nogrow;
     91       1.1  jonathan 	}
     92       1.1  jonathan 	return NULL;
     93       1.1  jonathan }
     94       1.1  jonathan 
     95       1.1  jonathan /*
     96       1.1  jonathan  * ipcomp_init() is called when an CPI is being set up.
     97       1.1  jonathan  */
     98       1.1  jonathan static int
     99      1.24  drochner ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
    100       1.1  jonathan {
    101      1.24  drochner 	const struct comp_algo *tcomp;
    102       1.1  jonathan 	struct cryptoini cric;
    103      1.17       tls 	int ses;
    104       1.1  jonathan 
    105       1.1  jonathan 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
    106       1.1  jonathan 	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
    107       1.1  jonathan 	if (tcomp == NULL) {
    108      1.34  christos 		DPRINTF(("%s: unsupported compression algorithm %d\n",
    109      1.34  christos 		    __func__, sav->alg_comp));
    110       1.1  jonathan 		return EINVAL;
    111       1.1  jonathan 	}
    112       1.1  jonathan 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
    113       1.1  jonathan 	sav->tdb_xform = xsp;
    114       1.1  jonathan 	sav->tdb_compalgxform = tcomp;
    115       1.1  jonathan 
    116       1.1  jonathan 	/* Initialize crypto session */
    117      1.34  christos 	memset(&cric, 0, sizeof(cric));
    118       1.1  jonathan 	cric.cri_alg = sav->tdb_compalgxform->type;
    119       1.1  jonathan 
    120      1.17       tls 	ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
    121      1.17       tls 	return ses;
    122       1.1  jonathan }
    123       1.1  jonathan 
    124       1.1  jonathan /*
    125       1.1  jonathan  * ipcomp_zeroize() used when IPCA is deleted
    126       1.1  jonathan  */
    127       1.1  jonathan static int
    128       1.1  jonathan ipcomp_zeroize(struct secasvar *sav)
    129       1.1  jonathan {
    130       1.1  jonathan 	int err;
    131       1.1  jonathan 
    132       1.1  jonathan 	err = crypto_freesession(sav->tdb_cryptoid);
    133       1.1  jonathan 	sav->tdb_cryptoid = 0;
    134       1.1  jonathan 	return err;
    135       1.1  jonathan }
    136       1.1  jonathan 
    137       1.1  jonathan /*
    138       1.1  jonathan  * ipcomp_input() gets called to uncompress an input packet
    139       1.1  jonathan  */
    140       1.1  jonathan static int
    141      1.42     ozaki ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    142       1.1  jonathan {
    143       1.1  jonathan 	struct tdb_crypto *tc;
    144       1.1  jonathan 	struct cryptodesc *crdc;
    145       1.1  jonathan 	struct cryptop *crp;
    146      1.55     ozaki 	int error, hlen = IPCOMP_HLENGTH, stat = IPCOMP_STAT_CRYPTO;
    147       1.1  jonathan 
    148  1.59.2.2  pgoyette 	KASSERT(skip + hlen <= m->m_pkthdr.len);
    149       1.1  jonathan 
    150       1.1  jonathan 	/* Get crypto descriptors */
    151       1.1  jonathan 	crp = crypto_getreq(1);
    152       1.1  jonathan 	if (crp == NULL) {
    153      1.34  christos 		DPRINTF(("%s: no crypto descriptors\n", __func__));
    154      1.55     ozaki 		error = ENOBUFS;
    155      1.55     ozaki 		goto error_m;
    156       1.1  jonathan 	}
    157       1.1  jonathan 	/* Get IPsec-specific opaque pointer */
    158      1.52     ozaki 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    159       1.1  jonathan 	if (tc == NULL) {
    160      1.34  christos 		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
    161      1.55     ozaki 		error = ENOBUFS;
    162      1.55     ozaki 		goto error_crp;
    163       1.1  jonathan 	}
    164      1.29  drochner 
    165      1.29  drochner 	error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
    166      1.29  drochner 	if (error) {
    167      1.34  christos 		DPRINTF(("%s: m_makewritable failed\n", __func__));
    168      1.55     ozaki 		goto error_tc;
    169      1.29  drochner 	}
    170      1.29  drochner 
    171      1.51     ozaki     {
    172      1.51     ozaki 	int s = pserialize_read_enter();
    173      1.51     ozaki 
    174      1.51     ozaki 	/*
    175      1.51     ozaki 	 * Take another reference to the SA for opencrypto callback.
    176      1.51     ozaki 	 */
    177      1.51     ozaki 	if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
    178      1.51     ozaki 		pserialize_read_exit(s);
    179      1.55     ozaki 		stat = IPCOMP_STAT_NOTDB;
    180      1.55     ozaki 		error = ENOENT;
    181      1.55     ozaki 		goto error_tc;
    182      1.51     ozaki 	}
    183      1.51     ozaki 	KEY_SA_REF(sav);
    184      1.51     ozaki 	pserialize_read_exit(s);
    185      1.51     ozaki     }
    186      1.51     ozaki 
    187       1.1  jonathan 	crdc = crp->crp_desc;
    188       1.1  jonathan 
    189       1.1  jonathan 	crdc->crd_skip = skip + hlen;
    190       1.1  jonathan 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
    191      1.29  drochner 	crdc->crd_inject = 0; /* unused */
    192       1.1  jonathan 
    193       1.1  jonathan 	/* Decompression operation */
    194       1.1  jonathan 	crdc->crd_alg = sav->tdb_compalgxform->type;
    195       1.1  jonathan 
    196       1.1  jonathan 	/* Crypto operation descriptor */
    197       1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
    198      1.25  drochner 	crp->crp_olen = MCLBYTES; /* hint to decompression code */
    199       1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    200      1.13  degroote 	crp->crp_buf = m;
    201       1.1  jonathan 	crp->crp_callback = ipcomp_input_cb;
    202       1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    203      1.13  degroote 	crp->crp_opaque = tc;
    204       1.1  jonathan 
    205       1.1  jonathan 	/* These are passed as-is to the callback */
    206       1.1  jonathan 	tc->tc_spi = sav->spi;
    207       1.1  jonathan 	tc->tc_dst = sav->sah->saidx.dst;
    208       1.1  jonathan 	tc->tc_proto = sav->sah->saidx.proto;
    209       1.1  jonathan 	tc->tc_protoff = protoff;
    210       1.1  jonathan 	tc->tc_skip = skip;
    211      1.42     ozaki 	tc->tc_sav = sav;
    212       1.1  jonathan 
    213       1.1  jonathan 	return crypto_dispatch(crp);
    214      1.55     ozaki 
    215      1.55     ozaki error_tc:
    216      1.55     ozaki 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    217      1.55     ozaki error_crp:
    218      1.55     ozaki 	crypto_freereq(crp);
    219      1.55     ozaki error_m:
    220      1.55     ozaki 	m_freem(m);
    221      1.55     ozaki 	IPCOMP_STATINC(stat);
    222      1.55     ozaki 	return error;
    223       1.1  jonathan }
    224       1.1  jonathan 
    225       1.1  jonathan #ifdef INET6
    226      1.40     ozaki #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do {		     \
    227       1.1  jonathan 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    228      1.40     ozaki 		error = ipsec6_common_input_cb(m, sav, skip, protoff);	     \
    229       1.1  jonathan 	} else {							     \
    230      1.40     ozaki 		error = ipsec4_common_input_cb(m, sav, skip, protoff);       \
    231       1.1  jonathan 	}								     \
    232       1.1  jonathan } while (0)
    233       1.1  jonathan #else
    234      1.40     ozaki #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff)			     \
    235      1.40     ozaki 	(error = ipsec4_common_input_cb(m, sav, skip, protoff))
    236       1.1  jonathan #endif
    237       1.1  jonathan 
    238       1.1  jonathan /*
    239       1.1  jonathan  * IPComp input callback from the crypto driver.
    240       1.1  jonathan  */
    241       1.1  jonathan static int
    242       1.1  jonathan ipcomp_input_cb(struct cryptop *crp)
    243       1.1  jonathan {
    244      1.38       ryo 	char buf[IPSEC_ADDRSTRLEN];
    245       1.1  jonathan 	struct tdb_crypto *tc;
    246       1.1  jonathan 	int skip, protoff;
    247       1.1  jonathan 	struct mbuf *m;
    248       1.1  jonathan 	struct secasvar *sav;
    249      1.31       mrg 	struct secasindex *saidx __diagused;
    250      1.48     ozaki 	int hlen = IPCOMP_HLENGTH, error, clen;
    251      1.34  christos 	uint8_t nproto;
    252      1.57      maxv 	struct ipcomp *ipc;
    253      1.48     ozaki 	IPSEC_DECLARE_LOCK_VARIABLE;
    254       1.1  jonathan 
    255      1.36     ozaki 	KASSERT(crp->crp_opaque != NULL);
    256      1.34  christos 	tc = crp->crp_opaque;
    257       1.1  jonathan 	skip = tc->tc_skip;
    258       1.1  jonathan 	protoff = tc->tc_protoff;
    259      1.34  christos 	m = crp->crp_buf;
    260       1.1  jonathan 
    261      1.48     ozaki 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    262       1.1  jonathan 
    263      1.42     ozaki 	sav = tc->tc_sav;
    264       1.1  jonathan 	saidx = &sav->sah->saidx;
    265      1.36     ozaki 	KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
    266      1.36     ozaki 	    saidx->dst.sa.sa_family == AF_INET6,
    267      1.36     ozaki 	    "unexpected protocol family %u", saidx->dst.sa.sa_family);
    268       1.1  jonathan 
    269       1.1  jonathan 	/* Check for crypto errors */
    270       1.1  jonathan 	if (crp->crp_etype) {
    271       1.1  jonathan 		/* Reset the session ID */
    272       1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    273       1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    274       1.1  jonathan 
    275       1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    276      1.50     ozaki 			KEY_SA_UNREF(&sav);
    277      1.48     ozaki 			IPSEC_RELEASE_GLOBAL_LOCKS();
    278       1.1  jonathan 			return crypto_dispatch(crp);
    279       1.1  jonathan 		}
    280       1.1  jonathan 
    281      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    282      1.34  christos 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    283       1.1  jonathan 		error = crp->crp_etype;
    284       1.1  jonathan 		goto bad;
    285       1.1  jonathan 	}
    286      1.45     ozaki 
    287      1.33  christos 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    288       1.1  jonathan 
    289      1.20  degroote 	/* Update the counters */
    290      1.20  degroote 	IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
    291      1.20  degroote 
    292      1.57      maxv 	/* Length of data after processing */
    293      1.57      maxv 	clen = crp->crp_olen;
    294       1.1  jonathan 
    295       1.1  jonathan 	/* Release the crypto descriptors */
    296      1.52     ozaki 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    297      1.47     ozaki 	tc = NULL;
    298      1.57      maxv 	crypto_freereq(crp);
    299      1.57      maxv 	crp = NULL;
    300       1.1  jonathan 
    301       1.1  jonathan 	/* In case it's not done already, adjust the size of the mbuf chain */
    302       1.1  jonathan 	m->m_pkthdr.len = clen + hlen + skip;
    303       1.1  jonathan 
    304  1.59.2.2  pgoyette 	/*
    305  1.59.2.2  pgoyette 	 * Get the next protocol field.
    306  1.59.2.2  pgoyette 	 *
    307  1.59.2.2  pgoyette 	 * XXX: Really, we should use m_copydata instead of m_pullup.
    308  1.59.2.2  pgoyette 	 */
    309       1.1  jonathan 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
    310      1.57      maxv 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    311      1.34  christos 		DPRINTF(("%s: m_pullup failed\n", __func__));
    312      1.57      maxv 		error = EINVAL;
    313       1.1  jonathan 		goto bad;
    314       1.1  jonathan 	}
    315      1.57      maxv 	ipc = (struct ipcomp *)(mtod(m, uint8_t *) + skip);
    316      1.57      maxv 	nproto = ipc->comp_nxt;
    317  1.59.2.2  pgoyette 
    318      1.34  christos 	switch (nproto) {
    319      1.34  christos 	case IPPROTO_IPCOMP:
    320      1.34  christos 	case IPPROTO_AH:
    321      1.34  christos 	case IPPROTO_ESP:
    322      1.26       spz 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    323      1.34  christos 		DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
    324      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    325      1.34  christos 		    (u_long) ntohl(sav->spi)));
    326      1.26       spz 		error = EINVAL;
    327      1.26       spz 		goto bad;
    328      1.34  christos 	default:
    329      1.34  christos 		break;
    330      1.26       spz 	}
    331       1.1  jonathan 
    332       1.1  jonathan 	/* Remove the IPCOMP header */
    333       1.1  jonathan 	error = m_striphdr(m, skip, hlen);
    334       1.1  jonathan 	if (error) {
    335      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    336      1.34  christos 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
    337      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    338      1.38       ryo 		    (u_long) ntohl(sav->spi)));
    339       1.1  jonathan 		goto bad;
    340       1.1  jonathan 	}
    341       1.1  jonathan 
    342       1.1  jonathan 	/* Restore the Next Protocol field */
    343  1.59.2.2  pgoyette 	m_copyback(m, protoff, sizeof(nproto), &nproto);
    344       1.1  jonathan 
    345      1.40     ozaki 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
    346       1.1  jonathan 
    347      1.50     ozaki 	KEY_SA_UNREF(&sav);
    348      1.48     ozaki 	IPSEC_RELEASE_GLOBAL_LOCKS();
    349       1.1  jonathan 	return error;
    350      1.57      maxv 
    351       1.1  jonathan bad:
    352       1.1  jonathan 	if (sav)
    353      1.50     ozaki 		KEY_SA_UNREF(&sav);
    354      1.48     ozaki 	IPSEC_RELEASE_GLOBAL_LOCKS();
    355       1.1  jonathan 	if (m)
    356       1.1  jonathan 		m_freem(m);
    357       1.1  jonathan 	if (tc != NULL)
    358      1.52     ozaki 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    359       1.1  jonathan 	if (crp)
    360       1.1  jonathan 		crypto_freereq(crp);
    361       1.1  jonathan 	return error;
    362       1.1  jonathan }
    363       1.1  jonathan 
    364       1.1  jonathan /*
    365       1.1  jonathan  * IPComp output routine, called by ipsec[46]_process_packet()
    366       1.1  jonathan  */
    367       1.1  jonathan static int
    368      1.57      maxv ipcomp_output(struct mbuf *m, const struct ipsecrequest *isr,
    369  1.59.2.4  pgoyette     struct secasvar *sav, int skip, int protoff)
    370       1.1  jonathan {
    371      1.38       ryo 	char buf[IPSEC_ADDRSTRLEN];
    372      1.24  drochner 	const struct comp_algo *ipcompx;
    373       1.9  degroote 	int error, ralen, hlen, maxpacketsize;
    374       1.1  jonathan 	struct cryptodesc *crdc;
    375       1.1  jonathan 	struct cryptop *crp;
    376       1.1  jonathan 	struct tdb_crypto *tc;
    377       1.1  jonathan 
    378      1.43     ozaki 	KASSERT(sav != NULL);
    379      1.36     ozaki 	KASSERT(sav->tdb_compalgxform != NULL);
    380       1.1  jonathan 	ipcompx = sav->tdb_compalgxform;
    381       1.1  jonathan 
    382      1.57      maxv 	/* Raw payload length before comp. */
    383      1.57      maxv 	ralen = m->m_pkthdr.len - skip;
    384      1.57      maxv 
    385      1.57      maxv 	/* Don't process the packet if it is too short */
    386       1.9  degroote 	if (ralen < ipcompx->minlen) {
    387      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
    388      1.43     ozaki 		return ipsec_process_done(m, isr, sav);
    389       1.9  degroote 	}
    390       1.9  degroote 
    391       1.1  jonathan 	hlen = IPCOMP_HLENGTH;
    392       1.1  jonathan 
    393      1.18   thorpej 	IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
    394       1.1  jonathan 
    395       1.1  jonathan 	/* Check for maximum packet size violations. */
    396       1.1  jonathan 	switch (sav->sah->saidx.dst.sa.sa_family) {
    397       1.1  jonathan #ifdef INET
    398       1.1  jonathan 	case AF_INET:
    399      1.57      maxv 		maxpacketsize = IP_MAXPACKET;
    400       1.1  jonathan 		break;
    401      1.57      maxv #endif
    402       1.1  jonathan #ifdef INET6
    403       1.1  jonathan 	case AF_INET6:
    404      1.57      maxv 		maxpacketsize = IPV6_MAXPACKET;
    405       1.1  jonathan 		break;
    406      1.57      maxv #endif
    407       1.1  jonathan 	default:
    408      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    409      1.34  christos 		DPRINTF(("%s: unknown/unsupported protocol family %d"
    410      1.34  christos 		    ", IPCA %s/%08lx\n", __func__,
    411       1.1  jonathan 		    sav->sah->saidx.dst.sa.sa_family,
    412      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    413       1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    414       1.1  jonathan 		error = EPFNOSUPPORT;
    415       1.1  jonathan 		goto bad;
    416       1.1  jonathan 	}
    417       1.1  jonathan 	if (skip + hlen + ralen > maxpacketsize) {
    418      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
    419      1.34  christos 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
    420      1.34  christos 		    "(len %u, max len %u)\n", __func__,
    421      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    422       1.1  jonathan 		    (u_long) ntohl(sav->spi),
    423       1.1  jonathan 		    skip + hlen + ralen, maxpacketsize));
    424       1.1  jonathan 		error = EMSGSIZE;
    425       1.1  jonathan 		goto bad;
    426       1.1  jonathan 	}
    427       1.1  jonathan 
    428       1.1  jonathan 	/* Update the counters */
    429      1.18   thorpej 	IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
    430       1.1  jonathan 
    431       1.1  jonathan 	m = m_clone(m);
    432       1.1  jonathan 	if (m == NULL) {
    433      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    434      1.34  christos 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
    435      1.38       ryo 		    __func__,
    436      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    437       1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    438       1.1  jonathan 		error = ENOBUFS;
    439       1.1  jonathan 		goto bad;
    440       1.1  jonathan 	}
    441       1.1  jonathan 
    442       1.1  jonathan 	/* Ok now, we can pass to the crypto processing */
    443       1.1  jonathan 
    444       1.1  jonathan 	/* Get crypto descriptors */
    445       1.1  jonathan 	crp = crypto_getreq(1);
    446       1.1  jonathan 	if (crp == NULL) {
    447      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    448      1.34  christos 		DPRINTF(("%s: failed to acquire crypto descriptor\n",
    449      1.34  christos 		    __func__));
    450       1.1  jonathan 		error = ENOBUFS;
    451       1.1  jonathan 		goto bad;
    452       1.1  jonathan 	}
    453       1.1  jonathan 	crdc = crp->crp_desc;
    454       1.1  jonathan 
    455       1.1  jonathan 	/* Compression descriptor */
    456       1.9  degroote 	crdc->crd_skip = skip;
    457       1.9  degroote 	crdc->crd_len = m->m_pkthdr.len - skip;
    458       1.1  jonathan 	crdc->crd_flags = CRD_F_COMP;
    459       1.9  degroote 	crdc->crd_inject = skip;
    460       1.1  jonathan 
    461       1.1  jonathan 	/* Compression operation */
    462       1.1  jonathan 	crdc->crd_alg = ipcompx->type;
    463       1.1  jonathan 
    464       1.1  jonathan 	/* IPsec-specific opaque crypto info */
    465      1.52     ozaki 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    466       1.1  jonathan 	if (tc == NULL) {
    467      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    468      1.34  christos 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
    469       1.1  jonathan 		crypto_freereq(crp);
    470       1.1  jonathan 		error = ENOBUFS;
    471       1.1  jonathan 		goto bad;
    472       1.1  jonathan 	}
    473       1.1  jonathan 
    474      1.49     ozaki     {
    475      1.49     ozaki 	int s = pserialize_read_enter();
    476      1.49     ozaki 
    477      1.51     ozaki 	/*
    478      1.51     ozaki 	 * Take another reference to the SP and the SA for opencrypto callback.
    479      1.51     ozaki 	 */
    480      1.51     ozaki 	if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
    481      1.51     ozaki 	    sav->state == SADB_SASTATE_DEAD)) {
    482      1.49     ozaki 		pserialize_read_exit(s);
    483      1.52     ozaki 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    484      1.49     ozaki 		crypto_freereq(crp);
    485      1.49     ozaki 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    486      1.49     ozaki 		error = ENOENT;
    487      1.49     ozaki 		goto bad;
    488      1.49     ozaki 	}
    489      1.49     ozaki 	KEY_SP_REF(isr->sp);
    490      1.51     ozaki 	KEY_SA_REF(sav);
    491      1.49     ozaki 	pserialize_read_exit(s);
    492      1.49     ozaki     }
    493      1.49     ozaki 
    494       1.1  jonathan 	tc->tc_isr = isr;
    495       1.1  jonathan 	tc->tc_spi = sav->spi;
    496       1.1  jonathan 	tc->tc_dst = sav->sah->saidx.dst;
    497       1.1  jonathan 	tc->tc_proto = sav->sah->saidx.proto;
    498      1.10  degroote 	tc->tc_skip = skip;
    499      1.10  degroote 	tc->tc_protoff = protoff;
    500      1.42     ozaki 	tc->tc_sav = sav;
    501       1.1  jonathan 
    502       1.1  jonathan 	/* Crypto operation descriptor */
    503       1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
    504       1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    505      1.13  degroote 	crp->crp_buf = m;
    506       1.1  jonathan 	crp->crp_callback = ipcomp_output_cb;
    507      1.13  degroote 	crp->crp_opaque = tc;
    508       1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    509       1.1  jonathan 
    510       1.1  jonathan 	return crypto_dispatch(crp);
    511      1.57      maxv 
    512       1.1  jonathan bad:
    513       1.1  jonathan 	if (m)
    514       1.1  jonathan 		m_freem(m);
    515      1.57      maxv 	return error;
    516       1.1  jonathan }
    517       1.1  jonathan 
    518       1.1  jonathan /*
    519       1.1  jonathan  * IPComp output callback from the crypto driver.
    520       1.1  jonathan  */
    521       1.1  jonathan static int
    522       1.1  jonathan ipcomp_output_cb(struct cryptop *crp)
    523       1.1  jonathan {
    524      1.38       ryo 	char buf[IPSEC_ADDRSTRLEN];
    525       1.1  jonathan 	struct tdb_crypto *tc;
    526      1.53     ozaki 	const struct ipsecrequest *isr;
    527       1.1  jonathan 	struct secasvar *sav;
    528       1.9  degroote 	struct mbuf *m, *mo;
    529      1.48     ozaki 	int error, skip, rlen, roff;
    530      1.34  christos 	uint8_t prot;
    531      1.34  christos 	uint16_t cpi;
    532       1.9  degroote 	struct ipcomp * ipcomp;
    533      1.48     ozaki 	IPSEC_DECLARE_LOCK_VARIABLE;
    534       1.9  degroote 
    535      1.36     ozaki 	KASSERT(crp->crp_opaque != NULL);
    536      1.34  christos 	tc = crp->crp_opaque;
    537      1.34  christos 	m = crp->crp_buf;
    538       1.1  jonathan 	skip = tc->tc_skip;
    539       1.1  jonathan 	rlen = crp->crp_ilen - skip;
    540       1.1  jonathan 
    541      1.48     ozaki 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    542       1.1  jonathan 
    543       1.1  jonathan 	isr = tc->tc_isr;
    544      1.42     ozaki 	sav = tc->tc_sav;
    545       1.1  jonathan 
    546       1.1  jonathan 	/* Check for crypto errors */
    547       1.1  jonathan 	if (crp->crp_etype) {
    548       1.1  jonathan 		/* Reset session ID */
    549       1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    550       1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    551       1.1  jonathan 
    552       1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    553      1.48     ozaki 			IPSEC_RELEASE_GLOBAL_LOCKS();
    554       1.1  jonathan 			return crypto_dispatch(crp);
    555       1.1  jonathan 		}
    556      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    557      1.34  christos 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    558       1.1  jonathan 		error = crp->crp_etype;
    559       1.1  jonathan 		goto bad;
    560       1.1  jonathan 	}
    561      1.45     ozaki 
    562      1.33  christos 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    563       1.1  jonathan 
    564       1.1  jonathan 	if (rlen > crp->crp_olen) {
    565       1.9  degroote 		/* Inject IPCOMP header */
    566       1.9  degroote 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
    567       1.9  degroote 		if (mo == NULL) {
    568      1.18   thorpej 			IPCOMP_STATINC(IPCOMP_STAT_WRAP);
    569      1.34  christos 			DPRINTF(("%s: failed to inject IPCOMP header for "
    570      1.34  christos 			    "IPCA %s/%08lx\n", __func__,
    571      1.38       ryo 			    ipsec_address(&sav->sah->saidx.dst, buf,
    572      1.38       ryo 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    573       1.9  degroote 			error = ENOBUFS;
    574       1.9  degroote 			goto bad;
    575       1.9  degroote 		}
    576      1.12  degroote 		ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
    577       1.9  degroote 
    578       1.9  degroote 		/* Initialize the IPCOMP header */
    579       1.9  degroote 		/* XXX alignment always correct? */
    580       1.9  degroote 		switch (sav->sah->saidx.dst.sa.sa_family) {
    581       1.9  degroote #ifdef INET
    582       1.9  degroote 		case AF_INET:
    583       1.9  degroote 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
    584      1.57      maxv 			break;
    585      1.57      maxv #endif
    586       1.9  degroote #ifdef INET6
    587       1.9  degroote 		case AF_INET6:
    588       1.9  degroote 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
    589      1.57      maxv 			break;
    590       1.9  degroote #endif
    591       1.9  degroote 		}
    592       1.9  degroote 		ipcomp->comp_flags = 0;
    593       1.9  degroote 
    594       1.9  degroote 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
    595      1.57      maxv 			cpi = sav->alg_enc;
    596       1.9  degroote 		else
    597       1.9  degroote 			cpi = ntohl(sav->spi) & 0xffff;
    598       1.9  degroote 		ipcomp->comp_cpi = htons(cpi);
    599       1.9  degroote 
    600       1.9  degroote 		/* Fix Next Protocol in IPv4/IPv6 header */
    601       1.9  degroote 		prot = IPPROTO_IPCOMP;
    602  1.59.2.2  pgoyette 		m_copyback(m, tc->tc_protoff, sizeof(prot), &prot);
    603       1.9  degroote 
    604       1.1  jonathan 		/* Adjust the length in the IP header */
    605       1.1  jonathan 		switch (sav->sah->saidx.dst.sa.sa_family) {
    606       1.1  jonathan #ifdef INET
    607       1.1  jonathan 		case AF_INET:
    608       1.1  jonathan 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    609       1.1  jonathan 			break;
    610      1.57      maxv #endif
    611       1.1  jonathan #ifdef INET6
    612       1.1  jonathan 		case AF_INET6:
    613       1.1  jonathan 			mtod(m, struct ip6_hdr *)->ip6_plen =
    614  1.59.2.1  pgoyette 			    htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    615       1.1  jonathan 			break;
    616      1.57      maxv #endif
    617       1.1  jonathan 		default:
    618      1.18   thorpej 			IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    619       1.1  jonathan 			DPRINTF(("ipcomp_output: unknown/unsupported protocol "
    620       1.5     perry 			    "family %d, IPCA %s/%08lx\n",
    621       1.1  jonathan 			    sav->sah->saidx.dst.sa.sa_family,
    622      1.38       ryo 			    ipsec_address(&sav->sah->saidx.dst, buf,
    623      1.38       ryo 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    624       1.1  jonathan 			error = EPFNOSUPPORT;
    625       1.1  jonathan 			goto bad;
    626       1.1  jonathan 		}
    627       1.1  jonathan 	} else {
    628       1.1  jonathan 		/* compression was useless, we have lost time */
    629      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_USELESS);
    630      1.57      maxv 		DPRINTF(("ipcomp_output_cb: compression was useless: initial"
    631      1.57      maxv 		    "size was %d and compressed size is %d\n", rlen,
    632      1.57      maxv 		    crp->crp_olen));
    633       1.1  jonathan 	}
    634       1.1  jonathan 
    635       1.1  jonathan 	/* Release the crypto descriptor */
    636      1.52     ozaki 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    637       1.1  jonathan 	crypto_freereq(crp);
    638       1.1  jonathan 
    639       1.1  jonathan 	/* NB: m is reclaimed by ipsec_process_done. */
    640      1.43     ozaki 	error = ipsec_process_done(m, isr, sav);
    641      1.50     ozaki 	KEY_SA_UNREF(&sav);
    642      1.49     ozaki 	KEY_SP_UNREF(&isr->sp);
    643      1.48     ozaki 	IPSEC_RELEASE_GLOBAL_LOCKS();
    644       1.1  jonathan 	return error;
    645      1.57      maxv 
    646       1.1  jonathan bad:
    647       1.1  jonathan 	if (sav)
    648      1.50     ozaki 		KEY_SA_UNREF(&sav);
    649      1.49     ozaki 	KEY_SP_UNREF(&isr->sp);
    650      1.48     ozaki 	IPSEC_RELEASE_GLOBAL_LOCKS();
    651       1.1  jonathan 	if (m)
    652       1.1  jonathan 		m_freem(m);
    653      1.52     ozaki 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    654       1.1  jonathan 	crypto_freereq(crp);
    655       1.1  jonathan 	return error;
    656       1.1  jonathan }
    657       1.1  jonathan 
    658       1.1  jonathan static struct xformsw ipcomp_xformsw = {
    659      1.39     ozaki 	.xf_type	= XF_IPCOMP,
    660      1.39     ozaki 	.xf_flags	= XFT_COMP,
    661      1.39     ozaki 	.xf_name	= "IPcomp",
    662      1.39     ozaki 	.xf_init	= ipcomp_init,
    663      1.39     ozaki 	.xf_zeroize	= ipcomp_zeroize,
    664      1.39     ozaki 	.xf_input	= ipcomp_input,
    665      1.39     ozaki 	.xf_output	= ipcomp_output,
    666      1.39     ozaki 	.xf_next	= NULL,
    667       1.1  jonathan };
    668       1.1  jonathan 
    669      1.37     ozaki void
    670       1.1  jonathan ipcomp_attach(void)
    671       1.1  jonathan {
    672      1.18   thorpej 	ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
    673      1.52     ozaki 	ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
    674      1.52     ozaki 	    coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
    675      1.52     ozaki 	    NULL, NULL, NULL);
    676       1.1  jonathan 	xform_register(&ipcomp_xformsw);
    677       1.1  jonathan }
    678