nd6_rtr.c revision 1.26 1 1.26 itojun /* $NetBSD: nd6_rtr.c,v 1.26 2001/10/17 10:55:10 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.2 itojun
33 1.2 itojun #include <sys/param.h>
34 1.2 itojun #include <sys/systm.h>
35 1.2 itojun #include <sys/malloc.h>
36 1.2 itojun #include <sys/mbuf.h>
37 1.2 itojun #include <sys/socket.h>
38 1.2 itojun #include <sys/sockio.h>
39 1.2 itojun #include <sys/time.h>
40 1.2 itojun #include <sys/kernel.h>
41 1.2 itojun #include <sys/errno.h>
42 1.2 itojun #include <sys/ioctl.h>
43 1.2 itojun #include <sys/syslog.h>
44 1.2 itojun
45 1.2 itojun #include <net/if.h>
46 1.2 itojun #include <net/if_types.h>
47 1.2 itojun #include <net/if_dl.h>
48 1.2 itojun #include <net/route.h>
49 1.2 itojun #include <net/radix.h>
50 1.2 itojun
51 1.2 itojun #include <netinet/in.h>
52 1.2 itojun #include <netinet6/in6_var.h>
53 1.11 itojun #include <netinet/ip6.h>
54 1.2 itojun #include <netinet6/ip6_var.h>
55 1.2 itojun #include <netinet6/nd6.h>
56 1.11 itojun #include <netinet/icmp6.h>
57 1.2 itojun
58 1.7 itojun #include <net/net_osdep.h>
59 1.2 itojun
60 1.2 itojun #define SDL(s) ((struct sockaddr_dl *)s)
61 1.2 itojun
62 1.2 itojun static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *));
63 1.2 itojun static int prelist_add __P((struct nd_prefix *, struct nd_defrouter *));
64 1.2 itojun static struct nd_prefix *prefix_lookup __P((struct nd_prefix *));
65 1.2 itojun static struct in6_ifaddr *in6_ifadd __P((struct ifnet *, struct in6_addr *,
66 1.2 itojun struct in6_addr *, int));
67 1.2 itojun static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
68 1.2 itojun struct nd_defrouter *));
69 1.2 itojun static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *));
70 1.2 itojun static void pfxrtr_del __P((struct nd_pfxrouter *));
71 1.7 itojun static struct nd_pfxrouter *find_pfxlist_reachable_router __P((struct nd_prefix *));
72 1.2 itojun static void nd6_detach_prefix __P((struct nd_prefix *));
73 1.2 itojun static void nd6_attach_prefix __P((struct nd_prefix *));
74 1.7 itojun static void defrouter_addifreq __P((struct ifnet *));
75 1.2 itojun
76 1.2 itojun static void in6_init_address_ltimes __P((struct nd_prefix *ndpr,
77 1.7 itojun struct in6_addrlifetime *lt6,
78 1.7 itojun int update_vltime));
79 1.2 itojun
80 1.2 itojun static int rt6_deleteroute __P((struct radix_node *, void *));
81 1.2 itojun
82 1.2 itojun extern int nd6_recalc_reachtm_interval;
83 1.2 itojun
84 1.7 itojun struct ifnet *nd6_defifp;
85 1.7 itojun int nd6_defifindex;
86 1.7 itojun
87 1.2 itojun /*
88 1.2 itojun * Receive Router Solicitation Message - just for routers.
89 1.2 itojun * Router solicitation/advertisement is mostly managed by userland program
90 1.2 itojun * (rtadvd) so here we have no function like nd6_ra_output().
91 1.2 itojun *
92 1.2 itojun * Based on RFC 2461
93 1.2 itojun */
94 1.2 itojun void
95 1.2 itojun nd6_rs_input(m, off, icmp6len)
96 1.2 itojun struct mbuf *m;
97 1.2 itojun int off, icmp6len;
98 1.2 itojun {
99 1.2 itojun struct ifnet *ifp = m->m_pkthdr.rcvif;
100 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
101 1.12 itojun struct nd_router_solicit *nd_rs;
102 1.2 itojun struct in6_addr saddr6 = ip6->ip6_src;
103 1.2 itojun #if 0
104 1.2 itojun struct in6_addr daddr6 = ip6->ip6_dst;
105 1.2 itojun #endif
106 1.2 itojun char *lladdr = NULL;
107 1.2 itojun int lladdrlen = 0;
108 1.2 itojun #if 0
109 1.2 itojun struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
110 1.2 itojun struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
111 1.2 itojun struct rtentry *rt = NULL;
112 1.2 itojun int is_newentry;
113 1.2 itojun #endif
114 1.2 itojun union nd_opts ndopts;
115 1.2 itojun
116 1.2 itojun /* If I'm not a router, ignore it. */
117 1.2 itojun if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
118 1.12 itojun goto freeit;
119 1.2 itojun
120 1.2 itojun /* Sanity checks */
121 1.2 itojun if (ip6->ip6_hlim != 255) {
122 1.20 itojun nd6log((LOG_ERR,
123 1.20 itojun "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
124 1.20 itojun ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
125 1.20 itojun ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
126 1.20 itojun goto bad;
127 1.2 itojun }
128 1.2 itojun
129 1.2 itojun /*
130 1.2 itojun * Don't update the neighbor cache, if src = ::.
131 1.2 itojun * This indicates that the src has no IP address assigned yet.
132 1.2 itojun */
133 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
134 1.12 itojun goto freeit;
135 1.12 itojun
136 1.12 itojun #ifndef PULLDOWN_TEST
137 1.12 itojun IP6_EXTHDR_CHECK(m, off, icmp6len,);
138 1.12 itojun nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
139 1.12 itojun #else
140 1.12 itojun IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
141 1.12 itojun if (nd_rs == NULL) {
142 1.12 itojun icmp6stat.icp6s_tooshort++;
143 1.2 itojun return;
144 1.12 itojun }
145 1.12 itojun #endif
146 1.2 itojun
147 1.2 itojun icmp6len -= sizeof(*nd_rs);
148 1.2 itojun nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
149 1.2 itojun if (nd6_options(&ndopts) < 0) {
150 1.20 itojun nd6log((LOG_INFO,
151 1.20 itojun "nd6_rs_input: invalid ND option, ignored\n"));
152 1.20 itojun /* nd6_options have incremented stats */
153 1.12 itojun goto freeit;
154 1.2 itojun }
155 1.2 itojun
156 1.2 itojun if (ndopts.nd_opts_src_lladdr) {
157 1.2 itojun lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
158 1.2 itojun lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
159 1.2 itojun }
160 1.2 itojun
161 1.2 itojun if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
162 1.20 itojun nd6log((LOG_INFO,
163 1.2 itojun "nd6_rs_input: lladdrlen mismatch for %s "
164 1.2 itojun "(if %d, RS packet %d)\n",
165 1.20 itojun ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
166 1.20 itojun goto bad;
167 1.2 itojun }
168 1.2 itojun
169 1.6 itojun nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
170 1.12 itojun
171 1.12 itojun freeit:
172 1.12 itojun m_freem(m);
173 1.20 itojun return;
174 1.20 itojun
175 1.20 itojun bad:
176 1.20 itojun icmp6stat.icp6s_badrs++;
177 1.20 itojun m_freem(m);
178 1.2 itojun }
179 1.2 itojun
180 1.2 itojun /*
181 1.2 itojun * Receive Router Advertisement Message.
182 1.2 itojun *
183 1.2 itojun * Based on RFC 2461
184 1.2 itojun * TODO: on-link bit on prefix information
185 1.2 itojun * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
186 1.2 itojun */
187 1.2 itojun void
188 1.2 itojun nd6_ra_input(m, off, icmp6len)
189 1.2 itojun struct mbuf *m;
190 1.2 itojun int off, icmp6len;
191 1.2 itojun {
192 1.2 itojun struct ifnet *ifp = m->m_pkthdr.rcvif;
193 1.2 itojun struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
194 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
195 1.12 itojun struct nd_router_advert *nd_ra;
196 1.2 itojun struct in6_addr saddr6 = ip6->ip6_src;
197 1.2 itojun #if 0
198 1.2 itojun struct in6_addr daddr6 = ip6->ip6_dst;
199 1.12 itojun int flags; /* = nd_ra->nd_ra_flags_reserved; */
200 1.2 itojun int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
201 1.2 itojun int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
202 1.2 itojun #endif
203 1.2 itojun union nd_opts ndopts;
204 1.2 itojun struct nd_defrouter *dr;
205 1.2 itojun
206 1.2 itojun if (ip6_accept_rtadv == 0)
207 1.12 itojun goto freeit;
208 1.2 itojun
209 1.2 itojun if (ip6->ip6_hlim != 255) {
210 1.20 itojun nd6log((LOG_ERR,
211 1.20 itojun "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
212 1.20 itojun ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
213 1.20 itojun ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
214 1.20 itojun goto bad;
215 1.2 itojun }
216 1.2 itojun
217 1.2 itojun if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
218 1.20 itojun nd6log((LOG_ERR,
219 1.2 itojun "nd6_ra_input: src %s is not link-local\n",
220 1.20 itojun ip6_sprintf(&saddr6)));
221 1.20 itojun goto bad;
222 1.12 itojun }
223 1.12 itojun
224 1.12 itojun #ifndef PULLDOWN_TEST
225 1.12 itojun IP6_EXTHDR_CHECK(m, off, icmp6len,);
226 1.12 itojun nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
227 1.12 itojun #else
228 1.12 itojun IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
229 1.12 itojun if (nd_ra == NULL) {
230 1.12 itojun icmp6stat.icp6s_tooshort++;
231 1.2 itojun return;
232 1.2 itojun }
233 1.12 itojun #endif
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.26 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.2 itojun struct nd_opt_prefix_info *pi;
278 1.2 itojun struct nd_prefix pr;
279 1.2 itojun
280 1.2 itojun for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
281 1.2 itojun pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
282 1.2 itojun pt = (struct nd_opt_hdr *)((caddr_t)pt +
283 1.2 itojun (pt->nd_opt_len << 3))) {
284 1.2 itojun if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
285 1.2 itojun continue;
286 1.2 itojun pi = (struct nd_opt_prefix_info *)pt;
287 1.2 itojun
288 1.2 itojun if (pi->nd_opt_pi_len != 4) {
289 1.22 itojun nd6log((LOG_INFO,
290 1.22 itojun "nd6_ra_input: invalid option "
291 1.22 itojun "len %d for prefix information option, "
292 1.22 itojun "ignored\n", pi->nd_opt_pi_len));
293 1.2 itojun continue;
294 1.2 itojun }
295 1.2 itojun
296 1.2 itojun if (128 < pi->nd_opt_pi_prefix_len) {
297 1.22 itojun nd6log((LOG_INFO,
298 1.22 itojun "nd6_ra_input: invalid prefix "
299 1.22 itojun "len %d for prefix information option, "
300 1.22 itojun "ignored\n", pi->nd_opt_pi_prefix_len));
301 1.2 itojun continue;
302 1.2 itojun }
303 1.2 itojun
304 1.2 itojun if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
305 1.2 itojun || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
306 1.22 itojun nd6log((LOG_INFO,
307 1.22 itojun "nd6_ra_input: invalid prefix "
308 1.22 itojun "%s, ignored\n",
309 1.22 itojun ip6_sprintf(&pi->nd_opt_pi_prefix)));
310 1.2 itojun continue;
311 1.2 itojun }
312 1.2 itojun
313 1.2 itojun /* aggregatable unicast address, rfc2374 */
314 1.2 itojun if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
315 1.2 itojun && pi->nd_opt_pi_prefix_len != 64) {
316 1.22 itojun nd6log((LOG_INFO,
317 1.22 itojun "nd6_ra_input: invalid prefixlen "
318 1.22 itojun "%d for rfc2374 prefix %s, ignored\n",
319 1.22 itojun pi->nd_opt_pi_prefix_len,
320 1.22 itojun ip6_sprintf(&pi->nd_opt_pi_prefix)));
321 1.2 itojun continue;
322 1.2 itojun }
323 1.2 itojun
324 1.2 itojun bzero(&pr, sizeof(pr));
325 1.2 itojun pr.ndpr_prefix.sin6_family = AF_INET6;
326 1.2 itojun pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
327 1.2 itojun pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
328 1.2 itojun pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
329 1.2 itojun
330 1.2 itojun pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
331 1.2 itojun ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
332 1.2 itojun pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
333 1.2 itojun ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
334 1.2 itojun pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
335 1.2 itojun pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
336 1.2 itojun pr.ndpr_pltime =
337 1.2 itojun ntohl(pi->nd_opt_pi_preferred_time);
338 1.2 itojun
339 1.2 itojun if (in6_init_prefix_ltimes(&pr))
340 1.2 itojun continue; /* prefix lifetime init failed */
341 1.2 itojun
342 1.2 itojun (void)prelist_update(&pr, dr, m);
343 1.2 itojun }
344 1.2 itojun }
345 1.2 itojun
346 1.2 itojun /*
347 1.2 itojun * MTU
348 1.2 itojun */
349 1.2 itojun if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
350 1.2 itojun u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
351 1.2 itojun
352 1.2 itojun /* lower bound */
353 1.2 itojun if (mtu < IPV6_MMTU) {
354 1.22 itojun nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
355 1.2 itojun "mtu=%d sent from %s, ignoring\n",
356 1.22 itojun mtu, ip6_sprintf(&ip6->ip6_src)));
357 1.2 itojun goto skip;
358 1.2 itojun }
359 1.2 itojun
360 1.2 itojun /* upper bound */
361 1.2 itojun if (ndi->maxmtu) {
362 1.2 itojun if (mtu <= ndi->maxmtu) {
363 1.2 itojun int change = (ndi->linkmtu != mtu);
364 1.2 itojun
365 1.2 itojun ndi->linkmtu = mtu;
366 1.2 itojun if (change) /* in6_maxmtu may change */
367 1.2 itojun in6_setmaxmtu();
368 1.2 itojun } else {
369 1.22 itojun nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
370 1.2 itojun "mtu=%d sent from %s; "
371 1.2 itojun "exceeds maxmtu %d, ignoring\n",
372 1.2 itojun mtu, ip6_sprintf(&ip6->ip6_src),
373 1.22 itojun ndi->maxmtu));
374 1.2 itojun }
375 1.2 itojun } else {
376 1.22 itojun nd6log((LOG_INFO, "nd6_ra_input: mtu option "
377 1.2 itojun "mtu=%d sent from %s; maxmtu unknown, "
378 1.2 itojun "ignoring\n",
379 1.22 itojun mtu, ip6_sprintf(&ip6->ip6_src)));
380 1.2 itojun }
381 1.2 itojun }
382 1.2 itojun
383 1.2 itojun skip:
384 1.2 itojun
385 1.2 itojun /*
386 1.26 itojun * Source link layer address
387 1.2 itojun */
388 1.2 itojun {
389 1.2 itojun char *lladdr = NULL;
390 1.2 itojun int lladdrlen = 0;
391 1.2 itojun
392 1.2 itojun if (ndopts.nd_opts_src_lladdr) {
393 1.2 itojun lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
394 1.2 itojun lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
395 1.2 itojun }
396 1.2 itojun
397 1.2 itojun if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
398 1.20 itojun nd6log((LOG_INFO,
399 1.2 itojun "nd6_ra_input: lladdrlen mismatch for %s "
400 1.2 itojun "(if %d, RA packet %d)\n",
401 1.20 itojun ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
402 1.20 itojun goto bad;
403 1.2 itojun }
404 1.2 itojun
405 1.6 itojun nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
406 1.7 itojun
407 1.7 itojun /*
408 1.7 itojun * Installing a link-layer address might change the state of the
409 1.7 itojun * router's neighbor cache, which might also affect our on-link
410 1.7 itojun * detection of adveritsed prefixes.
411 1.7 itojun */
412 1.7 itojun pfxlist_onlink_check();
413 1.2 itojun }
414 1.12 itojun
415 1.20 itojun freeit:
416 1.20 itojun m_freem(m);
417 1.20 itojun return;
418 1.20 itojun
419 1.20 itojun bad:
420 1.20 itojun icmp6stat.icp6s_badra++;
421 1.12 itojun m_freem(m);
422 1.2 itojun }
423 1.2 itojun
424 1.2 itojun /*
425 1.2 itojun * default router list proccessing sub routines
426 1.2 itojun */
427 1.2 itojun void
428 1.2 itojun defrouter_addreq(new)
429 1.2 itojun struct nd_defrouter *new;
430 1.2 itojun {
431 1.2 itojun struct sockaddr_in6 def, mask, gate;
432 1.2 itojun int s;
433 1.2 itojun
434 1.2 itojun Bzero(&def, sizeof(def));
435 1.2 itojun Bzero(&mask, sizeof(mask));
436 1.2 itojun Bzero(&gate, sizeof(gate));
437 1.2 itojun
438 1.2 itojun def.sin6_len = mask.sin6_len = gate.sin6_len
439 1.2 itojun = sizeof(struct sockaddr_in6);
440 1.2 itojun def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
441 1.2 itojun gate.sin6_addr = new->rtaddr;
442 1.2 itojun
443 1.4 itojun s = splsoftnet();
444 1.2 itojun (void)rtrequest(RTM_ADD, (struct sockaddr *)&def,
445 1.2 itojun (struct sockaddr *)&gate, (struct sockaddr *)&mask,
446 1.2 itojun RTF_GATEWAY, NULL);
447 1.2 itojun splx(s);
448 1.2 itojun return;
449 1.2 itojun }
450 1.2 itojun
451 1.7 itojun /* Add a route to a given interface as default */
452 1.7 itojun static void
453 1.7 itojun defrouter_addifreq(ifp)
454 1.7 itojun struct ifnet *ifp;
455 1.7 itojun {
456 1.7 itojun struct sockaddr_in6 def, mask;
457 1.7 itojun struct ifaddr *ifa;
458 1.7 itojun int error, flags;
459 1.7 itojun
460 1.7 itojun bzero(&def, sizeof(def));
461 1.7 itojun bzero(&mask, sizeof(mask));
462 1.7 itojun
463 1.7 itojun def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
464 1.7 itojun def.sin6_family = mask.sin6_family = AF_INET6;
465 1.7 itojun
466 1.7 itojun /*
467 1.7 itojun * Search for an ifaddr beloging to the specified interface.
468 1.7 itojun * XXX: An IPv6 address are required to be assigned on the interface.
469 1.7 itojun */
470 1.7 itojun if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
471 1.22 itojun nd6log((LOG_ERR, /* better error? */
472 1.7 itojun "defrouter_addifreq: failed to find an ifaddr "
473 1.7 itojun "to install a route to interface %s\n",
474 1.22 itojun if_name(ifp)));
475 1.7 itojun return;
476 1.7 itojun }
477 1.7 itojun
478 1.7 itojun flags = ifa->ifa_flags;
479 1.7 itojun if ((ifp->if_flags & IFF_POINTOPOINT) != 0)
480 1.7 itojun flags &= ~RTF_CLONING;
481 1.7 itojun if ((error = rtrequest(RTM_ADD, (struct sockaddr *)&def,
482 1.7 itojun ifa->ifa_addr, (struct sockaddr *)&mask,
483 1.7 itojun flags, NULL)) != 0) {
484 1.22 itojun nd6log((LOG_ERR,
485 1.7 itojun "defrouter_addifreq: failed to install a route to "
486 1.7 itojun "interface %s (errno = %d)\n",
487 1.22 itojun if_name(ifp), error));
488 1.7 itojun }
489 1.7 itojun }
490 1.7 itojun
491 1.2 itojun struct nd_defrouter *
492 1.2 itojun defrouter_lookup(addr, ifp)
493 1.2 itojun struct in6_addr *addr;
494 1.2 itojun struct ifnet *ifp;
495 1.2 itojun {
496 1.2 itojun struct nd_defrouter *dr;
497 1.2 itojun
498 1.7 itojun for (dr = TAILQ_FIRST(&nd_defrouter); dr;
499 1.7 itojun dr = TAILQ_NEXT(dr, dr_entry)) {
500 1.2 itojun if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
501 1.2 itojun return(dr);
502 1.7 itojun }
503 1.2 itojun
504 1.2 itojun return(NULL); /* search failed */
505 1.2 itojun }
506 1.2 itojun
507 1.2 itojun void
508 1.2 itojun defrouter_delreq(dr, dofree)
509 1.2 itojun struct nd_defrouter *dr;
510 1.2 itojun int dofree;
511 1.2 itojun {
512 1.2 itojun struct sockaddr_in6 def, mask, gate;
513 1.2 itojun
514 1.2 itojun Bzero(&def, sizeof(def));
515 1.2 itojun Bzero(&mask, sizeof(mask));
516 1.2 itojun Bzero(&gate, sizeof(gate));
517 1.2 itojun
518 1.2 itojun def.sin6_len = mask.sin6_len = gate.sin6_len
519 1.2 itojun = sizeof(struct sockaddr_in6);
520 1.2 itojun def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
521 1.2 itojun gate.sin6_addr = dr->rtaddr;
522 1.2 itojun
523 1.2 itojun rtrequest(RTM_DELETE, (struct sockaddr *)&def,
524 1.2 itojun (struct sockaddr *)&gate,
525 1.2 itojun (struct sockaddr *)&mask,
526 1.2 itojun RTF_GATEWAY, (struct rtentry **)0);
527 1.2 itojun
528 1.7 itojun if (dofree) /* XXX: necessary? */
529 1.2 itojun free(dr, M_IP6NDP);
530 1.2 itojun }
531 1.2 itojun
532 1.2 itojun void
533 1.2 itojun defrtrlist_del(dr)
534 1.2 itojun struct nd_defrouter *dr;
535 1.2 itojun {
536 1.2 itojun struct nd_defrouter *deldr = NULL;
537 1.2 itojun struct nd_prefix *pr;
538 1.2 itojun
539 1.2 itojun /*
540 1.2 itojun * Flush all the routing table entries that use the router
541 1.2 itojun * as a next hop.
542 1.2 itojun */
543 1.2 itojun if (!ip6_forwarding && ip6_accept_rtadv) {
544 1.2 itojun /* above is a good condition? */
545 1.2 itojun rt6_flush(&dr->rtaddr, dr->ifp);
546 1.2 itojun }
547 1.2 itojun
548 1.7 itojun if (dr == TAILQ_FIRST(&nd_defrouter))
549 1.2 itojun deldr = dr; /* The router is primary. */
550 1.2 itojun
551 1.7 itojun TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
552 1.2 itojun
553 1.2 itojun /*
554 1.2 itojun * Also delete all the pointers to the router in each prefix lists.
555 1.2 itojun */
556 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
557 1.2 itojun struct nd_pfxrouter *pfxrtr;
558 1.2 itojun if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
559 1.2 itojun pfxrtr_del(pfxrtr);
560 1.2 itojun }
561 1.2 itojun pfxlist_onlink_check();
562 1.2 itojun
563 1.2 itojun /*
564 1.7 itojun * If the router is the primary one, choose a new one.
565 1.7 itojun * Note that defrouter_select() will remove the current gateway
566 1.7 itojun * from the routing table.
567 1.2 itojun */
568 1.2 itojun if (deldr)
569 1.7 itojun defrouter_select();
570 1.7 itojun
571 1.2 itojun free(dr, M_IP6NDP);
572 1.2 itojun }
573 1.2 itojun
574 1.7 itojun /*
575 1.7 itojun * Default Router Selection according to Section 6.3.6 of RFC 2461:
576 1.7 itojun * 1) Routers that are reachable or probably reachable should be
577 1.7 itojun * preferred.
578 1.7 itojun * 2) When no routers on the list are known to be reachable or
579 1.7 itojun * probably reachable, routers SHOULD be selected in a round-robin
580 1.7 itojun * fashion.
581 1.7 itojun * 3) If the Default Router List is empty, assume that all
582 1.7 itojun * destinations are on-link.
583 1.7 itojun */
584 1.7 itojun void
585 1.7 itojun defrouter_select()
586 1.7 itojun {
587 1.7 itojun int s = splsoftnet();
588 1.7 itojun struct nd_defrouter *dr, anydr;
589 1.7 itojun struct rtentry *rt = NULL;
590 1.7 itojun struct llinfo_nd6 *ln = NULL;
591 1.7 itojun
592 1.7 itojun /*
593 1.7 itojun * Search for a (probably) reachable router from the list.
594 1.7 itojun */
595 1.7 itojun for (dr = TAILQ_FIRST(&nd_defrouter); dr;
596 1.7 itojun dr = TAILQ_NEXT(dr, dr_entry)) {
597 1.7 itojun if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
598 1.7 itojun (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
599 1.7 itojun ND6_IS_LLINFO_PROBREACH(ln)) {
600 1.7 itojun /* Got it, and move it to the head */
601 1.7 itojun TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
602 1.7 itojun TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry);
603 1.7 itojun break;
604 1.7 itojun }
605 1.7 itojun }
606 1.7 itojun
607 1.7 itojun if ((dr = TAILQ_FIRST(&nd_defrouter))) {
608 1.7 itojun /*
609 1.7 itojun * De-install the previous default gateway and install
610 1.7 itojun * a new one.
611 1.7 itojun * Note that if there is no reachable router in the list,
612 1.7 itojun * the head entry will be used anyway.
613 1.7 itojun * XXX: do we have to check the current routing table entry?
614 1.7 itojun */
615 1.7 itojun bzero(&anydr, sizeof(anydr));
616 1.7 itojun defrouter_delreq(&anydr, 0);
617 1.7 itojun defrouter_addreq(dr);
618 1.7 itojun }
619 1.7 itojun else {
620 1.7 itojun /*
621 1.7 itojun * The Default Router List is empty, so install the default
622 1.7 itojun * route to an inteface.
623 1.7 itojun * XXX: The specification does not say this mechanism should
624 1.7 itojun * be restricted to hosts, but this would be not useful
625 1.7 itojun * (even harmful) for routers.
626 1.7 itojun */
627 1.7 itojun if (!ip6_forwarding) {
628 1.7 itojun /*
629 1.7 itojun * De-install the current default route
630 1.7 itojun * in advance.
631 1.7 itojun */
632 1.7 itojun bzero(&anydr, sizeof(anydr));
633 1.7 itojun defrouter_delreq(&anydr, 0);
634 1.7 itojun if (nd6_defifp) {
635 1.7 itojun /*
636 1.7 itojun * Install a route to the default interface
637 1.7 itojun * as default route.
638 1.20 itojun * XXX: we enable this for host only, because
639 1.20 itojun * this may override a default route installed
640 1.20 itojun * a user process (e.g. routing daemon) in a
641 1.20 itojun * router case.
642 1.7 itojun */
643 1.7 itojun defrouter_addifreq(nd6_defifp);
644 1.20 itojun } else {
645 1.20 itojun nd6log((LOG_INFO, "defrouter_select: "
646 1.20 itojun "there's no default router and no default"
647 1.20 itojun " interface\n"));
648 1.7 itojun }
649 1.7 itojun }
650 1.7 itojun }
651 1.7 itojun
652 1.7 itojun splx(s);
653 1.7 itojun return;
654 1.7 itojun }
655 1.7 itojun
656 1.2 itojun static struct nd_defrouter *
657 1.2 itojun defrtrlist_update(new)
658 1.2 itojun struct nd_defrouter *new;
659 1.2 itojun {
660 1.2 itojun struct nd_defrouter *dr, *n;
661 1.4 itojun int s = splsoftnet();
662 1.2 itojun
663 1.2 itojun if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
664 1.2 itojun /* entry exists */
665 1.2 itojun if (new->rtlifetime == 0) {
666 1.2 itojun defrtrlist_del(dr);
667 1.2 itojun dr = NULL;
668 1.2 itojun } else {
669 1.2 itojun /* override */
670 1.2 itojun dr->flags = new->flags; /* xxx flag check */
671 1.2 itojun dr->rtlifetime = new->rtlifetime;
672 1.2 itojun dr->expire = new->expire;
673 1.2 itojun }
674 1.2 itojun splx(s);
675 1.2 itojun return(dr);
676 1.2 itojun }
677 1.2 itojun
678 1.2 itojun /* entry does not exist */
679 1.2 itojun if (new->rtlifetime == 0) {
680 1.2 itojun splx(s);
681 1.2 itojun return(NULL);
682 1.2 itojun }
683 1.2 itojun
684 1.2 itojun n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
685 1.2 itojun if (n == NULL) {
686 1.2 itojun splx(s);
687 1.2 itojun return(NULL);
688 1.2 itojun }
689 1.2 itojun bzero(n, sizeof(*n));
690 1.2 itojun *n = *new;
691 1.7 itojun
692 1.7 itojun /*
693 1.7 itojun * Insert the new router at the end of the Default Router List.
694 1.7 itojun * If there is no other router, install it anyway. Otherwise,
695 1.16 itojun * just continue to use the current default router.
696 1.7 itojun */
697 1.7 itojun TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
698 1.7 itojun if (TAILQ_FIRST(&nd_defrouter) == n)
699 1.7 itojun defrouter_select();
700 1.2 itojun splx(s);
701 1.2 itojun
702 1.2 itojun return(n);
703 1.2 itojun }
704 1.2 itojun
705 1.2 itojun static struct nd_pfxrouter *
706 1.2 itojun pfxrtr_lookup(pr, dr)
707 1.2 itojun struct nd_prefix *pr;
708 1.2 itojun struct nd_defrouter *dr;
709 1.2 itojun {
710 1.2 itojun struct nd_pfxrouter *search;
711 1.2 itojun
712 1.2 itojun for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
713 1.2 itojun if (search->router == dr)
714 1.2 itojun break;
715 1.2 itojun }
716 1.2 itojun
717 1.2 itojun return(search);
718 1.2 itojun }
719 1.2 itojun
720 1.2 itojun static void
721 1.2 itojun pfxrtr_add(pr, dr)
722 1.2 itojun struct nd_prefix *pr;
723 1.2 itojun struct nd_defrouter *dr;
724 1.2 itojun {
725 1.2 itojun struct nd_pfxrouter *new;
726 1.2 itojun
727 1.2 itojun new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
728 1.2 itojun if (new == NULL)
729 1.2 itojun return;
730 1.2 itojun bzero(new, sizeof(*new));
731 1.2 itojun new->router = dr;
732 1.2 itojun
733 1.2 itojun LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
734 1.2 itojun
735 1.2 itojun pfxlist_onlink_check();
736 1.2 itojun }
737 1.2 itojun
738 1.2 itojun static void
739 1.2 itojun pfxrtr_del(pfr)
740 1.2 itojun struct nd_pfxrouter *pfr;
741 1.2 itojun {
742 1.2 itojun LIST_REMOVE(pfr, pfr_entry);
743 1.2 itojun free(pfr, M_IP6NDP);
744 1.2 itojun }
745 1.2 itojun
746 1.2 itojun static struct nd_prefix *
747 1.2 itojun prefix_lookup(pr)
748 1.2 itojun struct nd_prefix *pr;
749 1.2 itojun {
750 1.2 itojun struct nd_prefix *search;
751 1.2 itojun
752 1.2 itojun for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
753 1.2 itojun if (pr->ndpr_ifp == search->ndpr_ifp &&
754 1.2 itojun pr->ndpr_plen == search->ndpr_plen &&
755 1.2 itojun in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
756 1.2 itojun &search->ndpr_prefix.sin6_addr,
757 1.2 itojun pr->ndpr_plen)
758 1.2 itojun ) {
759 1.2 itojun break;
760 1.2 itojun }
761 1.2 itojun }
762 1.2 itojun
763 1.2 itojun return(search);
764 1.2 itojun }
765 1.2 itojun
766 1.2 itojun static int
767 1.2 itojun prelist_add(pr, dr)
768 1.2 itojun struct nd_prefix *pr;
769 1.2 itojun struct nd_defrouter *dr;
770 1.2 itojun {
771 1.2 itojun struct nd_prefix *new;
772 1.2 itojun int i, s;
773 1.2 itojun
774 1.2 itojun new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
775 1.2 itojun if (new == NULL)
776 1.2 itojun return ENOMEM;
777 1.2 itojun bzero(new, sizeof(*new));
778 1.2 itojun *new = *pr;
779 1.2 itojun
780 1.2 itojun /* initilization */
781 1.2 itojun new->ndpr_statef_onlink = pr->ndpr_statef_onlink;
782 1.2 itojun LIST_INIT(&new->ndpr_advrtrs);
783 1.2 itojun in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
784 1.2 itojun /* make prefix in the canonical form */
785 1.2 itojun for (i = 0; i < 4; i++)
786 1.2 itojun new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
787 1.2 itojun new->ndpr_mask.s6_addr32[i];
788 1.2 itojun
789 1.2 itojun /* xxx ND_OPT_PI_FLAG_ONLINK processing */
790 1.2 itojun
791 1.4 itojun s = splsoftnet();
792 1.2 itojun /* link ndpr_entry to nd_prefix list */
793 1.2 itojun LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
794 1.2 itojun splx(s);
795 1.2 itojun
796 1.2 itojun if (dr)
797 1.2 itojun pfxrtr_add(new, dr);
798 1.2 itojun
799 1.2 itojun return 0;
800 1.2 itojun }
801 1.2 itojun
802 1.2 itojun void
803 1.2 itojun prelist_remove(pr)
804 1.2 itojun struct nd_prefix *pr;
805 1.2 itojun {
806 1.2 itojun struct nd_pfxrouter *pfr, *next;
807 1.2 itojun int s;
808 1.2 itojun
809 1.4 itojun s = splsoftnet();
810 1.2 itojun /* unlink ndpr_entry from nd_prefix list */
811 1.2 itojun LIST_REMOVE(pr, ndpr_entry);
812 1.2 itojun
813 1.2 itojun /* free list of routers that adversed the prefix */
814 1.2 itojun for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
815 1.2 itojun next = pfr->pfr_next;
816 1.2 itojun
817 1.2 itojun free(pfr, M_IP6NDP);
818 1.2 itojun }
819 1.21 itojun splx(s);
820 1.21 itojun
821 1.2 itojun free(pr, M_IP6NDP);
822 1.2 itojun
823 1.2 itojun pfxlist_onlink_check();
824 1.2 itojun }
825 1.2 itojun
826 1.2 itojun /*
827 1.2 itojun * NOTE: We set address lifetime to keep
828 1.2 itojun * address lifetime <= prefix lifetime
829 1.2 itojun * invariant. This is to simplify on-link determination code.
830 1.2 itojun * If onlink determination is udated, this routine may have to be updated too.
831 1.2 itojun */
832 1.2 itojun int
833 1.2 itojun prelist_update(new, dr, m)
834 1.2 itojun struct nd_prefix *new;
835 1.2 itojun struct nd_defrouter *dr; /* may be NULL */
836 1.2 itojun struct mbuf *m;
837 1.2 itojun {
838 1.2 itojun struct in6_ifaddr *ia6 = NULL;
839 1.2 itojun struct nd_prefix *pr;
840 1.4 itojun int s = splsoftnet();
841 1.2 itojun int error = 0;
842 1.2 itojun int auth;
843 1.2 itojun struct in6_addrlifetime *lt6;
844 1.2 itojun
845 1.7 itojun auth = 0;
846 1.2 itojun if (m) {
847 1.2 itojun /*
848 1.2 itojun * Authenticity for NA consists authentication for
849 1.2 itojun * both IP header and IP datagrams, doesn't it ?
850 1.2 itojun */
851 1.7 itojun #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
852 1.2 itojun auth = (m->m_flags & M_AUTHIPHDR
853 1.2 itojun && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
854 1.7 itojun #endif
855 1.7 itojun }
856 1.2 itojun
857 1.2 itojun if ((pr = prefix_lookup(new)) != NULL) {
858 1.2 itojun if (pr->ndpr_ifp != new->ndpr_ifp) {
859 1.2 itojun error = EADDRNOTAVAIL;
860 1.2 itojun goto end;
861 1.2 itojun }
862 1.2 itojun /* update prefix information */
863 1.7 itojun pr->ndpr_flags = new->ndpr_flags;
864 1.2 itojun pr->ndpr_vltime = new->ndpr_vltime;
865 1.2 itojun pr->ndpr_pltime = new->ndpr_pltime;
866 1.2 itojun pr->ndpr_preferred = new->ndpr_preferred;
867 1.2 itojun pr->ndpr_expire = new->ndpr_expire;
868 1.2 itojun
869 1.2 itojun /*
870 1.2 itojun * RFC 2462 5.5.3 (d) or (e)
871 1.2 itojun * We got a prefix which we have seen in the past.
872 1.2 itojun */
873 1.2 itojun if (!new->ndpr_raf_auto)
874 1.2 itojun goto noautoconf1;
875 1.2 itojun
876 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
877 1.2 itojun ia6 = NULL;
878 1.2 itojun else
879 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
880 1.2 itojun
881 1.2 itojun if (ia6 == NULL) {
882 1.2 itojun /*
883 1.2 itojun * Special case:
884 1.2 itojun * (1) We have seen the prefix advertised before, but
885 1.2 itojun * we have never performed autoconfig for this prefix.
886 1.2 itojun * This is because Autonomous bit was 0 previously, or
887 1.2 itojun * autoconfig failed due to some other reasons.
888 1.2 itojun * (2) We have seen the prefix advertised before and
889 1.2 itojun * we have performed autoconfig in the past, but
890 1.2 itojun * we seem to have no interface address right now.
891 1.2 itojun * This is because the interface address have expired.
892 1.2 itojun *
893 1.2 itojun * This prefix is fresh, with respect to autoconfig
894 1.2 itojun * process.
895 1.2 itojun *
896 1.2 itojun * Add an address based on RFC 2462 5.5.3 (d).
897 1.2 itojun */
898 1.2 itojun ia6 = in6_ifadd(pr->ndpr_ifp,
899 1.2 itojun &pr->ndpr_prefix.sin6_addr, &pr->ndpr_addr,
900 1.2 itojun new->ndpr_plen);
901 1.2 itojun if (!ia6) {
902 1.2 itojun error = EADDRNOTAVAIL;
903 1.22 itojun nd6log((LOG_ERR,
904 1.22 itojun "prelist_update: failed to add a "
905 1.22 itojun "new address\n"));
906 1.2 itojun goto noautoconf1;
907 1.2 itojun }
908 1.2 itojun
909 1.2 itojun lt6 = &ia6->ia6_lifetime;
910 1.2 itojun
911 1.2 itojun /* address lifetime <= prefix lifetime */
912 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
913 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
914 1.7 itojun in6_init_address_ltimes(new, lt6, 1);
915 1.2 itojun } else {
916 1.2 itojun #define TWOHOUR (120*60)
917 1.2 itojun /*
918 1.2 itojun * We have seen the prefix before, and we have added
919 1.2 itojun * interface address in the past. We still have
920 1.2 itojun * the interface address assigned.
921 1.2 itojun *
922 1.2 itojun * update address lifetime based on RFC 2462
923 1.2 itojun * 5.5.3 (e).
924 1.2 itojun */
925 1.2 itojun int update = 0;
926 1.2 itojun
927 1.2 itojun lt6 = &ia6->ia6_lifetime;
928 1.2 itojun
929 1.2 itojun #if 0 /* RFC 2462 5.5.3 (e) */
930 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
931 1.2 itojun if (TWOHOUR < new->ndpr_vltime
932 1.2 itojun || lt6pr->nd < new->ndpr_vltime) {
933 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
934 1.2 itojun update++;
935 1.2 itojun } else if (auth
936 1.2 itojun && lt6->ia6t_vltime <= TWOHOUR0
937 1.2 itojun && new->ndpr_vltime <= lt6->ia6t_vltime) {
938 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
939 1.2 itojun update++;
940 1.2 itojun } else {
941 1.2 itojun lt6->ia6t_vltime = TWOHOUR;
942 1.2 itojun update++;
943 1.2 itojun }
944 1.2 itojun
945 1.7 itojun /* 2 hour rule is not imposed for pref lifetime */
946 1.7 itojun new->ndpr_apltime = new->ndpr_pltime;
947 1.7 itojun lt6->ia6t_pltime = new->ndpr_pltime;
948 1.2 itojun #else /* update from Jim Bound, (ipng 6712) */
949 1.2 itojun if (TWOHOUR < new->ndpr_vltime
950 1.2 itojun || lt6->ia6t_vltime < new->ndpr_vltime) {
951 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
952 1.2 itojun update++;
953 1.2 itojun } else if (auth) {
954 1.7 itojun lt6->ia6t_vltime = new->ndpr_vltime;
955 1.2 itojun update++;
956 1.2 itojun }
957 1.7 itojun
958 1.7 itojun /* jim bound rule is not imposed for pref lifetime */
959 1.7 itojun lt6->ia6t_pltime = new->ndpr_pltime;
960 1.2 itojun #endif
961 1.7 itojun in6_init_address_ltimes(new, lt6, update);
962 1.2 itojun }
963 1.2 itojun
964 1.2 itojun noautoconf1:
965 1.2 itojun
966 1.2 itojun #if 0
967 1.2 itojun /* address lifetime expire processing, RFC 2462 5.5.4. */
968 1.2 itojun if (pr->ndpr_preferred && pr->ndpr_preferred < time_second) {
969 1.2 itojun struct in6_ifaddr *ia6;
970 1.2 itojun
971 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
972 1.2 itojun if (ia6)
973 1.2 itojun ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
974 1.2 itojun }
975 1.2 itojun #endif
976 1.2 itojun
977 1.2 itojun if (dr && pfxrtr_lookup(pr, dr) == NULL)
978 1.2 itojun pfxrtr_add(pr, dr);
979 1.2 itojun } else {
980 1.2 itojun int error_tmp;
981 1.2 itojun
982 1.2 itojun if (new->ndpr_vltime == 0) goto end;
983 1.2 itojun
984 1.2 itojun bzero(&new->ndpr_addr, sizeof(struct in6_addr));
985 1.2 itojun
986 1.2 itojun /*
987 1.2 itojun * RFC 2462 5.5.3 (d)
988 1.2 itojun * We got a fresh prefix. Perform some sanity checks
989 1.2 itojun * and add an interface address by appending interface ID
990 1.2 itojun * to the advertised prefix.
991 1.2 itojun */
992 1.2 itojun if (!new->ndpr_raf_auto)
993 1.2 itojun goto noautoconf2;
994 1.2 itojun
995 1.2 itojun ia6 = in6_ifadd(new->ndpr_ifp, &new->ndpr_prefix.sin6_addr,
996 1.2 itojun &new->ndpr_addr, new->ndpr_plen);
997 1.2 itojun if (!ia6) {
998 1.2 itojun error = EADDRNOTAVAIL;
999 1.22 itojun nd6log((LOG_ERR, "prelist_update: "
1000 1.22 itojun "failed to add a new address\n"));
1001 1.2 itojun goto noautoconf2;
1002 1.2 itojun }
1003 1.2 itojun /* set onlink bit if an interface route is configured */
1004 1.2 itojun new->ndpr_statef_onlink = (ia6->ia_flags & IFA_ROUTE) ? 1 : 0;
1005 1.2 itojun
1006 1.2 itojun lt6 = &ia6->ia6_lifetime;
1007 1.2 itojun
1008 1.2 itojun /* address lifetime <= prefix lifetime */
1009 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
1010 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
1011 1.7 itojun in6_init_address_ltimes(new, lt6, 1);
1012 1.2 itojun
1013 1.2 itojun noautoconf2:
1014 1.2 itojun error_tmp = prelist_add(new, dr);
1015 1.2 itojun error = error_tmp ? error_tmp : error;
1016 1.2 itojun }
1017 1.2 itojun
1018 1.2 itojun end:
1019 1.2 itojun splx(s);
1020 1.2 itojun return error;
1021 1.2 itojun }
1022 1.2 itojun
1023 1.2 itojun /*
1024 1.7 itojun * A supplement function used in the on-link detection below;
1025 1.7 itojun * detect if a given prefix has a (probably) reachable advertising router.
1026 1.7 itojun * XXX: lengthy function name...
1027 1.7 itojun */
1028 1.7 itojun static struct nd_pfxrouter *
1029 1.7 itojun find_pfxlist_reachable_router(pr)
1030 1.7 itojun struct nd_prefix *pr;
1031 1.7 itojun {
1032 1.7 itojun struct nd_pfxrouter *pfxrtr;
1033 1.7 itojun struct rtentry *rt;
1034 1.7 itojun struct llinfo_nd6 *ln;
1035 1.7 itojun
1036 1.7 itojun for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1037 1.7 itojun pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1038 1.7 itojun if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1039 1.7 itojun pfxrtr->router->ifp)) &&
1040 1.7 itojun (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1041 1.7 itojun ND6_IS_LLINFO_PROBREACH(ln))
1042 1.7 itojun break; /* found */
1043 1.7 itojun }
1044 1.7 itojun
1045 1.7 itojun return(pfxrtr);
1046 1.7 itojun
1047 1.7 itojun }
1048 1.7 itojun
1049 1.7 itojun /*
1050 1.2 itojun * Check if each prefix in the prefix list has at least one available router
1051 1.25 itojun * that advertised the prefix (a router is "available" if its neighbor cache
1052 1.25 itojun * entry is reachable or probably reachable).
1053 1.7 itojun * If the check fails, the prefix may be off-link, because, for example,
1054 1.2 itojun * we have moved from the network but the lifetime of the prefix has not
1055 1.25 itojun * expired yet. So we should not use the prefix if there is another prefix
1056 1.25 itojun * that has an available router.
1057 1.25 itojun * But, if there is no prefix that has an available router, we still regards
1058 1.25 itojun * all the prefixes as on-link. This is because we can't tell if all the
1059 1.2 itojun * routers are simply dead or if we really moved from the network and there
1060 1.2 itojun * is no router around us.
1061 1.2 itojun */
1062 1.7 itojun void
1063 1.2 itojun pfxlist_onlink_check()
1064 1.2 itojun {
1065 1.2 itojun struct nd_prefix *pr;
1066 1.2 itojun
1067 1.7 itojun /*
1068 1.7 itojun * Check if there is a prefix that has a reachable advertising
1069 1.7 itojun * router.
1070 1.7 itojun */
1071 1.7 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1072 1.7 itojun if (find_pfxlist_reachable_router(pr))
1073 1.2 itojun break;
1074 1.7 itojun }
1075 1.2 itojun
1076 1.2 itojun if (pr) {
1077 1.2 itojun /*
1078 1.7 itojun * There is at least one prefix that has a reachable router.
1079 1.7 itojun * First, detach prefixes which has no reachable advertising
1080 1.7 itojun * router and then attach other prefixes.
1081 1.7 itojun * The order is important since an attached prefix and a
1082 1.7 itojun * detached prefix may have a same interface route.
1083 1.2 itojun */
1084 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1085 1.7 itojun if (find_pfxlist_reachable_router(pr) == NULL &&
1086 1.2 itojun pr->ndpr_statef_onlink) {
1087 1.2 itojun pr->ndpr_statef_onlink = 0;
1088 1.2 itojun nd6_detach_prefix(pr);
1089 1.2 itojun }
1090 1.2 itojun }
1091 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1092 1.7 itojun if (find_pfxlist_reachable_router(pr) &&
1093 1.7 itojun pr->ndpr_statef_onlink == 0)
1094 1.2 itojun nd6_attach_prefix(pr);
1095 1.2 itojun }
1096 1.2 itojun }
1097 1.2 itojun else {
1098 1.7 itojun /* there is no prefix that has a reachable router */
1099 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next)
1100 1.2 itojun if (pr->ndpr_statef_onlink == 0)
1101 1.2 itojun nd6_attach_prefix(pr);
1102 1.2 itojun }
1103 1.2 itojun }
1104 1.2 itojun
1105 1.2 itojun static void
1106 1.2 itojun nd6_detach_prefix(pr)
1107 1.2 itojun struct nd_prefix *pr;
1108 1.2 itojun {
1109 1.2 itojun struct in6_ifaddr *ia6;
1110 1.2 itojun struct sockaddr_in6 sa6, mask6;
1111 1.2 itojun
1112 1.2 itojun /*
1113 1.2 itojun * Delete the interface route associated with the prefix.
1114 1.2 itojun */
1115 1.2 itojun bzero(&sa6, sizeof(sa6));
1116 1.2 itojun sa6.sin6_family = AF_INET6;
1117 1.2 itojun sa6.sin6_len = sizeof(sa6);
1118 1.2 itojun bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1119 1.2 itojun sizeof(struct in6_addr));
1120 1.2 itojun bzero(&mask6, sizeof(mask6));
1121 1.2 itojun mask6.sin6_family = AF_INET6;
1122 1.2 itojun mask6.sin6_len = sizeof(sa6);
1123 1.2 itojun bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1124 1.2 itojun {
1125 1.2 itojun int e;
1126 1.2 itojun
1127 1.2 itojun e = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1128 1.2 itojun (struct sockaddr *)&mask6, 0, NULL);
1129 1.2 itojun if (e) {
1130 1.22 itojun nd6log((LOG_ERR,
1131 1.2 itojun "nd6_detach_prefix: failed to delete route: "
1132 1.2 itojun "%s/%d (errno = %d)\n",
1133 1.2 itojun ip6_sprintf(&sa6.sin6_addr),
1134 1.7 itojun pr->ndpr_plen,
1135 1.22 itojun e));
1136 1.2 itojun }
1137 1.2 itojun }
1138 1.2 itojun
1139 1.2 itojun /*
1140 1.2 itojun * Mark the address derived from the prefix detached so that
1141 1.2 itojun * it won't be used as a source address for a new connection.
1142 1.2 itojun */
1143 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1144 1.2 itojun ia6 = NULL;
1145 1.2 itojun else
1146 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
1147 1.2 itojun if (ia6)
1148 1.2 itojun ia6->ia6_flags |= IN6_IFF_DETACHED;
1149 1.2 itojun }
1150 1.2 itojun
1151 1.2 itojun static void
1152 1.2 itojun nd6_attach_prefix(pr)
1153 1.2 itojun struct nd_prefix *pr;
1154 1.2 itojun {
1155 1.2 itojun struct ifaddr *ifa;
1156 1.2 itojun struct in6_ifaddr *ia6;
1157 1.2 itojun
1158 1.2 itojun /*
1159 1.2 itojun * Add the interface route associated with the prefix(if necessary)
1160 1.2 itojun * Should we consider if the L bit is set in pr->ndpr_flags?
1161 1.2 itojun */
1162 1.2 itojun ifa = ifaof_ifpforaddr((struct sockaddr *)&pr->ndpr_prefix,
1163 1.2 itojun pr->ndpr_ifp);
1164 1.2 itojun if (ifa == NULL) {
1165 1.22 itojun nd6log((LOG_ERR,
1166 1.2 itojun "nd6_attach_prefix: failed to find any ifaddr"
1167 1.2 itojun " to add route for a prefix(%s/%d)\n",
1168 1.22 itojun ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen));
1169 1.2 itojun }
1170 1.2 itojun else {
1171 1.2 itojun int e;
1172 1.2 itojun struct sockaddr_in6 mask6;
1173 1.2 itojun
1174 1.2 itojun bzero(&mask6, sizeof(mask6));
1175 1.2 itojun mask6.sin6_family = AF_INET6;
1176 1.2 itojun mask6.sin6_len = sizeof(mask6);
1177 1.2 itojun mask6.sin6_addr = pr->ndpr_mask;
1178 1.2 itojun e = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1179 1.2 itojun ifa->ifa_addr, (struct sockaddr *)&mask6,
1180 1.2 itojun ifa->ifa_flags, NULL);
1181 1.2 itojun if (e == 0)
1182 1.2 itojun pr->ndpr_statef_onlink = 1;
1183 1.2 itojun else {
1184 1.22 itojun nd6log((LOG_DEBUG,
1185 1.2 itojun "nd6_attach_prefix: failed to add route for"
1186 1.2 itojun " a prefix(%s/%d), errno = %d\n",
1187 1.22 itojun ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen, e));
1188 1.2 itojun }
1189 1.2 itojun }
1190 1.2 itojun
1191 1.2 itojun /*
1192 1.2 itojun * Now the address derived from the prefix can be used as a source
1193 1.2 itojun * for a new connection, so clear the detached flag.
1194 1.2 itojun */
1195 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1196 1.2 itojun ia6 = NULL;
1197 1.2 itojun else
1198 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
1199 1.2 itojun if (ia6) {
1200 1.2 itojun ia6->ia6_flags &= ~IN6_IFF_DETACHED;
1201 1.2 itojun if (pr->ndpr_statef_onlink)
1202 1.2 itojun ia6->ia_flags |= IFA_ROUTE;
1203 1.2 itojun }
1204 1.2 itojun }
1205 1.2 itojun
1206 1.2 itojun static struct in6_ifaddr *
1207 1.2 itojun in6_ifadd(ifp, in6, addr, prefixlen)
1208 1.2 itojun struct ifnet *ifp;
1209 1.2 itojun struct in6_addr *in6;
1210 1.2 itojun struct in6_addr *addr;
1211 1.2 itojun int prefixlen; /* prefix len of the new prefix in "in6" */
1212 1.2 itojun {
1213 1.2 itojun struct ifaddr *ifa;
1214 1.2 itojun struct in6_ifaddr *ia, *ib, *oia;
1215 1.2 itojun int s, error;
1216 1.2 itojun struct in6_addr mask;
1217 1.2 itojun
1218 1.2 itojun in6_len2mask(&mask, prefixlen);
1219 1.2 itojun
1220 1.2 itojun /* find link-local address (will be interface ID) */
1221 1.12 itojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */
1222 1.2 itojun if (ifa)
1223 1.2 itojun ib = (struct in6_ifaddr *)ifa;
1224 1.2 itojun else
1225 1.2 itojun return NULL;
1226 1.2 itojun
1227 1.2 itojun #if 0 /* don't care link local addr state, and always do DAD */
1228 1.2 itojun /* if link-local address is not eligible, do not autoconfigure. */
1229 1.2 itojun if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1230 1.2 itojun printf("in6_ifadd: link-local address not ready\n");
1231 1.2 itojun return NULL;
1232 1.2 itojun }
1233 1.2 itojun #endif
1234 1.2 itojun
1235 1.2 itojun /* prefixlen + ifidlen must be equal to 128 */
1236 1.2 itojun if (prefixlen != in6_mask2len(&ib->ia_prefixmask.sin6_addr)) {
1237 1.22 itojun nd6log((LOG_ERR, "in6_ifadd: wrong prefixlen for %s"
1238 1.7 itojun "(prefix=%d ifid=%d)\n", if_name(ifp),
1239 1.2 itojun prefixlen,
1240 1.22 itojun 128 - in6_mask2len(&ib->ia_prefixmask.sin6_addr)));
1241 1.2 itojun return NULL;
1242 1.2 itojun }
1243 1.2 itojun
1244 1.2 itojun /* make ifaddr */
1245 1.2 itojun ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_DONTWAIT);
1246 1.2 itojun if (ia == NULL) {
1247 1.2 itojun printf("ENOBUFS in in6_ifadd %d\n", __LINE__);
1248 1.2 itojun return NULL;
1249 1.2 itojun }
1250 1.2 itojun
1251 1.2 itojun bzero((caddr_t)ia, sizeof(*ia));
1252 1.2 itojun ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1253 1.13 itojun if (ifp->if_flags & IFF_POINTOPOINT)
1254 1.13 itojun ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1255 1.13 itojun else
1256 1.13 itojun ia->ia_ifa.ifa_dstaddr = NULL;
1257 1.2 itojun ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1258 1.2 itojun ia->ia_ifp = ifp;
1259 1.2 itojun
1260 1.2 itojun /* link to in6_ifaddr */
1261 1.2 itojun if ((oia = in6_ifaddr) != NULL) {
1262 1.2 itojun for( ; oia->ia_next; oia = oia->ia_next)
1263 1.2 itojun continue;
1264 1.2 itojun oia->ia_next = ia;
1265 1.17 itojun } else {
1266 1.17 itojun /*
1267 1.17 itojun * This should be impossible, since we have at least one
1268 1.17 itojun * link-local address (see the beginning of this function).
1269 1.17 itojun * XXX: should we rather panic here?
1270 1.17 itojun */
1271 1.17 itojun printf("in6_ifadd: in6_ifaddr is NULL (impossible!)\n");
1272 1.2 itojun in6_ifaddr = ia;
1273 1.17 itojun }
1274 1.16 itojun /* gain a refcnt for the link from in6_ifaddr */
1275 1.9 thorpej IFAREF((struct ifaddr *)ia);
1276 1.2 itojun
1277 1.2 itojun /* link to if_addrlist */
1278 1.16 itojun TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
1279 1.16 itojun /* gain another refcnt for the link from if_addrlist */
1280 1.16 itojun IFAREF((struct ifaddr *)ia);
1281 1.2 itojun
1282 1.2 itojun /* new address */
1283 1.2 itojun ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
1284 1.2 itojun ia->ia_addr.sin6_family = AF_INET6;
1285 1.2 itojun /* prefix */
1286 1.2 itojun bcopy(in6, &ia->ia_addr.sin6_addr, sizeof(ia->ia_addr.sin6_addr));
1287 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1288 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1289 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1290 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1291 1.2 itojun /* interface ID */
1292 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[0]
1293 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1294 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[1]
1295 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1296 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[2]
1297 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1298 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[3]
1299 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1300 1.2 itojun
1301 1.2 itojun /* new prefix */
1302 1.2 itojun ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1303 1.2 itojun ia->ia_prefixmask.sin6_family = AF_INET6;
1304 1.2 itojun bcopy(&mask, &ia->ia_prefixmask.sin6_addr,
1305 1.2 itojun sizeof(ia->ia_prefixmask.sin6_addr));
1306 1.2 itojun
1307 1.2 itojun /* same routine */
1308 1.2 itojun ia->ia_ifa.ifa_rtrequest =
1309 1.2 itojun (ifp->if_type == IFT_PPP) ? nd6_p2p_rtrequest : nd6_rtrequest;
1310 1.2 itojun ia->ia_ifa.ifa_flags |= RTF_CLONING;
1311 1.2 itojun ia->ia_ifa.ifa_metric = ifp->if_metric;
1312 1.2 itojun
1313 1.2 itojun /* add interface route */
1314 1.2 itojun if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP|RTF_CLONING))) {
1315 1.22 itojun nd6log((LOG_NOTICE,
1316 1.22 itojun "in6_ifadd: failed to add an interface route "
1317 1.2 itojun "for %s/%d on %s, errno = %d\n",
1318 1.2 itojun ip6_sprintf(&ia->ia_addr.sin6_addr), prefixlen,
1319 1.22 itojun if_name(ifp), error));
1320 1.2 itojun }
1321 1.2 itojun else
1322 1.2 itojun ia->ia_flags |= IFA_ROUTE;
1323 1.2 itojun
1324 1.2 itojun *addr = ia->ia_addr.sin6_addr;
1325 1.2 itojun
1326 1.2 itojun if (ifp->if_flags & IFF_MULTICAST) {
1327 1.2 itojun int error; /* not used */
1328 1.2 itojun struct in6_addr sol6;
1329 1.2 itojun
1330 1.2 itojun /* Restore saved multicast addresses(if any). */
1331 1.2 itojun in6_restoremkludge(ia, ifp);
1332 1.2 itojun
1333 1.2 itojun /* join solicited node multicast address */
1334 1.2 itojun bzero(&sol6, sizeof(sol6));
1335 1.2 itojun sol6.s6_addr16[0] = htons(0xff02);
1336 1.2 itojun sol6.s6_addr16[1] = htons(ifp->if_index);
1337 1.2 itojun sol6.s6_addr32[1] = 0;
1338 1.2 itojun sol6.s6_addr32[2] = htonl(1);
1339 1.2 itojun sol6.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
1340 1.2 itojun sol6.s6_addr8[12] = 0xff;
1341 1.24 itojun if (!in6_addmulti(&sol6, ifp, &error)) {
1342 1.24 itojun nd6log((LOG_ERR, "%s: failed to join %s (errno=%d)\n",
1343 1.24 itojun if_name(ifp), ip6_sprintf(&sol6), error));
1344 1.24 itojun }
1345 1.2 itojun }
1346 1.2 itojun
1347 1.2 itojun ia->ia6_flags |= IN6_IFF_TENTATIVE;
1348 1.2 itojun
1349 1.2 itojun /*
1350 1.2 itojun * To make the interface up. Only AF_INET6 in ia is used...
1351 1.2 itojun */
1352 1.23 thorpej s = splnet();
1353 1.2 itojun if (ifp->if_ioctl && (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) {
1354 1.2 itojun splx(s);
1355 1.2 itojun return NULL;
1356 1.2 itojun }
1357 1.2 itojun splx(s);
1358 1.2 itojun
1359 1.2 itojun /* Perform DAD, if needed. */
1360 1.2 itojun nd6_dad_start((struct ifaddr *)ia, NULL);
1361 1.2 itojun
1362 1.2 itojun return ia;
1363 1.2 itojun }
1364 1.2 itojun
1365 1.2 itojun int
1366 1.2 itojun in6_ifdel(ifp, in6)
1367 1.2 itojun struct ifnet *ifp;
1368 1.2 itojun struct in6_addr *in6;
1369 1.2 itojun {
1370 1.2 itojun struct in6_ifaddr *ia = (struct in6_ifaddr *)NULL;
1371 1.2 itojun struct in6_ifaddr *oia = (struct in6_ifaddr *)NULL;
1372 1.2 itojun
1373 1.2 itojun if (!ifp)
1374 1.2 itojun return -1;
1375 1.2 itojun
1376 1.2 itojun ia = in6ifa_ifpwithaddr(ifp, in6);
1377 1.2 itojun if (!ia)
1378 1.2 itojun return -1;
1379 1.2 itojun
1380 1.2 itojun if (ifp->if_flags & IFF_MULTICAST) {
1381 1.2 itojun /*
1382 1.2 itojun * delete solicited multicast addr for deleting host id
1383 1.2 itojun */
1384 1.2 itojun struct in6_multi *in6m;
1385 1.2 itojun struct in6_addr llsol;
1386 1.2 itojun bzero(&llsol, sizeof(struct in6_addr));
1387 1.2 itojun llsol.s6_addr16[0] = htons(0xff02);
1388 1.2 itojun llsol.s6_addr16[1] = htons(ifp->if_index);
1389 1.2 itojun llsol.s6_addr32[1] = 0;
1390 1.2 itojun llsol.s6_addr32[2] = htonl(1);
1391 1.2 itojun llsol.s6_addr32[3] =
1392 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[3];
1393 1.2 itojun llsol.s6_addr8[12] = 0xff;
1394 1.2 itojun
1395 1.2 itojun IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1396 1.2 itojun if (in6m)
1397 1.2 itojun in6_delmulti(in6m);
1398 1.2 itojun }
1399 1.2 itojun
1400 1.2 itojun if (ia->ia_flags & IFA_ROUTE) {
1401 1.2 itojun rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1402 1.2 itojun ia->ia_flags &= ~IFA_ROUTE;
1403 1.2 itojun }
1404 1.2 itojun
1405 1.2 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
1406 1.12 itojun IFAFREE(&ia->ia_ifa);
1407 1.2 itojun
1408 1.2 itojun /* lladdr is never deleted */
1409 1.2 itojun oia = ia;
1410 1.2 itojun if (oia == (ia = in6_ifaddr))
1411 1.2 itojun in6_ifaddr = ia->ia_next;
1412 1.2 itojun else {
1413 1.2 itojun while (ia->ia_next && (ia->ia_next != oia))
1414 1.2 itojun ia = ia->ia_next;
1415 1.2 itojun if (ia->ia_next)
1416 1.2 itojun ia->ia_next = oia->ia_next;
1417 1.2 itojun else
1418 1.2 itojun return -1;
1419 1.2 itojun }
1420 1.2 itojun
1421 1.2 itojun in6_savemkludge(oia);
1422 1.2 itojun IFAFREE((&oia->ia_ifa));
1423 1.2 itojun /* xxx
1424 1.2 itojun rtrequest(RTM_DELETE,
1425 1.2 itojun (struct sockaddr *)&ia->ia_addr,
1426 1.2 itojun (struct sockaddr *)0
1427 1.2 itojun (struct sockaddr *)&ia->ia_prefixmask,
1428 1.2 itojun RTF_UP|RTF_CLONING,
1429 1.2 itojun (struct rtentry **)0);
1430 1.2 itojun */
1431 1.2 itojun return 0;
1432 1.2 itojun }
1433 1.2 itojun
1434 1.2 itojun int
1435 1.2 itojun in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1436 1.2 itojun {
1437 1.7 itojun long time_second = time.tv_sec;
1438 1.7 itojun
1439 1.26 itojun /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
1440 1.2 itojun if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1441 1.22 itojun nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1442 1.2 itojun "(%d) is greater than valid lifetime(%d)\n",
1443 1.22 itojun (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
1444 1.2 itojun return (EINVAL);
1445 1.2 itojun }
1446 1.7 itojun if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1447 1.2 itojun ndpr->ndpr_preferred = 0;
1448 1.2 itojun else
1449 1.2 itojun ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
1450 1.7 itojun if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1451 1.2 itojun ndpr->ndpr_expire = 0;
1452 1.2 itojun else
1453 1.2 itojun ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
1454 1.2 itojun
1455 1.2 itojun return 0;
1456 1.2 itojun }
1457 1.2 itojun
1458 1.2 itojun static void
1459 1.7 itojun in6_init_address_ltimes(struct nd_prefix *new,
1460 1.7 itojun struct in6_addrlifetime *lt6,
1461 1.7 itojun int update_vltime)
1462 1.2 itojun {
1463 1.7 itojun long time_second = time.tv_sec;
1464 1.7 itojun
1465 1.7 itojun /* Valid lifetime must not be updated unless explicitly specified. */
1466 1.7 itojun if (update_vltime) {
1467 1.7 itojun /* init ia6t_expire */
1468 1.7 itojun if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1469 1.7 itojun lt6->ia6t_expire = 0;
1470 1.7 itojun else {
1471 1.7 itojun lt6->ia6t_expire = time_second;
1472 1.7 itojun lt6->ia6t_expire += lt6->ia6t_vltime;
1473 1.7 itojun }
1474 1.7 itojun /* Ensure addr lifetime <= prefix lifetime. */
1475 1.7 itojun if (new->ndpr_expire && lt6->ia6t_expire &&
1476 1.7 itojun new->ndpr_expire < lt6->ia6t_expire)
1477 1.7 itojun lt6->ia6t_expire = new->ndpr_expire;
1478 1.2 itojun }
1479 1.7 itojun
1480 1.2 itojun /* init ia6t_preferred */
1481 1.7 itojun if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1482 1.2 itojun lt6->ia6t_preferred = 0;
1483 1.2 itojun else {
1484 1.2 itojun lt6->ia6t_preferred = time_second;
1485 1.2 itojun lt6->ia6t_preferred += lt6->ia6t_pltime;
1486 1.2 itojun }
1487 1.7 itojun /* Ensure addr lifetime <= prefix lifetime. */
1488 1.2 itojun if (new->ndpr_preferred && lt6->ia6t_preferred
1489 1.2 itojun && new->ndpr_preferred < lt6->ia6t_preferred)
1490 1.2 itojun lt6->ia6t_preferred = new->ndpr_preferred;
1491 1.2 itojun }
1492 1.2 itojun
1493 1.2 itojun /*
1494 1.2 itojun * Delete all the routing table entries that use the specified gateway.
1495 1.2 itojun * XXX: this function causes search through all entries of routing table, so
1496 1.16 itojun * it shouldn't be called when acting as a router.
1497 1.2 itojun */
1498 1.2 itojun void
1499 1.2 itojun rt6_flush(gateway, ifp)
1500 1.26 itojun struct in6_addr *gateway;
1501 1.26 itojun struct ifnet *ifp;
1502 1.2 itojun {
1503 1.2 itojun struct radix_node_head *rnh = rt_tables[AF_INET6];
1504 1.4 itojun int s = splsoftnet();
1505 1.2 itojun
1506 1.2 itojun /* We'll care only link-local addresses */
1507 1.2 itojun if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1508 1.2 itojun splx(s);
1509 1.2 itojun return;
1510 1.2 itojun }
1511 1.2 itojun /* XXX: hack for KAME's link-local address kludge */
1512 1.2 itojun gateway->s6_addr16[1] = htons(ifp->if_index);
1513 1.2 itojun
1514 1.2 itojun rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1515 1.2 itojun splx(s);
1516 1.2 itojun }
1517 1.2 itojun
1518 1.2 itojun static int
1519 1.2 itojun rt6_deleteroute(rn, arg)
1520 1.2 itojun struct radix_node *rn;
1521 1.2 itojun void *arg;
1522 1.2 itojun {
1523 1.2 itojun #define SIN6(s) ((struct sockaddr_in6 *)s)
1524 1.2 itojun struct rtentry *rt = (struct rtentry *)rn;
1525 1.2 itojun struct in6_addr *gate = (struct in6_addr *)arg;
1526 1.2 itojun
1527 1.2 itojun if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1528 1.2 itojun return(0);
1529 1.2 itojun
1530 1.2 itojun if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1531 1.2 itojun return(0);
1532 1.2 itojun
1533 1.2 itojun /*
1534 1.26 itojun * Do not delete a static route.
1535 1.26 itojun * XXX: this seems to be a bit ad-hoc. Should we consider the
1536 1.26 itojun * 'cloned' bit instead?
1537 1.26 itojun */
1538 1.26 itojun if ((rt->rt_flags & RTF_STATIC) != 0)
1539 1.26 itojun return(0);
1540 1.26 itojun
1541 1.26 itojun /*
1542 1.2 itojun * We delete only host route. This means, in particular, we don't
1543 1.2 itojun * delete default route.
1544 1.2 itojun */
1545 1.2 itojun if ((rt->rt_flags & RTF_HOST) == 0)
1546 1.2 itojun return(0);
1547 1.2 itojun
1548 1.2 itojun return(rtrequest(RTM_DELETE, rt_key(rt),
1549 1.2 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0));
1550 1.2 itojun #undef SIN6
1551 1.2 itojun }
1552 1.7 itojun
1553 1.7 itojun int
1554 1.7 itojun nd6_setdefaultiface(ifindex)
1555 1.7 itojun int ifindex;
1556 1.7 itojun {
1557 1.7 itojun int error = 0;
1558 1.7 itojun
1559 1.7 itojun if (ifindex < 0 || if_index < ifindex)
1560 1.7 itojun return(EINVAL);
1561 1.7 itojun
1562 1.7 itojun if (nd6_defifindex != ifindex) {
1563 1.7 itojun nd6_defifindex = ifindex;
1564 1.26 itojun if (nd6_defifindex > 0) {
1565 1.7 itojun nd6_defifp = ifindex2ifnet[nd6_defifindex];
1566 1.26 itojun } else
1567 1.7 itojun nd6_defifp = NULL;
1568 1.7 itojun
1569 1.7 itojun /*
1570 1.7 itojun * If the Default Router List is empty, install a route
1571 1.7 itojun * to the specified interface as default or remove the default
1572 1.7 itojun * route when the default interface becomes canceled.
1573 1.7 itojun * The check for the queue is actually redundant, but
1574 1.7 itojun * we do this here to avoid re-install the default route
1575 1.7 itojun * if the list is NOT empty.
1576 1.7 itojun */
1577 1.7 itojun if (TAILQ_FIRST(&nd_defrouter) == NULL)
1578 1.7 itojun defrouter_select();
1579 1.7 itojun }
1580 1.7 itojun
1581 1.7 itojun return(error);
1582 1.7 itojun }
1583