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/icmp6.c

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

    1 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1982, 1986, 1988, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 4. Neither the name of the University nor the names of its contributors
   45  *    may be used to endorse or promote products derived from this software
   46  *    without specific prior written permission.
   47  *
   48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   58  * SUCH DAMAGE.
   59  *
   60  *      @(#)ip_icmp.c   8.2 (Berkeley) 1/4/94
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __FBSDID("$FreeBSD: releng/10.1/sys/netinet6/icmp6.c 257961 2013-11-11 10:43:39Z ae $");
   65 
   66 #include "opt_inet.h"
   67 #include "opt_inet6.h"
   68 #include "opt_ipsec.h"
   69 
   70 #include <sys/param.h>
   71 #include <sys/domain.h>
   72 #include <sys/jail.h>
   73 #include <sys/kernel.h>
   74 #include <sys/lock.h>
   75 #include <sys/malloc.h>
   76 #include <sys/mbuf.h>
   77 #include <sys/proc.h>
   78 #include <sys/protosw.h>
   79 #include <sys/signalvar.h>
   80 #include <sys/socket.h>
   81 #include <sys/socketvar.h>
   82 #include <sys/sx.h>
   83 #include <sys/syslog.h>
   84 #include <sys/systm.h>
   85 #include <sys/time.h>
   86 
   87 #include <net/if.h>
   88 #include <net/if_dl.h>
   89 #include <net/if_llatbl.h>
   90 #include <net/if_types.h>
   91 #include <net/route.h>
   92 #include <net/vnet.h>
   93 
   94 #include <netinet/in.h>
   95 #include <netinet/in_pcb.h>
   96 #include <netinet/in_var.h>
   97 #include <netinet/ip6.h>
   98 #include <netinet/icmp6.h>
   99 #include <netinet/tcp_var.h>
  100 
  101 #include <netinet6/in6_ifattach.h>
  102 #include <netinet6/in6_pcb.h>
  103 #include <netinet6/ip6protosw.h>
  104 #include <netinet6/ip6_var.h>
  105 #include <netinet6/scope6_var.h>
  106 #include <netinet6/mld6_var.h>
  107 #include <netinet6/nd6.h>
  108 #include <netinet6/send.h>
  109 
  110 #ifdef IPSEC
  111 #include <netipsec/ipsec.h>
  112 #include <netipsec/key.h>
  113 #endif
  114 
  115 extern struct domain inet6domain;
  116 
  117 VNET_PCPUSTAT_DEFINE(struct icmp6stat, icmp6stat);
  118 VNET_PCPUSTAT_SYSINIT(icmp6stat);
  119 
  120 #ifdef VIMAGE
  121 VNET_PCPUSTAT_SYSUNINIT(icmp6stat);
  122 #endif /* VIMAGE */
  123 
  124 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
  125 VNET_DECLARE(struct inpcbhead, ripcb);
  126 VNET_DECLARE(int, icmp6errppslim);
  127 static VNET_DEFINE(int, icmp6errpps_count) = 0;
  128 static VNET_DEFINE(struct timeval, icmp6errppslim_last);
  129 VNET_DECLARE(int, icmp6_nodeinfo);
  130 
  131 #define V_ripcbinfo                     VNET(ripcbinfo)
  132 #define V_ripcb                         VNET(ripcb)
  133 #define V_icmp6errppslim                VNET(icmp6errppslim)
  134 #define V_icmp6errpps_count             VNET(icmp6errpps_count)
  135 #define V_icmp6errppslim_last           VNET(icmp6errppslim_last)
  136 #define V_icmp6_nodeinfo                VNET(icmp6_nodeinfo)
  137 
  138 static void icmp6_errcount(int, int);
  139 static int icmp6_rip6_input(struct mbuf **, int);
  140 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
  141 static const char *icmp6_redirect_diag(struct in6_addr *,
  142         struct in6_addr *, struct in6_addr *);
  143 static struct mbuf *ni6_input(struct mbuf *, int);
  144 static struct mbuf *ni6_nametodns(const char *, int, int);
  145 static int ni6_dnsmatch(const char *, int, const char *, int);
  146 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
  147                           struct ifnet **, struct in6_addr *);
  148 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
  149                                 struct ifnet *, int);
  150 static int icmp6_notify_error(struct mbuf **, int, int, int);
  151 
  152 /*
  153  * Kernel module interface for updating icmp6stat.  The argument is an index
  154  * into icmp6stat treated as an array of u_quad_t.  While this encodes the
  155  * general layout of icmp6stat into the caller, it doesn't encode its
  156  * location, so that future changes to add, for example, per-CPU stats
  157  * support won't cause binary compatibility problems for kernel modules.
  158  */
  159 void
  160 kmod_icmp6stat_inc(int statnum)
  161 {
  162 
  163         counter_u64_add(VNET(icmp6stat)[statnum], 1);
  164 }
  165 
  166 static void
  167 icmp6_errcount(int type, int code)
  168 {
  169         switch (type) {
  170         case ICMP6_DST_UNREACH:
  171                 switch (code) {
  172                 case ICMP6_DST_UNREACH_NOROUTE:
  173                         ICMP6STAT_INC(icp6s_odst_unreach_noroute);
  174                         return;
  175                 case ICMP6_DST_UNREACH_ADMIN:
  176                         ICMP6STAT_INC(icp6s_odst_unreach_admin);
  177                         return;
  178                 case ICMP6_DST_UNREACH_BEYONDSCOPE:
  179                         ICMP6STAT_INC(icp6s_odst_unreach_beyondscope);
  180                         return;
  181                 case ICMP6_DST_UNREACH_ADDR:
  182                         ICMP6STAT_INC(icp6s_odst_unreach_addr);
  183                         return;
  184                 case ICMP6_DST_UNREACH_NOPORT:
  185                         ICMP6STAT_INC(icp6s_odst_unreach_noport);
  186                         return;
  187                 }
  188                 break;
  189         case ICMP6_PACKET_TOO_BIG:
  190                 ICMP6STAT_INC(icp6s_opacket_too_big);
  191                 return;
  192         case ICMP6_TIME_EXCEEDED:
  193                 switch (code) {
  194                 case ICMP6_TIME_EXCEED_TRANSIT:
  195                         ICMP6STAT_INC(icp6s_otime_exceed_transit);
  196                         return;
  197                 case ICMP6_TIME_EXCEED_REASSEMBLY:
  198                         ICMP6STAT_INC(icp6s_otime_exceed_reassembly);
  199                         return;
  200                 }
  201                 break;
  202         case ICMP6_PARAM_PROB:
  203                 switch (code) {
  204                 case ICMP6_PARAMPROB_HEADER:
  205                         ICMP6STAT_INC(icp6s_oparamprob_header);
  206                         return;
  207                 case ICMP6_PARAMPROB_NEXTHEADER:
  208                         ICMP6STAT_INC(icp6s_oparamprob_nextheader);
  209                         return;
  210                 case ICMP6_PARAMPROB_OPTION:
  211                         ICMP6STAT_INC(icp6s_oparamprob_option);
  212                         return;
  213                 }
  214                 break;
  215         case ND_REDIRECT:
  216                 ICMP6STAT_INC(icp6s_oredirect);
  217                 return;
  218         }
  219         ICMP6STAT_INC(icp6s_ounknown);
  220 }
  221 
  222 /*
  223  * A wrapper function for icmp6_error() necessary when the erroneous packet
  224  * may not contain enough scope zone information.
  225  */
  226 void
  227 icmp6_error2(struct mbuf *m, int type, int code, int param,
  228     struct ifnet *ifp)
  229 {
  230         struct ip6_hdr *ip6;
  231 
  232         if (ifp == NULL)
  233                 return;
  234 
  235 #ifndef PULLDOWN_TEST
  236         IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
  237 #else
  238         if (m->m_len < sizeof(struct ip6_hdr)) {
  239                 m = m_pullup(m, sizeof(struct ip6_hdr));
  240                 if (m == NULL)
  241                         return;
  242         }
  243 #endif
  244 
  245         ip6 = mtod(m, struct ip6_hdr *);
  246 
  247         if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
  248                 return;
  249         if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
  250                 return;
  251 
  252         icmp6_error(m, type, code, param);
  253 }
  254 
  255 /*
  256  * Generate an error packet of type error in response to bad IP6 packet.
  257  */
  258 void
  259 icmp6_error(struct mbuf *m, int type, int code, int param)
  260 {
  261         struct ip6_hdr *oip6, *nip6;
  262         struct icmp6_hdr *icmp6;
  263         u_int preplen;
  264         int off;
  265         int nxt;
  266 
  267         ICMP6STAT_INC(icp6s_error);
  268 
  269         /* count per-type-code statistics */
  270         icmp6_errcount(type, code);
  271 
  272 #ifdef M_DECRYPTED      /*not openbsd*/
  273         if (m->m_flags & M_DECRYPTED) {
  274                 ICMP6STAT_INC(icp6s_canterror);
  275                 goto freeit;
  276         }
  277 #endif
  278 
  279 #ifndef PULLDOWN_TEST
  280         IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
  281 #else
  282         if (m->m_len < sizeof(struct ip6_hdr)) {
  283                 m = m_pullup(m, sizeof(struct ip6_hdr));
  284                 if (m == NULL)
  285                         return;
  286         }
  287 #endif
  288         oip6 = mtod(m, struct ip6_hdr *);
  289 
  290         /*
  291          * If the destination address of the erroneous packet is a multicast
  292          * address, or the packet was sent using link-layer multicast,
  293          * we should basically suppress sending an error (RFC 2463, Section
  294          * 2.4).
  295          * We have two exceptions (the item e.2 in that section):
  296          * - the Packet Too Big message can be sent for path MTU discovery.
  297          * - the Parameter Problem Message that can be allowed an icmp6 error
  298          *   in the option type field.  This check has been done in
  299          *   ip6_unknown_opt(), so we can just check the type and code.
  300          */
  301         if ((m->m_flags & (M_BCAST|M_MCAST) ||
  302              IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
  303             (type != ICMP6_PACKET_TOO_BIG &&
  304              (type != ICMP6_PARAM_PROB ||
  305               code != ICMP6_PARAMPROB_OPTION)))
  306                 goto freeit;
  307 
  308         /*
  309          * RFC 2463, 2.4 (e.5): source address check.
  310          * XXX: the case of anycast source?
  311          */
  312         if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
  313             IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
  314                 goto freeit;
  315 
  316         /*
  317          * If we are about to send ICMPv6 against ICMPv6 error/redirect,
  318          * don't do it.
  319          */
  320         nxt = -1;
  321         off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
  322         if (off >= 0 && nxt == IPPROTO_ICMPV6) {
  323                 struct icmp6_hdr *icp;
  324 
  325 #ifndef PULLDOWN_TEST
  326                 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
  327                 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
  328 #else
  329                 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
  330                         sizeof(*icp));
  331                 if (icp == NULL) {
  332                         ICMP6STAT_INC(icp6s_tooshort);
  333                         return;
  334                 }
  335 #endif
  336                 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
  337                     icp->icmp6_type == ND_REDIRECT) {
  338                         /*
  339                          * ICMPv6 error
  340                          * Special case: for redirect (which is
  341                          * informational) we must not send icmp6 error.
  342                          */
  343                         ICMP6STAT_INC(icp6s_canterror);
  344                         goto freeit;
  345                 } else {
  346                         /* ICMPv6 informational - send the error */
  347                 }
  348         } else {
  349                 /* non-ICMPv6 - send the error */
  350         }
  351 
  352         oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
  353 
  354         /* Finally, do rate limitation check. */
  355         if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
  356                 ICMP6STAT_INC(icp6s_toofreq);
  357                 goto freeit;
  358         }
  359 
  360         /*
  361          * OK, ICMP6 can be generated.
  362          */
  363 
  364         if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
  365                 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
  366 
  367         preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
  368         M_PREPEND(m, preplen, M_NOWAIT);        /* FIB is also copied over. */
  369         if (m && m->m_len < preplen)
  370                 m = m_pullup(m, preplen);
  371         if (m == NULL) {
  372                 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
  373                 return;
  374         }
  375 
  376         nip6 = mtod(m, struct ip6_hdr *);
  377         nip6->ip6_src  = oip6->ip6_src;
  378         nip6->ip6_dst  = oip6->ip6_dst;
  379 
  380         in6_clearscope(&oip6->ip6_src);
  381         in6_clearscope(&oip6->ip6_dst);
  382 
  383         icmp6 = (struct icmp6_hdr *)(nip6 + 1);
  384         icmp6->icmp6_type = type;
  385         icmp6->icmp6_code = code;
  386         icmp6->icmp6_pptr = htonl((u_int32_t)param);
  387 
  388         /*
  389          * icmp6_reflect() is designed to be in the input path.
  390          * icmp6_error() can be called from both input and output path,
  391          * and if we are in output path rcvif could contain bogus value.
  392          * clear m->m_pkthdr.rcvif for safety, we should have enough scope
  393          * information in ip header (nip6).
  394          */
  395         m->m_pkthdr.rcvif = NULL;
  396 
  397         ICMP6STAT_INC(icp6s_outhist[type]);
  398         icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
  399 
  400         return;
  401 
  402   freeit:
  403         /*
  404          * If we can't tell whether or not we can generate ICMP6, free it.
  405          */
  406         m_freem(m);
  407 }
  408 
  409 /*
  410  * Process a received ICMP6 message.
  411  */
  412 int
  413 icmp6_input(struct mbuf **mp, int *offp, int proto)
  414 {
  415         struct mbuf *m = *mp, *n;
  416         struct ifnet *ifp;
  417         struct ip6_hdr *ip6, *nip6;
  418         struct icmp6_hdr *icmp6, *nicmp6;
  419         int off = *offp;
  420         int icmp6len = m->m_pkthdr.len - *offp;
  421         int code, sum, noff;
  422         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  423         int ip6len, error;
  424 
  425         ifp = m->m_pkthdr.rcvif;
  426 
  427 #ifndef PULLDOWN_TEST
  428         IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
  429         /* m might change if M_LOOP.  So, call mtod after this */
  430 #endif
  431 
  432         /*
  433          * Locate icmp6 structure in mbuf, and check
  434          * that not corrupted and of at least minimum length
  435          */
  436 
  437         ip6 = mtod(m, struct ip6_hdr *);
  438         ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
  439         if (icmp6len < sizeof(struct icmp6_hdr)) {
  440                 ICMP6STAT_INC(icp6s_tooshort);
  441                 goto freeit;
  442         }
  443 
  444         /*
  445          * Check multicast group membership.
  446          * Note: SSM filters are not applied for ICMPv6 traffic.
  447          */
  448         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  449                 struct in6_multi        *inm;
  450 
  451                 inm = in6m_lookup(ifp, &ip6->ip6_dst);
  452                 if (inm == NULL) {
  453                         IP6STAT_INC(ip6s_notmember);
  454                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
  455                         goto freeit;
  456                 }
  457         }
  458 
  459         /*
  460          * calculate the checksum
  461          */
  462 #ifndef PULLDOWN_TEST
  463         icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
  464 #else
  465         IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
  466         if (icmp6 == NULL) {
  467                 ICMP6STAT_INC(icp6s_tooshort);
  468                 return IPPROTO_DONE;
  469         }
  470 #endif
  471         code = icmp6->icmp6_code;
  472 
  473         if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
  474                 nd6log((LOG_ERR,
  475                     "ICMP6 checksum error(%d|%x) %s\n",
  476                     icmp6->icmp6_type, sum,
  477                     ip6_sprintf(ip6bufs, &ip6->ip6_src)));
  478                 ICMP6STAT_INC(icp6s_checksum);
  479                 goto freeit;
  480         }
  481 
  482         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
  483                 /*
  484                  * Deliver very specific ICMP6 type only.
  485                  * This is important to deliver TOOBIG.  Otherwise PMTUD
  486                  * will not work.
  487                  */
  488                 switch (icmp6->icmp6_type) {
  489                 case ICMP6_DST_UNREACH:
  490                 case ICMP6_PACKET_TOO_BIG:
  491                 case ICMP6_TIME_EXCEEDED:
  492                         break;
  493                 default:
  494                         goto freeit;
  495                 }
  496         }
  497 
  498         ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
  499         icmp6_ifstat_inc(ifp, ifs6_in_msg);
  500         if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
  501                 icmp6_ifstat_inc(ifp, ifs6_in_error);
  502 
  503         switch (icmp6->icmp6_type) {
  504         case ICMP6_DST_UNREACH:
  505                 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
  506                 switch (code) {
  507                 case ICMP6_DST_UNREACH_NOROUTE:
  508                         code = PRC_UNREACH_NET;
  509                         break;
  510                 case ICMP6_DST_UNREACH_ADMIN:
  511                         icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
  512                         code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
  513                         break;
  514                 case ICMP6_DST_UNREACH_ADDR:
  515                         code = PRC_HOSTDEAD;
  516                         break;
  517                 case ICMP6_DST_UNREACH_BEYONDSCOPE:
  518                         /* I mean "source address was incorrect." */
  519                         code = PRC_PARAMPROB;
  520                         break;
  521                 case ICMP6_DST_UNREACH_NOPORT:
  522                         code = PRC_UNREACH_PORT;
  523                         break;
  524                 default:
  525                         goto badcode;
  526                 }
  527                 goto deliver;
  528                 break;
  529 
  530         case ICMP6_PACKET_TOO_BIG:
  531                 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
  532 
  533                 /* validation is made in icmp6_mtudisc_update */
  534 
  535                 code = PRC_MSGSIZE;
  536 
  537                 /*
  538                  * Updating the path MTU will be done after examining
  539                  * intermediate extension headers.
  540                  */
  541                 goto deliver;
  542                 break;
  543 
  544         case ICMP6_TIME_EXCEEDED:
  545                 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
  546                 switch (code) {
  547                 case ICMP6_TIME_EXCEED_TRANSIT:
  548                         code = PRC_TIMXCEED_INTRANS;
  549                         break;
  550                 case ICMP6_TIME_EXCEED_REASSEMBLY:
  551                         code = PRC_TIMXCEED_REASS;
  552                         break;
  553                 default:
  554                         goto badcode;
  555                 }
  556                 goto deliver;
  557                 break;
  558 
  559         case ICMP6_PARAM_PROB:
  560                 icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
  561                 switch (code) {
  562                 case ICMP6_PARAMPROB_NEXTHEADER:
  563                         code = PRC_UNREACH_PROTOCOL;
  564                         break;
  565                 case ICMP6_PARAMPROB_HEADER:
  566                 case ICMP6_PARAMPROB_OPTION:
  567                         code = PRC_PARAMPROB;
  568                         break;
  569                 default:
  570                         goto badcode;
  571                 }
  572                 goto deliver;
  573                 break;
  574 
  575         case ICMP6_ECHO_REQUEST:
  576                 icmp6_ifstat_inc(ifp, ifs6_in_echo);
  577                 if (code != 0)
  578                         goto badcode;
  579                 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
  580                         /* Give up remote */
  581                         break;
  582                 }
  583                 if ((n->m_flags & M_EXT) != 0
  584                  || n->m_len < off + sizeof(struct icmp6_hdr)) {
  585                         struct mbuf *n0 = n;
  586                         int n0len;
  587 
  588                         CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN);
  589                         n = m_gethdr(M_NOWAIT, n0->m_type);
  590                         if (n == NULL) {
  591                                 /* Give up remote */
  592                                 m_freem(n0);
  593                                 break;
  594                         }
  595 
  596                         m_move_pkthdr(n, n0);   /* FIB copied. */
  597                         n0len = n0->m_pkthdr.len;       /* save for use below */
  598                         /*
  599                          * Copy IPv6 and ICMPv6 only.
  600                          */
  601                         nip6 = mtod(n, struct ip6_hdr *);
  602                         bcopy(ip6, nip6, sizeof(struct ip6_hdr));
  603                         nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
  604                         bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
  605                         noff = sizeof(struct ip6_hdr);
  606                         /* new mbuf contains only ipv6+icmpv6 headers */
  607                         n->m_len = noff + sizeof(struct icmp6_hdr);
  608                         /*
  609                          * Adjust mbuf.  ip6_plen will be adjusted in
  610                          * ip6_output().
  611                          */
  612                         m_adj(n0, off + sizeof(struct icmp6_hdr));
  613                         /* recalculate complete packet size */
  614                         n->m_pkthdr.len = n0len + (noff - off);
  615                         n->m_next = n0;
  616                 } else {
  617                         nip6 = mtod(n, struct ip6_hdr *);
  618                         IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
  619                             sizeof(*nicmp6));
  620                         noff = off;
  621                 }
  622                 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
  623                 nicmp6->icmp6_code = 0;
  624                 if (n) {
  625                         ICMP6STAT_INC(icp6s_reflect);
  626                         ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
  627                         icmp6_reflect(n, noff);
  628                 }
  629                 break;
  630 
  631         case ICMP6_ECHO_REPLY:
  632                 icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
  633                 if (code != 0)
  634                         goto badcode;
  635                 break;
  636 
  637         case MLD_LISTENER_QUERY:
  638         case MLD_LISTENER_REPORT:
  639         case MLD_LISTENER_DONE:
  640         case MLDV2_LISTENER_REPORT:
  641                 /*
  642                  * Drop MLD traffic which is not link-local, has a hop limit
  643                  * of greater than 1 hop, or which does not have the
  644                  * IPv6 HBH Router Alert option.
  645                  * As IPv6 HBH options are stripped in ip6_input() we must
  646                  * check an mbuf header flag.
  647                  * XXX Should we also sanity check that these messages
  648                  * were directed to a link-local multicast prefix?
  649                  */
  650                 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
  651                         goto freeit;
  652                 if (mld_input(m, off, icmp6len) != 0)
  653                         return (IPPROTO_DONE);
  654                 /* m stays. */
  655                 break;
  656 
  657         case ICMP6_WRUREQUEST:  /* ICMP6_FQDN_QUERY */
  658             {
  659                 enum { WRU, FQDN } mode;
  660 
  661                 if (!V_icmp6_nodeinfo)
  662                         break;
  663 
  664                 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
  665                         mode = WRU;
  666                 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
  667                         mode = FQDN;
  668                 else
  669                         goto badlen;
  670 
  671                 if (mode == FQDN) {
  672 #ifndef PULLDOWN_TEST
  673                         IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
  674                             IPPROTO_DONE);
  675 #endif
  676                         n = m_copy(m, 0, M_COPYALL);
  677                         if (n)
  678                                 n = ni6_input(n, off);
  679                         /* XXX meaningless if n == NULL */
  680                         noff = sizeof(struct ip6_hdr);
  681                 } else {
  682                         struct prison *pr;
  683                         u_char *p;
  684                         int maxhlen, hlen;
  685 
  686                         /*
  687                          * XXX: this combination of flags is pointless,
  688                          * but should we keep this for compatibility?
  689                          */
  690                         if ((V_icmp6_nodeinfo & 5) != 5)
  691                                 break;
  692 
  693                         if (code != 0)
  694                                 goto badcode;
  695 
  696                         CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN);
  697                         n = m_gethdr(M_NOWAIT, m->m_type);
  698                         if (n == NULL) {
  699                                 /* Give up remote */
  700                                 break;
  701                         }
  702                         if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
  703                                 /*
  704                                  * Previous code did a blind M_COPY_PKTHDR
  705                                  * and said "just for rcvif".  If true, then
  706                                  * we could tolerate the dup failing (due to
  707                                  * the deep copy of the tag chain).  For now
  708                                  * be conservative and just fail.
  709                                  */
  710                                 m_free(n);
  711                                 n = NULL;
  712                         }
  713                         maxhlen = M_TRAILINGSPACE(n) -
  714                             (sizeof(*nip6) + sizeof(*nicmp6) + 4);
  715                         pr = curthread->td_ucred->cr_prison;
  716                         mtx_lock(&pr->pr_mtx);
  717                         hlen = strlen(pr->pr_hostname);
  718                         if (maxhlen > hlen)
  719                                 maxhlen = hlen;
  720                         /*
  721                          * Copy IPv6 and ICMPv6 only.
  722                          */
  723                         nip6 = mtod(n, struct ip6_hdr *);
  724                         bcopy(ip6, nip6, sizeof(struct ip6_hdr));
  725                         nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
  726                         bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
  727                         p = (u_char *)(nicmp6 + 1);
  728                         bzero(p, 4);
  729                         /* meaningless TTL */
  730                         bcopy(pr->pr_hostname, p + 4, maxhlen);
  731                         mtx_unlock(&pr->pr_mtx);
  732                         noff = sizeof(struct ip6_hdr);
  733                         n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
  734                                 sizeof(struct icmp6_hdr) + 4 + maxhlen;
  735                         nicmp6->icmp6_type = ICMP6_WRUREPLY;
  736                         nicmp6->icmp6_code = 0;
  737                 }
  738                 if (n) {
  739                         ICMP6STAT_INC(icp6s_reflect);
  740                         ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
  741                         icmp6_reflect(n, noff);
  742                 }
  743                 break;
  744             }
  745 
  746         case ICMP6_WRUREPLY:
  747                 if (code != 0)
  748                         goto badcode;
  749                 break;
  750 
  751         case ND_ROUTER_SOLICIT:
  752                 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
  753                 if (code != 0)
  754                         goto badcode;
  755                 if (icmp6len < sizeof(struct nd_router_solicit))
  756                         goto badlen;
  757                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
  758                         /* give up local */
  759 
  760                         /* Send incoming SeND packet to user space. */
  761                         if (send_sendso_input_hook != NULL) {
  762                                 IP6_EXTHDR_CHECK(m, off,
  763                                     icmp6len, IPPROTO_DONE);
  764                                 error = send_sendso_input_hook(m, ifp,
  765                                     SND_IN, ip6len);
  766                                 /* -1 == no app on SEND socket */
  767                                 if (error == 0)
  768                                         return (IPPROTO_DONE);
  769                                 nd6_rs_input(m, off, icmp6len);
  770                         } else
  771                                 nd6_rs_input(m, off, icmp6len);
  772                         m = NULL;
  773                         goto freeit;
  774                 }
  775                 if (send_sendso_input_hook != NULL) {
  776                         IP6_EXTHDR_CHECK(n, off,
  777                             icmp6len, IPPROTO_DONE);
  778                         error = send_sendso_input_hook(n, ifp,
  779                             SND_IN, ip6len);
  780                         if (error == 0)
  781                                 goto freeit;
  782                         /* -1 == no app on SEND socket */
  783                         nd6_rs_input(n, off, icmp6len);
  784                 } else
  785                         nd6_rs_input(n, off, icmp6len);
  786                 /* m stays. */
  787                 break;
  788 
  789         case ND_ROUTER_ADVERT:
  790                 icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
  791                 if (code != 0)
  792                         goto badcode;
  793                 if (icmp6len < sizeof(struct nd_router_advert))
  794                         goto badlen;
  795                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
  796 
  797                         /* Send incoming SeND-protected/ND packet to user space. */
  798                         if (send_sendso_input_hook != NULL) {
  799                                 error = send_sendso_input_hook(m, ifp,
  800                                     SND_IN, ip6len);
  801                                 if (error == 0)
  802                                         return (IPPROTO_DONE);
  803                                 nd6_ra_input(m, off, icmp6len);
  804                         } else
  805                                 nd6_ra_input(m, off, icmp6len);
  806                         m = NULL;
  807                         goto freeit;
  808                 }
  809                 if (send_sendso_input_hook != NULL) {
  810                         error = send_sendso_input_hook(n, ifp,
  811                             SND_IN, ip6len);
  812                         if (error == 0)
  813                                 goto freeit;
  814                         nd6_ra_input(n, off, icmp6len);
  815                 } else
  816                         nd6_ra_input(n, off, icmp6len);
  817                 /* m stays. */
  818                 break;
  819 
  820         case ND_NEIGHBOR_SOLICIT:
  821                 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
  822                 if (code != 0)
  823                         goto badcode;
  824                 if (icmp6len < sizeof(struct nd_neighbor_solicit))
  825                         goto badlen;
  826                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
  827                         if (send_sendso_input_hook != NULL) {
  828                                 error = send_sendso_input_hook(m, ifp,
  829                                     SND_IN, ip6len);
  830                                 if (error == 0)
  831                                         return (IPPROTO_DONE);
  832                                 nd6_ns_input(m, off, icmp6len);
  833                         } else
  834                                 nd6_ns_input(m, off, icmp6len);
  835                         m = NULL;
  836                         goto freeit;
  837                 }
  838                 if (send_sendso_input_hook != NULL) {
  839                         error = send_sendso_input_hook(n, ifp,
  840                             SND_IN, ip6len);
  841                         if (error == 0)
  842                                 goto freeit;
  843                         nd6_ns_input(n, off, icmp6len);
  844                 } else
  845                         nd6_ns_input(n, off, icmp6len);
  846                 /* m stays. */
  847                 break;
  848 
  849         case ND_NEIGHBOR_ADVERT:
  850                 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
  851                 if (code != 0)
  852                         goto badcode;
  853                 if (icmp6len < sizeof(struct nd_neighbor_advert))
  854                         goto badlen;
  855                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
  856 
  857                         /* Send incoming SeND-protected/ND packet to user space. */
  858                         if (send_sendso_input_hook != NULL) {
  859                                 error = send_sendso_input_hook(m, ifp,
  860                                     SND_IN, ip6len);
  861                                 if (error == 0)
  862                                         return (IPPROTO_DONE);
  863                                 nd6_na_input(m, off, icmp6len);
  864                         } else
  865                                 nd6_na_input(m, off, icmp6len);
  866                         m = NULL;
  867                         goto freeit;
  868                 }
  869                 if (send_sendso_input_hook != NULL) {
  870                         error = send_sendso_input_hook(n, ifp,
  871                             SND_IN, ip6len);
  872                         if (error == 0)
  873                                 goto freeit;
  874                         nd6_na_input(n, off, icmp6len);
  875                 } else
  876                         nd6_na_input(n, off, icmp6len);
  877                 /* m stays. */
  878                 break;
  879 
  880         case ND_REDIRECT:
  881                 icmp6_ifstat_inc(ifp, ifs6_in_redirect);
  882                 if (code != 0)
  883                         goto badcode;
  884                 if (icmp6len < sizeof(struct nd_redirect))
  885                         goto badlen;
  886                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
  887                         if (send_sendso_input_hook != NULL) {
  888                                 error = send_sendso_input_hook(m, ifp,
  889                                     SND_IN, ip6len);
  890                                 if (error == 0)
  891                                         return (IPPROTO_DONE);
  892                             icmp6_redirect_input(m, off);
  893                         } else
  894                                 icmp6_redirect_input(m, off);
  895                         m = NULL;
  896                         goto freeit;
  897                 }
  898                 if (send_sendso_input_hook != NULL) {
  899                         error = send_sendso_input_hook(n, ifp,
  900                             SND_IN, ip6len);
  901                         if (error == 0)
  902                                 goto freeit;
  903                         icmp6_redirect_input(n, off);
  904                 } else
  905                         icmp6_redirect_input(n, off);
  906                 /* m stays. */
  907                 break;
  908 
  909         case ICMP6_ROUTER_RENUMBERING:
  910                 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
  911                     code != ICMP6_ROUTER_RENUMBERING_RESULT)
  912                         goto badcode;
  913                 if (icmp6len < sizeof(struct icmp6_router_renum))
  914                         goto badlen;
  915                 break;
  916 
  917         default:
  918                 nd6log((LOG_DEBUG,
  919                     "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
  920                     icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  921                     ip6_sprintf(ip6bufd, &ip6->ip6_dst),
  922                     ifp ? ifp->if_index : 0));
  923                 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
  924                         /* ICMPv6 error: MUST deliver it by spec... */
  925                         code = PRC_NCMDS;
  926                         /* deliver */
  927                 } else {
  928                         /* ICMPv6 informational: MUST not deliver */
  929                         break;
  930                 }
  931         deliver:
  932                 if (icmp6_notify_error(&m, off, icmp6len, code) != 0) {
  933                         /* In this case, m should've been freed. */
  934                         return (IPPROTO_DONE);
  935                 }
  936                 break;
  937 
  938         badcode:
  939                 ICMP6STAT_INC(icp6s_badcode);
  940                 break;
  941 
  942         badlen:
  943                 ICMP6STAT_INC(icp6s_badlen);
  944                 break;
  945         }
  946 
  947         /* deliver the packet to appropriate sockets */
  948         icmp6_rip6_input(&m, *offp);
  949 
  950         return IPPROTO_DONE;
  951 
  952  freeit:
  953         m_freem(m);
  954         return IPPROTO_DONE;
  955 }
  956 
  957 static int
  958 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
  959 {
  960         struct mbuf *m = *mp;
  961         struct icmp6_hdr *icmp6;
  962         struct ip6_hdr *eip6;
  963         u_int32_t notifymtu;
  964         struct sockaddr_in6 icmp6src, icmp6dst;
  965 
  966         if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
  967                 ICMP6STAT_INC(icp6s_tooshort);
  968                 goto freeit;
  969         }
  970 #ifndef PULLDOWN_TEST
  971         IP6_EXTHDR_CHECK(m, off,
  972             sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
  973         icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
  974 #else
  975         IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
  976             sizeof(*icmp6) + sizeof(struct ip6_hdr));
  977         if (icmp6 == NULL) {
  978                 ICMP6STAT_INC(icp6s_tooshort);
  979                 return (-1);
  980         }
  981 #endif
  982         eip6 = (struct ip6_hdr *)(icmp6 + 1);
  983 
  984         /* Detect the upper level protocol */
  985         {
  986                 void (*ctlfunc)(int, struct sockaddr *, void *);
  987                 u_int8_t nxt = eip6->ip6_nxt;
  988                 int eoff = off + sizeof(struct icmp6_hdr) +
  989                     sizeof(struct ip6_hdr);
  990                 struct ip6ctlparam ip6cp;
  991                 struct in6_addr *finaldst = NULL;
  992                 int icmp6type = icmp6->icmp6_type;
  993                 struct ip6_frag *fh;
  994                 struct ip6_rthdr *rth;
  995                 struct ip6_rthdr0 *rth0;
  996                 int rthlen;
  997 
  998                 while (1) { /* XXX: should avoid infinite loop explicitly? */
  999                         struct ip6_ext *eh;
 1000 
 1001                         switch (nxt) {
 1002                         case IPPROTO_HOPOPTS:
 1003                         case IPPROTO_DSTOPTS:
 1004                         case IPPROTO_AH:
 1005 #ifndef PULLDOWN_TEST
 1006                                 IP6_EXTHDR_CHECK(m, 0,
 1007                                     eoff + sizeof(struct ip6_ext), -1);
 1008                                 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
 1009 #else
 1010                                 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
 1011                                     eoff, sizeof(*eh));
 1012                                 if (eh == NULL) {
 1013                                         ICMP6STAT_INC(icp6s_tooshort);
 1014                                         return (-1);
 1015                                 }
 1016 #endif
 1017 
 1018                                 if (nxt == IPPROTO_AH)
 1019                                         eoff += (eh->ip6e_len + 2) << 2;
 1020                                 else
 1021                                         eoff += (eh->ip6e_len + 1) << 3;
 1022                                 nxt = eh->ip6e_nxt;
 1023                                 break;
 1024                         case IPPROTO_ROUTING:
 1025                                 /*
 1026                                  * When the erroneous packet contains a
 1027                                  * routing header, we should examine the
 1028                                  * header to determine the final destination.
 1029                                  * Otherwise, we can't properly update
 1030                                  * information that depends on the final
 1031                                  * destination (e.g. path MTU).
 1032                                  */
 1033 #ifndef PULLDOWN_TEST
 1034                                 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
 1035                                 rth = (struct ip6_rthdr *)
 1036                                     (mtod(m, caddr_t) + eoff);
 1037 #else
 1038                                 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
 1039                                     eoff, sizeof(*rth));
 1040                                 if (rth == NULL) {
 1041                                         ICMP6STAT_INC(icp6s_tooshort);
 1042                                         return (-1);
 1043                                 }
 1044 #endif
 1045                                 rthlen = (rth->ip6r_len + 1) << 3;
 1046                                 /*
 1047                                  * XXX: currently there is no
 1048                                  * officially defined type other
 1049                                  * than type-0.
 1050                                  * Note that if the segment left field
 1051                                  * is 0, all intermediate hops must
 1052                                  * have been passed.
 1053                                  */
 1054                                 if (rth->ip6r_segleft &&
 1055                                     rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
 1056                                         int hops;
 1057 
 1058 #ifndef PULLDOWN_TEST
 1059                                         IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
 1060                                         rth0 = (struct ip6_rthdr0 *)
 1061                                             (mtod(m, caddr_t) + eoff);
 1062 #else
 1063                                         IP6_EXTHDR_GET(rth0,
 1064                                             struct ip6_rthdr0 *, m,
 1065                                             eoff, rthlen);
 1066                                         if (rth0 == NULL) {
 1067                                                 ICMP6STAT_INC(icp6s_tooshort);
 1068                                                 return (-1);
 1069                                         }
 1070 #endif
 1071                                         /* just ignore a bogus header */
 1072                                         if ((rth0->ip6r0_len % 2) == 0 &&
 1073                                             (hops = rth0->ip6r0_len/2))
 1074                                                 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
 1075                                 }
 1076                                 eoff += rthlen;
 1077                                 nxt = rth->ip6r_nxt;
 1078                                 break;
 1079                         case IPPROTO_FRAGMENT:
 1080 #ifndef PULLDOWN_TEST
 1081                                 IP6_EXTHDR_CHECK(m, 0, eoff +
 1082                                     sizeof(struct ip6_frag), -1);
 1083                                 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
 1084                                     eoff);
 1085 #else
 1086                                 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
 1087                                     eoff, sizeof(*fh));
 1088                                 if (fh == NULL) {
 1089                                         ICMP6STAT_INC(icp6s_tooshort);
 1090                                         return (-1);
 1091                                 }
 1092 #endif
 1093                                 /*
 1094                                  * Data after a fragment header is meaningless
 1095                                  * unless it is the first fragment, but
 1096                                  * we'll go to the notify label for path MTU
 1097                                  * discovery.
 1098                                  */
 1099                                 if (fh->ip6f_offlg & IP6F_OFF_MASK)
 1100                                         goto notify;
 1101 
 1102                                 eoff += sizeof(struct ip6_frag);
 1103                                 nxt = fh->ip6f_nxt;
 1104                                 break;
 1105                         default:
 1106                                 /*
 1107                                  * This case includes ESP and the No Next
 1108                                  * Header.  In such cases going to the notify
 1109                                  * label does not have any meaning
 1110                                  * (i.e. ctlfunc will be NULL), but we go
 1111                                  * anyway since we might have to update
 1112                                  * path MTU information.
 1113                                  */
 1114                                 goto notify;
 1115                         }
 1116                 }
 1117           notify:
 1118 #ifndef PULLDOWN_TEST
 1119                 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
 1120 #else
 1121                 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
 1122                     sizeof(*icmp6) + sizeof(struct ip6_hdr));
 1123                 if (icmp6 == NULL) {
 1124                         ICMP6STAT_INC(icp6s_tooshort);
 1125                         return (-1);
 1126                 }
 1127 #endif
 1128 
 1129                 /*
 1130                  * retrieve parameters from the inner IPv6 header, and convert
 1131                  * them into sockaddr structures.
 1132                  * XXX: there is no guarantee that the source or destination
 1133                  * addresses of the inner packet are in the same scope as
 1134                  * the addresses of the icmp packet.  But there is no other
 1135                  * way to determine the zone.
 1136                  */
 1137                 eip6 = (struct ip6_hdr *)(icmp6 + 1);
 1138 
 1139                 bzero(&icmp6dst, sizeof(icmp6dst));
 1140                 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
 1141                 icmp6dst.sin6_family = AF_INET6;
 1142                 if (finaldst == NULL)
 1143                         icmp6dst.sin6_addr = eip6->ip6_dst;
 1144                 else
 1145                         icmp6dst.sin6_addr = *finaldst;
 1146                 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
 1147                         goto freeit;
 1148                 bzero(&icmp6src, sizeof(icmp6src));
 1149                 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
 1150                 icmp6src.sin6_family = AF_INET6;
 1151                 icmp6src.sin6_addr = eip6->ip6_src;
 1152                 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
 1153                         goto freeit;
 1154                 icmp6src.sin6_flowinfo =
 1155                     (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
 1156 
 1157                 if (finaldst == NULL)
 1158                         finaldst = &eip6->ip6_dst;
 1159                 ip6cp.ip6c_m = m;
 1160                 ip6cp.ip6c_icmp6 = icmp6;
 1161                 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
 1162                 ip6cp.ip6c_off = eoff;
 1163                 ip6cp.ip6c_finaldst = finaldst;
 1164                 ip6cp.ip6c_src = &icmp6src;
 1165                 ip6cp.ip6c_nxt = nxt;
 1166 
 1167                 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
 1168                         notifymtu = ntohl(icmp6->icmp6_mtu);
 1169                         ip6cp.ip6c_cmdarg = (void *)&notifymtu;
 1170                         icmp6_mtudisc_update(&ip6cp, 1);        /*XXX*/
 1171                 }
 1172 
 1173                 ctlfunc = (void (*)(int, struct sockaddr *, void *))
 1174                     (inet6sw[ip6_protox[nxt]].pr_ctlinput);
 1175                 if (ctlfunc) {
 1176                         (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
 1177                             &ip6cp);
 1178                 }
 1179         }
 1180         *mp = m;
 1181         return (0);
 1182 
 1183   freeit:
 1184         m_freem(m);
 1185         return (-1);
 1186 }
 1187 
 1188 void
 1189 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
 1190 {
 1191         struct in6_addr *dst = ip6cp->ip6c_finaldst;
 1192         struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
 1193         struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
 1194         u_int mtu = ntohl(icmp6->icmp6_mtu);
 1195         struct in_conninfo inc;
 1196 
 1197 #if 0
 1198         /*
 1199          * RFC2460 section 5, last paragraph.
 1200          * even though minimum link MTU for IPv6 is IPV6_MMTU,
 1201          * we may see ICMPv6 too big with mtu < IPV6_MMTU
 1202          * due to packet translator in the middle.
 1203          * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
 1204          * special handling.
 1205          */
 1206         if (mtu < IPV6_MMTU)
 1207                 return;
 1208 #endif
 1209 
 1210         /*
 1211          * we reject ICMPv6 too big with abnormally small value.
 1212          * XXX what is the good definition of "abnormally small"?
 1213          */
 1214         if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
 1215                 return;
 1216 
 1217         if (!validated)
 1218                 return;
 1219 
 1220         /*
 1221          * In case the suggested mtu is less than IPV6_MMTU, we
 1222          * only need to remember that it was for above mentioned
 1223          * "alwaysfrag" case.
 1224          * Try to be as close to the spec as possible.
 1225          */
 1226         if (mtu < IPV6_MMTU)
 1227                 mtu = IPV6_MMTU - 8;
 1228 
 1229         bzero(&inc, sizeof(inc));
 1230         inc.inc_fibnum = M_GETFIB(m);
 1231         inc.inc_flags |= INC_ISIPV6;
 1232         inc.inc6_faddr = *dst;
 1233         if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
 1234                 return;
 1235 
 1236         if (mtu < tcp_maxmtu6(&inc, NULL)) {
 1237                 tcp_hc_updatemtu(&inc, mtu);
 1238                 ICMP6STAT_INC(icp6s_pmtuchg);
 1239         }
 1240 }
 1241 
 1242 /*
 1243  * Process a Node Information Query packet, based on
 1244  * draft-ietf-ipngwg-icmp-name-lookups-07.
 1245  *
 1246  * Spec incompatibilities:
 1247  * - IPv6 Subject address handling
 1248  * - IPv4 Subject address handling support missing
 1249  * - Proxy reply (answer even if it's not for me)
 1250  * - joins NI group address at in6_ifattach() time only, does not cope
 1251  *   with hostname changes by sethostname(3)
 1252  */
 1253 static struct mbuf *
 1254 ni6_input(struct mbuf *m, int off)
 1255 {
 1256         struct icmp6_nodeinfo *ni6, *nni6;
 1257         struct mbuf *n = NULL;
 1258         struct prison *pr;
 1259         u_int16_t qtype;
 1260         int subjlen;
 1261         int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
 1262         struct ni_reply_fqdn *fqdn;
 1263         int addrs;              /* for NI_QTYPE_NODEADDR */
 1264         struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
 1265         struct in6_addr in6_subj; /* subject address */
 1266         struct ip6_hdr *ip6;
 1267         int oldfqdn = 0;        /* if 1, return pascal string (03 draft) */
 1268         char *subj = NULL;
 1269         struct in6_ifaddr *ia6 = NULL;
 1270 
 1271         ip6 = mtod(m, struct ip6_hdr *);
 1272 #ifndef PULLDOWN_TEST
 1273         ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
 1274 #else
 1275         IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
 1276         if (ni6 == NULL) {
 1277                 /* m is already reclaimed */
 1278                 return (NULL);
 1279         }
 1280 #endif
 1281 
 1282         /*
 1283          * Validate IPv6 source address.
 1284          * The default configuration MUST be to refuse answering queries from
 1285          * global-scope addresses according to RFC4602.
 1286          * Notes:
 1287          *  - it's not very clear what "refuse" means; this implementation
 1288          *    simply drops it.
 1289          *  - it's not very easy to identify global-scope (unicast) addresses
 1290          *    since there are many prefixes for them.  It should be safer
 1291          *    and in practice sufficient to check "all" but loopback and
 1292          *    link-local (note that site-local unicast was deprecated and
 1293          *    ULA is defined as global scope-wise)
 1294          */
 1295         if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
 1296             !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
 1297             !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
 1298                 goto bad;
 1299 
 1300         /*
 1301          * Validate IPv6 destination address.
 1302          *
 1303          * The Responder must discard the Query without further processing
 1304          * unless it is one of the Responder's unicast or anycast addresses, or
 1305          * a link-local scope multicast address which the Responder has joined.
 1306          * [RFC4602, Section 5.]
 1307          */
 1308         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
 1309                 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
 1310                         goto bad;
 1311                 /* else it's a link-local multicast, fine */
 1312         } else {                /* unicast or anycast */
 1313                 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
 1314                         goto bad; /* XXX impossible */
 1315 
 1316                 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
 1317                     !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
 1318                         ifa_free(&ia6->ia_ifa);
 1319                         nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
 1320                                 "a temporary address in %s:%d",
 1321                                __FILE__, __LINE__));
 1322                         goto bad;
 1323                 }
 1324                 ifa_free(&ia6->ia_ifa);
 1325         }
 1326 
 1327         /* validate query Subject field. */
 1328         qtype = ntohs(ni6->ni_qtype);
 1329         subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
 1330         switch (qtype) {
 1331         case NI_QTYPE_NOOP:
 1332         case NI_QTYPE_SUPTYPES:
 1333                 /* 07 draft */
 1334                 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
 1335                         break;
 1336                 /* FALLTHROUGH */
 1337         case NI_QTYPE_FQDN:
 1338         case NI_QTYPE_NODEADDR:
 1339         case NI_QTYPE_IPV4ADDR:
 1340                 switch (ni6->ni_code) {
 1341                 case ICMP6_NI_SUBJ_IPV6:
 1342 #if ICMP6_NI_SUBJ_IPV6 != 0
 1343                 case 0:
 1344 #endif
 1345                         /*
 1346                          * backward compatibility - try to accept 03 draft
 1347                          * format, where no Subject is present.
 1348                          */
 1349                         if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
 1350                             subjlen == 0) {
 1351                                 oldfqdn++;
 1352                                 break;
 1353                         }
 1354 #if ICMP6_NI_SUBJ_IPV6 != 0
 1355                         if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
 1356                                 goto bad;
 1357 #endif
 1358 
 1359                         if (subjlen != sizeof(struct in6_addr))
 1360                                 goto bad;
 1361 
 1362                         /*
 1363                          * Validate Subject address.
 1364                          *
 1365                          * Not sure what exactly "address belongs to the node"
 1366                          * means in the spec, is it just unicast, or what?
 1367                          *
 1368                          * At this moment we consider Subject address as
 1369                          * "belong to the node" if the Subject address equals
 1370                          * to the IPv6 destination address; validation for
 1371                          * IPv6 destination address should have done enough
 1372                          * check for us.
 1373                          *
 1374                          * We do not do proxy at this moment.
 1375                          */
 1376                         /* m_pulldown instead of copy? */
 1377                         m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
 1378                             subjlen, (caddr_t)&in6_subj);
 1379                         if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
 1380                                 goto bad;
 1381 
 1382                         subj = (char *)&in6_subj;
 1383                         if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
 1384                                 break;
 1385 
 1386                         /*
 1387                          * XXX if we are to allow other cases, we should really
 1388                          * be careful about scope here.
 1389                          * basically, we should disallow queries toward IPv6
 1390                          * destination X with subject Y,
 1391                          * if scope(X) > scope(Y).
 1392                          * if we allow scope(X) > scope(Y), it will result in
 1393                          * information leakage across scope boundary.
 1394                          */
 1395                         goto bad;
 1396 
 1397                 case ICMP6_NI_SUBJ_FQDN:
 1398                         /*
 1399                          * Validate Subject name with gethostname(3).
 1400                          *
 1401                          * The behavior may need some debate, since:
 1402                          * - we are not sure if the node has FQDN as
 1403                          *   hostname (returned by gethostname(3)).
 1404                          * - the code does wildcard match for truncated names.
 1405                          *   however, we are not sure if we want to perform
 1406                          *   wildcard match, if gethostname(3) side has
 1407                          *   truncated hostname.
 1408                          */
 1409                         pr = curthread->td_ucred->cr_prison;
 1410                         mtx_lock(&pr->pr_mtx);
 1411                         n = ni6_nametodns(pr->pr_hostname,
 1412                             strlen(pr->pr_hostname), 0);
 1413                         mtx_unlock(&pr->pr_mtx);
 1414                         if (!n || n->m_next || n->m_len == 0)
 1415                                 goto bad;
 1416                         IP6_EXTHDR_GET(subj, char *, m,
 1417                             off + sizeof(struct icmp6_nodeinfo), subjlen);
 1418                         if (subj == NULL)
 1419                                 goto bad;
 1420                         if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
 1421                             n->m_len)) {
 1422                                 goto bad;
 1423                         }
 1424                         m_freem(n);
 1425                         n = NULL;
 1426                         break;
 1427 
 1428                 case ICMP6_NI_SUBJ_IPV4:        /* XXX: to be implemented? */
 1429                 default:
 1430                         goto bad;
 1431                 }
 1432                 break;
 1433         }
 1434 
 1435         /* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
 1436         switch (qtype) {
 1437         case NI_QTYPE_FQDN:
 1438                 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
 1439                         goto bad;
 1440                 break;
 1441         case NI_QTYPE_NODEADDR:
 1442         case NI_QTYPE_IPV4ADDR:
 1443                 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
 1444                         goto bad;
 1445                 break;
 1446         }
 1447 
 1448         /* guess reply length */
 1449         switch (qtype) {
 1450         case NI_QTYPE_NOOP:
 1451                 break;          /* no reply data */
 1452         case NI_QTYPE_SUPTYPES:
 1453                 replylen += sizeof(u_int32_t);
 1454                 break;
 1455         case NI_QTYPE_FQDN:
 1456                 /* XXX will append an mbuf */
 1457                 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
 1458                 break;
 1459         case NI_QTYPE_NODEADDR:
 1460                 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
 1461                 if ((replylen += addrs * (sizeof(struct in6_addr) +
 1462                     sizeof(u_int32_t))) > MCLBYTES)
 1463                         replylen = MCLBYTES; /* XXX: will truncate pkt later */
 1464                 break;
 1465         case NI_QTYPE_IPV4ADDR:
 1466                 /* unsupported - should respond with unknown Qtype? */
 1467                 break;
 1468         default:
 1469                 /*
 1470                  * XXX: We must return a reply with the ICMP6 code
 1471                  * `unknown Qtype' in this case.  However we regard the case
 1472                  * as an FQDN query for backward compatibility.
 1473                  * Older versions set a random value to this field,
 1474                  * so it rarely varies in the defined qtypes.
 1475                  * But the mechanism is not reliable...
 1476                  * maybe we should obsolete older versions.
 1477                  */
 1478                 qtype = NI_QTYPE_FQDN;
 1479                 /* XXX will append an mbuf */
 1480                 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
 1481                 oldfqdn++;
 1482                 break;
 1483         }
 1484 
 1485         /* Allocate an mbuf to reply. */
 1486         if (replylen > MCLBYTES) {
 1487                 /*
 1488                  * XXX: should we try to allocate more? But MCLBYTES
 1489                  * is probably much larger than IPV6_MMTU...
 1490                  */
 1491                 goto bad;
 1492         }
 1493         if (replylen > MHLEN)
 1494                 n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR);
 1495         else
 1496                 n = m_gethdr(M_NOWAIT, m->m_type);
 1497         if (n == NULL) {
 1498                 m_freem(m);
 1499                 return (NULL);
 1500         }
 1501         m_move_pkthdr(n, m); /* just for recvif and FIB */
 1502         n->m_pkthdr.len = n->m_len = replylen;
 1503 
 1504         /* copy mbuf header and IPv6 + Node Information base headers */
 1505         bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
 1506         nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
 1507         bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
 1508 
 1509         /* qtype dependent procedure */
 1510         switch (qtype) {
 1511         case NI_QTYPE_NOOP:
 1512                 nni6->ni_code = ICMP6_NI_SUCCESS;
 1513                 nni6->ni_flags = 0;
 1514                 break;
 1515         case NI_QTYPE_SUPTYPES:
 1516         {
 1517                 u_int32_t v;
 1518                 nni6->ni_code = ICMP6_NI_SUCCESS;
 1519                 nni6->ni_flags = htons(0x0000); /* raw bitmap */
 1520                 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
 1521                 v = (u_int32_t)htonl(0x0000000f);
 1522                 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
 1523                 break;
 1524         }
 1525         case NI_QTYPE_FQDN:
 1526                 nni6->ni_code = ICMP6_NI_SUCCESS;
 1527                 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
 1528                     sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
 1529                 nni6->ni_flags = 0; /* XXX: meaningless TTL */
 1530                 fqdn->ni_fqdn_ttl = 0;  /* ditto. */
 1531                 /*
 1532                  * XXX do we really have FQDN in hostname?
 1533                  */
 1534                 pr = curthread->td_ucred->cr_prison;
 1535                 mtx_lock(&pr->pr_mtx);
 1536                 n->m_next = ni6_nametodns(pr->pr_hostname,
 1537                     strlen(pr->pr_hostname), oldfqdn);
 1538                 mtx_unlock(&pr->pr_mtx);
 1539                 if (n->m_next == NULL)
 1540                         goto bad;
 1541                 /* XXX we assume that n->m_next is not a chain */
 1542                 if (n->m_next->m_next != NULL)
 1543                         goto bad;
 1544                 n->m_pkthdr.len += n->m_next->m_len;
 1545                 break;
 1546         case NI_QTYPE_NODEADDR:
 1547         {
 1548                 int lenlim, copied;
 1549 
 1550                 nni6->ni_code = ICMP6_NI_SUCCESS;
 1551                 n->m_pkthdr.len = n->m_len =
 1552                     sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
 1553                 lenlim = M_TRAILINGSPACE(n);
 1554                 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
 1555                 /* XXX: reset mbuf length */
 1556                 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
 1557                     sizeof(struct icmp6_nodeinfo) + copied;
 1558                 break;
 1559         }
 1560         default:
 1561                 break;          /* XXX impossible! */
 1562         }
 1563 
 1564         nni6->ni_type = ICMP6_NI_REPLY;
 1565         m_freem(m);
 1566         return (n);
 1567 
 1568   bad:
 1569         m_freem(m);
 1570         if (n)
 1571                 m_freem(n);
 1572         return (NULL);
 1573 }
 1574 
 1575 /*
 1576  * make a mbuf with DNS-encoded string.  no compression support.
 1577  *
 1578  * XXX names with less than 2 dots (like "foo" or "foo.section") will be
 1579  * treated as truncated name (two \0 at the end).  this is a wild guess.
 1580  *
 1581  * old - return pascal string if non-zero
 1582  */
 1583 static struct mbuf *
 1584 ni6_nametodns(const char *name, int namelen, int old)
 1585 {
 1586         struct mbuf *m;
 1587         char *cp, *ep;
 1588         const char *p, *q;
 1589         int i, len, nterm;
 1590 
 1591         if (old)
 1592                 len = namelen + 1;
 1593         else
 1594                 len = MCLBYTES;
 1595 
 1596         /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */
 1597         if (len > MLEN)
 1598                 m = m_getcl(M_NOWAIT, MT_DATA, 0);
 1599         else
 1600                 m = m_get(M_NOWAIT, MT_DATA);
 1601         if (m == NULL)
 1602                 goto fail;
 1603 
 1604         if (old) {
 1605                 m->m_len = len;
 1606                 *mtod(m, char *) = namelen;
 1607                 bcopy(name, mtod(m, char *) + 1, namelen);
 1608                 return m;
 1609         } else {
 1610                 m->m_len = 0;
 1611                 cp = mtod(m, char *);
 1612                 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
 1613 
 1614                 /* if not certain about my name, return empty buffer */
 1615                 if (namelen == 0)
 1616                         return m;
 1617 
 1618                 /*
 1619                  * guess if it looks like shortened hostname, or FQDN.
 1620                  * shortened hostname needs two trailing "\0".
 1621                  */
 1622                 i = 0;
 1623                 for (p = name; p < name + namelen; p++) {
 1624                         if (*p && *p == '.')
 1625                                 i++;
 1626                 }
 1627                 if (i < 2)
 1628                         nterm = 2;
 1629                 else
 1630                         nterm = 1;
 1631 
 1632                 p = name;
 1633                 while (cp < ep && p < name + namelen) {
 1634                         i = 0;
 1635                         for (q = p; q < name + namelen && *q && *q != '.'; q++)
 1636                                 i++;
 1637                         /* result does not fit into mbuf */
 1638                         if (cp + i + 1 >= ep)
 1639                                 goto fail;
 1640                         /*
 1641                          * DNS label length restriction, RFC1035 page 8.
 1642                          * "i == 0" case is included here to avoid returning
 1643                          * 0-length label on "foo..bar".
 1644                          */
 1645                         if (i <= 0 || i >= 64)
 1646                                 goto fail;
 1647                         *cp++ = i;
 1648                         bcopy(p, cp, i);
 1649                         cp += i;
 1650                         p = q;
 1651                         if (p < name + namelen && *p == '.')
 1652                                 p++;
 1653                 }
 1654                 /* termination */
 1655                 if (cp + nterm >= ep)
 1656                         goto fail;
 1657                 while (nterm-- > 0)
 1658                         *cp++ = '\0';
 1659                 m->m_len = cp - mtod(m, char *);
 1660                 return m;
 1661         }
 1662 
 1663         panic("should not reach here");
 1664         /* NOTREACHED */
 1665 
 1666  fail:
 1667         if (m)
 1668                 m_freem(m);
 1669         return NULL;
 1670 }
 1671 
 1672 /*
 1673  * check if two DNS-encoded string matches.  takes care of truncated
 1674  * form (with \0\0 at the end).  no compression support.
 1675  * XXX upper/lowercase match (see RFC2065)
 1676  */
 1677 static int
 1678 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
 1679 {
 1680         const char *a0, *b0;
 1681         int l;
 1682 
 1683         /* simplest case - need validation? */
 1684         if (alen == blen && bcmp(a, b, alen) == 0)
 1685                 return 1;
 1686 
 1687         a0 = a;
 1688         b0 = b;
 1689 
 1690         /* termination is mandatory */
 1691         if (alen < 2 || blen < 2)
 1692                 return 0;
 1693         if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
 1694                 return 0;
 1695         alen--;
 1696         blen--;
 1697 
 1698         while (a - a0 < alen && b - b0 < blen) {
 1699                 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
 1700                         return 0;
 1701 
 1702                 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
 1703                         return 0;
 1704                 /* we don't support compression yet */
 1705                 if (a[0] >= 64 || b[0] >= 64)
 1706                         return 0;
 1707 
 1708                 /* truncated case */
 1709                 if (a[0] == 0 && a - a0 == alen - 1)
 1710                         return 1;
 1711                 if (b[0] == 0 && b - b0 == blen - 1)
 1712                         return 1;
 1713                 if (a[0] == 0 || b[0] == 0)
 1714                         return 0;
 1715 
 1716                 if (a[0] != b[0])
 1717                         return 0;
 1718                 l = a[0];
 1719                 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
 1720                         return 0;
 1721                 if (bcmp(a + 1, b + 1, l) != 0)
 1722                         return 0;
 1723 
 1724                 a += 1 + l;
 1725                 b += 1 + l;
 1726         }
 1727 
 1728         if (a - a0 == alen && b - b0 == blen)
 1729                 return 1;
 1730         else
 1731                 return 0;
 1732 }
 1733 
 1734 /*
 1735  * calculate the number of addresses to be returned in the node info reply.
 1736  */
 1737 static int
 1738 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
 1739     struct in6_addr *subj)
 1740 {
 1741         struct ifnet *ifp;
 1742         struct in6_ifaddr *ifa6;
 1743         struct ifaddr *ifa;
 1744         int addrs = 0, addrsofif, iffound = 0;
 1745         int niflags = ni6->ni_flags;
 1746 
 1747         if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
 1748                 switch (ni6->ni_code) {
 1749                 case ICMP6_NI_SUBJ_IPV6:
 1750                         if (subj == NULL) /* must be impossible... */
 1751                                 return (0);
 1752                         break;
 1753                 default:
 1754                         /*
 1755                          * XXX: we only support IPv6 subject address for
 1756                          * this Qtype.
 1757                          */
 1758                         return (0);
 1759                 }
 1760         }
 1761 
 1762         IFNET_RLOCK_NOSLEEP();
 1763         TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
 1764                 addrsofif = 0;
 1765                 IF_ADDR_RLOCK(ifp);
 1766                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1767                         if (ifa->ifa_addr->sa_family != AF_INET6)
 1768                                 continue;
 1769                         ifa6 = (struct in6_ifaddr *)ifa;
 1770 
 1771                         if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
 1772                             IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
 1773                                 iffound = 1;
 1774 
 1775                         /*
 1776                          * IPv4-mapped addresses can only be returned by a
 1777                          * Node Information proxy, since they represent
 1778                          * addresses of IPv4-only nodes, which perforce do
 1779                          * not implement this protocol.
 1780                          * [icmp-name-lookups-07, Section 5.4]
 1781                          * So we don't support NI_NODEADDR_FLAG_COMPAT in
 1782                          * this function at this moment.
 1783                          */
 1784 
 1785                         /* What do we have to do about ::1? */
 1786                         switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
 1787                         case IPV6_ADDR_SCOPE_LINKLOCAL:
 1788                                 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
 1789                                         continue;
 1790                                 break;
 1791                         case IPV6_ADDR_SCOPE_SITELOCAL:
 1792                                 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
 1793                                         continue;
 1794                                 break;
 1795                         case IPV6_ADDR_SCOPE_GLOBAL:
 1796                                 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
 1797                                         continue;
 1798                                 break;
 1799                         default:
 1800                                 continue;
 1801                         }
 1802 
 1803                         /*
 1804                          * check if anycast is okay.
 1805                          * XXX: just experimental.  not in the spec.
 1806                          */
 1807                         if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
 1808                             (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
 1809                                 continue; /* we need only unicast addresses */
 1810                         if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
 1811                             (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
 1812                                 continue;
 1813                         }
 1814                         addrsofif++; /* count the address */
 1815                 }
 1816                 IF_ADDR_RUNLOCK(ifp);
 1817                 if (iffound) {
 1818                         *ifpp = ifp;
 1819                         IFNET_RUNLOCK_NOSLEEP();
 1820                         return (addrsofif);
 1821                 }
 1822 
 1823                 addrs += addrsofif;
 1824         }
 1825         IFNET_RUNLOCK_NOSLEEP();
 1826 
 1827         return (addrs);
 1828 }
 1829 
 1830 static int
 1831 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
 1832     struct ifnet *ifp0, int resid)
 1833 {
 1834         struct ifnet *ifp;
 1835         struct in6_ifaddr *ifa6;
 1836         struct ifaddr *ifa;
 1837         struct ifnet *ifp_dep = NULL;
 1838         int copied = 0, allow_deprecated = 0;
 1839         u_char *cp = (u_char *)(nni6 + 1);
 1840         int niflags = ni6->ni_flags;
 1841         u_int32_t ltime;
 1842 
 1843         if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
 1844                 return (0);     /* needless to copy */
 1845 
 1846         IFNET_RLOCK_NOSLEEP();
 1847         ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet);
 1848   again:
 1849 
 1850         for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
 1851                 IF_ADDR_RLOCK(ifp);
 1852                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1853                         if (ifa->ifa_addr->sa_family != AF_INET6)
 1854                                 continue;
 1855                         ifa6 = (struct in6_ifaddr *)ifa;
 1856 
 1857                         if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
 1858                             allow_deprecated == 0) {
 1859                                 /*
 1860                                  * prefererred address should be put before
 1861                                  * deprecated addresses.
 1862                                  */
 1863 
 1864                                 /* record the interface for later search */
 1865                                 if (ifp_dep == NULL)
 1866                                         ifp_dep = ifp;
 1867 
 1868                                 continue;
 1869                         } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
 1870                             allow_deprecated != 0)
 1871                                 continue; /* we now collect deprecated addrs */
 1872 
 1873                         /* What do we have to do about ::1? */
 1874                         switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
 1875                         case IPV6_ADDR_SCOPE_LINKLOCAL:
 1876                                 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
 1877                                         continue;
 1878                                 break;
 1879                         case IPV6_ADDR_SCOPE_SITELOCAL:
 1880                                 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
 1881                                         continue;
 1882                                 break;
 1883                         case IPV6_ADDR_SCOPE_GLOBAL:
 1884                                 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
 1885                                         continue;
 1886                                 break;
 1887                         default:
 1888                                 continue;
 1889                         }
 1890 
 1891                         /*
 1892                          * check if anycast is okay.
 1893                          * XXX: just experimental.  not in the spec.
 1894                          */
 1895                         if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
 1896                             (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
 1897                                 continue;
 1898                         if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
 1899                             (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
 1900                                 continue;
 1901                         }
 1902 
 1903                         /* now we can copy the address */
 1904                         if (resid < sizeof(struct in6_addr) +
 1905                             sizeof(u_int32_t)) {
 1906                                 IF_ADDR_RUNLOCK(ifp);
 1907                                 /*
 1908                                  * We give up much more copy.
 1909                                  * Set the truncate flag and return.
 1910                                  */
 1911                                 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
 1912                                 IFNET_RUNLOCK_NOSLEEP();
 1913                                 return (copied);
 1914                         }
 1915 
 1916                         /*
 1917                          * Set the TTL of the address.
 1918                          * The TTL value should be one of the following
 1919                          * according to the specification:
 1920                          *
 1921                          * 1. The remaining lifetime of a DHCP lease on the
 1922                          *    address, or
 1923                          * 2. The remaining Valid Lifetime of a prefix from
 1924                          *    which the address was derived through Stateless
 1925                          *    Autoconfiguration.
 1926                          *
 1927                          * Note that we currently do not support stateful
 1928                          * address configuration by DHCPv6, so the former
 1929                          * case can't happen.
 1930                          */
 1931                         if (ifa6->ia6_lifetime.ia6t_expire == 0)
 1932                                 ltime = ND6_INFINITE_LIFETIME;
 1933                         else {
 1934                                 if (ifa6->ia6_lifetime.ia6t_expire >
 1935                                     time_uptime)
 1936                                         ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime);
 1937                                 else
 1938                                         ltime = 0;
 1939                         }
 1940 
 1941                         bcopy(&ltime, cp, sizeof(u_int32_t));
 1942                         cp += sizeof(u_int32_t);
 1943 
 1944                         /* copy the address itself */
 1945                         bcopy(&ifa6->ia_addr.sin6_addr, cp,
 1946                             sizeof(struct in6_addr));
 1947                         in6_clearscope((struct in6_addr *)cp); /* XXX */
 1948                         cp += sizeof(struct in6_addr);
 1949 
 1950                         resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
 1951                         copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
 1952                 }
 1953                 IF_ADDR_RUNLOCK(ifp);
 1954                 if (ifp0)       /* we need search only on the specified IF */
 1955                         break;
 1956         }
 1957 
 1958         if (allow_deprecated == 0 && ifp_dep != NULL) {
 1959                 ifp = ifp_dep;
 1960                 allow_deprecated = 1;
 1961 
 1962                 goto again;
 1963         }
 1964 
 1965         IFNET_RUNLOCK_NOSLEEP();
 1966 
 1967         return (copied);
 1968 }
 1969 
 1970 /*
 1971  * XXX almost dup'ed code with rip6_input.
 1972  */
 1973 static int
 1974 icmp6_rip6_input(struct mbuf **mp, int off)
 1975 {
 1976         struct mbuf *m = *mp;
 1977         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1978         struct inpcb *in6p;
 1979         struct inpcb *last = NULL;
 1980         struct sockaddr_in6 fromsa;
 1981         struct icmp6_hdr *icmp6;
 1982         struct mbuf *opts = NULL;
 1983 
 1984 #ifndef PULLDOWN_TEST
 1985         /* this is assumed to be safe. */
 1986         icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
 1987 #else
 1988         IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
 1989         if (icmp6 == NULL) {
 1990                 /* m is already reclaimed */
 1991                 return (IPPROTO_DONE);
 1992         }
 1993 #endif
 1994 
 1995         /*
 1996          * XXX: the address may have embedded scope zone ID, which should be
 1997          * hidden from applications.
 1998          */
 1999         bzero(&fromsa, sizeof(fromsa));
 2000         fromsa.sin6_family = AF_INET6;
 2001         fromsa.sin6_len = sizeof(struct sockaddr_in6);
 2002         fromsa.sin6_addr = ip6->ip6_src;
 2003         if (sa6_recoverscope(&fromsa)) {
 2004                 m_freem(m);
 2005                 return (IPPROTO_DONE);
 2006         }
 2007 
 2008         INP_INFO_RLOCK(&V_ripcbinfo);
 2009         LIST_FOREACH(in6p, &V_ripcb, inp_list) {
 2010                 if ((in6p->inp_vflag & INP_IPV6) == 0)
 2011                         continue;
 2012                 if (in6p->inp_ip_p != IPPROTO_ICMPV6)
 2013                         continue;
 2014                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
 2015                    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
 2016                         continue;
 2017                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
 2018                    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
 2019                         continue;
 2020                 INP_RLOCK(in6p);
 2021                 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
 2022                     in6p->in6p_icmp6filt)) {
 2023                         INP_RUNLOCK(in6p);
 2024                         continue;
 2025                 }
 2026                 if (last != NULL) {
 2027                         struct  mbuf *n = NULL;
 2028 
 2029                         /*
 2030                          * Recent network drivers tend to allocate a single
 2031                          * mbuf cluster, rather than to make a couple of
 2032                          * mbufs without clusters.  Also, since the IPv6 code
 2033                          * path tries to avoid m_pullup(), it is highly
 2034                          * probable that we still have an mbuf cluster here
 2035                          * even though the necessary length can be stored in an
 2036                          * mbuf's internal buffer.
 2037                          * Meanwhile, the default size of the receive socket
 2038                          * buffer for raw sockets is not so large.  This means
 2039                          * the possibility of packet loss is relatively higher
 2040                          * than before.  To avoid this scenario, we copy the
 2041                          * received data to a separate mbuf that does not use
 2042                          * a cluster, if possible.
 2043                          * XXX: it is better to copy the data after stripping
 2044                          * intermediate headers.
 2045                          */
 2046                         if ((m->m_flags & M_EXT) && m->m_next == NULL &&
 2047                             m->m_len <= MHLEN) {
 2048                                 n = m_get(M_NOWAIT, m->m_type);
 2049                                 if (n != NULL) {
 2050                                         if (m_dup_pkthdr(n, m, M_NOWAIT)) {
 2051                                                 bcopy(m->m_data, n->m_data,
 2052                                                       m->m_len);
 2053                                                 n->m_len = m->m_len;
 2054                                         } else {
 2055                                                 m_free(n);
 2056                                                 n = NULL;
 2057                                         }
 2058                                 }
 2059                         }
 2060                         if (n != NULL ||
 2061                             (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
 2062                                 if (last->inp_flags & INP_CONTROLOPTS)
 2063                                         ip6_savecontrol(last, n, &opts);
 2064                                 /* strip intermediate headers */
 2065                                 m_adj(n, off);
 2066                                 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
 2067                                 if (sbappendaddr_locked(
 2068                                     &last->inp_socket->so_rcv,
 2069                                     (struct sockaddr *)&fromsa, n, opts)
 2070                                     == 0) {
 2071                                         /* should notify about lost packet */
 2072                                         m_freem(n);
 2073                                         if (opts) {
 2074                                                 m_freem(opts);
 2075                                         }
 2076                                         SOCKBUF_UNLOCK(
 2077                                             &last->inp_socket->so_rcv);
 2078                                 } else
 2079                                         sorwakeup_locked(last->inp_socket);
 2080                                 opts = NULL;
 2081                         }
 2082                         INP_RUNLOCK(last);
 2083                 }
 2084                 last = in6p;
 2085         }
 2086         INP_INFO_RUNLOCK(&V_ripcbinfo);
 2087         if (last != NULL) {
 2088                 if (last->inp_flags & INP_CONTROLOPTS)
 2089                         ip6_savecontrol(last, m, &opts);
 2090                 /* strip intermediate headers */
 2091                 m_adj(m, off);
 2092 
 2093                 /* avoid using mbuf clusters if possible (see above) */
 2094                 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
 2095                     m->m_len <= MHLEN) {
 2096                         struct mbuf *n;
 2097 
 2098                         n = m_get(M_NOWAIT, m->m_type);
 2099                         if (n != NULL) {
 2100                                 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
 2101                                         bcopy(m->m_data, n->m_data, m->m_len);
 2102                                         n->m_len = m->m_len;
 2103 
 2104                                         m_freem(m);
 2105                                         m = n;
 2106                                 } else {
 2107                                         m_freem(n);
 2108                                         n = NULL;
 2109                                 }
 2110                         }
 2111                 }
 2112                 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
 2113                 if (sbappendaddr_locked(&last->inp_socket->so_rcv,
 2114                     (struct sockaddr *)&fromsa, m, opts) == 0) {
 2115                         m_freem(m);
 2116                         if (opts)
 2117                                 m_freem(opts);
 2118                         SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
 2119                 } else
 2120                         sorwakeup_locked(last->inp_socket);
 2121                 INP_RUNLOCK(last);
 2122         } else {
 2123                 m_freem(m);
 2124                 IP6STAT_DEC(ip6s_delivered);
 2125         }
 2126         return IPPROTO_DONE;
 2127 }
 2128 
 2129 /*
 2130  * Reflect the ip6 packet back to the source.
 2131  * OFF points to the icmp6 header, counted from the top of the mbuf.
 2132  */
 2133 void
 2134 icmp6_reflect(struct mbuf *m, size_t off)
 2135 {
 2136         struct ip6_hdr *ip6;
 2137         struct icmp6_hdr *icmp6;
 2138         struct in6_ifaddr *ia = NULL;
 2139         int plen;
 2140         int type, code;
 2141         struct ifnet *outif = NULL;
 2142         struct in6_addr origdst, src, *srcp = NULL;
 2143 
 2144         /* too short to reflect */
 2145         if (off < sizeof(struct ip6_hdr)) {
 2146                 nd6log((LOG_DEBUG,
 2147                     "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
 2148                     (u_long)off, (u_long)sizeof(struct ip6_hdr),
 2149                     __FILE__, __LINE__));
 2150                 goto bad;
 2151         }
 2152 
 2153         /*
 2154          * If there are extra headers between IPv6 and ICMPv6, strip
 2155          * off that header first.
 2156          */
 2157 #ifdef DIAGNOSTIC
 2158         if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
 2159                 panic("assumption failed in icmp6_reflect");
 2160 #endif
 2161         if (off > sizeof(struct ip6_hdr)) {
 2162                 size_t l;
 2163                 struct ip6_hdr nip6;
 2164 
 2165                 l = off - sizeof(struct ip6_hdr);
 2166                 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
 2167                 m_adj(m, l);
 2168                 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
 2169                 if (m->m_len < l) {
 2170                         if ((m = m_pullup(m, l)) == NULL)
 2171                                 return;
 2172                 }
 2173                 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
 2174         } else /* off == sizeof(struct ip6_hdr) */ {
 2175                 size_t l;
 2176                 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
 2177                 if (m->m_len < l) {
 2178                         if ((m = m_pullup(m, l)) == NULL)
 2179                                 return;
 2180                 }
 2181         }
 2182         plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
 2183         ip6 = mtod(m, struct ip6_hdr *);
 2184         ip6->ip6_nxt = IPPROTO_ICMPV6;
 2185         icmp6 = (struct icmp6_hdr *)(ip6 + 1);
 2186         type = icmp6->icmp6_type; /* keep type for statistics */
 2187         code = icmp6->icmp6_code; /* ditto. */
 2188 
 2189         origdst = ip6->ip6_dst;
 2190         /*
 2191          * ip6_input() drops a packet if its src is multicast.
 2192          * So, the src is never multicast.
 2193          */
 2194         ip6->ip6_dst = ip6->ip6_src;
 2195 
 2196         /*
 2197          * If the incoming packet was addressed directly to us (i.e. unicast),
 2198          * use dst as the src for the reply.
 2199          * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
 2200          * (for example) when we encounter an error while forwarding procedure
 2201          * destined to a duplicated address of ours.
 2202          * Note that ip6_getdstifaddr() may fail if we are in an error handling
 2203          * procedure of an outgoing packet of our own, in which case we need
 2204          * to search in the ifaddr list.
 2205          */
 2206         if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
 2207                 if ((ia = ip6_getdstifaddr(m))) {
 2208                         if (!(ia->ia6_flags &
 2209                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
 2210                                 srcp = &ia->ia_addr.sin6_addr;
 2211                 } else {
 2212                         struct sockaddr_in6 d;
 2213 
 2214                         bzero(&d, sizeof(d));
 2215                         d.sin6_family = AF_INET6;
 2216                         d.sin6_len = sizeof(d);
 2217                         d.sin6_addr = origdst;
 2218                         ia = (struct in6_ifaddr *)
 2219                             ifa_ifwithaddr((struct sockaddr *)&d);
 2220                         if (ia &&
 2221                             !(ia->ia6_flags &
 2222                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
 2223                                 srcp = &ia->ia_addr.sin6_addr;
 2224                         }
 2225                 }
 2226         }
 2227 
 2228         if (srcp == NULL) {
 2229                 int e;
 2230                 struct sockaddr_in6 sin6;
 2231                 struct route_in6 ro;
 2232 
 2233                 /*
 2234                  * This case matches to multicasts, our anycast, or unicasts
 2235                  * that we do not own.  Select a source address based on the
 2236                  * source address of the erroneous packet.
 2237                  */
 2238                 bzero(&sin6, sizeof(sin6));
 2239                 sin6.sin6_family = AF_INET6;
 2240                 sin6.sin6_len = sizeof(sin6);
 2241                 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
 2242 
 2243                 bzero(&ro, sizeof(ro));
 2244                 e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &src);
 2245                 if (ro.ro_rt)
 2246                         RTFREE(ro.ro_rt); /* XXX: we could use this */
 2247                 if (e) {
 2248                         char ip6buf[INET6_ADDRSTRLEN];
 2249                         nd6log((LOG_DEBUG,
 2250                             "icmp6_reflect: source can't be determined: "
 2251                             "dst=%s, error=%d\n",
 2252                             ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
 2253                         goto bad;
 2254                 }
 2255                 srcp = &src;
 2256         }
 2257 
 2258         ip6->ip6_src = *srcp;
 2259         ip6->ip6_flow = 0;
 2260         ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
 2261         ip6->ip6_vfc |= IPV6_VERSION;
 2262         ip6->ip6_nxt = IPPROTO_ICMPV6;
 2263         if (outif)
 2264                 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
 2265         else if (m->m_pkthdr.rcvif) {
 2266                 /* XXX: This may not be the outgoing interface */
 2267                 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
 2268         } else
 2269                 ip6->ip6_hlim = V_ip6_defhlim;
 2270 
 2271         icmp6->icmp6_cksum = 0;
 2272         icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
 2273             sizeof(struct ip6_hdr), plen);
 2274 
 2275         /*
 2276          * XXX option handling
 2277          */
 2278 
 2279         m->m_flags &= ~(M_BCAST|M_MCAST);
 2280 
 2281         ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
 2282         if (outif)
 2283                 icmp6_ifoutstat_inc(outif, type, code);
 2284 
 2285         if (ia != NULL)
 2286                 ifa_free(&ia->ia_ifa);
 2287         return;
 2288 
 2289  bad:
 2290         if (ia != NULL)
 2291                 ifa_free(&ia->ia_ifa);
 2292         m_freem(m);
 2293         return;
 2294 }
 2295 
 2296 void
 2297 icmp6_fasttimo(void)
 2298 {
 2299 
 2300         mld_fasttimo();
 2301 }
 2302 
 2303 void
 2304 icmp6_slowtimo(void)
 2305 {
 2306 
 2307         mld_slowtimo();
 2308 }
 2309 
 2310 static const char *
 2311 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
 2312     struct in6_addr *tgt6)
 2313 {
 2314         static char buf[1024];
 2315         char ip6bufs[INET6_ADDRSTRLEN];
 2316         char ip6bufd[INET6_ADDRSTRLEN];
 2317         char ip6buft[INET6_ADDRSTRLEN];
 2318         snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
 2319             ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
 2320             ip6_sprintf(ip6buft, tgt6));
 2321         return buf;
 2322 }
 2323 
 2324 void
 2325 icmp6_redirect_input(struct mbuf *m, int off)
 2326 {
 2327         struct ifnet *ifp;
 2328         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 2329         struct nd_redirect *nd_rd;
 2330         int icmp6len = ntohs(ip6->ip6_plen);
 2331         char *lladdr = NULL;
 2332         int lladdrlen = 0;
 2333         struct rtentry *rt = NULL;
 2334         int is_router;
 2335         int is_onlink;
 2336         struct in6_addr src6 = ip6->ip6_src;
 2337         struct in6_addr redtgt6;
 2338         struct in6_addr reddst6;
 2339         union nd_opts ndopts;
 2340         char ip6buf[INET6_ADDRSTRLEN];
 2341 
 2342         M_ASSERTPKTHDR(m);
 2343         KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__));
 2344 
 2345         ifp = m->m_pkthdr.rcvif;
 2346 
 2347         /* XXX if we are router, we don't update route by icmp6 redirect */
 2348         if (V_ip6_forwarding)
 2349                 goto freeit;
 2350         if (!V_icmp6_rediraccept)
 2351                 goto freeit;
 2352 
 2353 #ifndef PULLDOWN_TEST
 2354         IP6_EXTHDR_CHECK(m, off, icmp6len,);
 2355         nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
 2356 #else
 2357         IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
 2358         if (nd_rd == NULL) {
 2359                 ICMP6STAT_INC(icp6s_tooshort);
 2360                 return;
 2361         }
 2362 #endif
 2363         redtgt6 = nd_rd->nd_rd_target;
 2364         reddst6 = nd_rd->nd_rd_dst;
 2365 
 2366         if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
 2367             in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
 2368                 goto freeit;
 2369         }
 2370 
 2371         /* validation */
 2372         if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
 2373                 nd6log((LOG_ERR,
 2374                     "ICMP6 redirect sent from %s rejected; "
 2375                     "must be from linklocal\n",
 2376                     ip6_sprintf(ip6buf, &src6)));
 2377                 goto bad;
 2378         }
 2379         if (ip6->ip6_hlim != 255) {
 2380                 nd6log((LOG_ERR,
 2381                     "ICMP6 redirect sent from %s rejected; "
 2382                     "hlim=%d (must be 255)\n",
 2383                     ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
 2384                 goto bad;
 2385         }
 2386     {
 2387         /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
 2388         struct sockaddr_in6 sin6;
 2389         struct in6_addr *gw6;
 2390 
 2391         bzero(&sin6, sizeof(sin6));
 2392         sin6.sin6_family = AF_INET6;
 2393         sin6.sin6_len = sizeof(struct sockaddr_in6);
 2394         bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
 2395         rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, RT_DEFAULT_FIB);
 2396         if (rt) {
 2397                 if (rt->rt_gateway == NULL ||
 2398                     rt->rt_gateway->sa_family != AF_INET6) {
 2399                         RTFREE_LOCKED(rt);
 2400                         nd6log((LOG_ERR,
 2401                             "ICMP6 redirect rejected; no route "
 2402                             "with inet6 gateway found for redirect dst: %s\n",
 2403                             icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2404                         goto bad;
 2405                 }
 2406 
 2407                 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
 2408                 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
 2409                         RTFREE_LOCKED(rt);
 2410                         nd6log((LOG_ERR,
 2411                             "ICMP6 redirect rejected; "
 2412                             "not equal to gw-for-src=%s (must be same): "
 2413                             "%s\n",
 2414                             ip6_sprintf(ip6buf, gw6),
 2415                             icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2416                         goto bad;
 2417                 }
 2418         } else {
 2419                 nd6log((LOG_ERR,
 2420                     "ICMP6 redirect rejected; "
 2421                     "no route found for redirect dst: %s\n",
 2422                     icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2423                 goto bad;
 2424         }
 2425         RTFREE_LOCKED(rt);
 2426         rt = NULL;
 2427     }
 2428         if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
 2429                 nd6log((LOG_ERR,
 2430                     "ICMP6 redirect rejected; "
 2431                     "redirect dst must be unicast: %s\n",
 2432                     icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2433                 goto bad;
 2434         }
 2435 
 2436         is_router = is_onlink = 0;
 2437         if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
 2438                 is_router = 1;  /* router case */
 2439         if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
 2440                 is_onlink = 1;  /* on-link destination case */
 2441         if (!is_router && !is_onlink) {
 2442                 nd6log((LOG_ERR,
 2443                     "ICMP6 redirect rejected; "
 2444                     "neither router case nor onlink case: %s\n",
 2445                     icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2446                 goto bad;
 2447         }
 2448         /* validation passed */
 2449 
 2450         icmp6len -= sizeof(*nd_rd);
 2451         nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
 2452         if (nd6_options(&ndopts) < 0) {
 2453                 nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n",
 2454                     __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2455                 /* nd6_options have incremented stats */
 2456                 goto freeit;
 2457         }
 2458 
 2459         if (ndopts.nd_opts_tgt_lladdr) {
 2460                 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
 2461                 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
 2462         }
 2463 
 2464         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
 2465                 nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s "
 2466                     "(if %d, icmp6 packet %d): %s\n",
 2467                     __func__, ip6_sprintf(ip6buf, &redtgt6),
 2468                     ifp->if_addrlen, lladdrlen - 2,
 2469                     icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
 2470                 goto bad;
 2471         }
 2472 
 2473         /* RFC 2461 8.3 */
 2474         nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
 2475             is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
 2476 
 2477         if (!is_onlink) {       /* better router case.  perform rtredirect. */
 2478                 /* perform rtredirect */
 2479                 struct sockaddr_in6 sdst;
 2480                 struct sockaddr_in6 sgw;
 2481                 struct sockaddr_in6 ssrc;
 2482                 u_int fibnum;
 2483 
 2484                 bzero(&sdst, sizeof(sdst));
 2485                 bzero(&sgw, sizeof(sgw));
 2486                 bzero(&ssrc, sizeof(ssrc));
 2487                 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
 2488                 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
 2489                         sizeof(struct sockaddr_in6);
 2490                 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
 2491                 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
 2492                 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
 2493                 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
 2494                         in6_rtredirect((struct sockaddr *)&sdst,
 2495                             (struct sockaddr *)&sgw, (struct sockaddr *)NULL,
 2496                             RTF_GATEWAY | RTF_HOST, (struct sockaddr *)&ssrc,
 2497                             fibnum);
 2498         }
 2499         /* finally update cached route in each socket via pfctlinput */
 2500     {
 2501         struct sockaddr_in6 sdst;
 2502 
 2503         bzero(&sdst, sizeof(sdst));
 2504         sdst.sin6_family = AF_INET6;
 2505         sdst.sin6_len = sizeof(struct sockaddr_in6);
 2506         bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
 2507         pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
 2508 #ifdef IPSEC
 2509         key_sa_routechange((struct sockaddr *)&sdst);
 2510 #endif /* IPSEC */
 2511     }
 2512 
 2513  freeit:
 2514         m_freem(m);
 2515         return;
 2516 
 2517  bad:
 2518         ICMP6STAT_INC(icp6s_badredirect);
 2519         m_freem(m);
 2520 }
 2521 
 2522 void
 2523 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
 2524 {
 2525         struct ifnet *ifp;      /* my outgoing interface */
 2526         struct in6_addr *ifp_ll6;
 2527         struct in6_addr *router_ll6;
 2528         struct ip6_hdr *sip6;   /* m0 as struct ip6_hdr */
 2529         struct mbuf *m = NULL;  /* newly allocated one */
 2530         struct m_tag *mtag;
 2531         struct ip6_hdr *ip6;    /* m as struct ip6_hdr */
 2532         struct nd_redirect *nd_rd;
 2533         struct llentry *ln = NULL;
 2534         size_t maxlen;
 2535         u_char *p;
 2536         struct ifnet *outif = NULL;
 2537         struct sockaddr_in6 src_sa;
 2538 
 2539         icmp6_errcount(ND_REDIRECT, 0);
 2540 
 2541         /* if we are not router, we don't send icmp6 redirect */
 2542         if (!V_ip6_forwarding)
 2543                 goto fail;
 2544 
 2545         /* sanity check */
 2546         if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
 2547                 goto fail;
 2548 
 2549         /*
 2550          * Address check:
 2551          *  the source address must identify a neighbor, and
 2552          *  the destination address must not be a multicast address
 2553          *  [RFC 2461, sec 8.2]
 2554          */
 2555         sip6 = mtod(m0, struct ip6_hdr *);
 2556         bzero(&src_sa, sizeof(src_sa));
 2557         src_sa.sin6_family = AF_INET6;
 2558         src_sa.sin6_len = sizeof(src_sa);
 2559         src_sa.sin6_addr = sip6->ip6_src;
 2560         if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
 2561                 goto fail;
 2562         if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
 2563                 goto fail;      /* what should we do here? */
 2564 
 2565         /* rate limit */
 2566         if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
 2567                 goto fail;
 2568 
 2569         /*
 2570          * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
 2571          * we almost always ask for an mbuf cluster for simplicity.
 2572          * (MHLEN < IPV6_MMTU is almost always true)
 2573          */
 2574 #if IPV6_MMTU >= MCLBYTES
 2575 # error assumption failed about IPV6_MMTU and MCLBYTES
 2576 #endif
 2577         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
 2578         if (m == NULL)
 2579                 goto fail;
 2580         M_SETFIB(m, rt->rt_fibnum);
 2581         maxlen = M_TRAILINGSPACE(m);
 2582         maxlen = min(IPV6_MMTU, maxlen);
 2583         /* just for safety */
 2584         if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
 2585             ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
 2586                 goto fail;
 2587         }
 2588 
 2589         {
 2590                 /* get ip6 linklocal address for ifp(my outgoing interface). */
 2591                 struct in6_ifaddr *ia;
 2592                 if ((ia = in6ifa_ifpforlinklocal(ifp,
 2593                                                  IN6_IFF_NOTREADY|
 2594                                                  IN6_IFF_ANYCAST)) == NULL)
 2595                         goto fail;
 2596                 ifp_ll6 = &ia->ia_addr.sin6_addr;
 2597                 /* XXXRW: reference released prematurely. */
 2598                 ifa_free(&ia->ia_ifa);
 2599         }
 2600 
 2601         /* get ip6 linklocal address for the router. */
 2602         if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
 2603                 struct sockaddr_in6 *sin6;
 2604                 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
 2605                 router_ll6 = &sin6->sin6_addr;
 2606                 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
 2607                         router_ll6 = (struct in6_addr *)NULL;
 2608         } else
 2609                 router_ll6 = (struct in6_addr *)NULL;
 2610 
 2611         /* ip6 */
 2612         ip6 = mtod(m, struct ip6_hdr *);
 2613         ip6->ip6_flow = 0;
 2614         ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
 2615         ip6->ip6_vfc |= IPV6_VERSION;
 2616         /* ip6->ip6_plen will be set later */
 2617         ip6->ip6_nxt = IPPROTO_ICMPV6;
 2618         ip6->ip6_hlim = 255;
 2619         /* ip6->ip6_src must be linklocal addr for my outgoing if. */
 2620         bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
 2621         bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
 2622 
 2623         /* ND Redirect */
 2624         nd_rd = (struct nd_redirect *)(ip6 + 1);
 2625         nd_rd->nd_rd_type = ND_REDIRECT;
 2626         nd_rd->nd_rd_code = 0;
 2627         nd_rd->nd_rd_reserved = 0;
 2628         if (rt->rt_flags & RTF_GATEWAY) {
 2629                 /*
 2630                  * nd_rd->nd_rd_target must be a link-local address in
 2631                  * better router cases.
 2632                  */
 2633                 if (!router_ll6)
 2634                         goto fail;
 2635                 bcopy(router_ll6, &nd_rd->nd_rd_target,
 2636                     sizeof(nd_rd->nd_rd_target));
 2637                 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
 2638                     sizeof(nd_rd->nd_rd_dst));
 2639         } else {
 2640                 /* make sure redtgt == reddst */
 2641                 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
 2642                     sizeof(nd_rd->nd_rd_target));
 2643                 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
 2644                     sizeof(nd_rd->nd_rd_dst));
 2645         }
 2646 
 2647         p = (u_char *)(nd_rd + 1);
 2648 
 2649         if (!router_ll6)
 2650                 goto nolladdropt;
 2651 
 2652         {
 2653                 /* target lladdr option */
 2654                 int len;
 2655                 struct nd_opt_hdr *nd_opt;
 2656                 char *lladdr;
 2657 
 2658                 IF_AFDATA_RLOCK(ifp);
 2659                 ln = nd6_lookup(router_ll6, 0, ifp);
 2660                 IF_AFDATA_RUNLOCK(ifp);
 2661                 if (ln == NULL)
 2662                         goto nolladdropt;
 2663 
 2664                 len = sizeof(*nd_opt) + ifp->if_addrlen;
 2665                 len = (len + 7) & ~7;   /* round by 8 */
 2666                 /* safety check */
 2667                 if (len + (p - (u_char *)ip6) > maxlen)                         
 2668                         goto nolladdropt;
 2669 
 2670                 if (ln->la_flags & LLE_VALID) {
 2671                         nd_opt = (struct nd_opt_hdr *)p;
 2672                         nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
 2673                         nd_opt->nd_opt_len = len >> 3;
 2674                         lladdr = (char *)(nd_opt + 1);
 2675                         bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen);
 2676                         p += len;
 2677                 }
 2678         }
 2679 nolladdropt:
 2680         if (ln != NULL)
 2681                 LLE_RUNLOCK(ln);
 2682                 
 2683         m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
 2684 
 2685         /* just to be safe */
 2686 #ifdef M_DECRYPTED      /*not openbsd*/
 2687         if (m0->m_flags & M_DECRYPTED)
 2688                 goto noredhdropt;
 2689 #endif
 2690         if (p - (u_char *)ip6 > maxlen)
 2691                 goto noredhdropt;
 2692 
 2693         {
 2694                 /* redirected header option */
 2695                 int len;
 2696                 struct nd_opt_rd_hdr *nd_opt_rh;
 2697 
 2698                 /*
 2699                  * compute the maximum size for icmp6 redirect header option.
 2700                  * XXX room for auth header?
 2701                  */
 2702                 len = maxlen - (p - (u_char *)ip6);
 2703                 len &= ~7;
 2704 
 2705                 /* This is just for simplicity. */
 2706                 if (m0->m_pkthdr.len != m0->m_len) {
 2707                         if (m0->m_next) {
 2708                                 m_freem(m0->m_next);
 2709                                 m0->m_next = NULL;
 2710                         }
 2711                         m0->m_pkthdr.len = m0->m_len;
 2712                 }
 2713 
 2714                 /*
 2715                  * Redirected header option spec (RFC2461 4.6.3) talks nothing
 2716                  * about padding/truncate rule for the original IP packet.
 2717                  * From the discussion on IPv6imp in Feb 1999,
 2718                  * the consensus was:
 2719                  * - "attach as much as possible" is the goal
 2720                  * - pad if not aligned (original size can be guessed by
 2721                  *   original ip6 header)
 2722                  * Following code adds the padding if it is simple enough,
 2723                  * and truncates if not.
 2724                  */
 2725                 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
 2726                         panic("assumption failed in %s:%d", __FILE__,
 2727                             __LINE__);
 2728 
 2729                 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
 2730                         /* not enough room, truncate */
 2731                         m0->m_pkthdr.len = m0->m_len = len -
 2732                             sizeof(*nd_opt_rh);
 2733                 } else {
 2734                         /* enough room, pad or truncate */
 2735                         size_t extra;
 2736 
 2737                         extra = m0->m_pkthdr.len % 8;
 2738                         if (extra) {
 2739                                 /* pad if easy enough, truncate if not */
 2740                                 if (8 - extra <= M_TRAILINGSPACE(m0)) {
 2741                                         /* pad */
 2742                                         m0->m_len += (8 - extra);
 2743                                         m0->m_pkthdr.len += (8 - extra);
 2744                                 } else {
 2745                                         /* truncate */
 2746                                         m0->m_pkthdr.len -= extra;
 2747                                         m0->m_len -= extra;
 2748                                 }
 2749                         }
 2750                         len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
 2751                         m0->m_pkthdr.len = m0->m_len = len -
 2752                             sizeof(*nd_opt_rh);
 2753                 }
 2754 
 2755                 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
 2756                 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
 2757                 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
 2758                 nd_opt_rh->nd_opt_rh_len = len >> 3;
 2759                 p += sizeof(*nd_opt_rh);
 2760                 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
 2761 
 2762                 /* connect m0 to m */
 2763                 m_tag_delete_chain(m0, NULL);
 2764                 m0->m_flags &= ~M_PKTHDR;
 2765                 m->m_next = m0;
 2766                 m->m_pkthdr.len = m->m_len + m0->m_len;
 2767                 m0 = NULL;
 2768         }
 2769 noredhdropt:;
 2770         if (m0) {
 2771                 m_freem(m0);
 2772                 m0 = NULL;
 2773         }
 2774 
 2775         /* XXX: clear embedded link IDs in the inner header */
 2776         in6_clearscope(&sip6->ip6_src);
 2777         in6_clearscope(&sip6->ip6_dst);
 2778         in6_clearscope(&nd_rd->nd_rd_target);
 2779         in6_clearscope(&nd_rd->nd_rd_dst);
 2780 
 2781         ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
 2782 
 2783         nd_rd->nd_rd_cksum = 0;
 2784         nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
 2785             sizeof(*ip6), ntohs(ip6->ip6_plen));
 2786 
 2787         if (send_sendso_input_hook != NULL) {
 2788                 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short),
 2789                         M_NOWAIT);
 2790                 if (mtag == NULL)
 2791                         goto fail;
 2792                 *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type;
 2793                 m_tag_prepend(m, mtag);
 2794         }
 2795 
 2796         /* send the packet to outside... */
 2797         ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
 2798         if (outif) {
 2799                 icmp6_ifstat_inc(outif, ifs6_out_msg);
 2800                 icmp6_ifstat_inc(outif, ifs6_out_redirect);
 2801         }
 2802         ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
 2803 
 2804         return;
 2805 
 2806 fail:
 2807         if (m)
 2808                 m_freem(m);
 2809         if (m0)
 2810                 m_freem(m0);
 2811 }
 2812 
 2813 /*
 2814  * ICMPv6 socket option processing.
 2815  */
 2816 int
 2817 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
 2818 {
 2819         int error = 0;
 2820         int optlen;
 2821         struct inpcb *inp = sotoinpcb(so);
 2822         int level, op, optname;
 2823 
 2824         if (sopt) {
 2825                 level = sopt->sopt_level;
 2826                 op = sopt->sopt_dir;
 2827                 optname = sopt->sopt_name;
 2828                 optlen = sopt->sopt_valsize;
 2829         } else
 2830                 level = op = optname = optlen = 0;
 2831 
 2832         if (level != IPPROTO_ICMPV6) {
 2833                 return EINVAL;
 2834         }
 2835 
 2836         switch (op) {
 2837         case PRCO_SETOPT:
 2838                 switch (optname) {
 2839                 case ICMP6_FILTER:
 2840                     {
 2841                         struct icmp6_filter ic6f;
 2842 
 2843                         if (optlen != sizeof(ic6f)) {
 2844                                 error = EMSGSIZE;
 2845                                 break;
 2846                         }
 2847                         error = sooptcopyin(sopt, &ic6f, optlen, optlen);
 2848                         if (error == 0) {
 2849                                 INP_WLOCK(inp);
 2850                                 *inp->in6p_icmp6filt = ic6f;
 2851                                 INP_WUNLOCK(inp);
 2852                         }
 2853                         break;
 2854                     }
 2855 
 2856                 default:
 2857                         error = ENOPROTOOPT;
 2858                         break;
 2859                 }
 2860                 break;
 2861 
 2862         case PRCO_GETOPT:
 2863                 switch (optname) {
 2864                 case ICMP6_FILTER:
 2865                     {
 2866                         struct icmp6_filter ic6f;
 2867 
 2868                         INP_RLOCK(inp);
 2869                         ic6f = *inp->in6p_icmp6filt;
 2870                         INP_RUNLOCK(inp);
 2871                         error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
 2872                         break;
 2873                     }
 2874 
 2875                 default:
 2876                         error = ENOPROTOOPT;
 2877                         break;
 2878                 }
 2879                 break;
 2880         }
 2881 
 2882         return (error);
 2883 }
 2884 
 2885 /*
 2886  * Perform rate limit check.
 2887  * Returns 0 if it is okay to send the icmp6 packet.
 2888  * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
 2889  * limitation.
 2890  *
 2891  * XXX per-destination/type check necessary?
 2892  *
 2893  * dst - not used at this moment
 2894  * type - not used at this moment
 2895  * code - not used at this moment
 2896  */
 2897 static int
 2898 icmp6_ratelimit(const struct in6_addr *dst, const int type,
 2899     const int code)
 2900 {
 2901         int ret;
 2902 
 2903         ret = 0;        /* okay to send */
 2904 
 2905         /* PPS limit */
 2906         if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
 2907             V_icmp6errppslim)) {
 2908                 /* The packet is subject to rate limit */
 2909                 ret++;
 2910         }
 2911 
 2912         return ret;
 2913 }

Cache object: 3791dc846d7d1b304b1140097229889a


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