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


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

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

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

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

Cache object: a9ce53c57b5ab666187eb6e631cd3e74


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