Home | History | Annotate | Line # | Download | only in netipsec
xform_ipcomp.c revision 1.38.2.1
      1  1.38.2.1       snj /*	$NetBSD: xform_ipcomp.c,v 1.38.2.1 2017/10/21 19:43:54 snj Exp $	*/
      2       1.1  jonathan /*	$FreeBSD: src/sys/netipsec/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.38.2.1       snj __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.38.2.1 2017/10/21 19:43:54 snj 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.38.2.1       snj #include <sys/pool.h>
     48  1.38.2.1       snj #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 #include <opencrypto/xform.h>
     74       1.1  jonathan 
     75      1.18   thorpej percpu_t *ipcompstat_percpu;
     76      1.18   thorpej 
     77       1.9  degroote int	ipcomp_enable = 1;
     78       1.1  jonathan 
     79       1.4       tls #ifdef __FreeBSD__
     80       1.1  jonathan SYSCTL_DECL(_net_inet_ipcomp);
     81       1.1  jonathan SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
     82       1.1  jonathan 	ipcomp_enable,	CTLFLAG_RW,	&ipcomp_enable,	0, "");
     83       1.1  jonathan SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
     84       1.1  jonathan 	stats,		CTLFLAG_RD,	&ipcompstat,	ipcompstat, "");
     85       1.4       tls #endif /* __FreeBSD__ */
     86       1.1  jonathan 
     87       1.1  jonathan static int ipcomp_input_cb(struct cryptop *crp);
     88       1.1  jonathan static int ipcomp_output_cb(struct cryptop *crp);
     89       1.1  jonathan 
     90      1.33  christos const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
     91      1.33  christos 
     92  1.38.2.1       snj static pool_cache_t ipcomp_tdb_crypto_pool_cache;
     93  1.38.2.1       snj 
     94      1.24  drochner const struct comp_algo *
     95       1.1  jonathan ipcomp_algorithm_lookup(int alg)
     96       1.1  jonathan {
     97       1.1  jonathan 	switch (alg) {
     98       1.1  jonathan 	case SADB_X_CALG_DEFLATE:
     99      1.25  drochner 		return &comp_algo_deflate_nogrow;
    100       1.1  jonathan 	}
    101       1.1  jonathan 	return NULL;
    102       1.1  jonathan }
    103       1.1  jonathan 
    104       1.1  jonathan /*
    105       1.1  jonathan  * ipcomp_init() is called when an CPI is being set up.
    106       1.1  jonathan  */
    107       1.1  jonathan static int
    108      1.24  drochner ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
    109       1.1  jonathan {
    110      1.24  drochner 	const struct comp_algo *tcomp;
    111       1.1  jonathan 	struct cryptoini cric;
    112      1.17       tls 	int ses;
    113       1.1  jonathan 
    114       1.1  jonathan 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
    115       1.1  jonathan 	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
    116       1.1  jonathan 	if (tcomp == NULL) {
    117      1.34  christos 		DPRINTF(("%s: unsupported compression algorithm %d\n",
    118      1.34  christos 		    __func__, sav->alg_comp));
    119       1.1  jonathan 		return EINVAL;
    120       1.1  jonathan 	}
    121       1.1  jonathan 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
    122       1.1  jonathan 	sav->tdb_xform = xsp;
    123       1.1  jonathan 	sav->tdb_compalgxform = tcomp;
    124       1.1  jonathan 
    125       1.1  jonathan 	/* Initialize crypto session */
    126      1.34  christos 	memset(&cric, 0, sizeof(cric));
    127       1.1  jonathan 	cric.cri_alg = sav->tdb_compalgxform->type;
    128       1.1  jonathan 
    129      1.17       tls 	ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
    130      1.17       tls 	return ses;
    131       1.1  jonathan }
    132       1.1  jonathan 
    133       1.1  jonathan /*
    134       1.1  jonathan  * ipcomp_zeroize() used when IPCA is deleted
    135       1.1  jonathan  */
    136       1.1  jonathan static int
    137       1.1  jonathan ipcomp_zeroize(struct secasvar *sav)
    138       1.1  jonathan {
    139       1.1  jonathan 	int err;
    140       1.1  jonathan 
    141       1.1  jonathan 	err = crypto_freesession(sav->tdb_cryptoid);
    142       1.1  jonathan 	sav->tdb_cryptoid = 0;
    143       1.1  jonathan 	return err;
    144       1.1  jonathan }
    145       1.1  jonathan 
    146       1.1  jonathan /*
    147       1.1  jonathan  * ipcomp_input() gets called to uncompress an input packet
    148       1.1  jonathan  */
    149       1.1  jonathan static int
    150  1.38.2.1       snj ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    151       1.1  jonathan {
    152       1.1  jonathan 	struct tdb_crypto *tc;
    153       1.1  jonathan 	struct cryptodesc *crdc;
    154       1.1  jonathan 	struct cryptop *crp;
    155      1.29  drochner 	int error, hlen = IPCOMP_HLENGTH;
    156       1.1  jonathan 
    157      1.34  christos 	IPSEC_SPLASSERT_SOFTNET(__func__);
    158       1.1  jonathan 
    159       1.1  jonathan 	/* Get crypto descriptors */
    160       1.1  jonathan 	crp = crypto_getreq(1);
    161       1.1  jonathan 	if (crp == NULL) {
    162       1.1  jonathan 		m_freem(m);
    163      1.34  christos 		DPRINTF(("%s: no crypto descriptors\n", __func__));
    164      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    165       1.1  jonathan 		return ENOBUFS;
    166       1.1  jonathan 	}
    167       1.1  jonathan 	/* Get IPsec-specific opaque pointer */
    168  1.38.2.1       snj 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    169       1.1  jonathan 	if (tc == NULL) {
    170       1.1  jonathan 		m_freem(m);
    171       1.1  jonathan 		crypto_freereq(crp);
    172      1.34  christos 		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
    173      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    174       1.1  jonathan 		return ENOBUFS;
    175       1.1  jonathan 	}
    176      1.29  drochner 
    177      1.29  drochner 	error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
    178      1.29  drochner 	if (error) {
    179      1.34  christos 		DPRINTF(("%s: m_makewritable failed\n", __func__));
    180      1.29  drochner 		m_freem(m);
    181  1.38.2.1       snj 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    182      1.29  drochner 		crypto_freereq(crp);
    183      1.29  drochner 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    184      1.29  drochner 		return error;
    185      1.29  drochner 	}
    186      1.29  drochner 
    187  1.38.2.1       snj     {
    188  1.38.2.1       snj 	int s = pserialize_read_enter();
    189  1.38.2.1       snj 
    190  1.38.2.1       snj 	/*
    191  1.38.2.1       snj 	 * Take another reference to the SA for opencrypto callback.
    192  1.38.2.1       snj 	 */
    193  1.38.2.1       snj 	if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
    194  1.38.2.1       snj 		pserialize_read_exit(s);
    195  1.38.2.1       snj 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    196  1.38.2.1       snj 		crypto_freereq(crp);
    197  1.38.2.1       snj 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    198  1.38.2.1       snj 		return ENOENT;
    199  1.38.2.1       snj 	}
    200  1.38.2.1       snj 	KEY_SA_REF(sav);
    201  1.38.2.1       snj 	pserialize_read_exit(s);
    202  1.38.2.1       snj     }
    203  1.38.2.1       snj 
    204       1.1  jonathan 	crdc = crp->crp_desc;
    205       1.1  jonathan 
    206       1.1  jonathan 	crdc->crd_skip = skip + hlen;
    207       1.1  jonathan 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
    208      1.29  drochner 	crdc->crd_inject = 0; /* unused */
    209       1.1  jonathan 
    210       1.1  jonathan 	/* Decompression operation */
    211       1.1  jonathan 	crdc->crd_alg = sav->tdb_compalgxform->type;
    212       1.1  jonathan 
    213       1.1  jonathan 	/* Crypto operation descriptor */
    214       1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
    215      1.25  drochner 	crp->crp_olen = MCLBYTES; /* hint to decompression code */
    216       1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    217      1.13  degroote 	crp->crp_buf = m;
    218       1.1  jonathan 	crp->crp_callback = ipcomp_input_cb;
    219       1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    220      1.13  degroote 	crp->crp_opaque = tc;
    221       1.1  jonathan 
    222       1.1  jonathan 	/* These are passed as-is to the callback */
    223       1.1  jonathan 	tc->tc_spi = sav->spi;
    224       1.1  jonathan 	tc->tc_dst = sav->sah->saidx.dst;
    225       1.1  jonathan 	tc->tc_proto = sav->sah->saidx.proto;
    226       1.1  jonathan 	tc->tc_protoff = protoff;
    227       1.1  jonathan 	tc->tc_skip = skip;
    228  1.38.2.1       snj 	tc->tc_sav = sav;
    229       1.1  jonathan 
    230       1.1  jonathan 	return crypto_dispatch(crp);
    231       1.1  jonathan }
    232       1.1  jonathan 
    233       1.1  jonathan #ifdef INET6
    234  1.38.2.1       snj #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do {		     \
    235       1.1  jonathan 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    236  1.38.2.1       snj 		error = ipsec6_common_input_cb(m, sav, skip, protoff);	     \
    237       1.1  jonathan 	} else {							     \
    238  1.38.2.1       snj 		error = ipsec4_common_input_cb(m, sav, skip, protoff);       \
    239       1.1  jonathan 	}								     \
    240       1.1  jonathan } while (0)
    241       1.1  jonathan #else
    242  1.38.2.1       snj #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff)			     \
    243  1.38.2.1       snj 	(error = ipsec4_common_input_cb(m, sav, skip, protoff))
    244       1.1  jonathan #endif
    245       1.1  jonathan 
    246       1.1  jonathan /*
    247       1.1  jonathan  * IPComp input callback from the crypto driver.
    248       1.1  jonathan  */
    249       1.1  jonathan static int
    250       1.1  jonathan ipcomp_input_cb(struct cryptop *crp)
    251       1.1  jonathan {
    252      1.38       ryo 	char buf[IPSEC_ADDRSTRLEN];
    253       1.1  jonathan 	struct tdb_crypto *tc;
    254       1.1  jonathan 	int skip, protoff;
    255       1.1  jonathan 	struct mbuf *m;
    256       1.1  jonathan 	struct secasvar *sav;
    257      1.31       mrg 	struct secasindex *saidx __diagused;
    258  1.38.2.1       snj 	int hlen = IPCOMP_HLENGTH, error, clen;
    259      1.34  christos 	uint8_t nproto;
    260      1.11  christos 	void *addr;
    261      1.34  christos 	uint16_t dport;
    262      1.34  christos 	uint16_t sport;
    263  1.38.2.1       snj 	IPSEC_DECLARE_LOCK_VARIABLE;
    264       1.1  jonathan 
    265      1.36     ozaki 	KASSERT(crp->crp_opaque != NULL);
    266      1.34  christos 	tc = crp->crp_opaque;
    267       1.1  jonathan 	skip = tc->tc_skip;
    268       1.1  jonathan 	protoff = tc->tc_protoff;
    269      1.34  christos 	m = crp->crp_buf;
    270       1.1  jonathan 
    271      1.14  degroote 	/* find the source port for NAT-T */
    272      1.30  christos 	nat_t_ports_get(m, &dport, &sport);
    273      1.14  degroote 
    274  1.38.2.1       snj 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    275       1.1  jonathan 
    276  1.38.2.1       snj 	sav = tc->tc_sav;
    277  1.38.2.1       snj 	if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
    278  1.38.2.1       snj 		KEY_SA_UNREF(&sav);
    279  1.38.2.1       snj 		sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
    280  1.38.2.1       snj 		    sport, dport);
    281  1.38.2.1       snj 		if (sav == NULL) {
    282  1.38.2.1       snj 			IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    283  1.38.2.1       snj 			DPRINTF(("%s: SA expired while in crypto\n", __func__));
    284  1.38.2.1       snj 			error = ENOBUFS;		/*XXX*/
    285  1.38.2.1       snj 			goto bad;
    286  1.38.2.1       snj 		}
    287       1.1  jonathan 	}
    288       1.1  jonathan 
    289       1.1  jonathan 	saidx = &sav->sah->saidx;
    290      1.36     ozaki 	KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
    291      1.36     ozaki 	    saidx->dst.sa.sa_family == AF_INET6,
    292      1.36     ozaki 	    "unexpected protocol family %u", saidx->dst.sa.sa_family);
    293       1.1  jonathan 
    294       1.1  jonathan 	/* Check for crypto errors */
    295       1.1  jonathan 	if (crp->crp_etype) {
    296       1.1  jonathan 		/* Reset the session ID */
    297       1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    298       1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    299       1.1  jonathan 
    300       1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    301  1.38.2.1       snj 			KEY_SA_UNREF(&sav);
    302  1.38.2.1       snj 			IPSEC_RELEASE_GLOBAL_LOCKS();
    303       1.1  jonathan 			return crypto_dispatch(crp);
    304       1.1  jonathan 		}
    305       1.1  jonathan 
    306      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    307      1.34  christos 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    308       1.1  jonathan 		error = crp->crp_etype;
    309       1.1  jonathan 		goto bad;
    310       1.1  jonathan 	}
    311  1.38.2.1       snj 
    312      1.33  christos 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    313       1.1  jonathan 
    314      1.20  degroote 	/* Update the counters */
    315      1.20  degroote 	IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
    316      1.20  degroote 
    317      1.20  degroote 
    318       1.1  jonathan 	clen = crp->crp_olen;		/* Length of data after processing */
    319       1.1  jonathan 
    320       1.1  jonathan 	/* Release the crypto descriptors */
    321  1.38.2.1       snj 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    322  1.38.2.1       snj 	tc = NULL;
    323       1.1  jonathan 	crypto_freereq(crp), crp = NULL;
    324       1.1  jonathan 
    325       1.1  jonathan 	/* In case it's not done already, adjust the size of the mbuf chain */
    326       1.1  jonathan 	m->m_pkthdr.len = clen + hlen + skip;
    327       1.1  jonathan 
    328       1.1  jonathan 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
    329      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);	/*XXX*/
    330      1.34  christos 		DPRINTF(("%s: m_pullup failed\n", __func__));
    331       1.1  jonathan 		error = EINVAL;				/*XXX*/
    332       1.1  jonathan 		goto bad;
    333       1.1  jonathan 	}
    334       1.1  jonathan 
    335       1.1  jonathan 	/* Keep the next protocol field */
    336      1.15  degroote 	addr = (uint8_t*) mtod(m, struct ip *) + skip;
    337       1.1  jonathan 	nproto = ((struct ipcomp *) addr)->comp_nxt;
    338      1.34  christos 	switch (nproto) {
    339      1.34  christos 	case IPPROTO_IPCOMP:
    340      1.34  christos 	case IPPROTO_AH:
    341      1.34  christos 	case IPPROTO_ESP:
    342      1.26       spz 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    343      1.34  christos 		DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
    344      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    345      1.34  christos 		    (u_long) ntohl(sav->spi)));
    346      1.26       spz 		error = EINVAL;
    347      1.26       spz 		goto bad;
    348      1.34  christos 	default:
    349      1.34  christos 		break;
    350      1.26       spz 	}
    351       1.1  jonathan 
    352       1.1  jonathan 	/* Remove the IPCOMP header */
    353       1.1  jonathan 	error = m_striphdr(m, skip, hlen);
    354       1.1  jonathan 	if (error) {
    355      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    356      1.34  christos 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
    357      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    358      1.38       ryo 		    (u_long) ntohl(sav->spi)));
    359       1.1  jonathan 		goto bad;
    360       1.1  jonathan 	}
    361       1.1  jonathan 
    362       1.1  jonathan 	/* Restore the Next Protocol field */
    363      1.34  christos 	m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *) &nproto);
    364       1.1  jonathan 
    365  1.38.2.1       snj 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
    366       1.1  jonathan 
    367  1.38.2.1       snj 	KEY_SA_UNREF(&sav);
    368  1.38.2.1       snj 	IPSEC_RELEASE_GLOBAL_LOCKS();
    369       1.1  jonathan 	return error;
    370       1.1  jonathan bad:
    371       1.1  jonathan 	if (sav)
    372  1.38.2.1       snj 		KEY_SA_UNREF(&sav);
    373  1.38.2.1       snj 	IPSEC_RELEASE_GLOBAL_LOCKS();
    374       1.1  jonathan 	if (m)
    375       1.1  jonathan 		m_freem(m);
    376       1.1  jonathan 	if (tc != NULL)
    377  1.38.2.1       snj 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    378       1.1  jonathan 	if (crp)
    379       1.1  jonathan 		crypto_freereq(crp);
    380       1.1  jonathan 	return error;
    381       1.1  jonathan }
    382       1.1  jonathan 
    383       1.1  jonathan /*
    384       1.1  jonathan  * IPComp output routine, called by ipsec[46]_process_packet()
    385       1.1  jonathan  */
    386       1.1  jonathan static int
    387       1.1  jonathan ipcomp_output(
    388       1.7  christos     struct mbuf *m,
    389  1.38.2.1       snj     const struct ipsecrequest *isr,
    390  1.38.2.1       snj     struct secasvar *sav,
    391       1.8  christos     struct mbuf **mp,
    392       1.7  christos     int skip,
    393       1.7  christos     int protoff
    394       1.1  jonathan )
    395       1.1  jonathan {
    396      1.38       ryo 	char buf[IPSEC_ADDRSTRLEN];
    397      1.24  drochner 	const struct comp_algo *ipcompx;
    398       1.9  degroote 	int error, ralen, hlen, maxpacketsize;
    399       1.1  jonathan 	struct cryptodesc *crdc;
    400       1.1  jonathan 	struct cryptop *crp;
    401       1.1  jonathan 	struct tdb_crypto *tc;
    402       1.1  jonathan 
    403      1.34  christos 	IPSEC_SPLASSERT_SOFTNET(__func__);
    404  1.38.2.1       snj 	KASSERT(sav != NULL);
    405      1.36     ozaki 	KASSERT(sav->tdb_compalgxform != NULL);
    406       1.1  jonathan 	ipcompx = sav->tdb_compalgxform;
    407       1.1  jonathan 
    408       1.1  jonathan 	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
    409       1.9  degroote 
    410       1.9  degroote     /* Don't process the packet if it is too short */
    411       1.9  degroote 	if (ralen < ipcompx->minlen) {
    412      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
    413  1.38.2.1       snj 		return ipsec_process_done(m, isr, sav);
    414       1.9  degroote 	}
    415       1.9  degroote 
    416       1.1  jonathan 	hlen = IPCOMP_HLENGTH;
    417       1.1  jonathan 
    418      1.18   thorpej 	IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
    419       1.1  jonathan 
    420       1.1  jonathan 	/* Check for maximum packet size violations. */
    421       1.1  jonathan 	switch (sav->sah->saidx.dst.sa.sa_family) {
    422       1.1  jonathan #ifdef INET
    423       1.1  jonathan 	case AF_INET:
    424       1.1  jonathan 		maxpacketsize =  IP_MAXPACKET;
    425       1.1  jonathan 		break;
    426       1.1  jonathan #endif /* INET */
    427       1.1  jonathan #ifdef INET6
    428       1.1  jonathan 	case AF_INET6:
    429       1.1  jonathan 		maxpacketsize =  IPV6_MAXPACKET;
    430       1.1  jonathan 		break;
    431       1.1  jonathan #endif /* INET6 */
    432       1.1  jonathan 	default:
    433      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    434      1.34  christos 		DPRINTF(("%s: unknown/unsupported protocol family %d"
    435      1.34  christos 		    ", IPCA %s/%08lx\n", __func__,
    436       1.1  jonathan 		    sav->sah->saidx.dst.sa.sa_family,
    437      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    438       1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    439       1.1  jonathan 		error = EPFNOSUPPORT;
    440       1.1  jonathan 		goto bad;
    441       1.1  jonathan 	}
    442       1.1  jonathan 	if (skip + hlen + ralen > maxpacketsize) {
    443      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
    444      1.34  christos 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
    445      1.34  christos 		    "(len %u, max len %u)\n", __func__,
    446      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    447       1.1  jonathan 		    (u_long) ntohl(sav->spi),
    448       1.1  jonathan 		    skip + hlen + ralen, maxpacketsize));
    449       1.1  jonathan 		error = EMSGSIZE;
    450       1.1  jonathan 		goto bad;
    451       1.1  jonathan 	}
    452       1.1  jonathan 
    453       1.1  jonathan 	/* Update the counters */
    454      1.18   thorpej 	IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
    455       1.1  jonathan 
    456       1.1  jonathan 	m = m_clone(m);
    457       1.1  jonathan 	if (m == NULL) {
    458      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    459      1.34  christos 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
    460      1.38       ryo 		    __func__,
    461      1.38       ryo 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    462       1.1  jonathan 		    (u_long) ntohl(sav->spi)));
    463       1.1  jonathan 		error = ENOBUFS;
    464       1.1  jonathan 		goto bad;
    465       1.1  jonathan 	}
    466       1.1  jonathan 
    467       1.1  jonathan 	/* Ok now, we can pass to the crypto processing */
    468       1.1  jonathan 
    469       1.1  jonathan 	/* Get crypto descriptors */
    470       1.1  jonathan 	crp = crypto_getreq(1);
    471       1.1  jonathan 	if (crp == NULL) {
    472      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    473      1.34  christos 		DPRINTF(("%s: failed to acquire crypto descriptor\n",
    474      1.34  christos 		    __func__));
    475       1.1  jonathan 		error = ENOBUFS;
    476       1.1  jonathan 		goto bad;
    477       1.1  jonathan 	}
    478       1.1  jonathan 	crdc = crp->crp_desc;
    479       1.1  jonathan 
    480       1.1  jonathan 	/* Compression descriptor */
    481       1.9  degroote 	crdc->crd_skip = skip;
    482       1.9  degroote 	crdc->crd_len = m->m_pkthdr.len - skip;
    483       1.1  jonathan 	crdc->crd_flags = CRD_F_COMP;
    484       1.9  degroote 	crdc->crd_inject = skip;
    485       1.1  jonathan 
    486       1.1  jonathan 	/* Compression operation */
    487       1.1  jonathan 	crdc->crd_alg = ipcompx->type;
    488       1.1  jonathan 
    489       1.1  jonathan 	/* IPsec-specific opaque crypto info */
    490  1.38.2.1       snj 	tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
    491       1.1  jonathan 	if (tc == NULL) {
    492      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    493      1.34  christos 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
    494       1.1  jonathan 		crypto_freereq(crp);
    495       1.1  jonathan 		error = ENOBUFS;
    496       1.1  jonathan 		goto bad;
    497       1.1  jonathan 	}
    498       1.1  jonathan 
    499  1.38.2.1       snj     {
    500  1.38.2.1       snj 	int s = pserialize_read_enter();
    501  1.38.2.1       snj 
    502  1.38.2.1       snj 	/*
    503  1.38.2.1       snj 	 * Take another reference to the SP and the SA for opencrypto callback.
    504  1.38.2.1       snj 	 */
    505  1.38.2.1       snj 	if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
    506  1.38.2.1       snj 	    sav->state == SADB_SASTATE_DEAD)) {
    507  1.38.2.1       snj 		pserialize_read_exit(s);
    508  1.38.2.1       snj 		pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    509  1.38.2.1       snj 		crypto_freereq(crp);
    510  1.38.2.1       snj 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    511  1.38.2.1       snj 		error = ENOENT;
    512  1.38.2.1       snj 		goto bad;
    513  1.38.2.1       snj 	}
    514  1.38.2.1       snj 	KEY_SP_REF(isr->sp);
    515  1.38.2.1       snj 	KEY_SA_REF(sav);
    516  1.38.2.1       snj 	pserialize_read_exit(s);
    517  1.38.2.1       snj     }
    518  1.38.2.1       snj 
    519       1.1  jonathan 	tc->tc_isr = isr;
    520       1.1  jonathan 	tc->tc_spi = sav->spi;
    521       1.1  jonathan 	tc->tc_dst = sav->sah->saidx.dst;
    522       1.1  jonathan 	tc->tc_proto = sav->sah->saidx.proto;
    523      1.10  degroote 	tc->tc_skip = skip;
    524      1.10  degroote 	tc->tc_protoff = protoff;
    525  1.38.2.1       snj 	tc->tc_sav = sav;
    526       1.1  jonathan 
    527       1.1  jonathan 	/* Crypto operation descriptor */
    528       1.1  jonathan 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
    529       1.1  jonathan 	crp->crp_flags = CRYPTO_F_IMBUF;
    530      1.13  degroote 	crp->crp_buf = m;
    531       1.1  jonathan 	crp->crp_callback = ipcomp_output_cb;
    532      1.13  degroote 	crp->crp_opaque = tc;
    533       1.1  jonathan 	crp->crp_sid = sav->tdb_cryptoid;
    534       1.1  jonathan 
    535       1.1  jonathan 	return crypto_dispatch(crp);
    536       1.1  jonathan bad:
    537       1.1  jonathan 	if (m)
    538       1.1  jonathan 		m_freem(m);
    539       1.1  jonathan 	return (error);
    540       1.1  jonathan }
    541       1.1  jonathan 
    542       1.1  jonathan /*
    543       1.1  jonathan  * IPComp output callback from the crypto driver.
    544       1.1  jonathan  */
    545       1.1  jonathan static int
    546       1.1  jonathan ipcomp_output_cb(struct cryptop *crp)
    547       1.1  jonathan {
    548      1.38       ryo 	char buf[IPSEC_ADDRSTRLEN];
    549       1.1  jonathan 	struct tdb_crypto *tc;
    550  1.38.2.1       snj 	const struct ipsecrequest *isr;
    551       1.1  jonathan 	struct secasvar *sav;
    552       1.9  degroote 	struct mbuf *m, *mo;
    553  1.38.2.1       snj 	int error, skip, rlen, roff;
    554      1.34  christos 	uint8_t prot;
    555      1.34  christos 	uint16_t cpi;
    556       1.9  degroote 	struct ipcomp * ipcomp;
    557  1.38.2.1       snj 	IPSEC_DECLARE_LOCK_VARIABLE;
    558       1.9  degroote 
    559      1.36     ozaki 	KASSERT(crp->crp_opaque != NULL);
    560      1.34  christos 	tc = crp->crp_opaque;
    561      1.34  christos 	m = crp->crp_buf;
    562       1.1  jonathan 	skip = tc->tc_skip;
    563       1.1  jonathan 	rlen = crp->crp_ilen - skip;
    564       1.1  jonathan 
    565  1.38.2.1       snj 	IPSEC_ACQUIRE_GLOBAL_LOCKS();
    566       1.1  jonathan 
    567       1.1  jonathan 	isr = tc->tc_isr;
    568  1.38.2.1       snj 	sav = tc->tc_sav;
    569  1.38.2.1       snj 	if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
    570      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    571  1.38.2.1       snj 		IPSECLOG(LOG_DEBUG,
    572  1.38.2.1       snj 		    "SP is being destroyed while in crypto (id=%u)\n",
    573  1.38.2.1       snj 		    isr->sp->id);
    574  1.38.2.1       snj 		error = ENOENT;
    575       1.1  jonathan 		goto bad;
    576       1.1  jonathan 	}
    577  1.38.2.1       snj 	if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
    578  1.38.2.1       snj 		KEY_SA_UNREF(&sav);
    579  1.38.2.1       snj 		sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
    580  1.38.2.1       snj 		if (sav == NULL) {
    581  1.38.2.1       snj 			IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    582  1.38.2.1       snj 			DPRINTF(("%s: SA expired while in crypto\n", __func__));
    583  1.38.2.1       snj 			error = ENOBUFS;		/*XXX*/
    584  1.38.2.1       snj 			goto bad;
    585  1.38.2.1       snj 		}
    586  1.38.2.1       snj 	}
    587       1.1  jonathan 
    588       1.1  jonathan 	/* Check for crypto errors */
    589       1.1  jonathan 	if (crp->crp_etype) {
    590       1.1  jonathan 		/* Reset session ID */
    591       1.1  jonathan 		if (sav->tdb_cryptoid != 0)
    592       1.1  jonathan 			sav->tdb_cryptoid = crp->crp_sid;
    593       1.1  jonathan 
    594       1.1  jonathan 		if (crp->crp_etype == EAGAIN) {
    595  1.38.2.1       snj 			IPSEC_RELEASE_GLOBAL_LOCKS();
    596       1.1  jonathan 			return crypto_dispatch(crp);
    597       1.1  jonathan 		}
    598      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    599      1.34  christos 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    600       1.1  jonathan 		error = crp->crp_etype;
    601       1.1  jonathan 		goto bad;
    602       1.1  jonathan 	}
    603  1.38.2.1       snj 
    604      1.33  christos 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    605       1.1  jonathan 
    606       1.1  jonathan 	if (rlen > crp->crp_olen) {
    607       1.9  degroote 		/* Inject IPCOMP header */
    608       1.9  degroote 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
    609       1.9  degroote 		if (mo == NULL) {
    610      1.18   thorpej 			IPCOMP_STATINC(IPCOMP_STAT_WRAP);
    611      1.34  christos 			DPRINTF(("%s: failed to inject IPCOMP header for "
    612      1.34  christos 			    "IPCA %s/%08lx\n", __func__,
    613      1.38       ryo 			    ipsec_address(&sav->sah->saidx.dst, buf,
    614      1.38       ryo 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    615       1.9  degroote 			error = ENOBUFS;
    616       1.9  degroote 			goto bad;
    617       1.9  degroote 		}
    618      1.12  degroote 		ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
    619       1.9  degroote 
    620       1.9  degroote 		/* Initialize the IPCOMP header */
    621       1.9  degroote 		/* XXX alignment always correct? */
    622       1.9  degroote 		switch (sav->sah->saidx.dst.sa.sa_family) {
    623       1.9  degroote #ifdef INET
    624       1.9  degroote 		case AF_INET:
    625       1.9  degroote 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
    626       1.9  degroote 			 break;
    627       1.9  degroote #endif /* INET */
    628       1.9  degroote #ifdef INET6
    629       1.9  degroote 		case AF_INET6:
    630       1.9  degroote 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
    631       1.9  degroote 		break;
    632       1.9  degroote #endif
    633       1.9  degroote 		}
    634       1.9  degroote 		ipcomp->comp_flags = 0;
    635       1.9  degroote 
    636       1.9  degroote 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
    637       1.9  degroote 			 cpi = sav->alg_enc;
    638       1.9  degroote 		else
    639       1.9  degroote 			cpi = ntohl(sav->spi) & 0xffff;
    640       1.9  degroote 		ipcomp->comp_cpi = htons(cpi);
    641       1.9  degroote 
    642       1.9  degroote 		/* Fix Next Protocol in IPv4/IPv6 header */
    643       1.9  degroote 		prot = IPPROTO_IPCOMP;
    644      1.34  christos 		m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
    645       1.9  degroote 
    646       1.1  jonathan 		/* Adjust the length in the IP header */
    647       1.1  jonathan 		switch (sav->sah->saidx.dst.sa.sa_family) {
    648       1.1  jonathan #ifdef INET
    649       1.1  jonathan 		case AF_INET:
    650       1.1  jonathan 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    651       1.1  jonathan 			break;
    652       1.1  jonathan #endif /* INET */
    653       1.1  jonathan #ifdef INET6
    654       1.1  jonathan 		case AF_INET6:
    655       1.1  jonathan 			mtod(m, struct ip6_hdr *)->ip6_plen =
    656       1.1  jonathan 				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
    657       1.1  jonathan 			break;
    658       1.1  jonathan #endif /* INET6 */
    659       1.1  jonathan 		default:
    660      1.18   thorpej 			IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    661       1.1  jonathan 			DPRINTF(("ipcomp_output: unknown/unsupported protocol "
    662       1.5     perry 			    "family %d, IPCA %s/%08lx\n",
    663       1.1  jonathan 			    sav->sah->saidx.dst.sa.sa_family,
    664      1.38       ryo 			    ipsec_address(&sav->sah->saidx.dst, buf,
    665      1.38       ryo 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    666       1.1  jonathan 			error = EPFNOSUPPORT;
    667       1.1  jonathan 			goto bad;
    668       1.1  jonathan 		}
    669       1.1  jonathan 	} else {
    670       1.1  jonathan 		/* compression was useless, we have lost time */
    671      1.18   thorpej 		IPCOMP_STATINC(IPCOMP_STAT_USELESS);
    672      1.16  degroote 		DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
    673      1.16  degroote 				   	"and compressed size is %d\n", rlen, crp->crp_olen));
    674       1.1  jonathan 	}
    675       1.1  jonathan 
    676      1.16  degroote 
    677       1.1  jonathan 	/* Release the crypto descriptor */
    678  1.38.2.1       snj 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    679       1.1  jonathan 	crypto_freereq(crp);
    680       1.1  jonathan 
    681       1.1  jonathan 	/* NB: m is reclaimed by ipsec_process_done. */
    682  1.38.2.1       snj 	error = ipsec_process_done(m, isr, sav);
    683  1.38.2.1       snj 	KEY_SA_UNREF(&sav);
    684  1.38.2.1       snj 	KEY_SP_UNREF(&isr->sp);
    685  1.38.2.1       snj 	IPSEC_RELEASE_GLOBAL_LOCKS();
    686       1.1  jonathan 	return error;
    687       1.1  jonathan bad:
    688       1.1  jonathan 	if (sav)
    689  1.38.2.1       snj 		KEY_SA_UNREF(&sav);
    690  1.38.2.1       snj 	KEY_SP_UNREF(&isr->sp);
    691  1.38.2.1       snj 	IPSEC_RELEASE_GLOBAL_LOCKS();
    692       1.1  jonathan 	if (m)
    693       1.1  jonathan 		m_freem(m);
    694  1.38.2.1       snj 	pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
    695       1.1  jonathan 	crypto_freereq(crp);
    696       1.1  jonathan 	return error;
    697       1.1  jonathan }
    698       1.1  jonathan 
    699       1.1  jonathan static struct xformsw ipcomp_xformsw = {
    700  1.38.2.1       snj 	.xf_type	= XF_IPCOMP,
    701  1.38.2.1       snj 	.xf_flags	= XFT_COMP,
    702  1.38.2.1       snj 	.xf_name	= "IPcomp",
    703  1.38.2.1       snj 	.xf_init	= ipcomp_init,
    704  1.38.2.1       snj 	.xf_zeroize	= ipcomp_zeroize,
    705  1.38.2.1       snj 	.xf_input	= ipcomp_input,
    706  1.38.2.1       snj 	.xf_output	= ipcomp_output,
    707  1.38.2.1       snj 	.xf_next	= NULL,
    708       1.1  jonathan };
    709       1.1  jonathan 
    710      1.37     ozaki void
    711       1.1  jonathan ipcomp_attach(void)
    712       1.1  jonathan {
    713      1.18   thorpej 	ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
    714  1.38.2.1       snj 	ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
    715  1.38.2.1       snj 	    coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
    716  1.38.2.1       snj 	    NULL, NULL, NULL);
    717       1.1  jonathan 	xform_register(&ipcomp_xformsw);
    718       1.1  jonathan }
    719