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