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


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

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

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

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

Cache object: a080e821011dc659c208646c75960945


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