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


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

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

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

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

Cache object: 88995efe498fd5f5a690f490570c9626


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