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/nd6.c

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

    1 /*      $FreeBSD: releng/5.4/sys/netinet6/nd6.c 143925 2005-03-21 16:05:36Z glebius $   */
    2 /*      $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 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 #include "opt_inet.h"
   34 #include "opt_inet6.h"
   35 #include "opt_mac.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/callout.h>
   40 #include <sys/mac.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mbuf.h>
   43 #include <sys/socket.h>
   44 #include <sys/sockio.h>
   45 #include <sys/time.h>
   46 #include <sys/kernel.h>
   47 #include <sys/protosw.h>
   48 #include <sys/errno.h>
   49 #include <sys/syslog.h>
   50 #include <sys/queue.h>
   51 #include <sys/sysctl.h>
   52 
   53 #include <net/if.h>
   54 #include <net/if_arc.h>
   55 #include <net/if_dl.h>
   56 #include <net/if_types.h>
   57 #include <net/if_atm.h>
   58 #include <net/iso88025.h>
   59 #include <net/fddi.h>
   60 #include <net/route.h>
   61 
   62 #include <netinet/in.h>
   63 #include <netinet/if_ether.h>
   64 #include <netinet6/in6_var.h>
   65 #include <netinet/ip6.h>
   66 #include <netinet6/ip6_var.h>
   67 #include <netinet6/nd6.h>
   68 #include <netinet/icmp6.h>
   69 
   70 #include <net/net_osdep.h>
   71 
   72 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
   73 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
   74 
   75 #define SIN6(s) ((struct sockaddr_in6 *)s)
   76 #define SDL(s) ((struct sockaddr_dl *)s)
   77 
   78 /* timer values */
   79 int     nd6_prune       = 1;    /* walk list every 1 seconds */
   80 int     nd6_delay       = 5;    /* delay first probe time 5 second */
   81 int     nd6_umaxtries   = 3;    /* maximum unicast query */
   82 int     nd6_mmaxtries   = 3;    /* maximum multicast query */
   83 int     nd6_useloopback = 1;    /* use loopback interface for local traffic */
   84 int     nd6_gctimer     = (60 * 60 * 24); /* 1 day: garbage collection timer */
   85 
   86 /* preventing too many loops in ND option parsing */
   87 int nd6_maxndopt = 10;  /* max # of ND options allowed */
   88 
   89 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
   90 
   91 #ifdef ND6_DEBUG
   92 int nd6_debug = 1;
   93 #else
   94 int nd6_debug = 0;
   95 #endif
   96 
   97 /* for debugging? */
   98 static int nd6_inuse, nd6_allocated;
   99 
  100 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
  101 struct nd_drhead nd_defrouter;
  102 struct nd_prhead nd_prefix = { 0 };
  103 
  104 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
  105 static struct sockaddr_in6 all1_sa;
  106 
  107 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
  108         struct ifnet *));
  109 static void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
  110 static void nd6_slowtimo __P((void *));
  111 static int regen_tmpaddr __P((struct in6_ifaddr *));
  112 
  113 struct callout nd6_slowtimo_ch;
  114 struct callout nd6_timer_ch;
  115 extern struct callout in6_tmpaddrtimer_ch;
  116 
  117 void
  118 nd6_init()
  119 {
  120         static int nd6_init_done = 0;
  121         int i;
  122 
  123         if (nd6_init_done) {
  124                 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
  125                 return;
  126         }
  127 
  128         all1_sa.sin6_family = AF_INET6;
  129         all1_sa.sin6_len = sizeof(struct sockaddr_in6);
  130         for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
  131                 all1_sa.sin6_addr.s6_addr[i] = 0xff;
  132 
  133         /* initialization of the default router list */
  134         TAILQ_INIT(&nd_defrouter);
  135 
  136         nd6_init_done = 1;
  137 
  138         /* start timer */
  139         callout_init(&nd6_slowtimo_ch, 0);
  140         callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
  141             nd6_slowtimo, NULL);
  142 }
  143 
  144 struct nd_ifinfo *
  145 nd6_ifattach(ifp)
  146         struct ifnet *ifp;
  147 {
  148         struct nd_ifinfo *nd;
  149 
  150         nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
  151         bzero(nd, sizeof(*nd));
  152 
  153         nd->initialized = 1;
  154 
  155         nd->chlim = IPV6_DEFHLIM;
  156         nd->basereachable = REACHABLE_TIME;
  157         nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
  158         nd->retrans = RETRANS_TIMER;
  159         /*
  160          * Note that the default value of ip6_accept_rtadv is 0, which means
  161          * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
  162          * here.
  163          */
  164         nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
  165 
  166         /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
  167         nd6_setmtu0(ifp, nd);
  168 
  169         return nd;
  170 }
  171 
  172 void
  173 nd6_ifdetach(nd)
  174         struct nd_ifinfo *nd;
  175 {
  176 
  177         free(nd, M_IP6NDP);
  178 }
  179 
  180 /*
  181  * Reset ND level link MTU. This function is called when the physical MTU
  182  * changes, which means we might have to adjust the ND level MTU.
  183  */
  184 void
  185 nd6_setmtu(ifp)
  186         struct ifnet *ifp;
  187 {
  188 
  189         nd6_setmtu0(ifp, ND_IFINFO(ifp));
  190 }
  191 
  192 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
  193 void
  194 nd6_setmtu0(ifp, ndi)
  195         struct ifnet *ifp;
  196         struct nd_ifinfo *ndi;
  197 {
  198         u_int32_t omaxmtu;
  199 
  200         omaxmtu = ndi->maxmtu;
  201 
  202         switch (ifp->if_type) {
  203         case IFT_ARCNET:
  204                 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
  205                 break;
  206         case IFT_ETHER:
  207                 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
  208                 break;
  209         case IFT_FDDI:
  210                 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
  211                 break;
  212         case IFT_ATM:
  213                 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
  214                 break;
  215         case IFT_IEEE1394:      /* XXX should be IEEE1394MTU(1500) */
  216                 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
  217                 break;
  218 #ifdef IFT_IEEE80211
  219         case IFT_IEEE80211:     /* XXX should be IEEE80211MTU(1500) */
  220                 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
  221                 break;
  222 #endif
  223          case IFT_ISO88025:
  224                  ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
  225                  break;
  226         default:
  227                 ndi->maxmtu = ifp->if_mtu;
  228                 break;
  229         }
  230 
  231         /*
  232          * Decreasing the interface MTU under IPV6 minimum MTU may cause
  233          * undesirable situation.  We thus notify the operator of the change
  234          * explicitly.  The check for omaxmtu is necessary to restrict the
  235          * log to the case of changing the MTU, not initializing it.
  236          */
  237         if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
  238                 log(LOG_NOTICE, "nd6_setmtu0: "
  239                     "new link MTU on %s (%lu) is too small for IPv6\n",
  240                     if_name(ifp), (unsigned long)ndi->maxmtu);
  241         }
  242 
  243         if (ndi->maxmtu > in6_maxmtu)
  244                 in6_setmaxmtu(); /* check all interfaces just in case */
  245 
  246 #undef MIN
  247 }
  248 
  249 void
  250 nd6_option_init(opt, icmp6len, ndopts)
  251         void *opt;
  252         int icmp6len;
  253         union nd_opts *ndopts;
  254 {
  255 
  256         bzero(ndopts, sizeof(*ndopts));
  257         ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
  258         ndopts->nd_opts_last
  259                 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
  260 
  261         if (icmp6len == 0) {
  262                 ndopts->nd_opts_done = 1;
  263                 ndopts->nd_opts_search = NULL;
  264         }
  265 }
  266 
  267 /*
  268  * Take one ND option.
  269  */
  270 struct nd_opt_hdr *
  271 nd6_option(ndopts)
  272         union nd_opts *ndopts;
  273 {
  274         struct nd_opt_hdr *nd_opt;
  275         int olen;
  276 
  277         if (!ndopts)
  278                 panic("ndopts == NULL in nd6_option");
  279         if (!ndopts->nd_opts_last)
  280                 panic("uninitialized ndopts in nd6_option");
  281         if (!ndopts->nd_opts_search)
  282                 return NULL;
  283         if (ndopts->nd_opts_done)
  284                 return NULL;
  285 
  286         nd_opt = ndopts->nd_opts_search;
  287 
  288         /* make sure nd_opt_len is inside the buffer */
  289         if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
  290                 bzero(ndopts, sizeof(*ndopts));
  291                 return NULL;
  292         }
  293 
  294         olen = nd_opt->nd_opt_len << 3;
  295         if (olen == 0) {
  296                 /*
  297                  * Message validation requires that all included
  298                  * options have a length that is greater than zero.
  299                  */
  300                 bzero(ndopts, sizeof(*ndopts));
  301                 return NULL;
  302         }
  303 
  304         ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
  305         if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
  306                 /* option overruns the end of buffer, invalid */
  307                 bzero(ndopts, sizeof(*ndopts));
  308                 return NULL;
  309         } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
  310                 /* reached the end of options chain */
  311                 ndopts->nd_opts_done = 1;
  312                 ndopts->nd_opts_search = NULL;
  313         }
  314         return nd_opt;
  315 }
  316 
  317 /*
  318  * Parse multiple ND options.
  319  * This function is much easier to use, for ND routines that do not need
  320  * multiple options of the same type.
  321  */
  322 int
  323 nd6_options(ndopts)
  324         union nd_opts *ndopts;
  325 {
  326         struct nd_opt_hdr *nd_opt;
  327         int i = 0;
  328 
  329         if (!ndopts)
  330                 panic("ndopts == NULL in nd6_options");
  331         if (!ndopts->nd_opts_last)
  332                 panic("uninitialized ndopts in nd6_options");
  333         if (!ndopts->nd_opts_search)
  334                 return 0;
  335 
  336         while (1) {
  337                 nd_opt = nd6_option(ndopts);
  338                 if (!nd_opt && !ndopts->nd_opts_last) {
  339                         /*
  340                          * Message validation requires that all included
  341                          * options have a length that is greater than zero.
  342                          */
  343                         icmp6stat.icp6s_nd_badopt++;
  344                         bzero(ndopts, sizeof(*ndopts));
  345                         return -1;
  346                 }
  347 
  348                 if (!nd_opt)
  349                         goto skip1;
  350 
  351                 switch (nd_opt->nd_opt_type) {
  352                 case ND_OPT_SOURCE_LINKADDR:
  353                 case ND_OPT_TARGET_LINKADDR:
  354                 case ND_OPT_MTU:
  355                 case ND_OPT_REDIRECTED_HEADER:
  356                         if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
  357                                 nd6log((LOG_INFO,
  358                                     "duplicated ND6 option found (type=%d)\n",
  359                                     nd_opt->nd_opt_type));
  360                                 /* XXX bark? */
  361                         } else {
  362                                 ndopts->nd_opt_array[nd_opt->nd_opt_type]
  363                                         = nd_opt;
  364                         }
  365                         break;
  366                 case ND_OPT_PREFIX_INFORMATION:
  367                         if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
  368                                 ndopts->nd_opt_array[nd_opt->nd_opt_type]
  369                                         = nd_opt;
  370                         }
  371                         ndopts->nd_opts_pi_end =
  372                                 (struct nd_opt_prefix_info *)nd_opt;
  373                         break;
  374                 default:
  375                         /*
  376                          * Unknown options must be silently ignored,
  377                          * to accomodate future extension to the protocol.
  378                          */
  379                         nd6log((LOG_DEBUG,
  380                             "nd6_options: unsupported option %d - "
  381                             "option ignored\n", nd_opt->nd_opt_type));
  382                 }
  383 
  384 skip1:
  385                 i++;
  386                 if (i > nd6_maxndopt) {
  387                         icmp6stat.icp6s_nd_toomanyopt++;
  388                         nd6log((LOG_INFO, "too many loop in nd opt\n"));
  389                         break;
  390                 }
  391 
  392                 if (ndopts->nd_opts_done)
  393                         break;
  394         }
  395 
  396         return 0;
  397 }
  398 
  399 /*
  400  * ND6 timer routine to expire default route list and prefix list
  401  */
  402 void
  403 nd6_timer(ignored_arg)
  404         void    *ignored_arg;
  405 {
  406         int s;
  407         struct llinfo_nd6 *ln;
  408         struct nd_defrouter *dr;
  409         struct nd_prefix *pr;
  410         struct ifnet *ifp;
  411         struct in6_ifaddr *ia6, *nia6;
  412         struct in6_addrlifetime *lt6;
  413 
  414         s = splnet();
  415         callout_reset(&nd6_timer_ch, nd6_prune * hz,
  416             nd6_timer, NULL);
  417 
  418         ln = llinfo_nd6.ln_next;
  419         while (ln && ln != &llinfo_nd6) {
  420                 struct rtentry *rt;
  421                 struct sockaddr_in6 *dst;
  422                 struct llinfo_nd6 *next = ln->ln_next;
  423                 /* XXX: used for the DELAY case only: */
  424                 struct nd_ifinfo *ndi = NULL;
  425 
  426                 if ((rt = ln->ln_rt) == NULL) {
  427                         ln = next;
  428                         continue;
  429                 }
  430                 if ((ifp = rt->rt_ifp) == NULL) {
  431                         ln = next;
  432                         continue;
  433                 }
  434                 ndi = ND_IFINFO(ifp);
  435                 dst = (struct sockaddr_in6 *)rt_key(rt);
  436 
  437                 if (ln->ln_expire > time_second) {
  438                         ln = next;
  439                         continue;
  440                 }
  441 
  442                 /* sanity check */
  443                 if (!rt)
  444                         panic("rt=0 in nd6_timer(ln=%p)", ln);
  445                 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
  446                         panic("rt_llinfo(%p) is not equal to ln(%p)",
  447                               rt->rt_llinfo, ln);
  448                 if (!dst)
  449                         panic("dst=0 in nd6_timer(ln=%p)", ln);
  450 
  451                 switch (ln->ln_state) {
  452                 case ND6_LLINFO_INCOMPLETE:
  453                         if (ln->ln_asked < nd6_mmaxtries) {
  454                                 ln->ln_asked++;
  455                                 ln->ln_expire = time_second +
  456                                         ND_IFINFO(ifp)->retrans / 1000;
  457                                 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
  458                                         ln, 0);
  459                         } else {
  460                                 struct mbuf *m = ln->ln_hold;
  461                                 if (m) {
  462                                         if (rt->rt_ifp) {
  463                                                 /*
  464                                                  * Fake rcvif to make ICMP error
  465                                                  * more helpful in diagnosing
  466                                                  * for the receiver.
  467                                                  * XXX: should we consider
  468                                                  * older rcvif?
  469                                                  */
  470                                                 m->m_pkthdr.rcvif = rt->rt_ifp;
  471                                         }
  472                                         icmp6_error(m, ICMP6_DST_UNREACH,
  473                                                     ICMP6_DST_UNREACH_ADDR, 0);
  474                                         ln->ln_hold = NULL;
  475                                 }
  476                                 next = nd6_free(rt);
  477                         }
  478                         break;
  479                 case ND6_LLINFO_REACHABLE:
  480                         if (ln->ln_expire) {
  481                                 ln->ln_state = ND6_LLINFO_STALE;
  482                                 ln->ln_expire = time_second + nd6_gctimer;
  483                         }
  484                         break;
  485 
  486                 case ND6_LLINFO_STALE:
  487                         /* Garbage Collection(RFC 2461 5.3) */
  488                         if (ln->ln_expire)
  489                                 next = nd6_free(rt);
  490                         break;
  491 
  492                 case ND6_LLINFO_DELAY:
  493                         if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
  494                                 /* We need NUD */
  495                                 ln->ln_asked = 1;
  496                                 ln->ln_state = ND6_LLINFO_PROBE;
  497                                 ln->ln_expire = time_second +
  498                                         ndi->retrans / 1000;
  499                                 nd6_ns_output(ifp, &dst->sin6_addr,
  500                                               &dst->sin6_addr,
  501                                               ln, 0);
  502                         } else {
  503                                 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
  504                                 ln->ln_expire = time_second + nd6_gctimer;
  505                         }
  506                         break;
  507                 case ND6_LLINFO_PROBE:
  508                         if (ln->ln_asked < nd6_umaxtries) {
  509                                 ln->ln_asked++;
  510                                 ln->ln_expire = time_second +
  511                                         ND_IFINFO(ifp)->retrans / 1000;
  512                                 nd6_ns_output(ifp, &dst->sin6_addr,
  513                                                &dst->sin6_addr, ln, 0);
  514                         } else {
  515                                 next = nd6_free(rt);
  516                         }
  517                         break;
  518                 }
  519                 ln = next;
  520         }
  521 
  522         /* expire default router list */
  523         dr = TAILQ_FIRST(&nd_defrouter);
  524         while (dr) {
  525                 if (dr->expire && dr->expire < time_second) {
  526                         struct nd_defrouter *t;
  527                         t = TAILQ_NEXT(dr, dr_entry);
  528                         defrtrlist_del(dr);
  529                         dr = t;
  530                 } else {
  531                         dr = TAILQ_NEXT(dr, dr_entry);
  532                 }
  533         }
  534 
  535         /*
  536          * expire interface addresses.
  537          * in the past the loop was inside prefix expiry processing.
  538          * However, from a stricter speci-confrmance standpoint, we should
  539          * rather separate address lifetimes and prefix lifetimes.
  540          */
  541   addrloop:
  542         for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
  543                 nia6 = ia6->ia_next;
  544                 /* check address lifetime */
  545                 lt6 = &ia6->ia6_lifetime;
  546                 if (IFA6_IS_INVALID(ia6)) {
  547                         int regen = 0;
  548 
  549                         /*
  550                          * If the expiring address is temporary, try
  551                          * regenerating a new one.  This would be useful when
  552                          * we suspended a laptop PC, then turned it on after a
  553                          * period that could invalidate all temporary
  554                          * addresses.  Although we may have to restart the
  555                          * loop (see below), it must be after purging the
  556                          * address.  Otherwise, we'd see an infinite loop of
  557                          * regeneration.
  558                          */
  559                         if (ip6_use_tempaddr &&
  560                             (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
  561                                 if (regen_tmpaddr(ia6) == 0)
  562                                         regen = 1;
  563                         }
  564 
  565                         in6_purgeaddr(&ia6->ia_ifa);
  566 
  567                         if (regen)
  568                                 goto addrloop; /* XXX: see below */
  569                 }
  570                 if (IFA6_IS_DEPRECATED(ia6)) {
  571                         int oldflags = ia6->ia6_flags;
  572 
  573                         ia6->ia6_flags |= IN6_IFF_DEPRECATED;
  574 
  575                         /*
  576                          * If a temporary address has just become deprecated,
  577                          * regenerate a new one if possible.
  578                          */
  579                         if (ip6_use_tempaddr &&
  580                             (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
  581                             (oldflags & IN6_IFF_DEPRECATED) == 0) {
  582 
  583                                 if (regen_tmpaddr(ia6) == 0) {
  584                                         /*
  585                                          * A new temporary address is
  586                                          * generated.
  587                                          * XXX: this means the address chain
  588                                          * has changed while we are still in
  589                                          * the loop.  Although the change
  590                                          * would not cause disaster (because
  591                                          * it's not a deletion, but an
  592                                          * addition,) we'd rather restart the
  593                                          * loop just for safety.  Or does this
  594                                          * significantly reduce performance??
  595                                          */
  596                                         goto addrloop;
  597                                 }
  598                         }
  599                 } else {
  600                         /*
  601                          * A new RA might have made a deprecated address
  602                          * preferred.
  603                          */
  604                         ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
  605                 }
  606         }
  607 
  608         /* expire prefix list */
  609         pr = nd_prefix.lh_first;
  610         while (pr) {
  611                 /*
  612                  * check prefix lifetime.
  613                  * since pltime is just for autoconf, pltime processing for
  614                  * prefix is not necessary.
  615                  */
  616                 if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
  617                         struct nd_prefix *t;
  618                         t = pr->ndpr_next;
  619 
  620                         /*
  621                          * address expiration and prefix expiration are
  622                          * separate.  NEVER perform in6_purgeaddr here.
  623                          */
  624 
  625                         prelist_remove(pr);
  626                         pr = t;
  627                 } else
  628                         pr = pr->ndpr_next;
  629         }
  630         splx(s);
  631 }
  632 
  633 static int
  634 regen_tmpaddr(ia6)
  635         struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
  636 {
  637         struct ifaddr *ifa;
  638         struct ifnet *ifp;
  639         struct in6_ifaddr *public_ifa6 = NULL;
  640 
  641         ifp = ia6->ia_ifa.ifa_ifp;
  642         for (ifa = ifp->if_addrlist.tqh_first; ifa;
  643              ifa = ifa->ifa_list.tqe_next) {
  644                 struct in6_ifaddr *it6;
  645 
  646                 if (ifa->ifa_addr->sa_family != AF_INET6)
  647                         continue;
  648 
  649                 it6 = (struct in6_ifaddr *)ifa;
  650 
  651                 /* ignore no autoconf addresses. */
  652                 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
  653                         continue;
  654 
  655                 /* ignore autoconf addresses with different prefixes. */
  656                 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
  657                         continue;
  658 
  659                 /*
  660                  * Now we are looking at an autoconf address with the same
  661                  * prefix as ours.  If the address is temporary and is still
  662                  * preferred, do not create another one.  It would be rare, but
  663                  * could happen, for example, when we resume a laptop PC after
  664                  * a long period.
  665                  */
  666                 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
  667                     !IFA6_IS_DEPRECATED(it6)) {
  668                         public_ifa6 = NULL;
  669                         break;
  670                 }
  671 
  672                 /*
  673                  * This is a public autoconf address that has the same prefix
  674                  * as ours.  If it is preferred, keep it.  We can't break the
  675                  * loop here, because there may be a still-preferred temporary
  676                  * address with the prefix.
  677                  */
  678                 if (!IFA6_IS_DEPRECATED(it6))
  679                     public_ifa6 = it6;
  680         }
  681 
  682         if (public_ifa6 != NULL) {
  683                 int e;
  684 
  685                 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
  686                         log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
  687                             " tmp addr,errno=%d\n", e);
  688                         return (-1);
  689                 }
  690                 return (0);
  691         }
  692 
  693         return (-1);
  694 }
  695 
  696 /*
  697  * Nuke neighbor cache/prefix/default router management table, right before
  698  * ifp goes away.
  699  */
  700 void
  701 nd6_purge(ifp)
  702         struct ifnet *ifp;
  703 {
  704         struct llinfo_nd6 *ln, *nln;
  705         struct nd_defrouter *dr, *ndr, drany;
  706         struct nd_prefix *pr, *npr;
  707 
  708         /* Nuke default router list entries toward ifp */
  709         if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
  710                 /*
  711                  * The first entry of the list may be stored in
  712                  * the routing table, so we'll delete it later.
  713                  */
  714                 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
  715                         ndr = TAILQ_NEXT(dr, dr_entry);
  716                         if (dr->ifp == ifp)
  717                                 defrtrlist_del(dr);
  718                 }
  719                 dr = TAILQ_FIRST(&nd_defrouter);
  720                 if (dr->ifp == ifp)
  721                         defrtrlist_del(dr);
  722         }
  723 
  724         /* Nuke prefix list entries toward ifp */
  725         for (pr = nd_prefix.lh_first; pr; pr = npr) {
  726                 npr = pr->ndpr_next;
  727                 if (pr->ndpr_ifp == ifp) {
  728                         /*
  729                          * Previously, pr->ndpr_addr is removed as well,
  730                          * but I strongly believe we don't have to do it.
  731                          * nd6_purge() is only called from in6_ifdetach(),
  732                          * which removes all the associated interface addresses
  733                          * by itself.
  734                          * (jinmei@kame.net 20010129)
  735                          */
  736                         prelist_remove(pr);
  737                 }
  738         }
  739 
  740         /* cancel default outgoing interface setting */
  741         if (nd6_defifindex == ifp->if_index)
  742                 nd6_setdefaultiface(0);
  743 
  744         if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
  745                 /* refresh default router list */
  746                 bzero(&drany, sizeof(drany));
  747                 defrouter_delreq(&drany, 0);
  748                 defrouter_select();
  749         }
  750 
  751         /*
  752          * Nuke neighbor cache entries for the ifp.
  753          * Note that rt->rt_ifp may not be the same as ifp,
  754          * due to KAME goto ours hack.  See RTM_RESOLVE case in
  755          * nd6_rtrequest(), and ip6_input().
  756          */
  757         ln = llinfo_nd6.ln_next;
  758         while (ln && ln != &llinfo_nd6) {
  759                 struct rtentry *rt;
  760                 struct sockaddr_dl *sdl;
  761 
  762                 nln = ln->ln_next;
  763                 rt = ln->ln_rt;
  764                 if (rt && rt->rt_gateway &&
  765                     rt->rt_gateway->sa_family == AF_LINK) {
  766                         sdl = (struct sockaddr_dl *)rt->rt_gateway;
  767                         if (sdl->sdl_index == ifp->if_index)
  768                                 nln = nd6_free(rt);
  769                 }
  770                 ln = nln;
  771         }
  772 }
  773 
  774 struct rtentry *
  775 nd6_lookup(addr6, create, ifp)
  776         struct in6_addr *addr6;
  777         int create;
  778         struct ifnet *ifp;
  779 {
  780         struct rtentry *rt;
  781         struct sockaddr_in6 sin6;
  782 
  783         bzero(&sin6, sizeof(sin6));
  784         sin6.sin6_len = sizeof(struct sockaddr_in6);
  785         sin6.sin6_family = AF_INET6;
  786         sin6.sin6_addr = *addr6;
  787         rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
  788         if (rt) {
  789                 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
  790                         /*
  791                          * This is the case for the default route.
  792                          * If we want to create a neighbor cache for the
  793                          * address, we should free the route for the
  794                          * destination and allocate an interface route.
  795                          */
  796                         RTFREE_LOCKED(rt);
  797                         rt = 0;
  798                 }
  799         }
  800         if (!rt) {
  801                 if (create && ifp) {
  802                         int e;
  803 
  804                         /*
  805                          * If no route is available and create is set,
  806                          * we allocate a host route for the destination
  807                          * and treat it like an interface route.
  808                          * This hack is necessary for a neighbor which can't
  809                          * be covered by our own prefix.
  810                          */
  811                         struct ifaddr *ifa =
  812                             ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
  813                         if (ifa == NULL)
  814                                 return (NULL);
  815 
  816                         /*
  817                          * Create a new route.  RTF_LLINFO is necessary
  818                          * to create a Neighbor Cache entry for the
  819                          * destination in nd6_rtrequest which will be
  820                          * called in rtrequest via ifa->ifa_rtrequest.
  821                          */
  822                         if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
  823                             ifa->ifa_addr, (struct sockaddr *)&all1_sa,
  824                             (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
  825                             ~RTF_CLONING, &rt)) != 0) {
  826                                 log(LOG_ERR,
  827                                     "nd6_lookup: failed to add route for a "
  828                                     "neighbor(%s), errno=%d\n",
  829                                     ip6_sprintf(addr6), e);
  830                         }
  831                         if (rt == NULL)
  832                                 return (NULL);
  833                         RT_LOCK(rt);
  834                         if (rt->rt_llinfo) {
  835                                 struct llinfo_nd6 *ln =
  836                                     (struct llinfo_nd6 *)rt->rt_llinfo;
  837                                 ln->ln_state = ND6_LLINFO_NOSTATE;
  838                         }
  839                 } else
  840                         return (NULL);
  841         }
  842         RT_LOCK_ASSERT(rt);
  843         RT_REMREF(rt);
  844         /*
  845          * Validation for the entry.
  846          * Note that the check for rt_llinfo is necessary because a cloned
  847          * route from a parent route that has the L flag (e.g. the default
  848          * route to a p2p interface) may have the flag, too, while the
  849          * destination is not actually a neighbor.
  850          * XXX: we can't use rt->rt_ifp to check for the interface, since
  851          *      it might be the loopback interface if the entry is for our
  852          *      own address on a non-loopback interface. Instead, we should
  853          *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
  854          *      interface.
  855          */
  856         if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
  857             rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
  858             (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
  859                 if (create) {
  860                         log(LOG_DEBUG,
  861                             "nd6_lookup: failed to lookup %s (if = %s)\n",
  862                             ip6_sprintf(addr6),
  863                             ifp ? if_name(ifp) : "unspec");
  864                         /* xxx more logs... kazu */
  865                 }
  866                 RT_UNLOCK(rt);
  867                 return (NULL);
  868         }
  869         RT_UNLOCK(rt);          /* XXX not ready to return rt locked */
  870         return (rt);
  871 }
  872 
  873 /*
  874  * Test whether a given IPv6 address is a neighbor or not, ignoring
  875  * the actual neighbor cache.  The neighbor cache is ignored in order
  876  * to not reenter the routing code from within itself.
  877  */
  878 static int
  879 nd6_is_new_addr_neighbor(addr, ifp)
  880         struct sockaddr_in6 *addr;
  881         struct ifnet *ifp;
  882 {
  883         struct nd_prefix *pr;
  884 
  885         /*
  886          * A link-local address is always a neighbor.
  887          * XXX: we should use the sin6_scope_id field rather than the embedded
  888          * interface index.
  889          * XXX: a link does not necessarily specify a single interface.
  890          */
  891         if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
  892             ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
  893                 return (1);
  894 
  895         /*
  896          * If the address matches one of our addresses,
  897          * it should be a neighbor.
  898          * If the address matches one of our on-link prefixes, it should be a
  899          * neighbor.
  900          */
  901         for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
  902                 if (pr->ndpr_ifp != ifp)
  903                         continue;
  904 
  905                 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
  906                         continue;
  907 
  908                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
  909                     &addr->sin6_addr, &pr->ndpr_mask))
  910                         return (1);
  911         }
  912 
  913         /*
  914          * If the default router list is empty, all addresses are regarded
  915          * as on-link, and thus, as a neighbor.
  916          * XXX: we restrict the condition to hosts, because routers usually do
  917          * not have the "default router list".
  918          */
  919         if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
  920             nd6_defifindex == ifp->if_index) {
  921                 return (1);
  922         }
  923 
  924         return (0);
  925 }
  926 
  927 
  928 /*
  929  * Detect if a given IPv6 address identifies a neighbor on a given link.
  930  * XXX: should take care of the destination of a p2p link?
  931  */
  932 int
  933 nd6_is_addr_neighbor(addr, ifp)
  934         struct sockaddr_in6 *addr;
  935         struct ifnet *ifp;
  936 {
  937 
  938         if (nd6_is_new_addr_neighbor(addr, ifp))
  939                 return (1);
  940 
  941         /*
  942          * Even if the address matches none of our addresses, it might be
  943          * in the neighbor cache.
  944          */
  945         if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
  946                 return (1);
  947 
  948         return (0);
  949 }
  950 
  951 /*
  952  * Free an nd6 llinfo entry.
  953  */
  954 struct llinfo_nd6 *
  955 nd6_free(rt)
  956         struct rtentry *rt;
  957 {
  958         struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
  959         struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
  960         struct nd_defrouter *dr;
  961 
  962         /*
  963          * we used to have pfctlinput(PRC_HOSTDEAD) here.
  964          * even though it is not harmful, it was not really necessary.
  965          */
  966 
  967         if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
  968                 int s;
  969                 s = splnet();
  970                 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
  971                     rt->rt_ifp);
  972 
  973                 if (ln->ln_router || dr) {
  974                         /*
  975                          * rt6_flush must be called whether or not the neighbor
  976                          * is in the Default Router List.
  977                          * See a corresponding comment in nd6_na_input().
  978                          */
  979                         rt6_flush(&in6, rt->rt_ifp);
  980                 }
  981 
  982                 if (dr) {
  983                         /*
  984                          * Unreachablity of a router might affect the default
  985                          * router selection and on-link detection of advertised
  986                          * prefixes.
  987                          */
  988 
  989                         /*
  990                          * Temporarily fake the state to choose a new default
  991                          * router and to perform on-link determination of
  992                          * prefixes correctly.
  993                          * Below the state will be set correctly,
  994                          * or the entry itself will be deleted.
  995                          */
  996                         ln->ln_state = ND6_LLINFO_INCOMPLETE;
  997 
  998                         /*
  999                          * Since defrouter_select() does not affect the
 1000                          * on-link determination and MIP6 needs the check
 1001                          * before the default router selection, we perform
 1002                          * the check now.
 1003                          */
 1004                         pfxlist_onlink_check();
 1005 
 1006                         if (dr == TAILQ_FIRST(&nd_defrouter)) {
 1007                                 /*
 1008                                  * It is used as the current default router,
 1009                                  * so we have to move it to the end of the
 1010                                  * list and choose a new one.
 1011                                  * XXX: it is not very efficient if this is
 1012                                  *      the only router.
 1013                                  */
 1014                                 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
 1015                                 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
 1016 
 1017                                 defrouter_select();
 1018                         }
 1019                 }
 1020                 splx(s);
 1021         }
 1022 
 1023         /*
 1024          * Before deleting the entry, remember the next entry as the
 1025          * return value.  We need this because pfxlist_onlink_check() above
 1026          * might have freed other entries (particularly the old next entry) as
 1027          * a side effect (XXX).
 1028          */
 1029         next = ln->ln_next;
 1030 
 1031         /*
 1032          * Detach the route from the routing tree and the list of neighbor
 1033          * caches, and disable the route entry not to be used in already
 1034          * cached routes.
 1035          */
 1036         rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
 1037             rt_mask(rt), 0, (struct rtentry **)0);
 1038 
 1039         return (next);
 1040 }
 1041 
 1042 /*
 1043  * Upper-layer reachability hint for Neighbor Unreachability Detection.
 1044  *
 1045  * XXX cost-effective metods?
 1046  */
 1047 void
 1048 nd6_nud_hint(rt, dst6, force)
 1049         struct rtentry *rt;
 1050         struct in6_addr *dst6;
 1051         int force;
 1052 {
 1053         struct llinfo_nd6 *ln;
 1054 
 1055         /*
 1056          * If the caller specified "rt", use that.  Otherwise, resolve the
 1057          * routing table by supplied "dst6".
 1058          */
 1059         if (!rt) {
 1060                 if (!dst6)
 1061                         return;
 1062                 if (!(rt = nd6_lookup(dst6, 0, NULL)))
 1063                         return;
 1064         }
 1065 
 1066         if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
 1067             (rt->rt_flags & RTF_LLINFO) == 0 ||
 1068             !rt->rt_llinfo || !rt->rt_gateway ||
 1069             rt->rt_gateway->sa_family != AF_LINK) {
 1070                 /* This is not a host route. */
 1071                 return;
 1072         }
 1073 
 1074         ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1075         if (ln->ln_state < ND6_LLINFO_REACHABLE)
 1076                 return;
 1077 
 1078         /*
 1079          * if we get upper-layer reachability confirmation many times,
 1080          * it is possible we have false information.
 1081          */
 1082         if (!force) {
 1083                 ln->ln_byhint++;
 1084                 if (ln->ln_byhint > nd6_maxnudhint)
 1085                         return;
 1086         }
 1087 
 1088         ln->ln_state = ND6_LLINFO_REACHABLE;
 1089         if (ln->ln_expire)
 1090                 ln->ln_expire = time_second +
 1091                         ND_IFINFO(rt->rt_ifp)->reachable;
 1092 }
 1093 
 1094 void
 1095 nd6_rtrequest(req, rt, info)
 1096         int     req;
 1097         struct rtentry *rt;
 1098         struct rt_addrinfo *info; /* xxx unused */
 1099 {
 1100         struct sockaddr *gate = rt->rt_gateway;
 1101         struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1102         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
 1103         struct ifnet *ifp = rt->rt_ifp;
 1104         struct ifaddr *ifa;
 1105 
 1106         RT_LOCK_ASSERT(rt);
 1107 
 1108         if ((rt->rt_flags & RTF_GATEWAY) != 0)
 1109                 return;
 1110 
 1111         if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
 1112                 /*
 1113                  * This is probably an interface direct route for a link
 1114                  * which does not need neighbor caches (e.g. fe80::%lo0/64).
 1115                  * We do not need special treatment below for such a route.
 1116                  * Moreover, the RTF_LLINFO flag which would be set below
 1117                  * would annoy the ndp(8) command.
 1118                  */
 1119                 return;
 1120         }
 1121 
 1122         if (req == RTM_RESOLVE &&
 1123             (nd6_need_cache(ifp) == 0 || /* stf case */
 1124              !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
 1125              ifp))) {
 1126                 /*
 1127                  * FreeBSD and BSD/OS often make a cloned host route based
 1128                  * on a less-specific route (e.g. the default route).
 1129                  * If the less specific route does not have a "gateway"
 1130                  * (this is the case when the route just goes to a p2p or an
 1131                  * stf interface), we'll mistakenly make a neighbor cache for
 1132                  * the host route, and will see strange neighbor solicitation
 1133                  * for the corresponding destination.  In order to avoid the
 1134                  * confusion, we check if the destination of the route is
 1135                  * a neighbor in terms of neighbor discovery, and stop the
 1136                  * process if not.  Additionally, we remove the LLINFO flag
 1137                  * so that ndp(8) will not try to get the neighbor information
 1138                  * of the destination.
 1139                  */
 1140                 rt->rt_flags &= ~RTF_LLINFO;
 1141                 return;
 1142         }
 1143 
 1144         switch (req) {
 1145         case RTM_ADD:
 1146                 /*
 1147                  * There is no backward compatibility :)
 1148                  *
 1149                  * if ((rt->rt_flags & RTF_HOST) == 0 &&
 1150                  *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
 1151                  *         rt->rt_flags |= RTF_CLONING;
 1152                  */
 1153                 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
 1154                         /*
 1155                          * Case 1: This route should come from
 1156                          * a route to interface.  RTF_LLINFO flag is set
 1157                          * for a host route whose destination should be
 1158                          * treated as on-link.
 1159                          */
 1160                         rt_setgate(rt, rt_key(rt),
 1161                                    (struct sockaddr *)&null_sdl);
 1162                         gate = rt->rt_gateway;
 1163                         SDL(gate)->sdl_type = ifp->if_type;
 1164                         SDL(gate)->sdl_index = ifp->if_index;
 1165                         if (ln)
 1166                                 ln->ln_expire = time_second;
 1167                         if (ln && ln->ln_expire == 0) {
 1168                                 /* kludge for desktops */
 1169                                 ln->ln_expire = 1;
 1170                         }
 1171                         if ((rt->rt_flags & RTF_CLONING) != 0)
 1172                                 break;
 1173                 }
 1174                 /*
 1175                  * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
 1176                  * We don't do that here since llinfo is not ready yet.
 1177                  *
 1178                  * There are also couple of other things to be discussed:
 1179                  * - unsolicited NA code needs improvement beforehand
 1180                  * - RFC2461 says we MAY send multicast unsolicited NA
 1181                  *   (7.2.6 paragraph 4), however, it also says that we
 1182                  *   SHOULD provide a mechanism to prevent multicast NA storm.
 1183                  *   we don't have anything like it right now.
 1184                  *   note that the mechanism needs a mutual agreement
 1185                  *   between proxies, which means that we need to implement
 1186                  *   a new protocol, or a new kludge.
 1187                  * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
 1188                  *   we need to check ip6forwarding before sending it.
 1189                  *   (or should we allow proxy ND configuration only for
 1190                  *   routers?  there's no mention about proxy ND from hosts)
 1191                  */
 1192 #if 0
 1193                 /* XXX it does not work */
 1194                 if (rt->rt_flags & RTF_ANNOUNCE)
 1195                         nd6_na_output(ifp,
 1196                               &SIN6(rt_key(rt))->sin6_addr,
 1197                               &SIN6(rt_key(rt))->sin6_addr,
 1198                               ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
 1199                               1, NULL);
 1200 #endif
 1201                 /* FALLTHROUGH */
 1202         case RTM_RESOLVE:
 1203                 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
 1204                         /*
 1205                          * Address resolution isn't necessary for a point to
 1206                          * point link, so we can skip this test for a p2p link.
 1207                          */
 1208                         if (gate->sa_family != AF_LINK ||
 1209                             gate->sa_len < sizeof(null_sdl)) {
 1210                                 log(LOG_DEBUG,
 1211                                     "nd6_rtrequest: bad gateway value: %s\n",
 1212                                     if_name(ifp));
 1213                                 break;
 1214                         }
 1215                         SDL(gate)->sdl_type = ifp->if_type;
 1216                         SDL(gate)->sdl_index = ifp->if_index;
 1217                 }
 1218                 if (ln != NULL)
 1219                         break;  /* This happens on a route change */
 1220                 /*
 1221                  * Case 2: This route may come from cloning, or a manual route
 1222                  * add with a LL address.
 1223                  */
 1224                 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
 1225                 rt->rt_llinfo = (caddr_t)ln;
 1226                 if (!ln) {
 1227                         log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
 1228                         break;
 1229                 }
 1230                 nd6_inuse++;
 1231                 nd6_allocated++;
 1232                 bzero(ln, sizeof(*ln));
 1233                 ln->ln_rt = rt;
 1234                 /* this is required for "ndp" command. - shin */
 1235                 if (req == RTM_ADD) {
 1236                         /*
 1237                          * gate should have some valid AF_LINK entry,
 1238                          * and ln->ln_expire should have some lifetime
 1239                          * which is specified by ndp command.
 1240                          */
 1241                         ln->ln_state = ND6_LLINFO_REACHABLE;
 1242                         ln->ln_byhint = 0;
 1243                 } else {
 1244                         /*
 1245                          * When req == RTM_RESOLVE, rt is created and
 1246                          * initialized in rtrequest(), so rt_expire is 0.
 1247                          */
 1248                         ln->ln_state = ND6_LLINFO_NOSTATE;
 1249                         ln->ln_expire = time_second;
 1250                 }
 1251                 rt->rt_flags |= RTF_LLINFO;
 1252                 ln->ln_next = llinfo_nd6.ln_next;
 1253                 llinfo_nd6.ln_next = ln;
 1254                 ln->ln_prev = &llinfo_nd6;
 1255                 ln->ln_next->ln_prev = ln;
 1256 
 1257                 /*
 1258                  * check if rt_key(rt) is one of my address assigned
 1259                  * to the interface.
 1260                  */
 1261                 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
 1262                     &SIN6(rt_key(rt))->sin6_addr);
 1263                 if (ifa) {
 1264                         caddr_t macp = nd6_ifptomac(ifp);
 1265                         ln->ln_expire = 0;
 1266                         ln->ln_state = ND6_LLINFO_REACHABLE;
 1267                         ln->ln_byhint = 0;
 1268                         if (macp) {
 1269                                 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
 1270                                 SDL(gate)->sdl_alen = ifp->if_addrlen;
 1271                         }
 1272                         if (nd6_useloopback) {
 1273                                 rt->rt_ifp = &loif[0];  /* XXX */
 1274                                 /*
 1275                                  * Make sure rt_ifa be equal to the ifaddr
 1276                                  * corresponding to the address.
 1277                                  * We need this because when we refer
 1278                                  * rt_ifa->ia6_flags in ip6_input, we assume
 1279                                  * that the rt_ifa points to the address instead
 1280                                  * of the loopback address.
 1281                                  */
 1282                                 if (ifa != rt->rt_ifa) {
 1283                                         IFAFREE(rt->rt_ifa);
 1284                                         IFAREF(ifa);
 1285                                         rt->rt_ifa = ifa;
 1286                                 }
 1287                         }
 1288                 } else if (rt->rt_flags & RTF_ANNOUNCE) {
 1289                         ln->ln_expire = 0;
 1290                         ln->ln_state = ND6_LLINFO_REACHABLE;
 1291                         ln->ln_byhint = 0;
 1292 
 1293                         /* join solicited node multicast for proxy ND */
 1294                         if (ifp->if_flags & IFF_MULTICAST) {
 1295                                 struct in6_addr llsol;
 1296                                 int error;
 1297 
 1298                                 llsol = SIN6(rt_key(rt))->sin6_addr;
 1299                                 llsol.s6_addr16[0] = htons(0xff02);
 1300                                 llsol.s6_addr16[1] = htons(ifp->if_index);
 1301                                 llsol.s6_addr32[1] = 0;
 1302                                 llsol.s6_addr32[2] = htonl(1);
 1303                                 llsol.s6_addr8[12] = 0xff;
 1304 
 1305                                 if (!in6_addmulti(&llsol, ifp, &error)) {
 1306                                         nd6log((LOG_ERR, "%s: failed to join "
 1307                                             "%s (errno=%d)\n", if_name(ifp),
 1308                                             ip6_sprintf(&llsol), error));
 1309                                 }
 1310                         }
 1311                 }
 1312                 break;
 1313 
 1314         case RTM_DELETE:
 1315                 if (!ln)
 1316                         break;
 1317                 /* leave from solicited node multicast for proxy ND */
 1318                 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
 1319                     (ifp->if_flags & IFF_MULTICAST) != 0) {
 1320                         struct in6_addr llsol;
 1321                         struct in6_multi *in6m;
 1322 
 1323                         llsol = SIN6(rt_key(rt))->sin6_addr;
 1324                         llsol.s6_addr16[0] = htons(0xff02);
 1325                         llsol.s6_addr16[1] = htons(ifp->if_index);
 1326                         llsol.s6_addr32[1] = 0;
 1327                         llsol.s6_addr32[2] = htonl(1);
 1328                         llsol.s6_addr8[12] = 0xff;
 1329 
 1330                         IN6_LOOKUP_MULTI(llsol, ifp, in6m);
 1331                         if (in6m)
 1332                                 in6_delmulti(in6m);
 1333                 }
 1334                 nd6_inuse--;
 1335                 ln->ln_next->ln_prev = ln->ln_prev;
 1336                 ln->ln_prev->ln_next = ln->ln_next;
 1337                 ln->ln_prev = NULL;
 1338                 rt->rt_llinfo = 0;
 1339                 rt->rt_flags &= ~RTF_LLINFO;
 1340                 if (ln->ln_hold)
 1341                         m_freem(ln->ln_hold);
 1342                 Free((caddr_t)ln);
 1343         }
 1344 }
 1345 
 1346 int
 1347 nd6_ioctl(cmd, data, ifp)
 1348         u_long cmd;
 1349         caddr_t data;
 1350         struct ifnet *ifp;
 1351 {
 1352         struct in6_drlist *drl = (struct in6_drlist *)data;
 1353         struct in6_oprlist *oprl = (struct in6_oprlist *)data;
 1354         struct in6_ndireq *ndi = (struct in6_ndireq *)data;
 1355         struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
 1356         struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
 1357         struct nd_defrouter *dr, any;
 1358         struct nd_prefix *pr;
 1359         struct rtentry *rt;
 1360         int i = 0, error = 0;
 1361         int s;
 1362 
 1363         switch (cmd) {
 1364         case SIOCGDRLST_IN6:
 1365                 /*
 1366                  * obsolete API, use sysctl under net.inet6.icmp6
 1367                  */
 1368                 bzero(drl, sizeof(*drl));
 1369                 s = splnet();
 1370                 dr = TAILQ_FIRST(&nd_defrouter);
 1371                 while (dr && i < DRLSTSIZ) {
 1372                         drl->defrouter[i].rtaddr = dr->rtaddr;
 1373                         in6_clearscope(&drl->defrouter[i].rtaddr);
 1374 
 1375                         drl->defrouter[i].flags = dr->flags;
 1376                         drl->defrouter[i].rtlifetime = dr->rtlifetime;
 1377                         drl->defrouter[i].expire = dr->expire;
 1378                         drl->defrouter[i].if_index = dr->ifp->if_index;
 1379                         i++;
 1380                         dr = TAILQ_NEXT(dr, dr_entry);
 1381                 }
 1382                 splx(s);
 1383                 break;
 1384         case SIOCGPRLST_IN6:
 1385                 /*
 1386                  * obsolete API, use sysctl under net.inet6.icmp6
 1387                  *
 1388                  * XXX the structure in6_prlist was changed in backward-
 1389                  * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
 1390                  * in6_prlist is used for nd6_sysctl() - fill_prlist().
 1391                  */
 1392                 /*
 1393                  * XXX meaning of fields, especialy "raflags", is very
 1394                  * differnet between RA prefix list and RR/static prefix list.
 1395                  * how about separating ioctls into two?
 1396                  */
 1397                 bzero(oprl, sizeof(*oprl));
 1398                 s = splnet();
 1399                 pr = nd_prefix.lh_first;
 1400                 while (pr && i < PRLSTSIZ) {
 1401                         struct nd_pfxrouter *pfr;
 1402                         int j;
 1403 
 1404                         (void)in6_embedscope(&oprl->prefix[i].prefix,
 1405                             &pr->ndpr_prefix, NULL, NULL);
 1406                         oprl->prefix[i].raflags = pr->ndpr_raf;
 1407                         oprl->prefix[i].prefixlen = pr->ndpr_plen;
 1408                         oprl->prefix[i].vltime = pr->ndpr_vltime;
 1409                         oprl->prefix[i].pltime = pr->ndpr_pltime;
 1410                         oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
 1411                         oprl->prefix[i].expire = pr->ndpr_expire;
 1412 
 1413                         pfr = pr->ndpr_advrtrs.lh_first;
 1414                         j = 0;
 1415                         while (pfr) {
 1416                                 if (j < DRLSTSIZ) {
 1417 #define RTRADDR oprl->prefix[i].advrtr[j]
 1418                                         RTRADDR = pfr->router->rtaddr;
 1419                                         in6_clearscope(&RTRADDR);
 1420 #undef RTRADDR
 1421                                 }
 1422                                 j++;
 1423                                 pfr = pfr->pfr_next;
 1424                         }
 1425                         oprl->prefix[i].advrtrs = j;
 1426                         oprl->prefix[i].origin = PR_ORIG_RA;
 1427 
 1428                         i++;
 1429                         pr = pr->ndpr_next;
 1430                 }
 1431                 splx(s);
 1432 
 1433                 break;
 1434         case OSIOCGIFINFO_IN6:
 1435                 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
 1436                 bzero(&ndi->ndi, sizeof(ndi->ndi));
 1437                 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
 1438                 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
 1439                 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
 1440                 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
 1441                 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
 1442                 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
 1443                 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
 1444                 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
 1445                 break;
 1446         case SIOCGIFINFO_IN6:
 1447                 ndi->ndi = *ND_IFINFO(ifp);
 1448                 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
 1449                 break;
 1450         case SIOCSIFINFO_FLAGS:
 1451                 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
 1452                 break;
 1453         case SIOCSNDFLUSH_IN6:  /* XXX: the ioctl name is confusing... */
 1454                 /* flush default router list */
 1455                 /*
 1456                  * xxx sumikawa: should not delete route if default
 1457                  * route equals to the top of default router list
 1458                  */
 1459                 bzero(&any, sizeof(any));
 1460                 defrouter_delreq(&any, 0);
 1461                 defrouter_select();
 1462                 /* xxx sumikawa: flush prefix list */
 1463                 break;
 1464         case SIOCSPFXFLUSH_IN6:
 1465         {
 1466                 /* flush all the prefix advertised by routers */
 1467                 struct nd_prefix *pr, *next;
 1468 
 1469                 s = splnet();
 1470                 for (pr = nd_prefix.lh_first; pr; pr = next) {
 1471                         struct in6_ifaddr *ia, *ia_next;
 1472 
 1473                         next = pr->ndpr_next;
 1474 
 1475                         if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
 1476                                 continue; /* XXX */
 1477 
 1478                         /* do we really have to remove addresses as well? */
 1479                         for (ia = in6_ifaddr; ia; ia = ia_next) {
 1480                                 /* ia might be removed.  keep the next ptr. */
 1481                                 ia_next = ia->ia_next;
 1482 
 1483                                 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
 1484                                         continue;
 1485 
 1486                                 if (ia->ia6_ndpr == pr)
 1487                                         in6_purgeaddr(&ia->ia_ifa);
 1488                         }
 1489                         prelist_remove(pr);
 1490                 }
 1491                 splx(s);
 1492                 break;
 1493         }
 1494         case SIOCSRTRFLUSH_IN6:
 1495         {
 1496                 /* flush all the default routers */
 1497                 struct nd_defrouter *dr, *next;
 1498 
 1499                 s = splnet();
 1500                 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
 1501                         /*
 1502                          * The first entry of the list may be stored in
 1503                          * the routing table, so we'll delete it later.
 1504                          */
 1505                         for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
 1506                                 next = TAILQ_NEXT(dr, dr_entry);
 1507                                 defrtrlist_del(dr);
 1508                         }
 1509                         defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
 1510                 }
 1511                 splx(s);
 1512                 break;
 1513         }
 1514         case SIOCGNBRINFO_IN6:
 1515         {
 1516                 struct llinfo_nd6 *ln;
 1517                 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
 1518 
 1519                 /*
 1520                  * XXX: KAME specific hack for scoped addresses
 1521                  *      XXXX: for other scopes than link-local?
 1522                  */
 1523                 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
 1524                     IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
 1525                         u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
 1526 
 1527                         if (*idp == 0)
 1528                                 *idp = htons(ifp->if_index);
 1529                 }
 1530 
 1531                 s = splnet();
 1532                 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
 1533                         error = EINVAL;
 1534                         splx(s);
 1535                         break;
 1536                 }
 1537                 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1538                 nbi->state = ln->ln_state;
 1539                 nbi->asked = ln->ln_asked;
 1540                 nbi->isrouter = ln->ln_router;
 1541                 nbi->expire = ln->ln_expire;
 1542                 splx(s);
 1543 
 1544                 break;
 1545         }
 1546         case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
 1547                 ndif->ifindex = nd6_defifindex;
 1548                 break;
 1549         case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
 1550                 return (nd6_setdefaultiface(ndif->ifindex));
 1551         }
 1552         return (error);
 1553 }
 1554 
 1555 /*
 1556  * Create neighbor cache entry and cache link-layer address,
 1557  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
 1558  */
 1559 struct rtentry *
 1560 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
 1561         struct ifnet *ifp;
 1562         struct in6_addr *from;
 1563         char *lladdr;
 1564         int lladdrlen;
 1565         int type;       /* ICMP6 type */
 1566         int code;       /* type dependent information */
 1567 {
 1568         struct rtentry *rt = NULL;
 1569         struct llinfo_nd6 *ln = NULL;
 1570         int is_newentry;
 1571         struct sockaddr_dl *sdl = NULL;
 1572         int do_update;
 1573         int olladdr;
 1574         int llchange;
 1575         int newstate = 0;
 1576 
 1577         if (!ifp)
 1578                 panic("ifp == NULL in nd6_cache_lladdr");
 1579         if (!from)
 1580                 panic("from == NULL in nd6_cache_lladdr");
 1581 
 1582         /* nothing must be updated for unspecified address */
 1583         if (IN6_IS_ADDR_UNSPECIFIED(from))
 1584                 return NULL;
 1585 
 1586         /*
 1587          * Validation about ifp->if_addrlen and lladdrlen must be done in
 1588          * the caller.
 1589          *
 1590          * XXX If the link does not have link-layer adderss, what should
 1591          * we do? (ifp->if_addrlen == 0)
 1592          * Spec says nothing in sections for RA, RS and NA.  There's small
 1593          * description on it in NS section (RFC 2461 7.2.3).
 1594          */
 1595 
 1596         rt = nd6_lookup(from, 0, ifp);
 1597         if (!rt) {
 1598 #if 0
 1599                 /* nothing must be done if there's no lladdr */
 1600                 if (!lladdr || !lladdrlen)
 1601                         return NULL;
 1602 #endif
 1603 
 1604                 rt = nd6_lookup(from, 1, ifp);
 1605                 is_newentry = 1;
 1606         } else {
 1607                 /* do nothing if static ndp is set */
 1608                 if (rt->rt_flags & RTF_STATIC)
 1609                         return NULL;
 1610                 is_newentry = 0;
 1611         }
 1612 
 1613         if (!rt)
 1614                 return NULL;
 1615         if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
 1616 fail:
 1617                 (void)nd6_free(rt);
 1618                 return NULL;
 1619         }
 1620         ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1621         if (!ln)
 1622                 goto fail;
 1623         if (!rt->rt_gateway)
 1624                 goto fail;
 1625         if (rt->rt_gateway->sa_family != AF_LINK)
 1626                 goto fail;
 1627         sdl = SDL(rt->rt_gateway);
 1628 
 1629         olladdr = (sdl->sdl_alen) ? 1 : 0;
 1630         if (olladdr && lladdr) {
 1631                 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
 1632                         llchange = 1;
 1633                 else
 1634                         llchange = 0;
 1635         } else
 1636                 llchange = 0;
 1637 
 1638         /*
 1639          * newentry olladdr  lladdr  llchange   (*=record)
 1640          *      0       n       n       --      (1)
 1641          *      0       y       n       --      (2)
 1642          *      0       n       y       --      (3) * STALE
 1643          *      0       y       y       n       (4) *
 1644          *      0       y       y       y       (5) * STALE
 1645          *      1       --      n       --      (6)   NOSTATE(= PASSIVE)
 1646          *      1       --      y       --      (7) * STALE
 1647          */
 1648 
 1649         if (lladdr) {           /* (3-5) and (7) */
 1650                 /*
 1651                  * Record source link-layer address
 1652                  * XXX is it dependent to ifp->if_type?
 1653                  */
 1654                 sdl->sdl_alen = ifp->if_addrlen;
 1655                 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
 1656         }
 1657 
 1658         if (!is_newentry) {
 1659                 if ((!olladdr && lladdr) ||             /* (3) */
 1660                     (olladdr && lladdr && llchange)) {  /* (5) */
 1661                         do_update = 1;
 1662                         newstate = ND6_LLINFO_STALE;
 1663                 } else                                  /* (1-2,4) */
 1664                         do_update = 0;
 1665         } else {
 1666                 do_update = 1;
 1667                 if (!lladdr)                            /* (6) */
 1668                         newstate = ND6_LLINFO_NOSTATE;
 1669                 else                                    /* (7) */
 1670                         newstate = ND6_LLINFO_STALE;
 1671         }
 1672 
 1673         if (do_update) {
 1674                 /*
 1675                  * Update the state of the neighbor cache.
 1676                  */
 1677                 ln->ln_state = newstate;
 1678 
 1679                 if (ln->ln_state == ND6_LLINFO_STALE) {
 1680                         /*
 1681                          * XXX: since nd6_output() below will cause
 1682                          * state tansition to DELAY and reset the timer,
 1683                          * we must set the timer now, although it is actually
 1684                          * meaningless.
 1685                          */
 1686                         ln->ln_expire = time_second + nd6_gctimer;
 1687 
 1688                         if (ln->ln_hold) {
 1689                                 /*
 1690                                  * we assume ifp is not a p2p here, so just
 1691                                  * set the 2nd argument as the 1st one.
 1692                                  */
 1693                                 nd6_output(ifp, ifp, ln->ln_hold,
 1694                                     (struct sockaddr_in6 *)rt_key(rt), rt);
 1695                                 ln->ln_hold = NULL;
 1696                         }
 1697                 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
 1698                         /* probe right away */
 1699                         ln->ln_expire = time_second;
 1700                 }
 1701         }
 1702 
 1703         /*
 1704          * ICMP6 type dependent behavior.
 1705          *
 1706          * NS: clear IsRouter if new entry
 1707          * RS: clear IsRouter
 1708          * RA: set IsRouter if there's lladdr
 1709          * redir: clear IsRouter if new entry
 1710          *
 1711          * RA case, (1):
 1712          * The spec says that we must set IsRouter in the following cases:
 1713          * - If lladdr exist, set IsRouter.  This means (1-5).
 1714          * - If it is old entry (!newentry), set IsRouter.  This means (7).
 1715          * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
 1716          * A quetion arises for (1) case.  (1) case has no lladdr in the
 1717          * neighbor cache, this is similar to (6).
 1718          * This case is rare but we figured that we MUST NOT set IsRouter.
 1719          *
 1720          * newentry olladdr  lladdr  llchange       NS  RS  RA  redir
 1721          *                                                      D R
 1722          *      0       n       n       --      (1)     c   ?     s
 1723          *      0       y       n       --      (2)     c   s     s
 1724          *      0       n       y       --      (3)     c   s     s
 1725          *      0       y       y       n       (4)     c   s     s
 1726          *      0       y       y       y       (5)     c   s     s
 1727          *      1       --      n       --      (6) c   c       c s
 1728          *      1       --      y       --      (7) c   c   s   c s
 1729          *
 1730          *                                      (c=clear s=set)
 1731          */
 1732         switch (type & 0xff) {
 1733         case ND_NEIGHBOR_SOLICIT:
 1734                 /*
 1735                  * New entry must have is_router flag cleared.
 1736                  */
 1737                 if (is_newentry)        /* (6-7) */
 1738                         ln->ln_router = 0;
 1739                 break;
 1740         case ND_REDIRECT:
 1741                 /*
 1742                  * If the icmp is a redirect to a better router, always set the
 1743                  * is_router flag.  Otherwise, if the entry is newly created,
 1744                  * clear the flag.  [RFC 2461, sec 8.3]
 1745                  */
 1746                 if (code == ND_REDIRECT_ROUTER)
 1747                         ln->ln_router = 1;
 1748                 else if (is_newentry) /* (6-7) */
 1749                         ln->ln_router = 0;
 1750                 break;
 1751         case ND_ROUTER_SOLICIT:
 1752                 /*
 1753                  * is_router flag must always be cleared.
 1754                  */
 1755                 ln->ln_router = 0;
 1756                 break;
 1757         case ND_ROUTER_ADVERT:
 1758                 /*
 1759                  * Mark an entry with lladdr as a router.
 1760                  */
 1761                 if ((!is_newentry && (olladdr || lladdr)) ||    /* (2-5) */
 1762                     (is_newentry && lladdr)) {                  /* (7) */
 1763                         ln->ln_router = 1;
 1764                 }
 1765                 break;
 1766         }
 1767 
 1768         /*
 1769          * When the link-layer address of a router changes, select the
 1770          * best router again.  In particular, when the neighbor entry is newly
 1771          * created, it might affect the selection policy.
 1772          * Question: can we restrict the first condition to the "is_newentry"
 1773          * case?
 1774          * XXX: when we hear an RA from a new router with the link-layer
 1775          * address option, defrouter_select() is called twice, since
 1776          * defrtrlist_update called the function as well.  However, I believe
 1777          * we can compromise the overhead, since it only happens the first
 1778          * time.
 1779          * XXX: although defrouter_select() should not have a bad effect
 1780          * for those are not autoconfigured hosts, we explicitly avoid such
 1781          * cases for safety.
 1782          */
 1783         if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
 1784                 defrouter_select();
 1785 
 1786         return rt;
 1787 }
 1788 
 1789 static void
 1790 nd6_slowtimo(ignored_arg)
 1791     void *ignored_arg;
 1792 {
 1793         int s = splnet();
 1794         struct nd_ifinfo *nd6if;
 1795         struct ifnet *ifp;
 1796 
 1797         callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
 1798             nd6_slowtimo, NULL);
 1799         IFNET_RLOCK();
 1800         for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
 1801                 nd6if = ND_IFINFO(ifp);
 1802                 if (nd6if->basereachable && /* already initialized */
 1803                     (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
 1804                         /*
 1805                          * Since reachable time rarely changes by router
 1806                          * advertisements, we SHOULD insure that a new random
 1807                          * value gets recomputed at least once every few hours.
 1808                          * (RFC 2461, 6.3.4)
 1809                          */
 1810                         nd6if->recalctm = nd6_recalc_reachtm_interval;
 1811                         nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
 1812                 }
 1813         }
 1814         IFNET_RUNLOCK();
 1815         splx(s);
 1816 }
 1817 
 1818 #define senderr(e) { error = (e); goto bad;}
 1819 int
 1820 nd6_output(ifp, origifp, m0, dst, rt0)
 1821         struct ifnet *ifp;
 1822         struct ifnet *origifp;
 1823         struct mbuf *m0;
 1824         struct sockaddr_in6 *dst;
 1825         struct rtentry *rt0;
 1826 {
 1827         struct mbuf *m = m0;
 1828         struct rtentry *rt = rt0;
 1829         struct sockaddr_in6 *gw6 = NULL;
 1830         struct llinfo_nd6 *ln = NULL;
 1831         int error = 0;
 1832 
 1833         if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
 1834                 goto sendpkt;
 1835 
 1836         if (nd6_need_cache(ifp) == 0)
 1837                 goto sendpkt;
 1838 
 1839         /*
 1840          * next hop determination.  This routine is derived from ether_outpout.
 1841          */
 1842 again:
 1843         if (rt) {
 1844                 if ((rt->rt_flags & RTF_UP) == 0) {
 1845                         rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
 1846                         if (rt != NULL) {
 1847                                 RT_REMREF(rt);
 1848                                 RT_UNLOCK(rt);
 1849                                 if (rt->rt_ifp != ifp)
 1850                                         /*
 1851                                          * XXX maybe we should update ifp too,
 1852                                          * but the original code didn't and I
 1853                                          * don't know what is correct here.
 1854                                          */
 1855                                         goto again;
 1856                         } else
 1857                                 senderr(EHOSTUNREACH);
 1858                 }
 1859 
 1860                 if (rt->rt_flags & RTF_GATEWAY) {
 1861                         gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
 1862 
 1863                         /*
 1864                          * We skip link-layer address resolution and NUD
 1865                          * if the gateway is not a neighbor from ND point
 1866                          * of view, regardless of the value of nd_ifinfo.flags.
 1867                          * The second condition is a bit tricky; we skip
 1868                          * if the gateway is our own address, which is
 1869                          * sometimes used to install a route to a p2p link.
 1870                          */
 1871                         if (!nd6_is_addr_neighbor(gw6, ifp) ||
 1872                             in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
 1873                                 /*
 1874                                  * We allow this kind of tricky route only
 1875                                  * when the outgoing interface is p2p.
 1876                                  * XXX: we may need a more generic rule here.
 1877                                  */
 1878                                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
 1879                                         senderr(EHOSTUNREACH);
 1880 
 1881                                 goto sendpkt;
 1882                         }
 1883 
 1884                         if (rt->rt_gwroute == 0)
 1885                                 goto lookup;
 1886                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
 1887                                 RT_LOCK(rt);
 1888                                 rtfree(rt); rt = rt0;
 1889                         lookup:
 1890                                 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
 1891                                 if ((rt = rt->rt_gwroute) == 0)
 1892                                         senderr(EHOSTUNREACH);
 1893                                 RT_UNLOCK(rt);
 1894                         }
 1895                 }
 1896         }
 1897 
 1898         /*
 1899          * Address resolution or Neighbor Unreachability Detection
 1900          * for the next hop.
 1901          * At this point, the destination of the packet must be a unicast
 1902          * or an anycast address(i.e. not a multicast).
 1903          */
 1904 
 1905         /* Look up the neighbor cache for the nexthop */
 1906         if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
 1907                 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1908         else {
 1909                 /*
 1910                  * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
 1911                  * the condition below is not very efficient.  But we believe
 1912                  * it is tolerable, because this should be a rare case.
 1913                  */
 1914                 if (nd6_is_addr_neighbor(dst, ifp) &&
 1915                     (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
 1916                         ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1917         }
 1918         if (!ln || !rt) {
 1919                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
 1920                     !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
 1921                         log(LOG_DEBUG,
 1922                             "nd6_output: can't allocate llinfo for %s "
 1923                             "(ln=%p, rt=%p)\n",
 1924                             ip6_sprintf(&dst->sin6_addr), ln, rt);
 1925                         senderr(EIO);   /* XXX: good error? */
 1926                 }
 1927 
 1928                 goto sendpkt;   /* send anyway */
 1929         }
 1930 
 1931         /* We don't have to do link-layer address resolution on a p2p link. */
 1932         if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
 1933             ln->ln_state < ND6_LLINFO_REACHABLE) {
 1934                 ln->ln_state = ND6_LLINFO_STALE;
 1935                 ln->ln_expire = time_second + nd6_gctimer;
 1936         }
 1937 
 1938         /*
 1939          * The first time we send a packet to a neighbor whose entry is
 1940          * STALE, we have to change the state to DELAY and a sets a timer to
 1941          * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
 1942          * neighbor unreachability detection on expiration.
 1943          * (RFC 2461 7.3.3)
 1944          */
 1945         if (ln->ln_state == ND6_LLINFO_STALE) {
 1946                 ln->ln_asked = 0;
 1947                 ln->ln_state = ND6_LLINFO_DELAY;
 1948                 ln->ln_expire = time_second + nd6_delay;
 1949         }
 1950 
 1951         /*
 1952          * If the neighbor cache entry has a state other than INCOMPLETE
 1953          * (i.e. its link-layer address is already resolved), just
 1954          * send the packet.
 1955          */
 1956         if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
 1957                 goto sendpkt;
 1958 
 1959         /*
 1960          * There is a neighbor cache entry, but no ethernet address
 1961          * response yet.  Replace the held mbuf (if any) with this
 1962          * latest one.
 1963          *
 1964          * This code conforms to the rate-limiting rule described in Section
 1965          * 7.2.2 of RFC 2461, because the timer is set correctly after sending
 1966          * an NS below.
 1967          */
 1968         if (ln->ln_state == ND6_LLINFO_NOSTATE)
 1969                 ln->ln_state = ND6_LLINFO_INCOMPLETE;
 1970         if (ln->ln_hold)
 1971                 m_freem(ln->ln_hold);
 1972         ln->ln_hold = m;
 1973         if (ln->ln_expire) {
 1974                 if (ln->ln_asked < nd6_mmaxtries &&
 1975                     ln->ln_expire < time_second) {
 1976                         ln->ln_asked++;
 1977                         ln->ln_expire = time_second +
 1978                                 ND_IFINFO(ifp)->retrans / 1000;
 1979                         nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
 1980                 }
 1981         }
 1982         return (0);
 1983 
 1984   sendpkt:
 1985 #ifdef IPSEC
 1986         /* clean ipsec history once it goes out of the node */
 1987         ipsec_delaux(m);
 1988 #endif
 1989 
 1990 #ifdef MAC
 1991         mac_create_mbuf_linklayer(ifp, m);
 1992 #endif
 1993         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
 1994                 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
 1995                     rt));
 1996         }
 1997         return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
 1998 
 1999   bad:
 2000         if (m)
 2001                 m_freem(m);
 2002         return (error);
 2003 }
 2004 #undef senderr
 2005 
 2006 int
 2007 nd6_need_cache(ifp)
 2008         struct ifnet *ifp;
 2009 {
 2010         /*
 2011          * XXX: we currently do not make neighbor cache on any interface
 2012          * other than ARCnet, Ethernet, FDDI and GIF.
 2013          *
 2014          * RFC2893 says:
 2015          * - unidirectional tunnels needs no ND
 2016          */
 2017         switch (ifp->if_type) {
 2018         case IFT_ARCNET:
 2019         case IFT_ETHER:
 2020         case IFT_FDDI:
 2021         case IFT_IEEE1394:
 2022 #ifdef IFT_L2VLAN
 2023         case IFT_L2VLAN:
 2024 #endif
 2025 #ifdef IFT_IEEE80211
 2026         case IFT_IEEE80211:
 2027 #endif
 2028 #ifdef IFT_CARP
 2029         case IFT_CARP:
 2030 #endif
 2031         case IFT_GIF:           /* XXX need more cases? */
 2032                 return (1);
 2033         default:
 2034                 return (0);
 2035         }
 2036 }
 2037 
 2038 int
 2039 nd6_storelladdr(ifp, rt0, m, dst, desten)
 2040         struct ifnet *ifp;
 2041         struct rtentry *rt0;
 2042         struct mbuf *m;
 2043         struct sockaddr *dst;
 2044         u_char *desten;
 2045 {
 2046         int i;
 2047         struct sockaddr_dl *sdl;
 2048         struct rtentry *rt;
 2049 
 2050         if (m->m_flags & M_MCAST) {
 2051                 switch (ifp->if_type) {
 2052                 case IFT_ETHER:
 2053                 case IFT_FDDI:
 2054 #ifdef IFT_L2VLAN
 2055                 case IFT_L2VLAN:
 2056 #endif
 2057 #ifdef IFT_IEEE80211
 2058                 case IFT_IEEE80211:
 2059 #endif
 2060                 case IFT_ISO88025:
 2061                         ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
 2062                                                  desten);
 2063                         return (0);
 2064                 case IFT_IEEE1394:
 2065                         /*
 2066                          * netbsd can use if_broadcastaddr, but we don't do so
 2067                          * to reduce # of ifdef.
 2068                          */
 2069                         for (i = 0; i < ifp->if_addrlen; i++)
 2070                                 desten[i] = ~0;
 2071                         return (0);
 2072                 case IFT_ARCNET:
 2073                         *desten = 0;
 2074                         return (0);
 2075                 default:
 2076                         m_freem(m);
 2077                         return (EAFNOSUPPORT);
 2078                 }
 2079         }
 2080 
 2081         i = rt_check(&rt, &rt0, dst);
 2082         if (i) {
 2083                 m_freem(m);
 2084                 return i;
 2085         }
 2086 
 2087         if (rt == NULL) {
 2088                 /* this could happen, if we could not allocate memory */
 2089                 m_freem(m);
 2090                 return (ENOMEM);
 2091         }
 2092         if (rt->rt_gateway->sa_family != AF_LINK) {
 2093                 printf("nd6_storelladdr: something odd happens\n");
 2094                 m_freem(m);
 2095                 return (EINVAL);
 2096         }
 2097         sdl = SDL(rt->rt_gateway);
 2098         if (sdl->sdl_alen == 0) {
 2099                 /* this should be impossible, but we bark here for debugging */
 2100                 printf("nd6_storelladdr: sdl_alen == 0\n");
 2101                 m_freem(m);
 2102                 return (EINVAL);
 2103         }
 2104 
 2105         bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
 2106         return (0);
 2107 }
 2108 
 2109 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
 2110 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
 2111 #ifdef SYSCTL_DECL
 2112 SYSCTL_DECL(_net_inet6_icmp6);
 2113 #endif
 2114 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
 2115         CTLFLAG_RD, nd6_sysctl_drlist, "");
 2116 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
 2117         CTLFLAG_RD, nd6_sysctl_prlist, "");
 2118 
 2119 static int
 2120 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
 2121 {
 2122         int error;
 2123         char buf[1024];
 2124         struct in6_defrouter *d, *de;
 2125         struct nd_defrouter *dr;
 2126 
 2127         if (req->newptr)
 2128                 return EPERM;
 2129         error = 0;
 2130 
 2131         for (dr = TAILQ_FIRST(&nd_defrouter); dr;
 2132              dr = TAILQ_NEXT(dr, dr_entry)) {
 2133                 d = (struct in6_defrouter *)buf;
 2134                 de = (struct in6_defrouter *)(buf + sizeof(buf));
 2135 
 2136                 if (d + 1 <= de) {
 2137                         bzero(d, sizeof(*d));
 2138                         d->rtaddr.sin6_family = AF_INET6;
 2139                         d->rtaddr.sin6_len = sizeof(d->rtaddr);
 2140                         if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
 2141                             dr->ifp) != 0)
 2142                                 log(LOG_ERR,
 2143                                     "scope error in "
 2144                                     "default router list (%s)\n",
 2145                                     ip6_sprintf(&dr->rtaddr));
 2146                         d->flags = dr->flags;
 2147                         d->rtlifetime = dr->rtlifetime;
 2148                         d->expire = dr->expire;
 2149                         d->if_index = dr->ifp->if_index;
 2150                 } else
 2151                         panic("buffer too short");
 2152 
 2153                 error = SYSCTL_OUT(req, buf, sizeof(*d));
 2154                 if (error)
 2155                         break;
 2156         }
 2157 
 2158         return (error);
 2159 }
 2160 
 2161 static int
 2162 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
 2163 {
 2164         int error;
 2165         char buf[1024];
 2166         struct in6_prefix *p, *pe;
 2167         struct nd_prefix *pr;
 2168 
 2169         if (req->newptr)
 2170                 return EPERM;
 2171         error = 0;
 2172 
 2173         for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
 2174                 u_short advrtrs;
 2175                 size_t advance;
 2176                 struct sockaddr_in6 *sin6, *s6;
 2177                 struct nd_pfxrouter *pfr;
 2178 
 2179                 p = (struct in6_prefix *)buf;
 2180                 pe = (struct in6_prefix *)(buf + sizeof(buf));
 2181 
 2182                 if (p + 1 <= pe) {
 2183                         bzero(p, sizeof(*p));
 2184                         sin6 = (struct sockaddr_in6 *)(p + 1);
 2185 
 2186                         p->prefix = pr->ndpr_prefix;
 2187                         if (in6_recoverscope(&p->prefix,
 2188                             &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
 2189                                 log(LOG_ERR,
 2190                                     "scope error in prefix list (%s)\n",
 2191                                     ip6_sprintf(&p->prefix.sin6_addr));
 2192                         p->raflags = pr->ndpr_raf;
 2193                         p->prefixlen = pr->ndpr_plen;
 2194                         p->vltime = pr->ndpr_vltime;
 2195                         p->pltime = pr->ndpr_pltime;
 2196                         p->if_index = pr->ndpr_ifp->if_index;
 2197                         p->expire = pr->ndpr_expire;
 2198                         p->refcnt = pr->ndpr_refcnt;
 2199                         p->flags = pr->ndpr_stateflags;
 2200                         p->origin = PR_ORIG_RA;
 2201                         advrtrs = 0;
 2202                         for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
 2203                              pfr = pfr->pfr_next) {
 2204                                 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
 2205                                         advrtrs++;
 2206                                         continue;
 2207                                 }
 2208                                 s6 = &sin6[advrtrs];
 2209                                 bzero(s6, sizeof(*s6));
 2210                                 s6->sin6_family = AF_INET6;
 2211                                 s6->sin6_len = sizeof(*sin6);
 2212                                 if (in6_recoverscope(s6, &pfr->router->rtaddr,
 2213                                     pfr->router->ifp) != 0)
 2214                                         log(LOG_ERR,
 2215                                             "scope error in "
 2216                                             "prefix list (%s)\n",
 2217                                             ip6_sprintf(&pfr->router->rtaddr));
 2218                                 advrtrs++;
 2219                         }
 2220                         p->advrtrs = advrtrs;
 2221                 } else
 2222                         panic("buffer too short");
 2223 
 2224                 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
 2225                 error = SYSCTL_OUT(req, buf, advance);
 2226                 if (error)
 2227                         break;
 2228         }
 2229 
 2230         return (error);
 2231 }

Cache object: ba51ff3f8408d7a6155b43dd3b17ace6


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