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

Cache object: 9914c43f2197cd29df9d87b462cb3c09


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