nd6_nbr.c revision 1.127 1 /* $NetBSD: nd6_nbr.c,v 1.127 2016/08/01 03:15:31 ozaki-r 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.127 2016/08/01 03:15:31 ozaki-r Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_inet.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sockio.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/ioctl.h>
51 #include <sys/syslog.h>
52 #include <sys/queue.h>
53 #include <sys/callout.h>
54
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/in6_ifattach.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/scope6_var.h>
67 #include <netinet6/nd6.h>
68 #include <netinet/icmp6.h>
69 #include <netinet6/icmp6_private.h>
70
71 #include "carp.h"
72 #if NCARP > 0
73 #include <netinet/ip_carp.h>
74 #endif
75
76 #include <net/net_osdep.h>
77
78 struct dadq;
79 static struct dadq *nd6_dad_find(struct ifaddr *);
80 static void nd6_dad_starttimer(struct dadq *, int);
81 static void nd6_dad_stoptimer(struct dadq *);
82 static void nd6_dad_timer(struct ifaddr *);
83 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
84 static void nd6_dad_ns_input(struct ifaddr *);
85 static void nd6_dad_na_input(struct ifaddr *);
86
87 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
88 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
89
90 /*
91 * Input a Neighbor Solicitation Message.
92 *
93 * Based on RFC 2461
94 * Based on RFC 2462 (duplicate address detection)
95 */
96 void
97 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
98 {
99 struct ifnet *ifp;
100 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
101 struct nd_neighbor_solicit *nd_ns;
102 struct in6_addr saddr6 = ip6->ip6_src;
103 struct in6_addr daddr6 = ip6->ip6_dst;
104 struct in6_addr taddr6;
105 struct in6_addr myaddr6;
106 char *lladdr = NULL;
107 struct ifaddr *ifa = NULL;
108 int lladdrlen = 0;
109 int anycast = 0, proxy = 0, tentative = 0;
110 int router = ip6_forwarding;
111 int tlladdr;
112 union nd_opts ndopts;
113 const struct sockaddr_dl *proxydl = NULL;
114 struct psref psref;
115 struct psref psref_ia;
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, ip6_sprintf(&ip6->ip6_src),
135 ip6_sprintf(&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 ip6_sprintf(&saddr6), if_name(ifp));
162 goto bad;
163 }
164 }
165
166
167 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
168 nd6log(LOG_INFO, "bad NS target (multicast)\n");
169 goto bad;
170 }
171
172 icmp6len -= sizeof(*nd_ns);
173 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
174 if (nd6_options(&ndopts) < 0) {
175 nd6log(LOG_INFO, "invalid ND option, ignored\n");
176 /* nd6_options have incremented stats */
177 goto freeit;
178 }
179
180 if (ndopts.nd_opts_src_lladdr) {
181 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
182 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
183 }
184
185 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
186 nd6log(LOG_INFO,
187 "bad DAD packet (link-layer address option)\n");
188 goto bad;
189 }
190
191 /*
192 * Attaching target link-layer address to the NA?
193 * (RFC 2461 7.2.4)
194 *
195 * NS IP dst is multicast MUST add
196 * Otherwise MAY be omitted
197 *
198 * In this implementation, we omit the target link-layer address
199 * in the "MAY" case.
200 */
201 #if 0 /* too much! */
202 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
203 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
204 tlladdr = 0;
205 else
206 #endif
207 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
208 tlladdr = 0;
209 else
210 tlladdr = 1;
211
212 /*
213 * Target address (taddr6) must be either:
214 * (1) Valid unicast/anycast address for my receiving interface,
215 * (2) Unicast address for which I'm offering proxy service, or
216 * (3) "tentative" address on which DAD is being performed.
217 */
218 /* (1) and (3) check. */
219 #if NCARP > 0
220 if (ifp->if_carp && ifp->if_type != IFT_CARP) {
221 int s = pserialize_read_enter();
222 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
223 if (ifa != NULL)
224 ifa_acquire(ifa, &psref_ia);
225 pserialize_read_exit(s);
226 } else
227 ifa = NULL;
228 if (!ifa)
229 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6,
230 &psref_ia);
231 #else
232 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6,
233 &psref_ia);
234 #endif
235
236 /* (2) check. */
237 if (ifa == NULL) {
238 struct rtentry *rt;
239 struct sockaddr_in6 tsin6;
240
241 sockaddr_in6_init(&tsin6, &taddr6, 0, 0, 0);
242
243 rt = rtalloc1(sin6tosa(&tsin6), 0);
244 if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
245 rt->rt_gateway->sa_family == AF_LINK) {
246 /*
247 * proxy NDP for single entry
248 */
249 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal_psref(ifp,
250 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST, &psref_ia);
251 if (ifa) {
252 proxy = 1;
253 proxydl = satocsdl(rt->rt_gateway);
254 router = 0; /* XXX */
255 }
256 }
257 if (rt)
258 rtfree(rt);
259 }
260 if (ifa == NULL) {
261 /*
262 * We've got an NS packet, and we don't have that address
263 * assigned for us. We MUST silently ignore it.
264 * See RFC2461 7.2.3.
265 */
266 goto freeit;
267 }
268 myaddr6 = *IFA_IN6(ifa);
269 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
270 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
271 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
272 goto freeit;
273
274 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
275 nd6log(LOG_INFO, "lladdrlen mismatch for %s "
276 "(if %d, NS packet %d)\n",
277 ip6_sprintf(&taddr6), 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 ip6_sprintf(&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_ns_input(ifa);
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", ip6_sprintf(&saddr6));
352 nd6log(LOG_ERR, "dst=%s\n", ip6_sprintf(&daddr6));
353 nd6log(LOG_ERR, "tgt=%s\n", ip6_sprintf(&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 int dad /* 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 #ifdef DIAGNOSTIC
394 if (max_linkhdr + maxlen >= MCLBYTES) {
395 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
396 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
397 panic("nd6_ns_output: insufficient MCLBYTES");
398 /* NOTREACHED */
399 }
400 #endif
401
402 MGETHDR(m, M_DONTWAIT, MT_DATA);
403 if (m && max_linkhdr + maxlen >= MHLEN) {
404 MCLGET(m, M_DONTWAIT);
405 if ((m->m_flags & M_EXT) == 0) {
406 m_free(m);
407 m = NULL;
408 }
409 }
410 if (m == NULL)
411 return;
412 m_reset_rcvif(m);
413
414 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
415 m->m_flags |= M_MCAST;
416 im6o.im6o_multicast_if_index = if_get_index(ifp);
417 im6o.im6o_multicast_hlim = 255;
418 im6o.im6o_multicast_loop = 0;
419 }
420
421 icmp6len = sizeof(*nd_ns);
422 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
423 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
424
425 /* fill neighbor solicitation packet */
426 ip6 = mtod(m, struct ip6_hdr *);
427 ip6->ip6_flow = 0;
428 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
429 ip6->ip6_vfc |= IPV6_VERSION;
430 /* ip6->ip6_plen will be set later */
431 ip6->ip6_nxt = IPPROTO_ICMPV6;
432 ip6->ip6_hlim = 255;
433 if (daddr6)
434 ip6->ip6_dst = *daddr6;
435 else {
436 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
437 ip6->ip6_dst.s6_addr16[1] = 0;
438 ip6->ip6_dst.s6_addr32[1] = 0;
439 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
440 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
441 ip6->ip6_dst.s6_addr8[12] = 0xff;
442 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
443 goto bad;
444 }
445 if (!dad) {
446 int s;
447 /*
448 * RFC2461 7.2.2:
449 * "If the source address of the packet prompting the
450 * solicitation is the same as one of the addresses assigned
451 * to the outgoing interface, that address SHOULD be placed
452 * in the IP Source Address of the outgoing solicitation.
453 * Otherwise, any one of the addresses assigned to the
454 * interface should be used."
455 *
456 * We use the source address for the prompting packet
457 * (hsrc), if:
458 * - hsrc is given from the caller (by giving "ln"), and
459 * - hsrc belongs to the outgoing interface.
460 * Otherwise, we perform the source address selection as usual.
461 */
462 s = pserialize_read_enter();
463 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
464 src = hsrc;
465 else {
466 int error;
467 struct sockaddr_in6 dst_sa;
468
469 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
470
471 src = in6_selectsrc(&dst_sa, NULL,
472 NULL, &ro, NULL, NULL, NULL, &error);
473 if (src == NULL) {
474 nd6log(LOG_DEBUG, "source can't be "
475 "determined: dst=%s, error=%d\n",
476 ip6_sprintf(&dst_sa.sin6_addr), error);
477 pserialize_read_exit(s);
478 goto bad;
479 }
480 }
481 pserialize_read_exit(s);
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 (!dad && (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 ip6->ip6_plen = htons((u_int16_t)icmp6len);
529 nd_ns->nd_ns_cksum = 0;
530 nd_ns->nd_ns_cksum =
531 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
532
533 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
534 icmp6_ifstat_inc(ifp, ifs6_out_msg);
535 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
536 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_SOLICIT);
537
538 rtcache_free(&ro);
539 return;
540
541 bad:
542 rtcache_free(&ro);
543 m_freem(m);
544 return;
545 }
546
547 /*
548 * Neighbor advertisement input handling.
549 *
550 * Based on RFC 2461
551 * Based on RFC 2462 (duplicate address detection)
552 *
553 * the following items are not implemented yet:
554 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
555 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
556 */
557 void
558 nd6_na_input(struct mbuf *m, int off, int icmp6len)
559 {
560 struct ifnet *ifp;
561 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
562 struct nd_neighbor_advert *nd_na;
563 struct in6_addr saddr6 = ip6->ip6_src;
564 struct in6_addr daddr6 = ip6->ip6_dst;
565 struct in6_addr taddr6;
566 int flags;
567 int is_router;
568 int is_solicited;
569 int is_override;
570 char *lladdr = NULL;
571 int lladdrlen = 0;
572 struct ifaddr *ifa;
573 struct llentry *ln = NULL;
574 union nd_opts ndopts;
575 struct sockaddr_in6 ssin6;
576 int rt_announce;
577 bool checklink = false;
578 struct psref psref;
579 struct psref psref_ia;
580
581 ifp = m_get_rcvif_psref(m, &psref);
582 if (ifp == NULL)
583 goto freeit;
584
585 if (ip6->ip6_hlim != 255) {
586 nd6log(LOG_ERR,
587 "invalid hlim (%d) from %s to %s on %s\n",
588 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
589 ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
590 goto bad;
591 }
592
593 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
594 if (nd_na == NULL) {
595 m_put_rcvif_psref(ifp, &psref);
596 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
597 return;
598 }
599
600 flags = nd_na->nd_na_flags_reserved;
601 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
602 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
603 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
604
605 taddr6 = nd_na->nd_na_target;
606 if (in6_setscope(&taddr6, ifp, NULL)) {
607 m_put_rcvif_psref(ifp, &psref);
608 return; /* XXX: impossible */
609 }
610
611 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
612 nd6log(LOG_ERR, "invalid target address %s\n",
613 ip6_sprintf(&taddr6));
614 goto bad;
615 }
616 if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
617 nd6log(LOG_ERR, "a solicited adv is multicasted\n");
618 goto bad;
619 }
620
621 icmp6len -= sizeof(*nd_na);
622 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
623 if (nd6_options(&ndopts) < 0) {
624 nd6log(LOG_INFO, "invalid ND option, ignored\n");
625 /* nd6_options have incremented stats */
626 goto freeit;
627 }
628
629 if (ndopts.nd_opts_tgt_lladdr) {
630 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
631 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
632 }
633
634 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6, &psref_ia);
635
636 /*
637 * Target address matches one of my interface address.
638 *
639 * If my address is tentative, this means that there's somebody
640 * already using the same address as mine. This indicates DAD failure.
641 * This is defined in RFC 2462.
642 *
643 * Otherwise, process as defined in RFC 2461.
644 */
645 if (ifa
646 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
647 nd6_dad_na_input(ifa);
648 ifa_release(ifa, &psref_ia);
649 ifa = NULL;
650 goto freeit;
651 }
652
653 /* Just for safety, maybe unnecessary. */
654 if (ifa) {
655 log(LOG_ERR,
656 "nd6_na_input: duplicate IP6 address %s\n",
657 ip6_sprintf(&taddr6));
658 ifa_release(ifa, &psref_ia);
659 ifa = NULL;
660 goto freeit;
661 }
662
663 /*
664 * Make sure the source address is from a neighbor's address.
665 */
666 sockaddr_in6_init(&ssin6, &saddr6, 0, 0, 0);
667 if (nd6_is_addr_neighbor(&ssin6, ifp) == 0) {
668 nd6log(LOG_INFO, "ND packet from non-neighbor %s on %s\n",
669 ip6_sprintf(&saddr6), if_name(ifp));
670 goto bad;
671 }
672
673 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
674 nd6log(LOG_INFO, "lladdrlen mismatch for %s "
675 "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6),
676 ifp->if_addrlen, lladdrlen - 2);
677 goto bad;
678 }
679
680 /*
681 * If no neighbor cache entry is found, NA SHOULD silently be
682 * discarded.
683 */
684 ln = nd6_lookup(&taddr6, ifp, true);
685 if (ln == NULL)
686 goto freeit;
687
688 rt_announce = 0;
689 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
690 /*
691 * If the link-layer has address, and no lladdr option came,
692 * discard the packet.
693 */
694 if (ifp->if_addrlen && !lladdr)
695 goto freeit;
696
697 /*
698 * Record link-layer address, and update the state.
699 */
700 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
701 ln->la_flags |= LLE_VALID;
702 rt_announce = 1;
703 if (is_solicited) {
704 ln->ln_state = ND6_LLINFO_REACHABLE;
705 ln->ln_byhint = 0;
706 if (!ND6_LLINFO_PERMANENT(ln)) {
707 nd6_llinfo_settimer(ln,
708 ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
709 }
710 } else {
711 ln->ln_state = ND6_LLINFO_STALE;
712 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
713 }
714 if ((ln->ln_router = is_router) != 0) {
715 /*
716 * This means a router's state has changed from
717 * non-reachable to probably reachable, and might
718 * affect the status of associated prefixes..
719 */
720 checklink = true;
721 }
722 } else {
723 int llchange;
724
725 /*
726 * Check if the link-layer address has changed or not.
727 */
728 if (lladdr == NULL)
729 llchange = 0;
730 else {
731 if (ln->la_flags & LLE_VALID) {
732 if (memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
733 llchange = rt_announce = 1;
734 else
735 llchange = 0;
736 } else
737 llchange = rt_announce = 1;
738 }
739
740 /*
741 * This is VERY complex. Look at it with care.
742 *
743 * override solicit lladdr llchange action
744 * (L: record lladdr)
745 *
746 * 0 0 n -- (2c)
747 * 0 0 y n (2b) L
748 * 0 0 y y (1) REACHABLE->STALE
749 * 0 1 n -- (2c) *->REACHABLE
750 * 0 1 y n (2b) L *->REACHABLE
751 * 0 1 y y (1) REACHABLE->STALE
752 * 1 0 n -- (2a)
753 * 1 0 y n (2a) L
754 * 1 0 y y (2a) L *->STALE
755 * 1 1 n -- (2a) *->REACHABLE
756 * 1 1 y n (2a) L *->REACHABLE
757 * 1 1 y y (2a) L *->REACHABLE
758 */
759 if (!is_override && lladdr != NULL && llchange) { /* (1) */
760 /*
761 * If state is REACHABLE, make it STALE.
762 * no other updates should be done.
763 */
764 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
765 ln->ln_state = ND6_LLINFO_STALE;
766 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
767 }
768 goto freeit;
769 } else if (is_override /* (2a) */
770 || (!is_override && lladdr != NULL && !llchange) /* (2b) */
771 || lladdr == NULL) { /* (2c) */
772 /*
773 * Update link-local address, if any.
774 */
775 if (lladdr != NULL) {
776 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
777 ln->la_flags |= LLE_VALID;
778 }
779
780 /*
781 * If solicited, make the state REACHABLE.
782 * If not solicited and the link-layer address was
783 * changed, make it STALE.
784 */
785 if (is_solicited) {
786 ln->ln_state = ND6_LLINFO_REACHABLE;
787 ln->ln_byhint = 0;
788 if (!ND6_LLINFO_PERMANENT(ln)) {
789 nd6_llinfo_settimer(ln,
790 ND_IFINFO(ifp)->reachable * hz);
791 }
792 } else {
793 if (lladdr && llchange) {
794 ln->ln_state = ND6_LLINFO_STALE;
795 nd6_llinfo_settimer(ln,
796 nd6_gctimer * hz);
797 }
798 }
799 }
800
801 if (ln->ln_router && !is_router) {
802 /*
803 * The peer dropped the router flag.
804 * Remove the sender from the Default Router List and
805 * update the Destination Cache entries.
806 */
807 struct nd_defrouter *dr;
808 const struct in6_addr *in6;
809 int s;
810
811 in6 = &ln->r_l3addr.addr6;
812
813 /*
814 * Lock to protect the default router list.
815 * XXX: this might be unnecessary, since this function
816 * is only called under the network software interrupt
817 * context. However, we keep it just for safety.
818 */
819 s = splsoftnet();
820 dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
821 if (dr)
822 defrtrlist_del(dr, NULL);
823 else if (!ip6_forwarding) {
824 /*
825 * Even if the neighbor is not in the default
826 * router list, the neighbor may be used
827 * as a next hop for some destinations
828 * (e.g. redirect case). So we must
829 * call rt6_flush explicitly.
830 */
831 rt6_flush(&ip6->ip6_src, ln->lle_tbl->llt_ifp);
832 }
833 splx(s);
834 }
835 ln->ln_router = is_router;
836 }
837 /*
838 * XXX: does this matter?
839 * rt->rt_flags &= ~RTF_REJECT;
840 */
841 ln->ln_asked = 0;
842 nd6_llinfo_release_pkts(ln, ifp);
843 /* FIXME */
844 #if 0
845 if (rt_announce) /* tell user process about any new lladdr */
846 rt_newmsg(RTM_CHANGE, rt);
847 #endif
848
849 freeit:
850 if (ln != NULL)
851 LLE_WUNLOCK(ln);
852
853 if (checklink)
854 pfxlist_onlink_check();
855
856 m_put_rcvif_psref(ifp, &psref);
857 m_freem(m);
858 return;
859
860 bad:
861 if (ln != NULL)
862 LLE_WUNLOCK(ln);
863
864 ICMP6_STATINC(ICMP6_STAT_BADNA);
865 m_put_rcvif_psref(ifp, &psref);
866 m_freem(m);
867 }
868
869 /*
870 * Neighbor advertisement output handling.
871 *
872 * Based on RFC 2461
873 *
874 * the following items are not implemented yet:
875 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
876 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
877 */
878 void
879 nd6_na_output(
880 struct ifnet *ifp,
881 const struct in6_addr *daddr6_0,
882 const struct in6_addr *taddr6,
883 u_long flags,
884 int tlladdr, /* 1 if include target link-layer address */
885 const struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
886 {
887 struct mbuf *m;
888 struct ip6_hdr *ip6;
889 struct nd_neighbor_advert *nd_na;
890 struct ip6_moptions im6o;
891 struct sockaddr *dst;
892 union {
893 struct sockaddr dst;
894 struct sockaddr_in6 dst6;
895 } u;
896 struct in6_addr *src, daddr6;
897 int icmp6len, maxlen, error;
898 const void *mac;
899 struct route ro;
900
901 mac = NULL;
902 memset(&ro, 0, sizeof(ro));
903
904 daddr6 = *daddr6_0; /* make a local copy for modification */
905
906 /* estimate the size of message */
907 maxlen = sizeof(*ip6) + sizeof(*nd_na);
908 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
909 #ifdef DIAGNOSTIC
910 if (max_linkhdr + maxlen >= MCLBYTES) {
911 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
912 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
913 panic("nd6_na_output: insufficient MCLBYTES");
914 /* NOTREACHED */
915 }
916 #endif
917
918 MGETHDR(m, M_DONTWAIT, MT_DATA);
919 if (m && max_linkhdr + maxlen >= MHLEN) {
920 MCLGET(m, M_DONTWAIT);
921 if ((m->m_flags & M_EXT) == 0) {
922 m_free(m);
923 m = NULL;
924 }
925 }
926 if (m == NULL)
927 return;
928 m_reset_rcvif(m);
929
930 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
931 m->m_flags |= M_MCAST;
932 im6o.im6o_multicast_if_index = if_get_index(ifp);
933 im6o.im6o_multicast_hlim = 255;
934 im6o.im6o_multicast_loop = 0;
935 }
936
937 icmp6len = sizeof(*nd_na);
938 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
939 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
940
941 /* fill neighbor advertisement packet */
942 ip6 = mtod(m, struct ip6_hdr *);
943 ip6->ip6_flow = 0;
944 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
945 ip6->ip6_vfc |= IPV6_VERSION;
946 ip6->ip6_nxt = IPPROTO_ICMPV6;
947 ip6->ip6_hlim = 255;
948 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
949 /* reply to DAD */
950 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
951 daddr6.s6_addr16[1] = 0;
952 daddr6.s6_addr32[1] = 0;
953 daddr6.s6_addr32[2] = 0;
954 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
955 if (in6_setscope(&daddr6, ifp, NULL))
956 goto bad;
957
958 flags &= ~ND_NA_FLAG_SOLICITED;
959 }
960 ip6->ip6_dst = daddr6;
961 sockaddr_in6_init(&u.dst6, &daddr6, 0, 0, 0);
962 dst = &u.dst;
963 if (rtcache_setdst(&ro, dst) != 0)
964 goto bad;
965
966 /*
967 * Select a source whose scope is the same as that of the dest.
968 */
969 src = in6_selectsrc(satosin6(dst), NULL, NULL, &ro, NULL, NULL, NULL, &error);
970 if (src == NULL) {
971 nd6log(LOG_DEBUG, "source can't be "
972 "determined: dst=%s, error=%d\n",
973 ip6_sprintf(&satocsin6(dst)->sin6_addr), error);
974 goto bad;
975 }
976 ip6->ip6_src = *src;
977 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
978 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
979 nd_na->nd_na_code = 0;
980 nd_na->nd_na_target = *taddr6;
981 in6_clearscope(&nd_na->nd_na_target); /* XXX */
982
983 /*
984 * "tlladdr" indicates NS's condition for adding tlladdr or not.
985 * see nd6_ns_input() for details.
986 * Basically, if NS packet is sent to unicast/anycast addr,
987 * target lladdr option SHOULD NOT be included.
988 */
989 if (tlladdr) {
990 /*
991 * sdl0 != NULL indicates proxy NA. If we do proxy, use
992 * lladdr in sdl0. If we are not proxying (sending NA for
993 * my address) use lladdr configured for the interface.
994 */
995 if (sdl0 == NULL)
996 mac = nd6_ifptomac(ifp);
997 else if (sdl0->sa_family == AF_LINK) {
998 const struct sockaddr_dl *sdl;
999 sdl = satocsdl(sdl0);
1000 if (sdl->sdl_alen == ifp->if_addrlen)
1001 mac = CLLADDR(sdl);
1002 }
1003 }
1004 if (tlladdr && mac) {
1005 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1006 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1007
1008 /* roundup to 8 bytes alignment! */
1009 optlen = (optlen + 7) & ~7;
1010
1011 m->m_pkthdr.len += optlen;
1012 m->m_len += optlen;
1013 icmp6len += optlen;
1014 memset((void *)nd_opt, 0, optlen);
1015 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1016 nd_opt->nd_opt_len = optlen >> 3;
1017 memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
1018 } else
1019 flags &= ~ND_NA_FLAG_OVERRIDE;
1020
1021 ip6->ip6_plen = htons((u_int16_t)icmp6len);
1022 nd_na->nd_na_flags_reserved = flags;
1023 nd_na->nd_na_cksum = 0;
1024 nd_na->nd_na_cksum =
1025 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1026
1027 ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
1028
1029 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1030 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1031 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_ADVERT);
1032
1033 rtcache_free(&ro);
1034 return;
1035
1036 bad:
1037 rtcache_free(&ro);
1038 m_freem(m);
1039 return;
1040 }
1041
1042 const void *
1043 nd6_ifptomac(const struct ifnet *ifp)
1044 {
1045 switch (ifp->if_type) {
1046 case IFT_ARCNET:
1047 case IFT_ETHER:
1048 case IFT_FDDI:
1049 case IFT_IEEE1394:
1050 case IFT_PROPVIRTUAL:
1051 case IFT_CARP:
1052 case IFT_L2VLAN:
1053 case IFT_IEEE80211:
1054 return CLLADDR(ifp->if_sadl);
1055 default:
1056 return NULL;
1057 }
1058 }
1059
1060 TAILQ_HEAD(dadq_head, dadq);
1061 struct dadq {
1062 TAILQ_ENTRY(dadq) dad_list;
1063 struct ifaddr *dad_ifa;
1064 int dad_count; /* max NS to send */
1065 int dad_ns_tcount; /* # of trials to send NS */
1066 int dad_ns_ocount; /* NS sent so far */
1067 int dad_ns_icount;
1068 int dad_na_icount;
1069 struct callout dad_timer_ch;
1070 };
1071
1072 static struct dadq_head dadq;
1073 static int dad_init = 0;
1074 static kmutex_t nd6_dad_lock;
1075
1076 static struct dadq *
1077 nd6_dad_find(struct ifaddr *ifa)
1078 {
1079 struct dadq *dp;
1080
1081 KASSERT(mutex_owned(&nd6_dad_lock));
1082
1083 TAILQ_FOREACH(dp, &dadq, dad_list) {
1084 if (dp->dad_ifa == ifa)
1085 return dp;
1086 }
1087 return NULL;
1088 }
1089
1090 static void
1091 nd6_dad_starttimer(struct dadq *dp, int ticks)
1092 {
1093
1094 callout_reset(&dp->dad_timer_ch, ticks,
1095 (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
1096 }
1097
1098 static void
1099 nd6_dad_stoptimer(struct dadq *dp)
1100 {
1101
1102 callout_halt(&dp->dad_timer_ch, softnet_lock);
1103 }
1104
1105 /*
1106 * Start Duplicate Address Detection (DAD) for specified interface address.
1107 *
1108 * Note that callout is used when xtick > 0 and not when xtick == 0.
1109 *
1110 * xtick: minimum delay ticks for IFF_UP event
1111 */
1112 void
1113 nd6_dad_start(struct ifaddr *ifa, int xtick)
1114 {
1115 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1116 struct dadq *dp;
1117
1118 if (!dad_init) {
1119 TAILQ_INIT(&dadq);
1120 mutex_init(&nd6_dad_lock, MUTEX_DEFAULT, IPL_NONE);
1121 dad_init++;
1122 }
1123
1124 /*
1125 * If we don't need DAD, don't do it.
1126 * There are several cases:
1127 * - DAD is disabled (ip6_dad_count == 0)
1128 * - the interface address is anycast
1129 */
1130 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1131 log(LOG_DEBUG,
1132 "nd6_dad_start: called with non-tentative address "
1133 "%s(%s)\n",
1134 ip6_sprintf(&ia->ia_addr.sin6_addr),
1135 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1136 return;
1137 }
1138 if (ia->ia6_flags & IN6_IFF_ANYCAST || !ip6_dad_count) {
1139 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1140 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1141 return;
1142 }
1143 KASSERT(ifa->ifa_ifp != NULL);
1144 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1145 return;
1146
1147 mutex_enter(&nd6_dad_lock);
1148 if (nd6_dad_find(ifa) != NULL) {
1149 mutex_exit(&nd6_dad_lock);
1150 /* DAD already in progress */
1151 return;
1152 }
1153
1154 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1155 if (dp == NULL) {
1156 mutex_exit(&nd6_dad_lock);
1157 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1158 "%s(%s)\n",
1159 ip6_sprintf(&ia->ia_addr.sin6_addr),
1160 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1161 return;
1162 }
1163 memset(dp, 0, sizeof(*dp));
1164 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1165
1166 /*
1167 * Send NS packet for DAD, ip6_dad_count times.
1168 * Note that we must delay the first transmission, if this is the
1169 * first packet to be sent from the interface after interface
1170 * (re)initialization.
1171 */
1172 dp->dad_ifa = ifa;
1173 ifaref(ifa); /* just for safety */
1174 dp->dad_count = ip6_dad_count;
1175 dp->dad_ns_icount = dp->dad_na_icount = 0;
1176 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1177 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1178
1179 nd6log(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1180 ip6_sprintf(&ia->ia_addr.sin6_addr));
1181
1182 if (xtick == 0) {
1183 nd6_dad_ns_output(dp, ifa);
1184 nd6_dad_starttimer(dp,
1185 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1186 } else
1187 nd6_dad_starttimer(dp, xtick);
1188 mutex_exit(&nd6_dad_lock);
1189 }
1190
1191 /*
1192 * terminate DAD unconditionally. used for address removals.
1193 */
1194 void
1195 nd6_dad_stop(struct ifaddr *ifa)
1196 {
1197 struct dadq *dp;
1198
1199 if (!dad_init)
1200 return;
1201
1202 mutex_enter(&nd6_dad_lock);
1203 dp = nd6_dad_find(ifa);
1204 if (dp == NULL) {
1205 mutex_exit(&nd6_dad_lock);
1206 /* DAD wasn't started yet */
1207 return;
1208 }
1209
1210 /* Prevent the timer from running anymore. */
1211 TAILQ_REMOVE(&dadq, dp, dad_list);
1212 mutex_exit(&nd6_dad_lock);
1213
1214 nd6_dad_stoptimer(dp);
1215
1216 free(dp, M_IP6NDP);
1217 dp = NULL;
1218 ifafree(ifa);
1219 }
1220
1221 static void
1222 nd6_dad_timer(struct ifaddr *ifa)
1223 {
1224 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1225 struct dadq *dp;
1226
1227 mutex_enter(softnet_lock);
1228 KERNEL_LOCK(1, NULL);
1229 mutex_enter(&nd6_dad_lock);
1230
1231 /* Sanity check */
1232 if (ia == NULL) {
1233 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1234 goto done;
1235 }
1236 dp = nd6_dad_find(ifa);
1237 if (dp == NULL) {
1238 /* DAD seems to be stopping, so do nothing. */
1239 goto done;
1240 }
1241 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1242 log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
1243 "%s(%s)\n",
1244 ip6_sprintf(&ia->ia_addr.sin6_addr),
1245 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1246 goto done;
1247 }
1248 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1249 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1250 "%s(%s)\n",
1251 ip6_sprintf(&ia->ia_addr.sin6_addr),
1252 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1253 goto done;
1254 }
1255
1256 /* timeouted with IFF_{RUNNING,UP} check */
1257 if (dp->dad_ns_tcount > dad_maxtry) {
1258 nd6log(LOG_INFO, "%s: could not run DAD, driver problem?\n",
1259 if_name(ifa->ifa_ifp));
1260
1261 TAILQ_REMOVE(&dadq, dp, dad_list);
1262 free(dp, M_IP6NDP);
1263 dp = NULL;
1264 ifafree(ifa);
1265 goto done;
1266 }
1267
1268 /* Need more checks? */
1269 if (dp->dad_ns_ocount < dp->dad_count) {
1270 /*
1271 * We have more NS to go. Send NS packet for DAD.
1272 */
1273 nd6_dad_ns_output(dp, ifa);
1274 nd6_dad_starttimer(dp,
1275 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1276 } else {
1277 /*
1278 * We have transmitted sufficient number of DAD packets.
1279 * See what we've got.
1280 */
1281 int duplicate;
1282
1283 duplicate = 0;
1284
1285 if (dp->dad_na_icount) {
1286 /*
1287 * the check is in nd6_dad_na_input(),
1288 * but just in case
1289 */
1290 duplicate++;
1291 }
1292
1293 if (dp->dad_ns_icount) {
1294 /* We've seen NS, means DAD has failed. */
1295 duplicate++;
1296 }
1297
1298 if (duplicate) {
1299 /* (*dp) will be freed in nd6_dad_duplicated() */
1300 dp = NULL;
1301 nd6_dad_duplicated(ifa);
1302 } else {
1303 /*
1304 * We are done with DAD. No NA came, no NS came.
1305 * No duplicate address found.
1306 */
1307 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1308 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1309
1310 nd6log(LOG_DEBUG,
1311 "%s: DAD complete for %s - no duplicates found\n",
1312 if_name(ifa->ifa_ifp),
1313 ip6_sprintf(&ia->ia_addr.sin6_addr));
1314
1315 TAILQ_REMOVE(&dadq, dp, dad_list);
1316 free(dp, M_IP6NDP);
1317 dp = NULL;
1318 ifafree(ifa);
1319 }
1320 }
1321
1322 done:
1323 mutex_exit(&nd6_dad_lock);
1324 KERNEL_UNLOCK_ONE(NULL);
1325 mutex_exit(softnet_lock);
1326 }
1327
1328 void
1329 nd6_dad_duplicated(struct ifaddr *ifa)
1330 {
1331 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1332 struct ifnet *ifp;
1333 struct dadq *dp;
1334
1335 mutex_enter(&nd6_dad_lock);
1336 dp = nd6_dad_find(ifa);
1337 if (dp == NULL) {
1338 mutex_exit(&nd6_dad_lock);
1339 /* DAD seems to be stopping, so do nothing. */
1340 return;
1341 }
1342
1343 ifp = ifa->ifa_ifp;
1344 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1345 "NS in/out=%d/%d, NA in=%d\n",
1346 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1347 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1348
1349 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1350 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1351
1352 /* We are done with DAD, with duplicated address found. (failure) */
1353 nd6_dad_stoptimer(dp);
1354
1355 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1356 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1357 log(LOG_ERR, "%s: manual intervention required\n",
1358 if_name(ifp));
1359
1360 /* Inform the routing socket that DAD has completed */
1361 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1362
1363 /*
1364 * If the address is a link-local address formed from an interface
1365 * identifier based on the hardware address which is supposed to be
1366 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1367 * operation on the interface SHOULD be disabled.
1368 * [rfc2462bis-03 Section 5.4.5]
1369 */
1370 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1371 struct in6_addr in6;
1372
1373 /*
1374 * To avoid over-reaction, we only apply this logic when we are
1375 * very sure that hardware addresses are supposed to be unique.
1376 */
1377 switch (ifp->if_type) {
1378 case IFT_ETHER:
1379 case IFT_FDDI:
1380 case IFT_ATM:
1381 case IFT_IEEE1394:
1382 #ifdef IFT_IEEE80211
1383 case IFT_IEEE80211:
1384 #endif
1385 in6 = ia->ia_addr.sin6_addr;
1386 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1387 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1388 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1389 log(LOG_ERR, "%s: possible hardware address "
1390 "duplication detected, disable IPv6\n",
1391 if_name(ifp));
1392 }
1393 break;
1394 }
1395 }
1396
1397 TAILQ_REMOVE(&dadq, dp, dad_list);
1398 mutex_exit(&nd6_dad_lock);
1399
1400 free(dp, M_IP6NDP);
1401 dp = NULL;
1402 ifafree(ifa);
1403 }
1404
1405 static void
1406 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1407 {
1408 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1409 struct ifnet *ifp = ifa->ifa_ifp;
1410
1411 dp->dad_ns_tcount++;
1412 if ((ifp->if_flags & IFF_UP) == 0) {
1413 #if 0
1414 printf("%s: interface down?\n", if_name(ifp));
1415 #endif
1416 return;
1417 }
1418 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1419 #if 0
1420 printf("%s: interface not running?\n", if_name(ifp));
1421 #endif
1422 return;
1423 }
1424
1425 dp->dad_ns_tcount = 0;
1426 dp->dad_ns_ocount++;
1427 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1428 }
1429
1430 static void
1431 nd6_dad_ns_input(struct ifaddr *ifa)
1432 {
1433 struct in6_ifaddr *ia;
1434 const struct in6_addr *taddr6;
1435 struct dadq *dp;
1436 int duplicate;
1437
1438 if (ifa == NULL)
1439 panic("ifa == NULL in nd6_dad_ns_input");
1440
1441 ia = (struct in6_ifaddr *)ifa;
1442 taddr6 = &ia->ia_addr.sin6_addr;
1443 duplicate = 0;
1444
1445 mutex_enter(&nd6_dad_lock);
1446 dp = nd6_dad_find(ifa);
1447
1448 /* Quickhack - completely ignore DAD NS packets */
1449 if (dad_ignore_ns) {
1450 nd6log(LOG_INFO, "ignoring DAD NS packet for "
1451 "address %s(%s)\n", ip6_sprintf(taddr6),
1452 if_name(ifa->ifa_ifp));
1453 return;
1454 }
1455
1456 /*
1457 * if I'm yet to start DAD, someone else started using this address
1458 * first. I have a duplicate and you win.
1459 */
1460 if (dp == NULL || dp->dad_ns_ocount == 0)
1461 duplicate++;
1462
1463 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1464
1465 if (duplicate) {
1466 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1467 mutex_exit(&nd6_dad_lock);
1468 nd6_dad_duplicated(ifa);
1469 } else {
1470 /*
1471 * not sure if I got a duplicate.
1472 * increment ns count and see what happens.
1473 */
1474 if (dp)
1475 dp->dad_ns_icount++;
1476 mutex_exit(&nd6_dad_lock);
1477 }
1478 }
1479
1480 static void
1481 nd6_dad_na_input(struct ifaddr *ifa)
1482 {
1483 struct dadq *dp;
1484
1485 if (ifa == NULL)
1486 panic("ifa == NULL in nd6_dad_na_input");
1487
1488 mutex_enter(&nd6_dad_lock);
1489 dp = nd6_dad_find(ifa);
1490 if (dp)
1491 dp->dad_na_icount++;
1492 mutex_exit(&nd6_dad_lock);
1493
1494 /* remove the address. */
1495 nd6_dad_duplicated(ifa);
1496 }
1497