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

Cache object: c598ed004633258c12e8ebca71c33821


[ 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.