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