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

Cache object: 3d6f4f937bb7f28241556faa6ece16d0


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