nd.c revision 1.1 1 1.1 roy /* $NetBSD: */
2 1.1 roy
3 1.1 roy /*
4 1.1 roy * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 roy *
6 1.1 roy * This code is derived from software contributed to The NetBSD Foundation
7 1.1 roy * by Roy Marples.
8 1.1 roy *
9 1.1 roy * Redistribution and use in source and binary forms, with or without
10 1.1 roy * modification, are permitted provided that the following conditions
11 1.1 roy * are met:
12 1.1 roy * 1. Redistributions of source code must retain the above copyright
13 1.1 roy * notice, this list of conditions and the following disclaimer.
14 1.1 roy * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 roy * notice, this list of conditions and the following disclaimer in the
16 1.1 roy * documentation and/or other materials provided with the distribution.
17 1.1 roy *
18 1.1 roy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 roy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 roy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 roy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 roy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 roy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 roy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 roy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 roy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 roy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 roy */
29 1.1 roy
30 1.1 roy #include <sys/cdefs.h>
31 1.1 roy __KERNEL_RCSID(0, "$NetBSD: nd.c,v 1.1 2020/09/11 14:59:22 roy Exp $");
32 1.1 roy
33 1.1 roy #include <sys/callout.h>
34 1.1 roy #include <sys/mbuf.h>
35 1.1 roy #include <sys/socketvar.h> /* for softnet_lock */
36 1.1 roy
37 1.1 roy #include <net/if_llatbl.h>
38 1.1 roy #include <net/nd.h>
39 1.1 roy #include <net/route.h>
40 1.1 roy
41 1.1 roy #include <netinet/in.h>
42 1.1 roy #include <netinet/ip6.h>
43 1.1 roy
44 1.1 roy static struct nd_domain *nd_domains[AF_MAX];
45 1.1 roy
46 1.1 roy static int nd_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
47 1.1 roy
48 1.1 roy static void nd_set_timertick(struct llentry *, time_t);
49 1.1 roy static struct nd_domain *nd_find_domain(int);
50 1.1 roy
51 1.1 roy static void
52 1.1 roy nd_timer(void *arg)
53 1.1 roy {
54 1.1 roy struct llentry *ln = arg;
55 1.1 roy struct nd_domain *nd;
56 1.1 roy struct ifnet *ifp = NULL;
57 1.1 roy struct psref psref;
58 1.1 roy struct mbuf *m = NULL;
59 1.1 roy bool send_ns = false, missed = false;
60 1.1 roy union nd_addr taddr, *daddrp = NULL;
61 1.1 roy
62 1.1 roy SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
63 1.1 roy LLE_WLOCK(ln);
64 1.1 roy
65 1.1 roy if (!(ln->la_flags & LLE_LINKED))
66 1.1 roy goto out;
67 1.1 roy if (ln->ln_ntick > 0) {
68 1.1 roy nd_set_timer(ln, ND_TIMER_TICK);
69 1.1 roy goto out;
70 1.1 roy }
71 1.1 roy
72 1.1 roy nd = nd_find_domain(ln->lle_tbl->llt_af);
73 1.1 roy ifp = ln->lle_tbl->llt_ifp;
74 1.1 roy KASSERT(ifp != NULL);
75 1.1 roy if_acquire(ifp, &psref);
76 1.1 roy
77 1.1 roy memcpy(&taddr, &ln->r_l3addr, sizeof(taddr));
78 1.1 roy
79 1.1 roy switch (ln->ln_state) {
80 1.1 roy case ND_LLINFO_WAITDELETE:
81 1.1 roy LLE_REMREF(ln);
82 1.1 roy nd->nd_free(ln, 0);
83 1.1 roy ln = NULL;
84 1.1 roy break;
85 1.1 roy
86 1.1 roy case ND_LLINFO_INCOMPLETE:
87 1.1 roy if (ln->ln_asked++ < nd->nd_mmaxtries) {
88 1.1 roy send_ns = true;
89 1.1 roy break;
90 1.1 roy }
91 1.1 roy
92 1.1 roy if (ln->ln_hold) {
93 1.1 roy struct mbuf *m0, *mnxt;
94 1.1 roy
95 1.1 roy /*
96 1.1 roy * Assuming every packet in ln_hold
97 1.1 roy * has the same IP header.
98 1.1 roy */
99 1.1 roy m = ln->ln_hold;
100 1.1 roy for (m0 = m->m_nextpkt; m0 != NULL; m0 = mnxt) {
101 1.1 roy mnxt = m0->m_nextpkt;
102 1.1 roy m0->m_nextpkt = NULL;
103 1.1 roy m_freem(m0);
104 1.1 roy }
105 1.1 roy
106 1.1 roy m->m_nextpkt = NULL;
107 1.1 roy ln->ln_hold = NULL;
108 1.1 roy }
109 1.1 roy
110 1.1 roy missed = true;
111 1.1 roy ln->ln_state = ND_LLINFO_WAITDELETE;
112 1.1 roy if (ln->ln_asked == nd->nd_mmaxtries)
113 1.1 roy nd_set_timer(ln, ND_TIMER_RETRANS);
114 1.1 roy else
115 1.1 roy send_ns = true;
116 1.1 roy break;
117 1.1 roy
118 1.1 roy case ND_LLINFO_REACHABLE:
119 1.1 roy if (!ND_IS_LLINFO_PERMANENT(ln)) {
120 1.1 roy ln->ln_state = ND_LLINFO_STALE;
121 1.1 roy nd_set_timer(ln, ND_TIMER_GC);
122 1.1 roy }
123 1.1 roy break;
124 1.1 roy
125 1.1 roy case ND_LLINFO_PURGE: /* FALLTHROUGH */
126 1.1 roy case ND_LLINFO_STALE:
127 1.1 roy if (!ND_IS_LLINFO_PERMANENT(ln)) {
128 1.1 roy LLE_REMREF(ln);
129 1.1 roy nd->nd_free(ln, 1);
130 1.1 roy ln = NULL;
131 1.1 roy }
132 1.1 roy break;
133 1.1 roy
134 1.1 roy case ND_LLINFO_DELAY:
135 1.1 roy if (nd->nd_nud_enabled(ifp)) {
136 1.1 roy ln->ln_asked = 1;
137 1.1 roy ln->ln_state = ND_LLINFO_PROBE;
138 1.1 roy send_ns = true;
139 1.1 roy daddrp = &taddr;
140 1.1 roy } else {
141 1.1 roy ln->ln_state = ND_LLINFO_STALE;
142 1.1 roy nd_set_timer(ln, ND_TIMER_GC);
143 1.1 roy }
144 1.1 roy break;
145 1.1 roy
146 1.1 roy case ND_LLINFO_PROBE:
147 1.1 roy if (ln->ln_asked < nd->nd_umaxtries) {
148 1.1 roy ln->ln_asked++;
149 1.1 roy send_ns = true;
150 1.1 roy daddrp = &taddr;
151 1.1 roy } else {
152 1.1 roy LLE_REMREF(ln);
153 1.1 roy nd->nd_free(ln, 0);
154 1.1 roy ln = NULL;
155 1.1 roy }
156 1.1 roy break;
157 1.1 roy }
158 1.1 roy
159 1.1 roy if (send_ns) {
160 1.1 roy uint8_t lladdr[255], *lladdrp;
161 1.1 roy union nd_addr src, *psrc;
162 1.1 roy
163 1.1 roy nd_set_timer(ln, ND_TIMER_RETRANS);
164 1.1 roy if (ln->ln_state > ND_LLINFO_INCOMPLETE &&
165 1.1 roy ln->la_flags & LLE_VALID)
166 1.1 roy {
167 1.1 roy KASSERT(sizeof(lladdr) >= ifp->if_addrlen);
168 1.1 roy memcpy(lladdr, &ln->ll_addr, ifp->if_addrlen);
169 1.1 roy lladdrp = lladdr;
170 1.1 roy } else
171 1.1 roy lladdrp = NULL;
172 1.1 roy psrc = nd->nd_holdsrc(ln, &src);
173 1.1 roy LLE_FREE_LOCKED(ln);
174 1.1 roy ln = NULL;
175 1.1 roy nd->nd_output(ifp, daddrp, &taddr, lladdrp, psrc);
176 1.1 roy }
177 1.1 roy
178 1.1 roy out:
179 1.1 roy if (ln != NULL)
180 1.1 roy LLE_FREE_LOCKED(ln);
181 1.1 roy SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
182 1.1 roy
183 1.1 roy if (missed)
184 1.1 roy nd->nd_missed(ifp, &taddr, m);
185 1.1 roy if (ifp != NULL)
186 1.1 roy if_release(ifp, &psref);
187 1.1 roy }
188 1.1 roy
189 1.1 roy static void
190 1.1 roy nd_set_timertick(struct llentry *ln, time_t xtick)
191 1.1 roy {
192 1.1 roy
193 1.1 roy CTASSERT(sizeof(time_t) > sizeof(int));
194 1.1 roy KASSERT(xtick >= 0);
195 1.1 roy
196 1.1 roy /*
197 1.1 roy * We have to take care of a reference leak which occurs if
198 1.1 roy * callout_reset overwrites a pending callout schedule. Unfortunately
199 1.1 roy * we don't have a mean to know the overwrite, so we need to know it
200 1.1 roy * using callout_stop. We need to call callout_pending first to exclude
201 1.1 roy * the case that the callout has never been scheduled.
202 1.1 roy */
203 1.1 roy if (callout_pending(&ln->la_timer)) {
204 1.1 roy bool expired;
205 1.1 roy
206 1.1 roy expired = callout_stop(&ln->la_timer);
207 1.1 roy if (!expired)
208 1.1 roy LLE_REMREF(ln);
209 1.1 roy }
210 1.1 roy
211 1.1 roy ln->ln_expire = time_uptime + xtick / hz;
212 1.1 roy LLE_ADDREF(ln);
213 1.1 roy if (xtick > INT_MAX) {
214 1.1 roy ln->ln_ntick = xtick - INT_MAX;
215 1.1 roy xtick = INT_MAX;
216 1.1 roy } else {
217 1.1 roy ln->ln_ntick = 0;
218 1.1 roy }
219 1.1 roy callout_reset(&ln->ln_timer_ch, xtick, nd_timer, ln);
220 1.1 roy }
221 1.1 roy
222 1.1 roy void
223 1.1 roy nd_set_timer(struct llentry *ln, int type)
224 1.1 roy {
225 1.1 roy time_t xtick;
226 1.1 roy struct ifnet *ifp;
227 1.1 roy struct nd_domain *nd;
228 1.1 roy
229 1.1 roy LLE_WLOCK_ASSERT(ln);
230 1.1 roy
231 1.1 roy ifp = ln->lle_tbl->llt_ifp;
232 1.1 roy nd = nd_find_domain(ln->lle_tbl->llt_af);
233 1.1 roy
234 1.1 roy switch (type) {
235 1.1 roy case ND_TIMER_IMMEDIATE:
236 1.1 roy xtick = 0;
237 1.1 roy break;
238 1.1 roy case ND_TIMER_TICK:
239 1.1 roy xtick = ln->ln_ntick;
240 1.1 roy break;
241 1.1 roy case ND_TIMER_RETRANS:
242 1.1 roy xtick = nd->nd_retrans(ifp) * hz / 1000;
243 1.1 roy break;
244 1.1 roy case ND_TIMER_REACHABLE:
245 1.1 roy xtick = nd->nd_reachable(ifp) * hz / 1000;
246 1.1 roy break;
247 1.1 roy case ND_TIMER_EXPIRE:
248 1.1 roy if (ln->ln_expire > time_uptime)
249 1.1 roy xtick = (ln->ln_expire - time_uptime) * hz;
250 1.1 roy else
251 1.1 roy xtick = nd_gctimer * hz;
252 1.1 roy break;
253 1.1 roy case ND_TIMER_DELAY:
254 1.1 roy xtick = nd->nd_delay * hz;
255 1.1 roy break;
256 1.1 roy case ND_TIMER_GC:
257 1.1 roy xtick = nd_gctimer * hz;
258 1.1 roy break;
259 1.1 roy default:
260 1.1 roy panic("%s: invalid timer type\n", __func__);
261 1.1 roy }
262 1.1 roy
263 1.1 roy nd_set_timertick(ln, xtick);
264 1.1 roy }
265 1.1 roy
266 1.1 roy int
267 1.1 roy nd_resolve(struct llentry *ln, const struct rtentry *rt, struct mbuf *m,
268 1.1 roy uint8_t *lldst, size_t dstsize)
269 1.1 roy {
270 1.1 roy struct ifnet *ifp;
271 1.1 roy struct nd_domain *nd;
272 1.1 roy int error;
273 1.1 roy
274 1.1 roy LLE_WLOCK_ASSERT(ln);
275 1.1 roy
276 1.1 roy ifp = ln->lle_tbl->llt_ifp;
277 1.1 roy nd = nd_find_domain(ln->lle_tbl->llt_af);
278 1.1 roy
279 1.1 roy /* We don't have to do link-layer address resolution on a p2p link. */
280 1.1 roy if (ifp->if_flags & IFF_POINTOPOINT &&
281 1.1 roy ln->ln_state < ND_LLINFO_REACHABLE)
282 1.1 roy {
283 1.1 roy ln->ln_state = ND_LLINFO_STALE;
284 1.1 roy nd_set_timer(ln, ND_TIMER_GC);
285 1.1 roy }
286 1.1 roy
287 1.1 roy /*
288 1.1 roy * The first time we send a packet to a neighbor whose entry is
289 1.1 roy * STALE, we have to change the state to DELAY and a sets a timer to
290 1.1 roy * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
291 1.1 roy * neighbor unreachability detection on expiration.
292 1.1 roy * (RFC 2461 7.3.3)
293 1.1 roy */
294 1.1 roy if (ln->ln_state == ND_LLINFO_STALE) {
295 1.1 roy ln->ln_asked = 0;
296 1.1 roy ln->ln_state = ND_LLINFO_DELAY;
297 1.1 roy nd_set_timer(ln, ND_TIMER_DELAY);
298 1.1 roy }
299 1.1 roy
300 1.1 roy /*
301 1.1 roy * If the neighbor cache entry has a state other than INCOMPLETE
302 1.1 roy * (i.e. its link-layer address is already resolved), just
303 1.1 roy * send the packet.
304 1.1 roy */
305 1.1 roy if (ln->ln_state > ND_LLINFO_INCOMPLETE) {
306 1.1 roy KASSERT((ln->la_flags & LLE_VALID) != 0);
307 1.1 roy memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
308 1.1 roy LLE_WUNLOCK(ln);
309 1.1 roy return 0;
310 1.1 roy }
311 1.1 roy
312 1.1 roy /*
313 1.1 roy * There is a neighbor cache entry, but no ethernet address
314 1.1 roy * response yet. Append this latest packet to the end of the
315 1.1 roy * packet queue in the mbuf, unless the number of the packet
316 1.1 roy * does not exceed maxqueuelen. When it exceeds maxqueuelen,
317 1.1 roy * the oldest packet in the queue will be removed.
318 1.1 roy */
319 1.1 roy if (ln->ln_state == ND_LLINFO_NOSTATE ||
320 1.1 roy ln->ln_state == ND_LLINFO_WAITDELETE)
321 1.1 roy ln->ln_state = ND_LLINFO_INCOMPLETE;
322 1.1 roy
323 1.1 roy if (ln->ln_hold != NULL) {
324 1.1 roy struct mbuf *m_hold;
325 1.1 roy int i;
326 1.1 roy
327 1.1 roy i = 0;
328 1.1 roy for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
329 1.1 roy i++;
330 1.1 roy if (m_hold->m_nextpkt == NULL) {
331 1.1 roy m_hold->m_nextpkt = m;
332 1.1 roy break;
333 1.1 roy }
334 1.1 roy }
335 1.1 roy while (i >= nd->nd_maxqueuelen) {
336 1.1 roy m_hold = ln->ln_hold;
337 1.1 roy ln->ln_hold = ln->ln_hold->m_nextpkt;
338 1.1 roy m_freem(m_hold);
339 1.1 roy i--;
340 1.1 roy }
341 1.1 roy } else
342 1.1 roy ln->ln_hold = m;
343 1.1 roy
344 1.1 roy if (ln->ln_asked >= nd->nd_mmaxtries)
345 1.1 roy error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
346 1.1 roy EHOSTUNREACH : EHOSTDOWN;
347 1.1 roy else
348 1.1 roy error = EWOULDBLOCK;
349 1.1 roy
350 1.1 roy /*
351 1.1 roy * If there has been no NS for the neighbor after entering the
352 1.1 roy * INCOMPLETE state, send the first solicitation.
353 1.1 roy */
354 1.1 roy if (!ND_IS_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
355 1.1 roy struct psref psref;
356 1.1 roy union nd_addr dst, src, *psrc;
357 1.1 roy
358 1.1 roy ln->ln_asked++;
359 1.1 roy nd_set_timer(ln, ND_TIMER_RETRANS);
360 1.1 roy memcpy(&dst, &ln->r_l3addr, sizeof(dst));
361 1.1 roy psrc = nd->nd_holdsrc(ln, &src);
362 1.1 roy if_acquire(ifp, &psref);
363 1.1 roy LLE_WUNLOCK(ln);
364 1.1 roy
365 1.1 roy nd->nd_output(ifp, NULL, &dst, NULL, psrc);
366 1.1 roy if_release(ifp, &psref);
367 1.1 roy } else
368 1.1 roy LLE_WUNLOCK(ln);
369 1.1 roy
370 1.1 roy return error;
371 1.1 roy }
372 1.1 roy
373 1.1 roy void
374 1.1 roy nd_nud_hint(struct llentry *ln)
375 1.1 roy {
376 1.1 roy struct nd_domain *nd;
377 1.1 roy
378 1.1 roy if (ln == NULL)
379 1.1 roy return;
380 1.1 roy
381 1.1 roy LLE_WLOCK_ASSERT(ln);
382 1.1 roy
383 1.1 roy if (ln->ln_state < ND_LLINFO_REACHABLE)
384 1.1 roy goto done;
385 1.1 roy
386 1.1 roy nd = nd_find_domain(ln->lle_tbl->llt_af);
387 1.1 roy
388 1.1 roy /*
389 1.1 roy * if we get upper-layer reachability confirmation many times,
390 1.1 roy * it is possible we have false information.
391 1.1 roy */
392 1.1 roy ln->ln_byhint++;
393 1.1 roy if (ln->ln_byhint > nd->nd_maxnudhint)
394 1.1 roy goto done;
395 1.1 roy
396 1.1 roy ln->ln_state = ND_LLINFO_REACHABLE;
397 1.1 roy if (!ND_IS_LLINFO_PERMANENT(ln))
398 1.1 roy nd_set_timer(ln, ND_TIMER_REACHABLE);
399 1.1 roy
400 1.1 roy done:
401 1.1 roy LLE_WUNLOCK(ln);
402 1.1 roy
403 1.1 roy return;
404 1.1 roy }
405 1.1 roy
406 1.1 roy static struct nd_domain *
407 1.1 roy nd_find_domain(int af)
408 1.1 roy {
409 1.1 roy
410 1.1 roy KASSERT(af < __arraycount(nd_domains) && nd_domains[af] != NULL);
411 1.1 roy return nd_domains[af];
412 1.1 roy }
413 1.1 roy
414 1.1 roy void
415 1.1 roy nd_attach_domain(struct nd_domain *nd)
416 1.1 roy {
417 1.1 roy
418 1.1 roy KASSERT(nd->nd_family < __arraycount(nd_domains));
419 1.1 roy nd_domains[nd->nd_family] = nd;
420 1.1 roy }
421