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