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


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

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

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

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

Cache object: 990689eb75407c0b6893d5195d2bdb21


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