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

Cache object: 0acef210a7f25a758f9d755760692840


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