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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 1e6b5fbbb7d492d9a9ea34964d1e01a2


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