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