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