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