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