nd6.c revision 1.4 1 /* $NetBSD: nd6.c,v 1.4 1999/07/03 21:30:19 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * XXX
34 * KAME 970409 note:
35 * BSD/OS version heavily modifies this code, related to llinfo.
36 * Since we don't have BSD/OS version of net/route.c in our hand,
37 * I left the code mostly as it was in 970310. -- itojun
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/time.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
50 #include <sys/ioctl.h>
51 #endif
52 #include <sys/syslog.h>
53 #include <sys/queue.h>
54
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_atm.h>
59 #include <net/route.h>
60
61 #include <netinet/in.h>
62 #ifndef __NetBSD__
63 #include <netinet/if_ether.h>
64 #ifdef __FreeBSD__
65 #include <netinet/if_fddi.h>
66 #endif
67 #ifdef __bsdi__
68 #include <net/if_fddi.h>
69 #endif
70 #else /* __NetBSD__ */
71 #include <net/if_ether.h>
72 #include <netinet/if_inarp.h>
73 #include <net/if_fddi.h>
74 #endif /* __NetBSD__ */
75 #include <netinet6/in6_systm.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet6/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/nd6.h>
80 #include <netinet6/icmp6.h>
81
82 #include "loop.h"
83 #ifdef __NetBSD__
84 extern struct ifnet loif[NLOOP];
85 #endif
86
87 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
88 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
89
90 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
91 #define time_second time.tv_sec
92 #endif
93
94 #define SIN6(s) ((struct sockaddr_in6 *)s)
95 #define SDL(s) ((struct sockaddr_dl *)s)
96
97 /* timer values */
98 int nd6_prune = 1; /* walk list every 1 seconds */
99 int nd6_delay = 5; /* delay first probe time 5 second */
100 int nd6_umaxtries = 3; /* maximum unicast query */
101 int nd6_mmaxtries = 3; /* maximum multicast query */
102 int nd6_useloopback = 1; /* use loopback interface for local traffic */
103 int nd6_proxyall = 0; /* enable Proxy Neighbor Advertisement */
104
105 /* for debugging? */
106 static int nd6_inuse, nd6_allocated;
107
108 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
109 struct nd_ifinfo *nd_ifinfo;
110 struct nd_drhead nd_defrouter = { 0 };
111 struct nd_prhead nd_prefix = { 0 };
112
113 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
114 #if 0
115 extern int ip6_forwarding;
116 #endif
117
118 static void nd6_slowtimo __P((void *));
119
120 void
121 nd6_init()
122 {
123 static int nd6_init_done = 0;
124
125 if (nd6_init_done) {
126 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
127 return;
128 }
129 nd6_init_done = 1;
130
131 /* start timer */
132 timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
133 }
134
135 void
136 nd6_ifattach(ifp)
137 struct ifnet *ifp;
138 {
139 static size_t if_indexlim = 8;
140
141 /*
142 * We have some arrays that should be indexed by if_index.
143 * since if_index will grow dynamically, they should grow too.
144 */
145 if (nd_ifinfo == NULL || if_index >= if_indexlim) {
146 size_t n;
147 caddr_t q;
148
149 while (if_index >= if_indexlim)
150 if_indexlim <<= 1;
151
152 /* grow nd_ifinfo */
153 n = if_indexlim * sizeof(struct nd_ifinfo);
154 q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
155 bzero(q, n);
156 if (nd_ifinfo) {
157 bcopy((caddr_t)nd_ifinfo, q, n/2);
158 free((caddr_t)nd_ifinfo, M_IP6NDP);
159 }
160 nd_ifinfo = (struct nd_ifinfo *)q;
161 }
162
163 #define ND nd_ifinfo[ifp->if_index]
164 ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
165 ND.chlim = IPV6_DEFHLIM;
166 ND.basereachable = REACHABLE_TIME;
167 ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
168 ND.retrans = RETRANS_TIMER;
169 ND.receivedra = 0;
170 nd6_setmtu(ifp);
171 #undef ND
172 }
173
174 /*
175 * Reset ND level link MTU. This function is called when the physical MTU
176 * changes, which means we might have to adjust the ND level MTU.
177 */
178 void
179 nd6_setmtu(ifp)
180 struct ifnet *ifp;
181 {
182 #define MIN(a,b) ((a) < (b) ? (a) : (b))
183 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
184 u_long oldmaxmtu = ndi->maxmtu;
185 u_long oldlinkmtu = ndi->linkmtu;
186
187 switch(ifp->if_type) {
188 case IFT_ETHER:
189 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
190 break;
191 #if defined(__FreeBSD__) || defined(__bsdi__)
192 case IFT_FDDI:
193 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
194 break;
195 #endif
196 case IFT_ATM:
197 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
198 break;
199 default:
200 ndi->maxmtu = ifp->if_mtu;
201 break;
202 }
203
204 if (oldmaxmtu != ndi->maxmtu) {
205 /*
206 * If the ND level MTU is not set yet, or if the maxmtu
207 * is reset to a smaller value than the ND level MTU,
208 * also reset the ND level MTU.
209 */
210 if (ndi->linkmtu == 0 ||
211 ndi->maxmtu < ndi->linkmtu) {
212 ndi->linkmtu = ndi->maxmtu;
213 /* also adjust in6_maxmtu if necessary. */
214 if (oldlinkmtu == 0) {
215 /*
216 * XXX: the case analysis is grotty, but
217 * it is not efficient to call in6_setmaxmtu()
218 * here when we are during the initialization
219 * procedure.
220 */
221 if (in6_maxmtu < ndi->linkmtu)
222 in6_maxmtu = ndi->linkmtu;
223 }
224 else
225 in6_setmaxmtu();
226 }
227 }
228 #undef MIN
229 }
230
231 void
232 nd6_option_init(opt, icmp6len, ndopts)
233 void *opt;
234 int icmp6len;
235 union nd_opts *ndopts;
236 {
237 bzero(ndopts, sizeof(*ndopts));
238 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
239 ndopts->nd_opts_last
240 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
241
242 if (icmp6len == 0) {
243 ndopts->nd_opts_done = 1;
244 ndopts->nd_opts_search = NULL;
245 }
246 }
247
248 /*
249 * Take one ND option.
250 */
251 struct nd_opt_hdr *
252 nd6_option(ndopts)
253 union nd_opts *ndopts;
254 {
255 struct nd_opt_hdr *nd_opt;
256 int olen;
257
258 if (!ndopts)
259 panic("ndopts == NULL in nd6_option\n");
260 if (!ndopts->nd_opts_last)
261 panic("uninitialized ndopts in nd6_option\n");
262 if (!ndopts->nd_opts_search)
263 return NULL;
264 if (ndopts->nd_opts_done)
265 return NULL;
266
267 nd_opt = ndopts->nd_opts_search;
268
269 olen = nd_opt->nd_opt_len << 3;
270 if (olen == 0) {
271 /*
272 * Message validation requires that all included
273 * options have a length that is greater than zero.
274 */
275 bzero(ndopts, sizeof(*ndopts));
276 return NULL;
277 }
278
279 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
280 if (!(ndopts->nd_opts_search < ndopts->nd_opts_last)) {
281 ndopts->nd_opts_done = 1;
282 ndopts->nd_opts_search = NULL;
283 }
284 return nd_opt;
285 }
286
287 /*
288 * Parse multiple ND options.
289 * This function is much easier to use, for ND routines that do not need
290 * multiple options of the same type.
291 */
292 int
293 nd6_options(ndopts)
294 union nd_opts *ndopts;
295 {
296 struct nd_opt_hdr *nd_opt;
297 int i = 0;
298
299 if (!ndopts)
300 panic("ndopts == NULL in nd6_options\n");
301 if (!ndopts->nd_opts_last)
302 panic("uninitialized ndopts in nd6_options\n");
303 if (!ndopts->nd_opts_search)
304 return 0;
305
306 while (1) {
307 nd_opt = nd6_option(ndopts);
308 if (!nd_opt && !ndopts->nd_opts_last) {
309 /*
310 * Message validation requires that all included
311 * options have a length that is greater than zero.
312 */
313 bzero(ndopts, sizeof(*ndopts));
314 return -1;
315 }
316
317 if (!nd_opt)
318 goto skip1;
319
320 switch (nd_opt->nd_opt_type) {
321 case ND_OPT_SOURCE_LINKADDR:
322 case ND_OPT_TARGET_LINKADDR:
323 case ND_OPT_MTU:
324 case ND_OPT_REDIRECTED_HEADER:
325 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
326 printf("duplicated ND6 option found "
327 "(type=%d)\n", nd_opt->nd_opt_type);
328 /* XXX bark? */
329 } else {
330 ndopts->nd_opt_array[nd_opt->nd_opt_type]
331 = nd_opt;
332 }
333 break;
334 case ND_OPT_PREFIX_INFORMATION:
335 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
336 ndopts->nd_opt_array[nd_opt->nd_opt_type]
337 = nd_opt;
338 }
339 ndopts->nd_opts_pi_end =
340 (struct nd_opt_prefix_info *)nd_opt;
341 break;
342 default:
343 /*
344 * Unknown options must be silently ignored,
345 * to accomodate future extension to the protocol.
346 */
347 log(LOG_INFO,
348 "nd6_options: unsupported option %d - "
349 "option ignored\n", nd_opt->nd_opt_type);
350 }
351
352 skip1:
353 i++;
354 if (i > 10) {
355 printf("too many loop in nd opt\n");
356 break;
357 }
358
359 if (ndopts->nd_opts_done)
360 break;
361 }
362
363 return 0;
364 }
365
366 /*
367 * ND6 timer routine to expire default route list and prefix list
368 */
369 void
370 nd6_timer(ignored_arg)
371 void *ignored_arg;
372 {
373 int s;
374 register struct llinfo_nd6 *ln;
375 register struct nd_defrouter *dr;
376 register struct nd_prefix *pr;
377
378 s = splnet();
379 timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
380
381 ln = llinfo_nd6.ln_next;
382 /* XXX BSD/OS separates this code -- itojun */
383 while (ln && ln != &llinfo_nd6) {
384 struct rtentry *rt;
385 struct ifnet *ifp;
386 struct sockaddr_in6 *dst;
387 struct llinfo_nd6 *next = ln->ln_next;
388
389 if ((rt = ln->ln_rt) == NULL) {
390 ln = next;
391 continue;
392 }
393 if ((ifp = rt->rt_ifp) == NULL) {
394 ln = next;
395 continue;
396 }
397 dst = (struct sockaddr_in6 *)rt_key(rt);
398
399 if (ln->ln_expire > time_second) {
400 ln = next;
401 continue;
402 }
403
404 /* sanity check */
405 if (!rt)
406 panic("rt=0 in nd6_timer(ln=%p)\n", ln);
407 if (!dst)
408 panic("dst=0 in nd6_timer(ln=%p)\n", ln);
409
410 switch (ln->ln_state) {
411 case ND6_LLINFO_INCOMPLETE:
412 if (ln->ln_asked < nd6_mmaxtries) {
413 ln->ln_asked++;
414 ln->ln_expire = time_second +
415 nd_ifinfo[ifp->if_index].retrans / 1000;
416 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
417 ln, 0);
418 } else {
419 struct mbuf *m = ln->ln_hold;
420 if (m) {
421 if (rt->rt_ifp) {
422 /*
423 * Fake rcvif to make ICMP error
424 * more helpful in diagnosing
425 * for the receiver.
426 * XXX: should we consider
427 * older rcvif?
428 */
429 m->m_pkthdr.rcvif = rt->rt_ifp;
430 }
431 icmp6_error(m, ICMP6_DST_UNREACH,
432 ICMP6_DST_UNREACH_ADDR, 0);
433 ln->ln_hold = NULL;
434 }
435 nd6_free(rt);
436 }
437 break;
438 case ND6_LLINFO_REACHABLE:
439 if (ln->ln_expire) {
440 ln->ln_state = ND6_LLINFO_STALE;
441 }
442 break;
443 /*
444 * ND6_LLINFO_STALE state requires nothing for timer
445 * routine.
446 */
447 case ND6_LLINFO_DELAY:
448 ln->ln_asked = 1;
449 ln->ln_state = ND6_LLINFO_PROBE;
450 ln->ln_expire = time_second +
451 nd_ifinfo[ifp->if_index].retrans / 1000;
452 nd6_ns_output(ifp, &dst->sin6_addr, &dst->sin6_addr,
453 ln, 0);
454 break;
455
456 case ND6_LLINFO_PROBE:
457 if (ln->ln_asked < nd6_umaxtries) {
458 ln->ln_asked++;
459 ln->ln_expire = time_second +
460 nd_ifinfo[ifp->if_index].retrans / 1000;
461 nd6_ns_output(ifp, &dst->sin6_addr,
462 &dst->sin6_addr, ln, 0);
463 } else {
464 nd6_free(rt);
465 }
466 break;
467 case ND6_LLINFO_WAITDELETE:
468 nd6_free(rt);
469 break;
470 }
471 ln = next;
472 }
473
474 /* expire */
475 dr = nd_defrouter.lh_first;
476 while (dr) {
477 if (dr->expire && dr->expire < time_second) {
478 struct nd_defrouter *t;
479 t = dr->dr_next;
480 defrtrlist_del(dr);
481 dr = t;
482 } else
483 dr = dr->dr_next;
484 }
485 pr = nd_prefix.lh_first;
486 while (pr) {
487 struct in6_ifaddr *ia6;
488 struct in6_addrlifetime *lt6;
489
490 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
491 ia6 = NULL;
492 else
493 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
494
495 if (ia6) {
496 /* check address lifetime */
497 lt6 = &ia6->ia6_lifetime;
498 if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
499 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
500 if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
501 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
502 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
503 /* xxx ND_OPT_PI_FLAG_ONLINK processing */
504 }
505 }
506
507 /*
508 * check prefix lifetime.
509 * since pltime is just for autoconf, pltime processing for
510 * prefix is not necessary.
511 *
512 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
513 * can use the old prefix information to validate the
514 * next prefix information to come. See prelist_update()
515 * for actual validation.
516 */
517 if (pr->ndpr_expire
518 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
519 struct nd_prefix *t;
520 t = pr->ndpr_next;
521
522 /*
523 * address expiration and prefix expiration are
524 * separate. NEVER perform in6_ifdel here.
525 */
526
527 prelist_remove(pr);
528 pr = t;
529 } else
530 pr = pr->ndpr_next;
531 }
532 splx(s);
533 }
534
535 static struct sockaddr_in6 all1_sa = {
536 sizeof(struct sockaddr_in6), AF_INET6, 0, 0,
537 {{{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}}}, 0};
538
539 struct rtentry *
540 nd6_lookup(addr6, create, ifp)
541 struct in6_addr *addr6;
542 int create;
543 struct ifnet *ifp;
544 {
545 struct rtentry *rt;
546 struct sockaddr_in6 sin6;
547
548 bzero(&sin6, sizeof(sin6));
549 sin6.sin6_len = sizeof(struct sockaddr_in6);
550 sin6.sin6_family = AF_INET6;
551 sin6.sin6_addr = *addr6;
552 rt = rtalloc1((struct sockaddr *)&sin6, create
553 #ifdef __FreeBSD__
554 , 0UL
555 #endif /*__FreeBSD__*/
556 );
557 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
558 /*
559 * This is the case for the default route.
560 * If we want to create a neighbor cache for the address, we
561 * should free the route for the destination and allocate an
562 * interface route.
563 */
564 if (create) {
565 RTFREE(rt);
566 rt = 0;
567 }
568 }
569 if (!rt) {
570 if (create && ifp) {
571 /*
572 * If no route is available and create is set,
573 * we allocate a host route for the destination
574 * and treat it like an interface route.
575 * This hack is necessary for a neighbor which can't
576 * be covered by our own prefix.
577 */
578 struct ifaddr *ifa =
579 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
580 if (ifa == NULL)
581 return(NULL);
582
583 /*
584 * Create a new route. RTF_LLINFO is necessary
585 * to create a Neighbor Cache entry for the
586 * destination in nd6_rtrequest which will be
587 * called in rtequest via ifa->ifa_rtrequest.
588 */
589 if (rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
590 ifa->ifa_addr,
591 (struct sockaddr *)&all1_sa,
592 (ifa->ifa_flags |
593 RTF_HOST | RTF_LLINFO) & ~RTF_CLONING,
594 &rt))
595 log(LOG_ERR,
596 "nd6_lookup: failed to add route for a "
597 "neighbor(%s)\n", ip6_sprintf(addr6));
598 if (rt == NULL)
599 return(NULL);
600 if (rt->rt_llinfo) {
601 struct llinfo_nd6 *ln =
602 (struct llinfo_nd6 *)rt->rt_llinfo;
603 ln->ln_state = ND6_LLINFO_NOSTATE;
604 }
605 }
606 else
607 return(NULL);
608 }
609 rt->rt_refcnt--;
610 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
611 rt->rt_gateway->sa_family != AF_LINK) {
612 if (create) {
613 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s\n",
614 ip6_sprintf(addr6));
615 /* xxx more logs... kazu */
616 }
617 return(0);
618 }
619 return(rt);
620 }
621
622 /*
623 * Free an nd6 llinfo entry.
624 */
625 void
626 nd6_free(rt)
627 struct rtentry *rt;
628 {
629 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
630 struct sockaddr_dl *sdl;
631
632 if (ln->ln_router) {
633 /* remove from default router list */
634 struct nd_defrouter *dr;
635 struct in6_addr *in6;
636 int s;
637 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
638
639 s = splnet();
640 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->
641 sin6_addr,
642 rt->rt_ifp);
643 if (dr)
644 defrtrlist_del(dr);
645 else if (!ip6_forwarding && ip6_accept_rtadv) {
646 /*
647 * rt6_flush must be called in any case.
648 * see the comment in nd6_na_input().
649 */
650 rt6_flush(in6, rt->rt_ifp);
651 }
652 splx(s);
653 }
654
655 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
656 sdl->sdl_family == AF_LINK) {
657 sdl->sdl_alen = 0;
658 ln->ln_state = ND6_LLINFO_WAITDELETE;
659 ln->ln_asked = 0;
660 rt->rt_flags &= ~RTF_REJECT;
661 return;
662 }
663 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
664 0, (struct rtentry **)0);
665 }
666
667 /*
668 * Upper-layer reachability hint for Neighbor Unreachability Detection.
669 *
670 * XXX cost-effective metods?
671 */
672 void
673 nd6_nud_hint(rt, dst6)
674 struct rtentry *rt;
675 struct in6_addr *dst6;
676 {
677 struct llinfo_nd6 *ln;
678
679 /*
680 * If the caller specified "rt", use that. Otherwise, resolve the
681 * routing table by supplied "dst6".
682 */
683 if (!rt) {
684 if (!dst6)
685 return;
686 if (!(rt = nd6_lookup(dst6, 0, NULL)))
687 return;
688 }
689
690 if ((rt->rt_flags & RTF_GATEWAY)
691 || (rt->rt_flags & RTF_LLINFO) == 0
692 || !rt->rt_llinfo
693 || !rt->rt_gateway
694 || rt->rt_gateway->sa_family != AF_LINK) {
695 /* This is not a host route. */
696 return;
697 }
698
699 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
700 if (ln->ln_state == ND6_LLINFO_INCOMPLETE)
701 return;
702
703 ln->ln_state = ND6_LLINFO_REACHABLE;
704 if (ln->ln_expire)
705 ln->ln_expire = time_second +
706 nd_ifinfo[rt->rt_ifp->if_index].reachable;
707 }
708
709 /*
710 * Resolve an IP6 address into an ethernet address. If success,
711 * desten is filled in. If there is no entry in ndptab,
712 * set one up and multicast a solicitation for the IP6 address.
713 * Hold onto this mbuf and resend it once the address
714 * is finally resolved. A return value of 1 indicates
715 * that desten has been filled in and the packet should be sent
716 * normally; a 0 return indicates that the packet has been
717 * taken over here, either now or for later transmission.
718 */
719 int
720 nd6_resolve(ifp, rt, m, dst, desten)
721 struct ifnet *ifp;
722 struct rtentry *rt;
723 struct mbuf *m;
724 struct sockaddr *dst;
725 u_char *desten;
726 {
727 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
728 struct sockaddr_dl *sdl;
729
730 if (m->m_flags & M_MCAST) {
731 switch (ifp->if_type) {
732 case IFT_ETHER:
733 case IFT_FDDI:
734 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
735 desten);
736 return(1);
737 break;
738 default:
739 return(0);
740 }
741 }
742 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
743 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
744 else {
745 if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
746 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
747 }
748 if (!ln || !rt) {
749 log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
750 ip6_sprintf(&(SIN6(dst)->sin6_addr)));
751 m_freem(m);
752 return(0);
753 }
754 sdl = SDL(rt->rt_gateway);
755 /*
756 * Ckeck the address family and length is valid, the address
757 * is resolved; otherwise, try to resolve.
758 */
759 if (ln->ln_state >= ND6_LLINFO_REACHABLE
760 && sdl->sdl_family == AF_LINK
761 && sdl->sdl_alen != 0) {
762 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
763 if (ln->ln_state == ND6_LLINFO_STALE) {
764 ln->ln_asked = 0;
765 ln->ln_state = ND6_LLINFO_DELAY;
766 ln->ln_expire = time_second + nd6_delay;
767 }
768 return(1);
769 }
770 /*
771 * There is an ndp entry, but no ethernet address
772 * response yet. Replace the held mbuf with this
773 * latest one.
774 *
775 * XXX Does the code conform to rate-limiting rule?
776 * (RFC 2461 7.2.2)
777 */
778 if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
779 ln->ln_state == ND6_LLINFO_NOSTATE)
780 ln->ln_state = ND6_LLINFO_INCOMPLETE;
781 if (ln->ln_hold)
782 m_freem(ln->ln_hold);
783 ln->ln_hold = m;
784 if (ln->ln_expire) {
785 rt->rt_flags &= ~RTF_REJECT;
786 if (ln->ln_asked < nd6_mmaxtries &&
787 ln->ln_expire < time_second) {
788 ln->ln_asked++;
789 ln->ln_expire = time_second +
790 nd_ifinfo[ifp->if_index].retrans / 1000;
791 nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
792 ln, 0);
793 }
794 }
795 return(0);
796 }
797
798 void
799 nd6_rtrequest(req, rt, sa)
800 int req;
801 struct rtentry *rt;
802 struct sockaddr *sa; /* xxx unused */
803 {
804 struct sockaddr *gate = rt->rt_gateway;
805 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
806 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
807 struct ifnet *ifp = rt->rt_ifp;
808 struct ifaddr *ifa;
809
810 if (rt->rt_flags & RTF_GATEWAY)
811 return;
812
813 switch (req) {
814 case RTM_ADD:
815 /*
816 * There is no backward compatibility :)
817 *
818 * if ((rt->rt_flags & RTF_HOST) == 0 &&
819 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
820 * rt->rt_flags |= RTF_CLONING;
821 */
822 if (rt->rt_flags & RTF_CLONING || rt->rt_flags & RTF_LLINFO) {
823 /*
824 * Case 1: This route should come from
825 * a route to interface. RTF_LLINFO flag is set
826 * for a host route whose destination should be
827 * treated as on-link.
828 */
829 rt_setgate(rt, rt_key(rt),
830 (struct sockaddr *)&null_sdl);
831 gate = rt->rt_gateway;
832 SDL(gate)->sdl_type = ifp->if_type;
833 SDL(gate)->sdl_index = ifp->if_index;
834 if (ln)
835 ln->ln_expire = time_second;
836 #if 1
837 if (ln && ln->ln_expire == 0) {
838 /* cludge for desktops */
839 #if 0
840 printf("nd6_request: time.tv_sec is zero; "
841 "treat it as 1\n");
842 #endif
843 ln->ln_expire = 1;
844 }
845 #endif
846 if (rt->rt_flags & RTF_CLONING)
847 break;
848 }
849 /* Announce a new entry if requested. */
850 if (rt->rt_flags & RTF_ANNOUNCE)
851 nd6_na_output(ifp,
852 &SIN6(rt_key(rt))->sin6_addr,
853 &SIN6(rt_key(rt))->sin6_addr,
854 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
855 1);
856 /* FALLTHROUGH */
857 case RTM_RESOLVE:
858 if (gate->sa_family != AF_LINK ||
859 gate->sa_len < sizeof(null_sdl)) {
860 log(LOG_DEBUG, "nd6_rtrequest: bad gateway value\n");
861 break;
862 }
863 SDL(gate)->sdl_type = ifp->if_type;
864 SDL(gate)->sdl_index = ifp->if_index;
865 if (ln != 0)
866 break; /* This happens on a route change */
867 /*
868 * Case 2: This route may come from cloning, or a manual route
869 * add with a LL address.
870 */
871 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
872 rt->rt_llinfo = (caddr_t)ln;
873 if (!ln) {
874 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
875 break;
876 }
877 nd6_inuse++;
878 nd6_allocated++;
879 Bzero(ln, sizeof(*ln));
880 ln->ln_rt = rt;
881 /* this is required for "ndp" command. - shin */
882 if (req == RTM_ADD) {
883 /*
884 * gate should have some valid AF_LINK entry,
885 * and ln->ln_expire should have some lifetime
886 * which is specified by ndp command.
887 */
888 ln->ln_state = ND6_LLINFO_REACHABLE;
889 } else {
890 /*
891 * When req == RTM_RESOLVE, rt is created and
892 * initialized in rtrequest(), so rt_expire is 0.
893 */
894 ln->ln_state = ND6_LLINFO_INCOMPLETE;
895 ln->ln_expire = time_second;
896 }
897 rt->rt_flags |= RTF_LLINFO;
898 #if 0
899 insque(ln, &llinfo_nd6);
900 #else
901 ln->ln_next = llinfo_nd6.ln_next;
902 llinfo_nd6.ln_next = ln;
903 ln->ln_prev = &llinfo_nd6;
904 ln->ln_next->ln_prev = ln;
905 #endif
906
907 /*
908 * check if rt_key(rt) is one of my address assigned
909 * to the interface.
910 */
911 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
912 &SIN6(rt_key(rt))->sin6_addr);
913 if (ifa) {
914 caddr_t macp = nd6_ifptomac(ifp);
915 ln->ln_expire = 0;
916 ln->ln_state = ND6_LLINFO_REACHABLE;
917 if (macp) {
918 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
919 SDL(gate)->sdl_alen = ifp->if_addrlen;
920 }
921 if (nd6_useloopback) {
922 #ifdef __bsdi__
923 extern struct ifnet loif;
924 rt->rt_ifp = &loif; /*XXX*/
925 #endif /*__bsdi__*/
926 #if defined(__FreeBSD__) || defined(__NetBSD__)
927 rt->rt_ifp = &loif[0]; /*XXX*/
928 #endif
929 /*
930 * Make sure rt_ifa be equal to the ifaddr
931 * corresponding to the address.
932 * We need this because when we refer
933 * rt_ifa->ia6_flags in ip6_input, we assume
934 * that the rt_ifa points to the address instead
935 * of the loopback address.
936 */
937 if (ifa != rt->rt_ifa) {
938 rt->rt_ifa->ifa_refcnt--;
939 ifa->ifa_refcnt++;
940 rt->rt_ifa = ifa;
941 }
942 }
943 }
944 break;
945
946 case RTM_DELETE:
947 if (!ln)
948 break;
949 nd6_inuse--;
950 #if 0
951 remque(ln);
952 #else
953 ln->ln_next->ln_prev = ln->ln_prev;
954 ln->ln_prev->ln_next = ln->ln_next;
955 ln->ln_prev = NULL;
956 #endif
957 rt->rt_llinfo = 0;
958 rt->rt_flags &= ~RTF_LLINFO;
959 if (ln->ln_hold)
960 m_freem(ln->ln_hold);
961 Free((caddr_t)ln);
962 }
963 }
964
965 void
966 nd6_p2p_rtrequest(req, rt, sa)
967 int req;
968 struct rtentry *rt;
969 struct sockaddr *sa; /* xxx unused */
970 {
971 struct sockaddr *gate = rt->rt_gateway;
972 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
973 struct ifnet *ifp = rt->rt_ifp;
974 struct ifaddr *ifa;
975
976 if (rt->rt_flags & RTF_GATEWAY)
977 return;
978
979 switch (req) {
980 case RTM_ADD:
981 /*
982 * There is no backward compatibility :)
983 *
984 * if ((rt->rt_flags & RTF_HOST) == 0 &&
985 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
986 * rt->rt_flags |= RTF_CLONING;
987 */
988 if (rt->rt_flags & RTF_CLONING) {
989 /*
990 * Case 1: This route should come from
991 * a route to interface.
992 */
993 rt_setgate(rt, rt_key(rt),
994 (struct sockaddr *)&null_sdl);
995 gate = rt->rt_gateway;
996 SDL(gate)->sdl_type = ifp->if_type;
997 SDL(gate)->sdl_index = ifp->if_index;
998 break;
999 }
1000 /* Announce a new entry if rqquested. */
1001 if (rt->rt_flags & RTF_ANNOUNCE)
1002 nd6_na_output(ifp,
1003 &SIN6(rt_key(rt))->sin6_addr,
1004 &SIN6(rt_key(rt))->sin6_addr,
1005 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1006 1);
1007 /* FALLTHROUGH */
1008 case RTM_RESOLVE:
1009 /*
1010 * check if rt_key(rt) is one of my address assigned
1011 * to the interface.
1012 */
1013 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1014 &SIN6(rt_key(rt))->sin6_addr);
1015 if (ifa) {
1016 if (nd6_useloopback) {
1017 #ifdef __bsdi__
1018 extern struct ifnet loif;
1019 rt->rt_ifp = &loif; /*XXX*/
1020 #endif /*__bsdi__*/
1021 #if defined(__FreeBSD__) || defined(__NetBSD__)
1022 rt->rt_ifp = &loif[0]; /*XXX*/
1023 #endif
1024 }
1025 }
1026 break;
1027 }
1028 }
1029
1030 int
1031 nd6_ioctl(cmd, data, ifp)
1032 u_long cmd;
1033 caddr_t data;
1034 struct ifnet *ifp;
1035 {
1036 struct in6_drlist *drl = (struct in6_drlist *)data;
1037 struct in6_prlist *prl = (struct in6_prlist *)data;
1038 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1039 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1040 struct nd_defrouter *dr, any;
1041 struct nd_prefix *pr;
1042 struct rtentry *rt;
1043 int i = 0, error = 0;
1044 int s;
1045
1046 switch (cmd) {
1047 case SIOCGDRLST_IN6:
1048 bzero(drl, sizeof(*drl));
1049 s = splnet();
1050 dr = nd_defrouter.lh_first;
1051 while (dr && i < DRLSTSIZ) {
1052 drl->defrouter[i].rtaddr = dr->rtaddr;
1053 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1054 /* XXX: need to this hack for KAME stack */
1055 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1056 }
1057 else
1058 log(LOG_ERR,
1059 "default router list contains a "
1060 "non-linklocal address(%s)\n",
1061 ip6_sprintf(&drl->defrouter[i].rtaddr));
1062
1063 drl->defrouter[i].flags = dr->flags;
1064 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1065 drl->defrouter[i].expire = dr->expire;
1066 drl->defrouter[i].if_index = dr->ifp->if_index;
1067 i++;
1068 dr = dr->dr_next;
1069 }
1070 splx(s);
1071 break;
1072 case SIOCGPRLST_IN6:
1073 bzero(prl, sizeof(*prl));
1074 s = splnet();
1075 pr = nd_prefix.lh_first;
1076 while (pr && i < PRLSTSIZ) {
1077 struct nd_pfxrouter *pfr;
1078 int j;
1079
1080 prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1081 prl->prefix[i].raflags = pr->ndpr_raf;
1082 prl->prefix[i].prefixlen = pr->ndpr_plen;
1083 prl->prefix[i].vltime = pr->ndpr_vltime;
1084 prl->prefix[i].pltime = pr->ndpr_pltime;
1085 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1086 prl->prefix[i].expire = pr->ndpr_expire;
1087
1088 pfr = pr->ndpr_advrtrs.lh_first;
1089 j = 0;
1090 while(pfr) {
1091 if (j < DRLSTSIZ) {
1092 #define RTRADDR prl->prefix[i].advrtr[j]
1093 RTRADDR = pfr->router->rtaddr;
1094 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1095 /* XXX: hack for KAME */
1096 RTRADDR.s6_addr16[1] = 0;
1097 }
1098 else
1099 log(LOG_ERR,
1100 "a router(%s) advertises "
1101 "a prefix with "
1102 "non-link local address\n",
1103 ip6_sprintf(&RTRADDR));
1104 #undef RTRADDR
1105 }
1106 j++;
1107 pfr = pfr->pfr_next;
1108 }
1109 prl->prefix[i].advrtrs = j;
1110
1111 i++;
1112 pr = pr->ndpr_next;
1113 }
1114 splx(s);
1115 break;
1116 case SIOCGIFINFO_IN6:
1117 ndi->ndi = nd_ifinfo[ifp->if_index];
1118 break;
1119 case SIOCSNDFLUSH_IN6:
1120 /* flush default router list */
1121 /*
1122 * xxx sumikawa: should not delete route if default
1123 * route equals to the top of default router list
1124 */
1125 bzero(&any, sizeof(any));
1126 defrouter_delreq(&any, 0);
1127 /* xxx sumikawa: flush prefix list */
1128 break;
1129 case SIOCSPFXFLUSH_IN6:
1130 {
1131 /* flush all the prefix advertised by routers */
1132 struct nd_prefix *pr, *next;
1133
1134 s = splnet();
1135 for (pr = nd_prefix.lh_first; pr; pr = next) {
1136 next = pr->ndpr_next;
1137 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1138 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1139 prelist_remove(pr);
1140 }
1141 splx(s);
1142 break;
1143 }
1144 case SIOCSRTRFLUSH_IN6:
1145 {
1146 /* flush all the default routers */
1147 struct nd_defrouter *dr, *next;
1148
1149 s = splnet();
1150 if ((dr = nd_defrouter.lh_first) != NULL) {
1151 /*
1152 * The first entry of the list may be stored in
1153 * the routing table, so we'll delete it later.
1154 */
1155 for (dr = dr->dr_next; dr; dr = next) {
1156 next = dr->dr_next;
1157 defrtrlist_del(dr);
1158 }
1159 defrtrlist_del(nd_defrouter.lh_first);
1160 }
1161 splx(s);
1162 break;
1163 }
1164 case SIOCGNBRINFO_IN6:
1165 {
1166 struct llinfo_nd6 *ln;
1167
1168 s = splnet();
1169 if ((rt = nd6_lookup(&nbi->addr, 0, ifp)) == NULL) {
1170 error = EINVAL;
1171 break;
1172 }
1173 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1174 nbi->state = ln->ln_state;
1175 nbi->asked = ln->ln_asked;
1176 nbi->isrouter = ln->ln_router;
1177 nbi->expire = ln->ln_expire;
1178 splx(s);
1179
1180 break;
1181 }
1182 }
1183 return(error);
1184 }
1185
1186 /*
1187 * Create neighbor cache entry and cache link-layer address,
1188 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1189 */
1190 struct rtentry *
1191 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type)
1192 struct ifnet *ifp;
1193 struct in6_addr *from;
1194 char *lladdr;
1195 int lladdrlen;
1196 int type; /* ICMP6 type */
1197 {
1198 struct rtentry *rt = NULL;
1199 struct llinfo_nd6 *ln = NULL;
1200 int is_newentry;
1201 struct sockaddr_dl *sdl = NULL;
1202 int do_update;
1203 int olladdr;
1204 int llchange;
1205 int newstate = 0;
1206
1207 if (!ifp)
1208 panic("ifp == NULL in nd6_cache_lladdr");
1209 if (!from)
1210 panic("from == NULL in nd6_cache_lladdr");
1211
1212 /* nothing must be updated for unspecified address */
1213 if (IN6_IS_ADDR_UNSPECIFIED(from))
1214 return NULL;
1215
1216 /*
1217 * Validation about ifp->if_addrlen and lladdrlen must be done in
1218 * the caller.
1219 *
1220 * XXX If the link does not have link-layer adderss, what should
1221 * we do? (ifp->if_addrlen == 0)
1222 * Spec says nothing in sections for RA, RS and NA. There's small
1223 * description on it in NS section (RFC 2461 7.2.3).
1224 */
1225
1226 rt = nd6_lookup(from, 0, ifp);
1227 if (!rt) {
1228 #if 0
1229 /* nothing must be done if there's no lladdr */
1230 if (!lladdr || !lladdrlen)
1231 return NULL;
1232 #endif
1233
1234 rt = nd6_lookup(from, 1, ifp);
1235 is_newentry = 1;
1236 } else
1237 is_newentry = 0;
1238
1239 if (!rt)
1240 return NULL;
1241 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1242 fail:
1243 nd6_free(rt);
1244 return NULL;
1245 }
1246 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1247 if (!ln)
1248 goto fail;
1249 if (!rt->rt_gateway)
1250 goto fail;
1251 if (rt->rt_gateway->sa_family != AF_LINK)
1252 goto fail;
1253 sdl = SDL(rt->rt_gateway);
1254
1255 olladdr = (sdl->sdl_alen) ? 1 : 0;
1256 if (olladdr && lladdr) {
1257 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1258 llchange = 1;
1259 else
1260 llchange = 0;
1261 } else
1262 llchange = 0;
1263
1264 /*
1265 * newentry olladdr lladdr llchange (*=record)
1266 * 0 n n -- (1)
1267 * 0 y n -- (2)
1268 * 0 n y -- (3) * STALE
1269 * 0 y y n (4) *
1270 * 0 y y y (5) * STALE
1271 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1272 * 1 -- y -- (7) * STALE
1273 */
1274
1275 if (lladdr) { /*(3-5) and (7)*/
1276 /*
1277 * Record source link-layer address
1278 * XXX is it dependent to ifp->if_type?
1279 */
1280 sdl->sdl_alen = ifp->if_addrlen;
1281 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1282 }
1283
1284 if (!is_newentry) {
1285 if ((!olladdr && lladdr) /*(3)*/
1286 || (olladdr && lladdr && llchange)) { /*(5)*/
1287 do_update = 1;
1288 newstate = ND6_LLINFO_STALE;
1289 } else /*(1-2,4)*/
1290 do_update = 0;
1291 } else {
1292 do_update = 1;
1293 if (!lladdr) /*(6)*/
1294 newstate = ND6_LLINFO_NOSTATE;
1295 else /*(7)*/
1296 newstate = ND6_LLINFO_STALE;
1297 }
1298
1299 if (do_update) {
1300 /*
1301 * Update the state of the neighbor cache.
1302 */
1303 ln->ln_state = newstate;
1304
1305 if (ln->ln_state == ND6_LLINFO_STALE) {
1306 rt->rt_flags &= ~RTF_REJECT;
1307 if (ln->ln_hold) {
1308 (*ifp->if_output)(ifp, ln->ln_hold,
1309 rt_key(rt), rt);
1310 ln->ln_hold = 0;
1311 }
1312 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1313 /* probe right away */
1314 ln->ln_expire = time_second;
1315 }
1316 }
1317
1318 /*
1319 * ICMP6 type dependent behavior.
1320 *
1321 * NS: clear IsRouter if new entry
1322 * RS: clear IsRouter
1323 * RA: set IsRouter if there's lladdr
1324 * redir: clear IsRouter if new entry
1325 *
1326 * RA case, (1):
1327 * The spec says that we must set IsRouter in the following cases:
1328 * - If lladdr exist, set IsRouter. This means (1-5).
1329 * - If it is old entry (!newentry), set IsRouter. This means (7).
1330 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1331 * A quetion arises for (1) case. (1) case has no lladdr in the
1332 * neighbor cache, this is similar to (6).
1333 * This case is rare but we figured that we MUST NOT set IsRouter.
1334 *
1335 * newentry olladdr lladdr llchange NS RS RA redir
1336 * 0 n n -- (1) c ?
1337 * 0 y n -- (2) c s
1338 * 0 n y -- (3) c s
1339 * 0 y y n (4) c s
1340 * 0 y y y (5) c s
1341 * 1 -- n -- (6) c c c
1342 * 1 -- y -- (7) c c s c
1343 *
1344 * (c=clear s=set)
1345 */
1346 switch (type & 0xff) {
1347 case ND_NEIGHBOR_SOLICIT:
1348 case ND_REDIRECT:
1349 /*
1350 * New entry must have is_router flag cleared.
1351 */
1352 if (is_newentry) /*(6-7)*/
1353 ln->ln_router = 0;
1354 break;
1355 case ND_ROUTER_SOLICIT:
1356 /*
1357 * is_router flag must always be cleared.
1358 */
1359 ln->ln_router = 0;
1360 break;
1361 case ND_ROUTER_ADVERT:
1362 /*
1363 * Mark an entry with lladdr as a router.
1364 */
1365 if ((!is_newentry && (olladdr || lladdr)) /*(2-5)*/
1366 || (is_newentry && lladdr)) { /*(7)*/
1367 ln->ln_router = 1;
1368 }
1369 break;
1370 }
1371
1372 return rt;
1373 }
1374
1375 static void
1376 nd6_slowtimo(ignored_arg)
1377 void *ignored_arg;
1378 {
1379 int s = splnet();
1380 register int i;
1381 register struct nd_ifinfo *nd6if;
1382
1383 timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
1384 for (i = 1; i < if_index + 1; i++) {
1385 nd6if = &nd_ifinfo[i];
1386 if (nd6if->basereachable && /* already initialized */
1387 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1388 /*
1389 * Since reachable time rarely changes by router
1390 * advertisements, we SHOULD insure that a new random
1391 * value gets recomputed at least once every few hours.
1392 * (RFC 2461, 6.3.4)
1393 */
1394 nd6if->recalctm = nd6_recalc_reachtm_interval;
1395 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1396 }
1397 }
1398 splx(s);
1399 }
1400
1401 #ifdef NEWIP6OUTPUT
1402 /* for experimental */
1403 #define senderr(e) { error = (e); goto bad;}
1404 int
1405 nd6_output(ifp, m0, dst, rt0)
1406 register struct ifnet *ifp;
1407 struct mbuf *m0;
1408 struct sockaddr_in6 *dst;
1409 struct rtentry *rt0;
1410 {
1411 register struct mbuf *m = m0;
1412 register struct rtentry *rt = rt0;
1413 struct llinfo_nd6 *ln = NULL;
1414 int error = 0;
1415
1416 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1417 goto sendpkt;
1418
1419 /*
1420 * XXX: we currently do not make neighbor cache on any interface
1421 * other than Ethernet and FDDI.
1422 */
1423 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_FDDI)
1424 goto sendpkt;
1425
1426 /*
1427 * next hop determination. This routine is derived from ether_outpout.
1428 */
1429 if (rt) {
1430 if ((rt->rt_flags & RTF_UP) == 0) {
1431 #ifdef __FreeBSD__
1432 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
1433 NULL) {
1434 #else
1435 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
1436 NULL) {
1437 #endif
1438 rt->rt_refcnt--;
1439 if (rt->rt_ifp != ifp)
1440 return nd6_output(ifp, m0, dst, rt); /* XXX: loop care? */
1441 } else
1442 senderr(EHOSTUNREACH);
1443 }
1444 if (rt->rt_flags & RTF_GATEWAY) {
1445 if (rt->rt_gwroute == 0)
1446 goto lookup;
1447 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1448 rtfree(rt); rt = rt0;
1449 #ifdef __FreeBSD__
1450 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1451 #else
1452 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1453 #endif
1454 if ((rt = rt->rt_gwroute) == 0)
1455 senderr(EHOSTUNREACH);
1456 #ifdef __bsdi__
1457 /* the "G" test below also prevents rt == rt0 */
1458 if ((rt->rt_flags & RTF_GATEWAY) ||
1459 (rt->rt_ifp != ifp)) {
1460 rt->rt_refcnt--;
1461 rt0->rt_gwroute = 0;
1462 senderr(EHOSTUNREACH);
1463 }
1464 #endif
1465 }
1466 }
1467 if (rt->rt_flags & RTF_REJECT)
1468 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1469 }
1470
1471 /*
1472 * Address resolution or Neighbor Unreachability Detection
1473 * for the next hop.
1474 * At this point, the destination of the packet must be a unicast
1475 * or an anycast address(i.e. not a multicast).
1476 */
1477
1478 /* Look up the neighbor cache for the nexthop */
1479 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1480 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1481 else {
1482 if ((rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1483 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1484 }
1485 if (!ln || !rt) {
1486 log(LOG_DEBUG, "nd6_output: can't allocate llinfo for %s "
1487 "(ln=%p, rt=%p)\n",
1488 ip6_sprintf(&dst->sin6_addr), ln, rt);
1489 senderr(EIO); /* XXX: good error? */
1490 }
1491
1492
1493 /*
1494 * The first time we send a packet to a neighbor whose entry is
1495 * STALE, we have to change the state to DELAY and a sets a timer to
1496 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1497 * neighbor unreachability detection on expiration.
1498 * (RFC 2461 7.3.3)
1499 */
1500 if (ln->ln_state == ND6_LLINFO_STALE) {
1501 ln->ln_asked = 0;
1502 ln->ln_state = ND6_LLINFO_DELAY;
1503 ln->ln_expire = time_second + nd6_delay;
1504 }
1505
1506 /*
1507 * If the neighbor cache entry has a state other than INCOMPLETE
1508 * (i.e. its link-layer address is already reloved), just
1509 * send the packet.
1510 */
1511 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1512 goto sendpkt;
1513
1514 /*
1515 * There is a neighbor cache entry, but no ethernet address
1516 * response yet. Replace the held mbuf (if any) with this
1517 * latest one.
1518 *
1519 * XXX Does the code conform to rate-limiting rule?
1520 * (RFC 2461 7.2.2)
1521 */
1522 if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
1523 ln->ln_state == ND6_LLINFO_NOSTATE)
1524 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1525 if (ln->ln_hold)
1526 m_freem(ln->ln_hold);
1527 ln->ln_hold = m;
1528 if (ln->ln_expire) {
1529 rt->rt_flags &= ~RTF_REJECT;
1530 if (ln->ln_asked < nd6_mmaxtries &&
1531 ln->ln_expire < time_second) {
1532 ln->ln_asked++;
1533 ln->ln_expire = time_second +
1534 nd_ifinfo[ifp->if_index].retrans / 1000;
1535 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1536 }
1537 }
1538 return(0);
1539
1540 sendpkt:
1541 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1542
1543 bad:
1544 if (m)
1545 m_freem(m);
1546 return (error);
1547 }
1548 #undef senderr
1549
1550 int
1551 nd6_storelladdr(ifp, rt, m, dst, desten)
1552 struct ifnet *ifp;
1553 struct rtentry *rt;
1554 struct mbuf *m;
1555 struct sockaddr *dst;
1556 u_char *desten;
1557 {
1558 struct sockaddr_dl *sdl;
1559
1560 if (m->m_flags & M_MCAST) {
1561 switch (ifp->if_type) {
1562 case IFT_ETHER:
1563 case IFT_FDDI:
1564 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1565 desten);
1566 return(1);
1567 break;
1568 default:
1569 return(0);
1570 }
1571 }
1572
1573 if (rt == NULL ||
1574 rt->rt_gateway->sa_family != AF_LINK) {
1575 printf("nd6_storelladdr: something odd happens\n");
1576 return(0);
1577 }
1578 sdl = SDL(rt->rt_gateway);
1579 if (sdl->sdl_alen != 0)
1580 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1581
1582 return(1);
1583 }
1584 #endif /* NEWIP6OUTPUT */
1585