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