]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/nd6_rtr.c
MFC r365015:
[FreeBSD/FreeBSD.git] / sys / netinet6 / nd6_rtr.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/refcount.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/errno.h>
51 #include <sys/rmlock.h>
52 #include <sys/rwlock.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <sys/queue.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_types.h>
60 #include <net/if_dl.h>
61 #include <net/route.h>
62 #include <net/route_var.h>
63 #include <net/radix.h>
64 #include <net/vnet.h>
65
66 #include <netinet/in.h>
67 #include <net/if_llatbl.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/in6_ifattach.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/nd6.h>
73 #include <netinet/icmp6.h>
74 #include <netinet6/scope6_var.h>
75
76 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
77 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
78     struct mbuf *, int);
79
80 VNET_DEFINE_STATIC(struct nd_drhead, nd6_defrouter);
81 #define V_nd6_defrouter                 VNET(nd6_defrouter)
82
83 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
84 #define V_nd6_recalc_reachtm_interval   VNET(nd6_recalc_reachtm_interval)
85
86 VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
87 VNET_DEFINE(int, nd6_defifindex);
88 #define V_nd6_defifp                    VNET(nd6_defifp)
89
90 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
91
92 VNET_DEFINE(int, ip6_desync_factor);
93 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
94 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
95
96 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
97
98 SYSCTL_DECL(_net_inet6_icmp6);
99
100 /* RTPREF_MEDIUM has to be 0! */
101 #define RTPREF_HIGH     1
102 #define RTPREF_MEDIUM   0
103 #define RTPREF_LOW      (-1)
104 #define RTPREF_RESERVED (-2)
105 #define RTPREF_INVALID  (-3)    /* internal */
106
107 void
108 defrouter_ref(struct nd_defrouter *dr)
109 {
110
111         refcount_acquire(&dr->refcnt);
112 }
113
114 void
115 defrouter_rele(struct nd_defrouter *dr)
116 {
117
118         if (refcount_release(&dr->refcnt))
119                 free(dr, M_IP6NDP);
120 }
121
122 /*
123  * Remove a router from the global list and optionally stash it in a
124  * caller-supplied queue.
125  */
126 void
127 defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq)
128 {
129
130         ND6_WLOCK_ASSERT();
131
132         TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
133         V_nd6_list_genid++;
134         if (drq != NULL)
135                 TAILQ_INSERT_TAIL(drq, dr, dr_entry);
136 }
137
138 /*
139  * Receive Router Solicitation Message - just for routers.
140  * Router solicitation/advertisement is mostly managed by userland program
141  * (rtadvd) so here we have no function like nd6_ra_output().
142  *
143  * Based on RFC 2461
144  */
145 void
146 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
147 {
148         struct ifnet *ifp;
149         struct ip6_hdr *ip6;
150         struct nd_router_solicit *nd_rs;
151         struct in6_addr saddr6;
152         union nd_opts ndopts;
153         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
154         char *lladdr;
155         int lladdrlen;
156
157         ifp = m->m_pkthdr.rcvif;
158
159         /*
160          * Accept RS only when V_ip6_forwarding=1 and the interface has
161          * no ND6_IFF_ACCEPT_RTADV.
162          */
163         if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
164                 goto freeit;
165
166         /* RFC 6980: Nodes MUST silently ignore fragments */   
167         if(m->m_flags & M_FRAGMENTED)
168                 goto freeit;
169
170         /* Sanity checks */
171         ip6 = mtod(m, struct ip6_hdr *);
172         if (ip6->ip6_hlim != 255) {
173                 nd6log((LOG_ERR,
174                     "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
175                     ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
176                     ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
177                 goto bad;
178         }
179
180         /*
181          * Don't update the neighbor cache, if src = ::.
182          * This indicates that the src has no IP address assigned yet.
183          */
184         saddr6 = ip6->ip6_src;
185         if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
186                 goto freeit;
187
188         if (m->m_len < off + icmp6len) {
189                 m = m_pullup(m, off + icmp6len);
190                 if (m == NULL) {
191                         IP6STAT_INC(ip6s_exthdrtoolong);
192                         return;
193                 }
194         }
195         ip6 = mtod(m, struct ip6_hdr *);
196         nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
197
198         icmp6len -= sizeof(*nd_rs);
199         nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
200         if (nd6_options(&ndopts) < 0) {
201                 nd6log((LOG_INFO,
202                     "%s: invalid ND option, ignored\n", __func__));
203                 /* nd6_options have incremented stats */
204                 goto freeit;
205         }
206
207         lladdr = NULL;
208         lladdrlen = 0;
209         if (ndopts.nd_opts_src_lladdr) {
210                 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
211                 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
212         }
213
214         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
215                 nd6log((LOG_INFO,
216                     "%s: lladdrlen mismatch for %s (if %d, RS packet %d)\n",
217                     __func__, ip6_sprintf(ip6bufs, &saddr6),
218                     ifp->if_addrlen, lladdrlen - 2));
219                 goto bad;
220         }
221
222         nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
223
224  freeit:
225         m_freem(m);
226         return;
227
228  bad:
229         ICMP6STAT_INC(icp6s_badrs);
230         m_freem(m);
231 }
232
233 /*
234  * Receive Router Advertisement Message.
235  *
236  * Based on RFC 2461
237  * TODO: on-link bit on prefix information
238  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
239  */
240 void
241 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
242 {
243         struct ifnet *ifp;
244         struct nd_ifinfo *ndi;
245         struct ip6_hdr *ip6;
246         struct nd_router_advert *nd_ra;
247         struct in6_addr saddr6;
248         struct nd_defrouter *dr;
249         union nd_opts ndopts;
250         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
251         int mcast;
252
253         /*
254          * We only accept RAs only when the per-interface flag
255          * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
256          */
257         ifp = m->m_pkthdr.rcvif;
258         ndi = ND_IFINFO(ifp);
259         if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
260                 goto freeit;
261
262         /* RFC 6980: Nodes MUST silently ignore fragments */
263         if(m->m_flags & M_FRAGMENTED)
264                 goto freeit;
265
266         ip6 = mtod(m, struct ip6_hdr *);
267         if (ip6->ip6_hlim != 255) {
268                 nd6log((LOG_ERR,
269                     "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
270                     ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
271                     ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
272                 goto bad;
273         }
274
275         saddr6 = ip6->ip6_src;
276         if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
277                 nd6log((LOG_ERR,
278                     "%s: src %s is not link-local\n", __func__,
279                     ip6_sprintf(ip6bufs, &saddr6)));
280                 goto bad;
281         }
282
283         if (m->m_len < off + icmp6len) {
284                 m = m_pullup(m, off + icmp6len);
285                 if (m == NULL) {
286                         IP6STAT_INC(ip6s_exthdrtoolong);
287                         return;
288                 }
289         }
290         ip6 = mtod(m, struct ip6_hdr *);
291         nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
292
293         icmp6len -= sizeof(*nd_ra);
294         nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
295         if (nd6_options(&ndopts) < 0) {
296                 nd6log((LOG_INFO,
297                     "%s: invalid ND option, ignored\n", __func__));
298                 /* nd6_options have incremented stats */
299                 goto freeit;
300         }
301
302         mcast = 0;
303         dr = NULL;
304     {
305         struct nd_defrouter dr0;
306         u_int32_t advreachable = nd_ra->nd_ra_reachable;
307
308         /* remember if this is a multicasted advertisement */
309         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
310                 mcast = 1;
311
312         bzero(&dr0, sizeof(dr0));
313         dr0.rtaddr = saddr6;
314         dr0.raflags = nd_ra->nd_ra_flags_reserved;
315         /*
316          * Effectively-disable routes from RA messages when
317          * ND6_IFF_NO_RADR enabled on the receiving interface or
318          * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
319          */
320         if (ndi->flags & ND6_IFF_NO_RADR)
321                 dr0.rtlifetime = 0;
322         else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
323                 dr0.rtlifetime = 0;
324         else
325                 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
326         dr0.expire = time_uptime + dr0.rtlifetime;
327         dr0.ifp = ifp;
328         /* unspecified or not? (RFC 2461 6.3.4) */
329         if (advreachable) {
330                 advreachable = ntohl(advreachable);
331                 if (advreachable <= MAX_REACHABLE_TIME &&
332                     ndi->basereachable != advreachable) {
333                         ndi->basereachable = advreachable;
334                         ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
335                         ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
336                 }
337         }
338         if (nd_ra->nd_ra_retransmit)
339                 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
340         if (nd_ra->nd_ra_curhoplimit) {
341                 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
342                         ndi->chlim = nd_ra->nd_ra_curhoplimit;
343                 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
344                         log(LOG_ERR, "RA with a lower CurHopLimit sent from "
345                             "%s on %s (current = %d, received = %d). "
346                             "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
347                             if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
348                 }
349         }
350         dr = defrtrlist_update(&dr0);
351     }
352
353         /*
354          * prefix
355          */
356         if (ndopts.nd_opts_pi) {
357                 struct nd_opt_hdr *pt;
358                 struct nd_opt_prefix_info *pi = NULL;
359                 struct nd_prefixctl pr;
360
361                 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
362                      pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
363                      pt = (struct nd_opt_hdr *)((caddr_t)pt +
364                                                 (pt->nd_opt_len << 3))) {
365                         if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
366                                 continue;
367                         pi = (struct nd_opt_prefix_info *)pt;
368
369                         if (pi->nd_opt_pi_len != 4) {
370                                 nd6log((LOG_INFO,
371                                     "%s: invalid option len %d for prefix "
372                                     "information option, ignored\n", __func__,
373                                     pi->nd_opt_pi_len));
374                                 continue;
375                         }
376
377                         if (128 < pi->nd_opt_pi_prefix_len) {
378                                 nd6log((LOG_INFO,
379                                     "%s: invalid prefix len %d for prefix "
380                                     "information option, ignored\n", __func__,
381                                     pi->nd_opt_pi_prefix_len));
382                                 continue;
383                         }
384
385                         if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
386                          || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
387                                 nd6log((LOG_INFO,
388                                     "%s: invalid prefix %s, ignored\n",
389                                     __func__, ip6_sprintf(ip6bufs,
390                                         &pi->nd_opt_pi_prefix)));
391                                 continue;
392                         }
393
394                         bzero(&pr, sizeof(pr));
395                         pr.ndpr_prefix.sin6_family = AF_INET6;
396                         pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
397                         pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
398                         pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
399
400                         pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
401                             ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
402                         pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
403                             ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
404                         pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
405                         pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
406                         pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
407                         (void)prelist_update(&pr, dr, m, mcast);
408                 }
409         }
410         if (dr != NULL) {
411                 defrouter_rele(dr);
412                 dr = NULL;
413         }
414
415         /*
416          * MTU
417          */
418         if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
419                 u_long mtu;
420                 u_long maxmtu;
421
422                 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
423
424                 /* lower bound */
425                 if (mtu < IPV6_MMTU) {
426                         nd6log((LOG_INFO, "%s: bogus mtu option mtu=%lu sent "
427                             "from %s, ignoring\n", __func__,
428                             mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
429                         goto skip;
430                 }
431
432                 /* upper bound */
433                 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
434                     ? ndi->maxmtu : ifp->if_mtu;
435                 if (mtu <= maxmtu) {
436                         int change = (ndi->linkmtu != mtu);
437
438                         ndi->linkmtu = mtu;
439                         if (change) {
440                                 /* in6_maxmtu may change */
441                                 in6_setmaxmtu();
442                                 rt_updatemtu(ifp);
443                         }
444                 } else {
445                         nd6log((LOG_INFO, "%s: bogus mtu=%lu sent from %s; "
446                             "exceeds maxmtu %lu, ignoring\n", __func__,
447                             mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
448                 }
449         }
450
451  skip:
452
453         /*
454          * Source link layer address
455          */
456     {
457         char *lladdr = NULL;
458         int lladdrlen = 0;
459
460         if (ndopts.nd_opts_src_lladdr) {
461                 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
462                 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
463         }
464
465         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
466                 nd6log((LOG_INFO,
467                     "%s: lladdrlen mismatch for %s (if %d, RA packet %d)\n",
468                     __func__, ip6_sprintf(ip6bufs, &saddr6),
469                     ifp->if_addrlen, lladdrlen - 2));
470                 goto bad;
471         }
472
473         nd6_cache_lladdr(ifp, &saddr6, lladdr,
474             lladdrlen, ND_ROUTER_ADVERT, 0);
475
476         /*
477          * Installing a link-layer address might change the state of the
478          * router's neighbor cache, which might also affect our on-link
479          * detection of adveritsed prefixes.
480          */
481         pfxlist_onlink_check();
482     }
483
484  freeit:
485         m_freem(m);
486         return;
487
488  bad:
489         ICMP6STAT_INC(icp6s_badra);
490         m_freem(m);
491 }
492
493 /* tell the change to user processes watching the routing socket. */
494 static void
495 nd6_rtmsg(int cmd, struct rtentry *rt)
496 {
497         struct rt_addrinfo info;
498         struct ifnet *ifp;
499         struct ifaddr *ifa;
500
501         bzero((caddr_t)&info, sizeof(info));
502         info.rti_info[RTAX_DST] = rt_key(rt);
503         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
504         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
505         ifp = rt->rt_ifp;
506         if (ifp != NULL) {
507                 IF_ADDR_RLOCK(ifp);
508                 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
509                 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
510                 ifa_ref(ifa);
511                 IF_ADDR_RUNLOCK(ifp);
512                 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
513         } else
514                 ifa = NULL;
515
516         rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
517         if (ifa != NULL)
518                 ifa_free(ifa);
519 }
520
521 /* PFXRTR */
522 static struct nd_pfxrouter *
523 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
524 {
525         struct nd_pfxrouter *search;
526
527         ND6_LOCK_ASSERT();
528
529         LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
530                 if (search->router == dr)
531                         break;
532         }
533         return (search);
534 }
535
536 static void
537 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
538 {
539         struct nd_pfxrouter *new;
540         bool update;
541
542         ND6_UNLOCK_ASSERT();
543
544         ND6_RLOCK();
545         if (pfxrtr_lookup(pr, dr) != NULL) {
546                 ND6_RUNLOCK();
547                 return;
548         }
549         ND6_RUNLOCK();
550
551         new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
552         if (new == NULL)
553                 return;
554         defrouter_ref(dr);
555         new->router = dr;
556
557         ND6_WLOCK();
558         if (pfxrtr_lookup(pr, dr) == NULL) {
559                 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
560                 update = true;
561         } else {
562                 /* We lost a race to add the reference. */
563                 defrouter_rele(dr);
564                 free(new, M_IP6NDP);
565                 update = false;
566         }
567         ND6_WUNLOCK();
568
569         if (update)
570                 pfxlist_onlink_check();
571 }
572
573 static void
574 pfxrtr_del(struct nd_pfxrouter *pfr)
575 {
576
577         ND6_WLOCK_ASSERT();
578
579         LIST_REMOVE(pfr, pfr_entry);
580         defrouter_rele(pfr->router);
581         free(pfr, M_IP6NDP);
582 }
583
584
585 /* Default router list processing sub routines. */
586 static void
587 defrouter_addreq(struct nd_defrouter *new)
588 {
589         struct sockaddr_in6 def, mask, gate;
590         struct rtentry *newrt = NULL;
591         int error;
592
593         bzero(&def, sizeof(def));
594         bzero(&mask, sizeof(mask));
595         bzero(&gate, sizeof(gate));
596
597         def.sin6_len = mask.sin6_len = gate.sin6_len =
598             sizeof(struct sockaddr_in6);
599         def.sin6_family = gate.sin6_family = AF_INET6;
600         gate.sin6_addr = new->rtaddr;
601
602         error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
603             (struct sockaddr *)&gate, (struct sockaddr *)&mask,
604             RTF_GATEWAY, &newrt, new->ifp->if_fib);
605         if (newrt) {
606                 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
607                 RTFREE(newrt);
608         }
609         if (error == 0)
610                 new->installed = 1;
611 }
612
613 /*
614  * Remove the default route for a given router.
615  * This is just a subroutine function for defrouter_select_fib(), and
616  * should not be called from anywhere else.
617  */
618 static void
619 defrouter_delreq(struct nd_defrouter *dr)
620 {
621         struct sockaddr_in6 def, mask, gate;
622         struct rtentry *oldrt = NULL;
623
624         bzero(&def, sizeof(def));
625         bzero(&mask, sizeof(mask));
626         bzero(&gate, sizeof(gate));
627
628         def.sin6_len = mask.sin6_len = gate.sin6_len =
629             sizeof(struct sockaddr_in6);
630         def.sin6_family = gate.sin6_family = AF_INET6;
631         gate.sin6_addr = dr->rtaddr;
632
633         in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
634             (struct sockaddr *)&gate,
635             (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
636         if (oldrt) {
637                 nd6_rtmsg(RTM_DELETE, oldrt);
638                 RTFREE(oldrt);
639         }
640
641         dr->installed = 0;
642 }
643
644 void
645 defrouter_del(struct nd_defrouter *dr)
646 {
647         struct nd_defrouter *deldr = NULL;
648         struct nd_prefix *pr;
649         struct nd_pfxrouter *pfxrtr;
650
651         ND6_UNLOCK_ASSERT();
652
653         /*
654          * Flush all the routing table entries that use the router
655          * as a next hop.
656          */
657         if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
658                 rt6_flush(&dr->rtaddr, dr->ifp);
659
660         if (dr->installed) {
661                 deldr = dr;
662                 defrouter_delreq(dr);
663         }
664
665         /*
666          * Also delete all the pointers to the router in each prefix lists.
667          */
668         ND6_WLOCK();
669         LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
670                 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
671                         pfxrtr_del(pfxrtr);
672         }
673         ND6_WUNLOCK();
674
675         pfxlist_onlink_check();
676
677         /*
678          * If the router is the primary one, choose a new one.
679          * Note that defrouter_select_fib() will remove the current
680          * gateway from the routing table.
681          */
682         if (deldr)
683                 defrouter_select_fib(deldr->ifp->if_fib);
684
685         /*
686          * Release the list reference.
687          */
688         defrouter_rele(dr);
689 }
690
691
692 struct nd_defrouter *
693 defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
694 {
695         struct nd_defrouter *dr;
696
697         ND6_LOCK_ASSERT();
698         TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
699                 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
700                         defrouter_ref(dr);
701                         return (dr);
702                 }
703         return (NULL);
704 }
705
706 struct nd_defrouter *
707 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
708 {
709         struct nd_defrouter *dr;
710
711         ND6_RLOCK();
712         dr = defrouter_lookup_locked(addr, ifp);
713         ND6_RUNLOCK();
714         return (dr);
715 }
716
717 /*
718  * Remove all default routes from default router list.
719  */
720 void
721 defrouter_reset(void)
722 {
723         struct nd_defrouter *dr, **dra;
724         int count, i;
725
726         count = i = 0;
727
728         /*
729          * We can't delete routes with the ND lock held, so make a copy of the
730          * current default router list and use that when deleting routes.
731          */
732         ND6_RLOCK();
733         TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
734                 count++;
735         ND6_RUNLOCK();
736
737         dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
738
739         ND6_RLOCK();
740         TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
741                 if (i == count)
742                         break;
743                 defrouter_ref(dr);
744                 dra[i++] = dr;
745         }
746         ND6_RUNLOCK();
747
748         for (i = 0; i < count && dra[i] != NULL; i++) {
749                 defrouter_delreq(dra[i]);
750                 defrouter_rele(dra[i]);
751         }
752         free(dra, M_TEMP);
753
754         /*
755          * XXX should we also nuke any default routers in the kernel, by
756          * going through them by rtalloc1()?
757          */
758 }
759
760 /*
761  * Look up a matching default router list entry and remove it. Returns true if a
762  * matching entry was found, false otherwise.
763  */
764 bool
765 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
766 {
767         struct nd_defrouter *dr;
768
769         ND6_WLOCK();
770         dr = defrouter_lookup_locked(addr, ifp);
771         if (dr == NULL) {
772                 ND6_WUNLOCK();
773                 return (false);
774         }
775
776         defrouter_unlink(dr, NULL);
777         ND6_WUNLOCK();
778         defrouter_del(dr);
779         defrouter_rele(dr);
780         return (true);
781 }
782
783 /*
784  * for default router selection
785  * regards router-preference field as a 2-bit signed integer
786  */
787 static int
788 rtpref(struct nd_defrouter *dr)
789 {
790         switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
791         case ND_RA_FLAG_RTPREF_HIGH:
792                 return (RTPREF_HIGH);
793         case ND_RA_FLAG_RTPREF_MEDIUM:
794         case ND_RA_FLAG_RTPREF_RSV:
795                 return (RTPREF_MEDIUM);
796         case ND_RA_FLAG_RTPREF_LOW:
797                 return (RTPREF_LOW);
798         default:
799                 /*
800                  * This case should never happen.  If it did, it would mean a
801                  * serious bug of kernel internal.  We thus always bark here.
802                  * Or, can we even panic?
803                  */
804                 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
805                 return (RTPREF_INVALID);
806         }
807         /* NOTREACHED */
808 }
809
810 /*
811  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
812  * draft-ietf-ipngwg-router-selection:
813  * 1) Routers that are reachable or probably reachable should be preferred.
814  *    If we have more than one (probably) reachable router, prefer ones
815  *    with the highest router preference.
816  * 2) When no routers on the list are known to be reachable or
817  *    probably reachable, routers SHOULD be selected in a round-robin
818  *    fashion, regardless of router preference values.
819  * 3) If the Default Router List is empty, assume that all
820  *    destinations are on-link.
821  *
822  * We assume nd_defrouter is sorted by router preference value.
823  * Since the code below covers both with and without router preference cases,
824  * we do not need to classify the cases by ifdef.
825  *
826  * At this moment, we do not try to install more than one default router,
827  * even when the multipath routing is available, because we're not sure about
828  * the benefits for stub hosts comparing to the risk of making the code
829  * complicated and the possibility of introducing bugs.
830  *
831  * We maintain a single list of routers for multiple FIBs, only considering one
832  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
833  * we do the whole thing multiple times.
834  */
835 void
836 defrouter_select_fib(int fibnum)
837 {
838         struct nd_defrouter *dr, *selected_dr, *installed_dr;
839         struct llentry *ln = NULL;
840
841         if (fibnum == RT_ALL_FIBS) {
842                 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
843                         defrouter_select_fib(fibnum);
844                 }
845         }
846
847         ND6_RLOCK();
848         /*
849          * Let's handle easy case (3) first:
850          * If default router list is empty, there's nothing to be done.
851          */
852         if (TAILQ_EMPTY(&V_nd6_defrouter)) {
853                 ND6_RUNLOCK();
854                 return;
855         }
856
857         /*
858          * Search for a (probably) reachable router from the list.
859          * We just pick up the first reachable one (if any), assuming that
860          * the ordering rule of the list described in defrtrlist_update().
861          */
862         selected_dr = installed_dr = NULL;
863         TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
864                 IF_AFDATA_RLOCK(dr->ifp);
865                 if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
866                     (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
867                     ND6_IS_LLINFO_PROBREACH(ln)) {
868                         selected_dr = dr;
869                         defrouter_ref(selected_dr);
870                 }
871                 IF_AFDATA_RUNLOCK(dr->ifp);
872                 if (ln != NULL) {
873                         LLE_RUNLOCK(ln);
874                         ln = NULL;
875                 }
876
877                 if (dr->installed && dr->ifp->if_fib == fibnum) {
878                         if (installed_dr == NULL) {
879                                 installed_dr = dr;
880                                 defrouter_ref(installed_dr);
881                         } else {
882                                 /*
883                                  * this should not happen.
884                                  * warn for diagnosis.
885                                  */
886                                 log(LOG_ERR, "defrouter_select_fib: more than "
887                                              "one router is installed\n");
888                         }
889                 }
890         }
891         /*
892          * If none of the default routers was found to be reachable,
893          * round-robin the list regardless of preference.
894          * Otherwise, if we have an installed router, check if the selected
895          * (reachable) router should really be preferred to the installed one.
896          * We only prefer the new router when the old one is not reachable
897          * or when the new one has a really higher preference value.
898          */
899         if (selected_dr == NULL) {
900                 if (installed_dr == NULL ||
901                     TAILQ_NEXT(installed_dr, dr_entry) == NULL)
902                         dr = TAILQ_FIRST(&V_nd6_defrouter);
903                 else
904                         dr = TAILQ_NEXT(installed_dr, dr_entry);
905
906                 /* Ensure we select a router for this FIB. */
907                 TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
908                         if (dr->ifp->if_fib == fibnum) {
909                                 selected_dr = dr;
910                                 defrouter_ref(selected_dr);
911                                 break;
912                         }
913                 }
914         } else if (installed_dr != NULL) {
915                 IF_AFDATA_RLOCK(installed_dr->ifp);
916                 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
917                                      installed_dr->ifp)) &&
918                     ND6_IS_LLINFO_PROBREACH(ln) &&
919                     installed_dr->ifp->if_fib == fibnum &&
920                     rtpref(selected_dr) <= rtpref(installed_dr)) {
921                         defrouter_rele(selected_dr);
922                         selected_dr = installed_dr;
923                 }
924                 IF_AFDATA_RUNLOCK(installed_dr->ifp);
925                 if (ln != NULL)
926                         LLE_RUNLOCK(ln);
927         }
928         ND6_RUNLOCK();
929
930         /*
931          * If we selected a router for this FIB and it's different
932          * than the installed one, remove the installed router and
933          * install the selected one in its place.
934          */
935         if (installed_dr != selected_dr) {
936                 if (installed_dr != NULL) {
937                         defrouter_delreq(installed_dr);
938                         defrouter_rele(installed_dr);
939                 }
940                 if (selected_dr != NULL)
941                         defrouter_addreq(selected_dr);
942         }
943         if (selected_dr != NULL)
944                 defrouter_rele(selected_dr);
945 }
946
947 /*
948  * Maintain old KPI for default router selection.
949  * If unspecified, we can re-select routers for all FIBs.
950  */
951 void
952 defrouter_select(void)
953 {
954         defrouter_select_fib(RT_ALL_FIBS);
955 }
956
957 static struct nd_defrouter *
958 defrtrlist_update(struct nd_defrouter *new)
959 {
960         struct nd_defrouter *dr, *n;
961         uint64_t genid;
962         int oldpref;
963         bool writelocked;
964
965         if (new->rtlifetime == 0) {
966                 defrouter_remove(&new->rtaddr, new->ifp);
967                 return (NULL);
968         }
969
970         ND6_RLOCK();
971         writelocked = false;
972 restart:
973         dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
974         if (dr != NULL) {
975                 oldpref = rtpref(dr);
976
977                 /* override */
978                 dr->raflags = new->raflags; /* XXX flag check */
979                 dr->rtlifetime = new->rtlifetime;
980                 dr->expire = new->expire;
981
982                 /*
983                  * If the preference does not change, there's no need
984                  * to sort the entries. Also make sure the selected
985                  * router is still installed in the kernel.
986                  */
987                 if (dr->installed && rtpref(new) == oldpref) {
988                         if (writelocked)
989                                 ND6_WUNLOCK();
990                         else
991                                 ND6_RUNLOCK();
992                         return (dr);
993                 }
994         }
995
996         /*
997          * The router needs to be reinserted into the default router
998          * list, so upgrade to a write lock. If that fails and the list
999          * has potentially changed while the lock was dropped, we'll
1000          * redo the lookup with the write lock held.
1001          */
1002         if (!writelocked) {
1003                 writelocked = true;
1004                 if (!ND6_TRY_UPGRADE()) {
1005                         genid = V_nd6_list_genid;
1006                         ND6_RUNLOCK();
1007                         ND6_WLOCK();
1008                         if (genid != V_nd6_list_genid)
1009                                 goto restart;
1010                 }
1011         }
1012
1013         if (dr != NULL) {
1014                 /*
1015                  * The preferred router may have changed, so relocate this
1016                  * router.
1017                  */
1018                 TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1019                 n = dr;
1020         } else {
1021                 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
1022                 if (n == NULL) {
1023                         ND6_WUNLOCK();
1024                         return (NULL);
1025                 }
1026                 memcpy(n, new, sizeof(*n));
1027                 /* Initialize with an extra reference for the caller. */
1028                 refcount_init(&n->refcnt, 2);
1029         }
1030
1031         /*
1032          * Insert the new router in the Default Router List;
1033          * The Default Router List should be in the descending order
1034          * of router-preferece.  Routers with the same preference are
1035          * sorted in the arriving time order.
1036          */
1037
1038         /* insert at the end of the group */
1039         TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1040                 if (rtpref(n) > rtpref(dr))
1041                         break;
1042         }
1043         if (dr != NULL)
1044                 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1045         else
1046                 TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1047         V_nd6_list_genid++;
1048         ND6_WUNLOCK();
1049
1050         defrouter_select_fib(new->ifp->if_fib);
1051
1052         return (n);
1053 }
1054
1055 static int
1056 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1057 {
1058         if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1059                 ndpr->ndpr_preferred = 0;
1060         else
1061                 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1062         if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1063                 ndpr->ndpr_expire = 0;
1064         else
1065                 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
1066
1067         return 0;
1068 }
1069
1070 static void
1071 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1072 {
1073         /* init ia6t_expire */
1074         if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1075                 lt6->ia6t_expire = 0;
1076         else {
1077                 lt6->ia6t_expire = time_uptime;
1078                 lt6->ia6t_expire += lt6->ia6t_vltime;
1079         }
1080
1081         /* init ia6t_preferred */
1082         if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1083                 lt6->ia6t_preferred = 0;
1084         else {
1085                 lt6->ia6t_preferred = time_uptime;
1086                 lt6->ia6t_preferred += lt6->ia6t_pltime;
1087         }
1088 }
1089
1090 static struct in6_ifaddr *
1091 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1092 {
1093         struct ifnet *ifp = pr->ndpr_ifp;
1094         struct ifaddr *ifa;
1095         struct in6_aliasreq ifra;
1096         struct in6_ifaddr *ia, *ib;
1097         int error, plen0;
1098         struct in6_addr mask;
1099         int prefixlen = pr->ndpr_plen;
1100         int updateflags;
1101         char ip6buf[INET6_ADDRSTRLEN];
1102
1103         in6_prefixlen2mask(&mask, prefixlen);
1104
1105         /*
1106          * find a link-local address (will be interface ID).
1107          * Is it really mandatory? Theoretically, a global or a site-local
1108          * address can be configured without a link-local address, if we
1109          * have a unique interface identifier...
1110          *
1111          * it is not mandatory to have a link-local address, we can generate
1112          * interface identifier on the fly.  we do this because:
1113          * (1) it should be the easiest way to find interface identifier.
1114          * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1115          * for multiple addresses on a single interface, and possible shortcut
1116          * of DAD.  we omitted DAD for this reason in the past.
1117          * (3) a user can prevent autoconfiguration of global address
1118          * by removing link-local address by hand (this is partly because we
1119          * don't have other way to control the use of IPv6 on an interface.
1120          * this has been our design choice - cf. NRL's "ifconfig auto").
1121          * (4) it is easier to manage when an interface has addresses
1122          * with the same interface identifier, than to have multiple addresses
1123          * with different interface identifiers.
1124          */
1125         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1126         if (ifa)
1127                 ib = (struct in6_ifaddr *)ifa;
1128         else
1129                 return NULL;
1130
1131         /* prefixlen + ifidlen must be equal to 128 */
1132         plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1133         if (prefixlen != plen0) {
1134                 ifa_free(ifa);
1135                 nd6log((LOG_INFO,
1136                     "%s: wrong prefixlen for %s (prefix=%d ifid=%d)\n",
1137                     __func__, if_name(ifp), prefixlen, 128 - plen0));
1138                 return NULL;
1139         }
1140
1141         /* make ifaddr */
1142         in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1143
1144         IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1145         /* interface ID */
1146         ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1147             (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1148         ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1149             (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1150         ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1151             (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1152         ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1153             (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1154         ifa_free(ifa);
1155
1156         /* lifetimes. */
1157         ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1158         ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1159
1160         /* XXX: scope zone ID? */
1161
1162         ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1163
1164         /*
1165          * Make sure that we do not have this address already.  This should
1166          * usually not happen, but we can still see this case, e.g., if we
1167          * have manually configured the exact address to be configured.
1168          */
1169         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1170             &ifra.ifra_addr.sin6_addr);
1171         if (ifa != NULL) {
1172                 ifa_free(ifa);
1173                 /* this should be rare enough to make an explicit log */
1174                 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1175                     ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1176                 return (NULL);
1177         }
1178
1179         /*
1180          * Allocate ifaddr structure, link into chain, etc.
1181          * If we are going to create a new address upon receiving a multicasted
1182          * RA, we need to impose a random delay before starting DAD.
1183          * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1184          */
1185         updateflags = 0;
1186         if (mcast)
1187                 updateflags |= IN6_IFAUPDATE_DADDELAY;
1188         if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1189                 nd6log((LOG_ERR,
1190                     "%s: failed to make ifaddr %s on %s (errno=%d)\n", __func__,
1191                     ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1192                     if_name(ifp), error));
1193                 return (NULL);  /* ifaddr must not have been allocated. */
1194         }
1195
1196         ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1197         /*
1198          * XXXRW: Assumption of non-NULLness here might not be true with
1199          * fine-grained locking -- should we validate it?  Or just return
1200          * earlier ifa rather than looking it up again?
1201          */
1202         return (ia);            /* this is always non-NULL  and referenced. */
1203 }
1204
1205 static struct nd_prefix *
1206 nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1207 {
1208         struct nd_prefix *search;
1209
1210         ND6_LOCK_ASSERT();
1211
1212         LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1213                 if (key->ndpr_ifp == search->ndpr_ifp &&
1214                     key->ndpr_plen == search->ndpr_plen &&
1215                     in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1216                     &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1217                         nd6_prefix_ref(search);
1218                         break;
1219                 }
1220         }
1221         return (search);
1222 }
1223
1224 struct nd_prefix *
1225 nd6_prefix_lookup(struct nd_prefixctl *key)
1226 {
1227         struct nd_prefix *search;
1228
1229         ND6_RLOCK();
1230         search = nd6_prefix_lookup_locked(key);
1231         ND6_RUNLOCK();
1232         return (search);
1233 }
1234
1235 void
1236 nd6_prefix_ref(struct nd_prefix *pr)
1237 {
1238
1239         refcount_acquire(&pr->ndpr_refcnt);
1240 }
1241
1242 void
1243 nd6_prefix_rele(struct nd_prefix *pr)
1244 {
1245
1246         if (refcount_release(&pr->ndpr_refcnt)) {
1247                 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1248                     ("prefix %p has advertising routers", pr));
1249                 free(pr, M_IP6NDP);
1250         }
1251 }
1252
1253 int
1254 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
1255     struct nd_prefix **newp)
1256 {
1257         struct nd_prefix *new;
1258         char ip6buf[INET6_ADDRSTRLEN];
1259         int error;
1260
1261         new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
1262         if (new == NULL)
1263                 return (ENOMEM);
1264         refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1265         new->ndpr_ifp = pr->ndpr_ifp;
1266         new->ndpr_prefix = pr->ndpr_prefix;
1267         new->ndpr_plen = pr->ndpr_plen;
1268         new->ndpr_vltime = pr->ndpr_vltime;
1269         new->ndpr_pltime = pr->ndpr_pltime;
1270         new->ndpr_flags = pr->ndpr_flags;
1271         if ((error = in6_init_prefix_ltimes(new)) != 0) {
1272                 free(new, M_IP6NDP);
1273                 return (error);
1274         }
1275         new->ndpr_lastupdate = time_uptime;
1276
1277         /* initialization */
1278         LIST_INIT(&new->ndpr_advrtrs);
1279         in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1280         /* make prefix in the canonical form */
1281         IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
1282
1283         ND6_WLOCK();
1284         LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1285         V_nd6_list_genid++;
1286         ND6_WUNLOCK();
1287
1288         /* ND_OPT_PI_FLAG_ONLINK processing */
1289         if (new->ndpr_raf_onlink) {
1290                 ND6_ONLINK_LOCK();
1291                 if ((error = nd6_prefix_onlink(new)) != 0) {
1292                         nd6log((LOG_ERR, "%s: failed to make the prefix %s/%d "
1293                             "on-link on %s (errno=%d)\n", __func__,
1294                             ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1295                             pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
1296                         /* proceed anyway. XXX: is it correct? */
1297                 }
1298                 ND6_ONLINK_UNLOCK();
1299         }
1300
1301         if (dr != NULL)
1302                 pfxrtr_add(new, dr);
1303         if (newp != NULL)
1304                 *newp = new;
1305         return (0);
1306 }
1307
1308 /*
1309  * Remove a prefix from the prefix list and optionally stash it in a
1310  * caller-provided list.
1311  *
1312  * The ND6 lock must be held.
1313  */
1314 void
1315 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1316 {
1317
1318         ND6_WLOCK_ASSERT();
1319
1320         LIST_REMOVE(pr, ndpr_entry);
1321         V_nd6_list_genid++;
1322         if (list != NULL)
1323                 LIST_INSERT_HEAD(list, pr, ndpr_entry);
1324 }
1325
1326 /*
1327  * Free an unlinked prefix, first marking it off-link if necessary.
1328  */
1329 void
1330 nd6_prefix_del(struct nd_prefix *pr)
1331 {
1332         struct nd_pfxrouter *pfr, *next;
1333         int e;
1334         char ip6buf[INET6_ADDRSTRLEN];
1335
1336         KASSERT(pr->ndpr_addrcnt == 0,
1337             ("prefix %p has referencing addresses", pr));
1338         ND6_UNLOCK_ASSERT();
1339
1340         /*
1341          * Though these flags are now meaningless, we'd rather keep the value
1342          * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1343          * when executing "ndp -p".
1344          */
1345         if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1346                 ND6_ONLINK_LOCK();
1347                 if ((e = nd6_prefix_offlink(pr)) != 0) {
1348                         nd6log((LOG_ERR,
1349                             "%s: failed to make the prefix %s/%d offlink on %s "
1350                             "(errno=%d)\n", __func__,
1351                             ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1352                             pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1353                         /* what should we do? */
1354                 }
1355                 ND6_ONLINK_UNLOCK();
1356         }
1357
1358         /* Release references to routers that have advertised this prefix. */
1359         ND6_WLOCK();
1360         LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1361                 pfxrtr_del(pfr);
1362         ND6_WUNLOCK();
1363
1364         nd6_prefix_rele(pr);
1365
1366         pfxlist_onlink_check();
1367 }
1368
1369 static int
1370 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
1371     struct mbuf *m, int mcast)
1372 {
1373         struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1374         struct ifaddr *ifa;
1375         struct ifnet *ifp = new->ndpr_ifp;
1376         struct nd_prefix *pr;
1377         int error = 0;
1378         int auth;
1379         struct in6_addrlifetime lt6_tmp;
1380         char ip6buf[INET6_ADDRSTRLEN];
1381
1382         auth = 0;
1383         if (m) {
1384                 /*
1385                  * Authenticity for NA consists authentication for
1386                  * both IP header and IP datagrams, doesn't it ?
1387                  */
1388 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1389                 auth = ((m->m_flags & M_AUTHIPHDR) &&
1390                     (m->m_flags & M_AUTHIPDGM));
1391 #endif
1392         }
1393
1394         if ((pr = nd6_prefix_lookup(new)) != NULL) {
1395                 /*
1396                  * nd6_prefix_lookup() ensures that pr and new have the same
1397                  * prefix on a same interface.
1398                  */
1399
1400                 /*
1401                  * Update prefix information.  Note that the on-link (L) bit
1402                  * and the autonomous (A) bit should NOT be changed from 1
1403                  * to 0.
1404                  */
1405                 if (new->ndpr_raf_onlink == 1)
1406                         pr->ndpr_raf_onlink = 1;
1407                 if (new->ndpr_raf_auto == 1)
1408                         pr->ndpr_raf_auto = 1;
1409                 if (new->ndpr_raf_onlink) {
1410                         pr->ndpr_vltime = new->ndpr_vltime;
1411                         pr->ndpr_pltime = new->ndpr_pltime;
1412                         (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1413                         pr->ndpr_lastupdate = time_uptime;
1414                 }
1415
1416                 if (new->ndpr_raf_onlink &&
1417                     (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1418                         ND6_ONLINK_LOCK();
1419                         if ((error = nd6_prefix_onlink(pr)) != 0) {
1420                                 nd6log((LOG_ERR,
1421                                     "%s: failed to make the prefix %s/%d "
1422                                     "on-link on %s (errno=%d)\n", __func__,
1423                                     ip6_sprintf(ip6buf,
1424                                         &pr->ndpr_prefix.sin6_addr),
1425                                     pr->ndpr_plen, if_name(pr->ndpr_ifp),
1426                                     error));
1427                                 /* proceed anyway. XXX: is it correct? */
1428                         }
1429                         ND6_ONLINK_UNLOCK();
1430                 }
1431
1432                 if (dr != NULL)
1433                         pfxrtr_add(pr, dr);
1434         } else {
1435                 if (new->ndpr_vltime == 0)
1436                         goto end;
1437                 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1438                         goto end;
1439
1440                 error = nd6_prelist_add(new, dr, &pr);
1441                 if (error != 0) {
1442                         nd6log((LOG_NOTICE, "%s: nd6_prelist_add() failed for "
1443                             "the prefix %s/%d on %s (errno=%d)\n", __func__,
1444                             ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1445                             new->ndpr_plen, if_name(new->ndpr_ifp), error));
1446                         goto end; /* we should just give up in this case. */
1447                 }
1448
1449                 /*
1450                  * XXX: from the ND point of view, we can ignore a prefix
1451                  * with the on-link bit being zero.  However, we need a
1452                  * prefix structure for references from autoconfigured
1453                  * addresses.  Thus, we explicitly make sure that the prefix
1454                  * itself expires now.
1455                  */
1456                 if (pr->ndpr_raf_onlink == 0) {
1457                         pr->ndpr_vltime = 0;
1458                         pr->ndpr_pltime = 0;
1459                         in6_init_prefix_ltimes(pr);
1460                 }
1461         }
1462
1463         /*
1464          * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1465          * Note that pr must be non NULL at this point.
1466          */
1467
1468         /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1469         if (!new->ndpr_raf_auto)
1470                 goto end;
1471
1472         /*
1473          * 5.5.3 (b). the link-local prefix should have been ignored in
1474          * nd6_ra_input.
1475          */
1476
1477         /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1478         if (new->ndpr_pltime > new->ndpr_vltime) {
1479                 error = EINVAL; /* XXX: won't be used */
1480                 goto end;
1481         }
1482
1483         /*
1484          * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
1485          * an address configured by stateless autoconfiguration already in the
1486          * list of addresses associated with the interface, and the Valid
1487          * Lifetime is not 0, form an address.  We first check if we have
1488          * a matching prefix.
1489          * Note: we apply a clarification in rfc2462bis-02 here.  We only
1490          * consider autoconfigured addresses while RFC2462 simply said
1491          * "address".
1492          */
1493         IF_ADDR_RLOCK(ifp);
1494         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1495                 struct in6_ifaddr *ifa6;
1496                 u_int32_t remaininglifetime;
1497
1498                 if (ifa->ifa_addr->sa_family != AF_INET6)
1499                         continue;
1500
1501                 ifa6 = (struct in6_ifaddr *)ifa;
1502
1503                 /*
1504                  * We only consider autoconfigured addresses as per rfc2462bis.
1505                  */
1506                 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1507                         continue;
1508
1509                 /*
1510                  * Spec is not clear here, but I believe we should concentrate
1511                  * on unicast (i.e. not anycast) addresses.
1512                  * XXX: other ia6_flags? detached or duplicated?
1513                  */
1514                 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1515                         continue;
1516
1517                 /*
1518                  * Ignore the address if it is not associated with a prefix
1519                  * or is associated with a prefix that is different from this
1520                  * one.  (pr is never NULL here)
1521                  */
1522                 if (ifa6->ia6_ndpr != pr)
1523                         continue;
1524
1525                 if (ia6_match == NULL) /* remember the first one */
1526                         ia6_match = ifa6;
1527
1528                 /*
1529                  * An already autoconfigured address matched.  Now that we
1530                  * are sure there is at least one matched address, we can
1531                  * proceed to 5.5.3. (e): update the lifetimes according to the
1532                  * "two hours" rule and the privacy extension.
1533                  * We apply some clarifications in rfc2462bis:
1534                  * - use remaininglifetime instead of storedlifetime as a
1535                  *   variable name
1536                  * - remove the dead code in the "two-hour" rule
1537                  */
1538 #define TWOHOUR         (120*60)
1539                 lt6_tmp = ifa6->ia6_lifetime;
1540
1541                 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1542                         remaininglifetime = ND6_INFINITE_LIFETIME;
1543                 else if (time_uptime - ifa6->ia6_updatetime >
1544                          lt6_tmp.ia6t_vltime) {
1545                         /*
1546                          * The case of "invalid" address.  We should usually
1547                          * not see this case.
1548                          */
1549                         remaininglifetime = 0;
1550                 } else
1551                         remaininglifetime = lt6_tmp.ia6t_vltime -
1552                             (time_uptime - ifa6->ia6_updatetime);
1553
1554                 /* when not updating, keep the current stored lifetime. */
1555                 lt6_tmp.ia6t_vltime = remaininglifetime;
1556
1557                 if (TWOHOUR < new->ndpr_vltime ||
1558                     remaininglifetime < new->ndpr_vltime) {
1559                         lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1560                 } else if (remaininglifetime <= TWOHOUR) {
1561                         if (auth) {
1562                                 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1563                         }
1564                 } else {
1565                         /*
1566                          * new->ndpr_vltime <= TWOHOUR &&
1567                          * TWOHOUR < remaininglifetime
1568                          */
1569                         lt6_tmp.ia6t_vltime = TWOHOUR;
1570                 }
1571
1572                 /* The 2 hour rule is not imposed for preferred lifetime. */
1573                 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1574
1575                 in6_init_address_ltimes(pr, &lt6_tmp);
1576
1577                 /*
1578                  * We need to treat lifetimes for temporary addresses
1579                  * differently, according to
1580                  * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1581                  * we only update the lifetimes when they are in the maximum
1582                  * intervals.
1583                  */
1584                 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1585                         u_int32_t maxvltime, maxpltime;
1586
1587                         if (V_ip6_temp_valid_lifetime >
1588                             (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1589                             V_ip6_desync_factor)) {
1590                                 maxvltime = V_ip6_temp_valid_lifetime -
1591                                     (time_uptime - ifa6->ia6_createtime) -
1592                                     V_ip6_desync_factor;
1593                         } else
1594                                 maxvltime = 0;
1595                         if (V_ip6_temp_preferred_lifetime >
1596                             (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1597                             V_ip6_desync_factor)) {
1598                                 maxpltime = V_ip6_temp_preferred_lifetime -
1599                                     (time_uptime - ifa6->ia6_createtime) -
1600                                     V_ip6_desync_factor;
1601                         } else
1602                                 maxpltime = 0;
1603
1604                         if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1605                             lt6_tmp.ia6t_vltime > maxvltime) {
1606                                 lt6_tmp.ia6t_vltime = maxvltime;
1607                         }
1608                         if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1609                             lt6_tmp.ia6t_pltime > maxpltime) {
1610                                 lt6_tmp.ia6t_pltime = maxpltime;
1611                         }
1612                 }
1613                 ifa6->ia6_lifetime = lt6_tmp;
1614                 ifa6->ia6_updatetime = time_uptime;
1615         }
1616         IF_ADDR_RUNLOCK(ifp);
1617         if (ia6_match == NULL && new->ndpr_vltime) {
1618                 int ifidlen;
1619
1620                 /*
1621                  * 5.5.3 (d) (continued)
1622                  * No address matched and the valid lifetime is non-zero.
1623                  * Create a new address.
1624                  */
1625
1626                 /*
1627                  * Prefix Length check:
1628                  * If the sum of the prefix length and interface identifier
1629                  * length does not equal 128 bits, the Prefix Information
1630                  * option MUST be ignored.  The length of the interface
1631                  * identifier is defined in a separate link-type specific
1632                  * document.
1633                  */
1634                 ifidlen = in6_if2idlen(ifp);
1635                 if (ifidlen < 0) {
1636                         /* this should not happen, so we always log it. */
1637                         log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1638                             if_name(ifp));
1639                         goto end;
1640                 }
1641                 if (ifidlen + pr->ndpr_plen != 128) {
1642                         nd6log((LOG_INFO,
1643                             "%s: invalid prefixlen %d for %s, ignored\n",
1644                             __func__, pr->ndpr_plen, if_name(ifp)));
1645                         goto end;
1646                 }
1647
1648                 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1649                         /*
1650                          * note that we should use pr (not new) for reference.
1651                          */
1652                         pr->ndpr_addrcnt++;
1653                         ia6->ia6_ndpr = pr;
1654
1655                         /*
1656                          * RFC 3041 3.3 (2).
1657                          * When a new public address is created as described
1658                          * in RFC2462, also create a new temporary address.
1659                          *
1660                          * RFC 3041 3.5.
1661                          * When an interface connects to a new link, a new
1662                          * randomized interface identifier should be generated
1663                          * immediately together with a new set of temporary
1664                          * addresses.  Thus, we specifiy 1 as the 2nd arg of
1665                          * in6_tmpifadd().
1666                          */
1667                         if (V_ip6_use_tempaddr) {
1668                                 int e;
1669                                 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1670                                         nd6log((LOG_NOTICE, "%s: failed to "
1671                                             "create a temporary address "
1672                                             "(errno=%d)\n", __func__, e));
1673                                 }
1674                         }
1675                         ifa_free(&ia6->ia_ifa);
1676
1677                         /*
1678                          * A newly added address might affect the status
1679                          * of other addresses, so we check and update it.
1680                          * XXX: what if address duplication happens?
1681                          */
1682                         pfxlist_onlink_check();
1683                 } else {
1684                         /* just set an error. do not bark here. */
1685                         error = EADDRNOTAVAIL; /* XXX: might be unused. */
1686                 }
1687         }
1688
1689 end:
1690         if (pr != NULL)
1691                 nd6_prefix_rele(pr);
1692         return (error);
1693 }
1694
1695 /*
1696  * A supplement function used in the on-link detection below;
1697  * detect if a given prefix has a (probably) reachable advertising router.
1698  * XXX: lengthy function name...
1699  */
1700 static struct nd_pfxrouter *
1701 find_pfxlist_reachable_router(struct nd_prefix *pr)
1702 {
1703         struct nd_pfxrouter *pfxrtr;
1704         struct llentry *ln;
1705         int canreach;
1706
1707         ND6_LOCK_ASSERT();
1708
1709         LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1710                 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
1711                 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1712                 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
1713                 if (ln == NULL)
1714                         continue;
1715                 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1716                 LLE_RUNLOCK(ln);
1717                 if (canreach)
1718                         break;
1719         }
1720         return (pfxrtr);
1721 }
1722
1723 /*
1724  * Check if each prefix in the prefix list has at least one available router
1725  * that advertised the prefix (a router is "available" if its neighbor cache
1726  * entry is reachable or probably reachable).
1727  * If the check fails, the prefix may be off-link, because, for example,
1728  * we have moved from the network but the lifetime of the prefix has not
1729  * expired yet.  So we should not use the prefix if there is another prefix
1730  * that has an available router.
1731  * But, if there is no prefix that has an available router, we still regard
1732  * all the prefixes as on-link.  This is because we can't tell if all the
1733  * routers are simply dead or if we really moved from the network and there
1734  * is no router around us.
1735  */
1736 void
1737 pfxlist_onlink_check(void)
1738 {
1739         struct nd_prefix *pr;
1740         struct in6_ifaddr *ifa;
1741         struct nd_defrouter *dr;
1742         struct nd_pfxrouter *pfxrtr = NULL;
1743         struct rm_priotracker in6_ifa_tracker;
1744         uint64_t genid;
1745         uint32_t flags;
1746
1747         ND6_ONLINK_LOCK();
1748         ND6_RLOCK();
1749
1750         /*
1751          * Check if there is a prefix that has a reachable advertising
1752          * router.
1753          */
1754         LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1755                 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1756                         break;
1757         }
1758
1759         /*
1760          * If we have no such prefix, check whether we still have a router
1761          * that does not advertise any prefixes.
1762          */
1763         if (pr == NULL) {
1764                 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1765                         struct nd_prefix *pr0;
1766
1767                         LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1768                                 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1769                                         break;
1770                         }
1771                         if (pfxrtr != NULL)
1772                                 break;
1773                 }
1774         }
1775         if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1776                 /*
1777                  * There is at least one prefix that has a reachable router,
1778                  * or at least a router which probably does not advertise
1779                  * any prefixes.  The latter would be the case when we move
1780                  * to a new link where we have a router that does not provide
1781                  * prefixes and we configure an address by hand.
1782                  * Detach prefixes which have no reachable advertising
1783                  * router, and attach other prefixes.
1784                  */
1785                 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1786                         /* XXX: a link-local prefix should never be detached */
1787                         if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1788                             pr->ndpr_raf_onlink == 0 ||
1789                             pr->ndpr_raf_auto == 0)
1790                                 continue;
1791
1792                         if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1793                             find_pfxlist_reachable_router(pr) == NULL)
1794                                 pr->ndpr_stateflags |= NDPRF_DETACHED;
1795                         else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1796                             find_pfxlist_reachable_router(pr) != NULL)
1797                                 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1798                 }
1799         } else {
1800                 /* there is no prefix that has a reachable router */
1801                 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1802                         if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1803                             pr->ndpr_raf_onlink == 0 ||
1804                             pr->ndpr_raf_auto == 0)
1805                                 continue;
1806                         pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1807                 }
1808         }
1809
1810         /*
1811          * Remove each interface route associated with a (just) detached
1812          * prefix, and reinstall the interface route for a (just) attached
1813          * prefix.  Note that all attempt of reinstallation does not
1814          * necessarily success, when a same prefix is shared among multiple
1815          * interfaces.  Such cases will be handled in nd6_prefix_onlink,
1816          * so we don't have to care about them.
1817          */
1818 restart:
1819         LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1820                 char ip6buf[INET6_ADDRSTRLEN];
1821                 int e;
1822
1823                 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1824                     pr->ndpr_raf_onlink == 0 ||
1825                     pr->ndpr_raf_auto == 0)
1826                         continue;
1827
1828                 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1829                 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1830                         genid = V_nd6_list_genid;
1831                         ND6_RUNLOCK();
1832                         if ((flags & NDPRF_ONLINK) != 0 &&
1833                             (e = nd6_prefix_offlink(pr)) != 0) {
1834                                 nd6log((LOG_ERR,
1835                                     "%s: failed to make %s/%d offlink "
1836                                     "(errno=%d)\n", __func__, 
1837                                     ip6_sprintf(ip6buf,
1838                                             &pr->ndpr_prefix.sin6_addr),
1839                                             pr->ndpr_plen, e));
1840                         } else if ((flags & NDPRF_ONLINK) == 0 &&
1841                             (e = nd6_prefix_onlink(pr)) != 0) {
1842                                 nd6log((LOG_ERR,
1843                                     "%s: failed to make %s/%d onlink "
1844                                     "(errno=%d)\n", __func__,
1845                                     ip6_sprintf(ip6buf,
1846                                             &pr->ndpr_prefix.sin6_addr),
1847                                             pr->ndpr_plen, e));
1848                         }
1849                         ND6_RLOCK();
1850                         if (genid != V_nd6_list_genid)
1851                                 goto restart;
1852                 }
1853         }
1854
1855         /*
1856          * Changes on the prefix status might affect address status as well.
1857          * Make sure that all addresses derived from an attached prefix are
1858          * attached, and that all addresses derived from a detached prefix are
1859          * detached.  Note, however, that a manually configured address should
1860          * always be attached.
1861          * The precise detection logic is same as the one for prefixes.
1862          */
1863         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1864         CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1865                 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1866                         continue;
1867
1868                 if (ifa->ia6_ndpr == NULL) {
1869                         /*
1870                          * This can happen when we first configure the address
1871                          * (i.e. the address exists, but the prefix does not).
1872                          * XXX: complicated relationships...
1873                          */
1874                         continue;
1875                 }
1876
1877                 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1878                         break;
1879         }
1880         if (ifa) {
1881                 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1882                         if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1883                                 continue;
1884
1885                         if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1886                                 continue;
1887
1888                         if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1889                                 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1890                                         ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1891                                         ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1892                                         nd6_dad_start((struct ifaddr *)ifa, 0);
1893                                 }
1894                         } else {
1895                                 ifa->ia6_flags |= IN6_IFF_DETACHED;
1896                         }
1897                 }
1898         } else {
1899                 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1900                         if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1901                                 continue;
1902
1903                         if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1904                                 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1905                                 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1906                                 /* Do we need a delay in this case? */
1907                                 nd6_dad_start((struct ifaddr *)ifa, 0);
1908                         }
1909                 }
1910         }
1911         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1912         ND6_RUNLOCK();
1913         ND6_ONLINK_UNLOCK();
1914 }
1915
1916 static int
1917 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1918 {
1919         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1920         struct rib_head *rnh;
1921         struct rtentry *rt;
1922         struct sockaddr_in6 mask6;
1923         u_long rtflags;
1924         int error, a_failure, fibnum, maxfib;
1925
1926         /*
1927          * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1928          * ifa->ifa_rtrequest = nd6_rtrequest;
1929          */
1930         bzero(&mask6, sizeof(mask6));
1931         mask6.sin6_len = sizeof(mask6);
1932         mask6.sin6_addr = pr->ndpr_mask;
1933         rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1934
1935         if(V_rt_add_addr_allfibs) {
1936                 fibnum = 0;
1937                 maxfib = rt_numfibs;
1938         } else {
1939                 fibnum = ifa->ifa_ifp->if_fib;
1940                 maxfib = fibnum + 1;
1941         }
1942         a_failure = 0;
1943         for (; fibnum < maxfib; fibnum++) {
1944
1945                 rt = NULL;
1946                 error = in6_rtrequest(RTM_ADD,
1947                     (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
1948                     (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1949                 if (error == 0) {
1950                         KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1951                             "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1952                             error, pr, ifa));
1953
1954                         rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1955                         /* XXX what if rhn == NULL? */
1956                         RIB_WLOCK(rnh);
1957                         RT_LOCK(rt);
1958                         if (rt_setgate(rt, rt_key(rt),
1959                             (struct sockaddr *)&null_sdl) == 0) {
1960                                 struct sockaddr_dl *dl;
1961
1962                                 dl = (struct sockaddr_dl *)rt->rt_gateway;
1963                                 dl->sdl_type = rt->rt_ifp->if_type;
1964                                 dl->sdl_index = rt->rt_ifp->if_index;
1965                         }
1966                         RIB_WUNLOCK(rnh);
1967                         nd6_rtmsg(RTM_ADD, rt);
1968                         RT_UNLOCK(rt);
1969                         pr->ndpr_stateflags |= NDPRF_ONLINK;
1970                 } else {
1971                         char ip6buf[INET6_ADDRSTRLEN];
1972                         char ip6bufg[INET6_ADDRSTRLEN];
1973                         char ip6bufm[INET6_ADDRSTRLEN];
1974                         struct sockaddr_in6 *sin6;
1975
1976                         sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1977                         nd6log((LOG_ERR, "%s: failed to add "
1978                             "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1979                             "flags=%lx errno = %d\n", __func__,
1980                             ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1981                             pr->ndpr_plen, if_name(pr->ndpr_ifp),
1982                             ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1983                             ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1984                             rtflags, error));
1985
1986                         /* Save last error to return, see rtinit(). */
1987                         a_failure = error;
1988                 }
1989
1990                 if (rt != NULL) {
1991                         RT_LOCK(rt);
1992                         RT_REMREF(rt);
1993                         RT_UNLOCK(rt);
1994                 }
1995         }
1996
1997         /* Return the last error we got. */
1998         return (a_failure);
1999 }
2000
2001 int
2002 nd6_prefix_onlink(struct nd_prefix *pr)
2003 {
2004         struct ifaddr *ifa;
2005         struct ifnet *ifp = pr->ndpr_ifp;
2006         struct nd_prefix *opr;
2007         char ip6buf[INET6_ADDRSTRLEN];
2008         int error;
2009
2010         ND6_ONLINK_LOCK_ASSERT();
2011         ND6_UNLOCK_ASSERT();
2012
2013         if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
2014                 return (EEXIST);
2015
2016         /*
2017          * Add the interface route associated with the prefix.  Before
2018          * installing the route, check if there's the same prefix on another
2019          * interface, and the prefix has already installed the interface route.
2020          * Although such a configuration is expected to be rare, we explicitly
2021          * allow it.
2022          */
2023         ND6_RLOCK();
2024         LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2025                 if (opr == pr)
2026                         continue;
2027
2028                 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2029                         continue;
2030
2031                 if (!V_rt_add_addr_allfibs &&
2032                     opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2033                         continue;
2034
2035                 if (opr->ndpr_plen == pr->ndpr_plen &&
2036                     in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2037                     &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2038                         ND6_RUNLOCK();
2039                         return (0);
2040                 }
2041         }
2042         ND6_RUNLOCK();
2043
2044         /*
2045          * We prefer link-local addresses as the associated interface address.
2046          */
2047         /* search for a link-local addr */
2048         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
2049             IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
2050         if (ifa == NULL) {
2051                 /* XXX: freebsd does not have ifa_ifwithaf */
2052                 IF_ADDR_RLOCK(ifp);
2053                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2054                         if (ifa->ifa_addr->sa_family == AF_INET6) {
2055                                 ifa_ref(ifa);
2056                                 break;
2057                         }
2058                 }
2059                 IF_ADDR_RUNLOCK(ifp);
2060                 /* should we care about ia6_flags? */
2061         }
2062         if (ifa == NULL) {
2063                 /*
2064                  * This can still happen, when, for example, we receive an RA
2065                  * containing a prefix with the L bit set and the A bit clear,
2066                  * after removing all IPv6 addresses on the receiving
2067                  * interface.  This should, of course, be rare though.
2068                  */
2069                 nd6log((LOG_NOTICE,
2070                     "%s: failed to find any ifaddr to add route for a "
2071                     "prefix(%s/%d) on %s\n", __func__,
2072                     ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2073                     pr->ndpr_plen, if_name(ifp)));
2074                 return (0);
2075         }
2076
2077         error = nd6_prefix_onlink_rtrequest(pr, ifa);
2078
2079         if (ifa != NULL)
2080                 ifa_free(ifa);
2081
2082         return (error);
2083 }
2084
2085 int
2086 nd6_prefix_offlink(struct nd_prefix *pr)
2087 {
2088         int error = 0;
2089         struct ifnet *ifp = pr->ndpr_ifp;
2090         struct nd_prefix *opr;
2091         struct sockaddr_in6 sa6, mask6;
2092         struct rtentry *rt;
2093         char ip6buf[INET6_ADDRSTRLEN];
2094         uint64_t genid;
2095         int fibnum, maxfib, a_failure;
2096
2097         ND6_ONLINK_LOCK_ASSERT();
2098         ND6_UNLOCK_ASSERT();
2099
2100         if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2101                 return (EEXIST);
2102
2103         bzero(&sa6, sizeof(sa6));
2104         sa6.sin6_family = AF_INET6;
2105         sa6.sin6_len = sizeof(sa6);
2106         bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
2107             sizeof(struct in6_addr));
2108         bzero(&mask6, sizeof(mask6));
2109         mask6.sin6_family = AF_INET6;
2110         mask6.sin6_len = sizeof(sa6);
2111         bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
2112
2113         if (V_rt_add_addr_allfibs) {
2114                 fibnum = 0;
2115                 maxfib = rt_numfibs;
2116         } else {
2117                 fibnum = ifp->if_fib;
2118                 maxfib = fibnum + 1;
2119         }
2120
2121         a_failure = 0;
2122         for (; fibnum < maxfib; fibnum++) {
2123                 rt = NULL;
2124                 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
2125                     (struct sockaddr *)&mask6, 0, &rt, fibnum);
2126                 if (error == 0) {
2127                         /* report the route deletion to the routing socket. */
2128                         if (rt != NULL)
2129                                 nd6_rtmsg(RTM_DELETE, rt);
2130                 } else {
2131                         /* Save last error to return, see rtinit(). */
2132                         a_failure = error;
2133                 }
2134                 if (rt != NULL) {
2135                         RTFREE(rt);
2136                 }
2137         }
2138         error = a_failure;
2139         a_failure = 1;
2140         if (error == 0) {
2141                 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
2142
2143                 /*
2144                  * There might be the same prefix on another interface,
2145                  * the prefix which could not be on-link just because we have
2146                  * the interface route (see comments in nd6_prefix_onlink).
2147                  * If there's one, try to make the prefix on-link on the
2148                  * interface.
2149                  */
2150                 ND6_RLOCK();
2151 restart:
2152                 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2153                         /*
2154                          * KAME specific: detached prefixes should not be
2155                          * on-link.
2156                          */
2157                         if (opr == pr || (opr->ndpr_stateflags &
2158                             (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
2159                                 continue;
2160
2161                         if (opr->ndpr_plen == pr->ndpr_plen &&
2162                             in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2163                             &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2164                                 int e;
2165
2166                                 genid = V_nd6_list_genid;
2167                                 ND6_RUNLOCK();
2168                                 if ((e = nd6_prefix_onlink(opr)) != 0) {
2169                                         nd6log((LOG_ERR,
2170                                             "%s: failed to recover a prefix "
2171                                             "%s/%d from %s to %s (errno=%d)\n",
2172                                             __func__, ip6_sprintf(ip6buf,
2173                                                 &opr->ndpr_prefix.sin6_addr),
2174                                             opr->ndpr_plen, if_name(ifp),
2175                                             if_name(opr->ndpr_ifp), e));
2176                                 } else
2177                                         a_failure = 0;
2178                                 ND6_RLOCK();
2179                                 if (genid != V_nd6_list_genid)
2180                                         goto restart;
2181                         }
2182                 }
2183                 ND6_RUNLOCK();
2184         } else {
2185                 /* XXX: can we still set the NDPRF_ONLINK flag? */
2186                 nd6log((LOG_ERR,
2187                     "%s: failed to delete route: %s/%d on %s (errno=%d)\n",
2188                     __func__, ip6_sprintf(ip6buf, &sa6.sin6_addr),
2189                     pr->ndpr_plen, if_name(ifp), error));
2190         }
2191
2192         if (a_failure)
2193                 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2194                     (struct sockaddr *)&mask6, LLE_STATIC);
2195
2196         return (error);
2197 }
2198
2199 /*
2200  * ia0 - corresponding public address
2201  */
2202 int
2203 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
2204 {
2205         struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
2206         struct in6_ifaddr *newia;
2207         struct in6_aliasreq ifra;
2208         int error;
2209         int trylimit = 3;       /* XXX: adhoc value */
2210         int updateflags;
2211         u_int32_t randid[2];
2212         time_t vltime0, pltime0;
2213
2214         in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
2215             &ia0->ia_prefixmask.sin6_addr);
2216
2217         ifra.ifra_addr = ia0->ia_addr;  /* XXX: do we need this ? */
2218         /* clear the old IFID */
2219         IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
2220             &ifra.ifra_prefixmask.sin6_addr);
2221
2222   again:
2223         if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2224             (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2225                 nd6log((LOG_NOTICE, "%s: failed to find a good random IFID\n",
2226                     __func__));
2227                 return (EINVAL);
2228         }
2229         ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2230             (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
2231         ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2232             (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
2233
2234         /*
2235          * in6_get_tmpifid() quite likely provided a unique interface ID.
2236          * However, we may still have a chance to see collision, because
2237          * there may be a time lag between generation of the ID and generation
2238          * of the address.  So, we'll do one more sanity check.
2239          */
2240
2241         if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
2242                 if (trylimit-- > 0) {
2243                         forcegen = 1;
2244                         goto again;
2245                 }
2246
2247                 /* Give up.  Something strange should have happened.  */
2248                 nd6log((LOG_NOTICE, "%s: failed to find a unique random IFID\n",
2249                     __func__));
2250                 return (EEXIST);
2251         }
2252
2253         /*
2254          * The Valid Lifetime is the lower of the Valid Lifetime of the
2255          * public address or TEMP_VALID_LIFETIME.
2256          * The Preferred Lifetime is the lower of the Preferred Lifetime
2257          * of the public address or TEMP_PREFERRED_LIFETIME -
2258          * DESYNC_FACTOR.
2259          */
2260         if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2261                 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2262                     (ia0->ia6_lifetime.ia6t_vltime -
2263                     (time_uptime - ia0->ia6_updatetime));
2264                 if (vltime0 > V_ip6_temp_valid_lifetime)
2265                         vltime0 = V_ip6_temp_valid_lifetime;
2266         } else
2267                 vltime0 = V_ip6_temp_valid_lifetime;
2268         if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2269                 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2270                     (ia0->ia6_lifetime.ia6t_pltime -
2271                     (time_uptime - ia0->ia6_updatetime));
2272                 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2273                         pltime0 = V_ip6_temp_preferred_lifetime -
2274                             V_ip6_desync_factor;
2275                 }
2276         } else
2277                 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2278         ifra.ifra_lifetime.ia6t_vltime = vltime0;
2279         ifra.ifra_lifetime.ia6t_pltime = pltime0;
2280
2281         /*
2282          * A temporary address is created only if this calculated Preferred
2283          * Lifetime is greater than REGEN_ADVANCE time units.
2284          */
2285         if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2286                 return (0);
2287
2288         /* XXX: scope zone ID? */
2289
2290         ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2291
2292         /* allocate ifaddr structure, link into chain, etc. */
2293         updateflags = 0;
2294         if (delay)
2295                 updateflags |= IN6_IFAUPDATE_DADDELAY;
2296         if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2297                 return (error);
2298
2299         newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2300         if (newia == NULL) {    /* XXX: can it happen? */
2301                 nd6log((LOG_ERR,
2302                     "%s: ifa update succeeded, but we got no ifaddr\n",
2303                     __func__));
2304                 return (EINVAL); /* XXX */
2305         }
2306         newia->ia6_ndpr = ia0->ia6_ndpr;
2307         newia->ia6_ndpr->ndpr_addrcnt++;
2308         ifa_free(&newia->ia_ifa);
2309
2310         /*
2311          * A newly added address might affect the status of other addresses.
2312          * XXX: when the temporary address is generated with a new public
2313          * address, the onlink check is redundant.  However, it would be safe
2314          * to do the check explicitly everywhere a new address is generated,
2315          * and, in fact, we surely need the check when we create a new
2316          * temporary address due to deprecation of an old temporary address.
2317          */
2318         pfxlist_onlink_check();
2319
2320         return (0);
2321 }
2322
2323 static int
2324 rt6_deleteroute(const struct rtentry *rt, void *arg)
2325 {
2326 #define SIN6(s) ((struct sockaddr_in6 *)s)
2327         struct in6_addr *gate = (struct in6_addr *)arg;
2328
2329         if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2330                 return (0);
2331
2332         if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2333                 return (0);
2334         }
2335
2336         /*
2337          * Do not delete a static route.
2338          * XXX: this seems to be a bit ad-hoc. Should we consider the
2339          * 'cloned' bit instead?
2340          */
2341         if ((rt->rt_flags & RTF_STATIC) != 0)
2342                 return (0);
2343
2344         /*
2345          * We delete only host route. This means, in particular, we don't
2346          * delete default route.
2347          */
2348         if ((rt->rt_flags & RTF_HOST) == 0)
2349                 return (0);
2350
2351         return (1);
2352 #undef SIN6
2353 }
2354
2355 /*
2356  * Delete all the routing table entries that use the specified gateway.
2357  * XXX: this function causes search through all entries of routing table, so
2358  * it shouldn't be called when acting as a router.
2359  */
2360 void
2361 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2362 {
2363
2364         /* We'll care only link-local addresses */
2365         if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2366                 return;
2367
2368         /* XXX Do we really need to walk any but the default FIB? */
2369         rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2370 }
2371
2372 int
2373 nd6_setdefaultiface(int ifindex)
2374 {
2375         int error = 0;
2376
2377         if (ifindex < 0 || V_if_index < ifindex)
2378                 return (EINVAL);
2379         if (ifindex != 0 && !ifnet_byindex(ifindex))
2380                 return (EINVAL);
2381
2382         if (V_nd6_defifindex != ifindex) {
2383                 V_nd6_defifindex = ifindex;
2384                 if (V_nd6_defifindex > 0)
2385                         V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2386                 else
2387                         V_nd6_defifp = NULL;
2388
2389                 /*
2390                  * Our current implementation assumes one-to-one maping between
2391                  * interfaces and links, so it would be natural to use the
2392                  * default interface as the default link.
2393                  */
2394                 scope6_setdefault(V_nd6_defifp);
2395         }
2396
2397         return (error);
2398 }
2399
2400 bool
2401 nd6_defrouter_list_empty(void)
2402 {
2403
2404         return (TAILQ_EMPTY(&V_nd6_defrouter));
2405 }
2406
2407 void
2408 nd6_defrouter_timer(void)
2409 {
2410         struct nd_defrouter *dr, *ndr;
2411         struct nd_drhead drq;
2412
2413         TAILQ_INIT(&drq);
2414
2415         ND6_WLOCK();
2416         TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2417                 if (dr->expire && dr->expire < time_uptime)
2418                         defrouter_unlink(dr, &drq);
2419         ND6_WUNLOCK();
2420
2421         while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2422                 TAILQ_REMOVE(&drq, dr, dr_entry);
2423                 defrouter_del(dr);
2424         }
2425 }
2426
2427 /*
2428  * Nuke default router list entries toward ifp.
2429  * We defer removal of default router list entries that is installed in the
2430  * routing table, in order to keep additional side effects as small as possible.
2431  */
2432 void
2433 nd6_defrouter_purge(struct ifnet *ifp)
2434 {
2435         struct nd_defrouter *dr, *ndr;
2436         struct nd_drhead drq;
2437
2438         TAILQ_INIT(&drq);
2439
2440         ND6_WLOCK();
2441         TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2442                 if (dr->installed)
2443                         continue;
2444                 if (dr->ifp == ifp)
2445                         defrouter_unlink(dr, &drq);
2446         }
2447         TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2448                 if (!dr->installed)
2449                         continue;
2450                 if (dr->ifp == ifp)
2451                         defrouter_unlink(dr, &drq);
2452         }
2453         ND6_WUNLOCK();
2454
2455         /* Delete the unlinked router objects. */
2456         while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2457                 TAILQ_REMOVE(&drq, dr, dr_entry);
2458                 defrouter_del(dr);
2459         }
2460 }
2461
2462 void
2463 nd6_defrouter_flush_all(void)
2464 {
2465         struct nd_defrouter *dr;
2466         struct nd_drhead drq;
2467
2468         TAILQ_INIT(&drq);
2469
2470         ND6_WLOCK();
2471         while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2472                 defrouter_unlink(dr, &drq);
2473         ND6_WUNLOCK();
2474
2475         while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2476                 TAILQ_REMOVE(&drq, dr, dr_entry);
2477                 defrouter_del(dr);
2478         }
2479 }
2480
2481 void
2482 nd6_defrouter_init(void)
2483 {
2484
2485         TAILQ_INIT(&V_nd6_defrouter);
2486 }
2487
2488 static int
2489 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2490 {
2491         struct in6_defrouter d;
2492         struct nd_defrouter *dr;
2493         int error;
2494
2495         if (req->newptr != NULL)
2496                 return (EPERM);
2497
2498         error = sysctl_wire_old_buffer(req, 0);
2499         if (error != 0)
2500                 return (error);
2501
2502         bzero(&d, sizeof(d));
2503         d.rtaddr.sin6_family = AF_INET6;
2504         d.rtaddr.sin6_len = sizeof(d.rtaddr);
2505
2506         ND6_RLOCK();
2507         TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2508                 d.rtaddr.sin6_addr = dr->rtaddr;
2509                 error = sa6_recoverscope(&d.rtaddr);
2510                 if (error != 0)
2511                         break;
2512                 d.flags = dr->raflags;
2513                 d.rtlifetime = dr->rtlifetime;
2514                 d.expire = dr->expire + (time_second - time_uptime);
2515                 d.if_index = dr->ifp->if_index;
2516                 error = SYSCTL_OUT(req, &d, sizeof(d));
2517                 if (error != 0)
2518                         break;
2519         }
2520         ND6_RUNLOCK();
2521         return (error);
2522 }
2523 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2524         CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2525         NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2526         "NDP default router list");