Home | History | Annotate | Line # | Download | only in netipsec
xform_ipcomp.c revision 1.47
      1 /*	$NetBSD: xform_ipcomp.c,v 1.47 2017/07/20 08:07:14 ozaki-r Exp $	*/
      2 /*	$FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $	*/
      3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
      4 
      5 /*
      6  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org)
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * 1. Redistributions of source code must retain the above copyright
     13  *   notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *   notice, this list of conditions and the following disclaimer in the
     16  *   documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote products
     18  *   derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.47 2017/07/20 08:07:14 ozaki-r Exp $");
     34 
     35 /* IP payload compression protocol (IPComp), see RFC 2393 */
     36 #if defined(_KERNEL_OPT)
     37 #include "opt_inet.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/socket.h>
     44 #include <sys/kernel.h>
     45 #include <sys/protosw.h>
     46 #include <sys/sysctl.h>
     47 #include <sys/socketvar.h> /* for softnet_lock */
     48 #include <sys/pool.h>
     49 
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/ip.h>
     53 #include <netinet/ip_var.h>
     54 
     55 #include <net/route.h>
     56 #include <netipsec/ipsec.h>
     57 #include <netipsec/ipsec_private.h>
     58 #include <netipsec/xform.h>
     59 
     60 #ifdef INET6
     61 #include <netinet/ip6.h>
     62 #include <netipsec/ipsec6.h>
     63 #endif
     64 
     65 #include <netipsec/ipcomp.h>
     66 #include <netipsec/ipcomp_var.h>
     67 
     68 #include <netipsec/key.h>
     69 #include <netipsec/key_debug.h>
     70 
     71 #include <opencrypto/cryptodev.h>
     72 #include <opencrypto/deflate.h>
     73 #include <opencrypto/xform.h>
     74 
     75 percpu_t *ipcompstat_percpu;
     76 
     77 int	ipcomp_enable = 1;
     78 
     79 #ifdef __FreeBSD__
     80 SYSCTL_DECL(_net_inet_ipcomp);
     81 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
     82 	ipcomp_enable,	CTLFLAG_RW,	&ipcomp_enable,	0, "");
     83 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
     84 	stats,		CTLFLAG_RD,	&ipcompstat,	ipcompstat, "");
     85 #endif /* __FreeBSD__ */
     86 
     87 static int ipcomp_input_cb(struct cryptop *crp);
     88 static int ipcomp_output_cb(struct cryptop *crp);
     89 
     90 const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
     91 
     92 static struct pool ipcomp_tdb_crypto_pool;
     93 
     94 const struct comp_algo *
     95 ipcomp_algorithm_lookup(int alg)
     96 {
     97 	switch (alg) {
     98 	case SADB_X_CALG_DEFLATE:
     99 		return &comp_algo_deflate_nogrow;
    100 	}
    101 	return NULL;
    102 }
    103 
    104 /*
    105  * ipcomp_init() is called when an CPI is being set up.
    106  */
    107 static int
    108 ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
    109 {
    110 	const struct comp_algo *tcomp;
    111 	struct cryptoini cric;
    112 	int ses;
    113 
    114 	/* NB: algorithm really comes in alg_enc and not alg_comp! */
    115 	tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
    116 	if (tcomp == NULL) {
    117 		DPRINTF(("%s: unsupported compression algorithm %d\n",
    118 		    __func__, sav->alg_comp));
    119 		return EINVAL;
    120 	}
    121 	sav->alg_comp = sav->alg_enc;		/* set for doing histogram */
    122 	sav->tdb_xform = xsp;
    123 	sav->tdb_compalgxform = tcomp;
    124 
    125 	/* Initialize crypto session */
    126 	memset(&cric, 0, sizeof(cric));
    127 	cric.cri_alg = sav->tdb_compalgxform->type;
    128 
    129 	ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
    130 	return ses;
    131 }
    132 
    133 /*
    134  * ipcomp_zeroize() used when IPCA is deleted
    135  */
    136 static int
    137 ipcomp_zeroize(struct secasvar *sav)
    138 {
    139 	int err;
    140 
    141 	err = crypto_freesession(sav->tdb_cryptoid);
    142 	sav->tdb_cryptoid = 0;
    143 	return err;
    144 }
    145 
    146 /*
    147  * ipcomp_input() gets called to uncompress an input packet
    148  */
    149 static int
    150 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    151 {
    152 	struct tdb_crypto *tc;
    153 	struct cryptodesc *crdc;
    154 	struct cryptop *crp;
    155 	int error, hlen = IPCOMP_HLENGTH;
    156 
    157 	IPSEC_SPLASSERT_SOFTNET(__func__);
    158 
    159 	/* Get crypto descriptors */
    160 	crp = crypto_getreq(1);
    161 	if (crp == NULL) {
    162 		m_freem(m);
    163 		DPRINTF(("%s: no crypto descriptors\n", __func__));
    164 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    165 		return ENOBUFS;
    166 	}
    167 	/* Get IPsec-specific opaque pointer */
    168 	tc = pool_get(&ipcomp_tdb_crypto_pool, PR_NOWAIT);
    169 	if (tc == NULL) {
    170 		m_freem(m);
    171 		crypto_freereq(crp);
    172 		DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
    173 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    174 		return ENOBUFS;
    175 	}
    176 
    177 	error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
    178 	if (error) {
    179 		DPRINTF(("%s: m_makewritable failed\n", __func__));
    180 		m_freem(m);
    181 		pool_put(&ipcomp_tdb_crypto_pool, tc);
    182 		crypto_freereq(crp);
    183 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    184 		return error;
    185 	}
    186 
    187 	crdc = crp->crp_desc;
    188 
    189 	crdc->crd_skip = skip + hlen;
    190 	crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
    191 	crdc->crd_inject = 0; /* unused */
    192 
    193 	/* Decompression operation */
    194 	crdc->crd_alg = sav->tdb_compalgxform->type;
    195 
    196 	/* Crypto operation descriptor */
    197 	crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
    198 	crp->crp_olen = MCLBYTES; /* hint to decompression code */
    199 	crp->crp_flags = CRYPTO_F_IMBUF;
    200 	crp->crp_buf = m;
    201 	crp->crp_callback = ipcomp_input_cb;
    202 	crp->crp_sid = sav->tdb_cryptoid;
    203 	crp->crp_opaque = tc;
    204 
    205 	/* These are passed as-is to the callback */
    206 	tc->tc_spi = sav->spi;
    207 	tc->tc_dst = sav->sah->saidx.dst;
    208 	tc->tc_proto = sav->sah->saidx.proto;
    209 	tc->tc_protoff = protoff;
    210 	tc->tc_skip = skip;
    211 	tc->tc_sav = sav;
    212 	KEY_SA_REF(sav);
    213 
    214 	return crypto_dispatch(crp);
    215 }
    216 
    217 #ifdef INET6
    218 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do {		     \
    219 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
    220 		error = ipsec6_common_input_cb(m, sav, skip, protoff);	     \
    221 	} else {							     \
    222 		error = ipsec4_common_input_cb(m, sav, skip, protoff);       \
    223 	}								     \
    224 } while (0)
    225 #else
    226 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff)			     \
    227 	(error = ipsec4_common_input_cb(m, sav, skip, protoff))
    228 #endif
    229 
    230 /*
    231  * IPComp input callback from the crypto driver.
    232  */
    233 static int
    234 ipcomp_input_cb(struct cryptop *crp)
    235 {
    236 	char buf[IPSEC_ADDRSTRLEN];
    237 	struct tdb_crypto *tc;
    238 	int skip, protoff;
    239 	struct mbuf *m;
    240 	struct secasvar *sav;
    241 	struct secasindex *saidx __diagused;
    242 	int s, hlen = IPCOMP_HLENGTH, error, clen;
    243 	uint8_t nproto;
    244 	void *addr;
    245 	uint16_t dport;
    246 	uint16_t sport;
    247 
    248 	KASSERT(crp->crp_opaque != NULL);
    249 	tc = crp->crp_opaque;
    250 	skip = tc->tc_skip;
    251 	protoff = tc->tc_protoff;
    252 	m = crp->crp_buf;
    253 
    254 	/* find the source port for NAT-T */
    255 	nat_t_ports_get(m, &dport, &sport);
    256 
    257 	s = splsoftnet();
    258 	mutex_enter(softnet_lock);
    259 
    260 	sav = tc->tc_sav;
    261 	if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
    262 		KEY_FREESAV(&sav);
    263 		sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
    264 		    sport, dport);
    265 		if (sav == NULL) {
    266 			IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    267 			DPRINTF(("%s: SA expired while in crypto\n", __func__));
    268 			error = ENOBUFS;		/*XXX*/
    269 			goto bad;
    270 		}
    271 	}
    272 
    273 	saidx = &sav->sah->saidx;
    274 	KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
    275 	    saidx->dst.sa.sa_family == AF_INET6,
    276 	    "unexpected protocol family %u", saidx->dst.sa.sa_family);
    277 
    278 	/* Check for crypto errors */
    279 	if (crp->crp_etype) {
    280 		/* Reset the session ID */
    281 		if (sav->tdb_cryptoid != 0)
    282 			sav->tdb_cryptoid = crp->crp_sid;
    283 
    284 		if (crp->crp_etype == EAGAIN) {
    285 			KEY_FREESAV(&sav);
    286 			mutex_exit(softnet_lock);
    287 			splx(s);
    288 			return crypto_dispatch(crp);
    289 		}
    290 
    291 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    292 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    293 		error = crp->crp_etype;
    294 		goto bad;
    295 	}
    296 
    297 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    298 
    299 	/* Update the counters */
    300 	IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
    301 
    302 
    303 	clen = crp->crp_olen;		/* Length of data after processing */
    304 
    305 	/* Release the crypto descriptors */
    306 	pool_put(&ipcomp_tdb_crypto_pool, tc);
    307 	tc = NULL;
    308 	crypto_freereq(crp), crp = NULL;
    309 
    310 	/* In case it's not done already, adjust the size of the mbuf chain */
    311 	m->m_pkthdr.len = clen + hlen + skip;
    312 
    313 	if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
    314 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);	/*XXX*/
    315 		DPRINTF(("%s: m_pullup failed\n", __func__));
    316 		error = EINVAL;				/*XXX*/
    317 		goto bad;
    318 	}
    319 
    320 	/* Keep the next protocol field */
    321 	addr = (uint8_t*) mtod(m, struct ip *) + skip;
    322 	nproto = ((struct ipcomp *) addr)->comp_nxt;
    323 	switch (nproto) {
    324 	case IPPROTO_IPCOMP:
    325 	case IPPROTO_AH:
    326 	case IPPROTO_ESP:
    327 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    328 		DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
    329 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    330 		    (u_long) ntohl(sav->spi)));
    331 		error = EINVAL;
    332 		goto bad;
    333 	default:
    334 		break;
    335 	}
    336 
    337 	/* Remove the IPCOMP header */
    338 	error = m_striphdr(m, skip, hlen);
    339 	if (error) {
    340 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    341 		DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
    342 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    343 		    (u_long) ntohl(sav->spi)));
    344 		goto bad;
    345 	}
    346 
    347 	/* Restore the Next Protocol field */
    348 	m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *) &nproto);
    349 
    350 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
    351 
    352 	KEY_FREESAV(&sav);
    353 	mutex_exit(softnet_lock);
    354 	splx(s);
    355 	return error;
    356 bad:
    357 	if (sav)
    358 		KEY_FREESAV(&sav);
    359 	mutex_exit(softnet_lock);
    360 	splx(s);
    361 	if (m)
    362 		m_freem(m);
    363 	if (tc != NULL)
    364 		pool_put(&ipcomp_tdb_crypto_pool, tc);
    365 	if (crp)
    366 		crypto_freereq(crp);
    367 	return error;
    368 }
    369 
    370 /*
    371  * IPComp output routine, called by ipsec[46]_process_packet()
    372  */
    373 static int
    374 ipcomp_output(
    375     struct mbuf *m,
    376     struct ipsecrequest *isr,
    377     struct secasvar *sav,
    378     struct mbuf **mp,
    379     int skip,
    380     int protoff
    381 )
    382 {
    383 	char buf[IPSEC_ADDRSTRLEN];
    384 	const struct comp_algo *ipcompx;
    385 	int error, ralen, hlen, maxpacketsize;
    386 	struct cryptodesc *crdc;
    387 	struct cryptop *crp;
    388 	struct tdb_crypto *tc;
    389 
    390 	IPSEC_SPLASSERT_SOFTNET(__func__);
    391 	KASSERT(sav != NULL);
    392 	KASSERT(sav->tdb_compalgxform != NULL);
    393 	ipcompx = sav->tdb_compalgxform;
    394 
    395 	ralen = m->m_pkthdr.len - skip;	/* Raw payload length before comp. */
    396 
    397     /* Don't process the packet if it is too short */
    398 	if (ralen < ipcompx->minlen) {
    399 		IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
    400 		return ipsec_process_done(m, isr, sav);
    401 	}
    402 
    403 	hlen = IPCOMP_HLENGTH;
    404 
    405 	IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
    406 
    407 	/* Check for maximum packet size violations. */
    408 	switch (sav->sah->saidx.dst.sa.sa_family) {
    409 #ifdef INET
    410 	case AF_INET:
    411 		maxpacketsize =  IP_MAXPACKET;
    412 		break;
    413 #endif /* INET */
    414 #ifdef INET6
    415 	case AF_INET6:
    416 		maxpacketsize =  IPV6_MAXPACKET;
    417 		break;
    418 #endif /* INET6 */
    419 	default:
    420 		IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    421 		DPRINTF(("%s: unknown/unsupported protocol family %d"
    422 		    ", IPCA %s/%08lx\n", __func__,
    423 		    sav->sah->saidx.dst.sa.sa_family,
    424 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    425 		    (u_long) ntohl(sav->spi)));
    426 		error = EPFNOSUPPORT;
    427 		goto bad;
    428 	}
    429 	if (skip + hlen + ralen > maxpacketsize) {
    430 		IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
    431 		DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
    432 		    "(len %u, max len %u)\n", __func__,
    433 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    434 		    (u_long) ntohl(sav->spi),
    435 		    skip + hlen + ralen, maxpacketsize));
    436 		error = EMSGSIZE;
    437 		goto bad;
    438 	}
    439 
    440 	/* Update the counters */
    441 	IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
    442 
    443 	m = m_clone(m);
    444 	if (m == NULL) {
    445 		IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
    446 		DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
    447 		    __func__,
    448 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    449 		    (u_long) ntohl(sav->spi)));
    450 		error = ENOBUFS;
    451 		goto bad;
    452 	}
    453 
    454 	/* Ok now, we can pass to the crypto processing */
    455 
    456 	/* Get crypto descriptors */
    457 	crp = crypto_getreq(1);
    458 	if (crp == NULL) {
    459 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    460 		DPRINTF(("%s: failed to acquire crypto descriptor\n",
    461 		    __func__));
    462 		error = ENOBUFS;
    463 		goto bad;
    464 	}
    465 	crdc = crp->crp_desc;
    466 
    467 	/* Compression descriptor */
    468 	crdc->crd_skip = skip;
    469 	crdc->crd_len = m->m_pkthdr.len - skip;
    470 	crdc->crd_flags = CRD_F_COMP;
    471 	crdc->crd_inject = skip;
    472 
    473 	/* Compression operation */
    474 	crdc->crd_alg = ipcompx->type;
    475 
    476 	/* IPsec-specific opaque crypto info */
    477 	tc = pool_get(&ipcomp_tdb_crypto_pool, PR_NOWAIT);
    478 	if (tc == NULL) {
    479 		IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
    480 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
    481 		crypto_freereq(crp);
    482 		error = ENOBUFS;
    483 		goto bad;
    484 	}
    485 
    486 	tc->tc_isr = isr;
    487 	KEY_SP_REF(isr->sp);
    488 	tc->tc_spi = sav->spi;
    489 	tc->tc_dst = sav->sah->saidx.dst;
    490 	tc->tc_proto = sav->sah->saidx.proto;
    491 	tc->tc_skip = skip;
    492 	tc->tc_protoff = protoff;
    493 	tc->tc_sav = sav;
    494 	KEY_SA_REF(sav);
    495 
    496 	/* Crypto operation descriptor */
    497 	crp->crp_ilen = m->m_pkthdr.len;	/* Total input length */
    498 	crp->crp_flags = CRYPTO_F_IMBUF;
    499 	crp->crp_buf = m;
    500 	crp->crp_callback = ipcomp_output_cb;
    501 	crp->crp_opaque = tc;
    502 	crp->crp_sid = sav->tdb_cryptoid;
    503 
    504 	return crypto_dispatch(crp);
    505 bad:
    506 	if (m)
    507 		m_freem(m);
    508 	return (error);
    509 }
    510 
    511 /*
    512  * IPComp output callback from the crypto driver.
    513  */
    514 static int
    515 ipcomp_output_cb(struct cryptop *crp)
    516 {
    517 	char buf[IPSEC_ADDRSTRLEN];
    518 	struct tdb_crypto *tc;
    519 	struct ipsecrequest *isr;
    520 	struct secasvar *sav;
    521 	struct mbuf *m, *mo;
    522 	int s, error, skip, rlen, roff;
    523 	uint8_t prot;
    524 	uint16_t cpi;
    525 	struct ipcomp * ipcomp;
    526 
    527 	KASSERT(crp->crp_opaque != NULL);
    528 	tc = crp->crp_opaque;
    529 	m = crp->crp_buf;
    530 	skip = tc->tc_skip;
    531 	rlen = crp->crp_ilen - skip;
    532 
    533 	s = splsoftnet();
    534 	mutex_enter(softnet_lock);
    535 
    536 	isr = tc->tc_isr;
    537 	sav = tc->tc_sav;
    538 	if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
    539 		IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    540 		IPSECLOG(LOG_DEBUG,
    541 		    "SP is being destroyed while in crypto (id=%u)\n",
    542 		    isr->sp->id);
    543 		error = ENOENT;
    544 		goto bad;
    545 	}
    546 	if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
    547 		KEY_FREESAV(&sav);
    548 		sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
    549 		if (sav == NULL) {
    550 			IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
    551 			DPRINTF(("%s: SA expired while in crypto\n", __func__));
    552 			error = ENOBUFS;		/*XXX*/
    553 			goto bad;
    554 		}
    555 	}
    556 
    557 	/* Check for crypto errors */
    558 	if (crp->crp_etype) {
    559 		/* Reset session ID */
    560 		if (sav->tdb_cryptoid != 0)
    561 			sav->tdb_cryptoid = crp->crp_sid;
    562 
    563 		if (crp->crp_etype == EAGAIN) {
    564 			mutex_exit(softnet_lock);
    565 			splx(s);
    566 			return crypto_dispatch(crp);
    567 		}
    568 		IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
    569 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
    570 		error = crp->crp_etype;
    571 		goto bad;
    572 	}
    573 
    574 	IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
    575 
    576 	if (rlen > crp->crp_olen) {
    577 		/* Inject IPCOMP header */
    578 		mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
    579 		if (mo == NULL) {
    580 			IPCOMP_STATINC(IPCOMP_STAT_WRAP);
    581 			DPRINTF(("%s: failed to inject IPCOMP header for "
    582 			    "IPCA %s/%08lx\n", __func__,
    583 			    ipsec_address(&sav->sah->saidx.dst, buf,
    584 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    585 			error = ENOBUFS;
    586 			goto bad;
    587 		}
    588 		ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
    589 
    590 		/* Initialize the IPCOMP header */
    591 		/* XXX alignment always correct? */
    592 		switch (sav->sah->saidx.dst.sa.sa_family) {
    593 #ifdef INET
    594 		case AF_INET:
    595 			ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
    596 			 break;
    597 #endif /* INET */
    598 #ifdef INET6
    599 		case AF_INET6:
    600 			ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
    601 		break;
    602 #endif
    603 		}
    604 		ipcomp->comp_flags = 0;
    605 
    606 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
    607 			 cpi = sav->alg_enc;
    608 		else
    609 			cpi = ntohl(sav->spi) & 0xffff;
    610 		ipcomp->comp_cpi = htons(cpi);
    611 
    612 		/* Fix Next Protocol in IPv4/IPv6 header */
    613 		prot = IPPROTO_IPCOMP;
    614 		m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
    615 
    616 		/* Adjust the length in the IP header */
    617 		switch (sav->sah->saidx.dst.sa.sa_family) {
    618 #ifdef INET
    619 		case AF_INET:
    620 			mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    621 			break;
    622 #endif /* INET */
    623 #ifdef INET6
    624 		case AF_INET6:
    625 			mtod(m, struct ip6_hdr *)->ip6_plen =
    626 				htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
    627 			break;
    628 #endif /* INET6 */
    629 		default:
    630 			IPCOMP_STATINC(IPCOMP_STAT_NOPF);
    631 			DPRINTF(("ipcomp_output: unknown/unsupported protocol "
    632 			    "family %d, IPCA %s/%08lx\n",
    633 			    sav->sah->saidx.dst.sa.sa_family,
    634 			    ipsec_address(&sav->sah->saidx.dst, buf,
    635 			    sizeof(buf)), (u_long) ntohl(sav->spi)));
    636 			error = EPFNOSUPPORT;
    637 			goto bad;
    638 		}
    639 	} else {
    640 		/* compression was useless, we have lost time */
    641 		IPCOMP_STATINC(IPCOMP_STAT_USELESS);
    642 		DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
    643 				   	"and compressed size is %d\n", rlen, crp->crp_olen));
    644 	}
    645 
    646 
    647 	/* Release the crypto descriptor */
    648 	pool_put(&ipcomp_tdb_crypto_pool, tc);
    649 	crypto_freereq(crp);
    650 
    651 	/* NB: m is reclaimed by ipsec_process_done. */
    652 	error = ipsec_process_done(m, isr, sav);
    653 	KEY_FREESAV(&sav);
    654 	KEY_FREESP(&isr->sp);
    655 	mutex_exit(softnet_lock);
    656 	splx(s);
    657 	return error;
    658 bad:
    659 	if (sav)
    660 		KEY_FREESAV(&sav);
    661 	KEY_FREESP(&isr->sp);
    662 	mutex_exit(softnet_lock);
    663 	splx(s);
    664 	if (m)
    665 		m_freem(m);
    666 	pool_put(&ipcomp_tdb_crypto_pool, tc);
    667 	crypto_freereq(crp);
    668 	return error;
    669 }
    670 
    671 static struct xformsw ipcomp_xformsw = {
    672 	.xf_type	= XF_IPCOMP,
    673 	.xf_flags	= XFT_COMP,
    674 	.xf_name	= "IPcomp",
    675 	.xf_init	= ipcomp_init,
    676 	.xf_zeroize	= ipcomp_zeroize,
    677 	.xf_input	= ipcomp_input,
    678 	.xf_output	= ipcomp_output,
    679 	.xf_next	= NULL,
    680 };
    681 
    682 void
    683 ipcomp_attach(void)
    684 {
    685 	ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
    686 	pool_init(&ipcomp_tdb_crypto_pool, sizeof(struct tdb_crypto),
    687 	    0, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET);
    688 	xform_register(&ipcomp_xformsw);
    689 }
    690