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

Cache object: 351d7fd6f269e0ebe06b375c751e09b2


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