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/netinet6/ipcomp_input.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 /*      $KAME: ipcomp_input.c,v 1.25 2001/03/01 09:12:09 itojun Exp $   */
    3 
    4 /*-
    5  * Copyright (C) 1999 WIDE Project.
    6  * All rights reserved.
    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  * 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. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * RFC2393 IP payload compression protocol (IPComp).
   35  */
   36 
   37 #include "opt_inet.h"
   38 #include "opt_inet6.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/mbuf.h>
   43 #include <sys/domain.h>
   44 #include <sys/protosw.h>
   45 #include <sys/socket.h>
   46 #include <sys/errno.h>
   47 #include <sys/time.h>
   48 #include <sys/syslog.h>
   49 
   50 #include <net/if.h>
   51 #include <net/route.h>
   52 #include <net/netisr.h>
   53 #include <net/zlib.h>
   54 #include <machine/cpu.h>
   55 
   56 #include <netinet/in.h>
   57 #include <netinet/in_systm.h>
   58 #include <netinet/in_var.h>
   59 #include <netinet/ip.h>
   60 #include <netinet/ip_var.h>
   61 #include <netinet/ip_ecn.h>
   62 
   63 #ifdef INET6
   64 #include <netinet/ip6.h>
   65 #include <netinet6/ip6_var.h>
   66 #endif
   67 #include <netinet6/ipcomp.h>
   68 #ifdef INET6
   69 #include <netinet6/ipcomp6.h>
   70 #endif
   71 
   72 #include <netinet6/ipsec.h>
   73 #ifdef INET6
   74 #include <netinet6/ipsec6.h>
   75 #endif
   76 #include <netkey/key.h>
   77 #include <netkey/keydb.h>
   78 
   79 #include <machine/stdarg.h>
   80 
   81 #include <net/net_osdep.h>
   82 
   83 #define IPLEN_FLIPPED
   84 
   85 #ifdef INET
   86 extern struct protosw inetsw[];
   87 
   88 void
   89 ipcomp4_input(m, off)
   90         struct mbuf *m;
   91         int off;
   92 {
   93         struct mbuf *md;
   94         struct ip *ip;
   95         struct ipcomp *ipcomp;
   96         const struct ipcomp_algorithm *algo;
   97         u_int16_t cpi;  /* host order */
   98         u_int16_t nxt;
   99         size_t hlen;
  100         int error;
  101         size_t newlen, olen;
  102         struct secasvar *sav = NULL;
  103 
  104         if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
  105                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
  106                     "(packet too short)\n"));
  107                 ipsecstat.in_inval++;
  108                 goto fail;
  109         }
  110 
  111         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
  112         if (!md) {
  113                 m = NULL;       /* already freed */
  114                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
  115                     "(pulldown failure)\n"));
  116                 ipsecstat.in_inval++;
  117                 goto fail;
  118         }
  119         ipcomp = mtod(md, struct ipcomp *);
  120         nxt = ipcomp->comp_nxt;
  121 
  122         /*
  123          * Check that the next header of the IPComp is not IPComp again, before
  124          * doing any real work.  Given it is not possible to do double
  125          * compression it means someone is playing tricks on us.
  126          */
  127         if (nxt == IPPROTO_IPCOMP) {
  128                 ipseclog((LOG_ERR, "IPv4 IPComp input: "
  129                     "recursive compression detected."));
  130                 ipsecstat.in_inval++;
  131                 goto fail;
  132         }
  133 
  134         ip = mtod(m, struct ip *);
  135 #ifdef _IP_VHL
  136         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
  137 #else
  138         hlen = ip->ip_hl << 2;
  139 #endif
  140 
  141         cpi = ntohs(ipcomp->comp_cpi);
  142 
  143         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
  144                 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
  145                         (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
  146                 if (sav != NULL
  147                  && (sav->state == SADB_SASTATE_MATURE
  148                   || sav->state == SADB_SASTATE_DYING)) {
  149                         cpi = sav->alg_enc;     /* XXX */
  150                         /* other parameters to look at? */
  151                 }
  152         }
  153         algo = ipcomp_algorithm_lookup(cpi);
  154         if (!algo) {
  155                 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
  156                         cpi));
  157                 ipsecstat.in_nosa++;
  158                 goto fail;
  159         }
  160 
  161         /* chop ipcomp header */
  162         ipcomp = NULL;
  163         md->m_data += sizeof(struct ipcomp);
  164         md->m_len -= sizeof(struct ipcomp);
  165         m->m_pkthdr.len -= sizeof(struct ipcomp);
  166 #ifdef IPLEN_FLIPPED
  167         ip->ip_len -= sizeof(struct ipcomp);
  168 #else
  169         ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
  170 #endif
  171 
  172         olen = m->m_pkthdr.len;
  173         newlen = m->m_pkthdr.len - off;
  174         error = (*algo->decompress)(m, m->m_next, &newlen);
  175         if (error != 0) {
  176                 if (error == EINVAL)
  177                         ipsecstat.in_inval++;
  178                 else if (error == ENOBUFS)
  179                         ipsecstat.in_nomem++;
  180                 m = NULL;
  181                 goto fail;
  182         }
  183         ipsecstat.in_comphist[cpi]++;
  184 
  185         /*
  186          * returning decompressed packet onto icmp is meaningless.
  187          * mark it decrypted to prevent icmp from attaching original packet.
  188          */
  189         m->m_flags |= M_DECRYPTED;
  190 
  191         m->m_pkthdr.len = off + newlen;
  192         ip = mtod(m, struct ip *);
  193     {
  194         size_t len;
  195 #ifdef IPLEN_FLIPPED
  196         len = ip->ip_len;
  197 #else
  198         len = ntohs(ip->ip_len);
  199 #endif
  200         /*
  201          * be careful about underflow.  also, do not assign exact value
  202          * as ip_len is manipulated differently on *BSDs.
  203          */
  204         len += m->m_pkthdr.len;
  205         len -= olen;
  206         if (len & ~0xffff) {
  207                 /* packet too big after decompress */
  208                 ipsecstat.in_inval++;
  209                 goto fail;
  210         }
  211 #ifdef IPLEN_FLIPPED
  212         ip->ip_len = len & 0xffff;
  213 #else
  214         ip->ip_len = htons(len & 0xffff);
  215 #endif
  216         ip->ip_p = nxt;
  217     }
  218 
  219         if (sav) {
  220                 key_sa_recordxfer(sav, m);
  221                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
  222                         ipsecstat.in_nomem++;
  223                         goto fail;
  224                 }
  225                 key_freesav(sav);
  226                 sav = NULL;
  227         }
  228 
  229         if (nxt != IPPROTO_DONE) {
  230                 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
  231                     ipsec4_in_reject(m, NULL)) {
  232                         ipsecstat.in_polvio++;
  233                         goto fail;
  234                 }
  235                 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
  236         } else
  237                 m_freem(m);
  238         m = NULL;
  239 
  240         ipsecstat.in_success++;
  241         return;
  242 
  243 fail:
  244         if (sav)
  245                 key_freesav(sav);
  246         if (m)
  247                 m_freem(m);
  248         return;
  249 }
  250 #endif /* INET */
  251 
  252 #ifdef INET6
  253 int
  254 ipcomp6_input(mp, offp, proto)
  255         struct mbuf **mp;
  256         int *offp, proto;
  257 {
  258         struct mbuf *m, *md;
  259         int off;
  260         struct ip6_hdr *ip6;
  261         struct ipcomp *ipcomp;
  262         const struct ipcomp_algorithm *algo;
  263         u_int16_t cpi;  /* host order */
  264         u_int16_t nxt;
  265         int error;
  266         size_t newlen;
  267         struct secasvar *sav = NULL;
  268         u_int8_t *prvnxtp;
  269 
  270         m = *mp;
  271         off = *offp;
  272 
  273         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
  274         if (!md) {
  275                 m = NULL;       /* already freed */
  276                 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
  277                     "(pulldown failure)\n"));
  278                 ipsec6stat.in_inval++;
  279                 goto fail;
  280         }
  281         ipcomp = mtod(md, struct ipcomp *);
  282         ip6 = mtod(m, struct ip6_hdr *);
  283         nxt = ipcomp->comp_nxt;
  284 
  285         /*
  286          * Check that the next header of the IPComp is not IPComp again, before
  287          * doing any real work.  Given it is not possible to do double
  288          * compression it means someone is playing tricks on us.
  289          */
  290         if (nxt == IPPROTO_IPCOMP) {
  291                 ipseclog((LOG_ERR, "IPv6 IPComp input: "
  292                     "recursive compression detected."));
  293                 ipsecstat.in_inval++;
  294                 goto fail;
  295         }
  296 
  297         cpi = ntohs(ipcomp->comp_cpi);
  298 
  299         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
  300                 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
  301                     (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
  302                 if (sav != NULL
  303                  && (sav->state == SADB_SASTATE_MATURE
  304                   || sav->state == SADB_SASTATE_DYING)) {
  305                         cpi = sav->alg_enc;     /* XXX */
  306                         /* other parameters to look at? */
  307                 }
  308         }
  309         algo = ipcomp_algorithm_lookup(cpi);
  310         if (!algo) {
  311                 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
  312                         "dropping the packet for simplicity\n", cpi));
  313                 ipsec6stat.in_nosa++;
  314                 goto fail;
  315         }
  316 
  317         /* chop ipcomp header */
  318         ipcomp = NULL;
  319         md->m_data += sizeof(struct ipcomp);
  320         md->m_len -= sizeof(struct ipcomp);
  321         m->m_pkthdr.len -= sizeof(struct ipcomp);
  322 
  323         newlen = m->m_pkthdr.len - off;
  324         error = (*algo->decompress)(m, md, &newlen);
  325         if (error != 0) {
  326                 if (error == EINVAL)
  327                         ipsec6stat.in_inval++;
  328                 else if (error == ENOBUFS)
  329                         ipsec6stat.in_nomem++;
  330                 m = NULL;
  331                 goto fail;
  332         }
  333         ipsec6stat.in_comphist[cpi]++;
  334         m->m_pkthdr.len = off + newlen;
  335 
  336         /*
  337          * returning decompressed packet onto icmp is meaningless.
  338          * mark it decrypted to prevent icmp from attaching original packet.
  339          */
  340         m->m_flags |= M_DECRYPTED;
  341 
  342         /* update next header field */
  343         prvnxtp = ip6_get_prevhdr(m, off);
  344         *prvnxtp = nxt;
  345 
  346         /*
  347          * no need to adjust payload length, as all the IPv6 protocols
  348          * look at m->m_pkthdr.len
  349          */
  350 
  351         if (sav) {
  352                 key_sa_recordxfer(sav, m);
  353                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
  354                         ipsec6stat.in_nomem++;
  355                         goto fail;
  356                 }
  357                 key_freesav(sav);
  358                 sav = NULL;
  359         }
  360         *offp = off;
  361         *mp = m;
  362         ipsec6stat.in_success++;
  363         return nxt;
  364 
  365 fail:
  366         if (m)
  367                 m_freem(m);
  368         if (sav)
  369                 key_freesav(sav);
  370         return IPPROTO_DONE;
  371 }
  372 #endif /* INET6 */

Cache object: 8aa483d3a0443b67ed5b46207749b79c


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