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