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