nd6_nbr.c revision 1.112 1 /* $NetBSD: nd6_nbr.c,v 1.112 2015/11/25 06:21:26 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.112 2015/11/25 06:21:26 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,
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 in6_addr *hsrc,
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 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
443 src = hsrc;
444 else {
445 int error;
446 struct sockaddr_in6 dst_sa;
447
448 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
449
450 src = in6_selectsrc(&dst_sa, NULL,
451 NULL, &ro, NULL, NULL, &error);
452 if (src == NULL) {
453 nd6log((LOG_DEBUG,
454 "nd6_ns_output: source can't be "
455 "determined: dst=%s, error=%d\n",
456 ip6_sprintf(&dst_sa.sin6_addr), error));
457 goto bad;
458 }
459 }
460 } else {
461 /*
462 * Source address for DAD packet must always be IPv6
463 * unspecified address. (0::0)
464 * We actually don't have to 0-clear the address (we did it
465 * above), but we do so here explicitly to make the intention
466 * clearer.
467 */
468 memset(&src_in, 0, sizeof(src_in));
469 src = &src_in;
470 }
471 ip6->ip6_src = *src;
472 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
473 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
474 nd_ns->nd_ns_code = 0;
475 nd_ns->nd_ns_reserved = 0;
476 nd_ns->nd_ns_target = *taddr6;
477 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
478
479 /*
480 * Add source link-layer address option.
481 *
482 * spec implementation
483 * --- ---
484 * DAD packet MUST NOT do not add the option
485 * there's no link layer address:
486 * impossible do not add the option
487 * there's link layer address:
488 * Multicast NS MUST add one add the option
489 * Unicast NS SHOULD add one add the option
490 */
491 if (!dad && (mac = nd6_ifptomac(ifp))) {
492 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
493 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
494 /* 8 byte alignments... */
495 optlen = (optlen + 7) & ~7;
496
497 m->m_pkthdr.len += optlen;
498 m->m_len += optlen;
499 icmp6len += optlen;
500 memset((void *)nd_opt, 0, optlen);
501 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
502 nd_opt->nd_opt_len = optlen >> 3;
503 memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
504 }
505
506 ip6->ip6_plen = htons((u_int16_t)icmp6len);
507 nd_ns->nd_ns_cksum = 0;
508 nd_ns->nd_ns_cksum =
509 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
510
511 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
512 icmp6_ifstat_inc(ifp, ifs6_out_msg);
513 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
514 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_SOLICIT);
515
516 rtcache_free(&ro);
517 return;
518
519 bad:
520 rtcache_free(&ro);
521 m_freem(m);
522 return;
523 }
524
525 /*
526 * Neighbor advertisement input handling.
527 *
528 * Based on RFC 2461
529 * Based on RFC 2462 (duplicate address detection)
530 *
531 * the following items are not implemented yet:
532 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
533 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
534 */
535 void
536 nd6_na_input(struct mbuf *m, int off, int icmp6len)
537 {
538 struct ifnet *ifp = m->m_pkthdr.rcvif;
539 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
540 struct nd_neighbor_advert *nd_na;
541 struct in6_addr saddr6 = ip6->ip6_src;
542 struct in6_addr daddr6 = ip6->ip6_dst;
543 struct in6_addr taddr6;
544 int flags;
545 int is_router;
546 int is_solicited;
547 int is_override;
548 char *lladdr = NULL;
549 int lladdrlen = 0;
550 struct ifaddr *ifa;
551 struct llentry *ln;
552 struct rtentry *rt = NULL;
553 struct sockaddr_dl *sdl;
554 union nd_opts ndopts;
555 struct sockaddr_in6 ssin6;
556 int rt_announce;
557
558 if (ip6->ip6_hlim != 255) {
559 nd6log((LOG_ERR,
560 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
561 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
562 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
563 goto bad;
564 }
565
566 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
567 if (nd_na == NULL) {
568 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
569 return;
570 }
571
572 flags = nd_na->nd_na_flags_reserved;
573 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
574 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
575 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
576
577 taddr6 = nd_na->nd_na_target;
578 if (in6_setscope(&taddr6, ifp, NULL))
579 return; /* XXX: impossible */
580
581 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
582 nd6log((LOG_ERR,
583 "nd6_na_input: invalid target address %s\n",
584 ip6_sprintf(&taddr6)));
585 goto bad;
586 }
587 if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
588 nd6log((LOG_ERR,
589 "nd6_na_input: a solicited adv is multicasted\n"));
590 goto bad;
591 }
592
593 icmp6len -= sizeof(*nd_na);
594 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
595 if (nd6_options(&ndopts) < 0) {
596 nd6log((LOG_INFO,
597 "nd6_na_input: invalid ND option, ignored\n"));
598 /* nd6_options have incremented stats */
599 goto freeit;
600 }
601
602 if (ndopts.nd_opts_tgt_lladdr) {
603 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
604 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
605 }
606
607 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
608
609 /*
610 * Target address matches one of my interface address.
611 *
612 * If my address is tentative, this means that there's somebody
613 * already using the same address as mine. This indicates DAD failure.
614 * This is defined in RFC 2462.
615 *
616 * Otherwise, process as defined in RFC 2461.
617 */
618 if (ifa
619 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
620 nd6_dad_na_input(ifa);
621 goto freeit;
622 }
623
624 /* Just for safety, maybe unnecessary. */
625 if (ifa) {
626 log(LOG_ERR,
627 "nd6_na_input: duplicate IP6 address %s\n",
628 ip6_sprintf(&taddr6));
629 goto freeit;
630 }
631
632 /*
633 * Make sure the source address is from a neighbor's address.
634 */
635 sockaddr_in6_init(&ssin6, &saddr6, 0, 0, 0);
636 if (nd6_is_addr_neighbor(&ssin6, ifp) == 0) {
637 nd6log((LOG_INFO, "nd6_na_input: "
638 "ND packet from non-neighbor\n"));
639 goto bad;
640 }
641
642 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
643 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
644 "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6),
645 ifp->if_addrlen, lladdrlen - 2));
646 goto bad;
647 }
648
649 /*
650 * If no neighbor cache entry is found, NA SHOULD silently be
651 * discarded.
652 */
653 rt = nd6_lookup(&taddr6, 0, ifp);
654 if ((rt == NULL) ||
655 ((ln = rt->rt_llinfo) == NULL) ||
656 ((sdl = satosdl(rt->rt_gateway)) == NULL))
657 goto freeit;
658
659 rt_announce = 0;
660 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
661 /*
662 * If the link-layer has address, and no lladdr option came,
663 * discard the packet.
664 */
665 if (ifp->if_addrlen && !lladdr)
666 goto freeit;
667
668 /*
669 * Record link-layer address, and update the state.
670 */
671 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lladdr,
672 ifp->if_addrlen);
673 rt_announce = 1;
674 if (is_solicited) {
675 ln->ln_state = ND6_LLINFO_REACHABLE;
676 ln->ln_byhint = 0;
677 if (!ND6_LLINFO_PERMANENT(ln)) {
678 nd6_llinfo_settimer(ln,
679 (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
680 }
681 } else {
682 ln->ln_state = ND6_LLINFO_STALE;
683 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
684 }
685 if ((ln->ln_router = is_router) != 0) {
686 /*
687 * This means a router's state has changed from
688 * non-reachable to probably reachable, and might
689 * affect the status of associated prefixes..
690 */
691 pfxlist_onlink_check();
692 }
693 } else {
694 int llchange;
695
696 /*
697 * Check if the link-layer address has changed or not.
698 */
699 if (lladdr == NULL)
700 llchange = 0;
701 else {
702 if (sdl->sdl_alen) {
703 if (memcmp(lladdr, CLLADDR(sdl), ifp->if_addrlen))
704 llchange = rt_announce = 1;
705 else
706 llchange = 0;
707 } else
708 llchange = rt_announce = 1;
709 }
710
711 /*
712 * This is VERY complex. Look at it with care.
713 *
714 * override solicit lladdr llchange action
715 * (L: record lladdr)
716 *
717 * 0 0 n -- (2c)
718 * 0 0 y n (2b) L
719 * 0 0 y y (1) REACHABLE->STALE
720 * 0 1 n -- (2c) *->REACHABLE
721 * 0 1 y n (2b) L *->REACHABLE
722 * 0 1 y y (1) REACHABLE->STALE
723 * 1 0 n -- (2a)
724 * 1 0 y n (2a) L
725 * 1 0 y y (2a) L *->STALE
726 * 1 1 n -- (2a) *->REACHABLE
727 * 1 1 y n (2a) L *->REACHABLE
728 * 1 1 y y (2a) L *->REACHABLE
729 */
730 if (!is_override && lladdr != NULL && llchange) { /* (1) */
731 /*
732 * If state is REACHABLE, make it STALE.
733 * no other updates should be done.
734 */
735 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
736 ln->ln_state = ND6_LLINFO_STALE;
737 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
738 }
739 goto freeit;
740 } else if (is_override /* (2a) */
741 || (!is_override && lladdr != NULL && !llchange) /* (2b) */
742 || lladdr == NULL) { /* (2c) */
743 /*
744 * Update link-local address, if any.
745 */
746 if (lladdr != NULL) {
747 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len,
748 lladdr, ifp->if_addrlen);
749 }
750
751 /*
752 * If solicited, make the state REACHABLE.
753 * If not solicited and the link-layer address was
754 * changed, make it STALE.
755 */
756 if (is_solicited) {
757 ln->ln_state = ND6_LLINFO_REACHABLE;
758 ln->ln_byhint = 0;
759 if (!ND6_LLINFO_PERMANENT(ln)) {
760 nd6_llinfo_settimer(ln,
761 (long)ND_IFINFO(ifp)->reachable * hz);
762 }
763 } else {
764 if (lladdr && llchange) {
765 ln->ln_state = ND6_LLINFO_STALE;
766 nd6_llinfo_settimer(ln,
767 (long)nd6_gctimer * hz);
768 }
769 }
770 }
771
772 if (ln->ln_router && !is_router) {
773 /*
774 * The peer dropped the router flag.
775 * Remove the sender from the Default Router List and
776 * update the Destination Cache entries.
777 */
778 struct nd_defrouter *dr;
779 const struct in6_addr *in6;
780 int s;
781
782 in6 = &satocsin6(rt_getkey(rt))->sin6_addr;
783
784 /*
785 * Lock to protect the default router list.
786 * XXX: this might be unnecessary, since this function
787 * is only called under the network software interrupt
788 * context. However, we keep it just for safety.
789 */
790 s = splsoftnet();
791 dr = defrouter_lookup(in6, rt->rt_ifp);
792 if (dr)
793 defrtrlist_del(dr, NULL);
794 else if (!ip6_forwarding) {
795 /*
796 * Even if the neighbor is not in the default
797 * router list, the neighbor may be used
798 * as a next hop for some destinations
799 * (e.g. redirect case). So we must
800 * call rt6_flush explicitly.
801 */
802 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
803 }
804 splx(s);
805 }
806 ln->ln_router = is_router;
807 }
808 rt->rt_flags &= ~RTF_REJECT;
809 ln->ln_asked = 0;
810 nd6_llinfo_release_pkts(ln, ifp, rt);
811 if (rt_announce) /* tell user process about any new lladdr */
812 rt_newmsg(RTM_CHANGE, rt);
813
814 freeit:
815 m_freem(m);
816 if (rt != NULL)
817 rtfree(rt);
818 return;
819
820 bad:
821 ICMP6_STATINC(ICMP6_STAT_BADNA);
822 m_freem(m);
823 }
824
825 /*
826 * Neighbor advertisement output handling.
827 *
828 * Based on RFC 2461
829 *
830 * the following items are not implemented yet:
831 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
832 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
833 */
834 void
835 nd6_na_output(
836 struct ifnet *ifp,
837 const struct in6_addr *daddr6_0,
838 const struct in6_addr *taddr6,
839 u_long flags,
840 int tlladdr, /* 1 if include target link-layer address */
841 const struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
842 {
843 struct mbuf *m;
844 struct ip6_hdr *ip6;
845 struct nd_neighbor_advert *nd_na;
846 struct ip6_moptions im6o;
847 struct sockaddr *dst;
848 union {
849 struct sockaddr dst;
850 struct sockaddr_in6 dst6;
851 } u;
852 struct in6_addr *src, daddr6;
853 int icmp6len, maxlen, error;
854 const void *mac;
855 struct route ro;
856
857 mac = NULL;
858 memset(&ro, 0, sizeof(ro));
859
860 daddr6 = *daddr6_0; /* make a local copy for modification */
861
862 /* estimate the size of message */
863 maxlen = sizeof(*ip6) + sizeof(*nd_na);
864 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
865 #ifdef DIAGNOSTIC
866 if (max_linkhdr + maxlen >= MCLBYTES) {
867 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
868 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
869 panic("nd6_na_output: insufficient MCLBYTES");
870 /* NOTREACHED */
871 }
872 #endif
873
874 MGETHDR(m, M_DONTWAIT, MT_DATA);
875 if (m && max_linkhdr + maxlen >= MHLEN) {
876 MCLGET(m, M_DONTWAIT);
877 if ((m->m_flags & M_EXT) == 0) {
878 m_free(m);
879 m = NULL;
880 }
881 }
882 if (m == NULL)
883 return;
884 m->m_pkthdr.rcvif = NULL;
885
886 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
887 m->m_flags |= M_MCAST;
888 im6o.im6o_multicast_ifp = ifp;
889 im6o.im6o_multicast_hlim = 255;
890 im6o.im6o_multicast_loop = 0;
891 }
892
893 icmp6len = sizeof(*nd_na);
894 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
895 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
896
897 /* fill neighbor advertisement packet */
898 ip6 = mtod(m, struct ip6_hdr *);
899 ip6->ip6_flow = 0;
900 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
901 ip6->ip6_vfc |= IPV6_VERSION;
902 ip6->ip6_nxt = IPPROTO_ICMPV6;
903 ip6->ip6_hlim = 255;
904 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
905 /* reply to DAD */
906 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
907 daddr6.s6_addr16[1] = 0;
908 daddr6.s6_addr32[1] = 0;
909 daddr6.s6_addr32[2] = 0;
910 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
911 if (in6_setscope(&daddr6, ifp, NULL))
912 goto bad;
913
914 flags &= ~ND_NA_FLAG_SOLICITED;
915 }
916 ip6->ip6_dst = daddr6;
917 sockaddr_in6_init(&u.dst6, &daddr6, 0, 0, 0);
918 dst = &u.dst;
919 if (rtcache_setdst(&ro, dst) != 0)
920 goto bad;
921
922 /*
923 * Select a source whose scope is the same as that of the dest.
924 */
925 src = in6_selectsrc(satosin6(dst), NULL, NULL, &ro, NULL, NULL, &error);
926 if (src == NULL) {
927 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
928 "determined: dst=%s, error=%d\n",
929 ip6_sprintf(&satocsin6(dst)->sin6_addr), error));
930 goto bad;
931 }
932 ip6->ip6_src = *src;
933 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
934 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
935 nd_na->nd_na_code = 0;
936 nd_na->nd_na_target = *taddr6;
937 in6_clearscope(&nd_na->nd_na_target); /* XXX */
938
939 /*
940 * "tlladdr" indicates NS's condition for adding tlladdr or not.
941 * see nd6_ns_input() for details.
942 * Basically, if NS packet is sent to unicast/anycast addr,
943 * target lladdr option SHOULD NOT be included.
944 */
945 if (tlladdr) {
946 /*
947 * sdl0 != NULL indicates proxy NA. If we do proxy, use
948 * lladdr in sdl0. If we are not proxying (sending NA for
949 * my address) use lladdr configured for the interface.
950 */
951 if (sdl0 == NULL)
952 mac = nd6_ifptomac(ifp);
953 else if (sdl0->sa_family == AF_LINK) {
954 const struct sockaddr_dl *sdl;
955 sdl = satocsdl(sdl0);
956 if (sdl->sdl_alen == ifp->if_addrlen)
957 mac = CLLADDR(sdl);
958 }
959 }
960 if (tlladdr && mac) {
961 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
962 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
963
964 /* roundup to 8 bytes alignment! */
965 optlen = (optlen + 7) & ~7;
966
967 m->m_pkthdr.len += optlen;
968 m->m_len += optlen;
969 icmp6len += optlen;
970 memset((void *)nd_opt, 0, optlen);
971 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
972 nd_opt->nd_opt_len = optlen >> 3;
973 memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
974 } else
975 flags &= ~ND_NA_FLAG_OVERRIDE;
976
977 ip6->ip6_plen = htons((u_int16_t)icmp6len);
978 nd_na->nd_na_flags_reserved = flags;
979 nd_na->nd_na_cksum = 0;
980 nd_na->nd_na_cksum =
981 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
982
983 ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
984
985 icmp6_ifstat_inc(ifp, ifs6_out_msg);
986 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
987 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_ADVERT);
988
989 rtcache_free(&ro);
990 return;
991
992 bad:
993 rtcache_free(&ro);
994 m_freem(m);
995 return;
996 }
997
998 const void *
999 nd6_ifptomac(const struct ifnet *ifp)
1000 {
1001 switch (ifp->if_type) {
1002 case IFT_ARCNET:
1003 case IFT_ETHER:
1004 case IFT_FDDI:
1005 case IFT_IEEE1394:
1006 case IFT_PROPVIRTUAL:
1007 case IFT_CARP:
1008 case IFT_L2VLAN:
1009 case IFT_IEEE80211:
1010 return CLLADDR(ifp->if_sadl);
1011 default:
1012 return NULL;
1013 }
1014 }
1015
1016 TAILQ_HEAD(dadq_head, dadq);
1017 struct dadq {
1018 TAILQ_ENTRY(dadq) dad_list;
1019 struct ifaddr *dad_ifa;
1020 int dad_count; /* max NS to send */
1021 int dad_ns_tcount; /* # of trials to send NS */
1022 int dad_ns_ocount; /* NS sent so far */
1023 int dad_ns_icount;
1024 int dad_na_icount;
1025 struct callout dad_timer_ch;
1026 };
1027
1028 static struct dadq_head dadq;
1029 static int dad_init = 0;
1030
1031 static struct dadq *
1032 nd6_dad_find(struct ifaddr *ifa)
1033 {
1034 struct dadq *dp;
1035
1036 TAILQ_FOREACH(dp, &dadq, dad_list) {
1037 if (dp->dad_ifa == ifa)
1038 return dp;
1039 }
1040 return NULL;
1041 }
1042
1043 static void
1044 nd6_dad_starttimer(struct dadq *dp, int ticks)
1045 {
1046
1047 callout_reset(&dp->dad_timer_ch, ticks,
1048 (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
1049 }
1050
1051 static void
1052 nd6_dad_stoptimer(struct dadq *dp)
1053 {
1054
1055 callout_stop(&dp->dad_timer_ch);
1056 }
1057
1058 /*
1059 * Start Duplicate Address Detection (DAD) for specified interface address.
1060 *
1061 * Note that callout is used when xtick > 0 and not when xtick == 0.
1062 *
1063 * xtick: minimum delay ticks for IFF_UP event
1064 */
1065 void
1066 nd6_dad_start(struct ifaddr *ifa, int xtick)
1067 {
1068 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1069 struct dadq *dp;
1070
1071 if (!dad_init) {
1072 TAILQ_INIT(&dadq);
1073 dad_init++;
1074 }
1075
1076 /*
1077 * If we don't need DAD, don't do it.
1078 * There are several cases:
1079 * - DAD is disabled (ip6_dad_count == 0)
1080 * - the interface address is anycast
1081 */
1082 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1083 log(LOG_DEBUG,
1084 "nd6_dad_start: called with non-tentative address "
1085 "%s(%s)\n",
1086 ip6_sprintf(&ia->ia_addr.sin6_addr),
1087 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1088 return;
1089 }
1090 if (ia->ia6_flags & IN6_IFF_ANYCAST || !ip6_dad_count) {
1091 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1092 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1093 return;
1094 }
1095 if (ifa->ifa_ifp == NULL)
1096 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1097 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1098 return;
1099 if (nd6_dad_find(ifa) != NULL) {
1100 /* DAD already in progress */
1101 return;
1102 }
1103
1104 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1105 if (dp == NULL) {
1106 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1107 "%s(%s)\n",
1108 ip6_sprintf(&ia->ia_addr.sin6_addr),
1109 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1110 return;
1111 }
1112 memset(dp, 0, sizeof(*dp));
1113 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1114 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1115
1116 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1117 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1118
1119 /*
1120 * Send NS packet for DAD, ip6_dad_count times.
1121 * Note that we must delay the first transmission, if this is the
1122 * first packet to be sent from the interface after interface
1123 * (re)initialization.
1124 */
1125 dp->dad_ifa = ifa;
1126 ifaref(ifa); /* just for safety */
1127 dp->dad_count = ip6_dad_count;
1128 dp->dad_ns_icount = dp->dad_na_icount = 0;
1129 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1130 if (xtick == 0) {
1131 nd6_dad_ns_output(dp, ifa);
1132 nd6_dad_starttimer(dp,
1133 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1134 } else
1135 nd6_dad_starttimer(dp, xtick);
1136 }
1137
1138 /*
1139 * terminate DAD unconditionally. used for address removals.
1140 */
1141 void
1142 nd6_dad_stop(struct ifaddr *ifa)
1143 {
1144 struct dadq *dp;
1145
1146 if (!dad_init)
1147 return;
1148 dp = nd6_dad_find(ifa);
1149 if (dp == NULL) {
1150 /* DAD wasn't started yet */
1151 return;
1152 }
1153
1154 nd6_dad_stoptimer(dp);
1155
1156 TAILQ_REMOVE(&dadq, dp, dad_list);
1157 free(dp, M_IP6NDP);
1158 dp = NULL;
1159 ifafree(ifa);
1160 }
1161
1162 static void
1163 nd6_dad_timer(struct ifaddr *ifa)
1164 {
1165 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1166 struct dadq *dp;
1167
1168 mutex_enter(softnet_lock);
1169 KERNEL_LOCK(1, NULL);
1170
1171 /* Sanity check */
1172 if (ia == NULL) {
1173 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1174 goto done;
1175 }
1176 dp = nd6_dad_find(ifa);
1177 if (dp == NULL) {
1178 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1179 goto done;
1180 }
1181 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1182 log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
1183 "%s(%s)\n",
1184 ip6_sprintf(&ia->ia_addr.sin6_addr),
1185 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1186 goto done;
1187 }
1188 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1189 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1190 "%s(%s)\n",
1191 ip6_sprintf(&ia->ia_addr.sin6_addr),
1192 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1193 goto done;
1194 }
1195
1196 /* timeouted with IFF_{RUNNING,UP} check */
1197 if (dp->dad_ns_tcount > dad_maxtry) {
1198 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1199 if_name(ifa->ifa_ifp)));
1200
1201 TAILQ_REMOVE(&dadq, dp, dad_list);
1202 free(dp, M_IP6NDP);
1203 dp = NULL;
1204 ifafree(ifa);
1205 goto done;
1206 }
1207
1208 /* Need more checks? */
1209 if (dp->dad_ns_ocount < dp->dad_count) {
1210 /*
1211 * We have more NS to go. Send NS packet for DAD.
1212 */
1213 nd6_dad_ns_output(dp, ifa);
1214 nd6_dad_starttimer(dp,
1215 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1216 } else {
1217 /*
1218 * We have transmitted sufficient number of DAD packets.
1219 * See what we've got.
1220 */
1221 int duplicate;
1222
1223 duplicate = 0;
1224
1225 if (dp->dad_na_icount) {
1226 /*
1227 * the check is in nd6_dad_na_input(),
1228 * but just in case
1229 */
1230 duplicate++;
1231 }
1232
1233 if (dp->dad_ns_icount) {
1234 /* We've seen NS, means DAD has failed. */
1235 duplicate++;
1236 }
1237
1238 if (duplicate) {
1239 /* (*dp) will be freed in nd6_dad_duplicated() */
1240 dp = NULL;
1241 nd6_dad_duplicated(ifa);
1242 } else {
1243 /*
1244 * We are done with DAD. No NA came, no NS came.
1245 * No duplicate address found.
1246 */
1247 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1248 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1249
1250 nd6log((LOG_DEBUG,
1251 "%s: DAD complete for %s - no duplicates found\n",
1252 if_name(ifa->ifa_ifp),
1253 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1254
1255 TAILQ_REMOVE(&dadq, dp, dad_list);
1256 free(dp, M_IP6NDP);
1257 dp = NULL;
1258 ifafree(ifa);
1259 }
1260 }
1261
1262 done:
1263 KERNEL_UNLOCK_ONE(NULL);
1264 mutex_exit(softnet_lock);
1265 }
1266
1267 void
1268 nd6_dad_duplicated(struct ifaddr *ifa)
1269 {
1270 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1271 struct ifnet *ifp;
1272 struct dadq *dp;
1273
1274 dp = nd6_dad_find(ifa);
1275 if (dp == NULL) {
1276 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1277 return;
1278 }
1279
1280 ifp = ifa->ifa_ifp;
1281 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1282 "NS in/out=%d/%d, NA in=%d\n",
1283 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1284 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1285
1286 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1287 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1288
1289 /* We are done with DAD, with duplicated address found. (failure) */
1290 nd6_dad_stoptimer(dp);
1291
1292 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1293 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1294 log(LOG_ERR, "%s: manual intervention required\n",
1295 if_name(ifp));
1296
1297 /* Inform the routing socket that DAD has completed */
1298 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1299
1300 /*
1301 * If the address is a link-local address formed from an interface
1302 * identifier based on the hardware address which is supposed to be
1303 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1304 * operation on the interface SHOULD be disabled.
1305 * [rfc2462bis-03 Section 5.4.5]
1306 */
1307 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1308 struct in6_addr in6;
1309
1310 /*
1311 * To avoid over-reaction, we only apply this logic when we are
1312 * very sure that hardware addresses are supposed to be unique.
1313 */
1314 switch (ifp->if_type) {
1315 case IFT_ETHER:
1316 case IFT_FDDI:
1317 case IFT_ATM:
1318 case IFT_IEEE1394:
1319 #ifdef IFT_IEEE80211
1320 case IFT_IEEE80211:
1321 #endif
1322 in6 = ia->ia_addr.sin6_addr;
1323 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1324 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1325 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1326 log(LOG_ERR, "%s: possible hardware address "
1327 "duplication detected, disable IPv6\n",
1328 if_name(ifp));
1329 }
1330 break;
1331 }
1332 }
1333
1334 TAILQ_REMOVE(&dadq, dp, dad_list);
1335 free(dp, M_IP6NDP);
1336 dp = NULL;
1337 ifafree(ifa);
1338 }
1339
1340 static void
1341 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1342 {
1343 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1344 struct ifnet *ifp = ifa->ifa_ifp;
1345
1346 dp->dad_ns_tcount++;
1347 if ((ifp->if_flags & IFF_UP) == 0) {
1348 #if 0
1349 printf("%s: interface down?\n", if_name(ifp));
1350 #endif
1351 return;
1352 }
1353 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1354 #if 0
1355 printf("%s: interface not running?\n", if_name(ifp));
1356 #endif
1357 return;
1358 }
1359
1360 dp->dad_ns_tcount = 0;
1361 dp->dad_ns_ocount++;
1362 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1363 }
1364
1365 static void
1366 nd6_dad_ns_input(struct ifaddr *ifa)
1367 {
1368 struct in6_ifaddr *ia;
1369 const struct in6_addr *taddr6;
1370 struct dadq *dp;
1371 int duplicate;
1372
1373 if (ifa == NULL)
1374 panic("ifa == NULL in nd6_dad_ns_input");
1375
1376 ia = (struct in6_ifaddr *)ifa;
1377 taddr6 = &ia->ia_addr.sin6_addr;
1378 duplicate = 0;
1379 dp = nd6_dad_find(ifa);
1380
1381 /* Quickhack - completely ignore DAD NS packets */
1382 if (dad_ignore_ns) {
1383 nd6log((LOG_INFO,
1384 "nd6_dad_ns_input: ignoring DAD NS packet for "
1385 "address %s(%s)\n", ip6_sprintf(taddr6),
1386 if_name(ifa->ifa_ifp)));
1387 return;
1388 }
1389
1390 /*
1391 * if I'm yet to start DAD, someone else started using this address
1392 * first. I have a duplicate and you win.
1393 */
1394 if (dp == NULL || dp->dad_ns_ocount == 0)
1395 duplicate++;
1396
1397 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1398
1399 if (duplicate) {
1400 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1401 nd6_dad_duplicated(ifa);
1402 } else {
1403 /*
1404 * not sure if I got a duplicate.
1405 * increment ns count and see what happens.
1406 */
1407 if (dp)
1408 dp->dad_ns_icount++;
1409 }
1410 }
1411
1412 static void
1413 nd6_dad_na_input(struct ifaddr *ifa)
1414 {
1415 struct dadq *dp;
1416
1417 if (ifa == NULL)
1418 panic("ifa == NULL in nd6_dad_na_input");
1419
1420 dp = nd6_dad_find(ifa);
1421 if (dp)
1422 dp->dad_na_icount++;
1423
1424 /* remove the address. */
1425 nd6_dad_duplicated(ifa);
1426 }
1427