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

Cache object: 09f1b2b25cd64ed9c8e3d2cfa233bf02


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