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