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