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