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