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

Cache object: 4f491d49c26d18886adea4552c652024


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