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