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