Sun Sep 1 11:00:31 2019 UTC ()
Pull up following revision(s) (requested by roy in ticket #131):

	sys/netinet6/nd6.c: revision 1.260

inet6: nd6_free assumes all routers are processed by kernel RA

This hasn't been the case for a long time if you're a dhcpcd
user with a default config. As such, it's possible for the default
IPv6 router as set by dhcpcd could be erroneously gc'ed by nd6_free.

This reduces the scope of the ND6_WLOCK taken as well as fixing an
issue where we write to ln->ln_state without a lock being held.


(martin)
diff -r1.256.2.2 -r1.256.2.3 src/sys/netinet6/nd6.c

cvs diff -r1.256.2.2 -r1.256.2.3 src/sys/netinet6/nd6.c (switch to unified diff)

--- src/sys/netinet6/nd6.c 2019/08/26 13:42:36 1.256.2.2
+++ src/sys/netinet6/nd6.c 2019/09/01 11:00:31 1.256.2.3
@@ -1,2293 +1,2280 @@ @@ -1,2293 +1,2280 @@
1/* $NetBSD: nd6.c,v 1.256.2.2 2019/08/26 13:42:36 martin Exp $ */ 1/* $NetBSD: nd6.c,v 1.256.2.3 2019/09/01 11:00:31 martin Exp $ */
2/* $KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $ */ 2/* $KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $ */
3 3
4/* 4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors 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 17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission. 18 * without specific prior written permission.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 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 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE. 30 * SUCH DAMAGE.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.256.2.2 2019/08/26 13:42:36 martin Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.256.2.3 2019/09/01 11:00:31 martin Exp $");
35 35
36#ifdef _KERNEL_OPT 36#ifdef _KERNEL_OPT
37#include "opt_net_mpsafe.h" 37#include "opt_net_mpsafe.h"
38#endif 38#endif
39 39
40#include "bridge.h" 40#include "bridge.h"
41#include "carp.h" 41#include "carp.h"
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/systm.h> 44#include <sys/systm.h>
45#include <sys/callout.h> 45#include <sys/callout.h>
46#include <sys/kmem.h> 46#include <sys/kmem.h>
47#include <sys/mbuf.h> 47#include <sys/mbuf.h>
48#include <sys/socket.h> 48#include <sys/socket.h>
49#include <sys/socketvar.h> 49#include <sys/socketvar.h>
50#include <sys/sockio.h> 50#include <sys/sockio.h>
51#include <sys/time.h> 51#include <sys/time.h>
52#include <sys/kernel.h> 52#include <sys/kernel.h>
53#include <sys/errno.h> 53#include <sys/errno.h>
54#include <sys/ioctl.h> 54#include <sys/ioctl.h>
55#include <sys/syslog.h> 55#include <sys/syslog.h>
56#include <sys/queue.h> 56#include <sys/queue.h>
57#include <sys/cprng.h> 57#include <sys/cprng.h>
58#include <sys/workqueue.h> 58#include <sys/workqueue.h>
59 59
60#include <net/if.h> 60#include <net/if.h>
61#include <net/if_dl.h> 61#include <net/if_dl.h>
62#include <net/if_llatbl.h> 62#include <net/if_llatbl.h>
63#include <net/if_types.h> 63#include <net/if_types.h>
64#include <net/route.h> 64#include <net/route.h>
65#include <net/if_ether.h> 65#include <net/if_ether.h>
66#include <net/if_fddi.h> 66#include <net/if_fddi.h>
67#include <net/if_arc.h> 67#include <net/if_arc.h>
68 68
69#include <netinet/in.h> 69#include <netinet/in.h>
70#include <netinet6/in6_var.h> 70#include <netinet6/in6_var.h>
71#include <netinet/ip6.h> 71#include <netinet/ip6.h>
72#include <netinet6/ip6_var.h> 72#include <netinet6/ip6_var.h>
73#include <netinet6/scope6_var.h> 73#include <netinet6/scope6_var.h>
74#include <netinet6/nd6.h> 74#include <netinet6/nd6.h>
75#include <netinet6/in6_ifattach.h> 75#include <netinet6/in6_ifattach.h>
76#include <netinet/icmp6.h> 76#include <netinet/icmp6.h>
77#include <netinet6/icmp6_private.h> 77#include <netinet6/icmp6_private.h>
78 78
79#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ 79#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
80#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ 80#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
81 81
82/* timer values */ 82/* timer values */
83int nd6_prune = 1; /* walk list every 1 seconds */ 83int nd6_prune = 1; /* walk list every 1 seconds */
84int nd6_delay = 5; /* delay first probe time 5 second */ 84int nd6_delay = 5; /* delay first probe time 5 second */
85int nd6_umaxtries = 3; /* maximum unicast query */ 85int nd6_umaxtries = 3; /* maximum unicast query */
86int nd6_mmaxtries = 3; /* maximum multicast query */ 86int nd6_mmaxtries = 3; /* maximum multicast query */
87int nd6_useloopback = 1; /* use loopback interface for local traffic */ 87int nd6_useloopback = 1; /* use loopback interface for local traffic */
88int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */ 88int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
89 89
90/* preventing too many loops in ND option parsing */ 90/* preventing too many loops in ND option parsing */
91int nd6_maxndopt = 10; /* max # of ND options allowed */ 91int nd6_maxndopt = 10; /* max # of ND options allowed */
92 92
93int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */ 93int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
94 94
95int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */ 95int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
96 96
97#ifdef ND6_DEBUG 97#ifdef ND6_DEBUG
98int nd6_debug = 1; 98int nd6_debug = 1;
99#else 99#else
100int nd6_debug = 0; 100int nd6_debug = 0;
101#endif 101#endif
102 102
103krwlock_t nd6_lock __cacheline_aligned; 103krwlock_t nd6_lock __cacheline_aligned;
104 104
105struct nd_drhead nd_defrouter; 105struct nd_drhead nd_defrouter;
106struct nd_prhead nd_prefix = { 0 }; 106struct nd_prhead nd_prefix = { 0 };
107 107
108int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL; 108int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
109 109
110static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *); 110static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
111static void nd6_slowtimo(void *); 111static void nd6_slowtimo(void *);
112static int regen_tmpaddr(const struct in6_ifaddr *); 112static int regen_tmpaddr(const struct in6_ifaddr *);
113static void nd6_free(struct llentry *, int); 113static void nd6_free(struct llentry *, int);
114static void nd6_llinfo_timer(void *); 114static void nd6_llinfo_timer(void *);
115static void nd6_timer(void *); 115static void nd6_timer(void *);
116static void nd6_timer_work(struct work *, void *); 116static void nd6_timer_work(struct work *, void *);
117static void clear_llinfo_pqueue(struct llentry *); 117static void clear_llinfo_pqueue(struct llentry *);
118static struct nd_opt_hdr *nd6_option(union nd_opts *); 118static struct nd_opt_hdr *nd6_option(union nd_opts *);
119 119
120static callout_t nd6_slowtimo_ch; 120static callout_t nd6_slowtimo_ch;
121static callout_t nd6_timer_ch; 121static callout_t nd6_timer_ch;
122static struct workqueue *nd6_timer_wq; 122static struct workqueue *nd6_timer_wq;
123static struct work nd6_timer_wk; 123static struct work nd6_timer_wk;
124 124
125static int fill_drlist(void *, size_t *); 125static int fill_drlist(void *, size_t *);
126static int fill_prlist(void *, size_t *); 126static int fill_prlist(void *, size_t *);
127 127
128static struct ifnet *nd6_defifp; 128static struct ifnet *nd6_defifp;
129static int nd6_defifindex; 129static int nd6_defifindex;
130 130
131static int nd6_setdefaultiface(int); 131static int nd6_setdefaultiface(int);
132 132
133MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery"); 133MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
134 134
135void 135void
136nd6_init(void) 136nd6_init(void)
137{ 137{
138 int error; 138 int error;
139 139
140 rw_init(&nd6_lock); 140 rw_init(&nd6_lock);
141 141
142 /* initialization of the default router list */ 142 /* initialization of the default router list */
143 ND_DEFROUTER_LIST_INIT(); 143 ND_DEFROUTER_LIST_INIT();
144 144
145 callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE); 145 callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
146 callout_init(&nd6_timer_ch, CALLOUT_MPSAFE); 146 callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
147 147
148 error = workqueue_create(&nd6_timer_wq, "nd6_timer", 148 error = workqueue_create(&nd6_timer_wq, "nd6_timer",
149 nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE); 149 nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
150 if (error) 150 if (error)
151 panic("%s: workqueue_create failed (%d)\n", __func__, error); 151 panic("%s: workqueue_create failed (%d)\n", __func__, error);
152 152
153 /* start timer */ 153 /* start timer */
154 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 154 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
155 nd6_slowtimo, NULL); 155 nd6_slowtimo, NULL);
156 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 156 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
157} 157}
158 158
159struct nd_ifinfo * 159struct nd_ifinfo *
160nd6_ifattach(struct ifnet *ifp) 160nd6_ifattach(struct ifnet *ifp)
161{ 161{
162 struct nd_ifinfo *nd; 162 struct nd_ifinfo *nd;
163 163
164 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP); 164 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
165 165
166 nd->initialized = 1; 166 nd->initialized = 1;
167 167
168 nd->chlim = IPV6_DEFHLIM; 168 nd->chlim = IPV6_DEFHLIM;
169 nd->basereachable = REACHABLE_TIME; 169 nd->basereachable = REACHABLE_TIME;
170 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable); 170 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
171 nd->retrans = RETRANS_TIMER; 171 nd->retrans = RETRANS_TIMER;
172 172
173 nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV; 173 nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
174 174
175 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL. 175 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
176 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL 176 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
177 * because one of its members should. */ 177 * because one of its members should. */
178 if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) || 178 if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
179 (ifp->if_flags & IFF_LOOPBACK)) 179 (ifp->if_flags & IFF_LOOPBACK))
180 nd->flags |= ND6_IFF_AUTO_LINKLOCAL; 180 nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
181 181
182 /* A loopback interface does not need to accept RTADV. 182 /* A loopback interface does not need to accept RTADV.
183 * A bridge interface should not accept RTADV 183 * A bridge interface should not accept RTADV
184 * because one of its members should. */ 184 * because one of its members should. */
185 if (ip6_accept_rtadv && 185 if (ip6_accept_rtadv &&
186 !(ifp->if_flags & IFF_LOOPBACK) && 186 !(ifp->if_flags & IFF_LOOPBACK) &&
187 !(ifp->if_type != IFT_BRIDGE)) 187 !(ifp->if_type != IFT_BRIDGE))
188 nd->flags |= ND6_IFF_ACCEPT_RTADV; 188 nd->flags |= ND6_IFF_ACCEPT_RTADV;
189 189
190 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */ 190 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
191 nd6_setmtu0(ifp, nd); 191 nd6_setmtu0(ifp, nd);
192 192
193 return nd; 193 return nd;
194} 194}
195 195
196void 196void
197nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext) 197nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
198{ 198{
199 199
200 /* Ensure all IPv6 addresses are purged before calling nd6_purge */ 200 /* Ensure all IPv6 addresses are purged before calling nd6_purge */
201 if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr); 201 if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
202 nd6_purge(ifp, ext); 202 nd6_purge(ifp, ext);
203 kmem_free(ext->nd_ifinfo, sizeof(struct nd_ifinfo)); 203 kmem_free(ext->nd_ifinfo, sizeof(struct nd_ifinfo));
204} 204}
205 205
206void 206void
207nd6_setmtu(struct ifnet *ifp) 207nd6_setmtu(struct ifnet *ifp)
208{ 208{
209 nd6_setmtu0(ifp, ND_IFINFO(ifp)); 209 nd6_setmtu0(ifp, ND_IFINFO(ifp));
210} 210}
211 211
212void 212void
213nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi) 213nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
214{ 214{
215 u_int32_t omaxmtu; 215 u_int32_t omaxmtu;
216 216
217 omaxmtu = ndi->maxmtu; 217 omaxmtu = ndi->maxmtu;
218 218
219 switch (ifp->if_type) { 219 switch (ifp->if_type) {
220 case IFT_ARCNET: 220 case IFT_ARCNET:
221 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */ 221 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
222 break; 222 break;
223 case IFT_FDDI: 223 case IFT_FDDI:
224 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); 224 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
225 break; 225 break;
226 default: 226 default:
227 ndi->maxmtu = ifp->if_mtu; 227 ndi->maxmtu = ifp->if_mtu;
228 break; 228 break;
229 } 229 }
230 230
231 /* 231 /*
232 * Decreasing the interface MTU under IPV6 minimum MTU may cause 232 * Decreasing the interface MTU under IPV6 minimum MTU may cause
233 * undesirable situation. We thus notify the operator of the change 233 * undesirable situation. We thus notify the operator of the change
234 * explicitly. The check for omaxmtu is necessary to restrict the 234 * explicitly. The check for omaxmtu is necessary to restrict the
235 * log to the case of changing the MTU, not initializing it. 235 * log to the case of changing the MTU, not initializing it.
236 */ 236 */
237 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) { 237 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
238 log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too" 238 log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too"
239 " small for IPv6 which needs %lu\n", 239 " small for IPv6 which needs %lu\n",
240 if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long) 240 if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long)
241 IPV6_MMTU); 241 IPV6_MMTU);
242 } 242 }
243 243
244 if (ndi->maxmtu > in6_maxmtu) 244 if (ndi->maxmtu > in6_maxmtu)
245 in6_setmaxmtu(); /* check all interfaces just in case */ 245 in6_setmaxmtu(); /* check all interfaces just in case */
246} 246}
247 247
248void 248void
249nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts) 249nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
250{ 250{
251 251
252 memset(ndopts, 0, sizeof(*ndopts)); 252 memset(ndopts, 0, sizeof(*ndopts));
253 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt; 253 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
254 ndopts->nd_opts_last 254 ndopts->nd_opts_last
255 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len); 255 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
256 256
257 if (icmp6len == 0) { 257 if (icmp6len == 0) {
258 ndopts->nd_opts_done = 1; 258 ndopts->nd_opts_done = 1;
259 ndopts->nd_opts_search = NULL; 259 ndopts->nd_opts_search = NULL;
260 } 260 }
261} 261}
262 262
263/* 263/*
264 * Take one ND option. 264 * Take one ND option.
265 */ 265 */
266static struct nd_opt_hdr * 266static struct nd_opt_hdr *
267nd6_option(union nd_opts *ndopts) 267nd6_option(union nd_opts *ndopts)
268{ 268{
269 struct nd_opt_hdr *nd_opt; 269 struct nd_opt_hdr *nd_opt;
270 int olen; 270 int olen;
271 271
272 KASSERT(ndopts != NULL); 272 KASSERT(ndopts != NULL);
273 KASSERT(ndopts->nd_opts_last != NULL); 273 KASSERT(ndopts->nd_opts_last != NULL);
274 274
275 if (ndopts->nd_opts_search == NULL) 275 if (ndopts->nd_opts_search == NULL)
276 return NULL; 276 return NULL;
277 if (ndopts->nd_opts_done) 277 if (ndopts->nd_opts_done)
278 return NULL; 278 return NULL;
279 279
280 nd_opt = ndopts->nd_opts_search; 280 nd_opt = ndopts->nd_opts_search;
281 281
282 /* make sure nd_opt_len is inside the buffer */ 282 /* make sure nd_opt_len is inside the buffer */
283 if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) { 283 if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
284 memset(ndopts, 0, sizeof(*ndopts)); 284 memset(ndopts, 0, sizeof(*ndopts));
285 return NULL; 285 return NULL;
286 } 286 }
287 287
288 olen = nd_opt->nd_opt_len << 3; 288 olen = nd_opt->nd_opt_len << 3;
289 if (olen == 0) { 289 if (olen == 0) {
290 /* 290 /*
291 * Message validation requires that all included 291 * Message validation requires that all included
292 * options have a length that is greater than zero. 292 * options have a length that is greater than zero.
293 */ 293 */
294 memset(ndopts, 0, sizeof(*ndopts)); 294 memset(ndopts, 0, sizeof(*ndopts));
295 return NULL; 295 return NULL;
296 } 296 }
297 297
298 ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen); 298 ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
299 if (ndopts->nd_opts_search > ndopts->nd_opts_last) { 299 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
300 /* option overruns the end of buffer, invalid */ 300 /* option overruns the end of buffer, invalid */
301 memset(ndopts, 0, sizeof(*ndopts)); 301 memset(ndopts, 0, sizeof(*ndopts));
302 return NULL; 302 return NULL;
303 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) { 303 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
304 /* reached the end of options chain */ 304 /* reached the end of options chain */
305 ndopts->nd_opts_done = 1; 305 ndopts->nd_opts_done = 1;
306 ndopts->nd_opts_search = NULL; 306 ndopts->nd_opts_search = NULL;
307 } 307 }
308 return nd_opt; 308 return nd_opt;
309} 309}
310 310
311/* 311/*
312 * Parse multiple ND options. 312 * Parse multiple ND options.
313 * This function is much easier to use, for ND routines that do not need 313 * This function is much easier to use, for ND routines that do not need
314 * multiple options of the same type. 314 * multiple options of the same type.
315 */ 315 */
316int 316int
317nd6_options(union nd_opts *ndopts) 317nd6_options(union nd_opts *ndopts)
318{ 318{
319 struct nd_opt_hdr *nd_opt; 319 struct nd_opt_hdr *nd_opt;
320 int i = 0; 320 int i = 0;
321 321
322 KASSERT(ndopts != NULL); 322 KASSERT(ndopts != NULL);
323 KASSERT(ndopts->nd_opts_last != NULL); 323 KASSERT(ndopts->nd_opts_last != NULL);
324 324
325 if (ndopts->nd_opts_search == NULL) 325 if (ndopts->nd_opts_search == NULL)
326 return 0; 326 return 0;
327  327
328 while (1) { 328 while (1) {
329 nd_opt = nd6_option(ndopts); 329 nd_opt = nd6_option(ndopts);
330 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) { 330 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
331 /* 331 /*
332 * Message validation requires that all included 332 * Message validation requires that all included
333 * options have a length that is greater than zero. 333 * options have a length that is greater than zero.
334 */ 334 */
335 ICMP6_STATINC(ICMP6_STAT_ND_BADOPT); 335 ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
336 memset(ndopts, 0, sizeof(*ndopts)); 336 memset(ndopts, 0, sizeof(*ndopts));
337 return -1; 337 return -1;
338 } 338 }
339 339
340 if (nd_opt == NULL) 340 if (nd_opt == NULL)
341 goto skip1; 341 goto skip1;
342 342
343 switch (nd_opt->nd_opt_type) { 343 switch (nd_opt->nd_opt_type) {
344 case ND_OPT_SOURCE_LINKADDR: 344 case ND_OPT_SOURCE_LINKADDR:
345 case ND_OPT_TARGET_LINKADDR: 345 case ND_OPT_TARGET_LINKADDR:
346 case ND_OPT_MTU: 346 case ND_OPT_MTU:
347 case ND_OPT_REDIRECTED_HEADER: 347 case ND_OPT_REDIRECTED_HEADER:
348 case ND_OPT_NONCE: 348 case ND_OPT_NONCE:
349 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) { 349 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
350 nd6log(LOG_INFO, 350 nd6log(LOG_INFO,
351 "duplicated ND6 option found (type=%d)\n", 351 "duplicated ND6 option found (type=%d)\n",
352 nd_opt->nd_opt_type); 352 nd_opt->nd_opt_type);
353 /* XXX bark? */ 353 /* XXX bark? */
354 } else { 354 } else {
355 ndopts->nd_opt_array[nd_opt->nd_opt_type] 355 ndopts->nd_opt_array[nd_opt->nd_opt_type]
356 = nd_opt; 356 = nd_opt;
357 } 357 }
358 break; 358 break;
359 case ND_OPT_PREFIX_INFORMATION: 359 case ND_OPT_PREFIX_INFORMATION:
360 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) { 360 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
361 ndopts->nd_opt_array[nd_opt->nd_opt_type] 361 ndopts->nd_opt_array[nd_opt->nd_opt_type]
362 = nd_opt; 362 = nd_opt;
363 } 363 }
364 ndopts->nd_opts_pi_end = 364 ndopts->nd_opts_pi_end =
365 (struct nd_opt_prefix_info *)nd_opt; 365 (struct nd_opt_prefix_info *)nd_opt;
366 break; 366 break;
367 default: 367 default:
368 /* 368 /*
369 * Unknown options must be silently ignored, 369 * Unknown options must be silently ignored,
370 * to accommodate future extension to the protocol. 370 * to accommodate future extension to the protocol.
371 */ 371 */
372 nd6log(LOG_DEBUG, 372 nd6log(LOG_DEBUG,
373 "nd6_options: unsupported option %d - " 373 "nd6_options: unsupported option %d - "
374 "option ignored\n", nd_opt->nd_opt_type); 374 "option ignored\n", nd_opt->nd_opt_type);
375 } 375 }
376 376
377skip1: 377skip1:
378 i++; 378 i++;
379 if (i > nd6_maxndopt) { 379 if (i > nd6_maxndopt) {
380 ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT); 380 ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
381 nd6log(LOG_INFO, "too many loop in nd opt\n"); 381 nd6log(LOG_INFO, "too many loop in nd opt\n");
382 break; 382 break;
383 } 383 }
384 384
385 if (ndopts->nd_opts_done) 385 if (ndopts->nd_opts_done)
386 break; 386 break;
387 } 387 }
388 388
389 return 0; 389 return 0;
390} 390}
391 391
392/* 392/*
393 * ND6 timer routine to handle ND6 entries 393 * ND6 timer routine to handle ND6 entries
394 */ 394 */
395void 395void
396nd6_llinfo_settimer(struct llentry *ln, time_t xtick) 396nd6_llinfo_settimer(struct llentry *ln, time_t xtick)
397{ 397{
398 398
399 CTASSERT(sizeof(time_t) > sizeof(int)); 399 CTASSERT(sizeof(time_t) > sizeof(int));
400 LLE_WLOCK_ASSERT(ln); 400 LLE_WLOCK_ASSERT(ln);
401 401
402 KASSERT(xtick >= 0); 402 KASSERT(xtick >= 0);
403 403
404 /* 404 /*
405 * We have to take care of a reference leak which occurs if 405 * We have to take care of a reference leak which occurs if
406 * callout_reset overwrites a pending callout schedule. Unfortunately 406 * callout_reset overwrites a pending callout schedule. Unfortunately
407 * we don't have a mean to know the overwrite, so we need to know it 407 * we don't have a mean to know the overwrite, so we need to know it
408 * using callout_stop. We need to call callout_pending first to exclude 408 * using callout_stop. We need to call callout_pending first to exclude
409 * the case that the callout has never been scheduled. 409 * the case that the callout has never been scheduled.
410 */ 410 */
411 if (callout_pending(&ln->la_timer)) { 411 if (callout_pending(&ln->la_timer)) {
412 bool expired = callout_stop(&ln->la_timer); 412 bool expired = callout_stop(&ln->la_timer);
413 if (!expired) 413 if (!expired)
414 LLE_REMREF(ln); 414 LLE_REMREF(ln);
415 } 415 }
416 416
417 ln->ln_expire = time_uptime + xtick / hz; 417 ln->ln_expire = time_uptime + xtick / hz;
418 LLE_ADDREF(ln); 418 LLE_ADDREF(ln);
419 if (xtick > INT_MAX) { 419 if (xtick > INT_MAX) {
420 ln->ln_ntick = xtick - INT_MAX; 420 ln->ln_ntick = xtick - INT_MAX;
421 callout_reset(&ln->ln_timer_ch, INT_MAX, 421 callout_reset(&ln->ln_timer_ch, INT_MAX,
422 nd6_llinfo_timer, ln); 422 nd6_llinfo_timer, ln);
423 } else { 423 } else {
424 ln->ln_ntick = 0; 424 ln->ln_ntick = 0;
425 callout_reset(&ln->ln_timer_ch, xtick, 425 callout_reset(&ln->ln_timer_ch, xtick,
426 nd6_llinfo_timer, ln); 426 nd6_llinfo_timer, ln);
427 } 427 }
428} 428}
429 429
430/* 430/*
431 * Gets source address of the first packet in hold queue 431 * Gets source address of the first packet in hold queue
432 * and stores it in @src. 432 * and stores it in @src.
433 * Returns pointer to @src (if hold queue is not empty) or NULL. 433 * Returns pointer to @src (if hold queue is not empty) or NULL.
434 */ 434 */
435static struct in6_addr * 435static struct in6_addr *
436nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src) 436nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
437{ 437{
438 struct ip6_hdr *hip6; 438 struct ip6_hdr *hip6;
439 439
440 if (ln == NULL || ln->ln_hold == NULL) 440 if (ln == NULL || ln->ln_hold == NULL)
441 return NULL; 441 return NULL;
442 442
443 /* 443 /*
444 * assuming every packet in ln_hold has the same IP header 444 * assuming every packet in ln_hold has the same IP header
445 */ 445 */
446 hip6 = mtod(ln->ln_hold, struct ip6_hdr *); 446 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
447 /* XXX pullup? */ 447 /* XXX pullup? */
448 if (sizeof(*hip6) < ln->ln_hold->m_len) 448 if (sizeof(*hip6) < ln->ln_hold->m_len)
449 *src = hip6->ip6_src; 449 *src = hip6->ip6_src;
450 else 450 else
451 src = NULL; 451 src = NULL;
452 452
453 return src; 453 return src;
454} 454}
455 455
456static void 456static void
457nd6_llinfo_timer(void *arg) 457nd6_llinfo_timer(void *arg)
458{ 458{
459 struct llentry *ln = arg; 459 struct llentry *ln = arg;
460 struct ifnet *ifp; 460 struct ifnet *ifp;
461 struct nd_ifinfo *ndi = NULL; 461 struct nd_ifinfo *ndi = NULL;
462 bool send_ns = false; 462 bool send_ns = false;
463 const struct in6_addr *daddr6 = NULL; 463 const struct in6_addr *daddr6 = NULL;
464 464
465 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 465 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
466 466
467 LLE_WLOCK(ln); 467 LLE_WLOCK(ln);
468 if ((ln->la_flags & LLE_LINKED) == 0) 468 if ((ln->la_flags & LLE_LINKED) == 0)
469 goto out; 469 goto out;
470 if (ln->ln_ntick > 0) { 470 if (ln->ln_ntick > 0) {
471 nd6_llinfo_settimer(ln, ln->ln_ntick); 471 nd6_llinfo_settimer(ln, ln->ln_ntick);
472 goto out; 472 goto out;
473 } 473 }
474 474
475 475
476 ifp = ln->lle_tbl->llt_ifp; 476 ifp = ln->lle_tbl->llt_ifp;
477 KASSERT(ifp != NULL); 477 KASSERT(ifp != NULL);
478 478
479 ndi = ND_IFINFO(ifp); 479 ndi = ND_IFINFO(ifp);
480 480
481 switch (ln->ln_state) { 481 switch (ln->ln_state) {
482 case ND6_LLINFO_INCOMPLETE: 482 case ND6_LLINFO_INCOMPLETE:
483 if (ln->ln_asked < nd6_mmaxtries) { 483 if (ln->ln_asked < nd6_mmaxtries) {
484 ln->ln_asked++; 484 ln->ln_asked++;
485 send_ns = true; 485 send_ns = true;
486 } else { 486 } else {
487 struct mbuf *m = ln->ln_hold; 487 struct mbuf *m = ln->ln_hold;
488 if (m) { 488 if (m) {
489 struct mbuf *m0; 489 struct mbuf *m0;
490 490
491 /* 491 /*
492 * assuming every packet in ln_hold has 492 * assuming every packet in ln_hold has
493 * the same IP header 493 * the same IP header
494 */ 494 */
495 m0 = m->m_nextpkt; 495 m0 = m->m_nextpkt;
496 m->m_nextpkt = NULL; 496 m->m_nextpkt = NULL;
497 ln->ln_hold = m0; 497 ln->ln_hold = m0;
498 clear_llinfo_pqueue(ln); 498 clear_llinfo_pqueue(ln);
499 } 499 }
500 LLE_REMREF(ln); 500 LLE_REMREF(ln);
501 nd6_free(ln, 0); 501 nd6_free(ln, 0);
502 ln = NULL; 502 ln = NULL;
503 if (m != NULL) { 503 if (m != NULL) {
504 icmp6_error2(m, ICMP6_DST_UNREACH, 504 icmp6_error2(m, ICMP6_DST_UNREACH,
505 ICMP6_DST_UNREACH_ADDR, 0, ifp); 505 ICMP6_DST_UNREACH_ADDR, 0, ifp);
506 } 506 }
507 } 507 }
508 break; 508 break;
509 case ND6_LLINFO_REACHABLE: 509 case ND6_LLINFO_REACHABLE:
510 if (!ND6_LLINFO_PERMANENT(ln)) { 510 if (!ND6_LLINFO_PERMANENT(ln)) {
511 ln->ln_state = ND6_LLINFO_STALE; 511 ln->ln_state = ND6_LLINFO_STALE;
512 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 512 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
513 } 513 }
514 break; 514 break;
515 515
516 case ND6_LLINFO_PURGE: 516 case ND6_LLINFO_PURGE:
517 case ND6_LLINFO_STALE: 517 case ND6_LLINFO_STALE:
518 /* Garbage Collection(RFC 2461 5.3) */ 518 /* Garbage Collection(RFC 2461 5.3) */
519 if (!ND6_LLINFO_PERMANENT(ln)) { 519 if (!ND6_LLINFO_PERMANENT(ln)) {
520 LLE_REMREF(ln); 520 LLE_REMREF(ln);
521 nd6_free(ln, 1); 521 nd6_free(ln, 1);
522 ln = NULL; 522 ln = NULL;
523 } 523 }
524 break; 524 break;
525 525
526 case ND6_LLINFO_DELAY: 526 case ND6_LLINFO_DELAY:
527 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) { 527 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
528 /* We need NUD */ 528 /* We need NUD */
529 ln->ln_asked = 1; 529 ln->ln_asked = 1;
530 ln->ln_state = ND6_LLINFO_PROBE; 530 ln->ln_state = ND6_LLINFO_PROBE;
531 daddr6 = &ln->r_l3addr.addr6; 531 daddr6 = &ln->r_l3addr.addr6;
532 send_ns = true; 532 send_ns = true;
533 } else { 533 } else {
534 ln->ln_state = ND6_LLINFO_STALE; /* XXX */ 534 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
535 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 535 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
536 } 536 }
537 break; 537 break;
538 case ND6_LLINFO_PROBE: 538 case ND6_LLINFO_PROBE:
539 if (ln->ln_asked < nd6_umaxtries) { 539 if (ln->ln_asked < nd6_umaxtries) {
540 ln->ln_asked++; 540 ln->ln_asked++;
541 daddr6 = &ln->r_l3addr.addr6; 541 daddr6 = &ln->r_l3addr.addr6;
542 send_ns = true; 542 send_ns = true;
543 } else { 543 } else {
544 LLE_REMREF(ln); 544 LLE_REMREF(ln);
545 nd6_free(ln, 0); 545 nd6_free(ln, 0);
546 ln = NULL; 546 ln = NULL;
547 } 547 }
548 break; 548 break;
549 } 549 }
550 550
551 if (send_ns) { 551 if (send_ns) {
552 struct in6_addr src, *psrc; 552 struct in6_addr src, *psrc;
553 const struct in6_addr *taddr6 = &ln->r_l3addr.addr6; 553 const struct in6_addr *taddr6 = &ln->r_l3addr.addr6;
554 554
555 nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000); 555 nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
556 psrc = nd6_llinfo_get_holdsrc(ln, &src); 556 psrc = nd6_llinfo_get_holdsrc(ln, &src);
557 LLE_FREE_LOCKED(ln); 557 LLE_FREE_LOCKED(ln);
558 ln = NULL; 558 ln = NULL;
559 nd6_ns_output(ifp, daddr6, taddr6, psrc, NULL); 559 nd6_ns_output(ifp, daddr6, taddr6, psrc, NULL);
560 } 560 }
561 561
562out: 562out:
563 if (ln != NULL) 563 if (ln != NULL)
564 LLE_FREE_LOCKED(ln); 564 LLE_FREE_LOCKED(ln);
565 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 565 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
566} 566}
567 567
568/* 568/*
569 * ND6 timer routine to expire default route list and prefix list 569 * ND6 timer routine to expire default route list and prefix list
570 */ 570 */
571static void 571static void
572nd6_timer_work(struct work *wk, void *arg) 572nd6_timer_work(struct work *wk, void *arg)
573{ 573{
574 struct nd_defrouter *next_dr, *dr; 574 struct nd_defrouter *next_dr, *dr;
575 struct nd_prefix *next_pr, *pr; 575 struct nd_prefix *next_pr, *pr;
576 struct in6_ifaddr *ia6, *nia6; 576 struct in6_ifaddr *ia6, *nia6;
577 int s, bound; 577 int s, bound;
578 struct psref psref; 578 struct psref psref;
579 579
580 callout_reset(&nd6_timer_ch, nd6_prune * hz, 580 callout_reset(&nd6_timer_ch, nd6_prune * hz,
581 nd6_timer, NULL); 581 nd6_timer, NULL);
582 582
583 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 583 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
584 584
585 /* expire default router list */ 585 /* expire default router list */
586 586
587 ND6_WLOCK(); 587 ND6_WLOCK();
588 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, next_dr) { 588 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, next_dr) {
589 if (dr->expire && dr->expire < time_uptime) { 589 if (dr->expire && dr->expire < time_uptime) {
590 nd6_defrtrlist_del(dr, NULL); 590 nd6_defrtrlist_del(dr, NULL);
591 } 591 }
592 } 592 }
593 ND6_UNLOCK(); 593 ND6_UNLOCK();
594 594
595 /* 595 /*
596 * expire interface addresses. 596 * expire interface addresses.
597 * in the past the loop was inside prefix expiry processing. 597 * in the past the loop was inside prefix expiry processing.
598 * However, from a stricter speci-confrmance standpoint, we should 598 * However, from a stricter speci-confrmance standpoint, we should
599 * rather separate address lifetimes and prefix lifetimes. 599 * rather separate address lifetimes and prefix lifetimes.
600 */ 600 */
601 bound = curlwp_bind(); 601 bound = curlwp_bind();
602 addrloop: 602 addrloop:
603 s = pserialize_read_enter(); 603 s = pserialize_read_enter();
604 for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) { 604 for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) {
605 nia6 = IN6_ADDRLIST_READER_NEXT(ia6); 605 nia6 = IN6_ADDRLIST_READER_NEXT(ia6);
606 606
607 ia6_acquire(ia6, &psref); 607 ia6_acquire(ia6, &psref);
608 pserialize_read_exit(s); 608 pserialize_read_exit(s);
609 609
610 /* check address lifetime */ 610 /* check address lifetime */
611 if (IFA6_IS_INVALID(ia6)) { 611 if (IFA6_IS_INVALID(ia6)) {
612 int regen = 0; 612 int regen = 0;
613 struct ifnet *ifp; 613 struct ifnet *ifp;
614 614
615 /* 615 /*
616 * If the expiring address is temporary, try 616 * If the expiring address is temporary, try
617 * regenerating a new one. This would be useful when 617 * regenerating a new one. This would be useful when
618 * we suspended a laptop PC, then turned it on after a 618 * we suspended a laptop PC, then turned it on after a
619 * period that could invalidate all temporary 619 * period that could invalidate all temporary
620 * addresses. Although we may have to restart the 620 * addresses. Although we may have to restart the
621 * loop (see below), it must be after purging the 621 * loop (see below), it must be after purging the
622 * address. Otherwise, we'd see an infinite loop of 622 * address. Otherwise, we'd see an infinite loop of
623 * regeneration. 623 * regeneration.
624 */ 624 */
625 if (ip6_use_tempaddr && 625 if (ip6_use_tempaddr &&
626 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 626 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
627 IFNET_LOCK(ia6->ia_ifa.ifa_ifp); 627 IFNET_LOCK(ia6->ia_ifa.ifa_ifp);
628 if (regen_tmpaddr(ia6) == 0) 628 if (regen_tmpaddr(ia6) == 0)
629 regen = 1; 629 regen = 1;
630 IFNET_UNLOCK(ia6->ia_ifa.ifa_ifp); 630 IFNET_UNLOCK(ia6->ia_ifa.ifa_ifp);
631 } 631 }
632 632
633 ifp = ia6->ia_ifa.ifa_ifp; 633 ifp = ia6->ia_ifa.ifa_ifp;
634 IFNET_LOCK(ifp); 634 IFNET_LOCK(ifp);
635 /* 635 /*
636 * Need to take the lock first to prevent if_detach 636 * Need to take the lock first to prevent if_detach
637 * from running in6_purgeaddr concurrently. 637 * from running in6_purgeaddr concurrently.
638 */ 638 */
639 if (!if_is_deactivated(ifp)) { 639 if (!if_is_deactivated(ifp)) {
640 ia6_release(ia6, &psref); 640 ia6_release(ia6, &psref);
641 in6_purgeaddr(&ia6->ia_ifa); 641 in6_purgeaddr(&ia6->ia_ifa);
642 } else { 642 } else {
643 /* 643 /*
644 * ifp is being destroyed, ia6 will be destroyed 644 * ifp is being destroyed, ia6 will be destroyed
645 * by if_detach. 645 * by if_detach.
646 */ 646 */
647 ia6_release(ia6, &psref); 647 ia6_release(ia6, &psref);
648 } 648 }
649 ia6 = NULL; 649 ia6 = NULL;
650 IFNET_UNLOCK(ifp); 650 IFNET_UNLOCK(ifp);
651 651
652 if (regen) 652 if (regen)
653 goto addrloop; /* XXX: see below */ 653 goto addrloop; /* XXX: see below */
654 } else if (IFA6_IS_DEPRECATED(ia6)) { 654 } else if (IFA6_IS_DEPRECATED(ia6)) {
655 int oldflags = ia6->ia6_flags; 655 int oldflags = ia6->ia6_flags;
656 656
657 if ((oldflags & IN6_IFF_DEPRECATED) == 0) { 657 if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
658 ia6->ia6_flags |= IN6_IFF_DEPRECATED; 658 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
659 rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6); 659 rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
660 } 660 }
661 661
662 /* 662 /*
663 * If a temporary address has just become deprecated, 663 * If a temporary address has just become deprecated,
664 * regenerate a new one if possible. 664 * regenerate a new one if possible.
665 */ 665 */
666 if (ip6_use_tempaddr && 666 if (ip6_use_tempaddr &&
667 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 667 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
668 (oldflags & IN6_IFF_DEPRECATED) == 0) { 668 (oldflags & IN6_IFF_DEPRECATED) == 0) {
669 int ret; 669 int ret;
670 670
671 IFNET_LOCK(ia6->ia_ifa.ifa_ifp); 671 IFNET_LOCK(ia6->ia_ifa.ifa_ifp);
672 ret = regen_tmpaddr(ia6); 672 ret = regen_tmpaddr(ia6);
673 IFNET_UNLOCK(ia6->ia_ifa.ifa_ifp); 673 IFNET_UNLOCK(ia6->ia_ifa.ifa_ifp);
674 if (ret == 0) { 674 if (ret == 0) {
675 /* 675 /*
676 * A new temporary address is 676 * A new temporary address is
677 * generated. 677 * generated.
678 * XXX: this means the address chain 678 * XXX: this means the address chain
679 * has changed while we are still in 679 * has changed while we are still in
680 * the loop. Although the change 680 * the loop. Although the change
681 * would not cause disaster (because 681 * would not cause disaster (because
682 * it's not a deletion, but an 682 * it's not a deletion, but an
683 * addition,) we'd rather restart the 683 * addition,) we'd rather restart the
684 * loop just for safety. Or does this 684 * loop just for safety. Or does this
685 * significantly reduce performance?? 685 * significantly reduce performance??
686 */ 686 */
687 ia6_release(ia6, &psref); 687 ia6_release(ia6, &psref);
688 goto addrloop; 688 goto addrloop;
689 } 689 }
690 } 690 }
691 } else { 691 } else {
692 /* 692 /*
693 * A new RA might have made a deprecated address 693 * A new RA might have made a deprecated address
694 * preferred. 694 * preferred.
695 */ 695 */
696 if (ia6->ia6_flags & IN6_IFF_DEPRECATED) { 696 if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
697 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED; 697 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
698 rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6); 698 rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
699 } 699 }
700 } 700 }
701 s = pserialize_read_enter(); 701 s = pserialize_read_enter();
702 ia6_release(ia6, &psref); 702 ia6_release(ia6, &psref);
703 } 703 }
704 pserialize_read_exit(s); 704 pserialize_read_exit(s);
705 curlwp_bindx(bound); 705 curlwp_bindx(bound);
706 706
707 /* expire prefix list */ 707 /* expire prefix list */
708 ND6_WLOCK(); 708 ND6_WLOCK();
709 ND_PREFIX_LIST_FOREACH_SAFE(pr, next_pr) { 709 ND_PREFIX_LIST_FOREACH_SAFE(pr, next_pr) {
710 /* 710 /*
711 * check prefix lifetime. 711 * check prefix lifetime.
712 * since pltime is just for autoconf, pltime processing for 712 * since pltime is just for autoconf, pltime processing for
713 * prefix is not necessary. 713 * prefix is not necessary.
714 */ 714 */
715 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME && 715 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
716 time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) { 716 time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
717 /* 717 /*
718 * Just invalidate the prefix here. Removing it 718 * Just invalidate the prefix here. Removing it
719 * will be done when purging an associated address. 719 * will be done when purging an associated address.
720 */ 720 */
721 KASSERT(pr->ndpr_refcnt > 0); 721 KASSERT(pr->ndpr_refcnt > 0);
722 nd6_invalidate_prefix(pr); 722 nd6_invalidate_prefix(pr);
723 } 723 }
724 } 724 }
725 ND6_UNLOCK(); 725 ND6_UNLOCK();
726 726
727 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 727 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
728} 728}
729 729
730static void 730static void
731nd6_timer(void *ignored_arg) 731nd6_timer(void *ignored_arg)
732{ 732{
733 733
734 workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL); 734 workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL);
735} 735}
736 736
737/* ia6: deprecated/invalidated temporary address */ 737/* ia6: deprecated/invalidated temporary address */
738static int 738static int
739regen_tmpaddr(const struct in6_ifaddr *ia6) 739regen_tmpaddr(const struct in6_ifaddr *ia6)
740{ 740{
741 struct ifaddr *ifa; 741 struct ifaddr *ifa;
742 struct ifnet *ifp; 742 struct ifnet *ifp;
743 struct in6_ifaddr *public_ifa6 = NULL; 743 struct in6_ifaddr *public_ifa6 = NULL;
744 int s; 744 int s;
745 745
746 ifp = ia6->ia_ifa.ifa_ifp; 746 ifp = ia6->ia_ifa.ifa_ifp;
747 s = pserialize_read_enter(); 747 s = pserialize_read_enter();
748 IFADDR_READER_FOREACH(ifa, ifp) { 748 IFADDR_READER_FOREACH(ifa, ifp) {
749 struct in6_ifaddr *it6; 749 struct in6_ifaddr *it6;
750 750
751 if (ifa->ifa_addr->sa_family != AF_INET6) 751 if (ifa->ifa_addr->sa_family != AF_INET6)
752 continue; 752 continue;
753 753
754 it6 = (struct in6_ifaddr *)ifa; 754 it6 = (struct in6_ifaddr *)ifa;
755 755
756 /* ignore no autoconf addresses. */ 756 /* ignore no autoconf addresses. */
757 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 757 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
758 continue; 758 continue;
759 759
760 /* ignore autoconf addresses with different prefixes. */ 760 /* ignore autoconf addresses with different prefixes. */
761 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) 761 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
762 continue; 762 continue;
763 763
764 /* 764 /*
765 * Now we are looking at an autoconf address with the same 765 * Now we are looking at an autoconf address with the same
766 * prefix as ours. If the address is temporary and is still 766 * prefix as ours. If the address is temporary and is still
767 * preferred, do not create another one. It would be rare, but 767 * preferred, do not create another one. It would be rare, but
768 * could happen, for example, when we resume a laptop PC after 768 * could happen, for example, when we resume a laptop PC after
769 * a long period. 769 * a long period.
770 */ 770 */
771 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 771 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
772 !IFA6_IS_DEPRECATED(it6)) { 772 !IFA6_IS_DEPRECATED(it6)) {
773 public_ifa6 = NULL; 773 public_ifa6 = NULL;
774 break; 774 break;
775 } 775 }
776 776
777 /* 777 /*
778 * This is a public autoconf address that has the same prefix 778 * This is a public autoconf address that has the same prefix
779 * as ours. If it is preferred, keep it. We can't break the 779 * as ours. If it is preferred, keep it. We can't break the
780 * loop here, because there may be a still-preferred temporary 780 * loop here, because there may be a still-preferred temporary
781 * address with the prefix. 781 * address with the prefix.
782 */ 782 */
783 if (!IFA6_IS_DEPRECATED(it6)) 783 if (!IFA6_IS_DEPRECATED(it6))
784 public_ifa6 = it6; 784 public_ifa6 = it6;
785 } 785 }
786 786
787 if (public_ifa6 != NULL) { 787 if (public_ifa6 != NULL) {
788 int e; 788 int e;
789 struct psref psref; 789 struct psref psref;
790 790
791 ia6_acquire(public_ifa6, &psref); 791 ia6_acquire(public_ifa6, &psref);
792 pserialize_read_exit(s); 792 pserialize_read_exit(s);
793 /* 793 /*
794 * Random factor is introduced in the preferred lifetime, so 794 * Random factor is introduced in the preferred lifetime, so
795 * we do not need additional delay (3rd arg to in6_tmpifadd). 795 * we do not need additional delay (3rd arg to in6_tmpifadd).
796 */ 796 */
797 ND6_WLOCK(); 797 ND6_WLOCK();
798 e = in6_tmpifadd(public_ifa6, 0, 0); 798 e = in6_tmpifadd(public_ifa6, 0, 0);
799 ND6_UNLOCK(); 799 ND6_UNLOCK();
800 if (e != 0) { 800 if (e != 0) {
801 ia6_release(public_ifa6, &psref); 801 ia6_release(public_ifa6, &psref);
802 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 802 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
803 " tmp addr, errno=%d\n", e); 803 " tmp addr, errno=%d\n", e);
804 return -1; 804 return -1;
805 } 805 }
806 ia6_release(public_ifa6, &psref); 806 ia6_release(public_ifa6, &psref);
807 return 0; 807 return 0;
808 } 808 }
809 pserialize_read_exit(s); 809 pserialize_read_exit(s);
810 810
811 return -1; 811 return -1;
812} 812}
813 813
814bool 814bool
815nd6_accepts_rtadv(const struct nd_ifinfo *ndi) 815nd6_accepts_rtadv(const struct nd_ifinfo *ndi)
816{ 816{
817 switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) { 817 switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) {
818 case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV: 818 case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV:
819 return true; 819 return true;
820 case ND6_IFF_ACCEPT_RTADV: 820 case ND6_IFF_ACCEPT_RTADV:
821 return ip6_accept_rtadv != 0; 821 return ip6_accept_rtadv != 0;
822 case ND6_IFF_OVERRIDE_RTADV: 822 case ND6_IFF_OVERRIDE_RTADV:
823 case 0: 823 case 0:
824 default: 824 default:
825 return false; 825 return false;
826 } 826 }
827} 827}
828 828
829/* 829/*
830 * Nuke neighbor cache/prefix/default router management table, right before 830 * Nuke neighbor cache/prefix/default router management table, right before
831 * ifp goes away. 831 * ifp goes away.
832 */ 832 */
833void 833void
834nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext) 834nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
835{ 835{
836 struct nd_defrouter *dr, *ndr; 836 struct nd_defrouter *dr, *ndr;
837 struct nd_prefix *pr, *npr; 837 struct nd_prefix *pr, *npr;
838 838
839 /* 839 /*
840 * During detach, the ND info might be already removed, but 840 * During detach, the ND info might be already removed, but
841 * then is explitly passed as argument. 841 * then is explitly passed as argument.
842 * Otherwise get it from ifp->if_afdata. 842 * Otherwise get it from ifp->if_afdata.
843 */ 843 */
844 if (ext == NULL) 844 if (ext == NULL)
845 ext = ifp->if_afdata[AF_INET6]; 845 ext = ifp->if_afdata[AF_INET6];
846 if (ext == NULL) 846 if (ext == NULL)
847 return; 847 return;
848 848
849 ND6_WLOCK(); 849 ND6_WLOCK();
850 /* 850 /*
851 * Nuke default router list entries toward ifp. 851 * Nuke default router list entries toward ifp.
852 * We defer removal of default router list entries that is installed 852 * We defer removal of default router list entries that is installed
853 * in the routing table, in order to keep additional side effects as 853 * in the routing table, in order to keep additional side effects as
854 * small as possible. 854 * small as possible.
855 */ 855 */
856 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) { 856 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) {
857 if (dr->installed) 857 if (dr->installed)
858 continue; 858 continue;
859 859
860 if (dr->ifp == ifp) { 860 if (dr->ifp == ifp) {
861 KASSERT(ext != NULL); 861 KASSERT(ext != NULL);
862 nd6_defrtrlist_del(dr, ext); 862 nd6_defrtrlist_del(dr, ext);
863 } 863 }
864 } 864 }
865 865
866 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) { 866 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) {
867 if (!dr->installed) 867 if (!dr->installed)
868 continue; 868 continue;
869 869
870 if (dr->ifp == ifp) { 870 if (dr->ifp == ifp) {
871 KASSERT(ext != NULL); 871 KASSERT(ext != NULL);
872 nd6_defrtrlist_del(dr, ext); 872 nd6_defrtrlist_del(dr, ext);
873 } 873 }
874 } 874 }
875 875
876 /* Nuke prefix list entries toward ifp */ 876 /* Nuke prefix list entries toward ifp */
877 ND_PREFIX_LIST_FOREACH_SAFE(pr, npr) { 877 ND_PREFIX_LIST_FOREACH_SAFE(pr, npr) {
878 if (pr->ndpr_ifp == ifp) { 878 if (pr->ndpr_ifp == ifp) {
879 /* 879 /*
880 * All addresses referencing pr should be already freed. 880 * All addresses referencing pr should be already freed.
881 */ 881 */
882 KASSERT(pr->ndpr_refcnt == 0); 882 KASSERT(pr->ndpr_refcnt == 0);
883 nd6_prelist_remove(pr); 883 nd6_prelist_remove(pr);
884 } 884 }
885 } 885 }
886 886
887 /* cancel default outgoing interface setting */ 887 /* cancel default outgoing interface setting */
888 if (nd6_defifindex == ifp->if_index) 888 if (nd6_defifindex == ifp->if_index)
889 nd6_setdefaultiface(0); 889 nd6_setdefaultiface(0);
890 890
891 /* XXX: too restrictive? */ 891 /* XXX: too restrictive? */
892 if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) { 892 if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) {
893 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 893 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
894 if (ndi && nd6_accepts_rtadv(ndi)) { 894 if (ndi && nd6_accepts_rtadv(ndi)) {
895 /* refresh default router list */ 895 /* refresh default router list */
896 nd6_defrouter_select(); 896 nd6_defrouter_select();
897 } 897 }
898 } 898 }
899 ND6_UNLOCK(); 899 ND6_UNLOCK();
900 900
901 /* 901 /*
902 * We may not need to nuke the neighbor cache entries here 902 * We may not need to nuke the neighbor cache entries here
903 * because the neighbor cache is kept in if_afdata[AF_INET6]. 903 * because the neighbor cache is kept in if_afdata[AF_INET6].
904 * nd6_purge() is invoked by in6_ifdetach() which is called 904 * nd6_purge() is invoked by in6_ifdetach() which is called
905 * from if_detach() where everything gets purged. However 905 * from if_detach() where everything gets purged. However
906 * in6_ifdetach is directly called from vlan(4), so we still 906 * in6_ifdetach is directly called from vlan(4), so we still
907 * need to purge entries here. 907 * need to purge entries here.
908 */ 908 */
909 if (ext->lltable != NULL) 909 if (ext->lltable != NULL)
910 lltable_purge_entries(ext->lltable); 910 lltable_purge_entries(ext->lltable);
911} 911}
912 912
913void 913void
914nd6_assert_purged(struct ifnet *ifp) 914nd6_assert_purged(struct ifnet *ifp)
915{ 915{
916 struct nd_defrouter *dr; 916 struct nd_defrouter *dr;
917 struct nd_prefix *pr; 917 struct nd_prefix *pr;
918 char ip6buf[INET6_ADDRSTRLEN] __diagused; 918 char ip6buf[INET6_ADDRSTRLEN] __diagused;
919 919
920 ND6_RLOCK(); 920 ND6_RLOCK();
921 ND_DEFROUTER_LIST_FOREACH(dr) { 921 ND_DEFROUTER_LIST_FOREACH(dr) {
922 KASSERTMSG(dr->ifp != ifp, 922 KASSERTMSG(dr->ifp != ifp,
923 "defrouter %s remains on %s", 923 "defrouter %s remains on %s",
924 IN6_PRINT(ip6buf, &dr->rtaddr), ifp->if_xname); 924 IN6_PRINT(ip6buf, &dr->rtaddr), ifp->if_xname);
925 } 925 }
926 926
927 ND_PREFIX_LIST_FOREACH(pr) { 927 ND_PREFIX_LIST_FOREACH(pr) {
928 KASSERTMSG(pr->ndpr_ifp != ifp, 928 KASSERTMSG(pr->ndpr_ifp != ifp,
929 "prefix %s/%d remains on %s", 929 "prefix %s/%d remains on %s",
930 IN6_PRINT(ip6buf, &pr->ndpr_prefix.sin6_addr), 930 IN6_PRINT(ip6buf, &pr->ndpr_prefix.sin6_addr),
931 pr->ndpr_plen, ifp->if_xname); 931 pr->ndpr_plen, ifp->if_xname);
932 } 932 }
933 ND6_UNLOCK(); 933 ND6_UNLOCK();
934} 934}
935 935
936struct llentry * 936struct llentry *
937nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock) 937nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock)
938{ 938{
939 struct sockaddr_in6 sin6; 939 struct sockaddr_in6 sin6;
940 struct llentry *ln; 940 struct llentry *ln;
941 941
942 sockaddr_in6_init(&sin6, addr6, 0, 0, 0); 942 sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
943 943
944 IF_AFDATA_RLOCK(ifp); 944 IF_AFDATA_RLOCK(ifp);
945 ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0, 945 ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0,
946 sin6tosa(&sin6)); 946 sin6tosa(&sin6));
947 IF_AFDATA_RUNLOCK(ifp); 947 IF_AFDATA_RUNLOCK(ifp);
948 948
949 return ln; 949 return ln;
950} 950}
951 951
952struct llentry * 952struct llentry *
953nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp) 953nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp)
954{ 954{
955 struct sockaddr_in6 sin6; 955 struct sockaddr_in6 sin6;
956 struct llentry *ln; 956 struct llentry *ln;
957 struct rtentry *rt; 957 struct rtentry *rt;
958 958
959 sockaddr_in6_init(&sin6, addr6, 0, 0, 0); 959 sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
960 rt = rtalloc1(sin6tosa(&sin6), 0); 960 rt = rtalloc1(sin6tosa(&sin6), 0);
961 961
962 IF_AFDATA_WLOCK(ifp); 962 IF_AFDATA_WLOCK(ifp);
963 ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, sin6tosa(&sin6), rt); 963 ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, sin6tosa(&sin6), rt);
964 IF_AFDATA_WUNLOCK(ifp); 964 IF_AFDATA_WUNLOCK(ifp);
965 965
966 if (rt != NULL) 966 if (rt != NULL)
967 rt_unref(rt); 967 rt_unref(rt);
968 if (ln != NULL) 968 if (ln != NULL)
969 ln->ln_state = ND6_LLINFO_NOSTATE; 969 ln->ln_state = ND6_LLINFO_NOSTATE;
970 970
971 return ln; 971 return ln;
972} 972}
973 973
974/* 974/*
975 * Test whether a given IPv6 address is a neighbor or not, ignoring 975 * Test whether a given IPv6 address is a neighbor or not, ignoring
976 * the actual neighbor cache. The neighbor cache is ignored in order 976 * the actual neighbor cache. The neighbor cache is ignored in order
977 * to not reenter the routing code from within itself. 977 * to not reenter the routing code from within itself.
978 */ 978 */
979static int 979static int
980nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 980nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
981{ 981{
982 struct nd_prefix *pr; 982 struct nd_prefix *pr;
983 struct ifaddr *dstaddr; 983 struct ifaddr *dstaddr;
984 int s; 984 int s;
985 985
986 /* 986 /*
987 * A link-local address is always a neighbor. 987 * A link-local address is always a neighbor.
988 * XXX: a link does not necessarily specify a single interface. 988 * XXX: a link does not necessarily specify a single interface.
989 */ 989 */
990 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 990 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
991 struct sockaddr_in6 sin6_copy; 991 struct sockaddr_in6 sin6_copy;
992 u_int32_t zone; 992 u_int32_t zone;
993 993
994 /* 994 /*
995 * We need sin6_copy since sa6_recoverscope() may modify the 995 * We need sin6_copy since sa6_recoverscope() may modify the
996 * content (XXX). 996 * content (XXX).
997 */ 997 */
998 sin6_copy = *addr; 998 sin6_copy = *addr;
999 if (sa6_recoverscope(&sin6_copy)) 999 if (sa6_recoverscope(&sin6_copy))
1000 return 0; /* XXX: should be impossible */ 1000 return 0; /* XXX: should be impossible */
1001 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 1001 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1002 return 0; 1002 return 0;
1003 if (sin6_copy.sin6_scope_id == zone) 1003 if (sin6_copy.sin6_scope_id == zone)
1004 return 1; 1004 return 1;
1005 else 1005 else
1006 return 0; 1006 return 0;
1007 } 1007 }
1008 1008
1009 /* 1009 /*
1010 * If the address matches one of our addresses, 1010 * If the address matches one of our addresses,
1011 * it should be a neighbor. 1011 * it should be a neighbor.
1012 * If the address matches one of our on-link prefixes, it should be a 1012 * If the address matches one of our on-link prefixes, it should be a
1013 * neighbor. 1013 * neighbor.
1014 */ 1014 */
1015 ND6_RLOCK(); 1015 ND6_RLOCK();
1016 ND_PREFIX_LIST_FOREACH(pr) { 1016 ND_PREFIX_LIST_FOREACH(pr) {
1017 if (pr->ndpr_ifp != ifp) 1017 if (pr->ndpr_ifp != ifp)
1018 continue; 1018 continue;
1019 1019
1020 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) { 1020 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1021 struct rtentry *rt; 1021 struct rtentry *rt;
1022 1022
1023 rt = rtalloc1(sin6tosa(&pr->ndpr_prefix), 0); 1023 rt = rtalloc1(sin6tosa(&pr->ndpr_prefix), 0);
1024 if (rt == NULL) 1024 if (rt == NULL)
1025 continue; 1025 continue;
1026 /* 1026 /*
1027 * This is the case where multiple interfaces 1027 * This is the case where multiple interfaces
1028 * have the same prefix, but only one is installed 1028 * have the same prefix, but only one is installed
1029 * into the routing table and that prefix entry 1029 * into the routing table and that prefix entry
1030 * is not the one being examined here. In the case 1030 * is not the one being examined here. In the case
1031 * where RADIX_MPATH is enabled, multiple route 1031 * where RADIX_MPATH is enabled, multiple route
1032 * entries (of the same rt_key value) will be 1032 * entries (of the same rt_key value) will be
1033 * installed because the interface addresses all 1033 * installed because the interface addresses all
1034 * differ. 1034 * differ.
1035 */ 1035 */
1036 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1036 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1037 &satocsin6(rt_getkey(rt))->sin6_addr)) { 1037 &satocsin6(rt_getkey(rt))->sin6_addr)) {
1038 rt_unref(rt); 1038 rt_unref(rt);
1039 continue; 1039 continue;
1040 } 1040 }
1041 rt_unref(rt); 1041 rt_unref(rt);
1042 } 1042 }
1043 1043
1044 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1044 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1045 &addr->sin6_addr, &pr->ndpr_mask)) { 1045 &addr->sin6_addr, &pr->ndpr_mask)) {
1046 ND6_UNLOCK(); 1046 ND6_UNLOCK();
1047 return 1; 1047 return 1;
1048 } 1048 }
1049 } 1049 }
1050 ND6_UNLOCK(); 1050 ND6_UNLOCK();
1051 1051
1052 /* 1052 /*
1053 * If the address is assigned on the node of the other side of 1053 * If the address is assigned on the node of the other side of
1054 * a p2p interface, the address should be a neighbor. 1054 * a p2p interface, the address should be a neighbor.
1055 */ 1055 */
1056 s = pserialize_read_enter(); 1056 s = pserialize_read_enter();
1057 dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr)); 1057 dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr));
1058 if (dstaddr != NULL) { 1058 if (dstaddr != NULL) {
1059 if (dstaddr->ifa_ifp == ifp) { 1059 if (dstaddr->ifa_ifp == ifp) {
1060 pserialize_read_exit(s); 1060 pserialize_read_exit(s);
1061 return 1; 1061 return 1;
1062 } 1062 }
1063 } 1063 }
1064 pserialize_read_exit(s); 1064 pserialize_read_exit(s);
1065 1065
1066 /* 1066 /*
1067 * If the default router list is empty, all addresses are regarded 1067 * If the default router list is empty, all addresses are regarded
1068 * as on-link, and thus, as a neighbor. 1068 * as on-link, and thus, as a neighbor.
1069 */ 1069 */
1070 ND6_RLOCK(); 1070 ND6_RLOCK();
1071 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && 1071 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
1072 ND_DEFROUTER_LIST_EMPTY() && nd6_defifindex == ifp->if_index) { 1072 ND_DEFROUTER_LIST_EMPTY() && nd6_defifindex == ifp->if_index) {
1073 ND6_UNLOCK(); 1073 ND6_UNLOCK();
1074 return 1; 1074 return 1;
1075 } 1075 }
1076 ND6_UNLOCK(); 1076 ND6_UNLOCK();
1077 1077
1078 return 0; 1078 return 0;
1079} 1079}
1080 1080
1081/* 1081/*
1082 * Detect if a given IPv6 address identifies a neighbor on a given link. 1082 * Detect if a given IPv6 address identifies a neighbor on a given link.
1083 * XXX: should take care of the destination of a p2p link? 1083 * XXX: should take care of the destination of a p2p link?
1084 */ 1084 */
1085int 1085int
1086nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 1086nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1087{ 1087{
1088 struct nd_prefix *pr; 1088 struct nd_prefix *pr;
1089 struct llentry *ln; 1089 struct llentry *ln;
1090 struct rtentry *rt; 1090 struct rtentry *rt;
1091 1091
1092 /* 1092 /*
1093 * A link-local address is always a neighbor. 1093 * A link-local address is always a neighbor.
1094 * XXX: a link does not necessarily specify a single interface. 1094 * XXX: a link does not necessarily specify a single interface.
1095 */ 1095 */
1096 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 1096 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1097 struct sockaddr_in6 sin6_copy; 1097 struct sockaddr_in6 sin6_copy;
1098 u_int32_t zone; 1098 u_int32_t zone;
1099 1099
1100 /* 1100 /*
1101 * We need sin6_copy since sa6_recoverscope() may modify the 1101 * We need sin6_copy since sa6_recoverscope() may modify the
1102 * content (XXX). 1102 * content (XXX).
1103 */ 1103 */
1104 sin6_copy = *addr; 1104 sin6_copy = *addr;
1105 if (sa6_recoverscope(&sin6_copy)) 1105 if (sa6_recoverscope(&sin6_copy))
1106 return 0; /* XXX: should be impossible */ 1106 return 0; /* XXX: should be impossible */
1107 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 1107 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1108 return 0; 1108 return 0;
1109 if (sin6_copy.sin6_scope_id == zone) 1109 if (sin6_copy.sin6_scope_id == zone)
1110 return 1; 1110 return 1;
1111 else 1111 else
1112 return 0; 1112 return 0;
1113 } 1113 }
1114 1114
1115 /* 1115 /*
1116 * If the address matches one of our on-link prefixes, it should be a 1116 * If the address matches one of our on-link prefixes, it should be a
1117 * neighbor. 1117 * neighbor.
1118 */ 1118 */
1119 ND6_RLOCK(); 1119 ND6_RLOCK();
1120 ND_PREFIX_LIST_FOREACH(pr) { 1120 ND_PREFIX_LIST_FOREACH(pr) {
1121 if (pr->ndpr_ifp != ifp) 1121 if (pr->ndpr_ifp != ifp)
1122 continue; 1122 continue;
1123 1123
1124 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) 1124 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
1125 continue; 1125 continue;
1126 1126
1127 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1127 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1128 &addr->sin6_addr, &pr->ndpr_mask)) { 1128 &addr->sin6_addr, &pr->ndpr_mask)) {
1129 ND6_UNLOCK(); 1129 ND6_UNLOCK();
1130 return 1; 1130 return 1;
1131 } 1131 }
1132 } 1132 }
1133 1133
1134 /* 1134 /*
1135 * If the default router list is empty, all addresses are regarded 1135 * If the default router list is empty, all addresses are regarded
1136 * as on-link, and thus, as a neighbor. 1136 * as on-link, and thus, as a neighbor.
1137 * XXX: we restrict the condition to hosts, because routers usually do 1137 * XXX: we restrict the condition to hosts, because routers usually do
1138 * not have the "default router list". 1138 * not have the "default router list".
1139 */ 1139 */
1140 if (!ip6_forwarding && ND_DEFROUTER_LIST_EMPTY() && 1140 if (!ip6_forwarding && ND_DEFROUTER_LIST_EMPTY() &&
1141 nd6_defifindex == ifp->if_index) { 1141 nd6_defifindex == ifp->if_index) {
1142 ND6_UNLOCK(); 1142 ND6_UNLOCK();
1143 return 1; 1143 return 1;
1144 } 1144 }
1145 ND6_UNLOCK(); 1145 ND6_UNLOCK();
1146 1146
1147 if (nd6_is_new_addr_neighbor(addr, ifp)) 1147 if (nd6_is_new_addr_neighbor(addr, ifp))
1148 return 1; 1148 return 1;
1149 1149
1150 /* 1150 /*
1151 * Even if the address matches none of our addresses, it might be 1151 * Even if the address matches none of our addresses, it might be
1152 * in the neighbor cache or a connected route. 1152 * in the neighbor cache or a connected route.
1153 */ 1153 */
1154 ln = nd6_lookup(&addr->sin6_addr, ifp, false); 1154 ln = nd6_lookup(&addr->sin6_addr, ifp, false);
1155 if (ln != NULL) { 1155 if (ln != NULL) {
1156 LLE_RUNLOCK(ln); 1156 LLE_RUNLOCK(ln);
1157 return 1; 1157 return 1;
1158 } 1158 }
1159 1159
1160 rt = rtalloc1(sin6tocsa(addr), 0); 1160 rt = rtalloc1(sin6tocsa(addr), 0);
1161 if (rt == NULL) 1161 if (rt == NULL)
1162 return 0; 1162 return 0;
1163 1163
1164 if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp 1164 if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp
1165#if NBRIDGE > 0 1165#if NBRIDGE > 0
1166 || rt->rt_ifp->if_bridge == ifp->if_bridge 1166 || rt->rt_ifp->if_bridge == ifp->if_bridge
1167#endif 1167#endif
1168#if NCARP > 0 1168#if NCARP > 0
1169 || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) || 1169 || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
1170 (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)|| 1170 (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
1171 (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP && 1171 (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
1172 rt->rt_ifp->if_carpdev == ifp->if_carpdev) 1172 rt->rt_ifp->if_carpdev == ifp->if_carpdev)
1173#endif 1173#endif
1174 )) { 1174 )) {
1175 rt_unref(rt); 1175 rt_unref(rt);
1176 return 1; 1176 return 1;
1177 } 1177 }
1178 rt_unref(rt); 1178 rt_unref(rt);
1179 1179
1180 return 0; 1180 return 0;
1181} 1181}
1182 1182
1183/* 1183/*
1184 * Free an nd6 llinfo entry. 1184 * Free an nd6 llinfo entry.
1185 * Since the function would cause significant changes in the kernel, DO NOT 1185 * Since the function would cause significant changes in the kernel, DO NOT
1186 * make it global, unless you have a strong reason for the change, and are sure 1186 * make it global, unless you have a strong reason for the change, and are sure
1187 * that the change is safe. 1187 * that the change is safe.
1188 */ 1188 */
1189static void 1189static void
1190nd6_free(struct llentry *ln, int gc) 1190nd6_free(struct llentry *ln, int gc)
1191{ 1191{
1192 struct nd_defrouter *dr; 
1193 struct ifnet *ifp; 1192 struct ifnet *ifp;
1194 struct in6_addr *in6; 1193 struct in6_addr *in6;
1195 struct sockaddr_in6 sin6; 1194 struct sockaddr_in6 sin6;
1196 1195
1197 KASSERT(ln != NULL); 1196 KASSERT(ln != NULL);
1198 LLE_WLOCK_ASSERT(ln); 1197 LLE_WLOCK_ASSERT(ln);
1199 1198
1200 ifp = ln->lle_tbl->llt_ifp; 1199 ifp = ln->lle_tbl->llt_ifp;
1201 in6 = &ln->r_l3addr.addr6; 1200 in6 = &ln->r_l3addr.addr6;
1202 /* 1201 /*
1203 * we used to have pfctlinput(PRC_HOSTDEAD) here. 1202 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1204 * even though it is not harmful, it was not really necessary. 1203 * even though it is not harmful, it was not really necessary.
1205 */ 1204 */
1206 1205
1207 if (!ip6_forwarding) { 1206 if (!ip6_forwarding && ln->ln_router) {
1208 ND6_WLOCK(); 1207 if (ln->ln_state == ND6_LLINFO_STALE && gc) {
1209 dr = nd6_defrouter_lookup(in6, ifp); 
1210 
1211 if (dr != NULL && dr->expire && 
1212 ln->ln_state == ND6_LLINFO_STALE && gc) { 
1213 /* 1208 /*
1214 * If the reason for the deletion is just garbage 1209 * If the reason for the deletion is just garbage
1215 * collection, and the neighbor is an active default 1210 * collection, and the neighbor is an active
1216 * router, do not delete it. Instead, reset the GC 1211 * router, do not delete it. Instead, reset the GC
1217 * timer using the router's lifetime. 1212 * timer using the router's lifetime.
1218 * Simply deleting the entry would affect default 1213 * Simply deleting the entry may affect default
1219 * router selection, which is not necessarily a good 1214 * router selection, which is not necessarily a good
1220 * thing, especially when we're using router preference 1215 * thing, especially when we're using router preference
1221 * values. 1216 * values.
1222 * XXX: the check for ln_state would be redundant, 1217 * XXX: the check for ln_state would be redundant,
1223 * but we intentionally keep it just in case. 1218 * but we intentionally keep it just in case.
1224 */ 1219 */
1225 if (dr->expire > time_uptime) 1220 if (ln->ln_expire > time_uptime)
1226 nd6_llinfo_settimer(ln, 1221 nd6_llinfo_settimer(ln,
1227 (dr->expire - time_uptime) * hz); 1222 (ln->ln_expire - time_uptime) * hz);
1228 else 1223 else
1229 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 1224 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
1230 ND6_UNLOCK(); 
1231 LLE_WUNLOCK(ln); 1225 LLE_WUNLOCK(ln);
1232 return; 1226 return;
1233 } 1227 }
1234 1228
1235 if (ln->ln_router || dr) { 1229 ND6_WLOCK();
1236 /* 
1237 * We need to unlock to avoid a LOR with nd6_rt_flush() 
1238 * with the rnh and for the calls to 
1239 * nd6_pfxlist_onlink_check() and nd6_defrouter_select() in the 
1240 * block further down for calls into nd6_lookup(). 
1241 * We still hold a ref. 
1242 */ 
1243 LLE_WUNLOCK(ln); 
1244 
1245 /* 
1246 * nd6_rt_flush must be called whether or not the neighbor 
1247 * is in the Default Router List. 
1248 * See a corresponding comment in nd6_na_input(). 
1249 */ 
1250 nd6_rt_flush(in6, ifp); 
1251 } 
1252 1230
1253 if (dr) { 1231 /*
1254 /* 1232 * We need to unlock to avoid a LOR with nd6_rt_flush()
1255 * Unreachablity of a router might affect the default 1233 * with the rnh and for the calls to
1256 * router selection and on-link detection of advertised 1234 * nd6_pfxlist_onlink_check() and nd6_defrouter_select() in the
1257 * prefixes. 1235 * block further down for calls into nd6_lookup().
1258 */ 1236 * We still hold a ref.
 1237 *
 1238 * Temporarily fake the state to choose a new default
 1239 * router and to perform on-link determination of
 1240 * prefixes correctly.
 1241 * Below the state will be set correctly,
 1242 * or the entry itself will be deleted.
 1243 */
 1244 ln->ln_state = ND6_LLINFO_INCOMPLETE;
 1245 LLE_WUNLOCK(ln);
1259 1246
1260 /* 1247 /*
1261 * Temporarily fake the state to choose a new default 1248 * nd6_rt_flush must be called whether or not the neighbor
1262 * router and to perform on-link determination of 1249 * is in the Default Router List.
1263 * prefixes correctly. 1250 * See a corresponding comment in nd6_na_input().
1264 * Below the state will be set correctly, 1251 */
1265 * or the entry itself will be deleted. 1252 nd6_rt_flush(in6, ifp);
1266 */ 
1267 ln->ln_state = ND6_LLINFO_INCOMPLETE; 
1268 1253
1269 /* 1254 /*
1270 * Since nd6_defrouter_select() does not affect the 1255 * Unreachablity of a router might affect the default
1271 * on-link determination and MIP6 needs the check 1256 * router selection and on-link detection of advertised
1272 * before the default router selection, we perform 1257 * prefixes.
1273 * the check now. 1258 *
1274 */ 1259 * Since nd6_defrouter_select() does not affect the
1275 nd6_pfxlist_onlink_check(); 1260 * on-link determination and MIP6 needs the check
 1261 * before the default router selection, we perform
 1262 * the check now.
 1263 */
 1264 nd6_pfxlist_onlink_check();
1276 1265
1277 /* 1266 /*
1278 * refresh default router list 1267 * refresh default router list
1279 */ 1268 */
1280 nd6_defrouter_select(); 1269 nd6_defrouter_select();
1281 } 
1282 1270
1283#ifdef __FreeBSD__ 1271#ifdef __FreeBSD__
1284 /* 1272 /*
1285 * If this entry was added by an on-link redirect, remove the 1273 * If this entry was added by an on-link redirect, remove the
1286 * corresponding host route. 1274 * corresponding host route.
1287 */ 1275 */
1288 if (ln->la_flags & LLE_REDIRECT) 1276 if (ln->la_flags & LLE_REDIRECT)
1289 nd6_free_redirect(ln); 1277 nd6_free_redirect(ln);
1290#endif 1278#endif
1291 ND6_UNLOCK(); 
1292 1279
1293 if (ln->ln_router || dr) 1280 ND6_UNLOCK();
1294 LLE_WLOCK(ln); 1281 LLE_WLOCK(ln);
1295 } 1282 }
1296 1283
1297 sockaddr_in6_init(&sin6, in6, 0, 0, 0); 1284 sockaddr_in6_init(&sin6, in6, 0, 0, 0);
1298 rt_clonedmsg(RTM_DELETE, sin6tosa(&sin6), 1285 rt_clonedmsg(RTM_DELETE, sin6tosa(&sin6),
1299 (const uint8_t *)&ln->ll_addr, ifp); 1286 (const uint8_t *)&ln->ll_addr, ifp);
1300 1287
1301 /* 1288 /*
1302 * Save to unlock. We still hold an extra reference and will not 1289 * Save to unlock. We still hold an extra reference and will not
1303 * free(9) in llentry_free() if someone else holds one as well. 1290 * free(9) in llentry_free() if someone else holds one as well.
1304 */ 1291 */
1305 LLE_WUNLOCK(ln); 1292 LLE_WUNLOCK(ln);
1306 IF_AFDATA_LOCK(ifp); 1293 IF_AFDATA_LOCK(ifp);
1307 LLE_WLOCK(ln); 1294 LLE_WLOCK(ln);
1308 1295
1309 lltable_free_entry(LLTABLE6(ifp), ln); 1296 lltable_free_entry(LLTABLE6(ifp), ln);
1310 1297
1311 IF_AFDATA_UNLOCK(ifp); 1298 IF_AFDATA_UNLOCK(ifp);
1312} 1299}
1313 1300
1314/* 1301/*
1315 * Upper-layer reachability hint for Neighbor Unreachability Detection. 1302 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1316 * 1303 *
1317 * XXX cost-effective methods? 1304 * XXX cost-effective methods?
1318 */ 1305 */
1319void 1306void
1320nd6_nud_hint(struct rtentry *rt) 1307nd6_nud_hint(struct rtentry *rt)
1321{ 1308{
1322 struct llentry *ln; 1309 struct llentry *ln;
1323 struct ifnet *ifp; 1310 struct ifnet *ifp;
1324 1311
1325 if (rt == NULL) 1312 if (rt == NULL)
1326 return; 1313 return;
1327 1314
1328 ifp = rt->rt_ifp; 1315 ifp = rt->rt_ifp;
1329 ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true); 1316 ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true);
1330 if (ln == NULL) 1317 if (ln == NULL)
1331 return; 1318 return;
1332 1319
1333 if (ln->ln_state < ND6_LLINFO_REACHABLE) 1320 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1334 goto done; 1321 goto done;
1335 1322
1336 /* 1323 /*
1337 * if we get upper-layer reachability confirmation many times, 1324 * if we get upper-layer reachability confirmation many times,
1338 * it is possible we have false information. 1325 * it is possible we have false information.
1339 */ 1326 */
1340 ln->ln_byhint++; 1327 ln->ln_byhint++;
1341 if (ln->ln_byhint > nd6_maxnudhint) 1328 if (ln->ln_byhint > nd6_maxnudhint)
1342 goto done; 1329 goto done;
1343 1330
1344 ln->ln_state = ND6_LLINFO_REACHABLE; 1331 ln->ln_state = ND6_LLINFO_REACHABLE;
1345 if (!ND6_LLINFO_PERMANENT(ln)) 1332 if (!ND6_LLINFO_PERMANENT(ln))
1346 nd6_llinfo_settimer(ln, ND_IFINFO(rt->rt_ifp)->reachable * hz); 1333 nd6_llinfo_settimer(ln, ND_IFINFO(rt->rt_ifp)->reachable * hz);
1347 1334
1348done: 1335done:
1349 LLE_WUNLOCK(ln); 1336 LLE_WUNLOCK(ln);
1350 1337
1351 return; 1338 return;
1352} 1339}
1353 1340
1354struct gc_args { 1341struct gc_args {
1355 int gc_entries; 1342 int gc_entries;
1356 const struct in6_addr *skip_in6; 1343 const struct in6_addr *skip_in6;
1357}; 1344};
1358 1345
1359static int 1346static int
1360nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg) 1347nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg)
1361{ 1348{
1362 struct gc_args *args = farg; 1349 struct gc_args *args = farg;
1363 int *n = &args->gc_entries; 1350 int *n = &args->gc_entries;
1364 const struct in6_addr *skip_in6 = args->skip_in6; 1351 const struct in6_addr *skip_in6 = args->skip_in6;
1365 1352
1366 if (*n <= 0) 1353 if (*n <= 0)
1367 return 0; 1354 return 0;
1368 1355
1369 if (ND6_LLINFO_PERMANENT(ln)) 1356 if (ND6_LLINFO_PERMANENT(ln))
1370 return 0; 1357 return 0;
1371 1358
1372 if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6)) 1359 if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6))
1373 return 0; 1360 return 0;
1374 1361
1375 LLE_WLOCK(ln); 1362 LLE_WLOCK(ln);
1376 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1363 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1377 ln->ln_state = ND6_LLINFO_STALE; 1364 ln->ln_state = ND6_LLINFO_STALE;
1378 else 1365 else
1379 ln->ln_state = ND6_LLINFO_PURGE; 1366 ln->ln_state = ND6_LLINFO_PURGE;
1380 nd6_llinfo_settimer(ln, 0); 1367 nd6_llinfo_settimer(ln, 0);
1381 LLE_WUNLOCK(ln); 1368 LLE_WUNLOCK(ln);
1382 1369
1383 (*n)--; 1370 (*n)--;
1384 return 0; 1371 return 0;
1385} 1372}
1386 1373
1387static void 1374static void
1388nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6) 1375nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6)
1389{ 1376{
1390 1377
1391 if (ip6_neighborgcthresh >= 0 && 1378 if (ip6_neighborgcthresh >= 0 &&
1392 lltable_get_entry_count(llt) >= ip6_neighborgcthresh) { 1379 lltable_get_entry_count(llt) >= ip6_neighborgcthresh) {
1393 struct gc_args gc_args = {10, in6}; 1380 struct gc_args gc_args = {10, in6};
1394 /* 1381 /*
1395 * XXX entries that are "less recently used" should be 1382 * XXX entries that are "less recently used" should be
1396 * freed first. 1383 * freed first.
1397 */ 1384 */
1398 lltable_foreach_lle(llt, nd6_purge_entry, &gc_args); 1385 lltable_foreach_lle(llt, nd6_purge_entry, &gc_args);
1399 } 1386 }
1400} 1387}
1401 1388
1402void 1389void
1403nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 1390nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
1404{ 1391{
1405 struct sockaddr *gate = rt->rt_gateway; 1392 struct sockaddr *gate = rt->rt_gateway;
1406 struct ifnet *ifp = rt->rt_ifp; 1393 struct ifnet *ifp = rt->rt_ifp;
1407 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen; 1394 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
1408 struct ifaddr *ifa; 1395 struct ifaddr *ifa;
1409 1396
1410 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1397 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1411 1398
1412 if (req == RTM_LLINFO_UPD) { 1399 if (req == RTM_LLINFO_UPD) {
1413 int rc; 1400 int rc;
1414 struct in6_addr *in6; 1401 struct in6_addr *in6;
1415 struct in6_addr in6_all; 1402 struct in6_addr in6_all;
1416 int anycast; 1403 int anycast;
1417 1404
1418 if ((ifa = info->rti_ifa) == NULL) 1405 if ((ifa = info->rti_ifa) == NULL)
1419 return; 1406 return;
1420 1407
1421 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1408 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1422 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST; 1409 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
1423 1410
1424 in6_all = in6addr_linklocal_allnodes; 1411 in6_all = in6addr_linklocal_allnodes;
1425 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) { 1412 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
1426 log(LOG_ERR, "%s: failed to set scope %s " 1413 log(LOG_ERR, "%s: failed to set scope %s "
1427 "(errno=%d)\n", __func__, if_name(ifp), rc); 1414 "(errno=%d)\n", __func__, if_name(ifp), rc);
1428 return; 1415 return;
1429 } 1416 }
1430 1417
1431 /* XXX don't set Override for proxy addresses */ 1418 /* XXX don't set Override for proxy addresses */
1432 nd6_na_output(ifa->ifa_ifp, &in6_all, in6, 1419 nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
1433 (anycast ? 0 : ND_NA_FLAG_OVERRIDE) 1420 (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
1434#if 0 1421#if 0
1435 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) 1422 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
1436#endif 1423#endif
1437 , 1, NULL); 1424 , 1, NULL);
1438 return; 1425 return;
1439 } 1426 }
1440 1427
1441 if ((rt->rt_flags & RTF_GATEWAY) != 0) 1428 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1442 return; 1429 return;
1443 1430
1444 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { 1431 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1445 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1432 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1446 /* 1433 /*
1447 * This is probably an interface direct route for a link 1434 * This is probably an interface direct route for a link
1448 * which does not need neighbor caches (e.g. fe80::%lo0/64). 1435 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1449 * We do not need special treatment below for such a route. 1436 * We do not need special treatment below for such a route.
1450 * Moreover, the RTF_LLINFO flag which would be set below 1437 * Moreover, the RTF_LLINFO flag which would be set below
1451 * would annoy the ndp(8) command. 1438 * would annoy the ndp(8) command.
1452 */ 1439 */
1453 return; 1440 return;
1454 } 1441 }
1455 1442
1456 switch (req) { 1443 switch (req) {
1457 case RTM_ADD: { 1444 case RTM_ADD: {
1458 struct psref psref; 1445 struct psref psref;
1459 1446
1460 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1447 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1461 /* 1448 /*
1462 * There is no backward compatibility :) 1449 * There is no backward compatibility :)
1463 * 1450 *
1464 * if ((rt->rt_flags & RTF_HOST) == 0 && 1451 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1465 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1452 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1466 * rt->rt_flags |= RTF_CLONING; 1453 * rt->rt_flags |= RTF_CLONING;
1467 */ 1454 */
1468 /* XXX should move to route.c? */ 1455 /* XXX should move to route.c? */
1469 if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) { 1456 if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) {
1470 union { 1457 union {
1471 struct sockaddr sa; 1458 struct sockaddr sa;
1472 struct sockaddr_dl sdl; 1459 struct sockaddr_dl sdl;
1473 struct sockaddr_storage ss; 1460 struct sockaddr_storage ss;
1474 } u; 1461 } u;
1475 /* 1462 /*
1476 * Case 1: This route should come from a route to 1463 * Case 1: This route should come from a route to
1477 * interface (RTF_CLONING case) or the route should be 1464 * interface (RTF_CLONING case) or the route should be
1478 * treated as on-link but is currently not 1465 * treated as on-link but is currently not
1479 * (RTF_LLINFO && ln == NULL case). 1466 * (RTF_LLINFO && ln == NULL case).
1480 */ 1467 */
1481 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), 1468 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
1482 ifp->if_index, ifp->if_type, 1469 ifp->if_index, ifp->if_type,
1483 NULL, namelen, NULL, addrlen) == NULL) { 1470 NULL, namelen, NULL, addrlen) == NULL) {
1484 printf("%s.%d: sockaddr_dl_init(, %zu, ) " 1471 printf("%s.%d: sockaddr_dl_init(, %zu, ) "
1485 "failed on %s\n", __func__, __LINE__, 1472 "failed on %s\n", __func__, __LINE__,
1486 sizeof(u.ss), if_name(ifp)); 1473 sizeof(u.ss), if_name(ifp));
1487 } 1474 }
1488 rt_setgate(rt, &u.sa); 1475 rt_setgate(rt, &u.sa);
1489 gate = rt->rt_gateway; 1476 gate = rt->rt_gateway;
1490 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1477 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1491 if (gate == NULL) { 1478 if (gate == NULL) {
1492 log(LOG_ERR, 1479 log(LOG_ERR,
1493 "%s: rt_setgate failed on %s\n", __func__, 1480 "%s: rt_setgate failed on %s\n", __func__,
1494 if_name(ifp)); 1481 if_name(ifp));
1495 break; 1482 break;
1496 } 1483 }
1497 1484
1498 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1485 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1499 if ((rt->rt_flags & RTF_CONNECTED) != 0) 1486 if ((rt->rt_flags & RTF_CONNECTED) != 0)
1500 break; 1487 break;
1501 } 1488 }
1502 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1489 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1503 /* 1490 /*
1504 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. 1491 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1505 * We don't do that here since llinfo is not ready yet. 1492 * We don't do that here since llinfo is not ready yet.
1506 * 1493 *
1507 * There are also couple of other things to be discussed: 1494 * There are also couple of other things to be discussed:
1508 * - unsolicited NA code needs improvement beforehand 1495 * - unsolicited NA code needs improvement beforehand
1509 * - RFC2461 says we MAY send multicast unsolicited NA 1496 * - RFC2461 says we MAY send multicast unsolicited NA
1510 * (7.2.6 paragraph 4), however, it also says that we 1497 * (7.2.6 paragraph 4), however, it also says that we
1511 * SHOULD provide a mechanism to prevent multicast NA storm. 1498 * SHOULD provide a mechanism to prevent multicast NA storm.
1512 * we don't have anything like it right now. 1499 * we don't have anything like it right now.
1513 * note that the mechanism needs a mutual agreement 1500 * note that the mechanism needs a mutual agreement
1514 * between proxies, which means that we need to implement 1501 * between proxies, which means that we need to implement
1515 * a new protocol, or a new kludge. 1502 * a new protocol, or a new kludge.
1516 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. 1503 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1517 * we need to check ip6forwarding before sending it. 1504 * we need to check ip6forwarding before sending it.
1518 * (or should we allow proxy ND configuration only for 1505 * (or should we allow proxy ND configuration only for
1519 * routers? there's no mention about proxy ND from hosts) 1506 * routers? there's no mention about proxy ND from hosts)
1520 */ 1507 */
1521#if 0 1508#if 0
1522 /* XXX it does not work */ 1509 /* XXX it does not work */
1523 if (rt->rt_flags & RTF_ANNOUNCE) 1510 if (rt->rt_flags & RTF_ANNOUNCE)
1524 nd6_na_output(ifp, 1511 nd6_na_output(ifp,
1525 &satocsin6(rt_getkey(rt))->sin6_addr, 1512 &satocsin6(rt_getkey(rt))->sin6_addr,
1526 &satocsin6(rt_getkey(rt))->sin6_addr, 1513 &satocsin6(rt_getkey(rt))->sin6_addr,
1527 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, 1514 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1528 1, NULL); 1515 1, NULL);
1529#endif 1516#endif
1530 1517
1531 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 1518 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1532 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1519 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1533 /* 1520 /*
1534 * Address resolution isn't necessary for a point to 1521 * Address resolution isn't necessary for a point to
1535 * point link, so we can skip this test for a p2p link. 1522 * point link, so we can skip this test for a p2p link.
1536 */ 1523 */
1537 if (gate->sa_family != AF_LINK || 1524 if (gate->sa_family != AF_LINK ||
1538 gate->sa_len < 1525 gate->sa_len <
1539 sockaddr_dl_measure(namelen, addrlen)) { 1526 sockaddr_dl_measure(namelen, addrlen)) {
1540 log(LOG_DEBUG, 1527 log(LOG_DEBUG,
1541 "nd6_rtrequest: bad gateway value: %s\n", 1528 "nd6_rtrequest: bad gateway value: %s\n",
1542 if_name(ifp)); 1529 if_name(ifp));
1543 break; 1530 break;
1544 } 1531 }
1545 satosdl(gate)->sdl_type = ifp->if_type; 1532 satosdl(gate)->sdl_type = ifp->if_type;
1546 satosdl(gate)->sdl_index = ifp->if_index; 1533 satosdl(gate)->sdl_index = ifp->if_index;
1547 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1534 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1548 } 1535 }
1549 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1536 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1550 1537
1551 /* 1538 /*
1552 * When called from rt_ifa_addlocal, we cannot depend on that 1539 * When called from rt_ifa_addlocal, we cannot depend on that
1553 * the address (rt_getkey(rt)) exits in the address list of the 1540 * the address (rt_getkey(rt)) exits in the address list of the
1554 * interface. So check RTF_LOCAL instead. 1541 * interface. So check RTF_LOCAL instead.
1555 */ 1542 */
1556 if (rt->rt_flags & RTF_LOCAL) { 1543 if (rt->rt_flags & RTF_LOCAL) {
1557 if (nd6_useloopback) 1544 if (nd6_useloopback)
1558 rt->rt_ifp = lo0ifp; /* XXX */ 1545 rt->rt_ifp = lo0ifp; /* XXX */
1559 break; 1546 break;
1560 } 1547 }
1561 1548
1562 /* 1549 /*
1563 * check if rt_getkey(rt) is an address assigned 1550 * check if rt_getkey(rt) is an address assigned
1564 * to the interface. 1551 * to the interface.
1565 */ 1552 */
1566 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, 1553 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp,
1567 &satocsin6(rt_getkey(rt))->sin6_addr, &psref); 1554 &satocsin6(rt_getkey(rt))->sin6_addr, &psref);
1568 if (ifa != NULL) { 1555 if (ifa != NULL) {
1569 if (nd6_useloopback) { 1556 if (nd6_useloopback) {
1570 rt->rt_ifp = lo0ifp; /* XXX */ 1557 rt->rt_ifp = lo0ifp; /* XXX */
1571 /* 1558 /*
1572 * Make sure rt_ifa be equal to the ifaddr 1559 * Make sure rt_ifa be equal to the ifaddr
1573 * corresponding to the address. 1560 * corresponding to the address.
1574 * We need this because when we refer 1561 * We need this because when we refer
1575 * rt_ifa->ia6_flags in ip6_input, we assume 1562 * rt_ifa->ia6_flags in ip6_input, we assume
1576 * that the rt_ifa points to the address instead 1563 * that the rt_ifa points to the address instead
1577 * of the loopback address. 1564 * of the loopback address.
1578 */ 1565 */
1579 if (!ISSET(info->rti_flags, RTF_DONTCHANGEIFA) 1566 if (!ISSET(info->rti_flags, RTF_DONTCHANGEIFA)
1580 && ifa != rt->rt_ifa) 1567 && ifa != rt->rt_ifa)
1581 rt_replace_ifa(rt, ifa); 1568 rt_replace_ifa(rt, ifa);
1582 } 1569 }
1583 } else if (rt->rt_flags & RTF_ANNOUNCE) { 1570 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1584 /* join solicited node multicast for proxy ND */ 1571 /* join solicited node multicast for proxy ND */
1585 if (ifp->if_flags & IFF_MULTICAST) { 1572 if (ifp->if_flags & IFF_MULTICAST) {
1586 struct in6_addr llsol; 1573 struct in6_addr llsol;
1587 int error; 1574 int error;
1588 1575
1589 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1576 llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1590 llsol.s6_addr32[0] = htonl(0xff020000); 1577 llsol.s6_addr32[0] = htonl(0xff020000);
1591 llsol.s6_addr32[1] = 0; 1578 llsol.s6_addr32[1] = 0;
1592 llsol.s6_addr32[2] = htonl(1); 1579 llsol.s6_addr32[2] = htonl(1);
1593 llsol.s6_addr8[12] = 0xff; 1580 llsol.s6_addr8[12] = 0xff;
1594 if (in6_setscope(&llsol, ifp, NULL)) 1581 if (in6_setscope(&llsol, ifp, NULL))
1595 goto out; 1582 goto out;
1596 if (!in6_addmulti(&llsol, ifp, &error, 0)) { 1583 if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1597 char ip6buf[INET6_ADDRSTRLEN]; 1584 char ip6buf[INET6_ADDRSTRLEN];
1598 nd6log(LOG_ERR, "%s: failed to join " 1585 nd6log(LOG_ERR, "%s: failed to join "
1599 "%s (errno=%d)\n", if_name(ifp), 1586 "%s (errno=%d)\n", if_name(ifp),
1600 IN6_PRINT(ip6buf, &llsol), error); 1587 IN6_PRINT(ip6buf, &llsol), error);
1601 } 1588 }
1602 } 1589 }
1603 } 1590 }
1604 out: 1591 out:
1605 ifa_release(ifa, &psref); 1592 ifa_release(ifa, &psref);
1606 /* 1593 /*
1607 * If we have too many cache entries, initiate immediate 1594 * If we have too many cache entries, initiate immediate
1608 * purging for some entries. 1595 * purging for some entries.
1609 */ 1596 */
1610 if (rt->rt_ifp != NULL) 1597 if (rt->rt_ifp != NULL)
1611 nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL); 1598 nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL);
1612 break; 1599 break;
1613 } 1600 }
1614 1601
1615 case RTM_DELETE: 1602 case RTM_DELETE:
1616 /* leave from solicited node multicast for proxy ND */ 1603 /* leave from solicited node multicast for proxy ND */
1617 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && 1604 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1618 (ifp->if_flags & IFF_MULTICAST) != 0) { 1605 (ifp->if_flags & IFF_MULTICAST) != 0) {
1619 struct in6_addr llsol; 1606 struct in6_addr llsol;
1620 1607
1621 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1608 llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1622 llsol.s6_addr32[0] = htonl(0xff020000); 1609 llsol.s6_addr32[0] = htonl(0xff020000);
1623 llsol.s6_addr32[1] = 0; 1610 llsol.s6_addr32[1] = 0;
1624 llsol.s6_addr32[2] = htonl(1); 1611 llsol.s6_addr32[2] = htonl(1);
1625 llsol.s6_addr8[12] = 0xff; 1612 llsol.s6_addr8[12] = 0xff;
1626 if (in6_setscope(&llsol, ifp, NULL) == 0) 1613 if (in6_setscope(&llsol, ifp, NULL) == 0)
1627 in6_lookup_and_delete_multi(&llsol, ifp); 1614 in6_lookup_and_delete_multi(&llsol, ifp);
1628 } 1615 }
1629 break; 1616 break;
1630 } 1617 }
1631} 1618}
1632 1619
1633int 1620int
1634nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp) 1621nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1635{ 1622{
1636 struct in6_drlist *drl = (struct in6_drlist *)data; 1623 struct in6_drlist *drl = (struct in6_drlist *)data;
1637 struct in6_oprlist *oprl = (struct in6_oprlist *)data; 1624 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1638 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1625 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1639 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1626 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1640 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1627 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1641 struct nd_defrouter *dr; 1628 struct nd_defrouter *dr;
1642 struct nd_prefix *pr; 1629 struct nd_prefix *pr;
1643 int i = 0, error = 0; 1630 int i = 0, error = 0;
1644 1631
1645 switch (cmd) { 1632 switch (cmd) {
1646 case SIOCGDRLST_IN6: 1633 case SIOCGDRLST_IN6:
1647 /* 1634 /*
1648 * obsolete API, use sysctl under net.inet6.icmp6 1635 * obsolete API, use sysctl under net.inet6.icmp6
1649 */ 1636 */
1650 memset(drl, 0, sizeof(*drl)); 1637 memset(drl, 0, sizeof(*drl));
1651 ND6_RLOCK(); 1638 ND6_RLOCK();
1652 ND_DEFROUTER_LIST_FOREACH(dr) { 1639 ND_DEFROUTER_LIST_FOREACH(dr) {
1653 if (i >= DRLSTSIZ) 1640 if (i >= DRLSTSIZ)
1654 break; 1641 break;
1655 drl->defrouter[i].rtaddr = dr->rtaddr; 1642 drl->defrouter[i].rtaddr = dr->rtaddr;
1656 in6_clearscope(&drl->defrouter[i].rtaddr); 1643 in6_clearscope(&drl->defrouter[i].rtaddr);
1657 1644
1658 drl->defrouter[i].flags = dr->flags; 1645 drl->defrouter[i].flags = dr->flags;
1659 drl->defrouter[i].rtlifetime = dr->rtlifetime; 1646 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1660 drl->defrouter[i].expire = dr->expire ? 1647 drl->defrouter[i].expire = dr->expire ?
1661 time_mono_to_wall(dr->expire) : 0; 1648 time_mono_to_wall(dr->expire) : 0;
1662 drl->defrouter[i].if_index = dr->ifp->if_index; 1649 drl->defrouter[i].if_index = dr->ifp->if_index;
1663 i++; 1650 i++;
1664 } 1651 }
1665 ND6_UNLOCK(); 1652 ND6_UNLOCK();
1666 break; 1653 break;
1667 case SIOCGPRLST_IN6: 1654 case SIOCGPRLST_IN6:
1668 /* 1655 /*
1669 * obsolete API, use sysctl under net.inet6.icmp6 1656 * obsolete API, use sysctl under net.inet6.icmp6
1670 * 1657 *
1671 * XXX the structure in6_prlist was changed in backward- 1658 * XXX the structure in6_prlist was changed in backward-
1672 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6, 1659 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1673 * in6_prlist is used for nd6_sysctl() - fill_prlist(). 1660 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1674 */ 1661 */
1675 /* 1662 /*
1676 * XXX meaning of fields, especialy "raflags", is very 1663 * XXX meaning of fields, especialy "raflags", is very
1677 * differnet between RA prefix list and RR/static prefix list. 1664 * differnet between RA prefix list and RR/static prefix list.
1678 * how about separating ioctls into two? 1665 * how about separating ioctls into two?
1679 */ 1666 */
1680 memset(oprl, 0, sizeof(*oprl)); 1667 memset(oprl, 0, sizeof(*oprl));
1681 ND6_RLOCK(); 1668 ND6_RLOCK();
1682 ND_PREFIX_LIST_FOREACH(pr) { 1669 ND_PREFIX_LIST_FOREACH(pr) {
1683 struct nd_pfxrouter *pfr; 1670 struct nd_pfxrouter *pfr;
1684 int j; 1671 int j;
1685 1672
1686 if (i >= PRLSTSIZ) 1673 if (i >= PRLSTSIZ)
1687 break; 1674 break;
1688 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr; 1675 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1689 oprl->prefix[i].raflags = pr->ndpr_raf; 1676 oprl->prefix[i].raflags = pr->ndpr_raf;
1690 oprl->prefix[i].prefixlen = pr->ndpr_plen; 1677 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1691 oprl->prefix[i].vltime = pr->ndpr_vltime; 1678 oprl->prefix[i].vltime = pr->ndpr_vltime;
1692 oprl->prefix[i].pltime = pr->ndpr_pltime; 1679 oprl->prefix[i].pltime = pr->ndpr_pltime;
1693 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index; 1680 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1694 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1681 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1695 oprl->prefix[i].expire = 0; 1682 oprl->prefix[i].expire = 0;
1696 else { 1683 else {
1697 time_t maxexpire; 1684 time_t maxexpire;
1698 1685
1699 /* XXX: we assume time_t is signed. */ 1686 /* XXX: we assume time_t is signed. */
1700 maxexpire = (-1) & 1687 maxexpire = (-1) &
1701 ~((time_t)1 << 1688 ~((time_t)1 <<
1702 ((sizeof(maxexpire) * 8) - 1)); 1689 ((sizeof(maxexpire) * 8) - 1));
1703 if (pr->ndpr_vltime < 1690 if (pr->ndpr_vltime <
1704 maxexpire - pr->ndpr_lastupdate) { 1691 maxexpire - pr->ndpr_lastupdate) {
1705 time_t expire; 1692 time_t expire;
1706 expire = pr->ndpr_lastupdate + 1693 expire = pr->ndpr_lastupdate +
1707 pr->ndpr_vltime; 1694 pr->ndpr_vltime;
1708 oprl->prefix[i].expire = expire ? 1695 oprl->prefix[i].expire = expire ?
1709 time_mono_to_wall(expire) : 0; 1696 time_mono_to_wall(expire) : 0;
1710 } else 1697 } else
1711 oprl->prefix[i].expire = maxexpire; 1698 oprl->prefix[i].expire = maxexpire;
1712 } 1699 }
1713 1700
1714 j = 0; 1701 j = 0;
1715 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 1702 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1716 if (j < DRLSTSIZ) { 1703 if (j < DRLSTSIZ) {
1717#define RTRADDR oprl->prefix[i].advrtr[j] 1704#define RTRADDR oprl->prefix[i].advrtr[j]
1718 RTRADDR = pfr->router->rtaddr; 1705 RTRADDR = pfr->router->rtaddr;
1719 in6_clearscope(&RTRADDR); 1706 in6_clearscope(&RTRADDR);
1720#undef RTRADDR 1707#undef RTRADDR
1721 } 1708 }
1722 j++; 1709 j++;
1723 } 1710 }
1724 oprl->prefix[i].advrtrs = j; 1711 oprl->prefix[i].advrtrs = j;
1725 oprl->prefix[i].origin = PR_ORIG_RA; 1712 oprl->prefix[i].origin = PR_ORIG_RA;
1726 1713
1727 i++; 1714 i++;
1728 } 1715 }
1729 ND6_UNLOCK(); 1716 ND6_UNLOCK();
1730 1717
1731 break; 1718 break;
1732 case OSIOCGIFINFO_IN6: 1719 case OSIOCGIFINFO_IN6:
1733#define ND ndi->ndi 1720#define ND ndi->ndi
1734 /* XXX: old ndp(8) assumes a positive value for linkmtu. */ 1721 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1735 memset(&ND, 0, sizeof(ND)); 1722 memset(&ND, 0, sizeof(ND));
1736 ND.linkmtu = IN6_LINKMTU(ifp); 1723 ND.linkmtu = IN6_LINKMTU(ifp);
1737 ND.maxmtu = ND_IFINFO(ifp)->maxmtu; 1724 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1738 ND.basereachable = ND_IFINFO(ifp)->basereachable; 1725 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1739 ND.reachable = ND_IFINFO(ifp)->reachable; 1726 ND.reachable = ND_IFINFO(ifp)->reachable;
1740 ND.retrans = ND_IFINFO(ifp)->retrans; 1727 ND.retrans = ND_IFINFO(ifp)->retrans;
1741 ND.flags = ND_IFINFO(ifp)->flags; 1728 ND.flags = ND_IFINFO(ifp)->flags;
1742 ND.recalctm = ND_IFINFO(ifp)->recalctm; 1729 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1743 ND.chlim = ND_IFINFO(ifp)->chlim; 1730 ND.chlim = ND_IFINFO(ifp)->chlim;
1744 break; 1731 break;
1745 case SIOCGIFINFO_IN6: 1732 case SIOCGIFINFO_IN6:
1746 ND = *ND_IFINFO(ifp); 1733 ND = *ND_IFINFO(ifp);
1747 break; 1734 break;
1748 case SIOCSIFINFO_IN6: 1735 case SIOCSIFINFO_IN6:
1749 /*  1736 /*
1750 * used to change host variables from userland. 1737 * used to change host variables from userland.
1751 * intented for a use on router to reflect RA configurations. 1738 * intented for a use on router to reflect RA configurations.
1752 */ 1739 */
1753 /* 0 means 'unspecified' */ 1740 /* 0 means 'unspecified' */
1754 if (ND.linkmtu != 0) { 1741 if (ND.linkmtu != 0) {
1755 if (ND.linkmtu < IPV6_MMTU || 1742 if (ND.linkmtu < IPV6_MMTU ||
1756 ND.linkmtu > IN6_LINKMTU(ifp)) { 1743 ND.linkmtu > IN6_LINKMTU(ifp)) {
1757 error = EINVAL; 1744 error = EINVAL;
1758 break; 1745 break;
1759 } 1746 }
1760 ND_IFINFO(ifp)->linkmtu = ND.linkmtu; 1747 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1761 } 1748 }
1762 1749
1763 if (ND.basereachable != 0) { 1750 if (ND.basereachable != 0) {
1764 int obasereachable = ND_IFINFO(ifp)->basereachable; 1751 int obasereachable = ND_IFINFO(ifp)->basereachable;
1765 1752
1766 ND_IFINFO(ifp)->basereachable = ND.basereachable; 1753 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1767 if (ND.basereachable != obasereachable) 1754 if (ND.basereachable != obasereachable)
1768 ND_IFINFO(ifp)->reachable = 1755 ND_IFINFO(ifp)->reachable =
1769 ND_COMPUTE_RTIME(ND.basereachable); 1756 ND_COMPUTE_RTIME(ND.basereachable);
1770 } 1757 }
1771 if (ND.retrans != 0) 1758 if (ND.retrans != 0)
1772 ND_IFINFO(ifp)->retrans = ND.retrans; 1759 ND_IFINFO(ifp)->retrans = ND.retrans;
1773 if (ND.chlim != 0) 1760 if (ND.chlim != 0)
1774 ND_IFINFO(ifp)->chlim = ND.chlim; 1761 ND_IFINFO(ifp)->chlim = ND.chlim;
1775 /* FALLTHROUGH */ 1762 /* FALLTHROUGH */
1776 case SIOCSIFINFO_FLAGS: 1763 case SIOCSIFINFO_FLAGS:
1777 { 1764 {
1778 struct ifaddr *ifa; 1765 struct ifaddr *ifa;
1779 struct in6_ifaddr *ia; 1766 struct in6_ifaddr *ia;
1780 int s; 1767 int s;
1781 1768
1782 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1769 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1783 !(ND.flags & ND6_IFF_IFDISABLED)) 1770 !(ND.flags & ND6_IFF_IFDISABLED))
1784 { 1771 {
1785 /* 1772 /*
1786 * If the interface is marked as ND6_IFF_IFDISABLED and 1773 * If the interface is marked as ND6_IFF_IFDISABLED and
1787 * has a link-local address with IN6_IFF_DUPLICATED, 1774 * has a link-local address with IN6_IFF_DUPLICATED,
1788 * do not clear ND6_IFF_IFDISABLED. 1775 * do not clear ND6_IFF_IFDISABLED.
1789 * See RFC 4862, section 5.4.5. 1776 * See RFC 4862, section 5.4.5.
1790 */ 1777 */
1791 int duplicated_linklocal = 0; 1778 int duplicated_linklocal = 0;
1792 1779
1793 s = pserialize_read_enter(); 1780 s = pserialize_read_enter();
1794 IFADDR_READER_FOREACH(ifa, ifp) { 1781 IFADDR_READER_FOREACH(ifa, ifp) {
1795 if (ifa->ifa_addr->sa_family != AF_INET6) 1782 if (ifa->ifa_addr->sa_family != AF_INET6)
1796 continue; 1783 continue;
1797 ia = (struct in6_ifaddr *)ifa; 1784 ia = (struct in6_ifaddr *)ifa;
1798 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) && 1785 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1799 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1786 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1800 { 1787 {
1801 duplicated_linklocal = 1; 1788 duplicated_linklocal = 1;
1802 break; 1789 break;
1803 } 1790 }
1804 } 1791 }
1805 pserialize_read_exit(s); 1792 pserialize_read_exit(s);
1806 1793
1807 if (duplicated_linklocal) { 1794 if (duplicated_linklocal) {
1808 ND.flags |= ND6_IFF_IFDISABLED; 1795 ND.flags |= ND6_IFF_IFDISABLED;
1809 log(LOG_ERR, "%s: Cannot enable an interface" 1796 log(LOG_ERR, "%s: Cannot enable an interface"
1810 " with a link-local address marked" 1797 " with a link-local address marked"
1811 " duplicate.\n", if_name(ifp)); 1798 " duplicate.\n", if_name(ifp));
1812 } else { 1799 } else {
1813 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED; 1800 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1814 if (ifp->if_flags & IFF_UP) 1801 if (ifp->if_flags & IFF_UP)
1815 in6_if_up(ifp); 1802 in6_if_up(ifp);
1816 } 1803 }
1817 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1804 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1818 (ND.flags & ND6_IFF_IFDISABLED)) { 1805 (ND.flags & ND6_IFF_IFDISABLED)) {
1819 int bound = curlwp_bind();  1806 int bound = curlwp_bind();
1820 /* Mark all IPv6 addresses as tentative. */ 1807 /* Mark all IPv6 addresses as tentative. */
1821 1808
1822 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1809 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1823 s = pserialize_read_enter(); 1810 s = pserialize_read_enter();
1824 IFADDR_READER_FOREACH(ifa, ifp) { 1811 IFADDR_READER_FOREACH(ifa, ifp) {
1825 struct psref psref; 1812 struct psref psref;
1826 if (ifa->ifa_addr->sa_family != AF_INET6) 1813 if (ifa->ifa_addr->sa_family != AF_INET6)
1827 continue; 1814 continue;
1828 ifa_acquire(ifa, &psref); 1815 ifa_acquire(ifa, &psref);
1829 pserialize_read_exit(s); 1816 pserialize_read_exit(s);
1830 1817
1831 nd6_dad_stop(ifa); 1818 nd6_dad_stop(ifa);
1832 1819
1833 ia = (struct in6_ifaddr *)ifa; 1820 ia = (struct in6_ifaddr *)ifa;
1834 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1821 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1835 1822
1836 s = pserialize_read_enter(); 1823 s = pserialize_read_enter();
1837 ifa_release(ifa, &psref); 1824 ifa_release(ifa, &psref);
1838 } 1825 }
1839 pserialize_read_exit(s); 1826 pserialize_read_exit(s);
1840 curlwp_bindx(bound); 1827 curlwp_bindx(bound);
1841 } 1828 }
1842 1829
1843 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) { 1830 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1844 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) { 1831 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1845 /* auto_linklocal 0->1 transition */ 1832 /* auto_linklocal 0->1 transition */
1846 1833
1847 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL; 1834 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1848 in6_ifattach(ifp, NULL); 1835 in6_ifattach(ifp, NULL);
1849 } else if (!(ND.flags & ND6_IFF_IFDISABLED) && 1836 } else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1850 ifp->if_flags & IFF_UP) 1837 ifp->if_flags & IFF_UP)
1851 { 1838 {
1852 /* 1839 /*
1853 * When the IF already has 1840 * When the IF already has
1854 * ND6_IFF_AUTO_LINKLOCAL, no link-local 1841 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1855 * address is assigned, and IFF_UP, try to 1842 * address is assigned, and IFF_UP, try to
1856 * assign one. 1843 * assign one.
1857 */ 1844 */
1858 int haslinklocal = 0; 1845 int haslinklocal = 0;
1859 1846
1860 s = pserialize_read_enter(); 1847 s = pserialize_read_enter();
1861 IFADDR_READER_FOREACH(ifa, ifp) { 1848 IFADDR_READER_FOREACH(ifa, ifp) {
1862 if (ifa->ifa_addr->sa_family !=AF_INET6) 1849 if (ifa->ifa_addr->sa_family !=AF_INET6)
1863 continue; 1850 continue;
1864 ia = (struct in6_ifaddr *)ifa; 1851 ia = (struct in6_ifaddr *)ifa;
1865 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){ 1852 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
1866 haslinklocal = 1; 1853 haslinklocal = 1;
1867 break; 1854 break;
1868 } 1855 }
1869 } 1856 }
1870 pserialize_read_exit(s); 1857 pserialize_read_exit(s);
1871 if (!haslinklocal) 1858 if (!haslinklocal)
1872 in6_ifattach(ifp, NULL); 1859 in6_ifattach(ifp, NULL);
1873 } 1860 }
1874 } 1861 }
1875 } 1862 }
1876 ND_IFINFO(ifp)->flags = ND.flags; 1863 ND_IFINFO(ifp)->flags = ND.flags;
1877 break; 1864 break;
1878#undef ND 1865#undef ND
1879 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1866 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1880 /* sync kernel routing table with the default router list */ 1867 /* sync kernel routing table with the default router list */
1881 ND6_WLOCK(); 1868 ND6_WLOCK();
1882 nd6_defrouter_reset(); 1869 nd6_defrouter_reset();
1883 nd6_defrouter_select(); 1870 nd6_defrouter_select();
1884 ND6_UNLOCK(); 1871 ND6_UNLOCK();
1885 break; 1872 break;
1886 case SIOCSPFXFLUSH_IN6: 1873 case SIOCSPFXFLUSH_IN6:
1887 { 1874 {
1888 /* flush all the prefix advertised by routers */ 1875 /* flush all the prefix advertised by routers */
1889 struct nd_prefix *pfx, *next; 1876 struct nd_prefix *pfx, *next;
1890 1877
1891 restart: 1878 restart:
1892 ND6_WLOCK(); 1879 ND6_WLOCK();
1893 ND_PREFIX_LIST_FOREACH_SAFE(pfx, next) { 1880 ND_PREFIX_LIST_FOREACH_SAFE(pfx, next) {
1894 struct in6_ifaddr *ia, *ia_next; 1881 struct in6_ifaddr *ia, *ia_next;
1895 int _s; 1882 int _s;
1896 1883
1897 /* Only flush prefixes for the given interface. */ 1884 /* Only flush prefixes for the given interface. */
1898 if (ifp != lo0ifp && ifp != pfx->ndpr_ifp) 1885 if (ifp != lo0ifp && ifp != pfx->ndpr_ifp)
1899 continue; 1886 continue;
1900 1887
1901 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr)) 1888 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
1902 continue; /* XXX */ 1889 continue; /* XXX */
1903 1890
1904 /* do we really have to remove addresses as well? */ 1891 /* do we really have to remove addresses as well? */
1905 _s = pserialize_read_enter(); 1892 _s = pserialize_read_enter();
1906 for (ia = IN6_ADDRLIST_READER_FIRST(); ia; 1893 for (ia = IN6_ADDRLIST_READER_FIRST(); ia;
1907 ia = ia_next) { 1894 ia = ia_next) {
1908 struct ifnet *ifa_ifp; 1895 struct ifnet *ifa_ifp;
1909 int bound; 1896 int bound;
1910 struct psref psref; 1897 struct psref psref;
1911 1898
1912 /* ia might be removed. keep the next ptr. */ 1899 /* ia might be removed. keep the next ptr. */
1913 ia_next = IN6_ADDRLIST_READER_NEXT(ia); 1900 ia_next = IN6_ADDRLIST_READER_NEXT(ia);
1914 1901
1915 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1902 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1916 continue; 1903 continue;
1917 1904
1918 if (ia->ia6_ndpr != pfx) 1905 if (ia->ia6_ndpr != pfx)
1919 continue; 1906 continue;
1920 1907
1921 bound = curlwp_bind(); 1908 bound = curlwp_bind();
1922 ia6_acquire(ia, &psref); 1909 ia6_acquire(ia, &psref);
1923 pserialize_read_exit(_s); 1910 pserialize_read_exit(_s);
1924 ND6_UNLOCK(); 1911 ND6_UNLOCK();
1925 1912
1926 ifa_ifp = ia->ia_ifa.ifa_ifp; 1913 ifa_ifp = ia->ia_ifa.ifa_ifp;
1927 if (ifa_ifp == ifp) { 1914 if (ifa_ifp == ifp) {
1928 /* Already have IFNET_LOCK(ifp) */ 1915 /* Already have IFNET_LOCK(ifp) */
1929 KASSERT(!if_is_deactivated(ifp)); 1916 KASSERT(!if_is_deactivated(ifp));
1930 ia6_release(ia, &psref); 1917 ia6_release(ia, &psref);
1931 in6_purgeaddr(&ia->ia_ifa); 1918 in6_purgeaddr(&ia->ia_ifa);
1932 curlwp_bindx(bound); 1919 curlwp_bindx(bound);
1933 goto restart; 1920 goto restart;
1934 } 1921 }
1935 IFNET_LOCK(ifa_ifp); 1922 IFNET_LOCK(ifa_ifp);
1936 /* 1923 /*
1937 * Need to take the lock first to prevent 1924 * Need to take the lock first to prevent
1938 * if_detach from running in6_purgeaddr 1925 * if_detach from running in6_purgeaddr
1939 * concurrently. 1926 * concurrently.
1940 */ 1927 */
1941 if (!if_is_deactivated(ifa_ifp)) { 1928 if (!if_is_deactivated(ifa_ifp)) {
1942 ia6_release(ia, &psref); 1929 ia6_release(ia, &psref);
1943 in6_purgeaddr(&ia->ia_ifa); 1930 in6_purgeaddr(&ia->ia_ifa);
1944 } else { 1931 } else {
1945 /* 1932 /*
1946 * ifp is being destroyed, ia will be 1933 * ifp is being destroyed, ia will be
1947 * destroyed by if_detach. 1934 * destroyed by if_detach.
1948 */ 1935 */
1949 ia6_release(ia, &psref); 1936 ia6_release(ia, &psref);
1950 /* XXX may cause busy loop */ 1937 /* XXX may cause busy loop */
1951 } 1938 }
1952 IFNET_UNLOCK(ifa_ifp); 1939 IFNET_UNLOCK(ifa_ifp);
1953 curlwp_bindx(bound); 1940 curlwp_bindx(bound);
1954 goto restart; 1941 goto restart;
1955 } 1942 }
1956 pserialize_read_exit(_s); 1943 pserialize_read_exit(_s);
1957 1944
1958 KASSERT(pfx->ndpr_refcnt == 0); 1945 KASSERT(pfx->ndpr_refcnt == 0);
1959 nd6_prelist_remove(pfx); 1946 nd6_prelist_remove(pfx);
1960 } 1947 }
1961 ND6_UNLOCK(); 1948 ND6_UNLOCK();
1962 break; 1949 break;
1963 } 1950 }
1964 case SIOCSRTRFLUSH_IN6: 1951 case SIOCSRTRFLUSH_IN6:
1965 { 1952 {
1966 /* flush all the default routers */ 1953 /* flush all the default routers */
1967 struct nd_defrouter *drtr, *next; 1954 struct nd_defrouter *drtr, *next;
1968 1955
1969 ND6_WLOCK(); 1956 ND6_WLOCK();
1970#if 0 1957#if 0
1971 /* XXX Is this really needed? */ 1958 /* XXX Is this really needed? */
1972 nd6_defrouter_reset(); 1959 nd6_defrouter_reset();
1973#endif 1960#endif
1974 ND_DEFROUTER_LIST_FOREACH_SAFE(drtr, next) { 1961 ND_DEFROUTER_LIST_FOREACH_SAFE(drtr, next) {
1975 /* Only flush routers for the given interface. */ 1962 /* Only flush routers for the given interface. */
1976 if (ifp != lo0ifp && ifp != drtr->ifp) 1963 if (ifp != lo0ifp && ifp != drtr->ifp)
1977 continue; 1964 continue;
1978 1965
1979 nd6_defrtrlist_del(drtr, NULL); 1966 nd6_defrtrlist_del(drtr, NULL);
1980 } 1967 }
1981 nd6_defrouter_select(); 1968 nd6_defrouter_select();
1982 ND6_UNLOCK(); 1969 ND6_UNLOCK();
1983 break; 1970 break;
1984 } 1971 }
1985 case SIOCGNBRINFO_IN6: 1972 case SIOCGNBRINFO_IN6:
1986 { 1973 {
1987 struct llentry *ln; 1974 struct llentry *ln;
1988 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1975 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1989 1976
1990 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1977 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1991 return error; 1978 return error;
1992 1979
1993 ln = nd6_lookup(&nb_addr, ifp, false); 1980 ln = nd6_lookup(&nb_addr, ifp, false);
1994 if (ln == NULL) { 1981 if (ln == NULL) {
1995 error = EINVAL; 1982 error = EINVAL;
1996 break; 1983 break;
1997 } 1984 }
1998 nbi->state = ln->ln_state; 1985 nbi->state = ln->ln_state;
1999 nbi->asked = ln->ln_asked; 1986 nbi->asked = ln->ln_asked;
2000 nbi->isrouter = ln->ln_router; 1987 nbi->isrouter = ln->ln_router;
2001 nbi->expire = ln->ln_expire ? 1988 nbi->expire = ln->ln_expire ?
2002 time_mono_to_wall(ln->ln_expire) : 0; 1989 time_mono_to_wall(ln->ln_expire) : 0;
2003 LLE_RUNLOCK(ln); 1990 LLE_RUNLOCK(ln);
2004 1991
2005 break; 1992 break;
2006 } 1993 }
2007 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1994 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
2008 ndif->ifindex = nd6_defifindex; 1995 ndif->ifindex = nd6_defifindex;
2009 break; 1996 break;
2010 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1997 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
2011 return nd6_setdefaultiface(ndif->ifindex); 1998 return nd6_setdefaultiface(ndif->ifindex);
2012 } 1999 }
2013 return error; 2000 return error;
2014} 2001}
2015 2002
2016void 2003void
2017nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp) 2004nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp)
2018{ 2005{
2019 struct mbuf *m_hold, *m_hold_next; 2006 struct mbuf *m_hold, *m_hold_next;
2020 struct sockaddr_in6 sin6; 2007 struct sockaddr_in6 sin6;
2021 2008
2022 LLE_WLOCK_ASSERT(ln); 2009 LLE_WLOCK_ASSERT(ln);
2023 2010
2024 sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0); 2011 sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
2025 2012
2026 m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0; 2013 m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0;
2027 2014
2028 LLE_WUNLOCK(ln); 2015 LLE_WUNLOCK(ln);
2029 for (; m_hold != NULL; m_hold = m_hold_next) { 2016 for (; m_hold != NULL; m_hold = m_hold_next) {
2030 m_hold_next = m_hold->m_nextpkt; 2017 m_hold_next = m_hold->m_nextpkt;
2031 m_hold->m_nextpkt = NULL; 2018 m_hold->m_nextpkt = NULL;
2032 2019
2033 /* 2020 /*
2034 * we assume ifp is not a p2p here, so 2021 * we assume ifp is not a p2p here, so
2035 * just set the 2nd argument as the  2022 * just set the 2nd argument as the
2036 * 1st one. 2023 * 1st one.
2037 */ 2024 */
2038 ip6_if_output(ifp, ifp, m_hold, &sin6, NULL); 2025 ip6_if_output(ifp, ifp, m_hold, &sin6, NULL);
2039 } 2026 }
2040 LLE_WLOCK(ln); 2027 LLE_WLOCK(ln);
2041} 2028}
2042 2029
2043/* 2030/*
2044 * Create neighbor cache entry and cache link-layer address, 2031 * Create neighbor cache entry and cache link-layer address,
2045 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 2032 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
2046 */ 2033 */
2047void 2034void
2048nd6_cache_lladdr( 2035nd6_cache_lladdr(
2049 struct ifnet *ifp, 2036 struct ifnet *ifp,
2050 struct in6_addr *from, 2037 struct in6_addr *from,
2051 char *lladdr, 2038 char *lladdr,
2052 int lladdrlen, 2039 int lladdrlen,
2053 int type, /* ICMP6 type */ 2040 int type, /* ICMP6 type */
2054 int code /* type dependent information */ 2041 int code /* type dependent information */
2055) 2042)
2056{ 2043{
2057 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 2044 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
2058 struct llentry *ln = NULL; 2045 struct llentry *ln = NULL;
2059 int is_newentry; 2046 int is_newentry;
2060 int do_update; 2047 int do_update;
2061 int olladdr; 2048 int olladdr;
2062 int llchange; 2049 int llchange;
2063 int newstate = 0; 2050 int newstate = 0;
2064 uint16_t router = 0; 2051 uint16_t router = 0;
2065 2052
2066 KASSERT(ifp != NULL); 2053 KASSERT(ifp != NULL);
2067 KASSERT(from != NULL); 2054 KASSERT(from != NULL);
2068 2055
2069 /* nothing must be updated for unspecified address */ 2056 /* nothing must be updated for unspecified address */
2070 if (IN6_IS_ADDR_UNSPECIFIED(from)) 2057 if (IN6_IS_ADDR_UNSPECIFIED(from))
2071 return; 2058 return;
2072 2059
2073 /* 2060 /*
2074 * Validation about ifp->if_addrlen and lladdrlen must be done in 2061 * Validation about ifp->if_addrlen and lladdrlen must be done in
2075 * the caller. 2062 * the caller.
2076 * 2063 *
2077 * XXX If the link does not have link-layer adderss, what should 2064 * XXX If the link does not have link-layer adderss, what should
2078 * we do? (ifp->if_addrlen == 0) 2065 * we do? (ifp->if_addrlen == 0)
2079 * Spec says nothing in sections for RA, RS and NA. There's small 2066 * Spec says nothing in sections for RA, RS and NA. There's small
2080 * description on it in NS section (RFC 2461 7.2.3). 2067 * description on it in NS section (RFC 2461 7.2.3).
2081 */ 2068 */
2082 2069
2083 ln = nd6_lookup(from, ifp, true); 2070 ln = nd6_lookup(from, ifp, true);
2084 if (ln == NULL) { 2071 if (ln == NULL) {
2085#if 0 2072#if 0
2086 /* nothing must be done if there's no lladdr */ 2073 /* nothing must be done if there's no lladdr */
2087 if (!lladdr || !lladdrlen) 2074 if (!lladdr || !lladdrlen)
2088 return NULL; 2075 return NULL;
2089#endif 2076#endif
2090 2077
2091 ln = nd6_create(from, ifp); 2078 ln = nd6_create(from, ifp);
2092 is_newentry = 1; 2079 is_newentry = 1;
2093 } else { 2080 } else {
2094 /* do nothing if static ndp is set */ 2081 /* do nothing if static ndp is set */
2095 if (ln->la_flags & LLE_STATIC) { 2082 if (ln->la_flags & LLE_STATIC) {
2096 LLE_WUNLOCK(ln); 2083 LLE_WUNLOCK(ln);
2097 return; 2084 return;
2098 } 2085 }
2099 is_newentry = 0; 2086 is_newentry = 0;
2100 } 2087 }
2101 2088
2102 if (ln == NULL) 2089 if (ln == NULL)
2103 return; 2090 return;
2104 2091
2105 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0; 2092 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
2106 if (olladdr && lladdr) { 2093 if (olladdr && lladdr) {
2107 llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen); 2094 llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen);
2108 } else 2095 } else
2109 llchange = 0; 2096 llchange = 0;
2110 2097
2111 /* 2098 /*
2112 * newentry olladdr lladdr llchange (*=record) 2099 * newentry olladdr lladdr llchange (*=record)
2113 * 0 n n -- (1) 2100 * 0 n n -- (1)
2114 * 0 y n -- (2) 2101 * 0 y n -- (2)
2115 * 0 n y -- (3) * STALE 2102 * 0 n y -- (3) * STALE
2116 * 0 y y n (4) * 2103 * 0 y y n (4) *
2117 * 0 y y y (5) * STALE 2104 * 0 y y y (5) * STALE
2118 * 1 -- n -- (6) NOSTATE(= PASSIVE) 2105 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2119 * 1 -- y -- (7) * STALE 2106 * 1 -- y -- (7) * STALE
2120 */ 2107 */
2121 2108
2122 if (lladdr) { /* (3-5) and (7) */ 2109 if (lladdr) { /* (3-5) and (7) */
2123 /* 2110 /*
2124 * Record source link-layer address 2111 * Record source link-layer address
2125 * XXX is it dependent to ifp->if_type? 2112 * XXX is it dependent to ifp->if_type?
2126 */ 2113 */
2127 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2114 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
2128 ln->la_flags |= LLE_VALID; 2115 ln->la_flags |= LLE_VALID;
2129 } 2116 }
2130 2117
2131 if (!is_newentry) { 2118 if (!is_newentry) {
2132 if ((!olladdr && lladdr) || /* (3) */ 2119 if ((!olladdr && lladdr) || /* (3) */
2133 (olladdr && lladdr && llchange)) { /* (5) */ 2120 (olladdr && lladdr && llchange)) { /* (5) */
2134 do_update = 1; 2121 do_update = 1;
2135 newstate = ND6_LLINFO_STALE; 2122 newstate = ND6_LLINFO_STALE;
2136 } else /* (1-2,4) */ 2123 } else /* (1-2,4) */
2137 do_update = 0; 2124 do_update = 0;
2138 } else { 2125 } else {
2139 do_update = 1; 2126 do_update = 1;
2140 if (lladdr == NULL) /* (6) */ 2127 if (lladdr == NULL) /* (6) */
2141 newstate = ND6_LLINFO_NOSTATE; 2128 newstate = ND6_LLINFO_NOSTATE;
2142 else /* (7) */ 2129 else /* (7) */
2143 newstate = ND6_LLINFO_STALE; 2130 newstate = ND6_LLINFO_STALE;
2144 } 2131 }
2145 2132
2146 if (do_update) { 2133 if (do_update) {
2147 /* 2134 /*
2148 * Update the state of the neighbor cache. 2135 * Update the state of the neighbor cache.
2149 */ 2136 */
2150 ln->ln_state = newstate; 2137 ln->ln_state = newstate;
2151 2138
2152 if (ln->ln_state == ND6_LLINFO_STALE) { 2139 if (ln->ln_state == ND6_LLINFO_STALE) {
2153 /* 2140 /*
2154 * XXX: since nd6_output() below will cause 2141 * XXX: since nd6_output() below will cause
2155 * state tansition to DELAY and reset the timer, 2142 * state tansition to DELAY and reset the timer,
2156 * we must set the timer now, although it is actually 2143 * we must set the timer now, although it is actually
2157 * meaningless. 2144 * meaningless.
2158 */ 2145 */
2159 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 2146 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
2160 2147
2161 nd6_llinfo_release_pkts(ln, ifp); 2148 nd6_llinfo_release_pkts(ln, ifp);
2162 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 2149 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
2163 /* probe right away */ 2150 /* probe right away */
2164 nd6_llinfo_settimer((void *)ln, 0); 2151 nd6_llinfo_settimer((void *)ln, 0);
2165 } 2152 }
2166 } 2153 }
2167 2154
2168 /* 2155 /*
2169 * ICMP6 type dependent behavior. 2156 * ICMP6 type dependent behavior.
2170 * 2157 *
2171 * NS: clear IsRouter if new entry 2158 * NS: clear IsRouter if new entry
2172 * RS: clear IsRouter 2159 * RS: clear IsRouter
2173 * RA: set IsRouter if there's lladdr 2160 * RA: set IsRouter if there's lladdr
2174 * redir: clear IsRouter if new entry 2161 * redir: clear IsRouter if new entry
2175 * 2162 *
2176 * RA case, (1): 2163 * RA case, (1):
2177 * The spec says that we must set IsRouter in the following cases: 2164 * The spec says that we must set IsRouter in the following cases:
2178 * - If lladdr exist, set IsRouter. This means (1-5). 2165 * - If lladdr exist, set IsRouter. This means (1-5).
2179 * - If it is old entry (!newentry), set IsRouter. This means (7). 2166 * - If it is old entry (!newentry), set IsRouter. This means (7).
2180 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 2167 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
2181 * A quetion arises for (1) case. (1) case has no lladdr in the 2168 * A quetion arises for (1) case. (1) case has no lladdr in the
2182 * neighbor cache, this is similar to (6). 2169 * neighbor cache, this is similar to (6).
2183 * This case is rare but we figured that we MUST NOT set IsRouter. 2170 * This case is rare but we figured that we MUST NOT set IsRouter.
2184 * 2171 *
2185 * newentry olladdr lladdr llchange NS RS RA redir 2172 * newentry olladdr lladdr llchange NS RS RA redir
2186 * D R 2173 * D R
2187 * 0 n n -- (1) c ? s 2174 * 0 n n -- (1) c ? s
2188 * 0 y n -- (2) c s s 2175 * 0 y n -- (2) c s s
2189 * 0 n y -- (3) c s s 2176 * 0 n y -- (3) c s s
2190 * 0 y y n (4) c s s 2177 * 0 y y n (4) c s s
2191 * 0 y y y (5) c s s 2178 * 0 y y y (5) c s s
2192 * 1 -- n -- (6) c c c s 2179 * 1 -- n -- (6) c c c s
2193 * 1 -- y -- (7) c c s c s 2180 * 1 -- y -- (7) c c s c s
2194 * 2181 *
2195 * (c=clear s=set) 2182 * (c=clear s=set)
2196 */ 2183 */
2197 switch (type & 0xff) { 2184 switch (type & 0xff) {
2198 case ND_NEIGHBOR_SOLICIT: 2185 case ND_NEIGHBOR_SOLICIT:
2199 /* 2186 /*
2200 * New entry must have is_router flag cleared. 2187 * New entry must have is_router flag cleared.
2201 */ 2188 */
2202 if (is_newentry) /* (6-7) */ 2189 if (is_newentry) /* (6-7) */
2203 ln->ln_router = 0; 2190 ln->ln_router = 0;
2204 break; 2191 break;
2205 case ND_REDIRECT: 2192 case ND_REDIRECT:
2206 /* 2193 /*
2207 * If the icmp is a redirect to a better router, always set the 2194 * If the icmp is a redirect to a better router, always set the
2208 * is_router flag. Otherwise, if the entry is newly created, 2195 * is_router flag. Otherwise, if the entry is newly created,
2209 * clear the flag. [RFC 2461, sec 8.3] 2196 * clear the flag. [RFC 2461, sec 8.3]
2210 */ 2197 */
2211 if (code == ND_REDIRECT_ROUTER) 2198 if (code == ND_REDIRECT_ROUTER)
2212 ln->ln_router = 1; 2199 ln->ln_router = 1;
2213 else if (is_newentry) /* (6-7) */ 2200 else if (is_newentry) /* (6-7) */
2214 ln->ln_router = 0; 2201 ln->ln_router = 0;
2215 break; 2202 break;
2216 case ND_ROUTER_SOLICIT: 2203 case ND_ROUTER_SOLICIT:
2217 /* 2204 /*
2218 * is_router flag must always be cleared. 2205 * is_router flag must always be cleared.
2219 */ 2206 */
2220 ln->ln_router = 0; 2207 ln->ln_router = 0;
2221 break; 2208 break;
2222 case ND_ROUTER_ADVERT: 2209 case ND_ROUTER_ADVERT:
2223 /* 2210 /*
2224 * Mark an entry with lladdr as a router. 2211 * Mark an entry with lladdr as a router.
2225 */ 2212 */
2226 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */ 2213 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
2227 (is_newentry && lladdr)) { /* (7) */ 2214 (is_newentry && lladdr)) { /* (7) */
2228 ln->ln_router = 1; 2215 ln->ln_router = 1;
2229 } 2216 }
2230 break; 2217 break;
2231 } 2218 }
2232 2219
2233 if (do_update) { 2220 if (do_update) {
2234 struct sockaddr_in6 sin6; 2221 struct sockaddr_in6 sin6;
2235 2222
2236 sockaddr_in6_init(&sin6, from, 0, 0, 0); 2223 sockaddr_in6_init(&sin6, from, 0, 0, 0);
2237 rt_clonedmsg(is_newentry ? RTM_ADD : RTM_CHANGE, 2224 rt_clonedmsg(is_newentry ? RTM_ADD : RTM_CHANGE,
2238 sin6tosa(&sin6), lladdr, ifp); 2225 sin6tosa(&sin6), lladdr, ifp);
2239 } 2226 }
2240 2227
2241 if (ln != NULL) { 2228 if (ln != NULL) {
2242 router = ln->ln_router; 2229 router = ln->ln_router;
2243 LLE_WUNLOCK(ln); 2230 LLE_WUNLOCK(ln);
2244 } 2231 }
2245 2232
2246 /* 2233 /*
2247 * If we have too many cache entries, initiate immediate 2234 * If we have too many cache entries, initiate immediate
2248 * purging for some entries. 2235 * purging for some entries.
2249 */ 2236 */
2250 if (is_newentry) 2237 if (is_newentry)
2251 nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6); 2238 nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6);
2252 2239
2253 /* 2240 /*
2254 * When the link-layer address of a router changes, select the 2241 * When the link-layer address of a router changes, select the
2255 * best router again. In particular, when the neighbor entry is newly 2242 * best router again. In particular, when the neighbor entry is newly
2256 * created, it might affect the selection policy. 2243 * created, it might affect the selection policy.
2257 * Question: can we restrict the first condition to the "is_newentry" 2244 * Question: can we restrict the first condition to the "is_newentry"
2258 * case? 2245 * case?
2259 * XXX: when we hear an RA from a new router with the link-layer 2246 * XXX: when we hear an RA from a new router with the link-layer
2260 * address option, nd6_defrouter_select() is called twice, since 2247 * address option, nd6_defrouter_select() is called twice, since
2261 * defrtrlist_update called the function as well. However, I believe 2248 * defrtrlist_update called the function as well. However, I believe
2262 * we can compromise the overhead, since it only happens the first 2249 * we can compromise the overhead, since it only happens the first
2263 * time. 2250 * time.
2264 * XXX: although nd6_defrouter_select() should not have a bad effect 2251 * XXX: although nd6_defrouter_select() should not have a bad effect
2265 * for those are not autoconfigured hosts, we explicitly avoid such 2252 * for those are not autoconfigured hosts, we explicitly avoid such
2266 * cases for safety. 2253 * cases for safety.
2267 */ 2254 */
2268 if (do_update && router && !ip6_forwarding && 2255 if (do_update && router && !ip6_forwarding &&
2269 nd6_accepts_rtadv(ndi)) { 2256 nd6_accepts_rtadv(ndi)) {
2270 ND6_WLOCK(); 2257 ND6_WLOCK();
2271 nd6_defrouter_select(); 2258 nd6_defrouter_select();
2272 ND6_UNLOCK(); 2259 ND6_UNLOCK();
2273 } 2260 }
2274} 2261}
2275 2262
2276static void 2263static void
2277nd6_slowtimo(void *ignored_arg) 2264nd6_slowtimo(void *ignored_arg)
2278{ 2265{
2279 struct nd_ifinfo *nd6if; 2266 struct nd_ifinfo *nd6if;
2280 struct ifnet *ifp; 2267 struct ifnet *ifp;
2281 int s; 2268 int s;
2282 2269
2283 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2270 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
2284 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 2271 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2285 nd6_slowtimo, NULL); 2272 nd6_slowtimo, NULL);
2286 2273
2287 s = pserialize_read_enter(); 2274 s = pserialize_read_enter();
2288 IFNET_READER_FOREACH(ifp) { 2275 IFNET_READER_FOREACH(ifp) {
2289 nd6if = ND_IFINFO(ifp); 2276 nd6if = ND_IFINFO(ifp);
2290 if (nd6if->basereachable && /* already initialized */ 2277 if (nd6if->basereachable && /* already initialized */
2291 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 2278 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2292 /* 2279 /*
2293 * Since reachable time rarely changes by router 2280 * Since reachable time rarely changes by router