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


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

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

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

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

Cache object: 71f0584800adeba0e25ffd768991278f


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