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/ip6_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 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1982, 1986, 1988, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 4. Neither the name of the University nor the names of its contributors
   45  *    may be used to endorse or promote products derived from this software
   46  *    without specific prior written permission.
   47  *
   48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   58  * SUCH DAMAGE.
   59  *
   60  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __FBSDID("$FreeBSD: releng/7.4/sys/netinet6/ip6_input.c 215368 2010-11-16 04:40:03Z sobomax $");
   65 
   66 #include "opt_inet.h"
   67 #include "opt_inet6.h"
   68 #include "opt_ipsec.h"
   69 
   70 #include <sys/param.h>
   71 #include <sys/systm.h>
   72 #include <sys/malloc.h>
   73 #include <sys/mbuf.h>
   74 #include <sys/proc.h>
   75 #include <sys/domain.h>
   76 #include <sys/protosw.h>
   77 #include <sys/socket.h>
   78 #include <sys/socketvar.h>
   79 #include <sys/errno.h>
   80 #include <sys/time.h>
   81 #include <sys/kernel.h>
   82 #include <sys/syslog.h>
   83 
   84 #include <net/if.h>
   85 #include <net/if_types.h>
   86 #include <net/if_dl.h>
   87 #include <net/route.h>
   88 #include <net/netisr.h>
   89 #include <net/pfil.h>
   90 
   91 #include <netinet/in.h>
   92 #include <netinet/in_systm.h>
   93 #ifdef INET
   94 #include <netinet/ip.h>
   95 #include <netinet/ip_icmp.h>
   96 #endif /* INET */
   97 #include <netinet/ip6.h>
   98 #include <netinet6/in6_var.h>
   99 #include <netinet6/ip6_var.h>
  100 #include <netinet/in_pcb.h>
  101 #include <netinet/icmp6.h>
  102 #include <netinet6/scope6_var.h>
  103 #include <netinet6/in6_ifattach.h>
  104 #include <netinet6/nd6.h>
  105 
  106 #ifdef IPSEC
  107 #include <netipsec/ipsec.h>
  108 #include <netinet6/ip6_ipsec.h>
  109 #include <netipsec/ipsec6.h>
  110 #endif /* IPSEC */
  111 
  112 #include <netinet6/ip6protosw.h>
  113 
  114 extern struct domain inet6domain;
  115 
  116 u_char ip6_protox[IPPROTO_MAX];
  117 static struct ifqueue ip6intrq;
  118 struct in6_ifaddr *in6_ifaddr;
  119 
  120 extern struct callout in6_tmpaddrtimer_ch;
  121 
  122 struct pfil_head inet6_pfil_hook;
  123 
  124 struct ip6stat ip6stat;
  125 
  126 static void ip6_init2(void *);
  127 static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
  128 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
  129 #ifdef PULLDOWN_TEST
  130 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
  131 #endif
  132 
  133 /*
  134  * IP6 initialization: fill in IP6 protocol switch table.
  135  * All protocols not implemented in kernel go to raw IP6 protocol handler.
  136  */
  137 void
  138 ip6_init(void)
  139 {
  140         struct ip6protosw *pr;
  141         int i;
  142 
  143 #ifdef DIAGNOSTIC
  144         if (sizeof(struct protosw) != sizeof(struct ip6protosw))
  145                 panic("sizeof(protosw) != sizeof(ip6protosw)");
  146 #endif
  147         pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  148         if (pr == NULL)
  149                 panic("ip6_init");
  150 
  151         /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
  152         for (i = 0; i < IPPROTO_MAX; i++)
  153                 ip6_protox[i] = pr - inet6sw;
  154         /*
  155          * Cycle through IP protocols and put them into the appropriate place
  156          * in ip6_protox[].
  157          */
  158         for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
  159             pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
  160                 if (pr->pr_domain->dom_family == PF_INET6 &&
  161                     pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
  162                         /* Be careful to only index valid IP protocols. */
  163                         if (pr->pr_protocol < IPPROTO_MAX)
  164                                 ip6_protox[pr->pr_protocol] = pr - inet6sw;
  165                 }
  166 
  167         /* Initialize packet filter hooks. */
  168         inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
  169         inet6_pfil_hook.ph_af = AF_INET6;
  170         if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
  171                 printf("%s: WARNING: unable to register pfil hook, "
  172                         "error %d\n", __func__, i);
  173 
  174         ip6intrq.ifq_maxlen = ifqmaxlen;
  175         mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
  176         netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
  177         scope6_init();
  178         addrsel_policy_init();
  179         nd6_init();
  180         frag6_init();
  181         ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
  182 }
  183 
  184 /*
  185  * The protocol to be inserted into ip6_protox[] must be already registered
  186  * in inet6sw[], either statically or through pf_proto_register().
  187  */
  188 int
  189 ip6proto_register(short ip6proto)
  190 {
  191         struct ip6protosw *pr;
  192 
  193         /* Sanity checks. */
  194         if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
  195                 return (EPROTONOSUPPORT);
  196 
  197         /*
  198          * The protocol slot must not be occupied by another protocol
  199          * already.  An index pointing to IPPROTO_RAW is unused.
  200          */
  201         pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  202         if (pr == NULL)
  203                 return (EPFNOSUPPORT);
  204         if (ip6_protox[ip6proto] != pr - inet6sw)       /* IPPROTO_RAW */
  205                 return (EEXIST);
  206 
  207         /*
  208          * Find the protocol position in inet6sw[] and set the index.
  209          */
  210         for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
  211             pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) {
  212                 if (pr->pr_domain->dom_family == PF_INET6 &&
  213                     pr->pr_protocol && pr->pr_protocol == ip6proto) {
  214                         ip6_protox[pr->pr_protocol] = pr - inet6sw;
  215                         return (0);
  216                 }
  217         }
  218         return (EPROTONOSUPPORT);
  219 }
  220 
  221 int
  222 ip6proto_unregister(short ip6proto)
  223 {
  224         struct ip6protosw *pr;
  225 
  226         /* Sanity checks. */
  227         if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
  228                 return (EPROTONOSUPPORT);
  229 
  230         /* Check if the protocol was indeed registered. */
  231         pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  232         if (pr == NULL)
  233                 return (EPFNOSUPPORT);
  234         if (ip6_protox[ip6proto] == pr - inet6sw)       /* IPPROTO_RAW */
  235                 return (ENOENT);
  236 
  237         /* Reset the protocol slot to IPPROTO_RAW. */
  238         ip6_protox[ip6proto] = pr - inet6sw;
  239         return (0);
  240 }
  241 
  242 static void
  243 ip6_init2(void *dummy)
  244 {
  245 
  246         /* nd6_timer_init */
  247         callout_init(&nd6_timer_ch, 0);
  248         callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
  249 
  250         /* timer for regeneranation of temporary addresses randomize ID */
  251         callout_init(&in6_tmpaddrtimer_ch, 0);
  252         callout_reset(&in6_tmpaddrtimer_ch,
  253                       (ip6_temp_preferred_lifetime - ip6_desync_factor -
  254                        ip6_temp_regen_advance) * hz,
  255                       in6_tmpaddrtimer, NULL);
  256 }
  257 
  258 /* cheat */
  259 /* This must be after route_init(), which is now SI_ORDER_THIRD */
  260 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
  261 
  262 extern struct   route_in6 ip6_forward_rt;
  263 
  264 void
  265 ip6_input(struct mbuf *m)
  266 {
  267         struct ip6_hdr *ip6;
  268         int off = sizeof(struct ip6_hdr), nest;
  269         u_int32_t plen;
  270         u_int32_t rtalert = ~0;
  271         int nxt, ours = 0;
  272         struct ifnet *deliverifp = NULL;
  273         struct in6_addr odst;
  274         int srcrt = 0;
  275 
  276         GIANT_REQUIRED;                 /* XXX for now */
  277 
  278 #ifdef IPSEC
  279         /*
  280          * should the inner packet be considered authentic?
  281          * see comment in ah4_input().
  282          * NB: m cannot be NULL when passed to the input routine
  283          */
  284 
  285         m->m_flags &= ~M_AUTHIPHDR;
  286         m->m_flags &= ~M_AUTHIPDGM;
  287 
  288 #endif /* IPSEC */
  289 
  290         /*
  291          * make sure we don't have onion peering information into m_tag.
  292          */
  293         ip6_delaux(m);
  294 
  295         /*
  296          * mbuf statistics
  297          */
  298         if (m->m_flags & M_EXT) {
  299                 if (m->m_next)
  300                         ip6stat.ip6s_mext2m++;
  301                 else
  302                         ip6stat.ip6s_mext1++;
  303         } else {
  304 #define M2MMAX  (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
  305                 if (m->m_next) {
  306                         if (m->m_flags & M_LOOP) {
  307                                 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
  308                         } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
  309                                 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
  310                         else
  311                                 ip6stat.ip6s_m2m[0]++;
  312                 } else
  313                         ip6stat.ip6s_m1++;
  314 #undef M2MMAX
  315         }
  316 
  317         /* drop the packet if IPv6 operation is disabled on the IF */
  318         if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
  319                 m_freem(m);
  320                 return;
  321         }
  322 
  323         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
  324         ip6stat.ip6s_total++;
  325 
  326 #ifndef PULLDOWN_TEST
  327         /*
  328          * L2 bridge code and some other code can return mbuf chain
  329          * that does not conform to KAME requirement.  too bad.
  330          * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
  331          */
  332         if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
  333                 struct mbuf *n;
  334 
  335                 MGETHDR(n, M_DONTWAIT, MT_HEADER);
  336                 if (n)
  337                         M_MOVE_PKTHDR(n, m);
  338                 if (n && n->m_pkthdr.len > MHLEN) {
  339                         MCLGET(n, M_DONTWAIT);
  340                         if ((n->m_flags & M_EXT) == 0) {
  341                                 m_freem(n);
  342                                 n = NULL;
  343                         }
  344                 }
  345                 if (n == NULL) {
  346                         m_freem(m);
  347                         return; /* ENOBUFS */
  348                 }
  349 
  350                 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
  351                 n->m_len = n->m_pkthdr.len;
  352                 m_freem(m);
  353                 m = n;
  354         }
  355         IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
  356 #endif
  357 
  358         if (m->m_len < sizeof(struct ip6_hdr)) {
  359                 struct ifnet *inifp;
  360                 inifp = m->m_pkthdr.rcvif;
  361                 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
  362                         ip6stat.ip6s_toosmall++;
  363                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
  364                         return;
  365                 }
  366         }
  367 
  368         ip6 = mtod(m, struct ip6_hdr *);
  369 
  370         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
  371                 ip6stat.ip6s_badvers++;
  372                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
  373                 goto bad;
  374         }
  375 
  376         ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
  377 
  378         /*
  379          * Check against address spoofing/corruption.
  380          */
  381         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
  382             IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
  383                 /*
  384                  * XXX: "badscope" is not very suitable for a multicast source.
  385                  */
  386                 ip6stat.ip6s_badscope++;
  387                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
  388                 goto bad;
  389         }
  390         if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
  391             !(m->m_flags & M_LOOP)) {
  392                 /*
  393                  * In this case, the packet should come from the loopback
  394                  * interface.  However, we cannot just check the if_flags,
  395                  * because ip6_mloopback() passes the "actual" interface
  396                  * as the outgoing/incoming interface.
  397                  */
  398                 ip6stat.ip6s_badscope++;
  399                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
  400                 goto bad;
  401         }
  402 
  403 #ifdef ALTQ
  404         if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
  405                 /* packet is dropped by traffic conditioner */
  406                 return;
  407         }
  408 #endif
  409         /*
  410          * The following check is not documented in specs.  A malicious
  411          * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
  412          * and bypass security checks (act as if it was from 127.0.0.1 by using
  413          * IPv6 src ::ffff:127.0.0.1).  Be cautious.
  414          *
  415          * This check chokes if we are in an SIIT cloud.  As none of BSDs
  416          * support IPv4-less kernel compilation, we cannot support SIIT
  417          * environment at all.  So, it makes more sense for us to reject any
  418          * malicious packets for non-SIIT environment, than try to do a
  419          * partial support for SIIT environment.
  420          */
  421         if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
  422             IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
  423                 ip6stat.ip6s_badscope++;
  424                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
  425                 goto bad;
  426         }
  427 #if 0
  428         /*
  429          * Reject packets with IPv4 compatible addresses (auto tunnel).
  430          *
  431          * The code forbids auto tunnel relay case in RFC1933 (the check is
  432          * stronger than RFC1933).  We may want to re-enable it if mech-xx
  433          * is revised to forbid relaying case.
  434          */
  435         if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
  436             IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
  437                 ip6stat.ip6s_badscope++;
  438                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
  439                 goto bad;
  440         }
  441 #endif
  442 
  443         /*
  444          * Run through list of hooks for input packets.
  445          *
  446          * NB: Beware of the destination address changing
  447          *     (e.g. by NAT rewriting).  When this happens,
  448          *     tell ip6_forward to do the right thing.
  449          */
  450         odst = ip6->ip6_dst;
  451 
  452         /* Jump over all PFIL processing if hooks are not active. */
  453         if (!PFIL_HOOKED(&inet6_pfil_hook))
  454                 goto passin;
  455 
  456         if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL))
  457                 return;
  458         if (m == NULL)                  /* consumed by filter */
  459                 return;
  460         ip6 = mtod(m, struct ip6_hdr *);
  461         srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
  462 
  463 passin:
  464         /*
  465          * Disambiguate address scope zones (if there is ambiguity).
  466          * We first make sure that the original source or destination address
  467          * is not in our internal form for scoped addresses.  Such addresses
  468          * are not necessarily invalid spec-wise, but we cannot accept them due
  469          * to the usage conflict.
  470          * in6_setscope() then also checks and rejects the cases where src or
  471          * dst are the loopback address and the receiving interface
  472          * is not loopback.
  473          */
  474         if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
  475                 ip6stat.ip6s_badscope++; /* XXX */
  476                 goto bad;
  477         }
  478         if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
  479             in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
  480                 ip6stat.ip6s_badscope++;
  481                 goto bad;
  482         }
  483 
  484         /*
  485          * Multicast check
  486          */
  487         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  488                 struct in6_multi *in6m = 0;
  489 
  490                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
  491                 /*
  492                  * See if we belong to the destination multicast group on the
  493                  * arrival interface.
  494                  */
  495                 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
  496                 if (in6m)
  497                         ours = 1;
  498                 else if (!ip6_mrouter) {
  499                         ip6stat.ip6s_notmember++;
  500                         ip6stat.ip6s_cantforward++;
  501                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
  502                         goto bad;
  503                 }
  504                 deliverifp = m->m_pkthdr.rcvif;
  505                 goto hbhcheck;
  506         }
  507 
  508         /*
  509          *  Unicast check
  510          */
  511         if (ip6_forward_rt.ro_rt != NULL &&
  512             (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
  513             IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
  514             &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
  515                 ip6stat.ip6s_forward_cachehit++;
  516         else {
  517                 struct sockaddr_in6 *dst6;
  518 
  519                 if (ip6_forward_rt.ro_rt) {
  520                         /* route is down or destination is different */
  521                         ip6stat.ip6s_forward_cachemiss++;
  522                         RTFREE(ip6_forward_rt.ro_rt);
  523                         ip6_forward_rt.ro_rt = 0;
  524                 }
  525 
  526                 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
  527                 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
  528                 dst6->sin6_len = sizeof(struct sockaddr_in6);
  529                 dst6->sin6_family = AF_INET6;
  530                 dst6->sin6_addr = ip6->ip6_dst;
  531 
  532                 rtalloc((struct route *)&ip6_forward_rt);
  533         }
  534 
  535 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
  536 
  537         /*
  538          * Accept the packet if the forwarding interface to the destination
  539          * according to the routing table is the loopback interface,
  540          * unless the associated route has a gateway.
  541          * Note that this approach causes to accept a packet if there is a
  542          * route to the loopback interface for the destination of the packet.
  543          * But we think it's even useful in some situations, e.g. when using
  544          * a special daemon which wants to intercept the packet.
  545          *
  546          * XXX: some OSes automatically make a cloned route for the destination
  547          * of an outgoing packet.  If the outgoing interface of the packet
  548          * is a loopback one, the kernel would consider the packet to be
  549          * accepted, even if we have no such address assinged on the interface.
  550          * We check the cloned flag of the route entry to reject such cases,
  551          * assuming that route entries for our own addresses are not made by
  552          * cloning (it should be true because in6_addloop explicitly installs
  553          * the host route).  However, we might have to do an explicit check
  554          * while it would be less efficient.  Or, should we rather install a
  555          * reject route for such a case?
  556          */
  557         if (ip6_forward_rt.ro_rt &&
  558             (ip6_forward_rt.ro_rt->rt_flags &
  559              (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
  560 #ifdef RTF_WASCLONED
  561             !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
  562 #endif
  563 #ifdef RTF_CLONED
  564             !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
  565 #endif
  566 #if 0
  567             /*
  568              * The check below is redundant since the comparison of
  569              * the destination and the key of the rtentry has
  570              * already done through looking up the routing table.
  571              */
  572             IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
  573             &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
  574 #endif
  575             ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
  576                 struct in6_ifaddr *ia6 =
  577                         (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
  578 
  579                 /*
  580                  * record address information into m_tag.
  581                  */
  582                 (void)ip6_setdstifaddr(m, ia6);
  583 
  584                 /*
  585                  * packets to a tentative, duplicated, or somehow invalid
  586                  * address must not be accepted.
  587                  */
  588                 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
  589                         /* this address is ready */
  590                         ours = 1;
  591                         deliverifp = ia6->ia_ifp;       /* correct? */
  592                         /* Count the packet in the ip address stats */
  593                         ia6->ia_ifa.if_ipackets++;
  594                         ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
  595                         goto hbhcheck;
  596                 } else {
  597                         char ip6bufs[INET6_ADDRSTRLEN];
  598                         char ip6bufd[INET6_ADDRSTRLEN];
  599                         /* address is not ready, so discard the packet. */
  600                         nd6log((LOG_INFO,
  601                             "ip6_input: packet to an unready address %s->%s\n",
  602                             ip6_sprintf(ip6bufs, &ip6->ip6_src),
  603                             ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
  604 
  605                         goto bad;
  606                 }
  607         }
  608 
  609         /*
  610          * FAITH (Firewall Aided Internet Translator)
  611          */
  612         if (ip6_keepfaith) {
  613                 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
  614                  && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
  615                         /* XXX do we need more sanity checks? */
  616                         ours = 1;
  617                         deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
  618                         goto hbhcheck;
  619                 }
  620         }
  621 
  622         /*
  623          * Now there is no reason to process the packet if it's not our own
  624          * and we're not a router.
  625          */
  626         if (!ip6_forwarding) {
  627                 ip6stat.ip6s_cantforward++;
  628                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
  629                 goto bad;
  630         }
  631 
  632   hbhcheck:
  633         /*
  634          * record address information into m_tag, if we don't have one yet.
  635          * note that we are unable to record it, if the address is not listed
  636          * as our interface address (e.g. multicast addresses, addresses
  637          * within FAITH prefixes and such).
  638          */
  639         if (deliverifp && !ip6_getdstifaddr(m)) {
  640                 struct in6_ifaddr *ia6;
  641 
  642                 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
  643                 if (ia6) {
  644                         if (!ip6_setdstifaddr(m, ia6)) {
  645                                 /*
  646                                  * XXX maybe we should drop the packet here,
  647                                  * as we could not provide enough information
  648                                  * to the upper layers.
  649                                  */
  650                         }
  651                 }
  652         }
  653 
  654         /*
  655          * Process Hop-by-Hop options header if it's contained.
  656          * m may be modified in ip6_hopopts_input().
  657          * If a JumboPayload option is included, plen will also be modified.
  658          */
  659         plen = (u_int32_t)ntohs(ip6->ip6_plen);
  660         if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
  661                 struct ip6_hbh *hbh;
  662 
  663                 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
  664 #if 0   /*touches NULL pointer*/
  665                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
  666 #endif
  667                         return; /* m have already been freed */
  668                 }
  669 
  670                 /* adjust pointer */
  671                 ip6 = mtod(m, struct ip6_hdr *);
  672 
  673                 /*
  674                  * if the payload length field is 0 and the next header field
  675                  * indicates Hop-by-Hop Options header, then a Jumbo Payload
  676                  * option MUST be included.
  677                  */
  678                 if (ip6->ip6_plen == 0 && plen == 0) {
  679                         /*
  680                          * Note that if a valid jumbo payload option is
  681                          * contained, ip6_hopopts_input() must set a valid
  682                          * (non-zero) payload length to the variable plen.
  683                          */
  684                         ip6stat.ip6s_badoptions++;
  685                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
  686                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
  687                         icmp6_error(m, ICMP6_PARAM_PROB,
  688                                     ICMP6_PARAMPROB_HEADER,
  689                                     (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
  690                         return;
  691                 }
  692 #ifndef PULLDOWN_TEST
  693                 /* ip6_hopopts_input() ensures that mbuf is contiguous */
  694                 hbh = (struct ip6_hbh *)(ip6 + 1);
  695 #else
  696                 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
  697                         sizeof(struct ip6_hbh));
  698                 if (hbh == NULL) {
  699                         ip6stat.ip6s_tooshort++;
  700                         return;
  701                 }
  702 #endif
  703                 nxt = hbh->ip6h_nxt;
  704 
  705                 /*
  706                  * If we are acting as a router and the packet contains a
  707                  * router alert option, see if we know the option value.
  708                  * Currently, we only support the option value for MLD, in which
  709                  * case we should pass the packet to the multicast routing
  710                  * daemon.
  711                  */
  712                 if (rtalert != ~0 && ip6_forwarding) {
  713                         switch (rtalert) {
  714                         case IP6OPT_RTALERT_MLD:
  715                                 ours = 1;
  716                                 break;
  717                         default:
  718                                 /*
  719                                  * RFC2711 requires unrecognized values must be
  720                                  * silently ignored.
  721                                  */
  722                                 break;
  723                         }
  724                 }
  725         } else
  726                 nxt = ip6->ip6_nxt;
  727 
  728         /*
  729          * Check that the amount of data in the buffers
  730          * is as at least much as the IPv6 header would have us expect.
  731          * Trim mbufs if longer than we expect.
  732          * Drop packet if shorter than we expect.
  733          */
  734         if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
  735                 ip6stat.ip6s_tooshort++;
  736                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
  737                 goto bad;
  738         }
  739         if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
  740                 if (m->m_len == m->m_pkthdr.len) {
  741                         m->m_len = sizeof(struct ip6_hdr) + plen;
  742                         m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
  743                 } else
  744                         m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
  745         }
  746 
  747         /*
  748          * Forward if desirable.
  749          */
  750         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  751                 /*
  752                  * If we are acting as a multicast router, all
  753                  * incoming multicast packets are passed to the
  754                  * kernel-level multicast forwarding function.
  755                  * The packet is returned (relatively) intact; if
  756                  * ip6_mforward() returns a non-zero value, the packet
  757                  * must be discarded, else it may be accepted below.
  758                  */
  759                 if (ip6_mrouter && ip6_mforward &&
  760                     ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
  761                         ip6stat.ip6s_cantforward++;
  762                         m_freem(m);
  763                         return;
  764                 }
  765                 if (!ours) {
  766                         m_freem(m);
  767                         return;
  768                 }
  769         } else if (!ours) {
  770                 ip6_forward(m, srcrt);
  771                 return;
  772         }
  773 
  774         ip6 = mtod(m, struct ip6_hdr *);
  775 
  776         /*
  777          * Malicious party may be able to use IPv4 mapped addr to confuse
  778          * tcp/udp stack and bypass security checks (act as if it was from
  779          * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
  780          *
  781          * For SIIT end node behavior, you may want to disable the check.
  782          * However, you will  become vulnerable to attacks using IPv4 mapped
  783          * source.
  784          */
  785         if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
  786             IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
  787                 ip6stat.ip6s_badscope++;
  788                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
  789                 goto bad;
  790         }
  791 
  792         /*
  793          * Tell launch routine the next header
  794          */
  795         ip6stat.ip6s_delivered++;
  796         in6_ifstat_inc(deliverifp, ifs6_in_deliver);
  797         nest = 0;
  798 
  799         while (nxt != IPPROTO_DONE) {
  800                 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
  801                         ip6stat.ip6s_toomanyhdr++;
  802                         goto bad;
  803                 }
  804 
  805                 /*
  806                  * protection against faulty packet - there should be
  807                  * more sanity checks in header chain processing.
  808                  */
  809                 if (m->m_pkthdr.len < off) {
  810                         ip6stat.ip6s_tooshort++;
  811                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
  812                         goto bad;
  813                 }
  814 
  815 #ifdef IPSEC
  816                 /*
  817                  * enforce IPsec policy checking if we are seeing last header.
  818                  * note that we do not visit this with protocols with pcb layer
  819                  * code - like udp/tcp/raw ip.
  820                  */
  821                 if (ip6_ipsec_input(m, nxt))
  822                         goto bad;
  823 #endif /* IPSEC */
  824                 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
  825         }
  826         return;
  827  bad:
  828         m_freem(m);
  829 }
  830 
  831 /*
  832  * set/grab in6_ifaddr correspond to IPv6 destination address.
  833  * XXX backward compatibility wrapper
  834  */
  835 static struct ip6aux *
  836 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
  837 {
  838         struct ip6aux *ip6a;
  839 
  840         ip6a = ip6_addaux(m);
  841         if (ip6a)
  842                 ip6a->ip6a_dstia6 = ia6;
  843         return ip6a;    /* NULL if failed to set */
  844 }
  845 
  846 struct in6_ifaddr *
  847 ip6_getdstifaddr(struct mbuf *m)
  848 {
  849         struct ip6aux *ip6a;
  850 
  851         ip6a = ip6_findaux(m);
  852         if (ip6a)
  853                 return ip6a->ip6a_dstia6;
  854         else
  855                 return NULL;
  856 }
  857 
  858 /*
  859  * Hop-by-Hop options header processing. If a valid jumbo payload option is
  860  * included, the real payload length will be stored in plenp.
  861  *
  862  * rtalertp - XXX: should be stored more smart way
  863  */
  864 static int
  865 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
  866     struct mbuf **mp, int *offp)
  867 {
  868         struct mbuf *m = *mp;
  869         int off = *offp, hbhlen;
  870         struct ip6_hbh *hbh;
  871         u_int8_t *opt;
  872 
  873         /* validation of the length of the header */
  874 #ifndef PULLDOWN_TEST
  875         IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
  876         hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
  877         hbhlen = (hbh->ip6h_len + 1) << 3;
  878 
  879         IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
  880         hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
  881 #else
  882         IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
  883                 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
  884         if (hbh == NULL) {
  885                 ip6stat.ip6s_tooshort++;
  886                 return -1;
  887         }
  888         hbhlen = (hbh->ip6h_len + 1) << 3;
  889         IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
  890                 hbhlen);
  891         if (hbh == NULL) {
  892                 ip6stat.ip6s_tooshort++;
  893                 return -1;
  894         }
  895 #endif
  896         off += hbhlen;
  897         hbhlen -= sizeof(struct ip6_hbh);
  898         opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
  899 
  900         if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
  901                                 hbhlen, rtalertp, plenp) < 0)
  902                 return (-1);
  903 
  904         *offp = off;
  905         *mp = m;
  906         return (0);
  907 }
  908 
  909 /*
  910  * Search header for all Hop-by-hop options and process each option.
  911  * This function is separate from ip6_hopopts_input() in order to
  912  * handle a case where the sending node itself process its hop-by-hop
  913  * options header. In such a case, the function is called from ip6_output().
  914  *
  915  * The function assumes that hbh header is located right after the IPv6 header
  916  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
  917  * opthead + hbhlen is located in continuous memory region.
  918  */
  919 int
  920 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
  921     u_int32_t *rtalertp, u_int32_t *plenp)
  922 {
  923         struct ip6_hdr *ip6;
  924         int optlen = 0;
  925         u_int8_t *opt = opthead;
  926         u_int16_t rtalert_val;
  927         u_int32_t jumboplen;
  928         const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
  929 
  930         for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
  931                 switch (*opt) {
  932                 case IP6OPT_PAD1:
  933                         optlen = 1;
  934                         break;
  935                 case IP6OPT_PADN:
  936                         if (hbhlen < IP6OPT_MINLEN) {
  937                                 ip6stat.ip6s_toosmall++;
  938                                 goto bad;
  939                         }
  940                         optlen = *(opt + 1) + 2;
  941                         break;
  942                 case IP6OPT_ROUTER_ALERT:
  943                         /* XXX may need check for alignment */
  944                         if (hbhlen < IP6OPT_RTALERT_LEN) {
  945                                 ip6stat.ip6s_toosmall++;
  946                                 goto bad;
  947                         }
  948                         if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
  949                                 /* XXX stat */
  950                                 icmp6_error(m, ICMP6_PARAM_PROB,
  951                                     ICMP6_PARAMPROB_HEADER,
  952                                     erroff + opt + 1 - opthead);
  953                                 return (-1);
  954                         }
  955                         optlen = IP6OPT_RTALERT_LEN;
  956                         bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
  957                         *rtalertp = ntohs(rtalert_val);
  958                         break;
  959                 case IP6OPT_JUMBO:
  960                         /* XXX may need check for alignment */
  961                         if (hbhlen < IP6OPT_JUMBO_LEN) {
  962                                 ip6stat.ip6s_toosmall++;
  963                                 goto bad;
  964                         }
  965                         if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
  966                                 /* XXX stat */
  967                                 icmp6_error(m, ICMP6_PARAM_PROB,
  968                                     ICMP6_PARAMPROB_HEADER,
  969                                     erroff + opt + 1 - opthead);
  970                                 return (-1);
  971                         }
  972                         optlen = IP6OPT_JUMBO_LEN;
  973 
  974                         /*
  975                          * IPv6 packets that have non 0 payload length
  976                          * must not contain a jumbo payload option.
  977                          */
  978                         ip6 = mtod(m, struct ip6_hdr *);
  979                         if (ip6->ip6_plen) {
  980                                 ip6stat.ip6s_badoptions++;
  981                                 icmp6_error(m, ICMP6_PARAM_PROB,
  982                                     ICMP6_PARAMPROB_HEADER,
  983                                     erroff + opt - opthead);
  984                                 return (-1);
  985                         }
  986 
  987                         /*
  988                          * We may see jumbolen in unaligned location, so
  989                          * we'd need to perform bcopy().
  990                          */
  991                         bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
  992                         jumboplen = (u_int32_t)htonl(jumboplen);
  993 
  994 #if 1
  995                         /*
  996                          * if there are multiple jumbo payload options,
  997                          * *plenp will be non-zero and the packet will be
  998                          * rejected.
  999                          * the behavior may need some debate in ipngwg -
 1000                          * multiple options does not make sense, however,
 1001                          * there's no explicit mention in specification.
 1002                          */
 1003                         if (*plenp != 0) {
 1004                                 ip6stat.ip6s_badoptions++;
 1005                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1006                                     ICMP6_PARAMPROB_HEADER,
 1007                                     erroff + opt + 2 - opthead);
 1008                                 return (-1);
 1009                         }
 1010 #endif
 1011 
 1012                         /*
 1013                          * jumbo payload length must be larger than 65535.
 1014                          */
 1015                         if (jumboplen <= IPV6_MAXPACKET) {
 1016                                 ip6stat.ip6s_badoptions++;
 1017                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1018                                     ICMP6_PARAMPROB_HEADER,
 1019                                     erroff + opt + 2 - opthead);
 1020                                 return (-1);
 1021                         }
 1022                         *plenp = jumboplen;
 1023 
 1024                         break;
 1025                 default:                /* unknown option */
 1026                         if (hbhlen < IP6OPT_MINLEN) {
 1027                                 ip6stat.ip6s_toosmall++;
 1028                                 goto bad;
 1029                         }
 1030                         optlen = ip6_unknown_opt(opt, m,
 1031                             erroff + opt - opthead);
 1032                         if (optlen == -1)
 1033                                 return (-1);
 1034                         optlen += 2;
 1035                         break;
 1036                 }
 1037         }
 1038 
 1039         return (0);
 1040 
 1041   bad:
 1042         m_freem(m);
 1043         return (-1);
 1044 }
 1045 
 1046 /*
 1047  * Unknown option processing.
 1048  * The third argument `off' is the offset from the IPv6 header to the option,
 1049  * which is necessary if the IPv6 header the and option header and IPv6 header
 1050  * is not continuous in order to return an ICMPv6 error.
 1051  */
 1052 int
 1053 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
 1054 {
 1055         struct ip6_hdr *ip6;
 1056 
 1057         switch (IP6OPT_TYPE(*optp)) {
 1058         case IP6OPT_TYPE_SKIP: /* ignore the option */
 1059                 return ((int)*(optp + 1));
 1060         case IP6OPT_TYPE_DISCARD:       /* silently discard */
 1061                 m_freem(m);
 1062                 return (-1);
 1063         case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
 1064                 ip6stat.ip6s_badoptions++;
 1065                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
 1066                 return (-1);
 1067         case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
 1068                 ip6stat.ip6s_badoptions++;
 1069                 ip6 = mtod(m, struct ip6_hdr *);
 1070                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
 1071                     (m->m_flags & (M_BCAST|M_MCAST)))
 1072                         m_freem(m);
 1073                 else
 1074                         icmp6_error(m, ICMP6_PARAM_PROB,
 1075                                     ICMP6_PARAMPROB_OPTION, off);
 1076                 return (-1);
 1077         }
 1078 
 1079         m_freem(m);             /* XXX: NOTREACHED */
 1080         return (-1);
 1081 }
 1082 
 1083 /*
 1084  * Create the "control" list for this pcb.
 1085  * These functions will not modify mbuf chain at all.
 1086  *
 1087  * With KAME mbuf chain restriction:
 1088  * The routine will be called from upper layer handlers like tcp6_input().
 1089  * Thus the routine assumes that the caller (tcp6_input) have already
 1090  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
 1091  * very first mbuf on the mbuf chain.
 1092  *
 1093  * ip6_savecontrol_v4 will handle those options that are possible to be
 1094  * set on a v4-mapped socket.
 1095  * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
 1096  * options and handle the v6-only ones itself.
 1097  */
 1098 struct mbuf **
 1099 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
 1100     int *v4only)
 1101 {
 1102         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1103 
 1104 #ifdef SO_TIMESTAMP
 1105         if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
 1106                 struct timeval tv;
 1107 
 1108                 microtime(&tv);
 1109                 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
 1110                     SCM_TIMESTAMP, SOL_SOCKET);
 1111                 if (*mp)
 1112                         mp = &(*mp)->m_next;
 1113         }
 1114 #endif
 1115 
 1116         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 1117                 if (v4only != NULL)
 1118                         *v4only = 1;
 1119                 return (mp);
 1120         }
 1121 
 1122 #define IS2292(inp, x, y)       (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
 1123         /* RFC 2292 sec. 5 */
 1124         if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
 1125                 struct in6_pktinfo pi6;
 1126 
 1127                 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
 1128                 in6_clearscope(&pi6.ipi6_addr); /* XXX */
 1129                 pi6.ipi6_ifindex =
 1130                     (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
 1131 
 1132                 *mp = sbcreatecontrol((caddr_t) &pi6,
 1133                     sizeof(struct in6_pktinfo),
 1134                     IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
 1135                 if (*mp)
 1136                         mp = &(*mp)->m_next;
 1137         }
 1138 
 1139         if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
 1140                 int hlim = ip6->ip6_hlim & 0xff;
 1141 
 1142                 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
 1143                     IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
 1144                     IPPROTO_IPV6);
 1145                 if (*mp)
 1146                         mp = &(*mp)->m_next;
 1147         }
 1148 
 1149         if (v4only != NULL)
 1150                 *v4only = 0;
 1151         return (mp);
 1152 }
 1153 
 1154 void
 1155 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
 1156 {
 1157         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1158         int v4only = 0;
 1159 
 1160         mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
 1161         if (v4only)
 1162                 return;
 1163 
 1164         if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
 1165                 u_int32_t flowinfo;
 1166                 int tclass;
 1167 
 1168                 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
 1169                 flowinfo >>= 20;
 1170 
 1171                 tclass = flowinfo & 0xff;
 1172                 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
 1173                     IPV6_TCLASS, IPPROTO_IPV6);
 1174                 if (*mp)
 1175                         mp = &(*mp)->m_next;
 1176         }
 1177 
 1178         /*
 1179          * IPV6_HOPOPTS socket option.  Recall that we required super-user
 1180          * privilege for the option (see ip6_ctloutput), but it might be too
 1181          * strict, since there might be some hop-by-hop options which can be
 1182          * returned to normal user.
 1183          * See also RFC 2292 section 6 (or RFC 3542 section 8).
 1184          */
 1185         if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
 1186                 /*
 1187                  * Check if a hop-by-hop options header is contatined in the
 1188                  * received packet, and if so, store the options as ancillary
 1189                  * data. Note that a hop-by-hop options header must be
 1190                  * just after the IPv6 header, which is assured through the
 1191                  * IPv6 input processing.
 1192                  */
 1193                 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
 1194                         struct ip6_hbh *hbh;
 1195                         int hbhlen = 0;
 1196 #ifdef PULLDOWN_TEST
 1197                         struct mbuf *ext;
 1198 #endif
 1199 
 1200 #ifndef PULLDOWN_TEST
 1201                         hbh = (struct ip6_hbh *)(ip6 + 1);
 1202                         hbhlen = (hbh->ip6h_len + 1) << 3;
 1203 #else
 1204                         ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
 1205                             ip6->ip6_nxt);
 1206                         if (ext == NULL) {
 1207                                 ip6stat.ip6s_tooshort++;
 1208                                 return;
 1209                         }
 1210                         hbh = mtod(ext, struct ip6_hbh *);
 1211                         hbhlen = (hbh->ip6h_len + 1) << 3;
 1212                         if (hbhlen != ext->m_len) {
 1213                                 m_freem(ext);
 1214                                 ip6stat.ip6s_tooshort++;
 1215                                 return;
 1216                         }
 1217 #endif
 1218 
 1219                         /*
 1220                          * XXX: We copy the whole header even if a
 1221                          * jumbo payload option is included, the option which
 1222                          * is to be removed before returning according to
 1223                          * RFC2292.
 1224                          * Note: this constraint is removed in RFC3542
 1225                          */
 1226                         *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
 1227                             IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
 1228                             IPPROTO_IPV6);
 1229                         if (*mp)
 1230                                 mp = &(*mp)->m_next;
 1231 #ifdef PULLDOWN_TEST
 1232                         m_freem(ext);
 1233 #endif
 1234                 }
 1235         }
 1236 
 1237         if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
 1238                 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
 1239 
 1240                 /*
 1241                  * Search for destination options headers or routing
 1242                  * header(s) through the header chain, and stores each
 1243                  * header as ancillary data.
 1244                  * Note that the order of the headers remains in
 1245                  * the chain of ancillary data.
 1246                  */
 1247                 while (1) {     /* is explicit loop prevention necessary? */
 1248                         struct ip6_ext *ip6e = NULL;
 1249                         int elen;
 1250 #ifdef PULLDOWN_TEST
 1251                         struct mbuf *ext = NULL;
 1252 #endif
 1253 
 1254                         /*
 1255                          * if it is not an extension header, don't try to
 1256                          * pull it from the chain.
 1257                          */
 1258                         switch (nxt) {
 1259                         case IPPROTO_DSTOPTS:
 1260                         case IPPROTO_ROUTING:
 1261                         case IPPROTO_HOPOPTS:
 1262                         case IPPROTO_AH: /* is it possible? */
 1263                                 break;
 1264                         default:
 1265                                 goto loopend;
 1266                         }
 1267 
 1268 #ifndef PULLDOWN_TEST
 1269                         if (off + sizeof(*ip6e) > m->m_len)
 1270                                 goto loopend;
 1271                         ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
 1272                         if (nxt == IPPROTO_AH)
 1273                                 elen = (ip6e->ip6e_len + 2) << 2;
 1274                         else
 1275                                 elen = (ip6e->ip6e_len + 1) << 3;
 1276                         if (off + elen > m->m_len)
 1277                                 goto loopend;
 1278 #else
 1279                         ext = ip6_pullexthdr(m, off, nxt);
 1280                         if (ext == NULL) {
 1281                                 ip6stat.ip6s_tooshort++;
 1282                                 return;
 1283                         }
 1284                         ip6e = mtod(ext, struct ip6_ext *);
 1285                         if (nxt == IPPROTO_AH)
 1286                                 elen = (ip6e->ip6e_len + 2) << 2;
 1287                         else
 1288                                 elen = (ip6e->ip6e_len + 1) << 3;
 1289                         if (elen != ext->m_len) {
 1290                                 m_freem(ext);
 1291                                 ip6stat.ip6s_tooshort++;
 1292                                 return;
 1293                         }
 1294 #endif
 1295 
 1296                         switch (nxt) {
 1297                         case IPPROTO_DSTOPTS:
 1298                                 if (!(in6p->inp_flags & IN6P_DSTOPTS))
 1299                                         break;
 1300 
 1301                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
 1302                                     IS2292(in6p,
 1303                                         IPV6_2292DSTOPTS, IPV6_DSTOPTS),
 1304                                     IPPROTO_IPV6);
 1305                                 if (*mp)
 1306                                         mp = &(*mp)->m_next;
 1307                                 break;
 1308                         case IPPROTO_ROUTING:
 1309                                 if (!in6p->inp_flags & IN6P_RTHDR)
 1310                                         break;
 1311 
 1312                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
 1313                                     IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
 1314                                     IPPROTO_IPV6);
 1315                                 if (*mp)
 1316                                         mp = &(*mp)->m_next;
 1317                                 break;
 1318                         case IPPROTO_HOPOPTS:
 1319                         case IPPROTO_AH: /* is it possible? */
 1320                                 break;
 1321 
 1322                         default:
 1323                                 /*
 1324                                  * other cases have been filtered in the above.
 1325                                  * none will visit this case.  here we supply
 1326                                  * the code just in case (nxt overwritten or
 1327                                  * other cases).
 1328                                  */
 1329 #ifdef PULLDOWN_TEST
 1330                                 m_freem(ext);
 1331 #endif
 1332                                 goto loopend;
 1333 
 1334                         }
 1335 
 1336                         /* proceed with the next header. */
 1337                         off += elen;
 1338                         nxt = ip6e->ip6e_nxt;
 1339                         ip6e = NULL;
 1340 #ifdef PULLDOWN_TEST
 1341                         m_freem(ext);
 1342                         ext = NULL;
 1343 #endif
 1344                 }
 1345           loopend:
 1346                 ;
 1347         }
 1348 }
 1349 #undef IS2292
 1350 
 1351 void
 1352 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
 1353 {
 1354         struct socket *so;
 1355         struct mbuf *m_mtu;
 1356         struct ip6_mtuinfo mtuctl;
 1357 
 1358         so =  in6p->inp_socket;
 1359 
 1360         if (mtu == NULL)
 1361                 return;
 1362 
 1363 #ifdef DIAGNOSTIC
 1364         if (so == NULL)         /* I believe this is impossible */
 1365                 panic("ip6_notify_pmtu: socket is NULL");
 1366 #endif
 1367 
 1368         bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */
 1369         mtuctl.ip6m_mtu = *mtu;
 1370         mtuctl.ip6m_addr = *dst;
 1371         if (sa6_recoverscope(&mtuctl.ip6m_addr))
 1372                 return;
 1373 
 1374         if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
 1375             IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
 1376                 return;
 1377 
 1378         if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
 1379             == 0) {
 1380                 m_freem(m_mtu);
 1381                 /* XXX: should count statistics */
 1382         } else
 1383                 sorwakeup(so);
 1384 
 1385         return;
 1386 }
 1387 
 1388 #ifdef PULLDOWN_TEST
 1389 /*
 1390  * pull single extension header from mbuf chain.  returns single mbuf that
 1391  * contains the result, or NULL on error.
 1392  */
 1393 static struct mbuf *
 1394 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
 1395 {
 1396         struct ip6_ext ip6e;
 1397         size_t elen;
 1398         struct mbuf *n;
 1399 
 1400 #ifdef DIAGNOSTIC
 1401         switch (nxt) {
 1402         case IPPROTO_DSTOPTS:
 1403         case IPPROTO_ROUTING:
 1404         case IPPROTO_HOPOPTS:
 1405         case IPPROTO_AH: /* is it possible? */
 1406                 break;
 1407         default:
 1408                 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
 1409         }
 1410 #endif
 1411 
 1412         m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
 1413         if (nxt == IPPROTO_AH)
 1414                 elen = (ip6e.ip6e_len + 2) << 2;
 1415         else
 1416                 elen = (ip6e.ip6e_len + 1) << 3;
 1417 
 1418         MGET(n, M_DONTWAIT, MT_DATA);
 1419         if (n && elen >= MLEN) {
 1420                 MCLGET(n, M_DONTWAIT);
 1421                 if ((n->m_flags & M_EXT) == 0) {
 1422                         m_free(n);
 1423                         n = NULL;
 1424                 }
 1425         }
 1426         if (!n)
 1427                 return NULL;
 1428 
 1429         n->m_len = 0;
 1430         if (elen >= M_TRAILINGSPACE(n)) {
 1431                 m_free(n);
 1432                 return NULL;
 1433         }
 1434 
 1435         m_copydata(m, off, elen, mtod(n, caddr_t));
 1436         n->m_len = elen;
 1437         return n;
 1438 }
 1439 #endif
 1440 
 1441 /*
 1442  * Get pointer to the previous header followed by the header
 1443  * currently processed.
 1444  * XXX: This function supposes that
 1445  *      M includes all headers,
 1446  *      the next header field and the header length field of each header
 1447  *      are valid, and
 1448  *      the sum of each header length equals to OFF.
 1449  * Because of these assumptions, this function must be called very
 1450  * carefully. Moreover, it will not be used in the near future when
 1451  * we develop `neater' mechanism to process extension headers.
 1452  */
 1453 char *
 1454 ip6_get_prevhdr(struct mbuf *m, int off)
 1455 {
 1456         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1457 
 1458         if (off == sizeof(struct ip6_hdr))
 1459                 return (&ip6->ip6_nxt);
 1460         else {
 1461                 int len, nxt;
 1462                 struct ip6_ext *ip6e = NULL;
 1463 
 1464                 nxt = ip6->ip6_nxt;
 1465                 len = sizeof(struct ip6_hdr);
 1466                 while (len < off) {
 1467                         ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
 1468 
 1469                         switch (nxt) {
 1470                         case IPPROTO_FRAGMENT:
 1471                                 len += sizeof(struct ip6_frag);
 1472                                 break;
 1473                         case IPPROTO_AH:
 1474                                 len += (ip6e->ip6e_len + 2) << 2;
 1475                                 break;
 1476                         default:
 1477                                 len += (ip6e->ip6e_len + 1) << 3;
 1478                                 break;
 1479                         }
 1480                         nxt = ip6e->ip6e_nxt;
 1481                 }
 1482                 if (ip6e)
 1483                         return (&ip6e->ip6e_nxt);
 1484                 else
 1485                         return NULL;
 1486         }
 1487 }
 1488 
 1489 /*
 1490  * get next header offset.  m will be retained.
 1491  */
 1492 int
 1493 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
 1494 {
 1495         struct ip6_hdr ip6;
 1496         struct ip6_ext ip6e;
 1497         struct ip6_frag fh;
 1498 
 1499         /* just in case */
 1500         if (m == NULL)
 1501                 panic("ip6_nexthdr: m == NULL");
 1502         if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
 1503                 return -1;
 1504 
 1505         switch (proto) {
 1506         case IPPROTO_IPV6:
 1507                 if (m->m_pkthdr.len < off + sizeof(ip6))
 1508                         return -1;
 1509                 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
 1510                 if (nxtp)
 1511                         *nxtp = ip6.ip6_nxt;
 1512                 off += sizeof(ip6);
 1513                 return off;
 1514 
 1515         case IPPROTO_FRAGMENT:
 1516                 /*
 1517                  * terminate parsing if it is not the first fragment,
 1518                  * it does not make sense to parse through it.
 1519                  */
 1520                 if (m->m_pkthdr.len < off + sizeof(fh))
 1521                         return -1;
 1522                 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
 1523                 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
 1524                 if (fh.ip6f_offlg & IP6F_OFF_MASK)
 1525                         return -1;
 1526                 if (nxtp)
 1527                         *nxtp = fh.ip6f_nxt;
 1528                 off += sizeof(struct ip6_frag);
 1529                 return off;
 1530 
 1531         case IPPROTO_AH:
 1532                 if (m->m_pkthdr.len < off + sizeof(ip6e))
 1533                         return -1;
 1534                 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
 1535                 if (nxtp)
 1536                         *nxtp = ip6e.ip6e_nxt;
 1537                 off += (ip6e.ip6e_len + 2) << 2;
 1538                 return off;
 1539 
 1540         case IPPROTO_HOPOPTS:
 1541         case IPPROTO_ROUTING:
 1542         case IPPROTO_DSTOPTS:
 1543                 if (m->m_pkthdr.len < off + sizeof(ip6e))
 1544                         return -1;
 1545                 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
 1546                 if (nxtp)
 1547                         *nxtp = ip6e.ip6e_nxt;
 1548                 off += (ip6e.ip6e_len + 1) << 3;
 1549                 return off;
 1550 
 1551         case IPPROTO_NONE:
 1552         case IPPROTO_ESP:
 1553         case IPPROTO_IPCOMP:
 1554                 /* give up */
 1555                 return -1;
 1556 
 1557         default:
 1558                 return -1;
 1559         }
 1560 
 1561         return -1;
 1562 }
 1563 
 1564 /*
 1565  * get offset for the last header in the chain.  m will be kept untainted.
 1566  */
 1567 int
 1568 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
 1569 {
 1570         int newoff;
 1571         int nxt;
 1572 
 1573         if (!nxtp) {
 1574                 nxt = -1;
 1575                 nxtp = &nxt;
 1576         }
 1577         while (1) {
 1578                 newoff = ip6_nexthdr(m, off, proto, nxtp);
 1579                 if (newoff < 0)
 1580                         return off;
 1581                 else if (newoff < off)
 1582                         return -1;      /* invalid */
 1583                 else if (newoff == off)
 1584                         return newoff;
 1585 
 1586                 off = newoff;
 1587                 proto = *nxtp;
 1588         }
 1589 }
 1590 
 1591 struct ip6aux *
 1592 ip6_addaux(struct mbuf *m)
 1593 {
 1594         struct m_tag *mtag;
 1595 
 1596         mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
 1597         if (!mtag) {
 1598                 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
 1599                     M_NOWAIT);
 1600                 if (mtag) {
 1601                         m_tag_prepend(m, mtag);
 1602                         bzero(mtag + 1, sizeof(struct ip6aux));
 1603                 }
 1604         }
 1605         return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
 1606 }
 1607 
 1608 struct ip6aux *
 1609 ip6_findaux(struct mbuf *m)
 1610 {
 1611         struct m_tag *mtag;
 1612 
 1613         mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
 1614         return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
 1615 }
 1616 
 1617 void
 1618 ip6_delaux(struct mbuf *m)
 1619 {
 1620         struct m_tag *mtag;
 1621 
 1622         mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
 1623         if (mtag)
 1624                 m_tag_delete(m, mtag);
 1625 }
 1626 
 1627 /*
 1628  * System control for IP6
 1629  */
 1630 
 1631 u_char  inet6ctlerrmap[PRC_NCMDS] = {
 1632         0,              0,              0,              0,
 1633         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
 1634         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
 1635         EMSGSIZE,       EHOSTUNREACH,   0,              0,
 1636         0,              0,              0,              0,
 1637         ENOPROTOOPT
 1638 };

Cache object: 139c1b672dcde956e087b3a18ac9c5b8


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