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

Cache object: a93e33fbd068e6a8cd7c1ad3bad428b3


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