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


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

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

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

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

Cache object: cad3dccf0902741a077f730d643f7ea6


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