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