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