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

Cache object: 6b0f011c604c6c0b19258bab687a8773


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