The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_input.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the project nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
   32  */
   33 
   34 /*-
   35  * Copyright (c) 1982, 1986, 1988, 1993
   36  *      The Regents of the University of California.  All rights reserved.
   37  *
   38  * Redistribution and use in source and binary forms, with or without
   39  * modification, are permitted provided that the following conditions
   40  * are met:
   41  * 1. Redistributions of source code must retain the above copyright
   42  *    notice, this list of conditions and the following disclaimer.
   43  * 2. Redistributions in binary form must reproduce the above copyright
   44  *    notice, this list of conditions and the following disclaimer in the
   45  *    documentation and/or other materials provided with the distribution.
   46  * 3. Neither the name of the University nor the names of its contributors
   47  *    may be used to endorse or promote products derived from this software
   48  *    without specific prior written permission.
   49  *
   50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   60  * SUCH DAMAGE.
   61  *
   62  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
   63  */
   64 
   65 #include <sys/cdefs.h>
   66 __FBSDID("$FreeBSD$");
   67 
   68 #include "opt_inet.h"
   69 #include "opt_inet6.h"
   70 #include "opt_ipsec.h"
   71 #include "opt_route.h"
   72 #include "opt_rss.h"
   73 
   74 #include <sys/param.h>
   75 #include <sys/systm.h>
   76 #include <sys/hhook.h>
   77 #include <sys/malloc.h>
   78 #include <sys/mbuf.h>
   79 #include <sys/proc.h>
   80 #include <sys/domain.h>
   81 #include <sys/protosw.h>
   82 #include <sys/sdt.h>
   83 #include <sys/socket.h>
   84 #include <sys/socketvar.h>
   85 #include <sys/errno.h>
   86 #include <sys/time.h>
   87 #include <sys/kernel.h>
   88 #include <sys/lock.h>
   89 #include <sys/rmlock.h>
   90 #include <sys/syslog.h>
   91 #include <sys/sysctl.h>
   92 
   93 #include <net/if.h>
   94 #include <net/if_var.h>
   95 #include <net/if_types.h>
   96 #include <net/if_dl.h>
   97 #include <net/route.h>
   98 #include <net/netisr.h>
   99 #include <net/rss_config.h>
  100 #include <net/pfil.h>
  101 #include <net/vnet.h>
  102 
  103 #include <netinet/in.h>
  104 #include <netinet/in_kdtrace.h>
  105 #include <netinet/ip_var.h>
  106 #include <netinet/in_systm.h>
  107 #include <net/if_llatbl.h>
  108 #ifdef INET
  109 #include <netinet/ip.h>
  110 #include <netinet/ip_icmp.h>
  111 #endif /* INET */
  112 #include <netinet/ip6.h>
  113 #include <netinet6/in6_var.h>
  114 #include <netinet6/ip6_var.h>
  115 #include <netinet/in_pcb.h>
  116 #include <netinet/icmp6.h>
  117 #include <netinet6/scope6_var.h>
  118 #include <netinet6/in6_ifattach.h>
  119 #include <netinet6/mld6_var.h>
  120 #include <netinet6/nd6.h>
  121 #include <netinet6/in6_rss.h>
  122 
  123 #include <netipsec/ipsec_support.h>
  124 
  125 #include <netinet6/ip6protosw.h>
  126 
  127 extern struct domain inet6domain;
  128 
  129 u_char ip6_protox[IPPROTO_MAX];
  130 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
  131 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
  132 VNET_DEFINE(u_long, in6_ifaddrhmask);
  133 
  134 static struct netisr_handler ip6_nh = {
  135         .nh_name = "ip6",
  136         .nh_handler = ip6_input,
  137         .nh_proto = NETISR_IPV6,
  138 #ifdef RSS
  139         .nh_m2cpuid = rss_soft_m2cpuid_v6,
  140         .nh_policy = NETISR_POLICY_CPU,
  141         .nh_dispatch = NETISR_DISPATCH_HYBRID,
  142 #else
  143         .nh_policy = NETISR_POLICY_FLOW,
  144 #endif
  145 };
  146 
  147 static int
  148 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
  149 {
  150         int error, qlimit;
  151 
  152         netisr_getqlimit(&ip6_nh, &qlimit);
  153         error = sysctl_handle_int(oidp, &qlimit, 0, req);
  154         if (error || !req->newptr)
  155                 return (error);
  156         if (qlimit < 1)
  157                 return (EINVAL);
  158         return (netisr_setqlimit(&ip6_nh, qlimit));
  159 }
  160 SYSCTL_DECL(_net_inet6_ip6);
  161 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
  162     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
  163     0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
  164     "Maximum size of the IPv6 input queue");
  165 
  166 #ifdef RSS
  167 static struct netisr_handler ip6_direct_nh = {
  168         .nh_name = "ip6_direct",
  169         .nh_handler = ip6_direct_input,
  170         .nh_proto = NETISR_IPV6_DIRECT,
  171         .nh_m2cpuid = rss_soft_m2cpuid_v6,
  172         .nh_policy = NETISR_POLICY_CPU,
  173         .nh_dispatch = NETISR_DISPATCH_HYBRID,
  174 };
  175 
  176 static int
  177 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
  178 {
  179         int error, qlimit;
  180 
  181         netisr_getqlimit(&ip6_direct_nh, &qlimit);
  182         error = sysctl_handle_int(oidp, &qlimit, 0, req);
  183         if (error || !req->newptr)
  184                 return (error);
  185         if (qlimit < 1)
  186                 return (EINVAL);
  187         return (netisr_setqlimit(&ip6_direct_nh, qlimit));
  188 }
  189 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
  190     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
  191     0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I",
  192     "Maximum size of the IPv6 direct input queue");
  193 
  194 #endif
  195 
  196 VNET_DEFINE(pfil_head_t, inet6_pfil_head);
  197 
  198 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
  199 VNET_PCPUSTAT_SYSINIT(ip6stat);
  200 #ifdef VIMAGE
  201 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
  202 #endif /* VIMAGE */
  203 
  204 struct rmlock in6_ifaddr_lock;
  205 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
  206 
  207 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
  208 
  209 /*
  210  * IP6 initialization: fill in IP6 protocol switch table.
  211  * All protocols not implemented in kernel go to raw IP6 protocol handler.
  212  */
  213 void
  214 ip6_init(void)
  215 {
  216         struct pfil_head_args args;
  217         struct protosw *pr;
  218         int i;
  219 
  220         TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
  221             &V_ip6_auto_linklocal);
  222         TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
  223         TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
  224 
  225         CK_STAILQ_INIT(&V_in6_ifaddrhead);
  226         V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
  227             &V_in6_ifaddrhmask);
  228 
  229         /* Initialize packet filter hooks. */
  230         args.pa_version = PFIL_VERSION;
  231         args.pa_flags = PFIL_IN | PFIL_OUT;
  232         args.pa_type = PFIL_TYPE_IP6;
  233         args.pa_headname = PFIL_INET6_NAME;
  234         V_inet6_pfil_head = pfil_head_register(&args);
  235 
  236         if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
  237             &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
  238             HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
  239                 printf("%s: WARNING: unable to register input helper hook\n",
  240                     __func__);
  241         if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
  242             &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
  243             HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
  244                 printf("%s: WARNING: unable to register output helper hook\n",
  245                     __func__);
  246 
  247         scope6_init();
  248         addrsel_policy_init();
  249         nd6_init();
  250         frag6_init();
  251 
  252         V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
  253 
  254         /* Skip global initialization stuff for non-default instances. */
  255 #ifdef VIMAGE
  256         if (!IS_DEFAULT_VNET(curvnet)) {
  257                 netisr_register_vnet(&ip6_nh);
  258 #ifdef RSS
  259                 netisr_register_vnet(&ip6_direct_nh);
  260 #endif
  261                 return;
  262         }
  263 #endif
  264 
  265         pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  266         if (pr == NULL)
  267                 panic("ip6_init");
  268 
  269         /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
  270         for (i = 0; i < IPPROTO_MAX; i++)
  271                 ip6_protox[i] = pr - inet6sw;
  272         /*
  273          * Cycle through IP protocols and put them into the appropriate place
  274          * in ip6_protox[].
  275          */
  276         for (pr = inet6domain.dom_protosw;
  277             pr < inet6domain.dom_protoswNPROTOSW; pr++)
  278                 if (pr->pr_domain->dom_family == PF_INET6 &&
  279                     pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
  280                         /* Be careful to only index valid IP protocols. */
  281                         if (pr->pr_protocol < IPPROTO_MAX)
  282                                 ip6_protox[pr->pr_protocol] = pr - inet6sw;
  283                 }
  284 
  285         netisr_register(&ip6_nh);
  286 #ifdef RSS
  287         netisr_register(&ip6_direct_nh);
  288 #endif
  289 }
  290 
  291 /*
  292  * The protocol to be inserted into ip6_protox[] must be already registered
  293  * in inet6sw[], either statically or through pf_proto_register().
  294  */
  295 int
  296 ip6proto_register(short ip6proto)
  297 {
  298         struct protosw *pr;
  299 
  300         /* Sanity checks. */
  301         if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
  302                 return (EPROTONOSUPPORT);
  303 
  304         /*
  305          * The protocol slot must not be occupied by another protocol
  306          * already.  An index pointing to IPPROTO_RAW is unused.
  307          */
  308         pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  309         if (pr == NULL)
  310                 return (EPFNOSUPPORT);
  311         if (ip6_protox[ip6proto] != pr - inet6sw)       /* IPPROTO_RAW */
  312                 return (EEXIST);
  313 
  314         /*
  315          * Find the protocol position in inet6sw[] and set the index.
  316          */
  317         for (pr = inet6domain.dom_protosw;
  318             pr < inet6domain.dom_protoswNPROTOSW; pr++) {
  319                 if (pr->pr_domain->dom_family == PF_INET6 &&
  320                     pr->pr_protocol && pr->pr_protocol == ip6proto) {
  321                         ip6_protox[pr->pr_protocol] = pr - inet6sw;
  322                         return (0);
  323                 }
  324         }
  325         return (EPROTONOSUPPORT);
  326 }
  327 
  328 int
  329 ip6proto_unregister(short ip6proto)
  330 {
  331         struct protosw *pr;
  332 
  333         /* Sanity checks. */
  334         if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
  335                 return (EPROTONOSUPPORT);
  336 
  337         /* Check if the protocol was indeed registered. */
  338         pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  339         if (pr == NULL)
  340                 return (EPFNOSUPPORT);
  341         if (ip6_protox[ip6proto] == pr - inet6sw)       /* IPPROTO_RAW */
  342                 return (ENOENT);
  343 
  344         /* Reset the protocol slot to IPPROTO_RAW. */
  345         ip6_protox[ip6proto] = pr - inet6sw;
  346         return (0);
  347 }
  348 
  349 #ifdef VIMAGE
  350 static void
  351 ip6_destroy(void *unused __unused)
  352 {
  353         struct ifaddr *ifa, *nifa;
  354         struct ifnet *ifp;
  355         int error;
  356 
  357 #ifdef RSS
  358         netisr_unregister_vnet(&ip6_direct_nh);
  359 #endif
  360         netisr_unregister_vnet(&ip6_nh);
  361 
  362         pfil_head_unregister(V_inet6_pfil_head);
  363         error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
  364         if (error != 0) {
  365                 printf("%s: WARNING: unable to deregister input helper hook "
  366                     "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
  367                     "error %d returned\n", __func__, error);
  368         }
  369         error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
  370         if (error != 0) {
  371                 printf("%s: WARNING: unable to deregister output helper hook "
  372                     "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
  373                     "error %d returned\n", __func__, error);
  374         }
  375 
  376         /* Cleanup addresses. */
  377         IFNET_RLOCK();
  378         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  379                 /* Cannot lock here - lock recursion. */
  380                 /* IF_ADDR_LOCK(ifp); */
  381                 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
  382                         if (ifa->ifa_addr->sa_family != AF_INET6)
  383                                 continue;
  384                         in6_purgeaddr(ifa);
  385                 }
  386                 /* IF_ADDR_UNLOCK(ifp); */
  387                 in6_ifdetach_destroy(ifp);
  388                 mld_domifdetach(ifp);
  389         }
  390         IFNET_RUNLOCK();
  391 
  392         /* Make sure any routes are gone as well. */
  393         rib_flush_routes_family(AF_INET6);
  394 
  395         frag6_destroy();
  396         nd6_destroy();
  397         in6_ifattach_destroy();
  398 
  399         hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
  400 }
  401 
  402 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
  403 #endif
  404 
  405 static int
  406 ip6_input_hbh(struct mbuf **mp, uint32_t *plen, uint32_t *rtalert, int *off,
  407     int *nxt, int *ours)
  408 {
  409         struct mbuf *m;
  410         struct ip6_hdr *ip6;
  411         struct ip6_hbh *hbh;
  412 
  413         if (ip6_hopopts_input(plen, rtalert, mp, off)) {
  414 #if 0   /*touches NULL pointer*/
  415                 in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard);
  416 #endif
  417                 goto out;       /* m have already been freed */
  418         }
  419 
  420         /* adjust pointer */
  421         m = *mp;
  422         ip6 = mtod(m, struct ip6_hdr *);
  423 
  424         /*
  425          * if the payload length field is 0 and the next header field
  426          * indicates Hop-by-Hop Options header, then a Jumbo Payload
  427          * option MUST be included.
  428          */
  429         if (ip6->ip6_plen == 0 && *plen == 0) {
  430                 /*
  431                  * Note that if a valid jumbo payload option is
  432                  * contained, ip6_hopopts_input() must set a valid
  433                  * (non-zero) payload length to the variable plen.
  434                  */
  435                 IP6STAT_INC(ip6s_badoptions);
  436                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
  437                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
  438                 icmp6_error(m, ICMP6_PARAM_PROB,
  439                             ICMP6_PARAMPROB_HEADER,
  440                             (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
  441                 goto out;
  442         }
  443         /* ip6_hopopts_input() ensures that mbuf is contiguous */
  444         hbh = (struct ip6_hbh *)(ip6 + 1);
  445         *nxt = hbh->ip6h_nxt;
  446 
  447         /*
  448          * If we are acting as a router and the packet contains a
  449          * router alert option, see if we know the option value.
  450          * Currently, we only support the option value for MLD, in which
  451          * case we should pass the packet to the multicast routing
  452          * daemon.
  453          */
  454         if (*rtalert != ~0) {
  455                 switch (*rtalert) {
  456                 case IP6OPT_RTALERT_MLD:
  457                         if (V_ip6_forwarding)
  458                                 *ours = 1;
  459                         break;
  460                 default:
  461                         /*
  462                          * RFC2711 requires unrecognized values must be
  463                          * silently ignored.
  464                          */
  465                         break;
  466                 }
  467         }
  468 
  469         return (0);
  470 
  471 out:
  472         return (1);
  473 }
  474 
  475 #ifdef RSS
  476 /*
  477  * IPv6 direct input routine.
  478  *
  479  * This is called when reinjecting completed fragments where
  480  * all of the previous checking and book-keeping has been done.
  481  */
  482 void
  483 ip6_direct_input(struct mbuf *m)
  484 {
  485         int off, nxt;
  486         int nest;
  487         struct m_tag *mtag;
  488         struct ip6_direct_ctx *ip6dc;
  489 
  490         mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
  491         KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
  492 
  493         ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
  494         nxt = ip6dc->ip6dc_nxt;
  495         off = ip6dc->ip6dc_off;
  496 
  497         nest = 0;
  498 
  499         m_tag_delete(m, mtag);
  500 
  501         while (nxt != IPPROTO_DONE) {
  502                 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
  503                         IP6STAT_INC(ip6s_toomanyhdr);
  504                         goto bad;
  505                 }
  506 
  507                 /*
  508                  * protection against faulty packet - there should be
  509                  * more sanity checks in header chain processing.
  510                  */
  511                 if (m->m_pkthdr.len < off) {
  512                         IP6STAT_INC(ip6s_tooshort);
  513                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
  514                         goto bad;
  515                 }
  516 
  517 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  518                 if (IPSEC_ENABLED(ipv6)) {
  519                         if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
  520                                 return;
  521                 }
  522 #endif /* IPSEC */
  523 
  524                 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
  525         }
  526         return;
  527 bad:
  528         m_freem(m);
  529 }
  530 #endif
  531 
  532 void
  533 ip6_input(struct mbuf *m)
  534 {
  535         struct in6_addr odst;
  536         struct ip6_hdr *ip6;
  537         struct in6_ifaddr *ia;
  538         struct ifnet *rcvif;
  539         u_int32_t plen;
  540         u_int32_t rtalert = ~0;
  541         int off = sizeof(struct ip6_hdr), nest;
  542         int nxt, ours = 0;
  543         int srcrt = 0;
  544 
  545         /*
  546          * Drop the packet if IPv6 operation is disabled on the interface.
  547          */
  548         rcvif = m->m_pkthdr.rcvif;
  549         if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
  550                 goto bad;
  551 
  552 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  553         /*
  554          * should the inner packet be considered authentic?
  555          * see comment in ah4_input().
  556          * NB: m cannot be NULL when passed to the input routine
  557          */
  558 
  559         m->m_flags &= ~M_AUTHIPHDR;
  560         m->m_flags &= ~M_AUTHIPDGM;
  561 
  562 #endif /* IPSEC */
  563 
  564         if (m->m_flags & M_FASTFWD_OURS) {
  565                 /*
  566                  * Firewall changed destination to local.
  567                  */
  568                 ip6 = mtod(m, struct ip6_hdr *);
  569                 goto passin;
  570         }
  571 
  572         /*
  573          * mbuf statistics
  574          */
  575         if (m->m_flags & M_EXT) {
  576                 if (m->m_next)
  577                         IP6STAT_INC(ip6s_mext2m);
  578                 else
  579                         IP6STAT_INC(ip6s_mext1);
  580         } else {
  581                 if (m->m_next) {
  582                         struct ifnet *ifp = (m->m_flags & M_LOOP) ? V_loif : rcvif;
  583                         int ifindex = ifp->if_index;
  584                         if (ifindex >= IP6S_M2MMAX)
  585                                 ifindex = 0;
  586                         IP6STAT_INC(ip6s_m2m[ifindex]);
  587                 } else
  588                         IP6STAT_INC(ip6s_m1);
  589         }
  590 
  591         in6_ifstat_inc(rcvif, ifs6_in_receive);
  592         IP6STAT_INC(ip6s_total);
  593 
  594         /*
  595          * L2 bridge code and some other code can return mbuf chain
  596          * that does not conform to KAME requirement.  too bad.
  597          * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
  598          */
  599         if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
  600                 struct mbuf *n;
  601 
  602                 if (m->m_pkthdr.len > MHLEN)
  603                         n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
  604                 else
  605                         n = m_gethdr(M_NOWAIT, MT_DATA);
  606                 if (n == NULL)
  607                         goto bad;
  608 
  609                 m_move_pkthdr(n, m);
  610                 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
  611                 n->m_len = n->m_pkthdr.len;
  612                 m_freem(m);
  613                 m = n;
  614         }
  615         if (m->m_len < sizeof(struct ip6_hdr)) {
  616                 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
  617                         IP6STAT_INC(ip6s_toosmall);
  618                         in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
  619                         goto bad;
  620                 }
  621         }
  622 
  623         ip6 = mtod(m, struct ip6_hdr *);
  624         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
  625                 IP6STAT_INC(ip6s_badvers);
  626                 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
  627                 goto bad;
  628         }
  629 
  630         IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
  631         IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
  632 
  633         /*
  634          * Check against address spoofing/corruption.
  635          */
  636         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
  637             IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
  638                 /*
  639                  * XXX: "badscope" is not very suitable for a multicast source.
  640                  */
  641                 IP6STAT_INC(ip6s_badscope);
  642                 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
  643                 goto bad;
  644         }
  645         if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
  646             !(m->m_flags & M_LOOP)) {
  647                 /*
  648                  * In this case, the packet should come from the loopback
  649                  * interface.  However, we cannot just check the if_flags,
  650                  * because ip6_mloopback() passes the "actual" interface
  651                  * as the outgoing/incoming interface.
  652                  */
  653                 IP6STAT_INC(ip6s_badscope);
  654                 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
  655                 goto bad;
  656         }
  657         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
  658             IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
  659                 /*
  660                  * RFC4291 2.7:
  661                  * Nodes must not originate a packet to a multicast address
  662                  * whose scop field contains the reserved value 0; if such
  663                  * a packet is received, it must be silently dropped.
  664                  */
  665                 IP6STAT_INC(ip6s_badscope);
  666                 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
  667                 goto bad;
  668         }
  669 #ifdef ALTQ
  670         if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
  671                 /* packet is dropped by traffic conditioner */
  672                 return;
  673         }
  674 #endif
  675         /*
  676          * The following check is not documented in specs.  A malicious
  677          * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
  678          * and bypass security checks (act as if it was from 127.0.0.1 by using
  679          * IPv6 src ::ffff:127.0.0.1).  Be cautious.
  680          *
  681          * We have supported IPv6-only kernels for a few years and this issue
  682          * has not come up.  The world seems to move mostly towards not using
  683          * v4mapped on the wire, so it makes sense for us to keep rejecting
  684          * any such packets.
  685          */
  686         if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
  687             IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
  688                 IP6STAT_INC(ip6s_badscope);
  689                 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
  690                 goto bad;
  691         }
  692 #if 0
  693         /*
  694          * Reject packets with IPv4 compatible addresses (auto tunnel).
  695          *
  696          * The code forbids auto tunnel relay case in RFC1933 (the check is
  697          * stronger than RFC1933).  We may want to re-enable it if mech-xx
  698          * is revised to forbid relaying case.
  699          */
  700         if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
  701             IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
  702                 IP6STAT_INC(ip6s_badscope);
  703                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
  704                 goto bad;
  705         }
  706 #endif
  707         /*
  708          * Try to forward the packet, but if we fail continue.
  709          * ip6_tryforward() does not generate redirects, so fall
  710          * through to normal processing if redirects are required.
  711          * ip6_tryforward() does inbound and outbound packet firewall
  712          * processing. If firewall has decided that destination becomes
  713          * our local address, it sets M_FASTFWD_OURS flag. In this
  714          * case skip another inbound firewall processing and update
  715          * ip6 pointer.
  716          */
  717         if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
  718 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  719             && (!IPSEC_ENABLED(ipv6) ||
  720             IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
  721 #endif
  722             ) {
  723                 if ((m = ip6_tryforward(m)) == NULL)
  724                         return;
  725                 if (m->m_flags & M_FASTFWD_OURS) {
  726                         ip6 = mtod(m, struct ip6_hdr *);
  727                         goto passin;
  728                 }
  729         }
  730 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  731         /*
  732          * Bypass packet filtering for packets previously handled by IPsec.
  733          */
  734         if (IPSEC_ENABLED(ipv6) &&
  735             IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
  736                         goto passin;
  737 #endif
  738         /*
  739          * Run through list of hooks for input packets.
  740          *
  741          * NB: Beware of the destination address changing
  742          *     (e.g. by NAT rewriting).  When this happens,
  743          *     tell ip6_forward to do the right thing.
  744          */
  745 
  746         /* Jump over all PFIL processing if hooks are not active. */
  747         if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
  748                 goto passin;
  749 
  750         odst = ip6->ip6_dst;
  751         if (pfil_run_hooks(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
  752             NULL) != PFIL_PASS)
  753                 return;
  754         ip6 = mtod(m, struct ip6_hdr *);
  755         srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
  756         if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
  757             m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
  758                 /*
  759                  * Directly ship the packet on.  This allows forwarding
  760                  * packets originally destined to us to some other directly
  761                  * connected host.
  762                  */
  763                 ip6_forward(m, 1);
  764                 return;
  765         }
  766 
  767 passin:
  768         /*
  769          * Disambiguate address scope zones (if there is ambiguity).
  770          * We first make sure that the original source or destination address
  771          * is not in our internal form for scoped addresses.  Such addresses
  772          * are not necessarily invalid spec-wise, but we cannot accept them due
  773          * to the usage conflict.
  774          * in6_setscope() then also checks and rejects the cases where src or
  775          * dst are the loopback address and the receiving interface
  776          * is not loopback.
  777          */
  778         if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
  779                 IP6STAT_INC(ip6s_badscope); /* XXX */
  780                 goto bad;
  781         }
  782         if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
  783             in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
  784                 IP6STAT_INC(ip6s_badscope);
  785                 goto bad;
  786         }
  787         if (m->m_flags & M_FASTFWD_OURS) {
  788                 m->m_flags &= ~M_FASTFWD_OURS;
  789                 ours = 1;
  790                 goto hbhcheck;
  791         }
  792         /*
  793          * Multicast check. Assume packet is for us to avoid
  794          * prematurely taking locks.
  795          */
  796         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  797                 ours = 1;
  798                 in6_ifstat_inc(rcvif, ifs6_in_mcast);
  799                 goto hbhcheck;
  800         }
  801         /*
  802          * Unicast check
  803          * XXX: For now we keep link-local IPv6 addresses with embedded
  804          *      scope zone id, therefore we use zero zoneid here.
  805          */
  806         ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
  807         if (ia != NULL) {
  808                 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
  809                         char ip6bufs[INET6_ADDRSTRLEN];
  810                         char ip6bufd[INET6_ADDRSTRLEN];
  811                         /* address is not ready, so discard the packet. */
  812                         nd6log((LOG_INFO,
  813                             "ip6_input: packet to an unready address %s->%s\n",
  814                             ip6_sprintf(ip6bufs, &ip6->ip6_src),
  815                             ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
  816                         goto bad;
  817                 }
  818                 /* Count the packet in the ip address stats */
  819                 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
  820                 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
  821                 ours = 1;
  822                 goto hbhcheck;
  823         }
  824 
  825         /*
  826          * Now there is no reason to process the packet if it's not our own
  827          * and we're not a router.
  828          */
  829         if (!V_ip6_forwarding) {
  830                 IP6STAT_INC(ip6s_cantforward);
  831                 goto bad;
  832         }
  833 
  834   hbhcheck:
  835         /*
  836          * Process Hop-by-Hop options header if it's contained.
  837          * m may be modified in ip6_hopopts_input().
  838          * If a JumboPayload option is included, plen will also be modified.
  839          */
  840         plen = (u_int32_t)ntohs(ip6->ip6_plen);
  841         if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
  842                 if (ip6_input_hbh(&m, &plen, &rtalert, &off, &nxt, &ours) != 0)
  843                         return;
  844         } else
  845                 nxt = ip6->ip6_nxt;
  846 
  847         /*
  848          * Use mbuf flags to propagate Router Alert option to
  849          * ICMPv6 layer, as hop-by-hop options have been stripped.
  850          */
  851         if (rtalert != ~0)
  852                 m->m_flags |= M_RTALERT_MLD;
  853 
  854         /*
  855          * Check that the amount of data in the buffers
  856          * is as at least much as the IPv6 header would have us expect.
  857          * Trim mbufs if longer than we expect.
  858          * Drop packet if shorter than we expect.
  859          */
  860         if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
  861                 IP6STAT_INC(ip6s_tooshort);
  862                 in6_ifstat_inc(rcvif, ifs6_in_truncated);
  863                 goto bad;
  864         }
  865         if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
  866                 if (m->m_len == m->m_pkthdr.len) {
  867                         m->m_len = sizeof(struct ip6_hdr) + plen;
  868                         m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
  869                 } else
  870                         m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
  871         }
  872 
  873         /*
  874          * Forward if desirable.
  875          */
  876         if (V_ip6_mrouter &&
  877             IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  878                 /*
  879                  * If we are acting as a multicast router, all
  880                  * incoming multicast packets are passed to the
  881                  * kernel-level multicast forwarding function.
  882                  * The packet is returned (relatively) intact; if
  883                  * ip6_mforward() returns a non-zero value, the packet
  884                  * must be discarded, else it may be accepted below.
  885                  *
  886                  * XXX TODO: Check hlim and multicast scope here to avoid
  887                  * unnecessarily calling into ip6_mforward().
  888                  */
  889                 if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) {
  890                         IP6STAT_INC(ip6s_cantforward);
  891                         goto bad;
  892                 }
  893         } else if (!ours) {
  894                 ip6_forward(m, srcrt);
  895                 return;
  896         }
  897 
  898         /*
  899          * Tell launch routine the next header
  900          */
  901         IP6STAT_INC(ip6s_delivered);
  902         in6_ifstat_inc(rcvif, ifs6_in_deliver);
  903         nest = 0;
  904 
  905         while (nxt != IPPROTO_DONE) {
  906                 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
  907                         IP6STAT_INC(ip6s_toomanyhdr);
  908                         goto bad;
  909                 }
  910 
  911                 /*
  912                  * protection against faulty packet - there should be
  913                  * more sanity checks in header chain processing.
  914                  */
  915                 if (m->m_pkthdr.len < off) {
  916                         IP6STAT_INC(ip6s_tooshort);
  917                         in6_ifstat_inc(rcvif, ifs6_in_truncated);
  918                         goto bad;
  919                 }
  920 
  921 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  922                 if (IPSEC_ENABLED(ipv6)) {
  923                         if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
  924                                 return;
  925                 }
  926 #endif /* IPSEC */
  927 
  928                 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
  929         }
  930         return;
  931 bad:
  932         in6_ifstat_inc(rcvif, ifs6_in_discard);
  933         if (m != NULL)
  934                 m_freem(m);
  935 }
  936 
  937 /*
  938  * Hop-by-Hop options header processing. If a valid jumbo payload option is
  939  * included, the real payload length will be stored in plenp.
  940  *
  941  * rtalertp - XXX: should be stored more smart way
  942  */
  943 static int
  944 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
  945     struct mbuf **mp, int *offp)
  946 {
  947         struct mbuf *m = *mp;
  948         int off = *offp, hbhlen;
  949         struct ip6_hbh *hbh;
  950 
  951         /* validation of the length of the header */
  952         if (m->m_len < off + sizeof(*hbh)) {
  953                 m = m_pullup(m, off + sizeof(*hbh));
  954                 if (m == NULL) {
  955                         IP6STAT_INC(ip6s_exthdrtoolong);
  956                         *mp = NULL;
  957                         return (-1);
  958                 }
  959         }
  960         hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
  961         hbhlen = (hbh->ip6h_len + 1) << 3;
  962 
  963         if (m->m_len < off + hbhlen) {
  964                 m = m_pullup(m, off + hbhlen);
  965                 if (m == NULL) {
  966                         IP6STAT_INC(ip6s_exthdrtoolong);
  967                         *mp = NULL;
  968                         return (-1);
  969                 }
  970         }
  971         hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
  972         off += hbhlen;
  973         hbhlen -= sizeof(struct ip6_hbh);
  974         if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
  975                                 hbhlen, rtalertp, plenp) < 0) {
  976                 *mp = NULL;
  977                 return (-1);
  978         }
  979 
  980         *offp = off;
  981         *mp = m;
  982         return (0);
  983 }
  984 
  985 /*
  986  * Search header for all Hop-by-hop options and process each option.
  987  * This function is separate from ip6_hopopts_input() in order to
  988  * handle a case where the sending node itself process its hop-by-hop
  989  * options header. In such a case, the function is called from ip6_output().
  990  *
  991  * The function assumes that hbh header is located right after the IPv6 header
  992  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
  993  * opthead + hbhlen is located in contiguous memory region.
  994  */
  995 int
  996 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
  997     u_int32_t *rtalertp, u_int32_t *plenp)
  998 {
  999         struct ip6_hdr *ip6;
 1000         int optlen = 0;
 1001         u_int8_t *opt = opthead;
 1002         u_int16_t rtalert_val;
 1003         u_int32_t jumboplen;
 1004         const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
 1005 
 1006         for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
 1007                 switch (*opt) {
 1008                 case IP6OPT_PAD1:
 1009                         optlen = 1;
 1010                         break;
 1011                 case IP6OPT_PADN:
 1012                         if (hbhlen < IP6OPT_MINLEN) {
 1013                                 IP6STAT_INC(ip6s_toosmall);
 1014                                 goto bad;
 1015                         }
 1016                         optlen = *(opt + 1) + 2;
 1017                         break;
 1018                 case IP6OPT_ROUTER_ALERT:
 1019                         /* XXX may need check for alignment */
 1020                         if (hbhlen < IP6OPT_RTALERT_LEN) {
 1021                                 IP6STAT_INC(ip6s_toosmall);
 1022                                 goto bad;
 1023                         }
 1024                         if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
 1025                                 /* XXX stat */
 1026                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1027                                     ICMP6_PARAMPROB_HEADER,
 1028                                     erroff + opt + 1 - opthead);
 1029                                 return (-1);
 1030                         }
 1031                         optlen = IP6OPT_RTALERT_LEN;
 1032                         bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
 1033                         *rtalertp = ntohs(rtalert_val);
 1034                         break;
 1035                 case IP6OPT_JUMBO:
 1036                         /* XXX may need check for alignment */
 1037                         if (hbhlen < IP6OPT_JUMBO_LEN) {
 1038                                 IP6STAT_INC(ip6s_toosmall);
 1039                                 goto bad;
 1040                         }
 1041                         if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
 1042                                 /* XXX stat */
 1043                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1044                                     ICMP6_PARAMPROB_HEADER,
 1045                                     erroff + opt + 1 - opthead);
 1046                                 return (-1);
 1047                         }
 1048                         optlen = IP6OPT_JUMBO_LEN;
 1049 
 1050                         /*
 1051                          * IPv6 packets that have non 0 payload length
 1052                          * must not contain a jumbo payload option.
 1053                          */
 1054                         ip6 = mtod(m, struct ip6_hdr *);
 1055                         if (ip6->ip6_plen) {
 1056                                 IP6STAT_INC(ip6s_badoptions);
 1057                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1058                                     ICMP6_PARAMPROB_HEADER,
 1059                                     erroff + opt - opthead);
 1060                                 return (-1);
 1061                         }
 1062 
 1063                         /*
 1064                          * We may see jumbolen in unaligned location, so
 1065                          * we'd need to perform bcopy().
 1066                          */
 1067                         bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
 1068                         jumboplen = (u_int32_t)htonl(jumboplen);
 1069 
 1070 #if 1
 1071                         /*
 1072                          * if there are multiple jumbo payload options,
 1073                          * *plenp will be non-zero and the packet will be
 1074                          * rejected.
 1075                          * the behavior may need some debate in ipngwg -
 1076                          * multiple options does not make sense, however,
 1077                          * there's no explicit mention in specification.
 1078                          */
 1079                         if (*plenp != 0) {
 1080                                 IP6STAT_INC(ip6s_badoptions);
 1081                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1082                                     ICMP6_PARAMPROB_HEADER,
 1083                                     erroff + opt + 2 - opthead);
 1084                                 return (-1);
 1085                         }
 1086 #endif
 1087 
 1088                         /*
 1089                          * jumbo payload length must be larger than 65535.
 1090                          */
 1091                         if (jumboplen <= IPV6_MAXPACKET) {
 1092                                 IP6STAT_INC(ip6s_badoptions);
 1093                                 icmp6_error(m, ICMP6_PARAM_PROB,
 1094                                     ICMP6_PARAMPROB_HEADER,
 1095                                     erroff + opt + 2 - opthead);
 1096                                 return (-1);
 1097                         }
 1098                         *plenp = jumboplen;
 1099 
 1100                         break;
 1101                 default:                /* unknown option */
 1102                         if (hbhlen < IP6OPT_MINLEN) {
 1103                                 IP6STAT_INC(ip6s_toosmall);
 1104                                 goto bad;
 1105                         }
 1106                         optlen = ip6_unknown_opt(opt, m,
 1107                             erroff + opt - opthead);
 1108                         if (optlen == -1)
 1109                                 return (-1);
 1110                         optlen += 2;
 1111                         break;
 1112                 }
 1113         }
 1114 
 1115         return (0);
 1116 
 1117   bad:
 1118         m_freem(m);
 1119         return (-1);
 1120 }
 1121 
 1122 /*
 1123  * Unknown option processing.
 1124  * The third argument `off' is the offset from the IPv6 header to the option,
 1125  * which is necessary if the IPv6 header the and option header and IPv6 header
 1126  * is not contiguous in order to return an ICMPv6 error.
 1127  */
 1128 int
 1129 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
 1130 {
 1131         struct ip6_hdr *ip6;
 1132 
 1133         switch (IP6OPT_TYPE(*optp)) {
 1134         case IP6OPT_TYPE_SKIP: /* ignore the option */
 1135                 return ((int)*(optp + 1));
 1136         case IP6OPT_TYPE_DISCARD:       /* silently discard */
 1137                 m_freem(m);
 1138                 return (-1);
 1139         case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
 1140                 IP6STAT_INC(ip6s_badoptions);
 1141                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
 1142                 return (-1);
 1143         case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
 1144                 IP6STAT_INC(ip6s_badoptions);
 1145                 ip6 = mtod(m, struct ip6_hdr *);
 1146                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
 1147                     (m->m_flags & (M_BCAST|M_MCAST)))
 1148                         m_freem(m);
 1149                 else
 1150                         icmp6_error(m, ICMP6_PARAM_PROB,
 1151                                     ICMP6_PARAMPROB_OPTION, off);
 1152                 return (-1);
 1153         }
 1154 
 1155         m_freem(m);             /* XXX: NOTREACHED */
 1156         return (-1);
 1157 }
 1158 
 1159 /*
 1160  * Create the "control" list for this pcb.
 1161  * These functions will not modify mbuf chain at all.
 1162  *
 1163  * The routine will be called from upper layer handlers like tcp6_input().
 1164  * Thus the routine assumes that the caller (tcp6_input) have already
 1165  * called m_pullup() and all the extension headers are located in the
 1166  * very first mbuf on the mbuf chain.
 1167  *
 1168  * ip6_savecontrol_v4 will handle those options that are possible to be
 1169  * set on a v4-mapped socket.
 1170  * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
 1171  * options and handle the v6-only ones itself.
 1172  */
 1173 struct mbuf **
 1174 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
 1175     int *v4only)
 1176 {
 1177         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1178 
 1179 #ifdef SO_TIMESTAMP
 1180         if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
 1181                 union {
 1182                         struct timeval tv;
 1183                         struct bintime bt;
 1184                         struct timespec ts;
 1185                 } t;
 1186                 struct bintime boottimebin, bt1;
 1187                 struct timespec ts1;
 1188                 bool stamped;
 1189 
 1190                 stamped = false;
 1191                 switch (inp->inp_socket->so_ts_clock) {
 1192                 case SO_TS_REALTIME_MICRO:
 1193                         if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
 1194                             M_TSTMP)) {
 1195                                 mbuf_tstmp2timespec(m, &ts1);
 1196                                 timespec2bintime(&ts1, &bt1);
 1197                                 getboottimebin(&boottimebin);
 1198                                 bintime_add(&bt1, &boottimebin);
 1199                                 bintime2timeval(&bt1, &t.tv);
 1200                         } else {
 1201                                 microtime(&t.tv);
 1202                         }
 1203                         *mp = sbcreatecontrol((caddr_t) &t.tv, sizeof(t.tv),
 1204                             SCM_TIMESTAMP, SOL_SOCKET);
 1205                         if (*mp != NULL) {
 1206                                 mp = &(*mp)->m_next;
 1207                                 stamped = true;
 1208                         }
 1209                         break;
 1210 
 1211                 case SO_TS_BINTIME:
 1212                         if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
 1213                             M_TSTMP)) {
 1214                                 mbuf_tstmp2timespec(m, &ts1);
 1215                                 timespec2bintime(&ts1, &t.bt);
 1216                                 getboottimebin(&boottimebin);
 1217                                 bintime_add(&t.bt, &boottimebin);
 1218                         } else {
 1219                                 bintime(&t.bt);
 1220                         }
 1221                         *mp = sbcreatecontrol((caddr_t)&t.bt, sizeof(t.bt),
 1222                             SCM_BINTIME, SOL_SOCKET);
 1223                         if (*mp != NULL) {
 1224                                 mp = &(*mp)->m_next;
 1225                                 stamped = true;
 1226                         }
 1227                         break;
 1228 
 1229                 case SO_TS_REALTIME:
 1230                         if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
 1231                             M_TSTMP)) {
 1232                                 mbuf_tstmp2timespec(m, &t.ts);
 1233                                 getboottimebin(&boottimebin);
 1234                                 bintime2timespec(&boottimebin, &ts1);
 1235                                 timespecadd(&t.ts, &ts1, &t.ts);
 1236                         } else {
 1237                                 nanotime(&t.ts);
 1238                         }
 1239                         *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
 1240                             SCM_REALTIME, SOL_SOCKET);
 1241                         if (*mp != NULL) {
 1242                                 mp = &(*mp)->m_next;
 1243                                 stamped = true;
 1244                         }
 1245                         break;
 1246 
 1247                 case SO_TS_MONOTONIC:
 1248                         if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
 1249                             M_TSTMP))
 1250                                 mbuf_tstmp2timespec(m, &t.ts);
 1251                         else
 1252                                 nanouptime(&t.ts);
 1253                         *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
 1254                             SCM_MONOTONIC, SOL_SOCKET);
 1255                         if (*mp != NULL) {
 1256                                 mp = &(*mp)->m_next;
 1257                                 stamped = true;
 1258                         }
 1259                         break;
 1260 
 1261                 default:
 1262                         panic("unknown (corrupted) so_ts_clock");
 1263                 }
 1264                 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) ==
 1265                     (M_PKTHDR | M_TSTMP)) {
 1266                         struct sock_timestamp_info sti;
 1267 
 1268                         bzero(&sti, sizeof(sti));
 1269                         sti.st_info_flags = ST_INFO_HW;
 1270                         if ((m->m_flags & M_TSTMP_HPREC) != 0)
 1271                                 sti.st_info_flags |= ST_INFO_HW_HPREC;
 1272                         *mp = sbcreatecontrol((caddr_t)&sti, sizeof(sti),
 1273                             SCM_TIME_INFO, SOL_SOCKET);
 1274                         if (*mp != NULL)
 1275                                 mp = &(*mp)->m_next;
 1276                 }
 1277         }
 1278 #endif
 1279 
 1280 #define IS2292(inp, x, y)       (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
 1281         /* RFC 2292 sec. 5 */
 1282         if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
 1283                 struct in6_pktinfo pi6;
 1284 
 1285                 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 1286 #ifdef INET
 1287                         struct ip *ip;
 1288 
 1289                         ip = mtod(m, struct ip *);
 1290                         pi6.ipi6_addr.s6_addr32[0] = 0;
 1291                         pi6.ipi6_addr.s6_addr32[1] = 0;
 1292                         pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
 1293                         pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
 1294 #else
 1295                         /* We won't hit this code */
 1296                         bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
 1297 #endif
 1298                 } else {        
 1299                         bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
 1300                         in6_clearscope(&pi6.ipi6_addr); /* XXX */
 1301                 }
 1302                 pi6.ipi6_ifindex =
 1303                     (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
 1304 
 1305                 *mp = sbcreatecontrol((caddr_t) &pi6,
 1306                     sizeof(struct in6_pktinfo),
 1307                     IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
 1308                 if (*mp)
 1309                         mp = &(*mp)->m_next;
 1310         }
 1311 
 1312         if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
 1313                 int hlim;
 1314 
 1315                 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 1316 #ifdef INET
 1317                         struct ip *ip;
 1318 
 1319                         ip = mtod(m, struct ip *);
 1320                         hlim = ip->ip_ttl;
 1321 #else
 1322                         /* We won't hit this code */
 1323                         hlim = 0;
 1324 #endif
 1325                 } else {
 1326                         hlim = ip6->ip6_hlim & 0xff;
 1327                 }
 1328                 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
 1329                     IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
 1330                     IPPROTO_IPV6);
 1331                 if (*mp)
 1332                         mp = &(*mp)->m_next;
 1333         }
 1334 
 1335         if ((inp->inp_flags & IN6P_TCLASS) != 0) {
 1336                 int tclass;
 1337 
 1338                 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 1339 #ifdef INET
 1340                         struct ip *ip;
 1341 
 1342                         ip = mtod(m, struct ip *);
 1343                         tclass = ip->ip_tos;
 1344 #else
 1345                         /* We won't hit this code */
 1346                         tclass = 0;
 1347 #endif
 1348                 } else {
 1349                         u_int32_t flowinfo;
 1350 
 1351                         flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
 1352                         flowinfo >>= 20;
 1353                         tclass = flowinfo & 0xff;
 1354                 }
 1355                 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
 1356                     IPV6_TCLASS, IPPROTO_IPV6);
 1357                 if (*mp)
 1358                         mp = &(*mp)->m_next;
 1359         }
 1360 
 1361         if (v4only != NULL) {
 1362                 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 1363                         *v4only = 1;
 1364                 } else {
 1365                         *v4only = 0;
 1366                 }
 1367         }
 1368 
 1369         return (mp);
 1370 }
 1371 
 1372 void
 1373 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp)
 1374 {
 1375         struct ip6_hdr *ip6;
 1376         int v4only = 0;
 1377 
 1378         mp = ip6_savecontrol_v4(inp, m, mp, &v4only);
 1379         if (v4only)
 1380                 return;
 1381 
 1382         ip6 = mtod(m, struct ip6_hdr *);
 1383         /*
 1384          * IPV6_HOPOPTS socket option.  Recall that we required super-user
 1385          * privilege for the option (see ip6_ctloutput), but it might be too
 1386          * strict, since there might be some hop-by-hop options which can be
 1387          * returned to normal user.
 1388          * See also RFC 2292 section 6 (or RFC 3542 section 8).
 1389          */
 1390         if ((inp->inp_flags & IN6P_HOPOPTS) != 0) {
 1391                 /*
 1392                  * Check if a hop-by-hop options header is contatined in the
 1393                  * received packet, and if so, store the options as ancillary
 1394                  * data. Note that a hop-by-hop options header must be
 1395                  * just after the IPv6 header, which is assured through the
 1396                  * IPv6 input processing.
 1397                  */
 1398                 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
 1399                         struct ip6_hbh *hbh;
 1400                         int hbhlen;
 1401 
 1402                         hbh = (struct ip6_hbh *)(ip6 + 1);
 1403                         hbhlen = (hbh->ip6h_len + 1) << 3;
 1404 
 1405                         /*
 1406                          * XXX: We copy the whole header even if a
 1407                          * jumbo payload option is included, the option which
 1408                          * is to be removed before returning according to
 1409                          * RFC2292.
 1410                          * Note: this constraint is removed in RFC3542
 1411                          */
 1412                         *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
 1413                             IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
 1414                             IPPROTO_IPV6);
 1415                         if (*mp)
 1416                                 mp = &(*mp)->m_next;
 1417                 }
 1418         }
 1419 
 1420         if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
 1421                 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
 1422 
 1423                 /*
 1424                  * Search for destination options headers or routing
 1425                  * header(s) through the header chain, and stores each
 1426                  * header as ancillary data.
 1427                  * Note that the order of the headers remains in
 1428                  * the chain of ancillary data.
 1429                  */
 1430                 while (1) {     /* is explicit loop prevention necessary? */
 1431                         struct ip6_ext *ip6e = NULL;
 1432                         int elen;
 1433 
 1434                         /*
 1435                          * if it is not an extension header, don't try to
 1436                          * pull it from the chain.
 1437                          */
 1438                         switch (nxt) {
 1439                         case IPPROTO_DSTOPTS:
 1440                         case IPPROTO_ROUTING:
 1441                         case IPPROTO_HOPOPTS:
 1442                         case IPPROTO_AH: /* is it possible? */
 1443                                 break;
 1444                         default:
 1445                                 goto loopend;
 1446                         }
 1447 
 1448                         if (off + sizeof(*ip6e) > m->m_len)
 1449                                 goto loopend;
 1450                         ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
 1451                         if (nxt == IPPROTO_AH)
 1452                                 elen = (ip6e->ip6e_len + 2) << 2;
 1453                         else
 1454                                 elen = (ip6e->ip6e_len + 1) << 3;
 1455                         if (off + elen > m->m_len)
 1456                                 goto loopend;
 1457 
 1458                         switch (nxt) {
 1459                         case IPPROTO_DSTOPTS:
 1460                                 if (!(inp->inp_flags & IN6P_DSTOPTS))
 1461                                         break;
 1462 
 1463                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
 1464                                     IS2292(inp,
 1465                                         IPV6_2292DSTOPTS, IPV6_DSTOPTS),
 1466                                     IPPROTO_IPV6);
 1467                                 if (*mp)
 1468                                         mp = &(*mp)->m_next;
 1469                                 break;
 1470                         case IPPROTO_ROUTING:
 1471                                 if (!(inp->inp_flags & IN6P_RTHDR))
 1472                                         break;
 1473 
 1474                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
 1475                                     IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR),
 1476                                     IPPROTO_IPV6);
 1477                                 if (*mp)
 1478                                         mp = &(*mp)->m_next;
 1479                                 break;
 1480                         case IPPROTO_HOPOPTS:
 1481                         case IPPROTO_AH: /* is it possible? */
 1482                                 break;
 1483 
 1484                         default:
 1485                                 /*
 1486                                  * other cases have been filtered in the above.
 1487                                  * none will visit this case.  here we supply
 1488                                  * the code just in case (nxt overwritten or
 1489                                  * other cases).
 1490                                  */
 1491                                 goto loopend;
 1492                         }
 1493 
 1494                         /* proceed with the next header. */
 1495                         off += elen;
 1496                         nxt = ip6e->ip6e_nxt;
 1497                         ip6e = NULL;
 1498                 }
 1499           loopend:
 1500                 ;
 1501         }
 1502 
 1503         if (inp->inp_flags2 & INP_RECVFLOWID) {
 1504                 uint32_t flowid, flow_type;
 1505 
 1506                 flowid = m->m_pkthdr.flowid;
 1507                 flow_type = M_HASHTYPE_GET(m);
 1508 
 1509                 /*
 1510                  * XXX should handle the failure of one or the
 1511                  * other - don't populate both?
 1512                  */
 1513                 *mp = sbcreatecontrol((caddr_t) &flowid,
 1514                     sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
 1515                 if (*mp)
 1516                         mp = &(*mp)->m_next;
 1517                 *mp = sbcreatecontrol((caddr_t) &flow_type,
 1518                     sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
 1519                 if (*mp)
 1520                         mp = &(*mp)->m_next;
 1521         }
 1522 
 1523 #ifdef  RSS
 1524         if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
 1525                 uint32_t flowid, flow_type;
 1526                 uint32_t rss_bucketid;
 1527 
 1528                 flowid = m->m_pkthdr.flowid;
 1529                 flow_type = M_HASHTYPE_GET(m);
 1530 
 1531                 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
 1532                         *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
 1533                            sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
 1534                         if (*mp)
 1535                                 mp = &(*mp)->m_next;
 1536                 }
 1537         }
 1538 #endif
 1539 
 1540 }
 1541 #undef IS2292
 1542 
 1543 void
 1544 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
 1545 {
 1546         struct socket *so;
 1547         struct mbuf *m_mtu;
 1548         struct ip6_mtuinfo mtuctl;
 1549 
 1550         KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
 1551         /*
 1552          * Notify the error by sending IPV6_PATHMTU ancillary data if
 1553          * application wanted to know the MTU value.
 1554          * NOTE: we notify disconnected sockets, because some udp
 1555          * applications keep sending sockets disconnected.
 1556          * NOTE: our implementation doesn't notify connected sockets that has
 1557          * foreign address that is different than given destination addresses
 1558          * (this is permitted by RFC 3542).
 1559          */
 1560         if ((inp->inp_flags & IN6P_MTU) == 0 || (
 1561             !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
 1562             !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
 1563                 return;
 1564 
 1565         mtuctl.ip6m_mtu = mtu;
 1566         mtuctl.ip6m_addr = *dst;
 1567         if (sa6_recoverscope(&mtuctl.ip6m_addr))
 1568                 return;
 1569 
 1570         if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
 1571             IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
 1572                 return;
 1573 
 1574         so =  inp->inp_socket;
 1575         if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
 1576             == 0) {
 1577                 soroverflow(so);
 1578                 m_freem(m_mtu);
 1579                 /* XXX: should count statistics */
 1580         } else
 1581                 sorwakeup(so);
 1582 }
 1583 
 1584 /*
 1585  * Get pointer to the previous header followed by the header
 1586  * currently processed.
 1587  */
 1588 int
 1589 ip6_get_prevhdr(const struct mbuf *m, int off)
 1590 {
 1591         struct ip6_ext ip6e;
 1592         struct ip6_hdr *ip6;
 1593         int len, nlen, nxt;
 1594 
 1595         if (off == sizeof(struct ip6_hdr))
 1596                 return (offsetof(struct ip6_hdr, ip6_nxt));
 1597         if (off < sizeof(struct ip6_hdr))
 1598                 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
 1599 
 1600         ip6 = mtod(m, struct ip6_hdr *);
 1601         nxt = ip6->ip6_nxt;
 1602         len = sizeof(struct ip6_hdr);
 1603         nlen = 0;
 1604         while (len < off) {
 1605                 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
 1606                 switch (nxt) {
 1607                 case IPPROTO_FRAGMENT:
 1608                         nlen = sizeof(struct ip6_frag);
 1609                         break;
 1610                 case IPPROTO_AH:
 1611                         nlen = (ip6e.ip6e_len + 2) << 2;
 1612                         break;
 1613                 default:
 1614                         nlen = (ip6e.ip6e_len + 1) << 3;
 1615                 }
 1616                 len += nlen;
 1617                 nxt = ip6e.ip6e_nxt;
 1618         }
 1619         return (len - nlen);
 1620 }
 1621 
 1622 /*
 1623  * get next header offset.  m will be retained.
 1624  */
 1625 int
 1626 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
 1627 {
 1628         struct ip6_hdr ip6;
 1629         struct ip6_ext ip6e;
 1630         struct ip6_frag fh;
 1631 
 1632         /* just in case */
 1633         if (m == NULL)
 1634                 panic("ip6_nexthdr: m == NULL");
 1635         if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
 1636                 return -1;
 1637 
 1638         switch (proto) {
 1639         case IPPROTO_IPV6:
 1640                 if (m->m_pkthdr.len < off + sizeof(ip6))
 1641                         return -1;
 1642                 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
 1643                 if (nxtp)
 1644                         *nxtp = ip6.ip6_nxt;
 1645                 off += sizeof(ip6);
 1646                 return off;
 1647 
 1648         case IPPROTO_FRAGMENT:
 1649                 /*
 1650                  * terminate parsing if it is not the first fragment,
 1651                  * it does not make sense to parse through it.
 1652                  */
 1653                 if (m->m_pkthdr.len < off + sizeof(fh))
 1654                         return -1;
 1655                 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
 1656                 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
 1657                 if (fh.ip6f_offlg & IP6F_OFF_MASK)
 1658                         return -1;
 1659                 if (nxtp)
 1660                         *nxtp = fh.ip6f_nxt;
 1661                 off += sizeof(struct ip6_frag);
 1662                 return off;
 1663 
 1664         case IPPROTO_AH:
 1665                 if (m->m_pkthdr.len < off + sizeof(ip6e))
 1666                         return -1;
 1667                 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
 1668                 if (nxtp)
 1669                         *nxtp = ip6e.ip6e_nxt;
 1670                 off += (ip6e.ip6e_len + 2) << 2;
 1671                 return off;
 1672 
 1673         case IPPROTO_HOPOPTS:
 1674         case IPPROTO_ROUTING:
 1675         case IPPROTO_DSTOPTS:
 1676                 if (m->m_pkthdr.len < off + sizeof(ip6e))
 1677                         return -1;
 1678                 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
 1679                 if (nxtp)
 1680                         *nxtp = ip6e.ip6e_nxt;
 1681                 off += (ip6e.ip6e_len + 1) << 3;
 1682                 return off;
 1683 
 1684         case IPPROTO_NONE:
 1685         case IPPROTO_ESP:
 1686         case IPPROTO_IPCOMP:
 1687                 /* give up */
 1688                 return -1;
 1689 
 1690         default:
 1691                 return -1;
 1692         }
 1693 
 1694         /* NOTREACHED */
 1695 }
 1696 
 1697 /*
 1698  * get offset for the last header in the chain.  m will be kept untainted.
 1699  */
 1700 int
 1701 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
 1702 {
 1703         int newoff;
 1704         int nxt;
 1705 
 1706         if (!nxtp) {
 1707                 nxt = -1;
 1708                 nxtp = &nxt;
 1709         }
 1710         while (1) {
 1711                 newoff = ip6_nexthdr(m, off, proto, nxtp);
 1712                 if (newoff < 0)
 1713                         return off;
 1714                 else if (newoff < off)
 1715                         return -1;      /* invalid */
 1716                 else if (newoff == off)
 1717                         return newoff;
 1718 
 1719                 off = newoff;
 1720                 proto = *nxtp;
 1721         }
 1722 }
 1723 
 1724 /*
 1725  * System control for IP6
 1726  */
 1727 
 1728 u_char  inet6ctlerrmap[PRC_NCMDS] = {
 1729         0,              0,              0,              0,
 1730         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
 1731         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
 1732         EMSGSIZE,       EHOSTUNREACH,   0,              0,
 1733         0,              0,              EHOSTUNREACH,   0,
 1734         ENOPROTOOPT,    ECONNREFUSED
 1735 };

Cache object: 2c1fbc278aca276b07317b851c7409c5


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