1 /*	$OpenBSD: nd6_rtr.c,v 1.33 2004/11/17 03:22:31 itojun Exp $	*/
2 /*	$KAME: nd6_rtr.c,v 1.97 2001/02/07 11:09:13 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/time.h>
40 #include <sys/kernel.h>
41 #include <sys/errno.h>
42 #include <sys/ioctl.h>
43 #include <sys/syslog.h>
44 #include <dev/rndvar.h>
45 
46 #include <net/if.h>
47 #include <net/if_types.h>
48 #include <net/if_dl.h>
49 #include <net/route.h>
50 #include <net/radix.h>
51 
52 #include <netinet/in.h>
53 #include <netinet6/in6_var.h>
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet6/nd6.h>
57 #include <netinet/icmp6.h>
58 
59 #define SDL(s)	((struct sockaddr_dl *)s)
60 
61 static int rtpref(struct nd_defrouter *);
62 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
63 static struct in6_ifaddr *in6_ifadd(struct nd_prefix *);
64 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
65 	struct nd_defrouter *);
66 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
67 static void pfxrtr_del(struct nd_pfxrouter *);
68 static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
69 static void defrouter_delreq(struct nd_defrouter *);
70 static void nd6_rtmsg(int, struct rtentry *);
71 
72 static void in6_init_address_ltimes(struct nd_prefix *,
73 	struct in6_addrlifetime *);
74 
75 static int rt6_deleteroute(struct radix_node *, void *);
76 
77 extern int nd6_recalc_reachtm_interval;
78 
79 static struct ifnet *nd6_defifp;
80 int nd6_defifindex;
81 
82 /*
83  * Receive Router Solicitation Message - just for routers.
84  * Router solicitation/advertisement is mostly managed by userland program
85  * (rtadvd) so here we have no function like nd6_ra_output().
86  *
87  * Based on RFC 2461
88  */
89 void
nd6_rs_input(m,off,icmp6len)90 nd6_rs_input(m, off, icmp6len)
91 	struct	mbuf *m;
92 	int off, icmp6len;
93 {
94 	struct ifnet *ifp = m->m_pkthdr.rcvif;
95 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
96 	struct nd_router_solicit *nd_rs;
97 	struct in6_addr saddr6 = ip6->ip6_src;
98 #if 0
99 	struct in6_addr daddr6 = ip6->ip6_dst;
100 #endif
101 	char *lladdr = NULL;
102 	int lladdrlen = 0;
103 #if 0
104 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
105 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
106 	struct rtentry *rt = NULL;
107 	int is_newentry;
108 #endif
109 	union nd_opts ndopts;
110 
111 	/* If I'm not a router, ignore it. */
112 	if (ip6_accept_rtadv != 0 || !ip6_forwarding)
113 		goto freeit;
114 
115 	/* Sanity checks */
116 	if (ip6->ip6_hlim != 255) {
117 		nd6log((LOG_ERR,
118 		    "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
119 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
120 		    ip6_sprintf(&ip6->ip6_dst), ifp->if_xname));
121 		goto bad;
122 	}
123 
124 	/*
125 	 * Don't update the neighbor cache, if src = ::.
126 	 * This indicates that the src has no IP address assigned yet.
127 	 */
128 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
129 		goto freeit;
130 
131 	IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
132 	if (nd_rs == NULL) {
133 		icmp6stat.icp6s_tooshort++;
134 		return;
135 	}
136 
137 	icmp6len -= sizeof(*nd_rs);
138 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
139 	if (nd6_options(&ndopts) < 0) {
140 		nd6log((LOG_INFO,
141 		    "nd6_rs_input: invalid ND option, ignored\n"));
142 		/* nd6_options have incremented stats */
143 		goto freeit;
144 	}
145 
146 	if (ndopts.nd_opts_src_lladdr) {
147 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
148 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
149 	}
150 
151 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
152 		nd6log((LOG_INFO,
153 		    "nd6_rs_input: lladdrlen mismatch for %s "
154 		    "(if %d, RS packet %d)\n",
155 		    ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
156 		goto bad;
157 	}
158 
159 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
160 
161  freeit:
162 	m_freem(m);
163 	return;
164 
165  bad:
166 	icmp6stat.icp6s_badrs++;
167 	m_freem(m);
168 }
169 
170 /*
171  * Receive Router Advertisement Message.
172  *
173  * Based on RFC 2461
174  * TODO: on-link bit on prefix information
175  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
176  */
177 void
nd6_ra_input(m,off,icmp6len)178 nd6_ra_input(m, off, icmp6len)
179 	struct	mbuf *m;
180 	int off, icmp6len;
181 {
182 	struct ifnet *ifp = m->m_pkthdr.rcvif;
183 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
184 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
185 	struct nd_router_advert *nd_ra;
186 	struct in6_addr saddr6 = ip6->ip6_src;
187 #if 0
188 	struct in6_addr daddr6 = ip6->ip6_dst;
189 	int flags; /* = nd_ra->nd_ra_flags_reserved; */
190 	int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
191 	int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
192 #endif
193 	union nd_opts ndopts;
194 	struct nd_defrouter *dr;
195 
196 	/*
197 	 * We only accept RAs only when
198 	 * the system-wide variable allows the acceptance, and
199 	 * per-interface variable allows RAs on the receiving interface.
200 	 */
201 	if (ip6_accept_rtadv == 0)
202 		goto freeit;
203 	if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
204 		goto freeit;
205 
206 	if (ip6->ip6_hlim != 255) {
207 		nd6log((LOG_ERR,
208 		    "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
209 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
210 		    ip6_sprintf(&ip6->ip6_dst), ifp->if_xname));
211 		goto bad;
212 	}
213 
214 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
215 		nd6log((LOG_ERR,
216 		    "nd6_ra_input: src %s is not link-local\n",
217 		    ip6_sprintf(&saddr6)));
218 		goto bad;
219 	}
220 
221 	IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
222 	if (nd_ra == NULL) {
223 		icmp6stat.icp6s_tooshort++;
224 		return;
225 	}
226 
227 	icmp6len -= sizeof(*nd_ra);
228 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
229 	if (nd6_options(&ndopts) < 0) {
230 		nd6log((LOG_INFO,
231 		    "nd6_ra_input: invalid ND option, ignored\n"));
232 		/* nd6_options have incremented stats */
233 		goto freeit;
234 	}
235 
236     {
237 	struct nd_defrouter dr0;
238 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
239 
240 	Bzero(&dr0, sizeof(dr0));
241 	dr0.rtaddr = saddr6;
242 	dr0.flags  = nd_ra->nd_ra_flags_reserved;
243 	dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
244 	dr0.expire = time.tv_sec + dr0.rtlifetime;
245 	dr0.ifp = ifp;
246 	/* unspecified or not? (RFC 2461 6.3.4) */
247 	if (advreachable) {
248 		NTOHL(advreachable);
249 		if (advreachable <= MAX_REACHABLE_TIME &&
250 		    ndi->basereachable != advreachable) {
251 			ndi->basereachable = advreachable;
252 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
253 			ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
254 		}
255 	}
256 	if (nd_ra->nd_ra_retransmit)
257 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
258 	if (nd_ra->nd_ra_curhoplimit)
259 		ndi->chlim = nd_ra->nd_ra_curhoplimit;
260 	dr = defrtrlist_update(&dr0);
261     }
262 
263 	/*
264 	 * prefix
265 	 */
266 	if (ndopts.nd_opts_pi) {
267 		struct nd_opt_hdr *pt;
268 		struct nd_opt_prefix_info *pi = NULL;
269 		struct nd_prefix pr;
270 
271 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
272 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
273 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
274 						(pt->nd_opt_len << 3))) {
275 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
276 				continue;
277 			pi = (struct nd_opt_prefix_info *)pt;
278 
279 			if (pi->nd_opt_pi_len != 4) {
280 				nd6log((LOG_INFO,
281 				    "nd6_ra_input: invalid option "
282 				    "len %d for prefix information option, "
283 				    "ignored\n", pi->nd_opt_pi_len));
284 				continue;
285 			}
286 
287 			if (128 < pi->nd_opt_pi_prefix_len) {
288 				nd6log((LOG_INFO,
289 				    "nd6_ra_input: invalid prefix "
290 				    "len %d for prefix information option, "
291 				    "ignored\n", pi->nd_opt_pi_prefix_len));
292 				continue;
293 			}
294 
295 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
296 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
297 				nd6log((LOG_INFO,
298 				    "nd6_ra_input: invalid prefix "
299 				    "%s, ignored\n",
300 				    ip6_sprintf(&pi->nd_opt_pi_prefix)));
301 				continue;
302 			}
303 
304 			/* aggregatable unicast address, rfc2374 */
305 			if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
306 			 && pi->nd_opt_pi_prefix_len != 64) {
307 				nd6log((LOG_INFO,
308 				    "nd6_ra_input: invalid prefixlen "
309 				    "%d for rfc2374 prefix %s, ignored\n",
310 				    pi->nd_opt_pi_prefix_len,
311 				    ip6_sprintf(&pi->nd_opt_pi_prefix)));
312 				continue;
313 			}
314 
315 			bzero(&pr, sizeof(pr));
316 			pr.ndpr_prefix.sin6_family = AF_INET6;
317 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
318 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
319 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
320 
321 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
322 			     ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
323 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
324 			     ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
325 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
326 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
327 			pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
328 			pr.ndpr_lastupdate = time.tv_sec;
329 
330 			if (in6_init_prefix_ltimes(&pr))
331 				continue; /* prefix lifetime init failed */
332 
333 			(void)prelist_update(&pr, dr, m);
334 		}
335 	}
336 
337 	/*
338 	 * MTU
339 	 */
340 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
341 		u_long mtu;
342 		u_long maxmtu;
343 
344 		mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
345 
346 		/* lower bound */
347 		if (mtu < IPV6_MMTU) {
348 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
349 			    "mtu=%lu sent from %s, ignoring\n",
350 			    mtu, ip6_sprintf(&ip6->ip6_src)));
351 			goto skip;
352 		}
353 
354 		/* upper bound */
355 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
356 		    ? ndi->maxmtu : ifp->if_mtu;
357 		if (mtu <= maxmtu) {
358 			int change = (ndi->linkmtu != mtu);
359 
360 			ndi->linkmtu = mtu;
361 			if (change) /* in6_maxmtu may change */
362 				in6_setmaxmtu();
363 		} else {
364 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
365 			    "mtu=%lu sent from %s; "
366 			    "exceeds maxmtu %lu, ignoring\n",
367 			    mtu, ip6_sprintf(&ip6->ip6_src), maxmtu));
368 		}
369 	}
370 
371  skip:
372 
373 	/*
374 	 * Source link layer address
375 	 */
376     {
377 	char *lladdr = NULL;
378 	int lladdrlen = 0;
379 
380 	if (ndopts.nd_opts_src_lladdr) {
381 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
382 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
383 	}
384 
385 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
386 		nd6log((LOG_INFO,
387 		    "nd6_ra_input: lladdrlen mismatch for %s "
388 		    "(if %d, RA packet %d)\n", ip6_sprintf(&saddr6),
389 		    ifp->if_addrlen, lladdrlen - 2));
390 		goto bad;
391 	}
392 
393 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
394 
395 	/*
396 	 * Installing a link-layer address might change the state of the
397 	 * router's neighbor cache, which might also affect our on-link
398 	 * detection of adveritsed prefixes.
399 	 */
400 	pfxlist_onlink_check();
401     }
402 
403  freeit:
404 	m_freem(m);
405 	return;
406 
407  bad:
408 	icmp6stat.icp6s_badra++;
409 	m_freem(m);
410 }
411 
412 /*
413  * default router list processing sub routines
414  */
415 
416 /* tell the change to user processes watching the routing socket. */
417 static void
nd6_rtmsg(cmd,rt)418 nd6_rtmsg(cmd, rt)
419 	int cmd;
420 	struct rtentry *rt;
421 {
422 	struct rt_addrinfo info;
423 
424 	bzero((caddr_t)&info, sizeof(info));
425 	info.rti_info[RTAX_DST] = rt_key(rt);
426 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
427 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
428 	if (rt->rt_ifp) {
429 		info.rti_info[RTAX_IFP] =
430 		    TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
431 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
432 	}
433 
434 	rt_missmsg(cmd, &info, rt->rt_flags, 0);
435 }
436 
437 void
defrouter_addreq(new)438 defrouter_addreq(new)
439 	struct nd_defrouter *new;
440 {
441 	struct sockaddr_in6 def, mask, gate;
442 	struct rtentry *newrt = NULL;
443 	int s;
444 	int error;
445 
446 	Bzero(&def, sizeof(def));
447 	Bzero(&mask, sizeof(mask));
448 	Bzero(&gate, sizeof(gate)); /* for safety */
449 
450 	def.sin6_len = mask.sin6_len = gate.sin6_len =
451 	    sizeof(struct sockaddr_in6);
452 	def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
453 	gate.sin6_addr = new->rtaddr;
454 #ifndef SCOPEDROUTING
455 	gate.sin6_scope_id = 0;	/* XXX */
456 #endif
457 
458 	s = splsoftnet();
459 	error = rtrequest(RTM_ADD, (struct sockaddr *)&def,
460 	    (struct sockaddr *)&gate, (struct sockaddr *)&mask,
461 	    RTF_GATEWAY, &newrt);
462 	if (newrt) {
463 		nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
464 		newrt->rt_refcnt--;
465 	}
466 	if (error == 0)
467 		new->installed = 1;
468 	splx(s);
469 	return;
470 }
471 
472 struct nd_defrouter *
defrouter_lookup(addr,ifp)473 defrouter_lookup(addr, ifp)
474 	struct in6_addr *addr;
475 	struct ifnet *ifp;
476 {
477 	struct nd_defrouter *dr;
478 
479 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
480 	     dr = TAILQ_NEXT(dr, dr_entry)) {
481 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
482 			return (dr);
483 		}
484 	}
485 
486 	return (NULL);		/* search failed */
487 }
488 
489 void
defrtrlist_del(dr)490 defrtrlist_del(dr)
491 	struct nd_defrouter *dr;
492 {
493 	struct nd_defrouter *deldr = NULL;
494 	struct nd_prefix *pr;
495 
496 	/*
497 	 * Flush all the routing table entries that use the router
498 	 * as a next hop.
499 	 */
500 	if (!ip6_forwarding && ip6_accept_rtadv) /* XXX: better condition? */
501 		rt6_flush(&dr->rtaddr, dr->ifp);
502 
503 	if (dr->installed) {
504 		deldr = dr;
505 		defrouter_delreq(dr);
506 	}
507 	TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
508 
509 	/*
510 	 * Also delete all the pointers to the router in each prefix lists.
511 	 */
512 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
513 		struct nd_pfxrouter *pfxrtr;
514 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
515 			pfxrtr_del(pfxrtr);
516 	}
517 	pfxlist_onlink_check();
518 
519 	/*
520 	 * If the router is the primary one, choose a new one.
521 	 * Note that defrouter_select() will remove the current gateway
522 	 * from the routing table.
523 	 */
524 	if (deldr)
525 		defrouter_select();
526 
527 	free(dr, M_IP6NDP);
528 }
529 
530 /*
531  * Remove the default route for a given router.
532  * This is just a subroutine function for defrouter_select(), and should
533  * not be called from anywhere else.
534  */
535 static void
defrouter_delreq(dr)536 defrouter_delreq(dr)
537 	struct nd_defrouter *dr;
538 {
539 	struct sockaddr_in6 def, mask, gw;
540 	struct rtentry *oldrt = NULL;
541 
542 #ifdef DIAGNOSTIC
543 	if (!dr)
544 		panic("dr == NULL in defrouter_delreq");
545 #endif
546 
547 	Bzero(&def, sizeof(def));
548 	Bzero(&mask, sizeof(mask));
549 	Bzero(&gw, sizeof(gw));	/* for safety */
550 
551 	def.sin6_len = mask.sin6_len = gw.sin6_len =
552 	    sizeof(struct sockaddr_in6);
553 	def.sin6_family = mask.sin6_family = gw.sin6_family = AF_INET6;
554 	gw.sin6_addr = dr->rtaddr;
555 #ifndef SCOPEDROUTING
556 	gw.sin6_scope_id = 0;	/* XXX */
557 #endif
558 
559 	rtrequest(RTM_DELETE, (struct sockaddr *)&def,
560 	    (struct sockaddr *)&gw,
561 	    (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt);
562 	if (oldrt) {
563 		nd6_rtmsg(RTM_DELETE, oldrt);
564 		if (oldrt->rt_refcnt <= 0) {
565 			/*
566 			 * XXX: borrowed from the RTM_DELETE case of
567 			 * rtrequest().
568 			 */
569 			oldrt->rt_refcnt++;
570 			rtfree(oldrt);
571 		}
572 	}
573 
574 	dr->installed = 0;
575 }
576 
577 /*
578  * remove all default routes from default router list
579  */
580 void
defrouter_reset()581 defrouter_reset()
582 {
583 	struct nd_defrouter *dr;
584 
585 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
586 	     dr = TAILQ_NEXT(dr, dr_entry))
587 		defrouter_delreq(dr);
588 
589 	/*
590 	 * XXX should we also nuke any default routers in the kernel, by
591 	 * going through them by rtalloc1()?
592 	 */
593 }
594 
595 /*
596  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
597  * draft-ietf-ipngwg-router-selection:
598  * 1) Routers that are reachable or probably reachable should be preferred.
599  *    If we have more than one (probably) reachable router, prefer ones
600  *    with the highest router preference.
601  * 2) When no routers on the list are known to be reachable or
602  *    probably reachable, routers SHOULD be selected in a round-robin
603  *    fashion, regardless of router preference values.
604  * 3) If the Default Router List is empty, assume that all
605  *    destinations are on-link.
606  *
607  * We assume nd_defrouter is sorted by router preference value.
608  * Since the code below covers both with and without router preference cases,
609  * we do not need to classify the cases by ifdef.
610  *
611  * At this moment, we do not try to install more than one default router,
612  * even when the multipath routing is available, because we're not sure about
613  * the benefits for stub hosts comparing to the risk of making the code
614  * complicated and the possibility of introducing bugs.
615  */
616 void
defrouter_select()617 defrouter_select()
618 {
619 	int s = splsoftnet();
620 	struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
621 	struct rtentry *rt = NULL;
622 	struct llinfo_nd6 *ln = NULL;
623 
624 	/*
625 	 * This function should be called only when acting as an autoconfigured
626 	 * host.  Although the remaining part of this function is not effective
627 	 * if the node is not an autoconfigured host, we explicitly exclude
628 	 * such cases here for safety.
629 	 */
630 	if (ip6_forwarding || !ip6_accept_rtadv) {
631 		nd6log((LOG_WARNING,
632 		    "defrouter_select: called unexpectedly (forwarding=%d, "
633 		    "accept_rtadv=%d)\n", ip6_forwarding, ip6_accept_rtadv));
634 		splx(s);
635 		return;
636 	}
637 
638 	/*
639 	 * Let's handle easy case (3) first:
640 	 * If default router list is empty, there's nothing to be done.
641 	 */
642 	if (!TAILQ_FIRST(&nd_defrouter)) {
643 		splx(s);
644 		return;
645 	}
646 
647 	/*
648 	 * Search for a (probably) reachable router from the list.
649 	 * We just pick up the first reachable one (if any), assuming that
650 	 * the ordering rule of the list described in defrtrlist_update().
651 	 */
652 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
653 	     dr = TAILQ_NEXT(dr, dr_entry)) {
654 		if (!selected_dr &&
655 		    (rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
656 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
657 		    ND6_IS_LLINFO_PROBREACH(ln)) {
658 			selected_dr = dr;
659 		}
660 
661 		if (dr->installed && !installed_dr)
662 			installed_dr = dr;
663 		else if (dr->installed && installed_dr) {
664 			/* this should not happen.  warn for diagnosis. */
665 			log(LOG_ERR, "defrouter_select: more than one router"
666 			    " is installed\n");
667 		}
668 	}
669 	/*
670 	 * If none of the default routers was found to be reachable,
671 	 * round-robin the list regardless of preference.
672 	 * Otherwise, if we have an installed router, check if the selected
673 	 * (reachable) router should really be preferred to the installed one.
674 	 * We only prefer the new router when the old one is not reachable
675 	 * or when the new one has a really higher preference value.
676 	 */
677 	if (!selected_dr) {
678 		if (!installed_dr || !TAILQ_NEXT(installed_dr, dr_entry))
679 			selected_dr = TAILQ_FIRST(&nd_defrouter);
680 		else
681 			selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
682 	} else if (installed_dr &&
683 	    (rt = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
684 	    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
685 	    ND6_IS_LLINFO_PROBREACH(ln) &&
686 	    rtpref(selected_dr) <= rtpref(installed_dr)) {
687 		selected_dr = installed_dr;
688 	}
689 
690 	/*
691 	 * If the selected router is different than the installed one,
692 	 * remove the installed router and install the selected one.
693 	 * Note that the selected router is never NULL here.
694 	 */
695 	if (installed_dr != selected_dr) {
696 		if (installed_dr)
697 			defrouter_delreq(installed_dr);
698 		defrouter_addreq(selected_dr);
699 	}
700 
701 	splx(s);
702 	return;
703 }
704 
705 /*
706  * for default router selection
707  * regards router-preference field as a 2-bit signed integer
708  */
709 static int
rtpref(struct nd_defrouter * dr)710 rtpref(struct nd_defrouter *dr)
711 {
712 #ifdef RTPREF
713 	switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
714 	case ND_RA_FLAG_RTPREF_HIGH:
715 		return RTPREF_HIGH;
716 	case ND_RA_FLAG_RTPREF_MEDIUM:
717 	case ND_RA_FLAG_RTPREF_RSV:
718 		return RTPREF_MEDIUM;
719 	case ND_RA_FLAG_RTPREF_LOW:
720 		return RTPREF_LOW;
721 	default:
722 		/*
723 		 * This case should never happen.  If it did, it would mean a
724 		 * serious bug of kernel internal.  We thus always bark here.
725 		 * Or, can we even panic?
726 		 */
727 		log(LOG_ERR, "rtpref: impossible RA flag %x", dr->flags);
728 		return RTPREF_INVALID;
729 	}
730 	/* NOTREACHED */
731 #else
732 	return 0;
733 #endif
734 }
735 
736 static struct nd_defrouter *
defrtrlist_update(new)737 defrtrlist_update(new)
738 	struct nd_defrouter *new;
739 {
740 	struct nd_defrouter *dr, *n;
741 	int s = splsoftnet();
742 
743 	if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
744 		/* entry exists */
745 		if (new->rtlifetime == 0) {
746 			defrtrlist_del(dr);
747 			dr = NULL;
748 		} else {
749 			int oldpref = rtpref(dr);
750 
751 			/* override */
752 			dr->flags = new->flags; /* xxx flag check */
753 			dr->rtlifetime = new->rtlifetime;
754 			dr->expire = new->expire;
755 
756 			/*
757 			 * If the preference does not change, there's no need
758 			 * to sort the entries.
759 			 */
760 			if (rtpref(new) == oldpref) {
761 				splx(s);
762 				return (dr);
763 			}
764 
765 			/*
766 			 * preferred router may be changed, so relocate
767 			 * this router.
768 			 * XXX: calling TAILQ_REMOVE directly is a bad manner.
769 			 * However, since defrtrlist_del() has many side
770 			 * effects, we intentionally do so here.
771 			 * defrouter_select() below will handle routing
772 			 * changes later.
773 			 */
774 			TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
775 			n = dr;
776 			goto insert;
777 		}
778 		splx(s);
779 		return (dr);
780 	}
781 
782 	/* entry does not exist */
783 	if (new->rtlifetime == 0) {
784 		splx(s);
785 		return (NULL);
786 	}
787 
788 	n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
789 	if (n == NULL) {
790 		splx(s);
791 		return (NULL);
792 	}
793 	bzero(n, sizeof(*n));
794 	*n = *new;
795 
796 insert:
797 	/*
798 	 * Insert the new router in the Default Router List;
799 	 * The Default Router List should be in the descending order
800 	 * of router-preferece.  Routers with the same preference are
801 	 * sorted in the arriving time order.
802 	 */
803 
804 	/* insert at the end of the group */
805 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
806 	     dr = TAILQ_NEXT(dr, dr_entry)) {
807 		if (rtpref(n) > rtpref(dr))
808 			break;
809 	}
810 	if (dr)
811 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
812 	else
813 		TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
814 
815 	defrouter_select();
816 
817 	splx(s);
818 
819 	return (n);
820 }
821 
822 static struct nd_pfxrouter *
pfxrtr_lookup(pr,dr)823 pfxrtr_lookup(pr, dr)
824 	struct nd_prefix *pr;
825 	struct nd_defrouter *dr;
826 {
827 	struct nd_pfxrouter *search;
828 
829 	for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
830 		if (search->router == dr)
831 			break;
832 	}
833 
834 	return (search);
835 }
836 
837 static void
pfxrtr_add(pr,dr)838 pfxrtr_add(pr, dr)
839 	struct nd_prefix *pr;
840 	struct nd_defrouter *dr;
841 {
842 	struct nd_pfxrouter *new;
843 
844 	new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
845 	if (new == NULL)
846 		return;
847 	bzero(new, sizeof(*new));
848 	new->router = dr;
849 
850 	LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
851 
852 	pfxlist_onlink_check();
853 }
854 
855 static void
pfxrtr_del(pfr)856 pfxrtr_del(pfr)
857 	struct nd_pfxrouter *pfr;
858 {
859 	LIST_REMOVE(pfr, pfr_entry);
860 	free(pfr, M_IP6NDP);
861 }
862 
863 struct nd_prefix *
nd6_prefix_lookup(pr)864 nd6_prefix_lookup(pr)
865 	struct nd_prefix *pr;
866 {
867 	struct nd_prefix *search;
868 
869 	for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
870 		if (pr->ndpr_ifp == search->ndpr_ifp &&
871 		    pr->ndpr_plen == search->ndpr_plen &&
872 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
873 		    &search->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
874 			break;
875 		}
876 	}
877 
878 	return (search);
879 }
880 
881 int
nd6_prelist_add(pr,dr,newp)882 nd6_prelist_add(pr, dr, newp)
883 	struct nd_prefix *pr, **newp;
884 	struct nd_defrouter *dr;
885 {
886 	struct nd_prefix *new = NULL;
887 	int i, s;
888 
889 	new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
890 	if (new == NULL)
891 		return ENOMEM;
892 	bzero(new, sizeof(*new));
893 	*new = *pr;
894 	if (newp != NULL)
895 		*newp = new;
896 
897 	/* initilization */
898 	LIST_INIT(&new->ndpr_advrtrs);
899 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
900 	/* make prefix in the canonical form */
901 	for (i = 0; i < 4; i++)
902 		new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
903 		    new->ndpr_mask.s6_addr32[i];
904 
905 	s = splsoftnet();
906 	/* link ndpr_entry to nd_prefix list */
907 	LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
908 	splx(s);
909 
910 	/* ND_OPT_PI_FLAG_ONLINK processing */
911 	if (new->ndpr_raf_onlink) {
912 		int e;
913 
914 		if ((e = nd6_prefix_onlink(new)) != 0) {
915 			nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
916 			    "the prefix %s/%d on-link on %s (errno=%d)\n",
917 			    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
918 			    pr->ndpr_plen, pr->ndpr_ifp->if_xname, e));
919 			/* proceed anyway. XXX: is it correct? */
920 		}
921 	}
922 
923 	if (dr)
924 		pfxrtr_add(new, dr);
925 
926 	return 0;
927 }
928 
929 void
prelist_remove(pr)930 prelist_remove(pr)
931 	struct nd_prefix *pr;
932 {
933 	struct nd_pfxrouter *pfr, *next;
934 	int e, s;
935 
936 	/* make sure to invalidate the prefix until it is really freed. */
937 	pr->ndpr_vltime = 0;
938 	pr->ndpr_pltime = 0;
939 #if 0
940 	/*
941 	 * Though these flags are now meaningless, we'd rather keep the value
942 	 * not to confuse users when executing "ndp -p".
943 	 */
944 	pr->ndpr_raf_onlink = 0;
945 	pr->ndpr_raf_auto = 0;
946 #endif
947 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
948 	    (e = nd6_prefix_offlink(pr)) != 0) {
949 		nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
950 		    "on %s, errno=%d\n",
951 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
952 		    pr->ndpr_plen, pr->ndpr_ifp->if_xname, e));
953 		/* what should we do? */
954 	}
955 
956 	if (pr->ndpr_refcnt > 0)
957 		return;		/* notice here? */
958 
959 	s = splsoftnet();
960 
961 	/* unlink ndpr_entry from nd_prefix list */
962 	LIST_REMOVE(pr, ndpr_entry);
963 
964 	/* free list of routers that adversed the prefix */
965 	for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
966 		next = pfr->pfr_next;
967 
968 		free(pfr, M_IP6NDP);
969 	}
970 	splx(s);
971 
972 	free(pr, M_IP6NDP);
973 
974 	pfxlist_onlink_check();
975 }
976 
977 int
prelist_update(new,dr,m)978 prelist_update(new, dr, m)
979 	struct nd_prefix *new;
980 	struct nd_defrouter *dr; /* may be NULL */
981 	struct mbuf *m;
982 {
983 	struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
984 	struct ifaddr *ifa;
985 	struct ifnet *ifp = new->ndpr_ifp;
986 	struct nd_prefix *pr;
987 	int s = splsoftnet();
988 	int error = 0;
989 	int newprefix = 0;
990 	int auth;
991 	struct in6_addrlifetime lt6_tmp;
992 
993 	auth = 0;
994 	if (m) {
995 		/*
996 		 * Authenticity for NA consists authentication for
997 		 * both IP header and IP datagrams, doesn't it ?
998 		 */
999 		auth = ((m->m_flags & M_AUTH_AH) && (m->m_flags & M_AUTH));
1000 	}
1001 
1002 	if ((pr = nd6_prefix_lookup(new)) != NULL) {
1003 		/*
1004 		 * nd6_prefix_lookup() ensures that pr and new have the same
1005 		 * prefix on a same interface.
1006 		 */
1007 
1008 		/*
1009 		 * Update prefix information.  Note that the on-link (L) bit
1010 		 * and the autonomous (A) bit should NOT be changed from 1
1011 		 * to 0.
1012 		 */
1013 		if (new->ndpr_raf_onlink == 1)
1014 			pr->ndpr_raf_onlink = 1;
1015 		if (new->ndpr_raf_auto == 1)
1016 			pr->ndpr_raf_auto = 1;
1017 		if (new->ndpr_raf_onlink) {
1018 			pr->ndpr_vltime = new->ndpr_vltime;
1019 			pr->ndpr_pltime = new->ndpr_pltime;
1020 			pr->ndpr_preferred = new->ndpr_preferred;
1021 			pr->ndpr_expire = new->ndpr_expire;
1022 			pr->ndpr_lastupdate = new->ndpr_lastupdate;
1023 		}
1024 
1025 		if (new->ndpr_raf_onlink &&
1026 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1027 			int e;
1028 
1029 			if ((e = nd6_prefix_onlink(pr)) != 0) {
1030 				nd6log((LOG_ERR,
1031 				    "prelist_update: failed to make "
1032 				    "the prefix %s/%d on-link on %s "
1033 				    "(errno=%d)\n",
1034 				    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1035 				    pr->ndpr_plen, pr->ndpr_ifp->if_xname, e));
1036 				/* proceed anyway. XXX: is it correct? */
1037 			}
1038 		}
1039 
1040 		if (dr && pfxrtr_lookup(pr, dr) == NULL)
1041 			pfxrtr_add(pr, dr);
1042 	} else {
1043 		struct nd_prefix *newpr = NULL;
1044 
1045 		newprefix = 1;
1046 
1047 		if (new->ndpr_vltime == 0)
1048 			goto end;
1049 		if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1050 			goto end;
1051 
1052 		error = nd6_prelist_add(new, dr, &newpr);
1053 		if (error != 0 || newpr == NULL) {
1054 			nd6log((LOG_NOTICE, "prelist_update: "
1055 			    "nd6_prelist_add failed for %s/%d on %s "
1056 			    "errno=%d, returnpr=%p\n",
1057 			    ip6_sprintf(&new->ndpr_prefix.sin6_addr),
1058 			    new->ndpr_plen, new->ndpr_ifp->if_xname,
1059 			    error, newpr));
1060 			goto end; /* we should just give up in this case. */
1061 		}
1062 
1063 		/*
1064 		 * XXX: from the ND point of view, we can ignore a prefix
1065 		 * with the on-link bit being zero.  However, we need a
1066 		 * prefix structure for references from autoconfigured
1067 		 * addresses.  Thus, we explicitly make sure that the prefix
1068 		 * itself expires now.
1069 		 */
1070 		if (newpr->ndpr_raf_onlink == 0) {
1071 			newpr->ndpr_vltime = 0;
1072 			newpr->ndpr_pltime = 0;
1073 			in6_init_prefix_ltimes(newpr);
1074 		}
1075 
1076 		pr = newpr;
1077 	}
1078 
1079 	/*
1080 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1081 	 * Note that pr must be non NULL at this point.
1082 	 */
1083 
1084 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
1085 	if (!new->ndpr_raf_auto)
1086 		goto end;
1087 
1088 	/*
1089 	 * 5.5.3 (b). the link-local prefix should have been ignored in
1090 	 * nd6_ra_input.
1091 	 */
1092 
1093 	/*
1094 	 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime.
1095 	 * This should have been done in nd6_ra_input.
1096 	 */
1097 
1098 	/*
1099 	 * 5.5.3 (d). If the prefix advertised does not match the prefix of an
1100 	 * address already in the list, and the Valid Lifetime is not 0,
1101 	 * form an address.  Note that even a manually configured address
1102 	 * should reject autoconfiguration of a new address.
1103 	 */
1104 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1105 	{
1106 		struct in6_ifaddr *ifa6;
1107 		int ifa_plen;
1108 		u_int32_t storedlifetime;
1109 
1110 		if (ifa->ifa_addr->sa_family != AF_INET6)
1111 			continue;
1112 
1113 		ifa6 = (struct in6_ifaddr *)ifa;
1114 
1115 		/*
1116 		 * Spec is not clear here, but I believe we should concentrate
1117 		 * on unicast (i.e. not anycast) addresses.
1118 		 * XXX: other ia6_flags? detached or duplicated?
1119 		 */
1120 		if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1121 			continue;
1122 
1123 		ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL);
1124 		if (ifa_plen != new->ndpr_plen ||
1125 		    !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr,
1126 		    &new->ndpr_prefix.sin6_addr, ifa_plen))
1127 			continue;
1128 
1129 		if (ia6_match == NULL) /* remember the first one */
1130 			ia6_match = ifa6;
1131 
1132 		if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1133 			continue;
1134 
1135 		/*
1136 		 * An already autoconfigured address matched.  Now that we
1137 		 * are sure there is at least one matched address, we can
1138 		 * proceed to 5.5.3. (e): update the lifetimes according to the
1139 		 * "two hours" rule and the privacy extension.
1140 		 */
1141 #define TWOHOUR		(120*60)
1142 		/*
1143 		 * RFC2462 introduces the notion of StoredLifetime to the
1144 		 * "two hours" rule as follows:
1145 		 *   the Lifetime associated with the previously autoconfigured
1146 		 *   address.
1147 		 * Our interpretation of this definition is "the remaining
1148 		 * lifetime to expiration at the evaluation time".  One might
1149 		 * be wondering if this interpretation is really conform to the
1150 		 * RFC, because the text can read that "Lifetimes" are never
1151 		 * decreased, and our definition of the "storedlifetime" below
1152 		 * essentially reduces the "Valid Lifetime" advertised in the
1153 		 * previous RA.  But, this is due to the wording of the text,
1154 		 * and our interpretation is the same as an author's intention.
1155 		 * See the discussion in the IETF ipngwg ML in August 2001,
1156 		 * with the Subject "StoredLifetime in RFC 2462".
1157 		 */
1158 		lt6_tmp = ifa6->ia6_lifetime;
1159 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1160 			storedlifetime = ND6_INFINITE_LIFETIME;
1161 		else if (time.tv_sec - ifa6->ia6_updatetime >
1162 			 lt6_tmp.ia6t_vltime) {
1163 			/*
1164 			 * The case of "invalid" address.  We should usually
1165 			 * not see this case.
1166 			 */
1167 			storedlifetime = 0;
1168 		} else
1169 			storedlifetime = lt6_tmp.ia6t_vltime -
1170 				(time.tv_sec - ifa6->ia6_updatetime);
1171 		if (TWOHOUR < new->ndpr_vltime ||
1172 		    storedlifetime < new->ndpr_vltime) {
1173 			lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1174 		} else if (storedlifetime <= TWOHOUR
1175 #if 0
1176 			   /*
1177 			    * This condition is logically redundant, so we just
1178 			    * omit it.
1179 			    * See IPng 6712, 6717, and 6721.
1180 			    */
1181 			   && new->ndpr_vltime <= storedlifetime
1182 #endif
1183 			) {
1184 			if (auth) {
1185 				lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1186 			}
1187 		} else {
1188 			/*
1189 			 * new->ndpr_vltime <= TWOHOUR &&
1190 			 * TWOHOUR < storedlifetime
1191 			 */
1192 			lt6_tmp.ia6t_vltime = TWOHOUR;
1193 		}
1194 
1195 		/* The 2 hour rule is not imposed for preferred lifetime. */
1196 		lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1197 
1198 		in6_init_address_ltimes(pr, &lt6_tmp);
1199 
1200 		ifa6->ia6_lifetime = lt6_tmp;
1201 		ifa6->ia6_updatetime = time.tv_sec;
1202 	}
1203 	if (ia6_match == NULL && new->ndpr_vltime) {
1204 		/*
1205 		 * No address matched and the valid lifetime is non-zero.
1206 		 * Create a new address.
1207 		 */
1208 		if ((ia6 = in6_ifadd(new)) != NULL) {
1209 			/*
1210 			 * note that we should use pr (not new) for reference.
1211 			 */
1212 			pr->ndpr_refcnt++;
1213 			ia6->ia6_ndpr = pr;
1214 
1215 			/*
1216 			 * A newly added address might affect the status
1217 			 * of other addresses, so we check and update it.
1218 			 * XXX: what if address duplication happens?
1219 			 */
1220 			pfxlist_onlink_check();
1221 		} else {
1222 			/* just set an error. do not bark here. */
1223 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
1224 		}
1225 	}
1226 
1227  end:
1228 	splx(s);
1229 	return error;
1230 }
1231 
1232 /*
1233  * A supplement function used in the on-link detection below;
1234  * detect if a given prefix has a (probably) reachable advertising router.
1235  * XXX: lengthy function name...
1236  */
1237 static struct nd_pfxrouter *
find_pfxlist_reachable_router(pr)1238 find_pfxlist_reachable_router(pr)
1239 	struct nd_prefix *pr;
1240 {
1241 	struct nd_pfxrouter *pfxrtr;
1242 	struct rtentry *rt;
1243 	struct llinfo_nd6 *ln;
1244 
1245 	for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1246 	     pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1247 		if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1248 		    pfxrtr->router->ifp)) &&
1249 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1250 		    ND6_IS_LLINFO_PROBREACH(ln))
1251 			break;	/* found */
1252 	}
1253 
1254 	return (pfxrtr);
1255 }
1256 
1257 /*
1258  * Check if each prefix in the prefix list has at least one available router
1259  * that advertised the prefix (a router is "available" if its neighbor cache
1260  * entry is reachable or probably reachable).
1261  * If the check fails, the prefix may be off-link, because, for example,
1262  * we have moved from the network but the lifetime of the prefix has not
1263  * expired yet.  So we should not use the prefix if there is another prefix
1264  * that has an available router.
1265  * But, if there is no prefix that has an available router, we still regards
1266  * all the prefixes as on-link.  This is because we can't tell if all the
1267  * routers are simply dead or if we really moved from the network and there
1268  * is no router around us.
1269  */
1270 void
pfxlist_onlink_check()1271 pfxlist_onlink_check()
1272 {
1273 	struct nd_prefix *pr;
1274 	struct in6_ifaddr *ifa;
1275 
1276 	/*
1277 	 * Check if there is a prefix that has a reachable advertising
1278 	 * router.
1279 	 */
1280 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1281 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1282 			break;
1283 	}
1284 	if (pr != NULL || TAILQ_FIRST(&nd_defrouter) != NULL) {
1285 		/*
1286 		 * There is at least one prefix that has a reachable router,
1287 		 * or at least a router which probably does not advertise
1288 		 * any prefixes.  The latter would be the case when we move
1289 		 * to a new link where we have a router that does not provide
1290 		 * prefixes and we configure an address by hand.
1291 		 * Detach prefixes which have no reachable advertising
1292 		 * router, and attach other prefixes.
1293 		 */
1294 		for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1295 			/* XXX: a link-local prefix should never be detached */
1296 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1297 				continue;
1298 
1299 			/*
1300 			 * we aren't interested in prefixes without the L bit
1301 			 * set.
1302 			 */
1303 			if (pr->ndpr_raf_onlink == 0)
1304 				continue;
1305 
1306 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1307 			    find_pfxlist_reachable_router(pr) == NULL)
1308 				pr->ndpr_stateflags |= NDPRF_DETACHED;
1309 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1310 			    find_pfxlist_reachable_router(pr) != 0)
1311 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1312 		}
1313 	} else {
1314 		/* there is no prefix that has a reachable router */
1315 		for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1316 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1317 				continue;
1318 
1319 			if (pr->ndpr_raf_onlink == 0)
1320 				continue;
1321 
1322 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1323 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1324 		}
1325 	}
1326 
1327 	/*
1328 	 * Remove each interface route associated with a (just) detached
1329 	 * prefix, and reinstall the interface route for a (just) attached
1330 	 * prefix.  Note that all attempt of reinstallation does not
1331 	 * necessarily success, when a same prefix is shared among multiple
1332 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
1333 	 * so we don't have to care about them.
1334 	 */
1335 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1336 		int e;
1337 
1338 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1339 			continue;
1340 
1341 		if (pr->ndpr_raf_onlink == 0)
1342 			continue;
1343 
1344 		if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1345 		    (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1346 			if ((e = nd6_prefix_offlink(pr)) != 0) {
1347 				nd6log((LOG_ERR,
1348 				    "pfxlist_onlink_check: failed to "
1349 				    "make %s/%d offlink, errno=%d\n",
1350 				    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1351 				    pr->ndpr_plen, e));
1352 			}
1353 		}
1354 		if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1355 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1356 		    pr->ndpr_raf_onlink) {
1357 			if ((e = nd6_prefix_onlink(pr)) != 0) {
1358 				nd6log((LOG_ERR,
1359 				    "pfxlist_onlink_check: failed to "
1360 				    "make %s/%d offlink, errno=%d\n",
1361 				    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1362 				    pr->ndpr_plen, e));
1363 			}
1364 		}
1365 	}
1366 
1367 	/*
1368 	 * Changes on the prefix status might affect address status as well.
1369 	 * Make sure that all addresses derived from an attached prefix are
1370 	 * attached, and that all addresses derived from a detached prefix are
1371 	 * detached.  Note, however, that a manually configured address should
1372 	 * always be attached.
1373 	 * The precise detection logic is same as the one for prefixes.
1374 	 */
1375 	for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1376 		if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1377 			continue;
1378 
1379 		if (ifa->ia6_ndpr == NULL) {
1380 			/*
1381 			 * This can happen when we first configure the address
1382 			 * (i.e. the address exists, but the prefix does not).
1383 			 * XXX: complicated relationships...
1384 			 */
1385 			continue;
1386 		}
1387 
1388 		if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1389 			break;
1390 	}
1391 	if (ifa) {
1392 		for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1393 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1394 				continue;
1395 
1396 			if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1397 				continue;
1398 
1399 			if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1400 				ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1401 			else
1402 				ifa->ia6_flags |= IN6_IFF_DETACHED;
1403 		}
1404 	}
1405 	else {
1406 		for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1407 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1408 				continue;
1409 
1410 			ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1411 		}
1412 	}
1413 }
1414 
1415 int
nd6_prefix_onlink(pr)1416 nd6_prefix_onlink(pr)
1417 	struct nd_prefix *pr;
1418 {
1419 	struct ifaddr *ifa;
1420 	struct ifnet *ifp = pr->ndpr_ifp;
1421 	struct sockaddr_in6 mask6;
1422 	struct nd_prefix *opr;
1423 	u_long rtflags;
1424 	int error = 0;
1425 	struct rtentry *rt = NULL;
1426 
1427 	/* sanity check */
1428 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1429 		nd6log((LOG_ERR,
1430 		    "nd6_prefix_onlink: %s/%d is already on-link\n",
1431 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1432 		return (EEXIST);
1433 	}
1434 
1435 	/*
1436 	 * Add the interface route associated with the prefix.  Before
1437 	 * installing the route, check if there's the same prefix on another
1438 	 * interface, and the prefix has already installed the interface route.
1439 	 * Although such a configuration is expected to be rare, we explicitly
1440 	 * allow it.
1441 	 */
1442 	for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1443 		if (opr == pr)
1444 			continue;
1445 
1446 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1447 			continue;
1448 
1449 		if (opr->ndpr_plen == pr->ndpr_plen &&
1450 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1451 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1452 			return (0);
1453 	}
1454 
1455 	/*
1456 	 * We prefer link-local addresses as the associated interface address.
1457 	 */
1458 	/* search for a link-local addr */
1459 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1460 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1461 	if (ifa == NULL) {
1462 		/* XXX: freebsd does not have ifa_ifwithaf */
1463 		for (ifa = ifp->if_addrlist.tqh_first;
1464 		     ifa;
1465 		     ifa = ifa->ifa_list.tqe_next)
1466 		{
1467 			if (ifa->ifa_addr->sa_family == AF_INET6)
1468 				break;
1469 		}
1470 		/* should we care about ia6_flags? */
1471 	}
1472 	if (ifa == NULL) {
1473 		/*
1474 		 * This can still happen, when, for example, we receive an RA
1475 		 * containing a prefix with the L bit set and the A bit clear,
1476 		 * after removing all IPv6 addresses on the receiving
1477 		 * interface.  This should, of course, be rare though.
1478 		 */
1479 		nd6log((LOG_NOTICE,
1480 		    "nd6_prefix_onlink: failed to find any ifaddr"
1481 		    " to add route for a prefix(%s/%d) on %s\n",
1482 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1483 		    pr->ndpr_plen, ifp->if_xname));
1484 		return (0);
1485 	}
1486 
1487 	/*
1488 	 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1489 	 * ifa->ifa_rtrequest = nd6_rtrequest;
1490 	 */
1491 	bzero(&mask6, sizeof(mask6));
1492 	mask6.sin6_len = sizeof(mask6);
1493 	mask6.sin6_addr = pr->ndpr_mask;
1494 	/* rtrequest() will probably set RTF_UP, but we're not sure. */
1495 	rtflags = ifa->ifa_flags | RTF_UP;
1496 	if (nd6_need_cache(ifp)) {
1497 		/* explicitly set in case ifa_flags does not set the flag. */
1498 		rtflags |= RTF_CLONING;
1499 	} else {
1500 		/*
1501 		 * explicitly clear the cloning bit in case ifa_flags sets it.
1502 		 */
1503 		rtflags &= ~RTF_CLONING;
1504 	}
1505 	error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1506 	    ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags, &rt);
1507 	if (error == 0) {
1508 		if (rt != NULL) /* this should be non NULL, though */
1509 			nd6_rtmsg(RTM_ADD, rt);
1510 		pr->ndpr_stateflags |= NDPRF_ONLINK;
1511 	} else {
1512 		nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
1513 		    " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1514 		    "errno = %d\n",
1515 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1516 		    pr->ndpr_plen, ifp->if_xname,
1517 		    ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1518 		    ip6_sprintf(&mask6.sin6_addr), rtflags, error));
1519 	}
1520 
1521 	if (rt != NULL)
1522 		rt->rt_refcnt--;
1523 
1524 	return (error);
1525 }
1526 
1527 int
nd6_prefix_offlink(pr)1528 nd6_prefix_offlink(pr)
1529 	struct nd_prefix *pr;
1530 {
1531 	int error = 0;
1532 	struct ifnet *ifp = pr->ndpr_ifp;
1533 	struct nd_prefix *opr;
1534 	struct sockaddr_in6 sa6, mask6;
1535 	struct rtentry *rt = NULL;
1536 
1537 	/* sanity check */
1538 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1539 		nd6log((LOG_ERR,
1540 		    "nd6_prefix_offlink: %s/%d is already off-link\n",
1541 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1542 		return (EEXIST);
1543 	}
1544 
1545 	bzero(&sa6, sizeof(sa6));
1546 	sa6.sin6_family = AF_INET6;
1547 	sa6.sin6_len = sizeof(sa6);
1548 	bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1549 	    sizeof(struct in6_addr));
1550 	bzero(&mask6, sizeof(mask6));
1551 	mask6.sin6_family = AF_INET6;
1552 	mask6.sin6_len = sizeof(sa6);
1553 	bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1554 	error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1555 	    (struct sockaddr *)&mask6, 0, &rt);
1556 	if (error == 0) {
1557 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1558 
1559 		/* report the route deletion to the routing socket. */
1560 		if (rt != NULL)
1561 			nd6_rtmsg(RTM_DELETE, rt);
1562 
1563 		/*
1564 		 * There might be the same prefix on another interface,
1565 		 * the prefix which could not be on-link just because we have
1566 		 * the interface route (see comments in nd6_prefix_onlink).
1567 		 * If there's one, try to make the prefix on-link on the
1568 		 * interface.
1569 		 */
1570 		for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1571 			if (opr == pr)
1572 				continue;
1573 
1574 			if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1575 				continue;
1576 
1577 			/*
1578 			 * KAME specific: detached prefixes should not be
1579 			 * on-link.
1580 			 */
1581 			if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1582 				continue;
1583 
1584 			if (opr->ndpr_plen == pr->ndpr_plen &&
1585 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1586 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1587 				int e;
1588 
1589 				if ((e = nd6_prefix_onlink(opr)) != 0) {
1590 					nd6log((LOG_ERR,
1591 					    "nd6_prefix_offlink: failed to "
1592 					    "recover a prefix %s/%d from %s "
1593 					    "to %s (errno = %d)\n",
1594 					    ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
1595 					    opr->ndpr_plen, ifp->if_xname,
1596 					    opr->ndpr_ifp->if_xname, e));
1597 				}
1598 			}
1599 		}
1600 	} else {
1601 		/* XXX: can we still set the NDPRF_ONLINK flag? */
1602 		nd6log((LOG_ERR,
1603 		    "nd6_prefix_offlink: failed to delete route: "
1604 		    "%s/%d on %s (errno = %d)\n",
1605 		    ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, ifp->if_xname,
1606 		    error));
1607 	}
1608 
1609 	if (rt != NULL) {
1610 		if (rt->rt_refcnt <= 0) {
1611 			/* XXX: we should free the entry ourselves. */
1612 			rt->rt_refcnt++;
1613 			rtfree(rt);
1614 		}
1615 	}
1616 
1617 	return (error);
1618 }
1619 
1620 static struct in6_ifaddr *
in6_ifadd(pr)1621 in6_ifadd(pr)
1622 	struct nd_prefix *pr;
1623 {
1624 	struct ifnet *ifp = pr->ndpr_ifp;
1625 	struct ifaddr *ifa;
1626 	struct in6_aliasreq ifra;
1627 	struct in6_ifaddr *ia, *ib;
1628 	int error, plen0;
1629 	struct in6_addr mask;
1630 	int prefixlen = pr->ndpr_plen;
1631 
1632 	in6_prefixlen2mask(&mask, prefixlen);
1633 
1634 	/*
1635 	 * find a link-local address (will be interface ID).
1636 	 * Is it really mandatory? Theoretically, a global or a site-local
1637 	 * address can be configured without a link-local address, if we
1638 	 * have a unique interface identifier...
1639 	 *
1640 	 * it is not mandatory to have a link-local address, we can generate
1641 	 * interface identifier on the fly.  we do this because:
1642 	 * (1) it should be the easiest way to find interface identifier.
1643 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1644 	 * for multiple addresses on a single interface, and possible shortcut
1645 	 * of DAD.  we omitted DAD for this reason in the past.
1646 	 * (3) a user can prevent autoconfiguration of global address
1647 	 * by removing link-local address by hand (this is partly because we
1648 	 * don't have other way to control the use of IPv6 on a interface.
1649 	 * this has been our design choice - cf. NRL's "ifconfig auto").
1650 	 * (4) it is easier to manage when an interface has addresses
1651 	 * with the same interface identifier, than to have multiple addresses
1652 	 * with different interface identifiers.
1653 	 */
1654 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1655 	if (ifa)
1656 		ib = (struct in6_ifaddr *)ifa;
1657 	else
1658 		return NULL;
1659 
1660 #if 0 /* don't care link local addr state, and always do DAD */
1661 	/* if link-local address is not eligible, do not autoconfigure. */
1662 	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1663 		printf("in6_ifadd: link-local address not ready\n");
1664 		return NULL;
1665 	}
1666 #endif
1667 
1668 	/* prefixlen + ifidlen must be equal to 128 */
1669 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1670 	if (prefixlen != plen0) {
1671 		nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1672 		    "(prefix=%d ifid=%d)\n",
1673 		    ifp->if_xname, prefixlen, 128 - plen0));
1674 		return NULL;
1675 	}
1676 
1677 	/* make ifaddr */
1678 
1679 	bzero(&ifra, sizeof(ifra));
1680 	/*
1681 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
1682 	 * for safety.
1683 	 */
1684 	strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name));
1685 	ifra.ifra_addr.sin6_family = AF_INET6;
1686 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1687 	/* prefix */
1688 	bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr,
1689 	    sizeof(ifra.ifra_addr.sin6_addr));
1690 	ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1691 	ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1692 	ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1693 	ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1694 
1695 	/* interface ID */
1696 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1697 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1698 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1699 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1700 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1701 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1702 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1703 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1704 
1705 	/* new prefix mask. */
1706 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1707 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
1708 	bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1709 	    sizeof(ifra.ifra_prefixmask.sin6_addr));
1710 
1711 	/*
1712 	 * lifetime.
1713 	 * XXX: in6_init_address_ltimes would override these values later.
1714 	 * We should reconsider this logic.
1715 	 */
1716 	ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1717 	ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1718 
1719 	/* XXX: scope zone ID? */
1720 
1721 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1722 
1723 	/* allocate ifaddr structure, link into chain, etc. */
1724 	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
1725 		nd6log((LOG_ERR,
1726 		    "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1727 		    ip6_sprintf(&ifra.ifra_addr.sin6_addr), ifp->if_xname,
1728 		    error));
1729 		return (NULL);	/* ifaddr must not have been allocated. */
1730 	}
1731 
1732 	ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1733 
1734 	return (ia);		/* this is always non-NULL */
1735 }
1736 
1737 int
in6_init_prefix_ltimes(struct nd_prefix * ndpr)1738 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1739 {
1740 
1741 	/* check if preferred lifetime > valid lifetime.  RFC2462 5.5.3 (c) */
1742 	if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1743 		nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1744 		    "(%d) is greater than valid lifetime(%d)\n",
1745 		    (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
1746 		return (EINVAL);
1747 	}
1748 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1749 		ndpr->ndpr_preferred = 0;
1750 	else
1751 		ndpr->ndpr_preferred = time.tv_sec + ndpr->ndpr_pltime;
1752 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1753 		ndpr->ndpr_expire = 0;
1754 	else
1755 		ndpr->ndpr_expire = time.tv_sec + ndpr->ndpr_vltime;
1756 
1757 	return 0;
1758 }
1759 
1760 static void
in6_init_address_ltimes(struct nd_prefix * new,struct in6_addrlifetime * lt6)1761 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1762 {
1763 
1764 	/* Valid lifetime must not be updated unless explicitly specified. */
1765 	/* init ia6t_expire */
1766 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1767 		lt6->ia6t_expire = 0;
1768 	else {
1769 		lt6->ia6t_expire = time.tv_sec;
1770 		lt6->ia6t_expire += lt6->ia6t_vltime;
1771 	}
1772 
1773 	/* init ia6t_preferred */
1774 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1775 		lt6->ia6t_preferred = 0;
1776 	else {
1777 		lt6->ia6t_preferred = time.tv_sec;
1778 		lt6->ia6t_preferred += lt6->ia6t_pltime;
1779 	}
1780 }
1781 
1782 /*
1783  * Delete all the routing table entries that use the specified gateway.
1784  * XXX: this function causes search through all entries of routing table, so
1785  * it shouldn't be called when acting as a router.
1786  */
1787 void
rt6_flush(gateway,ifp)1788 rt6_flush(gateway, ifp)
1789     struct in6_addr *gateway;
1790     struct ifnet *ifp;
1791 {
1792 	struct radix_node_head *rnh = rt_tables[AF_INET6];
1793 	int s = splsoftnet();
1794 
1795 	/* We'll care only link-local addresses */
1796 	if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1797 		splx(s);
1798 		return;
1799 	}
1800 	/* XXX: hack for KAME's link-local address kludge */
1801 	gateway->s6_addr16[1] = htons(ifp->if_index);
1802 
1803 	rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1804 	splx(s);
1805 }
1806 
1807 static int
rt6_deleteroute(rn,arg)1808 rt6_deleteroute(rn, arg)
1809 	struct radix_node *rn;
1810 	void *arg;
1811 {
1812 #define SIN6(s)	((struct sockaddr_in6 *)s)
1813 	struct rtentry *rt = (struct rtentry *)rn;
1814 	struct in6_addr *gate = (struct in6_addr *)arg;
1815 
1816 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1817 		return (0);
1818 
1819 	if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1820 		return (0);
1821 
1822 	/*
1823 	 * Do not delete a static route.
1824 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
1825 	 * 'cloned' bit instead?
1826 	 */
1827 	if ((rt->rt_flags & RTF_STATIC) != 0)
1828 		return (0);
1829 
1830 	/*
1831 	 * We delete only host route. This means, in particular, we don't
1832 	 * delete default route.
1833 	 */
1834 	if ((rt->rt_flags & RTF_HOST) == 0)
1835 		return (0);
1836 
1837 	return (rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1838 	    rt_mask(rt), rt->rt_flags, 0));
1839 #undef SIN6
1840 }
1841 
1842 int
nd6_setdefaultiface(ifindex)1843 nd6_setdefaultiface(ifindex)
1844 	int ifindex;
1845 {
1846 	int error = 0;
1847 
1848 	if (ifindex < 0 || if_indexlim <= ifindex)
1849 		return (EINVAL);
1850 	if (ifindex != 0 && !ifindex2ifnet[ifindex])
1851 		return (EINVAL);
1852 
1853 	if (nd6_defifindex != ifindex) {
1854 		nd6_defifindex = ifindex;
1855 		if (nd6_defifindex > 0) {
1856 			nd6_defifp = ifindex2ifnet[nd6_defifindex];
1857 		} else
1858 			nd6_defifp = NULL;
1859 	}
1860 
1861 	return (error);
1862 }
1863