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  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 76c3285c48bf762c01757263d81052b1


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