]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.20 2002/08/02 20:49:14 rwatson Exp $ */ |
9bccf70c | 2 | /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */ |
1c79356b A |
3 | |
4 | /* | |
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the name of the project nor the names of its contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | /* | |
34 | * XXX | |
35 | * KAME 970409 note: | |
36 | * BSD/OS version heavily modifies this code, related to llinfo. | |
37 | * Since we don't have BSD/OS version of net/route.c in our hand, | |
38 | * I left the code mostly as it was in 970310. -- itojun | |
39 | */ | |
40 | ||
41 | #include <sys/param.h> | |
42 | #include <sys/systm.h> | |
43 | #include <sys/malloc.h> | |
44 | #include <sys/mbuf.h> | |
45 | #include <sys/socket.h> | |
46 | #include <sys/sockio.h> | |
47 | #include <sys/time.h> | |
48 | #include <sys/kernel.h> | |
2d21ac55 | 49 | #include <sys/sysctl.h> |
1c79356b | 50 | #include <sys/errno.h> |
1c79356b A |
51 | #include <sys/syslog.h> |
52 | #include <sys/protosw.h> | |
53 | #include <kern/queue.h> | |
91447636 | 54 | #include <kern/lock.h> |
1c79356b | 55 | |
9bccf70c | 56 | #define DONT_WARN_OBSOLETE |
1c79356b A |
57 | #include <net/if.h> |
58 | #include <net/if_dl.h> | |
59 | #include <net/if_types.h> | |
1c79356b | 60 | #include <net/if_atm.h> |
1c79356b A |
61 | #include <net/route.h> |
62 | #include <net/dlil.h> | |
63 | ||
64 | #include <netinet/in.h> | |
1c79356b | 65 | #include <netinet/if_ether.h> |
1c79356b | 66 | #include <netinet/if_fddi.h> |
1c79356b A |
67 | #include <netinet6/in6_var.h> |
68 | #include <netinet/ip6.h> | |
69 | #include <netinet6/ip6_var.h> | |
70 | #include <netinet6/nd6.h> | |
71 | #include <netinet6/in6_prefix.h> | |
72 | #include <netinet/icmp6.h> | |
73 | ||
1c79356b | 74 | #include "loop.h" |
1c79356b A |
75 | |
76 | #include <net/net_osdep.h> | |
77 | ||
78 | #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ | |
79 | #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ | |
80 | ||
81 | #define SIN6(s) ((struct sockaddr_in6 *)s) | |
82 | #define SDL(s) ((struct sockaddr_dl *)s) | |
83 | ||
84 | /* timer values */ | |
85 | int nd6_prune = 1; /* walk list every 1 seconds */ | |
86 | int nd6_delay = 5; /* delay first probe time 5 second */ | |
87 | int nd6_umaxtries = 3; /* maximum unicast query */ | |
88 | int nd6_mmaxtries = 3; /* maximum multicast query */ | |
89 | int nd6_useloopback = 1; /* use loopback interface for local traffic */ | |
9bccf70c | 90 | int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */ |
1c79356b A |
91 | |
92 | /* preventing too many loops in ND option parsing */ | |
93 | int nd6_maxndopt = 10; /* max # of ND options allowed */ | |
94 | ||
9bccf70c A |
95 | int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */ |
96 | ||
97 | #if ND6_DEBUG | |
98 | int nd6_debug = 1; | |
99 | #else | |
100 | int nd6_debug = 0; | |
101 | #endif | |
102 | ||
1c79356b A |
103 | /* for debugging? */ |
104 | static int nd6_inuse, nd6_allocated; | |
105 | ||
2d21ac55 | 106 | struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6, NULL, NULL, 0, 0, 0, 0, 0 }; |
4a3eedf9 | 107 | size_t nd_ifinfo_indexlim = 32; /* increased for 5589193 */ |
1c79356b A |
108 | struct nd_ifinfo *nd_ifinfo = NULL; |
109 | struct nd_drhead nd_defrouter; | |
110 | struct nd_prhead nd_prefix = { 0 }; | |
111 | ||
112 | int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL; | |
113 | static struct sockaddr_in6 all1_sa; | |
114 | ||
91447636 A |
115 | static int regen_tmpaddr(struct in6_ifaddr *); |
116 | extern lck_mtx_t *rt_mtx; | |
117 | extern lck_mtx_t *ip6_mutex; | |
118 | extern lck_mtx_t *nd6_mutex; | |
1c79356b | 119 | |
91447636 | 120 | static void nd6_slowtimo(void *ignored_arg); |
1c79356b A |
121 | |
122 | void | |
123 | nd6_init() | |
124 | { | |
125 | static int nd6_init_done = 0; | |
126 | int i; | |
127 | ||
128 | if (nd6_init_done) { | |
129 | log(LOG_NOTICE, "nd6_init called more than once(ignored)\n"); | |
130 | return; | |
131 | } | |
132 | ||
133 | all1_sa.sin6_family = AF_INET6; | |
134 | all1_sa.sin6_len = sizeof(struct sockaddr_in6); | |
135 | for (i = 0; i < sizeof(all1_sa.sin6_addr); i++) | |
136 | all1_sa.sin6_addr.s6_addr[i] = 0xff; | |
137 | ||
138 | /* initialization of the default router list */ | |
139 | TAILQ_INIT(&nd_defrouter); | |
140 | ||
141 | nd6_init_done = 1; | |
142 | ||
143 | /* start timer */ | |
91447636 | 144 | timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz); |
1c79356b A |
145 | } |
146 | ||
147 | void | |
91447636 A |
148 | nd6_ifattach( |
149 | struct ifnet *ifp) | |
1c79356b | 150 | { |
1c79356b A |
151 | |
152 | /* | |
153 | * We have some arrays that should be indexed by if_index. | |
154 | * since if_index will grow dynamically, they should grow too. | |
155 | */ | |
9bccf70c | 156 | if (nd_ifinfo == NULL || if_index >= nd_ifinfo_indexlim) { |
1c79356b A |
157 | size_t n; |
158 | caddr_t q; | |
159 | ||
9bccf70c A |
160 | while (if_index >= nd_ifinfo_indexlim) |
161 | nd_ifinfo_indexlim <<= 1; | |
1c79356b A |
162 | |
163 | /* grow nd_ifinfo */ | |
9bccf70c | 164 | n = nd_ifinfo_indexlim * sizeof(struct nd_ifinfo); |
1c79356b A |
165 | q = (caddr_t)_MALLOC(n, M_IP6NDP, M_WAITOK); |
166 | bzero(q, n); | |
167 | if (nd_ifinfo) { | |
168 | bcopy((caddr_t)nd_ifinfo, q, n/2); | |
4a3eedf9 A |
169 | /* Radar 5589193: |
170 | * SU fix purposely leaks the old nd_ifinfo array | |
171 | * if we grow the arraw to more than 32 interfaces | |
172 | * Fix for future release is to use proper locking. | |
173 | ||
9bccf70c | 174 | FREE((caddr_t)nd_ifinfo, M_IP6NDP); |
4a3eedf9 | 175 | */ |
1c79356b A |
176 | } |
177 | nd_ifinfo = (struct nd_ifinfo *)q; | |
178 | } | |
179 | ||
180 | #define ND nd_ifinfo[ifp->if_index] | |
9bccf70c A |
181 | |
182 | /* | |
183 | * Don't initialize if called twice. | |
184 | * XXX: to detect this, we should choose a member that is never set | |
185 | * before initialization of the ND structure itself. We formaly used | |
186 | * the linkmtu member, which was not suitable because it could be | |
187 | * initialized via "ifconfig mtu". | |
188 | */ | |
189 | if (ND.basereachable) | |
190 | return; | |
191 | ||
1c79356b A |
192 | ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu; |
193 | ND.chlim = IPV6_DEFHLIM; | |
194 | ND.basereachable = REACHABLE_TIME; | |
195 | ND.reachable = ND_COMPUTE_RTIME(ND.basereachable); | |
196 | ND.retrans = RETRANS_TIMER; | |
197 | ND.receivedra = 0; | |
198 | ND.flags = ND6_IFF_PERFORMNUD; | |
199 | nd6_setmtu(ifp); | |
200 | #undef ND | |
201 | } | |
202 | ||
203 | /* | |
204 | * Reset ND level link MTU. This function is called when the physical MTU | |
205 | * changes, which means we might have to adjust the ND level MTU. | |
206 | */ | |
207 | void | |
2d21ac55 | 208 | nd6_setmtu(struct ifnet *ifp) |
1c79356b | 209 | { |
55e303ae | 210 | struct nd_ifinfo *ndi; |
2d21ac55 | 211 | u_long oldmaxmtu; |
55e303ae A |
212 | |
213 | /* | |
214 | * Make sure IPv6 is enabled for the interface first, | |
215 | * because this can be called directly from SIOCSIFMTU for IPv4 | |
216 | */ | |
217 | ||
218 | if (ifp->if_index >= nd_ifinfo_indexlim) { | |
55e303ae A |
219 | return; /* we're out of bound for nd_ifinfo */ |
220 | } | |
221 | ||
222 | ndi = &nd_ifinfo[ifp->if_index]; | |
223 | oldmaxmtu = ndi->maxmtu; | |
1c79356b | 224 | |
2d21ac55 A |
225 | /* |
226 | * The ND level maxmtu is somewhat redundant to the interface MTU | |
227 | * and is an implementation artifact of KAME. Instead of hard- | |
228 | * limiting the maxmtu based on the interface type here, we simply | |
229 | * take the if_mtu value since SIOCSIFMTU would have taken care of | |
230 | * the sanity checks related to the maximum MTU allowed for the | |
231 | * interface (a value that is known only by the interface layer), | |
232 | * by sending the request down via ifnet_ioctl(). The use of the | |
233 | * ND level maxmtu and linkmtu (the latter obtained via RA) are done | |
234 | * via IN6_LINKMTU() which does further checking against if_mtu. | |
235 | */ | |
236 | ndi->maxmtu = ifp->if_mtu; | |
1c79356b | 237 | |
2d21ac55 A |
238 | /* |
239 | * Decreasing the interface MTU under IPV6 minimum MTU may cause | |
240 | * undesirable situation. We thus notify the operator of the change | |
241 | * explicitly. The check for oldmaxmtu is necessary to restrict the | |
242 | * log to the case of changing the MTU, not initializing it. | |
243 | */ | |
244 | if (oldmaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) { | |
245 | log(LOG_NOTICE, "nd6_setmtu: " | |
246 | "new link MTU on %s%d (%lu) is too small for IPv6\n", | |
247 | ifp->if_name, ifp->if_unit, (unsigned long)ndi->maxmtu); | |
1c79356b | 248 | } |
2d21ac55 A |
249 | |
250 | /* also adjust in6_maxmtu if necessary. */ | |
251 | if (ndi->maxmtu > in6_maxmtu) | |
252 | in6_setmaxmtu(); | |
1c79356b A |
253 | } |
254 | ||
255 | void | |
91447636 A |
256 | nd6_option_init( |
257 | void *opt, | |
258 | int icmp6len, | |
259 | union nd_opts *ndopts) | |
1c79356b A |
260 | { |
261 | bzero(ndopts, sizeof(*ndopts)); | |
262 | ndopts->nd_opts_search = (struct nd_opt_hdr *)opt; | |
263 | ndopts->nd_opts_last | |
264 | = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len); | |
265 | ||
266 | if (icmp6len == 0) { | |
267 | ndopts->nd_opts_done = 1; | |
268 | ndopts->nd_opts_search = NULL; | |
269 | } | |
270 | } | |
271 | ||
272 | /* | |
273 | * Take one ND option. | |
274 | */ | |
275 | struct nd_opt_hdr * | |
91447636 A |
276 | nd6_option( |
277 | union nd_opts *ndopts) | |
1c79356b A |
278 | { |
279 | struct nd_opt_hdr *nd_opt; | |
280 | int olen; | |
281 | ||
282 | if (!ndopts) | |
283 | panic("ndopts == NULL in nd6_option\n"); | |
284 | if (!ndopts->nd_opts_last) | |
285 | panic("uninitialized ndopts in nd6_option\n"); | |
286 | if (!ndopts->nd_opts_search) | |
287 | return NULL; | |
288 | if (ndopts->nd_opts_done) | |
289 | return NULL; | |
290 | ||
291 | nd_opt = ndopts->nd_opts_search; | |
292 | ||
9bccf70c A |
293 | /* make sure nd_opt_len is inside the buffer */ |
294 | if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) { | |
295 | bzero(ndopts, sizeof(*ndopts)); | |
296 | return NULL; | |
297 | } | |
298 | ||
1c79356b A |
299 | olen = nd_opt->nd_opt_len << 3; |
300 | if (olen == 0) { | |
301 | /* | |
302 | * Message validation requires that all included | |
303 | * options have a length that is greater than zero. | |
304 | */ | |
305 | bzero(ndopts, sizeof(*ndopts)); | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen); | |
9bccf70c A |
310 | if (ndopts->nd_opts_search > ndopts->nd_opts_last) { |
311 | /* option overruns the end of buffer, invalid */ | |
312 | bzero(ndopts, sizeof(*ndopts)); | |
313 | return NULL; | |
314 | } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) { | |
315 | /* reached the end of options chain */ | |
1c79356b A |
316 | ndopts->nd_opts_done = 1; |
317 | ndopts->nd_opts_search = NULL; | |
318 | } | |
319 | return nd_opt; | |
320 | } | |
321 | ||
322 | /* | |
323 | * Parse multiple ND options. | |
324 | * This function is much easier to use, for ND routines that do not need | |
325 | * multiple options of the same type. | |
326 | */ | |
327 | int | |
91447636 A |
328 | nd6_options( |
329 | union nd_opts *ndopts) | |
1c79356b A |
330 | { |
331 | struct nd_opt_hdr *nd_opt; | |
332 | int i = 0; | |
333 | ||
334 | if (!ndopts) | |
335 | panic("ndopts == NULL in nd6_options\n"); | |
336 | if (!ndopts->nd_opts_last) | |
337 | panic("uninitialized ndopts in nd6_options\n"); | |
338 | if (!ndopts->nd_opts_search) | |
339 | return 0; | |
340 | ||
341 | while (1) { | |
342 | nd_opt = nd6_option(ndopts); | |
343 | if (!nd_opt && !ndopts->nd_opts_last) { | |
344 | /* | |
345 | * Message validation requires that all included | |
346 | * options have a length that is greater than zero. | |
347 | */ | |
9bccf70c | 348 | icmp6stat.icp6s_nd_badopt++; |
1c79356b A |
349 | bzero(ndopts, sizeof(*ndopts)); |
350 | return -1; | |
351 | } | |
352 | ||
353 | if (!nd_opt) | |
354 | goto skip1; | |
355 | ||
356 | switch (nd_opt->nd_opt_type) { | |
357 | case ND_OPT_SOURCE_LINKADDR: | |
358 | case ND_OPT_TARGET_LINKADDR: | |
359 | case ND_OPT_MTU: | |
360 | case ND_OPT_REDIRECTED_HEADER: | |
1c79356b | 361 | if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) { |
9bccf70c A |
362 | nd6log((LOG_INFO, |
363 | "duplicated ND6 option found (type=%d)\n", | |
364 | nd_opt->nd_opt_type)); | |
1c79356b A |
365 | /* XXX bark? */ |
366 | } else { | |
367 | ndopts->nd_opt_array[nd_opt->nd_opt_type] | |
368 | = nd_opt; | |
369 | } | |
370 | break; | |
371 | case ND_OPT_PREFIX_INFORMATION: | |
372 | if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) { | |
373 | ndopts->nd_opt_array[nd_opt->nd_opt_type] | |
374 | = nd_opt; | |
375 | } | |
376 | ndopts->nd_opts_pi_end = | |
377 | (struct nd_opt_prefix_info *)nd_opt; | |
378 | break; | |
1c79356b A |
379 | default: |
380 | /* | |
381 | * Unknown options must be silently ignored, | |
382 | * to accomodate future extension to the protocol. | |
383 | */ | |
9bccf70c | 384 | nd6log((LOG_DEBUG, |
1c79356b | 385 | "nd6_options: unsupported option %d - " |
9bccf70c | 386 | "option ignored\n", nd_opt->nd_opt_type)); |
1c79356b A |
387 | } |
388 | ||
389 | skip1: | |
390 | i++; | |
391 | if (i > nd6_maxndopt) { | |
392 | icmp6stat.icp6s_nd_toomanyopt++; | |
9bccf70c | 393 | nd6log((LOG_INFO, "too many loop in nd opt\n")); |
1c79356b A |
394 | break; |
395 | } | |
396 | ||
397 | if (ndopts->nd_opts_done) | |
398 | break; | |
399 | } | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | /* | |
405 | * ND6 timer routine to expire default route list and prefix list | |
406 | */ | |
407 | void | |
91447636 | 408 | nd6_timer( |
2d21ac55 | 409 | __unused void *ignored_arg) |
0b4e3aa0 | 410 | { |
9bccf70c A |
411 | struct llinfo_nd6 *ln; |
412 | struct nd_defrouter *dr; | |
413 | struct nd_prefix *pr; | |
91447636 | 414 | struct ifnet *ifp = NULL; |
9bccf70c A |
415 | struct in6_ifaddr *ia6, *nia6; |
416 | struct in6_addrlifetime *lt6; | |
91447636 A |
417 | struct timeval timenow; |
418 | ||
419 | getmicrotime(&timenow); | |
1c79356b | 420 | |
9bccf70c | 421 | |
1c79356b A |
422 | |
423 | ln = llinfo_nd6.ln_next; | |
1c79356b A |
424 | while (ln && ln != &llinfo_nd6) { |
425 | struct rtentry *rt; | |
1c79356b A |
426 | struct sockaddr_in6 *dst; |
427 | struct llinfo_nd6 *next = ln->ln_next; | |
428 | /* XXX: used for the DELAY case only: */ | |
429 | struct nd_ifinfo *ndi = NULL; | |
430 | ||
431 | if ((rt = ln->ln_rt) == NULL) { | |
432 | ln = next; | |
433 | continue; | |
434 | } | |
435 | if ((ifp = rt->rt_ifp) == NULL) { | |
436 | ln = next; | |
437 | continue; | |
438 | } | |
439 | ndi = &nd_ifinfo[ifp->if_index]; | |
440 | dst = (struct sockaddr_in6 *)rt_key(rt); | |
441 | ||
91447636 | 442 | if (ln->ln_expire > timenow.tv_sec) { |
1c79356b A |
443 | ln = next; |
444 | continue; | |
445 | } | |
55e303ae | 446 | |
1c79356b | 447 | /* sanity check */ |
55e303ae A |
448 | if (!rt) { |
449 | printf("rt=0 in nd6_timer(ln=%p)\n", ln); | |
450 | ln = next; | |
451 | continue; | |
452 | } | |
453 | if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln) { | |
454 | printf("rt_llinfo(%p) is not equal to ln(%p)\n", | |
1c79356b | 455 | rt->rt_llinfo, ln); |
55e303ae A |
456 | ln = next; |
457 | continue; | |
458 | } | |
459 | if (!dst) { | |
460 | printf("dst=0 in nd6_timer(ln=%p)\n", ln); | |
461 | ln = next; | |
462 | continue; | |
463 | } | |
1c79356b A |
464 | |
465 | switch (ln->ln_state) { | |
466 | case ND6_LLINFO_INCOMPLETE: | |
467 | if (ln->ln_asked < nd6_mmaxtries) { | |
468 | ln->ln_asked++; | |
91447636 | 469 | ln->ln_expire = timenow.tv_sec + |
1c79356b A |
470 | nd_ifinfo[ifp->if_index].retrans / 1000; |
471 | nd6_ns_output(ifp, NULL, &dst->sin6_addr, | |
91447636 | 472 | ln, 0, 0); |
1c79356b A |
473 | } else { |
474 | struct mbuf *m = ln->ln_hold; | |
55e303ae | 475 | ln->ln_hold = NULL; |
1c79356b A |
476 | if (m) { |
477 | if (rt->rt_ifp) { | |
478 | /* | |
479 | * Fake rcvif to make ICMP error | |
480 | * more helpful in diagnosing | |
481 | * for the receiver. | |
482 | * XXX: should we consider | |
483 | * older rcvif? | |
484 | */ | |
485 | m->m_pkthdr.rcvif = rt->rt_ifp; | |
486 | } | |
487 | icmp6_error(m, ICMP6_DST_UNREACH, | |
488 | ICMP6_DST_UNREACH_ADDR, 0); | |
489 | ln->ln_hold = NULL; | |
490 | } | |
9bccf70c | 491 | next = nd6_free(rt); |
1c79356b A |
492 | } |
493 | break; | |
494 | case ND6_LLINFO_REACHABLE: | |
9bccf70c | 495 | if (ln->ln_expire) { |
1c79356b | 496 | ln->ln_state = ND6_LLINFO_STALE; |
91447636 | 497 | ln->ln_expire = timenow.tv_sec + nd6_gctimer; |
9bccf70c | 498 | } |
1c79356b | 499 | break; |
9bccf70c A |
500 | |
501 | case ND6_LLINFO_STALE: | |
502 | /* Garbage Collection(RFC 2461 5.3) */ | |
503 | if (ln->ln_expire) | |
504 | next = nd6_free(rt); | |
505 | break; | |
506 | ||
1c79356b A |
507 | case ND6_LLINFO_DELAY: |
508 | if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) { | |
509 | /* We need NUD */ | |
510 | ln->ln_asked = 1; | |
511 | ln->ln_state = ND6_LLINFO_PROBE; | |
91447636 | 512 | ln->ln_expire = timenow.tv_sec + |
1c79356b A |
513 | ndi->retrans / 1000; |
514 | nd6_ns_output(ifp, &dst->sin6_addr, | |
515 | &dst->sin6_addr, | |
91447636 | 516 | ln, 0, 0); |
9bccf70c | 517 | } else { |
1c79356b | 518 | ln->ln_state = ND6_LLINFO_STALE; /* XXX */ |
91447636 | 519 | ln->ln_expire = timenow.tv_sec + nd6_gctimer; |
9bccf70c | 520 | } |
1c79356b A |
521 | break; |
522 | case ND6_LLINFO_PROBE: | |
523 | if (ln->ln_asked < nd6_umaxtries) { | |
524 | ln->ln_asked++; | |
91447636 | 525 | ln->ln_expire = timenow.tv_sec + |
1c79356b A |
526 | nd_ifinfo[ifp->if_index].retrans / 1000; |
527 | nd6_ns_output(ifp, &dst->sin6_addr, | |
91447636 | 528 | &dst->sin6_addr, ln, 0, 0); |
1c79356b | 529 | } else { |
9bccf70c | 530 | next = nd6_free(rt); |
1c79356b A |
531 | } |
532 | break; | |
1c79356b A |
533 | } |
534 | ln = next; | |
535 | } | |
536 | ||
9bccf70c | 537 | /* expire default router list */ |
91447636 | 538 | lck_mtx_lock(nd6_mutex); |
1c79356b A |
539 | dr = TAILQ_FIRST(&nd_defrouter); |
540 | while (dr) { | |
91447636 | 541 | if (dr->expire && dr->expire < timenow.tv_sec) { |
1c79356b A |
542 | struct nd_defrouter *t; |
543 | t = TAILQ_NEXT(dr, dr_entry); | |
91447636 | 544 | defrtrlist_del(dr, 1); |
1c79356b A |
545 | dr = t; |
546 | } else { | |
1c79356b A |
547 | dr = TAILQ_NEXT(dr, dr_entry); |
548 | } | |
549 | } | |
1c79356b | 550 | |
9bccf70c A |
551 | /* |
552 | * expire interface addresses. | |
553 | * in the past the loop was inside prefix expiry processing. | |
554 | * However, from a stricter speci-confrmance standpoint, we should | |
555 | * rather separate address lifetimes and prefix lifetimes. | |
556 | */ | |
557 | addrloop: | |
91447636 | 558 | for (ia6 = in6_ifaddrs; ia6; ia6 = nia6) { |
9bccf70c A |
559 | nia6 = ia6->ia_next; |
560 | /* check address lifetime */ | |
561 | lt6 = &ia6->ia6_lifetime; | |
562 | if (IFA6_IS_INVALID(ia6)) { | |
563 | int regen = 0; | |
564 | ||
565 | /* | |
566 | * If the expiring address is temporary, try | |
567 | * regenerating a new one. This would be useful when | |
55e303ae | 568 | * we suspended a laptop PC, then turned it on after a |
9bccf70c A |
569 | * period that could invalidate all temporary |
570 | * addresses. Although we may have to restart the | |
571 | * loop (see below), it must be after purging the | |
572 | * address. Otherwise, we'd see an infinite loop of | |
573 | * regeneration. | |
574 | */ | |
575 | if (ip6_use_tempaddr && | |
576 | (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { | |
2d21ac55 A |
577 | /* NOTE: We have to drop the lock here because |
578 | * regen_tmpaddr() eventually calls in6_update_ifa(), | |
579 | * which must take the lock and would otherwise cause a | |
580 | * hang. This is safe because the goto addrloop | |
581 | * leads to a reevaluation of the in6_ifaddrs list | |
582 | */ | |
583 | lck_mtx_unlock(nd6_mutex); | |
584 | if (regen_tmpaddr(ia6) == 0) | |
9bccf70c | 585 | regen = 1; |
2d21ac55 | 586 | lck_mtx_lock(nd6_mutex); |
9bccf70c A |
587 | } |
588 | ||
91447636 | 589 | in6_purgeaddr(&ia6->ia_ifa, 1); |
9bccf70c A |
590 | |
591 | if (regen) | |
592 | goto addrloop; /* XXX: see below */ | |
55e303ae A |
593 | } |
594 | if (IFA6_IS_DEPRECATED(ia6)) { | |
9bccf70c A |
595 | int oldflags = ia6->ia6_flags; |
596 | ||
597 | ia6->ia6_flags |= IN6_IFF_DEPRECATED; | |
598 | ||
599 | /* | |
600 | * If a temporary address has just become deprecated, | |
601 | * regenerate a new one if possible. | |
602 | */ | |
603 | if (ip6_use_tempaddr && | |
604 | (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && | |
605 | (oldflags & IN6_IFF_DEPRECATED) == 0) { | |
606 | ||
2d21ac55 A |
607 | /* see NOTE above */ |
608 | lck_mtx_unlock(nd6_mutex); | |
9bccf70c A |
609 | if (regen_tmpaddr(ia6) == 0) { |
610 | /* | |
611 | * A new temporary address is | |
612 | * generated. | |
613 | * XXX: this means the address chain | |
614 | * has changed while we are still in | |
615 | * the loop. Although the change | |
616 | * would not cause disaster (because | |
55e303ae A |
617 | * it's not a deletion, but an |
618 | * addition,) we'd rather restart the | |
9bccf70c A |
619 | * loop just for safety. Or does this |
620 | * significantly reduce performance?? | |
621 | */ | |
2d21ac55 | 622 | lck_mtx_lock(nd6_mutex); |
9bccf70c A |
623 | goto addrloop; |
624 | } | |
2d21ac55 | 625 | lck_mtx_lock(nd6_mutex); |
1c79356b | 626 | } |
55e303ae | 627 | } else { |
9bccf70c A |
628 | /* |
629 | * A new RA might have made a deprecated address | |
630 | * preferred. | |
631 | */ | |
632 | ia6->ia6_flags &= ~IN6_IFF_DEPRECATED; | |
1c79356b | 633 | } |
9bccf70c | 634 | } |
1c79356b | 635 | |
9bccf70c A |
636 | /* expire prefix list */ |
637 | pr = nd_prefix.lh_first; | |
638 | while (pr) { | |
1c79356b A |
639 | /* |
640 | * check prefix lifetime. | |
641 | * since pltime is just for autoconf, pltime processing for | |
642 | * prefix is not necessary. | |
1c79356b | 643 | */ |
91447636 | 644 | if (pr->ndpr_expire && pr->ndpr_expire < timenow.tv_sec) { |
1c79356b A |
645 | struct nd_prefix *t; |
646 | t = pr->ndpr_next; | |
647 | ||
648 | /* | |
649 | * address expiration and prefix expiration are | |
9bccf70c | 650 | * separate. NEVER perform in6_purgeaddr here. |
1c79356b A |
651 | */ |
652 | ||
91447636 | 653 | prelist_remove(pr, 1); |
1c79356b A |
654 | pr = t; |
655 | } else | |
656 | pr = pr->ndpr_next; | |
657 | } | |
91447636 A |
658 | lck_mtx_unlock(nd6_mutex); |
659 | timeout(nd6_timer, (caddr_t)0, nd6_prune * hz); | |
1c79356b A |
660 | } |
661 | ||
9bccf70c | 662 | static int |
91447636 A |
663 | regen_tmpaddr( |
664 | struct in6_ifaddr *ia6) /* deprecated/invalidated temporary address */ | |
9bccf70c A |
665 | { |
666 | struct ifaddr *ifa; | |
667 | struct ifnet *ifp; | |
668 | struct in6_ifaddr *public_ifa6 = NULL; | |
91447636 A |
669 | struct timeval timenow; |
670 | ||
671 | getmicrotime(&timenow); | |
9bccf70c A |
672 | |
673 | ifp = ia6->ia_ifa.ifa_ifp; | |
91447636 | 674 | ifnet_lock_exclusive(ifp); |
9bccf70c A |
675 | for (ifa = ifp->if_addrlist.tqh_first; ifa; |
676 | ifa = ifa->ifa_list.tqe_next) | |
677 | { | |
678 | struct in6_ifaddr *it6; | |
679 | ||
680 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
681 | continue; | |
682 | ||
683 | it6 = (struct in6_ifaddr *)ifa; | |
684 | ||
685 | /* ignore no autoconf addresses. */ | |
686 | if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) | |
687 | continue; | |
688 | ||
689 | /* ignore autoconf addresses with different prefixes. */ | |
690 | if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) | |
691 | continue; | |
692 | ||
693 | /* | |
694 | * Now we are looking at an autoconf address with the same | |
695 | * prefix as ours. If the address is temporary and is still | |
696 | * preferred, do not create another one. It would be rare, but | |
697 | * could happen, for example, when we resume a laptop PC after | |
698 | * a long period. | |
699 | */ | |
700 | if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && | |
701 | !IFA6_IS_DEPRECATED(it6)) { | |
702 | public_ifa6 = NULL; | |
703 | break; | |
704 | } | |
705 | ||
706 | /* | |
707 | * This is a public autoconf address that has the same prefix | |
708 | * as ours. If it is preferred, keep it. We can't break the | |
709 | * loop here, because there may be a still-preferred temporary | |
710 | * address with the prefix. | |
711 | */ | |
712 | if (!IFA6_IS_DEPRECATED(it6)) | |
713 | public_ifa6 = it6; | |
714 | } | |
91447636 | 715 | ifnet_lock_done(ifp); |
9bccf70c A |
716 | |
717 | if (public_ifa6 != NULL) { | |
718 | int e; | |
719 | ||
720 | if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) { | |
721 | log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" | |
722 | " tmp addr,errno=%d\n", e); | |
723 | return(-1); | |
724 | } | |
725 | return(0); | |
726 | } | |
727 | ||
728 | return(-1); | |
729 | } | |
730 | ||
1c79356b A |
731 | /* |
732 | * Nuke neighbor cache/prefix/default router management table, right before | |
733 | * ifp goes away. | |
734 | */ | |
735 | void | |
91447636 A |
736 | nd6_purge( |
737 | struct ifnet *ifp) | |
1c79356b A |
738 | { |
739 | struct llinfo_nd6 *ln, *nln; | |
740 | struct nd_defrouter *dr, *ndr, drany; | |
741 | struct nd_prefix *pr, *npr; | |
742 | ||
743 | /* Nuke default router list entries toward ifp */ | |
91447636 | 744 | lck_mtx_lock(nd6_mutex); |
1c79356b A |
745 | if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) { |
746 | /* | |
747 | * The first entry of the list may be stored in | |
748 | * the routing table, so we'll delete it later. | |
749 | */ | |
750 | for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) { | |
751 | ndr = TAILQ_NEXT(dr, dr_entry); | |
752 | if (dr->ifp == ifp) | |
91447636 | 753 | defrtrlist_del(dr, 1); |
1c79356b A |
754 | } |
755 | dr = TAILQ_FIRST(&nd_defrouter); | |
756 | if (dr->ifp == ifp) | |
91447636 | 757 | defrtrlist_del(dr, 1); |
1c79356b A |
758 | } |
759 | ||
760 | /* Nuke prefix list entries toward ifp */ | |
761 | for (pr = nd_prefix.lh_first; pr; pr = npr) { | |
762 | npr = pr->ndpr_next; | |
763 | if (pr->ndpr_ifp == ifp) { | |
9bccf70c A |
764 | /* |
765 | * Previously, pr->ndpr_addr is removed as well, | |
766 | * but I strongly believe we don't have to do it. | |
767 | * nd6_purge() is only called from in6_ifdetach(), | |
768 | * which removes all the associated interface addresses | |
769 | * by itself. | |
770 | * (jinmei@kame.net 20010129) | |
771 | */ | |
91447636 | 772 | prelist_remove(pr, 1); |
1c79356b A |
773 | } |
774 | } | |
775 | ||
776 | /* cancel default outgoing interface setting */ | |
777 | if (nd6_defifindex == ifp->if_index) | |
778 | nd6_setdefaultiface(0); | |
779 | ||
55e303ae | 780 | if (!ip6_forwarding && (ip6_accept_rtadv || (ifp->if_eflags & IFEF_ACCEPT_RTADVD))) { |
9bccf70c A |
781 | /* refresh default router list */ |
782 | bzero(&drany, sizeof(drany)); | |
783 | defrouter_delreq(&drany, 0); | |
784 | defrouter_select(); | |
785 | } | |
91447636 | 786 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
787 | |
788 | /* | |
789 | * Nuke neighbor cache entries for the ifp. | |
790 | * Note that rt->rt_ifp may not be the same as ifp, | |
791 | * due to KAME goto ours hack. See RTM_RESOLVE case in | |
792 | * nd6_rtrequest(), and ip6_input(). | |
793 | */ | |
794 | ln = llinfo_nd6.ln_next; | |
795 | while (ln && ln != &llinfo_nd6) { | |
796 | struct rtentry *rt; | |
797 | struct sockaddr_dl *sdl; | |
798 | ||
799 | nln = ln->ln_next; | |
800 | rt = ln->ln_rt; | |
801 | if (rt && rt->rt_gateway && | |
802 | rt->rt_gateway->sa_family == AF_LINK) { | |
803 | sdl = (struct sockaddr_dl *)rt->rt_gateway; | |
804 | if (sdl->sdl_index == ifp->if_index) | |
9bccf70c | 805 | nln = nd6_free(rt); |
1c79356b A |
806 | } |
807 | ln = nln; | |
808 | } | |
809 | } | |
810 | ||
811 | struct rtentry * | |
91447636 A |
812 | nd6_lookup( |
813 | struct in6_addr *addr6, | |
814 | int create, | |
815 | struct ifnet *ifp, | |
816 | int rt_locked) | |
1c79356b A |
817 | { |
818 | struct rtentry *rt; | |
819 | struct sockaddr_in6 sin6; | |
820 | ||
821 | bzero(&sin6, sizeof(sin6)); | |
822 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
823 | sin6.sin6_family = AF_INET6; | |
824 | sin6.sin6_addr = *addr6; | |
9bccf70c A |
825 | #if SCOPEDROUTING |
826 | sin6.sin6_scope_id = in6_addr2scopeid(ifp, addr6); | |
827 | #endif | |
91447636 A |
828 | if (!rt_locked) |
829 | lck_mtx_lock(rt_mtx); | |
830 | rt = rtalloc1_locked((struct sockaddr *)&sin6, create, 0UL); | |
1c79356b A |
831 | if (rt && (rt->rt_flags & RTF_LLINFO) == 0) { |
832 | /* | |
833 | * This is the case for the default route. | |
834 | * If we want to create a neighbor cache for the address, we | |
835 | * should free the route for the destination and allocate an | |
836 | * interface route. | |
837 | */ | |
838 | if (create) { | |
91447636 | 839 | rtfree_locked(rt); |
1c79356b A |
840 | rt = 0; |
841 | } | |
842 | } | |
843 | if (!rt) { | |
844 | if (create && ifp) { | |
845 | int e; | |
846 | ||
847 | /* | |
848 | * If no route is available and create is set, | |
849 | * we allocate a host route for the destination | |
850 | * and treat it like an interface route. | |
851 | * This hack is necessary for a neighbor which can't | |
852 | * be covered by our own prefix. | |
853 | */ | |
854 | struct ifaddr *ifa = | |
855 | ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp); | |
91447636 A |
856 | if (ifa == NULL) { |
857 | if (!rt_locked) | |
858 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 859 | return(NULL); |
91447636 | 860 | } |
1c79356b A |
861 | |
862 | /* | |
55e303ae | 863 | * Create a new route. RTF_LLINFO is necessary |
1c79356b A |
864 | * to create a Neighbor Cache entry for the |
865 | * destination in nd6_rtrequest which will be | |
55e303ae | 866 | * called in rtrequest via ifa->ifa_rtrequest. |
1c79356b | 867 | */ |
91447636 | 868 | if ((e = rtrequest_locked(RTM_ADD, (struct sockaddr *)&sin6, |
1c79356b A |
869 | ifa->ifa_addr, |
870 | (struct sockaddr *)&all1_sa, | |
871 | (ifa->ifa_flags | | |
872 | RTF_HOST | RTF_LLINFO) & | |
873 | ~RTF_CLONING, | |
91447636 A |
874 | &rt)) != 0) { |
875 | if (e != EEXIST) | |
876 | log(LOG_ERR, | |
877 | "nd6_lookup: failed to add route for a " | |
878 | "neighbor(%s), errno=%d\n", | |
879 | ip6_sprintf(addr6), e); | |
880 | } | |
881 | ifafree(ifa); | |
882 | if (rt == NULL) { | |
883 | if (!rt_locked) | |
884 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 885 | return(NULL); |
91447636 | 886 | } |
1c79356b A |
887 | if (rt->rt_llinfo) { |
888 | struct llinfo_nd6 *ln = | |
889 | (struct llinfo_nd6 *)rt->rt_llinfo; | |
890 | ln->ln_state = ND6_LLINFO_NOSTATE; | |
891 | } | |
91447636 A |
892 | } else { |
893 | if (!rt_locked) | |
894 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 895 | return(NULL); |
91447636 | 896 | } |
1c79356b | 897 | } |
9bccf70c | 898 | rtunref(rt); |
1c79356b A |
899 | /* |
900 | * Validation for the entry. | |
55e303ae A |
901 | * Note that the check for rt_llinfo is necessary because a cloned |
902 | * route from a parent route that has the L flag (e.g. the default | |
903 | * route to a p2p interface) may have the flag, too, while the | |
904 | * destination is not actually a neighbor. | |
1c79356b A |
905 | * XXX: we can't use rt->rt_ifp to check for the interface, since |
906 | * it might be the loopback interface if the entry is for our | |
907 | * own address on a non-loopback interface. Instead, we should | |
55e303ae A |
908 | * use rt->rt_ifa->ifa_ifp, which would specify the REAL |
909 | * interface. | |
1c79356b | 910 | */ |
2d21ac55 | 911 | if ((ifp && ifp->if_type !=IFT_PPP) && ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || |
55e303ae | 912 | rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL || |
55e303ae | 913 | (ifp && rt->rt_ifa->ifa_ifp != ifp))) { |
91447636 A |
914 | if (!rt_locked) |
915 | lck_mtx_unlock(rt_mtx); | |
1c79356b A |
916 | if (create) { |
917 | log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n", | |
918 | ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec"); | |
919 | /* xxx more logs... kazu */ | |
920 | } | |
55e303ae | 921 | return(NULL); |
91447636 A |
922 | } |
923 | if (!rt_locked) | |
924 | lck_mtx_unlock(rt_mtx); | |
1c79356b A |
925 | return(rt); |
926 | } | |
927 | ||
928 | /* | |
929 | * Detect if a given IPv6 address identifies a neighbor on a given link. | |
930 | * XXX: should take care of the destination of a p2p link? | |
931 | */ | |
932 | int | |
91447636 A |
933 | nd6_is_addr_neighbor( |
934 | struct sockaddr_in6 *addr, | |
935 | struct ifnet *ifp, | |
936 | int rt_locked) | |
1c79356b | 937 | { |
9bccf70c | 938 | struct ifaddr *ifa; |
1c79356b A |
939 | int i; |
940 | ||
941 | #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr) | |
942 | #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr) | |
943 | ||
9bccf70c A |
944 | /* |
945 | * A link-local address is always a neighbor. | |
946 | * XXX: we should use the sin6_scope_id field rather than the embedded | |
947 | * interface index. | |
948 | */ | |
949 | if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) && | |
950 | ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index) | |
1c79356b A |
951 | return(1); |
952 | ||
953 | /* | |
954 | * If the address matches one of our addresses, | |
955 | * it should be a neighbor. | |
956 | */ | |
91447636 | 957 | ifnet_lock_shared(ifp); |
1c79356b A |
958 | for (ifa = ifp->if_addrlist.tqh_first; |
959 | ifa; | |
960 | ifa = ifa->ifa_list.tqe_next) | |
1c79356b A |
961 | { |
962 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
91447636 | 963 | continue; |
1c79356b A |
964 | |
965 | for (i = 0; i < 4; i++) { | |
9bccf70c A |
966 | if ((IFADDR6(ifa).s6_addr32[i] ^ |
967 | addr->sin6_addr.s6_addr32[i]) & | |
91447636 A |
968 | IFMASK6(ifa).s6_addr32[i]) |
969 | continue; | |
1c79356b | 970 | } |
91447636 | 971 | ifnet_lock_done(ifp); |
1c79356b A |
972 | return(1); |
973 | } | |
91447636 | 974 | ifnet_lock_done(ifp); |
1c79356b A |
975 | |
976 | /* | |
977 | * Even if the address matches none of our addresses, it might be | |
978 | * in the neighbor cache. | |
979 | */ | |
91447636 | 980 | if (nd6_lookup(&addr->sin6_addr, 0, ifp, rt_locked) != NULL) |
1c79356b A |
981 | return(1); |
982 | ||
983 | return(0); | |
984 | #undef IFADDR6 | |
985 | #undef IFMASK6 | |
986 | } | |
987 | ||
988 | /* | |
989 | * Free an nd6 llinfo entry. | |
990 | */ | |
9bccf70c | 991 | struct llinfo_nd6 * |
91447636 A |
992 | nd6_free( |
993 | struct rtentry *rt) | |
1c79356b | 994 | { |
9bccf70c | 995 | struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next; |
1c79356b A |
996 | struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr; |
997 | struct nd_defrouter *dr; | |
998 | ||
999 | /* | |
9bccf70c A |
1000 | * we used to have pfctlinput(PRC_HOSTDEAD) here. |
1001 | * even though it is not harmful, it was not really necessary. | |
1c79356b | 1002 | */ |
1c79356b | 1003 | |
55e303ae | 1004 | if (!ip6_forwarding && (ip6_accept_rtadv || (rt->rt_ifp->if_eflags & IFEF_ACCEPT_RTADVD))) { |
91447636 | 1005 | lck_mtx_lock(nd6_mutex); |
1c79356b A |
1006 | dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, |
1007 | rt->rt_ifp); | |
9bccf70c | 1008 | |
91447636 | 1009 | if ((ln && ln->ln_router) || dr) { |
1c79356b A |
1010 | /* |
1011 | * rt6_flush must be called whether or not the neighbor | |
1012 | * is in the Default Router List. | |
1013 | * See a corresponding comment in nd6_na_input(). | |
1014 | */ | |
1015 | rt6_flush(&in6, rt->rt_ifp); | |
1016 | } | |
1017 | ||
1018 | if (dr) { | |
1019 | /* | |
1020 | * Unreachablity of a router might affect the default | |
1021 | * router selection and on-link detection of advertised | |
1022 | * prefixes. | |
1023 | */ | |
1024 | ||
1025 | /* | |
1026 | * Temporarily fake the state to choose a new default | |
1027 | * router and to perform on-link determination of | |
55e303ae | 1028 | * prefixes correctly. |
1c79356b A |
1029 | * Below the state will be set correctly, |
1030 | * or the entry itself will be deleted. | |
1031 | */ | |
1032 | ln->ln_state = ND6_LLINFO_INCOMPLETE; | |
1033 | ||
9bccf70c A |
1034 | /* |
1035 | * Since defrouter_select() does not affect the | |
1036 | * on-link determination and MIP6 needs the check | |
1037 | * before the default router selection, we perform | |
1038 | * the check now. | |
1039 | */ | |
91447636 | 1040 | pfxlist_onlink_check(1); |
9bccf70c | 1041 | |
1c79356b A |
1042 | if (dr == TAILQ_FIRST(&nd_defrouter)) { |
1043 | /* | |
1044 | * It is used as the current default router, | |
1045 | * so we have to move it to the end of the | |
1046 | * list and choose a new one. | |
1047 | * XXX: it is not very efficient if this is | |
1048 | * the only router. | |
1049 | */ | |
1050 | TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); | |
1051 | TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry); | |
1052 | ||
1053 | defrouter_select(); | |
1054 | } | |
1c79356b | 1055 | } |
91447636 | 1056 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
1057 | } |
1058 | ||
9bccf70c A |
1059 | /* |
1060 | * Before deleting the entry, remember the next entry as the | |
1061 | * return value. We need this because pfxlist_onlink_check() above | |
1062 | * might have freed other entries (particularly the old next entry) as | |
55e303ae | 1063 | * a side effect (XXX). |
9bccf70c | 1064 | */ |
55e303ae A |
1065 | if (ln) |
1066 | next = ln->ln_next; | |
1067 | else | |
1068 | next = 0; | |
1c79356b | 1069 | |
9bccf70c A |
1070 | /* |
1071 | * Detach the route from the routing tree and the list of neighbor | |
1072 | * caches, and disable the route entry not to be used in already | |
1073 | * cached routes. | |
1074 | */ | |
1c79356b A |
1075 | rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, |
1076 | rt_mask(rt), 0, (struct rtentry **)0); | |
9bccf70c A |
1077 | |
1078 | return(next); | |
1c79356b A |
1079 | } |
1080 | ||
1081 | /* | |
1082 | * Upper-layer reachability hint for Neighbor Unreachability Detection. | |
1083 | * | |
1084 | * XXX cost-effective metods? | |
1085 | */ | |
1086 | void | |
91447636 A |
1087 | nd6_nud_hint( |
1088 | struct rtentry *rt, | |
1089 | struct in6_addr *dst6, | |
1090 | int force) | |
1c79356b A |
1091 | { |
1092 | struct llinfo_nd6 *ln; | |
91447636 A |
1093 | struct timeval timenow; |
1094 | ||
1095 | getmicrotime(&timenow); | |
1c79356b A |
1096 | |
1097 | /* | |
1098 | * If the caller specified "rt", use that. Otherwise, resolve the | |
1099 | * routing table by supplied "dst6". | |
1100 | */ | |
1101 | if (!rt) { | |
1102 | if (!dst6) | |
1103 | return; | |
91447636 | 1104 | if (!(rt = nd6_lookup(dst6, 0, NULL, 0))) |
1c79356b A |
1105 | return; |
1106 | } | |
1107 | ||
9bccf70c A |
1108 | if ((rt->rt_flags & RTF_GATEWAY) != 0 || |
1109 | (rt->rt_flags & RTF_LLINFO) == 0 || | |
1110 | !rt->rt_llinfo || !rt->rt_gateway || | |
1111 | rt->rt_gateway->sa_family != AF_LINK) { | |
1c79356b A |
1112 | /* This is not a host route. */ |
1113 | return; | |
1114 | } | |
1115 | ||
1116 | ln = (struct llinfo_nd6 *)rt->rt_llinfo; | |
1117 | if (ln->ln_state < ND6_LLINFO_REACHABLE) | |
1118 | return; | |
1119 | ||
1c79356b | 1120 | /* |
9bccf70c A |
1121 | * if we get upper-layer reachability confirmation many times, |
1122 | * it is possible we have false information. | |
1c79356b | 1123 | */ |
9bccf70c A |
1124 | if (!force) { |
1125 | ln->ln_byhint++; | |
1126 | if (ln->ln_byhint > nd6_maxnudhint) | |
1127 | return; | |
1c79356b | 1128 | } |
9bccf70c A |
1129 | |
1130 | ln->ln_state = ND6_LLINFO_REACHABLE; | |
1131 | if (ln->ln_expire) | |
91447636 | 1132 | ln->ln_expire = timenow.tv_sec + |
9bccf70c | 1133 | nd_ifinfo[rt->rt_ifp->if_index].reachable; |
1c79356b | 1134 | } |
1c79356b A |
1135 | |
1136 | void | |
91447636 A |
1137 | nd6_rtrequest( |
1138 | int req, | |
1139 | struct rtentry *rt, | |
2d21ac55 | 1140 | __unused struct sockaddr *sa) |
1c79356b A |
1141 | { |
1142 | struct sockaddr *gate = rt->rt_gateway; | |
1143 | struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo; | |
2d21ac55 A |
1144 | static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK, 0, 0, 0, 0, 0, |
1145 | {0,0,0,0,0,0,0,0,0,0,0,0,} }; | |
1c79356b A |
1146 | struct ifnet *ifp = rt->rt_ifp; |
1147 | struct ifaddr *ifa; | |
91447636 A |
1148 | struct timeval timenow; |
1149 | ||
1c79356b | 1150 | |
55e303ae | 1151 | if ((rt->rt_flags & RTF_GATEWAY)) |
1c79356b A |
1152 | return; |
1153 | ||
9bccf70c A |
1154 | if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { |
1155 | /* | |
1156 | * This is probably an interface direct route for a link | |
1157 | * which does not need neighbor caches (e.g. fe80::%lo0/64). | |
1158 | * We do not need special treatment below for such a route. | |
1159 | * Moreover, the RTF_LLINFO flag which would be set below | |
1160 | * would annoy the ndp(8) command. | |
1161 | */ | |
1162 | return; | |
1163 | } | |
1164 | ||
55e303ae A |
1165 | if (req == RTM_RESOLVE && |
1166 | (nd6_need_cache(ifp) == 0 || /* stf case */ | |
91447636 | 1167 | !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp, 1))) { |
55e303ae A |
1168 | /* |
1169 | * FreeBSD and BSD/OS often make a cloned host route based | |
1170 | * on a less-specific route (e.g. the default route). | |
1171 | * If the less specific route does not have a "gateway" | |
1172 | * (this is the case when the route just goes to a p2p or an | |
1173 | * stf interface), we'll mistakenly make a neighbor cache for | |
1174 | * the host route, and will see strange neighbor solicitation | |
1175 | * for the corresponding destination. In order to avoid the | |
1176 | * confusion, we check if the destination of the route is | |
1177 | * a neighbor in terms of neighbor discovery, and stop the | |
1178 | * process if not. Additionally, we remove the LLINFO flag | |
1179 | * so that ndp(8) will not try to get the neighbor information | |
1180 | * of the destination. | |
1181 | */ | |
1182 | rt->rt_flags &= ~RTF_LLINFO; | |
1183 | return; | |
1184 | } | |
1185 | ||
91447636 | 1186 | getmicrotime(&timenow); |
1c79356b A |
1187 | switch (req) { |
1188 | case RTM_ADD: | |
1189 | /* | |
1190 | * There is no backward compatibility :) | |
1191 | * | |
1192 | * if ((rt->rt_flags & RTF_HOST) == 0 && | |
1193 | * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) | |
1194 | * rt->rt_flags |= RTF_CLONING; | |
1195 | */ | |
1196 | if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) { | |
1197 | /* | |
1198 | * Case 1: This route should come from | |
55e303ae | 1199 | * a route to interface. RTF_LLINFO flag is set |
1c79356b A |
1200 | * for a host route whose destination should be |
1201 | * treated as on-link. | |
1202 | */ | |
1203 | rt_setgate(rt, rt_key(rt), | |
1204 | (struct sockaddr *)&null_sdl); | |
1205 | gate = rt->rt_gateway; | |
1206 | SDL(gate)->sdl_type = ifp->if_type; | |
1207 | SDL(gate)->sdl_index = ifp->if_index; | |
1208 | if (ln) | |
91447636 | 1209 | ln->ln_expire = timenow.tv_sec; |
1c79356b A |
1210 | #if 1 |
1211 | if (ln && ln->ln_expire == 0) { | |
9bccf70c | 1212 | /* kludge for desktops */ |
1c79356b | 1213 | #if 0 |
55e303ae | 1214 | printf("nd6_rtequest: time.tv_sec is zero; " |
1c79356b A |
1215 | "treat it as 1\n"); |
1216 | #endif | |
1217 | ln->ln_expire = 1; | |
1218 | } | |
1219 | #endif | |
55e303ae | 1220 | if ((rt->rt_flags & RTF_CLONING)) |
1c79356b A |
1221 | break; |
1222 | } | |
1223 | /* | |
1224 | * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. | |
1225 | * We don't do that here since llinfo is not ready yet. | |
1226 | * | |
1227 | * There are also couple of other things to be discussed: | |
1228 | * - unsolicited NA code needs improvement beforehand | |
1229 | * - RFC2461 says we MAY send multicast unsolicited NA | |
1230 | * (7.2.6 paragraph 4), however, it also says that we | |
1231 | * SHOULD provide a mechanism to prevent multicast NA storm. | |
1232 | * we don't have anything like it right now. | |
9bccf70c | 1233 | * note that the mechanism needs a mutual agreement |
1c79356b | 1234 | * between proxies, which means that we need to implement |
9bccf70c A |
1235 | * a new protocol, or a new kludge. |
1236 | * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. | |
1c79356b A |
1237 | * we need to check ip6forwarding before sending it. |
1238 | * (or should we allow proxy ND configuration only for | |
1239 | * routers? there's no mention about proxy ND from hosts) | |
1240 | */ | |
1241 | #if 0 | |
1242 | /* XXX it does not work */ | |
1243 | if (rt->rt_flags & RTF_ANNOUNCE) | |
1244 | nd6_na_output(ifp, | |
1245 | &SIN6(rt_key(rt))->sin6_addr, | |
1246 | &SIN6(rt_key(rt))->sin6_addr, | |
1247 | ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, | |
1248 | 1, NULL); | |
1249 | #endif | |
1250 | /* FALLTHROUGH */ | |
1251 | case RTM_RESOLVE: | |
9bccf70c | 1252 | if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { |
1c79356b A |
1253 | /* |
1254 | * Address resolution isn't necessary for a point to | |
1255 | * point link, so we can skip this test for a p2p link. | |
1256 | */ | |
1257 | if (gate->sa_family != AF_LINK || | |
1258 | gate->sa_len < sizeof(null_sdl)) { | |
1259 | log(LOG_DEBUG, | |
9bccf70c A |
1260 | "nd6_rtrequest: bad gateway value: %s\n", |
1261 | if_name(ifp)); | |
1c79356b A |
1262 | break; |
1263 | } | |
1264 | SDL(gate)->sdl_type = ifp->if_type; | |
1265 | SDL(gate)->sdl_index = ifp->if_index; | |
1266 | } | |
1267 | if (ln != NULL) | |
1268 | break; /* This happens on a route change */ | |
1269 | /* | |
1270 | * Case 2: This route may come from cloning, or a manual route | |
1271 | * add with a LL address. | |
1272 | */ | |
1273 | R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln)); | |
1274 | rt->rt_llinfo = (caddr_t)ln; | |
1275 | if (!ln) { | |
1276 | log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n"); | |
1277 | break; | |
1278 | } | |
1279 | nd6_inuse++; | |
1280 | nd6_allocated++; | |
1281 | Bzero(ln, sizeof(*ln)); | |
1282 | ln->ln_rt = rt; | |
1283 | /* this is required for "ndp" command. - shin */ | |
1284 | if (req == RTM_ADD) { | |
1285 | /* | |
1286 | * gate should have some valid AF_LINK entry, | |
1287 | * and ln->ln_expire should have some lifetime | |
1288 | * which is specified by ndp command. | |
1289 | */ | |
1290 | ln->ln_state = ND6_LLINFO_REACHABLE; | |
9bccf70c | 1291 | ln->ln_byhint = 0; |
1c79356b A |
1292 | } else { |
1293 | /* | |
1294 | * When req == RTM_RESOLVE, rt is created and | |
1295 | * initialized in rtrequest(), so rt_expire is 0. | |
1296 | */ | |
1297 | ln->ln_state = ND6_LLINFO_NOSTATE; | |
91447636 | 1298 | ln->ln_expire = timenow.tv_sec; |
1c79356b A |
1299 | } |
1300 | rt->rt_flags |= RTF_LLINFO; | |
1301 | ln->ln_next = llinfo_nd6.ln_next; | |
1302 | llinfo_nd6.ln_next = ln; | |
1303 | ln->ln_prev = &llinfo_nd6; | |
1304 | ln->ln_next->ln_prev = ln; | |
1305 | ||
1306 | /* | |
1307 | * check if rt_key(rt) is one of my address assigned | |
1308 | * to the interface. | |
1309 | */ | |
1310 | ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp, | |
1311 | &SIN6(rt_key(rt))->sin6_addr); | |
1312 | if (ifa) { | |
1313 | caddr_t macp = nd6_ifptomac(ifp); | |
1314 | ln->ln_expire = 0; | |
1315 | ln->ln_state = ND6_LLINFO_REACHABLE; | |
9bccf70c | 1316 | ln->ln_byhint = 0; |
1c79356b A |
1317 | if (macp) { |
1318 | Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen); | |
1319 | SDL(gate)->sdl_alen = ifp->if_addrlen; | |
1320 | } | |
1321 | if (nd6_useloopback) { | |
2d21ac55 | 1322 | rt->rt_ifp = lo_ifp; /* XXX */ |
1c79356b A |
1323 | /* |
1324 | * Make sure rt_ifa be equal to the ifaddr | |
1325 | * corresponding to the address. | |
1326 | * We need this because when we refer | |
1327 | * rt_ifa->ia6_flags in ip6_input, we assume | |
1328 | * that the rt_ifa points to the address instead | |
1329 | * of the loopback address. | |
1330 | */ | |
1331 | if (ifa != rt->rt_ifa) { | |
9bccf70c | 1332 | rtsetifa(rt, ifa); |
1c79356b A |
1333 | } |
1334 | } | |
1335 | } else if (rt->rt_flags & RTF_ANNOUNCE) { | |
1336 | ln->ln_expire = 0; | |
1337 | ln->ln_state = ND6_LLINFO_REACHABLE; | |
9bccf70c | 1338 | ln->ln_byhint = 0; |
1c79356b A |
1339 | |
1340 | /* join solicited node multicast for proxy ND */ | |
1341 | if (ifp->if_flags & IFF_MULTICAST) { | |
1342 | struct in6_addr llsol; | |
1343 | int error; | |
1344 | ||
1345 | llsol = SIN6(rt_key(rt))->sin6_addr; | |
1346 | llsol.s6_addr16[0] = htons(0xff02); | |
1347 | llsol.s6_addr16[1] = htons(ifp->if_index); | |
1348 | llsol.s6_addr32[1] = 0; | |
1349 | llsol.s6_addr32[2] = htonl(1); | |
1350 | llsol.s6_addr8[12] = 0xff; | |
1351 | ||
91447636 | 1352 | if (!in6_addmulti(&llsol, ifp, &error, 0)) { |
9bccf70c A |
1353 | nd6log((LOG_ERR, "%s: failed to join " |
1354 | "%s (errno=%d)\n", if_name(ifp), | |
1355 | ip6_sprintf(&llsol), error)); | |
1356 | } | |
1c79356b A |
1357 | } |
1358 | } | |
1359 | break; | |
1360 | ||
1361 | case RTM_DELETE: | |
1362 | if (!ln) | |
1363 | break; | |
1364 | /* leave from solicited node multicast for proxy ND */ | |
1365 | if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && | |
1366 | (ifp->if_flags & IFF_MULTICAST) != 0) { | |
1367 | struct in6_addr llsol; | |
1368 | struct in6_multi *in6m; | |
1369 | ||
1370 | llsol = SIN6(rt_key(rt))->sin6_addr; | |
1371 | llsol.s6_addr16[0] = htons(0xff02); | |
1372 | llsol.s6_addr16[1] = htons(ifp->if_index); | |
1373 | llsol.s6_addr32[1] = 0; | |
1374 | llsol.s6_addr32[2] = htonl(1); | |
1375 | llsol.s6_addr8[12] = 0xff; | |
1376 | ||
91447636 | 1377 | ifnet_lock_shared(ifp); |
1c79356b | 1378 | IN6_LOOKUP_MULTI(llsol, ifp, in6m); |
91447636 | 1379 | ifnet_lock_done(ifp); |
1c79356b | 1380 | if (in6m) |
91447636 | 1381 | in6_delmulti(in6m, 0); |
1c79356b A |
1382 | } |
1383 | nd6_inuse--; | |
1384 | ln->ln_next->ln_prev = ln->ln_prev; | |
1385 | ln->ln_prev->ln_next = ln->ln_next; | |
1386 | ln->ln_prev = NULL; | |
1387 | rt->rt_llinfo = 0; | |
1388 | rt->rt_flags &= ~RTF_LLINFO; | |
1389 | if (ln->ln_hold) | |
1390 | m_freem(ln->ln_hold); | |
55e303ae | 1391 | ln->ln_hold = NULL; |
91447636 | 1392 | R_Free((caddr_t)ln); |
1c79356b A |
1393 | } |
1394 | } | |
1395 | ||
1c79356b | 1396 | int |
91447636 A |
1397 | nd6_ioctl( |
1398 | u_long cmd, | |
1399 | caddr_t data, | |
1400 | struct ifnet *ifp) | |
1c79356b A |
1401 | { |
1402 | struct in6_drlist *drl = (struct in6_drlist *)data; | |
1403 | struct in6_prlist *prl = (struct in6_prlist *)data; | |
1404 | struct in6_ndireq *ndi = (struct in6_ndireq *)data; | |
1405 | struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; | |
1406 | struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; | |
1407 | struct nd_defrouter *dr, any; | |
1408 | struct nd_prefix *pr; | |
1409 | struct rtentry *rt; | |
1410 | int i = 0, error = 0; | |
1c79356b A |
1411 | |
1412 | switch (cmd) { | |
1413 | case SIOCGDRLST_IN6: | |
9bccf70c A |
1414 | /* |
1415 | * obsolete API, use sysctl under net.inet6.icmp6 | |
1416 | */ | |
91447636 | 1417 | lck_mtx_lock(nd6_mutex); |
1c79356b | 1418 | bzero(drl, sizeof(*drl)); |
1c79356b A |
1419 | dr = TAILQ_FIRST(&nd_defrouter); |
1420 | while (dr && i < DRLSTSIZ) { | |
1421 | drl->defrouter[i].rtaddr = dr->rtaddr; | |
1422 | if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) { | |
1423 | /* XXX: need to this hack for KAME stack */ | |
1424 | drl->defrouter[i].rtaddr.s6_addr16[1] = 0; | |
9bccf70c | 1425 | } else |
1c79356b A |
1426 | log(LOG_ERR, |
1427 | "default router list contains a " | |
1428 | "non-linklocal address(%s)\n", | |
1429 | ip6_sprintf(&drl->defrouter[i].rtaddr)); | |
1430 | ||
1431 | drl->defrouter[i].flags = dr->flags; | |
1432 | drl->defrouter[i].rtlifetime = dr->rtlifetime; | |
1433 | drl->defrouter[i].expire = dr->expire; | |
1434 | drl->defrouter[i].if_index = dr->ifp->if_index; | |
1435 | i++; | |
1436 | dr = TAILQ_NEXT(dr, dr_entry); | |
1437 | } | |
91447636 | 1438 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
1439 | break; |
1440 | case SIOCGPRLST_IN6: | |
9bccf70c A |
1441 | /* |
1442 | * obsolete API, use sysctl under net.inet6.icmp6 | |
1443 | */ | |
1c79356b A |
1444 | /* |
1445 | * XXX meaning of fields, especialy "raflags", is very | |
1446 | * differnet between RA prefix list and RR/static prefix list. | |
1447 | * how about separating ioctls into two? | |
1448 | */ | |
1449 | bzero(prl, sizeof(*prl)); | |
91447636 | 1450 | lck_mtx_lock(nd6_mutex); |
1c79356b A |
1451 | pr = nd_prefix.lh_first; |
1452 | while (pr && i < PRLSTSIZ) { | |
1453 | struct nd_pfxrouter *pfr; | |
1454 | int j; | |
1455 | ||
9bccf70c A |
1456 | (void)in6_embedscope(&prl->prefix[i].prefix, |
1457 | &pr->ndpr_prefix, NULL, NULL); | |
1c79356b A |
1458 | prl->prefix[i].raflags = pr->ndpr_raf; |
1459 | prl->prefix[i].prefixlen = pr->ndpr_plen; | |
1460 | prl->prefix[i].vltime = pr->ndpr_vltime; | |
1461 | prl->prefix[i].pltime = pr->ndpr_pltime; | |
1462 | prl->prefix[i].if_index = pr->ndpr_ifp->if_index; | |
1463 | prl->prefix[i].expire = pr->ndpr_expire; | |
1464 | ||
1465 | pfr = pr->ndpr_advrtrs.lh_first; | |
1466 | j = 0; | |
9bccf70c | 1467 | while (pfr) { |
1c79356b A |
1468 | if (j < DRLSTSIZ) { |
1469 | #define RTRADDR prl->prefix[i].advrtr[j] | |
1470 | RTRADDR = pfr->router->rtaddr; | |
1471 | if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) { | |
1472 | /* XXX: hack for KAME */ | |
1473 | RTRADDR.s6_addr16[1] = 0; | |
9bccf70c | 1474 | } else |
1c79356b A |
1475 | log(LOG_ERR, |
1476 | "a router(%s) advertises " | |
1477 | "a prefix with " | |
1478 | "non-link local address\n", | |
1479 | ip6_sprintf(&RTRADDR)); | |
1480 | #undef RTRADDR | |
1481 | } | |
1482 | j++; | |
1483 | pfr = pfr->pfr_next; | |
1484 | } | |
1485 | prl->prefix[i].advrtrs = j; | |
1486 | prl->prefix[i].origin = PR_ORIG_RA; | |
1487 | ||
1488 | i++; | |
1489 | pr = pr->ndpr_next; | |
1490 | } | |
1491 | { | |
1492 | struct rr_prefix *rpp; | |
1493 | ||
1494 | for (rpp = LIST_FIRST(&rr_prefix); rpp; | |
1495 | rpp = LIST_NEXT(rpp, rp_entry)) { | |
1496 | if (i >= PRLSTSIZ) | |
1497 | break; | |
9bccf70c A |
1498 | (void)in6_embedscope(&prl->prefix[i].prefix, |
1499 | &pr->ndpr_prefix, NULL, NULL); | |
1c79356b A |
1500 | prl->prefix[i].raflags = rpp->rp_raf; |
1501 | prl->prefix[i].prefixlen = rpp->rp_plen; | |
1502 | prl->prefix[i].vltime = rpp->rp_vltime; | |
1503 | prl->prefix[i].pltime = rpp->rp_pltime; | |
1504 | prl->prefix[i].if_index = rpp->rp_ifp->if_index; | |
1505 | prl->prefix[i].expire = rpp->rp_expire; | |
1506 | prl->prefix[i].advrtrs = 0; | |
1507 | prl->prefix[i].origin = rpp->rp_origin; | |
1508 | i++; | |
1509 | } | |
1510 | } | |
91447636 | 1511 | lck_mtx_unlock(nd6_mutex); |
9bccf70c A |
1512 | break; |
1513 | case OSIOCGIFINFO_IN6: | |
1514 | if (!nd_ifinfo || i >= nd_ifinfo_indexlim) { | |
1515 | error = EINVAL; | |
1516 | break; | |
1517 | } | |
2d21ac55 | 1518 | ndi->ndi.linkmtu = IN6_LINKMTU(ifp); |
9bccf70c A |
1519 | ndi->ndi.maxmtu = nd_ifinfo[ifp->if_index].maxmtu; |
1520 | ndi->ndi.basereachable = | |
1521 | nd_ifinfo[ifp->if_index].basereachable; | |
1522 | ndi->ndi.reachable = nd_ifinfo[ifp->if_index].reachable; | |
1523 | ndi->ndi.retrans = nd_ifinfo[ifp->if_index].retrans; | |
1524 | ndi->ndi.flags = nd_ifinfo[ifp->if_index].flags; | |
1525 | ndi->ndi.recalctm = nd_ifinfo[ifp->if_index].recalctm; | |
1526 | ndi->ndi.chlim = nd_ifinfo[ifp->if_index].chlim; | |
1527 | ndi->ndi.receivedra = nd_ifinfo[ifp->if_index].receivedra; | |
1c79356b A |
1528 | break; |
1529 | case SIOCGIFINFO_IN6: | |
9bccf70c A |
1530 | if (!nd_ifinfo || i >= nd_ifinfo_indexlim) { |
1531 | error = EINVAL; | |
1532 | break; | |
1533 | } | |
1c79356b A |
1534 | ndi->ndi = nd_ifinfo[ifp->if_index]; |
1535 | break; | |
1536 | case SIOCSIFINFO_FLAGS: | |
1537 | /* XXX: almost all other fields of ndi->ndi is unused */ | |
9bccf70c A |
1538 | if (!nd_ifinfo || i >= nd_ifinfo_indexlim) { |
1539 | error = EINVAL; | |
1540 | break; | |
1541 | } | |
1c79356b A |
1542 | nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags; |
1543 | break; | |
1544 | case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ | |
1545 | /* flush default router list */ | |
1546 | /* | |
1547 | * xxx sumikawa: should not delete route if default | |
1548 | * route equals to the top of default router list | |
1549 | */ | |
1550 | bzero(&any, sizeof(any)); | |
91447636 A |
1551 | lck_mtx_lock(nd6_mutex); |
1552 | defrouter_delreq(&any, 1); | |
1c79356b | 1553 | defrouter_select(); |
91447636 | 1554 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
1555 | /* xxx sumikawa: flush prefix list */ |
1556 | break; | |
1557 | case SIOCSPFXFLUSH_IN6: | |
1558 | { | |
1559 | /* flush all the prefix advertised by routers */ | |
2d21ac55 | 1560 | struct nd_prefix *next; |
91447636 | 1561 | lck_mtx_lock(nd6_mutex); |
1c79356b | 1562 | |
1c79356b | 1563 | for (pr = nd_prefix.lh_first; pr; pr = next) { |
9bccf70c A |
1564 | struct in6_ifaddr *ia, *ia_next; |
1565 | ||
1c79356b | 1566 | next = pr->ndpr_next; |
9bccf70c A |
1567 | |
1568 | if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) | |
1569 | continue; /* XXX */ | |
1570 | ||
1571 | /* do we really have to remove addresses as well? */ | |
91447636 | 1572 | for (ia = in6_ifaddrs; ia; ia = ia_next) { |
55e303ae | 1573 | /* ia might be removed. keep the next ptr. */ |
9bccf70c A |
1574 | ia_next = ia->ia_next; |
1575 | ||
1576 | if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) | |
1577 | continue; | |
1578 | ||
1579 | if (ia->ia6_ndpr == pr) | |
91447636 | 1580 | in6_purgeaddr(&ia->ia_ifa, 1); |
9bccf70c | 1581 | } |
91447636 | 1582 | prelist_remove(pr, 1); |
1c79356b | 1583 | } |
91447636 | 1584 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
1585 | break; |
1586 | } | |
1587 | case SIOCSRTRFLUSH_IN6: | |
1588 | { | |
1589 | /* flush all the default routers */ | |
2d21ac55 | 1590 | struct nd_defrouter *next; |
1c79356b | 1591 | |
91447636 | 1592 | lck_mtx_lock(nd6_mutex); |
1c79356b A |
1593 | if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) { |
1594 | /* | |
1595 | * The first entry of the list may be stored in | |
1596 | * the routing table, so we'll delete it later. | |
1597 | */ | |
1598 | for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) { | |
1599 | next = TAILQ_NEXT(dr, dr_entry); | |
91447636 | 1600 | defrtrlist_del(dr, 1); |
1c79356b | 1601 | } |
91447636 | 1602 | defrtrlist_del(TAILQ_FIRST(&nd_defrouter), 1); |
1c79356b | 1603 | } |
91447636 | 1604 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
1605 | break; |
1606 | } | |
1607 | case SIOCGNBRINFO_IN6: | |
1608 | { | |
1609 | struct llinfo_nd6 *ln; | |
1610 | struct in6_addr nb_addr = nbi->addr; /* make local for safety */ | |
1611 | ||
1612 | /* | |
1613 | * XXX: KAME specific hack for scoped addresses | |
1614 | * XXXX: for other scopes than link-local? | |
1615 | */ | |
1616 | if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) || | |
1617 | IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) { | |
1618 | u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2]; | |
1619 | ||
1620 | if (*idp == 0) | |
1621 | *idp = htons(ifp->if_index); | |
1622 | } | |
1623 | ||
91447636 | 1624 | if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) { |
1c79356b | 1625 | error = EINVAL; |
1c79356b A |
1626 | break; |
1627 | } | |
1628 | ln = (struct llinfo_nd6 *)rt->rt_llinfo; | |
1629 | nbi->state = ln->ln_state; | |
1630 | nbi->asked = ln->ln_asked; | |
1631 | nbi->isrouter = ln->ln_router; | |
1632 | nbi->expire = ln->ln_expire; | |
1c79356b A |
1633 | |
1634 | break; | |
1635 | } | |
1636 | case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ | |
1637 | ndif->ifindex = nd6_defifindex; | |
1638 | break; | |
1639 | case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ | |
1640 | return(nd6_setdefaultiface(ndif->ifindex)); | |
1641 | break; | |
1642 | } | |
1643 | return(error); | |
1644 | } | |
1645 | ||
1646 | /* | |
1647 | * Create neighbor cache entry and cache link-layer address, | |
1648 | * on reception of inbound ND6 packets. (RS/RA/NS/redirect) | |
1649 | */ | |
1650 | struct rtentry * | |
91447636 A |
1651 | nd6_cache_lladdr( |
1652 | struct ifnet *ifp, | |
1653 | struct in6_addr *from, | |
1654 | char *lladdr, | |
2d21ac55 | 1655 | __unused int lladdrlen, |
91447636 A |
1656 | int type, /* ICMP6 type */ |
1657 | int code) /* type dependent information */ | |
1c79356b A |
1658 | { |
1659 | struct rtentry *rt = NULL; | |
1660 | struct llinfo_nd6 *ln = NULL; | |
1661 | int is_newentry; | |
1662 | struct sockaddr_dl *sdl = NULL; | |
1663 | int do_update; | |
1664 | int olladdr; | |
1665 | int llchange; | |
1666 | int newstate = 0; | |
91447636 | 1667 | struct timeval timenow; |
1c79356b A |
1668 | |
1669 | if (!ifp) | |
1670 | panic("ifp == NULL in nd6_cache_lladdr"); | |
1671 | if (!from) | |
1672 | panic("from == NULL in nd6_cache_lladdr"); | |
1673 | ||
1674 | /* nothing must be updated for unspecified address */ | |
1675 | if (IN6_IS_ADDR_UNSPECIFIED(from)) | |
1676 | return NULL; | |
1677 | ||
1678 | /* | |
1679 | * Validation about ifp->if_addrlen and lladdrlen must be done in | |
1680 | * the caller. | |
1681 | * | |
1682 | * XXX If the link does not have link-layer adderss, what should | |
1683 | * we do? (ifp->if_addrlen == 0) | |
1684 | * Spec says nothing in sections for RA, RS and NA. There's small | |
1685 | * description on it in NS section (RFC 2461 7.2.3). | |
1686 | */ | |
91447636 | 1687 | getmicrotime(&timenow); |
1c79356b | 1688 | |
91447636 A |
1689 | lck_mtx_lock(rt_mtx); |
1690 | rt = nd6_lookup(from, 0, ifp, 1); | |
1c79356b A |
1691 | if (!rt) { |
1692 | #if 0 | |
1693 | /* nothing must be done if there's no lladdr */ | |
1694 | if (!lladdr || !lladdrlen) | |
1695 | return NULL; | |
1696 | #endif | |
1697 | ||
91447636 | 1698 | rt = nd6_lookup(from, 1, ifp, 1); |
1c79356b | 1699 | is_newentry = 1; |
9bccf70c A |
1700 | } else { |
1701 | /* do nothing if static ndp is set */ | |
91447636 A |
1702 | if (rt->rt_flags & RTF_STATIC) { |
1703 | lck_mtx_unlock(rt_mtx); | |
9bccf70c | 1704 | return NULL; |
91447636 | 1705 | } |
1c79356b | 1706 | is_newentry = 0; |
9bccf70c | 1707 | } |
1c79356b | 1708 | |
91447636 A |
1709 | lck_mtx_unlock(rt_mtx); |
1710 | ||
1711 | if (!rt) | |
1c79356b A |
1712 | return NULL; |
1713 | if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) { | |
1714 | fail: | |
9bccf70c | 1715 | (void)nd6_free(rt); |
1c79356b A |
1716 | return NULL; |
1717 | } | |
1718 | ln = (struct llinfo_nd6 *)rt->rt_llinfo; | |
1719 | if (!ln) | |
1720 | goto fail; | |
1721 | if (!rt->rt_gateway) | |
1722 | goto fail; | |
1723 | if (rt->rt_gateway->sa_family != AF_LINK) | |
1724 | goto fail; | |
1725 | sdl = SDL(rt->rt_gateway); | |
1726 | ||
1727 | olladdr = (sdl->sdl_alen) ? 1 : 0; | |
1728 | if (olladdr && lladdr) { | |
1729 | if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) | |
1730 | llchange = 1; | |
1731 | else | |
1732 | llchange = 0; | |
1733 | } else | |
1734 | llchange = 0; | |
1735 | ||
1736 | /* | |
1737 | * newentry olladdr lladdr llchange (*=record) | |
1738 | * 0 n n -- (1) | |
1739 | * 0 y n -- (2) | |
1740 | * 0 n y -- (3) * STALE | |
1741 | * 0 y y n (4) * | |
1742 | * 0 y y y (5) * STALE | |
1743 | * 1 -- n -- (6) NOSTATE(= PASSIVE) | |
1744 | * 1 -- y -- (7) * STALE | |
1745 | */ | |
1746 | ||
55e303ae | 1747 | if (lladdr) { /* (3-5) and (7) */ |
1c79356b A |
1748 | /* |
1749 | * Record source link-layer address | |
1750 | * XXX is it dependent to ifp->if_type? | |
1751 | */ | |
1752 | sdl->sdl_alen = ifp->if_addrlen; | |
1753 | bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); | |
1754 | } | |
1755 | ||
1756 | if (!is_newentry) { | |
55e303ae A |
1757 | if ((!olladdr && lladdr) /* (3) */ |
1758 | || (olladdr && lladdr && llchange)) { /* (5) */ | |
1c79356b A |
1759 | do_update = 1; |
1760 | newstate = ND6_LLINFO_STALE; | |
55e303ae | 1761 | } else /* (1-2,4) */ |
1c79356b A |
1762 | do_update = 0; |
1763 | } else { | |
1764 | do_update = 1; | |
55e303ae | 1765 | if (!lladdr) /* (6) */ |
1c79356b | 1766 | newstate = ND6_LLINFO_NOSTATE; |
55e303ae | 1767 | else /* (7) */ |
1c79356b A |
1768 | newstate = ND6_LLINFO_STALE; |
1769 | } | |
1770 | ||
1771 | if (do_update) { | |
1772 | /* | |
1773 | * Update the state of the neighbor cache. | |
1774 | */ | |
1775 | ln->ln_state = newstate; | |
1776 | ||
1777 | if (ln->ln_state == ND6_LLINFO_STALE) { | |
9bccf70c A |
1778 | /* |
1779 | * XXX: since nd6_output() below will cause | |
1780 | * state tansition to DELAY and reset the timer, | |
1781 | * we must set the timer now, although it is actually | |
1782 | * meaningless. | |
1783 | */ | |
91447636 | 1784 | ln->ln_expire = timenow.tv_sec + nd6_gctimer; |
9bccf70c | 1785 | |
1c79356b | 1786 | if (ln->ln_hold) { |
9bccf70c A |
1787 | /* |
1788 | * we assume ifp is not a p2p here, so just | |
1789 | * set the 2nd argument as the 1st one. | |
1790 | */ | |
1791 | nd6_output(ifp, ifp, ln->ln_hold, | |
1c79356b | 1792 | (struct sockaddr_in6 *)rt_key(rt), |
91447636 | 1793 | rt, 0); |
9bccf70c | 1794 | ln->ln_hold = NULL; |
1c79356b A |
1795 | } |
1796 | } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { | |
1797 | /* probe right away */ | |
91447636 | 1798 | ln->ln_expire = timenow.tv_sec; |
1c79356b A |
1799 | } |
1800 | } | |
1801 | ||
1802 | /* | |
1803 | * ICMP6 type dependent behavior. | |
1804 | * | |
1805 | * NS: clear IsRouter if new entry | |
1806 | * RS: clear IsRouter | |
1807 | * RA: set IsRouter if there's lladdr | |
1808 | * redir: clear IsRouter if new entry | |
1809 | * | |
1810 | * RA case, (1): | |
1811 | * The spec says that we must set IsRouter in the following cases: | |
1812 | * - If lladdr exist, set IsRouter. This means (1-5). | |
1813 | * - If it is old entry (!newentry), set IsRouter. This means (7). | |
1814 | * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. | |
1815 | * A quetion arises for (1) case. (1) case has no lladdr in the | |
1816 | * neighbor cache, this is similar to (6). | |
1817 | * This case is rare but we figured that we MUST NOT set IsRouter. | |
1818 | * | |
1819 | * newentry olladdr lladdr llchange NS RS RA redir | |
1820 | * D R | |
1821 | * 0 n n -- (1) c ? s | |
1822 | * 0 y n -- (2) c s s | |
1823 | * 0 n y -- (3) c s s | |
1824 | * 0 y y n (4) c s s | |
1825 | * 0 y y y (5) c s s | |
1826 | * 1 -- n -- (6) c c c s | |
1827 | * 1 -- y -- (7) c c s c s | |
1828 | * | |
1829 | * (c=clear s=set) | |
1830 | */ | |
1831 | switch (type & 0xff) { | |
1832 | case ND_NEIGHBOR_SOLICIT: | |
1833 | /* | |
1834 | * New entry must have is_router flag cleared. | |
1835 | */ | |
55e303ae | 1836 | if (is_newentry) /* (6-7) */ |
1c79356b A |
1837 | ln->ln_router = 0; |
1838 | break; | |
1839 | case ND_REDIRECT: | |
1840 | /* | |
1841 | * If the icmp is a redirect to a better router, always set the | |
1842 | * is_router flag. Otherwise, if the entry is newly created, | |
1843 | * clear the flag. [RFC 2461, sec 8.3] | |
1c79356b A |
1844 | */ |
1845 | if (code == ND_REDIRECT_ROUTER) | |
1846 | ln->ln_router = 1; | |
55e303ae | 1847 | else if (is_newentry) /* (6-7) */ |
1c79356b A |
1848 | ln->ln_router = 0; |
1849 | break; | |
1850 | case ND_ROUTER_SOLICIT: | |
1851 | /* | |
1852 | * is_router flag must always be cleared. | |
1853 | */ | |
1854 | ln->ln_router = 0; | |
1855 | break; | |
1856 | case ND_ROUTER_ADVERT: | |
1857 | /* | |
1858 | * Mark an entry with lladdr as a router. | |
1859 | */ | |
55e303ae A |
1860 | if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */ |
1861 | || (is_newentry && lladdr)) { /* (7) */ | |
1c79356b A |
1862 | ln->ln_router = 1; |
1863 | } | |
1864 | break; | |
1865 | } | |
1866 | ||
9bccf70c A |
1867 | /* |
1868 | * When the link-layer address of a router changes, select the | |
1869 | * best router again. In particular, when the neighbor entry is newly | |
1870 | * created, it might affect the selection policy. | |
1871 | * Question: can we restrict the first condition to the "is_newentry" | |
1872 | * case? | |
1873 | * XXX: when we hear an RA from a new router with the link-layer | |
1874 | * address option, defrouter_select() is called twice, since | |
1875 | * defrtrlist_update called the function as well. However, I believe | |
1876 | * we can compromise the overhead, since it only happens the first | |
1877 | * time. | |
1878 | * XXX: although defrouter_select() should not have a bad effect | |
1879 | * for those are not autoconfigured hosts, we explicitly avoid such | |
1880 | * cases for safety. | |
1881 | */ | |
91447636 A |
1882 | if (do_update && ln->ln_router && !ip6_forwarding && (ip6_accept_rtadv || (ifp->if_eflags & IFEF_ACCEPT_RTADVD))) { |
1883 | lck_mtx_lock(nd6_mutex); | |
9bccf70c | 1884 | defrouter_select(); |
91447636 A |
1885 | lck_mtx_unlock(nd6_mutex); |
1886 | } | |
9bccf70c | 1887 | |
1c79356b A |
1888 | return rt; |
1889 | } | |
1890 | ||
1891 | static void | |
91447636 | 1892 | nd6_slowtimo( |
2d21ac55 | 1893 | __unused void *ignored_arg) |
1c79356b | 1894 | { |
9bccf70c A |
1895 | int i; |
1896 | struct nd_ifinfo *nd6if; | |
1c79356b | 1897 | |
91447636 | 1898 | lck_mtx_lock(nd6_mutex); |
1c79356b | 1899 | for (i = 1; i < if_index + 1; i++) { |
9bccf70c A |
1900 | if (!nd_ifinfo || i >= nd_ifinfo_indexlim) |
1901 | continue; | |
1c79356b A |
1902 | nd6if = &nd_ifinfo[i]; |
1903 | if (nd6if->basereachable && /* already initialized */ | |
1904 | (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { | |
1905 | /* | |
1906 | * Since reachable time rarely changes by router | |
1907 | * advertisements, we SHOULD insure that a new random | |
1908 | * value gets recomputed at least once every few hours. | |
1909 | * (RFC 2461, 6.3.4) | |
1910 | */ | |
1911 | nd6if->recalctm = nd6_recalc_reachtm_interval; | |
1912 | nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); | |
1913 | } | |
1914 | } | |
91447636 A |
1915 | lck_mtx_unlock(nd6_mutex); |
1916 | timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz); | |
9bccf70c | 1917 | } |
1c79356b | 1918 | |
1c79356b A |
1919 | |
1920 | #define senderr(e) { error = (e); goto bad;} | |
1921 | int | |
91447636 A |
1922 | nd6_output( |
1923 | struct ifnet *ifp, | |
1924 | struct ifnet *origifp, | |
1925 | struct mbuf *m0, | |
1926 | struct sockaddr_in6 *dst, | |
1927 | struct rtentry *rt0, | |
1928 | int locked) | |
1c79356b | 1929 | { |
9bccf70c A |
1930 | struct mbuf *m = m0; |
1931 | struct rtentry *rt = rt0; | |
1932 | struct sockaddr_in6 *gw6 = NULL; | |
1c79356b A |
1933 | struct llinfo_nd6 *ln = NULL; |
1934 | int error = 0; | |
91447636 | 1935 | struct timeval timenow; |
1c79356b A |
1936 | |
1937 | if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) | |
1938 | goto sendpkt; | |
1939 | ||
9bccf70c | 1940 | if (nd6_need_cache(ifp) == 0) |
1c79356b A |
1941 | goto sendpkt; |
1942 | ||
1943 | /* | |
55e303ae | 1944 | * next hop determination. This routine is derived from ether_outpout. |
1c79356b | 1945 | */ |
91447636 | 1946 | lck_mtx_lock(rt_mtx); |
1c79356b A |
1947 | if (rt) { |
1948 | if ((rt->rt_flags & RTF_UP) == 0) { | |
91447636 | 1949 | if ((rt0 = rt = rtalloc1_locked((struct sockaddr *)dst, 1, 0UL)) != |
1c79356b | 1950 | NULL) |
1c79356b | 1951 | { |
9bccf70c A |
1952 | rtunref(rt); |
1953 | if (rt->rt_ifp != ifp) { | |
1954 | /* XXX: loop care? */ | |
91447636 | 1955 | lck_mtx_unlock(rt_mtx); |
9bccf70c | 1956 | return nd6_output(ifp, origifp, m0, |
91447636 | 1957 | dst, rt, locked); |
9bccf70c | 1958 | } |
91447636 A |
1959 | } else { |
1960 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 1961 | senderr(EHOSTUNREACH); |
91447636 | 1962 | } |
1c79356b | 1963 | } |
9bccf70c | 1964 | |
1c79356b | 1965 | if (rt->rt_flags & RTF_GATEWAY) { |
9bccf70c A |
1966 | gw6 = (struct sockaddr_in6 *)rt->rt_gateway; |
1967 | ||
1968 | /* | |
1969 | * We skip link-layer address resolution and NUD | |
1970 | * if the gateway is not a neighbor from ND point | |
55e303ae A |
1971 | * of view, regardless of the value of nd_ifinfo.flags. |
1972 | * The second condition is a bit tricky; we skip | |
9bccf70c A |
1973 | * if the gateway is our own address, which is |
1974 | * sometimes used to install a route to a p2p link. | |
1975 | */ | |
91447636 | 1976 | if (!nd6_is_addr_neighbor(gw6, ifp, 1) || |
9bccf70c A |
1977 | in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) { |
1978 | /* | |
1979 | * We allow this kind of tricky route only | |
1980 | * when the outgoing interface is p2p. | |
1981 | * XXX: we may need a more generic rule here. | |
1982 | */ | |
91447636 | 1983 | lck_mtx_unlock(rt_mtx); |
9bccf70c A |
1984 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) |
1985 | senderr(EHOSTUNREACH); | |
1986 | ||
1987 | goto sendpkt; | |
1988 | } | |
1989 | ||
1c79356b A |
1990 | if (rt->rt_gwroute == 0) |
1991 | goto lookup; | |
1992 | if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { | |
91447636 A |
1993 | rtfree_locked(rt); rt = rt0; |
1994 | lookup: rt->rt_gwroute = rtalloc1_locked(rt->rt_gateway, 1, 0UL); | |
1995 | if ((rt = rt->rt_gwroute) == 0) { | |
1996 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 1997 | senderr(EHOSTUNREACH); |
91447636 | 1998 | } |
1c79356b A |
1999 | } |
2000 | } | |
1c79356b A |
2001 | } |
2002 | ||
2003 | /* | |
2004 | * Address resolution or Neighbor Unreachability Detection | |
2005 | * for the next hop. | |
2006 | * At this point, the destination of the packet must be a unicast | |
2007 | * or an anycast address(i.e. not a multicast). | |
2008 | */ | |
2009 | ||
2010 | /* Look up the neighbor cache for the nexthop */ | |
2011 | if (rt && (rt->rt_flags & RTF_LLINFO) != 0) | |
2012 | ln = (struct llinfo_nd6 *)rt->rt_llinfo; | |
2013 | else { | |
9bccf70c A |
2014 | /* |
2015 | * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), | |
55e303ae | 2016 | * the condition below is not very efficient. But we believe |
9bccf70c A |
2017 | * it is tolerable, because this should be a rare case. |
2018 | */ | |
91447636 A |
2019 | if (nd6_is_addr_neighbor(dst, ifp, 1) && |
2020 | (rt = nd6_lookup(&dst->sin6_addr, 1, ifp, 1)) != NULL) | |
1c79356b A |
2021 | ln = (struct llinfo_nd6 *)rt->rt_llinfo; |
2022 | } | |
91447636 | 2023 | lck_mtx_unlock(rt_mtx); |
1c79356b | 2024 | if (!ln || !rt) { |
9bccf70c A |
2025 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && |
2026 | !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) { | |
2027 | log(LOG_DEBUG, | |
2028 | "nd6_output: can't allocate llinfo for %s " | |
2029 | "(ln=%p, rt=%p)\n", | |
2030 | ip6_sprintf(&dst->sin6_addr), ln, rt); | |
2031 | senderr(EIO); /* XXX: good error? */ | |
2032 | } | |
2033 | ||
2034 | goto sendpkt; /* send anyway */ | |
1c79356b A |
2035 | } |
2036 | ||
91447636 A |
2037 | getmicrotime(&timenow); |
2038 | ||
1c79356b A |
2039 | /* We don't have to do link-layer address resolution on a p2p link. */ |
2040 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && | |
9bccf70c | 2041 | ln->ln_state < ND6_LLINFO_REACHABLE) { |
1c79356b | 2042 | ln->ln_state = ND6_LLINFO_STALE; |
91447636 | 2043 | ln->ln_expire = timenow.tv_sec + nd6_gctimer; |
9bccf70c | 2044 | } |
1c79356b A |
2045 | |
2046 | /* | |
2047 | * The first time we send a packet to a neighbor whose entry is | |
2048 | * STALE, we have to change the state to DELAY and a sets a timer to | |
2049 | * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do | |
2050 | * neighbor unreachability detection on expiration. | |
2051 | * (RFC 2461 7.3.3) | |
2052 | */ | |
2053 | if (ln->ln_state == ND6_LLINFO_STALE) { | |
2054 | ln->ln_asked = 0; | |
2055 | ln->ln_state = ND6_LLINFO_DELAY; | |
91447636 | 2056 | ln->ln_expire = timenow.tv_sec + nd6_delay; |
1c79356b A |
2057 | } |
2058 | ||
2059 | /* | |
2060 | * If the neighbor cache entry has a state other than INCOMPLETE | |
55e303ae | 2061 | * (i.e. its link-layer address is already resolved), just |
1c79356b A |
2062 | * send the packet. |
2063 | */ | |
2064 | if (ln->ln_state > ND6_LLINFO_INCOMPLETE) | |
2065 | goto sendpkt; | |
2066 | ||
2067 | /* | |
2068 | * There is a neighbor cache entry, but no ethernet address | |
55e303ae | 2069 | * response yet. Replace the held mbuf (if any) with this |
1c79356b A |
2070 | * latest one. |
2071 | * | |
55e303ae A |
2072 | * This code conforms to the rate-limiting rule described in Section |
2073 | * 7.2.2 of RFC 2461, because the timer is set correctly after sending | |
2074 | * an NS below. | |
1c79356b | 2075 | */ |
9bccf70c | 2076 | if (ln->ln_state == ND6_LLINFO_NOSTATE) |
1c79356b A |
2077 | ln->ln_state = ND6_LLINFO_INCOMPLETE; |
2078 | if (ln->ln_hold) | |
2079 | m_freem(ln->ln_hold); | |
2080 | ln->ln_hold = m; | |
2081 | if (ln->ln_expire) { | |
1c79356b | 2082 | if (ln->ln_asked < nd6_mmaxtries && |
91447636 | 2083 | ln->ln_expire < timenow.tv_sec) { |
1c79356b | 2084 | ln->ln_asked++; |
91447636 | 2085 | ln->ln_expire = timenow.tv_sec + |
1c79356b | 2086 | nd_ifinfo[ifp->if_index].retrans / 1000; |
91447636 | 2087 | nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0, locked); |
1c79356b A |
2088 | } |
2089 | } | |
2090 | return(0); | |
2091 | ||
2092 | sendpkt: | |
2093 | #ifdef __APPLE__ | |
9bccf70c A |
2094 | |
2095 | /* Make sure the HW checksum flags are cleaned before sending the packet */ | |
2096 | ||
9bccf70c A |
2097 | m->m_pkthdr.csum_data = 0; |
2098 | m->m_pkthdr.csum_flags = 0; | |
2099 | ||
2100 | if ((ifp->if_flags & IFF_LOOPBACK) != 0) { | |
55e303ae | 2101 | m->m_pkthdr.rcvif = origifp; /* forwarding rules require the original scope_id */ |
91447636 A |
2102 | if (locked) |
2103 | lck_mtx_unlock(ip6_mutex); | |
2104 | error = dlil_output(origifp, PF_INET6, m, (caddr_t)rt, (struct sockaddr *)dst, 0); | |
2105 | if (locked) | |
2106 | lck_mtx_lock(ip6_mutex); | |
2107 | return error; | |
e5568f75 A |
2108 | } else { |
2109 | /* Do not allow loopback address to wind up on a wire */ | |
2110 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
2111 | ||
2112 | if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || | |
2113 | IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst))) { | |
2114 | ip6stat.ip6s_badscope++; | |
2115 | /* | |
91447636 A |
2116 | * Do not simply drop the packet just like a firewall -- we want the |
2117 | * the application to feel the pain. | |
2118 | * Return ENETUNREACH like ip6_output does in some similar cases. | |
2119 | * This can startle the otherwise clueless process that specifies | |
e5568f75 A |
2120 | * loopback as the source address. |
2121 | */ | |
91447636 | 2122 | error = ENETUNREACH; |
e5568f75 A |
2123 | goto bad; |
2124 | } | |
9bccf70c A |
2125 | } |
2126 | ||
91447636 A |
2127 | m->m_pkthdr.rcvif = 0; |
2128 | if (locked) | |
2129 | lck_mtx_unlock(ip6_mutex); | |
2130 | error = dlil_output(ifp, PF_INET6, m, (caddr_t)rt, (struct sockaddr *)dst, 0); | |
2131 | if (locked) | |
2132 | lck_mtx_lock(ip6_mutex); | |
2133 | return(error); | |
1c79356b | 2134 | #else |
9bccf70c A |
2135 | if ((ifp->if_flags & IFF_LOOPBACK) != 0) { |
2136 | return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst, | |
2137 | rt)); | |
2138 | } | |
1c79356b A |
2139 | return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt)); |
2140 | #endif | |
2141 | ||
2142 | bad: | |
2143 | if (m) | |
2144 | m_freem(m); | |
2145 | return (error); | |
2146 | } | |
2147 | #undef senderr | |
2148 | ||
9bccf70c | 2149 | int |
91447636 A |
2150 | nd6_need_cache( |
2151 | struct ifnet *ifp) | |
9bccf70c A |
2152 | { |
2153 | /* | |
2154 | * XXX: we currently do not make neighbor cache on any interface | |
2155 | * other than ARCnet, Ethernet, FDDI and GIF. | |
2156 | * | |
2157 | * RFC2893 says: | |
2158 | * - unidirectional tunnels needs no ND | |
2159 | */ | |
2160 | switch (ifp->if_type) { | |
2161 | case IFT_ARCNET: | |
2162 | case IFT_ETHER: | |
2163 | case IFT_FDDI: | |
2164 | case IFT_IEEE1394: | |
9bccf70c | 2165 | case IFT_L2VLAN: |
91447636 | 2166 | case IFT_IEEE8023ADLAG: |
9bccf70c A |
2167 | #if IFT_IEEE80211 |
2168 | case IFT_IEEE80211: | |
2169 | #endif | |
2170 | case IFT_GIF: /* XXX need more cases? */ | |
2171 | return(1); | |
2172 | default: | |
2173 | return(0); | |
2174 | } | |
2175 | } | |
2176 | ||
1c79356b | 2177 | int |
91447636 A |
2178 | nd6_storelladdr( |
2179 | struct ifnet *ifp, | |
2180 | struct rtentry *rt, | |
2181 | struct mbuf *m, | |
2182 | struct sockaddr *dst, | |
2183 | u_char *desten) | |
1c79356b | 2184 | { |
9bccf70c | 2185 | int i; |
1c79356b A |
2186 | struct sockaddr_dl *sdl; |
2187 | ||
2188 | if (m->m_flags & M_MCAST) { | |
2189 | switch (ifp->if_type) { | |
2190 | case IFT_ETHER: | |
9bccf70c | 2191 | case IFT_FDDI: |
91447636 A |
2192 | case IFT_L2VLAN: |
2193 | case IFT_IEEE8023ADLAG: | |
9bccf70c A |
2194 | #if IFT_IEEE80211 |
2195 | case IFT_IEEE80211: | |
2196 | #endif | |
1c79356b A |
2197 | ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, |
2198 | desten); | |
2199 | return(1); | |
9bccf70c A |
2200 | case IFT_IEEE1394: |
2201 | for (i = 0; i < ifp->if_addrlen; i++) | |
2202 | desten[i] = ~0; | |
2203 | return(1); | |
1c79356b A |
2204 | case IFT_ARCNET: |
2205 | *desten = 0; | |
2206 | return(1); | |
2207 | default: | |
55e303ae | 2208 | return(0); /* caller will free mbuf */ |
1c79356b A |
2209 | } |
2210 | } | |
2211 | ||
9bccf70c A |
2212 | if (rt == NULL) { |
2213 | /* this could happen, if we could not allocate memory */ | |
55e303ae | 2214 | return(0); /* caller will free mbuf */ |
9bccf70c A |
2215 | } |
2216 | if (rt->rt_gateway->sa_family != AF_LINK) { | |
1c79356b | 2217 | printf("nd6_storelladdr: something odd happens\n"); |
55e303ae | 2218 | return(0); /* caller will free mbuf */ |
1c79356b A |
2219 | } |
2220 | sdl = SDL(rt->rt_gateway); | |
2221 | if (sdl->sdl_alen == 0) { | |
2222 | /* this should be impossible, but we bark here for debugging */ | |
2223 | printf("nd6_storelladdr: sdl_alen == 0\n"); | |
55e303ae | 2224 | return(0); /* caller will free mbuf */ |
1c79356b A |
2225 | } |
2226 | ||
2227 | bcopy(LLADDR(sdl), desten, sdl->sdl_alen); | |
2228 | return(1); | |
2229 | } | |
91447636 A |
2230 | |
2231 | extern errno_t arp_route_to_gateway_route(const struct sockaddr *net_dest, | |
2232 | route_t hint, route_t *out_route); | |
2233 | ||
2234 | errno_t | |
2235 | nd6_lookup_ipv6( | |
2236 | ifnet_t ifp, | |
2237 | const struct sockaddr_in6 *ip6_dest, | |
2238 | struct sockaddr_dl *ll_dest, | |
2239 | size_t ll_dest_len, | |
2240 | route_t hint, | |
2241 | mbuf_t packet) | |
2242 | { | |
2243 | route_t route = hint; | |
2244 | errno_t result = 0; | |
2245 | struct sockaddr_dl *sdl = NULL; | |
2246 | size_t copy_len; | |
2247 | ||
2248 | if (ip6_dest->sin6_family != AF_INET6) | |
2249 | return EAFNOSUPPORT; | |
2250 | ||
2251 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) | |
2252 | return ENETDOWN; | |
2253 | ||
2254 | if (hint) { | |
2255 | result = arp_route_to_gateway_route((const struct sockaddr*)ip6_dest, hint, &route); | |
2256 | if (result != 0) | |
2257 | return result; | |
2258 | } | |
2259 | ||
2260 | if ((packet->m_flags & M_MCAST) != 0) { | |
2261 | return dlil_resolve_multi(ifp, (const struct sockaddr*)ip6_dest, | |
2d21ac55 | 2262 | (struct sockaddr *)ll_dest, ll_dest_len); |
91447636 A |
2263 | } |
2264 | ||
2265 | if (route == NULL) { | |
2266 | /* this could happen, if we could not allocate memory */ | |
2267 | return ENOBUFS; | |
2268 | } | |
2269 | ||
2270 | lck_mtx_lock(rt_mtx); | |
2271 | ||
2272 | if (route->rt_gateway->sa_family != AF_LINK) { | |
2273 | printf("nd6_lookup_ipv6: gateway address not AF_LINK\n"); | |
2274 | result = EADDRNOTAVAIL; | |
2275 | goto done; | |
2276 | } | |
2277 | ||
2278 | sdl = SDL(route->rt_gateway); | |
2279 | if (sdl->sdl_alen == 0) { | |
2280 | /* this should be impossible, but we bark here for debugging */ | |
2281 | printf("nd6_storelladdr: sdl_alen == 0\n"); | |
2282 | result = EHOSTUNREACH; | |
2283 | } | |
2284 | ||
2285 | copy_len = sdl->sdl_len <= ll_dest_len ? sdl->sdl_len : ll_dest_len; | |
2286 | bcopy(sdl, ll_dest, copy_len); | |
2287 | ||
2288 | done: | |
2289 | lck_mtx_unlock(rt_mtx); | |
2290 | return result; | |
2291 | } | |
2292 | ||
9bccf70c | 2293 | SYSCTL_DECL(_net_inet6_icmp6); |
9bccf70c A |
2294 | |
2295 | static int | |
2296 | nd6_sysctl_drlist SYSCTL_HANDLER_ARGS | |
2297 | { | |
2d21ac55 | 2298 | #pragma unused(oidp, arg1, arg2) |
9bccf70c A |
2299 | int error; |
2300 | char buf[1024]; | |
2301 | struct in6_defrouter *d, *de; | |
2302 | struct nd_defrouter *dr; | |
2303 | ||
2304 | if (req->newptr) | |
2305 | return EPERM; | |
2306 | error = 0; | |
2307 | ||
91447636 | 2308 | lck_mtx_lock(nd6_mutex); |
9bccf70c A |
2309 | for (dr = TAILQ_FIRST(&nd_defrouter); |
2310 | dr; | |
2311 | dr = TAILQ_NEXT(dr, dr_entry)) { | |
2312 | d = (struct in6_defrouter *)buf; | |
2313 | de = (struct in6_defrouter *)(buf + sizeof(buf)); | |
2314 | ||
2315 | if (d + 1 <= de) { | |
2316 | bzero(d, sizeof(*d)); | |
2317 | d->rtaddr.sin6_family = AF_INET6; | |
2318 | d->rtaddr.sin6_len = sizeof(d->rtaddr); | |
2319 | if (in6_recoverscope(&d->rtaddr, &dr->rtaddr, | |
2320 | dr->ifp) != 0) | |
2321 | log(LOG_ERR, | |
2322 | "scope error in " | |
2323 | "default router list (%s)\n", | |
2324 | ip6_sprintf(&dr->rtaddr)); | |
2325 | d->flags = dr->flags; | |
2326 | d->rtlifetime = dr->rtlifetime; | |
2327 | d->expire = dr->expire; | |
2328 | d->if_index = dr->ifp->if_index; | |
2329 | } else | |
2330 | panic("buffer too short"); | |
2331 | ||
2332 | error = SYSCTL_OUT(req, buf, sizeof(*d)); | |
2333 | if (error) | |
2334 | break; | |
2335 | } | |
91447636 | 2336 | lck_mtx_unlock(nd6_mutex); |
9bccf70c A |
2337 | return error; |
2338 | } | |
2339 | ||
2340 | static int | |
2341 | nd6_sysctl_prlist SYSCTL_HANDLER_ARGS | |
2342 | { | |
2d21ac55 | 2343 | #pragma unused(oidp, arg1, arg2) |
9bccf70c A |
2344 | int error; |
2345 | char buf[1024]; | |
2346 | struct in6_prefix *p, *pe; | |
2347 | struct nd_prefix *pr; | |
2348 | ||
2349 | if (req->newptr) | |
2350 | return EPERM; | |
2351 | error = 0; | |
2352 | ||
91447636 A |
2353 | lck_mtx_lock(nd6_mutex); |
2354 | ||
9bccf70c | 2355 | for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { |
2d21ac55 | 2356 | u_short advrtrs = 0; |
9bccf70c A |
2357 | size_t advance; |
2358 | struct sockaddr_in6 *sin6, *s6; | |
2359 | struct nd_pfxrouter *pfr; | |
2360 | ||
2361 | p = (struct in6_prefix *)buf; | |
2362 | pe = (struct in6_prefix *)(buf + sizeof(buf)); | |
2363 | ||
2364 | if (p + 1 <= pe) { | |
2365 | bzero(p, sizeof(*p)); | |
2366 | sin6 = (struct sockaddr_in6 *)(p + 1); | |
2367 | ||
2368 | p->prefix = pr->ndpr_prefix; | |
2369 | if (in6_recoverscope(&p->prefix, | |
2370 | &p->prefix.sin6_addr, pr->ndpr_ifp) != 0) | |
2371 | log(LOG_ERR, | |
2372 | "scope error in prefix list (%s)\n", | |
2373 | ip6_sprintf(&p->prefix.sin6_addr)); | |
2374 | p->raflags = pr->ndpr_raf; | |
2375 | p->prefixlen = pr->ndpr_plen; | |
2376 | p->vltime = pr->ndpr_vltime; | |
2377 | p->pltime = pr->ndpr_pltime; | |
2378 | p->if_index = pr->ndpr_ifp->if_index; | |
2379 | p->expire = pr->ndpr_expire; | |
2380 | p->refcnt = pr->ndpr_refcnt; | |
2381 | p->flags = pr->ndpr_stateflags; | |
2382 | p->origin = PR_ORIG_RA; | |
2383 | advrtrs = 0; | |
2384 | for (pfr = pr->ndpr_advrtrs.lh_first; | |
2385 | pfr; | |
2386 | pfr = pfr->pfr_next) { | |
2387 | if ((void *)&sin6[advrtrs + 1] > | |
2388 | (void *)pe) { | |
2389 | advrtrs++; | |
2390 | continue; | |
2391 | } | |
2392 | s6 = &sin6[advrtrs]; | |
2393 | bzero(s6, sizeof(*s6)); | |
2394 | s6->sin6_family = AF_INET6; | |
2395 | s6->sin6_len = sizeof(*sin6); | |
2396 | if (in6_recoverscope(s6, | |
2397 | &pfr->router->rtaddr, | |
2398 | pfr->router->ifp) != 0) | |
2399 | log(LOG_ERR, | |
2400 | "scope error in " | |
2401 | "prefix list (%s)\n", | |
2402 | ip6_sprintf(&pfr->router->rtaddr)); | |
2403 | advrtrs++; | |
2404 | } | |
2405 | p->advrtrs = advrtrs; | |
2406 | } else | |
2407 | panic("buffer too short"); | |
2408 | ||
2409 | advance = sizeof(*p) + sizeof(*sin6) * advrtrs; | |
2410 | error = SYSCTL_OUT(req, buf, advance); | |
2411 | if (error) | |
2412 | break; | |
2413 | } | |
91447636 | 2414 | lck_mtx_unlock(nd6_mutex); |
9bccf70c A |
2415 | return error; |
2416 | } | |
2d21ac55 A |
2417 | SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist, |
2418 | CTLFLAG_RD, 0, 0, nd6_sysctl_drlist, "S,in6_defrouter",""); | |
2419 | SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, | |
2420 | CTLFLAG_RD, 0, 0, nd6_sysctl_prlist, "S,in6_defrouter",""); | |
2421 |