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