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