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 /*      $OpenBSD: ip6_input.c,v 1.254 2022/08/21 14:15:55 bluhm Exp $   */
    2 /*      $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun 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  * 3. 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 "pf.h"
   65 #include "carp.h"
   66 
   67 #include <sys/param.h>
   68 #include <sys/systm.h>
   69 #include <sys/mbuf.h>
   70 #include <sys/domain.h>
   71 #include <sys/sysctl.h>
   72 #include <sys/protosw.h>
   73 #include <sys/socket.h>
   74 #include <sys/socketvar.h>
   75 #include <sys/errno.h>
   76 #include <sys/time.h>
   77 #include <sys/timeout.h>
   78 #include <sys/kernel.h>
   79 #include <sys/syslog.h>
   80 #include <sys/task.h>
   81 
   82 #include <net/if.h>
   83 #include <net/if_var.h>
   84 #include <net/if_types.h>
   85 #include <net/route.h>
   86 #include <net/netisr.h>
   87 
   88 #include <netinet/in.h>
   89 
   90 #include <netinet/ip.h>
   91 
   92 #include <netinet/in_pcb.h>
   93 #include <netinet/ip_var.h>
   94 #include <netinet6/in6_var.h>
   95 #include <netinet6/in6_ifattach.h>
   96 #include <netinet/ip6.h>
   97 #include <netinet6/ip6_var.h>
   98 #include <netinet/icmp6.h>
   99 #include <netinet6/nd6.h>
  100 
  101 #include "gif.h"
  102 #include "bpfilter.h"
  103 
  104 #ifdef MROUTING
  105 #include <netinet6/ip6_mroute.h>
  106 #endif
  107 
  108 #if NPF > 0
  109 #include <net/pfvar.h>
  110 #endif
  111 
  112 #if NCARP > 0
  113 #include <netinet/ip_carp.h>
  114 #endif
  115 
  116 struct niqueue ip6intrq = NIQUEUE_INITIALIZER(IPQ_MAXLEN, NETISR_IPV6);
  117 
  118 struct cpumem *ip6counters;
  119 
  120 uint8_t ip6_soiikey[IP6_SOIIKEY_LEN];
  121 
  122 int ip6_ours(struct mbuf **, int *, int, int);
  123 int ip6_check_rh0hdr(struct mbuf *, int *);
  124 int ip6_hbhchcheck(struct mbuf **, int *, int *);
  125 int ip6_hopopts_input(struct mbuf **, int *, u_int32_t *, u_int32_t *);
  126 struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
  127 int ip6_sysctl_soiikey(void *, size_t *, void *, size_t);
  128 
  129 static struct mbuf_queue        ip6send_mq;
  130 
  131 static void ip6_send_dispatch(void *);
  132 static struct task ip6send_task =
  133         TASK_INITIALIZER(ip6_send_dispatch, &ip6send_mq);
  134 
  135 /*
  136  * IP6 initialization: fill in IP6 protocol switch table.
  137  * All protocols not implemented in kernel go to raw IP6 protocol handler.
  138  */
  139 void
  140 ip6_init(void)
  141 {
  142         const struct protosw *pr;
  143         int i;
  144 
  145         pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  146         if (pr == NULL)
  147                 panic("%s", __func__);
  148         for (i = 0; i < IPPROTO_MAX; i++)
  149                 ip6_protox[i] = pr - inet6sw;
  150         for (pr = inet6domain.dom_protosw;
  151             pr < inet6domain.dom_protoswNPROTOSW; pr++)
  152                 if (pr->pr_domain->dom_family == PF_INET6 &&
  153                     pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW &&
  154                     pr->pr_protocol < IPPROTO_MAX)
  155                         ip6_protox[pr->pr_protocol] = pr - inet6sw;
  156         ip6_randomid_init();
  157         nd6_init();
  158         frag6_init();
  159 
  160         mq_init(&ip6send_mq, 64, IPL_SOFTNET);
  161 
  162         ip6counters = counters_alloc(ip6s_ncounters);
  163 #ifdef MROUTING
  164         rt_timer_queue_init(&ip6_mrouterq, MCAST_EXPIRE_TIMEOUT,
  165             &mf6c_expire_route);
  166 #endif
  167 }
  168 
  169 struct ip6_offnxt {
  170         int     ion_off;
  171         int     ion_nxt;
  172 };
  173 
  174 /*
  175  * Enqueue packet for local delivery.  Queuing is used as a boundary
  176  * between the network layer (input/forward path) running with
  177  * NET_LOCK_SHARED() and the transport layer needing it exclusively.
  178  */
  179 int
  180 ip6_ours(struct mbuf **mp, int *offp, int nxt, int af)
  181 {
  182         /* ip6_hbhchcheck() may be run before, then off and nxt are set */
  183         if (*offp == 0) {
  184                 nxt = ip6_hbhchcheck(mp, offp, NULL);
  185                 if (nxt == IPPROTO_DONE)
  186                         return IPPROTO_DONE;
  187         }
  188 
  189         /* We are already in a IPv4/IPv6 local deliver loop. */
  190         if (af != AF_UNSPEC)
  191                 return nxt;
  192 
  193         /* save values for later, use after dequeue */
  194         if (*offp != sizeof(struct ip6_hdr)) {
  195                 struct m_tag *mtag;
  196                 struct ip6_offnxt *ion;
  197 
  198                 /* mbuf tags are expensive, but only used for header options */
  199                 mtag = m_tag_get(PACKET_TAG_IP6_OFFNXT, sizeof(*ion),
  200                     M_NOWAIT);
  201                 if (mtag == NULL) {
  202                         ip6stat_inc(ip6s_idropped);
  203                         m_freemp(mp);
  204                         return IPPROTO_DONE;
  205                 }
  206                 ion = (struct ip6_offnxt *)(mtag + 1);
  207                 ion->ion_off = *offp;
  208                 ion->ion_nxt = nxt;
  209 
  210                 m_tag_prepend(*mp, mtag);
  211         }
  212 
  213         niq_enqueue(&ip6intrq, *mp);
  214         *mp = NULL;
  215         return IPPROTO_DONE;
  216 }
  217 
  218 /*
  219  * Dequeue and process locally delivered packets.
  220  * This is called with exclusive NET_LOCK().
  221  */
  222 void
  223 ip6intr(void)
  224 {
  225         struct mbuf *m;
  226 
  227         while ((m = niq_dequeue(&ip6intrq)) != NULL) {
  228                 struct m_tag *mtag;
  229                 int off, nxt;
  230 
  231 #ifdef DIAGNOSTIC
  232                 if ((m->m_flags & M_PKTHDR) == 0)
  233                         panic("ip6intr no HDR");
  234 #endif
  235                 mtag = m_tag_find(m, PACKET_TAG_IP6_OFFNXT, NULL);
  236                 if (mtag != NULL) {
  237                         struct ip6_offnxt *ion;
  238 
  239                         ion = (struct ip6_offnxt *)(mtag + 1);
  240                         off = ion->ion_off;
  241                         nxt = ion->ion_nxt;
  242 
  243                         m_tag_delete(m, mtag);
  244                 } else {
  245                         struct ip6_hdr *ip6;
  246 
  247                         ip6 = mtod(m, struct ip6_hdr *);
  248                         off = sizeof(struct ip6_hdr);
  249                         nxt = ip6->ip6_nxt;
  250                 }
  251                 nxt = ip_deliver(&m, &off, nxt, AF_INET6);
  252                 KASSERT(nxt == IPPROTO_DONE);
  253         }
  254 }
  255 
  256 void
  257 ipv6_input(struct ifnet *ifp, struct mbuf *m)
  258 {
  259         int off, nxt;
  260 
  261         off = 0;
  262         nxt = ip6_input_if(&m, &off, IPPROTO_IPV6, AF_UNSPEC, ifp);
  263         KASSERT(nxt == IPPROTO_DONE);
  264 }
  265 
  266 struct mbuf *
  267 ipv6_check(struct ifnet *ifp, struct mbuf *m)
  268 {
  269         struct ip6_hdr *ip6;
  270 
  271         if (m->m_len < sizeof(*ip6)) {
  272                 m = m_pullup(m, sizeof(*ip6));
  273                 if (m == NULL) {
  274                         ip6stat_inc(ip6s_toosmall);
  275                         return (NULL);
  276                 }
  277         }
  278 
  279         ip6 = mtod(m, struct ip6_hdr *);
  280 
  281         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
  282                 ip6stat_inc(ip6s_badvers);
  283                 goto bad;
  284         }
  285 
  286         /*
  287          * Check against address spoofing/corruption.
  288          */
  289         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
  290             IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
  291                 /*
  292                  * XXX: "badscope" is not very suitable for a multicast source.
  293                  */
  294                 ip6stat_inc(ip6s_badscope);
  295                 goto bad;
  296         }
  297         if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
  298             IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
  299             (ifp->if_flags & IFF_LOOPBACK) == 0) {
  300                 ip6stat_inc(ip6s_badscope);
  301                 goto bad;
  302         }
  303         /* Drop packets if interface ID portion is already filled. */
  304         if (((IN6_IS_SCOPE_EMBED(&ip6->ip6_src) && ip6->ip6_src.s6_addr16[1]) ||
  305             (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) && ip6->ip6_dst.s6_addr16[1])) &&
  306             (ifp->if_flags & IFF_LOOPBACK) == 0) {
  307                 ip6stat_inc(ip6s_badscope);
  308                 goto bad;
  309         }
  310         if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
  311             !(m->m_flags & M_LOOP)) {
  312                 /*
  313                  * In this case, the packet should come from the loopback
  314                  * interface.  However, we cannot just check the if_flags,
  315                  * because ip6_mloopback() passes the "actual" interface
  316                  * as the outgoing/incoming interface.
  317                  */
  318                 ip6stat_inc(ip6s_badscope);
  319                 goto bad;
  320         }
  321 
  322         /*
  323          * The following check is not documented in specs.  A malicious
  324          * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
  325          * and bypass security checks (act as if it was from 127.0.0.1 by using
  326          * IPv6 src ::ffff:127.0.0.1).  Be cautious.
  327          *
  328          * This check chokes if we are in an SIIT cloud.  As none of BSDs
  329          * support IPv4-less kernel compilation, we cannot support SIIT
  330          * environment at all.  So, it makes more sense for us to reject any
  331          * malicious packets for non-SIIT environment, than try to do a
  332          * partial support for SIIT environment.
  333          */
  334         if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
  335             IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
  336                 ip6stat_inc(ip6s_badscope);
  337                 goto bad;
  338         }
  339 
  340         /*
  341          * Reject packets with IPv4 compatible addresses (auto tunnel).
  342          *
  343          * The code forbids automatic tunneling as per RFC4213.
  344          */
  345         if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
  346             IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
  347                 ip6stat_inc(ip6s_badscope);
  348                 goto bad;
  349         }
  350 
  351         return (m);
  352 bad:
  353         m_freem(m);
  354         return (NULL);
  355 }
  356 
  357 int
  358 ip6_input_if(struct mbuf **mp, int *offp, int nxt, int af, struct ifnet *ifp)
  359 {
  360         struct mbuf *m;
  361         struct ip6_hdr *ip6;
  362         struct sockaddr_in6 sin6;
  363         struct rtentry *rt = NULL;
  364         int ours = 0;
  365         u_int16_t src_scope, dst_scope;
  366 #if NPF > 0
  367         struct in6_addr odst;
  368 #endif
  369         int srcrt = 0;
  370 
  371         KASSERT(*offp == 0);
  372 
  373         ip6stat_inc(ip6s_total);
  374 
  375         m = *mp = ipv6_check(ifp, *mp);
  376         if (m == NULL)
  377                 goto bad;
  378 
  379         ip6 = mtod(m, struct ip6_hdr *);
  380 
  381 #if NCARP > 0
  382         if (carp_lsdrop(ifp, m, AF_INET6, ip6->ip6_src.s6_addr32,
  383             ip6->ip6_dst.s6_addr32, (ip6->ip6_nxt == IPPROTO_ICMPV6 ? 0 : 1)))
  384                 goto bad;
  385 #endif
  386         ip6stat_inc(ip6s_nxthist + ip6->ip6_nxt);
  387 
  388         /*
  389          * If the packet has been received on a loopback interface it
  390          * can be destined to any local address, not necessarily to
  391          * an address configured on `ifp'.
  392          */
  393         if (ifp->if_flags & IFF_LOOPBACK) {
  394                 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
  395                         src_scope = ip6->ip6_src.s6_addr16[1];
  396                         ip6->ip6_src.s6_addr16[1] = 0;
  397                 }
  398                 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
  399                         dst_scope = ip6->ip6_dst.s6_addr16[1];
  400                         ip6->ip6_dst.s6_addr16[1] = 0;
  401                 }
  402         }
  403 
  404 #if NPF > 0
  405         /*
  406          * Packet filter
  407          */
  408         odst = ip6->ip6_dst;
  409         if (pf_test(AF_INET6, PF_IN, ifp, mp) != PF_PASS)
  410                 goto bad;
  411         m = *mp;
  412         if (m == NULL)
  413                 goto bad;
  414 
  415         ip6 = mtod(m, struct ip6_hdr *);
  416         srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
  417 #endif
  418 
  419         /*
  420          * Without embedded scope ID we cannot find link-local
  421          * addresses in the routing table.
  422          */
  423         if (ifp->if_flags & IFF_LOOPBACK) {
  424                 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  425                         ip6->ip6_src.s6_addr16[1] = src_scope;
  426                 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  427                         ip6->ip6_dst.s6_addr16[1] = dst_scope;
  428         } else {
  429                 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  430                         ip6->ip6_src.s6_addr16[1] = htons(ifp->if_index);
  431                 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  432                         ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
  433         }
  434 
  435         /*
  436          * Be more secure than RFC5095 and scan for type 0 routing headers.
  437          * If pf has already scanned the header chain, do not do it twice.
  438          */
  439         if (!(m->m_pkthdr.pf.flags & PF_TAG_PROCESSED) &&
  440             ip6_check_rh0hdr(m, offp)) {
  441                 ip6stat_inc(ip6s_badoptions);
  442                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, *offp);
  443                 m = *mp = NULL;
  444                 goto bad;
  445         }
  446 
  447 #if NPF > 0
  448         if (pf_ouraddr(m) == 1) {
  449                 nxt = ip6_ours(mp, offp, nxt, af);
  450                 goto out;
  451         }
  452 #endif
  453 
  454         /*
  455          * Multicast check
  456          */
  457         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  458                 /*
  459                  * Make sure M_MCAST is set.  It should theoretically
  460                  * already be there, but let's play safe because upper
  461                  * layers check for this flag.
  462                  */
  463                 m->m_flags |= M_MCAST;
  464 
  465                 /*
  466                  * See if we belong to the destination multicast group on the
  467                  * arrival interface.
  468                  */
  469                 if (in6_hasmulti(&ip6->ip6_dst, ifp))
  470                         ours = 1;
  471 
  472 #ifdef MROUTING
  473                 if (ip6_mforwarding && ip6_mrouter[ifp->if_rdomain]) {
  474                         int error;
  475 
  476                         nxt = ip6_hbhchcheck(&m, offp, &ours);
  477                         if (nxt == IPPROTO_DONE)
  478                                 goto out;
  479 
  480                         ip6 = mtod(m, struct ip6_hdr *);
  481 
  482                         /*
  483                          * If we are acting as a multicast router, all
  484                          * incoming multicast packets are passed to the
  485                          * kernel-level multicast forwarding function.
  486                          * The packet is returned (relatively) intact; if
  487                          * ip6_mforward() returns a non-zero value, the packet
  488                          * must be discarded, else it may be accepted below.
  489                          */
  490                         KERNEL_LOCK();
  491                         error = ip6_mforward(ip6, ifp, m);
  492                         KERNEL_UNLOCK();
  493                         if (error) {
  494                                 ip6stat_inc(ip6s_cantforward);
  495                                 goto bad;
  496                         }
  497 
  498                         if (ours) {
  499                                 if (af == AF_UNSPEC)
  500                                         nxt = ip6_ours(mp, offp, nxt, af);
  501                                 goto out;
  502                         }
  503                         goto bad;
  504                 }
  505 #endif
  506                 if (!ours) {
  507                         ip6stat_inc(ip6s_notmember);
  508                         if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
  509                                 ip6stat_inc(ip6s_cantforward);
  510                         goto bad;
  511                 }
  512                 nxt = ip6_ours(mp, offp, nxt, af);
  513                 goto out;
  514         }
  515 
  516 
  517         /*
  518          *  Unicast check
  519          */
  520         memset(&sin6, 0, sizeof(struct sockaddr_in6));
  521         sin6.sin6_len = sizeof(struct sockaddr_in6);
  522         sin6.sin6_family = AF_INET6;
  523         sin6.sin6_addr = ip6->ip6_dst;
  524         rt = rtalloc_mpath(sin6tosa(&sin6), &ip6->ip6_src.s6_addr32[0],
  525             m->m_pkthdr.ph_rtableid);
  526 
  527         /*
  528          * Accept the packet if the route to the destination is marked
  529          * as local.
  530          */
  531         if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_LOCAL)) {
  532                 struct in6_ifaddr *ia6 = ifatoia6(rt->rt_ifa);
  533 
  534                 if (ip6_forwarding == 0 && rt->rt_ifidx != ifp->if_index &&
  535                     !((ifp->if_flags & IFF_LOOPBACK) ||
  536                     (ifp->if_type == IFT_ENC) ||
  537                     (m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST))) {
  538                         /* received on wrong interface */
  539 #if NCARP > 0
  540                         struct ifnet *out_if;
  541 
  542                         /*
  543                          * Virtual IPs on carp interfaces need to be checked
  544                          * also against the parent interface and other carp
  545                          * interfaces sharing the same parent.
  546                          */
  547                         out_if = if_get(rt->rt_ifidx);
  548                         if (!(out_if && carp_strict_addr_chk(out_if, ifp))) {
  549                                 ip6stat_inc(ip6s_wrongif);
  550                                 if_put(out_if);
  551                                 goto bad;
  552                         }
  553                         if_put(out_if);
  554 #else
  555                         ip6stat_inc(ip6s_wrongif);
  556                         goto bad;
  557 #endif
  558                 }
  559                 /*
  560                  * packets to a tentative, duplicated, or somehow invalid
  561                  * address must not be accepted.
  562                  */
  563                 if ((ia6->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED))) {
  564                         char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
  565 
  566                         inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src));
  567                         inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst));
  568                         /* address is not ready, so discard the packet. */
  569                         nd6log((LOG_INFO,
  570                             "%s: packet to an unready address %s->%s\n",
  571                             __func__, src, dst));
  572 
  573                         goto bad;
  574                 } else {
  575                         nxt = ip6_ours(mp, offp, nxt, af);
  576                         goto out;
  577                 }
  578         }
  579 
  580 #if NCARP > 0
  581         if (ip6->ip6_nxt == IPPROTO_ICMPV6 &&
  582             carp_lsdrop(ifp, m, AF_INET6, ip6->ip6_src.s6_addr32,
  583             ip6->ip6_dst.s6_addr32, 1))
  584                 goto bad;
  585 #endif
  586         /*
  587          * Now there is no reason to process the packet if it's not our own
  588          * and we're not a router.
  589          */
  590         if (!ip6_forwarding) {
  591                 ip6stat_inc(ip6s_cantforward);
  592                 goto bad;
  593         }
  594 
  595         nxt = ip6_hbhchcheck(&m, offp, &ours);
  596         if (nxt == IPPROTO_DONE)
  597                 goto out;
  598 
  599         if (ours) {
  600                 if (af == AF_UNSPEC)
  601                         nxt = ip6_ours(mp, offp, nxt, af);
  602                 goto out;
  603         }
  604 
  605 #ifdef IPSEC
  606         if (ipsec_in_use) {
  607                 int rv;
  608 
  609                 rv = ipsec_forward_check(m, *offp, AF_INET6);
  610                 if (rv != 0) {
  611                         ip6stat_inc(ip6s_cantforward);
  612                         goto bad;
  613                 }
  614                 /*
  615                  * Fall through, forward packet. Outbound IPsec policy
  616                  * checking will occur in ip6_forward().
  617                  */
  618         }
  619 #endif /* IPSEC */
  620 
  621         ip6_forward(m, rt, srcrt);
  622         *mp = NULL;
  623         return IPPROTO_DONE;
  624  bad:
  625         nxt = IPPROTO_DONE;
  626         m_freemp(mp);
  627  out:
  628         rtfree(rt);
  629         return nxt;
  630 }
  631 
  632 /* On error free mbuf and return IPPROTO_DONE. */
  633 int
  634 ip6_hbhchcheck(struct mbuf **mp, int *offp, int *oursp)
  635 {
  636         struct ip6_hdr *ip6;
  637         u_int32_t plen, rtalert = ~0;
  638         int nxt;
  639 
  640         ip6 = mtod(*mp, struct ip6_hdr *);
  641 
  642         /*
  643          * Process Hop-by-Hop options header if it's contained.
  644          * m may be modified in ip6_hopopts_input().
  645          * If a JumboPayload option is included, plen will also be modified.
  646          */
  647         plen = (u_int32_t)ntohs(ip6->ip6_plen);
  648         *offp = sizeof(struct ip6_hdr);
  649         if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
  650                 struct ip6_hbh *hbh;
  651 
  652                 if (ip6_hopopts_input(mp, offp, &plen, &rtalert))
  653                         goto bad;       /* m have already been freed */
  654 
  655                 /* adjust pointer */
  656                 ip6 = mtod(*mp, struct ip6_hdr *);
  657 
  658                 /*
  659                  * if the payload length field is 0 and the next header field
  660                  * indicates Hop-by-Hop Options header, then a Jumbo Payload
  661                  * option MUST be included.
  662                  */
  663                 if (ip6->ip6_plen == 0 && plen == 0) {
  664                         /*
  665                          * Note that if a valid jumbo payload option is
  666                          * contained, ip6_hopopts_input() must set a valid
  667                          * (non-zero) payload length to the variable plen.
  668                          */
  669                         ip6stat_inc(ip6s_badoptions);
  670                         icmp6_error(*mp, ICMP6_PARAM_PROB,
  671                                     ICMP6_PARAMPROB_HEADER,
  672                                     (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
  673                         goto bad;
  674                 }
  675                 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, *mp,
  676                     sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
  677                 if (hbh == NULL) {
  678                         ip6stat_inc(ip6s_tooshort);
  679                         goto bad;
  680                 }
  681                 nxt = hbh->ip6h_nxt;
  682 
  683                 /*
  684                  * accept the packet if a router alert option is included
  685                  * and we act as an IPv6 router.
  686                  */
  687                 if (rtalert != ~0 && ip6_forwarding && oursp != NULL)
  688                         *oursp = 1;
  689         } else
  690                 nxt = ip6->ip6_nxt;
  691 
  692         /*
  693          * Check that the amount of data in the buffers
  694          * is as at least much as the IPv6 header would have us expect.
  695          * Trim mbufs if longer than we expect.
  696          * Drop packet if shorter than we expect.
  697          */
  698         if ((*mp)->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
  699                 ip6stat_inc(ip6s_tooshort);
  700                 m_freemp(mp);
  701                 goto bad;
  702         }
  703         if ((*mp)->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
  704                 if ((*mp)->m_len == (*mp)->m_pkthdr.len) {
  705                         (*mp)->m_len = sizeof(struct ip6_hdr) + plen;
  706                         (*mp)->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
  707                 } else {
  708                         m_adj((*mp), sizeof(struct ip6_hdr) + plen -
  709                             (*mp)->m_pkthdr.len);
  710                 }
  711         }
  712 
  713         return nxt;
  714  bad:
  715         return IPPROTO_DONE;
  716 }
  717 
  718 /* scan packet for RH0 routing header. Mostly stolen from pf.c:pf_test() */
  719 int
  720 ip6_check_rh0hdr(struct mbuf *m, int *offp)
  721 {
  722         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  723         struct ip6_rthdr rthdr;
  724         struct ip6_ext opt6;
  725         u_int8_t proto = ip6->ip6_nxt;
  726         int done = 0, lim, off, rh_cnt = 0;
  727 
  728         off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr);
  729         lim = min(m->m_pkthdr.len, ntohs(ip6->ip6_plen) + sizeof(*ip6));
  730         do {
  731                 switch (proto) {
  732                 case IPPROTO_ROUTING:
  733                         if (rh_cnt++) {
  734                                 /* more than one rh header present */
  735                                 *offp = off;
  736                                 return (1);
  737                         }
  738 
  739                         if (off + sizeof(rthdr) > lim) {
  740                                 /* packet to short to make sense */
  741                                 *offp = off;
  742                                 return (1);
  743                         }
  744 
  745                         m_copydata(m, off, sizeof(rthdr), &rthdr);
  746 
  747                         if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
  748                                 *offp = off +
  749                                     offsetof(struct ip6_rthdr, ip6r_type);
  750                                 return (1);
  751                         }
  752 
  753                         off += (rthdr.ip6r_len + 1) * 8;
  754                         proto = rthdr.ip6r_nxt;
  755                         break;
  756                 case IPPROTO_AH:
  757                 case IPPROTO_HOPOPTS:
  758                 case IPPROTO_DSTOPTS:
  759                         /* get next header and header length */
  760                         if (off + sizeof(opt6) > lim) {
  761                                 /*
  762                                  * Packet to short to make sense, we could
  763                                  * reject the packet but as a router we
  764                                  * should not do that so forward it.
  765                                  */
  766                                 return (0);
  767                         }
  768 
  769                         m_copydata(m, off, sizeof(opt6), &opt6);
  770 
  771                         if (proto == IPPROTO_AH)
  772                                 off += (opt6.ip6e_len + 2) * 4;
  773                         else
  774                                 off += (opt6.ip6e_len + 1) * 8;
  775                         proto = opt6.ip6e_nxt;
  776                         break;
  777                 case IPPROTO_FRAGMENT:
  778                 default:
  779                         /* end of header stack */
  780                         done = 1;
  781                         break;
  782                 }
  783         } while (!done);
  784 
  785         return (0);
  786 }
  787 
  788 /*
  789  * Hop-by-Hop options header processing. If a valid jumbo payload option is
  790  * included, the real payload length will be stored in plenp.
  791  * On error free mbuf and return -1.
  792  *
  793  * rtalertp - XXX: should be stored in a more smart way
  794  */
  795 int
  796 ip6_hopopts_input(struct mbuf **mp, int *offp, u_int32_t *plenp,
  797     u_int32_t *rtalertp)
  798 {
  799         int off = *offp, hbhlen;
  800         struct ip6_hbh *hbh;
  801 
  802         /* validation of the length of the header */
  803         IP6_EXTHDR_GET(hbh, struct ip6_hbh *, *mp,
  804                 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
  805         if (hbh == NULL) {
  806                 ip6stat_inc(ip6s_tooshort);
  807                 return -1;
  808         }
  809         hbhlen = (hbh->ip6h_len + 1) << 3;
  810         IP6_EXTHDR_GET(hbh, struct ip6_hbh *, *mp, sizeof(struct ip6_hdr),
  811                 hbhlen);
  812         if (hbh == NULL) {
  813                 ip6stat_inc(ip6s_tooshort);
  814                 return -1;
  815         }
  816         off += hbhlen;
  817         hbhlen -= sizeof(struct ip6_hbh);
  818 
  819         if (ip6_process_hopopts(mp, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
  820                                 hbhlen, rtalertp, plenp) < 0)
  821                 return (-1);
  822 
  823         *offp = off;
  824         return (0);
  825 }
  826 
  827 /*
  828  * Search header for all Hop-by-hop options and process each option.
  829  * This function is separate from ip6_hopopts_input() in order to
  830  * handle a case where the sending node itself process its hop-by-hop
  831  * options header. In such a case, the function is called from ip6_output().
  832  * On error free mbuf and return -1.
  833  *
  834  * The function assumes that hbh header is located right after the IPv6 header
  835  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
  836  * opthead + hbhlen is located in continuous memory region.
  837  */
  838 int
  839 ip6_process_hopopts(struct mbuf **mp, u_int8_t *opthead, int hbhlen,
  840     u_int32_t *rtalertp, u_int32_t *plenp)
  841 {
  842         struct ip6_hdr *ip6;
  843         int optlen = 0;
  844         u_int8_t *opt = opthead;
  845         u_int16_t rtalert_val;
  846         u_int32_t jumboplen;
  847         const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
  848 
  849         for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
  850                 switch (*opt) {
  851                 case IP6OPT_PAD1:
  852                         optlen = 1;
  853                         break;
  854                 case IP6OPT_PADN:
  855                         if (hbhlen < IP6OPT_MINLEN) {
  856                                 ip6stat_inc(ip6s_toosmall);
  857                                 goto bad;
  858                         }
  859                         optlen = *(opt + 1) + 2;
  860                         break;
  861                 case IP6OPT_ROUTER_ALERT:
  862                         /* XXX may need check for alignment */
  863                         if (hbhlen < IP6OPT_RTALERT_LEN) {
  864                                 ip6stat_inc(ip6s_toosmall);
  865                                 goto bad;
  866                         }
  867                         if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
  868                                 /* XXX stat */
  869                                 icmp6_error(*mp, ICMP6_PARAM_PROB,
  870                                     ICMP6_PARAMPROB_HEADER,
  871                                     erroff + opt + 1 - opthead);
  872                                 return (-1);
  873                         }
  874                         optlen = IP6OPT_RTALERT_LEN;
  875                         memcpy((caddr_t)&rtalert_val, (caddr_t)(opt + 2), 2);
  876                         *rtalertp = ntohs(rtalert_val);
  877                         break;
  878                 case IP6OPT_JUMBO:
  879                         /* XXX may need check for alignment */
  880                         if (hbhlen < IP6OPT_JUMBO_LEN) {
  881                                 ip6stat_inc(ip6s_toosmall);
  882                                 goto bad;
  883                         }
  884                         if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
  885                                 /* XXX stat */
  886                                 icmp6_error(*mp, ICMP6_PARAM_PROB,
  887                                     ICMP6_PARAMPROB_HEADER,
  888                                     erroff + opt + 1 - opthead);
  889                                 return (-1);
  890                         }
  891                         optlen = IP6OPT_JUMBO_LEN;
  892 
  893                         /*
  894                          * IPv6 packets that have non 0 payload length
  895                          * must not contain a jumbo payload option.
  896                          */
  897                         ip6 = mtod(*mp, struct ip6_hdr *);
  898                         if (ip6->ip6_plen) {
  899                                 ip6stat_inc(ip6s_badoptions);
  900                                 icmp6_error(*mp, ICMP6_PARAM_PROB,
  901                                     ICMP6_PARAMPROB_HEADER,
  902                                     erroff + opt - opthead);
  903                                 return (-1);
  904                         }
  905 
  906                         /*
  907                          * We may see jumbolen in unaligned location, so
  908                          * we'd need to perform memcpy().
  909                          */
  910                         memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
  911                         jumboplen = (u_int32_t)htonl(jumboplen);
  912 
  913 #if 1
  914                         /*
  915                          * if there are multiple jumbo payload options,
  916                          * *plenp will be non-zero and the packet will be
  917                          * rejected.
  918                          * the behavior may need some debate in ipngwg -
  919                          * multiple options does not make sense, however,
  920                          * there's no explicit mention in specification.
  921                          */
  922                         if (*plenp != 0) {
  923                                 ip6stat_inc(ip6s_badoptions);
  924                                 icmp6_error(*mp, ICMP6_PARAM_PROB,
  925                                     ICMP6_PARAMPROB_HEADER,
  926                                     erroff + opt + 2 - opthead);
  927                                 return (-1);
  928                         }
  929 #endif
  930 
  931                         /*
  932                          * jumbo payload length must be larger than 65535.
  933                          */
  934                         if (jumboplen <= IPV6_MAXPACKET) {
  935                                 ip6stat_inc(ip6s_badoptions);
  936                                 icmp6_error(*mp, ICMP6_PARAM_PROB,
  937                                     ICMP6_PARAMPROB_HEADER,
  938                                     erroff + opt + 2 - opthead);
  939                                 return (-1);
  940                         }
  941                         *plenp = jumboplen;
  942 
  943                         break;
  944                 default:                /* unknown option */
  945                         if (hbhlen < IP6OPT_MINLEN) {
  946                                 ip6stat_inc(ip6s_toosmall);
  947                                 goto bad;
  948                         }
  949                         optlen = ip6_unknown_opt(mp, opt,
  950                             erroff + opt - opthead);
  951                         if (optlen == -1)
  952                                 return (-1);
  953                         optlen += 2;
  954                         break;
  955                 }
  956         }
  957 
  958         return (0);
  959 
  960   bad:
  961         m_freemp(mp);
  962         return (-1);
  963 }
  964 
  965 /*
  966  * Unknown option processing.
  967  * The third argument `off' is the offset from the IPv6 header to the option,
  968  * which allows returning an ICMPv6 error even if the IPv6 header and the
  969  * option header are not continuous.
  970  * On error free mbuf and return -1.
  971  */
  972 int
  973 ip6_unknown_opt(struct mbuf **mp, u_int8_t *optp, int off)
  974 {
  975         struct ip6_hdr *ip6;
  976 
  977         switch (IP6OPT_TYPE(*optp)) {
  978         case IP6OPT_TYPE_SKIP: /* ignore the option */
  979                 return ((int)*(optp + 1));
  980         case IP6OPT_TYPE_DISCARD:       /* silently discard */
  981                 m_freemp(mp);
  982                 return (-1);
  983         case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
  984                 ip6stat_inc(ip6s_badoptions);
  985                 icmp6_error(*mp, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
  986                 return (-1);
  987         case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
  988                 ip6stat_inc(ip6s_badoptions);
  989                 ip6 = mtod(*mp, struct ip6_hdr *);
  990                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
  991                     ((*mp)->m_flags & (M_BCAST|M_MCAST)))
  992                         m_freemp(mp);
  993                 else
  994                         icmp6_error(*mp, ICMP6_PARAM_PROB,
  995                                     ICMP6_PARAMPROB_OPTION, off);
  996                 return (-1);
  997         }
  998 
  999         m_freemp(mp);           /* XXX: NOTREACHED */
 1000         return (-1);
 1001 }
 1002 
 1003 /*
 1004  * Create the "control" list for this pcb.
 1005  *
 1006  * The routine will be called from upper layer handlers like udp_input().
 1007  * Thus the routine assumes that the caller (udp_input) have already
 1008  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
 1009  * very first mbuf on the mbuf chain.
 1010  * We may want to add some infinite loop prevention or sanity checks for safety.
 1011  * (This applies only when you are using KAME mbuf chain restriction, i.e.
 1012  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
 1013  */
 1014 void
 1015 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
 1016 {
 1017         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1018 
 1019         if (in6p->inp_socket->so_options & SO_TIMESTAMP) {
 1020                 struct timeval tv;
 1021 
 1022                 m_microtime(m, &tv);
 1023                 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
 1024                     SCM_TIMESTAMP, SOL_SOCKET);
 1025                 if (*mp)
 1026                         mp = &(*mp)->m_next;
 1027         }
 1028 
 1029         /* RFC 2292 sec. 5 */
 1030         if ((in6p->inp_flags & IN6P_PKTINFO) != 0) {
 1031                 struct in6_pktinfo pi6;
 1032                 memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
 1033                 if (IN6_IS_SCOPE_EMBED(&pi6.ipi6_addr))
 1034                         pi6.ipi6_addr.s6_addr16[1] = 0;
 1035                 pi6.ipi6_ifindex = m ? m->m_pkthdr.ph_ifidx : 0;
 1036                 *mp = sbcreatecontrol((caddr_t) &pi6,
 1037                     sizeof(struct in6_pktinfo),
 1038                     IPV6_PKTINFO, IPPROTO_IPV6);
 1039                 if (*mp)
 1040                         mp = &(*mp)->m_next;
 1041         }
 1042 
 1043         if ((in6p->inp_flags & IN6P_HOPLIMIT) != 0) {
 1044                 int hlim = ip6->ip6_hlim & 0xff;
 1045                 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
 1046                     IPV6_HOPLIMIT, IPPROTO_IPV6);
 1047                 if (*mp)
 1048                         mp = &(*mp)->m_next;
 1049         }
 1050 
 1051         if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
 1052                 u_int32_t flowinfo;
 1053                 int tclass;
 1054 
 1055                 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
 1056                 flowinfo >>= 20;
 1057 
 1058                 tclass = flowinfo & 0xff;
 1059                 *mp = sbcreatecontrol((caddr_t)&tclass, sizeof(tclass),
 1060                     IPV6_TCLASS, IPPROTO_IPV6);
 1061                 if (*mp)
 1062                         mp = &(*mp)->m_next;
 1063         }
 1064 
 1065         /*
 1066          * IPV6_HOPOPTS socket option.  Recall that we required super-user
 1067          * privilege for the option (see ip6_ctloutput), but it might be too
 1068          * strict, since there might be some hop-by-hop options which can be
 1069          * returned to normal user.
 1070          * See also RFC 2292 section 6 (or RFC 3542 section 8).
 1071          */
 1072         if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
 1073                 /*
 1074                  * Check if a hop-by-hop options header is contained in the
 1075                  * received packet, and if so, store the options as ancillary
 1076                  * data. Note that a hop-by-hop options header must be
 1077                  * just after the IPv6 header, which is assured through the
 1078                  * IPv6 input processing.
 1079                  */
 1080                 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1081                 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
 1082                         struct ip6_hbh *hbh;
 1083                         int hbhlen = 0;
 1084                         struct mbuf *ext;
 1085 
 1086                         ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
 1087                             ip6->ip6_nxt);
 1088                         if (ext == NULL) {
 1089                                 ip6stat_inc(ip6s_tooshort);
 1090                                 return;
 1091                         }
 1092                         hbh = mtod(ext, struct ip6_hbh *);
 1093                         hbhlen = (hbh->ip6h_len + 1) << 3;
 1094                         if (hbhlen != ext->m_len) {
 1095                                 m_freem(ext);
 1096                                 ip6stat_inc(ip6s_tooshort);
 1097                                 return;
 1098                         }
 1099 
 1100                         /*
 1101                          * XXX: We copy the whole header even if a
 1102                          * jumbo payload option is included, the option which
 1103                          * is to be removed before returning according to
 1104                          * RFC2292.
 1105                          * Note: this constraint is removed in RFC3542.
 1106                          */
 1107                         *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
 1108                             IPV6_HOPOPTS,
 1109                             IPPROTO_IPV6);
 1110                         if (*mp)
 1111                                 mp = &(*mp)->m_next;
 1112                         m_freem(ext);
 1113                 }
 1114         }
 1115 
 1116         /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
 1117         if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
 1118                 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1119                 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
 1120 
 1121                 /*
 1122                  * Search for destination options headers or routing
 1123                  * header(s) through the header chain, and stores each
 1124                  * header as ancillary data.
 1125                  * Note that the order of the headers remains in
 1126                  * the chain of ancillary data.
 1127                  */
 1128                 while (1) {     /* is explicit loop prevention necessary? */
 1129                         struct ip6_ext *ip6e = NULL;
 1130                         int elen;
 1131                         struct mbuf *ext = NULL;
 1132 
 1133                         /*
 1134                          * if it is not an extension header, don't try to
 1135                          * pull it from the chain.
 1136                          */
 1137                         switch (nxt) {
 1138                         case IPPROTO_DSTOPTS:
 1139                         case IPPROTO_ROUTING:
 1140                         case IPPROTO_HOPOPTS:
 1141                         case IPPROTO_AH: /* is it possible? */
 1142                                 break;
 1143                         default:
 1144                                 goto loopend;
 1145                         }
 1146 
 1147                         ext = ip6_pullexthdr(m, off, nxt);
 1148                         if (ext == NULL) {
 1149                                 ip6stat_inc(ip6s_tooshort);
 1150                                 return;
 1151                         }
 1152                         ip6e = mtod(ext, struct ip6_ext *);
 1153                         if (nxt == IPPROTO_AH)
 1154                                 elen = (ip6e->ip6e_len + 2) << 2;
 1155                         else
 1156                                 elen = (ip6e->ip6e_len + 1) << 3;
 1157                         if (elen != ext->m_len) {
 1158                                 m_freem(ext);
 1159                                 ip6stat_inc(ip6s_tooshort);
 1160                                 return;
 1161                         }
 1162 
 1163                         switch (nxt) {
 1164                         case IPPROTO_DSTOPTS:
 1165                                 if (!(in6p->inp_flags & IN6P_DSTOPTS))
 1166                                         break;
 1167 
 1168                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
 1169                                     IPV6_DSTOPTS,
 1170                                     IPPROTO_IPV6);
 1171                                 if (*mp)
 1172                                         mp = &(*mp)->m_next;
 1173                                 break;
 1174 
 1175                         case IPPROTO_ROUTING:
 1176                                 if (!(in6p->inp_flags & IN6P_RTHDR))
 1177                                         break;
 1178 
 1179                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
 1180                                     IPV6_RTHDR,
 1181                                     IPPROTO_IPV6);
 1182                                 if (*mp)
 1183                                         mp = &(*mp)->m_next;
 1184                                 break;
 1185 
 1186                         case IPPROTO_HOPOPTS:
 1187                         case IPPROTO_AH: /* is it possible? */
 1188                                 break;
 1189 
 1190                         default:
 1191                                 /*
 1192                                  * other cases have been filtered in the above.
 1193                                  * none will visit this case.  here we supply
 1194                                  * the code just in case (nxt overwritten or
 1195                                  * other cases).
 1196                                  */
 1197                                 m_freem(ext);
 1198                                 goto loopend;
 1199 
 1200                         }
 1201 
 1202                         /* proceed with the next header. */
 1203                         off += elen;
 1204                         nxt = ip6e->ip6e_nxt;
 1205                         ip6e = NULL;
 1206                         m_freem(ext);
 1207                         ext = NULL;
 1208                 }
 1209 loopend:
 1210                 ;
 1211         }
 1212 }
 1213 
 1214 /*
 1215  * pull single extension header from mbuf chain.  returns single mbuf that
 1216  * contains the result, or NULL on error.
 1217  */
 1218 struct mbuf *
 1219 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
 1220 {
 1221         struct ip6_ext ip6e;
 1222         size_t elen;
 1223         struct mbuf *n;
 1224 
 1225 #ifdef DIAGNOSTIC
 1226         switch (nxt) {
 1227         case IPPROTO_DSTOPTS:
 1228         case IPPROTO_ROUTING:
 1229         case IPPROTO_HOPOPTS:
 1230         case IPPROTO_AH: /* is it possible? */
 1231                 break;
 1232         default:
 1233                 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
 1234         }
 1235 #endif
 1236 
 1237         if (off + sizeof(ip6e) > m->m_pkthdr.len)
 1238                 return NULL;
 1239 
 1240         m_copydata(m, off, sizeof(ip6e), &ip6e);
 1241         if (nxt == IPPROTO_AH)
 1242                 elen = (ip6e.ip6e_len + 2) << 2;
 1243         else
 1244                 elen = (ip6e.ip6e_len + 1) << 3;
 1245 
 1246         if (off + elen > m->m_pkthdr.len)
 1247                 return NULL;
 1248 
 1249         MGET(n, M_DONTWAIT, MT_DATA);
 1250         if (n && elen >= MLEN) {
 1251                 MCLGET(n, M_DONTWAIT);
 1252                 if ((n->m_flags & M_EXT) == 0) {
 1253                         m_free(n);
 1254                         n = NULL;
 1255                 }
 1256         }
 1257         if (n == NULL) {
 1258                 ip6stat_inc(ip6s_idropped);
 1259                 return NULL;
 1260         }
 1261 
 1262         n->m_len = 0;
 1263         if (elen >= m_trailingspace(n)) {
 1264                 m_free(n);
 1265                 return NULL;
 1266         }
 1267 
 1268         m_copydata(m, off, elen, mtod(n, caddr_t));
 1269         n->m_len = elen;
 1270         return n;
 1271 }
 1272 
 1273 /*
 1274  * Get offset to the previous header followed by the header
 1275  * currently processed.
 1276  */
 1277 int
 1278 ip6_get_prevhdr(struct mbuf *m, int off)
 1279 {
 1280         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1281 
 1282         if (off == sizeof(struct ip6_hdr)) {
 1283                 return offsetof(struct ip6_hdr, ip6_nxt);
 1284         } else if (off < sizeof(struct ip6_hdr)) {
 1285                 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
 1286         } else {
 1287                 int len, nlen, nxt;
 1288                 struct ip6_ext ip6e;
 1289 
 1290                 nxt = ip6->ip6_nxt;
 1291                 len = sizeof(struct ip6_hdr);
 1292                 nlen = 0;
 1293                 while (len < off) {
 1294                         m_copydata(m, len, sizeof(ip6e), &ip6e);
 1295 
 1296                         switch (nxt) {
 1297                         case IPPROTO_FRAGMENT:
 1298                                 nlen = sizeof(struct ip6_frag);
 1299                                 break;
 1300                         case IPPROTO_AH:
 1301                                 nlen = (ip6e.ip6e_len + 2) << 2;
 1302                                 break;
 1303                         default:
 1304                                 nlen = (ip6e.ip6e_len + 1) << 3;
 1305                                 break;
 1306                         }
 1307                         len += nlen;
 1308                         nxt = ip6e.ip6e_nxt;
 1309                 }
 1310 
 1311                 return (len - nlen);
 1312         }
 1313 }
 1314 
 1315 /*
 1316  * get next header offset.  m will be retained.
 1317  */
 1318 int
 1319 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
 1320 {
 1321         struct ip6_hdr ip6;
 1322         struct ip6_ext ip6e;
 1323         struct ip6_frag fh;
 1324 
 1325         /* just in case */
 1326         if (m == NULL)
 1327                 panic("%s: m == NULL", __func__);
 1328         if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
 1329                 return -1;
 1330 
 1331         switch (proto) {
 1332         case IPPROTO_IPV6:
 1333                 if (m->m_pkthdr.len < off + sizeof(ip6))
 1334                         return -1;
 1335                 m_copydata(m, off, sizeof(ip6), &ip6);
 1336                 if (nxtp)
 1337                         *nxtp = ip6.ip6_nxt;
 1338                 off += sizeof(ip6);
 1339                 return off;
 1340 
 1341         case IPPROTO_FRAGMENT:
 1342                 /*
 1343                  * terminate parsing if it is not the first fragment,
 1344                  * it does not make sense to parse through it.
 1345                  */
 1346                 if (m->m_pkthdr.len < off + sizeof(fh))
 1347                         return -1;
 1348                 m_copydata(m, off, sizeof(fh), &fh);
 1349                 if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
 1350                         return -1;
 1351                 if (nxtp)
 1352                         *nxtp = fh.ip6f_nxt;
 1353                 off += sizeof(struct ip6_frag);
 1354                 return off;
 1355 
 1356         case IPPROTO_AH:
 1357                 if (m->m_pkthdr.len < off + sizeof(ip6e))
 1358                         return -1;
 1359                 m_copydata(m, off, sizeof(ip6e), &ip6e);
 1360                 if (nxtp)
 1361                         *nxtp = ip6e.ip6e_nxt;
 1362                 off += (ip6e.ip6e_len + 2) << 2;
 1363                 if (m->m_pkthdr.len < off)
 1364                         return -1;
 1365                 return off;
 1366 
 1367         case IPPROTO_HOPOPTS:
 1368         case IPPROTO_ROUTING:
 1369         case IPPROTO_DSTOPTS:
 1370                 if (m->m_pkthdr.len < off + sizeof(ip6e))
 1371                         return -1;
 1372                 m_copydata(m, off, sizeof(ip6e), &ip6e);
 1373                 if (nxtp)
 1374                         *nxtp = ip6e.ip6e_nxt;
 1375                 off += (ip6e.ip6e_len + 1) << 3;
 1376                 if (m->m_pkthdr.len < off)
 1377                         return -1;
 1378                 return off;
 1379 
 1380         case IPPROTO_NONE:
 1381         case IPPROTO_ESP:
 1382         case IPPROTO_IPCOMP:
 1383                 /* give up */
 1384                 return -1;
 1385 
 1386         default:
 1387                 return -1;
 1388         }
 1389 
 1390         return -1;
 1391 }
 1392 
 1393 /*
 1394  * get offset for the last header in the chain.  m will be kept untainted.
 1395  */
 1396 int
 1397 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
 1398 {
 1399         int newoff;
 1400         int nxt;
 1401 
 1402         if (!nxtp) {
 1403                 nxt = -1;
 1404                 nxtp = &nxt;
 1405         }
 1406         while (1) {
 1407                 newoff = ip6_nexthdr(m, off, proto, nxtp);
 1408                 if (newoff < 0)
 1409                         return off;
 1410                 else if (newoff < off)
 1411                         return -1;      /* invalid */
 1412                 else if (newoff == off)
 1413                         return newoff;
 1414 
 1415                 off = newoff;
 1416                 proto = *nxtp;
 1417         }
 1418 }
 1419 
 1420 /*
 1421  * System control for IP6
 1422  */
 1423 
 1424 const u_char inet6ctlerrmap[PRC_NCMDS] = {
 1425         0,              0,              0,              0,
 1426         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
 1427         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
 1428         EMSGSIZE,       EHOSTUNREACH,   0,              0,
 1429         0,              0,              0,              0,
 1430         ENOPROTOOPT
 1431 };
 1432 
 1433 #ifdef MROUTING
 1434 extern int ip6_mrtproto;
 1435 #endif
 1436 
 1437 const struct sysctl_bounded_args ipv6ctl_vars[] = {
 1438         { IPV6CTL_DAD_PENDING, &ip6_dad_pending, SYSCTL_INT_READONLY },
 1439 #ifdef MROUTING
 1440         { IPV6CTL_MRTPROTO, &ip6_mrtproto, SYSCTL_INT_READONLY },
 1441 #endif
 1442         { IPV6CTL_FORWARDING, &ip6_forwarding, 0, 1 },
 1443         { IPV6CTL_SENDREDIRECTS, &ip6_sendredirects, 0, 1 },
 1444         { IPV6CTL_DEFHLIM, &ip6_defhlim, 0, 255 },
 1445         { IPV6CTL_MAXFRAGPACKETS, &ip6_maxfragpackets, 0, 1000 },
 1446         { IPV6CTL_LOG_INTERVAL, &ip6_log_interval, 0, INT_MAX },
 1447         { IPV6CTL_HDRNESTLIMIT, &ip6_hdrnestlimit, 0, 100 },
 1448         { IPV6CTL_DAD_COUNT, &ip6_dad_count, 0, 10 },
 1449         { IPV6CTL_AUTO_FLOWLABEL, &ip6_auto_flowlabel, 0, 1 },
 1450         { IPV6CTL_DEFMCASTHLIM, &ip6_defmcasthlim, 0, 255 },
 1451         { IPV6CTL_USE_DEPRECATED, &ip6_use_deprecated, 0, 1 },
 1452         { IPV6CTL_MAXFRAGS, &ip6_maxfrags, 0, 1000 },
 1453         { IPV6CTL_MFORWARDING, &ip6_mforwarding, 0, 1 },
 1454         { IPV6CTL_MULTIPATH, &ip6_multipath, 0, 1 },
 1455         { IPV6CTL_MCAST_PMTU, &ip6_mcast_pmtu, 0, 1 },
 1456         { IPV6CTL_NEIGHBORGCTHRESH, &ip6_neighborgcthresh, -1, 5 * 2048 },
 1457         { IPV6CTL_MAXDYNROUTES, &ip6_maxdynroutes, -1, 5 * 4096 },
 1458 };
 1459 
 1460 int
 1461 ip6_sysctl_ip6stat(void *oldp, size_t *oldlenp, void *newp)
 1462 {
 1463         struct ip6stat *ip6stat;
 1464         int ret;
 1465 
 1466         CTASSERT(sizeof(*ip6stat) == (ip6s_ncounters * sizeof(uint64_t)));
 1467 
 1468         ip6stat = malloc(sizeof(*ip6stat), M_TEMP, M_WAITOK);
 1469         counters_read(ip6counters, (uint64_t *)ip6stat, ip6s_ncounters);
 1470         ret = sysctl_rdstruct(oldp, oldlenp, newp,
 1471             ip6stat, sizeof(*ip6stat));
 1472         free(ip6stat, M_TEMP, sizeof(*ip6stat));
 1473 
 1474         return (ret);
 1475 }
 1476 
 1477 int
 1478 ip6_sysctl_soiikey(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
 1479 {
 1480         uint8_t oldkey[IP6_SOIIKEY_LEN];
 1481         int error;
 1482 
 1483         error = suser(curproc);
 1484         if (error != 0)
 1485                 return (error);
 1486 
 1487         memcpy(oldkey, ip6_soiikey, sizeof(oldkey));
 1488 
 1489         error = sysctl_struct(oldp, oldlenp, newp, newlen, ip6_soiikey,
 1490             sizeof(ip6_soiikey));
 1491 
 1492         return (error);
 1493 }
 1494 
 1495 int
 1496 ip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
 1497     void *newp, size_t newlen)
 1498 {
 1499 #ifdef MROUTING
 1500         extern struct mrt6stat mrt6stat;
 1501 #endif
 1502         int error;
 1503 
 1504         /* Almost all sysctl names at this level are terminal. */
 1505         if (namelen != 1 && name[0] != IPV6CTL_IFQUEUE)
 1506                 return (ENOTDIR);
 1507 
 1508         switch (name[0]) {
 1509         case IPV6CTL_STATS:
 1510                 return (ip6_sysctl_ip6stat(oldp, oldlenp, newp));
 1511 #ifdef MROUTING
 1512         case IPV6CTL_MRTSTATS:
 1513                 if (newp != NULL)
 1514                         return (EPERM);
 1515                 NET_LOCK();
 1516                 error = sysctl_struct(oldp, oldlenp, newp, newlen,
 1517                     &mrt6stat, sizeof(mrt6stat));
 1518                 NET_UNLOCK();
 1519                 return (error);
 1520         case IPV6CTL_MRTMIF:
 1521                 if (newp)
 1522                         return (EPERM);
 1523                 NET_LOCK();
 1524                 error = mrt6_sysctl_mif(oldp, oldlenp);
 1525                 NET_UNLOCK();
 1526                 return (error);
 1527         case IPV6CTL_MRTMFC:
 1528                 if (newp)
 1529                         return (EPERM);
 1530                 NET_LOCK();
 1531                 error = mrt6_sysctl_mfc(oldp, oldlenp);
 1532                 NET_UNLOCK();
 1533                 return (error);
 1534 #else
 1535         case IPV6CTL_MRTSTATS:
 1536         case IPV6CTL_MRTPROTO:
 1537         case IPV6CTL_MRTMIF:
 1538         case IPV6CTL_MRTMFC:
 1539                 return (EOPNOTSUPP);
 1540 #endif
 1541         case IPV6CTL_MTUDISCTIMEOUT:
 1542                 NET_LOCK();
 1543                 error = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
 1544                     &ip6_mtudisc_timeout, 0, INT_MAX);
 1545                 rt_timer_queue_change(&icmp6_mtudisc_timeout_q,
 1546                     ip6_mtudisc_timeout);
 1547                 NET_UNLOCK();
 1548                 return (error);
 1549         case IPV6CTL_IFQUEUE:
 1550                 return (sysctl_niq(name + 1, namelen - 1,
 1551                     oldp, oldlenp, newp, newlen, &ip6intrq));
 1552         case IPV6CTL_SOIIKEY:
 1553                 return (ip6_sysctl_soiikey(oldp, oldlenp, newp, newlen));
 1554         default:
 1555                 NET_LOCK();
 1556                 error = sysctl_bounded_arr(ipv6ctl_vars, nitems(ipv6ctl_vars),
 1557                     name, namelen, oldp, oldlenp, newp, newlen);
 1558                 NET_UNLOCK();
 1559                 return (error);
 1560         }
 1561         /* NOTREACHED */
 1562 }
 1563 
 1564 void
 1565 ip6_send_dispatch(void *xmq)
 1566 {
 1567         struct mbuf_queue *mq = xmq;
 1568         struct mbuf *m;
 1569         struct mbuf_list ml;
 1570 
 1571         mq_delist(mq, &ml);
 1572         if (ml_empty(&ml))
 1573                 return;
 1574 
 1575         NET_LOCK();
 1576         while ((m = ml_dequeue(&ml)) != NULL) {
 1577                 ip6_output(m, NULL, NULL, 0, NULL, NULL);
 1578         }
 1579         NET_UNLOCK();
 1580 }
 1581 
 1582 void
 1583 ip6_send(struct mbuf *m)
 1584 {
 1585         mq_enqueue(&ip6send_mq, m);
 1586         task_add(net_tq(0), &ip6send_task);
 1587 }

Cache object: e00ea09229fd5cfc8bfa40d84904c562


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