nd6_nbr.c revision 1.179 1 /* $NetBSD: nd6_nbr.c,v 1.179 2020/06/12 11:04:45 roy Exp $ */
2 /* $KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6_nbr.c,v 1.179 2020/06/12 11:04:45 roy Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_inet.h"
38 #include "opt_net_mpsafe.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kmem.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sockio.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/errno.h>
51 #include <sys/ioctl.h>
52 #include <sys/syslog.h>
53 #include <sys/queue.h>
54 #include <sys/callout.h>
55 #include <sys/cprng.h>
56
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet6/in6_ifattach.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet6/scope6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/icmp6_private.h>
72
73 #include "carp.h"
74 #if NCARP > 0
75 #include <netinet/ip_carp.h>
76 #endif
77
78 struct dadq;
79 static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *, bool *);
80 static bool nd6_dad_ownnonce(struct ifaddr *, struct nd_opt_nonce *nonce);
81 static void nd6_dad_starttimer(struct dadq *, int);
82 static void nd6_dad_destroytimer(struct dadq *);
83 static void nd6_dad_timer(struct dadq *);
84 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
85 static void nd6_dad_input(struct ifaddr *, struct nd_opt_nonce *,
86 const struct sockaddr_dl *);
87 static void nd6_dad_duplicated(struct ifaddr *, struct dadq *,
88 const struct sockaddr_dl *);
89
90 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
91
92 /*
93 * Input a Neighbor Solicitation Message.
94 *
95 * Based on RFC 2461
96 * Based on RFC 2462 (duplicate address detection)
97 */
98 void
99 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
100 {
101 struct ifnet *ifp;
102 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
103 struct nd_neighbor_solicit *nd_ns;
104 struct in6_addr saddr6 = ip6->ip6_src;
105 struct in6_addr daddr6 = ip6->ip6_dst;
106 struct in6_addr taddr6;
107 struct in6_addr myaddr6;
108 char *lladdr = NULL;
109 struct ifaddr *ifa = NULL;
110 int lladdrlen = 0;
111 int anycast = 0, proxy = 0, tentative = 0;
112 int router = ip6_forwarding;
113 int tlladdr;
114 union nd_opts ndopts;
115 const struct sockaddr_dl *proxydl = NULL;
116 struct psref psref;
117 struct psref psref_ia;
118 char ip6buf[INET6_ADDRSTRLEN], ip6buf2[INET6_ADDRSTRLEN];
119
120 ifp = m_get_rcvif_psref(m, &psref);
121 if (ifp == NULL)
122 goto freeit;
123
124 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
125 if (nd_ns == NULL) {
126 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
127 m_put_rcvif_psref(ifp, &psref);
128 return;
129 }
130 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
131 taddr6 = nd_ns->nd_ns_target;
132 if (in6_setscope(&taddr6, ifp, NULL) != 0)
133 goto bad;
134
135 if (ip6->ip6_hlim != 255) {
136 nd6log(LOG_ERR, "invalid hlim (%d) from %s to %s on %s\n",
137 ip6->ip6_hlim, IN6_PRINT(ip6buf, &ip6->ip6_src),
138 IN6_PRINT(ip6buf2, &ip6->ip6_dst), if_name(ifp));
139 goto bad;
140 }
141
142 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
143 /* dst has to be a solicited node multicast address. */
144 /* don't check ifindex portion */
145 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
146 daddr6.s6_addr32[1] == 0 &&
147 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
148 daddr6.s6_addr8[12] == 0xff) {
149 ; /* good */
150 } else {
151 nd6log(LOG_INFO, "bad DAD packet (wrong ip6 dst)\n");
152 goto bad;
153 }
154 } else {
155 struct sockaddr_in6 ssin6;
156
157 /*
158 * Make sure the source address is from a neighbor's address.
159 */
160 sockaddr_in6_init(&ssin6, &saddr6, 0, 0, 0);
161 if (nd6_is_addr_neighbor(&ssin6, ifp) == 0) {
162 nd6log(LOG_INFO,
163 "NS packet from non-neighbor %s on %s\n",
164 IN6_PRINT(ip6buf, &saddr6), if_name(ifp));
165 goto bad;
166 }
167 }
168
169 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
170 nd6log(LOG_INFO, "bad NS target (multicast)\n");
171 goto bad;
172 }
173
174 icmp6len -= sizeof(*nd_ns);
175 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
176 if (nd6_options(&ndopts) < 0) {
177 nd6log(LOG_INFO, "invalid ND option, ignored\n");
178 /* nd6_options have incremented stats */
179 goto freeit;
180 }
181
182 if (ndopts.nd_opts_src_lladdr) {
183 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
184 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
185 }
186
187 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
188 nd6log(LOG_INFO,
189 "bad DAD packet (link-layer address option)\n");
190 goto bad;
191 }
192
193 /*
194 * Attaching target link-layer address to the NA?
195 * (RFC 2461 7.2.4)
196 *
197 * NS IP dst is multicast MUST add
198 * Otherwise MAY be omitted
199 *
200 * In this implementation, we omit the target link-layer address
201 * in the "MAY" case.
202 */
203 #if 0 /* too much! */
204 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
205 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
206 tlladdr = 0;
207 else
208 #endif
209 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
210 tlladdr = 0;
211 else
212 tlladdr = 1;
213
214 /*
215 * Target address (taddr6) must be either:
216 * (1) Valid unicast/anycast address for my receiving interface,
217 * (2) Unicast address for which I'm offering proxy service, or
218 * (3) "tentative" address on which DAD is being performed.
219 */
220 /* (1) and (3) check. */
221 #if NCARP > 0
222 if (ifp->if_carp && ifp->if_type != IFT_CARP) {
223 int s = pserialize_read_enter();
224 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
225 if (ifa != NULL)
226 ifa_acquire(ifa, &psref_ia);
227 pserialize_read_exit(s);
228 } else
229 ifa = NULL;
230 if (!ifa)
231 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6,
232 &psref_ia);
233 #else
234 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6,
235 &psref_ia);
236 #endif
237
238 /* (2) check. */
239 if (ifa == NULL) {
240 struct rtentry *rt;
241 struct sockaddr_in6 tsin6;
242
243 sockaddr_in6_init(&tsin6, &taddr6, 0, 0, 0);
244
245 rt = rtalloc1(sin6tosa(&tsin6), 0);
246 if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
247 rt->rt_gateway->sa_family == AF_LINK) {
248 /*
249 * proxy NDP for single entry
250 */
251 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal_psref(ifp,
252 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST, &psref_ia);
253 if (ifa) {
254 proxy = 1;
255 proxydl = satocsdl(rt->rt_gateway);
256 router = 0; /* XXX */
257 }
258 }
259 if (rt)
260 rt_unref(rt);
261 }
262 if (ifa == NULL) {
263 /*
264 * We've got an NS packet, and we don't have that address
265 * assigned for us. We MUST silently ignore it.
266 * See RFC2461 7.2.3.
267 */
268 goto freeit;
269 }
270 myaddr6 = *IFA_IN6(ifa);
271 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
272 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
273 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
274 goto freeit;
275
276 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
277 nd6log(LOG_INFO, "lladdrlen mismatch for %s "
278 "(if %d, NS packet %d)\n",
279 IN6_PRINT(ip6buf, &taddr6),
280 ifp->if_addrlen, lladdrlen - 2);
281 goto bad;
282 }
283
284 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
285 nd6log(LOG_INFO, "duplicate IP6 address %s\n",
286 IN6_PRINT(ip6buf, &saddr6));
287 goto freeit;
288 }
289
290 /*
291 * We have neighbor solicitation packet, with target address equals to
292 * one of my tentative address.
293 *
294 * src addr how to process?
295 * --- ---
296 * multicast of course, invalid (rejected in ip6_input)
297 * unicast somebody is doing address resolution -> ignore
298 * unspec dup address detection
299 *
300 * The processing is defined in RFC 2462.
301 */
302 if (tentative) {
303 /*
304 * If source address is unspecified address, it is for
305 * duplicate address detection.
306 *
307 * If not, the packet is for addess resolution;
308 * silently ignore it.
309 */
310 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
311 struct sockaddr_dl sdl, *sdlp;
312
313 if (lladdr != NULL)
314 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl),
315 ifp->if_index, ifp->if_type,
316 NULL, 0, lladdr, lladdrlen);
317 else
318 sdlp = NULL;
319 nd6_dad_input(ifa, ndopts.nd_opts_nonce, sdlp);
320 }
321 goto freeit;
322 }
323
324 /*
325 * It looks that sender is performing DAD.
326 * Check that the nonce is not being used by the same address
327 * on another interface.
328 */
329 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6) && ndopts.nd_opts_nonce != NULL) {
330 if (nd6_dad_ownnonce(ifa, ndopts.nd_opts_nonce))
331 goto freeit;
332 }
333
334 ifa_release(ifa, &psref_ia);
335 ifa = NULL;
336
337 /*
338 * If the source address is unspecified address, entries must not
339 * be created or updated.
340 * It looks that sender is performing DAD. Output NA toward
341 * all-node multicast address, to tell the sender that I'm using
342 * the address.
343 * S bit ("solicited") must be zero.
344 */
345 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
346 struct in6_addr in6_all;
347
348 in6_all = in6addr_linklocal_allnodes;
349 if (in6_setscope(&in6_all, ifp, NULL) != 0)
350 goto bad;
351 nd6_na_output(ifp, &in6_all, &taddr6,
352 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
353 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
354 tlladdr, (const struct sockaddr *)proxydl);
355 goto freeit;
356 }
357
358 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
359
360 nd6_na_output(ifp, &saddr6, &taddr6,
361 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
362 (router ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
363 tlladdr, (const struct sockaddr *)proxydl);
364 freeit:
365 ifa_release(ifa, &psref_ia);
366 m_put_rcvif_psref(ifp, &psref);
367 m_freem(m);
368 return;
369
370 bad:
371 nd6log(LOG_ERR, "src=%s\n", IN6_PRINT(ip6buf, &saddr6));
372 nd6log(LOG_ERR, "dst=%s\n", IN6_PRINT(ip6buf, &daddr6));
373 nd6log(LOG_ERR, "tgt=%s\n", IN6_PRINT(ip6buf, &taddr6));
374 ICMP6_STATINC(ICMP6_STAT_BADNS);
375 ifa_release(ifa, &psref_ia);
376 m_put_rcvif_psref(ifp, &psref);
377 m_freem(m);
378 }
379
380 /*
381 * Output a Neighbor Solicitation Message. Caller specifies:
382 * - ICMP6 header source IP6 address
383 * - ND6 header target IP6 address
384 * - ND6 header source datalink address
385 *
386 * Based on RFC 2461
387 * Based on RFC 2462 (duplicate address detection)
388 */
389 void
390 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
391 const struct in6_addr *taddr6,
392 struct in6_addr *hsrc,
393 uint8_t *nonce /* duplicate address detection */)
394 {
395 struct mbuf *m;
396 struct ip6_hdr *ip6;
397 struct nd_neighbor_solicit *nd_ns;
398 struct in6_addr *src, src_in;
399 struct ip6_moptions im6o;
400 int icmp6len;
401 int maxlen;
402 const void *mac;
403 struct route ro;
404
405 if (IN6_IS_ADDR_MULTICAST(taddr6))
406 return;
407
408 memset(&ro, 0, sizeof(ro));
409
410 /* estimate the size of message */
411 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
412 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
413 KASSERTMSG(max_linkhdr + maxlen <= MCLBYTES,
414 "max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
415 max_linkhdr, maxlen, MCLBYTES);
416
417 MGETHDR(m, M_DONTWAIT, MT_DATA);
418 if (m && max_linkhdr + maxlen >= MHLEN) {
419 MCLGET(m, M_DONTWAIT);
420 if ((m->m_flags & M_EXT) == 0) {
421 m_free(m);
422 m = NULL;
423 }
424 }
425 if (m == NULL)
426 return;
427 m_reset_rcvif(m);
428
429 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
430 m->m_flags |= M_MCAST;
431 im6o.im6o_multicast_if_index = if_get_index(ifp);
432 im6o.im6o_multicast_hlim = 255;
433 im6o.im6o_multicast_loop = 0;
434 }
435
436 icmp6len = sizeof(*nd_ns);
437 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
438 m->m_data += max_linkhdr; /* or m_align() equivalent? */
439
440 /* fill neighbor solicitation packet */
441 ip6 = mtod(m, struct ip6_hdr *);
442 ip6->ip6_flow = 0;
443 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
444 ip6->ip6_vfc |= IPV6_VERSION;
445 /* ip6->ip6_plen will be set later */
446 ip6->ip6_nxt = IPPROTO_ICMPV6;
447 ip6->ip6_hlim = 255;
448 if (daddr6)
449 ip6->ip6_dst = *daddr6;
450 else {
451 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
452 ip6->ip6_dst.s6_addr16[1] = 0;
453 ip6->ip6_dst.s6_addr32[1] = 0;
454 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
455 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
456 ip6->ip6_dst.s6_addr8[12] = 0xff;
457 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
458 goto bad;
459 }
460 if (nonce == NULL) {
461 int s;
462 /*
463 * RFC2461 7.2.2:
464 * "If the source address of the packet prompting the
465 * solicitation is the same as one of the addresses assigned
466 * to the outgoing interface, that address SHOULD be placed
467 * in the IP Source Address of the outgoing solicitation.
468 * Otherwise, any one of the addresses assigned to the
469 * interface should be used."
470 *
471 * We use the source address for the prompting packet
472 * (hsrc), if:
473 * - hsrc is given from the caller (by giving "ln"), and
474 * - hsrc belongs to the outgoing interface.
475 * Otherwise, we perform the source address selection as usual.
476 */
477 s = pserialize_read_enter();
478 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc)) {
479 pserialize_read_exit(s);
480 src = hsrc;
481 } else {
482 int error;
483 struct sockaddr_in6 dst_sa;
484
485 pserialize_read_exit(s);
486
487 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
488
489 error = in6_selectsrc(&dst_sa, NULL,
490 NULL, &ro, NULL, NULL, NULL, &src_in);
491 if (error != 0) {
492 char ip6buf[INET6_ADDRSTRLEN];
493 nd6log(LOG_DEBUG, "source can't be "
494 "determined: dst=%s, error=%d\n",
495 IN6_PRINT(ip6buf, &dst_sa.sin6_addr),
496 error);
497 goto bad;
498 }
499 src = &src_in;
500 }
501 } else {
502 /*
503 * Source address for DAD packet must always be IPv6
504 * unspecified address. (0::0)
505 * We actually don't have to 0-clear the address (we did it
506 * above), but we do so here explicitly to make the intention
507 * clearer.
508 */
509 memset(&src_in, 0, sizeof(src_in));
510 src = &src_in;
511 }
512 ip6->ip6_src = *src;
513 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
514 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
515 nd_ns->nd_ns_code = 0;
516 nd_ns->nd_ns_reserved = 0;
517 nd_ns->nd_ns_target = *taddr6;
518 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
519
520 /*
521 * Add source link-layer address option.
522 *
523 * spec implementation
524 * --- ---
525 * DAD packet MUST NOT do not add the option
526 * there's no link layer address:
527 * impossible do not add the option
528 * there's link layer address:
529 * Multicast NS MUST add one add the option
530 * Unicast NS SHOULD add one add the option
531 */
532 if (nonce == NULL && (mac = nd6_ifptomac(ifp))) {
533 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
534 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
535 /* 8 byte alignments... */
536 optlen = (optlen + 7) & ~7;
537
538 m->m_pkthdr.len += optlen;
539 m->m_len += optlen;
540 icmp6len += optlen;
541 memset((void *)nd_opt, 0, optlen);
542 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
543 nd_opt->nd_opt_len = optlen >> 3;
544 memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
545 }
546
547 /* Add a nonce option (RFC 3971) to detect looped back NS messages.
548 * This behavior is documented in RFC 7527. */
549 if (nonce != NULL) {
550 int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
551 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
552
553 /* 8-byte alignment is required. */
554 optlen = (optlen + 7) & ~7;
555 m->m_pkthdr.len += optlen;
556 m->m_len += optlen;
557 icmp6len += optlen;
558 memset(nd_opt, 0, optlen);
559 nd_opt->nd_opt_type = ND_OPT_NONCE;
560 nd_opt->nd_opt_len = optlen >> 3;
561 memcpy(nd_opt + 1, nonce, ND_OPT_NONCE_LEN);
562 }
563
564 ip6->ip6_plen = htons((u_int16_t)icmp6len);
565 nd_ns->nd_ns_cksum = 0;
566 nd_ns->nd_ns_cksum =
567 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
568
569 ip6_output(m, NULL, &ro, nonce != NULL ? IPV6_UNSPECSRC : 0,
570 &im6o, NULL, NULL);
571 icmp6_ifstat_inc(ifp, ifs6_out_msg);
572 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
573 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_SOLICIT);
574
575 rtcache_free(&ro);
576 return;
577
578 bad:
579 rtcache_free(&ro);
580 m_freem(m);
581 return;
582 }
583
584 /*
585 * Neighbor advertisement input handling.
586 *
587 * Based on RFC 2461
588 * Based on RFC 2462 (duplicate address detection)
589 *
590 * the following items are not implemented yet:
591 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
592 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
593 */
594 void
595 nd6_na_input(struct mbuf *m, int off, int icmp6len)
596 {
597 struct ifnet *ifp;
598 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
599 struct nd_neighbor_advert *nd_na;
600 struct in6_addr saddr6 = ip6->ip6_src;
601 struct in6_addr daddr6 = ip6->ip6_dst;
602 struct in6_addr taddr6;
603 int flags;
604 int is_router;
605 int is_solicited;
606 int is_override;
607 int rt_cmd;
608 char *lladdr = NULL;
609 int lladdrlen = 0;
610 struct ifaddr *ifa;
611 struct llentry *ln = NULL;
612 union nd_opts ndopts;
613 struct sockaddr_in6 ssin6;
614 struct psref psref;
615 struct psref psref_ia;
616 char ip6buf[INET6_ADDRSTRLEN], ip6buf2[INET6_ADDRSTRLEN];
617
618 ifp = m_get_rcvif_psref(m, &psref);
619 if (ifp == NULL)
620 goto freeit;
621
622 if (ip6->ip6_hlim != 255) {
623 nd6log(LOG_ERR,
624 "invalid hlim (%d) from %s to %s on %s\n",
625 ip6->ip6_hlim, IN6_PRINT(ip6buf, &ip6->ip6_src),
626 IN6_PRINT(ip6buf2, &ip6->ip6_dst), if_name(ifp));
627 goto bad;
628 }
629
630 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
631 if (nd_na == NULL) {
632 m_put_rcvif_psref(ifp, &psref);
633 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
634 return;
635 }
636
637 flags = nd_na->nd_na_flags_reserved;
638 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
639 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
640 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
641
642 taddr6 = nd_na->nd_na_target;
643 if (in6_setscope(&taddr6, ifp, NULL)) {
644 goto bad;
645 }
646
647 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
648 nd6log(LOG_ERR, "invalid target address %s\n",
649 IN6_PRINT(ip6buf, &taddr6));
650 goto bad;
651 }
652 if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
653 nd6log(LOG_ERR, "a solicited adv is multicasted\n");
654 goto bad;
655 }
656
657 icmp6len -= sizeof(*nd_na);
658 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
659 if (nd6_options(&ndopts) < 0) {
660 nd6log(LOG_INFO, "invalid ND option, ignored\n");
661 /* nd6_options have incremented stats */
662 goto freeit;
663 }
664
665 if (ndopts.nd_opts_tgt_lladdr != NULL) {
666 struct ifnet *ifp_ll;
667 struct psref psref_ll;
668
669 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
670 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
671
672 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
673 nd6log(LOG_INFO, "lladdrlen mismatch for %s "
674 "(if %d, NA packet %d)\n", IN6_PRINT(ip6buf, &taddr6),
675 ifp->if_addrlen, lladdrlen - 2);
676 goto bad;
677 }
678
679 ifp_ll = if_get_bylla(lladdr, ifp->if_addrlen, &psref_ll);
680 if (ifp_ll != NULL) {
681 /* it's from me, ignore it. */
682 if_put(ifp_ll, &psref_ll);
683 goto freeit;
684 }
685 }
686
687 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6, &psref_ia);
688
689 /*
690 * Target address matches one of my interface address.
691 *
692 * If my address is tentative, this means that there's somebody
693 * already using the same address as mine. This indicates DAD failure.
694 * This is defined in RFC 2462.
695 *
696 * Otherwise, process as defined in RFC 2461.
697 */
698 if (ifa) {
699 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE) {
700 struct sockaddr_dl sdl, *sdlp;
701
702 if (lladdr != NULL)
703 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl),
704 ifp->if_index, ifp->if_type,
705 NULL, 0, lladdr, lladdrlen);
706 else
707 sdlp = NULL;
708 nd6_dad_input(ifa, NULL, sdlp);
709 } else
710 log(LOG_ERR,
711 "nd6_na_input: duplicate IP6 address %s\n",
712 IN6_PRINT(ip6buf, &taddr6));
713 ifa_release(ifa, &psref_ia);
714 ifa = NULL;
715 goto freeit;
716 }
717
718 /*
719 * Make sure the source address is from a neighbor's address.
720 */
721 sockaddr_in6_init(&ssin6, &saddr6, 0, 0, 0);
722 if (nd6_is_addr_neighbor(&ssin6, ifp) == 0) {
723 nd6log(LOG_INFO, "ND packet from non-neighbor %s on %s\n",
724 IN6_PRINT(ip6buf, &saddr6), if_name(ifp));
725 goto bad;
726 }
727
728 /*
729 * If no neighbor cache entry is found, NA SHOULD silently be
730 * discarded.
731 */
732 ln = nd6_lookup(&taddr6, ifp, true);
733 if (ln == NULL)
734 goto freeit;
735
736 rt_cmd = 0;
737 if (ln->ln_state <= ND6_LLINFO_INCOMPLETE) {
738 /*
739 * If the link-layer has address, and no lladdr option came,
740 * discard the packet.
741 */
742 if (ifp->if_addrlen && !lladdr)
743 goto freeit;
744
745 /*
746 * Record link-layer address, and update the state.
747 */
748 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
749 ln->la_flags |= LLE_VALID;
750 rt_cmd = RTM_ADD;
751 if (is_solicited) {
752 ln->ln_state = ND6_LLINFO_REACHABLE;
753 ln->ln_byhint = 0;
754 if (!ND6_LLINFO_PERMANENT(ln)) {
755 nd6_llinfo_settimer(ln,
756 ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
757 }
758 } else {
759 ln->ln_state = ND6_LLINFO_STALE;
760 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
761 }
762 } else {
763 bool llchange;
764
765 /*
766 * Check if the link-layer address has changed or not.
767 */
768 if (lladdr == NULL)
769 llchange = false;
770 else {
771 if (ln->la_flags & LLE_VALID) {
772 if (memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
773 llchange = true;
774 else
775 llchange = false;
776 } else
777 llchange = true;
778 }
779 if (llchange)
780 rt_cmd = RTM_CHANGE;
781
782 /*
783 * This is VERY complex. Look at it with care.
784 *
785 * override solicit lladdr llchange action
786 * (L: record lladdr)
787 *
788 * 0 0 n -- (2c)
789 * 0 0 y n (2b) L
790 * 0 0 y y (1) REACHABLE->STALE
791 * 0 1 n -- (2c) *->REACHABLE
792 * 0 1 y n (2b) L *->REACHABLE
793 * 0 1 y y (1) REACHABLE->STALE
794 * 1 0 n -- (2a)
795 * 1 0 y n (2a) L
796 * 1 0 y y (2a) L *->STALE
797 * 1 1 n -- (2a) *->REACHABLE
798 * 1 1 y n (2a) L *->REACHABLE
799 * 1 1 y y (2a) L *->REACHABLE
800 */
801 if (!is_override && lladdr != NULL && llchange) { /* (1) */
802 /*
803 * If state is REACHABLE, make it STALE.
804 * no other updates should be done.
805 */
806 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
807 ln->ln_state = ND6_LLINFO_STALE;
808 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
809 }
810 goto freeit;
811 } else if (is_override /* (2a) */
812 || (!is_override && lladdr != NULL && !llchange) /* (2b) */
813 || lladdr == NULL) { /* (2c) */
814 /*
815 * Update link-local address, if any.
816 */
817 if (lladdr != NULL) {
818 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
819 ln->la_flags |= LLE_VALID;
820 }
821
822 /*
823 * If solicited, make the state REACHABLE.
824 * If not solicited and the link-layer address was
825 * changed, make it STALE.
826 */
827 if (is_solicited) {
828 ln->ln_state = ND6_LLINFO_REACHABLE;
829 ln->ln_byhint = 0;
830 if (!ND6_LLINFO_PERMANENT(ln)) {
831 nd6_llinfo_settimer(ln,
832 ND_IFINFO(ifp)->reachable * hz);
833 }
834 } else {
835 if (lladdr && llchange) {
836 ln->ln_state = ND6_LLINFO_STALE;
837 nd6_llinfo_settimer(ln,
838 nd6_gctimer * hz);
839 }
840 }
841 }
842 ln->ln_router = is_router;
843 }
844 /*
845 * XXX: does this matter?
846 * rt->rt_flags &= ~RTF_REJECT;
847 */
848 ln->ln_asked = 0;
849 nd6_llinfo_release_pkts(ln, ifp);
850
851 if (rt_cmd != 0) {
852 struct sockaddr_in6 sin6;
853
854 sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
855 rt_clonedmsg(rt_cmd, sin6tosa(&ssin6), sin6tosa(&sin6),
856 (char *)&ln->ll_addr, ln->lle_tbl->llt_ifp);
857 }
858
859 freeit:
860 if (ln != NULL)
861 LLE_WUNLOCK(ln);
862
863 m_put_rcvif_psref(ifp, &psref);
864 m_freem(m);
865 return;
866
867 bad:
868 if (ln != NULL)
869 LLE_WUNLOCK(ln);
870
871 ICMP6_STATINC(ICMP6_STAT_BADNA);
872 m_put_rcvif_psref(ifp, &psref);
873 m_freem(m);
874 }
875
876 /*
877 * Neighbor advertisement output handling.
878 *
879 * Based on RFC 2461
880 *
881 * the following items are not implemented yet:
882 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
883 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
884 */
885 void
886 nd6_na_output(
887 struct ifnet *ifp,
888 const struct in6_addr *daddr6_0,
889 const struct in6_addr *taddr6,
890 u_long flags,
891 int tlladdr, /* 1 if include target link-layer address */
892 const struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
893 {
894 struct mbuf *m;
895 struct ip6_hdr *ip6;
896 struct nd_neighbor_advert *nd_na;
897 struct ip6_moptions im6o;
898 struct sockaddr *dst;
899 union {
900 struct sockaddr dst;
901 struct sockaddr_in6 dst6;
902 } u;
903 struct in6_addr daddr6;
904 int icmp6len, maxlen, error;
905 const void *mac;
906 struct route ro;
907
908 mac = NULL;
909 memset(&ro, 0, sizeof(ro));
910
911 daddr6 = *daddr6_0; /* make a local copy for modification */
912
913 /* estimate the size of message */
914 maxlen = sizeof(*ip6) + sizeof(*nd_na);
915 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
916 KASSERTMSG(max_linkhdr + maxlen <= MCLBYTES,
917 "max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
918 max_linkhdr, maxlen, MCLBYTES);
919
920 MGETHDR(m, M_DONTWAIT, MT_DATA);
921 if (m && max_linkhdr + maxlen >= MHLEN) {
922 MCLGET(m, M_DONTWAIT);
923 if ((m->m_flags & M_EXT) == 0) {
924 m_free(m);
925 m = NULL;
926 }
927 }
928 if (m == NULL)
929 return;
930 m_reset_rcvif(m);
931
932 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
933 m->m_flags |= M_MCAST;
934 im6o.im6o_multicast_if_index = if_get_index(ifp);
935 im6o.im6o_multicast_hlim = 255;
936 im6o.im6o_multicast_loop = 0;
937 }
938
939 icmp6len = sizeof(*nd_na);
940 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
941 m->m_data += max_linkhdr; /* or m_align() equivalent? */
942
943 /* fill neighbor advertisement packet */
944 ip6 = mtod(m, struct ip6_hdr *);
945 ip6->ip6_flow = 0;
946 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
947 ip6->ip6_vfc |= IPV6_VERSION;
948 ip6->ip6_nxt = IPPROTO_ICMPV6;
949 ip6->ip6_hlim = 255;
950 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
951 /* reply to DAD */
952 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
953 daddr6.s6_addr16[1] = 0;
954 daddr6.s6_addr32[1] = 0;
955 daddr6.s6_addr32[2] = 0;
956 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
957 if (in6_setscope(&daddr6, ifp, NULL))
958 goto bad;
959
960 flags &= ~ND_NA_FLAG_SOLICITED;
961 }
962 ip6->ip6_dst = daddr6;
963 sockaddr_in6_init(&u.dst6, &daddr6, 0, 0, 0);
964 dst = &u.dst;
965 if (rtcache_setdst(&ro, dst) != 0)
966 goto bad;
967
968 /*
969 * Select a source whose scope is the same as that of the dest.
970 */
971 error = in6_selectsrc(satosin6(dst), NULL, NULL, &ro, NULL, NULL, NULL,
972 &ip6->ip6_src);
973 if (error != 0) {
974 char ip6buf[INET6_ADDRSTRLEN];
975 nd6log(LOG_DEBUG, "source can't be "
976 "determined: dst=%s, error=%d\n",
977 IN6_PRINT(ip6buf, &satocsin6(dst)->sin6_addr), error);
978 goto bad;
979 }
980 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
981 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
982 nd_na->nd_na_code = 0;
983 nd_na->nd_na_target = *taddr6;
984 in6_clearscope(&nd_na->nd_na_target); /* XXX */
985
986 /*
987 * "tlladdr" indicates NS's condition for adding tlladdr or not.
988 * see nd6_ns_input() for details.
989 * Basically, if NS packet is sent to unicast/anycast addr,
990 * target lladdr option SHOULD NOT be included.
991 */
992 if (tlladdr) {
993 /*
994 * sdl0 != NULL indicates proxy NA. If we do proxy, use
995 * lladdr in sdl0. If we are not proxying (sending NA for
996 * my address) use lladdr configured for the interface.
997 */
998 if (sdl0 == NULL)
999 mac = nd6_ifptomac(ifp);
1000 else if (sdl0->sa_family == AF_LINK) {
1001 const struct sockaddr_dl *sdl;
1002 sdl = satocsdl(sdl0);
1003 if (sdl->sdl_alen == ifp->if_addrlen)
1004 mac = CLLADDR(sdl);
1005 }
1006 }
1007 if (tlladdr && mac) {
1008 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1009 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1010
1011 /* roundup to 8 bytes alignment! */
1012 optlen = (optlen + 7) & ~7;
1013
1014 m->m_pkthdr.len += optlen;
1015 m->m_len += optlen;
1016 icmp6len += optlen;
1017 memset((void *)nd_opt, 0, optlen);
1018 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1019 nd_opt->nd_opt_len = optlen >> 3;
1020 memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
1021 } else
1022 flags &= ~ND_NA_FLAG_OVERRIDE;
1023
1024 ip6->ip6_plen = htons((u_int16_t)icmp6len);
1025 nd_na->nd_na_flags_reserved = flags;
1026 nd_na->nd_na_cksum = 0;
1027 nd_na->nd_na_cksum =
1028 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1029
1030 ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
1031
1032 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1033 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1034 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_ADVERT);
1035
1036 rtcache_free(&ro);
1037 return;
1038
1039 bad:
1040 rtcache_free(&ro);
1041 m_freem(m);
1042 return;
1043 }
1044
1045 const void *
1046 nd6_ifptomac(const struct ifnet *ifp)
1047 {
1048 switch (ifp->if_type) {
1049 case IFT_ARCNET:
1050 case IFT_ETHER:
1051 case IFT_IEEE1394:
1052 case IFT_PROPVIRTUAL:
1053 case IFT_CARP:
1054 case IFT_L2VLAN:
1055 case IFT_IEEE80211:
1056 return CLLADDR(ifp->if_sadl);
1057 default:
1058 return NULL;
1059 }
1060 }
1061
1062 TAILQ_HEAD(dadq_head, dadq);
1063 struct dadq {
1064 TAILQ_ENTRY(dadq) dad_list;
1065 struct ifaddr *dad_ifa;
1066 int dad_count; /* max NS to send */
1067 int dad_ns_tcount; /* # of trials to send NS */
1068 int dad_ns_ocount; /* NS sent so far */
1069 int dad_ns_lcount; /* looped back NS */
1070 struct callout dad_timer_ch;
1071 #define ND_OPT_NONCE_STORE 3 /* dad_count should not exceed this */
1072 /*
1073 * The default ip6_dad_count is 1 as specified by RFC 4862 and
1074 * practically must users won't exceed this.
1075 * A storage of 3 is defaulted to here, in-case the administrator wants
1076 * to match the equivalent behaviour in our ARP implementation.
1077 * This constraint could be removed by sending the on wire nonce as
1078 * hmac(key, dad_ns_ocount), but that would increase the nonce size
1079 * sent on the wire.
1080 */
1081 uint8_t dad_nonce[ND_OPT_NONCE_STORE][ND_OPT_NONCE_LEN];
1082 };
1083
1084 static struct dadq_head dadq;
1085 static kmutex_t nd6_dad_lock;
1086
1087 void
1088 nd6_nbr_init(void)
1089 {
1090
1091 TAILQ_INIT(&dadq);
1092 mutex_init(&nd6_dad_lock, MUTEX_DEFAULT, IPL_NONE);
1093 }
1094
1095 static struct dadq *
1096 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *nonce, bool *found_nonce)
1097 {
1098 struct in6_addr *myaddr6, *dadaddr6;
1099 bool match_ifa;
1100 struct dadq *dp;
1101 int i, nonce_max;
1102
1103 KASSERT(mutex_owned(&nd6_dad_lock));
1104 KASSERT(ifa != NULL);
1105
1106 myaddr6 = IFA_IN6(ifa);
1107 if (nonce != NULL &&
1108 nonce->nd_opt_nonce_len != (ND_OPT_NONCE_LEN + 2) / 8)
1109 nonce = NULL;
1110 match_ifa = nonce == NULL || found_nonce == NULL || *found_nonce == false;
1111 if (found_nonce != NULL)
1112 *found_nonce = false;
1113
1114 TAILQ_FOREACH(dp, &dadq, dad_list) {
1115 if (match_ifa) {
1116 if (dp->dad_ifa != ifa)
1117 continue;
1118 } else {
1119 dadaddr6 = IFA_IN6(dp->dad_ifa);
1120 if (!IN6_ARE_ADDR_EQUAL(myaddr6, dadaddr6))
1121 continue;
1122 }
1123
1124 if (nonce == NULL)
1125 break;
1126
1127 nonce_max = MIN(dp->dad_ns_ocount, ND_OPT_NONCE_STORE);
1128 for (i = 0; i < nonce_max; i++) {
1129 if (memcmp(nonce->nd_opt_nonce,
1130 dp->dad_nonce[i],
1131 ND_OPT_NONCE_LEN) == 0)
1132 break;
1133 }
1134 if (i < nonce_max) {
1135 char ip6buf[INET6_ADDRSTRLEN];
1136
1137 *found_nonce = true;
1138 log(LOG_DEBUG,
1139 "%s: detected a looped back NS message for %s\n",
1140 if_name(ifa->ifa_ifp), IN6_PRINT(ip6buf, myaddr6));
1141 dp->dad_ns_lcount++;
1142 continue;
1143 }
1144
1145 break;
1146 }
1147 return dp;
1148 }
1149
1150 static bool
1151 nd6_dad_ownnonce(struct ifaddr *ifa, struct nd_opt_nonce *nonce)
1152 {
1153 bool found_nonce = true;
1154
1155 mutex_enter(&nd6_dad_lock);
1156 nd6_dad_find(ifa, nonce, &found_nonce);
1157 mutex_exit(&nd6_dad_lock);
1158
1159 return found_nonce;
1160 }
1161
1162 static void
1163 nd6_dad_starttimer(struct dadq *dp, int ticks)
1164 {
1165
1166 callout_reset(&dp->dad_timer_ch, ticks,
1167 (void (*)(void *))nd6_dad_timer, dp);
1168 }
1169
1170 static void
1171 nd6_dad_stoptimer(struct dadq *dp)
1172 {
1173
1174 KASSERT(mutex_owned(&nd6_dad_lock));
1175
1176 TAILQ_REMOVE(&dadq, dp, dad_list);
1177 /* Tell the timer that dp is being destroyed. */
1178 dp->dad_ifa = NULL;
1179 callout_halt(&dp->dad_timer_ch, &nd6_dad_lock);
1180 }
1181
1182 static void
1183 nd6_dad_destroytimer(struct dadq *dp)
1184 {
1185
1186 KASSERT(dp->dad_ifa == NULL);
1187 callout_destroy(&dp->dad_timer_ch);
1188 kmem_intr_free(dp, sizeof(*dp));
1189 }
1190
1191 /*
1192 * Start Duplicate Address Detection (DAD) for specified interface address.
1193 *
1194 * Note that callout is used when xtick > 0 and not when xtick == 0.
1195 *
1196 * xtick: minimum delay ticks for IFF_UP event
1197 */
1198 void
1199 nd6_dad_start(struct ifaddr *ifa, int xtick)
1200 {
1201 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1202 struct dadq *dp;
1203 char ip6buf[INET6_ADDRSTRLEN];
1204
1205 /*
1206 * If we don't need DAD, don't do it.
1207 * There are several cases:
1208 * - DAD is disabled
1209 * - the interface address is anycast
1210 */
1211 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1212 log(LOG_DEBUG,
1213 "nd6_dad_start: called with non-tentative address "
1214 "%s(%s)\n",
1215 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
1216 if_name(ifa->ifa_ifp));
1217 return;
1218 }
1219 if (ia->ia6_flags & IN6_IFF_ANYCAST || !ip6_dad_enabled()) {
1220 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1221 rt_addrmsg(RTM_NEWADDR, ifa);
1222 return;
1223 }
1224 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1225 return;
1226
1227 dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP);
1228
1229 mutex_enter(&nd6_dad_lock);
1230 if (nd6_dad_find(ifa, NULL, NULL) != NULL) {
1231 mutex_exit(&nd6_dad_lock);
1232 /* DAD already in progress */
1233 if (dp != NULL)
1234 kmem_intr_free(dp, sizeof(*dp));
1235 return;
1236 }
1237
1238 if (dp == NULL) {
1239 mutex_exit(&nd6_dad_lock);
1240 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1241 "%s(%s)\n",
1242 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
1243 if_name(ifa->ifa_ifp));
1244 return;
1245 }
1246
1247 /*
1248 * Send NS packet for DAD, ip6_dad_count times.
1249 * Note that we must delay the first transmission, if this is the
1250 * first packet to be sent from the interface after interface
1251 * (re)initialization.
1252 */
1253 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1254 dp->dad_ifa = ifa;
1255 ifaref(ifa); /* just for safety */
1256 dp->dad_count = ip6_dad_count;
1257 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1258 dp->dad_ns_lcount = 0;
1259 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1260
1261 nd6log(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1262 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr));
1263
1264 if (xtick == 0) {
1265 nd6_dad_ns_output(dp, ifa);
1266 nd6_dad_starttimer(dp,
1267 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1268 } else
1269 nd6_dad_starttimer(dp, xtick);
1270 mutex_exit(&nd6_dad_lock);
1271 }
1272
1273 /*
1274 * terminate DAD unconditionally. used for address removals.
1275 */
1276 void
1277 nd6_dad_stop(struct ifaddr *ifa)
1278 {
1279 struct dadq *dp;
1280
1281 mutex_enter(&nd6_dad_lock);
1282 dp = nd6_dad_find(ifa, NULL, NULL);
1283 if (dp == NULL) {
1284 mutex_exit(&nd6_dad_lock);
1285 /* DAD wasn't started yet */
1286 return;
1287 }
1288
1289 /* Prevent the timer from running anymore. */
1290 nd6_dad_stoptimer(dp);
1291
1292 mutex_exit(&nd6_dad_lock);
1293
1294 nd6_dad_destroytimer(dp);
1295 ifafree(ifa);
1296 }
1297
1298 static void
1299 nd6_dad_timer(struct dadq *dp)
1300 {
1301 struct ifaddr *ifa;
1302 struct in6_ifaddr *ia;
1303 char ip6buf[INET6_ADDRSTRLEN];
1304 bool need_free = false;
1305
1306 KERNEL_LOCK_UNLESS_NET_MPSAFE();
1307 mutex_enter(&nd6_dad_lock);
1308
1309 ifa = dp->dad_ifa;
1310 if (ifa == NULL) {
1311 /* dp is being destroyed by someone. Do nothing. */
1312 goto done;
1313 }
1314
1315 ia = (struct in6_ifaddr *)ifa;
1316 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1317 log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
1318 "%s(%s)\n",
1319 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
1320 if_name(ifa->ifa_ifp));
1321 goto done;
1322 }
1323 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1324 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1325 "%s(%s)\n",
1326 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
1327 if_name(ifa->ifa_ifp));
1328 goto done;
1329 }
1330
1331 /* timeouted with IFF_{RUNNING,UP} check */
1332 if (dp->dad_ns_tcount > dad_maxtry) {
1333 nd6log(LOG_INFO, "%s: could not run DAD, driver problem?\n",
1334 if_name(ifa->ifa_ifp));
1335
1336 nd6_dad_stoptimer(dp);
1337 need_free = true;
1338 goto done;
1339 }
1340
1341 /* Need more checks? */
1342 if (dp->dad_ns_ocount < dp->dad_count) {
1343 /*
1344 * We have more NS to go. Send NS packet for DAD.
1345 */
1346 nd6_dad_ns_output(dp, ifa);
1347 nd6_dad_starttimer(dp,
1348 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1349 } else {
1350 /*
1351 * We are done with DAD. No NA came, no NS came.
1352 * No duplicate address found.
1353 */
1354 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1355 rt_addrmsg(RTM_NEWADDR, ifa);
1356
1357 nd6log(LOG_DEBUG,
1358 "%s: DAD complete for %s - no duplicates found\n",
1359 if_name(ifa->ifa_ifp),
1360 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr));
1361
1362 nd6_dad_stoptimer(dp);
1363 need_free = true;
1364 }
1365 done:
1366 mutex_exit(&nd6_dad_lock);
1367
1368 if (need_free) {
1369 nd6_dad_destroytimer(dp);
1370 KASSERT(ifa != NULL);
1371 ifafree(ifa);
1372 }
1373
1374 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1375 }
1376
1377 static void
1378 nd6_dad_duplicated(struct ifaddr *ifa, struct dadq *dp,
1379 const struct sockaddr_dl *from)
1380 {
1381 struct in6_ifaddr *ia;
1382 struct ifnet *ifp;
1383 char ip6buf[INET6_ADDRSTRLEN], llabuf[LLA_ADDRSTRLEN], *llastr;
1384
1385 KASSERT(mutex_owned(&nd6_dad_lock));
1386 KASSERT(ifa != NULL);
1387
1388 ifp = ifa->ifa_ifp;
1389 ia = (struct in6_ifaddr *)ifa;
1390
1391 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1392 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1393
1394 if (__predict_false(from == NULL))
1395 llastr = NULL;
1396 else
1397 llastr = lla_snprintf(llabuf, sizeof(llabuf),
1398 CLLADDR(from), from->sdl_alen);
1399
1400 log(LOG_ERR, "%s: DAD duplicate address %s from %s\n",
1401 if_name(ifp), IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr), llastr);
1402
1403 /* Inform the routing socket that DAD has completed */
1404 rt_addrmsg_src(RTM_NEWADDR, ifa, (const struct sockaddr *)from);
1405
1406 /*
1407 * If the address is a link-local address formed from an interface
1408 * identifier based on the hardware address which is supposed to be
1409 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1410 * operation on the interface SHOULD be disabled.
1411 * [rfc2462bis-03 Section 5.4.5]
1412 */
1413 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1414 struct in6_addr in6;
1415
1416 /*
1417 * To avoid over-reaction, we only apply this logic when we are
1418 * very sure that hardware addresses are supposed to be unique.
1419 */
1420 switch (ifp->if_type) {
1421 case IFT_ETHER:
1422 case IFT_ATM:
1423 case IFT_IEEE1394:
1424 case IFT_IEEE80211:
1425 in6 = ia->ia_addr.sin6_addr;
1426 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1427 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1428 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1429 log(LOG_ERR, "%s: possible hardware address "
1430 "duplication detected, disable IPv6\n",
1431 if_name(ifp));
1432 }
1433 break;
1434 }
1435 }
1436 }
1437
1438 static void
1439 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1440 {
1441 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1442 struct ifnet *ifp = ifa->ifa_ifp;
1443 uint8_t *nonce;
1444
1445 dp->dad_ns_tcount++;
1446 if ((ifp->if_flags & IFF_UP) == 0) {
1447 #if 0
1448 printf("%s: interface down?\n", if_name(ifp));
1449 #endif
1450 return;
1451 }
1452 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1453 #if 0
1454 printf("%s: interface not running?\n", if_name(ifp));
1455 #endif
1456 return;
1457 }
1458
1459 dp->dad_ns_tcount = 0;
1460 nonce = dp->dad_nonce[dp->dad_ns_ocount % ND_OPT_NONCE_STORE];
1461 cprng_fast(nonce, ND_OPT_NONCE_LEN);
1462 dp->dad_ns_ocount++;
1463
1464 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, nonce);
1465 }
1466
1467 static void
1468 nd6_dad_input(struct ifaddr *ifa, struct nd_opt_nonce *nonce,
1469 const struct sockaddr_dl *from)
1470 {
1471 struct dadq *dp;
1472 bool found_nonce = false;
1473
1474 KASSERT(ifa != NULL);
1475
1476 mutex_enter(&nd6_dad_lock);
1477 dp = nd6_dad_find(ifa, nonce, &found_nonce);
1478 if (!found_nonce) {
1479 nd6_dad_duplicated(ifa, dp, from);
1480 if (dp != NULL)
1481 nd6_dad_stoptimer(dp);
1482 }
1483 mutex_exit(&nd6_dad_lock);
1484 if (dp != NULL) {
1485 nd6_dad_destroytimer(dp);
1486 ifafree(ifa);
1487 }
1488 }
1489