1105197Ssam/* $FreeBSD$ */ 2105197Ssam/* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */ 3105197Ssam 4139823Simp/*- 5105197Ssam * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org) 6105197Ssam * 7105197Ssam * Redistribution and use in source and binary forms, with or without 8105197Ssam * modification, are permitted provided that the following conditions 9105197Ssam * are met: 10105197Ssam * 11105197Ssam * 1. Redistributions of source code must retain the above copyright 12105197Ssam * notice, this list of conditions and the following disclaimer. 13105197Ssam * 2. Redistributions in binary form must reproduce the above copyright 14105197Ssam * notice, this list of conditions and the following disclaimer in the 15105197Ssam * documentation and/or other materials provided with the distribution. 16105197Ssam * 3. The name of the author may not be used to endorse or promote products 17105197Ssam * derived from this software without specific prior written permission. 18105197Ssam * 19105197Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20105197Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21105197Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22105197Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23105197Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24105197Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25105197Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26105197Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27105197Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28105197Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29105197Ssam */ 30105197Ssam 31105197Ssam/* IP payload compression protocol (IPComp), see RFC 2393 */ 32105197Ssam#include "opt_inet.h" 33105197Ssam#include "opt_inet6.h" 34105197Ssam 35105197Ssam#include <sys/param.h> 36105197Ssam#include <sys/systm.h> 37105197Ssam#include <sys/mbuf.h> 38119643Ssam#include <sys/lock.h> 39119643Ssam#include <sys/mutex.h> 40220206Sfabient#include <sys/rwlock.h> 41105197Ssam#include <sys/socket.h> 42105197Ssam#include <sys/kernel.h> 43105197Ssam#include <sys/protosw.h> 44105197Ssam#include <sys/sysctl.h> 45105197Ssam 46105197Ssam#include <netinet/in.h> 47105197Ssam#include <netinet/in_systm.h> 48105197Ssam#include <netinet/ip.h> 49105197Ssam#include <netinet/ip_var.h> 50105197Ssam 51105197Ssam#include <net/route.h> 52195699Srwatson#include <net/vnet.h> 53195699Srwatson 54105197Ssam#include <netipsec/ipsec.h> 55105197Ssam#include <netipsec/xform.h> 56105197Ssam 57105197Ssam#ifdef INET6 58105197Ssam#include <netinet/ip6.h> 59105197Ssam#include <netipsec/ipsec6.h> 60105197Ssam#endif 61105197Ssam 62105197Ssam#include <netipsec/ipcomp.h> 63105197Ssam#include <netipsec/ipcomp_var.h> 64105197Ssam 65105197Ssam#include <netipsec/key.h> 66105197Ssam#include <netipsec/key_debug.h> 67105197Ssam 68105197Ssam#include <opencrypto/cryptodev.h> 69105197Ssam#include <opencrypto/deflate.h> 70105197Ssam#include <opencrypto/xform.h> 71105197Ssam 72199947SbzVNET_DEFINE(int, ipcomp_enable) = 1; 73253088SaeVNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat); 74253088SaeVNET_PCPUSTAT_SYSINIT(ipcompstat); 75105197Ssam 76253088Sae#ifdef VIMAGE 77253088SaeVNET_PCPUSTAT_SYSUNINIT(ipcompstat); 78253088Sae#endif /* VIMAGE */ 79253088Sae 80105197SsamSYSCTL_DECL(_net_inet_ipcomp); 81195699SrwatsonSYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO, 82195699Srwatson ipcomp_enable, CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, ""); 83253088SaeSYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats, 84253088Sae struct ipcompstat, ipcompstat, 85253088Sae "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h"); 86105197Ssam 87105197Ssamstatic int ipcomp_input_cb(struct cryptop *crp); 88105197Ssamstatic int ipcomp_output_cb(struct cryptop *crp); 89105197Ssam 90105197Ssamstruct comp_algo * 91105197Ssamipcomp_algorithm_lookup(int alg) 92105197Ssam{ 93105197Ssam if (alg >= IPCOMP_ALG_MAX) 94105197Ssam return NULL; 95105197Ssam switch (alg) { 96105197Ssam case SADB_X_CALG_DEFLATE: 97105197Ssam return &comp_algo_deflate; 98105197Ssam } 99105197Ssam return NULL; 100105197Ssam} 101105197Ssam 102105197Ssam/* 103105197Ssam * ipcomp_init() is called when an CPI is being set up. 104105197Ssam */ 105105197Ssamstatic int 106105197Ssamipcomp_init(struct secasvar *sav, struct xformsw *xsp) 107105197Ssam{ 108105197Ssam struct comp_algo *tcomp; 109105197Ssam struct cryptoini cric; 110105197Ssam 111105197Ssam /* NB: algorithm really comes in alg_enc and not alg_comp! */ 112105197Ssam tcomp = ipcomp_algorithm_lookup(sav->alg_enc); 113105197Ssam if (tcomp == NULL) { 114120585Ssam DPRINTF(("%s: unsupported compression algorithm %d\n", __func__, 115105197Ssam sav->alg_comp)); 116105197Ssam return EINVAL; 117105197Ssam } 118105197Ssam sav->alg_comp = sav->alg_enc; /* set for doing histogram */ 119105197Ssam sav->tdb_xform = xsp; 120105197Ssam sav->tdb_compalgxform = tcomp; 121105197Ssam 122105197Ssam /* Initialize crypto session */ 123105197Ssam bzero(&cric, sizeof (cric)); 124105197Ssam cric.cri_alg = sav->tdb_compalgxform->type; 125105197Ssam 126181803Sbz return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support); 127105197Ssam} 128105197Ssam 129105197Ssam/* 130105197Ssam * ipcomp_zeroize() used when IPCA is deleted 131105197Ssam */ 132105197Ssamstatic int 133105197Ssamipcomp_zeroize(struct secasvar *sav) 134105197Ssam{ 135105197Ssam int err; 136105197Ssam 137105197Ssam err = crypto_freesession(sav->tdb_cryptoid); 138105197Ssam sav->tdb_cryptoid = 0; 139105197Ssam return err; 140105197Ssam} 141105197Ssam 142105197Ssam/* 143105197Ssam * ipcomp_input() gets called to uncompress an input packet 144105197Ssam */ 145105197Ssamstatic int 146105197Ssamipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 147105197Ssam{ 148105197Ssam struct tdb_crypto *tc; 149105197Ssam struct cryptodesc *crdc; 150105197Ssam struct cryptop *crp; 151220247Sbz struct ipcomp *ipcomp; 152220247Sbz caddr_t addr; 153105197Ssam int hlen = IPCOMP_HLENGTH; 154105197Ssam 155220247Sbz /* 156220247Sbz * Check that the next header of the IPComp is not IPComp again, before 157220247Sbz * doing any real work. Given it is not possible to do double 158220247Sbz * compression it means someone is playing tricks on us. 159220247Sbz */ 160220247Sbz if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) { 161252028Sae IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/ 162220247Sbz DPRINTF(("%s: m_pullup failed\n", __func__)); 163220247Sbz return (ENOBUFS); 164220247Sbz } 165220247Sbz addr = (caddr_t) mtod(m, struct ip *) + skip; 166220247Sbz ipcomp = (struct ipcomp *)addr; 167220247Sbz if (ipcomp->comp_nxt == IPPROTO_IPCOMP) { 168220247Sbz m_freem(m); 169252028Sae IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */ 170220247Sbz DPRINTF(("%s: recursive compression detected\n", __func__)); 171220247Sbz return (EINVAL); 172220247Sbz } 173220247Sbz 174105197Ssam /* Get crypto descriptors */ 175105197Ssam crp = crypto_getreq(1); 176105197Ssam if (crp == NULL) { 177105197Ssam m_freem(m); 178120585Ssam DPRINTF(("%s: no crypto descriptors\n", __func__)); 179252028Sae IPCOMPSTAT_INC(ipcomps_crypto); 180105197Ssam return ENOBUFS; 181105197Ssam } 182105197Ssam /* Get IPsec-specific opaque pointer */ 183105197Ssam tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO); 184105197Ssam if (tc == NULL) { 185105197Ssam m_freem(m); 186105197Ssam crypto_freereq(crp); 187120585Ssam DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__)); 188252028Sae IPCOMPSTAT_INC(ipcomps_crypto); 189105197Ssam return ENOBUFS; 190105197Ssam } 191105197Ssam crdc = crp->crp_desc; 192105197Ssam 193105197Ssam crdc->crd_skip = skip + hlen; 194105197Ssam crdc->crd_len = m->m_pkthdr.len - (skip + hlen); 195105197Ssam crdc->crd_inject = skip; 196105197Ssam 197105197Ssam tc->tc_ptr = 0; 198105197Ssam 199105197Ssam /* Decompression operation */ 200105197Ssam crdc->crd_alg = sav->tdb_compalgxform->type; 201105197Ssam 202105197Ssam /* Crypto operation descriptor */ 203105197Ssam crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); 204117058Ssam crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 205105197Ssam crp->crp_buf = (caddr_t) m; 206105197Ssam crp->crp_callback = ipcomp_input_cb; 207105197Ssam crp->crp_sid = sav->tdb_cryptoid; 208105197Ssam crp->crp_opaque = (caddr_t) tc; 209105197Ssam 210105197Ssam /* These are passed as-is to the callback */ 211105197Ssam tc->tc_spi = sav->spi; 212105197Ssam tc->tc_dst = sav->sah->saidx.dst; 213105197Ssam tc->tc_proto = sav->sah->saidx.proto; 214105197Ssam tc->tc_protoff = protoff; 215105197Ssam tc->tc_skip = skip; 216220206Sfabient KEY_ADDREFSA(sav); 217220206Sfabient tc->tc_sav = sav; 218105197Ssam 219105197Ssam return crypto_dispatch(crp); 220105197Ssam} 221105197Ssam 222105197Ssam/* 223105197Ssam * IPComp input callback from the crypto driver. 224105197Ssam */ 225105197Ssamstatic int 226105197Ssamipcomp_input_cb(struct cryptop *crp) 227105197Ssam{ 228105197Ssam struct cryptodesc *crd; 229105197Ssam struct tdb_crypto *tc; 230105197Ssam int skip, protoff; 231105197Ssam struct mtag *mtag; 232105197Ssam struct mbuf *m; 233105197Ssam struct secasvar *sav; 234105197Ssam struct secasindex *saidx; 235119643Ssam int hlen = IPCOMP_HLENGTH, error, clen; 236105197Ssam u_int8_t nproto; 237105197Ssam caddr_t addr; 238105197Ssam 239105197Ssam crd = crp->crp_desc; 240105197Ssam 241105197Ssam tc = (struct tdb_crypto *) crp->crp_opaque; 242120585Ssam IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!")); 243105197Ssam skip = tc->tc_skip; 244105197Ssam protoff = tc->tc_protoff; 245105197Ssam mtag = (struct mtag *) tc->tc_ptr; 246105197Ssam m = (struct mbuf *) crp->crp_buf; 247105197Ssam 248220206Sfabient sav = tc->tc_sav; 249220206Sfabient IPSEC_ASSERT(sav != NULL, ("null SA!")); 250105197Ssam 251105197Ssam saidx = &sav->sah->saidx; 252120585Ssam IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 253105197Ssam saidx->dst.sa.sa_family == AF_INET6, 254120585Ssam ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 255105197Ssam 256105197Ssam /* Check for crypto errors */ 257105197Ssam if (crp->crp_etype) { 258105197Ssam /* Reset the session ID */ 259105197Ssam if (sav->tdb_cryptoid != 0) 260105197Ssam sav->tdb_cryptoid = crp->crp_sid; 261105197Ssam 262105197Ssam if (crp->crp_etype == EAGAIN) { 263199905Sbz return crypto_dispatch(crp); 264105197Ssam } 265252028Sae IPCOMPSTAT_INC(ipcomps_noxform); 266120585Ssam DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 267105197Ssam error = crp->crp_etype; 268105197Ssam goto bad; 269105197Ssam } 270105197Ssam /* Shouldn't happen... */ 271105197Ssam if (m == NULL) { 272252028Sae IPCOMPSTAT_INC(ipcomps_crypto); 273120585Ssam DPRINTF(("%s: null mbuf returned from crypto\n", __func__)); 274105197Ssam error = EINVAL; 275105197Ssam goto bad; 276105197Ssam } 277252028Sae IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]); 278105197Ssam 279105197Ssam clen = crp->crp_olen; /* Length of data after processing */ 280105197Ssam 281105197Ssam /* Release the crypto descriptors */ 282105197Ssam free(tc, M_XDATA), tc = NULL; 283105197Ssam crypto_freereq(crp), crp = NULL; 284105197Ssam 285105197Ssam /* In case it's not done already, adjust the size of the mbuf chain */ 286105197Ssam m->m_pkthdr.len = clen + hlen + skip; 287105197Ssam 288105197Ssam if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { 289252028Sae IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/ 290120585Ssam DPRINTF(("%s: m_pullup failed\n", __func__)); 291105197Ssam error = EINVAL; /*XXX*/ 292105197Ssam goto bad; 293105197Ssam } 294105197Ssam 295105197Ssam /* Keep the next protocol field */ 296105197Ssam addr = (caddr_t) mtod(m, struct ip *) + skip; 297105197Ssam nproto = ((struct ipcomp *) addr)->comp_nxt; 298105197Ssam 299105197Ssam /* Remove the IPCOMP header */ 300105197Ssam error = m_striphdr(m, skip, hlen); 301105197Ssam if (error) { 302252028Sae IPCOMPSTAT_INC(ipcomps_hdrops); 303120585Ssam DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__, 304105197Ssam ipsec_address(&sav->sah->saidx.dst), 305105197Ssam (u_long) ntohl(sav->spi))); 306105197Ssam goto bad; 307105197Ssam } 308105197Ssam 309105197Ssam /* Restore the Next Protocol field */ 310105197Ssam m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); 311105197Ssam 312221129Sbz switch (saidx->dst.sa.sa_family) { 313221129Sbz#ifdef INET6 314221129Sbz case AF_INET6: 315221129Sbz error = ipsec6_common_input_cb(m, sav, skip, protoff, NULL); 316221129Sbz break; 317221129Sbz#endif 318221129Sbz#ifdef INET 319221129Sbz case AF_INET: 320221129Sbz error = ipsec4_common_input_cb(m, sav, skip, protoff, NULL); 321221129Sbz break; 322221129Sbz#endif 323221129Sbz default: 324221129Sbz panic("%s: Unexpected address family: %d saidx=%p", __func__, 325221129Sbz saidx->dst.sa.sa_family, saidx); 326221129Sbz } 327105197Ssam 328105197Ssam KEY_FREESAV(&sav); 329105197Ssam return error; 330105197Ssambad: 331105197Ssam if (sav) 332105197Ssam KEY_FREESAV(&sav); 333105197Ssam if (m) 334105197Ssam m_freem(m); 335105197Ssam if (tc != NULL) 336105197Ssam free(tc, M_XDATA); 337105197Ssam if (crp) 338105197Ssam crypto_freereq(crp); 339105197Ssam return error; 340105197Ssam} 341105197Ssam 342105197Ssam/* 343105197Ssam * IPComp output routine, called by ipsec[46]_process_packet() 344105197Ssam */ 345105197Ssamstatic int 346105197Ssamipcomp_output( 347105197Ssam struct mbuf *m, 348105197Ssam struct ipsecrequest *isr, 349105197Ssam struct mbuf **mp, 350105197Ssam int skip, 351105197Ssam int protoff 352105197Ssam) 353105197Ssam{ 354105197Ssam struct secasvar *sav; 355105197Ssam struct comp_algo *ipcompx; 356199899Sbz int error, ralen, maxpacketsize; 357105197Ssam struct cryptodesc *crdc; 358105197Ssam struct cryptop *crp; 359105197Ssam struct tdb_crypto *tc; 360105197Ssam 361105197Ssam sav = isr->sav; 362120585Ssam IPSEC_ASSERT(sav != NULL, ("null SA")); 363105197Ssam ipcompx = sav->tdb_compalgxform; 364120585Ssam IPSEC_ASSERT(ipcompx != NULL, ("null compression xform")); 365105197Ssam 366199896Sbz /* 367199896Sbz * Do not touch the packet in case our payload to compress 368199896Sbz * is lower than the minimal threshold of the compression 369199896Sbz * alogrithm. We will just send out the data uncompressed. 370199896Sbz * See RFC 3173, 2.2. Non-Expansion Policy. 371199896Sbz */ 372199896Sbz if (m->m_pkthdr.len <= ipcompx->minlen) { 373252028Sae IPCOMPSTAT_INC(ipcomps_threshold); 374199896Sbz return ipsec_process_done(m, isr); 375199896Sbz } 376199896Sbz 377105197Ssam ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ 378252028Sae IPCOMPSTAT_INC(ipcomps_output); 379105197Ssam 380105197Ssam /* Check for maximum packet size violations. */ 381105197Ssam switch (sav->sah->saidx.dst.sa.sa_family) { 382105197Ssam#ifdef INET 383105197Ssam case AF_INET: 384199897Sbz maxpacketsize = IP_MAXPACKET; 385105197Ssam break; 386105197Ssam#endif /* INET */ 387105197Ssam#ifdef INET6 388105197Ssam case AF_INET6: 389199897Sbz maxpacketsize = IPV6_MAXPACKET; 390105197Ssam break; 391105197Ssam#endif /* INET6 */ 392105197Ssam default: 393252028Sae IPCOMPSTAT_INC(ipcomps_nopf); 394120585Ssam DPRINTF(("%s: unknown/unsupported protocol family %d, " 395120585Ssam "IPCA %s/%08lx\n", __func__, 396105197Ssam sav->sah->saidx.dst.sa.sa_family, 397105197Ssam ipsec_address(&sav->sah->saidx.dst), 398105197Ssam (u_long) ntohl(sav->spi))); 399105197Ssam error = EPFNOSUPPORT; 400105197Ssam goto bad; 401105197Ssam } 402199899Sbz if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) { 403252028Sae IPCOMPSTAT_INC(ipcomps_toobig); 404120585Ssam DPRINTF(("%s: packet in IPCA %s/%08lx got too big " 405120585Ssam "(len %u, max len %u)\n", __func__, 406105197Ssam ipsec_address(&sav->sah->saidx.dst), 407105197Ssam (u_long) ntohl(sav->spi), 408199899Sbz ralen + skip + IPCOMP_HLENGTH, maxpacketsize)); 409105197Ssam error = EMSGSIZE; 410105197Ssam goto bad; 411105197Ssam } 412105197Ssam 413105197Ssam /* Update the counters */ 414252028Sae IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip); 415105197Ssam 416156756Ssam m = m_unshare(m, M_NOWAIT); 417105197Ssam if (m == NULL) { 418252028Sae IPCOMPSTAT_INC(ipcomps_hdrops); 419120585Ssam DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n", 420120585Ssam __func__, ipsec_address(&sav->sah->saidx.dst), 421105197Ssam (u_long) ntohl(sav->spi))); 422105197Ssam error = ENOBUFS; 423105197Ssam goto bad; 424105197Ssam } 425105197Ssam 426199899Sbz /* Ok now, we can pass to the crypto processing. */ 427105197Ssam 428105197Ssam /* Get crypto descriptors */ 429105197Ssam crp = crypto_getreq(1); 430105197Ssam if (crp == NULL) { 431252028Sae IPCOMPSTAT_INC(ipcomps_crypto); 432120585Ssam DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 433105197Ssam error = ENOBUFS; 434105197Ssam goto bad; 435105197Ssam } 436105197Ssam crdc = crp->crp_desc; 437105197Ssam 438105197Ssam /* Compression descriptor */ 439199899Sbz crdc->crd_skip = skip; 440199899Sbz crdc->crd_len = ralen; 441105197Ssam crdc->crd_flags = CRD_F_COMP; 442199899Sbz crdc->crd_inject = skip; 443105197Ssam 444105197Ssam /* Compression operation */ 445105197Ssam crdc->crd_alg = ipcompx->type; 446105197Ssam 447105197Ssam /* IPsec-specific opaque crypto info */ 448105197Ssam tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto), 449105197Ssam M_XDATA, M_NOWAIT|M_ZERO); 450105197Ssam if (tc == NULL) { 451252028Sae IPCOMPSTAT_INC(ipcomps_crypto); 452120585Ssam DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 453105197Ssam crypto_freereq(crp); 454105197Ssam error = ENOBUFS; 455105197Ssam goto bad; 456105197Ssam } 457105197Ssam 458105197Ssam tc->tc_isr = isr; 459220206Sfabient KEY_ADDREFSA(sav); 460220206Sfabient tc->tc_sav = sav; 461105197Ssam tc->tc_spi = sav->spi; 462105197Ssam tc->tc_dst = sav->sah->saidx.dst; 463105197Ssam tc->tc_proto = sav->sah->saidx.proto; 464199899Sbz tc->tc_protoff = protoff; 465199899Sbz tc->tc_skip = skip; 466105197Ssam 467105197Ssam /* Crypto operation descriptor */ 468105197Ssam crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ 469117058Ssam crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 470105197Ssam crp->crp_buf = (caddr_t) m; 471105197Ssam crp->crp_callback = ipcomp_output_cb; 472105197Ssam crp->crp_opaque = (caddr_t) tc; 473105197Ssam crp->crp_sid = sav->tdb_cryptoid; 474105197Ssam 475105197Ssam return crypto_dispatch(crp); 476105197Ssambad: 477105197Ssam if (m) 478105197Ssam m_freem(m); 479105197Ssam return (error); 480105197Ssam} 481105197Ssam 482105197Ssam/* 483105197Ssam * IPComp output callback from the crypto driver. 484105197Ssam */ 485105197Ssamstatic int 486105197Ssamipcomp_output_cb(struct cryptop *crp) 487105197Ssam{ 488105197Ssam struct tdb_crypto *tc; 489105197Ssam struct ipsecrequest *isr; 490105197Ssam struct secasvar *sav; 491105197Ssam struct mbuf *m; 492199899Sbz int error, skip; 493105197Ssam 494105197Ssam tc = (struct tdb_crypto *) crp->crp_opaque; 495120585Ssam IPSEC_ASSERT(tc != NULL, ("null opaque data area!")); 496105197Ssam m = (struct mbuf *) crp->crp_buf; 497105197Ssam skip = tc->tc_skip; 498105197Ssam 499105197Ssam isr = tc->tc_isr; 500120585Ssam IPSECREQUEST_LOCK(isr); 501220206Sfabient sav = tc->tc_sav; 502220206Sfabient /* With the isr lock released SA pointer can be updated. */ 503220206Sfabient if (sav != isr->sav) { 504252028Sae IPCOMPSTAT_INC(ipcomps_notdb); 505120585Ssam DPRINTF(("%s: SA expired while in crypto\n", __func__)); 506105197Ssam error = ENOBUFS; /*XXX*/ 507105197Ssam goto bad; 508105197Ssam } 509105197Ssam 510105197Ssam /* Check for crypto errors */ 511105197Ssam if (crp->crp_etype) { 512199905Sbz /* Reset the session ID */ 513105197Ssam if (sav->tdb_cryptoid != 0) 514105197Ssam sav->tdb_cryptoid = crp->crp_sid; 515105197Ssam 516105197Ssam if (crp->crp_etype == EAGAIN) { 517120585Ssam IPSECREQUEST_UNLOCK(isr); 518199899Sbz return crypto_dispatch(crp); 519105197Ssam } 520252028Sae IPCOMPSTAT_INC(ipcomps_noxform); 521120585Ssam DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 522105197Ssam error = crp->crp_etype; 523105197Ssam goto bad; 524105197Ssam } 525105197Ssam /* Shouldn't happen... */ 526105197Ssam if (m == NULL) { 527252028Sae IPCOMPSTAT_INC(ipcomps_crypto); 528120585Ssam DPRINTF(("%s: bogus return buffer from crypto\n", __func__)); 529105197Ssam error = EINVAL; 530105197Ssam goto bad; 531105197Ssam } 532252028Sae IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]); 533105197Ssam 534199899Sbz if (crp->crp_ilen - skip > crp->crp_olen) { 535199899Sbz struct mbuf *mo; 536199899Sbz struct ipcomp *ipcomp; 537199899Sbz int roff; 538199899Sbz uint8_t prot; 539199899Sbz 540199899Sbz /* Compression helped, inject IPCOMP header. */ 541199899Sbz mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff); 542199899Sbz if (mo == NULL) { 543252028Sae IPCOMPSTAT_INC(ipcomps_wrap); 544199899Sbz DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n", 545199899Sbz __func__, ipsec_address(&sav->sah->saidx.dst), 546199899Sbz (u_long) ntohl(sav->spi))); 547199899Sbz error = ENOBUFS; 548199899Sbz goto bad; 549199899Sbz } 550199899Sbz ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff); 551199899Sbz 552199899Sbz /* Initialize the IPCOMP header */ 553199899Sbz /* XXX alignment always correct? */ 554199899Sbz switch (sav->sah->saidx.dst.sa.sa_family) { 555199899Sbz#ifdef INET 556199899Sbz case AF_INET: 557199899Sbz ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; 558199899Sbz break; 559199899Sbz#endif /* INET */ 560199899Sbz#ifdef INET6 561199899Sbz case AF_INET6: 562199899Sbz ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; 563199899Sbz break; 564199899Sbz#endif 565199899Sbz } 566199899Sbz ipcomp->comp_flags = 0; 567199899Sbz ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi)); 568199899Sbz 569199899Sbz /* Fix Next Protocol in IPv4/IPv6 header */ 570199899Sbz prot = IPPROTO_IPCOMP; 571199899Sbz m_copyback(m, tc->tc_protoff, sizeof(u_int8_t), 572199899Sbz (u_char *)&prot); 573199899Sbz 574105197Ssam /* Adjust the length in the IP header */ 575105197Ssam switch (sav->sah->saidx.dst.sa.sa_family) { 576105197Ssam#ifdef INET 577105197Ssam case AF_INET: 578105197Ssam mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 579105197Ssam break; 580105197Ssam#endif /* INET */ 581105197Ssam#ifdef INET6 582105197Ssam case AF_INET6: 583105197Ssam mtod(m, struct ip6_hdr *)->ip6_plen = 584105197Ssam htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); 585105197Ssam break; 586105197Ssam#endif /* INET6 */ 587105197Ssam default: 588252028Sae IPCOMPSTAT_INC(ipcomps_nopf); 589120585Ssam DPRINTF(("%s: unknown/unsupported protocol " 590120585Ssam "family %d, IPCA %s/%08lx\n", __func__, 591105197Ssam sav->sah->saidx.dst.sa.sa_family, 592199897Sbz ipsec_address(&sav->sah->saidx.dst), 593105197Ssam (u_long) ntohl(sav->spi))); 594105197Ssam error = EPFNOSUPPORT; 595105197Ssam goto bad; 596105197Ssam } 597105197Ssam } else { 598199946Sbz /* Compression was useless, we have lost time. */ 599252028Sae IPCOMPSTAT_INC(ipcomps_uncompr); 600199946Sbz DPRINTF(("%s: compressions was useless %d - %d <= %d\n", 601199946Sbz __func__, crp->crp_ilen, skip, crp->crp_olen)); 602199899Sbz /* XXX remember state to not compress the next couple 603199899Sbz * of packets, RFC 3173, 2.2. Non-Expansion Policy */ 604105197Ssam } 605105197Ssam 606105197Ssam /* Release the crypto descriptor */ 607105197Ssam free(tc, M_XDATA); 608105197Ssam crypto_freereq(crp); 609105197Ssam 610105197Ssam /* NB: m is reclaimed by ipsec_process_done. */ 611105197Ssam error = ipsec_process_done(m, isr); 612105197Ssam KEY_FREESAV(&sav); 613120585Ssam IPSECREQUEST_UNLOCK(isr); 614105197Ssam return error; 615105197Ssambad: 616105197Ssam if (sav) 617105197Ssam KEY_FREESAV(&sav); 618120585Ssam IPSECREQUEST_UNLOCK(isr); 619105197Ssam if (m) 620105197Ssam m_freem(m); 621105197Ssam free(tc, M_XDATA); 622105197Ssam crypto_freereq(crp); 623105197Ssam return error; 624105197Ssam} 625105197Ssam 626105197Ssamstatic struct xformsw ipcomp_xformsw = { 627105197Ssam XF_IPCOMP, XFT_COMP, "IPcomp", 628105197Ssam ipcomp_init, ipcomp_zeroize, ipcomp_input, 629105197Ssam ipcomp_output 630105197Ssam}; 631105197Ssam 632105197Ssamstatic void 633105197Ssamipcomp_attach(void) 634105197Ssam{ 635185088Szec 636105197Ssam xform_register(&ipcomp_xformsw); 637105197Ssam} 638190787Szec 639125099SsamSYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL); 640