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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD$       */
    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         crd = crp->crp_desc;
  244 
  245         tc = (struct tdb_crypto *) crp->crp_opaque;
  246         IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
  247         skip = tc->tc_skip;
  248         protoff = tc->tc_protoff;
  249         mtag = (struct mtag *) tc->tc_ptr;
  250         m = (struct mbuf *) crp->crp_buf;
  251 
  252         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
  253         if (sav == NULL) {
  254                 ipcompstat.ipcomps_notdb++;
  255                 DPRINTF(("%s: SA expired while in crypto\n", __func__));
  256                 error = ENOBUFS;                /*XXX*/
  257                 goto bad;
  258         }
  259 
  260         saidx = &sav->sah->saidx;
  261         IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
  262                 saidx->dst.sa.sa_family == AF_INET6,
  263                 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
  264 
  265         /* Check for crypto errors */
  266         if (crp->crp_etype) {
  267                 /* Reset the session ID */
  268                 if (sav->tdb_cryptoid != 0)
  269                         sav->tdb_cryptoid = crp->crp_sid;
  270 
  271                 if (crp->crp_etype == EAGAIN) {
  272                         KEY_FREESAV(&sav);
  273                         return crypto_dispatch(crp);
  274                 }
  275                 ipcompstat.ipcomps_noxform++;
  276                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  277                 error = crp->crp_etype;
  278                 goto bad;
  279         }
  280         /* Shouldn't happen... */
  281         if (m == NULL) {
  282                 ipcompstat.ipcomps_crypto++;
  283                 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
  284                 error = EINVAL;
  285                 goto bad;
  286         }
  287         ipcompstat.ipcomps_hist[sav->alg_comp]++;
  288 
  289         clen = crp->crp_olen;           /* Length of data after processing */
  290 
  291         /* Release the crypto descriptors */
  292         free(tc, M_XDATA), tc = NULL;
  293         crypto_freereq(crp), crp = NULL;
  294 
  295         /* In case it's not done already, adjust the size of the mbuf chain */
  296         m->m_pkthdr.len = clen + hlen + skip;
  297 
  298         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
  299                 ipcompstat.ipcomps_hdrops++;            /*XXX*/
  300                 DPRINTF(("%s: m_pullup failed\n", __func__));
  301                 error = EINVAL;                         /*XXX*/
  302                 goto bad;
  303         }
  304 
  305         /* Keep the next protocol field */
  306         addr = (caddr_t) mtod(m, struct ip *) + skip;
  307         nproto = ((struct ipcomp *) addr)->comp_nxt;
  308 
  309         /* Remove the IPCOMP header */
  310         error = m_striphdr(m, skip, hlen);
  311         if (error) {
  312                 ipcompstat.ipcomps_hdrops++;
  313                 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
  314                          ipsec_address(&sav->sah->saidx.dst),
  315                          (u_long) ntohl(sav->spi)));
  316                 goto bad;
  317         }
  318 
  319         /* Restore the Next Protocol field */
  320         m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
  321 
  322         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
  323 
  324         KEY_FREESAV(&sav);
  325         return error;
  326 bad:
  327         if (sav)
  328                 KEY_FREESAV(&sav);
  329         if (m)
  330                 m_freem(m);
  331         if (tc != NULL)
  332                 free(tc, M_XDATA);
  333         if (crp)
  334                 crypto_freereq(crp);
  335         return error;
  336 }
  337 
  338 /*
  339  * IPComp output routine, called by ipsec[46]_process_packet()
  340  */
  341 static int
  342 ipcomp_output(
  343         struct mbuf *m,
  344         struct ipsecrequest *isr,
  345         struct mbuf **mp,
  346         int skip,
  347         int protoff
  348 )
  349 {
  350         struct secasvar *sav;
  351         struct comp_algo *ipcompx;
  352         int error, ralen, maxpacketsize;
  353         struct cryptodesc *crdc;
  354         struct cryptop *crp;
  355         struct tdb_crypto *tc;
  356 
  357         IPSEC_SPLASSERT_SOFTNET(__func__);
  358 
  359         sav = isr->sav;
  360         IPSEC_ASSERT(sav != NULL, ("null SA"));
  361         ipcompx = sav->tdb_compalgxform;
  362         IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
  363 
  364         /*
  365          * Do not touch the packet in case our payload to compress
  366          * is lower than the minimal threshold of the compression
  367          * alogrithm.  We will just send out the data uncompressed.
  368          * See RFC 3173, 2.2. Non-Expansion Policy.
  369          */
  370         if (m->m_pkthdr.len <= ipcompx->minlen) {
  371                 ipcompstat.ipcomps_threshold++;
  372                 return ipsec_process_done(m, isr);
  373         }
  374 
  375         ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
  376         ipcompstat.ipcomps_output++;
  377 
  378         /* Check for maximum packet size violations. */
  379         switch (sav->sah->saidx.dst.sa.sa_family) {
  380 #ifdef INET
  381         case AF_INET:
  382                 maxpacketsize = IP_MAXPACKET;
  383                 break;
  384 #endif /* INET */
  385 #ifdef INET6
  386         case AF_INET6:
  387                 maxpacketsize = IPV6_MAXPACKET;
  388                 break;
  389 #endif /* INET6 */
  390         default:
  391                 ipcompstat.ipcomps_nopf++;
  392                 DPRINTF(("%s: unknown/unsupported protocol family %d, "
  393                     "IPCA %s/%08lx\n", __func__,
  394                     sav->sah->saidx.dst.sa.sa_family,
  395                     ipsec_address(&sav->sah->saidx.dst),
  396                     (u_long) ntohl(sav->spi)));
  397                 error = EPFNOSUPPORT;
  398                 goto bad;
  399         }
  400         if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
  401                 ipcompstat.ipcomps_toobig++;
  402                 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
  403                     "(len %u, max len %u)\n", __func__,
  404                     ipsec_address(&sav->sah->saidx.dst),
  405                     (u_long) ntohl(sav->spi),
  406                     ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
  407                 error = EMSGSIZE;
  408                 goto bad;
  409         }
  410 
  411         /* Update the counters */
  412         ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
  413 
  414         m = m_unshare(m, M_NOWAIT);
  415         if (m == NULL) {
  416                 ipcompstat.ipcomps_hdrops++;
  417                 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
  418                     __func__, ipsec_address(&sav->sah->saidx.dst),
  419                     (u_long) ntohl(sav->spi)));
  420                 error = ENOBUFS;
  421                 goto bad;
  422         }
  423 
  424         /* Ok now, we can pass to the crypto processing. */
  425 
  426         /* Get crypto descriptors */
  427         crp = crypto_getreq(1);
  428         if (crp == NULL) {
  429                 ipcompstat.ipcomps_crypto++;
  430                 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
  431                 error = ENOBUFS;
  432                 goto bad;
  433         }
  434         crdc = crp->crp_desc;
  435 
  436         /* Compression descriptor */
  437         crdc->crd_skip = skip;
  438         crdc->crd_len = ralen;
  439         crdc->crd_flags = CRD_F_COMP;
  440         crdc->crd_inject = skip;
  441 
  442         /* Compression operation */
  443         crdc->crd_alg = ipcompx->type;
  444 
  445         /* IPsec-specific opaque crypto info */
  446         tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
  447                 M_XDATA, M_NOWAIT|M_ZERO);
  448         if (tc == NULL) {
  449                 ipcompstat.ipcomps_crypto++;
  450                 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
  451                 crypto_freereq(crp);
  452                 error = ENOBUFS;
  453                 goto bad;
  454         }
  455 
  456         tc->tc_isr = isr;
  457         tc->tc_spi = sav->spi;
  458         tc->tc_dst = sav->sah->saidx.dst;
  459         tc->tc_proto = sav->sah->saidx.proto;
  460         tc->tc_protoff = protoff;
  461         tc->tc_skip = skip;
  462 
  463         /* Crypto operation descriptor */
  464         crp->crp_ilen = m->m_pkthdr.len;        /* Total input length */
  465         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  466         crp->crp_buf = (caddr_t) m;
  467         crp->crp_callback = ipcomp_output_cb;
  468         crp->crp_opaque = (caddr_t) tc;
  469         crp->crp_sid = sav->tdb_cryptoid;
  470 
  471         return crypto_dispatch(crp);
  472 bad:
  473         if (m)
  474                 m_freem(m);
  475         return (error);
  476 }
  477 
  478 /*
  479  * IPComp output callback from the crypto driver.
  480  */
  481 static int
  482 ipcomp_output_cb(struct cryptop *crp)
  483 {
  484         struct tdb_crypto *tc;
  485         struct ipsecrequest *isr;
  486         struct secasvar *sav;
  487         struct mbuf *m;
  488         int error, skip;
  489 
  490         tc = (struct tdb_crypto *) crp->crp_opaque;
  491         IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
  492         m = (struct mbuf *) crp->crp_buf;
  493         skip = tc->tc_skip;
  494 
  495         isr = tc->tc_isr;
  496         IPSECREQUEST_LOCK(isr);
  497         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
  498         if (sav == NULL) {
  499                 ipcompstat.ipcomps_notdb++;
  500                 DPRINTF(("%s: SA expired while in crypto\n", __func__));
  501                 error = ENOBUFS;                /*XXX*/
  502                 goto bad;
  503         }
  504         IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
  505 
  506         /* Check for crypto errors */
  507         if (crp->crp_etype) {
  508                 /* Reset the session ID */
  509                 if (sav->tdb_cryptoid != 0)
  510                         sav->tdb_cryptoid = crp->crp_sid;
  511 
  512                 if (crp->crp_etype == EAGAIN) {
  513                         KEY_FREESAV(&sav);
  514                         IPSECREQUEST_UNLOCK(isr);
  515                         return crypto_dispatch(crp);
  516                 }
  517                 ipcompstat.ipcomps_noxform++;
  518                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  519                 error = crp->crp_etype;
  520                 goto bad;
  521         }
  522         /* Shouldn't happen... */
  523         if (m == NULL) {
  524                 ipcompstat.ipcomps_crypto++;
  525                 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
  526                 error = EINVAL;
  527                 goto bad;
  528         }
  529         ipcompstat.ipcomps_hist[sav->alg_comp]++;
  530 
  531         if (crp->crp_ilen - skip > crp->crp_olen) {
  532                 struct mbuf *mo;
  533                 struct ipcomp *ipcomp;
  534                 int roff;
  535                 uint8_t prot;
  536 
  537                 /* Compression helped, inject IPCOMP header. */
  538                 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
  539                 if (mo == NULL) {
  540                         ipcompstat.ipcomps_wrap++;
  541                         DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
  542                             __func__, ipsec_address(&sav->sah->saidx.dst),
  543                             (u_long) ntohl(sav->spi)));
  544                         error = ENOBUFS;
  545                         goto bad;
  546                 }
  547                 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
  548 
  549                 /* Initialize the IPCOMP header */
  550                 /* XXX alignment always correct? */
  551                 switch (sav->sah->saidx.dst.sa.sa_family) {
  552 #ifdef INET
  553                 case AF_INET:
  554                         ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
  555                         break;
  556 #endif /* INET */
  557 #ifdef INET6
  558                 case AF_INET6:
  559                         ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
  560                         break;
  561 #endif
  562                 }
  563                 ipcomp->comp_flags = 0;
  564                 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
  565 
  566                 /* Fix Next Protocol in IPv4/IPv6 header */
  567                 prot = IPPROTO_IPCOMP;
  568                 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
  569                     (u_char *)&prot);
  570 
  571                 /* Adjust the length in the IP header */
  572                 switch (sav->sah->saidx.dst.sa.sa_family) {
  573 #ifdef INET
  574                 case AF_INET:
  575                         mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
  576                         break;
  577 #endif /* INET */
  578 #ifdef INET6
  579                 case AF_INET6:
  580                         mtod(m, struct ip6_hdr *)->ip6_plen =
  581                                 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
  582                         break;
  583 #endif /* INET6 */
  584                 default:
  585                         ipcompstat.ipcomps_nopf++;
  586                         DPRINTF(("%s: unknown/unsupported protocol "
  587                             "family %d, IPCA %s/%08lx\n", __func__,
  588                             sav->sah->saidx.dst.sa.sa_family,
  589                             ipsec_address(&sav->sah->saidx.dst),
  590                             (u_long) ntohl(sav->spi)));
  591                         error = EPFNOSUPPORT;
  592                         goto bad;
  593                 }
  594         } else {
  595                 /* Compression was useless, we have lost time. */
  596                 ipcompstat.ipcomps_uncompr++;
  597                 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
  598                     __func__, crp->crp_ilen, skip, crp->crp_olen));
  599                 /* XXX remember state to not compress the next couple
  600                  *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
  601         }
  602 
  603         /* Release the crypto descriptor */
  604         free(tc, M_XDATA);
  605         crypto_freereq(crp);
  606 
  607         /* NB: m is reclaimed by ipsec_process_done. */
  608         error = ipsec_process_done(m, isr);
  609         KEY_FREESAV(&sav);
  610         IPSECREQUEST_UNLOCK(isr);
  611         return error;
  612 bad:
  613         if (sav)
  614                 KEY_FREESAV(&sav);
  615         IPSECREQUEST_UNLOCK(isr);
  616         if (m)
  617                 m_freem(m);
  618         free(tc, M_XDATA);
  619         crypto_freereq(crp);
  620         return error;
  621 }
  622 
  623 static struct xformsw ipcomp_xformsw = {
  624         XF_IPCOMP,              XFT_COMP,               "IPcomp",
  625         ipcomp_init,            ipcomp_zeroize,         ipcomp_input,
  626         ipcomp_output
  627 };
  628 
  629 static void
  630 ipcomp_attach(void)
  631 {
  632 
  633         ipcompstat.version = IPCOMPSTAT_VERSION;
  634         xform_register(&ipcomp_xformsw);
  635 }
  636 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);

Cache object: 69e5d45a0833503488f2839953dcdc2b


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