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