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