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