The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netipsec/xform_ipcomp.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: stable/8/sys/netipsec/xform_ipcomp.c 221525 2011-05-06 13:24:10Z fabient $    */
    2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
    3 
    4 /*-
    5  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  *
   11  * 1. Redistributions of source code must retain the above copyright
   12  *   notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *   notice, this list of conditions and the following disclaimer in the
   15  *   documentation and/or other materials provided with the distribution.
   16  * 3. The name of the author may not be used to endorse or promote products
   17  *   derived from this software without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 
   31 /* IP payload compression protocol (IPComp), see RFC 2393 */
   32 #include "opt_inet.h"
   33 #include "opt_inet6.h"
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 #include <sys/mbuf.h>
   38 #include <sys/lock.h>
   39 #include <sys/mutex.h>
   40 #include <sys/rwlock.h>
   41 #include <sys/socket.h>
   42 #include <sys/kernel.h>
   43 #include <sys/protosw.h>
   44 #include <sys/sysctl.h>
   45 
   46 #include <netinet/in.h>
   47 #include <netinet/in_systm.h>
   48 #include <netinet/ip.h>
   49 #include <netinet/ip_var.h>
   50 
   51 #include <net/route.h>
   52 #include <net/vnet.h>
   53 
   54 #include <netipsec/ipsec.h>
   55 #include <netipsec/xform.h>
   56 
   57 #ifdef INET6
   58 #include <netinet/ip6.h>
   59 #include <netipsec/ipsec6.h>
   60 #endif
   61 
   62 #include <netipsec/ipcomp.h>
   63 #include <netipsec/ipcomp_var.h>
   64 
   65 #include <netipsec/key.h>
   66 #include <netipsec/key_debug.h>
   67 
   68 #include <opencrypto/cryptodev.h>
   69 #include <opencrypto/deflate.h>
   70 #include <opencrypto/xform.h>
   71 
   72 VNET_DEFINE(int, ipcomp_enable) = 1;
   73 VNET_DEFINE(struct ipcompstat, ipcompstat);
   74 
   75 SYSCTL_DECL(_net_inet_ipcomp);
   76 SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
   77         ipcomp_enable,  CTLFLAG_RW,     &VNET_NAME(ipcomp_enable),      0, "");
   78 SYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
   79         stats,          CTLFLAG_RD,     &VNET_NAME(ipcompstat), ipcompstat, "");
   80 
   81 static int ipcomp_input_cb(struct cryptop *crp);
   82 static int ipcomp_output_cb(struct cryptop *crp);
   83 
   84 struct comp_algo *
   85 ipcomp_algorithm_lookup(int alg)
   86 {
   87         if (alg >= IPCOMP_ALG_MAX)
   88                 return NULL;
   89         switch (alg) {
   90         case SADB_X_CALG_DEFLATE:
   91                 return &comp_algo_deflate;
   92         }
   93         return NULL;
   94 }
   95 
   96 /*
   97  * ipcomp_init() is called when an CPI is being set up.
   98  */
   99 static int
  100 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
  101 {
  102         struct comp_algo *tcomp;
  103         struct cryptoini cric;
  104 
  105         /* NB: algorithm really comes in alg_enc and not alg_comp! */
  106         tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
  107         if (tcomp == NULL) {
  108                 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
  109                          sav->alg_comp));
  110                 return EINVAL;
  111         }
  112         sav->alg_comp = sav->alg_enc;           /* set for doing histogram */
  113         sav->tdb_xform = xsp;
  114         sav->tdb_compalgxform = tcomp;
  115 
  116         /* Initialize crypto session */
  117         bzero(&cric, sizeof (cric));
  118         cric.cri_alg = sav->tdb_compalgxform->type;
  119 
  120         return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
  121 }
  122 
  123 /*
  124  * ipcomp_zeroize() used when IPCA is deleted
  125  */
  126 static int
  127 ipcomp_zeroize(struct secasvar *sav)
  128 {
  129         int err;
  130 
  131         err = crypto_freesession(sav->tdb_cryptoid);
  132         sav->tdb_cryptoid = 0;
  133         return err;
  134 }
  135 
  136 /*
  137  * ipcomp_input() gets called to uncompress an input packet
  138  */
  139 static int
  140 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
  141 {
  142         struct tdb_crypto *tc;
  143         struct cryptodesc *crdc;
  144         struct cryptop *crp;
  145         struct ipcomp *ipcomp;
  146         caddr_t addr;
  147         int hlen = IPCOMP_HLENGTH;
  148 
  149         /*
  150          * Check that the next header of the IPComp is not IPComp again, before
  151          * doing any real work.  Given it is not possible to do double
  152          * compression it means someone is playing tricks on us.
  153          */
  154         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
  155                 V_ipcompstat.ipcomps_hdrops++;          /*XXX*/
  156                 DPRINTF(("%s: m_pullup failed\n", __func__));
  157                 return (ENOBUFS);
  158         }
  159         addr = (caddr_t) mtod(m, struct ip *) + skip;
  160         ipcomp = (struct ipcomp *)addr;
  161         if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
  162                 m_freem(m);
  163                 V_ipcompstat.ipcomps_pdrops++;  /* XXX have our own stats? */
  164                 DPRINTF(("%s: recursive compression detected\n", __func__));
  165                 return (EINVAL);
  166         }
  167 
  168         /* Get crypto descriptors */
  169         crp = crypto_getreq(1);
  170         if (crp == NULL) {
  171                 m_freem(m);
  172                 DPRINTF(("%s: no crypto descriptors\n", __func__));
  173                 V_ipcompstat.ipcomps_crypto++;
  174                 return ENOBUFS;
  175         }
  176         /* Get IPsec-specific opaque pointer */
  177         tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
  178         if (tc == NULL) {
  179                 m_freem(m);
  180                 crypto_freereq(crp);
  181                 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
  182                 V_ipcompstat.ipcomps_crypto++;
  183                 return ENOBUFS;
  184         }
  185         crdc = crp->crp_desc;
  186 
  187         crdc->crd_skip = skip + hlen;
  188         crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
  189         crdc->crd_inject = skip;
  190 
  191         tc->tc_ptr = 0;
  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_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  199         crp->crp_buf = (caddr_t) m;
  200         crp->crp_callback = ipcomp_input_cb;
  201         crp->crp_sid = sav->tdb_cryptoid;
  202         crp->crp_opaque = (caddr_t) tc;
  203 
  204         /* These are passed as-is to the callback */
  205         tc->tc_spi = sav->spi;
  206         tc->tc_dst = sav->sah->saidx.dst;
  207         tc->tc_proto = sav->sah->saidx.proto;
  208         tc->tc_protoff = protoff;
  209         tc->tc_skip = skip;
  210         KEY_ADDREFSA(sav);
  211         tc->tc_sav = sav;
  212 
  213         return crypto_dispatch(crp);
  214 }
  215 
  216 #ifdef INET6
  217 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {              \
  218         if (saidx->dst.sa.sa_family == AF_INET6) {                           \
  219                 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
  220         } else {                                                             \
  221                 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
  222         }                                                                    \
  223 } while (0)
  224 #else
  225 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)                   \
  226         (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
  227 #endif
  228 
  229 /*
  230  * IPComp input callback from the crypto driver.
  231  */
  232 static int
  233 ipcomp_input_cb(struct cryptop *crp)
  234 {
  235         struct cryptodesc *crd;
  236         struct tdb_crypto *tc;
  237         int skip, protoff;
  238         struct mtag *mtag;
  239         struct mbuf *m;
  240         struct secasvar *sav;
  241         struct secasindex *saidx;
  242         int hlen = IPCOMP_HLENGTH, error, clen;
  243         u_int8_t nproto;
  244         caddr_t addr;
  245 
  246         crd = crp->crp_desc;
  247 
  248         tc = (struct tdb_crypto *) crp->crp_opaque;
  249         IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
  250         skip = tc->tc_skip;
  251         protoff = tc->tc_protoff;
  252         mtag = (struct mtag *) tc->tc_ptr;
  253         m = (struct mbuf *) crp->crp_buf;
  254 
  255         sav = tc->tc_sav;
  256         IPSEC_ASSERT(sav != NULL, ("null SA!"));
  257 
  258         saidx = &sav->sah->saidx;
  259         IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
  260                 saidx->dst.sa.sa_family == AF_INET6,
  261                 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
  262 
  263         /* Check for crypto errors */
  264         if (crp->crp_etype) {
  265                 /* Reset the session ID */
  266                 if (sav->tdb_cryptoid != 0)
  267                         sav->tdb_cryptoid = crp->crp_sid;
  268 
  269                 if (crp->crp_etype == EAGAIN) {
  270                         return crypto_dispatch(crp);
  271                 }
  272                 V_ipcompstat.ipcomps_noxform++;
  273                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  274                 error = crp->crp_etype;
  275                 goto bad;
  276         }
  277         /* Shouldn't happen... */
  278         if (m == NULL) {
  279                 V_ipcompstat.ipcomps_crypto++;
  280                 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
  281                 error = EINVAL;
  282                 goto bad;
  283         }
  284         V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
  285 
  286         clen = crp->crp_olen;           /* Length of data after processing */
  287 
  288         /* Release the crypto descriptors */
  289         free(tc, M_XDATA), tc = NULL;
  290         crypto_freereq(crp), crp = NULL;
  291 
  292         /* In case it's not done already, adjust the size of the mbuf chain */
  293         m->m_pkthdr.len = clen + hlen + skip;
  294 
  295         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
  296                 V_ipcompstat.ipcomps_hdrops++;          /*XXX*/
  297                 DPRINTF(("%s: m_pullup failed\n", __func__));
  298                 error = EINVAL;                         /*XXX*/
  299                 goto bad;
  300         }
  301 
  302         /* Keep the next protocol field */
  303         addr = (caddr_t) mtod(m, struct ip *) + skip;
  304         nproto = ((struct ipcomp *) addr)->comp_nxt;
  305 
  306         /* Remove the IPCOMP header */
  307         error = m_striphdr(m, skip, hlen);
  308         if (error) {
  309                 V_ipcompstat.ipcomps_hdrops++;
  310                 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
  311                          ipsec_address(&sav->sah->saidx.dst),
  312                          (u_long) ntohl(sav->spi)));
  313                 goto bad;
  314         }
  315 
  316         /* Restore the Next Protocol field */
  317         m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
  318 
  319         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
  320 
  321         KEY_FREESAV(&sav);
  322         return error;
  323 bad:
  324         if (sav)
  325                 KEY_FREESAV(&sav);
  326         if (m)
  327                 m_freem(m);
  328         if (tc != NULL)
  329                 free(tc, M_XDATA);
  330         if (crp)
  331                 crypto_freereq(crp);
  332         return error;
  333 }
  334 
  335 /*
  336  * IPComp output routine, called by ipsec[46]_process_packet()
  337  */
  338 static int
  339 ipcomp_output(
  340         struct mbuf *m,
  341         struct ipsecrequest *isr,
  342         struct mbuf **mp,
  343         int skip,
  344         int protoff
  345 )
  346 {
  347         struct secasvar *sav;
  348         struct comp_algo *ipcompx;
  349         int error, ralen, maxpacketsize;
  350         struct cryptodesc *crdc;
  351         struct cryptop *crp;
  352         struct tdb_crypto *tc;
  353 
  354         sav = isr->sav;
  355         IPSEC_ASSERT(sav != NULL, ("null SA"));
  356         ipcompx = sav->tdb_compalgxform;
  357         IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
  358 
  359         /*
  360          * Do not touch the packet in case our payload to compress
  361          * is lower than the minimal threshold of the compression
  362          * alogrithm.  We will just send out the data uncompressed.
  363          * See RFC 3173, 2.2. Non-Expansion Policy.
  364          */
  365         if (m->m_pkthdr.len <= ipcompx->minlen) {
  366                 V_ipcompstat.ipcomps_threshold++;
  367                 return ipsec_process_done(m, isr);
  368         }
  369 
  370         ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
  371         V_ipcompstat.ipcomps_output++;
  372 
  373         /* Check for maximum packet size violations. */
  374         switch (sav->sah->saidx.dst.sa.sa_family) {
  375 #ifdef INET
  376         case AF_INET:
  377                 maxpacketsize = IP_MAXPACKET;
  378                 break;
  379 #endif /* INET */
  380 #ifdef INET6
  381         case AF_INET6:
  382                 maxpacketsize = IPV6_MAXPACKET;
  383                 break;
  384 #endif /* INET6 */
  385         default:
  386                 V_ipcompstat.ipcomps_nopf++;
  387                 DPRINTF(("%s: unknown/unsupported protocol family %d, "
  388                     "IPCA %s/%08lx\n", __func__,
  389                     sav->sah->saidx.dst.sa.sa_family,
  390                     ipsec_address(&sav->sah->saidx.dst),
  391                     (u_long) ntohl(sav->spi)));
  392                 error = EPFNOSUPPORT;
  393                 goto bad;
  394         }
  395         if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
  396                 V_ipcompstat.ipcomps_toobig++;
  397                 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
  398                     "(len %u, max len %u)\n", __func__,
  399                     ipsec_address(&sav->sah->saidx.dst),
  400                     (u_long) ntohl(sav->spi),
  401                     ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
  402                 error = EMSGSIZE;
  403                 goto bad;
  404         }
  405 
  406         /* Update the counters */
  407         V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
  408 
  409         m = m_unshare(m, M_NOWAIT);
  410         if (m == NULL) {
  411                 V_ipcompstat.ipcomps_hdrops++;
  412                 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
  413                     __func__, ipsec_address(&sav->sah->saidx.dst),
  414                     (u_long) ntohl(sav->spi)));
  415                 error = ENOBUFS;
  416                 goto bad;
  417         }
  418 
  419         /* Ok now, we can pass to the crypto processing. */
  420 
  421         /* Get crypto descriptors */
  422         crp = crypto_getreq(1);
  423         if (crp == NULL) {
  424                 V_ipcompstat.ipcomps_crypto++;
  425                 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
  426                 error = ENOBUFS;
  427                 goto bad;
  428         }
  429         crdc = crp->crp_desc;
  430 
  431         /* Compression descriptor */
  432         crdc->crd_skip = skip;
  433         crdc->crd_len = ralen;
  434         crdc->crd_flags = CRD_F_COMP;
  435         crdc->crd_inject = skip;
  436 
  437         /* Compression operation */
  438         crdc->crd_alg = ipcompx->type;
  439 
  440         /* IPsec-specific opaque crypto info */
  441         tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
  442                 M_XDATA, M_NOWAIT|M_ZERO);
  443         if (tc == NULL) {
  444                 V_ipcompstat.ipcomps_crypto++;
  445                 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
  446                 crypto_freereq(crp);
  447                 error = ENOBUFS;
  448                 goto bad;
  449         }
  450 
  451         tc->tc_isr = isr;
  452         KEY_ADDREFSA(sav);
  453         tc->tc_sav = sav;
  454         tc->tc_spi = sav->spi;
  455         tc->tc_dst = sav->sah->saidx.dst;
  456         tc->tc_proto = sav->sah->saidx.proto;
  457         tc->tc_protoff = protoff;
  458         tc->tc_skip = skip;
  459 
  460         /* Crypto operation descriptor */
  461         crp->crp_ilen = m->m_pkthdr.len;        /* Total input length */
  462         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  463         crp->crp_buf = (caddr_t) m;
  464         crp->crp_callback = ipcomp_output_cb;
  465         crp->crp_opaque = (caddr_t) tc;
  466         crp->crp_sid = sav->tdb_cryptoid;
  467 
  468         return crypto_dispatch(crp);
  469 bad:
  470         if (m)
  471                 m_freem(m);
  472         return (error);
  473 }
  474 
  475 /*
  476  * IPComp output callback from the crypto driver.
  477  */
  478 static int
  479 ipcomp_output_cb(struct cryptop *crp)
  480 {
  481         struct tdb_crypto *tc;
  482         struct ipsecrequest *isr;
  483         struct secasvar *sav;
  484         struct mbuf *m;
  485         int error, skip;
  486 
  487         tc = (struct tdb_crypto *) crp->crp_opaque;
  488         IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
  489         m = (struct mbuf *) crp->crp_buf;
  490         skip = tc->tc_skip;
  491 
  492         isr = tc->tc_isr;
  493         IPSECREQUEST_LOCK(isr);
  494         sav = tc->tc_sav;
  495         /* With the isr lock released SA pointer can be updated. */
  496         if (sav != isr->sav) {
  497                 V_ipcompstat.ipcomps_notdb++;
  498                 DPRINTF(("%s: SA expired while in crypto\n", __func__));
  499                 error = ENOBUFS;                /*XXX*/
  500                 goto bad;
  501         }
  502 
  503         /* Check for crypto errors */
  504         if (crp->crp_etype) {
  505                 /* Reset the session ID */
  506                 if (sav->tdb_cryptoid != 0)
  507                         sav->tdb_cryptoid = crp->crp_sid;
  508 
  509                 if (crp->crp_etype == EAGAIN) {
  510                         IPSECREQUEST_UNLOCK(isr);
  511                         return crypto_dispatch(crp);
  512                 }
  513                 V_ipcompstat.ipcomps_noxform++;
  514                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  515                 error = crp->crp_etype;
  516                 goto bad;
  517         }
  518         /* Shouldn't happen... */
  519         if (m == NULL) {
  520                 V_ipcompstat.ipcomps_crypto++;
  521                 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
  522                 error = EINVAL;
  523                 goto bad;
  524         }
  525         V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
  526 
  527         if (crp->crp_ilen - skip > crp->crp_olen) {
  528                 struct mbuf *mo;
  529                 struct ipcomp *ipcomp;
  530                 int roff;
  531                 uint8_t prot;
  532 
  533                 /* Compression helped, inject IPCOMP header. */
  534                 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
  535                 if (mo == NULL) {
  536                         V_ipcompstat.ipcomps_wrap++;
  537                         DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
  538                             __func__, ipsec_address(&sav->sah->saidx.dst),
  539                             (u_long) ntohl(sav->spi)));
  540                         error = ENOBUFS;
  541                         goto bad;
  542                 }
  543                 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
  544 
  545                 /* Initialize the IPCOMP header */
  546                 /* XXX alignment always correct? */
  547                 switch (sav->sah->saidx.dst.sa.sa_family) {
  548 #ifdef INET
  549                 case AF_INET:
  550                         ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
  551                         break;
  552 #endif /* INET */
  553 #ifdef INET6
  554                 case AF_INET6:
  555                         ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
  556                         break;
  557 #endif
  558                 }
  559                 ipcomp->comp_flags = 0;
  560                 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
  561 
  562                 /* Fix Next Protocol in IPv4/IPv6 header */
  563                 prot = IPPROTO_IPCOMP;
  564                 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
  565                     (u_char *)&prot);
  566 
  567                 /* Adjust the length in the IP header */
  568                 switch (sav->sah->saidx.dst.sa.sa_family) {
  569 #ifdef INET
  570                 case AF_INET:
  571                         mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
  572                         break;
  573 #endif /* INET */
  574 #ifdef INET6
  575                 case AF_INET6:
  576                         mtod(m, struct ip6_hdr *)->ip6_plen =
  577                                 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
  578                         break;
  579 #endif /* INET6 */
  580                 default:
  581                         V_ipcompstat.ipcomps_nopf++;
  582                         DPRINTF(("%s: unknown/unsupported protocol "
  583                             "family %d, IPCA %s/%08lx\n", __func__,
  584                             sav->sah->saidx.dst.sa.sa_family,
  585                             ipsec_address(&sav->sah->saidx.dst),
  586                             (u_long) ntohl(sav->spi)));
  587                         error = EPFNOSUPPORT;
  588                         goto bad;
  589                 }
  590         } else {
  591                 /* Compression was useless, we have lost time. */
  592                 V_ipcompstat.ipcomps_uncompr++;
  593                 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
  594                     __func__, crp->crp_ilen, skip, crp->crp_olen));
  595                 /* XXX remember state to not compress the next couple
  596                  *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
  597         }
  598 
  599         /* Release the crypto descriptor */
  600         free(tc, M_XDATA);
  601         crypto_freereq(crp);
  602 
  603         /* NB: m is reclaimed by ipsec_process_done. */
  604         error = ipsec_process_done(m, isr);
  605         KEY_FREESAV(&sav);
  606         IPSECREQUEST_UNLOCK(isr);
  607         return error;
  608 bad:
  609         if (sav)
  610                 KEY_FREESAV(&sav);
  611         IPSECREQUEST_UNLOCK(isr);
  612         if (m)
  613                 m_freem(m);
  614         free(tc, M_XDATA);
  615         crypto_freereq(crp);
  616         return error;
  617 }
  618 
  619 static struct xformsw ipcomp_xformsw = {
  620         XF_IPCOMP,              XFT_COMP,               "IPcomp",
  621         ipcomp_init,            ipcomp_zeroize,         ipcomp_input,
  622         ipcomp_output
  623 };
  624 
  625 static void
  626 ipcomp_attach(void)
  627 {
  628 
  629         xform_register(&ipcomp_xformsw);
  630 }
  631 
  632 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
  633 
  634 static void
  635 vnet_ipcomp_attach(const void *unused __unused)
  636 {
  637 
  638         V_ipcompstat.version = IPCOMPSTAT_VERSION;
  639 }
  640 
  641 VNET_SYSINIT(vnet_ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
  642     vnet_ipcomp_attach, NULL);

Cache object: c07496031bdaedabfa1676927333c109


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.