HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / netinet6 / nd6_nbr.c
blob5aff021ce76640ce6989be6cf50f7b972426f7ea
1 /* $FreeBSD: src/sys/netinet6/nd6_nbr.c,v 1.4.2.6 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/nd6_nbr.c,v 1.23 2008/03/07 11:34:21 sephe Exp $ */
3 /* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 #include "opt_carp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/time.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
50 #include <sys/callout.h>
51 #include <sys/thread2.h>
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/nd6.h>
64 #include <netinet/icmp6.h>
66 #ifdef IPSEC
67 #include <netinet6/ipsec.h>
68 #ifdef INET6
69 #include <netinet6/ipsec6.h>
70 #endif
71 #endif
73 #include <net/net_osdep.h>
75 #ifdef CARP
76 #include <netinet/ip_carp.h>
77 #endif
80 #define SDL(s) ((struct sockaddr_dl *)s)
82 struct dadq;
83 static struct dadq *nd6_dad_find (struct ifaddr *);
84 static void nd6_dad_starttimer (struct dadq *, int);
85 static void nd6_dad_stoptimer (struct dadq *);
86 static void nd6_dad_timer (struct ifaddr *);
87 static void nd6_dad_ns_output (struct dadq *, struct ifaddr *);
88 static void nd6_dad_ns_input (struct ifaddr *);
89 static void nd6_dad_na_input (struct ifaddr *);
91 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
92 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
95 * Input an Neighbor Solicitation Message.
97 * Based on RFC 2461
98 * Based on RFC 2462 (duplicated address detection)
100 void
101 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
103 struct ifnet *ifp = m->m_pkthdr.rcvif;
104 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
105 struct nd_neighbor_solicit *nd_ns;
106 struct in6_addr saddr6 = ip6->ip6_src;
107 struct in6_addr daddr6 = ip6->ip6_dst;
108 struct in6_addr taddr6;
109 struct in6_addr myaddr6;
110 char *lladdr = NULL;
111 struct ifaddr *ifa = NULL;
112 int lladdrlen = 0;
113 int anycast = 0, proxy = 0, tentative = 0;
114 int tlladdr;
115 union nd_opts ndopts;
116 struct sockaddr_dl *proxydl = NULL;
118 #ifndef PULLDOWN_TEST
119 IP6_EXTHDR_CHECK(m, off, icmp6len,);
120 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
121 #else
122 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
123 if (nd_ns == NULL) {
124 icmp6stat.icp6s_tooshort++;
125 return;
127 #endif
128 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
129 taddr6 = nd_ns->nd_ns_target;
131 if (ip6->ip6_hlim != 255) {
132 nd6log((LOG_ERR,
133 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
134 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
135 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
136 goto bad;
139 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
140 /* dst has to be solicited node multicast address. */
141 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
142 /* don't check ifindex portion */
143 && daddr6.s6_addr32[1] == 0
144 && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
145 && daddr6.s6_addr8[12] == 0xff) {
146 ; /* good */
147 } else {
148 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
149 "(wrong ip6 dst)\n"));
150 goto bad;
154 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
155 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
156 goto bad;
159 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
160 taddr6.s6_addr16[1] = htons(ifp->if_index);
162 icmp6len -= sizeof(*nd_ns);
163 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
164 if (nd6_options(&ndopts) < 0) {
165 nd6log((LOG_INFO,
166 "nd6_ns_input: invalid ND option, ignored\n"));
167 /* nd6_options have incremented stats */
168 goto freeit;
171 if (ndopts.nd_opts_src_lladdr) {
172 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
173 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
176 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
177 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
178 "(link-layer address option)\n"));
179 goto bad;
183 * Attaching target link-layer address to the NA?
184 * (RFC 2461 7.2.4)
186 * NS IP dst is unicast/anycast MUST NOT add
187 * NS IP dst is solicited-node multicast MUST add
189 * In implementation, we add target link-layer address by default.
190 * We do not add one in MUST NOT cases.
192 #if 0 /* too much! */
193 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
194 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
195 tlladdr = 0;
196 else
197 #endif
198 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
199 tlladdr = 0;
200 else
201 tlladdr = 1;
204 * Target address (taddr6) must be either:
205 * (1) Valid unicast/anycast address for my receiving interface,
206 * (2) Unicast address for which I'm offering proxy service, or
207 * (3) "tentative" address on which DAD is being performed.
209 /* (1) and (3) check. */
210 #ifdef CARP
211 if (ifp->if_carp)
212 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
213 if (!ifa)
214 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
215 #else
216 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
217 #endif
219 /* (2) check. */
220 if (!ifa) {
221 struct rtentry *rt;
222 struct sockaddr_in6 tsin6;
224 bzero(&tsin6, sizeof tsin6);
225 tsin6.sin6_len = sizeof(struct sockaddr_in6);
226 tsin6.sin6_family = AF_INET6;
227 tsin6.sin6_addr = taddr6;
229 rt = rtpurelookup((struct sockaddr *)&tsin6);
230 if (rt != NULL && (rt->rt_flags & RTF_ANNOUNCE) &&
231 rt->rt_gateway->sa_family == AF_LINK) {
233 * proxy NDP for single entry
235 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
236 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
237 if (ifa) {
238 proxy = 1;
239 proxydl = SDL(rt->rt_gateway);
242 if (rt != NULL)
243 --rt->rt_refcnt;
245 if (ifa == NULL) {
247 * We've got an NS packet, and we don't have that adddress
248 * assigned for us. We MUST silently ignore it.
249 * See RFC2461 7.2.3.
251 goto freeit;
253 myaddr6 = *IFA_IN6(ifa);
254 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
255 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
256 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
257 goto freeit;
259 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
260 nd6log((LOG_INFO,
261 "nd6_ns_input: lladdrlen mismatch for %s "
262 "(if %d, NS packet %d)\n",
263 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
264 goto bad;
267 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
268 nd6log((LOG_INFO,
269 "nd6_ns_input: duplicate IP6 address %s\n",
270 ip6_sprintf(&saddr6)));
271 goto freeit;
275 * We have neighbor solicitation packet, with target address equals to
276 * one of my tentative address.
278 * src addr how to process?
279 * --- ---
280 * multicast of course, invalid (rejected in ip6_input)
281 * unicast somebody is doing address resolution -> ignore
282 * unspec dup address detection
284 * The processing is defined in RFC 2462.
286 if (tentative) {
288 * If source address is unspecified address, it is for
289 * duplicated address detection.
291 * If not, the packet is for addess resolution;
292 * silently ignore it.
294 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
295 nd6_dad_ns_input(ifa);
297 goto freeit;
301 * If the source address is unspecified address, entries must not
302 * be created or updated.
303 * It looks that sender is performing DAD. Output NA toward
304 * all-node multicast address, to tell the sender that I'm using
305 * the address.
306 * S bit ("solicited") must be zero.
308 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
309 saddr6 = kin6addr_linklocal_allnodes;
310 saddr6.s6_addr16[1] = htons(ifp->if_index);
311 nd6_na_output(ifp, &saddr6, &taddr6,
312 ((anycast || proxy || !tlladdr)
313 ? 0 : ND_NA_FLAG_OVERRIDE)
314 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
315 tlladdr, (struct sockaddr *)proxydl);
316 goto freeit;
319 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
321 nd6_na_output(ifp, &saddr6, &taddr6,
322 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
323 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
324 | ND_NA_FLAG_SOLICITED,
325 tlladdr, (struct sockaddr *)proxydl);
326 freeit:
327 m_freem(m);
328 return;
330 bad:
331 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
332 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
333 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
334 icmp6stat.icp6s_badns++;
335 m_freem(m);
339 * Output an Neighbor Solicitation Message. Caller specifies:
340 * - ICMP6 header source IP6 address
341 * - ND6 header target IP6 address
342 * - ND6 header source datalink address
344 * Based on RFC 2461
345 * Based on RFC 2462 (duplicated address detection)
347 void
348 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
349 const struct in6_addr *taddr6,
350 struct llinfo_nd6 *ln, /* for source address determination */
351 int dad) /* duplicated address detection */
353 struct mbuf *m;
354 struct ip6_hdr *ip6;
355 struct nd_neighbor_solicit *nd_ns;
356 struct in6_ifaddr *ia = NULL;
357 struct ip6_moptions im6o;
358 int icmp6len;
359 int maxlen;
360 caddr_t mac;
361 struct ifnet *outif = NULL;
363 if (IN6_IS_ADDR_MULTICAST(taddr6))
364 return;
366 /* estimate the size of message */
367 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
368 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
369 if (max_linkhdr + maxlen >= MCLBYTES) {
370 #ifdef DIAGNOSTIC
371 kprintf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
372 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
373 #endif
374 return;
377 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
378 if (m == NULL)
379 return;
381 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
382 m->m_flags |= M_MCAST;
383 im6o.im6o_multicast_ifp = ifp;
384 im6o.im6o_multicast_hlim = 255;
385 im6o.im6o_multicast_loop = 0;
388 icmp6len = sizeof(*nd_ns);
389 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
390 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
392 /* fill neighbor solicitation packet */
393 ip6 = mtod(m, struct ip6_hdr *);
394 ip6->ip6_flow = 0;
395 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
396 ip6->ip6_vfc |= IPV6_VERSION;
397 /* ip6->ip6_plen will be set later */
398 ip6->ip6_nxt = IPPROTO_ICMPV6;
399 ip6->ip6_hlim = 255;
400 if (daddr6)
401 ip6->ip6_dst = *daddr6;
402 else {
403 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
404 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
405 ip6->ip6_dst.s6_addr32[1] = 0;
406 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
407 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
408 ip6->ip6_dst.s6_addr8[12] = 0xff;
410 if (!dad) {
411 #if 0 /* KAME way, exact address scope match */
413 * Select a source whose scope is the same as that of the dest.
414 * Typically, the dest is link-local solicitation multicast
415 * (i.e. neighbor discovery) or link-local/global unicast
416 * (i.e. neighbor un-reachability detection).
418 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
419 if (ia == NULL) {
420 m_freem(m);
421 return;
423 ip6->ip6_src = ia->ia_addr.sin6_addr;
424 #else /* spec-wise correct */
426 * RFC2461 7.2.2:
427 * "If the source address of the packet prompting the
428 * solicitation is the same as one of the addresses assigned
429 * to the outgoing interface, that address SHOULD be placed
430 * in the IP Source Address of the outgoing solicitation.
431 * Otherwise, any one of the addresses assigned to the
432 * interface should be used."
434 * We use the source address for the prompting packet
435 * (saddr6), if:
436 * - saddr6 is given from the caller (by giving "ln"), and
437 * - saddr6 belongs to the outgoing interface.
438 * Otherwise, we perform a scope-wise match.
440 struct ip6_hdr *hip6; /* hold ip6 */
441 struct in6_addr *saddr6;
443 if (ln && ln->ln_hold) {
444 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
445 /* XXX pullup? */
446 if (sizeof(*hip6) < ln->ln_hold->m_len)
447 saddr6 = &hip6->ip6_src;
448 else
449 saddr6 = NULL;
450 } else
451 saddr6 = NULL;
452 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
453 bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
454 else {
455 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
456 if (ia == NULL) {
457 m_freem(m);
458 return;
460 ip6->ip6_src = ia->ia_addr.sin6_addr;
462 #endif
463 } else {
465 * Source address for DAD packet must always be IPv6
466 * unspecified address. (0::0)
468 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
470 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
471 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
472 nd_ns->nd_ns_code = 0;
473 nd_ns->nd_ns_reserved = 0;
474 nd_ns->nd_ns_target = *taddr6;
476 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
477 nd_ns->nd_ns_target.s6_addr16[1] = 0;
480 * Add source link-layer address option.
482 * spec implementation
483 * --- ---
484 * DAD packet MUST NOT do not add the option
485 * there's no link layer address:
486 * impossible do not add the option
487 * there's link layer address:
488 * Multicast NS MUST add one add the option
489 * Unicast NS SHOULD add one add the option
491 if (!dad && (mac = nd6_ifptomac(ifp))) {
492 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
493 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
494 /* 8 byte alignments... */
495 optlen = (optlen + 7) & ~7;
497 m->m_pkthdr.len += optlen;
498 m->m_len += optlen;
499 icmp6len += optlen;
500 bzero((caddr_t)nd_opt, optlen);
501 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
502 nd_opt->nd_opt_len = optlen >> 3;
503 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
506 ip6->ip6_plen = htons((u_short)icmp6len);
507 nd_ns->nd_ns_cksum = 0;
508 nd_ns->nd_ns_cksum
509 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
511 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif, NULL);
512 if (outif) {
513 icmp6_ifstat_inc(outif, ifs6_out_msg);
514 icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
516 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
520 * Neighbor advertisement input handling.
522 * Based on RFC 2461
523 * Based on RFC 2462 (duplicated address detection)
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)
529 void
530 nd6_na_input(struct mbuf *m, int off, int icmp6len)
532 struct ifnet *ifp = m->m_pkthdr.rcvif;
533 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
534 struct nd_neighbor_advert *nd_na;
535 #if 0
536 struct in6_addr saddr6 = ip6->ip6_src;
537 #endif
538 struct in6_addr daddr6 = ip6->ip6_dst;
539 struct in6_addr taddr6;
540 int flags;
541 int is_router;
542 int is_solicited;
543 int is_override;
544 char *lladdr = NULL;
545 int lladdrlen = 0;
546 struct ifaddr *ifa;
547 struct llinfo_nd6 *ln;
548 struct rtentry *rt;
549 struct sockaddr_dl *sdl;
550 union nd_opts ndopts;
552 if (ip6->ip6_hlim != 255) {
553 nd6log((LOG_ERR,
554 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
555 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
556 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
557 goto bad;
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;
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);
576 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
577 taddr6.s6_addr16[1] = htons(ifp->if_index);
579 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
580 nd6log((LOG_ERR,
581 "nd6_na_input: invalid target address %s\n",
582 ip6_sprintf(&taddr6)));
583 goto bad;
585 if (IN6_IS_ADDR_MULTICAST(&daddr6))
586 if (is_solicited) {
587 nd6log((LOG_ERR,
588 "nd6_na_input: a solicited adv is multicasted\n"));
589 goto bad;
592 icmp6len -= sizeof(*nd_na);
593 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
594 if (nd6_options(&ndopts) < 0) {
595 nd6log((LOG_INFO,
596 "nd6_na_input: invalid ND option, ignored\n"));
597 /* nd6_options have incremented stats */
598 goto freeit;
601 if (ndopts.nd_opts_tgt_lladdr) {
602 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
603 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
606 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
609 * Target address matches one of my interface address.
611 * If my address is tentative, this means that there's somebody
612 * already using the same address as mine. This indicates DAD failure.
613 * This is defined in RFC 2462.
615 * Otherwise, process as defined in RFC 2461.
617 if (ifa
618 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
619 nd6_dad_na_input(ifa);
620 goto freeit;
623 /* Just for safety, maybe unnecessary. */
624 if (ifa) {
625 log(LOG_ERR,
626 "nd6_na_input: duplicate IP6 address %s\n",
627 ip6_sprintf(&taddr6));
628 goto freeit;
631 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
632 nd6log((LOG_INFO,
633 "nd6_na_input: lladdrlen mismatch for %s "
634 "(if %d, NA packet %d)\n",
635 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
636 goto bad;
640 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
642 rt = nd6_lookup(&taddr6, 0, ifp);
643 if ((rt == NULL) ||
644 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
645 ((sdl = SDL(rt->rt_gateway)) == NULL))
646 goto freeit;
648 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
650 * If the link-layer has address, and no lladdr option came,
651 * discard the packet.
653 if (ifp->if_addrlen && !lladdr)
654 goto freeit;
657 * Record link-layer address, and update the state.
659 sdl->sdl_alen = ifp->if_addrlen;
660 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
661 if (is_solicited) {
662 ln->ln_state = ND6_LLINFO_REACHABLE;
663 ln->ln_byhint = 0;
664 if (ln->ln_expire)
665 ln->ln_expire = time_second +
666 ND_IFINFO(rt->rt_ifp)->reachable;
667 } else {
668 ln->ln_state = ND6_LLINFO_STALE;
669 ln->ln_expire = time_second + nd6_gctimer;
671 if ((ln->ln_router = is_router) != 0) {
673 * This means a router's state has changed from
674 * non-reachable to probably reachable, and might
675 * affect the status of associated prefixes..
677 pfxlist_onlink_check();
679 } else {
680 int llchange;
683 * Check if the link-layer address has changed or not.
685 if (!lladdr)
686 llchange = 0;
687 else {
688 if (sdl->sdl_alen) {
689 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
690 llchange = 1;
691 else
692 llchange = 0;
693 } else
694 llchange = 1;
698 * This is VERY complex. Look at it with care.
700 * override solicit lladdr llchange action
701 * (L: record lladdr)
703 * 0 0 n -- (2c)
704 * 0 0 y n (2b) L
705 * 0 0 y y (1) REACHABLE->STALE
706 * 0 1 n -- (2c) *->REACHABLE
707 * 0 1 y n (2b) L *->REACHABLE
708 * 0 1 y y (1) REACHABLE->STALE
709 * 1 0 n -- (2a)
710 * 1 0 y n (2a) L
711 * 1 0 y y (2a) L *->STALE
712 * 1 1 n -- (2a) *->REACHABLE
713 * 1 1 y n (2a) L *->REACHABLE
714 * 1 1 y y (2a) L *->REACHABLE
716 if (!is_override && (lladdr && llchange)) { /* (1) */
718 * If state is REACHABLE, make it STALE.
719 * no other updates should be done.
721 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
722 ln->ln_state = ND6_LLINFO_STALE;
723 ln->ln_expire = time_second + nd6_gctimer;
725 goto freeit;
726 } else if (is_override /* (2a) */
727 || (!is_override && (lladdr && !llchange)) /* (2b) */
728 || !lladdr) { /* (2c) */
730 * Update link-local address, if any.
732 if (lladdr) {
733 sdl->sdl_alen = ifp->if_addrlen;
734 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
738 * If solicited, make the state REACHABLE.
739 * If not solicited and the link-layer address was
740 * changed, make it STALE.
742 if (is_solicited) {
743 ln->ln_state = ND6_LLINFO_REACHABLE;
744 ln->ln_byhint = 0;
745 if (ln->ln_expire) {
746 ln->ln_expire = time_second +
747 ND_IFINFO(ifp)->reachable;
749 } else {
750 if (lladdr && llchange) {
751 ln->ln_state = ND6_LLINFO_STALE;
752 ln->ln_expire = time_second + nd6_gctimer;
757 if (ln->ln_router && !is_router) {
759 * The peer dropped the router flag.
760 * Remove the sender from the Default Router List and
761 * update the Destination Cache entries.
763 struct nd_defrouter *dr;
764 struct in6_addr *in6;
766 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
769 * Lock to protect the default router list.
770 * XXX: this might be unnecessary, since this function
771 * is only called under the network software interrupt
772 * context. However, we keep it just for safety.
774 crit_enter();
775 dr = defrouter_lookup(in6, rt->rt_ifp);
776 if (dr)
777 defrtrlist_del(dr);
778 else if (!ip6_forwarding && ip6_accept_rtadv) {
780 * Even if the neighbor is not in the default
781 * router list, the neighbor may be used
782 * as a next hop for some destinations
783 * (e.g. redirect case). So we must
784 * call rt6_flush explicitly.
786 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
788 crit_exit();
790 ln->ln_router = is_router;
792 rt->rt_flags &= ~RTF_REJECT;
793 ln->ln_asked = 0;
794 if (ln->ln_hold) {
796 * we assume ifp is not a loopback here, so just set the 2nd
797 * argument as the 1st one.
799 nd6_output(ifp, ifp, ln->ln_hold,
800 (struct sockaddr_in6 *)rt_key(rt), rt);
801 ln->ln_hold = 0;
804 freeit:
805 m_freem(m);
806 return;
808 bad:
809 icmp6stat.icp6s_badna++;
810 m_freem(m);
814 * Neighbor advertisement output handling.
816 * Based on RFC 2461
818 * the following items are not implemented yet:
819 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
820 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
822 void
823 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6,
824 const struct in6_addr *taddr6, u_long flags,
825 int tlladdr, /* 1 if include target link-layer address */
826 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
828 struct mbuf *m;
829 struct ip6_hdr *ip6;
830 struct nd_neighbor_advert *nd_na;
831 struct in6_ifaddr *ia = NULL;
832 struct ip6_moptions im6o;
833 int icmp6len;
834 int maxlen;
835 caddr_t mac;
836 struct ifnet *outif = NULL;
838 /* estimate the size of message */
839 maxlen = sizeof(*ip6) + sizeof(*nd_na);
840 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
841 if (max_linkhdr + maxlen >= MCLBYTES) {
842 #ifdef DIAGNOSTIC
843 kprintf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
844 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
845 #endif
846 return;
849 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
850 if (m == NULL)
851 return;
853 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
854 m->m_flags |= M_MCAST;
855 im6o.im6o_multicast_ifp = ifp;
856 im6o.im6o_multicast_hlim = 255;
857 im6o.im6o_multicast_loop = 0;
860 icmp6len = sizeof(*nd_na);
861 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
862 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
864 /* fill neighbor advertisement packet */
865 ip6 = mtod(m, struct ip6_hdr *);
866 ip6->ip6_flow = 0;
867 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
868 ip6->ip6_vfc |= IPV6_VERSION;
869 ip6->ip6_nxt = IPPROTO_ICMPV6;
870 ip6->ip6_hlim = 255;
871 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
872 /* reply to DAD */
873 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
874 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
875 ip6->ip6_dst.s6_addr32[1] = 0;
876 ip6->ip6_dst.s6_addr32[2] = 0;
877 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
878 flags &= ~ND_NA_FLAG_SOLICITED;
879 } else
880 ip6->ip6_dst = *daddr6;
883 * Select a source whose scope is the same as that of the dest.
885 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
886 if (ia == NULL) {
887 m_freem(m);
888 return;
890 ip6->ip6_src = ia->ia_addr.sin6_addr;
891 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
892 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
893 nd_na->nd_na_code = 0;
894 nd_na->nd_na_target = *taddr6;
895 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
896 nd_na->nd_na_target.s6_addr16[1] = 0;
899 * "tlladdr" indicates NS's condition for adding tlladdr or not.
900 * see nd6_ns_input() for details.
901 * Basically, if NS packet is sent to unicast/anycast addr,
902 * target lladdr option SHOULD NOT be included.
904 mac = NULL;
905 if (tlladdr) {
907 * sdl0 != NULL indicates proxy NA. If we do proxy, use
908 * lladdr in sdl0. If we are not proxying (sending NA for
909 * my address) use lladdr configured for the interface.
911 if (sdl0 == NULL) {
912 #ifdef CARP
913 if (ifp->if_carp)
914 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
915 if (mac == NULL)
916 mac = nd6_ifptomac(ifp);
917 #else
918 mac = nd6_ifptomac(ifp);
919 #endif
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);
927 if (mac != NULL) {
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);
931 /* roundup to 8 bytes alignment! */
932 optlen = (optlen + 7) & ~7;
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;
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);
950 ip6_output(m, NULL, NULL, 0, &im6o, &outif, NULL);
951 if (outif) {
952 icmp6_ifstat_inc(outif, ifs6_out_msg);
953 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
955 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
958 caddr_t
959 nd6_ifptomac(struct ifnet *ifp)
961 switch (ifp->if_type) {
962 case IFT_ETHER:
963 case IFT_IEEE1394:
964 #ifdef IFT_L2VLAN
965 case IFT_L2VLAN:
966 #endif
967 #ifdef IFT_IEEE80211
968 case IFT_IEEE80211:
969 #endif
970 #ifdef IFT_CARP
971 case IFT_CARP:
972 #endif
973 return ((caddr_t)(ifp + 1));
974 break;
975 default:
976 return NULL;
980 TAILQ_HEAD(dadq_head, dadq);
981 struct dadq {
982 TAILQ_ENTRY(dadq) dad_list;
983 struct ifaddr *dad_ifa;
984 int dad_count; /* max NS to send */
985 int dad_ns_tcount; /* # of trials to send NS */
986 int dad_ns_ocount; /* NS sent so far */
987 int dad_ns_icount;
988 int dad_na_icount;
989 struct callout dad_timer_ch;
992 static struct dadq_head dadq;
993 static int dad_init = 0;
995 static struct dadq *
996 nd6_dad_find(struct ifaddr *ifa)
998 struct dadq *dp;
1000 TAILQ_FOREACH(dp, &dadq, dad_list) {
1001 if (dp->dad_ifa == ifa)
1002 return dp;
1004 return NULL;
1007 static void
1008 nd6_dad_starttimer(struct dadq *dp, int ticks)
1011 callout_reset(&dp->dad_timer_ch, ticks,
1012 (void (*) (void *))nd6_dad_timer, (void *)dp->dad_ifa);
1015 static void
1016 nd6_dad_stoptimer(struct dadq *dp)
1019 callout_stop(&dp->dad_timer_ch);
1023 * Start Duplicated Address Detection (DAD) for specified interface address.
1025 void
1026 nd6_dad_start(struct ifaddr *ifa,
1027 int *tick) /* minimum delay ticks for IFF_UP event */
1029 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1030 struct dadq *dp;
1032 if (!dad_init) {
1033 TAILQ_INIT(&dadq);
1034 dad_init++;
1038 * If we don't need DAD, don't do it.
1039 * There are several cases:
1040 * - DAD is disabled (ip6_dad_count == 0)
1041 * - the interface address is anycast
1043 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1044 log(LOG_DEBUG,
1045 "nd6_dad_start: called with non-tentative address "
1046 "%s(%s)\n",
1047 ip6_sprintf(&ia->ia_addr.sin6_addr),
1048 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1049 return;
1051 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1052 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1053 return;
1055 if (!ip6_dad_count) {
1056 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1057 return;
1059 if (!ifa->ifa_ifp)
1060 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1061 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1062 return;
1063 if (nd6_dad_find(ifa) != NULL) {
1064 /* DAD already in progress */
1065 return;
1068 dp = kmalloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1069 if (dp == NULL) {
1070 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1071 "%s(%s)\n",
1072 ip6_sprintf(&ia->ia_addr.sin6_addr),
1073 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1074 return;
1076 callout_init(&dp->dad_timer_ch);
1077 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1079 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1080 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1083 * Send NS packet for DAD, ip6_dad_count times.
1084 * Note that we must delay the first transmission, if this is the
1085 * first packet to be sent from the interface after interface
1086 * (re)initialization.
1088 dp->dad_ifa = ifa;
1089 crit_enter(); /* XXX MP not MP safe */
1090 _IFAREF(ifa, 0); /* just for safety */
1091 crit_exit();
1092 dp->dad_count = ip6_dad_count;
1093 dp->dad_ns_icount = dp->dad_na_icount = 0;
1094 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1095 if (tick == NULL) {
1096 nd6_dad_ns_output(dp, ifa);
1097 nd6_dad_starttimer(dp,
1098 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1099 } else {
1100 int ntick;
1102 if (*tick == 0)
1103 ntick = krandom() % (MAX_RTR_SOLICITATION_DELAY * hz);
1104 else
1105 ntick = *tick + krandom() % (hz / 2);
1106 *tick = ntick;
1107 nd6_dad_starttimer(dp, ntick);
1112 * terminate DAD unconditionally. used for address removals.
1114 void
1115 nd6_dad_stop(struct ifaddr *ifa)
1117 struct dadq *dp;
1119 if (!dad_init)
1120 return;
1121 dp = nd6_dad_find(ifa);
1122 if (!dp) {
1123 /* DAD wasn't started yet */
1124 return;
1127 nd6_dad_stoptimer(dp);
1129 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1130 kfree(dp, M_IP6NDP);
1131 dp = NULL;
1132 crit_enter(); /* XXX MP not MP safe */
1133 _IFAFREE(ifa, 0);
1134 crit_exit();
1137 static void
1138 nd6_dad_timer(struct ifaddr *ifa)
1140 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1141 struct dadq *dp;
1143 crit_enter(); /* XXX */
1145 /* Sanity check */
1146 if (ia == NULL) {
1147 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1148 goto done;
1150 dp = nd6_dad_find(ifa);
1151 if (dp == NULL) {
1152 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1153 goto done;
1155 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1156 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1157 "%s(%s)\n",
1158 ip6_sprintf(&ia->ia_addr.sin6_addr),
1159 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1160 goto done;
1162 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1163 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1164 "%s(%s)\n",
1165 ip6_sprintf(&ia->ia_addr.sin6_addr),
1166 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1167 goto done;
1170 /* timeouted with IFF_{RUNNING,UP} check */
1171 if (dp->dad_ns_tcount > dad_maxtry) {
1172 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1173 if_name(ifa->ifa_ifp)));
1175 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1176 kfree(dp, M_IP6NDP);
1177 dp = NULL;
1178 crit_enter(); /* XXX MP not MP safe */
1179 _IFAFREE(ifa, 0);
1180 crit_exit();
1181 goto done;
1184 /* Need more checks? */
1185 if (dp->dad_ns_ocount < dp->dad_count) {
1187 * We have more NS to go. Send NS packet for DAD.
1189 nd6_dad_ns_output(dp, ifa);
1190 nd6_dad_starttimer(dp,
1191 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1192 } else {
1194 * We have transmitted sufficient number of DAD packets.
1195 * See what we've got.
1197 int duplicate;
1199 duplicate = 0;
1201 if (dp->dad_na_icount) {
1203 * the check is in nd6_dad_na_input(),
1204 * but just in case
1206 duplicate++;
1209 if (dp->dad_ns_icount) {
1210 #if 0 /* heuristics */
1212 * if
1213 * - we have sent many(?) DAD NS, and
1214 * - the number of NS we sent equals to the
1215 * number of NS we've got, and
1216 * - we've got no NA
1217 * we may have a faulty network card/driver which
1218 * loops back multicasts to myself.
1220 if (3 < dp->dad_count
1221 && dp->dad_ns_icount == dp->dad_count
1222 && dp->dad_na_icount == 0) {
1223 log(LOG_INFO, "DAD questionable for %s(%s): "
1224 "network card loops back multicast?\n",
1225 ip6_sprintf(&ia->ia_addr.sin6_addr),
1226 if_name(ifa->ifa_ifp));
1227 /* XXX consider it a duplicate or not? */
1228 /* duplicate++; */
1229 } else {
1230 /* We've seen NS, means DAD has failed. */
1231 duplicate++;
1233 #else
1234 /* We've seen NS, means DAD has failed. */
1235 duplicate++;
1236 #endif
1239 if (duplicate) {
1240 /* (*dp) will be freed in nd6_dad_duplicated() */
1241 dp = NULL;
1242 nd6_dad_duplicated(ifa);
1243 } else {
1245 * We are done with DAD. No NA came, no NS came.
1246 * duplicated address found.
1248 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1250 nd6log((LOG_DEBUG,
1251 "%s: DAD complete for %s - no duplicates found\n",
1252 if_name(ifa->ifa_ifp),
1253 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1255 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1256 kfree(dp, M_IP6NDP);
1257 dp = NULL;
1258 _IFAFREE(ifa, 0);
1262 done:
1263 crit_exit();
1266 void
1267 nd6_dad_duplicated(struct ifaddr *ifa)
1269 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1270 struct dadq *dp;
1272 dp = nd6_dad_find(ifa);
1273 if (dp == NULL) {
1274 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1275 return;
1278 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1279 "NS in/out=%d/%d, NA in=%d\n",
1280 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1281 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1283 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1284 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1286 /* We are done with DAD, with duplicated address found. (failure) */
1287 nd6_dad_stoptimer(dp);
1289 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1290 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1291 log(LOG_ERR, "%s: manual intervention required\n",
1292 if_name(ifa->ifa_ifp));
1294 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1295 kfree(dp, M_IP6NDP);
1296 dp = NULL;
1297 crit_enter(); /* XXX MP not MP safe */
1298 _IFAFREE(ifa, 0);
1299 crit_exit();
1302 static void
1303 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1305 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1306 struct ifnet *ifp = ifa->ifa_ifp;
1308 dp->dad_ns_tcount++;
1309 if (!(ifp->if_flags & IFF_UP)) {
1310 #if 0
1311 kprintf("%s: interface down?\n", if_name(ifp));
1312 #endif
1313 return;
1315 if (!(ifp->if_flags & IFF_RUNNING)) {
1316 #if 0
1317 kprintf("%s: interface not running?\n", if_name(ifp));
1318 #endif
1319 return;
1322 dp->dad_ns_ocount++;
1323 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1326 static void
1327 nd6_dad_ns_input(struct ifaddr *ifa)
1329 struct in6_ifaddr *ia;
1330 struct ifnet *ifp;
1331 const struct in6_addr *taddr6;
1332 struct dadq *dp;
1333 int duplicate;
1335 if (!ifa)
1336 panic("ifa == NULL in nd6_dad_ns_input");
1338 ia = (struct in6_ifaddr *)ifa;
1339 ifp = ifa->ifa_ifp;
1340 taddr6 = &ia->ia_addr.sin6_addr;
1341 duplicate = 0;
1342 dp = nd6_dad_find(ifa);
1344 /* Quickhack - completely ignore DAD NS packets */
1345 if (dad_ignore_ns) {
1346 nd6log((LOG_INFO,
1347 "nd6_dad_ns_input: ignoring DAD NS packet for "
1348 "address %s(%s)\n", ip6_sprintf(taddr6),
1349 if_name(ifa->ifa_ifp)));
1350 return;
1354 * if I'm yet to start DAD, someone else started using this address
1355 * first. I have a duplicate and you win.
1357 if (!dp || dp->dad_ns_ocount == 0)
1358 duplicate++;
1360 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1362 if (duplicate) {
1363 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1364 nd6_dad_duplicated(ifa);
1365 } else {
1367 * not sure if I got a duplicate.
1368 * increment ns count and see what happens.
1370 if (dp)
1371 dp->dad_ns_icount++;
1375 static void
1376 nd6_dad_na_input(struct ifaddr *ifa)
1378 struct dadq *dp;
1380 if (!ifa)
1381 panic("ifa == NULL in nd6_dad_na_input");
1383 dp = nd6_dad_find(ifa);
1384 if (dp)
1385 dp->dad_na_icount++;
1387 /* remove the address. */
1388 nd6_dad_duplicated(ifa);